DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_SLA_EVENTS_UPG_PKG

Source


1 PACKAGE BODY FA_SLA_EVENTS_UPG_PKG as
2 /* $Header: FAEVUPGB.pls 120.88.12020000.2 2012/07/23 08:35:46 rmandali ship $   */
3 
4 Procedure Upgrade_Inv_Events (
5              p_start_rowid             IN            rowid,
6              p_end_rowid               IN            rowid,
7              p_batch_size              IN            number,
8              x_success_count              OUT NOCOPY number,
9              x_failure_count              OUT NOCOPY number,
10              x_return_status              OUT NOCOPY number
11             ) IS
12 
13    c_application_id        constant number(15) := 140;
14    c_upgrade_bugno         constant number(15) := -4107161;
15    c_fnd_user              constant number(15) := 2;
16 
17    c_entity_code           constant varchar2(30) := 'INTER_ASSET_TRANSACTIONS';
18    c_amb_context_code      constant varchar2(30) := 'DEFAULT';
19 
20    -- this value can be altered in order to process more of less per batch
21    l_batch_size             NUMBER;
22 
23    l_rows_processed         NUMBER;
24 
25    -- type for table variable
26    type num_tbl_type  is table of number        index by binary_integer;
27    type char_tbl_type is table of varchar2(150) index by binary_integer;
28    type date_tbl_type is table of date          index by binary_integer;
29    type rowid_tbl_type is table of rowid        index by binary_integer;
30 
31    -- used for bulk fetching
32    -- main cursor
33    l_event_id_tbl                               num_tbl_type;
34    l_src_asset_id_tbl                           num_tbl_type;
35    l_dest_asset_id_tbl                          num_tbl_type;
36    l_book_type_code_tbl                         char_tbl_type;
37    l_primary_set_of_books_id_tbl                num_tbl_type;
38    l_org_id_tbl                                 num_tbl_type;
39    l_src_trans_type_code_tbl                    char_tbl_type;
40    l_dest_trans_type_code_tbl                   char_tbl_type;
41    l_transaction_date_entered_tbl               date_tbl_type;
42    l_src_thid_tbl                               num_tbl_type;
43    l_dest_thid_tbl                              num_tbl_type;
44    l_period_counter_tbl                         num_tbl_type;
45    l_period_name_tbl                            char_tbl_type;
46    l_cal_period_close_date_tbl                  date_tbl_type;
47    l_src_rowid_tbl                              rowid_tbl_type;
48    l_dest_rowid_tbl                             rowid_tbl_type;
49    l_tr_rowid_tbl                               rowid_tbl_type;
50    l_trx_reference_id_tbl                       num_tbl_type;
51    l_src_hdr_desc_tbl                           char_tbl_type;
52    l_dest_hdr_desc_tbl                          char_tbl_type;
53    l_date_effective_tbl                         date_tbl_type;
54 
55    l_upg_batch_id                               number;
56    l_ae_header_id                               number;
57 
58    l_entity_id_tbl                              num_tbl_type;
59    l_event_class_code_tbl                       char_tbl_type;
60    l_rep_set_of_books_id_tbl                    num_tbl_type;
61    l_currency_code_tbl                          char_tbl_type;
62    l_je_category_name_tbl                       char_tbl_type;
63 
64    l_adj_thid_tbl                               num_tbl_type;
65    l_adj_line_id_tbl                            num_tbl_type;
66    l_xla_gl_sl_link_id_tbl                      num_tbl_type;
67    l_ae_line_num_tbl                            num_tbl_type;
68    l_debit_amount_tbl                           num_tbl_type;
69    l_credit_amount_tbl                          num_tbl_type;
70    l_ccid_tbl                                   num_tbl_type;
71    l_acct_class_code_tbl                        char_tbl_type;
72    l_line_def_owner_code_tbl                    char_tbl_type;
73    l_line_def_code_tbl                          char_tbl_type;
74    l_line_desc_tbl                              char_tbl_type;
75    l_gl_transfer_status_code_tbl                char_tbl_type;
76    l_je_batch_id_tbl                            num_tbl_type;
77    l_je_header_id_tbl                           num_tbl_type;
78    l_je_line_num_tbl                            num_tbl_type;
79    l_distribution_id_tbl                        num_tbl_type;
80    l_line_num_tbl                               num_tbl_type; -- Bug 6827893
81    l_ae_header_id_tbl                           num_tbl_type; -- Bug 6827893
82 
83    l_error_level_tbl                            char_tbl_type;
84    l_err_entity_id_tbl                          num_tbl_type;
85    l_err_event_id_tbl                           num_tbl_type;
86    l_err_ae_header_id_tbl                       num_tbl_type;
87    l_err_ae_line_num_tbl                        num_tbl_type;
88    l_err_temp_line_num_tbl                      num_tbl_type;
89    l_error_message_name_tbl                     char_tbl_type;
90 
91    CURSOR c_trans IS
92       select /*+ leading(tr) rowid(tr) */
93              xla_events_s.nextval,
94              th1.asset_id,
95              th2.asset_id,
96              tr.book_type_code,
97              bc.set_of_books_id,
98              bc.org_id,
99              th1.transaction_type_code,
100              th2.transaction_type_code,
101              greatest (th1.transaction_date_entered,
102                            dp.calendar_period_open_date), -- Bug 12533865
103              th1.transaction_header_id,
104              th2.transaction_header_id,
105              th1.rowid,
106              th2.rowid,
107              tr.rowid,
108              xla_transaction_entities_s.nextval,
109              decode(tr.transaction_type,
110                'RESERVE TRANSFER', 'RESERVE_TRANSFERS',
111                'INVOICE TRANSFER',
112                         decode (th1.transaction_type_code,
113                                 'CIP ADJUSTMENT',
114                                      decode (th2.transaction_type_code,
115                                           'CIP ADJUSTMENT',
116                                                'CIP_SOURCE_LINE_TRANSFERS',
117                                                'SOURCE_LINE_TRANSFERS'),
118                                 'SOURCE_LINE_TRANSFERS'),
119                 'OTHER'
120              ) event_class_code,
121              dp.period_name,
122              dp.period_counter,
123              dp.calendar_period_close_date,
124              tr.trx_reference_id,
125              lk1.description || ' - ' ||
126                 to_char(dp.calendar_period_close_date, 'DD-MON-RR'),
127              lk2.description || ' - ' ||
128                 to_char(dp.calendar_period_close_date, 'DD-MON-RR'),
129              nvl (decode(tr.transaction_type,
130                'RESERVE TRANSFER', bc.je_adjustment_category,
131                'INVOICE TRANSFER',
132                         decode (th1.transaction_type_code,
133                                 'CIP ADJUSTMENT',
134                                      decode (th2.transaction_type_code,
135                                           'CIP ADJUSTMENT',
136                                                bc.je_cip_adjustment_category,
137                                                bc.je_adjustment_category),
138                                 bc.je_adjustment_category),
139                 bc.je_adjustment_category
140              ), 'OTHER') je_category_name,
141              th1.date_effective
142       from   fa_transaction_headers th1,
143              fa_transaction_headers th2,
144              fa_book_controls bc,
145              fa_deprn_periods dp,
146              fa_trx_references tr,
147              fa_lookups_tl lk1,
148              fa_lookups_tl lk2,
149              gl_period_statuses ps
150       where  tr.rowid between p_start_rowid and p_end_rowid
151       and    ps.application_id = 101
152       and    ((ps.migration_status_code in ('P', 'U')) or
153               (dp.period_close_date is null))
154       and    substr(dp.xla_conversion_status, 1, 1) in
155              ('H', 'U', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
156       and    dp.xla_conversion_status not in ('UT', 'UA')
157       and    ps.set_of_books_id = bc.set_of_books_id
158       and    ps.period_name = dp.period_name
159       and    tr.transaction_type in ('INVOICE TRANSFER', 'RESERVE TRANSFER')
160       and    tr.book_type_code = th1.book_type_code
161       and    tr.src_asset_id = th1.asset_id
162       and    tr.src_transaction_header_id = th1.transaction_header_id
163       and    tr.book_type_code = th2.book_type_code
164       and    tr.dest_asset_id = th2.asset_id
165       and    tr.dest_transaction_header_id = th2.transaction_header_id
166       and    tr.book_type_code = bc.book_type_code
167       and    bc.book_type_code = dp.book_type_code
168       and    th1.date_effective between
169                 dp.period_open_date and nvl(dp.period_close_date, sysdate)
170       and    th1.member_transaction_header_id is null
171       and    th1.event_id is null
172       and    th1.transaction_type_code = lk1.lookup_code
173       and    lk1.lookup_type = 'FAXOLTRX'
174       and    th2.transaction_type_code = lk2.lookup_code
175       and    lk2.lookup_type = 'FAXOLTRX'
176       and    userenv('LANG') = lk1.language
177       and    userenv('LANG') = lk2.language
178       and exists
179       (
180        select 'x'
181        from   fa_adjustments adj
182        where  th1.transaction_header_id = adj.transaction_header_id
183        and    th1.book_type_code = adj.book_type_code
184        and    th1.asset_id = adj.asset_id
185       )
186       and    th2.date_effective between
187                 dp.period_open_date and nvl(dp.period_close_date, sysdate)
188       and    th2.member_transaction_header_id is null
189       and    th2.event_id is null
190       and exists
191       (
192        select 'x'
193        from   fa_adjustments adj
194        where  th2.transaction_header_id = adj.transaction_header_id
195        and    th2.book_type_code = adj.book_type_code
196        and    th2.asset_id = adj.asset_id
197       );
198 
199    -- Bug 6827893 : Added outerjoin between DD and gljh
200    --               Corrected the Reserve row (second union)
201    CURSOR c_adj (l_book_type_code        varchar2,
202                  l_asset_id              number,
203                  l_transaction_header_id number,
204                  l_date_effective        date) IS
205       select adj.transaction_header_id,
206              adj.adjustment_line_id,
207              decode (adj.debit_credit_flag,
208                      'DR', adj.adjustment_amount, null),
209              decode (adj.debit_credit_flag,
210                      'CR', adj.adjustment_amount, null),
211              adj.code_combination_id,
212              glsob.currency_code,
213              decode (adj.adjustment_type,
214                 'BONUS EXPENSE', 'EXPENSE',
215                 'BONUS RESERVE', 'ASSET',
216                 'CIP COST', 'ASSET',
217                 'COST', 'ASSET',
218                 'COST CLEARING', 'ASSET',
219                 'DEPRN ADJUST', 'EXPENSE',
220                 'EXPENSE', 'EXPENSE',
221                 'GRP COR RESERVE', 'ASSET',
222                 'GRP PRC RESERVE', 'ASSET',
223                 'INTERCO AP', 'LIABILITY',
224                 'INTERCO AR', 'ASSET',
225                 'NBV RETIRED', 'ASSET',
226                 'PROCEEDS', 'ASSET',
227                 'PROCEEDS CLR', 'ASSET',
228                 'REMOVALCOST', 'ASSET',
229                 'REMOVALCOST CLR', 'ASSET',
230                 'RESERVE', 'ASSET',
231                 'REVAL RESERVE', 'ASSET',
232                 'REVAL RSV RET', 'ASSET',
233                 'REVAL AMORT', 'EXPENSE',
234                 'REVAL EXPENSE', 'EXPENSE',
235                 'ASSET'),
236              lk.description,
237              decode (gljh.je_header_id, null,
238                                         decode (bc.gl_posting_allowed_flag, 'YES',
239                                                                             'N', 'NT'),
240                                         'Y'), -- Bug 12879380
241              gljh.je_batch_id,
242              adj.je_header_id,
243              nvl(adj.je_line_num, 0),
244              adj.distribution_id
245       from   fa_adjustments adj,
246              gl_sets_of_books glsob,
247              fa_book_controls bc,
248              fa_lookups_tl lk,
249              gl_je_headers gljh
250       where  bc.book_type_code = l_book_type_code
251       and    bc.set_of_books_id = glsob.set_of_books_id
252       and    bc.book_type_code = adj.book_type_code
253       and    adj.asset_id = l_asset_id
254       and    adj.transaction_header_id = l_transaction_header_id
255       and    adj.code_combination_id is not null
256       and    nvl(adj.track_member_flag, 'N') = 'N'
257       and    lk.lookup_type = 'JOURNAL ENTRIES'
258       and    lk.lookup_code = decode (adj.adjustment_type,
259                               'BONUS EXPENSE', 'BONUS DEPRECIATION EXPENSE',
260                               'BONUS RESERVE', 'BONUS DEPRECIATION RESERVE',
261                               'CIP COST', adj.source_type_code ||' COST',
262                               adj.source_type_code ||' '|| adj.adjustment_type)
263       and    userenv('LANG') = lk.language
264       and    adj.je_header_id = gljh.je_header_id (+)
265    UNION ALL
266       select adj.transaction_header_id,
267              adj.adjustment_line_id,
268              decode (adj.debit_credit_flag,
269                      'CR', adj.adjustment_amount, null),
270              decode (adj.debit_credit_flag,
271                      'DR', adj.adjustment_amount, null),
272              nvl(nvl(gljl.code_combination_id, da.deprn_reserve_account_ccid),
273                  cb.reserve_account_ccid),
274              glsob.currency_code,
275              decode (adj.adjustment_type,
276                 'BONUS EXPENSE', 'EXPENSE',
277                 'BONUS RESERVE', 'ASSET',
278                 'CIP COST', 'ASSET',
279                 'COST', 'ASSET',
280                 'COST CLEARING', 'ASSET',
281                 'DEPRN ADJUST', 'EXPENSE',
282                 'EXPENSE', 'ASSET',
283                 'GRP COR RESERVE', 'ASSET',
284                 'GRP PRC RESERVE', 'ASSET',
285                 'INTERCO AP', 'LIABILITY',
286                 'INTERCO AR', 'ASSET',
287                 'NBV RETIRED', 'ASSET',
288                 'PROCEEDS', 'ASSET',
289                 'PROCEEDS CLR', 'ASSET',
290                 'REMOVALCOST', 'ASSET',
291                 'REMOVALCOST CLR', 'ASSET',
292                 'RESERVE', 'ASSET',
293                 'REVAL RESERVE', 'ASSET',
294                 'REVAL RSV RET', 'ASSET',
295                 'REVAL AMORT', 'EXPENSE',
296                 'REVAL EXPENSE', 'EXPENSE',
297                 'ASSET'),
298              lk.description,
299              decode (gljh.je_header_id, null,
300                                         decode (bc.gl_posting_allowed_flag, 'YES',
301                                                                             'N', 'NT'),
302                                         'Y'), -- Bug 12879380
303              gljh.je_batch_id,
304              dd.je_header_id,
305              nvl(dd.deprn_reserve_je_line_num, 0),
306              adj.distribution_id
307       from   fa_adjustments adj,
308              gl_sets_of_books glsob,
309              fa_book_controls bc,
310              fa_lookups_tl lk,
311              gl_je_headers gljh,
312              fa_deprn_detail dd,
313              gl_je_lines gljl,
314              fa_distribution_accounts da,
315              fa_asset_history ah,
316              fa_category_books cb
317       where  bc.book_type_code = l_book_type_code
318       and    bc.set_of_books_id = glsob.set_of_books_id
319       and    bc.book_type_code = adj.book_type_code
320       and    adj.asset_id = l_asset_id
321       and    adj.transaction_header_id = l_transaction_header_id
322       and    adj.adjustment_type = 'EXPENSE'
323       and    adj.source_type_code in ('DEPRECIATION', 'CIP RETIREMENT',
324                                       'RETIREMENT')
325       and    adj.code_combination_id is not null
326       and    nvl(adj.track_member_flag, 'N') = 'N'
327       and    lk.lookup_type = 'JOURNAL ENTRIES'
328       and    lk.lookup_code = adj.source_type_code ||' RESERVE'
329       and    userenv('LANG') = lk.language
330       and    adj.asset_id = dd.asset_id (+)
331       and    adj.book_type_code = dd.book_type_code (+)
332       and    adj.distribution_id = dd.distribution_id (+)
333       and    adj.period_counter_created = dd.period_counter (+)
334       and    dd.je_header_id = gljl.je_header_id (+)
335       and    dd.deprn_reserve_je_line_num = gljl.je_line_num (+)
336       and    dd.je_header_id = gljh.je_header_id (+)
337       and    adj.book_type_code = da.book_type_code (+)
338       and    adj.distribution_id = da.distribution_id (+)
339       and    adj.asset_id = ah.asset_id
340       and    l_date_effective >= ah.date_effective
341       and    l_date_effective < nvl(ah.date_ineffective, sysdate+1)
342       and    ah.category_id = cb.category_id
343       and    bc.book_type_code = cb.book_type_code
344    UNION ALL
345       select adj.transaction_header_id,
346              adj.adjustment_line_id,
347              decode (adj.debit_credit_flag,
348                      'DR', adj.adjustment_amount, null),
349              decode (adj.debit_credit_flag,
350                      'CR', adj.adjustment_amount, null),
351              nvl(nvl(gljl.code_combination_id, da.deprn_reserve_account_ccid),
352                  cb.reserve_account_ccid),
353              glsob.currency_code,
354              decode (adj.adjustment_type,
355                 'BONUS EXPENSE', 'EXPENSE',
356                 'BONUS RESERVE', 'ASSET',
357                 'CIP COST', 'ASSET',
358                 'COST', 'ASSET',
359                 'COST CLEARING', 'ASSET',
360                 'DEPRN ADJUST', 'EXPENSE',
361                 'EXPENSE', 'EXPENSE',
362                 'GRP COR RESERVE', 'ASSET',
363                 'GRP PRC RESERVE', 'ASSET',
364                 'INTERCO AP', 'LIABILITY',
365                 'INTERCO AR', 'ASSET',
366                 'NBV RETIRED', 'ASSET',
367                 'PROCEEDS', 'ASSET',
368                 'PROCEEDS CLR', 'ASSET',
369                 'REMOVALCOST', 'ASSET',
370                 'REMOVALCOST CLR', 'ASSET',
371                 'RESERVE', 'ASSET',
372                 'REVAL RESERVE', 'ASSET',
373                 'REVAL RSV RET', 'ASSET',
374                 'REVAL AMORT', 'EXPENSE',
375                 'REVAL EXPENSE', 'EXPENSE',
376                 'ASSET'),
377              lk.description,
378              decode (gljh.je_header_id, null,
379                                         decode (bc.gl_posting_allowed_flag, 'YES',
380                                                                             'N', 'NT'),
381                                         'Y'), -- Bug 12879380
382              gljh.je_batch_id,
383              dd.je_header_id,
384              nvl(dd.deprn_reserve_je_line_num, 0),
385              adj.distribution_id
386       from   fa_adjustments adj,
387              gl_sets_of_books glsob,
388              fa_book_controls bc,
389              fa_lookups_tl lk,
390              gl_je_headers gljh,
391              fa_deprn_detail dd,
392              gl_je_lines gljl,
393              fa_distribution_accounts da,
394              fa_asset_history ah,
395              fa_category_books cb
396       where  bc.book_type_code = l_book_type_code
397       and    bc.set_of_books_id = glsob.set_of_books_id
398       and    bc.book_type_code = adj.book_type_code
399       and    adj.asset_id = l_asset_id
400       and    adj.transaction_header_id = l_transaction_header_id
401       and    adj.adjustment_type = 'BONUS EXPENSE'
402       and    adj.source_type_code = 'DEPRECIATION'
403       and    adj.code_combination_id is not null
404       and    nvl(adj.track_member_flag, 'N') = 'N'
405       and    lk.lookup_type = 'JOURNAL ENTRIES'
406       and    lk.lookup_code = 'BONUS DEPRECIATION RESERVE'
407       and    userenv('LANG') = lk.language
408       and    adj.asset_id = dd.asset_id (+)
409       and    adj.book_type_code = dd.book_type_code (+)
410       and    adj.distribution_id = dd.distribution_id (+)
411       and    adj.period_counter_created = dd.period_counter (+)
412       and    dd.je_header_id = gljl.je_header_id (+)
413       and    dd.bonus_deprn_rsv_je_line_num = gljl.je_line_num (+)
414       and    dd.je_header_id = gljh.je_header_id (+)
415       and    adj.book_type_code = da.book_type_code (+)
416       and    adj.distribution_id = da.distribution_id (+)
417       and    adj.asset_id = ah.asset_id
418       and    l_date_effective >= ah.date_effective
419       and    l_date_effective < nvl(ah.date_ineffective, sysdate+1)
420       and    ah.category_id = cb.category_id
421       and    bc.book_type_code = cb.book_type_code;
422 
423    CURSOR c_mc_books (l_book_type_code      varchar2) IS
424    select set_of_books_id
425      from fa_mc_book_controls
426     where book_type_code = l_book_type_code
427       and enabled_flag = 'Y';
428 
429    -- Bug 6827893 : Added outerjoin between DD and gljh
430    --               Corrected the Reserve row (second union)
431    CURSOR c_mc_adj (l_book_type_code        varchar2,
432                     l_asset_id              number,
433                     l_set_of_books_id       number,
434                     l_transaction_header_id number,
435                     l_date_effective        date) IS
436       select adj.transaction_header_id,
437              adj.adjustment_line_id,
438              decode (adj.debit_credit_flag,
439                      'DR', adj.adjustment_amount, null),
440              decode (adj.debit_credit_flag,
441                      'CR', adj.adjustment_amount, null),
442              adj.code_combination_id,
443              glsob.currency_code,
444              decode (adj.adjustment_type,
445                 'BONUS EXPENSE', 'EXPENSE',
446                 'BONUS RESERVE', 'ASSET',
447                 'CIP COST', 'ASSET',
448                 'COST', 'ASSET',
449                 'COST CLEARING', 'ASSET',
450                 'DEPRN ADJUST', 'EXPENSE',
451                 'EXPENSE', 'EXPENSE',
452                 'GRP COR RESERVE', 'ASSET',
453                 'GRP PRC RESERVE', 'ASSET',
454                 'INTERCO AP', 'LIABILITY',
455                 'INTERCO AR', 'ASSET',
456                 'NBV RETIRED', 'ASSET',
457                 'PROCEEDS', 'ASSET',
458                 'PROCEEDS CLR', 'ASSET',
459                 'REMOVALCOST', 'ASSET',
460                 'REMOVALCOST CLR', 'ASSET',
461                 'RESERVE', 'ASSET',
462                 'REVAL RESERVE', 'ASSET',
463                 'REVAL RSV RET', 'ASSET',
464                 'REVAL AMORT', 'EXPENSE',
465                 'REVAL EXPENSE', 'EXPENSE',
466                 'ASSET'),
467              lk.description,
468              decode (gljh.je_header_id, null,
469                                         decode (bcp.gl_posting_allowed_flag, 'YES',
470                                                                              'N', 'NT'),
471                                         'Y'), -- Bug 12879380
472              gljh.je_batch_id,
473              adj.je_header_id,
474              nvl(adj.je_line_num, 0),
475              adj.distribution_id
476       from   fa_mc_adjustments adj,
477              gl_sets_of_books glsob,
478              fa_mc_book_controls bc,
479              fa_book_controls bcp,
480              fa_lookups_tl lk,
481              gl_je_headers gljh
482       where  bc.book_type_code = l_book_type_code
483       and    bc.set_of_books_id = l_set_of_books_id
484       and    bcp.book_type_code = l_book_type_code
485       and    bc.enabled_flag = 'Y'
486       and    bc.set_of_books_id = glsob.set_of_books_id
487       and    bc.book_type_code = adj.book_type_code
488       and    bc.set_of_books_id = adj.set_of_books_id
489       and    adj.asset_id = l_asset_id
490       and    adj.transaction_header_id = l_transaction_header_id
491       and    adj.code_combination_id is not null
492       and    nvl(adj.track_member_flag, 'N') = 'N'
493       and    lk.lookup_type = 'JOURNAL ENTRIES'
494       and    lk.lookup_code = decode (adj.adjustment_type,
495                               'BONUS EXPENSE', 'BONUS DEPRECIATION EXPENSE',
496                               'BONUS RESERVE', 'BONUS DEPRECIATION RESERVE',
497                               'CIP COST', adj.source_type_code ||' COST',
498                               adj.source_type_code ||' '|| adj.adjustment_type)
499       and    userenv('LANG') = lk.language
500       and    adj.je_header_id = gljh.je_header_id (+)
501    UNION ALL
502       select adj.transaction_header_id,
503              adj.adjustment_line_id,
504              decode (adj.debit_credit_flag,
505                      'CR', adj.adjustment_amount, null),
506              decode (adj.debit_credit_flag,
507                      'DR', adj.adjustment_amount, null),
508              nvl(nvl(gljl.code_combination_id, da.deprn_reserve_account_ccid),
509                  cb.reserve_account_ccid),
510              glsob.currency_code,
511              decode (adj.adjustment_type,
512                 'BONUS EXPENSE', 'EXPENSE',
513                 'BONUS RESERVE', 'ASSET',
514                 'CIP COST', 'ASSET',
515                 'COST', 'ASSET',
516                 'COST CLEARING', 'ASSET',
517                 'DEPRN ADJUST', 'EXPENSE',
518                 'EXPENSE', 'ASSET',
519                 'GRP COR RESERVE', 'ASSET',
520                 'GRP PRC RESERVE', 'ASSET',
521                 'INTERCO AP', 'LIABILITY',
522                 'INTERCO AR', 'ASSET',
523                 'NBV RETIRED', 'ASSET',
524                 'PROCEEDS', 'ASSET',
525                 'PROCEEDS CLR', 'ASSET',
526                 'REMOVALCOST', 'ASSET',
527                 'REMOVALCOST CLR', 'ASSET',
528                 'RESERVE', 'ASSET',
529                 'REVAL RESERVE', 'ASSET',
530                 'REVAL RSV RET', 'ASSET',
531                 'REVAL AMORT', 'EXPENSE',
532                 'REVAL EXPENSE', 'EXPENSE',
533                 'ASSET'),
534              lk.description,
535              decode (gljh.je_header_id, null,
536                                         decode (bcp.gl_posting_allowed_flag, 'YES',
537                                                                              'N', 'NT'),
538                                         'Y'), -- Bug 12879380
539              gljh.je_batch_id,
540              dd.je_header_id,
541              nvl(dd.deprn_reserve_je_line_num, 0),
542              adj.distribution_id
543       from   fa_mc_adjustments adj,
544              gl_sets_of_books glsob,
545              fa_mc_book_controls bc,
546              fa_book_controls bcp,
547              fa_lookups_tl lk,
548              gl_je_headers gljh,
549              fa_mc_deprn_detail dd,
550              gl_je_lines gljl,
551              fa_distribution_accounts da,
552              fa_asset_history ah,
553              fa_category_books cb
554       where  bc.book_type_code = l_book_type_code
555       and    bc.set_of_books_id = l_set_of_books_id
556       and    bcp.book_type_code = l_book_type_code
557       and    bc.enabled_flag = 'Y'
558       and    bc.set_of_books_id = glsob.set_of_books_id
559       and    bc.book_type_code = adj.book_type_code
560       and    bc.set_of_books_id = adj.set_of_books_id
561       and    adj.asset_id = l_asset_id
562       and    adj.transaction_header_id = l_transaction_header_id
563       and    adj.adjustment_type = 'EXPENSE'
564       and    adj.source_type_code in ('DEPRECIATION', 'CIP RETIREMENT',
565                                       'RETIREMENT')
566       and    adj.code_combination_id is not null
567       and    nvl(adj.track_member_flag, 'N') = 'N'
568       and    lk.lookup_type = 'JOURNAL ENTRIES'
569       and    lk.lookup_code = adj.source_type_code ||' RESERVE'
570       and    userenv('LANG') = lk.language
571       and    adj.asset_id = dd.asset_id (+)
572       and    adj.book_type_code = dd.book_type_code (+)
573       and    adj.set_of_books_id = dd.set_of_books_id (+)
574       and    adj.distribution_id = dd.distribution_id (+)
575       and    adj.period_counter_created = dd.period_counter (+)
576       and    dd.je_header_id = gljl.je_header_id (+)
577       and    dd.deprn_reserve_je_line_num = gljl.je_line_num (+)
578       and    dd.je_header_id = gljh.je_header_id (+)
579       and    adj.book_type_code = da.book_type_code (+)
580       and    adj.distribution_id = da.distribution_id (+)
581       and    adj.asset_id = ah.asset_id
582       and    l_date_effective >= ah.date_effective
583       and    l_date_effective < nvl(ah.date_ineffective, sysdate+1)
584       and    ah.category_id = cb.category_id
585       and    bc.book_type_code = cb.book_type_code
586    UNION ALL
587       select adj.transaction_header_id,
588              adj.adjustment_line_id,
589              decode (adj.debit_credit_flag,
590                      'DR', adj.adjustment_amount, null),
591              decode (adj.debit_credit_flag,
592                      'CR', adj.adjustment_amount, null),
593              nvl(nvl(gljl.code_combination_id, da.deprn_reserve_account_ccid),
594                  cb.reserve_account_ccid),
595              glsob.currency_code,
596              decode (adj.adjustment_type,
597                 'BONUS EXPENSE', 'EXPENSE',
598                 'BONUS RESERVE', 'ASSET',
599                 'CIP COST', 'ASSET',
600                 'COST', 'ASSET',
601                 'COST CLEARING', 'ASSET',
602                 'DEPRN ADJUST', 'EXPENSE',
603                 'EXPENSE', 'EXPENSE',
604                 'GRP COR RESERVE', 'ASSET',
605                 'GRP PRC RESERVE', 'ASSET',
606                 'INTERCO AP', 'LIABILITY',
607                 'INTERCO AR', 'ASSET',
608                 'NBV RETIRED', 'ASSET',
609                 'PROCEEDS', 'ASSET',
610                 'PROCEEDS CLR', 'ASSET',
611                 'REMOVALCOST', 'ASSET',
612                 'REMOVALCOST CLR', 'ASSET',
613                 'RESERVE', 'ASSET',
614                 'REVAL RESERVE', 'ASSET',
615                 'REVAL RSV RET', 'ASSET',
616                 'REVAL AMORT', 'EXPENSE',
617                 'REVAL EXPENSE', 'EXPENSE',
618                 'ASSET'),
619              lk.description,
620              decode (gljh.je_header_id, null,
621                                         decode (bcp.gl_posting_allowed_flag, 'YES',
622                                                                              'N', 'NT'),
623                                         'Y'), -- Bug 12879380
624              gljh.je_batch_id,
625              dd.je_header_id,
626              nvl(dd.deprn_reserve_je_line_num, 0),
627              adj.distribution_id
628       from   fa_mc_adjustments adj,
629              gl_sets_of_books glsob,
630              fa_mc_book_controls bc,
631              fa_book_controls bcp,
632              fa_lookups_tl lk,
633              gl_je_headers gljh,
634              fa_mc_deprn_detail dd,
635              gl_je_lines gljl,
636              fa_distribution_accounts da,
637              fa_asset_history ah,
638              fa_category_books cb
639       where  bc.book_type_code = l_book_type_code
640       and    bc.set_of_books_id = l_set_of_books_id
641       and    bcp.book_type_code = l_book_type_code
642       and    bc.enabled_flag = 'Y'
643       and    bc.set_of_books_id = glsob.set_of_books_id
644       and    bc.book_type_code = adj.book_type_code
645       and    bc.set_of_books_id = adj.set_of_books_id
646       and    adj.asset_id = l_asset_id
647       and    adj.transaction_header_id = l_transaction_header_id
648       and    adj.adjustment_type = 'BONUS EXPENSE'
649       and    adj.source_type_code = 'DEPRECIATION'
650       and    adj.code_combination_id is not null
651       and    nvl(adj.track_member_flag, 'N') = 'N'
652       and    lk.lookup_type = 'JOURNAL ENTRIES'
653       and    lk.lookup_code = 'BONUS DEPRECIATION RESERVE'
654       and    userenv('LANG') = lk.language
655       and    adj.asset_id = dd.asset_id (+)
656       and    adj.book_type_code = dd.book_type_code (+)
657       and    adj.set_of_books_id = dd.set_of_books_id (+)
658       and    adj.distribution_id = dd.distribution_id (+)
659       and    adj.period_counter_created = dd.period_counter (+)
660       and    dd.je_header_id = gljl.je_header_id (+)
661       and    dd.bonus_deprn_rsv_je_line_num = gljl.je_line_num (+)
662       and    dd.je_header_id = gljh.je_header_id (+)
663       and    adj.book_type_code = da.book_type_code (+)
664       and    adj.distribution_id = da.distribution_id (+)
665       and    adj.asset_id = ah.asset_id
666       and    l_date_effective >= ah.date_effective
667       and    l_date_effective < nvl(ah.date_ineffective, sysdate+1)
668       and    ah.category_id = cb.category_id
669       and    bc.book_type_code = cb.book_type_code;
670 
671 BEGIN
672 
673    x_success_count := 0;
674    x_failure_count := 0;
675 
676    l_batch_size := nvl(nvl(p_batch_size, fa_cache_pkg.fa_batch_size), 1000);
677 
678    open c_trans;
679    loop
680 
681          fetch c_trans bulk collect
682           into l_event_id_tbl,
683                l_src_asset_id_tbl,
684                l_dest_asset_id_tbl,
685                l_book_type_code_tbl,
686                l_primary_set_of_books_id_tbl,
687                l_org_id_tbl,
688                l_src_trans_type_code_tbl,
689                l_dest_trans_type_code_tbl,
690                l_transaction_date_entered_tbl,
691                l_src_thid_tbl,
692                l_dest_thid_tbl,
693                l_src_rowid_tbl,
694                l_dest_rowid_tbl,
695                l_tr_rowid_tbl,
696                l_entity_id_tbl,
697                l_event_class_code_tbl,
698                l_period_name_tbl,
699                l_period_counter_tbl,
700                l_cal_period_close_date_tbl,
701                l_trx_reference_id_tbl,
702                l_src_hdr_desc_tbl,
703                l_dest_hdr_desc_tbl,
704                l_je_category_name_tbl,
705                l_date_effective_tbl
706                limit l_batch_size;
707 
708       if (l_event_id_tbl.count = 0) then exit; end if;
709 
710       -- Select upg_batch_id
711       select xla_upg_batches_s.nextval
712       into   l_upg_batch_id
713       from   dual;
714 
715       -- Update table with event_id
716       FORALL l_count IN 1..l_event_id_tbl.count
717          update fa_trx_references trx
718          set    trx.event_id = l_event_id_tbl(l_count)
719          where  trx.rowid = l_tr_rowid_tbl(l_count);
720 
721       FORALL l_count IN 1..l_event_id_tbl.count
722          update fa_transaction_headers th
723          set    th.event_id = l_event_id_tbl(l_count)
724          where  th.rowid = l_src_rowid_tbl(l_count);
725 
726       FORALL l_count IN 1..l_event_id_tbl.count
727          update fa_transaction_headers th
728          set    th.event_id = l_event_id_tbl(l_count)
729          where  th.rowid = l_dest_rowid_tbl(l_count);
730 
731       l_rows_processed := l_event_id_tbl.count;
732 
733       -- Business Rules for xla_transaction_entities
734       -- * ledger_id is the same as set_of_books_id
735       -- * legal_entity_id is null
736       -- * entity_code is INTER_ASSET_TRANSACTIONS
737       -- * for TRANSACTIONS:
738       --       source_id_int_1 is trx_reference_id
739       --       transaction_number is trx_reference_id
740       -- * for DEPRECIATION:
741       --       source_id_int is asset_id
742       --       source_id_int_2 is period_counter
743       --       source_id_int_3 is deprn_run_id
744       --       transaction_number is set_of_books_id
745       -- * source_char_id_1 is book_type_code
746       -- * valuation_method is book_type_code
747 
748       FORALL i IN 1..l_event_id_tbl.count
749          INSERT INTO xla_transaction_entities_upg (
750             upg_batch_id,
751             application_id,
752             ledger_id,
753             legal_entity_id,
754             entity_code,
755             source_id_int_1,
756             source_id_int_2,
757             source_id_int_3,
758             source_id_int_4,
759             source_id_char_1,
760             source_id_char_2,
761             source_id_char_3,
762             source_id_char_4,
763             security_id_int_1,
764             security_id_int_2,
765             security_id_int_3,
766             security_id_char_1,
767             security_id_char_2,
768             security_id_char_3,
769             transaction_number,
770             valuation_method,
771             source_application_id,
772             creation_date,
773             created_by,
774             last_update_date,
775             last_updated_by,
776             last_update_login,
777             entity_id,
778             upg_source_application_id
779          ) values (
780             l_upg_batch_id,                        -- upg_batch_id
781             c_application_id,                      -- application_id
782             l_primary_set_of_books_id_tbl(i),      -- ledger_id
783             null,                                  -- legal_entity_id,
784             c_entity_code,                         -- entity_code
785             l_trx_reference_id_tbl(i),             -- source_id_int_1
786             null,                                  -- source_id_int_2
787             null,                                  -- source_id_int_3
788             null,                                  -- source_id_int_4
789             l_book_type_code_tbl(i),               -- source_id_char_1  -- Bug 8239360
790             null,                                  -- source_id_char_2
791             null,                                  -- source_id_char_3
792             null,                                  -- source_id_char_4
793             null,                                  -- security_id_int_1
794             null,                                  -- security_id_int_2
795             null,                                  -- security_id_int_3
796             null,                                  -- security_id_char_1
797             null,                                  -- security_id_char_2
798             null,                                  -- security_id_char_3
799             l_trx_reference_id_tbl(i),             -- transaction number
800             l_book_type_code_tbl(i),               -- valuation_method
801             c_application_id,                      -- source_application_id
802             sysdate,                               -- creation_date
803             c_fnd_user,                            -- created_by
804             sysdate,                               -- last_update_date
805             c_upgrade_bugno,                       -- last_update_by
806             c_upgrade_bugno,                       -- last_update_login
807             l_entity_id_tbl(i),                    -- entity_id
808             c_application_id                       -- upg_source_application_id
809          );
810 
811       -- Business Rules for xla_events
812       -- * event_type_code is similar to transaction_type_code
813       -- * event_number is 1, but is the serial event for chronological order
814       -- * event_status_code: N if event creates no journal entries
815       --                      P if event would ultimately yield journals
816       --                      I if event is not ready to be processed
817       --                      U never use this value for upgrade
818       -- * process_status_code: E if error and journals not yet created
819       --                        P if processed and journals already generated
820       --                        U if unprocessed and journals not generated
821       --                        D only used for Global Accounting Engine
822       --                        I do not use for upgrade
823       -- * on_hold_flag: N should always be this value for upgraded entries
824       -- * event_date is basically transaction_date_entered
825 
826       FORALL i IN 1..l_event_id_tbl.count
827          insert into xla_events (
828             upg_batch_id,
829             application_id,
830             event_type_code,
831             event_number,
832             event_status_code,
833             process_status_code,
834             on_hold_flag,
835             event_date,
836             creation_date,
837             created_by,
838             last_update_date,
839             last_updated_by,
840             last_update_login,
841             program_update_date,
842             program_id,
843             program_application_id,
844             request_id,
845             entity_id,
846             event_id,
847             upg_source_application_id,
848             transaction_date
849          ) values (
850             l_upg_batch_id,                          -- upg_batch_id
851             c_application_id,                        -- application_id
852             l_event_class_code_tbl(i),               -- event_type_code
853             '1',                                     -- event_number
854             'P',                                     -- event_status_code
855             'P',                                     -- process_status_code
856             'N',                                     -- on_hold_flag
857             l_transaction_date_entered_tbl(i),       -- event_date
858             sysdate,                                 -- creation_date
859             c_fnd_user,                              -- created_by
860             sysdate,                                 -- last_update_date
861             c_upgrade_bugno,                         -- last_update_by
862             c_upgrade_bugno,                         -- last_update_login
863             null,                                    -- program_update_date
864             null,                                    -- program_id
865             null,                                    -- program_application_id
866             null,                                    -- program_update_date
867             l_entity_id_tbl(i),                      -- entity_id
868             l_event_id_tbl(i),                       -- event_id
869             c_application_id,                        -- upg_source_appl_id
870             l_transaction_date_entered_tbl(i)        -- transaction_date
871          );
872 
873       FOR i IN 1..l_event_id_tbl.count LOOP
874 
875             open c_adj (l_book_type_code_tbl(i),
876                         l_src_asset_id_tbl(i),
877                         l_src_thid_tbl(i),
878                         l_date_effective_tbl(i));
879             fetch c_adj bulk collect
880              into l_adj_thid_tbl,
881                   l_adj_line_id_tbl,
882                   l_debit_amount_tbl,
883                   l_credit_amount_tbl,
884                   l_ccid_tbl,
885                   l_currency_code_tbl,
886                   l_acct_class_code_tbl,
887                   l_line_desc_tbl,
888                   l_gl_transfer_status_code_tbl,
889                   l_je_batch_id_tbl,
890                   l_je_header_id_tbl,
891                   l_je_line_num_tbl,
892                   l_distribution_id_tbl;
893             close c_adj;
894 
895             FOR j IN 1..l_adj_line_id_tbl.count LOOP
896                l_ae_line_num_tbl(j) := j;
897 
898                select xla_gl_sl_link_id_s.nextval
899                into   l_xla_gl_sl_link_id_tbl(j)
900                from   dual;
901 
902             END LOOP;
903 
904             select xla_ae_headers_s.nextval
905             into   l_ae_header_id
906             from   dual;
907 
908             l_line_num_tbl(1) :=  l_adj_line_id_tbl.count; -- Bug 6827893
909             l_ae_header_id_tbl(1) :=  l_ae_header_id; -- Bug 6827893
910 
911             -- Business Rules for xla_ae_headers
912             -- * amb_context_code is DEFAULT
913             -- * reference_date must be null
914             -- * balance_type_code:
915             --     A: Actual
916             --     B: Budget
917             --     E: Encumbrance
918             -- * gl_transfer_status_code:
919             --     Y: already transferred to GL
920             --     N: not transferred to GL
921             -- * gl_transfer_date is date entry transferred to GL
922             -- * accounting_entry_status_code must be F
923             -- * accounting_entry_type_code must be STANDARD
924             -- * product_rule_* not relevant for upgrade
925 
926                insert into xla_ae_headers (
927                   upg_batch_id,
928                   application_id,
929                   amb_context_code,
930                   entity_id,
931                   event_id,
932                   event_type_code,
933                   ae_header_id,
934                   ledger_id,
935                   accounting_date,
936                   period_name,
937                   reference_date,
938                   balance_type_code,
939                   je_category_name,
940                   gl_transfer_status_code,
941                   gl_transfer_date,
942                   accounting_entry_status_code,
943                   accounting_entry_type_code,
944                   description,
945                   budget_version_id,
946                   funds_status_code,
947                   encumbrance_type_id,
948                   completed_date,
949                   doc_sequence_id,
950                   doc_sequence_value,
951                   doc_category_code,
952                   packet_id,
953                   group_id,
954                   creation_date,
955                   created_by,
956                   last_update_date,
957                   last_updated_by,
958                   last_update_login,
959                   program_id,
960                   program_application_id,
961                   program_update_date,
962                   request_id,
963                   close_acct_seq_assign_id,
964                   close_acct_seq_version_id,
965                   close_acct_seq_value,
966                   completion_acct_seq_assign_id,
967                   completion_acct_seq_version_id,
968                   completion_acct_seq_value,
969                   accounting_batch_id,
970                   product_rule_type_code,
971                   product_rule_code,
972                   product_rule_version,
973                   upg_source_application_id,
974                   upg_valid_flag
975                ) values (
976                   l_upg_batch_id,               -- upg_batch_id
977                   c_application_id,             -- application_id
978                   c_amb_context_code,           -- amb_context_code
979                   l_entity_id_tbl(i),           -- entity_id
980                   l_event_id_tbl(i),            -- event_id,
981                   l_event_class_code_tbl(i),    -- event_type_code
982                   l_ae_header_id,               -- ae_header_id,
983                   l_primary_set_of_books_id_tbl(i),
984                                                 -- ledger_id/sob_id
985                   l_cal_period_close_date_tbl(i),
986                                                 -- accounting_date,
987                   l_period_name_tbl(i),         -- period_name,
988                   null,                         -- reference_date
989                   'A',                          -- balance_type_code,
990                   l_je_category_name_tbl(i),    -- je_category_name
991                   l_gl_transfer_status_code_tbl(1),  -- Bug 6811554
992                                                 -- gl_transfer_status_code
993                   null,                         -- gl_transfer_date
994                   'F',                          -- accounting_entry_status_code
995                   'STANDARD',                   -- accounting_entry_type_code
996                   l_src_hdr_desc_tbl(i),        -- description
997                   null,                         -- budget_version_id
998                   null,                         -- funds_status_code
999                   null,                         -- encumbrance_type_id
1000                   null,                         -- completed_date
1001                   null,                         -- doc_sequence_id
1002                   null,                         -- doc_sequence_value
1003                   null,                         -- doc_category_code
1004                   null,                         -- packet_id,
1005                   null,                         -- group_id
1006                   sysdate,                      -- creation_date
1007                   c_fnd_user,                   -- created_by
1008                   sysdate,                      -- last_update_date
1009                   c_fnd_user,                   -- last_updated_by
1010                   c_upgrade_bugno,              -- last_update_login
1011                   null,                         -- program_id
1012                   c_application_id,             -- program_application_id
1013                   sysdate,                      -- program_update_date
1014                   null,                         -- request_id
1015                   null,                         -- close_acct_seq_assign_id
1016                   null,                         -- close_acct_seq_version_id
1017                   null,                         -- close_acct_seq_value
1018                   null,                         -- compl_acct_seq_assign_id
1019                   null,                         -- compl_acct_seq_version_id
1020                   null,                         -- compl_acct_seq_value
1021                   null,                         -- accounting_batch_id
1022                   null,                         -- product_rule_type_code
1023                   null,                         -- product_rule_code
1024                   null,                         -- product_rule_version
1025                   c_application_id,             -- upg_souce_application_id
1026                   null                          -- upg_valid_flag
1027                );
1028 
1029             -- Business Rules for xla_ae_lines
1030             -- * gl_transfer_mode_code:
1031             --       D: Detailed mode when transferred to GL
1032             --       S: Summary mode when transferred to GL
1033             -- * gl_sl_link_table must be XLAJEL
1034             -- * currency_conversion_* needs to be populated only if
1035             --   different from ledger currency
1036 
1037             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
1038             insert into xla_ae_lines (
1039                   upg_batch_id,
1040                   ae_header_id,
1041                   ae_line_num,
1042                   displayed_line_number,
1043                   application_id,
1044                   code_combination_id,
1045                   gl_transfer_mode_code,
1046                   accounted_dr,
1047                   accounted_cr,
1048                   currency_code,
1049                   currency_conversion_date,
1050                   currency_conversion_rate,
1051                   currency_conversion_type,
1052                   entered_dr,
1053                   entered_cr,
1054                   description,
1055                   accounting_class_code,
1056                   gl_sl_link_id,
1057                   gl_sl_link_table,
1058                   party_type_code,
1059                   party_id,
1060                   party_site_id,
1061                   statistical_amount,
1062                   ussgl_transaction_code,
1063                   jgzz_recon_ref,
1064                   control_balance_flag,
1065                   analytical_balance_flag,
1066                   upg_tax_reference_id1,
1067                   upg_tax_reference_id2,
1068                   upg_tax_reference_id3,
1069                   creation_date,
1070                   created_by,
1071                   last_update_date,
1072                   last_updated_by,
1073                   last_update_login,
1074                   program_update_date,
1075                   program_id,
1076                   program_application_id,
1077                   request_id,
1078                   gain_or_loss_flag,
1079                   accounting_date,
1080                   ledger_id
1081             ) values (
1082                   l_upg_batch_id,                 -- upg_batch_id
1083                   l_ae_header_id,                 -- ae_header_id
1084                   l_ae_line_num_tbl(j),           -- ae_line_num
1085                   l_ae_line_num_tbl(j),           -- displayed_line_num
1086                   c_application_id,               -- application_id
1087                   l_ccid_tbl(j),                  -- code_combination_id
1088                   'S',                            -- gl_transfer_mode_code
1089                   l_debit_amount_tbl(j),          -- accounted_dr
1090                   l_credit_amount_tbl(j),         -- accounted_cr
1091                   l_currency_code_tbl(j),         -- currency_code
1092                   null,                           -- currency_conversion_date
1093                   null,                           -- currency_conversion_rate
1094                   null,                           -- currency_conversion_type
1095                   l_debit_amount_tbl(j),          -- entered_dr
1096                   l_credit_amount_tbl(j),         -- entered_cr
1097                   l_line_desc_tbl(j) || ' - ' ||
1098                      to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
1099                                                   -- description
1100                   l_acct_class_code_tbl(j),       -- accounting_class_code
1101                   l_xla_gl_sl_link_id_tbl(j),     -- gl_sl_link_id
1102                   'XLAJEL',                       -- gl_sl_link_table
1103                   null,                           -- party_type_code
1104                   null,                           -- party_id
1105                   null,                           -- party_site_id
1106                   null,                           -- statistical_amount
1107                   null,                           -- ussgl_transaction_code
1108                   null,                           -- glzz_recon_ref
1109                   null,                           -- control_balance_flag
1110                   null,                           -- analytical_balance_flag
1111                   null,                           -- upg_tax_reference_id1
1112                   null,                           -- upg_tax_reference_id2
1113                   null,                           -- upg_tax_reference_id3
1114                   sysdate,                        -- creation_date
1115                   c_fnd_user,                     -- created_by
1116                   sysdate,                        -- last_update_date
1117                   c_fnd_user,                     -- last_updated_by
1118                   c_upgrade_bugno,                -- last_update_login
1119                   null,                           -- program_update_date
1120                   null,                           -- program_id
1121                   c_application_id,               -- program_application_id
1122                   null,                           -- request_id
1123                   'N',                            -- gain_or_loss_flag
1124                   l_cal_period_close_date_tbl(i), -- accounting_date,
1125                   l_primary_set_of_books_id_tbl(i)
1126                                                   -- ledger_id/sob_id
1127             );
1128 
1129             -- Business Rules for xla_distribution_links
1130             -- * accounting_line_code is similar to adjustment_type
1131             -- * accounting_line_type_code is S
1132             -- * merge_duplicate_code is N
1133             -- * source_distribution_type is TRX
1134             -- * source_distribution_id_num_1 is trx_reference_id
1135             -- * source_distribution_id_num_2 is event_id
1136 
1137             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
1138             insert into xla_distribution_links (
1139                   upg_batch_id,
1140                   application_id,
1141                   event_id,
1142                   ae_header_id,
1143                   ae_line_num,
1144                   accounting_line_code,
1145                   accounting_line_type_code,
1146                   source_distribution_type,
1147                   source_distribution_id_char_1,
1148                   source_distribution_id_char_2,
1149                   source_distribution_id_char_3,
1150                   source_distribution_id_char_4,
1151                   source_distribution_id_char_5,
1152                   source_distribution_id_num_1,
1153                   source_distribution_id_num_2,
1154                   source_distribution_id_num_3,
1155                   source_distribution_id_num_4,
1156                   source_distribution_id_num_5,
1157                   merge_duplicate_code,
1158                   statistical_amount,
1159                   unrounded_entered_dr,
1160                   unrounded_entered_cr,
1161                   unrounded_accounted_dr,
1162                   unrounded_accounted_cr,
1163                   ref_ae_header_id,
1164                   ref_temp_line_num,
1165                   ref_event_id,
1166                   temp_line_num,
1167                   tax_line_ref_id,
1168                   tax_summary_line_ref_id,
1169                   tax_rec_nrec_dist_ref_id,
1170                   line_definition_owner_code,
1171                   line_definition_code,
1172                   event_class_code,
1173                   event_type_code
1174             ) values (
1175                   l_upg_batch_id,              -- upg_batch_id
1176                   c_application_id,            -- application_id
1177                   l_event_id_tbl(i),           -- event_id
1178                   l_ae_header_id,              -- ae_header_id
1179                   l_ae_line_num_tbl(j),        -- ae_line_num
1180                   null,                        -- accounting_line_code
1181                   'S',                         -- accounting_line_type_code
1182                   'TRX',                       -- source_distribution_type
1183                   null,                        -- source_distribution_id_char_1
1184                   null,                        -- source_distribution_id_char_2
1185                   null,                        -- source_distribution_id_char_3
1186                   null,                        -- source_distribution_id_char_4
1187                   null,                        -- source_distribution_id_char_5
1188                   l_adj_thid_tbl(j),           -- BUG# 6827893
1189                                                -- l_trx_reference_id_tbl(i),
1190                                                -- source_distribution_id_num_1
1191                   l_adj_line_id_tbl(j),        -- source_distribution_id_num_2
1192                   null,                        -- source_distribution_id_num_3
1193                   null,                        -- source_distribution_id_num_4
1194                   null,                        -- source_distribution_id_num_5
1195                   'N',                         -- merge_duplicate_code
1196                   null,                        -- statistical_amount
1197                   l_debit_amount_tbl(j),       -- unrounded_entered_dr
1198                   l_credit_amount_tbl(j),      -- unrounded_entered_cr
1199                   l_debit_amount_tbl(j),       -- unrounded_accounted_dr
1200                   l_credit_amount_tbl(j),      -- unrounded_accounted_cr
1201                   l_ae_header_id,              -- ref_ae_header_id
1202                   null,                        -- ref_temp_line_num
1203                   null,                        -- ref_event_id
1204                   l_ae_line_num_tbl(j),        -- temp_line_num
1205                   null,                        -- tax_line_ref_id
1206                   null,                        -- tax_summary_line_ref_id
1207                   null,                        -- tax_rec_nrec_dist_ref_id
1208                   null,                        -- line_definition_owner_code
1209                   null,                        -- line_definition_code
1210                   l_event_class_code_tbl(i),   -- event_class_code
1211                   l_event_class_code_tbl(i)    -- event_type_code
1212             );
1213 
1214             for j IN 1..l_xla_gl_sl_link_id_tbl.count loop
1215                if (l_je_batch_id_tbl(j) is not null) then
1216                   insert into gl_import_references (
1217                      je_batch_id,
1218                      je_header_id,
1219                      je_line_num,
1220                      last_update_date,
1221                      last_updated_by,
1222                      creation_date,
1223                      created_by,
1224                      last_update_login,
1225                      reference_1,
1226                      reference_2,
1227                      reference_3,
1228                      reference_4,
1229                      reference_5,
1230                      reference_6,
1231                      reference_7,
1232                      reference_8,
1233                      reference_9,
1234                      reference_10,
1235                      subledger_doc_sequence_id,
1236                      subledger_doc_sequence_value,
1237                      gl_sl_link_id,
1238                      gl_sl_link_table
1239                   ) values (
1240                      l_je_batch_id_tbl(j),        -- je_batch_id
1241                      l_je_header_id_tbl(j),       -- je_header_id
1242                      l_je_line_num_tbl(j),        -- je_line_num
1243                      sysdate,                     -- last_update_date
1244                      c_fnd_user,                  -- last_updated_by
1245                      sysdate,                     -- creation_date
1246                      c_fnd_user,                  -- created_by
1247                      c_upgrade_bugno,             -- last_update_login
1248                      to_char(l_src_thid_tbl(i)),
1249                                                   -- reference_1
1250                      to_char(l_src_asset_id_tbl(i)),
1251                                                   -- reference_2
1252                      to_char(l_distribution_id_tbl(j)),
1253                                                   -- reference_3
1254                      to_char(l_adj_line_id_tbl(j)),
1255                                                   -- reference_4
1256                      l_book_type_code_tbl(i),     -- reference_5
1257                      to_char(l_period_counter_tbl(i)),
1258                                                   -- reference_6
1259                      null,                        -- reference_7
1260                      null,                        -- reference_8
1261                      null,                        -- reference_9
1262                      null,                        -- reference_10
1263                      null,                        -- subledger_doc_seq_id
1264                      null,                        -- subledger_doc_seq_value
1265                      l_xla_gl_sl_link_id_tbl(j),  -- gl_sl_link_id
1266                      'XLAJEL'                     -- gl_sl_link_table
1267                   );
1268                end if;
1269             end loop;
1270 
1271             l_adj_line_id_tbl.delete;
1272             l_xla_gl_sl_link_id_tbl.delete;
1273             l_ae_line_num_tbl.delete;
1274             l_debit_amount_tbl.delete;
1275             l_credit_amount_tbl.delete;
1276             l_ccid_tbl.delete;
1277             l_acct_class_code_tbl.delete;
1278             l_currency_code_tbl.delete;
1279             l_line_def_owner_code_tbl.delete;
1280             l_line_def_code_tbl.delete;
1281             l_line_desc_tbl.delete;
1282             l_gl_transfer_status_code_tbl.delete;
1283             l_je_batch_id_tbl.delete;
1284             l_je_header_id_tbl.delete;
1285             l_je_line_num_tbl.delete;
1286             l_distribution_id_tbl.delete;
1287 
1288             open c_mc_books (l_book_type_code_tbl(i));
1289             fetch c_mc_books bulk collect
1290              into l_rep_set_of_books_id_tbl;
1291             close c_mc_books;
1292 
1293            for k IN 1..l_rep_set_of_books_id_tbl.count loop
1294 
1295             open c_mc_adj (l_book_type_code_tbl(i),
1296                         l_src_asset_id_tbl(i),
1297                         l_rep_set_of_books_id_tbl(k),
1298                         l_src_thid_tbl(i),
1299                         l_date_effective_tbl(i));
1300             fetch c_mc_adj bulk collect
1301              into l_adj_thid_tbl,
1302                   l_adj_line_id_tbl,
1303                   l_debit_amount_tbl,
1304                   l_credit_amount_tbl,
1305                   l_ccid_tbl,
1306                   l_currency_code_tbl,
1307                   l_acct_class_code_tbl,
1308                   l_line_desc_tbl,
1309                   l_gl_transfer_status_code_tbl,
1310                   l_je_batch_id_tbl,
1311                   l_je_header_id_tbl,
1312                   l_je_line_num_tbl,
1313                   l_distribution_id_tbl;
1314            close c_mc_adj;
1315 
1316             FOR j IN 1..l_adj_line_id_tbl.count LOOP
1317                l_ae_line_num_tbl(j) := j;
1318 
1319                select xla_gl_sl_link_id_s.nextval
1320                into   l_xla_gl_sl_link_id_tbl(j)
1321                from   dual;
1322             END LOOP;
1323 
1324             select xla_ae_headers_s.nextval
1325             into   l_ae_header_id
1326             from   dual;
1327 
1328             l_line_num_tbl(k+1) :=  l_adj_line_id_tbl.count; -- Bug 6827893
1329             l_ae_header_id_tbl(k+1) :=  l_ae_header_id; -- Bug 6827893
1330 
1331             -- Business Rules for xla_ae_headers
1332             -- * amb_context_code is DEFAULT
1333             -- * reference_date must be null
1334             -- * balance_type_code:
1335             --     A: Actual
1336             --     B: Budget
1337             --     E: Encumbrance
1338             -- * gl_transfer_status_code:
1339             --     Y: already transferred to GL
1340             --     N: not transferred to GL
1341             -- * gl_transfer_date is date entry transferred to GL
1342             -- * accounting_entry_status_code must be F
1343             -- * accounting_entry_type_code must be STANDARD
1344             -- * product_rule_* not relevant for upgrade
1345 
1346                insert into xla_ae_headers (
1347                   upg_batch_id,
1348                   application_id,
1349                   amb_context_code,
1350                   entity_id,
1351                   event_id,
1352                   event_type_code,
1353                   ae_header_id,
1354                   ledger_id,
1355                   accounting_date,
1356                   period_name,
1357                   reference_date,
1358                   balance_type_code,
1359                   je_category_name,
1360                   gl_transfer_status_code,
1361                   gl_transfer_date,
1362                   accounting_entry_status_code,
1363                   accounting_entry_type_code,
1364                   description,
1365                   budget_version_id,
1366                   funds_status_code,
1367                   encumbrance_type_id,
1368                   completed_date,
1369                   doc_sequence_id,
1370                   doc_sequence_value,
1371                   doc_category_code,
1372                   packet_id,
1373                   group_id,
1374                   creation_date,
1375                   created_by,
1376                   last_update_date,
1377                   last_updated_by,
1378                   last_update_login,
1379                   program_id,
1380                   program_application_id,
1381                   program_update_date,
1382                   request_id,
1383                   close_acct_seq_assign_id,
1384                   close_acct_seq_version_id,
1385                   close_acct_seq_value,
1386                   completion_acct_seq_assign_id,
1387                   completion_acct_seq_version_id,
1388                   completion_acct_seq_value,
1389                   accounting_batch_id,
1390                   product_rule_type_code,
1391                   product_rule_code,
1392                   product_rule_version,
1393                   upg_source_application_id,
1394                   upg_valid_flag
1395                ) values (
1396                   l_upg_batch_id,               -- upg_batch_id
1397                   c_application_id,             -- application_id
1398                   c_amb_context_code,           -- amb_context_code
1399                   l_entity_id_tbl(i),           -- entity_id
1400                   l_event_id_tbl(i),            -- event_id,
1401                   l_event_class_code_tbl(i),    -- event_type_code
1402                   l_ae_header_id,               -- ae_header_id,
1403                   l_rep_set_of_books_id_tbl(k), -- ledger_id/sob_id
1404                   l_cal_period_close_date_tbl(i),
1405                                                 -- accounting_date,
1406                   l_period_name_tbl(i),         -- period_name,
1407                   null,                         -- reference_date
1408                   'A',                          -- balance_type_code,
1409                   l_je_category_name_tbl(i),    -- je_category_name
1410                   l_gl_transfer_status_code_tbl(1),  -- Bug 6811554
1411                                                 -- gl_transfer_status_code
1412                   null,                         -- gl_transfer_date
1413                   'F',                          -- accounting_entry_status_code
1414                   'STANDARD',                   -- accounting_entry_type_code
1415                   l_src_hdr_desc_tbl(i),        -- description
1416                   null,                         -- budget_version_id
1417                   null,                         -- funds_status_code
1418                   null,                         -- encumbrance_type_id
1419                   null,                         -- completed_date
1420                   null,                         -- doc_sequence_id
1421                   null,                         -- doc_sequence_value
1422                   null,                         -- doc_category_code
1423                   null,                         -- packet_id,
1424                   null,                         -- group_id
1425                   sysdate,                      -- creation_date
1426                   c_fnd_user,                   -- created_by
1427                   sysdate,                      -- last_update_date
1428                   c_fnd_user,                   -- last_updated_by
1429                   c_upgrade_bugno,              -- last_update_login
1430                   null,                         -- program_id
1431                   c_application_id,             -- program_application_id
1432                   sysdate,                      -- program_update_date
1433                   null,                         -- request_id
1434                   null,                         -- close_acct_seq_assign_id
1435                   null,                         -- close_acct_seq_version_id
1436                   null,                         -- close_acct_seq_value
1437                   null,                         -- compl_acct_seq_assign_id
1438                   null,                         -- compl_acct_seq_version_id
1439                   null,                         -- compl_acct_seq_value
1440                   null,                         -- accounting_batch_id
1441                   null,                         -- product_rule_type_code
1442                   null,                         -- product_rule_code
1443                   null,                         -- product_rule_version
1444                   c_application_id,             -- upg_souce_application_id
1445                   null                          -- upg_valid_flag
1446                );
1447 
1448             -- Business Rules for xla_ae_lines
1449             -- * gl_transfer_mode_code:
1450             --       D: Detailed mode when transferred to GL
1451             --       S: Summary mode when transferred to GL
1452             -- * gl_sl_link_table must be XLAJEL
1453             -- * currency_conversion_* needs to be populated only if
1454             --   different from ledger currency
1455 
1456             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
1457             insert into xla_ae_lines (
1458                   upg_batch_id,
1459                   ae_header_id,
1460                   ae_line_num,
1461                   displayed_line_number,
1462                   application_id,
1463                   code_combination_id,
1464                   gl_transfer_mode_code,
1465                   accounted_dr,
1466                   accounted_cr,
1467                   currency_code,
1468                   currency_conversion_date,
1469                   currency_conversion_rate,
1470                   currency_conversion_type,
1471                   entered_dr,
1472                   entered_cr,
1473                   description,
1474                   accounting_class_code,
1475                   gl_sl_link_id,
1476                   gl_sl_link_table,
1477                   party_type_code,
1478                   party_id,
1479                   party_site_id,
1480                   statistical_amount,
1481                   ussgl_transaction_code,
1482                   jgzz_recon_ref,
1483                   control_balance_flag,
1484                   analytical_balance_flag,
1485                   upg_tax_reference_id1,
1486                   upg_tax_reference_id2,
1487                   upg_tax_reference_id3,
1488                   creation_date,
1489                   created_by,
1490                   last_update_date,
1491                   last_updated_by,
1492                   last_update_login,
1493                   program_update_date,
1494                   program_id,
1495                   program_application_id,
1496                   request_id,
1497                   gain_or_loss_flag,
1498                   accounting_date,
1499                   ledger_id
1500             ) values (
1501                   l_upg_batch_id,                 -- upg_batch_id
1502                   l_ae_header_id,                 -- ae_header_id
1503                   l_ae_line_num_tbl(j),           -- ae_line_num
1504                   l_ae_line_num_tbl(j),           -- displayed_line_num
1505                   c_application_id,               -- application_id
1506                   l_ccid_tbl(j),                  -- code_combination_id
1507                   'S',                            -- gl_transfer_mode_code
1508                   l_debit_amount_tbl(j),          -- accounted_dr
1509                   l_credit_amount_tbl(j),         -- accounted_cr
1510                   l_currency_code_tbl(j),         -- currency_code
1511                   null,                           -- currency_conversion_date
1512                   null,                           -- currency_conversion_rate
1513                   null,                           -- currency_conversion_type
1514                   l_debit_amount_tbl(j),          -- entered_dr
1515                   l_credit_amount_tbl(j),         -- entered_cr
1516                   l_line_desc_tbl(j) || ' - ' ||
1517                      to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
1518                                                   -- description
1519                   l_acct_class_code_tbl(j),       -- accounting_class_code
1520                   l_xla_gl_sl_link_id_tbl(j),     -- gl_sl_link_id
1521                   'XLAJEL',                       -- gl_sl_link_table
1522                   null,                           -- party_type_code
1523                   null,                           -- party_id
1524                   null,                           -- party_site_id
1525                   null,                           -- statistical_amount
1526                   null,                           -- ussgl_transaction_code
1527                   null,                           -- glzz_recon_ref
1528                   null,                           -- control_balance_flag
1529                   null,                           -- analytical_balance_flag
1530                   null,                           -- upg_tax_reference_id1
1531                   null,                           -- upg_tax_reference_id2
1532                   null,                           -- upg_tax_reference_id3
1533                   sysdate,                        -- creation_date
1534                   c_fnd_user,                     -- created_by
1535                   sysdate,                        -- last_update_date
1536                   c_fnd_user,                     -- last_updated_by
1537                   c_upgrade_bugno,                -- last_update_login
1538                   null,                           -- program_update_date
1539                   null,                           -- program_id
1540                   c_application_id,               -- program_application_id
1541                   null,                           -- request_id
1542                   'N',                            -- gain_or_loss_flag
1543                   l_cal_period_close_date_tbl(i), -- accounting_date,
1544                   l_rep_set_of_books_id_tbl(k)    -- ledger_id/sob_id
1545             );
1546 
1547             -- Business Rules for xla_distribution_links
1548             -- * accounting_line_code is similar to adjustment_type
1549             -- * accounting_line_type_code is S
1550             -- * merge_duplicate_code is N
1551             -- * source_distribution_type is TRX
1552             -- * source_distribution_id_num_1 is trx_reference_id
1553             -- * source_distribution_id_num_2 is event_id
1554 
1555             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
1556             insert into xla_distribution_links (
1557                   upg_batch_id,
1558                   application_id,
1559                   event_id,
1560                   ae_header_id,
1561                   ae_line_num,
1562                   accounting_line_code,
1563                   accounting_line_type_code,
1564                   source_distribution_type,
1565                   source_distribution_id_char_1,
1566                   source_distribution_id_char_2,
1567                   source_distribution_id_char_3,
1568                   source_distribution_id_char_4,
1569                   source_distribution_id_char_5,
1570                   source_distribution_id_num_1,
1571                   source_distribution_id_num_2,
1572                   source_distribution_id_num_3,
1573                   source_distribution_id_num_4,
1574                   source_distribution_id_num_5,
1575                   merge_duplicate_code,
1576                   statistical_amount,
1577                   unrounded_entered_dr,
1578                   unrounded_entered_cr,
1579                   unrounded_accounted_dr,
1580                   unrounded_accounted_cr,
1581                   ref_ae_header_id,
1582                   ref_temp_line_num,
1583                   ref_event_id,
1584                   temp_line_num,
1585                   tax_line_ref_id,
1586                   tax_summary_line_ref_id,
1587                   tax_rec_nrec_dist_ref_id,
1588                   line_definition_owner_code,
1589                   line_definition_code,
1590                   event_class_code,
1591                   event_type_code
1592             ) values (
1593                   l_upg_batch_id,              -- upg_batch_id
1594                   c_application_id,            -- application_id
1595                   l_event_id_tbl(i),           -- event_id
1596                   l_ae_header_id,              -- ae_header_id
1597                   l_ae_line_num_tbl(j),        -- ae_line_num
1598                   null,                        -- accounting_line_code
1599                   'S',                         -- accounting_line_type_code
1600                   'TRX',                       -- source_distribution_type
1601                   null,                        -- source_distribution_id_char_1
1602                   null,                        -- source_distribution_id_char_2
1603                   null,                        -- source_distribution_id_char_3
1604                   null,                        -- source_distribution_id_char_4
1605 
1606                   null,                        -- source_distribution_id_char_5
1607                   l_adj_thid_tbl(j),           -- Bug 6827893
1608                                                -- source_distribution_id_num_1
1609                   l_adj_line_id_tbl(j),        -- source_distribution_id_num_2
1610                   null,                        -- source_distribution_id_num_3
1611                   null,                        -- source_distribution_id_num_4
1612                   null,                        -- source_distribution_id_num_5
1613                   'N',                         -- merge_duplicate_code
1614                   null,                        -- statistical_amount
1615                   l_debit_amount_tbl(j),       -- unrounded_entered_dr
1616                   l_credit_amount_tbl(j),      -- unrounded_entered_cr
1617                   l_debit_amount_tbl(j),       -- unrounded_accounted_dr
1618                   l_credit_amount_tbl(j),      -- unrounded_accounted_cr
1619                   l_ae_header_id,              -- ref_ae_header_id
1620                   null,                        -- ref_temp_line_num
1621                   null,                        -- ref_event_id
1622                   l_ae_line_num_tbl(j),        -- temp_line_num
1623                   null,                        -- tax_line_ref_id
1624                   null,                        -- tax_summary_line_ref_id
1625                   null,                        -- tax_rec_nrec_dist_ref_id
1626                   null,                        -- line_definition_owner_code
1627                   null,                        -- line_definition_code
1628                   l_event_class_code_tbl(i),   -- event_class_code
1629                   l_event_class_code_tbl(i)    -- event_type_code
1630             );
1631 
1632             for j IN 1..l_xla_gl_sl_link_id_tbl.count loop
1633                if (l_je_batch_id_tbl(j) is not null) then
1634                   insert into gl_import_references (
1635                      je_batch_id,
1636                      je_header_id,
1637                      je_line_num,
1638                      last_update_date,
1639                      last_updated_by,
1640                      creation_date,
1641                      created_by,
1642                      last_update_login,
1643                      reference_1,
1644                      reference_2,
1645                      reference_3,
1646                      reference_4,
1647                      reference_5,
1648                      reference_6,
1649                      reference_7,
1650                      reference_8,
1651                      reference_9,
1652                      reference_10,
1653                      subledger_doc_sequence_id,
1654                      subledger_doc_sequence_value,
1655                      gl_sl_link_id,
1656                      gl_sl_link_table
1657                   ) values (
1658                      l_je_batch_id_tbl(j),        -- je_batch_id
1659                      l_je_header_id_tbl(j),       -- je_header_id
1660                      l_je_line_num_tbl(j),        -- je_line_num
1661                      sysdate,                     -- last_update_date
1662                      c_upgrade_bugno,             -- last_updated_by
1663                      sysdate,                     -- creation_date
1664                      c_fnd_user,                  -- created_by
1665                      c_upgrade_bugno,             -- last_update_login
1666                      to_char(l_src_thid_tbl(i)),
1667                                                   -- reference_1
1668                      to_char(l_src_asset_id_tbl(i)),
1669                                                   -- reference_2
1670                      to_char(l_distribution_id_tbl(j)),
1671                                                   -- reference_3
1672                      to_char(l_adj_line_id_tbl(j)),
1673                                                   -- reference_4
1674                      l_book_type_code_tbl(i),     -- reference_5
1675                      to_char(l_period_counter_tbl(i)),
1676                                                   -- reference_6
1677                      null,                        -- reference_7
1678                      null,                        -- reference_8
1679                      null,                        -- reference_9
1680                      null,                        -- reference_10
1681                      null,                        -- subledger_doc_seq_id
1682                      null,                        -- subledger_doc_seq_value
1683                      l_xla_gl_sl_link_id_tbl(j),  -- gl_sl_link_id
1684                      'XLAJEL'                     -- gl_sl_link_table
1685                   );
1686                end if;
1687             end loop;
1688 
1689             l_adj_line_id_tbl.delete;
1690             l_xla_gl_sl_link_id_tbl.delete;
1691             l_ae_line_num_tbl.delete;
1692             l_debit_amount_tbl.delete;
1693             l_credit_amount_tbl.delete;
1694             l_ccid_tbl.delete;
1695             l_acct_class_code_tbl.delete;
1696             l_currency_code_tbl.delete;
1697             l_line_def_owner_code_tbl.delete;
1698             l_line_def_code_tbl.delete;
1699             l_line_desc_tbl.delete;
1700             l_gl_transfer_status_code_tbl.delete;
1701             l_je_batch_id_tbl.delete;
1702             l_je_header_id_tbl.delete;
1703             l_je_line_num_tbl.delete;
1704             l_distribution_id_tbl.delete;
1705 
1706             end loop;
1707 
1708             l_rep_set_of_books_id_tbl.delete;
1709 
1710             open c_adj (l_book_type_code_tbl(i),
1711                         l_dest_asset_id_tbl(i),
1712                         l_dest_thid_tbl(i),
1713                         l_date_effective_tbl(i));
1714             fetch c_adj bulk collect
1715              into l_adj_thid_tbl,
1716                   l_adj_line_id_tbl,
1717                   l_debit_amount_tbl,
1718                   l_credit_amount_tbl,
1719                   l_ccid_tbl,
1720                   l_currency_code_tbl,
1721                   l_acct_class_code_tbl,
1722                   l_line_desc_tbl,
1723                   l_gl_transfer_status_code_tbl,
1724                   l_je_batch_id_tbl,
1725                   l_je_header_id_tbl,
1726                   l_je_line_num_tbl,
1727                   l_distribution_id_tbl;
1728             close c_adj;
1729 
1730             FOR j IN 1..l_adj_line_id_tbl.count LOOP
1731                l_ae_line_num_tbl(j) := j + l_line_num_tbl(1); -- Bug 6827893
1732 
1733                select xla_gl_sl_link_id_s.nextval
1734                into   l_xla_gl_sl_link_id_tbl(j)
1735                from   dual;
1736             END LOOP;
1737 
1738             -- Bug 6827893: No need to create new ae_hdr_id. Use same as src
1739             l_ae_header_id := l_ae_header_id_tbl(1);
1740 
1741             -- Business Rules for xla_ae_lines
1742             -- * gl_transfer_mode_code:
1743             --       D: Detailed mode when transferred to GL
1744             --       S: Summary mode when transferred to GL
1745             -- * gl_sl_link_table must be XLAJEL
1746             -- * currency_conversion_* needs to be populated only if
1747             --   different from ledger currency
1748 
1749             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
1750             insert into xla_ae_lines (
1751                   upg_batch_id,
1752                   ae_header_id,
1753                   ae_line_num,
1754                   displayed_line_number,
1755                   application_id,
1756                   code_combination_id,
1757                   gl_transfer_mode_code,
1758                   accounted_dr,
1759                   accounted_cr,
1760                   currency_code,
1761                   currency_conversion_date,
1762                   currency_conversion_rate,
1763                   currency_conversion_type,
1764                   entered_dr,
1765                   entered_cr,
1766                   description,
1767                   accounting_class_code,
1768                   gl_sl_link_id,
1769                   gl_sl_link_table,
1770                   party_type_code,
1771                   party_id,
1772                   party_site_id,
1773                   statistical_amount,
1774                   ussgl_transaction_code,
1775                   jgzz_recon_ref,
1776                   control_balance_flag,
1777                   analytical_balance_flag,
1778                   upg_tax_reference_id1,
1779                   upg_tax_reference_id2,
1780                   upg_tax_reference_id3,
1781                   creation_date,
1782                   created_by,
1783                   last_update_date,
1784                   last_updated_by,
1785                   last_update_login,
1786                   program_update_date,
1787                   program_id,
1788                   program_application_id,
1789                   request_id,
1790                   gain_or_loss_flag,
1791                   accounting_date,
1792                   ledger_id
1793             ) values (
1794                   l_upg_batch_id,                 -- upg_batch_id
1795                   l_ae_header_id,                 -- ae_header_id
1796                   l_ae_line_num_tbl(j),           -- ae_line_num
1797                   l_ae_line_num_tbl(j),           -- displayed_line_num
1798                   c_application_id,               -- application_id
1799                   l_ccid_tbl(j),                  -- code_combination_id
1800                   'S',                            -- gl_transfer_mode_code
1801                   l_debit_amount_tbl(j),          -- accounted_dr
1802                   l_credit_amount_tbl(j),         -- accounted_cr
1803                   l_currency_code_tbl(j),         -- currency_code
1804                   null,                           -- currency_conversion_date
1805                   null,                           -- currency_conversion_rate
1806                   null,                           -- currency_conversion_type
1807                   l_debit_amount_tbl(j),          -- entered_dr
1808                   l_credit_amount_tbl(j),         -- entered_cr
1809                   l_line_desc_tbl(j) || ' - ' ||
1810                      to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
1811                                                   -- description
1812                   l_acct_class_code_tbl(j),       -- accounting_class_code
1813                   l_xla_gl_sl_link_id_tbl(j),     -- gl_sl_link_id
1814                   'XLAJEL',                       -- gl_sl_link_table
1815                   null,                           -- party_type_code
1816                   null,                           -- party_id
1817                   null,                           -- party_site_id
1818                   null,                           -- statistical_amount
1819                   null,                           -- ussgl_transaction_code
1820                   null,                           -- glzz_recon_ref
1821                   null,                           -- control_balance_flag
1822                   null,                           -- analytical_balance_flag
1823                   null,                           -- upg_tax_reference_id1
1824                   null,                           -- upg_tax_reference_id2
1825                   null,                           -- upg_tax_reference_id3
1826                   sysdate,                        -- creation_date
1827                   c_fnd_user,                     -- created_by
1828                   sysdate,                        -- last_update_date
1829                   c_fnd_user,                     -- last_updated_by
1830                   c_upgrade_bugno,                -- last_update_login
1831                   null,                           -- program_update_date
1832                   null,                           -- program_id
1833                   c_application_id,               -- program_application_id
1834                   null,                           -- request_id
1835                   'N',                            -- gain_or_loss_flag
1836                   l_cal_period_close_date_tbl(i), -- accounting_date,
1837                   l_primary_set_of_books_id_tbl(i)
1838                                                   -- ledger_id/sob_id
1839             );
1840 
1841             -- Business Rules for xla_distribution_links
1842             -- * accounting_line_code is similar to adjustment_type
1843             -- * accounting_line_type_code is S
1844             -- * merge_duplicate_code is N
1845             -- * source_distribution_type is TRX
1846             -- * source_distribution_id_num_1 is transaction_header_id
1847             -- * source_distribution_id_num_2 is event_id
1848 
1849             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
1850             insert into xla_distribution_links (
1851                   upg_batch_id,
1852                   application_id,
1853                   event_id,
1854                   ae_header_id,
1855                   ae_line_num,
1856                   accounting_line_code,
1857                   accounting_line_type_code,
1858                   source_distribution_type,
1859                   source_distribution_id_char_1,
1860                   source_distribution_id_char_2,
1861                   source_distribution_id_char_3,
1862                   source_distribution_id_char_4,
1863                   source_distribution_id_char_5,
1864                   source_distribution_id_num_1,
1865                   source_distribution_id_num_2,
1866                   source_distribution_id_num_3,
1867                   source_distribution_id_num_4,
1868                   source_distribution_id_num_5,
1869                   merge_duplicate_code,
1870                   statistical_amount,
1871                   unrounded_entered_dr,
1872                   unrounded_entered_cr,
1873                   unrounded_accounted_dr,
1874                   unrounded_accounted_cr,
1875                   ref_ae_header_id,
1876                   ref_temp_line_num,
1877                   ref_event_id,
1878                   temp_line_num,
1879                   tax_line_ref_id,
1880                   tax_summary_line_ref_id,
1881                   tax_rec_nrec_dist_ref_id,
1882                   line_definition_owner_code,
1883                   line_definition_code,
1884                   event_class_code,
1885                   event_type_code
1886                ) values (
1887                   l_upg_batch_id,              -- upg_batch_id
1888                   c_application_id,            -- application_id
1889                   l_event_id_tbl(i),           -- event_id
1890                   l_ae_header_id,              -- ae_header_id
1891                   l_ae_line_num_tbl(j),        -- ae_line_num
1892                   null,                        -- accounting_line_code
1893                   'S',                         -- accounting_line_type_code
1894                   'TRX',                       -- source_distribution_type
1895                   null,                        -- source_distribution_id_char_1
1896                   null,                        -- source_distribution_id_char_2
1897                   null,                        -- source_distribution_id_char_3
1898                   null,                        -- source_distribution_id_char_4
1899                   null,                        -- source_distribution_id_char_5
1900                   l_adj_thid_tbl(j),           -- Bug 6827893
1901                                                -- source_distribution_id_num_1
1902                   l_adj_line_id_tbl(j),        -- source_distribution_id_num_2
1903                   null,                        -- source_distribution_id_num_3
1904                   null,                        -- source_distribution_id_num_4
1905                   null,                        -- source_distribution_id_num_5
1906                   'N',                         -- merge_duplicate_code
1907                   null,                        -- statistical_amount
1908                   l_debit_amount_tbl(j),       -- unrounded_entered_dr
1909                   l_credit_amount_tbl(j),      -- unrounded_entered_cr
1910                   l_debit_amount_tbl(j),       -- unrounded_accounted_dr
1911                   l_credit_amount_tbl(j),      -- unrounded_accounted_cr
1912                   l_ae_header_id,              -- ref_ae_header_id
1913                   null,                        -- ref_temp_line_num
1914                   null,                        -- ref_event_id
1915                   l_ae_line_num_tbl(j),        -- temp_line_num
1916                   null,                        -- tax_line_ref_id
1917                   null,                        -- tax_summary_line_ref_id
1918                   null,                        -- tax_rec_nrec_dist_ref_id
1919                   null,                        -- line_definition_owner_code
1920                   null,                        -- line_definition_code
1921                   l_event_class_code_tbl(i),   -- event_class_code
1922                   l_event_class_code_tbl(i)    -- event_type_code
1923                );
1924 
1925             for j IN 1..l_xla_gl_sl_link_id_tbl.count loop
1926                if (l_je_batch_id_tbl(j) is not null) then
1927                   insert into gl_import_references (
1928                      je_batch_id,
1929                      je_header_id,
1930                      je_line_num,
1931                      last_update_date,
1932                      last_updated_by,
1933                      creation_date,
1934                      created_by,
1935                      last_update_login,
1936                      reference_1,
1937                      reference_2,
1938                      reference_3,
1939                      reference_4,
1940                      reference_5,
1941                      reference_6,
1942                      reference_7,
1943                      reference_8,
1944                      reference_9,
1945                      reference_10,
1946                      subledger_doc_sequence_id,
1947                      subledger_doc_sequence_value,
1948                      gl_sl_link_id,
1949                      gl_sl_link_table
1950                   ) values (
1951                      l_je_batch_id_tbl(j),        -- je_batch_id
1952                      l_je_header_id_tbl(j),       -- je_header_id
1953                      l_je_line_num_tbl(j),        -- je_line_num
1954                      sysdate,                     -- last_update_date
1955                      c_fnd_user,                  -- last_updated_by
1956                      sysdate,                     -- creation_date
1957                      c_fnd_user,                  -- created_by
1958                      c_upgrade_bugno,             -- last_update_login
1959                      to_char(l_dest_thid_tbl(i)),
1960                                                   -- reference_1
1961                      to_char(l_dest_asset_id_tbl(i)),
1962                                                   -- reference_2
1963                      to_char(l_distribution_id_tbl(j)),
1964                                                   -- reference_3
1965                      to_char(l_adj_line_id_tbl(j)),
1966                                                   -- reference_4
1967                      l_book_type_code_tbl(i),     -- reference_5
1968                      to_char(l_period_counter_tbl(i)),
1969                                                   -- reference_6
1970                      null,                        -- reference_7
1971                      null,                        -- reference_8
1972                      null,                        -- reference_9
1973                      null,                        -- reference_10
1974                      null,                        -- subledger_doc_seq_id
1975                      null,                        -- subledger_doc_seq_value
1976                      l_xla_gl_sl_link_id_tbl(j),  -- gl_sl_link_id
1977                      'XLAJEL'                     -- gl_sl_link_table
1978                   );
1979                end if;
1980             end loop;
1981 
1982             l_adj_line_id_tbl.delete;
1983             l_xla_gl_sl_link_id_tbl.delete;
1984             l_ae_line_num_tbl.delete;
1985             l_debit_amount_tbl.delete;
1986             l_credit_amount_tbl.delete;
1987             l_ccid_tbl.delete;
1988             l_acct_class_code_tbl.delete;
1989             l_currency_code_tbl.delete;
1990             l_line_def_owner_code_tbl.delete;
1991             l_line_def_code_tbl.delete;
1992             l_line_desc_tbl.delete;
1993             l_gl_transfer_status_code_tbl.delete;
1994             l_je_batch_id_tbl.delete;
1995             l_je_header_id_tbl.delete;
1996             l_je_line_num_tbl.delete;
1997             l_distribution_id_tbl.delete;
1998 
1999             open c_mc_books (l_book_type_code_tbl(i));
2000             fetch c_mc_books bulk collect
2001              into l_rep_set_of_books_id_tbl;
2002             close c_mc_books;
2003 
2004            for k IN 1..l_rep_set_of_books_id_tbl.count loop
2005 
2006             open c_mc_adj (l_book_type_code_tbl(i),
2007                         l_dest_asset_id_tbl(i),
2008                         l_rep_set_of_books_id_tbl(k),
2009                         l_dest_thid_tbl(i),
2010                         l_date_effective_tbl(i));
2011             fetch c_mc_adj bulk collect
2012              into l_adj_thid_tbl,
2013                   l_adj_line_id_tbl,
2014                   l_debit_amount_tbl,
2015                   l_credit_amount_tbl,
2016                   l_ccid_tbl,
2017                   l_currency_code_tbl,
2018                   l_acct_class_code_tbl,
2019                   l_line_desc_tbl,
2020                   l_gl_transfer_status_code_tbl,
2021                   l_je_batch_id_tbl,
2022                   l_je_header_id_tbl,
2023                   l_je_line_num_tbl,
2024                   l_distribution_id_tbl;
2025            close c_mc_adj;
2026 
2027             FOR j IN 1..l_adj_line_id_tbl.count LOOP
2028                l_ae_line_num_tbl(j) := j + l_line_num_tbl(k+1); -- Bug 6827893
2029 
2030                select xla_gl_sl_link_id_s.nextval
2031                into   l_xla_gl_sl_link_id_tbl(j)
2032                from   dual;
2033             END LOOP;
2034 
2035             -- Bug 6827893: No need to create new ae_hdr_id. Use same as src
2036             l_ae_header_id := l_ae_header_id_tbl(k+1);
2037 
2038             -- Business Rules for xla_ae_headers
2039             -- * amb_context_code is DEFAULT
2040             -- * reference_date must be null
2041             -- * balance_type_code:
2042             --     A: Actual
2043             --     B: Budget
2044             --     E: Encumbrance
2045             -- * gl_transfer_status_code:
2046             --     Y: already transferred to GL
2047             --     N: not transferred to GL
2048             -- * gl_transfer_date is date entry transferred to GL
2049             -- * accounting_entry_status_code must be F
2050             -- * accounting_entry_type_code must be STANDARD
2051             -- * product_rule_* not relevant for upgrade
2052 
2053                insert into xla_ae_headers (
2054                   upg_batch_id,
2055                   application_id,
2056                   amb_context_code,
2057                   entity_id,
2058                   event_id,
2059                   event_type_code,
2060                   ae_header_id,
2061                   ledger_id,
2062                   accounting_date,
2063                   period_name,
2064                   reference_date,
2065                   balance_type_code,
2066                   je_category_name,
2067                   gl_transfer_status_code,
2068                   gl_transfer_date,
2069                   accounting_entry_status_code,
2070                   accounting_entry_type_code,
2071                   description,
2072                   budget_version_id,
2073                   funds_status_code,
2074                   encumbrance_type_id,
2075                   completed_date,
2076                   doc_sequence_id,
2077                   doc_sequence_value,
2078                   doc_category_code,
2079                   packet_id,
2080                   group_id,
2081                   creation_date,
2082                   created_by,
2083                   last_update_date,
2084                   last_updated_by,
2085                   last_update_login,
2086                   program_id,
2087                   program_application_id,
2088                   program_update_date,
2089                   request_id,
2090                   close_acct_seq_assign_id,
2091                   close_acct_seq_version_id,
2092                   close_acct_seq_value,
2093                   completion_acct_seq_assign_id,
2094                   completion_acct_seq_version_id,
2095                   completion_acct_seq_value,
2096                   accounting_batch_id,
2097                   product_rule_type_code,
2098                   product_rule_code,
2099                   product_rule_version,
2100                   upg_source_application_id,
2101                   upg_valid_flag
2102                ) values (
2103                   l_upg_batch_id,               -- upg_batch_id
2104                   c_application_id,             -- application_id
2105                   c_amb_context_code,           -- amb_context_code
2106                   l_entity_id_tbl(i),           -- entity_id
2107                   l_event_id_tbl(i),            -- event_id,
2108                   l_event_class_code_tbl(i),    -- event_type_code
2109                   l_ae_header_id,               -- ae_header_id,
2110                   l_rep_set_of_books_id_tbl(k), -- ledger_id/sob_id
2111                   l_cal_period_close_date_tbl(i),
2112                                                 -- accounting_date,
2113                   l_period_name_tbl(i),         -- period_name,
2114                   null,                         -- reference_date
2115                   'A',                          -- balance_type_code,
2116                   l_je_category_name_tbl(i),    -- je_category_name
2117                   l_gl_transfer_status_code_tbl(1),  -- Bug 6811554
2118                                                 -- gl_transfer_status_code
2119                   null,                         -- gl_transfer_date
2120                   'F',                          -- accounting_entry_status_code
2121                   'STANDARD',                   -- accounting_entry_type_code
2122                   l_src_hdr_desc_tbl(i),        -- description
2123                   null,                         -- budget_version_id
2124                   null,                         -- funds_status_code
2125                   null,                         -- encumbrance_type_id
2126                   null,                         -- completed_date
2127                   null,                         -- doc_sequence_id
2128                   null,                         -- doc_sequence_value
2129                   null,                         -- doc_category_code
2130                   null,                         -- packet_id,
2131                   null,                         -- group_id
2132                   sysdate,                      -- creation_date
2133                   c_fnd_user,                   -- created_by
2134                   sysdate,                      -- last_update_date
2135                   c_fnd_user,                   -- last_updated_by
2136                   c_upgrade_bugno,              -- last_update_login
2137                   null,                         -- program_id
2138                   c_application_id,             -- program_application_id
2139                   sysdate,                      -- program_update_date
2140                   null,                         -- request_id
2141                   null,                         -- close_acct_seq_assign_id
2142                   null,                         -- close_acct_seq_version_id
2143                   null,                         -- close_acct_seq_value
2144                   null,                         -- compl_acct_seq_assign_id
2145                   null,                         -- compl_acct_seq_version_id
2146                   null,                         -- compl_acct_seq_value
2147                   null,                         -- accounting_batch_id
2148                   null,                         -- product_rule_type_code
2149                   null,                         -- product_rule_code
2150                   null,                         -- product_rule_version
2151                   c_application_id,             -- upg_souce_application_id
2152                   null                          -- upg_valid_flag
2153                );
2154 
2155             -- Business Rules for xla_ae_lines
2156             -- * gl_transfer_mode_code:
2157             --       D: Detailed mode when transferred to GL
2158             --       S: Summary mode when transferred to GL
2159             -- * gl_sl_link_table must be XLAJEL
2160             -- * currency_conversion_* needs to be populated only if
2161             --   different from ledger currency
2162 
2163             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
2164             insert into xla_ae_lines (
2165                   upg_batch_id,
2166                   ae_header_id,
2167                   ae_line_num,
2168                   displayed_line_number,
2169                   application_id,
2170                   code_combination_id,
2171                   gl_transfer_mode_code,
2172                   accounted_dr,
2173                   accounted_cr,
2174                   currency_code,
2175                   currency_conversion_date,
2176                   currency_conversion_rate,
2177                   currency_conversion_type,
2178                   entered_dr,
2179                   entered_cr,
2180                   description,
2181                   accounting_class_code,
2182                   gl_sl_link_id,
2183                   gl_sl_link_table,
2184                   party_type_code,
2185                   party_id,
2186                   party_site_id,
2187                   statistical_amount,
2188                   ussgl_transaction_code,
2189                   jgzz_recon_ref,
2190                   control_balance_flag,
2191                   analytical_balance_flag,
2192                   upg_tax_reference_id1,
2193                   upg_tax_reference_id2,
2194                   upg_tax_reference_id3,
2195                   creation_date,
2196                   created_by,
2197                   last_update_date,
2198                   last_updated_by,
2199                   last_update_login,
2200                   program_update_date,
2201                   program_id,
2202                   program_application_id,
2203                   request_id,
2204                   gain_or_loss_flag,
2205                   accounting_date,
2206                   ledger_id
2207             ) values (
2208                   l_upg_batch_id,                 -- upg_batch_id
2209                   l_ae_header_id,                 -- ae_header_id
2210                   l_ae_line_num_tbl(j),           -- ae_line_num
2211                   l_ae_line_num_tbl(j),           -- displayed_line_num
2212                   c_application_id,               -- application_id
2213                   l_ccid_tbl(j),                  -- code_combination_id
2214                   'S',                            -- gl_transfer_mode_code
2215                   l_debit_amount_tbl(j),          -- accounted_dr
2216                   l_credit_amount_tbl(j),         -- accounted_cr
2217                   l_currency_code_tbl(j),         -- currency_code
2218                   null,                           -- currency_conversion_date
2219                   null,                           -- currency_conversion_rate
2220                   null,                           -- currency_conversion_type
2221                   l_debit_amount_tbl(j),          -- entered_dr
2222                   l_credit_amount_tbl(j),         -- entered_cr
2223                   l_line_desc_tbl(j) || ' - ' ||
2224                      to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
2225                                                   -- description
2226                   l_acct_class_code_tbl(j),       -- accounting_class_code
2227                   l_xla_gl_sl_link_id_tbl(j),     -- gl_sl_link_id
2228                   'XLAJEL',                       -- gl_sl_link_table
2229                   null,                           -- party_type_code
2230                   null,                           -- party_id
2231                   null,                           -- party_site_id
2232                   null,                           -- statistical_amount
2233                   null,                           -- ussgl_transaction_code
2234                   null,                           -- glzz_recon_ref
2235                   null,                           -- control_balance_flag
2236                   null,                           -- analytical_balance_flag
2237                   null,                           -- upg_tax_reference_id1
2238                   null,                           -- upg_tax_reference_id2
2239                   null,                           -- upg_tax_reference_id3
2240                   sysdate,                        -- creation_date
2241                   c_fnd_user,                     -- created_by
2242                   sysdate,                        -- last_update_date
2243                   c_fnd_user,                     -- last_updated_by
2244                   c_upgrade_bugno,                -- last_update_login
2245                   null,                           -- program_update_date
2246                   null,                           -- program_id
2247                   c_application_id,               -- program_application_id
2248                   null,                           -- request_id
2249                   'N',                            -- gain_or_loss_flag
2250                   l_cal_period_close_date_tbl(i), -- accounting_date,
2251                   l_rep_set_of_books_id_tbl(k)    -- ledger_id/sob_id
2252             );
2253 
2254             -- Business Rules for xla_distribution_links
2255             -- * accounting_line_code is similar to adjustment_type
2256             -- * accounting_line_type_code is S
2257             -- * merge_duplicate_code is N
2258             -- * source_distribution_type is TRX
2259             -- * source_distribution_id_num_1 is trx_reference_id
2260             -- * source_distribution_id_num_2 is event_id
2261 
2262             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
2263             insert into xla_distribution_links (
2264                   upg_batch_id,
2265                   application_id,
2266                   event_id,
2267                   ae_header_id,
2268                   ae_line_num,
2269                   accounting_line_code,
2270                   accounting_line_type_code,
2271                   source_distribution_type,
2272                   source_distribution_id_char_1,
2273                   source_distribution_id_char_2,
2274                   source_distribution_id_char_3,
2275                   source_distribution_id_char_4,
2276                   source_distribution_id_char_5,
2277                   source_distribution_id_num_1,
2278                   source_distribution_id_num_2,
2279                   source_distribution_id_num_3,
2280                   source_distribution_id_num_4,
2281                   source_distribution_id_num_5,
2282                   merge_duplicate_code,
2283                   statistical_amount,
2284                   unrounded_entered_dr,
2285                   unrounded_entered_cr,
2286                   unrounded_accounted_dr,
2287                   unrounded_accounted_cr,
2288                   ref_ae_header_id,
2289                   ref_temp_line_num,
2290                   ref_event_id,
2291                   temp_line_num,
2292                   tax_line_ref_id,
2293                   tax_summary_line_ref_id,
2294                   tax_rec_nrec_dist_ref_id,
2295                   line_definition_owner_code,
2296                   line_definition_code,
2297                   event_class_code,
2298                   event_type_code
2299             ) values (
2300                   l_upg_batch_id,              -- upg_batch_id
2301                   c_application_id,            -- application_id
2302                   l_event_id_tbl(i),           -- event_id
2303                   l_ae_header_id,              -- ae_header_id
2304                   l_ae_line_num_tbl(j),        -- ae_line_num
2305                   null,                        -- accounting_line_code
2306                   'S',                         -- accounting_line_type_code
2307                   'TRX',                       -- source_distribution_type
2308                   null,                        -- source_distribution_id_char_1
2309                   null,                        -- source_distribution_id_char_2
2310                   null,                        -- source_distribution_id_char_3
2311                   null,                        -- source_distribution_id_char_4
2312 
2313                   null,                        -- source_distribution_id_char_5
2314                   l_adj_thid_tbl(j),           -- Bug 6827893
2315                                                -- source_distribution_id_num_1
2316                   l_adj_line_id_tbl(j),        -- source_distribution_id_num_2
2317                   null,                        -- source_distribution_id_num_3
2318                   null,                        -- source_distribution_id_num_4
2319                   null,                        -- source_distribution_id_num_5
2320                   'N',                         -- merge_duplicate_code
2321                   null,                        -- statistical_amount
2322                   l_debit_amount_tbl(j),       -- unrounded_entered_dr
2323                   l_credit_amount_tbl(j),      -- unrounded_entered_cr
2324                   l_debit_amount_tbl(j),       -- unrounded_accounted_dr
2325                   l_credit_amount_tbl(j),      -- unrounded_accounted_cr
2326                   l_ae_header_id,              -- ref_ae_header_id
2327                   null,                        -- ref_temp_line_num
2328                   null,                        -- ref_event_id
2329                   l_ae_line_num_tbl(j),        -- temp_line_num
2330                   null,                        -- tax_line_ref_id
2331                   null,                        -- tax_summary_line_ref_id
2332                   null,                        -- tax_rec_nrec_dist_ref_id
2333                   null,                        -- line_definition_owner_code
2334                   null,                        -- line_definition_code
2335                   l_event_class_code_tbl(i),   -- event_class_code
2336                   l_event_class_code_tbl(i)    -- event_type_code
2337             );
2338 
2339             for j IN 1..l_xla_gl_sl_link_id_tbl.count loop
2340                if (l_je_batch_id_tbl(j) is not null) then
2341                   insert into gl_import_references (
2342                      je_batch_id,
2343                      je_header_id,
2344                      je_line_num,
2345                      last_update_date,
2346                      last_updated_by,
2347                      creation_date,
2348                      created_by,
2349                      last_update_login,
2350                      reference_1,
2351                      reference_2,
2352                      reference_3,
2353                      reference_4,
2354                      reference_5,
2355                      reference_6,
2356                      reference_7,
2357                      reference_8,
2358                      reference_9,
2359                      reference_10,
2360                      subledger_doc_sequence_id,
2361                      subledger_doc_sequence_value,
2362                      gl_sl_link_id,
2363                      gl_sl_link_table
2364                   ) values (
2365                      l_je_batch_id_tbl(j),        -- je_batch_id
2366                      l_je_header_id_tbl(j),       -- je_header_id
2367                      l_je_line_num_tbl(j),        -- je_line_num
2368                      sysdate,                     -- last_update_date
2369                      c_fnd_user,                  -- last_updated_by
2370                      sysdate,                     -- creation_date
2371                      c_fnd_user,                  -- created_by
2372                      c_upgrade_bugno,             -- last_update_login
2373                      to_char(l_src_thid_tbl(i)),
2374                                                   -- reference_1
2375                      to_char(l_src_asset_id_tbl(i)),
2376                                                   -- reference_2
2377                      to_char(l_distribution_id_tbl(j)),
2378                                                   -- reference_3
2379                      to_char(l_adj_line_id_tbl(j)),
2380                                                   -- reference_4
2381                      l_book_type_code_tbl(i),     -- reference_5
2382                      to_char(l_period_counter_tbl(i)),
2383                                                   -- reference_6
2384                      null,                        -- reference_7
2385                      null,                        -- reference_8
2386                      null,                        -- reference_9
2387                      null,                        -- reference_10
2388                      null,                        -- subledger_doc_seq_id
2389                      null,                        -- subledger_doc_seq_value
2390                      l_xla_gl_sl_link_id_tbl(j),  -- gl_sl_link_id
2391                      'XLAJEL'                     -- gl_sl_link_table
2392                   );
2393                end if;
2394             end loop;
2395 
2396             l_adj_line_id_tbl.delete;
2397             l_xla_gl_sl_link_id_tbl.delete;
2398             l_ae_line_num_tbl.delete;
2399             l_debit_amount_tbl.delete;
2400             l_credit_amount_tbl.delete;
2401             l_ccid_tbl.delete;
2402             l_acct_class_code_tbl.delete;
2403             l_currency_code_tbl.delete;
2404             l_line_def_owner_code_tbl.delete;
2405             l_line_def_code_tbl.delete;
2406             l_line_desc_tbl.delete;
2407             l_gl_transfer_status_code_tbl.delete;
2408             l_je_batch_id_tbl.delete;
2409             l_je_header_id_tbl.delete;
2410             l_je_line_num_tbl.delete;
2411             l_distribution_id_tbl.delete;
2412 
2413             end loop;
2414 
2415             l_rep_set_of_books_id_tbl.delete;
2416             l_line_num_tbl.delete;     -- Bug 6827893
2417             l_ae_header_id_tbl.delete; -- Bug 6827893
2418 
2419       END LOOP;
2420 
2421       l_src_rowid_tbl.delete;
2422       l_dest_rowid_tbl.delete;
2423       l_event_id_tbl.delete;
2424       l_src_asset_id_tbl.delete;
2425       l_dest_asset_id_tbl.delete;
2426       l_book_type_code_tbl.delete;
2427       l_primary_set_of_books_id_tbl.delete;
2428       l_org_id_tbl.delete;
2429       l_src_trans_type_code_tbl.delete;
2430       l_dest_trans_type_code_tbl.delete;
2431       l_transaction_date_entered_tbl.delete;
2432       l_src_thid_tbl.delete;
2433       l_dest_thid_tbl.delete;
2434       l_period_counter_tbl.delete;
2435       l_period_name_tbl.delete;
2436       l_cal_period_close_date_tbl.delete;
2437       l_entity_id_tbl.delete;
2438       l_event_class_code_tbl.delete;
2439       l_trx_reference_id_tbl.delete;
2440       l_src_hdr_desc_tbl.delete;
2441       l_dest_hdr_desc_tbl.delete;
2442       l_je_category_name_tbl.delete;
2443       l_date_effective_tbl.delete;
2444 
2445       commit;
2446 
2447       if (l_rows_processed < l_batch_size) then exit; end if;
2448 
2449    end loop;
2450    close c_trans;
2451 
2452 EXCEPTION
2453    WHEN OTHERS THEN
2454       rollback;
2455       raise;
2456 
2457 END Upgrade_Inv_Events;
2458 
2459 Procedure Upgrade_Group_Trxn_Events (
2460              p_start_rowid             IN            rowid,
2461              p_end_rowid               IN            rowid,
2462              p_batch_size              IN            number,
2463              x_success_count              OUT NOCOPY number,
2464              x_failure_count              OUT NOCOPY number,
2465              x_return_status              OUT NOCOPY number
2466             ) IS
2467 
2468    c_application_id            constant number(15) := 140;
2469    c_upgrade_bugno             constant number(15) := -4107161;
2470    c_fnd_user                  constant number(15) := 2;
2471 
2472    c_entity_code               constant varchar2(30) := 'TRANSACTIONS';
2473    c_amb_context_code          constant varchar2(30) := 'DEFAULT';
2474 
2475    -- this value can be altered in order to process more of less per batch
2476    l_batch_size                NUMBER;
2477 
2478    l_rows_processed            NUMBER;
2479 
2480    -- type for table variable
2481    type num_tbl_type  is table of number        index by binary_integer;
2482    type char_tbl_type is table of varchar2(150) index by binary_integer;
2483    type date_tbl_type is table of date          index by binary_integer;
2484    type rowid_tbl_type is table of rowid        index by binary_integer;
2485 
2486    -- used for bulk fetching
2487    -- main cursor
2488    l_event_id_tbl                               num_tbl_type;
2489    l_asset_id_tbl                               num_tbl_type;
2490    l_book_type_code_tbl                         char_tbl_type;
2491    l_primary_set_of_books_id_tbl                num_tbl_type;
2492    l_org_id_tbl                                 num_tbl_type;
2493    l_transaction_type_code_tbl                  char_tbl_type;
2494    l_transaction_date_entered_tbl               date_tbl_type;
2495    l_transaction_header_id_tbl                  num_tbl_type;
2496    l_period_counter_tbl                         num_tbl_type;
2497    l_period_name_tbl                            char_tbl_type;
2498    l_cal_period_close_date_tbl                  date_tbl_type;
2499    l_rowid_tbl                                  rowid_tbl_type;
2500    l_member_thid_tbl                            num_tbl_type;
2501 
2502    l_upg_batch_id                               number;
2503    l_ae_header_id                               number;
2504 
2505    l_entity_id_tbl                              num_tbl_type;
2506    l_event_class_code_tbl                       char_tbl_type;
2507    l_rep_set_of_books_id_tbl                    num_tbl_type;
2508    l_currency_code_tbl                          char_tbl_type;
2509    l_hdr_desc_tbl                               char_tbl_type;
2510    l_je_category_name_tbl                       char_tbl_type;
2511    l_date_effective_tbl                         date_tbl_type;
2512 
2513    l_adj_line_id_tbl                            num_tbl_type;
2514    l_xla_gl_sl_link_id_tbl                      num_tbl_type;
2515    l_ae_line_num_tbl                            num_tbl_type;
2516    l_debit_amount_tbl                           num_tbl_type;
2517    l_credit_amount_tbl                          num_tbl_type;
2518    l_ccid_tbl                                   num_tbl_type;
2519    l_acct_class_code_tbl                        char_tbl_type;
2520    l_line_def_owner_code_tbl                    char_tbl_type;
2521    l_line_def_code_tbl                          char_tbl_type;
2522    l_line_desc_tbl                              char_tbl_type;
2523    l_gl_transfer_status_code_tbl                char_tbl_type;
2524    l_je_batch_id_tbl                            num_tbl_type;
2525    l_je_header_id_tbl                           num_tbl_type;
2526    l_je_line_num_tbl                            num_tbl_type;
2527    l_distribution_id_tbl                        num_tbl_type;
2528 
2529    l_error_level_tbl                            char_tbl_type;
2530    l_err_entity_id_tbl                          num_tbl_type;
2531    l_err_event_id_tbl                           num_tbl_type;
2532    l_err_ae_header_id_tbl                       num_tbl_type;
2533    l_err_ae_line_num_tbl                        num_tbl_type;
2534    l_err_temp_line_num_tbl                      num_tbl_type;
2535    l_error_message_name_tbl                     char_tbl_type;
2536 
2537    CURSOR c_trans IS
2538       select /*+ leading(th) rowid(th) */
2539              th.asset_id,
2540              th.book_type_code,
2541              bc.set_of_books_id,
2542              bc.org_id,
2543              th.transaction_type_code,
2544              greatest (th.transaction_date_entered,
2545                            dp.calendar_period_open_date), -- Bug 12533865
2546              th.transaction_header_id,
2547              th.rowid,
2548              decode(th.transaction_type_code,
2549                 'ADDITION', decode (ah.asset_type,
2550                                     'CIP', 'CAPITALIZATION',
2551                                     'ADDITIONS'),
2552                 'ADJUSTMENT', 'ADJUSTMENTS',
2553                 'CIP ADDITION', 'CIP_ADDITIONS',
2554                 'CIP ADJUSTMENT', 'CIP_ADJUSTMENTS',
2555                 'CIP REVERSE', 'REVERSE_CAPITALIZATION',
2556                 'CIP REINSTATEMENT', 'CIP_REINSTATEMENTS',
2557                 'CIP RETIREMENT', 'CIP_RETIREMENTS',
2558                 'CIP TRANSFER', 'CIP_TRANSFERS',
2559                 'CIP UNIT ADJUSTMENTS', 'CIP_UNIT_ADJUSTMENTS',
2560                 'FULL RETIREMENT', 'RETIREMENTS',
2561                 'PARTIAL RETIREMENT', 'RETIREMENTS',
2562                 'RECLASS', 'CATEGORY_RECLASS',
2563                 'REINSTATEMENT', 'REINSTATEMENTS',
2564                 'REVALUATION', 'REVALUATION',
2565                 'TRANSFER', 'TRANSFERS',
2566                 'TRANSFER IN', 'TRANSFERS',
2567                 'TRANSFER IN/VOID', 'TRANSFERS',
2568                 'TRANSFER OUT', 'TRANSFERS',
2569                 'UNIT ADJUSTMENT', 'UNIT_ADJUSTMENTS',
2570                 'UNPLANNED DEPRN', 'UNPLANNED_DEPRECIATION',
2571                 'TAX', 'DEPRECIATION_ADJUSTMENTS',
2572                 'OTHER'
2573              ) event_class_code,
2574              dp.period_name,
2575              dp.period_counter,
2576              dp.calendar_period_close_date,
2577              th.member_transaction_header_id,
2578              lk.description || ' - ' ||
2579                 to_char(dp.calendar_period_close_date, 'DD-MON-RR'),
2580              nvl (decode(th.transaction_type_code,
2581                 'ADDITION',             bc.je_addition_category,
2582                 'ADJUSTMENT',           bc.je_adjustment_category,
2583                 'CIP ADDITION',         bc.je_cip_addition_category,
2584                 'CIP ADJUSTMENT',       bc.je_cip_adjustment_category,
2585                 'CIP REVERSE',          bc.je_cip_addition_category,
2586                 'CIP REINSTATEMENT',    bc.je_cip_retirement_category,
2587                 'CIP RETIREMENT',       bc.je_cip_retirement_category,
2588                 'CIP TRANSFER',         bc.je_cip_transfer_category,
2589                 'CIP UNIT ADJUSTMENTS', bc.je_cip_transfer_category,
2590                 'FULL RETIREMENT',      bc.je_retirement_category,
2591                 'PARTIAL RETIREMENT',   bc.je_retirement_category,
2592                 'RECLASS',              bc.je_reclass_category,
2593                 'REINSTATEMENT',        bc.je_retirement_category,
2594                 'REVALUATION',          bc.je_reval_category,
2595                 'TRANSFER',             bc.je_transfer_category,
2596                 'TRANSFER IN',          bc.je_transfer_category,
2597                 'TRANSFER IN/VOID',     bc.je_transfer_category,
2598                 'TRANSFER OUT',         bc.je_transfer_category,
2599                 'UNIT ADJUSTMENT',      bc.je_transfer_category,
2600                 'UNPLANNED DEPRN',      bc.je_depreciation_category,
2601                 'TAX',                  bc.je_deprn_adjustment_category
2602              ), 'OTHER') je_category_name,
2603              th.date_effective
2604       from   fa_transaction_headers th,
2605              fa_asset_history ah,
2606              fa_deprn_periods dp,
2607              gl_sets_of_books glsob,
2608              fa_book_controls bc,
2609              fa_lookups_tl lk,
2610              gl_period_statuses ps
2611       where  th.rowid between p_start_rowid and p_end_rowid
2612       and    ps.application_id = 101
2613       and    ((ps.migration_status_code in ('P', 'U')) or
2614               (dp.period_close_date is null))
2615       and    substr(dp.xla_conversion_status, 1, 1) in
2616              ('H', 'U', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
2617       and    dp.xla_conversion_status not in ('UT', 'UA')
2618       and    ps.set_of_books_id = bc.set_of_books_id
2619       and    ps.period_name = dp.period_name
2620       and    th.book_type_code = bc.book_type_code
2621       and    bc.allow_group_deprn_flag = 'Y'
2622       and    bc.set_of_books_id = glsob.set_of_books_id
2623       and    dp.book_type_code = bc.book_type_code
2624       and    th.book_type_code = bc.book_type_code
2625       and    th.date_effective between
2626                 dp.period_open_date and nvl(dp.period_close_date, sysdate)
2627       and    th.member_transaction_header_id is not null
2628       and    th.transaction_subtype not in ('GA', 'M%', 'GC', 'GV')
2629       and    th.asset_id = ah.asset_id (+)
2630       and    th.transaction_header_id = ah.transaction_header_id_out (+)
2631       and    th.event_id is null
2632       and    th.transaction_type_code = lk.lookup_code
2633       and    lk.lookup_type = 'FAXOLTRX'
2634       and    userenv('LANG') = lk.language
2635       and exists
2636       (
2637        select 'x'
2638        from   fa_adjustments adj
2639        where  th.transaction_header_id = adj.transaction_header_id
2640        and    th.book_type_code = adj.book_type_code
2641        and    th.asset_id = adj.asset_id
2642       )
2643    UNION ALL
2644       select /*+ leading(th) rowid(th) */
2645              th.asset_id,
2646              th.book_type_code,
2647              bc.set_of_books_id,
2648              bc.org_id,
2649              th.transaction_type_code,
2650              dp.calendar_period_open_date,  -- Bug 12533865
2651              th.transaction_header_id,
2652              th.rowid,
2653              decode(th.transaction_type_code,
2654                 'CIP TRANSFER', 'CIP_TRANSFERS',
2655                 'CIP UNIT ADJUSTMENTS', 'CIP_UNIT_ADJUSTMENTS',
2656                 'CIP REINSTATEMENT', 'CIP_TRANSFERS',
2657                 'RECLASS', 'CATEGORY_RECLASS',
2658                 'REINSTATEMENT', 'REINSTATEMENTS',
2659                 'TRANSFER', 'TRANSFERS',
2660                 'TRANSFER OUT', 'TRANSFERS',
2661                 'UNIT ADJUSTMENT', 'UNIT_ADJUSTMENTS',
2662                 'OTHER'
2663              ) event_class_code,
2664              dp.period_name,
2665              dp.period_counter,
2666              dp.calendar_period_close_date,
2667              th.member_transaction_header_id,
2668              lk.description || ' - ' ||
2669                 to_char(dp.calendar_period_close_date, 'DD-MON-RR'),
2670              nvl (decode(th.transaction_type_code,
2671                 'CIP REINSTATEMENT',    bc.je_cip_transfer_category,
2672                 'CIP TRANSFER',         bc.je_cip_transfer_category,
2673                 'CIP UNIT ADJUSTMENTS', bc.je_cip_transfer_category,
2674                 'RECLASS',              bc.je_reclass_category,
2675                 'REINSTATEMENT',        bc.je_transfer_category,
2676                 'TRANSFER',             bc.je_transfer_category,
2677                 'TRANSFER OUT',         bc.je_transfer_category,
2678                 'UNIT ADJUSTMENT',      bc.je_transfer_category),
2679                 'OTHER') je_category_name,
2680              th.date_effective
2681       from   fa_transaction_headers th,
2682              fa_deprn_periods dp,
2683              gl_sets_of_books glsob,
2684              fa_book_controls bc,
2685              fa_lookups_tl lk,
2686              gl_period_statuses ps
2687       where  th.rowid between p_start_rowid and p_end_rowid
2688       and    ps.application_id = 101
2689       and    ((ps.migration_status_code in ('P', 'U')) or
2690               (dp.period_close_date is null))
2691       and    substr(dp.xla_conversion_status, 1, 1) in
2692              ('H', 'U', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
2693       and    dp.xla_conversion_status not in ('UT', 'UA')
2694       and    ps.set_of_books_id = bc.set_of_books_id
2695       and    ps.period_name = dp.period_name
2696       and    th.transaction_type_code in ('TRANSFER', 'TRANSFER OUT',
2697                    'RECLASS', 'UNIT ADJUSTMENT', 'REINSTATEMENT',
2698                    'CIP REINSTATEMENT', 'CIP TRANSFER', 'CIP UNIT ADJUSTMENTS')
2699       and    th.book_type_code = bc.distribution_source_book
2700       and    bc.book_class = 'TAX'
2701       and    bc.allow_group_deprn_flag = 'Y'
2702       and    bc.set_of_books_id = glsob.set_of_books_id
2703       and    dp.book_type_code = bc.book_type_code
2704       and    th.date_effective between
2705                 dp.period_open_date and nvl(dp.period_close_date, sysdate)
2706       and    th.member_transaction_header_id is not null
2707       and    th.transaction_subtype not in ('GA', 'M%', 'GC', 'GV')
2708       and    th.event_id is null
2709       and    th.transaction_type_code = lk.lookup_code
2710       and    lk.lookup_type = 'FAXOLTRX'
2711       and    userenv('LANG') = lk.language
2712       and exists
2713       (
2714        select 'x'
2715        from   fa_adjustments adj
2716        where  th.transaction_header_id = adj.transaction_header_id
2717        and    bc.book_type_code = adj.book_type_code
2718        and    th.asset_id = adj.asset_id
2719       );
2720 
2721    -- Bug 6827893 : Added outerjoin between DD and gljh
2722    --               Corrected the Reserve row (second union)
2723    CURSOR c_adj (l_book_type_code        varchar2,
2724                  l_asset_id              number,
2725                  l_transaction_header_id number,
2726                  l_date_effective        date) IS
2727       select adj.adjustment_line_id,
2728              decode (adj.debit_credit_flag,
2729                      'DR', adj.adjustment_amount, null),
2730              decode (adj.debit_credit_flag,
2731                      'CR', adj.adjustment_amount, null),
2732              adj.code_combination_id,
2733              glsob.currency_code,
2734              decode (adj.adjustment_type,
2735                 'BONUS EXPENSE', 'EXPENSE',
2736                 'BONUS RESERVE', 'ASSET',
2737                 'CIP COST', 'ASSET',
2738                 'COST', 'ASSET',
2739                 'COST CLEARING', 'ASSET',
2740                 'DEPRN ADJUST', 'EXPENSE',
2741                 'EXPENSE', 'EXPENSE',
2742                 'GRP COR RESERVE', 'ASSET',
2743                 'GRP PRC RESERVE', 'ASSET',
2744                 'INTERCO AP', 'LIABILITY',
2745                 'INTERCO AR', 'ASSET',
2746                 'NBV RETIRED', 'ASSET',
2747                 'PROCEEDS', 'ASSET',
2748                 'PROCEEDS CLR', 'ASSET',
2749                 'REMOVALCOST', 'ASSET',
2750                 'REMOVALCOST CLR', 'ASSET',
2751                 'RESERVE', 'ASSET',
2752                 'REVAL RESERVE', 'ASSET',
2753                 'REVAL RSV RET', 'ASSET',
2754                 'REVAL AMORT', 'EXPENSE',
2755                 'REVAL EXPENSE', 'EXPENSE',
2756                 'ASSET'),
2757              lk.description,
2758              decode (gljh.je_header_id, null,
2759                                         decode (bc.gl_posting_allowed_flag, 'YES',
2760                                                                             'N', 'NT'),
2761                                         'Y'), -- Bug 12879380
2762              gljh.je_batch_id,
2763              adj.je_header_id,
2764              nvl(adj.je_line_num, 0),
2765              adj.distribution_id
2766       from   fa_adjustments adj,
2767              fa_lookups_tl lk,
2768              gl_je_headers gljh,
2769              fa_book_controls bc,
2770              gl_sets_of_books glsob
2771       where  bc.book_type_code = l_book_type_code
2772       and    bc.set_of_books_id = glsob.set_of_books_id
2773       and    bc.book_type_code = adj.book_type_code
2774       and    adj.asset_id = l_asset_id
2775       and    adj.transaction_header_id = l_transaction_header_id
2776       and    adj.code_combination_id is not null
2777       and    nvl(adj.track_member_flag, 'N') = 'N'
2778       and    lk.lookup_type = 'JOURNAL ENTRIES'
2779       and    lk.lookup_code = decode (adj.adjustment_type,
2780                               'BONUS EXPENSE', 'BONUS DEPRECIATION EXPENSE',
2781                               'BONUS RESERVE', 'BONUS DEPRECIATION RESERVE',
2782                               'CIP COST', adj.source_type_code ||' COST',
2783                               adj.source_type_code ||' '|| adj.adjustment_type)
2784       and    userenv('LANG') = lk.language
2785       and    adj.je_header_id = gljh.je_header_id (+)
2786    UNION ALL
2787       select adj.adjustment_line_id,
2788              decode (adj.debit_credit_flag,
2789                      'CR', adj.adjustment_amount, null),
2790              decode (adj.debit_credit_flag,
2791                      'DR', adj.adjustment_amount, null),
2792              nvl(nvl(gljl.code_combination_id, da.deprn_reserve_account_ccid),
2793                  cb.reserve_account_ccid),
2794              glsob.currency_code,
2795              decode (adj.adjustment_type,
2796                 'BONUS EXPENSE', 'EXPENSE',
2797                 'BONUS RESERVE', 'ASSET',
2798                 'CIP COST', 'ASSET',
2799                 'COST', 'ASSET',
2800                 'COST CLEARING', 'ASSET',
2801                 'DEPRN ADJUST', 'EXPENSE',
2802                 'EXPENSE', 'ASSET',
2803                 'GRP COR RESERVE', 'ASSET',
2804                 'GRP PRC RESERVE', 'ASSET',
2805                 'INTERCO AP', 'LIABILITY',
2806                 'INTERCO AR', 'ASSET',
2807                 'NBV RETIRED', 'ASSET',
2808                 'PROCEEDS', 'ASSET',
2809                 'PROCEEDS CLR', 'ASSET',
2810                 'REMOVALCOST', 'ASSET',
2811                 'REMOVALCOST CLR', 'ASSET',
2812                 'RESERVE', 'ASSET',
2813                 'REVAL RESERVE', 'ASSET',
2814                 'REVAL RSV RET', 'ASSET',
2815                 'REVAL AMORT', 'EXPENSE',
2816                 'REVAL EXPENSE', 'EXPENSE',
2817                 'ASSET'),
2818              lk.description,
2819              decode (gljh.je_header_id, null,
2820                                         decode (bc.gl_posting_allowed_flag, 'YES',
2821                                                                             'N', 'NT'),
2822                                         'Y'), -- Bug 12879380
2823              gljh.je_batch_id,
2824              dd.je_header_id,
2825              nvl(dd.deprn_reserve_je_line_num, 0),
2826              adj.distribution_id
2827       from   fa_adjustments adj,
2828              gl_sets_of_books glsob,
2829              fa_book_controls bc,
2830              fa_lookups_tl lk,
2831              gl_je_headers gljh,
2832              fa_deprn_detail dd,
2833              gl_je_lines gljl,
2834              fa_distribution_accounts da,
2835              fa_asset_history ah,
2836              fa_category_books cb
2837       where  bc.book_type_code = l_book_type_code
2838       and    bc.set_of_books_id = glsob.set_of_books_id
2839       and    bc.book_type_code = adj.book_type_code
2840       and    adj.asset_id = l_asset_id
2841       and    adj.transaction_header_id = l_transaction_header_id
2842       and    adj.adjustment_type = 'EXPENSE'
2843       and    adj.source_type_code in ('DEPRECIATION', 'CIP RETIREMENT',
2844                                       'RETIREMENT')
2845       and    adj.code_combination_id is not null
2846       and    nvl(adj.track_member_flag, 'N') = 'N'
2847       and    lk.lookup_type = 'JOURNAL ENTRIES'
2848       and    lk.lookup_code = adj.source_type_code ||' RESERVE'
2849       and    userenv('LANG') = lk.language
2850       and    adj.asset_id = dd.asset_id (+)
2851       and    adj.book_type_code = dd.book_type_code (+)
2852       and    adj.distribution_id = dd.distribution_id (+)
2853       and    adj.period_counter_created = dd.period_counter (+)
2854       and    dd.je_header_id = gljl.je_header_id (+)
2855       and    dd.deprn_reserve_je_line_num = gljl.je_line_num (+)
2856       and    dd.je_header_id = gljh.je_header_id (+)
2857       and    adj.book_type_code = da.book_type_code (+)
2858       and    adj.distribution_id = da.distribution_id (+)
2859       and    adj.asset_id = ah.asset_id
2860       and    l_date_effective >= ah.date_effective
2861       and    l_date_effective < nvl(ah.date_ineffective, sysdate+1)
2862       and    ah.category_id = cb.category_id
2863       and    bc.book_type_code = cb.book_type_code
2864    UNION ALL
2865       select adj.adjustment_line_id,
2866              decode (adj.debit_credit_flag,
2867                      'DR', adj.adjustment_amount, null),
2868              decode (adj.debit_credit_flag,
2869                      'CR', adj.adjustment_amount, null),
2870              nvl(nvl(gljl.code_combination_id, da.deprn_reserve_account_ccid),
2871                  cb.reserve_account_ccid),
2872              glsob.currency_code,
2873              decode (adj.adjustment_type,
2874                 'BONUS EXPENSE', 'EXPENSE',
2875                 'BONUS RESERVE', 'ASSET',
2876                 'CIP COST', 'ASSET',
2877                 'COST', 'ASSET',
2878                 'COST CLEARING', 'ASSET',
2879                 'DEPRN ADJUST', 'EXPENSE',
2880                 'EXPENSE', 'EXPENSE',
2881                 'GRP COR RESERVE', 'ASSET',
2882                 'GRP PRC RESERVE', 'ASSET',
2883                 'INTERCO AP', 'LIABILITY',
2884                 'INTERCO AR', 'ASSET',
2885                 'NBV RETIRED', 'ASSET',
2886                 'PROCEEDS', 'ASSET',
2887                 'PROCEEDS CLR', 'ASSET',
2888                 'REMOVALCOST', 'ASSET',
2889                 'REMOVALCOST CLR', 'ASSET',
2890                 'RESERVE', 'ASSET',
2891                 'REVAL RESERVE', 'ASSET',
2892                 'REVAL RSV RET', 'ASSET',
2893                 'REVAL AMORT', 'EXPENSE',
2894                 'REVAL EXPENSE', 'EXPENSE',
2895                 'ASSET'),
2896              lk.description,
2897              decode (gljh.je_header_id, null,
2898                                         decode (bc.gl_posting_allowed_flag, 'YES',
2899                                                                             'N', 'NT'),
2900                                         'Y'), -- Bug 12879380
2901              gljh.je_batch_id,
2902              dd.je_header_id,
2903              nvl(dd.deprn_reserve_je_line_num, 0),
2904              adj.distribution_id
2905       from   fa_adjustments adj,
2906              gl_sets_of_books glsob,
2907              fa_book_controls bc,
2908              fa_lookups_tl lk,
2909              gl_je_headers gljh,
2910              fa_deprn_detail dd,
2911              gl_je_lines gljl,
2912              fa_distribution_accounts da,
2913              fa_asset_history ah,
2914              fa_category_books cb
2915       where  bc.book_type_code = l_book_type_code
2916       and    bc.set_of_books_id = glsob.set_of_books_id
2917       and    bc.book_type_code = adj.book_type_code
2918       and    adj.asset_id = l_asset_id
2919       and    adj.transaction_header_id = l_transaction_header_id
2920       and    adj.adjustment_type = 'BONUS EXPENSE'
2921       and    adj.source_type_code = 'DEPRECIATION'
2922       and    adj.code_combination_id is not null
2923       and    nvl(adj.track_member_flag, 'N') = 'N'
2924       and    lk.lookup_type = 'JOURNAL ENTRIES'
2925       and    lk.lookup_code = 'BONUS DEPRECIATION RESERVE'
2926       and    userenv('LANG') = lk.language
2927       and    adj.asset_id = dd.asset_id (+)
2928       and    adj.book_type_code = dd.book_type_code (+)
2929       and    adj.distribution_id = dd.distribution_id (+)
2930       and    adj.period_counter_created = dd.period_counter (+)
2931       and    dd.je_header_id = gljl.je_header_id (+)
2932       and    dd.bonus_deprn_rsv_je_line_num = gljl.je_line_num (+)
2933       and    dd.je_header_id = gljh.je_header_id (+)
2934       and    adj.book_type_code = da.book_type_code (+)
2935       and    adj.distribution_id = da.distribution_id (+)
2936       and    adj.asset_id = ah.asset_id
2937       and    l_date_effective >= ah.date_effective
2938       and    l_date_effective < nvl(ah.date_ineffective, sysdate+1)
2939       and    ah.category_id = cb.category_id
2940       and    bc.book_type_code = cb.book_type_code;
2941 
2942    CURSOR c_mc_books (l_book_type_code      varchar2) IS
2943    select set_of_books_id
2944      from fa_mc_book_controls
2945     where book_type_code = l_book_type_code
2946       and enabled_flag = 'Y';
2947 
2948    -- Bug 6827893 : Added outerjoin between DD and gljh
2949    --               Corrected the Reserve row (second union)
2950    CURSOR c_mc_adj (l_book_type_code        varchar2,
2951                     l_asset_id              number,
2952                     l_set_of_books_id       number,
2953                     l_transaction_header_id number,
2954                     l_date_effective        date) IS
2955       select adj.adjustment_line_id,
2956              decode (adj.debit_credit_flag,
2957                      'DR', adj.adjustment_amount, null),
2958              decode (adj.debit_credit_flag,
2959                      'CR', adj.adjustment_amount, null),
2960              adj.code_combination_id,
2961              glsob.currency_code,
2962              decode (adj.adjustment_type,
2963                 'BONUS EXPENSE', 'EXPENSE',
2964                 'BONUS RESERVE', 'ASSET',
2965                 'CIP COST', 'ASSET',
2966                 'COST', 'ASSET',
2967                 'COST CLEARING', 'ASSET',
2968                 'DEPRN ADJUST', 'EXPENSE',
2969                 'EXPENSE', 'EXPENSE',
2970                 'GRP COR RESERVE', 'ASSET',
2971                 'GRP PRC RESERVE', 'ASSET',
2972                 'INTERCO AP', 'LIABILITY',
2973                 'INTERCO AR', 'ASSET',
2974                 'NBV RETIRED', 'ASSET',
2975                 'PROCEEDS', 'ASSET',
2976                 'PROCEEDS CLR', 'ASSET',
2977                 'REMOVALCOST', 'ASSET',
2978                 'REMOVALCOST CLR', 'ASSET',
2979                 'RESERVE', 'ASSET',
2980                 'REVAL RESERVE', 'ASSET',
2981                 'REVAL RSV RET', 'ASSET',
2982                 'REVAL AMORT', 'EXPENSE',
2983                 'REVAL EXPENSE', 'EXPENSE',
2984                 'ASSET'),
2985              lk.description,
2986              decode (gljh.je_header_id, null,
2987                                         decode (bcp.gl_posting_allowed_flag, 'YES',
2988                                                                              'N', 'NT'),
2989                                         'Y'), -- Bug 12879380
2990              gljh.je_batch_id,
2991              adj.je_header_id,
2992              nvl(adj.je_line_num, 0),
2993              adj.distribution_id
2994       from   fa_mc_adjustments adj,
2995              fa_lookups_tl lk,
2996              gl_je_headers gljh,
2997              fa_mc_book_controls bc,
2998              fa_book_controls bcp,
2999              gl_sets_of_books glsob
3000       where  bc.book_type_code = l_book_type_code
3001       and    bc.set_of_books_id = l_set_of_books_id
3002       and    bcp.book_type_code = l_book_type_code
3003       and    bc.enabled_flag = 'Y'
3004       and    bc.set_of_books_id = glsob.set_of_books_id
3005       and    bc.book_type_code = adj.book_type_code
3006       and    bc.set_of_books_id = adj.set_of_books_id
3007       and    adj.asset_id = l_asset_id
3008       and    adj.transaction_header_id = l_transaction_header_id
3009       and    adj.code_combination_id is not null
3010       and    nvl(adj.track_member_flag, 'N') = 'N'
3011       and    lk.lookup_type = 'JOURNAL ENTRIES'
3012       and    lk.lookup_code = decode (adj.adjustment_type,
3013                               'BONUS EXPENSE', 'BONUS DEPRECIATION EXPENSE',
3014                               'BONUS RESERVE', 'BONUS DEPRECIATION RESERVE',
3015                               'CIP COST', adj.source_type_code ||' COST',
3016                               adj.source_type_code ||' '|| adj.adjustment_type)
3017       and    userenv('LANG') = lk.language
3018       and    adj.je_header_id = gljh.je_header_id (+)
3019    UNION ALL
3020       select adj.adjustment_line_id,
3021              decode (adj.debit_credit_flag,
3022                      'CR', adj.adjustment_amount, null),
3023              decode (adj.debit_credit_flag,
3024                      'DR', adj.adjustment_amount, null),
3025              nvl(nvl(gljl.code_combination_id, da.deprn_reserve_account_ccid),
3026                  cb.reserve_account_ccid),
3027              glsob.currency_code,
3028              decode (adj.adjustment_type,
3029                 'BONUS EXPENSE', 'EXPENSE',
3030                 'BONUS RESERVE', 'ASSET',
3031                 'CIP COST', 'ASSET',
3032                 'COST', 'ASSET',
3033                 'COST CLEARING', 'ASSET',
3034                 'DEPRN ADJUST', 'EXPENSE',
3035                 'EXPENSE', 'ASSET',
3036                 'GRP COR RESERVE', 'ASSET',
3037                 'GRP PRC RESERVE', 'ASSET',
3038                 'INTERCO AP', 'LIABILITY',
3039                 'INTERCO AR', 'ASSET',
3040                 'NBV RETIRED', 'ASSET',
3041                 'PROCEEDS', 'ASSET',
3042                 'PROCEEDS CLR', 'ASSET',
3043                 'REMOVALCOST', 'ASSET',
3044                 'REMOVALCOST CLR', 'ASSET',
3045                 'RESERVE', 'ASSET',
3046                 'REVAL RESERVE', 'ASSET',
3047                 'REVAL RSV RET', 'ASSET',
3048                 'REVAL AMORT', 'EXPENSE',
3049                 'REVAL EXPENSE', 'EXPENSE',
3050                 'ASSET'),
3051              lk.description,
3052              decode (gljh.je_header_id, null,
3053                                         decode (bcp.gl_posting_allowed_flag, 'YES',
3054                                                                              'N', 'NT'),
3055                                         'Y'), -- Bug 12879380
3056              gljh.je_batch_id,
3057              dd.je_header_id,
3058              nvl(dd.deprn_reserve_je_line_num, 0),
3059              adj.distribution_id
3060       from   fa_mc_adjustments adj,
3061              gl_sets_of_books glsob,
3062              fa_mc_book_controls bc,
3063              fa_book_controls bcp,
3064              fa_lookups_tl lk,
3065              gl_je_headers gljh,
3066              fa_mc_deprn_detail dd,
3067              gl_je_lines gljl,
3068              fa_distribution_accounts da,
3069              fa_asset_history ah,
3070              fa_category_books cb
3071       where  bc.book_type_code = l_book_type_code
3072       and    bc.set_of_books_id = l_set_of_books_id
3073       and    bcp.book_type_code = l_book_type_code
3074       and    bc.enabled_flag = 'Y'
3075       and    bc.set_of_books_id = glsob.set_of_books_id
3076       and    bc.book_type_code = adj.book_type_code
3077       and    bc.set_of_books_id = adj.set_of_books_id
3078       and    adj.asset_id = l_asset_id
3079       and    adj.transaction_header_id = l_transaction_header_id
3080       and    adj.adjustment_type = 'EXPENSE'
3081       and    adj.source_type_code in ('DEPRECIATION', 'CIP RETIREMENT',
3082                                       'RETIREMENT')
3083       and    adj.code_combination_id is not null
3084       and    nvl(adj.track_member_flag, 'N') = 'N'
3085       and    lk.lookup_type = 'JOURNAL ENTRIES'
3086       and    lk.lookup_code = adj.source_type_code ||' RESERVE'
3087       and    userenv('LANG') = lk.language
3088       and    adj.asset_id = dd.asset_id (+)
3089       and    adj.book_type_code = dd.book_type_code (+)
3090       and    adj.set_of_books_id = dd.set_of_books_id (+)
3091       and    adj.distribution_id = dd.distribution_id (+)
3092       and    adj.period_counter_created = dd.period_counter (+)
3093       and    dd.je_header_id = gljl.je_header_id (+)
3094       and    dd.deprn_reserve_je_line_num = gljl.je_line_num (+)
3095       and    dd.je_header_id = gljh.je_header_id (+)
3096       and    adj.book_type_code = da.book_type_code (+)
3097       and    adj.distribution_id = da.distribution_id (+)
3098       and    adj.asset_id = ah.asset_id
3099       and    l_date_effective >= ah.date_effective
3100       and    l_date_effective < nvl(ah.date_ineffective, sysdate+1)
3101       and    ah.category_id = cb.category_id
3102       and    bc.book_type_code = cb.book_type_code
3103    UNION ALL
3104       select adj.adjustment_line_id,
3105              decode (adj.debit_credit_flag,
3106                      'DR', adj.adjustment_amount, null),
3107              decode (adj.debit_credit_flag,
3108                      'CR', adj.adjustment_amount, null),
3109              nvl(nvl(gljl.code_combination_id, da.deprn_reserve_account_ccid),
3110                  cb.reserve_account_ccid),
3111              glsob.currency_code,
3112              decode (adj.adjustment_type,
3113                 'BONUS EXPENSE', 'EXPENSE',
3114                 'BONUS RESERVE', 'ASSET',
3115                 'CIP COST', 'ASSET',
3116                 'COST', 'ASSET',
3117                 'COST CLEARING', 'ASSET',
3118                 'DEPRN ADJUST', 'EXPENSE',
3119                 'EXPENSE', 'EXPENSE',
3120                 'GRP COR RESERVE', 'ASSET',
3121                 'GRP PRC RESERVE', 'ASSET',
3122                 'INTERCO AP', 'LIABILITY',
3123                 'INTERCO AR', 'ASSET',
3124                 'NBV RETIRED', 'ASSET',
3125                 'PROCEEDS', 'ASSET',
3126                 'PROCEEDS CLR', 'ASSET',
3127                 'REMOVALCOST', 'ASSET',
3128                 'REMOVALCOST CLR', 'ASSET',
3129                 'RESERVE', 'ASSET',
3130                 'REVAL RESERVE', 'ASSET',
3131                 'REVAL RSV RET', 'ASSET',
3132                 'REVAL AMORT', 'EXPENSE',
3133                 'REVAL EXPENSE', 'EXPENSE',
3134                 'ASSET'),
3135              lk.description,
3136              decode (gljh.je_header_id, null,
3137                                         decode (bcp.gl_posting_allowed_flag, 'YES',
3138                                                                              'N', 'NT'),
3139                                         'Y'), -- Bug 12879380
3140              gljh.je_batch_id,
3141              dd.je_header_id,
3142              nvl(dd.deprn_reserve_je_line_num, 0),
3143              adj.distribution_id
3144       from   fa_mc_adjustments adj,
3145              gl_sets_of_books glsob,
3146              fa_mc_book_controls bc,
3147              fa_book_controls bcp,
3148              fa_lookups_tl lk,
3149              gl_je_headers gljh,
3150              fa_mc_deprn_detail dd,
3151              gl_je_lines gljl,
3152              fa_distribution_accounts da,
3153              fa_asset_history ah,
3154              fa_category_books cb
3155       where  bc.book_type_code = l_book_type_code
3156       and    bc.set_of_books_id = l_set_of_books_id
3157       and    bcp.book_type_code = l_book_type_code
3158       and    bc.enabled_flag = 'Y'
3159       and    bc.set_of_books_id = glsob.set_of_books_id
3160       and    bc.book_type_code = adj.book_type_code
3161       and    bc.set_of_books_id = adj.set_of_books_id
3162       and    adj.asset_id = l_asset_id
3163       and    adj.transaction_header_id = l_transaction_header_id
3164       and    adj.adjustment_type = 'BONUS EXPENSE'
3165       and    adj.source_type_code = 'DEPRECIATION'
3166       and    adj.code_combination_id is not null
3167       and    nvl(adj.track_member_flag, 'N') = 'N'
3168       and    lk.lookup_type = 'JOURNAL ENTRIES'
3169       and    lk.lookup_code = 'BONUS DEPRECIATION RESERVE'
3170       and    userenv('LANG') = lk.language
3171       and    adj.asset_id = dd.asset_id (+)
3172       and    adj.book_type_code = dd.book_type_code (+)
3173       and    adj.set_of_books_id = dd.set_of_books_id (+)
3174       and    adj.distribution_id = dd.distribution_id (+)
3175       and    adj.period_counter_created = dd.period_counter (+)
3176       and    dd.je_header_id = gljl.je_header_id (+)
3177       and    dd.bonus_deprn_rsv_je_line_num = gljl.je_line_num (+)
3178       and    dd.je_header_id = gljh.je_header_id (+)
3179       and    adj.book_type_code = da.book_type_code (+)
3180       and    adj.distribution_id = da.distribution_id (+)
3181       and    adj.asset_id = ah.asset_id
3182       and    l_date_effective >= ah.date_effective
3183       and    l_date_effective < nvl(ah.date_ineffective, sysdate+1)
3184       and    ah.category_id = cb.category_id
3185       and    bc.book_type_code = cb.book_type_code;
3186 
3187 BEGIN
3188 
3189    x_success_count := 0;
3190    x_failure_count := 0;
3191 
3192    l_batch_size := nvl(nvl(p_batch_size, fa_cache_pkg.fa_batch_size), 1000);
3193 
3194    open c_trans;
3195    loop
3196 
3197          fetch c_trans bulk collect
3198           into l_asset_id_tbl,
3199                l_book_type_code_tbl,
3200                l_primary_set_of_books_id_tbl,
3201                l_org_id_tbl,
3202                l_transaction_type_code_tbl,
3203                l_transaction_date_entered_tbl,
3204                l_transaction_header_id_tbl,
3205                l_rowid_tbl,
3206                l_event_class_code_tbl,
3207                l_period_name_tbl,
3208                l_period_counter_tbl,
3209                l_cal_period_close_date_tbl,
3210                l_member_thid_tbl,
3211                l_hdr_desc_tbl,
3212                l_je_category_name_tbl,
3213                l_date_effective_tbl
3214                limit l_batch_size;
3215 
3216       if (l_event_id_tbl.count = 0) then exit; end if;
3217 
3218       -- Select upg_batch_id
3219       select xla_upg_batches_s.nextval
3220       into   l_upg_batch_id
3221       from   dual;
3222 
3223       FOR i IN 1..l_rowid_tbl.count LOOP
3224          select xla_events_s.nextval, xla_transaction_entities_s.nextval
3225          into   l_event_id_tbl(i), l_entity_id_tbl(i)
3226          from   dual;
3227       END LOOP;
3228 
3229       -- Update table with event_id
3230       FORALL l_count IN 1..l_event_id_tbl.count
3231          update fa_transaction_headers th
3232          set    th.event_id = l_event_id_tbl(l_count)
3233          where  th.rowid = l_rowid_tbl(l_count);
3234 
3235       l_rows_processed := l_event_id_tbl.count;
3236 
3237       FORALL l_count IN 1..l_event_id_tbl.count
3238          update fa_transaction_headers th
3239          set    th.event_id = l_event_id_tbl(l_count)
3240          where  th.transaction_header_id = l_member_thid_tbl(l_count);
3241 
3242       -- Business Rules for xla_transaction_entities
3243       -- * ledger_id is the same as set_of_books_id
3244       -- * legal_entity_id is null
3245       -- * entity_code can be TRANSACTIONS or DEPRECIATION
3246       -- * for TRANSACTIONS:
3247       --       source_id_int_1 is transaction_header_id
3248       --       transaction_number is transaction_header_id
3249       -- * for DEPRECIATION:
3250       --       source_id_int is asset_id
3251       --       source_id_int_2 is period_counter
3252       --       source_id_int_3 is deprn_run_id
3253       --       transaction_number is set_of_books_id
3254       -- * source_char_id_1 is book_type_code
3255       -- * valuation_method is book_type_code
3256 
3257       FORALL i IN 1..l_event_id_tbl.count
3258          INSERT INTO xla_transaction_entities_upg (
3259             upg_batch_id,
3260             application_id,
3261             ledger_id,
3262             legal_entity_id,
3263             entity_code,
3264             source_id_int_1,
3265             source_id_int_2,
3266             source_id_int_3,
3267             source_id_int_4,
3268             source_id_char_1,
3269             source_id_char_2,
3270             source_id_char_3,
3271             source_id_char_4,
3272             security_id_int_1,
3273             security_id_int_2,
3274             security_id_int_3,
3275             security_id_char_1,
3276             security_id_char_2,
3277             security_id_char_3,
3278             transaction_number,
3279             valuation_method,
3280             source_application_id,
3281             creation_date,
3282             created_by,
3283             last_update_date,
3284             last_updated_by,
3285             last_update_login,
3286             entity_id,
3287             upg_source_application_id
3288          ) values (
3289             l_upg_batch_id,                        -- upg_batch_id
3290             c_application_id,                      -- application_id
3291             l_primary_set_of_books_id_tbl(i),      -- ledger_id
3292             null,                                  -- legal_entity_id,
3293             c_entity_code,                         -- entity_code
3294             l_transaction_header_id_tbl(i),        -- source_id_int_1
3295             null,                                  -- source_id_int_2
3296             null,                                  -- source_id_int_3
3297             null,                                  -- source_id_int_4
3298             l_book_type_code_tbl(i),               -- source_id_char_1  -- Bug 8239360
3299             null,                                  -- source_id_char_2
3300             null,                                  -- source_id_char_3
3301             null,                                  -- source_id_char_4
3302             null,                                  -- security_id_int_1
3303             null,                                  -- security_id_int_2
3304             null,                                  -- security_id_int_3
3305             null,                                  -- security_id_char_1
3306             null,                                  -- security_id_char_2
3307             null,                                  -- security_id_char_3
3308             l_transaction_header_id_tbl(i),        -- transaction number
3309             l_book_type_code_tbl(i),               -- valuation_method
3310             c_application_id,                      -- source_application_id
3311             sysdate,                               -- creation_date
3312             c_fnd_user,                            -- created_by
3313             sysdate,                               -- last_update_date
3314             c_upgrade_bugno,                       -- last_update_by
3315             c_upgrade_bugno,                       -- last_update_login
3316             l_entity_id_tbl(i),                    -- entity_id
3317             c_application_id                       -- upg_source_application_id
3318          );
3319 
3320       -- Business Rules for xla_events
3321       -- * event_type_code is similar to transaction_type_code
3322       -- * event_number is 1, but is the serial event for chronological order
3323       -- * event_status_code: N if event creates no journal entries
3324       --                      P if event would ultimately yield journals
3325       --                      I if event is not ready to be processed
3326       --                      U never use this value for upgrade
3327       -- * process_status_code: E if error and journals not yet created
3328       --                        P if processed and journals already generated
3329       --                        U if unprocessed and journals not generated
3330       --                        D only used for Global Accounting Engine
3331       --                        I do not use for upgrade
3332       -- * on_hold_flag: N should always be this value for upgraded entries
3333       -- * event_date is basically transaction_date_entered
3334 
3335       FORALL i IN 1..l_event_id_tbl.count
3336          insert into xla_events (
3337             upg_batch_id,
3338             application_id,
3339             event_type_code,
3340             event_number,
3341             event_status_code,
3342             process_status_code,
3343             on_hold_flag,
3344             event_date,
3345             creation_date,
3346             created_by,
3347             last_update_date,
3348             last_updated_by,
3349             last_update_login,
3350             program_update_date,
3351             program_id,
3352             program_application_id,
3353             request_id,
3354             entity_id,
3355             event_id,
3356             upg_source_application_id,
3357             transaction_date
3358          ) values (
3359             l_upg_batch_id,                          -- upg_batch_id
3360             c_application_id,                        -- application_id
3361             l_event_class_code_tbl(i),               -- event_type_code
3362             '1',                                     -- event_number
3363             'P',                                     -- event_status_code
3364             'P',                                     -- process_status_code
3365             'N',                                     -- on_hold_flag
3366             l_transaction_date_entered_tbl(i),       -- event_date
3367             sysdate,                                 -- creation_date
3368             c_fnd_user,                              -- created_by
3369             sysdate,                                 -- last_update_date
3370             c_upgrade_bugno,                         -- last_update_by
3371             c_upgrade_bugno,                         -- last_update_login
3372             null,                                    -- program_update_date
3373             null,                                    -- program_id
3374             null,                                    -- program_application_id
3375             null,                                    -- program_update_date
3376             l_entity_id_tbl(i),                      -- entity_id
3377             l_event_id_tbl(i),                       -- event_id
3378             c_application_id,                        -- upg_source_appl_id
3379             l_transaction_date_entered_tbl(i)        -- transaction_date
3380          );
3381 
3382       FOR i IN 1..l_event_id_tbl.count LOOP
3383 
3384             open c_adj (l_book_type_code_tbl(i),
3385                         l_asset_id_tbl(i),
3386                         l_transaction_header_id_tbl(i),
3387                         l_date_effective_tbl(i));
3388             fetch c_adj bulk collect
3389              into l_adj_line_id_tbl,
3390                   l_debit_amount_tbl,
3391                   l_credit_amount_tbl,
3392                   l_ccid_tbl,
3393                   l_currency_code_tbl,
3394                   l_acct_class_code_tbl,
3395                   l_line_desc_tbl,
3396                   l_gl_transfer_status_code_tbl,
3397                   l_je_batch_id_tbl,
3398                   l_je_header_id_tbl,
3399                   l_je_line_num_tbl,
3400                   l_distribution_id_tbl;
3401             close c_adj;
3402 
3403             FOR j IN 1..l_adj_line_id_tbl.count LOOP
3404                l_ae_line_num_tbl(j) := j;
3405 
3406                select xla_gl_sl_link_id_s.nextval
3407                into   l_xla_gl_sl_link_id_tbl(j)
3408                from   dual;
3409             END LOOP;
3410 
3411             select xla_ae_headers_s.nextval
3412             into   l_ae_header_id
3413             from   dual;
3414 
3415       -- Business Rules for xla_ae_headers
3416       -- * amb_context_code is DEFAULT
3417       -- * reference_date must be null
3418       -- * balance_type_code:
3419       --     A: Actual
3420       --     B: Budget
3421       --     E: Encumbrance
3422       -- * gl_transfer_status_code:
3423       --     Y: already transferred to GL
3424       --     N: not transferred to GL
3425       -- * gl_transfer_date is date entry transferred to GL
3426       -- * accounting_entry_status_code must be F
3427       -- * accounting_entry_type_code must be STANDARD
3428       -- * product_rule_* not relevant for upgrade
3429 
3430          insert into xla_ae_headers (
3431             upg_batch_id,
3432             application_id,
3433             amb_context_code,
3434             entity_id,
3435             event_id,
3436             event_type_code,
3437             ae_header_id,
3438             ledger_id,
3439             accounting_date,
3440             period_name,
3441             reference_date,
3442             balance_type_code,
3443             je_category_name,
3444             gl_transfer_status_code,
3445             gl_transfer_date,
3446             accounting_entry_status_code,
3447             accounting_entry_type_code,
3448             description,
3449             budget_version_id,
3450             funds_status_code,
3451             encumbrance_type_id,
3452             completed_date,
3453             doc_sequence_id,
3454             doc_sequence_value,
3455             doc_category_code,
3456             packet_id,
3457             group_id,
3458             creation_date,
3459             created_by,
3460             last_update_date,
3461             last_updated_by,
3462             last_update_login,
3463             program_id,
3464             program_application_id,
3465             program_update_date,
3466             request_id,
3467             close_acct_seq_assign_id,
3468             close_acct_seq_version_id,
3469             close_acct_seq_value,
3470             completion_acct_seq_assign_id,
3471             completion_acct_seq_version_id,
3472             completion_acct_seq_value,
3473             accounting_batch_id,
3474             product_rule_type_code,
3475             product_rule_code,
3476             product_rule_version,
3477             upg_source_application_id,
3478             upg_valid_flag
3479          ) values (
3480             l_upg_batch_id,                     -- upg_batch_id
3481             c_application_id,                   -- application_id
3482             c_amb_context_code,                 -- amb_context_code
3483             l_entity_id_tbl(i),                 -- entity_id
3484             l_event_id_tbl(i),                  -- event_id,
3485             l_event_class_code_tbl(i),          -- event_type_code
3486             l_ae_header_id,                     -- ae_header_id,
3487             l_primary_set_of_books_id_tbl(i),   -- ledger_id/sob_id
3488             l_cal_period_close_date_tbl(i),     -- accounting_date,
3489             l_period_name_tbl(i),               -- period_name,
3490             null,                               -- reference_date
3491             'A',                                -- balance_type_code,
3492             l_je_category_name_tbl(i),          -- je_category_name
3493             'Y',                                -- gl_transfer_status_code
3494             null,                               -- gl_transfer_date
3495             'F',                                -- accounting_entry_status_code
3496             'STANDARD',                         -- accounting_entry_type_code
3497             l_hdr_desc_tbl(i),                  -- description
3498             null,                               -- budget_version_id
3499             null,                               -- funds_status_code
3500             null,                               -- encumbrance_type_id
3501             null,                               -- completed_date
3502             null,                               -- doc_sequence_id
3503             null,                               -- doc_sequence_value
3504             null,                               -- doc_category_code
3505             null,                               -- packet_id,
3506             null,                               -- group_id
3507             sysdate,                            -- creation_date
3508             c_fnd_user,                         -- created_by
3509             sysdate,                            -- last_update_date
3510             c_fnd_user,                         -- last_updated_by
3511             c_upgrade_bugno,                    -- last_update_login
3512             null,                               -- program_id
3513             c_application_id,                   -- program_application_id
3514             sysdate,                            -- program_update_date
3515             null,                               -- request_id
3516             null,                               -- close_acct_seq_assign_id
3517             null,                               -- close_acct_seq_version_id
3518             null,                               -- close_acct_seq_value
3519             null,                               -- compl_acct_seq_assign_id
3520             null,                               -- compl_acct_seq_version_id
3521             null,                               -- compl_acct_seq_value
3522             null,                               -- accounting_batch_id
3523             null,                               -- product_rule_type_code
3524             null,                               -- product_rule_code
3525             null,                               -- product_rule_version
3526             c_application_id,                   -- upg_souce_application_id
3527             null                                -- upg_valid_flag
3528          );
3529 
3530             -- Business Rules for xla_ae_lines
3531             -- * gl_transfer_mode_code:
3532             --       D: Detailed mode when transferred to GL
3533             --       S: Summary mode when transferred to GL
3534             -- * gl_sl_link_table must be XLAJEL
3535             -- * currency_conversion_* needs to be populated only if
3536             --   different from ledger currency
3537 
3538             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
3539             insert into xla_ae_lines (
3540                   upg_batch_id,
3541                   ae_header_id,
3542                   ae_line_num,
3543                   displayed_line_number,
3544                   application_id,
3545                   code_combination_id,
3546                   gl_transfer_mode_code,
3547                   accounted_dr,
3548                   accounted_cr,
3549                   currency_code,
3550                   currency_conversion_date,
3551                   currency_conversion_rate,
3552                   currency_conversion_type,
3553                   entered_dr,
3554                   entered_cr,
3555                   description,
3556                   accounting_class_code,
3557                   gl_sl_link_id,
3558                   gl_sl_link_table,
3559                   party_type_code,
3560                   party_id,
3561                   party_site_id,
3562                   statistical_amount,
3563                   ussgl_transaction_code,
3564                   jgzz_recon_ref,
3565                   control_balance_flag,
3566                   analytical_balance_flag,
3567                   upg_tax_reference_id1,
3568                   upg_tax_reference_id2,
3569                   upg_tax_reference_id3,
3570                   creation_date,
3571                   created_by,
3572                   last_update_date,
3573                   last_updated_by,
3574                   last_update_login,
3575                   program_update_date,
3576                   program_id,
3577                   program_application_id,
3578                   request_id,
3579                   gain_or_loss_flag,
3580                   accounting_date,
3581                   ledger_id
3582             ) values (
3583                   l_upg_batch_id,                 -- upg_batch_id
3584                   l_ae_header_id,                 -- ae_header_id
3585                   l_ae_line_num_tbl(j),           -- ae_line_num
3586                   l_ae_line_num_tbl(j),           -- displayed_line_num
3587                   c_application_id,               -- application_id
3588                   l_ccid_tbl(j),                  -- code_combination_id
3589                   'S',                            -- gl_transfer_mode_code
3590                   l_debit_amount_tbl(j),          -- accounted_dr
3591                   l_credit_amount_tbl(j),         -- accounted_cr
3592                   l_currency_code_tbl(j),         -- currency_code
3593                   null,                           -- currency_conversion_date
3594                   null,                           -- currency_conversion_rate
3595                   null,                           -- currency_conversion_type
3596                   l_debit_amount_tbl(j),          -- entered_dr
3597                   l_credit_amount_tbl(j),         -- entered_cr
3598                   l_line_desc_tbl(j) || ' - ' ||
3599                      to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
3600                                                   -- description
3601                   l_acct_class_code_tbl(j),       -- accounting_class_code
3602                   l_xla_gl_sl_link_id_tbl(j),     -- gl_sl_link_id
3603                   'XLAJEL',                       -- gl_sl_link_table
3604                   null,                           -- party_type_code
3605                   null,                           -- party_id
3606                   null,                           -- party_site_id
3607                   null,                           -- statistical_amount
3608                   null,                           -- ussgl_transaction_code
3609                   null,                           -- glzz_recon_ref
3610                   null,                           -- control_balance_flag
3611                   null,                           -- analytical_balance_flag
3612                   null,                           -- upg_tax_reference_id1
3613                   null,                           -- upg_tax_reference_id2
3614                   null,                           -- upg_tax_reference_id3
3615                   sysdate,                        -- creation_date
3616                   c_fnd_user,                     -- created_by
3617                   sysdate,                        -- last_update_date
3618                   c_fnd_user,                     -- last_updated_by
3619                   c_upgrade_bugno,                -- last_update_login
3620                   null,                           -- program_update_date
3621                   null,                           -- program_id
3622                   c_application_id,               -- program_application_id
3623                   null,                           -- request_id
3624                   'N',                            -- gain_or_loss_flag
3625                   l_cal_period_close_date_tbl(i), -- accounting_date,
3626                   l_primary_set_of_books_id_tbl(i)
3627                                                   -- ledger_id/sob_id
3628             );
3629 
3630             -- Business Rules for xla_distribution_links
3631             -- * accounting_line_code is similar to adjustment_type
3632             -- * accounting_line_type_code is S
3633             -- * merge_duplicate_code is N
3634             -- * source_distribution_type is TRX
3635             -- * source_distribution_id_num_1 is transaction_header_id
3636             -- * source_distribution_id_num_2 is event_id
3637 
3638             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
3639             insert into xla_distribution_links (
3640                   upg_batch_id,
3641                   application_id,
3642                   event_id,
3643                   ae_header_id,
3644                   ae_line_num,
3645                   accounting_line_code,
3646                   accounting_line_type_code,
3647                   source_distribution_type,
3648                   source_distribution_id_char_1,
3649                   source_distribution_id_char_2,
3650                   source_distribution_id_char_3,
3651                   source_distribution_id_char_4,
3652                   source_distribution_id_char_5,
3653                   source_distribution_id_num_1,
3654                   source_distribution_id_num_2,
3655                   source_distribution_id_num_3,
3656                   source_distribution_id_num_4,
3657                   source_distribution_id_num_5,
3658                   merge_duplicate_code,
3659                   statistical_amount,
3660                   unrounded_entered_dr,
3661                   unrounded_entered_cr,
3662                   unrounded_accounted_dr,
3663                   unrounded_accounted_cr,
3664                   ref_ae_header_id,
3665                   ref_temp_line_num,
3666                   ref_event_id,
3667                   temp_line_num,
3668                   tax_line_ref_id,
3669                   tax_summary_line_ref_id,
3670                   tax_rec_nrec_dist_ref_id,
3671                   line_definition_owner_code,
3672                   line_definition_code,
3673                   event_class_code,
3674                   event_type_code
3675                ) values (
3676                   l_upg_batch_id,              -- upg_batch_id
3677                   c_application_id,            -- application_id
3678                   l_event_id_tbl(i),           -- event_id
3679                   l_ae_header_id,              -- ae_header_id
3680                   l_ae_line_num_tbl(j),        -- ae_line_num
3681                   null,                        -- accounting_line_code
3682                   'S',                         -- accounting_line_type_code
3683                   'TRX',                       -- source_distribution_type
3684                   null,                        -- source_distribution_id_char_1
3685                   null,                        -- source_distribution_id_char_2
3686                   null,                        -- source_distribution_id_char_3
3687                   null,                        -- source_distribution_id_char_4
3688                   null,                        -- source_distribution_id_char_5
3689                   l_transaction_header_id_tbl(i),
3690                                                -- source_distribution_id_num_1
3691                   l_adj_line_id_tbl(j),        -- source_distribution_id_num_2
3692                   null,                        -- source_distribution_id_num_3
3693                   null,                        -- source_distribution_id_num_4
3694                   null,                        -- source_distribution_id_num_5
3695                   'N',                         -- merge_duplicate_code
3696                   null,                        -- statistical_amount
3697                   l_debit_amount_tbl(j),       -- unrounded_entered_dr
3698                   l_credit_amount_tbl(j),      -- unrounded_entered_cr
3699                   l_debit_amount_tbl(j),       -- unrounded_accounted_dr
3700                   l_credit_amount_tbl(j),      -- unrounded_accounted_cr
3701                   l_ae_header_id,              -- ref_ae_header_id
3702                   null,                        -- ref_temp_line_num
3703                   null,                        -- ref_event_id
3704                   l_ae_line_num_tbl(j),        -- temp_line_num
3705                   null,                        -- tax_line_ref_id
3706                   null,                        -- tax_summary_line_ref_id
3707                   null,                        -- tax_rec_nrec_dist_ref_id
3708                   null,                        -- line_definition_owner_code
3709                   null,                        -- line_definition_code
3710                   l_event_class_code_tbl(i),   -- event_class_code
3711                   l_event_class_code_tbl(i)    -- event_type_code
3712                );
3713 
3714             for j IN 1..l_xla_gl_sl_link_id_tbl.count loop
3715                if (l_je_batch_id_tbl(j) is not null) then
3716                   insert into gl_import_references (
3717                      je_batch_id,
3718                      je_header_id,
3719                      je_line_num,
3720                      last_update_date,
3721                      last_updated_by,
3722                      creation_date,
3723                      created_by,
3724                      last_update_login,
3725                      reference_1,
3726                      reference_2,
3727                      reference_3,
3728                      reference_4,
3729                      reference_5,
3730                      reference_6,
3731                      reference_7,
3732                      reference_8,
3733                      reference_9,
3734                      reference_10,
3735                      subledger_doc_sequence_id,
3736                      subledger_doc_sequence_value,
3737                      gl_sl_link_id,
3738                      gl_sl_link_table
3739                   ) values (
3740                      l_je_batch_id_tbl(j),        -- je_batch_id
3741                      l_je_header_id_tbl(j),       -- je_header_id
3742                      l_je_line_num_tbl(j),        -- je_line_num
3743                      sysdate,                     -- last_update_date
3744                      c_fnd_user,                  -- last_updated_by
3745                      sysdate,                     -- creation_date
3746                      c_fnd_user,                  -- created_by
3747                      c_upgrade_bugno,             -- last_update_login
3748                      to_char(l_transaction_header_id_tbl(i)),
3749                                                   -- reference_1
3750                      to_char(l_asset_id_tbl(i)),  -- reference_2
3751                      to_char(l_distribution_id_tbl(j)),
3752                                                   -- reference_3
3753                      to_char(l_adj_line_id_tbl(j)),
3754                                                   -- reference_4
3755                      l_book_type_code_tbl(i),     -- reference_5
3756                      to_char(l_period_counter_tbl(i)),
3757                                                   -- reference_6
3758                      null,                        -- reference_7
3759                      null,                        -- reference_8
3760                      null,                        -- reference_9
3761                      null,                        -- reference_10
3762                      null,                        -- subledger_doc_seq_id
3763                      null,                        -- subledger_doc_seq_value
3764                      l_xla_gl_sl_link_id_tbl(j),  -- gl_sl_link_id
3765                      'XLAJEL'                     -- gl_sl_link_table
3766                   );
3767                end if;
3768             end loop;
3769 
3770             l_adj_line_id_tbl.delete;
3771             l_xla_gl_sl_link_id_tbl.delete;
3772             l_ae_line_num_tbl.delete;
3773             l_debit_amount_tbl.delete;
3774             l_credit_amount_tbl.delete;
3775             l_ccid_tbl.delete;
3776             l_currency_code_tbl.delete;
3777             l_acct_class_code_tbl.delete;
3778             l_line_def_owner_code_tbl.delete;
3779             l_line_def_code_tbl.delete;
3780             l_line_desc_tbl.delete;
3781             l_gl_transfer_status_code_tbl.delete;
3782             l_je_batch_id_tbl.delete;
3783             l_je_header_id_tbl.delete;
3784             l_je_line_num_tbl.delete;
3785             l_distribution_id_tbl.delete;
3786 
3787             open c_mc_books (l_book_type_code_tbl(i));
3788             fetch c_mc_books bulk collect
3789              into l_rep_set_of_books_id_tbl;
3790             close c_mc_books;
3791 
3792            for k IN 1..l_rep_set_of_books_id_tbl.count loop
3793 
3794             open c_mc_adj (l_book_type_code_tbl(i),
3795                         l_asset_id_tbl(i),
3796                         l_rep_set_of_books_id_tbl(k),
3797                         l_transaction_header_id_tbl(i),
3798                         l_date_effective_tbl(i));
3799             fetch c_mc_adj bulk collect
3800              into l_adj_line_id_tbl,
3801                   l_debit_amount_tbl,
3802                   l_credit_amount_tbl,
3803                   l_ccid_tbl,
3804                   l_currency_code_tbl,
3805                   l_acct_class_code_tbl,
3806                   l_line_desc_tbl,
3807                   l_gl_transfer_status_code_tbl,
3808                   l_je_batch_id_tbl,
3809                   l_je_header_id_tbl,
3810                   l_je_line_num_tbl,
3811                   l_distribution_id_tbl;
3812            close c_mc_adj;
3813 
3814             FOR j IN 1..l_adj_line_id_tbl.count LOOP
3815                l_ae_line_num_tbl(j) := j;
3816 
3817                select xla_gl_sl_link_id_s.nextval
3818                into   l_xla_gl_sl_link_id_tbl(j)
3819                from   dual;
3820             END LOOP;
3821 
3822             select xla_ae_headers_s.nextval
3823             into   l_ae_header_id
3824             from   dual;
3825 
3826       -- Business Rules for xla_ae_headers
3827       -- * amb_context_code is DEFAULT
3828       -- * reference_date must be null
3829       -- * balance_type_code:
3830       --     A: Actual
3831       --     B: Budget
3832       --     E: Encumbrance
3833       -- * gl_transfer_status_code:
3834       --     Y: already transferred to GL
3835       --     N: not transferred to GL
3836       -- * gl_transfer_date is date entry transferred to GL
3837       -- * accounting_entry_status_code must be F
3838       -- * accounting_entry_type_code must be STANDARD
3839       -- * product_rule_* not relevant for upgrade
3840 
3841          insert into xla_ae_headers (
3842             upg_batch_id,
3843             application_id,
3844             amb_context_code,
3845             entity_id,
3846             event_id,
3847             event_type_code,
3848             ae_header_id,
3849             ledger_id,
3850             accounting_date,
3851             period_name,
3852             reference_date,
3853             balance_type_code,
3854             je_category_name,
3855             gl_transfer_status_code,
3856             gl_transfer_date,
3857             accounting_entry_status_code,
3858             accounting_entry_type_code,
3859             description,
3860             budget_version_id,
3861             funds_status_code,
3862             encumbrance_type_id,
3863             completed_date,
3864             doc_sequence_id,
3865             doc_sequence_value,
3866             doc_category_code,
3867             packet_id,
3868             group_id,
3869             creation_date,
3870             created_by,
3871             last_update_date,
3872             last_updated_by,
3873             last_update_login,
3874             program_id,
3875             program_application_id,
3876             program_update_date,
3877             request_id,
3878             close_acct_seq_assign_id,
3879             close_acct_seq_version_id,
3880             close_acct_seq_value,
3881             completion_acct_seq_assign_id,
3882             completion_acct_seq_version_id,
3883             completion_acct_seq_value,
3884             accounting_batch_id,
3885             product_rule_type_code,
3886             product_rule_code,
3887             product_rule_version,
3888             upg_source_application_id,
3889             upg_valid_flag
3890          ) values (
3891             l_upg_batch_id,                     -- upg_batch_id
3892             c_application_id,                   -- application_id
3893             c_amb_context_code,                 -- amb_context_code
3894             l_entity_id_tbl(i),                 -- entity_id
3895             l_event_id_tbl(i),                  -- event_id,
3896             l_event_class_code_tbl(i),          -- event_type_code
3897             l_ae_header_id,                     -- ae_header_id,
3898             l_rep_set_of_books_id_tbl(k),       -- ledger_id/sob_id
3899             l_cal_period_close_date_tbl(i),     -- accounting_date,
3900             l_period_name_tbl(i),               -- period_name,
3901             null,                               -- reference_date
3902             'A',                                -- balance_type_code,
3903             l_je_category_name_tbl(i),          -- je_category_name
3904             'Y',                                -- gl_transfer_status_code
3905             null,                               -- gl_transfer_date
3906             'F',                                -- accounting_entry_status_code
3907             'STANDARD',                         -- accounting_entry_type_code
3908             l_hdr_desc_tbl(i),                  -- description
3909             null,                               -- budget_version_id
3910             null,                               -- funds_status_code
3911             null,                               -- encumbrance_type_id
3912             null,                               -- completed_date
3913 
3914             null,                               -- doc_sequence_id
3915             null,                               -- doc_sequence_value
3916             null,                               -- doc_category_code
3917             null,                               -- packet_id,
3918             null,                               -- group_id
3919             sysdate,                            -- creation_date
3920             c_fnd_user,                         -- created_by
3921             sysdate,                            -- last_update_date
3922             c_fnd_user,                         -- last_updated_by
3923             c_upgrade_bugno,                    -- last_update_login
3924             null,                               -- program_id
3925             c_application_id,                   -- program_application_id
3926             sysdate,                            -- program_update_date
3927             null,                               -- request_id
3928             null,                               -- close_acct_seq_assign_id
3929             null,                               -- close_acct_seq_version_id
3930             null,                               -- close_acct_seq_value
3931             null,                               -- compl_acct_seq_assign_id
3932             null,                               -- compl_acct_seq_version_id
3933             null,                               -- compl_acct_seq_value
3934             null,                               -- accounting_batch_id
3935             null,                               -- product_rule_type_code
3936             null,                               -- product_rule_code
3937             null,                               -- product_rule_version
3938             c_application_id,                   -- upg_souce_application_id
3939             null                                -- upg_valid_flag
3940          );
3941 
3942             -- Business Rules for xla_ae_lines
3943             -- * gl_transfer_mode_code:
3944             --       D: Detailed mode when transferred to GL
3945             --       S: Summary mode when transferred to GL
3946             -- * gl_sl_link_table must be XLAJEL
3947             -- * currency_conversion_* needs to be populated only if
3948             --   different from ledger currency
3949 
3950             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
3951             insert into xla_ae_lines (
3952                   upg_batch_id,
3953                   ae_header_id,
3954                   ae_line_num,
3955                   displayed_line_number,
3956                   application_id,
3957                   code_combination_id,
3958                   gl_transfer_mode_code,
3959                   accounted_dr,
3960                   accounted_cr,
3961                   currency_code,
3962                   currency_conversion_date,
3963                   currency_conversion_rate,
3964                   currency_conversion_type,
3965                   entered_dr,
3966                   entered_cr,
3967                   description,
3968                   accounting_class_code,
3969                   gl_sl_link_id,
3970                   gl_sl_link_table,
3971                   party_type_code,
3972                   party_id,
3973                   party_site_id,
3974                   statistical_amount,
3975                   ussgl_transaction_code,
3976                   jgzz_recon_ref,
3977                   control_balance_flag,
3978                   analytical_balance_flag,
3979                   upg_tax_reference_id1,
3980                   upg_tax_reference_id2,
3981                   upg_tax_reference_id3,
3982                   creation_date,
3983                   created_by,
3984                   last_update_date,
3985                   last_updated_by,
3986                   last_update_login,
3987                   program_update_date,
3988                   program_id,
3989                   program_application_id,
3990                   request_id,
3991                   gain_or_loss_flag,
3992                   accounting_date,
3993                   ledger_id
3994             ) values (
3995                   l_upg_batch_id,                 -- upg_batch_id
3996                   l_ae_header_id,                 -- ae_header_id
3997                   l_ae_line_num_tbl(j),           -- ae_line_num
3998                   l_ae_line_num_tbl(j),           -- displayed_line_num
3999                   c_application_id,               -- application_id
4000                   l_ccid_tbl(j),                  -- code_combination_id
4001                   'S',                            -- gl_transfer_mode_code
4002                   l_debit_amount_tbl(j),          -- accounted_dr
4003                   l_credit_amount_tbl(j),         -- accounted_cr
4004                   l_currency_code_tbl(j),         -- currency_code
4005                   null,                           -- currency_conversion_date
4006                   null,                           -- currency_conversion_rate
4007                   null,                           -- currency_conversion_type
4008                   l_debit_amount_tbl(j),          -- entered_dr
4009                   l_credit_amount_tbl(j),         -- entered_cr
4010                   l_line_desc_tbl(j) || ' - ' ||
4011                      to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
4012                                                   -- description
4013                   l_acct_class_code_tbl(j),       -- accounting_class_code
4014                   l_xla_gl_sl_link_id_tbl(j),     -- gl_sl_link_id
4015                   'XLAJEL',                       -- gl_sl_link_table
4016                   null,                           -- party_type_code
4017                   null,                           -- party_id
4018                   null,                           -- party_site_id
4019                   null,                           -- statistical_amount
4020                   null,                           -- ussgl_transaction_code
4021                   null,                           -- glzz_recon_ref
4022                   null,                           -- control_balance_flag
4023                   null,                           -- analytical_balance_flag
4024                   null,                           -- upg_tax_reference_id1
4025                   null,                           -- upg_tax_reference_id2
4026                   null,                           -- upg_tax_reference_id3
4027                   sysdate,                        -- creation_date
4028                   c_fnd_user,                     -- created_by
4029                   sysdate,                        -- last_update_date
4030                   c_fnd_user,                     -- last_updated_by
4031                   c_upgrade_bugno,                -- last_update_login
4032                   null,                           -- program_update_date
4033                   null,                           -- program_id
4034                   c_application_id,               -- program_application_id
4035                   null,                           -- request_id
4036                   'N',                            -- gain_or_loss_flag
4037                   l_cal_period_close_date_tbl(i), -- accounting_date,
4038                   l_rep_set_of_books_id_tbl(k)    -- ledger_id/sob_id
4039             );
4040 
4041             -- Business Rules for xla_distribution_links
4042             -- * accounting_line_code is similar to adjustment_type
4043             -- * accounting_line_type_code is S
4044             -- * merge_duplicate_code is N
4045             -- * source_distribution_type is TRX
4046             -- * source_distribution_id_num_1 is transaction_header_id
4047             -- * source_distribution_id_num_2 is event_id
4048 
4049             forall j IN 1..l_xla_gl_sl_link_id_tbl.count
4050             insert into xla_distribution_links (
4051                   upg_batch_id,
4052                   application_id,
4053                   event_id,
4054                   ae_header_id,
4055                   ae_line_num,
4056                   accounting_line_code,
4057                   accounting_line_type_code,
4058                   source_distribution_type,
4059                   source_distribution_id_char_1,
4060                   source_distribution_id_char_2,
4061                   source_distribution_id_char_3,
4062                   source_distribution_id_char_4,
4063                   source_distribution_id_char_5,
4064                   source_distribution_id_num_1,
4065                   source_distribution_id_num_2,
4066                   source_distribution_id_num_3,
4067                   source_distribution_id_num_4,
4068                   source_distribution_id_num_5,
4069                   merge_duplicate_code,
4070                   statistical_amount,
4071                   unrounded_entered_dr,
4072                   unrounded_entered_cr,
4073                   unrounded_accounted_dr,
4074                   unrounded_accounted_cr,
4075                   ref_ae_header_id,
4076                   ref_temp_line_num,
4077                   ref_event_id,
4078                   temp_line_num,
4079                   tax_line_ref_id,
4080                   tax_summary_line_ref_id,
4081                   tax_rec_nrec_dist_ref_id,
4082                   line_definition_owner_code,
4083                   line_definition_code,
4084                   event_class_code,
4085                   event_type_code
4086                ) values (
4087                   l_upg_batch_id,              -- upg_batch_id
4088                   c_application_id,            -- application_id
4089                   l_event_id_tbl(i),           -- event_id
4090                   l_ae_header_id,              -- ae_header_id
4091                   l_ae_line_num_tbl(j),        -- ae_line_num
4092                   null,                        -- accounting_line_code
4093                   'S',                         -- accounting_line_type_code
4094                   'TRX',                       -- source_distribution_type
4095                   null,                        -- source_distribution_id_char_1
4096                   null,                        -- source_distribution_id_char_2
4097                   null,                        -- source_distribution_id_char_3
4098                   null,                        -- source_distribution_id_char_4
4099                   null,                        -- source_distribution_id_char_5
4100                   l_transaction_header_id_tbl(i),
4101                                                -- source_distribution_id_num_1
4102                   l_adj_line_id_tbl(j),        -- source_distribution_id_num_2
4103                   null,                        -- source_distribution_id_num_3
4104                   null,                        -- source_distribution_id_num_4
4105                   null,                        -- source_distribution_id_num_5
4106                   'N',                         -- merge_duplicate_code
4107                   null,                        -- statistical_amount
4108                   l_debit_amount_tbl(j),       -- unrounded_entered_dr
4109                   l_credit_amount_tbl(j),      -- unrounded_entered_cr
4110                   l_debit_amount_tbl(j),       -- unrounded_accounted_dr
4111                   l_credit_amount_tbl(j),      -- unrounded_accounted_cr
4112                   l_ae_header_id,              -- ref_ae_header_id
4113                   null,                        -- ref_temp_line_num
4114                   null,                        -- ref_event_id
4115                   l_ae_line_num_tbl(j),        -- temp_line_num
4116                   null,                        -- tax_line_ref_id
4117                   null,                        -- tax_summary_line_ref_id
4118                   null,                        -- tax_rec_nrec_dist_ref_id
4119                   null,                        -- line_definition_owner_code
4120                   null,                        -- line_definition_code
4121                   l_event_class_code_tbl(i),   -- event_class_code
4122                   l_event_class_code_tbl(i)    -- event_type_code
4123                );
4124 
4125             for j IN 1..l_xla_gl_sl_link_id_tbl.count loop
4126                if (l_je_batch_id_tbl(j) is not null) then
4127                   insert into gl_import_references (
4128                      je_batch_id,
4129                      je_header_id,
4130                      je_line_num,
4131                      last_update_date,
4132                      last_updated_by,
4133                      creation_date,
4134                      created_by,
4135                      last_update_login,
4136                      reference_1,
4137                      reference_2,
4138                      reference_3,
4139                      reference_4,
4140                      reference_5,
4141                      reference_6,
4142                      reference_7,
4143                      reference_8,
4144                      reference_9,
4145                      reference_10,
4146                      subledger_doc_sequence_id,
4147                      subledger_doc_sequence_value,
4148                      gl_sl_link_id,
4149                      gl_sl_link_table
4150                   ) values (
4151                      l_je_batch_id_tbl(j),        -- je_batch_id
4152                      l_je_header_id_tbl(j),       -- je_header_id
4153                      l_je_line_num_tbl(j),        -- je_line_num
4154                      sysdate,                     -- last_update_date
4155                      c_fnd_user,                  -- last_updated_by
4156                      sysdate,                     -- creation_date
4157                      c_fnd_user,                  -- created_by
4158                      c_upgrade_bugno,             -- last_update_login
4159                      to_char(l_transaction_header_id_tbl(i)),
4160                                                   -- reference_1
4161                      to_char(l_asset_id_tbl(i)),  -- reference_2
4162                      to_char(l_distribution_id_tbl(j)),
4163                                                   -- reference_3
4164                      to_char(l_adj_line_id_tbl(j)),
4165                                                   -- reference_4
4166                      l_book_type_code_tbl(i),     -- reference_5
4167                      to_char(l_period_counter_tbl(i)),
4168                                                   -- reference_6
4169                      null,                        -- reference_7
4170                      null,                        -- reference_8
4171                      null,                        -- reference_9
4172                      null,                        -- reference_10
4173                      null,                        -- subledger_doc_seq_id
4174                      null,                        -- subledger_doc_seq_value
4175                      l_xla_gl_sl_link_id_tbl(j),  -- gl_sl_link_id
4176                      'XLAJEL'                     -- gl_sl_link_table
4177                   );
4178                end if;
4179             end loop;
4180 
4181             l_adj_line_id_tbl.delete;
4182             l_xla_gl_sl_link_id_tbl.delete;
4183             l_ae_line_num_tbl.delete;
4184             l_debit_amount_tbl.delete;
4185             l_credit_amount_tbl.delete;
4186             l_ccid_tbl.delete;
4187             l_currency_code_tbl.delete;
4188             l_acct_class_code_tbl.delete;
4189             l_line_def_owner_code_tbl.delete;
4190             l_line_def_code_tbl.delete;
4191             l_line_desc_tbl.delete;
4192             l_gl_transfer_status_code_tbl.delete;
4193             l_je_batch_id_tbl.delete;
4194             l_je_header_id_tbl.delete;
4195             l_je_line_num_tbl.delete;
4196             l_distribution_id_tbl.delete;
4197 
4198          end loop;
4199 
4200          l_rep_set_of_books_id_tbl.delete;
4201 
4202       END LOOP;
4203 
4204       l_rowid_tbl.delete;
4205       l_event_id_tbl.delete;
4206       l_asset_id_tbl.delete;
4207       l_book_type_code_tbl.delete;
4208       l_primary_set_of_books_id_tbl.delete;
4209       l_org_id_tbl.delete;
4210       l_transaction_type_code_tbl.delete;
4211       l_transaction_date_entered_tbl.delete;
4212       l_transaction_header_id_tbl.delete;
4213       l_period_counter_tbl.delete;
4214       l_period_name_tbl.delete;
4215       l_cal_period_close_date_tbl.delete;
4216       l_entity_id_tbl.delete;
4217       l_event_class_code_tbl.delete;
4218       l_member_thid_tbl.delete;
4219       l_hdr_desc_tbl.delete;
4220       l_je_category_name_tbl.delete;
4221       l_date_effective_tbl.delete;
4222 
4223       commit;
4224 
4225       if (l_rows_processed < l_batch_size) then exit; end if;
4226 
4227    end loop;
4228    close c_trans;
4229 
4230 EXCEPTION
4231    WHEN OTHERS THEN
4232       rollback;
4233       raise;
4234 
4235 End Upgrade_Group_Trxn_Events;
4236 
4237 Procedure Upgrade_Trxn_Events (
4238              p_start_rowid             IN            rowid,
4239              p_end_rowid               IN            rowid,
4240              p_batch_size              IN            number,
4241              x_success_count              OUT NOCOPY number,
4242              x_failure_count              OUT NOCOPY number,
4243              x_return_status              OUT NOCOPY number
4244             ) IS
4245 
4246    c_application_id            constant number(15) := 140;
4247    c_upgrade_bugno             constant number(15) := -4107161;
4248    c_fnd_user                  constant number(15) := 2;
4249 
4250 
4251    c_entity_code               constant varchar2(30) := 'TRANSACTIONS';
4252    c_amb_context_code          constant varchar2(30) := 'DEFAULT';
4253 
4254    -- this value can be altered in order to process more of less per batch
4255    l_batch_size                NUMBER;
4256 
4257    l_rows_processed            NUMBER;
4258 
4259    l_upg_batch_id              NUMBER(15);
4260    l_bonus_deprn_rsv_desc      VARCHAR2(80);
4261    l_mc_books                  NUMBER;
4262 
4263 
4264 BEGIN
4265 
4266    x_success_count := 0;
4267    x_failure_count := 0;
4268 
4269    l_batch_size := nvl(nvl(p_batch_size, fa_cache_pkg.fa_batch_size), 1000);
4270 
4271    -- Select upg_batch_id
4272    select xla_upg_batches_s.nextval
4273    into   l_upg_batch_id
4274    from   dual;
4275 
4276    -- Save value into a variable
4277    select description
4278    into   l_bonus_deprn_rsv_desc
4279    from   fa_lookups
4280    where  lookup_type = 'JOURNAL ENTRIES'
4281    and    lookup_code = 'BONUS DEPRECIATION RESERVE';
4282 
4283    insert all
4284    when 1 = 1 then
4285       into fa_xla_upg_events_gt (
4286             event_id,
4287             entity_id,
4288             transaction_header_id,
4289             set_of_books_id,
4290             period_name,
4291             calendar_period_close_date,
4292             date_effective,
4293             je_category_name,
4294             description,
4295             event_type_code,
4296             event_class_code,
4297             asset_id,
4298             book_type_code,
4299             period_counter
4300       ) values (
4301             xla_events_s.nextval,
4302             xla_transaction_entities_s.nextval,
4303             transaction_header_id,
4304             set_of_books_id,
4305             period_name,
4306             calendar_period_close_date,
4307             date_effective,
4308             je_category_name,
4309             description,
4310             event_type_code,
4311             event_class_code,
4312             asset_id,
4313             book_type_code,
4314             period_counter
4315       )
4316    when 1 = 1 then
4317       into xla_transaction_entities_upg (
4318             upg_batch_id,
4319             application_id,
4320             ledger_id,
4321             legal_entity_id,
4322             entity_code,
4323             source_id_int_1,
4324             source_id_int_2,
4325             source_id_int_3,
4326             source_id_int_4,
4327             source_id_char_1,
4328             source_id_char_2,
4329             source_id_char_3,
4330             source_id_char_4,
4331             security_id_int_1,
4332             security_id_int_2,
4333             security_id_int_3,
4334             security_id_char_1,
4335             security_id_char_2,
4336             security_id_char_3,
4337             transaction_number,
4338             valuation_method,
4339             source_application_id,
4340             creation_date,
4341             created_by,
4342             last_update_date,
4343             last_updated_by,
4344             last_update_login,
4345             entity_id,
4346             upg_source_application_id
4347       ) values (
4348             l_upg_batch_id,        -- upg_batch_id
4349             c_application_id,      -- application_id
4350             set_of_books_id,       -- ledger_id
4351             null,                  -- legal_entity_id
4352             c_entity_code,         -- entity_code
4353             transaction_header_id, -- source_id_int_1
4354             null,                  -- source_id_int_2
4355             null,                  -- source_id_int_3
4356             null,                  -- source_id_int_4
4357             book_type_code,        -- source_id_char_1  -- Bug 8239360
4358             null,                  -- source_id_char_2
4359             null,                  -- source_id_char_3
4360             null,                  -- source_id_char_4
4361             null,                  -- security_id_int_1
4362             null,                  -- security_id_int_2
4363             null,                  -- security_id_int_3
4364             null,                  -- security_id_char_1
4365             null,                  -- security_id_char_2
4366             null,                  -- security_id_char_3
4367             to_char(transaction_header_id),
4368                                    -- transaction_number
4369             book_type_code,        -- valuation_method
4370             c_application_id,      -- source_application_id
4371             sysdate,               -- creation_date
4372             c_fnd_user,            -- created_by
4373             sysdate,               -- last_update_date
4374             c_fnd_user,            -- last_updated_by
4375             c_upgrade_bugno,       -- last_update_login
4376             xla_transaction_entities_s.currval,
4377                                    -- entity_id
4378             c_application_id       -- upg_source_application_id
4379       )
4380    when 1 = 1 then
4381       into xla_events (
4382             upg_batch_id,
4383             application_id,
4384             event_type_code,
4385             event_number,
4386             event_status_code,
4387             process_status_code,
4388             on_hold_flag,
4389             event_date,
4390             creation_date,
4391             created_by,
4392             last_update_date,
4393             last_updated_by,
4394             last_update_login,
4395             program_update_date,
4396             program_id,
4397             program_application_id,
4398             request_id,
4399             entity_id,
4400             event_id,
4401             upg_source_application_id,
4402             transaction_date
4403       ) values (
4404             l_upg_batch_id,        -- upg_batch_id
4405             c_application_id,      -- application_id
4406             event_type_code,
4407             1,                     -- event_number
4408             event_status_code,     -- Bug 6811554: event_status_code
4409             process_status_code,   -- Bug 6811554: process_status_code
4410             'N',                   -- on_hold_flag
4411             transaction_date_entered,
4412                                    -- event_date
4413             sysdate,               -- creation_date
4414             c_fnd_user,            -- created_by
4415             sysdate,               -- last_update_date
4416             c_fnd_user,            -- last_updated_by
4417             c_upgrade_bugno,       -- last_update_login
4418             null,                  -- program_update_date
4419             null,                  -- program_id
4420             null,                  -- program_application_id
4421             null,                  -- request_id
4422             xla_transaction_entities_s.currval,
4423                                    -- entity_id
4424             xla_events_s.currval,  -- event_id
4425             c_application_id,      -- upg_source_application_id
4426             transaction_date_entered
4427                                    -- transaction_date
4428       )
4429    when event_id is null then   -- Bug 6811554
4430       into fa_xla_upg_headers_gt (
4431          ae_header_id,
4432          event_id,
4433          set_of_books_id
4434       ) values (
4435          xla_ae_headers_s.nextval,
4436          xla_events_s.currval,
4437          set_of_books_id
4438       )
4439 --   when 1 = 1 then
4440    when event_id is null then   -- Bug 6811554
4441       into xla_ae_headers (
4442             upg_batch_id,
4443             application_id,
4444             amb_context_code,
4445             entity_id,
4446             event_id,
4447             event_type_code,
4448             ae_header_id,
4449             ledger_id,
4450             accounting_date,
4451             period_name,
4452             reference_date,
4453             balance_type_code,
4454             je_category_name,
4455             gl_transfer_status_code,
4456             gl_transfer_date,
4457             accounting_entry_status_code,
4458             accounting_entry_type_code,
4459             description,
4460             budget_version_id,
4461             funds_status_code,
4462             encumbrance_type_id,
4463             completed_date,
4464             doc_sequence_id,
4465             doc_sequence_value,
4466             doc_category_code,
4467             packet_id,
4468             group_id,
4469             creation_date,
4470             created_by,
4471             last_update_date,
4472             last_updated_by,
4473             last_update_login,
4474             program_id,
4475             program_application_id,
4476             program_update_date,
4477             request_id,
4478             close_acct_seq_assign_id,
4479             close_acct_seq_version_id,
4480             close_acct_seq_value,
4481             completion_acct_seq_assign_id,
4482             completion_acct_seq_version_id,
4483             completion_acct_seq_value,
4484             accounting_batch_id,
4485             product_rule_type_code,
4486             product_rule_code,
4487             product_rule_version,
4488             upg_source_application_id,
4489             upg_valid_flag
4490       ) values (
4491             l_upg_batch_id,             -- upg_batch_id
4492             c_application_id,           -- application_id
4493             c_amb_context_code,         -- amb_context_code
4494             xla_transaction_entities_s.currval,
4495                                         -- entity_id
4496             xla_events_s.currval,       -- event_id
4497             event_type_code,            -- event_type_code
4498             xla_ae_headers_s.currval,   -- ae_header_id
4499             set_of_books_id,            -- ledger_id
4500             calendar_period_close_date, -- accounting_date
4501             period_name,                -- period_name
4502             null,                       -- reference_date
4503             'A',                        -- balance_type_code
4504             je_category_name,           -- je_category_name
4505             -- Bug 6811554 , 12879380
4506             decode(je_hdr_id, null, decode (gl_posting_allowed_flag,
4507                                                               'YES', 'N', 'NT')
4508                                   ,'Y'),-- gl_transfer_status_code
4509             null,                       -- gl_transfer_date
4510             'F',                        -- accounting_entry_status_code
4511             'STANDARD',                 -- accounting_entry_type_code
4512             description,                -- description
4513             null,                       -- budget_version_id
4514             null,                       -- funds_status_code
4515             null,                       -- encumbrance_type_id
4516             null,                       -- completed_date
4517             null,                       -- doc_sequence_id
4518             null,                       -- doc_sequence_value
4519             null,                       -- doc_category_code
4520             null,                       -- packet_id
4521             null,                       -- group_id
4522             sysdate,                    -- creation_date
4523             c_fnd_user,                 -- created_by
4524             sysdate,                    -- last_update_date
4525             c_fnd_user,                 -- last_updated_by
4526             c_upgrade_bugno,            -- last_update_login
4527             null,                       -- program_id
4528             c_application_id,           -- program_application_id
4529             sysdate,                    -- program_update_date
4530             null,                       -- request_id
4531             null,                       -- close_acct_seq_assign_id
4532             null,                       -- close_acct_seq_version_id
4533             null,                       -- close_acct_seq_value
4534             null,                       -- completion_acct_seq_assign_id
4535             null,                       -- completion_acct_seq_version_id
4536             null,                       -- completion_acct_seq_value
4537             null,                       -- accounting_batch_id
4538             null,                       -- product_rule_type_code
4539             null,                       -- product_rule_code
4540             null,                       -- product_rule_version
4541             c_application_id,           -- upg_source_application_id
4542             null                        -- upg_valid_flag
4543       )
4544       -- Bug 7498880: Added hint to use GL_PERIOD_STATUSES_U1
4545       -- Bug 9857478: Added ad join so that events are not created for 'EXPENSED' assets
4546       select /*+ leading(th bc dp ps) rowid(th) swap_join_inputs(bc) swap_join_inputs(dp) index(ps GL_PERIOD_STATUSES_U1)*/
4547              bc.set_of_books_id                 set_of_books_id,
4548              th.transaction_header_id           transaction_header_id,
4549              bc.book_type_code                  book_type_code,
4550              bc.org_id                          org_id,
4551              decode(th.transaction_type_code,
4552                 'ADDITION', decode (ah.asset_type,
4553                                     'CIP', 'CAPITALIZATION',
4554                                     'ADDITIONS'),
4555                 'ADJUSTMENT', 'ADJUSTMENTS',
4556                 'CIP ADDITION', 'CIP_ADDITIONS',
4557                 'CIP ADJUSTMENT', 'CIP_ADJUSTMENTS',
4558                 'CIP REVERSE', 'REVERSE_CAPITALIZATION',
4559                 'CIP REINSTATEMENT', 'CIP_REINSTATEMENTS',
4560                 'CIP RETIREMENT', 'CIP_RETIREMENTS',
4561                 'CIP TRANSFER', 'CIP_TRANSFERS',
4562                 'CIP UNIT ADJUSTMENTS', 'CIP_UNIT_ADJUSTMENTS',
4563                 'FULL RETIREMENT', 'RETIREMENTS',
4564                 'PARTIAL RETIREMENT', 'RETIREMENTS',
4565                 'RECLASS', 'CATEGORY_RECLASS',
4566                 'REINSTATEMENT', 'REINSTATEMENTS',
4567                 'REVALUATION', 'REVALUATION',
4568                 'TRANSFER', 'TRANSFERS',
4569                 'TRANSFER IN', 'TRANSFERS',
4570                 'TRANSFER IN/VOID', 'TRANSFERS',
4571                 'TRANSFER OUT', 'TRANSFERS',
4572                 'UNIT ADJUSTMENT', 'UNIT_ADJUSTMENTS',
4573                 'UNPLANNED DEPRN', 'UNPLANNED_DEPRECIATION',
4574                 'TAX', 'DEPRECIATION_ADJUSTMENTS',
4575                 'OTHER'
4576              )                                  event_class_code,
4577              decode(th.transaction_type_code,
4578                 'ADDITION', decode (ah.asset_type,
4579                                     'CIP', 'CAPITALIZATION',
4580                                     'ADDITIONS'),
4581                 'ADJUSTMENT', 'ADJUSTMENTS',
4582                 'CIP ADDITION', 'CIP_ADDITIONS',
4583                 'CIP ADJUSTMENT', 'CIP_ADJUSTMENTS',
4584                 'CIP REVERSE', 'REVERSE_CAPITALIZATION',
4585                 'CIP REINSTATEMENT', 'CIP_REINSTATEMENTS',
4586                 'CIP RETIREMENT', 'CIP_RETIREMENTS',
4587                 'CIP TRANSFER', 'CIP_TRANSFERS',
4588                 'CIP UNIT ADJUSTMENTS', 'CIP_UNIT_ADJUSTMENTS',
4589                 'FULL RETIREMENT', 'RETIREMENTS',
4590                 'PARTIAL RETIREMENT', 'RETIREMENTS',
4591                 'RECLASS', 'CATEGORY_RECLASS',
4592                 'REINSTATEMENT', 'REINSTATEMENTS',
4593                 'REVALUATION', 'REVALUATION',
4594                 'TRANSFER', 'TRANSFERS',
4595                 'TRANSFER IN', 'TRANSFERS',
4596                 'TRANSFER IN/VOID', 'TRANSFERS',
4597                 'TRANSFER OUT', 'TRANSFERS',
4598                 'UNIT ADJUSTMENT', 'UNIT_ADJUSTMENTS',
4599                 'UNPLANNED DEPRN', 'UNPLANNED_DEPRECIATION',
4600                 'TAX', 'DEPRECIATION_ADJUSTMENTS',
4601                 'OTHER'
4602              )                                  event_type_code,
4603              greatest (th.transaction_date_entered,
4604                   dp.calendar_period_open_date) transaction_date_entered, -- Bug 12533865
4605              dp.period_name                     period_name,
4606              dp.calendar_period_close_date      calendar_period_close_date,
4607              th.date_effective                  date_effective,
4608              nvl (decode(th.transaction_type_code,
4609                 'ADDITION',             bc.je_addition_category,
4610                 'ADJUSTMENT',           bc.je_adjustment_category,
4611                 'CIP ADDITION',         bc.je_cip_addition_category,
4612                 'CIP ADJUSTMENT',       bc.je_cip_adjustment_category,
4613                 'CIP REVERSE',          bc.je_cip_addition_category,
4614                 'CIP REINSTATEMENT',    bc.je_cip_retirement_category,
4615                 'CIP RETIREMENT',       bc.je_cip_retirement_category,
4616                 'CIP TRANSFER',         bc.je_cip_transfer_category,
4617                 'CIP UNIT ADJUSTMENTS', bc.je_cip_transfer_category,
4618                 'FULL RETIREMENT',      bc.je_retirement_category,
4619                 'PARTIAL RETIREMENT',   bc.je_retirement_category,
4620                 'RECLASS',              bc.je_reclass_category,
4621                 'REINSTATEMENT',        bc.je_retirement_category,
4622                 'REVALUATION',          bc.je_reval_category,
4623                 'TRANSFER',             bc.je_transfer_category,
4624                 'TRANSFER IN',          bc.je_transfer_category,
4625                 'TRANSFER IN/VOID',     bc.je_transfer_category,
4626                 'TRANSFER OUT',         bc.je_transfer_category,
4627                 'UNIT ADJUSTMENT',      bc.je_transfer_category,
4628                 'UNPLANNED DEPRN',      bc.je_depreciation_category,
4629                 'TAX',                  bc.je_deprn_adjustment_category
4630              ), 'OTHER')                        je_category_name,
4631              lk.description || ' - ' ||
4632                 to_char(dp.calendar_period_close_date, 'DD-MON-RR')
4633                                                 description,
4634              dp.period_counter                  period_counter,
4635              th.asset_id                        asset_id,
4636              th.event_id                        event_id,  -- Bug 6811554
4637              decode(th.event_id, -2, 'U',
4638                                  -3, 'I',
4639                                  'P') event_status_code,   -- Bug 6811554
4640              decode(th.event_id, -2, 'U',
4641                                  -3, 'U',
4642                                  'P') process_status_code, -- Bug 6811554
4643              (select min(je_header_id)
4644               from   fa_adjustments adj
4645               where  adj.book_type_code = th.book_type_code
4646               and    adj.asset_id = th.asset_id
4647               and    adj.transaction_header_id = th.transaction_header_id
4648               and    nvl(adj.je_header_id,-1) > 0) je_hdr_id, -- Bug 6811554
4649              bc.gl_posting_allowed_flag gl_posting_allowed_flag
4650       from   fa_transaction_headers th,
4651              fa_book_controls bc,
4652              fa_deprn_periods dp,
4653              gl_period_statuses ps,
4654              fa_lookups_tl lk,
4655              fa_asset_history ah,
4656              fa_additions_b ad
4657       where th.rowid between p_start_rowid and p_end_rowid
4658       and   th.member_transaction_header_id is null
4659       and   (th.event_id is null or th.event_id in (-2,-3)) -- Bug 6811554
4660       and   ps.application_id = 101
4661       and   ((ps.migration_status_code in ('P', 'U')) or
4662              (dp.period_close_date is null))
4663       and   substr (dp.xla_conversion_status, 1, 1) in
4664             ('H', 'U', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
4665       and   dp.xla_conversion_status not in ('UT', 'UA')
4666       and   ps.set_of_books_id = bc.set_of_books_id
4667       and   ps.period_name = dp.period_name
4668       and   th.book_type_code = bc.book_type_code
4669       and   dp.book_type_code = bc.book_type_code
4670       and   th.date_effective between dp.period_open_date and
4671                                       nvl(dp.period_close_date, sysdate)
4672       and   th.asset_id = ah.asset_id (+)
4673       and   th.transaction_header_id = ah.transaction_header_id_out (+)
4674       and   th.transaction_type_code = lk.lookup_code
4675       and   lk.lookup_type = 'FAXOLTRX'
4676       and   userenv('LANG') = lk.language
4677       and   th.asset_id = ad.asset_id
4678       and   ad.asset_type <> 'EXPENSED'
4679       and   (exists
4680       (
4681        select /*+ index(adj, FA_ADJUSTMENTS_U1) */
4682               'x'
4683        from   fa_adjustments adj
4684        where  th.transaction_header_id = adj.transaction_header_id
4685        and    bc.book_type_code = adj.book_type_code
4686        and    th.asset_id = adj.asset_id
4687       ) or th.event_id = -3)  -- Bug 9055709
4688    union all -- Added for bug6820729
4689    -- Bug 7498880: Added hint to use GL_PERIOD_STATUSES_U1
4690    select /*+ leading(th bc dp ps) rowid(th) swap_join_inputs(bc) swap_join_inputs(dp) index(ps GL_PERIOD_STATUSES_U1)*/
4691              bc.set_of_books_id                 set_of_books_id,
4692              th.transaction_header_id           transaction_header_id,
4693              bc.book_type_code                  book_type_code,
4694              bc.org_id                          org_id,
4695              decode(th.transaction_type_code,
4696                 'CIP REINSTATEMENT', 'CIP_REINSTATEMENTS',
4697                 'CIP TRANSFER', 'CIP_TRANSFERS',
4698                 'CIP UNIT ADJUSTMENTS', 'CIP_UNIT_ADJUSTMENTS',
4699                 'RECLASS', 'CATEGORY_RECLASS',
4700                 'REINSTATEMENT', 'REINSTATEMENTS',
4701                 'TRANSFER', 'TRANSFERS',
4702                 'TRANSFER OUT', 'TRANSFERS',
4703                 'UNIT ADJUSTMENT', 'UNIT_ADJUSTMENTS',
4704                 'OTHER'
4705              )                                  event_class_code,
4706              decode(th.transaction_type_code,
4707                 'CIP REINSTATEMENT', 'CIP_REINSTATEMENTS',
4708                 'CIP TRANSFER', 'CIP_TRANSFERS',
4709                 'CIP UNIT ADJUSTMENTS', 'CIP_UNIT_ADJUSTMENTS',
4710                 'RECLASS', 'CATEGORY_RECLASS',
4711                 'REINSTATEMENT', 'REINSTATEMENTS',
4712                 'TRANSFER', 'TRANSFERS',
4713                 'TRANSFER OUT', 'TRANSFERS',
4714                 'UNIT ADJUSTMENT', 'UNIT_ADJUSTMENTS',
4715                 'OTHER'
4716              )                                  event_type_code,
4717              greatest (th.transaction_date_entered,
4718                   dp.calendar_period_open_date) transaction_date_entered, -- Bug 12533865
4719              dp.period_name                     period_name,
4720              dp.calendar_period_close_date      calendar_period_close_date,
4721              th.date_effective                  date_effective,
4722              nvl (decode(th.transaction_type_code,
4723                 'CIP REINSTATEMENT',    bc.je_cip_retirement_category,
4724                 'CIP TRANSFER',         bc.je_cip_transfer_category,
4725                 'CIP UNIT ADJUSTMENTS', bc.je_cip_transfer_category,
4726                 'RECLASS',              bc.je_reclass_category,
4727                 'REINSTATEMENT',        bc.je_retirement_category,
4728                 'TRANSFER',             bc.je_transfer_category,
4729                 'TRANSFER OUT',         bc.je_transfer_category,
4730                 'UNIT ADJUSTMENT',      bc.je_transfer_category),
4731                 'OTHER')                        je_category_name,
4732              lk.description || ' - ' ||
4733                 to_char(dp.calendar_period_close_date, 'DD-MON-RR')
4734                                                 description,
4735              dp.period_counter                  period_counter,
4736              th.asset_id                        asset_id,
4737              th.event_id                        event_id,  -- Bug 6811554
4738              decode(th.event_id, -2, 'U',
4739                                  -3, 'I',
4740                                  'P') event_status_code,   -- Bug 6811554
4741              decode(th.event_id, -2, 'U',
4742                                  -3, 'U',
4743                                  'P') process_status_code, -- Bug 6811554
4744              (select min(je_header_id)
4745               from   fa_adjustments adj
4746               where  adj.book_type_code = th.book_type_code
4747               and    adj.asset_id = th.asset_id
4748               and    adj.transaction_header_id = th.transaction_header_id
4749               and    nvl(adj.je_header_id,-1) > 0) je_hdr_id, -- Bug 6811554
4750              bc.gl_posting_allowed_flag gl_posting_allowed_flag
4751       from   fa_transaction_headers th,
4752              fa_book_controls bc,
4753              fa_deprn_periods dp,
4754              gl_period_statuses ps,
4755              fa_lookups_tl lk,
4756              fa_additions_b ad
4757       where th.rowid between p_start_rowid and p_end_rowid
4758       and   th.member_transaction_header_id is null
4759       and   (th.event_id is null or th.event_id in (-2,-3)) -- Bug 6811554
4760       and   ps.application_id = 101
4761       and   ((ps.migration_status_code in ('P', 'U')) or
4762              (dp.period_close_date is null))
4763       and   substr (dp.xla_conversion_status, 1, 1) in
4764             ('H', 'U', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
4765       and   dp.xla_conversion_status not in ('UT', 'UA')
4766       and   ps.set_of_books_id = bc.set_of_books_id
4767       and   ps.period_name = dp.period_name
4768       and   th.transaction_type_code in('TRANSFER','TRANSFER OUT',
4769                    'RECLASS','UNIT ADJUSTMENT','REINSTATEMENT',
4770                    'CIP REINSTATEMENT','CIP TRANSFER','CIP UNIT ADJUSTMENTS')
4771       and   th.book_type_code = bc.distribution_source_book
4772       and   bc.book_class = 'TAX'
4773       and   dp.book_type_code = bc.book_type_code
4774       and   th.date_effective between dp.period_open_date and
4775                                       nvl(dp.period_close_date, sysdate)
4776       and   th.transaction_type_code = lk.lookup_code
4777       and   lk.lookup_type = 'FAXOLTRX'
4778       and   userenv('LANG') = lk.language
4779       and   th.asset_id = ad.asset_id
4780       and   ad.asset_type <> 'EXPENSED'
4781       and   (exists
4782       (
4783        select /*+ index(adj, FA_ADJUSTMENTS_U1) */
4784               'x'
4785        from   fa_adjustments adj
4786        where  th.transaction_header_id = adj.transaction_header_id
4787        and    bc.book_type_code = adj.book_type_code
4788        and    th.asset_id = adj.asset_id
4789       ) or th.event_id = -3);  -- Bug 9055709
4790 
4791    update /*+ rowid(th) */
4792           fa_transaction_headers th
4793    set    th.event_id = nvl(
4794    ( select ev.event_id
4795      from   fa_xla_upg_events_gt ev
4796      where  ev.transaction_header_id = th.transaction_header_id
4797      and    ev.book_type_code = th.book_type_code
4798    ), th.event_id)
4799    where  th.rowid between p_start_rowid and p_end_rowid;
4800 
4801    insert all
4802       when (adj_row = 1) then
4803       into xla_ae_lines (
4804                   upg_batch_id,
4805                   ae_header_id,
4806                   ae_line_num,
4807                   displayed_line_number,
4808                   application_id,
4809                   code_combination_id,
4810                   gl_transfer_mode_code,
4811                   accounted_dr,
4812                   accounted_cr,
4813                   currency_code,
4814                   currency_conversion_date,
4815                   currency_conversion_rate,
4816                   currency_conversion_type,
4817                   entered_dr,
4818                   entered_cr,
4819                   description,
4820                   accounting_class_code,
4821                   gl_sl_link_id,
4822                   gl_sl_link_table,
4823                   party_type_code,
4824                   party_id,
4825                   party_site_id,
4826                   statistical_amount,
4827                   ussgl_transaction_code,
4828                   jgzz_recon_ref,
4829                   control_balance_flag,
4830                   analytical_balance_flag,
4831                   upg_tax_reference_id1,
4832                   upg_tax_reference_id2,
4833                   upg_tax_reference_id3,
4834                   creation_date,
4835                   created_by,
4836                   last_update_date,
4837                   last_updated_by,
4838                   last_update_login,
4839                   program_update_date,
4840                   program_id,
4841                   program_application_id,
4842                   request_id,
4843                   gain_or_loss_flag,
4844                   accounting_date,
4845                   ledger_id
4846             ) values (
4847                   l_upg_batch_id,                 -- upg_batch_id
4848                   ae_header_id,                   -- ae_header_id
4849                   ae_line_num,                    -- ae_line_num
4850                   ae_line_num,                    -- displayed_line_num
4851                   c_application_id,               -- application_id
4852                   ccid1,                          -- code_combination_id
4853                   'S',                            -- gl_transfer_mode_code
4854                   debit_amount,                   -- accounted_dr
4855                   credit_amount,                  -- accounted_cr
4856                   currency_code,                  -- currency_code
4857                   null,                           -- currency_conversion_date
4858                   null,                           -- currency_conversion_rate
4859                   null,                           -- currency_conversion_type
4860                   debit_amount,                   -- entered_dr
4861                   credit_amount,                  -- entered_cr
4862                   line_desc1 || ' - ' ||
4863                      to_char(cal_period_close_date, 'DD-MON-RR'),
4864                                                   -- description
4865                   accounting_class_code,          -- accounting_class_code
4866                   xla_gl_sl_link_id_s.nextval,    -- gl_sl_link_id
4867                   'XLAJEL',                       -- gl_sl_link_table
4868                   null,                           -- party_type_code
4869                   null,                           -- party_id
4870                   null,                           -- party_site_id
4871                   null,                           -- statistical_amount
4872                   null,                           -- ussgl_transaction_code
4873                   null,                           -- glzz_recon_ref
4874                   null,                           -- control_balance_flag
4875                   null,                           -- analytical_balance_flag
4876                   null,                           -- upg_tax_reference_id1
4877                   null,                           -- upg_tax_reference_id2
4878                   null,                           -- upg_tax_reference_id3
4879                   sysdate,                        -- creation_date
4880                   c_fnd_user,                     -- created_by
4881                   sysdate,                        -- last_update_date
4882                   c_fnd_user,                     -- last_updated_by
4883                   c_upgrade_bugno,                -- last_update_login
4884                   null,                           -- program_update_date
4885                   null,                           -- program_id
4886                   c_application_id,               -- program_application_id
4887                   null,                           -- request_id
4888                   'N',                            -- gain_or_loss_flag
4889                   cal_period_close_date,          -- accounting_date
4890                   set_of_books_id                 -- ledger_id
4891             )
4892       when (adj_row = 1) then
4893       into xla_distribution_links (
4894                   upg_batch_id,
4895                   application_id,
4896                   event_id,
4897                   ae_header_id,
4898                   ae_line_num,
4899                   accounting_line_code,
4900                   accounting_line_type_code,
4901                   source_distribution_type,
4902                   source_distribution_id_char_1,
4903                   source_distribution_id_char_2,
4904                   source_distribution_id_char_3,
4905                   source_distribution_id_char_4,
4906                   source_distribution_id_char_5,
4907                   source_distribution_id_num_1,
4908                   source_distribution_id_num_2,
4909                   source_distribution_id_num_3,
4910                   source_distribution_id_num_4,
4911                   source_distribution_id_num_5,
4912                   merge_duplicate_code,
4913                   statistical_amount,
4914                   unrounded_entered_dr,
4915                   unrounded_entered_cr,
4916                   unrounded_accounted_dr,
4917                   unrounded_accounted_cr,
4918                   ref_ae_header_id,
4919                   ref_temp_line_num,
4920                   ref_event_id,
4921                   temp_line_num,
4922                   tax_line_ref_id,
4923                   tax_summary_line_ref_id,
4924                   tax_rec_nrec_dist_ref_id,
4925                   line_definition_owner_code,
4926                   line_definition_code,
4927                   event_class_code,
4928                   event_type_code
4929       ) values (
4930                   l_upg_batch_id,              -- upg_batch_id
4931                   c_application_id,            -- application_id
4932                   event_id,                    -- event_id
4933                   ae_header_id,                -- ae_header_id
4934                   ae_line_num,                 -- ae_line_num
4935                   null,                        -- accounting_line_code
4936                   'S',                         -- accounting_line_type_code
4937                   'TRX',                       -- source_distribution_type
4938                   null,                        -- source_distribution_id_char_1
4939                   null,                        -- source_distribution_id_char_2
4940                   null,                        -- source_distribution_id_char_3
4941                   null,                        -- source_distribution_id_char_4
4942                   null,                        -- source_distribution_id_char_5
4943                   transaction_header_id,       -- source_distribution_id_num_1
4944                   adj_line_id,                 -- source_distribution_id_num_2
4945                   null,                        -- source_distribution_id_num_3
4946                   null,                        -- source_distribution_id_num_4
4947                   null,                        -- source_distribution_id_num_5
4948                   'N',                         -- merge_duplicate_code
4949                   null,                        -- statistical_amount
4950                   debit_amount,                -- unrounded_entered_dr
4951                   credit_amount,               -- unrounded_entered_cr
4952                   debit_amount,                -- unrounded_accounted_dr
4953                   credit_amount,               -- unrounded_accounted_cr
4954                   ae_header_id,                -- ref_ae_header_id
4955                   null,                        -- ref_temp_line_num
4956                   null,                        -- ref_event_id
4957                   ae_line_num,                 -- temp_line_num
4958                   null,                        -- tax_line_ref_id
4959                   null,                        -- tax_summary_line_ref_id
4960                   null,                        -- tax_rec_nrec_dist_ref_id
4961                   null,                        -- line_definition_owner_code
4962                   null,                        -- line_definition_code
4963                   event_class_code,            -- event_class_code
4964                   event_type_code              -- event_type_code
4965       )
4966       when (adj_row = 1) and (je_batch_id1 is not null) then
4967       into gl_import_references (
4968                      je_batch_id,
4969                      je_header_id,
4970                      je_line_num,
4971                      last_update_date,
4972                      last_updated_by,
4973                      creation_date,
4974                      created_by,
4975                      last_update_login,
4976                      reference_1,
4977                      reference_2,
4978                      reference_3,
4979                      reference_4,
4980                      reference_5,
4981                      reference_6,
4982                      reference_7,
4983                      reference_8,
4984                      reference_9,
4985                      reference_10,
4986                      subledger_doc_sequence_id,
4987                      subledger_doc_sequence_value,
4988                      gl_sl_link_id,
4989                      gl_sl_link_table
4990       ) values (
4991                      je_batch_id1,                -- je_batch_id
4992                      je_header_id1,               -- je_header_id
4993                      je_line_num1,                -- je_line_num
4994                      sysdate,                     -- last_update_date
4995                      c_fnd_user,                  -- last_updated_by
4996                      sysdate,                     -- creation_date
4997                      c_fnd_user,                  -- created_by
4998                      c_upgrade_bugno,             -- last_update_login
4999                      to_char(transaction_header_id),
5000                                                   -- reference_1
5001                      to_char(asset_id),           -- reference_2
5002                      to_char(distribution_id),    -- reference_3
5003                      to_char(adj_line_id),        -- reference_4
5004                      book_type_code,              -- reference_5
5005                      to_char(period_counter),     -- reference_6
5006                      null,                        -- reference_7
5007                      null,                        -- reference_8
5008                      null,                        -- reference_9
5009                      null,                        -- reference_10
5010                      null,                        -- subledger_doc_seq_id
5011                      null,                        -- subledger_doc_seq_value
5012                      xla_gl_sl_link_id_s.currval, -- gl_sl_link_id
5013                      'XLAJEL'                     -- gl_sl_link_table
5014       )
5015       when (adj_row = 2) then
5016       into xla_ae_lines (
5017                   upg_batch_id,
5018                   ae_header_id,
5019                   ae_line_num,
5020                   displayed_line_number,
5021                   application_id,
5022                   code_combination_id,
5023                   gl_transfer_mode_code,
5024                   accounted_dr,
5025                   accounted_cr,
5026                   currency_code,
5027                   currency_conversion_date,
5028                   currency_conversion_rate,
5029                   currency_conversion_type,
5030                   entered_dr,
5031                   entered_cr,
5032                   description,
5033                   accounting_class_code,
5034                   gl_sl_link_id,
5035                   gl_sl_link_table,
5036                   party_type_code,
5037                   party_id,
5038                   party_site_id,
5039                   statistical_amount,
5040                   ussgl_transaction_code,
5041                   jgzz_recon_ref,
5042                   control_balance_flag,
5043                   analytical_balance_flag,
5044                   upg_tax_reference_id1,
5045                   upg_tax_reference_id2,
5046                   upg_tax_reference_id3,
5047                   creation_date,
5048                   created_by,
5049                   last_update_date,
5050                   last_updated_by,
5051                   last_update_login,
5052                   program_update_date,
5053                   program_id,
5054                   program_application_id,
5055                   request_id,
5056                   gain_or_loss_flag,
5057                   accounting_date,
5058                   ledger_id
5059             ) values (
5060                   l_upg_batch_id,                 -- upg_batch_id
5061                   ae_header_id,                   -- ae_header_id
5062                   ae_line_num,                    -- ae_line_num
5063                   ae_line_num,                    -- displayed_line_num
5064                   c_application_id,               -- application_id
5065                   ccid2,                          -- code_combination_id
5066                   'S',                            -- gl_transfer_mode_code
5067                   -- Fix for Bug #5131737.  Need to switch CR/DR for pseudo-row
5068                   credit_amount,                  -- accounted_dr
5069                   debit_amount,                   -- accounted_cr
5070                   currency_code,                  -- currency_code
5071                   null,                           -- currency_conversion_date
5072                   null,                           -- currency_conversion_rate
5073                   null,                           -- currency_conversion_type
5074                   credit_amount,                  -- entered_dr
5075                   debit_amount,                   -- entered_cr
5076                   line_desc2 || ' - ' ||
5077                      to_char(cal_period_close_date, 'DD-MON-RR'),
5078                                                   -- description
5079                   'ASSET',                        -- accounting_class_code
5080                   xla_gl_sl_link_id_s.nextval,    -- gl_sl_link_id
5081                   'XLAJEL',                       -- gl_sl_link_table
5082                   null,                           -- party_type_code
5083                   null,                           -- party_id
5084                   null,                           -- party_site_id
5085                   null,                           -- statistical_amount
5086                   null,                           -- ussgl_transaction_code
5087                   null,                           -- glzz_recon_ref
5088                   null,                           -- control_balance_flag
5089                   null,                           -- analytical_balance_flag
5090                   null,                           -- upg_tax_reference_id1
5091                   null,                           -- upg_tax_reference_id2
5092                   null,                           -- upg_tax_reference_id3
5093                   sysdate,                        -- creation_date
5094                   c_fnd_user,                     -- created_by
5095                   sysdate,                        -- last_update_date
5096                   c_fnd_user,                     -- last_updated_by
5097                   c_upgrade_bugno,                -- last_update_login
5098                   null,                           -- program_update_date
5099                   null,                           -- program_id
5100                   c_application_id,               -- program_application_id
5101                   null,                           -- request_id
5102                   'N',                            -- gain_or_loss_flag
5103                   cal_period_close_date,          -- accounting_date
5104                   set_of_books_id                 -- ledger_id
5105             )
5106       when (adj_row = 2) then
5107       into xla_distribution_links (
5108                   upg_batch_id,
5109                   application_id,
5110                   event_id,
5111                   ae_header_id,
5112                   ae_line_num,
5113                   accounting_line_code,
5114                   accounting_line_type_code,
5115                   source_distribution_type,
5116                   source_distribution_id_char_1,
5117                   source_distribution_id_char_2,
5118                   source_distribution_id_char_3,
5119                   source_distribution_id_char_4,
5120                   source_distribution_id_char_5,
5121                   source_distribution_id_num_1,
5122                   source_distribution_id_num_2,
5123                   source_distribution_id_num_3,
5124                   source_distribution_id_num_4,
5125                   source_distribution_id_num_5,
5126                   merge_duplicate_code,
5127                   statistical_amount,
5128                   unrounded_entered_dr,
5129                   unrounded_entered_cr,
5130                   unrounded_accounted_dr,
5131                   unrounded_accounted_cr,
5132                   ref_ae_header_id,
5133                   ref_temp_line_num,
5134                   ref_event_id,
5135                   temp_line_num,
5136                   tax_line_ref_id,
5137                   tax_summary_line_ref_id,
5138                   tax_rec_nrec_dist_ref_id,
5139                   line_definition_owner_code,
5140                   line_definition_code,
5141                   event_class_code,
5142                   event_type_code
5143       ) values (
5144                   l_upg_batch_id,              -- upg_batch_id
5145                   c_application_id,            -- application_id
5146                   event_id,                    -- event_id
5147                   ae_header_id,                -- ae_header_id
5148                   ae_line_num,                 -- ae_line_num
5149                   null,                        -- accounting_line_code
5150                   'S',                         -- accounting_line_type_code
5151                   'TRX',                       -- source_distribution_type
5152                   null,                        -- source_distribution_id_char_1
5153                   null,                        -- source_distribution_id_char_2
5154                   null,                        -- source_distribution_id_char_3
5155                   null,                        -- source_distribution_id_char_4
5156                   null,                        -- source_distribution_id_char_5
5157                   transaction_header_id,       -- source_distribution_id_num_1
5158                   adj_line_id,                 -- source_distribution_id_num_2
5159                   null,                        -- source_distribution_id_num_3
5160                   null,                        -- source_distribution_id_num_4
5161                   null,                        -- source_distribution_id_num_5
5162                   'N',                         -- merge_duplicate_code
5163                   null,                        -- statistical_amount
5164                   -- Fix for Bug #5131737.  Need to switch CR/DR for pseudo-row
5165                   credit_amount,               -- unrounded_entered_dr
5166                   debit_amount,                -- unrounded_entered_cr
5167                   credit_amount,               -- unrounded_accounted_dr
5168                   debit_amount,                -- unrounded_accounted_cr
5169                   ae_header_id,                -- ref_ae_header_id
5170                   null,                        -- ref_temp_line_num
5171                   null,                        -- ref_event_id
5172                   ae_line_num,                 -- temp_line_num
5173                   null,                        -- tax_line_ref_id
5174                   null,                        -- tax_summary_line_ref_id
5175                   null,                        -- tax_rec_nrec_dist_ref_id
5176                   null,                        -- line_definition_owner_code
5177                   null,                        -- line_definition_code
5178                   event_class_code,            -- event_class_code
5179                   event_type_code              -- event_type_code
5180       )
5181       when (adj_row = 2) and (je_batch_id2 is not null) then
5182       into gl_import_references (
5183                      je_batch_id,
5184                      je_header_id,
5185                      je_line_num,
5186                      last_update_date,
5187                      last_updated_by,
5188                      creation_date,
5189                      created_by,
5190                      last_update_login,
5191                      reference_1,
5192                      reference_2,
5193                      reference_3,
5194                      reference_4,
5195                      reference_5,
5196                      reference_6,
5197                      reference_7,
5198                      reference_8,
5199                      reference_9,
5200                      reference_10,
5201                      subledger_doc_sequence_id,
5202                      subledger_doc_sequence_value,
5203                      gl_sl_link_id,
5204                      gl_sl_link_table
5205       ) values (
5206                      je_batch_id2,                -- je_batch_id
5207                      je_header_id2,               -- je_header_id
5208                      je_line_num2,                -- je_line_num
5209                      sysdate,                     -- last_update_date
5210                      c_fnd_user,                  -- last_updated_by
5211                      sysdate,                     -- creation_date
5212                      c_fnd_user,                  -- created_by
5213                      c_upgrade_bugno,             -- last_update_login
5214                      to_char(transaction_header_id),
5215                                                   -- reference_1
5216                      to_char(asset_id),           -- reference_2
5217                      to_char(distribution_id),    -- reference_3
5218                      to_char(adj_line_id),        -- reference_4
5219                      book_type_code,              -- reference_5
5220                      to_char(period_counter),     -- reference_6
5221                      null,                        -- reference_7
5222                      null,                        -- reference_8
5223                      null,                        -- reference_9
5224                      null,                        -- reference_10
5225                      null,                        -- subledger_doc_seq_id
5226                      null,                        -- subledger_doc_seq_value
5227                      xla_gl_sl_link_id_s.currval, -- gl_sl_link_id
5228                      'XLAJEL'                     -- gl_sl_link_table
5229       )
5230       when (adj_row = 3) then
5231       into xla_ae_lines (
5232                   upg_batch_id,
5233                   ae_header_id,
5234                   ae_line_num,
5235                   displayed_line_number,
5236                   application_id,
5237                   code_combination_id,
5238                   gl_transfer_mode_code,
5239                   accounted_dr,
5240                   accounted_cr,
5241                   currency_code,
5242                   currency_conversion_date,
5243                   currency_conversion_rate,
5244                   currency_conversion_type,
5245                   entered_dr,
5246                   entered_cr,
5247                   description,
5248                   accounting_class_code,
5249                   gl_sl_link_id,
5250                   gl_sl_link_table,
5251                   party_type_code,
5252                   party_id,
5253                   party_site_id,
5254                   statistical_amount,
5255                   ussgl_transaction_code,
5256                   jgzz_recon_ref,
5257                   control_balance_flag,
5258                   analytical_balance_flag,
5259                   upg_tax_reference_id1,
5260                   upg_tax_reference_id2,
5261                   upg_tax_reference_id3,
5262                   creation_date,
5263                   created_by,
5264                   last_update_date,
5265                   last_updated_by,
5266                   last_update_login,
5267                   program_update_date,
5268                   program_id,
5269                   program_application_id,
5270                   request_id,
5271                   gain_or_loss_flag,
5272                   accounting_date,
5273                   ledger_id
5274             ) values (
5275                   l_upg_batch_id,                 -- upg_batch_id
5276                   ae_header_id,                   -- ae_header_id
5277                   ae_line_num,                    -- ae_line_num
5278                   ae_line_num,                    -- displayed_line_num
5279                   c_application_id,               -- application_id
5280                   ccid3,                          -- code_combination_id
5281                   'S',                            -- gl_transfer_mode_code
5282                   -- Fix for Bug #5131737.  Need to switch CR/DR for pseudo-row
5283                   credit_amount,                  -- accounted_dr
5284                   debit_amount,                   -- accounted_cr
5285                   currency_code,                  -- currency_code
5286                   null,                           -- currency_conversion_date
5287                   null,                           -- currency_conversion_rate
5288                   null,                           -- currency_conversion_type
5289                   credit_amount,                  -- entered_dr
5290                   debit_amount,                   -- entered_cr
5291                   l_bonus_deprn_rsv_desc || ' - ' ||
5292                      to_char(cal_period_close_date, 'DD-MON-RR'),
5293                                                   -- description
5294                   'ASSET',                        -- accounting_class_code
5295                   xla_gl_sl_link_id_s.nextval,    -- gl_sl_link_id
5296                   'XLAJEL',                       -- gl_sl_link_table
5297                   null,                           -- party_type_code
5298                   null,                           -- party_id
5299                   null,                           -- party_site_id
5300                   null,                           -- statistical_amount
5301                   null,                           -- ussgl_transaction_code
5302                   null,                           -- glzz_recon_ref
5303                   null,                           -- control_balance_flag
5304                   null,                           -- analytical_balance_flag
5305                   null,                           -- upg_tax_reference_id1
5306                   null,                           -- upg_tax_reference_id2
5307                   null,                           -- upg_tax_reference_id3
5308                   sysdate,                        -- creation_date
5309                   c_fnd_user,                     -- created_by
5310                   sysdate,                        -- last_update_date
5311                   c_fnd_user,                     -- last_updated_by
5312                   c_upgrade_bugno,                -- last_update_login
5313                   null,                           -- program_update_date
5314                   null,                           -- program_id
5315                   c_application_id,               -- program_application_id
5316                   null,                           -- request_id
5317                   'N',                            -- gain_or_loss_flag
5318                   cal_period_close_date,          -- accounting_date
5319                   set_of_books_id                 -- ledger_id
5320             )
5321       when (adj_row = 3) then
5322       into xla_distribution_links (
5323                   upg_batch_id,
5324                   application_id,
5325                   event_id,
5326                   ae_header_id,
5327                   ae_line_num,
5328                   accounting_line_code,
5329                   accounting_line_type_code,
5330                   source_distribution_type,
5331                   source_distribution_id_char_1,
5332                   source_distribution_id_char_2,
5333                   source_distribution_id_char_3,
5334                   source_distribution_id_char_4,
5335                   source_distribution_id_char_5,
5336                   source_distribution_id_num_1,
5337                   source_distribution_id_num_2,
5338                   source_distribution_id_num_3,
5339                   source_distribution_id_num_4,
5340                   source_distribution_id_num_5,
5341                   merge_duplicate_code,
5342                   statistical_amount,
5343                   unrounded_entered_dr,
5344                   unrounded_entered_cr,
5345                   unrounded_accounted_dr,
5346                   unrounded_accounted_cr,
5347                   ref_ae_header_id,
5348                   ref_temp_line_num,
5349                   ref_event_id,
5350                   temp_line_num,
5351                   tax_line_ref_id,
5352                   tax_summary_line_ref_id,
5353                   tax_rec_nrec_dist_ref_id,
5354                   line_definition_owner_code,
5355                   line_definition_code,
5356                   event_class_code,
5357                   event_type_code
5358       ) values (
5359                   l_upg_batch_id,              -- upg_batch_id
5360                   c_application_id,            -- application_id
5361                   event_id,                    -- event_id
5362                   ae_header_id,                -- ae_header_id
5363                   ae_line_num,                 -- ae_line_num
5364                   null,                        -- accounting_line_code
5365                   'S',                         -- accounting_line_type_code
5366                   'TRX',                       -- source_distribution_type
5367                   null,                        -- source_distribution_id_char_1
5368                   null,                        -- source_distribution_id_char_2
5369                   null,                        -- source_distribution_id_char_3
5370                   null,                        -- source_distribution_id_char_4
5371                   null,                        -- source_distribution_id_char_5
5372                   transaction_header_id,       -- source_distribution_id_num_1
5373                   adj_line_id,                 -- source_distribution_id_num_2
5374                   null,                        -- source_distribution_id_num_3
5375                   null,                        -- source_distribution_id_num_4
5376                   null,                        -- source_distribution_id_num_5
5377                   'N',                         -- merge_duplicate_code
5378                   null,                        -- statistical_amount
5379                   -- Fix for Bug #5131737.  Need to switch CR/DR for pseudo-row
5380                   credit_amount,               -- unrounded_entered_dr
5381                   debit_amount,                -- unrounded_entered_cr
5382                   credit_amount,               -- unrounded_accounted_dr
5383                   debit_amount,                -- unrounded_accounted_cr
5384                   ae_header_id,                -- ref_ae_header_id
5385                   null,                        -- ref_temp_line_num
5386                   null,                        -- ref_event_id
5387                   ae_line_num,                 -- temp_line_num
5388                   null,                        -- tax_line_ref_id
5389                   null,                        -- tax_summary_line_ref_id
5390                   null,                        -- tax_rec_nrec_dist_ref_id
5391                   null,                        -- line_definition_owner_code
5392                   null,                        -- line_definition_code
5393                   event_class_code,            -- event_class_code
5394                   event_type_code              -- event_type_code
5395       )
5396       when (adj_row = 3) and (je_batch_id3 is not null) then
5397       into gl_import_references (
5398                      je_batch_id,
5399                      je_header_id,
5400                      je_line_num,
5401                      last_update_date,
5402                      last_updated_by,
5403                      creation_date,
5404                      created_by,
5405                      last_update_login,
5406                      reference_1,
5407                      reference_2,
5408                      reference_3,
5409                      reference_4,
5410                      reference_5,
5411                      reference_6,
5412                      reference_7,
5413                      reference_8,
5414                      reference_9,
5415                      reference_10,
5416                      subledger_doc_sequence_id,
5417                      subledger_doc_sequence_value,
5418                      gl_sl_link_id,
5419                      gl_sl_link_table
5420       ) values (
5421                      je_batch_id3,                -- je_batch_id
5422                      je_header_id3,               -- je_header_id
5423                      je_line_num3,                -- je_line_num
5424                      sysdate,                     -- last_update_date
5425                      c_fnd_user,                  -- last_updated_by
5426                      sysdate,                     -- creation_date
5427                      c_fnd_user,                  -- created_by
5428                      c_upgrade_bugno,             -- last_update_login
5429                      to_char(transaction_header_id),
5430                                                   -- reference_1
5431                      to_char(asset_id),           -- reference_2
5432                      to_char(distribution_id),    -- reference_3
5433                      to_char(adj_line_id),        -- reference_4
5434                      book_type_code,              -- reference_5
5435                      to_char(period_counter),     -- reference_6
5436                      null,                        -- reference_7
5437                      null,                        -- reference_8
5438                      null,                        -- reference_9
5439                      null,                        -- reference_10
5440                      null,                        -- subledger_doc_seq_id
5441                      null,                        -- subledger_doc_seq_value
5442                      xla_gl_sl_link_id_s.currval, -- gl_sl_link_id
5443                      'XLAJEL'                     -- gl_sl_link_table
5444       )
5445        -- Bug 7498880: Added hint to use FA_ASSET_HISTORY_N2 index
5446        select /*+ ordered index(adj, FA_ADJUSTMENTS_U1) use_nl(cb) index(cb, fa_category_books_u1) index(AH FA_ASSET_HISTORY_N2)*/
5447              adj.adjustment_line_id                    adj_line_id,
5448              decode(adj.debit_credit_flag,
5449                     'DR', adj.adjustment_amount, null) debit_amount,
5450              decode(adj.debit_credit_flag,
5451                     'CR', adj.adjustment_amount, null) credit_amount,
5452              adj.code_combination_id                   ccid1,
5453              nvl(nvl(gljl2.code_combination_id,
5454                      da.deprn_reserve_account_ccid),
5455                      cb.reserve_account_ccid)          ccid2,
5456              nvl(nvl(gljl3.code_combination_id,
5457                      da.deprn_reserve_account_ccid),
5458                      cb.reserve_account_ccid)          ccid3,
5459              glsob.currency_code                       currency_code,
5460              decode (adj.adjustment_type,
5461                 'BONUS EXPENSE', 'EXPENSE',
5462                 'BONUS RESERVE', 'ASSET',
5463                 'CIP COST', 'ASSET',
5464                 'COST', 'ASSET',
5465                 'COST CLEARING', 'ASSET',
5466                 'DEPRN ADJUST', 'EXPENSE',
5467                 'EXPENSE', 'EXPENSE',
5468                 'GRP COR RESERVE', 'ASSET',
5469                 'GRP PRC RESERVE', 'ASSET',
5470                 'INTERCO AP', 'LIABILITY',
5471                 'INTERCO AR', 'ASSET',
5472                 'NBV RETIRED', 'ASSET',
5473                 'PROCEEDS', 'ASSET',
5474                 'PROCEEDS CLR', 'ASSET',
5475                 'REMOVALCOST', 'ASSET',
5476                 'REMOVALCOST CLR', 'ASSET',
5477                 'RESERVE', 'ASSET',
5478                 'REVAL RESERVE', 'ASSET',
5479                 'REVAL RSV RET', 'ASSET',
5480                 'REVAL AMORT', 'EXPENSE',
5481                 'REVAL EXPENSE', 'EXPENSE',
5482                 'ASSET')                               accounting_class_code,
5483              lk.description                            line_desc1,
5484              lk2.description                           line_desc2,
5485              decode(adj.je_header_id, null, 'N', 'Y')  gl_transfer_status_code,
5486              gljh.je_batch_id                          je_batch_id1,
5487              gljh2.je_batch_id                         je_batch_id2,
5488              gljh2.je_batch_id                         je_batch_id3,
5489              adj.je_header_id                          je_header_id1,
5490              dd.je_header_id                           je_header_id2,
5491              dd.je_header_id                           je_header_id3,
5492              nvl(adj.je_line_num, 0)                   je_line_num1,
5493              nvl(dd.deprn_reserve_je_line_num,
5494                 nvl(adj.je_line_num, 0))               je_line_num2,
5495              nvl(dd.bonus_deprn_rsv_je_line_num,
5496                 nvl(adj.je_line_num, 0))               je_line_num3,
5497              adj.distribution_id                       distribution_id,
5498              ev.event_id                               event_id,
5499              he.ae_header_id                           ae_header_id,
5500              ev.calendar_period_close_date             cal_period_close_date,
5501              ev.transaction_header_id                  transaction_header_id,
5502              ev.event_type_code                        event_type_code,
5503              ev.event_class_code                       event_class_code,
5504              ev.asset_id                               asset_id,
5505              ev.book_type_code                         book_type_code,
5506              he.set_of_books_id                        set_of_books_id,
5507              ev.period_counter                         period_counter,
5508              adj.source_type_code                      source_type_code,
5509              adj.adjustment_type                       adjustment_type,
5510              row_number() over
5511                 (partition by ev.transaction_header_id
5512                  order by adj.adjustment_line_id, mult.adj_row)    ae_line_num,
5513              mult.adj_row                         adj_row
5514       from
5515              fa_xla_upg_events_gt ev,
5516              fa_xla_upg_headers_gt he,
5517              fa_adjustments adj,
5518              fa_lookups_tl lk,
5519              fa_lookups_tl lk2,
5520              gl_je_headers gljh,
5521              fa_deprn_detail dd,
5522              gl_je_headers gljh2,
5523              gl_je_lines gljl2,
5524              gl_je_lines gljl3,
5525              fa_book_controls bc,
5526              gl_sets_of_books glsob,
5527              fa_distribution_accounts da,
5528              fa_asset_history ah,
5529              fa_category_books cb,
5530              (select 1 adj_row from dual
5531               union all
5532               select 2 adj_row from dual
5533               union all
5534               select 3 adj_row from dual) mult
5535       where  ev.event_id = he.event_id
5536       and    bc.book_type_code = ev.book_type_code
5537       and    bc.set_of_books_id = glsob.set_of_books_id
5538       and    bc.book_type_code = adj.book_type_code
5539       and    he.set_of_books_id = bc.set_of_books_id
5540       and    adj.asset_id = ev.asset_id
5541       and    adj.book_type_code = ev.book_type_code
5542       and    adj.period_counter_created = ev.period_counter
5543       and    adj.transaction_header_id = ev.transaction_header_id
5544       and    adj.code_combination_id is not null
5545       and    nvl(adj.track_member_flag, 'N') = 'N'
5546       and    lk.lookup_type = 'JOURNAL ENTRIES'
5547       and    lk.lookup_code = decode (adj.adjustment_type,
5548                               'BONUS EXPENSE', 'BONUS DEPRECIATION EXPENSE',
5549                               'BONUS RESERVE', 'BONUS DEPRECIATION RESERVE',
5550                               'CIP COST', adj.source_type_code ||' COST',
5551                               adj.source_type_code ||' '|| adj.adjustment_type)
5552       and    lk2.lookup_type = 'JOURNAL ENTRIES'
5553       and    lk2.lookup_code = decode(adj.adjustment_type,
5554                                'EXPENSE', adj.source_type_code ||' RESERVE',
5555                                'BONUS EXPENSE', 'BONUS DEPRECIATION RESERVE',
5556                                'BONUS RESERVE', 'BONUS DEPRECIATION RESERVE',
5557                                'CIP COST', adj.source_type_code ||' COST',
5558                                adj.source_type_code ||' '||adj.adjustment_type)
5559       and    userenv('LANG') = lk.language
5560       and    userenv('LANG') = lk2.language
5561       and    adj.je_header_id = gljh.je_header_id (+)
5562       and    adj.asset_id = dd.asset_id (+)
5563       and    adj.book_type_code = dd.book_type_code (+)
5564       and    adj.distribution_id = dd.distribution_id (+)
5565       and    adj.period_counter_created = dd.period_counter (+)
5566       and    dd.je_header_id = gljl2.je_header_id (+)
5567       and    dd.deprn_reserve_je_line_num = gljl2.je_line_num (+)
5568       and    dd.je_header_id = gljh2.je_header_id (+)
5569       and    dd.je_header_id = gljl3.je_header_id (+)
5570       and    dd.bonus_deprn_rsv_je_line_num = gljl3.je_line_num (+)
5571       and    adj.book_type_code = da.book_type_code (+)
5572       and    adj.distribution_id = da.distribution_id (+)
5573       and    ev.asset_id = ah.asset_id
5574       and    ev.date_effective >= ah.date_effective
5575       and    ev.date_effective < nvl(ah.date_ineffective, sysdate+1)
5576       and    ah.category_id = cb.category_id
5577       and    bc.book_type_code = cb.book_type_code
5578       and    ((mult.adj_row = 1)
5579            or ((mult.adj_row = 2) and
5580                (adjustment_type = 'EXPENSE') and
5581                (source_type_code in
5582                   ('DEPRECIATION', 'CIP RETIREMENT', 'RETIREMENT')))
5583            or ((mult.adj_row = 3) and
5584                (adjustment_type = 'BONUS EXPENSE') and
5585                (source_type_code = 'DEPRECIATION')));
5586 
5587    select count(*)
5588    into   l_mc_books
5589    from   fa_mc_book_controls
5590    where  enabled_flag = 'Y'
5591    and    rownum = 1;
5592 
5593    if (l_mc_books > 0) then
5594 
5595    insert all
5596    when 1 = 1 then
5597       into fa_xla_upg_headers_gt (
5598          ae_header_id,
5599          event_id,
5600          set_of_books_id
5601       ) values (
5602          xla_ae_headers_s.nextval,
5603          event_id,
5604          set_of_books_id
5605       )
5606    when 1 = 1 then
5607       into xla_ae_headers (
5608             upg_batch_id,
5609             application_id,
5610             amb_context_code,
5611             entity_id,
5612             event_id,
5613             event_type_code,
5614             ae_header_id,
5615             ledger_id,
5616             accounting_date,
5617             period_name,
5618             reference_date,
5619             balance_type_code,
5620             je_category_name,
5621             gl_transfer_status_code,
5622             gl_transfer_date,
5623             accounting_entry_status_code,
5624             accounting_entry_type_code,
5625             description,
5626             budget_version_id,
5627             funds_status_code,
5628             encumbrance_type_id,
5629             completed_date,
5630             doc_sequence_id,
5631             doc_sequence_value,
5632             doc_category_code,
5633             packet_id,
5634             group_id,
5635             creation_date,
5636             created_by,
5637             last_update_date,
5638             last_updated_by,
5639             last_update_login,
5640             program_id,
5641             program_application_id,
5642             program_update_date,
5643             request_id,
5644             close_acct_seq_assign_id,
5645             close_acct_seq_version_id,
5646             close_acct_seq_value,
5647             completion_acct_seq_assign_id,
5648             completion_acct_seq_version_id,
5649             completion_acct_seq_value,
5650             accounting_batch_id,
5651             product_rule_type_code,
5652             product_rule_code,
5653             product_rule_version,
5654             upg_source_application_id,
5655             upg_valid_flag
5656       ) values (
5657             l_upg_batch_id,             -- upg_batch_id
5658             c_application_id,           -- application_id
5659             c_amb_context_code,         -- amb_context_code
5660             entity_id,                  -- entity_id
5661             event_id,                   -- event_id
5662             event_type_code,            -- event_type_code
5663             xla_ae_headers_s.currval,   -- ae_header_id
5664             set_of_books_id,            -- ledger_id
5665             calendar_period_close_date, -- accounting_date
5666             period_name,                -- period_name
5667             null,                       -- reference_date
5668             'A',                        -- balance_type_code
5669             je_category_name,           -- je_category_name
5670             'Y',                        -- gl_transfer_status_code -- Need to revisit
5671             null,                       -- gl_transfer_date
5672             'F',                        -- accounting_entry_status_code
5673             'STANDARD',                 -- accounting_entry_type_code
5674             description,                -- description
5675             null,                       -- budget_version_id
5676             null,                       -- funds_status_code
5677             null,                       -- encumbrance_type_id
5678             null,                       -- completed_date
5679             null,                       -- doc_sequence_id
5680             null,                       -- doc_sequence_value
5681             null,                       -- doc_category_code
5682             null,                       -- packet_id
5683             null,                       -- group_id
5684             sysdate,                    -- creation_date
5685             c_fnd_user,                 -- created_by
5686             sysdate,                    -- last_update_date
5687             c_fnd_user,                 -- last_updated_by
5688             c_upgrade_bugno,            -- last_update_login
5689             null,                       -- program_id
5690             c_application_id,           -- program_application_id
5691             sysdate,                    -- program_update_date
5692             null,                       -- request_id
5693             null,                       -- close_acct_seq_assign_id
5694             null,                       -- close_acct_seq_version_id
5695             null,                       -- close_acct_seq_value
5696             null,                       -- completion_acct_seq_assign_id
5697             null,                       -- completion_acct_seq_version_id
5698             null,                       -- completion_acct_seq_value
5699             null,                       -- accounting_batch_id
5700             null,                       -- product_rule_type_code
5701             null,                       -- product_rule_code
5702             null,                       -- product_rule_version
5703             c_application_id,           -- upg_source_application_id
5704             null                        -- upg_valid_flag
5705       )
5706       select faev.entity_id         entity_id,
5707              faev.event_id          event_id,
5708              faev.event_type_code   event_type_code,
5709              mc.set_of_books_id     set_of_books_id,
5710              faev.calendar_period_close_date
5711                                     calendar_period_close_date,
5712              faev.period_name       period_name,
5713              faev.je_category_name  je_category_name,
5714              faev.description       description
5715       from   fa_xla_upg_events_gt faev,
5716              fa_mc_book_controls mc
5717       where  mc.book_type_code = faev.book_type_code
5718       and    mc.enabled_flag = 'Y';
5719 
5720    insert all
5721       when (adj_row = 1) then
5722       into xla_ae_lines (
5723                   upg_batch_id,
5724                   ae_header_id,
5725                   ae_line_num,
5726                   displayed_line_number,
5727                   application_id,
5728                   code_combination_id,
5729                   gl_transfer_mode_code,
5730                   accounted_dr,
5731                   accounted_cr,
5732                   currency_code,
5733                   currency_conversion_date,
5734                   currency_conversion_rate,
5735                   currency_conversion_type,
5736                   entered_dr,
5737                   entered_cr,
5738                   description,
5739                   accounting_class_code,
5740                   gl_sl_link_id,
5741                   gl_sl_link_table,
5742                   party_type_code,
5743                   party_id,
5744                   party_site_id,
5745                   statistical_amount,
5746                   ussgl_transaction_code,
5747                   jgzz_recon_ref,
5748                   control_balance_flag,
5749                   analytical_balance_flag,
5750                   upg_tax_reference_id1,
5751                   upg_tax_reference_id2,
5752                   upg_tax_reference_id3,
5753                   creation_date,
5754                   created_by,
5755                   last_update_date,
5756                   last_updated_by,
5757                   last_update_login,
5758                   program_update_date,
5759                   program_id,
5760                   program_application_id,
5761                   request_id,
5762                   gain_or_loss_flag,
5763                   accounting_date,
5764                   ledger_id
5765             ) values (
5766                   l_upg_batch_id,                 -- upg_batch_id
5767                   ae_header_id,                   -- ae_header_id
5768                   ae_line_num,                    -- ae_line_num
5769                   ae_line_num,                    -- displayed_line_num
5770                   c_application_id,               -- application_id
5771                   ccid1,                          -- code_combination_id
5772                   'S',                            -- gl_transfer_mode_code
5773                   debit_amount,                   -- accounted_dr
5774                   credit_amount,                  -- accounted_cr
5775                   currency_code,                  -- currency_code
5776                   null,                           -- currency_conversion_date
5777                   null,                           -- currency_conversion_rate
5778                   null,                           -- currency_conversion_type
5779                   debit_amount,                   -- entered_dr
5780                   credit_amount,                  -- entered_cr
5781                   line_desc1 || ' - ' ||
5782                      to_char(cal_period_close_date, 'DD-MON-RR'),
5783                                                   -- description
5784                   accounting_class_code,          -- accounting_class_code
5785                   xla_gl_sl_link_id_s.nextval,    -- gl_sl_link_id
5786                   'XLAJEL',                       -- gl_sl_link_table
5787                   null,                           -- party_type_code
5788                   null,                           -- party_id
5789                   null,                           -- party_site_id
5790                   null,                           -- statistical_amount
5791                   null,                           -- ussgl_transaction_code
5792                   null,                           -- glzz_recon_ref
5793                   null,                           -- control_balance_flag
5794                   null,                           -- analytical_balance_flag
5795                   null,                           -- upg_tax_reference_id1
5796                   null,                           -- upg_tax_reference_id2
5797                   null,                           -- upg_tax_reference_id3
5798                   sysdate,                        -- creation_date
5799                   c_fnd_user,                     -- created_by
5800                   sysdate,                        -- last_update_date
5801                   c_fnd_user,                     -- last_updated_by
5802                   c_upgrade_bugno,                -- last_update_login
5803                   null,                           -- program_update_date
5804                   null,                           -- program_id
5805                   c_application_id,               -- program_application_id
5806                   null,                           -- request_id
5807                   'N',                            -- gain_or_loss_flag
5808                   cal_period_close_date,          -- accounting_date
5809                   set_of_books_id                 -- ledger_id
5810             )
5811       when (adj_row = 1) then
5812       into xla_distribution_links (
5813                   upg_batch_id,
5814                   application_id,
5815                   event_id,
5816                   ae_header_id,
5817                   ae_line_num,
5818                   accounting_line_code,
5819                   accounting_line_type_code,
5820                   source_distribution_type,
5821                   source_distribution_id_char_1,
5822                   source_distribution_id_char_2,
5823                   source_distribution_id_char_3,
5824                   source_distribution_id_char_4,
5825                   source_distribution_id_char_5,
5826                   source_distribution_id_num_1,
5827                   source_distribution_id_num_2,
5828                   source_distribution_id_num_3,
5829                   source_distribution_id_num_4,
5830                   source_distribution_id_num_5,
5831                   merge_duplicate_code,
5832                   statistical_amount,
5833                   unrounded_entered_dr,
5834                   unrounded_entered_cr,
5835                   unrounded_accounted_dr,
5836                   unrounded_accounted_cr,
5837                   ref_ae_header_id,
5838                   ref_temp_line_num,
5839                   ref_event_id,
5840                   temp_line_num,
5841                   tax_line_ref_id,
5842                   tax_summary_line_ref_id,
5843                   tax_rec_nrec_dist_ref_id,
5844                   line_definition_owner_code,
5845                   line_definition_code,
5846                   event_class_code,
5847                   event_type_code
5848       ) values (
5849                   l_upg_batch_id,              -- upg_batch_id
5850                   c_application_id,            -- application_id
5851                   event_id,                    -- event_id
5852                   ae_header_id,                -- ae_header_id
5853                   ae_line_num,                 -- ae_line_num
5854                   null,                        -- accounting_line_code
5855                   'S',                         -- accounting_line_type_code
5856                   'TRX',                       -- source_distribution_type
5857                   null,                        -- source_distribution_id_char_1
5858                   null,                        -- source_distribution_id_char_2
5859                   null,                        -- source_distribution_id_char_3
5860                   null,                        -- source_distribution_id_char_4
5861                   null,                        -- source_distribution_id_char_5
5862                   transaction_header_id,       -- source_distribution_id_num_1
5863                   adj_line_id,                 -- source_distribution_id_num_2
5864                   null,                        -- source_distribution_id_num_3
5865                   null,                        -- source_distribution_id_num_4
5866                   null,                        -- source_distribution_id_num_5
5867                   'N',                         -- merge_duplicate_code
5868                   null,                        -- statistical_amount
5869                   debit_amount,                -- unrounded_entered_dr
5870                   credit_amount,               -- unrounded_entered_cr
5871                   debit_amount,                -- unrounded_accounted_dr
5872                   credit_amount,               -- unrounded_accounted_cr
5873                   ae_header_id,                -- ref_ae_header_id
5874                   null,                        -- ref_temp_line_num
5875                   null,                        -- ref_event_id
5876                   ae_line_num,                 -- temp_line_num
5877                   null,                        -- tax_line_ref_id
5878                   null,                        -- tax_summary_line_ref_id
5879                   null,                        -- tax_rec_nrec_dist_ref_id
5880                   null,                        -- line_definition_owner_code
5881                   null,                        -- line_definition_code
5882                   event_class_code,            -- event_class_code
5883                   event_type_code              -- event_type_code
5884       )
5885       when (adj_row = 1) and (je_batch_id1 is not null) then
5886       into gl_import_references (
5887                      je_batch_id,
5888                      je_header_id,
5889                      je_line_num,
5890                      last_update_date,
5891                      last_updated_by,
5892                      creation_date,
5893                      created_by,
5894                      last_update_login,
5895                      reference_1,
5896                      reference_2,
5897                      reference_3,
5898                      reference_4,
5899                      reference_5,
5900                      reference_6,
5901                      reference_7,
5902                      reference_8,
5903                      reference_9,
5904                      reference_10,
5905                      subledger_doc_sequence_id,
5906                      subledger_doc_sequence_value,
5907                      gl_sl_link_id,
5908                      gl_sl_link_table
5909       ) values (
5910                      je_batch_id1,                -- je_batch_id
5911                      je_header_id1,               -- je_header_id
5912                      je_line_num1,                -- je_line_num
5913                      sysdate,                     -- last_update_date
5914                      c_fnd_user,                  -- last_updated_by
5915                      sysdate,                     -- creation_date
5916                      c_fnd_user,                  -- created_by
5917                      c_upgrade_bugno,             -- last_update_login
5918                      to_char(transaction_header_id),
5919                                                   -- reference_1
5920                      to_char(asset_id),           -- reference_2
5921                      to_char(distribution_id),    -- reference_3
5922                      to_char(adj_line_id),        -- reference_4
5923                      book_type_code,              -- reference_5
5924                      to_char(period_counter),     -- reference_6
5925                      null,                        -- reference_7
5926                      null,                        -- reference_8
5927                      null,                        -- reference_9
5928                      null,                        -- reference_10
5929                      null,                        -- subledger_doc_seq_id
5930                      null,                        -- subledger_doc_seq_value
5931                      xla_gl_sl_link_id_s.currval, -- gl_sl_link_id
5932                      'XLAJEL'                     -- gl_sl_link_table
5933       )
5934       when (adj_row = 2) then
5935       into xla_ae_lines (
5936                   upg_batch_id,
5937                   ae_header_id,
5938                   ae_line_num,
5939                   displayed_line_number,
5940                   application_id,
5941                   code_combination_id,
5942                   gl_transfer_mode_code,
5943                   accounted_dr,
5944                   accounted_cr,
5945                   currency_code,
5946                   currency_conversion_date,
5947                   currency_conversion_rate,
5948                   currency_conversion_type,
5949                   entered_dr,
5950                   entered_cr,
5951                   description,
5952                   accounting_class_code,
5953                   gl_sl_link_id,
5954                   gl_sl_link_table,
5955                   party_type_code,
5956                   party_id,
5957                   party_site_id,
5958                   statistical_amount,
5959                   ussgl_transaction_code,
5960                   jgzz_recon_ref,
5961                   control_balance_flag,
5962                   analytical_balance_flag,
5963                   upg_tax_reference_id1,
5964                   upg_tax_reference_id2,
5965                   upg_tax_reference_id3,
5966                   creation_date,
5967                   created_by,
5968                   last_update_date,
5969                   last_updated_by,
5970                   last_update_login,
5971                   program_update_date,
5972                   program_id,
5973                   program_application_id,
5974                   request_id,
5975                   gain_or_loss_flag,
5976                   accounting_date,
5977                   ledger_id
5978             ) values (
5979                   l_upg_batch_id,                 -- upg_batch_id
5980                   ae_header_id,                   -- ae_header_id
5981                   ae_line_num,                    -- ae_line_num
5982                   ae_line_num,                    -- displayed_line_num
5983                   c_application_id,               -- application_id
5984                   ccid2,                          -- code_combination_id
5985                   'S',                            -- gl_transfer_mode_code
5986                   -- Fix for Bug #5131737.  Need to switch CR/DR for pseudo-row
5987                   credit_amount,                  -- accounted_dr
5988                   debit_amount,                   -- accounted_cr
5989                   currency_code,                  -- currency_code
5990                   null,                           -- currency_conversion_date
5991                   null,                           -- currency_conversion_rate
5992                   null,                           -- currency_conversion_type
5993                   credit_amount,                  -- entered_dr
5994                   debit_amount,                   -- entered_cr
5995                   line_desc2 || ' - ' ||
5996                      to_char(cal_period_close_date, 'DD-MON-RR'),
5997                                                   -- description
5998                   'ASSET',                        -- accounting_class_code
5999                   xla_gl_sl_link_id_s.nextval,    -- gl_sl_link_id
6000                   'XLAJEL',                       -- gl_sl_link_table
6001                   null,                           -- party_type_code
6002                   null,                           -- party_id
6003                   null,                           -- party_site_id
6004                   null,                           -- statistical_amount
6005                   null,                           -- ussgl_transaction_code
6006                   null,                           -- glzz_recon_ref
6007                   null,                           -- control_balance_flag
6008                   null,                           -- analytical_balance_flag
6009                   null,                           -- upg_tax_reference_id1
6010                   null,                           -- upg_tax_reference_id2
6011                   null,                           -- upg_tax_reference_id3
6012                   sysdate,                        -- creation_date
6013                   c_fnd_user,                     -- created_by
6014                   sysdate,                        -- last_update_date
6015                   c_fnd_user,                     -- last_updated_by
6016                   c_upgrade_bugno,                -- last_update_login
6017                   null,                           -- program_update_date
6018                   null,                           -- program_id
6019                   c_application_id,               -- program_application_id
6020                   null,                           -- request_id
6021                   'N',                            -- gain_or_loss_flag
6022                   cal_period_close_date,          -- accounting_date
6023                   set_of_books_id                 -- ledger_id
6024             )
6025       when (adj_row = 2) then
6026       into xla_distribution_links (
6027                   upg_batch_id,
6028                   application_id,
6029                   event_id,
6030                   ae_header_id,
6031                   ae_line_num,
6032                   accounting_line_code,
6033                   accounting_line_type_code,
6034                   source_distribution_type,
6035                   source_distribution_id_char_1,
6036                   source_distribution_id_char_2,
6037                   source_distribution_id_char_3,
6038                   source_distribution_id_char_4,
6039                   source_distribution_id_char_5,
6040                   source_distribution_id_num_1,
6041                   source_distribution_id_num_2,
6042                   source_distribution_id_num_3,
6043                   source_distribution_id_num_4,
6044                   source_distribution_id_num_5,
6045                   merge_duplicate_code,
6046                   statistical_amount,
6047                   unrounded_entered_dr,
6048                   unrounded_entered_cr,
6049                   unrounded_accounted_dr,
6050                   unrounded_accounted_cr,
6051                   ref_ae_header_id,
6052                   ref_temp_line_num,
6053                   ref_event_id,
6054                   temp_line_num,
6055                   tax_line_ref_id,
6056                   tax_summary_line_ref_id,
6057                   tax_rec_nrec_dist_ref_id,
6058                   line_definition_owner_code,
6059                   line_definition_code,
6060                   event_class_code,
6061                   event_type_code
6062       ) values (
6063                   l_upg_batch_id,              -- upg_batch_id
6064                   c_application_id,            -- application_id
6065                   event_id,                    -- event_id
6066                   ae_header_id,                -- ae_header_id
6067                   ae_line_num,                 -- ae_line_num
6068                   null,                        -- accounting_line_code
6069                   'S',                         -- accounting_line_type_code
6070                   'TRX',                       -- source_distribution_type
6071                   null,                        -- source_distribution_id_char_1
6072                   null,                        -- source_distribution_id_char_2
6073                   null,                        -- source_distribution_id_char_3
6074                   null,                        -- source_distribution_id_char_4
6075                   null,                        -- source_distribution_id_char_5
6076                   transaction_header_id,       -- source_distribution_id_num_1
6077                   adj_line_id,                 -- source_distribution_id_num_2
6078                   null,                        -- source_distribution_id_num_3
6079                   null,                        -- source_distribution_id_num_4
6080                   null,                        -- source_distribution_id_num_5
6081                   'N',                         -- merge_duplicate_code
6082                   null,                        -- statistical_amount
6083                   -- Fix for Bug #5131737.  Need to switch CR/DR for pseudo-row
6084                   credit_amount,               -- unrounded_entered_dr
6085                   debit_amount,                -- unrounded_entered_cr
6086                   credit_amount,               -- unrounded_accounted_dr
6087                   debit_amount,                -- unrounded_accounted_cr
6088                   ae_header_id,                -- ref_ae_header_id
6089                   null,                        -- ref_temp_line_num
6090                   null,                        -- ref_event_id
6091                   ae_line_num,                 -- temp_line_num
6092                   null,                        -- tax_line_ref_id
6093                   null,                        -- tax_summary_line_ref_id
6094                   null,                        -- tax_rec_nrec_dist_ref_id
6095                   null,                        -- line_definition_owner_code
6096                   null,                        -- line_definition_code
6097                   event_class_code,            -- event_class_code
6098                   event_type_code              -- event_type_code
6099       )
6100       when (adj_row = 2) and (je_batch_id2 is not null) then
6101       into gl_import_references (
6102                      je_batch_id,
6103                      je_header_id,
6104                      je_line_num,
6105                      last_update_date,
6106                      last_updated_by,
6107                      creation_date,
6108                      created_by,
6109                      last_update_login,
6110                      reference_1,
6111                      reference_2,
6112                      reference_3,
6113                      reference_4,
6114                      reference_5,
6115                      reference_6,
6116                      reference_7,
6117                      reference_8,
6118                      reference_9,
6119                      reference_10,
6120                      subledger_doc_sequence_id,
6121                      subledger_doc_sequence_value,
6122                      gl_sl_link_id,
6123                      gl_sl_link_table
6124       ) values (
6125                      je_batch_id2,                -- je_batch_id
6126                      je_header_id2,               -- je_header_id
6127                      je_line_num2,                -- je_line_num
6128                      sysdate,                     -- last_update_date
6129                      c_fnd_user,                  -- last_updated_by
6130                      sysdate,                     -- creation_date
6131                      c_fnd_user,                  -- created_by
6132                      c_upgrade_bugno,             -- last_update_login
6133                      to_char(transaction_header_id),
6134                                                   -- reference_1
6135                      to_char(asset_id),           -- reference_2
6136                      to_char(distribution_id),    -- reference_3
6137                      to_char(adj_line_id),        -- reference_4
6138                      book_type_code,              -- reference_5
6139                      to_char(period_counter),     -- reference_6
6140                      null,                        -- reference_7
6141                      null,                        -- reference_8
6142                      null,                        -- reference_9
6143                      null,                        -- reference_10
6144                      null,                        -- subledger_doc_seq_id
6145                      null,                        -- subledger_doc_seq_value
6146                      xla_gl_sl_link_id_s.currval, -- gl_sl_link_id
6147                      'XLAJEL'                     -- gl_sl_link_table
6148       )
6149       when (adj_row = 3) then
6150       into xla_ae_lines (
6151                   upg_batch_id,
6152                   ae_header_id,
6153                   ae_line_num,
6154                   displayed_line_number,
6155                   application_id,
6156                   code_combination_id,
6157                   gl_transfer_mode_code,
6158                   accounted_dr,
6159                   accounted_cr,
6160                   currency_code,
6161                   currency_conversion_date,
6162                   currency_conversion_rate,
6163                   currency_conversion_type,
6164                   entered_dr,
6165                   entered_cr,
6166                   description,
6167                   accounting_class_code,
6168                   gl_sl_link_id,
6169                   gl_sl_link_table,
6170                   party_type_code,
6171                   party_id,
6172                   party_site_id,
6173                   statistical_amount,
6174                   ussgl_transaction_code,
6175                   jgzz_recon_ref,
6176                   control_balance_flag,
6177                   analytical_balance_flag,
6178                   upg_tax_reference_id1,
6179                   upg_tax_reference_id2,
6180                   upg_tax_reference_id3,
6181                   creation_date,
6182                   created_by,
6183                   last_update_date,
6184                   last_updated_by,
6185                   last_update_login,
6186                   program_update_date,
6187                   program_id,
6188                   program_application_id,
6189                   request_id,
6190                   gain_or_loss_flag,
6191                   accounting_date,
6192                   ledger_id
6193             ) values (
6194                   l_upg_batch_id,                 -- upg_batch_id
6195                   ae_header_id,                   -- ae_header_id
6196                   ae_line_num,                    -- ae_line_num
6197                   ae_line_num,                    -- displayed_line_num
6198                   c_application_id,               -- application_id
6199                   ccid3,                          -- code_combination_id
6200                   'S',                            -- gl_transfer_mode_code
6201                   -- Fix for Bug #5131737.  Need to switch CR/DR for pseudo-row
6202                   credit_amount,                  -- accounted_dr
6203                   debit_amount,                   -- accounted_cr
6204                   currency_code,                  -- currency_code
6205                   null,                           -- currency_conversion_date
6206                   null,                           -- currency_conversion_rate
6207                   null,                           -- currency_conversion_type
6208                   credit_amount,                  -- entered_dr
6209                   debit_amount,                   -- entered_cr
6210                   l_bonus_deprn_rsv_desc || ' - ' ||
6211                      to_char(cal_period_close_date, 'DD-MON-RR'),
6212                                                   -- description
6213                   'ASSET',                        -- accounting_class_code
6214                   xla_gl_sl_link_id_s.nextval,    -- gl_sl_link_id
6215                   'XLAJEL',                       -- gl_sl_link_table
6216                   null,                           -- party_type_code
6217                   null,                           -- party_id
6218                   null,                           -- party_site_id
6219                   null,                           -- statistical_amount
6220                   null,                           -- ussgl_transaction_code
6221                   null,                           -- glzz_recon_ref
6222                   null,                           -- control_balance_flag
6223                   null,                           -- analytical_balance_flag
6224                   null,                           -- upg_tax_reference_id1
6225                   null,                           -- upg_tax_reference_id2
6226                   null,                           -- upg_tax_reference_id3
6227                   sysdate,                        -- creation_date
6228                   c_fnd_user,                     -- created_by
6229                   sysdate,                        -- last_update_date
6230                   c_fnd_user,                     -- last_updated_by
6231                   c_upgrade_bugno,                -- last_update_login
6232                   null,                           -- program_update_date
6233                   null,                           -- program_id
6234                   c_application_id,               -- program_application_id
6235                   null,                           -- request_id
6236                   'N',                            -- gain_or_loss_flag
6237                   cal_period_close_date,          -- accounting_date
6238                   set_of_books_id                 -- ledger_id
6239             )
6240       when (adj_row = 3) then
6241       into xla_distribution_links (
6242                   upg_batch_id,
6243                   application_id,
6244                   event_id,
6245                   ae_header_id,
6246                   ae_line_num,
6247                   accounting_line_code,
6248                   accounting_line_type_code,
6249                   source_distribution_type,
6250                   source_distribution_id_char_1,
6251                   source_distribution_id_char_2,
6252                   source_distribution_id_char_3,
6253                   source_distribution_id_char_4,
6254                   source_distribution_id_char_5,
6255                   source_distribution_id_num_1,
6256                   source_distribution_id_num_2,
6257                   source_distribution_id_num_3,
6258                   source_distribution_id_num_4,
6259                   source_distribution_id_num_5,
6260                   merge_duplicate_code,
6261                   statistical_amount,
6262                   unrounded_entered_dr,
6263                   unrounded_entered_cr,
6264                   unrounded_accounted_dr,
6265                   unrounded_accounted_cr,
6266                   ref_ae_header_id,
6267                   ref_temp_line_num,
6268                   ref_event_id,
6269                   temp_line_num,
6270                   tax_line_ref_id,
6271                   tax_summary_line_ref_id,
6272                   tax_rec_nrec_dist_ref_id,
6273                   line_definition_owner_code,
6274                   line_definition_code,
6275                   event_class_code,
6276                   event_type_code
6277       ) values (
6278                   l_upg_batch_id,              -- upg_batch_id
6279                   c_application_id,            -- application_id
6280                   event_id,                    -- event_id
6281                   ae_header_id,                -- ae_header_id
6282                   ae_line_num,                 -- ae_line_num
6283                   null,                        -- accounting_line_code
6284                   'S',                         -- accounting_line_type_code
6285                   'TRX',                       -- source_distribution_type
6286                   null,                        -- source_distribution_id_char_1
6287                   null,                        -- source_distribution_id_char_2
6288                   null,                        -- source_distribution_id_char_3
6289                   null,                        -- source_distribution_id_char_4
6290                   null,                        -- source_distribution_id_char_5
6291                   transaction_header_id,       -- source_distribution_id_num_1
6292                   adj_line_id,                 -- source_distribution_id_num_2
6293                   null,                        -- source_distribution_id_num_3
6294                   null,                        -- source_distribution_id_num_4
6295                   null,                        -- source_distribution_id_num_5
6296                   'N',                         -- merge_duplicate_code
6297                   null,                        -- statistical_amount
6298                   -- Fix for Bug #5131737.  Need to switch CR/DR for pseudo-row
6299                   credit_amount,               -- unrounded_entered_dr
6300                   debit_amount,                -- unrounded_entered_cr
6301                   credit_amount,               -- unrounded_accounted_dr
6302                   debit_amount,                -- unrounded_accounted_cr
6303                   ae_header_id,                -- ref_ae_header_id
6304                   null,                        -- ref_temp_line_num
6305                   null,                        -- ref_event_id
6306                   ae_line_num,                 -- temp_line_num
6307                   null,                        -- tax_line_ref_id
6308                   null,                        -- tax_summary_line_ref_id
6309                   null,                        -- tax_rec_nrec_dist_ref_id
6310                   null,                        -- line_definition_owner_code
6311                   null,                        -- line_definition_code
6312                   event_class_code,            -- event_class_code
6313                   event_type_code              -- event_type_code
6314       )
6315       when (adj_row = 3) and (je_batch_id3 is not null) then
6316       into gl_import_references (
6317                      je_batch_id,
6318                      je_header_id,
6319                      je_line_num,
6320                      last_update_date,
6321                      last_updated_by,
6322                      creation_date,
6323                      created_by,
6324                      last_update_login,
6325                      reference_1,
6326                      reference_2,
6327                      reference_3,
6328                      reference_4,
6329                      reference_5,
6330                      reference_6,
6331                      reference_7,
6332                      reference_8,
6333                      reference_9,
6334                      reference_10,
6335                      subledger_doc_sequence_id,
6336                      subledger_doc_sequence_value,
6337                      gl_sl_link_id,
6338                      gl_sl_link_table
6339       ) values (
6340                      je_batch_id3,                -- je_batch_id
6341                      je_header_id3,               -- je_header_id
6342                      je_line_num3,                -- je_line_num
6343                      sysdate,                     -- last_update_date
6344                      c_fnd_user,                  -- last_updated_by
6345                      sysdate,                     -- creation_date
6346                      c_fnd_user,                  -- created_by
6347                      c_upgrade_bugno,             -- last_update_login
6348                      to_char(transaction_header_id),
6349                                                   -- reference_1
6350                      to_char(asset_id),           -- reference_2
6351                      to_char(distribution_id),    -- reference_3
6352                      to_char(adj_line_id),        -- reference_4
6353                      book_type_code,              -- reference_5
6354                      to_char(period_counter),     -- reference_6
6355                      null,                        -- reference_7
6356                      null,                        -- reference_8
6357                      null,                        -- reference_9
6358                      null,                        -- reference_10
6359                      null,                        -- subledger_doc_seq_id
6360                      null,                        -- subledger_doc_seq_value
6361                      xla_gl_sl_link_id_s.currval, -- gl_sl_link_id
6362                      'XLAJEL'                     -- gl_sl_link_table
6363       )
6364        select /*+ ordered index(adj, FA_MC_ADJUSTMENTS_U1) use_nl(cb) index(cb, fa_category_books_u1) */
6365              adj.adjustment_line_id                    adj_line_id,
6366              decode(adj.debit_credit_flag,
6367                     'DR', adj.adjustment_amount, null) debit_amount,
6368              decode(adj.debit_credit_flag,
6369                     'CR', adj.adjustment_amount, null) credit_amount,
6370              adj.code_combination_id                   ccid1,
6371              nvl(nvl(gljl2.code_combination_id,
6372                      da.deprn_reserve_account_ccid),
6373                      cb.reserve_account_ccid)          ccid2,
6374              nvl(nvl(gljl3.code_combination_id,
6375                      da.deprn_reserve_account_ccid),
6376                      cb.reserve_account_ccid)          ccid3,
6377              glsob.currency_code                       currency_code,
6378              decode (adj.adjustment_type,
6379                 'BONUS EXPENSE', 'EXPENSE',
6380                 'BONUS RESERVE', 'ASSET',
6381                 'CIP COST', 'ASSET',
6382                 'COST', 'ASSET',
6383                 'COST CLEARING', 'ASSET',
6384                 'DEPRN ADJUST', 'EXPENSE',
6385                 'EXPENSE', 'EXPENSE',
6386                 'GRP COR RESERVE', 'ASSET',
6387                 'GRP PRC RESERVE', 'ASSET',
6388                 'INTERCO AP', 'LIABILITY',
6389                 'INTERCO AR', 'ASSET',
6390                 'NBV RETIRED', 'ASSET',
6391                 'PROCEEDS', 'ASSET',
6392                 'PROCEEDS CLR', 'ASSET',
6393                 'REMOVALCOST', 'ASSET',
6394                 'REMOVALCOST CLR', 'ASSET',
6395                 'RESERVE', 'ASSET',
6396                 'REVAL RESERVE', 'ASSET',
6397                 'REVAL RSV RET', 'ASSET',
6398                 'REVAL AMORT', 'EXPENSE',
6399                 'REVAL EXPENSE', 'EXPENSE',
6400                 'ASSET')                               accounting_class_code,
6401              lk.description                            line_desc1,
6402              lk2.description                           line_desc2,
6403              decode(adj.je_header_id, null, 'N', 'Y')  gl_transfer_status_code,
6404              gljh.je_batch_id                          je_batch_id1,
6405              gljh2.je_batch_id                         je_batch_id2,
6406              gljh2.je_batch_id                         je_batch_id3,
6407              adj.je_header_id                          je_header_id1,
6408              dd.je_header_id                           je_header_id2,
6409              dd.je_header_id                           je_header_id3,
6410              nvl(adj.je_line_num, 0)                   je_line_num1,
6411              nvl(dd.deprn_reserve_je_line_num,
6412                 nvl(adj.je_line_num, 0))               je_line_num2,
6413              nvl(dd.bonus_deprn_rsv_je_line_num,
6414                 nvl(adj.je_line_num, 0))               je_line_num3,
6415              adj.distribution_id                       distribution_id,
6416              ev.event_id                               event_id,
6417              he.ae_header_id                           ae_header_id,
6418              ev.calendar_period_close_date             cal_period_close_date,
6419              ev.transaction_header_id                  transaction_header_id,
6420              ev.event_type_code                        event_type_code,
6421              ev.event_class_code                       event_class_code,
6422              ev.asset_id                               asset_id,
6423              ev.book_type_code                         book_type_code,
6424              he.set_of_books_id                        set_of_books_id,
6425              ev.period_counter                         period_counter,
6426              adj.source_type_code                      source_type_code,
6427              adj.adjustment_type                       adjustment_type,
6428              row_number() over
6429                 (partition by ev.transaction_header_id,
6430                               adj.set_of_books_id
6431                  order by adj.adjustment_line_id, mult.adj_row)  ae_line_num,
6432              mult.adj_row                              adj_row
6433       from
6434              fa_xla_upg_events_gt ev,
6435              fa_xla_upg_headers_gt he,
6436              fa_mc_adjustments adj,
6437              fa_lookups_tl lk,
6438              fa_lookups_tl lk2,
6439              gl_je_headers gljh,
6440              fa_mc_deprn_detail dd,
6441              gl_je_headers gljh2,
6442              gl_je_lines gljl2,
6443              gl_je_lines gljl3,
6444              fa_mc_book_controls bc,
6445              gl_sets_of_books glsob,
6446              fa_distribution_accounts da,
6447              fa_asset_history ah,
6448              fa_category_books cb,
6449              (select 1 adj_row from dual
6450               union all
6451               select 2 adj_row from dual
6452               union all
6453               select 3 adj_row from dual) mult
6454       where  ev.event_id = he.event_id
6455       and    bc.book_type_code = ev.book_type_code
6456       and    bc.set_of_books_id = glsob.set_of_books_id
6457       and    bc.book_type_code = adj.book_type_code
6458       and    bc.enabled_flag = 'Y'
6459       and    he.set_of_books_id = bc.set_of_books_id
6460       and    adj.set_of_books_id = bc.set_of_books_id
6461       and    adj.asset_id = ev.asset_id
6462       and    adj.book_type_code = ev.book_type_code
6463       and    adj.period_counter_created = ev.period_counter
6464       and    adj.transaction_header_id = ev.transaction_header_id
6465       and    adj.code_combination_id is not null
6466       and    nvl(adj.track_member_flag, 'N') = 'N'
6467       and    lk.lookup_type = 'JOURNAL ENTRIES'
6468       and    lk.lookup_code = decode (adj.adjustment_type,
6469                               'BONUS EXPENSE', 'BONUS DEPRECIATION EXPENSE',
6470                               'BONUS RESERVE', 'BONUS DEPRECIATION RESERVE',
6471                               'CIP COST', adj.source_type_code ||' COST',
6472                               adj.source_type_code ||' '|| adj.adjustment_type)
6473       and    lk2.lookup_type = 'JOURNAL ENTRIES'
6474       and    lk2.lookup_code = decode(adj.adjustment_type,
6475                                'EXPENSE', adj.source_type_code ||' RESERVE',
6476                                'BONUS EXPENSE', 'BONUS DEPRECIATION RESERVE',
6477                                'BONUS RESERVE', 'BONUS DEPRECIATION RESERVE',
6478                                'CIP COST', adj.source_type_code ||' COST',
6479                                adj.source_type_code ||' '||adj.adjustment_type)
6480       and    userenv('LANG') = lk.language
6481       and    userenv('LANG') = lk2.language
6482       and    adj.je_header_id = gljh.je_header_id (+)
6483       and    adj.asset_id = dd.asset_id (+)
6484       and    adj.book_type_code = dd.book_type_code (+)
6485       and    adj.distribution_id = dd.distribution_id (+)
6486       and    adj.period_counter_created = dd.period_counter (+)
6487       and    adj.set_of_books_id = dd.set_of_books_id (+)
6488       and    dd.je_header_id = gljl2.je_header_id (+)
6489       and    dd.deprn_reserve_je_line_num = gljl2.je_line_num (+)
6490       and    dd.je_header_id = gljh2.je_header_id (+)
6491       and    dd.je_header_id = gljl3.je_header_id (+)
6492       and    dd.bonus_deprn_rsv_je_line_num = gljl3.je_line_num (+)
6493       and    adj.book_type_code = da.book_type_code (+)
6494       and    adj.distribution_id = da.distribution_id (+)
6495       and    ev.asset_id = ah.asset_id
6496       and    ev.date_effective >= ah.date_effective
6497       and    ev.date_effective < nvl(ah.date_ineffective, sysdate+1)
6498       and    ah.category_id = cb.category_id
6499       and    bc.book_type_code = cb.book_type_code
6500       and    ((mult.adj_row = 1)
6501            or ((mult.adj_row = 2) and
6502                (adjustment_type = 'EXPENSE') and
6503                (source_type_code in
6504                   ('DEPRECIATION', 'CIP RETIREMENT', 'RETIREMENT')))
6505            or ((mult.adj_row = 3) and
6506                (adjustment_type = 'BONUS EXPENSE') and
6507                (source_type_code = 'DEPRECIATION')));
6508 
6509    end if;
6510 
6511    commit;
6512 
6513 EXCEPTION
6514    WHEN OTHERS THEN
6515       rollback;
6516       raise;
6517 
6518 End Upgrade_Trxn_Events;
6519 
6520 Procedure Upgrade_Deprn_Events (
6521              p_mode                    IN            varchar,
6522              p_start_rowid             IN            rowid,
6523              p_end_rowid               IN            rowid,
6524              p_batch_size              IN            number,
6525              x_success_count              OUT NOCOPY number,
6526              x_failure_count              OUT NOCOPY number,
6527              x_return_status              OUT NOCOPY number
6528             ) IS
6529 
6530    c_application_id            constant number(15) := 140;
6531    c_upgrade_bugno             constant number(15) := -4107161;
6532    c_fnd_user                  constant number(15) := 2;
6533 
6534 
6535    c_entity_code               constant varchar2(30) := 'DEPRECIATION';
6536    c_amb_context_code          constant varchar2(30) := 'DEFAULT';
6537 
6538    -- this value can be altered in order to process more of less per batch
6539    l_batch_size                NUMBER;
6540 
6541    l_rows_processed            NUMBER;
6542    l_deprn_rows_processed      NUMBER;
6543 
6544    -- type for table variable
6545    type num_tbl_type  is table of number        index by binary_integer;
6546    type char_tbl_type is table of varchar2(150) index by binary_integer;
6547    type date_tbl_type is table of date          index by binary_integer;
6548    type rowid_tbl_type is table of rowid        index by binary_integer;
6549 
6550    l_upg_batch_id                               number;
6551    l_mc_books                                   number;
6552 
6553    l_de_description                             varchar2(80);
6554    l_dr_description                             varchar2(80);
6555    l_be_description                             varchar2(80);
6556    l_br_description                             varchar2(80);
6557    l_ra_description                             varchar2(80);
6558    l_rr_description                             varchar2(80);
6559 
6560    l_event_id_tbl                               num_tbl_type;
6561    l_asset_id_tbl                               num_tbl_type;
6562    l_book_type_code_tbl                         char_tbl_type;
6563    l_period_counter_tbl                         num_tbl_type;
6564 
6565    l_err_entity_id_tbl                          num_tbl_type;
6566    l_err_event_id_tbl                           num_tbl_type;
6567    l_err_ae_header_id_tbl                       num_tbl_type;
6568    l_err_ae_line_num_tbl                        num_tbl_type;
6569    l_err_temp_line_num_tbl                      num_tbl_type;
6570    l_error_message_name_tbl                     char_tbl_type;
6571 
6572 BEGIN
6573 
6574    x_success_count := 0;
6575    x_failure_count := 0;
6576 
6577    l_batch_size := nvl(nvl(p_batch_size, fa_cache_pkg.fa_batch_size), 1000);
6578 
6579    -- Select upg_batch_id
6580    select xla_upg_batches_s.nextval
6581    into   l_upg_batch_id
6582    from   dual;
6583 
6584    select lk_de.description
6585    into   l_de_description
6586    from   fa_lookups_tl lk_de
6587    where  lk_de.lookup_type = 'JOURNAL ENTRIES'
6588    and    lk_de.lookup_code = 'DEPRECIATION EXPENSE'
6589    and    userenv('LANG') = lk_de.language;
6590 
6591    select lk_dr.description
6592    into   l_dr_description
6593    from   fa_lookups_tl lk_dr
6594    where  lk_dr.lookup_type = 'JOURNAL ENTRIES'
6595    and    lk_dr.lookup_code = 'DEPRECIATION RESERVE'
6596    and    userenv('LANG') = lk_dr.language;
6597 
6598    select lk_be.description
6599    into   l_be_description
6600    from   fa_lookups_tl lk_be
6601    where  lk_be.lookup_type = 'JOURNAL ENTRIES'
6602    and    lk_be.lookup_code = 'BONUS DEPRECIATION EXPENSE'
6603    and    userenv('LANG') = lk_be.language;
6604 
6605    select lk_br.description
6606    into   l_br_description
6607    from   fa_lookups_tl lk_br
6608    where  lk_br.lookup_type = 'JOURNAL ENTRIES'
6609    and    lk_br.lookup_code = 'BONUS DEPRECIATION RESERVE'
6610    and    userenv('LANG') = lk_br.language;
6611 
6612    select lk_ra.description
6613    into   l_ra_description
6614    from   fa_lookups_tl lk_ra
6615    where  lk_ra.lookup_type = 'JOURNAL ENTRIES'
6616    and    lk_ra.lookup_code = 'DEPRECIATION REVAL AMORT'
6617    and    userenv('LANG') = lk_ra.language;
6618 
6619    select lk_rr.description
6620    into   l_rr_description
6621    from   fa_lookups_tl lk_rr
6622    where  lk_rr.lookup_type = 'JOURNAL ENTRIES'
6623    and    lk_rr.lookup_code = 'DEPRECIATION REVAL RESERVE'
6624    and    userenv('LANG') = lk_rr.language;
6625 
6626    select count(*)
6627    into   l_mc_books
6628    from   fa_mc_book_controls
6629    where  enabled_flag = 'Y'
6630    and    rownum = 1;
6631 
6632    -- Bug 6811548 : Create events as U/U for downtime
6633    if (p_mode = 'downtime') then
6634 
6635       insert all
6636       when 1 = 1 then
6637          into fa_xla_upg_events_gt (
6638                event_id,
6639                entity_id,
6640                set_of_books_id,
6641                period_name,
6642                calendar_period_close_date,
6643                je_category_name,
6644                event_type_code,
6645                event_class_code,
6646                ds_asset_id,
6647                ds_book_type_code,
6648                ds_period_counter,
6649                reserve_acct_ccid,
6650                bonus_reserve_acct_ccid,
6651                reval_amort_acct_ccid,
6652                reval_reserve_acct_ccid
6653          ) values (
6654                xla_events_s.nextval,
6655                xla_transaction_entities_s.nextval,
6656                set_of_books_id,
6657                period_name,
6658                calendar_period_close_date,
6659                je_category_name,
6660                event_type_code,
6661                event_class_code,
6662                asset_id,
6663                book_type_code,
6664                period_counter,
6665                default_rsv_ccid,
6666                default_bonus_rsv_ccid,
6667                default_reval_amort_ccid,
6668                default_reval_rsv_ccid
6669          )
6670       when 1 = 1 then
6671          into fa_deprn_events (
6672             asset_id,
6673             book_type_code,
6674             period_counter,
6675             deprn_run_date,
6676             deprn_run_id,
6677             event_id,
6678             reversal_event_id,
6679             reversal_date,
6680             creation_date,
6681             created_by,
6682             last_update_date,
6683             last_updated_by,
6684             last_update_login
6685          ) values (
6686             asset_id,
6687             book_type_code,
6688             period_counter,
6689             transaction_date_entered,
6690             1,
6691             xla_events_s.currval,
6692             null,
6693             null,
6694             sysdate,
6695             c_upgrade_bugno,
6696             sysdate,
6697             c_upgrade_bugno,
6698             c_upgrade_bugno
6699          )
6700       when 1 = 1 then
6701          into xla_transaction_entities_upg (
6702                upg_batch_id,
6703                application_id,
6704                ledger_id,
6705                legal_entity_id,
6706                entity_code,
6707                source_id_int_1,
6708                source_id_int_2,
6709                source_id_int_3,
6710                source_id_int_4,
6711                source_id_char_1,
6712                source_id_char_2,
6713                source_id_char_3,
6714                source_id_char_4,
6715                security_id_int_1,
6716                security_id_int_2,
6717                security_id_int_3,
6718                security_id_char_1,
6719                security_id_char_2,
6720                security_id_char_3,
6721                transaction_number,
6722                valuation_method,
6723                source_application_id,
6724                creation_date,
6725                created_by,
6726                last_update_date,
6727                last_updated_by,
6728                last_update_login,
6729                entity_id,
6730                upg_source_application_id
6731          ) values (
6732                l_upg_batch_id,        -- upg_batch_id
6733                c_application_id,      -- application_id
6734                set_of_books_id,       -- ledger_id
6735                null,                  -- legal_entity_id
6736                c_entity_code,         -- entity_code
6737                asset_id,              -- source_id_int_1
6738                period_counter,        -- source_id_int_2
6739                1,                     -- source_id_int_3
6740                null,                  -- source_id_int_4
6741                book_type_code,        -- source_id_char_1
6742                null,                  -- source_id_char_2
6743                null,                  -- source_id_char_3
6744                null,                  -- source_id_char_4
6745                null,                  -- security_id_int_1
6746                null,                  -- security_id_int_2
6747                null,                  -- security_id_int_3
6748                null,                  -- security_id_char_1
6749                null,                  -- security_id_char_2
6750                null,                  -- security_id_char_3
6751                set_of_books_id,       -- transaction_number
6752                book_type_code,        -- valuation_method
6753                c_application_id,      -- source_application_id
6754                sysdate,               -- creation_date
6755                c_fnd_user,            -- created_by
6756                sysdate,               -- last_update_date
6757                c_fnd_user,            -- last_updated_by
6758                c_upgrade_bugno,       -- last_update_login
6759                xla_transaction_entities_s.currval,
6760                                       -- entity_id
6761                c_application_id       -- upg_source_application_id
6762          )
6763       when 1 = 1 then
6764          into xla_events (
6765                upg_batch_id,
6766                application_id,
6767                event_type_code,
6768                event_number,
6769                event_status_code,
6770                process_status_code,
6771                on_hold_flag,
6772                event_date,
6773                creation_date,
6774                created_by,
6775                last_update_date,
6776                last_updated_by,
6777                last_update_login,
6778                program_update_date,
6779                program_id,
6780                program_application_id,
6781                request_id,
6782                entity_id,
6783                event_id,
6784                upg_source_application_id,
6785                transaction_date
6786          ) values (
6787                l_upg_batch_id,        -- upg_batch_id
6788                c_application_id,      -- application_id
6789                event_type_code,       -- event_type_code
6790                1,                     -- event_number
6791                'U',                   -- Bug 6811548 event_status_code
6792                'U',                   -- Bug 6811548 process_status_code
6793                'N',                   -- on_hold_flag
6794                calendar_period_close_date,
6795 --  Bug 7036409             transaction_date_entered,
6796                                       -- event_date
6797                sysdate,               -- creation_date
6798                c_fnd_user,            -- created_by
6799                sysdate,               -- last_update_date
6800                c_fnd_user,            -- last_updated_by
6801                c_upgrade_bugno,       -- last_update_login
6802                null,                  -- program_update_date
6803                null,                  -- program_id
6804                null,                  -- program_application_id
6805                null,                  -- request_id
6806                xla_transaction_entities_s.currval,
6807                                       -- entity_id
6808                xla_events_s.currval,  -- event_id
6809                c_application_id,      -- upg_source_application_id
6810                transaction_date_entered
6811                                       -- transaction_date
6812          )
6813          select /*+ ordered leading(dp) index(dp fa_deprn_periods_u3) rowid(ds) index(ah fa_asset_history_n2) */
6814                 ds.asset_id                asset_id,
6815                 ds.book_type_code          book_type_code,
6816                 bc.set_of_books_id         set_of_books_id,
6817                 ds.deprn_run_date          transaction_date_entered,
6818                 'DEPRECIATION'             event_type_code,
6819                 'DEPRECIATION'             event_class_code,
6820                 dp.period_name             period_name,
6821                 dp.period_counter          period_counter,
6822                 dp.calendar_period_close_date
6823                                            calendar_period_close_date,
6824                 nvl(cb.reserve_account_ccid, -1)
6825                                            default_rsv_ccid,
6826                 nvl(cb.bonus_reserve_acct_ccid, -1)
6827                                            default_bonus_rsv_ccid,
6828                 nvl(cb.reval_amort_account_ccid, -1)
6829                                            default_reval_amort_ccid,
6830                 nvl(cb.reval_reserve_account_ccid, -1)
6831                                            default_reval_rsv_ccid,
6832                 nvl(bc.je_depreciation_category, 'OTHER')
6833                                            je_category_name
6834          from   fa_deprn_summary ds,
6835                 fa_book_controls bc,
6836                 gl_sets_of_books glsob,
6837                 fa_deprn_periods dp,
6838                 fa_asset_history ah,
6839                 fa_category_books cb
6840          where  ds.rowid between p_start_rowid and p_end_rowid
6841          and    ds.book_type_code = bc.book_type_code
6842          and    bc.set_of_books_id = glsob.set_of_books_id
6843          and    dp.book_type_code = ds.book_type_code
6844          and    dp.period_counter = ds.period_counter
6845          and    dp.period_close_date is null
6846          and    ds.event_id is null
6847          and    ds.deprn_source_code <> 'TRACK'
6848          and    (nvl(ds.deprn_amount,0) - nvl(ds.deprn_adjustment_amount,0) <> 0 or
6849                  nvl(ds.reval_amortization,0) <> 0 or
6850                  nvl(ds.bonus_deprn_amount,0) <> 0 ) -- Bug 9972336
6851          and    ds.asset_id = ah.asset_id
6852          and    nvl(dp.period_close_date, sysdate) >= ah.date_effective
6853          and    nvl(dp.period_close_date, sysdate) <
6854                 nvl(ah.date_ineffective, sysdate+1)
6855          and    ah.category_id = cb.category_id
6856          and    bc.book_type_code = cb.book_type_code;
6857 
6858    else
6859 
6860       insert all
6861       when 1 = 1 then
6862          into fa_xla_upg_events_gt (
6863                event_id,
6864                entity_id,
6865                set_of_books_id,
6866                period_name,
6867                calendar_period_close_date,
6868                je_category_name,
6869                event_type_code,
6870                event_class_code,
6871                ds_asset_id,
6872                ds_book_type_code,
6873                ds_period_counter,
6874                reserve_acct_ccid,
6875                bonus_reserve_acct_ccid,
6876                reval_amort_acct_ccid,
6877                reval_reserve_acct_ccid
6878          ) values (
6879                xla_events_s.nextval,
6880                xla_transaction_entities_s.nextval,
6881                set_of_books_id,
6882                period_name,
6883                calendar_period_close_date,
6884                je_category_name,
6885                event_type_code,
6886                event_class_code,
6887                asset_id,
6888                book_type_code,
6889                period_counter,
6890                default_rsv_ccid,
6891                default_bonus_rsv_ccid,
6892                default_reval_amort_ccid,
6893                default_reval_rsv_ccid
6894          )
6895       when 1 = 1 then
6896          into fa_deprn_events (
6897             asset_id,
6898             book_type_code,
6899             period_counter,
6900             deprn_run_date,
6901             deprn_run_id,
6902             event_id,
6903             reversal_event_id,
6904             reversal_date,
6905             creation_date,
6906             created_by,
6907             last_update_date,
6908             last_updated_by,
6909             last_update_login
6910          ) values (
6911             asset_id,
6912             book_type_code,
6913             period_counter,
6914             transaction_date_entered,
6915             1,
6916             xla_events_s.currval,
6917             null,
6918             null,
6919             sysdate,
6920             c_upgrade_bugno,
6921             sysdate,
6922             c_upgrade_bugno,
6923             c_upgrade_bugno
6924          )
6925       when 1 = 1 then
6926          into xla_transaction_entities_upg (
6927                upg_batch_id,
6928                application_id,
6929                ledger_id,
6930                legal_entity_id,
6931                entity_code,
6932                source_id_int_1,
6933                source_id_int_2,
6934                source_id_int_3,
6935                source_id_int_4,
6936                source_id_char_1,
6937                source_id_char_2,
6938                source_id_char_3,
6939                source_id_char_4,
6940                security_id_int_1,
6941                security_id_int_2,
6942                security_id_int_3,
6943                security_id_char_1,
6944                security_id_char_2,
6945                security_id_char_3,
6946                transaction_number,
6947                valuation_method,
6948                source_application_id,
6949                creation_date,
6950                created_by,
6951                last_update_date,
6952                last_updated_by,
6953                last_update_login,
6954                entity_id,
6955                upg_source_application_id
6956          ) values (
6957                l_upg_batch_id,        -- upg_batch_id
6958                c_application_id,      -- application_id
6959                set_of_books_id,       -- ledger_id
6960                null,                  -- legal_entity_id
6961                c_entity_code,         -- entity_code
6962                asset_id,              -- source_id_int_1
6963                period_counter,        -- source_id_int_2
6964                1,                     -- source_id_int_3
6965                null,                  -- source_id_int_4
6966                book_type_code,        -- source_id_char_1
6967                null,                  -- source_id_char_2
6968                null,                  -- source_id_char_3
6969                null,                  -- source_id_char_4
6970                null,                  -- security_id_int_1
6971                null,                  -- security_id_int_2
6972                null,                  -- security_id_int_3
6973                null,                  -- security_id_char_1
6974                null,                  -- security_id_char_2
6975                null,                  -- security_id_char_3
6976                set_of_books_id,       -- transaction_number
6977                book_type_code,        -- valuation_method
6978                c_application_id,      -- source_application_id
6979                sysdate,               -- creation_date
6980                c_fnd_user,            -- created_by
6981                sysdate,               -- last_update_date
6982                c_fnd_user,            -- last_updated_by
6983                c_upgrade_bugno,       -- last_update_login
6984                xla_transaction_entities_s.currval,
6985                                       -- entity_id
6986                c_application_id       -- upg_source_application_id
6987          )
6988       when 1 = 1 then
6989          into xla_events (
6990                upg_batch_id,
6991                application_id,
6992                event_type_code,
6993                event_number,
6994                event_status_code,
6995                process_status_code,
6996                on_hold_flag,
6997                event_date,
6998                creation_date,
6999                created_by,
7000                last_update_date,
7001                last_updated_by,
7002                last_update_login,
7003                program_update_date,
7004                program_id,
7005                program_application_id,
7006                request_id,
7007                entity_id,
7008                event_id,
7009                upg_source_application_id,
7010                transaction_date
7011          ) values (
7012                l_upg_batch_id,        -- upg_batch_id
7013                c_application_id,      -- application_id
7014                event_type_code,       -- event_type_code
7015                1,                     -- event_number
7016                'P',                   -- event_status_code
7017                'P',                   -- process_status_code
7018                'N',                   -- on_hold_flag
7019                calendar_period_close_date,
7020 --  Bug 7036409             transaction_date_entered,
7021                                       -- event_date
7022                sysdate,               -- creation_date
7023                c_fnd_user,            -- created_by
7024                sysdate,               -- last_update_date
7025                c_fnd_user,            -- last_updated_by
7026                c_upgrade_bugno,       -- last_update_login
7027                null,                  -- program_update_date
7028                null,                  -- program_id
7029                null,                  -- program_application_id
7030                null,                  -- request_id
7031                xla_transaction_entities_s.currval,
7032                                       -- entity_id
7033                xla_events_s.currval,  -- event_id
7034                c_application_id,      -- upg_source_application_id
7035                transaction_date_entered
7036                                       -- transaction_date
7037          )
7038       when 1 = 1 then
7039          into fa_xla_upg_headers_gt (
7040             ae_header_id,
7041             event_id,
7042             set_of_books_id
7043          ) values (
7044             xla_ae_headers_s.nextval,
7045             xla_events_s.currval,
7046             set_of_books_id
7047          )
7048       when 1 = 1 then
7049          into xla_ae_headers (
7050                upg_batch_id,
7051                application_id,
7052                amb_context_code,
7053                entity_id,
7054                event_id,
7055                event_type_code,
7056                ae_header_id,
7057                ledger_id,
7058                accounting_date,
7059                period_name,
7060                reference_date,
7061                balance_type_code,
7062                je_category_name,
7063                gl_transfer_status_code,
7064                gl_transfer_date,
7065                accounting_entry_status_code,
7066                accounting_entry_type_code,
7067                description,
7068                budget_version_id,
7069                funds_status_code,
7070                encumbrance_type_id,
7071                completed_date,
7072                doc_sequence_id,
7073                doc_sequence_value,
7074                doc_category_code,
7075                packet_id,
7076                group_id,
7077                creation_date,
7078                created_by,
7079                last_update_date,
7080                last_updated_by,
7081                last_update_login,
7082                program_id,
7083                program_application_id,
7084                program_update_date,
7085                request_id,
7086                close_acct_seq_assign_id,
7087                close_acct_seq_version_id,
7088                close_acct_seq_value,
7089                completion_acct_seq_assign_id,
7090                completion_acct_seq_version_id,
7091                completion_acct_seq_value,
7092                accounting_batch_id,
7093                product_rule_type_code,
7094                product_rule_code,
7095                product_rule_version,
7096                upg_source_application_id,
7097                upg_valid_flag
7098          ) values (
7099                l_upg_batch_id,             -- upg_batch_id
7100                c_application_id,           -- application_id
7101                c_amb_context_code,         -- amb_context_code
7102                xla_transaction_entities_s.currval,
7103                                            -- entity_id
7104                xla_events_s.currval,       -- event_id
7105                event_type_code,            -- event_type_code
7106                xla_ae_headers_s.currval,   -- ae_header_id
7107                set_of_books_id,            -- ledger_id
7108                calendar_period_close_date, -- accounting_date
7109                period_name,                -- period_name
7110                null,                       -- reference_date
7111                'A',                        -- balance_type_code
7112                je_category_name,           -- je_category_name
7113                -- Bug 6811554 , 12879380
7114                decode(je_hdr_id, null, decode (gl_posting_allowed_flag,
7115                                                                  'YES', 'N', 'NT')
7116                                      ,'Y'),-- gl_transfer_status_code
7117                null,                       -- gl_transfer_date
7118                'F',                        -- accounting_entry_status_code
7119                'STANDARD',                 -- accounting_entry_type_code
7120                'Depreciation - ' ||
7121                   to_char(calendar_period_close_date, 'DD-MON-RR'),
7122                                            -- description
7123                null,                       -- budget_version_id
7124                null,                       -- funds_status_code
7125                null,                       -- encumbrance_type_id
7126                null,                       -- completed_date
7127                null,                       -- doc_sequence_id
7128                null,                       -- doc_sequence_value
7129                null,                       -- doc_category_code
7130                null,                       -- packet_id
7131                null,                       -- group_id
7132                sysdate,                    -- creation_date
7133                c_fnd_user,                 -- created_by
7134                sysdate,                    -- last_update_date
7135                c_fnd_user,                 -- last_updated_by
7136                c_upgrade_bugno,            -- last_update_login
7137                null,                       -- program_id
7138                c_application_id,           -- program_application_id
7139                sysdate,                    -- program_update_date
7140                null,                       -- request_id
7141                null,                       -- close_acct_seq_assign_id
7142                null,                       -- close_acct_seq_version_id
7143                null,                       -- close_acct_seq_value
7144                null,                       -- completion_acct_seq_assign_id
7145                null,                       -- completion_acct_seq_version_id
7146                null,                       -- completion_acct_seq_value
7147                null,                       -- accounting_batch_id
7148                null,                       -- product_rule_type_code
7149                null,                       -- product_rule_code
7150                null,                       -- product_rule_version
7151                c_application_id,           -- upg_source_application_id
7152                null                        -- upg_valid_flag
7153          )
7154          select /*+ ordered leading(dp) index(dp fa_deprn_periods_u3) rowid(ds) index(ah fa_asset_history_n2) */
7155                 ds.asset_id                asset_id,
7156                 ds.book_type_code          book_type_code,
7157                 bc.set_of_books_id         set_of_books_id,
7158                 ds.deprn_run_date          transaction_date_entered,
7159                 'DEPRECIATION'             event_type_code,
7160                 'DEPRECIATION'             event_class_code,
7161                 dp.period_name             period_name,
7162                 dp.period_counter          period_counter,
7163                 dp.calendar_period_close_date
7164                                            calendar_period_close_date,
7165                 nvl(cb.reserve_account_ccid, -1)
7166                                            default_rsv_ccid,
7167                 nvl(cb.bonus_reserve_acct_ccid, -1)
7168                                            default_bonus_rsv_ccid,
7169                 nvl(cb.reval_amort_account_ccid, -1)
7170                                            default_reval_amort_ccid,
7171                 nvl(cb.reval_reserve_account_ccid, -1)
7172                                            default_reval_rsv_ccid,
7173                 nvl(bc.je_depreciation_category, 'OTHER')
7174                                            je_category_name,
7175                 (select min(je_header_id)
7176                  from   fa_deprn_detail dd
7177                  where  dd.book_type_code = ds.book_type_code
7178                  and    dd.asset_id = ds.asset_id
7179                  and    dd.period_counter = ds.period_counter
7180                  and    nvl(dd.je_header_id,-1) > 0) je_hdr_id, -- Bug 6811554
7181                 bc.gl_posting_allowed_flag gl_posting_allowed_flag
7182          from   fa_deprn_summary ds,
7183                 fa_book_controls bc,
7184                 gl_sets_of_books glsob,
7185                 fa_deprn_periods dp,
7186                 fa_asset_history ah,
7187                 fa_category_books cb,
7188                 gl_period_statuses ps
7189          where  ds.rowid between p_start_rowid and p_end_rowid
7190          and    ds.book_type_code = bc.book_type_code
7191          and    bc.set_of_books_id = glsob.set_of_books_id
7192          and    dp.book_type_code = ds.book_type_code
7193          and    dp.period_counter = ds.period_counter
7194          and    dp.period_close_date is not null
7195          and    ds.event_id is null
7196          and    ds.deprn_source_code <> 'TRACK'
7197          and    ds.asset_id = ah.asset_id
7198          and    (nvl(ds.deprn_amount,0) - nvl(ds.deprn_adjustment_amount,0) <> 0 or
7199                  nvl(ds.reval_amortization,0) <> 0 or
7200                  nvl(ds.bonus_deprn_amount,0) <> 0 ) -- Bug 9972336
7201          and    dp.period_close_date >= ah.date_effective
7202          and    dp.period_close_date < nvl(ah.date_ineffective, sysdate+1)
7203          and    ah.category_id = cb.category_id
7204          and    bc.book_type_code = cb.book_type_code
7205          and    ps.application_id = 101
7206          and    ps.migration_status_code in ('P', 'U')
7207          and    ps.set_of_books_id = bc.set_of_books_id
7208          and    ps.period_name = dp.period_name
7209          and    substr(dp.xla_conversion_status, 1, 1) in
7210                 ('H','U','E','0','1','2','3','4','5','6','7','8','9')
7211          and    dp.xla_conversion_status not in ('UD', 'UA')
7212          and    dp.xla_conversion_status is not null;
7213 
7214    end if;
7215 
7216    -- Update fa_deprn_summary table with event_id
7217    update /*+ rowid(ds) */
7218           fa_deprn_summary ds
7219    set    ds.event_id =
7220    ( select ev.event_id
7221      from   fa_xla_upg_events_gt ev
7222      where  ev.ds_asset_id = ds.asset_id
7223      and    ev.ds_book_type_code = ds.book_type_code
7224      and    ev.ds_period_counter = ds.period_counter
7225    ),
7226           ds.deprn_run_id = nvl(ds.deprn_run_id, 1)
7227    where  ds.rowid between p_start_rowid and p_end_rowid
7228    and    ds.event_id is null
7229    and    ds.deprn_source_code <> 'TRACK'
7230    and    (nvl(ds.deprn_amount,0) - nvl(ds.deprn_adjustment_amount,0) <> 0 or
7231            nvl(ds.reval_amortization,0) <> 0 or
7232            nvl(ds.bonus_deprn_amount,0) <> 0 ) -- Bug 9972336
7233    returning ds.event_id,
7234              ds.asset_id,
7235              ds.book_type_code,
7236              ds.period_counter
7237    bulk collect
7238    into      l_event_id_tbl,
7239              l_asset_id_tbl,
7240              l_book_type_code_tbl,
7241              l_period_counter_tbl;
7242 
7243    -- Bug 14007714 : Reinstated fix done in bug 6811544 only for uptime upgrade
7244    -- Downtime upgrade case will be handled in faevddpupg.sql
7245    if (p_mode <> 'downtime') then
7246       FORALL l_count IN 1..l_event_id_tbl.count
7247          update fa_deprn_detail
7248          set    event_id = l_event_id_tbl(l_count),
7249                 deprn_run_id = nvl(deprn_run_id, 1)
7250          where  asset_id = l_asset_id_tbl(l_count)
7251          and    book_type_code = l_book_type_code_tbl(l_count)
7252          and    period_counter = l_period_counter_tbl(l_count)
7253          and    event_id is null;
7254    end if;
7255 
7256    if (l_mc_books > 0) then
7257       FORALL l_count IN 1..l_event_id_tbl.count
7258          update fa_mc_deprn_summary
7259          set    event_id = l_event_id_tbl(l_count),
7260                 deprn_run_id = nvl(deprn_run_id, 1)
7261          where  asset_id = l_asset_id_tbl(l_count)
7262          and    book_type_code = l_book_type_code_tbl(l_count)
7263          and    period_counter = l_period_counter_tbl(l_count)
7264          and    event_id is null;
7265 
7266       -- Bug 14007714 : Reinstated fix done in bug 6811544 only for uptime upgrade
7267       -- Downtime upgrade case will be handled in faevddpupg.sql
7268       if (p_mode <> 'downtime') then
7269          FORALL l_count IN 1..l_event_id_tbl.count
7270             update fa_mc_deprn_detail
7271             set    event_id = l_event_id_tbl(l_count),
7272                    deprn_run_id = nvl(deprn_run_id, 1)
7273             where  asset_id = l_asset_id_tbl(l_count)
7274             and    book_type_code = l_book_type_code_tbl(l_count)
7275             and    period_counter = l_period_counter_tbl(l_count)
7276             and    event_id is null;
7277       end if;
7278    end if;
7279 
7280    -- Bug 12534019 : Reval Amort should be Cr and Reval Rsv Dr
7281    insert all
7282    when (1 = 1) then
7283       into xla_ae_lines (
7284             upg_batch_id,
7285             ae_header_id,
7286             ae_line_num,
7287             displayed_line_number,
7288             application_id,
7289             code_combination_id,
7290             gl_transfer_mode_code,
7291             accounted_dr,
7292             accounted_cr,
7293             currency_code,
7294             currency_conversion_date,
7295             currency_conversion_rate,
7296             currency_conversion_type,
7297             entered_dr,
7298             entered_cr,
7299             description,
7300             accounting_class_code,
7301             gl_sl_link_id,
7302             gl_sl_link_table,
7303             party_type_code,
7304             party_id,
7305             party_site_id,
7306             statistical_amount,
7307             ussgl_transaction_code,
7308             jgzz_recon_ref,
7309             control_balance_flag,
7310             analytical_balance_flag,
7311             upg_tax_reference_id1,
7312             upg_tax_reference_id2,
7313             upg_tax_reference_id3,
7314             creation_date,
7315             created_by,
7316             last_update_date,
7317             last_updated_by,
7318             last_update_login,
7319             program_update_date,
7320             program_id,
7321             program_application_id,
7322             request_id,
7323             gain_or_loss_flag,
7324             accounting_date,
7325             ledger_id
7326       ) values (
7327             l_upg_batch_id,                       -- upg_batch_id
7328             ae_header_id,                         -- ae_header_id
7329             ae_line_num,                          -- ae_line_num
7330             ae_line_num,                          -- displayed_line_num
7331             c_application_id,                     -- application_id
7332             decode(multiplier,                    -- code_combination_id
7333                    1, nvl(deprn_exp_ccid, default_ccid),
7334                    2, nvl(deprn_rsv_ccid,default_rsv_ccid),
7335                    3, nvl(bonus_exp_ccid, default_ccid),
7336                    4, nvl(bonus_rsv_ccid, default_bonus_rsv_ccid),
7337                    5, nvl(reval_amort_ccid, default_reval_amort_ccid),
7338                    6, nvl(reval_rsv_ccid, default_reval_rsv_ccid)),
7339             'S',                                  -- gl_transfer_mode_code
7340             decode(multiplier,                    -- accounted_dr
7341                    1, deprn_amount,
7342                    2, null,
7343                    3, bonus_amount,
7344                    4, null,
7345                    5, null,
7346                    6, reval_amount),
7347             decode(multiplier,                    -- accounted_cr
7348                    1, null,
7349                    2, deprn_amount,
7350                    3, null,
7351                    4, bonus_amount,
7352                    5, reval_amount,
7353                    6, null),
7354             currency_code,                        -- currency_code
7355             null,                                 -- currency_conversion_date
7356             null,                                 -- currency_conversion_rate
7357             null,                                 -- currency_conversion_type
7358             decode(multiplier,                    -- entered_dr
7359                    1, deprn_amount,
7360                    2, null,
7361                    3, bonus_amount,
7362                    4, null,
7363                    5, null,
7364                    6, reval_amount),
7365             decode(multiplier,                    -- entered_cr
7366                    1, null,
7367                    2, deprn_amount,
7368                    3, null,
7369                    4, bonus_amount,
7370                    5, reval_amount,
7371                    6, null),
7372             decode(multiplier,                    -- description
7373                    1, l_de_description,
7374                    2, l_dr_description,
7375                    3, l_be_description,
7376                    4, l_br_description,
7377                    5, l_ra_description,
7378                    6, l_rr_description) || ' - ' ||
7379                to_char(cal_period_close_date, 'DD-MON-RR'),
7380             decode(multiplier,                    -- accounting_class_code
7381                    1, 'EXPENSE',
7382                    2, 'ASSET',
7383                    3, 'EXPENSE',
7384                    4, 'ASSET',
7385                    5, 'EXPENSE',
7386                    6, 'ASSET'),
7387             xla_gl_sl_link_id_s.nextval,          -- gl_sl_link_id
7388             'XLAJEL',                             -- gl_sl_link_table
7389             null,                                 -- party_type_code
7390             null,                                 -- party_id
7391             null,                                 -- party_site_id
7392             null,                                 -- statistical_amount
7393             null,                                 -- ussgl_transaction_code
7394             null,                                 -- glzz_recon_ref
7395             null,                                 -- control_balance_flag
7396             null,                                 -- analytical_balance_flag
7397             null,                                 -- upg_tax_reference_id1
7398             null,                                 -- upg_tax_reference_id2
7399             null,                                 -- upg_tax_reference_id3
7400             sysdate,                              -- creation_date
7401             c_fnd_user,                           -- created_by
7402             sysdate,                              -- last_update_date
7403             c_fnd_user,                           -- last_updated_by
7404             c_upgrade_bugno,                      -- last_update_login
7405             null,                                 -- program_update_date
7406             null,                                 -- program_id
7407             c_application_id,                     -- program_application_id
7408             null,                                 -- request_id
7409             'N',                                  -- gain_or_loss_flag
7410             cal_period_close_date,                -- accounting_date,
7411             set_of_books_id                       -- ledger_id/sob_id
7412       )
7413    when (1 = 1) then
7414       into xla_distribution_links (
7415             upg_batch_id,
7416             application_id,
7417             event_id,
7418             ae_header_id,
7419             ae_line_num,
7420             accounting_line_code,
7421             accounting_line_type_code,
7422             source_distribution_type,
7423             source_distribution_id_char_1,
7424             source_distribution_id_char_2,
7425             source_distribution_id_char_3,
7426             source_distribution_id_char_4,
7427             source_distribution_id_char_5,
7428             source_distribution_id_num_1,
7429             source_distribution_id_num_2,
7430             source_distribution_id_num_3,
7431             source_distribution_id_num_4,
7432             source_distribution_id_num_5,
7433             merge_duplicate_code,
7434             statistical_amount,
7435             unrounded_entered_dr,
7436             unrounded_entered_cr,
7437             unrounded_accounted_dr,
7438             unrounded_accounted_cr,
7439             ref_ae_header_id,
7440             ref_temp_line_num,
7441             ref_event_id,
7442             temp_line_num,
7443             tax_line_ref_id,
7444             tax_summary_line_ref_id,
7445             tax_rec_nrec_dist_ref_id,
7446             line_definition_owner_code,
7447             line_definition_code,
7448             event_class_code,
7449             event_type_code
7450       ) values (
7451             l_upg_batch_id,                    -- upg_batch_id
7452             c_application_id,                  -- application_id
7453             event_id,                          -- event_id
7454             ae_header_id,                      -- ae_header_id
7455             ae_line_num,                       -- ae_line_num
7456             null,                              -- accounting_line_code
7457             'S',                               -- accounting_line_type_code
7458             'DEPRN',                           -- source_distribution_type
7459             null,                              -- source_distribution_id_char_1
7460             null,                              -- source_distribution_id_char_2
7461             null,                              -- source_distribution_id_char_3
7462             book_type_code,                    -- source_distribution_id_char_4
7463             null,                              -- source_distribution_id_char_5
7464             asset_id,                          -- source_distribution_id_num_1
7465             period_counter,                    -- source_distribution_id_num_2
7466             1,                                 -- source_distribution_id_num_3
7467             null,                              -- source_distribution_id_num_4
7468             distribution_id,                   -- source_distribution_id_num_5
7469             'N',                               -- merge_duplicate_code
7470             null,                              -- statistical_amount
7471             decode(multiplier,                 -- unrounded_entered_dr
7472                    1, deprn_amount,
7473                    2, null,
7474                    3, bonus_amount,
7475                    4, null,
7476                    5, null,
7477                    6, reval_amount),
7478             decode(multiplier,                -- unrounded_entered_cr
7479                    1, null,
7480                    2, deprn_amount,
7481                    3, null,
7482                    4, bonus_amount,
7483                    5, reval_amount,
7484                    6, null),
7485             decode(multiplier,                -- unrounded_accounted_dr
7486                    1, deprn_amount,
7487                    2, null,
7488                    3, bonus_amount,
7489                    4, null,
7490                    5, null,
7491                    6, reval_amount),
7492             decode(multiplier,                -- unrounded_accounted_cr
7493                    1, null,
7494                    2, deprn_amount,
7495                    3, null,
7496                    4, bonus_amount,
7497                    5, reval_amount,
7498                    6, null),
7499             ae_header_id,                      -- ref_ae_header_id
7500             null,                              -- ref_temp_line_num
7501             null,                              -- ref_event_id
7502             ae_line_num,                       -- temp_line_num
7503             null,                              -- tax_line_ref_id
7504             null,                              -- tax_summary_line_ref_id
7505             null,                              -- tax_rec_nrec_dist_ref_id
7506             null,                              -- line_definition_owner_code
7507             null,                              -- line_definition_code
7508             event_class_code,                  -- event_class_code
7509             event_type_code                    -- event_type_code
7510       )
7511    when (je_batch_id is not null) then
7512       into gl_import_references (
7513             je_batch_id,
7514             je_header_id,
7515             je_line_num,
7516             last_update_date,
7517             last_updated_by,
7518             creation_date,
7519             created_by,
7520             last_update_login,
7521             reference_1,
7522             reference_2,
7523             reference_3,
7524             reference_4,
7525             reference_5,
7526             reference_6,
7527             reference_7,
7528             reference_8,
7529             reference_9,
7530             reference_10,
7531             subledger_doc_sequence_id,
7532             subledger_doc_sequence_value,
7533             gl_sl_link_id,
7534             gl_sl_link_table
7535       ) values (
7536             je_batch_id,                       -- je_batch_id
7537             je_header_id,                      -- je_header_id
7538             decode(multiplier,                 -- je_line_num
7539                    1, de_je_line_num,
7540                    2, dr_je_line_num,
7541                    3, be_je_line_num,
7542                    4, br_je_line_num,
7543                    5, ra_je_line_num,
7544                    6, rr_je_line_num),
7545             sysdate,                           -- last_update_date
7546             c_fnd_user,                        -- last_updated_by
7547             sysdate,                           -- creation_date
7548             c_fnd_user,                        -- created_by
7549             c_upgrade_bugno,                   -- last_update_login
7550             null,                              -- reference_1
7551             to_char(asset_id),                 -- reference_2
7552             to_char(distribution_id),          -- reference_3
7553             null,                              -- reference_4
7554             book_type_code,                    -- reference_5
7555             to_char(period_counter),           -- reference_6
7556             null,                              -- reference_7
7557             null,                              -- reference_8
7558             null,                              -- reference_9
7559             null,                              -- reference_10
7560             null,                              -- subledger_doc_seq_id
7561             null,                              -- subledger_doc_seq_value
7562             xla_gl_sl_link_id_s.currval,       -- gl_sl_link_id : Bug 6811548
7563             'XLAJEL'                           -- gl_sl_link_table
7564       )
7565       select /*+ leading(ev) index(dd, FA_DEPRN_DETAIL_N1) */
7566              ev.event_id                       event_id,
7567              he.ae_header_id                   ae_header_id,
7568              ev.calendar_period_close_date     cal_period_close_date,
7569              ev.event_type_code                event_type_code,
7570              ev.event_class_code               event_class_code,
7571              ev.ds_asset_id                    asset_id,
7572              ev.ds_book_type_code              book_type_code,
7573              he.set_of_books_id                set_of_books_id,
7574              ev.ds_period_counter              period_counter,
7575              dd.distribution_id                distribution_id,
7576              glsob.currency_code               currency_code,
7577              dd.deprn_amount - nvl(dd.deprn_adjustment_amount, 0)
7578                              - nvl(dd.bonus_deprn_amount, 0)
7579                                                deprn_amount,
7580              nvl(dd.bonus_deprn_amount -
7581                  nvl(dd.bonus_deprn_adjustment_amount, 0), 0)
7582                                                bonus_amount,
7583              nvl(dd.reval_amortization, 0)     reval_amount,
7584              ev.reserve_acct_ccid              default_rsv_ccid,
7585              ev.bonus_reserve_acct_ccid        default_bonus_rsv_ccid,
7586              ev.reval_amort_acct_ccid          default_reval_amort_ccid,
7587              ev.reval_reserve_acct_ccid        default_reval_rsv_ccid,
7588              nvl(jl_de.code_combination_id, da.deprn_expense_account_ccid)
7589                                                deprn_exp_ccid,
7590              nvl(jl_dr.code_combination_id, da.deprn_reserve_account_ccid)
7591                                                deprn_rsv_ccid,
7592              nvl(jl_be.code_combination_id, da.bonus_exp_account_ccid)
7593                                                bonus_exp_ccid,
7594              nvl(jl_br.code_combination_id, da.bonus_rsv_account_ccid)
7595                                                bonus_rsv_ccid,
7596              nvl(jl_ra.code_combination_id, da.reval_amort_account_ccid)
7597                                                reval_amort_ccid,
7598              nvl(jl_rr.code_combination_id, da.reval_rsv_account_ccid)
7599                                                reval_rsv_ccid,
7600              nvl(dh.code_combination_id, -1)   default_ccid,
7601              decode (dd.je_header_id, null, 'N', 'Y')
7602                                                gl_transfer_status_code,
7603              gljh.je_batch_id                  je_batch_id,
7604              dd.je_header_id                   je_header_id,
7605              nvl(dd.deprn_expense_je_line_num, 0)
7606                                                de_je_line_num,
7607              nvl(dd.deprn_reserve_je_line_num, 0)
7608                                                dr_je_line_num,
7609              nvl(dd.bonus_deprn_exp_je_line_num, 0)
7610                                                be_je_line_num,
7611              nvl(dd.bonus_deprn_rsv_je_line_num, 0)
7612                                                br_je_line_num,
7613              nvl(dd.reval_amort_je_line_num, 0)
7614                                                ra_je_line_num,
7615              nvl(dd.reval_reserve_je_line_num, 0)
7616                                                rr_je_line_num,
7617              row_number() over
7618                 (partition by ev.event_id
7619                  order by dd.distribution_id, mult.multiplier)
7620                                                ae_line_num,
7621              mult.multiplier                   multiplier
7622       from   fa_xla_upg_events_gt ev,
7623              fa_xla_upg_headers_gt he,
7624              fa_deprn_detail dd,
7625              fa_distribution_history dh,
7626              fa_distribution_accounts da,
7627              gl_je_lines jl_de,
7628              gl_je_lines jl_dr,
7629              gl_je_lines jl_be,
7630              gl_je_lines jl_br,
7631              gl_je_lines jl_ra,
7632              gl_je_lines jl_rr,
7633              gl_je_headers gljh,
7634              fa_book_controls bc,
7635              gl_sets_of_books glsob,
7636              gl_row_multipliers mult
7637       where  ev.event_id = he.event_id
7638       and    bc.book_type_code = ev.ds_book_type_code
7639       and    bc.set_of_books_id = glsob.set_of_books_id
7640       and    bc.book_type_code = dd.book_type_code
7641       and    he.set_of_books_id = bc.set_of_books_id
7642       and    dd.book_type_code = ev.ds_book_type_code
7643       and    dd.asset_id = ev.ds_asset_id
7644       and    dd.period_counter = ev.ds_period_counter
7645       and    dd.deprn_source_code <> 'T'
7646       and    dd.je_header_id = jl_de.je_header_id (+)
7647       and    dd.je_header_id = jl_dr.je_header_id (+)
7648       and    dd.je_header_id = jl_be.je_header_id (+)
7649       and    dd.je_header_id = jl_br.je_header_id (+)
7650       and    dd.je_header_id = jl_ra.je_header_id (+)
7651       and    dd.je_header_id = jl_rr.je_header_id (+)
7652       and    dd.deprn_expense_je_line_num = jl_de.je_line_num (+)
7653       and    dd.deprn_reserve_je_line_num = jl_dr.je_line_num (+)
7654       and    dd.bonus_deprn_exp_je_line_num = jl_be.je_line_num (+)
7655       and    dd.bonus_deprn_rsv_je_line_num = jl_br.je_line_num (+)
7656       and    dd.reval_amort_je_line_num = jl_ra.je_line_num (+)
7657       and    dd.reval_reserve_je_line_num = jl_rr.je_line_num (+)
7658       and    dd.distribution_id = dh.distribution_id
7659       and    da.book_type_code (+) = dd.book_type_code
7660       and    da.distribution_id (+) = dd.distribution_id
7661       and    dd.je_header_id = gljh.je_header_id (+)
7662       and    mult.multiplier < 7
7663       and    ((mult.multiplier in (1, 2))
7664            or ((mult.multiplier in (3, 4)) and
7665                (nvl(dd.bonus_deprn_amount -
7666                     nvl(dd.bonus_deprn_adjustment_amount, 0), 0) <> 0))
7667            or ((mult.multiplier in (5, 6)) and
7668                (dd.reval_amortization <> 0)));
7669 
7670    if (l_mc_books > 0) then
7671 
7672       -- Bug 6811548 : No hdr data for downtime upgrade
7673       if (p_mode <> 'downtime') then
7674 
7675          insert all
7676          when 1 = 1 then
7677             into fa_xla_upg_headers_gt (
7678                ae_header_id,
7679                event_id,
7680                set_of_books_id
7681             ) values (
7682                xla_ae_headers_s.nextval,
7683                event_id,
7684                set_of_books_id
7685             )
7686          when 1 = 1 then
7687             into xla_ae_headers (
7688                upg_batch_id,
7689                application_id,
7690                amb_context_code,
7691                entity_id,
7692                event_id,
7693                event_type_code,
7694                ae_header_id,
7695                ledger_id,
7696                accounting_date,
7697                period_name,
7698                reference_date,
7699                balance_type_code,
7700                je_category_name,
7701                gl_transfer_status_code,
7702                gl_transfer_date,
7703                accounting_entry_status_code,
7704                accounting_entry_type_code,
7705                description,
7706                budget_version_id,
7707                funds_status_code,
7708                encumbrance_type_id,
7709                completed_date,
7710                doc_sequence_id,
7711                doc_sequence_value,
7712                doc_category_code,
7713                packet_id,
7714                group_id,
7715                creation_date,
7716                created_by,
7717                last_update_date,
7718                last_updated_by,
7719                last_update_login,
7720                program_id,
7721                program_application_id,
7722                program_update_date,
7723                request_id,
7724                close_acct_seq_assign_id,
7725                close_acct_seq_version_id,
7726                close_acct_seq_value,
7727                completion_acct_seq_assign_id,
7728                completion_acct_seq_version_id,
7729                completion_acct_seq_value,
7730                accounting_batch_id,
7731                product_rule_type_code,
7732                product_rule_code,
7733                product_rule_version,
7734                upg_source_application_id,
7735                upg_valid_flag
7736          ) values (
7737                l_upg_batch_id,             -- upg_batch_id
7738                c_application_id,           -- application_id
7739                c_amb_context_code,         -- amb_context_code
7740                entity_id,                  -- entity_id
7741                event_id,                   -- event_id
7742                event_type_code,            -- event_type_code
7743                xla_ae_headers_s.currval,   -- ae_header_id
7744                set_of_books_id,            -- ledger_id
7745                calendar_period_close_date, -- accounting_date
7746                period_name,                -- period_name
7747                null,                       -- reference_date
7748                'A',                        -- balance_type_code
7749                je_category_name,           -- je_category_name
7750                'Y',                        -- gl_transfer_status_code
7751                null,                       -- gl_transfer_date
7752                'F',                        -- accounting_entry_status_code
7753                'STANDARD',                 -- accounting_entry_type_code
7754                description,                -- description
7755                null,                       -- budget_version_id
7756                null,                       -- funds_status_code
7757                null,                       -- encumbrance_type_id
7758                null,                       -- completed_date
7759                null,                       -- doc_sequence_id
7760                null,                       -- doc_sequence_value
7761                null,                       -- doc_category_code
7762                null,                       -- packet_id
7763                null,                       -- group_id
7764                sysdate,                    -- creation_date
7765                c_fnd_user,                 -- created_by
7766                sysdate,                    -- last_update_date
7767                c_fnd_user,                 -- last_updated_by
7768                c_upgrade_bugno,            -- last_update_login
7769                null,                       -- program_id
7770                c_application_id,           -- program_application_id
7771                sysdate,                    -- program_update_date
7772                null,                       -- request_id
7773                null,                       -- close_acct_seq_assign_id
7774                null,                       -- close_acct_seq_version_id
7775                null,                       -- close_acct_seq_value
7776                null,                       -- completion_acct_seq_assign_id
7777                null,                       -- completion_acct_seq_version_id
7778                null,                       -- completion_acct_seq_value
7779                null,                       -- accounting_batch_id
7780                null,                       -- product_rule_type_code
7781                null,                       -- product_rule_code
7782                null,                       -- product_rule_version
7783                c_application_id,           -- upg_source_application_id
7784                null                        -- upg_valid_flag
7785          )
7786          select faev.entity_id         entity_id,
7787                 faev.event_id          event_id,
7788                 faev.event_type_code   event_type_code,
7789                 mc.set_of_books_id     set_of_books_id,
7790                 faev.calendar_period_close_date
7791                                        calendar_period_close_date,
7792                 faev.period_name       period_name,
7793                 faev.je_category_name  je_category_name,
7794                 faev.description       description
7795          from   fa_xla_upg_events_gt faev,
7796                 fa_mc_book_controls mc
7797          where  mc.book_type_code = faev.ds_book_type_code
7798          and    mc.enabled_flag = 'Y';
7799 
7800       end if;  -- p_mode <> 'downtime'
7801 
7802    -- Bug 12534019 : Reval Amort should be Cr and Reval Rsv Dr
7803    insert all
7804    when (1 = 1) then
7805       into xla_ae_lines (
7806             upg_batch_id,
7807             ae_header_id,
7808             ae_line_num,
7809             displayed_line_number,
7810             application_id,
7811             code_combination_id,
7812             gl_transfer_mode_code,
7813             accounted_dr,
7814             accounted_cr,
7815             currency_code,
7816             currency_conversion_date,
7817             currency_conversion_rate,
7818             currency_conversion_type,
7819             entered_dr,
7820             entered_cr,
7821             description,
7822             accounting_class_code,
7823             gl_sl_link_id,
7824             gl_sl_link_table,
7825             party_type_code,
7826             party_id,
7827             party_site_id,
7828             statistical_amount,
7829             ussgl_transaction_code,
7830             jgzz_recon_ref,
7831             control_balance_flag,
7832             analytical_balance_flag,
7833             upg_tax_reference_id1,
7834             upg_tax_reference_id2,
7835             upg_tax_reference_id3,
7836             creation_date,
7837             created_by,
7838             last_update_date,
7839             last_updated_by,
7840             last_update_login,
7841             program_update_date,
7842             program_id,
7843             program_application_id,
7844             request_id,
7845             gain_or_loss_flag,
7846             accounting_date,
7847             ledger_id
7848       ) values (
7849             l_upg_batch_id,                       -- upg_batch_id
7850             ae_header_id,                         -- ae_header_id
7851             ae_line_num,                          -- ae_line_num
7852             ae_line_num,                          -- displayed_line_num
7853             c_application_id,                     -- application_id
7854             decode(multiplier,                    -- code_combination_id
7855                    1, nvl(deprn_exp_ccid, default_ccid),
7856                    2, nvl(deprn_rsv_ccid,default_rsv_ccid),
7857                    3, nvl(bonus_exp_ccid, default_ccid),
7858                    4, nvl(bonus_rsv_ccid, default_bonus_rsv_ccid),
7859                    5, nvl(reval_amort_ccid, default_reval_amort_ccid),
7860                    6, nvl(reval_rsv_ccid, default_reval_rsv_ccid)),
7861             'S',                                  -- gl_transfer_mode_code
7862             decode(multiplier,                    -- accounted_dr
7863                    1, deprn_amount,
7864                    2, null,
7865                    3, bonus_amount,
7866                    4, null,
7867                    5, null,
7868                    6, reval_amount),
7869             decode(multiplier,                    -- accounted_cr
7870                    1, null,
7871                    2, deprn_amount,
7872                    3, null,
7873                    4, bonus_amount,
7874                    5, reval_amount,
7875                    6, null),
7876             currency_code,                        -- currency_code
7877             null,                                 -- currency_conversion_date
7878             null,                                 -- currency_conversion_rate
7879             null,                                 -- currency_conversion_type
7880             decode(multiplier,                    -- entered_dr
7881                    1, deprn_amount,
7882                    2, null,
7883                    3, bonus_amount,
7884                    4, null,
7885                    5, null,
7886                    6, reval_amount),
7887             decode(multiplier,                    -- entered_cr
7888                    1, null,
7889                    2, deprn_amount,
7890                    3, null,
7891                    4, bonus_amount,
7892                    5, reval_amount,
7893                    6, null),
7894             decode(multiplier,                    -- description
7895                    1, l_de_description,
7896                    2, l_dr_description,
7897                    3, l_be_description,
7898                    4, l_br_description,
7899                    5, l_ra_description,
7900                    6, l_rr_description) || ' - ' ||
7901                to_char(cal_period_close_date, 'DD-MON-RR'),
7902             decode(multiplier,                    -- accounting_class_code
7903                    1, 'EXPENSE',
7904                    2, 'ASSET',
7905                    3, 'EXPENSE',
7906                    4, 'ASSET',
7907                    5, 'EXPENSE',
7908                    6, 'ASSET'),
7909             xla_gl_sl_link_id_s.nextval,          -- gl_sl_link_id
7910             'XLAJEL',                             -- gl_sl_link_table
7911             null,                                 -- party_type_code
7912             null,                                 -- party_id
7913             null,                                 -- party_site_id
7914             null,                                 -- statistical_amount
7915             null,                                 -- ussgl_transaction_code
7916             null,                                 -- glzz_recon_ref
7917             null,                                 -- control_balance_flag
7918             null,                                 -- analytical_balance_flag
7919             null,                                 -- upg_tax_reference_id1
7920             null,                                 -- upg_tax_reference_id2
7921             null,                                 -- upg_tax_reference_id3
7922             sysdate,                              -- creation_date
7923             c_fnd_user,                           -- created_by
7924             sysdate,                              -- last_update_date
7925             c_fnd_user,                           -- last_updated_by
7926             c_upgrade_bugno,                      -- last_update_login
7927             null,                                 -- program_update_date
7928             null,                                 -- program_id
7929             c_application_id,                     -- program_application_id
7930             null,                                 -- request_id
7931             'N',                                  -- gain_or_loss_flag
7932             cal_period_close_date,                -- accounting_date,
7933             set_of_books_id                       -- ledger_id/sob_id
7934       )
7935    when (1 = 1) then
7936       into xla_distribution_links (
7937             upg_batch_id,
7938             application_id,
7939             event_id,
7940             ae_header_id,
7941             ae_line_num,
7942             accounting_line_code,
7943             accounting_line_type_code,
7944             source_distribution_type,
7945             source_distribution_id_char_1,
7946             source_distribution_id_char_2,
7947             source_distribution_id_char_3,
7948             source_distribution_id_char_4,
7949             source_distribution_id_char_5,
7950             source_distribution_id_num_1,
7951             source_distribution_id_num_2,
7952             source_distribution_id_num_3,
7953             source_distribution_id_num_4,
7954             source_distribution_id_num_5,
7955             merge_duplicate_code,
7956             statistical_amount,
7957             unrounded_entered_dr,
7958             unrounded_entered_cr,
7959             unrounded_accounted_dr,
7960             unrounded_accounted_cr,
7961             ref_ae_header_id,
7962             ref_temp_line_num,
7963             ref_event_id,
7964             temp_line_num,
7965             tax_line_ref_id,
7966             tax_summary_line_ref_id,
7967             tax_rec_nrec_dist_ref_id,
7968             line_definition_owner_code,
7969             line_definition_code,
7970             event_class_code,
7971             event_type_code
7972       ) values (
7973             l_upg_batch_id,                    -- upg_batch_id
7974             c_application_id,                  -- application_id
7975             event_id,                          -- event_id
7976             ae_header_id,                      -- ae_header_id
7977             ae_line_num,                       -- ae_line_num
7978             null,                              -- accounting_line_code
7979             'S',                               -- accounting_line_type_code
7980             'DEPRN',                           -- source_distribution_type
7981             null,                              -- source_distribution_id_char_1
7982             null,                              -- source_distribution_id_char_2
7983             null,                              -- source_distribution_id_char_3
7984             book_type_code,                    -- source_distribution_id_char_4
7985             null,                              -- source_distribution_id_char_5
7986             asset_id,                          -- source_distribution_id_num_1
7987             period_counter,                    -- source_distribution_id_num_2
7988             1,                                 -- source_distribution_id_num_3
7989             null,                              -- source_distribution_id_num_4
7990             distribution_id,                   -- source_distribution_id_num_5
7991             'N',                               -- merge_duplicate_code
7992             null,                              -- statistical_amount
7993             decode(multiplier,                 -- unrounded_entered_dr
7994                    1, deprn_amount,
7995                    2, null,
7996                    3, bonus_amount,
7997                    4, null,
7998                    5, null,
7999                    6, reval_amount),
8000             decode(multiplier,                -- unrounded_entered_cr
8001                    1, null,
8002                    2, deprn_amount,
8003                    3, null,
8004                    4, bonus_amount,
8005                    5, reval_amount,
8006                    6, null),
8007             decode(multiplier,                -- unrounded_accounted_dr
8008                    1, deprn_amount,
8009                    2, null,
8010                    3, bonus_amount,
8011                    4, null,
8012                    5, null,
8013                    6, reval_amount),
8014             decode(multiplier,                -- unrounded_accounted_cr
8015                    1, null,
8016                    2, deprn_amount,
8017                    3, null,
8018                    4, bonus_amount,
8019                    5, reval_amount,
8020                    6, null),
8021             ae_header_id,                      -- ref_ae_header_id
8022             null,                              -- ref_temp_line_num
8023             null,                              -- ref_event_id
8024             ae_line_num,                       -- temp_line_num
8025             null,                              -- tax_line_ref_id
8026             null,                              -- tax_summary_line_ref_id
8027             null,                              -- tax_rec_nrec_dist_ref_id
8028             null,                              -- line_definition_owner_code
8029             null,                              -- line_definition_code
8030             event_class_code,                  -- event_class_code
8031             event_type_code                    -- event_type_code
8032       )
8033    when (je_batch_id is not null) then
8034       into gl_import_references (
8035             je_batch_id,
8036             je_header_id,
8037             je_line_num,
8038             last_update_date,
8039             last_updated_by,
8040             creation_date,
8041             created_by,
8042             last_update_login,
8043             reference_1,
8044             reference_2,
8045             reference_3,
8046             reference_4,
8047             reference_5,
8048             reference_6,
8049             reference_7,
8050             reference_8,
8051             reference_9,
8052             reference_10,
8053             subledger_doc_sequence_id,
8054             subledger_doc_sequence_value,
8055             gl_sl_link_id,
8056             gl_sl_link_table
8057       ) values (
8058             je_batch_id,                       -- je_batch_id
8059             je_header_id,                      -- je_header_id
8060             decode(multiplier,                 -- je_line_num
8061                    1, de_je_line_num,
8062                    2, dr_je_line_num,
8063                    3, be_je_line_num,
8064                    4, br_je_line_num,
8065                    5, ra_je_line_num,
8066                    6, rr_je_line_num),
8067             sysdate,                           -- last_update_date
8068             c_fnd_user,                        -- last_updated_by
8069             sysdate,                           -- creation_date
8070             c_fnd_user,                        -- created_by
8071             c_upgrade_bugno,                   -- last_update_login
8072             null,                              -- reference_1
8073             to_char(asset_id),                 -- reference_2
8074             to_char(distribution_id),          -- reference_3
8075             null,                              -- reference_4
8076             book_type_code,                    -- reference_5
8077             to_char(period_counter),           -- reference_6
8078             null,                              -- reference_7
8079             null,                              -- reference_8
8080             null,                              -- reference_9
8081             null,                              -- reference_10
8082             null,                              -- subledger_doc_seq_id
8083             null,                              -- subledger_doc_seq_value
8084             xla_gl_sl_link_id_s.currval,       -- gl_sl_link_id : Bug 6811548
8085             'XLAJEL'                           -- gl_sl_link_table
8086       )
8087       select /*+ leading(ev) index(dd, FA_MC_DEPRN_DETAIL_N1) */
8088              ev.event_id                       event_id,
8089              he.ae_header_id                   ae_header_id,
8090              ev.calendar_period_close_date     cal_period_close_date,
8091              ev.event_type_code                event_type_code,
8092              ev.event_class_code               event_class_code,
8093              ev.ds_asset_id                    asset_id,
8094              ev.ds_book_type_code              book_type_code,
8095              he.set_of_books_id                set_of_books_id,
8096              ev.ds_period_counter              period_counter,
8097              dd.distribution_id                distribution_id,
8098              glsob.currency_code               currency_code,
8099              dd.deprn_amount - nvl(dd.deprn_adjustment_amount, 0)
8100                              - nvl(dd.bonus_deprn_amount, 0)
8101                                                deprn_amount,
8102              nvl(dd.bonus_deprn_amount -
8103                  nvl(dd.bonus_deprn_adjustment_amount, 0), 0)
8104                                                bonus_amount,
8105              nvl(dd.reval_amortization, 0)     reval_amount,
8106              ev.reserve_acct_ccid              default_rsv_ccid,
8107              ev.bonus_reserve_acct_ccid        default_bonus_rsv_ccid,
8108              ev.reval_amort_acct_ccid          default_reval_amort_ccid,
8109              ev.reval_reserve_acct_ccid        default_reval_rsv_ccid,
8110              nvl(jl_de.code_combination_id, da.deprn_expense_account_ccid)
8111                                                deprn_exp_ccid,
8112              nvl(jl_dr.code_combination_id, da.deprn_reserve_account_ccid)
8113                                                deprn_rsv_ccid,
8114              nvl(jl_be.code_combination_id, da.bonus_exp_account_ccid)
8115                                                bonus_exp_ccid,
8116              nvl(jl_br.code_combination_id, da.bonus_rsv_account_ccid)
8117                                                bonus_rsv_ccid,
8118              nvl(jl_ra.code_combination_id, da.reval_amort_account_ccid)
8119                                                reval_amort_ccid,
8120              nvl(jl_rr.code_combination_id, da.reval_rsv_account_ccid)
8121                                                reval_rsv_ccid,
8122              nvl(dh.code_combination_id, -1)   default_ccid,
8123              decode (dd.je_header_id, null, 'N', 'Y')
8124                                                gl_transfer_status_code,
8125              gljh.je_batch_id                  je_batch_id,
8126              dd.je_header_id                   je_header_id,
8127              nvl(dd.deprn_expense_je_line_num, 0)
8128                                                de_je_line_num,
8129              nvl(dd.deprn_reserve_je_line_num, 0)
8130                                                dr_je_line_num,
8131              nvl(dd.bonus_deprn_exp_je_line_num, 0)
8132                                                be_je_line_num,
8133              nvl(dd.bonus_deprn_rsv_je_line_num, 0)
8134                                                br_je_line_num,
8135              nvl(dd.reval_amort_je_line_num, 0)
8136                                                ra_je_line_num,
8137              nvl(dd.reval_reserve_je_line_num, 0)
8138                                                rr_je_line_num,
8139              row_number() over
8140                 (partition by ev.event_id
8141                  order by dd.distribution_id, mult.multiplier)
8142                                                ae_line_num,
8143              mult.multiplier                   multiplier
8144       from   fa_xla_upg_events_gt ev,
8145              fa_xla_upg_headers_gt he,
8146              fa_mc_deprn_detail dd,
8147              fa_distribution_history dh,
8148              fa_distribution_accounts da,
8149              gl_je_lines jl_de,
8150              gl_je_lines jl_dr,
8151              gl_je_lines jl_be,
8152              gl_je_lines jl_br,
8153              gl_je_lines jl_ra,
8154              gl_je_lines jl_rr,
8155              gl_je_headers gljh,
8156              fa_mc_book_controls bc,
8157              gl_sets_of_books glsob,
8158              gl_row_multipliers mult
8159       where  ev.event_id = he.event_id
8160       and    bc.book_type_code = ev.ds_book_type_code
8161       and    bc.set_of_books_id = glsob.set_of_books_id
8162       and    bc.enabled_flag = 'Y'
8163       and    he.set_of_books_id = bc.set_of_books_id
8164       and    dd.set_of_books_id = bc.set_of_books_id
8165       and    bc.book_type_code = dd.book_type_code
8166       and    dd.book_type_code = ev.ds_book_type_code
8167       and    dd.asset_id = ev.ds_asset_id
8168       and    dd.period_counter = ev.ds_period_counter
8169       and    dd.deprn_source_code <> 'T'
8170       and    dd.je_header_id = jl_de.je_header_id (+)
8171       and    dd.je_header_id = jl_dr.je_header_id (+)
8172       and    dd.je_header_id = jl_be.je_header_id (+)
8173       and    dd.je_header_id = jl_br.je_header_id (+)
8174       and    dd.je_header_id = jl_ra.je_header_id (+)
8175       and    dd.je_header_id = jl_rr.je_header_id (+)
8176       and    dd.deprn_expense_je_line_num = jl_de.je_line_num (+)
8177       and    dd.deprn_reserve_je_line_num = jl_dr.je_line_num (+)
8178       and    dd.bonus_deprn_exp_je_line_num = jl_be.je_line_num (+)
8179       and    dd.bonus_deprn_rsv_je_line_num = jl_br.je_line_num (+)
8180       and    dd.reval_amort_je_line_num = jl_ra.je_line_num (+)
8181       and    dd.reval_reserve_je_line_num = jl_rr.je_line_num (+)
8182       and    dd.distribution_id = dh.distribution_id
8183       and    da.book_type_code (+) = dd.book_type_code
8184       and    da.distribution_id (+) = dd.distribution_id
8185       and    dd.je_header_id = gljh.je_header_id (+)
8186       and    mult.multiplier < 7
8187       and    ((mult.multiplier in (1, 2))
8188            or ((mult.multiplier in (3, 4)) and
8189                (nvl(dd.bonus_deprn_amount -
8190                     nvl(dd.bonus_deprn_adjustment_amount, 0), 0) <> 0))
8191            or ((mult.multiplier in (5, 6)) and
8192                (dd.reval_amortization <> 0)));
8193 
8194    end if;
8195 
8196    l_event_id_tbl.delete;
8197    l_asset_id_tbl.delete;
8198    l_book_type_code_tbl.delete;
8199    l_period_counter_tbl.delete;
8200 
8201    commit;
8202 
8203 EXCEPTION
8204    WHEN OTHERS THEN
8205       rollback;
8206       raise;
8207 
8208 END Upgrade_Deprn_Events;
8209 
8210 Procedure Upgrade_Deferred_Events (
8211              p_start_rowid             IN            rowid,
8212              p_end_rowid               IN            rowid,
8213              p_batch_size              IN            number,
8214              x_success_count              OUT NOCOPY number,
8215              x_failure_count              OUT NOCOPY number,
8216              x_return_status              OUT NOCOPY number
8217             ) IS
8218 
8219    c_application_id           constant number(15) := 140;
8220    c_upgrade_bugno            constant number(15) := -4107161;
8221    c_fnd_user                 constant number(15) := 2;
8222 
8223 
8224    c_entity_code              constant varchar2(30) := 'DEFERRED_DEPRECIATION';
8225    c_amb_context_code         constant varchar2(30) := 'DEFAULT';
8226 
8227    -- this value can be altered in order to process more of less per batch
8228    l_batch_size                NUMBER;
8229 
8230    l_rows_processed            NUMBER;
8231 
8232    -- type for table variable
8233    type num_tbl_type  is table of number        index by binary_integer;
8234    type char_tbl_type is table of varchar2(150) index by binary_integer;
8235    type date_tbl_type is table of date          index by binary_integer;
8236    type rowid_tbl_type is table of rowid        index by binary_integer;
8237 
8238    -- used for bulk fetching
8239    -- main cursor
8240    l_event_id_tbl                               num_tbl_type;
8241    l_asset_id_tbl                               num_tbl_type;
8242    l_corp_book_type_code_tbl                    char_tbl_type;
8243    l_tax_book_type_code_tbl                     char_tbl_type;
8244    l_corp_period_counter_tbl                    num_tbl_type;
8245    l_tax_period_counter_tbl                     num_tbl_type;
8246    l_set_of_books_id_tbl                        num_tbl_type;
8247    l_org_id_tbl                                 num_tbl_type;
8248    l_transaction_type_code_tbl                  char_tbl_type;
8249    l_transaction_date_entered_tbl               date_tbl_type;
8250    l_period_name_tbl                            char_tbl_type;
8251    l_cal_period_close_date_tbl                  date_tbl_type;
8252    l_rowid_tbl                                  rowid_tbl_type;
8253 
8254    l_upg_batch_id                               number;
8255    l_ae_header_id                               number;
8256 
8257    l_entity_id_tbl                              num_tbl_type;
8258    l_event_class_code_tbl                       char_tbl_type;
8259    l_currency_code_tbl                          char_tbl_type;
8260    l_je_category_name_tbl                       char_tbl_type;
8261    l_ae_header_id_tbl                           num_tbl_type;
8262 
8263    l_cr_ccid_tbl                                num_tbl_type;
8264    l_dr_ccid_tbl                                num_tbl_type;
8265    l_credit_amount_tbl                          num_tbl_type;
8266    l_debit_amount_tbl                           num_tbl_type;
8267    l_exp_xla_gl_sl_link_id_tbl                  num_tbl_type;
8268    l_rsv_xla_gl_sl_link_id_tbl                  num_tbl_type;
8269    l_line_def_owner_code_tbl                    char_tbl_type;
8270    l_line_def_code_tbl                          char_tbl_type;
8271    l_dfr_exp_desc_tbl                           char_tbl_type;
8272    l_dfr_rsv_desc_tbl                           char_tbl_type;
8273    l_gl_transfer_status_code_tbl                char_tbl_type;
8274    l_je_batch_id_tbl                            num_tbl_type;
8275    l_je_header_id_tbl                           num_tbl_type;
8276    l_exp_je_line_num_tbl                        num_tbl_type;
8277    l_rsv_je_line_num_tbl                        num_tbl_type;
8278    l_distribution_id_tbl                        num_tbl_type;
8279    l_line_num_tbl                               num_tbl_type;
8280 
8281    l_rep_set_of_books_id_tbl                    num_tbl_type;
8282 
8283    l_error_level_tbl                            char_tbl_type;
8284    l_err_entity_id_tbl                          num_tbl_type;
8285    l_err_event_id_tbl                           num_tbl_type;
8286    l_err_ae_header_id_tbl                       num_tbl_type;
8287    l_err_ae_line_num_tbl                        num_tbl_type;
8288    l_err_temp_line_num_tbl                      num_tbl_type;
8289    l_error_message_name_tbl                     char_tbl_type;
8290 
8291    CURSOR c_deferred_deprn IS
8292       select /*+ leading(df) rowid(df) */ distinct
8293              df.asset_id,
8294              df.corp_book_type_code,
8295              df.tax_book_type_code,
8296              bc.set_of_books_id,
8297              bc.org_id,
8298              'DEFERRED DEPRN',
8299              dp.calendar_period_close_date,
8300              'DEFERRED_DEPRECIATION' event_class_code,
8301              dp.period_name,
8302              dp.calendar_period_close_date,
8303              df.corp_period_counter,
8304              df.tax_period_counter,
8305              glsob.currency_code,
8306              nvl (bc.je_deferred_deprn_category, 'OTHER') je_category_name
8307       from   fa_deferred_deprn df,
8308              fa_deprn_periods dp,
8309              gl_sets_of_books glsob,
8310              fa_book_controls bc,
8311              fa_lookups_tl lk_de,
8312              fa_lookups_tl lk_dr,
8313              gl_je_headers gljh,
8314              gl_period_statuses ps
8315       where  df.rowid between p_start_rowid and p_end_rowid
8316       and    df.corp_book_type_code = bc.book_type_code
8317       and    bc.set_of_books_id = glsob.set_of_books_id
8318       and    dp.book_type_code = df.corp_book_type_code
8319       and    dp.period_counter = df.corp_period_counter
8320       and    df.event_id is null
8321       and    ps.application_id = 101
8322       and    ((ps.migration_status_code in ('P', 'U')) or
8323               (dp.period_close_date is null))
8324       and    substr(dp.xla_conversion_status, 1, 1) in
8325              ('H', 'U', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
8326       and    ps.set_of_books_id = bc.set_of_books_id
8327       and    ps.period_name = dp.period_name
8328       and    lk_de.lookup_type = 'JOURNAL ENTRIES'
8329       and    lk_dr.lookup_type = 'JOURNAL ENTRIES'
8330       and    lk_de.lookup_code = 'DEFERRED DEPRN EXPENSE'
8331       and    lk_dr.lookup_code = 'DEFERRED DEPRN RESERVE'
8332       and    userenv('LANG') = lk_de.language
8333       and    userenv('LANG') = lk_dr.language
8334       and    df.je_header_id = gljh.je_header_id (+);
8335 
8336    CURSOR c_detail (l_corp_book_type_code varchar2,
8337                     l_tax_book_type_code  varchar2,
8338                     l_asset_id            number,
8339                     l_corp_period_counter number,
8340                     l_tax_period_counter  number) IS
8341    select df.distribution_id,
8342           df.deferred_deprn_expense_amount,
8343           df.deferred_deprn_reserve_amount,
8344           df.deferred_deprn_expense_ccid,
8345           df.deferred_deprn_reserve_ccid,
8346           nvl(df.expense_je_line_num, 0),
8347           nvl(df.reserve_je_line_num, 0),
8348           lk_de.description,
8349           lk_dr.description,
8350           gljh.je_batch_id,
8351           df.je_header_id,
8352           decode (df.je_header_id, null, 'N', 'Y')
8353     from  fa_deferred_deprn df,
8354           fa_lookups_tl lk_de,
8355           fa_lookups_tl lk_dr,
8356           gl_je_headers gljh
8357     where df.corp_book_type_code = l_corp_book_type_code
8358     and   df.tax_book_type_code = l_tax_book_type_code
8359     and   df.asset_id = l_asset_id
8360     and   df.corp_period_counter = l_corp_period_counter
8361     and   df.tax_period_counter = l_tax_period_counter
8362     and   lk_de.lookup_type = 'JOURNAL ENTRIES'
8363     and   lk_dr.lookup_type = 'JOURNAL ENTRIES'
8364     and   lk_de.lookup_code = 'DEFERRED DEPRN EXPENSE'
8365     and   lk_dr.lookup_code = 'DEFERRED DEPRN RESERVE'
8366     and   userenv('LANG') = lk_de.language
8367     and   userenv('LANG') = lk_dr.language
8368     and   df.je_header_id = gljh.je_header_id (+);
8369 
8370    CURSOR c_mc_books (l_book_type_code      varchar2) IS
8371    select set_of_books_id
8372      from fa_mc_book_controls
8373     where book_type_code = l_book_type_code
8374       and enabled_flag = 'Y';
8375 
8376    CURSOR c_mc_detail (l_corp_book_type_code varchar2,
8377                     l_tax_book_type_code  varchar2,
8378                     l_asset_id            number,
8379                     l_set_of_books_id     number,
8380                     l_corp_period_counter number,
8381                     l_tax_period_counter  number) IS
8382    select df.distribution_id,
8383           df.deferred_deprn_expense_amount,
8384           df.deferred_deprn_reserve_amount,
8385           df.deferred_deprn_expense_ccid,
8386           df.deferred_deprn_reserve_ccid,
8387           nvl(df.expense_je_line_num, 0),
8388           nvl(df.reserve_je_line_num, 0),
8389           lk_de.description,
8390           lk_dr.description,
8391           gljh.je_batch_id,
8392           df.je_header_id,
8393           decode (df.je_header_id, null, 'N', 'Y'),
8394           glsob.currency_code
8395     from  fa_mc_deferred_deprn df,
8396           fa_lookups_tl lk_de,
8397           fa_lookups_tl lk_dr,
8398           gl_je_headers gljh,
8399           gl_sets_of_books glsob
8400     where df.corp_book_type_code = l_corp_book_type_code
8401     and   df.tax_book_type_code = l_tax_book_type_code
8402     and   df.asset_id = l_asset_id
8403     and   df.set_of_books_id = l_set_of_books_id
8404     and   df.corp_period_counter = l_corp_period_counter
8405     and   df.tax_period_counter = l_tax_period_counter
8406     and   lk_de.lookup_type = 'JOURNAL ENTRIES'
8407     and   lk_dr.lookup_type = 'JOURNAL ENTRIES'
8408     and   lk_de.lookup_code = 'DEFERRED DEPRN EXPENSE'
8409     and   lk_dr.lookup_code = 'DEFERRED DEPRN RESERVE'
8410     and   userenv('LANG') = lk_de.language
8411     and   userenv('LANG') = lk_dr.language
8412     and   df.je_header_id = gljh.je_header_id (+)
8413     and   df.set_of_books_id = glsob.set_of_books_id;
8414 
8415 BEGIN
8416 
8417    x_success_count := 0;
8418    x_failure_count := 0;
8419 
8420    l_batch_size := nvl(nvl(p_batch_size, fa_cache_pkg.fa_batch_size), 1000);
8421 
8422    open c_deferred_deprn;
8423 
8424    loop
8425 
8426          fetch c_deferred_deprn bulk collect
8427           into
8428                l_asset_id_tbl,
8429                l_corp_book_type_code_tbl,
8430                l_tax_book_type_code_tbl,
8431                l_set_of_books_id_tbl,
8432                l_org_id_tbl,
8433                l_transaction_type_code_tbl,
8434                l_transaction_date_entered_tbl,
8435                l_event_class_code_tbl,
8436                l_period_name_tbl,
8437                l_cal_period_close_date_tbl,
8438                l_corp_period_counter_tbl,
8439                l_tax_period_counter_tbl,
8440                l_currency_code_tbl,
8441                l_je_category_name_tbl
8442                limit l_batch_size;
8443 
8444       FOR i IN 1..l_asset_id_tbl.count LOOP
8445          select xla_transaction_entities_s.nextval,
8446                 xla_events_s.nextval
8447           into  l_entity_id_tbl(i),
8448                 l_event_id_tbl(i)
8449           from  dual;
8450 
8451       END LOOP;
8452 
8453       -- Update fa_deferred_deprn table with event_id
8454       FORALL l_count IN 1..l_event_id_tbl.count
8455          update fa_deferred_deprn
8456          set    event_id = l_event_id_tbl(l_count)
8457          where  rowid between p_start_rowid and p_end_rowid
8458          and    asset_id = l_asset_id_tbl(l_count)
8459          and    corp_book_type_code = l_corp_book_type_code_tbl(l_count)
8460          and    tax_book_type_code = l_tax_book_type_code_tbl(l_count)
8461          and    corp_period_counter = l_corp_period_counter_tbl(l_count)
8462          and    event_id is null;
8463 
8464 
8465       l_rows_processed := l_event_id_tbl.count;
8466 
8467       -- Update fa_mc_deferred_deprn table with event_id
8468       FORALL l_count IN 1..l_event_id_tbl.count
8469          update fa_mc_deferred_deprn
8470          set    event_id = l_event_id_tbl(l_count)
8471          where  asset_id = l_asset_id_tbl(l_count)
8472          and    corp_book_type_code = l_corp_book_type_code_tbl(l_count)
8473          and    tax_book_type_code = l_tax_book_type_code_tbl(l_count)
8474          and    corp_period_counter = l_corp_period_counter_tbl(l_count)
8475          and    event_id is null;
8476 
8477       -- Add events to fa_deferred_deprn_events table
8478       FOR l_count IN 1..l_event_id_tbl.count LOOP
8479          insert into fa_deferred_deprn_events (
8480             asset_id,
8481             corp_book_type_code,
8482             tax_book_type_code,
8483             corp_period_counter,
8484             tax_period_counter,
8485             event_id,
8486             creation_date,
8487             created_by,
8488             last_update_date,
8489             last_updated_by,
8490             last_update_login
8491          ) select
8492             l_asset_id_tbl(l_count),
8493             l_corp_book_type_code_tbl(l_count),
8494             l_tax_book_type_code_tbl(l_count),
8495             l_corp_period_counter_tbl(l_count),
8496             l_tax_period_counter_tbl(l_count),
8497             l_event_id_tbl(l_count),
8498             sysdate,
8499             c_upgrade_bugno,
8500             sysdate,
8501             c_upgrade_bugno,
8502             c_upgrade_bugno
8503          from dual
8504          where not exists
8505          (
8506           select 'x'
8507           from   fa_deferred_deprn_events
8508           where  asset_id = l_asset_id_tbl(l_count)
8509           and    corp_book_type_code = l_corp_book_type_code_tbl(l_count)
8510           and    corp_period_counter = l_corp_period_counter_tbl(l_count)
8511           and    tax_book_type_code = l_tax_book_type_code_tbl(l_count)
8512           and    tax_period_counter = l_tax_period_counter_tbl(l_count)
8513          );
8514       END LOOP;
8515 
8516       -- Business Rules for xla_transaction_entities
8517       -- * ledger_id is the same as set_of_books_id
8518       -- * legal_entity_id is null
8519       -- * entity_code can be TRANSACTIONS or DEPRECIATION
8520       -- * for TRANSACTIONS:
8521       --       source_id_int_1 is transaction_header_id
8522       --       transaction_number is transaction_header_id
8523       -- * for DEPRECIATION:
8524       --       source_id_int is asset_id
8525       --       source_id_int_2 is period_counter
8526       --       source_id_int_3 is deprn_run_id (always 1 for upgrade)
8527       --       transaction_number is set_of_books_id
8528       -- * source_char_id_1 is book_type_code
8529       -- * valuation_method is book_type_code
8530 
8531       FORALL i IN 1..l_event_id_tbl.count
8532          INSERT INTO xla_transaction_entities_upg (
8533             upg_batch_id,
8534             application_id,
8535             ledger_id,
8536             legal_entity_id,
8537             entity_code,
8538             source_id_int_1,
8539             source_id_int_2,
8540             source_id_int_3,
8541             source_id_int_4,
8542             source_id_char_1,
8543             source_id_char_2,
8544             source_id_char_3,
8545             source_id_char_4,
8546             security_id_int_1,
8547             security_id_int_2,
8548             security_id_int_3,
8549             security_id_char_1,
8550             security_id_char_2,
8551             security_id_char_3,
8552             transaction_number,
8553             valuation_method,
8554             source_application_id,
8555             creation_date,
8556             created_by,
8557             last_update_date,
8558             last_updated_by,
8559             last_update_login,
8560             entity_id,
8561             upg_source_application_id
8562          ) values (
8563             l_upg_batch_id,                        -- upg_batch_id
8564             c_application_id,                      -- application_id
8565             l_set_of_books_id_tbl(i),              -- ledger_id
8566             null,                                  -- legal_entity_id,
8567             c_entity_code,                         -- entity_code
8568             l_asset_id_tbl(i),                     -- source_id_int_1
8569             null,                                  -- source_id_int_2
8570             l_corp_period_counter_tbl(i),          -- source_id_int_3
8571             null,                                  -- source_id_int_4
8572             null,                                  -- source_id_char_1
8573             l_corp_book_type_code_tbl(i),          -- source_id_char_2
8574             null,                                  -- source_id_char_3
8575             l_tax_book_type_code_tbl(i),           -- source_id_char_4
8576             null,                                  -- security_id_int_1
8577             null,                                  -- security_id_int_2
8578             null,                                  -- security_id_int_3
8579             null,                                  -- security_id_char_1
8580             null,                                  -- security_id_char_2
8581             null,                                  -- security_id_char_3
8582             l_set_of_books_id_tbl(i),              -- transaction number
8583             l_corp_book_type_code_tbl(i),          -- valuation_method
8584             c_application_id,                      -- source_application_id
8585             sysdate,                               -- creation_date
8586             c_fnd_user,                            -- created_by
8587             sysdate,                               -- last_update_date
8588             c_upgrade_bugno,                       -- last_update_by
8589             c_upgrade_bugno,                       -- last_update_login
8590             l_entity_id_tbl(i),                    -- entity_id
8591             c_application_id                       -- upg_source_application_id
8592          );
8593 
8594       -- Business Rules for xla_transaction_entities
8595       -- * event_type_code is similar to transaction_type_code
8596       -- * event_number is 1, but is the serial event for chronological order
8597       -- * event_status_code: N if event creates no journal entries
8598       --                      P if event would ultimately yield journals
8599       --                      I if event is not ready to be processed
8600       --                      U never use this value for upgrade
8601       -- * process_status_code: E if error and journals not yet created
8602       --                        P if processed and journals already generated
8603       --                        U if unprocessed and journals not generated
8604       --                        D only used for Global Accounting Engine
8605       --                        I do not use for upgrade
8606       -- * on_hold_flag: N should always be this value for upgraded entries
8607       -- * event_date is basically transaction_date_entered
8608 
8609       FORALL i IN 1..l_event_id_tbl.count
8610          insert into xla_events (
8611             upg_batch_id,
8612             application_id,
8613             event_type_code,
8614             event_number,
8615             event_status_code,
8616             process_status_code,
8617             on_hold_flag,
8618             event_date,
8619             creation_date,
8620             created_by,
8621             last_update_date,
8622             last_updated_by,
8623             last_update_login,
8624             program_update_date,
8625             program_id,
8626             program_application_id,
8627             request_id,
8628             entity_id,
8629             event_id,
8630             upg_source_application_id,
8631             transaction_date
8632          ) values (
8633             l_upg_batch_id,                          -- upg_batch_id
8634             c_application_id,                        -- application_id
8635             l_event_class_code_tbl(i),               -- event_type_code
8636             '1',                                     -- event_number
8637             'P',                                     -- event_status_code
8638             'P',                                     -- process_status_code
8639             'N',                                     -- on_hold_flag
8640             l_cal_period_close_date_tbl(i),          -- event_date
8641 -- Bug 7036409  l_transaction_date_entered_tbl(i),       -- event_date
8642             sysdate,                                 -- creation_date
8643             c_fnd_user,                              -- created_by
8644             sysdate,                                 -- last_update_date
8645             c_upgrade_bugno,                         -- last_update_by
8646             c_upgrade_bugno,                         -- last_update_login
8647             null,                                    -- program_update_date
8648             null,                                    -- program_id
8649             null,                                    -- program_application_id
8650             null,                                    -- program_update_date
8651             l_entity_id_tbl(i),                      -- entity_id
8652             l_event_id_tbl(i),                       -- event_id
8653             c_application_id,                        -- upg_source_appl_id
8654             l_transaction_date_entered_tbl(i)        -- transaction_date
8655          );
8656 
8657       FOR i IN 1..l_event_id_tbl.count LOOP
8658 
8659             open c_detail (l_corp_book_type_code_tbl(i),
8660                            l_tax_book_type_code_tbl(i),
8661                            l_asset_id_tbl(i),
8662                            l_corp_period_counter_tbl(i),
8663                            l_tax_period_counter_tbl(i));
8664             fetch c_detail bulk collect
8665             into l_distribution_id_tbl,
8666                  l_debit_amount_tbl,
8667                  l_credit_amount_tbl,
8668                  l_dr_ccid_tbl,
8669                  l_cr_ccid_tbl,
8670                  l_exp_je_line_num_tbl,
8671                  l_rsv_je_line_num_tbl,
8672                  l_dfr_exp_desc_tbl,
8673                  l_dfr_rsv_desc_tbl,
8674                  l_je_batch_id_tbl,
8675                  l_je_header_id_tbl,
8676                  l_gl_transfer_status_code_tbl;
8677             close c_detail;
8678 
8679             select xla_ae_headers_s.nextval
8680             into   l_ae_header_id
8681             from   dual;
8682 
8683             FOR j IN 1..l_distribution_id_tbl.count LOOP
8684 
8685                select xla_gl_sl_link_id_s.nextval
8686                into   l_exp_xla_gl_sl_link_id_tbl(j)
8687                from   dual;
8688 
8689                select xla_gl_sl_link_id_s.nextval
8690                into   l_rsv_xla_gl_sl_link_id_tbl(j)
8691                from   dual;
8692 
8693                l_line_num_tbl(j) := (j - 1) * 2;
8694             END LOOP;
8695 
8696       -- Business Rules for xla_ae_headers
8697       -- * amb_context_code is DEFAULT
8698       -- * reference_date must be null
8699       -- * balance_type_code:
8700       --     A: Actual
8701       --     B: Budget
8702       --     E: Encumbrance
8703       -- * gl_transfer_status_code:
8704       --     Y: already transferred to GL
8705       --     N: not transferred to GL
8706       -- * gl_transfer_date is date entry transferred to GL
8707       -- * accounting_entry_status_code must be F
8708       -- * accounting_entry_type_code must be STANDARD
8709       -- * product_rule_* not relevant for upgrade
8710 
8711          insert into xla_ae_headers (
8712             upg_batch_id,
8713             application_id,
8714             amb_context_code,
8715             entity_id,
8716             event_id,
8717             event_type_code,
8718             ae_header_id,
8719             ledger_id,
8720             accounting_date,
8721             period_name,
8722             reference_date,
8723             balance_type_code,
8724             je_category_name,
8725             gl_transfer_status_code,
8726             gl_transfer_date,
8727             accounting_entry_status_code,
8728             accounting_entry_type_code,
8729             description,
8730             budget_version_id,
8731             funds_status_code,
8732             encumbrance_type_id,
8733             completed_date,
8734             doc_sequence_id,
8735             doc_sequence_value,
8736             doc_category_code,
8737             packet_id,
8738             group_id,
8739             creation_date,
8740             created_by,
8741             last_update_date,
8742             last_updated_by,
8743             last_update_login,
8744             program_id,
8745             program_application_id,
8746             program_update_date,
8747             request_id,
8748             close_acct_seq_assign_id,
8749             close_acct_seq_version_id,
8750             close_acct_seq_value,
8751             completion_acct_seq_assign_id,
8752             completion_acct_seq_version_id,
8753             completion_acct_seq_value,
8754             accounting_batch_id,
8755             product_rule_type_code,
8756             product_rule_code,
8757             product_rule_version,
8758             upg_source_application_id,
8759             upg_valid_flag
8760          ) values (
8761             l_upg_batch_id,                     -- upg_batch_id
8762             c_application_id,                   -- application_id
8763             c_amb_context_code,                 -- amb_context_code
8764             l_entity_id_tbl(i),                 -- entity_id
8765             l_event_id_tbl(i),                  -- event_id,
8766             l_event_class_code_tbl(i),          -- event_type_code
8767             l_ae_header_id,                     -- ae_header_id,
8768             l_set_of_books_id_tbl(i),           -- ledger_id/sob_id
8769             l_cal_period_close_date_tbl(i),     -- accounting_date,
8770             l_period_name_tbl(i),               -- period_name,
8771             null,                               -- reference_date
8772             'A',                                -- balance_type_code,
8773             l_je_category_name_tbl(i),          -- je_category_name
8774             l_gl_transfer_status_code_tbl(1),   -- gl_transfer_status_code
8775             null,                               -- gl_transfer_date
8776             'F',                                -- accounting_entry_status_code
8777             'STANDARD',                         -- accounting_entry_type_code
8778             'Deferred Depreciation - ' ||
8779                to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
8780                                                 -- description
8781             null,                               -- budget_version_id
8782             null,                               -- funds_status_code
8783             null,                               -- encumbrance_type_id
8784             null,                               -- completed_date
8785             null,                               -- doc_sequence_id
8786             null,                               -- doc_sequence_value
8787             null,                               -- doc_category_code
8788             null,                               -- packet_id,
8789             null,                               -- group_id
8790             sysdate,                            -- creation_date
8791             c_fnd_user,                         -- created_by
8792             sysdate,                            -- last_update_date
8793             c_fnd_user,                         -- last_updated_by
8794             c_upgrade_bugno,                    -- last_update_login
8795             null,                               -- program_id
8796             c_application_id,                   -- program_application_id
8797             sysdate,                            -- program_update_date
8798             null,                               -- request_id
8799             null,                               -- close_acct_seq_assign_id
8800             null,                               -- close_acct_seq_version_id
8801             null,                               -- close_acct_seq_value
8802             null,                               -- compl_acct_seq_assign_id
8803             null,                               -- compl_acct_seq_version_id
8804             null,                               -- compl_acct_seq_value
8805             null,                               -- accounting_batch_id
8806             null,                               -- product_rule_type_code
8807             null,                               -- product_rule_code
8808             null,                               -- product_rule_version
8809             c_application_id,                   -- upg_souce_application_id
8810             null                                -- upg_valid_flag
8811          );
8812 
8813       -- Business Rules for xla_ae_lines
8814       -- * gl_transfer_mode_code:
8815       --       D: Detailed mode when transferred to GL
8816       --       S: Summary mode when transferred to GL
8817       -- * gl_sl_link_table must be XLAJEL
8818       -- * currency_conversion_* needs to be populated only if different from
8819       --       ledger currency
8820 
8821       FORALL j IN 1..l_distribution_id_tbl.count
8822          insert into xla_ae_lines (
8823             upg_batch_id,
8824             ae_header_id,
8825             ae_line_num,
8826             displayed_line_number,
8827             application_id,
8828             code_combination_id,
8829             gl_transfer_mode_code,
8830             accounted_dr,
8831             accounted_cr,
8832             currency_code,
8833             currency_conversion_date,
8834             currency_conversion_rate,
8835             currency_conversion_type,
8836             entered_dr,
8837             entered_cr,
8838             description,
8839             accounting_class_code,
8840             gl_sl_link_id,
8841             gl_sl_link_table,
8842             party_type_code,
8843             party_id,
8844             party_site_id,
8845             statistical_amount,
8846             ussgl_transaction_code,
8847             jgzz_recon_ref,
8848             control_balance_flag,
8849             analytical_balance_flag,
8850             upg_tax_reference_id1,
8851             upg_tax_reference_id2,
8852             upg_tax_reference_id3,
8853             creation_date,
8854             created_by,
8855             last_update_date,
8856             last_updated_by,
8857             last_update_login,
8858             program_update_date,
8859             program_id,
8860             program_application_id,
8861             request_id,
8862             gain_or_loss_flag,
8863             accounting_date,
8864             ledger_id
8865          ) values (
8866             l_upg_batch_id,                 -- upg_batch_id
8867             l_ae_header_id,                 -- ae_header_id
8868             l_line_num_tbl(j) + 1,          -- ae_line_num
8869             l_line_num_tbl(j) + 1,          -- displayed_line_num
8870             c_application_id,               -- application_id
8871             l_dr_ccid_tbl(j),               -- code_combination_id
8872             'S',                            -- gl_transfer_mode_code
8873             l_debit_amount_tbl(j),          -- accounted_dr
8874             null,                           -- accounted_cr
8875             l_currency_code_tbl(i),         -- currency_code
8876             null,                           -- currency_conversion_date
8877             null,                           -- currency_conversion_rate
8878             null,                           -- currency_conversion_type
8879             l_debit_amount_tbl(j),          -- entered_dr
8880             null,                           -- entered_cr
8881             l_dfr_exp_desc_tbl(j) || ' - ' ||
8882                to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
8883                                             -- description
8884             'EXPENSE',                      -- accounting_class_code
8885             l_exp_xla_gl_sl_link_id_tbl(j), -- gl_sl_link_id
8886             'XLAJEL',                       -- gl_sl_link_table
8887             null,                           -- party_type_code
8888             null,                           -- party_id
8889             null,                           -- party_site_id
8890             null,                           -- statistical_amount
8891             null,                           -- ussgl_transaction_code
8892             null,                           -- glzz_recon_ref
8893             null,                           -- control_balance_flag
8894             null,                           -- analytical_balance_flag
8895             null,                           -- upg_tax_reference_id1
8896             null,                           -- upg_tax_reference_id2
8897             null,                           -- upg_tax_reference_id3
8898             sysdate,                        -- creation_date
8899             c_fnd_user,                     -- created_by
8900             sysdate,                        -- last_update_date
8901             c_fnd_user,                     -- last_updated_by
8902             c_upgrade_bugno,                -- last_update_login
8903             null,                           -- program_update_date
8904             null,                           -- program_id
8905             c_application_id,               -- program_application_id
8906             null,                           -- request_id
8907             'N',                            -- gain_or_loss_flag
8908             l_cal_period_close_date_tbl(i), -- accounting_date,
8909             l_set_of_books_id_tbl(i)        -- ledger_id/sob_id
8910          );
8911 
8912       FORALL j IN 1..l_distribution_id_tbl.count
8913          insert into xla_ae_lines (
8914             upg_batch_id,
8915             ae_header_id,
8916             ae_line_num,
8917             displayed_line_number,
8918             application_id,
8919             code_combination_id,
8920             gl_transfer_mode_code,
8921             accounted_dr,
8922             accounted_cr,
8923             currency_code,
8924             currency_conversion_date,
8925             currency_conversion_rate,
8926             currency_conversion_type,
8927             entered_dr,
8928             entered_cr,
8929             description,
8930             accounting_class_code,
8931             gl_sl_link_id,
8932             gl_sl_link_table,
8933             party_type_code,
8934             party_id,
8935             party_site_id,
8936             statistical_amount,
8937             ussgl_transaction_code,
8938             jgzz_recon_ref,
8939             control_balance_flag,
8940             analytical_balance_flag,
8941             upg_tax_reference_id1,
8942             upg_tax_reference_id2,
8943             upg_tax_reference_id3,
8944             creation_date,
8945             created_by,
8946             last_update_date,
8947             last_updated_by,
8948             last_update_login,
8949             program_update_date,
8950             program_id,
8951             program_application_id,
8952             request_id,
8953             gain_or_loss_flag,
8954             accounting_date,
8955             ledger_id
8956          ) values (
8957             l_upg_batch_id,                 -- upg_batch_id
8958             l_ae_header_id,                 -- ae_header_id
8959             l_line_num_tbl(j) + 2,          -- ae_line_num
8960             l_line_num_tbl(j) + 2,          -- displayed_line_num
8961             c_application_id,               -- application_id
8962             l_cr_ccid_tbl(j),               -- code_combination_id
8963             'S',                            -- gl_transfer_mode_code
8964             null,                           -- accounted_dr
8965             l_credit_amount_tbl(j),         -- accounted_cr
8966             l_currency_code_tbl(i),         -- currency_code
8967             null,                           -- currency_conversion_date
8968             null,                           -- currency_conversion_rate
8969             null,                           -- currency_conversion_type
8970             null,                           -- entered_dr
8971             l_credit_amount_tbl(j),         -- entered_cr
8972             l_dfr_rsv_desc_tbl(j) || ' - ' ||
8973                to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
8974             'ASSET',                        -- accounting_class_code
8975             l_rsv_xla_gl_sl_link_id_tbl(j), -- gl_sl_link_id
8976             'XLAJEL',                       -- gl_sl_link_table
8977             null,                           -- party_type_code
8978             null,                           -- party_id
8979             null,                           -- party_site_id
8980             null,                           -- statistical_amount
8981             null,                           -- ussgl_transaction_code
8982             null,                           -- glzz_recon_ref
8983             null,                           -- control_balance_flag
8984             null,                           -- analytical_balance_flag
8985             null,                           -- upg_tax_reference_id1
8986             null,                           -- upg_tax_reference_id2
8987             null,                           -- upg_tax_reference_id3
8988             sysdate,                        -- creation_date
8989             c_fnd_user,                     -- created_by
8990             sysdate,                        -- last_update_date
8991             c_fnd_user,                     -- last_updated_by
8992             c_upgrade_bugno,                -- last_update_login
8993             null,                           -- program_update_date
8994             null,                           -- program_id
8995             c_application_id,               -- program_application_id
8996             null,                           -- request_id
8997             'N',                            -- gain_or_loss_flag
8998             l_cal_period_close_date_tbl(i), -- accounting_date,
8999             l_set_of_books_id_tbl(i)        -- ledger_id/sob_id
9000          );
9001 
9002       -- Business Rules for xla_distribution_links
9003       -- * accounting_line_code is similar to adjustment_type
9004       -- * accounting_line_type_code is S
9005       -- * merge_duplicate_code is N
9006       -- * source_distribution_type is DEFERRED
9007       -- * source_distribution_id_num_1 is transaction_header_id
9008       -- * source_distribution_id_num_2 is event_id
9009 
9010       FORALL j IN 1..l_distribution_id_tbl.count
9011          insert into xla_distribution_links (
9012             upg_batch_id,
9013             application_id,
9014             event_id,
9015             ae_header_id,
9016             ae_line_num,
9017             accounting_line_code,
9018             accounting_line_type_code,
9019             source_distribution_type,
9020             source_distribution_id_char_1,
9021             source_distribution_id_char_2,
9022             source_distribution_id_char_3,
9023             source_distribution_id_char_4,
9024             source_distribution_id_char_5,
9025             source_distribution_id_num_1,
9026             source_distribution_id_num_2,
9027             source_distribution_id_num_3,
9028             source_distribution_id_num_4,
9029             source_distribution_id_num_5,
9030             merge_duplicate_code,
9031             statistical_amount,
9032             unrounded_entered_dr,
9033             unrounded_entered_cr,
9034             unrounded_accounted_dr,
9035             unrounded_accounted_cr,
9036             ref_ae_header_id,
9037             ref_temp_line_num,
9038             ref_event_id,
9039             temp_line_num,
9040             tax_line_ref_id,
9041             tax_summary_line_ref_id,
9042             tax_rec_nrec_dist_ref_id,
9043             line_definition_owner_code,
9044             line_definition_code,
9045             event_class_code,
9046             event_type_code
9047          ) values (
9048             l_upg_batch_id,                 -- upg_batch_id
9049             c_application_id,               -- application_id
9050             l_event_id_tbl(i),              -- event_id
9051             l_ae_header_id,                 -- ae_header_id
9052             l_line_num_tbl(j) + 1,          -- ae_line_num
9053             null,                           -- accounting_line_code
9054             'S',                            -- accounting_line_type_code
9055             'DEFERRED',                     -- source_distribution_type
9056             null,                           -- source_distribution_id_char_1
9057             null,                           -- source_distribution_id_char_2
9058             null,                           -- source_distribution_id_char_3
9059             l_corp_book_type_code_tbl(i),   -- source_distribution_id_char_4
9060             l_tax_book_type_code_tbl(i),    -- source_distribution_id_char_5
9061             l_asset_id_tbl(i),              -- source_distribution_id_num_1
9062             l_corp_period_counter_tbl(i),   -- source_distribution_id_num_2
9063             l_distribution_id_tbl(j),       -- source_distribution_id_num_3
9064             null,                           -- source_distribution_id_num_4
9065             null,                           -- source_distribution_id_num_5
9066             'N',                            -- merge_duplicate_code
9067             null,                           -- statistical_amount
9068             l_debit_amount_tbl(j),          -- unrounded_entered_dr
9069             null,                           -- unrounded_entered_cr
9070             l_debit_amount_tbl(j),          -- unrounded_accounted_dr
9071             null,                           -- unrounded_accounted_cr
9072             l_ae_header_id,                 -- ref_ae_header_id
9073             null,                           -- ref_temp_line_num
9074             null,                           -- ref_event_id
9075             l_line_num_tbl(j) + 1,          -- temp_line_num
9076             null,                           -- tax_line_ref_id
9077             null,                           -- tax_summary_line_ref_id
9078             null,                           -- tax_rec_nrec_dist_ref_id
9079             null,                           -- line_definition_owner_code
9080             null,                           -- line_definition_code
9081             l_event_class_code_tbl(i),      -- event_class_code
9082             l_event_class_code_tbl(i)       -- event_type_code
9083          );
9084 
9085       FORALL j IN 1..l_distribution_id_tbl.count
9086          insert into xla_distribution_links (
9087             upg_batch_id,
9088             application_id,
9089             event_id,
9090             ae_header_id,
9091             ae_line_num,
9092             accounting_line_code,
9093             accounting_line_type_code,
9094             source_distribution_type,
9095             source_distribution_id_char_1,
9096             source_distribution_id_char_2,
9097             source_distribution_id_char_3,
9098             source_distribution_id_char_4,
9099             source_distribution_id_char_5,
9100             source_distribution_id_num_1,
9101             source_distribution_id_num_2,
9102             source_distribution_id_num_3,
9103             source_distribution_id_num_4,
9104             source_distribution_id_num_5,
9105             merge_duplicate_code,
9106             statistical_amount,
9107             unrounded_entered_dr,
9108             unrounded_entered_cr,
9109             unrounded_accounted_dr,
9110             unrounded_accounted_cr,
9111             ref_ae_header_id,
9112             ref_temp_line_num,
9113             ref_event_id,
9114             temp_line_num,
9115             tax_line_ref_id,
9116             tax_summary_line_ref_id,
9117             tax_rec_nrec_dist_ref_id,
9118             line_definition_owner_code,
9119             line_definition_code,
9120             event_class_code,
9121             event_type_code
9122          ) values (
9123             l_upg_batch_id,                 -- upg_batch_id
9124             c_application_id,               -- application_id
9125             l_event_id_tbl(i),              -- event_id
9126             l_ae_header_id,                 -- ae_header_id
9127             l_line_num_tbl(j) + 2,          -- ae_line_num
9128             null,                           -- accounting_line_code
9129             'S',                            -- accounting_line_type_code
9130             'DEFERRED',                     -- source_distribution_type
9131             null,                           -- source_distribution_id_char_1
9132             null,                           -- source_distribution_id_char_2
9133             null,                           -- source_distribution_id_char_3
9134             l_corp_book_type_code_tbl(i),   -- source_distribution_id_char_4
9135             l_tax_book_type_code_tbl(i),    -- source_distribution_id_char_5
9136             l_asset_id_tbl(i),              -- source_distribution_id_num_1
9137             l_corp_period_counter_tbl(i),   -- source_distribution_id_num_2
9138             l_distribution_id_tbl(j),       -- source_distribution_id_num_3
9139             null,                           -- source_distribution_id_num_4
9140             null,                           -- source_distribution_id_num_5
9141             'N',                            -- merge_duplicate_code
9142             null,                           -- statistical_amount
9143             null,                           -- unrounded_entered_dr
9144             l_credit_amount_tbl(j),         -- unrounded_entered_cr
9145             null,                           -- unrounded_accounted_dr
9146             l_credit_amount_tbl(j),         -- unrounded_accounted_cr
9147             l_ae_header_id,                 -- ref_ae_header_id
9148             null,                           -- ref_temp_line_num
9149             null,                           -- ref_event_id
9150             l_line_num_tbl(j) + 2,          -- temp_line_num
9151             null,                           -- tax_line_ref_id
9152             null,                           -- tax_summary_line_ref_id
9153             null,                           -- tax_rec_nrec_dist_ref_id
9154             null,                           -- line_definition_owner_code
9155             null,                           -- line_definition_code
9156             l_event_class_code_tbl(i),      -- event_class_code
9157             l_event_class_code_tbl(i)       -- event_type_code
9158          );
9159 
9160          FOR j IN 1..l_distribution_id_tbl.count LOOP
9161                if (l_je_batch_id_tbl(j) is not null) then
9162                   insert into gl_import_references (
9163                      je_batch_id,
9164                      je_header_id,
9165                      je_line_num,
9166                      last_update_date,
9167                      last_updated_by,
9168                      creation_date,
9169                      created_by,
9170                      last_update_login,
9171                      reference_1,
9172                      reference_2,
9173                      reference_3,
9174                      reference_4,
9175                      reference_5,
9176                      reference_6,
9177                      reference_7,
9178                      reference_8,
9179                      reference_9,
9180                      reference_10,
9181                      subledger_doc_sequence_id,
9182                      subledger_doc_sequence_value,
9183                      gl_sl_link_id,
9184                      gl_sl_link_table
9185                   ) values (
9186                      l_je_batch_id_tbl(j),        -- je_batch_id
9187                      l_je_header_id_tbl(j),       -- je_header_id
9188                      l_exp_je_line_num_tbl(j),    -- je_line_num
9189                      sysdate,                     -- last_update_date
9190                      c_fnd_user,                  -- last_updated_by
9191                      sysdate,                     -- creation_date
9192                      c_fnd_user,                  -- created_by
9193                      c_upgrade_bugno,             -- last_update_login
9194                      null,                        -- reference_1
9195                      to_char(l_asset_id_tbl(i)),  -- reference_2
9196                      to_char(l_distribution_id_tbl(j)),
9197                                                   -- reference_3
9198                      null,                        -- reference_4
9199                      l_corp_book_type_code_tbl(j),-- reference_5
9200                      to_char(l_corp_period_counter_tbl(j)),
9201                                                   -- reference_6
9202                      null,                        -- reference_7
9203                      null,                        -- reference_8
9204                      null,                        -- reference_9
9205                      null,                        -- reference_10
9206                      null,                        -- subledger_doc_seq_id
9207                      null,                        -- subledger_doc_seq_value
9208                      l_exp_xla_gl_sl_link_id_tbl(j),
9209                                                   -- gl_sl_link_id
9210                      'XLAJEL'                     -- gl_sl_link_table
9211                   );
9212 
9213                   insert into gl_import_references (
9214                      je_batch_id,
9215                      je_header_id,
9216                      je_line_num,
9217                      last_update_date,
9218                      last_updated_by,
9219                      creation_date,
9220                      created_by,
9221                      last_update_login,
9222                      reference_1,
9223                      reference_2,
9224                      reference_3,
9225                      reference_4,
9226                      reference_5,
9227                      reference_6,
9228                      reference_7,
9229                      reference_8,
9230                      reference_9,
9231                      reference_10,
9232                      subledger_doc_sequence_id,
9233                      subledger_doc_sequence_value,
9234                      gl_sl_link_id,
9235                      gl_sl_link_table
9236                   ) values (
9237                      l_je_batch_id_tbl(j),        -- je_batch_id
9238                      l_je_header_id_tbl(j),       -- je_header_id
9239                      l_rsv_je_line_num_tbl(j),    -- je_line_num
9240                      sysdate,                     -- last_update_date
9241                      c_fnd_user,                  -- last_updated_by
9242                      sysdate,                     -- creation_date
9243                      c_fnd_user,                  -- created_by
9244                      c_upgrade_bugno,             -- last_update_login
9245                      null,                        -- reference_1
9246                      to_char(l_asset_id_tbl(i)),  -- reference_2
9247                      to_char(l_distribution_id_tbl(j)),
9248                                                   -- reference_3
9249                      null,                        -- reference_4
9250                      l_corp_book_type_code_tbl(i),-- reference_5
9251                      to_char(l_corp_period_counter_tbl(i)),
9252                                                   -- reference_6
9253                      null,                        -- reference_7
9254                      null,                        -- reference_8
9255                      null,                        -- reference_9
9256                      null,                        -- reference_10
9257                      null,                        -- subledger_doc_seq_id
9258                      null,                        -- subledger_doc_seq_value
9259                      l_rsv_xla_gl_sl_link_id_tbl(j),
9260                                                   -- gl_sl_link_id
9261                      'XLAJEL'                     -- gl_sl_link_table
9262                   );
9263                end if;
9264             end loop;
9265 
9266       l_ae_header_id_tbl.delete;
9267       l_cr_ccid_tbl.delete;
9268       l_dr_ccid_tbl.delete;
9269       l_credit_amount_tbl.delete;
9270       l_debit_amount_tbl.delete;
9271       l_exp_xla_gl_sl_link_id_tbl.delete;
9272       l_rsv_xla_gl_sl_link_id_tbl.delete;
9273       l_line_def_owner_code_tbl.delete;
9274       l_line_def_code_tbl.delete;
9275       l_dfr_exp_desc_tbl.delete;
9276       l_dfr_rsv_desc_tbl.delete;
9277       l_gl_transfer_status_code_tbl.delete;
9278       l_je_batch_id_tbl.delete;
9279       l_je_header_id_tbl.delete;
9280       l_exp_je_line_num_tbl.delete;
9281       l_rsv_je_line_num_tbl.delete;
9282       l_distribution_id_tbl.delete;
9283       l_line_num_tbl.delete;
9284 
9285       open c_mc_books (l_corp_book_type_code_tbl(i));
9286       fetch c_mc_books bulk collect
9287       into l_rep_set_of_books_id_tbl;
9288       close c_mc_books;
9289 
9290       for k IN 1..l_rep_set_of_books_id_tbl.count loop
9291 
9292             open c_mc_detail (l_corp_book_type_code_tbl(i),
9293                            l_tax_book_type_code_tbl(i),
9294                            l_asset_id_tbl(i),
9295                            l_rep_set_of_books_id_tbl(k),
9296                            l_corp_period_counter_tbl(i),
9297                            l_tax_period_counter_tbl(i));
9298             fetch c_mc_detail bulk collect
9299             into l_distribution_id_tbl,
9300                  l_debit_amount_tbl,
9301                  l_credit_amount_tbl,
9302                  l_dr_ccid_tbl,
9303                  l_cr_ccid_tbl,
9304                  l_exp_je_line_num_tbl,
9305                  l_rsv_je_line_num_tbl,
9306                  l_dfr_exp_desc_tbl,
9307                  l_dfr_rsv_desc_tbl,
9308                  l_je_batch_id_tbl,
9309                  l_je_header_id_tbl,
9310                  l_gl_transfer_status_code_tbl,
9311                  l_currency_code_tbl;
9312             close c_mc_detail;
9313 
9314             select xla_ae_headers_s.nextval
9315             into   l_ae_header_id
9316             from   dual;
9317 
9318             FOR j IN 1..l_distribution_id_tbl.count LOOP
9319 
9320                select xla_gl_sl_link_id_s.nextval
9321                into   l_exp_xla_gl_sl_link_id_tbl(j)
9322                from   dual;
9323 
9324                select xla_gl_sl_link_id_s.nextval
9325                into   l_rsv_xla_gl_sl_link_id_tbl(j)
9326                from   dual;
9327 
9328                l_line_num_tbl(j) := (j - 1) * 2;
9329             END LOOP;
9330 
9331       -- Business Rules for xla_ae_headers
9332       -- * amb_context_code is DEFAULT
9333       -- * reference_date must be null
9334       -- * balance_type_code:
9335       --     A: Actual
9336       --     B: Budget
9337       --     E: Encumbrance
9338       -- * gl_transfer_status_code:
9339       --     Y: already transferred to GL
9340       --     N: not transferred to GL
9341       -- * gl_transfer_date is date entry transferred to GL
9342       -- * accounting_entry_status_code must be F
9343       -- * accounting_entry_type_code must be STANDARD
9344       -- * product_rule_* not relevant for upgrade
9345 
9346          insert into xla_ae_headers (
9347             upg_batch_id,
9348             application_id,
9349             amb_context_code,
9350             entity_id,
9351             event_id,
9352             event_type_code,
9353             ae_header_id,
9354             ledger_id,
9355             accounting_date,
9356             period_name,
9357             reference_date,
9358             balance_type_code,
9359             je_category_name,
9360             gl_transfer_status_code,
9361             gl_transfer_date,
9362             accounting_entry_status_code,
9363             accounting_entry_type_code,
9364             description,
9365             budget_version_id,
9366             funds_status_code,
9367             encumbrance_type_id,
9368             completed_date,
9369             doc_sequence_id,
9370             doc_sequence_value,
9371             doc_category_code,
9372             packet_id,
9373             group_id,
9374             creation_date,
9375             created_by,
9376             last_update_date,
9377             last_updated_by,
9378             last_update_login,
9379             program_id,
9380             program_application_id,
9381             program_update_date,
9382             request_id,
9383             close_acct_seq_assign_id,
9384             close_acct_seq_version_id,
9385             close_acct_seq_value,
9386             completion_acct_seq_assign_id,
9387             completion_acct_seq_version_id,
9388             completion_acct_seq_value,
9389             accounting_batch_id,
9390             product_rule_type_code,
9391             product_rule_code,
9392             product_rule_version,
9393             upg_source_application_id,
9394             upg_valid_flag
9395          ) values (
9396             l_upg_batch_id,                     -- upg_batch_id
9397             c_application_id,                   -- application_id
9398             c_amb_context_code,                 -- amb_context_code
9399             l_entity_id_tbl(i),                 -- entity_id
9400             l_event_id_tbl(i),                  -- event_id,
9401             l_event_class_code_tbl(i),          -- event_type_code
9402             l_ae_header_id,                     -- ae_header_id,
9403             l_rep_set_of_books_id_tbl(k),       -- ledger_id/sob_id
9404             l_cal_period_close_date_tbl(i),     -- accounting_date,
9405             l_period_name_tbl(i),               -- period_name,
9406             null,                               -- reference_date
9407             'A',                                -- balance_type_code,
9408             l_je_category_name_tbl(i),          -- je_category_name
9409             l_gl_transfer_status_code_tbl(1),   -- gl_transfer_status_code
9410             null,                               -- gl_transfer_date
9411             'F',                                -- accounting_entry_status_code
9412             'STANDARD',                         -- accounting_entry_type_code
9413             'Deferred Depreciation - ' ||
9414                to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
9415                                                 -- description
9416             null,                               -- budget_version_id
9417             null,                               -- funds_status_code
9418             null,                               -- encumbrance_type_id
9419             null,                               -- completed_date
9420             null,                               -- doc_sequence_id
9421             null,                               -- doc_sequence_value
9422             null,                               -- doc_category_code
9423             null,                               -- packet_id,
9424             null,                               -- group_id
9425             sysdate,                            -- creation_date
9426             c_fnd_user,                         -- created_by
9427             sysdate,                            -- last_update_date
9428             c_fnd_user,                         -- last_updated_by
9429             c_upgrade_bugno,                    -- last_update_login
9430             null,                               -- program_id
9431             c_application_id,                   -- program_application_id
9432             sysdate,                            -- program_update_date
9433             null,                               -- request_id
9434             null,                               -- close_acct_seq_assign_id
9435             null,                               -- close_acct_seq_version_id
9436             null,                               -- close_acct_seq_value
9437             null,                               -- compl_acct_seq_assign_id
9438             null,                               -- compl_acct_seq_version_id
9439             null,                               -- compl_acct_seq_value
9440             null,                               -- accounting_batch_id
9441             null,                               -- product_rule_type_code
9442             null,                               -- product_rule_code
9443             null,                               -- product_rule_version
9444             c_application_id,                   -- upg_souce_application_id
9445             null                                -- upg_valid_flag
9446          );
9447 
9448       -- Business Rules for xla_ae_lines
9449       -- * gl_transfer_mode_code:
9450       --       D: Detailed mode when transferred to GL
9451       --       S: Summary mode when transferred to GL
9452       -- * gl_sl_link_table must be XLAJEL
9453       -- * currency_conversion_* needs to be populated only if different from
9454       --       ledger currency
9455 
9456       FORALL j IN 1..l_distribution_id_tbl.count
9457          insert into xla_ae_lines (
9458             upg_batch_id,
9459             ae_header_id,
9460             ae_line_num,
9461             displayed_line_number,
9462             application_id,
9463             code_combination_id,
9464             gl_transfer_mode_code,
9465             accounted_dr,
9466             accounted_cr,
9467             currency_code,
9468             currency_conversion_date,
9469             currency_conversion_rate,
9470             currency_conversion_type,
9471             entered_dr,
9472             entered_cr,
9473             description,
9474             accounting_class_code,
9475             gl_sl_link_id,
9476             gl_sl_link_table,
9477             party_type_code,
9478             party_id,
9479             party_site_id,
9480             statistical_amount,
9481             ussgl_transaction_code,
9482             jgzz_recon_ref,
9483             control_balance_flag,
9484             analytical_balance_flag,
9485             upg_tax_reference_id1,
9486             upg_tax_reference_id2,
9487             upg_tax_reference_id3,
9488             creation_date,
9489             created_by,
9490             last_update_date,
9491             last_updated_by,
9492             last_update_login,
9493             program_update_date,
9494             program_id,
9495             program_application_id,
9496             request_id,
9497             gain_or_loss_flag,
9498             accounting_date,
9499             ledger_id
9500          ) values (
9501             l_upg_batch_id,                 -- upg_batch_id
9502             l_ae_header_id,                 -- ae_header_id
9503             l_line_num_tbl(j) + 1,          -- ae_line_num
9504             l_line_num_tbl(j) + 1,          -- displayed_line_num
9505             c_application_id,               -- application_id
9506             l_dr_ccid_tbl(j),               -- code_combination_id
9507             'S',                            -- gl_transfer_mode_code
9508             l_debit_amount_tbl(j),          -- accounted_dr
9509             null,                           -- accounted_cr
9510             l_currency_code_tbl(j),         -- currency_code
9511             null,                           -- currency_conversion_date
9512             null,                           -- currency_conversion_rate
9513             null,                           -- currency_conversion_type
9514             l_debit_amount_tbl(j),          -- entered_dr
9515             null,                           -- entered_cr
9516             l_dfr_exp_desc_tbl(j) || ' - ' ||
9517                to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
9518                                             -- description
9519             'EXPENSE',                      -- accounting_class_code
9520             l_exp_xla_gl_sl_link_id_tbl(j), -- gl_sl_link_id
9521             'XLAJEL',                       -- gl_sl_link_table
9522             null,                           -- party_type_code
9523             null,                           -- party_id
9524             null,                           -- party_site_id
9525             null,                           -- statistical_amount
9526             null,                           -- ussgl_transaction_code
9527             null,                           -- glzz_recon_ref
9528             null,                           -- control_balance_flag
9529             null,                           -- analytical_balance_flag
9530             null,                           -- upg_tax_reference_id1
9531             null,                           -- upg_tax_reference_id2
9532             null,                           -- upg_tax_reference_id3
9533             sysdate,                        -- creation_date
9534             c_fnd_user,                     -- created_by
9535             sysdate,                        -- last_update_date
9536             c_fnd_user,                     -- last_updated_by
9537             c_upgrade_bugno,                -- last_update_login
9538             null,                           -- program_update_date
9539             null,                           -- program_id
9540             c_application_id,               -- program_application_id
9541             null,                           -- request_id
9542             'N',                            -- gain_or_loss_flag
9543             l_cal_period_close_date_tbl(i), -- accounting_date,
9544             l_rep_set_of_books_id_tbl(k)    -- ledger_id/sob_id
9545          );
9546 
9547       FORALL j IN 1..l_distribution_id_tbl.count
9548          insert into xla_ae_lines (
9549             upg_batch_id,
9550             ae_header_id,
9551             ae_line_num,
9552             displayed_line_number,
9553             application_id,
9554             code_combination_id,
9555             gl_transfer_mode_code,
9556             accounted_dr,
9557             accounted_cr,
9558             currency_code,
9559             currency_conversion_date,
9560             currency_conversion_rate,
9561             currency_conversion_type,
9562             entered_dr,
9563             entered_cr,
9564             description,
9565             accounting_class_code,
9566             gl_sl_link_id,
9567             gl_sl_link_table,
9568             party_type_code,
9569             party_id,
9570             party_site_id,
9571             statistical_amount,
9572             ussgl_transaction_code,
9573             jgzz_recon_ref,
9574             control_balance_flag,
9575             analytical_balance_flag,
9576             upg_tax_reference_id1,
9577             upg_tax_reference_id2,
9578             upg_tax_reference_id3,
9579             creation_date,
9580             created_by,
9581             last_update_date,
9582             last_updated_by,
9583             last_update_login,
9584             program_update_date,
9585             program_id,
9586             program_application_id,
9587             request_id,
9588             gain_or_loss_flag,
9589             accounting_date,
9590             ledger_id
9591          ) values (
9592             l_upg_batch_id,                 -- upg_batch_id
9593             l_ae_header_id,                 -- ae_header_id
9594             l_line_num_tbl(j) + 2,          -- ae_line_num
9595             l_line_num_tbl(j) + 2,          -- displayed_line_num
9596             c_application_id,               -- application_id
9597             l_cr_ccid_tbl(j),               -- code_combination_id
9598             'S',                            -- gl_transfer_mode_code
9599             null,                           -- accounted_dr
9600             l_credit_amount_tbl(j),         -- accounted_cr
9601             l_currency_code_tbl(j),         -- currency_code
9602             null,                           -- currency_conversion_date
9603             null,                           -- currency_conversion_rate
9604             null,                           -- currency_conversion_type
9605             null,                           -- entered_dr
9606             l_credit_amount_tbl(j),         -- entered_cr
9607             l_dfr_rsv_desc_tbl(j) || ' - ' ||
9608                to_char(l_cal_period_close_date_tbl(i), 'DD-MON-RR'),
9609             'ASSET',                        -- accounting_class_code
9610             l_rsv_xla_gl_sl_link_id_tbl(j), -- gl_sl_link_id
9611             'XLAJEL',                       -- gl_sl_link_table
9612             null,                           -- party_type_code
9613             null,                           -- party_id
9614             null,                           -- party_site_id
9615             null,                           -- statistical_amount
9616             null,                           -- ussgl_transaction_code
9617             null,                           -- glzz_recon_ref
9618             null,                           -- control_balance_flag
9619             null,                           -- analytical_balance_flag
9620             null,                           -- upg_tax_reference_id1
9621             null,                           -- upg_tax_reference_id2
9622             null,                           -- upg_tax_reference_id3
9623             sysdate,                        -- creation_date
9624             c_fnd_user,                     -- created_by
9625             sysdate,                        -- last_update_date
9626             c_fnd_user,                     -- last_updated_by
9627             c_upgrade_bugno,                -- last_update_login
9628             null,                           -- program_update_date
9629             null,                           -- program_id
9630             c_application_id,               -- program_application_id
9631             null,                           -- request_id
9632             'N',                            -- gain_or_loss_flag
9633             l_cal_period_close_date_tbl(i), -- accounting_date,
9634             l_rep_set_of_books_id_tbl(k)    -- ledger_id/sob_id
9635          );
9636 
9637       -- Business Rules for xla_distribution_links
9638       -- * accounting_line_code is similar to adjustment_type
9639       -- * accounting_line_type_code is S
9640       -- * merge_duplicate_code is N
9641       -- * source_distribution_type is DEFERRED
9642       -- * source_distribution_id_num_1 is transaction_header_id
9643       -- * source_distribution_id_num_2 is event_id
9644 
9645       FORALL j IN 1..l_distribution_id_tbl.count
9646          insert into xla_distribution_links (
9647             upg_batch_id,
9648             application_id,
9649             event_id,
9650             ae_header_id,
9651             ae_line_num,
9652             accounting_line_code,
9653             accounting_line_type_code,
9654             source_distribution_type,
9655             source_distribution_id_char_1,
9656             source_distribution_id_char_2,
9657             source_distribution_id_char_3,
9658             source_distribution_id_char_4,
9659             source_distribution_id_char_5,
9660             source_distribution_id_num_1,
9661             source_distribution_id_num_2,
9662             source_distribution_id_num_3,
9663             source_distribution_id_num_4,
9664             source_distribution_id_num_5,
9665             merge_duplicate_code,
9666             statistical_amount,
9667             unrounded_entered_dr,
9668             unrounded_entered_cr,
9669             unrounded_accounted_dr,
9670             unrounded_accounted_cr,
9671             ref_ae_header_id,
9672             ref_temp_line_num,
9673             ref_event_id,
9674             temp_line_num,
9675             tax_line_ref_id,
9676             tax_summary_line_ref_id,
9677             tax_rec_nrec_dist_ref_id,
9678             line_definition_owner_code,
9679             line_definition_code,
9680             event_class_code,
9681             event_type_code
9682          ) values (
9683             l_upg_batch_id,                 -- upg_batch_id
9684             c_application_id,               -- application_id
9685             l_event_id_tbl(i),              -- event_id
9686             l_ae_header_id,                 -- ae_header_id
9687             l_line_num_tbl(j) + 1,          -- ae_line_num
9688             null,                           -- accounting_line_code
9689             'S',                            -- accounting_line_type_code
9690             'DEFERRED',                     -- source_distribution_type
9691             /*
9692             null,                           -- source_distribution_id_char_1
9693             l_corp_book_type_code_tbl(i),   -- source_distribution_id_char_2
9694             null,                           -- source_distribution_id_char_3
9695             l_tax_book_type_code_tbl(i),    -- source_distribution_id_char_4
9696             null,                           -- source_distribution_id_char_5
9697             l_asset_id_tbl(i),              -- source_distribution_id_num_1
9698             null,                           -- source_distribution_id_num_2
9699             l_corp_period_counter_tbl(i),   -- source_distribution_id_num_3
9700             null,                           -- source_distribution_id_num_4
9701             l_distribution_id_tbl(j),       -- source_distribution_id_num_5 */
9702             null,                           -- source_distribution_id_char_1
9703             null,                           -- source_distribution_id_char_2
9704             null,                           -- source_distribution_id_char_3
9705             l_corp_book_type_code_tbl(i),   -- source_distribution_id_char_4
9706             l_tax_book_type_code_tbl(i),    -- source_distribution_id_char_5
9707             l_asset_id_tbl(i),              -- source_distribution_id_num_1
9708             l_corp_period_counter_tbl(i),   -- source_distribution_id_num_2
9709             l_distribution_id_tbl(j),       -- source_distribution_id_num_3
9710             null,                           -- source_distribution_id_num_4
9711             null,                           -- source_distribution_id_num_5
9712             'N',                            -- merge_duplicate_code
9713             null,                           -- statistical_amount
9714             l_debit_amount_tbl(j),          -- unrounded_entered_dr
9715             null,                           -- unrounded_entered_cr
9716             l_debit_amount_tbl(j),          -- unrounded_accounted_dr
9717             null,                           -- unrounded_accounted_cr
9718             l_ae_header_id,                 -- ref_ae_header_id
9719             null,                           -- ref_temp_line_num
9720             null,                           -- ref_event_id
9721             1,                              -- temp_line_num
9722             null,                           -- tax_line_ref_id
9723             null,                           -- tax_summary_line_ref_id
9724             null,                           -- tax_rec_nrec_dist_ref_id
9725             null,                           -- line_definition_owner_code
9726             null,                           -- line_definition_code
9727             l_event_class_code_tbl(i),      -- event_class_code
9728             l_event_class_code_tbl(i)       -- event_type_code
9729          );
9730 
9731       FORALL j IN 1..l_distribution_id_tbl.count
9732          insert into xla_distribution_links (
9733             upg_batch_id,
9734             application_id,
9735             event_id,
9736             ae_header_id,
9737             ae_line_num,
9738             accounting_line_code,
9739             accounting_line_type_code,
9740             source_distribution_type,
9741             source_distribution_id_char_1,
9742             source_distribution_id_char_2,
9743             source_distribution_id_char_3,
9744             source_distribution_id_char_4,
9745             source_distribution_id_char_5,
9746             source_distribution_id_num_1,
9747             source_distribution_id_num_2,
9748             source_distribution_id_num_3,
9749             source_distribution_id_num_4,
9750             source_distribution_id_num_5,
9751             merge_duplicate_code,
9752             statistical_amount,
9753             unrounded_entered_dr,
9754             unrounded_entered_cr,
9755             unrounded_accounted_dr,
9756             unrounded_accounted_cr,
9757             ref_ae_header_id,
9758             ref_temp_line_num,
9759             ref_event_id,
9760             temp_line_num,
9761             tax_line_ref_id,
9762             tax_summary_line_ref_id,
9763             tax_rec_nrec_dist_ref_id,
9764             line_definition_owner_code,
9765             line_definition_code,
9766             event_class_code,
9767             event_type_code
9768          ) values (
9769             l_upg_batch_id,                 -- upg_batch_id
9770             c_application_id,               -- application_id
9771             l_event_id_tbl(i),              -- event_id
9772             l_ae_header_id,                 -- ae_header_id
9773             l_line_num_tbl(j) + 2,          -- ae_line_num
9774             null,                           -- accounting_line_code
9775             'S',                            -- accounting_line_type_code
9776             'DEFERRED',                     -- source_distribution_type
9777             null,                           -- source_distribution_id_char_1
9778             null,                           -- source_distribution_id_char_2
9779             null,                           -- source_distribution_id_char_3
9780             l_corp_book_type_code_tbl(i),   -- source_distribution_id_char_4
9781             l_tax_book_type_code_tbl(i),    -- source_distribution_id_char_5
9782             l_asset_id_tbl(i),              -- source_distribution_id_num_1
9783             l_corp_period_counter_tbl(i),   -- source_distribution_id_num_2
9784             l_distribution_id_tbl(j),       -- source_distribution_id_num_3
9785             null,                           -- source_distribution_id_num_4
9786             null,                           -- source_distribution_id_num_5
9787             'N',                            -- merge_duplicate_code
9788             null,                           -- statistical_amount
9789             null,                           -- unrounded_entered_dr
9790             l_credit_amount_tbl(j),         -- unrounded_entered_cr
9791             null,                           -- unrounded_accounted_dr
9792             l_credit_amount_tbl(j),         -- unrounded_accounted_cr
9793             l_ae_header_id,                 -- ref_ae_header_id
9794             null,                           -- ref_temp_line_num
9795             null,                           -- ref_event_id
9796             l_line_num_tbl(j) + 2,          -- temp_line_num
9797             null,                           -- tax_line_ref_id
9798             null,                           -- tax_summary_line_ref_id
9799             null,                           -- tax_rec_nrec_dist_ref_id
9800             null,                           -- line_definition_owner_code
9801             null,                           -- line_definition_code
9802             l_event_class_code_tbl(i),      -- event_class_code
9803             l_event_class_code_tbl(i)       -- event_type_code
9804          );
9805 
9806          FOR j IN 1..l_distribution_id_tbl.count LOOP
9807                if (l_je_batch_id_tbl(j) is not null) then
9808                   insert into gl_import_references (
9809                      je_batch_id,
9810                      je_header_id,
9811                      je_line_num,
9812                      last_update_date,
9813                      last_updated_by,
9814                      creation_date,
9815                      created_by,
9816                      last_update_login,
9817                      reference_1,
9818                      reference_2,
9819                      reference_3,
9820                      reference_4,
9821                      reference_5,
9822                      reference_6,
9823                      reference_7,
9824                      reference_8,
9825                      reference_9,
9826                      reference_10,
9827                      subledger_doc_sequence_id,
9828                      subledger_doc_sequence_value,
9829                      gl_sl_link_id,
9830                      gl_sl_link_table
9831                   ) values (
9832                      l_je_batch_id_tbl(j),        -- je_batch_id
9833                      l_je_header_id_tbl(j),       -- je_header_id
9834                      l_exp_je_line_num_tbl(j),    -- je_line_num
9835                      sysdate,                     -- last_update_date
9836                      c_fnd_user,                  -- last_updated_by
9837                      sysdate,                     -- creation_date
9838                      c_fnd_user,                  -- created_by
9839                      c_upgrade_bugno,             -- last_update_login
9840                      null,                        -- reference_1
9841                      to_char(l_asset_id_tbl(i)),  -- reference_2
9842                      to_char(l_distribution_id_tbl(j)),
9843                                                   -- reference_3
9844                      null,                        -- reference_4
9845                      l_corp_book_type_code_tbl(j),-- reference_5
9846                      to_char(l_corp_period_counter_tbl(j)),
9847                                                   -- reference_6
9848                      null,                        -- reference_7
9849                      null,                        -- reference_8
9850                      null,                        -- reference_9
9851                      null,                        -- reference_10
9852                      null,                        -- subledger_doc_seq_id
9853                      null,                        -- subledger_doc_seq_value
9854                      l_exp_xla_gl_sl_link_id_tbl(j),
9855                                                   -- gl_sl_link_id
9856                      'XLAJEL'                     -- gl_sl_link_table
9857                   );
9858 
9859                   insert into gl_import_references (
9860                      je_batch_id,
9861                      je_header_id,
9862                      je_line_num,
9863                      last_update_date,
9864                      last_updated_by,
9865                      creation_date,
9866                      created_by,
9867                      last_update_login,
9868                      reference_1,
9869                      reference_2,
9870                      reference_3,
9871                      reference_4,
9872                      reference_5,
9873                      reference_6,
9874                      reference_7,
9875                      reference_8,
9876                      reference_9,
9877                      reference_10,
9878                      subledger_doc_sequence_id,
9879                      subledger_doc_sequence_value,
9880                      gl_sl_link_id,
9881                      gl_sl_link_table
9882                   ) values (
9883                      l_je_batch_id_tbl(j),        -- je_batch_id
9884                      l_je_header_id_tbl(j),       -- je_header_id
9885                      l_rsv_je_line_num_tbl(j),    -- je_line_num
9886                      sysdate,                     -- last_update_date
9887                      c_fnd_user,                  -- last_updated_by
9888                      sysdate,                     -- creation_date
9889                      c_fnd_user,                  -- created_by
9890                      c_upgrade_bugno,             -- last_update_login
9891                      null,                        -- reference_1
9892                      to_char(l_asset_id_tbl(i)),  -- reference_2
9893                      to_char(l_distribution_id_tbl(j)),
9894                                                   -- reference_3
9895                      null,                        -- reference_4
9896                      l_corp_book_type_code_tbl(i),-- reference_5
9897                      to_char(l_corp_period_counter_tbl(i)),
9898                                                   -- reference_6
9899                      null,                        -- reference_7
9900                      null,                        -- reference_8
9901                      null,                        -- reference_9
9902                      null,                        -- reference_10
9903                      null,                        -- subledger_doc_seq_id
9904                      null,                        -- subledger_doc_seq_value
9905                      l_rsv_xla_gl_sl_link_id_tbl(j),
9906                                                   -- gl_sl_link_id
9907                      'XLAJEL'                     -- gl_sl_link_table
9908                   );
9909                end if;
9910             end loop;
9911 
9912       l_ae_header_id_tbl.delete;
9913       l_cr_ccid_tbl.delete;
9914       l_dr_ccid_tbl.delete;
9915       l_credit_amount_tbl.delete;
9916       l_debit_amount_tbl.delete;
9917       l_exp_xla_gl_sl_link_id_tbl.delete;
9918       l_rsv_xla_gl_sl_link_id_tbl.delete;
9919       l_line_def_owner_code_tbl.delete;
9920       l_line_def_code_tbl.delete;
9921       l_dfr_exp_desc_tbl.delete;
9922       l_dfr_rsv_desc_tbl.delete;
9923       l_gl_transfer_status_code_tbl.delete;
9924       l_je_batch_id_tbl.delete;
9925       l_je_header_id_tbl.delete;
9926       l_exp_je_line_num_tbl.delete;
9927       l_rsv_je_line_num_tbl.delete;
9928       l_distribution_id_tbl.delete;
9929       l_line_num_tbl.delete;
9930 
9931       end loop;
9932 
9933       l_rep_set_of_books_id_tbl.delete;
9934 
9935     END LOOP;
9936 
9937     l_rowid_tbl.delete;
9938     l_event_id_tbl.delete;
9939     l_asset_id_tbl.delete;
9940     l_corp_book_type_code_tbl.delete;
9941     l_tax_book_type_code_tbl.delete;
9942     l_set_of_books_id_tbl.delete;
9943     l_org_id_tbl.delete;
9944     l_transaction_type_code_tbl.delete;
9945     l_transaction_date_entered_tbl.delete;
9946     l_corp_period_counter_tbl.delete;
9947     l_tax_period_counter_tbl.delete;
9948     l_period_name_tbl.delete;
9949     l_cal_period_close_date_tbl.delete;
9950     l_entity_id_tbl.delete;
9951     l_event_class_code_tbl.delete;
9952     l_currency_code_tbl.delete;
9953 
9954     commit;
9955 
9956     if (l_rows_processed < l_batch_size) then exit; end if;
9957 
9958  end loop;
9959 
9960  close c_deferred_deprn;
9961 
9962 EXCEPTION
9963    WHEN OTHERS THEN
9964       rollback;
9965       raise;
9966 
9967 END Upgrade_Deferred_Events;
9968 
9969 END FA_SLA_EVENTS_UPG_PKG;