DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_CHK_BOOKSTS_PKG

Source


1 PACKAGE BODY FA_CHK_BOOKSTS_PKG as
2 /* $Header: FAXCKBKB.pls 120.27 2011/10/13 14:01:14 dvjoshi ship $ */
3 
4 g_release                  number  := fa_cache_pkg.fazarel_release;
5 
6 --
7 -- FUNCTION faxcbsx
8 --
9 
10 
11 FUNCTION faxcbsx(X_book              IN VARCHAR2,
12                  X_init_message_flag    VARCHAR2 DEFAULT 'NO',
13                  X_close_period      in NUMBER DEFAULT 1, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
14          return BOOLEAN is
15 
16    h_txn_status  BOOLEAN := FALSE;   -- TRUE if txn allowed, FALSE otherwise
17 
18 BEGIN
19 
20    if (X_init_message_flag = 'YES') then
21       fa_srvr_msg.init_server_message;
22       fa_debug_pkg.initialize;
23    end if;
24 
25    if (X_book is NULL) then
26       fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcbsx',
27                               name       => 'FA_SHARED_ARGUMENTS',
28                               token1     => 'PROGRAM',
29                               value1     => 'CHECK_BOOK_STATUS', p_log_level_rec => p_log_level_rec);
30       h_txn_status := FALSE;
31 
32    -- faxcbs will check status of any processes running on this book
33 
34    elsif (NOT faxcbs(X_book         => X_book,
35                      X_submit       => TRUE,
36                      X_start        => FALSE,
37                      X_asset_id     => 0,
38                      X_trx_type     => 'OTHER',
39                      X_txn_status   => h_txn_status,
40                      X_close_period => X_close_period,
41                      p_log_level_rec => p_log_level_rec)) then
42 
43       fa_srvr_msg.add_message(calling_fn =>'FA_CHK_BOOKSTS_PKG.faxcbsx',
44                               name       => 'FA_SHARED_END_WITH_ERROR',
45                               token1     => 'PROGRAM',value1=>'CHECK_BOOK_STATUS', p_log_level_rec => p_log_level_rec);
46       h_txn_status := FALSE;
47 
48    end if;
49 
50    -- h_txn_status, at this point, have value of TRUE if txn is approved
51    -- and FALSE if txn is not approved
52 
53    if (p_log_level_rec.statement_level) then
54        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcbsx','txn status',h_txn_status, p_log_level_rec => p_log_level_rec);
55    end if;
56 
57    return(h_txn_status);
58 
59 EXCEPTION
60    when others then
61 
62    fa_srvr_msg.add_sql_error(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcbsx', p_log_level_rec => p_log_level_rec);
63    return (FALSE);
64 
65 END faxcbsx;
66 
67 --
68 -- FUNCTION faxcrb
69 --
70 -- This function has been added to handle MRC enabled books.
71 -- This function will check if the book on which a transaction is being
72 -- performed is a primary book with reporting books associated to it(MRC).
73 -- It will check to see if the reporting books depreciation status is complete
74 -- and whether the primary and reporting book periods are in synch.
75 -- When depreciation is submitted, this function will check to see if
76 -- depreciation has already been run for the reporting books and will return
77 -- false if this is not the case. When depreciation is submitted for a
78 -- reporting book, we will check to see if depreciation has already been run
79 -- for the period in which the Primary books is and will return false if
80 -- this is the case.
81 
82 -- BUG# 1470923
83 --  removed the tax cursor and surrounding logic. instead, have
84 --  added a call to faxcrb from faxptb so it is called for each tax book
85 --  as it looped through
86 
87 -- BUG# 1920416
88 --  modified to simulate the full logic of transaction approval including
89 --  mass requests, close period option for deprn, etc.
90 
91 FUNCTION faxcrb (X_book         IN   VARCHAR2,
92                  X_trx_type     IN   VARCHAR2,
93                  X_asset_id     IN   NUMBER,
94                  X_close_period IN   NUMBER, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
95 
96          return BOOLEAN is
97 
98    h_mc_source_flag         varchar2(3);
99    h_last_period_counter    NUMBER(15);
100    h_approve                BOOLEAN := TRUE;
101    h_book                   VARCHAR2(30);
102    h_count                  NUMBER(15) := 0;
103    h_mrc_sob_type_code      VARCHAR2(1);
104    h_set_of_books_id        NUMBER(15);
105    conversion_exception     exception;
106    efc_conversion_exception exception;
107 
108 
109    -- used for any
110    CURSOR check_efc_conversion_status IS
111           select count(*)
112             from fa_mc_conversion_history
113            where efc_status in ('I', 'U', 'E')
114              and book_type_code = h_book;
115 
116    -- used for primary
117    -- Bug 4748124 spooyath
118    -- Added an nvl to conversion_status to catch
119    -- the case when conversion_status is null
120    CURSOR check_conversion_status IS
121           select count(*)
122             from fa_mc_book_controls
123            where book_type_code = h_book
124              and nvl(conversion_status,' ') in ('S', 'R', 'E',' ')
125              and mrc_converted_flag = 'N'
126              and enabled_flag = 'Y';
127 
128    CURSOR check_reporting_deprn_open IS
129           select count(1)
130             from fa_mc_book_controls mcbk
131            where mcbk.book_type_code = h_book
132              and (mcbk.deprn_status <> 'C' OR
133                   ((mcbk.last_period_counter <> h_last_period_counter + 1) AND
134                    (mcbk.last_period_counter <> h_last_period_counter)))
135              and enabled_flag = 'Y';
136 
137    CURSOR check_reporting_deprn_close IS
138           select count(1)
139             from fa_mc_book_controls mcbk
140            where mcbk.book_type_code = h_book
141              and (mcbk.deprn_status <> 'C' OR
142                   (mcbk.last_period_counter <> h_last_period_counter + 1))
143              and enabled_flag = 'Y';
144 
145    CURSOR check_reporting_mass IS
146           select count(1)
147             from fa_mc_book_controls mcbk
148            where mcbk.book_type_code = h_book
149              and (mcbk.deprn_status <> 'C'  OR
150                   (mcbk.last_period_counter <> h_last_period_counter) OR
151                    (mcbk.mass_request_id is not null))
152              and enabled_flag = 'Y';
153 
154    -- just like faxcds, asset transactions should not necessarily be prevented
155    -- in cases where deprn_status is E as we'll check whether they were
156    -- processed in faxcdr.
157 
158    CURSOR check_reporting_single IS
159           select count(1)
160             from fa_mc_book_controls mcbk
161            where mcbk.book_type_code = h_book
162              and (mcbk.deprn_status not in ('C', 'E')  OR
163                   (mcbk.last_period_counter <> h_last_period_counter) OR
164                   (mcbk.mass_request_id is not null))
165              and enabled_flag = 'Y';
166 
167 
168    -- used when called from a reporting book
169    CURSOR check_primary_sync IS
170           select count(1)
171             from fa_book_controls_bas bc
172            where bc.book_type_code = X_book
173              and (bc.last_period_counter <> h_last_period_counter or
174                   bc.mass_request_id is not null);
175 
176 BEGIN
177 
178    h_mc_source_flag       := FA_CACHE_PKG.fazcbc_record.mc_source_flag;
179    h_last_period_counter  := FA_CACHE_PKG.fazcbc_record.last_period_counter;
180    h_set_of_books_id      := FA_CACHE_PKG.fazcbc_record.set_of_books_id;
181 
182    if not fa_cache_pkg.fazcsob
183             (X_set_of_books_id   => h_set_of_books_id,
184              X_mrc_sob_type_code => h_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
185       fa_srvr_msg.add_sql_error
186            (calling_fn => 'fa_chk_booksts_pkg.faxcrb', p_log_level_rec => p_log_level_rec);
187       return(FALSE);
188    end if;
189 
190    if (p_log_level_rec.statement_level) then
191        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcrb',
192                         'sob_id',h_set_of_books_id, p_log_level_rec => p_log_level_rec);
193        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcrb',
194                         'reporting_type',h_mrc_sob_type_code, p_log_level_rec => p_log_level_rec);
195        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcrb',
196                         'last period counter',h_last_period_counter, p_log_level_rec => p_log_level_rec);
197    end if;
198 
199    -- check to see if the efc conversion is in process
200    open check_efc_conversion_status;
201    fetch check_efc_conversion_status into h_count;
202    close check_efc_conversion_status;
203 
204    if (h_count > 0) then
205       RAISE efc_conversion_exception;
206    end if;
207 
208    h_book := X_book;
209 
210    if (h_mrc_sob_type_code = 'P' and h_mc_source_flag = 'Y') then
211 
212       if (p_log_level_rec.statement_level) then
213           fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcrb','checking for ','Primary', p_log_level_rec => p_log_level_rec);
214       end if;
215 
216       -- check to see if mrc upgrade is in process for a reporting
217       -- book associated to this primary book
218       open check_conversion_status;
219       fetch check_conversion_status into h_count;
220       close check_conversion_status;
221 
222       if (h_count > 0) then
223           RAISE conversion_exception;
224       end if;
225 
226       if (X_trx_type = 'OTHER') then
227          -- txn type of OTHER is used when submitting depreciation from
228          -- FAXDPRUN, branch depending on close period flag
229          if (X_close_period = 1) then
230             open check_reporting_deprn_close;
231             fetch check_reporting_deprn_close into h_count;
232             if (h_count > 0) then
233                 h_approve := FALSE;
234                 FA_SRVR_MSG.add_message
235                       (CALLING_FN  =>  'FA_CHK_BOOKSTS_PKG.faxcrb',
236                        NAME        =>  'FA_MRC_REP_BOOK_DEP_NOT_RUN', p_log_level_rec => p_log_level_rec);
237             end if;
238             close check_reporting_deprn_close;
239          else
240             open check_reporting_deprn_open;
241             fetch check_reporting_deprn_open into h_count;
242             if (h_count > 0) then
243                h_approve := FALSE;
244                FA_SRVR_MSG.add_message
245                      (CALLING_FN  =>  'FA_CHK_BOOKSTS_PKG.faxcrb',
246                       NAME        =>  'FA_MRC_REP_BOOK_DEP_NOT_RUN', p_log_level_rec => p_log_level_rec);
247             end if;
248             close check_reporting_deprn_open;
249          end if;
250       elsif (X_trx_type = 'RB_DEP' or
251          X_trx_type = 'RB_CJE' or
252          X_trx_type = 'GAINLOSS' or
253          X_trx_type = 'PRODUCTION ENTRY') then
254          -- no need to check the reporting book here as they do not
255          -- impact the reporting books due to one-step, etc and no
256          -- other mass process on reporting would be relevant.
257          null;
258       elsif (X_asset_ID = 0) then -- for any mass transaction
259          open check_reporting_mass;
260          fetch check_reporting_mass into h_count;
261          if (h_count > 0) then
262             FA_SRVR_MSG.add_message
263                   (CALLING_FN  =>  'FA_CHK_BOOKSTS_PKG.faxcrb',
264                    NAME        =>  'FA_MRC_PRI_REP_PERIOD_DIFF', p_log_level_rec => p_log_level_rec);
265             h_approve := FALSE;
266          end if;
267          close check_reporting_mass;
268       else -- for any asset transaction
269          open check_reporting_single;
270          fetch check_reporting_single into h_count;
271          if (h_count > 0) then
272             FA_SRVR_MSG.add_message
273                   (CALLING_FN  =>  'FA_CHK_BOOKSTS_PKG.faxcrb',
274                    NAME        =>  'FA_MRC_PRI_REP_PERIOD_DIFF', p_log_level_rec => p_log_level_rec);
275             h_approve := FALSE;
276          end if;
277          close check_reporting_single;
278       end if;
279 
280    -- else submitting mass process on reporting book (dep, gl, rbdep)
281    -- prevent if depreciation for prior period has not run for Primary
282    -- or if a mass request is locking the primary book
283 
284    elsif (h_mrc_sob_type_code = 'R' and X_trx_type <> 'RB_DEP') then
285       if (p_log_level_rec.statement_level) then
286          fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcrb','checking','reporting', p_log_level_rec => p_log_level_rec);
287       end if;
288 
289       open check_primary_sync;
290       fetch check_primary_sync into h_count;
291       if (h_count > 0) then
292          FA_SRVR_MSG.add_message
293                (CALLING_FN  =>  'FA_CHK_BOOKSTS_PKG.faxcrb',
294                 NAME        =>  'FA_MRC_RUN_DEP_CORP_FIRST', p_log_level_rec => p_log_level_rec);
295          h_approve := FALSE;
296       end if;
297       close check_primary_sync;
298    end if;   --h_mc_source_flag
299 
300    RETURN(h_approve);
301 
302 EXCEPTION
303    WHEN conversion_exception THEN
304         FA_SRVR_MSG.add_message
305               (CALLING_FN  =>  'FA_CHK_BOOKSTS_PKG.faxcrb',
306                NAME        =>  'FA_MRC_CONV_PROCESS', p_log_level_rec => p_log_level_rec);
307         return(FALSE);
308 
309    WHEN efc_conversion_exception THEN
310         FA_SRVR_MSG.add_message
311               (CALLING_FN  =>  'FA_CHK_BOOKSTS_PKG.faxcrb',
312                NAME        =>  'FA_EFC_CONV_PROCESS', p_log_level_rec => p_log_level_rec);
313         return(FALSE);
314 
315    WHEN OTHERS THEN
316         fa_srvr_msg.add_sql_error(calling_fn => 'fa_chk_booksts_pkg.faxcrb', p_log_level_rec => p_log_level_rec);
317         return(FALSE);
318 
319 END faxcrb;
320 
321 
322 --
323 -- FUNCTION faxcbs
324 --
325 
326 FUNCTION faxcbs (X_book          IN     VARCHAR2,
327                  X_submit        IN     BOOLEAN,
328                  X_start         IN     BOOLEAN,
329                  X_asset_id      IN     NUMBER,
330                  X_trx_type      IN     VARCHAR2,
331                  X_txn_status    IN OUT NOCOPY BOOLEAN,
332                  X_close_period  IN     NUMBER DEFAULT 1, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
333          return BOOLEAN is
334 
335 
336    h_class_str              VARCHAR2(15);
337    h_allow_cip_assets_flag  VARCHAR2(3);
338    h_tmp                    BOOLEAN;
339 
340    ERROR_FOUND  EXCEPTION;
341 
342 BEGIN
343 
344    -- Note: X_txn_status is a boolean variable which is an argument
345    -- to several functions  and gets updated to TRUE if no error occured or
346    -- no process is running on the book, etc.
347    --
348    -- It gets set to FALSE if non-complete process is running on the book,
349    -- fail to obtain lock for the book, or unknown error occurs, etc
350 
351    if (NOT FA_CACHE_PKG.fazcbc(X_book => X_book, p_log_level_rec => p_log_level_rec)) then
352        raise ERROR_FOUND;
353    end if;
354 
355    h_class_str := fa_cache_pkg.fazcbc_record.book_class;
356    h_allow_cip_assets_flag := fa_cache_pkg.fazcbc_record.allow_cip_assets_flag;
357 
358    -- set savepoint, so that we can rollback when error occurs
359 
360     if (NOT faxsav(X_action     => 'S',
361                    X_txn_status => X_txn_status,
362                    p_log_level_rec => p_log_level_rec)) then
363        raise ERROR_FOUND;
364     end if;
365 
366    -- lock the book row before checking for txn approval
367 
368    if (NOT faxlck(X_book       => X_book,
369                   X_txn_status => X_txn_status,
370                   X_asset_id   => X_asset_id,
371                   X_trx_type   => X_trx_type,
372                   p_log_level_rec => p_log_level_rec)) then
373       raise ERROR_FOUND;
374    end if;
375 
376    if (p_log_level_rec.statement_level) then
377        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcbs','txn_status after faxlck',X_txn_status, p_log_level_rec => p_log_level_rec);
378    end if;
379 
380    -- X_txn_status is TRUE if nothing failed so far and check any
381    -- processes on this book
382 
383     if (X_txn_status) then
384        if (NOT faxcps(X_book         => X_book,
385                       X_submit       => X_submit,
386                       X_start        => X_start,
387                       X_asset_id     => X_asset_id,
388                       X_trx_type     => X_trx_type,
389                       X_txn_status   => X_txn_status,
390                       X_close_period => X_Close_Period,
391                       p_log_level_rec => p_log_level_rec)) then
392            raise ERROR_FOUND;
393        end if;
394     end if;
395 
396    -- faxcps above returns X_txn_status false when process is in running, inactive,
397    -- or pending status
398 
399    if (p_log_level_rec.statement_level) then
400        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcbs','txn status after faxcps',
401                          X_txn_status, p_log_level_rec => p_log_level_rec);
402    end if;
403    if (X_txn_status) then
404 
405       if (h_class_str = 'CORPORATE') then
406 
407          if (NOT X_submit) then
408 
409             -- not submitting depreciation, check linked tax books if
410             -- reclass or transfer. Original code has been merged with
411             -- CIP-TAX code below for easier maintenance
412 
413             -- cip-in-trx's have been removed since the trigger
414             -- solution has been replaced with a full api which
415             -- insures transaction approval is called for each book
416 
417             if (X_trx_type = 'TRANSFER' OR
418                 X_trx_type = 'RECLASS'  OR
419                 X_trx_type = 'TRANSFER OUT' OR
420                 X_trx_type = 'UNIT ADJUSTMENT' OR
421                 X_trx_type = 'PARTIAL UNIT RETIREMENT') then
422 
423                if (NOT faxptb(X_book       => X_book,
424                               X_start      => X_start,
425                               X_asset_id   => X_asset_id,
426                               X_trx_type   => X_trx_type,
427                               X_txn_status => X_txn_status,
428                               p_log_level_rec => p_log_level_rec)) then
429                        raise ERROR_FOUND;
430                end if;
431             end if;
432          end if;
433 
434       elsif (h_class_str = 'TAX') then
435 
436          -- check for incompatable processes on CORP book
437          --
438          -- BUG# 1936983 - do not check for rollback deprn
439          -- since no conflicting corp transaction would have
440          -- been allowed if deprn had been run withut closing
441          -- period.  Eventually we may need to enhance the
442          -- faxgcb function to accomidate other transactions
443          -- against mrc but currently this is the only one.
444 
445          -- Fix for Bug #2381635.  Added RB_CJE
446          if (X_trx_type not in ('RB_DEP', 'RB_CJE')) then
447 
448             if (NOT faxgcb(X_book       => X_book,
449                            X_txn_status => X_txn_status,
450                            p_log_level_rec => p_log_level_rec)) then
451                raise ERROR_FOUND;
452             end if;
453          end if;
454 
455       else
456          fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS.faxcbs',
457                                  name       => 'FA_TRXAPP_WRONG_BOOK_CLASS',
458                                  token1     =>'CLASS',value1=>h_class_str, p_log_level_rec => p_log_level_rec);
459 
460          h_tmp := faxsav(X_action           => 'R',
461                          X_txn_status       => X_txn_status,
462                          p_log_level_rec => p_log_level_rec); -- rollback the lock
463       end if;
464    end if;
465 
466    -- put the main book back on cache
467    if (NOT FA_CACHE_PKG.fazcbc(X_book => X_book, p_log_level_rec => p_log_level_rec)) then
468        raise ERROR_FOUND;
469    end if;
470 
471    -- check book status is complete and clear savepoint indicator for next txns,
472    -- but keep rows locked
473 
474    if (NOT faxsav(X_action     => 'C',
475                   X_txn_status => X_txn_status,
476                   p_log_level_rec => p_log_level_rec)) then
477       raise ERROR_FOUND;
478    end if;
479 
480    return(TRUE);
481 
482 EXCEPTION
483    WHEN ERROR_FOUND THEN
484         fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS.faxcbs', p_log_level_rec => p_log_level_rec);
485 
486         h_tmp := faxsav(X_action     => 'R',
487                         X_txn_status => X_txn_status,
488                         p_log_level_rec => p_log_level_rec);
489         -- rollback the lock, if any
490         return (FALSE);
491 
492    WHEN OTHERS THEN
493         fa_srvr_msg.add_sql_error(calling_fn => 'FA_CHK_BOOKSTS.faxcbs', p_log_level_rec => p_log_level_rec);
494         h_tmp := faxsav(X_action     => 'R',
495                         X_txn_status => X_txn_status,
496                         p_log_level_rec => p_log_level_rec);
497         -- rollback the lock, if any
498 
499         return(FALSE);
500 END faxcbs;
501 
502 --
503 -- FUNTION faxcps
504 --
505 
506 FUNCTION faxcps (X_book         IN     VARCHAR2,
507                  X_submit       IN     BOOLEAN,
508                  X_start        IN     BOOLEAN,
509                  X_asset_id     IN     NUMBER,
510                  X_trx_type     IN     VARCHAR2,
511                  X_txn_status   IN OUT NOCOPY BOOLEAN,
512                  X_close_period IN     NUMBER, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
513          return BOOLEAN is
514 
515    h_conc_request_id  NUMBER := fnd_global.conc_request_id;
516    h_request_id       NUMBER := 0;  -- set to non-zero if there is non-complete running process
517    h_ca_request_id    NUMBER := 0;  -- set to non-zero if there is non-complete running create accounting process
518    h_tmp              BOOLEAN;
519 
520    h_deprn_stat       varchar2(1);
521    h_deprn_req_id     number;
522    h_mass_req_id      number;
523    h_last_period_ctr  number;
524    h_sob_id           number;
525    h_mc_source_flag   varchar2(1);
526    h_allow_cip_assets varchar2(3);
527 
528 BEGIN
529 
530    -- BUG# 1910467
531    --
532    -- We are reanabling the caching features in fazcbc which
533    -- will impact multiple places.  Do to potential changes in values
534    -- like last_period_counter, we need to insure the data in the cache
535    -- is not stale.  Thus, we will do a direct select here and compare
536    -- the values.  If the data has changed, we will clear and reset
537    -- the cache accordingly.
538    --
539    -- caches have already been called from faxcbs or faxptb
540    -- also adding the new deprn period cache to this as well
541    --
542    --   -- bridgway 08/01/01
543 
544    if not fa_cache_pkg.fazcdp
545            (x_book_type_code => x_book, p_log_level_rec => p_log_level_rec) then
546       return false;
547    end if;
548 
549    -- BUG# 2247404 / BUG# 2230178
550    -- do the following only when called standalone for an individual trx
551    -- or for a mass transaction at main entry from that process
552    --
553    -- do not do it for each transaction called from a mass process
554    -- this will allow mass transactions like FAMAPT after a deprn run
555    -- on a line by line basis
556 
557    if (((nvl(fnd_global.conc_request_id, -1) >= 0)  and
558         (X_asset_id = 0)) or
559        ((nvl(fnd_global.conc_request_id, -1) < 0) and
560         (X_asset_id <> 0))) then
561 
562       select deprn_status,
563              deprn_request_id,
564              last_period_counter,
565              mass_request_id,
566              set_of_books_id,
567              mc_source_flag,
568              allow_cip_assets_flag
569         into h_deprn_stat,
570              h_deprn_req_id,
571              h_last_period_ctr,
572              h_mass_req_id,
573              h_sob_id,
574              h_mc_source_flag,
575              h_allow_cip_assets
576         from fa_book_controls
577        where book_type_code = X_book;
578 
579       if (nvl(h_last_period_ctr, -99) <>
580               nvl(FA_CACHE_PKG.fazcbc_record.last_period_counter, -99) OR
581           nvl(h_deprn_stat, 'X') <>
582                nvl(FA_CACHE_PKG.fazcbc_record.deprn_status, 'X') OR
583           nvl(h_deprn_req_id, -99) <>
584                nvl(FA_CACHE_PKG.fazcbc_record.deprn_request_id, -99) OR
585           nvl(h_mass_req_id, -99) <>
586                nvl(FA_CACHE_PKG.fazcbc_record.mass_request_id, -99) OR
587           nvl(h_sob_id, -99) <>
588                nvl(FA_CACHE_PKG.fazcbc_record.set_of_books_id, -99) OR
589           nvl(h_mc_source_flag, 'X') <>
590                nvl(FA_CACHE_PKG.fazcbc_record.mc_source_flag, 'X') OR
591           nvl(h_allow_cip_assets, 'X') <>
592                nvl(FA_CACHE_PKG.fazcbc_record.allow_cip_assets_flag, 'X'))  then
593 
594           -- clear the book from the cache (member and array)
595           if (NOT FA_CACHE_PKG.fazcbc_clr(X_book => X_book, p_log_level_rec => p_log_level_rec)) then
596               fa_srvr_msg.add_sql_error(calling_fn=>'fa_chk_booksts_pkg.faxcps', p_log_level_rec => p_log_level_rec);
597               return(FALSE);
598           end if;
599 
600           -- now recall it
601           if (NOT FA_CACHE_PKG.fazcbc(X_book => X_book, p_log_level_rec => p_log_level_rec)) then
602              fa_srvr_msg.add_sql_error(calling_fn=>'fa_chk_booksts_pkg.faxcps', p_log_level_rec => p_log_level_rec);
603              return(FALSE);
604           end if;
605       end if;
606 
607       if (FA_CACHE_PKG.fazcbc_record.last_period_counter + 1 <>
608           FA_CACHE_PKG.fazcdp_record.period_counter ) then
609 
610           -- clear the book from the cache (member and array)
611           if (NOT FA_CACHE_PKG.fazcdp_clr(X_book => X_book, p_log_level_rec => p_log_level_rec)) then
612               fa_srvr_msg.add_sql_error(calling_fn=>'fa_chk_booksts_pkg.faxcps', p_log_level_rec => p_log_level_rec);
613               return(FALSE);
614           end if;
615 
616           -- now recall it
617           if (NOT FA_CACHE_PKG.fazcdp(X_book_type_code => X_book, p_log_level_rec => p_log_level_rec)) then
618              fa_srvr_msg.add_sql_error(calling_fn=>'fa_chk_booksts_pkg.faxcps', p_log_level_rec => p_log_level_rec);
619              return(FALSE);
620           end if;
621       end if;
622 
623       if (NOT faxcrb(X_book         => X_book,
624                   X_trx_type     => X_trx_type,
625                   X_asset_id     => X_asset_id,
626                   X_close_period => X_close_period,
627                   p_log_level_rec => p_log_level_rec)) then
628          fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps', p_log_level_rec => p_log_level_rec);
629          return(FALSE);
630       end if;
631    end if;
632 
633    -- check if depreciation is running or errored
634    if (NOT faxcds(X_book       => X_book,
635                   X_submit     => X_submit,
636                   X_asset_id   => X_asset_id,
637                   X_trx_type   => X_trx_type,
638                   X_txn_status => X_txn_status,
639                   p_log_level_rec => p_log_level_rec)) then
640        fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps', p_log_level_rec => p_log_level_rec);
641        return(FALSE);
642    end if;
643 
644    -- if trx_approval failed due to deprn running, return
645    if (NOT X_txn_status) then
646        return (TRUE);
647    end if;
648 
649    -- check the status of mass request id, if any, in book_control
650    if(NOT faxcms(X_book       => X_book,
651                  X_request_id => h_request_id,
652                  p_log_level_rec => p_log_level_rec)) then
653        fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps', p_log_level_rec => p_log_level_rec);
654        return(FALSE);
655    end if;
656 
657    -- check if create accounting is running for cip reversals
658    -- deprn is done within faxcdr seperately
659    if x_trx_type in ('REVERSE') then
660 
661       if(NOT faxcca(X_book       => X_book,
662                     X_request_id => h_ca_request_id,
663 	   	 p_log_level_rec => p_log_level_rec)) then
664           fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps',
665                                   p_log_level_rec  => p_log_level_rec);
666           return(FALSE);
667       end if;
668    else
669       h_ca_request_id := 0;
670    end if;
671 
672    if (p_log_level_rec.statement_level) then
673        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcps','book',X_book, p_log_level_rec => p_log_level_rec);
674        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcps','mass_request_id',
675                         h_request_id, p_log_level_rec => p_log_level_rec);
676        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcps','create_accounting_request_id', h_ca_request_id, p_log_level_rec);
677        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcps','conc_request_id',
678                         h_conc_request_id, p_log_level_rec => p_log_level_rec);
679    end if;
680 
681    if (h_request_id <> 0) then
682       -- req_id is in status of running,inactive,or pending
683       fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps',
684                               name       => 'FA_TRXAPP_WAIT_REQUEST',
685                               token1     => 'REQUEST_ID',
686                               value1     => h_request_id,
687                               token2     => 'BOOK',
688                               value2     => X_book, p_log_level_rec => p_log_level_rec);
689       h_tmp := faxsav(X_action           => 'R',
690                       X_txn_status       => X_txn_status,
691                       p_log_level_rec => p_log_level_rec);
692       --rollback the lock, if any
693    elsif (h_ca_request_id <> 0) then
694       -- req_id is in status of running,inactive,or pending
695       fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps',
696                               name       => 'FA_TRXAPP_WAIT_REQUEST',
697                               token1     => 'REQUEST_ID',
698                               value1     => h_ca_request_id,
699                               token2     => 'BOOK',
700                               value2     => X_book,
701 			      p_log_level_rec => p_log_level_rec);
702       h_tmp := faxsav(X_action           => 'R',
703                       X_txn_status       => X_txn_status,
704 		      p_log_level_rec => p_log_level_rec);
705       --rollback the lock, if any
706    end if;
707 
708    if (X_start and (h_request_id <> 0 or h_ca_request_id <> 0)) then
709       --  for now faxwcr will always return TRUE
710       if (NOT faxwcr(X_request_id => h_request_id,
711                      p_log_level_rec => p_log_level_rec)) then
712          fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps', p_log_level_rec => p_log_level_rec);
713          return(FALSE);
714       end if;
715    end if;
716 
717    return(TRUE);
718 
719 EXCEPTION
720    WHEN OTHERS THEN
721         fa_srvr_msg.add_sql_error(calling_fn=>'FA_CHK_BOOKSTS_PKG.faxcps', p_log_level_rec => p_log_level_rec);
722         return(FALSE);
723 
724 END faxcps;
725 
726 
727 --
728 --  FUNCTION faxwcr
729 --
730 
731 FUNCTION faxwcr(X_request_id    IN  NUMBER, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
732          return BOOLEAN is
733 
734 BEGIN
735 
736    -- funtion is currently disabled, will return TRUE immediately
737    return(TRUE);
738 
739    -- The following is not converted and thus remains as C code
740 
741    /*
742     * if (X_request_id <> 0) then
743     *    select to_char(nvl(actual_completion_date,
744     *                       sysdate + least(greatest
745     *                                 (sysdate - actual_start_date,5/1440),1/24)),
746     *                   'DD/MM/YYYY HH24:MI:SS')
747     *      into h_restart
748     *      from fnd_concurrenct_requests
749     *     where request_id = X_request_id
750     * end if;
751     */
752 
753    -- Remaining code is  in faxchk.lpc file
754 
755 END faxwcr;
756 
757 --
758 -- FUNCTION faxcds
759 --
760 
761 FUNCTION faxcds(X_book        IN     VARCHAR2,
762                 X_submit      IN     BOOLEAN,
763                 X_asset_id    IN     NUMBER,
764                 X_trx_type    IN     VARCHAR2,
765                 X_txn_status  IN OUT NOCOPY BOOLEAN, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
766          return BOOLEAN is
767 
768    h_deprn_stat       varchar2(1);
769    h_deprn_req_id     number;
770    h_req_id_sav       number;
771    h_last_period_ctr  number;
772    h_curr_period_ctr  number;
773    h_tmp              boolean;
774    h_error            boolean := FALSE;
775    h_count            number := 0;
776    h_adj_count        number := 0;
777 
778    h_phase            varchar2(30);
779    h_status           varchar2(30);
780    h_dev_phase        varchar2(30);
781    h_dev_status       varchar2(30);
782    h_message          varchar2(240);
783 
784 BEGIN
785 
786    h_deprn_stat      := FA_CACHE_PKG.fazcbc_record.deprn_status;
787    h_deprn_req_id    := FA_CACHE_PKG.fazcbc_record.deprn_request_id;
788    h_last_period_ctr := FA_CACHE_PKG.fazcbc_record.last_period_counter;
789 
790    if (h_deprn_stat = 'C') then   -- status is completed, txn approved
791 
792       -- BUG# 1924172
793       -- need to account for deprn running without closing period
794       -- for both mass and asset level transactions. faxcdr will
795       -- account for either mass or asset level transactions based
796       -- on asset_id passed below.   -- bridgway
797 
798       -- if submitting depreciation, deleteing an asset or rolling back
799       -- depreciation, ok to go ahead
800 
801       if (X_submit or
802           X_trx_type = 'RB_DEP' or
803           X_trx_type = 'RB_CJE' or
804           X_trx_type = 'DELETE') then
805          return(TRUE);
806       end if;
807 
808       if (NOT faxcdr(X_book     => X_book,
809                      X_asset_id => X_asset_id,
810                      p_log_level_rec => p_log_level_rec)) then
811 
812           h_tmp := faxsav(X_action => 'R',
813                           X_txn_status => X_txn_status,
814                           p_log_level_rec => p_log_level_rec);  -- not approved
815       end if;
816       return(TRUE);
817 
818    elsif (h_deprn_stat = 'S' or h_deprn_stat = 'R') then
819       -- stat is submitted or running
820       if (fnd_concurrent.get_request_status
821                (h_deprn_req_id,NULL,NULL,h_phase,
822                 h_status,h_dev_phase,h_dev_status,h_message)) then
823          if (h_dev_phase = 'PENDING' OR
824              h_dev_phase = 'INACTIVE' OR
825              h_dev_phase = 'RUNNING') then
826 
827              fa_srvr_msg.add_message(calling_fn =>'fa_chk_booksts_pkg.faxcds',
828                                      name       => 'FA_TRXAPP_DEPRN_IS_RUNNING',
829                                      token1     =>'BOOK',
830                                      value1     =>X_book, p_log_level_rec => p_log_level_rec);
831 
832              h_tmp := faxsav(X_action     => 'R',
833                              X_txn_status => X_txn_status,
834                              p_log_level_rec => p_log_level_rec);
835              -- rollback the lock,set X_txn_status FALSE
836 
837          -- BUG# 1788850
838          -- this was previously breaking only on the dev_phase check
839          -- which is incorrect.  It must only do this for "S' not "R"
840          -- as the request could have core dumped or been cancelled
841          --
842          -- bridgway  05/17/01
843 
844          elsif ((h_dev_phase  = 'COMPLETE') and
845                 (h_deprn_stat = 'S') and
846                 (not X_submit) and
847                 (X_trx_type   <> 'RB_CJE')) then
848             if (NOT faxcdr(X_book     => X_book,
849                            X_asset_id => X_asset_id,
850                            p_log_level_rec => p_log_level_rec)) then
851 
852                 h_tmp := faxsav(X_action => 'R',
853                                 X_txn_status => X_txn_status,
854                                 p_log_level_rec => p_log_level_rec);
855                 -- not approved
856             end if;
857             return (TRUE);
858          else
859             -- Deprn stat is R, but request failed. e.g core dumped,
860             -- treat it as error
861             h_error := TRUE;
862          end if;
863 
864       else
865          fa_srvr_msg.add_message(calling_fn => 'fa_chk_booksts_pkg.faxcds',
866                                  name => 'FA_CONCURRENT_GET_STATUS', p_log_level_rec => p_log_level_rec);
867          h_error := true; -- BUG# 7669210: continue into error logic
868       end if;
869    end if;
870 
871    if (h_deprn_stat = 'E' or h_error = TRUE) then
872       -- if submitting depreciation or deleteing an asset ok to go ahead
873       -- Bug 12404946 : Return for rollback in R12 also.
874       if (X_submit or
875           X_trx_type = 'DELETE' or
876           X_trx_type = 'RB_CJE' or
877           (X_trx_type = 'RB_DEP' and G_release <> 11)) then
878          return(TRUE);
879       end if;
880 
881       if (X_asset_id = 0) then  -- this is mass process request
882 
883          -- This is a request for a mass process, no approval
884          -- because the last depreciation run failed.
885 
886          fa_srvr_msg.add_message(calling_fn => 'fa_chk_booksts_pkg.faxcds',
887                                  name       => 'FA_TRXAPP_DEPRN_FAILED',
888                                  token1     => 'BOOK',
889                                  value1     => X_book, p_log_level_rec => p_log_level_rec);
890          h_tmp := faxsav(X_action     => 'R',
891                          X_txn_status => X_txn_status,
892                          p_log_level_rec => p_log_level_rec);  -- not approved
893          return(TRUE);
894       end if;
895 
896       -- calling faxcdr so asset level transactions can be approved if
897       -- they have not been depreciated yet.
898       if (NOT faxcdr(X_book     => X_book,
899                      X_asset_id => X_asset_id,
900                      p_log_level_rec => p_log_level_rec)) then
901          h_tmp := faxsav(X_action     => 'R',
902                          X_txn_status => X_txn_status,
903                          p_log_level_rec => p_log_level_rec);  -- not approved
904       end if;
905       return(TRUE);
906 
907    else  -- unknown status
908       fa_srvr_msg.add_message(calling_fn => 'fa_chk_booksts_pkg.faxcds',
909                               name       => 'FA_TRXAPP_UNKNOWN_STATUS',
910                               token1     => 'STATUS',
911                               value1     => h_deprn_stat, p_log_level_rec => p_log_level_rec);
912       h_tmp := faxsav(X_action     => 'R',
913                       X_txn_status => X_txn_status,
914                       p_log_level_rec => p_log_level_rec);  -- not approved
915       return (TRUE);
916    end if;
917 
918 EXCEPTION
919    when others then
920         fa_srvr_msg.add_sql_error(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcds',  p_log_level_rec => p_log_level_rec);
921         return(FALSE);
922 END faxcds;
923 
924 
925 --
926 -- FUNCTION faxcms
927 --
928 
929 FUNCTION faxcms(X_book         IN    VARCHAR2,
930                 X_request_id   OUT NOCOPY   NUMBER, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
931          return BOOLEAN is
932 
933    h_conc_req_id     NUMBER;
934    h_parent_req_id   NUMBER;
935    h_mass_req_id     NUMBER;
936    h_phase           VARCHAR2(200) := NULL;
937    h_status          VARCHAR2(200) := NULL;
938    h_dev_phase       VARCHAR2(200) := NULL;
939    h_dev_status      VARCHAR2(200) := NULL;
940    h_message         VARCHAR2(2000):= NULL;
941    h_count           number;
942   cursor c_parent_request(p_request_id  NUMBER) is
943   select parent_request_id
944     from fnd_concurrent_requests
945    where request_id = p_request_id;
946 
947 BEGIN
948 
949    X_request_id  := 0;
950    h_mass_req_id := fa_cache_pkg.fazcbc_record.mass_request_id;
951    h_conc_req_id := fnd_global.conc_request_id;
952 
953    if (h_mass_req_id IS NOT NULL) then
954 
955       if (h_mass_req_id <> h_conc_req_id) then
956          -- account for the potential that this is a child
957          -- request by looking up parent_request_id
958          open c_parent_request (h_conc_req_id);
959          fetch c_parent_request into h_parent_req_id;
960 
961          if (c_parent_request%FOUND) then
962             h_conc_req_id := nvl(h_parent_req_id, h_conc_req_id);
963          end if;
964          close c_parent_request;
965 
966          if (h_mass_req_id <> h_conc_req_id) then
967             -- check for status of this mass request id
968             -- get the parent request if applicable
969 
970             if (h_conc_req_id = 0 and
971                 h_mass_req_id = 0) then
972                -- this is a mass request from pro*c command line
973                -- so allow the transaction. faxbmt will not allow
974                -- another program to run in such a case, so we
975                -- can be sure it's the same request.  For pl/sql
976                -- programs, it would be -1 from sql*plus
977                X_request_id := 0;
978 
979             elsif (fnd_concurrent.get_request_status
980                  (h_mass_req_id,NULL,NULL,h_phase,h_status,
981                   h_dev_phase, h_dev_status, h_message)) then
982 
983                if (h_dev_phase = 'PENDING' OR
984                    h_dev_phase = 'INACTIVE' OR
985                    h_dev_phase = 'RUNNING') then
986                   X_request_id := h_mass_req_id;
987                else  -- completed request
988                   -- BUG# 5114320
989                   -- even if request was cancelled/terminated,
990                   -- make sure the db processes no longer exist
991                   select count(*)
992                     into h_count
993                     from v$session a
994                    where a.audsid  in
995                          (select ORACLE_SESSION_ID
996                             from fnd_concurrent_requests
997                            where request_id        = h_mass_req_id
998                               or parent_request_id = h_mass_req_id);
999 
1000                   if (h_count = 0) then
1001                      X_request_id := 0;
1002                   else
1003                      X_request_id := h_mass_req_id;
1004                   end if;
1005                end if;
1006             else
1007                fa_srvr_msg.add_message(calling_fn => 'fa_chk_booksts_pkg.faxcms',
1008                                        name       => 'FA_CONCURRENT_GET_STATUS', p_log_level_rec => p_log_level_rec);
1009                -- return(FALSE); -- BUG# 7669210: do not error now
1010             end if;
1011          else -- same request based on parent
1012             X_request_id := 0;
1013          end if;
1014       else -- same request based on conc_req
1015          X_request_id := 0;
1016       end if;
1017    else -- no request is locking book
1018       X_request_id := 0;
1019    end if;
1020 
1021    if (p_log_level_rec.statement_level) then
1022        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcms','mass_request_id',
1023                          h_mass_req_id, p_log_level_rec => p_log_level_rec);
1024        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcms','status',h_dev_phase, p_log_level_rec => p_log_level_rec);
1025    end if;
1026 
1027    return(TRUE);
1028 
1029 EXCEPTION
1030    WHEN OTHERS THEN
1031         fa_srvr_msg.add_sql_error(calling_fn => 'fa_chk_booksts_pkg.faxcms', p_log_level_rec => p_log_level_rec);
1032         return(FALSE);
1033 
1034 END faxcms;
1035 
1036 FUNCTION faxcca(X_book         IN    VARCHAR2,
1037                 X_request_id   OUT NOCOPY   NUMBER,
1038 		p_log_level_rec IN  FA_API_TYPES.log_level_rec_type)
1039          return BOOLEAN is
1040 
1041    h_ca_req_id     NUMBER;
1042    h_phase           VARCHAR2(200) := NULL;
1043    h_status          VARCHAR2(200) := NULL;
1044    h_dev_phase       VARCHAR2(200) := NULL;
1045    h_dev_status      VARCHAR2(200) := NULL;
1046    h_message         VARCHAR2(2000):= NULL;
1047    h_count           number;
1048 
1049 BEGIN
1050 
1051    X_request_id  := 0;
1052    h_ca_req_id   := fa_cache_pkg.fazcbc_record.create_accounting_request_id;
1053 
1054    if (h_ca_req_id IS NOT NULL) then
1055 
1056       -- check for status of this mass request id
1057       -- get the parent request if applicable
1058 
1059       if (fnd_concurrent.get_request_status
1060           (h_ca_req_id,NULL,NULL,h_phase,h_status,
1061            h_dev_phase, h_dev_status, h_message)) then
1062 
1063          if (h_dev_phase = 'PENDING' OR
1064             h_dev_phase = 'INACTIVE' OR
1065             h_dev_phase = 'RUNNING') then
1066             X_request_id := h_ca_req_id;
1067          else  -- completed request
1068             -- BUG# 5114320
1069             -- even if request was cancelled/terminated,
1070             -- make sure the db processes no longer exist
1071             select count(*)
1072               into h_count
1073               from v$session a
1074              where a.audsid  in
1075                    (select ORACLE_SESSION_ID
1076                       from fnd_concurrent_requests
1077                      where request_id        = h_ca_req_id
1078                         or parent_request_id = h_ca_req_id);
1079 
1080             if (h_count = 0) then
1081                X_request_id := 0;
1082             else
1083                X_request_id := h_ca_req_id;
1084             end if;
1085          end if;
1086       else
1087          fa_srvr_msg.add_message(calling_fn => 'fa_chk_booksts_pkg.faxcms',
1088                                  name       => 'FA_CONCURRENT_GET_STATUS');
1089          -- BUG# 7669210: do not treat as error anylonger
1090          X_request_id := 0;
1091          return(TRUE);
1092       end if;
1093    else -- no request is locking book
1094       X_request_id := 0;
1095    end if;
1096 
1097    if (p_log_level_rec.statement_level) then
1098        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcca','create_accounting_request_id', h_ca_req_id, p_log_level_rec);
1099        fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcca','status',h_dev_phase, p_log_level_rec);
1100    end if;
1101 
1102    return(TRUE);
1103 
1104 EXCEPTION
1105    WHEN OTHERS THEN
1106         fa_srvr_msg.add_sql_error(calling_fn => 'fa_chk_booksts_pkg.faxcca',
1107 p_log_level_rec => p_log_level_rec);
1108         return(FALSE);
1109 
1110 END faxcca;
1111 
1112 --
1113 -- FUNCTION  faxptb
1114 --
1115 
1116 FUNCTION faxptb(X_book           IN     VARCHAR2,
1117                 X_start          IN     BOOLEAN,
1118                 X_asset_id       IN     NUMBER,
1119                 X_trx_type       IN     VARCHAR2,
1120                 X_txn_status     IN OUT NOCOPY BOOLEAN, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
1121         return BOOLEAN is
1122 
1123    h_tax_book      VARCHAR2(30);
1124    h_retval        BOOLEAN := TRUE;
1125    h_cursor        varchar2(6);
1126 
1127    cursor tax_cursor is
1128           select bc.book_type_code
1129           from fa_book_controls bc
1130           where bc.distribution_source_book = X_book
1131           and bc.book_class = 'TAX'
1132           and bc.date_ineffective is null;
1133 
1134 BEGIN
1135 
1136    open tax_cursor;
1137 
1138    while (X_txn_status) loop
1139       fetch tax_cursor into h_tax_book;
1140       exit when tax_cursor%NOTFOUND;
1141 
1142       --call the cache to put the current tax book info in the record
1143       if (NOT FA_CACHE_PKG.fazcbc(X_book => h_tax_book, p_log_level_rec => p_log_level_rec)) then
1144          fa_srvr_msg.add_sql_error(calling_fn=>'fa_chk_booksts_pkg.faxptb', p_log_level_rec => p_log_level_rec);
1145          return(FALSE);
1146       end if;
1147 
1148       if (NOT faxlck(X_book       => h_tax_book,
1149                      X_txn_status => X_txn_status,
1150                      X_asset_id   => X_asset_id,
1151                      X_trx_type   => X_trx_type,
1152                      p_log_level_rec => p_log_level_rec)) then   -- lock the tax book
1153          fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS.PKG.faxptb', p_log_level_rec => p_log_level_rec);
1154          h_retval := FALSE;
1155          exit;
1156       end if;
1157 
1158 
1159       if (NOT faxcps(X_book         => h_tax_book,
1160                      X_submit       => FALSE,
1161                      X_start        => X_start,
1162                      X_asset_id     => X_asset_id,
1163                      X_trx_type     => X_trx_type,
1164                      X_txn_status   => X_txn_status,
1165                      X_close_period => 0,
1166                      p_log_level_rec => p_log_level_rec)) then
1167          fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxptb', p_log_level_rec => p_log_level_rec);
1168          h_retval := FALSE;
1169          exit;
1170       end if;
1171 
1172       if (p_log_level_rec.statement_level) then
1173          fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxptb','book',h_tax_book, p_log_level_rec => p_log_level_rec);
1174          fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxptb','txn_status',X_txn_status, p_log_level_rec => p_log_level_rec);
1175       end if;
1176    end loop;
1177 
1178    close tax_cursor;
1179    return(h_retval);
1180 
1181 EXCEPTION
1182    WHEN OTHERS THEN
1183         fa_srvr_msg.add_sql_error(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxptb', p_log_level_rec => p_log_level_rec);
1184         close tax_cursor;
1185         return(FALSE);
1186 
1187 END faxptb;
1188 
1189 --
1190 -- FUNCTION faxgcb
1191 --
1192 
1193 FUNCTION faxgcb(X_book         IN     VARCHAR2,
1194                 X_txn_status   IN OUT NOCOPY BOOLEAN, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
1195          return BOOLEAN is
1196 
1197    h_corp_book        VARCHAR2(30);
1198    h_request_id       NUMBER := 0;
1199    h_conc_program_id  NUMBER;
1200    h_conflict         NUMBER;
1201    h_tmp              BOOLEAN;
1202 
1203 BEGIN
1204 
1205    h_corp_book := FA_CACHE_PKG.fazcbc_record.distribution_source_book;
1206 
1207    -- call the cache to put the corp book info in the record
1208    if (NOT FA_CACHE_PKG.fazcbc(X_book => h_corp_book, p_log_level_rec => p_log_level_rec)) then
1209       fa_srvr_msg.add_sql_error(calling_fn=>'fa_chk_booksts_pkg.faxgcb', p_log_level_rec => p_log_level_rec);
1210       return(FALSE);
1211    end if;
1212 
1213    if (NOT faxcms(X_book => h_corp_book,
1214                   X_request_id => h_request_id,
1215                   p_log_level_rec => p_log_level_rec)) then
1216       fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxgcb', p_log_level_rec => p_log_level_rec);
1217       return(FALSE);
1218    end if;
1219 
1220    if (h_request_id <> 0) then
1221 
1222       -- process is ruuning,inactive,pending status
1223 
1224       select re.concurrent_program_id
1225         into h_conc_program_id
1226         from fnd_concurrent_requests re
1227        where re.request_id = h_request_id
1228          and re.program_application_id = 140;
1229 
1230       select decode(pr.concurrent_program_name,
1231                     'FAMTFR',1,
1232                     'FAMAPT',1,
1233                     'GAINLOSS', 1,
1234                     'FAMRCL', 1,
1235                     'FAPPT', 1,
1236                     0)
1237         into h_conflict
1238         from fnd_concurrent_programs pr
1239        where pr.concurrent_program_id = h_conc_program_id
1240          and pr.application_id = 140;
1241 
1242       if (p_log_level_rec.statement_level) then
1243          fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxgcb','book',h_corp_book, p_log_level_rec => p_log_level_rec);
1244          fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxgcb','mass_request_id',
1245                           h_request_id, p_log_level_rec => p_log_level_rec);
1246          fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxgcb','conflict',h_conflict, p_log_level_rec => p_log_level_rec);
1247       end if;
1248 
1249       if (h_conflict <> 0) then  -- curr program either FAMTFR or FAMAPT
1250          fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS.PKG.faxgcb',
1251                                  name       => 'FA_TRXAPP_WAIT_REQUEST',
1252                                  token1     => 'REQUEST_ID',
1253                                  value1     => h_request_id,
1254                                  token2     => 'BOOK',
1255                                  value2     => X_book, p_log_level_rec => p_log_level_rec);
1256          h_tmp := faxsav(X_action     => 'R',
1257                          X_txn_status => X_txn_status,
1258                          p_log_level_rec => p_log_level_rec);
1259          -- rollback the lock, if any
1260       else
1261         X_txn_status := TRUE;
1262       end if;
1263    end if;
1264 
1265    return (TRUE);
1266 
1267 EXCEPTION
1268    WHEN NO_DATA_FOUND then
1269         fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxgcb',
1270                                 name       => 'CONC_MISSING_REQUEST',
1271                                 token1     => 'ROUTINE',value1=>'FA_TRXAPP',
1272                                 token2     => 'REQUEST',value2=>h_request_id, p_log_level_rec => p_log_level_rec);
1273         return(FALSE);
1274 
1275    WHEN OTHERS THEN
1276         fa_srvr_msg.add_sql_error(calling_fn=>'FA_CHK_BOOKSTS_PKG.faxgcb', p_log_level_rec => p_log_level_rec);
1277         return(FALSE);
1278 END faxgcb;
1279 
1280 --
1281 -- FUNCTION faxlck
1282 --
1283 
1284 FUNCTION faxlck(X_book        IN     VARCHAR2,
1285                 X_txn_status  IN OUT NOCOPY BOOLEAN,
1286                 X_asset_id    IN     NUMBER,
1287                 X_trx_type    IN     VARCHAR2, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
1288          return BOOLEAN is
1289 
1290    h_mass_id    NUMBER;
1291    h_tmp        BOOLEAN;
1292    h_trx_id_out NUMBER;
1293 
1294    h_count             number;
1295    h_adj_req_status    VARCHAR2(4);
1296    lock_error          exception;
1297 
1298 
1299    -- CURSOR defined for fix to bug 1067205
1300    -- snarayan
1301    CURSOR lock_asset IS
1302           SELECT transaction_header_id_out,
1303                  adjustment_required_status
1304             FROM fa_books
1305            WHERE book_type_code = X_book
1306              AND asset_id       = X_asset_id
1307              AND transaction_header_id_out is null
1308              FOR UPDATE OF
1309                  transaction_header_id_out
1310           NOWAIT;
1311 
1312    CURSOR lock_asset_mass IS
1313           SELECT transaction_header_id_out,
1314                  adjustment_required_status
1315             FROM fa_books
1316            WHERE book_type_code = X_book
1317              AND asset_id       = X_asset_id
1318              AND transaction_header_id_out is null
1319              FOR UPDATE OF
1320                  transaction_header_id_out;
1321 
1322 BEGIN
1323 
1324    if (X_book is NULL) then  /* Null book_type_code will rollback to savepoint */
1325 
1326       if (NOT faxsav(X_action => 'R',
1327                      X_txn_status => X_txn_status,
1328                      p_log_level_rec => p_log_level_rec)) then
1329           fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxlck', p_log_level_rec => p_log_level_rec);
1330           return(FALSE);
1331       end if;
1332 
1333    -- ******************************************************************
1334    -- Lock Book Controls row only if a Mass Request is submitted.
1335    -- For individual asset transaction only lock the active FA_BOOKS row
1336    -- For ADDITION not necessary to lock row. ADDITION should be allowed
1337    -- as long as depreciation or no mass request is running on the book
1338    -- Fix for Bug 1067205 - snarayan
1339    -- ******************************************************************
1340    elsif (X_trx_type NOT IN ('ADDITION',
1341                              'CIP ADDITION',
1342                              'TRANSFER IN')) then
1343 
1344       if (X_asset_id <> 0) then
1345 
1346          if (X_trx_type = 'GROUP ADDITION') then
1347              -- if it is group addition need to lock if group
1348              -- is in period of addition and backdated transaction
1349              -- results in catchup
1350              select count(*)
1351                into h_count
1352                from fa_asset_history
1353               where asset_id = X_asset_id
1354                 and date_ineffective is null;
1355 
1356              if (h_count = 0) then
1357                 return TRUE;
1358              end if;
1359          end if;
1360 
1361          -- BUG# 3315327
1362          -- conditionally use NOWAIT for non-mass requests
1363          -- wait when it is a mass request to see if we avoid
1364          -- block contention here
1365          if (nvl(fnd_global.conc_request_id, -1) > 1)  then
1366             OPEN lock_asset_mass;
1367             FETCH lock_asset_mass into h_trx_id_out, h_adj_req_status;
1368             CLOSE lock_asset_mass;
1369          else
1370             OPEN lock_asset;
1371             FETCH lock_asset into h_trx_id_out, h_adj_req_status;
1372             CLOSE lock_asset;
1373          end if;
1374 
1375          if h_adj_req_status = 'GADJ' then
1376             raise lock_error;
1377          end if;
1378       else
1379          select mass_request_id
1380            into h_mass_id
1381            from fa_book_controls
1382           where book_type_code = X_book
1383             for update of mass_request_id
1384          NOWAIT;
1385       end if;
1386    end if;
1387 
1388    return(TRUE);
1389 
1390 EXCEPTION
1391    WHEN LOCK_ERROR THEN
1392         fa_srvr_msg.add_sql_error(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxlck', p_log_level_rec => p_log_level_rec);
1393         h_tmp := faxsav(X_action => 'R',
1394                         X_txn_status => X_txn_status,
1395                         p_log_level_rec => p_log_level_rec);
1396         if (X_asset_id <> 0) then
1397            fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxlck',
1398                                    name       => 'FA_TRXAPP_ASSET_LOCK_FAILED',
1399                                    token1     => 'ASSET_ID',
1400                                    value1     => X_asset_id,
1401                                    token2     => 'BOOK',
1402                                    value2     => X_book, p_log_level_rec => p_log_level_rec);
1403         else
1404            fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxlck',
1405                                    name       => 'FA_TRXAPP_LOCK_FAILED',
1406                                    token1     => 'BOOK',
1407                                    value1     => X_book, p_log_level_rec => p_log_level_rec);
1408         end if;
1409 
1410         return(FALSE);
1411 
1412 
1413    WHEN OTHERS THEN
1414         fa_srvr_msg.add_sql_error(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxlck', p_log_level_rec => p_log_level_rec);
1415         h_tmp := faxsav(X_action => 'R',
1416                         X_txn_status => X_txn_status,
1417                         p_log_level_rec => p_log_level_rec);
1418         if (X_asset_id <> 0) then
1419            fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxlck',
1420                                    name       => 'FA_TRXAPP_ASSET_LOCK_FAILED',
1421                                    token1     => 'ASSET_ID',
1422                                    value1     => X_asset_id,
1423                                    token2     => 'BOOK',
1424                                    value2     => X_book, p_log_level_rec => p_log_level_rec);
1425         else
1426            fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxlck',
1427                                    name       => 'FA_TRXAPP_LOCK_FAILED',
1428                                    token1     => 'BOOK',
1429                                    value1     => X_book, p_log_level_rec => p_log_level_rec);
1430         end if;
1431 
1432         return(FALSE);
1433 
1434 END faxlck;
1435 
1436 --
1437 --  Function
1438 --
1439 
1440 FUNCTION faxsav(X_action         IN  VARCHAR2,
1441                 X_txn_status     IN OUT NOCOPY BOOLEAN, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type)
1442          return BOOLEAN is
1443 
1444 BEGIN
1445 
1446    if (X_action = 'S') then
1447       -- sets a savepoint to roll back when anything fails
1448       if (NOT savepoint_set) then
1449          SAVEPOINT lock_row;
1450          savepoint_set := TRUE;
1451          X_txn_status  := TRUE;
1452       end if;
1453 
1454    elsif (X_action = 'R') then  -- rollback locks, error occured
1455       if (savepoint_set) then
1456          ROLLBACK TO SAVEPOINT lock_row;
1457          savepoint_set := FALSE;
1458       end if;
1459       X_txn_status := FALSE;    -- txn not approved
1460 
1461    elsif (X_action = 'C') then  -- txn approved, clear savepoint for next call
1462       savepoint_set := FALSE;   -- make row stay locked
1463 
1464    else
1465       if (savepoint_set) then
1466          ROLLBACK TO SAVEPOINT lock_row;
1467          savepoint_set := FALSE;
1468       end if;
1469       X_txn_status := FALSE;
1470       return(FALSE);
1471 
1472    end if;
1473 
1474    if (p_log_level_rec.statement_level) then
1475       fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxsav','action',X_action, p_log_level_rec => p_log_level_rec);
1476       fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxsav','savepoint',savepoint_set, p_log_level_rec => p_log_level_rec);
1477       fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxsav','txn_status',X_txn_status, p_log_level_rec => p_log_level_rec);
1478    end if;
1479    return (TRUE);
1480 
1481 EXCEPTION
1482    WHEN OTHERS THEN
1483         if (savepoint_set) then
1484            ROLLBACK TO SAVEPOINT lock_row;
1485            savepoint_set := FALSE;
1486         end if;
1487         X_txn_status := FALSE;
1488         return(FALSE);
1489 
1490 END faxsav;
1491 
1492 
1493 FUNCTION faxcdr(X_book          IN      VARCHAR2,
1494                 X_asset_id      IN      NUMBER  DEFAULT 0, p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) return BOOLEAN IS
1495 
1496    deprn_run           VARCHAR2(1);
1497    h_count             NUMBER;
1498    h_mc_source_flag    VARCHAR2(3);
1499    h_set_of_books_id   NUMBER;
1500    h_mrc_sob_type_code VARCHAR2(3);
1501    h_ca_request_id     NUMBER;
1502 
1503    -- variables for api calls
1504    l_msg_count                 NUMBER;
1505    l_msg_data                  VARCHAR2(4000);
1506    l_return_status             VARCHAR2(1);
1507    l_deprn_run                 boolean := false;
1508    l_asset_hdr_rec             FA_API_TYPES.asset_hdr_rec_type;
1509 
1510    CURSOR check_deprn_run IS
1511           SELECT nvl(deprn_run, 'N')
1512             FROM fa_deprn_periods dp,
1513                  fa_book_controls bc
1514            WHERE bc.book_type_code = X_book
1515              AND dp.book_type_code = bc.book_type_code
1516              AND dp.period_close_date is null;
1517 
1518 BEGIN
1519 
1520    -- need to call cache here because of the mass forms call
1521    -- faxcdr directly.
1522 
1523    if (NOT FA_CACHE_PKG.fazcbc(X_book => X_book, p_log_level_rec => p_log_level_rec)) then
1524       fa_srvr_msg.add_sql_error
1525             (calling_fn => 'fa_chk_booksts_pkg.faxcdr', p_log_level_rec => p_log_level_rec);
1526       return (FALSE);
1527    end if;
1528 
1529    h_mc_source_flag       := FA_CACHE_PKG.fazcbc_record.mc_source_flag;
1530    h_set_of_books_id      := FA_CACHE_PKG.fazcbc_record.set_of_books_id;
1531 
1532    if not fa_cache_pkg.fazcsob
1533            (X_set_of_books_id   => h_set_of_books_id,
1534             X_mrc_sob_type_code => h_mrc_sob_type_code, p_log_level_rec => p_log_level_rec) then
1535       fa_srvr_msg.add_sql_error
1536             (calling_fn => 'fa_chk_booksts_pkg.faxcdr', p_log_level_rec => p_log_level_rec);
1537       return(FALSE);
1538    end if;
1539 
1540    if (X_asset_id = 0 and
1541        G_release <> 11) then
1542 
1543       -- SLA: allow mass programs even when deprn has been run
1544       -- we will rollback the processed deprn automatically
1545 
1546       null;
1547 
1548    elsif (X_asset_id = 0 and
1549           G_release = 11) then
1550       OPEN check_deprn_run;
1551       FETCH check_deprn_run INTO deprn_run;
1552       CLOSE check_deprn_run;
1553 
1554       if (deprn_run = 'Y') then
1555          fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcdr',
1556                                  name       => 'FA_DEPRN_ROLLBACK_BOOK',
1557                                  token1     => 'BOOK',
1558                                  value1     => X_book, p_log_level_rec => p_log_level_rec);
1559          return(FALSE);
1560       end if;
1561 
1562       if (h_mc_source_flag = 'Y' and h_mrc_sob_type_code = 'P') then
1563          SELECT count(*)
1564            INTO h_count
1565            FROM fa_mc_deprn_periods dp,
1566                 fa_mc_book_controls bc
1567           WHERE bc.book_type_code = X_book
1568             AND bc.book_type_code = dp.book_type_code
1569             AND bc.set_of_books_id = dp.set_of_books_id
1570             AND bc.enabled_flag = 'Y'
1571             AND dp.period_close_date is null
1572             AND dp.deprn_run = 'Y';
1573 
1574          if (h_count <> 0) then
1575             fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcdr',
1576                                     name       => 'FA_DEPRN_ROLLBACK_BOOK',
1577                                     token1     => 'BOOK',
1578                                     value1     => X_book, p_log_level_rec => p_log_level_rec);
1579             return(FALSE);
1580          end if;
1581       end if;
1582    elsif (G_release = 11) then
1583       SELECT count(*)
1584         INTO h_count
1585         FROM fa_deprn_summary ds,
1586              fa_book_controls bc
1587        WHERE bc.book_type_code = X_book
1588          AND ds.book_type_code = bc.book_type_code
1589          AND ds.period_counter = bc.last_period_counter + 1
1590          AND ds.asset_id = X_asset_id
1591          AND ds.deprn_source_code in ('DEPRN','TRACK');
1592 
1593 
1594       if (h_count <> 0) then
1595           fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcdr',
1596                                   name       => 'FA_DEPRN_ROLLBACK_ASSET',
1597                                   token1     => 'BOOK',
1598                                   value1     => X_book, p_log_level_rec => p_log_level_rec);
1599           return(FALSE);
1600       end if;
1601 
1602       if (h_mc_source_flag = 'Y' and h_mrc_sob_type_code = 'P') then
1603          SELECT count(*)
1604            INTO h_count
1605            FROM fa_mc_deprn_summary ds,
1606                 fa_book_controls bc
1607           WHERE bc.book_type_code = X_book
1608             AND ds.book_type_code = bc.book_type_code
1609             AND ds.period_counter = bc.last_period_counter + 1
1610             AND ds.asset_id = X_asset_id
1611             AND ds.deprn_source_code in ('DEPRN','TRACK');
1612 
1613          if (h_count <> 0) then
1614             fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcdr',
1615                                     name       => 'FA_DEPRN_ROLLBACK_ASSET',
1616                                     token1     => 'BOOK',
1617                                     value1     => X_book, p_log_level_rec => p_log_level_rec);
1618             return(FALSE);
1619          end if;
1620       end if;
1621 
1622       -- Bug 2059859 / 2115351
1623       -- We need to check if the Capitalization/CIP is
1624       -- done in the same period .
1625       SELECT count(*)
1626         INTO h_count
1627         FROM fa_adjustments adj,
1628              fa_book_controls bc,
1629              fa_deprn_summary ds
1630        WHERE bc.book_type_code  = X_book
1631          AND adj.book_type_code = X_book
1632          AND adj.period_counter_created = bc.last_period_counter + 1
1633          AND adj.asset_id = X_asset_id
1634          AND adj.source_type_code in ('ADDITION', 'CIP ADDITION')
1635          AND adj.adjustment_type in ('COST', 'CIP COST')
1636          AND ds.book_type_code = X_book
1637          AND ds.asset_id = X_asset_id
1638          AND ds.deprn_source_code = 'BOOKS'
1639          AND ds.period_counter = bc.last_period_counter;
1640 
1641 
1642       if (h_count <> 0) then
1643          fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcdr',
1644                                  name       => 'FA_DEPRN_ROLLBACK_ASSET',
1645                                  token1     => 'BOOK',
1646                                  value1     => X_book, p_log_level_rec => p_log_level_rec);
1647          return(FALSE);
1648       end if;
1649 
1650       if (h_mc_source_flag = 'Y' and h_mrc_sob_type_code = 'P') then
1651 
1652          SELECT count(*)
1653            INTO h_count
1654            FROM fa_mc_adjustments adj,
1655                 fa_mc_book_controls bc,
1656                 fa_mc_deprn_summary ds
1657           WHERE bc.book_type_code  = X_book
1658             AND bc.enabled_flag = 'Y'
1659             AND adj.set_of_books_id = adj.set_of_books_id
1660             AND adj.book_type_code = X_book
1661             AND adj.period_counter_created = bc.last_period_counter + 1
1662             AND adj.asset_id = X_asset_id
1663             AND adj.source_type_code in ('ADDITION', 'CIP ADDITION')
1664             AND adj.adjustment_type in ('COST', 'CIP COST')
1665             AND ds.set_of_books_id = bc.set_of_books_id
1666             AND ds.book_type_code = X_book
1667             AND ds.asset_id = X_asset_id
1668             AND ds.deprn_source_code = 'BOOKS'
1669             AND ds.period_counter = bc.last_period_counter;
1670 
1671           if (h_count <> 0) then
1672              fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcdr',
1673                                      name       => 'FA_DEPRN_ROLLBACK_ASSET',
1674                                      token1     => 'BOOK',
1675                                      value1     => X_book, p_log_level_rec => p_log_level_rec);
1676              return(FALSE);
1677           end if;
1678 
1679        end if;
1680    else -- release <> 11
1681       SELECT count(*)
1682         INTO h_count
1683         FROM fa_deprn_summary ds,
1684              fa_book_controls bc
1685        WHERE bc.book_type_code = X_book
1686          AND ds.book_type_code = bc.book_type_code
1687          AND ds.period_counter = bc.last_period_counter + 1
1688          AND ds.asset_id = X_asset_id
1689          AND ds.deprn_source_code in ('DEPRN','TRACK');
1690 
1691       if (h_count <> 0) then
1692          l_deprn_run := true;
1693 
1694       else
1695 
1696          -- Fix for Bug #6528245.  Still need check member assets for group.
1697          select count(*)
1698          into   h_count
1699          from   dual
1700          where  exists
1701          (select 'x'
1702           FROM fa_deprn_summary ds,
1703                fa_book_controls bc,
1704                fa_books bks,
1705                fa_books bks2,
1706                fa_additions_b ad
1707          WHERE bc.book_type_code = X_Book
1708            AND ds.book_type_code = bc.book_type_code
1709            AND ds.period_counter = bc.last_period_counter + 1
1710            AND ds.asset_id = bks.asset_id
1711            AND ds.deprn_source_code in ('DEPRN','TRACK')
1712            AND bks.group_asset_id = X_Asset_ID
1713            AND bks.book_type_code = bc.book_type_code
1714            AND bks.transaction_header_id_out is null
1715            AND ad.asset_id = X_Asset_ID
1716            AND ad.asset_type = 'GROUP'
1717            AND bks2.asset_id = ad.asset_id
1718            AND bks2.book_type_code = bc.book_type_code
1719            AND bks2.transaction_header_id_out is null
1720            AND bks2.tracking_method = 'CALCULATE'
1721          );
1722 
1723          if (h_count <> 0) then
1724             l_deprn_run := true;
1725          end if;
1726 
1727       end if;
1728 
1729       if (h_mc_source_flag = 'Y' and h_mrc_sob_type_code = 'P') then
1730          SELECT count(*)
1731            INTO h_count
1732            FROM fa_mc_deprn_summary ds,
1733                 fa_book_controls bc
1734           WHERE bc.book_type_code = X_book
1735             AND ds.book_type_code = bc.book_type_code
1736             AND ds.period_counter = bc.last_period_counter + 1
1737             AND ds.asset_id = X_asset_id
1738             AND ds.deprn_source_code in ('DEPRN','TRACK');
1739 
1740          if (h_count <> 0) then
1741             l_deprn_run := true;
1742          end if;
1743       end if;
1744 
1745       -- SLA: removing period of addition validation
1746       -- adding call to the rollback api so we can automatixally
1747       -- reverse deprn and continue with trx unless error occurs
1748       if (l_deprn_run) then
1749 
1750          l_asset_hdr_rec.asset_id       := X_asset_id;
1751          l_asset_hdr_rec.book_type_code := X_book;
1752 
1753          -- BUG# 5444002
1754          -- only allow automated rollback if create accounting is not currently running
1755 
1756          if(NOT faxcca(X_book       => X_book,
1757                        X_request_id => h_ca_request_id,
1758 	      	 p_log_level_rec => p_log_level_rec)) then
1759               fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps',
1760                                       p_log_level_rec  => p_log_level_rec);
1761               return(FALSE);
1762          end if;
1763 
1764          if (p_log_level_rec.statement_level) then
1765             fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcdr','book',X_book, p_log_level_rec);
1766             fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcdr','create_accounting_request_id',
1767                              h_ca_request_id, p_log_level_rec);
1768          end if;
1769 
1770          if (h_ca_request_id <> 0) then
1771             -- req_id is in status of running,inactive,or pending
1772             fa_srvr_msg.add_message(calling_fn => 'FA_CHK_BOOKSTS_PKG.faxcps',
1773                                     name       => 'FA_TRXAPP_WAIT_REQUEST',
1774                                     token1     => 'REQUEST_ID',
1775                                     value1     => h_ca_request_id,
1776                                     token2     => 'BOOK',
1777                                     value2     => X_book,
1778   			      p_log_level_rec => p_log_level_rec);
1779             return(FALSE);
1780 
1781          else
1782 
1783             FA_DEPRN_ROLLBACK_PUB.do_rollback
1784                (p_api_version             => 1.0,
1785                 p_init_msg_list           => FND_API.G_FALSE,
1786                 p_commit                  => FND_API.G_FALSE,
1787                 p_validation_level        => FND_API.G_VALID_LEVEL_FULL,
1788                 x_return_status           => l_return_status,
1789                 x_msg_count               => l_msg_count,
1790                 x_msg_data                => l_msg_data,
1791                 p_calling_fn              => 'FA_CHK_BOOKSTS_PKG.faxcdr', -- BUG# 8247224
1792                 px_asset_hdr_rec          => l_asset_hdr_rec);
1793 
1794             if (l_return_status <> FND_API.G_RET_STS_SUCCESS) then
1795                 return false;
1796             else
1797                 if (p_log_level_rec.statement_level) then
1798                    fa_debug_pkg.add('FA_CHK_BOOKSTS_PKG.faxcdr',
1799                                      'successfully rolled back deprn','',p_log_level_rec);
1800                 end if;
1801             end if;
1802          end if;
1803       end if;
1804 
1805 
1806    end if;  -- end if asset_id = 0
1807 
1808    return(TRUE);
1809 
1810 EXCEPTION
1811    WHEN OTHERS THEN
1812         fa_srvr_msg.add_sql_error(calling_fn => 'fa_chk_booksts_pkg.faxcdr', p_log_level_rec => p_log_level_rec);
1813         return(FALSE);
1814 END faxcdr;
1815 
1816 -- Bug 9032587: Wrapper procedure to call function faxcdr.
1817 PROCEDURE faxcdr_proc(
1818                 X_book          IN      VARCHAR2,
1819                 X_asset_id      IN      NUMBER DEFAULT 0,
1820                 X_return_value  OUT     nocopy integer)
1821 AS
1822 h_ret_value     boolean;
1823 p_log_level_rec FA_API_TYPES.log_level_rec_type;
1824 ERROR_FOUND     exception;
1825 
1826 begin
1827   X_return_value := 0;
1828 
1829   if (not p_log_level_rec.initialized) then
1830       if (NOT fa_util_pub.get_log_level_rec (
1831                 x_log_level_rec =>  p_log_level_rec
1832       )) then
1833          raise error_found;
1834       end if;
1835   end if;
1836 
1837   h_ret_value := FA_CHK_BOOKSTS_PKG.faxcdr(
1838                     X_book,
1839                     X_asset_id,
1840                     p_log_level_rec);
1841 
1842   if (h_ret_value) then
1843      X_return_value := 1;    /* True  */
1844   else
1845      X_return_value := 0;    /* False  */
1846   end if;
1847 
1848   return;
1849 
1850 EXCEPTION
1851 
1852    WHEN ERROR_FOUND THEN
1853         raise;
1854 
1855    WHEN OTHERS THEN
1856         fa_srvr_msg.add_sql_error(calling_fn => 'fa_chk_booksts_pkg.faxcdr_proc', p_log_level_rec => p_log_level_rec);
1857         X_return_value := 0;
1858         return;
1859 END faxcdr_proc;
1860 -- End Bug 9032587
1861 
1862 END FA_CHK_BOOKSTS_PKG;