DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_GAINLOSS_PKG

Source


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