DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_GAINLOSS_UPD_PKG

Source


1 PACKAGE BODY FA_GAINLOSS_UPD_PKG AS
2 /* $Header: fagupdb.pls 120.134.12020000.9 2013/03/29 07:24:23 mswetha ship $*/
3 
4 
5 /*============================================================================
6 | NAME        faginfo
7 |
8 | FUNCTION   Set selection_mode accordingly for Tax book
9 |
10 |
11 | History     YYOON          05/23/06         Created
12 |                            added for the bug 5149832 and 5231996
13 |===========================================================================*/
14 
15 
16 Function faginfo(
17         RET                 IN fa_ret_types.ret_struct,
18         BK                  IN fa_ret_types.book_struct,
19         cpd_ctr             IN NUMBER,
20         today               IN DATE,
21         user_id             IN NUMBER,
22         calling_module      IN  varchar,
23         candidate_mode      IN  varchar,
24         set_adj_row         IN  boolean,
25         unit_ret_in_corp    OUT nocopy boolean,
26         ret_id_in_corp      OUT nocopy number,
27         th_id_out_in_corp   OUT nocopy number,
28         balance_tfr_in_tax  OUT nocopy number,
29         adj_row             IN  OUT nocopy FA_ADJUST_TYPE_PKG.fa_adj_row_struct,
30         p_log_level_rec     IN  FA_API_TYPES.log_level_rec_type) return boolean IS
31 
32     l_unit_ret_in_corp    boolean;
33     l_ret_id_in_corp      number;
34     l_id_out              number;
35     l_selection_retid     number;
36     l_units_retired       number;
37     l_balance_tfr_in_tax  number;
38 
39     l_calling_fn        varchar2(40) := 'FA_GAINLOSS_UPD_PKG.faginfo';
40 
41     faginfo_error       EXCEPTION;
42 
43 BEGIN
44 
45    if (p_log_level_rec.statement_level) then
46       fa_debug_pkg.add(l_calling_fn, '++++++++++++++++++++++++', '...', p_log_level_rec => p_log_level_rec);
47       fa_debug_pkg.add(l_calling_fn, '++ CALLED BY ', calling_module, p_log_level_rec => p_log_level_rec);
48    end if;
49 
50    if candidate_mode in ('RETIRE', 'CLEAR_PARTIAL') then
51 
52           l_id_out := -1;
53 
54           begin
55 
56                select r.retirement_id
57                      ,r.units
58                  into l_ret_id_in_corp
59                      ,l_units_retired
60                from fa_transaction_headers sth
61                    ,fa_retirements r
62                    ,fa_book_controls bc
63                where sth.transaction_header_id = RET.th_id_in
64                  and sth.asset_id = RET.asset_id
65                  and sth.book_type_code = RET.book
66                  and bc.book_type_code = sth.book_type_code
67                  and bc.book_class = 'TAX'
68                  and r.asset_id = sth.asset_id
69                  and r.transaction_header_id_in = nvl(sth.source_transaction_header_id, sth.transaction_header_id)
70                  and rownum = 1;
71 
72                if (p_log_level_rec.statement_level) then
73                  fa_debug_pkg.add(l_calling_fn, '++ IN FAGINFO: Processing TAX', '...', p_log_level_rec => p_log_level_rec);
74                  fa_debug_pkg.add(l_calling_fn, '++ retirement_id in CORP', l_ret_id_in_corp, p_log_level_rec => p_log_level_rec);
75                  fa_debug_pkg.add(l_calling_fn, '++ l_units_retired in CORP', l_units_retired, p_log_level_rec => p_log_level_rec);
76                end if;
77 
78           exception
79                 when no_data_found then -- when called from Corp
80                   l_ret_id_in_corp := RET.retirement_id;
81                   l_units_retired := RET.units_retired;
82           end;
83 
84           l_unit_ret_in_corp := FALSE;
85           begin
86                -- calculate Corp's TRANSFER OUT THID
87                select transaction_header_id_out
88                  into l_id_out
89                from fa_distribution_history
90                where retirement_id =
91                 (select r.retirement_id
92                  from fa_transaction_headers sth
93                      ,fa_retirements r
94                  where sth.transaction_header_id = RET.th_id_in
95                    and sth.asset_id = RET.asset_id
96                    and sth.book_type_code = RET.book
97                    and r.asset_id = sth.asset_id
98                    and r.transaction_header_id_in = nvl(sth.source_transaction_header_id, sth.transaction_header_id)
99                 )
100                 and transaction_header_id_out is not null
101                 and rownum = 1;
102 
103                l_unit_ret_in_corp := TRUE;
104 
105           exception
106                 when no_data_found then
107                   l_unit_ret_in_corp := FALSE;
108           end;
109 
110 
111           -- Bug 5337905
112           begin
113 
114                -- check if there were balance transfers in Tax book due to changes to DIST IDs in Corp book.(done via ret api)
115                -- l_balance_tfr_in_tax will still remain zero for the partial retirement after reinstatement being copied to TAX.
116                begin
117 
118                   if nvl(ret.mrc_sob_type_code,'P') <> 'R' then
119                     select count(*)
120                       into l_balance_tfr_in_tax
121                     from fa_adjustments
122                     where book_type_code = RET.book
123                       and asset_id = RET.asset_id
124                       and transaction_header_id = l_id_out -- Corp's TRANSFER OUT THID
125                       and source_type_code in ('TRANSFER', 'RETIREMENT')
126                       and adjustment_amount <> 0
127                       and rownum = 1;
128                   else
129                     select count(*)
130                       into l_balance_tfr_in_tax
131                     from fa_mc_adjustments
132                     where book_type_code = RET.book
133                       and asset_id = RET.asset_id
134                       and transaction_header_id = l_id_out -- Corp's TRANSFER OUT THID
135                       and source_type_code in ('TRANSFER', 'RETIREMENT') -- TRANSFER for part-ret, RETIREMENT for reinst of full retirement
136                       and adjustment_amount <> 0
137                       and set_of_books_id = ret.set_of_books_id
138                       and rownum = 1;
139                   end if;
140 
141                exception
142                   when no_data_found then null;
143                   when others then
144                     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
145                     raise faginfo_error;
146                end;
147 
148           end;
149 
150 
151           -- issue #3 in Bug 4398887
152           begin
153 
154             if (p_log_level_rec.statement_level) then
155                  fa_debug_pkg.add(l_calling_fn, '++ IN FAGINFO: l_balance_tfr_in_tax BEFORE code for issue#3', l_balance_tfr_in_tax, p_log_level_rec => p_log_level_rec);
156             end if;
157 
158             if l_balance_tfr_in_tax = 0 then
159 
160                -- check if a transaction prior to this retirement in Tax book has already used the latest active dist IDs
161                -- if yes, l_balance_tfr_in_tax will be set to 2.
162                begin
163 
164                 if nvl(ret.mrc_sob_type_code,'P') <> 'R' then
165 
166 		 select 2
167                  into l_balance_tfr_in_tax
168                  from fa_distribution_history
169                  where asset_id = RET.asset_id
170                    and transaction_header_id_out is NULL
171                    and rownum = 1
172                    and distribution_id =
173                        nvl((select max(distribution_id)
174                             from fa_adjustments
175                             where book_type_code = RET.book
176                               and asset_id = RET.asset_id
177                               and adjustment_amount <> 0
178                               and transaction_header_id < RET.th_id_in
179  	                      and distribution_id in( /*12905004 - Select max dist_id in active dists */
180  	                                             select distribution_id
181  	                                               from fa_distribution_history
182  	                                              where asset_id = RET.asset_id
183  	                                                and transaction_header_id_out is NULL)),distribution_id);
184 
185                 else
186 
187                  select 2 -- this has to be set to 2 to differ from 1 for a regular balance tfr; used for fagurt
188                  into l_balance_tfr_in_tax
189                  from fa_distribution_history
190                  where asset_id = RET.asset_id
191                    and transaction_header_id_out is NULL
192                    and rownum = 1
193                    and distribution_id =
194                        nvl((select max(distribution_id) /*Bug 8301287. There may be a case where there exists no Records*/
195                             from fa_mc_adjustments
196                             where book_type_code = RET.book
197                               and asset_id = RET.asset_id
198                               -- and source_type_code in ('ADDITION')
199                               and adjustment_amount <> 0
200                               and set_of_books_id = ret.set_of_books_id
201                               and transaction_header_id < RET.th_id_in
202  	                      and distribution_id in( /*12905004 - Select max dist_id in active dists */
203  	                                             select distribution_id
204  	                                               from fa_distribution_history
205  	                                              where asset_id = RET.asset_id
206  	                                                and transaction_header_id_out is NULL)),distribution_id);
207                 end if;
208 
209                exception
210                   when no_data_found then
211                     l_balance_tfr_in_tax := 0;
212                   when others then
213                     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
214                     raise faginfo_error;
215                end;
216 
217 
218             end if;
219 
220             if (p_log_level_rec.statement_level) then
221                fa_debug_pkg.add(l_calling_fn, '++ IN FAGINFO: l_balance_tfr_in_tax AFTER code for issue#3', l_balance_tfr_in_tax, p_log_level_rec => p_log_level_rec);
222             end if;
223 
224           end;
225 
226 
227           if set_adj_row then
228 
229             if (bk.book_class and l_unit_ret_in_corp) then -- if partial unit ret in TAX book
230 
231                if (l_balance_tfr_in_tax > 0) then
232                  --adj_row.selection_retid := 0;
233                  --adj_row.units_retired := 0;
234                  adj_row.selection_thid := 0;
235                  adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
236                  if p_log_level_rec.statement_level then
237                     fa_debug_pkg.add(l_calling_fn, '++++ selection_mode', 'FA_STD_TYPES.FA_AJ_ACTIVE', p_log_level_rec => p_log_level_rec);
238                  end if;
239                else
240                  if candidate_mode in ('RETIRE') then
241                    adj_row.selection_retid := l_ret_id_in_corp;
242                    adj_row.units_retired := l_units_retired;
243                    adj_row.selection_mode := FA_STD_TYPES.FA_AJ_RETIRE;
244                    if p_log_level_rec.statement_level then
245                       fa_debug_pkg.add(l_calling_fn, '++++ selection_mode', 'FA_STD_TYPES.FA_AJ_RETIRE', p_log_level_rec => p_log_level_rec);
246                    end if;
247                  else
248                    adj_row.selection_thid := l_id_out;
249                    adj_row.selection_mode := FA_STD_TYPES.FA_AJ_CLEAR_PARTIAL;
250                    if p_log_level_rec.statement_level then
251                       fa_debug_pkg.add(l_calling_fn, '++++ selection_mode', 'FA_STD_TYPES.FA_AJ_CLEAR_PARTIAL', p_log_level_rec => p_log_level_rec);
252                    end if;
253                  end if;
254 
255                end if;
256 
257             else
258                --adj_row.selection_retid := 0;
259                --adj_row.units_retired := 0;
260                adj_row.selection_thid := 0;
261                adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
262                if p_log_level_rec.statement_level then
263                   fa_debug_pkg.add(l_calling_fn, '++++ in ELSE:  selection_mode', 'FA_STD_TYPES.FA_AJ_ACTIVE', p_log_level_rec => p_log_level_rec);
264                end if;
265             end if;
266 
267           end if;
268 
269   end if;
270 
271 
272   unit_ret_in_corp := l_unit_ret_in_corp;
273   ret_id_in_corp := l_ret_id_in_corp;
274   th_id_out_in_corp := l_id_out;
275   balance_tfr_in_tax := l_balance_tfr_in_tax;
276 
277   return TRUE;
278 EXCEPTION
279 
280     when faginfo_error then
281             fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
282             return FALSE;
283 
284     when others then
285             fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
286             return FALSE;
287 END;
288 
289 
290 
291 /*==========================================================================*
292 |  NAME         fagitc                                                      |
293 |                                                                           |
294 |  FUNCTION     This function calculates ITC_RECAPTURED (if necessary) for  |
295 |               this retirement. The ITC_AMOUNT_ID in FA_BOOKS must not be  |
296 |               null in order for ITC_RECAPTURED to be calculated.          |
297 |                                                                           |
298 |  HISTORY      1/17/89         R Rumanang      Created                     |
299 |               12/27/89        R RUmanang      Fixed bug in itc recaptured.|
300 |                                               we should take partial itc  |
301 |                                               recaptured when partial ret.|
302 |               05/03/91        M Chan          Modified for MPL 9          |
303 |               11/17/96        S Behura        Converted into PL/SQL       |
304 *===========================================================================*/
305 
306 FUNCTION fagitc(ret in out nocopy fa_ret_types.ret_struct,
307                 bk in out nocopy fa_ret_types.book_struct,
308                 cost_frac in number, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) Return boolean IS
309 
310     no_recapture        exception;
311     fagitc_err          exception;
312 
313     h_itc_amount        number;
314     h_itc_amount_id     number(15);
315     h_retirement_id     number(15);
316     h_asset_id          number(15);
317     h_recaptured        number;
318     h_itc_recapture_id  number(15);
319     h_date_placed       date;
320     h_date_retired      date;
321     h_book              varchar2(30);
322     h_years_kept        number(5);
323     h_cost_frac         number;
324     h_cur_units         number(6);
325 
326     l_calling_fn        varchar2(40) := 'FA_GAINLOSS_UPD_PKG.fagitc';
327 
328     BEGIN <<FAGITC>>
329 
330         h_itc_amount_id := bk.itc_used;
331         h_date_placed := bk.date_in_srv;
332         h_date_retired := ret.date_retired;
333         h_book := ret.book;
334         h_cur_units := bk.cur_units;
335         h_itc_amount := bk.itc_amount;
336         h_asset_id := ret.asset_id;
337         h_retirement_id := ret.retirement_id;
338         h_cost_frac := cost_frac;
339 
340         SELECT (MONTHS_BETWEEN(trunc(h_date_retired),
341                                trunc(h_date_placed)) / 12) + 1
342         INTO   h_years_kept
343         FROM   FA_RETIREMENTS
344         WHERE  RETIREMENT_ID = h_retirement_id;
345 
346         begin
347           SELECT  farecap.itc_recapture_id ,
348                   h_itc_amount * farecap.itc_recapture_rate *
349                   h_cost_frac
350           INTO
351                   h_itc_recapture_id,
352                   h_recaptured
353           FROM    fa_itc_recapture_rates farecap,
354                   fa_itc_rates farate
355           WHERE   farecap.tax_year = farate.tax_year
356           AND     farecap.life_in_months = farate.life_in_months
357           AND     farecap.year_of_retirement = h_years_kept
358           AND     farate.itc_amount_id = h_itc_amount_id;
359 
360           EXCEPTION
361 
362              when no_data_found then
363              RAISE no_recapture;
364 
365         end;
366 
367         -- Call faxrnd in fagitc
368         if not FA_UTILS_PKG.faxrnd(h_recaptured, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
369            fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
370            RAISE fagitc_err;
371         end if;
372 
373         if (ret.mrc_sob_type_code <> 'R') then
374             UPDATE      fa_retirements fr
375             SET         fr.itc_recaptured = h_recaptured,
376                         fr.itc_recapture_id = h_itc_recapture_id
377             WHERE       fr.retirement_id = h_retirement_id;
378         else
379             UPDATE      fa_mc_retirements fr
380             SET         fr.itc_recaptured = h_recaptured,
381                         fr.itc_recapture_id = h_itc_recapture_id
382             WHERE       fr.retirement_id = h_retirement_id
383               AND       fr.set_of_books_id = ret.set_of_books_id;
384         end if;
385 
386         return(TRUE);
387 
388     EXCEPTION
389 
390         when no_recapture then
391              fa_srvr_msg.add_message(
392                  calling_fn => NULL,
393                  name       => 'FA_RET_NO_ITC',
394                  token1     => 'MODULE',
395                  value1     => 'FAGITC', p_log_level_rec => p_log_level_rec);
396              return TRUE;
397 
398         when others then
399              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
400              return FALSE;
401 
402     END FAGITC;    -- End of Function FAGITC
403 
404 /*===========================================================================*
405 | NAME          fagurt                                                       |
406 |                                                                            |
407 | FUNCTION                                                                   |
408 |       It calculates GAIN/LOSS, NBV_RETIRED, STL_DEPRN_AMOUNT. Update the   |
409 | status in FA_RETIREMENTS table from 'PENDING' to 'PROCESSED'. It           |
410 | also inserts  GAIN/LOSS, PROCEEDS_OF_SALE, and COST_OF_REMOVAL to          |
411 | FA_ADJUSTMENTS table.                                                      |
412 |                                                                            |
413 |  HISTORY     01/12/89    R Rumanang  Created                               |
414 |              08/30/89    R Rumanang  Updated to insert to                  |
415 |                                      FA_ADJUSTMENTS.                       |
416 |              01/31/90    R Rumanang  Insert PROCEEDS_OF_SALE to            |
417 |                                      ADJUSTMENT                            |
418 |              05/03/91    M Chan      Rewrote for MPL 9                     |
419 |              12/30/96    S Behura    Rewriting in PL/SQL                   |
420 *============================================================================*/
421 
422 FUNCTION fagurt(ret       in out nocopy fa_ret_types.ret_struct,
423                 bk        in out nocopy fa_ret_types.book_struct,
424                 cpd_ctr          number,
425                 dpr       in out nocopy FA_STD_TYPES.dpr_struct,
426                 cost_frac in     number,
427                 retpdnum  in out nocopy number,
428                 today     in     date,
429                 user_id          number,
430                 p_log_level_rec  in FA_API_TYPES.log_level_rec_type) return boolean IS
431 
432     reval_deprn_amt     number;
433     reval_amort_amt     number;
434     cost_of_removal     number;
435     proceeds_of_sale    number;
436     proc_of_sale_clearing_acct          varchar2(26);
437     proceeds_of_sale_gain_acct          varchar2(26);
438     proceeds_of_sale_loss_acct          varchar2(26);
439     cost_of_removal_clearing_acct       varchar2(26);
440     cost_of_removal_gain_acct           varchar2(26);
441     cost_of_removal_loss_acct           varchar2(26);
442     nbv_retired_gain_acct               varchar2(26);
443     nbv_retired_loss_acct               varchar2(26);
444     reval_rsv_retired_gain_acct         varchar2(26);
445     reval_rsv_retired_loss_acct         varchar2(26);
446 
447     --adj_row     FA_STD_TYPES.fa_adj_row_struct;
448     adj_row     FA_ADJUST_TYPE_PKG.fa_adj_row_struct;
449 
450     h_dr_cr_flag        number;
451     h_retire_reval_flag number;
452     h_retirement_id     number(15);
453     h_th_id_in          number(15);
454     h_asset_id          number(15);
455     h_user_id           number(15);
456     h_nbv_retired       number;
457     h_g_nbv_retired     number; --Bug11886090
458     h_gain_loss         number;
459     h_reval_rsv_retired number;
460     h_bonus_rsv_retired number;
461     h_impair_rsv_retired number;
462     h_stl_deprn         number;
463     h_unrevalued_cost_retired   number;
464     h_dist_book         varchar2(30);
465     h_today             date;
466     h_book              varchar2(30);
467     h_proc_of_sale_clearing_acct        varchar2(26);
468     h_proceeds_of_sale_gain_acct        varchar2(26);
469     h_proceeds_of_sale_loss_acct        varchar2(26);
470     h_cost_of_removal_clr_acct          varchar2(26);
471     h_cost_of_removal_gain_acct         varchar2(26);
472     h_cost_of_removal_loss_acct         varchar2(26);
473     h_nbv_retired_gain_acct             varchar2(26);
474     h_nbv_retired_loss_acct             varchar2(26);
475     h_reval_rsv_retired_gain_acct       varchar2(26);
476     h_reval_rsv_retired_loss_acct       varchar2(26);
477 
478     X_LAST_UPDATE_DATE date := sysdate;
479     X_last_updated_by number := -1;
480     X_last_update_login number := -1;
481 
482     h_bonus_deprn_amt   number := 0;
483     h_impairment_amt    number := 0;
484 
485     l_calling_fn        varchar2(40) := 'FA_GAINLOSS_UPD_PKG.fagurt';
486 
487     /* Bug2316862 */
488     l_dist_id           NUMBER;
489     l_ccid              NUMBER;
490     l_location_id       NUMBER;
491     l_th_id_out         NUMBER;
492     l_new_dist_id       NUMBER;
493     l_dist_cost         NUMBER;
494     l_dist_reserve      NUMBER;
495 
496     /* bug 3519644 */
497     l_assigned_to       NUMBER;
498     total_adj_amount    NUMBER;
499     loop_counter        NUMBER;
500     tot_dist_lines      NUMBER;
501     /* bug 3519644 */
502 
503     l_unit_ret_in_corp  boolean;
504     l_ret_id_in_corp    number;
505     h_id_out            number;
506     l_balance_tfr_in_tax  number;
507 
508     l_dist_imp_rsv      NUMBER; --hh: 6867027
509 
510 
511     CURSOR c_ret_dists IS
512     SELECT DISTRIBUTION_ID,
513            CODE_COMBINATION_ID,
514            LOCATION_ID,
515            ASSIGNED_TO, -- bug 3519644
516            TRANSACTION_HEADER_ID_OUT
517     FROM   FA_DISTRIBUTION_HISTORY dist,
518            FA_BOOK_CONTROLS bc
519     -- Bug 5149832 WHERE  RETIREMENT_ID  = ret.retirement_id
520     WHERE  RETIREMENT_ID  = nvl(l_ret_id_in_corp, ret.retirement_id)
521     AND    ASSET_ID       = ret.asset_id
522     -- Bug 5149832 AND    BOOK_TYPE_CODE = ret.book;
523     AND    bc.book_type_code = ret.book
524     AND    dist.BOOK_TYPE_CODE = bc.distribution_source_book;
525 
526     CURSOR c_new_dist (c_ccid        number,
527                        c_location_id number,
528                        c_assigned_to number,  -- bug 3519644
529                        c_th_id_out   number) IS
530     SELECT DISTRIBUTION_ID
531     FROM   FA_DISTRIBUTION_HISTORY dist,
532            FA_BOOK_CONTROLS bc
533     WHERE  dist.TRANSACTION_HEADER_ID_IN = c_th_id_out
534     AND    dist.TRANSACTION_HEADER_ID_OUT is NULL
535     AND    dist.CODE_COMBINATION_ID = c_ccid
536     AND    dist.LOCATION_ID = c_location_id
537     AND    dist.ASSET_ID       = ret.asset_id
538     -- Bug 5149832 AND    BOOK_TYPE_CODE = ret.book
539     AND    bc.book_type_code = RET.book
540     AND    dist.BOOK_TYPE_CODE = bc.distribution_source_book
541     AND    nvl (dist.assigned_to, -99) = nvl (c_assigned_to, -99); -- bug 3519644
542 
543 
544     -- NEW for TAX
545     CURSOR c_new_dist_tax (c_ccid        number,
546                            c_location_id number,
547                            c_assigned_to number,
548                            c_th_id_out   number) IS
549     SELECT DISTRIBUTION_ID
550     FROM   FA_DISTRIBUTION_HISTORY dist,
551            FA_BOOK_CONTROLS bc
552     WHERE
553            dist.CODE_COMBINATION_ID = c_ccid
554     AND    dist.LOCATION_ID = c_location_id
555     AND    dist.ASSET_ID       = RET.asset_id
556     AND    bc.book_type_code = RET.book
557     AND    bc.book_class = 'TAX'
558     AND    dist.BOOK_TYPE_CODE = bc.distribution_source_book
559     AND    dist.transaction_header_id_in =
560           (select max(adj.transaction_header_id) -- get the latest THID in the same period that caused DIST ID to change
561            from fa_adjustments adj
562            where adj.book_type_code = RET.book
563              and adj.asset_id = RET.asset_id
564              and adj.source_type_code in ('RETIREMENT', 'TRANSFER') -- RETIREMENT: balance tfr for Reinstatement, TRANSFER: balance tfr for Retirement
565              and adj.period_counter_created = cpd_ctr
566              and adj.adjustment_type = 'COST'
567              and not exists -- check to see if adj.THID is from Corp
568                  (select 1
569                   from fa_transaction_headers th
570                   where th.transaction_header_id = adj.transaction_header_id
571                     and th.book_type_code = RET.book
572                     and th.asset_id = RET.asset_id
573                  )
574           )
575     ;
576 
577 
578 
579     /* BUG 2665163. No NBV RETIRED row in fa_adj.
580        Modified c_ret_amount cursor to handle TAX book as well */
581     CURSOR c_ret_amount (c_asset_id       number) is
582     SELECT DISTRIBUTION_ID,
583            CODE_COMBINATION_ID
584     FROM   FA_DISTRIBUTION_HISTORY dist,
585            FA_BOOK_CONTROLS bc
586     WHERE  TRANSACTION_HEADER_ID_OUT is NULL
587     AND    ASSET_ID       = c_asset_id
588     AND    bc.book_type_code = ret.book
589     AND    dist.book_type_code = bc.distribution_source_book;
590 
591 
592     CURSOR c_ret_rsv_costs (c_dist_id           number,
593                             c_new_dist_id       number,
594                             c_source_type_code  varchar2,
595                             c_adjustment_type   varchar2) IS
596     SELECT SUM(NVL(DECODE(DEBIT_CREDIT_FLAG,
597                             'CR', ADJUSTMENT_AMOUNT,
598                                   -1 * ADJUSTMENT_AMOUNT), 0)*
599                DECODE(ADJUSTMENT_TYPE, 'RESERVE', -1,
600                                        'IMPAIR RESERVE', -1, 1)) --hh: 6867027
601     FROM   FA_ADJUSTMENTS
602     WHERE  (DISTRIBUTION_ID       = c_dist_id
603          OR DISTRIBUTION_ID       = c_new_dist_id)
604     AND    BOOK_TYPE_CODE         = ret.book
605     AND    PERIOD_COUNTER_CREATED = cpd_ctr
606     AND    SOURCE_TYPE_CODE       = c_source_type_code
607     AND    ADJUSTMENT_TYPE        = c_adjustment_type
608     AND    TRANSACTION_HEADER_ID  = ret.th_id_in;
609 
610     CURSOR c_ret_rsv_costs_mrc (c_dist_id           number,
611                             c_new_dist_id       number,
612                             c_source_type_code  varchar2,
613                             c_adjustment_type   varchar2) IS
614     SELECT SUM(NVL(DECODE(DEBIT_CREDIT_FLAG,
615                             'CR', ADJUSTMENT_AMOUNT,
616                                   -1 * ADJUSTMENT_AMOUNT), 0)*
617                DECODE(ADJUSTMENT_TYPE, 'RESERVE', -1,
618                                        'IMPAIR RESERVE', -1, 1)) --hh: 6867027
619     FROM   FA_MC_ADJUSTMENTS
620     WHERE  (DISTRIBUTION_ID       = c_dist_id
621          OR DISTRIBUTION_ID       = c_new_dist_id)
622     AND    BOOK_TYPE_CODE         = ret.book
623     AND    PERIOD_COUNTER_CREATED = cpd_ctr
624     AND    SOURCE_TYPE_CODE       = c_source_type_code
625     AND    ADJUSTMENT_TYPE        = c_adjustment_type
626     AND    set_of_books_id        = ret.set_of_books_id
627     AND    TRANSACTION_HEADER_ID  = ret.th_id_in;
628     /* End of Bug2316862 */
629     /* Bug2316862 was causing a data corruption.
630        Fixed by BUG#2626812 */
631 
632     -- +++++ Get Current Unit of Group Asset +++++
633     CURSOR c_get_unit (c_asset_id number) is
634       select units
635       from   fa_asset_history
636       where  asset_id = c_asset_id
637       and    transaction_header_id_out is null;
638 
639     -- +++++ Get Group's reserve retired +++++
640     -- At this stage reserve retired entry against
641     -- Group has member asset's transaction_header_id.
642     -- This will be updated to group one in
643     -- FA_RETIREMENT_PVT.Do_Retirement_in_CGL
644     --
645     CURSOR c_get_g_rsv_ret is
646       select adjustment_amount
647       from   fa_adjustments
648       where  asset_id = bk.group_asset_id
649       and    book_type_code = ret.book
650       and    transaction_header_id = ret.th_id_in
651       and    adjustment_type = 'RESERVE';
652 
653     CURSOR c_get_g_rsv_ret_mrc is  --Bug 9103418
654       select adjustment_amount
655       from   fa_mc_adjustments
656       where  asset_id = bk.group_asset_id
657       and    book_type_code = ret.book
658       and    transaction_header_id = ret.th_id_in
659       and    adjustment_type = 'RESERVE'
660       and    set_of_books_id = ret.set_of_books_id;
661 
662     CURSOR c_get_nbv_ret is
663       select sum(decode(adjustment_type
664                ,'COST', decode(debit_credit_flag,'CR', nvl(adjustment_amount,0), -1 * nvl(adjustment_amount,0))
665                ,'RESERVE', decode(debit_credit_flag,'DR', -1 * nvl(adjustment_amount,0), nvl(adjustment_amount,0))
666                ,'IMPAIR RESERVE', decode(debit_credit_flag,'DR', -1 * nvl(adjustment_amount,0), nvl(adjustment_amount,0))
667              ,0))
668       from   fa_adjustments
669       where  asset_id = ret.asset_id
670       and    book_type_code = ret.book
671       and    transaction_header_id = ret.th_id_in
672       and    source_type_code='RETIREMENT'
673       and    adjustment_type in ('COST', 'RESERVE', 'IMPAIR RESERVE');  --hh: 6867027
674 
675    --created for bug# 5086360
676     CURSOR c_get_nbv_ret_mrc is
677       select sum(decode(adjustment_type
678                ,'COST', decode(debit_credit_flag,'CR', nvl(adjustment_amount,0), -1 * nvl(adjustment_amount,0))
679                ,'RESERVE', decode(debit_credit_flag,'DR', -1 * nvl(adjustment_amount,0), nvl(adjustment_amount,0))
680                ,'IMPAIR RESERVE', decode(debit_credit_flag,'DR', -1 * nvl(adjustment_amount,0), nvl(adjustment_amount,0))
681              ,0))
682       from   fa_mc_adjustments
683       where  asset_id = ret.asset_id
684       and    book_type_code = ret.book
685       and    transaction_header_id = ret.th_id_in
686       and    source_type_code='RETIREMENT'
687       and    set_of_books_id = ret.set_of_books_id
688       and    adjustment_type in ('COST', 'RESERVE', 'IMPAIR RESERVE'); --hh: 6867027
689 
690 
691     /* Bug2425233 */
692     l_adj_type    VARCHAR2(15);
693 
694     -- +++++ Records to hold member asset and group asset info +++++
695     l_asset_cat_rec_m  FA_API_TYPES.asset_cat_rec_type;
696     l_asset_hdr_rec_g  FA_API_TYPES.asset_hdr_rec_type;
697     l_asset_cat_rec_g  FA_API_TYPES.asset_cat_rec_type;
698 
699     l_g_reserve        NUMBER;
700 
701     l_prev_leveling_flag BOOLEAN; -- Bug 6666666
702 
703 BEGIN <<FAGURT>>
704 
705        h_retire_reval_flag := 0;
706        reval_deprn_amt := 0;
707        reval_amort_amt := 0;
708        cost_of_removal := 0;
709        proceeds_of_sale := 0;
710        h_nbv_retired := 0;
711        h_g_nbv_retired := 0; --Bug11886090
712        h_gain_loss := 0;
713        h_reval_rsv_retired := 0;
714        h_bonus_rsv_retired := 0;
715        h_unrevalued_cost_retired := 0;
716        h_impair_rsv_retired := 0;
717        h_stl_deprn := 0;
718 
719        h_th_id_in := ret.th_id_in;
720        h_asset_id := ret.asset_id;
721        h_retirement_id := ret.retirement_id;
722        h_user_id := user_id;
723        h_today := today;
724 
725        h_book := ret.book;
726        h_dist_book := bk.dis_book;
727 
728        if (bk.group_asset_id is not null) and
729           (nvl(bk.member_rollup_flag, 'N') = 'N') then
730           if (ret.mrc_sob_type_code <> 'R') then --Bug 9103418
731           OPEN c_get_g_rsv_ret;
732           FETCH c_get_g_rsv_ret INTO l_g_reserve;
733           CLOSE c_get_g_rsv_ret;
734           else
735           OPEN c_get_g_rsv_ret_mrc;
736           FETCH c_get_g_rsv_ret_mrc INTO l_g_reserve;
737           CLOSE c_get_g_rsv_ret_mrc;
738           end if;
739 
740           -- fix for bug 3627497
741           --h_nbv_retired := (ret.cost_retired + ret.cost_of_removal) -
742           --                 (ret.proceeds_of_sale + l_g_reserve);
743           --Bug11886090
744           h_g_nbv_retired := ret.cost_retired - nvl(l_g_reserve,0); --bug fix3639923
745 
746        elsif ret.wip_asset > 0 then
747           h_nbv_retired := ret.cost_retired;
748           h_g_nbv_retired := null; --Bug11886090
749 
750           if (p_log_level_rec.statement_level) then
751             fa_debug_pkg.add(l_calling_fn, '++ IN FAGURT(1.2): in ret.wip_asset > 0', '');
752           end if;
753 
754        end if;
755 
756        if (nvl(ret.wip_asset, 0) <= 0) then
757 
758          if (not((bk.group_asset_id is not null) and
759                  (nvl(bk.member_rollup_flag, 'N') = 'N'))) then
760             h_g_nbv_retired := null;
761          end if;
762 
763          -- Bug#8220521:fetching c_get_nbv_ret even in case of full_retirement.
764          --created for bug# 5086360
765           if (ret.mrc_sob_type_code <> 'R') then
766                   OPEN c_get_nbv_ret;
767                   FETCH c_get_nbv_ret INTO h_nbv_retired;
768                   CLOSE c_get_nbv_ret;
769           else
770                   OPEN c_get_nbv_ret_mrc;
771                   FETCH c_get_nbv_ret_mrc INTO h_nbv_retired;
772                   CLOSE c_get_nbv_ret_mrc;
773           end if;
774           if (p_log_level_rec.statement_level) then
775             fa_debug_pkg.add(l_calling_fn, '++ IN FAGURT(1.2): in else ret.units_retired is not null', '');
776           end if;
777 
778        end if;
779 
780        if (p_log_level_rec.statement_level) then
781            fa_debug_pkg.add(l_calling_fn, '++ IN FAGURT(1.5): ret.cost_retired', ret.cost_retired);
782            fa_debug_pkg.add(l_calling_fn, 'ret.rsv_retired', ret.rsv_retired, p_log_level_rec => p_log_level_rec);
783        end if;
784 
785        h_unrevalued_cost_retired := cost_frac * bk.unrevalued_cost;
786 
787        /****************************************************************
788          Calculate stl_deprn . The dpr.y_begin and dpr.y_end had been
789          setup in retire routine. However, if h_deprn_lastyr_flag is 1,
790          we should not take the last year depreciation. Thus, we
791          decrease dpr.y_end by 1. If dpr.y_end is less than dpr.y_begin,
792          we set pd_num = 0 which is special value and caused it NOT to
793          caluculate the stl depreciation.
794          ****************************************************************/
795 
796        /**************************************************************
797          If the asset is WIP, no STL calculation is required.
798          If the STL_METHOD_ID = 0, it implies that NO STL method is
799          used; no calculation is needed.
800          ************************************************************/
801 
802        if (not bk.depreciate_lastyr) and (bk.book_class) and
803           (ret.wip_asset is null or ret.wip_asset <= 0) and
804           (ret.stl_method_code is not null) then
805 
806           dpr.y_end := dpr.y_end - 1;
807           retpdnum := bk.pers_per_yr;
808 
809           if dpr.y_end < dpr.y_begin then
810              retpdnum := 0;                     -- Special value
811           end if;
812        end if;
813 
814        if (bk.book_class) and (retpdnum <> 0) and
815           (ret.wip_asset is null or ret.wip_asset <= 0) and
816           (ret.stl_method_code is not null) then
817 
818           dpr.method_code := ret.stl_method_code;
819           dpr.life := ret.stl_life;
820 
821            /**************************************************************
822              For STL depreciation calculation, the old deprn reserve
823              should not be used. zero depreciation reserve should be used.
824              ************************************************************/
825           dpr.deprn_rsv := 0;
826 
827           if not FA_GAINLOSS_DPR_PKG.fagcdp(dpr, h_stl_deprn,
828                              reval_deprn_amt, h_bonus_deprn_amt,
829                              h_impairment_amt,
830                              reval_amort_amt, bk.deprn_start_date,
831                              bk.d_cal, bk.p_cal, 0, retpdnum, bk.prorate_fy,
832                              bk.dsd_fy, bk.prorate_jdate,
833                              bk.deprn_start_jdate, p_log_level_rec => p_log_level_rec) then
834 
835                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
836                return(FALSE);
837 
838           end if;
839 
840           h_stl_deprn := cost_frac * (h_stl_deprn + reval_deprn_amt);
841 
842        end if;
843 
844        select  bc.proceeds_of_sale_clearing_acct,
845                bc.proceeds_of_sale_gain_acct,
846                bc.proceeds_of_sale_loss_acct,
847                bc.cost_of_removal_clearing_acct,
848                bc.cost_of_removal_gain_acct,
849                bc.cost_of_removal_loss_acct,
850                bc.nbv_retired_gain_acct,
851                bc.nbv_retired_loss_acct,
852                bc.reval_rsv_retired_gain_acct,
853                bc.reval_rsv_retired_loss_acct,
854                decode(bc.retire_reval_reserve_flag,'NO',0,1),
855                ad.asset_category_id
856        into    h_proc_of_sale_clearing_acct,
857                h_proceeds_of_sale_gain_acct,
858                h_proceeds_of_sale_loss_acct,
859                h_cost_of_removal_clr_acct,
860                h_cost_of_removal_gain_acct,
861                h_cost_of_removal_loss_acct,
862                h_nbv_retired_gain_acct,
863                h_nbv_retired_loss_acct,
864                h_reval_rsv_retired_gain_acct,
865                h_reval_rsv_retired_loss_acct,
866                h_retire_reval_flag,
867                l_asset_cat_rec_m.category_id
868        from    fa_book_controls bc,
869                fa_additions_b ad, fa_category_books cb
870        where   ad.asset_id = h_asset_id
871        and     cb.category_id = ad.asset_category_id
872        and     cb.book_type_code = h_book
873        and     bc.book_type_code = cb.book_type_code;
874 
875        proc_of_sale_clearing_acct := h_proc_of_sale_clearing_acct;
876        proceeds_of_sale_gain_acct := h_proceeds_of_sale_gain_acct;
877        proceeds_of_sale_loss_acct := h_proceeds_of_sale_loss_acct;
878        cost_of_removal_clearing_acct := h_cost_of_removal_clr_acct;
879        cost_of_removal_gain_acct := h_cost_of_removal_gain_acct;
880        cost_of_removal_loss_acct := h_cost_of_removal_loss_acct;
881        nbv_retired_gain_acct := h_nbv_retired_gain_acct;
882        nbv_retired_loss_acct := h_nbv_retired_loss_acct;
883        reval_rsv_retired_gain_acct := h_reval_rsv_retired_gain_acct;
884        reval_rsv_retired_loss_acct := h_reval_rsv_retired_loss_acct;
885 
886        if h_retire_reval_flag = 0 then
887           ret.reval_rsv_retired := 0;
888        end if;
889 
890        h_reval_rsv_retired := ret.reval_rsv_retired;
891        h_bonus_rsv_retired := ret.bonus_rsv_retired;
892        h_impair_rsv_retired := ret.impair_rsv_retired;
893 
894        -- Bug#5037745: added nvl to reval_rsv_retired
895        -- Bug11886090: include h_g_nbv_retired
896        h_gain_loss := (ret.proceeds_of_sale + nvl(ret.reval_rsv_retired,0)) -
897                       (nvl(h_g_nbv_retired, h_nbv_retired) + ret.cost_of_removal); /*13536787 - No need to substract impair reserve retired again as it is already
898 		                                                          considered in nbv calc. */
899        if (p_log_level_rec.statement_level) then
900            fa_debug_pkg.add(l_calling_fn, '++ IN FAGURT(2): ret.proceeds_of_sale', ret.proceeds_of_sale);
901            fa_debug_pkg.add(l_calling_fn, 'ret.cost_of_removal', ret.cost_of_removal, p_log_level_rec => p_log_level_rec);
902            fa_debug_pkg.add(l_calling_fn, 'ret.reval_rsv_retired', ret.reval_rsv_retired, p_log_level_rec => p_log_level_rec);
903            fa_debug_pkg.add(l_calling_fn, 'h_nbv_retired', h_nbv_retired, p_log_level_rec => p_log_level_rec);
904            fa_debug_pkg.add(l_calling_fn, 'h_g_nbv_retired', h_g_nbv_retired, p_log_level_rec => p_log_level_rec);
905            fa_debug_pkg.add(l_calling_fn, 'ret.impair_rsv_retired', ret.impair_rsv_retired, p_log_level_rec => p_log_level_rec);
906            fa_debug_pkg.add(l_calling_fn, 'h_gain_loss', h_gain_loss, p_log_level_rec => p_log_level_rec);
907        end if;
908 
909        -- Call faxrnd to round nbv_retired in fagurt
910        if not FA_UTILS_PKG.faxrnd(h_nbv_retired, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
911           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
912           return(FALSE);
913        end if;
914 
915        -- Bug11886090
916        if not FA_UTILS_PKG.faxrnd(h_g_nbv_retired, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
917           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
918           return(FALSE);
919        end if;
920 
921        -- Call faxrnd to round gain_loss in fagurt
922        if not FA_UTILS_PKG.faxrnd(h_gain_loss, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
923           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
924           return(FALSE);
925        end if;
926 
927        -- Call faxrnd to round stl_deprn in fagurt
928        if not FA_UTILS_PKG.faxrnd(h_stl_deprn, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
929           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
930           return(FALSE);
931        end if;
932 
933        -- Call faxrnd to round reval_rsv_retired in fagurt
934        if not FA_UTILS_PKG.faxrnd(h_reval_rsv_retired, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
935           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
936           return(FALSE);
937        end if;
938 
939        -- Call faxrnd to round bonus_rsv_retired in fagurt
940        if not FA_UTILS_PKG.faxrnd(h_bonus_rsv_retired, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
941           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
942           return(FALSE);
943        end if;
944 
945        -- Call faxrnd to round impair_rsv_retired in fagurt
946        if not FA_UTILS_PKG.faxrnd(h_impair_rsv_retired, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
947           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
948           return(FALSE);
949        end if;
950 
951        -- Call faxrnd to round unrevalued_cost_retired in fagurt
952        if not FA_UTILS_PKG.faxrnd(h_unrevalued_cost_retired, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
953           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
954           return(FALSE);
955        end if;
956 
957        if (ret.mrc_sob_type_code <> 'R') then
958                 UPDATE  fa_retirements fr
959                 SET     fr.nbv_retired   = nvl(h_g_nbv_retired, h_nbv_retired), --Bug11886090
960                         fr.gain_loss_amount = h_gain_loss,
961                         fr.stl_deprn_amount = h_stl_deprn,
962                         fr.reval_reserve_retired = h_reval_rsv_retired,
963                         bonus_reserve_retired = h_bonus_rsv_retired,
964                         impair_reserve_retired = h_impair_rsv_retired,
965                         fr.unrevalued_cost_retired =
966                                         h_unrevalued_cost_retired,
967                         fr.reserve_retired = nvl(fr.reserve_retired,ret.reserve_retired),
968                         fr.status                = 'PROCESSED',
969                         fr.last_update_date = h_today,
970                         fr.last_updated_by  = h_user_id
971                 WHERE
972                         fr.retirement_id         = h_retirement_id;
973        else
974                 UPDATE  fa_mc_retirements fr
975                 SET     fr.nbv_retired   = nvl(h_g_nbv_retired, h_nbv_retired), --Bug11886090
976                         fr.gain_loss_amount = h_gain_loss,
977                         fr.stl_deprn_amount = h_stl_deprn,
978                         fr.reval_reserve_retired = h_reval_rsv_retired,
979                         bonus_reserve_retired = h_bonus_rsv_retired,
980                         impair_reserve_retired = h_impair_rsv_retired,
981                         fr.unrevalued_cost_retired =
982                                         h_unrevalued_cost_retired,
983                         fr.reserve_retired = nvl(fr.reserve_retired,ret.reserve_retired),
984                         fr.status                = 'PROCESSED',
985                         fr.last_update_date = h_today,
986                         fr.last_updated_by  = h_user_id
987                 WHERE
988                         fr.retirement_id         = h_retirement_id
989                   AND   set_of_books_id = ret.set_of_books_id;
990        end if;
991 
992     /* If gain it's 1(CR), else it's 0(DR) */
993     /* h_dr_cr_flag = (int) ((h_gain_loss < 0) ? 0 : 1); */
994 
995        if h_gain_loss < 0 then
996           h_dr_cr_flag := 0;
997        else
998           h_dr_cr_flag := 1;
999        end if;
1000 
1001     /* Note that we debit or credit the account based on the gain-loss.
1002        The amount that we inserted into the table must be positive.
1003     */
1004 
1005        -- Setting l_unit_ret_in_corp
1006        if NOT faginfo(
1007                 RET, BK, cpd_ctr,today, user_id
1008                ,calling_module => l_calling_fn
1009                ,candidate_mode => 'RETIRE'
1010                ,set_adj_row => FALSE -- just to get l_unit_ret_in_corp and h_id_out
1011                ,unit_ret_in_corp => l_unit_ret_in_corp
1012                ,ret_id_in_corp => l_ret_id_in_corp
1013                ,th_id_out_in_corp => h_id_out
1014                ,balance_tfr_in_tax => l_balance_tfr_in_tax
1015                ,adj_row => adj_row
1016                ,p_log_level_rec => p_log_level_rec) then
1017                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1018                return(FALSE);
1019        end if;
1020 
1021        if (p_log_level_rec.statement_level) then
1022           fa_debug_pkg.add(l_calling_fn, '++++++ AFTER faginfo to get some variables...', '...', p_log_level_rec => p_log_level_rec);
1023           fa_debug_pkg.add(l_calling_fn, '++ l_unit_ret_in_corp', l_unit_ret_in_corp, p_log_level_rec => p_log_level_rec);
1024           fa_debug_pkg.add(l_calling_fn, '++ l_ret_id_in_corp', l_ret_id_in_corp, p_log_level_rec => p_log_level_rec);
1025           fa_debug_pkg.add(l_calling_fn, '++ h_id_out', h_id_out, p_log_level_rec => p_log_level_rec);
1026           fa_debug_pkg.add(l_calling_fn, '++ l_balance_tfr_in_tax (1=TRUE)', l_balance_tfr_in_tax);
1027        end if;
1028 
1029 
1030        adj_row.transaction_header_id := ret.th_id_in;
1031 
1032        if ret.wip_asset > 0 then
1033           adj_row.source_type_code := 'CIP RETIREMENT';
1034        else
1035           adj_row.source_type_code := 'RETIREMENT';
1036        end if;
1037 
1038        adj_row.book_type_code := ret.book;
1039        adj_row.period_counter_created := cpd_ctr;
1040        adj_row.asset_id := ret.asset_id;
1041        adj_row.period_counter_adjusted := cpd_ctr;
1042        adj_row.last_update_date := today;
1043        adj_row.current_units := bk.cur_units;
1044        adj_row.gen_ccid_flag := TRUE;
1045        adj_row.flush_adj_flag := TRUE;
1046        adj_row.annualized_adjustment := 0;
1047        adj_row.code_combination_id := 0;
1048        adj_row.distribution_id := 0;
1049        adj_row.selection_thid := 0;
1050        adj_row.asset_invoice_id := 0;
1051        adj_row.leveling_flag := TRUE;
1052 
1053 
1054        if (ret.units_retired <= 0 or ret.units_retired is null) then
1055 
1056           adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
1057           adj_row.selection_retid := 0;
1058           adj_row.units_retired := 0;
1059 
1060           if (bk.current_cost = ret.cost_retired) then
1061              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
1062           else
1063 
1064             if bk.book_class then
1065                if NOT faginfo(
1066                             RET, BK, cpd_ctr,today, user_id
1067                            ,calling_module => l_calling_fn
1068                            ,candidate_mode => 'RETIRE'
1069                            ,set_adj_row => TRUE -- set adj_row
1070                            ,unit_ret_in_corp => l_unit_ret_in_corp
1071                            ,ret_id_in_corp => l_ret_id_in_corp
1072                            ,th_id_out_in_corp => h_id_out
1073                            ,balance_tfr_in_tax => l_balance_tfr_in_tax
1074                            ,adj_row => adj_row
1075                            ,p_log_level_rec => p_log_level_rec            ) then
1076                   fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1077                   return(FALSE);
1078                end if;
1079             end if;
1080 
1081           end if;
1082 
1083        else
1084           adj_row.selection_mode := FA_STD_TYPES.FA_AJ_RETIRE;
1085           adj_row.selection_retid := ret.retirement_id;
1086           adj_row.units_retired := ret.units_retired;
1087        end if;
1088 
1089        if (bk.group_asset_id is not null) and
1090           (nvl(bk.member_rollup_flag, 'N') = 'N') then
1091           if not fa_cache_pkg.fazccb
1092                   (X_book   => ret.book,
1093                    X_cat_id => l_asset_cat_rec_m.category_id, p_log_level_rec => p_log_level_rec) then
1094              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1095              return(FALSE);
1096           end if;
1097 
1098        end if;
1099 
1100        -- +++++ BUG 2669638: Change operator from > to <> +++++
1101        -- +++++ Create PROCEEDS/RESERVE if this is not member asset with ALLOCATE +++++
1102        --8244128 Changed the check from Allocate to member rollup flag
1103        --8546627 Added condition for Stand Alone assets
1104        --8681627 Modified the check for member_rollup_flag to tracking method
1105        if (ret.proceeds_of_sale <> 0)  and
1106           (((bk.group_asset_id is not null) and (bk.tracking_method is not null)) or bk.group_asset_id is null)  then
1107 
1108           if (bk.group_asset_id is not null) and
1109              (nvl(bk.member_rollup_flag, 'N') = 'N') then
1110              adj_row.track_member_flag := 'Y';
1111           else
1112              adj_row.track_member_flag := null;
1113           end if;
1114 
1115           adj_row.adjustment_type := 'PROCEEDS';
1116 
1117           adj_row.adjustment_amount := ret.proceeds_of_sale;
1118 
1119           if h_dr_cr_flag = 1 then
1120              adj_row.account := proceeds_of_sale_gain_acct;
1121              adj_row.account_type := 'PROCEEDS_OF_SALE_GAIN_ACCT';
1122           else
1123              adj_row.account := proceeds_of_sale_loss_acct;
1124              adj_row.account_type := 'PROCEEDS_OF_SALE_LOSS_ACCT';
1125           end if;
1126 
1127           adj_row.debit_credit_flag := 'CR';
1128           adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1129           adj_row.set_of_books_id := ret.set_of_books_id;
1130 
1131           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1132                                        X_last_update_date,
1133                                        X_last_updated_by,
1134                                        X_last_update_login,
1135                                        p_log_level_rec => p_log_level_rec)) then
1136                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1137                 return(FALSE);
1138 
1139           end if;
1140 
1141           -- added for bug 3627497
1142           if (bk.group_asset_id is not null) and
1143              (nvl(bk.member_rollup_flag, 'N') = 'N') then
1144 
1145             adj_row.adjustment_type := 'PROCEEDS CLR';
1146             adj_row.adjustment_amount := ret.proceeds_of_sale;
1147             adj_row.account := proc_of_sale_clearing_acct;
1148             adj_row.account_type := 'PROCEEDS_OF_SALE_CLEARING_ACCT';
1149             adj_row.debit_credit_flag := 'DR';
1150             adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1151             adj_row.set_of_books_id := ret.set_of_books_id;
1152 
1153             if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1154                                        X_last_update_date,
1155                                        X_last_updated_by,
1156                                        X_last_update_login,
1157                                        p_log_level_rec => p_log_level_rec)) then
1158                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1159                return(FALSE);
1160             end if;
1161 
1162           end if;
1163 
1164        end if; -- (ret.proceeds_of_sale <> 0) and
1165 
1166        -- +++++ BUG 2669638: Changed operator from > to <>. +++++
1167        -- +++++ Create REMOVALCOST/RESERVE if this is not member asset with ALLOCATE +++++
1168        --8244128 Changed the check from Allocate to member rollup flag
1169        --8546627 Added condition for Stand Alone assets
1170        --8681627 Modified the check for member_rollup_flag to tracking method
1171        if (ret.cost_of_removal <> 0)  and
1172           (((bk.group_asset_id is not null) and (bk.tracking_method is not null)) or bk.group_asset_id is null)  then
1173 
1174           if (bk.group_asset_id is not null) and
1175              (nvl(bk.member_rollup_flag, 'N') = 'N') then
1176              adj_row.track_member_flag := 'Y';
1177           else
1178              adj_row.track_member_flag := null;
1179           end if;
1180 
1181           adj_row.adjustment_type := 'REMOVALCOST';
1182 
1183           adj_row.adjustment_amount := ret.cost_of_removal;
1184 
1185           if h_dr_cr_flag = 1 then
1186              adj_row.account := cost_of_removal_gain_acct;
1187              adj_row.account_type := 'COST_OF_REMOVAL_GAIN_ACCT';
1188           else
1189              adj_row.account := cost_of_removal_loss_acct;
1190              adj_row.account_type := 'COST_OF_REMOVAL_LOSS_ACCT';
1191           end if;
1192 
1193           adj_row.debit_credit_flag := 'DR';
1194           adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1195           adj_row.set_of_books_id := ret.set_of_books_id;
1196 
1197           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1198                                        X_last_update_date,
1199                                        X_last_updated_by,
1200                                        X_last_update_login,
1201                                        p_log_level_rec => p_log_level_rec)) then
1202              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1203              return(FALSE);
1204 
1205           end if;
1206 
1207 
1208         -- added for bug 3627497
1209         if (bk.group_asset_id is not null) and
1210            (nvl(bk.member_rollup_flag, 'N') = 'N') then
1211           adj_row.adjustment_type := 'REMOVALCOST CLR';
1212           adj_row.adjustment_amount := ret.cost_of_removal;
1213           adj_row.account := cost_of_removal_clearing_acct;
1214           adj_row.account_type := 'COST_OF_REMOVAL_CLEARING_ACCT';
1215           adj_row.debit_credit_flag := 'CR';
1216           adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1217           adj_row.set_of_books_id := ret.set_of_books_id;
1218 
1219           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1220                                        X_last_update_date,
1221                                        X_last_updated_by,
1222                                        X_last_update_login,
1223                                        p_log_level_rec => p_log_level_rec)) then
1224                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1225                return(FALSE);
1226 
1227           end if;
1228         end if;
1229 
1230        end if; -- (ret.cost_of_removal > 0) and
1231 
1232        /* BUG 2316862
1233         * NBV RETIRED entiries are created for each distribution lines retired
1234         * to avoid rounding errors.
1235         * Fisrt of all, get distribution which is effected by retirement.
1236         * If this is partial unit retirement, get only effected lines, otherwise
1237         * all ditribution lines.
1238         * Then find out cost and reserve retired and use these to find out
1239         * NBV retired for each effected distribution line.
1240         */
1241        if (p_log_level_rec.statement_level) then
1242            fa_debug_pkg.add(fname => l_calling_fn,
1243                             element => '+++ret.cost_retired before if condition',
1244                             value   => ret.cost_retired, p_log_level_rec => p_log_level_rec);
1245            fa_debug_pkg.add(fname => l_calling_fn,
1246                             element => '+++ret.rsv_retired before if condition',
1247                             value   => ret.rsv_retired, p_log_level_rec => p_log_level_rec);
1248            fa_debug_pkg.add(fname => l_calling_fn,
1249                             element => '+++h_nbv_retired before if condition',
1250                             value   => h_nbv_retired, p_log_level_rec => p_log_level_rec);
1251            fa_debug_pkg.add(fname => l_calling_fn,
1252                             element => '+++h_g_nbv_retired before if condition',
1253                             value   => h_g_nbv_retired, p_log_level_rec => p_log_level_rec);
1254        end if;
1255 
1256        -- +++++ BUG 2669638: Changed operator from > to <> +++++
1257        --8244128 Changed the check from Allocate to member rollup flag
1258        --8546627 Added condition for Stand Alone assets
1259        --8677070 , Added condition for processing member with group having rollupflag Yes and calculate tracking
1260        --8681627 Modified the check for member_rollup_flag to tracking method
1261        if (h_nbv_retired <> 0)  and
1262          (((bk.group_asset_id is not null) and (bk.tracking_method is not null)) or bk.group_asset_id is null)  then
1263 
1264           adj_row.selection_mode := fa_adjust_type_pkg.FA_AJ_TRANSFER_SINGLE;
1265 
1266           adj_row.adjustment_type := 'NBV RETIRED';
1267           adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1268 
1269           if (bk.group_asset_id is not null) and
1270              (nvl(bk.member_rollup_flag, 'N') = 'N') then
1271              adj_row.track_member_flag := 'Y';
1272           else
1273              adj_row.track_member_flag := null;
1274           end if;
1275 
1276           if h_dr_cr_flag = 1 then
1277              adj_row.account := nbv_retired_gain_acct;
1278              adj_row.account_type := 'NBV_RETIRED_GAIN_ACCT';
1279              adj_row.debit_credit_flag := 'DR';
1280           else
1281              adj_row.account := nbv_retired_loss_acct;
1282              adj_row.account_type := 'NBV_RETIRED_LOSS_ACCT';
1283              adj_row.debit_credit_flag := 'DR';
1284           end if;
1285 
1286           -- if (ret.units_retired is null and NOT l_unit_ret_in_corp) then
1287           -- fix for issue#3 in Bug 4398887
1288           /* Cursor used in each of the following conditions
1289 
1290                     Unit Ret =  True           False
1291            ------------------- ------------   --------------
1292           Bal Tfr=0            c_ret_dists     c_ret_amount
1293           Bal Tfr=1            c_ret_amount    c_ret_amount
1294           Bal Tfr=2            c_ret_amount    c_ret_amount
1295           */
1296 
1297           if (ret.units_retired is null and NOT (l_unit_ret_in_corp and l_balance_tfr_in_tax = 0)) then
1298 
1299             OPEN c_ret_amount (ret.asset_id);
1300             if (p_log_level_rec.statement_level) then
1301                fa_debug_pkg.add(fname => l_calling_fn,
1302                                 element => '+++in amount',
1303                                 value   => 1, p_log_level_rec => p_log_level_rec);
1304             end if;
1305           else
1306             OPEN c_ret_dists;
1307             if (p_log_level_rec.statement_level) then
1308                fa_debug_pkg.add(fname => l_calling_fn,
1309                                 element => '+++in dists',
1310                                 value   => 1, p_log_level_rec => p_log_level_rec);
1311             end if;
1312           end if;
1313 
1314              /* bug 3519644 */
1315 
1316           -- bug fix 5965367 (Replaced below if clause with new if clause)
1317           --   if (ret.units_retired is null and NOT l_unit_ret_in_corp) then
1318           if (ret.units_retired is null and NOT (l_unit_ret_in_corp and l_balance_tfr_in_tax = 0)) then
1319 
1320              SELECT count(*)
1321              INTO   tot_dist_lines
1322              FROM   FA_DISTRIBUTION_HISTORY dist,
1323                     FA_BOOK_CONTROLS bc
1324              WHERE  TRANSACTION_HEADER_ID_OUT is NULL
1325              AND    ASSET_ID       = RET.asset_id
1326              AND    bc.book_type_code = RET.book
1327              AND    dist.book_type_code = bc.distribution_source_book;
1328 
1329              else
1330 
1331              SELECT count(*)
1332              INTO   tot_dist_lines
1333              FROM   FA_DISTRIBUTION_HISTORY dist,
1334                     FA_BOOK_CONTROLS bc
1335              WHERE  dist.RETIREMENT_ID  = nvl(l_ret_id_in_corp, ret.retirement_id)
1336              AND    dist.ASSET_ID       = RET.asset_id
1337              AND    bc.book_type_code = RET.book
1338              AND    dist.book_type_code = bc.distribution_source_book;
1339 
1340              end if;
1341 
1342              loop_counter     := 0;
1343              total_adj_amount := 0;
1344              /* bug 3519644 */
1345 
1346           LOOP
1347 
1348               adj_row.adjustment_type := 'NBV RETIRED';
1349               adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1350               adj_row.set_of_books_id := ret.set_of_books_id;
1351 
1352               if (bk.group_asset_id is not null) and
1353                  (nvl(bk.member_rollup_flag, 'N') = 'N') then
1354                  adj_row.track_member_flag := 'Y';
1355               else
1356                  adj_row.track_member_flag := null;
1357               end if;
1358 
1359               if h_dr_cr_flag = 1 then
1360                  adj_row.account := nbv_retired_gain_acct;
1361                  adj_row.account_type := 'NBV_RETIRED_GAIN_ACCT';
1362                  adj_row.debit_credit_flag := 'DR';
1363               else
1364                  adj_row.account := nbv_retired_loss_acct;
1365                  adj_row.account_type := 'NBV_RETIRED_LOSS_ACCT';
1366                  adj_row.debit_credit_flag := 'DR';
1367               end if;
1368              /* Fetch all distribution effected process NBV retired */
1369              --if (ret.units_retired is null and NOT l_unit_ret_in_corp ) then
1370              if (ret.units_retired is null and NOT (l_unit_ret_in_corp and l_balance_tfr_in_tax = 0)) then
1371                FETCH c_ret_amount INTO l_dist_id, l_ccid;
1372              else
1373                FETCH c_ret_dists INTO l_dist_id, l_ccid, l_location_id, l_assigned_to, l_th_id_out; -- bug 3519644
1374              end if;
1375 
1376              --if (ret.units_retired is null and NOT l_unit_ret_in_corp) then
1377              if (ret.units_retired is null and NOT (l_unit_ret_in_corp and l_balance_tfr_in_tax = 0)) then
1378                EXIT WHEN c_ret_amount%NOTFOUND;
1379              else
1380                EXIT WHEN c_ret_dists%NOTFOUND;
1381                l_new_dist_id := to_number(null);
1382 
1383                /*
1384                 * If there are new distributiion created because of retirement
1385                 * get distribution id which will be used to determine cost and reserve
1386                 * retired.
1387                 */
1388                if (l_th_id_out is not null) then
1389 
1390                   if bk.book_class then -- if TRUE=TAX
1391                     OPEN c_new_dist_tax (l_ccid, l_location_id, l_assigned_to, l_th_id_out);
1392                     FETCH c_new_dist_tax INTO l_new_dist_id;
1393                     CLOSE c_new_dist_tax;
1394                   else
1395                     OPEN c_new_dist (l_ccid, l_location_id, l_assigned_to, l_th_id_out);
1396                     FETCH c_new_dist INTO l_new_dist_id;
1397                     CLOSE c_new_dist;
1398                   end if;
1399 
1400                end if;
1401 
1402                if l_new_dist_id is null then
1403                  l_new_dist_id := l_dist_id;
1404                else
1405                  if bk.book_class and l_balance_tfr_in_tax > 0 then
1406                    l_dist_id := l_new_dist_id;
1407                  end if;
1408                end if;
1409 
1410              end if;
1411 
1412              loop_counter := loop_counter + 1; -- bug 3519644
1413              adj_row.distribution_id := l_dist_id;
1414              adj_row.code_combination_id := l_ccid;
1415 
1416 
1417              if (p_log_level_rec.statement_level) then
1418                 fa_debug_pkg.add(fname => l_calling_fn,
1419                                  element => 'l_dist_id',
1420                                  value   => l_dist_id, p_log_level_rec => p_log_level_rec);
1421                 fa_debug_pkg.add(fname => l_calling_fn,
1422                                  element => 'l_ccid',
1423                                  value   => l_ccid, p_log_level_rec => p_log_level_rec);
1424                 fa_debug_pkg.add(fname => l_calling_fn,
1425                                  element => 'l_location_id',
1426                                  value   => l_location_id, p_log_level_rec => p_log_level_rec);
1427                 fa_debug_pkg.add(fname => l_calling_fn,
1428                                  element => 'l_th_id_out',
1429                                  value   => l_th_id_out, p_log_level_rec => p_log_level_rec);
1430                 fa_debug_pkg.add(fname => l_calling_fn,
1431                                  element => 'l_new_dist_id',
1432                                  value   => l_new_dist_id, p_log_level_rec => p_log_level_rec);
1433              end if;
1434 
1435 
1436              /* Get distribution level cost retired */
1437              /* Bug2425233
1438               *   Check source type code and set ajustment type
1439               *   accordingly.  Also moved reserve query inside
1440               *   of this check so reserve query should be executed
1441               *   only if it's necessary.
1442               */
1443              if (adj_row.source_type_code <> 'CIP RETIREMENT') then
1444                l_adj_type := 'COST';
1445 
1446                 /* Get distribution level reserve retired */
1447                 /* BUG# 2626812 */
1448                 if (ret.mrc_sob_type_code <> 'R') then
1449                   OPEN c_ret_rsv_costs (adj_row.distribution_id,
1450                                         l_new_dist_id,
1451                                         'RETIREMENT',
1452                                         'RESERVE');
1453                   FETCH c_ret_rsv_costs INTO l_dist_reserve;
1454                   CLOSE c_ret_rsv_costs;
1455                 else
1456                   OPEN c_ret_rsv_costs_mrc (adj_row.distribution_id,
1457                                             l_new_dist_id,
1458                                             'RETIREMENT',
1459                                             'RESERVE');
1460                   FETCH c_ret_rsv_costs_mrc INTO l_dist_reserve;
1461                   CLOSE c_ret_rsv_costs_mrc;
1462                end if;
1463                -- HH: 6867027
1464                -- Get dist impair reserve
1465                if (ret.mrc_sob_type_code <> 'R') then
1466                   OPEN c_ret_rsv_costs (adj_row.distribution_id,
1467                                         l_new_dist_id,
1468                                         'RETIREMENT',
1469                                         'IMPAIR RESERVE');
1470                   FETCH c_ret_rsv_costs INTO l_dist_imp_rsv;
1471                   CLOSE c_ret_rsv_costs;
1472                 else
1473                   OPEN c_ret_rsv_costs_mrc (adj_row.distribution_id,
1474                                             l_new_dist_id,
1475                                             'RETIREMENT',
1476                                             'IMPAIR RESERVE');
1477                   FETCH c_ret_rsv_costs_mrc INTO l_dist_imp_rsv;
1478                   CLOSE c_ret_rsv_costs_mrc;
1479                end if;
1480 
1481              else
1482                l_adj_type := 'CIP COST';
1483                l_dist_reserve := 0;
1484              end if;
1485 
1486              if (ret.mrc_sob_type_code <> 'R') then
1487                OPEN c_ret_rsv_costs (adj_row.distribution_id,
1488                                      l_new_dist_id,
1489                                      adj_row.source_type_code,
1490                                      l_adj_type);
1491                FETCH c_ret_rsv_costs INTO l_dist_cost;
1492                CLOSE c_ret_rsv_costs;
1493              else
1494                OPEN c_ret_rsv_costs_mrc (adj_row.distribution_id,
1495                                          l_new_dist_id,
1496                                          adj_row.source_type_code,
1497                                          l_adj_type);
1498                FETCH c_ret_rsv_costs_mrc INTO l_dist_cost;
1499                CLOSE c_ret_rsv_costs_mrc;
1500              end if;
1501 
1502              if (p_log_level_rec.statement_level) then
1503                 fa_debug_pkg.add(l_calling_fn, '++++ ret.th_id_in',  ret.th_id_in, p_log_level_rec => p_log_level_rec);
1504                 fa_debug_pkg.add(l_calling_fn, '++++ ret.book',  ret.book, p_log_level_rec => p_log_level_rec);
1505                 fa_debug_pkg.add(l_calling_fn, '++++ cpd_ctr',  cpd_ctr, p_log_level_rec => p_log_level_rec);
1506                 fa_debug_pkg.add(l_calling_fn, '++++ adj_row.distribution_id',  adj_row.distribution_id, p_log_level_rec => p_log_level_rec);
1507                 fa_debug_pkg.add(l_calling_fn, '++++ adj_row.source_type_code',  adj_row.source_type_code, p_log_level_rec => p_log_level_rec);
1508                 fa_debug_pkg.add(l_calling_fn, '++++ l_adj_type',  l_adj_type, p_log_level_rec => p_log_level_rec);
1509                 fa_debug_pkg.add(l_calling_fn, '++ l_dist_cost', l_dist_cost, p_log_level_rec => p_log_level_rec);
1510                 fa_debug_pkg.add(l_calling_fn, '++ l_dist_reserve', l_dist_reserve, p_log_level_rec => p_log_level_rec);
1511                 fa_debug_pkg.add(l_calling_fn, '++ l_dist_imp_rsv', l_dist_imp_rsv, p_log_level_rec => p_log_level_rec);
1512              end if;
1513 
1514              --commented for bug 3519644
1515              --adj_row.adjustment_amount := l_dist_cost - l_dist_reserve;
1516 
1517             if (p_log_level_rec.statement_level) then
1518                fa_debug_pkg.add(fname => l_calling_fn,
1519                                 element => '+++ loop_counter',
1520                                 value   => loop_counter, p_log_level_rec => p_log_level_rec);
1521                fa_debug_pkg.add(fname => l_calling_fn,
1522                                 element => '+++ tot_dist_lines',
1523                                 value   => tot_dist_lines, p_log_level_rec => p_log_level_rec);
1524             end if;
1525 
1526 
1527             /* bug 3519644 */
1528             if (loop_counter <> tot_dist_lines) then
1529                 --adj_row.adjustment_amount := l_dist_cost - l_dist_reserve - nvl(l_dist_imp_rsv,0);
1530                 -- Bug # 6975088 added NVL to l_dist_imp_rsv
1531                 adj_row.adjustment_amount := l_dist_cost - l_dist_reserve - nvl(l_dist_imp_rsv,0);
1532             else
1533                 adj_row.adjustment_amount := h_nbv_retired - total_adj_amount;
1534             end if;
1535 
1536             total_adj_amount := total_adj_amount + adj_row.adjustment_amount;
1537 
1538            /* bug 3519644 */
1539 
1540             if (p_log_level_rec.statement_level) then
1541                fa_debug_pkg.add(fname => l_calling_fn,
1542                                 element => '+++adjustment_amount for NBV retired',
1543                                 value   => adj_row.adjustment_amount, p_log_level_rec => p_log_level_rec);
1544             end if;
1545 
1546              if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1547                                                X_last_update_date,
1548                                                X_last_updated_by,
1549                                                X_last_update_login, p_log_level_rec => p_log_level_rec)) then
1550                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1551                 return(FALSE);
1552 
1553              end if;
1554 
1555             --Bug 6666666 If SORP is enabled create additional accounting entries
1556             --            to clear capital adjustment and general fund
1557             if fa_cache_pkg.fazcbc_record.sorp_enabled_flag = 'Y' then
1558 
1559                 l_prev_leveling_flag := adj_row.leveling_flag;
1560                 adj_row.leveling_flag := FALSE;
1561 
1562                 --******************************************************
1563                 --       Capital Adjustment
1564                 --******************************************************
1565                 adj_row.adjustment_type   := 'CAPITAL ADJ';
1566                 adj_row.account_type      := 'CAPITAL_ADJ_ACCT';
1567                 adj_row.account           := fa_cache_pkg.fazccb_record.capital_adj_acct;
1568                 adj_row.debit_credit_flag := 'DR';
1569 
1570                 if (p_log_level_rec.statement_level) then
1571                    fa_debug_pkg.add(l_calling_fn,'Calling faxinaj for ', 'Retirement
1572                                                             - Capital Adjustment', p_log_level_rec => p_log_level_rec);
1573                 end if;
1574 
1575                 if not FA_INS_ADJUST_PKG.faxinaj (adj_row,
1576                                                   X_last_update_date,
1577                                                   X_last_updated_by,
1578                                                   X_last_update_login, p_log_level_rec => p_log_level_rec) then
1579                     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1580                     return(FALSE);
1581                 end if;
1582 
1583                 --******************************************************
1584                 --       General Fund
1585                 --******************************************************
1586                 adj_row.adjustment_type   := 'GENERAL FUND';
1587                 adj_row.account_type      := 'GENERAL_FUND_ACCT';
1588                 adj_row.account           := fa_cache_pkg.fazccb_record.general_fund_acct;
1589                 adj_row.debit_credit_flag := 'CR';
1590 
1591                 if (p_log_level_rec.statement_level) then
1592                    fa_debug_pkg.add(l_calling_fn,'Calling faxinaj for ', 'Retirement
1593                                                           - General Fund Balance', p_log_level_rec => p_log_level_rec);
1594                 end if;
1595 
1596                 if not FA_INS_ADJUST_PKG.faxinaj (adj_row,
1597                                                   X_last_update_date,
1598                                                   X_last_updated_by,
1599                                                   X_last_update_login, p_log_level_rec => p_log_level_rec) then
1600                     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1601                     return(FALSE);
1602                 end if;
1603 
1604                 adj_row.leveling_flag := l_prev_leveling_flag;
1605 
1606             end if; -- If sorp is enabled
1607 
1608           END LOOP;
1609 
1610           --if (ret.units_retired is null and NOT l_unit_ret_in_corp) then
1611           if (ret.units_retired is null and NOT (l_unit_ret_in_corp and l_balance_tfr_in_tax = 0)) then
1612             CLOSE c_ret_amount;
1613           else
1614             CLOSE c_ret_dists;
1615           end if;
1616 
1617        end if; -- h_nbv_retired <> 0
1618 
1619        -- +++++ Process for Group Asset +++++
1620        adj_row.track_member_flag := null;
1621 
1622        if (bk.group_asset_id is not null) and
1623           (nvl(bk.member_rollup_flag, 'N') = 'N') and
1624           (h_nbv_retired <> 0) then
1625 
1626              l_asset_hdr_rec_g.asset_id := bk.group_asset_id;
1627              l_asset_hdr_rec_g.book_type_code := ret.book;
1628              l_asset_hdr_rec_g.set_of_books_id := ret.set_of_books_id;
1629 
1630              if not FA_UTIL_PVT.get_asset_cat_rec (
1631                    p_asset_hdr_rec         => l_asset_hdr_rec_g,
1632                    px_asset_cat_rec        => l_asset_cat_rec_g,
1633                    p_date_effective        => null, p_log_level_rec => p_log_level_rec) then
1634                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1635                 return(FALSE);
1636              end if;
1637 
1638              if not fa_cache_pkg.fazccb(
1639                    X_book   => ret.book,
1640                    X_cat_id => l_asset_cat_rec_g.category_id, p_log_level_rec => p_log_level_rec) then
1641                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1642                 return(FALSE);
1643              end if;
1644 
1645              OPEN c_get_unit (bk.group_asset_id);
1646              FETCH c_get_unit INTO adj_row.current_units;
1647              CLOSE c_get_unit;
1648 
1649              adj_row.asset_id := bk.group_asset_id;
1650              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
1651              adj_row.selection_retid := 0;
1652              adj_row.units_retired := 0;
1653 
1654              adj_row.adjustment_type := 'NBV RETIRED';
1655              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1656              adj_row.set_of_books_id := ret.set_of_books_id;
1657 
1658              /* commented for bug 3627497 */
1659              /* if ((ret.proceeds_of_sale + l_g_reserve)>
1660                  (ret.cost_retired + ret.cost_of_removal)) then
1661                 adj_row.account := nbv_retired_gain_acct;
1662                 adj_row.account_type := 'NBV_RETIRED_GAIN_ACCT';
1663                 adj_row.debit_credit_flag := 'CR';
1664              else
1665                 adj_row.account := nbv_retired_loss_acct;
1666                 adj_row.account_type := 'NBV_RETIRED_LOSS_ACCT';
1667                 adj_row.debit_credit_flag := 'DR';
1668              end if;
1669 
1670              adj_row.adjustment_amount := abs((ret.cost_retired + ret.cost_of_removal) -
1671                                               (ret.proceeds_of_sale + l_g_reserve));
1672              */
1673 
1674              -- added for bug 3627497
1675              adj_row.adjustment_amount := ret.cost_retired - nvl(l_g_reserve,0);--bug fix 3639923
1676 
1677               if h_dr_cr_flag = 1 then
1678                 adj_row.account := nbv_retired_gain_acct;
1679                 adj_row.account_type := 'NBV_RETIRED_GAIN_ACCT';
1680                 adj_row.debit_credit_flag := 'DR';
1681              else
1682                 adj_row.account := nbv_retired_loss_acct;
1683                 adj_row.account_type := 'NBV_RETIRED_LOSS_ACCT';
1684                 adj_row.debit_credit_flag := 'DR';
1685              end if;
1686 
1687              if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1688                                                X_last_update_date,
1689                                                X_last_updated_by,
1690                                                X_last_update_login,
1691                                                p_log_level_rec => p_log_level_rec)) then
1692                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1693                    return(FALSE);
1694              end if;
1695 
1696              -- +++++ Set asset id back to member asset +++++
1697              adj_row.asset_id := ret.asset_id;
1698        end if; -- (bk.group_asset_id is not null) and
1699 
1700 
1701        /* BUG# 2444408
1702           This error began to occur after the fix for bug 2316862
1703           which was propagated from pro*c code.
1704           The remaining part of this revaluation gain/loss logic
1705           still continue to be called in the same manner
1706           as done before the fix for bug 2316862 */
1707        if (ret.units_retired <= 0 or ret.units_retired is null) then
1708 
1709           adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
1710           adj_row.selection_retid := 0;
1711           adj_row.units_retired := 0;
1712        else
1713           adj_row.selection_mode := FA_STD_TYPES.FA_AJ_RETIRE;
1714           adj_row.selection_retid := ret.retirement_id;
1715           adj_row.units_retired := ret.units_retired;
1716        end if;
1717        /* End of fix for bug 2444408 */
1718        /*Bug#12768930 - allow creation of reval rsv ret entry for CIP assets and non sorp book */
1719        if ((h_retire_reval_flag = 1) and
1720            ((ret.wip_asset is null or ret.wip_asset <= 0) or
1721             (ret.wip_asset > 0 and nvl(fa_cache_pkg.fazcbc_record.sorp_enabled_flag,'N') = 'N'))) then
1722 
1723           if ret.reval_rsv_retired <> 0 then
1724              -- Bug 6666666 : For SORP, the reveal gain/loss should go into
1725              --               the capital adjustment account
1726              if fa_cache_pkg.fazcbc_record.sorp_enabled_flag = 'Y' then
1727                  adj_row.adjustment_type := 'CAPITAL ADJ';
1728                  l_prev_leveling_flag := adj_row.leveling_flag;
1729                  adj_row.leveling_flag := FALSE;
1730              else
1731                  adj_row.adjustment_type := 'REVAL RSV RET';
1732              end if;
1733              adj_row.adjustment_amount := ret.reval_rsv_retired;
1734              -- bug 418884, should always be CR since the 'REVAL RESERVE' is always DR
1735              adj_row.debit_credit_flag := 'CR';
1736 
1737             if h_dr_cr_flag = 1 then
1738                adj_row.account := reval_rsv_retired_gain_acct;
1739                adj_row.account_type := 'REVAL_RSV_RETIRED_GAIN_ACCT';
1740             else
1741                adj_row.account := reval_rsv_retired_loss_acct;
1742                adj_row.account_type := 'REVAL_RSV_RETIRED_LOSS_ACCT';
1743             end if;
1744 
1745             adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1746             adj_row.set_of_books_id := ret.set_of_books_id;
1747 
1748             if p_log_level_rec.statement_level then
1749               fa_debug_pkg.add
1750               (fname   => 'fagurt',
1751                element => '+++ before faxinaj for REVAL_RSV_RETIRED_GAIN_ACCT',
1752                value   => adj_row.selection_mode, p_log_level_rec => p_log_level_rec);
1753             end if;
1754 
1755             if (bk.group_asset_id is not null) and
1756                (nvl(bk.member_rollup_flag, 'N') = 'N') then
1757                adj_row.track_member_flag := 'Y';
1758             else
1759                adj_row.track_member_flag := null;
1760             end if;
1761 
1762 
1763             if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1764                                        X_last_update_date,
1765                                        X_last_updated_by,
1766                                        X_last_update_login,
1767                                        p_log_level_rec => p_log_level_rec)) then
1768                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1769                return(FALSE);
1770 
1771             end if;
1772 
1773             -- Bug 6666666 : Reset the leveling flag to the original
1774             if fa_cache_pkg.fazcbc_record.sorp_enabled_flag = 'Y' then
1775                 adj_row.leveling_flag := l_prev_leveling_flag;
1776             end if;
1777 
1778             if p_log_level_rec.statement_level then
1779               fa_debug_pkg.add
1780               (fname   => 'fagurt',
1781                element => '+++ before faxinaj for REVAL_RSV_RETIRED_GAIN_ACCT',
1782                value   => '', p_log_level_rec => p_log_level_rec);
1783             end if;
1784 
1785           end if;
1786 
1787          end if;
1788 
1789          --
1790          -- If this is a member asset, POS and COR needs to be inserted
1791          -- with group asset id. Also group is not the one retired
1792          -- so set other parameters accordingly.
1793          --
1794          if (bk.group_asset_id is not null) and
1795             (nvl(bk.member_rollup_flag, 'N') = 'N') then
1796             adj_row.track_member_flag := null;
1797             adj_row.asset_id := bk.group_asset_id;
1798             adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
1799             adj_row.selection_retid := 0;
1800             adj_row.units_retired := 0;
1801          end if;
1802 
1803          /* BUG 2669638: if ret.proceeds_of_sale > 0 then */
1804          if ret.proceeds_of_sale <> 0 then
1805 
1806             adj_row.adjustment_type := 'PROCEEDS CLR';
1807             adj_row.adjustment_amount := ret.proceeds_of_sale;
1808             adj_row.account := proc_of_sale_clearing_acct;
1809             adj_row.account_type := 'PROCEEDS_OF_SALE_CLEARING_ACCT';
1810             adj_row.debit_credit_flag := 'DR';
1811             adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1812             adj_row.set_of_books_id := ret.set_of_books_id;
1813 
1814             if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1815                                        X_last_update_date,
1816                                        X_last_updated_by,
1817                                        X_last_update_login,
1818                                        p_log_level_rec => p_log_level_rec)) then
1819                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1820                return(FALSE);
1821             end if;
1822 
1823             -- added for bug 3627497
1824          if (bk.group_asset_id is not null) and
1825             (nvl(bk.member_rollup_flag, 'N') = 'N') then
1826             adj_row.adjustment_type := 'PROCEEDS';
1827             adj_row.adjustment_amount := ret.proceeds_of_sale;
1828             adj_row.debit_credit_flag := 'CR';
1829             adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1830             adj_row.set_of_books_id := ret.set_of_books_id;
1831 
1832             if h_dr_cr_flag = 1 then
1833                adj_row.account := proceeds_of_sale_gain_acct;
1834                adj_row.account_type := 'PROCEEDS_OF_SALE_GAIN_ACCT';
1835             else
1836                adj_row.account := proceeds_of_sale_loss_acct;
1837                adj_row.account_type := 'PROCEEDS_OF_SALE_LOSS_ACCT';
1838             end if;
1839 
1840             if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1841                                        X_last_update_date,
1842                                        X_last_updated_by,
1843                                        X_last_update_login,
1844                                        p_log_level_rec => p_log_level_rec)) then
1845                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1846                return(FALSE);
1847             end if;
1848          end if;
1849        end if;
1850 
1851        /* BUG 2669638: if ret.cost_of_removal > 0 then */
1852        if ret.cost_of_removal <> 0 then
1853 
1854           adj_row.adjustment_type := 'REMOVALCOST CLR';
1855           adj_row.adjustment_amount := ret.cost_of_removal;
1856           adj_row.account := cost_of_removal_clearing_acct;
1857           adj_row.account_type := 'COST_OF_REMOVAL_CLEARING_ACCT';
1858           adj_row.debit_credit_flag := 'CR';
1859           adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1860           adj_row.set_of_books_id := ret.set_of_books_id;
1861 
1862           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1863                                        X_last_update_date,
1864                                        X_last_updated_by,
1865                                        X_last_update_login,
1866                                        p_log_level_rec => p_log_level_rec)) then
1867                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1868                return(FALSE);
1869 
1870           end if;
1871 
1872             -- added for bug 3627497
1873          if (bk.group_asset_id is not null) and
1874             (nvl(bk.member_rollup_flag, 'N') = 'N') then
1875           adj_row.adjustment_type := 'REMOVALCOST';
1876           adj_row.adjustment_amount := ret.cost_of_removal;
1877           adj_row.debit_credit_flag := 'DR';
1878           adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
1879           adj_row.set_of_books_id := ret.set_of_books_id;
1880 
1881           if h_dr_cr_flag = 1 then
1882              adj_row.account := cost_of_removal_gain_acct;
1883              adj_row.account_type := 'COST_OF_REMOVAL_GAIN_ACCT';
1884           else
1885              adj_row.account := cost_of_removal_loss_acct;
1886              adj_row.account_type := 'COST_OF_REMOVAL_LOSS_ACCT';
1887           end if;
1888 
1889           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
1890                                        X_last_update_date,
1891                                        X_last_updated_by,
1892                                        X_last_update_login,
1893                                        p_log_level_rec => p_log_level_rec)) then
1894                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1895                return(FALSE);
1896 
1897           end if;
1898         end if;
1899 
1900        end if;
1901 
1902        /* Calculate ITC recaptured */
1903 
1904        if (bk.itc_used > 0) and
1905           (ret.wip_asset is null or ret.wip_asset <= 0) then
1906 
1907           if not fagitc(ret, bk, cost_frac,p_log_level_rec) then
1908 
1909                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1910                return(FALSE);
1911 
1912           end if;
1913 
1914        end if;
1915 
1916        return(TRUE);
1917 
1918 EXCEPTION
1919    when others then
1920 
1921             if c_ret_amount%ISOPEN then
1922                CLOSE c_ret_amount;
1923             end if;
1924 
1925             if c_ret_dists%ISOPEN then
1926                CLOSE c_ret_dists;
1927             end if;
1928 
1929             if c_ret_rsv_costs%ISOPEN then
1930               CLOSE c_ret_rsv_costs;
1931             end if;
1932 
1933             if c_ret_rsv_costs_mrc%ISOPEN then
1934               CLOSE c_ret_rsv_costs_mrc;
1935             end if;
1936 
1937             fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
1938             return FALSE;
1939 
1940 END FAGURT;
1941 
1942 /*===========================================================================*
1943 | NAME        fagpct                                                         |
1944 |                                                                            |
1945 | FUNCTION    Add a cost retired adjustment into FA_ADJUSTMENTS. Credit to   |
1946 |             the asset account.                                             |
1947 |                                                                            |
1948 | HISTORY     08/30/89    R Rumanang      Created                            |
1949 |             05/03/91    M Chan          Rewrote for MPL 9                  |
1950 |             12/31/96    S Behura        Rewrote in PL/SQL                  |
1951 |                                                                            |
1952 *============================================================================*/
1953 
1954 FUNCTION fagpct(ret in out nocopy fa_ret_types.ret_struct,
1955                 bk in out nocopy fa_ret_types.book_struct,
1956                 cpd_ctr number, today in date,
1957                 user_id number,
1958                 p_log_level_rec in FA_API_TYPES.log_level_rec_type) return boolean IS
1959 
1960     fagpct_err          exception;
1961 
1962     asset_cost_acct     varchar2(26);
1963     cip_cost_acct       varchar2(26);
1964     -- adj_row             FA_STD_TYPES.fa_adj_row_struct;
1965     adj_row     FA_ADJUST_TYPE_PKG.fa_adj_row_struct;
1966 
1967     h_asset_id          number(15);
1968     h_ret_id            number(15);
1969     h_book              varchar2(30);
1970     h_id_out            number;
1971     h_cip_cost_acct     varchar2(26);
1972     h_asset_cost_acct   varchar2(26);
1973     h_cur_units         number;
1974 
1975     h_cost_retired        number;
1976     h_adjustment_amount   number;
1977     h_adjustment_ccid   number;
1978 
1979     l_balance_tfr_in_tax  number;
1980     l_unit_ret_in_corp    boolean;
1981     l_ret_id_in_corp    number;
1982 
1983     X_LAST_UPDATE_DATE date := sysdate;
1984     X_last_updated_by number := -1;
1985     X_last_update_login number := -1;
1986 
1987     l_calling_fn        varchar2(40) := 'FA_GAINLOSS_UPD_PKG.fagpct';
1988 
1989     l_dummy number;
1990 
1991     BEGIN <<FAGPCT>>
1992 
1993        h_cur_units := 0;
1994        h_asset_id := ret.asset_id;
1995        h_ret_id := ret.retirement_id;
1996        h_book := ret.book;
1997 
1998        if p_log_level_rec.statement_level then
1999           fa_debug_pkg.add
2000             (fname   => l_calling_fn,
2001              element => 'acct segment in fagpct',
2002              value   => '', p_log_level_rec => p_log_level_rec);
2003        end if;
2004 
2005        select  asset_cost_acct,
2006                nvl(cip_cost_acct, '0')
2007        into    h_asset_cost_acct,
2008                h_cip_cost_acct
2009        from    fa_additions_b    faadd,
2010                fa_category_books facb
2011        where   faadd.asset_id = h_asset_id
2012        and     facb.category_id = faadd.asset_category_id
2013        and     facb.book_type_code = h_book;
2014 
2015        asset_cost_acct := h_asset_cost_acct;
2016        cip_cost_acct := h_cip_cost_acct;
2017 
2018        adj_row.transaction_header_id := ret.th_id_in;
2019        adj_row.book_type_code := ret.book;
2020        adj_row.period_counter_created := cpd_ctr;
2021        adj_row.asset_id := ret.asset_id;
2022        adj_row.period_counter_adjusted := cpd_ctr;
2023        adj_row.last_update_date := today;
2024        adj_row.current_units := bk.cur_units;
2025        adj_row.gen_ccid_flag := TRUE;
2026        adj_row.flush_adj_flag := TRUE;
2027        adj_row.annualized_adjustment := 0;
2028        adj_row.code_combination_id := 0;
2029        adj_row.distribution_id := 0;
2030        adj_row.selection_retid := 0;
2031        adj_row.units_retired := 0;
2032        adj_row.asset_invoice_id := 0;
2033        adj_row.leveling_flag := FALSE;
2034 
2035        if ret.wip_asset > 0 then
2036           adj_row.source_type_code := 'CIP RETIREMENT';
2037           adj_row.account := cip_cost_acct;
2038           adj_row.account_type := 'CIP_COST_ACCT';
2039        else
2040           adj_row.source_type_code := 'RETIREMENT';
2041           adj_row.account := asset_cost_acct;
2042           adj_row.account_type := 'ASSET_COST_ACCT';
2043        end if;
2044 
2045        adj_row.adjustment_type := 'COST';
2046        adj_row.adjustment_amount := ret.cost_retired;
2047 
2048        if p_log_level_rec.statement_level then
2049           fa_debug_pkg.add
2050             (fname   => l_calling_fn,
2051              element => 'fagpct: ret.units_retired(1)',
2052              value   => ret.units_retired);
2053        end if;
2054 
2055        if (ret.units_retired <= 0 or ret.units_retired is null) then
2056 
2057            adj_row.selection_thid := 0;
2058            adj_row.debit_credit_flag := 'CR';
2059 
2060            if (bk.current_cost = ret.cost_retired) then
2061              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_CLEAR;
2062            else
2063 
2064              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
2065 
2066              if (bk.book_class) then
2067                 if NOT faginfo(
2068                             RET, BK, cpd_ctr,today, user_id
2069                            ,calling_module => l_calling_fn
2070                            ,candidate_mode => 'CLEAR_PARTIAL'
2071                            ,set_adj_row => TRUE
2072                            ,unit_ret_in_corp => l_unit_ret_in_corp
2073                            ,ret_id_in_corp => l_ret_id_in_corp
2074                            ,th_id_out_in_corp => h_id_out
2075                            ,balance_tfr_in_tax => l_balance_tfr_in_tax
2076                            ,adj_row => adj_row
2077                            ,p_log_level_rec => p_log_level_rec) then
2078                   fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2079                   return(FALSE);
2080                 end if;
2081              end if;
2082 
2083            end if;
2084 
2085            if p_log_level_rec.statement_level then
2086              fa_debug_pkg.add(l_calling_fn, '++ bk.book_class (TRUE=TAX)', bk.book_class);
2087              fa_debug_pkg.add(l_calling_fn, '++ ret.th_id_in', ret.th_id_in, p_log_level_rec => p_log_level_rec);
2088              fa_debug_pkg.add(l_calling_fn, '++ h_id_out=th_id_out_in_corp', h_id_out, p_log_level_rec => p_log_level_rec);
2089              fa_debug_pkg.add(l_calling_fn, '++ adj_row.selection_thid', adj_row.selection_thid, p_log_level_rec => p_log_level_rec);
2090            end if;
2091 
2092            adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
2093            adj_row.set_of_books_id := ret.set_of_books_id;
2094 
2095            if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
2096                                        X_last_update_date,
2097                                        X_last_updated_by,
2098                                        X_last_update_login
2099                                        , p_log_level_rec => p_log_level_rec)) then
2100               fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2101               return(FALSE);
2102 
2103           end if;
2104 
2105        else
2106             select      distinct nvl(transaction_header_id_out,0)
2107             into        h_id_out
2108             from        fa_distribution_history
2109             where       asset_id = h_asset_id
2110             and         book_type_code = h_book
2111             and         retirement_id = h_ret_id;
2112 
2113            /* Fix for Bug#4617352: We have decided to create adj lines only for affected rows
2114              to avoid rounding issues with remaining rows in partial unit intercompany retirement.
2115            */
2116            adj_row.selection_thid := h_id_out;
2117            adj_row.debit_credit_flag := 'CR';
2118            adj_row.selection_mode := FA_STD_TYPES.FA_AJ_CLEAR_PARTIAL;
2119            adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
2120            adj_row.set_of_books_id := ret.set_of_books_id;
2121 
2122            if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
2123                                        X_last_update_date,
2124                                        X_last_updated_by,
2125                                        X_last_update_login,
2126                                        p_log_level_rec => p_log_level_rec)) then
2127               fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2128               return(FALSE);
2129 
2130            end if;
2131 
2132            adj_row.adjustment_amount := adj_row.amount_inserted-
2133                                                 ret.cost_retired;
2134 
2135 /*
2136             select      nvl(units,0)
2137             into        h_cur_units
2138             from        fa_asset_history
2139             where       asset_id = h_asset_id
2140             and         date_ineffective is null;
2141 */
2142 
2143             h_cur_units := 0;
2144 
2145             begin
2146               select 1
2147                 into l_dummy
2148               from fa_distribution_history
2149               where asset_id = h_asset_id
2150                 and date_ineffective is null
2151                 and transaction_header_id_in = h_id_out
2152                 and rownum = 1;
2153 
2154 
2155               select sum(nvl(units_assigned,0))
2156                 into h_cur_units
2157               from fa_distribution_history
2158               where asset_id = h_asset_id
2159                 and date_ineffective is null
2160                 and transaction_header_id_in = h_id_out;
2161 
2162             exception
2163               when no_data_found then
2164                 h_cur_units := 0;
2165             end;
2166 
2167             if (h_cur_units <>0) then
2168 
2169               adj_row.current_units := h_cur_units;
2170               --adj_row.selection_thid :=  0;
2171               adj_row.selection_thid := h_id_out;
2172               adj_row.debit_credit_flag := 'DR';
2173               adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE_PARTIAL;
2174               adj_row.leveling_flag := FALSE;
2175               adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
2176               adj_row.set_of_books_id := ret.set_of_books_id;
2177 
2178               if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
2179                                        X_last_update_date,
2180                                        X_last_updated_by,
2181                                        X_last_update_login
2182                                        , p_log_level_rec => p_log_level_rec)) then
2183                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2184                 return(FALSE);
2185 
2186              end if;
2187 
2188            end if; -- if h_cur_units <> 0
2189 
2190 
2191       end if;
2192 
2193       return (TRUE);
2194 
2195 EXCEPTION
2196 
2197        when others then
2198 
2199             fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2200             return FALSE;
2201 
2202 END FAGPCT;
2203 
2204 /*======================================================================*
2205   | Name        farboe                                                  |
2206   |                                                                     |
2207   | Function                                                            |
2208   |     This function is to calculate the back out deprn_expense,       |
2209   |     reval_expense, and reval_amort we have taken so far and         |
2210   |     insert them into fa_adjustments table.                          |
2211   |                                                                     |
2212   | History     11/13/92        L. Sun          Created                 |
2213   |                                                                     |
2214   |             12/31/96        S. Behura       Rewrote into PL/SQL     |
2215   |             11/08/97        S. Behura      Rewrote into PL/SQL(10.7)|
2216   *=====================================================================*/
2217 
2218 FUNCTION farboe(asset_id number, book in varchar2,
2219                 current_fiscal_yr number, cost_frac in number,
2220                 start_pdnum number, end_pdnum number,
2221                 adj_type in varchar2, pds_per_year number,
2222                 dpr_evenly number, fiscal_year_name in varchar2,
2223                 units_retired number, th_id_in number,
2224                 cpd_ctr number, today in date,
2225                 current_units number, retirement_id number, d_cal in varchar2,
2226                 dpr in out nocopy FA_STD_TYPES.dpr_struct, p_cal in varchar2,
2227                 pds_catchup number, depreciate_lastyr boolean,
2228                 start_pp number, end_pp number,
2229                 mrc_sob_type_code in varchar2,
2230                 ret in fa_ret_types.ret_struct,
2231                 bk in out nocopy fa_ret_types.book_struct,
2232                 p_log_level_rec in FA_API_TYPES.log_level_rec_type) Return BOOLEAN IS
2233 
2234     farboe_err          exception;
2235 
2236     i                   integer;
2237     m                   integer;
2238     j                   integer;
2239     dpr_detail_counter  number;
2240     dpr_detail_size     number;
2241     dpr_detail          fa_RET_TYPES.dpr_detail_struct;
2242     in_dpr_detail       number;
2243     target_dpr_detail   number;
2244     expand_array        number;
2245     tot_backup_deprn    number;
2246     backup_deprn        number;
2247     frac_of_fiscal_year number;
2248     -- adj_row             FA_STD_TYPES.fa_adj_row_struct;
2249     adj_row             FA_ADJUST_TYPE_PKG.fa_adj_row_struct;
2250     period_fracs        FA_STD_TYPES.table_fa_cp_struct;
2251 
2252     -- dummy               FA_STD_TYPES.dpr_arr_type;
2253     dummy_num           number;
2254     dummy_num2          number;
2255     dpr_out             FA_STD_TYPES.dpr_out_struct;
2256     deprn_adjustment    number;
2257 
2258     temp_frac           number;
2259     temp_pds            number;
2260     y_begin             integer;
2261     pp_begin            integer;
2262     dpy_begin           integer;
2263     dpp_begin           integer;
2264     --fy_name             varchar2(20); -- bug 2719715
2265     /* the length of fy_name should be equal to
2266        the lenght of FA_FISCAL_YEAR.FISCAL_YEAR_NAME varchar2(30)  */
2267     --fy_name             varchar2(30); -- bug 2719715
2268     fy_name   FA_FISCAL_YEAR.FISCAL_YEAR_NAME%TYPE;
2269 
2270     temp_start_pp       number;
2271     source_type_code    varchar2(30);
2272     prev_dist_id        number;
2273     bonus_deprn_exp_acct varchar2(30);
2274     deprn_exp_acct      varchar2(30);
2275     impair_exp_acct     varchar2(30);
2276 
2277     h_start_pdnum       number;
2278     h_end_pdnum         number;
2279     h_i                 number;
2280     h_cost_frac         number;
2281     h_asset_id          number(15);
2282     h_book              varchar2(30);
2283     h_bonus_deprn_exp_acct varchar2(30);
2284     h_impair_exp_acct   varchar2(30);
2285     h_current_fiscal_yr number;
2286     h_adj_type          varchar2(16);
2287     h_adj_amount        number;
2288     h_dist_id           number(15);
2289     h_ccid              number;
2290     h_deprn_amount      number;
2291     h_annualized_adj    number;
2292     h_tot_deprn         number;
2293     h_units_retired     number(15);
2294     h_retirement_id     number(15);
2295     h_cpd_ctr           number(15);
2296     h_same_fy           integer;
2297     h_rate_source_rule  integer;
2298     h_dwacq             integer;
2299     h_depr_first_year_ret integer;
2300     h_adj_count         number;
2301     h_ret_count         number;
2302     h_curr_pd_add       number;
2303     h_currpd_amount     number;
2304     h_net_deprn_amount  number;
2305     h_source_type_code  varchar2(30);
2306     h_deprn_exp_acct    varchar2(30);
2307     h_prior_pd_tfr      number;
2308     h_curr_pd_reinst    number;
2309     h_old_reinst_trx_id number;
2310     h_old_reinst_pc     number;
2311     h_old_ret_pc        number;
2312     h_ret_prorate_pc    number;
2313     h_no_of_per_to_exclude      number;
2314 
2315     -- Fix for 4259471
2316     k                   integer;
2317     h_start_pd_endpp    number;
2318     h_start_pd_deprn    number;
2319     h_temp_startpd_deprn number;
2320     h_ret_pp            number;
2321     h_ret_pjdate        number;
2322     h_pc                number;
2323     h_start_pd_pc       number;
2324     deprn_amt           number;
2325     bonus_deprn_amt     number;
2326     impairment_amt      number;
2327     reval_deprn_amt     number;
2328     reval_amort         number;
2329     p_pers_per_yr       number(3);
2330     h_dpr_temp          FA_STD_TYPES.dpr_struct;
2331     h_amt_to_retain     number;
2332     h_temp_end_pp       number;
2333 
2334     deprn_start_pnum    number;
2335     deprn_start_fy      number;
2336 
2337 
2338 --bug fix 3558253 and 3518604 start
2339     h_temp_calc number;
2340     h_adj_exp_row       number;
2341     h_prior_fy_exp      number;
2342     h_backout_flag      number;
2343     h_ytd_deprn         number;
2344     h_bonus_ytd_deprn   number;  -- bug 3846296
2345     h_ytd_impairment    number;
2346     h_pd_num            number;
2347     h_temp_deprn_tot    number;
2348     h_fiscal_year       number;
2349 --bug fix 3558253 and 3518604 end
2350 
2351     h_Brow_deprn_reserve number; -- bug 5443855
2352 
2353     l_deprn_exp         number;
2354     l_bonus_deprn_exp   number;
2355     l_impairment_exp    number;
2356     l_asset_fin_rec_new FA_API_TYPES.asset_fin_rec_type;
2357 
2358     h_id_out              number;
2359     l_balance_tfr_in_tax  number;
2360     l_unit_ret_in_corp    boolean;
2361     l_ret_id_in_corp    number;
2362 
2363     h_asset_addition_pc    number := -1;
2364     h_fully_rsv_pd         number := -1;     --Bug#8978794
2365     l_same_fy              number := -1;     --Bug#8496694
2366     l_adj_overlap          number := -1;  -- Bug 14100792
2367     h_fiscal_year_name     varchar2(30);     -- Bug 9311291
2368 
2369     h_transaction_header_id  number;        -- Bug 11782268
2370     h_annual_adj_check       number := 0;   -- Bug 11782268
2371     l_num_d_periods          number;        -- Bug 11782268
2372 
2373     X_LAST_UPDATE_DATE date := sysdate;
2374     X_last_updated_by number := -1;
2375     X_last_update_login number := -1;
2376 
2377 
2378     -- Bug 11782268 Added transaction_header_id also
2379     /* The second half of the union was added in order to obtain
2380      * adjustments amounts for the current open period.  This is
2381      * neccessary since that period would not have deprn_detail
2382      * rows yet, and the original select statement would not have
2383      * returned these adjustment amounts.  --y.i.
2384      */
2385     CURSOR DEPRN_ADJ IS
2386         SELECT  fadd.distribution_id,
2387                 fadh.code_combination_id,
2388                 -1 * h_cost_frac *
2389                     (decode (h_adj_type,
2390                              'EXPENSE', fadd.deprn_amount,
2391                              'BONUS EXPENSE', fadd.bonus_deprn_amount,
2392                              'IMPAIR EXPENSE', fadd.impairment_amount,
2393                              'REVAL EXPENSE', fadd.reval_deprn_expense,
2394                              'REVAL AMORT', fadd.reval_amortization) -
2395                      nvl(sum(decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2396                              faadj.adjustment_amount), 0)),
2397                 nvl(SUM(-1 * h_cost_frac *
2398                         decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2399                         faadj.adjustment_amount), 0),
2400                 nvl(SUM(-1 * h_cost_frac *
2401                         decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2402                         faadj.annualized_adjustment), 0),
2403                 nvl(-1 * h_cost_frac *
2404                    (decode (h_adj_type,
2405                             'EXPENSE',(fadd.deprn_amount -
2406                                         fadd.deprn_adjustment_amount),
2407                             'BONUS EXPENSE',(fadd.bonus_deprn_amount -
2408                                         fadd.bonus_deprn_adjustment_amount),
2409                             'IMPAIR EXPENSE', fadd.impairment_amount,
2410                             'REVAL EXPENSE', fadd.reval_deprn_expense,
2411                             'REVAL AMORT', fadd.reval_amortization)),0),
2412                 nvl(-1 * h_cost_frac *
2413                     (decode (h_adj_type,
2414                              'EXPENSE', fadd.deprn_amount,
2415                              'BONUS EXPENSE', fadd.bonus_deprn_amount,
2416                              'IMPAIR EXPENSE', fadd.impairment_amount,
2417                              'REVAL EXPENSE', fadd.reval_deprn_expense,
2418                              'REVAL AMORT', fadd.reval_amortization)),0),
2419                 nvl(faadj.source_type_code, 'DEPRECIATION'),
2420                 fadp.period_counter,
2421                 faadj.transaction_header_id
2422                 FROM
2423                     fa_distribution_history     fadh,
2424                     fa_deprn_detail             fadd,
2425                     fa_deprn_periods            fadp,
2426                     fa_adjustments              faadj
2427                 WHERE
2428                        fadd.asset_id = h_asset_id
2429                 AND    fadd.distribution_id = fadh.distribution_id
2430                 AND    fadd.book_type_code = h_book
2431                 AND    fadd.deprn_source_code = 'D'
2432                 AND    fadd.period_counter = fadp.period_counter
2433                 AND    fadp.period_num = h_i
2434                 AND    fadp.book_type_code = h_book
2435                 AND    fadp.fiscal_year = h_current_fiscal_yr
2436                 AND    faadj.distribution_id(+) = fadd.distribution_id
2437                 AND    faadj.book_type_code(+) = fadd.book_type_code
2438                 AND    faadj.asset_id(+) = fadd.asset_id
2439                 AND    faadj.period_counter_created(+) = fadd.period_counter
2440                 AND    faadj.adjustment_type(+) = h_adj_type
2441         GROUP BY
2442                     fadd.distribution_id,
2443                     fadh.code_combination_id,
2444                     fadd.deprn_amount,
2445                     fadd.deprn_adjustment_amount,
2446                     fadd.bonus_deprn_amount,
2447                     fadd.bonus_deprn_adjustment_amount,
2448                     fadd.impairment_amount,
2449                     fadd.reval_deprn_expense,
2450                     fadd.reval_amortization,
2451                     faadj.distribution_id,
2452                     faadj.source_type_Code,
2453                     faadj.adjustment_amount,
2454                     fadp.period_counter,
2455                     faadj.transaction_header_id
2456         UNION
2457         SELECT  fadh.distribution_id,
2458                 fadh.code_combination_id,
2459                 0,
2460                 nvl(SUM(-1 * h_cost_frac *
2461                         decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2462                         faadj.adjustment_amount), 0),
2463                 nvl(SUM(-1 * h_cost_frac *
2464                         decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2465                         faadj.annualized_adjustment), 0),
2466                 0,
2467                 0,
2468                 nvl(faadj.source_type_code, 'DEPRECIATION'),
2469                 fadp.period_counter,
2470                 faadj.transaction_header_id
2471                 FROM
2472                     fa_distribution_history     fadh,
2473                     fa_deprn_periods            fadp,
2474                     fa_adjustments              faadj
2475                 WHERE
2476                        fadp.period_num = h_i
2477                 AND    fadp.book_type_code = h_book
2478                 AND    fadp.fiscal_year = h_current_fiscal_yr
2479                 AND    fadp.period_counter = h_cpd_ctr
2480                 AND    faadj.distribution_id = fadh.distribution_id
2481                 AND    faadj.book_type_code = fadp.book_type_code
2482                 AND    faadj.asset_id = h_asset_id
2483                 AND    faadj.period_counter_created = fadp.period_counter
2484                 AND    faadj.adjustment_type = h_adj_type
2485         GROUP BY
2486                     fadh.distribution_id,
2487                     fadh.code_combination_id,
2488                     faadj.distribution_id,
2489                     faadj.source_type_Code,
2490                     faadj.adjustment_amount,
2491                     fadp.period_counter,
2492                     faadj.transaction_header_id;
2493 
2494     -- Bug 11782268 Added transaction_header_id also
2495     CURSOR MRC_DEPRN_ADJ IS
2496         SELECT  fadd.distribution_id,
2497                 fadh.code_combination_id,
2498                 -1 * h_cost_frac *
2499                     (decode (h_adj_type,
2500                              'EXPENSE', fadd.deprn_amount,
2501                              'BONUS EXPENSE', fadd.bonus_deprn_amount,
2502                              'IMPAIR EXPENSE', fadd.impairment_amount,
2503                              'REVAL EXPENSE', fadd.reval_deprn_expense,
2504                              'REVAL AMORT', fadd.reval_amortization) -
2505                      nvl(sum(decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2506                              faadj.adjustment_amount), 0)),
2507                 nvl(SUM(-1 * h_cost_frac *
2508                         decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2509                         faadj.adjustment_amount), 0),
2510                 nvl(SUM(-1 * h_cost_frac *
2511                         decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2512                         faadj.annualized_adjustment), 0),
2513                 nvl(-1 * h_cost_frac *
2514                    (decode (h_adj_type,
2515                             'EXPENSE',(fadd.deprn_amount -
2516                                         fadd.deprn_adjustment_amount),
2517                             'BONUS EXPENSE',(fadd.bonus_deprn_amount -
2518                                         fadd.bonus_deprn_adjustment_amount),
2519                             'IMPAIR EXPENSE', fadd.impairment_amount,
2520                             'REVAL EXPENSE', fadd.reval_deprn_expense,
2521                             'REVAL AMORT', fadd.reval_amortization)),0),
2522                 nvl(-1 * h_cost_frac *
2523                     (decode (h_adj_type,
2524                              'EXPENSE', fadd.deprn_amount,
2525                              'BONUS EXPENSE', fadd.bonus_deprn_amount,
2526                              'IMPAIR EXPENSE', fadd.impairment_amount,
2527                              'REVAL EXPENSE', fadd.reval_deprn_expense,
2528                              'REVAL AMORT', fadd.reval_amortization)),0),
2529                 nvl(faadj.source_type_code, 'DEPRECIATION'),
2530                 fadp.period_counter,
2531                 faadj.transaction_header_id
2532                 FROM
2533                     fa_distribution_history     fadh,
2534                     fa_mc_deprn_detail          fadd,
2535                     fa_deprn_periods            fadp,
2536                     fa_mc_adjustments           faadj
2537                 WHERE
2538                        fadd.asset_id = h_asset_id
2539                 AND    fadd.distribution_id = fadh.distribution_id
2540                 AND    fadd.book_type_code = h_book
2541                 AND    fadd.deprn_source_code = 'D'
2542                 AND    fadd.period_counter = fadp.period_counter
2543                 AND    fadd.set_of_books_id = ret.set_of_books_id
2544                 AND    fadp.period_num = h_i
2545                 AND    fadp.book_type_code = h_book
2546                 AND    fadp.fiscal_year = h_current_fiscal_yr
2547                 AND    faadj.distribution_id(+) = fadd.distribution_id
2548                 AND    faadj.book_type_code(+) = fadd.book_type_code
2549                 AND    faadj.asset_id(+) = fadd.asset_id
2550                 AND    faadj.period_counter_created(+) = fadd.period_counter
2551                 AND    faadj.adjustment_type(+) = h_adj_type
2552                 AND    faadj.set_of_books_id(+) = ret.set_of_books_id
2553         GROUP BY
2554                     fadd.distribution_id,
2555                     fadh.code_combination_id,
2556                     fadd.deprn_amount,
2557                     fadd.deprn_adjustment_amount,
2558                     fadd.bonus_deprn_amount,
2559                     fadd.bonus_deprn_adjustment_amount,
2560                     fadd.impairment_amount,
2561                     fadd.reval_deprn_expense,
2562                     fadd.reval_amortization,
2563                     faadj.distribution_id,
2564                     faadj.source_type_Code,
2565                     faadj.adjustment_amount,
2566                     fadp.period_counter,
2567                     faadj.transaction_header_id
2568         UNION
2569         SELECT  fadh.distribution_id,
2570                 fadh.code_combination_id,
2571                 0,
2572                 nvl(SUM(-1 * h_cost_frac *
2573                         decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2574                         faadj.adjustment_amount), 0),
2575                 nvl(SUM(-1 * h_cost_frac *
2576                         decode (faadj.debit_credit_flag, 'DR', 1, -1) *
2577                         faadj.annualized_adjustment), 0),
2578                 0,
2579                 0,
2580                 nvl(faadj.source_type_code, 'DEPRECIATION'),
2581                 fadp.period_counter,
2582                 faadj.transaction_header_id
2583                 FROM
2584                     fa_distribution_history     fadh,
2585                     fa_deprn_periods            fadp,
2586                     fa_mc_adjustments           faadj
2587                 WHERE
2588                        fadp.period_num = h_i
2589                 AND    fadp.book_type_code = h_book
2590                 AND    fadp.fiscal_year = h_current_fiscal_yr
2591                 AND    fadp.period_counter = h_cpd_ctr
2592                 AND    faadj.distribution_id = fadh.distribution_id
2593                 AND    faadj.book_type_code = fadp.book_type_code
2594                 AND    faadj.asset_id = h_asset_id
2595                 AND    faadj.period_counter_created = fadp.period_counter
2596                 AND    faadj.adjustment_type = h_adj_type
2597                 AND    faadj.set_of_books_id = ret.set_of_books_id
2598         GROUP BY
2599                     fadh.distribution_id,
2600                     fadh.code_combination_id,
2601                     faadj.distribution_id,
2602                     faadj.source_type_Code,
2603                     faadj.adjustment_amount,
2604                     fadp.period_counter,
2605                     faadj.transaction_header_id;
2606 
2607 
2608     l_calling_fn        varchar2(40) := 'FA_GAINLOSS_UPD_PKG.farboe';
2609 
2610      --bug6503327
2611     h_ret_pd_nums               number;
2612     h_tot_days_in_mon           number;
2613     l_same_year                 varchar2(1) :='N' ;
2614     l_same_deprn_start_yr       varchar2(1) :='N' ; -- Bug#9739739
2615     l_tot_days                  number ;
2616     deprn_start_dp_num          number;
2617     h_frac                      number;
2618     h_ytd                       number;
2619     l_poa_ret                   number := 0; --Bug#14111772
2620     h_daily_annual_adj_check    number := 0; --Bug#16008341
2621 
2622     h_new_ret_pn     number;
2623     h_old_ret_pn    number;
2624     l_overlap_ret    boolean := FALSE; /* Bug#15909667 */
2625 
2626     BEGIN <<FARBOE>>
2627 
2628        if p_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in farboe', '', p_log_level_rec => p_log_level_rec); end if;
2629 
2630        if (p_log_level_rec.statement_level) then
2631           fa_debug_pkg.add('farboe','IN FARBOE',1, p_log_level_rec => p_log_level_rec);
2632           fa_debug_pkg.add('farboe','start_pp',start_pp, p_log_level_rec => p_log_level_rec);
2633           fa_debug_pkg.add('farboe','end_pp',end_pp, p_log_level_rec => p_log_level_rec);
2634           fa_debug_pkg.add('farboe','pds_catchup',pds_catchup, p_log_level_rec => p_log_level_rec);
2635        end if;
2636 
2637        tot_backup_deprn := 0;
2638        backup_deprn := 0;
2639        frac_of_fiscal_year := 0;
2640        h_cost_frac := 0;
2641        h_adj_amount := 0;
2642        h_deprn_amount := 0;
2643        h_annualized_adj := 0;
2644        h_tot_deprn := 0;
2645         h_temp_calc := 0;----bug fix 3558253 and 3518604 start
2646         h_adj_exp_row := 0;
2647         h_prior_fy_exp := 0;
2648         h_backout_flag := 1;
2649         h_ytd_deprn := 0;
2650         h_bonus_ytd_deprn := 0;
2651         h_ytd_impairment := 0;
2652         h_pd_num := 1;
2653         h_temp_deprn_tot := 0;--bug fix 3558253 and 3518604 end
2654        h_start_pdnum := start_pdnum;
2655        h_end_pdnum := end_pdnum;
2656        h_cost_frac := cost_frac;
2657        h_asset_id := asset_id;
2658        h_book := book;
2659        h_current_fiscal_yr := current_fiscal_yr;
2660        h_adj_type := adj_type;
2661        h_units_retired := units_retired;
2662        h_retirement_id := retirement_id;
2663        h_cpd_ctr := cpd_ctr;
2664        h_Brow_deprn_reserve := 0; -- bug 5443855
2665        h_fiscal_year_name := fiscal_year_name;  -- Bug 9311291
2666 
2667        if (p_log_level_rec.statement_level) then
2668           fa_debug_pkg.add('farboe','h_start_pdnum',h_start_pdnum, p_log_level_rec => p_log_level_rec);
2669           fa_debug_pkg.add('farboe','h_end_pdnum',h_end_pdnum, p_log_level_rec => p_log_level_rec);
2670        end if;
2671 
2672 
2673        expand_array := 2;
2674 
2675        for m in 1.. FA_RET_TYPES.FA_DPR_DETAIL_SIZE loop
2676 
2677           dpr_detail.dist_id(m) := 0;
2678           dpr_detail.ccid(m) := 0;
2679           dpr_detail.deprn_amount(m) := 0;
2680           dpr_detail.adj_amount(m) := 0;
2681           dpr_detail.annualized_adj(m) := 0;
2682 
2683        end loop;
2684 
2685        dpr_detail_size := FA_RET_TYPES.FA_DPR_DETAIL_SIZE;
2686        dpr_detail_counter := 1;
2687 
2688        -- bug fix 3558253 and 3518604 start
2689 
2690        if mrc_sob_type_code <> 'R' then
2691 
2692                 SELECT ytd_deprn, period_num, bonus_ytd_deprn, fiscal_year,
2693                        ytd_impairment, deprn_reserve
2694                 INTO   h_ytd_deprn, h_pd_num, h_bonus_ytd_deprn, h_fiscal_year,
2695                        h_ytd_impairment, h_Brow_deprn_reserve
2696                 FROM
2697                         fa_deprn_summary ds,
2698                         fa_deprn_periods dp
2699                 WHERE
2700                         ds.asset_id = h_asset_id
2701                 AND     ds.book_type_code = h_book
2702                 AND     ds.deprn_source_code = 'BOOKS'
2703                 AND     dp.book_type_code = h_book
2704                 AND     dp.period_counter = ds.period_counter;
2705 
2706        else
2707 
2708                 SELECT ytd_deprn,period_num, bonus_ytd_deprn, fiscal_year,
2709                        ytd_impairment, deprn_reserve
2710                 INTO   h_ytd_deprn, h_pd_num, h_bonus_ytd_deprn, h_fiscal_year,
2711                        h_ytd_impairment, h_Brow_deprn_reserve
2712                 FROM
2713                         fa_mc_deprn_summary ds,
2714                         fa_deprn_periods dp
2715                 WHERE
2716                         ds.asset_id = h_asset_id
2717                 AND     ds.book_type_code = h_book
2718                 AND     ds.deprn_source_code = 'BOOKS'
2719                 AND     ds.set_of_books_id = ret.set_of_books_id
2720                 AND     dp.book_type_code = h_book
2721                 AND     dp.period_counter = ds.period_counter;
2722 
2723        end if;
2724 
2725        -- bug fix 3558253 and 3518604 end
2726 
2727        for i in reverse h_start_pdnum..h_end_pdnum loop
2728 
2729           h_i := i;
2730           prev_dist_id := 0;
2731 
2732           if (p_log_level_rec.statement_level) then
2733              fa_debug_pkg.add('farboe','backing out pd',h_i, p_log_level_rec => p_log_level_rec);
2734              fa_debug_pkg.add('farboe','h_asset_id',h_asset_id, p_log_level_rec => p_log_level_rec);
2735              fa_debug_pkg.add('farboe','h_book',h_book, p_log_level_rec => p_log_level_rec);
2736              fa_debug_pkg.add('farboe','h_adj_type',h_adj_type, p_log_level_rec => p_log_level_rec);
2737              fa_debug_pkg.add('farboe','h_current_fiscal_yr',h_current_fiscal_yr, p_log_level_rec => p_log_level_rec);
2738              fa_debug_pkg.add('farboe','h_cpd_ctr',h_cpd_ctr, p_log_level_rec => p_log_level_rec);
2739              fa_debug_pkg.add('farboe','h_i',h_i, p_log_level_rec => p_log_level_rec);
2740           end if;
2741 
2742           if mrc_sob_type_code <> 'R' then
2743                open DEPRN_ADJ;
2744           else
2745                open MRC_DEPRN_ADJ;
2746           end if;
2747 
2748 
2749           if (p_log_level_rec.statement_level) then
2750              fa_debug_pkg.add('farboe','after open deprn_adj',1, p_log_level_rec => p_log_level_rec);
2751           end if;
2752 
2753           /* Get the number of adjustments in period being backed out
2754             Fix for 807256 */
2755 
2756           if mrc_sob_type_code <> 'R' then
2757 
2758             SELECT  count(*)
2759             INTO    h_ret_count
2760             FROM
2761                     fa_deprn_periods            fadp,
2762                     fa_adjustments              faadj
2763             WHERE
2764                     fadp.period_num = h_i
2765             AND     fadp.book_type_code = h_book
2766             AND     fadp.fiscal_year = h_current_fiscal_yr
2767             AND     faadj.book_type_code = h_book
2768             AND     faadj.asset_id = h_asset_id
2769             AND     faadj.period_counter_created = fadp.period_counter
2770             AND     faadj.adjustment_type = h_adj_type
2771             AND     faadj.source_type_code = 'RETIREMENT'
2772             AND     faadj.adjustment_amount <> 0;
2773 
2774             SELECT  count(*)
2775             INTO    h_adj_count
2776             FROM
2777                     fa_deprn_periods            fadp,
2778                     fa_adjustments              faadj
2779             WHERE
2780                     fadp.period_num = h_i
2781             AND     fadp.book_type_code = h_book
2782             AND     fadp.fiscal_year = h_current_fiscal_yr
2783             AND     faadj.book_type_code = h_book
2784             AND     faadj.asset_id = h_asset_id
2785             AND     faadj.period_counter_created = fadp.period_counter
2786             AND     faadj.adjustment_type = h_adj_type
2787             AND     faadj.source_type_code <> 'RETIREMENT'
2788             AND     faadj.adjustment_amount <> 0;
2789 
2790           else
2791 
2792             SELECT  count(*)
2793             INTO    h_ret_count
2794             FROM
2795                     fa_deprn_periods            fadp,
2796                     fa_mc_adjustments           faadj
2797             WHERE
2798                     fadp.period_num = h_i
2799             AND     fadp.book_type_code = h_book
2800             AND     fadp.fiscal_year = h_current_fiscal_yr
2801             AND     faadj.book_type_code = h_book
2802             AND     faadj.asset_id = h_asset_id
2803             AND     faadj.period_counter_created = fadp.period_counter
2804             AND     faadj.adjustment_type = h_adj_type
2805             AND     faadj.set_of_books_id = ret.set_of_books_id
2806             AND     faadj.source_type_code = 'RETIREMENT'
2807             AND     faadj.adjustment_amount <> 0;
2808 
2809             SELECT  count(*)
2810             INTO    h_adj_count
2811             FROM
2812                     fa_deprn_periods            fadp,
2813                     fa_mc_adjustments           faadj
2814             WHERE
2815                     fadp.period_num = h_i
2816             AND     fadp.book_type_code = h_book
2817             AND     fadp.fiscal_year = h_current_fiscal_yr
2818             AND     faadj.book_type_code = h_book
2819             AND     faadj.asset_id = h_asset_id
2820             AND     faadj.period_counter_created = fadp.period_counter
2821             AND     faadj.adjustment_type = h_adj_type
2822             AND     faadj.set_of_books_id = ret.set_of_books_id
2823             AND     faadj.source_type_code <> 'RETIREMENT'
2824             AND     faadj.adjustment_amount <> 0;
2825 
2826           end if;
2827 
2828           -- Fix for Bug #3941213.  Check if there are any prior period
2829           -- transfers in this period.
2830           h_prior_pd_tfr := 0;
2831 
2832           SELECT  count(*)
2833           INTO    h_prior_pd_tfr
2834           FROM    fa_deprn_periods dp1,
2835                   fa_deprn_periods dp2,
2836                   fa_transaction_headers th
2837           WHERE   th.asset_id = h_asset_id
2838           AND     th.book_type_code = h_book
2839           AND     th.transaction_type_code = 'TRANSFER'
2840           AND     th.date_effective between dp1.period_open_date
2841                           and nvl(dp1.period_close_date, sysdate)
2842           AND     dp1.book_type_code = th.book_type_code
2843           AND     dp1.period_num = h_i
2844           AND     dp1.fiscal_year = h_current_fiscal_yr
2845           AND     th.transaction_date_entered between
2846                   dp2.calendar_period_open_date and
2847                   dp2.calendar_period_close_date
2848           AND     dp2.book_type_code = th.book_type_code
2849           AND     dp2.period_num < h_i;
2850 
2851           --while deprn_adj%FOUND loop
2852           loop
2853 
2854              if (p_log_level_rec.statement_level) then
2855                 fa_debug_pkg.add('farboe','top of while loop',1, p_log_level_rec => p_log_level_rec);
2856              end if;
2857 
2858              h_adj_amount := 0;
2859              h_deprn_amount := 0;
2860              h_annualized_adj := 0;
2861 
2862              if mrc_sob_type_code <> 'R' then
2863 
2864                 fetch DEPRN_ADJ into
2865                   h_dist_id,
2866                   h_ccid,
2867                   h_deprn_amount,
2868                   h_adj_amount,
2869                   h_annualized_adj,
2870                   h_currpd_amount,
2871                   h_net_deprn_amount,
2872                   h_source_type_code,
2873                   h_pc,
2874                   h_transaction_header_id;
2875 
2876                 --Bug6503327
2877                 -- Get the number of periods per year in the rate calendar
2878                 if not fa_cache_pkg.fazcct(p_cal, p_log_level_rec => p_log_level_rec) then
2879                    fa_srvr_msg.add_message(calling_fn => 'fa_gainloss_upd_pkg.farboe',  p_log_level_rec => p_log_level_rec);
2880                    raise farboe_err;
2881                 end if;
2882 
2883                 p_pers_per_yr := fa_cache_pkg.fazcct_record.number_per_fiscal_year;
2884 
2885                 if p_pers_per_yr = 365  then
2886                     if h_i = h_start_pdnum then
2887 
2888                         -- Bug 9576503 : Use join with fa_fiscal_year to make sure of same year
2889                         select fcp2.period_num + 1  - fcp1.period_num, fcp3.end_date + 1 -  fcp3.start_date
2890                         into   h_ret_pd_nums, h_tot_days_in_mon
2891                         from   fa_calendar_periods fcp1,
2892                                 fa_calendar_periods fcp2,
2893                                 fa_calendar_periods fcp3,
2894                                 fa_fiscal_year fy
2895                         where  fcp1.calendar_type = p_cal
2896                         and    bk.ret_prorate_date between fcp1.start_date and fcp1.end_date
2897                         and    fcp2.calendar_type = fcp1.calendar_type
2898                         and    fcp3.calendar_type = d_cal
2899                         and    fcp3.period_num =  h_i
2900                         and    fcp3.end_date between fcp2.start_date and fcp2.end_date
2901                         and    fy.fiscal_year_name = h_fiscal_year_name
2902                         and    bk.ret_prorate_date between fy.start_date and fy.end_date
2903                         and    fcp3.start_date between fy.start_date and fy.end_date;
2904 
2905                      end if;
2906 
2907                     -- Bug 9311291 : Need to make sure that fiscal year is also same
2908                     -- Bug 9411825 : Removed the join between cp.start_date and bk.ret_prorate_date
2909                     select cp.end_date + 1 -  cp.start_date
2910                     into   h_tot_days_in_mon
2911                     from fa_calendar_periods cp,
2912                          fa_fiscal_year fy
2913                     where  cp.calendar_type = d_cal
2914                     and cp.period_num =  h_i
2915                     and fy.fiscal_year_name = h_fiscal_year_name
2916                     and bk.ret_prorate_date between fy.start_date and fy.end_date
2917                     and cp.start_date between fy.start_date and fy.end_date;
2918 
2919                 end if;
2920 
2921                 if (DEPRN_ADJ%NOTFOUND) then
2922                    --bug fix 3558253 and 3518604 start
2923                    if ((h_ytd_deprn <> 0) and (h_pd_num <> 0) and
2924                        (h_pd_num >= h_i) and
2925                        (h_fiscal_year = h_current_fiscal_yr) and
2926                        (DEPRN_ADJ%ROWCOUNT = 0)) then
2927 
2928                       -- Fix for Bug #4601712: h_start_pdnum should be taken into account for assets added in the middle of the current fy
2929                       -- Fix for Bug #3846296.  Separate out bonus.
2930                       if (adj_type = 'BONUS EXPENSE') then
2931 
2932                          h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
2933                              ((nvl(h_bonus_ytd_deprn,0) / (h_pd_num - h_start_pdnum + 1)) *
2934                                h_cost_frac);
2935                       elsif (adj_type = 'IMPAIR EXPENSE') then
2936 
2937                          h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
2938                              ((nvl(h_ytd_impairment,0) / (h_pd_num - h_start_pdnum + 1)) * h_cost_frac);
2939 
2940                       else
2941 
2942                          -- bug6503327 begins
2943                          -- added the if condition
2944                          -- Added the below logic so that the daily prorate conv can be taken
2945                          -- into account during retirement
2946                          -- Also changed the logic of temp_deprn_tot
2947                          --Bug#9739739:Changed query to use fa_fiscal_year
2948                             begin
2949                                 select 'Y'
2950                                 into  l_same_year
2951                                 from fa_transaction_headers th,
2952                                 fa_fiscal_year fy,
2953 				fa_deprn_periods dp
2954                                 where  th.asset_id =  h_asset_id
2955                                 and th.book_type_code = h_book
2956 				and dp.book_type_code = th.book_type_code
2957                                 and th.transaction_type_code = 'ADDITION'
2958 				and fy.fiscal_year_name = h_fiscal_year_name
2959                                 and th.date_effective between dp.period_open_date and nvl(dp.period_close_date,sysdate)
2960 				and dp.fiscal_year = fy.fiscal_year
2961                                 and th.transaction_date_entered between  fy.start_date and fy.end_date;
2962 
2963                              exception
2964                                 when others then
2965                                         l_same_year := 'N';
2966                            end;
2967                            if not fa_cache_pkg.fazccp(p_cal, fiscal_year_name,
2968                                                         bk.deprn_start_jdate,
2969                                                         deprn_start_pnum, deprn_start_fy, dummy_num, p_log_level_rec => p_log_level_rec) then
2970                                                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2971                                                raise farboe_err;
2972                            end if;
2973                            if not fa_cache_pkg.fazccp(d_cal, fiscal_year_name,
2974                                                         bk.deprn_start_jdate,
2975                                                         deprn_start_dp_num, deprn_start_fy, dummy_num, p_log_level_rec => p_log_level_rec) then
2976                                                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
2977                                                raise farboe_err;
2978                            end if;
2979                            if p_log_level_rec.statement_level then
2980                                 fa_debug_pkg.add('farboe','l_same_year',l_same_year, p_log_level_rec => p_log_level_rec);
2981                                 fa_debug_pkg.add('farboe',' deprn_start_pnum',deprn_start_pnum, p_log_level_rec => p_log_level_rec);
2982                                 fa_debug_pkg.add('farboe',' deprn_start_pnum',deprn_start_dp_num, p_log_level_rec => p_log_level_rec);
2983                            end if;
2984                            if l_same_year = 'Y' then
2985                                 if p_pers_per_yr = 365  then
2986 
2987                                    select to_char(trunc(dp.calendar_period_open_date),'J')-
2988                                                        to_char(trunc(th.transaction_date_entered),'J')
2989                                    into  l_tot_days
2990                                    from fa_transaction_headers th,
2991                                    fa_deprn_periods dp
2992                                    where  th.asset_id =  h_asset_id
2993                                    and    th.book_type_code = h_book
2994                                    and    th.transaction_type_code = 'ADDITION'
2995                                    and    dp.book_type_code = th.book_type_code
2996                                    and    th.date_effective between  dp.period_open_date and nvl(dp.period_close_date,sysdate)
2997                                    and    to_char(th.transaction_date_entered,'RRRR') = dp.fiscal_year;
2998                                         /* Bug16503037: Modified to use correct ytd for allocation set to Evenly */
2999                                         if h_i = h_start_pdnum and dpr_evenly = 0 then
3000                                                 h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3001                                                 ((h_ytd_deprn / l_tot_days) * h_ret_pd_nums * h_cost_frac);
3002                                         else
3003                                                 if dpr_evenly > 0 then
3004                                                         h_frac := (((1/p_pers_per_yr) * (p_pers_per_yr - deprn_start_pnum + 1))
3005                                                                    - ((1/ pds_per_year) * (pds_per_year - deprn_start_dp_num)))
3006                                                                          +((1/ pds_per_year)*(h_pd_num - deprn_start_dp_num));
3007 
3008                                                         h_ytd  := h_ytd_deprn/ h_frac;
3009                                                         if h_i <> deprn_start_dp_num then
3010 
3011                                                                 h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3012                                                                ((h_ytd * (1/pds_per_year)) * h_cost_frac);
3013                                                          else
3014                                                                  h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3015                                                                ((h_ytd * (((1/p_pers_per_yr) * (p_pers_per_yr - deprn_start_pnum + 1))
3016                                                                   - ((1/ pds_per_year) * (pds_per_year - deprn_start_dp_num)))) * h_cost_frac);
3017                                                         end if;
3018                                                 else
3019                                                         h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3020                                                         ((h_ytd_deprn / l_tot_days) * h_tot_days_in_mon * h_cost_frac);
3021                                                 end if;
3022                                         end if;
3023                                 else
3024 				       --Bug#9739739: Checking if addition is back_dated to previous fiscal_years
3025 				       begin
3026                                           select 'Y'
3027                                           into  l_same_deprn_start_yr
3028                                           from fa_transaction_headers th,
3029                                           fa_fiscal_year fy,
3030 					  fa_deprn_periods dp
3031                                           where  th.asset_id =  h_asset_id
3032                                           and th.book_type_code = h_book
3033 					  and dp.book_type_code = th.book_type_code
3034                                           and th.transaction_type_code = 'ADDITION'
3035 					  and fy.fiscal_year_name = h_fiscal_year_name
3036                                           and th.date_effective between dp.period_open_date and nvl(dp.period_close_date,sysdate)
3037 					  and dp.fiscal_year = fy.fiscal_year
3038 					  and bk.deprn_start_date between fy.start_date and fy.end_date;
3039                                        exception
3040                                           when others then
3041                                              l_same_deprn_start_yr := 'N';
3042                                        end;
3043 
3044 				       if l_same_deprn_start_yr = 'Y' then
3045 
3046 			                  h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3047                                           ((h_ytd_deprn / (h_pd_num - deprn_start_pnum + 1)) * h_cost_frac);
3048 			               else
3049                                           h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3050                                                 ((h_ytd_deprn / h_pd_num) * h_cost_frac);
3051                                        end if;
3052 
3053                                 end if;
3054                           else -- if not same year
3055                                  -- Bug#8616644: Prorating ytd_deprn accordingly in ret_prorate pc.
3056                                  if h_i = h_start_pdnum and p_pers_per_yr = 365 then
3057                                           h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3058                                              (((h_ytd_deprn / (h_pd_num * h_tot_days_in_mon))
3059                                              * h_ret_pd_nums) * h_cost_frac);
3060                                  else
3061                                           h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3062                                                 ((h_ytd_deprn / h_pd_num) * h_cost_frac);
3063                                  end if;
3064                          end if; -- if l_same_year = 'Y'
3065                       end if;
3066 
3067                      if p_log_level_rec.statement_level then
3068                        fa_debug_pkg.add
3069                             (fname   => l_calling_fn,
3070                              element => 'h_ytd_deprn (Fix for 3558253 and 3518604)',
3071                              value   => h_ytd_deprn);
3072                        fa_debug_pkg.add
3073                             (fname   => l_calling_fn,
3074                              element => 'h_pd_num',
3075                              value   => h_pd_num, p_log_level_rec => p_log_level_rec);
3076                        fa_debug_pkg.add
3077                             (fname   => l_calling_fn,
3078                              element => 'h_start_pdnum',
3079                              value   => h_start_pdnum, p_log_level_rec => p_log_level_rec);
3080                        fa_debug_pkg.add
3081                             (fname   => l_calling_fn,
3082                              element => 'h_cost_frac',
3083                              value   => h_cost_frac, p_log_level_rec => p_log_level_rec);
3084                        fa_debug_pkg.add
3085                             (fname   => l_calling_fn,
3086                              element => 'h_temp_deprn_tot',
3087                              value   => h_temp_deprn_tot, p_log_level_rec => p_log_level_rec);
3088                      end if;
3089                    end if;
3090                    --bug fix 3558253 and 3518604 end
3091 
3092                    exit;
3093 		--Bug#8682782
3094         --Bug#14111772:Modified condition to apply only for assets added with reserve
3095                 elsif (DEPRN_ADJ%FOUND) and p_pers_per_yr = 365 and h_i = h_start_pdnum and h_Brow_deprn_reserve <> 0 then
3096                     h_deprn_amount := (h_deprn_amount * ( h_ret_pd_nums / h_tot_days_in_mon ));
3097                 end if;
3098 
3099              else
3100 
3101                 fetch MRC_DEPRN_ADJ into
3102                   h_dist_id,
3103                   h_ccid,
3104                   h_deprn_amount,
3105                   h_adj_amount,
3106                   h_annualized_adj,
3107                   h_currpd_amount,
3108                   h_net_deprn_amount,
3109                   h_source_type_code,
3110                   h_pc,
3111                   h_transaction_header_id;
3112 
3113                 -- Bug#11698717: Added fixes done for bugs 6503327,8892905,                             -- 9487137 and 9230184 for mrc.
3114 		if not fa_cache_pkg.fazcct(p_cal) then
3115                    fa_srvr_msg.add_message(calling_fn => 'fa_gainloss_upd_pkg.farboe');
3116                    raise farboe_err;
3117                 end if;
3118 
3119                 p_pers_per_yr := fa_cache_pkg.fazcct_record.number_per_fiscal_year;
3120                 if p_pers_per_yr = 365  then
3121                     if h_i = h_start_pdnum then
3122                         select fcp2.period_num + 1  - fcp1.period_num, fcp3.end_date + 1 -  fcp3.start_date
3123                         into   h_ret_pd_nums, h_tot_days_in_mon
3124                         from   fa_calendar_periods fcp1,
3125                                 fa_calendar_periods fcp2,
3126                                 fa_calendar_periods fcp3,
3127                                 fa_fiscal_year      ffy1,
3128                                 fa_fiscal_year      ffy3
3129                         where  fcp1.calendar_type = p_cal
3130                         and    bk.ret_prorate_date between fcp1.start_date and fcp1.end_date
3131                         and    fcp2.calendar_type = fcp1.calendar_type
3132                         and    fcp3.calendar_type = d_cal
3133                         and    fcp3.period_num =  h_i
3134                         and    fcp3.end_date        between fcp2.start_date  and fcp2.end_date
3135                         and    fcp3.start_date      between ffy3.start_date  and ffy3.end_date
3136                         and    bk.ret_prorate_date  between ffy1.start_date  and ffy1.end_date
3137                         and    ffy1.fiscal_year_name = h_fiscal_year_name
3138                         and    ffy3.fiscal_year_name = h_fiscal_year_name
3139                         and    ffy1.fiscal_year      = ffy3.fiscal_year;
3140 
3141                     end if;
3142 
3143                     select cp.end_date + 1 -  cp.start_date
3144                     into   h_tot_days_in_mon
3145                     from fa_calendar_periods cp,
3146                          fa_fiscal_year fy
3147                     where  cp.calendar_type = d_cal
3148                     and cp.period_num =  h_i
3149                     and fy.fiscal_year_name = h_fiscal_year_name
3150                     and bk.ret_prorate_date between fy.start_date and fy.end_date
3151                     and cp.start_date between fy.start_date and fy.end_date;
3152 
3153                 end if;
3154 
3155                 if (MRC_DEPRN_ADJ%NOTFOUND) then
3156                    --bug fix 3558253 and 3518604 start
3157                    if ((h_ytd_deprn <> 0) and (h_pd_num <> 0) and
3158                        (h_pd_num >= h_i) and
3159                        (h_fiscal_year = h_current_fiscal_yr) and
3160                        (MRC_DEPRN_ADJ%ROWCOUNT = 0)) then
3161 
3162                       -- Fix for Bug #4601712: h_start_pdnum should be taken into account for assets added in the middle of the current fy
3163                       -- Fix for Bug #3846296.  Separate out bonus.
3164                       if (adj_type = 'BONUS EXPENSE') then
3165 
3166                          h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3167                              ((nvl(h_bonus_ytd_deprn,0) / (h_pd_num - h_start_pdnum + 1)) *
3168                                h_cost_frac);
3169                       elsif (adj_type = 'IMPAIR EXPENSE') then
3170 
3171                          h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3172                              ((nvl(h_ytd_impairment,0) / (h_pd_num - h_start_pdnum + 1)) * h_cost_frac);
3173 
3174                       else
3175                          begin
3176                                 select 'Y'
3177                                 into  l_same_year
3178                                 from fa_transaction_headers th,
3179                                      fa_deprn_periods dp,
3180                                      fa_fiscal_year   ffy
3181                                 where  th.asset_id =  h_asset_id
3182                                 and    th.book_type_code = h_book
3183                                 and    th.transaction_type_code = 'ADDITION'
3184                                 and    dp.book_type_code = th.book_type_code
3185                                 and    th.date_effective between  dp.period_open_date and dp.period_close_date
3186                                 and    th.transaction_date_entered between ffy.start_date and ffy.end_date
3187                                 and    ffy.fiscal_year_name = h_fiscal_year_name
3188                                 and    ffy.fiscal_year      = dp.fiscal_year;
3189                           exception
3190                                 when others then
3191                                         l_same_year := 'N';
3192                           end;
3193 
3194 			   if not fa_cache_pkg.fazccp(p_cal, fiscal_year_name,
3195                                                         bk.deprn_start_jdate,
3196                                                         deprn_start_pnum, deprn_start_fy, dummy_num) then
3197                                                fa_srvr_msg.add_message(calling_fn => l_calling_fn);
3198                               raise farboe_err;
3199                            end if;
3200                            if not fa_cache_pkg.fazccp(d_cal, fiscal_year_name,
3201                                                         bk.deprn_start_jdate,
3202                                                         deprn_start_dp_num, deprn_start_fy, dummy_num) then
3203                                                fa_srvr_msg.add_message(calling_fn => l_calling_fn);
3204                               raise farboe_err;
3205                            end if;
3206                            if p_log_level_rec.statement_level then
3207                                 fa_debug_pkg.add('farboe','l_same_year',l_same_year);
3208                                 fa_debug_pkg.add('farboe',' deprn_start_pnum',deprn_start_pnum);
3209                                 fa_debug_pkg.add('farboe',' deprn_start_pnum',deprn_start_dp_num);
3210                            end if;
3211                            if l_same_year = 'Y' then
3212                               if p_pers_per_yr = 365  then
3213 			         select to_char(trunc(dp.calendar_period_open_date),'J')-
3214                                                        to_char(trunc(th.transaction_date_entered),'J')
3215                                  into  l_tot_days
3216                                  from fa_transaction_headers th,
3217                                         fa_deprn_periods dp,
3218                                         fa_fiscal_year   ffy
3219                                  where  th.asset_id =  h_asset_id
3220                                    and    th.book_type_code = h_book
3221                                    and    th.transaction_type_code = 'ADDITION'
3222                                    and    dp.book_type_code = th.book_type_code
3223                                    and    th.date_effective between  dp.period_open_date and nvl(dp.period_close_date,sysdate)
3224                                    and    th.transaction_date_entered between ffy.start_date and ffy.end_date
3225                                    and    ffy.fiscal_year_name = h_fiscal_year_name
3226                                    and    ffy.fiscal_year      = dp.fiscal_year;
3227 
3228                                  if h_i = h_start_pdnum then
3229                                     h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3230                                                 ((h_ytd_deprn / l_tot_days) * h_ret_pd_nums * h_cost_frac);
3231                                  else
3232                                     if dpr_evenly > 0 then
3233                                        h_frac := (((1/p_pers_per_yr) * (p_pers_per_yr - deprn_start_pnum + 1))
3234                                                                    - ((1/ pds_per_year) * (pds_per_year - deprn_start_dp_num)))
3235                                                                          +((1/ pds_per_year)*(h_pd_num - deprn_start_dp_num));
3236 
3237                                        h_ytd  := h_ytd_deprn/ h_frac;
3238                                        if h_i <> deprn_start_dp_num then
3239 
3240                                            h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3241                                                                ((h_ytd * (1/pds_per_year)) * h_cost_frac);
3242                                         else
3243                                            h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3244                                                                ((h_ytd * (((1/p_pers_per_yr) * (p_pers_per_yr - deprn_start_pnum + 1))
3245                                                                   - ((1/ pds_per_year) * (pds_per_year - deprn_start_dp_num)))) * h_cost_frac);
3246                                         end if;
3247                                      else
3248                                         h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3249                                                         ((h_ytd_deprn / l_tot_days) * h_tot_days_in_mon * h_cost_frac);
3250                                     end if;
3251                                  end if;
3252                               else
3253                                  h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3254                                         ((h_ytd_deprn / (h_pd_num - deprn_start_pnum + 1)) * h_cost_frac);
3255                               end if;
3256                            else -- if not same year
3257                              if h_i = h_start_pdnum and p_pers_per_yr = 365 then
3258                                  h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3259                                              (((h_ytd_deprn / (h_pd_num * h_tot_days_in_mon))
3260                                              * h_ret_pd_nums) * h_cost_frac);
3261                               else
3262                                  h_temp_deprn_tot := nvl(h_temp_deprn_tot,0) +
3263                                                 ((h_ytd_deprn / h_pd_num) * h_cost_frac);
3264                               end if;
3265                            end if; -- if l_same_year = 'Y'
3266                       end if;
3267 
3268                      if p_log_level_rec.statement_level then
3269                        fa_debug_pkg.add
3270                        (fname   => l_calling_fn,
3271                         element => 'h_ytd_deprn (Fix for 3558253 and 3518604)',
3272                         value   => h_ytd_deprn);
3273                        fa_debug_pkg.add
3274                        (fname   => l_calling_fn,
3275                         element => 'h_pd_num',
3276                         value   => h_pd_num);
3277                        fa_debug_pkg.add
3278                        (fname   => l_calling_fn,
3279                         element => 'h_start_pdnum',
3280                         value   => h_start_pdnum);
3281                        fa_debug_pkg.add
3282                        (fname   => l_calling_fn,
3283                         element => 'h_cost_frac',
3284                         value   => h_cost_frac);
3285                        fa_debug_pkg.add
3286                        (fname   => l_calling_fn,
3287                         element => 'h_temp_deprn_tot',
3288                         value   => h_temp_deprn_tot);
3289                       end if;
3290                    end if;
3291                    --bug fix 3558253 and 3518604 end
3292 
3293                    exit;
3294                 elsif (MRC_DEPRN_ADJ%FOUND) and p_pers_per_yr = 365 and h_i = h_start_pdnum then
3295                    h_deprn_amount := (h_deprn_amount * ( h_ret_pd_nums / h_tot_days_in_mon ));
3296                 end if;
3297              end if;
3298 
3299              /* Bug15909667:overlapping retirement */
3300              if h_adj_amount <> 0 and h_i = h_end_pdnum and
3301              h_ret_count <> 0 and h_adj_count = 0 and
3302              p_pers_per_yr <> 365 then
3303 
3304                 select ( cp1.period_num) ,( cp2.period_num)
3305                 into h_new_ret_pn,h_old_ret_pn
3306                 from fa_retirements new_ret
3307                 ,fa_conventions conv
3308                 ,fa_calendar_periods cp1
3309                 ,fa_retirements old_ret
3310                 ,fa_conventions conv2
3311                 ,fa_calendar_periods cp2
3312                 where new_ret.transaction_header_id_in=ret.th_id_in
3313                 and conv.prorate_convention_code=new_ret.retirement_prorate_convention
3314                 and new_ret.date_retired between conv.start_date and conv.end_date
3315                 and cp1.calendar_type = d_cal
3316                 and conv.prorate_date between cp1.start_Date and cp1.end_date
3317                 and old_ret.transaction_header_id_in=    h_transaction_header_id
3318                 and conv2.prorate_convention_code=old_ret.retirement_prorate_convention
3319                 and old_ret.date_retired between conv2.start_date and conv2.end_date
3320                 and cp2.calendar_type = d_cal
3321                 and conv2.prorate_date between cp2.start_Date and cp2.end_date;
3322 
3323                 if h_i > h_old_ret_pn and h_i > h_new_ret_pn  then
3324                     l_overlap_ret := TRUE;
3325                     h_adj_amount := h_adj_amount * ( h_i - h_new_ret_pn ) / ( h_i -  h_old_ret_pn) ;
3326                 end if;
3327 
3328              end if;
3329 
3330              -- Bug 11782268 : Recalculate annualized_Adj for expensed transactions
3331              -- Exclude unplanned.
3332              -- Behavior of amortized and daily prorate are not tested
3333              -- Bug 16008341 : Restricting this fix only for non-daily prorate assets.
3334              if h_adj_amount <> 0 and p_pers_per_yr <> 365 then
3335 
3336                 h_annual_adj_check := 0;
3337                 select count(*)
3338                 into   h_annual_adj_check
3339                 from   fa_transaction_headers th
3340                 where  th.transaction_header_id = h_transaction_header_id
3341                 and    th.transaction_key is null
3342                 and    nvl(th.transaction_subtype,'XX') = 'EXPENSED';
3343 
3344                 if h_annual_adj_check <> 0 then
3345 
3346                    if p_log_level_rec.statement_level then
3347                       fa_debug_pkg.add('farboe','Annualized Adj Logic','Starts', p_log_level_rec => p_log_level_rec);
3348                       fa_debug_pkg.add('farboe','d_cal',d_cal, p_log_level_rec => p_log_level_rec);
3349                       fa_debug_pkg.add('farboe','bk.prorate_date',bk.prorate_date, p_log_level_rec => p_log_level_rec);
3350                       fa_debug_pkg.add('farboe','h_fiscal_year_name',h_fiscal_year_name, p_log_level_rec => p_log_level_rec);
3351                       fa_debug_pkg.add('farboe','pds_per_year',pds_per_year, p_log_level_rec => p_log_level_rec);
3352                    end if;
3353 
3354                    BEGIN
3355 
3356                       select h_pc - (fy1.fiscal_year * pds_per_year + cp1.period_num)
3357                       into   l_num_d_periods
3358                       from   fa_calendar_periods cp1
3359                            , fa_fiscal_year fy1
3360                       where  cp1.calendar_type = d_cal
3361                       and    bk.prorate_date between cp1.start_Date and cp1.end_date
3362                       and    fy1.fiscal_year_name = h_fiscal_year_name
3363                       and    bk.prorate_date between fy1.start_Date and fy1.end_date;
3364 
3365                    EXCEPTION
3366                       WHEN OTHERS THEN
3367                          fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3368                          raise farboe_err;
3369                    END;
3370 
3371 		   /*Bug#13625460: To avoid exception when retirement and addition prorate period are same */
3372                    if  l_num_d_periods <> 0 then
3373 		      h_annualized_adj :=  pds_per_year * (h_adj_amount / l_num_d_periods);
3374 
3375                       if not FA_UTILS_PKG.faxrnd(h_annualized_adj, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
3376                          fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3377                          raise farboe_err;
3378                       end if;
3379 		   end if;
3380 
3381                    if p_log_level_rec.statement_level then
3382                       fa_debug_pkg.add('farboe','l_num_d_periods',l_num_d_periods, p_log_level_rec => p_log_level_rec);
3383                       fa_debug_pkg.add('farboe','h_annualized_adj',h_annualized_adj, p_log_level_rec => p_log_level_rec);
3384                    end if;
3385                 end if;
3386              end if;
3387 
3388 
3389              if p_log_level_rec.statement_level then
3390                 fa_debug_pkg.add
3391                   (fname   => l_calling_fn,
3392                    element => 'deprn and adj',
3393                    value   => '', p_log_level_rec => p_log_level_rec);
3394                 fa_debug_pkg.add('farboe','deprn_adjFOUND',1, p_log_level_rec => p_log_level_rec);
3395                 fa_debug_pkg.add('farboe','h_deprn_amount',h_deprn_amount, p_log_level_rec => p_log_level_rec);
3396                 fa_debug_pkg.add('farboe','h_adj_amount',h_adj_amount, p_log_level_rec => p_log_level_rec);
3397                 fa_debug_pkg.add('farboe','h_currpd_amount',h_currpd_amount, p_log_level_rec => p_log_level_rec);
3398                 fa_debug_pkg.add('farboe','h_net_deprn_amount',h_net_deprn_amount, p_log_level_rec => p_log_level_rec);
3399                 fa_debug_pkg.add('farboe','h_transaction_header_id',h_transaction_header_id, p_log_level_rec => p_log_level_rec);
3400                 fa_debug_pkg.add('farboe','h_annual_adj_check',h_annual_adj_check, p_log_level_rec => p_log_level_rec);
3401              end if;
3402 
3403              source_type_code := h_source_type_code;
3404 
3405              in_dpr_detail := 0;
3406 
3407              for m in 1..dpr_detail_counter loop
3408 
3409                 if (dpr_detail.dist_id(m) = h_dist_id) then
3410 
3411                    in_dpr_detail := 1;
3412                    target_dpr_detail := m;
3413                    exit;
3414 
3415                 end if;
3416 
3417              end loop;
3418 
3419              if in_dpr_detail > 0 then
3420 
3421 /*  Commenting out. Do not add the deprn_amount if there is an adjustment
3422     at this stage. This does not work if there is more than 1 adjustment
3423     in the period being backed out. Fix for 807256 -  SNARAYAN
3424                 dpr_detail.deprn_amount(target_dpr_detail) :=
3425                    dpr_detail.deprn_amount(target_dpr_detail) +
3426                    h_deprn_amount;
3427 */
3428 
3429                 null;
3430 
3431              else
3432                 if dpr_detail_counter = dpr_detail_size then
3433                    for m in (dpr_detail_size + 1) ..
3434                      (FA_RET_TYPES.FA_DPR_DETAIL_SIZE * expand_array) loop
3435 
3436                       dpr_detail.dist_id(m) := 0;
3437                       dpr_detail.ccid(m) := 0;
3438                       dpr_detail.deprn_amount(m) := 0;
3439                       dpr_detail.adj_amount(m) := 0;
3440                       dpr_detail.annualized_adj(m) := 0;
3441 
3442                    end loop; -- end of loop - for m
3443 
3444                    dpr_detail_size :=
3445                         fa_RET_TYPES.FA_DPR_DETAIL_SIZE * expand_array;
3446                    expand_array := expand_array + 1;
3447                 end if;
3448 
3449                 dpr_detail.dist_id(dpr_detail_counter) := h_dist_id;
3450                 dpr_detail.ccid(dpr_detail_counter) := h_ccid;
3451 
3452 /*  Commenting out. Do not copy the deprn_amount if there is an adjustment
3453     at this stage. This does not work if there is more than 1 adjustment
3454     in the period being backed out. Fix for 807256 -  SNARAYAN
3455 
3456                 dpr_detail.deprn_amount(dpr_detail_counter) :=
3457                                                         h_deprn_amount;
3458 */
3459                 dpr_detail.adj_amount(dpr_detail_counter) :=
3460                                                         h_adj_amount;
3461                 dpr_detail.annualized_adj(dpr_detail_counter) :=
3462                                                         h_annualized_adj;
3463 
3464                 target_dpr_detail := dpr_detail_counter;
3465                 dpr_detail_counter := dpr_detail_counter + 1;
3466 
3467              end if;
3468 
3469              if (p_log_level_rec.statement_level) then
3470                  fa_debug_pkg.add('farboe','target_dpr_detail',
3471                                    target_dpr_detail, p_log_level_rec => p_log_level_rec);
3472                  fa_debug_pkg.add('farboe','dpr_detail_counter',
3473                                    dpr_detail_counter, p_log_level_rec => p_log_level_rec);
3474              end if;
3475 
3476              tot_backup_deprn := 0;
3477 
3478         /* check to see if any of the periods being backed out
3479            is period of addition */
3480 
3481                 SELECT  count(*)
3482                 INTO    h_curr_pd_add
3483                 FROM
3484                         fa_deprn_periods dp,
3485                         fa_transaction_headers th
3486                 WHERE   th.asset_id = h_asset_id
3487                 AND     th.book_type_code = h_book
3488                 AND     th.transaction_type_code || '' = 'ADDITION'
3489                 AND     th.date_effective between dp.period_open_date
3490                                 and nvl(dp.period_close_date, sysdate)
3491                 AND     dp.book_type_code = th.book_type_code
3492                 AND     dp.period_num = h_i
3493                 AND     dp.fiscal_year = h_current_fiscal_yr;
3494 
3495 
3496         /* check to see if any of the periods being backed out
3497            is period of reinstatement */
3498 
3499             begin
3500 
3501                 SELECT  count(*)
3502                 INTO    h_curr_pd_reinst
3503                 FROM
3504                         fa_deprn_periods dp,
3505                         fa_transaction_headers th
3506                 WHERE   th.asset_id = h_asset_id
3507                 AND     th.book_type_code = h_book
3508                 AND     th.transaction_type_code || '' = 'REINSTATEMENT'
3509                 AND     th.date_effective between dp.period_open_date
3510                                 and nvl(dp.period_close_date, sysdate)
3511                 AND     dp.book_type_code = th.book_type_code
3512                 AND     dp.period_num = h_i
3513                 AND     dp.fiscal_year = h_current_fiscal_yr;
3514             exception
3515                when others then h_curr_pd_reinst := 0;
3516             end;
3517 
3518 
3519             if (p_log_level_rec.statement_level) then
3520                  fa_debug_pkg.add('farboe','h_curr_pd_add',
3521                                    h_curr_pd_add, p_log_level_rec => p_log_level_rec);
3522             end if;
3523 
3524 --bug fix 3558253 and 3518604 start
3525 
3526             h_backout_flag := 1;
3527 
3528             if(nvl(h_curr_pd_add,0) > 0 )then
3529 
3530               if (p_log_level_rec.statement_level) then
3531                  fa_debug_pkg.add('farboe','++ h_adj_exp_row 1',
3532                                   h_adj_exp_row, p_log_level_rec => p_log_level_rec);
3533               end if;
3534 
3535               BEGIN
3536 
3537                      if mrc_sob_type_code <> 'R' then
3538 
3539                         SELECT  sum(nvl(adjustment_amount,0))--bug fix 3905436
3540                         INTO    h_adj_exp_row
3541                         FROM
3542                                 fa_adjustments adj,
3543                                 fa_deprn_periods dp,
3544                                 fa_transaction_headers th
3545                         WHERE
3546                                 th.asset_id = h_asset_id
3547                         AND     th.book_type_code = h_book
3548                         AND     th.transaction_type_code || '' = 'ADDITION'
3549                         AND     adj.source_type_code || '' = 'DEPRECIATION'
3550                         AND     adj.ADJUSTMENT_type || '' = 'EXPENSE'
3551                         AND     adj.asset_id = h_asset_id
3552                         AND     adj.book_type_code = h_book
3553                         AND     adj.distribution_id = h_dist_id --bug fix 3905436
3554                         AND     adj.period_counter_created = dp.period_counter
3555                         AND     th.date_effective between dp.period_open_date
3556                                         and nvl(dp.period_close_date, sysdate)
3557                         AND     dp.book_type_code = th.book_type_code
3558                         AND     dp.period_num = h_i
3559                         AND     dp.fiscal_year = h_current_fiscal_yr;
3560 
3561                      else
3562 
3563                         SELECT  sum(nvl(adjustment_amount,0))--bug fix 3905436
3564                         INTO    h_adj_exp_row
3565                         FROM
3566                                 fa_mc_adjustments adj,
3567                                 fa_deprn_periods dp,
3568                                 fa_transaction_headers th
3569                         WHERE
3570                                 th.asset_id = h_asset_id
3571                         AND     th.book_type_code = h_book
3572                         AND     th.transaction_type_code || '' = 'ADDITION'
3573                         AND     adj.source_type_code || '' = 'DEPRECIATION'
3574                         AND     adj.ADJUSTMENT_type || '' = 'EXPENSE'
3575                         AND     adj.asset_id = h_asset_id
3576                         AND     adj.book_type_code = h_book
3577                         AND     adj.set_of_books_id = ret.set_of_books_id
3578                         AND     adj.distribution_id = h_dist_id --bug fix 3905436
3579                         AND     adj.period_counter_created = dp.period_counter
3580                         AND     th.date_effective between dp.period_open_date
3581                                         and nvl(dp.period_close_date, sysdate)
3582                         AND     dp.book_type_code = th.book_type_code
3583                         AND     dp.period_num = h_i
3584                         AND     dp.fiscal_year = h_current_fiscal_yr;
3585 
3586                      end if;
3587 
3588               EXCEPTION
3589                    when no_data_found then
3590                           h_adj_exp_row := -1;
3591               END;
3592 
3593               if (p_log_level_rec.statement_level) then
3594                    fa_debug_pkg.add('farboe','++ h_adj_exp_row 2',
3595                                     h_adj_exp_row, p_log_level_rec => p_log_level_rec);
3596               end if;
3597 
3598               if(nvl(h_adj_exp_row,0) > 0)then
3599 
3600                 if mrc_sob_type_code <> 'R' then
3601 
3602                   BEGIN
3603 
3604                      SELECT  PRIOR_FY_EXPENSE
3605                      INTO    h_prior_fy_exp
3606                      FROM
3607                         fa_deprn_summary ds,
3608                         fa_deprn_periods dp,
3609                         fa_transaction_headers th
3610                      WHERE
3611                              th.asset_id = h_asset_id
3612                      AND     th.book_type_code = h_book
3613                      AND     th.transaction_type_code || '' = 'ADDITION'
3614                      AND     th.date_effective between dp.period_open_date
3615                                   and nvl(dp.period_close_date, sysdate)
3616                      AND     ds.asset_id = h_asset_id
3617                      AND     ds.book_type_code = h_book
3618                      AND     ds.period_counter = dp.period_counter
3619                      AND     dp.book_type_code = th.book_type_code
3620                      AND     dp.period_num = h_i
3621                      AND     dp.fiscal_year = h_current_fiscal_yr;
3622 
3623                    EXCEPTION
3624                          when no_data_found then
3625                               null;
3626                    END;
3627 
3628                 else
3629 
3630                    BEGIN
3631 
3632                      SELECT  PRIOR_FY_EXPENSE
3633                      INTO    h_prior_fy_exp
3634                      FROM
3635                         fa_mc_deprn_summary ds,
3636                         fa_deprn_periods dp,
3637                         fa_transaction_headers th
3638                      WHERE
3639                              th.asset_id = h_asset_id
3640                      AND     th.book_type_code = h_book
3641                      AND     th.transaction_type_code || '' = 'ADDITION'
3642                      AND     th.date_effective between dp.period_open_date
3643                                   and nvl(dp.period_close_date, sysdate)
3644                      AND     ds.asset_id = h_asset_id
3645                      AND     ds.book_type_code = h_book
3646                      AND     ds.period_counter = dp.period_counter
3647                      AND     ds.set_of_books_id = ret.set_of_books_id
3648                      AND     dp.book_type_code = th.book_type_code
3649                      AND     dp.period_num = h_i
3650                      AND     dp.fiscal_year = h_current_fiscal_yr;
3651 
3652                    EXCEPTION
3653                          when no_data_found then
3654                               null;
3655                    END;
3656 
3657                 end if;
3658 
3659 
3660                 if(h_adj_exp_row = nvl(h_prior_fy_exp,0))then
3661                       h_backout_flag := 0;
3662                       tot_backup_deprn := 0;
3663                 else
3664                       h_backout_flag := 1;
3665                 end if;
3666 
3667               end if;
3668 
3669               if (p_log_level_rec.statement_level) then
3670                   fa_debug_pkg.add('farboe','h_prior_fy_exp',
3671                                    h_prior_fy_exp, p_log_level_rec => p_log_level_rec);
3672               end if;
3673 
3674             elsif (nvl(h_curr_pd_reinst,0) > 0 )then
3675 
3676               if (p_log_level_rec.statement_level) then
3677                    fa_debug_pkg.add('farboe','START - RET AFTER REINST',
3678                                     ' ', p_log_level_rec => p_log_level_rec);
3679                    fa_debug_pkg.add('farboe','bk.ret_prorate_date',
3680                                     bk.ret_prorate_date, p_log_level_rec => p_log_level_rec);
3681                    fa_debug_pkg.add('farboe','ret.th_id_in',
3682                                     ret.th_id_in, p_log_level_rec => p_log_level_rec);
3683               end if;
3684 
3685               begin
3686 
3687                   if mrc_sob_type_code <> 'R' then
3688 
3689                         SELECT  sum(adjustment_amount), max(th.transaction_header_id)
3690                         INTO    h_adj_exp_row, h_old_reinst_trx_id
3691                         FROM
3692                                 fa_adjustments adj,
3693                                 fa_deprn_periods dp,
3694                                 fa_transaction_headers th
3695                         WHERE
3696                                 th.asset_id = h_asset_id
3697                         AND     th.book_type_code = h_book
3698                         AND     th.transaction_type_code || '' = 'REINSTATEMENT'
3699                         AND     adj.source_type_code || '' = 'RETIREMENT'
3700                         AND     adj.ADJUSTMENT_type || '' = 'EXPENSE'
3701                         AND     adj.asset_id = h_asset_id
3702                         AND     adj.book_type_code = h_book
3703                         AND     adj.distribution_id = h_dist_id --bug fix 3905436
3704                         AND     adj.period_counter_created = dp.period_counter
3705                         AND     th.date_effective between dp.period_open_date
3706                                         and nvl(dp.period_close_date, sysdate)
3707                         AND     dp.book_type_code = th.book_type_code
3708                         AND     dp.period_num = h_i
3709                         AND     dp.fiscal_year = h_current_fiscal_yr;
3710 
3711                   else
3712 
3713                         SELECT  sum(adjustment_amount), max(th.transaction_header_id)
3714                         INTO    h_adj_exp_row, h_old_reinst_trx_id
3715                         FROM
3716                                 fa_mc_adjustments adj,
3717                                 fa_deprn_periods dp,
3718                                 fa_transaction_headers th
3719                         WHERE
3720                                 th.asset_id = h_asset_id
3721                         AND     th.book_type_code = h_book
3722                         AND     th.transaction_type_code || '' = 'REINSTATEMENT'
3723                         AND     adj.source_type_code || '' = 'RETIREMENT'
3724                         AND     adj.ADJUSTMENT_type || '' = 'EXPENSE'
3725                         AND     adj.asset_id = h_asset_id
3726                         AND     adj.book_type_code = h_book
3727                         AND     adj.distribution_id = h_dist_id --bug fix 3905436
3728                         AND     adj.period_counter_created = dp.period_counter
3729                         AND     adj.set_of_books_id = ret.set_of_books_id
3730                         AND     th.date_effective between dp.period_open_date
3731                                         and nvl(dp.period_close_date, sysdate)
3732                         AND     dp.book_type_code = th.book_type_code
3733                         AND     dp.period_num = h_i
3734                         AND     dp.fiscal_year = h_current_fiscal_yr;
3735 
3736                   end if;
3737 
3738               exception
3739                          when no_data_found then
3740                             h_adj_exp_row := -1;
3741               end;
3742 
3743               begin
3744 
3745                 if mrc_sob_type_code <> 'R' then
3746 		  /* Bug13011372: Prorate convention for previous retirement should be considered */
3747                   select dp1.period_counter
3748                         ,dp2.period_counter
3749                   into   h_old_reinst_pc
3750                         ,h_old_ret_pc
3751                   from   fa_transaction_headers trx,
3752                          fa_deprn_periods dp1,
3753                          fa_retirements old_ret,
3754                          fa_deprn_periods dp2,
3755 			 fa_conventions conv
3756                   where  trx.transaction_header_id = h_old_reinst_trx_id
3757                     and  dp1.book_type_code = trx.book_type_code
3758                     and  trx.transaction_date_entered between dp1.CALENDAR_PERIOD_OPEN_DATE
3759                                                           and dp1.CALENDAR_PERIOD_CLOSE_DATE
3760                     and  old_ret.transaction_header_id_out = trx.transaction_header_id
3761                     and  dp2.book_type_code = trx.book_type_code
3762                     and  conv.prorate_convention_code=old_ret.retirement_prorate_convention
3763                     and  old_ret.date_retired between conv.start_date and conv.end_date
3764 		    and conv.prorate_date between dp2.CALENDAR_PERIOD_OPEN_DATE
3765                                                   and dp2.CALENDAR_PERIOD_CLOSE_DATE;
3766 
3767                   select dp.period_counter
3768                   into h_ret_prorate_pc
3769                   from fa_retirements new_ret
3770                       ,fa_conventions conv
3771                       ,fa_deprn_periods dp
3772                   where new_ret.transaction_header_id_in=ret.th_id_in
3773                     and conv.prorate_convention_code=new_ret.retirement_prorate_convention
3774                     and new_ret.date_retired between conv.start_date and conv.end_date
3775                     and dp.book_type_code = new_ret.book_type_code
3776                     and conv.prorate_date between dp.CALENDAR_PERIOD_OPEN_DATE
3777                                               and dp.CALENDAR_PERIOD_CLOSE_DATE
3778                   ;
3779 
3780                 else
3781 		  /* Bug13011372: Prorate convention for previous retirement should be considered */
3782                   select dp1.period_counter
3783                         ,dp2.period_counter
3784                   into   h_old_reinst_pc
3785                         ,h_old_ret_pc
3786                   from   fa_transaction_headers trx,
3787                          fa_deprn_periods dp1,
3788                          fa_mc_retirements old_ret,
3789                          fa_deprn_periods dp2,
3790 			 fa_conventions conv
3791                   where  trx.transaction_header_id = h_old_reinst_trx_id
3792                     and  dp1.book_type_code = trx.book_type_code
3793                     and  trx.transaction_date_entered between dp1.CALENDAR_PERIOD_OPEN_DATE
3794                                                           and dp1.CALENDAR_PERIOD_CLOSE_DATE
3795                     and  old_ret.transaction_header_id_out = trx.transaction_header_id
3796                     and  old_ret.set_of_books_id = ret.set_of_books_id
3797                     and  dp2.book_type_code = trx.book_type_code
3798                     and  conv.prorate_convention_code=old_ret.retirement_prorate_convention
3799                     and  old_ret.date_retired between conv.start_date and conv.end_date
3800 		    and conv.prorate_date between dp2.CALENDAR_PERIOD_OPEN_DATE
3801                                                   and dp2.CALENDAR_PERIOD_CLOSE_DATE;
3802 
3803                   select dp.period_counter
3804                   into h_ret_prorate_pc
3805                   from fa_mc_retirements new_ret
3806                       ,fa_conventions conv
3807                       ,fa_deprn_periods dp
3808                   where new_ret.transaction_header_id_in=ret.th_id_in
3809                     and conv.prorate_convention_code=new_ret.retirement_prorate_convention
3810                     and new_ret.date_retired between conv.start_date and conv.end_date
3811                     and new_ret.set_of_books_id = ret.set_of_books_id
3812                     and dp.book_type_code = new_ret.book_type_code
3813                     and conv.prorate_date between dp.CALENDAR_PERIOD_OPEN_DATE
3814                                               and dp.CALENDAR_PERIOD_CLOSE_DATE
3815                   ;
3816 
3817                 end if;
3818 
3819               exception
3820                    when no_data_found then null;
3821               end;
3822 
3823 
3824               -- backout amount = catchup for reinstatement(=h_adj_exp_row) * (num of periods to backout)/(num of periods for the catchup)
3825               h_no_of_per_to_exclude := h_cpd_ctr - h_ret_prorate_pc;
3826 
3827               if (p_log_level_rec.statement_level) then
3828                   fa_debug_pkg.add('farboe','h_old_reinst_pc',
3829                                    h_old_reinst_pc, p_log_level_rec => p_log_level_rec);
3830                   fa_debug_pkg.add('farboe','h_old_ret_pc',
3831                                    h_old_ret_pc, p_log_level_rec => p_log_level_rec);
3832                   fa_debug_pkg.add('farboe','h_ret_prorate_pc',
3833                                    h_ret_prorate_pc, p_log_level_rec => p_log_level_rec);
3834                   fa_debug_pkg.add('farboe','h_adj_exp_row',
3835                                    h_adj_exp_row, p_log_level_rec => p_log_level_rec);
3836                   fa_debug_pkg.add('farboe','h_no_of_per_to_exclude',
3837                                    h_no_of_per_to_exclude, p_log_level_rec => p_log_level_rec);
3838               end if;
3839 
3840 	      /* Bug13011372: To calculate correct backup when both reinst, ret done in same period */
3841 	      if ( h_ret_prorate_pc <> h_old_ret_pc ) and ( h_cpd_ctr <> h_old_reinst_pc ) then
3842                  if h_no_of_per_to_exclude > 0 and (h_old_reinst_pc - h_old_ret_pc) > 0 then
3843                     backup_deprn := h_adj_exp_row * h_no_of_per_to_exclude / (h_old_reinst_pc - h_old_ret_pc);
3844                  else
3845                     backup_deprn := 0;
3846                  end if;
3847 	      end if;
3848 
3849               if (p_log_level_rec.statement_level) then
3850                    fa_debug_pkg.add('farboe','backup_deprn',
3851                                     backup_deprn, p_log_level_rec => p_log_level_rec);
3852                    fa_debug_pkg.add('farboe','END - RET AFTER REINST',
3853                                     ' ', p_log_level_rec => p_log_level_rec);
3854               end if;
3855 
3856         end if;
3857 
3858 --bug fix 3558253 and 3518604 end
3859 
3860              if (p_log_level_rec.statement_level) then
3861                  fa_debug_pkg.add('farboe','before J in reverse',1, p_log_level_rec => p_log_level_rec);
3862                  fa_debug_pkg.add('farboe','h_start_pdnum',h_start_pdnum, p_log_level_rec => p_log_level_rec);
3863                  fa_debug_pkg.add('farboe','(i)',i);
3864              end if;
3865 
3866              if (h_adj_amount <> 0)and( h_backout_flag = 1) and nvl(h_curr_pd_reinst,0) = 0 then
3867                 -- Bug 11782268 : Expense in period i is catchup for periods till i-1
3868                 for j in reverse h_start_pdnum..(i-1) loop
3869                    backup_deprn := 0;
3870 
3871                    frac_of_fiscal_year := 0;
3872 
3873                    if dpr_evenly > 0 then
3874                       if (p_log_level_rec.statement_level) then
3875                           fa_debug_pkg.add('farboe','J in reverse',2, p_log_level_rec => p_log_level_rec);
3876                       end if;
3877 
3878                       frac_of_fiscal_year := 1 / pds_per_year;
3879                    else
3880                       if (p_log_level_rec.statement_level) then
3881                           fa_debug_pkg.add('farboe','J in reverse',3, p_log_level_rec => p_log_level_rec);
3882                           fa_debug_pkg.add('farboe','d_cal',d_cal, p_log_level_rec => p_log_level_rec);
3883                       end if;
3884 
3885                       if not fa_cache_pkg.fazcff (d_cal, book,
3886                                         current_fiscal_yr, period_fracs, p_log_level_rec => p_log_level_rec) then
3887                          fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
3888                          return(FALSE);
3889                       end if;
3890                       frac_of_fiscal_year := period_fracs(j-1).frac;
3891                    end if;
3892 
3893 --bug fix 3558253 and 3518604 start
3894                    if(nvl(h_adj_exp_row,0) <> 0)and (nvl(h_prior_fy_exp,0) <> 0)and
3895                      (nvl(frac_of_fiscal_year,0) <> 0)and (nvl(h_annualized_adj,0) <> 0)
3896                    then
3897                            h_temp_calc := (h_adj_exp_row - h_prior_fy_exp)/(round(frac_of_fiscal_year,3) * (h_annualized_adj/h_cost_frac));
3898 
3899                            h_temp_calc := abs(trunc(h_temp_calc));
3900                      if (p_log_level_rec.statement_level) then
3901                        fa_debug_pkg.add('farboe','in calc of h_temp_calc','', p_log_level_rec => p_log_level_rec);
3902                      end if;
3903                    end if;
3904 
3905                    if (p_log_level_rec.statement_level) then
3906                      fa_debug_pkg.add('farboe','h_adj_exp_row', h_adj_exp_row, p_log_level_rec => p_log_level_rec);
3907                      fa_debug_pkg.add('farboe','h_prior_fy_exp', h_prior_fy_exp, p_log_level_rec => p_log_level_rec);
3908                      fa_debug_pkg.add('farboe','frac_of_fiscal_year', frac_of_fiscal_year, p_log_level_rec => p_log_level_rec);
3909                      fa_debug_pkg.add('farboe','h_annualized_adj', h_annualized_adj, p_log_level_rec => p_log_level_rec);
3910                      fa_debug_pkg.add('farboe','h_cost_frac', h_cost_frac, p_log_level_rec => p_log_level_rec);
3911                      fa_debug_pkg.add('farboe','h_temp_calc', h_temp_calc, p_log_level_rec => p_log_level_rec);
3912                    end if;
3913 
3914                  if(h_temp_calc > 0)then
3915                    if(j <= h_temp_calc)then
3916                       backup_deprn := frac_of_fiscal_year * h_annualized_adj;
3917                       tot_backup_deprn := tot_backup_deprn + backup_deprn;
3918                     end if;
3919                  else
3920                       backup_deprn := frac_of_fiscal_year * h_annualized_adj;
3921                       tot_backup_deprn := tot_backup_deprn + backup_deprn;
3922                  end if;
3923 --bug fix 3558253 and 3518604 end
3924                 end loop; -- end of loop - for j
3925 
3926              end if;  -- end of - if h_adj_amount
3927 
3928           /* Take the lesser of the absolute values of the
3929            * tot_backup_deprn or the h_adj_amount
3930            * Remember that both values are negative in the compare.   */
3931 
3932 
3933            /* Perform this comparison only when annualized_adjustment
3934             * is not zero. Otherwise add adjustment_amount to tot_backup_deprn
3935             * Fix for 807256 -  SNARAYAN
3936             */
3937 
3938              if (p_log_level_rec.statement_level) then
3939                  fa_debug_pkg.add('farboe','h_curr_pd_add',
3940                                         h_curr_pd_add, p_log_level_rec => p_log_level_rec);
3941              end if;
3942 
3943              if (h_annualized_adj <> 0) then
3944                    if (abs(h_adj_amount) < abs(tot_backup_deprn)) then
3945                       tot_backup_deprn := h_adj_amount;
3946                    end if;
3947 
3948                /* Add the annualized_adj only when it is in a period
3949                 * other than period of addition as the annualized amount
3950                 * is already factored into deprn_amount in fa_deprn_detail
3951                */
3952                    -- Bug 11782268 : Add annual_adj if h_annual_adj_check is 1
3953                    if (h_curr_pd_add = 1) or (h_annual_adj_check <> 0) then
3954                       -- Fix for Bug #3941213.  Don't do this for prior
3955                       -- period transfer.
3956                       if (h_prior_pd_tfr = 0) then
3957                          dpr_detail.deprn_amount(target_dpr_detail) :=
3958                                 dpr_detail.deprn_amount(target_dpr_detail) +
3959                                         tot_backup_deprn;
3960                        end if;
3961                    end if;
3962 
3963              elsif ( nvl(h_curr_pd_reinst,0) > 0 ) then
3964 
3965                    if (p_log_level_rec.statement_level) then
3966                       fa_debug_pkg.add('farboe','START - RET AFTER REINST', '2.1', p_log_level_rec => p_log_level_rec);
3967                       fa_debug_pkg.add('farboe','dpr_detail.deprn_amount(target_dpr_detail)'
3968                                                , dpr_detail.deprn_amount(target_dpr_detail) );
3969                       fa_debug_pkg.add('farboe','h_adj_amount'
3970                                                , h_adj_amount , p_log_level_rec => p_log_level_rec);
3971                       fa_debug_pkg.add('farboe','backup_deprn'
3972                                                , backup_deprn , p_log_level_rec => p_log_level_rec);
3973                    end if;
3974 
3975                          dpr_detail.deprn_amount(target_dpr_detail) :=
3976                                         h_adj_amount + backup_deprn +
3977                                 dpr_detail.deprn_amount(target_dpr_detail);
3978 
3979                    if (p_log_level_rec.statement_level) then
3980                       fa_debug_pkg.add('farboe','NEW dpr_detail.deprn_amount(target_dpr_detail)'
3981                                                , dpr_detail.deprn_amount(target_dpr_detail) );
3982                       fa_debug_pkg.add('farboe','END - RET AFTER REINST', '2.9', p_log_level_rec => p_log_level_rec);
3983                    end if;
3984              else
3985            /* else to add adjustment amount to total backup deprn when
3986               there is no annualized adjustment amount. Add adjustment
3987               amount only in period other than addition. Since if there
3988               was adjustment expense in period of addition then it would
3989               mean it was a retroactive-backdated addition and this would
3990               means annualized_adj will be or should have a value. In this
3991               case there is specific logic just above */
3992 
3993                    if (p_log_level_rec.statement_level) then
3994                       fa_debug_pkg.add('farboe','6.1', 6.1, p_log_level_rec => p_log_level_rec);
3995                    end if;
3996 		   /* Bug#13020521: For considering adj_amt in poa for prior dated assets */
3997                    if (h_curr_pd_add = 0) or ((h_curr_pd_add = 1) and (h_annualized_adj = 0)) then
3998                       if (p_log_level_rec.statement_level) then
3999                          fa_debug_pkg.add('farboe','6.2', 6.2, p_log_level_rec => p_log_level_rec);
4000                          fa_debug_pkg.add('farboe','h_adj_amount',
4001                                                 h_adj_amount, p_log_level_rec => p_log_level_rec);
4002                          fa_debug_pkg.add('farboe','target_dpr_detail',
4003                                                 target_dpr_detail, p_log_level_rec => p_log_level_rec);
4004                       end if;
4005 
4006                       -- Fix for Bug #3941213.  Don't do this for prior
4007                       -- period transfer.
4008                       if (h_prior_pd_tfr = 0) then
4009                          dpr_detail.deprn_amount(target_dpr_detail) :=
4010                                         h_adj_amount +
4011                                 dpr_detail.deprn_amount(target_dpr_detail);
4012                       end if;
4013                       if (p_log_level_rec.statement_level) then
4014                          fa_debug_pkg.add('farboe','6.2.1', 6.21, p_log_level_rec => p_log_level_rec);
4015                       end if;
4016                    end if;
4017                 if (p_log_level_rec.statement_level) then
4018                     fa_debug_pkg.add('farboe','6.3', 6.3, p_log_level_rec => p_log_level_rec);
4019                 end if;
4020              end if;
4021 
4022 
4023        /* Based on the number of adjustments and retirement expense rows
4024         * add to the total backup deprn. The following was required as
4025         * fix for 807256. May not be the most elegant but  it works
4026         * without significant redesign of farboe. Always adding
4027         * h_deprn_amount which was the difference between deprn_amount
4028         * in fa_deprn_detail and adjustment_amount in fa_adjustments
4029         * does not work for all the cases. Instead the following
4030         * conditional logic adds the amounts correctly. SNARAYAN
4031        */
4032 
4033               if p_log_level_rec.statement_level then
4034                  fa_debug_pkg.add('farboe','7',7, p_log_level_rec => p_log_level_rec);
4035               end if;
4036 
4037              if ((h_adj_count > 0) and (h_ret_count = 0)) then
4038            /* When there are expense rows other than retirement expense
4039               add the current period deprn amount once to the total
4040               being backed out for each distinct distribution */
4041                  if (prev_dist_id <> h_dist_id) then
4042                      dpr_detail.deprn_amount(target_dpr_detail) :=
4043                                 h_currpd_amount +
4044                                  dpr_detail.deprn_amount(target_dpr_detail);
4045                  end if;
4046              elsif ((h_adj_count = 0) and (h_ret_count = 0)) then
4047            /* When there are no expense rows or retirment expense rows
4048               add the current period deprn amount for each distribution
4049               fetched to the total being backed out */
4050                     dpr_detail.deprn_amount(target_dpr_detail) :=
4051                                 h_deprn_amount +
4052                                 dpr_detail.deprn_amount(target_dpr_detail);
4053 
4054              elsif ((h_adj_count = 0) and (h_ret_count > 0) and
4055                         (h_source_type_code in ('DEPRECIATION', 'RETIREMENT'))) then --bug fix 4995325
4056                  if (prev_dist_id <> h_dist_id) then
4057 				     /* Bug16479989: Modified to calculate correct catchup for daily prorate */
4058                      if ( p_pers_per_yr = 365 and h_i = h_start_pdnum ) then
4059                         dpr_detail.deprn_amount(target_dpr_detail) :=
4060                                 h_deprn_amount - h_adj_amount +
4061                                  dpr_detail.deprn_amount(target_dpr_detail);
4062                      else
4063                         dpr_detail.deprn_amount(target_dpr_detail) :=
4064                                 h_currpd_amount +
4065                                  dpr_detail.deprn_amount(target_dpr_detail);
4066                      end if;
4067                  end if;
4068              elsif ((h_adj_count > 0) and (h_ret_count > 0)) then
4069                  if (prev_dist_id <> h_dist_id) then
4070                      dpr_detail.deprn_amount(target_dpr_detail) :=
4071                                 h_currpd_amount +
4072                                  dpr_detail.deprn_amount(target_dpr_detail);
4073                  end if;
4074              end if;
4075              prev_dist_id := h_dist_id;
4076              if p_log_level_rec.statement_level then
4077                 fa_debug_pkg.add('farboe','9',9, p_log_level_rec => p_log_level_rec);
4078              end if;
4079 
4080           end loop; -- end of - while deprn_adj
4081 
4082           if mrc_sob_type_code <> 'R' then
4083                  close DEPRN_ADJ;
4084           else
4085                  close MRC_DEPRN_ADJ;
4086           end if;
4087 
4088           -- if h_start_pdnum <> h_end_pdnum -1  and h_i = h_start_pdnum + 1 then
4089           if pds_catchup <> 0  and h_i = h_start_pdnum + 1 then
4090 
4091              h_temp_startpd_deprn := 0;
4092              for k in 1 .. dpr_detail_counter loop
4093                 h_temp_startpd_deprn := h_temp_startpd_deprn +
4094                                         dpr_detail.deprn_amount(k);
4095              end loop; -- end of - for k
4096           end if;
4097           if h_i = h_start_pdnum then
4098              --h_start_pd_pc := h_pc;
4099              --Bug#14237277: Reinitializing ret_prorate_pc
4100              h_start_pd_pc := ( h_current_fiscal_yr * pds_per_year ) + h_i;
4101           end if;
4102 
4103           h_i := i;
4104 
4105        end loop; -- end of - for i in reverse
4106 
4107        if adj_type = 'EXPENSE' then
4108 
4109           adj_row.adjustment_type := 'EXPENSE';
4110           adj_row.debit_credit_flag := 'DR';
4111 
4112        elsif adj_type = 'BONUS EXPENSE' then
4113 
4114           select nvl(cb.bonus_deprn_expense_acct,'0')
4115           into h_bonus_deprn_exp_acct
4116           from fa_additions_b ad,
4117                fa_category_books cb
4118           where ad.asset_id = h_asset_id
4119           and   cb.category_id = ad.asset_category_id
4120           and   cb.book_type_code = h_book;
4121 
4122           bonus_deprn_exp_acct := h_bonus_deprn_exp_acct;
4123           adj_row.account := bonus_deprn_exp_acct;
4124           adj_row.account_type := 'BONUS_DEPRN_EXPENSE_ACCT';
4125           adj_row.adjustment_type := 'BONUS EXPENSE';
4126           adj_row.debit_credit_flag := 'DR';
4127 
4128        elsif adj_type = 'IMPAIR EXPENSE' then
4129 
4130           select nvl(cb.impair_expense_acct,'0')
4131           into h_impair_exp_acct
4132           from fa_additions_b ad,
4133                fa_category_books cb
4134           where ad.asset_id = h_asset_id
4135           and   cb.category_id = ad.asset_category_id
4136           and   cb.book_type_code = h_book;
4137 
4138           impair_exp_acct := h_impair_exp_acct;
4139           adj_row.account := impair_exp_acct;
4140           adj_row.account_type := 'IMPAIR_EXPENSE_ACCT';
4141           adj_row.adjustment_type := 'IMPAIR EXPENSE';
4142           adj_row.debit_credit_flag := 'DR';
4143 
4144        elsif adj_type = 'REVAL EXPENSE' then
4145 
4146           adj_row.adjustment_type := 'REVAL EXPENSE';
4147           adj_row.debit_credit_flag := 'DR';
4148 
4149        else
4150 
4151           adj_row.adjustment_type := 'REVAL AMORT';
4152           adj_row.debit_credit_flag := 'DR';
4153 
4154        end if;
4155 
4156     /* BUG# 1400554
4157        populating the account seg for expense with the value
4158        in category books
4159            -- bridgway 09/14/00
4160 
4161        adj_row.account[0] = '\0';
4162     */
4163 
4164        select  facb.deprn_expense_acct
4165         into   h_deprn_exp_acct
4166         from   fa_additions_b    faadd,
4167                fa_category_books facb,
4168                fa_book_controls bc
4169        where   faadd.asset_id = h_asset_id
4170          and   facb.category_id = faadd.asset_category_id
4171          and   facb.book_type_code = h_book
4172          and   bc.book_type_code = facb.book_type_code;
4173 
4174        adj_row.account := h_deprn_exp_acct;
4175 
4176        adj_row.transaction_header_id := th_id_in;
4177        adj_row.source_type_code := 'RETIREMENT';
4178        adj_row.book_type_code := book;
4179        adj_row.period_counter_created := cpd_ctr;
4180        adj_row.asset_id := asset_id;
4181        adj_row.period_counter_adjusted := cpd_ctr;
4182        adj_row.last_update_date := today;
4183 
4184 --       adj_row.account_type := 'DEPRN_EXPENSE_ACCT';
4185 
4186 --bug fix 4492828 in case of bonus we need to use bonus account as set above
4187        if (adj_type not in ('BONUS EXPENSE', 'IMPAIR EXPENSE')) then
4188            adj_row.account_type := 'DEPRN_EXPENSE_ACCT';
4189        end if;
4190 
4191        adj_row.current_units := current_units;
4192        adj_row.selection_thid := 0;
4193        adj_row.flush_adj_flag := TRUE;
4194        adj_row.annualized_adjustment := 0;
4195        adj_row.asset_invoice_id := 0;
4196        adj_row.leveling_flag := TRUE;
4197        adj_row.gen_ccid_flag := TRUE;
4198        adj_row.code_combination_id := 0;
4199 
4200        h_tot_deprn := 0;
4201        h_start_pd_deprn := 0;
4202 
4203        if p_log_level_rec.statement_level then
4204           fa_debug_pkg.add('farboe','dpr_detail_counter',dpr_detail_counter, p_log_level_rec => p_log_level_rec);
4205        end if;
4206 
4207        for m in 1 .. dpr_detail_counter loop
4208           h_tot_deprn := h_tot_deprn + dpr_detail.deprn_amount(m);
4209        end loop; -- end of - for m
4210 
4211        h_start_pd_deprn := h_tot_deprn - h_temp_startpd_deprn;
4212 
4213        if p_log_level_rec.statement_level then
4214           fa_debug_pkg.add('farboe','h_tot_deprn',h_tot_deprn, p_log_level_rec => p_log_level_rec);
4215           fa_debug_pkg.add('farboe','h_temp_startpd_deprn',
4216                                      h_temp_startpd_deprn, p_log_level_rec => p_log_level_rec);
4217           fa_debug_pkg.add('farboe','h_start_pd_deprn',h_start_pd_deprn, p_log_level_rec => p_log_level_rec);
4218        end if;
4219 
4220 
4221       /* Bug#4605961 */
4222       begin
4223        SELECT  pcal.period_num
4224        INTO    h_start_pd_endpp
4225        FROM    fa_calendar_periods pcal,
4226                fa_deprn_periods dp
4227        WHERE   calendar_type = p_cal
4228        AND     dp.book_type_code = h_book
4229        AND     dp.fiscal_year = h_current_fiscal_yr
4230        AND     dp.period_num = h_start_pdnum
4231        AND     dp.calendar_period_close_date
4232                         between start_date and end_date;
4233       exception
4234         when no_data_found then
4235           h_start_pd_endpp := h_start_pdnum;
4236       end;
4237 
4238 
4239        adj_row.adjustment_amount := h_tot_deprn;
4240 
4241        if p_log_level_rec.statement_level then
4242           fa_debug_pkg.add('farboe','h_start_pd_endpp',h_start_pd_endpp, p_log_level_rec => p_log_level_rec);
4243        end if;
4244 
4245        if mrc_sob_type_code <> 'R' then
4246 
4247           SELECT decode(fy1.fiscal_year, fy2.fiscal_year,1,0),
4248                     decode(BC.DEPR_FIRST_YEAR_RET_FLAG, 'YES', 1, 0),
4249                     decode(ctype.depr_when_acquired_flag,'YES',1,0),
4250                     decode(mt.rate_source_rule,
4251                                 'CALCULATED', 1,
4252                                 'TABLE', 2,
4253                                 'FLAT', 3)
4254           INTO h_same_fy, h_depr_first_year_ret,
4255                h_dwacq, h_rate_source_rule
4256           FROM FA_FISCAL_YEAR fy1,
4257                FA_FISCAL_YEAR fy2,
4258                FA_BOOKS bk,
4259                FA_RETIREMENTS rt,
4260                FA_CONVENTION_TYPES ctype,
4261                FA_METHODS mt,
4262                FA_BOOK_CONTROLS bc
4263           WHERE   rt.date_retired between
4264                   fy1.start_date and fy1.end_date
4265           AND   bk.deprn_start_date between
4266                      fy2.start_date and fy2.end_date
4267           AND   rt.asset_id = h_asset_id
4268           AND   bk.asset_id = h_asset_id
4269           AND   bk.book_type_code = bc.book_type_code
4270           AND   bc.book_type_code = h_book
4271           AND   rt.retirement_id = h_retirement_id
4272           AND   bk.retirement_id = rt.retirement_id
4273         AND   bk.transaction_header_id_out is not null
4274         AND   bk.deprn_method_code = mt.method_code
4275         AND   nvl(bk.life_in_months,1) = nvl(mt.life_in_months,1)
4276         AND   bk.prorate_convention_code = ctype.prorate_convention_code
4277         AND   fy1.fiscal_year_name = bc.fiscal_year_name
4278         AND   fy2.fiscal_year_name = bc.fiscal_year_name;
4279 
4280       else
4281 
4282          select decode(fy1.fiscal_year, fy2.fiscal_year,1,0),
4283                     decode(BC.DEPR_FIRST_YEAR_RET_FLAG, 'YES', 1, 0),
4284                     decode(ctype.depr_when_acquired_flag,'YES',1,0),
4285                     decode(mt.rate_source_rule,
4286                                 'CALCULATED', 1,
4287                                 'TABLE', 2,
4288                                 'FLAT', 3)
4289         INTO h_same_fy, h_depr_first_year_ret,
4290              h_dwacq, h_rate_source_rule
4291         FROM FA_FISCAL_YEAR fy1,
4292            FA_FISCAL_YEAR fy2,
4293            FA_MC_BOOKS bk,
4294            FA_MC_RETIREMENTS rt,
4295            FA_CONVENTION_TYPES ctype,
4296            FA_METHODS mt,
4297            FA_BOOK_CONTROLS bc
4298         WHERE   rt.date_retired between
4299                   fy1.start_date and fy1.end_date
4300         AND   bk.deprn_start_date between
4301                      fy2.start_date and fy2.end_date
4302         AND   bk.set_of_books_id = ret.set_of_books_id
4303         AND   rt.set_of_books_id = ret.set_of_books_id
4304         AND   rt.asset_id = h_asset_id
4305         AND   bk.asset_id = h_asset_id
4306         AND   bk.book_type_code = bc.book_type_code
4307         AND   bc.book_type_code = h_book
4308         AND   rt.retirement_id = h_retirement_id
4309         AND   bk.retirement_id = rt.retirement_id
4310         AND   bk.transaction_header_id_out is not null
4311         AND   bk.deprn_method_code = mt.method_code
4312         AND   nvl(bk.life_in_months,1) = nvl(mt.life_in_months,1)
4313         AND   bk.prorate_convention_code = ctype.prorate_convention_code
4314         AND   fy1.fiscal_year_name = bc.fiscal_year_name
4315         AND   fy2.fiscal_year_name = bc.fiscal_year_name;
4316 
4317       end if;
4318 
4319     /* If depreciate_lastyr is TRUE then we calculate the fraction of
4320        expense to backout based on prorate periods. Otherwise we backout
4321        the entire amount for current fiscal year and it is not necessary
4322        to calculate the fraction. If depreciate_lastyr in the method is
4323        TRUE but asset if retired is retired in first year and
4324        DEPR_FIRST_YEAR_RET_FLAG in book controls is NO then we need to
4325        back out all the deprn taken so far and there is no need to calculate
4326        fraction of depreciation to backout.
4327     */
4328 
4329        if p_log_level_rec.statement_level then
4330           fa_debug_pkg.add('farboe(2)','h_same_fy',h_same_fy);
4331           fa_debug_pkg.add('farboe(2)','h_depr_first_year_ret',h_depr_first_year_ret);
4332        end if;
4333 
4334        temp_start_pp := 0;
4335 
4336        -- Get the number of periods per year in the rate calendar
4337        if not fa_cache_pkg.fazcct(p_cal, p_log_level_rec => p_log_level_rec) then
4338           fa_srvr_msg.add_message(calling_fn => 'fa_gainloss_ret_pkg.fagfpc',  p_log_level_rec => p_log_level_rec);
4339           raise farboe_err;
4340        end if;
4341 
4342        p_pers_per_yr := fa_cache_pkg.fazcct_record.number_per_fiscal_year;
4343 
4344        if p_log_level_rec.statement_level then
4345            fa_debug_pkg.add('farboe(1.4)','p_pers_per_yr',p_pers_per_yr);
4346            fa_debug_pkg.add('farboe(1.4)','depreciate_lastyr',depreciate_lastyr);
4347            fa_debug_pkg.add('farboe(1.4)','h_same_fy',h_same_fy);
4348            fa_debug_pkg.add('farboe(1.4)','h_depr_first_year_ret',h_depr_first_year_ret);
4349            fa_debug_pkg.add('farboe(1.4)','================','');
4350            fa_debug_pkg.add('farboe(1.4)','p_pers_per_yr',p_pers_per_yr);
4351            fa_debug_pkg.add('farboe(1.4)','h_end_pdnum',h_end_pdnum);
4352            fa_debug_pkg.add('farboe(1.4)','++h_start_pdnum',h_start_pdnum);
4353            fa_debug_pkg.add('farboe(1.4)','++bk.pc_fully_reserved',bk.pc_fully_reserved);
4354            fa_debug_pkg.add('farboe(1.4)','++h_start_pd_pc',h_start_pd_pc);
4355        end if;
4356 
4357      if p_pers_per_yr <> 365 then
4358        if (depreciate_lastyr and not
4359 
4360           /*  FIX for BUG#2787098:
4361              This logic seems to be trasformed wrongly from pro*C code.
4362              (h_same_fy = 1  and  (not h_depr_first_year_ret = 0))) then
4363           */
4364          (h_same_fy = 1  and  (not (h_depr_first_year_ret = 1)))) then
4365          if (h_same_fy = 1) then
4366 
4367            --  Get the Fiscal Year Name from FA_BOOK_CONTROLS cache
4368             if not fa_cache_pkg.fazcbc(book, p_log_level_rec => p_log_level_rec) then
4369              raise farboe_err;
4370 
4371             end if;
4372 
4373           fy_name := fa_cache_pkg.fazcbc_record.fiscal_year_name;
4374 
4375           -- Get the prorate period, and the corresponding fiscal year
4376           if not fa_cache_pkg.fazccp(p_cal, fy_name, dpr.prorate_jdate,
4377                              pp_begin, y_begin, dummy_num, p_log_level_rec => p_log_level_rec) then
4378              raise farboe_err;
4379 
4380           end if;
4381 
4382           /*  FIX for BUG#2787098:
4383             Changed dpr.prorate_jdate to dpr.deprn_start_jdate in call fazccp
4384             to make it in sinc with the pro*c code
4385           */
4386           -- Get the depreciation start prorate period
4387           if not fa_cache_pkg.fazccp(p_cal, fy_name, dpr.deprn_start_jdate,
4388                              dpp_begin, dpy_begin, dummy_num, p_log_level_rec => p_log_level_rec) then
4389              raise farboe_err;
4390 
4391           end if;
4392 
4393           /*  FIX for BUG#2787098:
4394             Changed condition of h_dwacq to make it in sinc with the pro*c code
4395           */
4396           if ((start_pp <  pp_begin) and (h_rate_source_rule = 1)) then
4397               temp_start_pp := pp_begin;
4398            elsif ((start_pp <  pp_begin) and (h_rate_source_rule <> 1)
4399                     and (not (h_dwacq = 0))) then
4400               temp_start_pp :=  pp_begin;
4401            elsif ((start_pp < pp_begin) and (h_rate_source_rule <> 1)
4402                     and (h_dwacq = 1)) then
4403               temp_start_pp :=  dpp_begin;
4404            else temp_start_pp := start_pp;
4405            end if;
4406          else
4407            temp_start_pp := start_pp;
4408          end if;
4409 
4410          temp_pds :=  -1 * pds_catchup;
4411          /*  FIX for BUG#2787098:
4412              Changed the following to make it in sinc with the pro*c code
4413              Old code: temp_frac := (end_pp - temp_start_pp) /temp_pds;
4414          */
4415 
4416          --Bug 5086360 In period of addition end_pp = temp_start_pp
4417          if (end_pp = temp_start_pp ) then
4418             temp_frac := 0;
4419          else
4420             temp_frac := temp_pds/(end_pp - temp_start_pp);
4421          end if;
4422 
4423          h_tot_deprn := h_tot_deprn * temp_frac;
4424 
4425 
4426          --------------------- Bug 5148828 : NEW APPROACH for Bug#5074257
4427          -- We can remove the logic above after some more verifications.
4428 
4429 
4430 
4431          -- Bug 5443855
4432          begin
4433 
4434             if mrc_sob_type_code <> 'R' then
4435 
4436               select period_counter + 1
4437               into h_asset_addition_pc
4438               from fa_deprn_summary
4439               where asset_id = h_asset_id
4440               and book_type_code = h_book
4441               and deprn_source_code = 'BOOKS';
4442 
4443 	      -- bug#8496694
4444 	      select decode(dp1.fiscal_year,dp2.fiscal_year,1,0)
4445 	      into l_same_fy
4446 	      from fa_deprn_periods dp1,
4447 	      fa_deprn_periods dp2
4448 	      where dp1.book_type_code = h_book
4449 	      and dp2.book_type_code = dp1.book_type_code
4450 	      and dp1.period_counter = h_asset_addition_pc
4451 	      and dp2.period_counter = h_start_pd_pc;
4452 
4453 	      --  Bug 14100792
4454 
4455 	      Select count(*)
4456 	      into l_adj_overlap
4457 	      from dual
4458 	      where EXISTS (select 'Ret Overlapping Adj'
4459 	                    from  fa_transaction_headers th,
4460                                   fa_deprn_periods dp
4461 	                    where dp.book_type_code = h_book
4462 	                    and   th.book_type_code = h_book
4463 	                    and   th.asset_id = h_asset_id
4464 	                    and   dp.period_counter = h_start_pd_pc - 1
4465 	                    and   th.transaction_type_code = 'ADJUSTMENT'
4466 	                    and   th.transaction_subtype = 'AMORTIZED'
4467 	                    and   th.date_effective > dp.period_close_date);
4468 
4469             else
4470 
4471               select period_counter + 1
4472               into h_asset_addition_pc
4473               from fa_mc_deprn_summary
4474               where asset_id = h_asset_id
4475               and book_type_code = h_book
4476               and set_of_books_id = ret.set_of_books_id
4477               and deprn_source_code = 'BOOKS';
4478 
4479 	      -- bug#8496694
4480 	      select decode(dp1.fiscal_year,dp2.fiscal_year,1,0)
4481 	      into l_same_fy
4482 	      from fa_mc_deprn_periods dp1,
4483 	      fa_mc_deprn_periods dp2
4484 	      where dp1.book_type_code = h_book
4485 	      and dp1.set_of_books_id = ret.set_of_books_id
4486 	      and dp2.set_of_books_id = dp1.set_of_books_id
4487 	      and dp2.book_type_code = dp1.book_type_code
4488 	      and dp1.period_counter = h_asset_addition_pc
4489 	      and dp2.period_counter = h_start_pd_pc;
4490 
4491 	      --  Bug 14100792
4492 	      Select count(*)
4493 	      into l_adj_overlap
4494 	      from dual
4495 	      where EXISTS ( Select 'Ret Overlapping Adj'
4496 	                     from  fa_transaction_headers th,
4497                                    fa_mc_deprn_periods dp
4498 	                     where dp.book_type_code = h_book
4499 	                     and   dp.set_of_books_id = ret.set_of_books_id
4500 	                     and   th.book_type_code = h_book
4501 	                     and   th.asset_id = h_asset_id
4502 	                     and   dp.period_counter = h_start_pd_pc - 1
4503 	                     and   th.transaction_type_code = 'ADJUSTMENT'
4504 	                     and   th.transaction_subtype = 'AMORTIZED'
4505 	                     and   th.date_effective > dp.period_close_date);
4506 
4507             end if;
4508 
4509             exception
4510                when no_data_found then null;
4511          end;
4512 
4513          if p_log_level_rec.statement_level then
4514            fa_debug_pkg.add('farboe','++ FOR CATCHUP: h_Brow_deprn_reserve', h_Brow_deprn_reserve, p_log_level_rec => p_log_level_rec);
4515            fa_debug_pkg.add('farboe','++ FOR CATCHUP: h_asset_addition_pc', h_asset_addition_pc, p_log_level_rec => p_log_level_rec);
4516            fa_debug_pkg.add('farboe','++ FOR CATCHUP: h_start_pd_pc (ret prorated pc)', h_start_pd_pc);
4517 	   fa_debug_pkg.add('farboe','++ FOR CATCHUP: l_same_fy', l_same_fy);
4518 	   fa_debug_pkg.add('farboe','++ FOR CATCHUP: l_adj_overlap', l_adj_overlap);
4519          end if;
4520 
4521          -- h_start_pd_pc : the period counter in which the prorated retired_date falls
4522          -- h_asset_addition_pc : the period counter in which the asset was added (i.e. period of addition)
4523          -- 7212162
4524          -- bug#8496694:Added condition to check if prorated ret_pc and poa in same fiscal year
4525 		 -- Bug 14100792 Added i_adj_overlap to below condition
4526          /* Bug15909667: Overlapping retirement in first year of asset life */
4527          if ( h_Brow_deprn_reserve = 0 and not ( l_same_fy = 1 and l_overlap_ret ) )
4528          or (( h_start_pd_pc > nvl(h_asset_addition_pc,0)) and l_same_fy = 1 )
4529 	     OR (l_adj_overlap > 0) then
4530 
4531            -- Bug# 5018194
4532            if not fa_cache_pkg.fazccp(p_cal, fiscal_year_name,
4533                                   bk.deprn_start_jdate,
4534                                   deprn_start_pnum, deprn_start_fy, dummy_num, p_log_level_rec => p_log_level_rec) then
4535                fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4536                raise farboe_err;
4537            end if;
4538 
4539            if p_log_level_rec.statement_level then
4540              fa_debug_pkg.add('farboe','++ CATCHUP: ret.th_id_in', ret.th_id_in, p_log_level_rec => p_log_level_rec);
4541              fa_debug_pkg.add('farboe','++ CATCHUP: deprn_start_pnum', deprn_start_pnum, p_log_level_rec => p_log_level_rec);
4542              fa_debug_pkg.add('farboe','++ CATCHUP: h_cost_frac', h_cost_frac, p_log_level_rec => p_log_level_rec);
4543              fa_debug_pkg.add('farboe','++ CATCHUP: h_Brow_deprn_reserve', h_Brow_deprn_reserve, p_log_level_rec => p_log_level_rec);
4544            end if;
4545 
4546            -- Bug 5738004
4547            dpr.calc_catchup := TRUE;
4548 
4549            if (not FA_GAINLOSS_DPR_PKG.CALC_CATCHUP(
4550                               ret                  => ret,
4551                               BK                   => bk,
4552                               DPR                  => dpr,
4553                               calc_catchup         => TRUE, -- (start_pd < cpdnum),
4554                               x_deprn_exp          => l_deprn_exp,
4555                               x_bonus_deprn_exp    => l_bonus_deprn_exp,
4556                               x_impairment_exp     => l_impairment_exp,
4557                               x_asset_fin_rec_new  => l_asset_fin_rec_new,
4558                               p_log_level_rec      => p_log_level_rec)) then
4559 
4560                     fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4561                     return(FALSE);
4562 
4563             end if;
4564 
4565            -- Bug 9930721 : When farboe is called for Bonus, use l_bonus_deprn_exp
4566            if adj_type = 'BONUS EXPENSE' then
4567               l_deprn_exp := l_bonus_deprn_exp;
4568            end if;
4569 
4570            -- Bug 5362790
4571            -- Bug 5652883 (Removed '=' from if clause)
4572            -- if deprn_start_pnum >= start_pp and h_same_fy=1  then
4573            if deprn_start_pnum > start_pp and h_same_fy=1  then
4574              h_tot_deprn := l_deprn_exp;
4575            else
4576 --              h_tot_deprn := l_deprn_exp * abs(pds_catchup) / (end_pp - start_pp);
4577 
4578           -- Fix for Bug #5844937/5851102.  To prevent divisor by zero error,
4579           -- set the h_tot_deprn to 0 when it should have calculated to 0.
4580           if (l_deprn_exp = 0) or (pds_catchup = 0) or (end_pp = start_pp) then
4581 
4582              h_tot_deprn := 0;
4583           else
4584              --Bug#8978794:Made changes when ret_prorate_pc < fully_rsvd_pc
4585              if bk.fully_reserved then
4586 	        begin
4587 		   select pcal.period_num
4588                    into    h_fully_rsv_pd
4589                    from    fa_calendar_periods pcal,
4590                    fa_deprn_periods dp
4591                    where   calendar_type = p_cal
4592                    and     dp.book_type_code = h_book
4593                    and     dp.fiscal_year = h_current_fiscal_yr
4594                    and     dp.period_counter = bk.pc_fully_reserved
4595                    and     dp.calendar_period_close_date
4596                    between start_date and end_date;
4597 
4598 		exception
4599 		   when no_data_found then null;
4600                 end;
4601 
4602 	        if p_log_level_rec.statement_level then
4603                    fa_debug_pkg.add('farboe','++ CATCHUP: h_fully_rsv_pd ', h_fully_rsv_pd, p_log_level_rec => p_log_level_rec);
4604                 end if;
4605 	     end if; --bk.fully_reserved
4606              --Bug#9466566: To avoid divide by zero error.
4607              if not ( h_fully_rsv_pd = -1 ) and not ( end_pp = h_fully_rsv_pd + 1 ) then
4608                 h_tot_deprn := l_deprn_exp * abs(pds_catchup) / ((end_pp - start_pp) * ((end_pp - h_fully_rsv_pd) - 1));
4609              else
4610                 h_tot_deprn := l_deprn_exp * abs(pds_catchup) / (end_pp - start_pp);
4611              end if;
4612           end if;
4613 
4614            end if;
4615 
4616 
4617            if p_log_level_rec.statement_level then
4618             fa_debug_pkg.add('farboe','++ CATCHUP: h_same_fy', h_same_fy, p_log_level_rec => p_log_level_rec);
4619             fa_debug_pkg.add('farboe','++ CATCHUP: l_deprn_exp', l_deprn_exp, p_log_level_rec => p_log_level_rec);
4620             fa_debug_pkg.add('farboe','++ CATCHUP: pds_catchup', pds_catchup, p_log_level_rec => p_log_level_rec);
4621             fa_debug_pkg.add('farboe','++ CATCHUP: start_pp', start_pp, p_log_level_rec => p_log_level_rec);
4622             fa_debug_pkg.add('farboe','++ CATCHUP: deprn_start_pnum', deprn_start_pnum, p_log_level_rec => p_log_level_rec);
4623             fa_debug_pkg.add('farboe','++ CATCHUP: end_pp', end_pp, p_log_level_rec => p_log_level_rec);
4624             fa_debug_pkg.add('farboe','++ CATCHUP: h_tot_deprn', h_tot_deprn, p_log_level_rec => p_log_level_rec);
4625            end if;
4626 
4627          end if;
4628        ---------------------
4629 
4630 
4631          if p_log_level_rec.statement_level then
4632            fa_debug_pkg.add('farboe(1.5)','temp_start_pp',temp_start_pp);
4633            fa_debug_pkg.add('farboe(1.5)','end_pp',end_pp);
4634            fa_debug_pkg.add('farboe(1.5)','pds_catchup',pds_catchup);
4635            fa_debug_pkg.add('farboe(1.5)','temp_pds',temp_pds);
4636            fa_debug_pkg.add('farboe(1.5)','temp_frac',temp_frac);
4637            fa_debug_pkg.add('farboe(1.5)','h_tot_deprn',h_tot_deprn);
4638          end if;
4639 
4640        end if;
4641 
4642     -- Bug# 5018194: elsif p_pers_per_yr = 365 and h_start_pdnum <> h_end_pdnum - 1 then  -- p_pds_per year is 365
4643     elsif p_pers_per_yr = 365 and pds_catchup <> 0
4644           and nvl(bk.pc_fully_reserved,-1) <> h_start_pd_pc then  -- p_pds_per year is 365
4645 
4646        if p_log_level_rec.statement_level then
4647           fa_debug_pkg.add('farboe','IN 365',365, p_log_level_rec => p_log_level_rec);
4648           fa_debug_pkg.add('farboe','++ p_cal',p_cal, p_log_level_rec => p_log_level_rec);
4649           fa_debug_pkg.add('farboe','++ fiscal_year_name',fiscal_year_name, p_log_level_rec => p_log_level_rec);
4650           fa_debug_pkg.add('farboe','++ bk.deprn_start_jdate',bk.deprn_start_jdate, p_log_level_rec => p_log_level_rec);
4651        end if;
4652 
4653        -- Bug fix 5660467(Added if condition to call funtion FA_GAINLOSS_DPR_PKG.CALC_CATCHUP
4654         -- only when asset was added without reserve)
4655 	   -- Bug:16008341 Checking for expensed adjustments
4656        if h_Brow_deprn_reserve <> 0    then
4657           if mrc_sob_type_code <> 'R' then
4658              begin
4659                 select count(*)
4660                 into h_daily_annual_adj_check
4661                 from fa_transaction_headers th,
4662                 fa_deprn_periods dp
4663                 where dp.book_type_code = h_book
4664                 and   dp.period_counter = h_start_pd_pc - 1
4665                 and   th.book_type_code = h_book
4666                 and   th.asset_id = h_asset_id
4667                 and   th.transaction_key is null
4668                 and   nvl(th.transaction_subtype,'XX') = 'EXPENSED'
4669                 and   th.date_effective > dp.period_close_date;
4670              exception
4671                 when no_data_found then
4672                    h_daily_annual_adj_check := 0;
4673                 end;
4674           else
4675              begin
4676                 select count(*)
4677                 into h_daily_annual_adj_check
4678                 from fa_transaction_headers th,
4679                 fa_mc_deprn_periods dp
4680                 where dp.book_type_code = h_book
4681                 and   dp.period_counter = h_start_pd_pc - 1
4682                 and   dp.set_of_books_id = ret.set_of_books_id
4683                 and   th.book_type_code = h_book
4684                 and   th.asset_id = h_asset_id
4685                 and   th.transaction_key is null
4686                 and   nvl(th.transaction_subtype,'XX') = 'EXPENSED'
4687                 and   th.date_effective > dp.period_close_date;
4688              exception
4689                 when no_data_found then
4690                    h_daily_annual_adj_check := 0;
4691               end;
4692           end if;
4693        end if;
4694 
4695        if p_log_level_rec.statement_level then
4696           fa_debug_pkg.add('farboe','++ h_daily_annual_adj_check ',h_daily_annual_adj_check, p_log_level_rec => p_log_level_rec);
4697        end if;
4698 
4699        if h_Brow_deprn_reserve = 0 or h_daily_annual_adj_check <> 0 then
4700           -- Bug# 5018194
4701           if not fa_cache_pkg.fazccp(p_cal, fiscal_year_name,
4702                                      bk.deprn_start_jdate,
4703                                     deprn_start_pnum, deprn_start_fy, dummy_num, p_log_level_rec => p_log_level_rec) then
4704                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4705                 raise farboe_err;
4706           end if;
4707 
4708          if p_log_level_rec.statement_level then
4709             fa_debug_pkg.add('farboe','++ deprn_start_fy', deprn_start_fy, p_log_level_rec => p_log_level_rec);
4710             fa_debug_pkg.add('farboe','++ deprn_start_pnum', deprn_start_pnum, p_log_level_rec => p_log_level_rec);
4711           end if;
4712 
4713           h_ret_pjdate := to_char(bk.ret_prorate_date, 'J');
4714 
4715           if p_log_level_rec.statement_level then
4716              fa_debug_pkg.add('farboe','h_ret_pjdate',h_ret_pjdate, p_log_level_rec => p_log_level_rec);
4717           end if;
4718 
4719           -- Get the depreciation start prorate period
4720           if not fa_cache_pkg.fazccp(p_cal, fiscal_year_name, h_ret_pjdate,
4721                             h_ret_pp, dummy_num2, dummy_num, p_log_level_rec => p_log_level_rec) then
4722                raise farboe_err;
4723           end if;
4724 
4725           if p_log_level_rec.statement_level then
4726              fa_debug_pkg.add('farboe','h_ret_pp',h_ret_pp, p_log_level_rec => p_log_level_rec);
4727              fa_debug_pkg.add('farboe','start_pp',start_pp, p_log_level_rec => p_log_level_rec);
4728              fa_debug_pkg.add('farboe','h_start_pd_endpp',h_start_pd_endpp, p_log_level_rec => p_log_level_rec);
4729           end if;
4730 
4731           /* Bug#4347020 */
4732           h_amt_to_retain :=
4733                 ((h_start_pd_deprn - nvl( -1 * h_adj_exp_row,0))
4734                           * ((h_ret_pp - start_pp) /
4735                             ((h_start_pd_endpp - start_pp) + 1)));
4736 
4737           h_amt_to_retain := h_amt_to_retain + (-1 * h_adj_exp_row);
4738 
4739 
4740           if not FA_UTILS_PKG.faxrnd(h_start_pd_deprn, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
4741              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4742              raise farboe_err;
4743           end if;
4744 
4745           if p_log_level_rec.statement_level then
4746              fa_debug_pkg.add('farboe','h_tot_deprn - Before', h_tot_deprn, p_log_level_rec => p_log_level_rec);
4747              fa_debug_pkg.add('farboe','h_amt_to_retain - Before', h_amt_to_retain, p_log_level_rec => p_log_level_rec);
4748           end if;
4749 
4750           h_tot_deprn := h_tot_deprn - h_amt_to_retain;
4751 
4752           if p_log_level_rec.statement_level then
4753              fa_debug_pkg.add('farboe','h_start_pd_deprn',h_start_pd_deprn, p_log_level_rec => p_log_level_rec);
4754              fa_debug_pkg.add('farboe','OLD: h_tot_deprn',h_tot_deprn, p_log_level_rec => p_log_level_rec);
4755           end if;
4756 
4757           --------------------- NEW APPROACH for Bug#5074257
4758           -- We can remove the logic above after some more verifications.
4759 
4760           if p_log_level_rec.statement_level then
4761                fa_debug_pkg.add('farboe','++ 365 CATCHUP: ret.th_id_in', ret.th_id_in, p_log_level_rec => p_log_level_rec);
4762                fa_debug_pkg.add('farboe','++ 365 CATCHUP: deprn_start_pnum', deprn_start_pnum, p_log_level_rec => p_log_level_rec);
4763                fa_debug_pkg.add('farboe','++ 365 CATCHUP: deprn_start_fy', deprn_start_fy, p_log_level_rec => p_log_level_rec);
4764           end if;
4765 
4766           if (not FA_GAINLOSS_DPR_PKG.CALC_CATCHUP(
4767                                  ret                  => ret,
4768                                  BK                   => bk,
4769                                  DPR                  => dpr,
4770                                  calc_catchup         => TRUE, -- (start_pd < cpdnum),
4771                                  x_deprn_exp          => l_deprn_exp,
4772                                  x_bonus_deprn_exp    => l_bonus_deprn_exp,
4773                                  x_impairment_exp     => l_impairment_exp,
4774                                  x_asset_fin_rec_new  => l_asset_fin_rec_new,
4775                                  p_log_level_rec      => p_log_level_rec)) then
4776 
4777                        fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
4778                        return(FALSE);
4779 
4780            end if;
4781 
4782            -- Bug 9930721 : When farboe is called for Bonus, use l_bonus_deprn_exp
4783            if adj_type = 'BONUS EXPENSE' then
4784               l_deprn_exp := l_bonus_deprn_exp;
4785            end if;
4786 
4787            --Bug#14111772:When retirement prorated to poa
4788            if h_same_fy  <> 1 then
4789               if mrc_sob_type_code <> 'R' then
4790                  begin
4791                     select  1
4792                     into l_poa_ret
4793                     from fa_deprn_summary ds,
4794                     fa_deprn_periods dp
4795                     where ds.book_type_code = h_book
4796                     and ds.asset_id = h_asset_id
4797                     and ds.deprn_source_code = 'BOOKS'
4798                     and dp.book_type_code = h_book
4799                     and dp.fiscal_year = h_current_fiscal_yr
4800                     and dp.period_counter = ds.period_counter + 1
4801                     and bk.ret_prorate_date between dp.calendar_period_open_date and dp.calendar_period_close_date;
4802                  exception
4803                  when no_data_found then
4804                     l_poa_ret := 0;
4805                  end;
4806               else
4807                  begin
4808                     select  1
4809                     into l_poa_ret
4810                     from fa_mc_deprn_summary ds,
4811                     fa_mc_deprn_periods dp
4812                     where ds.book_type_code = h_book
4813                     and ds.set_of_books_id = ret.set_of_books_id
4814                     and ds.asset_id = h_asset_id
4815                     and ds.deprn_source_code = 'BOOKS'
4816                     and dp.book_type_code = h_book
4817                     and dp.set_of_books_id = ret.set_of_books_id
4818                     and dp.fiscal_year = h_current_fiscal_yr
4819                     and dp.period_counter = ds.period_counter + 1
4820                     and bk.ret_prorate_date between dp.calendar_period_open_date and dp.calendar_period_close_date;
4821                  exception
4822                  when no_data_found then
4823                     l_poa_ret := 0;
4824                  end;
4825               end if;
4826            end if;
4827 
4828           -- Bug 5362790
4829           -- Bug 5652883 (Removed '=' from if clause)
4830           -- if deprn_start_pnum >= start_pp and h_same_fy=1  then
4831            if deprn_start_pnum > start_pp and h_same_fy=1  then
4832               h_tot_deprn := l_deprn_exp;
4833 
4834            else
4835 --              h_tot_deprn := l_deprn_exp * abs(pds_catchup) / (end_pp - start_pp);
4836 
4837           -- Fix for Bug #5844937/5851102/5870503.  To prevent divisor by
4838           -- zero error, set the h_tot_deprn to 0 when it should have
4839           -- calculated to 0.
4840           if (l_deprn_exp = 0) or (pds_catchup = 0) or (end_pp = start_pp) then
4841 
4842             h_tot_deprn := 0;
4843           --Bug#14111772: Should not consider catchup calculated in poa for prior-dated assets.
4844           elsif h_same_fy  <> 1 and l_poa_ret = 1  then
4845              h_amt_to_retain :=  h_start_pd_deprn * ((h_ret_pp - start_pp) /
4846                                   ((h_start_pd_endpp - start_pp) + 1));
4847 
4848              if p_log_level_rec.statement_level then
4849                 fa_debug_pkg.add('farboe','h_amt_to_retain ', h_amt_to_retain,p_log_level_rec => p_log_level_rec);
4850              end if;
4851              h_tot_deprn := l_deprn_exp - h_amt_to_retain;
4852           elsif bk.fully_reserved then
4853 
4854             --h_tot_deprn := l_deprn_exp * abs(pds_catchup) / (end_pp - start_pp);
4855 			--Bug#14111772:FP fix done for bug#8405763
4856                 h_amt_to_retain :=
4857                       ((h_start_pd_deprn - nvl( -1 * h_adj_exp_row,0))
4858                                 * ((h_ret_pp - start_pp) /
4859                                   ((h_start_pd_endpp - start_pp) + 1)));
4860 
4861                 h_amt_to_retain := h_amt_to_retain + (-1 * h_adj_exp_row);
4862 
4863                 if p_log_level_rec.statement_level then
4864                    fa_debug_pkg.add('farboe','h_tot_deprn - Before', h_tot_deprn,p_log_level_rec => p_log_level_rec);
4865                    fa_debug_pkg.add('farboe','h_amt_to_retain - Before', h_amt_to_retain,p_log_level_rec => p_log_level_rec);
4866                 end if;
4867 
4868                 h_tot_deprn := l_deprn_exp - h_amt_to_retain;
4869 		   else
4870 			    h_tot_deprn := l_deprn_exp * abs(pds_catchup) / (end_pp - start_pp);
4871 
4872 				if p_log_level_rec.statement_level then
4873                    fa_debug_pkg.add('farboe','l_deprn_exp ', l_deprn_exp,p_log_level_rec => p_log_level_rec);
4874 				   fa_debug_pkg.add('farboe','h_tot_deprn - after', h_tot_deprn,p_log_level_rec => p_log_level_rec);
4875                 end if;
4876            end if;
4877 
4878            end if;
4879         end if;
4880 
4881           if p_log_level_rec.statement_level then
4882             fa_debug_pkg.add('farboe','++ 365 CATCHUP: h_same_fy', h_same_fy, p_log_level_rec => p_log_level_rec);
4883             fa_debug_pkg.add('farboe','++ 365 CATCHUP: l_deprn_exp', l_deprn_exp, p_log_level_rec => p_log_level_rec);
4884             fa_debug_pkg.add('farboe','++ 365 CATCHUP: pds_catchup', pds_catchup, p_log_level_rec => p_log_level_rec);
4885             fa_debug_pkg.add('farboe','++ 365 CATCHUP: start_pp', start_pp, p_log_level_rec => p_log_level_rec);
4886             fa_debug_pkg.add('farboe','++ 365 CATCHUP: deprn_start_pnum', deprn_start_pnum, p_log_level_rec => p_log_level_rec);
4887             fa_debug_pkg.add('farboe','++ 365 CATCHUP: end_pp', end_pp, p_log_level_rec => p_log_level_rec);
4888             fa_debug_pkg.add('farboe','++ 365 CATCHUP: h_tot_deprn', h_tot_deprn, p_log_level_rec => p_log_level_rec);
4889         end if;
4890 
4891        ---------------------
4892 
4893 
4894     -- Bug# 5018194: elsif p_pers_per_yr = 365 and h_start_pdnum = h_end_pdnum - 1 and
4895     --    bk.pc_fully_reserved = h_start_pd_pc then
4896     elsif p_pers_per_yr = 365 and h_start_pdnum = h_end_pdnum - 1 and
4897           nvl(bk.pc_fully_reserved,-1) = h_start_pd_pc then
4898 
4899        if p_log_level_rec.statement_level then
4900           fa_debug_pkg.add('farboe','365 prorate and pcfr',h_start_pd_deprn, p_log_level_rec => p_log_level_rec);
4901        end if;
4902        h_dpr_temp := dpr;
4903        h_dpr_temp.rsv_known_flag := TRUE;
4904        h_dpr_temp.deprn_rsv := 0;
4905        h_dpr_temp.reval_rsv := 0;
4906        h_dpr_temp.prior_fy_exp := 0;
4907        h_dpr_temp.ytd_deprn := 0;
4908        h_dpr_temp.bonus_deprn_rsv := 0;
4909        h_dpr_temp.bonus_ytd_deprn := 0;
4910        h_dpr_temp.impairment_rsv := 0;
4911        h_dpr_temp.ytd_impairment := 0;
4912        h_dpr_temp.prior_fy_bonus_exp := 0;
4913        h_dpr_temp.jdate_retired :=  0;
4914        h_dpr_temp.ret_prorate_jdate := to_char(bk.ret_prorate_date,'J');
4915 
4916        if not FA_GAINLOSS_DPR_PKG.fagcdp(h_dpr_temp, deprn_amt,
4917                                 bonus_deprn_amt,
4918                                 impairment_amt,
4919                                 reval_deprn_amt,
4920                                 reval_amort, bk.deprn_start_date,
4921                                 bk.d_cal, bk.p_cal, h_start_pdnum,
4922                                 h_start_pdnum,
4923                                 bk.prorate_fy, bk.dsd_fy, bk.prorate_jdate,
4924                                 bk.deprn_start_jdate, p_log_level_rec => p_log_level_rec) then
4925 
4926                 fa_srvr_msg.add_message(
4927                   calling_fn => l_calling_fn,
4928                   name       => 'FA_RET_GENERIC_ERROR',
4929                   token1     => 'MODULE',
4930                   value1     => 'FAGCDP',
4931                   token2     => 'INFO',
4932                   value2     => 'depreciation number',
4933                   token3     => 'ASSET',
4934                   value3     => ret.asset_number , p_log_level_rec => p_log_level_rec);
4935 
4936                 return(FALSE);
4937 
4938        end if;
4939 
4940        if p_log_level_rec.statement_level then
4941           fa_debug_pkg.add('farboe','after fagcdp deprn_amt',deprn_amt, p_log_level_rec => p_log_level_rec);
4942        end if;
4943 -- bug fix 5716178
4944        if(deprn_amt <> 0)then
4945           h_temp_end_pp := (h_tot_deprn * ((h_start_pd_endpp - start_pp) + 1)) /
4946                             deprn_amt;
4947           h_temp_end_pp := -1 * h_temp_end_pp;
4948        else
4949           h_temp_end_pp := 0;
4950        end if;
4951        if not FA_UTILS_PKG.faxceil(h_temp_end_pp,
4952                                    ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
4953          raise farboe_err;
4954        end if;
4955 
4956        if p_log_level_rec.statement_level then
4957           fa_debug_pkg.add('farboe','after faxceil',h_temp_end_pp, p_log_level_rec => p_log_level_rec);
4958        end if;
4959 
4960        h_ret_pjdate := to_char(bk.ret_prorate_date, 'J');
4961        if p_log_level_rec.statement_level then
4962           fa_debug_pkg.add('farboe','h_ret_pjdate',h_ret_pjdate, p_log_level_rec => p_log_level_rec);
4963        end if;
4964 
4965        -- Get the depreciation start prorate period
4966        if not fa_cache_pkg.fazccp(p_cal, fiscal_year_name, h_ret_pjdate,
4967                          h_ret_pp, dummy_num2, dummy_num, p_log_level_rec => p_log_level_rec) then
4968              raise farboe_err;
4969        end if;
4970 
4971        /* Bug#10329414: Checking if ret_prorate_pc falls in poa */
4972        begin
4973           if mrc_sob_type_code <> 'R' then
4974              select period_counter + 1
4975              into h_asset_addition_pc
4976              from fa_deprn_summary
4977              where asset_id = h_asset_id
4978              and book_type_code = h_book
4979              and deprn_source_code = 'BOOKS';
4980 	  else
4981              select period_counter + 1
4982              into h_asset_addition_pc
4983              from fa_mc_deprn_summary
4984              where asset_id = h_asset_id
4985              and book_type_code = h_book
4986              and set_of_books_id = ret.set_of_books_id
4987              and deprn_source_code = 'BOOKS';
4988 	  end if;
4989        exception
4990           when no_data_found then null;
4991        end;
4992 
4993        if ( (h_ret_pp - start_pp < h_temp_end_pp) and (h_temp_end_pp <> 0)) then
4994           /* Bug#10329414: Modified amt_to_retain if ret_prorate_pc falls in poa */
4995           if h_asset_addition_pc = h_start_pd_pc then
4996 	     h_amt_to_retain := h_tot_deprn * (((h_start_pd_endpp - start_pp) + 1) /
4997                                           h_temp_end_pp);
4998           else
4999 	     h_amt_to_retain := h_tot_deprn * ((h_ret_pp - start_pp) /
5000                                           h_temp_end_pp);
5001           end if;
5002        else h_amt_to_retain := h_tot_deprn;
5003        end if;
5004 
5005        if not FA_UTILS_PKG.faxrnd(h_amt_to_retain, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
5006           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5007           raise farboe_err;
5008        end if;
5009 
5010        h_tot_deprn := h_tot_deprn - h_amt_to_retain;
5011 
5012     end if; -- 365
5013 
5014        adj_row.adjustment_amount := nvl(h_tot_deprn,0) - nvl(h_temp_deprn_tot,0); --bug fix 3558253 and 3518604 start
5015 
5016        if p_log_level_rec.statement_level then
5017           fa_debug_pkg.add('farboe','h_tot_deprn final',h_tot_deprn, p_log_level_rec => p_log_level_rec);
5018        end if;
5019 
5020 --bugfix 4380845
5021          -- Call faxrnd
5022           if not FA_UTILS_PKG.faxrnd(adj_row.adjustment_amount,book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
5023               fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5024               RAISE farboe_err;
5025            end if;
5026 
5027        -- if bonus expense amount is zero, skip the following and return
5028        if (adj_row.adjustment_type = 'BONUS EXPENSE' and
5029                         adj_row.adjustment_amount = 0 ) then
5030            return TRUE;
5031        end if;
5032 
5033        -- if impair expense amount is zero, skip the following and return
5034        if (adj_row.adjustment_type = 'IMPAIR EXPENSE' and
5035                         adj_row.adjustment_amount = 0 ) then
5036            return TRUE;
5037        end if;
5038 
5039        -- Fix for Bug#2676794
5040        if (adj_row.adjustment_type = 'REVAL EXPENSE' and
5041                 (adj_row.adjustment_amount = 0 or adj_row.adjustment_amount is null)) then
5042            return TRUE;
5043        end if;
5044        if (adj_row.adjustment_type = 'REVAL AMORT' and
5045                 (adj_row.adjustment_amount = 0 or adj_row.adjustment_amount is null)) then
5046            return TRUE;
5047        end if;
5048 
5049 
5050        if (units_retired is null or units_retired <= 0) then
5051                                                 -- partial cost retirement
5052 
5053           adj_row.selection_retid := 0;
5054           adj_row.units_retired := 0;
5055           adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
5056           adj_row.mrc_sob_type_code := mrc_sob_type_code;
5057           adj_row.set_of_books_id := ret.set_of_books_id;
5058 
5059           if (bk.book_class) then
5060              if NOT faginfo(
5061                             RET, BK, cpd_ctr,today, -1
5062                            ,calling_module => l_calling_fn
5063                            ,candidate_mode => 'RETIRE'
5064                            ,set_adj_row => TRUE
5065                            ,unit_ret_in_corp => l_unit_ret_in_corp
5066                            ,ret_id_in_corp => l_ret_id_in_corp
5067                            ,th_id_out_in_corp => h_id_out
5068                            ,balance_tfr_in_tax => l_balance_tfr_in_tax
5069                            ,adj_row => adj_row
5070                            ,p_log_level_rec => p_log_level_rec
5071                                        ) then
5072                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5073                 return(FALSE);
5074              end if;
5075           end if;
5076 
5077 
5078           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5079                                        X_last_update_date,
5080                                        X_last_updated_by,
5081                                        X_last_update_login,
5082                                        p_log_level_rec => p_log_level_rec)) then
5083                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5084              return(FALSE);
5085 
5086           end if;
5087 
5088        else  -- partial unit retirement
5089 
5090           adj_row.selection_retid := retirement_id;
5091           adj_row.units_retired := units_retired;
5092           adj_row.selection_mode := FA_STD_TYPES.FA_AJ_RETIRE;
5093 
5094           if p_log_level_rec.statement_level then
5095              fa_debug_pkg.add
5096                (fname   => l_calling_fn,
5097                 element => 'dist and deprn',
5098                 value   => '', p_log_level_rec => p_log_level_rec);
5099           end if;
5100 
5101           adj_row.mrc_sob_type_code := mrc_sob_type_code;
5102           adj_row.set_of_books_id := ret.set_of_books_id;
5103 
5104           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5105                                        X_last_update_date,
5106                                        X_last_updated_by,
5107                                        X_last_update_login
5108                                        , p_log_level_rec => p_log_level_rec)) then
5109              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5110              return(FALSE);
5111 
5112           end if;
5113 
5114        end if;
5115 
5116        /* Bug 6666666 : Neutralizing entries for SORP */
5117        if FA_CACHE_PKG.fazcbc_record.sorp_enabled_flag = 'Y'
5118             and adj_row.adjustment_type = 'EXPENSE' then
5119             if not FA_SORP_UTIL_PVT.create_sorp_neutral_acct (
5120                     p_amount                => adj_row.adjustment_amount,
5121                     p_reversal              => 'N',
5122                     p_adj                   => adj_row,
5123                     p_created_by            => NULL,
5124                     p_creation_date         => NULL,
5125                     p_last_update_date      => X_last_update_date,
5126                     p_last_updated_by       => X_last_updated_by,
5127                     p_last_update_login     => X_last_update_login,
5128                     p_who_mode              => 'UPDATE',
5129                     p_log_level_rec => p_log_level_rec) then
5130                        fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5131                        return(FALSE);
5132             end if;
5133        end if;
5134        /* End of Bug 6666666 */
5135 
5136        return(TRUE);
5137 
5138     EXCEPTION
5139 
5140        when others then
5141 
5142             fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5143             return FALSE;
5144 
5145     END FARBOE;
5146 
5147 /*==========================================================================*
5148 | NAME          fagpdp                                                      |
5149 |                                                                           |
5150 | FUNCTION                                                                  |
5151 |                                                                           |
5152 |       This function figures out how much depreciation expense needs to be |
5153 | allocated to each cost center (distribution). It inserts the amount into  |
5154 | the FA_ADJUSTMENTS table.                                                 |
5155 |       If the number of periods to be catchup is negative or the           |
5156 | DEPRECIATE_LAST_YEAR_FLAG is set to 'NO', we need to back out depreciation|
5157 | When the flag is set to NO, we need to back out the whole depreciation    |
5158 | taken so far this year.                                                   |
5159 |                                                                           |
5160 |                                                                           |
5161 | HISTORY       1/12/89         R Rumanang      Created                     |
5162 |               6/23/89         R Rumanang      Standarized                 |
5163 |               8/24/89         R Rumanang      Insert to FA_ADJUSTMENTS    |
5164 |               04/15/91        M Chan          Rewritten for MPL 9         |
5165 |               01/02/96        S Behura        Rewritten into PL/SQL       |
5166 *===========================================================================*/
5167 
5168 FUNCTION fagpdp(ret in out nocopy fa_ret_types.ret_struct,
5169                 bk in out nocopy fa_ret_types.book_struct,
5170                 dpr in out nocopy FA_STD_TYPES.dpr_struct,
5171                 today in date, pds_catchup number,
5172                 cpd_ctr number, cpdnum number,
5173                 cost_frac in number, deprn_amt in out nocopy number,
5174                 bonus_deprn_amt in out nocopy number,
5175                 impairment_amt in out nocopy number,
5176                 impairment_reserve in out nocopy number,
5177                 reval_deprn_amt in out nocopy number, reval_amort in out number,
5178                 reval_reserve in out nocopy number, user_id number,
5179                 p_log_level_rec in FA_API_TYPES.log_level_rec_type) Return BOOLEAN IS
5180 
5181     fagpdp_err          exception;
5182     dummy               number;
5183     fy_name             varchar2(30);
5184     deprn_start_pnum    number;
5185     deprn_start_fy      number;
5186     -- adj_row             FA_STD_TYPES.fa_adj_row_struct;
5187     adj_row             FA_ADJUST_TYPE_PKG.fa_adj_row_struct;
5188 
5189     h_work_pdnum        number;
5190     h_stop_pdnum        number;
5191     h_ret_p_date        date;
5192     h_d_cal             varchar2(16);
5193     h_asset_id          number(15);
5194     h_th_id_in          number(15);
5195     h_book              varchar2(30);
5196     h_cpd_ctr           number(15);
5197     h_deprn_amt         number;
5198     h_bonus_deprn_amt   number;
5199     h_impairment_amt    number;
5200     h_reval_deprn_amt   number;
5201     h_reval_amort       number;
5202     h_deprn_exp_acct    varchar2(30);
5203     h_bonus_deprn_exp_acct varchar2(30);
5204     h_impair_exp_acct   varchar2(30);
5205 
5206     h_dpis_pr_jdt       number;
5207         /* new variables for retirements to handle different
5208            prorate calendars  */
5209 
5210     h_cpp_jstartdate    number;
5211 
5212 
5213     h_cpp_jenddate      number;
5214     h_startpp           number;
5215     h_endpp             number;
5216     h_current_fiscal_yr integer;
5217     h_p_cal             varchar2(30);
5218     h_fy_name           varchar2(30);
5219     l_first_fiscal_year integer;
5220 
5221     h_id_out              number;
5222     l_balance_tfr_in_tax  number;
5223     l_unit_ret_in_corp    boolean;
5224     l_ret_id_in_corp    number;
5225 
5226 
5227     X_LAST_UPDATE_DATE date := sysdate;
5228     X_last_updated_by number := -1;
5229     X_last_update_login number := -1;
5230     l_decision_flag     BOOLEAN; -- Bug# 6920756
5231     l_calling_fn        varchar2(40) := 'FA_GAINLOSS_UPD_PKG.fagpdp';
5232 
5233     BEGIN <<FAGPDP>>
5234 
5235       if p_log_level_rec.statement_level then
5236          fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 1', '', p_log_level_rec => p_log_level_rec);
5237          fa_debug_pkg.add(l_calling_fn, 'pds_catchup=', pds_catchup, p_log_level_rec => p_log_level_rec);
5238       end if;
5239 
5240        h_book := ret.book;
5241        h_asset_id := ret.asset_id;
5242        h_th_id_in := ret.th_id_in;
5243        h_cpd_ctr := cpd_ctr;
5244        h_ret_p_date := bk.ret_prorate_date;
5245        h_d_cal := bk.d_cal;
5246        h_p_cal := bk.p_cal;
5247        h_current_fiscal_yr := bk.cpd_fiscal_year;
5248        h_startpp := 0;
5249        h_endpp := 0;
5250 
5251        -- Bug#4867806: if (pds_catchup = 0) and (bk.depreciate_lastyr) then
5252        if (pds_catchup = 0 and bk.depr_first_year_ret = 1) and (bk.depreciate_lastyr) then
5253           return(TRUE);
5254        end if;
5255 
5256        if p_log_level_rec.statement_level then
5257             fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 3', '', p_log_level_rec => p_log_level_rec);
5258        end if;
5259 
5260        begin
5261              select fy.fiscal_year
5262              into   l_first_fiscal_year
5263              from   fa_fiscal_year fy,
5264                     fa_book_controls bc,
5265                     fa_books bks
5266              where  bc.book_type_code = ret.book
5267              and    bc.fiscal_year_name = fy.fiscal_year_name
5268              and    bks.book_type_code = ret.book
5269              and    bks.asset_id = ret.asset_id
5270              and    bks.transaction_header_id_out is null
5271              and    least(bks.date_placed_in_service,bks.prorate_date) between
5272                     fy.start_date and fy.end_date;
5273        exception
5274           when others then
5275              -- We're going to assume that if there are errors, the asset is
5276              -- too old for the calendar, so we'll just set the first year to 0
5277              l_first_fiscal_year := 0;
5278        end;
5279 
5280 
5281     /* when depreciate_lastyr is FALSE, then we need to back out the whole
5282        depreaciation taken that year.  Period Num should be the greater
5283        of the first period this fiscal year or the first 'DEPRN' row.
5284     */
5285        -- Bug#4867806: if (pds_catchup <= 0) or (not bk.depreciate_lastyr) then
5286        if (pds_catchup <= 0) or
5287           (not bk.depreciate_lastyr)  or
5288           ( (bk.depr_first_year_ret = 0) and (h_current_fiscal_yr = l_first_fiscal_year) ) then
5289 
5290           if p_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 3', '', p_log_level_rec => p_log_level_rec); end if;
5291           h_stop_pdnum := cpdnum;
5292 
5293           /* Determine the period number to start marching forward */
5294 
5295           if not fa_cache_pkg.fazcbc(ret.book, p_log_level_rec => p_log_level_rec) then
5296              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5297              raise fagpdp_err;
5298           end if;
5299 
5300           fy_name := fa_cache_pkg.fazcbc_record.fiscal_year_name;
5301 
5302           if not fa_cache_pkg.fazccp(bk.d_cal, fy_name,
5303                                      bk.deprn_start_jdate,
5304                                      deprn_start_pnum, deprn_start_fy, dummy, p_log_level_rec => p_log_level_rec) then
5305              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5306              raise fagpdp_err;
5307           end if;
5308 
5309           if p_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 3.1', '', p_log_level_rec => p_log_level_rec); end if;
5310 
5311           if (p_log_level_rec.statement_level) then
5312              fa_debug_pkg.add('fagpdp','bk.depreciate_lastyr',bk.depreciate_lastyr, p_log_level_rec => p_log_level_rec);
5313              fa_debug_pkg.add('fagpdp','bk.depr_first_year_ret',bk.depr_first_year_ret, p_log_level_rec => p_log_level_rec);
5314           end if;
5315 
5316           -- Bug#4867806: if not bk.depreciate_lastyr then
5317           -- Bug#8665405: Checking if it is in first fiscal year
5318           if (not bk.depreciate_lastyr) or ((bk.depr_first_year_ret = 0) and (h_current_fiscal_yr = l_first_fiscal_year)) then
5319 
5320              if p_log_level_rec.statement_level then
5321                   fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 4', '', p_log_level_rec => p_log_level_rec);
5322              end if;
5323 
5324              if (deprn_start_fy * bk.pers_per_yr + deprn_start_pnum) >=
5325                 (bk.cpd_fiscal_year * bk.pers_per_yr + 1) then
5326 
5327                 h_work_pdnum := deprn_start_pnum;
5328 
5329                if (p_log_level_rec.statement_level) then
5330                     fa_debug_pkg.add('fagpdp(1.1)','h_work_pdnum',h_work_pdnum);
5331                end if;
5332 
5333              else
5334                h_work_pdnum := 1;
5335 
5336                if (p_log_level_rec.statement_level) then
5337                     fa_debug_pkg.add('fagpdp(1.2)','h_work_pdnum',h_work_pdnum);
5338                end if;
5339 
5340              end if;
5341 
5342              if p_log_level_rec.statement_level then
5343                  fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 4.1', '', p_log_level_rec => p_log_level_rec);
5344              end if;
5345 
5346           else
5347              /* get the depreciation period in which the retirement
5348                prorate date falls into */
5349              if p_log_level_rec.statement_level then
5350                 fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 5', '', p_log_level_rec => p_log_level_rec);
5351                 fa_debug_pkg.add(l_calling_fn, 'h_d_cal', h_d_cal, p_log_level_rec => p_log_level_rec);
5352                 fa_debug_pkg.add(l_calling_fn, 'h_ret_p_date', h_ret_p_date, p_log_level_rec => p_log_level_rec);
5353              end if;
5354 
5355              begin
5356                 SELECT   cp.period_num
5357                   INTO   h_work_pdnum
5358                   FROM   fa_calendar_periods cp
5359                  WHERE   h_ret_p_date
5360                          between cp.start_date and cp.end_date
5361                    AND   cp.calendar_type = h_d_cal;
5362                 EXCEPTION
5363                    when no_data_found then
5364                       raise fagpdp_err;
5365              end; -- end of - select
5366 
5367              if p_log_level_rec.statement_level then
5368                  fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 5.1.1', '', p_log_level_rec => p_log_level_rec);
5369              end if;
5370 
5371              if (deprn_start_fy * bk.pers_per_yr + deprn_start_pnum) >=
5372                 (bk.cpd_fiscal_year * bk.pers_per_yr + h_work_pdnum) then
5373 
5374                 h_work_pdnum := deprn_start_pnum;
5375 
5376              end if;
5377 
5378              if (p_log_level_rec.statement_level) then
5379                  fa_debug_pkg.add('fagpdp(1.3)','h_work_pdnum',h_work_pdnum);
5380              end if;
5381 
5382           end if;
5383 
5384           if p_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 3.2', '', p_log_level_rec => p_log_level_rec); end if;
5385 
5386             /* get the first prorate prorate period in h_work_pdnum
5387                and the last prorate period in h_stop_pdnum to obtain
5388                the total number of prorate periods in the periods that
5389                we are backing out. In farboe we will use prorate
5390                period catchup / total prorate periods to backout the
5391                correct fraction of expense */
5392 
5393 /*
5394   BUG# 780910: commentting this out so that the start date of the
5395                is obtained from the calendars table rather than
5396                from deprn_periods.  In cases where the implementation
5397                was done in the middle of the year, the prorate period
5398                may not have been opened (i.e. half-year)  --bridgway 08/23/00
5399 
5400             EXEC SQL
5401                 SELECT  to_number (to_char (dp.calendar_period_open_date, 'J'))
5402                 INTO    :h_cpp_jstartdate
5403                 FROM    fa_deprn_periods dp
5404                 WHERE   dp.book_type_code = :h_book
5405                 AND     dp.fiscal_year = :h_current_fiscal_yr
5406                 AND     dp.period_num = :h_work_pdnum;
5407 */
5408 
5409                 SELECT  to_number (to_char (dcp.start_date, 'J'))
5410                 INTO    h_cpp_jstartdate
5411                 FROM    fa_calendar_periods dcp,
5412                         fa_fiscal_year fy,
5413                         fa_book_controls bc
5414                 WHERE   bc.book_type_code = h_book
5415                 AND     bc.fiscal_year_name = fy.fiscal_year_name
5416                 AND     dcp.calendar_type   = h_d_cal
5417                 AND     fy.fiscal_year      = h_current_fiscal_yr
5418                 AND     dcp.period_num      = h_work_pdnum
5419                 AND     dcp.start_date
5420                              between fy.start_date and fy.end_date;
5421 
5422 
5423                 /* Bug fix 5652883 In case retrement prorate date lies in the
5424                 same period (as per deprn calendar) as of DPIS prorate date,
5425                 consider greatest of DPIS prorate date and calendar period
5426                 start date of retirement prorate date for calculating start_pp,
5427                 otherwise consider calendar period start date of retirement
5428                 prorate date as was happening earlier */
5429 
5430                 BEGIN
5431                     SELECT to_number (to_char (bk.prorate_date, 'J'))
5432                     INTO   h_dpis_pr_jdt
5433                     FROM   fa_books bk,
5434                            fa_calendar_periods dcp
5435                     WHERE  bk.book_type_code = ret.book
5436                     AND    transaction_header_id_out is null
5437                     AND    asset_id = ret.asset_id
5438                     AND    dcp.calendar_type   = h_d_cal
5439                     AND    bk.prorate_date
5440                            BETWEEN dcp.start_date AND dcp.end_date
5441                     AND   h_ret_p_date
5442                            BETWEEN dcp.start_date AND dcp.end_date;
5443                 EXCEPTION
5444                     WHEN no_data_found then
5445                         h_dpis_pr_jdt := null;
5446                 END;
5447 
5448                 h_cpp_jstartdate := greatest(h_cpp_jstartdate, nvl(h_dpis_pr_jdt, h_cpp_jstartdate));
5449 
5450                 -- Bug fix 5652883 ends here
5451 
5452 
5453                 SELECT  period_num
5454                 INTO    h_startpp
5455                 FROM    fa_calendar_periods
5456                 WHERE   calendar_type = h_p_cal
5457                 AND     to_date (h_cpp_jstartdate,'J')
5458                         between start_date and end_date;
5459 
5460                 SELECT  to_number (to_char (dp.calendar_period_open_date, 'J'))
5461                 INTO    h_cpp_jenddate
5462                 FROM    fa_deprn_periods dp
5463                 WHERE   dp.book_type_code = h_book
5464                 AND     dp.fiscal_year = h_current_fiscal_yr
5465                 AND     dp.period_num = h_stop_pdnum;
5466 
5467                 SELECT  period_num
5468                 INTO    h_endpp
5469                 FROM    fa_calendar_periods
5470                 WHERE   calendar_type = h_p_cal
5471                 AND     to_date (h_cpp_jenddate,'J')
5472                         between start_date and end_date;
5473 
5474 
5475         /* The amount to back-off is caculated as follows:
5476            (cost_retired/Cost for the period we're backing out) *
5477            [deprn_amount - (adjustment_amount - counter *
5478                             adjustment per period)]
5479            Counter is the difference between the period we're backing out and
5480            the prorate-retirement period. However, if the Absolute value of the
5481            counter * adj per period is greater than the adjustment amount,
5482            then use deprn_amount only.
5483            If the adjustment records were put up by Retirement program, then
5484            don't use the "counter" part (the adjustment_per_period is 0)
5485         */
5486 
5487           if p_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling farboe', '', p_log_level_rec => p_log_level_rec); end if;
5488 
5489           if (p_log_level_rec.statement_level) then
5490                  fa_debug_pkg.add('fagpdp(2)','before calling FARBOE','');
5491           end if;
5492 
5493           if not farboe(ret.asset_id, ret.book, bk.cpd_fiscal_year,
5494                         cost_frac, h_work_pdnum, h_stop_pdnum, 'EXPENSE',
5495                         bk.pers_per_yr, ret.dpr_evenly, bk.fiscal_year_name,
5496                         ret.units_retired, ret.th_id_in, cpd_ctr, today,
5497                         bk.cur_units,ret.retirement_id, bk.d_cal, dpr,
5498                         bk.p_cal, pds_catchup, bk.depreciate_lastyr,
5499                         h_startpp, h_endpp,
5500                         ret.mrc_sob_type_code,
5501                         ret,bk,
5502                         p_log_level_rec) then
5503 
5504              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5505              raise fagpdp_err;
5506 
5507           end if;
5508 
5509           /* BUG# 1646713
5510            making bonus to farboe for bonus expense conditional on whether
5511            a bonus rule has been linked to the asset. Note that once a bonus
5512            rule has been assigned, it can't be removed, it could only assigned
5513            to a 0% rule which would back out expense.
5514 
5515               -- bridgway   02/27/01
5516            */
5517           if (nvl(bk.bonus_rule,'NONE') <> 'NONE') then
5518 
5519              if not farboe(ret.asset_id, ret.book, bk.cpd_fiscal_year,
5520                            cost_frac, h_work_pdnum, h_stop_pdnum,
5521                            'BONUS EXPENSE', bk.pers_per_yr,
5522                            ret.dpr_evenly, bk.fiscal_year_name,
5523                            ret.units_retired, ret.th_id_in, cpd_ctr,
5524                            today, bk.cur_units, ret.retirement_id,
5525                            bk.d_cal, dpr, bk.p_cal, pds_catchup,
5526                            bk.depreciate_lastyr,h_startpp, h_endpp,
5527                            ret.mrc_sob_type_code,
5528                            ret,bk,
5529                            p_log_level_rec) then
5530 
5531                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5532                 raise fagpdp_err;
5533 
5534              end if;
5535 
5536           end if;
5537 
5538           if (impairment_reserve <> 0) then
5539 
5540              if not farboe(ret.asset_id, ret.book, bk.cpd_fiscal_year,
5541                            cost_frac, h_work_pdnum, h_stop_pdnum,
5542                            ' EXPENSE', bk.pers_per_yr,
5543                            ret.dpr_evenly, bk.fiscal_year_name,
5544                            ret.units_retired, ret.th_id_in, cpd_ctr,
5545                            today, bk.cur_units, ret.retirement_id,
5546                            bk.d_cal, dpr, bk.p_cal, pds_catchup,
5547                            bk.depreciate_lastyr,h_startpp, h_endpp,
5548                            ret.mrc_sob_type_code,
5549                            ret,bk,
5550                            p_log_level_rec) then
5551 
5552                  -- Error in farboe for reval expense
5553                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5554                 raise fagpdp_err;
5555 
5556              end if;
5557           end if;
5558 
5559           if (bk.current_cost > bk.unrevalued_cost
5560              or reval_reserve <> 0) then
5561              if not farboe(ret.asset_id, ret.book, bk.cpd_fiscal_year,
5562                            cost_frac, h_work_pdnum, h_stop_pdnum,
5563                            'REVAL EXPENSE', bk.pers_per_yr,
5564                            ret.dpr_evenly, bk.fiscal_year_name,
5565                            ret.units_retired, ret.th_id_in, cpd_ctr,
5566                            today, bk.cur_units, ret.retirement_id,
5567                            bk.d_cal, dpr, bk.p_cal, pds_catchup,
5568                            bk.depreciate_lastyr,h_startpp, h_endpp,
5569                            ret.mrc_sob_type_code,
5570                            ret,bk,
5571                            p_log_level_rec) then
5572 
5573                  -- Error in farboe for reval expense
5574                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5575                 raise fagpdp_err;
5576 
5577              end if;
5578 
5579              if not farboe(ret.asset_id, ret.book, bk.cpd_fiscal_year,
5580                            cost_frac, h_work_pdnum, h_stop_pdnum,
5581                            'REVAL AMORT', bk.pers_per_yr, ret.dpr_evenly,
5582                            bk.fiscal_year_name, ret.units_retired,
5583                            ret.th_id_in, cpd_ctr, today, bk.cur_units,
5584                            ret.retirement_id, bk.d_cal, dpr,
5585                            bk.p_cal, pds_catchup, bk.depreciate_lastyr,
5586                            h_startpp, h_endpp,
5587                            ret.mrc_sob_type_code,
5588                            ret,bk,
5589                            p_log_level_rec) then
5590                 -- Error in farboe for reval amort
5591                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5592                 raise fagpdp_err;
5593 
5594              end if;
5595 
5596           end if; -- end of - if bk.current_cost
5597 
5598        else  -- This must be pds_catchup > 0
5599 
5600           if p_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 2', '', p_log_level_rec => p_log_level_rec); end if;
5601 
5602           -- Bug#6920756 Using l_decision_flag to judge as asset as fully reserved/ fully extended.
5603           -- Bug 8211842 : Check if asset has started extended depreciation
5604           if bk.extended_flag and bk.start_extended then
5605              l_decision_flag := bk.fully_extended;
5606           else
5607              l_decision_flag := bk.fully_reserved;
5608           end if;
5609 
5610           if not l_decision_flag then -- Bug#6920756
5611                if p_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 2.1', '', p_log_level_rec => p_log_level_rec); end if;
5612             /* BUG# 1400554
5613                populating the account seg for expense with the value
5614                in category books
5615                    -- bridgway 09/14/00
5616 
5617                adj_row.account[0] = '\0';
5618             */
5619 
5620                select  facb.deprn_expense_acct
5621                into    h_deprn_exp_acct
5622                from    fa_additions_b    faadd,
5623                        fa_category_books facb,
5624                        fa_book_controls bc
5625                where   faadd.asset_id = h_asset_id
5626                and     facb.category_id = faadd.asset_category_id
5627                and     facb.book_type_code = h_book
5628                and     bc.book_type_code = facb.book_type_code;
5629 
5630              if p_log_level_rec.statement_level then
5631                  fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 3', '', p_log_level_rec => p_log_level_rec);
5632              end if;
5633 
5634              adj_row.account := h_deprn_exp_acct;
5635 
5636              adj_row.transaction_header_id := ret.th_id_in;
5637              adj_row.asset_invoice_id := 0;
5638              adj_row.source_type_code := 'RETIREMENT';
5639              adj_row.book_type_code := ret.book;
5640              adj_row.period_counter_created := cpd_ctr;
5641              adj_row.asset_id := ret.asset_id;
5642              adj_row.period_counter_adjusted := cpd_ctr;
5643              adj_row.last_update_date := today;
5644              adj_row.account_type := 'DEPRN_EXPENSE_ACCT';
5645              adj_row.current_units := bk.cur_units;
5646              adj_row.selection_thid := 0;
5647              adj_row.flush_adj_flag := TRUE;
5648              adj_row.gen_ccid_flag := TRUE;
5649              adj_row.annualized_adjustment := 0;
5650              adj_row.code_combination_id := 0;
5651              adj_row.distribution_id := 0;
5652              adj_row.leveling_flag := TRUE;
5653 
5654               --
5655               -- bug3627497: Added following to prevent
5656               -- non tracked entry for member assets
5657               --
5658               if (bk.group_asset_id is not null) and
5659                  (nvl(bk.member_rollup_flag, 'N') = 'N') then
5660                 adj_row.track_member_flag := 'Y';
5661               else
5662                 adj_row.track_member_flag := null;
5663               end if;
5664 
5665              if (ret.units_retired is NULL or ret.units_retired <= 0) then
5666 
5667                 if p_log_level_rec.statement_level then
5668                      fa_debug_pkg.add
5669                         (l_calling_fn,
5670                          'in FAGPDP 4', '', p_log_level_rec => p_log_level_rec);
5671                      fa_debug_pkg.add
5672                         (fname   => l_calling_fn,
5673                          element => 'Insert cost into fa_adj',
5674                          value   => '', p_log_level_rec => p_log_level_rec);
5675                 end if;
5676 
5677                 adj_row.debit_credit_flag := 'DR';
5678                 adj_row.adjustment_type := 'EXPENSE';
5679                 adj_row.adjustment_amount := deprn_amt;
5680                 adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
5681                 adj_row.selection_retid := 0;
5682                 adj_row.units_retired := 0;
5683                 adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5684                 adj_row.set_of_books_id := ret.set_of_books_id;
5685 
5686 
5687                 if (bk.book_class) then
5688                    if NOT faginfo(
5689                             RET, BK, cpd_ctr,today, user_id
5690                            ,calling_module => l_calling_fn
5691                            ,candidate_mode => 'RETIRE'
5692                            ,set_adj_row => TRUE
5693                            ,unit_ret_in_corp => l_unit_ret_in_corp
5694                            ,ret_id_in_corp => l_ret_id_in_corp
5695                            ,th_id_out_in_corp => h_id_out
5696                            ,balance_tfr_in_tax => l_balance_tfr_in_tax
5697                            ,adj_row => adj_row
5698                            ,p_log_level_rec => p_log_level_rec) then
5699                      fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5700                      return(FALSE);
5701                    end if;
5702                 end if;
5703 
5704 
5705                 -- insert expense into fa_adjustments
5706                 if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5707                                        X_last_update_date,
5708                                        X_last_updated_by,
5709                                        X_last_update_login,
5710                                        p_log_level_rec => p_log_level_rec)) then
5711                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5712                    return(FALSE);
5713 
5714                 end if; -- end of - if not FA_INS_ADJUST_PKG.faxinaj
5715 
5716                 /* Bug 6666666 : Neutralizing entries for SORP */
5717                 if FA_CACHE_PKG.fazcbc_record.sorp_enabled_flag = 'Y' then
5718                     if not FA_SORP_UTIL_PVT.create_sorp_neutral_acct (
5719                             p_amount                => deprn_amt,
5720                             p_reversal              => 'N',
5721                             p_adj                   => adj_row,
5722                             p_created_by            => NULL,
5723                             p_creation_date         => NULL,
5724                             p_last_update_date      => X_last_update_date,
5725                             p_last_updated_by       => X_last_updated_by,
5726                             p_last_update_login     => X_last_update_login,
5727                             p_who_mode              => 'UPDATE',
5728                             p_log_level_rec         => p_log_level_rec) then
5729                             fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5730                             return(FALSE);
5731                     end if;
5732                 end if;
5733                 /* End of Bug 6666666 */
5734 
5735                 if bonus_deprn_amt <> 0 then
5736                    select nvl(cb.bonus_deprn_expense_acct,'0')
5737                    into h_bonus_deprn_exp_acct
5738                    from fa_additions_b ad,
5739                         fa_category_books cb
5740                    where ad.asset_id = h_asset_id
5741                    and   cb.category_id = ad.asset_category_id
5742                    and   cb.book_type_code = h_book;
5743 
5744                    adj_row.account := h_bonus_deprn_exp_acct;
5745                    adj_row.account_type := 'BONUS_DEPRN_EXPENSE_ACCT';
5746                    adj_row.debit_credit_flag := 'DR';
5747                    adj_row.adjustment_type := 'BONUS EXPENSE';
5748                    adj_row.adjustment_amount := bonus_deprn_amt;
5749                    adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
5750                    adj_row.selection_retid := 0;
5751                    adj_row.units_retired := 0;
5752                    adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5753                    adj_row.set_of_books_id := ret.set_of_books_id;
5754 
5755                    if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5756                                        X_last_update_date,
5757                                        X_last_updated_by,
5758                                        X_last_update_login
5759                                        , p_log_level_rec => p_log_level_rec)) then
5760 
5761                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5762                       return(FALSE);
5763 
5764                    end if; -- end of if not FA_INS_ADJUST_PKG.faxinaj
5765 
5766                 end if;
5767 
5768                 if impairment_amt <> 0 then
5769                    select nvl(cb.impair_expense_acct,'0')
5770                    into h_impair_exp_acct
5771                    from fa_additions_b ad,
5772                         fa_category_books cb
5773                    where ad.asset_id = h_asset_id
5774                    and   cb.category_id = ad.asset_category_id
5775                    and   cb.book_type_code = h_book;
5776 
5777                    adj_row.account := h_impair_exp_acct;
5778                    adj_row.account_type := 'IMPAIR_EXPENSE_ACCT';
5779                    adj_row.debit_credit_flag := 'DR';
5780                    adj_row.adjustment_type := 'IMPAIR EXPENSE';
5781                    adj_row.adjustment_amount := impairment_amt;
5782                    adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
5783                    adj_row.selection_retid := 0;
5784                    adj_row.units_retired := 0;
5785                    adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5786                    adj_row.set_of_books_id := ret.set_of_books_id;
5787 
5788                    if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5789                                        X_last_update_date,
5790                                        X_last_updated_by,
5791                                        X_last_update_login
5792                                        , p_log_level_rec => p_log_level_rec)) then
5793 
5794                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5795                       return(FALSE);
5796 
5797                    end if; -- end of if not FA_INS_ADJUST_PKG.faxinaj
5798 
5799                 end if;
5800 
5801                 --  BUG# 1400554
5802                 --      resetting the account segment to value prior to
5803                 --      the bonus rule logic.  i.e. = cb.deprn_exp_acct
5804                 --      adj_row.account[0] = '\0';
5805 
5806                 adj_row.account := h_deprn_exp_acct;
5807                 adj_row.account_type := 'DEPRN_EXPENSE_ACCT';
5808 
5809                 if reval_deprn_amt > 0 then
5810 
5811                    adj_row.debit_credit_flag := 'DR';
5812                    adj_row.adjustment_type := 'REVAL EXPENSE';
5813                    adj_row.adjustment_amount := reval_deprn_amt;
5814 
5815                 -- This is now obsolete.  We do not calculate reval expense
5816                 -- seperately from deprn expense.  All expense is calculated
5817                 -- by the deprn engine.  I am leaving logic to calculate this
5818                 -- in case of future use, but we should not insert the
5819                 -- adjustment rows.
5820 
5821                 end if;
5822 
5823                 if reval_amort > 0  then
5824 
5825                    adj_row.debit_credit_flag := 'DR';
5826                    adj_row.adjustment_type := 'REVAL AMORT';
5827                    adj_row.adjustment_amount := reval_amort;
5828                    adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5829                    adj_row.set_of_books_id := ret.set_of_books_id;
5830 
5831                    if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5832                                        X_last_update_date,
5833                                        X_last_updated_by,
5834                                        X_last_update_login,
5835                                        p_log_level_rec => p_log_level_rec)) then
5836 
5837                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5838                       return(FALSE);
5839 
5840                    end if;
5841 
5842                 end if; -- end of - if reval_amort
5843 
5844              else
5845 
5846                 adj_row.debit_credit_flag := 'DR';
5847                 adj_row.adjustment_type := 'EXPENSE';
5848                 adj_row.adjustment_amount := deprn_amt;
5849                 adj_row.selection_mode := FA_STD_TYPES.FA_AJ_RETIRE;
5850                 adj_row.selection_retid := ret.retirement_id;
5851                 adj_row.units_retired := ret.units_retired;
5852                 adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5853                 adj_row.set_of_books_id := ret.set_of_books_id;
5854 
5855                 if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5856                                        X_last_update_date,
5857                                        X_last_updated_by,
5858                                        X_last_update_login,
5859                                        p_log_level_rec => p_log_level_rec)) then
5860 
5861                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5862                       return(FALSE);
5863 
5864                 end if;
5865 
5866                 /* Bug 6666666 : Neutralizing entries for SORP */
5867                 if FA_CACHE_PKG.fazcbc_record.sorp_enabled_flag = 'Y' then
5868                     if not FA_SORP_UTIL_PVT.create_sorp_neutral_acct (
5869                             p_amount                => deprn_amt,
5870                             p_reversal              => 'N',
5871                             p_adj                   => adj_row,
5872                             p_created_by            => NULL,
5873                             p_creation_date         => NULL,
5874                             p_last_update_date      => X_last_update_date,
5875                             p_last_updated_by       => X_last_updated_by,
5876                             p_last_update_login     => X_last_update_login,
5877                             p_who_mode              => 'UPDATE',
5878                             p_log_level_rec => p_log_level_rec) then
5879                               fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5880                               return(FALSE);
5881                     end if;
5882                 end if;
5883                 /* End of Bug 6666666 */
5884 
5885                 if bonus_deprn_amt <> 0 then
5886 
5887                    select nvl(cb.bonus_deprn_expense_acct,'0')
5888                    into h_bonus_deprn_exp_acct
5889                    from fa_additions_b ad,
5890                         fa_category_books cb
5891                    where ad.asset_id = h_asset_id
5892                    and   cb.category_id = ad.asset_category_id
5893                    and   cb.book_type_code = h_book;
5894 
5895                    adj_row.account := h_bonus_deprn_exp_acct;
5896                    adj_row.account_type := 'BONUS_DEPRN_EXPENSE_ACCT';
5897                    adj_row.debit_credit_flag := 'DR';
5898                    adj_row.adjustment_type := 'BONUS EXPENSE';
5899                    adj_row.adjustment_amount := bonus_deprn_amt;
5900                    adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
5901                    adj_row.selection_retid := 0;
5902                    adj_row.units_retired := 0;
5903                    adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5904                    adj_row.set_of_books_id := ret.set_of_books_id;
5905 
5906                    if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5907                                        X_last_update_date,
5908                                        X_last_updated_by,
5909                                        X_last_update_login,
5910                                        p_log_level_rec => p_log_level_rec)) then
5911 
5912                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5913                       return(FALSE);
5914 
5915                    end if; -- end of  if not FA_INS_ADJUST_PKG.faxinaj
5916 
5917                 end if;
5918 
5919                 if impairment_amt <> 0 then
5920 
5921                    select nvl(cb.impair_expense_acct,'0')
5922                    into h_impair_exp_acct
5923                    from fa_additions_b ad,
5924                         fa_category_books cb
5925                    where ad.asset_id = h_asset_id
5926                    and   cb.category_id = ad.asset_category_id
5927                    and   cb.book_type_code = h_book;
5928 
5929                    adj_row.account := h_impair_exp_acct;
5930                    adj_row.account_type := 'IMPAIR_EXPENSE_ACCT';
5931                    adj_row.debit_credit_flag := 'DR';
5932                    adj_row.adjustment_type := 'IMPAIR EXPENSE';
5933                    adj_row.adjustment_amount := impairment_amt;
5934                    adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
5935                    adj_row.selection_retid := 0;
5936                    adj_row.units_retired := 0;
5937                    adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5938                    adj_row.set_of_books_id := ret.set_of_books_id;
5939 
5940                    if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5941                                        X_last_update_date,
5942                                        X_last_updated_by,
5943                                        X_last_update_login
5944                                        , p_log_level_rec => p_log_level_rec)) then
5945 
5946                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5947                       return(FALSE);
5948 
5949                    end if; -- end of  if not FA_INS_ADJUST_PKG.faxinaj
5950 
5951                 end if;
5952 
5953                 --  BUG# 1400554
5954                 --      resetting the account segment to value prior to
5955                 --      the bonus rule logic.  i.e. = cb.deprn_exp_acct
5956                 --      adj_row.account[0] = '\0';
5957 
5958                 adj_row.account := h_deprn_exp_acct;
5959                 adj_row.account_type := 'DEPRN_EXPENSE_ACCT';
5960 
5961                 if reval_deprn_amt > 0 then
5962 
5963                    adj_row.debit_credit_flag := 'DR';
5964                    adj_row.adjustment_type := 'REVAL EXPENSE';
5965                    adj_row.adjustment_amount := reval_deprn_amt;
5966                    adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5967                    adj_row.set_of_books_id := ret.set_of_books_id;
5968 
5969                    if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5970                                        X_last_update_date,
5971                                        X_last_updated_by,
5972                                        X_last_update_login,
5973                                        p_log_level_rec => p_log_level_rec)) then
5974 
5975                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5976                       return(FALSE);
5977 
5978                    end if;
5979 
5980                 end if; -- end of - if reval_deprn_amt
5981 
5982                 if reval_amort > 0 then
5983 
5984                    adj_row.debit_credit_flag := 'DR';
5985                    adj_row.adjustment_type := 'REVAL AMORT';
5986                    adj_row.adjustment_amount := reval_amort;
5987                    adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
5988                    adj_row.set_of_books_id := ret.set_of_books_id;
5989 
5990                    if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
5991                                        X_last_update_date,
5992                                        X_last_updated_by,
5993                                        X_last_update_login,
5994                                        p_log_level_rec => p_log_level_rec)) then
5995 
5996                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
5997                       return(FALSE);
5998 
5999                    end if;
6000 
6001                 end if; -- end of - if reval_amort
6002 
6003              end if; -- end of - if (ret.units_retired
6004 
6005           end if; -- end of - if not bk.fully_reserved
6006 
6007        end if; -- end of - if (pds_catchup = 0)
6008 
6009        if p_log_level_rec.statement_level then
6010            fa_debug_pkg.add(l_calling_fn, 'in FAGPDP 3', '', p_log_level_rec => p_log_level_rec);
6011        end if;
6012 
6013        h_deprn_amt := 0;
6014        h_reval_deprn_amt := 0;
6015        h_reval_amort := 0;
6016        h_bonus_deprn_amt := 0;
6017        h_impairment_amt := 0;
6018 
6019        if p_log_level_rec.statement_level then
6020             fa_debug_pkg.add
6021               (fname   => l_calling_fn,
6022                element => 'total deprn expense',
6023                value   => '', p_log_level_rec => p_log_level_rec);
6024        end if;
6025 
6026        if (ret.mrc_sob_type_code <> 'R') then
6027          begin
6028             SELECT SUM(faadj.adjustment_amount)
6029             INTO   h_deprn_amt
6030             FROM   FA_ADJUSTMENTS faadj
6031             WHERE
6032                    faadj.transaction_header_id = h_th_id_in
6033             AND    faadj.source_type_code = 'RETIREMENT'
6034             AND    faadj.adjustment_type = 'EXPENSE'
6035             AND    faadj.book_type_Code = h_book
6036             AND    faadj.asset_id = h_asset_id
6037             AND    faadj.period_counter_created = h_cpd_ctr
6038             GROUP BY faadj.transaction_header_id;
6039 
6040           /* Test for a no rows found condition;
6041           * return zeroes in this case.
6042           * Set h_found_period_counter to zero also.
6043           */
6044             EXCEPTION
6045                when no_data_found then
6046                   h_deprn_amt := 0;
6047          end;
6048 
6049        else
6050 
6051          begin
6052             SELECT SUM(faadj.adjustment_amount)
6053             INTO   h_deprn_amt
6054             FROM   FA_MC_ADJUSTMENTS faadj
6055             WHERE
6056                    faadj.transaction_header_id = h_th_id_in
6057             AND    faadj.source_type_code = 'RETIREMENT'
6058             AND    faadj.adjustment_type = 'EXPENSE'
6059             AND    faadj.book_type_Code = h_book
6060             AND    faadj.asset_id = h_asset_id
6061             AND    faadj.period_counter_created = h_cpd_ctr
6062             AND    faadj.set_of_books_id = ret.set_of_books_id
6063             GROUP BY faadj.transaction_header_id;
6064 
6065           /* Test for a no rows found condition;
6066           * return zeroes in this case.
6067           * Set h_found_period_counter to zero also.
6068           */
6069             EXCEPTION
6070                when no_data_found then
6071                   h_deprn_amt := 0;
6072          end;
6073 
6074        end if;
6075 
6076        deprn_amt := h_deprn_amt;
6077 
6078        if (ret.mrc_sob_type_code <> 'R') then
6079          begin
6080             SELECT SUM(faadj.adjustment_amount)
6081             INTO   h_bonus_deprn_amt
6082             FROM   FA_ADJUSTMENTS faadj
6083             WHERE
6084                    faadj.transaction_header_id = h_th_id_in
6085             AND    faadj.source_type_code = 'RETIREMENT'
6086             AND    faadj.adjustment_type = 'BONUS EXPENSE'
6087             AND    faadj.book_type_Code = h_book
6088             AND    faadj.asset_id = h_asset_id
6089             AND    faadj.period_counter_created = h_cpd_ctr
6090             GROUP BY faadj.transaction_header_id;
6091 
6092           /* Test for a no rows found condition;
6093           * return zeroes in this case.
6094           * Set h_found_period_counter to zero also.
6095           */
6096             EXCEPTION
6097                when no_data_found then
6098                   h_bonus_deprn_amt := 0;
6099          end;
6100 
6101          begin
6102             SELECT SUM(faadj.adjustment_amount)
6103             INTO   h_impairment_amt
6104             FROM   FA_ADJUSTMENTS faadj
6105             WHERE
6106                    faadj.transaction_header_id = h_th_id_in
6107             AND    faadj.source_type_code = 'RETIREMENT'
6108             AND    faadj.adjustment_type = 'IMPAIR EXPENSE'
6109             AND    faadj.book_type_Code = h_book
6110             AND    faadj.asset_id = h_asset_id
6111             AND    faadj.period_counter_created = h_cpd_ctr
6112             GROUP BY faadj.transaction_header_id;
6113 
6114           /* Test for a no rows found condition;
6115           * return zeroes in this case.
6116           * Set h_found_period_counter to zero also.
6117           */
6118             EXCEPTION
6119                when no_data_found then
6120                   h_impairment_amt := 0;
6121          end;
6122 
6123        else
6124          begin
6125             SELECT SUM(faadj.adjustment_amount)
6126             INTO   h_bonus_deprn_amt
6127             FROM   FA_MC_ADJUSTMENTS faadj
6128             WHERE
6129                    faadj.transaction_header_id = h_th_id_in
6130             AND    faadj.source_type_code = 'RETIREMENT'
6131             AND    faadj.adjustment_type = 'BONUS EXPENSE'
6132             AND    faadj.book_type_Code = h_book
6133             AND    faadj.asset_id = h_asset_id
6134             AND    faadj.period_counter_created = h_cpd_ctr
6135             AND    faadj.set_of_books_id = ret.set_of_books_id
6136             GROUP BY faadj.transaction_header_id;
6137           /* Test for a no rows found condition;
6138           * return zeroes in this case.
6139           * Set h_found_period_counter to zero also.
6140           */
6141             EXCEPTION
6142                when no_data_found then
6143                   h_bonus_deprn_amt := 0;
6144          end;
6145 
6146          begin
6147             SELECT SUM(faadj.adjustment_amount)
6148             INTO   h_impairment_amt
6149             FROM   FA_MC_ADJUSTMENTS faadj
6150             WHERE
6151                    faadj.transaction_header_id = h_th_id_in
6152             AND    faadj.source_type_code = 'RETIREMENT'
6153             AND    faadj.adjustment_type = 'IMPAIR EXPENSE'
6154             AND    faadj.book_type_Code = h_book
6155             AND    faadj.asset_id = h_asset_id
6156             AND    faadj.period_counter_created = h_cpd_ctr
6157             AND    faadj.set_of_books_id = ret.set_of_books_id
6158             GROUP BY faadj.transaction_header_id;
6159           /* Test for a no rows found condition;
6160           * return zeroes in this case.
6161           * Set h_found_period_counter to zero also.
6162           */
6163             EXCEPTION
6164                when no_data_found then
6165                   h_impairment_amt := 0;
6166          end;
6167 
6168        end if;
6169 
6170        bonus_deprn_amt := h_bonus_deprn_amt;
6171        impairment_amt := h_impairment_amt;
6172 
6173        if p_log_level_rec.statement_level then
6174             fa_debug_pkg.add
6175               (fname   => l_calling_fn,
6176                element => 'total reval deprn expense',
6177                value   => '', p_log_level_rec => p_log_level_rec);
6178        end if;
6179 
6180        if (ret.mrc_sob_type_code <> 'R') then
6181          begin
6182             SELECT SUM(faadj.adjustment_amount)
6183             INTO   h_reval_deprn_amt
6184             FROM   FA_ADJUSTMENTS faadj
6185             WHERE
6186                    faadj.transaction_header_id = h_th_id_in
6187             AND    faadj.source_type_code = 'RETIREMENT'
6188             AND    faadj.adjustment_type = 'REVAL EXPENSE'
6189             AND    faadj.book_type_Code = h_book
6190             AND    faadj.asset_id = h_asset_id
6191             AND    faadj.period_counter_created = h_cpd_ctr
6192             GROUP BY faadj.transaction_header_id;
6193 
6194          EXCEPTION
6195             when no_data_found then
6196                h_reval_deprn_amt := 0;
6197          end;
6198        else
6199          begin
6200             SELECT SUM(faadj.adjustment_amount)
6201             INTO   h_reval_deprn_amt
6202             FROM   FA_MC_ADJUSTMENTS faadj
6203             WHERE
6204                    faadj.transaction_header_id = h_th_id_in
6205             AND    faadj.source_type_code = 'RETIREMENT'
6206             AND    faadj.adjustment_type = 'REVAL EXPENSE'
6207             AND    faadj.book_type_Code = h_book
6208             AND    faadj.asset_id = h_asset_id
6209             AND    faadj.period_counter_created = h_cpd_ctr
6210             AND    faadj.set_of_books_id = ret.set_of_books_id
6211             GROUP BY faadj.transaction_header_id;
6212 
6213          EXCEPTION
6214             when no_data_found then
6215                h_reval_deprn_amt := 0;
6216          end;
6217        end if;
6218 
6219        reval_deprn_amt := h_reval_deprn_amt;
6220 
6221        if p_log_level_rec.statement_level then
6222             fa_debug_pkg.add
6223               (fname   => l_calling_fn,
6224                element => 'total reval amort',
6225                value   => '', p_log_level_rec => p_log_level_rec);
6226        end if;
6227 
6228        if (ret.mrc_sob_type_code <> 'R') then
6229          begin
6230             SELECT SUM(faadj.adjustment_amount)
6231             INTO   h_reval_amort
6232             FROM   FA_ADJUSTMENTS faadj
6233             WHERE
6234                    faadj.transaction_header_id = h_th_id_in
6235             AND    faadj.source_type_code = 'RETIREMENT'
6236             AND    faadj.adjustment_type = 'REVAL AMORT'
6237             AND    faadj.book_type_Code = h_book
6238             AND    faadj.asset_id = h_asset_id
6239             AND    faadj.period_counter_created = h_cpd_ctr
6240             GROUP BY faadj.transaction_header_id;
6241 
6242          EXCEPTION
6243             when no_data_found then
6244                h_reval_amort := 0;
6245          end;
6246        else
6247          begin
6248             SELECT SUM(faadj.adjustment_amount)
6249             INTO   h_reval_amort
6250             FROM   FA_MC_ADJUSTMENTS faadj
6251             WHERE
6252                    faadj.transaction_header_id = h_th_id_in
6253             AND    faadj.source_type_code = 'RETIREMENT'
6254             AND    faadj.adjustment_type = 'REVAL AMORT'
6255             AND    faadj.book_type_Code = h_book
6256             AND    faadj.asset_id = h_asset_id
6257             AND    faadj.period_counter_created = h_cpd_ctr
6258             AND    faadj.set_of_books_id = ret.set_of_books_id
6259             GROUP BY faadj.transaction_header_id;
6260 
6261          EXCEPTION
6262             when no_data_found then
6263                h_reval_amort := 0;
6264          end;
6265        end if;
6266 
6267        reval_amort := h_reval_amort;
6268 
6269        return(TRUE);
6270 
6271 
6272     EXCEPTION
6273 
6274           when others then
6275 
6276             fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6277             return FALSE;
6278 
6279     END FAGPDP;
6280 
6281 
6282 /*===========================================================================*
6283 | NAME        fagprv                                                         |
6284 |                                                                            |
6285 | FUNCTION    Calculate reserve retired and insert it into FA_ADJUSTMENTS.   |
6286 |             It returns the  current depreciation reserve before adjusted.  |
6287 |                                                                            |
6288 | HISTORY     08/30/89    R Rumanang      Created                            |
6289 |             11/21/89    R Rumanang      Put distribution_id in adjustments |
6290 |             05/03/91    M Chan          Rewrote for MPL 9                  |
6291 |             01/03/96    S Behura        Rewrote using PL/SQL               |
6292 |                                                                            |
6293 *============================================================================*/
6294 
6295 FUNCTION fagprv(ret in out nocopy fa_ret_types.ret_struct,
6296                 bk in out nocopy fa_ret_types.book_struct,
6297                 cpd_ctr number, cost_frac in number,
6298                 today in date, user_id number,
6299                 deprn_amt in out nocopy number, reval_deprn_amt in out number,
6300                 reval_amort in out nocopy number, deprn_reserve in out number,
6301                 reval_reserve in out nocopy number,
6302                 bonus_deprn_amt in out nocopy number,
6303                 bonus_deprn_reserve in out nocopy number,
6304                 impairment_amt in out nocopy number,
6305                 impairment_reserve in out nocopy number,
6306                 g_deprn_amt in number,                       --Bug11886090
6307                 g_bonus_deprn_amt in number,                 --Bug11886090
6308                 p_log_level_rec in FA_API_TYPES.log_level_rec_type) Return BOOLEAN IS
6309 
6310     CURSOR c_get_unit is
6311       select units
6312       from   fa_asset_history
6313       where  asset_id = bk.group_asset_id
6314       and    transaction_header_id_out is null;
6315 
6316     CURSOR c_get_cost is
6317       select cost
6318       from   fa_books
6319       where  asset_id = bk.group_asset_id
6320       and    book_type_code = ret.book
6321       and    date_ineffective is null;
6322 
6323     /* New Fix for Bug# 2791196
6324        Cleared_reserves cursor will only return
6325        the RETIREMENT RESERVE DR rows in fa_adjustments for the distributions
6326        that are active and that are not the newly created rows
6327        by the partial unit retirement.
6328        The adj_amount for each of the rows will be updated
6329        with that of the corresponding row. */
6330 
6331 
6332     fagprv_err          exception;
6333 
6334     tot_deprn_reserve   number;
6335     tot_reval_reserve   number;
6336     tot_bonus_deprn_reserve number;
6337     tot_impairment_reserve  number;
6338     deprn_rsv_acct      varchar2(30);
6339     reval_rsv_acct      varchar2(30);
6340     bonus_deprn_rsv_acct varchar2(30);
6341     impairment_rsv_acct  varchar2(30);
6342     -- adj_row             FA_STD_TYPES.fa_adj_row_struct;
6343     adj_row             FA_ADJUST_TYPE_PKG.fa_adj_row_struct;
6344 
6345     h_retire_reval_flag number;
6346     h_asset_id          number(15);
6347     h_book              varchar2(30);
6348     h_ret_id            number(15);
6349     h_id_out            number(15);
6350     h_deprn_rsv_acct    varchar2(30);
6351     h_reval_rsv_acct    varchar2(30);
6352     h_bonus_deprn_rsv_acct varchar2(30);
6353     h_impairment_rsv_acct  varchar2(30);
6354     h_cur_units         number;
6355 
6356     -- Fix for Bug 3441030
6357     l_prev_deprn_reserve number;
6358     l_prev_adj_rec_cost  number;
6359     l_new_adj_rec_cost   number;
6360     l_final_rsv          number;
6361     l_fully_rsvd_flag    boolean := FALSE;
6362 
6363     X_LAST_UPDATE_DATE date := sysdate;
6364     X_last_updated_by number := -1;
6365     X_last_update_login number := -1;
6366 
6367     l_calling_fn        varchar2(40) := 'FA_GAINLOSS_UPD_PKG.fagprv';
6368 
6369     l_rsv_retired       number := null;
6370     l_temp_num          number;
6371     l_temp_char         varchar2(30);
6372     l_temp_bool         boolean;
6373     l_g_cost            number;
6374     l_g_rsv             number;
6375     l_g_bonus_rsv       number;
6376     l_g_impair_rsv      number;
6377     l_asset_hdr_rec   FA_API_TYPES.asset_hdr_rec_type;
6378     l_asset_cat_rec   FA_API_TYPES.asset_cat_rec_type;
6379 
6380     l_id number;
6381 
6382     l_balance_tfr_in_tax  number;
6383     l_unit_ret_in_corp    boolean;
6384     l_ret_id_in_corp    number;
6385 
6386 
6387     l_dummy number;
6388     h_sum_of_part_active_units number;
6389 
6390     H_DPR_ROW FA_STD_TYPES.fa_deprn_row_struct;
6391     h_dist_book          varchar2(30);
6392     --h_asset_id           number;
6393     h_success            boolean;
6394     --h_distribution_lines number;
6395     h_distribution_id    number;
6396     h_trans_units        number;
6397     h_units_assigned     number;
6398     h_dist_ccid          number;
6399     --h_calculated_ccid    number;
6400     --h_ret_id             number;
6401     --h_units_assigned     number;
6402     h_adj_type           varchar2(20);
6403     h_mesg_name          varchar2(30);
6404     h_row_ctr            number:=0;
6405     --h_amount_so_far      number :=0;
6406     --h_amount_to_insert   number:=0;
6407     --h_adj_dd_amount      number;
6408     h_total_amount       number:=0;
6409     h_total_units        number:=0;
6410     --h_adjustment_amount  number :=0;
6411 
6412    CURSOR C1 IS
6413     SELECT DISTRIBUTION_ID,
6414            CODE_COMBINATION_ID,
6415            TRANSACTION_UNITS,
6416 	   UNITS_ASSIGNED
6417       FROM FA_DISTRIBUTION_HISTORY
6418      WHERE ASSET_ID       = RET.asset_id
6419        AND BOOK_TYPE_CODE = h_dist_book
6420        AND RETIREMENT_ID  = RET.retirement_id
6421      ORDER BY DISTRIBUTION_ID;
6422 
6423     BEGIN <<FAGPRV>>
6424 
6425        if p_log_level_rec.statement_level then
6426 
6427           begin
6428             select retirement_id
6429             into l_id
6430             from fa_retirements
6431             where asset_id=ret.asset_id
6432               and book_type_code=ret.book;
6433           exception  when others then null;
6434               l_id := 0;
6435           end;
6436 
6437           fa_debug_pkg.add
6438               (fname   => l_calling_fn,
6439                element => 'l_id in fagprv(1)',
6440                value   => l_id);
6441 
6442           fa_debug_pkg.add
6443               (fname   => l_calling_fn,
6444                element => 'ret.retirement_id in fagprv(1)',
6445                value   => ret.retirement_id);
6446        end if;
6447 
6448        if p_log_level_rec.statement_level then
6449             fa_debug_pkg.add
6450               (fname   => l_calling_fn,
6451                element => 'cost frac in fagprv',
6452                value   => to_char(cost_frac)||':'||to_char(ret.reserve_retired));
6453        end if;
6454 
6455        l_rsv_retired := ret.reserve_retired;
6456 
6457        tot_deprn_reserve := 0;
6458        tot_bonus_deprn_reserve := 0;
6459        tot_impairment_reserve := 0;
6460        tot_reval_reserve := 0;
6461        h_cur_units := 0;
6462        h_asset_id := ret.asset_id;
6463        h_book := ret.book;
6464        h_ret_id := ret.retirement_id;
6465 
6466 
6467        -- Fix for Bug 3441030
6468        l_prev_deprn_reserve := deprn_reserve;
6469        select adjusted_recoverable_cost
6470        into l_prev_adj_rec_cost
6471        from fa_books
6472        where transaction_header_id_out = ret.th_id_in;
6473 
6474        if (l_prev_deprn_reserve = l_prev_adj_rec_cost) then
6475           l_fully_rsvd_flag := TRUE;
6476           select adjusted_recoverable_cost
6477           into l_new_adj_rec_cost
6478           from fa_books
6479           where transaction_header_id_in = ret.th_id_in;
6480           l_final_rsv := l_new_adj_rec_cost;
6481        end if;
6482 
6483        deprn_reserve := deprn_reserve * cost_frac;
6484        -- fix for 1972854 - cost_frac is set to zero when cost is zero
6485        -- which causes reval_reserve go zero, thus do the following
6486        -- multiplication only for non-zero cost_frac
6487        if cost_frac <> 0 then
6488           reval_reserve := reval_reserve * cost_frac;
6489 
6490           /* Bug#4459585 rounding issue */
6491           if not FA_UTILS_PKG.faxrnd(reval_reserve, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
6492             fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6493             raise fagprv_err;
6494           end if;
6495 
6496        end if;
6497 
6498        bonus_deprn_reserve := bonus_deprn_reserve * cost_frac;
6499        impairment_reserve := impairment_reserve * cost_frac;
6500 
6501        /* Bug#4459585 rounding issue */
6502        if not FA_UTILS_PKG.faxrnd(bonus_deprn_reserve, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
6503           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6504           raise fagprv_err;
6505        end if;
6506 
6507        if not FA_UTILS_PKG.faxrnd(impairment_reserve, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
6508           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6509           raise fagprv_err;
6510        end if;
6511 
6512        if p_log_level_rec.statement_level then
6513             fa_debug_pkg.add
6514               (fname   => l_calling_fn,
6515                element => 'deprn_reserve in fagprv',
6516                value   => to_char(deprn_reserve));
6517             fa_debug_pkg.add
6518               (fname   => l_calling_fn,
6519                element => 'deprn_amt in fagprv',
6520                value   => to_char(deprn_amt));
6521        end if;
6522 
6523        tot_deprn_reserve := deprn_reserve + deprn_amt;
6524        tot_reval_reserve := reval_reserve - reval_amort;
6525        tot_bonus_deprn_reserve := bonus_deprn_reserve + bonus_deprn_amt;
6526        tot_impairment_reserve := impairment_reserve + impairment_amt;
6527 
6528        if p_log_level_rec.statement_level then
6529             fa_debug_pkg.add
6530               (fname   => l_calling_fn,
6531                element => 'bonus_deprn_reserve in fagprv',
6532                value   => to_char(bonus_deprn_reserve));
6533             fa_debug_pkg.add
6534               (fname   => l_calling_fn,
6535                element => 'bonus_deprn_amt in fagprv',
6536                value   => to_char(bonus_deprn_amt));
6537        end if;
6538 
6539        -- Fix for Bug 3441030
6540        if (l_fully_rsvd_flag and deprn_amt = 0)  then
6541           tot_deprn_reserve := l_prev_deprn_reserve - l_final_rsv;
6542        end if;
6543 
6544     /*
6545      * Round tot_deprn_reserve according to functional currency
6546      */
6547        if not FA_UTILS_PKG.faxrnd(tot_deprn_reserve, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
6548           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6549           raise fagprv_err;
6550        end if;
6551 
6552        if not FA_UTILS_PKG.faxrnd(tot_bonus_deprn_reserve, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
6553           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6554           raise fagprv_err;
6555        end if;
6556 
6557        if not FA_UTILS_PKG.faxrnd(tot_impairment_reserve, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
6558           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6559           raise fagprv_err;
6560        end if;
6561 
6562     /*
6563      * Round tot_reval_reserve according to functional currency
6564      */
6565 
6566        if not FA_UTILS_PKG.faxrnd(tot_reval_reserve, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
6567           fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6568           raise fagprv_err;
6569        end if;
6570 
6571        if p_log_level_rec.statement_level then
6572             fa_debug_pkg.add
6573               (fname   => l_calling_fn,
6574                element => 'acct segment in fagprv',
6575                value   => '', p_log_level_rec => p_log_level_rec);
6576        end if;
6577 
6578        h_retire_reval_flag := 0;
6579 
6580        begin
6581           select  facb.deprn_reserve_acct,
6582                   facb.reval_reserve_acct,
6583                   facb.bonus_deprn_reserve_acct,
6584                   facb.impair_reserve_acct,
6585                   decode(bc.retire_reval_reserve_flag,'NO',0,1)
6586           into    h_deprn_rsv_acct,
6587                   h_reval_rsv_acct,
6588                   h_bonus_deprn_rsv_acct,
6589                   h_impairment_rsv_acct,
6590                   h_retire_reval_flag
6591           from    fa_additions_b    faadd,
6592                   fa_category_books facb,
6593                   fa_book_controls bc
6594           where   faadd.asset_id = h_asset_id
6595           and     facb.category_id = faadd.asset_category_id
6596           and     facb.book_type_code = h_book
6597           and     bc.book_type_code = facb.book_type_code;
6598           EXCEPTION
6599              when no_data_found then
6600                 raise fagprv_err;
6601        end;
6602 
6603        deprn_rsv_acct := h_deprn_rsv_acct;
6604        reval_rsv_acct := h_reval_rsv_acct;
6605        bonus_deprn_rsv_acct := h_bonus_deprn_rsv_acct;
6606        impairment_rsv_acct := h_impairment_rsv_acct;
6607        adj_row.transaction_header_id := ret.th_id_in;
6608        /*12768930 */
6609        if ret.wip_asset > 0 then
6610           adj_row.source_type_code := 'CIP RETIREMENT';
6611        else
6612           adj_row.source_type_code := 'RETIREMENT';
6613        end if;
6614        adj_row.book_type_code := ret.book;
6615        adj_row.period_counter_created := cpd_ctr;
6616        adj_row.asset_id := ret.asset_id;
6617        adj_row.period_counter_adjusted := cpd_ctr;
6618        adj_row.last_update_date := today;
6619        adj_row.current_units := bk.cur_units;
6620        adj_row.gen_ccid_flag := TRUE;
6621        adj_row.flush_adj_flag := TRUE;
6622        adj_row.annualized_adjustment := 0;
6623        adj_row.code_combination_id := 0;
6624        adj_row.distribution_id := 0;
6625        adj_row.selection_retid := 0;
6626        adj_row.units_retired := 0;
6627        adj_row.asset_invoice_id := 0;
6628        adj_row.leveling_flag := TRUE;
6629 
6630        if p_log_level_rec.statement_level then
6631             fa_debug_pkg.add
6632               (fname   => l_calling_fn,
6633                element => 'deprn_allocation_code in fagprv',
6634                value   => '', p_log_level_rec => p_log_level_rec);
6635        end if;
6636 
6637        --8244128 Changed the check from Allocate to member rollup flag
6638        --8546627 Added condition for Stand Alone assets
6639        --8631612 Modified the check for member_rollup_flag to tracking method
6640        if (ret.units_retired is null or ret.units_retired <= 0) and
6641           (((bk.group_asset_id is not null) and (bk.tracking_method is not null)) or bk.group_asset_id is null)  then
6642 
6643           adj_row.account := deprn_rsv_acct;
6644           adj_row.account_type := 'DEPRN_RESERVE_ACCT';
6645           adj_row.adjustment_type := 'RESERVE';
6646           adj_row.adjustment_amount := tot_deprn_reserve;
6647           adj_row.selection_thid := 0;
6648           adj_row.debit_credit_flag := 'DR';
6649 
6650           if (bk.current_cost = ret.cost_retired) then
6651              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_CLEAR;
6652           else
6653              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
6654              -- Bug 5149832
6655              if (bk.book_class) then
6656                 if NOT faginfo(
6657                             RET, BK, cpd_ctr,today, user_id
6658                            ,calling_module => l_calling_fn
6659                            ,candidate_mode => 'CLEAR_PARTIAL'
6660                            ,set_adj_row => TRUE
6661                            ,unit_ret_in_corp => l_unit_ret_in_corp
6662                            ,ret_id_in_corp => l_ret_id_in_corp
6663                            ,th_id_out_in_corp => h_id_out
6664                            ,balance_tfr_in_tax => l_balance_tfr_in_tax
6665                            ,adj_row => adj_row
6666                            ,p_log_level_rec => p_log_level_rec ) then
6667                   fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6668                   return(FALSE);
6669                 end if;
6670              end if;
6671 
6672           end if;
6673 
6674           if p_log_level_rec.statement_level then
6675              fa_debug_pkg.add(l_calling_fn, '++ bk.book_class (TRUE=TAX)', bk.book_class);
6676              fa_debug_pkg.add(l_calling_fn, '++ adj_row.selection_thid', adj_row.selection_thid, p_log_level_rec => p_log_level_rec);
6677           end if;
6678 
6679           adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
6680           adj_row.set_of_books_id := ret.set_of_books_id;
6681 
6682           if (bk.group_asset_id is not null) and
6683              (nvl(bk.member_rollup_flag, 'N') = 'N') then
6684              adj_row.track_member_flag := 'Y';
6685           else
6686              adj_row.track_member_flag := null;
6687           end if;
6688 
6689           if p_log_level_rec.statement_level then
6690             fa_debug_pkg.add
6691               (fname   => l_calling_fn,
6692                element => 'Before faxinaj for RESERVE',
6693                value   => '', p_log_level_rec => p_log_level_rec);
6694 
6695             fa_debug_pkg.add
6696               (fname   => l_calling_fn,
6697                element => 'account',
6698                value   => adj_row.account, p_log_level_rec => p_log_level_rec);
6699 
6700           end if;
6701 
6702           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
6703                                        X_last_update_date,
6704                                        X_last_updated_by,
6705                                        X_last_update_login,
6706                                        p_log_level_rec => p_log_level_rec)) then
6707 
6708              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6709              return(FALSE);
6710 
6711           end if;
6712 
6713           if p_log_level_rec.statement_level then
6714             fa_debug_pkg.add
6715               (fname   => l_calling_fn,
6716                element => 'After faxinaj for RESERVE',
6717                value   => '', p_log_level_rec => p_log_level_rec);
6718           end if;
6719 
6720           if (tot_bonus_deprn_reserve <> 0 ) then
6721 
6722              adj_row.account := bonus_deprn_rsv_acct;
6723              adj_row.account_type := 'BONUS_DEPRN_RESERVE_ACCT';
6724              adj_row.adjustment_type := 'BONUS RESERVE';
6725              adj_row.adjustment_amount := tot_bonus_deprn_reserve;
6726              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
6727              adj_row.set_of_books_id := ret.set_of_books_id;
6728 
6729              if (bk.group_asset_id is not null) and
6730                 (nvl(bk.member_rollup_flag, 'N') = 'N') then
6731                 adj_row.track_member_flag := 'Y';
6732              else
6733                 adj_row.track_member_flag := null;
6734              end if;
6735 
6736              if adj_row.adjustment_amount <> 0 then
6737 
6738                 if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
6739                                        X_last_update_date,
6740                                        X_last_updated_by,
6741                                        X_last_update_login,
6742                                        p_log_level_rec => p_log_level_rec)) then
6743 
6744                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6745                    return(FALSE);
6746 
6747                 end if;
6748 
6749              end if;
6750 
6751           end if; -- end of tot_bonus_deprn_reserve <> 0
6752 
6753 
6754           if p_log_level_rec.statement_level then
6755             fa_debug_pkg.add
6756               (fname   => l_calling_fn,
6757                element => 'After faxinaj for BONUS RESERVE',
6758                value   => '', p_log_level_rec => p_log_level_rec);
6759           end if;
6760 
6761           if (tot_impairment_reserve <> 0 ) then
6762 
6763              adj_row.account := impairment_rsv_acct;
6764              adj_row.account_type := 'IMPAIR_RESERVE_ACCT';
6765              adj_row.adjustment_type := 'IMPAIR RESERVE';
6766              adj_row.adjustment_amount := tot_impairment_reserve;
6767              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
6768              adj_row.set_of_books_id := ret.set_of_books_id;
6769 
6770              if (bk.group_asset_id is not null) and
6771                 (nvl(bk.member_rollup_flag, 'N') = 'N') then
6772                 adj_row.track_member_flag := 'Y';
6773              else
6774                 adj_row.track_member_flag := null;
6775              end if;
6776 
6777              if adj_row.adjustment_amount <> 0 then
6778 
6779                 if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
6780                                        X_last_update_date,
6781                                        X_last_updated_by,
6782                                        X_last_update_login
6783                                        , p_log_level_rec => p_log_level_rec)) then
6784 
6785                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6786                    return(FALSE);
6787 
6788                 end if;
6789 
6790              end if;
6791 
6792           end if; -- end of tot_impairment_reserve <> 0
6793 
6794 
6795           if p_log_level_rec.statement_level then
6796             fa_debug_pkg.add
6797               (fname   => l_calling_fn,
6798                element => 'After faxinaj for IMPAIR RESERVE',
6799                value   => '', p_log_level_rec => p_log_level_rec);
6800           end if;
6801 
6802           if (bk.current_cost > bk.unrevalued_cost OR reval_reserve <> 0) and
6803                                 (h_retire_reval_flag = 1) then
6804 
6805              adj_row.selection_mode := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE_REVAL;
6806              -- adj_row.selection_mode := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
6807              adj_row.account := reval_rsv_acct;
6808              adj_row.account_type := 'REVAL_RESERVE_ACCT';
6809              adj_row.adjustment_type := 'REVAL RESERVE';
6810              adj_row.adjustment_amount := tot_reval_reserve;
6811              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
6812              adj_row.set_of_books_id := ret.set_of_books_id;
6813 
6814              if (bk.group_asset_id is not null) and
6815                 (nvl(bk.member_rollup_flag, 'N') = 'N') then
6816                 adj_row.track_member_flag := 'Y';
6817              else
6818                 adj_row.track_member_flag := null;
6819              end if;
6820 
6821              if adj_row.adjustment_amount <> 0 then
6822                 if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
6823                                        X_last_update_date,
6824                                        X_last_updated_by,
6825                                        X_last_update_login,
6826                                        p_log_level_rec => p_log_level_rec)) then
6827 
6828                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6829                    return(FALSE);
6830 
6831                 end if;
6832              end if;
6833 
6834           end if; -- end of - if (bk.current_cost
6835 
6836          -- Bug3766289: Old condition didn't process tracked member asset
6837 --       elsif (bk.group_asset_id is null) then
6838        elsif (not((bk.group_asset_id is not null) and
6839                   (nvl(bk.tracking_method, 'ALLOCATE') = 'ALLOCATE'))) then
6840 
6841           if p_log_level_rec.statement_level then
6842             fa_debug_pkg.add
6843               (fname   => l_calling_fn,
6844                element => 'h_asset_id in fagprv(2)',
6845                value   => h_asset_id);
6846             fa_debug_pkg.add
6847               (fname   => l_calling_fn,
6848                element => 'h_book in fagprv(2)',
6849                value   => h_book);
6850             fa_debug_pkg.add
6851               (fname   => l_calling_fn,
6852                element => 'h_ret_id in fagprv(2)',
6853                value   => h_ret_id);
6854 
6855             begin
6856               select   distinct nvl(transaction_header_id_out,0)
6857               into     l_id
6858               from     fa_distribution_history
6859               where    asset_id = h_asset_id
6860               and      book_type_code = h_book
6861               and      retirement_id = h_ret_id;
6862 
6863             exception  when others then null;
6864               l_id := 0;
6865              end;
6866 
6867             fa_debug_pkg.add
6868               (fname   => l_calling_fn,
6869                element => 'l_id from dh in fagprv(2)',
6870                value   => l_id);
6871 
6872           end if;
6873 
6874           begin
6875              select   distinct nvl(transaction_header_id_out,0)
6876              into     h_id_out
6877              from     fa_distribution_history
6878              where    asset_id = h_asset_id
6879              and      book_type_code = h_book
6880              and      retirement_id = h_ret_id;
6881 
6882              EXCEPTION
6883                 when no_data_found then
6884                    raise fagprv_err;
6885           end;
6886 
6887           if p_log_level_rec.statement_level then
6888             fa_debug_pkg.add
6889               (fname   => l_calling_fn,
6890                element => 'transaction_header_id_out in fagprv',
6891                value   => h_id_out, p_log_level_rec => p_log_level_rec);
6892           end if;
6893 
6894           -- Bug# 5170275
6895           h_sum_of_part_active_units := 0;
6896 
6897           begin
6898 
6899             select sum(nvl(units_assigned,0))
6900             into h_sum_of_part_active_units
6901             from fa_distribution_history
6902             where asset_id = h_asset_id
6903               and transaction_header_id_in = h_id_out
6904               and date_ineffective is null;
6905 
6906           exception
6907             when no_data_found then
6908                h_sum_of_part_active_units := 0;
6909           end;
6910 
6911           if p_log_level_rec.statement_level then
6912             fa_debug_pkg.add
6913               (fname   => l_calling_fn,
6914                element => '++ h_sum_of_part_active_units in fagprv',
6915                value   => h_sum_of_part_active_units, p_log_level_rec => p_log_level_rec);
6916           end if;
6917 
6918           adj_row.account := deprn_rsv_acct;
6919           adj_row.account_type := 'DEPRN_RESERVE_ACCT';
6920           adj_row.adjustment_type := 'RESERVE';
6921           adj_row.adjustment_amount := 0;
6922           adj_row.selection_thid := h_id_out;
6923           adj_row.debit_credit_flag := 'DR';
6924 
6925           /* Fix for Bug#4617352: We have decided to create adj lines only for affected rows
6926              to avoid rounding issues with remaining rows in partial unit intercompany retirement.
6927           */
6928           adj_row.selection_mode := FA_STD_TYPES.FA_AJ_CLEAR_PARTIAL;
6929 
6930           adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
6931           adj_row.set_of_books_id := ret.set_of_books_id;
6932 
6933           if (bk.group_asset_id is not null) and
6934              (nvl(bk.member_rollup_flag, 'N') = 'N') then
6935              adj_row.track_member_flag := 'Y';
6936           else
6937              adj_row.track_member_flag := null;
6938           end if;
6939 
6940           if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
6941                                        X_last_update_date,
6942                                        X_last_updated_by,
6943                                        X_last_update_login,
6944                                        p_log_level_rec => p_log_level_rec)) then
6945 
6946                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6947                    return(FALSE);
6948 
6949           end if;
6950 
6951           if p_log_level_rec.statement_level then
6952             fa_debug_pkg.add
6953               (fname   => l_calling_fn,
6954                element => 'transaction_header_id_out in fagprv',
6955                value   => h_id_out, p_log_level_rec => p_log_level_rec);
6956             fa_debug_pkg.add
6957               (fname   => l_calling_fn,
6958                element => 'tot_deprn_reserve in fagprv',
6959                value   => tot_deprn_reserve, p_log_level_rec => p_log_level_rec);
6960           end if;
6961 
6962 
6963           adj_row.adjustment_amount := adj_row.amount_inserted -
6964                                        tot_deprn_reserve;
6965 
6966 
6967           if not FA_UTILS_PKG.faxrnd(adj_row.adjustment_amount, h_book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
6968              fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
6969              raise fagprv_err;
6970           end if;
6971 
6972 
6973           if p_log_level_rec.statement_level then
6974             fa_debug_pkg.add
6975               (fname   => l_calling_fn,
6976                element => 'cleared amount in fagprv(2)',
6977                value   => adj_row.amount_inserted);
6978             fa_debug_pkg.add
6979               (fname   => l_calling_fn,
6980                element => 'cost_frac in fagprv(2)',
6981                value   => cost_frac);
6982             fa_debug_pkg.add
6983               (fname   => l_calling_fn,
6984                element => 're-allocated amount in fagprv(2)',
6985                value   => adj_row.adjustment_amount);
6986           end if;
6987 
6988 /*
6989              select     nvl(units,0)
6990              into       h_cur_units
6991              from       fa_asset_history
6992              where      asset_id = h_asset_id
6993              and        date_ineffective is null;
6994 */
6995 
6996 
6997           h_cur_units := 0;
6998 
6999           begin
7000 
7001             select 1
7002               into l_dummy
7003             from fa_distribution_history
7004             where asset_id = h_asset_id
7005               and date_ineffective is null
7006               and transaction_header_id_in = h_id_out
7007               and rownum = 1;
7008 
7009 
7010           /* Fix for Bug#4617352 */
7011             select sum(nvl(units_assigned,0))
7012             into h_cur_units
7013             from fa_distribution_history
7014             where asset_id = h_asset_id
7015               and date_ineffective is null
7016               and transaction_header_id_in = h_id_out;
7017 
7018           exception
7019               when no_data_found then
7020                 h_cur_units := 0;
7021           end;
7022 
7023           if p_log_level_rec.statement_level then
7024             fa_debug_pkg.add
7025               (fname   => l_calling_fn,
7026                element => '++ h_cur_units in fagprv(2.5)',
7027                value   => h_cur_units);
7028           end if;
7029 
7030           if (h_cur_units <> 0) then
7031 
7032             adj_row.current_units := h_cur_units;
7033             --adj_row.selection_thid := 0;
7034             adj_row.selection_thid := h_id_out;
7035             adj_row.debit_credit_flag := 'CR';
7036             -- Bug 5170275:  adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE_PARTIAL;
7037             if h_sum_of_part_active_units > 0 then
7038               adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE_PARTIAL;
7039             else
7040               adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
7041             end if;
7042             adj_row.leveling_flag := FALSE;
7043             adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7044             adj_row.set_of_books_id := ret.set_of_books_id;
7045 
7046             if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7047                                        X_last_update_date,
7048                                        X_last_updated_by,
7049                                        X_last_update_login,
7050                                        p_log_level_rec => p_log_level_rec)) then
7051 
7052                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7053                    return(FALSE);
7054 
7055             end if;
7056 
7057 
7058             if (tot_bonus_deprn_reserve <> 0) then
7059 
7060              adj_row.current_units := bk.cur_units;
7061              adj_row.account := bonus_deprn_rsv_acct;
7062              adj_row.account_type := 'BONUS_DEPRN_RESERVE_ACCT';
7063              adj_row.adjustment_type := 'BONUS RESERVE';
7064              adj_row.adjustment_amount := 0;
7065              adj_row.amount_inserted := 0;
7066              adj_row.selection_thid := h_id_out;
7067              adj_row.debit_credit_flag := 'DR';
7068              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_CLEAR;
7069              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7070              adj_row.set_of_books_id := ret.set_of_books_id;
7071 
7072              if p_log_level_rec.statement_level then
7073                 fa_debug_pkg.add
7074                   (fname   => l_calling_fn,
7075                    element => 'Insert fa_adjustments in fagprv2,accnt_type',
7076                    value   => adj_row.account_type, p_log_level_rec => p_log_level_rec);
7077              end if;
7078 
7079              if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7080                                        X_last_update_date,
7081                                        X_last_updated_by,
7082                                        X_last_update_login
7083                                        , p_log_level_rec => p_log_level_rec)) then
7084 
7085                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7086                    return(FALSE);
7087 
7088              end if;
7089 
7090              adj_row.adjustment_amount := adj_row.amount_inserted -
7091                                        tot_bonus_deprn_reserve;
7092 
7093              adj_row.current_units := h_cur_units;
7094              adj_row.selection_thid := 0;
7095              adj_row.debit_credit_flag := 'CR';
7096              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
7097              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7098              adj_row.set_of_books_id := ret.set_of_books_id;
7099 
7100              if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7101                                        X_last_update_date,
7102                                        X_last_updated_by,
7103                                        X_last_update_login,
7104                                        p_log_level_rec => p_log_level_rec)) then
7105 
7106                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7107                    return(FALSE);
7108              end if;
7109 
7110             end if;
7111 
7112             if (tot_impairment_reserve <> 0) then
7113 
7114              adj_row.current_units := bk.cur_units;
7115              adj_row.account := impairment_rsv_acct;
7116              adj_row.account_type := 'IMPAIR_RESERVE_ACCT';
7117              adj_row.adjustment_type := 'IMPAIR RESERVE';
7118              adj_row.adjustment_amount := 0;
7119              adj_row.amount_inserted := 0;
7120              adj_row.selection_thid := h_id_out;
7121              adj_row.debit_credit_flag := 'DR';
7122              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_CLEAR;
7123              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7124              adj_row.set_of_books_id := ret.set_of_books_id;
7125 
7126              if p_log_level_rec.statement_level then
7127                 fa_debug_pkg.add
7128                   (fname   => l_calling_fn,
7129                    element => 'Insert fa_adjustments in fagprv2,accnt_type',
7130                    value   => adj_row.account_type, p_log_level_rec => p_log_level_rec);
7131              end if;
7132 
7133              if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7134                                        X_last_update_date,
7135                                        X_last_updated_by,
7136                                        X_last_update_login,
7137                                        p_log_level_rec => p_log_level_rec)) then
7138 
7139                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7140                    return(FALSE);
7141 
7142              end if;
7143 
7144              adj_row.adjustment_amount := adj_row.amount_inserted -
7145                                        tot_impairment_reserve;
7146 
7147              adj_row.current_units := h_cur_units;
7148              adj_row.selection_thid := 0;
7149              adj_row.debit_credit_flag := 'CR';
7150              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
7151              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7152              adj_row.set_of_books_id := ret.set_of_books_id;
7153 
7154              if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7155                                        X_last_update_date,
7156                                        X_last_updated_by,
7157                                        X_last_update_login,
7158                                        p_log_level_rec => p_log_level_rec)) then
7159 
7160                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7161                    return(FALSE);
7162              end if;
7163 
7164             end if;
7165 
7166           end if; -- if h_cur_units <> 0
7167 
7168           if p_log_level_rec.statement_level then
7169             fa_debug_pkg.add
7170               (fname   => l_calling_fn,
7171                element => 'Before faxinaj for REVAL RESERVE',
7172                value   => '', p_log_level_rec => p_log_level_rec);
7173           end if;
7174 
7175           if (bk.current_cost > bk.unrevalued_cost OR reval_reserve <> 0 ) and
7176                                 (h_retire_reval_flag = 1) then
7177 
7178              adj_row.current_units := bk.cur_units;
7179              adj_row.account := reval_rsv_acct;
7180              adj_row.account_type := 'REVAL_RESERVE_ACCT';
7181              adj_row.adjustment_type := 'REVAL RESERVE';
7182              adj_row.adjustment_amount := 0;
7183              adj_row.selection_thid := h_id_out;
7184              adj_row.debit_credit_flag := 'DR';
7185              /* Bug#7646218 - partial unit retirement Need to insert row in fa_adj for distribution id transferred out only.*/
7186              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_CLEAR_PARTIAL;
7187              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7188              adj_row.set_of_books_id := ret.set_of_books_id;
7189 
7190              if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7191                                        X_last_update_date,
7192                                        X_last_updated_by,
7193                                        X_last_update_login,
7194                                        p_log_level_rec => p_log_level_rec)) then
7195 
7196                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7197                    return(FALSE);
7198              end if;
7199 
7200              adj_row.adjustment_amount := adj_row.amount_inserted -
7201                                           tot_reval_reserve;
7202 
7203              -- Bug # 5170275
7204              if (h_cur_units <> 0) then
7205                adj_row.current_units := h_cur_units;
7206                adj_row.selection_thid := 0;
7207                adj_row.debit_credit_flag := 'CR';
7208                adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7209                adj_row.set_of_books_id := ret.set_of_books_id;
7210 
7211                adj_row.selection_mode := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE_REVAL;
7212                -- adj_row.selection_mode := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
7213 
7214                if adj_row.adjustment_amount <> 0 then
7215                   if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7216                                        X_last_update_date,
7217                                        X_last_updated_by,
7218                                        X_last_update_login,
7219                                        p_log_level_rec => p_log_level_rec)) then
7220 
7221                      fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7222                      return(FALSE);
7223                   end if;
7224                end if;
7225              end if; -- h_cur_units
7226           /*Bug13727632 - Move reval reserve to active distributions
7227 	                  partial unit retirement is done and retire_reval_reserve is NO */
7228           elsif( h_retire_reval_flag = 0 and reval_reserve <> 0 and (not bk.book_class)) then
7229              h_dist_book := bk.dis_book;
7230              adj_row.current_units := bk.cur_units;
7231              adj_row.account := reval_rsv_acct;
7232              adj_row.account_type := 'REVAL_RESERVE_ACCT';
7233              adj_row.adjustment_type := 'REVAL RESERVE';
7234              adj_row.selection_thid := 0;
7235              adj_row.debit_credit_flag := 'DR';
7236              adj_row.selection_mode := FA_STD_TYPES.FA_AJ_SINGLE;
7237              adj_row.selection_retid := 0;
7238              adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7239              adj_row.set_of_books_id := ret.set_of_books_id;
7240              adj_row.units_retired := 0;
7241              adj_row.flush_adj_flag := TRUE;
7242              adj_row.leveling_flag := FALSE;
7243 
7244              for distn IN C1 loop
7245                 h_distribution_id   := distn.distribution_id;
7246                 h_mesg_name         := 'FA_INS_ADJ_FCUR_C1';
7247                 h_dist_ccid         := distn.code_combination_id;
7248                 h_trans_units       := distn.transaction_units;
7249                 h_units_assigned    := distn.units_assigned;
7250                 h_row_ctr           := h_row_ctr+1;
7251                 H_DPR_ROW.asset_id  := adj_row.asset_id;
7252                 H_DPR_ROW.book      := adj_row.book_type_code;
7253                 H_DPR_ROW.dist_id   := h_distribution_id;
7254                 H_DPR_ROW.period_ctr:= 0;
7255                 H_DPR_ROW.mrc_sob_type_code := adj_row.mrc_sob_type_code;
7256                 H_DPR_ROW.set_of_books_id   := adj_row.set_of_books_id;
7257 
7258                 FA_QUERY_BALANCES_PKG.QUERY_BALANCES_INT(H_DPR_ROW,
7259                              'STANDARD',
7260                              FALSE,
7261                              H_SUCCESS,
7262                              -1,
7263                              p_log_level_rec => p_log_level_rec);
7264                 if not h_success then
7265                    FA_SRVR_MSG.ADD_MESSAGE(CALLING_FN => l_calling_fn,p_log_level_rec => p_log_level_rec);
7266                    return FALSE;
7267                 end if;
7268 
7269                 if H_DPR_ROW.reval_rsv <> 0 then
7270                    adj_row.adjustment_amount := H_DPR_ROW.reval_rsv;
7271                    adj_row.distribution_id := h_distribution_id;
7272                    adj_row.code_combination_id := h_dist_ccid;
7273 
7274                    if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7275                                        X_last_update_date,
7276                                        X_last_updated_by,
7277                                        X_last_update_login,
7278                                        p_log_level_rec => p_log_level_rec)) then
7279 
7280                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7281                       return(FALSE);
7282                    end if;
7283                    h_total_amount := h_total_amount + adj_row.adjustment_amount;
7284                 end if;
7285              end loop;
7286              adj_row.current_units := bk.cur_units - ret.units_retired;
7287              adj_row.adjustment_amount := h_total_amount ;
7288              adj_row.selection_mode := fa_std_types.FA_AJ_ACTIVE;
7289              adj_row.selection_retid := 0;
7290              adj_row.debit_credit_flag := 'CR';
7291              if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7292                                        X_last_update_date,
7293                                        X_last_updated_by,
7294                                        X_last_update_login
7295                                        , p_log_level_rec => p_log_level_rec)) then
7296 
7297                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7298                 return(FALSE);
7299              end if;
7300           end if; -- end of - if (bk.current_cost
7301        end if; -- end of - if (ret.units_retired
7302 
7303        ret.rsv_retired := tot_deprn_reserve;
7304        ret.reval_rsv_retired := tot_reval_reserve;
7305        ret.bonus_rsv_retired := tot_bonus_deprn_reserve;
7306        ret.impair_rsv_retired := tot_impairment_reserve;
7307 
7308        if p_log_level_rec.statement_level then
7309              fa_debug_pkg.add
7310                   (fname   => l_calling_fn,
7311                    element => 'Value of reval_rsv_retired is ',
7312                    value   => to_char(tot_reval_reserve));
7313        end if;
7314 
7315        if p_log_level_rec.statement_level then
7316              fa_debug_pkg.add
7317                   (fname   => l_calling_fn,
7318                    element => 'Value of rsv_retired is ',
7319                    value   => to_char(tot_deprn_reserve));
7320        end if;
7321 
7322        if p_log_level_rec.statement_level then
7323              fa_debug_pkg.add
7324                   (fname   => l_calling_fn,
7325                    element => 'Return true from EFA_RUPD.fagprv ',
7326                    value   => to_char(tot_deprn_reserve));
7327        end if;
7328 
7329        -- +++++ Process Create Reserve Retired entry for Group +++++ --
7330        if (bk.group_asset_id is not null) and
7331           (nvl(bk.member_rollup_flag, 'N') = 'N') then
7332 
7333           if (l_rsv_retired is not null) then
7334              ret.reserve_retired := l_rsv_retired;
7335           end if;
7336 
7337           fa_query_balances_pkg.query_balances(
7338                       X_asset_id => bk.group_asset_id,
7339                       X_book => ret.book,
7340                       X_period_ctr => 0,
7341                       X_dist_id => 0,
7342                       X_run_mode => 'STANDARD',
7343                       X_cost => l_temp_num,
7344                       X_deprn_rsv => l_g_rsv,
7345                       X_reval_rsv => l_temp_num,
7346                       X_ytd_deprn => l_temp_num,
7347                       X_ytd_reval_exp => l_temp_num,
7348                       X_reval_deprn_exp => l_temp_num,
7349                       X_deprn_exp => l_temp_num,
7350                       X_reval_amo => l_temp_num,
7351                       X_prod => l_temp_num,
7352                       X_ytd_prod => l_temp_num,
7353                       X_ltd_prod => l_temp_num,
7354                       X_adj_cost => l_temp_num,
7355                       X_reval_amo_basis => l_temp_num,
7356                       X_bonus_rate => l_temp_num,
7357                       X_deprn_source_code => l_temp_char,
7358                       X_adjusted_flag => l_temp_bool,
7359                       X_transaction_header_id => -1,
7360                       X_bonus_deprn_rsv => l_g_bonus_rsv,
7361                       X_bonus_ytd_deprn => l_temp_num,
7362                       X_bonus_deprn_amount => l_temp_num,
7363                       X_impairment_rsv => l_g_impair_rsv,
7364                       X_ytd_impairment => l_temp_num,
7365                       X_impairment_amount => l_temp_num,
7366                       X_capital_adjustment => l_temp_num,
7367                       X_general_fund => l_temp_num,
7368                       X_mrc_sob_type_code => ret.mrc_sob_type_code,
7369                       X_set_of_books_id => ret.set_of_books_id,
7370                       p_log_level_rec => p_log_level_rec);
7371 
7372           if (nvl(l_rsv_retired, 0) <> 0) or
7373              (nvl(l_g_rsv, 0) <> 0) or
7374              (nvl(l_g_bonus_rsv, 0) <> 0) or
7375              (nvl(l_g_impair_rsv, 0) <> 0) or
7376              (nvl(g_deprn_amt, 0) <> 0) or                       --Bug11886090
7377              (nvl(g_bonus_deprn_amt, 0) <> 0) then               --Bug11886090
7378 
7379              adj_row.asset_id := bk.group_asset_id;
7380 
7381              OPEN c_get_unit;
7382              FETCH c_get_unit INTO adj_row.current_units;
7383              CLOSE c_get_unit;
7384 
7385              l_asset_hdr_rec.asset_id := bk.group_asset_id;
7386              l_asset_hdr_rec.book_type_code := ret.book;
7387              l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
7388 
7389              if not FA_UTIL_PVT.get_asset_cat_rec (
7390                       p_asset_hdr_rec         => l_asset_hdr_rec,
7391                       px_asset_cat_rec        => l_asset_cat_rec,
7392                       p_date_effective        => null,
7393                       p_log_level_rec         => p_log_level_rec) then
7394                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7395                 return(FALSE);
7396              end if;
7397 
7398              if not fa_cache_pkg.fazccb(
7399                       X_book   => ret.book,
7400                       X_cat_id => l_asset_cat_rec.category_id,
7401                       p_log_level_rec => p_log_level_rec) then
7402                 fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7403                 return(FALSE);
7404              end if;
7405 
7406              OPEN c_get_cost;
7407              FETCH c_get_cost INTO l_g_cost;
7408              CLOSE c_get_cost;
7409 
7410              l_g_cost := nvl(l_g_cost, 0) + ret.cost_retired;
7411 
7412              --Bug11886090: include g_deprn_amt
7413              if (nvl(l_g_rsv, 0) <> 0) or (nvl(g_deprn_amt, 0) <> 0) then
7414                 if (nvl(bk.member_rollup_flag, 'N') <> 'Y') and
7415                    (l_rsv_retired is null) and
7416                    (nvl(l_g_cost,0) <> 0) then -- Bug 7504243
7417 
7418                    --Bug11886090: include g_deprn_amt
7419                    adj_row.adjustment_amount := (ret.cost_retired / l_g_cost) * nvl(l_g_rsv, 0) + nvl(g_deprn_amt, 0);
7420 
7421                    if not FA_UTILS_PKG.faxrnd(adj_row.adjustment_amount, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
7422                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7423                       raise fagprv_err;
7424                    end if;
7425 
7426                 else
7427                    adj_row.adjustment_amount := nvl(l_rsv_retired, ret.rsv_retired);
7428                    adj_row.adjustment_amount := nvl(adj_row.adjustment_amount,0) + nvl(g_deprn_amt, 0); -- Bug 750424, 11886090
7429                 end if;
7430 
7431                 --Bug7394159: Populate fa_ret_types.ret_struct with reserve(/rsv)_retired
7432                 ret.rsv_retired := adj_row.adjustment_amount;
7433                 ret.reserve_retired := adj_row.adjustment_amount;
7434 
7435                 adj_row.account := fa_cache_pkg.fazccb_record.deprn_reserve_acct;
7436                 adj_row.account_type := 'DEPRN_RESERVE_ACCT';
7437                 adj_row.adjustment_type := 'RESERVE';
7438                 adj_row.selection_thid := 0;
7439                 adj_row.debit_credit_flag := 'DR';
7440                 adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
7441                 adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7442                 adj_row.set_of_books_id := ret.set_of_books_id;
7443                 adj_row.track_member_flag := null;
7444 
7445                 if p_log_level_rec.statement_level then
7446                   fa_debug_pkg.add(fname   => l_calling_fn,
7447                                    element => 'Before faxinaj for Group RESERVE',
7448                                    value   => adj_row.adjustment_amount, p_log_level_rec => p_log_level_rec);
7449                 end if;
7450 
7451                 if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7452                                              X_last_update_date,
7453                                              X_last_updated_by,
7454                                              X_last_update_login,
7455                                              p_log_level_rec => p_log_level_rec)) then
7456 
7457                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7458                    return(FALSE);
7459 
7460                 end if;
7461              end if; -- (nvl(l_g_rsv, 0) <> 0)
7462 
7463              --Bug11886090: include g_bonus_deprn_amt
7464              if (nvl(l_g_bonus_rsv, 0) <> 0) or (nvl(g_bonus_deprn_amt, 0) <> 0) then
7465                 if (nvl(bk.member_rollup_flag, 'N') <> 'Y') and
7466                    (l_rsv_retired is null) and (nvl(l_g_impair_rsv,0) = 0) and
7467                    (nvl(l_g_cost,0) <> 0) then -- Bug 7504243
7468 
7469                    --Bug11886090: include g_bonus_deprn_amt
7470                    adj_row.adjustment_amount := (ret.cost_retired / l_g_cost) * nvl(l_g_bonus_rsv, 0) + nvl(g_bonus_deprn_amt, 0);
7471 
7472                    if not FA_UTILS_PKG.faxrnd(adj_row.adjustment_amount, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
7473                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7474                       raise fagprv_err;
7475                    end if;
7476 
7477                 elsif (l_rsv_retired is not null) or
7478                       (nvl(l_g_impair_rsv, 0) <> 0) and
7479                       (nvl(l_g_rsv,0) <> 0) then -- Bug 7504243
7480                    --Bug11886090: include g_bonus_deprn_amt
7481                    adj_row.adjustment_amount := (l_rsv_retired/l_g_rsv) * nvl(l_g_bonus_rsv, 0) + nvl(g_bonus_deprn_amt, 0);
7482 
7483                    if not FA_UTILS_PKG.faxrnd(adj_row.adjustment_amount, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
7484                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7485                       raise fagprv_err;
7486                    end if;
7487 
7488                 else
7489                    --Bug11886090: include g_bonus_deprn_amt
7490                    adj_row.adjustment_amount := ret.bonus_rsv_retired + nvl(g_bonus_deprn_amt, 0);
7491                 end if;
7492 
7493                 adj_row.account := fa_cache_pkg.fazccb_record.bonus_deprn_expense_acct;
7494                 adj_row.account_type := 'BONUS_DEPRN_RESERVE_ACCT';
7495                 adj_row.adjustment_type := 'BONUS RESERVE';
7496                 adj_row.selection_thid := 0;
7497                 adj_row.debit_credit_flag := 'DR';
7498                 adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
7499                 adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7500                 adj_row.set_of_books_id := ret.set_of_books_id;
7501                 adj_row.track_member_flag := null;
7502 
7503                 if p_log_level_rec.statement_level then
7504                   fa_debug_pkg.add(fname   => l_calling_fn,
7505                                    element => 'Before faxinaj for Group BONUS RESERVE',
7506                                    value   => adj_row.adjustment_amount, p_log_level_rec => p_log_level_rec);
7507                 end if;
7508 
7509                 if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7510                                              X_last_update_date,
7511                                              X_last_updated_by,
7512                                              X_last_update_login,
7513                                              p_log_level_rec => p_log_level_rec)) then
7514 
7515                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7516                    return(FALSE);
7517 
7518                 end if;
7519              end if; -- (nvl(l_g_bonus_rsv, 0) <> 0)
7520 
7521              if ret.mrc_sob_type_code ='R' then
7522                 update fa_mc_books_summary
7523                 set expense_adjustment_amount = nvl(expense_adjustment_amount, 0) + nvl(g_deprn_amt, 0)
7524                   , deprn_amount = nvl(deprn_amount, 0) + nvl(g_deprn_amt, 0)
7525                   , ytd_deprn = nvl(ytd_deprn, 0) + nvl(g_deprn_amt, 0)
7526                   , reserve_adjustment_amount = nvl(reserve_adjustment_amount, 0) - nvl(g_deprn_amt, 0)
7527                 where asset_id = bk.group_asset_id
7528                 and   book_type_code = ret.book
7529                 and   period_counter = cpd_ctr
7530                 and   set_of_books_id = ret.set_of_books_id;
7531              else
7532                 update fa_books_summary
7533                 set expense_adjustment_amount = nvl(expense_adjustment_amount, 0) + nvl(g_deprn_amt, 0)
7534                   , deprn_amount = nvl(deprn_amount, 0) + nvl(g_deprn_amt, 0)
7535                   , ytd_deprn = nvl(ytd_deprn, 0) + nvl(g_deprn_amt, 0)
7536                   , reserve_adjustment_amount = nvl(reserve_adjustment_amount, 0) - nvl(g_deprn_amt, 0)
7537                 where asset_id = bk.group_asset_id
7538                 and   book_type_code = ret.book
7539                 and   period_counter = cpd_ctr;
7540              end if;
7541 
7542              if (nvl(l_g_impair_rsv, 0) <> 0) then
7543                 if (nvl(bk.member_rollup_flag, 'N') <> 'Y') and
7544                    (l_rsv_retired is null) and (nvl(l_g_bonus_rsv,0) = 0) and
7545                    (nvl(l_g_cost,0) <> 0) then -- Bug 7504243
7546 
7547                    adj_row.adjustment_amount := (ret.cost_retired / l_g_cost) * l_g_bonus_rsv;
7548 
7549                    if not FA_UTILS_PKG.faxrnd(adj_row.adjustment_amount, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
7550                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7551                       raise fagprv_err;
7552                    end if;
7553 
7554                 elsif (l_rsv_retired is not null) or
7555                       (nvl(l_g_bonus_rsv, 0) <> 0) and
7556                       (nvl(l_g_rsv,0) <> 0) then -- Bug 7504243
7557                    adj_row.adjustment_amount := (l_rsv_retired/l_g_rsv) * l_g_impair_rsv;
7558 
7559                    if not FA_UTILS_PKG.faxrnd(adj_row.adjustment_amount, ret.book, ret.set_of_books_id, p_log_level_rec => p_log_level_rec) then
7560                       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7561                       raise fagprv_err;
7562                    end if;
7563 
7564                 else
7565                    adj_row.adjustment_amount := ret.impair_rsv_retired;
7566                 end if;
7567 
7568                 adj_row.account := fa_cache_pkg.fazccb_record.impair_expense_acct;
7569                 adj_row.account_type := 'IMPAIR_RESERVE_ACCT';
7570                 adj_row.adjustment_type := 'IMPAIR RESERVE';
7571                 adj_row.selection_thid := 0;
7572                 adj_row.debit_credit_flag := 'DR';
7573                 adj_row.selection_mode := FA_STD_TYPES.FA_AJ_ACTIVE;
7574                 adj_row.mrc_sob_type_code := ret.mrc_sob_type_code;
7575                 adj_row.set_of_books_id := ret.set_of_books_id;
7576                 adj_row.track_member_flag := null;
7577 
7578                 if p_log_level_rec.statement_level then
7579                   fa_debug_pkg.add(fname   => l_calling_fn,
7580                                    element => 'Before faxinaj for Group IMPAIR RESERVE',
7581                                    value   => adj_row.adjustment_amount, p_log_level_rec => p_log_level_rec);
7582                 end if;
7583 
7584                 if (NOT FA_INS_ADJUST_PKG.faxinaj(adj_row,
7585                                              X_last_update_date,
7586                                              X_last_updated_by,
7587                                              X_last_update_login,
7588                                              p_log_level_rec => p_log_level_rec)) then
7589 
7590                    fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7591                    return(FALSE);
7592 
7593                 end if;
7594              end if; -- (nvl(l_g_impair_rsv, 0) <> 0)
7595 
7596           end if;
7597 
7598        end if; -- (bk.group_asset_id is not null)
7599 
7600        return(TRUE);
7601 
7602     EXCEPTION
7603 
7604        when others then
7605 
7606             fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
7607             return FALSE;
7608 
7609     END FAGPRV;
7610 
7611 END FA_GAINLOSS_UPD_PKG;    -- End of Package EFA_RUPD