DBA Data[Home] [Help]

PACKAGE BODY: APPS.FA_INVOICE_PVT

Source


1 PACKAGE BODY FA_INVOICE_PVT as
2 /* $Header: FAVINVB.pls 120.23.12010000.2 2009/01/23 21:55:48 bridgway ship $   */
3 
4 G_profile_set_of_books_id  number;
5 G_orig_currency_context    varchar2(64);
6 
7 -- used for tracking the total payables cost cleared for all invoices
8 TYPE payables_cost_rec_type IS RECORD
9      (set_of_books_id              number,
10       payables_cost                number,
11       payables_code_combination_id number,
12       source_dest_code             varchar2(15),
13       source_line_id               number,
14       asset_invoice_id             number);
15 
16 TYPE payables_cost_tbl_type IS TABLE OF payables_cost_rec_type index by binary_integer;
17 
18 -- private prottypes
19 
20 g_print_debug boolean := fa_cache_pkg.fa_print_debug;
21 
22 FUNCTION inv_calc_info
23    (p_asset_hdr_rec          IN     FA_API_TYPES.asset_hdr_rec_type,
24     p_asset_type_rec         IN     FA_API_TYPES.asset_type_rec_type,
25     p_inv_trans_rec          IN     FA_API_TYPES.inv_trans_rec_type,
26     px_inv_rec               IN OUT NOCOPY FA_API_TYPES.inv_rec_type,
27     px_asset_fin_rec_new     IN OUT NOCOPY FA_API_TYPES.asset_fin_rec_type,
28     px_asset_deprn_rec_new   IN OUT NOCOPY FA_API_TYPES.asset_deprn_rec_type,
29     p_log_level_rec          IN fa_api_types.log_level_rec_type default null
30    ) RETURN BOOLEAN;
31 
32 FUNCTION  process_invoice
33    (px_trans_rec               IN OUT NOCOPY FA_API_TYPES.trans_rec_type,
34     px_asset_hdr_rec           IN OUT NOCOPY FA_API_TYPES.asset_hdr_rec_type,
35     p_inv_trans_rec            IN     FA_API_TYPES.inv_trans_rec_type,
36     px_inv_rec                 IN OUT NOCOPY FA_API_TYPES.inv_rec_type,
37     p_inv_rate_rec             IN     FA_API_TYPES.inv_rate_rec_type,
38     p_mrc_sob_type_code        IN     VARCHAR2,
39     p_log_level_rec            in fa_api_types.log_level_rec_type default null
40    ) RETURN BOOLEAN;
41 
42 FUNCTION get_inv_rate
43    (p_trans_rec                IN     FA_API_TYPES.trans_rec_type,
44     p_asset_hdr_rec            IN     FA_API_TYPES.asset_hdr_rec_type,
45     p_inv_trans_rec            IN     FA_API_TYPES.inv_trans_rec_type,
46     px_inv_tbl                 IN OUT NOCOPY FA_API_TYPES.inv_tbl_type,
47     p_log_level_rec            in fa_api_types.log_level_rec_type default null
48    ) RETURN BOOLEAN;
49 
50 FUNCTION post_clearing
51    (p_trans_rec                IN     FA_API_TYPES.trans_rec_type,
52     p_asset_hdr_rec            IN     FA_API_TYPES.asset_hdr_rec_type,
53     p_asset_desc_rec           IN     FA_API_TYPES.asset_desc_rec_type,
54     p_asset_type_rec           IN     FA_API_TYPES.asset_type_rec_type,
55     p_asset_cat_rec            IN     FA_API_TYPES.asset_cat_rec_type,
56     p_inv_trans_rec            IN     FA_API_TYPES.inv_trans_rec_type,
57     p_payables_cost_tbl        IN     payables_cost_tbl_type,
58     p_payables_cost_mrc_tbl    IN     payables_cost_tbl_type,
59     p_log_level_rec            in fa_api_types.log_level_rec_type default null
60    ) RETURN BOOLEAN;
61 
62 -- public function
63 
64 FUNCTION invoice_engine
65    (px_trans_rec               IN OUT NOCOPY FA_API_TYPES.trans_rec_type,
66     px_asset_hdr_rec           IN OUT NOCOPY FA_API_TYPES.asset_hdr_rec_type,
67     p_asset_desc_rec           IN     FA_API_TYPES.asset_desc_rec_type,
68     p_asset_type_rec           IN     FA_API_TYPES.asset_type_rec_type,
69     p_asset_cat_rec            IN     FA_API_TYPES.asset_cat_rec_type,
70     p_asset_fin_rec_adj        IN     FA_API_TYPES.asset_fin_rec_type,
71     x_asset_fin_rec_new           OUT NOCOPY FA_API_TYPES.asset_fin_rec_type,
72     x_asset_fin_mrc_tbl_new       OUT NOCOPY FA_API_TYPES.asset_fin_tbl_type,
73     px_inv_trans_rec           IN OUT NOCOPY FA_API_TYPES.inv_trans_rec_type,
74     px_inv_tbl                 IN OUT NOCOPY FA_API_TYPES.inv_tbl_type,
75     x_asset_deprn_rec_new         OUT NOCOPY FA_API_TYPES.asset_deprn_rec_type,
76     x_asset_deprn_mrc_tbl_new     OUT NOCOPY FA_API_TYPES.asset_deprn_tbl_type,
77     p_calling_fn               IN     VARCHAR2,
78     p_log_level_rec            in fa_api_types.log_level_rec_type default null
79    ) RETURN BOOLEAN IS
80 
81    l_rowid                   varchar2(100);  -- placeholder for table handlers
82    l_row_count               number := 0;
83    l_deprn_count             number := 0;
84    l_create_new_row          varchar2(3);
85    l_current_fa_cost         number;
86    l_current_pa_cost         number;
87    l_current_source_line_id  number;
88    l_inv_rec_fa_cost_primary number;
89 
90    -- local structs used for manipulation
91    l_asset_hdr_rec           FA_API_TYPES.asset_hdr_rec_type;
92    l_inv_rec                 FA_API_TYPES.inv_rec_type;
93    l_inv_rate_rec            FA_API_TYPES.inv_rate_rec_type;
94    l_asset_deprn_rec_adj     FA_API_TYPES.asset_deprn_rec_type;
95 
96    l_count                   number;
97 
98    -- mrc checks
99    l_sob_tbl                 FA_CACHE_PKG.fazcrsob_sob_tbl_type;
100    l_reporting_flag          varchar2(1);
101    l_mrc_rate_found_count    number := 0;
102    l_mrc_rate_index          number := 0;
103    l_mrc_fin_book_rec        number := 0;
104    l_mrc_deprn_rec           number := 0;
105 
106    l_mrc_date                date;
107 
108    l_payables_cost_tbl       payables_cost_tbl_type;
109    l_payables_cost_mrc_tbl   payables_cost_tbl_type;
110    l_payables_cost_count     number;
111    l_payables_cost_mrc_count number;
112 
113    -- new variables for merging mrc trigger logic
114    l_primary_sob_id          NUMBER;
115    l_exchange_date           date;
116 
117 
118    -- exceptions
119    l_calling_fn              varchar2(35) := 'fa_inv_pvt.invoice_engine';
120    error_found               exception;
121 
122 
123 BEGIN
124 
125     l_asset_hdr_rec              := px_asset_hdr_rec;
126 
127     if (g_print_debug) then
128        fa_debug_pkg.add(l_calling_fn,
129                         'px_inv_tbl.count',
130                         px_inv_tbl.count);
131     end if;
132 
133     if (px_inv_tbl.count = 0) then
134        fa_srvr_msg.add_message(
135            calling_fn => l_calling_fn,
136            name       => '***NO_INVOICES***');
137        raise error_found;
138     end if;
139 
140 
141     --  store the current profile value and then use the definitive value
142     --  for the book in question and check whether this is a reporting book.
143 
144     fnd_profile.get('GL_SET_OF_BKS_ID', G_profile_set_of_books_id);
145     G_orig_currency_context := SUBSTRB(USERENV('CLIENT_INFO'),45,10);
146     fnd_profile.put('GL_SET_OF_BKS_ID', px_asset_hdr_rec.set_of_books_id);
147 
148     if (fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y') then
149 
150        -- call the sob cache to get the table of sob_ids
151        if not FA_CACHE_PKG.fazcrsob
152                (x_book_type_code => px_asset_hdr_rec.book_type_code,
153                 x_sob_tbl        => l_sob_tbl) then
154           raise error_found;
155        end if;
156 
157     end if;
158 
159     -- insert invoice transaction row(only for null values)
160     -- when called for destination asset in a source line transfer it will be populated)
161 
162 
163     if (px_inv_trans_rec.invoice_transaction_id is null) then
164 
165        FA_INVOICE_TRANSACTIONS_PKG.Insert_Row
166          (X_Rowid                     => l_rowid,
167           X_Invoice_Transaction_Id    => px_inv_trans_rec.invoice_transaction_id ,
168           X_Book_Type_Code            => px_asset_hdr_rec.book_type_code,
169           X_Transaction_Type          => px_inv_trans_rec.transaction_type,
170           X_Date_Effective            => sysdate,
171           X_Calling_Fn                => 'FA_INVOICE_API_PKG.invoice_engine'
172          );
173 
174     end if;
175 
176 
177     --  initialize the new fin struct to the incoming values
178     --  in the case of the addition engine, this will be
179     --  populated before hand for fields like DPIS, etc.
180     --  for adjustments, it will be null anyway
181 
182     x_asset_fin_rec_new                           := p_asset_fin_rec_adj;
183     x_asset_fin_rec_new.adjusted_cost             := 0;
184     x_asset_fin_rec_new.original_cost             := 0;
185     -- NOTE: do not set salvage here as it needs to be null
186     -- when not populated do the default percent salvage is used
187     -- within the cal engine
188     -- x_asset_fin_rec_new.salvage_value             := 0;
189     x_asset_fin_rec_new.recoverable_cost          := 0;
190     x_asset_fin_rec_new.adjusted_recoverable_cost := 0;
191     x_asset_fin_rec_new.reval_amortization_basis  := 0;
192     x_asset_fin_rec_new.old_adjusted_cost         := 0;
193     x_asset_fin_rec_new.cost                      := 0;
194 
195     x_asset_deprn_rec_new.deprn_amount             := 0;
196     x_asset_deprn_rec_new.ytd_deprn                := 0;
197     x_asset_deprn_rec_new.deprn_reserve            := 0;
198     x_asset_deprn_rec_new.bonus_ytd_deprn          := 0;
199     x_asset_deprn_rec_new.bonus_deprn_reserve      := 0;
200     x_asset_deprn_rec_new.reval_amortization_basis := 0;
201     x_asset_deprn_rec_new.reval_deprn_expense      := 0;
202     x_asset_deprn_rec_new.reval_ytd_deprn          := 0;
203     x_asset_deprn_rec_new.reval_deprn_reserve      := 0;
204 
205 
206     -- init the mrc fin structs - the fin struct will NOT be populated yet for invoice trx's
207     -- any trx or just adjs via invoice trx?
208 
209 
210     l_count := 0;
211 
212     FOR l_sob_index in 1..l_sob_tbl.count LOOP
213 
214         l_count := l_count + 1;
215         x_asset_fin_mrc_tbl_new(l_count)                     := x_asset_fin_rec_new;
216         x_asset_fin_mrc_tbl_new(l_count).set_of_books_id     := l_sob_tbl(l_sob_index);
217         x_asset_deprn_mrc_tbl_new(l_count)                   := x_asset_deprn_rec_new;
218         x_asset_deprn_mrc_tbl_new(l_count).set_of_books_id   := l_sob_tbl(l_sob_index);
219 
220     end loop;
221 
222 
223     l_asset_hdr_rec.set_of_books_id := px_asset_hdr_rec.set_of_books_id;
224 
225     l_row_count := 0;
226 
227     -- load the inv_rate table for existing source lines
228     -- SLA - this will fetch all currencies at one time
229 
230     if not get_inv_rate
231             (p_trans_rec       => px_trans_rec,
232              p_asset_hdr_rec   => px_asset_hdr_rec,
233              p_inv_trans_rec   => px_inv_trans_rec,
234              px_inv_tbl        => px_inv_tbl) then
235          raise error_found;
236     end if;
237 
238     while (l_row_count < px_inv_tbl.count) loop  -- loop for invoices array
239 
240        l_current_source_line_id := px_inv_tbl(l_row_count + 1).source_line_id;
241 
242        -- call for primary book first
243        -- initialize the invoice structs to be passed
244        -- to process_invoice to the current row in the table.
245        -- The values in this struct will be added to those
246        -- being incremented in the p_fin_new struct.
247 
248 
249        -- load the non financial info into the struct using get utility
250        if ((px_inv_trans_rec.transaction_type <> 'MASS ADDITION' or
251             px_inv_trans_rec.transaction_type <> 'INVOICE ADDITION') and
252             px_inv_tbl(l_row_count + 1).source_line_id is not null) then
253 
254           if (g_print_debug) then
255              fa_debug_pkg.add(l_calling_fn,
256                               'in invoice loop, source_line_id',
257                               px_inv_tbl(l_row_count + 1).source_line_id);
258              fa_debug_pkg.add(l_calling_fn,
259                               'in invoice loop, fa cost',
260                               px_inv_tbl(l_row_count + 1).fixed_assets_cost);
261              fa_debug_pkg.add(l_calling_fn,
262                               'in invoice loop, ap cost',
263                               px_inv_tbl(l_row_count + 1).payables_cost);
264           end if;
265 
266           -- set the source_line and sob in the local struct
267           l_inv_rec.source_line_Id := px_inv_tbl(l_row_count + 1).source_line_id;
268 
269           if not FA_UTIL_PVT.get_inv_rec
270                    (px_inv_rec          => l_inv_rec,
271                     p_mrc_sob_type_code => 'P',
272                     p_inv_trans_rec     => px_inv_trans_rec) then
273              raise error_found;
274           end if;
275 
276           if (g_print_debug) then
277              fa_debug_pkg.add(l_calling_fn,
278                               'after get_inv_rec, deleted_flag',
279                               l_inv_rec.deleted_flag);
280              fa_debug_pkg.add(l_calling_fn,
281                               'after get_inv_rec, dep_in_grp_flag',
282                               l_inv_rec.depreciate_in_group_flag);
283           end if;
284 
285           -- set the current fa cost from the get_inv call
286           l_current_fa_cost := l_inv_rec.fixed_assets_cost;
287           l_current_pa_cost := l_inv_rec.payables_cost;
288 
289           -- reset any info that might change as a result of the invoice transaction
290           -- remember that the fa cost is always the delta thus, it goes to 0 if there
291           -- is no change.  for deletes and reinstates only the depreciate can flag
292           -- however, we need the current cost in order to set the delta info in the
293           -- fin rec struct. For deletes, we flip the sign and later on flip it back
294 
295           -- adding new flag for group requirements to allow for portions of
296           -- a cip asset's cost to be included in the depreciable basis
297           -- when set to Y, the cip_cost decreases!
298 
299           if (px_inv_trans_rec.transaction_type = 'INVOICE DELETE') then
300              if (l_inv_rec.deleted_flag = 'YES') then
301                 fa_srvr_msg.add_message(
302                     calling_fn => l_calling_fn,
303                     name       => '***FA_INV_ALREADY_DEL***');
304                 raise error_found;
305              end if;
306 
307              l_inv_rec.deleted_flag      := 'YES';
308              l_inv_rec.Fixed_Assets_Cost := -l_current_fa_cost;
309              l_inv_rec.Payables_Cost     := -l_current_pa_cost;
310              if (p_asset_type_rec.asset_type = 'CIP') then
311                 if (nvl(l_inv_rec.depreciate_in_group_flag, 'N') = 'Y') then
312                    l_inv_rec.Cip_Cost          := -l_current_fa_cost;
313                 else
314                    l_inv_rec.Cip_Cost          := 0;
315                 end if;
316              end if;
317           elsif (px_inv_trans_rec.transaction_type =  'INVOICE REINSTATE') then
318              if (nvl(l_inv_rec.deleted_flag, 'NO') = 'NO') then
319                 fa_srvr_msg.add_message(
320                     calling_fn => l_calling_fn,
321                     name       => '***FA_INV_ALREADY_REINS***');
322                 raise error_found;
323              end if;
324 
325              l_inv_rec.deleted_flag      := 'NO';
326              l_inv_rec.Fixed_Assets_Cost := l_current_fa_cost;
327              l_inv_rec.Payables_Cost     := l_current_pa_cost;
328              if (p_asset_type_rec.asset_type = 'CIP') then
329                 if (nvl(l_inv_rec.depreciate_in_group_flag, 'N') = 'Y') then
330                    l_inv_rec.Cip_Cost          := l_current_fa_cost;
331                 else
332                    l_inv_rec.Cip_Cost          := 0;
333                 end if;
334              end if;
335           elsif (px_inv_trans_rec.transaction_type = 'INVOICE DEP') then
336              if (l_inv_rec.depreciate_in_group_flag = 'Y') then
337                 fa_srvr_msg.add_message(
338                     calling_fn => l_calling_fn,
339                     name       => '***FA_INV_ALREADY_DEP***');
340                 raise error_found;
341              end if;
342 
343              if (p_asset_type_rec.asset_type = 'CIP') then
344                 l_inv_rec.depreciate_in_group_flag := 'Y';
345                 l_inv_rec.Cip_Cost          := -l_current_fa_cost;
346                 l_inv_rec.Fixed_Assets_Cost := 0;
347                 l_inv_rec.Payables_Cost     := 0;
348              else
349                 raise error_found;
350              end if;
351           elsif (px_inv_trans_rec.transaction_type = 'INVOICE NO DEP') then
352              if (nvl(l_inv_rec.depreciate_in_group_flag, 'N') = 'N') then
353                 fa_srvr_msg.add_message(
354                     calling_fn => l_calling_fn,
355                     name       => '***FA_INV_ALREADY_NO_DEP***');
356                 raise error_found;
357              end if;
358 
359              if (p_asset_type_rec.asset_type = 'CIP') then
360                 l_inv_rec.depreciate_in_group_flag := 'N';
361                 l_inv_rec.Cip_Cost          := l_current_fa_cost;
362                 l_inv_rec.Fixed_Assets_Cost := 0;
363                 l_inv_rec.Payables_Cost     := 0;
364              else
365                 raise error_found;
366              end if;
367           else
368              l_inv_rec.Fixed_Assets_Cost              := nvl(px_inv_tbl(l_row_count + 1).Fixed_Assets_Cost, 0);
369 
370              -- do not allow delta cost to exceed the current fa cost
371              -- in the case of an invoice transfer
372              if (px_inv_trans_rec.transaction_type = 'INVOICE TRANSFER' and
373                  px_inv_tbl(l_row_count + 1).source_line_id is not null) then
374 
375                 -- can't transfer nothing on non-zero line
376                 if (l_inv_rec.Fixed_Assets_Cost = 0) then
377                    if (l_current_fa_cost <> 0) then
378                       fa_srvr_msg.add_message(
379                           calling_fn  => l_calling_fn,
380                           name        => 'FA_TFRINV_NONE_TFR_COST');
381                       raise error_found;
382                    end if;
383                 -- must transfer zero on a zero line
384                 elsif (l_current_fa_cost = 0) then
385                    if (l_inv_rec.Fixed_Assets_Cost <> 0) then
386                       fa_srvr_msg.add_message(
387                           calling_fn  => l_calling_fn,
388                           name        => 'FA_TFRINV_ZERO_TFR_COST');
389                       raise error_found;
390                    end if;
391                 -- may not increase the value (due to same sign)
392                 elsif (sign(l_inv_rec.Fixed_Assets_Cost) = sign(l_current_fa_cost)) then
393                    fa_srvr_msg.add_message(
394                        calling_fn  => l_calling_fn,
395                        name        => 'FA_TFRINV_NOT_BTWN_TFR_COST');
396                    raise error_found;
397                 -- may not transfer more than current value
398                 elsif ((sign(l_current_fa_cost) > 0 and
399                         -l_inv_rec.Fixed_Assets_Cost > l_current_fa_cost) or
400                        (sign(l_current_fa_cost) < 0 and
401                         -l_inv_rec.Fixed_Assets_Cost < l_current_fa_cost)) then
402                    fa_srvr_msg.add_message(
403                        calling_fn  => l_calling_fn,
404                        name        => 'FA_TFRINV_NOT_BTWN_TFR_COST');
405                    raise error_found;
406                 end if;
407 
408              end if;  -- invoice transfer
409 
410              -- BUG# 2314466
411              -- do not use nvl here as we want to insert null intentionally
412              -- and the null value is needed to correctly derive original cost later
413              l_inv_rec.Payables_Cost                  := px_inv_tbl(l_row_count + 1).Payables_Cost;
414 
415              if (g_print_debug) then
416                 fa_debug_pkg.add(l_calling_fn,
417                                  'asset_type',
418                                  p_asset_type_rec.asset_type);
419              end if;
420 
421              -- group related stuff - insure we reflect changes to fa_cost in cip_cost
422              l_inv_rec.depreciate_in_group_flag := l_inv_rec.depreciate_in_group_flag;
423 
424              if (p_asset_type_rec.asset_type = 'CIP') then
425                 if (nvl(l_inv_rec.depreciate_in_group_flag, 'N') = 'Y') then
426                    l_inv_rec.Cip_Cost          := 0;
427                 else
428                    l_inv_rec.Cip_Cost          := nvl(px_inv_tbl(l_row_count + 1).Fixed_Assets_Cost, 0);
429                 end if;
430              end if;
431 
432           end if;
433 
434           if (g_print_debug) then
435               fa_debug_pkg.add(l_calling_fn,
436                                'cip_cost',
437                                l_inv_rec.Cip_Cost);
438               fa_debug_pkg.add(l_calling_fn,
439                                'fa_cost',
440                                l_inv_rec.fixed_assets_Cost);
441           end if;
442 
443 
444           l_inv_rec.Po_Vendor_Id                   := nvl(px_inv_tbl(l_row_count + 1).Po_Vendor_Id,
445                                                              l_inv_rec.Po_Vendor_Id);
446           l_inv_rec.Asset_Invoice_Id               := nvl(px_inv_tbl(l_row_count + 1).Asset_Invoice_Id,
447                                                              l_inv_rec.Asset_Invoice_Id);
448           l_inv_rec.Po_Number                      := nvl(px_inv_tbl(l_row_count + 1).Po_Number,
449                                                              l_inv_rec.Po_Number);
450           l_inv_rec.Invoice_Number                 := nvl(px_inv_tbl(l_row_count + 1).Invoice_Number,
451                                                              l_inv_rec.Invoice_Number);
452           l_inv_rec.Payables_Batch_Name            := nvl(px_inv_tbl(l_row_count + 1).Payables_Batch_Name,
453                                                              l_inv_rec.Payables_Batch_Name);
454           l_inv_rec.Payables_Code_Combination_Id   := nvl(px_inv_tbl(l_row_count + 1).Payables_Code_Combination_Id,
455                                                              l_inv_rec.Payables_Code_Combination_Id);
456           l_inv_rec.Feeder_System_Name             := nvl(px_inv_tbl(l_row_count + 1).Feeder_System_Name,
457                                                              l_inv_rec.Feeder_System_Name);
458           l_inv_rec.Create_Batch_Date              := nvl(px_inv_tbl(l_row_count + 1).Create_Batch_Date,
459                                                              l_inv_rec.Create_Batch_Date);
460           l_inv_rec.Create_Batch_Id                := nvl(px_inv_tbl(l_row_count + 1).Create_Batch_Id,
461                                                              l_inv_rec.Create_Batch_Id);
462           l_inv_rec.Invoice_Date                   := nvl(px_inv_tbl(l_row_count + 1).Invoice_Date,
463                                                              l_inv_rec.Invoice_Date);
464           l_inv_rec.Payables_Cost                  := nvl(px_inv_tbl(l_row_count + 1).Payables_Cost,
465                                                              l_inv_rec.Payables_Cost);
466           l_inv_rec.Post_Batch_Id                  := nvl(px_inv_tbl(l_row_count + 1).Post_Batch_Id,
467                                                              l_inv_rec.Post_Batch_Id);
468           l_inv_rec.Invoice_Id                     := nvl(px_inv_tbl(l_row_count + 1).Invoice_Id,
469                                                              l_inv_rec.Invoice_Id);
470           l_inv_rec.Ap_Distribution_Line_Number    := nvl(px_inv_tbl(l_row_count + 1).Ap_Distribution_Line_Number,
471                                                              l_inv_rec.Ap_Distribution_Line_Number);
472           l_inv_rec.Payables_Units                 := nvl(px_inv_tbl(l_row_count + 1).Payables_Units,
473                                                              l_inv_rec.Payables_Units);
474           l_inv_rec.Split_Merged_Code              := nvl(px_inv_tbl(l_row_count + 1).Split_Merged_Code,
475                                                              l_inv_rec.Split_Merged_Code);
476           l_inv_rec.Description                    := nvl(px_inv_tbl(l_row_count + 1).Description,
477                                                              l_inv_rec.Description);
478           l_inv_rec.Parent_Mass_Addition_Id        := nvl(px_inv_tbl(l_row_count + 1).Parent_Mass_Addition_Id,
479                                                              l_inv_rec.Parent_Mass_Addition_Id);
480           l_inv_rec.Attribute1                     := nvl(px_inv_tbl(l_row_count + 1).Attribute1,
481                                                              l_inv_rec.Attribute1);
482           l_inv_rec.Attribute2                     := nvl(px_inv_tbl(l_row_count + 1).Attribute2,
483                                                              l_inv_rec.Attribute2);
484           l_inv_rec.Attribute3                     := nvl(px_inv_tbl(l_row_count + 1).Attribute3,
485                                                              l_inv_rec.Attribute3);
486           l_inv_rec.Attribute4                     := nvl(px_inv_tbl(l_row_count + 1).Attribute4,
487                                                              l_inv_rec.Attribute4);
488           l_inv_rec.Attribute5                     := nvl(px_inv_tbl(l_row_count + 1).Attribute5,
489                                                              l_inv_rec.Attribute5);
490           l_inv_rec.Attribute6                     := nvl(px_inv_tbl(l_row_count + 1).Attribute6,
491                                                              l_inv_rec.Attribute6);
492           l_inv_rec.Attribute7                     := nvl(px_inv_tbl(l_row_count + 1).Attribute7,
493                                                              l_inv_rec.Attribute7);
494           l_inv_rec.Attribute8                     := nvl(px_inv_tbl(l_row_count + 1).Attribute8,
495                                                              l_inv_rec.Attribute8);
496           l_inv_rec.Attribute9                     := nvl(px_inv_tbl(l_row_count + 1).Attribute9,
497                                                              l_inv_rec.Attribute9);
498           l_inv_rec.Attribute10                    := nvl(px_inv_tbl(l_row_count + 1).Attribute10,
499                                                              l_inv_rec.Attribute10);
500           l_inv_rec.Attribute11                    := nvl(px_inv_tbl(l_row_count + 1).Attribute11,
501                                                              l_inv_rec.Attribute11);
502           l_inv_rec.Attribute12                    := nvl(px_inv_tbl(l_row_count + 1).Attribute12,
503                                                              l_inv_rec.Attribute12);
504           l_inv_rec.Attribute13                    := nvl(px_inv_tbl(l_row_count + 1).Attribute13,
505                                                              l_inv_rec.Attribute13);
506           l_inv_rec.Attribute14                    := nvl(px_inv_tbl(l_row_count + 1).Attribute14,
507                                                              l_inv_rec.Attribute14);
508           l_inv_rec.Attribute15                    := nvl(px_inv_tbl(l_row_count + 1).Attribute15,
509                                                              l_inv_rec.Attribute15);
510           l_inv_rec.Attribute_Category_Code        := nvl(px_inv_tbl(l_row_count + 1).Attribute_Category_Code,
511                                                              l_inv_rec.Attribute_Category_Code);
512           l_inv_rec.Unrevalued_Cost                := nvl(px_inv_tbl(l_row_count + 1).Unrevalued_Cost,
513                                                              l_inv_rec.Unrevalued_Cost);
514           l_inv_rec.Merged_Code                    := nvl(px_inv_tbl(l_row_count + 1).Merged_Code,
515                                                              l_inv_rec.Merged_Code);
516           l_inv_rec.Split_Code                     := nvl(px_inv_tbl(l_row_count + 1).Split_Code,
517                                                              l_inv_rec.Split_Code);
518           l_inv_rec.Merge_Parent_Mass_Additions_Id := nvl(px_inv_tbl(l_row_count + 1).Merge_Parent_Mass_Additions_Id,
519                                                              l_inv_rec.Merge_Parent_Mass_Additions_Id);
520           l_inv_rec.Split_Parent_Mass_Additions_Id := nvl(px_inv_tbl(l_row_count + 1).Split_Parent_Mass_Additions_Id,
521                                                              l_inv_rec.Split_Parent_Mass_Additions_Id);
522           l_inv_rec.Project_Asset_Line_Id          := nvl(px_inv_tbl(l_row_count + 1).Project_Asset_Line_Id,
523                                                              l_inv_rec.Project_Asset_Line_Id);
524           l_inv_rec.Project_Id                     := nvl(px_inv_tbl(l_row_count + 1).Project_Id,
525                                                              l_inv_rec.Project_Id);
526           l_inv_rec.Task_Id                        := nvl(px_inv_tbl(l_row_count + 1).Task_Id,
527                                                              l_inv_rec.Task_Id);
528 
529           -- added for R12
530           l_inv_rec.invoice_distribution_id        := nvl(px_inv_tbl(l_row_count + 1).invoice_distribution_id,
531                                                              l_inv_rec.invoice_distribution_id);
532           l_inv_rec.invoice_line_number            := nvl(px_inv_tbl(l_row_count + 1).invoice_line_number,
533                                                              l_inv_rec.invoice_line_number);
534           l_inv_rec.po_distribution_id             := nvl(px_inv_tbl(l_row_count + 1).po_distribution_id,
535                                                              l_inv_rec.po_distribution_id);
536 
537 
538 
539 
540           l_inv_rate_rec     := null;
541 
542        else
543 
544           l_current_fa_cost := 0;
545           l_current_pa_cost := 0;
546 
547           l_inv_rec          := px_inv_tbl(l_row_count + 1);
548           l_inv_rate_rec     := null;
549 
550           if (p_asset_type_rec.asset_type = 'CIP') then
551              if (nvl(l_inv_rec.depreciate_in_group_flag, 'N') = 'Y') then
552                 l_inv_rec.Cip_Cost          := 0;
553              else
554                 l_inv_rec.Cip_Cost          := nvl(px_inv_tbl(l_row_count + 1).Fixed_Assets_Cost, 0);
555              end if;
556           end if;
557 
558           if (g_print_debug) then
559               fa_debug_pkg.add(l_calling_fn,
560                                'cip_cost',
561                                l_inv_rec.Cip_Cost);
562               fa_debug_pkg.add(l_calling_fn,
563                                'fa_cost',
564                                l_inv_rec.fixed_assets_Cost);
565           end if;
566 
567        end if;
568 
569        -- call invoice calc
570 
571        if not inv_calc_info
572                (p_asset_hdr_rec         => px_asset_hdr_rec,
573                 p_asset_type_rec        => p_asset_type_rec,
574                 p_inv_trans_rec         => px_inv_trans_rec,
575                 px_inv_rec              => l_inv_rec,
576                 px_asset_fin_rec_new    => x_asset_fin_rec_new,
577                 px_asset_deprn_rec_new  => x_asset_deprn_rec_new
578                ) then
579            raise error_found;
580        end if;
581 
582        if (g_print_debug) then
583           fa_debug_pkg.add(l_calling_fn,
584                            'done',
585                            'inv_calc_info');
586        end if;
587 
588        -- In the case of a source line transfer on the source side,
589        -- check the delta and original cost values for the primary book
590        -- if they are equal and opposite (i.e. tranferring out all of the cost)
591        -- we will only terminate the existing row, not create a new one.
592        -- SRC line ret should have same behavior with inv transfer.
593        -- Reisntate is not handle below since existing cost +
594        -- cost to reinstate won't be zero.
595 
596        -- In case of  full source line retirement, new line with 0 amount
597        -- still needs to be created.
598 
599        -- bug 2543777 (old 2557171)
600 
601        -- also adding a join to asset_id to insure the invoice line
602        -- in question belongs to the asset on which transaction is being
603        -- performed.  get_inv_rec doesn't have such a parameter so doing
604        -- this to avoid dependancies.
605 
606        if (g_print_debug) then
607           fa_debug_pkg.add(l_calling_fn,
608                            'entering',
609                            'new row selection code');
610        end if;
611 
612        if (px_inv_trans_rec.transaction_type in ('INVOICE TRANSFER', 'REINSTATEMENT') and
613            l_inv_rec.source_line_id is not null) then
614 
615           select decode(fixed_assets_cost + l_inv_rec.fixed_assets_cost,
616                         0, 'NO',
617                         'YES')
618             into l_create_new_row
619             from fa_asset_invoices
620            where source_line_id = l_inv_rec.source_line_id
621              and asset_id       = px_asset_hdr_rec.asset_Id;
622 
623        else
624           if (l_inv_rec.source_line_id is not null) then
625              select 'YES'
626                into l_create_new_row
627                from fa_asset_invoices
628               where source_line_id = l_inv_rec.source_line_id
629                 and asset_id       = px_asset_hdr_rec.asset_id;
630           else
631              l_create_new_row := 'YES';
632           end if;
633        end if;
634 
635        -- for non-invoice-addition calls, terminate the existing rows for both
636        -- primary and mrc based on the incoming source line id
637 
638        if (g_print_debug) then
639           fa_debug_pkg.add(l_calling_fn,
640                            'terminating',
641                            'invoice rows');
642        end if;
643 
644        if (px_inv_trans_rec.transaction_type <> 'MASS ADDITION'  and
645            px_inv_trans_rec.transaction_type <> 'INVOICE ADDITION') then
646 
647           update fa_asset_invoices
648              set date_ineffective           = sysdate,
649                  invoice_transaction_id_out = px_inv_trans_rec.invoice_transaction_id
650            where source_line_id             = l_inv_rec.source_line_id;
651 
652           update fa_mc_asset_invoices
653              set date_ineffective           = sysdate,
654                  invoice_transaction_id_out = px_inv_trans_rec.invoice_transaction_id
655            where source_line_id             = l_inv_rec.source_line_id;
656 
657        end if;
658 
659 
660        -- BUG# 2622722
661        -- do this for all lines for addition and transfer transactions,
662        -- not just those from mass additions.   note that non-xfr adjustment
663        -- will use the flexbuilt ccids for now. section has been moved up so
664        -- tht the deltas are used, not the final amounts (alos done for mrc below)
665 
666        if (g_print_debug) then
667           fa_debug_pkg.add(l_calling_fn,
668                            'prior to loading payables info, trx_type',
669                            px_inv_trans_rec.transaction_type);
670           fa_debug_pkg.add(l_calling_fn,
671                            'prior to loading payables info, payables_cost',
672                            l_inv_rec.payables_cost);
673        end if;
674 
675        if ((px_inv_trans_rec.transaction_type = 'MASS ADDITION' or
676             px_inv_trans_rec.transaction_type = 'INVOICE TRANSFER' or
677             px_inv_trans_rec.transaction_type = 'INVOICE ADDITION') and
678            l_inv_rec.payables_cost <> 0 ) then
679            l_payables_cost_count := l_payables_cost_tbl.count + 1;
680            l_payables_cost_tbl(l_payables_cost_count).payables_cost :=
681               l_inv_rec.payables_cost;
682            l_payables_cost_tbl(l_payables_cost_count).payables_code_combination_id :=
683               l_inv_rec.payables_code_combination_id;
684            l_payables_cost_tbl(l_payables_cost_count).source_dest_code :=
685               l_inv_rec.source_dest_code;
686 
687            -- for new lines, the source line is is not known
688            -- yet so we assign this after nexval fetch
689            -- l_payables_cost_tbl(l_payables_cost_count).source_line_id :=
690            --   l_inv_rec.source_line_id;
691 
692        end if;
693 
694 
695        if (g_print_debug) then
696           fa_debug_pkg.add(l_calling_fn,
697                            'entering',
698                            'logic for create new row');
699        end if;
700 
701        if (l_create_new_row = 'YES') OR
702 	  (px_inv_trans_rec.transaction_type = 'INVOICE TRANSFER' and
703 	   l_create_new_row = 'NO') then
704 
705           -- now for invoice transfers and adjustments, use the current values of the invoice
706           -- that was just terminated and then add the delta.  This will be the resulting cost
707           -- of the new line.  For invoice transfers, if full cost was transfered, we will
708           -- be entering this logic for the source asset.  Delta is the net effect, neg or pos.
709           -- SRC line ret should have same behavior with inv transfer.
710 
711           if (px_inv_trans_rec.transaction_type = 'INVOICE ADJUSTMENT' or
712               (px_inv_trans_rec.transaction_type in ('INVOICE TRANSFER',
713                                                      'RETIREMENT',
714                                                      'REINSTATEMENT') and
715                l_inv_rec.source_line_id is not null)) then
716 
717              l_inv_rec.fixed_assets_cost := nvl(l_current_fa_cost, 0) +
718                                                nvl(l_inv_rec.fixed_assets_cost, 0);
719 
720              -- BUG# 2314466
721              -- do not use nvl as form transactions should insert null
722              -- and this is used for deriving original_cost later on too
723 
724              l_inv_rec.payables_cost     := l_current_pa_cost +
725                                                l_inv_rec.payables_cost;
726 
727 
728           -- for deletes, reset the sign on fa_cost
729           -- no need for group depreciation change as delta = 0
730           elsif (px_inv_trans_rec.transaction_type = 'INVOICE DELETE' or
731                  px_inv_trans_rec.transaction_type = 'INVOICE DEP' or
732                  px_inv_trans_rec.transaction_type = 'INVOICE NO DEP') then
733              l_inv_rec.Fixed_Assets_Cost := l_current_fa_cost;
734              l_inv_rec.Payables_Cost     := l_current_pa_cost;
735           end if;
736 
737           -- original code here for setting up the payables cost rec/table has been moved above
738           -- the previous section as this needs to be done, before resetting the cost amounts
739 
740 
741           -- Need to get the asset_invoice_id and source_line_id values here
742           -- first assign the old source_line_id to self join column
743           -- for audit trail (BUG# 3033220).  On the source side, the
744           -- source line id is populated, on the destination side, it
745           -- is not, but the invoice transfer api will populate the
746           -- the prior value for the dest
747 
748           if (l_inv_rec.source_line_id is not null) then
749              l_inv_rec.prior_source_line_id := l_inv_rec.source_line_id;
750           end if;
751 
752           select FA_ASSET_INVOICES_S.nextval
753             into l_inv_rec.source_line_id
754             from dual;
755 
756           -- need to assign source lne id here
757           if ((px_inv_trans_rec.transaction_type = 'MASS ADDITION' or
758                px_inv_trans_rec.transaction_type = 'INVOICE TRANSFER' or
759                px_inv_trans_rec.transaction_type = 'INVOICE ADDITION') and
760               l_inv_rec.payables_cost <> 0) then
761 
762              l_payables_cost_tbl(l_payables_cost_count).source_line_id :=
763                 l_inv_rec.source_line_id;
764           end if;
765 
766           if l_inv_rec.asset_invoice_id is null then
767              select FA_MASS_ADDITIONS_S.nextval
768                into l_inv_rec.asset_invoice_id
769                from dual;
770           end if;
771 
772           if (l_create_new_row = 'YES') then
773              if (g_print_debug) then
774                 fa_debug_pkg.add(l_calling_fn,
775                                  'calling',
776                                  'process_invoice');
777              end if;
778 
779              if not process_invoice
780                 (px_trans_rec            => px_trans_rec,
781                  px_asset_hdr_rec        => px_asset_hdr_rec,
782                  p_inv_trans_rec         => px_inv_trans_rec,
783                  px_inv_rec              => l_inv_rec,        -- current row primary,
784                  p_inv_rate_rec          => l_inv_rate_rec,   -- null for primary
785                  p_mrc_sob_type_code     => 'P'
786                 ) then raise error_found;
787               end if;
788           end if;
789 
790           l_inv_rec_fa_cost_primary := l_inv_rec.fixed_assets_cost;
791 
792           -- call process_invoice for reporting books by looping through each reporting book
793 
794           if (g_print_debug) then
795              fa_debug_pkg.add(l_calling_fn,
796                               'entering',
797                               'ALC logic');
798           end if;
799 
800           if (fa_cache_pkg.fazcbc_record.mc_source_flag = 'Y') then
801 
802              if (g_print_debug) then
803                 fa_debug_pkg.add(l_calling_fn,
804                                  'px_inv_tbl(l_row_count + 1).inv_rate_tbl.count',
805                                  px_inv_tbl(l_row_count + 1).inv_rate_tbl.count);
806              end if;
807 
808              if (px_inv_tbl(l_row_count + 1).inv_rate_tbl.count > 0 and
809                  px_inv_tbl(l_row_count + 1).inv_rate_tbl.count <> l_sob_tbl.count) then
810                 fa_srvr_msg.add_message(
811                     calling_fn => l_calling_fn,
812                     name       => '***NOT_ENOUGH_INVRATES***');
813                 raise error_found;    -- not enough rates for each invoice
814              end if;   -- end init mrc
815 
816              FOR l_sob_index in 1..l_sob_tbl.count LOOP
817                 if (g_print_debug) then
818                    fa_debug_pkg.add(l_calling_fn,
819                                     'in reporting loop, reporting_sob',
820                                     l_sob_tbl(l_sob_index));
821                 end if;
822 
823 
824 
825                 fnd_profile.put('GL_SET_OF_BKS_ID', l_sob_tbl(l_sob_index));
826                 fnd_client_info.set_currency_context(to_char(l_sob_tbl(l_sob_index)));
827 
828                 -- BUG# 2632955
829                 -- call the cache to set the sob_id used for rounding and other lower
830                 -- level code for each book.
831                 if NOT fa_cache_pkg.fazcbcs(X_book => px_asset_hdr_rec.book_type_code) then
832                    raise error_found;
833                 end if;
834 
835                 l_asset_hdr_rec.set_of_books_id := l_sob_tbl(l_sob_index);
836                 l_mrc_rate_found_count := 0;
837                 l_mrc_rate_index       := 0;
838 
839                 -- need to locate the matching invoice rate for the given invoice.
840                 -- can do this by this inv_indicator for new lines or theoretically
841                 -- via source_line_id for existing ones.  inv_indicator is simply a
842                 -- a temporary key so calling code doesn't have to access sequences, etc.
843 
844                 if (g_print_debug) then
845                    fa_debug_pkg.add(l_calling_fn,
846                                     'entering',
847                                     'ALC rate loop');
848                 end if;
849 
850                 for i in 1..px_inv_tbl(l_row_count + 1).inv_rate_tbl.count loop
851 
852                    if (g_print_debug) then
853                       fa_debug_pkg.add(l_calling_fn,
854                                        'inside',
855                                        'inv rate loop');
856                       fa_debug_pkg.add(l_calling_fn,
857                                        'px_inv_tbl(l_row_count + 1).inv_rate_tbl(i).set_of_books_id',
858                                        px_inv_tbl(l_row_count + 1).inv_rate_tbl(i).set_of_books_id);
859                       fa_debug_pkg.add(l_calling_fn,
860                                        'sob_id',
861                                        l_sob_tbl(l_sob_index));
862                    end if;
863 
864                    -- match only based on sob now, indicator is obsolete in SLA as we have nested the rate
865                    if (px_inv_tbl(l_row_count + 1).inv_rate_tbl(i).set_of_books_id = l_sob_tbl(l_sob_index)) then
866                          l_mrc_rate_found_count := l_mrc_rate_found_count + 1;
867                          l_mrc_rate_index       := i;
868                          l_inv_rate_rec         := px_inv_tbl(l_row_count + 1).inv_rate_tbl(i);
869                    end if;
870 
871                 end loop;
872 
873                 if (g_print_debug) then
874                    fa_debug_pkg.add(l_calling_fn,
875                                     'after',
876                                     'ALC rate loop');
877                 end if;
878 
879 
880                 -- BUG# 2613834
881                 -- exchange will no longer be derived at insertion
882                 -- via the trigger, but will be done here inside the engine
883                 -- thus if rate isn't populated, we'll find it for AP/PA/other lines
884 
885                 if (l_mrc_rate_found_count > 1) then
886 
887                    fa_srvr_msg.add_message(
888                       calling_fn => l_calling_fn,
889                       name       => '***WRONG_NUM_INVRATES2***');
890                    raise error_found;
891 
892                 elsif (l_mrc_rate_found_count = 0) then
893 
894                    if (g_print_debug) then
895                       fa_debug_pkg.add(l_calling_fn,
896                                        'entering',
897                                        'no rate found logic');
898                    end if;
899 
900                    l_inv_rate_rec.set_of_books_id := l_sob_tbl(l_sob_index);
901 
902                    -- note: the following is only used for non-AP and non-PA lines
903                    l_exchange_date                := p_asset_fin_rec_adj.date_placed_in_service; -- inv date?
904                    l_exchange_date                := px_trans_rec.transaction_date_entered;
905 
906                    -- R12: no longer using rates directly in calcs, so need to convert to
907                    -- an amount here before the calcs occur...
908 
909                    if not FA_MC_UTIL_PVT.get_invoice_rate
910                            (p_inv_rec                    => l_inv_rec,
911                             p_book_type_code             => px_asset_hdr_rec.book_type_code,
912                             p_set_of_books_id            => l_sob_tbl(l_sob_index),
913                             px_exchange_date             => l_exchange_date,
914                             px_inv_rate_rec              => l_inv_rate_rec)then
915                       raise error_found;
916                    end if;
917 
918                 end if; -- l_mrc_rate_found_count
919 
920                 if (g_print_debug) then
921                    fa_debug_pkg.add(l_calling_fn,
922                                     'after',
923                                     'rate found logic');
924                 end if;
925 
926                 -- use the rate located and multiply by the primary amounts
927                 -- in order to get the reporting value - taking precision into account.
928                 -- since the values in l_rec were already changed to the values to
929                 -- be inserted, we must revert to using the values in array
930                 --
931                 -- for invoice deletes and reinstates use the values already in
932                 -- record as they are the true deltas in terms of the adjustment
933                 -- for deletes, we need to flip the sign here
934 
935                 -- R12 / SLA - we will no longer be using the rate, but the amount
936                 -- directly in the same structure.  logi will be to use that for
937                 -- fa cost and then compare the remaining values and either
938                 -- use that same amount or the exchange rate if different
939                 --
940                 -- however in case where amount is not provided, we will still
941                 -- use the old method
942 
943                 if (g_print_debug) then
944                    fa_debug_pkg.add(l_calling_fn,
945                                     'checking',
946                                     'l_inv_rate_rec.cost');
947                 end if;
948 
949 
950                 -- the following code mirrors that for primary
951                 -- but we only need to worry about the amounts for alc
952                 -- validation has already been done and rest of inv_rec values
953                 -- can equal the primary values
954 
955                 if ((px_inv_trans_rec.transaction_type <> 'MASS ADDITION' or
956                      px_inv_trans_rec.transaction_type <> 'INVOICE ADDITION') and
957                      px_inv_tbl(l_row_count + 1).source_line_id is not null) then
958 
959                    if (g_print_debug) then
960                       fa_debug_pkg.add(l_calling_fn,
961                                     'in lgoic for existing line',
962                                     '');
963                    end if;
964 
965 
966                    select fixed_assets_cost,
967                           payables_cost
968                      into l_current_fa_cost,
969                           l_current_pa_cost
970                      from fa_asset_invoices_mrc_v
971                     where source_line_id = l_current_source_line_id;
972 
973                    if (px_inv_trans_rec.transaction_type = 'INVOICE DELETE') then
974                       l_inv_rec.Fixed_Assets_Cost := -l_current_fa_cost;
975                       l_inv_rec.Payables_Cost     := -l_current_pa_cost;
976                       if (p_asset_type_rec.asset_type = 'CIP') then
977                          if (nvl(l_inv_rec.depreciate_in_group_flag, 'N') = 'Y') then
978                             l_inv_rec.Cip_Cost          := -l_current_fa_cost;
979                          else
980                             l_inv_rec.Cip_Cost          := 0;
981                          end if;
982                       end if;
983                    elsif (px_inv_trans_rec.transaction_type =  'INVOICE REINSTATE') then
984                       l_inv_rec.Fixed_Assets_Cost := l_current_fa_cost;
985                       l_inv_rec.Payables_Cost     := l_current_pa_cost;
986                       if (p_asset_type_rec.asset_type = 'CIP') then
987                          if (nvl(l_inv_rec.depreciate_in_group_flag, 'N') = 'Y') then
988                             l_inv_rec.Cip_Cost          := l_current_fa_cost;
989                          else
990                             l_inv_rec.Cip_Cost          := 0;
991                          end if;
992                       end if;
993                    elsif (px_inv_trans_rec.transaction_type = 'INVOICE DEP') then
994                       l_inv_rec.Cip_Cost          := -l_current_fa_cost;
995                       l_inv_rec.Fixed_Assets_Cost := 0;
996                       l_inv_rec.Payables_Cost     := 0;
997                    elsif (px_inv_trans_rec.transaction_type = 'INVOICE NO DEP') then
998                       l_inv_rec.Cip_Cost          := l_current_fa_cost;
999                       l_inv_rec.Fixed_Assets_Cost := 0;
1000                       l_inv_rec.Payables_Cost     := 0;
1001                    else
1002                       -- on existing lines, we currently do not allow
1003                       -- a different exchange rate to be used that the original
1004                       -- thus we can ignore the amount and go directly to the rate logic
1005 
1006                       -- use the rate located and multiply by the primary amounts
1007                       -- in order to get the reporting value - taking precision into account.
1008                       -- since the values in l_rec were already changed to the values to
1009                       -- be inserted, we must revert to using the values in array
1010                       --
1011                       -- for invoice deletes and reinstates use the values already in
1012                       -- record as they are the true deltas in terms of the adjustment
1013                       -- for deletes, we need to flip the sign here
1014 
1015                       l_inv_rec.fixed_assets_cost := nvl(px_inv_tbl(l_row_count + 1).fixed_assets_cost, 0) *
1016                                                         l_inv_rate_rec.exchange_rate;
1017 
1018                       -- bug 2258936 we do not want to set these values to 0, but leave null
1019                       l_inv_rec.payables_cost     := px_inv_tbl(l_row_count + 1).payables_cost *
1020                                                         l_inv_rate_rec.exchange_rate;
1021                       l_inv_rec.unrevalued_cost   := px_inv_tbl(l_row_count + 1).unrevalued_cost *
1022                                                         l_inv_rate_rec.exchange_rate;
1023 
1024                       -- adding for new group requirements
1025                       l_inv_rec.cip_cost          := px_inv_tbl(l_row_count + 1).cip_cost *
1026                                                         l_inv_rate_rec.exchange_rate;
1027 
1028 /*                    l_inv_rec.salvage_value     := nvl(px_inv_tbl(l_row_count + 1).salvage_value, 0)     *
1029                                                         l_inv_rate_rec.exchange_rate; */
1030 
1031 
1032                    end if;  -- invoice transaction type
1033 
1034 /*   may not be needed
1035                 elsif () then
1036                    -- special ALC for treatment for destinat8ion side of invoice transfers
1037                    -- still need to insure we use the correct exchange rate here
1038 
1039 */
1040 
1041                 else -- new line scenario (add/massadd)
1042 
1043                    if (g_print_debug) then
1044                       fa_debug_pkg.add(l_calling_fn,
1045                                     'in logic for new line',
1046                                     '');
1047                    end if;
1048 
1049 
1050                    if (l_mrc_rate_found_count = 1 and
1051                        px_inv_tbl(l_row_count + 1).inv_rate_tbl(l_mrc_rate_index).cost is not null) then
1052 
1053                       -- now set the rate for subsequent use
1054                       -- accounting for possibility that primary is 0/null
1055 
1056                       if (px_inv_tbl(l_row_count + 1).fixed_assets_cost is not null and
1057                           px_inv_tbl(l_row_count + 1).fixed_assets_cost <> 0) then
1058                          l_inv_rate_rec.exchange_rate := l_inv_rate_rec.cost /
1059                                                             px_inv_tbl(l_row_count + 1).fixed_assets_cost;
1060                          l_inv_rec.fixed_assets_cost := px_inv_tbl(l_row_count + 1).inv_rate_tbl(l_mrc_rate_index).cost;
1061                       else
1062                          l_inv_rate_rec.exchange_rate := nvl(l_inv_rate_rec.exchange_rate, 0);
1063                       end if;
1064 
1065                    else  -- old rate based method
1066 
1067                       if (g_print_debug) then
1068                          fa_debug_pkg.add(l_calling_fn,
1069                                           'entering',
1070                                           'null ALC amount logic for cost');
1071                       end if;
1072 
1073                       l_inv_rec.fixed_assets_cost := nvl(px_inv_tbl(l_row_count + 1).fixed_assets_cost, 0) *
1074                                                         l_inv_rate_rec.exchange_rate;
1075                    end if;
1076 
1077                    -- BUG# 2314466
1078                    -- do not use nvl here as we want to insert null intentionally
1079                    -- and the null value is needed to correctly derive original cost later
1080                    if (px_inv_tbl(l_row_count + 1).payables_cost = px_inv_tbl(l_row_count + 1).fixed_assets_cost) then
1081                       l_inv_rec.payables_cost     := l_inv_rec.fixed_assets_cost;
1082                    else
1083                       -- bug 2258936 we do not want to set these values to 0, but leave null
1084                       l_inv_rec.payables_cost     := px_inv_tbl(l_row_count + 1).payables_cost *
1085                                                         l_inv_rate_rec.exchange_rate;
1086                    end if;
1087 
1088                    if (px_inv_tbl(l_row_count + 1).unrevalued_cost = px_inv_tbl(l_row_count + 1).fixed_assets_cost) then
1089                       l_inv_rec.unrevalued_cost   := l_inv_rec.fixed_assets_cost;
1090                    else
1091                       l_inv_rec.unrevalued_cost   := px_inv_tbl(l_row_count + 1).unrevalued_cost *
1092                                                      l_inv_rate_rec.exchange_rate;
1093                    end if;
1094 
1095 /*                 l_inv_rec.salvage_value     := nvl(px_inv_tbl(l_row_count + 1).salvage_value, 0)     *
1096                                                      l_inv_rate_rec.exchange_rate; */
1097 
1098 
1099                    if (p_asset_type_rec.asset_type = 'CIP') then
1100                       if (nvl(l_inv_rec.depreciate_in_group_flag, 'N') = 'Y') then
1101                          l_inv_rec.Cip_Cost       := 0;
1102                       else
1103                          l_inv_rec.Cip_Cost       := l_inv_rec.fixed_assets_cost;
1104                       end if;
1105                    end if;
1106 
1107                    if (px_asset_hdr_rec.period_of_addition = 'Y' and
1108                        px_inv_trans_rec.transaction_type in ('MASS ADDITION', 'INVOICE ADDITION') and
1109                        p_asset_type_rec.asset_type = 'CAPITALIZED') then
1110 
1111                       l_inv_rec.ytd_deprn                := nvl(px_inv_tbl(l_row_count + 1).ytd_deprn, 0) *
1112                                                                l_inv_rate_rec.exchange_rate;
1113                       l_inv_rec.deprn_reserve            := nvl(px_inv_tbl(l_row_count + 1).deprn_reserve, 0) *
1114                                                                l_inv_rate_rec.exchange_rate;
1115                       l_inv_rec.bonus_ytd_deprn          := nvl(px_inv_tbl(l_row_count + 1).bonus_ytd_deprn, 0) *
1116                                                                l_inv_rate_rec.exchange_rate;
1117                       l_inv_rec.bonus_deprn_reserve      := nvl(px_inv_tbl(l_row_count + 1).bonus_deprn_reserve, 0) *
1118                                                                l_inv_rate_rec.exchange_rate;
1119                       l_inv_rec.reval_ytd_deprn          := nvl(px_inv_tbl(l_row_count + 1).reval_ytd_deprn, 0) *
1120                                                                l_inv_rate_rec.exchange_rate;
1121                       l_inv_rec.reval_deprn_reserve      := nvl(px_inv_tbl(l_row_count + 1).reval_deprn_reserve, 0) *
1122                                                                l_inv_rate_rec.exchange_rate;
1123                       l_inv_rec.reval_amortization_basis := nvl(px_inv_tbl(l_row_count + 1).reval_amortization_basis, 0) *
1124                                                                l_inv_rate_rec.exchange_rate;
1125                    end if;
1126 
1127                 end if; -- end existing / new line logic
1128 
1129                 if (g_print_debug) then
1130                    fa_debug_pkg.add(l_calling_fn,
1131                                     'calculated delta ALC cost',
1132                                     l_inv_rec.fixed_assets_cost);
1133                 end if;
1134 
1135 
1136                 -- retrieve the mrc fin struct row from the table of structs
1137                 -- note that the order should match the sob_id cursor since
1138                 -- that was used to populate the above.  This will also be
1139                 -- used for deprn table
1140 
1141                 l_mrc_fin_book_rec := 0;
1142                 for i in 1..x_asset_fin_mrc_tbl_new.count loop
1143 
1144                    if ((x_asset_fin_mrc_tbl_new(i).set_of_books_id = l_sob_tbl(l_sob_index))) then
1145                       l_mrc_fin_book_rec := i;
1146                    end if;
1147 
1148                 end loop;
1149 
1150                 if (l_mrc_fin_book_rec = 0) then
1151                     fa_srvr_msg.add_message(
1152                        calling_fn => l_calling_fn,
1153                        name       => '***FAILED LOOKUP FININFO***');
1154                     raise error_found;
1155                 end if;
1156 
1157 
1158                 if (g_print_debug) then
1159                    fa_debug_pkg.add(l_calling_fn,
1160                                     'calling',
1161                                     'inv_calc_info');
1162                 end if;
1163 
1164                 --  call calc_inv for amounts
1165                 if not inv_calc_info
1166                         (p_asset_hdr_rec         => px_asset_hdr_rec,
1167                          p_asset_type_rec        => p_asset_type_rec,
1168                          p_inv_trans_rec         => px_inv_trans_rec,
1169                          px_inv_rec              => l_inv_rec,
1170                          px_asset_fin_rec_new    => x_asset_fin_mrc_tbl_new(l_mrc_fin_book_rec),
1171                          px_asset_deprn_rec_new  => x_asset_deprn_mrc_tbl_new(l_mrc_fin_book_rec)
1172                         ) then
1173                    raise error_found;
1174                 end if;
1175 
1176                 -- BUG# 2622722
1177                 -- do this for all lines for addition and transfer transactions,
1178                 -- not just those from mass additions. note that non-xfr adjustment
1179                 -- will use the flexbuilt ccids for now. section has been moved up so
1180                 -- tht the deltas are used, not the final amounts
1181 
1182                 if ((px_inv_trans_rec.transaction_type = 'MASS ADDITION' or
1183                      px_inv_trans_rec.transaction_type = 'INVOICE TRANSFER' or
1184                      px_inv_trans_rec.transaction_type = 'INVOICE ADDITION') and
1185                     l_inv_rec.payables_cost <> 0) then
1186                    l_payables_cost_mrc_count := l_payables_cost_mrc_tbl.count + 1;
1187                    l_payables_cost_mrc_tbl(l_payables_cost_mrc_count).set_of_books_id :=
1188                         l_sob_tbl(l_sob_index);
1189                    l_payables_cost_mrc_tbl(l_payables_cost_mrc_count).payables_cost :=
1190                         l_inv_rec.payables_cost;
1191                    l_payables_cost_mrc_tbl(l_payables_cost_mrc_count).payables_code_combination_id :=
1192                         l_inv_rec.payables_code_combination_id;
1193                    l_payables_cost_mrc_tbl(l_payables_cost_mrc_count).source_dest_code :=
1194                         l_inv_rec.source_dest_code;
1195                    l_payables_cost_mrc_tbl(l_payables_cost_mrc_count).source_line_id :=
1196                         l_inv_rec.source_line_id;
1197 
1198 
1199                 end if;
1200 
1201 
1202                 -- now for invoice transfers and adjustments, get the current values of the invoice
1203                 -- that was just terminated and then add the delta.  This will be the resulting cost
1204                 -- of the new line.  For invoice transfers, if full cost was transfered, we will
1205                 -- be entering this logic for the source asset.  Delta is the net effect, neg or pos.
1206                 -- SRC line ret should have same behavior with inv transfer.
1207                 --
1208                 -- for source line deletes and reinstatements, no need to rederive this
1209                 -- as the value set above using the rate is fine however we do need to
1210                 -- flip the sign back for deletes
1211 
1212                 if (px_inv_trans_rec.transaction_type = 'INVOICE ADJUSTMENT' or
1213                     (px_inv_trans_rec.transaction_type in ('INVOICE TRANSFER',
1214                                                            'RETIREMENT',
1215                                                            'REINSTATEMENT') and
1216                      l_current_source_line_id is not null)) then
1217 
1218                    -- special case - for invoice transfer (source)
1219                    -- load the delta amount back into the rate array
1220                    -- which will be used by the destination asset
1221                    if (px_inv_trans_rec.transaction_type = 'INVOICE TRANSFER') then
1222                        px_inv_tbl(l_row_count + 1).inv_rate_tbl(l_mrc_rate_index).cost := -l_inv_rec.fixed_assets_cost;
1223                    end if;
1224 
1225                    l_inv_rec.fixed_assets_cost := l_current_fa_cost + l_inv_rec.fixed_assets_cost;
1226                    l_inv_rec.payables_cost     := l_current_pa_cost + l_inv_rec.payables_cost;
1227 
1228                 end if;
1229 
1230                 if (l_create_new_row = 'YES') then
1231 
1232                    if not process_invoice
1233                       (px_trans_rec            => px_trans_rec,
1234                        px_asset_hdr_rec        => l_asset_hdr_rec,
1235                        p_inv_trans_rec         => px_inv_trans_rec,
1236                        px_inv_rec              => l_inv_rec,       -- current row reporting
1237                        p_inv_rate_rec          => l_inv_rate_rec,
1238                        p_mrc_sob_type_code     => 'R'
1239                       ) then raise error_found;
1240                    end if;
1241                 end if;
1242 
1243                 -- store the total payables cost cleared per book
1244 
1245              end loop; -- end mrc sob loop
1246 
1247              -- Bug:5887217
1248              -- reset GL sob id to original value
1249              fnd_profile.put('GL_SET_OF_BKS_ID', px_asset_hdr_rec.set_of_books_id);
1250              fnd_client_info.set_currency_context(to_char(px_asset_hdr_rec.set_of_books_id));
1251 
1252              -- call the cache to reset the sob_id for the primary book
1253              if NOT fa_cache_pkg.fazcbcs(X_book => px_asset_hdr_rec.book_type_code) then
1254                 raise error_found;
1255              end if;
1256 
1257 	  end if; -- end mrc enabled book
1258 
1259        end if;  -- end create new row is yes
1260 
1261        l_row_count := l_row_count + 1;
1262 
1263    end loop; -- end invoice loop
1264 
1265    -- call the post clearing code to clear all payables cost
1266    if ((px_inv_trans_rec.transaction_type = 'MASS ADDITION' or
1267         px_inv_trans_rec.transaction_type = 'INVOICE TRANSFER' or
1268         px_inv_trans_rec.transaction_type = 'INVOICE ADDITION') and
1269 --       px_asset_hdr_rec.period_of_addition <> 'Y') then
1270         p_calling_fn <> 'fa_addition_pub.do_addition') then
1271       if not post_clearing
1272              (p_trans_rec                => px_trans_rec,
1273               p_asset_hdr_rec            => px_asset_hdr_rec,
1274               p_asset_desc_rec           => p_asset_desc_rec,
1275               p_asset_type_rec           => p_asset_type_rec,
1276               p_asset_cat_rec            => p_asset_cat_rec,
1277               p_inv_trans_rec            => px_inv_trans_rec,
1278               p_payables_cost_tbl        => l_payables_cost_tbl,
1279               p_payables_cost_mrc_tbl    => l_payables_cost_mrc_tbl
1280             ) then raise error_found;
1281       end if;
1282    end if;
1283 
1284    -- reset GL sob id to original value
1285    fnd_profile.put('GL_SET_OF_BKS_ID', G_profile_set_of_books_id);
1286    fnd_client_info.set_currency_context (G_orig_currency_context);
1287 
1288    -- BUG# 2632955
1289    -- call the cache to reset the sob_id for the primary book
1290    if NOT fa_cache_pkg.fazcbcs(X_book => px_asset_hdr_rec.book_type_code) then
1291       raise error_found;
1292    end if;
1293 
1294    return true;
1295 
1296 EXCEPTION
1297    when error_found then
1298       fa_srvr_msg.add_message(calling_fn => 'fa_invoice_pvt.inv_engine');
1299 
1300       -- reset GL sob id to original value
1301       fnd_profile.put('GL_SET_OF_BKS_ID', G_profile_set_of_books_id);
1302       fnd_client_info.set_currency_context (G_orig_currency_context);
1303 
1304       return false;
1305 
1306    when others then
1307       fa_srvr_msg.add_sql_error(calling_fn => 'fa_invoice_pvt.inv_engine');
1308 
1309       -- reset GL sob id to original value
1310       fnd_profile.put('GL_SET_OF_BKS_ID', G_profile_set_of_books_id);
1311       fnd_client_info.set_currency_context (G_orig_currency_context);
1312 
1313       return false;
1314 
1315 END invoice_engine;
1316 
1317 ------------------------------------------------------------------------------------------
1318 
1319 FUNCTION inv_calc_info
1320                (p_asset_hdr_rec          IN     FA_API_TYPES.asset_hdr_rec_type,
1321                 p_asset_type_rec         IN     FA_API_TYPES.asset_type_rec_type,
1322                 p_inv_trans_rec          IN     FA_API_TYPES.inv_trans_rec_type,
1323                 px_inv_rec               IN OUT NOCOPY FA_API_TYPES.inv_rec_type,
1324                 px_asset_fin_rec_new     IN OUT NOCOPY FA_API_TYPES.asset_fin_rec_type,
1325                 px_asset_deprn_rec_new   IN OUT NOCOPY FA_API_TYPES.asset_deprn_rec_type,
1326                 p_log_level_rec            in fa_api_types.log_level_rec_type default null
1327                ) RETURN BOOLEAN IS
1328 
1329    l_calling_fn              varchar2(35) := 'fa_inv_pvt.inv_calc_info';
1330 
1331    error_found   exception;
1332 
1333 BEGIN
1334 
1335    -- Bug:5916116
1336    if not FA_UTILS_PKG.faxrnd(px_inv_rec.fixed_assets_cost,
1337                               p_asset_hdr_rec.book_type_code) then
1338           raise error_found;
1339    end if;
1340 
1341    -- populate / increment the fin_rec using the delta
1342 
1343    px_asset_fin_rec_new.cost            := nvl(px_asset_fin_rec_new.cost, 0) +
1344                                                  nvl(px_inv_rec.fixed_assets_cost, 0);
1345    px_asset_fin_rec_new.unrevalued_cost := nvl(px_asset_fin_rec_new.unrevalued_cost, 0) +
1346                                                  nvl(px_inv_rec.unrevalued_cost,
1347                                                      nvl(px_inv_rec.fixed_assets_cost, 0));
1348    px_asset_fin_rec_new.cip_cost        := nvl(px_asset_fin_rec_new.cip_cost, 0) +
1349                                                  nvl(px_inv_rec.cip_cost,0);
1350 
1351 
1352    if (g_print_debug) then
1353       fa_debug_pkg.add(l_calling_fn,
1354                        'px_asset_fin_rec_new.cost',
1355                        px_asset_fin_rec_new.cost);
1356       fa_debug_pkg.add(l_calling_fn,
1357                        'px_asset_fin_rec_new.cip_cost',
1358                        px_asset_fin_rec_new.cip_cost);
1359    end if;
1360 
1361 
1362 /*     px_asset_fin_rec_new.salvage_value   := nvl(px_asset_fin_rec_new.salvage_value, 0) +
1363                                                      nvl(px_inv_rec.salvage_value, 0);
1364 */
1365 
1366    if (p_asset_hdr_rec.period_of_addition = 'Y') then
1367        -- Bug:5916116
1368        if not FA_UTILS_PKG.faxrnd(px_inv_rec.payables_cost,
1369                                   p_asset_hdr_rec.book_type_code) then
1370               raise error_found;
1371        end if;
1372 
1373        px_asset_fin_rec_new.original_cost   := nvl(px_asset_fin_rec_new.original_cost,0) +
1374                                                      nvl(px_inv_rec.payables_cost,
1375                                                          nvl(px_inv_rec.fixed_assets_cost, 0));
1376    end if;
1377 
1378    if not FA_UTILS_PKG.faxrnd(px_inv_rec.fixed_assets_cost,
1379                               p_asset_hdr_rec.book_type_code) then
1380           raise error_found;
1381    end if;
1382 
1383    if not FA_UTILS_PKG.faxrnd(px_inv_rec.payables_cost,
1384                               p_asset_hdr_rec.book_type_code) then
1385           raise error_found;
1386    end if;
1387 
1388    if not FA_UTILS_PKG.faxrnd(px_inv_rec.cip_cost,
1389                               p_asset_hdr_rec.book_type_code) then
1390           raise error_found;
1391    end if;
1392 
1393 /*
1394    if not FA_UTILS_PKG.faxrnd(px_inv_rec.payables_cost,
1395                               p_asset_hdr_rec.book_type_code) then
1396           raise error_found;
1397    end if;
1398 */
1399 
1400    if (p_asset_hdr_rec.period_of_addition = 'Y' and
1401        p_inv_trans_rec.transaction_type in ('MASS ADDITION', 'INVOICE ADDITION') and
1402        p_asset_type_rec.asset_type = 'CAPITALIZED') then
1403 
1404        px_asset_deprn_rec_new.ytd_deprn            := nvl(px_asset_deprn_rec_new.ytd_deprn, 0) +
1405                                                         nvl(px_inv_rec.ytd_deprn, 0);
1406        px_asset_deprn_rec_new.deprn_reserve        := nvl(px_asset_deprn_rec_new.deprn_reserve, 0) +
1407                                                         nvl(px_inv_rec.deprn_reserve, 0);
1408        px_asset_deprn_rec_new.bonus_ytd_deprn      := nvl(px_asset_deprn_rec_new.bonus_ytd_deprn, 0) +
1409                                                         nvl(px_inv_rec.bonus_ytd_deprn, 0);
1410        px_asset_deprn_rec_new.bonus_deprn_reserve  := nvl(px_asset_deprn_rec_new.bonus_deprn_reserve, 0) +
1411                                                         nvl(px_inv_rec.bonus_deprn_reserve, 0);
1412        px_asset_deprn_rec_new.reval_ytd_deprn      := nvl(px_asset_deprn_rec_new.reval_ytd_deprn, 0) +
1413                                                         nvl(px_inv_rec.reval_ytd_deprn, 0);
1414        px_asset_deprn_rec_new.reval_deprn_reserve  := nvl(px_asset_deprn_rec_new.reval_deprn_reserve, 0) +
1415                                                         nvl(px_inv_rec.reval_deprn_reserve, 0);
1416        px_asset_deprn_rec_new.reval_amortization_basis := nvl(px_asset_deprn_rec_new.reval_amortization_basis, 0) +
1417                                                             nvl(px_inv_rec.reval_amortization_basis, 0);
1418 
1419        -- Bug 4243541 : Load fin_rec_new from deprn_rec
1420        px_asset_fin_rec_new.reval_amortization_basis := px_asset_deprn_rec_new.reval_amortization_basis;
1421 
1422        if not FA_UTILS_PKG.faxrnd(px_inv_rec.ytd_deprn,
1423                                   p_asset_hdr_rec.book_type_code) then
1424               raise error_found;
1425        end if;
1426 
1427        if not FA_UTILS_PKG.faxrnd(px_inv_rec.deprn_reserve       ,
1428                                   p_asset_hdr_rec.book_type_code) then
1429               raise error_found;
1430        end if;
1431 
1432        if not FA_UTILS_PKG.faxrnd(px_inv_rec.bonus_ytd_deprn     ,
1433                                   p_asset_hdr_rec.book_type_code) then
1434               raise error_found;
1435        end if;
1436 
1437        if not FA_UTILS_PKG.faxrnd(px_inv_rec.bonus_deprn_reserve ,
1438                                   p_asset_hdr_rec.book_type_code) then
1439               raise error_found;
1440        end if;
1441 
1442        if not FA_UTILS_PKG.faxrnd(px_inv_rec.reval_ytd_deprn     ,
1443                                   p_asset_hdr_rec.book_type_code) then
1444               raise error_found;
1445        end if;
1446 
1447        if not FA_UTILS_PKG.faxrnd(px_inv_rec.reval_deprn_reserve ,
1448                                   p_asset_hdr_rec.book_type_code) then
1449               raise error_found;
1450        end if;
1451 
1452        if not FA_UTILS_PKG.faxrnd(px_inv_rec.reval_amortization_basis,
1453                                   p_asset_hdr_rec.book_type_code) then
1454               raise error_found;
1455        end if;
1456 
1457        if not FA_UTILS_PKG.faxrnd( px_asset_fin_rec_new.reval_amortization_basis,
1458                                   p_asset_hdr_rec.book_type_code) then
1459               raise error_found;
1460        end if;
1461    end if;
1462 
1463    return true;
1464 
1465 EXCEPTION
1466    when error_found then
1467       fa_srvr_msg.add_sql_error(calling_fn => 'fa_invoice_pvt.inv_calc_info');
1468       return false;
1469 
1470    when others then
1471       fa_srvr_msg.add_sql_error(calling_fn => 'fa_invoice_pvt.inv_calc_info');
1472       return false;
1473 
1474 END inv_calc_info;
1475 
1476 ------------------------------------------------------------------------------------------
1477 
1478 FUNCTION  process_invoice
1479    (px_trans_rec               IN OUT NOCOPY FA_API_TYPES.trans_rec_type,
1480     px_asset_hdr_rec           IN OUT NOCOPY FA_API_TYPES.asset_hdr_rec_type,
1481     p_inv_trans_rec            IN     FA_API_TYPES.inv_trans_rec_type,
1482     px_inv_rec                 IN OUT NOCOPY FA_API_TYPES.inv_rec_type,
1483     p_inv_rate_rec             IN     FA_API_TYPES.inv_rate_rec_type,
1484     p_mrc_sob_type_code        IN     VARCHAR2,
1485     p_log_level_rec            in fa_api_types.log_level_rec_type default null
1486    ) RETURN BOOLEAN IS
1487 
1488  l_rowid                       varchar2(30);
1489  l_reporting_flag              varchar2(1);
1490  error_found                   EXCEPTION;
1491  /*
1492   * Following two variables are created for creating 0 cost source line
1493   * which may happned in source line retirement
1494   */
1495  l_date_ineffective            date   := null;
1496  l_invoice_transaction_id_out  number := null;
1497  l_calling_fn                  varchar2(35) := 'fa_inv_pvt.process_invoice';
1498 
1499 BEGIN
1500 
1501    /*
1502     * In case of  full source line retirement, new line with 0 amount
1503     * still needs to be created, but it needs to have DATE_INEFFECTIVE and
1504     * INVOICE_TRANSACTION_ID_OUT populated to not to display on source line window.
1505     */
1506    if (p_inv_trans_rec.transaction_type = 'RETIREMENT' and
1507        px_inv_rec.fixed_assets_cost = 0) then
1508 
1509       l_date_ineffective             := px_trans_rec.who_info.last_update_date;
1510       l_invoice_transaction_id_out := p_inv_trans_rec.invoice_transaction_id;
1511 
1512    end if;
1513 
1514    FA_ASSET_INVOICES_PKG.Insert_Row
1515                       (X_Rowid                          => l_rowid,
1516                        X_Asset_Id                       => px_asset_hdr_rec.asset_id,
1517                        X_Po_Vendor_Id                   => px_inv_rec.po_vendor_id,
1518                        X_Asset_Invoice_Id               => px_inv_rec.asset_invoice_id,    -- mass_add_id
1519                        X_Fixed_Assets_Cost              => px_inv_rec.fixed_assets_cost,
1520                        X_Date_Effective                 => px_trans_rec.who_info.last_update_date,
1521                        X_Date_Ineffective               => l_date_ineffective,
1522                        X_Invoice_Transaction_Id_In      => p_inv_trans_rec.invoice_transaction_id,
1523                        X_Invoice_Transaction_Id_Out     => l_invoice_transaction_id_out,
1524                        X_Deleted_Flag                   => px_inv_rec.deleted_flag,
1525                        X_Po_Number                      => px_inv_rec.po_number,
1526                        X_Invoice_Number                 => px_inv_rec.invoice_number,
1527                        X_Payables_Batch_Name            => px_inv_rec.payables_batch_name,
1528                        X_Payables_Code_Combination_Id   => px_inv_rec.payables_code_combination_id,
1529                        X_Feeder_System_Name             => px_inv_rec.feeder_system_name,
1530                        X_Create_Batch_Date              => px_inv_rec.create_batch_date,
1531                        X_Create_Batch_Id                => px_inv_rec.create_batch_id,
1532                        X_Invoice_Date                   => px_inv_rec.invoice_date,
1533                        X_Payables_Cost                  => px_inv_rec.payables_cost,
1534                        X_Post_Batch_Id                  => px_inv_rec.post_batch_id,
1535                        X_Invoice_Id                     => px_inv_rec.invoice_id,
1536                        X_Ap_Distribution_Line_Number    => px_inv_rec.ap_distribution_line_number,
1537                        X_Payables_Units                 => px_inv_rec.payables_units,
1538                        X_Split_Merged_Code              => px_inv_rec.split_merged_code,
1539                        X_Description                    => px_inv_rec.description,
1540                        X_Parent_Mass_Addition_Id        => px_inv_rec.parent_mass_addition_id,
1541                        X_Last_Update_Date               => px_trans_rec.who_info.last_update_date,
1542                        X_Last_Updated_By                => px_trans_rec.who_info.last_updated_by,
1543                        X_Created_By                     => px_trans_rec.who_info.created_by,
1544                        X_Creation_Date                  => px_trans_rec.who_info.creation_date,
1545                        X_Last_Update_Login              => px_trans_rec.who_info.last_update_login,
1546                        X_Attribute1                     => px_inv_rec.ATTRIBUTE1,
1547                        X_Attribute2                     => px_inv_rec.ATTRIBUTE2,
1548                        X_Attribute3                     => px_inv_rec.ATTRIBUTE3,
1549                        X_Attribute4                     => px_inv_rec.ATTRIBUTE4,
1550                        X_Attribute5                     => px_inv_rec.ATTRIBUTE5,
1551                        X_Attribute6                     => px_inv_rec.ATTRIBUTE6,
1552                        X_Attribute7                     => px_inv_rec.ATTRIBUTE7,
1553                        X_Attribute8                     => px_inv_rec.ATTRIBUTE8,
1554                        X_Attribute9                     => px_inv_rec.ATTRIBUTE9,
1555                        X_Attribute10                    => px_inv_rec.ATTRIBUTE10,
1556                        X_Attribute11                    => px_inv_rec.ATTRIBUTE11,
1557                        X_Attribute12                    => px_inv_rec.ATTRIBUTE12,
1558                        X_Attribute13                    => px_inv_rec.ATTRIBUTE13,
1559                        X_Attribute14                    => px_inv_rec.ATTRIBUTE14,
1560                        X_Attribute15                    => px_inv_rec.ATTRIBUTE15,
1561                        X_Attribute_Category_Code        => px_inv_rec.ATTRIBUTE_CATEGORY_CODE,
1562                        X_Unrevalued_Cost                => px_inv_rec.unrevalued_cost,
1563                        X_Merged_Code                    => px_inv_rec.merged_code,
1564                        X_Split_Code                     => px_inv_rec.split_code,
1565                        X_Merge_Parent_Mass_Add_Id       => px_inv_rec.merge_parent_mass_additions_id,
1566                        X_Split_Parent_Mass_Add_Id       => px_inv_rec.split_parent_mass_additions_id,
1567                        X_Project_Asset_Line_Id          => px_inv_rec.project_asset_line_id,
1568                        X_Project_Id                     => px_inv_rec.project_id,
1569                        X_Task_Id                        => px_inv_rec.task_id,
1570                        X_Material_Indicator_Flag        => px_inv_rec.material_indicator_flag,
1571                        X_source_line_id                 => px_inv_rec.source_line_id,
1572                        X_prior_source_line_id           => px_inv_rec.prior_source_line_id,
1573                        X_depreciate_in_group_flag       => px_inv_rec.depreciate_in_group_flag,
1574                        -- added for R12
1575                        X_invoice_distribution_id        => px_inv_rec.invoice_distribution_id,
1576                        X_invoice_line_number            => px_inv_rec.invoice_line_number,
1577                        X_po_distribution_id             => px_inv_rec.po_distribution_id,
1578                        X_exchange_rate                  => p_inv_rate_rec.exchange_rate,
1579                        X_mrc_sob_type_code              => p_mrc_sob_type_code,
1580                        X_Calling_Fn                     => 'fa_invoice_api_pkg.process_invoice'
1581                       );
1582 
1583    return true;
1584 
1585 EXCEPTION
1586    when error_found then
1587       fa_srvr_msg.add_message(calling_fn => 'fa_invoice_pvt.process_invoice');
1588       return false;
1589 
1590    when others then
1591       fa_srvr_msg.add_sql_error(calling_fn => 'fa_invoice_pvt.process_invoice');
1592       return false;
1593 
1594 END process_invoice;
1595 
1596 --------------------------------------------------------------------------------
1597 
1598 FUNCTION  get_inv_rate
1599    (p_trans_rec                IN     FA_API_TYPES.trans_rec_type,
1600     p_asset_hdr_rec            IN     FA_API_TYPES.asset_hdr_rec_type,
1601     p_inv_trans_rec            IN     FA_API_TYPES.inv_trans_rec_type,
1602     px_inv_tbl                 IN OUT NOCOPY FA_API_TYPES.inv_tbl_type,
1603     p_log_level_rec            in fa_api_types.log_level_rec_type default null
1604    ) RETURN BOOLEAN IS
1605 
1606    i               number := 0;
1607    l_inv_rate_ctr  number := 0;
1608 
1609    cursor c_mc_invoices (p_source_line_id in number) is
1610    select mcai.set_of_books_id,
1611           mcai.exchange_rate,
1612           mcai.fixed_assets_cost
1613      from fa_mc_asset_invoices mcai,
1614           fa_mc_book_controls  mcbk
1615     where mcai.source_line_id  = p_source_line_id
1616       and mcai.set_of_books_id = mcbk.set_of_books_id
1617       and mcbk.book_type_code  = p_asset_hdr_rec.book_type_code
1618       and mcbk.enabled_flag    = 'Y';
1619 
1620    l_calling_fn              varchar2(35) := 'fa_inv_pvt.get_inv_rate';
1621 
1622    error_found     EXCEPTION;
1623 
1624 BEGIN
1625 
1626    for i in 1..px_inv_tbl.count loop
1627 
1628       -- reset the starting value to one in each invoice
1629       l_inv_rate_ctr := 0;
1630 
1631       -- only populate for existing source lines
1632       if (px_inv_tbl(i).source_line_id is not null) then
1633 
1634           if (g_print_debug) then
1635               fa_debug_pkg.add(l_calling_fn,
1636                                'source_line_id',
1637                                px_inv_tbl(i).source_line_id);
1638           end if;
1639 
1640           for c_rec in c_mc_invoices(px_inv_tbl(i).source_line_id) loop
1641 
1642              if (g_print_debug) then
1643                 fa_debug_pkg.add(l_calling_fn,
1644                                  'set_of_books_id',
1645                                  c_rec.set_of_books_id);
1646                 fa_debug_pkg.add(l_calling_fn,
1647                                  'exchange_rate',
1648                                  c_rec.exchange_rate);
1649              end if;
1650 
1651              l_inv_rate_ctr := l_inv_rate_ctr  + 1;
1652              px_inv_tbl(i).inv_rate_tbl(l_inv_rate_ctr).set_of_books_id  := c_rec.set_of_books_id;
1653              px_inv_tbl(i).inv_rate_tbl(l_inv_rate_ctr).exchange_rate    := c_rec.exchange_rate;
1654              px_inv_tbl(i).inv_rate_tbl(l_inv_rate_ctr).cost             := c_rec.fixed_assets_cost;
1655 
1656           end loop;
1657       end if;
1658    end loop;
1659    return true;
1660 
1661 EXCEPTION
1662    when error_found then
1663       fa_srvr_msg.add_message(calling_fn => 'fa_invoice_pvt.get_inv_rate');
1664       return false;
1665 
1666    when others then
1667       fa_srvr_msg.add_sql_error(calling_fn => 'fa_invoice_pvt.get_inv_rate');
1668       return false;
1669 
1670 END get_inv_rate;
1671 
1672 --------------------------------------------------------------------------------
1673 
1674 FUNCTION post_clearing
1675    (p_trans_rec                IN     FA_API_TYPES.trans_rec_type,
1676     p_asset_hdr_rec            IN     FA_API_TYPES.asset_hdr_rec_type,
1677     p_asset_desc_rec           IN     FA_API_TYPES.asset_desc_rec_type,
1678     p_asset_type_rec           IN     FA_API_TYPES.asset_type_rec_type,
1679     p_asset_cat_rec            IN     FA_API_TYPES.asset_cat_rec_type,
1680     p_inv_trans_rec            IN     FA_API_TYPES.inv_trans_rec_type,
1681     p_payables_cost_tbl        IN     payables_cost_tbl_type,
1682     p_payables_cost_mrc_tbl    IN     payables_cost_tbl_type,
1683     p_log_level_rec            in fa_api_types.log_level_rec_type default null
1684    ) RETURN BOOLEAN IS
1685 
1686    TYPE num_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
1687 
1688    i                  number;
1689    l_adj              fa_adjust_type_pkg.fa_adj_row_struct;
1690    sob_processed_tbl  num_tbl;
1691    l_sob_tbl          FA_CACHE_PKG.fazcrsob_sob_tbl_type;
1692    l_sob_index        number;
1693 
1694    l_calling_fn              varchar2(35) := 'fa_inv_pvt.post_clearing';
1695 
1696    error_found        exception;
1697 
1698 BEGIN
1699 
1700    l_adj.transaction_header_id    := p_trans_rec.transaction_header_id;
1701    l_adj.asset_id                 := p_asset_hdr_rec.asset_id;
1702    l_adj.book_type_code           := p_asset_hdr_rec.book_type_code;
1703    l_adj.period_counter_created   := fa_cache_pkg.fazcbc_record.last_period_counter + 1;
1704    l_adj.period_counter_adjusted  := fa_cache_pkg.fazcbc_record.last_period_counter + 1;
1705    l_adj.current_units            := p_asset_desc_rec.current_units ;
1706    l_adj.selection_mode           := FA_ADJUST_TYPE_PKG.FA_AJ_ACTIVE;
1707    l_adj.selection_thid           := 0;
1708    l_adj.selection_retid          := 0;
1709    l_adj.leveling_flag            := TRUE;
1710    l_adj.last_update_date         := p_trans_rec.who_info.last_update_date;
1711 
1712    l_adj.flush_adj_flag           := FALSE;
1713    l_adj.gen_ccid_flag            := FALSE;
1714    l_adj.annualized_adjustment    := 0;
1715    l_adj.distribution_id          := 0;
1716 
1717    l_adj.adjustment_type          := 'COST CLEARING';
1718    l_adj.source_type_code         := p_trans_rec.transaction_type_code;
1719 
1720    if l_adj.source_type_code = 'CIP ADJUSTMENT' then
1721       l_adj.account_type     := 'CIP_CLEARING_ACCT';
1722    else
1723       l_adj.account_type     := 'ASSET_CLEARING_ACCT';
1724    end if;
1725 
1726    fnd_profile.put('GL_SET_OF_BKS_ID', G_profile_set_of_books_id);
1727    fnd_client_info.set_currency_context (G_orig_currency_context);
1728 
1729 
1730    -- post the primary rows
1731    for i in 1..p_payables_cost_tbl.count loop
1732 
1733       -- SLA changes
1734       l_adj.asset_invoice_id    := p_payables_cost_tbl(i).asset_invoice_id;
1735       l_adj.source_line_id      := p_payables_cost_tbl(i).source_line_id;
1736       l_adj.source_dest_code    := p_payables_cost_tbl(i).source_dest_code;
1737 
1738       l_adj.code_combination_id := nvl(p_payables_cost_tbl(i).payables_code_combination_id, 0);
1739       -- l_adj.asset_invoice_id    := 0;  -- could pass this in tbl
1740 
1741       if p_payables_cost_tbl(i).payables_cost > 0 then
1742          l_adj.debit_credit_flag   := 'CR';
1743          l_adj.adjustment_amount   := p_payables_cost_tbl(i).payables_cost ;
1744       else
1745          l_adj.debit_credit_flag   := 'DR';
1746          l_adj.adjustment_amount   := -p_payables_cost_tbl(i).payables_cost ;
1747       end if;
1748 
1749       l_adj.mrc_sob_type_code := 'P';
1750 
1751       if not FA_INS_ADJUST_PKG.faxinaj
1752                (l_adj,
1753                 p_trans_rec.who_info.last_update_date,
1754                 p_trans_rec.who_info.last_updated_by,
1755                 p_trans_rec.who_info.last_update_login) then
1756          raise error_found;
1757       end if;
1758    end loop;
1759 
1760    -- flush them
1761    l_adj.transaction_header_id := 0;
1762    l_adj.flush_adj_flag        := TRUE;
1763    l_adj.leveling_flag         := TRUE;
1764 
1765    if not FA_INS_ADJUST_PKG.faxinaj
1766             (l_adj,
1767              p_trans_rec.who_info.last_update_date,
1768              p_trans_rec.who_info.last_updated_by,
1769              p_trans_rec.who_info.last_update_login) then
1770       raise error_found;
1771    end if;
1772 
1773    -- need to reset this after each flush
1774    l_adj.transaction_header_id  := p_trans_rec.transaction_header_id;
1775 
1776 
1777    -- call the sob cache to get the table of sob_ids
1778    if not FA_CACHE_PKG.fazcrsob
1779             (x_book_type_code => p_asset_hdr_rec.book_type_code,
1780              x_sob_tbl        => l_sob_tbl) then
1781       raise error_found;
1782    end if;
1783 
1784    if (g_print_debug) then
1785       fa_debug_pkg.add('post clearing',
1786                        'entering',
1787                        'ALC loop');
1788    end if;
1789 
1790 
1791    -- post reporting rows one reporting book at a time
1792    FOR l_sob_index in 1..l_sob_tbl.count LOOP
1793 
1794       fnd_profile.put('GL_SET_OF_BKS_ID', l_sob_tbl(l_sob_index));
1795       fnd_client_info.set_currency_context(to_char(l_sob_tbl(l_sob_index)));
1796 
1797       for i in 1..p_payables_cost_mrc_tbl.count loop
1798 
1799          if (p_payables_cost_mrc_tbl(i).set_of_books_id = l_sob_tbl(l_sob_index)) then
1800 
1801             -- SLA changes
1802             l_adj.asset_invoice_id    := p_payables_cost_mrc_tbl(i).asset_invoice_id;
1803             l_adj.source_line_id      := p_payables_cost_mrc_tbl(i).source_line_id;
1804             l_adj.source_dest_code    := p_payables_cost_mrc_tbl(i).source_dest_code;
1805             l_adj.code_combination_id := nvl(p_payables_cost_mrc_tbl(i).payables_code_combination_id, 0);
1806             -- l_adj.asset_invoice_id    := 0;  -- could pass this in tbl
1807 
1808             if p_payables_cost_mrc_tbl(i).payables_cost > 0 then
1809                l_adj.debit_credit_flag   := 'CR';
1810                l_adj.adjustment_amount   := p_payables_cost_mrc_tbl(i).payables_cost ;
1811             else
1812                l_adj.debit_credit_flag   := 'DR';
1813                l_adj.adjustment_amount   := -p_payables_cost_mrc_tbl(i).payables_cost ;
1814             end if;
1815 
1816             l_adj.mrc_sob_type_code := 'R';
1817 
1818             if not FA_INS_ADJUST_PKG.faxinaj
1819                      (l_adj,
1820                       p_trans_rec.who_info.last_update_date,
1821                       p_trans_rec.who_info.last_updated_by,
1822                       p_trans_rec.who_info.last_update_login) then
1823                raise error_found;
1824             end if;
1825 
1826          end if;
1827 
1828       end loop; -- invoices
1829 
1830       -- flush them
1831       l_adj.transaction_header_id := 0;
1832       l_adj.flush_adj_flag        := TRUE;
1833       l_adj.leveling_flag         := TRUE;
1834 
1835       if not FA_INS_ADJUST_PKG.faxinaj
1836                (l_adj,
1837                 p_trans_rec.who_info.last_update_date,
1838                 p_trans_rec.who_info.last_updated_by,
1839                 p_trans_rec.who_info.last_update_login) then
1840          raise error_found;
1841       end if;
1842 
1843       -- need to reset this after each flush
1844       l_adj.transaction_header_id    := p_trans_rec.transaction_header_id;
1845 
1846    end loop; -- mrc
1847 
1848    fnd_profile.put('GL_SET_OF_BKS_ID', G_profile_set_of_books_id);
1849    fnd_client_info.set_currency_context (G_orig_currency_context);
1850 
1851    return true;
1852 
1853 EXCEPTION
1854    when error_found then
1855       fnd_profile.put('GL_SET_OF_BKS_ID', G_profile_set_of_books_id);
1856       fnd_client_info.set_currency_context (G_orig_currency_context);
1857       fa_srvr_msg.add_message(calling_fn => 'fa_invoice_pvt.post_clearing');
1858       return false;
1859 
1860    when others then
1861       fnd_profile.put('GL_SET_OF_BKS_ID', G_profile_set_of_books_id);
1862       fnd_client_info.set_currency_context (G_orig_currency_context);
1863       fa_srvr_msg.add_sql_error(calling_fn => 'fa_invoice_pvt.post_clearing');
1864       return false;
1865 
1866 END;
1867 
1868 
1869 END FA_INVOICE_PVT;