DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_BILLS_RECEIVABLE_MAIN

Source


1 PACKAGE BODY ARP_BILLS_RECEIVABLE_MAIN AS
2 /* $Header: ARBRACMB.pls 120.13 2006/02/07 00:42:12 hyu arhmapss.pls $ */
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   g_event                       VARCHAR2(30);
15   g_previous_status             VARCHAR2(30);
16 
17 /* =======================================================================
18  | Source Table constants
19  * ======================================================================*/
20   C_TH             CONSTANT VARCHAR2(14) := 'TH';
21   C_CTL            CONSTANT VARCHAR2(14) := 'CTL';
22 
23 /* =======================================================================
24  | Source Type constants
25  * ======================================================================*/
26   C_REC             CONSTANT VARCHAR2(14) := 'REC';
27   C_UNPAIDREC       CONSTANT VARCHAR2(14) := 'UNPAIDREC';
28   C_FACTOR          CONSTANT VARCHAR2(14) := 'FACTOR';
29   C_REMITTANCE      CONSTANT VARCHAR2(14) := 'REMITTANCE';
30   C_TAX             CONSTANT VARCHAR2(14) := 'TAX';
31   C_DEFERRED_TAX    CONSTANT VARCHAR2(14) := 'DEFERRED_TAX';
32 
33 /* =======================================================================
34  | Source Secondary Type constant
35  * ======================================================================*/
36   C_ASSIGNMENT      CONSTANT VARCHAR2(14) := 'ASSIGNMENT';
37 
38 /* =======================================================================
39  | Bills Receivable status constants
40  * ======================================================================*/
41   C_INCOMPLETE         CONSTANT VARCHAR2(20) := 'INCOMPLETE';
42   C_PENDING_ACCEPTANCE CONSTANT VARCHAR2(20) := 'PENDING_ACCEPTANCE';
43   C_PENDING_REMITTANCE CONSTANT VARCHAR2(20) := 'PENDING_REMITTANCE';
44   C_REMITTED           CONSTANT VARCHAR2(20) := 'REMITTED';
45   C_FACTORED           CONSTANT VARCHAR2(20) := 'FACTORED';
46   C_UNPAID             CONSTANT VARCHAR2(20) := 'UNPAID';
47   C_CANCELLED          CONSTANT VARCHAR2(20) := 'CANCELLED';
48   C_ENDORSED           CONSTANT VARCHAR2(20) := 'ENDORSED';
49 
50 /* =======================================================================
51  | Bills Receivable event constants
52  * ======================================================================*/
53   C_MATURITY_DATE      CONSTANT VARCHAR2(20) := 'MATURITY_DATE';
54   C_RECALLED           CONSTANT VARCHAR2(20) := 'RECALLED';
55 
56 /* =======================================================================
57  | Private Procedure/Function prototypes
58  * ======================================================================*/
59   PROCEDURE Init_Ae_Lines;
60 
61   PROCEDURE Derive_Accounting_Entry;
62 
63   PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
64 
65 PROCEDURE Delete_TH(p_ae_deleted   OUT NOCOPY BOOLEAN);
66 
67   PROCEDURE Reverse_BR;
68 
69   PROCEDURE Get_Doc_Entitity_Data (
70                 p_level          IN  VARCHAR2                           ,
71                 p_app_rec        OUT NOCOPY ar_receivable_applications%ROWTYPE ,
72                 p_cust_inv_rec   OUT NOCOPY ra_customer_trx%ROWTYPE            ,
73                 p_trh_rec        OUT NOCOPY ar_transaction_history%ROWTYPE     , -- jrautiai
74                 p_rule_rec       OUT NOCOPY ae_rule_rec_type                     );
75 
76   PROCEDURE Create_Ae_Lines_Common (
77                 p_level         IN VARCHAR2 );
78 
79   PROCEDURE Create_Ae_Lines_BR(
80                 p_app_rec        IN ar_receivable_applications%ROWTYPE ,
81                 p_cust_inv_rec   IN ra_customer_trx%ROWTYPE            ,
82                 p_trh_rec        IN ar_transaction_history%ROWTYPE     , -- jrautiai
83                 p_rule_rec       IN ae_rule_rec_type                     );
84 
85   PROCEDURE Assign_Ael_Elements(
86                 p_ae_line_rec           IN ae_line_rec_type );
87 
88 
89   PROCEDURE initialize_global_variables(p_trh_rec IN ar_transaction_history%ROWTYPE);
90 
91   FUNCTION trx_history_status(p_transaction_history_id  IN ar_transaction_history.transaction_history_id%TYPE) RETURN VARCHAR2;
92 
93   PROCEDURE create_exchanged_trx_acct(p_customer_trx_id IN ra_customer_trx.customer_trx_id%TYPE,
94                                       p_ae_rule_rec     IN ae_rule_rec_type);
95 
96   FUNCTION find_exchanged_trx_acct(p_transaction_history_id      IN ar_transaction_history.transaction_history_id%TYPE) RETURN NUMBER;
97 
98   PROCEDURE reverse_exchanged_trx_acct(p_transaction_history_id  IN  ar_transaction_history.transaction_history_id%TYPE);
99 
100   PROCEDURE reverse_single_dist_line(p_dist_rec   IN ar_distributions%ROWTYPE);
101 
102   PROCEDURE reverse_old_acct(p_transaction_history_id IN ar_transaction_history.transaction_history_id%TYPE,
103                              p_ps_id                  IN ar_payment_schedules.payment_schedule_id%TYPE);
104 
105   PROCEDURE find_rec_dist_record(p_transaction_history_id IN  ar_transaction_history.transaction_history_id%TYPE,
106                                  p_sign                   IN  NUMBER,
107                                  p_dist_rec               OUT NOCOPY ar_distributions%ROWTYPE);
108 
109 
110   PROCEDURE find_prev_posted_hist_record(p_transaction_history_id IN  ar_transaction_history.transaction_history_id%TYPE,
111                                          p_trh_rec                OUT NOCOPY ar_transaction_history%ROWTYPE,
112                                          p_status                 IN  ar_transaction_history.status%TYPE DEFAULT NULL,
113                                          p_event                  IN  ar_transaction_history.event%TYPE DEFAULT NULL);
114 
115   PROCEDURE reverse_deferred_tax(p_transaction_history_id ar_transaction_history.transaction_history_id%TYPE);
116 
117 /* =======================================================================
118  | Procedures/functions
119  * ======================================================================*/
120 /* =======================================================================
121  | PUBLIC PROCEDURE Delete_Acct
122  |
123  | DESCRIPTION
124  |      Accounting Entry Deletion
125  |      -------------------------
126  |      This procedure is the Accounting Entry deletion routine which
127  |      deletes the accounting associated with Transaction history source
128  |      table.
129  |
130  | PARAMETERS
131  |      p_mode          IN      Document or Accounting Event mode
132  |      p_ae_doc_rec    IN      Document Record
133  |      p_ae_event_rec  IN      Event Record
134  |      p_ae_deleted    OUT NOCOPY     AE Lines deletion status
135  * ======================================================================*/
136 PROCEDURE Delete_Acct( p_mode         IN  VARCHAR2,
137                        p_ae_doc_rec   IN  OUT NOCOPY ae_doc_rec_type,
138                        p_ae_event_rec IN  ae_event_rec_type,
139                        p_ae_deleted   OUT NOCOPY BOOLEAN            ) IS
140 
141 
142 BEGIN
143   IF PG_DEBUG in ('Y', 'C') THEN
144      arp_standard.debug(   'ARP_BILLS_RECEIVABLE_MAIN.Delete_Acct()+');
145   END IF;
146 
147   /*----------------------------------------------------+
148    | Copy Document/Event Data to Global                 |
149    +----------------------------------------------------*/
150   g_mode         := p_mode;
151   g_ae_doc_rec   := p_ae_doc_rec;
152   g_ae_event_rec := p_ae_event_rec;
153 
154   IF ( g_ae_doc_rec.source_table = C_TH ) THEN
155 
156      Delete_TH(p_ae_deleted => p_ae_deleted) ;
157 
158   END IF;
159 
160   IF PG_DEBUG in ('Y', 'C') THEN
161      arp_standard.debug(   'ARP_BILLS_RECEIVABLE_MAIN.Delete_Acct()-');
162   END IF;
163 
164 EXCEPTION
165   WHEN OTHERS THEN
166      IF PG_DEBUG in ('Y', 'C') THEN
167         arp_standard.debug(  'EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Delete_Acct');
168      END IF;
169      RAISE;
170 
171 END Delete_Acct;
172 
173 /* =======================================================================
174  | PUBLIC PROCEDURE Execute
175  |
176  | DESCRIPTION
177  | 	Accounting Entry Derivation Method
178  | 	----------------------------------
179  | 	This procedure is the Accounting Entry derivation method for all
180  | 	accounting events associated with the receivable applications layer.
181  |
182  | 	Functions of the AE Derivation Method are:
183  | 		- Single Entry Point for easy extensibility
184  | 		- Read Event Data
185  | 		- Read Transaction and Setup Data
186  | 		- Determine AE Lines affected
187  | 		- Derive AE Lines
188  | 		- Return AE Lines created in a PL/SQL table.
189  |
190  | PARAMETERS
191  |      p_mode          IN      Document or Accounting Event mode
192  |      p_ae_doc_rec    IN      Document Record
193  | 	p_ae_event_rec	IN	Event Record
194  | 	p_ae_line_tbl	OUT NOCOPY	AE Lines table
195  | 	p_ae_created	OUT NOCOPY	AE Lines creation status
196  * ======================================================================*/
197 PROCEDURE Execute( p_mode                IN VARCHAR2,
198                    p_ae_doc_rec          IN ae_doc_rec_type,
199                    p_ae_event_rec        IN ae_event_rec_type,
200                    p_ae_line_tbl         OUT NOCOPY ae_line_tbl_type,
201                    p_ae_created          OUT NOCOPY BOOLEAN) IS
202 
203 
204 BEGIN
205   IF PG_DEBUG in ('Y', 'C') THEN
206      arp_standard.debug(   'ARP_BILLS_RECEIVABLE_MAIN.Execute()+');
207   END IF;
208   /*------------------------------------------------------+
209    | Initialize Accounting Entry Header and Lines	  |
210    +------------------------------------------------------*/
211   g_ae_line_ctr := 0;
212   Init_Ae_Lines;
213 
214   /*---------------------------------------------------------------+
215    | Copy Document/Event Data to Global, derive System Options info|
216    +---------------------------------------------------------------*/
217   g_mode 	 := p_mode;
218   g_ae_doc_rec 	 := p_ae_doc_rec;
219   g_ae_event_rec := p_ae_event_rec;
220 
221   --Get system options info
222 
223   g_ae_sys_rec.set_of_books_id   := ARP_ACCT_MAIN.ae_sys_rec.set_of_books_id;
224   g_ae_sys_rec.gain_cc_id        := ARP_ACCT_MAIN.ae_sys_rec.gain_cc_id;
225   g_ae_sys_rec.loss_cc_id        := ARP_ACCT_MAIN.ae_sys_rec.loss_cc_id;
226   g_ae_sys_rec.round_cc_id       := ARP_ACCT_MAIN.ae_sys_rec.round_cc_id;
227   g_ae_sys_rec.coa_id            := ARP_ACCT_MAIN.ae_sys_rec.coa_id;
228   g_ae_sys_rec.base_currency     := ARP_ACCT_MAIN.ae_sys_rec.base_currency;
229   g_ae_sys_rec.base_precision    := ARP_ACCT_MAIN.ae_sys_rec.base_precision;
230   g_ae_sys_rec.base_min_acc_unit := ARP_ACCT_MAIN.ae_sys_rec.base_min_acc_unit;
231 
232   -- MRC Trigger Replacement: Initialize new global variable.
233   g_ae_sys_rec.sob_type          := NVL(ARP_ACCT_MAIN.ae_sys_rec.sob_type,'P');
234 
235   /*------------------------------------------------------+
236    | Derive Accounting Entry                              |
237    +------------------------------------------------------*/
238   Derive_Accounting_Entry;
239 
240   /*------------------------------------------------------+
241    | Return Accounting Entry Creation Status              |
242    +------------------------------------------------------*/
243   p_ae_line_tbl := g_ae_line_tbl;
244 
245   IF g_ae_line_tbl.EXISTS(g_ae_line_ctr) THEN
246 
247     IF PG_DEBUG in ('Y', 'C') THEN
248        arp_standard.debug(  'bills receivable... p_ea_created = TRUE');
249     END IF;
250     p_ae_created := TRUE;
251 
252   ELSE
253 
254     IF PG_DEBUG in ('Y', 'C') THEN
255        arp_standard.debug(  'bills receivable... p_ea_created = FALSE');
256     END IF;
257     p_ae_created := FALSE;
258 
259   END IF;
260 
261   IF PG_DEBUG in ('Y', 'C') THEN
262      arp_standard.debug(   'ARP_BILLS_RECEIVABLE_MAIN.Execute()-');
263   END IF;
264 
265 EXCEPTION
266   WHEN NO_DATA_FOUND THEN
267      IF PG_DEBUG in ('Y', 'C') THEN
268         arp_standard.debug(  'ARP_BILLS_RECEIVABLE_MAIN.Execute - NO_DATA_FOUND' );
269      END IF;
270      RAISE;
271 
272   WHEN OTHERS THEN
273      IF PG_DEBUG in ('Y', 'C') THEN
274         arp_standard.debug(  'EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Execute');
275      END IF;
276      RAISE;
277 
278 END Execute;
279 
280 
281 /* =======================================================================
282  |
283  | PROCEDURE Init_Ae_Lines
284  |
285  | DESCRIPTION
286  |      Initialises the Global lines table
287  |
288  | PARAMETERS
289  |      NONE
290  * ======================================================================*/
291 PROCEDURE Init_Ae_Lines IS
292 
293 BEGIN
294 
295   IF PG_DEBUG in ('Y', 'C') THEN
296      arp_standard.debug(   'ARP_BILLS_RECEIVABLE_MAIN.Init_Ae_Lines()+');
297   END IF;
298 
299   g_ae_line_tbl := g_empty_ae_line_tbl;
300 
301   IF PG_DEBUG in ('Y', 'C') THEN
302      arp_standard.debug(   'ARP_BILLS_RECEIVABLE_MAIN.Init_Ae_Lines()-');
303   END IF;
304 EXCEPTION
305 
306   WHEN OTHERS THEN
307      IF PG_DEBUG in ('Y', 'C') THEN
308         arp_standard.debug(  'EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Init_Ae_Lines');
309      END IF;
310      RAISE;
311 
312 END Init_Ae_Lines;
313 
314 
315 /* =======================================================================
316  |
317  | PROCEDURE Derive_Accounting_Entry
318  |
319  | DESCRIPTION
320  |      This procedure gets the necessary transaction data and determines
321  |      the accounting entries to be created at each of entity level.
322  |
323  | PARAMETERS
324  | 	Event_Rec	Global Event Record
325  * ======================================================================*/
326 PROCEDURE Derive_Accounting_Entry IS
327 
328 BEGIN
329   IF PG_DEBUG in ('Y', 'C') THEN
330      arp_standard.debug(   'ARP_BILLS_RECEIVABLE_MAIN.Derive_Accounting_Entry()+');
331   END IF;
332 
333   /*------------------------------------------------------------+
334    | Create Accounting Entries at the Document Entity level.    |
335    +------------------------------------------------------------*/
336   IF ( g_ae_doc_rec.accounting_entity_level = 'ONE' ) THEN
337 
338      IF ((g_ae_doc_rec.source_id_old IS NOT NULL) and (g_ae_doc_rec.other_flag = 'REVERSE')) THEN
339 
340        /*------------------------------------------------------------+
341         | Reverse Accounting Entry Lines for Transaction History     |
342         | accounting                                                 |
343         +------------------------------------------------------------*/
344        Reverse_BR;
345 
346      ELSIF ( g_ae_doc_rec.source_table = C_TH ) THEN
347 
348        /*---------------------------------------------------------+
349         | Create Accounting Entry Lines, Bills Receivable         |
350         +---------------------------------------------------------*/
351        Create_Ae_Lines_Common(p_level => C_TH);
352 
353      END IF;
354 
355   END IF;		-- accounting_entity_level = ONE
356 
357   /*------------------------------------------------------+
358    | Create Accounting Entries at the Document Level      |
359    | (All Entities)                                       |
360    +------------------------------------------------------*/
361   IF ( g_ae_doc_rec.accounting_entity_level = 'ALL' ) THEN
362 
363 	NULL;
364 
365   END IF;		-- accounting_entity_level = ALL?
366 
367   IF PG_DEBUG in ('Y', 'C') THEN
368      arp_standard.debug(   'ARP_BILLS_RECEIVABLE_MAIN.Derive_Accounting_Entry()-');
369   END IF;
370 
371 EXCEPTION
372   WHEN OTHERS THEN
373      IF PG_DEBUG in ('Y', 'C') THEN
374         arp_standard.debug(  'EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Derive_Accounting_Entry');
375      END IF;
376      RAISE;
377 END Derive_Accounting_Entry;
378 
379 /* =======================================================================
380  |
381  | PROCEDURE Delete_TH
382  |
383  | DESCRIPTION
384  |      Deletes accounting associated with a Transaction History id
385  |      from the AR_DISTRIBUTIONS table.This routine deletes all records
386  |      matching the input source_id. Note records from child table
387  |      (AR_DISTRIBUTIONS) be deleted first.
388  | NOTE
389  |      If this routine is called there must be accounting for the
390  |      parent Transaction history id in the distributions table
391  |      otherwise a NO_DATA_FOUND exception will be raised. In other
392  |      words the routine call should be valid.
393  |
394  | PARAMETERS
395  |      p_ae_deleted       indicates whether records were deleted
396  |                         for source_id
397  * ======================================================================*/
398 PROCEDURE Delete_TH(p_ae_deleted          OUT NOCOPY BOOLEAN) IS
399 
400 l_trans_hist            ar_transaction_history.transaction_history_id%TYPE;
401 
402 -- MRC Trigger Replacement:
403 l_ar_dist_key_value_list   gl_ca_utility_pkg.r_key_value_arr;
404 
405 BEGIN
406     IF PG_DEBUG in ('Y', 'C') THEN
407        arp_standard.debug('ARP_BILLS_RECEIVABLE_MAIN.Delete_TH()+');
408     END IF;
409 
410  /*-------------------------------------------------------------------+
411   | Verify that the source id is a valid candidate for deletion       |
412   +-------------------------------------------------------------------*/
413     SELECT th.transaction_history_id
414     INTO   l_trans_hist
415     FROM   ar_transaction_history th
416     WHERE  th.transaction_history_id = g_ae_doc_rec.source_id
417     AND    th.gl_posted_date is null
418     AND    th.posting_control_id = -3
419     AND    th.postable_flag = 'Y'
420     AND    g_ae_doc_rec.source_table = C_TH;
421 
422  /*-------------------------------------------------------------------+
423   | Delete all accounting for source id and source table combination  |
424   | if valid candidate for deletion, if not then a NO_DATA_FOUND      |
425   | exception will be raised by the above select statement.           |
426   +-------------------------------------------------------------------*/
427     DELETE FROM AR_DISTRIBUTIONS
428     WHERE  source_id    = g_ae_doc_rec.source_id
429     AND    source_table = C_TH
430     RETURNING line_id
431     BULK COLLECT INTO l_ar_dist_key_value_list;
432 
433     /*---------------------------------+
434      | Calling central MRC library     |
435      | for MRC Integration             |
436      +---------------------------------*/
437 --{BUG4301323
438 --     ar_mrc_engine.maintain_mrc_data(
439 --               p_event_mode        => 'DELETE',
440 --               p_table_name        => 'AR_DISTRIBUTIONS',
441 --               p_mode              => 'BATCH',
442 --               p_key_value_list    => l_ar_dist_key_value_list);
443 --}
444 
445     IF PG_DEBUG in ('Y', 'C') THEN
446        arp_standard.debug('ARP_BILLS_RECEIVABLE_MAIN.Delete_TH()-');
447     END IF;
448 
449 EXCEPTION
450   WHEN NO_DATA_FOUND THEN
451      IF PG_DEBUG in ('Y', 'C') THEN
452         arp_standard.debug('ARP_BILLS_RECEIVABLE_MAIN.Delete_TH - NO_DATA_FOUND' );
453      END IF;
454      p_ae_deleted := FALSE;
455      RAISE;
456 
457   WHEN OTHERS THEN
458      IF PG_DEBUG in ('Y', 'C') THEN
459         arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Delete_TH');
460      END IF;
461      p_ae_deleted := FALSE;
462      RAISE ;
463 
464 END Delete_TH;
465 
466 /* =======================================================================
467  |
468  | PROCEDURE Reverse_BR
469  |
470  | DESCRIPTION
471  |      This procedure reverses the records in AR_DISTRIBUTIONS for a
472  |      accounting associated with a Bills Receivable Transaction history.
473  |
474  | PARAMETERS
475  |      None
476  * ======================================================================*/
477 PROCEDURE Reverse_BR  IS
478 
479 -- MRC Trigger Replacement:  Enumerate all columns and add union
480 
481 CURSOR get_old_ard  IS
482        select ard.line_id,
483               ard.source_id,
484               ard.source_table,
485               ard.source_type,
486               ard.code_combination_id,
487               ard.amount_dr,
488               ard.amount_cr,
489               ard.acctd_amount_dr,
490               ard.acctd_amount_cr,
491               ard.creation_date,
492               ard.created_by,
493               ard.last_updated_by,
494               ard.last_update_date,
495               ard.last_update_login,
496               ard.org_id,
497               ard.source_table_secondary,
498               ard.source_id_secondary,
499               ard.currency_code,
500               ard.currency_conversion_rate,
501               ard.currency_conversion_type,
502               ard.currency_conversion_date,
503               ard.taxable_entered_dr,
504               ard.taxable_entered_cr,
505               ard.taxable_accounted_dr,
506               ard.taxable_accounted_cr,
507               ard.tax_link_id,
508               ard.third_party_id,
509               ard.third_party_sub_id,
510               ard.reversed_source_id,
511               ard.tax_code_id,
512               ard.location_segment_id,
513               ard.source_type_secondary,
514               ard.tax_group_code_id,
515               --{BUG#2979254
516               ard.ref_customer_trx_line_id,
517               ard.ref_cust_trx_line_gl_dist_id,
518               ard.ref_line_id,
519               ard.from_amount_dr,
520               ard.from_amount_cr,
521               ard.from_acctd_amount_dr,
522               ard.from_acctd_amount_cr
523               --}
524        from   ar_distributions ard
525        where  NVL(g_ae_sys_rec.sob_type,'P') = 'P'
526          and  ard.source_id = g_ae_doc_rec.source_id_old
527          and    ard.source_table = g_ae_doc_rec.source_table
528        order  by line_id ;
529 /*BUG4301323
530       UNION
531        select ard.line_id,
532               ard.source_id,
533               ard.source_table,
534               ard.source_type,
535               ard.code_combination_id,
536               ard.amount_dr,
537               ard.amount_cr,
538               ard.acctd_amount_dr,
539               ard.acctd_amount_cr,
540               ard.creation_date,
541               ard.created_by,
542               ard.last_updated_by,
543               ard.last_update_date,
544               ard.last_update_login,
545               ard.org_id,
546               ard.source_table_secondary,
547               ard.source_id_secondary,
548               ard.currency_code,
549               ard.currency_conversion_rate,
550               ard.currency_conversion_type,
551               ard.currency_conversion_date,
552               ard.taxable_entered_dr,
553               ard.taxable_entered_cr,
554               ard.taxable_accounted_dr,
555               ard.taxable_accounted_cr,
556               ard.tax_link_id,
557               ard.third_party_id,
558               ard.third_party_sub_id,
559               ard.reversed_source_id,
560               ard.tax_code_id,
561               ard.location_segment_id,
562               ard.source_type_secondary,
563               ard.tax_group_code_id,
564               --{BUG#2979254
565               ard.ref_customer_trx_line_id,
566               ard.ref_cust_trx_line_gl_dist_id,
567               ard.ref_line_id,
568               ard.from_amount_dr,
569               ard.from_amount_cr,
570               ard.from_acctd_amount_dr,
571               ard.from_acctd_amount_cr
572               --}
573        from   ar_mc_distributions_all ard
574        where  g_ae_sys_rec.sob_type = 'R'
575          and  ard.set_of_books_id = g_ae_sys_rec.set_of_books_id
576          and  ard.source_id = g_ae_doc_rec.source_id_old
577          and  ard.source_table = g_ae_doc_rec.source_table
578        order  by line_id ;
579 */
580 
581 l_ard_rec                ar_distributions%ROWTYPE;
582 
583 BEGIN
584 
585   arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Reverse_BR()+');
586   --{HYUDETUPT change the structure of this code so that it does not break everytime schema changes
587 
588   OPEN get_old_ard;
589   LOOP
590     FETCH get_old_ard INTO
591         l_ard_rec.line_id,
592         l_ard_rec.source_id,
593         l_ard_rec.source_table,
594         l_ard_rec.source_type,
595         l_ard_rec.code_combination_id,
596         l_ard_rec.amount_dr,
597         l_ard_rec.amount_cr,
598         l_ard_rec.acctd_amount_dr,
599         l_ard_rec.acctd_amount_cr,
600         l_ard_rec.creation_date,
601         l_ard_rec.created_by,
602         l_ard_rec.last_updated_by,
603         l_ard_rec.last_update_date,
604         l_ard_rec.last_update_login,
605         l_ard_rec.org_id,
606         l_ard_rec.source_table_secondary,
607         l_ard_rec.source_id_secondary,
608         l_ard_rec.currency_code,
609         l_ard_rec.currency_conversion_rate,
610         l_ard_rec.currency_conversion_type,
611         l_ard_rec.currency_conversion_date,
612         l_ard_rec.taxable_entered_dr,
613         l_ard_rec.taxable_entered_cr,
614         l_ard_rec.taxable_accounted_dr,
615         l_ard_rec.taxable_accounted_cr,
616         l_ard_rec.tax_link_id,
617         l_ard_rec.third_party_id,
618         l_ard_rec.third_party_sub_id,
619         l_ard_rec.reversed_source_id,
620         l_ard_rec.tax_code_id,
621         l_ard_rec.location_segment_id,
622         l_ard_rec.source_type_secondary,
623         l_ard_rec.tax_group_code_id,
624               --{BUG#2979254
625         l_ard_rec.ref_customer_trx_line_id,
626         l_ard_rec.ref_cust_trx_line_gl_dist_id,
627         l_ard_rec.ref_line_id,
628         l_ard_rec.from_amount_dr,
629         l_ard_rec.from_amount_cr,
630         l_ard_rec.from_acctd_amount_dr,
631         l_ard_rec.from_acctd_amount_cr;
632      EXIT WHEN get_old_ard%NOTFOUND;
633 
634      reverse_single_dist_line(l_ard_rec);
635 
636   END LOOP;
637   CLOSE get_old_ard;
638   --}
639   arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Reverse_BR()-');
640 
641 EXCEPTION
642   WHEN NO_DATA_FOUND THEN
643     arp_standard.debug('ARP_BILLS_RECEIVABLE_MAIN.Reverse_BR - NO_DATA_FOUND' );
644     RAISE;
645 
646   WHEN OTHERS THEN
647     arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Reverse_BR');
648     RAISE ;
649 
650 END Reverse_BR;
651 
652 /* =======================================================================
653  |
654  | PROCEDURE Get_Doc_Entitity_Data
655  |
656  | DESCRIPTION
657  |      This procedure gets the necessary transaction data for each entity
658  |      level.
659  |
660  | PARAMETERS
661  | 	p_level          Entitity Level
662  |      p_app_rec        Application Record
663  |      p_cust_inv_rec   Invoice document or On Account Credit Memo data
664  |      p_rule_rec       Rule record
665  |
666  | NOTES
667  * ======================================================================*/
668 PROCEDURE Get_Doc_Entitity_Data (
669                 p_level          IN VARCHAR2                            ,
670                 p_app_rec        OUT NOCOPY ar_receivable_applications%ROWTYPE ,
671                 p_cust_inv_rec   OUT NOCOPY ra_customer_trx%ROWTYPE            ,
672                 p_trh_rec        OUT NOCOPY ar_transaction_history%ROWTYPE     ,
673                 p_rule_rec       OUT NOCOPY ae_rule_rec_type) IS
674 
675 BEGIN
676 
677    IF PG_DEBUG in ('Y', 'C') THEN
678       arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Get_Doc_Entitity_Data()+');
679    END IF;
680 
681 /*----------------------------------------------------------------------------+
682  | Retrieve the data required for accounting for a bills receivable document  |
683  | Note : In this case we use the p_app_rec (receivable applications), this is|
684  |        because we want to simulate a Receipt application (payment) event   |
685  |        to move Deferred Tax using the tax accounting engine. This also     |
686  |        minimizes the changes to the tax accounting engine (ARALLOCB.pls).  |
687  +----------------------------------------------------------------------------*/
688 
689    -- MRC TRIGGER Replacement:  Modified to call functions for currency
690    -- sensitive data.
691 
692    IF p_level = C_TH then
693       select '',
694              pay.customer_trx_id,
695              pay.payment_schedule_id,
696              pay.amount_due_remaining,
697 --{BUG#4301323
698 --             DECODE(g_ae_sys_rec.sob_type, 'P',
699 --                    pay.acctd_amount_due_remaining,
700 --                    arp_mrc_acct_main.get_ps_entity_data(
701 --                      pay.payment_schedule_id,
702 --                      g_ae_sys_rec.set_of_books_id)) acctd_amount_due_remaining,
703              pay.acctd_amount_due_remaining      acctd_amount_due_remaining,
704 --}
705              pay.amount_line_items_remaining,
706              pay.tax_remaining,
707              pay.freight_remaining,
708              pay.receivables_charges_remaining,
709              'Y',
710              ctinv.invoice_currency_code,
711 --{BUG#4301323
712 --             DECODE(g_ae_sys_rec.sob_type, 'P',
713 --                    ctinv.exchange_rate,
714 --                    arp_mrc_acct_main.get_ctx_exg_rate(
715 --                        ctinv.customer_trx_id,
716 --                        g_ae_sys_rec.set_of_books_id)) exchange_rate,
717              ctinv.exchange_rate             exchange_rate,
718 --}
719 --{BUG#4301323
720 --             DECODE(g_ae_sys_rec.sob_type, 'P',
721 --                    ctinv.exchange_rate_type,
722 --                    arp_mrc_acct_main.get_ctx_exg_rate_type(
723 --                        ctinv.customer_trx_id,
724 --                        g_ae_sys_rec.set_of_books_id)) exchange_rate_type,
725              ctinv.exchange_rate_type      exchange_rate_type,
726 --}
727 --{BUG#4301323
728 --             DECODE(g_ae_sys_rec.sob_type, 'P',
729 --                    ctinv.exchange_date,
730 --                    arp_mrc_acct_main.get_ctx_exg_date(
731 --                        ctinv.customer_trx_id,
732 --                        g_ae_sys_rec.set_of_books_id)) exchange_date,
733             ctinv.exchange_date             exchange_date,
734 --}
735              ctinv.trx_date,
736              ctinv.bill_to_customer_id,
737              ctinv.bill_to_site_use_id,
738              ctinv.drawee_site_use_id,
739              th.customer_trx_id,
740              th.status,
741              th.event,
742              th.prv_trx_history_id
743       into   p_app_rec.receivable_application_id,
744              p_app_rec.applied_customer_trx_id,
745              p_app_rec.applied_payment_schedule_id,
746              p_app_rec.amount_applied,
747              p_app_rec.acctd_amount_applied_to,
748              p_app_rec.line_applied,
749              p_app_rec.tax_applied,
750              p_app_rec.freight_applied,
751              p_app_rec.receivables_charges_applied,
752              p_app_rec.confirmed_flag,
753              p_cust_inv_rec.invoice_currency_code,
754              p_cust_inv_rec.exchange_rate,
755              p_cust_inv_rec.exchange_rate_type,
756              p_cust_inv_rec.exchange_date,
757              p_cust_inv_rec.trx_date,
758              p_cust_inv_rec.bill_to_customer_id, --3rd party
759              p_cust_inv_rec.bill_to_site_use_id, --3rd party sub id
760              p_cust_inv_rec.drawee_site_use_id,
761              p_trh_rec.customer_trx_id,
762              p_trh_rec.status,
763              p_trh_rec.event,
764              p_trh_rec.prv_trx_history_id
765       from ar_transaction_history     th,
766            ra_customer_trx            ctinv,
767            ar_payment_schedules       pay
768       where th.transaction_history_id = g_ae_doc_rec.source_id
769       and   th.customer_trx_id = ctinv.customer_trx_id
770       and   ctinv.customer_trx_id = pay.customer_trx_id;
771 
772 /*-----------------------------------------------------------------------------+
773  | Initialize the rules buffer for discounts (Bills have no discounts) however |
774  | since the tax accounting engine is to be called we simulate a application   |
775  | hence in this case we initialize our rule buffer.                           |
776  +----------------------------------------------------------------------------*/
777     IF g_event in ('MATURITY_DATE','UNPAID') THEN
778          select 'NO_SOURCE', --gl account source
779                 'NO_SOURCE', -- tax code source
780                 '',          -- tax recoverable flag
781                 '',          -- discount ccid
782                 '',          --asset tax code
783                 '',          --liability tax code
784                 ''         ,
785                 ''         ,
786                 'NO_SOURCE',
787                 'NO_SOURCE',
788                 '',
789                 '',
790                 '',
791                 '',
792                 ''                                         ,
793                 ''
794          into   p_rule_rec.gl_account_source1, --Initialize Earned discounts
795                 p_rule_rec.tax_code_source1,
796                 p_rule_rec.tax_recoverable_flag1,
797                 p_rule_rec.code_combination_id1,
798                 p_rule_rec.asset_tax_code1,
799                 p_rule_rec.liability_tax_code1,
800                 p_rule_rec.act_tax_non_rec_ccid1,
801                 p_rule_rec.act_vat_tax_id1,
802                 p_rule_rec.gl_account_source2, --Initialize Unearned discounts
803                 p_rule_rec.tax_code_source2,
804                 p_rule_rec.tax_recoverable_flag2,
805                 p_rule_rec.code_combination_id2,
806                 p_rule_rec.asset_tax_code2,
807                 p_rule_rec.liability_tax_code2,
808                 p_rule_rec.act_tax_non_rec_ccid2,
809                 p_rule_rec.act_vat_tax_id2
810          from   dual;
811 
812       END IF; --end if Maturity date event
813 
814    END IF; --end if p_level = C_TH
815 
816    initialize_global_variables(p_trh_rec);
817 
818    IF PG_DEBUG in ('Y', 'C') THEN
819       arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Get_Doc_Entitity_Data()-');
820    END IF;
821 
822 EXCEPTION
823   WHEN NO_DATA_FOUND THEN
824      IF PG_DEBUG in ('Y', 'C') THEN
825         arp_standard.debug('ARP_BILLS_RECEIVABLE_MAIN.Get_Doc_Entitity_Data - NO_DATA_FOUND' );
826      END IF;
827      RAISE;
828 
829   WHEN OTHERS THEN
830      IF PG_DEBUG in ('Y', 'C') THEN
831         arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Get_Doc_Entitity_Data');
832      END IF;
833      RAISE ;
834 
835 END Get_Doc_Entitity_Data;
836 
837 /* =======================================================================
838  | PROCEDURE Create_Ae_Lines_Common
839  |
840  | DESCRIPTION
841  |      This procedure creates the AE lines at each entity level. Used
842  |      for creating lines as part of each accounting event associated
843  |      with the entity Transaction history.
844  |
845  |      Functions:
846  |      	- Create AE lines.
847  |      	- Get additional data to determine the type of AE lines
848  |
849  | PARAMETERS
850  |      p_level       Entity level from which the procedure was called
851  * ======================================================================*/
852 PROCEDURE Create_Ae_Lines_Common (
853 		p_level 	IN VARCHAR2 ) IS
854 
855   l_app_rec		ar_receivable_applications%ROWTYPE ;
856   l_cust_inv_rec        ra_customer_trx%ROWTYPE            ;
857   l_trh_rec             ar_transaction_history%ROWTYPE     ; -- jrautiai
858   l_rule_rec            ae_rule_rec_type                   ;
859 
860 BEGIN
861 
862    IF PG_DEBUG in ('Y', 'C') THEN
863       arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Create_Ae_Lines_Common()+');
864    END IF;
865 
866    /*-------------------------------------------------------------+
867     | Get Document Entitity specific data                         |
868     +-------------------------------------------------------------*/
869 
870      Get_Doc_Entitity_Data(p_level          => p_level        ,
871                            p_app_rec        => l_app_rec      ,
872                            p_cust_inv_rec   => l_cust_inv_rec ,
873                            p_trh_rec        => l_trh_rec      , -- jrautiai
874                            p_rule_rec       => l_rule_rec      );
875 
876   /*------------------------------------------------------+
877    | Create AE Lines for Transaction History              |
878    +------------------------------------------------------*/
879    IF (p_level = C_TH) THEN	-- Entity level Transaction History
880 
881    /*------------------------------------------------------+
882     | Create AE Lines for Transaction History Accounting   |
883     +------------------------------------------------------*/
884      Create_Ae_Lines_BR(p_app_rec        => l_app_rec        ,
885                         p_cust_inv_rec   => l_cust_inv_rec   ,
886                         p_trh_rec        => l_trh_rec        , -- jrautiai
887                         p_rule_rec       => l_rule_rec        );
888 
889   END IF;
890 
891   IF PG_DEBUG in ('Y', 'C') THEN
892      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Create_Ae_Lines_Common()-');
893   END IF;
894 
895 EXCEPTION
896   WHEN OTHERS THEN
897      IF PG_DEBUG in ('Y', 'C') THEN
898         arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Create_Ae_Lines_Common');
899      END IF;
900      RAISE;
901 
902 END Create_Ae_Lines_Common;
903 
904 /* =======================================================================
905  | PROCEDURE Create_Ae_Lines_BR
906  |
907  | DESCRIPTION
908  |      This procedure creates the AE lines for accounting events associated
909  |      with the transaction history record
910  |
911  |      Functions:
912  |              - Create AE lines for Transaction history.
913  |              - Determines Amounts Dr/Cr.
914  |
915  | PARAMETERS
916  |      p_app_rec       Receivables Application Record(Transaction data)
917  |      p_cust_inv_rec  Invoice header Record
918  |      p_rule_rec      Rule Record
919  * ======================================================================*/
920 PROCEDURE Create_Ae_Lines_BR(
921 		p_app_rec 	 IN ar_receivable_applications%ROWTYPE                 ,
922                 p_cust_inv_rec   IN ra_customer_trx%ROWTYPE                            ,
923                 p_trh_rec        IN ar_transaction_history%ROWTYPE                     , -- jrautiai
924                 p_rule_rec       IN ae_rule_rec_type                                     ) IS
925 
926   l_app_id                ar_receivable_applications.receivable_application_id%TYPE;
927   l_ael_line_rec	      ae_line_rec_type;
928   l_empty_ael_line_rec	  ae_line_rec_type;
929   l_adj_rec               ar_adjustments%ROWTYPE;
930   l_ae_line_tbl           ae_line_tbl_type;
931   l_ae_ctr                BINARY_INTEGER := 0;
932   l_ctr                   BINARY_INTEGER;
933   l_account_class         VARCHAR(30) := NULL;
934   l_ccid                  number;
935   l_concat_segments       varchar2(2000);
936   l_num_failed_dist_rows  number;
937   l_prev_posted_trh_rec   ar_transaction_history%ROWTYPE;
938   l_ae_deleted            BOOLEAN := FALSE;
939 
940 BEGIN
941   IF PG_DEBUG in ('Y', 'C') THEN
942      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Create_Ae_Lines_BR()+');
943   END IF;
944 
945  /*-------------------------------------------------------------------------+
946   | Create accounting based on the record status and event that took place  |
947   +-------------------------------------------------------------------------*/
948   IF g_event = C_MATURITY_DATE THEN
949 
950    /*------------------------------------------------------------+
951     | Maturity date events only tax is moved, that is done later |
952     +------------------------------------------------------------*/
953     NULL;
954 
955   ELSIF g_event = C_RECALLED THEN --recall
956 
957    /*------------------------------------------------------------+
958     | Recall event cancels all accounting on the previous record |
959     | Deferred tax is not moved and autoaccounting is not called |
960     +------------------------------------------------------------*/
961 
962    /*-----------------------------------------------------+
963     | Find the previous posted transaction history record |
964     +-----------------------------------------------------*/
965     find_prev_posted_hist_record(g_ae_doc_rec.source_id,l_prev_posted_trh_rec);
966     g_ae_doc_rec.source_id_old := l_prev_posted_trh_rec.transaction_history_id;
967 
968    /*-------------------------------------------------+
969     | Reverse accounting by the reverse functionality |
970     +-------------------------------------------------*/
971     g_ae_doc_rec.other_flag := 'REVERSE';
972     Reverse_BR;
973 
974   ELSIF p_trh_rec.status = C_PENDING_REMITTANCE THEN
975 
976    /*---------------------------------------------------------------+
977     | When status becomes PENDING_REMITTANCE the accounting depends |
978     | on the previous status. Autoaccounting is called called with  |
979     | class REC for both cases.                                     |
980     +---------------------------------------------------------------*/
981     l_account_class := C_REC;
982 
983     IF g_previous_status in (C_INCOMPLETE,C_PENDING_ACCEPTANCE) THEN
984 
985      /*---------------------------------------------------------------+
986       | When previous status is INCOMPLETE or PENDING_ACCEPTANCE      |
987       | accounting is created for all exchanged transactions.         |
988       +---------------------------------------------------------------*/
989       create_exchanged_trx_acct(p_trh_rec.customer_trx_id,
990                                 p_rule_rec);
991 
992     ELSE -- C_UNPAID -- restate
993 
994      /*---------------------------------------------------------------+
995       | Otherwise the previous status is UNPAID so the BR was restated|
996       | The old accounting is reversed.                               |
997       +---------------------------------------------------------------*/
998       reverse_old_acct(g_ae_doc_rec.source_id,
999                        p_app_rec.applied_payment_schedule_id);
1000 
1001     END IF;
1002 
1003   ELSIF p_trh_rec.status = C_CANCELLED THEN
1004    /*---------------------------------------------------------------+
1005     | When status becomes CANCELLED the previous accounting         |
1006     | is and all the accounting for the exchanged transactions is   |
1007     | reversed. Autoaccounting is not called.                       |
1008     +---------------------------------------------------------------*/
1009 
1010    /*---------------------------------------+
1011     | The old accounting is reversed.       |
1012     +---------------------------------------*/
1013     reverse_old_acct(g_ae_doc_rec.source_id,
1014                      p_app_rec.applied_payment_schedule_id);
1015 
1016    /*----------------------------------------------------------------------+
1017     | Reverse accounting for exchanged transactions created at completion. |
1018     +----------------------------------------------------------------------*/
1019     reverse_exchanged_trx_acct(g_ae_doc_rec.source_id);
1020 
1021   ELSIF p_trh_rec.status = C_UNPAID THEN
1022    /*-----------------------------------------------------------------+
1023     | When status becomes UNPAID the old accounting is                |
1024     | reversed. Autoaccounting is called called with class UNPAIDREC. |
1025     +-----------------------------------------------------------------*/
1026 
1027     l_account_class := C_UNPAIDREC;
1028 
1029    /*---------------------------------------+
1030     | The old accounting is reversed.       |
1031     +---------------------------------------*/
1032     reverse_old_acct(g_ae_doc_rec.source_id,
1033                      p_app_rec.applied_payment_schedule_id);
1034 
1035   ELSIF p_trh_rec.status = C_REMITTED THEN
1036    /*-------------------------------------------------------------------+
1037     | When status becomes STANDARD_REMITTED the old accounting is       |
1038     | reversed. Autoaccounting is called called with class REMITTANCE.  |
1039     +-------------------------------------------------------------------*/
1040 
1041     l_account_class := C_REMITTANCE;
1042 
1043    /*---------------------------------------+
1044     | The old accounting is reversed.       |
1045     +---------------------------------------*/
1046     reverse_old_acct(g_ae_doc_rec.source_id,
1047                      p_app_rec.applied_payment_schedule_id);
1048 
1049   ELSIF p_trh_rec.status = C_FACTORED THEN
1050 
1051    /*-------------------------------------------------------------------+
1052     | When status becomes FACTORED the old accounting is                |
1053     | reversed. Autoaccounting is called called with class FACTOR.      |
1054     +-------------------------------------------------------------------*/
1055 
1056     l_account_class := C_FACTOR;
1057 
1058    /*---------------------------------------+
1059     | The old accounting is reversed.       |
1060     +---------------------------------------*/
1061     reverse_old_acct(g_ae_doc_rec.source_id,
1062                      p_app_rec.applied_payment_schedule_id);
1063 
1064   ELSE -- all other cases reverse the previous accounting
1065    /*--------------------------------------------------------------------------+
1066     | Not supported, raise an error to notify that this has to be implemented. |
1067     +--------------------------------------------------------------------------*/
1068     IF PG_DEBUG in ('Y', 'C') THEN
1069        arp_standard.debug('ARP_BILLS_RECEIVABLE_MAIN.Create_Ae_Lines_BR - status '||p_trh_rec.status||' not supported');
1070     END IF;
1071     APP_EXCEPTION.raise_exception;
1072 
1073   END IF;
1074 
1075   /*----------------------------------------------------------+
1076    | If l_account_class is populated we call auto accounting. |
1077    +----------------------------------------------------------*/
1078 
1079    IF l_account_class is not null THEN
1080 
1081      BEGIN
1082 
1083       /*-----------------------+
1084        | Call auto accounting. |
1085        +-----------------------*/
1086        arp_auto_accounting_br.do_autoaccounting(
1087                                      'I',
1088                                      l_account_class,
1089                                      p_trh_rec.customer_trx_id,
1090                                      null, -- receivable_application_id
1091                                      null, -- br_unpaid_ccid
1092                                      null, -- cust_trx_type_id
1093                                      null, -- site_use_id
1094                                      null, -- receipt_method_id
1095                                      null, -- bank_account_id
1096                                      l_ccid, -- (out)
1097                                      l_concat_segments, -- (out)
1098                                      l_num_failed_dist_rows); -- (out)
1099 
1100      EXCEPTION
1101        WHEN arp_auto_accounting.no_ccid THEN
1102          IF PG_DEBUG in ('Y', 'C') THEN
1103             arp_standard.debug('ARP_BILLS_RECEIVABLE_MAIN.Create_Ae_Lines_BR - AutoAccounting Failed' );
1104          END IF;
1105          RAISE;
1106        WHEN OTHERS THEN
1107          RAISE;
1108      END;
1109    END IF;
1110 
1111   /*--------------------------------------------------------------------------+
1112    | Call the tax accounting engine for the deferred tax Maturity date event  |
1113    | for Bills Receivable                                                     |
1114    +--------------------------------------------------------------------------*/
1115    IF (g_event = C_MATURITY_DATE) AND (nvl(g_ae_doc_rec.deferred_tax,'Y') = 'Y')
1116       AND ((p_app_rec.amount_applied <> 0) or (p_app_rec.acctd_amount_applied_to <> 0)) THEN
1117 
1118       l_ae_line_tbl := g_empty_ae_line_tbl;
1119       l_ae_ctr      := 0;
1120 
1121     /*---------------------------------------------------------------------------+
1122      | The deferred tax flag is set by the Bills Receivable Houskeeper. Call the |
1123      | Bills Receivable wrapper routine to move deferred tax for the remaining   |
1124      | amount and accounted amount on the tax buckets, simulating a receipt      |
1125      | application. Hence it is noticible that the p_app_rec record is used for  |
1126      | processing associated with the maturity date event.                       |
1127      +---------------------------------------------------------------------------*/
1128        ARP_BR_ALLOC_WRAPPER_PKG.Allocate_Tax_BR_Main(
1129                  p_mode                 => g_mode         ,     --Document mode
1130                  p_ae_doc_rec           => g_ae_doc_rec   ,     --Document detail
1131                  p_ae_event_rec         => g_ae_event_rec ,     --Event record
1132                  p_ae_rule_rec          => p_rule_rec     ,     --Rule info for payment method
1133                  p_app_rec              => p_app_rec      ,     --Application details
1134                  p_cust_inv_rec         => p_cust_inv_rec ,     --Invoice details
1135                  p_adj_rec              => l_adj_rec      ,     --dummy adjustment record
1136                  p_ae_sys_rec           => g_ae_sys_rec   ,     --system parameters
1137                  p_ae_ctr               => l_ae_ctr       ,     --counter
1138                  p_ae_line_tbl          => l_ae_line_tbl); --final tax accounting table
1139 
1140        IF l_ae_line_tbl.EXISTS(l_ae_ctr) THEN --Atleast one Tax line exists
1141 
1142           FOR l_ctr IN l_ae_line_tbl.FIRST .. l_ae_line_tbl.LAST LOOP
1143 
1144             /*-----------------------------------------------------------------------+
1145              | It is necessary to populate the record and then call assign elements  |
1146              | because of standards and that the User Hook could override accounting |
1147              | so need to populate this record (rather than direct table assignments)|
1148              +-----------------------------------------------------------------------*/
1149               l_ael_line_rec := l_empty_ael_line_rec;
1150               l_ael_line_rec := l_ae_line_tbl(l_ctr);
1151 
1152             /*----------------------------------+
1153              | Assign AEL for REC for document  |
1154              +----------------------------------*/
1155               Assign_Ael_Elements( p_ae_line_rec         => l_ael_line_rec );
1156 
1157           END LOOP; --end loop tax accounting table
1158 
1159        END IF; --end if atleast one tax line exists
1160 
1161    ELSIF (g_event = C_UNPAID AND g_previous_status in (C_ENDORSED,C_FACTORED)) THEN
1162 
1163      /*-------------------------------------------------------------------+
1164       | Need to back out NOCOPY all deferred tax related to MATURITY_DATE row    |
1165       +-------------------------------------------------------------------*/
1166       reverse_deferred_tax(g_ae_doc_rec.source_id);
1167 
1168    END IF; --end if Maturity date event for a Bill
1169 
1170 
1171   IF PG_DEBUG in ('Y', 'C') THEN
1172      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Create_Ae_Lines_BR()-');
1173   END IF;
1174 
1175 EXCEPTION
1176   WHEN NO_DATA_FOUND THEN
1177      IF PG_DEBUG in ('Y', 'C') THEN
1178         arp_standard.debug('EXCEPTION: NO_DATA_FOUND' );
1179      END IF;
1180      RAISE;
1181 
1182   WHEN OTHERS THEN
1183      IF PG_DEBUG in ('Y', 'C') THEN
1184         arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Create_Ae_Lines_BR');
1185      END IF;
1186      RAISE;
1187 
1188 END Create_Ae_Lines_BR;
1189 
1190 /* =======================================================================
1191  | PROCEDURE Assign_Ael_Elements
1192  |
1193  | DESCRIPTION
1194  |      This procedure stores the AE Line record into AE Lines PLSQL table.
1195  |      Functions:
1196  |              - Determine regular or negative Dr/Cr.
1197  |              - Store AE Line Record in AE Lines PLSQL Table.
1198  |              - In a fully implemented SLA model, Will determine the
1199  |                account to use based on AE Line type and other parameters.
1200  |              - In a fully implemented SLA model, Will determine the
1201  |                account descriptions.
1202  |
1203  | GUIDELINE
1204  |      - This procedure can be shared across document types
1205  |      - Recommendation is to have one per document type(AE Derivation)
1206  |
1207  | PARAMETERS
1208  |      p_ae_line_rec     AE Line Record
1209  * ======================================================================*/
1210 PROCEDURE Assign_Ael_Elements(
1211                	p_ae_line_rec 		IN ae_line_rec_type ) IS
1212 
1213   l_account			NUMBER;
1214   l_account_valid		BOOLEAN;
1215   l_replace_default_account	BOOLEAN;
1216 
1217 BEGIN
1218 
1219     IF PG_DEBUG in ('Y', 'C') THEN
1220        arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Assign_Ael_Elements()+');
1221     END IF;
1222 
1223    /*------------------------------------------------------+
1224     | Call Hook to Override Account                        |
1225     +------------------------------------------------------*/
1226     ARP_ACCT_HOOK.Override_Account(
1227         	p_mode                  => g_mode,
1228         	p_ae_doc_rec            => g_ae_doc_rec,
1229         	p_ae_event_rec          => g_ae_event_rec,
1230 		p_ae_line_rec		=> p_ae_line_rec,
1231 		p_account		=> l_account,
1232 		p_account_valid		=> l_account_valid,
1233 		p_replace_default_account => l_replace_default_account
1234 		);
1235 
1236     IF ( NOT l_replace_default_account ) THEN
1237 
1238       /*------------------------------------------------------+
1239        | SLA : Build Account for AE Line Type                 |
1240        |       When SLA is fully implemented Account Builder  |
1241        |       will be called from here.                      |
1242        +------------------------------------------------------*/
1243        l_account := p_ae_line_rec.account;
1244 
1245     END IF;		-- Replace default account?
1246 
1247    /*------------------------------------------------------+
1248     | SLA : Build Account description for AE Line Type     |
1249     |       When SLA is fully implemented Description 	   |
1250     |       builder will be called from here.              |
1251     +------------------------------------------------------*/
1252 
1253    /*------------------------------------------------------+
1254     | SLA : Check Negative Dr/Cr for AE Line 		   |
1255     |       When SLA is fully implemented.            	   |
1256     +------------------------------------------------------*/
1257 
1258    /*------------------------------------------------------+
1259     | Store AE Line elements in AE Lines temp table        |
1260     +------------------------------------------------------*/
1261     g_ae_line_ctr := g_ae_line_ctr +1;
1262 
1263     g_ae_line_tbl(g_ae_line_ctr).ae_line_type             :=  p_ae_line_rec.ae_line_type;
1264     g_ae_line_tbl(g_ae_line_ctr).ae_line_type_secondary   :=  p_ae_line_rec.ae_line_type_secondary;
1265     g_ae_line_tbl(g_ae_line_ctr).source_id                :=  p_ae_line_rec.source_id;
1266     g_ae_line_tbl(g_ae_line_ctr).source_table             :=  p_ae_line_rec.source_table;
1267     g_ae_line_tbl(g_ae_line_ctr).account                  :=  p_ae_line_rec.account;
1268     g_ae_line_tbl(g_ae_line_ctr).entered_dr               :=  p_ae_line_rec.entered_dr;
1269     g_ae_line_tbl(g_ae_line_ctr).entered_cr               :=  p_ae_line_rec.entered_cr;
1270     g_ae_line_tbl(g_ae_line_ctr).accounted_dr             :=  p_ae_line_rec.accounted_dr;
1271     g_ae_line_tbl(g_ae_line_ctr).accounted_cr             :=  p_ae_line_rec.accounted_cr;
1272     g_ae_line_tbl(g_ae_line_ctr).source_id_secondary      :=  p_ae_line_rec.source_id_secondary;
1273     g_ae_line_tbl(g_ae_line_ctr).source_table_secondary   :=  p_ae_line_rec.source_table_secondary;
1274     g_ae_line_tbl(g_ae_line_ctr).currency_code            :=  p_ae_line_rec.currency_code;
1275     g_ae_line_tbl(g_ae_line_ctr).currency_conversion_rate :=  p_ae_line_rec.currency_conversion_rate;
1276     g_ae_line_tbl(g_ae_line_ctr).currency_conversion_type :=  p_ae_line_rec.currency_conversion_type;
1277     g_ae_line_tbl(g_ae_line_ctr).currency_conversion_date :=  p_ae_line_rec.currency_conversion_date;
1278     g_ae_line_tbl(g_ae_line_ctr).third_party_id           :=  p_ae_line_rec.third_party_id;
1279     g_ae_line_tbl(g_ae_line_ctr).third_party_sub_id       :=  p_ae_line_rec.third_party_sub_id;
1280     g_ae_line_tbl(g_ae_line_ctr).tax_code_id              :=  p_ae_line_rec.tax_code_id;
1281     g_ae_line_tbl(g_ae_line_ctr).tax_group_code_id        :=  p_ae_line_rec.tax_group_code_id;
1282     g_ae_line_tbl(g_ae_line_ctr).location_segment_id      :=  p_ae_line_rec.location_segment_id;
1283     g_ae_line_tbl(g_ae_line_ctr).taxable_entered_dr       :=  p_ae_line_rec.taxable_entered_dr;
1284     g_ae_line_tbl(g_ae_line_ctr).taxable_entered_cr       :=  p_ae_line_rec.taxable_entered_cr;
1285     g_ae_line_tbl(g_ae_line_ctr).taxable_accounted_dr     :=  p_ae_line_rec.taxable_accounted_dr;
1286     g_ae_line_tbl(g_ae_line_ctr).taxable_accounted_cr     :=  p_ae_line_rec.taxable_accounted_cr;
1287     g_ae_line_tbl(g_ae_line_ctr).applied_from_doc_table   :=  p_ae_line_rec.applied_from_doc_table;
1288     g_ae_line_tbl(g_ae_line_ctr).applied_from_doc_id      :=  p_ae_line_rec.applied_from_doc_id;
1289     g_ae_line_tbl(g_ae_line_ctr).applied_to_doc_table     :=  p_ae_line_rec.applied_to_doc_table;
1290     g_ae_line_tbl(g_ae_line_ctr).applied_to_doc_id        :=  p_ae_line_rec.applied_to_doc_id;
1291     g_ae_line_tbl(g_ae_line_ctr).tax_link_id              :=  p_ae_line_rec.tax_link_id;
1292     g_ae_line_tbl(g_ae_line_ctr).reversed_source_id       :=  p_ae_line_rec.reversed_source_id;
1293     --{3377004
1294     g_ae_line_tbl(g_ae_line_ctr).ref_line_id              :=  p_ae_line_rec.ref_line_id;
1295     g_ae_line_tbl(g_ae_line_ctr).ref_customer_trx_line_id :=  p_ae_line_rec.ref_customer_trx_line_id;
1296     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;
1297     g_ae_line_tbl(g_ae_line_ctr).from_amount_dr           :=  p_ae_line_rec.from_amount_dr;
1298     g_ae_line_tbl(g_ae_line_ctr).from_amount_cr           :=  p_ae_line_rec.from_amount_cr;
1299     g_ae_line_tbl(g_ae_line_ctr).from_acctd_amount_dr     :=  p_ae_line_rec.from_acctd_amount_dr;
1300     g_ae_line_tbl(g_ae_line_ctr).from_acctd_amount_cr     :=  p_ae_line_rec.from_acctd_amount_cr;
1301     g_ae_line_tbl(g_ae_line_ctr).activity_bucket                   :=  p_ae_line_rec.activity_bucket;
1302     g_ae_line_tbl(g_ae_line_ctr).ref_account_class                :=  p_ae_line_rec.ref_account_class;
1303     --}
1304 
1305     IF PG_DEBUG in ('Y', 'C') THEN
1306        arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.Assign_Ael_Elements()-');
1307     END IF;
1308 
1309 EXCEPTION
1310   WHEN OTHERS THEN
1311      IF PG_DEBUG in ('Y', 'C') THEN
1312         arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.Assign_Ael_Elements');
1313      END IF;
1314      RAISE;
1315 
1316 END Assign_Ael_Elements;
1317 
1318 /* =======================================================================
1319  | PROCEDURE initialize_global_variables
1320  |
1321  | DESCRIPTION
1322  |      This procedure detects the event that took place on the BR
1323  |      when accounting engine was called.
1324  |
1325  | PARAMETERS
1326  |      p_trh_rec     Transaction history record
1327  |
1328  | 02/11/05     VCRISOST        Bug 4178326 : if within the same window
1329  |                              session user Recalls, then Unpays a BR
1330  |                              accounting entries are incorrect for
1331  |                              UNPAID event
1332  |
1333  * ======================================================================*/
1334 PROCEDURE initialize_global_variables(p_trh_rec IN ar_transaction_history%ROWTYPE) IS
1335 
1336 BEGIN
1337   IF PG_DEBUG in ('Y', 'C') THEN
1338      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.initialize_global_variables()+');
1339   END IF;
1340 
1341  /*-----------------------------+
1342   | Fetch the previous status   |
1343   +-----------------------------*/
1344   g_previous_status := trx_history_status(p_trh_rec.prv_trx_history_id);
1345 
1346   IF p_trh_rec.event = C_MATURITY_DATE THEN
1347 
1348    /*-------------------------------------------------------------+
1349     | Bills Receivable transaction maturity date event occurred   |
1350     +-------------------------------------------------------------*/
1351     g_event := C_MATURITY_DATE;
1352 
1353   ELSIF p_trh_rec.event = C_RECALLED THEN
1354 
1355    /*-----------------+
1356     | BR was recalled |
1357     +-----------------*/
1358     g_event := C_RECALLED;
1359 
1360   ELSIF p_trh_rec.status = C_UNPAID THEN
1361 
1362    /*----------------------------------------------------------+
1363     | BR UNPAID, if the previous status was PENDING_REMITTANCE |
1364     | then no deferred tax has been moved, so it is not backed |
1365     | out. This information is used in the main routine to     |
1366     | decide whether to move the deferred tax or not           |
1367     +----------------------------------------------------------*/
1368     IF g_previous_status <> C_PENDING_REMITTANCE THEN
1369 
1370       g_event := C_UNPAID;
1371     ELSE
1372 
1373       -- Bug 4178326 : need to initialize this back to NULL, otherwise
1374       -- code uses the g_event from previous action and accounting is
1375       -- created incorrectly
1376       g_event := NULL;
1377 
1378     END IF;
1379 
1380   ELSE
1381     g_event := NULL;
1382   END IF;
1383 
1384   IF PG_DEBUG in ('Y', 'C') THEN
1385      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.initialize_global_variables()-');
1386   END IF;
1387 
1388   EXCEPTION
1389     WHEN OTHERS THEN
1390        IF PG_DEBUG in ('Y', 'C') THEN
1391           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.initialize_global_variables');
1392        END IF;
1393        RAISE;
1394 
1395 END initialize_global_variables;
1396 
1397 /* =======================================================================
1398  | PROCEDURE trx_history_status
1399  |
1400  | DESCRIPTION
1401  |      This function returns the status of the BR
1402  |
1403  | PARAMETERS
1404  |      p_transaction_history_id     Transaction history ID
1405  * ======================================================================*/
1406 FUNCTION trx_history_status(p_transaction_history_id ar_transaction_history.transaction_history_id%TYPE) RETURN VARCHAR2 IS
1407 
1408  /*-----------------------------+
1409   | Cursor to return the status |
1410   +-----------------------------*/
1411   CURSOR history_status_cur IS
1412     SELECT status
1413     FROM ar_transaction_history
1414     WHERE transaction_history_id = p_transaction_history_id;
1415 
1416  history_status_rec history_status_cur%ROWTYPE;
1417 
1418 BEGIN
1419   IF PG_DEBUG in ('Y', 'C') THEN
1420      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.trx_history_status()');
1421   END IF;
1422 
1423   /*------------------+
1424    | fetch the status |
1425    +------------------*/
1426    OPEN history_status_cur;
1427    FETCH history_status_cur INTO history_status_rec;
1428 
1429   /*---------------------------------+
1430    | If status not found return NULL |
1431    +---------------------------------*/
1432    IF history_status_cur%NOTFOUND THEN
1433      CLOSE history_status_cur;
1434      RETURN NULL;
1435    END IF;
1436 
1437    CLOSE history_status_cur;
1438 
1439    RETURN history_status_rec.status;
1440 
1441   EXCEPTION
1442     WHEN OTHERS THEN
1443        IF PG_DEBUG in ('Y', 'C') THEN
1444           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.trx_history_status');
1445        END IF;
1446        RAISE;
1447 
1448 END trx_history_status;
1449 
1450 /* =======================================================================
1451  | PROCEDURE reverse_single_dist_line
1452  |
1453  | DESCRIPTION
1454  |      This procedure reverses single distribution record given as parameter.
1455  |
1456  | PARAMETERS
1457  |      p_dist_rec     Distribution record
1458  * ======================================================================*/
1459 PROCEDURE reverse_single_dist_line(p_dist_rec ar_distributions%ROWTYPE) IS
1460   l_ael_line_rec         ae_line_rec_type;
1461   l_ael_empty_line_rec   ae_line_rec_type;
1462 BEGIN
1463   IF PG_DEBUG in ('Y', 'C') THEN
1464      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.reverse_single_dist_line()+');
1465   END IF;
1466 
1467  /*---------------------------+
1468   | Initialize build record   |
1469   +---------------------------*/
1470   l_ael_line_rec := l_ael_empty_line_rec ;
1471 
1472  /*----------------------+
1473   | Fill in the record   |
1474   +----------------------*/
1475   l_ael_line_rec.ae_line_type               := p_dist_rec.source_type;
1476   l_ael_line_rec.ae_line_type_secondary     := p_dist_rec.source_type_secondary;
1477   l_ael_line_rec.source_id                  := g_ae_doc_rec.source_id;
1478   l_ael_line_rec.source_table               := g_ae_doc_rec.source_table;
1479   l_ael_line_rec.source_id_secondary        := p_dist_rec.source_id_secondary;
1480   l_ael_line_rec.source_table_secondary     := p_dist_rec.source_table_secondary;
1481   l_ael_line_rec.account                    := p_dist_rec.code_combination_id;
1482   l_ael_line_rec.reversed_source_id         := NULL; /* used only for Receipt App by MRC */
1483 
1484  /*-----------------------------------------+
1485   | For reversals swap debits and credits   |
1486   +-----------------------------------------*/
1487   l_ael_line_rec.entered_cr   := p_dist_rec.amount_dr;
1488   l_ael_line_rec.accounted_cr := p_dist_rec.acctd_amount_dr;
1489 
1490   l_ael_line_rec.entered_dr   := p_dist_rec.amount_cr;
1491   l_ael_line_rec.accounted_dr := p_dist_rec.acctd_amount_cr;
1492 
1493   l_ael_line_rec.taxable_entered_cr    := p_dist_rec.taxable_entered_dr;
1494   l_ael_line_rec.taxable_accounted_cr  := p_dist_rec.taxable_accounted_dr;
1495 
1496   l_ael_line_rec.taxable_entered_dr    := p_dist_rec.taxable_entered_cr;
1497   l_ael_line_rec.taxable_accounted_dr  := p_dist_rec.taxable_accounted_cr;
1498 
1499   l_ael_line_rec.currency_code               := p_dist_rec.currency_code;
1500   l_ael_line_rec.currency_conversion_rate    := p_dist_rec.currency_conversion_rate;
1501   l_ael_line_rec.currency_conversion_type    := p_dist_rec.currency_conversion_type;
1502   l_ael_line_rec.currency_conversion_date    := p_dist_rec.currency_conversion_date;
1503   l_ael_line_rec.third_party_id              := p_dist_rec.third_party_id;
1504   l_ael_line_rec.third_party_sub_id          := p_dist_rec.third_party_sub_id;
1505   l_ael_line_rec.tax_code_id                 := p_dist_rec.tax_code_id;
1506   l_ael_line_rec.tax_group_code_id           := p_dist_rec.tax_group_code_id;
1507   l_ael_line_rec.location_segment_id         := p_dist_rec.location_segment_id;
1508   l_ael_line_rec.tax_link_id                 := p_dist_rec.tax_link_id;
1509   --{BUG#2979254
1510   l_ael_line_rec.ref_customer_trx_line_id    := p_dist_rec.ref_customer_trx_line_id;
1511   l_ael_line_rec.ref_cust_trx_line_gl_dist_id := p_dist_rec.ref_cust_trx_line_gl_dist_id;
1512   l_ael_line_rec.ref_line_id                 := p_dist_rec.ref_line_id;
1513   --}
1514   --{BUG#3377004
1515   l_ael_line_rec.from_amount_dr              := p_dist_rec.from_amount_cr;
1516   l_ael_line_rec.from_amount_cr              := p_dist_rec.from_amount_dr;
1517   l_ael_line_rec.from_acctd_amount_dr        := p_dist_rec.from_acctd_amount_cr;
1518   l_ael_line_rec.from_acctd_amount_cr        := p_dist_rec.from_acctd_amount_dr;
1519   --}
1520 
1521  /*---------------------------+
1522   | Assign AEL for Reversal   |
1523   +---------------------------*/
1524   Assign_Ael_Elements( p_ae_line_rec        => l_ael_line_rec );
1525 
1526   IF PG_DEBUG in ('Y', 'C') THEN
1527      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.reverse_single_dist_line()-');
1528   END IF;
1529 
1530   EXCEPTION
1531     WHEN OTHERS THEN
1532        IF PG_DEBUG in ('Y', 'C') THEN
1533           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.reverse_single_dist_line');
1534        END IF;
1535        RAISE;
1536 
1537 END reverse_single_dist_line;
1538 
1539 /* =======================================================================
1540  | PROCEDURE reverse_old_acct
1541  |
1542  | DESCRIPTION
1543  |      This procedure reverses previous accounting. This is used
1544  |      when reclassification reverses the old accounting.
1545  |
1546  | PARAMETERS
1547  |      p_transaction_history_id     Transaction history ID
1548  |      p_ps_id                      Payment Schedule ID
1549  * ======================================================================*/
1550 PROCEDURE reverse_old_acct(p_transaction_history_id IN ar_transaction_history.transaction_history_id%TYPE,
1551                            p_ps_id                  IN ar_payment_schedules.payment_schedule_id%TYPE) IS
1552 
1553  /*----------------------------------------------------------+
1554   | This is used in deciding which distribution record       |
1555   | to reverse and whether the amount has changed since last |
1556   | accounting due to ie an receipt application.             |
1557   +----------------------------------------------------------*/
1558 
1559   --MRC TRigger Replacement:  Enumerate Columns and select currency
1560   --sensitive data.
1561   CURSOR ps_cur IS
1562    select ps.amount_due_original,
1563           ps.amount_due_remaining,
1564 --{BUG4301323
1565 --          DECODE(g_ae_sys_rec.sob_type, 'P',
1566 --                    ps.acctd_amount_due_remaining,
1567 --                    arp_mrc_acct_main.get_ps_entity_data(
1568 --                      ps.payment_schedule_id,
1569 --                      g_ae_sys_rec.set_of_books_id)) acctd_amount_due_remaining
1570           ps.acctd_amount_due_remaining    acctd_amount_due_remaining
1571 --}
1572    from ar_payment_schedules ps
1573    where ps.payment_schedule_id = p_ps_id;
1574 
1575  l_ard_rec                     ar_distributions%ROWTYPE;
1576  l_prev_posted_trh_rec         ar_transaction_history%ROWTYPE;
1577  l_ps_rec                      ps_cur%ROWTYPE;
1578 
1579 BEGIN
1580   IF PG_DEBUG in ('Y', 'C') THEN
1581      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.reverse_old_acct()+');
1582   END IF;
1583 
1584  /*--------------------------------------+
1585   | Find previous posted history record  |
1586   +--------------------------------------*/
1587   find_prev_posted_hist_record(g_ae_doc_rec.source_id,l_prev_posted_trh_rec);
1588 
1589  /*-------------------------------------------------------------------+
1590   | Fetch the PS. This is used in deciding which distribution record  |
1591   | to reverse and whether the amount has changed since last          |
1592   | accounting due to ie an receipt application.                      |
1593   +-------------------------------------------------------------------*/
1594   OPEN ps_cur;
1595   FETCH ps_cur INTO l_ps_rec;
1596   CLOSE ps_cur;
1597 
1598  /*-------------------------------------------------------------------+
1599   | Find the accounting for the previous posted history record        |
1600   +-------------------------------------------------------------------*/
1601   find_rec_dist_record(l_prev_posted_trh_rec.transaction_history_id, SIGN(l_ps_rec.amount_due_original),l_ard_rec );
1602 
1603  /*-------------------------------------------------------------------+
1604   | If the amount remaining has changed after the previous accounting |
1605   | use the amount due remaining as the amount accounted              |
1606   +-------------------------------------------------------------------*/
1607   IF NVL(l_ps_rec.amount_due_remaining,0) <> 0 THEN
1608 
1609     IF (SIGN(l_ps_rec.amount_due_original) > 0) THEN
1610 
1611       IF ((nvl(l_ard_rec.AMOUNT_DR,0) <> NVL(l_ps_rec.amount_due_remaining,0)) OR (nvl(l_ard_rec.ACCTD_AMOUNT_DR,0) <> NVL(l_ps_rec.acctd_amount_due_remaining,0))) THEN
1612         l_ard_rec.AMOUNT_DR := l_ps_rec.amount_due_remaining;
1613         l_ard_rec.ACCTD_AMOUNT_DR := l_ps_rec.acctd_amount_due_remaining;
1614       END IF;
1615 
1616     ELSIF (SIGN(l_ps_rec.amount_due_original) < 0) THEN
1617       IF  ((nvl(l_ard_rec.AMOUNT_CR,0) <> NVL(l_ps_rec.amount_due_remaining,0)) OR (nvl(l_ard_rec.ACCTD_AMOUNT_CR,0) <> NVL(l_ps_rec.acctd_amount_due_remaining,0))) THEN
1618         l_ard_rec.AMOUNT_CR := l_ps_rec.amount_due_remaining;
1619         l_ard_rec.ACCTD_AMOUNT_CR := l_ps_rec.acctd_amount_due_remaining;
1620       END IF;
1621     END IF;
1622   ELSE
1623    /*------------------------------------------------------+
1624     | BR was cancelled, we use the amounts on the previous |
1625     | record (no activities exist on cancelled BR), so here|
1626     | we leave the amounts as is and do nothing            |
1627     +------------------------------------------------------*/
1628     NULL;
1629   END IF;
1630 
1631  /*---------------------------------------------------------------------+
1632   | Reverse the accounting for the previous posted history record       |
1633   +---------------------------------------------------------------------*/
1634   reverse_single_dist_line(l_ard_rec);
1635 
1636   IF PG_DEBUG in ('Y', 'C') THEN
1637      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.reverse_old_acct()-');
1638   END IF;
1639 
1640   EXCEPTION
1641     WHEN OTHERS THEN
1642        IF PG_DEBUG in ('Y', 'C') THEN
1643           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.reverse_old_acct');
1644        END IF;
1645        RAISE;
1646 
1647 END reverse_old_acct;
1648 
1649 /* =======================================================================
1650  | PROCEDURE find_prev_posted_hist_record
1651  |
1652  | DESCRIPTION
1653  |      This function finds the previous posted history record. If we are
1654  |      not specifically looking for MATURITY_DATE events (by giving value
1655  |      MATURITY_DATE as parameter p_event) posted history records with
1656  |      MATURITY_DATE event are skipped since they have only deferred tax
1657  |      accounting. If status is given as parameter the function only looks
1658  |      at given statuses.
1659  |
1660  | PARAMETERS
1661  |      p_transaction_history_id     Transaction history ID
1662  |      p_status                     Transaction history status
1663  |      p_event                      Transaction history event
1664  * ======================================================================*/
1665 PROCEDURE find_prev_posted_hist_record(p_transaction_history_id IN  ar_transaction_history.transaction_history_id%TYPE,
1666                                        p_trh_rec                OUT NOCOPY ar_transaction_history%ROWTYPE,
1667                                        p_status                 IN  ar_transaction_history.status%TYPE DEFAULT NULL,
1668                                        p_event                  IN  ar_transaction_history.event%TYPE DEFAULT NULL) IS
1669 
1670  /*-----------------------------------------------------+
1671   | Cursor to return the previous posted history record |
1672   | The cursor returns path of all posted records from  |
1673   | the current one to the first posted record. If      |
1674   | status and /or evenrt was given as parameter the    |
1675   | cursor only looks for specific status and/or event. |
1676   +-----------------------------------------------------*/
1677   CURSOR history_cur IS
1678     SELECT *
1679     FROM ar_transaction_history
1680     WHERE postable_flag = 'Y'
1681     AND status = NVL(p_status,status)
1682     AND event  = NVL(p_event,event)
1683     CONNECT BY PRIOR prv_trx_history_id = transaction_history_id
1684     START WITH transaction_history_id = p_transaction_history_id
1685     ORDER BY transaction_history_id desc;
1686 
1687  history_rec              history_cur%ROWTYPE;
1688 
1689 BEGIN
1690  IF PG_DEBUG in ('Y', 'C') THEN
1691     arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.find_prev_posted_hist_record()');
1692  END IF;
1693 
1694  /*--------------------------+
1695   | Fetch the current record |
1696   +--------------------------*/
1697   OPEN history_cur;
1698   FETCH history_cur INTO history_rec;
1699 
1700  /*---------------------------------------------+
1701   | Loop through previous posted records in the |
1702   | reverse order they were created. Exit when  |
1703   | first posted record, which is not           |
1704   | MATURITY_DATE event is found                |
1705   +---------------------------------------------*/
1706   WHILE history_cur%FOUND LOOP
1707 
1708     FETCH history_cur INTO history_rec;
1709 
1710    /*----------------------------------+
1711     | Check whether we are looking for |
1712     | MATURITY_DATE event or not       |
1713     +----------------------------------*/
1714 
1715     IF p_event = C_MATURITY_DATE THEN
1716 
1717      /*-------------------------------------+
1718       | Exit with last posted MATURITY_DATE |
1719       | transaction history record          |
1720       +-------------------------------------*/
1721       IF history_rec.event = C_MATURITY_DATE THEN
1722         EXIT;
1723       END IF;
1724 
1725     ELSE
1726 
1727      /*-----------------------------------------------------+
1728       | In all other cases MATURITY_DATE events are skipped |
1729       | since they have only deferred tax related to them   |
1730       +-----------------------------------------------------*/
1731       IF history_rec.event <> C_MATURITY_DATE THEN
1732         EXIT;
1733       END IF;
1734 
1735     END IF;
1736   END LOOP;
1737 
1738   CLOSE history_cur;
1739 
1740   IF history_rec.transaction_history_id IS NULL THEN
1741     IF PG_DEBUG in ('Y', 'C') THEN
1742        arp_standard.debug('find_prev_posted_hist_record: ' || 'Previous posted history record cannot be found.');
1743     END IF;
1744     RAISE NO_DATA_FOUND;
1745   END IF;
1746 
1747   p_trh_rec := history_rec;
1748 
1749   EXCEPTION
1750     WHEN OTHERS THEN
1751        IF PG_DEBUG in ('Y', 'C') THEN
1752           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.find_prev_posted_hist_record');
1753        END IF;
1754        RAISE;
1755 
1756 END find_prev_posted_hist_record;
1757 
1758 /* =======================================================================
1759  | PROCEDURE find_rec_dist_record
1760  |
1761  | DESCRIPTION
1762  |      This procedure find receivable accounting on a
1763  |      transaction history record.
1764  |
1765  | PARAMETERS
1766  |      p_transaction_history_id     Transaction history ID
1767  |      p_sign                       Sign of the original BR amount, used to decide
1768  |                                     whether to select debut or credit amount
1769  |      p_dist_rec                   Distribution record
1770  * ======================================================================*/
1771 PROCEDURE find_rec_dist_record(p_transaction_history_id IN  ar_transaction_history.transaction_history_id%TYPE,
1772                                p_sign                   IN  NUMBER,
1773                                p_dist_rec               OUT NOCOPY ar_distributions%ROWTYPE) IS
1774 
1775  /*-----------------------------------------------------+
1776   | Cursor to return the accounting record for          |
1777   | given transaction history record. This is used to   |
1778   | in reclassification reversing the                   |
1779   | receivable accounting.                              |
1780   +-----------------------------------------------------*/
1781 
1782   -- MRC Trigger Replacement:  Enumerated cursor and added union for
1783   -- Reporting SOB data
1784   CURSOR distribution_cur IS
1785     select line_id,
1786            source_id,
1787            source_table,
1788            source_type,
1789            code_combination_id,
1790            amount_dr,
1791            amount_cr,
1792            acctd_amount_dr,
1793            acctd_amount_cr,
1794            creation_date,
1795            created_by,
1796            last_updated_by,
1797            last_update_date,
1798            last_update_login,
1799            org_id,
1800            source_table_secondary,
1801            source_id_secondary,
1802            currency_code,
1803            currency_conversion_rate,
1804            currency_conversion_type,
1805            currency_conversion_date,
1806            taxable_entered_dr,
1807            taxable_entered_cr,
1808            taxable_accounted_dr,
1809            taxable_accounted_cr,
1810            tax_link_id,
1811            third_party_id,
1812            third_party_sub_id,
1813            reversed_source_id,
1814            tax_code_id,
1815            location_segment_id,
1816            source_type_secondary,
1817            tax_group_code_id,
1818            --{BUG#2979254
1819            ref_customer_trx_line_id,
1820            ref_cust_trx_line_gl_dist_id,
1821            ref_line_id,
1822            from_amount_dr,
1823            from_amount_cr,
1824            from_acctd_amount_dr,
1825            from_acctd_amount_cr
1826            --}
1827     from ar_distributions
1828     where NVL(g_ae_sys_rec.sob_type,'P') = 'P'
1829       and source_id = p_transaction_history_id
1830       and source_table = C_TH
1831       and source_type in (C_REC,C_FACTOR,C_REMITTANCE,C_UNPAIDREC)
1832       AND source_id_secondary is null
1833       AND source_table_secondary is null
1834       AND source_type_secondary is null
1835       and   (((sign(p_sign) > 0)
1836              and ((nvl(AMOUNT_DR,0) <> 0) OR (nvl(ACCTD_AMOUNT_DR,0) <> 0))
1837              and (nvl(AMOUNT_CR,0) = 0) and (nvl(ACCTD_AMOUNT_CR,0) = 0))
1838         OR ((sign(p_sign) < 0)
1839              and ((nvl(AMOUNT_CR,0) <> 0) OR (nvl(ACCTD_AMOUNT_CR,0) <> 0))
1840              and (nvl(AMOUNT_DR,0) = 0) and (nvl(ACCTD_AMOUNT_DR,0) = 0)))
1841    order by line_id desc;
1842 /*BUG4301323
1843    UNION
1844     select line_id,
1845            source_id,
1846            source_table,
1847            source_type,
1848            code_combination_id,
1849            amount_dr,
1850            amount_cr,
1851            acctd_amount_dr,
1852            acctd_amount_cr,
1853            creation_date,
1854            created_by,
1855            last_updated_by,
1856            last_update_date,
1857            last_update_login,
1858            org_id,
1859            source_table_secondary,
1860            source_id_secondary,
1861            currency_code,
1862            currency_conversion_rate,
1863            currency_conversion_type,
1864            currency_conversion_date,
1865            taxable_entered_dr,
1866            taxable_entered_cr,
1867            taxable_accounted_dr,
1868            taxable_accounted_cr,
1869            tax_link_id,
1870            third_party_id,
1871            third_party_sub_id,
1872            reversed_source_id,
1873            tax_code_id,
1874            location_segment_id,
1875            source_type_secondary,
1876            tax_group_code_id,
1877            --{BUG#2979254
1878            ref_customer_trx_line_id,
1879            ref_cust_trx_line_gl_dist_id,
1880            ref_line_id,
1881            from_amount_dr,
1882            from_amount_cr,
1883            from_acctd_amount_dr,
1884            from_acctd_amount_cr
1885            --}
1886     from ar_mc_distributions_all
1887     where g_ae_sys_rec.sob_type = 'R'
1888       and set_of_books_id = g_ae_sys_rec.set_of_books_id
1889       and source_id = p_transaction_history_id
1890       and source_table = C_TH
1891       and source_type in (C_REC,C_FACTOR,C_REMITTANCE,C_UNPAIDREC)
1892       AND source_id_secondary is null
1893       AND source_table_secondary is null
1894       AND source_type_secondary is null
1895       and   (((sign(p_sign) > 0)
1896              and ((nvl(AMOUNT_DR,0) <> 0) OR (nvl(ACCTD_AMOUNT_DR,0) <> 0))
1897              and (nvl(AMOUNT_CR,0) = 0) and (nvl(ACCTD_AMOUNT_CR,0) = 0))
1898         OR ((sign(p_sign) < 0)
1899              and ((nvl(AMOUNT_CR,0) <> 0) OR (nvl(ACCTD_AMOUNT_CR,0) <> 0))
1900              and (nvl(AMOUNT_DR,0) = 0) and (nvl(ACCTD_AMOUNT_DR,0) = 0)))
1901    order by line_id desc;
1902 */
1903  distribution_rec ar_distributions%ROWTYPE;
1904 
1905 BEGIN
1906  IF PG_DEBUG in ('Y', 'C') THEN
1907     arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.find_rec_dist_record()+');
1908  END IF;
1909 
1910  /*-----------------------------------+
1911   | Fetch the accounting record       |
1912   +-----------------------------------*/
1913   --{HYUDETUPT change the structure of this code so that it does not break everytime schema changes
1914   OPEN distribution_cur;
1915   FETCH distribution_cur INTO
1916            distribution_rec.line_id,
1917            distribution_rec.source_id,
1918            distribution_rec.source_table,
1919            distribution_rec.source_type,
1920            distribution_rec.code_combination_id,
1921            distribution_rec.amount_dr,
1922            distribution_rec.amount_cr,
1923            distribution_rec.acctd_amount_dr,
1924            distribution_rec.acctd_amount_cr,
1925            distribution_rec.creation_date,
1926            distribution_rec.created_by,
1927            distribution_rec.last_updated_by,
1928            distribution_rec.last_update_date,
1929            distribution_rec.last_update_login,
1930            distribution_rec.org_id,
1931            distribution_rec.source_table_secondary,
1932            distribution_rec.source_id_secondary,
1933            distribution_rec.currency_code,
1934            distribution_rec.currency_conversion_rate,
1935            distribution_rec.currency_conversion_type,
1936            distribution_rec.currency_conversion_date,
1937            distribution_rec.taxable_entered_dr,
1938            distribution_rec.taxable_entered_cr,
1939            distribution_rec.taxable_accounted_dr,
1940            distribution_rec.taxable_accounted_cr,
1941            distribution_rec.tax_link_id,
1942            distribution_rec.third_party_id,
1943            distribution_rec.third_party_sub_id,
1944            distribution_rec.reversed_source_id,
1945            distribution_rec.tax_code_id,
1946            distribution_rec.location_segment_id,
1947            distribution_rec.source_type_secondary,
1948            distribution_rec.tax_group_code_id,
1949            --{BUG#2979254
1950            distribution_rec.ref_customer_trx_line_id,
1951            distribution_rec.ref_cust_trx_line_gl_dist_id,
1952            distribution_rec.ref_line_id,
1953            distribution_rec.from_amount_dr,
1954            distribution_rec.from_amount_cr,
1955            distribution_rec.from_acctd_amount_dr,
1956            distribution_rec.from_acctd_amount_cr;
1957 
1958   IF distribution_cur%NOTFOUND THEN
1959 
1960     CLOSE distribution_cur;
1961     IF PG_DEBUG in ('Y', 'C') THEN
1962        arp_standard.debug('find_rec_dist_record: ' || 'Receivable accounting record cannot be found.');
1963     END IF;
1964     APP_EXCEPTION.raise_exception;
1965 
1966   END IF;
1967 
1968   IF distribution_cur%ISOPEN THEN
1969     CLOSE distribution_cur;
1970   END IF;
1971   --}
1972 
1973  /*------------------------------+
1974   | Return the accounting record |
1975   +------------------------------*/
1976   p_dist_rec := distribution_rec;
1977 
1978   IF PG_DEBUG in ('Y', 'C') THEN
1979      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.find_rec_dist_record()-');
1980   END IF;
1981 
1982   EXCEPTION
1983     WHEN OTHERS THEN
1984        IF PG_DEBUG in ('Y', 'C') THEN
1985           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.find_rec_dist_record');
1986        END IF;
1987        RAISE;
1988 
1989 END find_rec_dist_record;
1990 
1991 /* =======================================================================
1992  | PROCEDURE find_exchanged_trx_acct
1993  |
1994  | DESCRIPTION
1995  |      This function finds the original history record with stautus
1996  |      PENDING_REMITTANCE, which has the accounting for the exchanged
1997  |      transactions under it.
1998  |
1999  | PARAMETERS
2000  |      p_transaction_history_id     Transaction history ID
2001  * ======================================================================*/
2002 FUNCTION find_exchanged_trx_acct(p_transaction_history_id ar_transaction_history.transaction_history_id%TYPE) RETURN NUMBER IS
2003 
2004  l_prev_posted_history_id       ar_transaction_history.transaction_history_id%TYPE;
2005  l_prev_posted_trh_rec          ar_transaction_history%ROWTYPE;
2006 
2007 BEGIN
2008   IF PG_DEBUG in ('Y', 'C') THEN
2009      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.find_exchanged_trx_acct()');
2010   END IF;
2011 
2012  /*-------------------------------------------------------------------+
2013   | Store the current transaction history record id in local variable |
2014   | This is used as parameter in the loop.                            |
2015   +-------------------------------------------------------------------*/
2016   l_prev_posted_history_id := p_transaction_history_id;
2017 
2018   LOOP
2019 
2020    /*-------------------------------------------------------+
2021     | Find the previous posted transaction history record   |
2022     | with status PRENDING_REMITTANCE.                      |
2023     +-------------------------------------------------------*/
2024     find_prev_posted_hist_record(l_prev_posted_history_id,l_prev_posted_trh_rec,C_PENDING_REMITTANCE);
2025 
2026    /*-------------------------------------------------------+
2027     | Check whether the previous status for the record is   |
2028     | INCOMPLETE or PENDING_ACCEPTANCE. If so this record   |
2029     | has the exchanged transaction accounting under it so  |
2030     | exit the loop, otherwise loop to next                 |
2031     | PENDING_REMITTANCE record                             |
2032     +-------------------------------------------------------*/
2033     IF trx_history_status(l_prev_posted_trh_rec.prv_trx_history_id) in (C_INCOMPLETE,C_PENDING_ACCEPTANCE)
2034        OR l_prev_posted_trh_rec.transaction_history_id IS NULL THEN
2035 
2036       EXIT;
2037 
2038     END IF;
2039 
2040     l_prev_posted_history_id := l_prev_posted_trh_rec.transaction_history_id;
2041 
2042   END LOOP;
2043 
2044   IF l_prev_posted_trh_rec.transaction_history_id IS NULL THEN
2045 
2046     IF PG_DEBUG in ('Y', 'C') THEN
2047        arp_standard.debug('find_exchanged_trx_acct: ' || 'Previous transaction history record with exchange accountin cannot be found.');
2048     END IF;
2049     APP_EXCEPTION.raise_exception;
2050 
2051   END IF;
2052 
2053   RETURN l_prev_posted_trh_rec.transaction_history_id;
2054 
2055   EXCEPTION
2056     WHEN OTHERS THEN
2057        IF PG_DEBUG in ('Y', 'C') THEN
2058           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.find_exchanged_trx_acct');
2059        END IF;
2060        RAISE;
2061 
2062 END find_exchanged_trx_acct;
2063 
2064 /* =======================================================================
2065  | PROCEDURE reverse_exchanged_trx_acct
2066  |
2067  | DESCRIPTION
2068  |      This procedure reverses the accounting created for the exchanged
2069  |      transactions when BR was completed (to status PENDING_REMITTANCE).
2070  |
2071  | PARAMETERS
2072  |      p_transaction_history_id     Transaction history ID
2073  * ======================================================================*/
2074 PROCEDURE reverse_exchanged_trx_acct(p_transaction_history_id ar_transaction_history.transaction_history_id%TYPE) IS
2075 
2076  /*--------------------------------------------------+
2077   | Cursor to return the accounting for exchanged    |
2078   | transactions. The accounting for exchanged       |
2079   | transactions has the secondary columns populated |
2080   +--------------------------------------------------*/
2081 
2082   -- MRC Trigger Replacement:  Enumerated Columns and added UNION to
2083   -- select currency sensitive data.
2084 
2085   CURSOR last_exchange_accounting_cur(l_transaction_history_id ar_transaction_history.transaction_history_id%TYPE) IS
2086     select line_id,
2087            source_id,
2088            source_table,
2089            source_type,
2090            code_combination_id,
2091            amount_dr,
2092            amount_cr,
2093            acctd_amount_dr,
2094            acctd_amount_cr,
2095            creation_date,
2096            created_by,
2097            last_updated_by,
2098            last_update_date,
2099            last_update_login,
2100            org_id,
2101            source_table_secondary,
2102            source_id_secondary,
2103            currency_code,
2104            currency_conversion_rate,
2105            currency_conversion_type,
2106            currency_conversion_date,
2107            taxable_entered_dr,
2108            taxable_entered_cr,
2109            taxable_accounted_dr,
2110            taxable_accounted_cr,
2111            tax_link_id,
2112            third_party_id,
2113            third_party_sub_id,
2114            reversed_source_id,
2115            tax_code_id,
2116            location_segment_id,
2117            source_type_secondary,
2118            tax_group_code_id,
2119            --{BUG#2979254
2120            ref_customer_trx_line_id,
2121            ref_cust_trx_line_gl_dist_id,
2122            ref_line_id,
2123            from_amount_dr,
2124            from_amount_cr,
2125            from_acctd_amount_dr,
2126            from_acctd_amount_cr
2127            --}
2128     FROM ar_distributions
2129     WHERE NVL(g_ae_sys_rec.sob_type,'P') = 'P'
2130     AND source_id = l_transaction_history_id
2131     AND source_table = C_TH
2132     AND source_type =  C_REC
2133     AND source_id_secondary is not null
2134     AND source_table_secondary = C_CTL
2135     AND source_type_secondary = C_ASSIGNMENT
2136     ORDER BY line_id ASC;
2137 /*BUG4301323
2138   UNION
2139     select line_id,
2140            source_id,
2141            source_table,
2142            source_type,
2143            code_combination_id,
2144            amount_dr,
2145            amount_cr,
2146            acctd_amount_dr,
2147            acctd_amount_cr,
2148            creation_date,
2149            created_by,
2150            last_updated_by,
2151            last_update_date,
2152            last_update_login,
2153            org_id,
2154            source_table_secondary,
2155            source_id_secondary,
2156            currency_code,
2157            currency_conversion_rate,
2158            currency_conversion_type,
2159            currency_conversion_date,
2160            taxable_entered_dr,
2161            taxable_entered_cr,
2162            taxable_accounted_dr,
2163            taxable_accounted_cr,
2164            tax_link_id,
2165            third_party_id,
2166            third_party_sub_id,
2167            reversed_source_id,
2168            tax_code_id,
2169            location_segment_id,
2170            source_type_secondary,
2171            tax_group_code_id,
2172            --{BUG#2979254
2173            ref_customer_trx_line_id,
2174            ref_cust_trx_line_gl_dist_id,
2175            ref_line_id,
2176            from_amount_dr,
2177            from_amount_cr,
2178            from_acctd_amount_dr,
2179            from_acctd_amount_cr
2180            --}
2181     FROM ar_mc_distributions_all
2182     WHERE g_ae_sys_rec.sob_type = 'R'
2183     AND set_of_books_id = g_ae_sys_rec.set_of_books_id
2184     AND source_id = l_transaction_history_id
2185     AND source_table = C_TH
2186     AND source_type =  C_REC
2187     AND source_id_secondary is not null
2188     AND source_table_secondary = C_CTL
2189     AND source_type_secondary = C_ASSIGNMENT
2190     ORDER BY line_id ASC;
2191 */
2192  last_exchange_accounting_rec ar_distributions%ROWTYPE;
2193  l_last_exchanged_history_id ar_transaction_history.transaction_history_id%TYPE;
2194 
2195 BEGIN
2196  IF PG_DEBUG in ('Y', 'C') THEN
2197     arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.reverse_exchanged_trx_acct()+');
2198  END IF;
2199 
2200  /*-------------------------------------------------------+
2201   | Find the original posted transaction history record   |
2202   | with status PENDING_REMITTANCE.                       |
2203   +-------------------------------------------------------*/
2204   l_last_exchanged_history_id := find_exchanged_trx_acct(p_transaction_history_id);
2205 
2206  /*-------------------------------------------------------+
2207   | Loop through the exchanged transaction accounting     |
2208   | under the transaction history record                  |
2209   +-------------------------------------------------------*/
2210   OPEN last_exchange_accounting_cur(l_last_exchanged_history_id);
2211   LOOP
2212      FETCH last_exchange_accounting_cur INTO
2213 --      FOR last_exchange_accounting_rec IN last_exchange_accounting_cur(l_last_exchanged_history_id) LOOP
2214            last_exchange_accounting_rec.line_id,
2215            last_exchange_accounting_rec.source_id,
2216            last_exchange_accounting_rec.source_table,
2217            last_exchange_accounting_rec.source_type,
2218            last_exchange_accounting_rec.code_combination_id,
2219            last_exchange_accounting_rec.amount_dr,
2220            last_exchange_accounting_rec.amount_cr,
2221            last_exchange_accounting_rec.acctd_amount_dr,
2222            last_exchange_accounting_rec.acctd_amount_cr,
2223            last_exchange_accounting_rec.creation_date,
2224            last_exchange_accounting_rec.created_by,
2225            last_exchange_accounting_rec.last_updated_by,
2226            last_exchange_accounting_rec.last_update_date,
2227            last_exchange_accounting_rec.last_update_login,
2228            last_exchange_accounting_rec.org_id,
2229            last_exchange_accounting_rec.source_table_secondary,
2230            last_exchange_accounting_rec.source_id_secondary,
2231            last_exchange_accounting_rec.currency_code,
2232            last_exchange_accounting_rec.currency_conversion_rate,
2233            last_exchange_accounting_rec.currency_conversion_type,
2234            last_exchange_accounting_rec.currency_conversion_date,
2235            last_exchange_accounting_rec.taxable_entered_dr,
2236            last_exchange_accounting_rec.taxable_entered_cr,
2237            last_exchange_accounting_rec.taxable_accounted_dr,
2238            last_exchange_accounting_rec.taxable_accounted_cr,
2239            last_exchange_accounting_rec.tax_link_id,
2240            last_exchange_accounting_rec.third_party_id,
2241            last_exchange_accounting_rec.third_party_sub_id,
2242            last_exchange_accounting_rec.reversed_source_id,
2243            last_exchange_accounting_rec.tax_code_id,
2244            last_exchange_accounting_rec.location_segment_id,
2245            last_exchange_accounting_rec.source_type_secondary,
2246            last_exchange_accounting_rec.tax_group_code_id,
2247            --{BUG#2979254
2248            last_exchange_accounting_rec.ref_customer_trx_line_id,
2249            last_exchange_accounting_rec.ref_cust_trx_line_gl_dist_id,
2250            last_exchange_accounting_rec.ref_line_id,
2251            last_exchange_accounting_rec.from_amount_dr,
2252            last_exchange_accounting_rec.from_amount_cr,
2253            last_exchange_accounting_rec.from_acctd_amount_dr,
2254            last_exchange_accounting_rec.from_acctd_amount_cr;
2255 
2256       EXIT WHEN last_exchange_accounting_cur%NOTFOUND;
2257    /*--------------------------------------------------+
2258     | Reverse the exchanged transaction accounting     |
2259     +--------------------------------------------------*/
2260     reverse_single_dist_line(last_exchange_accounting_rec);
2261 
2262   END LOOP;
2263   CLOSE last_exchange_accounting_cur;
2264 
2265  IF PG_DEBUG in ('Y', 'C') THEN
2266     arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.reverse_exchanged_trx_acct()-');
2267  END IF;
2268 
2269   EXCEPTION
2270     WHEN OTHERS THEN
2271        IF PG_DEBUG in ('Y', 'C') THEN
2272           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.reverse_exchanged_trx_acct');
2273        END IF;
2274        RAISE;
2275 END reverse_exchanged_trx_acct;
2276 
2277 /* =======================================================================
2278  | PROCEDURE create_exchanged_trx_acct
2279  |
2280  | DESCRIPTION
2281  |      This procedure creates the accounting for the exchanged
2282  |      transactions when BR is completed (to status PENDING_REMITTANCE).
2283  |
2284  | PARAMETERS
2285  |      p_customer_trx_id     Transaction ID
2286  * ======================================================================*/
2287 PROCEDURE create_exchanged_trx_acct
2288 (p_customer_trx_id IN ra_customer_trx.customer_trx_id%TYPE,
2289  -- HYUDETDIST
2290  p_ae_rule_rec     IN ae_rule_rec_type)
2291  --}
2292  IS
2293  /*--------------------------------------------------+
2294   | Cursor to return all rows exchanged on the BR.   |
2295   +--------------------------------------------------*/
2296 
2297   -- MRC Trigger Replacement:  select currency sensitive data.
2298   CURSOR all_exchanges_cur(l_customer_trx_id ra_customer_trx.customer_trx_id%TYPE) IS
2299     SELECT ctl.br_ref_customer_trx_id          source_trx_id,
2300            ctl.br_ref_payment_schedule_id      source_ps_id,
2301            ctl.customer_trx_line_id,
2302            adj.amount       adjustment_amount,
2303 --{BUG4301323
2304 --           decode(g_ae_sys_rec.sob_type, 'P',
2305 --                  adj.acctd_amount,
2306 --                  arp_mrc_acct_main.get_adj_entity_data(
2307 --                     adj.adjustment_id,
2308 --                     g_ae_sys_rec.set_of_books_id))  adjustment_acctd_amount,
2309            adj.acctd_amount    adjustment_acctd_amount,
2310 --}
2311            --{HYUDETDIST
2312            adj.adjustment_id                 adjustment_id,
2313            adj.line_adjusted                 adjustment_line_amount,
2314            adj.freight_adjusted              adjustment_freight_amount,
2315            adj.tax_adjusted                  adjustment_tax_amount,
2316            adj.receivables_charges_adjusted  adjustment_charges_amount,
2317            adj.payment_schedule_id           adjustment_ps_id,
2318            adj.type                          adjustment_type,
2319            --}
2320 --{BUG#5016132
2321            adj.receivables_trx_id            adj_rec_trx_id,
2322            adj.adjustment_type               adj_adj_type,
2323 --}
2324            assignment_ps.class,
2325            assignment_ps.customer_id          assignment_customer_id,
2326            assignment_ps.customer_site_use_id assignment_site_use_id,
2327            assignment_ps.amount_due_original  assignment_amount_due_original,
2328            ps.invoice_currency_code,
2329 --{BUG4301323
2330 --           decode(g_ae_sys_rec.sob_type, 'P',
2331 --                  ps.exchange_rate,
2332 --                  arp_mrc_acct_main.get_ps_exg_rate(
2333 --                        ps.payment_schedule_id,
2334 --                        g_ae_sys_rec.set_of_books_id)) exchange_rate,
2335            ps.exchange_rate              exchange_rate,
2336 --}
2337 --{BUG4301323
2338 --           decode(g_ae_sys_rec.sob_type, 'P',
2339 --                  ps.exchange_rate_type,
2340 --                  arp_mrc_acct_main.get_ps_exg_rate_type(
2341 --                        ps.payment_schedule_id,
2342 --                        g_ae_sys_rec.set_of_books_id)) exchange_rate_type,
2343            ps.exchange_rate_type          exchange_rate_type,
2344 --}
2345 --{BUG4301323
2346 --           decode(g_ae_sys_rec.sob_type, 'P',
2347 --                  ps.exchange_date,
2348 --                  arp_mrc_acct_main.get_ps_exg_date(
2349 --                        ps.payment_schedule_id,
2350 --                        g_ae_sys_rec.set_of_books_id)) exchange_date
2351             ps.exchange_date              exchange_date
2352 --}
2353     FROM ra_customer_trx_lines ctl,
2354          ar_payment_schedules ps,
2355          ar_payment_schedules assignment_ps,
2356          ra_customer_trx ct,
2357          ar_adjustments adj
2358     WHERE assignment_ps.payment_schedule_id = ctl.br_ref_payment_schedule_id
2359     AND   ps.customer_trx_id                = ct.customer_trx_id
2360     AND   adj.adjustment_id                 = ctl.br_adjustment_id
2361     AND   ctl.customer_trx_id               = ct.customer_trx_id
2362     AND   ct.customer_trx_id                = l_customer_trx_id;
2363 
2364   CURSOR c_trx(p_customer_trx_id  IN NUMBER)
2365   IS
2366   SELECT * FROM ra_customer_trx
2367   WHERE customer_trx_id = p_customer_trx_id;
2368 
2369 
2370  /*--------------------------------------------------+
2371   | Cursor for the accounting on normal transactions |
2372   | from table ra_cust_trx_line_gl_dist              |
2373   +--------------------------------------------------*/
2374   CURSOR TRX_exchange_cur(l_customer_trx_id ra_customer_trx.customer_trx_id%TYPE) IS
2375     SELECT dist.code_combination_id
2376     FROM   ra_cust_trx_line_gl_dist dist
2377     WHERE  dist.customer_trx_id = l_customer_trx_id
2378     AND    dist.account_class = C_REC
2379     AND    dist.latest_rec_flag = 'Y';
2380 
2381  /*--------------------------------------------------+
2382   | Cursor for the currenct accounted record of the  |
2383   | exchanged BR                                     |
2384   +--------------------------------------------------*/
2385   CURSOR BR_exchange_cur(l_customer_trx_id ra_customer_trx.customer_trx_id%TYPE) IS
2386     SELECT th.transaction_history_id
2387     FROM   ar_transaction_history th
2388     WHERE  th.customer_trx_id = l_customer_trx_id
2389     AND    th.current_accounted_flag = 'Y';
2390 
2391   all_exchanges_rec all_exchanges_cur%ROWTYPE;
2392   TRX_exchange_rec TRX_exchange_cur%ROWTYPE;
2393   BR_exchange_rec BR_exchange_cur%ROWTYPE;
2394   BR_dist_rec ar_distributions%ROWTYPE;
2395 
2396   l_ael_line_rec         ae_line_rec_type;
2397   l_ael_empty_line_rec   ae_line_rec_type;
2398   --{HYUDETDIST
2399   l_adj_rec              ar_adjustments%ROWTYPE;
2400   l_cust_trx_rec         ra_customer_trx%ROWTYPE;
2401   l_app_rec              ar_receivable_applications%ROWTYPE;
2402   l_ae_ctr               NUMBER := 0;
2403   l_ae_line_tbl          ae_line_tbl_type;
2404   l_ae_empty_line_tbl    ae_line_tbl_type;
2405   l_current_trx_id       NUMBER := -9999;
2406   --}
2407 
2408 BEGIN
2409   IF PG_DEBUG in ('Y', 'C') THEN
2410      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.create_exchanged_trx_acct()+');
2411   END IF;
2412 
2413 
2414  /*--------------------------------------------------+
2415   | Loop through all transactions exchanged for BR   |
2416   +--------------------------------------------------*/
2417   FOR all_exchanges_rec IN all_exchanges_cur(p_customer_trx_id) LOOP
2418 
2419     --{HYUDETDIST
2420     --Get the shadow adjustment data
2421     l_adj_rec.customer_trx_id  := all_exchanges_rec.source_trx_id;
2422     l_adj_rec.amount           := all_exchanges_rec.adjustment_amount;
2423     l_adj_rec.acctd_amount     := all_exchanges_rec.adjustment_acctd_amount;
2424     l_adj_rec.line_adjusted    := all_exchanges_rec.adjustment_line_amount;
2425     l_adj_rec.freight_adjusted := all_exchanges_rec.adjustment_freight_amount;
2426     l_adj_rec.tax_adjusted     := all_exchanges_rec.adjustment_tax_amount;
2427     l_adj_rec.receivables_charges_adjusted  := all_exchanges_rec.adjustment_charges_amount;
2428     l_adj_rec.adjustment_id    := all_exchanges_rec.adjustment_id;
2429     l_adj_rec.payment_schedule_id    := all_exchanges_rec.adjustment_ps_id;
2430     l_adj_rec.type             := all_exchanges_rec.adjustment_type;
2431     --{BUG#5016123
2432     l_adj_rec.receivables_trx_id :=  all_exchanges_rec.adj_rec_trx_id;
2433     l_adj_rec.adjustment_type    :=  all_exchanges_rec.adj_adj_type;
2434     --}
2435     -- Get the assigned invoice row
2436     OPEN c_trx(p_customer_trx_id  => all_exchanges_rec.source_trx_id);
2437     FETCH c_trx INTO l_cust_trx_rec;
2438     CLOSE c_trx;
2439 
2440    /*--------------------------+
2441     | Initialize build record  |
2442     +--------------------------*/
2443     l_ael_line_rec := l_ael_empty_line_rec ;
2444     l_ae_line_tbl  := l_ae_empty_line_tbl ;
2445 
2446     -- Call allocation in simulation mode
2447     arp_allocation_pkg.Allocate_Tax (
2448                   p_ae_doc_rec    =>      g_ae_doc_rec,
2449                   p_ae_event_rec  =>      g_ae_event_rec,
2450                   p_ae_rule_rec   =>      p_ae_rule_rec,
2451                   p_app_rec       =>      l_app_rec,
2452                   p_cust_inv_rec  =>      l_cust_trx_rec,
2453                   p_adj_rec       =>      l_adj_rec,
2454                   p_ae_ctr        =>      l_ae_ctr,
2455                   p_ae_line_tbl   =>      l_ae_line_tbl,
2456                   p_br_cust_trx_line_id => all_exchanges_rec.source_trx_id,
2457                   p_simul_app     =>      'Y',
2458                   p_from_llca_call =>     'N',
2459                   p_gt_id          =>     NULL);
2460 
2461     --
2462     -- Create the detail distributions
2463     --
2464     IF l_ae_ctr <> 0 THEN
2465 
2466       FOR i IN l_ae_line_tbl.FIRST .. l_ae_line_tbl.LAST LOOP
2467 
2468      /*---------------------------+
2469       | Deduct debits and credits |
2470       +---------------------------*/
2471       -- Need top switch sign here not at adjustment for balance at line level
2472       l_ael_line_rec.entered_cr         := l_ae_line_tbl(i).entered_dr;
2473       l_ael_line_rec.entered_dr         := l_ae_line_tbl(i).entered_cr;
2474       l_ael_line_rec.accounted_cr       := l_ae_line_tbl(i).accounted_dr;
2475       l_ael_line_rec.accounted_dr       := l_ae_line_tbl(i).accounted_cr;
2476       l_ael_line_rec.from_amount_cr     := l_ae_line_tbl(i).from_amount_dr;
2477       l_ael_line_rec.from_amount_dr     := l_ae_line_tbl(i).from_amount_cr;
2478       l_ael_line_rec.taxable_entered_cr := l_ae_line_tbl(i).taxable_entered_dr;
2479       l_ael_line_rec.taxable_entered_dr := l_ae_line_tbl(i).taxable_entered_cr;
2480       l_ael_line_rec.taxable_accounted_cr := l_ae_line_tbl(i).taxable_accounted_dr;
2481       l_ael_line_rec.taxable_accounted_dr := l_ae_line_tbl(i).taxable_accounted_cr;
2482       --
2483       l_ael_line_rec.ref_customer_trx_line_id     := l_ae_line_tbl(i).ref_customer_trx_line_id;
2484       l_ael_line_rec.ref_cust_trx_line_gl_dist_id := l_ae_line_tbl(i).ref_cust_trx_line_gl_dist_id;
2485       l_ael_line_rec.ref_line_id                  := l_ae_line_tbl(i).ref_line_id;
2486       l_ael_line_rec.activity_bucket                       := l_ae_line_tbl(i).activity_bucket;
2487       l_ael_line_rec.ref_account_class                    := l_ae_line_tbl(i).ref_account_class;
2488 
2489      /*---------------------+
2490       | Fill in the record  |
2491       +---------------------*/
2492       l_ael_line_rec.source_id    := g_ae_doc_rec.source_id;
2493       l_ael_line_rec.source_table := C_TH;
2494       l_ael_line_rec.ae_line_type := C_REC;
2495       l_ael_line_rec.currency_code            := all_exchanges_rec.invoice_currency_code;
2496       l_ael_line_rec.currency_conversion_rate := all_exchanges_rec.exchange_rate;
2497       l_ael_line_rec.currency_conversion_type := all_exchanges_rec.exchange_rate_type;
2498       l_ael_line_rec.currency_conversion_date := all_exchanges_rec.exchange_date;
2499       l_ael_line_rec.source_id_secondary      := all_exchanges_rec.customer_trx_line_id;
2500       l_ael_line_rec.third_party_id           := all_exchanges_rec.assignment_customer_id;
2501       l_ael_line_rec.third_party_sub_id       := all_exchanges_rec.assignment_site_use_id;
2502       l_ael_line_rec.ae_line_type_secondary   := C_ASSIGNMENT;
2503       l_ael_line_rec.source_table_secondary   := C_CTL;
2504 
2505 
2506       IF PG_DEBUG in ('Y', 'C') THEN
2507          arp_standard.debug('source id = ' || to_char(l_ael_line_rec.source_id));
2508          arp_standard.debug('currency code = ' || l_ael_line_rec.currency_code);
2509       END IF;
2510 
2511 
2512       IF NVL(all_exchanges_rec.class,'INV') <> 'BR' THEN
2513 
2514        /*--------------------------------------------------+
2515         | If exchanges transaction is not BR fetch account |
2516         | from table ra_cust_trx_line_gl_dist.             |
2517         +--------------------------------------------------*/
2518         IF l_current_trx_id   <> all_exchanges_rec.source_trx_id THEN
2519 
2520           OPEN TRX_exchange_cur(all_exchanges_rec.source_trx_id);
2521           FETCH TRX_exchange_cur INTO TRX_exchange_rec;
2522           l_ael_line_rec.account            := TRX_exchange_rec.code_combination_id;
2523           CLOSE TRX_exchange_cur;
2524 
2525           l_current_trx_id := all_exchanges_rec.source_trx_id;
2526 
2527         ELSE
2528           l_ael_line_rec.account            := TRX_exchange_rec.code_combination_id;
2529 
2530         END IF;
2531 
2532       ELSE
2533 
2534       /*--------------------------------------------------+
2535        | If exchanges transaction is  BR fetch the        |
2536        | current accounted history record.                |
2537        +--------------------------------------------------*/
2538         IF l_current_trx_id   <> all_exchanges_rec.source_trx_id THEN
2539 
2540           OPEN BR_exchange_cur(all_exchanges_rec.source_trx_id);
2541           FETCH BR_exchange_cur INTO BR_exchange_rec;
2542           CLOSE BR_exchange_cur;
2543          /*--------------------------------------------------+
2544           | Fetch the receivable accounting for the BR       |
2545           | and use that as account.                         |
2546           +--------------------------------------------------*/
2547           find_rec_dist_record(BR_exchange_rec.transaction_history_id,
2548                                SIGN(all_exchanges_rec.assignment_amount_due_original),
2549                                BR_dist_rec);
2550           l_ael_line_rec.account            := BR_dist_rec.code_combination_id;
2551 
2552         ELSE
2553           l_ael_line_rec.account            := BR_dist_rec.code_combination_id;
2554         END IF;
2555 
2556       END IF;
2557 
2558      /*---------------------------+
2559       | Assign AEL for TH record  |
2560       +---------------------------*/
2561       Assign_Ael_Elements( p_ae_line_rec 	=> l_ael_line_rec );
2562     END LOOP;
2563    END IF;
2564 
2565   END LOOP;
2566 
2567   IF PG_DEBUG in ('Y', 'C') THEN
2568      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.create_exchanged_trx_acct()-');
2569   END IF;
2570 
2571   EXCEPTION
2572     WHEN OTHERS THEN
2573        IF PG_DEBUG in ('Y', 'C') THEN
2574           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.create_exchanged_trx_acct');
2575        END IF;
2576        RAISE;
2577 
2578 END create_exchanged_trx_acct;
2579 
2580 /* =======================================================================
2581  | PROCEDURE reverse_deferred_tax
2582  |
2583  | DESCRIPTION
2584  |      This procedure reverses deferred tax on given transaction history record
2585  |
2586  | PARAMETERS
2587  |      p_transaction_history_id     Transaction history ID
2588  * ======================================================================*/
2589 PROCEDURE reverse_deferred_tax(p_transaction_history_id ar_transaction_history.transaction_history_id%TYPE) IS
2590 
2591  /*--------------------------------------------------+
2592   | Cursor to return the tax accounting for given    |
2593   | transactions history record                      |
2594   +--------------------------------------------------*/
2595 
2596   -- MRC Trigger Replacment: enumerated columns and added union to select
2597   -- currency sensitive data.
2598 
2599   CURSOR tax_accounting_cur(l_transaction_history_id ar_transaction_history.transaction_history_id%TYPE) IS
2600     select line_id,
2601            source_id,
2602            source_table,
2603            source_type,
2604            code_combination_id,
2605            amount_dr,
2606            amount_cr,
2607            acctd_amount_dr,
2608            acctd_amount_cr,
2609            creation_date,
2610            created_by,
2611            last_updated_by,
2612            last_update_date,
2613            last_update_login,
2614            org_id,
2615            source_table_secondary,
2616            source_id_secondary,
2617            currency_code,
2618            currency_conversion_rate,
2619            currency_conversion_type,
2620            currency_conversion_date,
2621            taxable_entered_dr,
2622            taxable_entered_cr,
2623            taxable_accounted_dr,
2624            taxable_accounted_cr,
2625            tax_link_id,
2626            third_party_id,
2627            third_party_sub_id,
2628            reversed_source_id,
2629            tax_code_id,
2630            location_segment_id,
2631            source_type_secondary,
2632            tax_group_code_id,
2633            --{BUG#2979254
2634            ref_customer_trx_line_id,
2635            ref_cust_trx_line_gl_dist_id,
2636            ref_line_id,
2637            from_amount_dr,
2638            from_amount_cr,
2639            from_acctd_amount_dr,
2640            from_acctd_amount_cr
2641            --}
2642     FROM ar_distributions
2643     WHERE NVL(g_ae_sys_rec.sob_type,'P') = 'P'
2644     AND source_id = l_transaction_history_id
2645     AND source_table = C_TH
2646     AND source_type in (C_DEFERRED_TAX,C_TAX)
2647     ORDER BY line_id ASC;
2648 /*BUG4301323
2649  UNION
2650     select line_id,
2651            source_id,
2652            source_table,
2653            source_type,
2654            code_combination_id,
2655            amount_dr,
2656            amount_cr,
2657            acctd_amount_dr,
2658            acctd_amount_cr,
2659            creation_date,
2660            created_by,
2661            last_updated_by,
2662            last_update_date,
2663            last_update_login,
2664            org_id,
2665            source_table_secondary,
2666            source_id_secondary,
2667            currency_code,
2668            currency_conversion_rate,
2669            currency_conversion_type,
2670            currency_conversion_date,
2671            taxable_entered_dr,
2672            taxable_entered_cr,
2673            taxable_accounted_dr,
2674            taxable_accounted_cr,
2675            tax_link_id,
2676            third_party_id,
2677            third_party_sub_id,
2678            reversed_source_id,
2679            tax_code_id,
2680            location_segment_id,
2681            source_type_secondary,
2682            tax_group_code_id,
2683            --{BUG#2979254
2684            ref_customer_trx_line_id,
2685            ref_cust_trx_line_gl_dist_id,
2686            ref_line_id,
2687            from_amount_dr,
2688            from_amount_cr,
2689            from_acctd_amount_dr,
2690            from_acctd_amount_cr
2691            --}
2692     FROM ar_mc_distributions_all
2693     WHERE g_ae_sys_rec.sob_type = 'R'
2694     AND set_of_books_id = g_ae_sys_rec.set_of_books_id
2695     AND source_id = l_transaction_history_id
2696     AND source_table = C_TH
2697     AND source_type in (C_DEFERRED_TAX,C_TAX)
2698     ORDER BY line_id ASC;
2699 */
2700  tax_accounting_rec ar_distributions%ROWTYPE;
2701  l_prev_posted_trh_rec   ar_transaction_history%ROWTYPE;
2702 
2703 BEGIN
2704   IF PG_DEBUG in ('Y', 'C') THEN
2705      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.reverse_deferred_tax()+');
2706   END IF;
2707 
2708   BEGIN
2709    /*-------------------------------------------------------+
2710     | Find the original posted transaction history record   |
2711     | with MATURITY_DATE event .                            |
2712     +-------------------------------------------------------*/
2713     find_prev_posted_hist_record(p_transaction_history_id,l_prev_posted_trh_rec,NULL,C_MATURITY_DATE);
2714 
2715   EXCEPTION
2716     WHEN NO_DATA_FOUND THEN
2717       /*---------------------------------------------+
2718        | No deferred tax exists, so none is reversed |
2719        +---------------------------------------------*/
2720        IF PG_DEBUG in ('Y', 'C') THEN
2721           arp_standard.debug('reverse_deferred_tax: ' || 'No Deferred tax exist, so none is reversed ');
2722        END IF;
2723        NULL;
2724 
2725   END;
2726 
2727  /*---------------------------------+
2728   | Only reverse tax, if tax exists |
2729   +---------------------------------*/
2730   IF l_prev_posted_trh_rec.transaction_history_id IS NOT NULL THEN
2731 
2732    /*-------------------------------------------------------+
2733     | Loop through the tax accounting under the transaction |
2734     | history record                                        |
2735     +-------------------------------------------------------*/
2736 --    FOR tax_accounting_rec IN tax_accounting_cur(l_prev_posted_trh_rec.transaction_history_id) LOOP
2737     --{HYUDETUPT change the structure of this code so that it does not break everytime schema changes
2738     OPEN tax_accounting_cur(l_prev_posted_trh_rec.transaction_history_id);
2739     LOOP
2740       FETCH tax_accounting_cur INTO
2741            tax_accounting_rec.line_id,
2742            tax_accounting_rec.source_id,
2743            tax_accounting_rec.source_table,
2744            tax_accounting_rec.source_type,
2745            tax_accounting_rec.code_combination_id,
2746            tax_accounting_rec.amount_dr,
2747            tax_accounting_rec.amount_cr,
2748            tax_accounting_rec.acctd_amount_dr,
2749            tax_accounting_rec.acctd_amount_cr,
2750            tax_accounting_rec.creation_date,
2751            tax_accounting_rec.created_by,
2752            tax_accounting_rec.last_updated_by,
2753            tax_accounting_rec.last_update_date,
2754            tax_accounting_rec.last_update_login,
2755            tax_accounting_rec.org_id,
2756            tax_accounting_rec.source_table_secondary,
2757            tax_accounting_rec.source_id_secondary,
2758            tax_accounting_rec.currency_code,
2759            tax_accounting_rec.currency_conversion_rate,
2760            tax_accounting_rec.currency_conversion_type,
2761            tax_accounting_rec.currency_conversion_date,
2762            tax_accounting_rec.taxable_entered_dr,
2763            tax_accounting_rec.taxable_entered_cr,
2764            tax_accounting_rec.taxable_accounted_dr,
2765            tax_accounting_rec.taxable_accounted_cr,
2766            tax_accounting_rec.tax_link_id,
2767            tax_accounting_rec.third_party_id,
2768            tax_accounting_rec.third_party_sub_id,
2769            tax_accounting_rec.reversed_source_id,
2770            tax_accounting_rec.tax_code_id,
2771            tax_accounting_rec.location_segment_id,
2772            tax_accounting_rec.source_type_secondary,
2773            tax_accounting_rec.tax_group_code_id,
2774            --{BUG#2979254
2775            tax_accounting_rec.ref_customer_trx_line_id,
2776            tax_accounting_rec.ref_cust_trx_line_gl_dist_id,
2777            tax_accounting_rec.ref_line_id,
2778            tax_accounting_rec.from_amount_dr,
2779            tax_accounting_rec.from_amount_cr,
2780            tax_accounting_rec.from_acctd_amount_dr,
2781            tax_accounting_rec.from_acctd_amount_cr;
2782 
2783       EXIT WHEN tax_accounting_cur%NOTFOUND;
2784      /*--------------------------------+
2785       | Reverse the tax accounting     |
2786       +--------------------------------*/
2787       reverse_single_dist_line(tax_accounting_rec);
2788 
2789     END LOOP;
2790     CLOSE tax_accounting_cur;
2791     --}
2792   END IF;
2793 
2794   IF PG_DEBUG in ('Y', 'C') THEN
2795      arp_standard.debug( 'ARP_BILLS_RECEIVABLE_MAIN.reverse_deferred_tax()-');
2796   END IF;
2797 
2798   EXCEPTION
2799     WHEN OTHERS THEN
2800        IF PG_DEBUG in ('Y', 'C') THEN
2801           arp_standard.debug('EXCEPTION: ARP_BILLS_RECEIVABLE_MAIN.reverse_deferred_tax');
2802        END IF;
2803        RAISE;
2804 END reverse_deferred_tax;
2805 
2806 END ARP_BILLS_RECEIVABLE_MAIN;