DBA Data[Home] [Help]

PACKAGE BODY: APPS.IGI_IAC_DEPRN_PKG

Source


1 PACKAGE BODY IGI_IAC_DEPRN_PKG AS
2 -- $Header: igiiaprb.pls 120.20.12000000.2 2007/10/16 14:21:40 sharoy ship $
3 
4 --===========================FND_LOG.START=====================================
5 
6 g_state_level NUMBER;
7 g_proc_level  NUMBER;
8 g_event_level NUMBER;
9 g_excep_level NUMBER;
10 g_error_level NUMBER;
11 g_unexp_level NUMBER;
12 g_path        VARCHAR2(100);
13 
14 --===========================FND_LOG.END=======================================
15 
16 /*=========================================================================+
17  | Function Name:                                                          |
18  |    Synchronize_Calendars                                                |
19  |                                                                         |
20  | Description:                                                            |
21  |    This function synchronizes calendars used in IAC with the 	   |
22  |    corresponding FA calendar.                			   |
23  |                                                                         |
24  +=========================================================================*/
25     FUNCTION Synchronize_Calendars(p_book_type_code IN VARCHAR2) RETURN BOOLEAN IS
26 
27         /* Cursor for getting distinct calendar price index combinations */
28         CURSOR c_get_cal_price_indexes IS
29             SELECT DISTINCT cal_price_index_link_id
30             FROM igi_iac_category_books
31             WHERE book_type_code = p_book_type_code;
32 
33         /* Cursor for getting the last calendar period from Oracle Assets */
34         CURSOR c_fa_max_date(p_cal_price_index_link_id igi_iac_cal_price_indexes.cal_price_index_link_id%TYPE) IS
35             SELECT max(end_date)
36             FROM fa_calendar_periods
37             WHERE calendar_type = (
38                 SELECT calendar_type
39                 FROM igi_iac_cal_price_indexes
40                 WHERE cal_price_index_link_id = p_cal_price_index_link_id );
41 
42         /* Cursor for getting the last period from Inflation Accounting */
43         CURSOR c_iac_max_date(p_cal_price_index_link_id igi_iac_cal_price_indexes.cal_price_index_link_id%TYPE) IS
44             SELECT max(date_to)
45             FROM igi_iac_cal_idx_values
46             WHERE cal_price_index_link_id = p_cal_price_index_link_id;
47 
48         /* Cursor for getting the Price Index value defined for the last period defined in IAC */
49         CURSOR c_current_price_index(p_max_date_to date ,
50                             p_cal_price_index_link_id igi_iac_cal_price_indexes.cal_price_index_link_id%TYPE) IS
51             SELECT current_price_index_value
52             FROM igi_iac_cal_idx_values
53             WHERE trunc(date_to) = trunc(p_max_date_to)
54             AND cal_price_index_link_id = p_cal_price_index_link_id ;
55 
56         /* Cursor to get all periods from FA which are greater than the last period defined in IAC */
57         CURSOR c_get_fa_periods(p_max_date_to date ,
58                     p_cal_price_index_link_id igi_iac_cal_price_indexes.cal_price_index_link_id%TYPE) IS
59             SELECT start_date, end_date
60             FROM fa_calendar_periods
61             WHERE trunc(end_date) > trunc(p_max_date_to)
62             AND calendar_type = (
63                     SELECT calendar_type
64                     FROM igi_iac_cal_price_indexes
65                     WHERE cal_price_index_link_id = p_cal_price_index_link_id );
66 
67         l_fa_max_date	date;
68         l_iac_max_date	date;
69         l_rowid		varchar2(25);
70         l_curr_price_idx_value igi_iac_cal_idx_values.current_price_index_value%TYPE;
71         l_path 	 	VARCHAR2(100);
72     BEGIN
73         l_path := g_path||'Synchronize_Calendars';
74         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,' Start of Synchronizing IAC Calendars');
75         FOR l_get_cal_price_indexes IN c_get_cal_price_indexes LOOP
76 
77             OPEN c_fa_max_date(l_get_cal_price_indexes.cal_price_index_link_id);
78             FETCH c_fa_max_date INTO l_fa_max_date;
79             CLOSE c_fa_max_date;
80 
81             OPEN c_iac_max_date(l_get_cal_price_indexes.cal_price_index_link_id);
82             FETCH c_iac_max_date INTO l_iac_max_date;
83             CLOSE c_iac_max_date;
84 
85             IF trunc(l_iac_max_date) < trunc(l_fa_max_date) THEN
86                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Synchronizing for cal_price_index_link_id :'||to_char(l_get_cal_price_indexes.cal_price_index_link_id));
87                 /* Commented for bug 3197000 vgadde 19_dec-2003 Start(1)
88                 OPEN c_current_price_index(l_iac_max_date ,
89             	    			l_get_cal_price_indexes.cal_price_index_link_id);
90                 FETCH c_current_price_index INTO l_curr_price_idx_value;
91                 CLOSE c_current_price_index;
92                 Commented for bug 3197000 vgadde 19_dec-2003 End(1) */
93 
94                 /* Insert a record into IAC for each period that is defined in FA and not yet defined in IAC */
95                 FOR l_get_fa_period IN c_get_fa_periods(l_iac_max_date ,
96             	    					    l_get_cal_price_indexes.cal_price_index_link_id) LOOP
97 
98                     l_rowid := NULL;
99 
100                     igi_iac_cal_idx_values_pkg.insert_row(
101             	        	X_rowid		=> l_rowid ,
102             	        	X_cal_price_index_link_id => l_get_cal_price_indexes.cal_price_index_link_id,
103             	        	X_date_from	=> l_get_fa_period.start_date ,
104             	        	X_date_to	=> l_get_fa_period.end_date ,
105             	        	X_original_price_index_value => NULL ,
106             	        	X_current_price_index_value => 9999.99 );
107                 END LOOP;
108             END IF;
109         END LOOP;
110         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,' Successful completion of Synchronizing calendars');
111         RETURN true;
112 
113     EXCEPTION
114         WHEN OTHERS THEN
115             igi_iac_debug_pkg.debug_other_string(g_unexp_level,l_path,'***Error in IAC synchronize_calendars :'||sqlerrm);
116             RETURN false;
117     END Synchronize_calendars;
118 
119 /*=========================================================================+
120  | Function Name:                                                          |
121  |    Process_non_Deprn_Assets                                             |
122  |                                                                         |
123  | Description:                                                            |
124  |    This function calls additions catch-up process for non-depreciating  |
125  |    assets.  This function added for bug 2906034                         |
126  |                                                                         |
127  +=========================================================================*/
128     /*FUNCTION Process_non_Deprn_Assets(
129         p_book_type_code    IN VARCHAR2,
130         p_calling_function  IN VARCHAR2
131         ) return BOOLEAN IS
132 
133         CURSOR c_get_non_deprn_assets IS
134             SELECT bk.asset_id, ad.asset_category_id
135         	FROM fa_books bk, fa_additions ad
136         	WHERE bk.book_type_code = p_book_type_code
137         	AND bk.transaction_header_id_out IS NULL
138         	AND bk.depreciate_flag = 'NO'
139         	AND bk.adjustment_required_status = 'ADD'
140         	AND bk.asset_id = ad.asset_id
141         	AND NOT EXISTS (SELECT 'X'
142 				FROM igi_iac_asset_balances ab
143 				WHERE book_type_code = p_book_type_code
144 				AND  ab.asset_id = bk.asset_id);
145         l_path 	 	VARCHAR2(100);
146 
147     BEGIN
148         l_path 	:= g_path||'Process_non_Deprn_Assets';
149         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'Start of processing for non-depreciation assets');
150         FOR l_asset IN c_get_non_deprn_assets LOOP
151             IF NOT igi_iac_additions_pkg.do_prior_addition (
152                             p_book_type_code        => p_book_type_code,
153                             p_asset_id              => l_asset.asset_id,
154                             p_category_id           => l_asset.asset_category_id,
155                             p_deprn_method_code     => NULL,
156                             p_cost                  => NULL,
157                             p_adjusted_cost         => NULL,
158                             p_salvage_value         => NULL,
159                             p_current_unit          => NULL,
160                             p_life_in_months        => NULL,
161                             p_calling_function      => p_calling_function,
162                             p_event_id              => p_event_id ) THEN
163                 return FALSE;
164             END IF;
165         END LOOP;
166         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'End of processing of non-depreciation assets');
167         return TRUE;
168     EXCEPTION
169         WHEN OTHERS THEN
170         igi_iac_debug_pkg.debug_unexpected_msg(l_path);
171         return FALSE;
172     END Process_non_Deprn_Assets;*/
173 
174 
175 /*=========================================================================+
176  | Function Name:                                                          |
177  |    Periodic_Reval_of_Deprn                                              |
178  |                                                                         |
179  | Description:                                                            |
180  |    This function calculates complemetary depreciation amounts for IAC   |
181  |    corresponding FA depreciation amounts for each period.		   |
182  |                                                                         |
183  +=========================================================================*/
184     FUNCTION Periodic_Reval_of_Deprn( p_book_type_code varchar2
185         				 , p_period_counter number) RETURN BOOLEAN IS
186 
187         /* Cursor to get the assets that are depreciated in the current period by FA */
188         CURSOR c_fa_deprn_records IS
189    	        SELECT asset_id, deprn_amount
190    	        FROM fa_deprn_summary
191    	        WHERE book_type_code = p_book_type_code
192    	        AND   period_counter = p_period_counter ;
193 
194    	    /* Cursor to get the depreciation amount for the period for the asset */
195    	    CURSOR c_get_deprn_amount(p_asset_id fa_deprn_detail.asset_id%TYPE) IS
196    	    	SELECT sum(nvl(deprn_amount,0)), sum(nvl(deprn_adjustment_amount,0))
197    	    	FROM fa_deprn_detail
198    	    	WHERE book_type_code = p_book_type_code
199    	    	AND   period_counter = p_period_counter
200    	    	AND   asset_id = p_asset_id;
201 
202    	    /* Cursor to get the depreciation amount for the period for the asset */
203    	    CURSOR c_get_dist_deprn_amount(p_asset_id fa_deprn_detail.asset_id%TYPE,cp_distribution_id fa_deprn_detail.distribution_id%TYPE) IS
204    	    	SELECT nvl(deprn_amount,0) deprn_amount, nvl(deprn_adjustment_amount,0) deprn_adjustment_amount, nvl(deprn_reserve,0) deprn_reserve
205    	    	FROM fa_deprn_detail
206    	    	WHERE book_type_code = p_book_type_code
207    	    	AND   period_counter = p_period_counter
208    	    	AND   asset_id = p_asset_id
209    	    	AND   distribution_id = cp_distribution_id;
210 
211    	    /* Cursor to get fully reserved, fully retired info for the asset from FA */
212    	    CURSOR c_fa_books(p_asset_id fa_books.asset_id%TYPE) IS
213    	        SELECT period_counter_fully_reserved, period_counter_fully_retired,
214    	        	life_in_months, transaction_header_id_in, depreciate_flag,salvage_value,rate_adjustment_factor,cost
215    	        FROM fa_books
216    	        WHERE book_type_code = p_book_type_code
217    	        AND   asset_id = p_asset_id
218    	        AND   date_ineffective is NULL ;
219 
220 	    /* Cursor to get previous life of asset */
221 	    CURSOR c_get_prev_asset_life(p_asset_id fa_books.asset_id%TYPE,
222 	    				p_transaction_id fa_books.transaction_header_id_in%TYPE) IS
223    	        SELECT life_in_months
224    	        FROM fa_books
225    	        WHERE book_type_code = p_book_type_code
226    	        AND   asset_id = p_asset_id
227    	        AND   transaction_header_id_out = p_transaction_id
228    	        AND   adjustment_required_status <> 'ADD';
229 
230 	    /* Cursor to get the date of the transaction */
231 	    CURSOR c_get_transaction_info(p_transaction_id fa_books.transaction_header_id_in%TYPE) IS
232 	    	SELECT transaction_date_entered
233 	    	FROM fa_transaction_headers
234 	    	WHERE transaction_header_id = p_transaction_id;
235 
236    	    /* Cursor to get Current active transaction for asset from IAC */
237    	    CURSOR c_get_prev_adjustment(p_asset_id igi_iac_transaction_headers.asset_id%TYPE) IS
238    	        SELECT adjustment_id
239    	        FROM igi_iac_transaction_headers
240    	        WHERE asset_id = p_asset_id
241    	        AND book_type_code = p_book_type_code
242    	        AND adjustment_id_out is NULL ;
243 
244 	    /* Cursor to get the latest period for the asset for which balances exist */
245 	    CURSOR c_get_max_period_counter(p_asset_id igi_iac_asset_balances.asset_id%TYPE) IS
246 	    	SELECT max(period_counter)
247 	    	FROM igi_iac_asset_balances
248 	    	WHERE asset_id = p_asset_id
249 	    	AND book_type_code = p_book_type_code
250 	    	AND period_counter <= p_period_counter;
251 
252    	    /* Cursor to get balance information for the Asset */
253    	    CURSOR c_get_asset_balance (p_asset_id igi_iac_asset_balances.asset_id%TYPE,
254                                                       p_period_counter  igi_iac_asset_balances.period_counter%TYPE ) IS
255    	        SELECT *
256    	        FROM igi_iac_asset_balances
257    	        WHERE asset_id = p_asset_id
258    	        AND book_type_code = p_book_type_code
259    	        AND period_counter = p_period_counter ;
260 
261    	    /* Cursor to get total units assigned for the Asset from FA */
262    	    CURSOR c_get_fa_asset(p_asset_id fa_additions.asset_id%TYPE) IS
263    	        SELECT asset_category_id,current_units
264    	        FROM fa_additions
265    	        WHERE asset_id = p_asset_id;
266 
267    	    /* Cursor to get balance information for each distribution of asset */
268    	    CURSOR c_get_detail_balance(p_asset_id igi_iac_det_balances.asset_id%TYPE
269    	    			      ,p_prev_adjustment_id igi_iac_det_balances.adjustment_id%TYPE) IS
270    	        SELECT *
271    	        FROM igi_iac_det_balances
272    	        WHERE asset_id = p_asset_id
273    	        AND book_type_code = p_book_type_code
274    	        AND adjustment_id =  p_prev_adjustment_id ;
275 
276    	    /* Cursor to get number of units assigned for each distribution from FA */
277    	    CURSOR c_get_distribution_units(p_distribution_id igi_iac_det_balances.distribution_id%TYPE) IS
278    	        SELECT units_assigned
279    	        FROM fa_distribution_history
280    	        WHERE distribution_id = p_distribution_id ;
281 
282 	    /* Bug 2434532 vgadde 28/06/2002 Start(1) */
283 	    CURSOR c_get_deprn_calendar IS
284         	SELECT deprn_calendar
285         	FROM fa_book_controls
286         	WHERE book_type_code like p_book_type_code;
287 
288         CURSOR c_get_periods_in_year(p_calendar_type fa_calendar_types.calendar_type%TYPE) IS
289         	SELECT number_per_fiscal_year
290         	FROM fa_calendar_types
291         	WHERE calendar_type = p_calendar_type;
292             /* Bug 2434532 vgadde 28/06/2002 End(1) */
293 
294         CURSOR c_get_prev_year_inactive_dist(cp_asset_id igi_iac_det_balances.asset_id%TYPE
295             					,cp_distribution_id igi_iac_det_balances.distribution_id%TYPE) IS
296             SELECT 'X'
297             FROM igi_iac_det_balances
298             WHERE book_type_code = p_book_type_code
299             AND asset_id = cp_asset_id
300             AND distribution_id = cp_distribution_id
301             AND period_counter = p_period_counter - 1
302             AND nvl(active_flag,'Y') = 'N';
303 
304 	    CURSOR c_get_iac_fa_deprn_dists(cp_asset_id igi_iac_fa_deprn.asset_id%TYPE
305 	    				  ,cp_adjustment_id igi_iac_fa_deprn.adjustment_id%TYPE) IS
306             SELECT *
307             FROM igi_iac_fa_deprn
308             WHERE book_type_code = p_book_type_code
309             AND asset_id = cp_asset_id
310             AND adjustment_id = cp_adjustment_id;
311 
312         CURSOR c_get_prev_year_fa_inactive(cp_asset_id igi_iac_fa_deprn.asset_id%TYPE
313             					,cp_distribution_id igi_iac_fa_deprn.distribution_id%TYPE) IS
314             SELECT 'X'
315             FROM igi_iac_fa_deprn
316             WHERE book_type_code = p_book_type_code
317             AND asset_id = cp_asset_id
318             AND distribution_id = cp_distribution_id
319             AND period_counter = p_period_counter - 1
320             AND nvl(active_flag,'Y') = 'N';
321 
322         l_fully_reserved 	 fa_books.period_counter_fully_reserved%TYPE ;
323         l_fully_retired  	 fa_books.period_counter_fully_retired%TYPE ;
324         l_life_in_months	 fa_books.life_in_months%TYPE ;
325         l_depreciate_flag    fa_books.depreciate_flag%TYPE;
326         l_prev_life_months	 fa_books.life_in_months%TYPE ;
327         l_asset_balance  	 igi_iac_asset_balances%ROWTYPE;
328         l_asset_balance_next igi_iac_asset_balances%ROWTYPE;
329         l_asset_balance_curr igi_iac_asset_balances%ROWTYPE;
330         l_detail_balance	 igi_iac_det_balances%ROWTYPE;
331         l_adjustment_id  	 igi_iac_adjustments.adjustment_id%TYPE;
332         l_adjustment_id_out  igi_iac_adjustments.adjustment_id%TYPE;
333         l_prev_adjustment_id igi_iac_adjustments.adjustment_id%TYPE;
334         l_category_id    	 fa_additions.asset_category_id%TYPE;
335         l_asset_units	     fa_additions.current_units%TYPE;
336         l_distribution_units fa_distribution_history.units_assigned%TYPE;
337         l_deprn_ytd 	     igi_iac_det_balances.deprn_ytd%TYPE ;
338         l_deprn_amount	     igi_iac_asset_balances.deprn_amount%TYPE;
339         l_deprn_adj_amount	 fa_deprn_detail.deprn_adjustment_amount%TYPE;
340         l_reval_reserve  	 igi_iac_asset_balances.reval_reserve%TYPE;
341         l_general_fund   	 igi_iac_asset_balances.general_fund%TYPE;
342         l_reval_rsv_net  	 igi_iac_det_balances.reval_reserve_net%TYPE;
343         l_reval_rsv_gen_fund igi_iac_det_balances.reval_reserve_gen_fund%TYPE;
344         l_gen_fund_per   	 igi_iac_det_balances.general_fund_per%TYPE;
345         l_gen_fund_acc   	 igi_iac_det_balances.general_fund_acc%TYPE;
346         l_prd_rec		     igi_iac_types.prd_rec ;
347         l_errbuf		     varchar2(2000) ;
348         l_is_first_period 	 boolean ;
349         l_amount		     number ;
350         l_deprn_expense	     number ;
351         l_rowid  		     varchar2(25) ;
352         l_account_ccid	     number(15) ;
353         l_reval_reserve_ccid number(15) ;
354         l_set_of_books_id	 number(15) ;
355         l_chart_of_accts_id	 number(15) ;
356         l_currency_code	     varchar2(15) ;
357         l_precision		     varchar2(1) ;
358         l_dpis_period_counter igi_iac_asset_balances.period_counter%TYPE;
359         l_deprn_calendar      fa_calendar_types.calendar_type%TYPE;
360         l_periods_in_year     fa_calendar_types.number_per_fiscal_year%TYPE;
361         l_total_periods       NUMBER;
362         l_last_period_counter NUMBER;
363         l_max_period_counter  NUMBER;
364         l_fa_transaction_id   fa_books.transaction_header_id_in%TYPE;
365         l_transaction_date	  fa_transaction_headers.transaction_date_entered%TYPE;
366         l_Transaction_Type_Code	igi_iac_transaction_headers.transaction_type_code%TYPE;
367         l_Transaction_Id      igi_iac_transaction_headers.transaction_header_id%TYPE;
368         l_Mass_Reference_ID	  igi_iac_transaction_headers.mass_reference_id%TYPE;
369         l_Adjustment_Status   igi_iac_transaction_headers.adjustment_status%TYPE;
370         l_prev_year_inactive_dist varchar2(1);
371         l_salvage_value            Number;
372         l_salvage_value_correction Number;
373         l_rate_adjustment_factor   Number;
374         l_cost                     Number;
375         l_remaining_units          Number;
376         l_remaining_amount         Number;
377 
378 	    l_path 		       VARCHAR2(100);
379 	    p_event_id         NUMBER(15);
380 
381         -- Bulk changes
382         TYPE asset_id_tbl_type IS TABLE OF   FA_DEPRN_SUMMARY. ASSET_ID%TYPE
383                                            INDEX BY BINARY_INTEGER;
384         TYPE deprn_amount_tbl_type IS TABLE OF   FA_DEPRN_SUMMARY. DEPRN_AMOUNT%TYPE
385                                            INDEX BY BINARY_INTEGER;
386         l_fa_asset_id asset_id_tbl_type;
387         l_fa_deprn_amount deprn_amount_tbl_type;
388         l_loop_count                 number;
389         -- Bulk changes
390 
391         FUNCTION is_asset_revalued_once(p_asset_id igi_iac_asset_balances.asset_id%TYPE )
392                 RETURN boolean IS
393 	    /* This function returns True if asset is revalued atleast once in IAC else returns False */
394             CURSOR c_asset_reval_info IS
395                 SELECT count(*) from igi_iac_asset_balances
396                 WHERE asset_id = p_asset_id
397                 AND   book_type_code = p_book_type_code ;
398 
399             l_reval_count number;
400         BEGIN
401             OPEN c_asset_reval_info;
402             FETCH c_asset_reval_info INTO l_reval_count;
403             CLOSE c_asset_reval_info;
404 
405             IF (l_reval_count > 0) THEN
406                 RETURN true;
407             ELSE
408                 RETURN false;
409             END IF;
410         END is_asset_revalued_once;
411 
412     BEGIN
413         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,' Start of Depreciation Processing');
414 
415         l_Transaction_Type_Code	:= NULL;
416         l_Transaction_Id      := NULL;
417         l_Mass_Reference_ID	  := NULL;
418         l_Adjustment_Status   := NULL;
419 	    l_path 		:= g_path||'Periodic_Reval_of_Deprn';
420 
421         /* Get GL set_of_books_id for the IAC book */
422         IF NOT (igi_iac_common_utils.get_book_gl_info(p_book_type_code ,
423 			    				  l_set_of_books_id ,
424 			    				  l_chart_of_accts_id ,
425 			    				  l_currency_code ,
426 			    				  l_precision )) THEN
427 
428             igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'***Error in get_book_gl_info');
429             RETURN false;
430         END IF;
431 
432         /* Get period information from FA for the current period */
433         IF NOT (igi_iac_common_utils.Get_Period_Info_for_Counter(p_book_type_code,
434 	    							     p_period_counter,
435 	    							     l_prd_rec )) THEN
436 
437             igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'***Error in get_period_info_for_counter for current open period');
438             RETURN false;
439         END IF;
440 
441         IF NOT igi_iac_common_utils.populate_iac_fa_deprn_data(p_book_type_code,
442 	    							  'DEPRECIATION') THEN
443             igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'*** Error in Synchronizing Depreciation Data ***');
444             return FALSE;
445         END IF;
446 
447         --FOR l_fa_deprn_record IN c_fa_deprn_records LOOP
448         OPEN c_fa_deprn_records;
449         FETCH c_fa_deprn_records BULK COLLECT INTO
450                                        l_fa_asset_id,   l_fa_deprn_amount;
451         CLOSE c_fa_deprn_records;
452 
453         FOR l_loop_count IN 1.. l_fa_asset_id.count
454         LOOP
455 
456             OPEN c_fa_books(l_fa_asset_id(l_loop_count));
457             FETCH c_fa_books into l_fully_reserved,
458                                     l_fully_retired,
459                                     l_life_in_months,
460                                     l_fa_transaction_id,
461                                     l_depreciate_flag,
462                                     l_salvage_value,
463                                     l_rate_adjustment_factor,
464                                     l_cost;
465             CLOSE c_fa_books;
466             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,' Asset_id :'||to_char(l_fa_asset_id(l_loop_count)));
467             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'	    FA Transaction id :'||to_char(l_fa_transaction_id));
468 
469             l_transaction_date := NULL;
470             OPEN c_get_transaction_info(l_fa_transaction_id);
471             FETCH c_get_transaction_info INTO l_transaction_date;
472             CLOSE c_get_transaction_info;
473             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'	    FA Adjustment Transaction Date :'||to_char(l_transaction_date));
474 
475             l_prev_life_months := NULL;
476             IF (l_transaction_date IS NOT NULL) THEN
477                 IF (l_transaction_date >= l_prd_rec.period_start_date AND l_transaction_date <= l_prd_rec.period_end_date) THEN
478                     OPEN c_get_prev_asset_life(l_fa_asset_id(l_loop_count),l_fa_transaction_id);
479                     FETCH c_get_prev_asset_life INTO l_prev_life_months;
480                     CLOSE c_get_prev_asset_life;
481 
482                     l_fully_reserved := NULL;
483                 END IF;
484             END IF;
485             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'	    Previous Life in months :'||to_char(l_prev_life_months));
486 
487             /* Bug 2434532 vgadde 28/06/2002 Start(2) */
488             IF NOT igi_iac_common_utils.get_dpis_period_counter(p_book_type_code,
489                                                             l_fa_asset_id(l_loop_count),
490                                                             l_dpis_period_counter) THEN
491                 igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'*** Error in Fetching DPIS period counter');
492                 return FALSE;
493             END IF;
494 
495             OPEN c_get_deprn_calendar;
496             FETCH c_get_deprn_calendar INTO l_deprn_calendar;
497             CLOSE c_get_deprn_calendar;
498 
499             OPEN c_get_periods_in_year(l_deprn_calendar);
500             FETCH c_get_periods_in_year INTO l_periods_in_year;
501             CLOSE c_get_periods_in_year;
502 
503             l_total_periods := ceil((l_life_in_months*l_periods_in_year)/12);
504             l_last_period_counter := (l_dpis_period_counter + l_total_periods - 1);
505             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'		Last Period Counter :'||to_char(l_last_period_counter));
506 
507             IF (l_last_period_counter = p_period_counter) THEN
508                 l_fully_reserved := NULL;
509             END IF;
510             /* Bug 2434532 vgadde 28/06/2002 End(2) */
511 
512             IF(( l_fully_reserved is NULL ) AND
513                 ( l_fully_retired  is NULL ) AND
514                 l_depreciate_flag = 'YES' AND
515                 is_asset_revalued_once(l_fa_asset_id(l_loop_count))) THEN
516 
517                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Asset getting processed by depreciation ');
518 
519                 l_Transaction_Type_Code := NULL;
520                 l_Transaction_Id := NULL;
521                 l_Mass_Reference_ID := NULL;
522                 l_adjustment_id_out := NULL;
523                 l_prev_adjustment_id := NULL;
524                 l_Adjustment_Status := NULL;
525 
526                 IF NOT igi_iac_common_utils.Get_Latest_Transaction (
527                        		X_book_type_code    => p_book_type_code,
528                        		X_asset_id          => l_fa_asset_id(l_loop_count),
529                        		X_Transaction_Type_Code	=> l_Transaction_Type_Code,
530                        		X_Transaction_Id	=> l_Transaction_Id,
531                        		X_Mass_Reference_ID	=> l_Mass_Reference_ID,
532                        		X_Adjustment_Id		=> l_adjustment_id_out,
533                        		X_Prev_Adjustment_Id => l_prev_adjustment_id,
534                        		X_Adjustment_Status	=> l_Adjustment_Status) THEN
535                     igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'*** Error in fetching the latest transaction');
536                     return FALSE;
537                 END IF;
538 
539                     /* Commented for bug 2450345 vgadde 31-Jul-2002 Start
540                 OPEN c_get_prev_adjustment(l_fa_deprn_record.asset_id);
541                 FETCH c_get_prev_adjustment INTO l_prev_adjustment_id;
542                 CLOSE c_get_prev_adjustment;
543                     Commenting for bug 2450345 vgadde 31-Jul-2002 End */
544                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Previous actual adjustment id :'||to_char(l_prev_adjustment_id));
545                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Previous adjustment id :'||to_char(l_adjustment_id_out));
546 
547                 OPEN c_get_fa_asset(l_fa_asset_id(l_loop_count));
548                 FETCH c_get_fa_asset INTO l_category_id,l_asset_units;
549                 CLOSE c_get_fa_asset;
550                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Units assigned for the asset :'||to_char(l_asset_units));
551                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Inserting into igi_iac_transaction_headers');
552                 l_adjustment_id := NULL;
553                 l_rowid := NULL;
554 
555                 -- Fetching the event_id from fa_deprn_summary as in case of depreciation
556                 -- fa is not supplying the event_id
557                 -- R12 uptake
558                 select event_id into p_event_id from fa_deprn_summary
559                     where asset_id=l_fa_asset_id(l_loop_count)
560                     and book_type_code=p_book_type_code
561                     and period_counter=p_period_counter;
562 
563                 igi_iac_trans_headers_pkg.insert_row(
564                     X_rowid		 => l_rowid ,
565                     X_adjustment_id	 => l_adjustment_id ,
566                     X_transaction_header_id => NULL ,
567                     X_adjustment_id_out	 => NULL ,
568                     X_transaction_type_code => 'DEPRECIATION' ,
569                     X_transaction_date_entered => sysdate ,
570                     X_mass_refrence_id	 => NULL ,
571                     X_transaction_sub_type	 => NULL ,
572                     X_book_type_code	 => p_book_type_code ,
573                     X_asset_id		 => l_fa_asset_id(l_loop_count) ,
574                     X_category_id		 => l_category_id ,
575                     X_adj_deprn_start_date	 => l_prd_rec.period_end_date ,
576                     X_revaluation_type_flag => NULL,
577                     X_adjustment_status	 => 'COMPLETE' ,
578                     X_period_counter	 => p_period_counter,
579                     X_event_id           => p_event_id ) ;
580 
581                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     New adjustment id :'||to_char(l_adjustment_id));
582 
583                 OPEN c_get_max_period_counter(l_fa_asset_id(l_loop_count));
584                 FETCH c_get_max_period_counter INTO l_max_period_counter;
585                 CLOSE c_get_max_period_counter;
586                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'	 Max asset balances period counter :'||to_char(l_max_period_counter));
587 
588                 OPEN c_get_asset_balance(l_fa_asset_id(l_loop_count),l_max_period_counter);
589                 FETCH c_get_asset_balance INTO l_asset_balance;
590                 CLOSE c_get_asset_balance;
591 
592                 OPEN c_get_deprn_amount(l_fa_asset_id(l_loop_count));
593                 FETCH c_get_deprn_amount INTO l_deprn_amount,l_deprn_adj_amount;
594                 CLOSE c_get_deprn_amount;
595 
596                 /* Modified for adjustments sekhar Start*/
597                 /*IF ((nvl(l_prev_life_months,0)=0) OR (nvl(l_prev_life_months,0)=l_life_in_months)) THEN
598 		    	l_deprn_amount := l_deprn_amount - l_deprn_adj_amount;
599                 END IF;*/
600 
601                 l_deprn_amount := l_deprn_amount - l_deprn_adj_amount;
602                 /* Modified for adjustments sekhar  end*/
603 
604                 /* salvage value correction */
605                 If l_salvage_value <> 0 Then
606                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     FA Deprn amount for period before salvage correction:'||to_char(l_deprn_amount));
607                     IF NOT igi_iac_salvage_pkg.correction(p_asset_id => l_fa_asset_id(l_loop_count),
608                                                       P_book_type_code =>p_book_type_code,
609                                                       P_value=>l_deprn_amount,
610                                                       P_cost=>l_cost,
611                                                       P_salvage_value=>l_salvage_value,
612                                                       P_calling_program=>'DEPRECIATION') THEN
613                         igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'+Salavge Value Correction Failed : ');
614                         return false;
615                     END IF;
616                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     FA Deprn amount for period after salvage correction:'||to_char(l_deprn_amount));
617                 END IF;
618 	            /* salvage value correction */
619 
620                 IF NOT (igi_iac_common_utils.iac_round(l_deprn_amount,p_book_type_code)) THEN
621                     RETURN false;
622                 END IF;
623                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     FA Deprn amount for period :'||to_char(l_deprn_amount));
624 
625                 l_deprn_expense := (l_deprn_amount * l_asset_balance.cumulative_reval_factor) - l_deprn_amount;
626 
627                 IF NOT (igi_iac_common_utils.iac_round(l_deprn_expense,p_book_type_code)) THEN
628                     RETURN false;
629                 END IF;
630                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Cumulative revaluation rate :'||to_char(l_asset_balance.cumulative_reval_factor));
631                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     IAC Deprn amount for period :'||to_char(l_deprn_expense));
632 
633                 l_remaining_units := l_asset_units;
634                 l_remaining_amount := l_deprn_expense;
635 
636                 FOR l_detail_balance IN c_get_detail_balance(l_fa_asset_id(l_loop_count),l_prev_adjustment_id) LOOP
637 
638                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Distribution Id :'||to_char(l_detail_balance.distribution_id));
639                     IF ( l_prd_rec.period_num = 1) THEN
640                         l_deprn_ytd := 0 ;
641                         l_is_first_period := TRUE ;
642                     ELSE
643                         l_deprn_ytd := l_detail_balance.deprn_ytd ;
644                         l_is_first_period := FALSE ;
645                     END IF;
646 
647                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Current YTD for the distribution :'||to_char(l_deprn_ytd));
648                     IF (nvl(l_detail_balance.active_flag,'Y') <> 'N') THEN
649 
650                         OPEN c_get_distribution_units(l_detail_balance.distribution_id);
651                         FETCH c_get_distribution_units INTO l_distribution_units;
652                         CLOSE c_get_distribution_units;
653                         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Active distribution - Units :'||to_char(l_distribution_units));
654 
655                         l_remaining_units := l_remaining_units - l_distribution_units;
656                         IF l_remaining_units = 0 THEN
657                             l_amount := l_remaining_amount;
658                         ELSE
659                             l_amount := (l_distribution_units / l_asset_units) * l_deprn_expense;
660                             IF NOT (igi_iac_common_utils.iac_round(l_amount,p_book_type_code)) THEN
661                                 RETURN false;
662                             END IF;
663                             l_remaining_amount := l_remaining_amount - l_amount;
664                         END IF;
665                         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Prorated amount for the distribution :'||to_char(l_amount));
666 
667                         IF (nvl(l_amount,0) <> 0) THEN
668                         /* Create accounting entries for non zero values */
669                             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Creating accounting entries in igi_iac_adjustments');
670                             l_rowid := NULL;
671                             l_account_ccid := NULL ;
672 
673                             IF NOT (igi_iac_common_utils.get_account_ccid(p_book_type_code ,
674 			    						  l_asset_balance.asset_id ,
675 			    						  l_detail_balance.distribution_id ,
676 			    						  'DEPRN_EXPENSE_ACCT' ,
677 			    						  l_account_ccid )) THEN
678 
679                                 RETURN false;
680                             END IF;
681                             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Deprn Expense ccid :'||to_char(l_account_ccid));
682                             igi_iac_adjustments_pkg.insert_row(
683                                 X_rowid			=> l_rowid ,
684                                 X_adjustment_id		=> l_adjustment_id ,
685                                 X_book_type_code	=> p_book_type_code ,
686                                 X_code_combination_id	=> l_account_ccid,
687                                 X_set_of_books_id	=> l_set_of_books_id ,
688                                 X_dr_cr_flag   		=> 'DR' ,
689                                 X_amount               	=> l_amount ,
690                                 X_adjustment_type      	=> 'EXPENSE' ,
691                                 X_transfer_to_gl_flag  	=> 'Y' ,
692                                 X_units_assigned        => l_distribution_units ,
693                                 X_asset_id		=> l_asset_balance.asset_id ,
694                                 X_distribution_id      	=> l_detail_balance.distribution_id ,
695                                 X_period_counter       	=> p_period_counter,
696                                 X_adjustment_offset_type => 'RESERVE',
697                                 X_report_ccid            => NULL,
698                                 x_mode                  => 'R',
699                                 X_event_id           => p_event_id ) ;
700 
701                             l_rowid := NULL;
702                             l_account_ccid := NULL ;
703 
704                             IF NOT (igi_iac_common_utils.get_account_ccid(p_book_type_code ,
705 			    						  l_asset_balance.asset_id ,
706 			    						  l_detail_balance.distribution_id ,
707 			    						  'DEPRN_RESERVE_ACCT' ,
708 			    						  l_account_ccid )) THEN
709 
710                                 RETURN false;
711                             END IF;
712                             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Deprn Reserve ccid :'||to_char(l_account_ccid));
713                             igi_iac_adjustments_pkg.insert_row(
714                                 X_rowid			=> l_rowid ,
715                                 X_adjustment_id		=> l_adjustment_id ,
716                                 X_book_type_code	=> p_book_type_code ,
717                                 X_code_combination_id	=> l_account_ccid,
718                                 X_set_of_books_id	=> l_set_of_books_id ,
719                                 X_dr_cr_flag   		=> 'CR' ,
720                                 X_amount               	=> l_amount ,
721                                 X_adjustment_type      	=> 'RESERVE' ,
722                                 X_transfer_to_gl_flag  	=> 'Y' ,
723                                 X_units_assigned        => l_distribution_units ,
724                                 X_asset_id		=> l_asset_balance.asset_id ,
725                                 X_distribution_id      	=> l_detail_balance.distribution_id ,
726                                 X_period_counter       	=> p_period_counter,
727                                 X_adjustment_offset_type => 'EXPENSE',
728                                 X_report_ccid            => NULL,
729                                 x_mode                  => 'R',
730                                 X_event_id              => p_event_id ) ;
731 
732                             IF (l_asset_balance.adjusted_cost > 0) THEN
733 
734                                 l_rowid := NULL;
735                                 l_account_ccid := NULL ;
736 
737                                 IF NOT (igi_iac_common_utils.get_account_ccid(p_book_type_code ,
738 			    						  l_asset_balance.asset_id ,
739 			    						  l_detail_balance.distribution_id ,
740 			    						  'REVAL_RESERVE_ACCT' ,
741 			    						  l_account_ccid )) THEN
742 
743                                     RETURN false;
744                                 END IF;
745                                 l_reval_reserve_ccid := l_account_ccid;
746 
747                                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Reval reserve ccid :'||to_char(l_account_ccid));
748                                 igi_iac_adjustments_pkg.insert_row(
749                                     X_rowid			=> l_rowid ,
750                                     X_adjustment_id		=> l_adjustment_id ,
751                                     X_book_type_code	=> p_book_type_code ,
752                                     X_code_combination_id	=> l_account_ccid,
753                                     X_set_of_books_id	=> l_set_of_books_id ,
754                                     X_dr_cr_flag   		=> 'DR' ,
755                                     X_amount               	=> l_amount ,
756                                     X_adjustment_type      	=> 'REVAL RESERVE' ,
757                                     X_transfer_to_gl_flag  	=> 'Y' ,
758                                     X_units_assigned        => l_distribution_units ,
759                                     X_asset_id		=> l_asset_balance.asset_id ,
760                                     X_distribution_id      	=> l_detail_balance.distribution_id ,
761                                     X_period_counter       	=> p_period_counter,
762                                     X_adjustment_offset_type => 'GENERAL FUND',
763                                     X_report_ccid            => NULL,
764                                     x_mode                  => 'R',
765                                     X_event_id              => p_event_id ) ;
766 
767                                 l_rowid := NULL;
768                                 l_account_ccid := NULL ;
769 
770                                 IF NOT (igi_iac_common_utils.get_account_ccid(p_book_type_code ,
771 			    						  l_asset_balance.asset_id ,
772 			    						  l_detail_balance.distribution_id ,
773 			    						  'GENERAL_FUND_ACCT' ,
774 			    						  l_account_ccid )) THEN
775 
776                                     RETURN false;
777                                 END IF;
778                                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     General Fund ccid :'||to_char(l_account_ccid));
779                                 igi_iac_adjustments_pkg.insert_row(
780                                     X_rowid			=> l_rowid ,
781                                     X_adjustment_id		=> l_adjustment_id ,
782                                     X_book_type_code	=> p_book_type_code ,
783                                     X_code_combination_id	=> l_account_ccid,
784                                     X_set_of_books_id	=> l_set_of_books_id ,
785                                     X_dr_cr_flag   		=> 'CR' ,
786                                     X_amount               	=> l_amount ,
787                                     X_adjustment_type      	=> 'GENERAL FUND' ,
788                                     X_transfer_to_gl_flag  	=> 'Y' ,
789                                     X_units_assigned        => l_distribution_units ,
790                                     X_asset_id		=> l_asset_balance.asset_id ,
791                                     X_distribution_id      	=> l_detail_balance.distribution_id ,
792                                     X_period_counter       	=> p_period_counter,
793                                     X_adjustment_offset_type => 'REVAL RESERVE',
794                                     X_report_ccid            => l_reval_reserve_ccid,
795                                     x_mode                  => 'R',
796                                     X_event_id              => p_event_id ) ;
797                             END IF;
798                         END IF; /* Creating accounting entries for non zero values */
799 
800                         l_rowid := null ;
801 
802                         IF (nvl(l_detail_balance.active_flag,'Y') <> 'N') THEN
803                             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Inserting into igi_iac_det_balances');
804             	    		/*  Bug 2407393 vgadde 07/06/2002 Start(1) */
805                             IF (l_asset_balance.adjusted_cost > 0) THEN
806                                 l_reval_rsv_net := l_detail_balance.reval_reserve_net - l_amount;
807                                 l_reval_rsv_gen_fund := l_detail_balance.reval_reserve_gen_fund + l_amount;
808                                 l_gen_fund_acc := l_detail_balance.general_fund_acc + l_amount;
809                                 l_gen_fund_per := l_amount;
810                             ELSE
811                                 l_reval_rsv_net := l_detail_balance.reval_reserve_net;
812                                 l_reval_rsv_gen_fund := l_detail_balance.reval_reserve_gen_fund;
813                                 l_gen_fund_acc := l_detail_balance.general_fund_acc;
814                                 l_gen_fund_per := 0;
815                             END IF;
816             	    		/*  Bug 2407393 vgadde 07/06/2002 Start(1) */
817 
818                             igi_iac_det_balances_pkg.insert_row(
819                                 X_rowid			 => l_rowid ,
820                                 X_adjustment_id		 => l_adjustment_id ,
821                                 X_asset_id		 => l_asset_balance.asset_id ,
822                                 X_distribution_id	 => l_detail_balance.distribution_id ,
823                                 X_book_type_code	 => p_book_type_code ,
824                                 X_period_counter	 => p_period_counter ,
825                                 X_adjustment_cost	 => l_detail_balance.adjustment_cost ,
826                                 X_net_book_value	 => l_detail_balance.net_book_value - l_amount ,
827                                 X_reval_reserve_cost	 => l_detail_balance.reval_reserve_cost ,
828                                 X_reval_reserve_backlog  => l_detail_balance.reval_reserve_backlog ,
829                                 X_reval_reserve_gen_fund => l_reval_rsv_gen_fund ,
830                                 X_reval_reserve_net	 => l_reval_rsv_net ,
831                                 X_operating_acct_cost	 => l_detail_balance.operating_acct_cost ,
832                                 X_operating_acct_backlog => l_detail_balance.operating_acct_backlog ,
833                                 X_operating_acct_net	 => l_detail_balance.operating_acct_net ,
834                                 X_operating_acct_ytd	 => l_detail_balance.operating_acct_ytd ,
835                                 X_deprn_period		 => l_amount ,
836                                 X_deprn_ytd		 => l_deprn_ytd + l_amount ,
837                                 X_deprn_reserve		 => l_detail_balance.deprn_reserve + l_amount ,
838                                 X_deprn_reserve_backlog	 => l_detail_balance.deprn_reserve_backlog ,
839                                 X_general_fund_per	 => l_gen_fund_per ,
840                                 X_general_fund_acc	 => l_gen_fund_acc ,
841                                 X_last_reval_date	 => l_detail_balance.last_reval_date ,
842                                 X_current_reval_factor	 => l_detail_balance.current_reval_factor ,
843                                 X_cumulative_reval_factor =>l_detail_balance.cumulative_reval_factor ,
844                                 X_active_flag		 => l_detail_balance.active_flag ) ;
845                         END IF;
846 
847                     END IF;
848 
849                     IF ((nvl(l_detail_balance.active_flag,'Y') = 'N') AND (NOT l_is_first_period)) THEN
850                         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Processing inactive distribution');
851                         l_rowid := null;
852                         igi_iac_det_balances_pkg.insert_row(
853                             X_rowid			 => l_rowid ,
854                             X_adjustment_id		 => l_adjustment_id ,
855                             X_asset_id		 => l_asset_balance.asset_id ,
856                             X_distribution_id	 => l_detail_balance.distribution_id ,
857                             X_book_type_code	 => p_book_type_code ,
858                             X_period_counter	 => p_period_counter ,
859                             X_adjustment_cost	 => l_detail_balance.adjustment_cost ,
860                             X_net_book_value	 => l_detail_balance.net_book_value ,
861                             X_reval_reserve_cost	 => l_detail_balance.reval_reserve_cost ,
862                             X_reval_reserve_backlog  => l_detail_balance.reval_reserve_backlog ,
863                             X_reval_reserve_gen_fund => l_detail_balance.reval_reserve_gen_fund ,
864                             X_reval_reserve_net	 => l_detail_balance.reval_reserve_net ,
865                             X_operating_acct_cost	 => l_detail_balance.operating_acct_cost ,
866                             X_operating_acct_backlog => l_detail_balance.operating_acct_backlog ,
867                             X_operating_acct_net	 => l_detail_balance.operating_acct_net ,
868                             X_operating_acct_ytd	 => l_detail_balance.operating_acct_ytd ,
869                             X_deprn_period		 => l_detail_balance.deprn_period ,
870                             X_deprn_ytd		 => l_detail_balance.deprn_ytd ,
871                             X_deprn_reserve		 => l_detail_balance.deprn_reserve ,
872                             X_deprn_reserve_backlog	 => l_detail_balance.deprn_reserve_backlog ,
873                             X_general_fund_per	 => l_detail_balance.general_fund_per ,
874                             X_general_fund_acc	 => l_detail_balance.general_fund_acc ,
875                             X_last_reval_date	 => l_detail_balance.last_reval_date ,
876                             X_current_reval_factor	 => l_detail_balance.current_reval_factor ,
877                             X_cumulative_reval_factor =>l_detail_balance.cumulative_reval_factor ,
878                             X_active_flag		 => l_detail_balance.active_flag ) ;
879 
880                     END IF;
881 
882                     IF ((nvl(l_detail_balance.active_flag,'Y') = 'N') AND (l_is_first_period)) THEN
883                         l_prev_year_inactive_dist := NULL;
884                         OPEN c_get_prev_year_inactive_dist(  l_asset_balance.asset_id
885  			    					,l_detail_balance.distribution_id);
886                         FETCH c_get_prev_year_inactive_dist INTO l_prev_year_inactive_dist;
887                         CLOSE c_get_prev_year_inactive_dist;
888 
889                         IF l_prev_year_inactive_dist IS NULL THEN
890                             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Processing inactive distribution in first period');
891                             l_rowid := null;
892                             igi_iac_det_balances_pkg.insert_row(
893                                 X_rowid			 => l_rowid ,
894                                 X_adjustment_id		 => l_adjustment_id ,
895                                 X_asset_id		 => l_asset_balance.asset_id ,
896                                 X_distribution_id	 => l_detail_balance.distribution_id ,
897                                 X_book_type_code	 => p_book_type_code ,
898                                 X_period_counter	 => p_period_counter ,
899                                 X_adjustment_cost	 => 0 ,
900                                 X_net_book_value	 => 0 ,
901                                 X_reval_reserve_cost	 => 0 ,
902                                 X_reval_reserve_backlog  => 0 ,
903                                 X_reval_reserve_gen_fund => 0 ,
904                                 X_reval_reserve_net	 => 0 ,
905                                 X_operating_acct_cost	 => 0 ,
906                                 X_operating_acct_backlog => 0 ,
907                                 X_operating_acct_net	 => 0 ,
908                                 X_operating_acct_ytd	 => 0 ,
909                                 X_deprn_period		 => 0 ,
910                                 X_deprn_ytd		 => 0 ,
911                                 X_deprn_reserve		 => 0 ,
912                                 X_deprn_reserve_backlog	 => 0 ,
913                                 X_general_fund_per	 => 0 ,
914                                 X_general_fund_acc	 => 0 ,
915                                 X_last_reval_date	 => l_detail_balance.last_reval_date ,
916                                 X_current_reval_factor	 => l_detail_balance.current_reval_factor ,
917                                 X_cumulative_reval_factor =>l_detail_balance.cumulative_reval_factor ,
918                                 X_active_flag		 => l_detail_balance.active_flag ) ;
919                         END IF;
920                     END IF;
921 
922                 END LOOP;
923 
924                 FOR l_iac_fa_deprn_dist IN c_get_iac_fa_deprn_dists(l_asset_balance.asset_id,
925 		    							     l_prev_adjustment_id) LOOP
926                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Distribution Id :'||to_char(l_iac_fa_deprn_dist.distribution_id));
927                     IF ( l_prd_rec.period_num = 1) THEN
928                         l_deprn_ytd := 0 ;
929                         l_is_first_period := TRUE ;
930                     ELSE
931                         l_deprn_ytd := l_iac_fa_deprn_dist.deprn_ytd ;
932                         l_is_first_period := FALSE ;
933                     END IF;
934 
935                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Current YTD for the distribution :'||to_char(l_deprn_ytd));
936                     IF (nvl(l_iac_fa_deprn_dist.active_flag,'Y') <> 'N') THEN
937 
938                         FOR l_fa_dist_amounts IN c_get_dist_deprn_amount(l_asset_balance.asset_id,l_iac_fa_deprn_dist.distribution_id) LOOP
939                             l_rowid := NULL;
940                             igi_iac_fa_deprn_pkg.insert_row(
941                                 x_rowid			=> l_rowid,
942                                 x_book_type_code	=> p_book_type_code,
943                                 x_asset_id		=> l_asset_balance.asset_id,
944                                 x_distribution_id	=> l_iac_fa_deprn_dist.distribution_id,
945                                 x_period_counter	=> p_period_counter,
946                                 x_adjustment_id		=> l_adjustment_id,
947                                 x_deprn_period		=> l_fa_dist_amounts.deprn_amount - l_fa_dist_amounts.deprn_adjustment_amount,
948                                 x_deprn_ytd		=> l_deprn_ytd +
949 							        (l_fa_dist_amounts.deprn_amount - l_fa_dist_amounts.deprn_adjustment_amount),
950                                 x_deprn_reserve		=> l_fa_dist_amounts.deprn_reserve,
951                                 x_active_flag		=> l_iac_fa_deprn_dist.active_flag,
952                                 x_mode			=> 'R');
953                         END LOOP;
954 
955                     END IF;
956 
957                     IF ((nvl(l_iac_fa_deprn_dist.active_flag,'Y') = 'N') AND (NOT l_is_first_period)) THEN
958                         l_rowid := NULL;
959                         igi_iac_fa_deprn_pkg.insert_row(
960                             x_rowid			=> l_rowid,
961                             x_book_type_code	=> l_iac_fa_deprn_dist.book_type_code,
962                             x_asset_id		=> l_iac_fa_deprn_dist.asset_id,
963                             x_distribution_id	=> l_iac_fa_deprn_dist.distribution_id,
964                             x_period_counter	=> p_period_counter,
965                             x_adjustment_id		=> l_adjustment_id,
966                             x_deprn_period		=> l_iac_fa_deprn_dist.deprn_period,
967                             x_deprn_ytd		=> l_iac_fa_deprn_dist.deprn_ytd,
968                             x_deprn_reserve		=> l_iac_fa_deprn_dist.deprn_reserve,
969                             x_active_flag		=> l_iac_fa_deprn_dist.active_flag,
970                             x_mode			=> 'R');
971 
972                     END IF;
973 
974                     IF ((nvl(l_iac_fa_deprn_dist.active_flag,'Y') = 'N') AND (l_is_first_period)) THEN
975                         l_prev_year_inactive_dist := NULL;
976                         OPEN c_get_prev_year_fa_inactive(  l_iac_fa_deprn_dist.asset_id
977  			    					,l_iac_fa_deprn_dist.distribution_id);
978                         FETCH c_get_prev_year_fa_inactive INTO l_prev_year_inactive_dist;
979                         CLOSE c_get_prev_year_fa_inactive;
980 
981                         IF l_prev_year_inactive_dist IS NULL THEN
982                             l_rowid := NULL;
983                             igi_iac_fa_deprn_pkg.insert_row(
984                                 x_rowid			=> l_rowid,
985                                 x_book_type_code	=> l_iac_fa_deprn_dist.book_type_code,
986                                 x_asset_id		=> l_iac_fa_deprn_dist.asset_id,
987                                 x_distribution_id	=> l_iac_fa_deprn_dist.distribution_id,
988                                 x_period_counter	=> p_period_counter,
989                                 x_adjustment_id		=> l_adjustment_id,
990                                 x_deprn_period		=> 0,
991                                 x_deprn_ytd		=> 0,
992                                 x_deprn_reserve		=> 0,
993                                 x_active_flag		=> l_iac_fa_deprn_dist.active_flag,
994                                 x_mode			=> 'R');
995                         END IF;
996                     END IF;
997                 END LOOP;
998 
999                 /*  Bug 2407393 vgadde 07/06/2002 Start(2) */
1000                 IF (l_asset_balance.adjusted_cost > 0) THEN
1001                     l_reval_reserve := l_asset_balance.reval_reserve - l_deprn_expense;
1002                     l_general_fund := l_asset_balance.general_fund + l_deprn_expense;
1003                 ELSE
1004                     l_reval_reserve := l_asset_balance.reval_reserve;
1005                     l_general_fund := l_asset_balance.general_fund;
1006                 END IF;
1007                 /*  Bug 2407393 vgadde 07/06/2002 End(2) */
1008 
1009                 OPEN c_get_asset_balance(l_fa_asset_id(l_loop_count),p_period_counter );
1010                 FETCH c_get_asset_balance INTO l_asset_balance_curr;
1011                 IF c_get_asset_balance%FOUND THEN
1012 
1013                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Updating  asset balances for the current period');
1014                     igi_iac_asset_balances_pkg.update_row(
1015                         X_asset_id		=> l_asset_balance.asset_id ,
1016                         X_book_type_code	=> p_book_type_code ,
1017                         X_period_counter	=> p_period_counter ,
1018                         X_net_book_value	=> l_asset_balance.net_book_value - l_deprn_expense ,
1019                         X_adjusted_cost		=> l_asset_balance.adjusted_cost ,
1020                         X_operating_acct	=> l_asset_balance.operating_acct ,
1021                         X_reval_reserve		=> l_reval_reserve ,
1022                         X_deprn_amount		=> l_deprn_expense ,
1023                         X_deprn_reserve		=> l_asset_balance.deprn_reserve + l_deprn_expense ,
1024                         X_backlog_deprn_reserve => l_asset_balance.backlog_deprn_reserve ,
1025                         X_general_fund		=> l_general_fund ,
1026                         X_last_reval_date	=> l_asset_balance.last_reval_date ,
1027                         X_current_reval_factor	=> l_asset_balance.current_reval_factor ,
1028                         X_cumulative_reval_factor => l_asset_balance.cumulative_reval_factor ) ;
1029                 ELSE
1030                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'    Inserting asset balances for the current period');
1031                     l_rowid := NULL;
1032                     igi_iac_asset_balances_pkg.insert_row(
1033                         X_rowid			=> l_rowid ,
1034                         X_asset_id		=> l_asset_balance.asset_id ,
1035                         X_book_type_code	=> p_book_type_code ,
1036                         X_period_counter	=> p_period_counter ,
1037                         X_net_book_value	=> l_asset_balance.net_book_value - l_deprn_expense ,
1038                         X_adjusted_cost		=> l_asset_balance.adjusted_cost ,
1039                         X_operating_acct	=> l_asset_balance.operating_acct ,
1040                         X_reval_reserve		=> l_reval_reserve ,
1041                         X_deprn_amount		=> l_deprn_expense ,
1042                         X_deprn_reserve		=> l_asset_balance.deprn_reserve + l_deprn_expense ,
1043                         X_backlog_deprn_reserve => l_asset_balance.backlog_deprn_reserve ,
1044                         X_general_fund		=> l_general_fund ,
1045                         X_last_reval_date	=> l_asset_balance.last_reval_date ,
1046                         X_current_reval_factor	=> l_asset_balance.current_reval_factor ,
1047                         X_cumulative_reval_factor => l_asset_balance.cumulative_reval_factor ) ;
1048                 END IF;
1049     		    CLOSE c_get_asset_balance;
1050 
1051                 OPEN c_get_asset_balance(l_fa_asset_id(l_loop_count),p_period_counter +1);
1052                 FETCH c_get_asset_balance INTO l_asset_balance_next;
1053                 IF c_get_asset_balance%FOUND THEN
1054 
1055                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Updating  asset balances for the next period');
1056                     igi_iac_asset_balances_pkg.update_row(
1057                         X_asset_id		=> l_asset_balance.asset_id ,
1058                         X_book_type_code	=> p_book_type_code ,
1059                         X_period_counter	=> p_period_counter + 1 ,
1060                         X_net_book_value	=> l_asset_balance.net_book_value - l_deprn_expense ,
1061                         X_adjusted_cost		=> l_asset_balance.adjusted_cost ,
1062                         X_operating_acct	=> l_asset_balance.operating_acct ,
1063                         X_reval_reserve		=> l_reval_reserve ,
1064                         X_deprn_amount		=> l_deprn_expense ,
1065                         X_deprn_reserve		=> l_asset_balance.deprn_reserve + l_deprn_expense ,
1066                         X_backlog_deprn_reserve => l_asset_balance.backlog_deprn_reserve ,
1067                         X_general_fund		=> l_general_fund ,
1068                         X_last_reval_date	=> l_asset_balance.last_reval_date ,
1069                         X_current_reval_factor	=> l_asset_balance.current_reval_factor ,
1070                         X_cumulative_reval_factor => l_asset_balance.cumulative_reval_factor ) ;
1071                 ELSE
1072                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Inserting  asset balances for the next period');
1073                     l_rowid := NULL;
1074                     igi_iac_asset_balances_pkg.insert_row(
1075                         X_rowid			=> l_rowid ,
1076                         X_asset_id		=> l_asset_balance.asset_id ,
1077                         X_book_type_code	=> p_book_type_code ,
1078                         X_period_counter	=> p_period_counter + 1 ,
1079                         X_net_book_value	=> l_asset_balance.net_book_value - l_deprn_expense ,
1080                         X_adjusted_cost		=> l_asset_balance.adjusted_cost ,
1081                         X_operating_acct	=> l_asset_balance.operating_acct ,
1082                         X_reval_reserve		=> l_reval_reserve ,
1083                         X_deprn_amount		=> l_deprn_expense ,
1084                         X_deprn_reserve		=> l_asset_balance.deprn_reserve + l_deprn_expense ,
1085                         X_backlog_deprn_reserve => l_asset_balance.backlog_deprn_reserve ,
1086                         X_general_fund		=> l_general_fund ,
1087                         X_last_reval_date	=> l_asset_balance.last_reval_date ,
1088                         X_current_reval_factor	=> l_asset_balance.current_reval_factor ,
1089                         X_cumulative_reval_factor => l_asset_balance.cumulative_reval_factor ) ;
1090                 END IF;
1091                 CLOSE c_get_asset_balance;
1092 
1093                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Making previous transaction inactive.');
1094                 igi_iac_trans_headers_pkg.update_row(
1095                     X_prev_adjustment_id	=> l_adjustment_id_out ,
1096                     X_adjustment_id		=> l_adjustment_id ) ;
1097 
1098             END IF;
1099         END LOOP;
1100 
1101         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,' Successful completion of periodic_reval_of_deprn');
1102         RETURN true;
1103 
1104     EXCEPTION
1105         WHEN OTHERS THEN
1106             igi_iac_debug_pkg.debug_unexpected_msg(l_path);
1107             RETURN false;
1108     END Periodic_Reval_of_Deprn;
1109 
1110     FUNCTION Synchronize_Accounts(
1111         p_book_type_code    IN VARCHAR2,
1112         p_period_counter    IN NUMBER,
1113         p_calling_function  IN VARCHAR2
1114         ) return BOOLEAN IS
1115 
1116         CURSOR c_get_adjustments(c_adjustment_id number)  IS
1117         SELECT rowid,
1118             adjustment_id,
1119             book_type_code,
1120             code_combination_id,
1121             adjustment_type,
1122             asset_id,
1123             distribution_id,
1124             period_counter
1125         FROM igi_iac_adjustments
1126         WHERE book_type_code = p_book_type_code
1127         AND period_counter = p_period_counter
1128         AND adjustment_type IN ('COST','RESERVE','EXPENSE')
1129         AND adjustment_id = c_adjustment_id for update;
1130 
1131         CURSOR c_get_transaction IS
1132         SELECT adjustment_id,
1133                 transaction_header_id,
1134                 transaction_type_code
1135         FROM igi_iac_transaction_headers
1136         WHERE book_type_code = p_book_type_code
1137         AND period_counter = p_period_counter
1138         AND transaction_type_code in ('RECLASS','ADDITION','DEPRECIATION');
1139 
1140         CURSOR c_get_cost_reserve_ccid ( c_asset_id NUMBER,
1141                                         c_distribution_id NUMBER,
1142                                         c_adjustment_source_type_code VARCHAR2,
1143                                         c_adjustment_type   VARCHAR2,
1144                                         c_transaction_header_id NUMBER) IS
1145         SELECT code_combination_id
1146         FROM fa_adjustments
1147         WHERE book_type_code = p_book_type_code
1148         AND  asset_id = c_asset_id
1149         AND distribution_id = c_distribution_id
1150         AND period_counter_created = p_period_counter
1151         AND source_type_code = c_adjustment_source_type_code
1152         AND adjustment_type = c_adjustment_type;
1153         --AND transaction_header_id = nvl(c_transaction_header_id,transaction_header_id);
1154 
1155         CURSOR c_get_expense_ccid ( c_asset_id NUMBER,
1156                                     c_distribution_id NUMBER,
1157                                     c_transaction_header_id NUMBER) IS
1158         SELECT code_combination_id
1159         FROM fa_distribution_history
1160         WHERE book_type_code = p_book_type_code
1161         AND  asset_id = c_asset_id
1162         AND distribution_id = c_distribution_id
1163         AND transaction_header_id_in  = nvl(c_transaction_header_id,transaction_header_id_in);
1164 
1165         CURSOR c_get_accounts (c_distribution_id NUMBER) IS
1166         SELECT  nvl(ASSET_COST_ACCOUNT_CCID, -1),
1167                 nvl(DEPRN_EXPENSE_ACCOUNT_CCID, -1),
1168                 nvl(DEPRN_RESERVE_ACCOUNT_CCID, -1),
1169                 bc.accounting_flex_structure
1170         FROM    FA_DISTRIBUTION_ACCOUNTS da,
1171                     FA_BOOK_CONTROLS bc
1172         WHERE  bc.book_type_code = p_book_type_code
1173         AND      da.book_type_code = bc.book_type_code
1174         AND      da.distribution_id = c_distribution_id;
1175 
1176         CURSOR c_get_account_ccid ( c_asset_id NUMBER,
1177                                         c_distribution_id NUMBER,
1178                                         c_adjustment_source_type_code VARCHAR2,
1179                                         c_adjustment_type   VARCHAR2,
1180                                         c_transaction_header_id NUMBER) IS
1181         SELECT code_combination_id
1182         FROM fa_adjustments
1183         WHERE book_type_code = p_book_type_code
1184         AND  asset_id = c_asset_id
1185         AND distribution_id = c_distribution_id
1186         AND adjustment_type = c_adjustment_type;
1187 
1188         l_account_ccid NUMBER;
1189         l_adjustment_type VARCHAR2(50);
1190         l_rowid rowid;
1191         l_cost_ccid NUMBER;
1192         l_expense_ccid NUMBER;
1193         l_reserve_ccid NUMBER;
1194         l_flex_num NUMBER;
1195         l_account_type VARCHAR2(100);
1196         l_result BOOLEAN;
1197         l_asset_cost_acct VARCHAR2(25);
1198 		l_dep_exp_acct VARCHAR2(25);
1199 		l_dep_res_acct VARCHAR2(25);
1200 		l_asset_cost_account_ccid NUMBER;
1201 		l_reserve_account_ccid NUMBER;
1202         l_default_ccid NUMBER;
1203         l_category_id NUMBER;
1204         l_path		 VARCHAR2(100);
1205         l_validation_date date;
1206 	l_account_seg_val VARCHAR2(25);
1207 	l_acct_ccid NUMBER;
1208 
1209         -- bulk fecthes
1210         TYPE rowed_type_tbl_type   IS TABLE OF ROWID  INDEX BY BINARY_INTEGER;
1211         TYPE adj_id_tbl_type IS TABLE OF  IGI_IAC_ADJUSTMENTS. ADJUSTMENT_ID%TYPE
1212                   INDEX BY BINARY_INTEGER;
1213         TYPE book_type_tbl_type IS TABLE OF   IGI_IAC_ADJUSTMENTS.BOOK_TYPE_CODE%TYPE
1214               INDEX BY BINARY_INTEGER;
1215         TYPE code_comb_tbl_type IS TABLE OF IGI_IAC_ADJUSTMENTS.CODE_COMBINATION_ID%TYPE
1216               INDEX BY BINARY_INTEGER;
1217         TYPE adjustment_type_tbl_type IS TABLE OF  IGI_IAC_ADJUSTMENTS. ADJUSTMENT_TYPE%TYPE
1218               INDEX BY BINARY_INTEGER;
1219         TYPE asset_id_tbl_type IS TABLE OF   IGI_IAC_ADJUSTMENTS.ASSET_ID%TYPE
1220               INDEX BY BINARY_INTEGER;
1221         TYPE dist_id_tbl_type IS TABLE OF IGI_IAC_ADJUSTMENTS.DISTRIBUTION_ID%TYPE
1222               INDEX BY BINARY_INTEGER;
1223         TYPE period_counter_tbl_type IS TABLE OF IGI_IAC_ADJUSTMENTS.PERIOD_COUNTER%TYPE
1224               INDEX BY BINARY_INTEGER;
1225 
1226         l_row_id rowed_type_tbl_type;
1227         l_adj_id adj_id_tbl_type;
1228         l_book_code book_type_tbl_type;
1229         l_code_comb_id code_comb_tbl_type;
1230         l_adj_type adjustment_type_tbl_type;
1231         l_asset_id asset_id_tbl_type;
1232         l_dist_id dist_id_tbl_type;
1233         l_period_ctr period_counter_tbl_type;
1234         l_loop_count                 number;
1235         -- Bug 4714606
1236         l_dist_ccid number;
1237         -- Bug 4714606
1238 
1239     BEGIN
1240         l_path	:= g_path||'Synchronize_Accounts';
1241         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,' Start of processing for synchronize accounts ');
1242         FOR l_get_transaction IN c_get_transaction LOOP
1243             --get the  adjustments in the current period
1244             --FOR l_adjustment IN c_get_adjustments(l_get_transaction.adjustment_id) LOOP
1245 
1246             OPEN c_get_adjustments(l_get_transaction.adjustment_id);
1247             FETCH c_get_adjustments   BULK COLLECT INTO
1248                 l_row_id,
1249                 l_adj_id,
1250                 l_book_code,
1251                 l_code_comb_id,
1252                 l_adj_type,
1253                 l_asset_id,
1254                 l_dist_id,
1255                 l_period_ctr;
1256             CLOSE c_get_adjustments;
1257 
1258             FOR l_loop_count IN 1.. l_adj_id.count
1259             LOOP
1260 
1261                 -- Bug 4714606
1262                 l_dist_ccid := l_code_comb_id(l_loop_count);
1263                 -- Bug 4714606
1264                 l_rowid := l_row_id(l_loop_count);
1265                 l_account_ccid := -1;
1266 
1267                 -- fecth the required accounts form the fa_dsitribution accounts for the
1268                 --expense,cost and reserve
1269 
1270                 OPEN c_get_accounts(l_dist_id(l_loop_count));
1271                 FETCH c_get_accounts INTO
1272                         l_cost_ccid,
1273                         l_expense_ccid,
1274                         l_reserve_ccid,
1275                         l_flex_num;
1276                 IF (c_get_accounts%FOUND) THEN
1277                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     Success in  get account ccid  from distribution  accounts ');
1278                     IF (l_adj_type(l_loop_count) = 'COST') THEN
1279                         l_account_ccid := l_cost_ccid;
1280                     ELSIF (l_adj_type(l_loop_count) = 'RESERVE') THEN
1281                         l_account_ccid := l_reserve_ccid;
1282                     ELSIF (l_adj_type(l_loop_count) = 'EXPENSE') THEN
1283                          l_account_ccid := l_expense_ccid;
1284                     END IF;
1285                 END IF;
1286                 CLOSE c_get_accounts;
1287                 --- get the account from the fa_adjustmemts and fa_distribution_history if not found im
1288                 -- fa_distribution_accounts.
1289 
1290                 IF (l_account_ccid = -1)  THEN
1291                     igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'Could not get account ccid  from distribution  accounts  ');
1292 /*                    IF (l_adj_type(l_loop_count) in ('COST','RESERVE'  ) ) THEN
1293                         OPEN c_get_cost_reserve_ccid(l_asset_id(l_loop_count),l_dist_id(l_loop_count),
1294                                                             l_get_transaction.transaction_type_code ,
1295                                                             l_adj_type(l_loop_count),
1296                                                             l_get_transaction.transaction_header_id);
1297                         FETCH c_get_cost_reserve_ccid into l_account_ccid;
1298                         IF c_get_cost_reserve_ccid%NOTFOUND THEN
1299                             igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'     Failed to get  COST/RESERVE ccid  in synchronize accounts *****');
1300                         END IF;
1301                         CLOSE  c_get_cost_reserve_ccid;
1302                     ELSIF (l_adj_type(l_loop_count) ='EXPENSE' ) THEN
1303                         OPEN c_get_expense_ccid(l_asset_id(l_loop_count),l_dist_id(l_loop_count),
1304                                                             l_get_transaction.transaction_header_id);
1305                         FETCH c_get_expense_ccid into l_account_ccid;
1306                         IF c_get_expense_ccid%NOTFOUND THEN
1307                             igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'     Failed to get  EXPENSE ccid  in synchronize accounts *****');
1308                         END IF;
1309                         CLOSE c_get_expense_ccid;
1310                     END IF;
1311 */
1312 
1313 
1314                     OPEN c_get_account_ccid(l_asset_id(l_loop_count),l_dist_id(l_loop_count),
1315                                                             l_get_transaction.transaction_type_code ,
1316                                                             l_adj_type(l_loop_count),
1317                                                             l_get_transaction.transaction_header_id);
1318                     FETCH c_get_account_ccid into l_account_ccid;
1319                     IF c_get_account_ccid%NOTFOUND THEN
1320                         igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'Could not get  '||l_adj_type(l_loop_count)|| 'ccid  from  fa_adjustments');
1321                         l_account_ccid := -1;
1322                     END IF;
1323                     CLOSE  c_get_account_ccid;
1324 
1325                 END IF;
1326 
1327                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'   asset_id' || l_asset_id(l_loop_count));
1328                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'   distribution' ||  l_dist_id(l_loop_count));
1329                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'   adjustment type ' ||l_adj_type(l_loop_count));
1330                 -- get the account ccid for the adjustment
1331                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'   account ccid ' || l_account_ccid);
1332                 igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     fetched ccid '|| l_code_comb_id(l_loop_count));
1333 
1334                 IF l_account_ccid = -1 THEN
1335 
1336                    -- IF the accounts are not found
1337 		   -- generate them using FA workflow
1338 		   -- get the category ID for the asset
1339 
1340 		   SELECT a.category_id
1341 		   INTO  l_category_id
1342 		   FROM fa_asset_history a
1343                        ,fa_distribution_history d
1344                    WHERE d.distribution_id =   l_dist_id(l_loop_count)
1345                    AND a.asset_id = d.asset_id
1346                    AND d.date_effective >= a.date_effective
1347                    AND d.date_effective < nvl(a.date_ineffective,sysdate);
1348 
1349 		   -- Get the default accounts and ccids for a distributions
1350 
1351 		   SELECT asset_cost_acct, deprn_expense_acct, deprn_reserve_acct,
1352 		          asset_cost_account_ccid, reserve_account_ccid
1353 		   INTO l_asset_cost_acct, l_dep_exp_acct, l_dep_res_acct,
1354 		        l_asset_cost_account_ccid ,l_reserve_account_ccid
1355  	  	   FROM fa_category_books
1356 		   WHERE book_type_code = p_book_type_code
1357   		   AND category_id = l_category_id;
1358 
1359 		   -- get the flex_num and default CCID
1360 
1361 		   SELECT accounting_flex_structure, flexbuilder_defaults_ccid
1362 		   into l_flex_num, l_default_ccid
1363     		   FROM fa_book_controls
1364                    WHERE book_type_code =  p_book_type_code ;
1365 
1366                    IF (l_adj_type(l_loop_count) = 'COST') THEN
1367                       -- get the COST
1368  		      l_account_type := 'ASSET_COST';
1369  		      l_account_seg_val := l_asset_cost_acct;
1370  		      l_acct_ccid := l_asset_cost_account_ccid;
1371                    ELSIF (l_adj_type(l_loop_count) ='RESERVE' ) THEN
1372                       --  get the reserve account
1373    		      l_account_type := 'DEPRN_RSV';
1374 		      l_account_seg_val := l_dep_res_acct;
1375 		      l_acct_ccid := l_reserve_account_ccid ;
1376                    ELSIF (l_adj_type(l_loop_count) ='EXPENSE' ) THEN
1377 	  	      -- get the expense account
1378 		      l_account_type :=	'DEPRN_EXP' ;
1379 		      l_account_seg_val := l_dep_exp_acct;
1380 		      l_acct_ccid := l_code_comb_id(l_loop_count);
1381                       -- Bug 4714606
1382                       OPEN c_get_expense_ccid(l_asset_id(l_loop_count),
1383                                        l_dist_id(l_loop_count),
1384                                        l_get_transaction.transaction_header_id);
1385                       FETCH c_get_expense_ccid into l_dist_ccid;
1386                       IF c_get_expense_ccid%NOTFOUND THEN
1387                             igi_iac_debug_pkg.debug_other_string(g_error_level,l_path,'     Failed to get  EXPENSE ccid  in synchronize accounts *****');
1388 		         l_dist_ccid := l_code_comb_id(l_loop_count);
1389                       END IF;
1390                       CLOSE c_get_expense_ccid;
1391                       -- Bug 4714606
1392                    END IF;
1393 
1394                    Select calendar_period_close_date
1395                    into l_validation_date
1396                    From fa_deprn_periods
1397                    where book_type_code = p_book_type_code
1398                    and period_counter = p_period_counter;
1399 
1400 		   l_result := FAFLEX_PKG_WF.START_PROCESS(
1401                                 X_flex_account_type => l_account_type,
1402                                 X_book_type_code    => p_book_type_code,
1403                                 X_flex_num          => l_flex_num,
1404                 -- Bug 4714606
1405                                 X_dist_ccid         => l_dist_ccid,
1406                 -- Bug 4714606
1407                                 X_acct_segval       => l_account_seg_val,
1408                                 X_default_ccid      => l_default_ccid,
1409                                 X_account_ccid      => l_acct_ccid,
1410                                 X_distribution_id   => l_dist_id(l_loop_count),
1411                                 X_validation_date   => l_validation_date,
1412                                 X_return_ccid       => l_account_ccid);
1413                 END IF;
1414 
1415 
1416                 IF l_account_ccid = -1 THEN
1417                    FND_MESSAGE.SET_NAME('IGI', 'IGI_IAC_ACCOUNT_NOT_FOUND');
1418                    FND_MESSAGE.SET_TOKEN('PROCESS','Depreciation',TRUE);
1419                    igi_iac_debug_pkg.debug_other_msg(p_level => g_error_level,
1420                                     p_full_path => l_path,
1421                                     p_remove_from_stack => FALSE);
1422                    fnd_file.put_line(fnd_file.log, fnd_message.get);
1423 
1424                    return FALSE;
1425                 END IF;
1426                 IF l_account_ccid <>   (l_code_comb_id(l_loop_count))  THEN
1427                     -- Update the ccid for the adjustment
1428                     UPDATE igi_iac_adjustments
1429                     SET code_combination_id= l_account_ccid
1430                     WHERE rowid=l_rowid;
1431 
1432                     igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'       Updated the adjusment with correct ccid' );
1433                 END IF;
1434 
1435             END LOOP;
1436         END LOOP;
1437         return TRUE;
1438 
1439     EXCEPTION
1440         WHEN others THEN
1441         igi_iac_debug_pkg.debug_unexpected_msg(l_path);
1442         return FALSE;
1443     END Synchronize_Accounts;
1444 
1445 
1446 /*=========================================================================+
1447  | Function Name:                                                          |
1448  |    Do_Depreciation                                                	   |
1449  |                                                                         |
1450  | Description:                                                            |
1451  |    This is IAC Depreciation function. Called from 		 	   |
1452  |    fa_igi_ext_pkg.Do_Depreciation                			   |
1453  |                                                                         |
1454  +=========================================================================*/
1455     FUNCTION Do_Depreciation(
1456         p_book_type_code	in varchar2 ,
1457         p_period_counter	in number ,
1458         p_calling_function  in varchar2
1459     ) RETURN boolean IS
1460         l_path 	 VARCHAR2(100);
1461     BEGIN
1462         l_path 	 := g_path||'Do_Depreciation';
1463         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'*****************************************************');
1464         igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,' Start of IAC Depreciation Processing');
1465         IF NOT igi_iac_common_utils.is_iac_book(p_book_type_code) THEN
1466             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'     The book is not an IAC book');
1467             RETURN true;
1468         ELSE
1469             IF NOT Synchronize_Calendars(p_book_type_code) THEN
1470                 RETURN false;
1471             END IF;
1472 
1473             /* Bug 2906034 vgadde 25/04/2003 Start(2) */
1474             /*IF NOT Process_non_Deprn_Assets(p_book_type_code, 'DEPRECIATION',p_event_id) THEN
1475             	return FALSE;
1476             END IF;*/
1477             /* in 11i we need this explicit call for non depreciating assets,
1478                but in R12 we no longer need this explicit call as we are calling do_prior_additions() */
1479             /* Bug 2906034 vgadde 25/04/2003 End(2) */
1480 
1481             /* Added for Adjustments processing  Sekhar */
1482             IF NOT igi_iac_adj_pkg.do_process_adjustments ( p_book_type_code,
1483                                                          p_period_counter,
1484                                                          'DEPRECIATION') THEN
1485                 RETURN false;
1486             END IF;
1487             /* Added for Adjustments processing  Sekhar */
1488 
1489             IF NOT Periodic_Reval_of_Deprn( p_book_type_code, p_period_counter ) THEN
1490                 RETURN false;
1491             END IF;
1492 
1493             IF NOT Synchronize_Accounts(
1494                             p_book_type_code   => p_book_type_code,
1495                             p_period_counter    => p_period_counter,
1496                             p_calling_function  =>  'ADDITION'
1497                             ) THEN
1498                 RETURN false;
1499             END IF;
1500 
1501             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,' Successful completion of IAC Depreciation Processing');
1502             igi_iac_debug_pkg.debug_other_string(g_state_level,l_path,'******************************************************');
1503             RETURN true;
1504         END IF;
1505 
1506     EXCEPTION
1507         WHEN OTHERS THEN
1508             igi_iac_debug_pkg.debug_unexpected_msg(l_path);
1509             RETURN false;
1510     END Do_Depreciation;
1511 BEGIN
1512     --===========================FND_LOG.START=====================================
1513 
1514     g_state_level :=	FND_LOG.LEVEL_STATEMENT;
1515     g_proc_level  :=	FND_LOG.LEVEL_PROCEDURE;
1516     g_event_level :=	FND_LOG.LEVEL_EVENT;
1517     g_excep_level :=	FND_LOG.LEVEL_EXCEPTION;
1518     g_error_level :=	FND_LOG.LEVEL_ERROR;
1519     g_unexp_level :=	FND_LOG.LEVEL_UNEXPECTED;
1520     g_path        := 'IGI.PLSQL.igiiaprb.IGI_IAC_DEPRN_PKG.';
1521 
1522     --===========================FND_LOG.END=======================================
1523 
1524 END igi_iac_deprn_pkg; -- Package body