DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_GENACCTS_PKG

Source


1 PACKAGE BODY FA_GENACCTS_PKG as
2 /* $Header: fagendab.pls 120.22.12010000.1 2008/07/28 13:21:48 appldev ship $   */
3 
4 
5       G_gen_expense        varchar2(1) := NULL;
6       G_pregen_asset_acct  varchar2(1) := NULL;
7       G_pregen_cat_acct    varchar2(1) := NULL;
8       G_pregen_book_acct   varchar2(1) := NULL;
9       G_FY_first_pc        number      := NULL; --added for BUG# 1339219
10       G_request_id         number;
11 
12       G_success_count NUMBER := 0;
13       G_failure_count NUMBER := 0;
14 
15       TYPE t_number  is table of number index by binary_integer;
16       TYPE t_varchar is table of varchar2(150) index by binary_integer;
17 
18       g_print_debug boolean := fa_cache_pkg.fa_print_debug;
19 
20       g_log_level_rec fa_api_types.log_level_rec_type;
21 
22 
23 PROCEDURE GEN_ACCTS(
24                 p_book_type_code     IN     VARCHAR2,
25                 p_parent_request_id  IN     NUMBER,
26                 p_total_requests     IN     NUMBER,
27                 p_request_number     IN     NUMBER,
28                 x_success_count         OUT NOCOPY number,
29                 x_failure_count         OUT NOCOPY number,
30                 x_return_status         OUT NOCOPY number,
31                 x_worker_jobs           OUT NOCOPY number
32                 ) IS
33 
34    l_batch_size           number;
35    l_dist_source_book     varchar2(15);
36    l_calling_fn           varchar2(40) := 'FA_GENACCTS_PKG.GEN_ACCTS';
37 
38    -- book variables
39    h_default_ccid         number;
40    h_flex_num             number;
41    h_book_class           varchar(30);
42    h_nbv_gain_acct        varchar(30);
43    h_nbv_loss_acct        varchar(30);
44    h_pos_gain_acct        varchar(30);
45    h_pos_loss_acct        varchar(30);
46    h_cor_gain_acct        varchar(30);
47    h_cor_loss_acct        varchar(30);
48    h_cor_clearing_acct    varchar(30);
49    h_pos_clearing_acct    varchar(30);
50    h_deferred_exp_acct    varchar2(30);
51    h_deferred_rsv_acct    varchar2(30);
52    h_reval_rsv_loss_acct  varchar2(30);
53    h_reval_rsv_gain_acct  varchar2(30);
54    h_deprn_adj_acct       varchar2(30);
55    h_allow_reval_flag     varchar2(3);
56    h_allow_deprn_adjust   varchar2(3);
57    h_gl_posting_allowed   varchar2(3);
58 
59 
60    h_result               boolean;
61    h_return_ccid          number;
62    h_total_requests       number;
63    h_request_number       number;
64    status                 boolean;
65    h_submit_child         boolean := FALSE;
66    msg_count              NUMBER := 0;
67    msg_data               varchar2(512);
68    h_req_data             varchar2(10) := NULL;
69 
70    -- new paralism
71    l_unassigned_cnt       NUMBER := 0;
72    l_failed_cnt           NUMBER := 0;
73    l_wip_cnt              NUMBER := 0;
74    l_completed_cnt        NUMBER := 0;
75    l_total_cnt            NUMBER := 0;
76    l_count                NUMBER := 0;
77    l_start_range          NUMBER := 0;
78    l_end_range            NUMBER := 0;
79 
80 
81 
82    -- category_account variables
83    l_acct_ccid            t_number;
84    l_acct_seg             t_varchar;
85    l_reserve_acct         t_varchar;
86    l_cost_acct            t_varchar;
87    l_clearing_acct        t_varchar;
88    l_expense_acct         t_varchar;
89    l_cip_cost_acct        t_varchar;
90    l_cip_clearing_acct    t_varchar;
91    l_cost_ccid            t_number;
92    l_clearing_ccid        t_number;
93    l_reserve_ccid         t_number;
94    l_cip_cost_ccid        t_number;
95    l_cip_clearing_ccid    t_number;
96    l_reval_rsv_acct       t_varchar;
97    l_reval_amort_acct     t_varchar;
98    l_reval_rsv_ccid       t_number;
99    l_reval_amort_ccid     t_number;
100    l_bonus_exp_acct       t_varchar;
101    l_bonus_rsv_acct       t_varchar;
102    l_bonus_rsv_ccid       t_number;
103 
104    --distribution cursor variables
105    l_dist_id              t_number;
106    l_asset_number         t_varchar;
107    l_asset_id             t_number;
108    l_asset_type           t_varchar;
109    l_asset_category_id    t_number;
110    l_dist_ccid            t_number;
111    l_bonus_rule           t_varchar;  -- BUG# 1791317
112    l_group_asset_id       t_number;
113    l_tracking_method      t_varchar;
114 
115    done_exc               exception;
116    error_found            exception;
117 
118  CURSOR dist (p_book_type_code     varchar2,
119               p_dist_source_book   varchar2,
120               p_fy_first_pc        number,
121               p_pregen_asset_acct  varchar2,
122               p_pregen_cat_acct    varchar2,
123               p_pregen_book_acct   varchar2,
124               p_book_class         varchar2,
125               p_gl_posting_allowed varchar2,
126               p_allow_deprn_adjust varchar2,
127               p_allow_reval_flag   varchar2,
128               p_start_range        number,
129               p_end_range          number
130               ) is
131         SELECT  /*  leading(dh) index(dh FA_DISTRIBUTION_HISTORY_U1) */
132                 dh.distribution_id,
133                 ad.asset_number,
134                 ad.asset_id,
135                 ad.asset_type,
136                 ad.asset_category_id,
137                 dh.code_combination_id,
138                 bks.bonus_rule,
139                 bks.group_asset_id,
140                 bks.tracking_method
141         FROM    fa_distribution_accounts da2,
142                 fa_additions_b           ad,
143                 fa_distribution_history  dh,
144                 fa_books                 bks
145         WHERE   dh.date_ineffective is null
146         AND     dh.book_type_code      = p_dist_source_book
147         AND     da2.book_type_code(+)  = p_book_type_code
148         AND     da2.distribution_id(+) = dh.distribution_id
149         AND     ad.asset_id            = dh.asset_id
150                 /* BUG# 1339219: do not select distributions for assets
151                    which have been fully retired in a prior year */
152         AND     bks.asset_id           = dh.asset_id
153         AND     bks.book_type_code     = p_book_type_code
154         AND     bks.transaction_header_id_out is null
155         AND     (bks.period_counter_fully_retired is null OR
156                  bks.period_counter_fully_retired >= p_FY_first_pc)
157                 -- end BUG# 1339219
158         and     dh.distribution_id     between p_start_range and p_end_range
159         AND     (((p_pregen_asset_acct = 'Y') AND
160                    da2.deprn_expense_account_ccid     is NULL) OR
161                  ((p_pregen_cat_acct = 'Y') AND
162                   (((ad.asset_type   <> 'GROUP') AND
163                     (da2.asset_cost_account_ccid       is NULL or
164                      da2.asset_clearing_account_ccid   is NULL)) OR
165                      --da2.deprn_expense_account_ccid is NULL OR
166                    da2.deprn_reserve_account_ccid     is NULL OR
167                    ((ad.asset_type = 'CIP') AND
168                     (da2.cip_cost_account_ccid     is NULL OR
169                      da2.cip_clearing_account_ccid is NULL)) OR
170                    ((p_allow_reval_flag = 'YES' and
171                      ad.asset_type     <> 'GROUP') AND
172                         (da2.reval_amort_account_ccid is NULL OR
173                          da2.reval_rsv_account_ccid   is NULL)) OR
174                    ((bks.bonus_rule is not null ) AND
175                         (da2.bonus_exp_account_ccid   is NULL OR
176                          da2.bonus_rsv_account_ccid   is NULL)))) OR
177                  ((p_pregen_book_acct = 'Y' and
178                    ad.asset_type     <> 'GROUP') AND
179                   (da2.nbv_retired_gain_ccid       is NULL OR
180                    da2.nbv_retired_loss_ccid       is NULL OR
181                    da2.proceeds_sale_gain_ccid     is NULL OR
182                    da2.proceeds_sale_loss_ccid     is NULL OR
183                    da2.cost_removal_gain_ccid      is NULL OR
184                    da2.cost_removal_loss_ccid      is NULL OR
185                    da2.proceeds_sale_clearing_ccid is NULL OR
186                    da2.cost_removal_clearing_ccid  is NULL OR
187                    ((p_book_class = 'TAX') AND
188                     (((da2.deferred_exp_account_ccid is NULL OR
189                        da2.deferred_rsv_account_ccid is NULL)) OR
190                      ((p_allow_deprn_adjust = 'YES') AND
191                        da2.deprn_adj_account_ccid     is NULL)))) OR
192                      ((p_allow_reval_flag = 'YES') AND
193                       (da2.reval_rsv_gain_account_ccid  is NULL OR
194                        da2.reval_rsv_loss_account_ccid  is NULL))));
195 
196 BEGIN
197 
198    G_success_count := 0;
199    G_failure_count := 0;
200    x_success_count := 0;
201    x_failure_count := 0;
202    x_worker_jobs   := 0;
203 
204    if (not g_log_level_rec.initialized) then
205       if (NOT fa_util_pub.get_log_level_rec (
206                 x_log_level_rec =>  g_log_level_rec
207       )) then
208          raise error_found;
209       end if;
210    end if;
211 
212    -- Initialize server message stack and debug
213    FA_DEBUG_PKG.Initialize;
214 
215    -- get book information
216    if not fa_cache_pkg.fazcbc(X_book => p_book_type_code
217 			     ,p_log_level_rec => g_log_level_rec) then
218       raise error_found;
219    end if;
220 
221    l_batch_size  := nvl(fa_cache_pkg.fa_batch_size, 1000);
222    l_dist_source_book := fa_cache_pkg.fazcbc_record.distribution_source_book;
223 
224    fnd_profile.get('FA_GEN_EXPENSE_ACCOUNT', G_gen_expense);
225    fnd_profile.get('FA_PREGEN_ASSET_ACCOUNT', G_pregen_asset_acct);
226    fnd_profile.get('FA_PREGEN_CAT_ACCOUNT', G_pregen_cat_acct);
227    fnd_profile.get('FA_PREGEN_BOOK_ACCOUNT', G_pregen_book_acct);
228 
229    -- if null set accordingly
230    if (G_gen_expense is null) then
231       G_gen_expense := 'N';
232    end if;
233 
234    if (G_pregen_asset_acct is null) then
235       G_pregen_asset_acct := 'Y';
236    end if;
237 
238    if (G_pregen_cat_acct is null) then
239       G_pregen_cat_acct := 'Y';
240    end if;
241 
242    if (G_pregen_book_acct is null) then
243       G_pregen_book_acct:= 'Y';
244    end if;
245 
246    /* Get the first period counter of the current Fiscal Year --BUG# 1339219 */
247    h_default_ccid        := fa_cache_pkg.fazcbc_record.flexbuilder_defaults_ccid;
248    h_flex_num            := fa_cache_pkg.fazcbc_record.accounting_flex_structure;
249    h_book_class          := fa_cache_pkg.fazcbc_record.book_class;
250    h_nbv_gain_acct       := fa_cache_pkg.fazcbc_record.nbv_retired_gain_acct;
251    h_nbv_loss_acct       := fa_cache_pkg.fazcbc_record.nbv_retired_loss_acct;
252    h_pos_gain_acct       := fa_cache_pkg.fazcbc_record.proceeds_of_sale_gain_acct;
253    h_pos_loss_acct       := fa_cache_pkg.fazcbc_record.proceeds_of_sale_loss_acct;
254    h_pos_clearing_acct   := fa_cache_pkg.fazcbc_record.proceeds_of_sale_clearing_acct;
255    h_cor_gain_acct       := fa_cache_pkg.fazcbc_record.cost_of_removal_gain_acct;
256    h_cor_loss_acct       := fa_cache_pkg.fazcbc_record.cost_of_removal_loss_acct;
257    h_cor_clearing_acct   := fa_cache_pkg.fazcbc_record.cost_of_removal_clearing_acct;
258    h_reval_rsv_gain_acct := fa_cache_pkg.fazcbc_record.reval_rsv_retired_gain_acct;
259    h_reval_rsv_loss_acct := fa_cache_pkg.fazcbc_record.reval_rsv_retired_loss_acct;
260    h_deferred_exp_acct   := fa_cache_pkg.fazcbc_record.deferred_deprn_expense_acct;
261    h_deferred_rsv_acct   := fa_cache_pkg.fazcbc_record.deferred_deprn_reserve_acct;
262    h_deprn_adj_acct      := fa_cache_pkg.fazcbc_record.deprn_adjustment_acct;
263    h_allow_reval_flag    := fa_cache_pkg.fazcbc_record.allow_reval_flag;
264    h_allow_deprn_adjust  := fa_cache_pkg.fazcbc_record.allow_deprn_adjustments;
265    h_gl_posting_allowed  := fa_cache_pkg.fazcbc_record.gl_posting_allowed_flag;
266 
267    select dp.period_counter
268      into G_FY_first_pc
269      from fa_deprn_periods dp
270     where dp.book_type_code = p_book_type_code
271       and dp.fiscal_year    = fa_cache_pkg.fazcbc_record.current_fiscal_year
272       and dp.period_num     =
273           (select min(period_num)
274              from fa_deprn_periods dp2
275             where dp2.book_type_code = p_book_type_code
276               and dp2.fiscal_year    = fa_cache_pkg.fazcbc_record.current_fiscal_year);
277 
278    if not fa_cache_pkg.fazcdp
279             (x_book_type_code => p_book_type_code,
280              x_period_counter => null,
281              x_effective_date => null
282              ,p_log_level_rec => g_log_level_rec) then
283      raise error_found;
284    end if;
285 
286    G_validation_date := fa_cache_pkg.fazcdp_record.calendar_period_close_date;
287 
288    -- ------------------------------------------
289    -- Loop thru job list
290    -- -----------------------------------------
291 
292    if g_log_level_rec.statement_level then
293       fa_debug_pkg.add(l_calling_fn, 'looping through: ', 'job list'
294 			,p_log_level_rec => g_log_level_rec);
295    end if;
296 
297 
298 
299    SELECT NVL(sum(decode(status,'UNASSIGNED', 1, 0)),0),
300           NVL(sum(decode(status,'FAILED', 1, 0)),0),
301           NVL(sum(decode(status,'IN PROCESS', 1, 0)),0),
302           NVL(sum(decode(status,'COMPLETED',1 , 0)),0),
303           count(*)
304    INTO   l_unassigned_cnt,
305           l_failed_cnt,
306           l_wip_cnt,
307           l_completed_cnt,
308           l_total_cnt
309    FROM   FA_WORKER_JOBS
310    WHERE  request_id = p_parent_request_id;
311 
312    if g_log_level_rec.statement_level then
313       fa_debug_pkg.add(l_calling_fn, 'Job status - Unassigned: ', l_unassigned_cnt
314 			,p_log_level_rec => g_log_level_rec);
315       fa_debug_pkg.add(l_calling_fn, 'Job status - In Process: ', l_wip_cnt
316 			,p_log_level_rec => g_log_level_rec);
317       fa_debug_pkg.add(l_calling_fn, 'Job status - Completed: ',  l_completed_cnt
318 			,p_log_level_rec => g_log_level_rec);
319       fa_debug_pkg.add(l_calling_fn, 'Job status - Failed: ',     l_failed_cnt
320 			,p_log_level_rec => g_log_level_rec);
321       fa_debug_pkg.add(l_calling_fn, 'Job status - Total: ',      l_total_cnt
322 			,p_log_level_rec => g_log_level_rec);
323    end if;
324 
325    IF (l_failed_cnt > 0) THEN
326       if g_log_level_rec.statement_level then
327         fa_debug_pkg.add(l_calling_fn, 'Another worker has errored out: ', 'Stop processing'
328 			,p_log_level_rec => g_log_level_rec);
329       end if;
330       raise error_found;  -- probably not
331    ELSIF (l_unassigned_cnt = 0) THEN
332       if g_log_level_rec.statement_level then
333          fa_debug_pkg.add(l_calling_fn, 'No more jobs left', 'Terminating.'
334                   ,p_log_level_rec => g_log_level_rec);
335       end if;
336       raise done_exc;
337    ELSIF (l_completed_cnt = l_total_cnt) THEN
338       if g_log_level_rec.statement_level then
339          fa_debug_pkg.add(l_calling_fn, 'All jobs completed, no more jobs. ', 'Terminating'
340                   ,p_log_level_rec => g_log_level_rec);
341       end if;
342       raise done_exc;
343    ELSIF (l_unassigned_cnt > 0) THEN
344       UPDATE FA_WORKER_JOBS
345       SET    status = 'IN PROCESS',
346              worker_num = p_request_number
347       WHERE  status = 'UNASSIGNED'
348       AND    request_id = p_parent_request_id
349       AND    rownum < 2;
350 
351       l_count := sql%rowcount;
352 
353       if g_log_level_rec.statement_level then
354          fa_debug_pkg.add(l_calling_fn, 'Taking job from job queue',  sql%rowcount
355 			,p_log_level_rec => g_log_level_rec);
356       end if;
357 
358       if g_log_level_rec.statement_level then
359          fa_debug_pkg.add(l_calling_fn, 'l_count', l_count
360                   ,p_log_level_rec => g_log_level_rec);
361       end if;
362 
363 
364       x_worker_jobs := l_count;
365 
366       if g_log_level_rec.statement_level then
367          fa_debug_pkg.add(l_calling_fn, 'x_worker_jobs',  x_worker_jobs
368                   ,p_log_level_rec => g_log_level_rec);
369       end if;
370 
371       COMMIT;
372    END IF;
373 
374    -- -----------------------------------
375    -- There could be rare situations where
376    -- between Section 30 and Section 50
377    -- the unassigned job gets taken by
378    -- another worker.  So, if unassigned
379    -- job no longer exist.  Do nothing.
380    -- -----------------------------------
381       if g_log_level_rec.statement_level then
382          fa_debug_pkg.add(l_calling_fn, 'before entering', 'main logic'
383                   ,p_log_level_rec => g_log_level_rec);
384 
385      end if;
386    IF (l_count > 0) THEN
387 
388       if g_log_level_rec.statement_level then
389          fa_debug_pkg.add(l_calling_fn, 'entering', 'main logic'
390                   ,p_log_level_rec => g_log_level_rec);
391       end if;
392 
393       DECLARE
394       BEGIN
395 
396          if g_log_level_rec.statement_level then
397             fa_debug_pkg.add(l_calling_fn, 'selecting', 'ranges'
398                         ,p_log_level_rec => g_log_level_rec);
399          end if;
400 
401          SELECT start_range,
402                 end_range
403            INTO l_start_range,
404                 l_end_range
405            FROM FA_WORKER_JOBS
406           WHERE request_id = p_parent_request_id
407             AND worker_num = p_request_number
408             AND  status = 'IN PROCESS';
409 
410          if g_log_level_rec.statement_level then
411             fa_debug_pkg.add(l_calling_fn, 'opening cursor', 'dist'
412                         ,p_log_level_rec => g_log_level_rec);
413          end if;
414 
415          OPEN dist
416              (p_book_type_code     => p_book_type_code,
417               p_dist_source_book   => fa_cache_pkg.fazcbc_record.distribution_source_book,
418               p_fy_first_pc        => G_fy_first_pc,
419               p_pregen_asset_acct  => G_pregen_asset_acct,
420               p_pregen_cat_acct    => G_pregen_cat_acct,
421               p_pregen_book_acct   => G_pregen_book_acct,
422               p_book_class         => h_book_class,
423               p_gl_posting_allowed => h_gl_posting_allowed,
424               p_allow_deprn_adjust => h_allow_deprn_adjust,
425               p_allow_reval_flag   => h_allow_reval_flag,
426               p_start_range        => l_start_range,
427               p_end_range          => l_end_range);
428 
429          if g_log_level_rec.statement_level then
430             fa_debug_pkg.add(l_calling_fn, 'fecthing cursor', 'dist'
431                         ,p_log_level_rec => g_log_level_rec);
432          end if;
433 
434          FETCH dist bulk collect
435           into l_dist_id,
436                l_asset_number,
437                l_asset_id,
438                l_asset_type,
439                l_asset_category_id,
440                l_dist_ccid,
441                l_bonus_rule,
442                l_group_asset_id,
443                l_tracking_method;
444 
445          if g_log_level_rec.statement_level then
446             fa_debug_pkg.add(l_calling_fn, 'closing cursor', 'dist'
447                         ,p_log_level_rec => g_log_level_rec);
448          end if;
449 
450          close dist;
451 /*
452          if (l_dist_id.count = 0) then
453             raise done_exc;
454          end if;
455 */
456 
457          -- load the category values into the struct
458          if (g_pregen_cat_acct   = 'Y' or
459              g_pregen_asset_acct = 'Y') then
460 
461             if g_log_level_rec.statement_level then
462                fa_debug_pkg.add(l_calling_fn, 'processing ', 'cat and asset'
463                               ,p_log_level_rec => g_log_level_rec);
464             end if;
465 
466             for i in 1..l_dist_id.count loop
467 
468                if not (fa_cache_pkg.fazccb(X_book   => p_book_type_code,
469                                            X_cat_id => l_asset_category_id(i))) then
470                   raise error_found;
471                end if;
472 
473                l_cost_acct(i)         := fa_cache_pkg.fazccb_record.asset_cost_acct;
474                l_clearing_acct(i)     := fa_cache_pkg.fazccb_record.asset_clearing_acct;
475                l_cost_ccid(i)         := fa_cache_pkg.fazccb_record.asset_cost_account_ccid;
476                l_clearing_ccid(i)     := fa_cache_pkg.fazccb_record.asset_clearing_account_ccid;
477                l_reserve_acct(i)      := fa_cache_pkg.fazccb_record.deprn_reserve_acct;
478                l_reserve_ccid(i)      := fa_cache_pkg.fazccb_record.reserve_account_ccid;
479                l_expense_acct(i)      := fa_cache_pkg.fazccb_record.deprn_expense_acct;
480                l_cip_cost_acct(i)     := fa_cache_pkg.fazccb_record.cip_cost_acct;
481                l_cip_clearing_acct(i) := fa_cache_pkg.fazccb_record.cip_clearing_acct;
482                l_cip_cost_ccid(i)     := fa_cache_pkg.fazccb_record.wip_cost_account_ccid;
483                l_cip_clearing_ccid(i) := fa_cache_pkg.fazccb_record.wip_clearing_account_ccid;
484                l_reval_amort_acct(i)  := fa_cache_pkg.fazccb_record.reval_amortization_acct;
485                l_reval_amort_ccid(i)  := fa_cache_pkg.fazccb_record.reval_amort_account_ccid;
486                l_reval_rsv_acct(i)    := fa_cache_pkg.fazccb_record.reval_reserve_acct;
487                l_reval_rsv_ccid(i)    := fa_cache_pkg.fazccb_record.reval_reserve_account_ccid;
488                l_bonus_exp_acct(i)    := fa_cache_pkg.fazccb_record.bonus_deprn_expense_acct;
489                l_bonus_rsv_acct(i)    := fa_cache_pkg.fazccb_record.bonus_deprn_reserve_acct;
490                l_bonus_rsv_ccid(i)    := fa_cache_pkg.fazccb_record.bonus_reserve_acct_ccid;
491 
492             end loop;
493 
494          else
495 
496             if g_log_level_rec.statement_level then
497                fa_debug_pkg.add(l_calling_fn, 'skipping ', 'cat and asset'
498                               ,p_log_level_rec => g_log_level_rec);
499             end if;
500 
501             -- BUG# 3280298
502             -- need to load the table values here with null if we're not
503             -- generating the category accounts to avoid 1403
504 
505             for i in 1..l_dist_id.count loop
506 
507                l_cost_acct(i)         := null;
508                l_clearing_acct(i)     := null;
509                l_cost_ccid(i)         := null;
510                l_clearing_ccid(i)     := null;
511                l_reserve_acct(i)      := null;
512                l_reserve_ccid(i)      := null;
513                l_expense_acct(i)      := null;
514                l_cip_cost_acct(i)     := null;
515                l_cip_clearing_acct(i) := null;
516                l_cip_cost_ccid(i)     := null;
517                l_cip_clearing_ccid(i) := null;
518                l_reval_amort_acct(i)  := null;
519                l_reval_amort_ccid(i)  := null;
520                l_reval_rsv_acct(i)    := null;
521                l_reval_rsv_ccid(i)    := null;
522                l_bonus_exp_acct(i)    := null;
523                l_bonus_rsv_acct(i)    := null;
524                l_bonus_rsv_ccid(i)    := null;
525 
526             end loop;
527          end if;
528 
529          if g_log_level_rec.statement_level then
530             fa_debug_pkg.add(l_calling_fn, 'looping and calling', 'GEN_CCID'
531                         ,p_log_level_rec => g_log_level_rec);
532          end if;
533 
534          for i in 1..l_dist_id.count loop
535 
536              -- clear the debug stack for each line
537              -- FA_DEBUG_PKG.Initialize;
538              -- reset the message level to prevent bogus errors
539              FA_SRVR_MSG.Set_Message_Level(message_level => 10);
540 
541              GEN_CCID(
542                      X_book_type_code      => p_book_type_code,
543                      X_flex_num            => h_flex_num,
544                      X_default_ccid        => h_default_ccid,
545                      X_book_class          => h_book_class,
546                      X_nbv_gain_acct       => h_nbv_gain_acct,
547                      X_nbv_loss_acct       => h_nbv_loss_acct,
548                      X_pos_gain_acct       => h_pos_gain_acct,
549                      X_pos_loss_acct       => h_pos_loss_acct,
550                      X_cor_gain_acct       => h_cor_gain_acct,
551                      X_cor_loss_acct       => h_cor_loss_acct,
552                      X_cor_clearing_acct   => h_cor_clearing_acct,
553                      X_pos_clearing_acct   => h_pos_clearing_acct,
554                      X_reval_rsv_gain_acct => h_reval_rsv_gain_acct,
555                      X_reval_rsv_loss_acct => h_reval_rsv_loss_acct,
556                      X_deferred_exp_acct   => h_deferred_exp_acct,
557                      X_deferred_rsv_acct   => h_deferred_rsv_acct,
558                      X_deprn_adj_acct      => h_deprn_adj_acct,
559                      X_allow_reval_flag    => h_allow_reval_flag,
560                      X_allow_deprn_adjust  => h_allow_deprn_adjust,
561                      X_gl_posting_allowed  => h_gl_posting_allowed,
562 
563                      X_asset_number        => l_asset_number(i),
564                      X_asset_id            => l_asset_id(i),
565                      X_dist_ccid           => l_dist_ccid(i),
566                      X_reserve_acct        => l_reserve_acct(i),
567                      X_cost_acct           => l_cost_acct(i),
568                      X_clearing_acct       => l_clearing_acct(i),
569                      X_expense_acct        => l_expense_acct(i),
570                      X_cip_cost_acct       => l_cip_cost_acct(i),
571                      X_cip_clearing_acct   => l_cip_clearing_acct(i),
572                      X_cost_ccid           => l_cost_ccid(i),
573                      X_clearing_ccid       => l_clearing_ccid(i),
574                      X_reserve_ccid        => l_reserve_ccid(i),
575                      X_distribution_id     => l_dist_id(i),
576                      X_cip_cost_ccid       => l_cip_cost_ccid(i),
577                      X_cip_clearing_ccid   => l_cip_clearing_ccid(i),
578                      X_asset_type          => l_asset_type(i),
579                      X_reval_amort_acct    => l_reval_amort_acct(i),
580                      X_reval_amort_ccid    => l_reval_amort_ccid(i),
581                      X_reval_rsv_acct      => l_reval_rsv_acct(i),
582                      X_reval_rsv_ccid      => l_reval_rsv_ccid(i),
583                      X_bonus_exp_acct      => l_bonus_exp_acct(i),
584                      X_bonus_rsv_acct      => l_bonus_rsv_acct(i),
585                      X_bonus_rsv_ccid      => l_bonus_rsv_ccid(i),
586                      X_bonus_rule          => l_bonus_rule(i),   -- BUG# 1791317
587                      X_group_asset_id      => l_group_asset_id(i),
588                      X_tracking_method     => l_tracking_method(i),
589                      p_log_level_rec       => g_log_level_rec
590                     );
591 
592          END LOOP; -- bulk update loop
593 
594          if g_log_level_rec.statement_level then
595             fa_debug_pkg.add(l_calling_fn, 'updating', 'worker jobs'
596                         ,p_log_level_rec => g_log_level_rec);
597          end if;
598 
599          UPDATE FA_WORKER_JOBS
600             SET status     = 'COMPLETED'
601           WHERE request_id = p_parent_request_id
602             AND worker_num = p_request_number
603             AND status     = 'IN PROCESS';
604 
605          COMMIT;
606 
607          --   Handle any exception that occured during
608          --   your child process
609 
610       EXCEPTION
611          WHEN OTHERS THEN
612 
613               FA_SRVR_MSG.ADD_SQL_ERROR(
614                  CALLING_FN => 'FA_GENACCTS_PKG.gen_accts'
615                  ,p_log_level_rec => g_log_level_rec);
616 
617               UPDATE FA_WORKER_JOBS
618                  SET status     = 'FAILED'
619                WHERE request_id = p_parent_request_id
620                  AND worker_num = p_request_number
621                  AND status     = 'IN PROCESS';
622 
623               COMMIT;
624               Raise error_found;
625 
626       END;  -- block
627 
628    END IF; /* IF (l_count> 0) */
629 
630 
631    -- using these as dummys - leave as zero when we've done nothing
632    x_success_count := G_success_count;
633    x_failure_count := G_failure_count;
634 
635 -- Commented for bugfix 4672237
636 --   if (g_log_level_rec.statement_level) then
637 --       fa_debug_pkg.dump_debug_messages(max_mesgs => 0);
638 --   end if;
639 
640    x_return_status := 0;
641 
642 EXCEPTION
643    WHEN done_exc then
644         x_success_count := G_success_count;
645         x_failure_count := G_failure_count;
646 
647 -- Commented for bugfix 4672237
648 --        if (g_log_level_rec.statement_level) then
649 --           fa_debug_pkg.dump_debug_messages(max_mesgs => 0);
650 --        end if;
651 
652         x_return_status := 0;
653 
654    WHEN error_found then
655         x_success_count := G_success_count;
656         x_failure_count := G_failure_count;
657         fa_srvr_msg.add_message(calling_fn => 'fa_genaccts_pkg.gen_accts'
658                 ,p_log_level_rec => g_log_level_rec);
659 
660 -- Commented for bugfix 4672237
661 --        if (g_log_level_rec.statement_level) then
662 --           fa_debug_pkg.dump_debug_messages(max_mesgs => 0);
663 --        end if;
664         x_return_status := 2;
665 
666    WHEN OTHERS THEN
667         x_success_count := G_success_count;
668         x_failure_count := G_failure_count;
669         FA_SRVR_MSG.ADD_SQL_ERROR(
670            CALLING_FN => 'FA_GENACCTS_PKG.gen_accts'
671            ,p_log_level_rec => g_log_level_rec);
672 
673 -- Commented for bugfix 4672237
674 --        if (g_log_level_rec.statement_level) then
675 --           fa_debug_pkg.dump_debug_messages(max_mesgs => 0);
676 --        end if;
677         x_return_status := 2;
678 
679 END GEN_ACCTS;
680 
681 -----------------------------------------------------------------------
682 
683 PROCEDURE GEN_CCID (
684           X_book_type_code        in    varchar2,
685           X_flex_num              in    number,
686           X_dist_ccid             in    number,
687           X_asset_number          in    varchar2,
688           X_asset_id              in    number,
689           X_reserve_acct          in    varchar2,
690           X_cost_acct             in    varchar2,
691           X_clearing_acct         in    varchar2,
692           X_expense_acct          in    varchar2,
693           X_cip_cost_acct         in    varchar2,
694           X_cip_clearing_acct     in    varchar2,
695           X_default_ccid          in    number,
696           X_cost_ccid             in    number,
697           X_clearing_ccid         in    number,
698           X_reserve_ccid          in    number,
699           X_distribution_id       in    number,
700           X_cip_cost_ccid         in    number,
701           X_cip_clearing_ccid     in    number,
702           X_asset_type            in    varchar2,
703           X_book_class            in    varchar2,
704           X_nbv_gain_acct         in    varchar2,
705           X_nbv_loss_acct         in    varchar2,
706           X_pos_gain_acct         in    varchar2,
707           X_pos_loss_acct         in    varchar2,
708           X_cor_gain_acct         in    varchar2,
709           X_cor_loss_acct         in    varchar2,
710           X_cor_clearing_acct     in    varchar2,
711           X_pos_clearing_acct     in    varchar2,
712           X_reval_rsv_gain_acct   in    varchar2,
713           X_reval_rsv_loss_acct   in    varchar2,
714           X_deferred_exp_acct     in    varchar2,
715           X_deferred_rsv_acct     in    varchar2,
716           X_deprn_adj_acct        in    varchar2,
717           X_reval_amort_acct      in    varchar2,
718           X_reval_amort_ccid      in    number,
719           X_reval_rsv_acct        in    varchar2,
720           X_reval_rsv_ccid        in    number,
721           X_bonus_exp_acct        in    varchar2,
722           X_bonus_rsv_acct        in    varchar2,
723           X_bonus_rsv_ccid        in    number,
724           X_allow_reval_flag      in    varchar2,
725           X_allow_deprn_adjust    in    varchar2,
726           X_gl_posting_allowed    in    varchar2,
727           X_bonus_rule            in    varchar2,
728           X_group_asset_id        in    number,
729           X_tracking_method       in    varchar2
730           ,p_log_level_rec           IN     FA_API_TYPES.log_level_rec_type default null) IS
731 
732    result                 boolean;
733    h_count                number;
734    h_return_ccid          number;
735    h_acct_ccid            number;
736    h_acct_seg             varchar2(30);
737    h_flex_account_type    varchar2(30);
738 
739    h_cost_acct_ccid       number := NULL;
740    h_clearing_acct_ccid   number := NULL;
741    h_reserve_acct_ccid    number := NULL;
742    h_expense_acct_ccid    number := NULL;
743    h_cip_cost_ccid        number := NULL;
744    h_cip_clearing_ccid    number := NULL;
745    h_nbv_gain_ccid        number := NULL;
746    h_nbv_loss_ccid        number := NULL;
747    h_pos_gain_ccid        number := NULL;
748    h_pos_loss_ccid        number := NULL;
749    h_cor_gain_ccid        number := NULL;
750    h_cor_loss_ccid        number := NULL;
751    h_cor_clearing_ccid    number := NULL;
752    h_pos_clearing_ccid    number := NULL;
753 
754    h_deferred_exp_ccid    number := NULL;
755    h_deferred_rsv_ccid    number := NULL;
756    h_reval_rsv_loss_ccid  number := NULL;
757    h_reval_rsv_gain_ccid  number := NULL;
758    h_deprn_adj_ccid       number := NULL;
759    h_reval_rsv_ccid       number := NULL;
760    h_reval_amort_ccid     number := NULL;
761    h_bonus_exp_ccid       number := NULL;
762    h_bonus_rsv_ccid       number := NULL;
763 
764    found                  boolean := FALSE;
765 
766    h_user_id              number;
767    h_login_id             number;
768 
769    CURSOR get_ccids IS
770      SELECT ASSET_COST_ACCOUNT_CCID,
771             ASSET_CLEARING_ACCOUNT_CCID,
772             DEPRN_EXPENSE_ACCOUNT_CCID,
773             DEPRN_RESERVE_ACCOUNT_CCID,
774             CIP_COST_ACCOUNT_CCID,
775             CIP_CLEARING_ACCOUNT_CCID,
776             NBV_RETIRED_GAIN_CCID,
777             NBV_RETIRED_LOSS_CCID,
778             PROCEEDS_SALE_GAIN_CCID,
779             PROCEEDS_SALE_LOSS_CCID,
780             COST_REMOVAL_GAIN_CCID,
781             COST_REMOVAL_LOSS_CCID,
782             PROCEEDS_SALE_CLEARING_CCID,
783             COST_REMOVAL_CLEARING_CCID,
784             reval_rsv_gain_account_ccid,
785             reval_rsv_loss_account_ccid,
786             deferred_exp_account_ccid,
787             deferred_rsv_account_ccid,
788             deprn_adj_account_ccid,
789             reval_amort_account_ccid,
790             reval_rsv_account_ccid,
791             bonus_exp_account_ccid,
792             bonus_rsv_account_ccid
793        FROM FA_DISTRIBUTION_ACCOUNTS
794       WHERE BOOK_TYPE_CODE  = X_book_type_code
795         AND DISTRIBUTION_ID = X_distribution_id;
796 
797 BEGIN
798 
799       FA_SRVR_MSG.ADD_MESSAGE
800               (CALLING_FN => NULL,
801                NAME       => 'FA_FAGDA_DISTRIBUTION_ID',
802                TOKEN1     => 'DISTRIBUTION_ID',
803                VALUE1     => X_distribution_id
804                ,p_log_level_rec => p_log_level_rec);
805 
806       open get_ccids;
807       fetch get_ccids
808        into h_cost_acct_ccid,
809             h_clearing_acct_ccid,
810             h_expense_acct_ccid,
811             h_reserve_acct_ccid,
812             h_cip_cost_ccid,
813             h_cip_clearing_ccid,
814             h_nbv_gain_ccid,
815             h_nbv_loss_ccid,
816             h_pos_gain_ccid,
817             h_pos_loss_ccid,
818             h_cor_gain_ccid,
819             h_cor_loss_ccid,
820             h_pos_clearing_ccid,
821             h_cor_clearing_ccid,
822             h_reval_rsv_loss_ccid,
823             h_reval_rsv_gain_ccid,
824             h_deferred_exp_ccid,
825             h_deferred_rsv_ccid,
826             h_deprn_adj_ccid,
827             h_reval_amort_ccid,
828             h_reval_rsv_ccid,
829             h_bonus_exp_ccid,
830             h_bonus_rsv_ccid;
831 
832       if (get_ccids%NOTFOUND) then
833          found := FALSE;
834       else
835          found := TRUE;
836       end if;
837 
838       if (G_pregen_asset_acct = 'Y') then
839          if (((found and h_expense_acct_ccid is null) OR (not found)) AND
840              ((X_group_asset_id  is null) or
841               (X_group_asset_id  is not null and
842                X_tracking_method is not null))) then
843             /* Generate DEPRN_EXPENSE_ACCT */
844             if (G_gen_expense = 'Y') then
845                h_acct_seg          := X_expense_acct;
846                h_acct_ccid         := X_dist_ccid;
847                h_flex_account_type := 'DEPRN_EXP';
848                result := FAFLEX_PKG_WF.START_PROCESS(
849                                 X_flex_account_type => h_flex_account_type,
850                                 X_book_type_code    => X_book_type_code,
851                                 X_flex_num          => X_flex_num,
852                                 X_dist_ccid         => X_dist_ccid,
853                                 X_acct_segval       => h_acct_seg,
854                                 X_default_ccid      => X_default_ccid,
855                                 X_account_ccid      => h_acct_ccid,
856                                 X_distribution_id   => X_distribution_id,
857                                 X_validation_date   => G_validation_date,
858                                 X_return_ccid       => h_return_ccid
859                                 ,p_log_level_rec => p_log_level_rec);
860                if (result) then
861                    h_expense_acct_ccid := h_return_ccid;
862                    G_success_count := G_success_count + 1;
863                else
864                    Add_Messages(
865                       X_asset_number,
866                       X_asset_id,
867                       h_acct_ccid,
868                       h_acct_seg,
869                       h_flex_account_type,
870                       X_book_type_code,
871                       X_default_ccid,
872                       X_dist_ccid
873  	        	,p_log_level_rec => p_log_level_rec);
874                end if;
875             else
876                h_expense_acct_ccid := X_dist_ccid;
877             end if;
878          end if;
879       end if;
880 
881       if (G_pregen_cat_acct = 'Y') then
882          if (X_asset_type <> 'GROUP') then -- only prevents cost and clearing
883             if ((found and h_cost_acct_ccid is null) OR (not found)) then
884                /* Generate COST Account  */
885                h_acct_seg          := X_cost_acct;
886                h_acct_ccid         := X_cost_ccid;
887                h_flex_account_type := 'ASSET_COST';
888                result := FAFLEX_PKG_WF.START_PROCESS(
889                     X_flex_account_type => h_flex_account_type,
890                     X_book_type_code    => X_book_type_code,
891                     X_flex_num          => X_flex_num,
892                     X_dist_ccid         => X_dist_ccid,
893                     X_acct_segval       => h_acct_seg,
894                     X_default_ccid      => X_default_ccid,
895                     X_account_ccid      => h_acct_ccid,
896                     X_distribution_id   => X_distribution_id,
897                     X_validation_date   => G_validation_date,
898                     X_return_ccid       => h_return_ccid
899                     ,p_log_level_rec => p_log_level_rec);
900                if (result) then
901                   G_success_count := G_success_count + 1;
902                   h_cost_acct_ccid := h_return_ccid;
903                else
904                   Add_Messages(
905                      X_asset_number,
906                      X_asset_id,
907                      h_acct_ccid,
908                      h_acct_seg,
909                      h_flex_account_type,
910                      X_book_type_code,
911                      X_default_ccid,
912                      X_dist_ccid
913                      ,p_log_level_rec => p_log_level_rec);
914                end if;
915             end if;
916 
917             if ((found and h_clearing_acct_ccid is null) OR (not found)) then
918                /* Generate Cost Clearing account */
919                h_acct_seg          := X_clearing_acct;
920                h_acct_ccid         := X_clearing_ccid;
921                h_flex_account_type := 'ASSET_CLEARING';
922                result := FAFLEX_PKG_WF.START_PROCESS(
923                                 X_flex_account_type => h_flex_account_type,
924                                 X_book_type_code    => X_book_type_code,
925                                 X_flex_num          => X_flex_num,
926                                 X_dist_ccid         => X_dist_ccid,
927                                 X_acct_segval       => h_acct_seg,
928                                 X_default_ccid      => X_default_ccid,
929                                 X_account_ccid      => h_acct_ccid,
930                                 X_distribution_id   => X_distribution_id,
931                                 X_validation_date   => G_validation_date,
932                                 X_return_ccid       => h_return_ccid
933                                 ,p_log_level_rec => p_log_level_rec);
934                if (result) then
935                   G_success_count := G_success_count + 1;
936                   h_clearing_acct_ccid := h_return_ccid;
937                else
938                   Add_Messages(
939                      X_asset_number,
940                      X_asset_id,
941                      h_acct_ccid,
942                      h_acct_seg,
943                      h_flex_account_type,
944                      X_book_type_code,
945                      X_default_ccid,
946                      X_dist_ccid
947                      ,p_log_level_rec => p_log_level_rec);
948                end if;
949             end if;
950          end if; -- group
951 
952          if ((found and h_reserve_acct_ccid is null) OR (not found)) then
953             /* Generate DEPRN_RESERVE_ACCT */
954             h_acct_seg := X_reserve_acct;
955             h_acct_ccid := X_reserve_ccid;
956             h_flex_account_type := 'DEPRN_RSV';
957             result := FAFLEX_PKG_WF.START_PROCESS(
958                                 X_flex_account_type => h_flex_account_type,
959                                 X_book_type_code    => X_book_type_code,
960                                 X_flex_num          => X_flex_num,
961                                 X_dist_ccid         => X_dist_ccid,
962                                 X_acct_segval       => h_acct_seg,
963                                 X_default_ccid      => X_default_ccid,
964                                 X_account_ccid      => h_acct_ccid,
965                                 X_distribution_id   => X_distribution_id,
966                                 X_validation_date   => G_validation_date,
967                                 X_return_ccid       => h_return_ccid
968                                 ,p_log_level_rec => p_log_level_rec);
969             if (result) then
970                G_success_count := G_success_count + 1;
971                h_reserve_acct_ccid := h_return_ccid;
972             else
973                Add_Messages(
974                    X_asset_number,
975                    X_asset_id,
976                    h_acct_ccid,
977                    h_acct_seg,
978                    h_flex_account_type,
979                    X_book_type_code,
980                    X_default_ccid,
981                    X_dist_ccid
982                    ,p_log_level_rec => p_log_level_rec);
983             end if;
984          end if;
985 
986          if (X_asset_type = 'CIP') then
987 
988             if ((found and h_cip_cost_ccid is null) OR (not found)) then
989             /* Generate CIP COST account */
990                h_acct_seg          := X_cip_cost_acct;
991                h_acct_ccid         := X_cip_cost_ccid;
992                h_flex_account_type := 'CIP_COST';
993                result := FAFLEX_PKG_WF.START_PROCESS(
994                                 X_flex_account_type => h_flex_account_type,
995                                 X_book_type_code    => X_book_type_code,
996                                 X_flex_num          => X_flex_num,
997                                 X_dist_ccid         => X_dist_ccid,
998                                 X_acct_segval       => h_acct_seg,
999                                 X_default_ccid      => X_default_ccid,
1000                                 X_account_ccid      => h_acct_ccid,
1001                                 X_distribution_id   => X_distribution_id,
1002                                 X_validation_date   => G_validation_date,
1003                                 X_return_ccid       => h_return_ccid
1004                                 ,p_log_level_rec => p_log_level_rec);
1005                if (result) then
1006                   G_success_count := G_success_count + 1;
1007                   h_cip_cost_ccid := h_return_ccid;
1008                else
1009                   Add_Messages(
1010                       X_asset_number,
1011                       X_asset_id,
1012                       h_acct_ccid,
1013                       h_acct_seg,
1014                       h_flex_account_type,
1015                       X_book_type_code,
1016                       X_default_ccid,
1017                       X_dist_ccid
1018                       ,p_log_level_rec => p_log_level_rec);
1019                end if;
1020             end if;
1021 
1022             if ((found and h_cip_clearing_ccid is null) OR (not found)) then
1023                /* Generate CIP CLEARING account */
1024                h_acct_seg          := X_cip_clearing_acct;
1025                h_acct_ccid         := X_cip_clearing_ccid;
1026                h_flex_account_type := 'CIP_CLEARING';
1027                result := FAFLEX_PKG_WF.START_PROCESS(
1028                                 X_flex_account_type => h_flex_account_type,
1029                                 X_book_type_code    => X_book_type_code,
1030                                 X_flex_num          => X_flex_num,
1031                                 X_dist_ccid         => X_dist_ccid,
1032                                 X_acct_segval       => h_acct_seg,
1033                                 X_default_ccid      => X_default_ccid,
1034                                 X_account_ccid      => h_acct_ccid,
1035                                 X_distribution_id   => X_distribution_id,
1036                                 X_validation_date   => G_validation_date,
1037                                 X_return_ccid       => h_return_ccid
1038                                 ,p_log_level_rec => p_log_level_rec);
1039                if (result) then
1040                   G_success_count := G_success_count + 1;
1041                   h_cip_clearing_ccid := h_return_ccid;
1042                else
1043                   Add_Messages(
1044                       X_asset_number,
1045                       X_asset_id,
1046                       h_acct_ccid,
1047                       h_acct_seg,
1048                       h_flex_account_type,
1049                       X_book_type_code,
1050                       X_default_ccid,
1051                       X_dist_ccid
1052                       ,p_log_level_rec => p_log_level_rec);
1053                end if;
1054             end if;
1055          end if; -- asset_type CIP
1056 
1057          /* BUG# 1553682 */
1058          if (X_allow_reval_flag = 'YES' and
1059              X_asset_type      <> 'GROUP' AND
1060              X_group_asset_id  is null) then
1061 
1062             if ((found and h_reval_amort_ccid is null) OR (not found)) then
1063                /* Generate REVAL AMORT account */
1064                h_acct_seg          := X_reval_amort_acct;
1065                h_acct_ccid         := X_reval_amort_ccid;
1066                h_flex_account_type := 'REV_AMORT';
1067                result := FAFLEX_PKG_WF.START_PROCESS(
1068                                 X_flex_account_type => h_flex_account_type,
1069                                 X_book_type_code    => X_book_type_code,
1070                                 X_flex_num          => X_flex_num,
1071                                 X_dist_ccid         => X_dist_ccid,
1072                                 X_acct_segval       => h_acct_seg,
1073                                 X_default_ccid      => X_default_ccid,
1074                                 X_account_ccid      => h_acct_ccid,
1075                                 X_distribution_id   => X_distribution_id,
1076                                 X_validation_date   => G_validation_date,
1077                                 X_return_ccid       => h_return_ccid
1078                                 ,p_log_level_rec => p_log_level_rec);
1079                if (result) then
1080                   G_success_count := G_success_count + 1;
1081                   h_reval_amort_ccid := h_return_ccid;
1082                else
1083                   Add_Messages(
1084                       X_asset_number,
1085                       X_asset_id,
1086                       h_acct_ccid,
1087                       h_acct_seg,
1088                       h_flex_account_type,
1089                       X_book_type_code,
1090                       X_default_ccid,
1091                       X_dist_ccid
1092                       ,p_log_level_rec => p_log_level_rec);
1093                end if;
1094             end if;
1095 
1096 
1097             if ((found and h_reval_rsv_ccid is null) OR (not found)) then
1098                /* Generate REVAL RESERVE  account */
1099                h_acct_seg          := X_reval_rsv_acct;
1100                h_acct_ccid         := X_reval_rsv_ccid;
1101                h_flex_account_type := 'REV_RSV';
1102                result := FAFLEX_PKG_WF.START_PROCESS(
1103                                 X_flex_account_type => h_flex_account_type,
1104                                 X_book_type_code    => X_book_type_code,
1105                                 X_flex_num          => X_flex_num,
1106                                 X_dist_ccid         => X_dist_ccid,
1107                                 X_acct_segval       => h_acct_seg,
1108                                 X_default_ccid      => X_default_ccid,
1109                                 X_account_ccid      => h_acct_ccid,
1110                                 X_distribution_id   => X_distribution_id,
1111                                 X_validation_date   => G_validation_date,
1112                                 X_return_ccid       => h_return_ccid
1113                                 ,p_log_level_rec => p_log_level_rec);
1114                if (result) then
1115                   G_success_count := G_success_count + 1;
1116                   h_reval_rsv_ccid := h_return_ccid;
1117                else
1118                   Add_Messages(
1119                       X_asset_number,
1120                       X_asset_id,
1121                       h_acct_ccid,
1122                       h_acct_seg,
1123                       h_flex_account_type,
1124                       X_book_type_code,
1125                       X_default_ccid,
1126                       X_dist_ccid
1127                       ,p_log_level_rec => p_log_level_rec);
1128                end if;
1129             end if;
1130          end if;   -- reval allowed flag
1131 
1132 
1133          if (X_bonus_rule is not null) then  -- BUG# 1791317
1134 
1135             if ((found and h_bonus_exp_ccid is null) OR (not found)) then
1136                /* Generate BONUS EXPENSE account */
1137                h_acct_seg          := X_bonus_exp_acct;
1138                h_acct_ccid         := 0;  /* BONUS EXPENSE */
1139                h_flex_account_type := 'BONUS_DEPRN_EXP';
1140                result := FAFLEX_PKG_WF.START_PROCESS(
1141                                 X_flex_account_type => h_flex_account_type,
1142                                 X_book_type_code    => X_book_type_code,
1143                                 X_flex_num          => X_flex_num,
1144                                 X_dist_ccid         => X_dist_ccid,
1145                                 X_acct_segval       => h_acct_seg,
1146                                 X_default_ccid      => X_default_ccid,
1147                                 X_account_ccid      => h_acct_ccid,
1148                                 X_distribution_id   => X_distribution_id,
1149                                 X_validation_date   => G_validation_date,
1150                                 X_return_ccid       => h_return_ccid
1151                                 ,p_log_level_rec => p_log_level_rec);
1152                if (result) then
1153                   G_success_count := G_success_count + 1;
1154                   h_bonus_exp_ccid := h_return_ccid;
1155                else
1156                   Add_Messages(
1157                       X_asset_number,
1158                       X_asset_id,
1159                       h_acct_ccid,
1160                       h_acct_seg,
1161                       h_flex_account_type,
1162                       X_book_type_code,
1163                       X_default_ccid,
1164                       X_dist_ccid
1165                       ,p_log_level_rec => p_log_level_rec);
1166                end if;
1167             end if;
1168 
1169 
1170             if ((found and h_bonus_rsv_ccid is null) OR (not found)) then
1171                /* Generate BONUS RESERVE  account */
1172                h_acct_seg          := X_bonus_rsv_acct;
1173                h_acct_ccid         := X_bonus_rsv_ccid;
1174                h_flex_account_type := 'BONUS_DEPRN_RSV';
1175                result := FAFLEX_PKG_WF.START_PROCESS(
1176                                 X_flex_account_type => h_flex_account_type,
1177                                 X_book_type_code    => X_book_type_code,
1178                                 X_flex_num          => X_flex_num,
1179                                 X_dist_ccid         => X_dist_ccid,
1180                                 X_acct_segval       => h_acct_seg,
1181                                 X_default_ccid      => X_default_ccid,
1182                                 X_account_ccid      => h_acct_ccid,
1183                                 X_distribution_id   => X_distribution_id,
1184                                 X_validation_date   => G_validation_date,
1185                                 X_return_ccid       => h_return_ccid
1186                                 ,p_log_level_rec => p_log_level_rec);
1187                if (result) then
1188                   G_success_count := G_success_count + 1;
1189                   h_bonus_rsv_ccid := h_return_ccid;
1190                else
1191                   Add_Messages(
1192                      X_asset_number,
1193                      X_asset_id,
1194                      h_acct_ccid,
1195                      h_acct_seg,
1196                      h_flex_account_type,
1197                      X_book_type_code,
1198                      X_default_ccid,
1199                      X_dist_ccid
1200                      ,p_log_level_rec => p_log_level_rec);
1201                end if;
1202             end if;
1203          end if;  -- end bonus_rule not null
1204       end if;  -- end category accts
1205 
1206 
1207 
1208       if (G_pregen_book_acct = 'Y' AND
1209           X_asset_type <> 'GROUP') then
1210 
1211          if ((found and h_nbv_gain_ccid is null) OR (not found)) then
1212             /* Generate NBV_RETIRED_GAIN_ACCT */
1213             h_acct_seg          := X_nbv_gain_acct;
1214             h_acct_ccid         := 0;
1215             h_flex_account_type := 'NBV_GAIN';
1216             result := FAFLEX_PKG_WF.START_PROCESS(
1217                                 X_flex_account_type => h_flex_account_type,
1218                                 X_book_type_code    => X_book_type_code,
1219                                 X_flex_num          => X_flex_num,
1220                                 X_dist_ccid         => X_dist_ccid,
1221                                 X_acct_segval       => h_acct_seg,
1222                                 X_default_ccid      => X_default_ccid,
1223                                 X_account_ccid      => h_acct_ccid,
1224                                 X_distribution_id   => X_distribution_id,
1225                                 X_validation_date   => G_validation_date,
1226                                 X_return_ccid       => h_return_ccid
1227                                 ,p_log_level_rec => p_log_level_rec);
1228             if (result) then
1229                G_success_count := G_success_count + 1;
1230                h_nbv_gain_ccid := h_return_ccid;
1231             else
1232               Add_Messages(
1233                 X_asset_number,
1234                 X_asset_id,
1235                 h_acct_ccid,
1236                 h_acct_seg,
1237                 h_flex_account_type,
1238                 X_book_type_code,
1239                 X_default_ccid,
1240                 X_dist_ccid
1241                 ,p_log_level_rec => p_log_level_rec);
1242             end if;
1243          end if;
1244 
1245          if ((found and h_nbv_loss_ccid is null) OR (not found)) then
1246             /* Generate NBV_RETIRED_LOSS_ACCT */
1247             h_acct_seg          := X_nbv_loss_acct;
1248             h_acct_ccid         := 0;
1249             h_flex_account_type := 'NBV_LOSS';
1250             result := FAFLEX_PKG_WF.START_PROCESS(
1251                                 X_flex_account_type => h_flex_account_type,
1252                                 X_book_type_code    => X_book_type_code,
1253                                 X_flex_num          => X_flex_num,
1254                                 X_dist_ccid         => X_dist_ccid,
1255                                 X_acct_segval       => h_acct_seg,
1256                                 X_default_ccid      => X_default_ccid,
1257                                 X_account_ccid      => h_acct_ccid,
1258                                 X_distribution_id   => X_distribution_id,
1259                                 X_validation_date   => G_validation_date,
1260                                 X_return_ccid       => h_return_ccid
1261                                 ,p_log_level_rec => p_log_level_rec);
1262             if (result) then
1263                G_success_count := G_success_count + 1;
1264                h_nbv_loss_ccid := h_return_ccid;
1265             else
1266                Add_Messages(
1267                   X_asset_number,
1268                   X_asset_id,
1269                   h_acct_ccid,
1270                   h_acct_seg,
1271                   h_flex_account_type,
1272                   X_book_type_code,
1273                   X_default_ccid,
1274                   X_dist_ccid
1275                   ,p_log_level_rec => p_log_level_rec);
1276             end if;
1277          end if;
1278 
1279          if ((found and h_pos_gain_ccid is null) OR (not found)) then
1280             /* Generate PROCEEDS_OF_SALE_GAIN_ACCT */
1281             h_acct_seg          := X_pos_gain_acct;
1282             h_acct_ccid         := 0;
1283             h_flex_account_type := 'POS_GAIN';
1284             result := FAFLEX_PKG_WF.START_PROCESS(
1285                                 X_flex_account_type => h_flex_account_type,
1286                                 X_book_type_code    => X_book_type_code,
1287                                 X_flex_num          => X_flex_num,
1288                                 X_dist_ccid         => X_dist_ccid,
1289                                 X_acct_segval       => h_acct_seg,
1290                                 X_default_ccid      => X_default_ccid,
1291                                 X_account_ccid      => h_acct_ccid,
1292                                 X_distribution_id   => X_distribution_id,
1293                                 X_validation_date   => G_validation_date,
1294                                 X_return_ccid       => h_return_ccid
1295                                 ,p_log_level_rec => p_log_level_rec);
1296             if (result) then
1297                G_success_count := G_success_count + 1;
1298                h_pos_gain_ccid := h_return_ccid;
1299             else
1300                Add_Messages(
1301                   X_asset_number,
1302                   X_asset_id,
1303                   h_acct_ccid,
1304                   h_acct_seg,
1305                   h_flex_account_type,
1306                   X_book_type_code,
1307                   X_default_ccid,
1308                   X_dist_ccid
1309                   ,p_log_level_rec => p_log_level_rec);
1310             end if;
1311          end if;
1312 
1313          if ((found and h_pos_loss_ccid is null) OR (not found)) then
1314             /* Generate PROCEEDS_OF_SALE_LOSS_ACCT */
1315             h_acct_seg          := X_pos_loss_acct;
1316             h_acct_ccid         := 0;
1317             h_flex_account_type := 'POS_LOSS';
1318             result := FAFLEX_PKG_WF.START_PROCESS(
1319                                 X_flex_account_type => h_flex_account_type,
1320                                 X_book_type_code    => X_book_type_code,
1321                                 X_flex_num          => X_flex_num,
1322                                 X_dist_ccid         => X_dist_ccid,
1323                                 X_acct_segval       => h_acct_seg,
1324                                 X_default_ccid      => X_default_ccid,
1325                                 X_account_ccid      => h_acct_ccid,
1326                                 X_distribution_id   => X_distribution_id,
1327                                 X_validation_date   => G_validation_date,
1328                                 X_return_ccid       => h_return_ccid
1329                                 ,p_log_level_rec => p_log_level_rec);
1330             if (result) then
1331                G_success_count := G_success_count + 1;
1332                h_pos_loss_ccid := h_return_ccid;
1333             else
1334                Add_Messages(
1335                   X_asset_number,
1336                   X_asset_id,
1337                   h_acct_ccid,
1338                   h_acct_seg,
1339                   h_flex_account_type,
1340                   X_book_type_code,
1341                   X_default_ccid,
1342                   X_dist_ccid
1343                   ,p_log_level_rec => p_log_level_rec);
1344             end if;
1345          end if;
1346 
1347          if ((found and h_cor_gain_ccid is null) OR (not found)) then
1348             /* Generate COST_OF_REMOVAL_GAIN_ACCT */
1349             h_acct_seg          := X_cor_gain_acct;
1350             h_acct_ccid         := 0;
1351             h_flex_account_type := 'COR_GAIN';
1352             result := FAFLEX_PKG_WF.START_PROCESS(
1353                                 X_flex_account_type => h_flex_account_type,
1354                                 X_book_type_code    => X_book_type_code,
1355                                 X_flex_num          => X_flex_num,
1356                                 X_dist_ccid         => X_dist_ccid,
1357                                 X_acct_segval       => h_acct_seg,
1358                                 X_default_ccid      => X_default_ccid,
1359                                 X_account_ccid      => h_acct_ccid,
1360                                 X_distribution_id   => X_distribution_id,
1361                                 X_validation_date   => G_validation_date,
1362                                 X_return_ccid       => h_return_ccid
1363                                 ,p_log_level_rec => p_log_level_rec);
1364             if (result) then
1365                G_success_count := G_success_count + 1;
1366                h_cor_gain_ccid := h_return_ccid;
1367             else
1368                Add_Messages(
1369                   X_asset_number,
1370                   X_asset_id,
1371                   h_acct_ccid,
1372                   h_acct_seg,
1373                   h_flex_account_type,
1374                   X_book_type_code,
1375                   X_default_ccid,
1376                   X_dist_ccid
1377                   ,p_log_level_rec => p_log_level_rec);
1378             end if;
1379          end if;
1380 
1381          if ((found and h_cor_loss_ccid is null) OR (not found)) then
1382             /* Generate COST_OF_REMOVAL_LOSS_ACCT */
1383             h_acct_seg          := X_cor_loss_acct;
1384             h_acct_ccid         := 0;
1385             h_flex_account_type := 'COR_LOSS';
1386             result := FAFLEX_PKG_WF.START_PROCESS(
1387                                 X_flex_account_type => h_flex_account_type,
1388                                 X_book_type_code    => X_book_type_code,
1389                                 X_flex_num          => X_flex_num,
1390                                 X_dist_ccid         => X_dist_ccid,
1391                                 X_acct_segval       => h_acct_seg,
1392                                 X_default_ccid      => X_default_ccid,
1393                                 X_account_ccid      => h_acct_ccid,
1394                                 X_distribution_id   => X_distribution_id,
1395                                 X_validation_date   => G_validation_date,
1396                                 X_return_ccid       => h_return_ccid
1397                                 ,p_log_level_rec => p_log_level_rec);
1398             if (result) then
1399                G_success_count := G_success_count + 1;
1400                h_cor_loss_ccid := h_return_ccid;
1401             else
1402               Add_Messages(
1403                  X_asset_number,
1404                  X_asset_id,
1405                  h_acct_ccid,
1406                  h_acct_seg,
1407                  h_flex_account_type,
1408                  X_book_type_code,
1409                  X_default_ccid,
1410                  X_dist_ccid
1411                  ,p_log_level_rec => p_log_level_rec);
1412             end if;
1413          end if;
1414 
1415          if ((found and h_cor_clearing_ccid is null) OR (not found)) then
1416             /* Generate COST_OF_REMOVAL_CLEARING_ACCT */
1417             h_acct_seg          := X_cor_clearing_acct;
1418             h_acct_ccid         := 0;
1419             h_flex_account_type := 'COR_CLEARING';
1420             result := FAFLEX_PKG_WF.START_PROCESS(
1421                                 X_flex_account_type => h_flex_account_type,
1422                                 X_book_type_code    => X_book_type_code,
1423                                 X_flex_num          => X_flex_num,
1424                                 X_dist_ccid         => X_dist_ccid,
1425                                 X_acct_segval       => h_acct_seg,
1426                                 X_default_ccid      => X_default_ccid,
1427                                 X_account_ccid      => h_acct_ccid,
1428                                 X_distribution_id   => X_distribution_id,
1429                                 X_validation_date   => G_validation_date,
1430                                 X_return_ccid       => h_return_ccid
1431                                 ,p_log_level_rec => p_log_level_rec);
1432             if (result) then
1433                G_success_count := G_success_count + 1;
1434                h_cor_clearing_ccid := h_return_ccid;
1435             else
1436                Add_Messages(
1437                   X_asset_number,
1438                   X_asset_id,
1439                   h_acct_ccid,
1440                   h_acct_seg,
1441                   h_flex_account_type,
1442                   X_book_type_code,
1443                   X_default_ccid,
1444                   X_dist_ccid
1445                   ,p_log_level_rec => p_log_level_rec);
1446             end if;
1447          end if;
1448 
1449          if ((found and h_pos_clearing_ccid is null) OR (not found)) then
1450             /* Generate PROCEEDS_OF_SALE_CLEARING_ACCT */
1451             h_acct_seg          := X_pos_clearing_acct;
1452             h_acct_ccid         := 0;
1453             h_flex_account_type := 'POS_CLEARING';
1454             result := FAFLEX_PKG_WF.START_PROCESS(
1455                                 X_flex_account_type => h_flex_account_type,
1456                                 X_book_type_code    => X_book_type_code,
1457                                 X_flex_num          => X_flex_num,
1458                                 X_dist_ccid         => X_dist_ccid,
1459                                 X_acct_segval       => h_acct_seg,
1460                                 X_default_ccid      => X_default_ccid,
1461                                 X_account_ccid      => h_acct_ccid,
1462                                 X_distribution_id   => X_distribution_id,
1463                                 X_validation_date   => G_validation_date,
1464                                 X_return_ccid       => h_return_ccid
1465                                 ,p_log_level_rec => p_log_level_rec);
1466             if (result) then
1467                G_success_count := G_success_count + 1;
1468                h_pos_clearing_ccid := h_return_ccid;
1469             else
1470                Add_Messages(
1471                   X_asset_number,
1472                   X_asset_id,
1473                   h_acct_ccid,
1474                   h_acct_seg,
1475                   h_flex_account_type,
1476                   X_book_type_code,
1477                   X_default_ccid,
1478                   X_dist_ccid
1479                   ,p_log_level_rec => p_log_level_rec);
1480             end if;
1481          end if;
1482 
1483          /* BUG# 1553682 */
1484 
1485          if (X_book_class = 'TAX' and
1486              X_group_asset_id is null) then
1487 
1488             if (X_allow_deprn_adjust = 'YES') then
1489 
1490                if ((found and h_deprn_adj_ccid is null) OR (not found)) then
1491                   /* Generate DEPRN_ADJ */
1492                   h_acct_seg          := X_deprn_adj_acct;
1493                   h_acct_ccid         := 0;
1494                   h_flex_account_type := 'DEPRN_ADJ';
1495                   result := FAFLEX_PKG_WF.START_PROCESS(
1496                                 X_flex_account_type => h_flex_account_type,
1497                                 X_book_type_code    => X_book_type_code,
1498                                 X_flex_num          => X_flex_num,
1499                                 X_dist_ccid         => X_dist_ccid,
1500                                 X_acct_segval       => h_acct_seg,
1501                                 X_default_ccid      => X_default_ccid,
1502                                 X_account_ccid      => h_acct_ccid,
1503                                 X_distribution_id   => X_distribution_id,
1504                                 X_validation_date   => G_validation_date,
1505                                 X_return_ccid       => h_return_ccid
1506                                 ,p_log_level_rec => p_log_level_rec);
1507                    if (result) then
1508                       G_success_count := G_success_count + 1;
1509                       h_deprn_adj_ccid := h_return_ccid;
1510                    else
1511                       Add_Messages(
1512                         X_asset_number,
1513                         X_asset_id,
1514                         h_acct_ccid,
1515                         h_acct_seg,
1516                         h_flex_account_type,
1517                         X_book_type_code,
1518                         X_default_ccid,
1519                         X_dist_ccid
1520                         ,p_log_level_rec => p_log_level_rec);
1521                    end if;
1522                end if;
1523             end if;  -- allow deprn_adjust flag
1524 
1525 
1526             if ((found and h_deferred_exp_ccid is null) OR (not found)) then
1527                /* Generate DEFERRED_DEPRN_EXPENSE_ACCT */
1528                h_acct_seg          := X_deferred_exp_acct;
1529                h_acct_ccid         := 0;
1530                h_flex_account_type := 'DEF_DEPRN_EXP';
1531                result := FAFLEX_PKG_WF.START_PROCESS(
1532                              X_flex_account_type => h_flex_account_type,
1533                              X_book_type_code    => X_book_type_code,
1534                              X_flex_num          => X_flex_num,
1535                              X_dist_ccid         => X_dist_ccid,
1536                              X_acct_segval       => h_acct_seg,
1537                              X_default_ccid      => X_default_ccid,
1538                              X_account_ccid      => h_acct_ccid,
1539                              X_distribution_id   => X_distribution_id,
1540                              X_validation_date   => G_validation_date,
1541                              X_return_ccid       => h_return_ccid
1542                              ,p_log_level_rec => p_log_level_rec);
1543                if (result) then
1544                   G_success_count := G_success_count + 1;
1545                   h_deferred_exp_ccid := h_return_ccid;
1546                else
1547                   Add_Messages(
1548                      X_asset_number,
1549                      X_asset_id,
1550                      h_acct_ccid,
1551                      h_acct_seg,
1552                      h_flex_account_type,
1553                      X_book_type_code,
1554                      X_default_ccid,
1555                      X_dist_ccid
1556                      ,p_log_level_rec => p_log_level_rec);
1557                end if;
1558             end if;
1559 
1560             if ((found and h_deferred_rsv_ccid is null) OR (not found)) then
1561                /* Generate DEFERRED_DEPRN_RESERVE_ACCT */
1562                h_acct_seg          := X_deferred_rsv_acct;
1563                h_acct_ccid         := 0;
1564                h_flex_account_type := 'DEF_DEPRN_RSV';
1565                result := FAFLEX_PKG_WF.START_PROCESS(
1566                              X_flex_account_type => h_flex_account_type,
1567                              X_book_type_code    => X_book_type_code,
1568                              X_flex_num          => X_flex_num,
1569                              X_dist_ccid         => X_dist_ccid,
1570                              X_acct_segval       => h_acct_seg,
1571                              X_default_ccid      => X_default_ccid,
1572                              X_account_ccid      => h_acct_ccid,
1573                              X_distribution_id   => X_distribution_id,
1574                              X_validation_date   => G_validation_date,
1575                              X_return_ccid       => h_return_ccid
1576                              ,p_log_level_rec => p_log_level_rec);
1577                if (result) then
1578                   G_success_count := G_success_count + 1;
1579                   h_deferred_rsv_ccid := h_return_ccid;
1580                else
1581                   Add_Messages(
1582                      X_asset_number,
1583                      X_asset_id,
1584                      h_acct_ccid,
1585                      h_acct_seg,
1586                      h_flex_account_type,
1587                      X_book_type_code,
1588                      X_default_ccid,
1589                      X_dist_ccid
1590                      ,p_log_level_rec => p_log_level_rec);
1591                end if;
1592             end if;
1593 
1594          end if;    -- X_book_Class = TAX
1595 
1596 
1597          if (X_allow_reval_flag  = 'YES' and
1598              X_group_asset_id is null) then
1599 
1600             if ((found and h_reval_rsv_gain_ccid is null) OR (not found)) then
1601                /* Generate REVAL_RSV_RETIRED_GAIN_ACCT */
1602                h_acct_seg          := X_reval_rsv_gain_acct;
1603                h_acct_ccid         := 0;
1604                h_flex_account_type := 'REV_RSV_GAIN';
1605                result := FAFLEX_PKG_WF.START_PROCESS(
1606                                 X_flex_account_type => h_flex_account_type,
1607                                 X_book_type_code    => X_book_type_code,
1608                                 X_flex_num          => X_flex_num,
1609                                 X_dist_ccid         => X_dist_ccid,
1610                                 X_acct_segval       => h_acct_seg,
1611                                 X_default_ccid      => X_default_ccid,
1612                                 X_account_ccid      => h_acct_ccid,
1613                                 X_distribution_id   => X_distribution_id,
1614                                 X_validation_date   => G_validation_date,
1615                                 X_return_ccid       => h_return_ccid
1616                                 ,p_log_level_rec => p_log_level_rec);
1617                if (result) then
1618                   G_success_count := G_success_count + 1;
1619                   h_reval_rsv_gain_ccid := h_return_ccid;
1620                else
1621                   Add_Messages(
1622                         X_asset_number,
1623                         X_asset_id,
1624                         h_acct_ccid,
1625                         h_acct_seg,
1626                         h_flex_account_type,
1627                         X_book_type_code,
1628                         X_default_ccid,
1629                         X_dist_ccid
1630                         ,p_log_level_rec => p_log_level_rec);
1631                end if;
1632             end if;
1633 
1634             if ((found and h_reval_rsv_loss_ccid is null) OR (not found)) then
1635                /* Generate REVAL_RSV_RETIRED_LOSS_ACCT */
1636                h_acct_seg          := X_reval_rsv_loss_acct;
1637                h_acct_ccid         := 0;
1638                h_flex_account_type := 'REV_RSV_LOSS';
1639                result := FAFLEX_PKG_WF.START_PROCESS(
1640                                 X_flex_account_type => h_flex_account_type,
1641                                 X_book_type_code    => X_book_type_code,
1642                                 X_flex_num          => X_flex_num,
1643                                 X_dist_ccid         => X_dist_ccid,
1644                                 X_acct_segval       => h_acct_seg,
1645                                 X_default_ccid      => X_default_ccid,
1646                                 X_account_ccid      => h_acct_ccid,
1647                                 X_distribution_id   => X_distribution_id,
1648                                 X_validation_date   => G_validation_date,
1649                                 X_return_ccid       => h_return_ccid
1650                                 ,p_log_level_rec => p_log_level_rec);
1651                if (result) then
1652                   G_success_count := G_success_count + 1;
1653                   h_reval_rsv_loss_ccid := h_return_ccid;
1654                else
1655                   Add_Messages(
1656                         X_asset_number,
1657                         X_asset_id,
1658                         h_acct_ccid,
1659                         h_acct_seg,
1660                         h_flex_account_type,
1661                         X_book_type_code,
1662                         X_default_ccid,
1663                         X_dist_ccid
1664                         ,p_log_level_rec => p_log_level_rec);
1665                end if;
1666             end if;
1667 
1668          end if;    -- end if allow_reval_flag
1669 
1670       end if; --end book level accts
1671 
1672       close get_ccids;
1673 
1674       -- insert or update all the CCID's obtained for distribution
1675       -- into FA_DISTRIBUTION_ACCOUNTS
1676 
1677       h_user_id := fnd_global.user_id;
1678       h_login_id := fnd_global.login_id;
1679 
1680       if (not found) then
1681 
1682          INSERT INTO FA_DISTRIBUTION_ACCOUNTS(
1683                       BOOK_TYPE_CODE,
1684                       DISTRIBUTION_ID,
1685                       ASSET_COST_ACCOUNT_CCID,
1686                       ASSET_CLEARING_ACCOUNT_CCID,
1687                       DEPRN_EXPENSE_ACCOUNT_CCID,
1688                       DEPRN_RESERVE_ACCOUNT_CCID,
1689                       CIP_COST_ACCOUNT_CCID,
1690                       CIP_CLEARING_ACCOUNT_CCID,
1691                       NBV_RETIRED_GAIN_CCID,
1692                       NBV_RETIRED_LOSS_CCID,
1693                       PROCEEDS_SALE_GAIN_CCID,
1694                       PROCEEDS_SALE_LOSS_CCID,
1695                       COST_REMOVAL_GAIN_CCID,
1696                       COST_REMOVAL_LOSS_CCID,
1697                       COST_REMOVAL_CLEARING_CCID,
1698                       PROCEEDS_SALE_CLEARING_CCID,
1699                       LAST_UPDATE_DATE,
1700                       LAST_UPDATED_BY,
1701                       CREATED_BY,
1702                       CREATION_DATE,
1703                       LAST_UPDATE_LOGIN,
1704                       deferred_exp_account_ccid,
1705                       deferred_rsv_account_ccid,
1706                       reval_rsv_gain_account_ccid,
1707                       reval_rsv_loss_account_ccid,
1708                       deprn_adj_account_ccid,
1709                       reval_amort_account_ccid,
1710                       reval_rsv_account_ccid,
1711                       bonus_exp_account_ccid,
1712                       bonus_rsv_account_ccid
1713                       )VALUES(
1714           X_book_type_code,
1715           X_distribution_id,
1716           h_cost_acct_ccid,
1717           h_clearing_acct_ccid,
1718           h_expense_acct_ccid,
1719           h_reserve_acct_ccid,
1720           h_cip_cost_ccid,
1721           h_cip_clearing_ccid,
1722           h_nbv_gain_ccid,
1723           h_nbv_loss_ccid,
1724           h_pos_gain_ccid,
1725           h_pos_loss_ccid,
1726           h_cor_gain_ccid,
1727           h_cor_loss_ccid,
1728           h_cor_clearing_ccid,
1729           h_pos_clearing_ccid,
1730           sysdate,
1731           h_user_id,
1732           h_user_id,
1733           sysdate,
1734           h_login_id,
1735           h_deferred_exp_ccid,
1736           h_deferred_rsv_ccid,
1737           h_reval_rsv_gain_ccid,
1738           h_reval_rsv_loss_ccid,
1739           h_deprn_adj_ccid,
1740           h_reval_amort_ccid,
1741           h_reval_rsv_ccid,
1742           h_bonus_exp_ccid,
1743           h_bonus_rsv_ccid
1744           );
1745       else
1746        UPDATE FA_DISTRIBUTION_ACCOUNTS
1747           SET ASSET_COST_ACCOUNT_CCID     = h_cost_acct_ccid,
1748               ASSET_CLEARING_ACCOUNT_CCID = h_clearing_acct_ccid,
1749               DEPRN_EXPENSE_ACCOUNT_CCID  = h_expense_acct_ccid,
1750               DEPRN_RESERVE_ACCOUNT_CCID  = h_reserve_acct_ccid,
1751               CIP_COST_ACCOUNT_CCID       = h_cip_cost_ccid,
1752               CIP_CLEARING_ACCOUNT_CCID   = h_cip_clearing_ccid,
1753               NBV_RETIRED_GAIN_CCID       = h_nbv_gain_ccid,
1754               NBV_RETIRED_LOSS_CCID       = h_nbv_loss_ccid,
1755               PROCEEDS_SALE_GAIN_CCID     = h_pos_gain_ccid,
1756               PROCEEDS_SALE_LOSS_CCID     = h_pos_loss_ccid,
1757               COST_REMOVAL_GAIN_CCID      = h_cor_gain_ccid,
1758               COST_REMOVAL_LOSS_CCID      = h_cor_loss_ccid,
1759               COST_REMOVAL_CLEARING_CCID  = h_cor_clearing_ccid,
1760               PROCEEDS_SALE_CLEARING_CCID = h_pos_clearing_ccid,
1761               LAST_UPDATE_DATE            = sysdate,
1762               LAST_UPDATED_BY             = h_user_id,
1763               LAST_UPDATE_LOGIN           = h_login_id,
1764               deferred_exp_account_ccid   = h_deferred_exp_ccid,
1765               deferred_rsv_account_ccid   = h_deferred_rsv_ccid,
1766               reval_rsv_gain_account_ccid = h_reval_rsv_gain_ccid,
1767               reval_rsv_loss_account_ccid = h_reval_rsv_loss_ccid,
1768               deprn_adj_account_ccid      = h_deprn_adj_ccid,
1769               reval_amort_account_ccid    = h_reval_amort_ccid,
1770               reval_rsv_account_ccid      = h_reval_rsv_ccid,
1771               bonus_exp_account_ccid      = h_bonus_exp_ccid,
1772               bonus_rsv_account_ccid      = h_bonus_rsv_ccid
1773         WHERE BOOK_TYPE_CODE              = X_book_type_code
1774           AND DISTRIBUTION_ID             = X_distribution_id;
1775       end if;
1776       COMMIT;
1777 
1778 EXCEPTION
1779       WHEN OTHERS THEN
1780            FA_SRVR_MSG.ADD_SQL_ERROR(
1781               CALLING_FN => 'FA_GENACCTS_PKG.GEN_CCID'
1782               ,p_log_level_rec => p_log_level_rec);
1783            ROLLBACK WORK;
1784            wf_core.context('FA_FLEX_PKG',
1785                     'StartProcess',
1786                     X_book_type_code,
1787                     X_dist_ccid,
1788                     X_default_ccid,
1789                     'FAFLEXWF');
1790            raise;
1791 END GEN_CCID;
1792 
1793 ---------------------------------------------------------------------
1794 
1795 PROCEDURE Add_Messages(
1796           X_asset_number       IN     VARCHAR2,
1797           X_asset_id           IN     NUMBER,
1798           X_account_ccid       IN     NUMBER,
1799           X_acct_seg           IN     VARCHAR2,
1800           X_flex_account_type  IN     VARCHAR2,
1801           X_book_type_code     IN     VARCHAR2,
1802           X_default_ccid       IN     NUMBER,
1803           X_dist_ccid          IN     NUMBER
1804           ,p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type default null) IS
1805 
1806 BEGIN
1807 
1808    G_failure_count := G_failure_count + 1;
1809 
1810    -- BUG# 1504839
1811    -- Main flex error is already dumped out in FAFLEX_WF_PKG, so there
1812    -- is not need to dump it here as well...
1813    -- book and account_type are also dumped out in the message from FAFLEX_WF_PKG
1814 
1815    if (g_log_level_rec.statement_level) then
1816 
1817       FA_SRVR_MSG.ADD_MESSAGE
1818               (CALLING_FN => 'FA_GENACCTS_PKG.GEN_CCID',
1819                NAME       => 'FA_ASSET_NUMBER',
1820                TOKEN1     => 'ASSET_NUMBER',
1821                VALUE1     => X_asset_number
1822                ,p_log_level_rec => p_log_level_rec);
1823       FA_SRVR_MSG.ADD_MESSAGE
1824               (CALLING_FN => 'FA_GENACCTS_PKG.GEN_CCID',
1825                NAME       => 'FA_ASSET_ID',
1826                TOKEN1     => 'ASSET_ID',
1827                VALUE1     => X_asset_id
1828                ,p_log_level_rec => p_log_level_rec);
1829 
1830       FA_SRVR_MSG.ADD_MESSAGE
1831               (CALLING_FN => 'FA_GENACCTS_PKG.GEN_CCID',
1832                NAME       => 'FA_FLEX_ACCOUNT_SEGMENT',
1833                TOKEN1     => 'ACCOUNT_SEGMENT',
1834                VALUE1     => X_acct_seg
1835                ,p_log_level_rec => p_log_level_rec);
1836       FA_SRVR_MSG.ADD_MESSAGE
1837               (CALLING_FN => 'FA_GENACCTS_PKG.GEN_CCID',
1838                NAME       => 'FA_FLEX_ACCOUNT_CCID',
1839                TOKEN1     => 'ACCOUNT_CCID',
1840                VALUE1     => X_account_ccid
1841                ,p_log_level_rec => p_log_level_rec);
1842       FA_SRVR_MSG.ADD_MESSAGE
1843               (CALLING_FN => 'FA_GENACCTS_PKG.GEN_CCID',
1844                NAME       => 'FA_FLEX_DEFAULT_CCID',
1845                TOKEN1     => 'DEFAULT_CCID',
1846                VALUE1     => X_default_ccid
1847                ,p_log_level_rec => p_log_level_rec);
1848       FA_SRVR_MSG.ADD_MESSAGE
1849               (CALLING_FN => 'FA_GENACCTS_PKG.GEN_CCID',
1850                NAME       => 'FA_FLEX_DISTRIBUTION_CCID',
1851                TOKEN1     => 'DISTRIBUTION_CCID',
1852                VALUE1     => X_dist_ccid
1853                ,p_log_level_rec => p_log_level_rec);
1854    end if;
1855 
1856 END Add_Messages;
1857 
1858 -----------------------------------------------------------------------
1859 
1860 PROCEDURE Load_Workers(
1861                 p_book_type_code     IN     VARCHAR2,
1862                 p_parent_request_id  IN     NUMBER,
1863                 p_total_requests     IN     NUMBER,
1864                 x_worker_jobs           OUT NOCOPY NUMBER,
1865                 x_return_status         OUT NOCOPY number
1866                 ) is
1867 
1868    l_batch_size         number;
1869    l_dist_source_book   varchar2(15);
1870    l_calling_fn         varchar2(60) := 'FA_GENACCTS_PKG.Load_Workers';
1871 
1872    error_found          exception;
1873 
1874 BEGIN
1875 
1876 
1877    if (not g_log_level_rec.initialized) then
1878       if (NOT fa_util_pub.get_log_level_rec (
1879                 x_log_level_rec =>  g_log_level_rec
1880       )) then
1881          raise error_found;
1882       end if;
1883    end if;
1884 
1885    if not (fa_cache_pkg.fazcbc(x_book => p_book_type_code
1886                               ,p_log_level_rec => g_log_level_rec)) then
1887       raise error_found;
1888    end if;
1889 
1890    l_batch_size  := nvl(fa_cache_pkg.fa_batch_size, 1000);
1891    l_dist_source_book := fa_cache_pkg.fazcbc_record.distribution_source_book;
1892 
1893    INSERT INTO FA_WORKER_JOBS
1894           (START_RANGE, END_RANGE, WORKER_NUM, STATUS,REQUEST_ID)
1895    SELECT MIN(DISTRIBUTION_ID), MAX(DISTRIBUTION_ID), 0,
1896           'UNASSIGNED', p_parent_request_id  FROM ( SELECT /*+ parallel(DH) */
1897           DISTRIBUTION_ID, FLOOR(RANK()
1898           OVER (ORDER BY DISTRIBUTION_ID)/l_batch_size ) UNIT_ID
1899      FROM FA_DISTRIBUTION_HISTORY DH
1900     WHERE DH.BOOK_TYPE_CODE = l_dist_source_book )
1901     GROUP BY UNIT_ID;
1902 
1903    if g_log_level_rec.statement_level then
1904       fa_debug_pkg.add(l_calling_fn, 'rows inserted into worker jobs: ', SQL%ROWCOUNT
1905 			,p_log_level_rec => g_log_level_rec);
1906    end if;
1907 
1908    commit;
1909 
1910    x_return_status := 0;
1911 
1912 EXCEPTION
1913    WHEN error_found then
1914         rollback;
1915 
1916 -- Commented for bugfix 4672237
1917 --        if (g_log_level_rec.statement_level) then
1918 --           fa_debug_pkg.dump_debug_messages(max_mesgs => 0);
1919 --        end if;
1920         fa_srvr_msg.add_message(calling_fn => l_calling_fn
1921                 ,p_log_level_rec => g_log_level_rec);
1922         x_return_status := 2;
1923 
1924    WHEN OTHERS THEN
1925         FA_SRVR_MSG.ADD_SQL_ERROR(
1926            CALLING_FN => 'FA_GENACCTS_PKG.gen_accts'
1927            ,p_log_level_rec => g_log_level_rec);
1928         rollback;
1929 
1930 -- Commented for bugfix 4672237
1931 --        if (g_log_level_rec.statement_level) then
1932 --           fa_debug_pkg.dump_debug_messages(max_mesgs => 0);
1933 --        end if;
1934         x_return_status := 2;
1935 
1936 END Load_Workers;
1937 
1938 -----------------------------------------------------------------------
1939 
1940 END FA_GENACCTS_PKG;