DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_IMPAIRMENT_PREV_PVT

Source


1 PACKAGE BODY FA_IMPAIRMENT_PREV_PVT AS
2 /* $Header: FAVIMPWB.pls 120.22.12020000.3 2013/03/29 11:32:34 saalampa ship $ */
3 
4 g_print_debug boolean := fa_cache_pkg.fa_print_debug;
5 
6  --
7   -- Datatypes for pl/sql tables below
8   --
9   TYPE tab_rowid_type IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
10   TYPE tab_num15_type IS TABLE OF NUMBER(15) INDEX BY BINARY_INTEGER;
11   TYPE tab_date_type IS TABLE OF DATE INDEX BY BINARY_INTEGER;
12   TYPE tab_char1_type IS TABLE OF VARCHAR2(15) INDEX BY BINARY_INTEGER;
13   TYPE tab_char3_type IS TABLE OF VARCHAR2(3) INDEX BY BINARY_INTEGER;
14   TYPE tab_char15_type IS TABLE OF VARCHAR2(15) INDEX BY BINARY_INTEGER;
15   TYPE tab_char30_type IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;
16 
17   g_temp_number   number;
18   g_temp_integer  binary_integer;
19   g_temp_boolean  boolean;
20   g_temp_varchar2 varchar2(100);
21   g_error_flag tab_num15_type; --Bug#8614268
22 
23 
24 --*********************** Private functions ******************************--
25 -- private declaration for books (mrc) wrapper
26 FUNCTION process_depreciation(
27               p_request_id        IN NUMBER,
28               p_book_type_code    IN VARCHAR2,
29               p_worker_id         IN NUMBER,
30               p_period_rec        IN FA_API_TYPES.period_rec_type,
31               p_imp_period_rec    IN FA_API_TYPES.period_rec_type,
32               p_mrc_sob_type_code IN VARCHAR2,
33               p_set_of_books_id   IN NUMBER,
34               p_calling_fn        IN VARCHAR2
35 , p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) RETURN BOOLEAN IS
36 
37    l_calling_fn   varchar2(60) := 'FA_IMPAIRMENT_PREV_PVT.process_depreciation';
38 
39    CURSOR c_deprn is
40        SELECT  bk.asset_id                          asset_id
41              , ad.asset_category_id                 category_id
42              , bk.deprn_method_code                 deprn_method_code
43              , nvl(bk.life_in_months, 0)            life_in_months
44              , bk.rate_adjustment_factor            rate_adjustment_factor
45              , ad.current_units                     current_units
46              , bk.adjustment_required_status        adjustment_required_status
47              , bk.cost_change_flag                  cost_change_flag
48              , bk.retirement_pending_flag           retirement_pending_flag
49              , bk.period_counter_fully_retired      period_counter_fully_retired
50              , ad.asset_number                      asset_number
51              , bk.adjusted_cost                     adjusted_cost
52              , nvl(bk.adjusted_rate, 0)             adjusted_rate
53              , bk.recoverable_cost                  recoverable_cost
54              , nvl(bk.reval_amortization_basis, 0)  reval_amortization_basis
55              , bk.ceiling_name                      ceiling_name
56              , bk.bonus_rule                        bonus_rule
57              , bk.deprn_start_date                  deprn_start_date
58              , bk.date_placed_in_service            date_placed_in_service
59              , bk.prorate_date                      prorate_date
60              , bk.cost                              cost
61              , bk.production_capacity               production_capacity
62              , bk.adjusted_capacity                 adjusted_capacity
63              , bk.annual_deprn_rounding_flag        annual_deprn_rounding_flag
64              , bk.salvage_value                     salvage_value
65              , bk.period_counter_life_complete      period_counter_life_complete
66              , bk.adjusted_recoverable_cost         adjusted_recoverable_cost
67              , nvl(bk.short_fiscal_year_flag, 'NO') short_fiscal_year_flag
68              , bk.conversion_date                   conversion_date
69              , bk.original_deprn_start_date         original_deprn_start_date
70              , nvl(bk.formula_factor, 1)            formula_factor
71              , nvl(bk.old_adjusted_cost, 1)         old_adjusted_cost
72              , bk.tracking_method                   tracking_method
73              , bk.allocate_to_fully_ret_flag        allocate_to_fully_ret_flag
74              , bk.allocate_to_fully_rsv_flag        allocate_to_fully_rsv_flag
75              , bk.excess_allocation_option          excess_allocation_option
76              , bk.depreciation_option               depreciation_option
77              , bk.member_rollup_flag                member_rollup_flag
78              , ad.asset_type                        asset_type
79              , bk.group_asset_id                    group_asset_id
80              , nvl(bk.eofy_reserve, 0)              eofy_reserve
81              , bk.exclude_fully_rsv_flag            exclude_fully_rsv_flag
82              , 0  over_depreciate_option       -- over_depreciate_option
83              , 0  terminal_gain_loss --terminal gain loss
84              , 0  terminal_gain_loss_flag --terminal gain loss flag
85              , 0  super_group_id               -- super_group_id
86              , 0  super_group_pct_sal_value --super grp pct salvage value
87              , 0 net_book_value -- net_book_value
88              , 0 net_selling_price -- net_selling_price
89              , 0 value_in_use -- value_in_use
90              , imp.ytd_impairment -- ytd_impairment
91              , imp.impairment_reserve -- impairment_reserve
92              , sysdate deprn_run_date -- deprn_run_date
93              , 0 deprn_amount -- deprn_amount
94              , 0 ytd_deprn -- ytd_deprn
95              , 0 deprn_reserve -- deprn_reserve
96              , 'DEPRN' deprn_source_code -- deprn_source_code
97              , 0 bonus_rate -- bonus_rate
98              , 0 ltd_production -- ltd_production
99              , 0 production -- production
100              , 0 reval_amortization -- reval_amortization
101              , 0 reval_deprn_expense -- reval_deprn_expense
102              , 0 reval_reserve -- reval_reserve
103              , 0 ytd_production -- ytd_production
104              , 0 ytd_reval_deprn_expense -- ytd_reval_deprn_expense
105              , 0 prior_fy_expense -- prior_fy_expense
106              , 0 bonus_deprn_amount -- bonus_deprn_amount
107              , 0 bonus_ytd_deprn -- bonus_ytd_deprn
108              , 0 bonus_deprn_reserve -- bonus_deprn_reserve
109              , 0 prior_fy_bonus_expense -- prior_fy_bonus_expense
110              , 0 deprn_override_flag -- deprn_override_flag
111              , 0 system_deprn_amount -- system_deprn_amount
112              , 0 system_bonus_deprn_amount -- system_bonus_deprn_amount
113              , 0 deprn_adjustment_amount -- deprn_adjustment_amount
114              , 0 bonus_deprn_adjustment_amount -- bonus_deprn_adjustment_amount
115              , imp.rowid imp_rowid
116              , 'N' Period_of_addition_flag  -- Period_of_addition_flag
117              , imp.impairment_id -- impairment_id
118              , 0 capital_adjustment -- Bug 6666666 : Capital Adjustment amount
119              , 0 general_fund -- Bug 6666666 : General Fund Balance Amount
120              , bk.ALLOWED_DEPRN_LIMIT_AMOUNT
121 	     , bk.depreciate_flag --phase5
122 	     , bk.period_counter_fully_reserved --phase5
123 	     , 0 reval_loss_balance
124        FROM    fa_additions_b ad,
125                fa_methods mt,
126                fa_books bk,
127                fa_itf_impairments imp
128        WHERE   bk.book_type_code = p_book_type_code
129        AND     (bk.period_counter_fully_retired is null OR
130                     bk.adjustment_required_status <> 'NONE')
131        --phase5 AND     bk.depreciate_flag = 'YES'
132        AND     bk.date_effective <= nvl(p_period_rec.period_close_date, sysdate)
133        AND     bk.transaction_header_id_out is null
134        AND     AD.ASSET_ID=BK.ASSET_ID
135        AND     ad.asset_type = 'CAPITALIZED'
136        AND     bk.group_asset_id is null
137        AND     MT.METHOD_CODE = BK.DEPRN_METHOD_CODE
138        AND     nvl(mt.life_in_months, -99) = nvl(bk.life_in_months, -99)
139        AND     bk.deprn_start_date <= p_period_rec.calendar_period_close_date
140        AND     bk.asset_id = imp.asset_id
141        AND     imp.book_type_code = p_book_type_code
142        AND     imp.request_id = p_request_id
143        AND     imp.period_counter <= p_period_rec.period_counter
144        AND     imp.worker_id = p_worker_id;
145 
146    CURSOR c_mc_deprn is
147        SELECT  bk.asset_id                          asset_id
148              , ad.asset_category_id                 category_id
149              , bk.deprn_method_code                 deprn_method_code
150              , nvl(bk.life_in_months, 0)            life_in_months
151              , bk.rate_adjustment_factor            rate_adjustment_factor
152              , ad.current_units                     current_units
153              , bk.adjustment_required_status        adjustment_required_status
154              , bk.cost_change_flag                  cost_change_flag
155              , bk.retirement_pending_flag           retirement_pending_flag
156              , bk.period_counter_fully_retired      period_counter_fully_retired
157              , ad.asset_number                      asset_number
158              , bk.adjusted_cost                     adjusted_cost
159              , nvl(bk.adjusted_rate, 0)             adjusted_rate
160              , bk.recoverable_cost                  recoverable_cost
161              , nvl(bk.reval_amortization_basis, 0)  reval_amortization_basis
162              , bk.ceiling_name                      ceiling_name
163              , bk.bonus_rule                        bonus_rule
164              , bk.deprn_start_date                  deprn_start_date
165              , bk.date_placed_in_service            date_placed_in_service
166              , bk.prorate_date                      prorate_date
167              , bk.cost                              cost
168              , bk.production_capacity               production_capacity
169              , bk.adjusted_capacity                 adjusted_capacity
170              , bk.annual_deprn_rounding_flag        annual_deprn_rounding_flag
171              , bk.salvage_value                     salvage_value
172              , bk.period_counter_life_complete      period_counter_life_complete
173              , bk.adjusted_recoverable_cost         adjusted_recoverable_cost
174              , nvl(bk.short_fiscal_year_flag, 'NO') short_fiscal_year_flag
175              , bk.conversion_date                   conversion_date
176              , bk.original_deprn_start_date         original_deprn_start_date
177              , nvl(bk.formula_factor, 1)            formula_factor
178              , nvl(bk.old_adjusted_cost, 1)         old_adjusted_cost
179              , bk.tracking_method                   tracking_method
180              , bk.allocate_to_fully_ret_flag        allocate_to_fully_ret_flag
181              , bk.allocate_to_fully_rsv_flag        allocate_to_fully_rsv_flag
182              , bk.excess_allocation_option          excess_allocation_option
183              , bk.depreciation_option               depreciation_option
184              , bk.member_rollup_flag                member_rollup_flag
185              , ad.asset_type                        asset_type
186              , bk.group_asset_id                    group_asset_id
187              , nvl(bk.eofy_reserve, 0)              eofy_reserve
188              , bk.exclude_fully_rsv_flag            exclude_fully_rsv_flag
189              , 0  over_depreciate_option       -- over_depreciate_option
190              , 0  terminal_gain_loss --terminal gain loss
191              , 0  terminal_gain_loss_flag --terminal gain loss flag
192              , 0  super_group_id               -- super_group_id
193              , 0  super_group_pct_sal_value --super grp pct salvage value
194              , 0 net_book_value -- net_book_value
195              , 0 net_selling_price -- net_selling_price
196              , 0 value_in_use -- value_in_use
197              , imp.ytd_impairment -- ytd_impairment
198              , imp.impairment_reserve -- impairment_reserve
199              , sysdate deprn_run_date -- deprn_run_date
200              , 0 deprn_amount -- deprn_amount
201              , 0 ytd_deprn -- ytd_deprn
202              , 0 deprn_reserve -- deprn_reserve
203              , 'DEPRN' deprn_source_code -- deprn_source_code
204              , 0 bonus_rate -- bonus_rate
205              , 0 ltd_production -- ltd_production
206              , 0 production -- production
207              , 0 reval_amortization -- reval_amortization
208              , 0 reval_deprn_expense -- reval_deprn_expense
209              , 0 reval_reserve -- reval_reserve
210              , 0 ytd_production -- ytd_production
211              , 0 ytd_reval_deprn_expense -- ytd_reval_deprn_expense
212              , 0 prior_fy_expense -- prior_fy_expense
213              , 0 bonus_deprn_amount -- bonus_deprn_amount
214              , 0 bonus_ytd_deprn -- bonus_ytd_deprn
215              , 0 bonus_deprn_reserve -- bonus_deprn_reserve
216              , 0 prior_fy_bonus_expense -- prior_fy_bonus_expense
217              , 0 deprn_override_flag -- deprn_override_flag
218              , 0 system_deprn_amount -- system_deprn_amount
219              , 0 system_bonus_deprn_amount -- system_bonus_deprn_amount
220              , 0 deprn_adjustment_amount -- deprn_adjustment_amount
221              , 0 bonus_deprn_adjustment_amount -- bonus_deprn_adjustment_amount
222              , imp.rowid imp_rowid
223              , 'N' Period_of_addition_flag  -- Period_of_addition_flag
224              , imp.impairment_id -- impairment_id
225              , 0 capital_adjustment -- Bug 6666666 : Capital Adjustment amount
226              , 0 general_fund -- Bug 6666666 : General Fund Balance Amount
227              , bk.ALLOWED_DEPRN_LIMIT_AMOUNT
228 	     , bk.depreciate_flag --phase5
229 	     , bk.period_counter_fully_reserved --phase5
230              , 0 reval_loss_balance
231        FROM    fa_additions_b ad,
232                fa_methods mt,
233                fa_mc_books bk,
234                fa_mc_itf_impairments imp
235        WHERE   bk.book_type_code = p_book_type_code
236        AND     bk.set_of_books_id = p_set_of_books_id
237        AND     (bk.period_counter_fully_retired is null OR
238                     bk.adjustment_required_status <> 'NONE')
239        --phase5 AND     bk.depreciate_flag = 'YES'
240        AND     bk.date_effective <= nvl(p_period_rec.period_close_date, sysdate)
241        AND     bk.transaction_header_id_out is null
242        AND     AD.ASSET_ID=BK.ASSET_ID
243        AND     ad.asset_type = 'CAPITALIZED'
244        AND     bk.group_asset_id is null
245        AND     MT.METHOD_CODE = BK.DEPRN_METHOD_CODE
246        AND     nvl(mt.life_in_months, -99) = nvl(bk.life_in_months, -99)
247        AND     bk.deprn_start_date <= p_period_rec.calendar_period_close_date
248        AND     bk.asset_id = imp.asset_id
249        AND     imp.book_type_code = p_book_type_code
250        AND     imp.request_id = p_request_id
251        AND     imp.period_counter <= p_period_rec.period_counter
252        AND     imp.worker_id = p_worker_id
253        AND     imp.set_of_books_id = p_set_of_books_id;
254 
255 
256 
257 
258    CURSOR c_get_period_rec (c_start_date  date) IS
259      select cp.period_num
260           , fy.fiscal_year
261      from   fa_fiscal_year fy
262           , fa_calendar_periods cp
263      where  cp.calendar_type = fa_cache_pkg.fazcbc_record.deprn_calendar
264      and    fy.fiscal_year_name = fa_cache_pkg.fazcbc_record.fiscal_year_name
265      and    cp.start_date between fy.start_date and fy.end_date
266      and    c_start_date between cp.start_date and cp.end_date;
267 
268 
269 
270    t_asset_id                      tab_num15_type;
271    t_category_id                   tab_num15_type;
272    t_deprn_method_code             tab_char30_type;
273    t_life_in_months                tab_num15_type;
274    t_rate_adjustment_factor        tab_num_type;
275    t_current_units                 tab_num_type;
276    t_adjustment_required_status    tab_char30_type;
277    t_cost_change_flag              tab_char3_type;
278    t_retirement_pending_flag       tab_char3_type;
279    t_period_counter_fully_retired  tab_num15_type;
280    t_asset_number                  tab_char30_type;
281    t_adjusted_cost                 tab_num_type;
282    t_adjusted_rate                 tab_num_type;
283    t_recoverable_cost              tab_num_type;
284    t_reval_amortization_basis      tab_num_type;
285    t_ceiling_name                  tab_char30_type;
286    t_bonus_rule                    tab_char30_type;
287    t_deprn_start_date              tab_date_type;
288    t_date_placed_in_service        tab_date_type;
289    t_prorate_date                  tab_date_type;
290    t_cost                          tab_num_type;
291    t_production_capacity           tab_num_type;
292    t_adjusted_capacity             tab_num_type;
293    t_annual_deprn_rounding_flag    tab_char3_type;
294    t_salvage_value                 tab_num_type;
295    t_period_counter_life_complete  tab_num15_type;
296    t_adjusted_recoverable_cost     tab_num_type;
297    t_short_fiscal_year_flag        tab_char3_type;
298    t_conversion_date               tab_date_type;
299    t_original_deprn_start_date     tab_date_type;
300    t_formula_factor                tab_num_type;
301    t_old_adjusted_cost             tab_num_type;
302    t_tracking_method               tab_char30_type;
303    t_allocate_to_fully_ret_flag    tab_char1_type;
304    t_allocate_to_fully_rsv_flag    tab_char1_type;
305    t_excess_allocation_option      tab_char30_type;
306    t_depreciation_option           tab_char30_type;
307    t_member_rollup_flag            tab_char1_type;
308    t_asset_type                    tab_char30_type;
309    t_group_asset_id                tab_num15_type;
310    t_eofy_reserve                  tab_num_type;
311    t_exclude_fully_rsv_flag        tab_char1_type;
312    t_over_depreciate_option        tab_char30_type;
313    t_terminal_gain_loss            tab_char30_type;
314    t_terminal_gain_loss_flag       tab_char1_type;
315    t_super_group_id                tab_num15_type;
316    t_super_group_pct_sal_value     tab_num_type;
317    t_net_book_value                tab_num_type;
318    t_net_selling_price             tab_num_type;
319    t_value_in_use                  tab_num_type;
320    t_ytd_impairment                tab_num_type;
321    t_impairment_reserve                tab_num_type;
322    t_deprn_run_date                tab_date_type;
323    t_deprn_amount                  tab_num_type;
324    t_ytd_deprn                     tab_num_type;
325    t_deprn_reserve                 tab_num_type;
326    t_deprn_source_code             tab_char30_type;
327    t_bonus_rate                    tab_num_type;
328    t_ltd_production                tab_num_type;
329    t_production                    tab_num_type;
330    t_reval_amortization            tab_num_type;
331    t_reval_deprn_expense           tab_num_type;
332    t_reval_reserve                 tab_num_type;
333    t_ytd_production                tab_num_type;
334    t_ytd_reval_deprn_expense       tab_num_type;
335    t_prior_fy_expense              tab_num_type;
336    t_bonus_deprn_amount            tab_num_type;
337    t_bonus_ytd_deprn               tab_num_type;
338    t_bonus_deprn_reserve           tab_num_type;
339    t_prior_fy_bonus_expense        tab_num_type;
340    t_deprn_override_flag           tab_char30_type;
341    t_system_deprn_amount           tab_num_type;
342    t_system_bonus_deprn_amount     tab_num_type;
343    t_deprn_adjustment_amount       tab_num_type;
344    t_bonus_deprn_adj_amount        tab_num_type;
345    t_imp_rowid                     tab_rowid_type;
346    t_period_of_addition_flag       tab_char1_type;
347    t_impairment_id                 tab_num15_type;
348    t_capital_adjustment            tab_num_type; -- Bug 6666666
349    t_general_fund                  tab_num_type; -- Bug 6666666
350    t_allowed_deprn_limit_amount    tab_num_type;
351    t_depreciate_flag               tab_char1_type; --phase5
352    t_period_fully_reserve          tab_num_type;--phase5
353    t_reval_loss_balance            tab_num_type;
354    l_dpr_row         FA_STD_TYPES.FA_DEPRN_ROW_STRUCT;
355    l_status          boolean;
356    l_dpr_in          fa_std_types.dpr_struct;
357    l_dpr_out         fa_std_types.dpr_out_struct;
358    l_dpr_arr         fa_std_types.dpr_arr_type;
359    l_pa_dpr_in       fa_std_types.dpr_struct;
360    l_pa_dpr_out      fa_std_types.dpr_out_struct;
361    l_pa_dpr_arr      fa_std_types.dpr_arr_type;
362 
363    l_running_mode    VARCHAR2(20);
364    l_asset_hdr_rec   fa_api_types.asset_hdr_rec_type;
365 
366    l_limit           binary_integer := 200;  -- limit constant for c_deprn cursor
367    dpr_err           exception;
368 
369 BEGIN
370 
371    if (p_log_level_rec.statement_level) then
372       fa_debug_pkg.add(l_calling_fn,'Process Depreciation', 'BEGIN', p_log_level_rec => p_log_level_rec);
373    end if;
374 
375    g_error_flag.DELETE ; --Bug#8614268
376    --
377    -- Outer Loop
378    if (p_mrc_sob_type_code = 'R') then
379       OPEN c_mc_deprn;
380    else
381       OPEN c_deprn;
382    end if;
383 
384    LOOP
385 
386       if (p_mrc_sob_type_code = 'R') then
387          FETCH c_mc_deprn BULK COLLECT INTO t_asset_id
388                                           , t_category_id
389                                           , t_deprn_method_code
390                                           , t_life_in_months
391                                           , t_rate_adjustment_factor
392                                           , t_current_units
393                                           , t_adjustment_required_status
394                                           , t_cost_change_flag
395                                           , t_retirement_pending_flag
396                                           , t_period_counter_fully_retired
397                                           , t_asset_number
398                                           , t_adjusted_cost
399                                           , t_adjusted_rate
400                                           , t_recoverable_cost
401                                           , t_reval_amortization_basis
402                                           , t_ceiling_name
403                                           , t_bonus_rule
404                                           , t_deprn_start_date
405                                           , t_date_placed_in_service
406                                           , t_prorate_date
407                                           , t_cost
408                                           , t_production_capacity
409                                           , t_adjusted_capacity
410                                           , t_annual_deprn_rounding_flag
411                                           , t_salvage_value
412                                           , t_period_counter_life_complete
413                                           , t_adjusted_recoverable_cost
414                                           , t_short_fiscal_year_flag
415                                           , t_conversion_date
416                                           , t_original_deprn_start_date
417                                           , t_formula_factor
418                                           , t_old_adjusted_cost
419                                           , t_tracking_method
420                                           , t_allocate_to_fully_ret_flag
421                                           , t_allocate_to_fully_rsv_flag
422                                           , t_excess_allocation_option
423                                           , t_depreciation_option
424                                           , t_member_rollup_flag
425                                           , t_asset_type
426                                           , t_group_asset_id
427                                           , t_eofy_reserve
428                                           , t_exclude_fully_rsv_flag
429                                           , t_over_depreciate_option
430                                           , t_terminal_gain_loss
431                                           , t_terminal_gain_loss_flag
432                                           , t_super_group_id
433                                           , t_super_group_pct_sal_value
434                                           , t_net_book_value
435                                           , t_net_selling_price
436                                           , t_value_in_use
437                                           , t_ytd_impairment
438                                           , t_impairment_reserve
439                                           , t_deprn_run_date
440                                           , t_deprn_amount
441                                           , t_ytd_deprn
442                                           , t_deprn_reserve
443                                           , t_deprn_source_code
444                                           , t_bonus_rate
445                                           , t_ltd_production
446                                           , t_production
447                                           , t_reval_amortization
448                                           , t_reval_deprn_expense
449                                           , t_reval_reserve
450                                           , t_ytd_production
451                                           , t_ytd_reval_deprn_expense
452                                           , t_prior_fy_expense
453                                           , t_bonus_deprn_amount
454                                           , t_bonus_ytd_deprn
455                                           , t_bonus_deprn_reserve
456                                           , t_prior_fy_bonus_expense
457                                           , t_deprn_override_flag
458                                           , t_system_deprn_amount
459                                           , t_system_bonus_deprn_amount
460                                           , t_deprn_adjustment_amount
461                                           , t_bonus_deprn_adj_amount
462                                           , t_imp_rowid
463                                           , t_period_of_addition_flag
464                                           , t_impairment_id
465                                           , t_capital_adjustment -- Bug 6666666
466                                           , t_general_fund       -- Bug 6666666
467                                           , t_allowed_deprn_limit_amount
468 					  , t_depreciate_flag --phase5
469 					  , t_period_fully_reserve --phase5
470 					  , t_reval_loss_balance
471                                           LIMIT l_limit;
472 
473       else
474          FETCH c_deprn BULK COLLECT INTO t_asset_id
475                                        , t_category_id
476                                        , t_deprn_method_code
477                                        , t_life_in_months
478                                        , t_rate_adjustment_factor
479                                        , t_current_units
480                                        , t_adjustment_required_status
481                                        , t_cost_change_flag
482                                        , t_retirement_pending_flag
483                                        , t_period_counter_fully_retired
484                                        , t_asset_number
485                                        , t_adjusted_cost
486                                        , t_adjusted_rate
487                                        , t_recoverable_cost
488                                        , t_reval_amortization_basis
489                                        , t_ceiling_name
490                                        , t_bonus_rule
491                                        , t_deprn_start_date
492                                        , t_date_placed_in_service
493                                        , t_prorate_date
494                                        , t_cost
495                                        , t_production_capacity
496                                        , t_adjusted_capacity
497                                        , t_annual_deprn_rounding_flag
498                                        , t_salvage_value
499                                        , t_period_counter_life_complete
500                                        , t_adjusted_recoverable_cost
501                                        , t_short_fiscal_year_flag
502                                        , t_conversion_date
503                                        , t_original_deprn_start_date
504                                        , t_formula_factor
505                                        , t_old_adjusted_cost
506                                        , t_tracking_method
507                                        , t_allocate_to_fully_ret_flag
508                                        , t_allocate_to_fully_rsv_flag
509                                        , t_excess_allocation_option
510                                        , t_depreciation_option
511                                        , t_member_rollup_flag
512                                        , t_asset_type
513                                        , t_group_asset_id
514                                        , t_eofy_reserve
515                                        , t_exclude_fully_rsv_flag
516                                        , t_over_depreciate_option
517                                        , t_terminal_gain_loss
518                                        , t_terminal_gain_loss_flag
519                                        , t_super_group_id
520                                        , t_super_group_pct_sal_value
521                                        , t_net_book_value
522                                        , t_net_selling_price
523                                        , t_value_in_use
524                                        , t_ytd_impairment
525                                        , t_impairment_reserve
526                                        , t_deprn_run_date
527                                        , t_deprn_amount
528                                        , t_ytd_deprn
529                                        , t_deprn_reserve
530                                        , t_deprn_source_code
531                                        , t_bonus_rate
532                                        , t_ltd_production
533                                        , t_production
534                                        , t_reval_amortization
535                                        , t_reval_deprn_expense
536                                        , t_reval_reserve
537                                        , t_ytd_production
538                                        , t_ytd_reval_deprn_expense
539                                        , t_prior_fy_expense
540                                        , t_bonus_deprn_amount
541                                        , t_bonus_ytd_deprn
542                                        , t_bonus_deprn_reserve
543                                        , t_prior_fy_bonus_expense
544                                        , t_deprn_override_flag
545                                        , t_system_deprn_amount
546                                        , t_system_bonus_deprn_amount
547                                        , t_deprn_adjustment_amount
548                                        , t_bonus_deprn_adj_amount
549                                        , t_imp_rowid
550                                        , t_period_of_addition_flag
551                                        , t_impairment_id
552                                        , t_capital_adjustment -- Bug 6666666
553                                        , t_general_fund       -- Bug 6666666
554                                        , t_allowed_deprn_limit_amount
555 				       , t_depreciate_flag --phase5
556 				       , t_period_fully_reserve --phase5
557 				       , t_reval_loss_balance
558                                        LIMIT l_limit;
559 
560       end if;
561 
562       if (p_log_level_rec.statement_level) then
563          fa_debug_pkg.add(l_calling_fn, 'fetch count', t_asset_id.count, p_log_level_rec => p_log_level_rec);
564       end if;
565 
566       if (t_asset_id.count = 0) then
567 
568          if (p_mrc_sob_type_code = 'R') then
569             CLOSE c_mc_deprn;
570          else
571             CLOSE c_deprn;
572          end if;
573 
574          EXIT;
575 
576       end if;
577 
578       FOR i in 1..t_asset_id.count LOOP
579 
580          if (p_log_level_rec.statement_level) then
581             fa_debug_pkg.add(l_calling_fn, 'Processing', t_asset_id(i));
582             fa_debug_pkg.add(l_calling_fn, 'current period', p_period_rec.period_counter, p_log_level_rec => p_log_level_rec);
583             fa_debug_pkg.add(l_calling_fn, 'impaired period', p_imp_period_rec.period_counter, p_log_level_rec => p_log_level_rec);
584          end if;
585 
586 	 /*Phase5 Restrict Impairment posted on Asset added without reserve and depreciation flag as NO*/
587          IF t_depreciate_flag(i) = 'NO' then
588 	    IF NOT FA_ASSET_VAL_PVT.check_non_depreciating_asset (
589 	                           p_asset_id          => t_asset_id(i)
590                                  , p_book_type_code    => p_book_type_code
591                                  , p_log_level_rec     => p_log_level_rec) then
592                IF (p_log_level_rec.statement_level) then
593                   fa_debug_pkg.add(l_calling_fn, 'Error calling FA_ASSET_VAL_PVT.check_non_depreciating_asset',
594                                    t_asset_id(i), p_log_level_rec => p_log_level_rec);
595                   fa_debug_pkg.add(l_calling_fn, 'You can not Post an impairment for this asset',
596                                    'As this asset is added with depreciate flag as NO and without reserve', p_log_level_rec => p_log_level_rec);
597                END IF;
598 	       fa_srvr_msg.add_message(
599                          calling_fn => null,
600                          name       => 'FA_NO_IMP_NON_DEPR_ASSET',
601                          token1     => 'FA_ASSET_NUM',
602                          value1     =>  '' || t_asset_number(i),
603                          p_log_level_rec => p_log_level_rec);
604 
605                raise dpr_err;
606             END IF;
607 	 END IF;
608 	 /*end phase5*/
609 
610          --
611          -- If this impairment is back dated then call function to calculate reserve
612          -- as of impairment period
613          --
614          if (p_period_rec.period_counter > p_imp_period_rec.period_counter) then
615             if not process_history(p_request_id        => p_request_id
616                                  , p_impairment_id     => t_impairment_id(i)
617                                  , p_asset_id          => t_asset_id(i)
618                                  , p_book_type_code    => p_book_type_code
619                                  , p_period_rec        => p_period_rec
620                                  , p_imp_period_rec    => p_imp_period_rec
621                                  , p_date_placed_in_service => t_date_placed_in_service(i)
622                                  , x_dpr_out           => l_dpr_out
623                                  , x_dpr_in            => l_dpr_in
624                                  , p_mrc_sob_type_code => p_mrc_sob_type_code
625                                  , p_calling_fn        => l_calling_fn
626                                  , p_log_level_rec     => p_log_level_rec) then
627                if (p_log_level_rec.statement_level) then
628                   fa_debug_pkg.add(l_calling_fn, 'Error calling',
629                                    'fa_cache_pkg.fazccp', p_log_level_rec => p_log_level_rec);
630                   fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcbc_record.prorate_calendar',
631                                    fa_cache_pkg.fazcbc_record.prorate_calendar, p_log_level_rec => p_log_level_rec);
632                   fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcbc_record.fiscal_year_name',
633                                    fa_cache_pkg.fazcbc_record.fiscal_year_name, p_log_level_rec => p_log_level_rec);
634 
635                end if;
636 
637                raise dpr_err;
638 
639 
640             end if;
641 
642             l_dpr_row.impairment_rsv := nvl(l_dpr_out.new_impairment_rsv, 0);
643          else
644 
645             -- Terminal Gain loss in impairment should not be necessary because
646             -- group will have no cost and no impairment should allowed
647 
648 
649             -- Process periodic depreciation
650 
651             --+++++++ Populating l_dpr_in to call faxcde ++++++++++
652             l_dpr_in.asset_num             := t_asset_number(i);
653             l_dpr_in.calendar_type         := fa_cache_pkg.fazcbc_record.deprn_calendar;
654             l_dpr_in.book                  := p_book_type_code;
655             l_dpr_in.asset_id              := t_asset_id(i);
656 
657             l_dpr_row.asset_id             := t_asset_id(i);
658             l_dpr_row.book                 := p_book_type_code;
659             l_dpr_row.period_ctr           := p_period_rec.period_counter;
660             l_dpr_row.dist_id              := 0;
661             l_dpr_row.mrc_sob_type_code    := p_mrc_sob_type_code;
662             l_dpr_row.set_of_books_id      := p_set_of_books_id; --8666930
663 
664             l_running_mode                 := 'STANDARD';
665 
666             if (p_log_level_rec.statement_level) then
667                fa_debug_pkg.add(l_calling_fn, 'Calling', 'query_balances_int', p_log_level_rec => p_log_level_rec);
668             end if;
669 
670             fa_query_balances_pkg.query_balances_int(
671                                    X_DPR_ROW               => l_dpr_row,
672                                    X_RUN_MODE              => l_running_mode,
673                                    X_DEBUG                 => FALSE,
674                                    X_SUCCESS               => l_status,
675                                    X_CALLING_FN            => l_calling_fn,
676                                    X_TRANSACTION_HEADER_ID => -1, p_log_level_rec => p_log_level_rec);
677 
678             if (NOT l_status) then
679 
680                if (p_log_level_rec.statement_level) then
681                   fa_debug_pkg.add(l_calling_fn, 'ERROR',
682                                    'Calling fa_query_balances_pkg.query_balances_int', p_log_level_rec => p_log_level_rec);
683                end if;
684 
685                raise dpr_err;
686             end if;
687 
688             if (p_log_level_rec.statement_level) then
689                fa_debug_pkg.add(l_calling_fn, 'l_dpr_row.deprn_adjust_exp', l_dpr_row.deprn_adjust_exp, p_log_level_rec => p_log_level_rec);
690                fa_debug_pkg.add(l_calling_fn, 'l_dpr_row.deprn_exp', l_dpr_row.deprn_exp, p_log_level_rec => p_log_level_rec);
691                fa_debug_pkg.add(l_calling_fn, 'l_dpr_row.deprn_rsv', l_dpr_row.deprn_rsv, p_log_level_rec => p_log_level_rec);
692             end if;
693 
694             -- Bug5768359: Need to refrect exp into deprn adj amount in DD.
695             l_dpr_row.deprn_adjust_exp := nvl(l_dpr_row.deprn_exp, 0);
696             if (p_log_level_rec.statement_level) then
697                fa_debug_pkg.add(l_calling_fn, 'Query balance returned', 'following', p_log_level_rec => p_log_level_rec);
698                fa_debug_pkg.add(l_calling_fn, 'l_dpr_row.deprn_exp', l_dpr_row.deprn_exp, p_log_level_rec => p_log_level_rec);
699             end if;
700 
701 
702             if (p_log_level_rec.statement_level) then
703                fa_debug_pkg.add(l_calling_fn, 'Start populating', 'l_dpr_in', p_log_level_rec => p_log_level_rec);
704             end if;
705 
706 	    /*Phase5 Restrict call to deprn engine faxcde for assets having depreciation flag equal to  NO*/
707             /*Bug 9574021 Added condition for Extended Assets*/
708             IF ((t_depreciate_flag(i) = 'NO') OR (t_period_fully_reserve(i) is not null AND t_deprn_method_code(i) <> 'JP-STL-EXTND')) then
709 	       IF (p_log_level_rec.statement_level) then
710                   fa_debug_pkg.add(l_calling_fn, '======================================',
711                                    'NON DEPRECIATING/FULLY RESERVED ASSET', p_log_level_rec => p_log_level_rec);
712                   fa_debug_pkg.add(l_calling_fn, 'Skipping the call to FAXCDE for asset',
713                                    t_asset_id(i), p_log_level_rec => p_log_level_rec);
714                   fa_debug_pkg.add(l_calling_fn, '======================================',
715                                    'NON DEPRECIATING/FULLY RESERVED ASSET', p_log_level_rec => p_log_level_rec);
716                END IF;
717             ELSE --Deprn flag = YES
718 
719                l_dpr_in.rec_cost                   := t_recoverable_cost(i);
720                l_dpr_in.salvage_value              := t_salvage_value(i);
721                l_dpr_in.adj_rec_cost               := t_adjusted_recoverable_cost(i);
722                l_dpr_in.adj_cost                   := t_adjusted_cost(i);
723                l_dpr_in.old_adj_cost               := t_old_adjusted_cost(i);
724                l_dpr_in.formula_factor             := t_formula_factor(i);
725                l_dpr_in.rate_adj_factor            := t_rate_adjustment_factor(i);
726                l_dpr_in.eofy_reserve               := t_eofy_reserve(i);
727                l_dpr_in.method_code                := t_deprn_method_code(i);
728                l_dpr_in.life                       := t_life_in_months(i);
729                l_dpr_in.adj_rate                   := t_adjusted_rate(i);
730                l_dpr_in.capacity                   := t_production_capacity(i);
731                l_dpr_in.adj_capacity               := t_adjusted_capacity(i);
732                l_dpr_in.bonus_rule                 := t_bonus_rule(i);
733                l_dpr_in.ceil_name                  := t_ceiling_name(i);
734                l_dpr_in.reval_amo_basis            := t_reval_amortization_basis(i);
735                l_dpr_in.jdate_in_service           := to_number(to_char(t_date_placed_in_service(i), 'J'));
736                l_dpr_in.prorate_jdate              := to_number(to_char(t_prorate_date(i), 'J'));
737                l_dpr_in.deprn_start_jdate          := to_number(to_char(t_deprn_start_date(i), 'J'));
738                l_dpr_in.prorate_date               := t_prorate_date(i);
739                l_dpr_in.orig_deprn_start_date      := t_original_deprn_start_date(i);
740                l_dpr_in.jdate_retired              := 0;
741                l_dpr_in.ret_prorate_jdate          := 0;
742                l_dpr_in.ltd_prod                   := l_dpr_row.ltd_prod;
743                l_dpr_in.ytd_deprn                  := l_dpr_row.ytd_deprn;
744                l_dpr_in.deprn_rsv                  := l_dpr_row.deprn_rsv;
745                l_dpr_in.reval_rsv                  := l_dpr_row.reval_rsv;
746                l_dpr_in.bonus_deprn_exp            := 0;
747                l_dpr_in.bonus_ytd_deprn            := l_dpr_row.bonus_ytd_deprn;
748                l_dpr_in.bonus_deprn_rsv            := l_dpr_row.bonus_deprn_rsv;
749                l_dpr_in.prior_fy_exp               := l_dpr_row.prior_fy_exp;
750                l_dpr_in.prior_fy_bonus_exp         := l_dpr_row.prior_fy_bonus_exp;
751                l_dpr_in.impairment_exp             := 0;
752                l_dpr_in.ytd_impairment             := l_dpr_row.ytd_impairment;
753                l_dpr_in.impairment_rsv             := l_dpr_row.impairment_rsv;
754                l_dpr_in.short_fiscal_year_flag     := t_short_fiscal_year_flag(i);
755                l_dpr_in.conversion_date            := t_conversion_date(i);
756                l_dpr_in.super_group_id             := t_super_group_id(i);
757                l_dpr_in.over_depreciate_option     := t_over_depreciate_option(i);
758                l_dpr_in.tracking_method            := t_tracking_method(i);
759                l_dpr_in.allocate_to_fully_ret_flag := t_allocate_to_fully_ret_flag(i);
760                l_dpr_in.allocate_to_fully_rsv_flag := t_allocate_to_fully_rsv_flag(i);
761                l_dpr_in.excess_allocation_option   := t_excess_allocation_option(i);
762                l_dpr_in.depreciation_option        := t_depreciation_option(i);
763                l_dpr_in.member_rollup_flag         := t_member_rollup_flag(i);
764                l_dpr_in.pc_life_end                := t_period_counter_life_complete(i);
765                l_dpr_in.deprn_override_flag        := fa_std_types.FA_NO_OVERRIDE;
766                l_dpr_in.rsv_known_flag             := TRUE;
767                l_dpr_in.deprn_rounding_flag        := 'ADJ';
768                l_dpr_in.used_by_adjustment         := FALSE;
769                l_dpr_in.capital_adjustment         := l_dpr_row.capital_adjustment; -- Bug 6666666
770                l_dpr_in.general_fund               := l_dpr_row.general_fund;             -- Bug 6666666
771                l_dpr_in.set_of_books_id            := p_set_of_books_id;
772                l_dpr_in.mrc_sob_type_code          := p_mrc_sob_type_code;   -- Bug 9700559
773                l_running_mode                      := fa_std_types.FA_DPR_NORMAL;
774 
775                -- manual override
776                if fa_cache_pkg.fa_deprn_override_enabled then
777                   l_dpr_in.update_override_status := TRUE;
778                end if;
779 
780                if (p_log_level_rec.statement_level) then
781                   fa_debug_pkg.add(l_calling_fn, 'Calling', 'fa_cache_pkg.fazccp', p_log_level_rec => p_log_level_rec);
782                end if;
783 
784                if not fa_cache_pkg.fazccp(fa_cache_pkg.fazcbc_record.prorate_calendar,
785                                              fa_cache_pkg.fazcbc_record.fiscal_year_name,
786                                              l_dpr_in.prorate_jdate,
787                                              g_temp_number,
788                                              l_dpr_in.y_begin,
789                                              g_temp_integer, p_log_level_rec => p_log_level_rec) then
790                   if (p_log_level_rec.statement_level) then
791                      fa_debug_pkg.add(l_calling_fn, 'Error calling',
792                                       'fa_cache_pkg.fazccp', p_log_level_rec => p_log_level_rec);
793                      fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcbc_record.prorate_calendar',
794                                       fa_cache_pkg.fazcbc_record.prorate_calendar, p_log_level_rec => p_log_level_rec);
795                      fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcbc_record.fiscal_year_name',
796                                       fa_cache_pkg.fazcbc_record.fiscal_year_name, p_log_level_rec => p_log_level_rec);
797                   end if;
798 
799                   raise dpr_err;
800                end if;
801 
802                if not fa_cache_pkg.fazcct(fa_cache_pkg.fazcbc_record.deprn_calendar, p_log_level_rec => p_log_level_rec) then
803                   raise dpr_err;
804                end if;
805 
806                if (p_log_level_rec.statement_level) then
807                   fa_debug_pkg.add(l_calling_fn, 't_adjustment_required_status',
808                                    t_adjustment_required_status(i));
809                   fa_debug_pkg.add(l_calling_fn, 'l_dpr_in.deprn_rsv', l_dpr_in.deprn_rsv, p_log_level_rec => p_log_level_rec);
810                   fa_debug_pkg.add(l_calling_fn, 't_deprn_start_date', t_deprn_start_date(i));
811                end if;
812 
813                l_pa_dpr_out := null; --Bug#8744490
814                ---        *****************************      ---
815                ---        Prior period addition process      ---
816                ---        *****************************      ---
817                if (t_adjustment_required_status(i) = 'ADD') and
818                   (l_dpr_in.deprn_rsv = 0) and
819                   (t_deprn_start_date(i) < p_period_rec.calendar_period_open_date ) then
820 
821                   l_pa_dpr_in  := null;
822                   l_pa_dpr_arr.delete;
823                   l_pa_dpr_in := l_dpr_in;
824                   OPEN c_get_period_rec(t_date_placed_in_service(i));
825                   FETCH c_get_period_rec INTO l_pa_dpr_in.p_cl_begin
826                                             , l_pa_dpr_in.y_begin;
827                   CLOSE c_get_period_rec;
828 
829                   if (p_period_rec.period_num = 1) then
830                      l_pa_dpr_in.y_end := p_period_rec.fiscal_year - 1;
831                      l_pa_dpr_in.p_cl_end := fa_cache_pkg.fazcct_record.NUMBER_PER_FISCAL_YEAR;
832                   else
833                      l_pa_dpr_in.y_end := p_period_rec.fiscal_year;
834                      l_pa_dpr_in.p_cl_end := p_period_rec.period_num - 1;
835                   end if;
836 
837                   --+++++++ Call Depreciation engine to calculate periodic depreciation +++++++
838                   if not FA_CDE_PKG.faxcde(l_pa_dpr_in,
839                                            l_pa_dpr_arr,
840                                            l_pa_dpr_out,
841                                            l_running_mode, p_log_level_rec => p_log_level_rec) then
842                      if (p_log_level_rec.statement_level) then
843                         fa_debug_pkg.add(l_calling_fn, 'Error calling', 'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
844                      end if;
845 
846                      raise dpr_err;
847                   end if;
848 
849                   l_dpr_in.adj_rec_cost        := l_pa_dpr_out.new_adj_cost;
850                   l_dpr_in.reval_amo_basis     := l_pa_dpr_out.new_reval_amo_basis;
851                   l_dpr_in.ytd_deprn           := l_pa_dpr_out.new_ytd_deprn;
852                   l_dpr_in.deprn_rsv           := l_pa_dpr_out.new_deprn_rsv;
853                   l_dpr_in.reval_rsv           := l_pa_dpr_out.new_reval_rsv;
854                   l_dpr_in.adj_capacity        := l_pa_dpr_out.new_adj_capacity;
855                   l_dpr_in.ltd_prod            := l_pa_dpr_out.new_ltd_prod;
856                   l_dpr_in.eofy_reserve        := l_pa_dpr_out.new_eofy_reserve;
857                   l_dpr_row.ltd_prod           := l_pa_dpr_out.new_ltd_prod;
858                   l_dpr_row.ytd_prod           := l_dpr_row.ytd_prod + l_pa_dpr_out.prod;
859                   l_dpr_row.deprn_rsv          := l_pa_dpr_out.new_deprn_rsv;
860                   l_dpr_row.reval_rsv          := l_pa_dpr_out.new_reval_rsv;
861                   l_dpr_row.bonus_ytd_deprn    := l_dpr_row.bonus_ytd_deprn + l_pa_dpr_out.bonus_deprn_exp;
862                   l_dpr_row.bonus_deprn_rsv    := l_pa_dpr_out.new_bonus_deprn_rsv;
863                   l_dpr_row.prior_fy_exp       := l_pa_dpr_out.new_prior_fy_exp;
864                   l_dpr_row.prior_fy_bonus_exp := l_pa_dpr_out.new_prior_fy_bonus_exp;
865                   l_dpr_row.capital_adjustment := l_pa_dpr_out.new_capital_adjustment; -- Bug 6666666
866                   l_dpr_row.general_fund       := l_pa_dpr_out.new_general_fund; -- Bug 6666666
867                end if;
868                ---   ************************************   ---
869                ---   End of Prior period addition process   ---
870                ---   ************************************   ---
871 
872 
873                --
874                -- Prepare Running Depreciation
875                --
876                l_dpr_in.y_begin := p_period_rec.fiscal_year;
877                l_dpr_in.p_cl_begin := p_period_rec.period_num;
878                l_dpr_in.y_end := p_period_rec.fiscal_year;
879                l_dpr_in.p_cl_end := p_period_rec.period_num;
880 	       l_dpr_in.deprn_override_used_by := 'PRE'; --16275774
881 
882                if (p_log_level_rec.statement_level) then
883                   fa_debug_pkg.add(l_calling_fn, 'Calling', 'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
884                end if;
885 
886                --+++++++ Call Depreciation engine to calculate periodic depreciation +++++++
887                if not FA_CDE_PKG.faxcde(l_dpr_in,
888                                         l_dpr_arr,
889                                         l_dpr_out,
890                                         l_running_mode, p_log_level_rec => p_log_level_rec) then
891                   if (p_log_level_rec.statement_level) then
892                      fa_debug_pkg.add(l_calling_fn, 'Error calling',
893                                       'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
894                   end if;
895 
896                   raise dpr_err;
897                end if;
898 	    END IF; --depreciate falg = 'NO' phase5
899          end if; -- (p_period_rec.period_counter > p_imp_period_rec.period_counter)
900 
901          if (p_log_level_rec.statement_level) then
902             fa_debug_pkg.add(l_calling_fn, 'Storing values in local pl/sql tables', 'process deprn', p_log_level_rec => p_log_level_rec);
903          end if;
904          /*Bug 9574021 Added condition for Extended Assets*/
905          IF t_depreciate_flag(i) = 'YES' AND (t_period_fully_reserve(i) is null OR t_deprn_method_code(i) = 'JP-STL-EXTND')then
906 	    --Bug5768359: adding l_dpr_row.deprn_exp to refrect cur per catchup.
907             t_deprn_amount(i)              := l_dpr_out.deprn_exp + nvl(l_pa_dpr_out.new_deprn_rsv, 0) + nvl(l_dpr_row.deprn_exp, 0);
908             t_ytd_deprn(i)                 := l_dpr_out.new_ytd_deprn;--l_dpr_row.ytd_deprn + l_dpr_out.deprn_exp + nvl(l_pa_dpr_out.new_deprn_rsv, 0);
909             t_deprn_reserve(i)             := l_dpr_out.new_deprn_rsv;
910             t_adjusted_cost(i)             := l_dpr_out.new_adj_cost;
911             t_bonus_deprn_amount(i)        := l_dpr_out.bonus_deprn_exp;
912             t_bonus_ytd_deprn(i)           := l_dpr_row.bonus_ytd_deprn + l_dpr_out.bonus_deprn_exp;
913             t_bonus_deprn_reserve(i)       := l_dpr_out.new_bonus_deprn_rsv;
914             if l_dpr_out.bonus_rate_used = -1 then
915                t_bonus_rate(i)             := 0;
916             else
917                t_bonus_rate(i)             := l_dpr_out.bonus_rate_used;
918             end if;
919 	    t_production(i)                := l_dpr_out.prod;
920             t_ytd_production(i)            := l_dpr_row.ytd_prod + l_dpr_out.prod;
921             t_ltd_production(i)            := l_dpr_out.new_ltd_prod;
922             t_reval_amortization(i)        := l_dpr_out.reval_amo;
923             t_reval_amortization_basis(i)  := l_dpr_out.new_reval_amo_basis;
924             t_reval_deprn_expense(i)       := l_dpr_out.reval_exp;
925             t_ytd_reval_deprn_expense(i)   := l_dpr_row.ytd_reval_deprn_exp + l_dpr_out.reval_exp;
926             t_reval_reserve(i)             := l_dpr_out.new_reval_rsv;
927             t_prior_fy_expense(i)          := l_dpr_out.new_prior_fy_exp;
928             t_prior_fy_bonus_expense(i)    := l_dpr_out.new_prior_fy_bonus_exp;
929             t_deprn_override_flag(i)       := l_dpr_out.deprn_override_flag;
930             t_system_deprn_amount(i)       := l_dpr_out.deprn_exp;
931             t_system_bonus_deprn_amount(i) := l_dpr_out.bonus_deprn_exp;
932             t_deprn_adjustment_amount(i)   := nvl(l_dpr_row.deprn_adjust_exp, 0) + nvl(l_pa_dpr_out.new_deprn_rsv, 0);
933             t_bonus_deprn_adj_amount(i)    := 0;--l_dpr_row.bonus_deprn_adjust_exp;
934             t_capital_adjustment(i)        := l_dpr_out.new_capital_adjustment; -- Bug 6666666
935             t_general_fund(i)              := l_dpr_out.new_general_fund;       -- Bug 6666666
936 	    t_reval_loss_balance(i)        := l_dpr_row.reval_loss_balance ;
937 	    if (p_log_level_rec.statement_level) then
938                fa_debug_pkg.add(l_calling_fn, 'getting values from l_dpr_out struct', 'deprn run yes', p_log_level_rec => p_log_level_rec);
939             end if;
940          ELSE --t_depreciate_flag(i) = 'NO' /*override the values from l_dpr_row o/p of query balance*/
941 	    t_deprn_amount(i)              := 0;
942             t_ytd_deprn(i)                 := l_dpr_row.ytd_deprn;
943             t_deprn_reserve(i)             := l_dpr_row.deprn_rsv;
944             t_adjusted_cost(i)             := l_dpr_row.adj_cost;
945             t_bonus_deprn_amount(i)        := 0;
946             t_bonus_ytd_deprn(i)           := l_dpr_row.bonus_ytd_deprn ;
947             t_bonus_deprn_reserve(i)       := l_dpr_row.bonus_deprn_rsv;
948             t_bonus_rate(i)                := l_dpr_row.bonus_rate;
949             t_production(i)                := l_dpr_row.ltd_prod;
950             t_ytd_production(i)            := l_dpr_row.ytd_prod ;
951             t_ltd_production(i)            := l_dpr_row.ltd_prod;
952             t_reval_amortization(i)        := l_dpr_row.reval_amo;
953             t_reval_amortization_basis(i)  := l_dpr_row.reval_amo_basis;
954             t_reval_deprn_expense(i)       := l_dpr_row.reval_deprn_exp;
955             t_ytd_reval_deprn_expense(i)   := l_dpr_row.reval_deprn_exp;
956             t_reval_reserve(i)             := l_dpr_row.reval_rsv;
957             t_prior_fy_expense(i)          := l_dpr_row.prior_fy_exp;
958             t_prior_fy_bonus_expense(i)    := l_dpr_row.prior_fy_bonus_exp;
959             t_deprn_override_flag(i)       := l_dpr_row.deprn_override_flag;
960             t_system_deprn_amount(i)       := 0;
961             t_system_bonus_deprn_amount(i) := 0;
962             t_deprn_adjustment_amount(i)   := nvl(l_dpr_row.deprn_adjust_exp, 0);
963             t_bonus_deprn_adj_amount(i)    := 0;--l_dpr_row.bonus_deprn_adjust_exp;
964             t_capital_adjustment(i)        := l_dpr_row.capital_adjustment; -- Bug 6666666
965             t_general_fund(i)              := l_dpr_row.general_fund;       -- Bug 6666666
966 	    t_reval_loss_balance(i)        := l_dpr_row.reval_loss_balance ;
967 	    if (p_log_level_rec.statement_level) then
968                fa_debug_pkg.add(l_calling_fn, 'getting values from l_dpr_row struct', 'deprn run NO', p_log_level_rec => p_log_level_rec);
969             end if;
970 	 END IF; --deprn flag = YES phase5
971 
972 	 if (p_log_level_rec.statement_level) then
973             fa_debug_pkg.add(l_calling_fn, 'l_dpr_row.deprn_adjust_exp', l_dpr_row.deprn_adjust_exp, p_log_level_rec => p_log_level_rec);
974             fa_debug_pkg.add(l_calling_fn, 't_deprn_adjustment_amount('||to_char(i)||')', t_deprn_adjustment_amount(i));
975          end if;
976 
977          --
978          -- Store appropriate amount in eofy_reserve
979          --
980          if (p_log_level_rec.statement_level) then
981             fa_debug_pkg.add(l_calling_fn, 'Before Calling', 'fa_cache_pkg.fazccmt', p_log_level_rec => p_log_level_rec);
982             fa_debug_pkg.add(l_calling_fn, 'deprn_method_code', t_deprn_method_code(i));
983             fa_debug_pkg.add(l_calling_fn, 'life_in_months', t_life_in_months(i));
984          end if;
985 
986          if (not fa_cache_pkg.fazccmt(
987                     t_deprn_method_code(i),
988                     t_life_in_months(i),
989                     p_log_level_rec)) then
990             if (p_log_level_rec.statement_level) then
991                fa_debug_pkg.add(l_calling_fn, 'Error calling', 'fa_cache_pkg.fazccmt', p_log_level_rec => p_log_level_rec);
992             end if;
993 
994             raise dpr_err;
995          end if;
996 
997          if (p_log_level_rec.statement_level) then
998             fa_debug_pkg.add(l_calling_fn, 'rate_source_rule', fa_cache_pkg.fazccmt_record.rate_source_rule, p_log_level_rec => p_log_level_rec);
999             fa_debug_pkg.add(l_calling_fn, 'deprn_basis_rule', fa_cache_pkg.fazccmt_record.deprn_basis_rule, p_log_level_rec => p_log_level_rec);
1000             fa_debug_pkg.add(l_calling_fn, 'use_rsv_after_imp_flag', fa_cache_pkg.fazcdrd_record.use_rsv_after_imp_flag, p_log_level_rec => p_log_level_rec);
1001          end if;
1002 
1003          if (fa_cache_pkg.fazccmt_record.rate_source_rule = fa_std_types.FAD_RSR_FLAT) and
1004             (fa_cache_pkg.fazccmt_record.deprn_basis_rule = fa_std_types.FAD_DBR_NBV) and
1005             (fa_cache_pkg.fazcdrd_record.use_rsv_after_imp_flag = 'Y') then
1006             t_eofy_reserve(i) := nvl(l_dpr_out.new_deprn_rsv, 0);
1007          else
1008             t_eofy_reserve(i) := nvl(l_dpr_out.new_eofy_reserve, 0);
1009          end if;
1010 
1011          if (p_log_level_rec.statement_level) then
1012             fa_debug_pkg.add(l_calling_fn, 'eofy reserve', t_eofy_reserve(i));
1013             fa_debug_pkg.add(l_calling_fn, 'l_dpr_row.impairment_rsv', l_dpr_row.impairment_rsv, p_log_level_rec => p_log_level_rec);
1014             fa_debug_pkg.add(l_calling_fn, 'l_dpr_in.rec_cost', l_dpr_in.rec_cost, p_log_level_rec => p_log_level_rec);
1015             fa_debug_pkg.add(l_calling_fn, 'l_dpr_out.new_deprn_rsv', l_dpr_out.new_deprn_rsv, p_log_level_rec => p_log_level_rec);
1016          end if;
1017 
1018          /* Phase5 excluding savage value when asset is using JP Logic for NBV calculation for Impairment*/
1019 	 /*Bug 9576003  changed the variables to cal net_book_value in case of ELSE part*/
1020          /*Bug11684168 - Removed ifelse condition,salvage value should not be considered during calc of nbv for impairment */
1021 	 t_net_book_value(i) := t_cost(i) - t_deprn_reserve(i) - nvl(l_dpr_row.impairment_rsv, 0);
1022 
1023          t_ytd_impairment(i) := t_ytd_impairment(i) + nvl(l_dpr_row.ytd_impairment, 0);
1024          t_impairment_reserve(i) := t_impairment_reserve(i) + nvl(l_dpr_row.impairment_rsv, 0);
1025 
1026 
1027          -- Check if this is the period of addition
1028          l_asset_hdr_rec.asset_id          := t_asset_id(i);
1029          l_asset_hdr_rec.book_type_code    := p_book_type_code;
1030          l_asset_hdr_rec.set_of_books_id   := p_set_of_books_id;
1031 
1032          if (p_log_level_rec.statement_level) then
1033             fa_debug_pkg.add(l_calling_fn,'Calling function ', 'FA_ASSET_VAL_PVT.validate_period_of_addition',  p_log_level_rec => p_log_level_rec);
1034             fa_debug_pkg.add(l_calling_fn, 'l_asset_hdr_rec.asset_id', l_asset_hdr_rec.asset_id, p_log_level_rec => p_log_level_rec);
1035             fa_debug_pkg.add(l_calling_fn, 'p_book_type_code', p_book_type_code, p_log_level_rec => p_log_level_rec);
1036          end if;
1037 
1038          if not FA_ASSET_VAL_PVT.validate_period_of_addition
1039                               (p_asset_id            => l_asset_hdr_rec.asset_id,
1040                                p_book                => p_book_type_code,
1041                                p_mode                => 'ABSOLUTE',
1042                                px_period_of_addition => t_period_of_addition_flag(i),
1043                                p_log_level_rec     => p_log_level_rec) then
1044             if (p_log_level_rec.statement_level) then
1045                fa_debug_pkg.add(l_calling_fn,'Error Calling', 'FA_ASSET_VAL_PVT.validate_period_of_addition',  p_log_level_rec => p_log_level_rec);
1046             end if;
1047 
1048             raise dpr_err;
1049          end if;
1050 
1051          -- Create (cip_)cost entries if this is asset's period of addition
1052          if (p_log_level_rec.statement_level) then
1053             fa_debug_pkg.add(l_calling_fn,'Is this period of addition? ', t_period_of_addition_flag(i));
1054          end if;
1055          --Bug#8614268 start
1056          if (p_log_level_rec.statement_level) then
1057             fa_debug_pkg.add(l_calling_fn,'Before checking impairments accts for category id',t_category_id(i), p_log_level_rec => p_log_level_rec);
1058          end if;
1059          if not (fa_cache_pkg.fazccb (X_Book    => p_book_type_code,
1060                                       X_Cat_Id  => t_category_id(i),
1061                                       p_log_level_rec => p_log_level_rec)) then
1062             if (p_log_level_rec.statement_level) then
1063                fa_debug_pkg.add(l_calling_fn,'Error Calling','fa_cache_pkg.fazccb', p_log_level_rec => p_log_level_rec);
1064             end if;
1065             raise dpr_err;
1066          end if;
1067          if ((fa_cache_pkg.fazccb_record.IMPAIR_EXPENSE_ACCT is null) OR
1068              (fa_cache_pkg.fazccb_record.IMPAIR_RESERVE_ACCT is null)) then
1069             g_error_flag(i) := 1;
1070          else
1071             g_error_flag(i) := 0;
1072          end if;
1073          --Bug# 8614268 end
1074       END LOOP;
1075 
1076       if (p_log_level_rec.statement_level) then
1077          fa_debug_pkg.add(l_calling_fn,'t_net_book_value(1)', t_net_book_value(1));
1078       end if;
1079 
1080 
1081       if (p_mrc_sob_type_code = 'R') then
1082          FORALL i in 1..t_asset_id.count
1083             UPDATE FA_MC_ITF_IMPAIRMENTS
1084             SET    NET_BOOK_VALUE                = t_net_book_value(i)
1085                  , YTD_IMPAIRMENT                = t_ytd_impairment(i)
1086                  , impairment_reserve                = t_impairment_reserve(i)
1087                  , DEPRN_RUN_DATE                = t_deprn_run_date(i)
1088                  , DEPRN_AMOUNT                  = t_deprn_amount(i)
1089                  , YTD_DEPRN                     = t_ytd_deprn(i)
1090                  , DEPRN_RESERVE                 = t_deprn_reserve(i)
1091                  , ADJUSTED_COST                 = t_adjusted_cost(i)
1092                  , BONUS_RATE                    = t_bonus_rate(i)
1093                  , LTD_PRODUCTION                = t_ltd_production(i)
1094                  , PRODUCTION                    = t_production(i)
1095                  , REVAL_AMORTIZATION            = t_reval_amortization(i)
1096                  , REVAL_AMORTIZATION_BASIS      = t_reval_amortization_basis(i)
1097                  , REVAL_DEPRN_EXPENSE           = t_reval_deprn_expense(i)
1098                  , REVAL_RESERVE                 = t_reval_reserve(i)
1099                  , YTD_PRODUCTION                = t_ytd_production(i)
1100                  , YTD_REVAL_DEPRN_EXPENSE       = t_ytd_reval_deprn_expense(i)
1101                  , PRIOR_FY_EXPENSE              = t_prior_fy_expense(i)
1102                  , BONUS_DEPRN_AMOUNT            = t_bonus_deprn_amount(i)
1103                  , BONUS_YTD_DEPRN               = t_bonus_ytd_deprn(i)
1104                  , BONUS_DEPRN_RESERVE           = t_bonus_deprn_reserve(i)
1105                  , PRIOR_FY_BONUS_EXPENSE        = t_prior_fy_bonus_expense(i)
1106                  , DEPRN_OVERRIDE_FLAG           = t_deprn_override_flag(i)
1107                  , SYSTEM_DEPRN_AMOUNT           = t_system_deprn_amount(i)
1108                  , SYSTEM_BONUS_DEPRN_AMOUNT     = t_system_bonus_deprn_amount(i)
1109                  , DEPRN_ADJUSTMENT_AMOUNT       = t_deprn_adjustment_amount(i)
1110                  , BONUS_DEPRN_ADJUSTMENT_AMOUNT = t_bonus_deprn_adj_amount(i)
1111                  , CURRENT_UNITS                 = t_current_units(i)
1112                  , CATEGORY_ID                   = t_category_id(i)
1113                  , PERIOD_OF_ADDITION_FLAG       = t_period_of_addition_flag(i)
1114                  , EOFY_RESERVE                  = t_eofy_reserve(i)
1115                  , CAPITAL_ADJUSTMENT            = t_capital_adjustment(i) -- Bug 6666666
1116                  , GENERAL_FUND                  = t_general_fund(i)       -- Bug 6666666
1117                  , ALLOWED_DEPRN_LIMIT_AMOUNT    = t_allowed_deprn_limit_amount(i)
1118 		 , REVAL_LOSS_BALANCE            = t_reval_loss_balance(i)
1119             WHERE  ROWID                         = t_imp_rowid(i);
1120       else
1121          FORALL i in 1..t_asset_id.count
1122             UPDATE FA_ITF_IMPAIRMENTS
1123             SET    NET_BOOK_VALUE                = t_net_book_value(i)
1124                  , YTD_IMPAIRMENT                = t_ytd_impairment(i)
1125                  , impairment_reserve                = t_impairment_reserve(i)
1126                  , DEPRN_RUN_DATE                = t_deprn_run_date(i)
1127                  , DEPRN_AMOUNT                  = t_deprn_amount(i)
1128                  , YTD_DEPRN                     = t_ytd_deprn(i)
1129                  , DEPRN_RESERVE                 = t_deprn_reserve(i)
1130                  , ADJUSTED_COST                 = t_adjusted_cost(i)
1131                  , BONUS_RATE                    = t_bonus_rate(i)
1132                  , LTD_PRODUCTION                = t_ltd_production(i)
1133                  , PRODUCTION                    = t_production(i)
1134                  , REVAL_AMORTIZATION            = t_reval_amortization(i)
1135                  , REVAL_AMORTIZATION_BASIS      = t_reval_amortization_basis(i)
1136                  , REVAL_DEPRN_EXPENSE           = t_reval_deprn_expense(i)
1137                  , REVAL_RESERVE                 = t_reval_reserve(i)
1138                  , YTD_PRODUCTION                = t_ytd_production(i)
1139                  , YTD_REVAL_DEPRN_EXPENSE       = t_ytd_reval_deprn_expense(i)
1140                  , PRIOR_FY_EXPENSE              = t_prior_fy_expense(i)
1141                  , BONUS_DEPRN_AMOUNT            = t_bonus_deprn_amount(i)
1142                  , BONUS_YTD_DEPRN               = t_bonus_ytd_deprn(i)
1143                  , BONUS_DEPRN_RESERVE           = t_bonus_deprn_reserve(i)
1144                  , PRIOR_FY_BONUS_EXPENSE        = t_prior_fy_bonus_expense(i)
1145                  , DEPRN_OVERRIDE_FLAG           = t_deprn_override_flag(i)
1146                  , SYSTEM_DEPRN_AMOUNT           = t_system_deprn_amount(i)
1147                  , SYSTEM_BONUS_DEPRN_AMOUNT     = t_system_bonus_deprn_amount(i)
1148                  , DEPRN_ADJUSTMENT_AMOUNT       = t_deprn_adjustment_amount(i)
1149                  , BONUS_DEPRN_ADJUSTMENT_AMOUNT = t_bonus_deprn_adj_amount(i)
1150                  , CURRENT_UNITS                 = t_current_units(i)
1151                  , CATEGORY_ID                   = t_category_id(i)
1152                  , PERIOD_OF_ADDITION_FLAG       = t_period_of_addition_flag(i)
1153                  , EOFY_RESERVE                  = t_eofy_reserve(i)
1154                  , CAPITAL_ADJUSTMENT            = t_capital_adjustment(i) -- Bug 6666666
1155                  , GENERAL_FUND                  = t_general_fund(i)       -- Bug 6666666
1156                  , ALLOWED_DEPRN_LIMIT_AMOUNT    = t_allowed_deprn_limit_amount(i)
1157 		 , REVAL_LOSS_BALANCE            = t_reval_loss_balance(i)
1158             WHERE  ROWID                         = t_imp_rowid(i);
1159       end if;
1160 
1161 
1162    END LOOP;
1163    --
1164    --
1165 
1166    if (p_log_level_rec.statement_level) then
1167       fa_debug_pkg.add(l_calling_fn,'Process Depreciation', 'END', p_log_level_rec => p_log_level_rec);
1168    end if;
1169 
1170    return true;
1171 
1172 EXCEPTION
1173    WHEN dpr_err THEN
1174 
1175       if (p_log_level_rec.statement_level) then
1176          fa_debug_pkg.add(l_calling_fn,'EXCEPTION', 'prv_err', p_log_level_rec => p_log_level_rec);
1177          fa_debug_pkg.add(l_calling_fn,'sqlerrm', substrb(sqlerrm, 1, 200));
1178       end if;
1179 
1180       return false;
1181 
1182    WHEN OTHERS THEN
1183 
1184       if (p_log_level_rec.statement_level) then
1185          fa_debug_pkg.add(l_calling_fn,'EXCEPTION', 'OTHERS', p_log_level_rec => p_log_level_rec);
1186          fa_debug_pkg.add(l_calling_fn,'sqlerrm', substrb(sqlerrm, 1, 200));
1187       end if;
1188 
1189       return false;
1190 
1191 END process_depreciation;
1192 
1193 
1194 FUNCTION calc_total_nbv(
1195               p_request_id            IN NUMBER
1196             , p_book_type_code        IN VARCHAR2
1197             , p_transaction_date      IN DATE
1198             , p_period_rec            IN FA_API_TYPES.period_rec_type
1199             , p_mrc_sob_type_code     IN VARCHAR2
1200             , p_set_of_books_id       IN NUMBER
1201             , p_calling_fn            IN VARCHAR2
1202             , p_asset_id              OUT NOCOPY  tab_num_type
1203             , p_nbv              OUT NOCOPY  tab_num_type
1204 , p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) RETURN BOOLEAN IS
1205 
1206    l_calling_fn   varchar2(60) := 'post_impairment';
1207 
1208 
1209    l_rbs_name       VARCHAR2(30);
1210    l_sql_stmt      varchar2(101);
1211 
1212    l_msg_count         NUMBER := 0;
1213    l_msg_data          VARCHAR2(2000) := NULL;
1214    l_precision         NUMBER(15);
1215 
1216   CURSOR c_get_currency_info(c_set_of_books_id number) IS
1217       SELECT curr.precision
1218       FROM   fnd_currencies curr
1219            , gl_sets_of_books sob
1220       WHERE  sob.set_of_books_id = c_set_of_books_id
1221       AND    curr.currency_code  = sob.currency_code;
1222 
1223    CURSOR c_get_impairments IS
1224       select imp.rowid
1225            , imp.impairment_id
1226            , imp.cash_generating_unit_id
1227            , greatest(nvl(imp.net_selling_price, 0),nvl(imp.value_in_use, 0))
1228            , ROUND(imp.impairment_amount,l_precision)
1229            , nvl(sum(itf.net_book_value),0)
1230            , imp.asset_id -- Bug# 7000391
1231            , nvl(books.sorp_enabled_flag, 'N')  -- Bug 6666666
1232            , imp.net_selling_price --Bug# 7511258
1233            , imp.value_in_use      --Bug# 7511258
1234       from   fa_impairments imp
1235            , fa_itf_impairments itf
1236            , fa_book_controls books
1237       where  imp.request_id = p_request_id
1238       and    imp.book_type_code = p_book_type_code
1239       and    imp.impairment_id = itf.impairment_id
1240       and    nvl(itf.goodwill_asset_flag, 'N') <> 'Y'
1241       and    imp.period_counter_impaired = p_period_rec.period_counter
1242       and    books.book_type_code = p_book_type_code
1243       group by imp.rowid
1244            , imp.impairment_id
1245            , imp.cash_generating_unit_id
1246            , greatest(nvl(imp.net_selling_price, 0),nvl(imp.value_in_use, 0))
1247            , imp.impairment_amount
1248            , imp.asset_id  -- Bug# 7000391
1249            , nvl(books.sorp_enabled_flag, 'N') -- Bug 6666666
1250            , imp.net_selling_price --Bug# 7511258
1251            , imp.value_in_use ;     --Bug# 7511258
1252 
1253    CURSOR c_mc_get_impairments IS
1254       select imp.rowid
1255            , imp.impairment_id
1256            , imp.cash_generating_unit_id
1257            , greatest(nvl(imp.net_selling_price, 0),nvl(imp.value_in_use, 0))
1258            , nvl(imp.impairment_amount, 0)
1259            , nvl(sum(itf.net_book_value),0)
1260            , imp.asset_id -- Bug# 7000391
1261            , nvl(books.sorp_enabled_flag, 'N')  -- Bug 6666666
1262            , imp.net_selling_price --Bug# 7511258
1263            , imp.value_in_use      --Bug# 7511258
1264       from   fa_mc_impairments imp
1265            , fa_mc_itf_impairments itf
1266            , fa_book_controls books
1267       where  imp.request_id = p_request_id
1268       and    imp.book_type_code = p_book_type_code
1269       and    imp.impairment_id = itf.impairment_id
1270       and    nvl(itf.goodwill_asset_flag, 'N') <> 'Y'
1271       and    imp.period_counter_impaired = p_period_rec.period_counter
1272       and    books.book_type_code = p_book_type_code --8666930
1273       and    imp.set_of_books_id = p_set_of_books_id
1274       and    itf.set_of_books_id = p_set_of_books_id
1275       group by imp.rowid
1276            , imp.impairment_id
1277            , imp.cash_generating_unit_id
1278            , greatest(nvl(imp.net_selling_price, 0),nvl(imp.value_in_use, 0))
1279            , imp.impairment_amount
1280            , imp.asset_id -- Bug# 7000391
1281            , nvl(books.sorp_enabled_flag, 'N')  -- Bug 6666666
1282            , imp.net_selling_price --Bug# 7511258
1283            , imp.value_in_use;      --Bug# 7511258
1284 
1285    CURSOR c_get_sum(c_impairment_id number) IS
1286       select sum(impairment_amount)
1287       from   fa_itf_impairments
1288       where  impairment_id = c_impairment_id
1289       and    nvl(goodwill_asset_flag, 'N') <> 'Y';
1290 
1291    CURSOR c_mc_get_sum(c_impairment_id number) IS
1292       select sum(impairment_amount)
1293       from   fa_mc_itf_impairments
1294       where  impairment_id = c_impairment_id
1295       and    nvl(goodwill_asset_flag, 'N') <> 'Y'
1296       and    set_of_books_id = p_set_of_books_id;
1297 
1298    CURSOR c_get_itfs(c_impairment_id number) IS
1299       select rowid
1300            , net_book_value
1301            , impairment_amount
1302            , ytd_impairment
1303            , impairment_reserve
1304       from   fa_itf_impairments
1305       where  impairment_id = c_impairment_id
1306       and    net_book_value <> 0
1307       and    nvl(goodwill_asset_flag, 'N') <> 'Y';
1308 
1309    CURSOR c_mc_get_itfs(c_impairment_id number) IS
1310       select rowid
1311            , net_book_value
1312            , impairment_amount
1313            , ytd_impairment
1314            , impairment_reserve
1315       from   fa_mc_itf_impairments
1316       where  impairment_id = c_impairment_id
1317       and    net_book_value <> 0
1318       and    nvl(goodwill_asset_flag, 'N') <> 'Y'
1319       and    set_of_books_id = p_set_of_books_id;
1320 
1321    --Bug# 7045739 start - to process only first row if more than one rows are uploaded for an asset
1322                         --in the same request.
1323    /*Bug#9182681 - Modified for CGU) */
1324    CURSOR c_get_itf(c_asset_id number,c_cgu_id number) is
1325         select impairment_id
1326         from   fa_itf_impairments
1327         where  request_id = p_request_id
1328         and    (asset_id   = c_asset_id or cash_generating_unit_id = c_cgu_id) order by impairment_id;
1329 
1330    CURSOR c_mc_get_itf(c_asset_id number,c_cgu_id number) is
1331         select impairment_id
1332         from   fa_mc_itf_impairments
1333         where  request_id = p_request_id
1334         and    (asset_id   = c_asset_id or cash_generating_unit_id = c_cgu_id)
1335         and    set_of_books_id = p_set_of_books_id
1336         order by impairment_id;
1337 
1338   --Bug# 7045739 end
1339 
1340    --Bug# 7594562 - To check if any impairment is already posted in current period for an asset
1341    --Bug# 14595406 - Modified query
1342    CURSOR c_mc_check_imp(c_asset_id number,c_cgu_id number) is
1343         select 'POSTED'
1344         from   fa_mc_impairments
1345         where  status = 'POSTED'
1346         and     ((asset_id   = c_asset_id or cash_generating_unit_id = c_cgu_id) or
1347                 (asset_id in (select asset_id from fa_books fbs
1348                              where fbs.book_type_code = p_book_type_code
1349                                and fbs.transaction_header_id_out is null
1350                                and fbs.cash_generating_unit_id = c_cgu_id)) or
1351                    (cash_generating_unit_id = (select cash_generating_unit_id from fa_books fbs
1352                              where fbs.book_type_code = p_book_type_code
1353                                and fbs.transaction_header_id_out is null
1354                                and fbs.asset_id = c_asset_id)))
1355         and    book_type_code = p_book_type_code /*Bug# 8263733 - Added filter condition with book_type_code */
1356         AND PERIOD_COUNTER_IMPAIRED = p_period_rec.period_counter
1357         AND set_of_books_id = p_set_of_books_id;
1358 
1359    CURSOR c_check_imp(c_asset_id number,c_cgu_id number) is
1360         select 'POSTED'
1361         from   fa_impairments
1362         where  status = 'POSTED'
1363         and     ((asset_id   = c_asset_id or cash_generating_unit_id = c_cgu_id) or
1364                 (asset_id in (select asset_id from fa_books fbs
1365                              where fbs.book_type_code = p_book_type_code
1366                                and fbs.transaction_header_id_out is null
1367                                and fbs.cash_generating_unit_id = c_cgu_id)) or
1368                    (cash_generating_unit_id = (select cash_generating_unit_id from fa_books fbs
1369                              where fbs.book_type_code = p_book_type_code
1370                                and fbs.transaction_header_id_out is null
1371                                and fbs.asset_id = c_asset_id)))
1372         and    book_type_code = p_book_type_code /*Bug# 8263733 - Added filter condition with book_type_code */
1373         AND PERIOD_COUNTER_IMPAIRED = p_period_rec.period_counter;
1374 
1375    --Bug# 7594562 ends
1376 
1377    t_rowid             tab_rowid_type;
1378    t_impairment_id     tab_num15_type;
1379    t_cash_generating_unit_id tab_num15_type;
1380    t_fair_market_value tab_num_type;
1381    t_impairment_amount tab_num_type;
1382    t2_impairment_amount tab_num_type;
1383    t_net_book_value    tab_num_type;
1384    ti_rowid             tab_rowid_type;
1385    ti_net_book_value     tab_num_type;
1386    ti_ytd_impairment      tab_num_type;
1387    ti_impairment_reserve    tab_num_type;
1388    ti_impairment_amount tab_num_type;
1389    t_sorp_enabled_flag tab_char1_type; -- Bug 6666666
1390 
1391    --Bug# 7511258
1392    t_value_in_use tab_num_type;
1393    t_net_selling_price tab_num_type;
1394 
1395    l_sum_impairment_amount number;
1396    l_remainder             number;
1397    l_allocation_complete   boolean := FALSE;
1398 
1399    l_impairment_id number; --Bug# 7045739
1400 
1401    l_check_imp_flag varchar(15); --Bug# 7594562
1402 
1403    calc_err           exception;
1404 
1405 BEGIN
1406 
1407    if (p_log_level_rec.statement_level) then
1408       fa_debug_pkg.add(l_calling_fn,'calc_total_nbv', 'BEGIN', p_log_level_rec => p_log_level_rec);
1409       fa_debug_pkg.add(l_calling_fn,'period_counter', p_period_rec.period_counter, p_log_level_rec => p_log_level_rec);
1410    end if;
1411 
1412    OPEN c_get_currency_info(p_set_of_books_id);
1413    FETCH c_get_currency_info INTO l_precision;
1414    CLOSE c_get_currency_info;
1415 
1416 
1417 
1418 --
1419 -- May need to create outer loop to limit records to fetch
1420 --
1421    if (p_log_level_rec.statement_level) then
1422       fa_debug_pkg.add(l_calling_fn,'Fetching', 'from FA_IMPAIRMENTS', p_log_level_rec => p_log_level_rec);
1423    end if;
1424 
1425    if (p_mrc_sob_type_code = 'R') then
1426       OPEN c_mc_get_impairments;
1427       FETCH c_mc_get_impairments BULK COLLECT INTO t_rowid
1428                                                  , t_impairment_id
1429                                                  , t_cash_generating_unit_id
1430                                                  , t_fair_market_value
1431                                                  , t2_impairment_amount
1432                                                  , t_net_book_value
1433                                                  , p_asset_id -- Bug #7000391
1434                                                  , t_sorp_enabled_flag -- Bug 6666666
1435                                                  , t_net_selling_price --Bug# 7511258
1436                                                  , t_value_in_use; --Bug# 7511258
1437       CLOSE c_mc_get_impairments;
1438    else
1439       OPEN c_get_impairments;
1440       FETCH c_get_impairments BULK COLLECT INTO t_rowid
1441                                               , t_impairment_id
1442                                               , t_cash_generating_unit_id
1443                                               , t_fair_market_value
1444                                               , t2_impairment_amount
1445                                               , t_net_book_value
1446                                               , p_asset_id -- Bug #7000391
1447                                               , t_sorp_enabled_flag --Bug 6666666
1448                                               , t_net_selling_price --Bug# 7511258
1449                                               , t_value_in_use; --Bug# 7511258
1450       CLOSE c_get_impairments;
1451    end if;
1452 
1453    if (p_log_level_rec.statement_level) then
1454       fa_debug_pkg.add(l_calling_fn,'Fecthed impairments', t_impairment_id.count, p_log_level_rec => p_log_level_rec);
1455       fa_debug_pkg.add(l_calling_fn,'fmv', t_fair_market_value(1));
1456       fa_debug_pkg.add(l_calling_fn,'nbv', t_net_book_value(1));
1457       fa_debug_pkg.add(l_calling_fn,'Updating', 'FA_IMPAIRMENTS', p_log_level_rec => p_log_level_rec);
1458    end if;
1459 
1460    if (p_mrc_sob_type_code = 'R') then
1461       FORALL i in 1..t_impairment_id.count
1462          UPDATE FA_MC_IMPAIRMENTS IMP
1463          SET    NET_BOOK_VALUE = t_net_book_value(i)
1464               , IMPAIRMENT_AMOUNT = round(decode(nvl(t2_impairment_amount(i), 0), 0,
1465                                            t_net_book_value(i) - t_fair_market_value(i), t2_impairment_amount(i)),l_precision)
1466               , IMPAIRMENT_DATE = nvl(IMPAIRMENT_DATE, p_transaction_date)
1467 	      , NET_SELLING_PRICE = round(t_net_selling_price(i),l_precision)
1468 	      , VALUE_IN_USE = round(t_value_in_use(i),l_precision)
1469          WHERE  ROWID = t_rowid(i)
1470          RETURNING IMPAIRMENT_AMOUNT BULK COLLECT INTO t_impairment_amount;
1471    else
1472       FORALL i in 1..t_impairment_id.count
1473          UPDATE FA_IMPAIRMENTS IMP
1474          SET    NET_BOOK_VALUE = t_net_book_value(i)
1475               , IMPAIRMENT_AMOUNT = round(decode(nvl(t2_impairment_amount(i), 0), 0,
1476                                            t_net_book_value(i) - t_fair_market_value(i), t2_impairment_amount(i)),l_precision)
1477               , IMPAIRMENT_DATE = nvl(IMPAIRMENT_DATE, p_transaction_date)
1478 	      , NET_SELLING_PRICE = round(t_net_selling_price(i),l_precision)
1479 	      , VALUE_IN_USE = round(t_value_in_use(i),l_precision)
1480          WHERE  ROWID = t_rowid(i)
1481          RETURNING IMPAIRMENT_AMOUNT BULK COLLECT INTO t_impairment_amount;
1482    end if;
1483 
1484    --
1485    -- Allocate
1486    --
1487    if (p_log_level_rec.statement_level) then
1488       fa_debug_pkg.add(l_calling_fn,'Updating', 'FA_ITF_IMPAIRMENTS', p_log_level_rec => p_log_level_rec);
1489    end if;
1490 
1491    if (p_mrc_sob_type_code = 'R') then
1492       FORALL i in 1..t_impairment_id.count
1493         UPDATE FA_MC_ITF_IMPAIRMENTS ITF
1494          SET    IMPAIRMENT_AMOUNT = least(round(t_impairment_amount(i) *
1495                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1496                                           NET_BOOK_VALUE)
1497               , YTD_IMPAIRMENT = YTD_IMPAIRMENT + least(round(t_impairment_amount(i) *
1498                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1499                                           NET_BOOK_VALUE)
1500               , impairment_reserve = impairment_reserve + least(round(t_impairment_amount(i) *
1501                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1502                                           NET_BOOK_VALUE)
1503               , NET_SELLING_PRICE = least(round(t_net_selling_price(i) *
1504                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1505                                           NET_BOOK_VALUE) --Bug# 7511258,9182681
1506               , VALUE_IN_USE = least(round(t_value_in_use(i) *
1507                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1508                                           NET_BOOK_VALUE) --Bug# 7511258,9182681
1509          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1510          AND    nvl(GOODWILL_ASSET_FLAG, 'N') <> 'Y'
1511          AND    t_net_book_value(i) <> 0
1512          AND    set_of_books_id = p_set_of_books_id;  -- Bug# 6920854
1513 
1514       FORALL i in 1..t_impairment_id.count
1515          UPDATE FA_MC_ITF_IMPAIRMENTS ITF
1516          SET    NET_BOOK_VALUE = COST - DEPRN_RESERVE - impairment_reserve
1517          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1518          AND    GOODWILL_ASSET_FLAG = 'Y'
1519          AND    set_of_books_id = p_set_of_books_id;
1520 
1521    else
1522       FORALL i in 1..t_impairment_id.count
1523          UPDATE FA_ITF_IMPAIRMENTS ITF
1524          SET    IMPAIRMENT_AMOUNT = least(round(t_impairment_amount(i) *
1525                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1526                                           NET_BOOK_VALUE)
1527               , YTD_IMPAIRMENT = YTD_IMPAIRMENT + least(round(t_impairment_amount(i) *
1528                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1529                                           NET_BOOK_VALUE)
1530               , impairment_reserve = impairment_reserve + least(round(t_impairment_amount(i) *
1531                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1532                                           NET_BOOK_VALUE)
1533               , NET_SELLING_PRICE = least(round(t_net_selling_price(i) *
1534                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1535                                           NET_BOOK_VALUE) --Bug# 7511258,9182681
1536               , VALUE_IN_USE = least(round(t_value_in_use(i) *
1537                                           (NET_BOOK_VALUE/t_net_book_value(i)), l_precision),
1538                                           NET_BOOK_VALUE) --Bug# 7511258,9182681
1539          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1540          AND    nvl(GOODWILL_ASSET_FLAG, 'N') <> 'Y'
1541          AND    t_net_book_value(i) <> 0; -- Bug# 6920854
1542       FORALL i in 1..t_impairment_id.count
1543          UPDATE FA_ITF_IMPAIRMENTS ITF
1544          SET    NET_BOOK_VALUE = COST - DEPRN_RESERVE - impairment_reserve
1545          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1546          AND    GOODWILL_ASSET_FLAG = 'Y';
1547 
1548    end if;
1549    p_nbv := t_net_book_value; -- Bug# 7000391
1550 -- Get sum of impairments from itf
1551 -- compare it against one in t_impairment_amount
1552 -- if it is different, go reallocate
1553 -- set period counter fully reserved if it is fully reserved
1554 
1555    FOR i in 1..t_impairment_id.count LOOP
1556 
1557       if (t_cash_generating_unit_id is not null) then
1558          --Bug#7594562 start - to check if an impairment is already posted in current open period for this asset.
1559          --Bug# 7632938 - Initialize value of l_check_imp_flag.
1560          l_check_imp_flag := null;
1561          if p_mrc_sob_type_code = 'R' then
1562             OPEN c_mc_check_imp(p_asset_id(i),t_cash_generating_unit_id(i));
1563             FETCH c_mc_check_imp INTO l_check_imp_flag;
1564             CLOSE c_mc_check_imp;
1565          else
1566             OPEN c_check_imp(p_asset_id(i),t_cash_generating_unit_id(i));
1567             FETCH c_check_imp INTO l_check_imp_flag;
1568             CLOSE c_check_imp;
1569          end if;
1570          if nvl(l_check_imp_flag,'NOTPOSTED') = 'POSTED' then
1571             p_nbv(i) := -2;
1572          else
1573          --Bug#7594562 end
1574                  --Bug# 7045739 start - to process only first row if more than one rows are uploaded for an asset
1575                                      -- in the same request.
1576                  if p_mrc_sob_type_code = 'R' then
1577                     OPEN c_mc_get_itf(p_asset_id(i),t_cash_generating_unit_id(i));
1578                     FETCH c_mc_get_itf INTO l_impairment_id;
1579                     CLOSE c_mc_get_itf;
1580                  else
1581                     OPEN c_get_itf(p_asset_id(i),t_cash_generating_unit_id(i));
1582                     FETCH c_get_itf INTO l_impairment_id;
1583                     CLOSE c_get_itf;
1584                  end if;
1585 
1586                  if  l_impairment_id <> t_impairment_id(i) then
1587                      --Not a first row if more than one rows are uploaded for an asset in the same request
1588                      --set flag to reject it.
1589                      p_nbv(i) := -1;
1590                  else
1591                      --first row if more than one rows are uploaded for an asset in the same request
1592                      if t_impairment_amount(i) > t_net_book_value(i) then
1593                          --Impairment loss is greater then NBV after considering current month depreciation
1594                          --set flag to show warning.
1595                          p_nbv(i) := 0;
1596                       /*Bug#8555199 - Negative or zero impairmnet loss is not allowed
1597 		        Bug#9307239 - To allow negative impairment loss for non sorp books*/
1598                      elsif (t_sorp_enabled_flag(i) = 'Y' and t_impairment_amount(i) < 0 ) or t_impairment_amount(i) = 0 then
1599                          p_nbv(i) := -3;
1600                      else
1601                 --Bug# 7045739 end
1602                          if p_mrc_sob_type_code = 'R' then
1603                             OPEN c_mc_get_sum(t_impairment_id(i));
1604                             FETCH c_mc_get_sum INTO l_sum_impairment_amount;
1605                             CLOSE c_mc_get_sum;
1606                          else
1607                             OPEN c_get_sum(t_impairment_id(i));
1608                             FETCH c_get_sum INTO l_sum_impairment_amount;
1609                             CLOSE c_get_sum;
1610                          end if;
1611 
1612 
1613                          if t_impairment_amount(i) <> l_sum_impairment_amount then
1614                             l_remainder := t_impairment_amount(i) - l_sum_impairment_amount;
1615                             if t_net_book_value(i) <> 0 then -- Bug# 7000391
1616                                     t_net_book_value(i) := t_net_book_value(i) - l_remainder;
1617                             end if;
1618                             if p_mrc_sob_type_code = 'R' then
1619                                OPEN c_mc_get_itfs(t_impairment_id(i));
1620                             else
1621                                OPEN c_get_itfs(t_impairment_id(i));
1622                             end if;
1623 
1624                             LOOP -- Outer Loop
1625                                ti_rowid.delete;
1626                                ti_net_book_value.delete;
1627                                ti_impairment_amount.delete;
1628                                ti_ytd_impairment.delete;
1629                                ti_impairment_reserve.delete;
1630 
1631                                if p_mrc_sob_type_code = 'R' then
1632                                   FETCH c_mc_get_itfs BULK COLLECT INTO ti_rowid
1633                                                                       , ti_net_book_value
1634                                                                       , ti_impairment_amount
1635                                                                       , ti_ytd_impairment
1636                                                                       , ti_impairment_reserve;
1637                                else
1638                                   FETCH c_get_itfs BULK COLLECT INTO ti_rowid
1639                                                                    , ti_net_book_value
1640                                                                    , ti_impairment_amount
1641                                                                    , ti_ytd_impairment
1642                                                                    , ti_impairment_reserve;
1643                                end if;
1644 
1645                                if (ti_rowid.count = 0) then
1646                                   if p_mrc_sob_type_code = 'R' then
1647                                      CLOSE c_mc_get_itfs;
1648                                   else
1649                                      CLOSE c_get_itfs;
1650                                   end if;
1651 
1652                                   EXIT;
1653 
1654                                end if;
1655 
1656                                FOR j in 1..ti_rowid.count LOOP
1657 
1658                                   if (ti_net_book_value(j) < l_remainder) then
1659                                      -- remaining nbv is smaller than remaining impairment amounts
1660                                      ti_impairment_amount(j) := ti_impairment_amount(j) + ti_net_book_value(j);
1661                                      ti_ytd_impairment(j) := ti_ytd_impairment(j) + ti_net_book_value(j);
1662                                      ti_impairment_reserve(j) := ti_impairment_reserve(j) + ti_net_book_value(j);
1663                                      l_remainder := l_remainder - ti_net_book_value(j);
1664                                      ti_net_book_value(j) := 0;
1665                                   else
1666                                      -- This line can take all of remainder
1667                                      ti_impairment_amount(j) := ti_impairment_amount(j) + l_remainder;
1668                                      ti_ytd_impairment(j) := ti_ytd_impairment(j) + l_remainder;
1669                                      ti_impairment_reserve(j) := ti_impairment_reserve(j) + l_remainder;
1670                                      ti_net_book_value(j) := ti_net_book_value(j) - l_remainder;
1671                                      l_allocation_complete := TRUE;
1672                                      EXIT;
1673                                   end if;
1674 
1675                                END LOOP; -- j in 1..ti_rowid.count
1676 
1677                                if p_mrc_sob_type_code = 'R' then
1678                                   FORALL j in 1..ti_rowid.count
1679                                      UPDATE FA_MC_ITF_IMPAIRMENTS
1680                                      SET    IMPAIRMENT_AMOUNT = ti_impairment_amount(j)
1681                                           , YTD_IMPAIRMENT = ti_ytd_impairment(j)
1682                                           , impairment_reserve = ti_impairment_reserve(j)
1683                                           , NET_BOOK_VALUE = ti_net_book_value(j)
1684                                      WHERE  ROWID = ti_rowid(j);
1685                                else
1686                                   FORALL j in 1..ti_rowid.count
1687                                      UPDATE FA_ITF_IMPAIRMENTS
1688                                      SET    IMPAIRMENT_AMOUNT = ti_impairment_amount(j)
1689                                           , YTD_IMPAIRMENT = ti_ytd_impairment(j)
1690                                           , impairment_reserve = ti_impairment_reserve(j)
1691                                           , NET_BOOK_VALUE = ti_net_book_value(j)
1692                                      WHERE  ROWID = ti_rowid(j);
1693                                end if;
1694 
1695                                if (l_allocation_complete) then
1696                                   if p_mrc_sob_type_code = 'R' then
1697                                      CLOSE c_mc_get_itfs;
1698                                   else
1699                                      CLOSE c_get_itfs;
1700                                   end if;
1701 
1702                                   EXIT;
1703 
1704                                end if;
1705 
1706                             END LOOP; -- Outer Loop
1707 
1708                          end if; -- t_impairment_amount(i) <> l_sum_impairment_amount
1709 
1710                      end if; -- t_impairment_amount(i) > t_net_book_value(i)
1711 
1712                  end if; -- l_impairment_id <> t_impairment_id(i)
1713 
1714          end if; --Bug#7594562 nvl(l_check_imp_flag,'N') = 'Y'
1715 
1716       end if; --(t_cash_generating_unit_id is not null)
1717 
1718    END LOOP; -- i in 1..t_impairment_id.count
1719 
1720    --
1721    -- Adjusting impairment amount and reval reserve
1722    --
1723    if (p_log_level_rec.statement_level) then
1724       fa_debug_pkg.add(l_calling_fn,'Updating impairment for reval', 'FA_ITF_IMPAIRMENTS', p_log_level_rec => p_log_level_rec);
1725    end if;
1726 
1727    if (p_mrc_sob_type_code = 'R') then
1728       FORALL i in 1..t_impairment_id.count
1729          UPDATE FA_MC_ITF_IMPAIRMENTS ITF
1730          SET    IMPAIRMENT_AMOUNT        = IMPAIRMENT_AMOUNT - least(REVAL_RESERVE, IMPAIRMENT_AMOUNT)
1731               , YTD_IMPAIRMENT           = YTD_IMPAIRMENT - least(REVAL_RESERVE, IMPAIRMENT_AMOUNT)
1732               , REVAL_RESERVE            = REVAL_RESERVE - least(REVAL_RESERVE, IMPAIRMENT_AMOUNT)
1733               , REVAL_RESERVE_ADJ_AMOUNT = least(REVAL_RESERVE, IMPAIRMENT_AMOUNT)
1734          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1735          AND    nvl(REVAL_RESERVE, 0) <> 0
1736          AND    nvl(GOODWILL_ASSET_FLAG, 'N') <> 'Y'
1737          AND    t_sorp_enabled_flag(i) <> 'Y'
1738          AND    set_of_books_id = p_set_of_books_id; -- Bug 6666666
1739 
1740    else
1741       FORALL i in 1..t_impairment_id.count
1742          UPDATE FA_ITF_IMPAIRMENTS ITF
1743          SET    IMPAIRMENT_AMOUNT        = IMPAIRMENT_AMOUNT - least(REVAL_RESERVE, IMPAIRMENT_AMOUNT)
1744               , YTD_IMPAIRMENT           = YTD_IMPAIRMENT - least(REVAL_RESERVE, IMPAIRMENT_AMOUNT)
1745               , REVAL_RESERVE            = REVAL_RESERVE - least(REVAL_RESERVE, IMPAIRMENT_AMOUNT)
1746               , REVAL_RESERVE_ADJ_AMOUNT = least(REVAL_RESERVE, IMPAIRMENT_AMOUNT)
1747          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1748          AND    nvl(REVAL_RESERVE, 0) <> 0
1749          AND    nvl(GOODWILL_ASSET_FLAG, 'N') <> 'Y'
1750          AND    t_sorp_enabled_flag(i) <> 'Y'; -- Bug 6666666;
1751 
1752    end if;
1753 
1754 
1755    if p_mrc_sob_type_code = 'R' then
1756       FORALL i in 1..t_impairment_id.count
1757          UPDATE FA_MC_IMPAIRMENTS IMP
1758          SET    NET_BOOK_VALUE = t_net_book_value(i)
1759          WHERE  ROWID = t_rowid(i);
1760   else
1761       FORALL i in 1..t_impairment_id.count
1762          UPDATE FA_IMPAIRMENTS IMP
1763          SET    NET_BOOK_VALUE = t_net_book_value(i)
1764          WHERE  ROWID = t_rowid(i);
1765    end if;
1766    --Bug#8614268
1767    FOR i in 1..g_error_flag.count LOOP
1768       if(g_error_flag(i) = 1) then
1769          p_nbv(i) := -4;
1770       end if;
1771    end loop;
1772    -- Bug# 7000391 start
1773    if p_mrc_sob_type_code = 'R' then
1774       --Bug# 7045739 start - when impairment_amount > NBV,basically to sync Asset Impairment report and Preview form.
1775       FORALL i in 1..t_impairment_id.count
1776          UPDATE FA_MC_ITF_IMPAIRMENTS ITF
1777          SET DEPRN_RESERVE = DEPRN_RESERVE + (nvl(impairment_reserve,0) - NET_BOOK_VALUE),
1778              IMPAIRMENT_AMOUNT = t_impairment_amount(i),
1779              impairment_reserve = t_impairment_amount(i),
1780              NET_BOOK_VALUE = t_net_book_value(i)
1781          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1782          AND    p_nbv(i) =0
1783          AND    set_of_books_id = p_set_of_books_id;
1784 
1785       --To set status to deprn failed when multiple rows are uploaded for an asset.
1786       FORALL i in 1..t_impairment_id.count
1787          UPDATE FA_MC_ITF_IMPAIRMENTS ITF
1788          SET    PERIOD_OF_ADDITION_FLAG = 'F'
1789          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1790          AND    p_nbv(i) in (-1,-2,-3,-4)
1791          AND    set_of_books_id = p_set_of_books_id; --Bug#7594562 Added -2
1792       --Bug# 7045739 end
1793   else
1794       --Bug# 7045739 start - when impairment_amount > NBV,basically to sync Asset Impairment report and Preview form.
1795       FORALL i in 1..t_impairment_id.count
1796          UPDATE FA_ITF_IMPAIRMENTS ITF
1797          SET DEPRN_RESERVE = DEPRN_RESERVE + (nvl(impairment_reserve,0) - NET_BOOK_VALUE),
1798              IMPAIRMENT_AMOUNT = t_impairment_amount(i),
1799              impairment_reserve = t_impairment_amount(i),
1800              NET_BOOK_VALUE = t_net_book_value(i)
1801          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1802          AND    p_nbv(i) =0;
1803 
1804       --To set status to deprn failed when multiple rows are uploaded for an asset.
1805       FORALL i in 1..t_impairment_id.count
1806          UPDATE FA_ITF_IMPAIRMENTS ITF
1807          SET    PERIOD_OF_ADDITION_FLAG = 'F'
1808          WHERE  IMPAIRMENT_ID = t_impairment_id(i)
1809          AND    p_nbv(i) in (-1,-2,-3,-4) ; --Bug#7594562 Added -2
1810       --Bug# 7045739 end
1811   end if;
1812   -- Bug# 7000391 end
1813 
1814 
1815    -- SORP specific logic for adjustment of revaluation reserve
1816    FOR i in t_impairment_id.FIRST..t_impairment_id.LAST LOOP
1817        IF t_sorp_enabled_flag(i) = 'Y' THEN
1818            IF NOT FA_SORP_IMPAIRMENT_PVT.sorp_processing(
1819                         p_request_id => p_request_id
1820                       , p_impairment_id => t_impairment_id(i)
1821                       , p_mrc_sob_type_code => p_mrc_sob_type_code
1822                       , p_set_of_books_id => p_set_of_books_id
1823                       , p_book_type_code => p_book_type_code
1824                       , p_precision => l_precision
1825                       , p_log_level_rec     => p_log_level_rec
1826            ) THEN
1827                fa_debug_pkg.add(l_calling_fn,'SORP Error - sorp_processing returned an error', 'OTHERS', p_log_level_rec => p_log_level_rec);
1828                RETURN FALSE;
1829            END IF;
1830        END IF;
1831    END LOOP;
1832 
1833    if (p_log_level_rec.statement_level) then
1834       fa_debug_pkg.add(l_calling_fn,'calc_total_nbv', 'END', p_log_level_rec => p_log_level_rec);
1835    end if;
1836 
1837    return true;
1838 
1839 EXCEPTION
1840    WHEN calc_err THEN
1841 
1842       if (p_log_level_rec.statement_level) then
1843          fa_debug_pkg.add(l_calling_fn,'EXCEPTION', 'calc_err', p_log_level_rec => p_log_level_rec);
1844          fa_debug_pkg.add(l_calling_fn,'sqlerrm', substrb(sqlerrm, 1, 200));
1845       end if;
1846 
1847       return false;
1848 
1849    WHEN OTHERS THEN
1850 
1851       if (p_log_level_rec.statement_level) then
1852          fa_debug_pkg.add(l_calling_fn,'EXCEPTION', 'OTHERS', p_log_level_rec => p_log_level_rec);
1853          fa_debug_pkg.add(l_calling_fn,'sqlerrm', substrb(sqlerrm, 1, 200));
1854       end if;
1855 
1856       return false;
1857 
1858 END calc_total_nbv;
1859 
1860 FUNCTION process_history(p_request_id        number
1861                        , p_impairment_id     number
1862                        , p_asset_id          number
1863                        , p_book_type_code    varchar2
1864                        , p_period_rec        FA_API_TYPES.period_rec_type
1865                        , p_imp_period_rec    FA_API_TYPES.period_rec_type
1866                        , p_date_placed_in_service date
1867                        , x_dpr_out           OUT NOCOPY fa_std_types.dpr_out_struct
1868                        , x_dpr_in            OUT NOCOPY fa_std_types.dpr_struct
1869                        , p_mrc_sob_type_code varchar2
1870                        , p_calling_fn        varchar2
1871 , p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) RETURN BOOLEAN IS
1872 
1873    l_calling_fn VARCHAR2(30) := 'process_history';
1874 
1875    CURSOR c_get_member_trx IS
1876     select th.transaction_header_id
1877          , th.transaction_type_code
1878          , th.transaction_subtype
1879          , th.transaction_key
1880          , (fy.fiscal_year * ct.NUMBER_PER_FISCAL_YEAR + cp.PERIOD_NUM +
1881             decode(th.transaction_key, 'IM', 1, 0)) period_counter
1882          , (inbk.cost - nvl(outbk.cost, 0))
1883          , (nvl(inbk.cip_cost, 0) - nvl(outbk.cip_cost, 0))
1884 --         , (decode(inbk.salvage_type,
1885 --                          outbk.salvage_type,
1886 --                             inbk.salvage_value - nvl(outbk.salvage_value, 0),
1887 --                             inbk.salvage_value))
1888          , inbk.salvage_value - nvl(outbk.salvage_value, 0)
1889 --         , (decode(inbk.deprn_limit_type,
1890 --                       outbk.deprn_limit_type,
1891 --                          nvl(decode(inbk.deprn_limit_type, 'NONE', inbk.salvage_value,
1892 --                                                                    inbk.allowed_deprn_limit_amount), 0) -
1893 --                          nvl(decode(outbk.deprn_limit_type, 'NONE', outbk.salvage_value,
1894 --                                                                     outbk.allowed_deprn_limit_amount), 0),
1895 --                          nvl(decode(inbk.deprn_limit_type, 'NONE', inbk.salvage_value,
1896 --                                                                    inbk.allowed_deprn_limit_amount), 0)))
1897          , nvl(inbk.allowed_deprn_limit_amount, 0) - nvl(outbk.allowed_deprn_limit_amount, 0)
1898          , inbk.salvage_type
1899          , nvl(outbk.salvage_type, inbk.salvage_type)
1900          , inbk.percent_salvage_value
1901          , nvl(outbk.percent_salvage_value, 0)
1902          , inbk.deprn_limit_type
1903          , nvl(outbk.deprn_limit_type, inbk.deprn_limit_type)
1904          , inbk.allowed_deprn_limit
1905          , nvl(outbk.allowed_deprn_limit, null)
1906          , inbk.deprn_method_code
1907          , inbk.life_in_months
1908          , inbk.adjusted_rate
1909          , inbk.production_capacity
1910     from   fa_transaction_headers th
1911          , fa_books inbk
1912          , fa_books outbk
1913          , fa_calendar_types ct
1914          , fa_calendar_periods cp
1915          , fa_fiscal_year fy
1916     where  th.asset_id = p_asset_id
1917     and    th.book_type_code = p_book_type_code
1918     and    inbk.asset_id = th.asset_id
1919     and    inbk.asset_id = outbk.asset_id(+)
1920     and    inbk.book_type_code = outbk.book_type_code(+)
1921     and    inbk.transaction_header_id_in = th.transaction_header_id
1922     and    inbk.transaction_header_id_in = outbk.transaction_header_id_out(+)
1923     and    fy.fiscal_year_name = fa_cache_pkg.fazcbc_record.fiscal_year_name
1924     and    ct.fiscal_year_name = fa_cache_pkg.fazcbc_record.fiscal_year_name
1925     and    ct.calendar_type = fa_cache_pkg.fazcbc_record.deprn_calendar
1926     and    cp.calendar_type = ct.calendar_type
1927     and    cp.start_date between fy.start_date and fy.end_date
1928     and    fa_cache_pkg.fazcbc_record.last_period_counter + 1 >=
1929                                    fy.fiscal_year * ct.NUMBER_PER_FISCAL_YEAR + cp.PERIOD_NUM
1930     and (
1931          (
1932            th.transaction_type_code not in ('TRANSFER OUT', 'TRANSFER IN',
1933                                             'TRANSFER', 'TRANSFER IN/VOID',
1934                                             'RECLASS', 'UNIT ADJUSTMENT',
1935                                             'REINSTATEMENT', 'ADDITION/VOID',
1936                                             'ADDITION', 'CIP ADDITION',
1937                                             'CIP ADJUSTMENT', 'CIP ADDITION/VOID')
1938            and decode(th.transaction_subtype,
1939                   'EXPENSED', p_date_placed_in_service,
1940                               greatest(nvl(th.amortization_start_date,
1941                                            th.transaction_date_entered), p_date_placed_in_service))
1942                                       between cp.start_date
1943                                           and cp.end_date
1944          )
1945          or
1946          (
1947            th.transaction_type_code = 'ADDITION'
1948             and (
1949                   (
1950                     exists (select 1 from fa_deprn_summary ds
1951                             where ds.deprn_reserve <> 0
1952                             and   ds.book_type_code = p_book_type_code
1953                             and   ds.asset_id = p_asset_id
1954                             and   ds.deprn_source_code = 'BOOKS')
1955                     and decode(th.transaction_subtype,
1956                                      'AMORTIZED', th.amortization_start_date,
1957                                                   (select dp.calendar_period_open_date
1958                                                    from fa_deprn_periods dp
1959                                                    where dp.book_type_code = p_book_type_code
1960                                                    and   th.date_effective
1961                                                        between period_open_date
1962                                                            and nvl(period_close_date, sysdate)))
1963                                                                                          between cp.start_date
1964                                                                                              and cp.end_date
1965                   )
1966                  or
1967                   (
1968                     greatest(nvl(th.amortization_start_date,
1969                                   th.transaction_date_entered), p_date_placed_in_service)
1970 --to_date('01-JAN-1996', 'DD-MON-YYYY')
1971                                       between cp.start_date
1972                                           and cp.end_date
1973                     and exists (select 1 from fa_deprn_summary ds
1974                             where ds.deprn_reserve = 0
1975                             and   ds.book_type_code = p_book_type_code
1976                             and   ds.asset_id = p_asset_id
1977                             and   ds.deprn_source_code = 'BOOKS')
1978                      and not exists(select 1 from fa_transaction_headers th2
1979                                     where asset_id = p_asset_id
1980                                     and book_type_code = p_book_type_code
1981                                     and transaction_type_code not in ('TRANSFER OUT', 'TRANSFER IN',
1982                                                                       'TRANSFER', 'TRANSFER IN/VOID',
1983                                                                       'RECLASS', 'UNIT ADJUSTMENT',
1984                                                                       'REINSTATEMENT', 'ADDITION/VOID',
1985                                                                       'ADDITION', 'CIP ADDITION',
1986                                                                       'CIP ADJUSTMENT', 'CIP ADDITION/VOID')
1987                                     and nvl(th2.amortization_start_date, th2.transaction_date_entered) <
1988                                                   nvl(th.amortization_start_date, th.transaction_date_entered)
1989                                     )
1990                   )
1991                 )
1992          )
1993         )
1994 ;
1995 
1996    --
1997    -- Get all possible period information that the asset needs
1998    --
1999    CURSOR c_get_period_rec IS
2000      select fy.fiscal_year * ct.NUMBER_PER_FISCAL_YEAR + cp.PERIOD_NUM period_counter
2001           , cp.start_date calendar_period_open_date
2002           , cp.end_date calendar_period_close_date
2003           , cp.period_num period_num
2004           , fy.fiscal_year fiscal_year
2005           , 0       cost
2006           , 0       recoverable_cost
2007           , 0       adjusted_recoverable_cost
2008           , 0       adjusted_cost
2009           , 0       reval_amortization_basis
2010           , null    ceiling_name
2011           , null    bonus_rule
2012           , null    allowed_deprn_limit
2013           , null       percent_salvage_value
2014           , null       salvage_value
2015           , 0       change_in_sal
2016           , null    allowed_deprn_limit_amount
2017           , 0       change_in_limit
2018           , 'YES'   depreciate_flag
2019           , p_date_placed_in_service date_placed_in_service
2020           , null    deprn_method_code
2021           , 0       life_in_months
2022           , 0       adjusted_rate
2023           , 0       production_capacity
2024           , 0       adjusted_capacity
2025           , 0       expense_adjustment_amount
2026           , 0       reserve_adjustment_amount
2027           , 0       ytd_proceeds_of_sale
2028           , 0       ltd_proceeds_of_sale
2029           , 0       ytd_cost_of_removal
2030           , 0       ltd_cost_of_removal
2031           , 0       change_in_cost
2032           , 0       change_in_cip_cost
2033           , 'N'     reset_adjusted_cost_flag
2034           , 0       transaction_header_id
2035           , 0       change_in_retirements_cost
2036           , 0       change_in_eofy_reserve
2037           , 0       reval_reserve
2038           , 0       bonus_deprn_amount
2039           , 0       bonus_ytd_deprn
2040           , 0       bonus_deprn_reserve
2041           , 0       impairment_amount
2042           , 0       ytd_impairment
2043           , 0       impairment_reserve
2044           , 1       rate_adjustment_factor
2045           , 1       formula_factor
2046           , null    salvage_type
2047           , null    deprn_limit_type
2048           , 0       deprn_amount
2049           , 0       ytd_deprn
2050           , 0       deprn_reserve
2051           , 0       ltd_production
2052           , 0       ytd_production
2053           , 0       production
2054           , 'Y'     capitalized_flag
2055           , 0       unplanned_amount
2056           , 0       eofy_adj_cost
2057           , 0       eofy_formula_factor
2058           , 0       eofy_reserve
2059           , 0       eop_adj_cost
2060           , 0       bonus_rate
2061      from   fa_fiscal_year fy
2062           , fa_calendar_types ct
2063           , fa_calendar_periods cp
2064      where  ct.calendar_type = fa_cache_pkg.fazcbc_record.deprn_calendar
2065      and    fy.fiscal_year_name = fa_cache_pkg.fazcbc_record.fiscal_year_name
2066      and    ct.fiscal_year_name = fa_cache_pkg.fazcbc_record.fiscal_year_name
2067      and    ct.calendar_type = cp.calendar_type
2068      and    cp.start_date between fy.start_date and fy.end_date
2069      and    fa_cache_pkg.fazcbc_record.last_period_counter + 1 >=
2070                   fy.fiscal_year * ct.NUMBER_PER_FISCAL_YEAR + cp.PERIOD_NUM
2071      and    cp.end_date >= p_date_placed_in_service
2072      order by period_counter;
2073 
2074    --
2075    -- If this is slow, think about modifying c_get_member_trx to have
2076    -- period counter created as a paramater
2077    --
2078    CURSOR c_get_adj_amount (c_transaction_header_id number
2079                           , c_source_type_code varchar2
2080                           , c_adjustment_type  varchar2) IS
2081       SELECT sum(decode(debit_credit_flag, 'CR', -1, 1) * adjustment_amount)
2082       FROM   fa_adjustments
2083       WHERE  transaction_header_id = c_transaction_header_id
2084       AND    asset_id              = p_asset_id
2085       AND    book_type_code        = p_book_type_code
2086       AND    source_type_code      = c_source_type_code
2087       AND    adjustment_type       = c_adjustment_type;
2088 
2089    --
2090    -- This cursor to get initial reserve amount from b row
2091    --
2092    CURSOR c_get_init_rsv IS
2093       SELECT ytd_deprn
2094            , deprn_reserve
2095       FROM   fa_deprn_summary
2096       WHERE  asset_id = p_asset_id
2097       AND    book_type_code        = p_book_type_code
2098       AND    deprn_source_code = 'BOOKS';
2099 
2100    CURSOR c_get_ret_info (c_transaction_header_id number) IS
2101       select ret.cost_retired
2102       from   fa_retirements ret
2103       where  ret.transaction_header_id_in = c_transaction_header_id
2104       and    ret.transaction_header_id_out is null;
2105 
2106 
2107    l_cost_retired                number;
2108 
2109 
2110    tt_transaction_header_id      tab_num15_type;
2111    tt_transaction_type_code      tab_char30_type;
2112    tt_transaction_subtype        tab_char30_type;
2113    tt_transaction_key            tab_char3_type;
2114    tt_period_counter             tab_num15_type;
2115    tt_cost                       tab_num_type;
2116    tt_cip_cost                   tab_num_type;
2117    tt_salvage_value              tab_num_type;
2118    tt_allowed_deprn_limit_amount tab_num_type;
2119    tt_salvage_type_in            tab_char30_type;
2120    tt_salvage_type_out           tab_char30_type;
2121    tt_percent_salvage_value_in   tab_num_type;
2122    tt_percent_salvage_value_out  tab_num_type;
2123    tt_deprn_limit_type_in        tab_char30_type;
2124    tt_deprn_limit_type_out       tab_char30_type;
2125    tt_allowed_deprn_limit_in     tab_num_type;
2126    tt_allowed_deprn_limit_out    tab_num_type;
2127    tt_deprn_method_code          tab_char30_type;
2128    tt_life_in_months             tab_num15_type;
2129    tt_adjusted_rate              tab_num_type;
2130    tt_production_capacity        tab_num_type;
2131 
2132    tbs_transaction_header_id     tab_num15_type;
2133    tbs_change_in_sal             tab_num_type;
2134    tbs_change_in_limit           tab_num_type;
2135    tbs_change_in_retirements_cost tab_num_type;
2136 
2137    l_trans_rec                   FA_API_TYPES.trans_rec_type;
2138    l_asset_hdr_rec               FA_API_TYPES.asset_hdr_rec_type;
2139    l_asset_fin_rec_old           FA_API_TYPES.asset_fin_rec_type;
2140    l_asset_fin_rec_new           FA_API_TYPES.asset_fin_rec_type;
2141    l_asset_deprn_rec             FA_API_TYPES.asset_deprn_rec_type;
2142    l_asset_type_rec              FA_API_TYPES.asset_type_rec_type;
2143    l_period_rec                  FA_API_TYPES.period_rec_type;
2144 
2145    l_dpr_in          fa_std_types.dpr_struct;
2146    l_dpr_out         fa_std_types.dpr_out_struct;
2147    l_dpr_arr         fa_std_types.dpr_arr_type;
2148 
2149    loc                           binary_integer;
2150    l_start_pc                    number(15);
2151 
2152    l_running_mode                number := fa_std_types.FA_DPR_NORMAL;
2153 
2154    l_temp_num                    number;
2155 
2156    l_eofy_rec_cost               number := 0;
2157    l_eop_rec_cost                number := 0;
2158    l_eofy_sal_val                number := 0;
2159    l_eop_sal_val                 number := 0;
2160    l_eofy_ind                    binary_integer;
2161    l_bs_ind                      binary_integer;
2162    l_adjusted_ind                binary_integer;
2163 
2164    l_fiscal_year                 number;
2165    l_period_num                  number;
2166    l_period_counter              number;
2167 
2168    l_source_type_code            varchar2(15);
2169    l_adjustment_type             varchar2(15);
2170    l_adj_amount                  number;
2171    l_ret_imp_amount              number;
2172 
2173    l_process_this_trx            boolean := TRUE;  -- Become FALSE in the loop if ret has reinstated
2174    l_dummy_bool                  boolean;
2175 
2176    l_db_event_type               varchar2(30); -- passed to deprn basis rule function
2177    l_asset_retire_rec            fa_api_types.asset_retire_rec_type; -- passed to deprn basis rule function
2178    l_recoverable_cost            number;  -- passed to deprn basis rule function
2179    l_salvage_value               number; -- passed to deprn basis rule function
2180 
2181    l_ytd_deprn                   number; -- used with c_get_init_rsv
2182    l_deprn_reserve               number; -- used with c_get_init_rsv
2183 
2184    l_skip                        boolean := TRUE;
2185 
2186    pro_err EXCEPTION;
2187 
2188 BEGIN
2189 
2190    if (p_log_level_rec.statement_level) then
2191       fa_debug_pkg.add(l_calling_fn,'BEGIN', p_asset_id, p_log_level_rec => p_log_level_rec);
2192    end if;
2193 
2194    OPEN c_get_period_rec;
2195    FETCH c_get_period_rec BULK COLLECT INTO fa_amort_pvt.t_period_counter
2196                                           , fa_amort_pvt.t_calendar_period_open_date
2197                                           , fa_amort_pvt.t_calendar_period_close_date
2198                                           , fa_amort_pvt.t_period_num
2199                                           , fa_amort_pvt.t_fiscal_year
2200                                           , fa_amort_pvt.t_cost
2201                                           , fa_amort_pvt.t_recoverable_cost
2202                                           , fa_amort_pvt.t_adjusted_recoverable_cost
2203                                           , fa_amort_pvt.t_adjusted_cost
2204                                           , fa_amort_pvt.t_reval_amortization_basis
2205                                           , fa_amort_pvt.t_ceiling_name
2206                                           , fa_amort_pvt.t_bonus_rule
2207                                           , fa_amort_pvt.t_allowed_deprn_limit
2208                                           , fa_amort_pvt.t_percent_salvage_value
2209                                           , fa_amort_pvt.t_salvage_value
2210                                           , tbs_change_in_sal
2211                                           , fa_amort_pvt.t_allowed_deprn_limit_amount
2212                                           , tbs_change_in_limit
2213                                           , fa_amort_pvt.t_depreciate_flag
2214                                           , fa_amort_pvt.t_date_placed_in_service
2215                                           , fa_amort_pvt.t_deprn_method_code
2216                                           , fa_amort_pvt.t_life_in_months
2217                                           , fa_amort_pvt.t_adjusted_rate
2218                                           , fa_amort_pvt.t_production_capacity
2219                                           , fa_amort_pvt.t_adjusted_capacity
2220                                           , fa_amort_pvt.t_expense_adjustment_amount
2221                                           , fa_amort_pvt.t_reserve_adjustment_amount
2222                                           , fa_amort_pvt.t_ytd_proceeds_of_sale
2223                                           , fa_amort_pvt.t_ltd_proceeds_of_sale
2224                                           , fa_amort_pvt.t_ytd_cost_of_removal
2225                                           , fa_amort_pvt.t_ltd_cost_of_removal
2226                                           , fa_amort_pvt.t_change_in_cost
2227                                           , fa_amort_pvt.t_change_in_cip_cost
2228                                           , fa_amort_pvt.t_reset_adjusted_cost_flag
2229                                           , tbs_transaction_header_id
2230                                           , tbs_change_in_retirements_cost
2231                                           , fa_amort_pvt.t_change_in_eofy_reserve
2232                                           , fa_amort_pvt.t_reval_reserve
2233                                           , fa_amort_pvt.t_bonus_deprn_amount
2234                                           , fa_amort_pvt.t_bonus_ytd_deprn
2235                                           , fa_amort_pvt.t_bonus_deprn_reserve
2236                                           , fa_amort_pvt.t_impairment_amount
2237                                           , fa_amort_pvt.t_ytd_impairment
2238                                           , fa_amort_pvt.t_impairment_reserve
2239                                           , fa_amort_pvt.t_rate_adjustment_factor
2240                                           , fa_amort_pvt.t_formula_factor
2241                                           , fa_amort_pvt.t_salvage_type
2242                                           , fa_amort_pvt.t_deprn_limit_type
2243                                           , fa_amort_pvt.t_deprn_amount
2244                                           , fa_amort_pvt.t_ytd_deprn
2245                                           , fa_amort_pvt.t_deprn_reserve
2246                                           , fa_amort_pvt.t_ltd_production
2247                                           , fa_amort_pvt.t_ytd_production
2248                                           , fa_amort_pvt.t_production
2249                                           , fa_amort_pvt.t_capitalized_flag
2250                                           , fa_amort_pvt.t_unplanned_amount
2251                                           , fa_amort_pvt.t_eofy_adj_cost
2252                                           , fa_amort_pvt.t_eofy_formula_factor
2253                                           , fa_amort_pvt.t_eofy_reserve
2254                                           , fa_amort_pvt.t_eop_adj_cost
2255                                           , fa_amort_pvt.t_bonus_rate
2256     ;
2257     CLOSE c_get_period_rec;
2258 
2259 
2260    if (p_log_level_rec.statement_level) then
2261       fa_debug_pkg.add(l_calling_fn,'c_get_period_rec returned', fa_amort_pvt.t_period_counter.count,  p_log_level_rec => p_log_level_rec);
2262    end if;
2263 
2264 
2265    OPEN c_get_member_trx;
2266    FETCH c_get_member_trx BULK COLLECT INTO tt_transaction_header_id
2267                                           , tt_transaction_type_code
2268                                           , tt_transaction_subtype
2269                                           , tt_transaction_key
2270                                           , tt_period_counter
2271                                           , tt_cost
2272                                           , tt_cip_cost
2273                                           , tt_salvage_value
2274                                           , tt_allowed_deprn_limit_amount
2275                                           , tt_salvage_type_in
2276                                           , tt_salvage_type_out
2277                                           , tt_percent_salvage_value_in
2278                                           , tt_percent_salvage_value_out
2279                                           , tt_deprn_limit_type_in
2280                                           , tt_deprn_limit_type_out
2281                                           , tt_allowed_deprn_limit_in
2282                                           , tt_allowed_deprn_limit_out
2283                                           , tt_deprn_method_code
2284                                           , tt_life_in_months
2285                                           , tt_adjusted_rate
2286                                           , tt_production_capacity
2287 
2288    ;
2289    CLOSE c_get_member_trx;
2290 
2291    if (p_log_level_rec.statement_level) then
2292       fa_debug_pkg.add(l_calling_fn,'c_get_member_trx returned', tt_transaction_header_id.count, p_log_level_rec => p_log_level_rec);
2293    end if;
2294 
2295    fa_amort_pvt.t_reset_adjusted_cost_flag(1)   :=  'Y';
2296    l_start_pc := fa_amort_pvt.t_period_counter(1);
2297 
2298    if (p_log_level_rec.statement_level) then
2299       fa_debug_pkg.add(l_calling_fn,'Before trx loop', l_start_pc, p_log_level_rec => p_log_level_rec);
2300    end if;
2301 
2302    FOR i in 1..tt_transaction_header_id.count LOOP
2303 
2304       l_process_this_trx := TRUE;
2305       loc := tt_period_counter(i) - l_start_pc + 1;
2306 
2307       if (p_log_level_rec.statement_level) then
2308          fa_debug_pkg.add(l_calling_fn,'trx type code('||to_char(i)||')', tt_transaction_type_code(i));
2309          fa_debug_pkg.add(l_calling_fn,'trx key('||to_char(i)||')', tt_transaction_key(i));
2310          fa_debug_pkg.add(l_calling_fn,'trx period counter('||to_char(i)||')', tt_period_counter(i));
2311       end if;
2312 
2313       -- If this trx is ret, then check to see if it's been reinstated or not
2314       -- Do not process if it's been reinstate.
2315       if (tt_transaction_type_code(i) like '%RETIREMENT') then
2316          l_cost_retired := 0;
2317 
2318          OPEN c_get_ret_info(tt_transaction_header_id(i));
2319          FETCH c_get_ret_info INTO l_cost_retired;
2320 
2321          -- Skip this trx if this ret has reinstated
2322          if c_get_ret_info%NOTFOUND then
2323             l_process_this_trx := FALSE;
2324          else
2325             tbs_change_in_retirements_cost(loc) := tbs_change_in_retirements_cost(loc) + tt_cost(i);
2326          end if;
2327 
2328          CLOSE c_get_ret_info;
2329       end if;
2330 
2331       if (l_process_this_trx) then
2332 
2333          fa_amort_pvt.t_reset_adjusted_cost_flag(loc) := 'Y';
2334          fa_amort_pvt.t_change_in_cost(loc) := fa_amort_pvt.t_change_in_cost(loc) + tt_cost(i);
2335 
2336          if (p_log_level_rec.statement_level) then
2337             fa_debug_pkg.add(l_calling_fn,'trx type code('||to_char(i)||')', tt_transaction_type_code(i));
2338          end if;
2339 
2340          if (tt_transaction_type_code(i) = 'ADDITION') then
2341             OPEN c_get_init_rsv;
2342             FETCH c_get_init_rsv INTO l_ytd_deprn, l_deprn_reserve;
2343             CLOSE c_get_init_rsv;
2344 
2345             if (l_deprn_reserve <> 0) then
2346                fa_amort_pvt.t_ytd_deprn(loc-1) := l_ytd_deprn;
2347                fa_amort_pvt.t_deprn_reserve(loc-1) := l_deprn_reserve;
2348             end if;
2349          end if;
2350 
2351 
2352          if (p_log_level_rec.statement_level) then
2353             fa_debug_pkg.add(l_calling_fn,'tt_salvage_value('||to_char(i)||')', tt_salvage_value(i));
2354             fa_debug_pkg.add(l_calling_fn,'tt_allowed_deprn_limit_amount('||to_char(i)||')', tt_allowed_deprn_limit_amount(i));
2355          end if;
2356 
2357          tbs_change_in_sal(loc) := tbs_change_in_sal(loc) + tt_salvage_value(i);
2358          tbs_change_in_limit(loc) := tbs_change_in_limit(loc) + tt_allowed_deprn_limit_amount(i);
2359 
2360          if (p_log_level_rec.statement_level) then
2361             fa_debug_pkg.add(l_calling_fn,'tbs_change_in_sal('||to_char(loc)||')', tbs_change_in_sal(loc));
2362             fa_debug_pkg.add(l_calling_fn,'tbs_change_in_limit('||to_char(loc)||')', tbs_change_in_limit(loc));
2363             fa_debug_pkg.add(l_calling_fn,'tbs_transaction_header_id('||to_char(loc)||')', tbs_transaction_header_id(loc));
2364          end if;
2365 
2366          if (tbs_transaction_header_id(loc) < tt_transaction_header_id(i)) then
2367 
2368             if (p_log_level_rec.statement_level) then
2369                fa_debug_pkg.add(l_calling_fn,'tt_transaction_header_id('||to_char(i)||')', tt_transaction_header_id(i));
2370                fa_debug_pkg.add(l_calling_fn,'tt_salvage_type_in('||to_char(i)||')', tt_salvage_type_in(i));
2371             end if;
2372 
2373             tbs_transaction_header_id(loc)            := tt_transaction_header_id(i);
2374             fa_amort_pvt.t_deprn_method_code(loc)          := tt_deprn_method_code(i);
2375             fa_amort_pvt.t_salvage_type(loc)          := tt_salvage_type_in(i);
2376             fa_amort_pvt.t_percent_salvage_value(loc) := tt_percent_salvage_value_in(i);
2377             fa_amort_pvt.t_deprn_limit_type(loc)      := tt_deprn_limit_type_in(i);
2378             fa_amort_pvt.t_allowed_deprn_limit(loc)   := tt_allowed_deprn_limit_in(i);
2379             fa_amort_pvt.t_life_in_months(loc)        := tt_life_in_months(i);
2380             fa_amort_pvt.t_adjusted_rate(loc)         := tt_adjusted_rate(i);
2381             fa_amort_pvt.t_production_capacity(loc)   := tt_production_capacity(i);
2382          end if;
2383 
2384          if (p_log_level_rec.statement_level) then
2385             fa_debug_pkg.add(l_calling_fn,'Find Unplan and Impairment Amount', tt_transaction_key(i));
2386          end if;
2387          -- *********************
2388          -- Find unplanned amount and impairment
2389          -- *********************
2390          if (tt_transaction_key(i) in ('UE', 'UA', 'U', 'IM', 'R')) then
2391             if tt_transaction_key(i) = 'IM' then
2392                l_source_type_code := 'ADJUSTMENT';
2393                l_adjustment_type := 'IMPAIR EXPENSE';
2394             elsif (tt_transaction_type_code(i) like '%RETIREMENT') then
2395                l_source_type_code := 'RETIREMENT';
2396                l_adjustment_type := 'RESERVE';
2397             else
2398                l_source_type_code := 'DEPRECIATION';
2399                l_adjustment_type := 'EXPENSE';
2400             end if;
2401 
2402             OPEN c_get_adj_amount (tt_transaction_header_id(i)
2403                                  , l_source_type_code
2404                                  , l_adjustment_type);
2405             FETCH c_get_adj_amount INTO l_adj_amount;
2406             CLOSE c_get_adj_amount;
2407 
2408             if (p_log_level_rec.statement_level) then
2409                fa_debug_pkg.add(l_calling_fn,'l_adj_amount', l_adj_amount, p_log_level_rec => p_log_level_rec);
2410             end if;
2411 
2412             l_ret_imp_amount := 0;
2413 
2414             if tt_transaction_key(i) = 'R' then
2415                l_source_type_code := 'RETIREMENT';
2416                l_adjustment_type := 'IMPAIR RESERVE';
2417 
2418                OPEN c_get_adj_amount (tt_transaction_header_id(i)
2419                                     , l_source_type_code
2420                                     , l_adjustment_type);
2421                FETCH c_get_adj_amount INTO l_ret_imp_amount;
2422                CLOSE c_get_adj_amount;
2423 
2424 
2425             end if;
2426 
2427             if tt_transaction_key(i) = 'IM' then
2428                -- accumulation may not be necessary because we only allow a impairment per pereiod.
2429                fa_amort_pvt.t_impairment_amount(loc-1) := fa_amort_pvt.t_impairment_amount(loc-1) +
2430                                                         l_adj_amount;
2431             elsif (tt_transaction_key(i) in ('UE', 'UA', 'U')) then
2432                fa_amort_pvt.t_unplanned_amount(loc) := fa_amort_pvt.t_unplanned_amount(loc) +
2433                                                        l_adj_amount;
2434                fa_amort_pvt.t_expense_adjustment_amount(loc) := fa_amort_pvt.t_expense_adjustment_amount(loc) +
2435                                                                 l_adj_amount;
2436             elsif (tt_transaction_type_code(i) like '%RETIREMENT') then
2437                fa_amort_pvt.t_reserve_adjustment_amount(i) :=  fa_amort_pvt.t_reserve_adjustment_amount(loc) -
2438                                                                l_adj_amount;
2439                fa_amort_pvt.t_impairment_amount(loc-1) := fa_amort_pvt.t_impairment_amount(loc-1) -
2440                                                           l_ret_imp_amount;
2441             end if;
2442 
2443          end if; -- (tt_transaction_key(i) in ('UE', 'UA', 'U', 'IM')
2444 
2445       end if; -- (l_process_this_trx)
2446    END LOOP; -- i in 1..tt_transaction_header_id.count
2447 
2448    --
2449    -- Get reserve entry from fa_adjustments and fa_deprn_summary(b row)
2450    --
2451 
2452    l_asset_hdr_rec.asset_id           := p_asset_id;
2453    l_asset_hdr_rec.period_of_addition := null;
2454    l_asset_hdr_rec.book_type_code     := p_book_type_code;
2455    l_asset_hdr_rec.set_of_books_id    := fa_cache_pkg.fazcbc_record.set_of_books_id;
2456 
2457    l_dpr_in.calendar_type := fa_cache_pkg.fazcbc_record.deprn_calendar;
2458    l_dpr_in.book := l_asset_hdr_rec.book_type_code;
2459    l_dpr_in.asset_id := l_asset_hdr_rec.asset_id;
2460 
2461    if (p_log_level_rec.statement_level) then
2462       fa_debug_pkg.add(l_calling_fn,'calling', 'FA_UTIL_PVT.get_asset_fin_rec',  p_log_level_rec => p_log_level_rec);
2463    end if;
2464 
2465    -- Populate fin rec
2466    if not FA_UTIL_PVT.get_asset_fin_rec
2467               (p_asset_hdr_rec         => l_asset_hdr_rec,
2468                px_asset_fin_rec        => l_asset_fin_rec_old,
2469                p_transaction_header_id => NULL,
2470                p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
2471             raise pro_err;
2472    end if;
2473 
2474    if (p_log_level_rec.statement_level) then
2475       fa_debug_pkg.add(l_calling_fn,'period loop', 'Begin', p_log_level_rec => p_log_level_rec);
2476    end if;
2477 
2478    --
2479    -- Calculate periodic depreciation amounts
2480    --
2481    l_bs_ind := 1;
2482 
2483    l_asset_fin_rec_old.cost := 0;
2484    l_asset_fin_rec_old.recoverable_cost := 0;
2485    l_asset_fin_rec_old.adjusted_recoverable_cost := 0;
2486    l_asset_fin_rec_old.adjusted_cost := 0;
2487    l_asset_fin_rec_old.rate_adjustment_factor := 0;
2488    l_asset_fin_rec_old.formula_factor := 0;
2489    l_asset_fin_rec_old.eofy_reserve := 0;
2490    l_asset_fin_rec_old.reval_amortization_basis:= 0;
2491    l_asset_fin_rec_old.adjusted_capacity := 0;
2492    l_asset_fin_rec_new := l_asset_fin_rec_old;
2493 
2494 
2495    FOR i in 1..fa_amort_pvt.t_period_counter.count LOOP
2496 
2497       if (l_skip) and (tbs_transaction_header_id(i) = 0) then
2498 
2499          if (p_log_level_rec.statement_level) then
2500             fa_debug_pkg.add(l_calling_fn,'Skip this period', fa_amort_pvt.t_period_counter(i));
2501          end if;
2502 
2503          l_bs_ind := l_bs_ind + 1;
2504       else
2505          if (p_log_level_rec.statement_level) then
2506             fa_debug_pkg.add(l_calling_fn,'tbs_transaction_header_id('||to_char(i)||')', tbs_transaction_header_id(i));
2507          end if;
2508 
2509          l_skip := FALSE;
2510 
2511 
2512          if (i = 1) then
2513             fa_amort_pvt.t_cost(i) := fa_amort_pvt.t_cost(i) + fa_amort_pvt.t_change_in_cost(i);
2514          else
2515             fa_amort_pvt.t_cost(i) := fa_amort_pvt.t_cost(i-1) + fa_amort_pvt.t_change_in_cost(i);
2516          end if;
2517 
2518          if (p_log_level_rec.statement_level) then
2519             fa_debug_pkg.add(l_calling_fn,'t_salvage_type('||to_char(i)||')', fa_amort_pvt.t_salvage_type(i));
2520          end if;
2521 
2522          if (fa_amort_pvt.t_salvage_type(i) is null) then
2523             fa_amort_pvt.t_salvage_type(i) := fa_amort_pvt.t_salvage_type(i-1);
2524          end if;
2525 
2526          if (p_log_level_rec.statement_level) then
2527             fa_debug_pkg.add(l_calling_fn,'t_salvage_type('||to_char(i)||')', fa_amort_pvt.t_salvage_type(i));
2528          end if;
2529 
2530          if (fa_amort_pvt.t_salvage_type(i) = 'PCT') then
2531             if (p_log_level_rec.statement_level) then
2532                fa_debug_pkg.add(l_calling_fn,'t_percent_salvage_value('||to_char(i)||')', fa_amort_pvt.t_percent_salvage_value(i));
2533             end if;
2534 
2535             if (fa_amort_pvt.t_percent_salvage_value(i) is null) then
2536                if (i > 1) then
2537                   fa_amort_pvt.t_percent_salvage_value(i) := fa_amort_pvt.t_percent_salvage_value(i-1);
2538                else
2539                   fa_amort_pvt.t_percent_salvage_value(i) := 0;
2540                end if;
2541             end if;
2542 
2543             if (p_log_level_rec.statement_level) then
2544                fa_debug_pkg.add(l_calling_fn,'t_percent_salvage_value('||to_char(i)||')', fa_amort_pvt.t_percent_salvage_value(i));
2545             end if;
2546 
2547             l_temp_num := fa_amort_pvt.t_cost(i) * fa_amort_pvt.t_percent_salvage_value(i);
2548             fa_round_pkg.fa_ceil(l_temp_num, p_book_type_code, p_log_level_rec => p_log_level_rec);
2549             fa_amort_pvt.t_salvage_value(i) := l_temp_num;
2550          else
2551             if (p_log_level_rec.statement_level) then
2552                fa_debug_pkg.add(l_calling_fn,'tbs_change_in_sal('||to_char(i)||')', tbs_change_in_sal(i));
2553             end if;
2554 
2555             if (i > 1) then
2556                fa_amort_pvt.t_salvage_value(i) := fa_amort_pvt.t_salvage_value(i-1) + tbs_change_in_sal(i);
2557             else
2558                fa_amort_pvt.t_salvage_value(i) := tbs_change_in_sal(i);
2559             end if;
2560          end if;
2561 
2562          if (p_log_level_rec.statement_level) then
2563             fa_debug_pkg.add(l_calling_fn,'t_salvage_value('||to_char(i)||')', fa_amort_pvt.t_salvage_value(i));
2564             fa_debug_pkg.add(l_calling_fn,'t_deprn_limit_type('||to_char(i)||')', fa_amort_pvt.t_deprn_limit_type(i));
2565          end if;
2566 
2567          if (fa_amort_pvt.t_deprn_limit_type(i) is null) then
2568             fa_amort_pvt.t_deprn_limit_type(i) := fa_amort_pvt.t_deprn_limit_type(i-1);
2569          end if;
2570 
2571          if (p_log_level_rec.statement_level) then
2572             fa_debug_pkg.add(l_calling_fn,'t_deprn_limit_type('||to_char(i)||')', fa_amort_pvt.t_deprn_limit_type(i));
2573          end if;
2574 
2575          if (fa_amort_pvt.t_deprn_limit_type(i) = 'PCT') then
2576             if (fa_amort_pvt.t_allowed_deprn_limit(i) is null) then
2577                if (i > 1) then
2578                   fa_amort_pvt.t_allowed_deprn_limit(i) := fa_amort_pvt.t_allowed_deprn_limit(i-1);
2579                else
2580                   fa_amort_pvt.t_allowed_deprn_limit(i) := 0;
2581                end if;
2582             end if;
2583 
2584             l_temp_num := fa_amort_pvt.t_cost(i) * (1 -  fa_amort_pvt.t_allowed_deprn_limit(i));
2585             fa_round_pkg.fa_floor(l_temp_num, l_asset_hdr_rec.book_type_code, p_log_level_rec => p_log_level_rec);
2586             fa_amort_pvt.t_allowed_deprn_limit_amount(i) := l_temp_num;
2587 
2588          elsif(fa_amort_pvt.t_deprn_limit_type(i) = 'AMT') then
2589             if (i > 1) then
2590                fa_amort_pvt.t_allowed_deprn_limit_amount(i) := nvl(fa_amort_pvt.t_allowed_deprn_limit_amount(i-1), 0) +
2591                                                             tbs_change_in_limit(i);
2592             else
2593                fa_amort_pvt.t_allowed_deprn_limit_amount(i) := tbs_change_in_limit(i);
2594             end if;
2595          else  -- case of 'NONE'
2596             fa_amort_pvt.t_allowed_deprn_limit_amount(i) := null;
2597          end if;
2598 
2599          if (p_log_level_rec.statement_level) then
2600             fa_debug_pkg.add(l_calling_fn, 'sal_val('||to_char(i)||')', fa_amort_pvt.t_salvage_value(i));
2601             fa_debug_pkg.add(l_calling_fn, 'limit('||to_char(i)||')', fa_amort_pvt.t_allowed_deprn_limit_amount(i));
2602          end if;
2603 
2604          fa_amort_pvt.t_recoverable_cost(i) := fa_amort_pvt.t_cost(i) - fa_amort_pvt.t_salvage_value(i);
2605          fa_amort_pvt.t_adjusted_recoverable_cost(i) := fa_amort_pvt.t_cost(i) -
2606                                                         nvl(fa_amort_pvt.t_allowed_deprn_limit_amount(i), 0);
2607 
2608          if (p_log_level_rec.statement_level) then
2609             fa_debug_pkg.add(l_calling_fn, 'rec cost('||to_char(i)||')', fa_amort_pvt.t_recoverable_cost(i));
2610             fa_debug_pkg.add(l_calling_fn, 'adj_rec_cost('||to_char(i)||')', fa_amort_pvt.t_adjusted_recoverable_cost(i));
2611          end if;
2612 
2613          if (fa_amort_pvt.t_deprn_method_code(i) is null) then
2614             fa_amort_pvt.t_deprn_method_code(i) := fa_amort_pvt.t_deprn_method_code(i-1);
2615             fa_amort_pvt.t_adjusted_rate(i) := fa_amort_pvt.t_adjusted_rate(i-1);
2616             fa_amort_pvt.t_life_in_months(i) := fa_amort_pvt.t_life_in_months(i-1);
2617          end if;
2618 
2619          if (fa_amort_pvt.t_depreciate_flag(i) is null) then
2620             fa_amort_pvt.t_depreciate_flag(i) := fa_amort_pvt.t_depreciate_flag(i-1);
2621          end if;
2622 
2623          if (p_log_level_rec.statement_level) then
2624             fa_debug_pkg.add(l_calling_fn, 'fa_amort_pvt.t_period_counter', fa_amort_pvt.t_period_counter(i));
2625             fa_debug_pkg.add(l_calling_fn, 'p_imp_period_rec.period_counter', p_imp_period_rec.period_counter, p_log_level_rec => p_log_level_rec);
2626          end if;
2627 
2628          --
2629          -- Want to populate fa_amort_pvt tables but not necessary to calculate deprn basis
2630          -- nor periodic depreciations because impairment amount has to be determined first
2631          -- for periods after impairment transactions
2632          --
2633          if fa_amort_pvt.t_period_counter(i) <=  p_imp_period_rec.period_counter and
2634             l_bs_ind <= i then
2635 
2636             fa_amort_pvt.t_deprn_amount(i)   := nvl(fa_amort_pvt.t_expense_adjustment_amount(i), 0);
2637 
2638             if (i = 1) then
2639                fa_amort_pvt.t_ytd_deprn(i)      := fa_amort_pvt.t_deprn_amount(i);
2640                fa_amort_pvt.t_deprn_reserve(i)  := nvl(fa_amort_pvt.t_deprn_amount(i), 0) +
2641                                                              nvl(fa_amort_pvt.t_reserve_adjustment_amount(i), 0);
2642                fa_amort_pvt.t_eofy_reserve (i)  := nvl(fa_amort_pvt.t_change_in_eofy_reserve(i), 0);
2643             else
2644                fa_amort_pvt.t_deprn_reserve(i)  := fa_amort_pvt.t_deprn_reserve(i-1) +
2645                                                    nvl(fa_amort_pvt.t_deprn_amount(i), 0) +
2646                                                    nvl(fa_amort_pvt.t_reserve_adjustment_amount(i), 0);
2647                if (i > 1) then
2648                   if (fa_amort_pvt.t_period_num(i) = 1) then
2649                      fa_amort_pvt.t_ytd_deprn(i)      := fa_amort_pvt.t_deprn_amount(i);
2650                      fa_amort_pvt.t_eofy_reserve (i)       := fa_amort_pvt.t_deprn_reserve(i - 1) +
2651                                                               nvl(fa_amort_pvt.t_change_in_eofy_reserve(i), 0);
2652                   else
2653                      fa_amort_pvt.t_ytd_deprn(i)      := fa_amort_pvt.t_ytd_deprn(i-1) +
2654                                                          fa_amort_pvt.t_deprn_amount(i);
2655                      fa_amort_pvt.t_eofy_reserve (i)       := fa_amort_pvt.t_eofy_reserve(i - 1) +
2656                                                               nvl(fa_amort_pvt.t_change_in_eofy_reserve(i), 0);
2657                   end if;
2658                else
2659                   --
2660                   -- If user entered reserve exists, code below may need to be modified
2661                   --
2662                   if (fa_amort_pvt.t_period_num(i) = 1) then
2663                      fa_amort_pvt.t_eofy_reserve (i)       := 0;
2664                   else
2665                      fa_amort_pvt.t_eofy_reserve (i)       := 0;
2666                   end if;
2667                end if;
2668 
2669             end if;
2670 
2671             if (p_log_level_rec.statement_level) then
2672                fa_debug_pkg.add(l_calling_fn, 'fa_amort_pvt.t_deprn_reserve', fa_amort_pvt.t_deprn_reserve(i));
2673             end if;
2674 
2675 
2676             l_asset_fin_rec_old := l_asset_fin_rec_new;
2677             l_asset_fin_rec_new.cost := fa_amort_pvt.t_cost(i);
2678 
2679             if (p_log_level_rec.statement_level) then
2680                fa_debug_pkg.add(l_calling_fn, 'rec cost('||to_char(i)||')', fa_amort_pvt.t_recoverable_cost(i));
2681                fa_debug_pkg.add(l_calling_fn, 'adj_rec_cost('||to_char(i)||')', fa_amort_pvt.t_adjusted_recoverable_cost(i));
2682             end if;
2683 
2684             l_asset_fin_rec_new.recoverable_cost := fa_amort_pvt.t_recoverable_cost(i);
2685             l_asset_fin_rec_new.adjusted_recoverable_cost := fa_amort_pvt.t_adjusted_recoverable_cost(i);
2686             l_asset_fin_rec_new.adjusted_cost := fa_amort_pvt.t_adjusted_cost(i);
2687             l_asset_fin_rec_new.eofy_reserve := fa_amort_pvt.t_eofy_reserve(i);
2688 
2689              --
2690              -- If only trx happened in this period is ret then calculate deprn basis
2691              -- for ret mode, otherwise use amort_adj mode.
2692              --
2693              if (tbs_change_in_retirements_cost(i) <> 0) and
2694                 (tbs_change_in_retirements_cost(i) = fa_amort_pvt.t_change_in_cost(i)) then
2695                l_db_event_type := 'RETIREMENT';
2696                l_recoverable_cost := l_asset_fin_rec_new.recoverable_cost;
2697                l_salvage_value := l_asset_fin_rec_new.cost - l_asset_fin_rec_new.recoverable_cost;
2698                l_asset_retire_rec.cost_retired := -1 * tbs_change_in_retirements_cost(i);
2699             else
2700                l_db_event_type := 'AMORT_ADJ';
2701                l_recoverable_cost := null;
2702                l_salvage_value := null;
2703                l_asset_retire_rec := null;
2704             end if;
2705 
2706 
2707             l_dpr_in.adj_cost := fa_amort_pvt.t_recoverable_cost(i);
2708             l_dpr_in.salvage_value := fa_amort_pvt.t_salvage_value(i);
2709             l_dpr_in.rec_cost := fa_amort_pvt.t_recoverable_cost(i);
2710             l_dpr_in.adj_rec_cost := fa_amort_pvt.t_adjusted_recoverable_cost(i);
2711             l_dpr_in.reval_amo_basis := fa_amort_pvt.t_reval_amortization_basis(i);
2712             l_dpr_in.adj_rate := fa_amort_pvt.t_adjusted_rate(i);
2713             l_dpr_in.rate_adj_factor := 1;
2714             l_dpr_in.capacity := fa_amort_pvt.t_production_capacity(i);
2715             l_dpr_in.adj_capacity := fa_amort_pvt.t_adjusted_capacity(i);
2716             l_dpr_in.ltd_prod := 0;
2717             l_dpr_in.ytd_deprn := 0;    -- This needs to be 0 for the 1st faxcde call
2718             l_dpr_in.prior_fy_exp := 0;
2719             l_dpr_in.deprn_rsv := 0;
2720             l_dpr_in.reval_rsv := fa_amort_pvt.t_reval_reserve(i);
2721             l_dpr_in.bonus_deprn_exp := fa_amort_pvt.t_bonus_deprn_amount(i);
2722             l_dpr_in.bonus_ytd_deprn := fa_amort_pvt.t_bonus_ytd_deprn(i);
2723             l_dpr_in.bonus_deprn_rsv := fa_amort_pvt.t_bonus_deprn_reserve(i);
2724 
2725             if (i = 1) then
2726                l_dpr_in.impairment_exp := fa_amort_pvt.t_impairment_amount(i);
2727                l_dpr_in.ytd_impairment := fa_amort_pvt.t_ytd_impairment(i);
2728                l_dpr_in.impairment_rsv := fa_amort_pvt.t_impairment_reserve(i);
2729             else
2730                l_dpr_in.impairment_exp := fa_amort_pvt.t_impairment_amount(i);
2731                l_dpr_in.ytd_impairment := fa_amort_pvt.t_ytd_impairment(i-1) + l_dpr_in.impairment_exp;
2732                l_dpr_in.impairment_rsv := fa_amort_pvt.t_impairment_reserve(i-1) + l_dpr_in.impairment_exp;
2733             end if;
2734 
2735             l_dpr_in.ceil_name := fa_amort_pvt.t_ceiling_name(i);
2736             l_dpr_in.bonus_rule := fa_amort_pvt.t_bonus_rule(i);
2737             l_dpr_in.method_code := fa_amort_pvt.t_deprn_method_code(i);
2738             l_dpr_in.life        := fa_amort_pvt.t_life_in_months(i); -- bug5894464
2739             l_dpr_in.jdate_in_service :=
2740                       to_number(to_char(fa_amort_pvt.t_date_placed_in_service(i), 'J'));
2741             l_dpr_in.deprn_start_jdate := to_number(to_char(l_asset_fin_rec_old.deprn_start_date, 'J'));
2742             l_dpr_in.prorate_jdate := to_number(to_char(l_asset_fin_rec_old.prorate_date, 'J'));
2743 
2744             if (p_log_level_rec.statement_level) then
2745                fa_debug_pkg.add(l_calling_fn, 'Before Calling', 'fa_cache_pkg.fazccmt', p_log_level_rec => p_log_level_rec);
2746             end if;
2747 
2748             if (not fa_cache_pkg.fazccmt(
2749                        fa_amort_pvt.t_deprn_method_code(i),
2750                        fa_amort_pvt.t_life_in_months(i),
2751                        p_log_level_rec)) then
2752                if (p_log_level_rec.statement_level) then
2753                   fa_debug_pkg.add(l_calling_fn, 'Error calling', 'fa_cache_pkg.fazccmt', p_log_level_rec => p_log_level_rec);
2754                end if;
2755 
2756                raise pro_err;
2757             end if;
2758 
2759                if i = 1 then
2760                   if (p_log_level_rec.statement_level) then
2761                      fa_debug_pkg.add(l_calling_fn, 'Before Calling', 'fa_cache_pkg.fazccp', p_log_level_rec => p_log_level_rec);
2762                   end if;
2763 
2764                   if not fa_cache_pkg.fazccp(fa_cache_pkg.fazcbc_record.prorate_calendar,
2765                                              fa_cache_pkg.fazcbc_record.fiscal_year_name,
2766                                              l_dpr_in.prorate_jdate,
2767                                              g_temp_number,
2768                                              l_dpr_in.y_begin,
2769                                              g_temp_integer, p_log_level_rec => p_log_level_rec) then
2770                      if (p_log_level_rec.statement_level) then
2771                         fa_debug_pkg.add(l_calling_fn, 'Error calling',
2772                                          'fa_cache_pkg.fazccp', p_log_level_rec => p_log_level_rec);
2773                         fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcbc_record.prorate_calendar',
2774                                          fa_cache_pkg.fazcbc_record.prorate_calendar, p_log_level_rec => p_log_level_rec);
2775                         fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcbc_record.fiscal_year_name',
2776                                          fa_cache_pkg.fazcbc_record.fiscal_year_name, p_log_level_rec => p_log_level_rec);
2777                         fa_debug_pkg.add(l_calling_fn, 'l_dpr_in.prorate_jdate',
2778                                          l_dpr_in.prorate_jdate, p_log_level_rec => p_log_level_rec);
2779 
2780                      end if;
2781 
2782                      raise pro_err;
2783                   end if;
2784                end if;
2785 
2786 
2787                if (p_log_level_rec.statement_level) then
2788                   fa_debug_pkg.add(l_calling_fn, 'Before Calling', 'faxcde for hype reserve', p_log_level_rec => p_log_level_rec);
2789                end if;
2790 
2791                -- Skip faxcde call to find hyp rsv if method type is not (FLAT or PROD) and basis is COST
2792                if (((nvl(fa_cache_pkg.fazccmt_record.rate_source_rule, ' ') not in(fa_std_types.FAD_RSR_FLAT,
2793                                                                                    fa_std_types.FAD_RSR_PROD)) and
2794                     (nvl(fa_cache_pkg.fazccmt_record.deprn_basis_rule,' ') = fa_std_types.FAD_DBR_COST))) then
2795 
2796                   -- bug5894464
2797                   l_dpr_in.p_cl_begin := 1;
2798 
2799                   if (fa_amort_pvt.t_period_num(i) = 1) then
2800                      l_dpr_in.y_end := fa_amort_pvt.t_fiscal_year(i) - 1;
2801                      l_dpr_in.p_cl_end := fa_cache_pkg.fazcct_record.number_per_fiscal_year;
2802                   else
2803                      l_dpr_in.y_end := fa_amort_pvt.t_fiscal_year(i);
2804                      l_dpr_in.p_cl_end := fa_amort_pvt.t_period_num(i) - 1;
2805                   end if;
2806                   -- bug5894464
2807 
2808                   --+++++++ Call Depreciation engine for rate adjustment factor +++++++
2809                   if not FA_CDE_PKG.faxcde(l_dpr_in,
2810                                            l_dpr_arr,
2811                                            l_dpr_out,
2812                                            l_running_mode, p_log_level_rec => p_log_level_rec) then
2813                      if (p_log_level_rec.statement_level) then
2814                         fa_debug_pkg.add(l_calling_fn, 'Error calling',
2815                              'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
2816                      end if;
2817 
2818                      raise pro_err;
2819                   end if;
2820                end if; -- (((nvl(fa_cache_pkg.fazccmt_record.rate_source_rule, ' ') not in(fa_std_types.FAD_RSR_FLAT,
2821 
2822                l_period_rec.period_counter := fa_amort_pvt.t_period_counter(i);
2823                l_period_rec.period_num := fa_amort_pvt.t_period_num(i);
2824                l_period_rec.fiscal_year := fa_amort_pvt.t_fiscal_year(i);
2825 
2826                if (p_log_level_rec.statement_level) then
2827                   fa_debug_pkg.add(l_calling_fn, 'Calling', 'FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS', p_log_level_rec => p_log_level_rec);
2828                end if;
2829 
2830                l_asset_deprn_rec.impairment_reserve := --fa_amort_pvt.t_impairment_amount(i) +
2831                                                           l_dpr_out.new_impairment_rsv;
2832 
2833                -- Manipulate eofy_reserve in if following conditioin is satisfied as
2834                -- depreciable basis will not use actual eofy_reserve
2835 
2836                if (p_log_level_rec.statement_level) then
2837                   fa_debug_pkg.add(l_calling_fn, 'fa_amort_pvt.t_impairment_amount(i)', fa_amort_pvt.t_impairment_amount(i));
2838                   fa_debug_pkg.add(l_calling_fn, 'rule_name', fa_cache_pkg.fazcdbr_record.rule_name, p_log_level_rec => p_log_level_rec);
2839                   fa_debug_pkg.add(l_calling_fn, 'deprn_basis_rule', fa_cache_pkg.fazccmt_record.deprn_basis_rule, p_log_level_rec => p_log_level_rec);
2840                   fa_debug_pkg.add(l_calling_fn, 'use_rsv_after_imp_flag', fa_cache_pkg.fazcdrd_record.use_rsv_after_imp_flag, p_log_level_rec => p_log_level_rec);
2841                end if;
2842 
2843                if (i > 1) and (fa_amort_pvt.t_impairment_amount(i-1) <> 0) and
2844                   (fa_cache_pkg.fazcdbr_record.rule_name = 'FLAT RATE EXTENSION') and
2845                   (fa_cache_pkg.fazccmt_record.deprn_basis_rule = fa_std_types.FAD_DBR_NBV) and
2846                   (nvl(fa_cache_pkg.fazcdrd_record.use_rsv_after_imp_flag, 'N') = 'Y') then
2847 
2848                   l_asset_fin_rec_new.eofy_reserve := fa_amort_pvt.t_deprn_reserve(i-1) +
2849                                                       nvl(fa_amort_pvt.t_change_in_eofy_reserve(i), 0);
2850                   fa_amort_pvt.t_eofy_reserve(i-1) := l_asset_fin_rec_new.eofy_reserve;
2851 
2852                end if;
2853 
2854                if (p_log_level_rec.statement_level) then
2855                   fa_debug_pkg.add(l_calling_fn, 'Calling', 'FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS', p_log_level_rec => p_log_level_rec);
2856                end if;
2857 
2858                if (not FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS
2859                                       (p_event_type             => l_db_event_type,
2860                                        p_asset_fin_rec_new      => l_asset_fin_rec_new,
2861                                        p_asset_fin_rec_old      => l_asset_fin_rec_old,
2862                                        p_asset_hdr_rec          => l_asset_hdr_rec,
2863                                        p_asset_type_rec         => l_asset_type_rec,
2864                                        p_asset_deprn_rec        => l_asset_deprn_rec,
2865                                        p_trans_rec              => l_trans_rec,
2866                                        p_trans_rec_adj          => l_trans_rec,
2867                                        p_period_rec             => l_period_rec,
2868                                        p_asset_retire_rec          => l_asset_retire_rec,
2869                                        p_recoverable_cost       => l_recoverable_cost,
2870                                        p_salvage_value          => l_salvage_value,
2871                                        p_current_total_rsv      => l_asset_deprn_rec.deprn_reserve,
2872                                        p_current_rsv            => l_asset_deprn_rec.deprn_reserve -
2873                                                                    nvl(l_asset_deprn_rec.bonus_deprn_reserve, 0) -
2874                                                                    nvl(l_asset_deprn_rec.impairment_reserve, 0),
2875                                        p_current_total_ytd      => l_asset_deprn_rec.ytd_deprn,
2876                                        p_hyp_basis              => l_asset_fin_rec_new.adjusted_cost,
2877                                        p_hyp_total_rsv          => l_dpr_out.new_deprn_rsv,
2878                                        p_hyp_rsv                => l_dpr_out.new_deprn_rsv -
2879                                                                    nvl(l_dpr_out.new_bonus_deprn_rsv, 0) -
2880                                                                    nvl(l_dpr_out.new_impairment_rsv,0),
2881                                        p_eofy_recoverable_cost  => l_eofy_rec_cost,
2882                                        p_eop_recoverable_cost   => l_eop_rec_cost,
2883                                        p_eofy_salvage_value     => l_eofy_sal_val,
2884                                        p_eop_salvage_value      => l_eop_sal_val,
2885                                        p_mrc_sob_type_code      => p_mrc_sob_type_code,
2886                                        p_used_by_adjustment     => 'ADJUSTMENT',
2887                                        px_new_adjusted_cost     => fa_amort_pvt.t_adjusted_cost(i),
2888                                        px_new_raf               => fa_amort_pvt.t_rate_adjustment_factor(i),
2889                                        px_new_formula_factor    => fa_amort_pvt.t_formula_factor(i),
2890 
2891                                        p_log_level_rec     => p_log_level_rec)) then
2892                   if (p_log_level_rec.statement_level) then
2893                      fa_debug_pkg.add(l_calling_fn, 'Error calling',
2894                                       'FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS', p_log_level_rec => p_log_level_rec);
2895                   end if;
2896 
2897                   raise pro_err;
2898                end if;
2899 
2900                if (p_log_level_rec.statement_level) then
2901                   fa_debug_pkg.add(l_calling_fn, 'Returned values from ',
2902                                    'FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS', p_log_level_rec => p_log_level_rec);
2903                   fa_debug_pkg.add(l_calling_fn, 'adjusted_cost',
2904                                    fa_amort_pvt.t_adjusted_cost(i));
2905                   fa_debug_pkg.add(l_calling_fn, 'rate_adjustment_factor',
2906                                    fa_amort_pvt.t_rate_adjustment_factor(i));
2907                   fa_debug_pkg.add(l_calling_fn, 'formula_factor',
2908                                    fa_amort_pvt.t_formula_factor(i));
2909                   fa_debug_pkg.add(l_calling_fn, '====== ', '==============================', p_log_level_rec => p_log_level_rec);
2910                   fa_debug_pkg.add(l_calling_fn, 'Calling', 'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
2911                end if;
2912 
2913                l_adjusted_ind := 0;
2914 
2915                FOR j in (i + 1)..(fa_amort_pvt.t_period_counter.count) LOOP
2916                   l_adjusted_ind := l_adjusted_ind + 1;
2917 
2918 
2919                   if (p_log_level_rec.statement_level) then
2920                      fa_debug_pkg.add(l_calling_fn, 't_period_counter('||to_char(j)||') ', fa_amort_pvt.t_period_counter(j));
2921                      fa_debug_pkg.add(l_calling_fn, 'p_imp_period_rec.period_counter', p_imp_period_rec.period_counter, p_log_level_rec => p_log_level_rec);
2922                   end if;
2923 
2924                   if ((fa_amort_pvt.t_reset_adjusted_cost_flag(j) = 'Y') or
2925                       (j = (fa_amort_pvt.t_period_counter.count))) or
2926                      ( fa_amort_pvt.t_period_counter(j) = p_imp_period_rec.period_counter + 1) then
2927 
2928                      l_fiscal_year := fa_amort_pvt.t_fiscal_year(j-1);
2929                      l_period_num := fa_amort_pvt.t_period_num(j-1);
2930                      l_period_counter := fa_amort_pvt.t_period_counter(j-1);
2931                      EXIT;
2932                   end if;
2933 
2934                END LOOP;
2935 
2936                --
2937                -- Prepare Running Depreciation
2938                --
2939                l_dpr_in.y_begin := fa_amort_pvt.t_fiscal_year(i);
2940                l_dpr_in.p_cl_begin := fa_amort_pvt.t_period_num(i);
2941                l_dpr_in.y_end := l_fiscal_year;
2942                l_dpr_in.p_cl_end := l_period_num;
2943                l_dpr_in.ytd_deprn := fa_amort_pvt.t_ytd_deprn(i);
2944                l_dpr_in.deprn_rsv := fa_amort_pvt.t_deprn_reserve(i);
2945                l_dpr_in.adj_cost := fa_amort_pvt.t_adjusted_cost(i);
2946                l_dpr_in.eofy_reserve := fa_amort_pvt.t_eofy_reserve(i);
2947                l_dpr_in.rate_adj_factor := fa_amort_pvt.t_rate_adjustment_factor(i);
2948                l_dpr_in.formula_factor := fa_amort_pvt.t_formula_factor(i);
2949 
2950                if (l_period_rec.period_num <> 1) then
2951                   l_dpr_in.deprn_rounding_flag := 'ADJ';
2952                end if;
2953 
2954                if (p_log_level_rec.statement_level) then
2955                   fa_debug_pkg.add(l_calling_fn, 'y_begin', l_dpr_in.y_begin, p_log_level_rec => p_log_level_rec);
2956                   fa_debug_pkg.add(l_calling_fn, 'p_cl_begin', l_dpr_in.p_cl_begin, p_log_level_rec => p_log_level_rec);
2957                   fa_debug_pkg.add(l_calling_fn, 'y_end', l_dpr_in.y_end, p_log_level_rec => p_log_level_rec);
2958                   fa_debug_pkg.add(l_calling_fn, 'p_cl_end', l_dpr_in.p_cl_end, p_log_level_rec => p_log_level_rec);
2959                end if;
2960 
2961 
2962                --
2963                -- Calculate periodic depreciation
2964                --
2965 
2966                if not FA_CDE_PKG.faxcde(l_dpr_in,
2967                                         l_dpr_arr,
2968                                         l_dpr_out,
2969                                         l_running_mode,
2970                                         l_bs_ind, p_log_level_rec => p_log_level_rec) then
2971                   if (p_log_level_rec.statement_level) then
2972                      fa_debug_pkg.add(l_calling_fn, 'Error calling',
2973                                       'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
2974                   end if;
2975 
2976                   raise pro_err;
2977                end if;
2978 
2979                if (p_log_level_rec.statement_level) then
2980                   fa_debug_pkg.add(l_calling_fn, 'After Calling', 'faxcde', p_log_level_rec => p_log_level_rec);
2981                   fa_debug_pkg.add(l_calling_fn, 'l_dpr_out.impairment_rsv', l_dpr_out.new_impairment_rsv, p_log_level_rec => p_log_level_rec);
2982                end if;
2983 
2984                if (l_asset_fin_rec_new.adjusted_cost <> 0) or
2985                   (l_dpr_out.new_adj_cost <> 0) then
2986 
2987                   l_asset_fin_rec_new.reval_amortization_basis := l_dpr_out.new_reval_amo_basis;
2988                   l_asset_fin_rec_new.adjusted_cost := l_dpr_out.new_adj_cost;
2989                   l_asset_fin_rec_new.cost := fa_amort_pvt.t_cost(i);
2990                   l_asset_fin_rec_new.salvage_value := fa_amort_pvt.t_salvage_value(i);
2991                   l_asset_fin_rec_new.recoverable_cost := fa_amort_pvt.t_recoverable_cost(i);
2992                   l_asset_fin_rec_new.deprn_method_code := fa_amort_pvt.t_deprn_method_code(i);
2993                   l_asset_fin_rec_new.life_in_months := fa_amort_pvt.t_life_in_months(i);
2994                   l_asset_fin_rec_new.depreciate_flag := fa_amort_pvt.t_depreciate_flag(i);
2995                   l_asset_fin_rec_new.eofy_reserve := fa_amort_pvt.t_eofy_reserve(i);
2996                   l_asset_fin_rec_new.rate_adjustment_factor := fa_amort_pvt.t_rate_adjustment_factor(i);
2997                   l_asset_fin_rec_new.formula_factor := fa_amort_pvt.t_formula_factor(i);
2998 
2999                   l_asset_deprn_rec.deprn_reserve := l_dpr_out.new_deprn_rsv;
3000                   l_asset_deprn_rec.ytd_deprn := l_dpr_out.new_ytd_deprn;
3001                   l_asset_deprn_rec.reval_deprn_reserve := l_dpr_out.new_reval_rsv;
3002                   l_asset_deprn_rec.ltd_production := l_dpr_out.new_ltd_prod;
3003                   l_asset_fin_rec_new.eofy_reserve := l_dpr_out.new_eofy_reserve;
3004                   l_asset_deprn_rec.prior_fy_expense := l_dpr_out.new_prior_fy_exp;
3005                   l_asset_deprn_rec.bonus_deprn_amount := l_dpr_out.bonus_deprn_exp;
3006                   l_asset_deprn_rec.bonus_deprn_reserve := l_dpr_out.new_bonus_deprn_rsv;
3007                   l_asset_deprn_rec.prior_fy_bonus_expense := l_dpr_out.new_prior_fy_bonus_exp;
3008                   l_asset_deprn_rec.impairment_reserve := l_dpr_out.new_impairment_rsv;
3009 
3010                   if (p_log_level_rec.statement_level) then
3011                      fa_debug_pkg.add(l_calling_fn, 'l_asset_deprn_rec.impairment_reserve', l_asset_deprn_rec.impairment_reserve, p_log_level_rec => p_log_level_rec);
3012                   end if;
3013                end if;
3014 
3015                if (p_log_level_rec.statement_level) then
3016                   fa_debug_pkg.add(l_calling_fn, 'Finish copying to ', 'fin_rec_new', p_log_level_rec => p_log_level_rec);
3017                end if;
3018 
3019                l_eop_rec_cost := fa_amort_pvt.t_recoverable_cost(i);
3020                l_eop_sal_val  := fa_amort_pvt.t_salvage_value(i);
3021 
3022                l_eofy_ind := i - fa_amort_pvt.t_period_num(i);
3023 
3024                if (l_eofy_ind > 0) then
3025                   l_eofy_rec_cost := fa_amort_pvt.t_recoverable_cost(l_eofy_ind);
3026                   l_eofy_sal_val  := fa_amort_pvt.t_salvage_value(l_eofy_ind);
3027                end if;
3028 
3029                l_bs_ind := l_bs_ind + l_adjusted_ind;
3030 
3031          end if; --fa_amort_pvt.t_period_counter(i) <=  p_imp_period_rec.period_counter)
3032 
3033       end if; -- (l_skip) and (tbs_transaction_header_id(i) = 0)
3034 
3035    END LOOP; -- i in 1..fa_amort_pvt.t_period_counter.count
3036 
3037    --
3038    -- Preserve rows in FA_BOOKS_SUMMARY_T table for later use
3039    --
3040    if (p_log_level_rec.statement_level) then
3041       fa_debug_pkg.add(l_calling_fn,'Inserting into FA_BOOKS_SUMMARY_T',
3042                        fa_amort_pvt.t_period_counter.count,  p_log_level_rec => p_log_level_rec);
3043    end if;
3044 
3045    FORALL i in 1..fa_amort_pvt.t_period_counter.count
3046          INSERT INTO FA_BOOKS_SUMMARY_T(
3047                    REQUEST_ID
3048                  , IMPAIRMENT_ID
3049                  , ASSET_ID
3050                  , BOOK_TYPE_CODE
3051                  , PERIOD_COUNTER
3052                  , FISCAL_YEAR
3053                  , PERIOD_NUM
3054                  , CALENDAR_PERIOD_OPEN_DATE
3055                  , CALENDAR_PERIOD_CLOSE_DATE
3056                  , RESET_ADJUSTED_COST_FLAG
3057                  , CHANGE_IN_COST
3058 --                 , CHANGE_IN_ADDITIONS_COST
3059 --                 , CHANGE_IN_ADJUSTMENTS_COST
3060 --                 , CHANGE_IN_RETIREMENTS_COST
3061 --                 , CHANGE_IN_GROUP_REC_COST
3062                  , CHANGE_IN_CIP_COST
3063                  , COST
3064 --                 , CIP_COST
3065                  , SALVAGE_TYPE
3066                  , PERCENT_SALVAGE_VALUE
3067                  , SALVAGE_VALUE
3068 --                 , MEMBER_SALVAGE_VALUE
3069                  , RECOVERABLE_COST
3070                  , DEPRN_LIMIT_TYPE
3071                  , ALLOWED_DEPRN_LIMIT
3072                  , ALLOWED_DEPRN_LIMIT_AMOUNT
3073 --                 , MEMBER_DEPRN_LIMIT_AMOUNT
3074                  , ADJUSTED_RECOVERABLE_COST
3075                  , ADJUSTED_COST
3076                  , DEPRECIATE_FLAG
3077 --                 , DISABLED_FLAG
3078                  , DATE_PLACED_IN_SERVICE
3079                  , DEPRN_METHOD_CODE
3080                  , LIFE_IN_MONTHS
3081                  , RATE_ADJUSTMENT_FACTOR
3082                  , ADJUSTED_RATE
3083                  , BONUS_RULE
3084                  , ADJUSTED_CAPACITY
3085                  , PRODUCTION_CAPACITY
3086 --                 , UNIT_OF_MEASURE
3087 --                 , REMAINING_LIFE1
3088 --                 , REMAINING_LIFE2
3089                  , FORMULA_FACTOR
3090 --                 , UNREVALUED_COST
3091                  , REVAL_AMORTIZATION_BASIS
3092 --                 , REVAL_CEILING
3093                  , CEILING_NAME
3094                  , EOFY_ADJ_COST
3095                  , EOFY_FORMULA_FACTOR
3096                  , EOFY_RESERVE
3097                  , EOP_ADJ_COST
3098 --                 , EOP_FORMULA_FACTOR
3099 --                 , SHORT_FISCAL_YEAR_FLAG
3100 --                 , GROUP_ASSET_ID
3101 --                 , SUPER_GROUP_ID
3102 --                 , OVER_DEPRECIATE_OPTION
3103 --                 , FULLY_RSVD_REVALS_COUNTER
3104                  , CAPITALIZED_FLAG
3105 --                 , FULLY_RESERVED_FLAG
3106 --                 , FULLY_RETIRED_FLAG
3107 --                 , LIFE_COMPLETE_FLAG
3108 --                 , TERMINAL_GAIN_LOSS_AMOUNT
3109 --                 , TERMINAL_GAIN_LOSS_FLAG
3110                  , DEPRN_AMOUNT
3111                  , YTD_DEPRN
3112                  , DEPRN_RESERVE
3113                  , BONUS_DEPRN_AMOUNT
3114                  , BONUS_YTD_DEPRN
3115                  , BONUS_DEPRN_RESERVE
3116                  , BONUS_RATE
3117                  , LTD_PRODUCTION
3118                  , YTD_PRODUCTION
3119                  , PRODUCTION
3120 --n                 , REVAL_AMORTIZATION
3121 --n                 , REVAL_DEPRN_EXPENSE
3122                  , REVAL_RESERVE
3123 --n                 , YTD_REVAL_DEPRN_EXPENSE
3124 --n                 , DEPRN_OVERRIDE_FLAG
3125 --n                 , SYSTEM_DEPRN_AMOUNT
3126 --n                 , SYSTEM_BONUS_DEPRN_AMOUNT
3127                  , YTD_PROCEEDS_OF_SALE
3128                  , LTD_PROCEEDS_OF_SALE
3129                  , YTD_COST_OF_REMOVAL
3130                  , LTD_COST_OF_REMOVAL
3131 --                 , DEPRN_ADJUSTMENT_AMOUNT
3132                  , EXPENSE_ADJUSTMENT_AMOUNT
3133                  , UNPLANNED_AMOUNT
3134                  , RESERVE_ADJUSTMENT_AMOUNT
3135                  , CREATION_DATE
3136                  , CREATED_BY
3137                  , LAST_UPDATE_DATE
3138                  , LAST_UPDATED_BY
3139 --                 , LAST_UPDATE_LOGIN
3140                  , CHANGE_IN_EOFY_RESERVE
3141 --                 , SWITCH_CODE
3142 --                 , POLISH_DEPRN_BASIS
3143 --                 , POLISH_ADJ_REC_COST
3144                  , IMPAIRMENT_AMOUNT
3145                  , YTD_IMPAIRMENT
3146                  , IMPAIRMENT_RESERVE
3147          ) VALUES(
3148                    p_request_id
3149                  , p_impairment_id
3150                  , p_asset_id
3151                  , p_book_type_code
3152                  , fa_amort_pvt.t_period_counter(i)
3153                  , fa_amort_pvt.t_fiscal_year(i)
3154                  , fa_amort_pvt.t_period_num(i)
3155                  , fa_amort_pvt.t_calendar_period_open_date(i)
3156                  , fa_amort_pvt.t_calendar_period_close_date(i)
3157                  , fa_amort_pvt.t_reset_adjusted_cost_flag(i)
3158                  , fa_amort_pvt.t_change_in_cost(i)
3159                  , fa_amort_pvt.t_change_in_cip_cost(i)
3160                  , fa_amort_pvt.t_cost(i)
3161                  , fa_amort_pvt.t_salvage_type(i)
3162                  , fa_amort_pvt.t_percent_salvage_value(i)
3163                  , fa_amort_pvt.t_salvage_value(i)
3164                  , fa_amort_pvt.t_recoverable_cost(i)
3165                  , fa_amort_pvt.t_deprn_limit_type(i)
3166                  , fa_amort_pvt.t_allowed_deprn_limit(i)
3167                  , fa_amort_pvt.t_allowed_deprn_limit_amount(i)
3168                  , fa_amort_pvt.t_adjusted_recoverable_cost(i)
3169                  , fa_amort_pvt.t_adjusted_cost(i)
3170                  , fa_amort_pvt.t_depreciate_flag(i)
3171                  , fa_amort_pvt.t_date_placed_in_service(i)
3172                  , fa_amort_pvt.t_deprn_method_code(i)
3173                  , fa_amort_pvt.t_life_in_months(i)
3174                  , fa_amort_pvt.t_rate_adjustment_factor(i)
3175                  , fa_amort_pvt.t_adjusted_rate(i)
3176                  , fa_amort_pvt.t_bonus_rule(i)
3177                  , fa_amort_pvt.t_adjusted_capacity(i)
3178                  , fa_amort_pvt.t_production_capacity(i)
3179                  , fa_amort_pvt.t_formula_factor(i)
3180                  , fa_amort_pvt.t_reval_amortization_basis(i)
3181                  , fa_amort_pvt.t_ceiling_name(i)
3182                  , fa_amort_pvt.t_eofy_adj_cost(i)
3183                  , fa_amort_pvt.t_eofy_formula_factor(i)
3184                  , fa_amort_pvt.t_eofy_reserve(i)
3185                  , fa_amort_pvt.t_eop_adj_cost(i)
3186                  , fa_amort_pvt.t_capitalized_flag(i)
3187                  , fa_amort_pvt.t_deprn_amount(i)
3188                  , fa_amort_pvt.t_ytd_deprn(i)
3189                  , fa_amort_pvt.t_deprn_reserve(i)
3190                  , fa_amort_pvt.t_bonus_deprn_amount(i)
3191                  , fa_amort_pvt.t_bonus_ytd_deprn(i)
3192                  , fa_amort_pvt.t_bonus_deprn_reserve(i)
3193                  , fa_amort_pvt.t_bonus_rate(i)
3194                  , fa_amort_pvt.t_ltd_production(i)
3195                  , fa_amort_pvt.t_ytd_production(i)
3196                  , fa_amort_pvt.t_production(i)
3197                  , fa_amort_pvt.t_reval_reserve(i)
3198                  , fa_amort_pvt.t_ytd_proceeds_of_sale(i)
3199                  , fa_amort_pvt.t_ltd_proceeds_of_sale(i)
3200                  , fa_amort_pvt.t_ytd_cost_of_removal(i)
3201                  , fa_amort_pvt.t_ltd_cost_of_removal(i)
3202                  , fa_amort_pvt.t_expense_adjustment_amount(i)
3203                  , fa_amort_pvt.t_unplanned_amount(i)
3204                  , fa_amort_pvt.t_reserve_adjustment_amount(i)
3205                  , sysdate
3206                  , FND_GLOBAL.USER_ID
3207                  , sysdate
3208                  , FND_GLOBAL.USER_ID
3209                  , fa_amort_pvt.t_change_in_eofy_reserve(i)
3210                  , fa_amort_pvt.t_impairment_amount(i)
3211                  , fa_amort_pvt.t_ytd_impairment(i)
3212                  , fa_amort_pvt.t_impairment_reserve(i)
3213          );
3214 
3215    --********************************************************
3216    -- This can be removed later if x_dpr_out is used directly
3217    --********************************************************
3218    x_dpr_out := l_dpr_out;
3219    x_dpr_in  := l_dpr_in;
3220 
3221    if (p_log_level_rec.statement_level) then
3222       l_dummy_bool := fa_cde_pkg.faprdos(l_dpr_out, p_log_level_rec => p_log_level_rec);
3223       fa_debug_pkg.add(l_calling_fn,'period loop', 'End', p_log_level_rec => p_log_level_rec);
3224    end if;
3225 
3226 
3227    if (p_log_level_rec.statement_level) then
3228       fa_debug_pkg.add(l_calling_fn,'END', 'END', p_log_level_rec => p_log_level_rec);
3229    end if;
3230 
3231    return true;
3232 EXCEPTION
3233    WHEN pro_err THEN
3234 
3235       if (p_log_level_rec.statement_level) then
3236          fa_debug_pkg.add(l_calling_fn,'EXCEPTION', 'pro_err', p_log_level_rec => p_log_level_rec);
3237          fa_debug_pkg.add(l_calling_fn,'sqlerrm', substrb(sqlerrm, 1, 200));
3238       end if;
3239 
3240       return false;
3241 
3242    WHEN OTHERS THEN
3243 
3244       if (p_log_level_rec.statement_level) then
3245          fa_debug_pkg.add(l_calling_fn,'EXCEPTION', 'OTHERS', p_log_level_rec => p_log_level_rec);
3246          fa_debug_pkg.add(l_calling_fn,'sqlerrm', substrb(sqlerrm, 1, 200));
3247       end if;
3248 
3249       return false;
3250 END process_history;
3251 
3252 FUNCTION calculate_catchup(p_request_id        number
3253                          , p_book_type_code    IN VARCHAR2
3254                          , p_worker_id         IN NUMBER
3255                          , p_period_rec        IN FA_API_TYPES.period_rec_type
3256                          , p_imp_period_rec    IN FA_API_TYPES.period_rec_type
3257                          , p_mrc_sob_type_code IN VARCHAR2
3258                          , p_calling_fn        IN VARCHAR2
3259 , p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) RETURN BOOLEAN IS
3260 
3261    l_calling_fn VARCHAR2(30) := 'calculate_catchup';
3262 
3263    CURSOR c_get_assets IS
3264 SELECT
3265   imp.asset_id
3266 , imp.impairment_amount
3267 , imp.rowid
3268 FROM   fa_itf_impairments imp
3269 WHERE  imp.book_type_code = p_book_type_code
3270 AND    imp.request_id = p_request_id
3271 AND    imp.period_counter <= p_period_rec.period_counter
3272 AND    imp.worker_id = p_worker_id;
3273 
3274    CURSOR c_get_bst (c_asset_id number) IS
3275       SELECT PERIOD_COUNTER
3276            , FISCAL_YEAR
3277            , PERIOD_NUM
3278            , CALENDAR_PERIOD_OPEN_DATE
3279            , CALENDAR_PERIOD_CLOSE_DATE
3280            , RESET_ADJUSTED_COST_FLAG
3281            , CHANGE_IN_COST
3282 --           , CHANGE_IN_ADDITIONS_COST
3283 --           , CHANGE_IN_ADJUSTMENTS_COST
3284 --           , CHANGE_IN_RETIREMENTS_COST
3285 --           , CHANGE_IN_GROUP_REC_COST
3286            , CHANGE_IN_CIP_COST
3287            , COST
3288 --           , CIP_COST
3289            , SALVAGE_TYPE
3290            , PERCENT_SALVAGE_VALUE
3291            , SALVAGE_VALUE
3292 --           , MEMBER_SALVAGE_VALUE
3293            , RECOVERABLE_COST
3294            , DEPRN_LIMIT_TYPE
3295            , ALLOWED_DEPRN_LIMIT
3296            , ALLOWED_DEPRN_LIMIT_AMOUNT
3297 --           , MEMBER_DEPRN_LIMIT_AMOUNT
3298            , ADJUSTED_RECOVERABLE_COST
3299            , ADJUSTED_COST
3300            , DEPRECIATE_FLAG
3301 --           , DISABLED_FLAG
3302            , DATE_PLACED_IN_SERVICE
3303            , DEPRN_METHOD_CODE
3304            , LIFE_IN_MONTHS
3305            , RATE_ADJUSTMENT_FACTOR
3306            , ADJUSTED_RATE
3307            , BONUS_RULE
3308            , ADJUSTED_CAPACITY
3309            , PRODUCTION_CAPACITY
3310 --           , UNIT_OF_MEASURE
3311 --           , REMAINING_LIFE1
3312 --           , REMAINING_LIFE2
3313            , FORMULA_FACTOR
3314 --           , UNREVALUED_COST
3315            , REVAL_AMORTIZATION_BASIS
3316 --           , REVAL_CEILING
3317            , CEILING_NAME
3318            , EOFY_ADJ_COST
3319            , EOFY_FORMULA_FACTOR
3320            , EOFY_RESERVE
3321            , EOP_ADJ_COST
3322 --           , EOP_FORMULA_FACTOR
3323 --           , SHORT_FISCAL_YEAR_FLAG
3324 --           , GROUP_ASSET_ID
3325 --           , SUPER_GROUP_ID
3326 --           , OVER_DEPRECIATE_OPTION
3327 --           , FULLY_RSVD_REVALS_COUNTER
3328            , CAPITALIZED_FLAG
3329 --           , FULLY_RESERVED_FLAG
3330 --           , FULLY_RETIRED_FLAG
3331 --           , LIFE_COMPLETE_FLAG
3332 --           , TERMINAL_GAIN_LOSS_AMOUNT
3333 --           , TERMINAL_GAIN_LOSS_FLAG
3334            , DEPRN_AMOUNT
3335            , YTD_DEPRN
3336            , DEPRN_RESERVE
3337            , BONUS_DEPRN_AMOUNT
3338            , BONUS_YTD_DEPRN
3339            , BONUS_DEPRN_RESERVE
3340            , BONUS_RATE
3341            , LTD_PRODUCTION
3342            , YTD_PRODUCTION
3343            , PRODUCTION
3344 --n          , REVAL_AMORTIZATION
3345 --n          , REVAL_DEPRN_EXPENSE
3346            , REVAL_RESERVE
3347 --n          , YTD_REVAL_DEPRN_EXPENSE
3348 --n          , DEPRN_OVERRIDE_FLAG
3349 --n          , SYSTEM_DEPRN_AMOUNT
3350 --n          , SYSTEM_BONUS_DEPRN_AMOUNT
3351            , YTD_PROCEEDS_OF_SALE
3352            , LTD_PROCEEDS_OF_SALE
3353            , YTD_COST_OF_REMOVAL
3354            , LTD_COST_OF_REMOVAL
3355 --           , DEPRN_ADJUSTMENT_AMOUNT
3356            , EXPENSE_ADJUSTMENT_AMOUNT
3357            , UNPLANNED_AMOUNT
3358            , RESERVE_ADJUSTMENT_AMOUNT
3359 --           , CREATION_DATE
3360 --           , CREATED_BY
3361 --           , LAST_UPDATE_DATE
3362 --           , LAST_UPDATED_BY
3363 --           , LAST_UPDATE_LOGIN
3364            , CHANGE_IN_EOFY_RESERVE
3365 --           , SWITCH_CODE
3366 --           , POLISH_DEPRN_BASIS
3367 --           , POLISH_ADJ_REC_COST
3368            , IMPAIRMENT_AMOUNT
3369            , YTD_IMPAIRMENT
3370            , IMPAIRMENT_RESERVE
3371       FROM   fa_books_summary_t
3372       WHERE  asset_id = c_asset_id
3373       AND    book_type_code = p_book_type_code
3374       AND    period_counter >= p_imp_period_rec.period_counter
3375       AND    request_id = p_request_id
3376       ORDER BY period_counter;
3377 
3378 
3379    t_asset_id                    tab_num15_type;
3380    t_impairment_amount           tab_num_type;
3381    t_rowid                       tab_rowid_type;
3382    t_catchup                     tab_num_type;
3383    t_adjusted_cost               tab_num_type;
3384    t_raf                         tab_num_type;
3385    t_formula_factor              tab_num_type;
3386    t_eofy_reserve                tab_num_type;
3387 
3388 
3389    tbs_transaction_header_id     tab_num15_type;
3390    tbs_change_in_sal             tab_num_type;
3391    tbs_change_in_limit           tab_num_type;
3392    tbs_change_in_retirements_cost tab_num_type;
3393 
3394    l_trans_rec                   FA_API_TYPES.trans_rec_type;
3395    l_asset_hdr_rec               FA_API_TYPES.asset_hdr_rec_type;
3396    l_asset_fin_rec_old           FA_API_TYPES.asset_fin_rec_type;
3397    l_asset_fin_rec_new           FA_API_TYPES.asset_fin_rec_type;
3398    l_asset_deprn_rec             FA_API_TYPES.asset_deprn_rec_type;
3399    l_asset_type_rec              FA_API_TYPES.asset_type_rec_type;
3400    l_period_rec                  FA_API_TYPES.period_rec_type;
3401 
3402    l_dpr_in          fa_std_types.dpr_struct;
3403    l_dpr_out         fa_std_types.dpr_out_struct;
3404    l_dpr_arr         fa_std_types.dpr_arr_type;
3405 
3406    loc                           binary_integer;
3407    l_start_pc                    number(15);
3408 
3409    l_running_mode                number := fa_std_types.FA_DPR_NORMAL;
3410 
3411    l_temp_num                    number;
3412 
3413    l_eofy_rec_cost               number := 0;
3414    l_eop_rec_cost                number := 0;
3415    l_eofy_sal_val                number := 0;
3416    l_eop_sal_val                 number := 0;
3417    l_eofy_ind                    binary_integer;
3418    l_bs_ind                      binary_integer;
3419    l_adjusted_ind                binary_integer;
3420 
3421    l_fiscal_year                 number;
3422    l_period_num                  number;
3423    l_period_counter              number;
3424 
3425    -- variables for query balance
3426    l_dpr_row                     FA_STD_TYPES.FA_DEPRN_ROW_STRUCT;
3427    l_qb_running_mode                varchar2(10) := 'STANDARD';
3428    l_status                      boolean;
3429 
3430    l_dummy_bool                  boolean;
3431 
3432    calc_err EXCEPTION;
3433 
3434 BEGIN
3435 
3436    if (p_log_level_rec.statement_level) then
3437       fa_debug_pkg.add(l_calling_fn,'BEGIN', to_char(p_request_id)||':'||to_char(p_worker_id));
3438    end if;
3439 
3440    OPEN c_get_assets;
3441    FETCH c_get_assets BULK COLLECT INTO t_asset_id
3442                                       , t_impairment_amount
3443                                       , t_rowid
3444    ;
3445    CLOSE c_get_assets;
3446 
3447    if (p_log_level_rec.statement_level) then
3448       fa_debug_pkg.add(l_calling_fn,'Records returned from c_get_assets', t_asset_id.count, p_log_level_rec => p_log_level_rec);
3449    end if;
3450 
3451    FOR j in 1..t_asset_id.count LOOP
3452 
3453       OPEN c_get_bst(t_asset_id(j));
3454       FETCH c_get_bst BULK COLLECT INTO fa_amort_pvt.t_period_counter
3455                                       , fa_amort_pvt.t_fiscal_year
3456                                       , fa_amort_pvt.t_period_num
3457                                       , fa_amort_pvt.t_calendar_period_open_date
3458                                       , fa_amort_pvt.t_calendar_period_close_date
3459                                       , fa_amort_pvt.t_reset_adjusted_cost_flag
3460                                       , fa_amort_pvt.t_change_in_cost
3461                                       , fa_amort_pvt.t_change_in_cip_cost
3462                                       , fa_amort_pvt.t_cost
3463                                       , fa_amort_pvt.t_salvage_type
3464                                       , fa_amort_pvt.t_percent_salvage_value
3465                                       , fa_amort_pvt.t_salvage_value
3466                                       , fa_amort_pvt.t_recoverable_cost
3467                                       , fa_amort_pvt.t_deprn_limit_type
3468                                       , fa_amort_pvt.t_allowed_deprn_limit
3469                                       , fa_amort_pvt.t_allowed_deprn_limit_amount
3470                                       , fa_amort_pvt.t_adjusted_recoverable_cost
3471                                       , fa_amort_pvt.t_adjusted_cost
3472                                       , fa_amort_pvt.t_depreciate_flag
3473                                       , fa_amort_pvt.t_date_placed_in_service
3474                                       , fa_amort_pvt.t_deprn_method_code
3475                                       , fa_amort_pvt.t_life_in_months
3476                                       , fa_amort_pvt.t_rate_adjustment_factor
3477                                       , fa_amort_pvt.t_adjusted_rate
3478                                       , fa_amort_pvt.t_bonus_rule
3479                                       , fa_amort_pvt.t_adjusted_capacity
3480                                       , fa_amort_pvt.t_production_capacity
3481                                       , fa_amort_pvt.t_formula_factor
3482                                       , fa_amort_pvt.t_reval_amortization_basis
3483                                       , fa_amort_pvt.t_ceiling_name
3484                                       , fa_amort_pvt.t_eofy_adj_cost
3485                                       , fa_amort_pvt.t_eofy_formula_factor
3486                                       , fa_amort_pvt.t_eofy_reserve
3487                                       , fa_amort_pvt.t_eop_adj_cost
3488                                       , fa_amort_pvt.t_capitalized_flag
3489                                       , fa_amort_pvt.t_deprn_amount
3490                                       , fa_amort_pvt.t_ytd_deprn
3491                                       , fa_amort_pvt.t_deprn_reserve
3492                                       , fa_amort_pvt.t_bonus_deprn_amount
3493                                       , fa_amort_pvt.t_bonus_ytd_deprn
3494                                       , fa_amort_pvt.t_bonus_deprn_reserve
3495                                       , fa_amort_pvt.t_bonus_rate
3496                                       , fa_amort_pvt.t_ltd_production
3497                                       , fa_amort_pvt.t_ytd_production
3498                                       , fa_amort_pvt.t_production
3499                                       , fa_amort_pvt.t_reval_reserve
3500                                       , fa_amort_pvt.t_ytd_proceeds_of_sale
3501                                       , fa_amort_pvt.t_ltd_proceeds_of_sale
3502                                       , fa_amort_pvt.t_ytd_cost_of_removal
3503                                       , fa_amort_pvt.t_ltd_cost_of_removal
3504                                       , fa_amort_pvt.t_expense_adjustment_amount
3505                                       , fa_amort_pvt.t_unplanned_amount
3506                                       , fa_amort_pvt.t_reserve_adjustment_amount
3507                                       , fa_amort_pvt.t_change_in_eofy_reserve
3508                                       , fa_amort_pvt.t_impairment_amount
3509                                       , fa_amort_pvt.t_ytd_impairment
3510                                       , fa_amort_pvt.t_impairment_reserve
3511        ;
3512        CLOSE c_get_bst;
3513 
3514 
3515       if (p_log_level_rec.statement_level) then
3516          fa_debug_pkg.add(l_calling_fn,'Rec returned from c_get_bst', fa_amort_pvt.t_period_counter.count,  p_log_level_rec => p_log_level_rec);
3517       end if;
3518 
3519       --
3520       -- Get reserve entry from fa_adjustments and fa_deprn_summary(b row)
3521       --
3522 
3523       l_asset_hdr_rec.asset_id           := t_asset_id(j);
3524       l_asset_hdr_rec.period_of_addition := null;
3525       l_asset_hdr_rec.book_type_code     := p_book_type_code;
3526       l_asset_hdr_rec.set_of_books_id    := fa_cache_pkg.fazcbc_record.set_of_books_id;
3527 
3528       l_dpr_in.calendar_type := fa_cache_pkg.fazcbc_record.deprn_calendar;
3529       l_dpr_in.book := l_asset_hdr_rec.book_type_code;
3530       l_dpr_in.asset_id := l_asset_hdr_rec.asset_id;
3531 
3532       if (p_log_level_rec.statement_level) then
3533          fa_debug_pkg.add(l_calling_fn,'calling', 'FA_UTIL_PVT.get_asset_fin_rec',  p_log_level_rec => p_log_level_rec);
3534       end if;
3535 
3536       -- Populate fin rec
3537       if not FA_UTIL_PVT.get_asset_fin_rec
3538                  (p_asset_hdr_rec         => l_asset_hdr_rec,
3539                   px_asset_fin_rec        => l_asset_fin_rec_old,
3540                   p_transaction_header_id => NULL,
3541                   p_mrc_sob_type_code     => p_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
3542                raise calc_err;
3543       end if;
3544 
3545       l_asset_fin_rec_old.cost := fa_amort_pvt.t_cost(1);
3546       l_asset_fin_rec_old.recoverable_cost := fa_amort_pvt.t_recoverable_cost(1);
3547       l_asset_fin_rec_old.adjusted_recoverable_cost := fa_amort_pvt.t_adjusted_recoverable_cost(1);
3548       l_asset_fin_rec_old.adjusted_cost := fa_amort_pvt.t_adjusted_cost(1);
3549       l_asset_fin_rec_old.rate_adjustment_factor := fa_amort_pvt.t_rate_adjustment_factor(1);
3550       l_asset_fin_rec_old.formula_factor := fa_amort_pvt.t_formula_factor(1);
3551       l_asset_fin_rec_old.eofy_reserve := fa_amort_pvt.t_eofy_reserve(1);
3552       l_asset_fin_rec_old.reval_amortization_basis:= fa_amort_pvt.t_reval_amortization_basis(1);
3553       l_asset_fin_rec_old.adjusted_capacity := fa_amort_pvt.t_adjusted_capacity(1);
3554       l_asset_fin_rec_new := l_asset_fin_rec_old;
3555 
3556       --
3557       -- Factor impairment amount in
3558       --
3559       fa_amort_pvt.t_impairment_amount(1) := fa_amort_pvt.t_impairment_amount(1) + t_impairment_amount(j);
3560       fa_amort_pvt.t_ytd_impairment(1) := fa_amort_pvt.t_ytd_impairment(1) + t_impairment_amount(j);
3561       fa_amort_pvt.t_impairment_reserve(1) := fa_amort_pvt.t_impairment_reserve(1) + t_impairment_amount(j);
3562 
3563       if (p_log_level_rec.statement_level) then
3564          fa_debug_pkg.add(l_calling_fn,'period loop', 'Begin', p_log_level_rec => p_log_level_rec);
3565       end if;
3566 
3567       --
3568       -- Calculate periodic depreciation amounts
3569       --
3570       l_bs_ind := 2;
3571 
3572       FOR i in 2..fa_amort_pvt.t_period_counter.count LOOP
3573 
3574          if (p_log_level_rec.statement_level) then
3575             fa_debug_pkg.add(l_calling_fn, 't_period_counter', fa_amort_pvt.t_period_counter(i));
3576             fa_debug_pkg.add(l_calling_fn, 'imp period_counter', p_imp_period_rec.period_counter, p_log_level_rec => p_log_level_rec);
3577          end if;
3578 
3579          l_trans_rec.transaction_key := 'IM';
3580 
3581          if (l_bs_ind <= i) then
3582             fa_amort_pvt.t_ytd_impairment(i) := fa_amort_pvt.t_ytd_impairment(i-1) +
3583                                                 fa_amort_pvt.t_impairment_amount(i);
3584             fa_amort_pvt.t_impairment_reserve(i) := fa_amort_pvt.t_impairment_reserve(i-1) +
3585                                                     fa_amort_pvt.t_impairment_amount(i);
3586 
3587             fa_amort_pvt.t_deprn_amount(i)   := nvl(fa_amort_pvt.t_expense_adjustment_amount(i), 0);
3588 
3589             if (i = 1) then
3590                fa_amort_pvt.t_ytd_deprn(i)      := fa_amort_pvt.t_deprn_amount(i);
3591                fa_amort_pvt.t_deprn_reserve(i)  := nvl(fa_amort_pvt.t_deprn_amount(i), 0) +
3592                                                              nvl(fa_amort_pvt.t_reserve_adjustment_amount(i), 0);
3593                fa_amort_pvt.t_eofy_reserve (i)  := nvl(fa_amort_pvt.t_change_in_eofy_reserve(i), 0);
3594             else
3595                fa_amort_pvt.t_deprn_reserve(i)  := fa_amort_pvt.t_deprn_reserve(i-1) +
3596                                                    nvl(fa_amort_pvt.t_deprn_amount(i), 0) +
3597                                                    nvl(fa_amort_pvt.t_reserve_adjustment_amount(i), 0);
3598                if (i > 1) then
3599                   if (fa_amort_pvt.t_period_num(i) = 1) then
3600                      fa_amort_pvt.t_ytd_deprn(i)      := fa_amort_pvt.t_deprn_amount(i);
3601                      fa_amort_pvt.t_eofy_reserve (i)       := fa_amort_pvt.t_deprn_reserve(i - 1) +
3602                                                               nvl(fa_amort_pvt.t_change_in_eofy_reserve(i), 0);
3603                   else
3604                      fa_amort_pvt.t_ytd_deprn(i)      := fa_amort_pvt.t_ytd_deprn(i-1) +
3605                                                          fa_amort_pvt.t_deprn_amount(i);
3606                      fa_amort_pvt.t_eofy_reserve (i)       := fa_amort_pvt.t_eofy_reserve(i - 1) +
3607                                                               nvl(fa_amort_pvt.t_change_in_eofy_reserve(i), 0);
3608                   end if;
3609                else
3610                   --
3611                   -- If user entered reserve exists, code below may need to be modified
3612                   --
3613                   if (fa_amort_pvt.t_period_num(i) = 1) then
3614                      fa_amort_pvt.t_eofy_reserve (i)       := 0;
3615                   else
3616                      fa_amort_pvt.t_eofy_reserve (i)       := 0;
3617                   end if;
3618                end if;
3619 
3620             end if;
3621 
3622          if (p_log_level_rec.statement_level) then
3623             fa_debug_pkg.add(l_calling_fn, 'fa_amort_pvt.t_deprn_reserve', fa_amort_pvt.t_deprn_reserve(i));
3624          end if;
3625 
3626 
3627          --
3628          -- Want to populate fa_amort_pvt tables but not necessary to calculate deprn basis
3629          -- nor periodic depreciations because impairment amount has to be determined first
3630          -- for periods after impairment transactions
3631          --
3632             if (i = 1) then
3633                l_asset_fin_rec_old.cost := 0;
3634                l_asset_fin_rec_old.recoverable_cost := 0;
3635                l_asset_fin_rec_old.adjusted_recoverable_cost := 0;
3636                l_asset_fin_rec_old.adjusted_cost := 0;
3637                l_asset_fin_rec_old.rate_adjustment_factor := 0;
3638                l_asset_fin_rec_old.formula_factor := 0;
3639                l_asset_fin_rec_old.eofy_reserve := 0;
3640                l_asset_fin_rec_old.reval_amortization_basis:= 0;
3641                l_asset_fin_rec_old.adjusted_capacity := 0;
3642                l_asset_fin_rec_new := l_asset_fin_rec_old;
3643             end if;
3644 
3645             l_asset_fin_rec_old := l_asset_fin_rec_new;
3646             l_asset_fin_rec_new.cost := fa_amort_pvt.t_cost(i);
3647             l_asset_fin_rec_new.recoverable_cost := fa_amort_pvt.t_recoverable_cost(i);
3648             l_asset_fin_rec_new.adjusted_recoverable_cost := fa_amort_pvt.t_adjusted_recoverable_cost(i);
3649             l_asset_fin_rec_new.adjusted_cost := fa_amort_pvt.t_adjusted_cost(i);
3650             l_asset_fin_rec_new.eofy_reserve := fa_amort_pvt.t_eofy_reserve(i);
3651 
3652             if (p_log_level_rec.statement_level) then
3653                fa_debug_pkg.add(l_calling_fn, 'Start populating', 'l_dpr_in', p_log_level_rec => p_log_level_rec);
3654             end if;
3655 
3656             l_dpr_in.adj_cost := fa_amort_pvt.t_recoverable_cost(i);
3657             l_dpr_in.salvage_value := fa_amort_pvt.t_salvage_value(i);
3658             l_dpr_in.rec_cost := fa_amort_pvt.t_recoverable_cost(i);
3659             l_dpr_in.adj_rec_cost := fa_amort_pvt.t_adjusted_recoverable_cost(i);
3660             l_dpr_in.reval_amo_basis := fa_amort_pvt.t_reval_amortization_basis(i);
3661             l_dpr_in.adj_rate := fa_amort_pvt.t_adjusted_rate(i);
3662             l_dpr_in.rate_adj_factor := 1;
3663             l_dpr_in.capacity := fa_amort_pvt.t_production_capacity(i);
3664             l_dpr_in.adj_capacity := fa_amort_pvt.t_adjusted_capacity(i);
3665             l_dpr_in.ltd_prod := 0;
3666             l_dpr_in.ytd_deprn := 0;    -- This needs to be 0 for the 1st faxcde call
3667             l_dpr_in.deprn_rsv := 0;
3668             l_dpr_in.reval_rsv := fa_amort_pvt.t_reval_reserve(i);
3669             l_dpr_in.bonus_deprn_exp := fa_amort_pvt.t_bonus_deprn_amount(i);
3670             l_dpr_in.bonus_ytd_deprn := fa_amort_pvt.t_bonus_ytd_deprn(i);
3671             l_dpr_in.bonus_deprn_rsv := fa_amort_pvt.t_bonus_deprn_reserve(i);
3672             l_dpr_in.impairment_exp := fa_amort_pvt.t_impairment_amount(i);
3673             l_dpr_in.ytd_impairment := fa_amort_pvt.t_ytd_impairment(i);
3674             l_dpr_in.impairment_rsv := fa_amort_pvt.t_impairment_reserve(i);
3675             l_dpr_in.ceil_name := fa_amort_pvt.t_ceiling_name(i);
3676             l_dpr_in.bonus_rule := fa_amort_pvt.t_bonus_rule(i);
3677             l_dpr_in.method_code := fa_amort_pvt.t_deprn_method_code(i);
3678             l_dpr_in.life := fa_amort_pvt.t_life_in_months(i);
3679             l_dpr_in.jdate_in_service :=
3680                       to_number(to_char(fa_amort_pvt.t_date_placed_in_service(i), 'J'));
3681             l_dpr_in.deprn_start_jdate := to_number(to_char(l_asset_fin_rec_old.deprn_start_date, 'J'));
3682             l_dpr_in.prorate_jdate := to_number(to_char(l_asset_fin_rec_old.prorate_date, 'J'));
3683 
3684             if (p_log_level_rec.statement_level) then
3685                fa_debug_pkg.add(l_calling_fn, 'Before Calling', 'fa_cache_pkg.fazccmt', p_log_level_rec => p_log_level_rec);
3686             end if;
3687 
3688             if (not fa_cache_pkg.fazccmt(
3689                        fa_amort_pvt.t_deprn_method_code(i),
3690                        fa_amort_pvt.t_life_in_months(i),
3691                        p_log_level_rec)) then
3692                if (p_log_level_rec.statement_level) then
3693                   fa_debug_pkg.add(l_calling_fn, 'Error calling', 'fa_cache_pkg.fazccmt', p_log_level_rec => p_log_level_rec);
3694                end if;
3695 
3696                raise calc_err;
3697             end if;
3698 
3699                   if (p_log_level_rec.statement_level) then
3700                      fa_debug_pkg.add(l_calling_fn, 'Before Calling', 'fa_cache_pkg.fazccp', p_log_level_rec => p_log_level_rec);
3701                   end if;
3702 
3703                   -- bug5894464: removed if (i = 1) condition
3704                   if not fa_cache_pkg.fazccp(fa_cache_pkg.fazcbc_record.prorate_calendar,
3705                                              fa_cache_pkg.fazcbc_record.fiscal_year_name,
3706                                              l_dpr_in.prorate_jdate,
3707                                              g_temp_number,
3708                                              l_dpr_in.y_begin,
3709                                              g_temp_integer, p_log_level_rec => p_log_level_rec) then
3710                      if (p_log_level_rec.statement_level) then
3711                         fa_debug_pkg.add(l_calling_fn, 'Error calling',
3712                                          'fa_cache_pkg.fazccp', p_log_level_rec => p_log_level_rec);
3713                         fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcbc_record.prorate_calendar',
3714                                          fa_cache_pkg.fazcbc_record.prorate_calendar, p_log_level_rec => p_log_level_rec);
3715                         fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcbc_record.fiscal_year_name',
3716                                          fa_cache_pkg.fazcbc_record.fiscal_year_name, p_log_level_rec => p_log_level_rec);
3717                         fa_debug_pkg.add(l_calling_fn, 'l_dpr_in.prorate_jdate',
3718                                          l_dpr_in.prorate_jdate, p_log_level_rec => p_log_level_rec);
3719 
3720                      end if;
3721 
3722                      raise calc_err;
3723                   end if;
3724                   -- bug5894464
3725 
3726 
3727                if (p_log_level_rec.statement_level) then
3728                   fa_debug_pkg.add(l_calling_fn, 'Before Calling', 'faxcde for hype reserve', p_log_level_rec => p_log_level_rec);
3729                end if;
3730 
3731                -- Skip faxcde call to find hyp rsv if method type is not (FLAT or PROD) and basis is COST
3732                if (((nvl(fa_cache_pkg.fazccmt_record.rate_source_rule, ' ') not in(fa_std_types.FAD_RSR_FLAT,
3733                                                                                    fa_std_types.FAD_RSR_PROD)) and
3734                     (nvl(fa_cache_pkg.fazccmt_record.deprn_basis_rule,' ') = fa_std_types.FAD_DBR_COST))) then
3735 
3736                   -- bug5894464
3737                   l_dpr_in.p_cl_begin := 1;
3738 
3739                   if (fa_amort_pvt.t_period_num(i) = 1) then
3740                      l_dpr_in.y_end := fa_amort_pvt.t_fiscal_year(i) - 1;
3741                      l_dpr_in.p_cl_end := fa_cache_pkg.fazcct_record.number_per_fiscal_year;
3742                   else
3743                      l_dpr_in.y_end := fa_amort_pvt.t_fiscal_year(i);
3744                      l_dpr_in.p_cl_end := fa_amort_pvt.t_period_num(i) - 1;
3745                   end if;
3746                   -- bug5894464
3747 
3748                   --+++++++ Call Depreciation engine for rate adjustment factor +++++++
3749                   if not FA_CDE_PKG.faxcde(l_dpr_in,
3750                                            l_dpr_arr,
3751                                            l_dpr_out,
3752                                            l_running_mode, p_log_level_rec => p_log_level_rec) then
3753                      if (p_log_level_rec.statement_level) then
3754                         fa_debug_pkg.add(l_calling_fn, 'Error calling',
3755                              'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
3756                      end if;
3757 
3758                      raise calc_err;
3759                   end if;
3760                end if; -- (((nvl(fa_cache_pkg.fazccmt_record.rate_source_rule, ' ') not in(fa_std_types.FAD_RSR_FLAT,
3761 
3762                if (p_log_level_rec.statement_level) then
3763                   fa_debug_pkg.add(l_calling_fn, 'setting', 'deprn_rec for basis rule', p_log_level_rec => p_log_level_rec);
3764                end if;
3765 
3766                l_asset_deprn_rec.set_of_books_id          := l_asset_hdr_rec.set_of_books_id;
3767                l_asset_deprn_rec.deprn_amount             := fa_amort_pvt.t_deprn_amount(i);
3768                l_asset_deprn_rec.ytd_deprn                := fa_amort_pvt.t_ytd_deprn(i);
3769 
3770                l_asset_deprn_rec.deprn_reserve            := fa_amort_pvt.t_deprn_reserve(i);
3771 
3772                l_asset_deprn_rec.prior_fy_expense         := 0;  -- setting 0 for now.  not sure when this is required
3773                l_asset_deprn_rec.bonus_deprn_amount       := fa_amort_pvt.t_bonus_deprn_amount(i);
3774                l_asset_deprn_rec.bonus_ytd_deprn          := fa_amort_pvt.t_bonus_ytd_deprn(i);
3775                l_asset_deprn_rec.bonus_deprn_reserve      := fa_amort_pvt.t_bonus_deprn_reserve(i);
3776                l_asset_deprn_rec.prior_fy_bonus_expense   := 0; -- setting 0 for now.  not sure when this is required
3777                l_asset_deprn_rec.reval_amortization       := 0; -- setting 0 for now.  not sure when this is required
3778                l_asset_deprn_rec.reval_amortization_basis := fa_amort_pvt.t_reval_amortization_basis(i);
3779                l_asset_deprn_rec.reval_deprn_expense      := 0; -- setting 0 for now.  not sure when this is required
3780                l_asset_deprn_rec.reval_ytd_deprn          := 0; -- setting 0 for now.  not sure when this is required
3781                l_asset_deprn_rec.reval_deprn_reserve      := fa_amort_pvt.t_reval_reserve(i);
3782                l_asset_deprn_rec.production               := fa_amort_pvt.t_production(i);
3783                l_asset_deprn_rec.ytd_production           := fa_amort_pvt.t_ytd_production(i);
3784                l_asset_deprn_rec.ltd_production           := fa_amort_pvt.t_ltd_production(i);
3785                l_asset_deprn_rec.impairment_amount        := fa_amort_pvt.t_impairment_amount(i);
3786                l_asset_deprn_rec.ytd_impairment           := fa_amort_pvt.t_ytd_impairment(i);
3787 
3788                l_asset_deprn_rec.impairment_reserve           := fa_amort_pvt.t_impairment_reserve(i);
3789 
3790                if (nvl(fa_cache_pkg.fazcdrd_record.use_rsv_after_imp_flag, 'Y')  = 'Y') and
3791                   (nvl(fa_cache_pkg.fazccmt_record.rate_source_rule, ' ') = fa_std_types.FAD_RSR_FLAT) and
3792                   (nvl(fa_cache_pkg.fazccmt_record.deprn_basis_rule,' ') = fa_std_types.FAD_DBR_COST) then
3793                   l_asset_deprn_rec.impairment_reserve := fa_amort_pvt.t_impairment_reserve(i) + fa_amort_pvt.t_deprn_reserve(1);
3794                end if;
3795 
3796 
3797 
3798                l_period_rec.period_counter := fa_amort_pvt.t_period_counter(i);
3799                l_period_rec.period_num := fa_amort_pvt.t_period_num(i);
3800                l_period_rec.fiscal_year := fa_amort_pvt.t_fiscal_year(i);
3801 
3802                if (p_log_level_rec.statement_level) then
3803                   fa_debug_pkg.add(l_calling_fn, 'Calling', 'FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS', p_log_level_rec => p_log_level_rec);
3804                end if;
3805 
3806                if (p_log_level_rec.statement_level) then
3807                   fa_debug_pkg.add(l_calling_fn, 'fa_amort_pvt.t_impairment_amount(i)', fa_amort_pvt.t_impairment_amount(i));
3808                   fa_debug_pkg.add(l_calling_fn, 'rule_name', fa_cache_pkg.fazcdbr_record.rule_name, p_log_level_rec => p_log_level_rec);
3809                   fa_debug_pkg.add(l_calling_fn, 'deprn_basis_rule', fa_cache_pkg.fazccmt_record.deprn_basis_rule, p_log_level_rec => p_log_level_rec);
3810                   fa_debug_pkg.add(l_calling_fn, 'use_rsv_after_imp_flag', fa_cache_pkg.fazcdrd_record.use_rsv_after_imp_flag, p_log_level_rec => p_log_level_rec);
3811                end if;
3812 
3813                if (i > 1) and (fa_amort_pvt.t_impairment_amount(i-1) <> 0) and
3814                   (fa_cache_pkg.fazcdbr_record.rule_name = 'FLAT RATE EXTENSION') and
3815                   (fa_cache_pkg.fazccmt_record.deprn_basis_rule = fa_std_types.FAD_DBR_NBV) and
3816                   (nvl(fa_cache_pkg.fazcdrd_record.use_rsv_after_imp_flag, 'N') = 'Y') then
3817 
3818                   l_asset_fin_rec_new.eofy_reserve := fa_amort_pvt.t_deprn_reserve(i-1) +
3819                                                       nvl(fa_amort_pvt.t_change_in_eofy_reserve(i), 0);
3820                   fa_amort_pvt.t_eofy_reserve(i-1) := l_asset_fin_rec_new.eofy_reserve;
3821 
3822                end if;
3823 
3824 
3825 
3826                if (not FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS
3827                                       (p_event_type             => 'AMORT_ADJ',
3828                                        p_asset_fin_rec_new      => l_asset_fin_rec_new,
3829                                        p_asset_fin_rec_old      => l_asset_fin_rec_old,
3830                                        p_asset_hdr_rec          => l_asset_hdr_rec,
3831                                        p_asset_type_rec         => l_asset_type_rec,
3832                                        p_asset_deprn_rec        => l_asset_deprn_rec,
3833                                        p_trans_rec              => l_trans_rec,
3834                                        p_trans_rec_adj          => l_trans_rec,
3835                                        p_period_rec             => l_period_rec,
3836                                        p_current_total_rsv      => l_asset_deprn_rec.deprn_reserve,
3837                                        p_current_rsv            => l_asset_deprn_rec.deprn_reserve -
3838                                                                    nvl(l_asset_deprn_rec.bonus_deprn_reserve, 0) -
3839                                                                    nvl(l_asset_deprn_rec.impairment_reserve, 0),
3840                                        p_current_total_ytd      => l_asset_deprn_rec.ytd_deprn,
3841                                        p_hyp_basis              => l_asset_fin_rec_new.adjusted_cost,
3842                                        p_hyp_total_rsv          => l_dpr_out.new_deprn_rsv,
3843                                        p_hyp_rsv                => l_dpr_out.new_deprn_rsv -
3844                                                                    nvl(l_dpr_out.new_bonus_deprn_rsv, 0) -
3845                                                                    nvl(l_dpr_out.new_impairment_rsv,0),
3846                                        p_eofy_recoverable_cost  => l_eofy_rec_cost,
3847                                        p_eop_recoverable_cost   => l_eop_rec_cost,
3848                                        p_eofy_salvage_value     => l_eofy_sal_val,
3849                                        p_eop_salvage_value      => l_eop_sal_val,
3850                                        p_mrc_sob_type_code      => p_mrc_sob_type_code,
3851                                        p_used_by_adjustment     => 'ADJUSTMENT',
3852                                        px_new_adjusted_cost     => fa_amort_pvt.t_adjusted_cost(i),
3853                                        px_new_raf               => fa_amort_pvt.t_rate_adjustment_factor(i),
3854                                        px_new_formula_factor    => fa_amort_pvt.t_formula_factor(i),
3855                                        p_log_level_rec     => p_log_level_rec)) then
3856                   if (p_log_level_rec.statement_level) then
3857                      fa_debug_pkg.add(l_calling_fn, 'Error calling',
3858                                       'FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS', p_log_level_rec => p_log_level_rec);
3859                   end if;
3860 
3861                   raise calc_err;
3862                end if;
3863 
3864                if (p_log_level_rec.statement_level) then
3865                   fa_debug_pkg.add(l_calling_fn, 'Returned values from ',
3866                                    'FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS', p_log_level_rec => p_log_level_rec);
3867                   fa_debug_pkg.add(l_calling_fn, 'adjusted_cost',
3868                                    fa_amort_pvt.t_adjusted_cost(i));
3869                   fa_debug_pkg.add(l_calling_fn, 'rate_adjustment_factor',
3870                                    fa_amort_pvt.t_rate_adjustment_factor(i));
3871                   fa_debug_pkg.add(l_calling_fn, 'formula_factor',
3872                                    fa_amort_pvt.t_formula_factor(i));
3873                   fa_debug_pkg.add(l_calling_fn, '====== ', '==============================', p_log_level_rec => p_log_level_rec);
3874                   fa_debug_pkg.add(l_calling_fn, 'Calling', 'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
3875                end if;
3876 
3877                l_adjusted_ind := 0;
3878 
3879 
3880                if (fa_amort_pvt.t_period_counter(i) <> p_period_rec.period_counter) then
3881 
3882 
3883 
3884 
3885                FOR j in (i + 1)..(fa_amort_pvt.t_period_counter.count) LOOP
3886                   l_adjusted_ind := l_adjusted_ind + 1;
3887 
3888                   if (p_log_level_rec.statement_level) then
3889                      fa_debug_pkg.add(l_calling_fn, 't_reset_adjusted_cost_flag('||to_char(j)||')',
3890                                                     fa_amort_pvt.t_reset_adjusted_cost_flag(j));
3891                      fa_debug_pkg.add(l_calling_fn, 't_period_counter('||to_char(j)||')',
3892                                                     fa_amort_pvt.t_period_counter(j));
3893                      fa_debug_pkg.add(l_calling_fn, 'p_imp_period_rec.period_counter', p_imp_period_rec.period_counter, p_log_level_rec => p_log_level_rec);
3894                   end if;
3895 
3896                   if ((fa_amort_pvt.t_reset_adjusted_cost_flag(j) = 'Y') or
3897                       (j = (fa_amort_pvt.t_period_counter.count))) then
3898 
3899                      l_fiscal_year := fa_amort_pvt.t_fiscal_year(j-1);
3900                      l_period_num := fa_amort_pvt.t_period_num(j-1);
3901                      l_period_counter := fa_amort_pvt.t_period_counter(j-1);
3902                      EXIT;
3903                   end if;
3904 
3905                END LOOP;
3906 
3907                --
3908                -- Prepare Running Depreciation
3909                --
3910                l_dpr_in.y_begin := fa_amort_pvt.t_fiscal_year(i);
3911                l_dpr_in.p_cl_begin := fa_amort_pvt.t_period_num(i);
3912                l_dpr_in.y_end := l_fiscal_year;
3913                l_dpr_in.p_cl_end := l_period_num;
3914                l_dpr_in.ytd_deprn := fa_amort_pvt.t_ytd_deprn(i);
3915                l_dpr_in.deprn_rsv := fa_amort_pvt.t_deprn_reserve(i);
3916                l_dpr_in.adj_cost := fa_amort_pvt.t_adjusted_cost(i);
3917                l_dpr_in.eofy_reserve := fa_amort_pvt.t_eofy_reserve(i);
3918                l_dpr_in.rate_adj_factor := fa_amort_pvt.t_rate_adjustment_factor(i);
3919                l_dpr_in.formula_factor := fa_amort_pvt.t_formula_factor(i);
3920 
3921 
3922                --
3923                -- Calculate periodic depreciation
3924                --
3925                if not FA_CDE_PKG.faxcde(l_dpr_in,
3926                                         l_dpr_arr,
3927                                         l_dpr_out,
3928                                         l_running_mode,
3929                                         l_bs_ind, p_log_level_rec => p_log_level_rec) then
3930                   if (p_log_level_rec.statement_level) then
3931                      fa_debug_pkg.add(l_calling_fn, 'Error calling', 'FA_CDE_PKG.faxcde', p_log_level_rec => p_log_level_rec);
3932                   end if;
3933 
3934                   raise calc_err;
3935                end if;
3936 
3937                if (p_log_level_rec.statement_level) then
3938                   fa_debug_pkg.add(l_calling_fn, 'After Calling', 'faxcde', p_log_level_rec => p_log_level_rec);
3939                end if;
3940 
3941                if (l_asset_fin_rec_new.adjusted_cost <> 0) or
3942                   (l_dpr_out.new_adj_cost <> 0) then
3943 
3944                   l_asset_fin_rec_new.reval_amortization_basis := l_dpr_out.new_reval_amo_basis;
3945                   l_asset_fin_rec_new.adjusted_cost := l_dpr_out.new_adj_cost;
3946                   l_asset_fin_rec_new.cost := fa_amort_pvt.t_cost(i);
3947                   l_asset_fin_rec_new.salvage_value := fa_amort_pvt.t_salvage_value(i);
3948                   l_asset_fin_rec_new.recoverable_cost := fa_amort_pvt.t_recoverable_cost(i);
3949                   l_asset_fin_rec_new.deprn_method_code := fa_amort_pvt.t_deprn_method_code(i);
3950                   l_asset_fin_rec_new.life_in_months := fa_amort_pvt.t_life_in_months(i);
3951                   l_asset_fin_rec_new.depreciate_flag := fa_amort_pvt.t_depreciate_flag(i);
3952                   l_asset_fin_rec_new.eofy_reserve := fa_amort_pvt.t_eofy_reserve(i);
3953                   l_asset_fin_rec_new.rate_adjustment_factor := fa_amort_pvt.t_rate_adjustment_factor(i);
3954                   l_asset_fin_rec_new.formula_factor := fa_amort_pvt.t_formula_factor(i);
3955 
3956                end if;
3957 
3958                if (p_log_level_rec.statement_level) then
3959                   fa_debug_pkg.add(l_calling_fn, 'Finish copying to ', 'fin_rec_new', p_log_level_rec => p_log_level_rec);
3960                end if;
3961 
3962                l_eop_rec_cost := fa_amort_pvt.t_recoverable_cost(i);
3963                l_eop_sal_val  := fa_amort_pvt.t_salvage_value(i);
3964 
3965                l_eofy_ind := i - fa_amort_pvt.t_period_num(i);
3966 
3967                if (l_eofy_ind > 0) then
3968                   l_eofy_rec_cost := fa_amort_pvt.t_recoverable_cost(l_eofy_ind);
3969                   l_eofy_sal_val  := fa_amort_pvt.t_salvage_value(l_eofy_ind);
3970                end if;
3971 
3972                l_bs_ind := l_bs_ind + l_adjusted_ind;
3973 
3974             else
3975                -- This is current period so just add up expense and reserve entries
3976                -- from this period to deprn reserve.
3977 
3978                fa_amort_pvt.t_deprn_amount(i)  := fa_amort_pvt.t_expense_adjustment_amount(i);
3979 
3980                if fa_amort_pvt.t_period_num(i) = 1 then
3981                   fa_amort_pvt.t_ytd_deprn(i) := fa_amort_pvt.t_deprn_amount(i);
3982                else
3983                   fa_amort_pvt.t_ytd_deprn(i) := fa_amort_pvt.t_ytd_deprn(i-1) +
3984                                                  fa_amort_pvt.t_deprn_amount(i);
3985 
3986                end if;
3987 
3988                fa_amort_pvt.t_deprn_reserve(i) := fa_amort_pvt.t_deprn_reserve(i-1) +
3989                                                fa_amort_pvt.t_deprn_amount(i) +
3990                                                fa_amort_pvt.t_reserve_adjustment_amount(i);
3991 
3992             end if; -- (fa_amort_pvt.t_period_counter <> p_period_rec.period_counter)
3993 
3994          end if; -- l_bs_ind <= i then
3995 
3996       END LOOP; -- i in 2..fa_amort_pvt.t_period_counter.count
3997 
3998       --
3999       -- Find current reserve
4000       --
4001       l_dpr_row := null;
4002       l_dpr_row.asset_id             := t_asset_id(j);
4003       l_dpr_row.book                 := p_book_type_code;
4004       l_dpr_row.period_ctr           := p_period_rec.period_counter;
4005       l_dpr_row.dist_id              := 0;
4006       l_dpr_row.mrc_sob_type_code    := p_mrc_sob_type_code;
4007 
4008       if (p_log_level_rec.statement_level) then
4009          fa_debug_pkg.add(l_calling_fn, 'Calling', 'query_balances_int', p_log_level_rec => p_log_level_rec);
4010       end if;
4011 
4012       fa_query_balances_pkg.query_balances_int(
4013                                    X_DPR_ROW               => l_dpr_row,
4014                                    X_RUN_MODE              => l_qb_running_mode,
4015                                    X_DEBUG                 => FALSE,
4016                                    X_SUCCESS               => l_status,
4017                                    X_CALLING_FN            => l_calling_fn,
4018                                    X_TRANSACTION_HEADER_ID => -1, p_log_level_rec => p_log_level_rec);
4019 
4020       if (NOT l_status) then
4021 
4022                if (p_log_level_rec.statement_level) then
4023                   fa_debug_pkg.add(l_calling_fn, 'ERROR',
4024                                    'Calling fa_query_balances_pkg.query_balances_int', p_log_level_rec => p_log_level_rec);
4025                end if;
4026 
4027                raise calc_err;
4028       end if;
4029 
4030 
4031       --
4032       -- Find catch up by current reserve - new reserve
4033       --
4034       t_catchup(j) := fa_amort_pvt.t_deprn_reserve(fa_amort_pvt.t_period_counter.count) - l_dpr_row.deprn_rsv;
4035 
4036       --
4037       -- Store adjusted_cost, rate_adjustment_factor, and formula_factor for later use
4038       --
4039       t_adjusted_cost(j) := fa_amort_pvt.t_adjusted_cost(fa_amort_pvt.t_period_counter.count);
4040       t_raf(j) := fa_amort_pvt.t_rate_adjustment_factor(fa_amort_pvt.t_period_counter.count);
4041       t_formula_factor(j) := fa_amort_pvt.t_formula_factor(fa_amort_pvt.t_period_counter.count);
4042       t_eofy_reserve(j) := fa_amort_pvt.t_eofy_reserve(fa_amort_pvt.t_period_counter.count);
4043       if (p_log_level_rec.statement_level) then
4044          fa_debug_pkg.add(l_calling_fn, 'current reserve', l_dpr_row.deprn_rsv, p_log_level_rec => p_log_level_rec);
4045          fa_debug_pkg.add(l_calling_fn, 'catchup', t_catchup(j));
4046       end if;
4047 
4048    END LOOP; -- j in 1..t_asset_id.count
4049 
4050    --
4051    -- Store catchup amount
4052    --
4053    FORALL k in 1..t_asset_id.count
4054       UPDATE FA_ITF_IMPAIRMENTS
4055       SET    DEPRN_ADJUSTMENT_AMOUNT = t_catchup(k)
4056            , ADJUSTED_COST           = t_adjusted_cost(k)
4057            , RATE_ADJUSTMENT_FACTOR  = t_raf(k)
4058            , FORMULA_FACTOR          = t_formula_factor(k)
4059            , EOFY_RESERVE            = t_eofy_reserve(k)
4060       WHERE  ROWID                   = t_rowid(k);
4061 
4062    if (p_log_level_rec.statement_level) then
4063       l_dummy_bool := fa_cde_pkg.faprdos(l_dpr_out, p_log_level_rec => p_log_level_rec);
4064       fa_debug_pkg.add(l_calling_fn,'period loop', 'End', p_log_level_rec => p_log_level_rec);
4065    end if;
4066 
4067 
4068    if (p_log_level_rec.statement_level) then
4069       fa_debug_pkg.add(l_calling_fn,'END', 'END', p_log_level_rec => p_log_level_rec);
4070    end if;
4071 
4072    return true;
4073 EXCEPTION
4074    WHEN calc_err THEN
4075 
4076       if (p_log_level_rec.statement_level) then
4077          fa_debug_pkg.add(l_calling_fn,'EXCEPTION', 'pro_err', p_log_level_rec => p_log_level_rec);
4078          fa_debug_pkg.add(l_calling_fn,'sqlerrm', substrb(sqlerrm, 1, 200));
4079       end if;
4080 
4081       return false;
4082 
4083    WHEN OTHERS THEN
4084 
4085       if (p_log_level_rec.statement_level) then
4086          fa_debug_pkg.add(l_calling_fn,'EXCEPTION', 'OTHERS', p_log_level_rec => p_log_level_rec);
4087          fa_debug_pkg.add(l_calling_fn,'sqlerrm', substrb(sqlerrm, 1, 200));
4088       end if;
4089 
4090       return false;
4091 END calculate_catchup;
4092 
4093 
4094 END FA_IMPAIRMENT_PREV_PVT;