DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_RETIREMENT_PVT

Source


1 PACKAGE BODY FA_RETIREMENT_PVT as
2 /* $Header: FAVRETB.pls 120.89.12020000.4 2013/03/19 18:04:01 dvjoshi ship $ */
3 
4 -- +++++ Global Variables +++++
5 --Adding for 3440308
6 g_current_units number:=0;
7 g_grp_trx_hdr_id number; -- Bug 8674833
8 G_PART_RET_FLAG BOOLEAN;
9 
10 -- +++++ Forward Declarations +++++
11 FUNCTION CALC_GAIN_LOSS_FOR_RET(
12                p_trans_rec         IN     FA_API_TYPES.trans_rec_type,
13                p_asset_hdr_rec     IN     FA_API_TYPES.asset_hdr_rec_type,
14                p_asset_type_rec    IN     FA_API_TYPES.asset_type_rec_type,
15                p_asset_desc_rec    IN     FA_API_TYPES.asset_desc_rec_type,
16                p_asset_cat_rec     IN     FA_API_TYPES.asset_cat_rec_type,
17                p_asset_fin_rec     IN     FA_API_TYPES.asset_fin_rec_type,
18                p_period_rec        IN     FA_API_TYPES.period_rec_type,
19                p_asset_retire_rec  IN     FA_API_TYPES.asset_retire_rec_type,
20                p_group_thid        IN     NUMBER,
21                p_salvage_value_retired IN NUMBER,
22                p_mrc_sob_type_code IN     VARCHAR2,
23                p_mode              IN     VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
24 RETURN BOOLEAN;
25 
26 FUNCTION CALC_GAIN_LOSS_FOR_REI(
27            p_trans_rec         IN            FA_API_TYPES.trans_rec_type,
28            p_asset_hdr_rec     IN            FA_API_TYPES.asset_hdr_rec_type,
29            p_asset_type_rec    IN            FA_API_TYPES.asset_type_rec_type,
30            p_asset_desc_rec    IN            FA_API_TYPES.asset_desc_rec_type,
31            p_asset_cat_rec     IN            FA_API_TYPES.asset_cat_rec_type,
32            p_asset_fin_rec     IN            FA_API_TYPES.asset_fin_rec_type,
33            p_period_rec        IN            FA_API_TYPES.period_rec_type,
34            p_asset_retire_rec  IN            FA_API_TYPES.asset_retire_rec_type,
35            p_group_thid        IN            NUMBER,
36            x_asset_fin_rec        OUT NOCOPY FA_API_TYPES.asset_fin_rec_type,
37            p_mrc_sob_type_code IN            VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
38 RETURN BOOLEAN;
39 /*bug#15897249 */
40 FUNCTION MOVE_RESERVE_TO_ACT_DIST(
41                p_trans_rec         IN     FA_API_TYPES.trans_rec_type,
42                p_asset_hdr_rec     IN     FA_API_TYPES.asset_hdr_rec_type,
43                p_period_rec        IN     FA_API_TYPES.period_rec_type,
44                p_asset_retire_rec  IN     FA_API_TYPES.asset_retire_rec_type,
45                p_mrc_sob_type_code IN     VARCHAR2,
46                p_log_level_rec     IN     FA_API_TYPES.log_level_rec_type)
47 RETURN BOOLEAN;
48 -- +++++ End of Forward Declarations +++++
49 
50 FUNCTION DO_RETIREMENT(p_trans_rec             IN     FA_API_TYPES.trans_rec_type,
51                        p_asset_retire_rec      IN     FA_API_TYPES.asset_retire_rec_type,
52                        p_asset_hdr_rec         IN     FA_API_TYPES.asset_hdr_rec_type,
53                        p_asset_type_rec        IN     FA_API_TYPES.asset_type_rec_type,
54                        p_asset_cat_rec         IN     FA_API_TYPES.asset_cat_rec_type,
55                        p_asset_fin_rec         IN     FA_API_TYPES.asset_fin_rec_type,
56                        p_asset_desc_rec        IN     FA_API_TYPES.asset_desc_rec_type,
57                        p_period_rec            IN     FA_API_TYPES.period_rec_type,
58                        p_mrc_sob_type_code     IN     VARCHAR2,
59                        p_calling_fn            IN     VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
60 return boolean IS
61 
62   l_trans_rec             FA_API_TYPES.trans_rec_type;
63   l_asset_hdr_rec         FA_API_TYPES.asset_hdr_rec_type;
64 
65   CURSOR c_get_group_exp_row IS
66     select adjustment_type,
67            debit_credit_flag,
68            adjustment_amount,
69            period_counter_adjusted,
70            period_counter_created
71     from   fa_adjustments
72     where  asset_id = l_asset_hdr_rec.asset_id
73     and    book_type_code = l_asset_hdr_rec.book_type_code
74     and    transaction_header_id = l_trans_rec.transaction_header_id
75     and    source_type_code = 'DEPRECIATION';
76 
77   CURSOR c_member_exists IS
78     select 'Y'
79     from   fa_books
80     where  book_type_code = p_asset_hdr_rec.book_type_code
81     and    group_asset_id = p_asset_fin_rec.group_asset_id
82     and    period_counter_fully_retired is null
83     and    transaction_header_id_out is null
84     /*bug#16426081*/
85     and    not(
86                tracking_method = 'CALCULATE'
87                and member_rollup_flag='Y'
88                and cost=0
89                and adjusted_cost <> 0
90               );
91 
92   CURSOR c_get_thid is
93     select fa_transaction_headers_s.nextval
94     from   dual;
95 
96   CURSOR c_get_primary_thid is
97     select transaction_header_id
98     from   fa_transaction_headers
99     where  asset_id = p_asset_fin_rec.group_asset_id
100     and    book_type_code = p_asset_hdr_rec.book_type_code
101     and    member_transaction_header_id = p_trans_rec.transaction_header_id;
102 
103   l_calling_fn            VARCHAR2(100) := 'fa_retirement_pvt.do_retirement';
104 
105   l_transaction_header_id FA_TRANSACTION_HEADERS.TRANSACTION_HEADER_ID%TYPE;
106 
107   l_asset_desc_rec_m      FA_API_TYPES.asset_desc_rec_type;
108   l_asset_desc_rec        FA_API_TYPES.asset_desc_rec_type;
109   l_asset_type_rec        FA_API_TYPES.asset_type_rec_type;
110   l_asset_cat_rec         FA_API_TYPES.asset_cat_rec_type;
111   l_asset_fin_rec_new_m   FA_API_TYPES.asset_fin_rec_type;
112   l_asset_fin_rec_old     FA_API_TYPES.asset_fin_rec_type;
113   l_asset_fin_rec_adj     FA_API_TYPES.asset_fin_rec_type;
114   l_asset_fin_rec_new     FA_API_TYPES.asset_fin_rec_type;
115   l_inv_trans_rec         FA_API_TYPES.inv_trans_rec_type;
116   l_asset_deprn_rec_old   FA_API_TYPES.asset_deprn_rec_type;
117   l_asset_deprn_rec_adj   FA_API_TYPES.asset_deprn_rec_type;
118   l_asset_deprn_rec_new   FA_API_TYPES.asset_deprn_rec_type;
119 
120   /*bug#15897249 starts*/
121   l_asset_fin_rec_old_g     FA_API_TYPES.asset_fin_rec_type;
122   l_asset_deprn_rec_old_g   FA_API_TYPES.asset_deprn_rec_type;
123   l_trans_rec_g             FA_API_TYPES.trans_rec_type;
124   /*bug#15897249 ends */
125   l_group_reclass_options_rec FA_API_TYPES.group_reclass_options_rec_type;
126 
127   l_deprn_exp             NUMBER;
128   l_bonus_deprn_exp       NUMBER;
129   l_impairment_exp        NUMBER;
130 
131   l_adj                   FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT;
132 
133   l_member_exists         VARCHAR2(1) := 'N';
134   l_deprn_reserve         NUMBER;
135   l_temp_num              NUMBER;
136   l_temp_char             VARCHAR2(30);
137   l_temp_bool             BOOLEAN;
138 
139   l_new_reserve           NUMBER;
140   l_new_rec_cost          NUMBER;
141   l_recapture_amount      NUMBER;
142 
143   l_alloc_amount          NUMBER; -- Used to pass amount to allocate across
144                                   -- member assets
145 
146   l_rowid                 ROWID; -- temp variable
147   l_cglfr_mode            VARCHAR2(2); --For CALC_GAIN_LOSS_FOR_RET
148   l_cost_sign             NUMBER; --Bug 8535921
149   ret_err                 EXCEPTION;
150 
151 BEGIN
152    if (p_log_level_rec.statement_level) then
153       fa_debug_pkg.add(l_calling_fn, 'Begin',
154                        p_asset_hdr_rec.asset_id||':'||p_asset_fin_rec.group_asset_id, p_log_level_rec => p_log_level_rec);
155    end if;
156 
157    --Adding to bypass the issue in 3440308 (case where an equal # of units is
158    --retired as is left in the asset.
159    --This is required because we're resetting asset_desc_rec below
160    g_current_units := nvl(p_asset_desc_rec.current_units,0);
161 
162    -- Get asset description for member asset
163    -- p_asset_desc may be null
164    if not FA_UTIL_PVT.get_asset_desc_rec (
165                 p_asset_hdr_rec         => p_asset_hdr_rec,
166                 px_asset_desc_rec       => l_asset_desc_rec_m, p_log_level_rec => p_log_level_rec) then
167       raise ret_err;
168    end if;
169 
170    if (p_mrc_sob_type_code <> 'R') then
171       OPEN c_get_thid;
172       FETCH c_get_thid INTO l_trans_rec.transaction_header_id;
173       CLOSE c_get_thid;
174    else
175       OPEN c_get_primary_thid;
176       FETCH c_get_primary_thid INTO l_trans_rec.transaction_header_id;
177       CLOSE c_get_primary_thid;
178    end if;
179 
180    --
181    -- Prepare to call FA_ADJUSTMENT_PVT.do_adjustment to process group
182    -- asset after member asset retirement.
183    --
184 
185    l_trans_rec.transaction_type_code := 'GROUP ADJUSTMENT';
186    l_trans_rec.transaction_subtype := 'AMORTIZED';
187    l_trans_rec.transaction_date_entered := p_trans_rec.transaction_date_entered;
188    l_trans_rec.amortization_start_date  := p_trans_rec.transaction_date_entered;
189    l_trans_rec.transaction_key := 'MR';
190    l_trans_rec.who_info.creation_date := p_trans_rec.who_info.creation_date;
191    l_trans_rec.who_info.created_by := p_trans_rec.who_info.created_by;
192    l_trans_rec.who_info.last_update_date := p_trans_rec.who_info.last_update_date;
193    l_trans_rec.who_info.last_updated_by := p_trans_rec.who_info.last_updated_by;
194    l_trans_rec.who_info.last_update_login := p_trans_rec.who_info.last_update_login;
195    l_trans_rec.member_transaction_header_id := p_trans_rec.transaction_header_id;
196    l_trans_rec.mass_transaction_id := p_trans_rec.mass_transaction_id;
197    l_trans_rec.calling_interface := p_trans_rec.calling_interface;
198    l_trans_rec.mass_reference_id := p_trans_rec.mass_reference_id;
199    l_trans_rec.event_id := p_trans_rec.event_id;
200 
201    l_asset_hdr_rec.asset_id := p_asset_fin_rec.group_asset_id;
202    l_asset_hdr_rec.book_type_code := p_asset_hdr_rec.book_type_code;
203    l_asset_hdr_rec.set_of_books_id := p_asset_hdr_rec.set_of_books_id;
204    --l_asset_hdr_rec.period_of_addition := 'N'; -- Guess not necessary
205 
206    if not FA_UTIL_PVT.get_asset_type_rec (
207                    p_asset_hdr_rec      => l_asset_hdr_rec,
208                    px_asset_type_rec    => l_asset_type_rec, p_log_level_rec => p_log_level_rec) then
209       raise ret_err;
210    end if;
211 
212    if not FA_UTIL_PVT.get_asset_desc_rec (
213                    p_asset_hdr_rec         => l_asset_hdr_rec,
214                    px_asset_desc_rec       => l_asset_desc_rec, p_log_level_rec => p_log_level_rec) then
215       raise ret_err;
216    end if;
217 
218    if not FA_UTIL_PVT.get_asset_cat_rec (
219                    p_asset_hdr_rec         => l_asset_hdr_rec,
220                    px_asset_cat_rec        => l_asset_cat_rec,
221                    p_date_effective        => null, p_log_level_rec => p_log_level_rec) then
222       raise ret_err;
223    end if;
224 
225    if not fa_util_pvt.get_asset_fin_rec (
226                    p_asset_hdr_rec         => l_asset_hdr_rec,
227                    px_asset_fin_rec        => l_asset_fin_rec_old,
228                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
229       raise ret_err;
230    end if;
231 
232    /* Added for bug 8535921 */
233    if l_asset_fin_rec_old.cost < 0 then
234       l_cost_sign := -1;
235    else
236       l_cost_sign := 1;
237    end if;
238    /* End of bug 8535921 */
239 
240 --tk_util.DumpFinRec(l_asset_fin_rec_old, 'OLD');
241 
242    if not fa_util_pvt.get_asset_deprn_rec (
243                    p_asset_hdr_rec         => l_asset_hdr_rec,
244                    px_asset_deprn_rec      => l_asset_deprn_rec_old,
245                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
246       raise ret_err;
247    end if;
248 
249    if (p_asset_type_rec.asset_type = 'CIP') then
250       l_asset_fin_rec_adj.cip_cost := -1 * p_asset_retire_rec.cost_retired;
251    else
252       l_asset_fin_rec_adj.cost := -1 * p_asset_retire_rec.cost_retired;
253    end if;
254 
255    l_asset_fin_rec_adj.unrevalued_cost := -1 * p_asset_retire_rec.cost_retired;
256    l_asset_fin_rec_adj.ytd_proceeds := p_asset_retire_rec.proceeds_of_sale;
257    l_asset_fin_rec_adj.ltd_proceeds := p_asset_retire_rec.proceeds_of_sale;
258    l_asset_fin_rec_adj.ltd_cost_of_removal := p_asset_retire_rec.cost_of_removal;
259 
260    l_asset_deprn_rec_adj.deprn_reserve := -1 * p_asset_retire_rec.reserve_retired;
261 
262    -- Get new member's fin_rec
263    if not fa_util_pvt.get_asset_fin_rec (
264                    p_asset_hdr_rec         => p_asset_hdr_rec,
265                    px_asset_fin_rec        => l_asset_fin_rec_new_m,
266                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
267       raise ret_err;
268    end if;
269 
270    l_asset_fin_rec_adj.salvage_value := nvl(l_asset_fin_rec_new_m.salvage_value, 0) -
271                                         nvl(p_asset_fin_rec.salvage_value, 0);
272 
273    l_asset_fin_rec_adj.allowed_deprn_limit_amount :=
274                                         nvl(l_asset_fin_rec_new_m.allowed_deprn_limit_amount, 0) -
275                                         nvl(p_asset_fin_rec.allowed_deprn_limit_amount, 0);
276    --Need this for CALC_GAIN_LOSS_FOR_RET.
277    if p_calling_fn <> 'DO_RETIREMENT.CGLFR_CR_ONLY' then
278       l_cglfr_mode := 'DR';
279    else
280       l_cglfr_mode := 'CR';
281    end if;
282    --
283    -- Insert COST, RESERVE, PROCEEDS CLR, and REMOVALCOST CLR
284    -- into FA_ADJUSTMENTS.
285    --
286 
287    /* Added for bug 7537762 */
288    if not fa_util_pvt.get_asset_deprn_rec (
289                    p_asset_hdr_rec         => l_asset_hdr_rec,
290                    px_asset_deprn_rec      => l_asset_deprn_rec_old,
291                    p_mrc_sob_type_code     => p_mrc_sob_type_code,
292                    p_log_level_rec         => p_log_level_rec) then
293       raise ret_err;
294    end if;
295   /* bug#15897249 starts */
296    if(nvl(l_asset_fin_rec_old.tracking_method,'NO TRACK') = 'CALCULATE' and
297       nvl(l_asset_fin_rec_old.member_rollup_flag,'N') = 'Y' and
298       l_asset_fin_rec_old.recognize_gain_loss = 'NO'
299       ) then
300       l_asset_fin_rec_old_g := l_asset_fin_rec_old;
301       l_asset_deprn_rec_old_g := l_asset_deprn_rec_old;
302       l_trans_rec_g := l_trans_rec;
303       l_asset_hdr_rec.asset_id := p_asset_hdr_rec.asset_id;
304       if not fa_util_pvt.get_asset_deprn_rec (
305                       p_asset_hdr_rec         => p_asset_hdr_rec,
306                       px_asset_deprn_rec      => l_asset_deprn_rec_old,
307                       p_mrc_sob_type_code     => p_mrc_sob_type_code,
308                       p_log_level_rec         => p_log_level_rec) then
309          raise ret_err;
310       end if;
311    end if;
312   /* bug#15897249 ends */
313 
314 --tk_util.debug('Before calling CALC_GAIN_LOSS_FOR_RET');
315    if not CALC_GAIN_LOSS_FOR_RET(
316                               p_trans_rec             => p_trans_rec,
317                               p_asset_hdr_rec         => p_asset_hdr_rec,
318                               p_asset_type_rec        => p_asset_type_rec,
319                               p_asset_desc_rec        => l_asset_desc_rec_m,
320                               p_asset_cat_rec         => p_asset_cat_rec,
321                               p_asset_fin_rec         => p_asset_fin_rec,
322                               p_period_rec            => p_period_rec,
323                               p_asset_retire_rec      => p_asset_retire_rec,
324                               p_group_thid            => l_trans_rec.transaction_header_id,
325                               p_salvage_value_retired => l_asset_fin_rec_adj.salvage_value,
326                               p_mrc_sob_type_code     => p_mrc_sob_type_code,
327                               p_mode                  => l_cglfr_mode,
328                               p_log_level_rec       => p_log_level_rec) then
329          if (p_log_level_rec.statement_level) then
330             fa_debug_pkg.add(l_calling_fn, 'ERROR',
331                              'returned from CALC_GAIN_LOSS_FOR_RET', p_log_level_rec => p_log_level_rec);
332          end if;
333 
334          raise ret_err;
335    end if;
336 --tk_util.debug('After calling CALC_GAIN_LOSS_FOR_RET');
337    /* bug#15897249 starts */
338    if(nvl(l_asset_fin_rec_old.tracking_method,'NO TRACK') = 'CALCULATE' and
339       nvl(l_asset_fin_rec_old.member_rollup_flag,'N') = 'Y' and
340       l_asset_fin_rec_old.recognize_gain_loss = 'NO'
341       ) then
342          l_asset_fin_rec_old := p_asset_fin_rec;
343          l_trans_rec := p_trans_rec;
344     end if;
345    /* bug#15897249 ends */
346 
347    -- Do the rest only if not called for credit row (fix for 3188851)
348    if p_calling_fn <> 'DO_RETIREMENT.CGLFR_CR_ONLY' then
349    --
350    -- Process Recapture Excess Reserve
351    --
352    if (nvl(p_asset_fin_rec.recapture_reserve_flag, 'N') = 'Y') then
353 
354       if (nvl(p_asset_fin_rec.limit_proceeds_flag, 'N')  = 'Y') and
355          (nvl(p_asset_retire_rec.proceeds_of_sale, 0) -
356           nvl(p_asset_retire_rec.cost_of_removal, 0)) >
357                               (p_asset_retire_rec.cost_retired -
358                                l_asset_fin_rec_adj.salvage_value) then
359 
360          l_new_reserve := l_asset_deprn_rec_old.deprn_reserve +
361                           (p_asset_retire_rec.cost_retired -
362                            l_asset_fin_rec_adj.salvage_value) -
363                           p_asset_retire_rec.cost_retired;
364       else
365 
366          l_new_reserve := l_asset_deprn_rec_old.deprn_reserve +
367                           nvl(p_asset_retire_rec.proceeds_of_sale, 0) -
368                           p_asset_retire_rec.cost_retired -
369                           nvl(p_asset_retire_rec.cost_of_removal, 0);
370 
371       end if;
372 
373       l_new_rec_cost := l_asset_fin_rec_old.recoverable_cost -
374                         (p_asset_retire_rec.cost_retired -
375                          l_asset_fin_rec_adj.salvage_value);
376 
377       if (l_cost_sign*l_new_rec_cost < l_cost_sign*l_new_reserve) then --Bug8535921
378          l_adj.adjustment_amount := l_new_reserve - l_new_rec_cost;
379 
380          if (p_log_level_rec.statement_level) then
381             fa_debug_pkg.add(l_calling_fn, 'Recapture Excess Reserve',
382                              l_adj.adjustment_amount, p_log_level_rec => p_log_level_rec);
383          end if;
384 
385          if p_asset_type_rec.asset_type = 'CIP' then
386             l_adj.source_type_code := 'CIP RETIREMENT';
387          else
388             l_adj.source_type_code := 'RETIREMENT';
389          end if;
390 
391          l_adj.asset_id                := l_asset_hdr_rec.asset_id;
392          l_adj.transaction_header_id   := l_trans_rec.transaction_header_id;
393          l_adj.book_type_code          := l_asset_hdr_rec.book_type_code;
394          l_adj.period_counter_created  := p_period_rec.period_counter;
395          l_adj.period_counter_adjusted := p_period_rec.period_counter;
396          l_adj.current_units           := l_asset_desc_rec.current_units;
397          l_adj.selection_retid         := 0;
398          l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
399          l_adj.leveling_flag           := TRUE;
400          l_adj.flush_adj_flag          := FALSE;
401          l_adj.last_update_date        := p_trans_rec.who_info.last_update_date;
402          l_adj.gen_ccid_flag           := TRUE;
403          l_adj.adjustment_type         := 'RESERVE';
404          l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
405          l_adj.debit_credit_flag       := 'DR';
406          l_adj.set_of_books_id         := l_asset_hdr_rec.set_of_books_id;
407          l_adj.mrc_sob_type_code       := p_mrc_sob_type_code;
408 
409          if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
410                                     l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
411             fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
412             raise ret_err;
413          end if;
414 
415          l_adj.account           := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
416 
417          if not FA_INS_ADJUST_PKG.faxinaj
418                             (l_adj,
419                              p_trans_rec.who_info.last_update_date,
420                              p_trans_rec.who_info.last_updated_by,
421                              p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
422             raise ret_err;
423          end if;
424 
425          l_adj.adjustment_type         := 'NBV RETIRED';
426          l_adj.adjustment_amount       := l_new_reserve - l_new_rec_cost;
427          l_adj.flush_adj_flag          := TRUE;
428 
429          l_adj.debit_credit_flag       := 'CR';
430          l_adj.account_type            := 'NBV_RETIRED_GAIN_ACCT';
431          l_adj.account                 := fa_cache_pkg.fazcbc_record.nbv_retired_gain_acct;
432 
433          if not FA_INS_ADJUST_PKG.faxinaj
434                             (l_adj,
435                              p_trans_rec.who_info.last_update_date,
436                              p_trans_rec.who_info.last_updated_by,
437                              p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
438             raise ret_err;
439          end if;
440 
441          l_recapture_amount := l_new_reserve - l_new_rec_cost;
442       else
443          l_recapture_amount := 0;
444 
445       end if; -- (l_new_rec_cost < l_new_reserve)
446 
447    else
448 
449       l_recapture_amount := 0;
450 
451    end if; -- (nvl(p_asset_fin_rec.recapture_reserve_flag, 'N') = 'Y') and
452   /* bug#15897249 starts */
453    if(nvl(l_asset_fin_rec_old.tracking_method,'NO TRACK') = 'CALCULATE' and
454       nvl(l_asset_fin_rec_old.member_rollup_flag,'N') = 'Y' and
455       l_asset_fin_rec_old.recognize_gain_loss = 'NO'
456       ) then
457       l_asset_hdr_rec.asset_id := p_asset_fin_rec.group_asset_id;
458       l_asset_fin_rec_old := l_asset_fin_rec_old_g;
459       l_asset_deprn_rec_old := l_asset_deprn_rec_old_g;
460       l_trans_rec := l_trans_rec_g;
461    end if;
462   /* bug#15897249 ends */
463 -- ENERGY
464    if (not fa_cache_pkg.fazccmt(l_asset_fin_rec_old.deprn_method_code,
465                                 l_asset_fin_rec_old.life_in_months, p_log_level_rec => p_log_level_rec)) then
466       if (p_log_level_rec.statement_level) then
467             fa_debug_pkg.add(l_calling_fn, 'Error calling', 'fa_cache_pkg.fazccmt', p_log_level_rec => p_log_level_rec);
468       end if;
469 
470       raise ret_err;
471    end if;
472 
473    if (nvl(l_asset_fin_rec_old.tracking_method, 'NO TRACK') = 'ALLOCATE') then
474 
475 --and            -- ENERGY
476 --      (fa_cache_pkg.fazcdrd_record.rule_name = 'ENERGY PERIOD END BALANCE') then         -- ENERGY
477       l_asset_deprn_rec_adj.deprn_reserve := - 1 * (p_asset_retire_rec.cost_retired -
478                                                    (nvl(p_asset_retire_rec.proceeds_of_sale, 0) -
479                                                     nvl(p_asset_retire_rec.cost_of_removal, 0)) + nvl(l_recapture_amount,0));
480    end if;
481 -- ENERGY
482 
483    if (p_log_level_rec.statement_level) then
484       fa_debug_pkg.add(l_calling_fn, 'Call FA_ADJUSTMENT_PVT.do_adjustment1',
485                        'Begin',  p_log_level_rec => p_log_level_rec);
486    end if;
487 
488    if not FA_ADJUSTMENT_PVT.do_adjustment
489                (px_trans_rec              => l_trans_rec,
490                 px_asset_hdr_rec          => l_asset_hdr_rec,
491                 p_asset_desc_rec          => l_asset_desc_rec,
492                 p_asset_type_rec          => l_asset_type_rec,
493                 p_asset_cat_rec           => l_asset_cat_rec,
494                 p_asset_fin_rec_old       => l_asset_fin_rec_old,
495                 p_asset_fin_rec_adj       => l_asset_fin_rec_adj,
496                 x_asset_fin_rec_new       => l_asset_fin_rec_new,
497                 p_inv_trans_rec           => l_inv_trans_rec,
498                 p_asset_deprn_rec_old     => l_asset_deprn_rec_old,
499                 p_asset_deprn_rec_adj     => l_asset_deprn_rec_adj,
500                 x_asset_deprn_rec_new     => l_asset_deprn_rec_new,
501                 p_period_rec              => p_period_rec,
502                 p_mrc_sob_type_code       => p_mrc_sob_type_code,
503                 p_group_reclass_options_rec => l_group_reclass_options_rec,
504                 p_calling_fn              => l_calling_fn, p_log_level_rec => p_log_level_rec) then
505 
506       if (p_log_level_rec.statement_level) then
507          fa_debug_pkg.add(l_calling_fn, 'Calling FA_ADJUSTMENT_PVT.do_adjustment', 'Failed',  p_log_level_rec => p_log_level_rec);
508       end if;
509 
510       raise ret_err;
511    end if;
512 
513    if (p_log_level_rec.statement_level) then
514       fa_debug_pkg.add(l_calling_fn, 'FA_ADJUSTMENT_PVT.do_adjustment', 'End',  p_log_level_rec => p_log_level_rec);
515    end if;
516 
517    if (p_trans_rec.calling_interface <> 'FAXASSET') and
518       (nvl(l_asset_fin_rec_new.cost, 0) <> 0) and
519       (nvl(l_asset_fin_rec_new.tracking_method, 'NO TRACK') = 'ALLOCATE') and
520       (fa_cache_pkg.fazcdrd_record.rule_name = 'ENERGY PERIOD END BALANCE') then
521 
522       l_alloc_amount := p_asset_retire_rec.cost_retired -
523                         p_asset_retire_rec.reserve_retired -
524                         (nvl(p_asset_retire_rec.proceeds_of_sale, 0) -
525                          nvl(p_asset_retire_rec.cost_of_removal, 0) -
526                          nvl(l_recapture_amount, 0)
527                          );
528 
529       l_asset_deprn_rec_new.deprn_reserve := l_asset_deprn_rec_new.deprn_reserve -
530                                              (p_asset_retire_rec.cost_retired -
531                                               (nvl(p_asset_retire_rec.proceeds_of_sale, 0) -
532                                                nvl(p_asset_retire_rec.cost_of_removal, 0)));
533 
534       if (p_log_level_rec.statement_level) then
535          fa_debug_pkg.add(l_calling_fn, 'Calling Do_Allocation', l_alloc_amount, p_log_level_rec => p_log_level_rec);
536          fa_debug_pkg.add(l_calling_fn, 'Calling Do_Allocation', p_asset_retire_rec.proceeds_of_sale, p_log_level_rec => p_log_level_rec);
537          fa_debug_pkg.add(l_calling_fn, 'Calling Do_Allocation', p_asset_retire_rec.cost_of_removal, p_log_level_rec => p_log_level_rec);
538          fa_debug_pkg.add(l_calling_fn, 'Calling Do_Allocation', p_asset_retire_rec.reserve_retired, p_log_level_rec => p_log_level_rec);
539          fa_debug_pkg.add(l_calling_fn, 'Calling Do_Allocation : l_recapture_amount', l_recapture_amount, p_log_level_rec => p_log_level_rec);
540       end if;
541 
542       if not FA_RETIREMENT_PVT.Do_Allocation(
543                       p_trans_rec         => l_trans_rec,
544                       p_asset_hdr_rec     => l_asset_hdr_rec,
545                       p_asset_fin_rec     => l_asset_fin_rec_new,
546                       p_asset_deprn_rec_new => l_asset_deprn_rec_new, -- group new deprn rec
547                       p_period_rec        => p_period_rec,
548                       p_reserve_amount    => l_alloc_amount,
549                       p_mrc_sob_type_code => p_mrc_sob_type_code,
550                       p_calling_fn        => l_calling_fn, p_log_level_rec => p_log_level_rec) then
551          if (p_log_level_rec.statement_level) then
552             fa_debug_pkg.add(l_calling_fn, 'Calling FA_ADJUSTMENT_PVT.do_adjustment', 'Failed',  p_log_level_rec => p_log_level_rec);
553          end if;
554 
555          raise ret_err;
556       end if;
557    end if; -- (nvl(l_asset_fin_rec_new.cost, 0) <> 0) and
558 
559    --
560    -- Set status of retirement to PROCESSED so Gain Loss program won't pick this
561    -- retirement up later.   This cannot be done at the time of insert since
562    -- Distribution API called after FA_RETIREMENT_PUB.do_sub_regular_retirement
563    -- is expecting the status to be PENDING.
564    --
565    if (p_log_level_rec.statement_level) then
566       fa_debug_pkg.add(l_calling_fn, 'Call FA_RETIREMENTS_PKG.Update_Row', 'Begin', p_log_level_rec => p_log_level_rec);
567    end if;
568 
569    FA_RETIREMENTS_PKG.Update_Row(
570                               X_Rowid             => l_rowid,
571                               X_Retirement_Id     => p_asset_retire_rec.retirement_id,
572                               X_Status            => 'PROCESSED',
573                               X_Recapture_Amount  => l_recapture_amount,
574                               X_mrc_sob_type_code => p_mrc_sob_type_code,
575                               X_set_of_books_id   => p_asset_hdr_rec.set_of_books_id,
576                               X_Calling_Fn        => l_calling_fn, p_log_level_rec => p_log_level_rec);
577 
578    if (p_log_level_rec.statement_level) then
579       fa_debug_pkg.add(l_calling_fn, 'Call FA_RETIREMENTS_PKG.Update_Row', 'End', p_log_level_rec => p_log_level_rec);
580    end if;
581 
582    -- SLA: update the event status in SLA as well to unprocessed
583    -- as original api inserted as incomplete
584 
585    if (p_mrc_sob_type_code <> 'R') then
586 
587       if p_log_level_rec.statement_level then
588          fa_debug_pkg.add
589             (fname   => l_calling_fn,
590              element => 'calling fa_xla_events_pvt.update_transaction_event with thid: ',
591              value   => p_trans_rec.transaction_header_id);
592       end if;
593 
594       if not fa_xla_events_pvt.update_transaction_event
595                (p_ledger_id              => p_asset_hdr_rec.set_of_books_id,
596                 p_transaction_header_id  => p_trans_rec.transaction_header_id,
597                 p_book_type_code         => p_asset_hdr_rec.book_type_code,
598                 p_event_type_code        => 'RETIREMENTS',
599                 p_event_date             => p_asset_retire_rec.date_retired, --?
600                 p_event_status_code      => FA_XLA_EVENTS_PVT.C_EVENT_UNPROCESSED,
601                 p_calling_fn             => l_calling_fn,
602                 p_log_level_rec          => p_log_level_rec) then
603          raise ret_err;
604       end if;
605    end if;
606 
607 
608    -- Processing Terminal Gain Loss
609    --
610    -- If this is full retirement, check to see there is at least
611    -- one asset remained in this group.  If not, declare remaining
612    -- group reserve as terminal gain loss if group's terminal gain
613    -- loss is set to 'YES'.
614    --
615    if (nvl(l_asset_fin_rec_old.terminal_gain_loss, 'NO')  <> 'NO') and
616       (nvl(l_asset_fin_rec_new.cost, 0) = 0) and
617       (p_trans_rec.transaction_type_code = 'FULL RETIREMENT') then
618 
619       OPEN c_member_exists;
620       FETCH c_member_exists INTO l_member_exists;
621       CLOSE c_member_exists;
622 
623       if (p_log_level_rec.statement_level) then
624          fa_debug_pkg.add(l_calling_fn, 'Is there a member asset',
625                           l_member_exists, p_log_level_rec => p_log_level_rec);
626       end if;
627 
628       if (nvl(l_member_exists, 'N') <> 'Y') then
629 
630          FA_BOOKS_PKG.Update_Row(
631                    X_Book_Type_Code               => l_asset_hdr_rec.book_type_code,
632                    X_Asset_Id                     => l_asset_hdr_rec.asset_id,
633                    X_terminal_gain_loss_flag      => 'Y',
634                    X_mrc_sob_type_code            => p_mrc_sob_type_code,
635                    X_set_of_books_id              => p_asset_hdr_rec.set_of_books_id,
636                    X_Calling_Fn                   => l_calling_fn, p_log_level_rec => p_log_level_rec);
637 
638          /* bug#15897249 starts */
639          if(nvl(l_asset_fin_rec_old.tracking_method,'NO TRACK') = 'CALCULATE' and
640             nvl(l_asset_fin_rec_old.member_rollup_flag,'N') = 'Y' and
641             l_asset_fin_rec_old.recognize_gain_loss = 'NO'
642             ) then
643             if(p_mrc_sob_type_code = 'R') then
644                UPDATE FA_MC_BOOKS
645                SET    TERMINAL_GAIN_LOSS_FLAG = 'Y'
646                WHERE  GROUP_ASSET_ID = l_asset_hdr_rec.asset_id
647                AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
648                AND    TRANSACTION_HEADER_ID_OUT is null
649                AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
650             else
651                UPDATE FA_BOOKS
652                SET    TERMINAL_GAIN_LOSS_FLAG = 'Y'
653                WHERE  GROUP_ASSET_ID = l_asset_hdr_rec.asset_id
654                AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
655                AND    TRANSACTION_HEADER_ID_OUT is null;
656             end if;
657 
658          end if;
659          /* bug#15897249 ends */
660 
661       end if; -- (nvl(l_member_exists, 'N') <> 'Y')
662 
663    end  if; -- (nvl(l_asset_fin_rec_old.terminal_gain_loss, 'NO')  = 'YES')
664 
665    end if; -- p_calling_fn <> 'DO_RETIREMENT.CGLFR_CR_ONLY'
666    if (p_log_level_rec.statement_level) then
667       fa_debug_pkg.add(l_calling_fn, 'End', 'Success', p_log_level_rec => p_log_level_rec);
668    end if;
669 
670    return true;
671 EXCEPTION
672   when ret_err then
673     if (p_log_level_rec.statement_level) then
674        fa_debug_pkg.add(l_calling_fn, 'End', 'Failed', p_log_level_rec => p_log_level_rec);
675     end if;
676 
677     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
678     return false;
679 
680   when OTHERS then
681     if (p_log_level_rec.statement_level) then
682        fa_debug_pkg.add(l_calling_fn, 'End', 'Failed'||':'||sqlerrm, p_log_level_rec => p_log_level_rec);
683     end if;
684 
685     fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
686     return false;
687 
688 END DO_RETIREMENT;
689 
690 FUNCTION UNDO_RETIREMENT_REINSTATEMENT(p_transaction_header_id IN NUMBER,
691                                        p_asset_hdr_rec         IN  FA_API_TYPES.asset_hdr_rec_type,
692                                        p_group_asset_id        IN  NUMBER,
693                                        p_set_of_books_id       IN NUMBER,
694                                        p_mrc_sob_type_code     IN VARCHAR2,
695                                        p_calling_fn            IN VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
696 return boolean IS
697 
698   CURSOR c_get_thid IS
699     select transaction_header_id
700     from   fa_transaction_headers
701     where  asset_id = p_group_asset_id
702     and    book_type_code = p_asset_hdr_rec.book_type_code
703     and    member_transaction_header_id = p_transaction_header_id;
704 
705   l_transaction_header_id NUMBER(15);
706 
707   CURSOR c_adj IS
708     select rowid
709     from   fa_adjustments
710     where  asset_id = p_group_asset_id
711     and    book_type_code = p_asset_hdr_rec.book_type_code
712     and    transaction_header_id = l_transaction_header_id;
713 
714   CURSOR c_mc_adj IS
715     select rowid
716     from   fa_mc_adjustments
717     where  transaction_header_id = l_transaction_header_id
718     and    set_of_books_id = p_set_of_books_id;
719 
720   l_calling_fn            VARCHAR2(100) := 'fa_group_retirement_pvt.undo_retirement_reinstatement';
721 
722 BEGIN
723 --tk_util.debug(l_calling_fn||'()+');
724 
725   OPEN c_get_thid;
726   FETCH c_get_thid INTO l_transaction_header_id;
727   CLOSE c_get_thid;
728 
729   if (p_mrc_sob_type_code = 'R') then
730 
731     FA_BOOKS_PKG.Delete_Row(
732                        X_Transaction_Header_Id_in => l_transaction_header_id,
733                        X_mrc_sob_type_code        => p_mrc_sob_type_code,
734                        X_set_of_books_id          => p_set_of_books_id,
735                        X_Calling_Fn               => l_calling_fn, p_log_level_rec => p_log_level_rec);
736 
737     FA_BOOKS_PKG.Reactivate_Row(
738                        X_Transaction_Header_Id_Out => l_transaction_header_id,
739                        X_mrc_sob_type_code         => p_mrc_sob_type_code,
740                        X_set_of_books_id          => p_set_of_books_id,
741                        X_Calling_Fn                => l_calling_fn, p_log_level_rec => p_log_level_rec);
742 
743     for l_adj in c_mc_adj loop
744 
745       FA_ADJUSTMENTS_PKG.DELETE_ROW(
746                        X_Rowid             => l_adj.rowid,
747                        X_mrc_sob_type_code => p_mrc_sob_type_code,
748                        X_set_of_books_id          => p_set_of_books_id,
749                        X_Calling_Fn        => l_calling_fn, p_log_level_rec => p_log_level_rec);
750     end loop;
751 
752   else
753 
754     FA_BOOKS_PKG.Delete_Row(
755                        X_Transaction_Header_Id_in => l_transaction_header_id,
756                        X_mrc_sob_type_code        => p_mrc_sob_type_code,
757                        X_set_of_books_id          => p_set_of_books_id,
758                        X_Calling_Fn               => l_calling_fn, p_log_level_rec => p_log_level_rec);
759 
760     FA_BOOKS_PKG.Reactivate_Row(
761                        X_Transaction_Header_Id_Out => l_transaction_header_id,
762                        X_mrc_sob_type_code         => p_mrc_sob_type_code,
763                        X_set_of_books_id          => p_set_of_books_id,
764                        X_Calling_Fn                => l_calling_fn, p_log_level_rec => p_log_level_rec);
765 
766     for l_adj in c_adj loop
767 
768       FA_ADJUSTMENTS_PKG.DELETE_ROW(
769                          X_Rowid             => l_adj.rowid,
770                          X_mrc_sob_type_code => p_mrc_sob_type_code,
771                          X_set_of_books_id          => p_set_of_books_id,
772                          X_Calling_Fn        => l_calling_fn, p_log_level_rec => p_log_level_rec);
773     end loop;
774 
775   end if; -- (p_mrc_sob_type_code = 'R')
776 
777    if (p_log_level_rec.statement_level) then
778       fa_debug_pkg.add(l_calling_fn, 'End', 'Success', p_log_level_rec => p_log_level_rec);
779    end if;
780 
781   return true;
782 
783 EXCEPTION
784   when OTHERS then
785     fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
786 
787     if c_get_thid%ISOPEN then
788       CLOSE c_get_thid;
789     end if;
790 
791     return false;
792 
793 END UNDO_RETIREMENT_REINSTATEMENT;
794 
795 FUNCTION DO_REINSTATEMENT(
796                       p_trans_rec         IN     FA_API_TYPES.trans_rec_type,
797                       p_asset_retire_rec  IN     FA_API_TYPES.asset_retire_rec_type,
798                       p_asset_hdr_rec     IN     FA_API_TYPES.asset_hdr_rec_type,
799                       p_asset_type_rec    IN     FA_API_TYPES.asset_type_rec_type,
800                       p_asset_cat_rec     IN     FA_API_TYPES.asset_cat_rec_type,
801                       p_asset_fin_rec     IN     FA_API_TYPES.asset_fin_rec_type,
802                       p_asset_desc_rec    IN     FA_API_TYPES.asset_desc_rec_type,
803                       p_period_rec        IN     FA_API_TYPES.period_rec_type,
804                       p_mrc_sob_type_code IN     VARCHAR2,
805                       p_calling_fn        IN     VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
806 return boolean IS
807 
808   l_calling_fn             VARCHAR2(100) := 'fa_group_retirement_pvt.do_reinstatement';
809 
810   l_out_trx_id        NUMBER(15); -- Transfer Out thid due to retirement
811 
812   CURSOR c_get_ret_dists IS
813     select FA_DISTRIBUTION_HISTORY_S.NEXTVAL
814          , -1 * transaction_units
815          , assigned_to
816          , code_combination_id
817          , location_id
818                         , units_assigned
819     from   fa_distribution_history
820     where  retirement_id = p_asset_retire_rec.retirement_id;
821 
822   CURSOR c_get_cur_dists (c_assigned_to   number,
823                           c_expense_ccid  number,
824                           c_location_ccid number) IS
825     select distribution_id
826          , units_assigned + nvl(transaction_units, 0)
827     from   fa_distribution_history
828     where  asset_id = p_asset_hdr_rec.asset_id
829     and    book_type_code = p_asset_hdr_rec.book_type_code
830     and    transaction_header_id_out is null
831     and    (assigned_to = c_assigned_to
832        or  (assigned_to is null and
833             c_assigned_to is null))
834     and    code_combination_id = c_expense_ccid
835     and    location_id = c_location_ccid;
836 
837     CURSOR c_get_transaction_type_code IS
838     select transaction_type_code
839       from fa_transaction_headers
840      where asset_id =  p_asset_hdr_rec.asset_id
841        and book_type_code = p_asset_hdr_rec.book_type_code
842        and transaction_header_id = p_asset_retire_rec.detail_info.transaction_header_id_in;
843 
844   l_trans_rec             FA_API_TYPES.trans_rec_type;
845   l_asset_hdr_rec         FA_API_TYPES.asset_hdr_rec_type;
846 
847 
848   CURSOR c_get_thid is
849     select fa_transaction_headers_s.nextval
850     from   dual;
851 
852   CURSOR c_get_primary_thid is
853     select transaction_header_id
854     from   fa_transaction_headers
855     where  asset_id = p_asset_fin_rec.group_asset_id
856     and    book_type_code = p_asset_hdr_rec.book_type_code
857     and    member_transaction_header_id = p_trans_rec.transaction_header_id;
858 
859   l_rowid             ROWID;
860   l_status            BOOLEAN := TRUE;
861 
862   TYPE tab_num_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
863   TYPE tab_num15_type IS TABLE OF NUMBER(15) INDEX BY BINARY_INTEGER;
864 
865   t_distribution_id tab_num15_type;
866   t_units_assigned  tab_num_type;
867   t_assigned_to     tab_num15_type;
868   t_expense_ccid    tab_num15_type;
869   t_location_ccid   tab_num15_type;
870   t_tot_units       tab_num_type;
871   l_row_fetched          NUMBER;
872 
873   l_distribution_id NUMBER(15);
874   l_units_assigned  NUMBER;
875 
876   l_transaction_type_code varchar2(20);   -- Bug#8580378
877   l_partial_retirement_flag BOOLEAN := FALSE;
878   l_tot_units NUMBER := 0;
879 
880   l_adj              FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT;
881 
882   l_deprn_exp             NUMBER;
883   l_bonus_deprn_exp       NUMBER;
884   l_impairment_exp        NUMBER;
885 
886   -- +++++ Stores new fin_rec of member +++++
887   l_asset_fin_rec_mn      FA_API_TYPES.asset_fin_rec_type;
888 
889   l_period_rec            FA_API_TYPES.period_rec_type;
890   l_asset_desc_rec_m      FA_API_TYPES.asset_desc_rec_type;
891   l_asset_desc_rec        FA_API_TYPES.asset_desc_rec_type;
892   l_asset_type_rec        FA_API_TYPES.asset_type_rec_type;
893   l_asset_cat_rec         FA_API_TYPES.asset_cat_rec_type;
894   l_asset_fin_rec_new_m   FA_API_TYPES.asset_fin_rec_type;
895   l_asset_fin_rec_old     FA_API_TYPES.asset_fin_rec_type;
896   l_asset_fin_rec_adj     FA_API_TYPES.asset_fin_rec_type;
897   l_asset_fin_rec_new     FA_API_TYPES.asset_fin_rec_type;
898   l_inv_trans_rec         FA_API_TYPES.inv_trans_rec_type;
899   l_asset_deprn_rec_old   FA_API_TYPES.asset_deprn_rec_type;
900   l_asset_deprn_rec_adj   FA_API_TYPES.asset_deprn_rec_type;
901   l_asset_deprn_rec_new   FA_API_TYPES.asset_deprn_rec_type;
902   l_group_reclass_options_rec FA_API_TYPES.group_reclass_options_rec_type;
903 
904   l_salvage_value_retired NUMBER;
905   l_cip_cost_retired      NUMBER;
906   l_adj_rec_cost_retired  NUMBER;
907   l_rec_cost_retired      NUMBER;
908   l_deprn_limit_retired   NUMBER;
909 
910   l_reserve_allocated     NUMBER;
911 
912   cursor c_dist is
913     select distribution_id
914          , transaction_header_id_in
915          , transaction_header_id_out
916          , units_assigned
917          , transaction_units
918     from   fa_distribution_history
919     where  asset_id = p_asset_hdr_rec.asset_id
920     order by distribution_id;
921 
922 
923   rein_err           EXCEPTION;
924 BEGIN
925    if (p_log_level_rec.statement_level) then
926       fa_debug_pkg.add(l_calling_fn, 'Begin', p_asset_hdr_rec.asset_id||':'||p_asset_fin_rec.group_asset_id, p_log_level_rec => p_log_level_rec);
927    end if;
928 
929    G_PART_RET_FLAG := FALSE;
930 
931    -- +++++ populate current period_rec info +++++
932    if not FA_UTIL_PVT.get_period_rec
933           (p_book           => p_asset_hdr_rec.book_type_code,
934            x_period_rec     => l_period_rec, p_log_level_rec => p_log_level_rec) then
935 
936       if (p_log_level_rec.statement_level) then
937          fa_debug_pkg.add(l_calling_fn, 'Calling FA_UTIL_PVT.get_period_rec', 'Failed',  p_log_level_rec => p_log_level_rec);
938       end if;
939 
940       raise rein_err;
941    end if;
942 
943    -- Get asset description for member asset
944    -- p_asset_desc may be null
945    if not FA_UTIL_PVT.get_asset_desc_rec (
946                 p_asset_hdr_rec         => p_asset_hdr_rec,
947                 px_asset_desc_rec       => l_asset_desc_rec_m, p_log_level_rec => p_log_level_rec) then
948 
949       if (p_log_level_rec.statement_level) then
950          fa_debug_pkg.add(l_calling_fn, 'Calling FA_UTIL_PVT.get_asset_desc_rec', 'Failed',  p_log_level_rec => p_log_level_rec);
951       end if;
952 
953       raise rein_err;
954    end if;
955 
956    --
957    -- Recreate Distributions, Asset History, and Addition only if
958    -- retirement about to be reisntate is partial unit/full retirement and
959    -- book class is CORPORATE.
960    if nvl(p_asset_retire_rec.units_retired, 0) > 0 and
961       fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE' and
962       l_asset_desc_rec_m.current_units <> 0 then
963 
964       -- +++++ Get retired distribution +++++
965       OPEN c_get_ret_dists;
966       FETCH c_get_ret_dists BULK COLLECT INTO t_distribution_id,
967                                               t_units_assigned,
968                                               t_assigned_to,
969                                               t_expense_ccid,
970                                               t_location_ccid,
971                                               t_tot_units;
972       CLOSE c_get_ret_dists;
973 
974       l_row_fetched := t_distribution_id.COUNT;
975 
976       if (t_distribution_id.COUNT = 0) then
977 
978          if (p_log_level_rec.statement_level) then
979             fa_debug_pkg.add(l_calling_fn, 'Getting distribution info', 'Failed', p_log_level_rec => p_log_level_rec);
980          end if;
981 
982          raise rein_err;
983       end if;
984 
985       --
986       -- For each distribution retired, if there is a existing active distribution
987       -- which shares the same exp_ccid, location, and assigned_to, deactivate it
988       -- and then added existing units to the retired units.
989       for i in 1..l_row_fetched loop
990 
991          l_distribution_id          := to_number(null);
992          l_units_assigned           := to_number(null);
993 
994          OPEN c_get_cur_dists(t_assigned_to(i), t_expense_ccid(i), t_location_ccid(i));
995          FETCH c_get_cur_dists into l_distribution_id, l_units_assigned;
996          CLOSE c_get_cur_dists;
997 
998          if (l_distribution_id is not null) then
999 
1000             update FA_DISTRIBUTION_HISTORY
1001             set    transaction_units = nvl(transaction_units, t_units_assigned(i)),
1002                    transaction_header_id_out = p_trans_rec.transaction_header_id,
1003                    date_ineffective = p_trans_rec.who_info.last_update_date,
1004                    last_update_date = p_trans_rec.who_info.last_update_date,
1005                    last_updated_by = p_trans_rec.who_info.last_updated_by,
1006                    last_update_login = p_trans_rec.who_info.last_update_login
1007             where  distribution_id = l_distribution_id;
1008 
1009             t_units_assigned(i) := t_units_assigned(i) + l_units_assigned;
1010 
1011          end if;
1012          l_tot_units := l_tot_units + t_tot_units(i);
1013       end loop;
1014 
1015       -- +++++ Create retired distributions +++++
1016       FORALL i in 1..t_distribution_id.LAST
1017       insert into FA_DISTRIBUTION_HISTORY(
1018                             DISTRIBUTION_ID,
1019                             BOOK_TYPE_CODE,
1020                             ASSET_ID,
1021                             UNITS_ASSIGNED,
1022                             DATE_EFFECTIVE,
1023                             CODE_COMBINATION_ID,
1024                             LOCATION_ID,
1025                             TRANSACTION_HEADER_ID_IN,
1026                             LAST_UPDATE_DATE,
1027                             LAST_UPDATED_BY,
1028                             ASSIGNED_TO)
1029                      VALUES(t_distribution_id(i),
1030                             p_asset_hdr_rec.book_type_code,
1031                             p_asset_hdr_rec.asset_id,
1032                             t_units_assigned(i),
1033                             p_trans_rec.who_info.creation_date,
1034                             t_expense_ccid(i),
1035                             t_location_ccid(i),
1036                             p_trans_rec.transaction_header_id,
1037                             p_trans_rec.who_info.last_update_date,
1038                             p_trans_rec.who_info.last_updated_by,
1039                             t_assigned_to(i));
1040 
1041       --
1042       -- Full retirement didn't update Asset History and Additions, so maintain
1043       -- these only if it was partial unit retirement
1044       --
1045 
1046       if (l_tot_units <> p_asset_retire_rec.units_retired) then
1047           l_partial_retirement_flag := TRUE;
1048       else
1049           OPEN c_get_transaction_type_code;
1050           FETCH c_get_transaction_type_code into l_transaction_type_code;
1051           CLOSE c_get_transaction_type_code;
1052 
1053           if ( l_transaction_type_code <> 'FULL RETIREMENT' )  then
1054              l_partial_retirement_flag := TRUE;
1055           end if;
1056       end if;
1057 
1058       if ( l_partial_retirement_flag )  then
1059                    G_PART_RET_FLAG := TRUE;
1060          -- terminate old asset_history
1061          FA_ASSET_HISTORY_PKG.Update_Row
1062                    (X_Asset_Id                  => p_asset_hdr_rec.asset_id,
1063                     X_Date_Ineffective          => p_trans_rec.who_info.last_update_date,
1064                     X_Transaction_Header_Id_Out => p_trans_rec.transaction_header_id,
1065                     X_Last_Update_Date          => p_trans_rec.who_info.last_update_date,
1066                     X_Last_Updated_By           => p_trans_rec.who_info.last_updated_by,
1067                     X_Last_Update_Login         => p_trans_rec.who_info.last_update_login,
1068                     X_Return_Status             => l_status,
1069                     X_Calling_Fn                => l_calling_fn, p_log_level_rec => p_log_level_rec);
1070          if not(l_status) then
1071             raise rein_err;
1072          end if;
1073 
1074          -- insert new row with new units
1075          FA_ASSET_HISTORY_PKG.Insert_Row
1076                    (X_Rowid                     => l_rowid,
1077                     X_Asset_Id                  => p_asset_hdr_rec.asset_id,
1078                     X_Category_Id               => p_asset_cat_rec.category_id,
1079                     X_Asset_Type                => p_asset_type_rec.asset_type,
1080                     X_Units                     => l_asset_desc_rec_m.current_units + p_asset_retire_rec.units_retired,
1081                     X_Date_Effective            => p_trans_rec.who_info.last_update_date,
1082                     X_Transaction_Header_Id_In  => p_trans_rec.transaction_header_id,
1083                     X_Last_Update_Date          => p_trans_rec.who_info.last_update_date,
1084                     X_Last_Updated_By           => p_trans_rec.who_info.last_updated_by,
1085                     X_Return_Status             => l_status,
1086                     X_Calling_Fn                => 'FA_DISTRIBUTION_PVT.update_asset_history',  p_log_level_rec => p_log_level_rec);
1087          if not(l_status) then
1088             raise rein_err;
1089          end if;
1090 
1091          update fa_additions_b
1092          set    current_units = current_units + p_asset_retire_rec.units_retired
1093          where  asset_id = p_asset_hdr_rec.asset_id;
1094 
1095       end if; -- (l_asset_desc_rec_m.current_units > p_asset_retire_rec.units_retired)
1096 
1097    end if; --(nvl(p_asset_retire_rec.units_retired, 0) > 0)
1098 
1099    if (p_mrc_sob_type_code <> 'R') then
1100       OPEN c_get_thid;
1101       FETCH c_get_thid INTO l_trans_rec.transaction_header_id;
1102       CLOSE c_get_thid;
1103    else
1104       OPEN c_get_primary_thid;
1105       FETCH c_get_primary_thid INTO l_trans_rec.transaction_header_id;
1106       CLOSE c_get_primary_thid;
1107    end if;
1108 
1109    -- +++++ Process Group Asset +++++
1110 
1111    l_trans_rec.transaction_type_code := 'GROUP ADJUSTMENT';
1112    l_trans_rec.transaction_subtype := 'AMORTIZED';
1113 
1114    l_trans_rec.transaction_key := 'MS';
1115    l_trans_rec.who_info.creation_date := p_trans_rec.who_info.creation_date;
1116    l_trans_rec.who_info.created_by := p_trans_rec.who_info.created_by;
1117    l_trans_rec.who_info.last_update_date := p_trans_rec.who_info.last_update_date;
1118    l_trans_rec.who_info.last_updated_by := p_trans_rec.who_info.last_updated_by;
1119    l_trans_rec.who_info.last_update_login := p_trans_rec.who_info.last_update_login;
1120    l_trans_rec.member_transaction_header_id := p_trans_rec.transaction_header_id;
1121    l_trans_rec.mass_transaction_id := p_trans_rec.mass_transaction_id;
1122    l_trans_rec.calling_interface := p_trans_rec.calling_interface;
1123    l_trans_rec.mass_reference_id := p_trans_rec.mass_reference_id;
1124    l_trans_rec.event_id := p_trans_rec.event_id;
1125 
1126    -- Need deprn rec before reinstatement
1127    l_asset_hdr_rec.asset_id := p_asset_fin_rec.group_asset_id;
1128    l_asset_hdr_rec.book_type_code := p_asset_hdr_rec.book_type_code;
1129    l_asset_hdr_rec.set_of_books_id := p_asset_hdr_rec.set_of_books_id;
1130    --l_asset_hdr_rec.period_of_addition := 'N'; -- Guess not necessary
1131 
1132    if not CALC_GAIN_LOSS_FOR_REI(
1133                  p_trans_rec         => p_trans_rec,
1134                  p_asset_hdr_rec     => p_asset_hdr_rec,
1135                  p_asset_type_rec    => p_asset_type_rec,
1136                  p_asset_desc_rec    => l_asset_desc_rec_m,
1137                  p_asset_cat_rec     => p_asset_cat_rec,
1138                  p_asset_fin_rec     => p_asset_fin_rec,
1139                  p_period_rec        => l_period_rec,
1140                  p_asset_retire_rec  => p_asset_retire_rec,
1141                  p_group_thid        => l_trans_rec.transaction_header_id,
1142                  x_asset_fin_rec     => l_asset_fin_rec_mn,
1143                  p_mrc_sob_type_code => p_mrc_sob_type_code,
1144                  p_log_level_rec         => p_log_level_rec) then
1145       raise rein_err;
1146    end if;
1147 
1148    if not fa_util_pvt.get_asset_deprn_rec (
1149                    p_asset_hdr_rec         => l_asset_hdr_rec,
1150                    px_asset_deprn_rec      => l_asset_deprn_rec_old,
1151                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
1152       raise rein_err;
1153    end if;
1154 
1155    if not FA_UTIL_PVT.get_asset_type_rec
1156                   (p_asset_hdr_rec      => l_asset_hdr_rec,
1157                    px_asset_type_rec    => l_asset_type_rec, p_log_level_rec => p_log_level_rec) then
1158       raise rein_err;
1159    end if;
1160 
1161    if not FA_UTIL_PVT.get_asset_desc_rec
1162              (p_asset_hdr_rec         => l_asset_hdr_rec,
1163               px_asset_desc_rec       => l_asset_desc_rec, p_log_level_rec => p_log_level_rec) then
1164       raise rein_err;
1165    end if;
1166 
1167    if not FA_UTIL_PVT.get_asset_cat_rec
1168              (p_asset_hdr_rec         => l_asset_hdr_rec,
1169               px_asset_cat_rec        => l_asset_cat_rec,
1170               p_date_effective        => null, p_log_level_rec => p_log_level_rec) then
1171       raise rein_err;
1172    end if;
1173 
1174    if not fa_util_pvt.get_asset_fin_rec (
1175                    p_asset_hdr_rec         => l_asset_hdr_rec,
1176                    px_asset_fin_rec        => l_asset_fin_rec_old,
1177                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
1178       raise rein_err;
1179    end if;
1180 
1181    --HH Validate disabled_flag
1182    --We don't want to reinstate assets belonging to a disabled group.
1183    if not FA_ASSET_VAL_PVT.validate_disabled_flag
1184                   (p_group_asset_id => l_asset_hdr_rec.asset_id,
1185                    p_book_type_code => l_asset_hdr_rec.book_type_code,
1186                    p_old_flag       => l_asset_fin_rec_old.disabled_flag,
1187                    p_new_flag       => l_asset_fin_rec_old.disabled_flag
1188                   , p_log_level_rec => p_log_level_rec) then
1189                raise rein_err;
1190    end if; --End HH
1191 
1192 /*
1193    if not fa_util_pvt.get_asset_deprn_rec (
1194                    p_asset_hdr_rec         => l_asset_hdr_rec,
1195                    px_asset_deprn_rec      => l_asset_deprn_rec_old,
1196                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
1197       raise rein_err;
1198    end if;
1199 */
1200 
1201    l_asset_fin_rec_adj.cost := p_asset_retire_rec.cost_retired;
1202    l_asset_fin_rec_adj.unrevalued_cost := p_asset_retire_rec.cost_retired;
1203 
1204    -- Get new member's fin_rec
1205    if not fa_util_pvt.get_asset_fin_rec (
1206                    p_asset_hdr_rec         => p_asset_hdr_rec,
1207                    px_asset_fin_rec        => l_asset_fin_rec_new_m,
1208                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
1209       raise rein_err;
1210    end if;
1211 
1212    l_asset_fin_rec_adj.salvage_value := nvl(l_asset_fin_rec_new_m.salvage_value, 0) -
1213                                         nvl(p_asset_fin_rec.salvage_value, 0);
1214 
1215    l_asset_fin_rec_adj.allowed_deprn_limit_amount :=
1216                                         nvl(l_asset_fin_rec_new_m.allowed_deprn_limit_amount, 0) -
1217                                         nvl(p_asset_fin_rec.allowed_deprn_limit_amount, 0);
1218 
1219 --   l_asset_deprn_rec_adj.deprn_reserve := p_asset_retire_rec.reserve_retired;
1220 
1221    --
1222    -- Process member asset reinstatement
1223    --
1224    -- Find Salvage value retired, recoverable cost, deprn limit, adjusted recoverable cost
1225    -- cip_cost_retired, new adjusted cost by calling deprn basis.
1226    -- Also find out whether there is other amount to reinstate.  I guess not.
1227    FA_BOOKS_PKG.Deactivate_Row(
1228           X_Asset_Id                     => p_asset_hdr_rec.asset_id,
1229           X_Book_Type_Code               => p_asset_hdr_rec.book_type_code,
1230           X_Transaction_Header_Id_Out    => p_trans_rec.transaction_header_id,
1231           X_Date_Ineffective             => p_trans_rec.who_info.last_update_date,
1232           X_mrc_sob_type_code            => p_mrc_sob_type_code,
1233           X_set_of_books_id              => p_asset_hdr_rec.set_of_books_id,
1234           X_Calling_Fn                   => l_calling_fn, p_log_level_rec => p_log_level_rec);
1235 
1236 --tk_util.DumpFinRec(l_asset_fin_rec_mn, 'member');
1237 
1238    FA_BOOKS_PKG.INSERT_ROW(
1239           X_Rowid                        => l_rowid,
1240           X_Book_Type_Code               => p_asset_hdr_rec.book_type_code,
1241           X_Asset_Id                     => p_asset_hdr_rec.asset_id,
1242           X_Date_Placed_In_Service       => p_asset_fin_rec.date_placed_in_service,
1243           X_Date_Effective               => p_trans_rec.who_info.creation_date,
1244           X_Deprn_Start_Date             => p_asset_fin_rec.deprn_start_date,
1245           X_Deprn_Method_Code            => p_asset_fin_rec.deprn_method_code,
1246           X_Life_In_Months               => p_asset_fin_rec.life_in_months,
1247           X_Rate_Adjustment_Factor       => l_asset_fin_rec_mn.rate_adjustment_factor,
1248           X_Adjusted_Cost                => l_asset_fin_rec_mn.adjusted_cost,
1249           X_Cost                         => l_asset_fin_rec_mn.cost,
1250           X_Original_Cost                => p_asset_fin_rec.original_cost,
1251           X_Salvage_Value                => l_asset_fin_rec_mn.salvage_value,
1252           X_Prorate_Convention_Code      => p_asset_fin_rec.prorate_convention_code,
1253           X_Prorate_Date                 => p_asset_fin_rec.prorate_date,
1254           X_Cost_Change_Flag             => p_asset_fin_rec.cost_change_flag,
1255           X_Adjustment_Required_Status   => p_asset_fin_rec.adjustment_required_status,
1256           X_Capitalize_Flag              => p_asset_fin_rec.capitalize_flag,
1257           X_Retirement_Pending_Flag      => 'NO',
1258           X_Depreciate_Flag              => p_asset_fin_rec.depreciate_flag,
1259           X_Disabled_Flag                => p_asset_fin_rec.disabled_flag, --HH
1260           X_Last_Update_Date             => p_trans_rec.who_info.last_update_date,
1261           X_Last_Updated_By              => p_trans_rec.who_info.last_updated_by,
1262           X_Transaction_Header_Id_In     => p_trans_rec.transaction_header_id,
1263           X_Itc_Amount_Id                => p_asset_fin_rec.itc_amount_id,
1264           X_Itc_Amount                   => p_asset_fin_rec.itc_amount,
1265   --        X_Retirement_Id                => ,
1266   --        X_Tax_Request_Id               => ,
1267           X_Itc_Basis                    => p_asset_fin_rec.itc_basis,
1268           X_Basic_Rate                   => p_asset_fin_rec.basic_rate,
1269           X_Adjusted_Rate                => p_asset_fin_rec.adjusted_rate,
1270           X_Bonus_Rule                   => p_asset_fin_rec.bonus_rule,
1271           X_Ceiling_Name                 => p_asset_fin_rec.ceiling_name,
1272           X_Recoverable_Cost             => l_asset_fin_rec_mn.recoverable_cost,
1273           X_Adjusted_Capacity            => l_asset_fin_rec_mn.adjusted_capacity,
1274           X_Fully_Rsvd_Revals_Counter    => p_asset_fin_rec.fully_rsvd_revals_counter,
1275           X_Idled_Flag                   => p_asset_fin_rec.idled_flag,
1276           X_Period_Counter_Capitalized   => p_asset_fin_rec.period_counter_capitalized,
1277           X_PC_Fully_Reserved            => null,
1278           X_Production_Capacity          => p_asset_fin_rec.production_capacity,
1279           X_Reval_Amortization_Basis     => l_asset_fin_rec_mn.reval_amortization_basis,
1280           X_Reval_Ceiling                => l_asset_fin_rec_mn.reval_ceiling,
1281           X_Unit_Of_Measure              => p_asset_fin_rec.unit_of_measure,
1282           X_Unrevalued_Cost              => l_asset_fin_rec_mn.unrevalued_cost,
1283           X_Annual_Deprn_Rounding_Flag   => l_asset_fin_rec_mn.annual_deprn_rounding_flag,
1284           X_Percent_Salvage_Value        => p_asset_fin_rec.percent_salvage_value,
1285           X_Allowed_Deprn_Limit          => p_asset_fin_rec.allowed_deprn_limit,
1286           X_Allowed_Deprn_Limit_Amount   => l_asset_fin_rec_mn.allowed_deprn_limit_amount,
1287           X_Period_Counter_Life_Complete => p_asset_fin_rec.period_counter_life_complete,
1288           X_Adjusted_Recoverable_Cost    => l_asset_fin_rec_mn.adjusted_recoverable_cost,
1289           X_Short_Fiscal_Year_Flag       => p_asset_fin_rec.short_fiscal_year_flag,
1290           X_Conversion_Date              => p_asset_fin_rec.conversion_date,
1291           X_Orig_Deprn_Start_Date        => p_asset_fin_rec.orig_deprn_start_date,
1292           X_Remaining_Life1              => p_asset_fin_rec.remaining_life1,
1293           X_Remaining_Life2              => p_asset_fin_rec.remaining_life2,
1294           X_Old_Adj_Cost                 => p_asset_fin_rec.adjusted_cost,   ---???
1295           X_Formula_Factor               => l_asset_fin_rec_mn.formula_factor,
1296           X_gf_Attribute1                => p_asset_fin_rec.global_attribute1,
1297           X_gf_Attribute2                => p_asset_fin_rec.global_attribute2,
1298           X_gf_Attribute3                => p_asset_fin_rec.global_attribute3,
1299           X_gf_Attribute4                => p_asset_fin_rec.global_attribute4,
1300           X_gf_Attribute5                => p_asset_fin_rec.global_attribute5,
1301           X_gf_Attribute6                => p_asset_fin_rec.global_attribute6,
1302           X_gf_Attribute7                => p_asset_fin_rec.global_attribute7,
1303           X_gf_Attribute8                => p_asset_fin_rec.global_attribute8,
1304           X_gf_Attribute9                => p_asset_fin_rec.global_attribute9,
1305           X_gf_Attribute10               => p_asset_fin_rec.global_attribute10,
1306           X_gf_Attribute11               => p_asset_fin_rec.global_attribute11,
1307           X_gf_Attribute12               => p_asset_fin_rec.global_attribute12,
1308           X_gf_Attribute13               => p_asset_fin_rec.global_attribute13,
1309           X_gf_Attribute14               => p_asset_fin_rec.global_attribute14,
1310           X_gf_Attribute15               => p_asset_fin_rec.global_attribute15,
1311           X_gf_Attribute16               => p_asset_fin_rec.global_attribute16,
1312           X_gf_Attribute17               => p_asset_fin_rec.global_attribute17,
1313           X_gf_Attribute18               => p_asset_fin_rec.global_attribute18,
1314           X_gf_Attribute19               => p_asset_fin_rec.global_attribute19,
1315           X_gf_Attribute20               => p_asset_fin_rec.global_attribute20,
1316           X_global_attribute_category    => p_asset_fin_rec.global_attribute_category,
1317           X_group_asset_id               => p_asset_fin_rec.group_asset_id,
1318           X_salvage_type                 => p_asset_fin_rec.salvage_type,
1319           X_deprn_limit_type             => p_asset_fin_rec.deprn_limit_type,
1320           X_over_depreciate_option       => p_asset_fin_rec.over_depreciate_option,
1321           X_super_group_id               => p_asset_fin_rec.super_group_id,
1322           X_reduction_rate               => l_asset_fin_rec_mn.reduction_rate,
1323           X_reduce_addition_flag         => p_asset_fin_rec.reduce_addition_flag,
1324           X_reduce_adjustment_flag       => p_asset_fin_rec.reduce_adjustment_flag,
1325           X_reduce_retirement_flag       => p_asset_fin_rec.reduce_retirement_flag,
1326           X_recognize_gain_loss          => p_asset_fin_rec.recognize_gain_loss,
1327           X_recapture_reserve_flag       => p_asset_fin_rec.recapture_reserve_flag,
1328           X_limit_proceeds_flag          => p_asset_fin_rec.limit_proceeds_flag,
1329           X_terminal_gain_loss           => p_asset_fin_rec.terminal_gain_loss,
1330           X_tracking_method              => p_asset_fin_rec.tracking_method,
1331           X_allocate_to_fully_rsv_flag   => p_asset_fin_rec.allocate_to_fully_rsv_flag,
1332           X_allocate_to_fully_ret_flag   => p_asset_fin_rec.allocate_to_fully_ret_flag,
1333           X_exclude_fully_rsv_flag       => p_asset_fin_rec.exclude_fully_rsv_flag,
1334           X_excess_allocation_option     => p_asset_fin_rec.excess_allocation_option,
1335           X_depreciation_option          => p_asset_fin_rec.depreciation_option,
1336           X_member_rollup_flag           => p_asset_fin_rec.member_rollup_flag,
1337           X_ytd_proceeds                 => l_asset_fin_rec_mn.ytd_proceeds,
1338           X_ltd_proceeds                 => l_asset_fin_rec_mn.ltd_proceeds,
1339           X_eofy_reserve                 => l_asset_fin_rec_mn.eofy_reserve,
1340           X_cip_cost                     => l_asset_fin_rec_mn.cip_cost,
1341           X_terminal_gain_loss_amount    => l_asset_fin_rec_mn.terminal_gain_loss_amount,
1342           X_ltd_cost_of_removal          => l_asset_fin_rec_mn.ltd_cost_of_removal,
1343           X_cash_generating_unit_id      =>
1344                                       p_asset_fin_rec.cash_generating_unit_id,
1345           X_Return_Status                => l_status,
1346           X_mrc_sob_type_code            => p_mrc_sob_type_code,
1347           X_set_of_books_id              => p_asset_hdr_rec.set_of_books_id,
1348           X_Calling_Fn                   => l_calling_fn,
1349           p_log_level_rec                => p_log_level_rec);
1350 
1351    if (not(l_status)) then
1352       raise rein_err;
1353    end if;
1354 
1355    if (p_log_level_rec.statement_level) then
1356         fa_debug_pkg.add(l_calling_fn, 'transaction_date_entered',
1357                           p_trans_rec.transaction_date_entered, p_log_level_rec => p_log_level_rec);
1358         fa_debug_pkg.add(l_calling_fn, 'amortization_start_date',
1359                           p_trans_rec.amortization_start_date, p_log_level_rec => p_log_level_rec);
1360         fa_debug_pkg.add(l_calling_fn, 'new.cost', l_asset_fin_rec_new.cost, p_log_level_rec => p_log_level_rec);
1361         fa_debug_pkg.add(l_calling_fn, 'tracking_method',l_asset_fin_rec_old.tracking_method, p_log_level_rec => p_log_level_rec);
1362         fa_debug_pkg.add(l_calling_fn, 'rule_name',fa_cache_pkg.fazcdrd_record.rule_name, p_log_level_rec => p_log_level_rec);
1363    end if;
1364 
1365 
1366    if (nvl(l_asset_fin_rec_old.tracking_method, 'NO TRACK') = 'ALLOCATE') and
1367       (fa_cache_pkg.fazcdrd_record.rule_name = 'ENERGY PERIOD END BALANCE') then
1368       l_trans_rec.transaction_date_entered := p_trans_rec.transaction_date_entered;
1369       l_trans_rec.amortization_start_date := p_trans_rec.transaction_date_entered;
1370    else
1371       l_trans_rec.transaction_date_entered := p_asset_retire_rec.date_retired;
1372       l_trans_rec.amortization_start_date := p_asset_retire_rec.date_retired;
1373    end if;
1374 
1375    if (p_log_level_rec.statement_level) then
1376       fa_debug_pkg.add(l_calling_fn, 'Call FA_ADJUSTMENT_PVT.do_adjustment2',
1377                        'Begin',  p_log_level_rec => p_log_level_rec);
1378 
1379    end if;
1380 
1381 
1382       if not FA_ADJUSTMENT_PVT.do_adjustment
1383                (px_trans_rec              => l_trans_rec,
1384                 px_asset_hdr_rec          => l_asset_hdr_rec,
1385                 p_asset_desc_rec          => l_asset_desc_rec,
1386                 p_asset_type_rec          => l_asset_type_rec,
1387                 p_asset_cat_rec           => l_asset_cat_rec,
1388                 p_asset_fin_rec_old       => l_asset_fin_rec_old,
1389                 p_asset_fin_rec_adj       => l_asset_fin_rec_adj,
1390                 x_asset_fin_rec_new       => l_asset_fin_rec_new,
1391                 p_inv_trans_rec           => l_inv_trans_rec,
1392                 p_asset_deprn_rec_old     => l_asset_deprn_rec_old,
1393                 p_asset_deprn_rec_adj     => l_asset_deprn_rec_adj,
1394                 x_asset_deprn_rec_new     => l_asset_deprn_rec_new,
1395                 p_period_rec              => l_period_rec,
1396                 p_mrc_sob_type_code       => p_mrc_sob_type_code,
1397                 p_group_reclass_options_rec => l_group_reclass_options_rec,
1398                 p_calling_fn              => l_calling_fn, p_log_level_rec => p_log_level_rec)then
1399          raise rein_err;
1400       end if;
1401 
1402    if (nvl(l_asset_fin_rec_old.member_rollup_flag, 'N') = 'Y') then
1403       --
1404       -- This is Sum up member depreciation to group asset
1405       --
1406       if (p_log_level_rec.statement_level) then
1407          fa_debug_pkg.add(l_calling_fn, 'Process Member Rollup Group',
1408                           l_asset_hdr_rec.asset_id, p_log_level_rec => p_log_level_rec);
1409       end if;
1410       -- +++++ Remove group expense rows +++++
1411       DELETE FROM FA_ADJUSTMENTS
1412       WHERE  ASSET_ID = l_asset_hdr_rec.asset_id
1413       AND    BOOK_TYPE_CODE = l_asset_hdr_rec.book_type_code
1414       AND    TRANSACTION_HEADER_ID = l_trans_rec.member_transaction_header_id
1415       AND    SOURCE_TYPE_CODE = 'DEPRECIATION'
1416       AND    ADJUSTMENT_TYPE in ('EXPENSE', 'BONUS EXPENSE',
1417                                  'IMPAIR EXPENSE');
1418 
1419    end if; -- (nvl(l_asset_fin_rec_old.member_rollup_flag, 'N') = 'Y')
1420 
1421    -- +++++ Clear Terminal Gain Loss Amount +++++
1422    FA_BOOKS_PKG.Update_Row(
1423                    X_Book_Type_Code            => l_asset_hdr_rec.book_type_code,
1424                    X_Asset_Id                  => l_asset_hdr_rec.asset_id,
1425                    X_terminal_gain_loss_amount => FND_API.G_MISS_NUM,
1426                    X_mrc_sob_type_code         => p_mrc_sob_type_code,
1427                    X_set_of_books_id           => p_asset_hdr_rec.set_of_books_id,
1428                    X_Calling_Fn                => l_calling_fn, p_log_level_rec => p_log_level_rec);
1429 
1430 
1431    if (p_log_level_rec.statement_level) then
1432       fa_debug_pkg.add(l_calling_fn, 'End', 'Success', p_log_level_rec => p_log_level_rec);
1433    end if;
1434 
1435    if (p_trans_rec.calling_interface <> 'FAXASSET') then
1436 
1437       if (nvl(l_asset_fin_rec_old.tracking_method, 'NO TRACK') = 'ALLOCATE') and
1438          (fa_cache_pkg.fazcdrd_record.rule_name = 'ENERGY PERIOD END BALANCE') then
1439          if (p_log_level_rec.statement_level) then
1440             fa_debug_pkg.add(l_calling_fn, 'Calling Do_Allocation', ' ', p_log_level_rec => p_log_level_rec);
1441             fa_debug_pkg.add(l_calling_fn, 'proceeds_of_sale', p_asset_retire_rec.proceeds_of_sale, p_log_level_rec => p_log_level_rec);
1442             fa_debug_pkg.add(l_calling_fn, 'cost_of_removal', p_asset_retire_rec.cost_of_removal, p_log_level_rec => p_log_level_rec);
1443             fa_debug_pkg.add(l_calling_fn, 'reserve_retired', p_asset_retire_rec.reserve_retired, p_log_level_rec => p_log_level_rec);
1444             fa_debug_pkg.add(l_calling_fn, 'recapture_amount', p_asset_retire_rec.detail_info.recapture_amount, p_log_level_rec => p_log_level_rec);
1445          end if;
1446 
1447          l_reserve_allocated := p_asset_retire_rec.cost_retired -
1448                                 nvl(p_asset_retire_rec.proceeds_of_sale, 0) +
1449                                 nvl(p_asset_retire_rec.cost_of_removal, 0) -
1450                                 nvl(p_asset_retire_rec.reserve_retired, 0)+
1451                                 nvl(p_asset_retire_rec.detail_info.recapture_amount, 0);
1452 
1453          if not FA_RETIREMENT_PVT.Do_Allocation(
1454                          p_trans_rec         => l_trans_rec,
1455                          p_asset_hdr_rec     => l_asset_hdr_rec,
1456                          p_asset_fin_rec     => l_asset_fin_rec_new,
1457                          p_asset_deprn_rec_new => l_asset_deprn_rec_new,
1458                          p_period_rec        => l_period_rec,
1459                          p_reserve_amount    => l_reserve_allocated,
1460                          p_mem_ret_thid      => p_asset_retire_rec.detail_info.transaction_header_id_in,
1461                          p_mrc_sob_type_code => p_mrc_sob_type_code,
1462                          p_calling_fn        => l_calling_fn, p_log_level_rec => p_log_level_rec) then
1463             if (p_log_level_rec.statement_level) then
1464                fa_debug_pkg.add(l_calling_fn, 'Calling FA_ADJUSTMENT_PVT.Do_Allocation', 'Failed',  p_log_level_rec => p_log_level_rec);
1465             end if;
1466 
1467             raise rein_err;
1468          end if;
1469       end if; -- (nvl(l_asset_fin_rec_old.tracking_method, 'NO TRACK') = 'ALLOCATE')
1470 
1471    end if; -- (p_trans_rec.calling_interface <> 'FAXASSET')
1472 
1473    if (p_log_level_rec.statement_level) then
1474       for r_dist in c_dist loop
1475          fa_debug_pkg.add(l_calling_fn, 'distribution_id', r_dist.distribution_id, p_log_level_rec => p_log_level_rec);
1476          fa_debug_pkg.add(l_calling_fn, 'transaction_header_id_in', r_dist.transaction_header_id_in, p_log_level_rec => p_log_level_rec);
1477          fa_debug_pkg.add(l_calling_fn, 'transaction_header_id_out', r_dist.transaction_header_id_out, p_log_level_rec => p_log_level_rec);
1478          fa_debug_pkg.add(l_calling_fn, 'units_assigned', r_dist.units_assigned, p_log_level_rec => p_log_level_rec);
1479          fa_debug_pkg.add(l_calling_fn, 'transaction_units', r_dist.transaction_units, p_log_level_rec => p_log_level_rec);
1480       end loop;
1481    end if;
1482 
1483    return true;
1484 
1485 EXCEPTION
1486   when rein_err then
1487     if (p_log_level_rec.statement_level) then
1488        fa_debug_pkg.add(l_calling_fn, '(rein_err)Processing reinstatement', 'Failed');
1489     end if;
1490 
1491     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1492 
1493     return false;
1494 
1495   when OTHERS then
1496     if (p_log_level_rec.statement_level) then
1497        fa_debug_pkg.add(l_calling_fn, '(others)Processing reinstatement', 'Failed');
1498     end if;
1499 
1500     fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1501 
1502     return false;
1503 
1504 END DO_REINSTATEMENT;
1505 
1506 /*====================================================================+
1507  | Function                                                           |
1508  |   CALC_GAIN_LOSS_FOR_RET                                           |
1509  |                                                                    |
1510  | Description                                                        |
1511  |   This function maintain FA_(MC_)ADJUSTMENTS table for group       |
1512  |   retirement.                                                      |
1513  |                                                                    |
1514  +====================================================================*/
1515 FUNCTION CALC_GAIN_LOSS_FOR_RET(
1516                p_trans_rec             IN     FA_API_TYPES.trans_rec_type,
1517                p_asset_hdr_rec         IN     FA_API_TYPES.asset_hdr_rec_type,
1518                p_asset_type_rec        IN     FA_API_TYPES.asset_type_rec_type,
1519                p_asset_desc_rec        IN     FA_API_TYPES.asset_desc_rec_type,
1520                p_asset_cat_rec         IN     FA_API_TYPES.asset_cat_rec_type,
1521                p_asset_fin_rec         IN     FA_API_TYPES.asset_fin_rec_type,
1522                p_period_rec            IN     FA_API_TYPES.period_rec_type,
1523                p_asset_retire_rec      IN     FA_API_TYPES.asset_retire_rec_type,
1524                p_group_thid            IN     NUMBER,
1525                p_salvage_value_retired IN     NUMBER,
1526                p_mrc_sob_type_code     IN     VARCHAR2,
1527                p_mode                  IN     VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
1528 return BOOLEAN is
1529 
1530 /* --Commented out as part of fix for 3188851.
1531   -- Cursor to get transaction header id of transfer out in
1532   -- case of partial unit retirement
1533   CURSOR c_get_thid IS
1534     select distinct nvl(transaction_header_id_out,0)
1535     from   fa_distribution_history
1536     where  asset_id = p_asset_hdr_rec.asset_id
1537     and    book_type_code = p_asset_hdr_rec.book_type_code
1538     and    retirement_id = p_asset_retire_rec.retirement_id; */
1539 
1540    CURSOR c_get_group_method_info is                  -- ENERGY
1541       select db.rule_name                             -- ENERGY
1542       from   fa_deprn_basis_rules db                  -- ENERGY
1543            , fa_methods mt                            -- ENERGY
1544            , fa_books bk                              -- ENERGY
1545       where  bk.asset_id = p_asset_fin_rec.group_asset_id           -- ENERGY
1546       and    bk.book_type_code = p_asset_hdr_rec.book_type_code   -- ENERGY
1547       and    bk.transaction_header_id_out is null                      -- ENERGY
1548       and    bk.deprn_method_code = mt.method_code                     -- ENERGY
1549       and    nvl(bk.life_in_months, -99) = nvl(mt.life_in_months, -99) -- ENERGY
1550       and    mt.deprn_basis_rule_id = db.deprn_basis_rule_id;          -- ENERGY
1551 
1552   l_calling_fn        VARCHAR2(100) := 'fa_retirement_pvt.calc_gain_loss_for_ret';
1553 
1554   l_asset_hdr_rec   FA_API_TYPES.asset_hdr_rec_type;
1555   l_asset_cat_rec   FA_API_TYPES.asset_cat_rec_type;
1556 
1557   l_trans_rec         FA_API_TYPES.trans_rec_type;
1558   l_asset_fin_rec_adj FA_API_TYPES.asset_fin_rec_type;
1559   l_asset_fin_rec_new FA_API_TYPES.asset_fin_rec_type;
1560   l_asset_deprn_rec   FA_API_TYPES.asset_deprn_rec_type;
1561 
1562   l_adj               FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT;
1563 
1564   l_deprn_exp         NUMBER;
1565   l_bonus_deprn_exp   NUMBER;
1566   l_impairment_exp    NUMBER;
1567 
1568   l_group_db_rule_name VARCHAR2(80);
1569 
1570   calc_err            EXCEPTION;
1571 
1572 
1573 BEGIN
1574   if (p_log_level_rec.statement_level) then
1575     fa_debug_pkg.add(l_calling_fn, 'Begin',
1576                      p_asset_hdr_rec.asset_id||':'||p_asset_fin_rec.group_asset_id, p_log_level_rec => p_log_level_rec);
1577   end if;
1578 
1579   -- BUG# 3641747
1580   -- call cache as it was not yet initialized
1581   if not fa_cache_pkg.fazccb(p_asset_hdr_rec.book_type_code,
1582                              p_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
1583      fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1584      raise calc_err;
1585   end if;
1586 
1587   l_adj.transaction_header_id   := p_trans_rec.transaction_header_id;
1588   l_adj.book_type_code          := p_asset_hdr_rec.book_type_code;
1589   l_adj.period_counter_created  := p_period_rec.period_counter;
1590   l_adj.period_counter_adjusted := p_period_rec.period_counter;
1591   l_adj.current_units           := p_asset_desc_rec.current_units;
1592   l_adj.selection_retid         := 0;
1593   l_adj.leveling_flag           := TRUE;
1594   l_adj.flush_adj_flag          := FALSE;
1595 
1596   l_adj.last_update_date        := p_trans_rec.who_info.last_update_date;
1597 
1598   l_adj.gen_ccid_flag           := TRUE;
1599 
1600   l_adj.asset_id                := p_asset_hdr_rec.asset_id;
1601 
1602   l_adj.adjustment_type         := 'COST';
1603   l_adj.debit_credit_flag       := 'CR';
1604   l_adj.code_combination_id     := 0;
1605   l_adj.mrc_sob_type_code       := p_mrc_sob_type_code;
1606   l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
1607 
1608   if p_asset_type_rec.asset_type = 'CIP' then
1609     l_adj.source_type_code := 'CIP RETIREMENT';
1610     l_adj.account := fa_cache_pkg.fazccb_record.cip_cost_acct;
1611     l_adj.account_type := 'CIP_COST_ACCT';
1612   else
1613     l_adj.source_type_code := 'RETIREMENT';
1614     l_adj.account := fa_cache_pkg.fazccb_record.asset_cost_acct;
1615     l_adj.account_type := 'ASSET_COST_ACCT';
1616   end if;
1617 
1618   --fa_debug_pkg.add(l_calling_fn,'Before 1st check, book_class', fa_cache_pkg.fazcbc_record.book_class, p_log_level_rec => p_log_level_rec);
1619 
1620   if ((nvl(p_asset_retire_rec.units_retired, 0) > 0) and
1621       (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE') and
1622       (p_mode = 'CR')) then
1623     /* --Commented out as part of fix for 3188851.
1624      OPEN c_get_thid;
1625      FETCH c_get_thid INTO l_adj.selection_thid;
1626      CLOSE c_get_thid;
1627 
1628      if (nvl(l_adj.selection_thid, 0) = 0) then
1629 
1630         if (p_log_level_rec.statement_level) then
1631            fa_debug_pkg.add(l_calling_fn, 'Error', 'Getting selection_thid', p_log_level_rec => p_log_level_rec);
1632         end if;
1633 
1634         raise calc_err;
1635      end if; */
1636      l_adj.adjustment_amount       := p_asset_fin_rec.cost;
1637      l_adj.selection_thid          := p_trans_rec.transaction_header_id;
1638      l_adj.selection_mode          := FA_STD_TYPES.FA_AJ_CLEAR;
1639 --tk_util.DumpAdjRec(l_adj, 'URCR');
1640      if not FA_INS_ADJUST_PKG.faxinaj
1641                          (l_adj,
1642                           p_trans_rec.who_info.last_update_date,
1643                           p_trans_rec.who_info.last_updated_by,
1644                           p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1645         raise calc_err;
1646      end if;
1647 
1648   -- Not a partial unit ret.
1649   -- 3440308
1650   elsif ((p_asset_retire_rec.units_retired IS NULL) OR
1651         (NVL(p_asset_retire_rec.units_retired, 0) = g_current_units)) THEN
1652   --end 3440308
1653      l_adj.adjustment_amount       := p_asset_retire_rec.cost_retired;
1654      l_adj.selection_thid          := 0;
1655      l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
1656 
1657      if not FA_INS_ADJUST_PKG.faxinaj
1658                          (l_adj,
1659                           p_trans_rec.who_info.last_update_date,
1660                           p_trans_rec.who_info.last_updated_by,
1661                           p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1662         raise calc_err;
1663      end if;
1664   end if;
1665 
1666   --fa_debug_pkg.add(l_calling_fn,'Before DR logic, book',p_asset_hdr_rec.book_type_code, p_log_level_rec => p_log_level_rec);
1667   -- Need to call cache again as this gets reset when the dist api gets called.
1668   if not fa_cache_pkg.fazcbc(x_book => p_asset_hdr_rec.book_type_code, p_log_level_rec => p_log_level_rec) then
1669       raise calc_err;
1670   end if;
1671 
1672   --Doing this check due to 3188851.
1673   if p_mode <> 'CR' then
1674 
1675     --fa_debug_pkg.add(l_calling_fn,'In DR logic, units_retired',p_asset_retire_rec.units_retired, p_log_level_rec => p_log_level_rec);
1676     --fa_debug_pkg.add(l_calling_fn,'In DR logic, book_class', fa_cache_pkg.fazcbc_record.book_class, p_log_level_rec => p_log_level_rec);
1677 
1678     if ((nvl(p_asset_retire_rec.units_retired, 0) > 0) and
1679        (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE')) then
1680        --
1681        -- With unkonwn reason l_adj.account is set to null so
1682        -- need to resert the same value.
1683        --
1684        if p_asset_type_rec.asset_type = 'CIP' then
1685           l_adj.account := fa_cache_pkg.fazccb_record.cip_cost_acct;
1686        else
1687           l_adj.account := fa_cache_pkg.fazccb_record.asset_cost_acct;
1688        end if;
1689 
1690        l_adj.selection_thid          := 0;
1691        l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
1692        l_adj.current_units           := p_asset_desc_rec.current_units;
1693 
1694        l_adj.debit_credit_flag := 'DR';
1695        l_adj.leveling_flag := FALSE;
1696        l_adj.adjustment_amount := p_asset_fin_rec.cost - p_asset_retire_rec.cost_retired;
1697 --tk_util.DumpAdjRec(l_adj, 'URDR');
1698        if (l_adj.adjustment_amount <>0) then
1699           if not FA_INS_ADJUST_PKG.faxinaj
1700                               (l_adj,
1701                                p_trans_rec.who_info.last_update_date,
1702                                p_trans_rec.who_info.last_updated_by,
1703                                p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1704             raise calc_err;
1705           end if;
1706        end if;
1707 /* -- no need for this since it will use above condition in CR logic.
1708     else
1709       --tk_util.DumpAdjRec(l_adj, 'URCR');
1710        if not FA_INS_ADJUST_PKG.faxinaj
1711                          (l_adj,
1712                           p_trans_rec.who_info.last_update_date,
1713                           p_trans_rec.who_info.last_updated_by,
1714                           p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1715           raise calc_err;
1716        end if; */
1717 
1718     end if; -- ((nvl(p_asset_retire_rec.units_retired, 0) > 0) and
1719 
1720    --
1721    -- Creating Member Entires for Tracking
1722    --
1723    if (p_asset_fin_rec.tracking_method = 'ALLOCATE') or
1724       (p_asset_fin_rec.tracking_method = 'CALCULATE') and
1725        (nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'N') then
1726       l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
1727       l_adj.distribution_id         := 0;
1728 
1729       l_adj.adjustment_type         := 'RESERVE';
1730       l_adj.debit_credit_flag       := 'DR';
1731       l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
1732       l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
1733 
1734       OPEN c_get_group_method_info;                                                          -- ENERGY
1735       FETCH c_get_group_method_info INTO l_group_db_rule_name;                               -- ENERGY
1736       CLOSE c_get_group_method_info;                                                         -- ENERGY
1737 
1738       -- BUG# 6899255
1739       -- now handling member track amount the same for energy
1740       -- and non-energy cases by hitting the true tracked
1741       -- amount, rather than the cost to avoid leaving a balance.
1742       --
1743       -- (l_group_db_rule_name = 'ENERGY PERIOD END BALANCE')
1744       -- ENERGY
1745 
1746       if (nvl(p_asset_fin_rec.tracking_method, 'NON TRACK')  = 'ALLOCATE') then
1747                                                                                              -- ENERGY
1748          if not fa_util_pvt.get_asset_deprn_rec (                                            -- ENERGY
1749                  p_asset_hdr_rec         => p_asset_hdr_rec,                                 -- ENERGY
1750                  px_asset_deprn_rec      => l_asset_deprn_rec,                               -- ENERGY
1751                  p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then                        -- ENERGY
1752             raise calc_err;                                                                  -- ENERGY
1753          end if;                                                                             -- ENERGY
1754          -- bug9431199
1755          if (p_asset_fin_rec.cost <> 0 ) then                                                                                   -- ENERGY
1756              l_adj.adjustment_amount := l_asset_deprn_rec.deprn_reserve *                        -- ENERGY
1757                                         (p_asset_retire_rec.cost_retired/p_asset_fin_rec.cost);  -- ENERGY
1758          else
1759              l_adj.adjustment_amount :=0;
1760          end if;
1761                                                                                              -- ENERGY
1762          if not FA_UTILS_PKG.faxrnd(l_adj.adjustment_amount,                                 -- ENERGY
1763                                     p_asset_hdr_rec.book_type_code,
1764                                     p_asset_hdr_rec.set_of_books_id,
1765                                     p_log_level_rec => p_log_level_rec) then                    -- ENERGY
1766             raise calc_err;                                                                  -- ENERGY
1767          end if;                                                                             -- ENERGY
1768       else                                                                                   -- ENERGY
1769          l_adj.adjustment_amount       := p_asset_retire_rec.cost_retired;
1770       end if;                                                                                -- ENERGY
1771 
1772       l_adj.track_member_flag       := 'Y';
1773 
1774       if (nvl(p_asset_retire_rec.cost_of_removal, 0) = 0) and
1775          (nvl(p_asset_retire_rec.proceeds_of_sale, 0) = 0) then
1776          l_adj.flush_adj_flag          := TRUE;
1777       end if;
1778 
1779 --tk_util.DumpAdjRec(l_adj, 'RSVm');
1780       if (p_log_level_rec.statement_level) then
1781          fa_debug_pkg.add(l_calling_fn, 'Retired Reserve', l_adj.adjustment_amount, p_log_level_rec => p_log_level_rec);
1782       end if;
1783 
1784       if not FA_INS_ADJUST_PKG.faxinaj
1785                             (l_adj,
1786                              p_trans_rec.who_info.last_update_date,
1787                              p_trans_rec.who_info.last_updated_by,
1788                              p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1789         raise calc_err;
1790       end if;
1791 
1792       if (p_asset_fin_rec.tracking_method <>  'ALLOCATE') then
1793 
1794          if (nvl(p_asset_retire_rec.proceeds_of_sale, 0) <> 0) then
1795             l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
1796             l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
1797             l_adj.adjustment_amount       := p_asset_retire_rec.proceeds_of_sale;
1798             l_adj.track_member_flag       := 'Y';
1799             l_adj.flush_adj_flag          := FALSE;
1800 
1801 --tk_util.DumpAdjRec(l_adj, 'RSVposm');
1802 
1803             if (p_log_level_rec.statement_level) then
1804                fa_debug_pkg.add(l_calling_fn, 'Retired Reserve for Proceeds of Sale', l_adj.adjustment_amount, p_log_level_rec => p_log_level_rec);
1805             end if;
1806 
1807             if not FA_INS_ADJUST_PKG.faxinaj
1808                                   (l_adj,
1809                                    p_trans_rec.who_info.last_update_date,
1810                                    p_trans_rec.who_info.last_updated_by,
1811                                    p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1812                raise calc_err;
1813             end if;
1814          end if; -- (nvl(p_asset_retire_rec.proceeds_of_sale, 0) <> 0)
1815 
1816          if (nvl(p_asset_retire_rec.cost_of_removal, 0) <> 0) then
1817             l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
1818             l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
1819             l_adj.adjustment_amount       := p_asset_retire_rec.cost_of_removal;
1820             l_adj.track_member_flag       := 'Y';
1821             l_adj.flush_adj_flag          := FALSE;
1822 
1823 --tk_util.DumpAdjRec(l_adj, 'RSVcorm');
1824 
1825             if (p_log_level_rec.statement_level) then
1826                fa_debug_pkg.add(l_calling_fn, 'Retired Reserve for Cost of Removal', l_adj.adjustment_amount, p_log_level_rec => p_log_level_rec);
1827             end if;
1828 
1829             if not FA_INS_ADJUST_PKG.faxinaj
1830                                   (l_adj,
1831                                    p_trans_rec.who_info.last_update_date,
1832                                    p_trans_rec.who_info.last_updated_by,
1833                                    p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1834               raise calc_err;
1835             end if;
1836          end if;
1837 
1838          if (nvl(p_asset_retire_rec.cost_of_removal, 0) <> 0) then
1839             l_adj.adjustment_type         := 'REMOVALCOST CLR';
1840             l_adj.debit_credit_flag       := 'CR';
1841             l_adj.account_type            := 'COST_OF_REMOVAL_GAIN_ACCT';
1842             l_adj.account                 := fa_cache_pkg.fazcbc_record.cost_of_removal_gain_acct;
1843             l_adj.adjustment_amount       := p_asset_retire_rec.cost_of_removal;
1844             l_adj.flush_adj_flag          := FALSE;
1845             l_adj.track_member_flag       := 'Y';
1846 
1847 --tk_util.DumpAdjRec(l_adj, 'Member COR');
1848 
1849             if (p_log_level_rec.statement_level) then
1850                fa_debug_pkg.add(l_calling_fn, 'Cost of Removal for member asset', l_adj.adjustment_amount, p_log_level_rec => p_log_level_rec);
1851             end if;
1852 
1853             if not FA_INS_ADJUST_PKG.faxinaj
1854                                   (l_adj,
1855                                    p_trans_rec.who_info.last_update_date,
1856                                    p_trans_rec.who_info.last_updated_by,
1857                                    p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1858                raise calc_err;
1859             end if;
1860          end if;
1861 
1862         --BUG5614568: modified the account_type,account and added the adjustment_amount.
1863         if (nvl(p_asset_retire_rec.proceeds_of_sale, 0) <> 0) then
1864            l_adj.adjustment_type         := 'PROCEEDS CLR';
1865            l_adj.debit_credit_flag       := 'DR';
1866            l_adj.account_type            := 'PROCEEDS_OF_SALE_CLEARING_ACCT';
1867            l_adj.account                 := fa_cache_pkg.fazcbc_record.proceeds_of_sale_clearing_acct;
1868            l_adj.adjustment_amount       := p_asset_retire_rec.proceeds_of_sale;
1869            l_adj.flush_adj_flag          := FALSE;
1870            l_adj.track_member_flag       := 'Y';
1871 
1872 --tk_util.DumpAdjRec(l_adj, 'MemberPOS');
1873 
1874             if (p_log_level_rec.statement_level) then
1875                fa_debug_pkg.add(l_calling_fn, 'Proceeds of Sale for member asset', l_adj.adjustment_amount, p_log_level_rec => p_log_level_rec);
1876             end if;
1877 
1878             if not FA_INS_ADJUST_PKG.faxinaj
1879                                   (l_adj,
1880                                    p_trans_rec.who_info.last_update_date,
1881                                    p_trans_rec.who_info.last_updated_by,
1882                                    p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1883                raise calc_err;
1884             end if;
1885          end if;
1886       end if; -- ((p_asset_fin_rec.tracking_method <>  'ALLOCATE')
1887 
1888    end if; -- (p_asset_fin_rec.tracking_method = 'ALLOCATE') or
1889    -- End of Creating Member Entires for Tracking
1890 
1891 
1892   --***********************************************
1893   -- Prepare local variables with group information
1894   --
1895   l_asset_hdr_rec.asset_id := p_asset_fin_rec.group_asset_id;
1896   l_asset_hdr_rec.book_type_code := p_asset_hdr_rec.book_type_code;
1897   l_asset_hdr_rec.set_of_books_id := p_asset_hdr_rec.set_of_books_id;
1898   -- l_asset_hdr_rec.period_of_addition := 'N';  -- Set this if necessary
1899 
1900   if not FA_UTIL_PVT.get_asset_cat_rec (
1901                          p_asset_hdr_rec  => l_asset_hdr_rec,
1902                          px_asset_cat_rec => l_asset_cat_rec,
1903                          p_date_effective  => NULL, p_log_level_rec => p_log_level_rec) then
1904     raise calc_err;
1905   end if;
1906 
1907   --
1908   -- Set category book cache w/ group information.
1909   --
1910   /* bug#15897249 starts */
1911    If (not (nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
1912        nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) ) then
1913      if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
1914                                 l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
1915        fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1916        raise calc_err;
1917      end if;
1918 
1919      l_adj.asset_id                := p_asset_fin_rec.group_asset_id;
1920      l_adj.transaction_header_id   := p_group_thid;
1921    end if;
1922   /*bug#15897249 ends */
1923   if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
1924                              l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
1925     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1926     raise calc_err;
1927   end if;
1928 
1929   l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
1930   l_adj.distribution_id         := 0;
1931 
1932   l_adj.adjustment_type         := 'RESERVE';
1933   l_adj.debit_credit_flag       := 'DR';
1934   l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
1935   l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
1936   l_adj.adjustment_amount       := p_asset_retire_rec.cost_retired;
1937   l_adj.track_member_flag       := null;
1938 
1939   if (nvl(p_asset_retire_rec.cost_of_removal, 0) = 0) and
1940      (nvl(p_asset_retire_rec.proceeds_of_sale, 0) = 0) then
1941     l_adj.flush_adj_flag          := TRUE;
1942   end if;
1943 
1944 --tk_util.DumpAdjRec(l_adj, 'RSV');
1945   if not FA_INS_ADJUST_PKG.faxinaj
1946                          (l_adj,
1947                           p_trans_rec.who_info.last_update_date,
1948                           p_trans_rec.who_info.last_updated_by,
1949                           p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1950     raise calc_err;
1951   end if;
1952 
1953   --
1954   -- NBV RETIRED/RESERVE entries as a result of limiting net proceeds to cost retired
1955   --
1956   if (p_asset_fin_rec.limit_proceeds_flag = 'Y') then
1957      if (nvl(p_asset_retire_rec.proceeds_of_sale, 0) -
1958          nvl(p_asset_retire_rec.cost_of_removal, 0) > (p_asset_retire_rec.cost_retired -
1959                                                        p_salvage_value_retired)) then
1960 
1961         l_adj.adjustment_type   := 'RESERVE';
1962         l_adj.debit_credit_flag := 'DR';
1963         l_adj.account_type      := 'DEPRN_RESERVE_ACCT';
1964         l_adj.account           := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
1965         l_adj.adjustment_amount := p_asset_retire_rec.detail_info.gain_loss_amount;
1966 
1967 --tk_util.DumpAdjRec(l_adj, 'LNP RSV');
1968         if not FA_INS_ADJUST_PKG.faxinaj
1969                                (l_adj,
1970                                 p_trans_rec.who_info.last_update_date,
1971                                 p_trans_rec.who_info.last_updated_by,
1972                                 p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1973           raise calc_err;
1974         end if;
1975 
1976 
1977         l_adj.adjustment_type   := 'NBV RETIRED';
1978         l_adj.debit_credit_flag := 'CR';
1979         l_adj.account_type      := 'NBV_RETIRED_GAIN_ACCT';
1980         l_adj.account           := fa_cache_pkg.fazcbc_record.nbv_retired_gain_acct;
1981         l_adj.adjustment_amount := p_asset_retire_rec.detail_info.gain_loss_amount;
1982 
1983 --tk_util.DumpAdjRec(l_adj, 'LNP NBVRET');
1984         if not FA_INS_ADJUST_PKG.faxinaj
1985                                (l_adj,
1986                                 l_trans_rec.who_info.last_update_date,
1987                                 l_trans_rec.who_info.last_updated_by,
1988                                 l_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
1989            raise calc_err;
1990         end if;
1991      end if; -- nvl(p_asset_retire_rec.proceeds_of_sale, 0) -
1992 
1993 
1994   end if; -- (p_asset_fin_rec.limit_proceeds_flag = 'Y')
1995 
1996   --
1997   -- Create REMOVALCOST CLR and RESERVE entries
1998   --
1999   if (not(nvl(p_asset_retire_rec.cost_of_removal, 0) = 0)) then
2000     l_adj.adjustment_type   := 'RESERVE';
2001     l_adj.debit_credit_flag := 'DR';
2002     l_adj.account_type      := 'DEPRN_RESERVE_ACCT';
2003     l_adj.account           := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
2004     l_adj.adjustment_amount       := p_asset_retire_rec.cost_of_removal;
2005 --tk_util.DumpAdjRec(l_adj, 'COR');
2006     if not FA_INS_ADJUST_PKG.faxinaj
2007                            (l_adj,
2008                             p_trans_rec.who_info.last_update_date,
2009         --p_mode <> 'CR'                     p_trans_rec.who_info.last_updated_by,
2010                             p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2011       raise calc_err;
2012     end if;
2013 
2014     /* bug#15897249 added if condition */
2015     If (not (nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
2016       nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) ) then
2017       l_adj.asset_id                := p_asset_fin_rec.group_asset_id;
2018     End if;
2019     /* bug#15897249 end */
2020     l_adj.adjustment_type         := 'REMOVALCOST CLR';
2021     l_adj.debit_credit_flag       := 'CR';
2022     l_adj.account_type            := 'COST_OF_REMOVAL_GAIN_ACCT';
2023     l_adj.account                 := fa_cache_pkg.fazcbc_record.cost_of_removal_gain_acct;
2024 
2025     if (nvl(p_asset_retire_rec.proceeds_of_sale, 0) = 0) then
2026       l_adj.flush_adj_flag          := TRUE;
2027     end if;
2028 --tk_util.DumpAdjRec(l_adj, 'COR');
2029     if not FA_INS_ADJUST_PKG.faxinaj
2030                            (l_adj,
2031                             p_trans_rec.who_info.last_update_date,
2032                             p_trans_rec.who_info.last_updated_by,
2033                             p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2034       raise calc_err;
2035     end if;
2036 
2037   end if;
2038 
2039   --
2040   -- Create PROCEEDS CLR and RESERVE entries
2041   --
2042   if (not(nvl(p_asset_retire_rec.proceeds_of_sale, 0) = 0)) then
2043     l_adj.adjustment_type         := 'RESERVE';
2044     l_adj.debit_credit_flag       := 'CR';
2045     /* bug#15897249 added if condition */
2046     If (not (nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
2047       nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) ) then
2048       l_adj.asset_id                := p_asset_fin_rec.group_asset_id;
2049     End if;
2050     /* bug#15897249 end */
2051     l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
2052     l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
2053     l_adj.adjustment_amount       := p_asset_retire_rec.proceeds_of_sale;
2054 
2055 --tk_util.DumpAdjRec(l_adj, 'POS');
2056     if not FA_INS_ADJUST_PKG.faxinaj
2057                            (l_adj,
2058                             p_trans_rec.who_info.last_update_date,
2059                             p_trans_rec.who_info.last_updated_by,
2060                             p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2061       raise calc_err;
2062     end if;
2063 
2064     l_adj.adjustment_type         := 'PROCEEDS CLR';
2065     l_adj.debit_credit_flag       := 'DR';
2066     /* bug#15897249 added if condition */
2067     If (not (nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
2068       nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) ) then
2069       l_adj.asset_id                := p_asset_fin_rec.group_asset_id;
2070     End if;
2071     /* bug#15897249 end */
2072     --l_adj.account_type          := 'COST_OF_REMOVAL_GAIN_ACCT';  --bug 8580562
2073     --l_adj.account               := fa_cache_pkg.fazcbc_record.proceeds_of_sale_gain_acct;  --bug 8580562
2074     l_adj.account_type            := 'PROCEEDS_OF_SALE_CLEARING_ACCT';
2075     l_adj.account                 := fa_cache_pkg.fazcbc_record.proceeds_of_sale_clearing_acct;
2076     l_adj.flush_adj_flag          := TRUE;
2077 
2078 --tk_util.DumpAdjRec(l_adj, 'POS');
2079     if not FA_INS_ADJUST_PKG.faxinaj
2080                            (l_adj,
2081                             p_trans_rec.who_info.last_update_date,
2082                             p_trans_rec.who_info.last_updated_by,
2083                             p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2084       raise calc_err;
2085     end if;
2086 
2087   end if; -- (not(nvl(p_asset_retire_rec.proceeds_of_sale, 0) = 0))
2088 
2089   --
2090   -- Calculating catchup only if tracking method is "CALCULATE".
2091   -- This is only necessary if this is prior period retirement.
2092   -- For now member asset prior period retirement is restricted.
2093   --
2094   if (p_asset_fin_rec.tracking_method = 'CALCULATE') and
2095      (p_period_rec.calendar_period_open_date >
2096       p_trans_rec.transaction_date_entered) then
2097 
2098      l_trans_rec := p_trans_rec;
2099 
2100      if not fa_util_pvt.get_asset_deprn_rec (
2101                 p_asset_hdr_rec         => p_asset_hdr_rec,
2102                 px_asset_deprn_rec      => l_asset_deprn_rec,
2103                 p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
2104         raise calc_err;
2105      end if;
2106 
2107      l_asset_fin_rec_adj.cost := -1 * p_asset_retire_rec.cost_retired;
2108      l_asset_fin_rec_adj.unrevalued_cost := -1 * p_asset_retire_rec.cost_retired;
2109 
2110      if not FA_AMORT_PVT.faxama(
2111                          px_trans_rec          => l_trans_rec,
2112                          p_asset_hdr_rec       => p_asset_hdr_rec,
2113                          p_asset_desc_rec      => p_asset_desc_rec,
2114                          p_asset_cat_rec       => p_asset_cat_rec,
2115                          p_asset_type_rec      => p_asset_type_rec,
2116                          p_asset_fin_rec_old   => p_asset_fin_rec,
2117                          p_asset_fin_rec_adj   => l_asset_fin_rec_adj,
2118                          px_asset_fin_rec_new  => l_asset_fin_rec_new,
2119                          p_asset_deprn_rec     => l_asset_deprn_rec,
2120                          p_period_rec          => p_period_rec,
2121                          p_mrc_sob_type_code   => p_mrc_sob_type_code,
2122                          p_running_mode        => fa_std_types.FA_DPR_NORMAL,
2123                          p_used_by_revaluation => null,
2124                          x_deprn_exp           => l_deprn_exp,
2125                          x_bonus_deprn_exp     => l_bonus_deprn_exp,
2126                          x_impairment_exp      => l_impairment_exp, p_log_level_rec => p_log_level_rec) then
2127         raise calc_err;
2128      end if;
2129 
2130      if (nvl(l_deprn_exp, 0) <> 0) or
2131         (nvl(l_bonus_deprn_exp, 0) <> 0) or
2132         (nvl(l_impairment_exp, 0) <> 0) then
2133 
2134         l_adj.transaction_header_id   := p_trans_rec.transaction_header_id;
2135         l_adj.book_type_code          := p_asset_hdr_rec.book_type_code;
2136         l_adj.period_counter_created  := p_period_rec.period_counter;
2137         l_adj.period_counter_adjusted := p_period_rec.period_counter;
2138         l_adj.current_units           := p_asset_desc_rec.current_units;
2139         l_adj.selection_retid         := 0;
2140         l_adj.leveling_flag           := TRUE;
2141         l_adj.last_update_date        := p_trans_rec.who_info.last_update_date;
2142         l_adj.gen_ccid_flag           := TRUE;
2143         l_adj.flush_adj_flag          := FALSE;
2144 
2145         if (nvl(l_deprn_exp, 0) <> 0) then
2146            -- Catchup expense for Member
2147            -- Call category book cache for group to get expense account
2148            if not fa_cache_pkg.fazccb
2149                   (X_book   => p_asset_hdr_rec.book_type_code,
2150                    X_cat_id => p_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2151               raise calc_err;
2152            end if;
2153 
2154            l_adj.source_type_code        := 'DEPRECIATION';
2155            l_adj.adjustment_type         := 'EXPENSE';
2156            l_adj.account                 := fa_cache_pkg.fazccb_record.deprn_expense_acct;
2157            l_adj.account_type            := 'DEPRN_EXPENSE_ACCT';
2158            l_adj.asset_id                := p_asset_hdr_rec.asset_id;
2159            l_adj.adjustment_amount       := abs(l_deprn_exp);
2160            l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2161            l_adj.mrc_sob_type_code       :=  p_mrc_sob_type_code;
2162            l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
2163 
2164            if (nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'N') then
2165               l_adj.track_member_flag       := 'Y';
2166            else
2167               l_adj.track_member_flag       := null;
2168            end if;
2169 
2170            if (nvl(l_bonus_deprn_exp, 0) = 0) then
2171               l_adj.flush_adj_flag          := TRUE;
2172            end if;
2173 
2174            if (nvl(l_impairment_exp, 0) = 0) then
2175               l_adj.flush_adj_flag          := TRUE;
2176            end if;
2177 
2178            if (l_deprn_exp > 0) then
2179               l_adj.debit_credit_flag     := 'DR';
2180            else
2181               l_adj.debit_credit_flag     := 'CR';
2182            end if;
2183 
2184            if not FA_INS_ADJUST_PKG.faxinaj
2185                                 (l_adj,
2186                                  p_trans_rec.who_info.last_update_date,
2187                                  p_trans_rec.who_info.last_updated_by,
2188                                  p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2189               raise calc_err;
2190            end if;
2191         end if; --(nvl(l_deprn_exp, 0) <> 0)
2192 
2193         if (nvl(l_bonus_deprn_exp, 0) <> 0) then
2194            -- Catchup bonus expense for Member
2195            -- Need to call cache function if it is not called for expense.
2196 
2197            if (nvl(l_deprn_exp, 0) = 0) then
2198 
2199               if not fa_cache_pkg.fazccb(
2200                                  X_book   => p_asset_hdr_rec.book_type_code,
2201                                  X_cat_id => p_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2202                  raise calc_err;
2203               end if;
2204            end if;
2205 
2206            l_adj.source_type_code        := 'DEPRECIATION';
2207            l_adj.adjustment_type         := 'BONUS EXPENSE';
2208            l_adj.account                 := fa_cache_pkg.fazccb_record.bonus_deprn_expense_acct;
2209            l_adj.account_type            := 'BONUS_DEPRN_EXPENSE_ACCT';
2210            l_adj.asset_id                := p_asset_hdr_rec.asset_id;
2211            l_adj.adjustment_amount       := abs(l_deprn_exp);
2212            l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2213            l_adj.mrc_sob_type_code       :=  p_mrc_sob_type_code;
2214            l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
2215 
2216            if (nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'N') then
2217               l_adj.track_member_flag       := 'Y';
2218            else
2219               l_adj.track_member_flag       := null;
2220            end if;
2221            l_adj.flush_adj_flag          := TRUE;
2222 
2223            if (l_deprn_exp > 0) then
2224               l_adj.debit_credit_flag     := 'CR';
2225            else
2226               l_adj.debit_credit_flag     := 'DR';
2227            end if;
2228 
2229            if not FA_INS_ADJUST_PKG.faxinaj
2230                                 (l_adj,
2231                                  p_trans_rec.who_info.last_update_date,
2232                                  p_trans_rec.who_info.last_updated_by,
2233                                  p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2234               raise calc_err;
2235            end if;
2236         end if; -- (nvl(l_bonus_deprn_exp, 0) <> 0)
2237 
2238         if (nvl(l_impairment_exp, 0) <> 0) then
2239            -- Catchup impairment expense for Member
2240            -- Need to call cache function if it is not called for expense.
2241 
2242            if (nvl(l_deprn_exp, 0) = 0) then
2243 
2244               if not fa_cache_pkg.fazccb(
2245                                  X_book   => p_asset_hdr_rec.book_type_code,
2246                                  X_cat_id => p_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2247                  raise calc_err;
2248               end if;
2249            end if;
2250 
2251            l_adj.source_type_code        := 'DEPRECIATION';
2252            l_adj.adjustment_type         := 'IMPAIR EXPENSE';
2253            l_adj.account                 := fa_cache_pkg.fazccb_record.impair_expense_acct;
2254            l_adj.account_type            := 'IMPAIR_EXPENSE_ACCT';
2255            l_adj.asset_id                := p_asset_hdr_rec.asset_id;
2256            l_adj.adjustment_amount       := abs(l_impairment_exp);
2257            l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2258            l_adj.mrc_sob_type_code       :=  p_mrc_sob_type_code;
2259            l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
2260 
2261            if (nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'N') then
2262               l_adj.track_member_flag       := 'Y';
2263            else
2264               l_adj.track_member_flag       := null;
2265            end if;
2266            l_adj.flush_adj_flag          := TRUE;
2267 
2268            if (l_impairment_exp > 0) then
2269               l_adj.debit_credit_flag     := 'CR';
2270            else
2271               l_adj.debit_credit_flag     := 'DR';
2272            end if;
2273 
2274            if not FA_INS_ADJUST_PKG.faxinaj
2275                                 (l_adj,
2276                                  p_trans_rec.who_info.last_update_date,
2277                                  p_trans_rec.who_info.last_updated_by,
2278                                  p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2279               raise calc_err;
2280            end if;
2281         end if; -- (nvl(l_impairment_exp, 0) <> 0)
2282 
2283      end if; -- (nvl(l_deprn_exp, 0) <> 0) or
2284 
2285   end if; -- (p_asset_fin_rec.tracking_method = 'CALCULATE')
2286 
2287   end if; --p_mode <> 'CR'
2288 
2289   if (p_log_level_rec.statement_level) then
2290      fa_debug_pkg.add(l_calling_fn, 'End', 'Success', p_log_level_rec => p_log_level_rec);
2291   end if;
2292 
2293   return true;
2294 
2295 EXCEPTION
2296   when calc_err then
2297     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2298 
2299     return false;
2300 
2301   when OTHERS then
2302     fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2303 
2304     return false;
2305 
2306 END CALC_GAIN_LOSS_FOR_RET;
2307 
2308 /*====================================================================+
2309  | Function                                                           |
2310  |   CALC_GAIN_LOSS_FOR_REI                                           |
2311  |                                                                    |
2312  | Description                                                        |
2313  |   This function maintain FA_(MC_)ADJUSTMENTS table for group       |
2314  |   reinstatement.                                                   |
2315  |                                                                    |
2316  +====================================================================*/
2317 FUNCTION CALC_GAIN_LOSS_FOR_REI(
2318            p_trans_rec         IN            FA_API_TYPES.trans_rec_type,
2319            p_asset_hdr_rec     IN            FA_API_TYPES.asset_hdr_rec_type,
2320            p_asset_type_rec    IN            FA_API_TYPES.asset_type_rec_type,
2321            p_asset_desc_rec    IN            FA_API_TYPES.asset_desc_rec_type,
2322            p_asset_cat_rec     IN            FA_API_TYPES.asset_cat_rec_type,
2323            p_asset_fin_rec     IN            FA_API_TYPES.asset_fin_rec_type,
2324            p_period_rec        IN            FA_API_TYPES.period_rec_type,
2325            p_asset_retire_rec  IN            FA_API_TYPES.asset_retire_rec_type,
2326            p_group_thid        IN            NUMBER,
2327            x_asset_fin_rec        OUT NOCOPY FA_API_TYPES.asset_fin_rec_type,
2328            p_mrc_sob_type_code IN            VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
2329 RETURN BOOLEAN IS
2330 
2331   l_calling_fn             VARCHAR2(100) := 'fa_group_retirement_pvt.calc_gain_loss_for_rei';
2332 
2333   -- Cursor to get transaction header id of transfer out in
2334   -- case of partial unit retirement
2335   CURSOR c_get_thid_for_dist IS
2336 /*    select transaction_header_id_out
2337     from   fa_distribution_history
2338     where  asset_id = p_asset_hdr_rec.asset_id
2339     and    book_type_code = p_asset_hdr_rec.book_type_code
2340     and    retirement_id = p_asset_retire_rec.retirement_id;
2341 bug 4411892*/
2342     select  transaction_header_id_out
2343     from    fa_retirements
2344     where   asset_id = p_asset_hdr_rec.asset_id
2345     and     retirement_id = p_asset_retire_rec.retirement_id
2346     and     book_type_code = p_asset_hdr_rec.book_type_code;
2347 
2348    /* Added for bug 8527733 */
2349    CURSOR c_get_ret_salvage IS
2350    select outbk.salvage_value - inbk.salvage_value
2351    from   fa_books inbk , fa_books outbk
2352    where  inbk.transaction_header_id_in = p_asset_retire_rec.detail_info.transaction_header_id_in
2353    and    outbk.asset_id = p_asset_hdr_rec.asset_id
2354    and    outbk.book_type_code = p_asset_hdr_rec.book_type_code
2355    and    outbk.transaction_header_id_out = p_asset_retire_rec.detail_info.transaction_header_id_in;
2356 
2357    CURSOR c_get_ret_salvage_mrc IS
2358    select outbk.salvage_value - inbk.salvage_value
2359    from   fa_mc_books inbk , fa_mc_books outbk
2360    where  inbk.transaction_header_id_in = p_asset_retire_rec.detail_info.transaction_header_id_in
2361    and    outbk.asset_id = p_asset_hdr_rec.asset_id
2362    and    outbk.book_type_code = p_asset_hdr_rec.book_type_code
2363    and    outbk.transaction_header_id_out = p_asset_retire_rec.detail_info.transaction_header_id_in
2364    and    inbk.set_of_books_id = p_asset_hdr_rec.set_of_books_id
2365    and    outbk.set_of_books_id = p_asset_hdr_rec.set_of_books_id ;
2366    /*End of bug 8527733 */
2367 
2368 
2369   CURSOR c_get_group_thid IS
2370     SELECT transaction_header_id,
2371            asset_id,
2372            date_effective
2373     FROM   fa_transaction_headers
2374     WHERE  member_transaction_header_id =
2375                p_asset_retire_rec.detail_info.transaction_header_id_in
2376     AND    asset_id = p_asset_fin_rec.group_asset_id
2377     AND    book_type_code = p_asset_hdr_rec.book_type_code;
2378 
2379   l_group_thid     NUMBER(15);
2380   l_group_asset_id NUMBER(15);
2381   l_date_effective DATE;
2382   l_period_rec     FA_API_TYPES.period_rec_type;
2383 
2384   -- BUG# 3031357
2385   -- adding additional columns to use N2 index for performance
2386 
2387   CURSOR c_mc_pos_cor_adj IS
2388   SELECT TRANSACTION_HEADER_ID,
2389          SOURCE_TYPE_CODE,
2390          ADJUSTMENT_TYPE,
2391          DEBIT_CREDIT_FLAG,
2392          CODE_COMBINATION_ID,
2393          BOOK_TYPE_CODE,
2394          ASSET_ID,
2395          ADJUSTMENT_AMOUNT,
2396          DISTRIBUTION_ID,
2397          ANNUALIZED_ADJUSTMENT,
2398          PERIOD_COUNTER_ADJUSTED,
2399          PERIOD_COUNTER_CREATED,
2400          ASSET_INVOICE_ID
2401   FROM   FA_MC_ADJUSTMENTS
2402   WHERE  TRANSACTION_HEADER_ID  = l_group_thid
2403   AND    ASSET_ID               = l_group_asset_id
2404   AND    BOOK_TYPE_CODE         = p_asset_hdr_rec.book_type_code
2405   AND    PERIOD_COUNTER_CREATED = l_period_rec.period_counter
2406   AND    ADJUSTMENT_TYPE in ('PROCEEDS CLR', 'REMOVALCOST CLR', 'NBV RETIRED')
2407   AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
2408 
2409   CURSOR c_pos_cor_adj IS
2410   SELECT TRANSACTION_HEADER_ID,
2411          SOURCE_TYPE_CODE,
2412          ADJUSTMENT_TYPE,
2413          DEBIT_CREDIT_FLAG,
2414          CODE_COMBINATION_ID,
2415          BOOK_TYPE_CODE,
2416          ASSET_ID,
2417          ADJUSTMENT_AMOUNT,
2418          DISTRIBUTION_ID,
2419          ANNUALIZED_ADJUSTMENT,
2420          PERIOD_COUNTER_ADJUSTED,
2421          PERIOD_COUNTER_CREATED,
2422          ASSET_INVOICE_ID
2423   FROM   FA_ADJUSTMENTS
2424   WHERE  TRANSACTION_HEADER_ID  = l_group_thid
2425   AND    ASSET_ID               = l_group_asset_id
2426   AND    BOOK_TYPE_CODE         = p_asset_hdr_rec.book_type_code
2427   AND    PERIOD_COUNTER_CREATED = l_period_rec.period_counter
2428   AND    ADJUSTMENT_TYPE in ('PROCEEDS CLR', 'REMOVALCOST CLR', 'NBV RETIRED');
2429 
2430 --kawa
2431 --  CURSOR c_get_ret_reserve
2432 
2433   l_ret_period_counter     NUMBER := to_number(null);
2434   l_adj                    FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT;
2435 
2436   l_asset_hdr_rec   FA_API_TYPES.asset_hdr_rec_type;
2437   l_asset_cat_rec   FA_API_TYPES.asset_cat_rec_type;
2438 
2439   l_trans_rec         FA_API_TYPES.trans_rec_type;
2440   l_asset_fin_rec_adj FA_API_TYPES.asset_fin_rec_type;
2441   l_asset_fin_rec_new FA_API_TYPES.asset_fin_rec_type;
2442   l_asset_deprn_rec   FA_API_TYPES.asset_deprn_rec_type;
2443 
2444   l_deprn_exp         NUMBER;
2445   l_bonus_deprn_exp   NUMBER;
2446   l_impairment_exp    NUMBER;
2447 
2448   calc_err         EXCEPTION;
2449 
2450 
2451 BEGIN
2452 
2453    if (p_log_level_rec.statement_level) then
2454       fa_debug_pkg.add(l_calling_fn, 'Begin', p_asset_hdr_rec.asset_id||':'||p_asset_fin_rec.group_asset_id, p_log_level_rec => p_log_level_rec);
2455    end if;
2456 
2457    -- BUG# 3641747
2458    -- call cache as it was not yet initialized
2459    if not fa_cache_pkg.fazccb(p_asset_hdr_rec.book_type_code,
2460                               p_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2461       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2462       raise calc_err;
2463    end if;
2464   /* bug#15897249 start */
2465    If ((nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
2466       nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) ) then
2467          if not MOVE_RESERVE_TO_ACT_DIST(
2468                         p_trans_rec         => p_trans_rec,
2469                         p_asset_hdr_rec     => p_asset_hdr_rec,
2470                         p_period_rec        => p_period_rec,
2471                         p_asset_retire_rec  => p_asset_retire_rec,
2472                         p_mrc_sob_type_code => p_mrc_sob_type_code,
2473                         p_log_level_rec     => p_log_level_rec) then
2474             raise calc_err;
2475          end if;
2476    End if;
2477   /* bug#15897249 end */
2478 
2479    --
2480    -- populate FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT(l_adj)to
2481    -- call FA_INS_ADJUST_PKG.faxinaj
2482    --
2483    l_adj.transaction_header_id   := p_trans_rec.transaction_header_id;
2484    l_adj.source_type_code        := 'RETIREMENT';
2485    l_adj.period_counter_created  := p_period_rec.period_counter;
2486    l_adj.period_counter_adjusted := p_period_rec.period_counter;
2487    l_adj.current_units           := p_asset_desc_rec.current_units;
2488    l_adj.book_type_code          := p_asset_hdr_rec.book_type_code;
2489    l_adj.selection_thid          := 0;
2490    l_adj.selection_retid         := 0;
2491    l_adj.leveling_flag           := TRUE;
2492    l_adj.last_update_date        := p_trans_rec.who_info.last_update_date;
2493 
2494    l_adj.flush_adj_flag          := FALSE;
2495    l_adj.gen_ccid_flag           := TRUE;
2496 
2497    l_adj.asset_id                := p_asset_hdr_rec.asset_id;
2498 
2499    l_adj.adjustment_type         := 'COST';
2500    l_adj.code_combination_id     := 0;
2501    l_adj.adjustment_amount       := p_asset_retire_rec.cost_retired;
2502    l_adj.mrc_sob_type_code       := p_mrc_sob_type_code;
2503    l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
2504 
2505    -- +++++ Process Cost entries +++++
2506    if p_asset_type_rec.asset_type = 'CIP' then
2507       l_adj.source_type_code := 'CIP RETIREMENT';
2508       l_adj.account          := fa_cache_pkg.fazccb_record.cip_cost_acct;
2509       l_adj.account_type     := 'CIP_COST_ACCT';
2510    else
2511       l_adj.source_type_code := 'RETIREMENT';
2512       l_adj.account          := fa_cache_pkg.fazccb_record.asset_cost_acct;
2513       l_adj.account_type     := 'ASSET_COST_ACCT';
2514    end if;
2515 
2516    if ((nvl(p_asset_retire_rec.units_retired, 0) > 0) and
2517        (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE')) and
2518       --(p_asset_desc_rec.current_units > p_asset_retire_rec.units_retired)
2519                 (G_PART_RET_FLAG = TRUE) then
2520       OPEN c_get_thid_for_dist;
2521       FETCH c_get_thid_for_dist INTO l_adj.selection_thid;
2522       CLOSE c_get_thid_for_dist;
2523 
2524       if (nvl(l_adj.selection_thid, 0) = 0) then
2525          raise calc_err;
2526       end if;
2527 
2528       l_adj.selection_mode    := FA_STD_TYPES.FA_AJ_CLEAR;
2529       l_adj.debit_credit_flag := 'CR';
2530    else
2531       l_adj.debit_credit_flag := 'DR';
2532       l_adj.selection_thid    := 0;
2533       l_adj.selection_mode    := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2534    end if;
2535 
2536 --tk_util.DumpAdjRec(l_adj, l_adj.debit_credit_flag||':'||to_char(nvl(p_asset_retire_rec.units_retired, 0)));
2537    if not FA_INS_ADJUST_PKG.faxinaj
2538                          (l_adj,
2539                           p_trans_rec.who_info.last_update_date,
2540                           p_trans_rec.who_info.last_updated_by,
2541                           p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2542       raise calc_err;
2543    end if;
2544 
2545    if ((nvl(p_asset_retire_rec.units_retired, 0) > 0) and
2546        (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE')) and
2547       --(p_asset_desc_rec.current_units > p_asset_retire_rec.units_retired)
2548                  (G_PART_RET_FLAG = TRUE) then
2549 
2550       --
2551       -- With unkonwn reason l_adj.account is set to null so
2552       -- need to resert the same value.
2553       --
2554       if p_asset_type_rec.asset_type = 'CIP' then
2555          l_adj.account := fa_cache_pkg.fazccb_record.cip_cost_acct;
2556       else
2557          l_adj.account := fa_cache_pkg.fazccb_record.asset_cost_acct;
2558       end if;
2559 
2560       l_adj.selection_thid    := 0;
2561       l_adj.selection_mode    := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2562       l_adj.current_units     := p_asset_desc_rec.current_units;
2563       l_adj.debit_credit_flag := 'DR';
2564       l_adj.leveling_flag     := FALSE;
2565       l_adj.adjustment_amount := l_adj.amount_inserted +
2566                                 p_asset_retire_rec.cost_retired;
2567 --tk_util.DumpAdjRec(l_adj, 'URDR');
2568       if not FA_INS_ADJUST_PKG.faxinaj
2569                              (l_adj,
2570                               p_trans_rec.who_info.last_update_date,
2571                               p_trans_rec.who_info.last_updated_by,
2572                               p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2573          raise calc_err;
2574       end if;
2575 
2576    end if; -- ((nvl(p_asset_retire_rec.units_retired, 0) > 0) and
2577 
2578 -- ENERGY
2579    if (not fa_cache_pkg.fazccmt(p_asset_fin_rec.deprn_method_code,
2580                                 p_asset_fin_rec.life_in_months, p_log_level_rec => p_log_level_rec)) then
2581       if (p_log_level_rec.statement_level) then
2582             fa_debug_pkg.add(l_calling_fn, 'Error calling', 'fa_cache_pkg.fazccmt', p_log_level_rec => p_log_level_rec);
2583       end if;
2584 
2585       raise calc_err;
2586    end if;
2587 
2588    if (p_log_level_rec.statement_level) then
2589       fa_debug_pkg.add(l_calling_fn, 'tracking_method',
2590                        p_asset_fin_rec.tracking_method, p_log_level_rec => p_log_level_rec);
2591       fa_debug_pkg.add(l_calling_fn, 'rule_name',
2592                        fa_cache_pkg.fazcdrd_record.rule_name, p_log_level_rec => p_log_level_rec);
2593    end if;
2594    -- Energy: Need to reinstate member reserve entry cre
2595    -- BUG# 6899255
2596    -- handle all allocate cases the same  rather than just energy
2597    --  (fa_cache_pkg.fazcdrd_record.rule_name = 'ENERGY PERIOD END BALANCE')
2598    -- ENERGY
2599 
2600    if (nvl(p_asset_fin_rec.tracking_method, 'NO TRACK') = 'ALLOCATE')
2601       or
2602       (p_asset_fin_rec.tracking_method = 'CALCULATE') and
2603          (nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'N'
2604        ) then
2605       if (p_log_level_rec.statement_level) then
2606          fa_debug_pkg.add(l_calling_fn, 'reserve retired',
2607                           p_asset_retire_rec.reserve_retired, p_log_level_rec => p_log_level_rec);
2608       end if;
2609 
2610   l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2611   l_adj.distribution_id         := 0;
2612 
2613   l_adj.adjustment_type         := 'RESERVE';
2614   l_adj.debit_credit_flag       := 'CR';
2615   l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
2616   l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
2617   l_adj.adjustment_amount       := p_asset_retire_rec.reserve_retired;
2618   l_adj.track_member_flag       := 'Y';
2619 
2620 --tk_util.DumpAdjRec(l_adj, 'mRSV');
2621   if not FA_INS_ADJUST_PKG.faxinaj
2622                          (l_adj,
2623                           p_trans_rec.who_info.last_update_date,
2624                           p_trans_rec.who_info.last_updated_by,
2625                           p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2626     raise calc_err;
2627   end if;
2628 
2629    end if; -- ENERGY
2630 
2631   --
2632   -- Process Reserve
2633   -- Prepare local variables with group information so category cache will
2634   -- hold group category.  This is because Reserve, POS, and COR entries are
2635   -- created with group info.
2636   --
2637   l_asset_hdr_rec.asset_id := p_asset_fin_rec.group_asset_id;
2638   l_asset_hdr_rec.book_type_code := p_asset_hdr_rec.book_type_code;
2639   l_asset_hdr_rec.set_of_books_id := p_asset_hdr_rec.set_of_books_id;
2640   -- l_asset_hdr_rec.period_of_addition := 'N';  -- Set this if necessary
2641 
2642   if not FA_UTIL_PVT.get_asset_cat_rec (
2643                          p_asset_hdr_rec  => l_asset_hdr_rec,
2644                          px_asset_cat_rec => l_asset_cat_rec,
2645                          p_date_effective  => NULL, p_log_level_rec => p_log_level_rec) then
2646     raise calc_err;
2647   end if;
2648 
2649   --
2650   -- Set category book cache w/ group information.
2651   --
2652   if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
2653                              l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2654     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2655     raise calc_err;
2656   end if;
2657 
2658   /* bug#15897249 added if condition */
2659   If (not (nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
2660       nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) ) then
2661 
2662      if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
2663                                 l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2664        fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2665        raise calc_err;
2666      end if;
2667 
2668      l_adj.asset_id                := p_asset_fin_rec.group_asset_id;
2669      l_adj.transaction_header_id   := p_group_thid;
2670   End if;
2671   /* bug#15897249 end */
2672   l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2673   l_adj.distribution_id         := 0;
2674 
2675   l_adj.adjustment_type         := 'RESERVE';
2676   l_adj.debit_credit_flag       := 'CR';
2677   l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
2678   l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
2679   l_adj.adjustment_amount       := p_asset_retire_rec.cost_retired;
2680   l_adj.track_member_flag       := null;
2681 
2682 --tk_util.DumpAdjRec(l_adj, 'RSV');
2683   if not FA_INS_ADJUST_PKG.faxinaj
2684                          (l_adj,
2685                           p_trans_rec.who_info.last_update_date,
2686                           p_trans_rec.who_info.last_updated_by,
2687                           p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2688     raise calc_err;
2689   end if;
2690 
2691   -- +++ Get Group's transaciton_header_id for retirement to be reinstated
2692   OPEN c_get_group_thid;
2693   FETCH c_get_group_thid INTO l_group_thid, l_group_asset_id, l_date_effective;
2694   CLOSE c_get_group_thid;
2695 
2696   if not FA_UTIL_PVT.get_period_rec(
2697                   p_book           => l_asset_hdr_rec.book_type_code,
2698                   p_effective_date => l_date_effective,
2699                   x_period_rec     => l_period_rec, p_log_level_rec => p_log_level_rec) then
2700      raise calc_err;
2701   end if;
2702 
2703   --
2704   -- Reinstate Proceeds of Sale and Cost of Removal and reserve entries for
2705   -- each of these.
2706   --
2707  /* bug#15897249 added if condition */
2708   If (nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
2709       nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) then
2710       l_group_asset_id := p_asset_hdr_rec.asset_id;
2711       l_group_thid := p_asset_retire_rec.detail_info.transaction_header_id_in;
2712   End if;
2713   /* bug#15897249 end */
2714   if (p_mrc_sob_type_code <> 'R') then
2715      for r_pos_cor_adj in c_pos_cor_adj loop
2716         if (r_pos_cor_adj.debit_credit_flag = 'CR') then
2717            l_adj.debit_credit_flag   := 'DR';
2718         else
2719            l_adj.debit_credit_flag   := 'CR';
2720         end if;
2721 
2722         l_adj.adjustment_type     := r_pos_cor_adj.adjustment_type;
2723         l_adj.code_combination_id := r_pos_cor_adj.code_combination_id;
2724         l_adj.asset_id            := r_pos_cor_adj.asset_id;
2725         l_adj.adjustment_amount   := r_pos_cor_adj.adjustment_amount;
2726         l_adj.distribution_id     := r_pos_cor_adj.distribution_id;
2727 --tk_util.DumpAdjRec(l_adj, r_pos_cor_adj.adjustment_type);
2728         if not FA_INS_ADJUST_PKG.faxinaj
2729                                  (l_adj,
2730                                   p_trans_rec.who_info.last_update_date,
2731                                   p_trans_rec.who_info.last_updated_by,
2732                                   p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2733            raise calc_err;
2734         end if;
2735 
2736         --
2737         -- Set category book cache w/ group information.
2738         --
2739         if (l_asset_cat_rec.category_id is null) then
2740            if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
2741                                       l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2742               fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2743               raise calc_err;
2744            end if;
2745         end if;
2746 
2747       /* bug#15897249 added if condition */
2748         If (not (nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
2749             nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) ) then
2750            --
2751            -- Set category book cache w/ group information.
2752            --
2753            if (l_asset_cat_rec.category_id is null) then
2754               if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
2755                                          l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2756                  fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2757                  raise calc_err;
2758               end if;
2759            end if;
2760 
2761             l_adj.asset_id                := p_asset_fin_rec.group_asset_id;
2762         End if;
2763         /* bug#15897249 end */
2764         l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2765         l_adj.distribution_id         := 0;
2766         l_adj.adjustment_type         := 'RESERVE';
2767 
2768         if (l_adj.debit_credit_flag = 'CR') then
2769            l_adj.debit_credit_flag       := 'DR';
2770         else
2771            l_adj.debit_credit_flag       := 'CR';
2772         end if;
2773 
2774         l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
2775         l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
2776 
2777 --tk_util.DumpAdjRec(l_adj, 'RSV');
2778         if not FA_INS_ADJUST_PKG.faxinaj
2779                                       (l_adj,
2780                                        p_trans_rec.who_info.last_update_date,
2781                                        p_trans_rec.who_info.last_updated_by,
2782                                        p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2783            raise calc_err;
2784         end if;
2785 
2786      end loop;
2787   else -- Reporting Book
2788      for r_pos_cor_adj in c_mc_pos_cor_adj loop
2789         if (r_pos_cor_adj.debit_credit_flag = 'CR') then
2790            l_adj.debit_credit_flag   := 'DR';
2791         else
2792            l_adj.debit_credit_flag   := 'CR';
2793         end if;
2794 
2795         l_adj.adjustment_type     := r_pos_cor_adj.adjustment_type;
2796         l_adj.code_combination_id := r_pos_cor_adj.code_combination_id;
2797         l_adj.asset_id            := r_pos_cor_adj.asset_id;
2798         l_adj.adjustment_amount   := r_pos_cor_adj.adjustment_amount;
2799         l_adj.distribution_id     := r_pos_cor_adj.distribution_id;
2800 
2801         if not FA_INS_ADJUST_PKG.faxinaj
2802                                  (l_adj,
2803                                   p_trans_rec.who_info.last_update_date,
2804                                   p_trans_rec.who_info.last_updated_by,
2805                                   p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2806            raise calc_err;
2807         end if;
2808 
2809         --
2810         -- Set category book cache w/ group information.
2811         --
2812         if (l_asset_cat_rec.category_id is null) then
2813            if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
2814                                       l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2815               fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2816               raise calc_err;
2817            end if;
2818         end if;
2819 
2820       /* bug#15897249 added if condition */
2821         If (not (nvl(p_asset_fin_rec.tracking_method,'NULL') = 'CALCULATE' and
2822             nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'Y' ) ) then
2823            --
2824            -- Set category book cache w/ group information.
2825            --
2826            if (l_asset_cat_rec.category_id is null) then
2827               if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
2828                                          l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2829                  fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2830                  raise calc_err;
2831               end if;
2832            end if;
2833 
2834            l_adj.asset_id                := p_asset_fin_rec.group_asset_id;
2835         End if;
2836         /* bug#15897249 end */
2837         l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2838         l_adj.distribution_id         := 0;
2839         l_adj.adjustment_type         := 'RESERVE';
2840 
2841         if (l_adj.debit_credit_flag = 'CR') then
2842            l_adj.debit_credit_flag       := 'DR';
2843         else
2844            l_adj.debit_credit_flag       := 'CR';
2845         end if;
2846 
2847         l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
2848         l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
2849 
2850 --tk_util.DumpAdjRec(l_adj, 'RSV');
2851         if not FA_INS_ADJUST_PKG.faxinaj
2852                                       (l_adj,
2853                                        p_trans_rec.who_info.last_update_date,
2854                                        p_trans_rec.who_info.last_updated_by,
2855                                        p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2856            raise calc_err;
2857         end if;
2858 
2859      end loop;
2860 
2861   end if; --(p_mrc_sob_type_code <> 'R')
2862 
2863   --
2864   -- Flash adjustments cache.
2865   --
2866   l_adj.flush_adj_flag          := TRUE;
2867   l_adj.transaction_header_id   := 0;
2868   l_adj.mrc_sob_type_code       :=  p_mrc_sob_type_code;
2869   l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
2870 
2871 
2872   if not FA_INS_ADJUST_PKG.faxinaj
2873                              (l_adj,
2874                               p_trans_rec.who_info.last_update_date,
2875                               p_trans_rec.who_info.last_updated_by,
2876                               p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
2877      raise calc_err;
2878   end if;
2879 
2880   l_trans_rec := p_trans_rec;
2881 
2882   if not fa_util_pvt.get_asset_deprn_rec (
2883                 p_asset_hdr_rec         => p_asset_hdr_rec,
2884                 px_asset_deprn_rec      => l_asset_deprn_rec,
2885                 p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
2886      raise calc_err;
2887   end if;
2888 
2889   l_asset_fin_rec_adj.cost := p_asset_retire_rec.cost_retired;
2890   l_asset_fin_rec_adj.unrevalued_cost := p_asset_retire_rec.cost_retired;
2891 --tk_util.debug('recovering cost : '||to_char(l_asset_fin_rec_adj.cost));
2892 
2893   --Bug 8527733
2894   if (p_asset_fin_rec.salvage_type='AMT') then
2895      if (p_mrc_sob_type_code = 'R') then
2896         OPEN c_get_ret_salvage_mrc;
2897         FETCH c_get_ret_salvage_mrc
2898         INTO l_asset_fin_rec_adj.salvage_value;
2899         CLOSE c_get_ret_salvage_mrc;
2900      else
2901         OPEN c_get_ret_salvage;
2902         FETCH c_get_ret_salvage
2903         INTO l_asset_fin_rec_adj.salvage_value;
2904         CLOSE c_get_ret_salvage;
2905      end if;
2906   end if;
2907 
2908   if (p_log_level_rec.statement_level) then
2909      fa_debug_pkg.add(l_calling_fn, 'l_asset_deprn_rec.deprn_reserve', l_asset_deprn_rec.deprn_reserve, p_log_level_rec => p_log_level_rec);
2910   end if;
2911 
2912   if not FA_AMORT_PVT.faxama
2913                         (px_trans_rec          => l_trans_rec,
2914                          p_asset_hdr_rec       => p_asset_hdr_rec,
2915                          p_asset_desc_rec      => p_asset_desc_rec,
2916                          p_asset_cat_rec       => p_asset_cat_rec,
2917                          p_asset_type_rec      => p_asset_type_rec,
2918                          p_asset_fin_rec_old   => p_asset_fin_rec,
2919                          p_asset_fin_rec_adj   => l_asset_fin_rec_adj,
2920                          px_asset_fin_rec_new  => l_asset_fin_rec_new,
2921                          p_asset_deprn_rec     => l_asset_deprn_rec,
2922                          p_period_rec          => p_period_rec,
2923                          p_mrc_sob_type_code   => p_mrc_sob_type_code,
2924                          p_running_mode        => fa_std_types.FA_DPR_NORMAL,
2925                          p_used_by_revaluation => null,
2926                          x_deprn_exp           => l_deprn_exp,
2927                          x_bonus_deprn_exp     => l_bonus_deprn_exp,
2928                          x_impairment_exp      => l_impairment_exp, p_log_level_rec => p_log_level_rec) then
2929      raise calc_err;
2930   end if;
2931 
2932   x_asset_fin_rec := l_asset_fin_rec_new;
2933 --tk_util.debug('new cost : '||to_char(x_asset_fin_rec.cost));
2934 --tk_util.debug('new adj cost : '||to_char(x_asset_fin_rec.adjusted_cost));
2935 
2936   if (p_log_level_rec.statement_level) then
2937      fa_debug_pkg.add(l_calling_fn, 'l_deprn_exp', l_deprn_exp, p_log_level_rec => p_log_level_rec);
2938      fa_debug_pkg.add(l_calling_fn, 'p_asset_fin_rec.tracking_method', p_asset_fin_rec.tracking_method, p_log_level_rec => p_log_level_rec);
2939      fa_debug_pkg.add(l_calling_fn, 'p_period_rec.calendar_period_open_date', p_period_rec.calendar_period_open_date,
2940                       p_log_level_rec => p_log_level_rec);
2941      fa_debug_pkg.add(l_calling_fn, 'p_trans_rec.transaction_date_entered', p_trans_rec.transaction_date_entered,
2942                       p_log_level_rec => p_log_level_rec);
2943   end if;
2944 
2945   --
2946   -- Calculating catchup only if tracking method is "CALCULATE".
2947   -- This is only necessary if this is prior period retirement.
2948   -- For now member asset prior period retirement is restricted.
2949   --
2950   if (p_asset_fin_rec.tracking_method = 'CALCULATE') then
2951 
2952      if (nvl(l_deprn_exp, 0) <> 0) or
2953         (nvl(l_bonus_deprn_exp, 0) <> 0) or
2954         (nvl(l_impairment_exp, 0) <> 0) then
2955 
2956         l_adj.transaction_header_id   := p_trans_rec.transaction_header_id;
2957         l_adj.book_type_code          := p_asset_hdr_rec.book_type_code;
2958         l_adj.period_counter_created  := p_period_rec.period_counter;
2959         l_adj.period_counter_adjusted := p_period_rec.period_counter;
2960         l_adj.current_units           := p_asset_desc_rec.current_units;
2961         l_adj.selection_retid         := 0;
2962         l_adj.leveling_flag           := TRUE;
2963         l_adj.last_update_date        := p_trans_rec.who_info.last_update_date;
2964         l_adj.gen_ccid_flag           := TRUE;
2965         l_adj.flush_adj_flag          := FALSE;
2966 
2967         if (nvl(l_deprn_exp, 0) <> 0) then
2968            -- Catchup expense for Member
2969            -- Call category book cache for group to get expense account
2970 
2971            if not fa_cache_pkg.fazccb
2972                      (X_book   => p_asset_hdr_rec.book_type_code,
2973                       X_cat_id => p_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
2974               raise calc_err;
2975            end if;
2976 
2977            l_adj.source_type_code        := 'DEPRECIATION';
2978            l_adj.adjustment_type         := 'EXPENSE';
2979            l_adj.account                 := fa_cache_pkg.fazccb_record.deprn_expense_acct;
2980            l_adj.account_type            := 'DEPRN_EXPENSE_ACCT';
2981            l_adj.asset_id                := p_asset_hdr_rec.asset_id;
2982            l_adj.adjustment_amount       := abs(l_deprn_exp);
2983            l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
2984            l_adj.mrc_sob_type_code       :=  p_mrc_sob_type_code;
2985            l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
2986 
2987            if (nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'N') then
2988               l_adj.track_member_flag       := 'Y';
2989            else
2990               l_adj.track_member_flag       := null;
2991            end if;
2992 
2993            if (nvl(l_bonus_deprn_exp, 0) = 0) then
2994               l_adj.flush_adj_flag          := TRUE;
2995            end if;
2996 
2997            if (nvl(l_impairment_exp, 0) = 0) then
2998               l_adj.flush_adj_flag          := TRUE;
2999            end if;
3000 
3001            if (l_deprn_exp > 0) then
3002               l_adj.debit_credit_flag     := 'DR';
3003            else
3004               l_adj.debit_credit_flag     := 'CR';
3005            end if;
3006 
3007            if not FA_INS_ADJUST_PKG.faxinaj
3008                                    (l_adj,
3009                                     p_trans_rec.who_info.last_update_date,
3010                                     p_trans_rec.who_info.last_updated_by,
3011                                     p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
3012               raise calc_err;
3013            end if;
3014         end if; --(nvl(l_deprn_exp, 0) <> 0)
3015 
3016         if (nvl(l_bonus_deprn_exp, 0) <> 0) then
3017            -- Catchup bonus expense for Member
3018            -- Need to call cache function if it is not called for expense.
3019 
3020            if (nvl(l_deprn_exp, 0) = 0) then
3021               if not fa_cache_pkg.fazccb
3022                       (X_book   => p_asset_hdr_rec.book_type_code,
3023                        X_cat_id => p_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
3024                  raise calc_err;
3025               end if;
3026            end if;
3027 
3028            l_adj.source_type_code        := 'DEPRECIATION';
3029            l_adj.adjustment_type         := 'BONUS EXPENSE';
3030            l_adj.account                 := fa_cache_pkg.fazccb_record.bonus_deprn_expense_acct;
3031            l_adj.account_type            := 'BONUS_DEPRN_EXPENSE_ACCT';
3032            l_adj.asset_id                := p_asset_hdr_rec.asset_id;
3033            l_adj.adjustment_amount       := abs(l_deprn_exp);
3034            l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
3035            l_adj.mrc_sob_type_code       :=  p_mrc_sob_type_code;
3036            l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
3037 
3038            if (nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'N') then
3039               l_adj.track_member_flag       := 'Y';
3040            else
3041               l_adj.track_member_flag       := null;
3042            end if;
3043            l_adj.flush_adj_flag          := TRUE;
3044 
3045            if (l_deprn_exp > 0) then
3046               l_adj.debit_credit_flag     := 'CR';
3047            else
3048               l_adj.debit_credit_flag     := 'DR';
3049            end if;
3050 
3051            if not FA_INS_ADJUST_PKG.faxinaj
3052                                    (l_adj,
3053                                     p_trans_rec.who_info.last_update_date,
3054                                     p_trans_rec.who_info.last_updated_by,
3055                                     p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
3056               raise calc_err;
3057            end if;
3058         end if; -- (nvl(l_bonus_deprn_exp, 0) <> 0)
3059 
3060         if (nvl(l_impairment_exp, 0) <> 0) then
3061            -- Catchup impairment expense for Member
3062            -- Need to call cache function if it is not called for expense.
3063 
3064            if (nvl(l_deprn_exp, 0) = 0) then
3065               if not fa_cache_pkg.fazccb
3066                       (X_book   => p_asset_hdr_rec.book_type_code,
3067                        X_cat_id => p_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
3068                  raise calc_err;
3069               end if;
3070            end if;
3071 
3072            l_adj.source_type_code        := 'DEPRECIATION';
3073            l_adj.adjustment_type         := 'IMPAIR EXPENSE';
3074            l_adj.account                 := fa_cache_pkg.fazccb_record.impair_expense_acct;
3075            l_adj.account_type            := 'IMPAIR_EXPENSE_ACCT';
3076            l_adj.asset_id                := p_asset_hdr_rec.asset_id;
3077            l_adj.adjustment_amount       := abs(l_impairment_exp);
3078            l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
3079            l_adj.mrc_sob_type_code       :=  p_mrc_sob_type_code;
3080            l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
3081 
3082            if (nvl(p_asset_fin_rec.member_rollup_flag, 'N') = 'N') then
3083               l_adj.track_member_flag       := 'Y';
3084            else
3085               l_adj.track_member_flag       := null;
3086            end if;
3087            l_adj.flush_adj_flag          := TRUE;
3088 
3089            if (l_impairment_exp > 0) then
3090               l_adj.debit_credit_flag     := 'CR';
3091            else
3092               l_adj.debit_credit_flag     := 'DR';
3093            end if;
3094 
3095            if not FA_INS_ADJUST_PKG.faxinaj
3096                                    (l_adj,
3097                                     p_trans_rec.who_info.last_update_date,
3098                                     p_trans_rec.who_info.last_updated_by,
3099                                     p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
3100               raise calc_err;
3101            end if;
3102         end if; -- (nvl(l_impairment_exp, 0) <> 0)
3103 
3104      end if; -- (nvl(l_deprn_exp, 0) <> 0) or
3105 
3106   end if; -- (p_asset_fin_rec.tracking_method = 'CALCULATE')
3107 
3108   if (p_log_level_rec.statement_level) then
3109      fa_debug_pkg.add(l_calling_fn, 'End', 'Success', p_log_level_rec => p_log_level_rec);
3110   end if;
3111 
3112   return true;
3113 
3114 EXCEPTION
3115   when calc_err then
3116     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3117 
3118     return false;
3119 
3120   when OTHERS then
3121     fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3122 
3123     return false;
3124 
3125 END CALC_GAIN_LOSS_FOR_REI;
3126 
3127 FUNCTION Do_Retirement_in_CGL(
3128                       p_ret                 IN fa_ret_types.ret_struct,
3129                       p_bk                  IN fa_ret_types.book_struct,
3130                       p_dpr                 IN fa_STD_TYPES.dpr_struct,
3131                       p_asset_deprn_rec_old IN FA_API_TYPES.asset_deprn_rec_type,
3132                       p_mrc_sob_type_code   IN VARCHAR2,
3133                       p_calling_fn          IN VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
3134 return boolean IS
3135 
3136   CURSOR c_get_thid is
3137     select fa_transaction_headers_s.nextval
3138     from   dual;
3139 
3140   CURSOR c_get_thx is
3141     select trx.transaction_date_entered
3142          , trx.last_update_date
3143          , trx.last_updated_by
3144          , trx.last_update_login
3145          , trx.mass_transaction_id
3146          , trx.calling_interface
3147          , trx.mass_reference_id
3148          , nvl(trx.amortization_start_date, trx.transaction_date_entered)
3149          , trx.event_id
3150     from   fa_transaction_headers trx
3151     where  trx.transaction_header_id = p_ret.th_id_in;
3152 
3153   CURSOR c_member_exists IS
3154     select 'Y'
3155     from   fa_books
3156     where  book_type_code = p_ret.book
3157     and    group_asset_id = p_bk.group_asset_id
3158     and    ((period_counter_fully_retired is null)
3159         or  (period_counter_fully_retired is not null
3160          and retirement_pending_flag = 'YES'))
3161     and    transaction_header_id_out is null;
3162 
3163   CURSOR c_get_ret_info IS
3164     select -1 * eofy_reserve
3165     from   fa_retirements
3166     where retirement_id = p_ret.retirement_id;
3167 
3168   CURSOR c_get_mc_ret_info IS
3169     select -1 * eofy_reserve
3170     from   fa_mc_retirements
3171     where retirement_id = p_ret.retirement_id
3172     and set_of_books_id = p_ret.set_of_books_id;
3173 
3174   l_calling_fn            VARCHAR2(100) := 'fa_retirement_pvt.Do_Retirement_in_CGL';
3175 
3176   l_trans_rec             FA_API_TYPES.trans_rec_type;
3177   l_asset_hdr_rec         FA_API_TYPES.asset_hdr_rec_type;
3178   l_asset_hdr_rec_m       FA_API_TYPES.asset_hdr_rec_type;
3179   l_asset_desc_rec        FA_API_TYPES.asset_desc_rec_type;
3180   l_asset_type_rec        FA_API_TYPES.asset_type_rec_type;
3181   l_asset_cat_rec         FA_API_TYPES.asset_cat_rec_type;
3182   l_asset_fin_rec_new_m   FA_API_TYPES.asset_fin_rec_type;
3183   l_asset_fin_rec_old     FA_API_TYPES.asset_fin_rec_type;
3184   l_asset_fin_rec_adj     FA_API_TYPES.asset_fin_rec_type;
3185   l_asset_fin_rec_new     FA_API_TYPES.asset_fin_rec_type;
3186   l_inv_trans_rec         FA_API_TYPES.inv_trans_rec_type;
3187   l_asset_deprn_rec_adj   FA_API_TYPES.asset_deprn_rec_type;
3188   l_asset_deprn_rec_new   FA_API_TYPES.asset_deprn_rec_type;
3189   l_period_rec            FA_API_TYPES.period_rec_type;
3190   l_group_reclass_options_rec FA_API_TYPES.group_reclass_options_rec_type;
3191 
3192   l_member_exists         VARCHAR2(1) := 'N';
3193   l_deprn_reserve         NUMBER;
3194   l_temp_num              NUMBER;
3195   l_temp_char             VARCHAR2(30);
3196   l_temp_bool             BOOLEAN;
3197 
3198   l_new_reserve           NUMBER;
3199   l_new_rec_cost          NUMBER;
3200   l_recapture_amount      NUMBER;
3201 
3202   l_adj                    FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT;
3203 
3204   l_rowid                 ROWID; -- temp variable
3205 
3206   l_cost_sign             NUMBER; --Bug 8535921
3207 
3208   calc_err    EXCEPTION;
3209 
3210 BEGIN
3211    if (p_log_level_rec.statement_level) then
3212       fa_debug_pkg.add(l_calling_fn, 'Begin',
3213                        p_ret.asset_id||':'||p_bk.group_asset_id, p_log_level_rec => p_log_level_rec);
3214    end if;
3215 
3216    /* Added for bug 8535921 */
3217    if p_ret.cost_retired < 0 then
3218       l_cost_sign := -1;
3219    else
3220       l_cost_sign := 1;
3221    end if;
3222    /* End of bug 8535921 */
3223 
3224    --
3225    -- Prepare to call FA_ADJUSTMENT_PVT.do_adjustment to process group
3226    -- asset after member asset retirement.
3227    --
3228    if (p_mrc_sob_type_code <> 'R') then
3229       OPEN c_get_thid;
3230       FETCH c_get_thid INTO l_trans_rec.transaction_header_id;
3231       CLOSE c_get_thid;
3232 
3233       -- Bug 8674833: Save the grp_trx_hdr_id for mrc loop
3234       g_grp_trx_hdr_id := l_trans_rec.transaction_header_id;
3235    else
3236       -- Bug 8674833: Get the grp_trx_hdr_id from the global
3237       l_trans_rec.transaction_header_id := g_grp_trx_hdr_id;
3238    end if;
3239 
3240 --tk_util.debug('l_trans_rec.transaction_header_id: '||to_char(l_trans_rec.transaction_header_id));
3241 
3242    l_trans_rec.transaction_type_code := 'GROUP ADJUSTMENT';
3243    l_trans_rec.transaction_subtype := 'AMORTIZED';
3244    l_trans_rec.member_transaction_header_id := p_ret.th_id_in;
3245    l_trans_rec.transaction_key := 'MR';
3246 
3247    OPEN c_get_thx;
3248    FETCH c_get_thx INTO l_trans_rec.transaction_date_entered
3249                       , l_trans_rec.who_info.last_update_date
3250                       , l_trans_rec.who_info.last_updated_by
3251                       , l_trans_rec.who_info.last_update_login
3252                       , l_trans_rec.mass_transaction_id
3253                       , l_trans_rec.calling_interface
3254                       , l_trans_rec.mass_reference_id
3255                       , l_trans_rec.amortization_start_date
3256                       , l_trans_rec.event_id;
3257    CLOSE c_get_thx;
3258 
3259    if (l_trans_rec.transaction_date_entered is null) then
3260       if (p_log_level_rec.statement_level) then
3261          fa_debug_pkg.add(l_calling_fn, 'Looking for retirement transaction', 'FAILED', p_log_level_rec => p_log_level_rec);
3262 
3263       end if;
3264 
3265       raise calc_err;
3266    end if;
3267 --tk_util.DumpTrxRec(l_trans_rec, 'trx');
3268 
3269    l_trans_rec.who_info.creation_date := l_trans_rec.who_info.last_update_date;
3270    l_trans_rec.who_info.created_by := l_trans_rec.who_info.last_updated_by;
3271 
3272    l_asset_hdr_rec.asset_id := p_bk.group_asset_id;
3273    l_asset_hdr_rec.book_type_code := p_ret.book;
3274 
3275    -- Bug 8674833 : Populate sob_id from p_ret populated in Do_Calc_GainLoss_Asset
3276    l_asset_hdr_rec.set_of_books_id := p_ret.set_of_books_id;
3277 
3278    if not FA_UTIL_PVT.get_asset_type_rec (
3279                    p_asset_hdr_rec      => l_asset_hdr_rec,
3280                    px_asset_type_rec    => l_asset_type_rec, p_log_level_rec => p_log_level_rec) then
3281       raise calc_err;
3282    end if;
3283 
3284    if not FA_UTIL_PVT.get_asset_desc_rec (
3285                    p_asset_hdr_rec         => l_asset_hdr_rec,
3286                    px_asset_desc_rec       => l_asset_desc_rec, p_log_level_rec => p_log_level_rec) then
3287       raise calc_err;
3288    end if;
3289 
3290    if not FA_UTIL_PVT.get_asset_cat_rec (
3291                    p_asset_hdr_rec         => l_asset_hdr_rec,
3292                    px_asset_cat_rec        => l_asset_cat_rec,
3293                    p_date_effective        => null, p_log_level_rec => p_log_level_rec) then
3294       raise calc_err;
3295    end if;
3296 
3297    if not fa_util_pvt.get_asset_fin_rec (
3298                    p_asset_hdr_rec         => l_asset_hdr_rec,
3299                    px_asset_fin_rec        => l_asset_fin_rec_old,
3300                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
3301       raise calc_err;
3302    end if;
3303 
3304    if not FA_UTIL_PVT.get_period_rec(
3305                    p_book           => l_asset_hdr_rec.book_type_code,
3306                    x_period_rec     => l_period_rec, p_log_level_rec => p_log_level_rec) then
3307       raise calc_err;
3308    end if;
3309 
3310 --tk_util.DumpFinRec(l_asset_fin_rec_old, 'OLD');
3311 
3312    if (l_asset_type_rec.asset_type = 'CIP') then
3313       l_asset_fin_rec_adj.cip_cost := -1 * p_ret.cost_retired;
3314    else
3315       l_asset_fin_rec_adj.cost := -1 * p_ret.cost_retired;
3316    end if;
3317 
3318    l_asset_fin_rec_adj.unrevalued_cost := -1 * p_ret.cost_retired;
3319    l_asset_fin_rec_adj.ytd_proceeds := p_ret.proceeds_of_sale;
3320    l_asset_fin_rec_adj.ltd_proceeds := p_ret.proceeds_of_sale;
3321    l_asset_fin_rec_adj.ltd_cost_of_removal := p_ret.cost_of_removal;
3322 
3323    --  This seems not necessary since the amount is in old_deprn_rec
3324    --Bug11886090: passing group portion of reserve retired
3325    l_asset_deprn_rec_adj.deprn_reserve := -1 * p_asset_deprn_rec_old.deprn_reserve * (p_ret.cost_retired/l_asset_fin_rec_old.cost);
3326 
3327    if not FA_UTILS_PKG.faxrnd(l_asset_deprn_rec_adj.deprn_reserve, p_ret.book,
3328                               p_ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
3329       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3330       raise calc_err;
3331    end if;
3332 
3333    if (p_log_level_rec.statement_level) then
3334       fa_debug_pkg.add(l_calling_fn, 'reserve retired for group',
3335                              l_asset_deprn_rec_adj.deprn_reserve, p_log_level_rec => p_log_level_rec);
3336    end if;
3337 
3338    if (p_mrc_sob_type_code = 'R') then
3339       OPEN c_get_mc_ret_info;
3340       FETCH c_get_mc_ret_info INTO  l_asset_fin_rec_adj.eofy_reserve;
3341       CLOSE c_get_mc_ret_info;
3342    else
3343       OPEN c_get_ret_info;
3344       FETCH c_get_ret_info INTO l_asset_fin_rec_adj.eofy_reserve;
3345       CLOSE c_get_ret_info;
3346    end if;
3347 
3348    l_asset_hdr_rec_m.asset_id := p_ret.asset_id;
3349    l_asset_hdr_rec_m.book_type_code := p_ret.book;
3350 
3351    -- Bug 8674833 : Populate sob_id from p_ret populated in Do_Calc_GainLoss_Asset
3352    l_asset_hdr_rec_m.set_of_books_id := p_ret.set_of_books_id;
3353 
3354    -- Get new member's fin_rec
3355    if not fa_util_pvt.get_asset_fin_rec (
3356                    p_asset_hdr_rec         => l_asset_hdr_rec_m,
3357                    px_asset_fin_rec        => l_asset_fin_rec_new_m,
3358                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
3359       raise calc_err;
3360    end if;
3361 
3362    l_asset_fin_rec_adj.salvage_value := nvl(l_asset_fin_rec_new_m.salvage_value, 0) -
3363                                         nvl(p_bk.salvage_value, 0);
3364 
3365    l_asset_fin_rec_adj.allowed_deprn_limit_amount :=
3366                                         nvl(l_asset_fin_rec_new_m.allowed_deprn_limit_amount, 0) -
3367                                         nvl(p_bk.allowed_deprn_limit_amount, 0);
3368 
3369    -- ** Code to handle recapture excess reserve has been moved to fa_gainloss_ret_pkg.faggrp
3370 
3371 
3372    if (p_log_level_rec.statement_level) then
3373       fa_debug_pkg.add(l_calling_fn, 'Call FA_ADJUSTMENT_PVT.do_adjustment3',
3374                        'Begin',  p_log_level_rec => p_log_level_rec);
3375    end if;
3376 
3377    if not FA_ADJUSTMENT_PVT.do_adjustment
3378                   (px_trans_rec              => l_trans_rec,
3379                    px_asset_hdr_rec          => l_asset_hdr_rec,
3380                    p_asset_desc_rec          => l_asset_desc_rec,
3381                    p_asset_type_rec          => l_asset_type_rec,
3382                    p_asset_cat_rec           => l_asset_cat_rec,
3383                    p_asset_fin_rec_old       => l_asset_fin_rec_old,
3384                    p_asset_fin_rec_adj       => l_asset_fin_rec_adj,
3385                    x_asset_fin_rec_new       => l_asset_fin_rec_new,
3386                    p_inv_trans_rec           => l_inv_trans_rec,
3387                    p_asset_deprn_rec_old     => p_asset_deprn_rec_old,
3388                    p_asset_deprn_rec_adj     => l_asset_deprn_rec_adj,
3389                    x_asset_deprn_rec_new     => l_asset_deprn_rec_new,
3390                    p_period_rec              => l_period_rec,
3391                    p_mrc_sob_type_code       => p_mrc_sob_type_code,
3392                    p_group_reclass_options_rec => l_group_reclass_options_rec,
3393                    p_calling_fn              => l_calling_fn, p_log_level_rec => p_log_level_rec)then
3394 
3395       if (p_log_level_rec.statement_level) then
3396          fa_debug_pkg.add(l_calling_fn, 'Call FA_ADJUSTMENT_PVT.do_adjustment',
3397                           'Failed',  p_log_level_rec => p_log_level_rec);
3398       end if;
3399 
3400       raise calc_err;
3401 
3402    end if;
3403 
3404    if (nvl(l_asset_fin_rec_old.member_rollup_flag, 'N') = 'Y') then
3405       --
3406       -- This is Sum up member depreciation to group asset
3407       --
3408       if (p_log_level_rec.statement_level) then
3409          fa_debug_pkg.add(l_calling_fn, 'Process Member Rollup Group',
3410                           l_asset_hdr_rec.asset_id, p_log_level_rec => p_log_level_rec);
3411       end if;
3412    end if; -- (nvl(l_asset_fin_rec_old.member_rollup_flag, 'N') = 'N')
3413 
3414    if (nvl(l_asset_fin_rec_old.terminal_gain_loss, 'NO') <> 'NO' ) and
3415       (nvl(l_asset_fin_rec_new.cost, 0) = 0) then
3416 
3417       OPEN c_member_exists;
3418       FETCH c_member_exists INTO l_member_exists;
3419       CLOSE c_member_exists;
3420 
3421       if (p_log_level_rec.statement_level) then
3422          fa_debug_pkg.add(l_calling_fn, 'Is there a member asset',
3423                           l_member_exists, p_log_level_rec => p_log_level_rec);
3424       end if;
3425 
3426       if (nvl(l_member_exists, 'N') <> 'Y') then
3427 
3428          if (nvl(l_asset_fin_rec_old.terminal_gain_loss, 'NO') = 'END_OF_YEAR') then
3429 
3430             FA_BOOKS_PKG.Update_Row(
3431                    X_Book_Type_Code               => l_asset_hdr_rec.book_type_code,
3432                    X_Asset_Id                     => l_asset_hdr_rec.asset_id,
3433                    X_terminal_gain_loss_flag      => 'Y',
3434                    X_mrc_sob_type_code            => p_mrc_sob_type_code,
3435                    X_set_of_books_id              => p_ret.set_of_books_id,
3436                    X_Calling_Fn                   => l_calling_fn, p_log_level_rec => p_log_level_rec);
3437 
3438          else
3439 
3440             if (p_log_level_rec.statement_level) then
3441                fa_debug_pkg.add(l_calling_fn, 'Call',
3442                                 'fa_query_balances_pkg.query_balances', p_log_level_rec => p_log_level_rec);
3443             end if;
3444 
3445             fa_query_balances_pkg.query_balances(
3446                       X_asset_id => l_asset_hdr_rec.asset_id,
3447                       X_book => l_asset_hdr_rec.book_type_code,
3448                       X_period_ctr => 0,
3449                       X_dist_id => 0,
3450                       X_run_mode => 'STANDARD',
3451                       X_cost => l_temp_num,
3452                       X_deprn_rsv => l_deprn_reserve,
3453                       X_reval_rsv => l_temp_num,
3454                       X_ytd_deprn => l_temp_num,
3455                       X_ytd_reval_exp => l_temp_num,
3456                       X_reval_deprn_exp => l_temp_num,
3457                       X_deprn_exp => l_temp_num,
3458                       X_reval_amo => l_temp_num,
3459                       X_prod => l_temp_num,
3460                       X_ytd_prod => l_temp_num,
3461                       X_ltd_prod => l_temp_num,
3462                       X_adj_cost => l_temp_num,
3463                       X_reval_amo_basis => l_temp_num,
3464                       X_bonus_rate => l_temp_num,
3465                       X_deprn_source_code => l_temp_char,
3466                       X_adjusted_flag => l_temp_bool,
3467                       X_transaction_header_id => -1,
3468                       X_bonus_deprn_rsv => l_temp_num,
3469                       X_bonus_ytd_deprn => l_temp_num,
3470                       X_bonus_deprn_amount => l_temp_num,
3471                       X_impairment_rsv => l_temp_num,
3472                       X_ytd_impairment => l_temp_num,
3473                       X_impairment_amount => l_temp_num,
3474                       X_capital_adjustment => l_temp_num,
3475                       X_general_fund => l_temp_num,
3476                       X_mrc_sob_type_code => p_mrc_sob_type_code,
3477                       X_set_of_books_id => l_asset_hdr_rec.set_of_books_id,
3478                       p_log_level_rec => p_log_level_rec);
3479 
3480             if (p_log_level_rec.statement_level) then
3481                fa_debug_pkg.add(l_calling_fn, 'l_deprn_reserve',
3482                                 l_deprn_reserve, p_log_level_rec => p_log_level_rec);
3483             end if;
3484 
3485             -- If remaining resrve is not 0, insert NBV RETIRED GAIN/LOSS,
3486             -- and RESERVE to fa_adjustments table.
3487             -- update terminal gain loss amount in fa_books
3488             if (nvl(l_deprn_reserve, 0) <> 0) then
3489 
3490                if (p_log_level_rec.statement_level) then
3491                   fa_debug_pkg.add(l_calling_fn, 'Create Terminal Gain/Loss entries',
3492                                    'Begin', p_log_level_rec => p_log_level_rec);
3493                end if;
3494 
3495                l_adj.asset_id                := l_asset_hdr_rec.asset_id;
3496                l_adj.transaction_header_id   := l_trans_rec.transaction_header_id;
3497                l_adj.book_type_code          := l_asset_hdr_rec.book_type_code;
3498                l_adj.period_counter_created  := l_period_rec.period_counter;
3499                l_adj.period_counter_adjusted := l_period_rec.period_counter;
3500                l_adj.current_units           := l_asset_desc_rec.current_units;
3501                l_adj.selection_retid         := 0;
3502                l_adj.leveling_flag           := TRUE;
3503                l_adj.flush_adj_flag          := FALSE;
3504                l_adj.last_update_date        := l_trans_rec.who_info.last_update_date;
3505                l_adj.gen_ccid_flag           := TRUE;
3506                l_adj.adjustment_type         := 'RESERVE';
3507                l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
3508                l_adj.adjustment_amount       := l_deprn_reserve;
3509                l_adj.mrc_sob_type_code       := p_mrc_sob_type_code;
3510                l_adj.set_of_books_id         := p_ret.set_of_books_id;
3511 
3512                if (l_deprn_reserve > 0) then
3513                   l_adj.debit_credit_flag       := 'DR';
3514                else
3515                   l_adj.debit_credit_flag       := 'CR';
3516                end if;
3517 
3518                if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
3519                                           l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
3520                   fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3521                   raise calc_err;
3522                end if;
3523 
3524                l_adj.account           := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
3525 
3526                if not FA_INS_ADJUST_PKG.faxinaj
3527                                (l_adj,
3528                                 l_trans_rec.who_info.last_update_date,
3529                                 l_trans_rec.who_info.last_updated_by,
3530                                 l_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
3531                   raise calc_err;
3532                end if;
3533 
3534                l_adj.adjustment_type         := 'NBV RETIRED';
3535                l_adj.adjustment_amount := l_deprn_reserve;
3536                l_adj.flush_adj_flag          := TRUE;
3537 
3538                if (l_deprn_reserve > 0) then
3539                   l_adj.debit_credit_flag := 'CR';
3540                   l_adj.account_type      := 'NBV_RETIRED_GAIN_ACCT';
3541                   l_adj.account           := fa_cache_pkg.fazcbc_record.nbv_retired_gain_acct;
3542                else
3543                   l_adj.debit_credit_flag       := 'DR';
3544                   l_adj.debit_credit_flag := 'CR';
3545                   l_adj.account_type      := 'NBV_RETIRED_LOSS_ACCT';
3546                   l_adj.account           := fa_cache_pkg.fazcbc_record.nbv_retired_loss_acct;
3547                end if; -- (nvl(l_deprn_reserve, 0) <> 0)
3548 
3549                if not FA_INS_ADJUST_PKG.faxinaj
3550                                (l_adj,
3551                                 l_trans_rec.who_info.last_update_date,
3552                                 l_trans_rec.who_info.last_updated_by,
3553                                 l_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
3554                   raise calc_err;
3555                end if;
3556 
3557                if (p_log_level_rec.statement_level) then
3558                   fa_debug_pkg.add(l_calling_fn, 'Create Terminal Gain/Loss entries',
3559                                    'End', p_log_level_rec => p_log_level_rec);
3560                end if;
3561 
3562                FA_BOOKS_PKG.Update_Row(
3563                       X_Book_Type_Code            => l_asset_hdr_rec.book_type_code,
3564                       X_Asset_Id                  => l_asset_hdr_rec.asset_id,
3565                       X_terminal_gain_loss_amount => l_deprn_reserve,
3566                       X_mrc_sob_type_code         => p_mrc_sob_type_code,
3567                       X_set_of_books_id           => p_ret.set_of_books_id,
3568                       X_Calling_Fn                => l_calling_fn, p_log_level_rec => p_log_level_rec);
3569 
3570             end if;
3571 
3572          end if; -- (nvl(l_asset_fin_rec_old.terminal_gain_loss, 'NO') = 'END_OF_YEAR')
3573 
3574       end if; -- (nvl(l_member_exists, 'N') <> 'Y')
3575 
3576    end if; -- (nvl(l_asset_fin_rec_old.terminal_gain_loss, 'NO')  = 'YES') and
3577 
3578    if (p_log_level_rec.statement_level) then
3579       fa_debug_pkg.add(l_calling_fn, 'End', 'Success', p_log_level_rec => p_log_level_rec);
3580    end if;
3581 
3582    return TRUE;
3583 
3584 EXCEPTION
3585    WHEN calc_err THEN
3586       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3587 
3588       return false;
3589 
3590    WHEN OTHERS THEN
3591       fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3592 
3593       return false;
3594 
3595 
3596 END Do_Retirement_in_CGL;
3597 
3598 FUNCTION Do_Reinstatement_in_CGL(
3599                       p_ret               IN fa_ret_types.ret_struct,
3600                       p_bk                IN fa_ret_types.book_struct,
3601                       p_dpr               IN fa_STD_TYPES.dpr_struct,
3602                       p_mrc_sob_type_code IN VARCHAR2,
3603                       p_calling_fn        IN VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
3604 return boolean IS
3605 
3606   CURSOR c_get_thid is
3607     select fa_transaction_headers_s.nextval
3608     from   dual;
3609 
3610   CURSOR c_get_thx is
3611     select trx.transaction_header_id
3612          , trx.transaction_date_entered
3613          , trx.last_update_date
3614          , trx.last_updated_by
3615          , trx.last_update_login
3616          , trx.mass_transaction_id
3617          , trx.calling_interface
3618          , trx.mass_reference_id
3619          , nvl(trx.amortization_start_date, trx.transaction_date_entered)
3620          , event_id
3621     from   fa_retirements ret
3622        ,   fa_transaction_headers trx
3623     where  ret.retirement_id = p_ret.retirement_id
3624     and    ret.transaction_header_id_out = trx.transaction_header_id;
3625 
3626 
3627   --Bug8425794: finding reserve retired if fa_retirements doesn't have it.
3628   --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3629   -- gets reserve retired amounts if FA_RETIREMENTS doesn't store it
3630   --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3631   CURSOR c_get_rsv_retired (c_transaction_header_id number) IS
3632     select sum(decode(debit_credit_flag, 'CR', -1 * adjustment_amount,
3633                                                   adjustment_amount))
3634     from   fa_adjustments
3635     where  source_type_code = 'RETIREMENT'
3636     and    adjustment_type = 'RESERVE'
3637     and    asset_id = p_ret.asset_id
3638     and    book_type_code = p_ret.book
3639     and    transaction_header_id = c_transaction_header_id;
3640 
3641   --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3642   -- For MRC
3643   -- gets reserve retired amounts if FA_RETIREMENTS doesn't store it
3644   --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3645   CURSOR c_get_mc_rsv_retired (c_transaction_header_id number) IS
3646     select sum(decode(debit_credit_flag, 'CR', -1 * adjustment_amount,
3647                                                   adjustment_amount))
3648     from   fa_mc_adjustments
3649     where  source_type_code = 'RETIREMENT'
3650     and    adjustment_type = 'RESERVE'
3651     and    asset_id = p_ret.asset_id
3652     and    book_type_code = p_ret.book
3653     and    transaction_header_id = c_transaction_header_id
3654     and    set_of_books_id = p_ret.set_of_books_id;
3655 
3656   --Bug11886090: Finding catchup expense created at the time of retirement
3657   --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3658   -- gets expense that retirement created
3659   --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3660   CURSOR c_get_ret_info IS
3661     select sum(decode(debit_credit_flag, 'DR', -1, 1) * adjustment_amount)
3662     from   fa_adjustments aj, fa_transaction_headers th
3663     where  aj.source_type_code = 'RETIREMENT'
3664     and    aj.adjustment_type = 'EXPENSE'
3665     and    aj.asset_id = p_bk.group_asset_id
3666     and    aj.book_type_code = p_ret.book
3667     and    aj.transaction_header_id = th.transaction_header_id
3668     and    th.asset_id = p_bk.group_asset_id
3669     and    th.book_type_code = p_ret.book
3670     and    th.member_transaction_header_id = p_ret.th_id_in;
3671 
3672   --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3673   -- For MRC: gets expense that retirement created
3674   --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3675   CURSOR c_get_mc_ret_info IS
3676     select sum(decode(debit_credit_flag, 'DR', -1, 1) * adjustment_amount)
3677     from   fa_mc_adjustments aj, fa_transaction_headers th
3678     where  aj.source_type_code = 'RETIREMENT'
3679     and    aj.adjustment_type = 'EXPENSE'
3680     and    aj.asset_id = p_bk.group_asset_id
3681     and    aj.book_type_code = p_ret.book
3682     and    aj.transaction_header_id = th.transaction_header_id
3683     and    th.asset_id = p_bk.group_asset_id
3684     and    th.book_type_code = p_ret.book
3685     and    th.member_transaction_header_id = p_ret.th_id_in
3686     and    aj.set_of_books_id = p_ret.set_of_books_id;
3687 
3688   l_calling_fn            VARCHAR2(100) := 'fa_retirement_pvt.Do_Reinstatement_in_CGL';
3689 
3690   l_trans_rec             FA_API_TYPES.trans_rec_type;
3691   l_asset_hdr_rec         FA_API_TYPES.asset_hdr_rec_type;
3692   l_asset_hdr_rec_m       FA_API_TYPES.asset_hdr_rec_type;
3693   l_asset_desc_rec        FA_API_TYPES.asset_desc_rec_type;
3694   l_asset_type_rec        FA_API_TYPES.asset_type_rec_type;
3695   l_asset_cat_rec         FA_API_TYPES.asset_cat_rec_type;
3696   l_asset_fin_rec_new_m   FA_API_TYPES.asset_fin_rec_type;
3697   l_asset_fin_rec_old     FA_API_TYPES.asset_fin_rec_type;
3698   l_asset_fin_rec_adj     FA_API_TYPES.asset_fin_rec_type;
3699   l_asset_fin_rec_new     FA_API_TYPES.asset_fin_rec_type;
3700   l_inv_trans_rec         FA_API_TYPES.inv_trans_rec_type;
3701   l_asset_deprn_rec_old   FA_API_TYPES.asset_deprn_rec_type;
3702   l_asset_deprn_rec_adj   FA_API_TYPES.asset_deprn_rec_type;
3703   l_asset_deprn_rec_new   FA_API_TYPES.asset_deprn_rec_type;
3704   l_period_rec            FA_API_TYPES.period_rec_type;
3705   l_group_reclass_options_rec FA_API_TYPES.group_reclass_options_rec_type;
3706 
3707   l_adj                    FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT; --Bug11886090
3708 
3709   calc_err    EXCEPTION;
3710 
3711 BEGIN
3712    if (p_log_level_rec.statement_level) then
3713       fa_debug_pkg.add(l_calling_fn, 'Begin',
3714                        p_ret.asset_id||':'||p_bk.group_asset_id, p_log_level_rec => p_log_level_rec);
3715    end if;
3716 
3717    --
3718    -- Prepare to call FA_ADJUSTMENT_PVT.do_adjustment to process group
3719    -- asset after member asset retirement.
3720    --
3721    if (p_mrc_sob_type_code <> 'R') then
3722       OPEN c_get_thid;
3723       FETCH c_get_thid INTO l_trans_rec.transaction_header_id;
3724       CLOSE c_get_thid;
3725 
3726       -- Bug 8674833: Save the grp_trx_hdr_id for mrc loop
3727       g_grp_trx_hdr_id := l_trans_rec.transaction_header_id;
3728    else
3729       -- Bug 8674833: Get the grp_trx_hdr_id from the global
3730       l_trans_rec.transaction_header_id := g_grp_trx_hdr_id;
3731    end if;
3732 
3733    l_trans_rec.transaction_type_code := 'GROUP ADJUSTMENT';
3734    l_trans_rec.transaction_subtype := 'AMORTIZED';
3735    l_trans_rec.transaction_key := 'MS';
3736 
3737    OPEN c_get_thx;
3738    FETCH c_get_thx INTO l_trans_rec.member_transaction_header_id
3739                       , l_trans_rec.transaction_date_entered
3740                       , l_trans_rec.who_info.last_update_date
3741                       , l_trans_rec.who_info.last_updated_by
3742                       , l_trans_rec.who_info.last_update_login
3743                       , l_trans_rec.mass_transaction_id
3744                       , l_trans_rec.calling_interface
3745                       , l_trans_rec.mass_reference_id
3746                       , l_trans_rec.amortization_start_date
3747                       , l_trans_rec.event_id;
3748    CLOSE c_get_thx;
3749 
3750    if (l_trans_rec.transaction_date_entered is null) then
3751       if (p_log_level_rec.statement_level) then
3752          fa_debug_pkg.add(l_calling_fn, 'Looking for retirement transaction', 'FAILED', p_log_level_rec => p_log_level_rec);
3753 
3754       end if;
3755 
3756       raise calc_err;
3757    end if;
3758 
3759    l_trans_rec.who_info.creation_date := l_trans_rec.who_info.last_update_date;
3760    l_trans_rec.who_info.created_by := l_trans_rec.who_info.last_updated_by;
3761 
3762    l_asset_hdr_rec.asset_id := p_bk.group_asset_id;
3763    l_asset_hdr_rec.book_type_code := p_ret.book;
3764    l_asset_hdr_rec.set_of_books_id := p_ret.set_of_books_id;
3765 
3766    if not FA_UTIL_PVT.get_asset_type_rec (
3767                    p_asset_hdr_rec      => l_asset_hdr_rec,
3768                    px_asset_type_rec    => l_asset_type_rec, p_log_level_rec => p_log_level_rec) then
3769       raise calc_err;
3770    end if;
3771 
3772    if not FA_UTIL_PVT.get_asset_desc_rec (
3773                    p_asset_hdr_rec         => l_asset_hdr_rec,
3774                    px_asset_desc_rec       => l_asset_desc_rec, p_log_level_rec => p_log_level_rec) then
3775       raise calc_err;
3776    end if;
3777 
3778    if not FA_UTIL_PVT.get_asset_cat_rec (
3779                    p_asset_hdr_rec         => l_asset_hdr_rec,
3780                    px_asset_cat_rec        => l_asset_cat_rec,
3781                    p_date_effective        => null, p_log_level_rec => p_log_level_rec) then
3782       raise calc_err;
3783    end if;
3784 
3785    if not fa_util_pvt.get_asset_fin_rec (
3786                    p_asset_hdr_rec         => l_asset_hdr_rec,
3787                    px_asset_fin_rec        => l_asset_fin_rec_old,
3788                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
3789       raise calc_err;
3790    end if;
3791 
3792    if not FA_UTIL_PVT.get_period_rec(
3793                    p_book           => l_asset_hdr_rec.book_type_code,
3794                    x_period_rec     => l_period_rec, p_log_level_rec => p_log_level_rec) then
3795       raise calc_err;
3796    end if;
3797 
3798 --tk_util.DumpFinRec(l_asset_fin_rec_old, 'OLD');
3799 
3800    if not fa_util_pvt.get_asset_deprn_rec (
3801                    p_asset_hdr_rec         => l_asset_hdr_rec,
3802                    px_asset_deprn_rec      => l_asset_deprn_rec_old,
3803                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
3804       raise calc_err;
3805    end if;
3806 
3807    if (l_asset_type_rec.asset_type = 'CIP') then
3808       l_asset_fin_rec_adj.cip_cost := p_ret.cost_retired;
3809    else
3810       l_asset_fin_rec_adj.cost :=  p_ret.cost_retired;
3811    end if;
3812 
3813    l_asset_fin_rec_adj.unrevalued_cost := p_ret.cost_retired;
3814    l_asset_fin_rec_adj.ytd_proceeds := p_ret.proceeds_of_sale;
3815    l_asset_fin_rec_adj.ltd_proceeds := p_ret.proceeds_of_sale;
3816    l_asset_fin_rec_adj.ltd_cost_of_removal := p_ret.cost_of_removal;
3817 
3818 
3819    --Bug8425794: passing reserve to be reinstated
3820    --            Ideally the reserve retired is stored in fa_retirements but
3821    --            the may not be the case so fetching the value if it is null
3822    --l_asset_deprn_rec_adj.deprn_reserve := -1*p_ret.reserve_retired;
3823    if p_ret.reserve_retired is null then
3824       if (p_mrc_sob_type_code = 'R') then
3825          OPEN c_get_mc_rsv_retired (p_ret.th_id_in);
3826          FETCH c_get_mc_rsv_retired INTO l_asset_deprn_rec_adj.deprn_reserve;
3827          CLOSE c_get_mc_rsv_retired;
3828       else
3829          OPEN c_get_rsv_retired (p_ret.th_id_in);
3830          FETCH c_get_rsv_retired INTO l_asset_deprn_rec_adj.deprn_reserve;
3831          CLOSE c_get_rsv_retired;
3832       end if;
3833    end if;
3834 
3835    -- Bug11886090: Getting (catchup)expense created during retirement
3836    if (p_mrc_sob_type_code = 'R') then
3837       OPEN c_get_mc_ret_info;
3838       FETCH c_get_mc_ret_info INTO l_asset_deprn_rec_adj.deprn_amount;
3839       CLOSE c_get_mc_ret_info;
3840    else
3841       OPEN c_get_ret_info;
3842       FETCH c_get_ret_info INTO l_asset_deprn_rec_adj.deprn_amount;
3843       CLOSE c_get_ret_info;
3844    end if;
3845 
3846    -- Bug11886090: Creating reverse entry for catchup expense created during retirement
3847    if nvl(l_asset_deprn_rec_adj.deprn_amount, 0) <> 0 then
3848       l_adj.asset_id                := l_asset_hdr_rec.asset_id;
3849       l_adj.transaction_header_id   := l_trans_rec.transaction_header_id;
3850       l_adj.book_type_code          := l_asset_hdr_rec.book_type_code;
3851       l_adj.period_counter_created  := l_period_rec.period_counter;
3852       l_adj.period_counter_adjusted := l_period_rec.period_counter;
3853       l_adj.current_units           := l_asset_desc_rec.current_units;
3854       l_adj.selection_retid         := 0;
3855       l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
3856       l_adj.leveling_flag           := TRUE;
3857       l_adj.flush_adj_flag          := TRUE;
3858       l_adj.last_update_date        := l_trans_rec.who_info.last_update_date;
3859       l_adj.gen_ccid_flag           := TRUE;
3860       l_adj.adjustment_type         := 'EXPENSE';
3861       l_adj.account_type            := 'DEPRN_EXPNESE_ACCT';
3862       l_adj.adjustment_amount       := l_asset_deprn_rec_adj.deprn_amount;
3863       l_adj.mrc_sob_type_code       := p_ret.mrc_sob_type_code;
3864       l_adj.set_of_books_id         := p_ret.set_of_books_id;
3865       l_adj.debit_credit_flag       := 'DR';
3866       l_adj.source_type_code        := 'RETIREMENT';
3867 
3868       if not fa_cache_pkg.fazccb(l_asset_hdr_rec.book_type_code,
3869                                  l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
3870          fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3871          raise calc_err;
3872       end if;
3873 
3874       l_adj.account                := fa_cache_pkg.fazccb_record.DEPRN_EXPENSE_ACCT;
3875 
3876       if not FA_INS_ADJUST_PKG.faxinaj
3877                                (l_adj,
3878                                 l_trans_rec.who_info.last_update_date,
3879                                 l_trans_rec.who_info.last_updated_by,
3880                                 l_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
3881          raise calc_err;
3882       end if;
3883 
3884    end if;
3885 
3886    if (p_log_level_rec.statement_level) then
3887       fa_debug_pkg.add(l_calling_fn, 'l_asset_deprn_rec_adj.deprn_reserve',l_asset_deprn_rec_adj.deprn_reserve, p_log_level_rec);
3888       fa_debug_pkg.add(l_calling_fn, 'l_asset_deprn_rec_adj.deprn_amount',l_asset_deprn_rec_adj.deprn_amount, p_log_level_rec);
3889    end if;
3890 
3891    l_asset_hdr_rec_m.asset_id := p_ret.asset_id;
3892    l_asset_hdr_rec_m.book_type_code := p_ret.book;
3893    l_asset_hdr_rec_m.set_of_books_id := p_ret.set_of_books_id;
3894 
3895    -- Get new member's fin_rec
3896    if not fa_util_pvt.get_asset_fin_rec (
3897                    p_asset_hdr_rec         => l_asset_hdr_rec_m,
3898                    px_asset_fin_rec        => l_asset_fin_rec_new_m,
3899                    p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
3900       raise calc_err;
3901    end if;
3902 
3903    l_asset_fin_rec_adj.salvage_value := nvl(l_asset_fin_rec_new_m.salvage_value, 0) -
3904                                         nvl(p_bk.salvage_value, 0);
3905 
3906    l_asset_fin_rec_adj.allowed_deprn_limit_amount :=
3907                                         nvl(l_asset_fin_rec_new_m.allowed_deprn_limit_amount, 0) -
3908                                         nvl(p_bk.allowed_deprn_limit_amount, 0);
3909 
3910    UPDATE FA_ADJUSTMENTS
3911    SET TRANSACTION_HEADER_ID = l_trans_rec.transaction_header_id
3912    WHERE TRANSACTION_HEADER_ID = l_trans_rec.member_transaction_header_id
3913    AND   ASSET_ID = l_asset_hdr_rec.asset_id
3914    AND   BOOK_TYPE_CODE = l_asset_hdr_rec.book_type_code;
3915 
3916    /*8206076 -Start */
3917    if not((nvl(l_asset_fin_rec_old.tracking_method, 'NO TRACK') = 'ALLOCATE') and
3918        (fa_cache_pkg.fazcdrd_record.rule_name = 'ENERGY PERIOD END BALANCE')) then
3919                l_trans_rec.transaction_date_entered := p_ret.date_retired;
3920                l_trans_rec.amortization_start_date := p_ret.date_retired;
3921           if (p_log_level_rec.statement_level) then
3922             fa_debug_pkg.add(l_calling_fn, 'New transaction_date_entered',l_trans_rec.transaction_date_entered);
3923             fa_debug_pkg.add(l_calling_fn, 'New amortization_start_date',l_trans_rec.transaction_date_entered);
3924           end if;
3925    end if;
3926   /*8206076 -End */
3927 
3928    if (p_log_level_rec.statement_level) then
3929       fa_debug_pkg.add(l_calling_fn, 'Call FA_ADJUSTMENT_PVT.do_adjustment4',
3930                        'Begin',  p_log_level_rec => p_log_level_rec);
3931 
3932    end if;
3933 
3934       if not FA_ADJUSTMENT_PVT.do_adjustment
3935                   (px_trans_rec              => l_trans_rec,
3936                    px_asset_hdr_rec          => l_asset_hdr_rec,
3937                    p_asset_desc_rec          => l_asset_desc_rec,
3938                    p_asset_type_rec          => l_asset_type_rec,
3939                    p_asset_cat_rec           => l_asset_cat_rec,
3940                    p_asset_fin_rec_old       => l_asset_fin_rec_old,
3941                    p_asset_fin_rec_adj       => l_asset_fin_rec_adj,
3942                    x_asset_fin_rec_new       => l_asset_fin_rec_new,
3943                    p_inv_trans_rec           => l_inv_trans_rec,
3944                    p_asset_deprn_rec_old     => l_asset_deprn_rec_old,
3945                    p_asset_deprn_rec_adj     => l_asset_deprn_rec_adj,
3946                    x_asset_deprn_rec_new     => l_asset_deprn_rec_new,
3947                    p_period_rec              => l_period_rec,
3948                    p_mrc_sob_type_code       => p_mrc_sob_type_code,
3949                    p_group_reclass_options_rec => l_group_reclass_options_rec,
3950                    p_calling_fn              => l_calling_fn, p_log_level_rec => p_log_level_rec)then
3951          raise calc_err;
3952       end if;
3953 
3954    if (nvl(l_asset_fin_rec_old.member_rollup_flag, 'N') = 'Y') then
3955       --
3956       -- This is Sum up member depreciation to group asset
3957       --
3958       if (p_log_level_rec.statement_level) then
3959          fa_debug_pkg.add(l_calling_fn, 'Process Member Rollup Group',
3960                           l_asset_hdr_rec.asset_id, p_log_level_rec => p_log_level_rec);
3961       end if;
3962 
3963       -- +++++ Remove group expense rows +++++
3964       DELETE FROM FA_ADJUSTMENTS
3965       WHERE  ASSET_ID = l_asset_hdr_rec.asset_id
3966       AND    BOOK_TYPE_CODE = l_asset_hdr_rec.book_type_code
3967       AND    TRANSACTION_HEADER_ID = l_trans_rec.member_transaction_header_id
3968       AND    SOURCE_TYPE_CODE = 'DEPRECIATION'
3969       AND    ADJUSTMENT_TYPE in ('EXPENSE', 'BONUS EXPENSE', 'IMPAIR EXPENSE');
3970 
3971    end if; -- (nvl(l_asset_fin_rec_old.member_rollup_flag, 'N') = 'N')
3972 
3973    -- +++++ Clear Terminal Gain Loss Amount +++++
3974    FA_BOOKS_PKG.Update_Row(
3975                    X_Book_Type_Code            => l_asset_hdr_rec.book_type_code,
3976                    X_Asset_Id                  => l_asset_hdr_rec.asset_id,
3977                    X_terminal_gain_loss_amount => FND_API.G_MISS_NUM,
3978                    X_mrc_sob_type_code         => p_mrc_sob_type_code,
3979                    X_set_of_books_id           => p_ret.set_of_books_id,
3980                    X_Calling_Fn                => l_calling_fn, p_log_level_rec => p_log_level_rec);
3981    return true;
3982 
3983 EXCEPTION
3984    WHEN calc_err THEN
3985       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3986 
3987       return false;
3988 
3989    WHEN OTHERS THEN
3990       if c_get_ret_info%ISOPEN then
3991          close c_get_ret_info;
3992       end if;
3993       if c_get_mc_ret_info%ISOPEN then
3994          close c_get_mc_ret_info;
3995       end if;
3996       if c_get_rsv_retired%ISOPEN then
3997          close c_get_rsv_retired;
3998       end if;
3999       if c_get_mc_rsv_retired%ISOPEN then
4000          close c_get_mc_rsv_retired;
4001       end if;
4002       if c_get_thid%ISOPEN then
4003          close c_get_thid;
4004       end if;
4005       if c_get_thx%ISOPEN then
4006          close c_get_thx;
4007       end if;
4008 
4009       fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4010 
4011       return false;
4012 
4013 END Do_Reinstatement_in_CGL;
4014 
4015 FUNCTION Do_Terminal_Gain_Loss_All_Bk (
4016    p_book_type_code    VARCHAR2,
4017    p_asset_id          NUMBER,
4018    p_period_rec        FA_API_TYPES.period_rec_type,
4019    p_mrc_sob_type_code VARCHAR2,
4020    p_set_of_books_id   NUMBER,
4021    p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)RETURN BOOLEAN IS
4022 
4023    l_calling_fn VARCHAR2(50) := 'FA_RETIREMENT_PVT.Do_Terminal_Gain_Loss_All_Bk';
4024 
4025    cursor c_member_exists IS
4026      select 'Y'
4027      from   fa_books
4028      where  book_type_code = p_book_type_code
4029      and    group_asset_id = p_asset_id
4030      and    period_counter_fully_retired is null
4031      and    transaction_header_id_out is null;
4032 
4033    cursor c_max_ret_thid is
4034      select max(gth.transaction_header_id)
4035      from   fa_transaction_headers gth,
4036             fa_transaction_headers mth,
4037             fa_retirements ret,
4038             fa_books bk
4039      where  bk.group_asset_id = p_asset_id
4040      and    bk.book_type_code = p_book_type_code
4041      and    ret.asset_id = bk.asset_id
4042      and    ret.book_type_code = p_book_type_code
4043      and    mth.asset_id = bk.asset_id
4044      and    mth.book_type_code = p_book_type_code
4045      and    gth.asset_id = p_asset_id
4046      and    gth.book_type_code = p_book_type_code
4047      and    mth.transaction_type_code = 'FULL RETIREMENT'
4048      and    mth.transaction_header_id = gth.member_transaction_header_id;
4049 
4050    cursor c_mc_member_exists IS
4051      select 'Y'
4052      from   fa_mc_books
4053      where  book_type_code = p_book_type_code
4054      and    group_asset_id = p_asset_id
4055      and    period_counter_fully_retired is null
4056      and    transaction_header_id_out is null
4057      and    set_of_books_id = p_set_of_books_id;
4058 
4059    cursor c_max_mc_ret_thid is
4060      select max(gth.transaction_header_id)
4061      from   fa_transaction_headers gth,
4062             fa_transaction_headers mth,
4063             fa_mc_retirements ret,
4064             fa_mc_books bk
4065      where  bk.group_asset_id = p_asset_id
4066      and    bk.book_type_code = p_book_type_code
4067      and    bk.set_of_books_id = p_set_of_books_id
4068      and    ret.asset_id = bk.asset_id
4069      and    ret.book_type_code = p_book_type_code
4070      and    ret.set_of_books_id = p_set_of_books_id
4071      and    mth.asset_id = bk.asset_id
4072      and    mth.book_type_code = p_book_type_code
4073      and    gth.asset_id = p_asset_id
4074      and    gth.book_type_code = p_book_type_code
4075      and    mth.transaction_type_code = 'FULL RETIREMENT'
4076      and    mth.transaction_header_id = gth.member_transaction_header_id;
4077 
4078     -- +++++ Get Current Unit of Group Asset +++++
4079    cursor c_get_unit is
4080      select units,
4081             category_id
4082      from   fa_asset_history
4083      where  asset_id = p_asset_id
4084      and    transaction_header_id_out is null;
4085 
4086    l_adj                        fa_adjust_type_pkg.fa_adj_row_struct;
4087    l_asset_cat_rec              FA_API_TYPES.asset_cat_rec_type;
4088 
4089    l_member_exists              VARCHAR2(1) := 'N';
4090    l_deprn_reserve   NUMBER;
4091    l_last_update_date  DATE := sysdate;
4092    l_last_updated_by   NUMBER(15) := fnd_global.user_id;
4093    l_last_update_login NUMBER(15) := fnd_global.user_id;
4094 
4095   l_temp_num              NUMBER;
4096   l_temp_char             VARCHAR2(30);
4097   l_temp_bool             BOOLEAN;
4098 
4099    gl_err      EXCEPTION;
4100 
4101 BEGIN
4102 
4103    if (p_mrc_sob_type_code = 'R') then
4104       OPEN c_mc_member_exists;
4105       FETCH c_mc_member_exists INTO l_member_exists;
4106       CLOSE c_mc_member_exists;
4107    else
4108       OPEN c_member_exists;
4109       FETCH c_member_exists INTO l_member_exists;
4110       CLOSE c_member_exists;
4111    end if;
4112 
4113    if (nvl(l_member_exists, 'N') = 'N' ) then
4114 
4115       if (p_log_level_rec.statement_level) then
4116          fa_debug_pkg.add(l_calling_fn, 'Call',
4117                           'fa_query_balances_pkg.query_balances', p_log_level_rec => p_log_level_rec);
4118       end if;
4119 
4120       fa_query_balances_pkg.query_balances(
4121                       X_asset_id => p_asset_id,
4122                       X_book => p_book_type_code,
4123                       X_period_ctr => 0,
4124                       X_dist_id => 0,
4125                       X_run_mode => 'STANDARD',
4126                       X_cost => l_temp_num,
4127                       X_deprn_rsv => l_deprn_reserve,
4128                       X_reval_rsv => l_temp_num,
4129                       X_ytd_deprn => l_temp_num,
4130                       X_ytd_reval_exp => l_temp_num,
4131                       X_reval_deprn_exp => l_temp_num,
4132                       X_deprn_exp => l_temp_num,
4133                       X_reval_amo => l_temp_num,
4134                       X_prod => l_temp_num,
4135                       X_ytd_prod => l_temp_num,
4136                       X_ltd_prod => l_temp_num,
4137                       X_adj_cost => l_temp_num,
4138                       X_reval_amo_basis => l_temp_num,
4139                       X_bonus_rate => l_temp_num,
4140                       X_deprn_source_code => l_temp_char,
4141                       X_adjusted_flag => l_temp_bool,
4142                       X_transaction_header_id => -1,
4143                       X_bonus_deprn_rsv => l_temp_num,
4144                       X_bonus_ytd_deprn => l_temp_num,
4145                       X_bonus_deprn_amount => l_temp_num,
4146                       X_impairment_rsv => l_temp_num,
4147                       X_ytd_impairment => l_temp_num,
4148                       X_impairment_amount => l_temp_num,
4149                       X_capital_adjustment => l_temp_num,
4150                       X_general_fund => l_temp_num,
4151                       X_mrc_sob_type_code => p_mrc_sob_type_code,
4152                       X_set_of_books_id => p_set_of_books_id,
4153                       p_log_level_rec => p_log_level_rec);
4154 
4155       if (p_log_level_rec.statement_level) then
4156          fa_debug_pkg.add(l_calling_fn, 'Coming back from Query Balance',
4157                           l_deprn_reserve, p_log_level_rec => p_log_level_rec);
4158       end if;
4159 
4160       l_adj.asset_id                := p_asset_id;
4161 
4162       if (p_mrc_sob_type_code = 'R') then
4163          OPEN c_max_mc_ret_thid;
4164          FETCH c_max_mc_ret_thid INTO l_adj.transaction_header_id;
4165          CLOSE c_max_mc_ret_thid;
4166       else
4167          OPEN c_max_ret_thid;
4168          FETCH c_max_ret_thid INTO l_adj.transaction_header_id;
4169          CLOSE c_max_ret_thid;
4170       end if;
4171 
4172       if (p_log_level_rec.statement_level) then
4173          fa_debug_pkg.add(l_calling_fn, 'Transaction_Header_Id for Terminal GL',
4174                           l_adj.transaction_header_id, p_log_level_rec => p_log_level_rec);
4175       end if;
4176 
4177       l_adj.book_type_code          := p_book_type_code;
4178       l_adj.period_counter_created  := p_period_rec.period_counter;
4179       l_adj.period_counter_adjusted := p_period_rec.period_counter;
4180 
4181       OPEN c_get_unit;
4182       FETCH c_get_unit INTO l_adj.current_units , l_asset_cat_rec.category_id;
4183       CLOSE c_get_unit;
4184 
4185       l_adj.selection_retid         := 0;
4186       l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
4187       l_adj.leveling_flag           := TRUE;
4188       l_adj.flush_adj_flag          := FALSE;
4189       l_adj.last_update_date        := sysdate;
4190       l_adj.gen_ccid_flag           := TRUE;
4191       l_adj.adjustment_type         := 'RESERVE';
4192       l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
4193       l_adj.adjustment_amount       := l_deprn_reserve;
4194 
4195       if (l_deprn_reserve > 0) then
4196          l_adj.debit_credit_flag    := 'DR';
4197       else
4198          l_adj.debit_credit_flag    := 'CR';
4199       end if;
4200 
4201       if not fa_cache_pkg.fazccb(p_book_type_code,
4202                                  l_asset_cat_rec.category_id, p_log_level_rec => p_log_level_rec) then
4203          fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4204          raise gl_err;
4205       end if;
4206 
4207       l_adj.account           := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
4208       l_adj.mrc_sob_type_code := p_mrc_sob_type_code;
4209       l_adj.set_of_books_id   := p_set_of_books_id;
4210 
4211       if not FA_INS_ADJUST_PKG.faxinaj
4212                                (l_adj,
4213                                 l_last_update_date,
4214                                 l_last_updated_by,
4215                                 l_last_update_login, p_log_level_rec => p_log_level_rec) then
4216          raise gl_err;
4217       end if;
4218 
4219       l_adj.adjustment_type      := 'NBV RETIRED';
4220       l_adj.adjustment_amount    := l_deprn_reserve;
4221       l_adj.flush_adj_flag       := TRUE;
4222 
4223       if (l_deprn_reserve > 0) then
4224          l_adj.debit_credit_flag := 'CR';
4225          l_adj.account_type      := 'NBV_RETIRED_GAIN_ACCT';
4226          l_adj.account           := fa_cache_pkg.fazcbc_record.nbv_retired_gain_acct;
4227       else
4228          l_adj.debit_credit_flag := 'CR';
4229          l_adj.account_type      := 'NBV_RETIRED_LOSS_ACCT';
4230          l_adj.account           := fa_cache_pkg.fazcbc_record.nbv_retired_loss_acct;
4231       end if;
4232 
4233       if not FA_INS_ADJUST_PKG.faxinaj
4234                                (l_adj,
4235                                 l_last_update_date,
4236                                 l_last_updated_by,
4237                                 l_last_update_login, p_log_level_rec => p_log_level_rec) then
4238 
4239          raise gl_err;
4240       end if;
4241 
4242       FA_BOOKS_PKG.Update_Row(
4243                       X_Book_Type_Code               => p_book_type_code,
4244                       X_Asset_Id                     => p_asset_id,
4245                       X_terminal_gain_loss_amount    => l_deprn_reserve,
4246                       X_terminal_gain_loss_flag      => 'N',
4247                       X_mrc_sob_type_code            => p_mrc_sob_type_code,
4248                       X_set_of_books_id              => fa_cache_pkg.fazcbc_record.set_of_books_id,
4249                       X_Calling_Fn                   => l_calling_fn, p_log_level_rec => p_log_level_rec);
4250 
4251    else
4252       if (p_log_level_rec.statement_level) then
4253           fa_debug_pkg.add(l_calling_fn, 'There is at least non retired member exists',
4254                            p_asset_id, p_log_level_rec => p_log_level_rec);
4255       end if;
4256       --
4257       -- Must not calculate terminal gain loss as long as some
4258       -- non-retired member exists
4259       --
4260       FA_BOOKS_PKG.Update_Row(
4261                    X_Book_Type_Code               => p_book_type_code,
4262                    X_Asset_Id                     => p_asset_id,
4263                    X_terminal_gain_loss_flag      => FND_API.G_MISS_CHAR,
4264                    X_mrc_sob_type_code            => p_mrc_sob_type_code,
4265                    X_set_of_books_id              => fa_cache_pkg.fazcbc_record.set_of_books_id,
4266                    X_Calling_Fn                   => l_calling_fn, p_log_level_rec => p_log_level_rec);
4267 
4268    end if; -- (nvl(l_member_exists, 'N') = 'N' )
4269 
4270   return true;
4271 
4272 EXCEPTION
4273    WHEN gl_err THEN
4274       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4275 
4276       return false;
4277 
4278    WHEN OTHERS THEN
4279       fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4280 
4281       return false;
4282 
4283 END Do_Terminal_Gain_Loss_All_Bk;
4284 
4285 FUNCTION Do_Terminal_Gain_Loss (
4286    p_book_type_code    VARCHAR2,
4287    p_set_of_books_id    NUMBER,
4288    p_total_requests     NUMBER,
4289    p_request_number     NUMBER,
4290    p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)RETURN BOOLEAN IS
4291 
4292    l_calling_fn VARCHAR2(50) := 'FA_RETIREMENT_PVT.Do_Terminal_Gain_Loss';
4293 
4294    l_period_rec    FA_API_TYPES.period_rec_type;
4295 
4296    cursor c_get_groups is
4297      select bk.asset_id asset_id
4298      from   fa_books bk,
4299             fa_additions ad
4300      where  bk.book_type_code = p_book_type_code
4301      and    bk.transaction_header_id_out is null
4302      and    bk.asset_id = ad.asset_id
4303      and    ad.asset_type = 'GROUP'
4304      and    bk.terminal_gain_loss = 'YES'
4305      and    bk.terminal_gain_loss_flag = 'Y'
4306      and    MOD(bk.asset_id, p_total_requests) = (p_request_number - 1);
4307 
4308    cursor c_get_eofy_groups is
4309      select bk.asset_id asset_id
4310      from   fa_books bk,
4311             fa_additions ad
4312      where  bk.book_type_code = p_book_type_code
4313      and    bk.transaction_header_id_out is null
4314      and    bk.asset_id = ad.asset_id
4315      and    ad.asset_type = 'GROUP'
4316      and    bk.terminal_gain_loss_flag = 'Y'
4317      and    MOD(bk.asset_id, p_total_requests) = (p_request_number - 1);
4318 
4319    l_sob_tbl                    FA_CACHE_PKG.fazcrsob_sob_tbl_type;
4320 
4321    TYPE tab_num15_type IS TABLE OF NUMBER(15) INDEX BY BINARY_INTEGER;
4322    t_asset_id                   tab_num15_type;
4323 
4324    gl_err      EXCEPTION;
4325 
4326 BEGIN
4327 
4328    if (p_log_level_rec.statement_level) then
4329       fa_debug_pkg.add(l_calling_fn, 'Begin', p_book_type_code, p_log_level_rec => p_log_level_rec);
4330    end if;
4331 
4332    --+++++ Get Current Period Info +++++
4333    if not FA_UTIL_PVT.get_period_rec(
4334                    p_book           => p_book_type_code,
4335                    x_period_rec     => l_period_rec, p_log_level_rec => p_log_level_rec) then
4336       raise gl_err;
4337    end if;
4338 
4339    if not fa_cache_pkg.fazcct(fa_cache_pkg.fazcbc_record.deprn_calendar, p_log_level_rec => p_log_level_rec) then
4340       raise gl_err;
4341    end if;
4342 
4343    -- Process Terminal Gain Loss only if this is last period of
4344    -- the fiscal year.
4345 
4346    --+++++ Check to see if this is ast period of the fiscal year.
4347    if (fa_cache_pkg.fazcct_record.number_per_fiscal_year =
4348       l_period_rec.period_num) then
4349 
4350       OPEN c_get_eofy_groups;
4351    else
4352       OPEN c_get_groups;
4353    end if;
4354 
4355    LOOP -- loop for bulk fetch with limit
4356 
4357       if (fa_cache_pkg.fazcct_record.number_per_fiscal_year =
4358           l_period_rec.period_num) then
4359 
4360          FETCH c_get_eofy_groups BULK COLLECT INTO t_asset_id;
4361       else
4362          FETCH c_get_groups BULK COLLECT INTO t_asset_id;
4363       end if;
4364 
4365       if (p_log_level_rec.statement_level) then
4366          fa_debug_pkg.add(l_calling_fn, 'Number of rows fetched',
4367                           to_char(t_asset_id.count));
4368       end if;
4369 
4370       EXIT WHEN t_asset_id.count = 0;
4371 
4372       for i IN 1..t_asset_id.count loop
4373 
4374          if not Do_Terminal_Gain_Loss_All_Bk (
4375                       p_book_type_code => p_book_type_code,
4376                       p_asset_id       => t_asset_id(i),
4377                       p_period_rec     => l_period_rec,
4378                       p_mrc_sob_type_code => 'P',
4379                       p_set_of_books_id => p_set_of_books_id,
4380                       p_log_level_rec     => p_log_level_rec) then
4381             raise gl_err;
4382          end if;
4383 
4384          if fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y' then
4385 
4386             -- call the sob cache to get the table of sob_ids
4387             if not FA_CACHE_PKG.fazcrsob
4388                       (x_book_type_code => p_book_type_code,
4389                        x_sob_tbl        => l_sob_tbl, p_log_level_rec => p_log_level_rec) then
4390                raise gl_err;
4391             end if;
4392 
4393             for l_sob_index in 1..l_sob_tbl.count loop
4394 
4395                if p_log_level_rec.statement_level then
4396                   fa_debug_pkg.add (l_calling_fn, 'Reporting book loop: Set_of_books_id',
4397                                     l_sob_tbl(l_sob_index));
4398                end if;
4399 
4400                if not fa_cache_pkg.fazcbcs(x_book => p_book_type_code,
4401                                            x_set_of_books_id => p_set_of_books_id,
4402                                            p_log_level_rec => p_log_level_rec) then
4403                   raise gl_err;
4404                end if;
4405 
4406                if not Do_Terminal_Gain_Loss_All_Bk (
4407                              p_book_type_code => p_book_type_code,
4408                              p_asset_id       => t_asset_id(i),
4409                              p_period_rec     => l_period_rec,
4410                              p_mrc_sob_type_code => 'P',
4411                              p_set_of_books_id => p_set_of_books_id,
4412                              p_log_level_rec     => p_log_level_rec) then
4413                   raise gl_err;
4414                end if;
4415 
4416             end loop;
4417          end if; -- fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y'
4418 
4419       end loop; -- for i IN 1..t_asset_id.count
4420 
4421    end loop; -- loop for bulk fetch with limit
4422 
4423    if (fa_cache_pkg.fazcct_record.number_per_fiscal_year =
4424        l_period_rec.period_num) then
4425 
4426       CLOSE c_get_eofy_groups;
4427    else
4428       CLOSE c_get_groups;
4429    end if;
4430 
4431    if (p_log_level_rec.statement_level) then
4432       fa_debug_pkg.add(l_calling_fn, 'End', p_book_type_code, p_log_level_rec => p_log_level_rec);
4433    end if;
4434 
4435    return true;
4436 
4437 EXCEPTION
4438    WHEN gl_err THEN
4439       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4440 
4441       return false;
4442 
4443    WHEN OTHERS THEN
4444       fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4445 
4446       return false;
4447 
4448 END Do_Terminal_Gain_Loss;
4449 
4450 FUNCTION Check_Terminal_Gain_Loss(
4451                       p_trans_rec         IN     FA_API_TYPES.trans_rec_type,
4452                       p_asset_hdr_rec     IN     FA_API_TYPES.asset_hdr_rec_type,
4453                       p_asset_type_rec    IN     FA_API_TYPES.asset_type_rec_type,
4454                       p_asset_fin_rec     IN     FA_API_TYPES.asset_fin_rec_type,
4455                       p_period_rec        IN     FA_API_TYPES.period_rec_type,
4456                       p_mrc_sob_type_code IN     VARCHAR2,
4457                       p_calling_fn        IN     VARCHAR2
4458 , p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) RETURN BOOLEAN IS
4459 
4460    l_calling_fn     VARCHAR2(50) := 'FA_RETIREMENT_PVT.Check_Terminal_Gain_Loss';
4461 
4462    --
4463    -- This cursor tries to find not full retired member of given group asset
4464    --  if there is any.
4465    --
4466    CURSOR c_find_member IS
4467       select asset_id
4468       from   fa_books
4469       where  group_asset_id = p_asset_hdr_rec.asset_id
4470       and    book_type_code = p_asset_hdr_rec.book_type_code
4471       and    period_counter_fully_retired is null
4472       and    transaction_header_id_out is null;
4473 
4474    CURSOR c_find_mc_member IS
4475       select asset_id
4476       from   fa_mc_books
4477       where  group_asset_id = p_asset_hdr_rec.asset_id
4478       and    book_type_code = p_asset_hdr_rec.book_type_code
4479       and    period_counter_fully_retired is null
4480       and    transaction_header_id_out is null
4481       and    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
4482 
4483 
4484    l_asset_id       NUMBER;
4485    l_is_tgl_due     BOOLEAN := FALSE;
4486 
4487    -- query balance variables
4488    l_deprn_reserve         NUMBER;
4489    l_temp_num              NUMBER;
4490    l_temp_char             VARCHAR2(30);
4491    l_temp_bool             BOOLEAN;
4492 
4493    tgl_err   EXCEPTION;
4494 BEGIN
4495    if (p_log_level_rec.statement_level) then
4496       fa_debug_pkg.add(l_calling_fn, 'Begin', to_char(p_asset_hdr_rec.asset_id)||':'||
4497                                               p_asset_hdr_rec.book_type_code);
4498    end if;
4499 
4500    --
4501    -- Check to see the group asset is eligible for terminal gain loss
4502    --
4503    if (p_asset_fin_rec.cost = 0) and
4504       (p_asset_fin_rec.cip_cost =  0) and
4505       (p_asset_fin_rec.terminal_gain_loss in ('YES', 'END_OF_YEAR')) then
4506 
4507       if (p_mrc_sob_type_code = 'R') then
4508 
4509          OPEN c_find_mc_member;
4510          FETCH c_find_mc_member INTO l_asset_id;
4511          CLOSE c_find_mc_member;
4512 
4513       else
4514 
4515          OPEN c_find_member;
4516          FETCH c_find_member INTO l_asset_id;
4517          CLOSE c_find_member;
4518 
4519       end if;
4520 
4521       if (l_asset_id is not null) then
4522 
4523          if (p_log_level_rec.statement_level) then
4524             fa_debug_pkg.add(l_calling_fn, 'Member exists', to_char(l_asset_id));
4525             fa_debug_pkg.add(l_calling_fn, 'End', 'No Terminal Gain Loss Calculatd', p_log_level_rec => p_log_level_rec);
4526          end if;
4527 
4528          return TRUE;
4529 
4530       end if;
4531 
4532       if (p_log_level_rec.statement_level) then
4533             fa_debug_pkg.add(l_calling_fn, 'Continue', 'Checking remaining reserve ', p_log_level_rec => p_log_level_rec);
4534       end if;
4535 
4536       if (p_log_level_rec.statement_level) then
4537          fa_debug_pkg.add(l_calling_fn, 'Call',
4538                           'fa_query_balances_pkg.query_balances', p_log_level_rec => p_log_level_rec);
4539       end if;
4540 
4541       fa_query_balances_pkg.query_balances(
4542                       X_asset_id => p_asset_hdr_rec.asset_id,
4543                       X_book => p_asset_hdr_rec.book_type_code,
4544                       X_period_ctr => 0,
4545                       X_dist_id => 0,
4546                       X_run_mode => 'STANDARD',
4547                       X_cost => l_temp_num,
4548                       X_deprn_rsv => l_deprn_reserve,
4549                       X_reval_rsv => l_temp_num,
4550                       X_ytd_deprn => l_temp_num,
4551                       X_ytd_reval_exp => l_temp_num,
4552                       X_reval_deprn_exp => l_temp_num,
4553                       X_deprn_exp => l_temp_num,
4554                       X_reval_amo => l_temp_num,
4555                       X_prod => l_temp_num,
4556                       X_ytd_prod => l_temp_num,
4557                       X_ltd_prod => l_temp_num,
4558                       X_adj_cost => l_temp_num,
4559                       X_reval_amo_basis => l_temp_num,
4560                       X_bonus_rate => l_temp_num,
4561                       X_deprn_source_code => l_temp_char,
4562                       X_adjusted_flag => l_temp_bool,
4563                       X_transaction_header_id => -1,
4564                       X_bonus_deprn_rsv => l_temp_num,
4565                       X_bonus_ytd_deprn => l_temp_num,
4566                       X_bonus_deprn_amount => l_temp_num,
4567                       X_impairment_rsv => l_temp_num,
4568                       X_ytd_impairment => l_temp_num,
4569                       X_impairment_amount => l_temp_num,
4570                       X_capital_adjustment => l_temp_num,
4571                       X_general_fund => l_temp_num,
4572                       X_mrc_sob_type_code => p_mrc_sob_type_code,
4573                       X_set_of_books_id => p_asset_hdr_rec.set_of_books_id,
4574                       p_log_level_rec => p_log_level_rec);
4575 
4576       if (p_log_level_rec.statement_level) then
4577          fa_debug_pkg.add(l_calling_fn, 'l_deprn_reserve',
4578                           l_deprn_reserve, p_log_level_rec => p_log_level_rec);
4579       end if;
4580 
4581       if (nvl(l_deprn_reserve, 0) = 0) then
4582 
4583          if (p_log_level_rec.statement_level) then
4584             fa_debug_pkg.add(l_calling_fn, 'Reserve is 0', 'No Terminal Gain Loss Calculatd', p_log_level_rec => p_log_level_rec);
4585          end if;
4586 
4587          return TRUE;
4588 
4589       end if;
4590 
4591       if (p_log_level_rec.statement_level) then
4592             fa_debug_pkg.add(l_calling_fn, 'Continue', 'Eligible for Terminal Gain Loss', p_log_level_rec => p_log_level_rec);
4593       end if;
4594 
4595       --
4596       -- Setting TERMINAL_GAIN_LOSS_FLAG so terminal gain loss will be
4597       -- processed during depreciation
4598       --
4599       if (p_mrc_sob_type_code = 'R') then
4600 
4601          UPDATE FA_MC_BOOKS
4602          SET    TERMINAL_GAIN_LOSS_FLAG = 'Y'
4603          WHERE  ASSET_ID = p_asset_hdr_rec.asset_id
4604          AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4605          AND    TRANSACTION_HEADER_ID_OUT is null
4606          AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
4607 
4608          UPDATE FA_MC_BOOKS_SUMMARY
4609          SET    TERMINAL_GAIN_LOSS_FLAG = 'Y'
4610          WHERE  ASSET_ID = p_asset_hdr_rec.asset_id
4611          AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4612          AND    PERIOD_COUNTER = p_period_rec.period_counter
4613          AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
4614 
4615          /*bug#15897249 starts */
4616          UPDATE FA_MC_BOOKS
4617          SET    TERMINAL_GAIN_LOSS_FLAG = 'Y'
4618          WHERE  GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4619          AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4620          AND    TRANSACTION_HEADER_ID_OUT is null
4621          AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id
4622          AND    tracking_method = 'CALCULATE'
4623          AND    recognize_gain_loss = 'NO'
4624          and    member_rollup_flag = 'Y';
4625          /*bug#15897249 ends */
4626 
4627       else
4628 
4629          UPDATE FA_BOOKS
4630          SET    TERMINAL_GAIN_LOSS_FLAG = 'Y'
4631          WHERE  ASSET_ID = p_asset_hdr_rec.asset_id
4632          AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4633          AND    TRANSACTION_HEADER_ID_OUT is null;
4634 
4635          UPDATE FA_BOOKS_SUMMARY
4636          SET    TERMINAL_GAIN_LOSS_FLAG = 'Y'
4637          WHERE  ASSET_ID = p_asset_hdr_rec.asset_id
4638          AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4639          AND    PERIOD_COUNTER = p_period_rec.period_counter;
4640 
4641          /*bug#15897249 starts */
4642          UPDATE FA_BOOKS
4643          SET    TERMINAL_GAIN_LOSS_FLAG = 'Y'
4644          WHERE  GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4645          AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4646          AND    TRANSACTION_HEADER_ID_OUT is null
4647          AND    tracking_method = 'CALCULATE'
4648          AND    recognize_gain_loss = 'NO'
4649          and    member_rollup_flag = 'Y';
4650          /*bug#15897249 ends */
4651 
4652       end if;
4653 
4654    end if; -- (p_asset_fin_rec.cost = 0) and
4655 
4656 
4657    if (p_log_level_rec.statement_level) then
4658       fa_debug_pkg.add(l_calling_fn, 'l_is_tgl_due', l_is_tgl_due, p_log_level_rec => p_log_level_rec);
4659       fa_debug_pkg.add(l_calling_fn, 'End', 'SUCCESS', p_log_level_rec => p_log_level_rec);
4660    end if;
4661 
4662    return TRUE;
4663 
4664 EXCEPTION
4665    WHEN tgl_err THEN
4666       if (p_log_level_rec.statement_level) then
4667          fa_debug_pkg.add(l_calling_fn, 'End', FALSE, p_log_level_rec => p_log_level_rec);
4668       end if;
4669 
4670       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4671       return FALSE;
4672 
4673    WHEN OTHERS THEN
4674       if (p_log_level_rec.statement_level) then
4675          fa_debug_pkg.add(l_calling_fn, 'End', FALSE, p_log_level_rec => p_log_level_rec);
4676       end if;
4677 
4678       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4679       return FALSE;
4680 
4681 END Check_Terminal_Gain_Loss;
4682 
4683 FUNCTION Do_Allocation(
4684                       p_trans_rec         IN     FA_API_TYPES.trans_rec_type,
4685                       p_asset_hdr_rec     IN     FA_API_TYPES.asset_hdr_rec_type,
4686                       p_asset_fin_rec     IN     FA_API_TYPES.asset_fin_rec_type,
4687                       p_asset_deprn_rec_new IN   FA_API_TYPES.asset_deprn_rec_type,
4688                       p_period_rec        IN     FA_API_TYPES.period_rec_type,
4689                       p_reserve_amount    IN     NUMBER,
4690                       p_mem_ret_thid      IN     NUMBER  DEFAULT NULL,
4691                       p_mode              IN     VARCHAR2 DEFAULT 'NORMAL',
4692                       p_mrc_sob_type_code IN     VARCHAR2,
4693                       p_calling_fn        IN     VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
4694 RETURN BOOLEAN IS
4695 
4696    l_calling_fn varchar2(40) := 'FA_RETIREMENT_PVT.Do_Allocation';
4697    /* bug 8633654 starts*/
4698    h_rein_mem_asset_id number;
4699    l_ind number;
4700    bln_rein_mem_reqd boolean := FALSE;
4701    bln_other_mem_exists boolean := FALSE;
4702    bln_use_cur_asset    boolean := TRUE;
4703    l_rein_mem_deprn_resv fa_books.adjusted_cost%type;
4704    l_rein_mem_adj_cost fa_books.adjusted_cost%type;
4705 
4706 
4707    CURSOR c_reins_mem_details IS
4708       SELECT BK.ASSET_ID                  ASSET_ID
4709            , BK.TRANSACTION_HEADER_ID_IN  TRANSACTION_HEADER_ID_IN
4710            , BK.RATE_ADJUSTMENT_FACTOR    RATE_ADJUSTMENT_FACTOR
4711            , BK.ADJUSTED_COST             ADJUSTED_COST
4712            , BK.RECOVERABLE_COST          RECOVERABLE_COST
4713       FROM   FA_BOOKS BK
4714            , FA_ADDITIONS_B AD
4715            , fa_transaction_headers th
4716       WHERE  BK.BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4717       AND    BK.TRANSACTION_HEADER_ID_OUT is null
4718       AND    BK.GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4719       AND    nvl(BK.CIP_COST, 0) = 0
4720       AND    BK.ASSET_ID = th.asset_id
4721       AND    th.transaction_header_id = p_mem_ret_thid
4722       AND    AD.ASSET_TYPE = 'CAPITALIZED';
4723 
4724    CURSOR c_mc_reins_mem_details IS
4725       SELECT BK.ASSET_ID                  ASSET_ID
4726            , BK.TRANSACTION_HEADER_ID_IN  TRANSACTION_HEADER_ID_IN
4727            , BK.RATE_ADJUSTMENT_FACTOR    RATE_ADJUSTMENT_FACTOR
4728            , BK.ADJUSTED_COST             ADJUSTED_COST
4729            , BK.RECOVERABLE_COST          RECOVERABLE_COST
4730       FROM   FA_MC_BOOKS BK
4731            , FA_ADDITIONS_B AD
4732            , fa_transaction_headers th
4733       WHERE  BK.BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4734       AND    BK.TRANSACTION_HEADER_ID_OUT is null
4735       AND    BK.GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4736       AND    nvl(BK.CIP_COST, 0) = 0
4737       AND    BK.ASSET_ID = th.asset_id
4738       AND    th.transaction_header_id = p_mem_ret_thid
4739       AND    AD.ASSET_TYPE = 'CAPITALIZED'
4740       AND    BK.SET_OF_BOOKS_ID = p_asset_hdr_rec.set_of_books_id;
4741 
4742    l_reins_mem_details c_reins_mem_details%rowtype;
4743 
4744    /* bug 8633654 ends*/
4745 
4746    CURSOR C_GET_SUM_REC_COST IS
4747       SELECT SUM(RECOVERABLE_COST)
4748       FROM   FA_BOOKS
4749       WHERE  BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4750       AND    TRANSACTION_HEADER_ID_OUT is null
4751       AND    PERIOD_COUNTER_FULLY_RESERVED is null
4752       AND    PERIOD_COUNTER_FULLY_RETIRED is null
4753       AND    nvl(CIP_COST, 0) = 0
4754       AND    GROUP_ASSET_ID = p_asset_hdr_rec.asset_id;
4755 
4756 
4757    CURSOR C_MC_GET_SUM_REC_COST IS
4758       SELECT SUM(RECOVERABLE_COST)
4759       FROM   FA_MC_BOOKS
4760       WHERE  BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4761       AND    TRANSACTION_HEADER_ID_OUT is null
4762       AND    PERIOD_COUNTER_FULLY_RESERVED is null
4763       AND    PERIOD_COUNTER_FULLY_RETIRED is null
4764       AND    nvl(CIP_COST, 0) = 0
4765       AND    GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4766       AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
4767 
4768    CURSOR C_GET_BS_SUM_ADJ_COST IS
4769       SELECT SUM(ADJUSTED_COST)
4770       FROM   FA_BOOKS_SUMMARY
4771       WHERE  BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4772       AND    TRANSACTION_HEADER_ID_OUT is null
4773       AND    nvl(FULLY_RESERVED_FLAG, 'N') = 'N'
4774       AND    nvl(FULLY_RETIRED_FLAG, 'N') = 'N'
4775       AND    GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4776       AND    nvl(CIP_COST, 0) = 0
4777       AND    PERIOD_COUNTER = p_period_rec.period_counter;
4778 
4779    CURSOR C_MC_GET_BS_SUM_ADJ_COST IS
4780       SELECT SUM(ADJUSTED_COST)
4781       FROM   FA_MC_BOOKS_SUMMARY
4782       WHERE  BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4783       AND    TRANSACTION_HEADER_ID_OUT is null
4784       AND    nvl(FULLY_RESERVED_FLAG, 'N') = 'N'
4785       AND    nvl(FULLY_RETIRED_FLAG, 'N') = 'N'
4786       AND    GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4787       AND    nvl(CIP_COST, 0) = 0
4788       AND    PERIOD_COUNTER = p_period_rec.period_counter
4789       AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
4790 
4791    CURSOR C_GET_GROUP_RSV_RET IS
4792       SELECT sum(decode(AJ.DEBIT_CREDIT_FLAG, 'CR', -1, 1)*AJ.ADJUSTMENT_AMOUNT)
4793       FROM   FA_ADJUSTMENTS AJ
4794       WHERE  AJ.ASSET_ID = p_asset_hdr_rec.asset_id
4795       AND    AJ.BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4796       AND    AJ.TRANSACTION_HEADER_ID = p_trans_rec.transaction_header_id;
4797 
4798    CURSOR C_MC_GET_GROUP_RSV_RET IS
4799       SELECT sum(decode(AJ.DEBIT_CREDIT_FLAG, 'CR', -1, 1)*AJ.ADJUSTMENT_AMOUNT)
4800       FROM   FA_ADJUSTMENTS AJ
4801       WHERE  AJ.ASSET_ID = p_asset_hdr_rec.asset_id
4802       AND    AJ.BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4803       AND    AJ.TRANSACTION_HEADER_ID = p_trans_rec.transaction_header_id;
4804 
4805 
4806    CURSOR C_GET_MEMBER_ASSETS IS
4807       SELECT BK.ASSET_ID                  ASSET_ID
4808            , BK.TRANSACTION_HEADER_ID_IN  TRANSACTION_HEADER_ID_IN
4809            , BK.RATE_ADJUSTMENT_FACTOR    RATE_ADJUSTMENT_FACTOR
4810            , BK.ADJUSTED_COST             ADJUSTED_COST
4811            , BK.CAPITALIZE_FLAG           CAPITALIZE_FLAG
4812            , BK.RECOVERABLE_COST          RECOVERABLE_COST
4813            , BK.ADJUSTED_RECOVERABLE_COST ADJUSTED_RECOVERABLE_COST
4814            , AD.ASSET_CATEGORY_ID         ASSET_CATEGORY_ID
4815            , AD.CURRENT_UNITS             CURRENT_UNITS
4816            , 'NO' REINS_ASSET_FLAG
4817       FROM   FA_BOOKS BK
4818            , FA_ADDITIONS_B AD
4819       WHERE  BK.BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4820       AND    BK.TRANSACTION_HEADER_ID_OUT is null
4821       AND    BK.GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4822       AND    nvl(BK.CIP_COST, 0) = 0
4823       AND    BK.PERIOD_COUNTER_FULLY_RESERVED is null
4824       AND    BK.PERIOD_COUNTER_FULLY_RETIRED is null
4825       AND    BK.ASSET_ID = AD.ASSET_ID
4826       AND    BK.ASSET_ID <> decode(p_mode,'RECURR',nvl(h_rein_mem_asset_id,-1),-1)
4827       AND    AD.ASSET_TYPE = 'CAPITALIZED'
4828       UNION ALL
4829       SELECT BK.ASSET_ID                  ASSET_ID
4830            , BK.TRANSACTION_HEADER_ID_IN  TRANSACTION_HEADER_ID_IN
4831            , BK.RATE_ADJUSTMENT_FACTOR    RATE_ADJUSTMENT_FACTOR
4832            , BK.ADJUSTED_COST             ADJUSTED_COST
4833            , BK.CAPITALIZE_FLAG           CAPITALIZE_FLAG
4834            , BK.RECOVERABLE_COST          RECOVERABLE_COST
4835            , BK.ADJUSTED_RECOVERABLE_COST ADJUSTED_RECOVERABLE_COST
4836            , AD.ASSET_CATEGORY_ID         ASSET_CATEGORY_ID
4837            , AD.CURRENT_UNITS             CURRENT_UNITS
4838            , 'YES' REINS_ASSET_FLAG
4839       FROM   FA_BOOKS BK
4840            , FA_ADDITIONS_B AD
4841       WHERE  BK.BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4842       AND    BK.TRANSACTION_HEADER_ID_OUT is null
4843       AND    BK.GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4844       AND    nvl(BK.CIP_COST, 0) = 0
4845       AND    BK.ASSET_ID = AD.ASSET_ID
4846       AND    BK.ASSET_ID = decode(p_mode,'RECURR',nvl(h_rein_mem_asset_id,-1),-1)
4847       AND    AD.ASSET_TYPE = 'CAPITALIZED'
4848       order by 10,1; --Please never change this Order By clause. If changed will cause huge corruption.
4849 
4850 
4851    CURSOR C_MC_GET_MEMBER_ASSETS IS
4852       SELECT BK.ASSET_ID                  ASSET_ID
4853            , BK.TRANSACTION_HEADER_ID_IN  TRANSACTION_HEADER_ID_IN
4854            , BK.RATE_ADJUSTMENT_FACTOR    RATE_ADJUSTMENT_FACTOR
4855            , BK.ADJUSTED_COST             ADJUSTED_COST
4856            , BK.CAPITALIZE_FLAG           CAPITALIZE_FLAG
4857            , BK.RECOVERABLE_COST          RECOVERABLE_COST
4858            , BK.ADJUSTED_RECOVERABLE_COST ADJUSTED_RECOVERABLE_COST
4859            , AD.ASSET_CATEGORY_ID         ASSET_CATEGORY_ID
4860            , AD.CURRENT_UNITS             CURRENT_UNITS
4861            , 'NO' REINS_ASSET_FLAG
4862       FROM   FA_MC_BOOKS BK
4863            , FA_ADDITIONS_B AD
4864       WHERE  BK.BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4865       AND    BK.TRANSACTION_HEADER_ID_OUT is null
4866       AND    BK.GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4867       AND    nvl(BK.CIP_COST, 0) = 0
4868       AND    BK.PERIOD_COUNTER_FULLY_RESERVED is null
4869       AND    BK.PERIOD_COUNTER_FULLY_RETIRED is null
4870       AND    BK.ASSET_ID = AD.ASSET_ID
4871       AND    BK.ASSET_ID <> decode(p_mode,'RECURR',nvl(h_rein_mem_asset_id,-1),-1)
4872       AND    AD.ASSET_TYPE = 'CAPITALIZED'
4873       AND    BK.SET_OF_BOOKS_ID = p_asset_hdr_rec.set_of_books_id
4874       UNION ALL
4875       SELECT BK.ASSET_ID                  ASSET_ID
4876            , BK.TRANSACTION_HEADER_ID_IN  TRANSACTION_HEADER_ID_IN
4877            , BK.RATE_ADJUSTMENT_FACTOR    RATE_ADJUSTMENT_FACTOR
4878            , BK.ADJUSTED_COST             ADJUSTED_COST
4879            , BK.CAPITALIZE_FLAG           CAPITALIZE_FLAG
4880            , BK.RECOVERABLE_COST          RECOVERABLE_COST
4881            , BK.ADJUSTED_RECOVERABLE_COST ADJUSTED_RECOVERABLE_COST
4882            , AD.ASSET_CATEGORY_ID         ASSET_CATEGORY_ID
4883            , AD.CURRENT_UNITS             CURRENT_UNITS
4884            , 'YES' REINS_ASSET_FLAG
4885       FROM   FA_MC_BOOKS BK
4886            , FA_ADDITIONS_B AD
4887       WHERE  BK.BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4888       AND    BK.TRANSACTION_HEADER_ID_OUT is null
4889       AND    BK.GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4890       AND    nvl(BK.CIP_COST, 0) = 0
4891       AND    BK.ASSET_ID = AD.ASSET_ID
4892       AND    BK.ASSET_ID = decode(p_mode,'RECURR',nvl(h_rein_mem_asset_id,-1),-1)
4893       AND    AD.ASSET_TYPE = 'CAPITALIZED'
4894       AND    BK.SET_OF_BOOKS_ID = p_asset_hdr_rec.set_of_books_id
4895       order by 10,1; --Please never change this Order By clause. If changed will cause huge corruption.
4896 
4897    /*Bug 9076882 - For MRC we need to retrieve the transaction headers from primary*/
4898    CURSOR C_THIDS (p_asset_id number,p_source_trans_id number) is
4899       SELECT min(transaction_header_id)
4900         FROM fa_transaction_headers fath
4901        WHERE fath.asset_id = p_asset_id
4902          and fath.source_transaction_header_id = p_source_trans_id;
4903 
4904    CURSOR C_THIDS_RECURR (p_asset_id number,p_source_trans_id number) is
4905       SELECT max(transaction_header_id)
4906         FROM fa_transaction_headers fath
4907        WHERE fath.asset_id = p_asset_id
4908          and fath.source_transaction_header_id = p_source_trans_id;
4909 
4910    CURSOR C_DIST_EXCESS IS
4911       SELECT ASSET_ID
4912            , TRANSACTION_HEADER_ID_IN
4913            , BK.RECOVERABLE_COST -
4914              (FA_UTIL_PVT.get_asset_deprn_resv(p_asset_hdr_rec.book_type_code,BK.ASSET_ID, p_asset_hdr_rec.set_of_books_id
4915                                               , null, 'P' ) )
4916              ADJUSTED_COST
4917       FROM   FA_BOOKS BK
4918       WHERE  BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4919       AND    TRANSACTION_HEADER_ID_OUT is null
4920       AND    GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4921       AND    PERIOD_COUNTER_FULLY_RESERVED is null
4922       AND    PERIOD_COUNTER_FULLY_RETIRED is null
4923       AND    nvl(CIP_COST, 0) = 0
4924       AND    (BK.RECOVERABLE_COST -
4925               (FA_UTIL_PVT.get_asset_deprn_resv(p_asset_hdr_rec.book_type_code,BK.ASSET_ID, p_asset_hdr_rec.set_of_books_id
4926                                               , null, 'P' ) )
4927              ) <> 0;
4928 
4929    CURSOR C_MC_DIST_EXCESS IS
4930       SELECT ASSET_ID
4931            , TRANSACTION_HEADER_ID_IN
4932            , BK.RECOVERABLE_COST -
4933            (FA_UTIL_PVT.get_asset_deprn_resv(p_asset_hdr_rec.book_type_code,BK.ASSET_ID, p_asset_hdr_rec.set_of_books_id
4934                                               , null, 'R' ) )
4935              ADJUSTED_COST
4936       FROM   FA_MC_BOOKS BK
4937       WHERE  BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
4938       AND    TRANSACTION_HEADER_ID_OUT is null
4939       AND    GROUP_ASSET_ID = p_asset_hdr_rec.asset_id
4940       AND    PERIOD_COUNTER_FULLY_RESERVED is null
4941       AND    PERIOD_COUNTER_FULLY_RETIRED is null
4942       AND    nvl(CIP_COST, 0) = 0
4943       AND    (BK.RECOVERABLE_COST -
4944               (FA_UTIL_PVT.get_asset_deprn_resv(p_asset_hdr_rec.book_type_code,BK.ASSET_ID, p_asset_hdr_rec.set_of_books_id
4945                                               , null, 'R' ) )
4946              ) <> 0
4947       AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
4948 
4949 
4950    -- Get old trx info for member and group
4951    CURSOR c_get_ret_member IS
4952       select th.asset_id
4953            , th.transaction_header_id
4954            , aj.debit_credit_flag
4955            , ad.current_units
4956            , ad.asset_category_id
4957            , bk.adjusted_recoverable_cost
4958            , sum(aj.adjustment_amount)
4959       from   fa_transaction_headers th    -- member
4960            , fa_transaction_headers gth -- group
4961            , fa_adjustments aj
4962            , fa_deprn_periods dp
4963            , fa_additions_b ad
4964            , fa_books bk
4965       where  th.book_type_code = p_asset_hdr_rec.book_type_code
4966 --      and    th.date_effective = gth.date_effective
4967       and    th.source_transaction_header_id = gth.transaction_header_id
4968       and    gth.book_type_code = p_asset_hdr_rec.book_type_code
4969       and    gth.member_transaction_header_id = p_mem_ret_thid
4970       and    aj.asset_id = th.asset_id
4971       and    aj.book_type_code = p_asset_hdr_rec.book_type_code
4972       and    aj.transaction_header_id  = th.transaction_header_id
4973       and    dp.book_type_code = p_asset_hdr_rec.book_type_code
4974       and    gth.date_effective between dp.period_open_date and nvl(dp.period_close_date, sysdate)
4975       and    dp.period_counter = aj.period_counter_created
4976       and    th.asset_id = ad.asset_id
4977       and    bk.asset_id = th.asset_id
4978       and    bk.book_type_code = p_asset_hdr_rec.book_type_code
4979       and    bk.group_asset_id = gth.asset_id
4980       and    bk.transaction_header_id_out is null
4981       group by th.asset_id
4982              , th.transaction_header_id
4983              , aj.debit_credit_flag
4984              , ad.current_units
4985              , ad.asset_category_id
4986              , bk.adjusted_recoverable_cost;
4987 
4988    /*Bug 9076882 Get old trx info for member and group*/
4989    CURSOR c_mc_get_ret_member IS
4990       select th.asset_id
4991            , th.transaction_header_id
4992            , aj.debit_credit_flag
4993            , ad.current_units
4994            , ad.asset_category_id
4995            , bk.adjusted_recoverable_cost
4996            , sum(aj.adjustment_amount)
4997       from   fa_transaction_headers th    -- member
4998            , fa_transaction_headers gth -- group
4999            , fa_mc_adjustments aj
5000            , fa_mc_deprn_periods dp
5001            , fa_additions_b ad
5002            , fa_mc_books bk
5003       where  th.book_type_code = p_asset_hdr_rec.book_type_code
5004 --      and    th.date_effective = gth.date_effective
5005       and    th.source_transaction_header_id = gth.transaction_header_id
5006       and    gth.book_type_code = p_asset_hdr_rec.book_type_code
5007       and    gth.member_transaction_header_id = p_mem_ret_thid
5008       and    aj.asset_id = th.asset_id
5009       and    aj.book_type_code = p_asset_hdr_rec.book_type_code
5010       and    aj.transaction_header_id  = th.transaction_header_id
5011       and    dp.book_type_code = p_asset_hdr_rec.book_type_code
5012       and    gth.date_effective between dp.period_open_date and nvl(dp.period_close_date, sysdate)
5013       and    dp.period_counter = aj.period_counter_created
5014       and    th.asset_id = ad.asset_id
5015       and    bk.asset_id = th.asset_id
5016       and    bk.book_type_code = p_asset_hdr_rec.book_type_code
5017       and    bk.group_asset_id = gth.asset_id
5018       and    bk.transaction_header_id_out is null
5019       and    bk.set_of_books_id = p_asset_hdr_rec.set_of_books_id
5020       and    aj.set_of_books_id = bk.set_of_books_id
5021       and    dp.set_of_books_id = aj.set_of_books_id
5022       group by th.asset_id
5023              , th.transaction_header_id
5024              , aj.debit_credit_flag
5025              , ad.current_units
5026              , ad.asset_category_id
5027              , bk.adjusted_recoverable_cost;
5028 
5029    CURSOR c_get_thid (c_asset_id number) IS
5030       select  transaction_header_id_in
5031       from    fa_books
5032       where book_type_code = p_asset_hdr_rec.book_type_code
5033       and   asset_id = c_asset_id
5034       and   transaction_header_id_out is null;
5035 
5036    CURSOR c_mc_get_thid (c_asset_id number) IS
5037       select  transaction_header_id_in
5038       from    fa_mc_books
5039       where book_type_code = p_asset_hdr_rec.book_type_code
5040       and   asset_id = c_asset_id
5041       and   transaction_header_id_out is null
5042       and   set_of_books_id = p_asset_hdr_rec.set_of_books_id;
5043 
5044    /*
5045    Bug 8633654
5046    cursor to find if any retirement adjustment happened against any
5047    member during retirement. If not, not need to reverse during
5048    reinstatement
5049    */
5050    CURSOR c_ret_adj_count IS
5051      select count(1)
5052      from   fa_transaction_headers th    -- member
5053           , fa_transaction_headers gth -- group
5054           , fa_adjustments aj
5055      where  th.book_type_code = p_asset_hdr_rec.book_type_code
5056      and    th.source_transaction_header_id = gth.transaction_header_id
5057      and    gth.book_type_code = p_asset_hdr_rec.book_type_code
5058      and    gth.member_transaction_header_id = p_mem_ret_thid
5059      and    aj.asset_id = th.asset_id
5060      and    aj.book_type_code = p_asset_hdr_rec.book_type_code
5061      and    aj.transaction_header_id  = th.transaction_header_id;
5062 
5063    TYPE tab_num15_type IS TABLE OF NUMBER(15) INDEX BY BINARY_INTEGER;
5064    TYPE tab_num_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
5065    TYPE tab_char3_type IS TABLE OF VARCHAR2(3) INDEX BY BINARY_INTEGER;
5066 
5067    l_adj              FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT;
5068    t_asset_id                    FA_NUM15_TBL_TYPE := FA_NUM15_TBL_TYPE(); --Bug 9076882
5069    t_transaction_header_id_in    tab_num15_type;
5070    t_rate_adjustment_factor      tab_num_type;
5071    t_recoverable_cost            tab_num_type;
5072    t_adjusted_cost               tab_num_type;
5073    t_capitalize_flag             tab_char3_type;
5074    t_adjusted_recoverable_cost   tab_num_type;
5075    t_asset_category_id           tab_num15_type;
5076    t_current_units               tab_num_type;
5077    t_thid                        tab_num15_type;
5078    t_new_adj_cost                tab_num_type;
5079    t_new_deprn_reserve           tab_num_type;
5080    t_adjustment_amount           tab_num_type;
5081    l_ret_adj_count               number := 0;
5082    l_actual_rsv_total            number := 0;
5083 
5084    t_debit_credit_flag           tab_char3_type;  -- reinstatement only
5085    t_reins_asset_flag            tab_char3_type;  -- RECURR only, bug 8633654
5086    l_th_count                    number := 0;
5087 
5088 
5089    l_last_asset_id    number(15);
5090    l_last_thid        number(15);
5091 
5092    l_sum_rec_cost     number;
5093    l_sum_adj_cost     number;
5094    l_group_rsv_ret    number;
5095 
5096    l_reallocate_amount number;
5097 
5098    -- Query balance parameters
5099    l_deprn_reserve         NUMBER;
5100    l_temp_num              NUMBER;
5101    l_temp_char             VARCHAR2(30);
5102    l_temp_bool             BOOLEAN;
5103 
5104    l_limit   BINARY_INTEGER := 500;
5105    allocate_err       EXCEPTION;
5106 
5107    l_loc   varchar2(245);
5108 
5109 BEGIN
5110 
5111    if (p_log_level_rec.statement_level) then
5112       fa_debug_pkg.add(l_calling_fn, 'Begin', p_asset_hdr_rec.asset_id, p_log_level_rec => p_log_level_rec);
5113       fa_debug_pkg.add(l_calling_fn, 'Transaction_key', p_trans_rec.transaction_key, p_log_level_rec => p_log_level_rec);
5114       fa_debug_pkg.add(l_calling_fn, 'p_mode', p_mode,p_log_level_rec);
5115       fa_debug_pkg.add(l_calling_fn, 'p_reserve_amount', p_reserve_amount,p_log_level_rec);
5116       fa_debug_pkg.add(l_calling_fn, 'p_mrc_sob_type_code', p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec);
5117       fa_debug_pkg.add(l_calling_fn, 'p_asset_hdr_rec.set_of_books_id', p_asset_hdr_rec.set_of_books_id, p_log_level_rec => p_log_level_rec);
5118    end if;
5119 
5120    if (p_reserve_amount = 0) then
5121       if (p_log_level_rec.statement_level) then
5122          fa_debug_pkg.add(l_calling_fn, 'Returning from do_allocation as p_reserve_amount is 0', '',p_log_level_rec);
5123       end if;
5124       return TRUE;
5125    end if;
5126 
5127    -- Prepare common FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT parameters
5128    l_adj.book_type_code          := p_asset_hdr_rec.book_type_code;
5129    l_adj.period_counter_created  := p_period_rec.period_counter;
5130    l_adj.period_counter_adjusted := p_period_rec.period_counter;
5131    l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
5132    l_adj.selection_retid         := 0;
5133    l_adj.leveling_flag           := TRUE;
5134    l_adj.flush_adj_flag          := TRUE;  -- FALSE;
5135    l_adj.last_update_date        := p_trans_rec.who_info.last_update_date;
5136    l_adj.gen_ccid_flag           := TRUE;
5137    l_adj.adjustment_type         := 'RESERVE';
5138    l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
5139    l_adj.track_member_flag       := 'Y';
5140    l_adj.source_type_code := 'ADJUSTMENT';
5141 
5142    l_adj.mrc_sob_type_code := p_mrc_sob_type_code;
5143    l_adj.set_of_books_id   := p_asset_hdr_rec.set_of_books_id;
5144    --
5145    -- If this is called from retirement, then we know
5146    -- debit/credit flag from p_reserve_amount
5147    --
5148    if (p_trans_rec.transaction_key = 'MR') or
5149       (p_mode = 'RECURR') then
5150 
5151       /* bug 8633654 starts */
5152       if (p_mode = 'RECURR') then
5153         if (p_mrc_sob_type_code = 'R') then
5154             open c_mc_reins_mem_details;
5155             fetch c_mc_reins_mem_details into l_reins_mem_details;
5156             close c_mc_reins_mem_details;
5157          else
5158             open c_reins_mem_details;
5159             fetch c_reins_mem_details into l_reins_mem_details;
5160             close c_reins_mem_details;
5161          end if;
5162          h_rein_mem_asset_id := l_reins_mem_details.asset_id;
5163          if (p_log_level_rec.statement_level) then
5164             fa_debug_pkg.add(l_calling_fn, 'h_rein_mem_asset_id', h_rein_mem_asset_id,p_log_level_rec);
5165          end if;
5166 
5167          fa_query_balances_pkg.query_balances(
5168                    X_asset_id => h_rein_mem_asset_id,
5169                    X_book => p_asset_hdr_rec.book_type_code,
5170                    X_period_ctr => 0,
5171                    X_dist_id => 0,
5172                    X_run_mode => 'STANDARD',
5173                    X_cost => l_temp_num,
5174                    X_deprn_rsv => l_rein_mem_deprn_resv,
5175                    X_reval_rsv => l_temp_num,
5176                    X_ytd_deprn => l_temp_num,
5177                    X_ytd_reval_exp => l_temp_num,
5178                    X_reval_deprn_exp => l_temp_num,
5179                    X_deprn_exp => l_temp_num,
5180                    X_reval_amo => l_temp_num,
5181                    X_prod => l_temp_num,
5182                    X_ytd_prod => l_temp_num,
5183                    X_ltd_prod => l_temp_num,
5184                    X_adj_cost => l_temp_num,
5185                    X_reval_amo_basis => l_temp_num,
5186                    X_bonus_rate => l_temp_num,
5187                    X_deprn_source_code => l_temp_char,
5188                    X_adjusted_flag => l_temp_bool,
5189                    X_transaction_header_id => -1,
5190                    X_bonus_deprn_rsv => l_temp_num,
5191                    X_bonus_ytd_deprn => l_temp_num,
5192                    X_bonus_deprn_amount => l_temp_num,
5193                    X_impairment_rsv => l_temp_num,
5194                    X_ytd_impairment => l_temp_num,
5195                    X_impairment_amount => l_temp_num,
5196                    X_capital_adjustment => l_temp_num,
5197                    X_general_fund => l_temp_num,
5198                    X_mrc_sob_type_code => p_mrc_sob_type_code,
5199                    X_set_of_books_id => p_asset_hdr_rec.set_of_books_id,
5200                    p_log_level_rec => p_log_level_rec);
5201          l_rein_mem_adj_cost := l_reins_mem_details.recoverable_cost - l_rein_mem_deprn_resv;
5202          if (p_log_level_rec.statement_level) then
5203             fa_debug_pkg.add(l_calling_fn, 'l_rein_mem_adj_cost', l_rein_mem_adj_cost,p_log_level_rec);
5204          end if;
5205       end if;
5206       /* bug 8633654 ends */
5207 
5208       --
5209       -- If this is called from retirement, then we know
5210       -- debit/credit flag from p_reserve_amount
5211       --
5212       if (p_reserve_amount > 0) then
5213          l_adj.debit_credit_flag := 'DR';
5214       else
5215          l_adj.debit_credit_flag := 'CR';
5216       end if;
5217 
5218       l_sum_adj_cost := p_asset_fin_rec.recoverable_cost - p_asset_deprn_rec_new.deprn_reserve -
5219                         p_reserve_amount - nvl(l_rein_mem_adj_cost,0);
5220 
5221       if (p_log_level_rec.statement_level) then
5222          fa_debug_pkg.add(l_calling_fn, 'group reserve', p_asset_deprn_rec_new.deprn_reserve, p_log_level_rec => p_log_level_rec);
5223          fa_debug_pkg.add(l_calling_fn, 'sum rec_cost', p_asset_fin_rec.recoverable_cost, p_log_level_rec => p_log_level_rec);
5224          fa_debug_pkg.add(l_calling_fn, 'p_reserve_amount', p_reserve_amount, p_log_level_rec => p_log_level_rec);
5225          fa_debug_pkg.add(l_calling_fn, 'l_rein_mem_adj_cost', l_rein_mem_adj_cost,p_log_level_rec => p_log_level_rec);
5226          fa_debug_pkg.add(l_calling_fn, 'l_sum_adj_cost', l_sum_adj_cost, p_log_level_rec => p_log_level_rec);
5227       end if;
5228 
5229 
5230       if (p_mrc_sob_type_code = 'R') then
5231          OPEN C_MC_GET_MEMBER_ASSETS;
5232       else
5233          OPEN C_GET_MEMBER_ASSETS;
5234       end if;
5235 
5236    elsif (p_trans_rec.transaction_key = 'MS') then
5237       /* Bug 8633654 */
5238       open c_ret_adj_count;
5239       fetch c_ret_adj_count into l_ret_adj_count;
5240       close c_ret_adj_count;
5241       IF (p_log_level_rec.statement_level) then
5242          fa_debug_pkg.add(l_calling_fn, 'l_ret_adj_count', l_ret_adj_count,p_log_level_rec);
5243       END IF;
5244       IF l_ret_adj_count = 0 THEN
5245          IF (p_log_level_rec.statement_level) then
5246             fa_debug_pkg.add(l_calling_fn, 'No allocation happened during retirement', ' ',p_log_level_rec);
5247             fa_debug_pkg.add(l_calling_fn, 'End of allocation', ' ',p_log_level_rec);
5248          END IF;
5249          return TRUE;
5250 
5251       END IF;
5252 
5253       if (p_mrc_sob_type_code <> 'R') then
5254          OPEN c_get_ret_member;
5255       else
5256          OPEN c_mc_get_ret_member;
5257       end if;
5258    end if;
5259 
5260    l_loc := 'Before Main Loop';
5261    LOOP -- MAIN OUTER LOOP
5262 
5263       t_thid.delete;
5264       t_asset_id.delete;
5265       t_transaction_header_id_in.delete;
5266       t_rate_adjustment_factor.delete;
5267       t_adjusted_cost.delete;
5268       t_capitalize_flag.delete;
5269       t_recoverable_cost.delete;
5270       t_adjusted_recoverable_cost.delete;
5271       t_asset_category_id.delete;
5272       t_current_units.delete;
5273       t_new_adj_cost.delete;
5274       t_adjustment_amount.delete;
5275       t_reins_asset_flag.delete;
5276 
5277       if (p_trans_rec.transaction_key = 'MR') or
5278          (p_mode = 'RECURR') then
5279          -- can this be update statement?
5280          if (p_mrc_sob_type_code = 'R') then
5281             FETCH C_MC_GET_MEMBER_ASSETS BULK COLLECT INTO t_asset_id
5282                                                       , t_transaction_header_id_in
5283                                                       , t_rate_adjustment_factor
5284                                                       , t_adjusted_cost
5285                                                       , t_capitalize_flag
5286                                                       , t_recoverable_cost
5287                                                       , t_adjusted_recoverable_cost
5288                                                       , t_asset_category_id
5289                                                       , t_current_units
5290                                                       , t_reins_asset_flag LIMIT l_limit;
5291          else
5292             FETCH C_GET_MEMBER_ASSETS BULK COLLECT INTO t_asset_id
5293                                                       , t_transaction_header_id_in
5294                                                       , t_rate_adjustment_factor
5295                                                       , t_adjusted_cost
5296                                                       , t_capitalize_flag
5297                                                       , t_recoverable_cost
5298                                                       , t_adjusted_recoverable_cost
5299                                                       , t_asset_category_id
5300                                                       , t_current_units
5301                                                       , t_reins_asset_flag LIMIT l_limit;
5302          end if;
5303 
5304          if (p_log_level_rec.statement_level) then
5305             fa_debug_pkg.add(l_calling_fn, 'In Main Cursor', t_asset_id.count, p_log_level_rec => p_log_level_rec);
5306             fa_debug_pkg.add(l_calling_fn, 'l_sum_adj_cost', l_sum_adj_cost, p_log_level_rec => p_log_level_rec);
5307          end if;
5308 
5309          if t_asset_id.count = 0 then
5310 
5311             if (p_mrc_sob_type_code = 'R') then
5312                CLOSE C_MC_GET_MEMBER_ASSETS;
5313             else
5314                CLOSE C_GET_MEMBER_ASSETS;
5315             end if;
5316             EXIT;
5317          end if;
5318 
5319       elsif (p_trans_rec.transaction_key = 'MS') then
5320 
5321          if (p_mrc_sob_type_code <> 'R') then --Bug 9076882
5322          FETCH c_get_ret_member BULK COLLECT INTO t_asset_id
5323                                                 , t_transaction_header_id_in
5324                                                 , t_debit_credit_flag
5325                                                 , t_current_units
5326                                                 , t_asset_category_id
5327                                                 , t_adjusted_recoverable_cost
5328                                                 , t_adjustment_amount
5329                                                   LIMIT l_limit;
5330 
5331          else
5332          FETCH c_mc_get_ret_member BULK COLLECT INTO t_asset_id
5333                                                 , t_transaction_header_id_in
5334                                                 , t_debit_credit_flag
5335                                                 , t_current_units
5336                                                 , t_asset_category_id
5337                                                 , t_adjusted_recoverable_cost
5338                                                 , t_adjustment_amount
5339                                                   LIMIT l_limit;
5340          end if;
5341          if t_asset_id.count = 0 then
5342 
5343             IF (p_mrc_sob_type_code <> 'R') THEN
5344                CLOSE c_get_ret_member;
5345                EXIT;
5346             ELSE
5347                CLOSE c_mc_get_ret_member;
5348                EXIT;
5349             END IF;
5350 
5351          end if;
5352 
5353       end if; -- (p_trans_rec.transaction_key = 'MR')
5354 
5355       l_loc := 'Before Insert TH';
5356       l_th_count := t_asset_id.last;
5357 
5358       /* Do not insert last record in RECURR mode and reinstated asset flag is YES */
5359 
5360       if(p_mode = 'RECURR' AND t_reins_asset_flag(l_th_count) = 'YES') THEN
5361          l_th_count := l_th_count-1;
5362       END IF;
5363 
5364       IF (p_mrc_sob_type_code <> 'R') THEN  --Bug 9076882 No need insert transaction headers for MRC
5365       FORALL i in t_asset_id.first..l_th_count
5366          INSERT INTO FA_TRANSACTION_HEADERS(
5367                          TRANSACTION_HEADER_ID
5368                        , BOOK_TYPE_CODE
5369                        , ASSET_ID
5370                        , TRANSACTION_TYPE_CODE
5371                        , TRANSACTION_DATE_ENTERED
5372                        , DATE_EFFECTIVE
5373                        , LAST_UPDATE_DATE
5374                        , LAST_UPDATED_BY
5375                        , SOURCE_TRANSACTION_HEADER_ID
5376                        , MASS_REFERENCE_ID
5377                        , LAST_UPDATE_LOGIN
5378                        , TRANSACTION_SUBTYPE
5379                        , TRANSACTION_KEY
5380                        , AMORTIZATION_START_DATE
5381                        , CALLING_INTERFACE
5382                        , MASS_TRANSACTION_ID
5383          ) VALUES (
5384                          FA_TRANSACTION_HEADERS_S.NEXTVAL
5385                        , p_asset_hdr_rec.book_type_code
5386                        , t_asset_id(i)
5387                        , 'ADJUSTMENT'
5388                        , p_trans_rec.transaction_date_entered
5389                        , p_trans_rec.who_info.last_update_date
5390                        , p_trans_rec.who_info.last_update_date
5391                        , p_trans_rec.who_info.last_updated_by
5392                        , p_trans_rec.transaction_header_id
5393                        , p_trans_rec.mass_reference_id
5394                        , p_trans_rec.who_info.last_update_login
5395                        , 'AMORTIZED'
5396                        , 'RA'
5397                        , p_trans_rec.amortization_start_date
5398                        , p_trans_rec.calling_interface
5399                        , p_trans_rec.mass_transaction_id
5400          ) RETURNING transaction_header_id BULK COLLECT INTO t_thid;
5401       ELSE --Fetch all primary transaction hedaers for mrc
5402          /* Bug 10087923, there are potential errors, mismatch in transaction headers if
5403          we use bulk collect here. Better to use individual cursor
5404          */
5405          FOR ind in t_asset_id.first..l_th_count
5406          loop
5407             if(p_mode <> 'RECURR') then
5408                OPEN C_THIDS (p_asset_id => t_asset_id(ind),p_source_trans_id => p_trans_rec.transaction_header_id);
5409                FETCH C_THIDS INTO t_thid(ind);
5410                CLOSE C_THIDS;
5411             else
5412                OPEN C_THIDS_RECURR (p_asset_id => t_asset_id(ind),p_source_trans_id => p_trans_rec.transaction_header_id);
5413                FETCH C_THIDS_RECURR INTO t_thid(ind);
5414                CLOSE C_THIDS_RECURR;
5415             end if;
5416          end loop;
5417       END IF;
5418 
5419 
5420       l_loc := 'Before ADJ LOOP';
5421       -- Prepare non-common FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT parameters and
5422       -- call faxinaj to create records in FA_ADJUSTMENTS table
5423       if (p_log_level_rec.statement_level) then
5424             fa_debug_pkg.add(l_calling_fn, 'Before ADJ LOOP', t_asset_id(1));
5425             fa_debug_pkg.add(l_calling_fn, 't_asset_id.last', t_asset_id.last, p_log_level_rec => p_log_level_rec);
5426       end if;
5427 
5428 
5429       FOR i in 1..t_asset_id.last LOOP
5430 
5431          if (p_log_level_rec.statement_level) then
5432              fa_debug_pkg.add(l_calling_fn, 'asset_id', t_asset_id(i),p_log_level_rec => p_log_level_rec);
5433             fa_debug_pkg.add(l_calling_fn, 'p_reserve_amount', p_reserve_amount, p_log_level_rec => p_log_level_rec);
5434             fa_debug_pkg.add(l_calling_fn, 'l_sum_adj_cost', l_sum_adj_cost, p_log_level_rec => p_log_level_rec);
5435          end if;
5436 
5437          if (p_trans_rec.transaction_key = 'MR') or
5438             (p_mode = 'RECURR') then
5439 
5440             if (p_log_level_rec.statement_level) then
5441                fa_debug_pkg.add(l_calling_fn, 'Call',
5442                                 'fa_query_balances_pkg.query_balances', p_log_level_rec => p_log_level_rec);
5443             end if;
5444             bln_use_cur_asset := FALSE;
5445             if (p_trans_rec.transaction_key = 'MR' OR (p_mode = 'RECURR' AND t_reins_asset_flag(i) = 'NO') ) THEN
5446                /*13805724 - No need to call query balance for retirement,fetched in cursor directly */
5447                if ((p_mode = 'RECURR' AND t_reins_asset_flag(i) = 'NO') ) THEN
5448                   bln_other_mem_exists := TRUE;
5449                   fa_query_balances_pkg.query_balances(
5450                       X_asset_id => t_asset_id(i),
5451                       X_book => p_asset_hdr_rec.book_type_code,
5452                       X_period_ctr => 0,
5453                       X_dist_id => 0,
5454                       X_run_mode => 'STANDARD',
5455                       X_cost => l_temp_num,
5456                       X_deprn_rsv => l_deprn_reserve,
5457                       X_reval_rsv => l_temp_num,
5458                       X_ytd_deprn => l_temp_num,
5459                       X_ytd_reval_exp => l_temp_num,
5460                       X_reval_deprn_exp => l_temp_num,
5461                       X_deprn_exp => l_temp_num,
5462                       X_reval_amo => l_temp_num,
5463                       X_prod => l_temp_num,
5464                       X_ytd_prod => l_temp_num,
5465                       X_ltd_prod => l_temp_num,
5466                       X_adj_cost => l_temp_num,
5467                       X_reval_amo_basis => l_temp_num,
5468                       X_bonus_rate => l_temp_num,
5469                       X_deprn_source_code => l_temp_char,
5470                       X_adjusted_flag => l_temp_bool,
5471                       X_transaction_header_id => -1,
5472                       X_bonus_deprn_rsv => l_temp_num,
5473                       X_bonus_ytd_deprn => l_temp_num,
5474                       X_bonus_deprn_amount => l_temp_num,
5475                       X_impairment_rsv => l_temp_num,
5476                       X_ytd_impairment => l_temp_num,
5477                       X_impairment_amount => l_temp_num,
5478                       X_capital_adjustment => l_temp_num,
5479                       X_general_fund => l_temp_num,
5480                       X_mrc_sob_type_code => p_mrc_sob_type_code,
5481                       X_set_of_books_id => p_asset_hdr_rec.set_of_books_id,
5482                       p_log_level_rec     => p_log_level_rec);
5483 
5484                   if (p_log_level_rec.statement_level) then
5485                      fa_debug_pkg.add(l_calling_fn, 't_asset_id('||to_char(i)||')',
5486                                    t_asset_id(i),p_log_level_rec);
5487                      fa_debug_pkg.add(l_calling_fn, 'l_deprn_reserve',
5488                                    l_deprn_reserve,p_log_level_rec);
5489                      fa_debug_pkg.add(l_calling_fn, 't_adjusted_cost('||to_char(i)||')',
5490                                    t_adjusted_cost(i),p_log_level_rec);
5491                      fa_debug_pkg.add(l_calling_fn, 't_recoverable_cost('||to_char(i)||')',
5492                                    t_recoverable_cost(i),p_log_level_rec);
5493                      fa_debug_pkg.add(l_calling_fn, 't_adjusted_recoverable_cost('||to_char(i)||')',
5494                                    t_adjusted_recoverable_cost(i),p_log_level_rec);
5495                      fa_debug_pkg.add(l_calling_fn, 'l_actual_rsv_total',
5496                                    l_actual_rsv_total,p_log_level_rec);
5497                   end if;
5498 
5499                   t_adjusted_cost(i) := t_recoverable_cost(i) - l_deprn_reserve;
5500 
5501                end if;
5502 
5503             if (p_log_level_rec.statement_level) then
5504                fa_debug_pkg.add(l_calling_fn, 't_adjusted_cost('||to_char(i)||')', t_adjusted_cost(i));
5505             end if;
5506 
5507             if t_adjusted_cost(i) <> 0 then
5508                   l_adj.adjustment_amount := abs(p_reserve_amount) * (t_adjusted_cost(i)/l_sum_adj_cost);
5509             else
5510                   l_adj.adjustment_amount := 0;
5511             end if;
5512 
5513             if not FA_UTILS_PKG.faxrnd(l_adj.adjustment_amount,
5514                                        p_asset_hdr_rec.book_type_code,
5515                                        p_asset_hdr_rec.set_of_books_id,
5516                                        p_log_level_rec => p_log_level_rec) then
5517                raise allocate_err;
5518             end if;
5519 
5520             t_adjustment_amount(i) := sign(p_reserve_amount) * -1 * l_adj.adjustment_amount;
5521 
5522 
5523                /* Bug 8633654 starts */
5524                if (t_adjusted_recoverable_cost(i) >= 0) then
5525                  if (t_adjusted_recoverable_cost(i) - l_deprn_reserve - t_adjustment_amount(i) < 0) then
5526                    l_adj.adjustment_amount := t_adjusted_recoverable_cost(i) - l_deprn_reserve;
5527                    bln_rein_mem_reqd := TRUE;
5528                  end if;
5529                else
5530                  if (t_adjusted_recoverable_cost(i) - l_deprn_reserve - t_adjustment_amount(i) > 0) then
5531                    l_adj.adjustment_amount := t_adjusted_recoverable_cost(i) - l_deprn_reserve;
5532                    bln_rein_mem_reqd := TRUE;
5533                  end if;
5534                end if;
5535                bln_use_cur_asset := TRUE;
5536                /* Bug 8633654 ends */
5537 
5538                if (p_log_level_rec.statement_level) then
5539                   fa_debug_pkg.add(l_calling_fn, 'new l_adj.adjustment_amount',
5540                                    l_adj.adjustment_amount,p_log_level_rec);
5541                end if;
5542 
5543                l_actual_rsv_total := l_actual_rsv_total + sign(p_reserve_amount) * l_adj.adjustment_amount;
5544                t_adjustment_amount(i) := sign(p_reserve_amount) * -1 * l_adj.adjustment_amount;
5545 
5546             /* Bug 8633654 starts */
5547             elsif (p_mode = 'RECURR' AND t_reins_asset_flag(i) = 'YES') THEN
5548                if (p_log_level_rec.statement_level) then
5549                   fa_debug_pkg.add(l_calling_fn, 'Reinstated member','start',p_log_level_rec);
5550                end if;
5551                if (bln_rein_mem_reqd OR (NOT bln_other_mem_exists) ) THEN
5552                   if (p_log_level_rec.statement_level) then
5553                      fa_debug_pkg.add(l_calling_fn, 'Reinstated member','allocate remaining',p_log_level_rec);
5554                   end if;
5555                   bln_rein_mem_reqd := TRUE;
5556                   bln_use_cur_asset := TRUE;
5557                   l_adj.adjustment_amount := abs(p_reserve_amount - l_actual_rsv_total);
5558                   t_adjustment_amount(i) := sign(p_reserve_amount) * -1 * l_adj.adjustment_amount;
5559                   if (p_log_level_rec.statement_level) then
5560                      fa_debug_pkg.add(l_calling_fn, 't_asset_id('||to_char(i)||')',
5561                                       t_asset_id(i),p_log_level_rec);
5562                      fa_debug_pkg.add(l_calling_fn, 'l_rein_mem_deprn_resv',
5563                                       l_rein_mem_deprn_resv,p_log_level_rec);
5564                      fa_debug_pkg.add(l_calling_fn, 't_adjusted_recoverable_cost('||to_char(i)||')',
5565                                       t_adjusted_recoverable_cost(i),p_log_level_rec);
5566                      fa_debug_pkg.add(l_calling_fn, 't_adjustment_amount('||to_char(i)||')',
5567                                       t_adjustment_amount(i),p_log_level_rec);
5568                   end if;
5569 
5570                   if (t_adjusted_recoverable_cost(i) >= 0) then
5571                      if (t_adjusted_recoverable_cost(i) - l_rein_mem_deprn_resv - t_adjustment_amount(i) < 0) then
5572                         fa_srvr_msg.add_message(
5573                         calling_fn => l_calling_fn,
5574                         name       => 'FA_EGY_REINS_NOT_POSSIBLE',
5575                         p_log_level_rec => p_log_level_rec);
5576                         raise allocate_err;
5577                      end if;
5578                   else
5579                      if (t_adjusted_recoverable_cost(i) - l_rein_mem_deprn_resv - t_adjustment_amount(i) > 0) then
5580                         fa_srvr_msg.add_message(
5581                         calling_fn => l_calling_fn,
5582                         name       => 'FA_EGY_REINS_NOT_POSSIBLE',
5583                         p_log_level_rec => p_log_level_rec);
5584                         raise allocate_err;
5585                      end if;
5586                   end if;
5587 
5588                   IF (p_mrc_sob_type_code <> 'R') THEN
5589                      --Bug 9076882. No need to insert transaction for MRC.This is already fethced above using c_thids cursor
5590 
5591                      select FA_TRANSACTION_HEADERS_S.NEXTVAL
5592                      into   t_thid(i)
5593                      from   dual;
5594 
5595                      /* Insert record into fa_transaction_headers */
5596                      INSERT INTO FA_TRANSACTION_HEADERS(
5597                                      TRANSACTION_HEADER_ID
5598                                    , BOOK_TYPE_CODE
5599                                    , ASSET_ID
5600                                    , TRANSACTION_TYPE_CODE
5601                                    , TRANSACTION_DATE_ENTERED
5602                                    , DATE_EFFECTIVE
5603                                    , LAST_UPDATE_DATE
5604                                    , LAST_UPDATED_BY
5605                                    , SOURCE_TRANSACTION_HEADER_ID
5606                                    , MASS_REFERENCE_ID
5607                                    , LAST_UPDATE_LOGIN
5608                                    , TRANSACTION_SUBTYPE
5609                                    , TRANSACTION_KEY
5610                                    , AMORTIZATION_START_DATE
5611                                    , CALLING_INTERFACE
5612                                    , MASS_TRANSACTION_ID
5613                      ) VALUES (
5614                                      t_thid(i)
5615                                    , p_asset_hdr_rec.book_type_code
5616                                    , t_asset_id(i)
5617                                    , 'ADJUSTMENT'
5618                                    , p_trans_rec.transaction_date_entered
5619                                    , p_trans_rec.who_info.last_update_date
5620                                    , p_trans_rec.who_info.last_update_date
5621                                    , p_trans_rec.who_info.last_updated_by
5622                                    , p_trans_rec.transaction_header_id
5623                                    , p_trans_rec.mass_reference_id
5624                                    , p_trans_rec.who_info.last_update_login
5625                                    , 'AMORTIZED'
5626                                    , 'RA'
5627                                    , p_trans_rec.amortization_start_date
5628                                    , p_trans_rec.calling_interface
5629                                    , p_trans_rec.mass_transaction_id
5630                      );
5631                   ELSE
5632                      OPEN C_THIDS_RECURR (p_asset_id => t_asset_id(i),p_source_trans_id => p_trans_rec.transaction_header_id);
5633                      FETCH C_THIDS_RECURR INTO t_thid(i);
5634                      CLOSE C_THIDS_RECURR;
5635                   END IF;
5636 
5637                   l_actual_rsv_total := p_reserve_amount;
5638 
5639                   if (p_log_level_rec.statement_level) then
5640                      fa_debug_pkg.add(l_calling_fn, 'l_adj.adjustment_amount',l_adj.adjustment_amount,p_log_level_rec);
5641                      fa_debug_pkg.add(l_calling_fn, 't_adjustment_amount('||i||')',t_adjustment_amount(i),p_log_level_rec);
5642                   end if;
5643                end if;
5644             /* Bug 8633654 ends */
5645             end if;
5646 
5647          elsif (p_trans_rec.transaction_key = 'MS') then
5648 
5649             if (p_mrc_sob_type_code = 'R') then
5650                OPEN c_mc_get_thid(t_asset_id(i));
5651                FETCH c_mc_get_thid INTO t_transaction_header_id_in(i);
5652                CLOSE c_mc_get_thid;
5653 
5654             else
5655 
5656                OPEN c_get_thid(t_asset_id(i));
5657                FETCH c_get_thid INTO t_transaction_header_id_in(i);
5658                CLOSE c_get_thid;
5659 
5660             end if;
5661 
5662             fa_query_balances_pkg.query_balances(
5663                       X_asset_id => t_asset_id(i),
5664                       X_book => p_asset_hdr_rec.book_type_code,
5665                       X_period_ctr => 0,
5666                       X_dist_id => 0,
5667                       X_run_mode => 'STANDARD',
5668                       X_cost => l_temp_num,
5669                       X_deprn_rsv => l_deprn_reserve,
5670                       X_reval_rsv => l_temp_num,
5671                       X_ytd_deprn => l_temp_num,
5672                       X_ytd_reval_exp => l_temp_num,
5673                       X_reval_deprn_exp => l_temp_num,
5674                       X_deprn_exp => l_temp_num,
5675                       X_reval_amo => l_temp_num,
5676                       X_prod => l_temp_num,
5677                       X_ytd_prod => l_temp_num,
5678                       X_ltd_prod => l_temp_num,
5679                       X_adj_cost => l_temp_num,
5680                       X_reval_amo_basis => l_temp_num,
5681                       X_bonus_rate => l_temp_num,
5682                       X_deprn_source_code => l_temp_char,
5683                       X_adjusted_flag => l_temp_bool,
5684                       X_transaction_header_id => -1,
5685                       X_bonus_deprn_rsv => l_temp_num,
5686                       X_bonus_ytd_deprn => l_temp_num,
5687                       X_bonus_deprn_amount => l_temp_num,
5688                       X_impairment_rsv => l_temp_num,
5689                       X_ytd_impairment => l_temp_num,
5690                       X_impairment_amount => l_temp_num,
5691                       X_capital_adjustment => l_temp_num,
5692                       X_general_fund => l_temp_num,
5693                       X_mrc_sob_type_code => p_mrc_sob_type_code,
5694                       X_set_of_books_id => p_asset_hdr_rec.set_of_books_id,
5695                       p_log_level_rec     => p_log_level_rec);
5696 
5697             if (p_log_level_rec.statement_level) then
5698                fa_debug_pkg.add(l_calling_fn, 'l_deprn_reserve',
5699                                 l_deprn_reserve, p_log_level_rec => p_log_level_rec);
5700                fa_debug_pkg.add(l_calling_fn, 't_adjusted_recoverable_cost('||to_char(i)||')',
5701                                 t_adjusted_recoverable_cost(i));
5702             end if;
5703 
5704             t_adjusted_cost(i) := t_adjusted_recoverable_cost(i) - l_deprn_reserve;
5705 
5706             /*8425794 - Start - 1*/
5707             if (t_debit_credit_flag(i) = 'CR') then
5708                t_adjustment_amount(i) := -1 * t_adjustment_amount(i);
5709             end if;
5710             /*8425794 - End - 1*/
5711 
5712             -- Check to see if the asset can take entire reserve reinstated or not.
5713             -- If not, it takes as much as it can.
5714             if (t_adjusted_recoverable_cost(i) >= 0) then
5715                if (t_adjusted_recoverable_cost(i) - l_deprn_reserve - t_adjustment_amount(i) < 0) then
5716                   t_adjustment_amount(i) := t_adjusted_recoverable_cost(i) - l_deprn_reserve;
5717                end if;
5718             else
5719                 if (t_adjusted_recoverable_cost(i) - l_deprn_reserve - t_adjustment_amount(i) > 0) then
5720                     t_adjustment_amount(i) := t_adjusted_recoverable_cost(i) - l_deprn_reserve;
5721                 end if;
5722             end if;
5723 
5724             if (t_debit_credit_flag(i) = 'DR') then
5725                l_adj.adjustment_amount := t_adjustment_amount(i);
5726                l_adj.debit_credit_flag := 'CR';
5727             else
5728                t_adjustment_amount(i) := -1 * t_adjustment_amount(i);
5729                l_adj.adjustment_amount := t_adjustment_amount(i);
5730                l_adj.debit_credit_flag := 'DR';
5731             end if;
5732 
5733             /*8425794 - Start - 2*/
5734             if (p_reserve_amount >= 0) then
5735                 l_actual_rsv_total := l_actual_rsv_total + l_adj.adjustment_amount;
5736             else
5737                l_actual_rsv_total := l_actual_rsv_total - l_adj.adjustment_amount;
5738             end if;
5739             /*8425794 - End - 2*/
5740 
5741 
5742 
5743 
5744 
5745          end if;
5746 
5747          if (p_log_level_rec.statement_level) then
5748             fa_debug_pkg.add(l_calling_fn, 'Recapture Excess Reserve',
5749                              l_adj.adjustment_amount, p_log_level_rec => p_log_level_rec);
5750          end if;
5751 
5752          IF(bln_use_cur_asset) THEN
5753 
5754          l_adj.asset_id                := t_asset_id(i);
5755          l_adj.transaction_header_id   := t_thid(i);
5756          l_adj.current_units           := t_current_units(i);
5757 
5758          if not fa_cache_pkg.fazccb(p_asset_hdr_rec.book_type_code,
5759                                     t_asset_category_id(i),
5760                                     p_log_level_rec) then
5761             fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5762             raise allocate_err;
5763          end if;
5764 
5765          l_adj.account           := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
5766 
5767          if not FA_INS_ADJUST_PKG.faxinaj
5768                             (l_adj,
5769                              p_trans_rec.who_info.last_update_date,
5770                              p_trans_rec.who_info.last_updated_by,
5771                              p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
5772             raise allocate_err;
5773          end if;
5774 --tk_util.debug('finish adj');
5775 
5776          l_last_asset_id := t_asset_id(i);
5777          l_last_thid     := t_thid(i);
5778          END IF;
5779       END LOOP;
5780       /* Bug 8633654 starts (delete last record when it is not required)*/
5781 
5782       IF (p_mode = 'RECURR' and (NOT bln_rein_mem_reqd)) THEN
5783          if (p_log_level_rec.statement_level) then
5784             fa_debug_pkg.add(l_calling_fn, 'Delete Reinstated member from PL-SQL','CHECK',p_log_level_rec);
5785          end if;
5786          l_loc := 'Before PL-SQL Delete';
5787 
5788          IF (t_asset_id(t_asset_id.last) = h_rein_mem_asset_id) THEN
5789             if (p_log_level_rec.statement_level) then
5790                fa_debug_pkg.add(l_calling_fn, 't_asset_id(t_asset_id.last)',t_asset_id(t_asset_id.last),p_log_level_rec);
5791             end if;
5792 
5793             l_ind := t_thid.last;
5794             t_asset_id.delete(t_asset_id.last);
5795             t_transaction_header_id_in.delete(t_transaction_header_id_in.last);
5796             t_rate_adjustment_factor.delete(t_rate_adjustment_factor.last);
5797             t_adjusted_cost.delete(t_adjusted_cost.last);
5798             t_capitalize_flag.delete(t_capitalize_flag.last);
5799             t_recoverable_cost.delete(t_recoverable_cost.last);
5800             t_adjusted_recoverable_cost.delete(t_adjusted_recoverable_cost.last);
5801             t_asset_category_id.delete(t_asset_category_id.last);
5802             t_current_units.delete(t_current_units.last);
5803             t_reins_asset_flag.delete(t_reins_asset_flag.last);
5804          END IF;
5805          if (p_log_level_rec.statement_level) then
5806             fa_debug_pkg.add(l_calling_fn, 'Delete Reinstated member from PL-SQL','Done',p_log_level_rec);
5807          end if;
5808       END IF;
5809 
5810       /* Bug 8633654 ends*/
5811 
5812       if (p_mrc_sob_type_code = 'R') then
5813          l_loc := 'Before MRC BS';
5814          -- Update FA_MC_BOOKS_SUMAMRY table with new reserve adjustment amount and
5815          -- related columns.  Return new adjusted_cost and deprn reserve for later use
5816          FORALL i in t_asset_id.first..t_asset_id.last
5817             UPDATE FA_MC_BOOKS_SUMMARY
5818             SET    RESERVE_ADJUSTMENT_AMOUNT = RESERVE_ADJUSTMENT_AMOUNT + t_adjustment_amount(i)
5819                  , DEPRN_RESERVE = DEPRN_RESERVE + t_adjustment_amount(i)
5820                  , ADJUSTED_COST = RECOVERABLE_COST - DEPRN_RESERVE - t_adjustment_amount(i)
5821             WHERE  ASSET_ID = t_asset_id(i)
5822             AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
5823             AND    PERIOD_COUNTER = p_period_rec.period_counter
5824             AND    TRANSACTION_HEADER_ID_OUT IS NULL
5825             AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
5826 
5827          l_loc := 'Before MRC Books';
5828          -- Deactivate FA_BOOKS row for processed assets
5829          FORALL i in t_asset_id.first..t_asset_id.last
5830             UPDATE FA_MC_BOOKS
5831             SET    DATE_INEFFECTIVE = p_trans_rec.who_info.last_update_date
5832                  , TRANSACTION_HEADER_ID_OUT = t_thid(i)
5833             WHERE  TRANSACTION_HEADER_ID_IN = t_transaction_header_id_in(i)
5834               AND  set_of_books_id = p_asset_hdr_rec.set_of_books_id;
5835 
5836          l_loc := 'Before MRC Insert Books';
5837          -- Insert new FA_BOOKS records for processed assets
5838          FORALL i in t_asset_id.first..t_asset_id.last
5839             INSERT INTO FA_MC_BOOKS( BOOK_TYPE_CODE
5840                                       , ASSET_ID
5841                                       , DATE_PLACED_IN_SERVICE
5842                                       , DATE_EFFECTIVE
5843                                       , DEPRN_START_DATE
5844                                       , DEPRN_METHOD_CODE
5845                                       , LIFE_IN_MONTHS
5846                                       , RATE_ADJUSTMENT_FACTOR
5847                                       , ADJUSTED_COST
5848                                       , COST
5849                                       , ORIGINAL_COST
5850                                       , SALVAGE_VALUE
5851                                       , PRORATE_CONVENTION_CODE
5852                                       , PRORATE_DATE
5853                                       , COST_CHANGE_FLAG
5854                                       , ADJUSTMENT_REQUIRED_STATUS
5855                                       , CAPITALIZE_FLAG
5856                                       , RETIREMENT_PENDING_FLAG
5857                                       , DEPRECIATE_FLAG
5858                                       , LAST_UPDATE_DATE
5859                                       , LAST_UPDATED_BY
5860                                       , TRANSACTION_HEADER_ID_IN
5861                                       , ITC_AMOUNT_ID
5862                                       , ITC_AMOUNT
5863                                       , RETIREMENT_ID
5864                                       , TAX_REQUEST_ID
5865                                       , ITC_BASIS
5866                                       , BASIC_RATE
5867                                       , ADJUSTED_RATE
5868                                       , BONUS_RULE
5869                                       , CEILING_NAME
5870                                       , RECOVERABLE_COST
5871                                       , LAST_UPDATE_LOGIN
5872                                       , ADJUSTED_CAPACITY
5873                                       , FULLY_RSVD_REVALS_COUNTER
5874                                       , IDLED_FLAG
5875                                       , PERIOD_COUNTER_CAPITALIZED
5876                                       , PERIOD_COUNTER_FULLY_RESERVED
5877                                       , PERIOD_COUNTER_FULLY_RETIRED
5878                                       , PRODUCTION_CAPACITY
5879                                       , REVAL_AMORTIZATION_BASIS
5880                                       , REVAL_CEILING
5881                                       , UNIT_OF_MEASURE
5882                                       , UNREVALUED_COST
5883                                       , ANNUAL_DEPRN_ROUNDING_FLAG
5884                                       , PERCENT_SALVAGE_VALUE
5885                                       , ALLOWED_DEPRN_LIMIT
5886                                       , ALLOWED_DEPRN_LIMIT_AMOUNT
5887                                       , PERIOD_COUNTER_LIFE_COMPLETE
5888                                       , ADJUSTED_RECOVERABLE_COST
5889                                       , ANNUAL_ROUNDING_FLAG
5890                                       , GLOBAL_ATTRIBUTE1
5891                                       , GLOBAL_ATTRIBUTE2
5892                                       , GLOBAL_ATTRIBUTE3
5893                                       , GLOBAL_ATTRIBUTE4
5894                                       , GLOBAL_ATTRIBUTE5
5895                                       , GLOBAL_ATTRIBUTE6
5896                                       , GLOBAL_ATTRIBUTE7
5897                                       , GLOBAL_ATTRIBUTE8
5898                                       , GLOBAL_ATTRIBUTE9
5899                                       , GLOBAL_ATTRIBUTE10
5900                                       , GLOBAL_ATTRIBUTE11
5901                                       , GLOBAL_ATTRIBUTE12
5902                                       , GLOBAL_ATTRIBUTE13
5903                                       , GLOBAL_ATTRIBUTE14
5904                                       , GLOBAL_ATTRIBUTE15
5905                                       , GLOBAL_ATTRIBUTE16
5906                                       , GLOBAL_ATTRIBUTE17
5907                                       , GLOBAL_ATTRIBUTE18
5908                                       , GLOBAL_ATTRIBUTE19
5909                                       , GLOBAL_ATTRIBUTE20
5910                                       , GLOBAL_ATTRIBUTE_CATEGORY
5911                                       , EOFY_ADJ_COST
5912                                       , EOFY_FORMULA_FACTOR
5913                                       , SHORT_FISCAL_YEAR_FLAG
5914                                       , CONVERSION_DATE
5915                                       , ORIGINAL_DEPRN_START_DATE
5916                                       , REMAINING_LIFE1
5917                                       , REMAINING_LIFE2
5918                                       , OLD_ADJUSTED_COST
5919                                       , FORMULA_FACTOR
5920                                       , GROUP_ASSET_ID
5921                                       , SALVAGE_TYPE
5922                                       , DEPRN_LIMIT_TYPE
5923                                       , REDUCTION_RATE
5924                                       , REDUCE_ADDITION_FLAG
5925                                       , REDUCE_ADJUSTMENT_FLAG
5926                                       , REDUCE_RETIREMENT_FLAG
5927                                       , RECOGNIZE_GAIN_LOSS
5928                                       , RECAPTURE_RESERVE_FLAG
5929                                       , LIMIT_PROCEEDS_FLAG
5930                                       , TERMINAL_GAIN_LOSS
5931                                       , TRACKING_METHOD
5932                                       , EXCLUDE_FULLY_RSV_FLAG
5933                                       , EXCESS_ALLOCATION_OPTION
5934                                       , DEPRECIATION_OPTION
5935                                       , MEMBER_ROLLUP_FLAG
5936                                       , ALLOCATE_TO_FULLY_RSV_FLAG
5937                                       , ALLOCATE_TO_FULLY_RET_FLAG
5938                                       , TERMINAL_GAIN_LOSS_AMOUNT
5939                                       , CIP_COST
5940                                       , YTD_PROCEEDS
5941                                       , LTD_PROCEEDS
5942                                       , LTD_COST_OF_REMOVAL
5943                                       , EOFY_RESERVE
5944                                       , PRIOR_EOFY_RESERVE
5945                                       , EOP_ADJ_COST
5946                                       , EOP_FORMULA_FACTOR
5947                                       , EXCLUDE_PROCEEDS_FROM_BASIS
5948                                       , RETIREMENT_DEPRN_OPTION
5949                                       , TERMINAL_GAIN_LOSS_FLAG
5950                                       , SUPER_GROUP_ID
5951                                       , OVER_DEPRECIATE_OPTION
5952                                       , DISABLED_FLAG
5953                                       , SET_OF_BOOKS_ID
5954             ) SELECT BOOK_TYPE_CODE
5955                    , ASSET_ID
5956                    , DATE_PLACED_IN_SERVICE
5957                    , p_trans_rec.who_info.last_update_date -- DATE_EFFECTIVE
5958                    , DEPRN_START_DATE
5959                    , DEPRN_METHOD_CODE
5960                    , LIFE_IN_MONTHS
5961                    , RATE_ADJUSTMENT_FACTOR
5962                    , t_adjusted_cost(i) - t_adjustment_amount(i)
5963                    , COST
5964                    , ORIGINAL_COST
5965                    , SALVAGE_VALUE
5966                    , PRORATE_CONVENTION_CODE
5967                    , PRORATE_DATE
5968                    , COST_CHANGE_FLAG
5969                    , ADJUSTMENT_REQUIRED_STATUS
5970                    , CAPITALIZE_FLAG
5971                    , RETIREMENT_PENDING_FLAG
5972                    , DEPRECIATE_FLAG
5973                    , p_trans_rec.who_info.last_update_date -- LAST_UPDATE_DATE
5974                    , p_trans_rec.who_info.last_updated_by -- LAST_UPDATED_BY
5975                    , t_thid(i) -- TRANSACTION_HEADER_ID_IN
5976                    , ITC_AMOUNT_ID
5977                    , ITC_AMOUNT
5978                    , RETIREMENT_ID
5979                    , TAX_REQUEST_ID
5980                    , ITC_BASIS
5981                    , BASIC_RATE
5982                    , ADJUSTED_RATE
5983                    , BONUS_RULE
5984                    , CEILING_NAME
5985                    , RECOVERABLE_COST
5986                    , p_trans_rec.who_info.last_update_login -- LAST_UPDATE_LOGIN
5987                    , ADJUSTED_CAPACITY
5988                    , FULLY_RSVD_REVALS_COUNTER
5989                    , IDLED_FLAG
5990                    , PERIOD_COUNTER_CAPITALIZED
5991                    , PERIOD_COUNTER_FULLY_RESERVED
5992                    , PERIOD_COUNTER_FULLY_RETIRED
5993                    , PRODUCTION_CAPACITY
5994                    , REVAL_AMORTIZATION_BASIS
5995                    , REVAL_CEILING
5996                    , UNIT_OF_MEASURE
5997                    , UNREVALUED_COST
5998                    , ANNUAL_DEPRN_ROUNDING_FLAG
5999                    , PERCENT_SALVAGE_VALUE
6000                    , ALLOWED_DEPRN_LIMIT
6001                    , ALLOWED_DEPRN_LIMIT_AMOUNT
6002                    , PERIOD_COUNTER_LIFE_COMPLETE
6003                    , ADJUSTED_RECOVERABLE_COST
6004                    , ANNUAL_ROUNDING_FLAG
6005                    , GLOBAL_ATTRIBUTE1
6006                    , GLOBAL_ATTRIBUTE2
6007                    , GLOBAL_ATTRIBUTE3
6008                    , GLOBAL_ATTRIBUTE4
6009                    , GLOBAL_ATTRIBUTE5
6010                    , GLOBAL_ATTRIBUTE6
6011                    , GLOBAL_ATTRIBUTE7
6012                    , GLOBAL_ATTRIBUTE8
6013                    , GLOBAL_ATTRIBUTE9
6014                    , GLOBAL_ATTRIBUTE10
6015                    , GLOBAL_ATTRIBUTE11
6016                    , GLOBAL_ATTRIBUTE12
6017                    , GLOBAL_ATTRIBUTE13
6018                    , GLOBAL_ATTRIBUTE14
6019                    , GLOBAL_ATTRIBUTE15
6020                    , GLOBAL_ATTRIBUTE16
6021                    , GLOBAL_ATTRIBUTE17
6022                    , GLOBAL_ATTRIBUTE18
6023                    , GLOBAL_ATTRIBUTE19
6024                    , GLOBAL_ATTRIBUTE20
6025                    , GLOBAL_ATTRIBUTE_CATEGORY
6026                    , EOFY_ADJ_COST
6027                    , EOFY_FORMULA_FACTOR
6028                    , SHORT_FISCAL_YEAR_FLAG
6029                    , CONVERSION_DATE
6030                    , ORIGINAL_DEPRN_START_DATE
6031                    , REMAINING_LIFE1
6032                    , REMAINING_LIFE2
6033                    , OLD_ADJUSTED_COST
6034                    , FORMULA_FACTOR
6035                    , GROUP_ASSET_ID
6036                    , SALVAGE_TYPE
6037                    , DEPRN_LIMIT_TYPE
6038                    , REDUCTION_RATE
6039                    , REDUCE_ADDITION_FLAG
6040                    , REDUCE_ADJUSTMENT_FLAG
6041                    , REDUCE_RETIREMENT_FLAG
6042                    , RECOGNIZE_GAIN_LOSS
6043                    , RECAPTURE_RESERVE_FLAG
6044                    , LIMIT_PROCEEDS_FLAG
6045                    , TERMINAL_GAIN_LOSS
6046                    , TRACKING_METHOD
6047                    , EXCLUDE_FULLY_RSV_FLAG
6048                    , EXCESS_ALLOCATION_OPTION
6049                    , DEPRECIATION_OPTION
6050                    , MEMBER_ROLLUP_FLAG
6051                    , ALLOCATE_TO_FULLY_RSV_FLAG
6052                    , ALLOCATE_TO_FULLY_RET_FLAG
6053                    , TERMINAL_GAIN_LOSS_AMOUNT
6054                    , CIP_COST
6055                    , YTD_PROCEEDS
6056                    , LTD_PROCEEDS
6057                    , LTD_COST_OF_REMOVAL
6058                    , EOFY_RESERVE
6059                    , PRIOR_EOFY_RESERVE
6060                    , EOP_ADJ_COST
6061                    , EOP_FORMULA_FACTOR
6062                    , EXCLUDE_PROCEEDS_FROM_BASIS
6063                    , RETIREMENT_DEPRN_OPTION
6064                    , TERMINAL_GAIN_LOSS_FLAG
6065                    , SUPER_GROUP_ID
6066                    , OVER_DEPRECIATE_OPTION
6067                    , DISABLED_FLAG
6068                    , SET_OF_BOOKS_ID
6069               FROM  FA_MC_BOOKS
6070               WHERE TRANSACTION_HEADER_ID_IN = t_transaction_header_id_in(i)
6071                 AND set_of_books_id = p_asset_hdr_rec.set_of_books_id;
6072 
6073       else
6074          l_loc := 'Before BS';
6075          -- Update FA_BOOKS_SUMAMRY table with new reserve adjustment amount and
6076          -- related columns.  Return new adjusted_cost and deprn reserve for later use
6077          FORALL i in t_asset_id.first..t_asset_id.last
6078             UPDATE FA_BOOKS_SUMMARY
6079             SET    RESERVE_ADJUSTMENT_AMOUNT = RESERVE_ADJUSTMENT_AMOUNT + t_adjustment_amount(i)
6080                  , DEPRN_RESERVE = DEPRN_RESERVE + t_adjustment_amount(i)
6081                  , ADJUSTED_COST = RECOVERABLE_COST - DEPRN_RESERVE - t_adjustment_amount(i)
6082             WHERE  ASSET_ID = t_asset_id(i)
6083             AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6084             AND    PERIOD_COUNTER = p_period_rec.period_counter;
6085 
6086          if (p_log_level_rec.statement_level) then
6087             for i in t_asset_id.first..t_asset_id.last loop
6088                fa_debug_pkg.add(l_calling_fn, 'i', i, p_log_level_rec => p_log_level_rec);
6089                fa_debug_pkg.add(l_calling_fn, 't_transaction_header_id_in', t_transaction_header_id_in(i));
6090                fa_debug_pkg.add(l_calling_fn, 't_asset_id', t_asset_id(i));
6091                fa_debug_pkg.add(l_calling_fn, 't_thid', t_thid(i));
6092             end loop;
6093          end if;
6094 
6095          l_loc := 'Before primary Books';
6096          -- Deactivate FA_BOOKS row for processed assets
6097          FORALL i in t_asset_id.first..t_asset_id.last
6098             UPDATE FA_BOOKS
6099             SET    DATE_INEFFECTIVE = p_trans_rec.who_info.last_update_date
6100                  , TRANSACTION_HEADER_ID_OUT = t_thid(i)
6101             WHERE  TRANSACTION_HEADER_ID_IN = t_transaction_header_id_in(i);
6102 
6103          l_loc := 'Before primary Insert Books';
6104          -- Insert new FA_BOOKS records for processed assets
6105          FORALL i in t_asset_id.first..t_asset_id.last
6106             INSERT INTO FA_BOOKS( BOOK_TYPE_CODE
6107                                 , ASSET_ID
6108                                 , DATE_PLACED_IN_SERVICE
6109                                 , DATE_EFFECTIVE
6110                                 , DEPRN_START_DATE
6111                                 , DEPRN_METHOD_CODE
6112                                 , LIFE_IN_MONTHS
6113                                 , RATE_ADJUSTMENT_FACTOR
6114                                 , ADJUSTED_COST
6115                                 , COST
6116                                 , ORIGINAL_COST
6117                                 , SALVAGE_VALUE
6118                                 , PRORATE_CONVENTION_CODE
6119                                 , PRORATE_DATE
6120                                 , COST_CHANGE_FLAG
6121                                 , ADJUSTMENT_REQUIRED_STATUS
6122                                 , CAPITALIZE_FLAG
6123                                 , RETIREMENT_PENDING_FLAG
6124                                 , DEPRECIATE_FLAG
6125                                 , LAST_UPDATE_DATE
6126                                 , LAST_UPDATED_BY
6127                                 , TRANSACTION_HEADER_ID_IN
6128                                 , ITC_AMOUNT_ID
6129                                 , ITC_AMOUNT
6130                                 , RETIREMENT_ID
6131                                 , TAX_REQUEST_ID
6132                                 , ITC_BASIS
6133                                 , BASIC_RATE
6134                                 , ADJUSTED_RATE
6135                                 , BONUS_RULE
6136                                 , CEILING_NAME
6137                                 , RECOVERABLE_COST
6138                                 , LAST_UPDATE_LOGIN
6139                                 , ADJUSTED_CAPACITY
6140                                 , FULLY_RSVD_REVALS_COUNTER
6141                                 , IDLED_FLAG
6142                                 , PERIOD_COUNTER_CAPITALIZED
6143                                 , PERIOD_COUNTER_FULLY_RESERVED
6144                                 , PERIOD_COUNTER_FULLY_RETIRED
6145                                 , PRODUCTION_CAPACITY
6146                                 , REVAL_AMORTIZATION_BASIS
6147                                 , REVAL_CEILING
6148                                 , UNIT_OF_MEASURE
6149                                 , UNREVALUED_COST
6150                                 , ANNUAL_DEPRN_ROUNDING_FLAG
6151                                 , PERCENT_SALVAGE_VALUE
6152                                 , ALLOWED_DEPRN_LIMIT
6153                                 , ALLOWED_DEPRN_LIMIT_AMOUNT
6154                                 , PERIOD_COUNTER_LIFE_COMPLETE
6155                                 , ADJUSTED_RECOVERABLE_COST
6156                                 , ANNUAL_ROUNDING_FLAG
6157                                 , GLOBAL_ATTRIBUTE1
6158                                 , GLOBAL_ATTRIBUTE2
6159                                 , GLOBAL_ATTRIBUTE3
6160                                 , GLOBAL_ATTRIBUTE4
6161                                 , GLOBAL_ATTRIBUTE5
6162                                 , GLOBAL_ATTRIBUTE6
6163                                 , GLOBAL_ATTRIBUTE7
6164                                 , GLOBAL_ATTRIBUTE8
6165                                 , GLOBAL_ATTRIBUTE9
6166                                 , GLOBAL_ATTRIBUTE10
6167                                 , GLOBAL_ATTRIBUTE11
6168                                 , GLOBAL_ATTRIBUTE12
6169                                 , GLOBAL_ATTRIBUTE13
6170                                 , GLOBAL_ATTRIBUTE14
6171                                 , GLOBAL_ATTRIBUTE15
6172                                 , GLOBAL_ATTRIBUTE16
6173                                 , GLOBAL_ATTRIBUTE17
6174                                 , GLOBAL_ATTRIBUTE18
6175                                 , GLOBAL_ATTRIBUTE19
6176                                 , GLOBAL_ATTRIBUTE20
6177                                 , GLOBAL_ATTRIBUTE_CATEGORY
6178                                 , EOFY_ADJ_COST
6179                                 , EOFY_FORMULA_FACTOR
6180                                 , SHORT_FISCAL_YEAR_FLAG
6181                                 , CONVERSION_DATE
6182                                 , ORIGINAL_DEPRN_START_DATE
6183                                 , REMAINING_LIFE1
6184                                 , REMAINING_LIFE2
6185                                 , OLD_ADJUSTED_COST
6186                                 , FORMULA_FACTOR
6187                                 , GROUP_ASSET_ID
6188                                 , SALVAGE_TYPE
6189                                 , DEPRN_LIMIT_TYPE
6190                                 , REDUCTION_RATE
6191                                 , REDUCE_ADDITION_FLAG
6192                                 , REDUCE_ADJUSTMENT_FLAG
6193                                 , REDUCE_RETIREMENT_FLAG
6194                                 , RECOGNIZE_GAIN_LOSS
6195                                 , RECAPTURE_RESERVE_FLAG
6196                                 , LIMIT_PROCEEDS_FLAG
6197                                 , TERMINAL_GAIN_LOSS
6198                                 , TRACKING_METHOD
6199                                 , EXCLUDE_FULLY_RSV_FLAG
6200                                 , EXCESS_ALLOCATION_OPTION
6201                                 , DEPRECIATION_OPTION
6202                                 , MEMBER_ROLLUP_FLAG
6203                                 , ALLOCATE_TO_FULLY_RSV_FLAG
6204                                 , ALLOCATE_TO_FULLY_RET_FLAG
6205                                 , TERMINAL_GAIN_LOSS_AMOUNT
6206                                 , CIP_COST
6207                                 , YTD_PROCEEDS
6208                                 , LTD_PROCEEDS
6209                                 , LTD_COST_OF_REMOVAL
6210                                 , EOFY_RESERVE
6211                                 , PRIOR_EOFY_RESERVE
6212                                 , EOP_ADJ_COST
6213                                 , EOP_FORMULA_FACTOR
6214                                 , EXCLUDE_PROCEEDS_FROM_BASIS
6215                                 , RETIREMENT_DEPRN_OPTION
6216                                 , TERMINAL_GAIN_LOSS_FLAG
6217                                 , SUPER_GROUP_ID
6218                                 , OVER_DEPRECIATE_OPTION
6219                                 , DISABLED_FLAG
6220             ) SELECT BOOK_TYPE_CODE
6221                    , ASSET_ID
6222                    , DATE_PLACED_IN_SERVICE
6223                    , p_trans_rec.who_info.last_update_date -- DATE_EFFECTIVE
6224                    , DEPRN_START_DATE
6225                    , DEPRN_METHOD_CODE
6226                    , LIFE_IN_MONTHS
6227                    , RATE_ADJUSTMENT_FACTOR
6228                    , t_adjusted_cost(i) - t_adjustment_amount(i) -- ADJUSTED_COST
6229                    , COST
6230                    , ORIGINAL_COST
6231                    , SALVAGE_VALUE
6232                    , PRORATE_CONVENTION_CODE
6233                    , PRORATE_DATE
6234                    , COST_CHANGE_FLAG
6235                    , ADJUSTMENT_REQUIRED_STATUS
6236                    , CAPITALIZE_FLAG
6237                    , RETIREMENT_PENDING_FLAG
6238                    , DEPRECIATE_FLAG
6239                    , p_trans_rec.who_info.last_update_date -- LAST_UPDATE_DATE
6240                    , p_trans_rec.who_info.last_updated_by -- LAST_UPDATED_BY
6241                    , t_thid(i) -- TRANSACTION_HEADER_ID_IN
6242                    , ITC_AMOUNT_ID
6243                    , ITC_AMOUNT
6244                    , RETIREMENT_ID
6245                    , TAX_REQUEST_ID
6246                    , ITC_BASIS
6247                    , BASIC_RATE
6248                    , ADJUSTED_RATE
6249                    , BONUS_RULE
6250                    , CEILING_NAME
6251                    , RECOVERABLE_COST
6252                    , p_trans_rec.who_info.last_update_login -- LAST_UPDATE_LOGIN
6253                    , ADJUSTED_CAPACITY
6254                    , FULLY_RSVD_REVALS_COUNTER
6255                    , IDLED_FLAG
6256                    , PERIOD_COUNTER_CAPITALIZED
6257                    , PERIOD_COUNTER_FULLY_RESERVED
6258                    , PERIOD_COUNTER_FULLY_RETIRED
6259                    , PRODUCTION_CAPACITY
6260                    , REVAL_AMORTIZATION_BASIS
6261                    , REVAL_CEILING
6262                    , UNIT_OF_MEASURE
6263                    , UNREVALUED_COST
6264                    , ANNUAL_DEPRN_ROUNDING_FLAG
6265                    , PERCENT_SALVAGE_VALUE
6266                    , ALLOWED_DEPRN_LIMIT
6267                    , ALLOWED_DEPRN_LIMIT_AMOUNT
6268                    , PERIOD_COUNTER_LIFE_COMPLETE
6269                    , ADJUSTED_RECOVERABLE_COST
6270                    , ANNUAL_ROUNDING_FLAG
6271                    , GLOBAL_ATTRIBUTE1
6272                    , GLOBAL_ATTRIBUTE2
6273                    , GLOBAL_ATTRIBUTE3
6274                    , GLOBAL_ATTRIBUTE4
6275                    , GLOBAL_ATTRIBUTE5
6276                    , GLOBAL_ATTRIBUTE6
6277                    , GLOBAL_ATTRIBUTE7
6278                    , GLOBAL_ATTRIBUTE8
6279                    , GLOBAL_ATTRIBUTE9
6280                    , GLOBAL_ATTRIBUTE10
6281                    , GLOBAL_ATTRIBUTE11
6282                    , GLOBAL_ATTRIBUTE12
6283                    , GLOBAL_ATTRIBUTE13
6284                    , GLOBAL_ATTRIBUTE14
6285                    , GLOBAL_ATTRIBUTE15
6286                    , GLOBAL_ATTRIBUTE16
6287                    , GLOBAL_ATTRIBUTE17
6288                    , GLOBAL_ATTRIBUTE18
6289                    , GLOBAL_ATTRIBUTE19
6290                    , GLOBAL_ATTRIBUTE20
6291                    , GLOBAL_ATTRIBUTE_CATEGORY
6292                    , EOFY_ADJ_COST
6293                    , EOFY_FORMULA_FACTOR
6294                    , SHORT_FISCAL_YEAR_FLAG
6295                    , CONVERSION_DATE
6296                    , ORIGINAL_DEPRN_START_DATE
6297                    , REMAINING_LIFE1
6298                    , REMAINING_LIFE2
6299                    , OLD_ADJUSTED_COST
6300                    , FORMULA_FACTOR
6301                    , GROUP_ASSET_ID
6302                    , SALVAGE_TYPE
6303                    , DEPRN_LIMIT_TYPE
6304                    , REDUCTION_RATE
6305                    , REDUCE_ADDITION_FLAG
6306                    , REDUCE_ADJUSTMENT_FLAG
6307                    , REDUCE_RETIREMENT_FLAG
6308                    , RECOGNIZE_GAIN_LOSS
6309                    , RECAPTURE_RESERVE_FLAG
6310                    , LIMIT_PROCEEDS_FLAG
6311                    , TERMINAL_GAIN_LOSS
6312                    , TRACKING_METHOD
6313                    , EXCLUDE_FULLY_RSV_FLAG
6314                    , EXCESS_ALLOCATION_OPTION
6315                    , DEPRECIATION_OPTION
6316                    , MEMBER_ROLLUP_FLAG
6317                    , ALLOCATE_TO_FULLY_RSV_FLAG
6318                    , ALLOCATE_TO_FULLY_RET_FLAG
6319                    , TERMINAL_GAIN_LOSS_AMOUNT
6320                    , CIP_COST
6321                    , YTD_PROCEEDS
6322                    , LTD_PROCEEDS
6323                    , LTD_COST_OF_REMOVAL
6324                    , EOFY_RESERVE
6325                    , PRIOR_EOFY_RESERVE
6326                    , EOP_ADJ_COST
6327                    , EOP_FORMULA_FACTOR
6328                    , EXCLUDE_PROCEEDS_FROM_BASIS
6329                    , RETIREMENT_DEPRN_OPTION
6330                    , TERMINAL_GAIN_LOSS_FLAG
6331                    , SUPER_GROUP_ID
6332                    , OVER_DEPRECIATE_OPTION
6333                    , DISABLED_FLAG
6334               FROM  FA_BOOKS
6335               WHERE TRANSACTION_HEADER_ID_IN = t_transaction_header_id_in(i);
6336 
6337       end if; -- (p_mrc_sob_type_code = 'R')
6338 
6339       -- flush the rows to the db
6340       l_adj.transaction_header_id := 0;
6341       l_adj.flush_adj_flag        := TRUE;
6342       l_adj.leveling_flag         := TRUE;
6343       l_adj.mrc_sob_type_code     :=  p_mrc_sob_type_code;
6344       l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
6345 
6346       if not FA_INS_ADJUST_PKG.faxinaj
6347              (l_adj,
6348               p_trans_rec.who_info.last_update_date,
6349               p_trans_rec.who_info.last_updated_by,
6350               p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
6351          raise allocate_err;
6352       end if;
6353 
6354       -- As of now, following commit is commented out.
6355       -- It may be necessary in future if we see pl/sql table used in this function
6356       -- caused memory problem.
6357 --      COMMIT;
6358    END LOOP; -- MAIN OUTER LOOP
6359 
6360    if (p_log_level_rec.statement_level) then
6361       fa_debug_pkg.add(l_calling_fn, 'End of Main Loop', p_reserve_amount - l_actual_rsv_total, p_log_level_rec => p_log_level_rec);
6362    end if;
6363 
6364    if (l_actual_rsv_total = p_reserve_amount) then
6365       if (p_log_level_rec.statement_level) then
6366          fa_debug_pkg.add(l_calling_fn, 'End of allocation', ' ', p_log_level_rec => p_log_level_rec);
6367       end if;
6368 
6369       return TRUE;
6370 
6371    end if;
6372 
6373     /* Bug 8633654 */
6374             if (nvl(l_actual_rsv_total,0) = 0 and p_trans_rec.transaction_key = 'MR') then
6375                if (p_log_level_rec.statement_level) then
6376                   fa_debug_pkg.add(l_calling_fn, 'No member to allocate', ' ',p_log_level_rec);
6377                end if;
6378 
6379                return TRUE;
6380 
6381             end if;
6382 
6383    --
6384    -- If process reaches to this point, it means that reinstatement could not
6385    -- reinstate all amounts because member at the time of retirement does not exist
6386    -- or could not reinstate the reserve because some member become fully reserved.
6387    if (p_trans_rec.transaction_key = 'MS') and
6388       (p_mode = 'NORMAL') then
6389       -- Call Do_Allocation again to distribute remaining amounts
6390       if (p_log_level_rec.statement_level) then
6391          fa_debug_pkg.add(l_calling_fn, 'Recurrsive Call Do_Allocation', 'Calling...', p_log_level_rec => p_log_level_rec);
6392       end if;
6393       if not Do_Allocation(
6394                       p_trans_rec         => p_trans_rec,
6395                       p_asset_hdr_rec     => p_asset_hdr_rec,
6396                       p_asset_fin_rec     => p_asset_fin_rec,
6397                       p_asset_deprn_rec_new => p_asset_deprn_rec_new, -- group new deprn rec
6398                       p_period_rec        => p_period_rec,
6399                       p_reserve_amount    => -1 *(p_reserve_amount - l_actual_rsv_total),
6400                       p_mode              => 'RECURR',
6401                       p_mrc_sob_type_code => p_mrc_sob_type_code,
6402                       p_mem_ret_thid      => p_mem_ret_thid,
6403                       p_calling_fn        => l_calling_fn,
6404                       p_log_level_rec       => p_log_level_rec) then
6405          if (p_log_level_rec.statement_level) then
6406             fa_debug_pkg.add(l_calling_fn, 'Recurrsive Call to Do_Allocation', 'Failed', p_log_level_rec => p_log_level_rec);
6407          end if;
6408 
6409          raise allocate_err;
6410       end if;
6411 
6412       return TRUE;
6413    end if;
6414 
6415    -- This portion of code is necessary to distribute fraction
6416    -- produced during allocation process above.
6417    -- For performance reason (to avoid issuing sql) reuse what's in pl/sql table now
6418    -- If there is a difference between
6419    if (p_reserve_amount <> l_actual_rsv_total) then
6420 
6421       if (p_log_level_rec.statement_level) then
6422          fa_debug_pkg.add(l_calling_fn, 'Inside rounding correction', ' ',p_log_level_rec);
6423          fa_debug_pkg.add(l_calling_fn, 'p_reserve_amount', p_reserve_amount,p_log_level_rec);
6424          fa_debug_pkg.add(l_calling_fn, 'l_actual_rsv_total', l_actual_rsv_total,p_log_level_rec);
6425       end if;
6426 
6427       if (p_reserve_amount > 0) then
6428          l_reallocate_amount := p_reserve_amount - l_actual_rsv_total;
6429       else
6430          l_reallocate_amount := l_actual_rsv_total - p_reserve_amount;
6431       end if;
6432 
6433       if (p_log_level_rec.statement_level) then
6434          fa_debug_pkg.add(l_calling_fn, 'l_reallocate_amount', l_reallocate_amount,p_log_level_rec);
6435       end if;
6436 
6437       -- First, if it needs to take out some reserve, then do that against
6438       -- last processed asset. If not, then first go through the assets in
6439       -- pl/sql table to check for the same. if not done yet, hit db to do the same.
6440       if (p_reserve_amount < 0 or
6441           p_reserve_amount < l_actual_rsv_total) then
6442 
6443           if (p_log_level_rec.statement_level) then
6444                      fa_debug_pkg.add(l_calling_fn, 'Rounding : Takeout some resv', ' ',p_log_level_rec);
6445           end if;
6446 
6447          -- Need to backout some reserve so do that from last processed asset
6448 
6449          if (p_mrc_sob_type_code = 'R') then
6450             UPDATE FA_MC_ADJUSTMENTS
6451             SET    ADJUSTMENT_AMOUNT = ADJUSTMENT_AMOUNT + l_reallocate_amount
6452             WHERE  ASSET_ID = l_last_asset_id
6453             AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6454             AND    TRANSACTION_HEADER_ID = l_last_thid
6455             AND    DISTRIBUTION_ID = (SELECT MIN(DISTRIBUTION_ID)
6456                                       FROM   FA_MC_ADJUSTMENTS
6457                                       WHERE  ASSET_ID = l_last_asset_id
6458                                       AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6459                                       AND    TRANSACTION_HEADER_ID = l_last_thid
6460                                       AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id)
6461             AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
6462 
6463             UPDATE FA_MC_BOOKS_SUMMARY
6464             SET    RESERVE_ADJUSTMENT_AMOUNT = RESERVE_ADJUSTMENT_AMOUNT + ( l_reallocate_amount * sign(p_reserve_amount) * -1 )
6465                  , DEPRN_RESERVE = DEPRN_RESERVE + ( l_reallocate_amount * sign(p_reserve_amount) * -1 )
6466                  , ADJUSTED_COST = ADJUSTED_COST - ( l_reallocate_amount * sign(p_reserve_amount) * -1 )
6467             WHERE  ASSET_ID = l_last_asset_id
6468             AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6469             AND    PERIOD_COUNTER = p_period_rec.period_counter
6470             AND    TRANSACTION_HEADER_ID_OUT IS NULL
6471             AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id;
6472 
6473 
6474             UPDATE FA_MC_BOOKS
6475             SET    ADJUSTED_COST = ADJUSTED_COST - ( l_reallocate_amount * sign(p_reserve_amount) * -1 )
6476             WHERE  TRANSACTION_HEADER_ID_IN = l_last_thid
6477               AND  set_of_books_id = p_asset_hdr_rec.set_of_books_id;
6478          else
6479             UPDATE FA_ADJUSTMENTS
6480             SET    ADJUSTMENT_AMOUNT = ADJUSTMENT_AMOUNT + l_reallocate_amount
6481             WHERE  ASSET_ID = l_last_asset_id
6482             AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6483             AND    TRANSACTION_HEADER_ID = l_last_thid
6484             AND    DISTRIBUTION_ID = (SELECT MIN(DISTRIBUTION_ID)
6485                                       FROM   FA_ADJUSTMENTS
6486                                       WHERE  ASSET_ID = l_last_asset_id
6487                                       AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6488                                       AND    TRANSACTION_HEADER_ID = l_last_thid);
6489 
6490             UPDATE FA_BOOKS_SUMMARY
6491             SET    RESERVE_ADJUSTMENT_AMOUNT = RESERVE_ADJUSTMENT_AMOUNT + ( l_reallocate_amount * sign(p_reserve_amount) * -1 )
6492                  , DEPRN_RESERVE = DEPRN_RESERVE + ( l_reallocate_amount * sign(p_reserve_amount) * -1 )
6493                  , ADJUSTED_COST = ADJUSTED_COST - ( l_reallocate_amount * sign(p_reserve_amount) * -1 )
6494             WHERE  ASSET_ID = l_last_asset_id
6495             AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6496             AND    PERIOD_COUNTER = p_period_rec.period_counter
6497             AND    TRANSACTION_HEADER_ID_OUT IS NULL;
6498 
6499 
6500             UPDATE FA_BOOKS
6501             SET    ADJUSTED_COST = ADJUSTED_COST - ( l_reallocate_amount * sign(p_reserve_amount) * -1 )
6502             WHERE  TRANSACTION_HEADER_ID_IN = l_last_thid;
6503          end if; -- (p_mrc_sob_type_code = 'R')
6504       else
6505 
6506          if (p_log_level_rec.statement_level) then
6507             fa_debug_pkg.add(l_calling_fn, 'Rounding : Add additional resv', ' ',p_log_level_rec);
6508             fa_debug_pkg.add(l_calling_fn, 'Rounding : current PL_SQL count', t_asset_id.count,p_log_level_rec);
6509          end if;
6510 
6511          -- Need to hit db to allocate excess amount if any at this point.
6512          -- Basically, do the same thing done in above loop but this time against
6513          -- data in db.
6514          if (l_reallocate_amount <> 0) then
6515 
6516             if (p_log_level_rec.statement_level) then
6517                fa_debug_pkg.add(l_calling_fn, 'Rounding : reallocate_amount at 2', l_reallocate_amount,p_log_level_rec);
6518             end if;
6519 
6520             t_thid.delete;
6521             t_asset_id.delete;
6522             t_adjusted_cost.delete;
6523 
6524             if (p_mrc_sob_type_code = 'R') then
6525                OPEN C_MC_DIST_EXCESS;
6526             else
6527                OPEN C_DIST_EXCESS;
6528             end if;
6529 
6530             LOOP -- *** DIST EXCESS OUTER LOOP **
6531 
6532                if (l_reallocate_amount = 0) then
6533                   if (p_mrc_sob_type_code = 'R') then
6534                      CLOSE C_MC_DIST_EXCESS;
6535                   else
6536                      CLOSE C_DIST_EXCESS;
6537                   end if;
6538                   EXIT;
6539                end if;
6540 
6541                if (p_mrc_sob_type_code = 'R') then
6542                   FETCH C_MC_DIST_EXCESS BULK COLLECT INTO t_asset_id
6543                                                       , t_thid
6544                                                       , t_adjusted_cost LIMIT l_limit;
6545                else
6546                   FETCH C_DIST_EXCESS BULK COLLECT INTO t_asset_id
6547                                                       , t_thid
6548                                                       , t_adjusted_cost LIMIT l_limit;
6549                end if;
6550 
6551                if (t_asset_id.count = 0) then
6552                   if (p_mrc_sob_type_code = 'R') then
6553                      CLOSE C_MC_DIST_EXCESS;
6554                   else
6555                      CLOSE C_DIST_EXCESS;
6556                   end if;
6557                   EXIT;
6558                end if;
6559 
6560                FOR i in t_asset_id.first..t_asset_id.last LOOP
6561 
6562                   if (p_log_level_rec.statement_level) then
6563                      fa_debug_pkg.add(l_calling_fn, 'Rounding 2 : ', i,p_log_level_rec);
6564                      fa_debug_pkg.add(l_calling_fn, 'Rounding 2 : t_asset_id('||i||')', t_asset_id(i),p_log_level_rec);
6565                      fa_debug_pkg.add(l_calling_fn, 'Rounding 2 : t_adjusted_cost('||i||')', t_adjusted_cost(i),p_log_level_rec);
6566                   end if;
6567 
6568                   if (t_adjusted_cost(i) >= l_reallocate_amount) then
6569 
6570                      if (p_log_level_rec.statement_level) then
6571                         fa_debug_pkg.add(l_calling_fn, 'Rounding 2 : Adjust reallocate_amount ',' ',p_log_level_rec);
6572                      end if;
6573 
6574                      if (p_mrc_sob_type_code = 'R') then
6575                         UPDATE FA_MC_ADJUSTMENTS
6576                         SET    ADJUSTMENT_AMOUNT = ADJUSTMENT_AMOUNT + l_reallocate_amount
6577                         WHERE  ASSET_ID = t_asset_id(i)
6578                         AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6579                         AND    TRANSACTION_HEADER_ID = t_thid(i)
6580                         AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id
6581                         AND    DISTRIBUTION_ID = (SELECT MIN(DISTRIBUTION_ID)
6582                                                   FROM   FA_MC_ADJUSTMENTS
6583                                                   WHERE  ASSET_ID = t_asset_id(i)
6584                                                   AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6585                                                   AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id
6586                                                   AND    TRANSACTION_HEADER_ID = t_thid(i));
6587 
6588                         UPDATE FA_MC_BOOKS_SUMMARY
6589                         SET    RESERVE_ADJUSTMENT_AMOUNT = RESERVE_ADJUSTMENT_AMOUNT + (l_reallocate_amount * sign(p_reserve_amount) * -1)
6590                              , DEPRN_RESERVE = DEPRN_RESERVE + (l_reallocate_amount * sign(p_reserve_amount) * -1)
6591                              , ADJUSTED_COST = ADJUSTED_COST + l_reallocate_amount
6592                         WHERE  ASSET_ID = t_asset_id(i)
6593                         AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6594                         AND    PERIOD_COUNTER = p_period_rec.period_counter
6595                         AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id
6596                         AND    TRANSACTION_HEADER_ID_OUT is null;
6597 
6598 
6599                         UPDATE FA_MC_BOOKS
6600                         SET    ADJUSTED_COST = ADJUSTED_COST + l_reallocate_amount
6601                         WHERE  TRANSACTION_HEADER_ID_IN = t_thid(i)
6602                           AND  set_of_books_id = p_asset_hdr_rec.set_of_books_id;
6603                      else
6604                         if (p_log_level_rec.statement_level) then
6605                            fa_debug_pkg.add(l_calling_fn, 'Rounding 2 : adj rallocate ',(l_reallocate_amount * sign(p_reserve_amount) * -1),p_log_level_rec);
6606                         end if;
6607 
6608                         UPDATE FA_ADJUSTMENTS
6609                         SET    ADJUSTMENT_AMOUNT = ADJUSTMENT_AMOUNT + l_reallocate_amount
6610                         WHERE  ASSET_ID = t_asset_id(i)
6611                         AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6612                         AND    TRANSACTION_HEADER_ID = t_thid(i)
6613                         AND    DISTRIBUTION_ID = (SELECT MIN(DISTRIBUTION_ID)
6614                                                   FROM   FA_ADJUSTMENTS
6615                                                   WHERE  ASSET_ID = t_asset_id(i)
6616                                                   AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6617                                                   AND    TRANSACTION_HEADER_ID = t_thid(i));
6618 
6619                         UPDATE FA_BOOKS_SUMMARY
6620                         SET    RESERVE_ADJUSTMENT_AMOUNT = RESERVE_ADJUSTMENT_AMOUNT + (l_reallocate_amount * sign(p_reserve_amount) * -1)
6621                              , DEPRN_RESERVE = DEPRN_RESERVE + (l_reallocate_amount * sign(p_reserve_amount) * -1)
6622                              , ADJUSTED_COST = ADJUSTED_COST + l_reallocate_amount
6623                         WHERE  ASSET_ID = t_asset_id(i)
6624                         AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6625                         AND    PERIOD_COUNTER = p_period_rec.period_counter
6626                         AND    TRANSACTION_HEADER_ID_OUT is null;
6627 
6628 
6629                         UPDATE FA_BOOKS
6630                         SET    ADJUSTED_COST = ADJUSTED_COST + l_reallocate_amount
6631                         WHERE  TRANSACTION_HEADER_ID_IN = t_thid(i);
6632                      end if;
6633 
6634                      l_reallocate_amount := 0;
6635 
6636                   else
6637 
6638                      if (p_mrc_sob_type_code = 'R') then
6639                         UPDATE FA_MC_ADJUSTMENTS
6640                         SET    ADJUSTMENT_AMOUNT = ADJUSTMENT_AMOUNT + t_adjusted_cost(i)
6641                         WHERE  ASSET_ID = t_asset_id(i)
6642                         AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6643                         AND    TRANSACTION_HEADER_ID = t_thid(i)
6644                         AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id
6645                         AND    DISTRIBUTION_ID = (SELECT MIN(DISTRIBUTION_ID)
6646                                                   FROM   FA_MC_ADJUSTMENTS
6647                                                   WHERE  ASSET_ID = t_asset_id(i)
6648                                                   AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6649                                                   AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id
6650                                                   AND    TRANSACTION_HEADER_ID = t_thid(i));
6651 
6652                         UPDATE FA_MC_BOOKS_SUMMARY
6653                         SET    RESERVE_ADJUSTMENT_AMOUNT = RESERVE_ADJUSTMENT_AMOUNT + t_adjusted_cost(i)
6654                              , DEPRN_RESERVE = ADJUSTED_RECOVERABLE_COST
6655                              , ADJUSTED_COST = RECOVERABLE_COST - ADJUSTED_RECOVERABLE_COST
6656                         WHERE  ASSET_ID = t_asset_id(i)
6657                         AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6658                         AND    set_of_books_id = p_asset_hdr_rec.set_of_books_id
6659                         AND    PERIOD_COUNTER = p_period_rec.period_counter;
6660 
6661 
6662                         UPDATE FA_MC_BOOKS
6663                         SET    ADJUSTED_COST = RECOVERABLE_COST - ADJUSTED_RECOVERABLE_COST
6664                         WHERE  TRANSACTION_HEADER_ID_IN = t_thid(i)
6665                           AND  set_of_books_id = p_asset_hdr_rec.set_of_books_id;
6666                      else
6667                         UPDATE FA_ADJUSTMENTS
6668                         SET    ADJUSTMENT_AMOUNT = ADJUSTMENT_AMOUNT + t_adjusted_cost(i)
6669                         WHERE  ASSET_ID = t_asset_id(i)
6670                         AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6671                         AND    TRANSACTION_HEADER_ID = t_thid(i)
6672                         AND    DISTRIBUTION_ID = (SELECT MIN(DISTRIBUTION_ID)
6673                                                   FROM   FA_ADJUSTMENTS
6674                                                   WHERE  ASSET_ID = t_asset_id(i)
6675                                                   AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6676                                                   AND    TRANSACTION_HEADER_ID = t_thid(i));
6677 
6678                         UPDATE FA_BOOKS_SUMMARY
6679                         SET    RESERVE_ADJUSTMENT_AMOUNT = RESERVE_ADJUSTMENT_AMOUNT + t_adjusted_cost(i)
6680                              , DEPRN_RESERVE = ADJUSTED_RECOVERABLE_COST
6681                              , ADJUSTED_COST = RECOVERABLE_COST - ADJUSTED_RECOVERABLE_COST
6682                         WHERE  ASSET_ID = t_asset_id(i)
6683                         AND    BOOK_TYPE_CODE = p_asset_hdr_rec.book_type_code
6684                         AND    PERIOD_COUNTER = p_period_rec.period_counter;
6685 
6686 
6687                         UPDATE FA_BOOKS
6688                         SET    ADJUSTED_COST = RECOVERABLE_COST - ADJUSTED_RECOVERABLE_COST
6689                         WHERE  TRANSACTION_HEADER_ID_IN = t_thid(i);
6690                      end if;
6691 
6692                      l_reallocate_amount := l_reallocate_amount - t_adjusted_cost(i);
6693                   end if; -- (t_adjusted_recoverable_cost(i) -  t_new_deprn_reserve(i) >= l_reallocate_amount)
6694 
6695                   if (l_reallocate_amount = 0) then
6696                      EXIT;
6697                   end if;
6698 
6699 
6700                END LOOP; -- FOR i in t_asset_id.first..t_asset_id.last
6701 
6702             END LOOP; -- *** DIST EXCESS OUTER LOOP ***
6703 
6704          end if;
6705 
6706       end if; -- (p_reserve_amount < 0 or
6707 
6708    end if; -- (p_reserve_amount <> l_actual_rsv_total)
6709 
6710 
6711    if (p_log_level_rec.statement_level) then
6712       fa_debug_pkg.add(l_calling_fn, 'End', p_reserve_amount, p_log_level_rec => p_log_level_rec);
6713    end if;
6714 
6715    return true;
6716 
6717 EXCEPTION
6718    WHEN OTHERS THEN
6719     if (p_log_level_rec.statement_level) then
6720        fa_debug_pkg.add(l_calling_fn, 'l_loc', l_loc, p_log_level_rec => p_log_level_rec);
6721        fa_debug_pkg.add(l_calling_fn, 'p_mrc_sob_type_code', p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec);
6722        fa_debug_pkg.add(l_calling_fn, 'End', 'Failed'||':'||sqlerrm, p_log_level_rec => p_log_level_rec);
6723     end if;
6724 
6725     fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6726     return false;
6727 
6728 END Do_Allocation;
6729 
6730 /*bug#15897249 starts */
6731 FUNCTION MOVE_RESERVE_TO_ACT_DIST(
6732                p_trans_rec         IN     FA_API_TYPES.trans_rec_type,
6733                p_asset_hdr_rec     IN     FA_API_TYPES.asset_hdr_rec_type,
6734                p_period_rec        IN     FA_API_TYPES.period_rec_type,
6735                p_asset_retire_rec  IN     FA_API_TYPES.asset_retire_rec_type,
6736                p_mrc_sob_type_code IN     VARCHAR2,
6737                p_log_level_rec     IN     FA_API_TYPES.log_level_rec_type)
6738 RETURN BOOLEAN is
6739    l_adj                   FA_ADJUST_TYPE_PKG.FA_ADJ_ROW_STRUCT;
6740    calc_err                exception;
6741    l_calling_fn            VARCHAR2(100) := 'fa_group_retirement_pvt.move_reserve_to_act_dist';
6742    l_loc                   varchar2(100);
6743    l_deprn_reserve         NUMBER;
6744    l_bonus_reserve         NUMBER;
6745    l_temp_num              NUMBER;
6746    l_temp_char             VARCHAR2(30);
6747    l_temp_bool             boolean;
6748    l_tot_deprn_reserve     NUMBER;
6749    l_tot_bonus_reserve     NUMBER;
6750    cursor c_inactive_dist is
6751    select distribution_id
6752    from   FA_DISTRIBUTION_HISTORY
6753    where  book_type_code = p_asset_hdr_rec.book_type_code
6754    and    asset_id = p_asset_hdr_rec.asset_id
6755    and    transaction_header_id_out = p_trans_rec.transaction_header_id;
6756 BEGIN
6757    l_loc := 'START';
6758    l_adj.transaction_header_id   := p_trans_rec.transaction_header_id;
6759    l_adj.source_type_code        := 'RETIREMENT';
6760    l_adj.period_counter_created  := p_period_rec.period_counter;
6761    l_adj.period_counter_adjusted := p_period_rec.period_counter;
6762    l_adj.book_type_code          := p_asset_hdr_rec.book_type_code;
6763    l_adj.selection_thid          := 0;
6764    l_adj.selection_retid         := 0;
6765    l_adj.leveling_flag           := TRUE;
6766    l_adj.last_update_date        := p_trans_rec.who_info.last_update_date;
6767    l_adj.selection_mode          := FA_ADJUST_TYPE_PKG.FA_AJ_SINGLE;
6768    l_adj.current_units           := null;
6769    l_adj.debit_credit_flag       := 'DR';
6770 
6771    l_adj.flush_adj_flag          := TRUE;
6772    l_adj.gen_ccid_flag           := TRUE;
6773 
6774    l_adj.asset_id                := p_asset_hdr_rec.asset_id;
6775    l_adj.adjustment_type         := 'RESERVE';
6776    l_adj.account_type            := 'DEPRN_RESERVE_ACCT';
6777    l_adj.account                 := fa_cache_pkg.fazccb_record.DEPRN_RESERVE_ACCT;
6778    l_adj.code_combination_id     := 0;
6779    l_adj.mrc_sob_type_code       := p_mrc_sob_type_code;
6780    l_adj.set_of_books_id         := p_asset_hdr_rec.set_of_books_id;
6781    l_tot_deprn_reserve := 0;
6782    l_tot_bonus_reserve := 0;
6783    for l_dist in c_inactive_dist
6784    loop
6785       fa_query_balances_pkg.query_balances(
6786                 X_asset_id => p_asset_hdr_rec.asset_id,
6787                 X_book => p_asset_hdr_rec.book_type_code,
6788                 X_period_ctr => 0,
6789                 X_dist_id => l_dist.distribution_id,
6790                 X_run_mode => 'STANDARD',
6791                 X_cost => l_temp_num,
6792                 X_deprn_rsv => l_deprn_reserve,
6793                 X_reval_rsv => l_temp_num,
6794                 X_ytd_deprn => l_temp_num,
6795                 X_ytd_reval_exp => l_temp_num,
6796                 X_reval_deprn_exp => l_temp_num,
6797                 X_deprn_exp => l_temp_num,
6798                 X_reval_amo => l_temp_num,
6799                 X_prod => l_temp_num,
6800                 X_ytd_prod => l_temp_num,
6801                 X_ltd_prod => l_temp_num,
6802                 X_adj_cost => l_temp_num,
6803                 X_reval_amo_basis => l_temp_num,
6804                 X_bonus_rate => l_temp_num,
6805                 X_deprn_source_code => l_temp_char,
6806                 X_adjusted_flag => l_temp_bool,
6807                 X_transaction_header_id => -1,
6808                 X_bonus_deprn_rsv => l_bonus_reserve,
6809                 X_bonus_ytd_deprn => l_temp_num,
6810                 X_bonus_deprn_amount => l_temp_num,
6811                 X_impairment_rsv => l_temp_num,
6812                 X_ytd_impairment => l_temp_num,
6813                 X_impairment_amount => l_temp_num,
6814                 X_capital_adjustment => l_temp_num,
6815                 X_general_fund => l_temp_num,
6816                 X_mrc_sob_type_code => p_mrc_sob_type_code,
6817                 X_set_of_books_id => p_asset_hdr_rec.set_of_books_id,
6818                 p_log_level_rec => p_log_level_rec);
6819       l_tot_deprn_reserve := l_tot_deprn_reserve+l_deprn_reserve;
6820       l_tot_bonus_reserve := l_tot_bonus_reserve+l_bonus_reserve;
6821       l_adj.adjustment_amount := l_deprn_reserve;
6822       l_adj.distribution_id   := l_dist.distribution_id;
6823       if l_deprn_reserve <> 0 then
6824          if not FA_INS_ADJUST_PKG.faxinaj
6825                             (l_adj,
6826                              p_trans_rec.who_info.last_update_date,
6827                              p_trans_rec.who_info.last_updated_by,
6828                              p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
6829             raise calc_err;
6830          end if;
6831       end if;
6832    end loop;
6833    l_adj.adjustment_amount := l_tot_deprn_reserve;
6834    l_adj.debit_credit_flag := 'CR';
6835    l_adj.selection_thid    := 0;
6836    l_adj.distribution_id   := 0;
6837    l_adj.selection_mode    := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
6838    if l_tot_deprn_reserve <> 0 then
6839       if not FA_INS_ADJUST_PKG.faxinaj
6840                          (l_adj,
6841                           p_trans_rec.who_info.last_update_date,
6842                           p_trans_rec.who_info.last_updated_by,
6843                           p_trans_rec.who_info.last_update_login, p_log_level_rec => p_log_level_rec) then
6844          raise calc_err;
6845       end if;
6846    end if;
6847    return true;
6848 
6849 EXCEPTION
6850    WHEN OTHERS THEN
6851     if (p_log_level_rec.statement_level) then
6852        fa_debug_pkg.add(l_calling_fn, 'l_loc', l_loc, p_log_level_rec => p_log_level_rec);
6853        fa_debug_pkg.add(l_calling_fn, 'p_mrc_sob_type_code', p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec);
6854        fa_debug_pkg.add(l_calling_fn, 'End', 'Failed'||':'||sqlerrm, p_log_level_rec => p_log_level_rec);
6855     end if;
6856 
6857     fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6858     return false;
6859 
6860 END MOVE_RESERVE_TO_ACT_DIST;
6861 /*bug#15897249 end */
6862 
6863 
6864 END FA_RETIREMENT_PVT;