DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_UNPLANNED_PUB

Source


1 PACKAGE BODY FA_UNPLANNED_PUB as
2 /* $Header: FAPUNPLB.pls 120.24.12020000.2 2012/07/23 10:04:16 rmandali ship $   */
3 
4 --*********************** Global constants ******************************--
5 
6 G_PKG_NAME      CONSTANT   varchar2(30) := 'FA_UNPLANNED_PUB';
7 G_API_NAME      CONSTANT   varchar2(30) := 'Unplanned API';
8 G_API_VERSION   CONSTANT   number       := 1.0;
9 
10 g_log_level_rec fa_api_types.log_level_rec_type;
11 g_release                  number  := fa_cache_pkg.fazarel_release;
12 
13 --*********************** Private functions ******************************--
14 
15 -- private declaration for books (mrc) wrapper
16 
17 FUNCTION do_all_books
18    (px_trans_rec               IN OUT NOCOPY FA_API_TYPES.trans_rec_type,
19     px_asset_hdr_rec           IN OUT NOCOPY FA_API_TYPES.asset_hdr_rec_type,
20     p_asset_desc_rec           IN     FA_API_TYPES.asset_desc_rec_type,
21     p_asset_type_rec           IN     FA_API_TYPES.asset_type_rec_type,
22     p_asset_cat_rec            IN     FA_API_TYPES.asset_cat_rec_type,
23     p_unplanned_deprn_rec      IN     FA_API_TYPES.unplanned_deprn_rec_type
24    , p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) RETURN BOOLEAN;
25 
26 
27 --*********************** Public procedures ******************************--
28 
29 PROCEDURE do_unplanned
30    (p_api_version             IN     NUMBER,
31     p_init_msg_list           IN     VARCHAR2 := FND_API.G_FALSE,
32     p_commit                  IN     VARCHAR2 := FND_API.G_FALSE,
33     p_validation_level        IN     NUMBER   := FND_API.G_VALID_LEVEL_FULL,
34     p_calling_fn              IN     VARCHAR2 := NULL,
35     x_return_status              OUT NOCOPY VARCHAR2,
36     x_msg_count                  OUT NOCOPY NUMBER,
37     x_msg_data                   OUT NOCOPY VARCHAR2,
38     px_trans_rec              IN OUT NOCOPY FA_API_TYPES.trans_rec_type,
39     px_asset_hdr_rec          IN OUT NOCOPY FA_API_TYPES.asset_hdr_rec_type,
40     p_unplanned_deprn_rec     IN     FA_API_TYPES.unplanned_deprn_rec_type) IS
41 
42    l_asset_desc_rec        FA_API_TYPES.asset_desc_rec_type;
43    l_asset_type_rec        FA_API_TYPES.asset_type_rec_type;
44    l_asset_cat_rec         FA_API_TYPES.asset_cat_rec_type;
45 
46    l_reporting_flag        VARCHAR2(1);
47 
48    l_calling_fn            VARCHAR2(35) := 'fa_unplanned_pub.do_unplanned';
49    unp_err                 EXCEPTION;
50 
51 BEGIN
52 
53    SAVEPOINT do_unplanned;
54 
55    if (not g_log_level_rec.initialized) then
56       if (NOT fa_util_pub.get_log_level_rec (
57                 x_log_level_rec =>  g_log_level_rec
58       )) then
59          raise unp_err;
60       end if;
61    end if;
62 
63    -- Initialize message list if p_init_msg_list is set to TRUE.
64    if (fnd_api.to_boolean(p_init_msg_list)) then
65         -- initialize error message stack.
66         fa_srvr_msg.init_server_message;
67 
68         -- initialize debug message stack.
69         fa_debug_pkg.initialize;
70    end if;
71 
72    -- reset after above cache initialization
73    g_release := fa_cache_pkg.fazarel_release;
74 
75    -- Check version of the API
76    -- Standard call to check for API call compatibility.
77    if NOT fnd_api.compatible_api_call (
78           G_API_VERSION,
79           p_api_version,
80           G_API_NAME,
81           G_PKG_NAME
82          ) then
83       x_return_status := FND_API.G_RET_STS_ERROR;
84       raise unp_err;
85    end if;
86 
87    -- set up sob/mrc info
88    -- call the cache for the primary transaction book
89    if (NOT fa_cache_pkg.fazcbc(X_book => px_asset_hdr_rec.book_type_code, p_log_level_rec => g_log_level_rec)) then
90       raise unp_err;
91    end if;
92 
93    px_asset_hdr_rec.set_of_books_id := fa_cache_pkg.fazcbc_record.set_of_books_id;
94 
95    if not (FA_ASSET_VAL_PVT.validate_asset_book
96               (p_transaction_type_code      => 'ADJUSTMENT',
97                p_book_type_code             => px_asset_hdr_rec.book_type_code,
98                p_asset_id                   => px_asset_hdr_rec.asset_id,
99                p_calling_fn                 => l_calling_fn, p_log_level_rec => g_log_level_rec)) then
100       raise unp_err;
101    end if;
102 
103    --  Account for transaction submitted from a responsibility
104    --  that is not tied to a SOB_ID by getting the value from
105    --  the book struct
106 
107    -- Get the book type code P,R or N
108    if not fa_cache_pkg.fazcsob
109            (X_set_of_books_id   => px_asset_hdr_rec.set_of_books_id,
110             X_mrc_sob_type_code => l_reporting_flag, p_log_level_rec => g_log_level_rec) then
111       raise unp_err;
112    end if;
113 
114    --  Error out if the program is submitted from the Reporting Responsibility
115    --  No transaction permitted directly on reporting books.
116 
117    IF l_reporting_flag = 'R' THEN
118       fa_srvr_msg.add_message
119           (NAME       => 'MRC_OSP_INVALID_BOOK_TYPE',
120            CALLING_FN => l_calling_fn, p_log_level_rec => g_log_level_rec);
121       raise unp_err;
122    END IF;
123 
124    -- end initial MRC validation
125 
126 
127    -- verify that the amortized type is populated and valid
128    if (p_unplanned_deprn_rec.unplanned_type is not null) then
129       if not FA_ASSET_VAL_PVT.validate_fa_lookup_code
130               (p_lookup_type  => 'UNPLANNED DEPRN',
131                p_lookup_code  => p_unplanned_deprn_rec.unplanned_type, p_log_level_rec => g_log_level_rec) then
132          raise unp_err;
133       end if;
134    end if;
135 
136    --Check if impairment has been posted in current period.
137    if not FA_ASSET_VAL_PVT.validate_impairment_exists
138           (p_asset_id           => px_asset_hdr_rec.asset_id,
139            p_book               => px_asset_hdr_rec.book_type_code,
140            p_mrc_sob_type_code  => 'P',
141            p_set_of_books_id    => px_asset_hdr_rec.set_of_books_id,
142            p_log_level_rec      => g_log_level_rec) then
143         raise FND_API.G_EXC_ERROR;
144     end if;
145 
146    -- pop the asset type
147    if not FA_UTIL_PVT.get_asset_type_rec
148           (p_asset_hdr_rec         => px_asset_hdr_rec,
149            px_asset_type_rec       => l_asset_type_rec,
150            p_date_effective        => null
151           , p_log_level_rec => g_log_level_rec) then
152       raise unp_err;
153    end if;
154 
155    -- pop the asset category
156    if not FA_UTIL_PVT.get_asset_cat_rec
157           (p_asset_hdr_rec         => px_asset_hdr_rec,
158            px_asset_cat_rec        => l_asset_cat_rec,
159            p_date_effective        => null
160           , p_log_level_rec => g_log_level_rec) then
161       raise unp_err;
162    end if;
163 
164    -- pop the asset desc (needed for current units)
165    if not FA_UTIL_PVT.get_asset_desc_rec
166           (p_asset_hdr_rec         => px_asset_hdr_rec,
167            px_asset_desc_rec       => l_asset_desc_rec
168           , p_log_level_rec => g_log_level_rec) then
169       raise unp_err;
170    end if;
171 
172    if (l_asset_type_rec.asset_type <> 'CAPITALIZED' and
173        l_asset_type_rec.asset_type <> 'GROUP') then
174       fa_srvr_msg.add_message
175           (name       => '** NO_UNPLANNED_NON_CAP **',
176            calling_fn => l_calling_fn,
177            p_log_level_rec => g_log_level_rec);
178       raise unp_err;
179    end if;
180 
181    -- Bug:4944700
182    if not FA_ASSET_VAL_PVT.validate_period_of_addition
183           (p_asset_id            => px_asset_hdr_rec.asset_id,
184            p_book                => px_asset_hdr_rec.book_type_code,
185            p_mode                => 'ABSOLUTE',
186            px_period_of_addition => px_asset_hdr_rec.period_of_addition
187           , p_log_level_rec => g_log_level_rec) then
188       raise unp_err;
189    end if;
190 
191    if (g_log_level_rec.statement_level) then
192       fa_debug_pkg.add(l_calling_fn, 'px_asset_hdr_rec.period_of_addition', px_asset_hdr_rec.period_of_addition, p_log_level_rec => g_log_level_rec);
193    end if;
194 
195 
196    -- check / default the trx info
197    -- check the transaction_date
198 
199    -- R12 conditional handling
200    if (px_asset_hdr_rec.period_of_addition = 'Y' and
201        G_release = 11) then
202       if l_asset_type_rec.asset_type = 'GROUP' then
203          px_trans_rec.transaction_type_code := 'GROUP ADDITION';
204       else
205          px_trans_rec.transaction_type_code := 'ADDITION';
206       end if;
207    else
208       if l_asset_type_rec.asset_type = 'GROUP' then
209          px_trans_rec.transaction_type_code := 'GROUP ADJUSTMENT';
210       else
211          px_trans_rec.transaction_type_code := 'ADJUSTMENT';
212       end if;
213    end if;
214 
215    if (px_trans_rec.transaction_subtype is null) then
216        px_trans_rec.transaction_subtype := 'EXPENSED';
217    end if;
218 
219    -- but for group assets, we always amortize
220    if (l_asset_type_rec.asset_type = 'GROUP') then
221        px_trans_rec.transaction_subtype := 'AMORTIZED';
222    end if;
223 
224    -- set the subtype
225    if (px_trans_rec.transaction_subtype = 'AMORTIZED') then
226       px_trans_rec.transaction_key := 'UA';
227    else
228       px_trans_rec.transaction_key := 'UE';
229    end if;
230 
231    /* Added for bug 8584206 */
232    IF not FA_ASSET_VAL_PVT.validate_energy_transactions (
233                p_trans_rec            => px_trans_rec,
234                p_asset_hdr_rec        => px_asset_hdr_rec,
235                p_log_level_rec        => g_log_level_rec) then
236            raise unp_err;
237         END IF;
238 
239    -- check for prior amortizations/retirements
240    if (px_trans_rec.transaction_subtype <> 'AMORTIZED') then
241       if not FA_ASSET_VAL_PVT.validate_exp_after_amort
242              (p_asset_id           => px_asset_hdr_rec.asset_id,
243               p_book               => px_asset_hdr_rec.book_type_code
244               , p_log_level_rec => g_log_level_rec) then
245          raise unp_err;
246       end if;
247    else
248       if (fa_cache_pkg.fazcbc_record.amortize_flag = 'NO') then
249          fa_srvr_msg.add_message(
250             calling_fn => l_calling_fn,
251             name       => 'FA_BOOK_AMORTIZED_NOT_ALLOW', p_log_level_rec => g_log_level_rec);
252          raise unp_err;
253       end if;
254    end if;
255 
256    if FA_ASSET_VAL_PVT.validate_fully_retired
257           (p_asset_id           => px_asset_hdr_rec.asset_id,
258            p_book               => px_asset_hdr_rec.book_type_code
259           , p_log_level_rec => g_log_level_rec) then
260       fa_srvr_msg.add_message
261           (name       => 'FA_REC_RETIRED',
262            calling_fn =>  l_calling_fn, p_log_level_rec => g_log_level_rec);
263       raise unp_err;
264    end if;
265 
266    -- call the mrc wrapper for the transaction book
267    if not do_all_books
268          (px_trans_rec               => px_trans_rec,
269           px_asset_hdr_rec           => px_asset_hdr_rec,
270           p_asset_desc_rec           => l_asset_desc_rec,
271           p_asset_type_rec           => l_asset_type_rec,
272           p_asset_cat_rec            => l_asset_cat_rec,
273           p_unplanned_deprn_rec      => p_unplanned_deprn_rec,
274           p_log_level_rec => g_log_level_rec
275           )then
276       raise unp_err;
277    end if;
278 
279    -- commit if p_commit is TRUE.
280    if (fnd_api.to_boolean (p_commit)) then
281         COMMIT WORK;
282    end if;
283 
284    x_return_status :=  FND_API.G_RET_STS_SUCCESS;
285 
286 EXCEPTION
287    when unp_err then
288       ROLLBACK to do_unplanned;
289 
290       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => g_log_level_rec);
291 
292       FA_DEBUG_PKG.dump_debug_messages(max_mesgs => 0, p_log_level_rec => g_log_level_rec);
293 
294       FND_MSG_PUB.count_and_get (
295          p_count => x_msg_count,
296          p_data  => x_msg_data
297       );
298 
299       x_return_status := FND_API.G_RET_STS_ERROR;
300 
301    when others then
302       ROLLBACK to do_unplanned;
303 
304       fa_srvr_msg.add_sql_error(
305                calling_fn => l_calling_fn, p_log_level_rec => g_log_level_rec);
306 
307       FA_DEBUG_PKG.dump_debug_messages(max_mesgs => 0, p_log_level_rec => g_log_level_rec);
308 
309       FND_MSG_PUB.count_and_get (
310          p_count => x_msg_count,
311          p_data  => x_msg_data
312       );
313 
314       x_return_status := FND_API.G_RET_STS_ERROR;
315 
316 END do_unplanned;
317 
318 
319 -----------------------------------------------------------------------------
320 
321 -- Books (MRC) Wrapper - called from public API above
322 --
323 -- For non mrc books, this just calls the private API with provided params
324 -- For MRC, it processes the primary and then loops through each reporting
325 -- book calling the private api for each.
326 
327 
328 FUNCTION do_all_books
329    (px_trans_rec               IN OUT NOCOPY FA_API_TYPES.trans_rec_type,
330     px_asset_hdr_rec           IN OUT NOCOPY FA_API_TYPES.asset_hdr_rec_type,
331     p_asset_desc_rec           IN     FA_API_TYPES.asset_desc_rec_type,
332     p_asset_type_rec           IN     FA_API_TYPES.asset_type_rec_type,
333     p_asset_cat_rec            IN     FA_API_TYPES.asset_cat_rec_type,
334     p_unplanned_deprn_rec      IN     FA_API_TYPES.unplanned_deprn_rec_type
335    , p_log_level_rec        IN     FA_API_TYPES.log_level_rec_type) RETURN BOOLEAN IS
336 
337    -- used for calling private api for reporting books
338    l_asset_hdr_rec            FA_API_TYPES.asset_hdr_rec_type;
339    l_asset_fin_rec_adj        FA_API_TYPES.asset_fin_rec_type;
340    l_asset_deprn_rec_adj      FA_API_TYPES.asset_deprn_rec_type;
341 
342    -- used for retrieving "old" and "new" structs from private api calls
343    l_asset_fin_rec            FA_API_TYPES.asset_fin_rec_type;
344    l_asset_deprn_rec          FA_API_TYPES.asset_deprn_rec_type;
345 
346    l_unplanned_deprn_rec FA_API_TYPES.unplanned_deprn_rec_type;
347 
348    l_reporting_flag           varchar2(1);
349    l_period_rec               FA_API_TYPES.period_rec_type;
350    l_sob_tbl                  FA_CACHE_PKG.fazcrsob_sob_tbl_type;
351 
352    -- used for local runs
353    l_responsibility_id        number;
354    l_application_id           number;
355 
356    -- used for get_rate
357    l_deprn_ratio              number;
358    l_exchange_date            date;
359    l_rate                     number;
360    l_result_code              varchar2(15);
361 
362    l_complete                 varchar2(1);
363    l_result_code1             varchar2(15);
364 
365    l_exchange_rate            number;
366    l_avg_rate                 number;
367 
368    l_transaction_date         date;
369 
370    l_flex_structure_num       number;
371    n_segs                     number;
372    all_segments               fnd_flex_ext.SegmentArray;
373    l_account_segnum           number;
374 
375    l_status                boolean;
376    l_rowid                 rowid;
377    l_primary_cost            number;
378 
379    -- Track Member
380    l_grp_trans_rec                FA_API_TYPES.trans_rec_type;
381    l_grp_asset_hdr_rec            FA_API_TYPES.asset_hdr_rec_type;
382    l_grp_asset_desc_rec           FA_API_TYPES.asset_desc_rec_type;
383    l_grp_asset_type_rec           FA_API_TYPES.asset_type_rec_type;
384    l_grp_asset_cat_rec            FA_API_TYPES.asset_cat_rec_type;
385    l_grp_asset_fin_rec            FA_API_TYPES.asset_fin_rec_type;
386    l_grp_asset_deprn_rec          FA_API_TYPES.asset_deprn_rec_type;
387    l_grp_deprn_basis_rule         VARCHAR2(4);
388    l_grp_rate_source_rule         VARCHAR2(10);
389    l_ret_code                     number;
390    l_group_level_override         VARCHAR2(1) := 'Y';
391    x_group_deprn_amount           number;
392    x_group_bonus_amount           number;
393 
394 
395    l_calling_fn               varchar2(30) := 'fa_unplanned_pub.do_all_books';
396    unp_err                    EXCEPTION;
397 
398 BEGIN
399 
400    -- call transaction approval
401    if not FA_TRX_APPROVAL_PKG.faxcat
402           (X_book              => px_asset_hdr_rec.book_type_code,
403            X_asset_id          => px_asset_hdr_rec.asset_id,
404            X_trx_type          => 'ADJUSTMENT',
405            X_trx_date          => px_trans_rec.transaction_date_entered,
406            X_init_message_flag => 'NO'
407           , p_log_level_rec => p_log_level_rec) then
408       raise unp_err;
409    end if;
410 
411    if not FA_UTIL_PVT.get_period_rec
412           (p_book           => px_asset_hdr_rec.book_type_code,
413            p_effective_date => NULL,
414            x_period_rec     => l_period_rec
415           , p_log_level_rec => p_log_level_rec) then
416       raise unp_err;
417    end if;
418 
419    -- Bug # 4882700
420    -- value for parameter "p_mode" is changed to ABSOLUTE from CAPITALIZED.
421    -- check period of addition
422    if not FA_ASSET_VAL_PVT.validate_period_of_addition
423          (p_asset_id            => px_asset_hdr_rec.asset_id,
424           p_book                => px_asset_hdr_rec.book_type_code,
425           p_mode                => 'ABSOLUTE',
426           px_period_of_addition => px_asset_hdr_rec.period_of_addition
427          , p_log_level_rec => p_log_level_rec) then
428       raise unp_err;
429    end if;
430 
431 
432    -- trx_date for all expensed transactions will be last date of open period
433    l_transaction_date := greatest(l_period_rec.calendar_period_open_date,
434                                   least(sysdate,l_period_rec.calendar_period_close_date));
435    /*Bug#13861504 - Validate ccid */
436    if not FA_ASSET_VAL_PVT.validate_ccid
437                (p_book_type_code         => px_asset_hdr_rec.book_type_code,
438                 p_ccid                   => p_unplanned_deprn_rec.code_combination_id,
439                 p_gl_chart_id            => fa_cache_pkg.fazcbc_record.accounting_flex_structure,
440 		p_ledger_id              => fa_cache_pkg.fazcbc_record.set_of_books_id,
441 		p_validation_date        => l_transaction_date,
442                 p_calling_fn             => l_calling_fn,
443                 p_log_level_rec          => p_log_level_rec
444                ) then
445       raise unp_err;
446    end if;
447    if (px_trans_rec.transaction_subtype = 'EXPENSED') then
448 
449        if not FA_ASSET_VAL_PVT.validate_exp_after_amort
450                 (p_asset_id     => px_asset_hdr_rec.asset_id,
451                  p_book         => px_asset_hdr_rec.book_type_code
452                 , p_log_level_rec => p_log_level_rec) then
453           raise unp_err;
454        end if;
455 
456        px_trans_rec.transaction_date_entered :=
457           to_date(to_char(l_transaction_date,'DD/MM/YYYY'),'DD/MM/YYYY');
458 
459        px_trans_rec.amortization_start_date := NULL;
460 
461    else
462        -- might want to try to determin user intent here
463        -- if they populate amort_start_date instead of trx_date
464        if (px_trans_rec.amortization_start_date is not null) then
465           px_trans_rec.transaction_date_entered := px_trans_rec.amortization_start_date;
466        else
467           if (px_trans_rec.transaction_date_entered is null) then
468              l_transaction_date := greatest(l_period_rec.calendar_period_open_date,
469                                            least(sysdate,l_period_rec.calendar_period_close_date));
470              px_trans_rec.transaction_date_entered :=
471                 to_date(to_char(l_transaction_date,'DD/MM/YYYY'),'DD/MM/YYYY');
472           end if;
473           px_trans_rec.amortization_start_date := px_trans_rec.transaction_date_entered;
474        end if;
475    end if;
476 
477    -- BUG# 3549470
478    -- remove time stamps from both dates
479 
480    px_trans_rec.transaction_date_entered :=
481       to_date(to_char(px_trans_rec.transaction_date_entered,'DD/MM/YYYY'),'DD/MM/YYYY');
482 
483    px_trans_rec.amortization_start_date :=
484       to_date(to_char(px_trans_rec.amortization_start_date,'DD/MM/YYYY'),'DD/MM/YYYY');
485 
486    if FA_ASSET_VAL_PVT.validate_fully_retired
487           (p_asset_id           => px_asset_hdr_rec.asset_id,
488            p_book               => px_asset_hdr_rec.book_type_code
489           , p_log_level_rec => p_log_level_rec) then
490       fa_srvr_msg.add_message
491           (name       => 'FA_REC_RETIRED',
492            calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
493       raise unp_err;
494    end if;
495 
496    l_flex_structure_num             := fa_cache_pkg.fazcbc_record.accounting_flex_structure;
497 
498    -- check the ccid / set up flex info
499    if not fnd_flex_apis.get_qualifier_segnum
500                 (appl_id          => 101,
501                  key_flex_code    => 'GL#',
502                  structure_number => l_flex_structure_num,
503                  flex_qual_name   => 'GL_BALANCING',
504                  segment_number   => l_account_segnum
505                  ) then
506       fa_srvr_msg.add_message
507          (calling_fn => 'fnd_flex_apis.get_qualifier_segnum', p_log_level_rec => p_log_level_rec);
508       raise unp_err;
509    end if;
510 
511    if not fnd_flex_ext.get_segments
512                 (application_short_name => 'SQLGL',
513                  key_flex_code          => 'GL#',
514                  structure_number       => l_flex_structure_num,
515                  combination_id         => p_unplanned_deprn_rec.code_combination_id,
516                  n_segments             => n_segs,
517                  segments               => all_segments
518                 ) then
519       fa_srvr_msg.add_message
520          (calling_fn => 'fnd_flex_ext.get_segments', p_log_level_rec => p_log_level_rec);
521       raise unp_err;
522    end if;
523 
524    G_expense_account := all_segments(l_account_segnum);
525 
526 
527    -- Check whether the balancing segments values for
528    -- different distributions are same or not.
529 
530    l_status := FALSE;
531 
532    FA_CHK_BALSEG_PKG.check_balancing_segments
533           (book       => px_asset_hdr_rec.book_type_code,
534            asset_id   => px_asset_hdr_rec.asset_id,
535            success    => l_status,
536            calling_fn => 'CLIENT', p_log_level_rec => p_log_level_rec);
537 
538    if not (l_status) then
539       FA_SRVR_MSG.ADD_MESSAGE
540          (calling_fn => 'FA_CHK_BALSEG_PKG.chk_bal_segs',  p_log_level_rec => p_log_level_rec);
541       raise unp_err;
542    end if;
543 
544 
545    -- call the sob cache to get the table of sob_ids
546    if not FA_CACHE_PKG.fazcrsob
547           (x_book_type_code => px_asset_hdr_rec.book_type_code,
548            x_sob_tbl        => l_sob_tbl, p_log_level_rec => p_log_level_rec) then
549       raise unp_err;
550    end if;
551 
552    -- set up the local asset_header and sob_id
553    l_asset_hdr_rec                 := px_asset_hdr_rec;
554 
555 
556    -- loop through each book starting with the primary and
557    -- call the private API for each
558 
559    FOR l_sob_index in 0..l_sob_tbl.count LOOP
560 
561 
562       if (l_sob_index = 0) then
563          l_reporting_flag := 'P';
564       else
565          l_reporting_flag := 'R';
566          l_asset_hdr_rec.set_of_books_id := l_sob_tbl(l_sob_index);
567       end if;
568 
569 
570       -- call the cache to set the sob_id used for rounding and other lower
571       -- level code for each book.
572       if NOT fa_cache_pkg.fazcbcs(X_book => px_asset_hdr_rec.book_type_code,
573                                   X_set_of_books_id => l_asset_hdr_rec.set_of_books_id,
574                                   p_log_level_rec => p_log_level_rec) then
575          raise unp_err;
576       end if;
577 
578       -- load the old structs
579       if not FA_UTIL_PVT.get_asset_fin_rec
580               (p_asset_hdr_rec            => l_asset_hdr_rec,
581                px_asset_fin_rec           => l_asset_fin_rec,
582                p_transaction_header_id    => NULL,
583                p_mrc_sob_type_code        => l_reporting_flag
584                , p_log_level_rec => p_log_level_rec) then
585          raise unp_err;
586       end if;
587 
588       if not FA_UTIL_PVT.get_asset_deprn_rec
589               (p_asset_hdr_rec            => l_asset_hdr_rec ,
590                px_asset_deprn_rec         => l_asset_deprn_rec,
591                p_period_counter           => NULL,
592                p_mrc_sob_type_code        => l_reporting_flag
593                , p_log_level_rec => p_log_level_rec) then
594          raise unp_err;
595       end if;
596       -- set the primary structs
597       l_unplanned_deprn_rec           := p_unplanned_deprn_rec; /*Bug#8766910  - Intialize here */
598       -- load the adj structs
599       if (l_sob_index = 0) then
600          l_primary_cost := l_asset_fin_rec.cost;
601          -- do not allow unplanned on member assets in group centric
602          -- setups...   will allow when tracking is enabled
603 
604          if (l_asset_fin_rec.group_asset_id is not null and
605              l_asset_fin_rec.tracking_method is null) then
606             fa_srvr_msg.add_message
607                (name       => '***FA_NO_UNPLANNED_MEMBER***',
608                 calling_fn => l_calling_fn,
609                 p_log_level_rec => p_log_level_rec);
610             raise unp_err;
611          end if;
612 
613       else
614          l_avg_rate:= l_asset_fin_rec.cost / l_primary_cost;
615          l_unplanned_deprn_rec.unplanned_amount := l_unplanned_deprn_rec.unplanned_amount * l_avg_rate;
616 
617          if not FA_UTILS_PKG.faxrnd(l_unplanned_deprn_rec.unplanned_amount,
618                                     px_asset_hdr_rec.book_type_code,
619                                     l_asset_hdr_rec.set_of_books_id,
620                                     p_log_level_rec => p_log_level_rec) then
621              raise unp_err;
622          end if;
623 
624       end if;
625 
626 
627       if (p_log_level_rec.statement_level) then
628          fa_debug_pkg.add(l_calling_fn, 'call private do_unplanned', px_trans_rec.calling_interface, p_log_level_rec => p_log_level_rec);
629       end if;
630 
631       -- this could be broken into a seperate do_all_books function if desired
632       -- call the private api for the primary book
633       if not FA_UNPLANNED_PVT.do_unplanned
634              (px_trans_rec              => px_trans_rec,
635               p_asset_hdr_rec           => l_asset_hdr_rec,
636               p_asset_desc_rec          => p_asset_desc_rec,
637               p_asset_type_rec          => p_asset_type_rec,
638               p_asset_cat_rec           => p_asset_cat_rec,
639               p_asset_fin_rec           => l_asset_fin_rec,    -- mrc
640               p_asset_deprn_rec         => l_asset_deprn_rec,  -- mrc
641               p_unplanned_deprn_rec     => l_unplanned_deprn_rec,
642               p_period_rec              => l_period_rec,
643               p_mrc_sob_type_code       => l_reporting_flag
644              , p_log_level_rec => p_log_level_rec) then
645          raise unp_err;
646       end if;
647 
648 
649       if (p_log_level_rec.statement_level) then
650          fa_debug_pkg.add(l_calling_fn, 'Done FA_UNPLANNED_PVT.do_unplanned', ' ',  p_log_level_rec => p_log_level_rec);
651       end if;
652 
653       if (l_sob_index = 0) then
654 
655          -- set the primary structs
656          -- no need anymore - using locals anyway
657          -- Code hook for IAC
658          if (FA_IGI_EXT_PKG.IAC_Enabled) then
659             if not FA_IGI_EXT_PKG.Do_Unplanned(
660                     p_trans_rec               => px_trans_rec,
661                     p_asset_hdr_rec           => px_asset_hdr_rec,
662                     p_asset_cat_rec           => p_asset_cat_rec,
663                     p_asset_desc_rec          => p_asset_desc_rec,
664                     p_asset_type_rec          => p_asset_type_rec,
665                     p_unplanned_deprn_rec     => p_unplanned_deprn_rec,
666                     p_period_rec              => l_period_rec,
667                     p_calling_function        => 'FA_UNPLANNED_PUB.Do_Unplanned') then raise unp_err;
668             end if;
669 
670          end if; -- (FA_IGI_EXT_PKG.IAC_Enabled)
671 
672       else
673          -- insert rates row into mc_books_rates
674          MC_FA_UTILITIES_PKG.insert_books_rates
675            (p_set_of_books_id              => l_asset_hdr_rec.set_of_books_id,
676             p_asset_id                     => l_asset_hdr_rec.asset_id,
677             p_book_type_code               => l_asset_hdr_rec.book_type_code,
678             p_transaction_header_id        => px_trans_rec.transaction_header_id,
679             p_invoice_transaction_id       => NULL,
680             p_exchange_date                => px_trans_rec.transaction_date_entered,
681             p_cost                         => 0,
682             p_exchange_rate                => l_avg_rate,
683             p_avg_exchange_rate            => l_avg_rate,
684             p_last_updated_by              => px_trans_rec.who_info.last_updated_by,
685             p_last_update_date             => px_trans_rec.who_info.last_update_date,
686             p_last_update_login            => px_trans_rec.who_info.last_update_login,
687             p_complete                     => 'Y',
688             p_trigger                      => 'unplanned api',
689             p_currency_code                => l_asset_hdr_rec.set_of_books_id, p_log_level_rec => p_log_level_rec);
690       end if;    -- primary book
691 
692 
693       -- ENERGY
694       -- Eventually, following allocation process should not happen when user performs
695       -- unplan from FAXASSET.  For rel 11, I modified the condition so that the
696       -- allocation process will differ to Process Group Adjustment only if the deprn
697       -- basis rule is ENERGY PERIOD END BALANCE'.
698       --      if (px_trans_rec.calling_interface <> 'FAXASSET') then
699 
700       if (not (px_trans_rec.calling_interface = 'FAXASSET' and
701                  fa_cache_pkg.fazcdrd_record.rule_name = 'ENERGY PERIOD END BALANCE')) then
702 
703          -- Calcualte Group Assets to which the processed member asset belongs
704          -- This is only when the Tracking Method is ALLOCATE.
705 
706          -- If the processed asset is GROUP asset and tracking method is 'ALLOCATE',
707          -- Call TRACK_ASSETS to allocate unplanned amount into members.
708          -- For track member feature
709          -- Only when the unplanned depreciation is kicked from group asset whose tracking method is
710          -- ALLOCATE, system needs to allocate the entered unplanned depreciation amount into
711          -- members.
712 
713          if l_asset_fin_rec.group_asset_id is null and
714             nvl(l_asset_fin_rec.tracking_method,'OTHER') = 'ALLOCATE' then
715 
716             if not fa_cache_pkg.fazccmt (l_asset_fin_rec.deprn_method_code,l_asset_fin_rec.life_in_months, p_log_level_rec => p_log_level_rec) then
717                fa_srvr_msg.add_message (calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
718                raise unp_err;
719             end if;
720 
721 
722             l_ret_code := FA_TRACK_MEMBER_PVT.TRACK_ASSETS
723                               (P_book_type_code             => px_asset_hdr_rec.book_type_code,
724                                P_group_asset_id             => px_asset_hdr_rec.asset_id,
725                                P_period_counter             => l_period_rec.period_num,
726                                P_fiscal_year                => l_period_rec.fiscal_year,
727                                P_group_deprn_basis          => fa_cache_pkg.fazccmt_record.deprn_basis_rule,
728                                P_group_exclude_salvage      => fa_cache_pkg.fazccmt_record.exclude_salvage_value_flag,
729                                P_group_bonus_rule           => l_asset_fin_rec.bonus_rule,
730                                P_group_deprn_amount         => l_unplanned_deprn_rec.unplanned_amount,
731                                P_group_bonus_amount         => 0,
732                                P_tracking_method            => l_asset_fin_rec.tracking_method,
733                                P_allocate_to_fully_ret_flag => l_asset_fin_rec.allocate_to_fully_ret_flag,
734                                P_allocate_to_fully_rsv_flag => l_asset_fin_rec.allocate_to_fully_rsv_flag,
735                                P_excess_allocation_option   => l_asset_fin_rec.excess_allocation_option,
736                                P_subtraction_flag           => 'N',
737                                P_group_level_override       => l_group_level_override,
738                                P_transaction_date_entered   => px_trans_rec.transaction_date_entered,
739                                P_mode                       => 'UNPLANNED',
740                                P_MRC_SOB_TYPE_CODE          => l_reporting_flag,
741                                P_SET_OF_BOOKS_ID            => l_asset_hdr_rec.set_of_books_id,
742 
743                                X_new_deprn_amount           => x_group_deprn_amount,
744                                X_new_bonus_amount           => x_group_bonus_amount,  p_log_level_rec => p_log_level_rec);
745 
746             if l_ret_code <> 0 then
747                raise unp_err;
748             elsif x_group_deprn_amount <> l_unplanned_deprn_rec.unplanned_amount then
749                raise unp_err;
750             end if;
751 
752          end if;
753 
754 
755       end if;
756 
757       -- If the asset is member asset and the group asset has tracking method, ALLOCATE,
758       -- Need to process Group Level Unplanned Depreciation Calculation.
759 
760       if l_asset_fin_rec.group_asset_id is not null and
761          nvl(l_asset_fin_rec.tracking_method,'OTHER') = 'ALLOCATE' then
762 
763          -- Copy group asset id to l_asset_hdr_rec.asset_id and get asset_fin_rec for the group
764          -- Copy member transaction header id to l_grp_trans_rec.member_transaction_header_id
765          -- and get new transaction_header_id for the group
766 
767          l_grp_asset_hdr_rec := px_asset_hdr_rec;
768          l_grp_asset_hdr_rec.asset_id := l_asset_fin_rec.group_asset_id;
769 
770          l_grp_trans_rec := px_trans_rec;
771          l_grp_trans_rec.member_transaction_header_id := px_trans_rec.transaction_header_id;
772 
773          select fa_transaction_headers_s.nextval
774            into l_grp_trans_rec.transaction_header_id
775            from dual;
776 
777          -- check period of addition for the group
778 
779          if not FA_ASSET_VAL_PVT.validate_period_of_addition
780             (p_asset_id            => l_grp_asset_hdr_rec.asset_id,
781              p_book                => l_grp_asset_hdr_rec.book_type_code,
782              p_mode                => 'ABSOLUTE',
783              px_period_of_addition => l_grp_asset_hdr_rec.period_of_addition
784             , p_log_level_rec => p_log_level_rec) then
785             raise unp_err;
786          end if;
787 
788          -- pop the asset type
789          if not FA_UTIL_PVT.get_asset_type_rec
790            (p_asset_hdr_rec         => l_grp_asset_hdr_rec,
791             px_asset_type_rec       => l_grp_asset_type_rec,
792             p_date_effective        => null
793             , p_log_level_rec => p_log_level_rec) then
794             raise unp_err;
795          end if;
796 
797          -- pop the asset category
798          if not FA_UTIL_PVT.get_asset_cat_rec
799             (p_asset_hdr_rec         => l_grp_asset_hdr_rec,
800              px_asset_cat_rec        => l_grp_asset_cat_rec,
801              p_date_effective        => null
802             , p_log_level_rec => p_log_level_rec) then
803            raise unp_err;
804          end if;
805 
806          -- pop the asset desc (needed for current units)
807          if not FA_UTIL_PVT.get_asset_desc_rec
808             (p_asset_hdr_rec         => l_grp_asset_hdr_rec,
809              px_asset_desc_rec       => l_grp_asset_desc_rec
810             , p_log_level_rec => p_log_level_rec) then
811            raise unp_err;
812          end if;
813 
814          if (l_grp_asset_type_rec.asset_type <> 'CAPITALIZED' and
815              l_grp_asset_type_rec.asset_type <> 'GROUP') then
816            fa_srvr_msg.add_message
817             (name       => '** NO_UNPLANNED_NON_CAP **',
818              calling_fn => l_calling_fn,
819              p_log_level_rec => p_log_level_rec);
820            raise unp_err;
821          end if;
822 
823          -- check / default the trx info (This process assumes Group Asset)
824          -- R12 conditional handling
825          if (l_grp_asset_hdr_rec.period_of_addition = 'Y' and
826              G_release = 11) then
827              l_grp_trans_rec.transaction_type_code := 'GROUP ADDITION';
828          else
829              l_grp_trans_rec.transaction_type_code := 'GROUP ADJUSTMENT';
830          end if;
831 
832          l_grp_trans_rec.transaction_subtype := 'AMORTIZED';
833          l_grp_trans_rec.transaction_key := 'UA';
834 
835          if FA_ASSET_VAL_PVT.validate_fully_retired
836             (p_asset_id           => l_grp_asset_hdr_rec.asset_id,
837              p_book               => l_grp_asset_hdr_rec.book_type_code
838             , p_log_level_rec => p_log_level_rec) then
839            fa_srvr_msg.add_message
840             (name       => '*** fully retired ***',
841              calling_fn => l_calling_fn,
842              p_log_level_rec => p_log_level_rec);
843            raise unp_err;
844          end if;
845 
846          -- Check whether the balancing segments values for
847          -- different distributions are same or not.
848 
849          l_status := FALSE;
850 
851          FA_CHK_BALSEG_PKG.check_balancing_segments
852              (book       => l_grp_asset_hdr_rec.book_type_code,
853               asset_id   => l_grp_asset_hdr_rec.asset_id,
854               success    => l_status,
855               calling_fn => 'CLIENT', p_log_level_rec => p_log_level_rec);
856 
857 --???  message needed here?
858         if not (l_status) then
859            FA_SRVR_MSG.ADD_MESSAGE
860                 (NAME => '***flex fail***',
861                  CALLING_FN => l_calling_fn,
862                  p_log_level_rec => p_log_level_rec);
863            raise unp_err;
864         end if;
865 
866         -- load the old structs
867         if not FA_UTIL_PVT.get_asset_fin_rec
868               (p_asset_hdr_rec            => l_grp_asset_hdr_rec,
869                px_asset_fin_rec           => l_grp_asset_fin_rec,
870                p_transaction_header_id    => NULL,
871                p_mrc_sob_type_code        => l_reporting_flag
872                , p_log_level_rec => p_log_level_rec) then
873            raise unp_err;
874         end if;
875 
876         --HH validate disabled_flag
877         --Doing this just as a precaustion in case the api get called directly.
878         --The form, if used, won't allow the trx as the button is not shown.
879         if not FA_ASSET_VAL_PVT.validate_disabled_flag
880                   (p_group_asset_id => l_grp_asset_hdr_rec.asset_id,
881                    p_book_type_code => l_grp_asset_hdr_rec.book_type_code,
882                    p_old_flag       => l_grp_asset_fin_rec.disabled_flag,
883                    p_new_flag       => l_grp_asset_fin_rec.disabled_flag
884                   , p_log_level_rec => p_log_level_rec) then
885             fa_srvr_msg.add_message
886                    (name       => 'FA_NO_UNPLANNED_DIS_GROUP',
887                     calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
888             raise unp_err;
889         end if; --end HH.
890 
891         if not FA_UTIL_PVT.get_asset_deprn_rec
892               (p_asset_hdr_rec            => l_grp_asset_hdr_rec ,
893                px_asset_deprn_rec         => l_grp_asset_deprn_rec,
894                p_period_counter           => NULL,
895                p_mrc_sob_type_code        => l_reporting_flag
896                , p_log_level_rec => p_log_level_rec) then
897            raise unp_err;
898         end if;
899 
900         -- this could be broken into a seperate do_all_books function if desired
901         -- call the private api for the primary book
902         if not FA_UNPLANNED_PVT.do_unplanned
903              (px_trans_rec              => l_grp_trans_rec,
904               p_asset_hdr_rec           => l_grp_asset_hdr_rec,
905               p_asset_desc_rec          => l_grp_asset_desc_rec,
906               p_asset_type_rec          => l_grp_asset_type_rec,
907               p_asset_cat_rec           => l_grp_asset_cat_rec,
908               p_asset_fin_rec           => l_grp_asset_fin_rec, -- mrc
909               p_asset_deprn_rec         => l_grp_asset_deprn_rec, --mrc
910               p_unplanned_deprn_rec     => l_unplanned_deprn_rec,
911               p_period_rec              => l_period_rec,
912               p_mrc_sob_type_code       => l_reporting_flag
913              , p_log_level_rec => p_log_level_rec) then
914           raise unp_err;
915         end if;
916 
917       end if; -- Group Asset Process
918 
919    end loop;      -- sob loop
920 
921    return true;
922 
923 EXCEPTION
924    when unp_err then
925       fa_srvr_msg.add_message(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
926       return false;
927 
928    when others then
929       fa_srvr_msg.add_sql_error(calling_fn => l_calling_fn, p_log_level_rec => p_log_level_rec);
930       return false;
931 
932 END do_all_books;
933 
934 END FA_UNPLANNED_PUB;