DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_GAINLOSS_PKG

Source


1 PACKAGE BODY FA_GAINLOSS_PKG as
2 /* $Header: FAGMNB.pls 120.41.12020000.2 2012/07/23 10:54:59 rmandali ship $   */
3 
4 g_log_level_rec fa_api_types.log_level_rec_type;
5 g_release       number  := fa_cache_pkg.fazarel_release;
6 g_run_mode      varchar2(20) := 'NORMAL';
7 
8 PROCEDURE Do_Calc_GainLoss (
9                 p_book_type_code     IN     VARCHAR2,
10                 p_parent_request_id  IN     NUMBER,
11                 p_total_requests     IN     NUMBER,
12                 p_request_number     IN     NUMBER,
13                 px_max_retirement_id IN OUT NOCOPY number,
14                 x_success_count         OUT NOCOPY number,
15                 x_failure_count         OUT NOCOPY number,
16                 x_return_status         OUT NOCOPY number) IS
17 
18    -- used for bulk fetching
19    l_batch_size                 number;
20    l_loop_count                 number;
21 
22    -- local variables
23    l_count                      number;
24    l_request_id                 number := -1;
25    l_user_id                    number := -1;
26    l_login                      number := -1;
27    l_book_type_code             varchar2(30);
28    l_return_status              number;
29    -- local variables
30    TYPE num_tbl  IS TABLE OF NUMBER       INDEX BY BINARY_INTEGER;
31    TYPE v15_tbl  IS TABLE OF VARCHAR2(15) INDEX BY BINARY_INTEGER; --Bug# 6937117
32 
33    l_retirement_id              num_tbl;
34    l_asset_id                   num_tbl;
35    l_asset_number               v15_tbl; --Bug# 6937117
36 
37    -- variables and structs used for api call
38    l_calling_fn                 VARCHAR2(50) := 'fa_gainloss_pkg.do_calc_gainloss';
39 
40    ret                          FA_RET_TYPES.ret_struct; -- used in fagpsa
41 
42    fail                         number;
43    -- bug# 4510549
44    -- Bug# 6937117
45    cursor c_assets is
46        SELECT   /*+ ORDERED index ( RET FA_RETIREMENTS_N3 ) index( FAB FA_BOOKS_N5 ) use_nl ( RET FAB ) */
47                  RET.RETIREMENT_ID
48                 ,RET.ASSET_ID
49                 ,FAA.ASSET_NUMBER
50        FROM     FA_RETIREMENTS          RET,
51                 FA_BOOKS                FAB,
52                 FA_METHODS              M,
53                 FA_ADDITIONS_B          FAA
54        WHERE    RET.BOOK_TYPE_CODE = p_book_type_code
55        AND      RET.STATUS in ('PENDING', 'REINSTATE', 'PARTIAL')
56        AND      M.METHOD_CODE(+) =      RET.STL_METHOD_CODE
57        AND      M.LIFE_IN_MONTHS(+) =   RET.STL_LIFE_IN_MONTHS
58        AND      FAB.RETIREMENT_ID=      RET.RETIREMENT_ID
59        AND      FAB.BOOK_TYPE_CODE=     RET.BOOK_TYPE_CODE
60        AND      FAB.ASSET_ID=           RET.ASSET_ID
61        AND      RET.RETIREMENT_ID > px_max_retirement_id
62        AND      MOD(nvl(FAB.GROUP_ASSET_ID,RET.RETIREMENT_ID), p_total_requests) = (p_request_number - 1)
63        AND      FAA.ASSET_ID = FAB.ASSET_ID
64        ORDER BY RET.RETIREMENT_ID;
65 
66    done_exc      EXCEPTION;
67    gainloss_err  EXCEPTION;
68    ret_err       EXCEPTION;
69 
70 BEGIN
71 
72    px_max_retirement_id := nvl(px_max_retirement_id, 0);
73    x_success_count := 0;
74    x_failure_count := 0;
75 
76    if p_parent_request_id = -4882887 then
77       g_run_mode := 'UPGRADE';
78    end if;
79 
80    if (not g_log_level_rec.initialized) then
81       if (NOT fa_util_pub.get_log_level_rec (
82                 x_log_level_rec =>  g_log_level_rec
83       )) then
84          raise gainloss_err;
85       end if;
86    end if;
87 
88    if g_log_level_rec.statement_level then
89             fa_debug_pkg.add
90               (fname   => l_calling_fn,
91                element => '+++ Do_Calc_GainLoss: Step 1',
92                value   => '', p_log_level_rec => g_log_level_rec);
93    end if;
94 
95    -- call the book controls cache
96    if not fa_cache_pkg.fazcbc(X_book => p_book_type_code, p_log_level_rec => g_log_level_rec) then
97       raise gainloss_err;
98    end if;
99 
100    l_batch_size := nvl(fa_cache_pkg.fa_batch_size, 200);
101 
102    if g_log_level_rec.statement_level then
103             fa_debug_pkg.add
104               (fname   => l_calling_fn,
105                element => '+++ Do_Calc_GainLoss: Step 2',
106                value   => '', p_log_level_rec => g_log_level_rec);
107    end if;
108 
109    if (TRUE) then
110 
111       OPEN c_assets;
112 
113       FETCH c_assets BULK COLLECT INTO
114             l_retirement_id,
115             l_asset_id,
116             l_asset_number  -- Bug# 6937117
117             LIMIT l_batch_size;
118 
119       CLOSE c_assets;
120 
121       if l_retirement_id.count = 0 then
122          raise done_exc;
123       end if;
124 
125       for l_loop_count in 1..l_retirement_id.count loop
126 
127          -- clear the debug stack for each asset
128          FA_DEBUG_PKG.Initialize;
129          -- reset the message level to prevent bogus errors
130          FA_SRVR_MSG.Set_Message_Level(message_level => 10, p_log_level_rec => g_log_level_rec);
131 
132          BEGIN
133             FA_GAINLOSS_PKG.Do_Calc_GainLoss_Asset
134               (p_retirement_id => l_retirement_id(l_loop_count),
135                x_return_status => l_return_status,
136                p_log_level_rec => g_log_level_rec
137               );
138 
139             if (l_return_status <> 0) then
140                raise gainloss_err;
141             end if;
142 
143             x_success_count := x_success_count + 1;
144 
145            --fix for bug no.3883501.success count is displayed incorrectly after 3087644
146            --x_success_count := x_success_count + 1;
147 
148            --bug 3087644 fix starts
149             fa_srvr_msg.add_message(
150                 calling_fn => NULL,
151                   name       =>'FA_RET_STATUS_SUCCEED',
152                   token1     => 'RETID',
153                   value1     => l_retirement_id(l_loop_count),
154                   token2     => 'ASSET',
155                   value2     => l_asset_number(l_loop_count),
156                    p_log_level_rec => g_log_level_rec);
157 
158             fa_srvr_msg.add_message(
159                 calling_fn => NULL,
160                   name       => 'FA_ASSET_ID',
161                   token1     => 'ASSET_ID',
162                   value1     => l_asset_id(l_loop_count),
163                    p_log_level_rec => g_log_level_rec);
164 --bug 3087644 fix ends.
165             if (g_log_level_rec.statement_level) then
166                   fa_debug_pkg.dump_debug_messages(max_mesgs => 0, p_log_level_rec => g_log_level_rec);
167             end if;
168 
169          EXCEPTION
170             when gainloss_err then
171                FND_CONCURRENT.AF_ROLLBACK;
172                if (g_log_level_rec.statement_level) then
173                   fa_debug_pkg.dump_debug_messages(max_mesgs => 0, p_log_level_rec => g_log_level_rec);
174                end if;
175                x_failure_count := x_failure_count + 1;
176             --for bug no.3883501.modified the message name to FA_RET_STATUS_FAIL
177             fa_srvr_msg.add_message(
178                 calling_fn => NULL,
179                   name       =>'FA_RET_STATUS_FAIL',
180                   token1     => 'RETID',
181                   value1     => l_retirement_id(l_loop_count),
182                   token2     => 'ASSET',
183                   value2     => l_asset_number(l_loop_count),
184                    p_log_level_rec => g_log_level_rec); --Bug# 6937117
185 
186             fa_srvr_msg.add_message(
187                 calling_fn => NULL,
188                   name       => 'FA_ASSET_ID',
189                   token1     => 'ASSET_ID',
190                   value1     => l_asset_id(l_loop_count),
191                    p_log_level_rec => g_log_level_rec);
192 
193             when others then
194                FND_CONCURRENT.AF_ROLLBACK;
195                if (g_log_level_rec.statement_level) then
196                   fa_debug_pkg.dump_debug_messages(max_mesgs => 0, p_log_level_rec => g_log_level_rec);
197                end if;
198                x_failure_count := x_failure_count + 1;
199             --for bug no. 3883501.modifying the value2 to display the asset number
200                fa_srvr_msg.add_message(
201                   calling_fn => l_calling_fn,
202                   name       => 'FA_RET_STATUS_FAIL',
203                   token1     => 'RETID',
204                   value1     => l_retirement_id(l_loop_count),
205                   token2     => 'ASSET',
206                   value2     => l_asset_number(l_loop_count),
207                    p_log_level_rec => g_log_level_rec); -- Bug# 6937117
208 
209          END;
210 
211          -- FND_CONCURRENT.AF_COMMIT each record
212          FND_CONCURRENT.AF_COMMIT;
213 
214       end loop;  -- main bulk fetch loop
215 
216       px_max_retirement_id := l_retirement_id(l_retirement_id.count);
217 
218    end if;
219 
220    x_return_status :=  0;
221 
222 EXCEPTION
223    when done_exc then
224       if (g_log_level_rec.statement_level) then
225          fa_debug_pkg.dump_debug_messages(max_mesgs => 0, p_log_level_rec => g_log_level_rec);
226       end if;
227 
228       x_return_status :=  0;
229 
230    when gainloss_err then
231       FND_CONCURRENT.AF_ROLLBACK;
232       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => g_log_level_rec);
233       if (g_log_level_rec.statement_level) then
234          FA_DEBUG_PKG.dump_debug_messages(max_mesgs => 0, p_log_level_rec => g_log_level_rec);
235       end if;
236       x_return_status :=  2;
237 
238    when others then
239       FND_CONCURRENT.AF_ROLLBACK;
240       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => g_log_level_rec);
241       if (g_log_level_rec.statement_level) then
242          fa_debug_pkg.dump_debug_messages(max_mesgs => 0, p_log_level_rec => g_log_level_rec);
243       end if;
244 
245       x_return_status :=  2;
246 
247 END Do_Calc_GainLoss;
248 
249 
250 PROCEDURE Do_Calc_GainLoss_Asset(
251              p_retirement_id      in         NUMBER,
252              x_return_status      out NOCOPY NUMBER,
253              p_log_level_rec       IN     fa_api_types.log_level_rec_type default null) IS
254 
255    l_count                 number := 0;
256    l_user_id               number;
257    l_sysdate               date;
258 
259    -- Local record types
260    l_sob_tbl               FA_CACHE_PKG.fazcrsob_sob_tbl_type;
261    l_asset_hdr_rec         FA_API_TYPES.asset_hdr_rec_type;
262    l_trans_rec             FA_API_TYPES.trans_rec_type;
263    l_asset_desc_rec        FA_API_TYPES.asset_desc_rec_type;
264    l_asset_type_rec        FA_API_TYPES.asset_type_rec_type;
265    l_asset_retire_rec      FA_API_TYPES.asset_retire_rec_type;
266    lv_asset_retire_rec     FA_API_TYPES.asset_retire_rec_type;
267    l_period_rec            FA_API_TYPES.period_rec_type;
268 
269    l_trx_type_code         varchar2(30);
270 
271    -- added for R12
272    l_primary_sob_id        NUMBER;
273    l_thid                  NUMBER;
274    l_trx_date_entered      DATE;
275    l_event_type_code       VARCHAR2(30);
276    l_asset_type            VARCHAR2(15);
277    l_event_id              NUMBER;
278 
279    --- Variable for retirement struct
280    ret                     FA_RET_TYPES.ret_struct;
281    --Secondary changes
282    l_secondary_trans_rec         FA_API_TYPES.trans_rec_type;
283    l_secondary_asset_hdr_rec     FA_API_TYPES.asset_hdr_rec_type;
284    l_secondary_sob_id            number;
285 
286    gainloss_err            EXCEPTION;
287 
288    l_calling_fn            varchar2(50) := 'FA_GAINLOSS_PKG.do_calc_gainloss_asset';
289    l_temp_calling_fn       varchar2(50);
290 
291 BEGIN
292 
293    savepoint Do_Calc_GainLoss_Asset;
294 
295    l_asset_retire_rec.retirement_id := p_retirement_id;
296 
297    l_user_id := FND_GLOBAL.USER_ID;
298 
299    l_sysdate := SYSDATE;
300 
301    if p_log_level_rec.statement_level then
302             fa_debug_pkg.add
303               (fname   => l_calling_fn,
304                element => '+++ Step 1 +++',
305                value   => '', p_log_level_rec => p_log_level_rec);
306    end if;
307 
308    -- pop asset_retire_rec to get the rowid of retirement
309    if not FA_UTIL_PVT.get_asset_retire_rec
310           (px_asset_retire_rec => l_asset_retire_rec,
311            p_mrc_sob_type_code => 'P',
312            p_set_of_books_id => null
313           , p_log_level_rec => p_log_level_rec) then
314               raise gainloss_err;
315    end if;
316 
317    l_asset_hdr_rec.asset_id       := l_asset_retire_rec.detail_info.asset_id;
318    l_asset_hdr_rec.book_type_code := l_asset_retire_rec.detail_info.book_type_code;
319 
320    if p_log_level_rec.statement_level then
321             fa_debug_pkg.add
322               (fname   => l_calling_fn,
323                element => '+++ Step 2 +++',
324                value   => '', p_log_level_rec => p_log_level_rec);
325    end if;
326 
327    if p_log_level_rec.statement_level then
328             fa_debug_pkg.add
329               (fname   => l_calling_fn,
330                element => 'l_asset_hdr_rec.asset_id',
331                value   => l_asset_hdr_rec.asset_id, p_log_level_rec => p_log_level_rec);
332    end if;
333 
334    if p_log_level_rec.statement_level then
335             fa_debug_pkg.add
336               (fname   => l_calling_fn,
337                element => 'l_asset_hdr_rec.book_type_code',
338                value   => l_asset_hdr_rec.book_type_code, p_log_level_rec => p_log_level_rec);
339    end if;
340 
341    -- call the book controls cache
342    if not fa_cache_pkg.fazcbc(X_book => l_asset_hdr_rec.book_type_code, p_log_level_rec => p_log_level_rec) then
343       raise gainloss_err;
344    end if;
345 
346    -- CHECK: Would asset_number be really necessary for processing fagpsa ?
347    -- pop asset_desc_rec to get asset_number
348    if not FA_UTIL_PVT.get_asset_desc_rec
349           (p_asset_hdr_rec      => l_asset_hdr_rec
350           ,px_asset_desc_rec    => l_asset_desc_rec
351           , p_log_level_rec => p_log_level_rec) then
352               raise gainloss_err;
353    end if;
354 
355    -- pop current period_rec info
356    if not FA_UTIL_PVT.get_period_rec
357           (p_book           => l_asset_hdr_rec.book_type_code
358           ,p_effective_date => NULL
359           ,x_period_rec     => l_period_rec
360           , p_log_level_rec => p_log_level_rec) then
361               raise gainloss_err;
362    end if;
363 
364    if p_log_level_rec.statement_level then
365             fa_debug_pkg.add
366               (fname   => l_calling_fn,
367                element => 'current_period: l_period_rec.period_name',
368                value   => l_period_rec.period_name, p_log_level_rec => p_log_level_rec);
369    end if;
370 
371    /***********
372    * Validation
373    ************/
374    /*
375    Fix for Bug 1346402. Since trx approval allows gain loss to
376    be run although deprn_status is E, need to check if asset
377    has already depreciated in the current period. If so
378    do not process retirement or reinstatement - snarayan
379    */
380    l_count := 0;
381    begin
382      select  count(*)
383      into    l_count
384      from    fa_deprn_summary
385      where   book_type_code = l_asset_hdr_rec.book_type_code
386        and   asset_id = l_asset_hdr_rec.asset_id
387        and   period_counter = l_period_rec.period_counter;
388 
389    exception
390      /* continue if no current period DS rows */
391      when others then
392         null;
393    end;
394 
395    if (l_count <> 0) then
396          fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
397    end if;
398 
399    if p_log_level_rec.statement_level then
400             fa_debug_pkg.add
401               (fname   => l_calling_fn,
402                element => 'Passed basic validation',
403                value   => '', p_log_level_rec => p_log_level_rec);
404    end if;
405 
406 
407    /***********************
408    * Process Primary Book
409    ************************/
410    if l_asset_retire_rec.status in ('PENDING', 'REINSTATE') then
411 
412       ret.mrc_sob_type_code := 'P'; -- Primary
413       ret.status := l_asset_retire_rec.status;
414       ret.retirement_id := l_asset_retire_rec.retirement_id;
415       ret.asset_id := l_asset_retire_rec.detail_info.asset_id;
416       ret.book := l_asset_retire_rec.detail_info.book_type_code;
417 
418       /*
419         New Fix for Bug 2937365, 3033462:
420           The original solution provided through the fix on Bug 2937365
421           caused a problem described in Bug 3033462.
422           For full retirement transactions,
423           instead of nulling out units_retired column of
424           fa_retirements table directly as a solution,
425           we will null out ret.units_retired variable
426           to make sure that retirement routines process full retirement transactions
427           correctly.
428           (Null value of ret.units_retired is considered full retirement inside the code)
429       */
430       begin
431 
432         select  transaction_type_code
433         into    l_trx_type_code
434         from    fa_transaction_headers
435         where   transaction_header_id=
436           (select transaction_header_id_in
437            from fa_retirements
438            where retirement_id=p_retirement_id);
439 
440       exception
441        when others then
442           null;
443       end;
444 
445       if (l_asset_retire_rec.status = 'PENDING' and l_trx_type_code='FULL RETIREMENT') then
446         ret.units_retired := null;
447       else
448         ret.units_retired := l_asset_retire_rec.units_retired;
449       end if;
450 
451       ret.stl_life := l_asset_retire_rec.detail_info.stl_life_in_months;
452       ret.itc_recapid := nvl(l_asset_retire_rec.detail_info.itc_recapture_id,0);
453       ret.asset_number := l_asset_desc_rec.asset_number;
454       ret.date_retired := l_asset_retire_rec.date_retired;
455       ret.cost_retired := l_asset_retire_rec.cost_retired;
456       ret.proceeds_of_sale := l_asset_retire_rec.proceeds_of_sale;
457       ret.cost_of_removal := l_asset_retire_rec.cost_of_removal;
458       ret.retirement_type_code := l_asset_retire_rec.retirement_type_code;
459 
460       ret.th_id_in := l_asset_retire_rec.detail_info.transaction_header_id_in;
461       ret.stl_method_code := l_asset_retire_rec.detail_info.stl_method_code;
462 
463       -- ++++++ Added for Group Asset +++++
464       ret.recognize_gain_loss    := l_asset_retire_rec.recognize_gain_loss;
465       ret.recapture_reserve_flag := l_asset_retire_rec.recapture_reserve_flag;
466       ret.limit_proceeds_flag    := l_asset_retire_rec.limit_proceeds_flag;
467       ret.terminal_gain_loss     := l_asset_retire_rec.terminal_gain_loss;
468       ret.reduction_rate         := l_asset_retire_rec.reduction_rate;
469       ret.eofy_reserve           := l_asset_retire_rec.eofy_reserve;
470       ret.recapture_amount       := l_asset_retire_rec.detail_info.recapture_amount;
471       ret.reserve_retired        := l_asset_retire_rec.reserve_retired;
472 
473       -- UPGRADE: refer to bug#2363878
474       -- Replace the following sql with a new component of retire struct
475       -- ret.date_effective := l_asset_retire_rec.detail_info.date_effective;
476       select date_effective
477         into ret.date_effective
478       from fa_retirements
479       where retirement_id = l_asset_retire_rec.retirement_id;
480 
481       ret.prorate_convention := l_asset_retire_rec.retirement_prorate_convention;
482 
483       -- from primary book
484       if fa_cache_pkg.fazcbc_record.deprn_allocation_code = 'E' then
485            ret.dpr_evenly := 1;
486       else
487            ret.dpr_evenly := 0;
488       end if;
489 
490       if p_log_level_rec.statement_level then
491             fa_debug_pkg.add
492               (fname   => l_calling_fn,
493                element => 'l_asset_retire_rec.cost_retired',
494                value   => l_asset_retire_rec.cost_retired, p_log_level_rec => p_log_level_rec);
495       end if;
496 
497       ret.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
498 
499       -- Call fagpsa to process a retirement
500       if not FA_GAINLOSS_PRO_PKG.fagpsa
501              (ret,
502               l_sysdate,
503               l_period_rec.period_name,
504               l_period_rec.period_counter,
505               l_user_id, p_log_level_rec => p_log_level_rec) then
506                            raise gainloss_err;
507       else
508          /*
509           * Code hook for IAC
510           * Call IAC hook if IAC is enabled.
511           */
512          if (FA_IGI_EXT_PKG.IAC_Enabled  and
513              G_release = 11)then
514 
515             if not FA_IGI_EXT_PKG.Do_Gain_Loss(
516                          p_retirement_id    => ret.retirement_id,
517                          p_asset_id         => ret.asset_id,
518                          p_book_type_code   => ret.book,
519                          p_event_id         => l_trans_rec.event_id,
520                          p_calling_function => l_calling_fn) then
521 
522                    fa_srvr_msg.add_message(
523                       calling_fn => 'l_calling_fn'||'(Calling IAC)',
524                       name       => 'FA_RET_STATUS_FAIL',
525                       token1     => 'RETID',
526                       value1     => ret.retirement_id,
527                       token2     => 'ASSET',
528                       value2     => ret.asset_number ,
529                    p_log_level_rec => p_log_level_rec);
530                    raise gainloss_err;
531             end if;
532 
533          end if; -- IAC hook
534 
535          if fa_cse_callouts_pvt.is_oat_enabled then
536             if l_asset_retire_rec.status = 'PENDING' then
537                if not fa_cse_callouts_pvt.retire(
538                     p_asset_id         => ret.asset_id,
539                     p_book_type_code   => ret.book,
540                     p_retirement_id    => ret.retirement_id,
541                     p_retirement_date  => l_asset_retire_rec.date_retired,
542                     p_retirement_units => l_asset_retire_rec.units_retired) then
543                   raise gainloss_err;
544                end if;
545             elsif l_asset_retire_rec.status = 'REINSTATE' then
546                if not fa_cse_callouts_pvt.reinstate(
547                     p_asset_id         => ret.asset_id,
548                     p_book_type_code   => ret.book,
549                     p_retirement_id    => ret.retirement_id,
550                     p_reinstatement_date  => l_asset_retire_rec.date_retired,
551                     p_reinstatement_units => l_asset_retire_rec.units_retired) then
552                  raise gainloss_err;
553                end if;
554             end if;
555          end if;
556 
557       end if; -- fagpsa
558 
559       -- code fix for bug no.3641602.Call the book controls cache again
560       if not fa_cache_pkg.fazcbc(X_book => l_asset_hdr_rec.book_type_code, p_log_level_rec => p_log_level_rec) then
561          raise gainloss_err;
562       end if;
563 
564       -- SLA: store the ledger id for future use
565       l_primary_sob_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
566       l_asset_hdr_rec.set_of_books_id := l_primary_sob_id;--Bug 10252159
567 
568       /*******************************
569       * MRC LOOP for Reporting books
570       ********************************/
571       -- if this is a primary book, process reporting books(sobs)
572       if fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y' then
573 
574           -- call the sob cache to get the table of sob_ids
575           if not FA_CACHE_PKG.fazcrsob
576                  (x_book_type_code => l_asset_hdr_rec.book_type_code,
577                   x_sob_tbl        => l_sob_tbl, p_log_level_rec => p_log_level_rec) then
578              raise gainloss_err;
579           end if;
580 
581           -- loop through each book starting with the primary and
582           -- call sub routine for each
583           for l_sob_index in 1..l_sob_tbl.count loop
584 
585             if p_log_level_rec.statement_level then
586                  fa_debug_pkg.add
587                  (fname   => l_calling_fn,
588                   element => '+++ Step 2: in Reporting book loop: Set_of_books_id',
589                   value   => l_sob_tbl(l_sob_index));
590             end if;
591 
592             if not fa_cache_pkg.fazcbcs(x_book => l_asset_hdr_rec.book_type_code,
593                                         x_set_of_books_id => l_asset_hdr_rec.set_of_books_id,
594                                         p_log_level_rec => p_log_level_rec) then
595                      raise gainloss_err;
596             end if;
597 
598             lv_asset_retire_rec.retirement_id := p_retirement_id;
599 
600             -- pop asset_retire_rec to get the rowid of retirement
601             if not FA_UTIL_PVT.get_asset_retire_rec
602                   (px_asset_retire_rec => lv_asset_retire_rec,
603                    p_mrc_sob_type_code => 'R',
604                    p_set_of_books_id => l_sob_tbl(l_sob_index)
605                    , p_log_level_rec => p_log_level_rec) then
606                        raise gainloss_err;
607             end if;
608 
609             -- set up the local asset_header and sob_id
610             -- lv_asset_hdr_rec    := l_asset_hdr_rec;
611             -- lv_asset_hdr_rec.set_of_books_id := l_sob_tbl(l_sob_index);
612 
613             if lv_asset_retire_rec.status in ('PENDING', 'REINSTATE') then
614 
615                ret.mrc_sob_type_code := 'R'; -- Reporting
616                ret.set_of_books_id := l_sob_tbl(l_sob_index);
617                ret.status := lv_asset_retire_rec.status;
618                ret.retirement_id := lv_asset_retire_rec.retirement_id;
619                ret.asset_id := lv_asset_retire_rec.detail_info.asset_id;
620                ret.book := lv_asset_retire_rec.detail_info.book_type_code;
621                --fix for bug# 5086360
622                if (lv_asset_retire_rec.status = 'PENDING' and l_trx_type_code='FULL RETIREMENT') then
623                    ret.units_retired := null;
624                else
625                    ret.units_retired := lv_asset_retire_rec.units_retired;
626                end if;
627                --ret.units_retired := lv_asset_retire_rec.units_retired; --bug# 5086360
628                ret.stl_life := lv_asset_retire_rec.detail_info.stl_life_in_months;
629                ret.itc_recapid := nvl(lv_asset_retire_rec.detail_info.itc_recapture_id,0);
630                -- asset_number should be the same as that for the primary book
631                ret.asset_number := l_asset_desc_rec.asset_number;
632                ret.date_retired := lv_asset_retire_rec.date_retired;
633                ret.cost_retired := lv_asset_retire_rec.cost_retired;
634                ret.reserve_retired := lv_asset_retire_rec.reserve_retired; --Bug 9103418
635                ret.proceeds_of_sale := lv_asset_retire_rec.proceeds_of_sale;
636                ret.cost_of_removal := lv_asset_retire_rec.cost_of_removal;
637                ret.retirement_type_code := lv_asset_retire_rec.retirement_type_code;
638 
639                ret.th_id_in := lv_asset_retire_rec.detail_info.transaction_header_id_in;
640                ret.stl_method_code := lv_asset_retire_rec.detail_info.stl_method_code;
641 
642                -- UPGRADE: refer to bug#2363878
643                -- Replace the following sql with a new component of retire struct
644                -- ret.date_effective := lv_asset_retire_rec.detail_info.date_effective;
645                select date_effective
646                  into ret.date_effective
647                from fa_mc_retirements
648                where retirement_id = lv_asset_retire_rec.retirement_id
649                  and  set_of_books_id = l_sob_tbl(l_sob_index);
650 
651                ret.prorate_convention := lv_asset_retire_rec.retirement_prorate_convention;
652                -- from reporting book
653                if fa_cache_pkg.fazcbcs_record.deprn_allocation_code = 'E' then
654                     ret.dpr_evenly := 1;
655                else
656                     ret.dpr_evenly := 0;
657                end if;
658 
659                if p_log_level_rec.statement_level then
660                  fa_debug_pkg.add
661                  (fname   => l_calling_fn,
662                   element => 'lv_asset_retire_rec.cost_retired',
663                   value   => lv_asset_retire_rec.cost_retired, p_log_level_rec => p_log_level_rec);
664                end if;
665 
666 
667                -- Call fagpsa to process a retirement
668                if not FA_GAINLOSS_PRO_PKG.fagpsa
669                         (ret,
670                          l_sysdate,
671                          l_period_rec.period_name,
672                          l_period_rec.period_counter,
673                          l_user_id, p_log_level_rec => p_log_level_rec) then
674                                       raise gainloss_err;
675                end if;
676 
677 
678             end if; -- if trx in reporting is either retirement or reinstatement
679 
680           end loop; -- loop through reporting books
681 
682          --for bug no.3831503
683          if not fa_cache_pkg.fazcbc(X_book => l_asset_hdr_rec.book_type_code, p_log_level_rec => p_log_level_rec) then
684            raise gainloss_err;
685          end if;
686       end if; -- if this is a primary book
687 
688       if (g_run_mode <> 'UPGRADE' and
689           G_release <> 11)  then
690 
691          if p_log_level_rec.statement_level then
692             fa_debug_pkg.add
693                (fname   => l_calling_fn,
694                 element => 'getting asset type for asset ',
695                 value   => l_asset_hdr_rec.asset_id,
696                 p_log_level_rec => p_log_level_rec);
697          end if;
698 
699          select asset_type
700            into l_asset_type
701            from fa_additions_b
702           where asset_id = l_asset_hdr_rec.asset_id;
703 
704          if p_log_level_rec.statement_level then
705             fa_debug_pkg.add
706                (fname   => l_calling_fn,
707                 element => 'setting up parameters for event update, ret_status',
708                 value   => l_asset_retire_rec.status,
709                 p_log_level_rec => p_log_level_rec);
710          end if;
711 
712          if (l_asset_retire_rec.status = 'PENDING') then
713             l_thid            := l_asset_retire_rec.detail_info.transaction_header_id_in;
714             l_event_type_code := 'RETIREMENTS';
715          else
716             select transaction_header_id_out
717               into l_thid
718               from fa_retirements
719              where retirement_id = l_asset_retire_rec.retirement_id;
720 
721             l_event_type_code := 'REINSTATEMENTS';
722          end if;
723 
724          if (l_asset_type = 'CIP') then
725             l_event_type_code := 'CIP_' || l_event_type_code;
726          end if;
727 
728          select event_id,
729                 transaction_date_entered
730            into l_event_id,
731                 L_trx_date_entered
732            from fa_transaction_headers
733           where transaction_header_id = l_thid;
734 
735 
736          if (l_event_id is null) then
737 
738             l_asset_type_rec.asset_type          := l_asset_type;
739             l_trans_rec.transaction_header_id    := l_thid;
740             l_trans_rec.transaction_date_entered := l_trx_date_entered;
741 
742             if (l_asset_retire_rec.status = 'PENDING') then
743                l_temp_calling_fn := 'FA_RETIREMENT_PUB.do_all_books_retirement';
744             else
745                l_temp_calling_fn := 'FA_RETIREMENT_PUB.do_sub_regular_reinstatement';
746             end if;
747 
748             if not fa_xla_events_pvt.create_transaction_event
749                (p_asset_hdr_rec => l_asset_hdr_rec,
750                 p_asset_type_rec=> l_asset_type_rec,
751                 px_trans_rec    => l_trans_rec,
752                 p_event_status  => FA_XLA_EVENTS_PVT.C_EVENT_UNPROCESSED,
753                 p_calling_fn    => l_temp_calling_fn
754                 ,p_log_level_rec => p_log_level_rec) then
755                 raise gainloss_err;
756             end if;
757             /*=================================================================
758             Secondary Changes Start
759             If primary and secondary sob_id is different then we need to
760             create event for secondary ledger*/
761             l_secondary_sob_id := FA_XLA_EVENTS_PVT.get_secondary_sob_id(l_asset_hdr_rec.book_type_code);
762 
763             if p_log_level_rec.statement_level then fa_debug_pkg.add (fname   => l_calling_fn,element => 'before secondary create l_secondary_sob_id ',value   => l_secondary_sob_id); end if;
764             if p_log_level_rec.statement_level then fa_debug_pkg.add (fname   => l_calling_fn,element => 'before secondary create l_primary_sob_id ',value   => l_primary_sob_id); end if;
765             if p_log_level_rec.statement_level then fa_debug_pkg.add (fname   => l_calling_fn,element => 'before secondary create fa_cache_pkg.fazcbcs_record.SET_OF_BOOKS_ID ',value   => fa_cache_pkg.fazcbcs_record.SET_OF_BOOKS_ID); end if;
766             if (l_secondary_sob_id is not null) then
767                 l_secondary_asset_hdr_rec := l_asset_hdr_rec;
768             l_secondary_asset_hdr_rec.set_of_books_id :=  l_secondary_sob_id;
769             l_secondary_trans_rec                         := l_trans_rec;
770                 if not fa_xla_events_pvt.create_transaction_event
771                    (p_asset_hdr_rec => l_secondary_asset_hdr_rec,
772                     p_asset_type_rec=> l_asset_type_rec,
773                     px_trans_rec    => l_secondary_trans_rec,
774                     p_event_status  => FA_XLA_EVENTS_PVT.C_EVENT_UNPROCESSED,
775                     p_calling_fn    => l_temp_calling_fn
776                     ,p_log_level_rec => p_log_level_rec) then
777                     raise gainloss_err;
778                 end if;
779             end if;
780 
781             /*Secondary Changes End
782             ==================================================================*/
783             update fa_transaction_headers
784                set event_id = l_trans_rec.event_id
785              where transaction_header_id = l_thid;
786 
787          else
788 
789             if p_log_level_rec.statement_level then
790                fa_debug_pkg.add
791                (fname   => l_calling_fn,
792                 element => 'calling fa_xla_events_pvt.update_transaction_event with thid: ',
793                 value   => l_thid);
794             end if;
795 
796             -- Bug 7292561: Changed the Event_date to match the event_date used
797             -- in create_transaction_event
798             if not fa_xla_events_pvt.update_transaction_event
799                   (p_ledger_id              => l_primary_sob_id,
800                    p_transaction_header_id  => l_thid,
801                    p_book_type_code         => l_asset_hdr_rec.book_type_code,
802                    p_event_type_code        => l_event_type_code,
803                    p_event_date             => greatest(l_trx_date_entered,
804                                                           l_period_rec.calendar_period_open_date),
805                    p_event_status_code      => FA_XLA_EVENTS_PVT.C_EVENT_UNPROCESSED,
806                    p_calling_fn             => l_calling_fn,
807                    p_log_level_rec => p_log_level_rec) then
808                raise gainloss_err;
809             end if;
810             /*=================================================================
811             Secondary Changes Start
812             If primary and secondary sob_id is different then we need to
813             update event for secondary ledger*/
814             l_secondary_sob_id := FA_XLA_EVENTS_PVT.get_secondary_sob_id(l_asset_hdr_rec.book_type_code);
815             if p_log_level_rec.statement_level then fa_debug_pkg.add (fname   => l_calling_fn,element => 'before secondary update l_secondary_sob_id ',value   => l_secondary_sob_id); end if;
816             if p_log_level_rec.statement_level then fa_debug_pkg.add (fname   => l_calling_fn,element => 'before secondary update l_primary_sob_id ',value   => l_primary_sob_id); end if;
817 
818             if(l_secondary_sob_id is not null) then
819                 if not fa_xla_events_pvt.update_transaction_event
820                       (p_ledger_id              => l_secondary_sob_id,
821                        p_transaction_header_id  => l_thid,
822                        p_book_type_code         => l_asset_hdr_rec.book_type_code,
823                        p_event_type_code        => l_event_type_code,
824                        p_event_date             => greatest(l_trx_date_entered,
825                                                               l_period_rec.calendar_period_open_date),
826                        p_event_status_code      => FA_XLA_EVENTS_PVT.C_EVENT_UNPROCESSED,
827                        p_secondary_flag               => TRUE,
828                        p_secondary_sob_id  => l_secondary_sob_id,
829                        p_calling_fn             => l_calling_fn,
830                        p_log_level_rec => p_log_level_rec) then
831                    raise gainloss_err;
832                 end if;
833              end if;
834             /*Secondary Changes End
835             ==================================================================*/
836             --Bug6391045
837             --Assigning event_id to l_trans_rec so that it can be passed to the IAC hook
838             l_trans_rec.event_id := l_event_id;
839 
840 
841          end if; -- null event
842 
843 
844          --Bug6391045
845          if (FA_IGI_EXT_PKG.IAC_Enabled  and
846             G_release <> 11) then
847 
848             if not FA_IGI_EXT_PKG.Do_Gain_Loss(
849                                  p_retirement_id    => ret.retirement_id,
850                                  p_asset_id         => ret.asset_id,
851                                  p_book_type_code   => ret.book,
852                                  p_event_id         => l_trans_rec.event_id,
853                                  p_calling_function => l_calling_fn) then
854 
855                            fa_srvr_msg.add_message(
856                               calling_fn => 'l_calling_fn'||'(Calling IAC)',
857                               name       => 'FA_RET_STATUS_FAIL',
858                               token1     => 'RETID',
859                               value1     => ret.retirement_id,
860                               token2     => 'ASSET',
861                               value2     => ret.asset_number );
862                            raise gainloss_err;
863             end if;
864 
865          end if; -- IAC hook
866 
867       end if;  -- upgrade
868 
869    end if; -- if trx in primary is either retirement or reinstatement
870 
871 
872    if p_log_level_rec.statement_level then
873             fa_debug_pkg.add
874               (fname   => l_calling_fn,
875                element => 'Process status for retirement_id:'||p_retirement_id,
876                value   => 'Success', p_log_level_rec => p_log_level_rec);
877    end if;
878 
879    x_return_status :=  0;
880 
881 EXCEPTION
882 
883    when gainloss_err then
884       ROLLBACK to Do_Calc_GainLoss_Asset;
885       if p_log_level_rec.statement_level then
886             fa_debug_pkg.add
887               (fname   => l_calling_fn,
888                element => 'RETIREMENT_ID',
889                value   => p_retirement_id, p_log_level_rec => p_log_level_rec);
890       end if;
891       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
892       if (g_log_level_rec.statement_level) then
893          FA_DEBUG_PKG.dump_debug_messages(max_mesgs => 0, p_log_level_rec => p_log_level_rec);
894       end if;
895       x_return_status :=  2;
896 
897    when others then
898       ROLLBACK to Do_Calc_GainLoss_Asset;
899       if p_log_level_rec.statement_level then
900             fa_debug_pkg.add
901               (fname   => l_calling_fn,
902                element => 'RETIREMENT_ID',
903                value   => p_retirement_id, p_log_level_rec => p_log_level_rec);
904       end if;
905       fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
906       x_return_status :=  2;
907 
908 END Do_Calc_GainLoss_Asset;
909 
910 END FA_GAINLOSS_PKG;