DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_RETIREMENT_PUB

Source


1 PACKAGE BODY FA_RETIREMENT_PUB as
2 /* $Header: FAPRETB.pls 120.78.12010000.2 2008/07/31 06:49:38 sbhaskar ship $   */
3 
4 -- API info
5 G_PKG_NAME      CONSTANT   varchar2(30) := 'FA_RETIREMENT_PUB';
6 G_API_NAME      CONSTANT   varchar2(30) := 'Retirement API';
7 G_API_VERSION   CONSTANT   number       := 1.0;
8 
9 --l_calling_fn    VARCHAR2(100) := 'FA_RETIREMENT_PUB';
10 
11 g_retirement         VARCHAR2(30) := 'RETIREMENT';
12 g_reinstatement      VARCHAR2(30) := 'REINSTATEMENT';
13 g_undo_retirement    VARCHAR2(30) := 'UNDO RETIREMENT';
14 g_undo_reinstatement VARCHAR2(30) := 'UNDO REINSTATEMENT';
15 
16 -- global variables used to go back to original env.
17 g_orig_set_of_books_id   number;
18 g_orig_currency_context  varchar2(64);
19 
20 g_primary_set_of_books_id number; /* BUG#2919562 */
21 
22 -- error message
23 g_msg_name               varchar2(80) := null;
24 g_token1                 varchar2(80) := null;
25 g_token2                 varchar2(80) := null;
26 g_value1                 varchar2(80) := null;
27 g_value2                 varchar2(80) := null;
28 
29 /*
30  * Added for Group Asset uptake
31  * g_inv_trans_rec to pass FA_API_TYPES.inv_trans_rec
32  * when calling FA_GROUP_RETIREMENT_PVT.DO_RETIREMENT
33  * This is necessary without passing this value from
34  * Do_Retirement to do_sub_regular_retirement
35  */
36 g_inv_trans_rec           FA_API_TYPES.inv_trans_rec_type;
37 /*** End uptake ***/
38 
39 g_log_level_rec            fa_api_types.log_level_rec_type;
40 
41 FUNCTION do_all_books_retirement
42         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
43         ,px_dist_trans_rec            in out NOCOPY FA_API_TYPES.trans_rec_type
44         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
45         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
46         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
47         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
48         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
49         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
50         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
51         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
52         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
53         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
54 
55 FUNCTION do_sub_retirement
56         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
57         ,px_dist_trans_rec            in out NOCOPY FA_API_TYPES.trans_rec_type
58         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
59         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
60         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
61         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
62         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
63         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
64         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
65         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
66         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
67         ,p_mrc_sob_type_code          in     VARCHAR2
68         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
69 
70 FUNCTION do_sub_regular_retirement
71         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
72         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
73         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
74         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
75         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
76         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
77         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
78         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
79         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
80         ,p_mrc_sob_type_code          in     VARCHAR2
81         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
82 
83 FUNCTION calculate_gain_loss
84         (p_retirement_id              in     number
85         ,p_mrc_sob_type_code          in     varchar2
86         ,p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
87 
88 FUNCTION do_all_books_reinstatement
89         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
90         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
91         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
92         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
93         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
94         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
95         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
96         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
97         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
98         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
99         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
100 
101 FUNCTION do_sub_reinstatement
102         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
103         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
104         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
105         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
106         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
107         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
108         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
109         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
110         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
111         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
112         ,p_rate                       in     number
113         ,p_mrc_sob_type_code          in     VARCHAR2
114         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
115 
116 FUNCTION do_sub_regular_reinstatement
117         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
118         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
119         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
120         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
121         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
122         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
123         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
124         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
125         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
126         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
127         ,p_mrc_sob_type_code          in     VARCHAR2
128         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
129 
130 FUNCTION undo_all_books_retirement
131         (p_trans_rec                  in     FA_API_TYPES.trans_rec_type
132         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
133         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
134         ,p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
135 
136 FUNCTION undo_sub_retirement
137    (p_trans_rec                  in     FA_API_TYPES.trans_rec_type
138    ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
139    ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
140    ,p_mrc_sob_type_code          in     VARCHAR2
141    ,p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
142 
143 FUNCTION undo_all_books_reinstatement
144    (p_trans_rec                  in     FA_API_TYPES.trans_rec_type
145    ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
146    ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
147    ,p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
148 
149 FUNCTION undo_sub_reinstatement
150    (p_trans_rec                  in     FA_API_TYPES.trans_rec_type
151    ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
152    ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
153    ,p_mrc_sob_type_code          in     VARCHAR2
154    ,p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
155 
156 FUNCTION do_validation
157         (p_validation_type            in     varchar2
158         ,p_trans_rec                  in     FA_API_TYPES.trans_rec_type
159         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
160         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
161         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
162         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
163         ,p_asset_retire_rec           in     FA_API_TYPES.asset_retire_rec_type
164         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
165         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
166         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
167         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
168         ,p_calling_fn                 in     varchar2
169         ,p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
170 
171 FUNCTION reinstate_src_line(
172                   px_trans_rec             IN OUT NOCOPY FA_API_TYPES.trans_rec_type,
173                   px_asset_hdr_rec         IN OUT NOCOPY FA_API_TYPES.asset_hdr_rec_type,
174                   px_asset_fin_rec         IN OUT NOCOPY FA_API_TYPES.asset_fin_rec_type,
175                   p_asset_desc_rec         IN     FA_API_TYPES.asset_desc_rec_type,
176                   p_invoice_transaction_id IN     NUMBER,
177                   p_inv_tbl                IN     FA_API_TYPES.inv_tbl_type,
178                   p_rowid                  IN     ROWID,
179                   p_calling_fn             IN     VARCHAR2,
180                   p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN;
181 
182 ------------------------------------------------------------------------------
183 PROCEDURE do_retirement
184    (p_api_version                in     NUMBER
185    ,p_init_msg_list              in     VARCHAR2 := FND_API.G_FALSE
186    ,p_commit                     in     VARCHAR2 := FND_API.G_FALSE
187    ,p_validation_level           in     NUMBER   := FND_API.G_VALID_LEVEL_FULL
188    ,p_calling_fn                 in     VARCHAR2
189    ,x_return_status              out    NOCOPY VARCHAR2
190    ,x_msg_count                  out    NOCOPY NUMBER
191    ,x_msg_data                   out    NOCOPY VARCHAR2
192 
193    ,px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
194    ,px_dist_trans_rec            in out NOCOPY FA_API_TYPES.trans_rec_type
195    ,px_asset_hdr_rec             in out NOCOPY FA_API_TYPES.asset_hdr_rec_type
196    ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
197    ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
198    ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
199    ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
200    ) IS
201 
202    -- local asset info
203    l_trans_rec        FA_API_TYPES.trans_rec_type;
204    l_dist_trans_rec   FA_API_TYPES.trans_rec_type;
205    l_asset_hdr_rec    FA_API_TYPES.asset_hdr_rec_type;
206    l_asset_desc_rec   FA_API_TYPES.asset_desc_rec_type;
207    l_asset_type_rec   FA_API_TYPES.asset_type_rec_type;
208    l_asset_fin_rec    FA_API_TYPES.asset_fin_rec_type;
209    l_asset_retire_rec FA_API_TYPES.asset_retire_rec_type;
210    l_asset_dist_tbl   FA_API_TYPES.asset_dist_tbl_type;
211    l_subcomp_tbl      FA_API_TYPES.subcomp_tbl_type;
212    l_inv_tbl          FA_API_TYPES.inv_tbl_type;
213    l_period_rec       FA_API_TYPES.period_rec_type;
214 
215    -- used for loop through tax books
216    l_tax_book_tbl     FA_CACHE_PKG.fazctbk_tbl_type;
217    l_tax_index        number;
218 
219    -- used for tax books when doing cip-in-tax or autocopy
220    lv_trans_rec      FA_API_TYPES.trans_rec_type;
221    lv_dist_trans_rec FA_API_TYPES.trans_rec_type;
222    lv_asset_hdr_rec  FA_API_TYPES.asset_hdr_rec_type;
223    lv_asset_retire_rec FA_API_TYPES.asset_retire_rec_type;
224 
225    -- local individual variables
226    l_transaction_type           varchar2(30) := null;
227    l_reporting_flag             varchar2(1);
228    l_retirement_id              number(15);
229    l_calculate_gain_loss_flag   varchar2(1);
230 
231 
232    /**
233    -- used for category defaulting
234    l_ret_prorate_convention     FA_CATEGORY_BOOK_DEFAULTS.prorate_convention_code%TYPE;
235    l_use_stl_retirements_flag   FA_CATEGORY_BOOK_DEFAULTS.use_stl_retirements_flag%TYPE;
236    l_stl_method_code    FA_CATEGORY_BOOK_DEFAULTS.stl_method_code%TYPE;
237    l_stl_life_in_months FA_CATEGORY_BOOK_DEFAULTS.stl_life_in_months%TYPE;
238    **/
239 
240    /*
241     * Added for Group Asset uptake
242     */
243    l_asset_cat_rec           FA_API_TYPES.asset_cat_rec_type;
244    l_new_asset_fin_rec       FA_API_TYPES.asset_fin_rec_type;
245    l_new_asset_fin_mrc_tbl   FA_API_TYPES.asset_fin_tbl_type;
246    l_asset_deprn_rec_new     FA_API_TYPES.asset_deprn_rec_type;
247    l_asset_deprn_mrc_tbl_new FA_API_TYPES.asset_deprn_tbl_type;
248    /*** End of uptake ***/
249 
250    -- used to store original sob info upon entry into api
251    l_orig_set_of_books_id    number;
252    l_orig_currency_context   varchar2(64);
253 
254    l_ins_status                 boolean := FALSE;
255 
256    l_calling_fn  varchar2(80) := 'FA_RETIREMENT_PUB.do_retirement';
257 
258 BEGIN
259 
260    SAVEPOINT do_retirement;
261 
262 
263    if (not g_log_level_rec.initialized) then
264       if (NOT fa_util_pub.get_log_level_rec (
265                 x_log_level_rec =>  g_log_level_rec
266       )) then
267          raise FND_API.G_EXC_UNEXPECTED_ERROR;
268       end if;
269    end if;
270 
271    -- ****************************************************
272    -- **  API compatibility check and initialization
273    -- ****************************************************
274 
275    -- check version of the API
276    -- standard call to check for API call compatibility.
277 
278    if not FND_API.compatible_api_call
279           (G_API_VERSION
280           ,p_api_version
281           ,G_API_NAME
282           ,G_PKG_NAME) then
283                          raise  FND_API.G_EXC_UNEXPECTED_ERROR;
284    end if;
285 
286    -- initialize message list if p_init_msg_list is set to TRUE.
287    if (FND_API.to_boolean(p_init_msg_list)) then
288         -- initialize error message stack.
289         fa_srvr_msg.init_server_message;
290 
291         -- initialize debug message stack.
292         fa_debug_pkg.initialize;
293    end if;
294 
295    -- override FA:PRINT_DEBUG profile option.
296    -- if (p_debug_flag = 'YES') then
297    --   fa_debug_pkg.set_debug_flag;
298    -- end if;
299 
300    -- ****************************************************
301    -- **  Assign input parameters to local rec/tbl types
302    -- ****************************************************
303    l_trans_rec        := px_trans_rec;
304    l_dist_trans_rec   := px_dist_trans_rec;
305    l_asset_hdr_rec    := px_asset_hdr_rec;
306    l_asset_retire_rec := px_asset_retire_rec;
307    l_asset_dist_tbl   := p_asset_dist_tbl;
308    l_subcomp_tbl      := p_subcomp_tbl;
309    l_inv_tbl          := p_inv_tbl;
310 
311    lv_asset_retire_rec := px_asset_retire_rec;
312 
313    l_calculate_gain_loss_flag := l_asset_retire_rec.calculate_gain_loss;
314 
315    -- ***************************
316    -- **  Transaction approval
317    -- ***************************
318    -- common for all types
319    -- bug 1230315... adding FA_TRX_APPROVAL_PKG for all retirement
320    -- transactions
321 
322    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'call trx approval pkg - faxcat', '', g_log_level_rec); end if;
323    l_ins_status := FA_TRX_APPROVAL_PKG.faxcat
324                    (x_book              => l_asset_hdr_rec.book_type_code
325                    ,x_asset_id          => l_asset_hdr_rec.asset_id
326                    ,x_trx_type          => 'PARTIAL UNIT RETIREMENT'
327                    ,x_trx_date          => l_asset_retire_rec.date_retired
328                    ,x_init_message_flag => 'NO'
329                    ,p_log_level_rec     => g_log_level_rec);
330    if not l_ins_status then
331         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'failed in FA_TRX_APPROVAL_PKG.faxcat', '', g_log_level_rec); end if;
332         -- g_msg_name := 'error msg name-after trx_app';
333         raise FND_API.G_EXC_ERROR;
334    end if;
335 
336    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'ret_id1 ', l_retirement_id, g_log_level_rec); end if;
337    -- ***********************************
338    -- **  Call the cache for book
339    -- **  and do initial MRC validation
340    -- ***********************************
341 
342    if l_asset_hdr_rec.book_type_code is not null then
343 
344         -- call the cache for the primary transaction book
345         if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
346                                   ,p_log_level_rec => g_log_level_rec) then
347              raise FND_API.G_EXC_UNEXPECTED_ERROR;
348         end if;
349 
350         -- l_book_class               := fa_cache_pkg.fazcbc_record.book_class;
351         -- l_set_of_books_id          := fa_cache_pkg.fazcbc_record.set_of_books_id;
352         -- l_distribution_source_book := fa_cache_pkg.fazcbc_record.distribution_source_book;
353         -- l_mc_source_flag           := fa_cache_pkg.fazcbc_record.mc_source_flag;
354 
355         l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
356 
357         fnd_profile.get('GL_SET_OF_BKS_ID',g_orig_set_of_books_id);
358         g_orig_currency_context :=  substrb(userenv('CLIENT_INFO'),45,10);
359 
360         -- get the book type code P,R or N
361         if not fa_cache_pkg.fazcsob
362                (x_set_of_books_id   => l_asset_hdr_rec.set_of_books_id
363                ,x_mrc_sob_type_code => l_reporting_flag
364                ,p_log_level_rec     => g_log_level_rec)
365                then
366                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
367         end if;
368 
369         -- Error out if the program is submitted from the Reporting Responsibility
370         -- No transaction permitted directly on reporting books.
371         if l_reporting_flag = 'R' then
372              FND_MESSAGE.set_name('GL','MRC_OSP_INVALID_BOOK_TYPE');
373              FND_FILE.PUT_LINE(fnd_file.log,fnd_message.get);
374              raise FND_API.G_EXC_UNEXPECTED_ERROR;
375         end if;
376 
377 
378    end if; -- book_type_code
379 
380    -- *********************************
381    -- **  Populate local record types
382    -- *********************************
383    -- populate rec_types that were not provided by users
384 
385    -- set the sob_id and currency context
386    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
387    fnd_client_info.set_currency_context (to_char(l_asset_hdr_rec.set_of_books_id));
388 
389    -- BUG# 2376085, 2487707
390    -- store the sob_id value for later use.
391    fnd_profile.get ('GL_SET_OF_BKS_ID',l_orig_set_of_books_id);
392    l_orig_currency_context :=  SUBSTRB(USERENV('CLIENT_INFO'),45,10);
393 
394    -- pop asset_desc_rec
395    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_desc_rec', '', g_log_level_rec); end if;
396    if not FA_UTIL_PVT.get_asset_desc_rec
397           (p_asset_hdr_rec      => l_asset_hdr_rec
398           ,px_asset_desc_rec    => l_asset_desc_rec
399           ,p_log_level_rec     => g_log_level_rec) then
400               raise FND_API.G_EXC_UNEXPECTED_ERROR;
401    end if;
402 
403    -- pop asset_type_rec
404    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_type_rec', '', g_log_level_rec); end if;
405    if not FA_UTIL_PVT.get_asset_type_rec
406           (p_asset_hdr_rec      => l_asset_hdr_rec
407           ,px_asset_type_rec    => l_asset_type_rec
408           ,p_date_effective     => NULL
409           ,p_log_level_rec     => g_log_level_rec) then
410               raise FND_API.G_EXC_UNEXPECTED_ERROR;
411    end if;
412 
413    -- pop asset_fin_rec
414    -- get fa_books row where transaction_header_id_out is null
415    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_fin_rec', '', g_log_level_rec); end if;
416    if not FA_UTIL_PVT.get_asset_fin_rec
417           (p_asset_hdr_rec         => l_asset_hdr_rec
418           ,px_asset_fin_rec        => l_asset_fin_rec
419           ,p_transaction_header_id => NULL
420           ,p_mrc_sob_type_code  => 'P'
421           ,p_log_level_rec     => g_log_level_rec) then
422               raise FND_API.G_EXC_UNEXPECTED_ERROR;
423    end if;
424 
425    -- pop current period_rec info
426    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop period_rec', '', g_log_level_rec); end if;
427    if not FA_UTIL_PVT.get_period_rec
428           (p_book           => l_asset_hdr_rec.book_type_code
429           ,x_period_rec     => l_period_rec
430           ,p_log_level_rec     => g_log_level_rec) then
431               raise FND_API.G_EXC_UNEXPECTED_ERROR;
432    end if;
433 
434 
435    -- get the current units of the asset from fa_asset_history table
436    -- ? call the following get_current_units function
437    -- to make sure that the current_units
438    -- of asset_desc_rec is correct.
439    -- => this call shouldn't be necessary
440    --    if asset_desc_rec.current_units always reflects current units.
441    if not FA_UTIL_PVT.get_current_units
442           (p_calling_fn    => l_calling_fn
443           ,p_asset_id      => l_asset_hdr_rec.asset_id
444           ,x_current_units => l_asset_desc_rec.current_units
445           ,p_log_level_rec     => g_log_level_rec) then
446               raise FND_API.G_EXC_UNEXPECTED_ERROR;
447    end if;
448 
449    -- *********************************************
450    -- **  Set default values unless provided
451    -- *********************************************
452    -- set default values unless provided
453    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'set default values unless provided', '', g_log_level_rec); end if;
454 
455    -- default date_retired to the current period if it is null
456    if l_asset_retire_rec.date_retired is null then
457 
458         -- default date_retired to the current period
459         l_asset_retire_rec.date_retired
460           := greatest(least(l_period_rec.calendar_period_close_date, sysdate)
461                      ,l_period_rec.calendar_period_open_date
462                      );
463    end if;
464 
465    -- BUG# 3549470
466    -- remove time stamps
467 
468    l_asset_retire_rec.date_retired :=
469        to_date(to_char(l_asset_retire_rec.date_retired, 'DD/MM/YYYY'),'DD/MM/YYYY');
470 
471    -- set transaction_date_entered
472    if l_asset_retire_rec.date_retired
473       <> l_trans_rec.transaction_date_entered
474       or l_trans_rec.transaction_date_entered is null then
475         l_trans_rec.transaction_date_entered := l_asset_retire_rec.date_retired;
476    end if;
477 
478 
479 /****
480   *** BUG#4663092: Move this book-specific validation to do_all_retirement level.
481 
482    -- default retirement prorate convention if not provided.
483 
484    if l_asset_retire_rec.retirement_prorate_convention is null then
485      SELECT retirement_prorate_convention,
486              use_stl_retirements_flag,
487              stl_method_code,
488              stl_life_in_months
489      INTO
490            l_ret_prorate_convention,
491            l_use_stl_retirements_flag,
492            l_stl_method_code,
493            l_stl_life_in_months
494      FROM       fa_category_book_defaults, fa_additions_b a
495      WHERE      book_type_code = l_asset_hdr_rec.book_type_code
496      and        category_id = a.asset_category_id
497      and        a.asset_id  = l_asset_hdr_rec.asset_id
498      and        l_asset_fin_rec.Date_Placed_In_Service between start_dpis and
499                      nvl(end_dpis,l_asset_fin_rec.Date_Placed_In_Service);
500 
501      l_asset_retire_rec.retirement_prorate_convention := l_ret_prorate_convention;
502      --
503      if (l_Use_STL_Retirements_Flag = 'NO') then
504         l_asset_retire_rec.detail_info.stl_method_code := NULL;
505         l_asset_retire_rec.detail_info.stl_life_in_months := NULL;
506      else
507         if (l_asset_retire_rec.detail_info.stl_method_code is null) then
508            l_asset_retire_rec.detail_info.stl_method_code :=
509                                                 l_stl_method_code;
510         end if;
511         if (l_asset_retire_rec.detail_info.stl_life_in_months is null) then
512            l_asset_retire_rec.detail_info.stl_life_in_months :=
513                                                 l_stl_life_in_months;
514         end if;
515      end if;
516    end if;
517    ***
518    */
519 
520 
521    /*
522     * Added for Group Asset uptake
523     */
524 
525 -- source line retirement now available for core FA too.
526      /*
527       * Call Invoice API to populate cost_retired in case of
528       * source line retirement.
529       */
530      if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'l_inv_tbl.last: ', to_char(l_inv_tbl.last), g_log_level_rec); end if;
531 
532      if (nvl(l_inv_tbl.last, 0) > 0) then
533 
534        -- Populate asset_cat_rec
535        if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_cat_rec', '', g_log_level_rec); end if;
536        if not FA_UTIL_PVT.get_asset_cat_rec (
537                                p_asset_hdr_rec  => l_asset_hdr_rec,
538                                px_asset_cat_rec => l_asset_cat_rec,
539                                p_date_effective  => NULL,
540                                p_log_level_rec     => g_log_level_rec) then
541          raise FND_API.G_EXC_UNEXPECTED_ERROR;
542        end if;
543 
544        -- Initialize g_inv_trans_rec global variable
545        g_inv_trans_rec.invoice_transaction_id := to_number(null);
546        g_inv_trans_rec.transaction_type := 'RETIREMENT';
547 
548        if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Before calling FA_INVOICE_PVT.INVOICE_ENGINE', '', g_log_level_rec); end if;
549        if not FA_INVOICE_PVT.INVOICE_ENGINE (
550                                px_trans_rec              => l_trans_rec,
551                                px_asset_hdr_rec          => l_asset_hdr_rec,
552                                p_asset_desc_rec          => l_asset_desc_rec,
553                                p_asset_type_rec          => l_asset_type_rec,
554                                p_asset_cat_rec           => l_asset_cat_rec,
555                                p_asset_fin_rec_adj       => l_asset_fin_rec,
556                                x_asset_fin_rec_new       => l_new_asset_fin_rec,
557                                x_asset_fin_mrc_tbl_new   => l_new_asset_fin_mrc_tbl,
558                                px_inv_trans_rec          => g_inv_trans_rec,
559                                px_inv_tbl                => l_inv_tbl,
560                                x_asset_deprn_rec_new     => l_asset_deprn_rec_new,
561                                x_asset_deprn_mrc_tbl_new => l_asset_deprn_mrc_tbl_new,
562                                p_calling_fn              => l_calling_fn,
563                                p_log_level_rec           => g_log_level_rec) then
564          if g_log_level_rec.statement_level then
565             fa_debug_pkg.add(l_calling_fn, 'Error Calling FA_INVOICE_PVT.INVOICE_ENGINE', '', g_log_level_rec);
566             fa_debug_pkg.add(l_calling_fn, 'SQLERRM: ', SQLERRM, g_log_level_rec);
567          end if;
568          raise FND_API.G_EXC_UNEXPECTED_ERROR;
569        end if;
570 
571        if (g_log_level_rec.statement_level) then
572              FA_DEBUG_PKG.ADD (fname=>'FAPRETB.pls',
573                 element=>'cost_retired',
574                value=> l_asset_retire_rec.cost_retired
575                ,p_log_level_rec => g_log_level_rec);
576              FA_DEBUG_PKG.ADD (fname=>'do_all_book_retirement',
577                 element=>'cost',
578                value=> l_asset_fin_rec.cost
579                ,p_log_level_rec => g_log_level_rec);
580        end if;
581 
582 -- fixed_assets_cost is obviously passed in as negative value for src line ret.
583        l_asset_retire_rec.cost_retired := nvl(l_new_asset_fin_rec.cost, 0) * - 1;
584 
585 
586 /*
587        dbms_output.put_line('l_asset_retire_rec.cost_retired: '||
588                             to_char(l_asset_retire_rec.cost_retired));
589        dbms_output.put_line('l_asset_fin_rec.cost: '||to_char(l_new_asset_fin_rec.cost));
590 
591        dbms_output.put_line('l_inv_tbl.last: '||to_char(l_inv_tbl.last));
592 
593        for i in 1..nvl(l_inv_tbl.last, 0) loop
594 
595          dbms_output.put_line('l_inv_tbl('||to_char(i)||
596                               ').fixed_assets_cost: '||to_char(l_inv_tbl(i).fixed_assets_cost));
597          l_asset_retire_rec.cost_retired := l_asset_retire_rec.cost_retired -
598                                             nvl(l_inv_tbl(i).fixed_assets_cost, 0);
599        end loop;
600 */
601 
602      end if; -- (nvl(l_inv_tbl.last, 0) > 0 )
603 
604    /*** End of uptake ***/
605 
606    -- *********************************************
607    -- **  Do validation
608    -- *********************************************
609    -- validate that all user-entered input parameters are valid
610    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Before calling do_validation', '', g_log_level_rec); end if;
611    if not do_validation
612           (p_validation_type   => g_retirement
613           ,p_trans_rec         => l_trans_rec
614           ,p_asset_hdr_rec     => l_asset_hdr_rec
615           ,p_asset_desc_rec    => l_asset_desc_rec
616           ,p_asset_type_rec    => l_asset_type_rec
617           ,p_asset_fin_rec     => l_asset_fin_rec
618           ,p_asset_retire_rec  => l_asset_retire_rec
619           ,p_asset_dist_tbl    => l_asset_dist_tbl
620           ,p_subcomp_tbl       => l_subcomp_tbl
621           ,p_inv_tbl           => l_inv_tbl
622           ,p_period_rec        => l_period_rec
623           ,p_calling_fn        => p_calling_fn
624           ,p_log_level_rec     => g_log_level_rec) then
625               raise FND_API.G_EXC_UNEXPECTED_ERROR;
626    end if;
627 
628 
629    -- **********************************************
630    -- **  Do asset-level validation on transaction
631    -- **********************************************
632    -- begin asset-level validation on transaction
633 
634    -- check if trx is partial retirement on CIP asset
635    if g_log_level_rec.statement_level then
636        fa_debug_pkg.add(l_calling_fn, 'begin asset-level validation on transaction', '', g_log_level_rec);
637        fa_debug_pkg.add(l_calling_fn, 'check if trx is partial retirement on CIP asset', '', g_log_level_rec);
638    end if;
639    if l_asset_retire_rec.units_retired is not null then
640 
641         -- can only fully retire CIP assets unless source line retirement
642         if l_asset_type_rec.asset_type = 'CIP' and p_inv_tbl.count = 0
643            and l_asset_retire_rec.units_retired <> l_asset_desc_rec.current_units then
644              g_msg_name := 'FA_RET_WHOLE_CIP_ASSET';
645              raise FND_API.G_EXC_ERROR;
646         end if;
647 
648       else -- if units_retired is null
649 
650         -- can only fully retire CIP assets unless source line retirement
651         if l_asset_type_rec.asset_type = 'CIP' and p_inv_tbl.count = 0
652            and l_asset_retire_rec.cost_retired <> l_asset_fin_rec.cost then
653              g_msg_name := 'FA_RET_WHOLE_CIP_ASSET';
654              raise FND_API.G_EXC_ERROR;
655         end if;
656 
657    end if; -- partial retirement on CIP asset
658 
659    -- The following code was in the initialize part
660    -- of retirement in FAXASSET(i.e. fa_retire_pkg.initialize).
661    -- ? not sure of the following code - need more investigation
662    if l_asset_desc_rec.unit_adjustment_flag = 'YES' then
663         g_msg_name := 'FA_RET_CHANGE_UNITS_TFR_FORM';
664         raise FND_API.G_EXC_ERROR;
665    end if;
666 
667    -- FYI: We don't allow unit retirements for TAX book
668    -- probably because TAX book does not have its own distributions
669    -- in DH table.
670    if fa_cache_pkg.fazcbc_record.book_class = 'TAX'
671       and l_asset_retire_rec.units_retired is not null then
672         g_msg_name := 'FA_RET_NO_PART_UNIT_IN_TAX';
673          -- ? can not find this message name in msg table
674         raise FND_API.G_EXC_ERROR;
675    end if;
676 
677    -- check if any adjustment is pending
678    -- Users cannot retire the asset if an adjustment is pending
679    -- FA_RET_PENDING_ADJUSTMENT
680    -- ? Is this adj check really needed ? - No, not in do_retirement
681 
682 
683    -- ***************************
684    -- **  Do basic calculation
685    -- ***************************
686    -- begin retirement-specific calculation
687    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin calculation', '', g_log_level_rec); end if;
688 
689    if l_asset_retire_rec.units_retired is not null then
690 
691         -- derive cost_retired from units_retired
692         -- regardless of cost_retired once units_retired is proved
693         -- just as faxasset form does so.
694         -- formula: cost_retired <= units_retired/current_units * cost
695         l_asset_retire_rec.cost_retired
696            := (l_asset_retire_rec.units_retired / l_asset_desc_rec.current_units)
697                * l_asset_fin_rec.cost;
698 
699         if not FA_UTILS_PKG.faxrnd(x_amount => l_asset_retire_rec.cost_retired
700                                   ,x_book   => l_asset_hdr_rec.book_type_code
701                                 ,p_log_level_rec => g_log_level_rec) then
702                                       raise FND_API.G_EXC_UNEXPECTED_ERROR;
703         end if;
704 
705         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'derived cost_retired: ', l_asset_retire_rec.cost_retired, g_log_level_rec); end if;
706 
707         if l_asset_retire_rec.units_retired = l_asset_desc_rec.current_units then
708            l_transaction_type := 'FULL RETIREMENT';
709         else
710            l_transaction_type := 'PARTIAL UNIT RETIREMENT';
711         end if;
712 
713       else -- if units_retired is null
714 
715         -- ? need to check the condition for the transaction type, full retirement.
716         if l_asset_retire_rec.cost_retired = l_asset_fin_rec.cost then
717              l_transaction_type := 'FULL RETIREMENT';
718         else
719              l_transaction_type := 'PARTIAL COST RETIREMENT';
720         end if;
721 
722    end if; -- units_retired/cost_retired
723 
724    -- Make sure that transaction type becomes FULL RETIREMENT
725    -- when retiring an asset with zero cost
726    if l_asset_fin_rec.cost = 0 then
727         l_transaction_type := 'FULL RETIREMENT';
728    end if;
729 
730    -- ? need to check again
731    if l_transaction_type = 'FULL RETIREMENT'
732          then
733            l_trans_rec.transaction_type_code := 'FULL RETIREMENT';
734    elsif l_transaction_type = 'PARTIAL UNIT RETIREMENT'
735          or l_transaction_type = 'PARTIAL COST RETIREMENT'
736          then
737            l_trans_rec.transaction_type_code := 'PARTIAL RETIREMENT';
738    end if;
739 
740    -- assign date_retired to trans_date_entered
741    l_trans_rec.transaction_date_entered := l_asset_retire_rec.date_retired;
742 
743    l_asset_retire_rec.status := 'PENDING';
744 
745    -- end retirement-specific calculation
746    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'end calculation', '', g_log_level_rec); end if;
747 
748    -- Fix for Bug #3187975.  Do Polish rule validations.
749    -- Need to do this after transaction_type_code is derived.
750    if not FA_ASSET_VAL_PVT.validate_polish (
751       p_transaction_type_code => l_trans_rec.transaction_type_code,
752       p_method_code           => l_asset_fin_rec.deprn_method_code,
753       p_life_in_months        => l_asset_fin_rec.life_in_months,
754       p_asset_type            => l_asset_type_rec.asset_type,
755       p_bonus_rule            => l_asset_fin_rec.bonus_rule,
756       p_ceiling_name          => l_asset_fin_rec.ceiling_name,
757       p_deprn_limit_type      => l_asset_fin_rec.deprn_limit_type,
758       p_group_asset_id        => l_asset_fin_rec.group_asset_id,
759       p_calling_fn            => 'FA_RETIREMENT_PUB.do_retirement',
760       p_log_level_rec         => g_log_level_rec) then
761       g_msg_name := null;
762       g_token1 := null;
763       raise FND_API.G_EXC_ERROR;
764    end if;
765 
766    -- ***************************
767    -- **  Main
768    -- ***************************
769    if g_log_level_rec.statement_level then
770        fa_debug_pkg.add
771             (fname   => l_calling_fn,
772              element => 'Entering corporate book',
773              value   => ''
774              ,p_log_level_rec => g_log_level_rec);
775    end if;
776 
777    if not do_all_books_retirement
778           (px_trans_rec        => l_trans_rec
779           ,px_dist_trans_rec   => l_dist_trans_rec
780           ,p_asset_hdr_rec     => l_asset_hdr_rec
781           ,p_asset_desc_rec    => l_asset_desc_rec
782           ,p_asset_type_rec    => l_asset_type_rec
783           ,p_asset_fin_rec     => l_asset_fin_rec
784           ,px_asset_retire_rec => l_asset_retire_rec
785           ,p_asset_dist_tbl    => l_asset_dist_tbl
786           ,p_subcomp_tbl       => l_subcomp_tbl
787           ,p_inv_tbl           => l_inv_tbl
788           ,p_period_rec        => l_period_rec
789           ,p_log_level_rec     => g_log_level_rec) then
790               raise FND_API.G_EXC_ERROR;
791    end if;
792 
793    l_retirement_id    := l_asset_retire_rec.retirement_id;
794 
795    if g_log_level_rec.statement_level then
796        fa_debug_pkg.add
797             (fname   => l_calling_fn,
798              element => 'Finished corporate book',
799              value   => ''
800              ,p_log_level_rec => g_log_level_rec);
801    end if;
802 
803    px_trans_rec := l_trans_rec;
804    px_dist_trans_rec := l_dist_trans_rec;
805    px_asset_retire_rec := l_asset_retire_rec;
806 
807    -- BUG# 2376085, 2487707
808    -- reset GL sob id to original value before moving to next book
809    fnd_profile.put('GL_SET_OF_BKS_ID', l_orig_set_of_books_id);
810    fnd_client_info.set_currency_context (l_orig_currency_context);
811 
812    /* if book is a corporate book, process cip assets and autocopy */
813 
814    -- start processing tax books for cip-in-tax and autocopy
815    if (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE') then
816 
817       lv_trans_rec := l_trans_rec;
818       lv_asset_hdr_rec := l_asset_hdr_rec;
819 
820       if (l_asset_type_rec.asset_type = 'CIP'
821           or l_asset_type_rec.asset_type = 'CAPITALIZED') then
822 
823          if g_log_level_rec.statement_level then
824               fa_debug_pkg.add
825               (fname   => l_calling_fn,
826                element => 'Asset type',
827                value   => l_asset_type_rec.asset_type
828                ,p_log_level_rec => g_log_level_rec);
829          end if;
830 
831          if not fa_cache_pkg.fazctbk
832                 (x_corp_book    => l_asset_hdr_rec.book_type_code
833                 ,x_asset_type   => l_asset_type_rec.asset_type
834                 ,x_tax_book_tbl => l_tax_book_tbl
835                 ,p_log_level_rec     => g_log_level_rec) then
836                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
837          end if;
838 
839          for l_tax_index in 1..l_tax_book_tbl.count loop
840 
841            if g_log_level_rec.statement_level then
842              fa_debug_pkg.add(l_calling_fn, 'entered loop for tax books', '', g_log_level_rec);
843              fa_debug_pkg.add(l_calling_fn, 'selected tax book: ', l_tax_book_tbl(l_tax_index), g_log_level_rec);
844            end if;
845 
846            if not FA_ASSET_VAL_PVT.validate_asset_book
847                   (p_transaction_type_code      => px_trans_rec.transaction_type_code
848                   ,p_book_type_code             => l_tax_book_tbl(l_tax_index)
849                   ,p_asset_id                   => px_asset_hdr_rec.asset_id
850                   ,p_calling_fn                 => l_calling_fn
851                   ,p_log_level_rec              => g_log_level_rec) then
852 
853                      null; -- just to ignore the error
854 
855            else
856 
857              -- cache the book information for the tax book
858              if not fa_cache_pkg.fazcbc(x_book => l_tax_book_tbl(l_tax_index)) then
859                   raise FND_API.G_EXC_UNEXPECTED_ERROR;
860              end if;
861 
862              -- ? Excerpt from Brad's comment on this part - need more investigation:
863              -- 'May need to set the transaction date, trx_type, subtype here as well
864              --  based on the open period and settings for each tax book in the loop'
865 
866              lv_asset_hdr_rec.book_type_code           := l_tax_book_tbl(l_tax_index);
867              lv_trans_rec.source_transaction_header_id := l_trans_rec.transaction_header_id;
868              lv_trans_rec.transaction_header_id        := null;
869 
870              if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling do_all_books_retirement for tax book', '', g_log_level_rec); end if;
871 
872              if g_log_level_rec.statement_level then
873                fa_debug_pkg.add
874                (fname   => l_calling_fn,
875                 element => 'l_tax_book_tbl.count',
876                 value   => l_tax_book_tbl.count);
877              end if;
878 
879              if not do_all_books_retirement
880                     (px_trans_rec        => lv_trans_rec      -- tax
881                     ,px_dist_trans_rec   => lv_dist_trans_rec -- null
882                     ,p_asset_hdr_rec     => lv_asset_hdr_rec  -- tax
883                     ,p_asset_desc_rec    => l_asset_desc_rec
884                     ,p_asset_type_rec    => l_asset_type_rec
885                     ,p_asset_fin_rec     => l_asset_fin_rec
886                     ,px_asset_retire_rec => lv_asset_retire_rec
887                     ,p_asset_dist_tbl    => l_asset_dist_tbl
888                     ,p_subcomp_tbl       => l_subcomp_tbl
889                     ,p_inv_tbl           => l_inv_tbl
890                     ,p_period_rec        => l_period_rec
891                     ,p_log_level_rec     => g_log_level_rec) then
892                         raise FND_API.G_EXC_ERROR;
893              end if;
894 
895 
896              -- BUG# 2376085, 2487707
897              -- reset GL sob id to original value before moving to next book
898              fnd_profile.put('GL_SET_OF_BKS_ID', l_orig_set_of_books_id);
899              fnd_client_info.set_currency_context (l_orig_currency_context);
900 
901            end if;
902 
903          end loop;
904 
905       end if; -- asset_type
906 
907    end if; -- book_class
908 
909    if g_log_level_rec.statement_level then
910        fa_debug_pkg.add
911             (fname   => l_calling_fn,
912              element => 'Check to see if calculate gain/loss has to be submitted',
913              value   => ''
914              ,p_log_level_rec => g_log_level_rec);
915    end if;
916 
917    if g_log_level_rec.statement_level then
918        fa_debug_pkg.add(l_calling_fn, 'before calc gain/loss', '', g_log_level_rec);
919    end if;
920 
921    -- submit calculate_gain_loss programs if flag is set
922 
923    if l_calculate_gain_loss_flag = FND_API.G_TRUE then
924 
925         if g_log_level_rec.statement_level then
926            fa_debug_pkg.add
927             (fname   => l_calling_fn,
928              element => 'Running calculate gain/loss...',
929              value   => ''
930              ,p_log_level_rec => g_log_level_rec);
931         end if;
932 
933         if g_log_level_rec.statement_level then
934           fa_debug_pkg.add(l_calling_fn, 'ret_id:', l_retirement_id, g_log_level_rec);
935           fa_debug_pkg.add(l_calling_fn, 'submit calculate gain/loss program', '', g_log_level_rec);
936         end if;
937         if not calculate_gain_loss
938                (p_retirement_id     => l_retirement_id
939                ,p_mrc_sob_type_code => 'P'
940                 ,p_log_level_rec => g_log_level_rec) then
941                    raise FND_API.G_EXC_UNEXPECTED_ERROR;
942         end if;
943         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'right after calc gain/loss', '', g_log_level_rec); end if;
944 
945    end if;
946 
947    -- call to workflow business event
948    fa_business_events.raise(p_event_name => 'oracle.apps.fa.retirement.asset.retire',
949                  p_event_key => l_retirement_id || to_char(sysdate,'RRDDDSSSSS'),
950                  p_parameter_name1 => 'RETIREMENT_ID',
951                  p_parameter_value1 => l_retirement_id,
952                  p_parameter_name2 => 'ASSET_ID',
953                  p_parameter_value2 => px_asset_hdr_rec.asset_id,
954                  p_parameter_name3 => 'ASSET_NUMBER',
955                  p_parameter_value3 => l_asset_desc_rec.asset_number,
956                  p_parameter_name4 => 'BOOK_TYPE_CODE',
957                  p_parameter_value4 => px_asset_hdr_rec.book_type_code,
958                  p_log_level_rec     => g_log_level_rec);
959 
960 
961    -- commit if p_commit is TRUE.
962    if FND_API.to_boolean(p_commit) then
963       COMMIT WORK;
964    end if;
965 
966    -- Standard call to get message count and if count is 1 get message info.
967    FND_MSG_PUB.count_and_get(p_count   => x_msg_count
968                             ,p_data    => x_msg_data
969                             );
970 
971    -- set back to original environment when the procedure is finished
972    fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
973    fnd_client_info.set_currency_context (g_orig_currency_context);
974 
975    -- return the status.
976    x_return_status := FND_API.G_RET_STS_SUCCESS;
977 
978 EXCEPTION
979 
980    when FND_API.G_EXC_ERROR then
981 
982           ROLLBACK TO do_retirement;
983 
984           x_return_status := FND_API.G_RET_STS_ERROR;
985 
986           if g_token1 is null then
987                fa_srvr_msg.add_message(calling_fn => l_calling_fn
988                                       ,name       => g_msg_name
989                                       ,p_log_level_rec => g_log_level_rec);
990           else
991                fa_srvr_msg.add_message(calling_fn => l_calling_fn
992                                       ,name       => g_msg_name
993                                       ,token1     => g_token1
994                                       ,value1     => g_value1
995                                       ,p_log_level_rec => g_log_level_rec);
996           end if;
997 
998           FND_MSG_PUB.count_and_get(p_count => x_msg_count
999                                    ,p_data  => x_msg_data
1000                                    );
1001           -- set back to original environment when the procedure is finished
1002           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
1003           fnd_client_info.set_currency_context (g_orig_currency_context);
1004 
1005    when FND_API.G_EXC_UNEXPECTED_ERROR then
1006 
1007           ROLLBACK TO do_retirement;
1008 
1009           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1010 
1011           fa_srvr_msg.add_message(calling_fn => l_calling_fn
1012                     ,p_log_level_rec => g_log_level_rec);
1013           FND_MSG_PUB.count_and_get(p_count => x_msg_count
1014                                    ,p_data  => x_msg_data
1015                                    );
1016           -- set back to original environment when the procedure is finished
1017           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
1018           fnd_client_info.set_currency_context (g_orig_currency_context);
1019 
1020    when others then
1021 
1022           ROLLBACK TO do_retirement;
1023 
1024           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1025 
1026           fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
1027                     ,p_log_level_rec => g_log_level_rec);
1028           FND_MSG_PUB.count_and_get(p_count => x_msg_count
1029                                    ,p_data  => x_msg_data
1030                                    );
1031           -- set back to original environment when the procedure is finished
1032           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
1033           fnd_client_info.set_currency_context (g_orig_currency_context);
1034 
1035 END do_retirement;
1036 
1037 FUNCTION do_all_books_retirement
1038         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
1039         ,px_dist_trans_rec            in out NOCOPY FA_API_TYPES.trans_rec_type
1040         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
1041         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
1042         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
1043         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
1044         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
1045         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
1046         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
1047         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
1048         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
1049         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
1050 -- will return retirement_id of asset_retire_rec
1051 IS
1052 
1053    -- Returns the reporting GL set_of_books_ids
1054    -- associated with the set_of_books_id of given primary book_type_code
1055    CURSOR sob_cursor(p_book_type_code in varchar2
1056                     ,p_sob_id         in number) is
1057    SELECT set_of_books_id AS sob_id
1058      FROM fa_mc_book_controls
1059     WHERE book_type_code          = p_book_type_code
1060       AND primary_set_of_books_id = p_sob_id
1061       AND enabled_flag            = 'Y';
1062 
1063    -- used for main transaction book
1064    l_book_class                 varchar2(15);
1065    l_set_of_books_id            number;
1066    l_distribution_source_book   varchar2(15);
1067    l_mc_source_flag             varchar2(1);
1068 
1069    -- local asset info
1070    l_trans_rec         FA_API_TYPES.trans_rec_type;
1071    l_dist_trans_rec    FA_API_TYPES.trans_rec_type;
1072    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
1073    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
1074    l_asset_type_rec    FA_API_TYPES.asset_type_rec_type;
1075    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
1076    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
1077    l_asset_dist_tbl    FA_API_TYPES.asset_dist_tbl_type;
1078    l_subcomp_tbl       FA_API_TYPES.subcomp_tbl_type;
1079    l_inv_tbl           FA_API_TYPES.inv_tbl_type;
1080    l_period_rec        FA_API_TYPES.period_rec_type;
1081 
1082    l_sob_tbl           FA_CACHE_PKG.fazcrsob_sob_tbl_type;
1083 
1084    lv_asset_hdr_rec    FA_API_TYPES.asset_hdr_rec_type;
1085 
1086    -- local individual variables
1087    l_latest_trans_date date;
1088    l_ret_prorate_date  date;
1089    l_prorate_calendar  varchar2(15);
1090    l_fiscal_year_name  varchar2(30);
1091 
1092    -- used for category defaulting
1093    l_ret_prorate_convention     FA_CATEGORY_BOOK_DEFAULTS.prorate_convention_code%TYPE;
1094    l_use_stl_retirements_flag   FA_CATEGORY_BOOK_DEFAULTS.use_stl_retirements_flag%TYPE;
1095    l_stl_method_code    FA_CATEGORY_BOOK_DEFAULTS.stl_method_code%TYPE;
1096    l_stl_life_in_months FA_CATEGORY_BOOK_DEFAULTS.stl_life_in_months%TYPE;
1097 
1098 
1099    -- added for bug 3684222
1100    l_latest_reval_date date;
1101 
1102    -- msg
1103    g_msg_name                   varchar2(30);
1104 
1105    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.do_all_books_retirement';
1106 
1107 BEGIN
1108 
1109 
1110    -- ****************************************************
1111    -- **  Assign input parameters to local rec/tbl types
1112    -- ****************************************************
1113    l_trans_rec        := px_trans_rec;
1114    l_dist_trans_rec   := px_dist_trans_rec;
1115    l_asset_hdr_rec    := p_asset_hdr_rec;
1116    l_asset_desc_rec   := p_asset_desc_rec;
1117    l_asset_type_rec   := p_asset_type_rec;
1118    l_asset_retire_rec := px_asset_retire_rec;
1119    l_asset_dist_tbl   := p_asset_dist_tbl;
1120    l_subcomp_tbl      := p_subcomp_tbl;
1121    l_inv_tbl          := p_inv_tbl;
1122 
1123    -- *********************************
1124    -- **  Populate local record types
1125    -- *********************************
1126    -- populate rec_types that were not provided by users
1127 
1128    -- call the cache for the primary transaction book
1129    if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
1130   ,p_log_level_rec => p_log_level_rec) then
1131          raise FND_API.G_EXC_UNEXPECTED_ERROR;
1132    end if;
1133 
1134    l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
1135 
1136    -- set the sob_id and currency context
1137    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
1138    fnd_client_info.set_currency_context(to_char(l_asset_hdr_rec.set_of_books_id));
1139 
1140    -- pop asset_fin_rec
1141    -- get fa_books row where transaction_header_id_out is null
1142    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_fin_rec', '', p_log_level_rec); end if;
1143    if not FA_UTIL_PVT.get_asset_fin_rec
1144           (p_asset_hdr_rec         => l_asset_hdr_rec
1145           ,px_asset_fin_rec        => l_asset_fin_rec
1146           ,p_transaction_header_id => NULL
1147           ,p_mrc_sob_type_code     => 'P'
1148           ,p_log_level_rec => p_log_level_rec) then
1149               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1150    end if;
1151 
1152    -- pop current period_rec info
1153    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop period_rec', '', p_log_level_rec); end if;
1154    if not FA_UTIL_PVT.get_period_rec
1155           (p_book           => l_asset_hdr_rec.book_type_code
1156           ,p_effective_date => NULL
1157           ,x_period_rec     => l_period_rec
1158           ,p_log_level_rec => p_log_level_rec) then
1159               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1160    end if;
1161 
1162    -- ***************************************************
1163    -- **  Do asset/book-level validation on transaction
1164    -- ***************************************************
1165    -- begin asset/book-level validation on transaction
1166 
1167    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin asset/book-level validation on transaction', '', p_log_level_rec); end if;
1168 
1169    -- check if the asset is in period of addition
1170    -- used CAPITALIZED as mode since the current FAXASSET form uses this logic
1171    -- We allow users to retire CIP assets. But we don't allow retirements
1172    -- in period of capitalization.
1173    -- The following mode 'CAPITALIZED' makes sure that the asset
1174    -- is neither in the period of addition nor in the period of capitalization.
1175 
1176    if g_log_level_rec.statement_level then
1177        fa_debug_pkg.add(l_calling_fn, 'check if the asset is in period of addition', '', p_log_level_rec);
1178        fa_debug_pkg.add(l_calling_fn, 'asset_id: ',  l_asset_hdr_rec.asset_id, p_log_level_rec);
1179        fa_debug_pkg.add(l_calling_fn, 'book: ',  l_asset_hdr_rec.book_type_code, p_log_level_rec);
1180    end if;
1181 
1182    -- BUG# 4053626
1183    -- the above was incorrect interpretation - need to use absolute
1184 
1185    -- SLA Uptake
1186    -- allowing retirements in period of addition
1187 
1188    if FA_ASSET_VAL_PVT.validate_period_of_addition
1189       (p_asset_id            => l_asset_hdr_rec.asset_id
1190       ,p_book                => l_asset_hdr_rec.book_type_code
1191       ,p_mode                => 'ABSOLUTE'
1192       ,px_period_of_addition => l_asset_hdr_rec.period_of_addition
1193       ,p_log_level_rec => p_log_level_rec) then
1194      if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'perid_of_addition_flag: ',  l_asset_hdr_rec.period_of_addition, p_log_level_rec); end if;
1195 
1196           null;
1197           /*
1198           if l_asset_hdr_rec.period_of_addition='Y' then
1199                -- error out since retirement is not allowed in period of addition
1200                g_msg_name := 'FA_RET_CANT_RET_NONDEPRN';
1201                raise FND_API.G_EXC_ERROR;
1202           end if;
1203           */
1204    else
1205           raise FND_API.G_EXC_UNEXPECTED_ERROR;
1206    end if;
1207    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'perid_of_addition_flag: ',  l_asset_hdr_rec.period_of_addition, p_log_level_rec); end if;
1208 
1209    -- check if there is an add-to-asset transaction pending
1210    -- Users must post their mass additions before they can retire the asset
1211    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if there is an add-to-asset transaction pending', '', p_log_level_rec); end if;
1212    if FA_ASSET_VAL_PVT.validate_add_to_asset_pending
1213       (p_asset_id   => l_asset_hdr_rec.asset_id
1214       ,p_book       => l_asset_hdr_rec.book_type_code
1215       ,p_log_level_rec => p_log_level_rec) then
1216           -- Users must post their mass additions before they can retire the asset
1217           g_msg_name := 'FA_RET_CANT_RET_INCOMPLETE_ASS';
1218           raise FND_API.G_EXC_ERROR;
1219    end if;
1220 
1221    -- check if another retirement/reinstatement already pending
1222    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if another retirement/reinstatement already pending', '', p_log_level_rec); end if;
1223    if FA_ASSET_VAL_PVT.validate_ret_rst_pending
1224       (p_asset_id   => l_asset_hdr_rec.asset_id
1225       ,p_book       => l_asset_hdr_rec.book_type_code
1226       ,p_log_level_rec => p_log_level_rec) then
1227           g_msg_name := 'FA_RET_PENDING_RETIREMENTS';
1228           raise FND_API.G_EXC_ERROR;
1229    end if;
1230 
1231    -- check if the asset has already been fully retired
1232    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if the asset is already fully retired', '', p_log_level_rec); end if;
1233    if FA_ASSET_VAL_PVT.validate_fully_retired
1234       (p_asset_id  => l_asset_hdr_rec.asset_id
1235       ,p_book      => l_asset_hdr_rec.book_type_code
1236       ,p_log_level_rec => p_log_level_rec) then
1237           g_msg_name := 'FA_REC_RETIRED';
1238           raise FND_API.G_EXC_ERROR;
1239    end if;
1240 
1241    -- check if date_retired is valid in terms of trx date
1242    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if date_retired is valid in terms of trx date', '', p_log_level_rec); end if;
1243    if l_asset_retire_rec.date_retired is not null then
1244 
1245         -- no transactions except Retirements and Reinstatements may be
1246         -- dated after the latest trx date
1247         if not FA_UTIL_PVT.get_latest_trans_date
1248                (p_calling_fn        => l_calling_fn
1249                ,p_asset_id          => l_asset_hdr_rec.asset_id
1250                ,p_book              => l_asset_hdr_rec.book_type_code
1251                ,x_latest_trans_date => l_latest_trans_date
1252                ,p_log_level_rec => p_log_level_rec) then
1253                    raise FND_API.G_EXC_UNEXPECTED_ERROR;
1254         end if;
1255 
1256         if l_asset_retire_rec.date_retired < l_latest_trans_date then
1257              g_msg_name := 'FA_SHARED_OTHER_TRX_FOLLOW';
1258              raise FND_API.G_EXC_ERROR;
1259         end if;
1260 
1261         -- BUG# 3575340
1262         -- need to prevent the backdate of a retirement not only
1263         -- on the retire date but also the prorate date
1264 
1265         l_prorate_calendar     := fa_cache_pkg.fazcbc_record.prorate_calendar;
1266         l_fiscal_year_name     := fa_cache_pkg.fazcbc_record.fiscal_year_name;
1267 
1268         if g_log_level_rec.statement_level then
1269               fa_debug_pkg.add
1270               (fname   => l_calling_fn,
1271                element => 'fa_cache_pkg.fazcbc_record.prorate_calendar',
1272                value   => fa_cache_pkg.fazcbc_record.prorate_calendar);
1273               fa_debug_pkg.add
1274               (fname   => l_calling_fn,
1275                element => 'l_asset_retire_rec.retirement_prorate_convention',
1276                value   => l_asset_retire_rec.retirement_prorate_convention);
1277         end if;
1278 
1279         if g_log_level_rec.statement_level then
1280            fa_debug_pkg.add(l_calling_fn, 'Get book-specific ret convention info and etc', '', p_log_level_rec);
1281         end if;
1282 
1283         /* Bug#4663092: Moved Book-specific validation/calculation to do_all_retirement function. */
1284         if l_asset_retire_rec.retirement_prorate_convention is null then
1285 
1286           SELECT retirement_prorate_convention,
1287              use_stl_retirements_flag,
1288              stl_method_code,
1289              stl_life_in_months
1290           INTO
1291             l_ret_prorate_convention,
1292             l_use_stl_retirements_flag,
1293             l_stl_method_code,
1294             l_stl_life_in_months
1295           FROM   fa_category_book_defaults, fa_additions_b a
1296           WHERE  book_type_code = l_asset_hdr_rec.book_type_code
1297             and  category_id = a.asset_category_id
1298             and  a.asset_id  = l_asset_hdr_rec.asset_id
1299             and  l_asset_fin_rec.Date_Placed_In_Service between start_dpis and
1300                    nvl(end_dpis,l_asset_fin_rec.Date_Placed_In_Service);
1301 
1302           l_asset_retire_rec.retirement_prorate_convention := l_ret_prorate_convention;
1303           --
1304 
1305           if (l_Use_STL_Retirements_Flag = 'NO') then
1306             l_asset_retire_rec.detail_info.stl_method_code := NULL;
1307             l_asset_retire_rec.detail_info.stl_life_in_months := NULL;
1308           else
1309             if (l_asset_retire_rec.detail_info.stl_method_code is null) then
1310               l_asset_retire_rec.detail_info.stl_method_code :=
1311                                                 l_stl_method_code;
1312             end if;
1313             if (l_asset_retire_rec.detail_info.stl_life_in_months is null) then
1314                l_asset_retire_rec.detail_info.stl_life_in_months :=
1315                                                 l_stl_life_in_months;
1316             end if;
1317           end if;
1318 
1319         end if;
1320 
1321 
1322         if not fa_cache_pkg.fazccvt
1323                (x_prorate_convention_code => l_asset_retire_rec.retirement_prorate_convention,
1324                 x_fiscal_year_name        => l_fiscal_year_name
1325                 ,p_log_level_rec => p_log_level_rec) then
1326            raise FND_API.G_EXC_ERROR;
1327         end if;
1328 
1329         if not fa_cache_pkg.fazcdp
1330                  (x_book_type_code => l_asset_hdr_rec.book_type_code
1331                                   ,p_log_level_rec => p_log_level_rec) then
1332            raise FND_API.G_EXC_ERROR;
1333         end if;
1334 
1335         if g_log_level_rec.statement_level then
1336            fa_debug_pkg.add(l_calling_fn, 'Get ret prorate_date...', '', p_log_level_rec);
1337         end if;
1338 
1339         select prorate_date
1340           into l_ret_prorate_date
1341           from fa_calendar_periods cp,
1342                fa_conventions conv
1343          where conv.prorate_convention_code   = l_asset_retire_rec.retirement_prorate_convention
1344            and conv.start_date               <= l_asset_retire_rec.date_retired
1345            and conv.end_date                 >= l_asset_retire_rec.date_retired
1346            and cp.calendar_type               = l_prorate_calendar
1347            and conv.prorate_date             >= cp.start_date
1348            and conv.prorate_date             <= cp.end_date;
1349 
1350         -- added this for bug 3684222
1351         select MAX(transaction_date_entered)
1352         into l_latest_reval_date
1353         from fa_transaction_headers
1354         where asset_id       = l_asset_hdr_rec.asset_id
1355         and book_type_code = l_asset_hdr_rec.book_type_code
1356         and transaction_type_code in ('REVALUATION'
1357 );
1358 
1359         -- added this for bug 3684222
1360         if (l_ret_prorate_date < l_latest_reval_date and
1361              l_ret_prorate_date < fa_cache_pkg.fazcdp_record.calendar_period_open_date) then
1362              g_msg_name := 'FA_SHARED_OTHER_TRX_FOLLOW';
1363              raise FND_API.G_EXC_ERROR;
1364         end if;
1365 
1366 
1367    end if; -- date_retired
1368 
1369    if (not fa_cache_pkg.fazccmt(l_asset_fin_rec.deprn_method_code,
1370                                 l_asset_fin_rec.life_in_months
1371                                 ,p_log_level_rec => p_log_level_rec)) then
1372       if (p_log_level_rec.statement_level) then
1373             fa_debug_pkg.add(l_calling_fn, 'Error calling', 'fa_cache_pkg.fazccmt'
1374                         ,p_log_level_rec => p_log_level_rec);
1375       end if;
1376 
1377       raise FND_API.G_EXC_UNEXPECTED_ERROR;
1378    end if;
1379 
1380    --
1381    -- Bug3254818: Lift this ristriction for FLAT method type
1382    --
1383    -- check if date_retired is valid
1384    if (l_asset_retire_rec.date_retired is not null) and
1385       (fa_cache_pkg.fazccmt_record.rate_source_rule <> fa_std_types.FAD_RSR_FLAT) then
1386 
1387         if l_asset_retire_rec.date_retired < l_period_rec.calendar_period_open_date
1388            and nvl(l_asset_fin_rec.period_counter_fully_reserved,99)
1389                <> nvl(l_asset_fin_rec.period_counter_life_complete,99)
1390            then
1391 
1392              g_msg_name := 'FA_NO_TRX_WHEN_LIFE_COMPLETE';
1393              raise FND_API.G_EXC_ERROR;
1394 
1395         end if;
1396 
1397    end if; -- date_retired
1398 
1399    -- check if cost_retired is valid when units_retired is null
1400    if l_asset_retire_rec.units_retired is null then
1401 
1402         -- check if absolute value of retired_cost is greater than that of current_cost
1403 
1404 
1405         if abs(l_asset_retire_rec.cost_retired) > abs(l_asset_fin_rec.cost)
1406            or sign(l_asset_retire_rec.cost_retired) <> sign(l_asset_fin_rec.cost) then
1407 
1408           if (p_log_level_rec.statement_level) then
1409              FA_DEBUG_PKG.ADD (fname=>'do_all_book_retirement',
1410                 element=>'In error: cost_retired',
1411                value=> l_asset_retire_rec.cost_retired
1412                ,p_log_level_rec => p_log_level_rec);
1413              FA_DEBUG_PKG.ADD (fname=>'do_all_book_retirement',
1414                 element=>'In error: cost',
1415                value=> l_asset_fin_rec.cost
1416                ,p_log_level_rec => p_log_level_rec);
1417           end if;
1418 
1419              g_msg_name := 'FA_RET_COST_TOO_BIG';
1420              raise FND_API.G_EXC_ERROR;
1421         end if;
1422 
1423    end if; -- units_retired
1424 
1425    if (fa_cache_pkg.fazcbc_record.book_class = 'TAX'
1426        and l_asset_retire_rec.units_retired is not null) then
1427 
1428         -- In tax book, treat unit retirement as cost retirement
1429         l_asset_retire_rec.cost_retired
1430           := l_asset_fin_rec.cost
1431              * l_asset_retire_rec.units_retired
1432                / l_asset_desc_rec.current_units;
1433 
1434         l_asset_retire_rec.units_retired := NULL;
1435 
1436         -- round the converted amounts
1437         if not FA_UTILS_PKG.faxrnd(x_amount => l_asset_retire_rec.cost_retired
1438                                   ,x_book   => l_asset_hdr_rec.book_type_code
1439                                   ,p_log_level_rec => p_log_level_rec) then
1440                                       raise FND_API.G_EXC_UNEXPECTED_ERROR;
1441         end if;
1442 
1443    end if;
1444 
1445    if l_asset_retire_rec.cost_retired is null then
1446       l_asset_retire_rec.cost_retired := 0;
1447    end if;
1448 
1449    if l_asset_retire_rec.proceeds_of_sale is null then
1450       l_asset_retire_rec.proceeds_of_sale := 0;
1451    end if;
1452 
1453    if l_asset_retire_rec.cost_of_removal is null then
1454       l_asset_retire_rec.cost_of_removal := 0;
1455    end if;
1456 
1457    -- Pop the transaction_header_id for the RETIREMENT row
1458    select fa_transaction_headers_s.nextval
1459    into   l_trans_rec.transaction_header_id
1460    from   dual;
1461 
1462     /* Bug# 4370014: Direct fetch for THID, RETID */
1463    select fa_retirements_s.nextval
1464    into l_asset_retire_rec.retirement_id
1465    from dual;
1466 
1467 
1468    -- SLA UPTAKE
1469    -- assign an event for the transaction
1470    -- at this point key info asset/book/trx info is known from above code
1471 
1472    if not fa_xla_events_pvt.create_transaction_event
1473         (p_asset_hdr_rec => l_asset_hdr_rec,
1474          p_asset_type_rec=> l_asset_type_rec,
1475          px_trans_rec    => l_trans_rec,
1476          p_event_status  => XLA_EVENTS_PUB_PKG.C_EVENT_INCOMPLETE,
1477          p_calling_fn    => 'FA_RETIREMENT_PUB.do_all_books_retirement'
1478          ,p_log_level_rec => p_log_level_rec) then
1479       raise FND_API.G_EXC_UNEXPECTED_ERROR;
1480    end if;
1481 
1482    -- ***************************
1483    -- **  Main
1484    -- ***************************
1485 
1486    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling do_sub_retirement for each book/sob', '', p_log_level_rec); end if;
1487 
1488    if not do_sub_retirement
1489           (px_trans_rec        => l_trans_rec
1490           ,px_dist_trans_rec   => l_dist_trans_rec
1491           ,p_asset_hdr_rec     => l_asset_hdr_rec
1492           ,p_asset_desc_rec    => l_asset_desc_rec
1493           ,p_asset_type_rec    => l_asset_type_rec
1494           ,p_asset_fin_rec     => l_asset_fin_rec
1495           ,px_asset_retire_rec => l_asset_retire_rec
1496           ,p_asset_dist_tbl    => l_asset_dist_tbl
1497           ,p_subcomp_tbl       => l_subcomp_tbl
1498           ,p_inv_tbl           => l_inv_tbl
1499           ,p_period_rec        => l_period_rec
1500           ,p_mrc_sob_type_code => 'P'
1501 ,p_log_level_rec => p_log_level_rec) then
1502               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1503    end if;
1504 
1505    if g_log_level_rec.statement_level then
1506       fa_debug_pkg.add(l_calling_fn, 'do_all_books_retirement: retirement_id: ', l_asset_retire_rec.retirement_id, p_log_level_rec);
1507       fa_debug_pkg.add(l_calling_fn, 'do_all_books_retirement: transaction_header_id: ', l_trans_rec.transaction_header_id, p_log_level_rec);
1508    end if;
1509 
1510    if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
1511                              ,p_log_level_rec => p_log_level_rec) then
1512         fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
1513                 ,p_log_level_rec => p_log_level_rec);
1514         return FALSE;
1515    end if;
1516 
1517    -- MRC LOOP
1518    -- if this is a primary book, process reporting books(sobs)
1519    if fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y' then
1520 
1521        g_primary_set_of_books_id := l_asset_hdr_rec.set_of_books_id;
1522 
1523        -- call the sob cache to get the table of sob_ids
1524        if not FA_CACHE_PKG.fazcrsob
1525               (x_book_type_code => l_asset_hdr_rec.book_type_code,
1526                x_sob_tbl        => l_sob_tbl
1527                ,p_log_level_rec => p_log_level_rec) then
1528           raise FND_API.G_EXC_UNEXPECTED_ERROR;
1529        end if;
1530 
1531        -- loop through each book starting with the primary and
1532        -- call sub routine for each
1533        for l_sob_index in 1..l_sob_tbl.count loop
1534 
1535          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in sob_id loop', '', p_log_level_rec); end if;
1536 
1537          -- set the sob_id and currency context for reporting book(sob)
1538          fnd_profile.put('GL_SET_OF_BKS_ID', l_sob_tbl(l_sob_index));
1539          fnd_client_info.set_currency_context(to_char(l_sob_tbl(l_sob_index)));
1540 
1541          if not fa_cache_pkg.fazcbcs(x_book => l_asset_hdr_rec.book_type_code
1542                                     ,p_log_level_rec => p_log_level_rec) then
1543            fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
1544                       ,p_log_level_rec => p_log_level_rec);
1545            return FALSE;
1546          end if;
1547 
1548          -- set up the local asset_header and sob_id
1549          lv_asset_hdr_rec    := l_asset_hdr_rec;
1550          lv_asset_hdr_rec.set_of_books_id := l_sob_tbl(l_sob_index);
1551 
1552          if not do_sub_retirement
1553                 (px_trans_rec        => l_trans_rec
1554                 ,px_dist_trans_rec   => l_dist_trans_rec
1555                 ,p_asset_hdr_rec     => lv_asset_hdr_rec
1556                 ,p_asset_desc_rec    => l_asset_desc_rec
1557                 ,p_asset_type_rec    => l_asset_type_rec
1558                 ,p_asset_fin_rec     => l_asset_fin_rec
1559                 ,px_asset_retire_rec => l_asset_retire_rec
1560                 ,p_asset_dist_tbl    => l_asset_dist_tbl
1561                 ,p_subcomp_tbl       => l_subcomp_tbl
1562                 ,p_inv_tbl           => l_inv_tbl
1563                 ,p_period_rec        => l_period_rec
1564                 ,p_mrc_sob_type_code => 'R'
1565                 ,p_log_level_rec => p_log_level_rec) then
1566                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
1567          end if;
1568 
1569        end loop;
1570 
1571    end if;
1572 
1573    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'do_all_books_retirement: retirement_id: ', l_asset_retire_rec.retirement_id, p_log_level_rec); end if;
1574    -- px_asset_retire_rec := l_asset_retire_rec;
1575    px_asset_retire_rec.retirement_id := l_asset_retire_rec.retirement_id;
1576    px_trans_rec := l_trans_rec;
1577    px_dist_trans_rec := l_dist_trans_rec;
1578 
1579    return TRUE;
1580 
1581 EXCEPTION
1582 
1583    when others then
1584 
1585           if g_token1 is null then
1586                fa_srvr_msg.add_message(calling_fn => l_calling_fn
1587                                       ,name       => g_msg_name
1588                                       ,p_log_level_rec => p_log_level_rec);
1589           else
1590                fa_srvr_msg.add_message(calling_fn => l_calling_fn
1591                                       ,name       => g_msg_name
1592                                       ,token1     => g_token1
1593                                       ,value1     => g_value1
1594                                       ,p_log_level_rec => p_log_level_rec);
1595           end if;
1596 
1597           fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
1598                     ,p_log_level_rec => g_log_level_rec);
1599 
1600           return FALSE;
1601 
1602 END do_all_books_retirement;
1603 
1604 
1605 FUNCTION do_sub_retirement
1606         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
1607         ,px_dist_trans_rec            in out NOCOPY FA_API_TYPES.trans_rec_type
1608         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
1609         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
1610         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
1611         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
1612         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
1613         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
1614         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
1615         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
1616         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
1617         ,p_mrc_sob_type_code          in     VARCHAR2
1618         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
1619 -- will return retirement_id of px_asset_retire_rec
1620 IS
1621 
1622    -- local asset info
1623    l_trans_rec         FA_API_TYPES.trans_rec_type;
1624    l_dist_trans_rec    FA_API_TYPES.trans_rec_type;
1625    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
1626    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
1627    l_asset_type_rec    FA_API_TYPES.asset_type_rec_type;
1628    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
1629    l_asset_fin_mrc_rec FA_API_TYPES.asset_fin_rec_type;
1630    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
1631    l_asset_retire_mrc_rec  FA_API_TYPES.asset_retire_rec_type;
1632    l_asset_dist_tbl    FA_API_TYPES.asset_dist_tbl_type;
1633    l_subcomp_tbl       FA_API_TYPES.subcomp_tbl_type;
1634    l_inv_tbl           FA_API_TYPES.inv_tbl_type;
1635    l_period_rec        FA_API_TYPES.period_rec_type;
1636 
1637    l_asset_cat_rec     FA_API_TYPES.asset_cat_rec_type;
1638 
1639    l_rate                      number;
1640 
1641    l_fraction_remaining        number;
1642    l_deprn_rounding_flag       varchar2(30);
1643    l_period_counter_fully_ret  number;
1644 
1645 
1646    /* BUG#2919562 */
1647    l_trx_rate                  number;
1648 
1649    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.do_sub_retirement';
1650 
1651 BEGIN
1652 
1653 
1654    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin ', l_calling_fn, p_log_level_rec); end if;
1655    -- ****************************************************
1656    -- **  Assign input parameters to local rec/tbl types
1657    -- ****************************************************
1658    l_trans_rec         := px_trans_rec;
1659    l_dist_trans_rec    := px_dist_trans_rec;
1660    l_asset_hdr_rec     := p_asset_hdr_rec;
1661    l_asset_desc_rec    := p_asset_desc_rec;
1662    l_asset_type_rec    := p_asset_type_rec;
1663    l_asset_fin_rec     := p_asset_fin_rec;
1664    l_asset_retire_rec  := px_asset_retire_rec;
1665    l_asset_retire_mrc_rec := px_asset_retire_rec;
1666    l_asset_dist_tbl    := p_asset_dist_tbl;
1667    l_subcomp_tbl       := p_subcomp_tbl;
1668    l_inv_tbl           := p_inv_tbl;
1669    l_period_rec        := p_period_rec;
1670 
1671    -- ***************************
1672    -- **  Pop local rec types
1673    -- ***************************
1674 
1675    -- set the sob_id and currency context
1676    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
1677    fnd_client_info.set_currency_context(to_char(l_asset_hdr_rec.set_of_books_id));
1678 
1679    -- pop asset_fin_rec
1680    -- get fa_books row where transaction_header_id_out is null
1681    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_fin_rec', '', p_log_level_rec); end if;
1682    if not FA_UTIL_PVT.get_asset_fin_rec
1683           (p_asset_hdr_rec         => l_asset_hdr_rec
1684           ,px_asset_fin_rec        => l_asset_fin_mrc_rec
1685           ,p_transaction_header_id => NULL
1686           ,p_mrc_sob_type_code     => p_mrc_sob_type_code
1687 
1688           ,p_log_level_rec => p_log_level_rec) then
1689               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1690    end if;
1691 
1692    -- pop asset_cat_rec
1693    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_cat_rec', '', p_log_level_rec); end if;
1694    if not FA_UTIL_PVT.get_asset_cat_rec
1695           (p_asset_hdr_rec      => l_asset_hdr_rec
1696           ,px_asset_cat_rec     => l_asset_cat_rec
1697           ,p_date_effective     => NULL
1698           ,p_log_level_rec => p_log_level_rec) then
1699               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1700    end if;
1701 
1702    -- ***************************
1703    -- **  Do basic calculation
1704    -- ***************************
1705 
1706    if p_mrc_sob_type_code = 'R' then
1707 
1708       if l_asset_fin_rec.cost <> 0 then
1709 
1710          -- rate is calculated as reporting cost divided by primary cost
1711          l_rate := l_asset_fin_mrc_rec.cost / l_asset_fin_rec.cost;
1712 
1713       else
1714 
1715        /********* BUG#2919562
1716         -- get average rate from the latest transaction record
1717         -- when cost is zero
1718         select br1.avg_exchange_rate
1719         into l_rate
1720         from fa_mc_books_rates br1
1721         where br1.asset_id              = l_asset_hdr_rec.asset_id
1722           and br1.book_type_code        = l_asset_hdr_rec.book_type_code
1723           and br1.set_of_books_id       = l_asset_hdr_rec.set_of_books_id
1724           and br1.transaction_header_id =
1725              (select max(br2.transaction_header_id)
1726               from fa_mc_books_rates br2
1727               where br2.asset_id        = l_asset_hdr_rec.asset_id
1728                 and br2.book_type_code  = l_asset_hdr_rec.book_type_code
1729                 and br2.set_of_books_id = l_asset_hdr_rec.set_of_books_id);
1730        *****/
1731 
1732           l_rate := 1;
1733 
1734 
1735       end if;
1736 
1737        /* BUG#2919562 */
1738       if not FA_MC_UTIL_PVT.get_trx_rate
1739               (p_prim_set_of_books_id      => g_primary_set_of_books_id,
1740                p_reporting_set_of_books_id => l_asset_hdr_rec.set_of_books_id,
1741                px_exchange_date            => l_asset_retire_rec.date_retired,
1742                p_book_type_code            => l_asset_hdr_rec.book_type_code,
1743                px_rate                     => l_trx_rate
1744                ,p_log_level_rec => p_log_level_rec) then return false;
1745 
1746       end if;
1747 
1748    else
1749 
1750       l_rate := 1;
1751       l_trx_rate := 1;
1752 
1753    end if;
1754 
1755    -- convert the financial amounts using the retrieved rate
1756    -- cost_retired is calculated as primary cost_retired multiplied by l_rate
1757    l_asset_retire_mrc_rec.cost_retired     := l_asset_retire_rec.cost_retired
1758                                               * l_rate;
1759    l_asset_retire_mrc_rec.proceeds_of_sale := l_asset_retire_rec.proceeds_of_sale
1760                                               * l_trx_rate;
1761    l_asset_retire_mrc_rec.cost_of_removal  := l_asset_retire_rec.cost_of_removal
1762                                               * l_trx_rate;
1763 
1764    -- round the converted amounts
1765    if not FA_UTILS_PKG.faxrnd(x_amount => l_asset_retire_mrc_rec.cost_retired
1766                              ,x_book   => l_asset_hdr_rec.book_type_code
1767 ,p_log_level_rec => p_log_level_rec) then
1768                                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
1769    end if;
1770    if not FA_UTILS_PKG.faxrnd(x_amount => l_asset_retire_mrc_rec.proceeds_of_sale
1771                              ,x_book   => l_asset_hdr_rec.book_type_code
1772                              ,p_log_level_rec => p_log_level_rec) then
1773                                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
1774    end if;
1775    if not FA_UTILS_PKG.faxrnd(x_amount => l_asset_retire_mrc_rec.cost_of_removal
1776                              ,x_book   => l_asset_hdr_rec.book_type_code
1777                              ,p_log_level_rec => p_log_level_rec) then
1778                                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
1779    end if;
1780 
1781    -- if transaction is full retirement
1782    -- then set cost_retired to full cost
1783    -- so that there is no rounding issue.
1784 
1785    -- Bug 5925437: Added condition l_asset_fin_rec.cost <> 0
1786    if l_asset_fin_rec.cost = l_asset_retire_rec.cost_retired and
1787       l_asset_fin_rec.cost <> 0 then
1788 
1789       l_asset_retire_mrc_rec.cost_retired := l_asset_fin_mrc_rec.cost;
1790       --bug 5453230
1791       l_asset_retire_rec.units_retired := l_asset_desc_rec.current_units;
1792       l_asset_retire_mrc_rec.units_retired := l_asset_desc_rec.current_units;
1793 
1794    end if;
1795 
1796    -- ***************************
1797    -- **  Main
1798    -- ***************************
1799 
1800    if not do_sub_regular_retirement
1801           (px_trans_rec        => l_trans_rec
1802           ,p_asset_hdr_rec     => l_asset_hdr_rec
1803           ,p_asset_desc_rec    => l_asset_desc_rec
1804           ,p_asset_fin_rec     => l_asset_fin_mrc_rec
1805           ,px_asset_retire_rec => l_asset_retire_mrc_rec
1806           ,p_asset_dist_tbl    => l_asset_dist_tbl
1807           ,p_subcomp_tbl       => l_subcomp_tbl
1808           ,p_inv_tbl           => l_inv_tbl
1809           ,p_period_rec        => l_period_rec
1810           ,p_mrc_sob_type_code => p_mrc_sob_type_code
1811           ,p_log_level_rec => p_log_level_rec) then
1812               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1813    end if;
1814 
1815    if l_asset_retire_rec.units_retired = l_asset_desc_rec.current_units
1816       or l_asset_retire_rec.units_retired is null then
1817       -- if full unit retirement or cost (both full and partial) retirement
1818 
1819         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'treat this trx as regular', '', p_log_level_rec); end if;
1820         null;
1821 
1822    elsif l_asset_retire_rec.units_retired
1823          < l_asset_desc_rec.current_units then -- if partial unit retirement
1824 
1825         -- Due lue that came in in l_trans_to 3188851, need to insert CR row for members before calling dist api.
1826 
1827         --Need this for selection_thid
1828         SELECT fa_transaction_headers_s.nextval
1829           into l_dist_trans_rec.transaction_header_id /* 3513319 */
1830           FROM dual;
1831 
1832         if (l_asset_fin_rec.group_asset_id is not null) and
1833            (l_asset_retire_rec.recognize_gain_loss = 'NO') then
1834 
1835             /*
1836             -- due to 3513319.  Passing the thid straight to the dist api and keeping
1837             -- the value that came in in l_trans_rec.
1838             */
1839             if not FA_RETIREMENT_PVT.DO_RETIREMENT(
1840                        p_trans_rec         => l_trans_rec,
1841                        p_asset_retire_rec  => l_asset_retire_mrc_rec,
1842                        p_asset_hdr_rec     => l_asset_hdr_rec,
1843                        p_asset_type_rec    => l_asset_type_rec,
1844                        p_asset_cat_rec     => l_asset_cat_rec,
1845                        p_asset_fin_rec     => l_asset_fin_mrc_rec,
1846                        p_asset_desc_rec    => l_asset_desc_rec,
1847                        p_period_rec        => l_period_rec,
1848                        p_mrc_sob_type_code => p_mrc_sob_type_code,
1849                        p_calling_fn        => 'DO_RETIREMENT.CGLFR_CR_ONLY'
1850                        ,p_log_level_rec => p_log_level_rec) then
1851 
1852                 raise FND_API.G_EXC_UNEXPECTED_ERROR;
1853             end if;
1854         -- SLA
1855         -- else
1856         --    l_dist_trans_rec.transaction_header_id := NULL;
1857         end if; --group_asset_id <> null
1858 
1859         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'treat this trx as partial unit retirement', '', p_log_level_rec); end if;
1860 
1861         -- make a local copy of trans_rec and change trx_type_code
1862         -- this local copy of trans_rec will be used
1863         -- for distribution api and fautfr
1864         l_dist_trans_rec.transaction_date_entered :=
1865             l_trans_rec.transaction_date_entered;
1866         l_dist_trans_rec.source_transaction_header_id :=
1867             l_trans_rec.source_transaction_header_id;
1868         l_dist_trans_rec.mass_reference_id := l_trans_rec.mass_reference_id;
1869         l_dist_trans_rec.transaction_subtype := l_trans_rec.transaction_subtype;
1870         l_dist_trans_rec.transaction_key := l_trans_rec.transaction_key;
1871         l_dist_trans_rec.amortization_start_date :=
1872             l_trans_rec.amortization_start_date;
1873         l_dist_trans_rec.calling_interface := l_trans_rec.calling_interface;
1874         l_dist_trans_rec.who_info := l_trans_rec.who_info;
1875         l_dist_trans_rec.transaction_type_code := 'TRANSFER OUT';
1876 
1877         -- FYI: current_units is used as parameter of units_retired in Distribution API
1878         /* --Commenting this out for bug 3440308.  Don't c a use for it below, but if
1879            --some use was intended, then this logic is incorrect.
1880         l_asset_desc_rec.current_units := l_asset_retire_rec.units_retired;
1881            -- end 3440308 */
1882 
1883         -- FYI: call distribution api
1884         -- only when set of books is a primary GL book
1885         -- and FA book is a corporate book
1886         -- and transaction is a partial unit retirement.
1887         -- Assumption: fautfr in distribution API
1888         --             is handling all MRC part of adjustments table
1889         if (l_asset_dist_tbl.count > 0 )
1890            and
1891            (l_asset_hdr_rec.set_of_books_id
1892              = fa_cache_pkg.fazcbc_record.set_of_books_id)
1893            and
1894            (fa_cache_pkg.fazcbc_record.book_class='CORPORATE') then
1895 
1896              -- Call distribution API to process partial-unit retirement.
1897              -- do_distribution will handle TH, DH, AD and AH tables for TRANSFER OUT transaction
1898              -- and call 'fautfr' function in it.
1899 
1900              -- assuming that fautfr is inserting adjustment rows for TAX books.
1901              -- and calculate gain/loss is taking care of those for CORPORATE book.
1902 
1903              -- Required parameters for TRANSFER OUT transaction
1904              --   trans_rec: transaction_date_entered
1905              --   asset_hdr_rec: asset_id, book_type_code(only CORPORATE)
1906              --   asset_dist_tbl: distribution_id, trx_units
1907 
1908              if g_log_level_rec.statement_level then
1909                 fa_debug_pkg.add(l_calling_fn, 'trx_type_code:', l_dist_trans_rec.transaction_type_code, p_log_level_rec);
1910                 fa_debug_pkg.add(l_calling_fn, 'trx_date_entered:', l_dist_trans_rec.transaction_date_entered, p_log_level_rec);
1911                 fa_debug_pkg.add(l_calling_fn, 'asset_id:', l_asset_hdr_rec.asset_id, p_log_level_rec);
1912                 fa_debug_pkg.add(l_calling_fn, 'book_type_code:', l_asset_hdr_rec.book_type_code, p_log_level_rec);
1913              end if;
1914 
1915              /************* routine for debug
1916              for i in 1..l_asset_dist_tbl.count loop
1917 
1918                  if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'dist_id:', l_asset_dist_tbl(1).distribution_id, p_log_level_rec); end if;
1919                  if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'trx_units:', l_asset_dist_tbl(1).transaction_units, p_log_level_rec); end if;
1920 
1921              end loop;
1922              *************/
1923 
1924              if not FA_DISTRIBUTION_PVT.do_distribution
1925                     (px_trans_rec            => l_dist_trans_rec
1926                     ,px_asset_hdr_rec        => l_asset_hdr_rec
1927                     ,px_asset_cat_rec_new    => l_asset_cat_rec
1928                     ,px_asset_dist_tbl       => l_asset_dist_tbl
1929                     ,p_log_level_rec => p_log_level_rec) then
1930                         raise FND_API.G_EXC_UNEXPECTED_ERROR;
1931              end if;
1932 
1933         end if;
1934 
1935    end if; -- main
1936 
1937    -- fix for Bug 4966209
1938    -- call book cache to reset with right book
1939 
1940    -- call the cache for the book
1941    if not fa_cache_pkg.fazcbc(X_book => l_asset_hdr_rec.book_type_code) then
1942         raise FND_API.G_EXC_UNEXPECTED_ERROR;
1943    end if;
1944 
1945    --
1946    -- If this is member asset and Recognize Gain Loss is set
1947    -- to "NO", create adjustment entries right away and
1948    -- make sure gain loss won't process this retirement later.
1949    --
1950    if (l_asset_fin_rec.group_asset_id is not null) and
1951       (nvl(l_asset_retire_rec.recognize_gain_loss, 'NO') = 'NO') then
1952 
1953 -- ENERGY: Decide to fetch recognize gain loss using group asset id
1954 -- ENERGY: or not.
1955 
1956       if not FA_RETIREMENT_PVT.DO_RETIREMENT(
1957                        p_trans_rec         => l_trans_rec,
1958                        p_asset_retire_rec  => l_asset_retire_mrc_rec,
1959                        p_asset_hdr_rec     => l_asset_hdr_rec,
1960                        p_asset_type_rec    => l_asset_type_rec,
1961                        p_asset_cat_rec     => l_asset_cat_rec,
1962                        p_asset_fin_rec     => l_asset_fin_mrc_rec,
1963                        p_asset_desc_rec    => l_asset_desc_rec,
1964                        p_period_rec        => l_period_rec,
1965                        p_mrc_sob_type_code => p_mrc_sob_type_code,
1966                        p_calling_fn        => l_calling_fn
1967                        ,p_log_level_rec => p_log_level_rec) then
1968 
1969          raise FND_API.G_EXC_UNEXPECTED_ERROR;
1970       end if;
1971 
1972    end if;
1973 
1974 
1975    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'do_sub_retirement: retirement_id: ', l_asset_retire_rec.retirement_id, p_log_level_rec); end if;
1976 
1977    l_asset_retire_rec.retirement_id := l_asset_retire_mrc_rec.retirement_id;
1978    px_asset_retire_rec := l_asset_retire_rec;
1979    px_trans_rec := l_trans_rec;
1980    px_dist_trans_rec := l_dist_trans_rec;
1981 
1982    return TRUE;
1983 
1984 EXCEPTION
1985 
1986    when others then
1987 
1988           if g_token1 is null then
1989                fa_srvr_msg.add_message(calling_fn => l_calling_fn
1990                                       ,name       => g_msg_name
1991                                       ,p_log_level_rec => p_log_level_rec);
1992           else
1993                fa_srvr_msg.add_message(calling_fn => l_calling_fn
1994                                       ,name       => g_msg_name
1995                                       ,token1     => g_token1
1996                                       ,value1     => g_value1
1997                                       ,p_log_level_rec => p_log_level_rec);
1998           end if;
1999 
2000           fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
2001                                    ,p_log_level_rec => g_log_level_rec);
2002 
2003           return FALSE;
2004 
2005 END do_sub_retirement;
2006 
2007 FUNCTION do_sub_regular_retirement
2008         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
2009         ,p_asset_hdr_rec              in            FA_API_TYPES.asset_hdr_rec_type
2010         ,p_asset_desc_rec             in            FA_API_TYPES.asset_desc_rec_type
2011         ,p_asset_fin_rec              in            FA_API_TYPES.asset_fin_rec_type
2012         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
2013         ,p_asset_dist_tbl             in            FA_API_TYPES.asset_dist_tbl_type
2014         ,p_subcomp_tbl                in            FA_API_TYPES.subcomp_tbl_type
2015         ,p_inv_tbl                    in            FA_API_TYPES.inv_tbl_type
2016         ,p_period_rec                 in            FA_API_TYPES.period_rec_type
2017         ,p_mrc_sob_type_code          in            VARCHAR2
2018         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
2019 IS
2020 
2021    p_sob_id                    number;
2022    p_book_type_code            varchar2(15);
2023    p_asset_id                  number;
2024    p_dpis                      date;
2025 
2026 
2027    -- changed fa_lookups to fa_lookups_b for high-cost sql fix
2028    CURSOR bk_cursor IS
2029      select bk.transaction_header_id_in
2030            ,bk.Allowed_Deprn_Limit_Amount
2031            ,bk.date_effective
2032            ,itc.basis_reduction_rate
2033            ,ce.limit
2034            ,lk.lookup_type
2035      from   fa_books     bk
2036            ,fa_itc_rates itc
2037            ,fa_ceilings  ce
2038            ,fa_lookups_b   lk
2039      where  bk.asset_id = p_asset_id
2040      and    bk.book_type_code = p_book_type_code
2041      and    bk.date_ineffective is null
2042      and    bk.itc_amount_id = itc.itc_amount_id(+)
2043      and    bk.ceiling_name  = ce.ceiling_name(+)
2044      and    bk.date_placed_in_service between
2045                nvl(ce.start_date, bk.date_placed_in_service)
2046                and nvl(ce.end_date, bk.date_placed_in_service)
2047      and    bk.ceiling_name = lk.lookup_code(+)
2048      and    p_mrc_sob_type_code = 'P'
2049      UNION
2050      select bk.transaction_header_id_in
2051            ,bk.Allowed_Deprn_Limit_Amount
2052            ,bk.date_effective
2053            ,itc.basis_reduction_rate
2054            ,ce.limit
2055            ,lk.lookup_type
2056      from   fa_mc_books     bk
2057            ,fa_itc_rates itc
2058            ,fa_ceilings  ce
2059            ,fa_lookups_b   lk
2060      where  bk.asset_id = p_asset_id
2061      and    bk.book_type_code = p_book_type_code
2062      and    bk.date_ineffective is null
2063      and    bk.itc_amount_id = itc.itc_amount_id(+)
2064      and    bk.ceiling_name  = ce.ceiling_name(+)
2065      and    bk.date_placed_in_service between
2066                nvl(ce.start_date, bk.date_placed_in_service)
2067                and nvl(ce.end_date, bk.date_placed_in_service)
2068      and    bk.ceiling_name = lk.lookup_code(+)
2069      and    p_mrc_sob_type_code <> 'P'
2070      and    bk.set_of_books_id = p_sob_id;
2071 
2072    bk_rec   bk_cursor%ROWTYPE;
2073 
2074 
2075    CURSOR salvage_percent_deprn_limits IS
2076       select  cbd.percent_salvage_value
2077              ,cbd.use_deprn_limits_flag
2078              ,cbd.allowed_deprn_limit
2079              ,cbd.special_deprn_limit_amount
2080       from fa_additions_b            fad
2081           ,fa_category_book_defaults cbd
2082       where fad.asset_id = p_asset_id
2083       and   cbd.category_id = fad.asset_category_id
2084       and   cbd.book_type_code = p_book_type_code
2085       and   p_dpis
2086             between cbd.start_dpis
2087                 and nvl(cbd.end_dpis,to_date('31-12-4712','DD-MM-YYYY'));
2088 
2089    limit_rec   salvage_percent_deprn_limits%ROWTYPE;
2090 
2091    CURSOR dh_cursor (p_asset_id       in number
2092                     ,p_book_type_code in varchar2) IS
2093         select  distribution_id,
2094                 book_type_code,
2095                 asset_id,
2096                 units_assigned,
2097                 date_effective,
2098                 code_combination_id,
2099                 location_id,
2100                 transaction_header_id_in,
2101                 last_update_date,
2102                 last_updated_by,
2103                 date_ineffective,
2104                 assigned_to,
2105                 transaction_header_id_out,
2106                 transaction_units,
2107                 retirement_id,
2108                 last_update_login
2109         from fa_distribution_history
2110         where asset_id = p_asset_id
2111         and book_type_code = p_book_type_code
2112         and date_ineffective is null;
2113 
2114    CURSOR c_get_reserve is                                -- ENERGY
2115       select deprn_reserve                                -- ENERGY
2116       from   fa_books_summary                             -- ENERGY
2117       where  asset_id = p_asset_id                        -- ENERGY
2118       and    book_type_code = p_book_type_code            -- ENERGY
2119       and    period_counter = p_period_rec.period_counter -- ENERGY
2120       and    transaction_header_id_out is null;           -- ENERGY
2121 
2122    CURSOR c_get_group_method_info is                  -- ENERGY
2123       select db.rule_name                             -- ENERGY
2124       from   fa_deprn_basis_rules db                  -- ENERGY
2125            , fa_methods mt                            -- ENERGY
2126            , fa_books bk                              -- ENERGY
2127       where  bk.asset_id = p_asset_fin_rec.group_asset_id           -- ENERGY
2128       and    bk.book_type_code = p_asset_hdr_rec.book_type_code   -- ENERGY
2129       and    bk.transaction_header_id_out is null                      -- ENERGY
2130       and    bk.deprn_method_code = mt.method_code                     -- ENERGY
2131       and    nvl(bk.life_in_months, -99) = nvl(mt.life_in_months, -99) -- ENERGY
2132       and    mt.deprn_basis_rule_id = db.deprn_basis_rule_id;          -- ENERGY
2133 
2134 
2135    -- local asset info
2136    l_trans_rec        FA_API_TYPES.trans_rec_type;
2137    l_asset_hdr_rec    FA_API_TYPES.asset_hdr_rec_type;
2138    l_asset_desc_rec   FA_API_TYPES.asset_desc_rec_type;
2139    l_asset_type_rec   FA_API_TYPES.asset_type_rec_type;
2140    l_asset_fin_rec    FA_API_TYPES.asset_fin_rec_type;
2141    l_asset_retire_rec FA_API_TYPES.asset_retire_rec_type;
2142    l_asset_dist_tbl   FA_API_TYPES.asset_dist_tbl_type;
2143    l_subcomp_tbl      FA_API_TYPES.subcomp_tbl_type;
2144    l_inv_tbl          FA_API_TYPES.inv_tbl_type;
2145    l_period_rec       FA_API_TYPES.period_rec_type;
2146    l_asset_deprn_rec  FA_API_TYPES.asset_deprn_rec_type;
2147 
2148    l_asset_cat_rec             FA_API_TYPES.asset_cat_rec_type;
2149 
2150    l_rowid                     ROWID;
2151    l_Fraction_Remaining        number;
2152    l_deprn_rounding_flag       varchar2(30);
2153    l_period_counter_fully_ret  number := null;
2154 
2155    l_percent_salvage_value     number := 0;
2156 
2157    l_adjusted_cost_new         number := 0;
2158    l_cost_new                  number := 0;
2159    l_salvage_value_new         number := 0;
2160    l_unrevalued_cost_new       number := 0;
2161    l_recoverable_cost_new      number := 0;
2162    l_recoverable               number := 0;
2163    l_adjusted_rec_cost         number := 0;
2164    l_eofy_reserve_new          number := 0;
2165    l_reval_amort_basis_new     number := 0;
2166 
2167    l_reserve_retired           number;                         -- ENERGY
2168    l_group_db_rule_name        varchar2(80);                   -- ENERGY
2169 
2170    l_status                    boolean := TRUE;
2171 
2172    l_th_rowid                  rowid;
2173    l_bk_rowid                  rowid;
2174 
2175    l_retirement_pending_flag   varchar2(3);
2176    l_rate_in_use               number;   -- Bug 6665510: FP: Japan Tax Reform Project
2177    l_calling_fn  varchar2(80) := 'FA_RETIREMENT_PUB.do_sub_regular_retirement';
2178 
2179 BEGIN
2180 
2181    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin ', l_calling_fn, p_log_level_rec); end if;
2182 
2183    -- ****************************************************
2184    -- **  Assign input parameters to local rec/tbl types
2185    -- ****************************************************
2186    l_trans_rec        := px_trans_rec;
2187    l_asset_hdr_rec    := p_asset_hdr_rec;
2188    l_asset_desc_rec   := p_asset_desc_rec;
2189    l_asset_fin_rec    := p_asset_fin_rec;
2190    l_asset_retire_rec := px_asset_retire_rec;
2191    l_asset_dist_tbl   := p_asset_dist_tbl;
2192    l_subcomp_tbl      := p_subcomp_tbl;
2193    l_inv_tbl          := p_inv_tbl;
2194    l_period_rec       := p_period_rec;
2195 
2196    -- ***************************
2197    -- **  Main
2198    -- ***************************
2199    -- determine deprn_rounding_flag and transaction_type_code
2200    -- between full retirement and partial retirement
2201    -- after evaluating units_retired vs. current_units
2202    -- and cost_retired vs. current_cost
2203 
2204    -- cost retirements on 0 cost assets are in fact
2205    -- treated as full retirements. (bug1565792)
2206 
2207    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'set_of_books_id: ', l_asset_hdr_rec.set_of_books_id, p_log_level_rec); end if;
2208 
2209    -- pop asset_fin_rec
2210    -- get fa_books row where transaction_header_id_out is null
2211    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_fin_rec', '', p_log_level_rec); end if;
2212    if not FA_UTIL_PVT.get_asset_fin_rec
2213           (p_asset_hdr_rec         => l_asset_hdr_rec,
2214            px_asset_fin_rec        => l_asset_fin_rec,
2215            p_transaction_header_id => NULL,
2216            p_mrc_sob_type_code     => p_mrc_sob_type_code
2217            ,p_log_level_rec => p_log_level_rec) then
2218       raise FND_API.G_EXC_UNEXPECTED_ERROR;
2219    end if;
2220 
2221    if not FA_UTIL_PVT.get_asset_type_rec
2222           (p_asset_hdr_rec      => l_asset_hdr_rec,
2223            px_asset_type_rec    => l_asset_type_rec
2224            ,p_log_level_rec => p_log_level_rec) then
2225       raise FND_API.G_EXC_UNEXPECTED_ERROR;
2226    end if;
2227 
2228    if (p_mrc_sob_type_code <> 'R') then
2229 
2230        if ((l_asset_retire_rec.units_retired = l_asset_desc_rec.current_units
2231            )
2232            or (l_asset_retire_rec.cost_retired = l_asset_fin_rec.cost
2233               and l_asset_retire_rec.cost_retired <> 0
2234               )
2235            or (l_asset_retire_rec.cost_retired = l_asset_fin_rec.cost
2236               and l_asset_retire_rec.cost_retired = 0
2237               and l_asset_retire_rec.units_retired is NULL
2238               )) then
2239 
2240           l_trans_rec.transaction_type_code := 'FULL RETIREMENT';
2241           l_period_counter_fully_ret := l_period_rec.period_counter;
2242           l_deprn_rounding_flag := 'RET';
2243 
2244           -- In case of TAX book, units_retired must always be NULL
2245           -- so that Calculate Gain/Loss program will not lead to
2246           -- distribution logic.
2247           if fa_cache_pkg.fazcbc_record.book_class='TAX' then
2248              l_asset_retire_rec.units_retired := NULL;
2249           else
2250              l_asset_retire_rec.units_retired := l_asset_desc_rec.current_units;
2251           end if;
2252 
2253        else
2254           l_trans_rec.transaction_type_code := 'PARTIAL RETIREMENT';
2255           l_period_counter_fully_ret := NULL;
2256           l_deprn_rounding_flag := 'RET';
2257 
2258        end if;
2259 
2260        l_trans_rec.who_info.creation_date := sysdate;
2261        l_trans_rec.who_info.last_update_date := sysdate;
2262 
2263        -- Fix for Bug #3187975.  Do Polish rule validations.
2264        -- Need to do this after transaction_type_code is derived.
2265        if not FA_ASSET_VAL_PVT.validate_polish (
2266           p_transaction_type_code => l_trans_rec.transaction_type_code,
2267           p_method_code           => l_asset_fin_rec.deprn_method_code,
2268           p_life_in_months        => l_asset_fin_rec.life_in_months,
2269           p_asset_type            => l_asset_type_rec.asset_type,
2270           p_bonus_rule            => l_asset_fin_rec.bonus_rule,
2271           p_ceiling_name          => l_asset_fin_rec.ceiling_name,
2272           p_deprn_limit_type      => l_asset_fin_rec.deprn_limit_type,
2273           p_group_asset_id        => l_asset_fin_rec.group_asset_id,
2274           p_calling_fn            =>
2275                                'FA_RETIREMENT_PUB.do_sub_regular_retirement'
2276                                ,p_log_level_rec => p_log_level_rec) then
2277           g_msg_name := null;
2278           g_token1 := null;
2279           raise FND_API.G_EXC_ERROR;
2280        end if;
2281 
2282        if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'do fa_transaction_headers_pkg.insert_row', '', p_log_level_rec); end if;
2283        fa_transaction_headers_pkg.insert_row
2284              (x_rowid                          => l_th_rowid,
2285               x_transaction_header_id          => l_trans_rec.transaction_header_id,
2286               x_book_type_code                 => l_asset_hdr_rec.book_type_code,
2287               x_asset_id                       => l_asset_hdr_rec.asset_id,
2288               x_transaction_type_code          => l_trans_rec.transaction_type_code,
2289               x_transaction_date_entered       => l_trans_rec.transaction_date_entered,
2290               x_date_effective                 => l_trans_rec.who_info.creation_date,
2291               x_last_update_date               => l_trans_rec.who_info.last_update_date,
2292               x_last_updated_by                => l_trans_rec.who_info.last_updated_by,
2293               x_transaction_name               => l_trans_rec.transaction_name,
2294               x_invoice_transaction_id         => g_inv_trans_rec.invoice_transaction_id,
2295               x_source_transaction_Header_id   => l_trans_rec.source_transaction_header_id,
2296               x_mass_reference_id              => l_trans_rec.mass_reference_id,
2297               x_last_Update_login              => l_trans_rec.who_info.last_update_login,
2298               x_transaction_subtype            => null, -- l_trans_rec.transaction_subtype
2299               x_Attribute1                     => l_trans_rec.desc_flex.attribute1,
2300               x_Attribute2                     => l_trans_rec.desc_flex.attribute2,
2301               x_Attribute3                     => l_trans_rec.desc_flex.attribute3,
2302               x_Attribute4                     => l_trans_rec.desc_flex.attribute4,
2303               x_Attribute5                     => l_trans_rec.desc_flex.attribute5,
2304               x_Attribute6                     => l_trans_rec.desc_flex.attribute6,
2305               x_Attribute7                     => l_trans_rec.desc_flex.attribute7,
2306               x_Attribute8                     => l_trans_rec.desc_flex.attribute8,
2307               x_Attribute9                     => l_trans_rec.desc_flex.attribute9,
2308               x_Attribute10                    => l_trans_rec.desc_flex.attribute10,
2309               x_Attribute11                    => l_trans_rec.desc_flex.attribute11,
2310               x_Attribute12                    => l_trans_rec.desc_flex.attribute12,
2311               x_Attribute13                    => l_trans_rec.desc_flex.attribute13,
2312               x_Attribute14                    => l_trans_rec.desc_flex.attribute14,
2313               x_Attribute15                    => l_trans_rec.desc_flex.attribute15,
2314               x_attribute_category_code        => l_trans_rec.desc_flex.attribute_category_code,
2315               x_transaction_key                => 'R', -- l_trans_rec.transaction_key
2316               x_mass_transaction_id            => l_trans_rec.mass_transaction_id,
2317               x_calling_interface               => l_trans_rec.calling_interface,
2318               x_event_id                       => l_trans_rec.event_id,
2319               x_return_status                  => l_status,
2320               x_calling_fn                     => l_calling_fn
2321               ,p_log_level_rec => p_log_level_rec);
2322 
2323       -- returning trans_rec to reuse for mrc tables
2324       px_trans_rec := l_trans_rec;
2325 
2326    end if; -- reporting_flag
2327 
2328    if (l_asset_fin_rec.cost = 0) then
2329      l_fraction_remaining := 0;
2330    else
2331      l_fraction_remaining
2332                := 1 - l_asset_retire_rec.cost_retired/l_asset_fin_rec.cost;
2333    end if;
2334 
2335    l_cost_new := l_asset_fin_rec.cost - l_asset_retire_rec.cost_retired;
2336    l_unrevalued_cost_new := l_asset_fin_rec.unrevalued_cost * l_fraction_remaining;
2337    l_eofy_reserve_new := l_asset_fin_rec.eofy_reserve * l_fraction_remaining;
2338 
2339    if (fa_cache_pkg.fazcbc_record.retire_reval_reserve_flag='YES') and
2340       (l_asset_fin_rec.Reval_Amortization_Basis is not null) then
2341        l_reval_amort_basis_new := l_asset_fin_rec.Reval_Amortization_Basis * l_fraction_remaining;
2342    else
2343        l_reval_amort_basis_new := l_asset_fin_rec.Reval_Amortization_Basis;
2344    end if;
2345 
2346    -- BUG# 3933689
2347    -- correcting logic for salvage to account for type
2348    -- note that cost is already rounded coming in
2349    -- so we just need to floor or round the salvage
2350    -- prior to rec_cost derivation
2351 
2352    if (l_asset_fin_rec.salvage_type = 'PCT') then
2353        l_salvage_value_new :=
2354           l_cost_new * nvl(l_asset_fin_rec.percent_salvage_value, 0);
2355 
2356        fa_round_pkg.fa_ceil(l_salvage_value_new,
2357                             l_asset_hdr_rec.book_type_code
2358                             ,p_log_level_rec => p_log_level_rec);
2359 
2360    else
2361       l_salvage_value_new := l_asset_fin_rec.salvage_value * l_fraction_remaining;
2362       if not FA_UTILS_PKG.faxrnd(x_amount => l_salvage_value_new,
2363                                  x_book   => l_asset_hdr_rec.book_type_code
2364                                  ,p_log_level_rec => p_log_level_rec) then
2365          raise FND_API.G_EXC_UNEXPECTED_ERROR;
2366       end if;
2367 
2368    end if;
2369 
2370    p_sob_id         := l_asset_hdr_rec.set_of_books_id;
2371    p_asset_id       := l_asset_hdr_rec.asset_id;
2372    p_book_type_code := l_asset_hdr_rec.book_type_code;
2373 
2374    OPEN bk_cursor;
2375    FETCH bk_cursor INTO bk_rec;
2376    CLOSE bk_cursor;
2377 
2378    l_recoverable := l_cost_new -
2379                     l_salvage_value_new -
2380                     nvl(l_asset_fin_rec.itc_basis, 0) *
2381                     nvl(bk_rec.basis_reduction_rate, 0);
2382 
2383    if (l_asset_fin_rec.cost = l_asset_retire_rec.cost_retired) then
2384      l_recoverable_cost_new := 0;
2385    else
2386      -- set deprn_rounding_flag for partial cost retirement
2387      l_deprn_rounding_flag := 'RET';
2388 
2389      if (bk_rec.lookup_type = 'RECOVERABLE COST CEILING') then
2390        l_recoverable_cost_new := least(l_recoverable, bk_rec.limit);
2391      else
2392        l_recoverable_cost_new := l_recoverable;
2393      end if;
2394    end if;
2395 
2396    p_dpis           := p_asset_fin_rec.date_placed_in_service;
2397 
2398 
2399 --   OPEN salvage_percent_deprn_limits;
2400 --   FETCH salvage_percent_deprn_limits INTO limit_rec;
2401 --   CLOSE salvage_percent_deprn_limits;
2402 
2403    -- now set Japan deprn_limits
2404    -- don't touch salvage value
2405 
2406    if (l_trans_rec.transaction_type_code = 'PARTIAL RETIREMENT') then
2407 
2408       if (l_asset_fin_rec.deprn_limit_type = 'PCT') then
2409          l_asset_fin_rec.allowed_deprn_limit_amount := l_cost_new -
2410                               l_cost_new * nvl(l_asset_fin_rec.allowed_deprn_limit, 0);
2411 
2412          if (l_asset_fin_rec.allowed_deprn_limit_amount <> 0) then
2413             fa_round_pkg.fa_ceil(l_asset_fin_rec.allowed_deprn_limit_amount,
2414                                  l_asset_hdr_rec.book_type_code
2415                                  ,p_log_level_rec => p_log_level_rec);
2416          end if;
2417 
2418          l_asset_fin_rec.adjusted_recoverable_cost :=
2419                                 l_cost_new - l_asset_fin_rec.allowed_deprn_limit_amount;
2420 
2421       elsif (l_asset_fin_rec.deprn_limit_type = 'AMT') then
2422 
2423          l_asset_fin_rec.adjusted_recoverable_cost :=
2424                                 l_cost_new - l_asset_fin_rec.allowed_deprn_limit_amount;
2425       else
2426          l_asset_fin_rec.adjusted_recoverable_cost := l_recoverable_cost_new;
2427       end if;
2428 
2429       fa_round_pkg.fa_floor(l_asset_fin_rec.adjusted_recoverable_cost,
2430                             l_asset_hdr_rec.book_type_code
2431                             ,p_log_level_rec => p_log_level_rec);
2432       -- BUG# 4942017
2433       -- Period_Counter_Fully_Retired was not populated for
2434       -- reporting books
2435       l_period_counter_fully_ret := NULL;
2436 
2437    else -- Trx_Type_code = 'FULL RETIREMENT'
2438 
2439      -- BUG# 3371210
2440      -- leave the percent unchanged
2441      --   l_asset_fin_rec.percent_salvage_value := 0;
2442      l_asset_fin_rec.adjusted_recoverable_cost := 0;
2443 
2444      -- BUG# 4942017
2445      -- Period_Counter_Fully_Retired was not populated for
2446      -- reporting books
2447      l_period_counter_fully_ret := l_period_rec.period_counter;
2448 
2449    end if;
2450 
2451 --bug fix 3982941 starts
2452    if(l_asset_fin_rec.Adjusted_Cost = l_asset_fin_rec.Recoverable_Cost)
2453     then
2454           l_adjusted_cost_new := l_Recoverable_Cost_New;
2455     end if;
2456 --bug fix 3982941 ends
2457 
2458    -- rounding values
2459    if not FA_UTILS_PKG.faxrnd(x_amount => l_recoverable_cost_new,
2460                               x_book   => l_asset_hdr_rec.book_type_code
2461                               ,p_log_level_rec => p_log_level_rec) then
2462      raise FND_API.G_EXC_UNEXPECTED_ERROR;
2463    end if;
2464 
2465    if not FA_UTILS_PKG.faxrnd(x_amount => l_unrevalued_cost_new,
2466                               x_book   => l_asset_hdr_rec.book_type_code
2467                               ,p_log_level_rec => p_log_level_rec) then
2468          raise FND_API.G_EXC_UNEXPECTED_ERROR;
2469    end if;
2470 
2471    if not FA_UTILS_PKG.faxrnd(x_amount => l_asset_retire_rec.eofy_reserve,
2472                               x_book   => l_asset_hdr_rec.book_type_code
2473                               ,p_log_level_rec => p_log_level_rec) then
2474      raise FND_API.G_EXC_UNEXPECTED_ERROR;
2475    end if;
2476 
2477    if not FA_UTILS_PKG.faxrnd(x_amount => l_eofy_reserve_new,
2478                               x_book   => l_asset_hdr_rec.book_type_code
2479                               ,p_log_level_rec => p_log_level_rec) then
2480      raise FND_API.G_EXC_UNEXPECTED_ERROR;
2481    end if;
2482 
2483    if not FA_UTILS_PKG.faxrnd(x_amount => l_reval_amort_basis_new,
2484                               x_book   => l_asset_hdr_rec.book_type_code
2485                               ,p_log_level_rec => p_log_level_rec) then
2486      raise FND_API.G_EXC_UNEXPECTED_ERROR;
2487    end if;
2488 
2489    --
2490    -- Processing member asset if Recognize Gain Loss is NO.
2491    -- If it is YES, it will be processed at the time of Gain Loss.
2492    -- Below condition validating group asset id but it is not
2493    -- absolutely necessary since recognize gain loss should be null for
2494    -- stand alone assets.
2495    --
2496    if (l_asset_fin_rec.group_asset_id is not null) and
2497       (nvl(l_asset_retire_rec.recognize_gain_loss, 'NO') = 'NO') then
2498 
2499      l_retirement_pending_flag := 'NO';
2500      l_asset_retire_rec.detail_info.nbv_retired := 0;
2501 
2502      if (l_asset_retire_rec.limit_proceeds_flag = 'Y') and
2503         ((l_asset_fin_rec.recoverable_cost - l_recoverable_cost_new) <
2504          (l_asset_retire_rec.proceeds_of_sale - l_asset_retire_rec.cost_of_removal)) then
2505 
2506         l_asset_retire_rec.detail_info.gain_loss_amount := l_asset_retire_rec.proceeds_of_sale -
2507                                                            l_asset_retire_rec.cost_of_removal -
2508                                                            (l_asset_fin_rec.recoverable_cost -
2509                                                             l_recoverable_cost_new);
2510         l_asset_retire_rec.reserve_retired := 0;
2511         l_asset_retire_rec.detail_info.nbv_retired := l_asset_retire_rec.cost_retired;
2512 
2513      else
2514 
2515         l_asset_retire_rec.detail_info.gain_loss_amount := 0;
2516 
2517        if (p_log_level_rec.statement_level) then
2518           fa_debug_pkg.add(l_calling_fn, 'l_asset_deprn_rec.deprn_reserve',
2519                            l_asset_deprn_rec.deprn_reserve
2520                            ,p_log_level_rec => p_log_level_rec);
2521           fa_debug_pkg.add(l_calling_fn, 'l_asset_fin_rec.tracking_method',
2522                            l_asset_fin_rec.tracking_method
2523                            ,p_log_level_rec => p_log_level_rec);
2524           fa_debug_pkg.add(l_calling_fn, 'fa_cache_pkg.fazcdrd_record.rule_name',
2525                            fa_cache_pkg.fazcdrd_record.rule_name
2526                            ,p_log_level_rec => p_log_level_rec);
2527           fa_debug_pkg.add(l_calling_fn, 'l_asset_fin_rec.group_asset_id',
2528                            l_asset_fin_rec.group_asset_id
2529                            ,p_log_level_rec => p_log_level_rec);
2530        end if;
2531 
2532         OPEN c_get_group_method_info;
2533         FETCH c_get_group_method_info INTO l_group_db_rule_name;
2534         CLOSE c_get_group_method_info;
2535 
2536         if (p_log_level_rec.statement_level) then
2537            fa_debug_pkg.add(l_calling_fn, 'l_group_db_rule_name',
2538                             l_group_db_rule_name
2539                             ,p_log_level_rec => p_log_level_rec);
2540         end if;
2541 
2542         -- BUG# 6899255
2543         -- handle all allocate cases the same way rathern than just energy:
2544         -- (l_group_db_rule_name = 'ENERGY PERIOD END BALANCE')
2545         -- ENERGY
2546 
2547         if (nvl(l_asset_fin_rec.tracking_method, 'NO TRACK') = 'ALLOCATE') then
2548                                                                                               -- ENERGY
2549            OPEN c_get_reserve;                                                                -- ENERGY
2550            FETCH c_get_reserve INTO l_reserve_retired;                                        -- ENERGY
2551            CLOSE c_get_reserve;                                                               -- ENERGY
2552                                                                                               -- ENERGY
2553            l_reserve_retired := l_reserve_retired * l_asset_retire_rec.cost_retired/l_asset_fin_rec.cost; -- ENERGY
2554                                                                                               -- ENERGY
2555            if not FA_UTILS_PKG.faxrnd(x_amount => l_reserve_retired,                          -- ENERGY
2556                                       x_book   => l_asset_hdr_rec.book_type_code
2557                                       ,p_log_level_rec => p_log_level_rec) then        -- ENERGY
2558               raise FND_API.G_EXC_UNEXPECTED_ERROR;                                           -- ENERGY
2559            end if;                                                                            -- ENERGY
2560                                                                                               -- ENERGY
2561            l_asset_retire_rec.reserve_retired := l_reserve_retired;                           -- ENERGY
2562 
2563            if (p_log_level_rec.statement_level) then                                                            -- ENERGY
2564               fa_debug_pkg.add(l_calling_fn, 'l_asset_retire_rec.reserve_retired',            -- ENERGY
2565                                l_asset_retire_rec.reserve_retired
2566                                ,p_log_level_rec => p_log_level_rec);                           -- ENERGY
2567            end if;                                                                            -- ENERGY
2568 
2569         else                                                                                  -- ENERGY
2570            l_asset_retire_rec.reserve_retired := nvl(l_asset_retire_rec.cost_retired, 0) -
2571                                                  nvl(l_asset_retire_rec.proceeds_of_sale, 0) +
2572                                                  nvl(l_asset_retire_rec.cost_of_removal, 0);
2573         end if;
2574 
2575         l_asset_retire_rec.detail_info.nbv_retired := l_asset_retire_rec.cost_retired -
2576                                                       l_asset_retire_rec.reserve_retired;
2577      end if; -- (l_asset_retire_rec.limit_proceeds_flag = 'Y') and
2578 
2579      px_asset_retire_rec.reserve_retired := l_asset_retire_rec.reserve_retired;
2580 
2581    else -- Gain Loss will be processed by concurrent program
2582      l_retirement_pending_flag := 'YES';
2583 
2584      if (l_asset_fin_rec.group_asset_id is null) then
2585         l_asset_retire_rec.reserve_retired := to_number(null);
2586      end if;
2587    end if;
2588 
2589    -- Subtract Prior Year Reserve Retired from eofy_reserve
2590    if p_log_level_rec.statement_level then
2591        fa_debug_pkg.add(l_calling_fn, 'l_eofy_reserve_new', l_eofy_reserve_new
2592               ,p_log_level_rec => p_log_level_rec);
2593        fa_debug_pkg.add(l_calling_fn, 'l_asset_fin_rec.eofy_reserve',  l_asset_fin_rec.eofy_reserve
2594               ,p_log_level_rec => p_log_level_rec);
2595        fa_debug_pkg.add(l_calling_fn, 'l_asset_retire_rec.eofy_reserve', l_asset_retire_rec.eofy_reserve
2596               ,p_log_level_rec => p_log_level_rec);
2597    end if;
2598 
2599    if (l_asset_retire_rec.eofy_reserve is null) then
2600       l_asset_fin_rec.eofy_reserve := l_eofy_reserve_new;
2601    else
2602       l_asset_fin_rec.eofy_reserve := nvl(l_asset_fin_rec.eofy_reserve,0) -
2603                                       nvl(l_asset_retire_rec.eofy_reserve, 0);
2604    end if;
2605 
2606    -- Get asset_deprn_rec for Depreciable Basis Rule
2607    if (not FA_UTIL_PVT.get_asset_deprn_rec (
2608              p_asset_hdr_rec     => l_asset_hdr_rec,
2609              px_asset_deprn_rec  => l_asset_deprn_rec,
2610              p_period_counter    => l_period_rec.period_counter,
2611              p_mrc_sob_type_code => p_mrc_sob_type_code
2612              ,p_log_level_rec => p_log_level_rec))
2613    then
2614        fa_srvr_msg.add_message(calling_fn => l_calling_fn
2615               ,p_log_level_rec => p_log_level_rec);
2616        RETURN FALSE;
2617    end if;
2618 
2619    if (l_asset_fin_rec.group_asset_id is not null) and
2620       (l_group_db_rule_name = 'ENERGY PERIOD END BALANCE') then
2621       l_asset_deprn_rec.deprn_reserve := l_asset_deprn_rec.deprn_reserve - l_asset_retire_rec.reserve_retired; -- ENERGY
2622    end if;
2623 
2624 
2625    -----------------------------------
2626    -- Call Depreciable Basis Rule
2627    -----------------------------------
2628    if (not FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS
2629                     (p_event_type             => 'RETIREMENT',
2630                      p_asset_fin_rec_new      => l_asset_fin_rec,
2631                      p_asset_fin_rec_old      => l_asset_fin_rec,
2632                      p_asset_hdr_rec          => l_asset_hdr_rec,
2633                      p_asset_type_rec         => l_asset_type_rec,
2634                      p_trans_rec              => l_trans_rec,
2635                      p_period_rec             => l_period_rec,
2636                      p_asset_retire_rec       => l_asset_retire_rec,
2637                      p_asset_deprn_rec        => l_asset_deprn_rec,
2638                      p_recoverable_cost       => l_recoverable_cost_new,
2639                      p_salvage_value          => l_salvage_value_new,
2640                      p_mrc_sob_type_code      => p_mrc_sob_type_code,
2641                      px_new_adjusted_cost     => l_adjusted_cost_new,
2642                      px_new_raf               => l_asset_fin_rec.rate_adjustment_factor,
2643        px_new_formula_factor    => l_asset_fin_rec.formula_factor
2644        ,p_log_level_rec => p_log_level_rec)) then
2645        fa_srvr_msg.add_message(calling_fn =>
2646                             'FA_RETIREMENT_PUB.do_sub_regular_retirement'
2647                             ,p_log_level_rec => p_log_level_rec);
2648        RETURN FALSE;
2649    end if;
2650 
2651    -- Bug4343087: This rounding function call was relocated from the point before
2652    -- calling FA_CALC_DEPRN_BASIS1_PKG.CALL_DEPRN_BASIS.
2653    if not FA_UTILS_PKG.faxrnd(x_amount => l_adjusted_cost_new,
2654                               x_book   => l_asset_hdr_rec.book_type_code
2655                               ,p_log_level_rec => p_log_level_rec) then
2656      raise FND_API.G_EXC_UNEXPECTED_ERROR;
2657    end if;
2658 
2659    if p_log_level_rec.statement_level then
2660        fa_debug_pkg.add(l_calling_fn, 'l_adjusted_cost_new', l_adjusted_cost_new
2661               ,p_log_level_rec => p_log_level_rec);
2662    end if;
2663 
2664    if g_log_level_rec.statement_level then
2665      fa_debug_pkg.add(l_calling_fn, 'do fa_retirements_pkg.insert_row', '', p_log_level_rec);
2666      fa_debug_pkg.add(l_calling_fn, 'retirement_id: ', l_asset_retire_rec.retirement_id, p_log_level_rec);
2667      fa_debug_pkg.add(l_calling_fn, 'asset_id: ', l_asset_hdr_rec.asset_id, p_log_level_rec);
2668      fa_debug_pkg.add(l_calling_fn, 'book: ', l_asset_hdr_rec.book_type_code, p_log_level_rec);
2669      fa_debug_pkg.add(l_calling_fn, 'cost_retired: ', l_asset_retire_rec.cost_retired, p_log_level_rec);
2670    end if;
2671 
2672    fa_retirements_pkg.insert_row
2673          (x_rowid                     => l_asset_retire_rec.detail_info.row_id,
2674           X_Retirement_Id             => l_asset_retire_rec.retirement_id,
2675           X_Book_Type_Code            => l_asset_hdr_rec.book_type_code,
2676           X_Asset_Id                  => l_asset_hdr_rec.asset_id,
2677           X_Transaction_Header_Id_In  => l_trans_rec.transaction_header_id,
2678           X_Date_Retired              => l_asset_retire_rec.date_retired,
2679           X_Date_Effective            => l_trans_rec.who_info.creation_date,
2680           X_Cost_Retired              => l_asset_retire_rec.cost_retired,
2681           X_Status                    => l_asset_retire_rec.status,-- ? need to check
2682           X_Last_Update_Date          => l_trans_rec.who_info.last_update_date,
2683           X_Last_Updated_By           => l_trans_rec.who_info.last_updated_by,
2684           X_Ret_Prorate_Convention    => l_asset_retire_rec.retirement_prorate_convention,
2685           X_Transaction_Header_Id_Out => NULL,
2686           X_Units                     => l_asset_retire_rec.units_retired,
2687           X_Cost_Of_Removal           => l_asset_retire_rec.cost_of_removal,
2688           X_Nbv_Retired               => l_asset_retire_rec.detail_info.nbv_retired,
2689           X_Gain_Loss_Amount          => l_asset_retire_rec.detail_info.gain_loss_amount,
2690           X_Proceeds_Of_Sale          => l_asset_retire_rec.proceeds_of_sale,
2691           X_Gain_Loss_Type_Code       => l_asset_retire_rec.detail_info.gain_loss_type_code,
2692           X_Retirement_Type_Code      => l_asset_retire_rec.retirement_type_code,
2693           X_Itc_Recaptured            => l_asset_retire_rec.detail_info.itc_recaptured,
2694           X_Itc_Recapture_Id          => l_asset_retire_rec.detail_info.itc_recapture_id,
2695           X_Reference_Num             => l_asset_retire_rec.reference_num,
2696           X_Sold_To                   => l_asset_retire_rec.sold_to,
2697           X_Trade_In_Asset_Id         => l_asset_retire_rec.trade_in_asset_id,
2698           X_Stl_Method_Code           => l_asset_retire_rec.detail_info.stl_method_code,
2699           X_Stl_Life_In_Months        => l_asset_retire_rec.detail_info.stl_life_in_months,
2700           X_Stl_Deprn_Amount          => l_asset_retire_rec.detail_info.stl_deprn_amount,
2701           X_Created_By                => l_trans_rec.who_info.created_by,
2702           X_Creation_Date             => l_trans_rec.who_info.creation_date,
2703           X_Last_Update_Login         => l_trans_rec.who_info.last_update_login,
2704           X_Attribute1                => l_asset_retire_rec.desc_flex.attribute1,
2705           X_Attribute2                => l_asset_retire_rec.desc_flex.attribute2,
2706           X_Attribute3                => l_asset_retire_rec.desc_flex.attribute3,
2707           X_Attribute4                => l_asset_retire_rec.desc_flex.attribute4,
2708           X_Attribute5                => l_asset_retire_rec.desc_flex.attribute5,
2709           X_Attribute6                => l_asset_retire_rec.desc_flex.attribute6,
2710           X_Attribute7                => l_asset_retire_rec.desc_flex.attribute7,
2711           X_Attribute8                => l_asset_retire_rec.desc_flex.attribute8,
2712           X_Attribute9                => l_asset_retire_rec.desc_flex.attribute9,
2713           X_Attribute10               => l_asset_retire_rec.desc_flex.attribute10,
2714           X_Attribute11               => l_asset_retire_rec.desc_flex.attribute11,
2715           X_Attribute12               => l_asset_retire_rec.desc_flex.attribute12,
2716           X_Attribute13               => l_asset_retire_rec.desc_flex.attribute13,
2717           X_Attribute14               => l_asset_retire_rec.desc_flex.attribute14,
2718           X_Attribute15               => l_asset_retire_rec.desc_flex.attribute15,
2719           X_Attribute_Category_Code   => l_asset_retire_rec.desc_flex.attribute_category_code,
2720           X_Reval_Reserve_Retired     => l_asset_retire_rec.detail_info.reval_reserve_retired,
2721           X_Unrevalued_Cost_Retired   => l_asset_retire_rec.detail_info.unrevalued_cost_retired,
2722           X_Recognize_Gain_Loss       => l_asset_retire_rec.recognize_gain_loss,
2723           X_Recapture_Reserve_Flag    => l_asset_retire_rec.recapture_reserve_flag,
2724           X_Limit_Proceeds_Flag       => l_asset_retire_rec.limit_proceeds_flag,
2725           X_Terminal_Gain_Loss        => l_asset_retire_rec.terminal_gain_loss,
2726           X_Reserve_Retired           => l_asset_retire_rec.reserve_retired,
2727           X_Eofy_Reserve              => l_asset_retire_rec.eofy_reserve,
2728           X_Reduction_Rate            => l_asset_retire_rec.reduction_rate,
2729           X_Recapture_Amount          => l_asset_retire_rec.detail_info.recapture_amount,
2730           X_mrc_sob_type_code         => p_mrc_sob_type_code,
2731           x_calling_fn                => l_calling_fn
2732           ,p_log_level_rec => p_log_level_rec);
2733 
2734    -- return retirement_id if book is primary
2735    -- The retirement_ids of all reporting books will be same.
2736    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'retirement_id: ', l_asset_retire_rec.retirement_id, p_log_level_rec); end if;
2737    px_asset_retire_rec := l_asset_retire_rec;
2738 
2739    -- Bug 6665510: FP: Japan Tax Reform Project (Start)
2740    if nvl(fa_cache_pkg.fazccmt_record.GUARANTEE_RATE_METHOD_FLAG,'NO') = 'YES' then
2741 
2742       SELECT nvl(rate_in_use,0)
2743       INTO l_rate_in_use
2744       FROM fa_books
2745       WHERE asset_id = l_asset_hdr_rec.asset_id
2746       AND book_type_code = l_asset_hdr_rec.Book_Type_Code
2747       AND transaction_header_id_out is null;
2748 
2749       if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Fetching rate_in_use ', l_rate_in_use, p_log_level_rec); end if;
2750 
2751    end if;
2752 
2753    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'l_rate_in_use: ', l_rate_in_use, p_log_level_rec); end if;
2754    -- Bug 6665510: FP: Japan Tax Reform Project (End)
2755 
2756    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'do fa_books_pkg.update_row', '', p_log_level_rec); end if;
2757    -- terminate current fa_books row
2758    fa_books_pkg.update_row (
2759           X_Rowid                        => NULL,
2760           X_Book_Type_Code               => l_asset_hdr_rec.Book_Type_Code,
2761           X_Asset_Id                     => l_asset_hdr_rec.Asset_Id,
2762           X_Date_Placed_In_Service       => l_asset_fin_rec.Date_Placed_In_Service,
2763           X_Date_Effective               => bk_rec.Date_Effective,
2764           X_Deprn_Start_Date             => l_asset_fin_rec.Deprn_Start_Date,
2765           X_Deprn_Method_Code            => l_asset_fin_rec.Deprn_Method_Code,
2766           X_Life_In_Months               => l_asset_fin_rec.Life_In_Months,
2767           X_Rate_Adjustment_Factor       => l_asset_fin_rec.Rate_Adjustment_Factor,
2768           X_Adjusted_Cost                => l_asset_fin_rec.Adjusted_Cost,
2769           X_Cost                         => l_asset_fin_rec.Cost,
2770           X_Original_Cost                => l_asset_fin_rec.Original_Cost,
2771           X_Salvage_Value                => l_asset_fin_rec.Salvage_Value,
2772           X_Prorate_Convention_Code      => l_asset_fin_rec.Prorate_Convention_Code,
2773           X_Prorate_Date                 => l_asset_fin_rec.Prorate_Date,
2774           X_Cost_Change_Flag             => l_asset_fin_rec.Cost_Change_Flag,
2775           X_Adjustment_Required_Status   => l_asset_fin_rec.Adjustment_Required_Status,
2776           X_Capitalize_Flag              => l_asset_fin_rec.Capitalize_Flag,
2777           X_Retirement_Pending_Flag      => l_retirement_pending_flag,
2778           X_Depreciate_Flag              => l_asset_fin_rec.Depreciate_Flag,
2779           X_Disabled_Flag                => l_asset_fin_rec.Disabled_Flag, --HH
2780           X_Last_Update_Date             => l_trans_rec.who_info.Last_Update_Date,
2781           X_Last_Updated_By              => l_trans_rec.who_info.Last_Updated_By,
2782           X_Date_Ineffective             => l_trans_rec.who_info.creation_date,
2783           X_Transaction_Header_Id_In     => bk_rec.Transaction_Header_Id_In,
2784           X_Transaction_Header_Id_Out    => l_trans_rec.Transaction_Header_Id,
2785           X_Itc_Amount_Id                => l_asset_fin_rec.Itc_Amount_Id,
2786           X_Itc_Amount                   => l_asset_fin_rec.Itc_Amount,
2787           X_Retirement_Id                => l_asset_retire_rec.Retirement_Id,
2788           X_Tax_Request_Id               => l_asset_fin_rec.Tax_Request_Id,
2789           X_Itc_Basis                    => l_asset_fin_rec.Itc_Basis,
2790           X_Basic_Rate                   => l_asset_fin_rec.Basic_Rate,
2791           X_Adjusted_Rate                => l_asset_fin_rec.Adjusted_Rate,
2792           X_Bonus_Rule                   => l_asset_fin_rec.Bonus_Rule,
2793           X_Ceiling_Name                 => l_asset_fin_rec.Ceiling_Name,
2794           X_Recoverable_Cost             => l_asset_fin_rec.Recoverable_Cost,
2795           X_Last_Update_Login            => l_trans_rec.who_info.Last_Update_Login,
2796           X_Adjusted_Capacity            => l_asset_fin_rec.Adjusted_Capacity,
2797           X_Fully_Rsvd_Revals_Counter    => l_asset_fin_rec.Fully_Rsvd_Revals_Counter,
2798           X_Idled_Flag                   => l_asset_fin_rec.Idled_Flag,
2799           X_Period_Counter_Capitalized   => l_asset_fin_rec.Period_Counter_Capitalized,
2800           X_PC_Fully_Reserved            => l_asset_fin_rec.Period_Counter_Fully_Reserved,
2801           X_Period_Counter_Fully_Retired => l_Period_Counter_Fully_Ret,
2802           X_Production_Capacity          => l_asset_fin_rec.Production_Capacity,
2803           X_Reval_Amortization_Basis     => l_asset_fin_rec.Reval_Amortization_Basis,
2804           X_Reval_Ceiling                => l_asset_fin_rec.Reval_Ceiling,
2805           X_Unit_Of_Measure              => l_asset_fin_rec.Unit_Of_Measure,
2806           X_Unrevalued_Cost              => l_asset_fin_rec.Unrevalued_Cost,
2807           X_Annual_Deprn_Rounding_Flag   => 'RET',
2808 --          X_Percent_Salvage_Value        => l_asset_fin_rec.Percent_Salvage_Value,
2809 --          X_Allowed_Deprn_Limit          => l_asset_fin_rec.allowed_deprn_limit,
2810 --          X_Allowed_Deprn_Limit_Amount   => l_asset_fin_rec.allowed_deprn_limit_amount,
2811           X_Period_Counter_Life_Complete => l_asset_fin_rec.Period_Counter_Life_Complete,
2812 --          X_Adjusted_Recoverable_Cost    => l_asset_fin_rec.Adjusted_Recoverable_Cost,
2813           X_Group_Asset_Id               => l_asset_fin_rec.Group_Asset_ID,
2814           X_salvage_type                 => l_asset_fin_rec.salvage_type,
2815           X_deprn_limit_type             => l_asset_fin_rec.deprn_limit_type,
2816           X_over_depreciate_option       => l_asset_fin_rec.over_depreciate_option,
2817           X_super_group_id               => l_asset_fin_rec.super_group_id,
2818           X_reduction_rate               => l_asset_retire_rec.reduction_rate,
2819           X_reduce_addition_flag         => l_asset_fin_rec.reduce_addition_flag,
2820           X_reduce_adjustment_flag       => l_asset_fin_rec.reduce_adjustment_flag,
2821           X_reduce_retirement_flag       => l_asset_fin_rec.reduce_retirement_flag,
2822           X_recognize_gain_loss          => l_asset_fin_rec.recognize_gain_loss,
2823           X_recapture_reserve_flag       => l_asset_fin_rec.recapture_reserve_flag,
2824           X_limit_proceeds_flag          => l_asset_fin_rec.limit_proceeds_flag,
2825           X_terminal_gain_loss           => l_asset_fin_rec.terminal_gain_loss,
2826           X_tracking_method              => l_asset_fin_rec.tracking_method,
2827           X_allocate_to_fully_rsv_flag   => l_asset_fin_rec.allocate_to_fully_rsv_flag,
2828           X_allocate_to_fully_ret_flag   => l_asset_fin_rec.allocate_to_fully_ret_flag,
2829           X_exclude_fully_rsv_flag       => l_asset_fin_rec.exclude_fully_rsv_flag,
2830           X_excess_allocation_option     => l_asset_fin_rec.excess_allocation_option,
2831           X_depreciation_option          => l_asset_fin_rec.depreciation_option,
2832           X_member_rollup_flag           => l_asset_fin_rec.member_rollup_flag,
2833           X_mrc_sob_type_code            => p_mrc_sob_type_code,
2834           X_Calling_Fn                   => l_calling_fn
2835           ,p_log_level_rec => p_log_level_rec);
2836 
2837    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'do fa_books_pkg.insert_row', '', p_log_level_rec); end if;
2838    -- insert into fa books
2839    fa_books_pkg.insert_row
2840          (X_Rowid                        => l_bk_rowid,
2841           X_Book_Type_Code               => l_asset_hdr_rec.book_type_code,
2842           X_Asset_Id                     => l_asset_hdr_rec.asset_id,
2843           X_Date_Placed_In_Service       => l_asset_fin_rec.date_placed_in_service,
2844           X_Date_Effective               => l_trans_rec.who_info.last_update_date,
2845           X_Deprn_Start_Date             => l_asset_fin_rec.deprn_start_date,
2846           X_Deprn_Method_Code            => l_asset_fin_rec.deprn_method_code,
2847           X_Life_In_Months               => l_asset_fin_rec.life_in_months,
2848           X_Rate_Adjustment_Factor       => l_asset_fin_rec.rate_adjustment_factor,
2849           X_Adjusted_Cost                => l_adjusted_cost_new,
2850           X_Cost                         => l_cost_new,
2851           X_Original_Cost                => l_asset_fin_rec.original_cost,
2852           X_Salvage_Value                => l_salvage_value_new,
2853           X_Prorate_Convention_Code      => l_asset_fin_rec.prorate_convention_code,
2854           X_Prorate_Date                 => l_asset_fin_rec.prorate_date,
2855           X_Cost_Change_Flag             => l_asset_fin_rec.cost_change_flag,
2856           X_Adjustment_Required_Status   => l_asset_fin_rec.adjustment_required_status,
2857           X_Capitalize_Flag              => l_asset_fin_rec.capitalize_flag,
2858           X_Retirement_Pending_Flag      => l_retirement_pending_flag,
2859           X_Depreciate_Flag              => l_asset_fin_rec.depreciate_flag,
2860           X_Disabled_Flag                => l_asset_fin_rec.Disabled_Flag, --HH
2861           X_Last_Update_Date             => l_trans_rec.who_info.last_update_date,
2862           X_Last_Updated_By              => l_trans_rec.who_info.last_updated_by,
2863           X_Date_Ineffective             => NULL,
2864           X_Transaction_Header_Id_In     => l_trans_rec.transaction_header_id,
2865           X_Transaction_Header_Id_Out    => NULL,
2866           X_Itc_Amount_Id                => l_asset_fin_rec.itc_amount_id,
2867           X_Itc_Amount                   => l_asset_fin_rec.itc_amount,
2868           X_Retirement_Id                => NULL,
2869           X_Tax_Request_Id               => l_asset_fin_rec.tax_request_id,
2870           X_Itc_Basis                    => l_asset_fin_rec.itc_basis,
2871           X_Basic_Rate                   => l_asset_fin_rec.basic_rate,
2872           X_Adjusted_Rate                => l_asset_fin_rec.adjusted_rate,
2873           X_Bonus_Rule                   => l_asset_fin_rec.bonus_rule,
2874           X_Ceiling_Name                 => l_asset_fin_rec.ceiling_name,
2875           X_Recoverable_Cost             => l_recoverable_cost_new,
2876           X_Last_Update_Login            => l_trans_rec.who_info.last_update_login,
2877           X_Adjusted_Capacity            => l_asset_fin_rec.adjusted_capacity,
2878           X_Fully_Rsvd_Revals_Counter    => l_asset_fin_rec.fully_rsvd_revals_counter,
2879           X_Idled_Flag                   => l_asset_fin_rec.idled_flag,
2880           X_Period_Counter_Capitalized   => l_asset_fin_rec.period_counter_capitalized,
2881           X_PC_Fully_Reserved            => l_asset_fin_rec.period_counter_fully_reserved,
2882           X_Period_Counter_Fully_Retired => l_period_counter_fully_ret,
2883           X_Production_Capacity          => l_asset_fin_rec.production_capacity,
2884           X_Reval_Amortization_Basis     => l_reval_amort_basis_new,
2885           X_Reval_Ceiling                => l_asset_fin_rec.reval_ceiling,
2886           X_Unit_Of_Measure              => l_asset_fin_rec.unit_of_measure,
2887           X_Unrevalued_Cost              => l_unrevalued_cost_new,
2888           --X_Annual_Deprn_Rounding_Flag   => l_deprn_rounding_flag,
2889           X_Annual_Deprn_Rounding_Flag   => 'RET',
2890           X_Percent_Salvage_Value        => l_asset_fin_rec.percent_salvage_value,
2891           X_Allowed_Deprn_Limit          => l_asset_fin_rec.allowed_deprn_limit,
2892           X_Allowed_Deprn_Limit_Amount   => l_asset_fin_rec.allowed_deprn_limit_amount,
2893           X_Period_Counter_Life_Complete => l_asset_fin_rec.period_counter_life_complete,
2894           X_Adjusted_Recoverable_Cost    => l_asset_fin_rec.adjusted_recoverable_cost,
2895           X_Short_Fiscal_Year_Flag       => l_asset_fin_rec.short_fiscal_year_flag,
2896           X_Conversion_Date              => l_asset_fin_rec.conversion_date,
2897           X_Orig_Deprn_Start_Date        => l_asset_fin_rec.orig_deprn_start_date,
2898           X_Remaining_Life1              => l_asset_fin_rec.remaining_life1,
2899           X_Remaining_Life2              => l_asset_fin_rec.remaining_life2,
2900           X_Old_Adj_Cost                 => l_asset_fin_rec.old_adjusted_cost,
2901           X_Formula_Factor               => l_asset_fin_rec.formula_factor,
2902           X_gf_Attribute1                => l_asset_fin_rec.global_attribute1,
2903           X_gf_Attribute2                => l_asset_fin_rec.global_attribute2,
2904           X_gf_Attribute3                => l_asset_fin_rec.global_attribute3,
2905           X_gf_Attribute4                => l_asset_fin_rec.global_attribute4,
2906           X_gf_Attribute5                => l_asset_fin_rec.global_attribute5,
2907           X_gf_Attribute6                => l_asset_fin_rec.global_attribute6,
2908           X_gf_Attribute7                => l_asset_fin_rec.global_attribute7,
2909           X_gf_Attribute8                => l_asset_fin_rec.global_attribute8,
2910           X_gf_Attribute9                => l_asset_fin_rec.global_attribute9,
2911           X_gf_Attribute10               => l_asset_fin_rec.global_attribute10,
2912           X_gf_Attribute11               => l_asset_fin_rec.global_attribute11,
2913           X_gf_Attribute12               => l_asset_fin_rec.global_attribute12,
2914           X_gf_Attribute13               => l_asset_fin_rec.global_attribute13,
2915           X_gf_Attribute14               => l_asset_fin_rec.global_attribute14,
2916           X_gf_Attribute15               => l_asset_fin_rec.global_attribute15,
2917           X_gf_Attribute16               => l_asset_fin_rec.global_attribute16,
2918           X_gf_Attribute17               => l_asset_fin_rec.global_attribute17,
2919           X_gf_Attribute18               => l_asset_fin_rec.global_attribute18,
2920           X_gf_Attribute19               => l_asset_fin_rec.global_attribute19,
2921           X_gf_Attribute20               => l_asset_fin_rec.global_attribute20,
2922           X_global_attribute_category    => l_asset_fin_rec.global_attribute_category,
2923           X_group_asset_id               => l_asset_fin_rec.group_asset_id,
2924           X_salvage_type                 => l_asset_fin_rec.salvage_type,
2925           X_deprn_limit_type             => l_asset_fin_rec.deprn_limit_type,
2926           X_over_depreciate_option       => l_asset_fin_rec.over_depreciate_option,
2927           X_super_group_id               => l_asset_fin_rec.super_group_id,
2928           X_reduction_rate               => l_asset_fin_rec.reduction_rate,
2929           X_reduce_addition_flag         => l_asset_fin_rec.reduce_addition_flag,
2930           X_reduce_adjustment_flag       => l_asset_fin_rec.reduce_adjustment_flag,
2931           X_reduce_retirement_flag       => l_asset_fin_rec.reduce_retirement_flag,
2932           X_recognize_gain_loss          => l_asset_fin_rec.recognize_gain_loss,
2933           X_recapture_reserve_flag       => l_asset_fin_rec.recapture_reserve_flag,
2934           X_limit_proceeds_flag          => l_asset_fin_rec.limit_proceeds_flag,
2935           X_terminal_gain_loss           => l_asset_fin_rec.terminal_gain_loss,
2936           X_exclude_proceeds_from_basis  => l_asset_fin_rec.exclude_proceeds_from_basis,
2937           X_retirement_deprn_option      => l_asset_fin_rec.retirement_deprn_option,
2938           X_tracking_method              => l_asset_fin_rec.tracking_method,
2939           X_allocate_to_fully_rsv_flag   => l_asset_fin_rec.allocate_to_fully_rsv_flag,
2940           X_allocate_to_fully_ret_flag   => l_asset_fin_rec.allocate_to_fully_ret_flag,
2941           X_exclude_fully_rsv_flag       => l_asset_fin_rec.exclude_fully_rsv_flag,
2942           X_excess_allocation_option     => l_asset_fin_rec.excess_allocation_option,
2943           X_depreciation_option          => l_asset_fin_rec.depreciation_option,
2944           X_member_rollup_flag           => l_asset_fin_rec.member_rollup_flag,
2945           X_ytd_proceeds                 => nvl(l_asset_fin_rec.ytd_proceeds, 0) +
2946                                             nvl(l_asset_retire_rec.proceeds_of_sale, 0),
2947           X_ltd_proceeds                 => nvl(l_asset_fin_rec.ltd_proceeds, 0) +
2948                                             nvl(l_asset_retire_rec.proceeds_of_sale, 0),
2949           X_eofy_reserve                 => l_asset_fin_rec.eofy_reserve,
2950           X_terminal_gain_loss_amount    => l_asset_fin_rec.terminal_gain_loss_amount,
2951           X_ltd_cost_of_removal          => nvl(l_asset_fin_rec.ltd_cost_of_removal, 0) +
2952                                             nvl(l_asset_retire_rec.cost_of_removal, 0),
2953           X_extended_deprn_flag          => l_asset_fin_rec.extended_deprn_flag,          -- Japan Tax phase3
2954           X_extended_depreciation_period => l_asset_fin_rec.extended_depreciation_period, -- Japan Tax phase3
2955           X_mrc_sob_type_code            => p_mrc_sob_type_code,
2956           X_Return_Status                => l_status,
2957           X_Calling_Fn                   => l_calling_fn);
2958 
2959     -- Bug 6665510: FP: Japan Tax Reform Project (Start)
2960     if nvl(fa_cache_pkg.fazccmt_record.GUARANTEE_RATE_METHOD_FLAG,'NO') = 'YES' then
2961 
2962        UPDATE fa_books
2963        SET rate_in_use = l_rate_in_use
2964        WHERE asset_id = l_asset_hdr_rec.asset_id
2965        AND book_type_code = l_asset_hdr_rec.Book_Type_Code
2966        AND transaction_header_id_out is null;
2967 
2968     end if;
2969     -- Bug 6665510: FP: Japan Tax Reform Project (End)
2970 
2971    -- Full retirement does not update DH table at all
2972    -- since DH table only has distribtuions only for corporate book
2973    if fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE' and
2974       l_trans_rec.transaction_type_code = 'FULL RETIREMENT' then
2975 
2976       -- full retirement
2977       for dh_rec in dh_cursor(l_asset_hdr_rec.asset_id,
2978                               l_asset_hdr_rec.book_type_code) loop
2979 
2980          dh_rec.transaction_units := -1 * dh_rec.units_assigned;
2981 
2982          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'do fa_distribution_history_pkg.update_row', '', p_log_level_rec); end if;
2983 
2984          fa_distribution_history_pkg.update_row
2985                        (null,
2986                         dh_rec.distribution_id,
2987                         dh_rec.book_type_code,
2988                         dh_rec.asset_id,
2989                         dh_rec.units_assigned,
2990                         dh_rec.date_effective,
2991                         dh_rec.code_combination_id,
2992                         dh_rec.location_id,
2993                         dh_rec.transaction_header_id_in,
2994                         l_trans_rec.who_info.last_update_date,
2995                         l_trans_rec.who_info.last_updated_by,
2996                         dh_rec.date_ineffective,
2997                         dh_rec.assigned_to,
2998                         dh_rec.transaction_header_id_out,
2999                         dh_rec.transaction_units,
3000                         l_asset_retire_rec.retirement_id,
3001                         l_trans_rec.who_info.last_update_login,
3002                         l_calling_fn
3003                         ,p_log_level_rec => p_log_level_rec);
3004       end loop;
3005 
3006    end if;
3007 
3008    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'end ', l_calling_fn, p_log_level_rec); end if;
3009 
3010    return TRUE;
3011 
3012 EXCEPTION
3013    /*
3014     * Added for Group Asset uptake
3015     */
3016    when FND_API.G_EXC_UNEXPECTED_ERROR then
3017           fa_srvr_msg.add_message(calling_fn => l_calling_fn
3018                     ,p_log_level_rec => p_log_level_rec);
3019 
3020           return FALSE;
3021    /*** End of uptake ***/
3022    when others then
3023 
3024           if g_token1 is null then
3025                fa_srvr_msg.add_message(calling_fn => l_calling_fn
3026                                       ,name       => g_msg_name
3027                                       ,p_log_level_rec => p_log_level_rec);
3028           else
3029                fa_srvr_msg.add_message(calling_fn => l_calling_fn
3030                                       ,name       => g_msg_name
3031                                       ,token1     => g_token1
3032                                       ,value1     => g_value1
3033                                       ,p_log_level_rec => p_log_level_rec);
3034           end if;
3035 
3036           fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
3037                     ,p_log_level_rec => g_log_level_rec);
3038 
3039           return FALSE;
3040 
3041 END do_sub_regular_retirement;
3042 
3043 FUNCTION calculate_gain_loss
3044         (p_retirement_id            in     number
3045         ,p_mrc_sob_type_code        in     VARCHAR2
3046         ,p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
3047 IS
3048 
3049    l_return_status                   number := 0;
3050 
3051    l_calling_fn  varchar2(80) := 'FA_RETIREMENT_PUB.calculate_gain_loss';
3052 
3053 BEGIN
3054 
3055 
3056    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin ', l_calling_fn, p_log_level_rec); end if;
3057 
3058    if (p_mrc_sob_type_code = 'P') then
3059        if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling Do_Calc_GainLoss_Asset... ', '', p_log_level_rec); end if;
3060        FA_GAINLOSS_PKG.Do_Calc_GainLoss_Asset
3061                       (p_retirement_id => p_retirement_id
3062                       ,x_return_status => l_return_status
3063                       ,p_log_level_rec => p_log_level_rec);
3064    end if;
3065 
3066    if l_return_status > 0 then
3067       raise FND_API.G_EXC_UNEXPECTED_ERROR;
3068    end if;
3069 
3070    return TRUE;
3071 
3072 EXCEPTION
3073 
3074    when FND_API.G_EXC_UNEXPECTED_ERROR then
3075           fa_srvr_msg.add_message(calling_fn => l_calling_fn
3076                     ,p_log_level_rec => p_log_level_rec);
3077 
3078           return FALSE;
3079 
3080    when others then
3081 
3082           if g_token1 is null then
3083                fa_srvr_msg.add_message(calling_fn => l_calling_fn
3084                                       ,name       => g_msg_name
3085 ,p_log_level_rec => p_log_level_rec);
3086           else
3087                fa_srvr_msg.add_message(calling_fn => l_calling_fn
3088                                       ,name       => g_msg_name
3089                                       ,token1     => g_token1
3090                                       ,value1     => g_value1
3091 ,p_log_level_rec => p_log_level_rec);
3092           end if;
3093 
3094           return FALSE;
3095 END calculate_gain_loss;
3096 -----------------------------------------------------------------------------
3097 
3098 PROCEDURE undo_retirement
3099    (p_api_version                in     NUMBER
3100    ,p_init_msg_list              in     VARCHAR2 := FND_API.G_FALSE
3101    ,p_commit                     in     VARCHAR2 := FND_API.G_FALSE
3102    ,p_validation_level           in     NUMBER   := FND_API.G_VALID_LEVEL_FULL
3103    ,p_calling_fn                 in     VARCHAR2
3104    ,x_return_status              out    NOCOPY VARCHAR2
3105    ,x_msg_count                  out    NOCOPY NUMBER
3106    ,x_msg_data                   out    NOCOPY VARCHAR2
3107 
3108    ,px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
3109    ,px_asset_hdr_rec             in out NOCOPY FA_API_TYPES.asset_hdr_rec_type
3110    ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
3111    )
3112 
3113 IS
3114    -- local asset info
3115    l_trans_rec         FA_API_TYPES.trans_rec_type;
3116    lv_trans_rec        FA_API_TYPES.trans_rec_type;
3117    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
3118    lv_asset_hdr_rec    FA_API_TYPES.asset_hdr_rec_type;
3119    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
3120    l_asset_type_rec    FA_API_TYPES.asset_type_rec_type;
3121    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
3122    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
3123    lv_asset_retire_rec FA_API_TYPES.asset_retire_rec_type;
3124    l_asset_dist_tbl    FA_API_TYPES.asset_dist_tbl_type;
3125    l_subcomp_tbl       FA_API_TYPES.subcomp_tbl_type;
3126    l_inv_tbl           FA_API_TYPES.inv_tbl_type;
3127    l_period_rec        FA_API_TYPES.period_rec_type;
3128 
3129    -- used to loop through tax books
3130    l_tax_book_tbl      FA_CACHE_PKG.fazctbk_tbl_type;
3131    l_tax_index         number;  -- index for tax loop
3132 
3133    l_reporting_flag             varchar2(1);
3134 
3135    -- used to store original sob info upon entry into api
3136    l_orig_set_of_books_id    number;
3137    l_orig_currency_context   varchar2(64);
3138 
3139    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.undo_retirement';
3140 
3141 BEGIN
3142 
3143    savepoint undo_retirement;
3144 
3145 
3146    if (not g_log_level_rec.initialized) then
3147       if (NOT fa_util_pub.get_log_level_rec (
3148                 x_log_level_rec =>  g_log_level_rec
3149       )) then
3150          raise FND_API.G_EXC_UNEXPECTED_ERROR;
3151       end if;
3152    end if;
3153 
3154    -- initialize message list if p_init_msg_list is set to TRUE.
3155    if (FND_API.to_boolean(p_init_msg_list)) then
3156         -- initialize error message stack.
3157         fa_srvr_msg.init_server_message;
3158 
3159         -- initialize debug message stack.
3160         fa_debug_pkg.initialize;
3161    end if;
3162 
3163    -- override FA:PRINT_DEBUG profile option.
3164    -- if (p_debug_flag = 'YES') then
3165    --      fa_debug_pkg.set_debug_flag;
3166    -- end if;
3167 
3168 
3169    -- ****************************************************
3170    -- **  Assign input parameters to local rec/tbl types
3171    -- ****************************************************
3172    l_trans_rec        := px_trans_rec;
3173    l_asset_hdr_rec    := px_asset_hdr_rec;
3174    l_asset_retire_rec := px_asset_retire_rec;
3175    l_asset_desc_rec   := null;
3176    l_asset_type_rec   := null;
3177    l_period_rec       := null;
3178 
3179    -- ***********************************
3180    -- **  Call the cache for book
3181    -- **  and do initial MRC validation
3182    -- ***********************************
3183 
3184    fnd_profile.get('GL_SET_OF_BKS_ID',g_orig_set_of_books_id);
3185    g_orig_currency_context :=  substrb(userenv('CLIENT_INFO'),45,10);
3186 
3187    if not FA_UTIL_PVT.get_asset_retire_rec
3188           (px_asset_retire_rec => l_asset_retire_rec,
3189            p_mrc_sob_type_code => 'P'
3190            ,p_log_level_rec => g_log_level_rec) then
3191               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3192    end if;
3193 
3194    l_asset_hdr_rec.asset_id       := l_asset_retire_rec.detail_info.asset_id;
3195    l_asset_hdr_rec.book_type_code := l_asset_retire_rec.detail_info.book_type_code;
3196 
3197    if not FA_UTIL_PVT.get_asset_type_rec
3198           (p_asset_hdr_rec      => l_asset_hdr_rec
3199           ,px_asset_type_rec    => l_asset_type_rec
3200           ,p_date_effective     => NULL
3201           ,p_log_level_rec => g_log_level_rec) then
3202               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3203    end if;
3204 
3205    if l_asset_hdr_rec.book_type_code is not null then
3206 
3207         -- call the cache for the primary transaction book
3208         if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
3209                                   ,p_log_level_rec => g_log_level_rec) then
3210              raise FND_API.G_EXC_UNEXPECTED_ERROR;
3211         end if;
3212 
3213         l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
3214         lv_asset_hdr_rec := l_asset_hdr_rec;
3215 
3216         -- get the book type code P,R or N
3217         if not fa_cache_pkg.fazcsob
3218                (x_set_of_books_id   => l_asset_hdr_rec.set_of_books_id
3219                ,x_mrc_sob_type_code => l_reporting_flag
3220                ,p_log_level_rec => g_log_level_rec)
3221                then
3222                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
3223         end if;
3224 
3225         -- Error out if the program is submitted from the Reporting Responsibility
3226         -- No transaction permitted directly on reporting books.
3227         if l_reporting_flag = 'R' then
3228              FND_MESSAGE.set_name('GL','MRC_OSP_INVALID_BOOK_TYPE');
3229              FND_FILE.PUT_LINE(fnd_file.log,fnd_message.get);
3230              raise FND_API.G_EXC_UNEXPECTED_ERROR;
3231         end if;
3232 
3233 
3234    end if; -- book_type_code
3235 
3236    -- set the sob_id and currency context
3237    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
3238    fnd_client_info.set_currency_context (to_char(l_asset_hdr_rec.set_of_books_id));
3239 
3240    -- BUG# 2376085, 2487707
3241    -- store the sob_id value for later use.
3242    fnd_profile.get ('GL_SET_OF_BKS_ID',l_orig_set_of_books_id);
3243    l_orig_currency_context :=  SUBSTRB(USERENV('CLIENT_INFO'),45,10);
3244 
3245    -- *********************************************
3246    -- **  Do basic validation on input parameters
3247    -- *********************************************
3248 
3249    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'before do_validation', '', g_log_level_rec); end if;
3250 
3251    -- validate that all user-entered input parameters are valid
3252    if not do_validation
3253           (p_validation_type   => g_undo_retirement
3254           ,p_trans_rec         => l_trans_rec
3255           ,p_asset_hdr_rec     => l_asset_hdr_rec
3256           ,p_asset_desc_rec    => l_asset_desc_rec
3257           ,p_asset_type_rec    => l_asset_type_rec
3258           ,p_asset_fin_rec     => l_asset_fin_rec
3259           ,p_asset_retire_rec  => l_asset_retire_rec
3260           ,p_asset_dist_tbl    => l_asset_dist_tbl
3261           ,p_subcomp_tbl       => l_subcomp_tbl
3262           ,p_inv_tbl           => l_inv_tbl
3263           ,p_period_rec        => l_period_rec
3264           ,p_calling_fn        => p_calling_fn
3265           ,p_log_level_rec => g_log_level_rec) then
3266               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3267    end if;
3268 
3269 
3270    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'after do_validation', '', g_log_level_rec); end if;
3271 
3272    l_trans_rec.transaction_type_code := null;
3273 
3274 
3275    -- ***************************
3276    -- **  Main
3277    -- ***************************
3278 
3279    if not undo_all_books_retirement
3280           (p_trans_rec         => l_trans_rec
3281           ,p_asset_hdr_rec     => l_asset_hdr_rec
3282           ,px_asset_retire_rec => l_asset_retire_rec
3283           ,p_log_level_rec => g_log_level_rec) then
3284               raise FND_API.G_EXC_ERROR;
3285    end if;
3286 
3287    -- BUG# 2376085, 2487707
3288    -- reset GL sob id to original value before moving to next book
3289    fnd_profile.put('GL_SET_OF_BKS_ID', l_orig_set_of_books_id);
3290    fnd_client_info.set_currency_context (l_orig_currency_context);
3291 
3292    /* if book is a corporate book, process cip assets and autocopy */
3293 
3294    -- start processing tax books for cip-in-tax and autocopy
3295    if (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE') then
3296 
3297       lv_trans_rec := l_trans_rec;
3298 
3299       if (l_asset_type_rec.asset_type = 'CIP'
3300           or l_asset_type_rec.asset_type = 'CAPITALIZED') then
3301 
3302          if not fa_cache_pkg.fazctbk
3303                 (x_corp_book    => l_asset_hdr_rec.book_type_code
3304                 ,x_asset_type   => l_asset_type_rec.asset_type
3305                 ,x_tax_book_tbl => l_tax_book_tbl
3306                 ,p_log_level_rec => g_log_level_rec) then
3307                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
3308          end if;
3309 
3310          for l_tax_index in 1..l_tax_book_tbl.count loop
3311 
3312            if not FA_ASSET_VAL_PVT.validate_asset_book
3313                   (p_transaction_type_code      => l_trans_rec.transaction_type_code
3314                   ,p_book_type_code             => l_tax_book_tbl(l_tax_index)
3315                   ,p_asset_id                   => l_asset_hdr_rec.asset_id
3316                   ,p_calling_fn                 => l_calling_fn) then
3317 
3318                      null; -- just to ignore the error
3319 
3320            else
3321 
3322              -- cache the book information for the tax book
3323              if not fa_cache_pkg.fazcbc(x_book => l_tax_book_tbl(l_tax_index)) then
3324                   raise FND_API.G_EXC_UNEXPECTED_ERROR;
3325              end if;
3326 
3327              lv_trans_rec.transaction_header_id   := null;
3328              lv_asset_retire_rec                  := null;
3329 
3330              -- to get the new retirement_id for the retrieved tax book
3331              select retirement_id
3332              into lv_asset_retire_rec.retirement_id
3333              from fa_retirements
3334              where asset_id=lv_asset_hdr_rec.asset_id
3335                and book_type_code=l_tax_book_tbl(l_tax_index)
3336                and status = 'PENDING';
3337 
3338              if not undo_all_books_retirement
3339                     (p_trans_rec         => lv_trans_rec     -- tax
3340                     ,p_asset_hdr_rec     => lv_asset_hdr_rec -- tax
3341                     ,px_asset_retire_rec => lv_asset_retire_rec -- tax
3342                     ,p_log_level_rec => g_log_level_rec) then
3343                         raise FND_API.G_EXC_ERROR;
3344              end if;
3345 
3346              -- BUG# 2376085, 2487707
3347              -- reset GL sob id to original value before moving to next book
3348              fnd_profile.put('GL_SET_OF_BKS_ID', l_orig_set_of_books_id);
3349              fnd_client_info.set_currency_context (l_orig_currency_context);
3350 
3351            end if;
3352 
3353          end loop;
3354 
3355 
3356       end if; -- asset_type
3357 
3358    end if; -- book_class
3359 
3360 
3361    -- commit if p_commit is TRUE.
3362    if FND_API.to_boolean(p_commit) then
3363       COMMIT WORK;
3364    end if;
3365 
3366    -- Standard call to get message count and if count is 1 get message info.
3367    FND_MSG_PUB.count_and_get(p_count   => x_msg_count
3368                             ,p_data    => x_msg_data
3369                             );
3370 
3371    -- set back to original environment when the procedure is finished
3372    fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
3373    fnd_client_info.set_currency_context (g_orig_currency_context);
3374 
3375    -- return the status.
3376    x_return_status := FND_API.G_RET_STS_SUCCESS;
3377 
3378 EXCEPTION
3379 
3380    when FND_API.G_EXC_ERROR then
3381 
3382           ROLLBACK TO undo_retirement;
3383 
3384           x_return_status := FND_API.G_RET_STS_ERROR;
3385 
3386           if g_token1 is null then
3387                fa_srvr_msg.add_message(calling_fn => l_calling_fn
3388                                       ,name       => g_msg_name
3389                                       ,p_log_level_rec => g_log_level_rec);
3390           else
3391                fa_srvr_msg.add_message(calling_fn => l_calling_fn
3392                                       ,name       => g_msg_name
3393                                       ,token1     => g_token1
3394                                       ,value1     => g_value1
3395                                       ,p_log_level_rec => g_log_level_rec);
3396           end if;
3397 
3398           FND_MSG_PUB.count_and_get(p_count => x_msg_count
3399                                    ,p_data  => x_msg_data
3400                                    );
3401           -- set back to original environment when the procedure is finished
3402           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
3403           fnd_client_info.set_currency_context (g_orig_currency_context);
3404 
3405    when FND_API.G_EXC_UNEXPECTED_ERROR then
3406 
3407           ROLLBACK TO undo_retirement;
3408 
3409           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3410 
3411           fa_srvr_msg.add_message(calling_fn => l_calling_fn
3412                     ,p_log_level_rec => g_log_level_rec);
3413           FND_MSG_PUB.count_and_get(p_count => x_msg_count
3414                                    ,p_data  => x_msg_data
3415                                    );
3416           -- set back to original environment when the procedure is finished
3417           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
3418           fnd_client_info.set_currency_context (g_orig_currency_context);
3419 
3420    when others then
3421 
3422           ROLLBACK TO undo_retirement;
3423 
3424           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3425 
3426           fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
3427                     ,p_log_level_rec => g_log_level_rec);
3428           FND_MSG_PUB.count_and_get(p_count => x_msg_count
3429                                    ,p_data  => x_msg_data
3430                                    );
3431           -- set back to original environment when the procedure is finished
3432           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
3433           fnd_client_info.set_currency_context (g_orig_currency_context);
3434 
3435 
3436 END undo_retirement;
3437 -----------------------------------------------------------------------------
3438 
3439 FUNCTION undo_all_books_retirement
3440         (p_trans_rec           in     FA_API_TYPES.trans_rec_type
3441         ,p_asset_hdr_rec       in     FA_API_TYPES.asset_hdr_rec_type
3442         ,px_asset_retire_rec   in out NOCOPY FA_API_TYPES.asset_retire_rec_type
3443         ,p_log_level_rec       IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
3444 IS
3445 
3446    -- Returns the reporting GL set_of_books_ids
3447    -- associated with the set_of_books_id of given primary book_type_code
3448    CURSOR sob_cursor(p_book_type_code in varchar2
3449                     ,p_sob_id         in number) is
3450    SELECT set_of_books_id AS sob_id
3451      FROM fa_mc_book_controls
3452     WHERE book_type_code          = p_book_type_code
3453       AND primary_set_of_books_id = p_sob_id
3454       AND enabled_flag            = 'Y';
3455 
3456    -- used for main transaction book
3457    l_book_class                 varchar2(15);
3458    l_set_of_books_id            number;
3459    l_distribution_source_book   varchar2(15);
3460    l_mc_source_flag             varchar2(1);
3461 
3462    -- local asset info
3463    l_trans_rec         FA_API_TYPES.trans_rec_type;
3464    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
3465    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
3466    l_period_rec        FA_API_TYPES.period_rec_type;
3467 
3468    l_ins_status                 boolean := FALSE;
3469 
3470    l_retirement_id              number(15);
3471 
3472    -- local conversion rate
3473    l_exchange_date              date;
3474    l_rate                       number;
3475 
3476    -- msg
3477    g_msg_name                   varchar2(30);
3478 
3479    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.undo_all_books_retirement';
3480 
3481 BEGIN
3482 
3483 
3484    -- ****************************************************
3485    -- **  Assign input parameters to local rec/tbl types
3486    -- ****************************************************
3487    l_trans_rec        := p_trans_rec;
3488    l_asset_hdr_rec    := p_asset_hdr_rec;
3489    l_asset_retire_rec := px_asset_retire_rec;
3490 
3491    -- *********************************
3492    -- **  Populate local record types
3493    -- *********************************
3494    -- populate rec_types that were not provided by users
3495 
3496    -- pop asset_retire_rec to get the rowid of retirement
3497    if not FA_UTIL_PVT.get_asset_retire_rec
3498           (px_asset_retire_rec => l_asset_retire_rec,
3499            p_mrc_sob_type_code => 'P'
3500            ,p_log_level_rec => p_log_level_rec) then
3501               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3502    end if;
3503 
3504    l_asset_hdr_rec.asset_id       := l_asset_retire_rec.detail_info.asset_id;
3505    l_asset_hdr_rec.book_type_code := l_asset_retire_rec.detail_info.book_type_code;
3506 
3507    -- call the cache for the primary transaction book
3508    if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
3509                              ,p_log_level_rec => p_log_level_rec) then
3510          raise FND_API.G_EXC_UNEXPECTED_ERROR;
3511    end if;
3512 
3513    l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
3514 
3515 
3516    -- set the sob_id and currency context
3517    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
3518    fnd_client_info.set_currency_context(to_char(l_asset_hdr_rec.set_of_books_id));
3519 
3520    -- pop current period_rec info
3521    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop period_rec', '', p_log_level_rec); end if;
3522    if not FA_UTIL_PVT.get_period_rec
3523           (p_book           => l_asset_hdr_rec.book_type_code
3524           ,p_effective_date => NULL
3525           ,x_period_rec     => l_period_rec
3526           ,p_log_level_rec => p_log_level_rec) then
3527               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3528    end if;
3529 
3530    -- ***************************************************
3531    -- **  Do asset/book-level validation on transaction
3532    -- ***************************************************
3533    -- begin asset/book-level validation on transaction
3534 
3535    -- nothing to do here
3536    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin asset/book-level validation on transaction', '', p_log_level_rec); end if;
3537 
3538    -- ***************************
3539    -- **  Transaction approval
3540    -- ***************************
3541 
3542    -- SLA: delete the event
3543    if not fa_xla_events_pvt.delete_transaction_event
3544            (p_ledger_id              => l_asset_hdr_rec.set_of_books_id,
3545             p_transaction_header_id  => l_asset_retire_rec.detail_info.transaction_header_id_in,
3546             p_book_type_code         => l_asset_hdr_rec.book_type_code,
3547             p_calling_fn             => 'fa_retirement_pub.undo_all_books_retirement'
3548             ,p_log_level_rec => p_log_level_rec) then
3549       raise FND_API.G_EXC_UNEXPECTED_ERROR;
3550    end if;
3551 
3552 
3553    -- ***************************
3554    -- **  Main
3555    -- ***************************
3556 
3557    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling do_sub_retirement for each book/sob', '', p_log_level_rec); end if;
3558 
3559    l_rate := 1;
3560    if not undo_sub_retirement
3561           (p_trans_rec         => l_trans_rec
3562           ,p_asset_hdr_rec     => l_asset_hdr_rec
3563           ,px_asset_retire_rec => l_asset_retire_rec
3564           ,p_mrc_sob_type_code => 'P'
3565           ,p_log_level_rec => p_log_level_rec) then
3566               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3567    end if;
3568 
3569    if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
3570                              ,p_log_level_rec => p_log_level_rec) then
3571         fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
3572                 ,p_log_level_rec => p_log_level_rec);
3573         return FALSE;
3574    end if;
3575 
3576    -- MRC LOOP
3577    -- if this is a primary book, process reporting books(sobs)
3578    if fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y' then
3579 
3580        -- loop thourgh reporting set of books
3581        for sob_rec in sob_cursor(l_asset_hdr_rec.book_type_code
3582                                 ,l_asset_hdr_rec.set_of_books_id) loop
3583 
3584          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in sob_id loop', '', p_log_level_rec); end if;
3585 
3586          -- set the sob_id and currency context for reporting book(sob)
3587          fnd_profile.put('GL_SET_OF_BKS_ID', sob_rec.sob_id);
3588          fnd_client_info.set_currency_context(to_char(sob_rec.sob_id));
3589 
3590          l_asset_hdr_rec.set_of_books_id := sob_rec.sob_id;
3591 
3592          if not fa_cache_pkg.fazcbcs(x_book => l_asset_hdr_rec.book_type_code
3593                                     ,p_log_level_rec => p_log_level_rec) then
3594            fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
3595                       ,p_log_level_rec => p_log_level_rec);
3596            return FALSE;
3597          end if;
3598 
3599          if not undo_sub_retirement
3600                 (p_trans_rec         => l_trans_rec
3601                 ,p_asset_hdr_rec     => l_asset_hdr_rec
3602                 ,px_asset_retire_rec => l_asset_retire_rec
3603                 ,p_mrc_sob_type_code => 'R'
3604                 ,p_log_level_rec => p_log_level_rec) then
3605                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
3606          end if;
3607 
3608 
3609        end loop;
3610 
3611    end if;
3612 
3613    return TRUE;
3614 
3615 EXCEPTION
3616 
3617    when others then
3618 
3619           if g_token1 is null then
3620                fa_srvr_msg.add_message(calling_fn => l_calling_fn
3621                                       ,name       => g_msg_name
3622                                       ,p_log_level_rec => p_log_level_rec);
3623           else
3624                fa_srvr_msg.add_message(calling_fn => l_calling_fn
3625                                       ,name       => g_msg_name
3626                                       ,token1     => g_token1
3627                                       ,value1     => g_value1
3628                                       ,p_log_level_rec => p_log_level_rec);
3629           end if;
3630 
3631           return FALSE;
3632 
3633 END undo_all_books_retirement;
3634 ------------------------------------------------------------------------------
3635 
3636 FUNCTION undo_sub_retirement
3637    (p_trans_rec                in     FA_API_TYPES.trans_rec_type
3638    ,p_asset_hdr_rec            in     FA_API_TYPES.asset_hdr_rec_type
3639    ,px_asset_retire_rec        in out NOCOPY FA_API_TYPES.asset_retire_rec_type
3640    ,p_mrc_sob_type_code        in     VARCHAR2
3641    ,p_log_level_rec            IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
3642 IS
3643 
3644    transfer_id          number(15);
3645    l_trans_units        number;
3646    l_units_assigned     number;
3647 
3648    l_partial_unit_ret_flag varchar2(1) := 'N';
3649 
3650    l_old_cost           number;
3651    l_old_units          number;
3652    -- local asset info
3653    l_trans_rec         FA_API_TYPES.trans_rec_type;
3654    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
3655    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
3656    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
3657    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
3658 
3659    cursor get_transfer_id is
3660       select transaction_header_id_out
3661             ,transaction_units
3662             ,units_assigned
3663       from fa_distribution_history
3664       where retirement_id = l_asset_retire_rec.retirement_id;
3665 
3666    cursor adj_def is
3667       select rowid
3668       from fa_adjustments
3669       where asset_id = p_asset_hdr_rec.asset_id
3670       and transaction_header_id = transfer_id
3671       and source_type_code = 'TRANSFER'
3672       and adjustment_type in ('COST', 'RESERVE'
3673 );
3674 
3675    l_adj_rowid    rowid;
3676 
3677    /*
3678     * Check to see previous retirement was source line retirement or not
3679     */
3680    cursor c_inv_trx_id (c_thid number)is
3681       select invoice_transaction_id
3682       from   fa_transaction_headers
3683       where  transaction_header_id = c_thid;
3684 
3685    cursor get_cost_before_ret(c_asset_id number, c_book varchar2, c_ret_id number, c_reporting_flag varchar2) is
3686       select cost
3687       from fa_books
3688       where asset_id = c_asset_id
3689         and book_type_code = c_book
3690         and retirement_id = c_ret_id
3691         and c_reporting_flag <> 'R'
3692       union
3693       select cost
3694       from fa_mc_books
3695       where asset_id = c_asset_id
3696         and book_type_code = c_book
3697         and retirement_id = c_ret_id
3698         and c_reporting_flag = 'R';
3699 
3700    --bug 5489382: Added cursor to fetch the units before retirement
3701    cursor get_units_before_ret(c_thid number, c_asset_id number) is
3702       select units
3703       from fa_asset_history
3704       where transaction_header_id_out = c_thid
3705         and asset_id = c_asset_id;
3706 
3707    l_invoice_transaction_id    number;  -- Local variable to store return value of c_inv_trx_id
3708 
3709    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.undo_sub_retirement';
3710 
3711 BEGIN
3712 
3713 
3714    -- ****************************************************
3715    -- **  Assign input parameters to local rec/tbl types
3716    -- ****************************************************
3717    l_trans_rec        := p_trans_rec;
3718    l_asset_hdr_rec    := p_asset_hdr_rec;
3719    l_asset_retire_rec := px_asset_retire_rec;
3720 
3721    -- pop asset_retire_rec to get the rowid of retirement
3722    if not FA_UTIL_PVT.get_asset_retire_rec
3723           (px_asset_retire_rec => l_asset_retire_rec,
3724            p_mrc_sob_type_code => p_mrc_sob_type_code
3725            ,p_log_level_rec => p_log_level_rec) then
3726               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3727    end if;
3728 
3729    l_asset_hdr_rec.asset_id       := l_asset_retire_rec.detail_info.asset_id;
3730    l_asset_hdr_rec.book_type_code := l_asset_retire_rec.detail_info.book_type_code;
3731 
3732    -- pop asset_desc_rec
3733    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_desc_rec', '', p_log_level_rec); end if;
3734    if not FA_UTIL_PVT.get_asset_desc_rec
3735           (p_asset_hdr_rec      => l_asset_hdr_rec
3736           ,px_asset_desc_rec    => l_asset_desc_rec
3737           ,p_log_level_rec => p_log_level_rec) then
3738               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3739    end if;
3740 
3741    -- pop asset_fin_rec
3742    -- get fa_books row where transaction_header_id_out is null
3743    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_fin_rec', '', p_log_level_rec); end if;
3744    if not FA_UTIL_PVT.get_asset_fin_rec
3745           (p_asset_hdr_rec         => l_asset_hdr_rec
3746           ,px_asset_fin_rec        => l_asset_fin_rec
3747           ,p_transaction_header_id => NULL
3748           ,p_mrc_sob_type_code     => p_mrc_sob_type_code
3749           ,p_log_level_rec => p_log_level_rec) then
3750               raise FND_API.G_EXC_UNEXPECTED_ERROR;
3751    end if;
3752 
3753    if g_log_level_rec.statement_level then
3754      fa_debug_pkg.add(l_calling_fn, 'after pop asset_fin_rec', '', p_log_level_rec);
3755      fa_debug_pkg.add(l_calling_fn, 'asset_hdr: set_of_books_id: ', l_asset_hdr_rec.set_of_books_id, p_log_level_rec);
3756    end if;
3757 
3758    /*
3759     * Take care source line
3760     */
3761    OPEN  c_inv_trx_id(l_asset_retire_rec.detail_info.transaction_header_id_in);
3762    FETCH c_inv_trx_id into l_invoice_transaction_id;
3763    CLOSE c_inv_trx_id;
3764 
3765    if (l_invoice_transaction_id is not null) then
3766       if (p_mrc_sob_type_code = 'R') then
3767 
3768          DELETE FROM FA_MC_ASSET_INVOICES
3769          WHERE       ASSET_ID = l_asset_hdr_rec.asset_id
3770          AND         INVOICE_TRANSACTION_ID_IN = l_invoice_transaction_id;
3771 
3772          UPDATE FA_MC_ASSET_INVOICES
3773          SET    INVOICE_TRANSACTION_ID_OUT = '',
3774                 DATE_INEFFECTIVE = ''
3775          WHERE  ASSET_ID = l_asset_hdr_rec.asset_id
3776          AND    INVOICE_TRANSACTION_ID_OUT = l_invoice_transaction_id;
3777 
3778       else
3779 
3780          DELETE FROM FA_ASSET_INVOICES
3781          WHERE       ASSET_ID = l_asset_hdr_rec.asset_id
3782          AND         INVOICE_TRANSACTION_ID_IN = l_invoice_transaction_id;
3783 
3784          UPDATE FA_ASSET_INVOICES
3785          SET    INVOICE_TRANSACTION_ID_OUT = '',
3786                 DATE_INEFFECTIVE = ''
3787          WHERE  ASSET_ID = l_asset_hdr_rec.asset_id
3788          AND    INVOICE_TRANSACTION_ID_OUT = l_invoice_transaction_id;
3789 
3790          /*
3791           * This needs to happen only once so do this for primary book.
3792           */
3793          DELETE FROM FA_INVOICE_TRANSACTIONS
3794          WHERE       INVOICE_TRANSACTION_ID = l_invoice_transaction_id;
3795 
3796       end if; --(p_mrc_sob_type_code = 'R')
3797 
3798 
3799    end if; -- (l_invoice_transaction_id is not null)
3800 
3801    /*
3802     * Added for Group Asset uptake
3803     */
3804    if (l_asset_fin_rec.group_asset_id is not null) and
3805          (l_asset_retire_rec.recognize_gain_loss = 'NO') then
3806       if not FA_RETIREMENT_PVT.UNDO_RETIREMENT_REINSTATEMENT(
3807                     p_transaction_header_id =>l_asset_retire_rec.detail_info.transaction_header_id_in,
3808                     p_asset_hdr_rec         => l_asset_hdr_rec,
3809                     p_group_asset_id        => l_asset_fin_rec.group_asset_id,
3810                     p_set_of_books_id       => l_asset_hdr_rec.set_of_books_id,
3811                     p_mrc_sob_type_code     => p_mrc_sob_type_code,
3812                     p_calling_fn            => l_calling_fn
3813                     ,p_log_level_rec => p_log_level_rec) then
3814          raise FND_API.G_EXC_UNEXPECTED_ERROR;
3815       end if;
3816    end if;
3817            /*** End of uptake ***/
3818 
3819    -- l_trans_rec.transaction_header_id should be trx_header_id
3820    -- of PARTIAL RETIREMENT or FULL RETIREMENT row in TH table
3821 
3822    OPEN  get_cost_before_ret(l_asset_hdr_rec.asset_id,
3823                              l_asset_hdr_rec.book_type_code,
3824                              l_asset_retire_rec.retirement_id,
3825                              p_mrc_sob_type_code);
3826    FETCH get_cost_before_ret into l_old_cost;
3827    CLOSE get_cost_before_ret;
3828 
3829     /* bug 5489382: commented following code as it was giving current units instead of old units
3830     -- added for Bug# 5098320 get the current units of the asset from fa_asset_history table
3831            if not FA_UTIL_PVT.get_current_units
3832                   (p_calling_fn    => l_calling_fn
3833                   ,p_asset_id      => l_asset_hdr_rec.asset_id
3834                   ,x_current_units => l_old_units
3835                   ) then
3836                       g_msg_name := null;
3837                       g_token1 := null;
3838                       raise FND_API.G_EXC_UNEXPECTED_ERROR;
3839            end if; */
3840 
3841    --bug 5489382
3842    OPEN get_units_before_ret(l_asset_retire_rec.detail_info.transaction_header_id_in,
3843                              l_asset_hdr_rec.asset_id);
3844    FETCH get_units_before_ret into l_old_units;
3845    CLOSE get_units_before_ret;
3846 
3847    if g_log_level_rec.statement_level then
3848      fa_debug_pkg.add(l_calling_fn, 'Asset_Id:', l_asset_hdr_rec.Asset_Id, p_log_level_rec);
3849      fa_debug_pkg.add(l_calling_fn, 'p_mrc_sob_type_code:', p_mrc_sob_type_code, p_log_level_rec);
3850      fa_debug_pkg.add(l_calling_fn, 'Cost before ret:', l_old_cost, p_log_level_rec);
3851      fa_debug_pkg.add(l_calling_fn, 'l_asset_retire_rec.cost_retired:', l_asset_retire_rec.cost_retired, p_log_level_rec);
3852      fa_debug_pkg.add(l_calling_fn, 'l_asset_retire_rec.units_retired:', l_asset_retire_rec.units_retired, p_log_level_rec);
3853      fa_debug_pkg.add(l_calling_fn, 'l_old_units:', l_old_units, p_log_level_rec);
3854      fa_debug_pkg.add(l_calling_fn, 'l_asset_desc_rec.current_units:', l_asset_desc_rec.current_units, p_log_level_rec);
3855 
3856    end if;
3857    -- Bug#2306068: Fixed wrong check of partial unit retirement
3858    -- 0 cost condition added to allow for partial unit retirements/reinstatements
3859    -- on assets with 0 cost.
3860    /* Bug# 5098320: undo retirement for zero cost asset was failing.
3861       changed the if condition to prevent that.
3862    if (
3863         (l_asset_retire_rec.units_retired is not null)
3864         and ( (l_asset_retire_rec.cost_retired <> l_old_cost) or
3865                 (l_asset_retire_rec.cost_retired = 0 and l_old_cost = 0))
3866       ) then*/
3867    if (
3868         (l_asset_retire_rec.units_retired <> l_old_units)
3869         and ((l_asset_retire_rec.cost_retired <> l_old_cost) or
3870                 (l_asset_retire_rec.cost_retired = 0 and l_old_cost = 0))
3871       ) then
3872         -- partial unit retirement
3873 
3874         -- l_trans_rec.transaction_header_id should be trx_header_id
3875         -- of PARTIAL RETIREMENT row in TH table
3876 
3877         if g_log_level_rec.statement_level then
3878           fa_debug_pkg.add(l_calling_fn, 'deleting a partial unit retirement', '', p_log_level_rec);
3879 
3880           fa_debug_pkg.add(l_calling_fn, 'FA_BOOKS_PKG.DELETE_ROW', '', p_log_level_rec);
3881           fa_debug_pkg.add(l_calling_fn, 'trx_id: ', l_asset_retire_rec.detail_info.transaction_header_id_in, p_log_level_rec);
3882         end if;
3883 
3884         FA_BOOKS_PKG.DELETE_ROW
3885                         (X_Transaction_Header_Id_In =>
3886                                         l_asset_retire_rec.detail_info.transaction_header_id_in,
3887                          X_mrc_sob_type_code => p_mrc_sob_type_code,
3888                          X_Calling_Fn => l_calling_fn
3889                          ,p_log_level_rec => p_log_level_rec);
3890 
3891         if g_log_level_rec.statement_level then
3892           fa_debug_pkg.add(l_calling_fn, 'FA_BOOKS_PKG.REACTIVATE_ROW', '', p_log_level_rec);
3893           fa_debug_pkg.add(l_calling_fn, 'trx_id: ', l_asset_retire_rec.detail_info.transaction_header_id_in, p_log_level_rec);
3894         end if;
3895 
3896         FA_BOOKS_PKG.REACTIVATE_ROW
3897                         (X_Transaction_Header_Id_Out =>
3898                                         l_asset_retire_rec.detail_info.transaction_header_id_in,
3899                          X_mrc_sob_type_code => p_mrc_sob_type_code,
3900                          X_Calling_Fn => l_calling_fn
3901                          ,p_log_level_rec => p_log_level_rec);
3902 
3903         if g_log_level_rec.statement_level then
3904           fa_debug_pkg.add(l_calling_fn, 'FA_BOOKS_PKG.DELETE_ROW', '', p_log_level_rec);
3905           fa_debug_pkg.add(l_calling_fn, 'ROW: ', l_asset_retire_rec.detail_info.row_id, p_log_level_rec);
3906         end if;
3907 
3908         FA_RETIREMENTS_PKG.DELETE_ROW
3909                         (X_Rowid => l_asset_retire_rec.detail_info.row_id,
3910                          X_mrc_sob_type_code => p_mrc_sob_type_code,
3911                          X_Calling_Fn => l_calling_fn
3912                          ,p_log_level_rec => p_log_level_rec);
3913 
3914         if g_log_level_rec.statement_level then
3915           fa_debug_pkg.add(l_calling_fn, 'Asset_Id:', l_asset_hdr_rec.Asset_Id, p_log_level_rec);
3916           fa_debug_pkg.add(l_calling_fn, 'Book_Type_Code:', l_asset_hdr_rec.Book_Type_Code, p_log_level_rec);
3917           fa_debug_pkg.add(l_calling_fn, 'p_mrc_sob_type_code:', p_mrc_sob_type_code, p_log_level_rec);
3918           fa_debug_pkg.add(l_calling_fn, 'book class:', fa_cache_pkg.fazcbc_record.book_class, p_log_level_rec);
3919         end if;
3920 
3921         if (p_mrc_sob_type_code <> 'R') then
3922 
3923            if (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE') then
3924               if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'before get_transfer_id', '', p_log_level_rec); end if;
3925 
3926               l_partial_unit_ret_flag := 'N';
3927 
3928               open get_transfer_id;
3929 
3930               loop
3931 
3932                 fetch get_transfer_id
3933                 into transfer_id
3934                      ,l_trans_units
3935                      ,l_units_assigned;
3936 
3937                 EXIT when get_transfer_id%NOTFOUND;
3938 
3939                 /* Fix for bug#3246439: Partial unit transfer issue
3940                                         with multi-distributed asset */
3941                 if (l_trans_units <> l_units_assigned * -1) then
3942                    l_partial_unit_ret_flag := 'Y';
3943                 end if;
3944 
3945               end loop;
3946 
3947               close get_transfer_id;
3948 
3949 
3950               if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'after get_transfer_id', '', p_log_level_rec); end if;
3951 
3952               if (l_partial_unit_ret_flag='Y') then
3953 
3954                   --delete a row only if a distribution is partially retired
3955                   if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_DISTRIBUTION_HISTORY_PKG.DELETE_ROW', '', p_log_level_rec); end if;
3956                   FA_DISTRIBUTION_HISTORY_PKG.DELETE_ROW
3957                         (X_Asset_Id     => l_asset_hdr_rec.Asset_Id,
3958                         X_Book_Type_Code=> l_asset_hdr_rec.Book_Type_Code,
3959                         X_Transaction_Header_Id => transfer_id,
3960                         X_Calling_Fn => l_calling_fn
3961                         ,p_log_level_rec => p_log_level_rec);
3962 
3963               end if;
3964 
3965               if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_DISTRIBUTION_HISTORY_PKG.REACTIVATE_ROW', '', p_log_level_rec); end if;
3966               FA_DISTRIBUTION_HISTORY_PKG.REACTIVATE_ROW
3967                         (X_Transaction_Header_Id_Out =>
3968                                         transfer_id,
3969                         X_Asset_Id      => l_asset_hdr_rec.Asset_Id,
3970                         X_Book_Type_Code=> l_asset_hdr_rec.Book_Type_Code,
3971                         X_Calling_Fn => l_calling_fn
3972                         ,p_log_level_rec => p_log_level_rec);
3973 
3974               if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_ASSET_HISTORY_PKG.DELETE_ROW', '', p_log_level_rec); end if;
3975               FA_ASSET_HISTORY_PKG.DELETE_ROW
3976                         (X_Transaction_Header_Id_In =>
3977                                 l_asset_retire_rec.detail_info.transaction_header_id_in,
3978                          X_Asset_Id   => l_asset_hdr_rec.Asset_Id,
3979                          X_Calling_Fn => l_calling_fn
3980                          ,p_log_level_rec => p_log_level_rec);
3981 
3982               if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_ASSET_HISTORY_PKG.REACTIVATE_ROW', '', p_log_level_rec); end if;
3983               FA_ASSET_HISTORY_PKG.REACTIVATE_ROW
3984                         (X_Transaction_Header_Id_Out =>
3985                                 l_asset_retire_rec.detail_info.transaction_header_id_in,
3986                          X_Asset_Id   => l_asset_hdr_rec.Asset_Id,
3987                          X_Calling_Fn => l_calling_fn
3988                          ,p_log_level_rec => p_log_level_rec);
3989 
3990               if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_ADDITIONS_PKG.UPDATE_UNITS', '', p_log_level_rec); end if;
3991               FA_ADDITIONS_PKG.UPDATE_UNITS
3992                         (X_Asset_Id => l_asset_hdr_rec.Asset_Id,
3993 
3994                          X_Calling_Fn => l_calling_fn
3995                          ,p_log_level_rec => p_log_level_rec);
3996               if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_TRANSFER_DETAILS_PKG.DELETE_ROW', '', p_log_level_rec); end if;
3997               FA_TRANSFER_DETAILS_PKG.DELETE_ROW
3998                         (X_Transfer_Header_Id => transfer_id,
3999                          X_Calling_Fn => l_calling_fn
4000                          ,p_log_level_rec => p_log_level_rec);
4001 
4002            end if; -- if corporate
4003 
4004            if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_TRANSACTION_HEADERS_PKG.DELETE_ROW', '', p_log_level_rec); end if;
4005            FA_TRANSACTION_HEADERS_PKG.DELETE_ROW -- delete PARTIAL RETIREMENT row
4006                         (X_Transaction_Header_Id =>
4007                                 l_asset_retire_rec.detail_info.transaction_header_id_in,
4008                          X_Calling_Fn => l_calling_fn);
4009 
4010            if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_TRANSACTION_HEADERS_PKG.DELETE_ROW -- delete TRANSFER OUT row', '', p_log_level_rec); end if;
4011            FA_TRANSACTION_HEADERS_PKG.DELETE_ROW -- delete TRANSFER OUT row
4012                         (X_Transaction_Header_Id =>
4013                                 transfer_id,
4014                          X_Calling_Fn => l_calling_fn);
4015 
4016         end if; -- if reporting_flag <> 'R'
4017 
4018         -- Remove any cost and reserve adjustment rows
4019         -- if UNDOing PARTIAL UNIT RETIREMENT
4020 
4021         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin - deleting fa_adjustments', '', p_log_level_rec); end if;
4022 
4023         open adj_def;
4024 
4025         begin
4026 
4027           loop
4028                fetch adj_def into l_adj_rowid;
4029 
4030                exit when adj_def%NOTFOUND;
4031 
4032                if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_ADJUSTMENTS_PKG.delete_row', '', p_log_level_rec); end if;
4033                FA_ADJUSTMENTS_PKG.delete_row(
4034                           X_Rowid       => l_adj_rowid,
4035                           X_Asset_Id    => l_asset_hdr_rec.asset_id,
4036                           X_Calling_Fn  => l_calling_fn,
4037                           X_mrc_sob_type_code => p_mrc_sob_type_code
4038                           ,p_log_level_rec => p_log_level_rec);
4039 
4040           end loop;
4041 
4042         exception
4043           when others then null;
4044         end;
4045 
4046         close adj_def;
4047 
4048         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'done - deleting fa_adjustments', '', p_log_level_rec); end if;
4049 
4050 
4051    else -- if not partial unit retirement
4052 
4053         if g_log_level_rec.statement_level then
4054            fa_debug_pkg.add(l_calling_fn, 'not a partial unit retirement', '', p_log_level_rec);
4055            fa_debug_pkg.add(l_calling_fn, 'FA_BOOKS_PKG.DELETE_ROW', '', p_log_level_rec);
4056         end if;
4057         FA_BOOKS_PKG.DELETE_ROW
4058                                 (X_Transaction_Header_Id_In =>
4059                                 l_asset_retire_rec.detail_info.transaction_header_id_in,
4060                                 X_mrc_sob_type_code => p_mrc_sob_type_code,
4061                                 X_Calling_Fn => l_calling_fn
4062                                 ,p_log_level_rec => p_log_level_rec);
4063         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_BOOKS_PKG.REACTIVATE_ROW', '', p_log_level_rec); end if;
4064         FA_BOOKS_PKG.REACTIVATE_ROW
4065                         (X_Transaction_Header_Id_Out =>
4066                                         l_asset_retire_rec.detail_info.transaction_header_id_in,
4067                          X_mrc_sob_type_code => p_mrc_sob_type_code,
4068                          X_Calling_Fn => l_calling_fn
4069                          ,p_log_level_rec => p_log_level_rec);
4070 
4071         if g_log_level_rec.statement_level then
4072            fa_debug_pkg.add(l_calling_fn, 'FA_RETIREMENTS_PKG.DELETE_ROW', '', p_log_level_rec);
4073            fa_debug_pkg.add(l_calling_fn, 'ROW: ', l_asset_retire_rec.detail_info.row_id, p_log_level_rec);
4074            fa_debug_pkg.add(l_calling_fn, 'retirement_id: ', l_asset_retire_rec.retirement_id, p_log_level_rec);
4075         end if;
4076         FA_RETIREMENTS_PKG.DELETE_ROW
4077                         (X_Rowid      => l_asset_retire_rec.detail_info.row_id,
4078                          X_mrc_sob_type_code => p_mrc_sob_type_code,
4079                          X_Calling_Fn => l_calling_fn
4080                          ,p_log_level_rec => p_log_level_rec);
4081 
4082         if (p_mrc_sob_type_code <> 'R') then
4083 
4084            if (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE') then
4085                 if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_DISTRIBUTION_HISTORY_PKG.REACTIVATE_ROW', '', p_log_level_rec); end if;
4086                 FA_DISTRIBUTION_HISTORY_PKG.REACTIVATE_ROW
4087                         (X_Asset_Id     => l_asset_hdr_rec.asset_id,
4088                          X_Book_Type_Code=> l_asset_hdr_rec.book_type_code,
4089                          X_Calling_Fn => l_calling_fn
4090                          ,p_log_level_rec => p_log_level_rec);
4091            end if; -- if corporate
4092 
4093            if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'FA_TRANSACTION_HEADERS_PKG.DELETE_ROW', '', p_log_level_rec); end if;
4094            FA_TRANSACTION_HEADERS_PKG.DELETE_ROW
4095                         (X_Transaction_Header_Id =>
4096                                 l_asset_retire_rec.detail_info.transaction_header_id_in,
4097                          X_Calling_Fn => l_calling_fn
4098                          ,p_log_level_rec => p_log_level_rec);
4099 
4100         end if; -- reporting_flag
4101 
4102    end if;
4103 
4104    return TRUE;
4105 
4106 EXCEPTION
4107    /*
4108     * Added for Group Asset uptake
4109     */
4110     when FND_API.G_EXC_UNEXPECTED_ERROR then
4111         -- Make sure to close curosr opened for source line retirement
4112         if c_inv_trx_id%ISOPEN then
4113           CLOSE c_inv_trx_id;
4114         end if;
4115 
4116         fa_srvr_msg.add_message(calling_fn => l_calling_fn
4117                 ,p_log_level_rec => p_log_level_rec);
4118         return FALSE;
4119    /*** End of uptake ***/
4120     when others then
4121 
4122         close adj_def;
4123 
4124         -- Make sure to close curosr opened for source line retirement
4125         if c_inv_trx_id%ISOPEN then
4126           CLOSE c_inv_trx_id;
4127         end if;
4128 
4129         return FALSE;
4130 
4131 END undo_sub_retirement;
4132 -----------------------------------------------------------------------------
4133 
4134 -- In addition to the existing functionalities,
4135 -- Do_reinstatement will handle asset_dist_tbl for distributions
4136 
4137 -- Table access
4138 -- 1. insert into TH
4139 -- 2. update RET
4140 
4141 -- AAA
4142 -- Users are allowed to do reinstatement on an asset whose retirement status
4143 -- is set to 'PROCESSED', which indicates
4144 -- 'Retirement and Calculate Gain/Loss are processed'.
4145 
4146 -- undo_retirement, do_resinsatement and undo_reinstatement allow
4147 -- users to give them either asset_id/book or retirement_id
4148 -- as a target for the transactions while do_retirement allows only
4149 -- asset_id/book parameter.
4150 
4151 PROCEDURE do_reinstatement
4152    (p_api_version                in     NUMBER
4153    ,p_init_msg_list              in     VARCHAR2 := FND_API.G_FALSE
4154    ,p_commit                     in     VARCHAR2 := FND_API.G_FALSE
4155    ,p_validation_level           in     NUMBER   := FND_API.G_VALID_LEVEL_FULL
4156    ,p_calling_fn                 in     VARCHAR2
4157    ,x_return_status              out    NOCOPY VARCHAR2
4158    ,x_msg_count                  out    NOCOPY NUMBER
4159    ,x_msg_data                   out    NOCOPY VARCHAR2
4160 
4161    ,px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
4162    ,px_asset_hdr_rec             in out NOCOPY FA_API_TYPES.asset_hdr_rec_type
4163    ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
4164    ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
4165    ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
4166    ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type)
4167 IS
4168    -- local asset info
4169    l_trans_rec         FA_API_TYPES.trans_rec_type;
4170    lv_trans_rec        FA_API_TYPES.trans_rec_type;
4171    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
4172    lv_asset_hdr_rec    FA_API_TYPES.asset_hdr_rec_type;
4173    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
4174    l_asset_type_rec    FA_API_TYPES.asset_type_rec_type;
4175    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
4176    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
4177    lv_asset_retire_rec FA_API_TYPES.asset_retire_rec_type;
4178    l_asset_dist_tbl    FA_API_TYPES.asset_dist_tbl_type;
4179    l_subcomp_tbl       FA_API_TYPES.subcomp_tbl_type;
4180    l_inv_tbl           FA_API_TYPES.inv_tbl_type;
4181    l_period_rec        FA_API_TYPES.period_rec_type;
4182 
4183    -- used for loop through tax books
4184    l_tax_book_tbl     FA_CACHE_PKG.fazctbk_tbl_type;
4185    l_tax_index        number;
4186 
4187    cursor tde_cursor is
4188    select max(th.transaction_date_entered)
4189    from fa_deprn_periods       dp
4190        ,fa_transaction_headers th
4191        ,fa_books               bk
4192    where dp.book_type_code = l_asset_hdr_rec.book_type_code
4193      and dp.period_close_date is null
4194      and bk.book_type_code = dp.book_type_code
4195      and bk.asset_id       = l_asset_hdr_rec.asset_id
4196      and th.date_effective
4197          between bk.date_effective
4198              and nvl(bk.date_ineffective,sysdate)
4199      and th.date_effective
4200          between dp.period_open_date
4201              and nvl(dp.period_close_date,sysdate)
4202      and th.book_type_code = dp.book_type_code
4203      and th.asset_id = bk.asset_id
4204      and th.transaction_type_code like '%RETIREMENT';
4205 
4206    l_transaction_date_entered   date;
4207    l_reporting_flag             varchar2(1);
4208 
4209    l_retirement_id              number(15);
4210    l_calculate_gain_loss_flag   varchar2(1);
4211 
4212    -- used to store original sob info upon entry into api
4213    l_orig_set_of_books_id    number;
4214    l_orig_currency_context   varchar2(64);
4215 
4216    l_ins_status                 boolean := FALSE;
4217 
4218    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.do_reinstatement';
4219 
4220 BEGIN
4221 
4222    savepoint do_reinstatement;
4223 
4224 
4225    if (not g_log_level_rec.initialized) then
4226       if (NOT fa_util_pub.get_log_level_rec (
4227                 x_log_level_rec =>  g_log_level_rec
4228       )) then
4229          raise FND_API.G_EXC_UNEXPECTED_ERROR;
4230       end if;
4231    end if;
4232 
4233    -- initialize message list if p_init_msg_list is set to TRUE.
4234    if (FND_API.to_boolean(p_init_msg_list)) then
4235         -- initialize error message stack.
4236         fa_srvr_msg.init_server_message;
4237 
4238         -- initialize debug message stack.
4239         fa_debug_pkg.initialize;
4240    end if;
4241 
4242    -- ****************************************************
4243    -- **  Assign input parameters to local rec/tbl types
4244    -- ****************************************************
4245    l_trans_rec        := px_trans_rec;
4246    l_asset_hdr_rec    := px_asset_hdr_rec;
4247    l_asset_retire_rec := px_asset_retire_rec;
4248    l_asset_dist_tbl   := p_asset_dist_tbl;
4249    l_subcomp_tbl      := p_subcomp_tbl;
4250    l_inv_tbl          := p_inv_tbl;
4251 
4252    l_retirement_id    := l_asset_retire_rec.retirement_id;
4253    l_calculate_gain_loss_flag := l_asset_retire_rec.calculate_gain_loss;
4254 
4255    -- *********************************
4256    -- **  Populate local record types
4257    -- *********************************
4258 
4259    -- pop asset_retire_rec to get the rowid of retirement
4260       if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_retire_rec', '', g_log_level_rec); end if;
4261    if not FA_UTIL_PVT.get_asset_retire_rec
4262           (px_asset_retire_rec => l_asset_retire_rec,
4263            p_mrc_sob_type_code => 'P'
4264            ,p_log_level_rec => g_log_level_rec) then
4265               raise FND_API.G_EXC_UNEXPECTED_ERROR;
4266    end if;
4267 
4268    l_asset_hdr_rec.asset_id       := l_asset_retire_rec.detail_info.asset_id;
4269    l_asset_hdr_rec.book_type_code := l_asset_retire_rec.detail_info.book_type_code;
4270 
4271    if g_log_level_rec.statement_level then
4272      fa_debug_pkg.add(l_calling_fn, 'asset_id:', l_asset_hdr_rec.asset_id, g_log_level_rec);
4273      fa_debug_pkg.add(l_calling_fn, 'book_type_code:', l_asset_hdr_rec.book_type_code, g_log_level_rec);
4274      fa_debug_pkg.add(l_calling_fn, 'fa_ret retirement_id:', l_asset_retire_rec.retirement_id, g_log_level_rec);
4275    end if;
4276 
4277    -- ***************************
4278    -- **  Transaction approval
4279    -- ***************************
4280    -- common for all types
4281    -- bug 1230315... adding FA_TRX_APPROVAL_PKG for all retirement
4282    -- transactions
4283 
4284    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'call trx approval pkg - faxcat', '', g_log_level_rec); end if;
4285    l_ins_status := FA_TRX_APPROVAL_PKG.faxcat
4286                    (x_book              => l_asset_hdr_rec.book_type_code
4287                    ,x_asset_id          => l_asset_hdr_rec.asset_id
4288                    ,x_trx_type          => 'REINSTATEMENT'
4289                    ,x_trx_date          => l_asset_retire_rec.date_retired
4290                    ,x_init_message_flag => 'NO'
4291                    ,p_log_level_rec => g_log_level_rec);
4292    if not l_ins_status then
4293         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'failed in FA_TRX_APPROVAL_PKG.faxcat', '', g_log_level_rec); end if;
4294         -- g_msg_name := 'error msg name-after trx_app';
4295         raise FND_API.G_EXC_ERROR;
4296    end if;
4297    -- ***********************************
4298    -- **  Call the cache for book
4299    -- **  and do initial MRC validation
4300    -- ***********************************
4301 
4302    if l_asset_hdr_rec.book_type_code is not null then
4303 
4304         fnd_profile.get('GL_SET_OF_BKS_ID',g_orig_set_of_books_id);
4305         g_orig_currency_context :=  substrb(userenv('CLIENT_INFO'),45,10);
4306 
4307         -- call the cache for the primary transaction book
4308         if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
4309                                   ,p_log_level_rec => g_log_level_rec) then
4310              raise FND_API.G_EXC_UNEXPECTED_ERROR;
4311         end if;
4312 
4313         -- l_book_class               := fa_cache_pkg.fazcbc_record.book_class;
4314         -- l_set_of_books_id          := fa_cache_pkg.fazcbc_record.set_of_books_id;
4315         -- l_distribution_source_book := fa_cache_pkg.fazcbc_record.distribution_source_book;
4316         -- l_mc_source_flag           := fa_cache_pkg.fazcbc_record.mc_source_flag;
4317 
4318         l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
4319 
4320         -- set the sob_id and currency context
4321         fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
4322         fnd_client_info.set_currency_context (to_char(l_asset_hdr_rec.set_of_books_id));
4323 
4324         -- BUG# 2376085, 2487707
4325         -- store the sob_id value for later use.
4326         fnd_profile.get ('GL_SET_OF_BKS_ID',l_orig_set_of_books_id);
4327         l_orig_currency_context :=  SUBSTRB(USERENV('CLIENT_INFO'),45,10);
4328 
4329         -- get the book type code P,R or N
4330         if not fa_cache_pkg.fazcsob
4331                (x_set_of_books_id   => l_asset_hdr_rec.set_of_books_id
4332                ,x_mrc_sob_type_code => l_reporting_flag
4333                ,p_log_level_rec => g_log_level_rec)
4334                then
4335                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
4336         end if;
4337 
4338         -- Error out if the program is submitted from the Reporting Responsibility
4339         -- No transaction permitted directly on reporting books.
4340         if l_reporting_flag = 'R' then
4341              FND_MESSAGE.set_name('GL','MRC_OSP_INVALID_BOOK_TYPE');
4342              FND_FILE.PUT_LINE(fnd_file.log,fnd_message.get);
4343              raise FND_API.G_EXC_UNEXPECTED_ERROR;
4344         end if;
4345 
4346 
4347    end if; -- book_type_code
4348 
4349    -- pop current period_rec info
4350    if g_log_level_rec.statement_level then
4351       fa_debug_pkg.add(l_calling_fn, 'pop period_rec', '', g_log_level_rec);
4352       fa_debug_pkg.add(l_calling_fn, 'book:',  l_asset_retire_rec.detail_info.book_type_code, g_log_level_rec);
4353    end if;
4354    if not FA_UTIL_PVT.get_period_rec
4355           (p_book           => l_asset_hdr_rec.book_type_code
4356           ,p_effective_date => NULL
4357           ,x_period_rec     => l_period_rec
4358           ,p_log_level_rec => g_log_level_rec) then
4359               raise FND_API.G_EXC_UNEXPECTED_ERROR;
4360    end if;
4361 
4362    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'passed pop period_rec', '', g_log_level_rec); end if;
4363 
4364    -- pop asset_type_rec
4365    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_type_rec', '', g_log_level_rec); end if;
4366    if not FA_UTIL_PVT.get_asset_type_rec
4367           (p_asset_hdr_rec      => l_asset_hdr_rec
4368           ,px_asset_type_rec    => l_asset_type_rec
4369           ,p_date_effective     => NULL
4370           ,p_log_level_rec => g_log_level_rec) then
4371               raise FND_API.G_EXC_UNEXPECTED_ERROR;
4372    end if;
4373 
4374    -- **********************************************
4375    -- **  Do asset-level validation on transaction
4376    -- **********************************************
4377    -- check that the status of the retirement to reinstate is PROCESSED
4378 
4379    -- get the latest transaction_date_entered from TH table
4380    open tde_cursor;
4381    fetch tde_cursor
4382          into l_trans_rec.transaction_date_entered;
4383    close tde_cursor;
4384 
4385    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'passed tde_cursor', '', g_log_level_rec); end if;
4386 
4387    -- default transaction_date_entered to the current period if it is null
4388    if l_trans_rec.transaction_date_entered is null then
4389 
4390         -- default date_retired to the current period
4391         l_trans_rec.transaction_date_entered
4392           := greatest(least(l_period_rec.calendar_period_close_date, sysdate)
4393                      ,l_period_rec.calendar_period_open_date
4394                      );
4395    end if;
4396 
4397    if g_log_level_rec.statement_level then
4398      fa_debug_pkg.add(l_calling_fn, 'passed transaction_date_entered', '', g_log_level_rec);
4399      fa_debug_pkg.add(l_calling_fn, 'Transaction Date: ', l_trans_rec.transaction_date_entered , g_log_level_rec);
4400    end if;
4401 
4402    -- *********************************************
4403    -- **  Do validation
4404    -- *********************************************
4405    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'before validation', '', g_log_level_rec); end if;
4406    -- validate that all user-entered input parameters are valid
4407    if not do_validation
4408           (p_validation_type   => g_reinstatement
4409           ,p_trans_rec         => l_trans_rec
4410           ,p_asset_hdr_rec     => l_asset_hdr_rec
4411           ,p_asset_desc_rec    => l_asset_desc_rec
4412           ,p_asset_type_rec    => l_asset_type_rec
4413           ,p_asset_fin_rec     => l_asset_fin_rec
4414           ,p_asset_retire_rec  => l_asset_retire_rec
4415           ,p_asset_dist_tbl    => l_asset_dist_tbl
4416           ,p_subcomp_tbl       => l_subcomp_tbl
4417           ,p_inv_tbl           => l_inv_tbl
4418           ,p_period_rec        => l_period_rec
4419           ,p_calling_fn        => p_calling_fn
4420           ,p_log_level_rec => g_log_level_rec) then
4421               raise FND_API.G_EXC_UNEXPECTED_ERROR;
4422    end if;
4423 
4424       if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'after validation', '', g_log_level_rec); end if;
4425 
4426    -- ***************************
4427    -- **  Main
4428    -- ***************************
4429 
4430    if not do_all_books_reinstatement
4431           (px_trans_rec        => l_trans_rec
4432           ,p_asset_hdr_rec     => l_asset_hdr_rec
4433           ,p_asset_desc_rec    => l_asset_desc_rec
4434           ,p_asset_type_rec    => l_asset_type_rec
4435           ,p_asset_fin_rec     => l_asset_fin_rec
4436           ,px_asset_retire_rec => l_asset_retire_rec
4437           ,p_asset_dist_tbl    => l_asset_dist_tbl
4438           ,p_subcomp_tbl       => l_subcomp_tbl
4439           ,p_inv_tbl           => l_inv_tbl
4440           ,p_period_rec        => l_period_rec
4441           ,p_log_level_rec => g_log_level_rec) then
4442               raise FND_API.G_EXC_ERROR;
4443    end if;
4444 
4445    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'after do_all_books_reinstatement', '', g_log_level_rec); end if;
4446 
4447 
4448    -- BUG# 2376085, 2487707
4449    -- reset GL sob id to original value before moving to next book
4450    fnd_profile.put('GL_SET_OF_BKS_ID', l_orig_set_of_books_id);
4451    fnd_client_info.set_currency_context (l_orig_currency_context);
4452 
4453 
4454    /* if book is a corporate book, process cip assets and autocopy */
4455 
4456    -- start processing tax books for cip-in-tax and autocopy
4457    if (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE') then
4458 
4459       lv_trans_rec := l_trans_rec;
4460       lv_asset_hdr_rec := l_asset_hdr_rec;
4461       lv_asset_retire_rec := l_asset_retire_rec;
4462 
4463       if (l_asset_type_rec.asset_type = 'CIP'
4464           or l_asset_type_rec.asset_type = 'CAPITALIZED') then
4465 
4466          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Asset type: ', l_asset_type_rec.asset_type, g_log_level_rec); end if;
4467 
4468          if not fa_cache_pkg.fazctbk
4469                 (x_corp_book    => l_asset_hdr_rec.book_type_code
4470                 ,x_asset_type   => l_asset_type_rec.asset_type
4471                 ,x_tax_book_tbl => l_tax_book_tbl
4472                 ,p_log_level_rec => g_log_level_rec) then
4473                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
4474          end if;
4475 
4476          for l_tax_index in 1..l_tax_book_tbl.count loop
4477 
4478            if g_log_level_rec.statement_level then
4479               fa_debug_pkg.add(l_calling_fn, 'entered loop for tax books', '', g_log_level_rec);
4480               fa_debug_pkg.add(l_calling_fn, 'selected tax book: ', l_tax_book_tbl(l_tax_index), g_log_level_rec);
4481            end if;
4482 
4483            if not FA_ASSET_VAL_PVT.validate_asset_book
4484                   (p_transaction_type_code      => l_trans_rec.transaction_type_code
4485                   ,p_book_type_code             => l_tax_book_tbl(l_tax_index)
4486                   ,p_asset_id                   => l_asset_hdr_rec.asset_id
4487                   ,p_calling_fn                 => l_calling_fn) then
4488 
4489                      null; -- just to ignore the error
4490                 if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'failed in validate_asset_book ', '', g_log_level_rec); end if;
4491 
4492            else
4493 
4494              -- cache the book information for the tax book
4495              if not fa_cache_pkg.fazcbc(x_book => l_tax_book_tbl(l_tax_index)) then
4496                   raise FND_API.G_EXC_UNEXPECTED_ERROR;
4497              end if;
4498 
4499              -- ? Excerpt from Brad's comment on this part - need more investigation:
4500              -- 'May need to set the transaction date, trx_type, subtype here as well
4501              --  based on the open period and settings for each tax book in the loop'
4502 
4503              lv_asset_hdr_rec.book_type_code           := l_tax_book_tbl(l_tax_index);
4504              lv_trans_rec.source_transaction_header_id := l_trans_rec.transaction_header_id;
4505              lv_trans_rec.transaction_header_id        := null;
4506 
4507              select max(retirement_id)
4508              into lv_asset_retire_rec.retirement_id
4509              from fa_retirements
4510              where asset_id=l_asset_hdr_rec.asset_id
4511                and book_type_code=lv_asset_hdr_rec.book_type_code;
4512 
4513              if g_log_level_rec.statement_level then
4514                fa_debug_pkg.add(l_calling_fn, 'calling do_all_books_reinstatement for tax book', '', g_log_level_rec);
4515                fa_debug_pkg.add(l_calling_fn, 'retirement_id for TAX:', lv_asset_retire_rec.retirement_id, g_log_level_rec);
4516              end if;
4517 
4518              if not do_all_books_reinstatement
4519                     (px_trans_rec        => lv_trans_rec     -- tax
4520                     ,p_asset_hdr_rec     => lv_asset_hdr_rec -- tax
4521                     ,p_asset_desc_rec    => l_asset_desc_rec
4522                     ,p_asset_type_rec    => l_asset_type_rec
4523                     ,p_asset_fin_rec     => l_asset_fin_rec
4524                     ,px_asset_retire_rec => lv_asset_retire_rec
4525                     ,p_asset_dist_tbl    => l_asset_dist_tbl
4526                     ,p_subcomp_tbl       => l_subcomp_tbl
4527                     ,p_inv_tbl           => l_inv_tbl
4528                     ,p_period_rec        => l_period_rec
4529                     ,p_log_level_rec => g_log_level_rec) then
4530                         raise FND_API.G_EXC_ERROR;
4531              end if;
4532 
4533 
4534              -- BUG# 2376085, 2487707
4535              -- reset GL sob id to original value before moving to next book
4536              fnd_profile.put('GL_SET_OF_BKS_ID', l_orig_set_of_books_id);
4537              fnd_client_info.set_currency_context (l_orig_currency_context);
4538 
4539              if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'passed do_all_books_reinstatement for tax book', '', g_log_level_rec); end if;
4540            end if;
4541 
4542          end loop;
4543 
4544 
4545       end if; -- asset_type
4546 
4547    end if; -- book_class
4548 
4549    -- submit calculate_gain_loss programs if flag is set
4550 
4551    if l_calculate_gain_loss_flag = FND_API.G_TRUE then
4552 
4553         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'submit calculate gain/loss program', '', g_log_level_rec); end if;
4554         if not calculate_gain_loss
4555                (p_retirement_id     => l_retirement_id
4556                ,p_mrc_sob_type_code => 'P'
4557                ,p_log_level_rec => g_log_level_rec) then
4558                    raise FND_API.G_EXC_UNEXPECTED_ERROR;
4559         end if;
4560 
4561    end if;
4562 
4563    -- commit if p_commit is TRUE.
4564    if FND_API.to_boolean(p_commit) then
4565       COMMIT WORK;
4566    end if;
4567 
4568    -- Standard call to get message count and if count is 1 get message info.
4569    FND_MSG_PUB.count_and_get(p_count   => x_msg_count
4570                             ,p_data    => x_msg_data
4571                             );
4572 
4573    -- set back to original environment when the procedure is finished
4574    fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
4575    fnd_client_info.set_currency_context (g_orig_currency_context);
4576 
4577    -- return the status.
4578    x_return_status := FND_API.G_RET_STS_SUCCESS;
4579 
4580 EXCEPTION
4581 
4582    when FND_API.G_EXC_ERROR then
4583 
4584           ROLLBACK TO do_reinstatement;
4585 
4586           x_return_status := FND_API.G_RET_STS_ERROR;
4587 
4588           if g_token1 is null then
4589                fa_srvr_msg.add_message(calling_fn => l_calling_fn
4590                                       ,name       => g_msg_name
4591                                       ,p_log_level_rec => g_log_level_rec);
4592           else
4593                fa_srvr_msg.add_message(calling_fn => l_calling_fn
4594                                       ,name       => g_msg_name
4595                                       ,token1     => g_token1
4596                                       ,value1     => g_value1
4597                                       ,p_log_level_rec => g_log_level_rec);
4598           end if;
4599 
4600           FND_MSG_PUB.count_and_get(p_count => x_msg_count
4601                                    ,p_data  => x_msg_data
4602                                    );
4603           -- set back to original environment when the procedure is finished
4604           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
4605           fnd_client_info.set_currency_context (g_orig_currency_context);
4606 
4607    when FND_API.G_EXC_UNEXPECTED_ERROR then
4608 
4609           ROLLBACK TO do_reinstatement;
4610 
4611           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4612 
4613           fa_srvr_msg.add_message(calling_fn => l_calling_fn
4614                     ,p_log_level_rec => g_log_level_rec);
4615           FND_MSG_PUB.count_and_get(p_count => x_msg_count
4616                                    ,p_data  => x_msg_data
4617                                    );
4618           -- set back to original environment when the procedure is finished
4619           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
4620           fnd_client_info.set_currency_context (g_orig_currency_context);
4621 
4622    when others then
4623 
4624           ROLLBACK TO do_reinstatement;
4625 
4626           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4627 
4628           fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
4629                     ,p_log_level_rec => g_log_level_rec);
4630           FND_MSG_PUB.count_and_get(p_count => x_msg_count
4631                                    ,p_data  => x_msg_data
4632                                    );
4633           -- set back to original environment when the procedure is finished
4634           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
4635           fnd_client_info.set_currency_context (g_orig_currency_context);
4636 
4637 
4638 END do_reinstatement;
4639 -----------------------------------------------------------------------------
4640 
4641 FUNCTION do_all_books_reinstatement
4642         (px_trans_rec          in out NOCOPY FA_API_TYPES.trans_rec_type
4643         ,p_asset_hdr_rec       in     FA_API_TYPES.asset_hdr_rec_type
4644         ,p_asset_desc_rec      in     FA_API_TYPES.asset_desc_rec_type
4645         ,p_asset_type_rec      in     FA_API_TYPES.asset_type_rec_type
4646         ,p_asset_fin_rec       in     FA_API_TYPES.asset_fin_rec_type
4647         ,px_asset_retire_rec   in out NOCOPY FA_API_TYPES.asset_retire_rec_type
4648         ,p_asset_dist_tbl      in     FA_API_TYPES.asset_dist_tbl_type
4649         ,p_subcomp_tbl         in     FA_API_TYPES.subcomp_tbl_type
4650         ,p_inv_tbl             in     FA_API_TYPES.inv_tbl_type
4651         ,p_period_rec          in     FA_API_TYPES.period_rec_type
4652         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
4653 -- will return retirement_id of asset_retire_rec
4654 IS
4655 
4656    -- Returns the reporting GL set_of_books_ids
4657    -- associated with the set_of_books_id of given primary book_type_code
4658    CURSOR sob_cursor(p_book_type_code in varchar2
4659                     ,p_sob_id         in number) is
4660    SELECT set_of_books_id AS sob_id
4661      FROM fa_mc_book_controls
4662     WHERE book_type_code          = p_book_type_code
4663       AND primary_set_of_books_id = p_sob_id
4664       AND enabled_flag            = 'Y';
4665 
4666    -- used for main transaction book
4667    l_book_class                 varchar2(15);
4668    l_set_of_books_id            number;
4669    l_distribution_source_book   varchar2(15);
4670    l_mc_source_flag             varchar2(1);
4671 
4672    -- local asset info
4673    l_trans_rec         FA_API_TYPES.trans_rec_type;
4674    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
4675    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
4676    l_asset_type_rec    FA_API_TYPES.asset_type_rec_type;
4677    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
4678    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
4679    l_asset_dist_tbl    FA_API_TYPES.asset_dist_tbl_type;
4680    l_subcomp_tbl       FA_API_TYPES.subcomp_tbl_type;
4681    l_inv_tbl           FA_API_TYPES.inv_tbl_type;
4682    l_period_rec        FA_API_TYPES.period_rec_type;
4683 
4684    -- local individual variables
4685    l_latest_trans_date          date;
4686 
4687    -- local conversion rate
4688    l_exchange_date              date;
4689    l_rate                       number;
4690 
4691    -- msg
4692    g_msg_name                   varchar2(30);
4693 
4694    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.do_all_books_reinstatement';
4695 
4696 BEGIN
4697 
4698 
4699    -- ****************************************************
4700    -- **  Assign input parameters to local rec/tbl types
4701    -- ****************************************************
4702    l_trans_rec        := px_trans_rec;
4703    l_asset_hdr_rec    := p_asset_hdr_rec;
4704    l_asset_desc_rec   := p_asset_desc_rec;
4705    l_asset_type_rec   := p_asset_type_rec;
4706    l_asset_retire_rec := px_asset_retire_rec;
4707    l_asset_dist_tbl   := p_asset_dist_tbl;
4708    l_subcomp_tbl      := p_subcomp_tbl;
4709    l_inv_tbl          := p_inv_tbl;
4710 
4711    -- *********************************
4712    -- **  Populate local record types
4713    -- *********************************
4714    -- populate rec_types that were not provided by users
4715 
4716    -- call the cache for the primary transaction book
4717    if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
4718   ,p_log_level_rec => p_log_level_rec) then
4719          raise FND_API.G_EXC_UNEXPECTED_ERROR;
4720    end if;
4721 
4722    l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
4723 
4724    -- set the sob_id and currency context
4725    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
4726    fnd_client_info.set_currency_context(to_char(l_asset_hdr_rec.set_of_books_id));
4727 
4728    -- pop current period_rec info
4729    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop period_rec', '', p_log_level_rec); end if;
4730    if not FA_UTIL_PVT.get_period_rec
4731           (p_book           => l_asset_hdr_rec.book_type_code
4732           ,p_effective_date => NULL
4733           ,x_period_rec     => l_period_rec
4734 ,p_log_level_rec => p_log_level_rec) then
4735               raise FND_API.G_EXC_UNEXPECTED_ERROR;
4736    end if;
4737 
4738    -- ***************************************************
4739    -- **  Do asset/book-level validation on transaction
4740    -- ***************************************************
4741    -- begin asset/book-level validation on transaction
4742 
4743    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin asset/book-level validation on transaction', '', p_log_level_rec); end if;
4744 
4745    -- check if there is an add-to-asset transaction pending
4746    -- Users must post their mass additions before they can retire the asset
4747    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if there is an add-to-asset transaction pending', '', p_log_level_rec); end if;
4748    if FA_ASSET_VAL_PVT.validate_add_to_asset_pending
4749       (p_asset_id   => l_asset_hdr_rec.asset_id
4750       ,p_book       => l_asset_hdr_rec.book_type_code
4751       ,p_log_level_rec => p_log_level_rec) then
4752           -- Users must post their mass additions before they can retire the asset
4753           g_msg_name := 'FA_RET_CANT_RET_INCOMPLETE_ASS';
4754           raise FND_API.G_EXC_ERROR;
4755    end if;
4756 
4757    -- check if another retirement/reinstatement already pending
4758    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if another retirement/reinstatement already pending', '', p_log_level_rec); end if;
4759    if FA_ASSET_VAL_PVT.validate_ret_rst_pending
4760       (p_asset_id   => l_asset_hdr_rec.asset_id
4761       ,p_book       => l_asset_hdr_rec.book_type_code
4762       ,p_log_level_rec => p_log_level_rec) then
4763           g_msg_name := 'FA_SHARED_PENDING_RETIREMENT';
4764           raise FND_API.G_EXC_ERROR;
4765    end if;
4766 
4767    -- check if date_retired is valid in terms of trx date
4768    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if date_retired is valid in terms of trx date', '', p_log_level_rec); end if;
4769    if l_asset_retire_rec.date_retired is not null then
4770 
4771         -- no transactions except Retirements and Reinstatements may be
4772         -- dated after the latest trx date
4773         if not FA_UTIL_PVT.get_latest_trans_date
4774                (p_calling_fn        => l_calling_fn
4775                ,p_asset_id          => l_asset_hdr_rec.asset_id
4776                ,p_book              => l_asset_hdr_rec.book_type_code
4777                ,x_latest_trans_date => l_latest_trans_date
4778                ,p_log_level_rec => p_log_level_rec) then
4779                    raise FND_API.G_EXC_UNEXPECTED_ERROR;
4780         end if;
4781 
4782         -- Bug3130595
4783         --  Removing following check since we need to allow reinstatement
4784         --  of retirement even there are another trx in between ret and reinstatement
4785         --
4786         --if l_asset_retire_rec.date_retired < l_latest_trans_date then
4787         --     g_msg_name := 'FA_SHARED_OTHER_TRX_FOLLOW';
4788         --     raise FND_API.G_EXC_ERROR;
4789         --end if;
4790 
4791    end if; -- date_retired
4792 
4793    -- Pop the transaction_header_id for the REINSTATEMENT row
4794    select fa_transaction_headers_s.nextval
4795    into   l_trans_rec.transaction_header_id
4796    from   dual;
4797 
4798    -- SLA UPTAKE
4799    -- moving original event creation into do_sub_regular_reinstatement
4800 
4801    -- ***************************
4802    -- **  Main
4803    -- ***************************
4804 
4805    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling do_sub_retirement for each book/sob', '', p_log_level_rec); end if;
4806 
4807    l_rate := 1;
4808    if not do_sub_reinstatement
4809           (px_trans_rec        => l_trans_rec
4810           ,p_asset_hdr_rec     => l_asset_hdr_rec
4811           ,p_asset_desc_rec    => l_asset_desc_rec
4812           ,p_asset_type_rec    => l_asset_type_rec
4813           ,p_asset_fin_rec     => l_asset_fin_rec
4814           ,px_asset_retire_rec => l_asset_retire_rec
4815           ,p_asset_dist_tbl    => l_asset_dist_tbl
4816           ,p_subcomp_tbl       => l_subcomp_tbl
4817           ,p_inv_tbl           => l_inv_tbl
4818           ,p_period_rec        => l_period_rec
4819           ,p_rate              => l_rate
4820           ,p_mrc_sob_type_code => 'P'
4821           ,p_log_level_rec => p_log_level_rec) then
4822               raise FND_API.G_EXC_UNEXPECTED_ERROR;
4823    end if;
4824 
4825 
4826    if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
4827                              ,p_log_level_rec => p_log_level_rec) then
4828         fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
4829                 ,p_log_level_rec => p_log_level_rec);
4830         return FALSE;
4831    end if;
4832 
4833    -- MRC LOOP
4834    -- if this is a primary book, process reporting books(sobs)
4835    if fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y' then
4836 
4837        -- loop thourgh reporting set of books
4838        for sob_rec in sob_cursor(l_asset_hdr_rec.book_type_code
4839                                 ,l_asset_hdr_rec.set_of_books_id) loop
4840 
4841          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in sob_id loop', '', p_log_level_rec); end if;
4842 
4843          -- set the sob_id and currency context for reporting book(sob)
4844          fnd_profile.put('GL_SET_OF_BKS_ID', sob_rec.sob_id);
4845          fnd_client_info.set_currency_context(to_char(sob_rec.sob_id));
4846 
4847          l_asset_hdr_rec.set_of_books_id := sob_rec.sob_id;
4848 
4849          if not fa_cache_pkg.fazcbcs(x_book => l_asset_hdr_rec.book_type_code
4850                                     ,p_log_level_rec => p_log_level_rec) then
4851             fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
4852                         ,p_log_level_rec => p_log_level_rec);
4853             return FALSE;
4854          end if;
4855 
4856          -- ? code for conversion rate when invoice trx is involved will be
4857          -- discussed later
4858          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'get currency conversion rates', '', p_log_level_rec); end if;
4859          -- if l_inv_trans_rec.transaction_type is null then
4860 
4861          /******* routine to get existing rates when invoice trx is involved
4862          -- if (l_inv_rec.source_transaction_header_id is not null)
4863              and (fa_cache_pkg.fazcbc_record.book_class = 'TAX') then
4864 
4865                if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling get_existing_rate', '', p_log_level_rec); end if;
4866 
4867                -- get the exchange rate from the corporate transaction
4868                if not FA_MC_UTIL_PVT.get_existing_rate
4869                   (p_set_of_books_id        => sob_rec.sob_id,
4870                    p_transaction_header_id  => l_trans_rec.source_transaction_header_id,
4871                    px_rate                  => l_rate
4872                    ,p_log_level_rec => p_log_level_rec) then return false;
4873                end if;
4874 
4875          *******/
4876 
4877          if TRUE then
4878 
4879                l_exchange_date    := l_trans_rec.transaction_date_entered;
4880 
4881                if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling get_trx_rate', '', p_log_level_rec); end if;
4882 
4883                if not FA_MC_UTIL_PVT.get_trx_rate
4884                   (p_prim_set_of_books_id       => fa_cache_pkg.fazcbc_record.set_of_books_id,
4885                    p_reporting_set_of_books_id  => sob_rec.sob_id,
4886                    px_exchange_date             => l_exchange_date,
4887                    p_book_type_code             => l_asset_hdr_rec.book_type_code,
4888                    px_rate                      => l_rate
4889                    ,p_log_level_rec => p_log_level_rec)then return false;
4890                end if;
4891 
4892          end if;
4893 
4894          -- else
4895             -- ? code for conversion rate when invoice trx is involved will be
4896             -- discussed later
4897          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'NOTE: Invoice trx can not be handled in mrc for now', '', p_log_level_rec); end if;
4898 
4899          -- end if; -- if invoice trx is not involved
4900 
4901 
4902          if not do_sub_reinstatement
4903                 (px_trans_rec        => l_trans_rec
4904                 ,p_asset_hdr_rec     => l_asset_hdr_rec
4905                 ,p_asset_desc_rec    => l_asset_desc_rec
4906                 ,p_asset_type_rec    => l_asset_type_rec
4907                 ,p_asset_fin_rec     => l_asset_fin_rec
4908                 ,px_asset_retire_rec => l_asset_retire_rec
4909                 ,p_asset_dist_tbl    => l_asset_dist_tbl
4910                 ,p_subcomp_tbl       => l_subcomp_tbl
4911                 ,p_inv_tbl           => l_inv_tbl
4912                 ,p_period_rec        => l_period_rec
4913                 ,p_rate              => l_rate
4914                 ,p_mrc_sob_type_code => 'R'
4915                 ,p_log_level_rec => p_log_level_rec) then
4916                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
4917          end if;
4918 
4919          -- ? may need to call MC_FA_UTILITIES_PKG.insert_books_rates procedure
4920          -- to process invoice trx
4921 
4922        end loop;
4923 
4924    end if;
4925 
4926    px_trans_rec := l_trans_rec;
4927 
4928    return TRUE;
4929 
4930 EXCEPTION
4931 
4932    when others then
4933 
4934           if g_token1 is null then
4935                fa_srvr_msg.add_message(calling_fn => l_calling_fn
4936                                       ,name       => g_msg_name
4937                                       ,p_log_level_rec => p_log_level_rec);
4938           else
4939                fa_srvr_msg.add_message(calling_fn => l_calling_fn
4940                                       ,name       => g_msg_name
4941                                       ,token1     => g_token1
4942                                       ,value1     => g_value1
4943                                       ,p_log_level_rec => p_log_level_rec);
4944           end if;
4945 
4946           return FALSE;
4947 
4948 END do_all_books_reinstatement;
4949 
4950 
4951 FUNCTION do_sub_reinstatement
4952         (px_trans_rec           in out NOCOPY FA_API_TYPES.trans_rec_type
4953         ,p_asset_hdr_rec        in     FA_API_TYPES.asset_hdr_rec_type
4954         ,p_asset_desc_rec       in     FA_API_TYPES.asset_desc_rec_type
4955         ,p_asset_type_rec       in     FA_API_TYPES.asset_type_rec_type
4956         ,p_asset_fin_rec        in     FA_API_TYPES.asset_fin_rec_type
4957         ,px_asset_retire_rec    in out NOCOPY FA_API_TYPES.asset_retire_rec_type
4958         ,p_asset_dist_tbl       in     FA_API_TYPES.asset_dist_tbl_type
4959         ,p_subcomp_tbl          in     FA_API_TYPES.subcomp_tbl_type
4960         ,p_inv_tbl              in     FA_API_TYPES.inv_tbl_type
4961         ,p_period_rec           in     FA_API_TYPES.period_rec_type
4962         ,p_rate                 in     number
4963         ,p_mrc_sob_type_code    in     VARCHAR2
4964         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
4965 -- will return retirement_id of px_asset_retire_rec
4966 IS
4967 
4968    -- local asset info
4969    l_trans_rec         FA_API_TYPES.trans_rec_type;
4970    lv_trans_rec        FA_API_TYPES.trans_rec_type;
4971    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
4972    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
4973    l_asset_type_rec    FA_API_TYPES.asset_type_rec_type;
4974    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
4975    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
4976    l_asset_dist_tbl    FA_API_TYPES.asset_dist_tbl_type;
4977    l_subcomp_tbl       FA_API_TYPES.subcomp_tbl_type;
4978    l_inv_tbl           FA_API_TYPES.inv_tbl_type;
4979    l_period_rec        FA_API_TYPES.period_rec_type;
4980 
4981    l_asset_cat_rec     FA_API_TYPES.asset_cat_rec_type;
4982 
4983    l_rate              number;
4984 
4985    l_fraction_remaining        number;
4986    l_deprn_rounding_flag       varchar2(30);
4987    l_period_counter_fully_ret  number;
4988 
4989    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.do_sub_reinstatement';
4990 
4991 BEGIN
4992 
4993 
4994    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin ', l_calling_fn, p_log_level_rec); end if;
4995    -- ****************************************************
4996    -- **  Assign input parameters to local rec/tbl types
4997    -- ****************************************************
4998    l_trans_rec        := px_trans_rec;
4999    l_asset_hdr_rec    := p_asset_hdr_rec;
5000    l_asset_desc_rec   := p_asset_desc_rec;
5001    l_asset_type_rec   := p_asset_type_rec;
5002    l_asset_retire_rec := px_asset_retire_rec;
5003    l_asset_dist_tbl   := p_asset_dist_tbl;
5004    l_subcomp_tbl      := p_subcomp_tbl;
5005    l_inv_tbl          := p_inv_tbl;
5006 
5007    l_rate             := p_rate;
5008 
5009    -- ***************************
5010    -- **  Pop local rec types
5011    -- ***************************
5012 
5013    -- set the sob_id and currency context
5014    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
5015    fnd_client_info.set_currency_context(to_char(l_asset_hdr_rec.set_of_books_id));
5016 
5017    -- pop asset_fin_rec
5018    -- get fa_books row where transaction_header_id_out is null
5019    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_fin_rec', '', p_log_level_rec); end if;
5020    if not FA_UTIL_PVT.get_asset_fin_rec
5021           (p_asset_hdr_rec         => l_asset_hdr_rec
5022           ,px_asset_fin_rec        => l_asset_fin_rec
5023           ,p_transaction_header_id => NULL
5024           ,p_mrc_sob_type_code     => p_mrc_sob_type_code
5025           ,p_log_level_rec => p_log_level_rec) then
5026               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5027    end if;
5028 
5029    -- pop asset_retire_rec
5030    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_retire_rec', '', p_log_level_rec); end if;
5031    if not FA_UTIL_PVT.get_asset_retire_rec
5032           (px_asset_retire_rec => l_asset_retire_rec,
5033            p_mrc_sob_type_code     => p_mrc_sob_type_code
5034            ,p_log_level_rec => p_log_level_rec) then
5035               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5036    end if;
5037 
5038    -- pop asset_cat_rec
5039    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop asset_cat_rec', '', p_log_level_rec); end if;
5040    if not FA_UTIL_PVT.get_asset_cat_rec
5041           (p_asset_hdr_rec     => l_asset_hdr_rec
5042           ,px_asset_cat_rec    => l_asset_cat_rec
5043           ,p_date_effective    => NULL
5044           ,p_log_level_rec => p_log_level_rec) then
5045               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5046    end if;
5047 
5048    -- ***************************
5049    -- **  Do basic calculation
5050    -- ***************************
5051    -- nothing to add here now
5052 
5053    -- ***************************
5054    -- **  Main
5055    -- ***************************
5056 
5057    if not do_sub_regular_reinstatement
5058           (px_trans_rec        => l_trans_rec
5059           ,p_asset_hdr_rec     => l_asset_hdr_rec
5060           ,p_asset_desc_rec    => l_asset_desc_rec
5061           ,p_asset_type_rec    => l_asset_type_rec
5062           ,p_asset_fin_rec     => l_asset_fin_rec
5063           ,px_asset_retire_rec => l_asset_retire_rec
5064           ,p_asset_dist_tbl    => l_asset_dist_tbl
5065           ,p_subcomp_tbl       => l_subcomp_tbl
5066           ,p_inv_tbl           => l_inv_tbl
5067           ,p_period_rec        => l_period_rec
5068           ,p_mrc_sob_type_code => p_mrc_sob_type_code
5069           ,p_log_level_rec => p_log_level_rec) then
5070               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5071    end if;
5072 
5073    /*************************************************************
5074    * Partial unit reinstatement will be handled
5075    * once remaining part of the process handled in gain/loss
5076    * is verified...
5077    *-------------------------------------------------------------
5078         -- make a local copy of trans_rec and change trx_type_code
5079         -- this local copy of trans_rec will be used
5080         -- for distribution api and fautfr
5081         lv_trans_rec := l_trans_rec;
5082         lv_trans_rec.transaction_type_code := 'TRANSFER OUT';
5083         lv_trans_rec.transaction_header_id := null;
5084 
5085         -- FYI: current_units is used as parameter of units_retired in Distribution API
5086         l_asset_desc_rec.current_units := l_asset_retire_rec.units_retired;
5087 
5088         -- FYI: call distribution api
5089         -- only when set of books is a primary GL book
5090         -- and FA book is a corporate book
5091         -- and transaction is a partial unit retirement.
5092         -- Assumption: fautfr in distribution API
5093         --             is handling all MRC part of adjustments table
5094         if (l_asset_dist_tbl.count > 0 )
5095            and
5096            (l_asset_hdr_rec.set_of_books_id
5097              = fa_cache_pkg.fazcbc_record.set_of_books_id)
5098            and
5099            (fa_cache_pkg.fazcbc_record.book_class='CORPORATE') then
5100 
5101              -- Call distribution API to process partial-unit reinstatement.
5102              -- do_distribution will handle TH, DH, AD and AH tables for TRANSFER OUT transaction
5103              -- and call 'fautfr' function in it.
5104 
5105              -- assuming that fautfr is inserting adjustment rows for TAX books.
5106              -- and calculate gain/loss is taking care of those for CORPORATE book.
5107 
5108              -- Required parameters for TRANSFER OUT transaction
5109              --   trans_rec: transaction_date_entered
5110              --   asset_hdr_rec: asset_id, book_type_code(only CORPORATE)
5111              --   asset_dist_tbl: distribution_id, trx_units
5112 
5113              if g_log_level_rec.statement_level then
5114                fa_debug_pkg.add(l_calling_fn, 'trx_type_code:', lv_trans_rec.transaction_type_code, p_log_level_rec);
5115                fa_debug_pkg.add(l_calling_fn, 'trx_date_entered:', lv_trans_rec.transaction_date_entered, p_log_level_rec);
5116                fa_debug_pkg.add(l_calling_fn, 'asset_id:', l_asset_hdr_rec.asset_id, p_log_level_rec);
5117                fa_debug_pkg.add(l_calling_fn, 'book_type_code:', l_asset_hdr_rec.book_type_code, p_log_level_rec);
5118              end if;
5119 
5120              --for i in 1..l_asset_dist_tbl.count loop
5121 
5122                if g_log_level_rec.statement_level then
5123                   fa_debug_pkg.add(l_calling_fn, 'dist_id:', l_asset_dist_tbl(1).distribution_id, p_log_level_rec);
5124                   fa_debug_pkg.add(l_calling_fn, 'trx_units:', l_asset_dist_tbl(1).transaction_units, p_log_level_rec);
5125                end if;
5126 
5127              --end loop;
5128 
5129              if not FA_DISTRIBUTION_PVT.do_distribution
5130                     (px_trans_rec            => lv_trans_rec
5131                     ,px_asset_hdr_rec        => l_asset_hdr_rec
5132                     ,px_asset_cat_rec_new    => l_asset_cat_rec
5133                     ,px_asset_dist_tbl       => l_asset_dist_tbl
5134                     ,p_log_level_rec => p_log_level_rec) then
5135                         raise FND_API.G_EXC_UNEXPECTED_ERROR;
5136              end if;
5137 
5138         end if;
5139         ********************************/
5140 
5141    --
5142    -- If this is member asset and Recognize Gain Loss is set
5143    -- to "NO", create adjustment entries right away and
5144    -- make sure gain loss won't process this reinstatement later.
5145    --
5146    if (l_asset_fin_rec.group_asset_id is not null) and
5147       (l_asset_retire_rec.recognize_gain_loss = 'NO') then
5148 
5149       if not FA_RETIREMENT_PVT.DO_REINSTATEMENT(
5150                       p_trans_rec         => l_trans_rec,
5151                       p_asset_retire_rec  => l_asset_retire_rec,
5152                       p_asset_hdr_rec     => l_asset_hdr_rec,
5153                       p_asset_type_rec    => l_asset_type_rec,
5154                       p_asset_cat_rec     => l_asset_cat_rec,
5155                       p_asset_fin_rec     => l_asset_fin_rec,
5156                       p_asset_desc_rec    => l_asset_desc_rec,
5157                       p_period_rec        => l_period_rec,
5158                       p_mrc_sob_type_code => p_mrc_sob_type_code,
5159                       p_calling_fn        => l_calling_fn
5160                       ,p_log_level_rec => p_log_level_rec) then
5161          raise FND_API.G_EXC_UNEXPECTED_ERROR;
5162       end if;
5163 
5164    end if;
5165 
5166    px_trans_rec := l_trans_rec;
5167 
5168    return TRUE;
5169 
5170 EXCEPTION
5171 
5172    when others then
5173 
5174           if g_token1 is null then
5175                fa_srvr_msg.add_message(calling_fn => l_calling_fn
5176                                       ,name       => g_msg_name
5177                                       ,p_log_level_rec => p_log_level_rec);
5178           else
5179                fa_srvr_msg.add_message(calling_fn => l_calling_fn
5180                                       ,name       => g_msg_name
5181                                       ,token1     => g_token1
5182                                       ,value1     => g_value1
5183                                       ,p_log_level_rec => p_log_level_rec);
5184           end if;
5185 
5186           return FALSE;
5187 
5188 
5189 END do_sub_reinstatement;
5190 ----------------------------------------------------------------
5191 
5192 FUNCTION do_sub_regular_reinstatement
5193         (px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
5194         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
5195         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
5196         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
5197         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
5198         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
5199         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
5200         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
5201         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
5202         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
5203         ,p_mrc_sob_type_code          in     VARCHAR2
5204         ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
5205 IS
5206 
5207    -- local asset info
5208    l_trans_rec        FA_API_TYPES.trans_rec_type;
5209    l_asset_hdr_rec    FA_API_TYPES.asset_hdr_rec_type;
5210    l_asset_desc_rec   FA_API_TYPES.asset_desc_rec_type;
5211    l_asset_fin_rec    FA_API_TYPES.asset_fin_rec_type;
5212    l_asset_retire_rec FA_API_TYPES.asset_retire_rec_type;
5213    l_asset_dist_tbl   FA_API_TYPES.asset_dist_tbl_type;
5214    l_subcomp_tbl      FA_API_TYPES.subcomp_tbl_type;
5215    l_inv_tbl          FA_API_TYPES.inv_tbl_type;
5216    l_period_rec       FA_API_TYPES.period_rec_type;
5217 
5218    l_asset_cat_rec             FA_API_TYPES.asset_cat_rec_type;
5219 
5220    l_rowid                     ROWID;
5221    l_Fraction_Remaining        number;
5222    l_deprn_rounding_flag       varchar2(30);
5223    l_period_counter_fully_ret  number := null;
5224 
5225    l_percent_salvage_value     number := 0;
5226 
5227    l_adjusted_cost_new         number := 0;
5228    l_cost_new                  number := 0;
5229    l_salvage_value_new         number := 0;
5230    l_unrevalued_cost_new       number := 0;
5231    l_recoverable_cost_new      number := 0;
5232    l_recoverable               number := 0;
5233    l_adjusted_rec_cost         number := 0;
5234 
5235    l_status                    boolean := TRUE;
5236 
5237    /*
5238     * Getting FA_TRANSACTION_HEADERS.INVOICE_TRANSACTION_ID
5239     */
5240    cursor c_inv_trx_id (c_thid number) is
5241    select invoice_transaction_id
5242    from   fa_transaction_headers
5243    where  transaction_header_id = c_thid;
5244 
5245    l_invoice_transaction_id    number;  -- Local variable to store return value of c_inv_trx_id;
5246 
5247    l_event_status              varchar2(1);  -- SLA status
5248 
5249    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.do_sub_regular_reinstatement';
5250 
5251 BEGIN
5252 
5253 
5254    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin ', l_calling_fn, p_log_level_rec); end if;
5255 
5256    -- ****************************************************
5257    -- **  Assign input parameters to local rec/tbl types
5258    -- ****************************************************
5259    l_trans_rec        := px_trans_rec;
5260    l_asset_hdr_rec    := p_asset_hdr_rec;
5261    l_asset_retire_rec := px_asset_retire_rec;
5262    l_asset_dist_tbl   := p_asset_dist_tbl;
5263    l_subcomp_tbl      := p_subcomp_tbl;
5264    l_inv_tbl          := p_inv_tbl;
5265    l_asset_fin_rec    := p_asset_fin_rec;
5266 
5267    -- ***************************
5268    -- **  Main
5269    -- ***************************
5270 
5271    -- SLA Uptake - moving this to top so we know before event creation
5272 
5273    -- If the asset is member asset and Recognize Gain Loss is
5274    -- 'NO', set the status to 'DELETED' since all other process
5275    -- will be taken care in FA_RETIREMENT_PVT.DO_REINSTATEMENT
5276    -- and this asset should not be picked up by FARET
5277    --
5278    if (p_asset_fin_rec.group_asset_id is not null) and
5279       (l_asset_retire_rec.recognize_gain_loss = 'NO') then
5280      l_asset_retire_rec.status := 'DELETED';
5281    else
5282      l_asset_retire_rec.status := 'REINSTATE';
5283    end if;
5284 
5285 
5286    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'set_of_books_id: ', l_asset_hdr_rec.set_of_books_id, p_log_level_rec); end if;
5287 
5288    if (p_mrc_sob_type_code <> 'R') then
5289 
5290       if ((l_asset_retire_rec.units_retired = l_asset_desc_rec.current_units
5291           )
5292           or (l_asset_retire_rec.cost_retired = l_asset_fin_rec.cost
5293              and l_asset_retire_rec.cost_retired <> 0
5294              )
5295           or (l_asset_retire_rec.cost_retired = l_asset_fin_rec.cost
5296              and l_asset_retire_rec.cost_retired = 0
5297              and l_asset_retire_rec.units_retired is NULL
5298              )) then
5299 
5300              l_trans_rec.transaction_type_code := 'REINSTATEMENT';
5301              l_period_counter_fully_ret := l_period_rec.period_counter;
5302              --l_deprn_rounding_flag := 'RET';
5303 
5304       else
5305              l_trans_rec.transaction_type_code := 'REINSTATEMENT';
5306              l_period_counter_fully_ret := NULL;
5307              --l_deprn_rounding_flag := 'RET';
5308 
5309       end if;
5310 
5311       l_trans_rec.who_info.creation_date := sysdate;
5312       l_trans_rec.who_info.last_update_date := sysdate;
5313 
5314 
5315       -- SLA UPTAKE
5316       -- moving original event creation into do_sub_regular_reinstatement
5317       -- assign an event for the transaction
5318       -- at this point key info asset/book/trx info is known from above code
5319 
5320       if (l_asset_retire_rec.status = 'REINSTATE') then
5321          l_event_status := XLA_EVENTS_PUB_PKG.C_EVENT_INCOMPLETE;
5322       else
5323          l_event_status := XLA_EVENTS_PUB_PKG.C_EVENT_UNPROCESSED;
5324       end if;
5325 
5326       if not fa_xla_events_pvt.create_transaction_event
5327            (p_asset_hdr_rec => l_asset_hdr_rec,
5328             p_asset_type_rec=> p_asset_type_rec,
5329             px_trans_rec    => l_trans_rec,
5330             p_event_status  => l_event_status,
5331             p_calling_fn    => 'FA_RETIREMENT_PUB.do_sub_regular_reinstatement',
5332             p_log_level_rec => p_log_level_rec) then
5333          raise FND_API.G_EXC_UNEXPECTED_ERROR;
5334       end if;
5335 
5336       if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'do fa_transaction_headers_pkg.insert_row', '', p_log_level_rec); end if;
5337 
5338       fa_transaction_headers_pkg.insert_row
5339                       (x_rowid                          => l_rowid,
5340                        x_transaction_header_id          => l_trans_rec.transaction_header_id,
5341                        x_book_type_code                 => l_asset_hdr_rec.book_type_code,
5342                        x_asset_id                       => l_asset_hdr_rec.asset_id,
5343                        x_transaction_type_code          => l_trans_rec.transaction_type_code,
5344                        x_transaction_date_entered       => l_trans_rec.transaction_date_entered,
5345                        x_date_effective                 => l_trans_rec.who_info.creation_date,
5346                        x_last_update_date               => l_trans_rec.who_info.last_update_date,
5347                        x_last_updated_by                => l_trans_rec.who_info.last_updated_by,
5348                        x_transaction_name               => null, --l_trans_rec.transaction_name,
5349                        x_invoice_transaction_id         => null,
5350                        x_source_transaction_Header_id   => l_trans_rec.source_transaction_header_id,
5351                        x_mass_reference_id              => l_trans_rec.mass_reference_id,
5352                        x_last_Update_login              => l_trans_rec.who_info.last_update_login,
5353                        x_transaction_subtype            => null, --l_trans_rec.transaction_subtype
5354                        x_Attribute1                     => l_trans_rec.desc_flex.attribute1,
5355                        x_Attribute2                     => l_trans_rec.desc_flex.attribute2,
5356                        x_Attribute3                     => l_trans_rec.desc_flex.attribute3,
5357                        x_Attribute4                     => l_trans_rec.desc_flex.attribute4,
5358                        x_Attribute5                     => l_trans_rec.desc_flex.attribute5,
5359                        x_Attribute6                     => l_trans_rec.desc_flex.attribute6,
5360                        x_Attribute7                     => l_trans_rec.desc_flex.attribute7,
5361                        x_Attribute8                     => l_trans_rec.desc_flex.attribute8,
5362                        x_Attribute9                     => l_trans_rec.desc_flex.attribute9,
5363                        x_Attribute10                    => l_trans_rec.desc_flex.attribute10,
5364                        x_Attribute11                    => l_trans_rec.desc_flex.attribute11,
5365                        x_Attribute12                    => l_trans_rec.desc_flex.attribute12,
5366                        x_Attribute13                    => l_trans_rec.desc_flex.attribute13,
5367                        x_Attribute14                    => l_trans_rec.desc_flex.attribute14,
5368                        x_Attribute15                    => l_trans_rec.desc_flex.attribute15,
5369                        x_attribute_category_code        => l_trans_rec.desc_flex.attribute_category_code,
5370                        x_transaction_key                => 'R', -- l_trans_rec.transaction_key
5371                        x_mass_transaction_id            => l_trans_rec.mass_transaction_id,
5372                        x_calling_interface              => l_trans_rec.calling_interface,
5373                        x_event_id                       => l_trans_rec.event_id,
5374                        x_return_status                  => l_status,
5375                        x_calling_fn                     => l_calling_fn
5376                        ,p_log_level_rec => p_log_level_rec);
5377 
5378       -- returning trans_rec to reuse for MRC tables
5379       px_trans_rec := l_trans_rec;
5380 
5381    end if; -- reporting_flag
5382 
5383    if g_log_level_rec.statement_level then
5384      fa_debug_pkg.add(l_calling_fn, 'do fa_retirements_pkg.update_row', '', p_log_level_rec);
5385      fa_debug_pkg.add(l_calling_fn, 'retirement_id: ', l_asset_retire_rec.Retirement_Id, p_log_level_rec);
5386    end if;
5387 
5388    -- ? just for now
5389    -- l_asset_retire_rec.detail_info := null;
5390 
5391 
5392    FA_RETIREMENTS_PKG.UPDATE_ROW (
5393               X_Rowid                     => l_asset_retire_rec.detail_info.row_id,
5394               X_Retirement_Id             => l_asset_retire_rec.Retirement_Id,
5395               X_Book_Type_Code            => l_asset_hdr_rec.Book_Type_Code,
5396               X_Asset_Id                  => l_asset_hdr_rec.Asset_Id,
5397               X_Transaction_Header_Id_In  => l_asset_retire_rec.detail_info.Transaction_Header_Id_in,
5398               X_Date_Retired              => l_asset_retire_rec.Date_Retired,
5399               X_Date_Effective            => null,
5400               X_Cost_Retired              => l_asset_retire_rec.Cost_Retired,
5401               X_Status                    => l_asset_retire_rec.status,
5402               X_Last_Update_Date          => l_trans_rec.who_info.last_update_date,
5403               X_Last_Updated_By           => l_trans_rec.who_info.last_updated_by,
5404               X_Ret_Prorate_Convention    => l_asset_retire_rec.retirement_prorate_convention,
5405               X_Transaction_Header_Id_Out => l_trans_rec.transaction_header_id,
5406               X_Units                     => l_asset_retire_rec.units_retired,
5407               X_Cost_Of_Removal           => l_asset_retire_rec.cost_of_removal,
5408               X_Nbv_Retired               => l_asset_retire_rec.detail_info.nbv_retired,
5409               X_Gain_Loss_Amount          => l_asset_retire_rec.detail_info.gain_loss_amount,
5410               X_Proceeds_Of_Sale          => l_asset_retire_rec.proceeds_of_sale,
5411               X_Gain_Loss_Type_Code       => l_asset_retire_rec.detail_info.gain_loss_type_code,
5412               X_Retirement_Type_Code      => l_asset_retire_rec.retirement_type_code,
5413               X_Itc_Recaptured            => l_asset_retire_rec.detail_info.itc_recaptured,
5414               X_Itc_Recapture_Id          => l_asset_retire_rec.detail_info.itc_recapture_id,
5415               X_Reference_Num             => l_asset_retire_rec.reference_num,
5416               X_Sold_To                   => l_asset_retire_rec.sold_to,
5417               X_Trade_In_Asset_Id         => l_asset_retire_rec.trade_in_asset_id,
5418               X_Stl_Method_Code           => l_asset_retire_rec.detail_info.stl_method_code,
5419               X_Stl_Life_In_Months        => l_asset_retire_rec.detail_info.stl_life_in_months,
5420               X_Stl_Deprn_Amount          => l_asset_retire_rec.detail_info.stl_deprn_amount,
5421               X_Last_Update_Login         => l_trans_rec.who_info.last_update_login,
5422               X_Attribute1                => l_asset_retire_rec.desc_flex.attribute1,
5423               X_Attribute2                => l_asset_retire_rec.desc_flex.attribute2,
5424               X_Attribute3                => l_asset_retire_rec.desc_flex.attribute3,
5425               X_Attribute4                => l_asset_retire_rec.desc_flex.attribute4,
5426               X_Attribute5                => l_asset_retire_rec.desc_flex.attribute5,
5427               X_Attribute6                => l_asset_retire_rec.desc_flex.attribute6,
5428               X_Attribute7                => l_asset_retire_rec.desc_flex.attribute7,
5429               X_Attribute8                => l_asset_retire_rec.desc_flex.attribute8,
5430               X_Attribute9                => l_asset_retire_rec.desc_flex.attribute9,
5431               X_Attribute10               => l_asset_retire_rec.desc_flex.attribute10,
5432               X_Attribute11               => l_asset_retire_rec.desc_flex.attribute11,
5433               X_Attribute12               => l_asset_retire_rec.desc_flex.attribute12,
5434               X_Attribute13               => l_asset_retire_rec.desc_flex.attribute13,
5435               X_Attribute14               => l_asset_retire_rec.desc_flex.attribute14,
5436               X_Attribute15               => l_asset_retire_rec.desc_flex.attribute15,
5437               X_Attribute_Category_Code   => l_asset_retire_rec.desc_flex.attribute_category_code,
5438               X_Reval_Reserve_Retired     => l_asset_retire_rec.detail_info.reval_reserve_retired,
5439               X_Unrevalued_Cost_Retired   => l_asset_retire_rec.detail_info.unrevalued_cost_retired,
5440               X_Recognize_Gain_Loss       => l_asset_retire_rec.recognize_gain_loss,
5441               X_Recapture_Reserve_Flag    => l_asset_retire_rec.recapture_reserve_flag,
5442               X_Limit_Proceeds_Flag       => l_asset_retire_rec.limit_proceeds_flag,
5443               X_Terminal_Gain_Loss        => l_asset_retire_rec.terminal_gain_loss,
5444               X_Reserve_Retired           => l_asset_retire_rec.reserve_retired,
5445               X_Eofy_Reserve              => l_asset_retire_rec.eofy_reserve,
5446               X_Reduction_Rate            => l_asset_retire_rec.reduction_rate,
5447               X_Recapture_Amount          => l_asset_retire_rec.detail_info.recapture_amount,
5448               X_mrc_sob_type_code         => p_mrc_sob_type_code,
5449               X_Calling_Fn                => l_calling_fn
5450               ,p_log_level_rec => p_log_level_rec);
5451 
5452    /*
5453     * Check to see previous retirement involved source lines or not.
5454     */
5455    OPEN c_inv_trx_id (l_asset_retire_rec.detail_info.Transaction_Header_Id_in);
5456    FETCH c_inv_trx_id into l_invoice_transaction_id;
5457    CLOSE c_inv_trx_id;
5458 
5459    /*
5460     * If previous retirement involved source lines, call
5461     * REINSTATE_SRC_LINE to reinstate source lines.
5462     * REINSTATE_SRC_LINE needs to be called only once since
5463     * Invoice API which is called from REINSTATE_SRC_LINE takes
5464     * care mrc records when it is called for primary book.
5465     */
5466    if (l_invoice_transaction_id is not null) and
5467       (p_mrc_sob_type_code = 'P') then
5468       if not REINSTATE_SRC_LINE( px_trans_rec              => l_trans_rec,
5469                                  px_asset_hdr_rec          => l_asset_hdr_rec,
5470                                  px_asset_fin_rec          => l_asset_fin_rec,
5471                                  p_asset_desc_rec          => p_asset_desc_rec,
5472                                  p_invoice_transaction_id  => l_invoice_transaction_id,
5473                                  p_inv_tbl                 => l_inv_tbl,
5474                                  p_rowid                   => l_rowid,
5475                                  p_calling_fn              => l_calling_fn
5476                                  ,p_log_level_rec => p_log_level_rec) then
5477          raise FND_API.G_EXC_UNEXPECTED_ERROR;
5478       end if;
5479 
5480    end if; -- (l_invoice_transaction_id is not null)
5481 
5482    return TRUE;
5483 
5484 EXCEPTION
5485    /*
5486     * Added for Group Asset uptake
5487     */
5488     when FND_API.G_EXC_UNEXPECTED_ERROR then
5489            -- Make sure to close curosr opened for source line retirement
5490           if c_inv_trx_id%ISOPEN then
5491              CLOSE c_inv_trx_id;
5492           end if;
5493 
5494           fa_srvr_msg.add_message(calling_fn => l_calling_fn
5495                     ,p_log_level_rec => p_log_level_rec);
5496           return FALSE;
5497    /*** End of uptake ***/
5498    when others then
5499 
5500           if g_token1 is null then
5501                fa_srvr_msg.add_message(calling_fn => l_calling_fn
5502                                       ,name       => g_msg_name
5503                                       ,p_log_level_rec => p_log_level_rec);
5504           else
5505                fa_srvr_msg.add_message(calling_fn => l_calling_fn
5506                                       ,name       => g_msg_name
5507                                       ,token1     => g_token1
5508                                       ,value1     => g_value1
5509                                       ,p_log_level_rec => p_log_level_rec);
5510           end if;
5511 
5512            -- Make sure to close curosr opened for source line retirement
5513           if c_inv_trx_id%ISOPEN then
5514              CLOSE c_inv_trx_id;
5515           end if;
5516 
5517           return FALSE;
5518 
5519 END do_sub_regular_reinstatement;
5520 ------------------------------------------------------------------------
5521 
5522 -- Table access
5523 -- 1. delete into TH
5524 -- 2. update RET
5525 PROCEDURE undo_reinstatement
5526    (p_api_version                in     NUMBER
5527    ,p_init_msg_list              in     VARCHAR2 := FND_API.G_FALSE
5528    ,p_commit                     in     VARCHAR2 := FND_API.G_FALSE
5529    ,p_validation_level           in     NUMBER   := FND_API.G_VALID_LEVEL_FULL
5530    ,p_calling_fn                 in     VARCHAR2
5531    ,x_return_status              out    NOCOPY VARCHAR2
5532    ,x_msg_count                  out    NOCOPY NUMBER
5533    ,x_msg_data                   out    NOCOPY VARCHAR2
5534 
5535    ,px_trans_rec                 in out NOCOPY FA_API_TYPES.trans_rec_type
5536    ,px_asset_hdr_rec             in out NOCOPY FA_API_TYPES.asset_hdr_rec_type
5537    ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
5538    )
5539 IS
5540 
5541    -- local asset info
5542    l_trans_rec         FA_API_TYPES.trans_rec_type;
5543    lv_trans_rec        FA_API_TYPES.trans_rec_type;
5544    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
5545    lv_asset_hdr_rec    FA_API_TYPES.asset_hdr_rec_type;
5546    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
5547    lv_asset_retire_rec FA_API_TYPES.asset_retire_rec_type;
5548    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
5549    l_asset_type_rec    FA_API_TYPES.asset_type_rec_type;
5550    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
5551    l_asset_dist_tbl    FA_API_TYPES.asset_dist_tbl_type;
5552    l_subcomp_tbl       FA_API_TYPES.subcomp_tbl_type;
5553    l_inv_tbl           FA_API_TYPES.inv_tbl_type;
5554    l_period_rec        FA_API_TYPES.period_rec_type;
5555 
5556    -- used to loop through tax books
5557    l_tax_book_tbl      FA_CACHE_PKG.fazctbk_tbl_type;
5558    l_tax_index         number;  -- index for tax loop
5559 
5560    l_reporting_flag             varchar2(1);
5561 
5562    -- used to store original sob info upon entry into api
5563    l_orig_set_of_books_id    number;
5564    l_orig_currency_context   varchar2(64);
5565 
5566    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.undo_reinstatement';
5567 
5568 BEGIN
5569 
5570    savepoint undo_reinstatement;
5571 
5572 
5573    if (not g_log_level_rec.initialized) then
5574       if (NOT fa_util_pub.get_log_level_rec (
5575                 x_log_level_rec =>  g_log_level_rec
5576       )) then
5577          raise FND_API.G_EXC_UNEXPECTED_ERROR;
5578       end if;
5579    end if;
5580 
5581    -- initialize message list if p_init_msg_list is set to TRUE.
5582    if (FND_API.to_boolean(p_init_msg_list)) then
5583         -- initialize error message stack.
5584         fa_srvr_msg.init_server_message;
5585 
5586         -- initialize debug message stack.
5587         fa_debug_pkg.initialize;
5588    end if;
5589 
5590    -- ****************************************************
5591    -- **  Assign input parameters to local rec/tbl types
5592    -- ****************************************************
5593    l_trans_rec        := px_trans_rec;
5594    l_asset_hdr_rec    := px_asset_hdr_rec;
5595    l_asset_retire_rec := px_asset_retire_rec;
5596 
5597    -- ***********************************
5598    -- **  Call the cache for book
5599    -- **  and do initial MRC validation
5600    -- ***********************************
5601 
5602    fnd_profile.get('GL_SET_OF_BKS_ID',g_orig_set_of_books_id);
5603    g_orig_currency_context :=  substrb(userenv('CLIENT_INFO'),45,10);
5604 
5605    if not FA_UTIL_PVT.get_asset_retire_rec
5606           (px_asset_retire_rec => l_asset_retire_rec,
5607            p_mrc_sob_type_code => 'P'
5608            ,p_log_level_rec => g_log_level_rec) then
5609               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5610    end if;
5611 
5612    l_asset_hdr_rec.asset_id       := l_asset_retire_rec.detail_info.asset_id;
5613    l_asset_hdr_rec.book_type_code := l_asset_retire_rec.detail_info.book_type_code;
5614 
5615    if not FA_UTIL_PVT.get_asset_type_rec
5616           (p_asset_hdr_rec      => l_asset_hdr_rec
5617           ,px_asset_type_rec    => l_asset_type_rec
5618           ,p_date_effective     => NULL
5619           ,p_log_level_rec => g_log_level_rec) then
5620               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5621    end if;
5622 
5623    if l_asset_hdr_rec.book_type_code is not null then
5624 
5625         fnd_profile.get('GL_SET_OF_BKS_ID',g_orig_set_of_books_id);
5626         g_orig_currency_context :=  substrb(userenv('CLIENT_INFO'),45,10);
5627 
5628         -- call the cache for the primary transaction book
5629         if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
5630                                   ,p_log_level_rec => g_log_level_rec) then
5631              raise FND_API.G_EXC_UNEXPECTED_ERROR;
5632         end if;
5633 
5634         l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
5635         lv_asset_hdr_rec := l_asset_hdr_rec;
5636 
5637         -- set the sob_id and currency context
5638         fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
5639         fnd_client_info.set_currency_context (to_char(l_asset_hdr_rec.set_of_books_id));
5640 
5641         -- BUG# 2376085, 2487707
5642         -- store the sob_id value for later use.
5643         fnd_profile.get ('GL_SET_OF_BKS_ID',l_orig_set_of_books_id);
5644         l_orig_currency_context :=  SUBSTRB(USERENV('CLIENT_INFO'),45,10);
5645 
5646         -- get the book type code P,R or N
5647         if not fa_cache_pkg.fazcsob
5648                (x_set_of_books_id   => l_asset_hdr_rec.set_of_books_id
5649                ,x_mrc_sob_type_code => l_reporting_flag
5650                ,p_log_level_rec => g_log_level_rec)
5651                then
5652                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
5653         end if;
5654 
5655         -- Error out if the program is submitted from the Reporting Responsibility
5656         -- No transaction permitted directly on reporting books.
5657         if l_reporting_flag = 'R' then
5658              FND_MESSAGE.set_name('GL','MRC_OSP_INVALID_BOOK_TYPE');
5659              FND_FILE.PUT_LINE(fnd_file.log,fnd_message.get);
5660              raise FND_API.G_EXC_UNEXPECTED_ERROR;
5661         end if;
5662 
5663 
5664    end if; -- book_type_code
5665 
5666    -- set the sob_id and currency context
5667    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
5668    fnd_client_info.set_currency_context (to_char(l_asset_hdr_rec.set_of_books_id));
5669 
5670    -- BUG# 2376085, 2487707
5671    -- store the sob_id value for later use.
5672    fnd_profile.get ('GL_SET_OF_BKS_ID',l_orig_set_of_books_id);
5673    l_orig_currency_context :=  SUBSTRB(USERENV('CLIENT_INFO'),45,10);
5674 
5675    -- *********************************************
5676    -- **  Do basic validation on input parameters
5677    -- *********************************************
5678 
5679    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin do_validation', '', g_log_level_rec); end if;
5680    -- validate that all user-entered input parameters are valid
5681    if not do_validation
5682           (p_validation_type   => g_undo_reinstatement
5683           ,p_trans_rec         => l_trans_rec
5684           ,p_asset_hdr_rec     => l_asset_hdr_rec
5685           ,p_asset_desc_rec    => l_asset_desc_rec
5686           ,p_asset_type_rec    => l_asset_type_rec
5687           ,p_asset_fin_rec     => l_asset_fin_rec
5688           ,p_asset_retire_rec  => l_asset_retire_rec
5689           ,p_asset_dist_tbl    => l_asset_dist_tbl
5690           ,p_subcomp_tbl       => l_subcomp_tbl
5691           ,p_inv_tbl           => l_inv_tbl
5692           ,p_period_rec        => l_period_rec
5693           ,p_calling_fn        => p_calling_fn
5694           ,p_log_level_rec => g_log_level_rec) then
5695               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5696    end if;
5697 
5698    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'end do_validation', '', g_log_level_rec); end if;
5699 
5700    if not undo_all_books_reinstatement
5701       (p_trans_rec         => l_trans_rec
5702       ,p_asset_hdr_rec     => l_asset_hdr_rec
5703       ,px_asset_retire_rec => l_asset_retire_rec
5704       ,p_log_level_rec => g_log_level_rec) then
5705           raise  FND_API.G_EXC_UNEXPECTED_ERROR;
5706    end if;
5707 
5708    -- BUG# 2376085, 2487707
5709    -- reset GL sob id to original value before moving to next book
5710    fnd_profile.put('GL_SET_OF_BKS_ID', l_orig_set_of_books_id);
5711    fnd_client_info.set_currency_context (l_orig_currency_context);
5712 
5713    /* if book is a corporate book, process cip assets and autocopy */
5714 
5715    -- start processing tax books for cip-in-tax and autocopy
5716    if (fa_cache_pkg.fazcbc_record.book_class = 'CORPORATE') then
5717 
5718       lv_trans_rec := l_trans_rec;
5719 
5720       if (l_asset_type_rec.asset_type = 'CIP'
5721           or l_asset_type_rec.asset_type = 'CAPITALIZED') then
5722 
5723          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Asset type: ', l_asset_type_rec.asset_type, g_log_level_rec); end if;
5724 
5725          if not fa_cache_pkg.fazctbk
5726                 (x_corp_book    => l_asset_hdr_rec.book_type_code
5727                 ,x_asset_type   => l_asset_type_rec.asset_type
5728                 ,x_tax_book_tbl => l_tax_book_tbl
5729                 ,p_log_level_rec => g_log_level_rec) then
5730                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
5731          end if;
5732 
5733          for l_tax_index in 1..l_tax_book_tbl.count loop
5734 
5735            if g_log_level_rec.statement_level then
5736                fa_debug_pkg.add(l_calling_fn, 'entered loop for tax books', '', g_log_level_rec);
5737                fa_debug_pkg.add(l_calling_fn, 'selected tax book: ', l_tax_book_tbl(l_tax_index), g_log_level_rec);
5738            end if;
5739 
5740            if not FA_ASSET_VAL_PVT.validate_asset_book
5741                   (p_transaction_type_code      => l_trans_rec.transaction_type_code
5742                   ,p_book_type_code             => l_tax_book_tbl(l_tax_index)
5743                   ,p_asset_id                   => l_asset_hdr_rec.asset_id
5744                   ,p_calling_fn                 => l_calling_fn) then
5745 
5746                      null; -- just to ignore the error
5747 
5748            else
5749 
5750              -- cache the book information for the tax book
5751              if not fa_cache_pkg.fazcbc(x_book => l_tax_book_tbl(l_tax_index)) then
5752                   raise FND_API.G_EXC_UNEXPECTED_ERROR;
5753              end if;
5754 
5755              lv_trans_rec.transaction_header_id   := null;
5756              lv_asset_retire_rec                  := null;
5757 
5758              -- to get the new retirement_id for the retrieved tax book
5759              select retirement_id
5760              into lv_asset_retire_rec.retirement_id
5761              from fa_retirements
5762              where asset_id=lv_asset_hdr_rec.asset_id
5763                and book_type_code=l_tax_book_tbl(l_tax_index)
5764                and status = 'REINSTATE';
5765 
5766              if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling undo_sub_retirement for tax book', '', g_log_level_rec); end if;
5767 
5768              if not undo_all_books_reinstatement
5769                     (p_trans_rec         => lv_trans_rec     -- tax
5770                     ,p_asset_hdr_rec     => lv_asset_hdr_rec -- tax
5771                     ,px_asset_retire_rec => lv_asset_retire_rec -- tax
5772                     ,p_log_level_rec => g_log_level_rec) then
5773                         raise FND_API.G_EXC_ERROR;
5774              end if;
5775 
5776              -- BUG# 2376085, 2487707
5777              -- reset GL sob id to original value before moving to next book
5778              fnd_profile.put('GL_SET_OF_BKS_ID', l_orig_set_of_books_id);
5779              fnd_client_info.set_currency_context (l_orig_currency_context);
5780 
5781            end if;
5782 
5783          end loop;
5784 
5785 
5786       end if; -- asset_type
5787 
5788    end if; -- book_class
5789 
5790 
5791    -- commit if p_commit is TRUE.
5792    if FND_API.to_boolean(p_commit) then
5793       COMMIT WORK;
5794    end if;
5795 
5796    -- Standard call to get message count and if count is 1 get message info.
5797    FND_MSG_PUB.count_and_get(p_count   => x_msg_count
5798                             ,p_data    => x_msg_data
5799                             );
5800 
5801    -- set back to original environment when the procedure is finished
5802    fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
5803    fnd_client_info.set_currency_context (g_orig_currency_context);
5804 
5805    -- return the status.
5806    x_return_status := FND_API.G_RET_STS_SUCCESS;
5807 
5808 EXCEPTION
5809 
5810    when FND_API.G_EXC_ERROR then
5811 
5812           ROLLBACK TO undo_reinstatement;
5813 
5814           x_return_status := FND_API.G_RET_STS_ERROR;
5815 
5816           if g_token1 is null then
5817                fa_srvr_msg.add_message(calling_fn => l_calling_fn
5818                                       ,name       => g_msg_name
5819                                       ,p_log_level_rec => g_log_level_rec);
5820           else
5821                fa_srvr_msg.add_message(calling_fn => l_calling_fn
5822                                       ,name       => g_msg_name
5823                                       ,token1     => g_token1
5824                                       ,value1     => g_value1
5825                                       ,p_log_level_rec => g_log_level_rec);
5826           end if;
5827 
5828           FND_MSG_PUB.count_and_get(p_count => x_msg_count
5829                                    ,p_data  => x_msg_data
5830                                    );
5831           -- set back to original environment when the procedure is finished
5832           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
5833           fnd_client_info.set_currency_context (g_orig_currency_context);
5834 
5835    when FND_API.G_EXC_UNEXPECTED_ERROR then
5836 
5837           ROLLBACK TO undo_reinstatement;
5838 
5839           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5840 
5841           fa_srvr_msg.add_message(calling_fn => l_calling_fn
5842                     ,p_log_level_rec => g_log_level_rec);
5843           FND_MSG_PUB.count_and_get(p_count => x_msg_count
5844                                    ,p_data  => x_msg_data
5845                                    );
5846           -- set back to original environment when the procedure is finished
5847           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
5848           fnd_client_info.set_currency_context (g_orig_currency_context);
5849 
5850    when others then
5851 
5852           ROLLBACK TO undo_reinstatement;
5853 
5854           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5855 
5856           fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
5857                     ,p_log_level_rec => g_log_level_rec);
5858           FND_MSG_PUB.count_and_get(p_count => x_msg_count
5859                                    ,p_data  => x_msg_data
5860                                    );
5861           -- set back to original environment when the procedure is finished
5862           fnd_profile.put('GL_SET_OF_BKS_ID', g_orig_set_of_books_id);
5863           fnd_client_info.set_currency_context (g_orig_currency_context);
5864 
5865 END undo_reinstatement;
5866 ----------------------------------------------------
5867 
5868 FUNCTION undo_all_books_reinstatement
5869         (p_trans_rec                  in     FA_API_TYPES.trans_rec_type
5870         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
5871         ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
5872         ,p_log_level_rec              IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
5873 IS
5874 
5875    -- Returns the reporting GL set_of_books_ids
5876    -- associated with the set_of_books_id of given primary book_type_code
5877    CURSOR sob_cursor(p_book_type_code in varchar2
5878                     ,p_sob_id         in number) is
5879    SELECT set_of_books_id AS sob_id
5880      FROM fa_mc_book_controls
5881     WHERE book_type_code          = p_book_type_code
5882       AND primary_set_of_books_id = p_sob_id
5883       AND enabled_flag            = 'Y';
5884 
5885    -- used for main transaction book
5886    l_book_class                 varchar2(15);
5887    l_set_of_books_id            number;
5888    l_distribution_source_book   varchar2(15);
5889    l_mc_source_flag             varchar2(1);
5890 
5891    -- local asset info
5892    l_trans_rec         FA_API_TYPES.trans_rec_type;
5893    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
5894    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
5895    l_period_rec        FA_API_TYPES.period_rec_type;
5896 
5897    l_ins_status                 boolean := FALSE;
5898 
5899    -- local conversion rate
5900    l_exchange_date              date;
5901    l_rate                       number;
5902 
5903    -- msg
5904    g_msg_name                   varchar2(30);
5905 
5906    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.undo_all_books_reinstatement';
5907 
5908 BEGIN
5909 
5910 
5911    -- ****************************************************
5912    -- **  Assign input parameters to local rec/tbl types
5913    -- ****************************************************
5914    l_trans_rec        := p_trans_rec;
5915    l_asset_hdr_rec    := p_asset_hdr_rec;
5916    l_asset_retire_rec := px_asset_retire_rec;
5917 
5918    -- *********************************
5919    -- **  Populate local record types
5920    -- *********************************
5921    -- populate rec_types that were not provided by users
5922 
5923    -- pop asset_retire_rec to get the rowid of retirement
5924    if not FA_UTIL_PVT.get_asset_retire_rec
5925           (px_asset_retire_rec => l_asset_retire_rec,
5926            p_mrc_sob_type_code => 'P'
5927            ,p_log_level_rec => p_log_level_rec) then
5928               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5929    end if;
5930 
5931    l_asset_hdr_rec.asset_id       := l_asset_retire_rec.detail_info.asset_id;
5932    l_asset_hdr_rec.book_type_code := l_asset_retire_rec.detail_info.book_type_code;
5933 
5934    -- call the cache for the primary transaction book
5935    if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
5936   ,p_log_level_rec => p_log_level_rec) then
5937          raise FND_API.G_EXC_UNEXPECTED_ERROR;
5938    end if;
5939 
5940    l_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
5941 
5942    -- set the sob_id and currency context
5943    fnd_profile.put('GL_SET_OF_BKS_ID', l_asset_hdr_rec.set_of_books_id);
5944    fnd_client_info.set_currency_context(to_char(l_asset_hdr_rec.set_of_books_id));
5945 
5946    -- pop current period_rec info
5947    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop period_rec', '', p_log_level_rec); end if;
5948    if not FA_UTIL_PVT.get_period_rec
5949           (p_book           => l_asset_hdr_rec.book_type_code
5950           ,p_effective_date => NULL
5951           ,x_period_rec     => l_period_rec
5952           ,p_log_level_rec => p_log_level_rec) then
5953               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5954    end if;
5955 
5956    -- ***************************************************
5957    -- **  Do asset/book-level validation on transaction
5958    -- ***************************************************
5959    -- begin asset/book-level validation on transaction
5960 
5961    -- nothing to do here
5962    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin asset/book-level validation on transaction', '', p_log_level_rec); end if;
5963 
5964    -- ***************************
5965    -- **  Transaction approval
5966    -- ***************************
5967 
5968    -- delete the event
5969    if not fa_xla_events_pvt.delete_transaction_event
5970            (p_ledger_id              => l_asset_hdr_rec.set_of_books_id,
5971             p_transaction_header_id  => l_asset_retire_rec.detail_info.transaction_header_id_out,
5972             p_book_type_code         => l_asset_hdr_rec.book_type_code,
5973             p_calling_fn             => 'fa_retirement_pub.undo_all_books_reinstatement'
5974             ,p_log_level_rec => p_log_level_rec) then
5975       raise FND_API.G_EXC_UNEXPECTED_ERROR;
5976    end if;
5977 
5978    -- ***************************
5979    -- **  Main
5980    -- ***************************
5981 
5982    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'calling do_sub_retirement for each book/sob', '', p_log_level_rec); end if;
5983 
5984    l_rate := 1;
5985    if not undo_sub_reinstatement
5986           (p_trans_rec         => l_trans_rec
5987           ,p_asset_hdr_rec     => l_asset_hdr_rec
5988           ,px_asset_retire_rec => l_asset_retire_rec
5989           ,p_mrc_sob_type_code => 'P'
5990           ,p_log_level_rec => p_log_level_rec) then
5991               raise FND_API.G_EXC_UNEXPECTED_ERROR;
5992    end if;
5993 
5994    if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
5995   ,p_log_level_rec => p_log_level_rec) then
5996         fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
5997                 ,p_log_level_rec => p_log_level_rec);
5998         return FALSE;
5999    end if;
6000 
6001    -- MRC LOOP
6002    -- if this is a primary book, process reporting books(sobs)
6003    if fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y' then
6004 
6005        -- loop thourgh reporting set of books
6006        for sob_rec in sob_cursor(l_asset_hdr_rec.book_type_code
6007                                 ,l_asset_hdr_rec.set_of_books_id) loop
6008 
6009          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'in sob_id loop', '', p_log_level_rec); end if;
6010 
6011          -- set the sob_id and currency context for reporting book(sob)
6012          fnd_profile.put('GL_SET_OF_BKS_ID', sob_rec.sob_id);
6013          fnd_client_info.set_currency_context(to_char(sob_rec.sob_id));
6014 
6015          l_asset_hdr_rec.set_of_books_id := sob_rec.sob_id;
6016          if not fa_cache_pkg.fazcbcs(x_book => l_asset_hdr_rec.book_type_code
6017                                     ,p_log_level_rec => p_log_level_rec) then
6018            fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
6019                       ,p_log_level_rec => p_log_level_rec);
6020            return FALSE;
6021          end if;
6022 
6023          if not undo_sub_reinstatement
6024                 (p_trans_rec         => l_trans_rec
6025                 ,p_asset_hdr_rec     => l_asset_hdr_rec
6026                 ,px_asset_retire_rec => l_asset_retire_rec
6027                 ,p_mrc_sob_type_code => 'R'
6028                 ,p_log_level_rec => p_log_level_rec) then
6029                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
6030          end if;
6031 
6032        end loop;
6033 
6034    end if;
6035 
6036    return TRUE;
6037 
6038 EXCEPTION
6039 
6040    when others then
6041 
6042           if g_token1 is null then
6043                fa_srvr_msg.add_message(calling_fn => l_calling_fn
6044                                       ,name       => g_msg_name
6045                                       ,p_log_level_rec => p_log_level_rec);
6046           else
6047                fa_srvr_msg.add_message(calling_fn => l_calling_fn
6048                                       ,name       => g_msg_name
6049                                       ,token1     => g_token1
6050                                       ,value1     => g_value1
6051                                       ,p_log_level_rec => p_log_level_rec);
6052           end if;
6053 
6054           return FALSE;
6055 
6056 END undo_all_books_reinstatement;
6057 ------------------------------------------------------------------------------
6058 -- p_trans_rec should have trx_id for the previous REINSTATEMENT transaction
6059 FUNCTION undo_sub_reinstatement
6060    (p_trans_rec                  in     FA_API_TYPES.trans_rec_type
6061    ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
6062    ,px_asset_retire_rec          in out NOCOPY FA_API_TYPES.asset_retire_rec_type
6063    ,p_mrc_sob_type_code          in     VARCHAR2
6064    ,p_log_level_rec              IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
6065 IS
6066    -- local asset info
6067    l_trans_rec         FA_API_TYPES.trans_rec_type;
6068    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
6069    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
6070 
6071    /*
6072     * Check to see previous retirement was source line retirement or not
6073     */
6074    cursor c_inv_trx_id (c_thid number)is
6075       select invoice_transaction_id
6076       from   fa_transaction_headers
6077       where  transaction_header_id = c_thid;
6078 
6079    l_invoice_transaction_id    number;  -- Local variable to store return value of c_inv_trx_id
6080 
6081    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.undo_sub_reinstatement';
6082 
6083 BEGIN
6084 
6085 
6086    -- ****************************************************
6087    -- **  Assign input parameters to local rec/tbl types
6088    -- ****************************************************
6089    l_trans_rec        := p_trans_rec;
6090    l_asset_hdr_rec    := p_asset_hdr_rec;
6091    l_asset_retire_rec := px_asset_retire_rec;
6092 
6093    -- pop local asset_retire_rec for retirement
6094    if not FA_UTIL_PVT.get_asset_retire_rec
6095           (px_asset_retire_rec => l_asset_retire_rec,
6096            p_mrc_sob_type_code => p_mrc_sob_type_code
6097            ,p_log_level_rec => p_log_level_rec) then
6098               raise FND_API.G_EXC_UNEXPECTED_ERROR;
6099    end if;
6100 
6101    l_asset_retire_rec.status := 'PROCESSED';
6102 
6103    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'set_of_books_id: ', l_asset_hdr_rec.set_of_books_id, p_log_level_rec); end if;
6104 
6105    /*
6106     * Check to see previous retirement involved source lines or not.
6107     */
6108    OPEN c_inv_trx_id (l_asset_retire_rec.detail_info.transaction_header_id_out);
6109    FETCH c_inv_trx_id into l_invoice_transaction_id;
6110    CLOSE c_inv_trx_id;
6111 
6112    if (l_invoice_transaction_id is not null) then
6113       if (p_mrc_sob_type_code = 'R') then
6114          DELETE FROM FA_MC_ASSET_INVOICES
6115          WHERE       ASSET_ID = l_asset_retire_rec.detail_info.asset_id
6116          AND         INVOICE_TRANSACTION_ID_IN = l_invoice_transaction_id
6117          AND         INVOICE_TRANSACTION_ID_OUT is null
6118          AND         SET_OF_BOOKS_ID = l_asset_hdr_rec.set_of_books_id;
6119 
6120          /*
6121           * This reactivate record with FIXED_ASSETS_COST <> 0
6122           * since 0 cost row should not be appeared on source line window.
6123           */
6124          UPDATE FA_MC_ASSET_INVOICES
6125          SET    INVOICE_TRANSACTION_ID_OUT = '',
6126                 DATE_INEFFECTIVE = ''
6127          WHERE  ASSET_ID = l_asset_retire_rec.detail_info.asset_id
6128          AND    INVOICE_TRANSACTION_ID_OUT = l_invoice_transaction_id
6129          AND    FIXED_ASSETS_COST <> 0
6130          AND    SET_OF_BOOKS_ID = l_asset_hdr_rec.set_of_books_id;
6131 
6132          /*
6133           * This set record as after retirement.
6134           * After retirement, if cost is 0, INVOICE_TRANSACTION_ID_OUT
6135           * and DATE_INEFFECTIVE is populated with the same value as
6136           * INVOICE_TRANSACTION_ID_IN and DATE_EFFECTIVE.
6137           */
6138          UPDATE FA_MC_ASSET_INVOICES
6139          SET    INVOICE_TRANSACTION_ID_OUT = INVOICE_TRANSACTION_ID_IN,
6140                 DATE_INEFFECTIVE = DATE_EFFECTIVE
6141          WHERE  ASSET_ID = l_asset_retire_rec.detail_info.asset_id
6142          AND    INVOICE_TRANSACTION_ID_OUT = l_invoice_transaction_id
6143          AND    FIXED_ASSETS_COST = 0
6144          AND    SET_OF_BOOKS_ID = l_asset_hdr_rec.set_of_books_id;
6145 
6146       else -- This is primary book
6147 
6148          DELETE FROM FA_ASSET_INVOICES
6149          WHERE       ASSET_ID = l_asset_retire_rec.detail_info.asset_id
6150          AND         INVOICE_TRANSACTION_ID_IN = l_invoice_transaction_id
6151          AND         INVOICE_TRANSACTION_ID_OUT is null;
6152 
6153          /*
6154           * This reactivate record with FIXED_ASSETS_COST <> 0
6155           * since 0 cost row should not be appeared on source line window.
6156           */
6157          UPDATE FA_ASSET_INVOICES
6158          SET    INVOICE_TRANSACTION_ID_OUT = '',
6159                 DATE_INEFFECTIVE = ''
6160          WHERE  ASSET_ID = l_asset_retire_rec.detail_info.asset_id
6161          AND    INVOICE_TRANSACTION_ID_OUT = l_invoice_transaction_id
6162          AND    FIXED_ASSETS_COST <> 0;
6163 
6164          /*
6165           * This set record as after retirement.
6166           * After retirement, if cost is 0, INVOICE_TRANSACTION_ID_OUT
6167           * and DATE_INEFFECTIVE is populated with the same value as
6168           * INVOICE_TRANSACTION_ID_IN and DATE_EFFECTIVE.
6169           */
6170          UPDATE FA_ASSET_INVOICES
6171          SET    INVOICE_TRANSACTION_ID_OUT = INVOICE_TRANSACTION_ID_IN,
6172                 DATE_INEFFECTIVE = DATE_EFFECTIVE
6173          WHERE  ASSET_ID = l_asset_retire_rec.detail_info.asset_id
6174          AND    INVOICE_TRANSACTION_ID_OUT = l_invoice_transaction_id
6175          AND    FIXED_ASSETS_COST = 0;
6176 
6177          DELETE FROM FA_INVOICE_TRANSACTIONS
6178          WHERE       INVOICE_TRANSACTION_ID = l_invoice_transaction_id;
6179 
6180       end if; -- (p_mrc_sob_type_code = 'R')
6181    end if; -- (l_invoice_transaction_id is not null)
6182 
6183    if (p_mrc_sob_type_code <> 'R') then
6184       -- ? check this parameter again
6185       -- delete transaction_headers row only for primary book
6186       FA_TRANSACTION_HEADERS_PKG.DELETE_ROW
6187                 (X_Transaction_Header_Id => l_asset_retire_rec.detail_info.transaction_header_id_out,
6188                  X_Calling_Fn => l_calling_fn
6189                  ,p_log_level_rec => p_log_level_rec);
6190 
6191    end if; -- reporting_flag
6192 
6193    FA_RETIREMENTS_PKG.UPDATE_ROW(
6194               X_Rowid                     => l_asset_retire_rec.detail_info.row_id,
6195               X_Retirement_Id             => l_asset_retire_rec.Retirement_Id,
6196               X_Book_Type_Code            => l_asset_retire_rec.detail_info.Book_Type_Code,
6197               X_Asset_Id                  => l_asset_retire_rec.detail_info.Asset_Id,
6198               X_Transaction_Header_Id_In  => l_asset_retire_rec.detail_info.Transaction_Header_Id_in,
6199               X_Date_Retired              => l_asset_retire_rec.Date_Retired,
6200               X_Date_Effective            => l_trans_rec.who_info.creation_date,
6201               X_Cost_Retired              => l_asset_retire_rec.Cost_Retired,
6202               X_Status                    => l_asset_retire_rec.status,
6203               X_Last_Update_Date          => l_trans_rec.who_info.last_update_date,
6204               X_Last_Updated_By           => l_trans_rec.who_info.last_updated_by,
6205               X_Ret_Prorate_Convention    => l_asset_retire_rec.retirement_prorate_convention,
6206               X_Transaction_Header_Id_Out => FND_API.G_MISS_NUM,--bug fix 4088953
6207               X_Units                     => l_asset_retire_rec.units_retired,
6208               X_Cost_Of_Removal           => l_asset_retire_rec.cost_of_removal,
6209               X_Nbv_Retired               => l_asset_retire_rec.detail_info.nbv_retired,
6210               X_Gain_Loss_Amount          => l_asset_retire_rec.detail_info.gain_loss_amount,
6211               X_Proceeds_Of_Sale          => l_asset_retire_rec.proceeds_of_sale,
6212               X_Gain_Loss_Type_Code       => l_asset_retire_rec.detail_info.gain_loss_type_code,
6213               X_Retirement_Type_Code      => l_asset_retire_rec.retirement_type_code,
6214               X_Itc_Recaptured            => l_asset_retire_rec.detail_info.itc_recaptured,
6215               X_Itc_Recapture_Id          => l_asset_retire_rec.detail_info.itc_recapture_id,
6216               X_Reference_Num             => l_asset_retire_rec.reference_num,
6217               X_Sold_To                   => l_asset_retire_rec.sold_to,
6218               X_Trade_In_Asset_Id         => l_asset_retire_rec.trade_in_asset_id,
6219               X_Stl_Method_Code           => l_asset_retire_rec.detail_info.stl_method_code,
6220               X_Stl_Life_In_Months        => l_asset_retire_rec.detail_info.stl_life_in_months,
6221               X_Stl_Deprn_Amount          => l_asset_retire_rec.detail_info.stl_deprn_amount,
6222               X_Last_Update_Login         => l_trans_rec.who_info.last_update_login,
6223               X_Attribute1                => l_asset_retire_rec.desc_flex.attribute1,
6224               X_Attribute2                => l_asset_retire_rec.desc_flex.attribute2,
6225               X_Attribute3                => l_asset_retire_rec.desc_flex.attribute3,
6226               X_Attribute4                => l_asset_retire_rec.desc_flex.attribute4,
6227               X_Attribute5                => l_asset_retire_rec.desc_flex.attribute5,
6228               X_Attribute6                => l_asset_retire_rec.desc_flex.attribute6,
6229               X_Attribute7                => l_asset_retire_rec.desc_flex.attribute7,
6230               X_Attribute8                => l_asset_retire_rec.desc_flex.attribute8,
6231               X_Attribute9                => l_asset_retire_rec.desc_flex.attribute9,
6232               X_Attribute10               => l_asset_retire_rec.desc_flex.attribute10,
6233               X_Attribute11               => l_asset_retire_rec.desc_flex.attribute11,
6234               X_Attribute12               => l_asset_retire_rec.desc_flex.attribute12,
6235               X_Attribute13               => l_asset_retire_rec.desc_flex.attribute13,
6236               X_Attribute14               => l_asset_retire_rec.desc_flex.attribute14,
6237               X_Attribute15               => l_asset_retire_rec.desc_flex.attribute15,
6238               X_Attribute_Category_Code   => l_asset_retire_rec.desc_flex.attribute_category_code,
6239               X_Reval_Reserve_Retired     => l_asset_retire_rec.detail_info.reval_reserve_retired,
6240               X_Unrevalued_Cost_Retired   => l_asset_retire_rec.detail_info.unrevalued_cost_retired,
6241               X_Recognize_Gain_Loss       => l_asset_retire_rec.recognize_gain_loss,
6242               X_Recapture_Reserve_Flag    => l_asset_retire_rec.recapture_reserve_flag,
6243               X_Limit_Proceeds_Flag       => l_asset_retire_rec.limit_proceeds_flag,
6244               X_Terminal_Gain_Loss        => l_asset_retire_rec.terminal_gain_loss,
6245               X_Reserve_Retired           => l_asset_retire_rec.reserve_retired,
6246               X_Eofy_Reserve              => l_asset_retire_rec.eofy_reserve,
6247               X_Reduction_Rate            => l_asset_retire_rec.reduction_rate,
6248               X_Recapture_Amount          => l_asset_retire_rec.detail_info.recapture_amount,
6249               X_mrc_sob_type_code         => p_mrc_sob_type_code,
6250               X_Calling_Fn                => l_calling_fn
6251               ,p_log_level_rec => p_log_level_rec);
6252 
6253    return TRUE;
6254 
6255 EXCEPTION
6256    /*
6257     * Added for Group Asset uptake
6258     */
6259     when FND_API.G_EXC_UNEXPECTED_ERROR then
6260           -- Make sure to close curosr opened for source line retirement
6261           if c_inv_trx_id%ISOPEN then
6262              CLOSE c_inv_trx_id;
6263           end if;
6264 
6265           fa_srvr_msg.add_message(calling_fn => l_calling_fn
6266                     ,p_log_level_rec => p_log_level_rec);
6267           return FALSE;
6268    /*** End of uptake ***/
6269     when others then
6270 
6271           -- Make sure to close curosr opened for source line retirement
6272           if c_inv_trx_id%ISOPEN then
6273              CLOSE c_inv_trx_id;
6274           end if;
6275 
6276           return FALSE;
6277 END undo_sub_reinstatement;
6278 
6279 
6280 -- This routine mainly validates input parameters and whether the trx is runnable
6281 FUNCTION do_validation
6282         (p_validation_type            in     varchar2
6283         ,p_trans_rec                  in     FA_API_TYPES.trans_rec_type
6284         ,p_asset_hdr_rec              in     FA_API_TYPES.asset_hdr_rec_type
6285         ,p_asset_desc_rec             in     FA_API_TYPES.asset_desc_rec_type
6286         ,p_asset_type_rec             in     FA_API_TYPES.asset_type_rec_type
6287         ,p_asset_fin_rec              in     FA_API_TYPES.asset_fin_rec_type
6288         ,p_asset_retire_rec           in     FA_API_TYPES.asset_retire_rec_type
6289         ,p_asset_dist_tbl             in     FA_API_TYPES.asset_dist_tbl_type
6290         ,p_subcomp_tbl                in     FA_API_TYPES.subcomp_tbl_type
6291         ,p_inv_tbl                    in     FA_API_TYPES.inv_tbl_type
6292         ,p_period_rec                 in     FA_API_TYPES.period_rec_type
6293         ,p_calling_fn                 in     varchar2
6294         ,p_log_level_rec              IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN
6295 IS
6296 
6297    -- local asset info
6298    l_validation_type   varchar2(30);
6299    l_trans_rec         FA_API_TYPES.trans_rec_type;
6300    l_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
6301    l_asset_desc_rec    FA_API_TYPES.asset_desc_rec_type;
6302    l_asset_type_rec    FA_API_TYPES.asset_type_rec_type;
6303    l_asset_fin_rec     FA_API_TYPES.asset_fin_rec_type;
6304    l_asset_retire_rec  FA_API_TYPES.asset_retire_rec_type;
6305    l_asset_dist_tbl    FA_API_TYPES.asset_dist_tbl_type;
6306    l_subcomp_tbl       FA_API_TYPES.subcomp_tbl_type;
6307    l_inv_tbl           FA_API_TYPES.inv_tbl_type;
6308    l_period_rec        FA_API_TYPES.period_rec_type;
6309 
6310    lv_asset_retire_rec FA_API_TYPES.asset_retire_rec_type;
6311 
6312    l_sum_units         number := 0;
6313    l_override_flag     varchar2(1);
6314 
6315    l_latest_ret_thid   number := -1;
6316 
6317    CURSOR c_last_grp_reclass IS
6318      select th.transaction_date_entered
6319      from   fa_transaction_headers th
6320           , fa_trx_references tr
6321      where  th.asset_id = p_asset_hdr_rec.asset_id
6322      and    th.book_type_code = p_asset_hdr_rec.book_type_code
6323      and    th.transaction_header_id = tr.member_transaction_header_id
6324      and    th.trx_reference_id = tr.trx_reference_id
6325      and    tr.member_asset_id = p_asset_hdr_rec.asset_id
6326      and    tr.book_type_code = p_asset_hdr_rec.book_type_code
6327      and    tr.transaction_type = 'GROUP CHANGE'
6328      and    nvl(th.amortization_start_date, th.transaction_date_entered) > l_asset_retire_rec.date_retired
6329      ;
6330 
6331    l_trx_date  date;  -- Store return value of cursor c_check_other_trx
6332 
6333    l_calling_fn varchar2(80) := 'FA_RETIREMENT_PUB.do_validation';
6334 
6335 BEGIN
6336 
6337 
6338    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'begin ', l_calling_fn, p_log_level_rec); end if;
6339    -- ****************************************************
6340    -- **  Assign input parameters to local rec/tbl types
6341    -- ****************************************************
6342    l_validation_type  := p_validation_type;
6343    l_trans_rec        := p_trans_rec;
6344    l_asset_hdr_rec    := p_asset_hdr_rec;
6345    l_asset_desc_rec   := p_asset_desc_rec;
6346    l_asset_type_rec   := p_asset_type_rec;
6347    l_asset_fin_rec    := p_asset_fin_rec;
6348    l_asset_retire_rec := p_asset_retire_rec;
6349    l_asset_dist_tbl   := p_asset_dist_tbl;
6350    l_subcomp_tbl      := p_subcomp_tbl;
6351    l_inv_tbl          := p_inv_tbl;
6352    l_period_rec       := p_period_rec;
6353 
6354    lv_asset_retire_rec := null;
6355 
6356    -- *********************************************
6357    -- **  Do basic validation on input parameters
6358    -- *********************************************
6359    -- do fundamental validation on input parameters
6360    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check fundamental validation on input parameters', '', p_log_level_rec); end if;
6361 
6362    -- check list for validation on input parameters:
6363    -- validation of asset_id, book_type_code - done
6364    -- validation of transaction date(retired_date)
6365    --  : within FY, future date, default if null - done
6366    -- validation of cost_retired and units_retired - done
6367    -- cross validation of cost_retired and units_retired - done
6368    -- validation of retirement_type - done
6369    -- validation of retirement_convention_code - done
6370    -- validation of trade_in_asset_id - done
6371    -- validate that asset_dist_tbl has all valid distributions and their units -done
6372    -- validation of units_retired to see if it is a whole number - done
6373    -- we do not allow retirement of CIP on TAX directly. Only through CORP book. -done
6374 
6375    -- check if asset_id and book are provided
6376    if l_validation_type=g_retirement then
6377       if l_asset_hdr_rec.asset_id is null
6378          or l_asset_hdr_rec.book_type_code is null then
6379            if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Both asset_id and book_type_code should be provided.', '', p_log_level_rec); end if;
6380            -- msg_name: FA_API_SHARED_INVALID_NOTNULL
6381            -- msg_text: Invalid value for <>. <> is a required field.
6382            g_msg_name := 'FA_API_SHARED_INVALID_NOTNULL';
6383            g_token1 := 'XMLTAG';
6384            g_value1 := 'ASSET_ID and BOOK_TYPE_CODE';
6385            raise FND_API.G_EXC_ERROR;
6386       end if;
6387    end if;
6388 
6389    -- check if asset is attached to hierarchy and see if it can
6390    -- be override to proceed with normal partial unit retirement
6391    if (nvl(fnd_profile.value('CRL-FA ENABLED'), 'N') = 'Y') then
6392        if (not fa_cua_asset_APIS.check_override_allowed(
6393                      p_attribute_name => 'DISTRIBUTION',
6394                      p_book_type_code => l_asset_hdr_rec.book_type_code,
6395                      p_asset_id => l_asset_hdr_rec.asset_id,
6396                      x_override_flag => l_override_flag)) then
6397            fa_srvr_msg.add_message(
6398                       calling_fn => 'FA_RETIREMENT_PUB.valid_input'
6399                       ,p_log_level_rec => p_log_level_rec);
6400            return FALSE;
6401        end if;
6402        -- if override flag is set to No, do not allow the transfer
6403        if (l_override_flag = 'N') then
6404            fa_srvr_msg.add_message(
6405                       calling_fn => 'FA_RETIREMENT_PUB.valid_input',
6406                       name => 'CUA_NO_DIST_CHANGE_ALLOWED'
6407                       ,p_log_level_rec => p_log_level_rec);
6408            return FALSE;
6409        end if;
6410    end if;
6411 
6412    if l_validation_type in (g_reinstatement
6413                            ,g_undo_retirement
6414                            ,g_undo_reinstatement) then
6415       if not ((l_asset_hdr_rec.asset_id is not null
6416                and l_asset_hdr_rec.book_type_code is not null
6417                )
6418               or
6419               (l_asset_retire_rec.retirement_id is not null
6420               )) then
6421            if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Either asset_id/book or retirement_id should be provided.', '', p_log_level_rec); end if;
6422            -- msg_name: FA_API_SHARED_INVALID_NOTNULL
6423            -- msg_text: Invalid value for <>. <> is a required field.
6424            g_msg_name := 'FA_API_SHARED_INVALID_NOTNULL';
6425            g_token1 := 'XMLTAG';
6426            g_value1 := 'ASSET_ID/BOOK_TYPE_CODE or RETIREMENT_ID';
6427            raise FND_API.G_EXC_ERROR;
6428       end if;
6429    end if;
6430 
6431    -- check if retirement type exists in fa_lookups if it is provided
6432    if l_validation_type=g_retirement then
6433       if l_asset_retire_rec.retirement_type_code is not null then
6434          if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if retirement type exists in fa_lookups', '', p_log_level_rec); end if;
6435          if not FA_ASSET_VAL_PVT.validate_fa_lookup_code
6436                 (p_lookup_type => 'RETIREMENT'
6437                 ,p_lookup_code => l_asset_retire_rec.retirement_type_code
6438 ,p_log_level_rec => p_log_level_rec) then
6439                     g_msg_name := null;
6440                     g_token1 := null;
6441                     if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Error: Retirement type is invalid', '', p_log_level_rec); end if;
6442                     raise FND_API.G_EXC_UNEXPECTED_ERROR;
6443          end if;
6444       end if;
6445    end if;
6446 
6447    -- check if trade_in_asset_id is valid if it is not null
6448    if l_validation_type=g_retirement then
6449       if l_asset_retire_rec.trade_in_asset_id is not NULL then
6450 
6451           if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if trade in asset number is the asset you are retiring', '', p_log_level_rec); end if;
6452           if l_asset_hdr_rec.asset_id=l_asset_retire_rec.trade_in_asset_id then
6453                g_msg_name := 'FA_RET_INVALID_TRADE_IN';
6454                g_token1 := null;
6455                raise FND_API.G_EXC_ERROR;
6456           end if;
6457 
6458           -- check if the trade-in asset exists in Oracle Assets
6459           if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if trade_in_asset_id exists in Oracle Assets', '', p_log_level_rec); end if;
6460           if not FA_ASSET_VAL_PVT.validate_asset_id_exist
6461                  (p_asset_id   => l_asset_hdr_rec.asset_id
6462                                   ,p_log_level_rec => p_log_level_rec) then
6463                      g_msg_name := 'FA_RET_TRADE_IN_NONEXISTENT';
6464                      g_token1 := null;
6465                      raise FND_API.G_EXC_ERROR;
6466           end if;
6467 
6468       end if; -- trade-in asset
6469    end if; -- g_retirement
6470 
6471    -- check if either units_retired or cost_retired is provided
6472    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check if either units_retired or cost_retired is provided', '', p_log_level_rec); end if;
6473    if l_validation_type=g_retirement then
6474       if l_asset_retire_rec.units_retired is null
6475          and l_asset_retire_rec.cost_retired is null then
6476            if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Either units_retired or cost_retired should be provided.', '', p_log_level_rec); end if;
6477            g_msg_name := 'FA_API_SHARED_INVALID_NOTNULL';
6478            g_token1 := 'XMLTAG';
6479            g_value1 := 'UNITS_RETIRED or COST_RETIRED';
6480            raise FND_API.G_EXC_ERROR;
6481       end if;
6482    end if;
6483 
6484    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check units', '', p_log_level_rec); end if;
6485    if l_validation_type=g_retirement then
6486       if l_asset_retire_rec.units_retired is not null then
6487 
6488            -- check to make sure that units_retired is positive
6489            if l_asset_retire_rec.units_retired < 0 then
6490                 g_msg_name := 'FA_SHARED_GREATER_THAN_ZERO';
6491                 g_token1 := null;
6492                 raise FND_API.G_EXC_ERROR;
6493            end if;
6494 
6495            -- get the current units of the asset from fa_asset_history table
6496            if not FA_UTIL_PVT.get_current_units
6497                   (p_calling_fn    => l_calling_fn
6498                   ,p_asset_id      => l_asset_hdr_rec.asset_id
6499                   ,x_current_units => l_asset_desc_rec.current_units
6500                   ,p_log_level_rec => p_log_level_rec) then
6501                       g_msg_name := null;
6502                       g_token1 := null;
6503                       raise FND_API.G_EXC_UNEXPECTED_ERROR;
6504            end if;
6505 
6506            -- check if units_retired exceeds current units
6507            if l_asset_retire_rec.units_retired > l_asset_desc_rec.current_units then
6508                 g_msg_name := 'FA_RET_UNITS_TOO_BIG';
6509                 g_token1 := null;
6510                 raise FND_API.G_EXC_ERROR;
6511            end if;
6512 
6513       end if;
6514    end if;
6515 
6516    -- Basically we need to populate asset_retire_rec at this point
6517    -- so that the following validation against units_retired
6518    -- ,which users will not probably provide, can be done.
6519 
6520    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'pop local asset_retire_rec', '', p_log_level_rec); end if;
6521    if l_validation_type in (g_reinstatement
6522                            ,g_undo_retirement
6523                            ,g_undo_reinstatement) then
6524       -- pop local asset_retire_rec for retirement
6525       lv_asset_retire_rec.retirement_id := l_asset_retire_rec.retirement_id;
6526       if not FA_UTIL_PVT.get_asset_retire_rec
6527              (px_asset_retire_rec => lv_asset_retire_rec,
6528               p_mrc_sob_type_code => 'P'
6529               ,p_log_level_rec => p_log_level_rec) then
6530                  g_msg_name := null;
6531                  g_token1 := null;
6532                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
6533       end if;
6534       -- At this point, lv_asset_retire_rec has the previous retirement info.
6535    end if;
6536 
6537    /* Bug #2117746:
6538       Bypass the following validation temporarily
6539       when p_calling_fn is 'fa_ciptax_api_pkg.cip_retirement'.
6540       This validation against p_calling_fn will be removed later.
6541    */
6542    if p_calling_fn <> 'fa_ciptax_api_pkg.cip_retirement' then
6543 
6544      -- check to make sure that the transaction is not for CIP on TAX book
6545      -- since we do not allow retirement of CIP directly in TAX book.
6546      -- Instead retirement of CIP on TAX book can be copied through CORP book only.
6547      if l_validation_type = g_retirement then
6548 
6549         -- call the cache for the transaction book
6550         if not fa_cache_pkg.fazcbc(x_book => l_asset_hdr_rec.book_type_code
6551   ,p_log_level_rec => p_log_level_rec) then
6552              raise FND_API.G_EXC_UNEXPECTED_ERROR;
6553         end if;
6554 
6555         if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check to make sure that the transaction is not for CIP on TAX book', '', p_log_level_rec); end if;
6556         if fa_cache_pkg.fazcbc_record.book_class = 'TAX'
6557            and l_asset_type_rec.asset_type = 'CIP' then
6558 
6559              -- we do not support this transaction
6560              if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Error: You can not perform this transaction on CIP asset.', '', p_log_level_rec); end if;
6561              g_msg_name := null;
6562              g_token1 := null;
6563              raise FND_API.G_EXC_ERROR;
6564 
6565         end if;
6566 
6567      elsif l_validation_type in (g_reinstatement
6568                            ,g_undo_retirement
6569                            ,g_undo_reinstatement) then
6570 
6571         -- call the cache for the transaction book
6572         if not fa_cache_pkg.fazcbc(x_book => lv_asset_retire_rec.detail_info.book_type_code
6573                                   ,p_log_level_rec => p_log_level_rec) then
6574              g_msg_name := null;
6575              g_token1 := null;
6576              raise FND_API.G_EXC_UNEXPECTED_ERROR;
6577         end if;
6578 
6579         if fa_cache_pkg.fazcbc_record.book_class = 'TAX'
6580            and l_asset_type_rec.asset_type = 'CIP' then
6581 
6582              -- we do not support this transaction
6583              if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Error: You can not perform this transaction on CIP asset.', '', p_log_level_rec); end if;
6584              g_msg_name := null;
6585              g_token1 := null;
6586              raise FND_API.G_EXC_ERROR;
6587 
6588         end if;
6589 
6590      end if; -- check CIP on TAX
6591 
6592    end if; -- check p_calling_fn
6593 
6594 
6595    if l_validation_type = g_reinstatement then
6596    if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'check to make sure that units_retired is valid', '', p_log_level_rec); end if;
6597 
6598       if l_asset_retire_rec.units_retired is not null then
6599 
6600            -- default units_to_reinsate to total units retired before
6601            l_asset_retire_rec.units_retired := -1 * l_asset_retire_rec.units_retired;
6602 
6603            -- check to make sure that units_retired is negative
6604            if l_asset_retire_rec.units_retired > 0 then
6605                 g_msg_name := 'FA_SHARED_LESS_THAN_ZERO'; -- ? may need to add to msg table
6606                 g_token1 := null;
6607                 raise FND_API.G_EXC_ERROR;
6608            end if;
6609 
6610            -- check to make sure that units_retired for reinstatement
6611            -- does not exceed the units_retired in retirement row.
6612            if (-1 * l_asset_retire_rec.units_retired)
6613               > lv_asset_retire_rec.units_retired then
6614                 g_msg_name := 'FA_RET_UNITS_TOO_BIG'; -- Is this correct message even for reinstatement ?
6615                 g_token1 := null;
6616                 raise FND_API.G_EXC_ERROR;
6617            end if;
6618 
6619       end if;
6620 
6621    end if;
6622 
6623    -- check if distributions have valid info if trx is PARTIAL unit retirement
6624    -- or PARTIAL unit reinstatement.
6625    -- check if asset_dist_tbl has all valid units in distributions
6626    -- check if distribution lines are out of balance
6627    -- msg name: FA_RET_UNIS_OUT_OF_BALANCE
6628    -- msg text: Distribution lines are out of balance
6629    --           Cause: The sum of the distributed units is not equal to the total units of ....
6630 
6631    if l_validation_type in (g_retirement, g_reinstatement) then
6632       if l_asset_retire_rec.units_retired is not null
6633          and l_asset_dist_tbl.count > 0 then
6634 
6635            -- check if units_retired is a whole number
6636            -- make sure that units_retired is a whole number
6637            if trunc(l_asset_retire_rec.units_retired)
6638               <> l_asset_retire_rec.units_retired then
6639 
6640                 -- g_msg_name := 'VALUE_MUST_BE_POSITIVE_INT'; -- this must be generic enough to be also good for reinstatement since units_retired for reinstatement should be negative.
6641                 g_msg_name := null;
6642                 g_token1 := null;
6643                 raise FND_API.G_EXC_ERROR;
6644            end if;
6645 
6646            -- check if distributions are valid
6647            if l_asset_dist_tbl.count >= 1 then
6648 
6649                 -- check if the sum of units in all distributions is equal
6650                 -- to the total units_retired
6651 
6652                 l_sum_units := 0;
6653 
6654                 for i in 1..l_asset_dist_tbl.count loop
6655 
6656                       l_sum_units := l_sum_units
6657                                      + l_asset_dist_tbl(i).transaction_units;
6658 
6659                       -- check if provided dist_id is valid
6660                       if l_asset_dist_tbl(i).distribution_id is not null then
6661 
6662                            if not FA_ASSET_VAL_PVT.validate_dist_id
6663                                   (p_asset_id  => l_asset_hdr_rec.asset_id
6664                                   ,p_dist_id   => l_asset_dist_tbl(i).distribution_id) then
6665                                       -- Error: Unable to get distribution information
6666                                       g_msg_name := 'FA_EXP_FETCH_DH';
6667                                       g_token1 := null;
6668                                       raise FND_API.G_EXC_ERROR;
6669                            end if;
6670 
6671                       else -- if dist is null
6672                            -- error out since dist_id can not be null
6673                            g_msg_name := 'FA_API_SHARED_INVALID_NOTNULL';
6674                            g_token1 := 'XMLTAG';
6675                            g_value1 := 'DISTRIBUTION_ID';
6676                            raise FND_API.G_EXC_ERROR;
6677                       end if;
6678 
6679                 end loop;
6680 
6681                 -- error out when the two values are not equal
6682                 if (l_sum_units*-1) <> l_asset_retire_rec.units_retired then
6683                      g_msg_name := 'FA_RET_UNIS_OUT_OF_BALANCE';
6684                      g_token1 := null;
6685                      raise FND_API.G_EXC_ERROR;
6686                 end if;
6687 
6688            else
6689                 -- error out: need at least one row in asset_dist_tbl type
6690                 --            if this is partial unit retirement
6691                 g_msg_name := 'FA_API_SHARED_INVALID_NOTNULL';
6692                 g_token1 := 'XMLTAG';
6693                 g_value1 := 'ASSET_DIST_TBL_REC';
6694                 raise FND_API.G_EXC_ERROR;
6695            end if;
6696 
6697       end if; -- units_retired
6698    end if; -- if g_ret or g_reinst
6699 
6700    -- check that the retired date of retirement is within the current
6701    -- fiscal year
6702    if l_validation_type=g_retirement
6703       and l_asset_retire_rec.date_retired is not null then
6704 
6705         -- ? this validation will be replaced properly later
6706         -- fa_date.validate('LOW_RANGE', l_asset_retire_rec.date_retired);
6707 
6708         -- check if transaction date(date_retired) is in the current fiscal year
6709         -- date must be in current fiscal year
6710         if l_asset_retire_rec.date_retired < l_period_rec.fy_start_date
6711            or l_asset_retire_rec.date_retired > l_period_rec.fy_end_date then
6712               g_msg_name := 'FA_RET_DATE_MUSTBE_IN_CUR_FY';
6713               g_token1 := null;
6714               raise FND_API.G_EXC_ERROR;
6715         end if;
6716 
6717         -- check if date_retired is a future date
6718         -- date_retired must not be in a future period
6719         if l_asset_retire_rec.date_retired
6720            > l_period_rec.calendar_period_close_date then
6721                 g_msg_name := 'FA_SHARED_CANNOT_FUTURE';
6722                 g_token1 := null;
6723                 raise FND_API.G_EXC_ERROR;
6724         end if;
6725 
6726    end if;
6727 
6728    -- check that the retired date of the retirement to reinstate is within the current
6729    -- fiscal year
6730    if l_validation_type=g_reinstatement
6731       and lv_asset_retire_rec.date_retired is not null then
6732 
6733         -- check if transaction date(date_retired) is in the current fiscal year
6734         -- date must be in current fiscal year
6735         if lv_asset_retire_rec.date_retired < l_period_rec.fy_start_date
6736            or lv_asset_retire_rec.date_retired > l_period_rec.fy_end_date then
6737               if g_log_level_rec.statement_level then
6738                  fa_debug_pkg.add(l_calling_fn, 'Error: You can not reinstate assets that were retired in previous fiscal year', '', p_log_level_rec);
6739               end if;
6740               g_msg_name := 'FA_RET_DATE_MUSTBE_IN_CUR_FY';
6741               g_token1 := null;
6742               raise FND_API.G_EXC_ERROR;
6743         end if;
6744 
6745    end if;
6746 
6747    -- check if date_retired/retirement_convention is valid
6748    if l_validation_type = g_retirement
6749       and l_asset_retire_rec.date_retired is not null then
6750 
6751         -- validate retirement_prorate_convention if it is not null
6752         if l_asset_retire_rec.retirement_prorate_convention is not null then
6753              if not fa_cache_pkg.fazccvt
6754                       (x_prorate_convention_code => l_asset_retire_rec.retirement_prorate_convention,
6755                        x_fiscal_year_name        => fa_cache_pkg.fazcbc_record.fiscal_year_name
6756                        ,p_log_level_rec => p_log_level_rec) then
6757                 g_msg_name := 'FA_RET_CANT_GET_RET_PRO_DATE';
6758                 g_token1   := null;
6759 
6760                 raise FND_API.G_EXC_ERROR;
6761              end if;
6762         end if;
6763 
6764    end if; -- date_retired/retirement_convention
6765 
6766    --
6767    -- Validating date retired for member asset
6768    -- No prior period nor overlapping retirement trx
6769    -- with other trxs are allowed.
6770    --
6771    if l_validation_type = g_retirement and
6772       l_asset_retire_rec.date_retired is not null and
6773       l_asset_fin_rec.group_asset_id is not null then
6774       l_trx_date := null;
6775       OPEN c_last_grp_reclass;
6776       FETCH c_last_grp_reclass INTO l_trx_date;
6777       CLOSE c_last_grp_reclass;
6778 
6779       if l_trx_date is not null then
6780          g_msg_name := 'FA_SHARED_OTHER_TRX_FOLLOW';
6781          raise FND_API.G_EXC_ERROR;
6782       end if;
6783 
6784       if p_period_rec.calendar_period_open_date
6785              > l_asset_retire_rec.date_retired then
6786          g_msg_name := 'FA_NO_PRIOR_RET';
6787          raise FND_API.G_EXC_ERROR;
6788       end if;
6789 
6790    end if;
6791 
6792 
6793 
6794    -- check to make sure that the transaction is doable
6795    if l_validation_type = g_retirement then
6796 
6797          -- check if the asset has already been fully retired
6798          if g_log_level_rec.statement_level then
6799             fa_debug_pkg.add(l_calling_fn, 'check if the asset is already fully retired', '', p_log_level_rec);
6800          end if;
6801          if FA_ASSET_VAL_PVT.validate_fully_retired
6802            (p_asset_id  => l_asset_hdr_rec.asset_id
6803            ,p_book      => l_asset_hdr_rec.book_type_code
6804            ,p_log_level_rec => p_log_level_rec) then
6805                g_msg_name := 'FA_REC_RETIRED';
6806                g_token1 := null;
6807                raise FND_API.G_EXC_ERROR;
6808          end if;
6809 
6810    elsif l_validation_type = g_reinstatement then
6811 
6812       if lv_asset_retire_rec.status <> 'PROCESSED' then
6813             if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Error: You can not perform this transaction.', '', p_log_level_rec); end if;
6814             g_msg_name := null;
6815             g_token1 := null;
6816             raise FND_API.G_EXC_ERROR;
6817       end if;
6818 
6819       SELECT nvl(max(transaction_header_id),0)
6820       INTO   l_latest_ret_thid
6821       FROM   fa_transaction_headers
6822       WHERE  asset_id = l_asset_hdr_rec.asset_id
6823         AND  book_type_code = l_asset_hdr_rec.book_type_code
6824         AND  transaction_key = 'R'
6825         AND  transaction_type_code||'' like '%RETIREMENT';
6826 
6827      if l_latest_ret_thid
6828         <> lv_asset_retire_rec.detail_info.transaction_header_id_in then
6829             g_msg_name := 'FA_CREATE_NOT_ALLOWED';
6830             g_token1 := null;
6831             raise FND_API.G_EXC_ERROR;
6832      end if;
6833 
6834    elsif l_validation_type = g_undo_retirement then
6835 
6836       -- ? Is PARTIAL required ?  when is it used ?
6837       if lv_asset_retire_rec.status not in ('PENDING','PARTIAL') then
6838             if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Error: You can not perform this transaction.', '', p_log_level_rec); end if;
6839             g_msg_name := null;
6840             g_token1 := null;
6841             raise FND_API.G_EXC_ERROR;
6842       end if;
6843 
6844       /*---------------------------------------------------------------+
6845       | Bug 1577955.                                                   |
6846       | We need to check if there are any partial Unit Retirements in  |
6847       | the Corporate book. Then we check if there were any cost       |
6848       | adjustments or if depreciation was run on  any of the          |
6849       | associated Tax books before running Gain/Loss in the Corporate |
6850       | book. If that is the case, we will not allow                   |
6851       | the use of the 'Undo Retirement' function.                     |
6852       | Instead, Gain/Loss must be run on the Corp book first          |
6853       | and then you may reinstate the asset.                          |
6854       +---------------------------------------------------------------+*/
6855 
6856       if FA_ASSET_VAL_PVT.validate_corp_pending_ret
6857          (p_asset_id                  => l_asset_hdr_rec.asset_id
6858          ,p_book                      => l_asset_hdr_rec.book_type_code
6859          ,p_transaction_header_id_in  => lv_asset_retire_rec.detail_info.transaction_header_id_in
6860          ,p_log_level_rec => p_log_level_rec) then
6861              g_msg_name := 'FA_RET_CORP_PENDING_RETIREMENT';
6862              g_token1 := null;
6863              raise FND_API.G_EXC_ERROR;
6864       end if;
6865 
6866    elsif l_validation_type = g_undo_reinstatement then
6867 
6868       if lv_asset_retire_rec.status not in ('REINSTATE') then
6869             if g_log_level_rec.statement_level then fa_debug_pkg.add(l_calling_fn, 'Error: You can not perform this transaction.', '', p_log_level_rec); end if;
6870              g_msg_name := null;
6871              g_token1 := null;
6872             raise FND_API.G_EXC_ERROR;
6873       end if;
6874 
6875    end if;
6876 
6877 
6878 /***** ? Need to investigate the difference between PARTIAL and PENDING in forms code
6879 
6880    elsif (:retire.status = 'PARTIAL') then
6881      fa_retirements_val.toggle_retire_button('UNDO RETIRE');
6882      set_retire_fields('OFF'
6883      ,p_log_level_rec => p_log_level_rec);
6884      --
6885    elsif (:retire.status = 'PENDING') then
6886      fa_retirements_val.toggle_retire_button('UNDO RETIRE');
6887      set_retire_fields('ON'
6888      ,p_log_level_rec => p_log_level_rec);
6889      --
6890    elsif (:retire.status = 'REINSTATE') then
6891      fa_retirements_val.toggle_retire_button('UNDO REINSTATE');
6892      set_retire_fields('OFF'
6893      ,p_log_level_rec => p_log_level_rec);
6894 
6895 ***/
6896 
6897    return TRUE;
6898 
6899 EXCEPTION
6900 
6901    when FND_API.G_EXC_ERROR then
6902 
6903           if g_token1 is null then
6904                fa_srvr_msg.add_message(calling_fn => l_calling_fn
6905                                       ,name       => g_msg_name
6906                                       ,p_log_level_rec => p_log_level_rec);
6907           else
6908                fa_srvr_msg.add_message(calling_fn => l_calling_fn
6909                                       ,name       => g_msg_name
6910                                       ,token1     => g_token1
6911                                       ,value1     => g_value1
6912                                       ,p_log_level_rec => p_log_level_rec);
6913           end if;
6914 
6915           return FALSE;
6916 
6917    when FND_API.G_EXC_UNEXPECTED_ERROR then
6918 
6919           fa_srvr_msg.add_message(calling_fn => l_calling_fn
6920                     ,p_log_level_rec => p_log_level_rec);
6921           return FALSE;
6922 
6923    when others then
6924 
6925           fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn
6926                     ,p_log_level_rec => p_log_level_rec);
6927           return FALSE;
6928 
6929 END do_validation;
6930 
6931 /*====================================================================+
6932  | Function                                                           |
6933  |   REINSTATE_SRC_LINE                                               |
6934  |                                                                    |
6935  | Description                                                        |
6936  |   This is similar to FA_CUA_REINSTATE_APIS_PKG.REINSTATE_SRC_LINE. |
6937  |   The function has been modified to handle mrc as well by calling  |
6938  |   Invoice API instead of making direct DML call.                   |
6939  |                                                                    |
6940  +====================================================================*/
6941 FUNCTION REINSTATE_SRC_LINE(
6942                   px_trans_rec             IN OUT NOCOPY FA_API_TYPES.trans_rec_type,
6943                   px_asset_hdr_rec         IN OUT NOCOPY FA_API_TYPES.asset_hdr_rec_type,
6944                   px_asset_fin_rec         IN OUT NOCOPY FA_API_TYPES.asset_fin_rec_type,
6945                   p_asset_desc_rec         IN     FA_API_TYPES.asset_desc_rec_type,
6946                   p_invoice_transaction_id IN     NUMBER,
6947                   p_inv_tbl                IN     FA_API_TYPES.inv_tbl_type,
6948                   p_rowid                  IN     ROWID,
6949                   p_calling_fn             IN     VARCHAR2,
6950                   p_log_level_rec          IN     FA_API_TYPES.log_level_rec_type default null) return BOOLEAN Is
6951 
6952   l_calling_fn             VARCHAR2(80) := 'fa_retirement_pub.reinstate_src_line';
6953 
6954   CURSOR  New_Asset_Invoices_C (c_asset_invoice_id NUMBER) is
6955   SELECT  SOURCE_LINE_ID
6956         , FIXED_ASSETS_COST
6957   FROM    FA_ASSET_INVOICES
6958   WHERE   INVOICE_TRANSACTION_ID_IN = p_invoice_transaction_id
6959   AND     ASSET_INVOICE_ID = c_asset_invoice_id;
6960 
6961   -- added a condition asset_id = c_asset_id for high-cost sql fix
6962   CURSOR  Old_Asset_Invoices_C (c_asset_id NUMBER) is
6963   SELECT  ASSET_INVOICE_ID
6964         , FIXED_ASSETS_COST
6965         , SOURCE_LINE_ID
6966   FROM    FA_ASSET_INVOICES
6967   WHERE   INVOICE_TRANSACTION_ID_OUT = p_invoice_transaction_id
6968   AND     ASSET_ID = c_asset_id;
6969 
6970   /*
6971    * For calling invoice api
6972    */
6973   l_trans_rec               FA_API_TYPES.trans_rec_type;
6974   l_asset_hdr_rec           FA_API_TYPES.asset_hdr_rec_type;
6975   l_asset_cat_rec           FA_API_TYPES.asset_cat_rec_type;
6976   l_asset_type_rec          FA_API_TYPES.asset_type_rec_type;
6977   l_new_asset_fin_rec       FA_API_TYPES.asset_fin_rec_type;
6978   l_new_asset_fin_mrc_tbl   FA_API_TYPES.asset_fin_tbl_type;
6979   l_inv_tbl                 FA_API_TYPES.inv_tbl_type;
6980   l_asset_deprn_rec_new     FA_API_TYPES.asset_deprn_rec_type;
6981   l_asset_deprn_mrc_tbl_new FA_API_TYPES.asset_deprn_tbl_type;
6982   l_inv_trans_rec           FA_API_TYPES.inv_trans_rec_type;
6983 
6984   l_ind                     BINARY_INTEGER;
6985 
6986   l_temp_src_line_id        NUMBER;
6987   l_fixed_assets_cost       NUMBER;
6988 
6989 BEGIN
6990 
6991   if (nvl(p_inv_tbl.last, 0) = 0) then
6992     l_inv_tbl.delete;
6993 
6994     OPEN Old_Asset_Invoices_C(px_asset_hdr_rec.asset_id);
6995 
6996     l_ind := 0;
6997 
6998     /*
6999      * Populate old source line fixed_assets_cost for reinstatement
7000      */
7001     LOOP
7002 
7003       l_ind := l_ind + 1;
7004       l_temp_src_line_id := to_number(null);
7005       l_fixed_assets_cost := to_number(null);
7006 
7007       FETCH Old_Asset_Invoices_C INTO l_inv_tbl(l_ind).asset_invoice_id,
7008                                       l_inv_tbl(l_ind).fixed_assets_cost,
7009                                       l_temp_src_line_id;
7010       EXIT when Old_Asset_Invoices_C%NOTFOUND;
7011 
7012       OPEN New_Asset_Invoices_C (l_inv_tbl(l_ind).asset_invoice_id);
7013       FETCH New_Asset_Invoices_C INTO l_inv_tbl(l_ind).source_line_id,
7014                                       l_fixed_assets_cost;
7015       CLOSE New_Asset_Invoices_C;
7016 
7017       /*
7018        * Invoice API now does NOT default this flag to 'NO'
7019        */
7020       if (l_inv_tbl(l_ind).source_line_id is null) then
7021         l_inv_tbl(l_ind).source_line_id := l_temp_src_line_id;
7022         l_inv_tbl(l_ind).fixed_assets_cost := 0;
7023       else
7024         l_inv_tbl(l_ind).fixed_assets_cost := l_inv_tbl(l_ind).fixed_assets_cost -
7025                                               l_fixed_assets_cost;
7026       end if;
7027 
7028     End Loop;
7029 
7030     CLOSE Old_Asset_Invoices_C;
7031 
7032   else
7033     l_inv_tbl := p_inv_tbl;
7034   end if;
7035 
7036   if (nvl(l_inv_tbl.last, 0) > 0) then
7037     -- Populate asset_cat_rec
7038     -- Populating p_asset_cat_rec which may not be necessary since
7039     -- Invoice API seems not using it  as of Feb 2002
7040     if not FA_UTIL_PVT.get_asset_cat_rec (
7041                                p_asset_hdr_rec  => px_asset_hdr_rec,
7042                                px_asset_cat_rec => l_asset_cat_rec,
7043                                p_date_effective  => NULL
7044                                ,p_log_level_rec => p_log_level_rec) then
7045       raise FND_API.G_EXC_UNEXPECTED_ERROR;
7046     end if;
7047 
7048     if not FA_UTIL_PVT.get_asset_type_rec (
7049                                p_asset_hdr_rec      => px_asset_hdr_rec,
7050                                px_asset_type_rec    => l_asset_type_rec,
7051                                p_date_effective     => NULL
7052                                ,p_log_level_rec => p_log_level_rec) then
7053       raise FND_API.G_EXC_UNEXPECTED_ERROR;
7054     end if;
7055 
7056     l_trans_rec     := px_trans_rec;
7057     l_asset_hdr_rec := px_asset_hdr_rec;
7058 
7059     l_inv_trans_rec.invoice_transaction_id := to_number(null);
7060     l_inv_trans_rec.transaction_type := 'REINSTATEMENT';
7061 
7062     if not FA_INVOICE_PVT.INVOICE_ENGINE (
7063                                px_trans_rec              => l_trans_rec,
7064                                px_asset_hdr_rec          => l_asset_hdr_rec,
7065                                p_asset_desc_rec          => p_asset_desc_rec,
7066                                p_asset_type_rec          => l_asset_type_rec,
7067                                p_asset_cat_rec           => l_asset_cat_rec,
7068                                p_asset_fin_rec_adj       => px_asset_fin_rec,
7069                                x_asset_fin_rec_new       => l_new_asset_fin_rec,
7070                                x_asset_fin_mrc_tbl_new   => l_new_asset_fin_mrc_tbl,
7071                                px_inv_trans_rec          => l_inv_trans_rec,
7072                                px_inv_tbl                => l_inv_tbl,
7073                                x_asset_deprn_rec_new     => l_asset_deprn_rec_new,
7074                                x_asset_deprn_mrc_tbl_new => l_asset_deprn_mrc_tbl_new,
7075                                p_calling_fn              => l_calling_fn
7076                                ,p_log_level_rec => p_log_level_rec) then
7077       if g_log_level_rec.statement_level then
7078         fa_debug_pkg.add(l_calling_fn, 'Error Calling FA_INVOICE_PVT.INVOICE_ENGINE', '', p_log_level_rec);
7079         fa_debug_pkg.add(l_calling_fn, 'SQLERRM: ', SQLERRM, p_log_level_rec);
7080       end if;
7081       raise FND_API.G_EXC_UNEXPECTED_ERROR;
7082     end if;
7083 
7084     FA_TRANSACTION_HEADERS_PKG.UPDATE_ROW(
7085                X_Rowid                  => p_rowid,
7086                X_Invoice_Transaction_Id => l_inv_trans_rec.invoice_transaction_id,
7087                X_Calling_Fn             => l_calling_fn
7088                ,p_log_level_rec => p_log_level_rec);
7089 
7090   end if; -- (nvl(l_inv_tbl.last, 0) > 0)
7091 
7092   return TRUE;
7093 
7094 EXCEPTION
7095   when OTHERS then
7096 
7097     if New_Asset_Invoices_C%ISOPEN then
7098       CLOSE New_Asset_Invoices_C;
7099     end if;
7100 
7101     if Old_Asset_Invoices_C%ISOPEN then
7102       CLOSE Old_Asset_Invoices_C;
7103     end if;
7104 
7105     return FALSE;
7106 
7107 END REINSTATE_SRC_LINE;
7108 ---------------------------------------
7109 END FA_RETIREMENT_PUB;