DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_RETIREMENT_PUB

Source


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