DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSTPAPBR

Source


1 PACKAGE BODY CSTPAPBR AS
2 /* $Header: CSTAPBRB.pls 120.63.12020000.6 2013/05/22 06:43:38 yunfchen ship $ */
3 
4 g_debug_flag VARCHAR2(1) := NVL(fnd_profile.value('MRP_DEBUG'), 'N'); /* Bug 4586534 */
5 
6 /*============================================================================+
7 | This procedure is called by the Accounting Package for the Accounting Lib.  |
8 | It first gets the details of the transaction from either                    |
9 | MTL_MATERIAL_TRANSACTIONS or WIP_TRANSACTIONS depending on the type of txn  |
10 | that is being processed. The appropriate procedure is then called to create |
11 | the accounting entry lines in the form of a PL/SQL table.                   |
12 |============================================================================*/
13 
14 PROCEDURE create_acct_lines (
15         i_legal_entity          IN      NUMBER,
16         i_cost_type_id          IN      NUMBER,
17         i_cost_group_id         IN      NUMBER,
18         i_period_id             IN      NUMBER,
19         i_transaction_id        IN      NUMBER,
20         i_event_type_id         IN      VARCHAR2,
21         i_txn_type_flag         IN      VARCHAR2, --Bug 4586534
22         o_err_num                OUT NOCOPY        NUMBER,
23         o_err_code                OUT NOCOPY        VARCHAR2,
24         o_err_msg                OUT NOCOPY        VARCHAR2
25 )IS
26         l_ae_txn_rec            CSTPALTY.CST_AE_TXN_REC_TYPE;
27         l_ae_line_rec_tbl       CSTPALTY.CST_AE_LINE_TBL_TYPE := CSTPALTY.CST_AE_LINE_TBL_TYPE();
28         l_ae_err_rec            CSTPALTY.CST_AE_ERR_REC_TYPE;
29         l_cost_type_name        VARCHAR2(10);
30         l_cost_group_name        VARCHAR2(10);
31         l_period_name                VARCHAR2(15);
32         l_operating_unit        NUMBER;
33         l_txn_cost_group_id     NUMBER;
34         l_xfer_cg_exists        NUMBER;
35         l_stmt_num                NUMBER;
36         CST_TXN_TYPE_FAIL        EXCEPTION;
37         CST_DIST_PKG_ERROR        EXCEPTION;
38 BEGIN
39 
40   IF g_debug_flag = 'Y' THEN
41     fnd_file.put_line(fnd_file.log, 'CSTPAPBR.Create_acct_lines <<');
42     fnd_file.put_line(fnd_file.log, 'i_transaction_id: ' || i_transaction_id);
43   END IF;
44 
45   l_stmt_num := 10;
46   o_err_num := 0;
47 
48   ------------------------------------------------------------------------------
49   -- Determine the transaction type based on the event type
50   -- the event type is a concatenated string of the form
51   -- <txn type id>-<txn action id>-<txn source type id> for INV
52   -- <txn type id>                                      for WIP
53   -----------------------------------------------------------------------------
54 
55     /* Bug 4586534 */
56   l_ae_txn_rec.txn_type_flag :=  i_txn_type_flag ;
57   /*
58   SELECT
59   transaction_type_flag
60   INTO
61   l_ae_txn_rec.txn_type_flag
62   FROM
63   cst_accounting_event_types_v caet
64   WHERE
65   caet.event_type = i_event_type_id;
66   */
67   -----------------------------------------------------------------------------
68   -- Get the set of books id
69   -----------------------------------------------------------------------------
70 
71   IF g_debug_flag = 'Y' THEN
72     fnd_file.put_line(fnd_file.log,'Event type: '||(i_event_type_id));
73   END IF;
74 
75   l_stmt_num := 15;
76   SELECT
77   set_of_books_id
78   INTO
79   l_ae_txn_rec.set_of_books_id
80   FROM
81   cst_le_cost_types clct
82   WHERE
83   clct.legal_entity = i_legal_entity AND
84   clct.cost_type_id = i_cost_type_id;
85 
86   ----------------------------------------------------------------------------
87   -- If the txn type is INV, then get transaction data from MMT
88   -- If WIP, then get transaction data from WT
89   ----------------------------------------------------------------------------
90 
91   IF (l_ae_txn_rec.txn_type_flag = 'INV') THEN
92 
93     l_ae_txn_rec.source_table := 'MMT';
94     l_ae_txn_rec.source_id := i_transaction_id;
95 
96     l_stmt_num := 20;
97 
98     SELECT
99     i_event_type_id,
100     mmt.transaction_action_id,
101     mmt.transaction_source_type_id,
102     nvl(mmt.transaction_source_id,-1),
103     mmt.transaction_type_id,
104     null,
105     mmt.transaction_id,
106     mmt.inventory_item_id,
107     i_legal_entity,
108     i_cost_type_id,
109     i_cost_group_id,
110     nvl(mmt.periodic_primary_quantity,mmt.primary_quantity),
111     mmt.subinventory_code,
112     mmt.transfer_organization_id,
113     mmt.transfer_subinventory,
114     mmt.transfer_transaction_id,
115     nvl(mmt.distribution_account_id,-1),
116     mmt.currency_code,
117     mmt.currency_conversion_type,
118     nvl(mmt.currency_conversion_date,transaction_date),
119     nvl(mmt.currency_conversion_rate,-1),
120     'MTL',
121     i_period_id,
122     mmt.transaction_date,
123     mmt.organization_id,
124     nvl(mmt.material_account, -1),
125     nvl(mmt.material_overhead_account, -1),
126     nvl(mmt.resource_account, -1),
127     nvl(mmt.outside_processing_account, -1),
128     nvl(mmt.overhead_account, -1),
129     decode(mmt.flow_schedule,'Y',1,0),
130     decode(msi.inventory_asset_flag,'Y',0,1),
131     mmt.repetitive_line_id,
132     nvl(mmt.encumbrance_account, -1),
133     nvl(mmt.encumbrance_amount, 0),
134     -- Reveue / COGS Matching
135     nvl(mmt.so_issue_account_type,0),
136     mmt.trx_source_line_id,
137     mmt.cogs_recognition_percent,
138     nvl(mmt.expense_account_id,-1)
139     INTO
140     l_ae_txn_rec.event_type_id,
141     l_ae_txn_rec.txn_action_id,
142     l_ae_txn_rec.txn_src_type_id,
143     l_ae_txn_rec.txn_src_id,
144     l_ae_txn_rec.txn_type_id,
145     --l_ae_txn_rec.wip_txn_type,
146     l_ae_txn_rec.txn_type,
147     l_ae_txn_rec.transaction_id,
148     l_ae_txn_rec.inventory_item_id,
149     l_ae_txn_rec.legal_entity_id,
150     l_ae_txn_rec.cost_type_id,
151     l_ae_txn_rec.cost_group_id,
152     l_ae_txn_rec.primary_quantity,
153     l_ae_txn_rec.subinventory_code,
154     l_ae_txn_rec.xfer_organization_id,
155     l_ae_txn_rec.xfer_subinventory,
156     l_ae_txn_rec.xfer_transaction_id,
157     l_ae_txn_rec.dist_acct_id,
158     l_ae_txn_rec.currency_code,
159     l_ae_txn_rec.currency_conv_type,
160     l_ae_txn_rec.currency_conv_date,
161     l_ae_txn_rec.currency_conv_rate,
162     l_ae_txn_rec.ae_category,
163     l_ae_txn_rec.accounting_period_id,
164     l_ae_txn_rec.accounting_date,
165     l_ae_txn_rec.organization_id,
166     l_ae_txn_rec.mat_account,
167     l_ae_txn_rec.mat_ovhd_account,
168     l_ae_txn_rec.res_account,
169     l_ae_txn_rec.osp_account,
170     l_ae_txn_rec.ovhd_account,
171     l_ae_txn_rec.flow_schedule,
172     l_ae_txn_rec.exp_item ,
173     l_ae_txn_rec.line_id,
174     l_ae_txn_rec.encum_account,
175     l_ae_txn_rec.encum_amount,
176     -- Revenue / COGS Matching
177     l_ae_txn_rec.so_issue_acct_type,
178     l_ae_txn_rec.om_line_id,
179     l_ae_txn_rec.cogs_percentage,
180     l_ae_txn_rec.expense_account_id
181     FROM
182     mtl_material_transactions mmt,
183     mtl_system_items msi
184     WHERE
185     mmt.inventory_item_id = msi.inventory_item_id AND
186     mmt.organization_id = msi.organization_id AND
187     mmt.transaction_id = i_transaction_id;
188 
189     l_stmt_num := 25;
190     select
191     transaction_type_name
192     into
193     l_ae_txn_rec.description
194     from
195     mtl_transaction_types
196     where transaction_type_id = l_ae_txn_rec.txn_type_id;
197 
198     IF (NVL(l_ae_txn_rec.txn_src_id,-1) > 0 AND
199                            l_ae_txn_rec.txn_src_type_id = 5) THEN
200       l_stmt_num := 27;
201       SELECT  entity_type
202       INTO    l_ae_txn_rec.wip_entity_type
203       FROM    wip_entities we
204       WHERE   we.wip_entity_id = l_ae_txn_rec.txn_src_id;
205 
206     ELSE
207 
208       l_ae_txn_rec.wip_entity_type := NULL;
209 
210     END IF;
211 
212     IF (l_ae_txn_rec.txn_src_type_id in (1,7,8) ) THEN
213 
214 /* The following Select statement has already been commented out.
215    The Select statement will still however be made to refer to the
216    CST_ORGANIZATION_DEFINITIONS as an impact of the HR-PROFILE option */
217 
218 
219 /* this new SELECT query below has been comented out because the above query had
220   been commented out.The only change being this new query refers to
221   cst_organization_definitions.In the future if this above query needs to be
222   uncommented,please DO NOT uncomment the above query.Instead uncomment
223   this below query*/
224 
225      l_stmt_num := 28;
226      IF (l_ae_txn_rec.txn_src_type_id = 1) THEN
227 
228        SELECT NVL(operating_unit,-1)
229        INTO l_operating_unit
230        FROM cst_organization_definitions
231        WHERE organization_id = l_ae_txn_rec.organization_id;
232 
233      ELSIF (l_ae_txn_rec.txn_src_type_id IN (7,8)) THEN /* Internal req/internal order */
234 
235        /* Get cost group of the transaction org */
236        BEGIN
237            SELECT NVL(cost_group_id, -1)
238            INTO   l_txn_cost_group_id
239            FROM   cst_cost_group_assignments
240            WHERE  organization_id = l_ae_txn_rec.organization_id;
241        EXCEPTION
242            WHEN no_data_found THEN
243              l_txn_cost_group_id := -1;
244        END;
245 
246        /* Select the operating unit of the org belonging to the processing cost group */
247        SELECT NVL(operating_unit,-1)
248        INTO l_operating_unit
249        FROM cst_organization_definitions
250        WHERE organization_id = decode(i_cost_group_id,
251                                          l_txn_cost_group_id, l_ae_txn_rec.organization_id,
252                                                                          l_ae_txn_rec.xfer_organization_id);
253          END IF;
254 
255      IF g_debug_flag = 'Y' THEN
256        fnd_file.put_line(fnd_file.log,'Operating Unit: '||to_char(l_operating_unit));
257      END IF;
258 
259        l_stmt_num := 29;
260        SELECT decode(l_ae_txn_rec.txn_src_type_id,
261                      1,g1.ENCUMBRANCE_TYPE_ID,
262                      g2.ENCUMBRANCE_TYPE_ID)
263        INTO   l_ae_txn_rec.encum_type_id
264        FROM   gl_encumbrance_types g1, gl_encumbrance_types g2
265        WHERE  g1.encumbrance_type_key = 'Obligation' AND
266               g2.encumbrance_type_key = 'Commitment';
267 
268     END IF;
269 
270 
271   ELSIF (l_ae_txn_rec.txn_type_flag = 'WIP') THEN
272 
273     l_ae_txn_rec.source_table := 'WT';
274     l_ae_txn_rec.source_id := i_transaction_id;
275 
276     l_stmt_num := 30;
277     select
278     i_event_type_id,
279     null,
280     null,
281     wt.wip_entity_id,
282     --null,  moved to after transaction_type to populate transaction_type
283     wt.transaction_type,  -- assign to transaction_type_id
284     null,
285     wt.transaction_id,
286     wt.primary_item_id,
287     i_legal_entity,
288     i_cost_type_id,
289     i_cost_group_id,
290     null,
291     wt.primary_quantity,
292     null,
293     null,
294     null,
295     null,
296     null,
297     wt.currency_code,
298     wt.currency_conversion_type,
299     nvl(wt.currency_conversion_date,transaction_date),
300     nvl(wt.currency_conversion_rate,-1),
301     'WIP',
302     i_period_id,
303     wt.transaction_date,
304     wt.organization_id,
305     null,
306     null,
307     null,
308     null,
309     null,
310     0,
311     wt.line_id,
312  /* added decode on wt.primary_item_id for non-std jobs - fix 3179823 */
313     DECODE(transaction_type,6,0, decode(wt.primary_item_id,NULL,-1,1))
314 --{BUG#9356654
315 , ENCUMBRANCE_TYPE_ID
316 , ENCUMBRANCE_CCID
317 , ENCUMBRANCE_AMOUNT
318 --}
319     INTO
320     l_ae_txn_rec.event_type_id,
321     l_ae_txn_rec.txn_action_id,
322     l_ae_txn_rec.txn_src_type_id,
323     l_ae_txn_rec.txn_src_id,
324     l_ae_txn_rec.txn_type_id,
325     --l_ae_txn_rec.wip_txn_type,
326     l_ae_txn_rec.txn_type,
327     l_ae_txn_rec.transaction_id,
328     l_ae_txn_rec.inventory_item_id,
329     l_ae_txn_rec.legal_entity_id,
330     l_ae_txn_rec.cost_type_id,
331     l_ae_txn_rec.cost_group_id      ,
332     l_ae_txn_rec.xfer_cost_group_id,
333     l_ae_txn_rec.primary_quantity,
334     l_ae_txn_rec.subinventory_code,
335     l_ae_txn_rec.xfer_organization_id,
336     l_ae_txn_rec.xfer_subinventory,
337     l_ae_txn_rec.xfer_transaction_id,
338     l_ae_txn_rec.dist_acct_id,
339     l_ae_txn_rec.currency_code,
340     l_ae_txn_rec.currency_conv_type,
341     l_ae_txn_rec.currency_conv_date,
342     l_ae_txn_rec.currency_conv_rate,
343     l_ae_txn_rec.ae_category,
344     l_ae_txn_rec.accounting_period_id,
345     l_ae_txn_rec.accounting_date,
346     l_ae_txn_rec.organization_id,
347     l_ae_txn_rec.mat_account,
348     l_ae_txn_rec.mat_ovhd_account,
349     l_ae_txn_rec.res_account,
350     l_ae_txn_rec.osp_account,
351     l_ae_txn_rec.ovhd_account,
352     l_ae_txn_rec.flow_schedule,
353     l_ae_txn_rec.line_id,
354     l_ae_txn_rec.exp_item
355 --{BUG#9356654
356 ,    l_ae_txn_rec.encum_type_id
357 ,    l_ae_txn_rec.encum_account
358 ,    l_ae_txn_rec.encum_amount
359 --}
360     FROM
361     wip_transactions wt
362     WHERE
363     wt.transaction_id = i_transaction_id;
364 
365     SELECT  entity_type
366     INTO    l_ae_txn_rec.wip_entity_type
367     FROM    wip_entities we
368     WHERE   we.wip_entity_id = l_ae_txn_rec.txn_src_id;
369 
370     if (l_ae_txn_rec.exp_item = 1) then
371     select
372     decode(msi.inventory_asset_flag,'Y',0,1)
373     INTO
374     l_ae_txn_rec.exp_item
375     FROM
376     mtl_system_items msi,
377     wip_transactions wt
378     WHERE
379     msi.inventory_item_id = wt.primary_item_id AND
380     wt.organization_id = msi.organization_id AND
381     wt.transaction_id = i_transaction_id;
382     end if;
383 
384     select
385     meaning
386     into
387     l_ae_txn_rec.description
388     from
389     mfg_lookups
390     where
391     lookup_type = 'WIP_TRANSACTION_TYPE'
392     and lookup_code = l_ae_txn_rec.txn_type_id;
393     --and lookup_code = l_ae_txn_rec.wip_txn_type;
394 
395 
396 
397 
398   ELSE
399     RAISE CST_TXN_TYPE_FAIL;
400   END IF;
401 
402   SELECT
403   cost_type
404   INTO
405   l_cost_type_name
406   FROM
407   cst_cost_types
408   WHERE
409   cost_type_id = i_cost_type_id;
410 
411   SELECT
412   cost_group
413   INTO
414   l_cost_group_name
415   FROM
416   cst_cost_groups
417   WHERE
418   cost_group_id = i_cost_group_id;
419 
420   SELECT
421   period_name
422   INTO
423   l_period_name
424   FROM
425   cst_pac_periods
426   WHERE
427   pac_period_id = i_period_id;
428 
429   l_ae_txn_rec.description := l_ae_txn_rec.description ||' : '||l_cost_type_name||' : '||l_cost_group_name||' : '||l_period_name;
430 
431 
432   l_stmt_num := 40;
433 
434 
435   -----------------------------------------------------------------------------
436   -- Get the transfer organization and cost group (if any)
437   -----------------------------------------------------------------------------
438   SELECT
439   count(organization_id)
440   INTO
441   l_xfer_cg_exists
442   FROM
443   cst_cost_group_assignments
444   WHERE
445   organization_id = nvl(l_ae_txn_rec.xfer_organization_id,-1)
446   AND rownum < 2; /* bug 4586534 added rownum=1 for performance as its only existence check. */
447 
448   IF (l_xfer_cg_exists = 0) THEN
449     l_ae_txn_rec.xfer_cost_group_id := NULL;
450   ELSE
451     SELECT
452     DECODE (l_ae_txn_rec.xfer_organization_id,NULL,NULL,cost_group_id)
453     INTO
454     l_ae_txn_rec.xfer_cost_group_id
455     FROM
456     cst_cost_group_assignments
457     WHERE
458     organization_id = nvl(l_ae_txn_rec.xfer_organization_id,organization_id);
459   END IF;
460 
461   -----------------------------------------------------------------------------
462   -- Get the period name for the period being processed
463   -----------------------------------------------------------------------------
464 
465   l_stmt_num := 50;
466   SELECT
467   period_name
468   INTO
469   l_ae_txn_rec.accounting_period_name
470   FROM
471   cst_pac_periods
472   WHERE
473   pac_period_id = l_ae_txn_rec.accounting_period_id AND
474   cost_type_id = l_ae_txn_rec.cost_type_id AND
475   legal_entity = l_ae_txn_rec.legal_entity_id;
476 
477   l_stmt_num := 60;
478   -----------------------------------------------------------------------------
479   -- Get the category for the item for the org
480   -- This will be used later to determine the category accounts from MFCA
481   -----------------------------------------------------------------------------
482 
483   /* Bug 12795089 : Category is not required for wip transactions, which use
484      accounts defined for the job
485   IF ((l_ae_txn_rec.txn_type_id = 6 and l_ae_txn_rec.txn_type_flag = 'WIP')
486        or (l_ae_txn_rec.inventory_item_id is NULL and l_ae_txn_rec.txn_type_flag = 'WIP')) THEN */
487 
488   IF l_ae_txn_rec.txn_type_flag = 'WIP' THEN
489 
490     l_ae_txn_rec.category_id := NULL;
491 
492   ELSE
493 
494     /* Added as bugfix 2045870:
495        modified the check on functional_area_id from 1(inventory) to 5 (costing) */
496     SELECT
497     category_id
498     INTO
499     l_ae_txn_rec.category_id
500     FROM
501     mtl_item_categories mic
502     WHERE
503     mic.inventory_item_id = l_ae_txn_rec.inventory_item_id
504     AND /* Bug 12794529: In case of interorg transactions, we could be processing the transfer organization */
505     mic.organization_id = Decode(i_cost_group_id, Nvl(l_ae_txn_rec.xfer_cost_group_id, -1), l_ae_txn_rec.xfer_organization_id, l_ae_txn_rec.organization_id)
506     AND
507     mic.category_set_id = (SELECT category_set_id
508                            FROM mtl_default_category_sets
509                            WHERE functional_area_id = 5);
510 
511   END IF;
512 
513 
514   l_stmt_num := 80;
515 
516   -----------------------------------------------------------------------------
517   -- Call the INV procedure if txn type is INV
518   -- Call the WIP procedure if txn type is WIP
519   -----------------------------------------------------------------------------
520 
521   IF (l_ae_txn_rec.txn_type_flag = 'INV') THEN
522      create_inv_ae_lines(
523         l_ae_txn_rec,
524         l_ae_line_rec_tbl,
525         l_ae_err_rec);
526   ELSIF (l_ae_txn_rec.txn_type_flag = 'WIP') THEN
527      create_wip_ae_lines(
528         l_ae_txn_rec,
529         l_ae_line_rec_tbl,
530         l_ae_err_rec);
531   END IF;
532 
533   IF (l_ae_err_rec.l_err_num <> 0 and l_ae_err_rec.l_err_num is not null) THEN
534     RAISE CST_DIST_PKG_ERROR;
535   END IF;
536 
537 
538   -----------------------------------------------------------------------------
539   -- If accounting entry lines were returned by the procedure , insert into the
540   -- accounting tables
541   -----------------------------------------------------------------------------
542 
543   IF (l_ae_err_rec.l_err_num IS NULL OR l_ae_err_rec.l_err_num = 0) THEN
544     IF (l_ae_line_rec_tbl.EXISTS(1)) THEN
545     CSTPALBR.insert_ae_lines(
546         l_ae_txn_rec,
547         l_ae_line_rec_tbl,
548         l_ae_err_rec);
549     END IF;
550   END IF;
551 
552   IF g_debug_flag = 'Y' THEN
553     fnd_file.put_line(fnd_file.log, 'Create_acct_lines >>');
554   END IF;
555 
556 
557 EXCEPTION
558 WHEN CST_DIST_PKG_ERROR THEN
559         o_err_num := 30001;
560         o_err_code := SQLCODE;
561         o_err_msg :=  to_char(l_ae_err_rec.l_err_num)||l_ae_err_rec.l_err_msg;
562 
563 WHEN CST_TXN_TYPE_FAIL THEN
564         o_err_num := 30002;
565         o_err_code := SQLCODE;
566         o_err_msg :=  'CSTPAPBR.create_acct_lines :Invalid Transaction Type Code';
567 
568 WHEN OTHERS THEN
569   o_err_num := SQLCODE;
570   o_err_code := '';
571   o_err_msg := 'CSTPAPBR.create_acct_lines : ' || to_char(l_stmt_num) || ':'||
572   substr(SQLERRM,1,180);
573 
574 
575 END create_acct_lines;
576 
577 /*============================================================================+
578 | This procedure processes the transaction data and creates accounting entry  |
579 | lines in the form of PL/SQL table and returns to the main procedure.        |
580 |============================================================================*/
581 
582 PROCEDURE create_inv_ae_lines(
583   i_ae_txn_rec          IN     CSTPALTY.cst_ae_txn_rec_type,
584   o_ae_line_rec_tbl     OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
585   o_ae_err_rec          OUT NOCOPY    CSTPALTY.cst_ae_err_rec_type
586 ) IS
587   l_ae_line_tbl                CSTPALTY.cst_ae_line_tbl_type := CSTPALTY.cst_ae_line_tbl_type();
588   l_curr_rec                   CSTPALTY.cst_ae_curr_rec_type;
589   l_err_rec                    CSTPALTY.cst_ae_err_rec_type;
590   l_ae_txn_rec1                CSTPALTY.CST_AE_TXN_REC_TYPE;
591 
592   /* Drop Ship/Global Proc changes */
593   l_logical_transaction   NUMBER;
594 
595   l_curr_from_org         NUMBER;
596   l_curr_to_org           NUMBER;
597 
598   l_txn_cost_group_id     NUMBER;
599   l_fob_point             NUMBER;
600   l_enc_rev                    NUMBER;
601   l_hook                       NUMBER;
602   l_stmt_num                   NUMBER := 0;
603 
604   --Bug 14541396 Forward port
605 
606 	l_par_rcv_txn                NUMBER;
607 	l_rcv_txn                    NUMBER;
608 	l_acq_exists                 NUMBER;
609 	l_nqr                        NUMBER;
610 	l_costed_quantity            NUMBER;
611 	l_acq_cost                   NUMBER;
612 	l_acq_cost_ent_inv           NUMBER;
613 	l_acq_cost_ent_po            NUMBER;
614 	l_acq_cost_ent               NUMBER;
615 	l_curr_conv_rate             NUMBER;
616 
617   -- FP 12.1.1 bug 7346244 fix
618   l_enc_amount                 NUMBER;
619   l_enc_account                NUMBER;
620 
621   process_error                EXCEPTION;
622 
623   l_api_name   CONSTANT VARCHAR2(30)    := 'CSTPAPBR.create_inv_ae_lines';
624   l_pd_txfr_ind  NUMBER;
625   l_exp_sub      NUMBER;
626 
627 BEGIN
628 
629   IF g_debug_flag = 'Y' THEN
630     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': begin <<'
631                            || ' transaction_id: ' || i_ae_txn_rec.transaction_id);
632   END IF;
633 
634   l_ae_line_tbl := CSTPALTY.cst_ae_line_tbl_type();
635 --l_ae_line_tbl := CSTPALBR.cst_ae_line_tbl_type();
636 
637 -- Initialize local variables.
638 -- ---------------------------
639   l_err_rec.l_err_num := 0;
640   l_err_rec.l_err_code := '';
641   l_err_rec.l_err_msg := '';
642 
643 -- Populate the Currency Record Type
644 -- ---------------------------------
645   l_stmt_num := 10;
646   select currency_code
647   into l_curr_rec.pri_currency
648   from gl_sets_of_books
649   where set_of_books_id = i_ae_txn_rec.set_of_books_id;
650 
651   /* Changes for INVCONV Bug 5337465 */
652   IF (i_ae_txn_rec.txn_action_id in (3,12,21,15,22)) THEN /* Direct and Intransit Transfers */
653         l_stmt_num := 11;
654 
655         IF (i_ae_txn_rec.txn_action_id IN (12,21,15,22)) THEN -- INVCONV
656 
657           BEGIN
658             SELECT NVL(cost_group_id, -1)
659             INTO   l_txn_cost_group_id
660             FROM   cst_cost_group_assignments
661             WHERE  organization_id = i_ae_txn_rec.organization_id;
662           EXCEPTION
663             WHEN no_data_found THEN
664               l_txn_cost_group_id := -1;
665           END;
666 
667           IF (i_ae_txn_rec.txn_action_id in (12,15)) THEN -- INVCONV
668 
669             l_stmt_num := 12;
670             SELECT nvl(mmt.fob_point,mip.fob_point)
671             INTO l_fob_point
672             FROM mtl_material_transactions mmt,mtl_interorg_parameters mip
673             WHERE mmt.transaction_id = i_ae_txn_rec.transaction_id
674             AND   mip.from_organization_id = i_ae_txn_rec.xfer_organization_id
675             AND   mip.to_organization_id = i_ae_txn_rec.organization_id;
676 
677            END IF;
678 
679         END IF;
680 
681         /* Currency conversion is only required during an ownership change. */
682         IF ( (i_ae_txn_rec.txn_action_id = 3 AND i_ae_txn_rec.primary_quantity > 0) /* direct rcpt processed by rcv CG */
683               OR (i_ae_txn_rec.txn_action_id = 21 AND i_ae_txn_rec.cost_group_id <> l_txn_cost_group_id) /*intransit ship, FOB ship, processed by rcv CG */
684               OR (i_ae_txn_rec.txn_action_id = 12 AND i_ae_txn_rec.cost_group_id = l_txn_cost_group_id AND l_fob_point = 2) /*intransit rcpt, FOB rcpt, processed by rcv CG */
685               OR (i_ae_txn_rec.txn_action_id = 15 AND l_fob_point = 1) /* Logical intransit rcpt, FOB shpmt, INVCONV */
686            ) THEN
687               l_stmt_num := 13;
688 
689               SELECT currency_code
690               INTO   l_curr_rec.alt_currency -- currency of the sending org
691               FROM   cst_organization_definitions
692               WHERE  organization_id = decode(i_ae_txn_rec.txn_action_id,
693                                                 21, i_ae_txn_rec.organization_id,
694                                                 i_ae_txn_rec.xfer_organization_id);
695 
696               l_curr_rec.currency_conv_date := i_ae_txn_rec.currency_conv_date;
697               l_curr_rec.currency_conv_type := i_ae_txn_rec.currency_conv_type;
698 
699               l_stmt_num := 14;
700               /* Get_Snd_Rcv_Rate returns the GL Daily Rate for the transaction date.
701                  This should usually be the same as i_ae_txn_rec.currency_conv_rate,
702                  which stores MMT.currency_conversion_rate.  However, in the corner
703                  case that the daily rate is different (ex. if user changes the GL
704                  daily rate after the transaction), we use the GL Daily Rate
705                  (always from the sending org currency to the receiving org currency) */
706              IF (i_ae_txn_rec.txn_action_id = 21 AND i_ae_txn_rec.cost_group_id <> l_txn_cost_group_id) THEN
707                l_curr_from_org := i_ae_txn_rec.organization_id;
708                l_curr_to_org := i_ae_txn_rec.xfer_organization_id;
709              ELSE
710                l_curr_from_org := i_ae_txn_rec.xfer_organization_id;
711                l_curr_to_org := i_ae_txn_rec.organization_id;
712              END IF;
713              CSTPPINV.Get_Snd_Rcv_Rate( i_ae_txn_rec.transaction_id,
714                                          l_curr_from_org,
715                                          l_curr_to_org,
716                                          l_curr_rec.currency_conv_rate,
717                                          l_err_rec.l_err_num,
718                                          l_err_rec.l_err_code,
719                                          l_err_rec.l_err_msg);
720 
721               IF (l_err_rec.l_err_num <> 0) THEN
722                 raise process_error;
723               END IF;
724 
725         ELSE
726               l_stmt_num := 15;
727            /* no ownership change */
728               l_curr_rec.alt_currency := NULL;
729               l_curr_rec.currency_conv_date := NULL;
730               l_curr_rec.currency_conv_type := NULL;
731               l_curr_rec.currency_conv_rate := NULL;
732         END IF;
733   ELSE   /* non-interorg transactions */
734     l_stmt_num := 16;
735     l_curr_rec.alt_currency := i_ae_txn_rec.currency_code;
736     l_curr_rec.currency_conv_date := i_ae_txn_rec.currency_conv_date;
737     l_curr_rec.currency_conv_type := i_ae_txn_rec.currency_conv_type;
738     l_curr_rec.currency_conv_rate := i_ae_txn_rec.currency_conv_rate;
739   END IF;
740 
741   IF g_debug_flag = 'Y' THEN
742     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num ||
743                                     ': l_curr_rec.pri_currency = ' || l_curr_rec.pri_currency ||
744                                     '; l_curr_rec.alt_currency = ' || l_curr_rec.alt_currency);
745   END IF;
746 
747   if (l_curr_rec.alt_currency is not NULL and l_curr_rec.currency_conv_rate = -1) then
748     if (l_curr_rec.alt_currency <> l_curr_rec.pri_currency) then
749       if (l_curr_rec.currency_conv_type is NULL) then
750         FND_PROFILE.get('CURRENCY_CONVERSION_TYPE', l_curr_rec.currency_conv_type);
751       end if;
752       l_stmt_num := 16;
753       l_curr_rec.currency_conv_rate := gl_currency_api.get_rate(i_ae_txn_rec.set_of_books_id,
754                                                                 l_curr_rec.alt_currency,
755                                                                 i_ae_txn_rec.accounting_date,
756                                                                 l_curr_rec.currency_conv_type);
757     end if;
758   end if;
759 
760   IF g_debug_flag = 'Y' THEN
761     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num ||
762                         ': l_curr_rec.currency_conv_rate = ' || l_curr_rec.currency_conv_rate);
763   END IF;
764 
765   /* forward port for 14541396*/
766  /* added for bug 5867021 */
767 
768  	   IF (i_ae_txn_rec.txn_action_id =27 and i_ae_txn_rec.txn_src_type_id =1) OR
769  	       (i_ae_txn_rec.txn_action_id =1 and i_ae_txn_rec.txn_src_type_id =1) OR
770  	       (i_ae_txn_rec.txn_action_id =29 and i_ae_txn_rec.txn_src_type_id =1)
771  	   THEN
772  	         SELECT mmt.rcv_transaction_id into l_rcv_txn
773  	         FROM mtl_material_transactions mmt
774  	         WHERE mmt.transaction_id = i_ae_txn_rec.transaction_id;
775 
776  	         fnd_file.put_line(fnd_file.log,'i_ae_txn_rec.transaction_id << '|| i_ae_txn_rec.transaction_id);
777  	         fnd_file.put_line(fnd_file.log,'l_rcv_txn  << '|| l_rcv_txn );
778 
779 
780  	         SELECT
781  	         rt6.transaction_id
782  	         INTO
783  	         l_par_rcv_txn
784  	         FROM
785  	         rcv_transactions rt6
786  	         where rt6.transaction_type = 'RECEIVE'
787  	         START WITH
788  	         rt6.transaction_id = l_rcv_txn
789  	         CONNECT BY
790  	         rt6.transaction_id = prior rt6.parent_transaction_id;
791 
792  	         fnd_file.put_line(fnd_file.log,'l_par_rcv_txn << '|| l_par_rcv_txn);
793 
794  	         SELECT
795  	         COUNT(rcv_transaction_id)
796  	         INTO
797  	         l_acq_exists
798  	         FROM
799  	         cst_rcv_acq_costs crac
800  	         WHERE
801  	         crac.rcv_transaction_id = l_par_rcv_txn AND
802  	         crac.cost_type_id = i_ae_txn_rec.cost_type_id AND
803  	         crac.cost_group_id = i_ae_txn_rec.cost_group_id;
804 
805  	         IF (l_acq_exists > 0) THEN
806 
807  	         l_stmt_num := 32;
808 
809  	         SELECT
810  	         crac.net_quantity_received,
811  	         crac.costed_quantity,
812  	         crac.acquisition_cost
813  	         INTO
814  	         l_nqr,
815  	         l_costed_quantity,
816  	         l_acq_cost
817  	         from cst_rcv_acq_costs crac
818  	         where crac.rcv_transaction_id = l_par_rcv_txn AND
819  	         crac.cost_type_id = i_ae_txn_rec.cost_type_id AND
820  	         crac.cost_group_id = i_ae_txn_rec.cost_group_id;
821 
822  	         fnd_file.put_line(fnd_file.log,'stmt num32 << ');
823  	         fnd_file.put_line(fnd_file.log,'l_nqr << ' || l_nqr);
824  	         fnd_file.put_line(fnd_file.log,'l_costed_quantity << '|| l_costed_quantity);
825  	         fnd_file.put_line(fnd_file.log,'l_acq_cost << '||l_acq_cost);
826 
827  	         IF (l_nqr = 0) THEN
828  	                 IF g_debug_flag = 'Y' THEN
829  	                         fnd_file.put_line(fnd_file.log, 'The Net Quantity Received is Zero');
830  	                 END IF;
831  	         return;
832  	         END IF;
833 
834  	         l_stmt_num := 33;
835  	 /*
836  	         SELECT
837  	         SUM(cracd.amount/NVL(aida.exchange_rate,1))
838  	         INTO
839  	         l_acq_cost_ent_inv
840  	         FROM
841  	         cst_rcv_acq_cost_details cracd,
842  	         ap_invoice_distributions_all aida
843  	         WHERE aida.invoice_distribution_id = cracd.invoice_distribution_id
844  	         AND cracd.header_id = (SELECT header_id
845  	                                FROM cst_rcv_acq_costs crac
846  	                                WHERE rcv_transaction_id = l_par_rcv_txn
847  	                                AND crac.cost_type_id = i_ae_txn_rec.cost_type_id
848  	                                AND crac.cost_group_id = i_ae_txn_rec.cost_group_id)
849  	        AND cracd.source_type = 'INVOICE';*/
850 
851 			   select
852 			   sum(cracd.amount/NVL(aia.exchange_rate,1))
853 			   into
854 			   l_acq_cost_ent_inv
855 			   from
856 			   cst_rcv_acq_cost_details cracd,
857 			   ap_invoice_distributions_all aida,
858 			   ap_invoices_all aia
859 			   where
860 			   aida.invoice_distribution_id = cracd.invoice_distribution_id
861 			   and aia.invoice_id = aida.invoice_id
862 			   and aia.org_id = aida.org_id
863 			   and cracd.header_id = (select header_id
864 				 from cst_rcv_acq_costs crac
865 				 where rcv_transaction_id = l_par_rcv_txn
866 			   AND
867 			   crac.cost_type_id = i_ae_txn_rec.cost_type_id AND
868 			   crac.cost_group_id = i_ae_txn_rec.cost_group_id)
869 			   and cracd.source_type = 'INVOICE';
870 
871  	         fnd_file.put_line(fnd_file.log,'stmt num 33 << ');
872  	         fnd_file.put_line(fnd_file.log,'l_acq_cost_ent_inv<<' || l_acq_cost_ent_inv );
873 
874  	         l_stmt_num := 34;
875 
876  	         SELECT
877  	         SUM(cracd.amount/ decode(poll.match_option,
878  	                                 'P',CSTPPACQ.get_po_rate(rt.transaction_id),
879  	                                 'R',rt.currency_conversion_rate))
880  	         INTO
881  	         l_acq_cost_ent_po
882  	         FROM
883  	         cst_rcv_acq_cost_details cracd,
884  	         po_line_locations_all poll,
885  	         rcv_transactions rt
886  	         WHERE
887  	         poll.line_location_id = cracd.po_line_location_id
888  	         AND cracd.header_id = (SELECT header_id
889  	                                 FROM cst_rcv_acq_costs crac
890  	                                 WHERE rcv_transaction_id = rt.transaction_id
891  	                                 AND crac.cost_type_id = i_ae_txn_rec.cost_type_id
892  	                                 AND crac.cost_group_id = i_ae_txn_rec.cost_group_id)
893  	         AND rt.transaction_id = l_par_rcv_txn
894  	         AND rt.po_line_location_id = poll.line_location_id
895  	         AND cracd.source_type = 'PO';
896 
897  	         fnd_file.put_line(fnd_file.log,'stmt num 34 << '  );
898  	         fnd_file.put_line(fnd_file.log,'l_acq_cost_ent_po << '||  l_acq_cost_ent_po);
899 
900  	         IF (l_acq_cost_ent_po < 0) THEN
901  	                 IF g_debug_flag = 'Y' THEN
902  	                         fnd_file.put_line(fnd_file.log,'Error: No Acquisition Rate');
903  	                 END IF;
904  	         l_err_rec.l_err_num := 999;
905  	         END IF;
906 
907  	         l_acq_cost_ent := (nvl(l_acq_cost_ent_inv,0) + nvl(l_acq_cost_ent_po,0))/l_costed_quantity;
908 
909  	         fnd_file.put_line(fnd_file.log,'l_acq_cost_ent << '||  l_acq_cost_ent);
910 
911  	         IF (l_acq_cost_ent = 0) THEN
912  	             l_curr_conv_rate := 1;
913  	         ELSE
914  	             l_curr_conv_rate := l_acq_cost/l_acq_cost_ent;
915  	         END IF;
916 
917  	         fnd_file.put_line(fnd_file.log,'l_curr_conv_rate << '|| l_curr_conv_rate );
918 
919  	         l_curr_rec.currency_conv_rate :=  l_curr_conv_rate;
920 
921  	 ELSE
922  	         IF g_debug_flag = 'Y' THEN
923  	                 fnd_file.put_line(fnd_file.log,'No Acquisition Cost');
924  	         END IF;
925  	         RETURN;
926  	 END IF;
927 
928  	 end if;
929 
930  	   /* added for bug 5867021 */
931 
932   /* Determine if a logical transaction
933    It is assumed that a parent physical transaction is never passed to this
934    procedure */
935 
936   select nvl(logical_transaction, 2)
937   into l_logical_transaction
938   from mtl_material_transactions
939   where transaction_id = i_ae_txn_rec.transaction_id;
940 
941 -- Calling procedures, depending on transaction_source_type_id and transaction_action_id.
942 -- --------------------------------------------------------------------------------------
943   /*******************************************************************
944    **   break down the inventory transactions into 5 categories:    **
945    ** 1) WIP Cost transactions                                      **
946    ** 2) subinventory transfers                                                     **
947    ** 3) interorg transfers                                                             **
948    ** 4) Periodic cost update                                                       **
949    ** 5) rest of inventory transactions                                         **
950    ** 6) Logical Accounting Events                                  **
951    ** 7) Consigned Price Update                                     **
952    *******************************************************************/
953   if (i_ae_txn_rec.txn_src_type_id = 5 and i_ae_txn_rec.txn_action_id <> 2) then
954 
955 -- WIP transaction
956 -- These always occur in base currency, so set alt currency as null.
957 -- -----------------------------------------------------------------
958      l_curr_rec.alt_currency := NULL;
959      l_curr_rec.currency_conv_date := NULL;
960      l_curr_rec.currency_conv_type := NULL;
961      l_curr_rec.currency_conv_rate := NULL;
962 
963      l_stmt_num := 20;
964      CSTPAPBR.wip_cost_txn (i_ae_txn_rec,
965                            l_curr_rec,
966                            l_ae_line_tbl,
967                            l_err_rec);
968      -- check error;
969      if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
970         raise process_error;
971      end if;
972   /* Changes for VMI. Adding Planning Transfer transaction */
973   elsif (i_ae_txn_rec.txn_action_id in (2,5,28)) then
974 
975 -- Subinventory transfer.
976 -- ----------------------
977      l_stmt_num := 30;
978      CSTPAPBR.sub_cost_txn (i_ae_txn_rec,
979                            l_curr_rec,
980                            l_ae_line_tbl,
981                            l_err_rec);
982      -- check error;
983      if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
984         raise process_error;
985      end if;
986   elsif (i_ae_txn_rec.txn_action_id in (3,12,21,15,22)) then -- INVCONV sikhanna
987 
988 -- Interorg transfers.
989 -- -------------------
990      l_stmt_num := 40;
991      CSTPAPBR.interorg_cost_txn (i_ae_txn_rec,
992                                 l_curr_rec,
993                                 l_ae_line_tbl,
994                                 l_err_rec);
995      -- check error;
996      if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
997         raise process_error;
998      end if;
999   elsif (i_ae_txn_rec.txn_action_id = 24 and i_ae_txn_rec.txn_src_type_id = 14) then
1000 
1001 -- Cost Update.
1002 -- ------------
1003      l_stmt_num := 50;
1004      CSTPAPBR.pcu_cost_txn (i_ae_txn_rec,
1005                             l_curr_rec,
1006                             l_ae_line_tbl,
1007                             l_err_rec);
1008      -- check error;
1009      if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1010         raise process_error;
1011      end if;
1012 
1013   --Call the logical procedure if a logical transaction
1014   elsif (l_logical_transaction = 1 and i_ae_txn_rec.txn_action_id <> 17) then -- Bug 5485387
1015 
1016     l_stmt_num := 55;
1017     CSTPAPBR.cost_logical_txn (i_ae_txn_rec,
1018                            l_curr_rec,
1019                            l_ae_line_tbl,
1020                            l_err_rec);
1021      if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1022         raise process_error;
1023      end if;
1024 
1025   --For consigned price update transaction
1026   elsif (i_ae_txn_rec.txn_action_id = 25 and i_ae_txn_rec.txn_src_type_id = 1) then
1027     l_stmt_num := 57;
1028     CSTPAPBR.cost_consigned_update_txn(i_ae_txn_rec,
1029                            l_curr_rec,
1030                            l_ae_line_tbl,
1031                            l_err_rec);
1032      if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1033         raise process_error;
1034      end if;
1035 
1036   -- For Internal Order Issue and Logical Expense Requisition Bug 5485387
1037   elsif ((i_ae_txn_rec.txn_action_id = 1 and i_ae_txn_rec.txn_src_type_id = 8)
1038           OR i_ae_txn_rec.txn_action_id = 17) then
1039 
1040      l_stmt_num := 58;
1041 
1042      CSTPAPBR.cost_internal_order_exp_txn(i_ae_txn_rec,
1043                                           l_curr_rec,
1044                                           l_ae_line_tbl,
1045                                           l_err_rec);
1046 
1047       if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1048          raise process_error;
1049       end if;
1050 
1051   else
1052 
1053 -- Rest of inventory transactions.
1054 -- -------------------------------
1055      l_stmt_num := 60;
1056      CSTPAPBR.inv_cost_txn (i_ae_txn_rec,
1057                            l_curr_rec,
1058                            l_ae_line_tbl,
1059                            l_err_rec);
1060      -- check error;
1061      if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1062         raise process_error;
1063      end if;
1064   end if;
1065 
1066 -- Take care of rounding errors.
1067 -- -----------------------------
1068   l_stmt_num := 70;
1069   balance_account (l_ae_line_tbl,
1070                    l_err_rec);
1071 
1072   -- check error
1073   if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1074       raise process_error;
1075   end if;
1076 
1077 /********************************************************************************
1078 FP 12.1.1 bug 7346244 fix:
1079  Changes for Encumbrance accounting for Logical Expense receipt
1080     - Excluded the Internal order issue transaction from the following
1081       and added another piece of code for handling that.
1082  if (i_ae_txn_rec.txn_src_type_id in (1,7,8))  then
1083  ********************************************************************************/
1084 
1085    select
1086    encumbrance_reversal_flag
1087    into
1088    l_enc_rev
1089    from
1090    mtl_parameters
1091    where organization_id = i_ae_txn_rec.organization_id;
1092 
1093   IF g_debug_flag = 'Y' THEN
1094    fnd_file.put_line(fnd_file.log,'Organization_id : '||(i_ae_txn_rec.organization_id));
1095    fnd_file.put_line(fnd_file.log,'l_enc_rev : '||(l_enc_rev));
1096    fnd_file.put_line(fnd_file.log,'transaction_id : '||(i_ae_txn_rec.transaction_id));
1097    fnd_file.put_line(fnd_file.log,'txn_action_id : '||(i_ae_txn_rec.txn_action_id));
1098   END IF;
1099 
1100  if ((i_ae_txn_rec.txn_src_type_id = 1) OR (i_ae_txn_rec.txn_src_type_id in (7,8) AND i_ae_txn_rec.txn_action_id NOT IN (1,17) ))  then
1101 
1102    if (l_enc_rev = 1 and i_ae_txn_rec.encum_amount <> 0) THEN
1103       encumbrance_account
1104                          (i_ae_txn_rec,
1105                           l_curr_rec,
1106                           l_ae_line_tbl,
1107                           l_err_rec);
1108      -- check error;
1109      if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1110         raise process_error;
1111      end if;
1112 
1113    end if;
1114  end if;
1115 
1116 -- FP 12.1.1 bug 7346244 fix
1117 if (l_enc_rev = 1 AND i_ae_txn_rec.txn_action_id = 17 AND i_ae_txn_rec.exp_item = 0  ) then
1118 
1119   IF g_debug_flag = 'Y' THEN
1120    fnd_file.put_line(fnd_file.log,'CompEncumbrance_IntOrdersExp << : ');
1121   END IF;
1122 
1123     /* Check if the transfer is between Disc to Disc and the subinventory from which internal order issue is done is Expense then don't create encumbrance reversal as no actuals would be created */
1124 
1125    l_stmt_num := 80;
1126    /* Check if it is a transfer between Process and Discrete Orgs */
1127    SELECT MOD(SUM(DECODE(process_enabled_flag,'Y',1,2)), 2)
1128      INTO l_pd_txfr_ind
1129      FROM MTL_PARAMETERS MP
1130     WHERE MP.ORGANIZATION_ID = i_ae_txn_rec.xfer_organization_id
1131        OR MP.ORGANIZATION_ID    = i_ae_txn_rec.organization_id;
1132    /* Get the expense sub or asset sub from the issue sub for logical exp req rcpt */
1133    SELECT decode(asset_inventory,1,0,1)
1134      INTO  l_exp_sub
1135      FROM  mtl_secondary_inventories
1136     WHERE secondary_inventory_name = i_ae_txn_rec.xfer_subinventory
1137       AND organization_id = i_ae_txn_rec.xfer_organization_id;
1138 
1139      IF ( l_exp_sub = 0
1140           OR(l_exp_sub =1 AND l_pd_txfr_ind = 1 )
1141         ) THEN
1142 
1143         CompEncumbrance_IntOrdersExp (
1144           p_api_version         => 1.0,
1145           p_transaction_id      => i_ae_txn_rec.transaction_id,
1146           x_encumbrance_amount  => l_enc_amount,
1147           x_encumbrance_account => l_enc_account,
1148           o_ae_err_rec          => l_err_rec);
1149 
1150          if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1151             raise process_error;
1152          end if;
1153       ELSE
1154          IF g_debug_flag = 'Y' THEN
1155  	   fnd_file.put_line(fnd_file.log,'No Encumbrance Reversal as Int order issue is done from Exp sub in Discrete to Discrete Txfer');
1156  	  END IF;
1157  	  l_enc_amount := 0;
1158       END IF;
1159 
1160       l_ae_txn_rec1:=i_ae_txn_rec;
1161       l_ae_txn_rec1.encum_amount      :=l_enc_amount;
1162       l_ae_txn_rec1.encum_account     :=l_enc_account;
1163 
1164   IF g_debug_flag = 'Y' THEN
1165    fnd_file.put_line(fnd_file.log,'encum_amount << : '||l_ae_txn_rec1.encum_amount);
1166    fnd_file.put_line(fnd_file.log,'encum_account << : '||l_ae_txn_rec1.encum_account);
1167   END IF;
1168 
1169    if (l_enc_rev = 1 and l_ae_txn_rec1.encum_amount <> 0) THEN
1170 
1171       encumbrance_account
1172                          (l_ae_txn_rec1,
1173                           l_curr_rec,
1174                           l_ae_line_tbl,
1175                           l_err_rec);
1176    end if;
1177 
1178 End if; /* End CompEncumbrance_IntOrdersExp IF */
1179 
1180 
1181 -- Return the lines pl/sql table.
1182 -- ------------------------------
1183   l_stmt_num := 80;
1184   o_ae_line_rec_tbl := l_ae_line_tbl;
1185 
1186   IF g_debug_flag = 'Y' THEN
1187     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': return >>');
1188   END IF;
1189 
1190 EXCEPTION
1191 
1192   when process_error then
1193   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
1194   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
1195   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
1196 
1197   when others then
1198   o_ae_err_rec.l_err_num := SQLCODE;
1199   o_ae_err_rec.l_err_code := '';
1200   o_ae_err_rec.l_err_msg := 'CSTPAPBR.create_inv_ae_lines : ' || to_char(l_stmt_num) || ':'||
1201   substr(SQLERRM,1,180);
1202 
1203 END create_inv_ae_lines;
1204 
1205 -- ===================================================================
1206 -- WIP transactions.
1207 -- ===================================================================
1208 procedure wip_cost_txn(
1209   i_ae_txn_rec                IN        CSTPALTY.cst_ae_txn_rec_type,
1210   i_ae_curr_rec               IN        CSTPALTY.cst_ae_curr_rec_type,
1211   l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
1212   o_ae_err_rec                OUT NOCOPY        CSTPALTY.cst_ae_err_rec_type
1213   --i_ae_txn_rec                IN              CSTPALBR.cst_ae_txn_rec_type,
1214   --i_ae_curr_rec               IN              CSTPALBR.cst_ae_curr_rec_type,
1215   --l_ae_line_tbl               IN OUT    CSTPALBR.cst_ae_line_tbl_type,
1216   --o_ae_err_rec                OUT             CSTPALBR.cst_ae_err_rec_type
1217 ) IS
1218   l_exp_sub                             NUMBER;
1219   l_exp_acct                            NUMBER;
1220   l_exp_job                             NUMBER;
1221   l_ovhd_absp                           NUMBER;
1222   l_mat_ovhd_exists                     NUMBER;
1223   l_rep_sched_id                        NUMBER;
1224   l_cost                                NUMBER;
1225   l_err_rec                             CSTPALTY.cst_ae_err_rec_type;
1226   l_acct_rec                            CSTPALTY.cst_ae_acct_rec_type;
1227   --l_err_rec                           CSTPALBR.cst_ae_err_rec_type;
1228   --l_acct_rec                          CSTPALBR.cst_ae_acct_rec_type;
1229   l_acct_class                          VARCHAR2(10);
1230   l_acct_line_type                      NUMBER;
1231   l_elemental                           NUMBER;
1232   l_dr_flag                             BOOLEAN;
1233   l_exp_flag                            BOOLEAN;
1234   l_stmt_num                            NUMBER;
1235   process_error                         EXCEPTION;
1236 
1237 BEGIN
1238 
1239   IF g_debug_flag ='Y' THEN
1240     fnd_file.put_line(fnd_file.log, 'Wip_cost_txn <<');
1241   END IF;
1242 -- Initialize variables.
1243 -- ---------------------
1244   l_err_rec.l_err_num := 0;
1245   l_err_rec.l_err_code := '';
1246   l_err_rec.l_err_msg := '';
1247   l_ovhd_absp := 0;
1248   l_mat_ovhd_exists := '';
1249 
1250 -- There are no cost distributions for expense items.
1251 -- --------------------------------------------------
1252   if (i_ae_txn_rec.exp_item = 1) then
1253     IF g_debug_flag = 'Y' THEN
1254       fnd_file.put_line(fnd_file.log,'No accounting for Expense item');
1255     END IF;
1256     return;
1257   end if;
1258 
1259 
1260 -- Figure out accts, expense subinventory and expense job flags.
1261 -- -------------------------------------------------------------
1262 
1263       -- repetitive schedules
1264 
1265       IF (i_ae_txn_rec.wip_entity_type = 2) THEN
1266         IF g_debug_flag = 'Y' THEN
1267           fnd_file.put_line(fnd_file.log, 'Repetitive Schedule: ');
1268         END IF;
1269 
1270         l_stmt_num := 10;
1271 
1272         SELECT MAX(wrs.repetitive_schedule_id)
1273         INTO   l_rep_sched_id
1274         FROM   wip_repetitive_schedules wrs
1275         WHERE  wrs.line_id = i_ae_txn_rec.line_id;
1276 
1277         l_stmt_num := 15;
1278 
1279         SELECT material_account,
1280                material_overhead_account,
1281                resource_account,
1282                overhead_account,
1283                outside_processing_account
1284         INTO   l_acct_rec.mat_account,
1285                l_acct_rec.mat_ovhd_account,
1286                l_acct_rec.res_account,
1287            l_acct_rec.ovhd_account,
1288                l_acct_rec.osp_account
1289         FROM   wip_repetitive_schedules wrs
1290         WHERE  wrs.repetitive_schedule_id = l_rep_sched_id;
1291 
1292         SELECT
1293         class_code
1294         INTO
1295         l_acct_class
1296         FROM
1297         wip_repetitive_items wri
1298         WHERE
1299         wri.wip_entity_id = i_ae_txn_rec.txn_src_id AND
1300         wri.line_id = i_ae_txn_rec.line_id AND
1301         wri.organization_id = i_ae_txn_rec.organization_id;
1302 
1303       -- flow schedules
1304       ELSIF (i_ae_txn_rec.wip_entity_type = 4) THEN
1305         IF g_debug_flag = 'Y' THEN
1306           fnd_file.put_line(fnd_file.log,'Flow Schedule: ');
1307         END IF;
1308 
1309         SELECT
1310         material_account,
1311         material_overhead_account,
1312         resource_account,
1313         outside_processing_account,
1314         overhead_account,
1315         class_code
1316         INTO
1317         l_acct_rec.mat_account,
1318         l_acct_rec.mat_ovhd_account,
1319         l_acct_rec.res_account,
1320         l_acct_rec.osp_account,
1321         l_acct_rec.ovhd_account,
1322         l_acct_class
1323         FROM wip_flow_schedules
1324         WHERE organization_id = i_ae_txn_rec.organization_id
1325         AND wip_entity_id = i_ae_txn_rec.txn_src_id;
1326       ELSE
1327         -- discrete jobs
1328         IF g_debug_flag = 'Y' THEN
1329           fnd_file.put_line(fnd_file.log,'Discrete Job: ');
1330         END IF;
1331         l_stmt_num := 17;
1332         select
1333         material_account,
1334         material_overhead_account,
1335         resource_account,
1336         outside_processing_account,
1337         overhead_account,
1338         class_code
1339         into
1340         l_acct_rec.mat_account,
1341         l_acct_rec.mat_ovhd_account,
1342         l_acct_rec.res_account,
1343         l_acct_rec.osp_account,
1344         l_acct_rec.ovhd_account,
1345         l_acct_class
1346         from wip_discrete_jobs
1347         where organization_id = i_ae_txn_rec.organization_id
1348         and wip_entity_id = i_ae_txn_rec.txn_src_id;
1349       END IF;
1350 
1351   l_stmt_num := 20;
1352 
1353   select decode(class_type, 4,1,0)
1354   into l_exp_job
1355   from wip_accounting_classes
1356   where class_code = l_acct_class
1357   and organization_id = i_ae_txn_rec.organization_id;
1358 
1359   l_stmt_num := 30;
1360 
1361 -- Scrap transactions do not have inventory impact!!
1362 -- -------------------------------------------------
1363   if (i_ae_txn_rec.txn_action_id <> 30) then
1364     IF g_debug_flag = 'Y' THEN
1365       fnd_file.put_line(fnd_file.log,'Scrap txn: ');
1366     END IF;
1367     select decode(asset_inventory,1,0,1)
1368     into l_exp_sub
1369     from mtl_secondary_inventories
1370     where secondary_inventory_name = i_ae_txn_rec.subinventory_code
1371     and organization_id = i_ae_txn_rec.organization_id;
1372 
1373 -- Transactions between expense subinventories and expense jobs are
1374 -- not distributed.
1375 -- ----------------------------------------------------------------
1376     if (l_exp_sub = 1 and l_exp_job = 1) then
1377       IF g_debug_flag = 'Y' THEN
1378         fnd_file.put_line(fnd_file.log,'No accounting for Expense Sub or Expense Job');
1379       END IF;
1380       return;
1381     end if;
1382     if (l_exp_sub = 1) then
1383        l_acct_rec.account := -1;
1384     end if;
1385   end if;
1386 
1387 -- Debit/Credit WIP accounts.
1388 -- --------------------------
1389 
1390 -- Material overhead absorption happens for the assembly completion
1391 -- and asembly return transactions.
1392 -- ----------------------------------------------------------------
1393 
1394   if (i_ae_txn_rec.txn_action_id in (31,32)) then
1395     IF g_debug_flag = 'Y' THEN
1396       fnd_file.put_line(fnd_file.log,'Assembly completion/return');
1397     END IF;
1398     l_stmt_num := 40;
1399     select count(transaction_id) /* Bug No. 4586534 */
1400     into l_mat_ovhd_exists
1401     from mtl_pac_actual_cost_details
1402     where transaction_id = i_ae_txn_rec.transaction_id
1403     and pac_period_id = i_ae_txn_rec.accounting_period_id
1404     and cost_group_id = i_ae_txn_rec.cost_group_id
1405     and cost_element_id = 2
1406     and level_type = 1;
1407     if (l_mat_ovhd_exists > 0) then
1408        l_ovhd_absp := 1;
1409     end if;
1410   end if;
1411 
1412   if (i_ae_txn_rec.txn_action_id in (31,27,33,1,34,32)) then
1413      if (i_ae_txn_rec.txn_action_id in (31,27,33)) then
1414        l_dr_flag := "TRUE"; -- debit
1415      else
1416        l_dr_flag := "FALSE"; -- credit
1417      end if;
1418 
1419      /* Bug 3293554: should use the expense account if it is non-asset subinventory */
1420      if (l_exp_sub = 1) then
1421         l_exp_flag := "TRUE";
1422      else
1423         l_exp_flag := "FALSE";
1424         l_acct_rec.account := '';
1425      end if;
1426 
1427      inventory_accounts(i_ae_txn_rec,
1428                        i_ae_curr_rec,
1429                        l_exp_flag,
1430                        l_acct_rec.account,
1431                        l_dr_flag,
1432                        l_ae_line_tbl,
1433                        l_err_rec);
1434      if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1435        raise process_error;
1436      end if;
1437 
1438 -- Toggle the Debit Flag.
1439 -- ----------------------
1440      l_dr_flag := not l_dr_flag;
1441 
1442      l_acct_rec.account := '';
1443 
1444      l_acct_line_type := 7;
1445      l_elemental := 1;
1446 
1447      --------------------------------------------------------------------
1448      -- Call WIP_accounts procedure to Cr/Dr WIP valuation accounts
1449      -- In case of negative Job balance also Cr/dr WIP variance accounts
1450      --------------------------------------------------------------------
1451       WIP_accounts(i_ae_txn_rec,
1452                    i_ae_curr_rec,
1453                    l_acct_line_type,
1454                    l_ovhd_absp,
1455                    l_dr_flag,
1456                    l_acct_rec,
1457                    l_ae_line_tbl,
1458                    l_err_rec);
1459 
1460      if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1461         raise process_error;
1462      end if;
1463 
1464      if (l_ovhd_absp = 1) then
1465        ovhd_accounts(i_ae_txn_rec,
1466                      i_ae_curr_rec,
1467                      l_dr_flag,
1468                      l_ae_line_tbl,
1469                      l_err_rec);
1470        if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1471           raise process_error;
1472        end if;
1473 
1474      end if;
1475   elsif (i_ae_txn_rec.txn_action_id = 30) then
1476 
1477      -----------------------------------------------------------------------------
1478      -- For Assembly Scrap Credit the WIP Valuation => Set the l_dr_flag to FALSE
1479      -- For Assembly Scrap Return Debit WIP Valuation => Set the l_dr_flag to TRUE
1480      -----------------------------------------------------------------------------
1481      IF (i_ae_txn_rec.primary_quantity > 0 ) THEN
1482         l_dr_flag := "FALSE";
1483      ELSE
1484         l_dr_flag := "TRUE";
1485      END IF;
1486 
1487      l_acct_rec.account := '';
1488      l_acct_line_type := 7;
1489      l_elemental := 1;
1490      --------------------------------------------------------------------
1491      -- Call WIP_accounts procedure to Cr/Dr WIP valuation accounts
1492      -- In case of negative Job balance also Cr/dr WIP variance accounts
1493      --------------------------------------------------------------------
1494      WIP_accounts(i_ae_txn_rec,
1495                   i_ae_curr_rec,
1496                   l_acct_line_type,
1497                   l_ovhd_absp,
1498                   l_dr_flag,
1499                   l_acct_rec,
1500                   l_ae_line_tbl,
1501                   l_err_rec);
1502 
1503      if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1504         raise process_error;
1505      end if;
1506 
1507 -- Toggle the Debit Flag.
1508 -- ----------------------
1509      l_dr_flag := not l_dr_flag;
1510 
1511      -- For Flow Schedules, If Scrap Account is not mentioned then use Variance Accounts
1512      IF (i_ae_txn_rec.wip_entity_type = 4) AND (i_ae_txn_rec.dist_acct_id = -1) THEN
1513         select material_variance_account,
1514                material_variance_account,
1515                resource_variance_account,
1516                outside_proc_variance_account,
1517                overhead_variance_account
1518           into l_acct_rec.mat_account,
1519                l_acct_rec.mat_ovhd_account,
1520                l_acct_rec.res_account,
1521                l_acct_rec.osp_account,
1522                l_acct_rec.ovhd_account
1523           from wip_flow_schedules
1524          where organization_id = i_ae_txn_rec.organization_id
1525            and wip_entity_id = i_ae_txn_rec.txn_src_id;
1526      ELSE
1527         l_acct_rec.mat_account := i_ae_txn_rec.dist_acct_id;
1528         l_acct_rec.mat_ovhd_account := i_ae_txn_rec.dist_acct_id;
1529         l_acct_rec.res_account := i_ae_txn_rec.dist_acct_id;
1530         l_acct_rec.osp_account := i_ae_txn_rec.dist_acct_id;
1531         l_acct_rec.ovhd_account := i_ae_txn_rec.dist_acct_id;
1532         l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
1533      END IF;
1534 
1535      l_acct_line_type := 2;
1536      l_elemental := 1;
1537 
1538      offset_accounts(i_ae_txn_rec,
1539                      i_ae_curr_rec,
1540                      l_acct_line_type,
1541                      l_elemental,
1542                      l_ovhd_absp,
1543                      l_dr_flag,
1544                      l_acct_rec,
1545                      l_ae_line_tbl,
1546                      l_err_rec);
1547      if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1548         raise process_error;
1549      end if;
1550 
1551   end if;
1552   IF g_debug_flag = 'Y' THEN
1553     fnd_file.put_line(fnd_file.log,'Wip_cost_txn >>');
1554   END IF;
1555 
1556  EXCEPTION
1557 
1558   when process_error then
1559   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
1560   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
1561   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
1562 
1563   when others then
1564   o_ae_err_rec.l_err_num := SQLCODE;
1565   o_ae_err_rec.l_err_code := '';
1566   o_ae_err_rec.l_err_msg := 'CSTPAPBR.wip_cost_txn' || to_char(l_stmt_num) ||
1567   substr(SQLERRM,1,180);
1568 
1569 END wip_cost_txn;
1570 
1571 -- ===================================================================
1572 -- Subinventory transfer transactions.
1573 -- ===================================================================
1574 procedure sub_cost_txn(
1575   i_ae_txn_rec                IN        CSTPALTY.cst_ae_txn_rec_type,
1576   i_ae_curr_rec               IN        CSTPALTY.cst_ae_curr_rec_type,
1577   l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
1578   o_ae_err_rec                OUT NOCOPY        CSTPALTY.cst_ae_err_rec_type
1579 ) IS
1580   l_subinv                                    VARCHAR2(10);
1581   l_exp_sub1                             NUMBER;
1582   l_exp_acct1                           NUMBER;
1583   l_exp_sub2                            NUMBER;
1584   l_exp_acct2                           NUMBER;
1585   l_exp_sub                                   NUMBER;
1586   l_exp_acct                            NUMBER;
1587   l_stmt_num                            NUMBER;
1588   process_error                         EXCEPTION;
1589   l_dr_flag                             BOOLEAN;
1590   l_exp_flag                            BOOLEAN;
1591   l_err_rec                             CSTPALTY.cst_ae_err_rec_type;
1592   l_acct_rec                            CSTPALTY.cst_ae_acct_rec_type;
1593 BEGIN
1594 
1595   IF g_debug_flag = 'Y' THEN
1596     fnd_file.put_line(fnd_file.log,'Sub_cost_txn <<');
1597   END IF;
1598 
1599 -- Initialize variables.
1600 -- ---------------------
1601   l_err_rec.l_err_num := 0;
1602   l_err_rec.l_err_code := '';
1603   l_err_rec.l_err_msg := '';
1604 
1605 -- There are no cost distributions for expense items.
1606 -- --------------------------------------------------
1607   if (i_ae_txn_rec.exp_item = 1) then
1608     IF g_debug_flag = 'Y' THEN
1609       fnd_file.put_line(fnd_file.log,'No accounting for Expense Item');
1610     END IF;
1611     return;
1612   end if;
1613 
1614 -- Figure out expense subinventory for both from and to.
1615 -- -----------------------------------------------------
1616   l_stmt_num := 10;
1617 
1618   select decode(asset_inventory,1,0,1)
1619   into l_exp_sub1
1620   from mtl_secondary_inventories
1621   where secondary_inventory_name = i_ae_txn_rec.subinventory_code
1622   and organization_id = i_ae_txn_rec.organization_id;
1623 
1624   l_stmt_num := 20;
1625   select decode(asset_inventory,1,0,1)
1626   into l_exp_sub2
1627   from mtl_secondary_inventories
1628   where secondary_inventory_name = i_ae_txn_rec.xfer_subinventory
1629   and organization_id = i_ae_txn_rec.organization_id;
1630 
1631 -- If expense to expense transfer or asset to asset then no accounting entries.
1632 -- ----------------------------------------------------------------------------
1633   if (l_exp_sub1 = 1 and l_exp_sub2 = 1) or
1634      (l_exp_sub1 = 0 and l_exp_sub2 = 0)
1635   then
1636     IF g_debug_flag = 'Y' THEN
1637       fnd_file.put_line(fnd_file.log,'No accounting for Expense to Expense or Asset to Asset');
1638     END IF;
1639     return;
1640   end if;
1641 
1642 -- Do distribution for both subs.
1643 -- ------------------------------
1644   if (l_exp_sub1=1 or i_ae_txn_rec.exp_item = 1) then
1645      l_exp_flag := "TRUE";
1646      l_acct_rec.account := -1;
1647   else
1648      /* Bug 2517809: Reset l_exp_flag */
1649      l_exp_flag := "FALSE";
1650   end if;
1651   l_dr_flag := "FALSE";
1652   inventory_accounts(i_ae_txn_rec,
1653                      i_ae_curr_rec,
1654                      l_exp_flag,
1655                      l_acct_rec.account,
1656                      l_dr_flag,
1657                      l_ae_line_tbl,
1658                      l_err_rec);
1659   if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1660     raise process_error;
1661   end if;
1662 
1663   if (l_exp_sub2=1 or i_ae_txn_rec.exp_item = 1) then
1664      l_exp_flag := "TRUE";
1665      l_acct_rec.account := -1;
1666   else
1667      /* Bug 2517809: Reset l_exp_flag */
1668      l_exp_flag := "FALSE";
1669   end if;
1670   l_dr_flag := "TRUE";
1671   inventory_accounts(i_ae_txn_rec,
1672                      i_ae_curr_rec,
1673                      l_exp_flag,
1674                      l_acct_rec.account,
1675                      l_dr_flag,
1676                      l_ae_line_tbl,
1677                      l_err_rec);
1678   if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
1679     raise process_error;
1680   end if;
1681   IF g_debug_flag = 'Y' THEN
1682     fnd_file.put_line(fnd_file.log,'Sub_cost_txn >>');
1683   END IF;
1684 
1685  EXCEPTION
1686 
1687   when process_error then
1688   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
1689   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
1690   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
1691 
1692   when others then
1693   o_ae_err_rec.l_err_num := SQLCODE;
1694   o_ae_err_rec.l_err_code := '';
1695   o_ae_err_rec.l_err_msg := 'CSTPAPBR.sub_cost_txn' || to_char(l_stmt_num) ||
1696   substr(SQLERRM,1,180);
1697 
1698 END sub_cost_txn;
1699 
1700 
1701 /* ===================================================================
1702    Procedure for Inter Org Transfer: interorg_cost_txn()
1703    ===================================================================
1704 This procedure creates distributions for Interorg transactions.
1705 
1706 Below is the algorithm followed for interorg txns across cost groups:
1707 This also includes the changes made for OPM Inventory Convergence R12 Project.
1708 
1709 |-IF (shipment txn) THEN
1710 |   |- IF (Internal Order AND FOB-S AND Include Process-Discrete Internal Orders) THEN
1711 |   |   |- IF (sending CG) THEN
1712 |   |   |   |- Credit Inventory-OnHand at MPACD Cost
1713 |   |   |   |- Debit COGS at MPACD Cost (PWAC Cost during shipment)
1714 |   |   |- ELSE  (Receiving CG) -- For Process-Discrete Transfers, Profile=2 always
1715 |   |   |   |- IF (CST:Transfer Price Option = Yes, Price As Incoming Cost;2) THEN
1716 |   |   |   |   |- Credit Intercompany Accrual at MPACD Cost (Transfer Price)
1717 |   |   |   |   |- Debit Inventory-Intransit at MPACD Cost (Transfer Price) THEN
1718 |   |   |   |  ELSE (CST:Transfer Price Option = Yes, Price Not As Incoming Cost;1)
1719 |   |   |   |   |- Credit Intercompany Accrual at MMT Transfer Price
1720 |   |   |   |   |- Debit Inventory-Intransit at MPACD Cost (Estimated sending CG cost)
1721 |   |   |   |  END IF;
1722 |   |   |- END IF;
1723 |   |- ELSIF (Internal Order AND FOB-R) THEN
1724 |   |   |    This can only be the sending CG
1725 |   |   |   |- Credit Inventory-OnHand at MPACD Cost(PWAC Cost during shipment)
1726 |   |   |   |- Debit Inventory-Intransit at MPACD Cost
1727 |   |- ELSE  (Ordinary Interorg Shipments)
1728 |   |   |- IF (FOB-S AND sending CG) THEN
1729 |   |   |   |- IF (Discrete-Discrete Org Transfer) THEN
1730 |   |   |   |   |- Credit Inventory-OnHand at MPACD Cost (PWAC Cost during shipment)
1731 |   |   |   |   |- Debit Receivables at (CPIC/Perpetual/Prior Period PWAC Cost)
1732 |   |   |   |   |-  + Transfer Cost(If PACP is not used)
1733 |   |   |   |- ELSE (this is Process Discrete Transfer)
1734 |   |   |   |   |- Debit Receivables at Transfer Price
1735 |   |   |   |   |- Credit Inventory at PWAC
1736 |   |   |   |- END IF;
1737 |   |   |- ELSIF (FOB-S AND receiving CG) THEN
1738 |   |   |   |- Credit Payables at MPACD Cost
1739 |   |   |   |- Debit Inventory-Intransit at MPACD Cost
1740 |   |   |   |- Credit Freight Expense Account at transportation Cost
1741 |   |   |- ELSIF (FOB-R) THEN (Can be only sending CG) THEN
1742 |   |   |   |- Credit Inventory-OnHand at MPACD Cost(PWAC Cost during shipment)
1743 |   |   |   |- Debit Inventory-Intransit at MPACD Cost
1744 |   |   |- ELSIF (Direct Interorg) THEN
1745 |   |   |   |- IF (Discrete-Discrete Transfers) THEN
1746 |   |   |   |   |- Debit Receivables at (CPIC/Perpetual/Prior Period PWAC Cost +
1747 |   |   |   |   |  transportation Cost + Transfer Cost)
1748 |   |   |   |   |- Credit InterOrg Transfer Credit at Transfer Cost
1749 |   |   |   |   |- Credit Freight Expense Account at transportation Cost
1750 |   |   |   |   |- Credit Inventory at MPACD Cost (PWAC Cost during Shipment)
1751 |   |   |   |   |- Debit/Credit Transfer Variance at ABS(Receivable's Cost - MPACD Cost)
1752 |   |   |   |- ELSE (Process-Discrete Transfers)
1753 |   |   |   |   |- Debit Receivables at Transfer Price
1754 |   |   |   |   |- Credit Freight Expense Account at transportation Cost
1755 |   |   |   |   |- Credit Inventory at MPACD Cost (PWAC Cost during Shipment)
1756 |   |   |   |- END IF;
1757 |   |   |- END IF;
1758 |   |- END IF;
1759 |- ELSE (Receipt Transactions AND Include Process-Discrete Internal Orders)
1760 |   |- IF (Internal Order AND FOB-R) THEN
1761 |   |   |- IF (sending CG) THEN
1762 |   |   |   |- Credit Intransit at MPACD Cost (PWAC Cost during Receipt)
1763 |   |   |   |- Debit COGS at MPACD Cost
1764 |   |   |- ELSE (Receiving CG) -- For Process-Discrete Transfers, Profile=2 always
1765 |   |   |   |- IF (CST:Transfer Price Option = Yes, Price As Incoming Cost;2) THEN
1766 |   |   |   |   |- Credit Intercompany Accrual at MPACD Cost (Transfer Price)
1767 |   |   |   |   |- Debit On-hand Inventory at PWAC Cost
1768 |   |   |   |  ELSE (CST:Transfer Price Option = Yes, Price Not As Incoming Cost;1)
1769 |   |   |   |   |- Credit Intercompany Accrual at MMT Transfer Price
1770 |   |   |   |   |- Debit On-hand Inventory at PWAC Cost
1771 |   |   |   |  END IF;
1772 |   |   |- END IF;
1773 |   |- ELSIF (Internal Order AND FOB-S) THEN
1774 |   |   |   This can only be the receiving CG
1775 |   |   |   |- Credit Inventory-Intransit at MPACD Cost
1776 |   |   |   |- Debit Inventory-OnHand at MPACD Cost
1777 |   |- ELSE  (Ordinary Interorg Receipts)
1778 |   |   |- IF (FOB-R AND receiving CG) THEN
1779 |   |   |   |- IF (Discrete-Discrete Transfer) THEN
1780 |   |   |   |   |- Credit Payables at MPACD Cost
1781 |   |   |   |   |- Debit Inventory at MPACD Cost
1782 |   |   |   |- ELSE (Process-Discrete Transfers)
1783 |   |   |   |   |- Credit Payables at transfer Price Cost
1784 |   |   |   |   |- Debit Inventory at transfer Price Cost
1785 |   |   |   |  END IF;
1786 |   |   |- ELSIF (FOB-R AND sending CG) THEN
1787 |   |   |   |- Debit Receivables at (CPIC/Perpetual/Prior Period PWAC Cost +
1788 |   |   |   |  transportation Cost + Transfer Cost)
1789 |   |   |   |- Credit Inventory-Intransit at MPACD Cost
1790 |   |   |   |- Credit InterOrg Transfer Credit at Transfer Cost
1791 |   |   |   |- Credit Freight Expense Account at transportation
1792 |   |   |   |  Cost
1793 |   |   |   |- Debit/Credit Transfer Variance at ABS(Receivable's Cost - MPACD
1794 |   |   |   |  Cost)
1795 |   |   |- ELSIF (FOB-S) THEN  (Can be only receiving CG)
1796 |   |   |   |- IF (Discrete-Discrete Transfer) THEN
1797 |   |   |   |   |- Credit Inventory-Intransit at MPACD Cost
1798 |   |   |   |   |- Debit Inventory-OnHand at MPACD Cost
1799 |   |   |   |  ELSE (Process-Discrete Transfer)
1800 |   |   |   |   |- Credit Inventory-Intransit at Transfer price + Freight Cost
1801 |   |   |   |   |- Debit Inventory-OnHand at Transfer price + Freight Cost
1802 |   |   |   |  END IF;
1803 |   |   |- ELSIF (Direct Interorg) THEN
1804 |   |   |   |- Credit Payables at MPACD Cost
1805 |   |   |   |- Debit Inventory-OnHand at MPACD Cost
1806 |   |   |- END IF;
1807 |   |- END IF;
1808 |- END IF;
1809 |- IF (Logical Receipt for OPM-Disc and not Internal Order)
1810 |   |- Credit Interorg Payables at Transfer Price
1811 |   |- Credit Freight at Freight cost
1812 |   |- Debit Intransit-Inventory  at Transfer price + Freight Cost
1813 |- ELSIF (Logical Shipment for OPM-Disc and not Internal Order)
1814 |   |- Credit Intransit-Inventory at PWAC cost
1815 |   |- Credit Freight at Freight cost
1816 |   |- Debit InterOrg Receivables at Transfer Price
1817 |- END IF;
1818 
1819 
1820 For same cost group transfers, regardless of whether the transfer is
1821 direct or intransit, we make a debit/credit to an offset account and
1822 a debit/credit to inventory account (if asset sub) or expense account
1823 (if expense sub).
1824 
1825    ===================================================================
1826    =================================================================== */
1827 
1828   procedure interorg_cost_txn(
1829     i_ae_txn_rec                IN              CSTPALTY.cst_ae_txn_rec_type,
1830     i_ae_curr_rec               IN              CSTPALTY.cst_ae_curr_rec_type,
1831     l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
1832     o_ae_err_rec                OUT NOCOPY      CSTPALTY.cst_ae_err_rec_type
1833   ) IS
1834     l_exp_sub1                  NUMBER;
1835     l_exp_sub2                  NUMBER;
1836     l_exp_sub                   NUMBER;
1837     l_exp1                      NUMBER;
1838     l_exp2                      NUMBER;
1839     l_dr_flag                   BOOLEAN;
1840     l_acct_exist                NUMBER;
1841     l_stmt_num                  NUMBER := 0;
1842     l_acct_rec                  CSTPALTY.cst_ae_acct_rec_type;
1843     l_err_rec                   CSTPALTY.cst_ae_err_rec_type;
1844     l_txn_cost_group_id         NUMBER;
1845     l_txfr_txn_cost_group_id    NUMBER;
1846 
1847     process_error               EXCEPTION;
1848     no_cg_acct_error            EXCEPTION;
1849     l_exp_flg                   BOOLEAN ;
1850     l_exp_account               NUMBER;
1851     l_fob_point                 NUMBER;
1852 
1853     l_processing_cg_id      NUMBER;
1854     l_tprice_option         NUMBER;
1855     l_io_rcv_acct           NUMBER;
1856     l_io_freight_acct       NUMBER;
1857     l_io_pay_acct           NUMBER;
1858     l_io_txfr_cr_acct       NUMBER;
1859     l_io_txfr_var_acct      NUMBER;
1860     l_io_intransit_acct     NUMBER;
1861     l_transfer_cost_flag    VARCHAR2(1);
1862     l_txfr_legal_entity     NUMBER;
1863     l_txfr_cost_group_id    NUMBER;
1864     l_same_le_ct            NUMBER;
1865     l_pacp_pwac_cost        NUMBER;
1866     l_prev_period_id        NUMBER;
1867     l_prev_period_pwac_cost NUMBER;
1868     l_txfr_percent          NUMBER;
1869     l_txfr_credit           NUMBER;
1870     l_txfr_cost             NUMBER;
1871     l_trp_cost              NUMBER;
1872     l_io_rcv_value          NUMBER;
1873     l_io_pay_value          NUMBER;
1874     l_mpacd_cost            NUMBER;
1875     l_txfr_var_value        NUMBER;
1876     l_mat_ovhd              NUMBER;
1877     l_other_ele_costs       NUMBER;
1878     l_txfr_txn_id           NUMBER;
1879     l_shipping_cg_id        NUMBER;
1880     l_conv_rate             NUMBER;
1881     l_mat_account           NUMBER;
1882     l_mat_ovhd_account      NUMBER;
1883     l_cogs_account          NUMBER;
1884     l_accrual_account       NUMBER;
1885 
1886     l_profit_in_inv_account NUMBER;
1887     l_profit_in_inv_value   NUMBER;
1888     l_transfer_price        NUMBER;
1889     l_txfr_txn_qty          NUMBER;
1890 
1891     l_mat_ovhd_exists       NUMBER;
1892     l_mat_ovhd_cost         NUMBER;
1893     l_ovhd_absp             NUMBER;
1894     l_mptcd_cost            NUMBER;
1895 
1896     l_ae_line_rec       CSTPALTY.cst_ae_line_rec_type;
1897 
1898     -- INVCONV sikhanna, Variables added for OPM Convergence project
1899     l_pwac_cost            NUMBER;
1900     l_profit_or_loss       NUMBER;
1901     l_pe_flag              VARCHAR2(1);
1902     l_pd_txfr_ind          NUMBER := 0;
1903     no_mfca_acct_error     EXCEPTION;
1904     no_interorg_profit_acct_error EXCEPTION;
1905     no_profit_in_inv_acct_error EXCEPTION;
1906 
1907     l_perp_ship_value         NUMBER;
1908     l_use_prev_period_cost   NUMBER;
1909     l_cg_exp_item            NUMBER;
1910     l_api_name   CONSTANT VARCHAR2(30)          := 'CSTPAPBR.interorg_cost_txn';
1911 
1912   BEGIN
1913 
1914         IF g_debug_flag = 'Y' THEN
1915           fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': begin <<'
1916                             || ' transaction_id: ' || i_ae_txn_rec.transaction_id);
1917         END IF;
1918 
1919         -- Initialize variables.
1920         -- ---------------------
1921         l_err_rec.l_err_num := 0;
1922         l_err_rec.l_err_code := '';
1923         l_err_rec.l_err_msg := '';
1924 
1925         l_exp_flg := FALSE;
1926         l_transfer_cost_flag := 'N';
1927 
1928         l_mat_ovhd_exists  := 0;
1929         l_mat_ovhd_cost    := 0;
1930         l_ovhd_absp        := 0;
1931 
1932         l_stmt_num := 10;
1933 
1934         IF i_ae_txn_rec.subinventory_code IS NULL THEN
1935             l_exp_sub1:=0; -- If subInv code is missing, as in case of Logical Intransit Receipt
1936                            -- Treat it as asset subInv
1937         ELSE
1938             SELECT decode(asset_inventory,1,0,1)
1939             INTO  l_exp_sub1
1940             FROM  mtl_secondary_inventories
1941             WHERE secondary_inventory_name = i_ae_txn_rec.subinventory_code
1942             AND   organization_id = i_ae_txn_rec.organization_id;
1943         END IF;
1944 
1945         l_stmt_num := 15;
1946         BEGIN
1947            SELECT nvl(expense_account, -1)
1948            INTO  l_exp_account
1949            FROM  mtl_fiscal_cat_accounts
1950            WHERE legal_entity_id = i_ae_txn_rec.legal_entity_id
1951            AND   cost_type_id    = i_ae_txn_rec.cost_type_id
1952            AND   cost_group_id   = i_ae_txn_rec.cost_group_id
1953            AND   category_id     = i_ae_txn_rec.category_id;
1954         EXCEPTION
1955            WHEN no_data_found THEN
1956              fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num ||
1957                ': Category : '||to_char(i_ae_txn_rec.category_id) ||' has no accounts defined');
1958              raise no_mfca_acct_error;
1959         END;
1960 
1961         l_stmt_num := 20;
1962 
1963         /* Get FOB Point */
1964         IF (i_ae_txn_rec.txn_action_id IN (12,21,15,22)) THEN -- INVCONV sikhanna
1965             SELECT nvl(mmt.fob_point,mip.fob_point)
1966             INTO l_fob_point
1967             FROM mtl_material_transactions mmt,mtl_interorg_parameters mip
1968             WHERE mmt.transaction_id = i_ae_txn_rec.transaction_id
1969             AND   mip.from_organization_id = decode(i_ae_txn_rec.txn_action_id,
1970                                                     21,i_ae_txn_rec.organization_id,
1971                                                     22,i_ae_txn_rec.organization_id, -- INVCONV sikhanna
1972                                                     i_ae_txn_rec.xfer_organization_id)
1973             AND   mip.to_organization_id = decode(i_ae_txn_rec.txn_action_id,
1974                                                   21,i_ae_txn_rec.xfer_organization_id,
1975                                                   22,i_ae_txn_rec.xfer_organization_id, -- INVCONV sikhanna
1976                                                   i_ae_txn_rec.organization_id);
1977         END IF;
1978 
1979 
1980         l_stmt_num := 22;
1981 
1982         /* To determine whether this is a same cost group transfer or
1983                          cross cost group transfer, we need to compare the transaction org
1984                          cost group with the transfer transaction org cost group */
1985         BEGIN
1986            SELECT NVL(cost_group_id, -1)
1987            INTO   l_txn_cost_group_id
1988            FROM   cst_cost_group_assignments
1989            WHERE  organization_id = i_ae_txn_rec.organization_id;
1990         EXCEPTION
1991            WHEN no_data_found THEN
1992              l_txn_cost_group_id := -1;
1993         END;
1994 
1995         l_txfr_txn_cost_group_id := NVL(i_ae_txn_rec.xfer_cost_group_id, -1);
1996 
1997         IF g_debug_flag = 'Y' THEN
1998             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
1999                               ': cost_group_id = ' || i_ae_txn_rec.cost_group_id ||
2000                               ': txn_cost_group_id ' || l_txn_cost_group_id ||
2001                               ': txfr_txn_cost_group_id ' || l_txfr_txn_cost_group_id);
2002         END IF;
2003 
2004 
2005         /* If the transaction org cost group = the transfer transaction org cost group,
2006                          the transfer is within the same cost group */
2007 
2008         IF (l_txn_cost_group_id = l_txfr_txn_cost_group_id) THEN /* Same Cost Group Transfer */
2009 
2010           IF g_debug_flag = 'Y' THEN
2011             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Transfer Within Same Cost Group');
2012           END IF;
2013 
2014           /* Do not create accounting for same cost group transfers of expense items */
2015           IF (i_ae_txn_rec.exp_item = 1 and i_ae_txn_rec.txn_action_id = 3) THEN
2016             l_stmt_num := 23;
2017             IF g_debug_flag = 'Y' THEN
2018               fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': return >> ' ||
2019                 '(No accounting for Expense item)');
2020             END IF;
2021             RETURN;
2022           END IF;
2023 
2024           -- Figure out asset and expense subinventory for both sending and receiving.
2025           -- -------------------------------------------------------------------------
2026           IF (i_ae_txn_rec.txn_action_id = 3) THEN
2027 
2028                  l_stmt_num := 25;
2029 
2030                  IF g_debug_flag = 'Y' THEN
2031                    fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Direct Transfer');
2032                  END IF;
2033 
2034                  SELECT decode(asset_inventory,1,0,1)
2035                  INTO l_exp_sub2
2036                  FROM mtl_secondary_inventories
2037                  WHERE secondary_inventory_name = i_ae_txn_rec.xfer_subinventory
2038                  AND organization_id = i_ae_txn_rec.xfer_organization_id;
2039 
2040                  IF (l_exp_sub1 = 0 AND l_exp_sub2 = 0) OR
2041                     (l_exp_sub1 = 1 AND l_exp_sub2 = 1)
2042                  THEN
2043                    IF g_debug_flag = 'Y' THEN
2044                    fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': return >> '
2045                                                         || 'No accounting for Expense to Expense or Asset to Asset Subs');
2046                    END IF;
2047                    RETURN;
2048                  END IF;
2049 
2050                  IF (l_exp_sub1 = 0) THEN
2051                     l_dr_flag := TRUE;
2052                  ELSIF (l_exp_sub2 = 0) THEN
2053                     l_dr_flag := FALSE;
2054                  END IF;
2055           ELSE
2056                  l_stmt_num := 30;
2057 
2058                  IF g_debug_flag = 'Y' THEN
2059                    fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Intransit Transfer');
2060                  END IF;
2061 
2062                  if (l_exp_sub1 = 1 or i_ae_txn_rec.exp_item = 1) then
2063                    l_exp1 := 1;
2064                  else
2065                    l_exp1 := 0;
2066                  end if;
2067 
2068                  if ((l_fob_point = 1 and i_ae_txn_rec.txn_action_id = 21) or (l_fob_point = 2 and i_ae_txn_rec.txn_action_id = 12)) then
2069                    select decode(inventory_asset_flag,'Y',0,1)
2070                    into l_exp2
2071                    from mtl_system_items
2072                    where inventory_item_id = i_ae_txn_rec.inventory_item_id
2073                      and organization_id = i_ae_txn_rec.xfer_organization_id;
2074                  elsif ((l_fob_point = 2 and i_ae_txn_rec.txn_action_id = 21) or (l_fob_point = 1 and i_ae_txn_rec.txn_action_id = 12)) then
2075                    select decode(inventory_asset_flag,'Y',0,1)
2076                    into l_exp2
2077                    from mtl_system_items
2078                    where inventory_item_id = i_ae_txn_rec.inventory_item_id
2079                      and organization_id = i_ae_txn_rec.organization_id;
2080                  end if;
2081 
2082                  IF ((l_exp1 = 0 and l_exp2 = 0) or (l_exp1 = 1 and l_exp2 = 1)) THEN
2083                      return;
2084                  END IF;
2085 
2086                  IF (i_ae_txn_rec.txn_action_id = 21) THEN
2087                    if (l_exp1 = 1) then
2088                      l_dr_flag := TRUE;
2089                    else
2090                      l_dr_flag := FALSE;
2091                    end if;
2092                  ELSE
2093                    if (l_exp1 = 0) then
2094                      l_dr_flag := TRUE;
2095                    else
2096                      l_dr_flag := FALSE;
2097                    end if;
2098                  END IF;
2099 
2100              CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2101                                           i_ae_curr_rec,
2102                                           "FALSE", -- Exp Flag
2103                                           null, -- Exp Acct
2104                                           l_dr_flag,
2105                                           l_ae_line_tbl,
2106                                           l_err_rec);
2107 
2108           -- Check Error
2109           if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
2110               raise process_error;
2111           end if;
2112 
2113           l_dr_flag := not l_dr_flag;
2114 
2115              CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2116                                           i_ae_curr_rec,
2117                                           "TRUE", -- Exp Flag
2118                                           l_exp_account, -- Exp Acct
2119                                           l_dr_flag,
2120                                           l_ae_line_tbl,
2121                                           l_err_rec);
2122 
2123           -- Check Error
2124           if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
2125               raise process_error;
2126           end if;
2127 
2128           if (g_debug_flag = 'Y') then
2129               fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': Interorg_cost_txn >>');
2130           end if;
2131 
2132           return; -- done processing same cost group transfers
2133 
2134           END IF; /* end IF (i_ae_txn_rec.txn_action_id = 3)  */
2135 
2136           -- Create accounting for same cost group transfers
2137           ---------------------------------------------------
2138           l_stmt_num := 31;
2139 
2140           IF g_debug_flag = 'Y' THEN
2141             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Create Accounting for same cost group transfer');
2142           END IF;
2143 
2144           -- In R12, the Inventory Offset Account field has been removed from
2145           -- the Periodic Account Assignments form, so cst_org_cost_group_accounts.
2146           -- inventory_offset_account will not be set up.  Instead, we will use the
2147           -- expense account from mtl_fiscal_cat_accounts as the offset account.
2148 
2149           l_acct_rec.account := l_exp_account;
2150 
2151           -- For same cost group transfers, regardless of whether the transfer is
2152           -- direct or intransit, we make a debit/credit to an offset account and
2153           -- a debit/credit to inventory account (if asset sub) or expense account
2154           -- (if expense sub).
2155 
2156           -- Call Offset Accounts
2157           ------------------------
2158           l_stmt_num := 32;
2159           CSTPAPBR.offset_accounts(i_ae_txn_rec,
2160                                    i_ae_curr_rec,
2161                                    2, -- Accounting Line Type
2162                                    0, -- Elemental Flag
2163                                    0, -- Overhead Flag
2164                                    l_dr_flag,
2165                                    l_acct_rec,
2166                                    l_ae_line_tbl,
2167                                    l_err_rec);
2168 
2169           -- Check error
2170           if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
2171             raise process_error;
2172           end if;
2173 
2174           -- Toggle the Debit Flag
2175           ------------------------
2176           l_dr_flag := not l_dr_flag;
2177 
2178           -- Call Inventory Accounts
2179           --------------------------
2180           l_stmt_num := 33;
2181           if (l_exp_flg) then
2182              CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2183                                           i_ae_curr_rec,
2184                                           l_exp_flg, -- Exp Flag
2185                                           l_exp_account, -- Exp Acct
2186                                           l_dr_flag,
2187                                           l_ae_line_tbl,
2188                                           l_err_rec);
2189           else
2190              CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2191                                           i_ae_curr_rec,
2192                                           "FALSE", -- Exp Flag
2193                                           null, -- Exp Acct
2194                                           l_dr_flag,
2195                                           l_ae_line_tbl,
2196                                           l_err_rec);
2197           end if;
2198 
2199           -- Check Error
2200           if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
2201               raise process_error;
2202           end if;
2203 
2204           if (g_debug_flag = 'Y') then
2205               fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': Interorg_cost_txn >>');
2206           end if;
2207 
2208           return; -- done processing same cost group transfers
2209 
2210         ELSE   /* Different Cost Group Transfer */
2211 
2212               l_stmt_num := 35;
2213 
2214               IF g_debug_flag = 'Y' THEN
2215                 fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Transfer across different cost groups');
2216               END IF;
2217 
2218               /* Select the expense item status of the item in the org belonging
2219                  to the cost group currently being processed.  This may be different
2220                  than the cost group of the transaction org. */
2221               IF (i_ae_txn_rec.cost_group_id <> l_txn_cost_group_id) THEN
2222                 SELECT decode(inventory_asset_flag, 'Y', 0, 1)
2223                 INTO l_cg_exp_item
2224                 FROM mtl_system_items
2225                 WHERE organization_id = i_ae_txn_rec.xfer_organization_id
2226                 AND inventory_item_id = i_ae_txn_rec.inventory_item_id;
2227               ELSE
2228                 l_cg_exp_item := i_ae_txn_rec.exp_item;
2229               END IF;
2230 
2231               IF (l_exp_sub1 = 1 OR l_cg_exp_item = 1) THEN
2232                 /* Prior to R12, the code would return and not create accounting
2233                  * for expense subs or for expense items.
2234                  * Starting in R12, we should be creating accounting for expense items and
2235                  * subs, using the expense account when creating entries for On-hand.
2236                  */
2237                 l_exp_flg := TRUE;
2238               END IF;
2239 
2240               IF (i_ae_txn_rec.txn_action_id = 3 and i_ae_txn_rec.primary_quantity < 0)
2241                   OR (i_ae_txn_rec.txn_action_id in (21,15)) THEN -- INVCONV sikhanna
2242                 l_dr_flag := TRUE;
2243               ELSE
2244                 l_dr_flag := FALSE;
2245               END IF;
2246         END IF;
2247 
2248         l_stmt_num := 40;
2249 
2250         /* currency conversion rate in i_ae_curr_rec is always from the sending org to the receiving org */
2251         l_conv_rate := i_ae_curr_rec.currency_conv_rate;
2252 
2253         IF g_debug_flag = 'Y' THEN
2254            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Currency conversion rate: ' || l_conv_rate);
2255         END IF;
2256 
2257         /* Get transfer transaction id for receipt transactions */
2258         IF ( i_ae_txn_rec.txn_action_id in (12,22)
2259              OR (i_ae_txn_rec.txn_action_id = 3 AND sign(i_ae_txn_rec.primary_quantity) = 1) ) THEN
2260 
2261             l_stmt_num := 43;
2262             SELECT transfer_transaction_id
2263             INTO l_txfr_txn_id
2264             FROM mtl_material_transactions
2265             WHERE transaction_id = i_ae_txn_rec.transaction_id;
2266 
2267         END IF;
2268 
2269             /* Get shipment CG */
2270             l_stmt_num := 45;
2271             -- changed as a bug found during testing.
2272             BEGIN
2273                 SELECT cost_group_id
2274                 INTO l_shipping_cg_id
2275                 FROM cst_cost_group_assignments
2276                 WHERE organization_id = (SELECT decode(i_ae_txn_rec.txn_action_id,
2277                                                         21, organization_id,
2278                                                         22, organization_id, -- Logical Shipping txn
2279                                                         12, transfer_organization_id,
2280                                                         15, transfer_organization_id, -- Logical Receipt txn
2281                                                         3, decode(sign(i_ae_txn_rec.primary_quantity),
2282                                                                     1, transfer_organization_id,
2283                                                                         organization_id),
2284                                                           NULL)
2285                                         FROM mtl_material_transactions
2286                                         WHERE transaction_id = i_ae_txn_rec.transaction_id);
2287             EXCEPTION
2288             WHEN no_data_found THEN
2289                 l_shipping_cg_id := -1;
2290             END;
2291 
2292         /* Process Shipment transactions */
2293 
2294         IF ( i_ae_txn_rec.txn_action_id in (21,15) or (i_ae_txn_rec.txn_action_id = 3 and i_ae_txn_rec.primary_quantity < 0) ) THEN
2295 
2296            l_stmt_num := 50;
2297 
2298            /* Get profile status for internal sales orders */
2299            BEGIN
2300                /* Change the Query for the performance. Bug 4586534 */
2301                SELECT nvl(fnd_profile.value('CST_TRANSFER_PRICING_OPTION'), 0)
2302                INTO   l_tprice_option
2303                FROM   mtl_intercompany_parameters MIP
2304                WHERE  fnd_profile.value('INV_INTERCOMPANY_INVOICE_INTERNAL_ORDER') = 1
2305                AND (select  count(1)
2306                           from    hr_organization_information HOI
2307                           where   HOI.organization_id = decode(i_ae_txn_rec.txn_action_id,
2308                                                    21, i_ae_txn_rec.organization_id,
2309                                                    i_ae_txn_rec.xfer_organization_id)
2310                           AND    HOI.org_information_context = 'Accounting Information'
2311                           AND    MIP.ship_organization_id = to_number(HOI.org_information3)
2312                           AND    ROWNUM < 2) >0
2313                AND ( select  count(1)
2314                            from    hr_organization_information HOI2
2315                            where   HOI2.organization_id = decode(i_ae_txn_rec.txn_action_id,
2316                                                     21, i_ae_txn_rec.xfer_organization_id,
2317                                                     i_ae_txn_rec.organization_id)
2318                             AND    HOI2.org_information_context = 'Accounting Information'
2319                             AND    MIP.sell_organization_id = to_number(HOI2.org_information3)
2320                             AND    ROWNUM < 2)>0
2321                AND    MIP.flow_type = 1;
2322 
2323            EXCEPTION
2324            WHEN NO_DATA_FOUND THEN
2325                l_tprice_option := -1; /* Chenged it to be -1, will toggle to 0 later */
2326            END;
2327 
2328             -- Processing the process-discrete txns
2329             -- INVCONV sikhanna START
2330             l_stmt_num := 52;
2331             SELECT MOD(SUM(DECODE(process_enabled_flag,'Y',1,2)), 2)
2332             INTO l_pd_txfr_ind
2333             FROM MTL_PARAMETERS MP
2334             WHERE MP.ORGANIZATION_ID = i_ae_txn_rec.xfer_organization_id
2335             OR MP.ORGANIZATION_ID    = i_ae_txn_rec.organization_id;
2336 
2337             /* Process-Discrete X-fers set the profile to 2 if ICR relations setup and in diff OU */
2338             /* l_tprice_option=-1 only when ICR is not set up and exception is thrown, then continue as normal */
2339             IF (l_pd_txfr_ind=1 and l_tprice_option <> -1) THEN -- OPM ST BUG 5351896
2340 
2341                l_stmt_num := 54;
2342 
2343                l_tprice_option := 2; /* Make it 2 to ignore the CST Transfer Price profile */
2344 
2345                IF g_debug_flag = 'Y' THEN
2346                   fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ' ICR Set up:process-discrete xfer');
2347                END IF;
2348 
2349             END IF;
2350 
2351             IF l_tprice_option = -1 THEN
2352                l_tprice_option := 0; /* Toggle it to 0 as 0 is used later */
2353             END IF;
2354             -- INVCONV sikhanna END
2355 
2356             IF g_debug_flag = 'Y' THEN
2357                        fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2358                                                                     ': l_tprice_option:  ' || l_tprice_option || ', process-discrete xfer: ' || l_pd_txfr_ind);
2359             END IF;
2360 
2361             l_stmt_num := 55;
2362 
2363             -- INVCONV sikhanna, process Internal sales order for OPM conv here
2364             IF ((i_ae_txn_rec.txn_src_type_id IN (7,8)) AND (l_tprice_option IN (1,2))) THEN    /* Process Internal Orders */
2365 
2366                 IF g_debug_flag = 'Y' THEN
2367                        fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2368                                                                     ': Shipment txn of Internal Order Transfer');
2369                 END IF;
2370            /* Internal Sales Order with transfer price specified and profiles set.
2371               CST_TRANSFER_PRICING_OPTION is set to: Yes,Price as Incoming Cost or Yes, Price Not as Incoming Cost.
2372               For other values of this profile,the transaction is treated as an ordinary interorg transfer */
2373 
2374                 IF (l_fob_point = 1) THEN   /* FOB - Shipment */
2375 
2376                    IF (i_ae_txn_rec.cost_group_id = l_shipping_cg_id) THEN /* This is the sending CG */
2377 
2378                        l_stmt_num := 57;
2379 
2380                        IF g_debug_flag = 'Y' THEN
2381                          fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2382                                                                       ': Shipment txn of Internal Order FOB Shipment - Sending Cost Group');
2383                        END IF;
2384 
2385                           IF g_debug_flag = 'Y' THEN
2386                             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2387                                                                            ': Discrete-Discrete Transfer');
2388                           END IF;
2389 
2390                        IF (l_cg_exp_item <> 1) THEN
2391                           /* Credit On-Hand and Debit COGS */ -- only if the item is not expense in the sending org
2392 
2393                           -- Bug 5573993 - Derive the COGS account to break dependency on perpetual
2394                           -- code as if the trxn is not costed, mmt has incorrect account stamped
2395 
2396                           IF g_debug_flag = 'Y' THEN
2397                              fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2398                                                                        'Deriving COGS account');
2399                           END IF;
2400 
2401                           l_stmt_num := 61;
2402 
2403                           l_cogs_account := get_intercompany_account (i_ae_txn_rec => i_ae_txn_rec,
2404                                                                       o_ae_err_rec => l_err_rec);
2405 
2406                           IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2407                               RAISE process_error;
2408                           END IF;
2409 
2410                           IF g_debug_flag = 'Y' THEN
2411                              fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2412                                                                        'COGS Account ID: ' || l_cogs_account);
2413                           END IF;
2414 
2415                           -- Stamp all elemental accounts with the COGS account
2416                           l_acct_rec.mat_account := l_cogs_account;
2417                           l_acct_rec.mat_ovhd_account := l_cogs_account;
2418                           l_acct_rec.res_account := l_cogs_account;
2419                           l_acct_rec.osp_account := l_cogs_account;
2420                           l_acct_rec.ovhd_account := l_cogs_account;
2421                           l_acct_rec.account := l_cogs_account;
2422 
2423                           l_dr_flag := TRUE;
2424 
2425                           -- Debit COGS
2426                           -- ----------
2427                           l_stmt_num := 62;
2428 
2429                           CSTPAPBR.offset_accounts (i_ae_txn_rec,
2430                                                    i_ae_curr_rec,
2431                                                    2,    --- Acct Line Type
2432                                                    0,    --- Elemental Flag
2433                                                    0,    --- Ovhd Flag
2434                                                    l_dr_flag,
2435                                                    l_acct_rec,
2436                                                    l_ae_line_tbl,
2437                                                    l_err_rec);
2438                           -- check error
2439                           IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2440                               RAISE process_error;
2441                           END IF;
2442 
2443                           l_dr_flag := not l_dr_flag;
2444 
2445                           l_stmt_num := 64;
2446 
2447                           -- Credit On-hand (if expense, then use expense account)
2448                           -- -----------------------------------------------------
2449                           IF (l_exp_flg) THEN
2450                                CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2451                                                             i_ae_curr_rec,
2452                                                             l_exp_flg, -- Exp Flag
2453                                                             l_exp_account, -- Exp Acct
2454                                                             l_dr_flag,
2455                                                             l_ae_line_tbl,
2456                                                             l_err_rec);
2457                           ELSE
2458                                CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2459                                                             i_ae_curr_rec,
2460                                                             "FALSE", -- Exp Flag
2461                                                             null, -- Exp Acct
2462                                                             l_dr_flag,
2463                                                             l_ae_line_tbl,
2464                                                             l_err_rec);
2465                           END IF;
2466 
2467                           -- check error
2468                           IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2469                               RAISE process_error;
2470                           END IF;
2471 
2472                        END IF; /* IF (l_cg_exp_item <> 1) THEN  */
2473 
2474                    ELSE /* This is the receiving CG */
2475 
2476                          l_stmt_num := 65;
2477 
2478                          IF g_debug_flag = 'Y' THEN
2479                            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2480                                                                     ': Shipment txn of Internal Order FOB Shipment - Receiving Cost Group');
2481                          END IF;
2482 
2483                          /* Credit Intercompany Accrual and Debit Intransit */
2484 
2485                          /* Get Intercompany Accrual Account */
2486 
2487                          -- Bug 5573993 - Derive the Intercompany account to break dependency on perpetual
2488                          -- code as if the trxn is not costed, mmt has incorrect or no account stamped
2489 
2490                          IF g_debug_flag = 'Y' THEN
2491                             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2492                                                                        'Deriving intercompany account');
2493                          END IF;
2494 
2495                          l_accrual_account := get_intercompany_account (i_ae_txn_rec => i_ae_txn_rec,
2496                                                                         o_ae_err_rec => l_err_rec);
2497 
2498                          IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2499                              RAISE process_error;
2500                          END IF;
2501 
2502                          IF g_debug_flag = 'Y' THEN
2503                             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2504                                                                        'Intercompany Account ID: ' || l_accrual_account);
2505                          END IF;
2506 
2507 
2508                          l_dr_flag := FALSE;
2509 
2510                          /* Absorb MOH if exists */
2511                          l_stmt_num := 67;
2512                          select count(transaction_id)
2513                          into l_mat_ovhd_exists
2514                          from mtl_pac_actual_cost_details
2515                          where transaction_id = i_ae_txn_rec.transaction_id
2516                          and pac_period_id = i_ae_txn_rec.accounting_period_id
2517                          and cost_group_id = i_ae_txn_rec.cost_group_id
2518                          and cost_element_id = 2
2519                          and level_type = 1;
2520 
2521                          l_stmt_num := 68;
2522                          if (l_mat_ovhd_exists > 0) then
2523                             ovhd_accounts(i_ae_txn_rec,
2524                                           i_ae_curr_rec,
2525                                           l_dr_flag,
2526                                           l_ae_line_tbl,
2527                                           l_err_rec);
2528                            -- check error
2529                            if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
2530                              raise process_error;
2531                            end if;
2532                          end if;
2533 
2534                          -- Credit Intercompany accrual @ transfer price
2535                          -- --------------------------------------------
2536                             l_stmt_num := 69;
2537 
2538                             /* 5638994: For txn_axn 15, Logical Intr Rcpt, the transfer price
2539                                is already in base currency. No currency conv needed */
2540                             IF (i_ae_txn_rec.txn_action_id = 15) THEN
2541 
2542                                 SELECT  NVL(mmt.transfer_price,0)
2543                                 INTO    l_transfer_price
2544                                 FROM    mtl_material_transactions MMT
2545                                 WHERE   MMT.transaction_id = i_ae_txn_rec.transaction_id;
2546 
2547                             ELSE
2548 
2549                                 SELECT  NVL(mmt.transfer_price,0) * l_conv_rate
2550                                 INTO    l_transfer_price
2551                                 FROM    mtl_material_transactions MMT
2552                                 WHERE   MMT.transaction_id = i_ae_txn_rec.transaction_id;
2553 
2554                             END IF;
2555 
2556                             IF g_debug_flag = 'Y' THEN
2557                                fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2558                                ': Cr Intercompany Accrual @ : ' || l_transfer_price);
2559                             END IF;
2560 
2561                             l_ae_line_rec.account := l_accrual_account;
2562                             l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity) * l_transfer_price;
2563                             l_ae_line_rec.resource_id := NULL;
2564                             l_ae_line_rec.cost_element_id := NULL;
2565                             l_ae_line_rec.ae_line_type := 2;
2566 
2567                             insert_account (i_ae_txn_rec,
2568                                            i_ae_curr_rec,
2569                                            l_dr_flag,
2570                                            l_ae_line_rec,
2571                                            l_ae_line_tbl,
2572                                            l_err_rec);
2573 
2574                             -- check error
2575                             IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2576                                 RAISE process_error;
2577                             END IF;
2578 
2579                          IF (l_tprice_option = 2) THEN
2580 
2581                            IF g_debug_flag = 'Y' THEN
2582                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2583                                ': CST:Transfer Price Option = Yes, Price As Incoming Cost');
2584                             END IF;
2585 
2586                          ELSIF (l_tprice_option = 1) THEN
2587 
2588                              IF g_debug_flag = 'Y' THEN
2589                                 fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2590                                   ': CST:Transfer Price Option = Yes, Price Not As Incoming Cost');
2591                              END IF;
2592 
2593                             -- Debit/Credit Profit in Inventory
2594                             l_stmt_num := 80;
2595 
2596                             SELECT sum(nvl(transaction_cost,0))
2597                             INTO   l_mptcd_cost
2598                             FROM   mtl_pac_txn_cost_details
2599                             WHERE  transaction_id = i_ae_txn_rec.transaction_id
2600                             AND    pac_period_id = i_ae_txn_rec.accounting_period_id
2601                             AND    cost_group_id = i_ae_txn_rec.cost_group_id;
2602 
2603                             l_profit_in_inv_value := l_mptcd_cost - l_transfer_price;
2604 
2605                             IF (sign(l_profit_in_inv_value) = 1) THEN
2606                                 l_dr_flag := FALSE;
2607                             ELSIF (sign(l_profit_in_inv_value) = -1) THEN
2608                                 l_dr_flag := TRUE;
2609                             ELSE
2610                                 l_dr_flag := NULL;
2611                             END IF;
2612 
2613                             l_profit_in_inv_value := abs(l_profit_in_inv_value);
2614 
2615                             IF (l_dr_flag IS NOT NULL) THEN
2616 
2617                                     l_stmt_num := 84;
2618                                     SELECT nvl(profit_in_inv_account, -1)
2619                                     INTO   l_profit_in_inv_account
2620                                     FROM   mtl_interorg_parameters
2621                                     WHERE  from_organization_id = i_ae_txn_rec.organization_id
2622                                     AND    to_organization_id = i_ae_txn_rec.xfer_organization_id;
2623 
2624                                     IF (l_profit_in_inv_account = -1) THEN
2625                                        RAISE no_profit_in_inv_acct_error;
2626                                     END IF;
2627 
2628                                     /* Profit in Inventory entry */
2629                                     l_ae_line_rec.account := l_profit_in_inv_account;
2630                                     l_ae_line_rec.transaction_value := l_profit_in_inv_value  * abs(i_ae_txn_rec.primary_quantity);
2631                                     l_ae_line_rec.resource_id := NULL;
2632                                     l_ae_line_rec.cost_element_id := NULL;
2633                                     l_ae_line_rec.ae_line_type := 30;   -- Profit in Inventory
2634 
2635                                     l_stmt_num := 86;
2636 
2637                                     insert_account (i_ae_txn_rec,
2638                                                     i_ae_curr_rec,
2639                                                     l_dr_flag,
2640                                                     l_ae_line_rec,
2641                                                     l_ae_line_tbl,
2642                                                     l_err_rec);
2643 
2644                                     -- check error
2645                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2646                                         RAISE process_error;
2647                                     END IF;
2648 
2649                             END IF; /* (l_dr_flag IS NOT NULL) */
2650 
2651                          END IF; /* (l_tprice_option = 2) */
2652 
2653 
2654                          -- Debit Intransit (or Expense if an expense item in the receiving CG)
2655                          ----------------------------------------------------------------------
2656                          l_dr_flag := TRUE;
2657 
2658                          /* If the item is expense in the receiving CG, hit the expense account instead of Intransit */
2659                          IF (l_cg_exp_item = 1) THEN
2660                            /* Debit Expense */
2661                            CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2662                                                        i_ae_curr_rec,
2663                                                        l_exp_flg, -- Exp Flag
2664                                                        l_exp_account, -- Exp Acct
2665                                                        l_dr_flag,
2666                                                        l_ae_line_tbl,
2667                                                        l_err_rec);
2668                          ELSE
2669                            /* Debit Inventory - Intransit */
2670                            CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2671                                                      i_ae_curr_rec,
2672                                                      "FALSE",   --- Exp Flag
2673                                                       null,      --- Exp Acct
2674                                                       l_dr_flag,
2675                                                       l_ae_line_tbl,
2676                                                       l_err_rec,
2677                                                       1);
2678                         END IF;
2679 
2680                         -- check error
2681                         IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2682                            RAISE process_error;
2683                         END IF;
2684 
2685                    END IF;
2686 
2687                 ELSIF (l_fob_point = 2) THEN /* Internal Order FOBR */
2688 
2689                         l_stmt_num := 90;
2690                         /* This can only be the sending CG */
2691 
2692                          IF g_debug_flag = 'Y' THEN
2693                            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2694                                                          ': Shipment txn of Internal Order FOB Receipt - Shipping Cost Group');
2695                          END IF;
2696 
2697                      IF (l_cg_exp_item <> 1) THEN
2698                         /*   Debit Intransit and Credit OnHand - only if this is not an expense item */
2699 
2700                         l_dr_flag := TRUE;
2701 
2702                         -- Debit Intransit
2703                         ------------------
2704                        CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2705                                                     i_ae_curr_rec,
2706                                                     "FALSE",   --- Exp Flag
2707                                                     null,      --- Exp Acct
2708                                                     l_dr_flag,
2709                                                     l_ae_line_tbl,
2710                                                     l_err_rec,
2711                                                     1);
2712 
2713                           -- check error
2714                           IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2715                               RAISE process_error;
2716                           END IF;
2717 
2718                        l_dr_flag := not l_dr_flag;
2719 
2720                        -- Credit On-hand (if expense, then use expense account)
2721                        -- -----------------------------------------------------
2722                        IF (l_exp_flg) THEN
2723                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2724                                                        i_ae_curr_rec,
2725                                                        l_exp_flg, -- Exp Flag
2726                                                        l_exp_account, -- Exp Acct
2727                                                        l_dr_flag,
2728                                                        l_ae_line_tbl,
2729                                                        l_err_rec);
2730                        ELSE
2731                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2732                                                        i_ae_curr_rec,
2733                                                        "FALSE", -- Exp Flag
2734                                                        null, -- Exp Acct
2735                                                        l_dr_flag,
2736                                                        l_ae_line_tbl,
2737                                                        l_err_rec);
2738                        END IF;
2739 
2740                        -- check error
2741                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2742                            RAISE process_error;
2743                        END IF;
2744 
2745                     END IF; /* IF (l_cg_exp_item <> 1) THEN  */
2746 
2747                  END IF;
2748 
2749             /* Ignore OPM-Discrete Logical txns; to be processed at the end */
2750             ELSIF (i_ae_txn_rec.txn_action_id not in (15,22)) THEN /* Ordinary Interorg shipments */
2751 
2752                  l_stmt_num := 92;
2753                  IF g_debug_flag = 'Y' THEN
2754                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2755                                                           ': Interorg Shipment transaction');
2756                  END IF;
2757 
2758                  IF ((i_ae_txn_rec.cost_group_id = l_shipping_cg_id) AND l_fob_point = 1) THEN /* This is the sending CG for FOBS */
2759 
2760                      l_stmt_num := 94;
2761                      IF g_debug_flag = 'Y' THEN
2762                         fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2763                                                               ': Shipment txn of FOB Shipment - Shipping Cost Group');
2764                      END IF;
2765 
2766 
2767                    l_dr_flag := FALSE;
2768                    IF (l_cg_exp_item <> 1) THEN
2769                      /* Credit OnHand at PWAC Cost */
2770 
2771                      -- Credit On-hand (if expense, then use expense account)
2772                      -- -----------------------------------------------------
2773                      IF (l_exp_flg) THEN
2774                         CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2775                                                      i_ae_curr_rec,
2776                                                      l_exp_flg, -- Exp Flag
2777                                                      l_exp_account, -- Exp Acct
2778                                                      l_dr_flag,
2779                                                      l_ae_line_tbl,
2780                                                      l_err_rec);
2781                      ELSE
2782                         CSTPAPBR.inventory_accounts (i_ae_txn_rec,
2783                                                      i_ae_curr_rec,
2784                                                      "FALSE", -- Exp Flag
2785                                                      null, -- Exp Acct
2786                                                      l_dr_flag,
2787                                                      l_ae_line_tbl,
2788                                                      l_err_rec);
2789                      END IF;
2790                    END IF; /* IF (l_cg_exp_item <> 1) THEN      */
2791 
2792                     /* Debit Receivables at estimated cost: This can be CPIC cost/previous period cost/MTA cost */
2793 
2794                    l_dr_flag := not l_dr_flag;
2795 
2796                     l_stmt_num := 96;
2797 
2798                     /* Get relevant accounts */
2799                     SELECT mip.interorg_receivables_account,
2800                            mmt.transportation_dist_account,
2801                            mip.interorg_transfer_cr_account,
2802                            nvl(mip.interorg_profit_account, -1)
2803                     INTO   l_io_rcv_acct,
2804                            l_io_freight_acct,
2805                            l_io_txfr_cr_acct,
2806                            l_io_txfr_var_acct
2807                     FROM   mtl_interorg_parameters mip,
2808                            mtl_material_transactions mmt
2809                     WHERE  mip.from_organization_id = i_ae_txn_rec.organization_id
2810                     AND    mip.to_organization_id = i_ae_txn_rec.xfer_organization_id
2811                     AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
2812 
2813                     l_ae_line_rec.account := l_io_rcv_acct;
2814 
2815                     l_stmt_num := 98;
2816 
2817                     /* Get prior period id */
2818                     SELECT nvl(max(cpp.pac_period_id), -1)
2819                     INTO  l_prev_period_id
2820                     FROM  cst_pac_periods cpp
2821                     WHERE cpp.cost_type_id = i_ae_txn_rec.cost_type_id
2822                     AND   cpp.legal_entity = i_ae_txn_rec.legal_entity_id
2823                     AND   cpp.pac_period_id < i_ae_txn_rec.accounting_period_id;
2824 
2825                     l_stmt_num := 100;
2826 
2827                     /* The flag selected below indicates if PACP is used or not */
2828                     SELECT TRANSFER_COST_FLAG
2829                     INTO   l_transfer_cost_flag
2830                     FROM   CST_LE_COST_TYPES
2831                     WHERE  LEGAL_ENTITY = i_ae_txn_rec.legal_entity_id
2832                     AND    COST_TYPE_ID = i_ae_txn_rec.cost_type_id;
2833 
2834                     l_stmt_num := 102;
2835 
2836                     SELECT NVL(MAX(cost_group_id),-1)
2837                     INTO   l_txfr_cost_group_id
2838                     FROM   cst_cost_group_assignments
2839                     WHERE  organization_id = i_ae_txn_rec.xfer_organization_id;
2840 
2841                     l_stmt_num := 104;
2842 
2843                    /* Get legal entity of the other cost group,if available */
2844                     BEGIN
2845                         SELECT legal_entity
2846                         INTO l_txfr_legal_entity
2847                         FROM cst_cost_groups
2848                         WHERE cost_group_id = l_txfr_cost_group_id;
2849                     EXCEPTION
2850                     WHEN NO_DATA_FOUND THEN
2851                         l_txfr_legal_entity := NULL;
2852                     END;
2853 
2854                    /* See if i_cost_type_id is attached to the transfer LE as well */
2855                    l_stmt_num := 106;
2856                    SELECT count(*)
2857                    INTO   l_same_le_ct
2858                    FROM   cst_le_cost_types
2859                    WHERE  legal_entity = l_txfr_legal_entity
2860                    AND    cost_type_id = i_ae_txn_rec.cost_type_id;
2861 
2862                    /* Check for the same LE/CT combination */
2863                    IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0 AND l_transfer_cost_flag = 'Y') THEN
2864 
2865                     --  IF (l_transfer_cost_flag = 'Y') THEN
2866 
2867                            /* PACP used:
2868                               The estimated cost is available in CPICD */
2869                            l_stmt_num := 110;
2870 
2871                            SELECT NVL(CPIC.item_cost,0)
2872                            INTO   l_pacp_pwac_cost
2873                            FROM   CST_PAC_ITEM_COSTS CPIC
2874                            WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
2875                            AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
2876                            AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
2877 
2878                            IF g_debug_flag = 'Y' THEN
2879                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2880                                                          ': Using PAC Absorption Cost: ' || l_pacp_pwac_cost);
2881                            END IF;
2882 
2883                            /* Get transfer credit information */
2884                            l_stmt_num := 112;
2885                            SELECT nvl(transfer_percentage,0),nvl(transfer_cost,0)
2886                            INTO   l_txfr_percent,l_txfr_cost
2887                            FROM   mtl_material_transactions
2888                            WHERE  transaction_id = i_ae_txn_rec.transaction_id;
2889 
2890                            IF (l_txfr_percent <> 0) THEN
2891                                l_txfr_credit := (l_txfr_percent * l_pacp_pwac_cost / 100);
2892                            ELSIF (l_txfr_cost <> 0) THEN
2893                                l_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
2894                            ELSE
2895                                l_txfr_credit := 0;
2896                            END IF;
2897 
2898                            l_io_rcv_value := (l_pacp_pwac_cost + l_txfr_credit);
2899 
2900                            l_ae_line_rec.transaction_value := l_io_rcv_value * abs(i_ae_txn_rec.primary_quantity);
2901                            l_ae_line_rec.resource_id := NULL;
2902                            l_ae_line_rec.cost_element_id := NULL;
2903                            l_ae_line_rec.ae_line_type := 10;  -- Receivables
2904 
2905                            l_stmt_num := 114;
2906                            insert_account (i_ae_txn_rec,
2907                                            i_ae_curr_rec,
2908                                            l_dr_flag,
2909                                            l_ae_line_rec,
2910                                            l_ae_line_tbl,
2911                                            l_err_rec);
2912 
2913                           -- check error
2914                           IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2915                               RAISE process_error;
2916                           END IF;
2917 
2918                            /* Create entries for transfer credit*/
2919                            /* No need to create transfer variance entry since estimated sending CG cost = sending CG PMAC in CPIC */
2920 
2921                            l_dr_flag := FALSE;
2922 
2923                            l_ae_line_rec.account := l_io_txfr_cr_acct;
2924                            l_ae_line_rec.transaction_value := l_txfr_credit * abs(i_ae_txn_rec.primary_quantity);
2925                            l_ae_line_rec.resource_id := NULL;
2926                            l_ae_line_rec.cost_element_id := NULL;
2927                            l_ae_line_rec.ae_line_type := 11;  -- Transfer Credit
2928 
2929                            IF (l_ae_line_rec.transaction_value <> 0) THEN
2930 
2931                                 insert_account (i_ae_txn_rec,
2932                                                 i_ae_curr_rec,
2933                                                 l_dr_flag,
2934                                                 l_ae_line_rec,
2935                                                 l_ae_line_tbl,
2936                                                 l_err_rec);
2937                                 -- check error
2938                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
2939                                     RAISE process_error;
2940                                 END IF;
2941 
2942                            END IF;
2943                   ELSE
2944 
2945                      IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0 AND l_prev_period_id <> -1) THEN
2946 
2947                            l_stmt_num := 115;
2948                            l_use_prev_period_cost := 1;
2949 
2950                            /* Get prior period PWAC Cost */
2951                                 BEGIN
2952                                     SELECT nvl(CPIC.item_cost,0)
2953                                       INTO l_prev_period_pwac_cost
2954                                       FROM CST_PAC_ITEM_COSTS CPIC
2955                                      WHERE CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
2956                                        AND CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
2957                                        AND CPIC.PAC_PERIOD_ID     = l_prev_period_id;
2958                                 EXCEPTION
2959                                 when no_data_found then
2960                                     /* Use perpetual cost if prior period cost is not available */
2961                                     l_use_prev_period_cost := 0;
2962                                 END;
2963                      ELSE
2964                        /* Use perpetual cost if prior period cost is not available or if the cost groups are not in the same LE/CT. */
2965                            l_use_prev_period_cost := 0;
2966                      END IF;
2967 
2968                      IF (l_use_prev_period_cost = 1) THEN
2969 
2970                            IF g_debug_flag = 'Y' THEN
2971                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
2972                                                                              ': Using prior period PWAC cost: ' || l_prev_period_pwac_cost);
2973                            END IF;
2974 
2975                            /* Get transfer credit information */
2976                            l_stmt_num := 117;
2977                            SELECT nvl(transfer_percentage,0),nvl(transfer_cost,0)
2978                            INTO   l_txfr_percent,l_txfr_cost
2979                            FROM   mtl_material_transactions
2980                            WHERE  transaction_id = i_ae_txn_rec.transaction_id;
2981 
2982                            IF (l_txfr_percent <> 0) THEN
2983                                l_txfr_credit := (l_txfr_percent * l_prev_period_pwac_cost / 100);
2984                            ELSIF (l_txfr_cost <> 0) THEN
2985                                l_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
2986                            ELSE
2987                                l_txfr_credit := 0;
2988                            END IF;
2989 
2990                            l_io_rcv_value := (l_prev_period_pwac_cost + l_txfr_credit);
2991 
2992                            l_ae_line_rec.transaction_value := l_io_rcv_value * abs(i_ae_txn_rec.primary_quantity);
2993                            l_ae_line_rec.resource_id := NULL;
2994                            l_ae_line_rec.cost_element_id := NULL;
2995                            l_ae_line_rec.ae_line_type := 10;  -- Receivables
2996 
2997                            l_stmt_num := 118;
2998                            insert_account (i_ae_txn_rec,
2999                                            i_ae_curr_rec,
3000                                            l_dr_flag,
3001                                            l_ae_line_rec,
3002                                            l_ae_line_tbl,
3003                                            l_err_rec);
3004 
3005                            -- check error
3006                            IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3007                                RAISE process_error;
3008                            END IF;
3009 
3010                            /* Create entries for transfer credit and transfer variance */
3011                            l_stmt_num := 120;
3012 
3013                            l_dr_flag := FALSE;
3014 
3015                            l_ae_line_rec.account := l_io_txfr_cr_acct;
3016                            l_ae_line_rec.transaction_value := l_txfr_credit * abs(i_ae_txn_rec.primary_quantity);
3017                            l_ae_line_rec.resource_id := NULL;
3018                            l_ae_line_rec.cost_element_id := NULL;
3019                            l_ae_line_rec.ae_line_type := 11;  -- Transfer Credit
3020 
3021                            IF (l_ae_line_rec.transaction_value <> 0) THEN
3022 
3023                                 insert_account (i_ae_txn_rec,
3024                                                 i_ae_curr_rec,
3025                                                 l_dr_flag,
3026                                                 l_ae_line_rec,
3027                                                 l_ae_line_tbl,
3028                                                 l_err_rec);
3029                                 -- check error
3030                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3031                                     RAISE process_error;
3032                                 END IF;
3033 
3034                            END IF;
3035 
3036                            l_stmt_num := 122;
3037                            SELECT sum(nvl(actual_cost,0))
3038                            INTO   l_mpacd_cost
3039                            FROM   mtl_pac_actual_cost_details
3040                            WHERE  transaction_id = i_ae_txn_rec.transaction_id
3041                            AND    pac_period_id = i_ae_txn_rec.accounting_period_id
3042                            AND    cost_group_id = i_ae_txn_rec.cost_group_id;
3043 
3044                            l_stmt_num := 125;
3045 
3046                            l_txfr_var_value := l_io_rcv_value - (l_mpacd_cost + l_txfr_credit);
3047 
3048                            IF (sign(l_txfr_var_value) = 1) THEN
3049                               l_dr_flag := FALSE;
3050                            ELSIF (sign(l_txfr_var_value) = -1) THEN
3051                               l_dr_flag := TRUE;
3052                            ELSE
3053                               l_dr_flag := NULL;
3054                            END IF;
3055 
3056                            l_txfr_var_value := abs(l_txfr_var_value);
3057 
3058                            IF (l_dr_flag IS NOT NULL) THEN
3059 
3060                              IF (l_io_txfr_var_acct = -1) THEN
3061                                RAISE no_interorg_profit_acct_error;
3062                              END IF;
3063 
3064                              /* Transfer variance entry */
3065                              l_ae_line_rec.account := l_io_txfr_var_acct;
3066                              l_ae_line_rec.transaction_value := l_txfr_var_value * abs(i_ae_txn_rec.primary_quantity);
3067                              l_ae_line_rec.resource_id := NULL;
3068                              l_ae_line_rec.cost_element_id := NULL;
3069                              l_ae_line_rec.ae_line_type := 34;   -- Interorg profit in inventory
3070 
3071                              insert_account (i_ae_txn_rec,
3072                                              i_ae_curr_rec,
3073                                              l_dr_flag,
3074                                              l_ae_line_rec,
3075                                              l_ae_line_tbl,
3076                                              l_err_rec);
3077 
3078                              -- check error
3079                              IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3080                                 RAISE process_error;
3081                              END IF;
3082 
3083                            END IF;
3084 
3085                      ELSE /* MTA Entries to be used */ /* Not same LE/CT, INVCONV processed here */
3086                                                        /* or same LE/CT where there is no prior period cost */
3087                        l_stmt_num := 130;
3088 
3089                        -- Processing the process-discrete txns
3090                        -- INVCONV sikhanna
3091                        SELECT MOD(SUM(DECODE(process_enabled_flag,'Y',1,2)), 2)
3092                        INTO l_pd_txfr_ind
3093                        FROM MTL_PARAMETERS MP
3094                        WHERE MP.ORGANIZATION_ID = i_ae_txn_rec.xfer_organization_id
3095                        OR MP.ORGANIZATION_ID    = i_ae_txn_rec.organization_id;
3096 
3097 
3098                        l_stmt_num := 135;
3099 
3100                        IF (l_pd_txfr_ind = 0) THEN -- Discrete-Discrete X-fers
3101 
3102                             IF g_debug_flag = 'Y' THEN
3103                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3104                                                          ': Discrete-Discrete Transfer: Using MTA cost');
3105                             END IF;
3106 
3107                             BEGIN
3108                               SELECT nvl(SUM(ABS(NVL(base_transaction_value, 0))),0)
3109                               INTO l_perp_ship_value
3110                               FROM mtl_transaction_accounts mta
3111                               WHERE mta.transaction_id = i_ae_txn_rec.transaction_id
3112                               and mta.organization_id = i_ae_txn_rec.organization_id
3113                               and mta.accounting_line_type IN (1,2,14)
3114                               and mta.base_transaction_value < 0;
3115                             EXCEPTION
3116                               WHEN no_data_found THEN
3117                                  l_perp_ship_value := 0;
3118                             END;
3119 
3120                             IF g_debug_flag = 'Y' THEN
3121                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3122                                                          ': l_perp_ship_value = ' || l_perp_ship_value);
3123                             END IF;
3124 
3125                             l_stmt_num := 145;
3126 
3127                             SELECT nvl(transfer_percentage,0),nvl(transfer_cost,0)
3128                             INTO l_txfr_percent,l_txfr_cost
3129                             FROM mtl_material_transactions
3130                             WHERE transaction_id = i_ae_txn_rec.transaction_id;
3131 
3132                             IF (l_txfr_percent <> 0) THEN
3133                                 l_txfr_credit := (l_txfr_percent * (l_perp_ship_value)/ (100 * abs(i_ae_txn_rec.primary_quantity)));
3134                             elsif (l_txfr_cost <> 0) THEN
3135                                 l_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
3136                             ELSE
3137                                 l_txfr_credit := 0;
3138                             END IF;
3139 
3140                             IF g_debug_flag = 'Y' THEN
3141                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3142                                                          ': l_txfr_credit = ' || l_txfr_credit);
3143                             END IF;
3144 
3145                             l_stmt_num := 150;
3146 
3147                             l_io_rcv_value := l_txfr_credit + (l_perp_ship_value / abs(i_ae_txn_rec.primary_quantity));
3148 
3149                             IF g_debug_flag = 'Y' THEN
3150                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3151                                                          ': l_io_rcv_value = ' || l_io_rcv_value);
3152                             END IF;
3153 
3154                             l_ae_line_rec.transaction_value := l_io_rcv_value * abs(i_ae_txn_rec.primary_quantity);
3155                             l_ae_line_rec.account := l_io_rcv_acct;
3156                             l_ae_line_rec.resource_id := NULL;
3157                             l_ae_line_rec.cost_element_id := NULL;
3158                             l_ae_line_rec.ae_line_type := 10;  -- Receivables
3159 
3160                             l_stmt_num := 155;
3161 
3162                             /* Debit Receivables */
3163                             insert_account (i_ae_txn_rec,
3164                                             i_ae_curr_rec,
3165                                             l_dr_flag,
3166                                             l_ae_line_rec,
3167                                             l_ae_line_tbl,
3168                                             l_err_rec);
3169 
3170                             -- check error
3171                             IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3172                                 RAISE process_error;
3173                             END IF;
3174 
3175                             /* Create entries for transfer credit and transfer variance */
3176                             l_stmt_num := 160;
3177 
3178                             l_dr_flag := FALSE;
3179 
3180                             l_ae_line_rec.account := l_io_txfr_cr_acct;
3181                             l_ae_line_rec.transaction_value := l_txfr_credit * abs(i_ae_txn_rec.primary_quantity);
3182                             l_ae_line_rec.resource_id := NULL;
3183                             l_ae_line_rec.cost_element_id := NULL;
3184                             l_ae_line_rec.ae_line_type := 11;   -- Transfer Credit
3185 
3186                             IF (l_ae_line_rec.transaction_value <> 0) THEN
3187 
3188                                 insert_account (i_ae_txn_rec,
3189                                                 i_ae_curr_rec,
3190                                                 l_dr_flag,
3191                                                 l_ae_line_rec,
3192                                                 l_ae_line_tbl,
3193                                                 l_err_rec);
3194                                 -- check error
3195                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3196                                     RAISE process_error;
3197                                 END IF;
3198 
3199                             END IF;
3200 
3201                             l_stmt_num := 165;
3202 
3203                             SELECT sum(nvl(actual_cost,0))
3204                             INTO   l_mpacd_cost
3205                             FROM   mtl_pac_actual_cost_details
3206                             WHERE  transaction_id = i_ae_txn_rec.transaction_id
3207                             AND    pac_period_id = i_ae_txn_rec.accounting_period_id
3208                             AND    cost_group_id = i_ae_txn_rec.cost_group_id;
3209 
3210                             l_txfr_var_value := l_io_rcv_value - (l_mpacd_cost + l_txfr_credit);
3211 
3212                             IF (sign(l_txfr_var_value) = 1) THEN
3213                               l_dr_flag := FALSE;
3214                             ELSIF (sign(l_txfr_var_value) = -1) THEN
3215                               l_dr_flag := TRUE;
3216                             ELSE
3217                               l_dr_flag := NULL;
3218                             END IF;
3219 
3220                             l_txfr_var_value := abs(l_txfr_var_value);
3221 
3222                             IF g_debug_flag = 'Y' THEN
3223                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3224                                                          ': l_txfr_var_value = ' || l_txfr_var_value);
3225                             END IF;
3226 
3227                             l_stmt_num := 170;
3228 
3229                             IF (l_dr_flag IS NOT NULL) THEN
3230 
3231                               IF (l_io_txfr_var_acct = -1) THEN
3232                                 RAISE no_interorg_profit_acct_error;
3233                               END IF;
3234 
3235                               /* transfer variance entry */
3236                               l_ae_line_rec.account := l_io_txfr_var_acct;
3237                               l_ae_line_rec.transaction_value := l_txfr_var_value * abs(i_ae_txn_rec.primary_quantity);
3238                               l_ae_line_rec.resource_id := NULL;
3239                               l_ae_line_rec.cost_element_id := NULL;
3240                               l_ae_line_rec.ae_line_type := 34;  -- Interorg profit in inventory
3241 
3242                               l_stmt_num := 175;
3243 
3244                               insert_account (i_ae_txn_rec,
3245                                               i_ae_curr_rec,
3246                                               l_dr_flag,
3247                                               l_ae_line_rec,
3248                                               l_ae_line_tbl,
3249                                               l_err_rec);
3250 
3251                               -- check error
3252                               IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3253                                  RAISE process_error;
3254                               END IF;
3255 
3256                             END IF;
3257 
3258                        ELSE  /* Org is process enabled */
3259 
3260                             l_stmt_num := 180;
3261 
3262                             IF g_debug_flag = 'Y' THEN
3263                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3264                                                          ': Process-Discrete Transfer');
3265                             END IF;
3266 
3267                             SELECT mip.interorg_receivables_account,
3268                                    mmt.transportation_dist_account,
3269                                    nvl(mip.interorg_profit_account, -1),
3270                                    nvl(mmt.transfer_price,0),
3271                                    nvl(mmt.transportation_cost,0)
3272                             INTO   l_io_rcv_acct,
3273                                    l_io_freight_acct,
3274                                    l_io_txfr_var_acct,
3275                                    l_txfr_cost, -- transfer price
3276                                    l_trp_cost   -- transportation cost
3277                             FROM   mtl_interorg_parameters mip,mtl_material_transactions mmt
3278                             WHERE  mip.from_organization_id = i_ae_txn_rec.organization_id
3279                             AND    mip.to_organization_id = i_ae_txn_rec.xfer_organization_id
3280                             AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
3281 
3282                             IF (l_io_txfr_var_acct = -1) THEN
3283                               RAISE no_interorg_profit_acct_error;
3284                             END IF;
3285 
3286                             l_stmt_num := 185;
3287 
3288                             SELECT NVL(CPIC.item_cost,0)
3289                             INTO   l_pwac_cost
3290                             FROM   CST_PAC_ITEM_COSTS CPIC
3291                             WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
3292                             AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
3293                             AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
3294 
3295                             l_dr_flag := TRUE;
3296 
3297                             l_stmt_num := 190;
3298 
3299                             l_ae_line_rec.transaction_value := l_txfr_cost * abs(i_ae_txn_rec.primary_quantity); -- Receivables only at transfer price
3300                             l_ae_line_rec.account := l_io_rcv_acct;
3301                             l_ae_line_rec.resource_id := NULL;
3302                             l_ae_line_rec.cost_element_id := NULL;
3303                             l_ae_line_rec.ae_line_type := 10;  -- Receivables
3304 
3305                             l_stmt_num := 195;
3306 
3307                             insert_account (i_ae_txn_rec,
3308                                             i_ae_curr_rec,
3309                                             l_dr_flag,
3310                                             l_ae_line_rec,
3311                                             l_ae_line_tbl,
3312                                             l_err_rec);
3313 
3314                             -- check error
3315                             IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3316                                 RAISE process_error;
3317                             END IF;
3318 
3319                             l_profit_or_loss := l_pwac_cost - l_txfr_cost;
3320 
3321                             IF l_profit_or_loss <> 0 THEN
3322 
3323                                 IF l_profit_or_loss < 0 THEN
3324                                     l_dr_flag := not l_dr_flag; -- If -ve then Credit interorg profit
3325                                 END IF;
3326 
3327                                 l_stmt_num := 200;
3328 
3329                                 l_ae_line_rec.transaction_value := abs(l_profit_or_loss * i_ae_txn_rec.primary_quantity);
3330                                 l_ae_line_rec.account := l_io_txfr_var_acct;
3331                                 l_ae_line_rec.resource_id := NULL;
3332                                 l_ae_line_rec.cost_element_id := NULL;
3333                                 l_ae_line_rec.ae_line_type := 34;   -- interorg profit account
3334 
3335                                 l_stmt_num := 205;
3336 
3337                                 insert_account (i_ae_txn_rec,
3338                                                 i_ae_curr_rec,
3339                                                 l_dr_flag,
3340                                                 l_ae_line_rec,
3341                                                 l_ae_line_tbl,
3342                                                 l_err_rec);
3343 
3344                                 -- check error
3345                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3346                                     RAISE process_error;
3347                                 END IF;
3348 
3349                             END IF;
3350 
3351                      END IF;  /* (l_pd_txfr_ind = 0) */ -- End INVCONV check sikhanna
3352 
3353                     END IF; /* IF (l_use_prev_period_cost = 1) THEN*/
3354                   END IF; /* (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0) */ -- End of LE/CT check
3355 
3356                  ELSIF ((i_ae_txn_rec.cost_group_id <> l_shipping_cg_id) AND l_fob_point = 1) THEN /* This is the receiving CG for FOBS */
3357 
3358                        l_stmt_num := 210;
3359 
3360                        IF g_debug_flag = 'Y' THEN
3361                          fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3362                                                          ': Shipment txn of FOB Shipment - Receiving Cost Group');
3363                        END IF;
3364 
3365                        SELECT mip.interorg_payables_account,
3366                               mmt.transportation_dist_account
3367                        INTO   l_io_pay_acct,
3368                               l_io_freight_acct
3369                        FROM   mtl_interorg_parameters mip,
3370                               mtl_material_transactions mmt
3371                        WHERE  mip.from_organization_id = i_ae_txn_rec.organization_id
3372                        AND    mip.to_organization_id = i_ae_txn_rec.xfer_organization_id
3373                        AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
3374 
3375                        /* Debit Intransit or Expense if an expense item */
3376 
3377                        l_dr_flag := TRUE;
3378 
3379                        /* If the item is expense in the receiving CG, hit the expense account instead of Intransit */
3380                        IF (l_cg_exp_item = 1) THEN
3381                            /* Debit Expense */
3382                            CSTPAPBR.inventory_accounts (i_ae_txn_rec,
3383                                                        i_ae_curr_rec,
3384                                                        l_exp_flg, -- Exp Flag
3385                                                        l_exp_account, -- Exp Acct
3386                                                        l_dr_flag,
3387                                                        l_ae_line_tbl,
3388                                                        l_err_rec);
3389                        ELSE
3390                            /* Debit Inventory - Intransit */
3391                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
3392                                                     i_ae_curr_rec,
3393                                                     "FALSE",   --- Exp Flag
3394                                                     null,      --- Exp Acct
3395                                                     l_dr_flag,
3396                                                     l_ae_line_tbl,
3397                                                     l_err_rec,
3398                                                     1);
3399 
3400                        END IF;
3401 
3402                        -- check error
3403                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3404                            RAISE process_error;
3405                        END IF;
3406 
3407                        l_dr_flag := not l_dr_flag;
3408 
3409                        /* Credit freight account */
3410 
3411                            l_stmt_num := 215;
3412 
3413                            SELECT nvl(transportation_cost,0) * l_conv_rate /* convert from shipping org currency to receiving org currency */
3414                            INTO l_trp_cost
3415                            FROM mtl_material_transactions
3416                            WHERE transaction_id = i_ae_txn_rec.transaction_id;
3417 
3418                             l_ae_line_rec.transaction_value := l_trp_cost;
3419                             l_ae_line_rec.account := l_io_freight_acct;
3420                             l_ae_line_rec.resource_id := NULL;
3421                             l_ae_line_rec.cost_element_id := NULL;
3422                             l_ae_line_rec.ae_line_type := 12;
3423 
3424                            IF (l_ae_line_rec.transaction_value <> 0) THEN
3425 
3426                                 insert_account (i_ae_txn_rec,
3427                                                 i_ae_curr_rec,
3428                                                 l_dr_flag,
3429                                                 l_ae_line_rec,
3430                                                 l_ae_line_tbl,
3431                                                 l_err_rec);
3432 
3433                                 -- check error
3434                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3435                                     RAISE process_error;
3436                                 END IF;
3437 
3438                            END IF;
3439 
3440                          /* Absorb MOH if exists */
3441                          l_stmt_num := 217;
3442                          select count(transaction_id)
3443                          into l_mat_ovhd_exists
3444                          from mtl_pac_actual_cost_details
3445                          where transaction_id = i_ae_txn_rec.transaction_id
3446                          and pac_period_id = i_ae_txn_rec.accounting_period_id
3447                          and cost_group_id = i_ae_txn_rec.cost_group_id
3448                          and cost_element_id = 2
3449                          and level_type = 1;
3450 
3451                          l_stmt_num := 220;
3452 
3453                          l_mat_ovhd_cost := 0;
3454 
3455                          if (l_mat_ovhd_exists > 0) then
3456                             ovhd_accounts(i_ae_txn_rec,
3457                                           i_ae_curr_rec,
3458                                           l_dr_flag,
3459                                           l_ae_line_tbl,
3460                                           l_err_rec);
3461                            -- check error
3462                            if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
3463                              raise process_error;
3464                            end if;
3465 
3466                            l_stmt_num := 222;
3467 
3468                            select sum(nvl(actual_cost,0))
3469                            into   l_mat_ovhd_cost
3470                            from mtl_pac_cost_subelements
3471                            where transaction_id = i_ae_txn_rec.transaction_id
3472                            and cost_group_id = i_ae_txn_rec.cost_group_id
3473                            and pac_period_id = i_ae_txn_rec.accounting_period_id
3474                            and cost_type_id = i_ae_txn_rec.cost_type_id
3475                            and cost_element_id = 2;
3476 
3477                          end if;
3478 
3479                        /* Credit Payables */
3480                        l_stmt_num := 224;
3481 
3482                        SELECT sum(nvl(transaction_cost,0))
3483                        INTO l_mptcd_cost
3484                        FROM mtl_pac_txn_cost_details
3485                        WHERE transaction_id = i_ae_txn_rec.transaction_id
3486                        AND pac_period_id = i_ae_txn_rec.accounting_period_id
3487                        AND cost_group_id = i_ae_txn_rec.cost_group_id;
3488 
3489                        l_ae_line_rec.transaction_value := (l_mptcd_cost * abs(i_ae_txn_rec.primary_quantity)) - l_trp_cost;
3490 
3491                        l_ae_line_rec.account := l_io_pay_acct;
3492                        l_ae_line_rec.resource_id := NULL;
3493                        l_ae_line_rec.cost_element_id := NULL;
3494                        l_ae_line_rec.ae_line_type := 9;  -- Payables
3495 
3496                        insert_account (i_ae_txn_rec,
3497                                        i_ae_curr_rec,
3498                                        l_dr_flag,
3499                                        l_ae_line_rec,
3500                                        l_ae_line_tbl,
3501                                        l_err_rec);
3502 
3503                        -- check error
3504                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3505                            RAISE process_error;
3506                        END IF;
3507 
3508                  ELSIF (l_fob_point = 2) THEN /* FOBR - This can only be the shipping CG */
3509 
3510                         l_stmt_num := 225;
3511 
3512                         IF g_debug_flag = 'Y' THEN
3513                           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3514                                                          ': Shipment txn of FOB Receipt - Shipping Cost Group');
3515                         END IF;
3516 
3517                      IF (l_cg_exp_item <> 1) THEN
3518                         /* Debit Intransit and Credit OnHand */
3519 
3520                         l_dr_flag := TRUE;
3521 
3522                        -- Debit Intransit
3523                        -- ---------------
3524                        CSTPAPBR.inventory_accounts (i_ae_txn_rec,
3525                                                     i_ae_curr_rec,
3526                                                     "FALSE",   --- Exp Flag
3527                                                      null,      --- Exp Acct
3528                                                     l_dr_flag,
3529                                                     l_ae_line_tbl,
3530                                                     l_err_rec,
3531                                                     1);
3532 
3533                        -- check error
3534                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3535                            RAISE process_error;
3536                        END IF;
3537 
3538                        l_dr_flag := not l_dr_flag;
3539 
3540                        -- Credit On-hand (if expense, then use expense account)
3541                        -- -----------------------------------------------------
3542                        IF (l_exp_flg) THEN
3543                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
3544                                                        i_ae_curr_rec,
3545                                                        l_exp_flg, -- Exp Flag
3546                                                        l_exp_account, -- Exp Acct
3547                                                        l_dr_flag,
3548                                                        l_ae_line_tbl,
3549                                                        l_err_rec);
3550                        ELSE
3551                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
3552                                                        i_ae_curr_rec,
3553                                                        "FALSE", -- Exp Flag
3554                                                        null, -- Exp Acct
3555                                                        l_dr_flag,
3556                                                        l_ae_line_tbl,
3557                                                        l_err_rec);
3558                        END IF;
3559 
3560                        -- check error
3561                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3562                            RAISE process_error;
3563                        END IF;
3564                     END IF; /*  IF (l_cg_exp_item <> 1) THEN */
3565 
3566                  ELSIF (i_ae_txn_rec.txn_action_id = 3) THEN /* Direct Interorg */
3567 
3568                         l_stmt_num := 230;
3569 
3570                         IF g_debug_flag = 'Y' THEN
3571                           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3572                                                          ': Shipment txn of Direct Interorg - Shipping Cost Group');
3573                         END IF;
3574 
3575                         /* Credit Onhand inventory */
3576 
3577                         l_dr_flag := FALSE;
3578 
3579                      IF (l_cg_exp_item <> 1) THEN
3580                         -- Credit On-hand (if expense, then use expense account)
3581                         -- -----------------------------------------------------
3582                         IF (l_exp_flg) THEN
3583                            CSTPAPBR.inventory_accounts (i_ae_txn_rec,
3584                                                         i_ae_curr_rec,
3585                                                         l_exp_flg, -- Exp Flag
3586                                                         l_exp_account, -- Exp Acct
3587                                                         l_dr_flag,
3588                                                         l_ae_line_tbl,
3589                                                         l_err_rec);
3590                         ELSE
3591                            CSTPAPBR.inventory_accounts (i_ae_txn_rec,
3592                                                         i_ae_curr_rec,
3593                                                         "FALSE", -- Exp Flag
3594                                                         null, -- Exp Acct
3595                                                         l_dr_flag,
3596                                                         l_ae_line_tbl,
3597                                                         l_err_rec);
3598                         END IF;
3599 
3600                         -- check error
3601                         IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3602                             RAISE process_error;
3603                         END IF;
3604 
3605                      END IF; /*  IF (l_cg_exp_item <> 1) THEN */
3606 
3607                         l_dr_flag := not l_dr_flag;
3608 
3609                         /* Create entries for Receivables, Transfer Credit, Transportation, etc. */
3610 
3611                         l_stmt_num := 235;
3612 
3613                         /* Get relevant accounts */
3614                         SELECT mip.interorg_receivables_account,
3615                                mmt.transportation_dist_account,
3616                                mip.interorg_transfer_cr_account,
3617                                nvl(mip.interorg_profit_account, -1)
3618                         INTO   l_io_rcv_acct,
3619                                l_io_freight_acct,
3620                                l_io_txfr_cr_acct,
3621                                l_io_txfr_var_acct
3622                         FROM   mtl_interorg_parameters mip,mtl_material_transactions mmt
3623                         WHERE  mip.from_organization_id = i_ae_txn_rec.organization_id
3624                         AND    mip.to_organization_id = i_ae_txn_rec.xfer_organization_id
3625                         AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
3626 
3627                         IF (l_io_txfr_var_acct = -1) THEN
3628                               RAISE no_interorg_profit_acct_error;
3629                         END IF;
3630 
3631                         l_ae_line_rec.account := l_io_rcv_acct;
3632 
3633                         l_stmt_num := 240;
3634 
3635                         SELECT TRANSFER_COST_FLAG
3636                         INTO   l_transfer_cost_flag
3637                         FROM   CST_LE_COST_TYPES
3638                         WHERE  LEGAL_ENTITY = i_ae_txn_rec.legal_entity_id
3639                         AND    COST_TYPE_ID = i_ae_txn_rec.cost_type_id;
3640 
3641                         l_stmt_num := 245;
3642 
3643                         /* Prior period id */
3644                         SELECT nvl(max(cpp.pac_period_id), -1)
3645                         INTO   l_prev_period_id
3646                         FROM   cst_pac_periods cpp
3647                         WHERE  cpp.cost_type_id = i_ae_txn_rec.cost_type_id
3648                         AND    cpp.legal_entity = i_ae_txn_rec.legal_entity_id
3649                         AND    cpp.pac_period_id < i_ae_txn_rec.accounting_period_id;
3650 
3651                         l_stmt_num := 250;
3652 
3653                         SELECT NVL(MAX(cost_group_id),-1)
3654                         INTO   l_txfr_cost_group_id
3655                         FROM   cst_cost_group_assignments
3656                         WHERE  organization_id = i_ae_txn_rec.xfer_organization_id;
3657 
3658                         l_stmt_num := 255;
3659 
3660                         BEGIN
3661                             SELECT legal_entity
3662                             INTO l_txfr_legal_entity
3663                             FROM cst_cost_groups
3664                             WHERE cost_group_id = l_txfr_cost_group_id;
3665 
3666                             EXCEPTION
3667                             WHEN NO_DATA_FOUND THEN
3668                                l_txfr_legal_entity := NULL;
3669                         END;
3670 
3671                         l_stmt_num := 260;
3672 
3673                         /* Check for the same LE/CT combination */
3674                         SELECT count(*)
3675                         INTO   l_same_le_ct
3676                         FROM   cst_le_cost_types
3677                         WHERE  legal_entity = l_txfr_legal_entity
3678                         AND    cost_type_id = i_ae_txn_rec.cost_type_id;
3679 
3680                         l_stmt_num := 265;
3681 
3682                         IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0 AND l_transfer_cost_flag = 'Y') THEN
3683 
3684                                /* PACP used: Use CPIC cost */
3685                                l_stmt_num := 270;
3686 
3687                                 SELECT NVL(CPIC.item_cost,0)
3688                                 INTO   l_pacp_pwac_cost
3689                                 FROM   CST_PAC_ITEM_COSTS CPIC
3690                                 WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
3691                                 AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
3692                                 AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
3693 
3694                                 IF g_debug_flag = 'Y' THEN
3695                                    fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3696                                                          ': Using PAC Absorption Cost: ' || l_pacp_pwac_cost);
3697                                 END IF;
3698 
3699                                 l_stmt_num := 272;
3700                                 SELECT nvl(transfer_percentage,0),
3701                                        nvl(transfer_cost,0),
3702                                        nvl(transportation_cost,0)
3703                                 INTO   l_txfr_percent,
3704                                        l_txfr_cost,
3705                                        l_trp_cost
3706                                 FROM   mtl_material_transactions
3707                                 WHERE  transaction_id = i_ae_txn_rec.transaction_id;
3708 
3709                                 IF (l_txfr_percent <> 0) THEN
3710                                     l_txfr_credit := (l_txfr_percent * l_pacp_pwac_cost / 100);
3711                                 ELSIF (l_txfr_cost <> 0) THEN
3712                                     l_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
3713                                 ELSE
3714                                     l_txfr_credit := 0;
3715                                 END IF;
3716 
3717                                 l_stmt_num := 274;
3718 
3719                                 /* Debit Receivables */
3720                                 l_io_rcv_value := (l_pacp_pwac_cost +
3721                                                   (((l_trp_cost / abs(i_ae_txn_rec.primary_quantity))+ l_txfr_credit)));
3722 
3723                                 l_ae_line_rec.transaction_value := l_io_rcv_value * abs(i_ae_txn_rec.primary_quantity);
3724                                 l_ae_line_rec.resource_id := NULL;
3725                                 l_ae_line_rec.cost_element_id := NULL;
3726                                 l_ae_line_rec.ae_line_type := 10;    -- Receivables
3727 
3728                                 insert_account (i_ae_txn_rec,
3729                                                 i_ae_curr_rec,
3730                                                 l_dr_flag,
3731                                                 l_ae_line_rec,
3732                                                 l_ae_line_tbl,
3733                                                 l_err_rec);
3734 
3735                                 -- check error
3736                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3737                                     RAISE process_error;
3738                                 END IF;
3739 
3740 
3741                                 l_stmt_num := 276;
3742                                 /* Create entries for transfer credit and freight */
3743 
3744                                 l_dr_flag := FALSE;
3745 
3746                                 l_ae_line_rec.transaction_value := l_trp_cost;
3747                                 l_ae_line_rec.account := l_io_freight_acct;
3748                                 l_ae_line_rec.resource_id := NULL;
3749                                 l_ae_line_rec.cost_element_id := NULL;
3750                                 l_ae_line_rec.ae_line_type := 12;  -- freight account
3751 
3752                                 /* Freight account entry */
3753                                 IF (l_ae_line_rec.transaction_value <> 0) THEN
3754 
3755                                     insert_account (i_ae_txn_rec,
3756                                                     i_ae_curr_rec,
3757                                                     l_dr_flag,
3758                                                     l_ae_line_rec,
3759                                                     l_ae_line_tbl,
3760                                                     l_err_rec);
3761 
3762                                     -- check error
3763                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3764                                         RAISE process_error;
3765                                     END IF;
3766 
3767                                 END IF;
3768 
3769                                 l_ae_line_rec.account := l_io_txfr_cr_acct;
3770                                 l_ae_line_rec.transaction_value := (l_txfr_credit * abs(i_ae_txn_rec.primary_quantity));
3771                                 l_ae_line_rec.resource_id := NULL;
3772                                 l_ae_line_rec.cost_element_id := NULL;
3773                                 l_ae_line_rec.ae_line_type := 11;  -- transfer credit
3774 
3775                                 l_stmt_num := 278;
3776 
3777                                 /* Transfer credit entry */
3778                                 IF (l_ae_line_rec.transaction_value <> 0) THEN
3779 
3780                                     insert_account (i_ae_txn_rec,
3781                                                     i_ae_curr_rec,
3782                                                     l_dr_flag,
3783                                                     l_ae_line_rec,
3784                                                     l_ae_line_tbl,
3785                                                     l_err_rec);
3786 
3787                                     -- check error
3788                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3789                                         RAISE process_error;
3790                                     END IF;
3791 
3792                                 END IF;
3793 
3794                        ELSE /*  IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0 AND l_transfer_cost_flag = 'Y') THEN */
3795 
3796                          IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0 AND l_prev_period_id <> -1) THEN
3797 
3798                            l_stmt_num := 280;
3799                            l_use_prev_period_cost := 1;
3800 
3801                            /* Get prior period PWAC Cost */
3802                                 BEGIN
3803                                     SELECT nvl(CPIC.item_cost,0)
3804                                       INTO l_prev_period_pwac_cost
3805                                       FROM CST_PAC_ITEM_COSTS CPIC
3806                                      WHERE CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
3807                                        AND CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
3808                                        AND CPIC.PAC_PERIOD_ID     = l_prev_period_id;
3809                                 EXCEPTION
3810                                 when no_data_found then
3811                                     /* Use perpetual cost if prior period cost is not available */
3812                                     l_use_prev_period_cost := 0;
3813                                 END;
3814                          ELSE
3815                          /* Use perpetual cost if prior period cost is not available or if the cost groups are not in the same LE/CT. */
3816                            l_use_prev_period_cost := 0;
3817                          END IF;
3818 
3819                          IF (l_use_prev_period_cost = 1) THEN
3820                                 l_stmt_num := 282;
3821 
3822                                 IF g_debug_flag = 'Y' THEN
3823                                    fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3824                                                                         ': Using prior period PWAC cost: ' || l_prev_period_pwac_cost);
3825                                 END IF;
3826 
3827                                 l_stmt_num := 285;
3828 
3829                                 SELECT nvl(transfer_percentage,0),
3830                                        nvl(transfer_cost,0),
3831                                        nvl(transportation_cost,0)
3832                                 INTO   l_txfr_percent,
3833                                        l_txfr_cost,
3834                                        l_trp_cost
3835                                 FROM   mtl_material_transactions
3836                                 WHERE  transaction_id = i_ae_txn_rec.transaction_id;
3837 
3838                                 IF (l_txfr_percent <> 0) THEN
3839                                     l_txfr_credit := (l_txfr_percent * l_prev_period_pwac_cost / 100);
3840                                 ELSIF (l_txfr_cost <> 0) THEN
3841                                     l_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
3842                                 ELSE
3843                                     l_txfr_credit := 0;
3844                                 END IF;
3845 
3846                                 l_stmt_num := 290;
3847 
3848                                 /* Debit Receivables */
3849                                 l_io_rcv_value := (l_prev_period_pwac_cost +
3850                                                   (((l_trp_cost / abs(i_ae_txn_rec.primary_quantity))+ l_txfr_credit)));
3851 
3852                                 l_ae_line_rec.transaction_value := l_io_rcv_value * abs(i_ae_txn_rec.primary_quantity);
3853                                 l_ae_line_rec.resource_id := NULL;
3854                                 l_ae_line_rec.cost_element_id := NULL;
3855                                 l_ae_line_rec.ae_line_type := 10;   -- Receivables
3856 
3857                                 l_stmt_num := 295;
3858 
3859                                 insert_account (i_ae_txn_rec,
3860                                                 i_ae_curr_rec,
3861                                                 l_dr_flag,
3862                                                 l_ae_line_rec,
3863                                                 l_ae_line_tbl,
3864                                                 l_err_rec);
3865 
3866                                 -- check error
3867                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3868                                     RAISE process_error;
3869                                 END IF;
3870 
3871                                 l_stmt_num := 300;
3872 
3873                                 /* Create entries for transfer credit,freight and transfer variance */
3874 
3875                                 l_dr_flag := FALSE;
3876 
3877                                 l_ae_line_rec.transaction_value := l_trp_cost;
3878                                 l_ae_line_rec.account := l_io_freight_acct;
3879                                 l_ae_line_rec.resource_id := NULL;
3880                                 l_ae_line_rec.cost_element_id := NULL;
3881                                 l_ae_line_rec.ae_line_type := 12;  -- freight account
3882 
3883                                 /* Freight account entry */
3884                                 IF (l_ae_line_rec.transaction_value <> 0) THEN
3885 
3886                                     insert_account (i_ae_txn_rec,
3887                                                     i_ae_curr_rec,
3888                                                     l_dr_flag,
3889                                                     l_ae_line_rec,
3890                                                     l_ae_line_tbl,
3891                                                     l_err_rec);
3892 
3893                                     -- check error
3894                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3895                                         RAISE process_error;
3896                                     END IF;
3897 
3898                                     l_stmt_num := 305;
3899 
3900                                 END IF;
3901 
3902                                 l_ae_line_rec.account := l_io_txfr_cr_acct;
3903                                 l_ae_line_rec.transaction_value := (l_txfr_credit * abs(i_ae_txn_rec.primary_quantity));
3904                                 l_ae_line_rec.resource_id := NULL;
3905                                 l_ae_line_rec.cost_element_id := NULL;
3906                                 l_ae_line_rec.ae_line_type := 11;  -- transfer credit
3907 
3908                                 l_stmt_num := 310;
3909 
3910                                 /* Transfer credit entry */
3911                                 IF (l_ae_line_rec.transaction_value <> 0) THEN
3912 
3913                                     insert_account (i_ae_txn_rec,
3914                                                     i_ae_curr_rec,
3915                                                     l_dr_flag,
3916                                                     l_ae_line_rec,
3917                                                     l_ae_line_tbl,
3918                                                     l_err_rec);
3919 
3920                                     -- check error
3921                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3922                                         RAISE process_error;
3923                                     END IF;
3924 
3925                                 END IF;
3926 
3927                                 l_stmt_num := 315;
3928 
3929                                 SELECT sum(nvl(actual_cost,0))
3930                                 INTO   l_mpacd_cost
3931                                 FROM   mtl_pac_actual_cost_details
3932                                 WHERE  transaction_id = i_ae_txn_rec.transaction_id
3933                                 AND    pac_period_id = i_ae_txn_rec.accounting_period_id
3934                                 AND    cost_group_id = i_ae_txn_rec.cost_group_id;
3935 
3936                                 l_txfr_var_value := l_io_rcv_value - (l_mpacd_cost + l_txfr_credit + l_trp_cost/abs(i_ae_txn_rec.primary_quantity));
3937 
3938                                 IF (sign(l_txfr_var_value) = 1) THEN
3939                                   l_dr_flag := FALSE;
3940                                 ELSIF (sign(l_txfr_var_value) = -1) THEN
3941                                   l_dr_flag := TRUE;
3942                                 ELSE
3943                                   l_dr_flag := NULL;
3944                                 END IF;
3945 
3946                                 l_txfr_var_value := abs(l_txfr_var_value);
3947 
3948                                 IF (l_dr_flag IS NOT NULL) THEN
3949 
3950                                     IF (l_io_txfr_var_acct = -1) THEN
3951                                       RAISE no_interorg_profit_acct_error;
3952                                     END IF;
3953 
3954                                     /* Transfer variance entry */
3955                                     l_ae_line_rec.account := l_io_txfr_var_acct;
3956                                     l_ae_line_rec.transaction_value := l_txfr_var_value  * abs(i_ae_txn_rec.primary_quantity);
3957                                     l_ae_line_rec.resource_id := NULL;
3958                                     l_ae_line_rec.cost_element_id := NULL;
3959                                     l_ae_line_rec.ae_line_type := 34;   -- Interorg profit in inventory
3960 
3961                                     l_stmt_num := 320;
3962 
3963                                     insert_account (i_ae_txn_rec,
3964                                                     i_ae_curr_rec,
3965                                                     l_dr_flag,
3966                                                     l_ae_line_rec,
3967                                                     l_ae_line_tbl,
3968                                                     l_err_rec);
3969 
3970                                     -- check error
3971                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
3972                                         RAISE process_error;
3973                                     END IF;
3974 
3975                                 END IF; /* (l_dr_flag IS NOT NULL) */
3976 
3977                         ELSE /* IF (l_use_prev_period_cost = 1) THEN */
3978                            /* Different LE/CT, or No Prior Period - Perpetual side:MTA Entries to be used */
3979 
3980                           -- Processing the process-discrete txns
3981                           -- INVCONV sikhanna
3982                           l_stmt_num := 322;
3983                           SELECT MOD(SUM(DECODE(process_enabled_flag,'Y',1,2)), 2)
3984                           INTO l_pd_txfr_ind
3985                           FROM MTL_PARAMETERS MP
3986                           WHERE MP.ORGANIZATION_ID = i_ae_txn_rec.xfer_organization_id
3987                           OR MP.ORGANIZATION_ID    = i_ae_txn_rec.organization_id;
3988 
3989                           l_stmt_num := 324;
3990 
3991                           IF (l_pd_txfr_ind = 1) THEN -- Process-Discrete X-fers
3992 
3993                             IF g_debug_flag = 'Y' THEN
3994                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
3995                                                          ': Process-Discrete X-fers: ');
3996                             END IF;
3997 
3998                             SELECT mip.interorg_receivables_account,
3999                                    mmt.transportation_dist_account,
4000                                    mip.interorg_transfer_cr_account,
4001                                    nvl(mip.interorg_profit_account, -1)
4002                             INTO   l_io_rcv_acct,
4003                                    l_io_freight_acct,
4004                                    l_io_txfr_cr_acct,
4005                                    l_io_txfr_var_acct
4006                             FROM   mtl_interorg_parameters mip,mtl_material_transactions mmt
4007                             WHERE  mip.from_organization_id = i_ae_txn_rec.organization_id -- INVCONV condition is opposite
4008                             AND    mip.to_organization_id = i_ae_txn_rec.xfer_organization_id
4009                             AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
4010 
4011                             IF (l_io_txfr_var_acct = -1) THEN
4012                               RAISE no_interorg_profit_acct_error;
4013                             END IF;
4014 
4015                             l_stmt_num := 326;
4016 
4017                             SELECT nvl(transfer_price,0),
4018                                    nvl(transportation_cost,0)
4019                             INTO   l_txfr_cost, -- transfer price
4020                                    l_trp_cost   -- transportation cost
4021                             FROM   mtl_material_transactions mmt
4022                             WHERE  mmt.transaction_id = i_ae_txn_rec.transaction_id;
4023 
4024                             l_stmt_num := 328;
4025                             /* create debit entries */
4026 
4027                             l_dr_flag := true;
4028 
4029                             /* Debit Receivables at Transfer Price */
4030                             l_ae_line_rec.transaction_value := (l_txfr_cost  * abs(i_ae_txn_rec.primary_quantity));
4031                             l_ae_line_rec.account := l_io_rcv_acct;
4032                             l_ae_line_rec.resource_id := NULL;
4033                             l_ae_line_rec.cost_element_id := NULL;
4034                             l_ae_line_rec.ae_line_type := 10;  -- Receivables
4035 
4036                             l_stmt_num := 330;
4037 
4038                             insert_account (i_ae_txn_rec,
4039                                             i_ae_curr_rec,
4040                                             l_dr_flag,
4041                                             l_ae_line_rec,
4042                                             l_ae_line_tbl,
4043                                             l_err_rec);
4044                             -- check error
4045                             IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4046                              RAISE process_error;
4047                             END IF;
4048 
4049                             l_stmt_num := 332;
4050 
4051                             /* create credit entries */
4052 
4053                             l_dr_flag := NOT l_dr_flag;
4054 
4055                             l_ae_line_rec.transaction_value := l_trp_cost;
4056                             l_ae_line_rec.account := l_io_freight_acct;
4057                             l_ae_line_rec.resource_id := NULL;
4058                             l_ae_line_rec.cost_element_id := NULL;
4059                             l_ae_line_rec.ae_line_type := 12;   -- freight account
4060 
4061                             l_stmt_num := 334;
4062 
4063                             /* freight account entry */
4064                             IF (l_ae_line_rec.transaction_value <> 0) THEN
4065 
4066                                 insert_account (i_ae_txn_rec,
4067                                                 i_ae_curr_rec,
4068                                                 l_dr_flag,
4069                                                 l_ae_line_rec,
4070                                                 l_ae_line_tbl,
4071                                                 l_err_rec);
4072                                 -- check error
4073                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4074                                  RAISE process_error;
4075                                 END IF;
4076 
4077                             END IF;
4078 
4079                             l_stmt_num := 336;
4080 
4081                             SELECT NVL(CPIC.item_cost,0)
4082                             INTO   l_pwac_cost
4083                             FROM   CST_PAC_ITEM_COSTS CPIC
4084                             WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
4085                             AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
4086                             AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
4087 
4088                             l_profit_or_loss := l_txfr_cost - (l_pwac_cost + (l_trp_cost / abs(i_ae_txn_rec.primary_quantity)));
4089 
4090                             IF l_profit_or_loss <> 0 THEN
4091 
4092                                 IF l_profit_or_loss < 0 THEN -- Making a loss
4093                                     l_dr_flag := not l_dr_flag; -- Debit interorg profit
4094                                 END IF;
4095 
4096                                 l_stmt_num := 338;
4097 
4098                                 l_ae_line_rec.transaction_value := abs(l_profit_or_loss) * abs(i_ae_txn_rec.primary_quantity);
4099                                 l_ae_line_rec.account := l_io_txfr_var_acct;
4100                                 l_ae_line_rec.resource_id := NULL;
4101                                 l_ae_line_rec.cost_element_id := NULL;
4102                                 l_ae_line_rec.ae_line_type := 34;   -- interorg profit account
4103 
4104                                 insert_account (i_ae_txn_rec,
4105                                                 i_ae_curr_rec,
4106                                                 l_dr_flag,
4107                                                 l_ae_line_rec,
4108                                                 l_ae_line_tbl,
4109                                                 l_err_rec);
4110                                 -- check error
4111                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4112                                  RAISE process_error;
4113                                 END IF;
4114 
4115                               END IF;
4116 
4117 
4118                           ELSE /* Discrete-Discrete X-Fers */
4119 
4120 
4121                             l_stmt_num := 340;
4122                             IF g_debug_flag = 'Y' THEN
4123                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4124                                                          ': Using MTA cost: ');
4125                             END IF;
4126 
4127 
4128                             BEGIN
4129                              SELECT nvl(SUM(ABS(NVL(base_transaction_value, 0))),0)
4130                              INTO l_perp_ship_value
4131                              FROM mtl_transaction_accounts mta
4132                              WHERE mta.transaction_id = i_ae_txn_rec.transaction_id
4133                              and mta.organization_id = i_ae_txn_rec.organization_id
4134                              and mta.accounting_line_type IN (1,2,14)
4135                              and mta.base_transaction_value < 0;
4136                             EXCEPTION
4137                              WHEN no_data_found THEN
4138                                  l_perp_ship_value := 0;
4139                             END;
4140 
4141                             l_stmt_num := 342;
4142 
4143                             SELECT nvl(transfer_percentage,0),nvl(transfer_cost,0),nvl(transportation_cost,0)
4144                             INTO l_txfr_percent,l_txfr_cost,l_trp_cost
4145                             FROM mtl_material_transactions
4146                             WHERE transaction_id = i_ae_txn_rec.transaction_id;
4147 
4148                             IF (l_txfr_percent <> 0) THEN
4149                                 l_txfr_credit := (l_txfr_percent * l_perp_ship_value) / (100 * abs(i_ae_txn_rec.primary_quantity));
4150                             ELSIF (l_txfr_cost <> 0) THEN
4151                                 l_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
4152                             ELSE
4153                                 l_txfr_credit := 0;
4154                             END IF;
4155 
4156                             l_io_rcv_value := l_txfr_credit + (l_perp_ship_value + l_trp_cost)/abs(i_ae_txn_rec.primary_quantity);
4157 --((l_other_ele_costs / abs(i_ae_txn_rec.primary_quantity)) +
4158 --                                               ((((l_mat_ovhd + l_trp_cost)/ abs(i_ae_txn_rec.primary_quantity)) + l_txfr_credit)));
4159 
4160                             l_ae_line_rec.transaction_value := l_io_rcv_value  * abs(i_ae_txn_rec.primary_quantity);
4161                             l_ae_line_rec.account := l_io_rcv_acct;
4162                             l_ae_line_rec.resource_id := NULL;
4163                             l_ae_line_rec.cost_element_id := NULL;
4164                             l_ae_line_rec.ae_line_type := 10;  -- Receivables
4165 
4166                             l_stmt_num := 344;
4167 
4168                             /* Debit receivables */
4169                             insert_account (i_ae_txn_rec,
4170                                             i_ae_curr_rec,
4171                                             l_dr_flag,
4172                                             l_ae_line_rec,
4173                                             l_ae_line_tbl,
4174                                             l_err_rec);
4175 
4176                             -- check error
4177                             IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4178                                 RAISE process_error;
4179                             END IF;
4180 
4181                             /* Create entries for transfer credit,freight and transfer variance */
4182 
4183                             l_dr_flag := FALSE;
4184 
4185                             l_ae_line_rec.transaction_value := l_trp_cost;
4186                             l_ae_line_rec.account := l_io_freight_acct;
4187                             l_ae_line_rec.resource_id := NULL;
4188                             l_ae_line_rec.cost_element_id := NULL;
4189                             l_ae_line_rec.ae_line_type := 12;   -- freight account
4190 
4191                             l_stmt_num := 345;
4192 
4193                             /* Freight account entry */
4194                             IF (l_ae_line_rec.transaction_value <> 0) THEN
4195 
4196                                  insert_account (i_ae_txn_rec,
4197                                                  i_ae_curr_rec,
4198                                                  l_dr_flag,
4199                                                  l_ae_line_rec,
4200                                                  l_ae_line_tbl,
4201                                                  l_err_rec);
4202 
4203                                 -- check error
4204                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4205                                     RAISE process_error;
4206                                 END IF;
4207 
4208                             END IF;
4209 
4210                             l_ae_line_rec.account := l_io_txfr_cr_acct;
4211                             l_ae_line_rec.transaction_value := (l_txfr_credit * abs(i_ae_txn_rec.primary_quantity));
4212                             l_ae_line_rec.resource_id := NULL;
4213                             l_ae_line_rec.cost_element_id := NULL;
4214                             l_ae_line_rec.ae_line_type := 11;   -- transfer credit
4215 
4216                             l_stmt_num := 350;
4217 
4218                             /* transfer credit entry */
4219                             IF (l_ae_line_rec.transaction_value <> 0) THEN
4220 
4221                                  insert_account (i_ae_txn_rec,
4222                                                  i_ae_curr_rec,
4223                                                  l_dr_flag,
4224                                                  l_ae_line_rec,
4225                                                  l_ae_line_tbl,
4226                                                  l_err_rec);
4227                                 -- check error
4228                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4229                                     RAISE process_error;
4230                                 END IF;
4231 
4232                             END IF;
4233 
4234                             l_stmt_num := 355;
4235                             SELECT sum(nvl(actual_cost,0))
4236                             INTO   l_mpacd_cost
4237                             FROM   mtl_pac_actual_cost_details
4238                             WHERE  transaction_id = i_ae_txn_rec.transaction_id
4239                             AND    pac_period_id = i_ae_txn_rec.accounting_period_id
4240                             AND    cost_group_id = i_ae_txn_rec.cost_group_id;
4241 
4242                             /* Transfer variance */
4243 
4244                             l_txfr_var_value := l_io_rcv_value - (l_mpacd_cost + l_txfr_credit + l_trp_cost/abs(i_ae_txn_rec.primary_quantity));
4245 
4246                             IF (sign(l_txfr_var_value) = 1) THEN
4247                               l_dr_flag := FALSE;
4248                             ELSIF (sign(l_txfr_var_value) = -1) THEN
4249                               l_dr_flag := TRUE;
4250                             ELSE
4251                               l_dr_flag := NULL;
4252                             END IF;
4253 
4254                             l_txfr_var_value := abs(l_txfr_var_value);
4255 
4256                             IF (l_dr_flag IS NOT NULL) THEN
4257 
4258                                  IF (l_io_txfr_var_acct = -1) THEN
4259                                    RAISE no_interorg_profit_acct_error;
4260                                  END IF;
4261 
4262                                  /* transfer variance entry */
4263                                  l_ae_line_rec.account := l_io_txfr_var_acct;
4264                                  l_ae_line_rec.transaction_value := l_txfr_var_value  * abs(i_ae_txn_rec.primary_quantity);
4265                                  l_ae_line_rec.resource_id := NULL;
4266                                  l_ae_line_rec.cost_element_id := NULL;
4267                                  l_ae_line_rec.ae_line_type := 34; -- Interorg profit in inventory
4268 
4269                                  l_stmt_num := 360;
4270 
4271                                  insert_account (i_ae_txn_rec,
4272                                                  i_ae_curr_rec,
4273                                                  l_dr_flag,
4274                                                  l_ae_line_rec,
4275                                                  l_ae_line_tbl,
4276                                                  l_err_rec);
4277                                 -- check error
4278                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4279                                     RAISE process_error;
4280                                 END IF;
4281 
4282                                  l_stmt_num := 365;
4283 
4284                             END IF;
4285 
4286                           END IF; /* (l_pd_txfr_ind = 1); Process-Discrete X-fers */
4287 
4288                         END IF; /* IF (l_use_prev_period_cost = 1) */
4289 
4290                       END IF; /*  IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0 AND l_transfer_cost_flag = 'Y') THEN */
4291 
4292                  END IF; /*All cases of ordinary interorg shipments handled */
4293 
4294             END IF; /* End for interorg shipments */
4295 
4296         /* Else - Receipt transactions */
4297         ELSIF ( i_ae_txn_rec.txn_action_id in (12,22) OR (i_ae_txn_rec.txn_action_id = 3 AND i_ae_txn_rec.primary_quantity > 0)) THEN
4298 
4299            l_stmt_num := 370;
4300 
4301            BEGIN
4302                /* Change the Query for the performance. Bug 4586534 */
4303                SELECT nvl(fnd_profile.value('CST_TRANSFER_PRICING_OPTION'), 0)
4304                INTO   l_tprice_option
4305                FROM   mtl_intercompany_parameters MIP
4306                WHERE  fnd_profile.value('INV_INTERCOMPANY_INVOICE_INTERNAL_ORDER') = 1
4307                AND  ( select  count(1)
4308                             FROM hr_organization_information HOI
4309                             where   HOI.organization_id = decode(i_ae_txn_rec.txn_action_id, 21,
4310                                                                  i_ae_txn_rec.organization_id,
4311                                                                  i_ae_txn_rec.xfer_organization_id)
4312                             AND    HOI.org_information_context = 'Accounting Information'
4313                             AND    MIP.ship_organization_id = to_number(HOI.org_information3)
4314                             AND    ROWNUM < 2) > 0
4315 
4316                AND  (select  count(1)
4317                            FROM hr_organization_information HOI2
4318                            WHERE HOI2.organization_id = decode(i_ae_txn_rec.txn_action_id, 21,
4319                                                                i_ae_txn_rec.xfer_organization_id,
4320                                                                i_ae_txn_rec.organization_id)
4321                            AND    HOI2.org_information_context = 'Accounting Information'
4322                            AND    MIP.sell_organization_id = to_number(HOI2.org_information3)
4323                            AND    ROWNUM < 2) > 0
4324                AND    MIP.flow_type = 1;
4325 
4326                EXCEPTION
4327                WHEN NO_DATA_FOUND THEN
4328                l_tprice_option := -1; /* Chenged it to be -1, will toggle to 0 later */
4329            END;
4330 
4331             -- Processing the process-discrete txns
4332             -- INVCONV sikhanna START
4333             l_stmt_num := 372;
4334             SELECT MOD(SUM(DECODE(process_enabled_flag,'Y',1,2)), 2)
4335             INTO l_pd_txfr_ind
4336             FROM MTL_PARAMETERS MP
4337             WHERE MP.ORGANIZATION_ID = i_ae_txn_rec.xfer_organization_id
4338             OR MP.ORGANIZATION_ID    = i_ae_txn_rec.organization_id;
4339 
4340             /* Process-Discrete X-fers set the profile to 2 if ICR relations setup and in diff OU */
4341             /* l_tprice_option=-1 only when ICR is not set up and exception is thrown, then continue as normal */
4342             IF (l_pd_txfr_ind=1 and l_tprice_option <> -1) THEN -- OPM ST BUG 5351896
4343 
4344                l_stmt_num := 374;
4345 
4346                l_tprice_option := 2; /* Make it 2 to ignore the CST Transfer Price profile */
4347 
4348                IF g_debug_flag = 'Y' THEN
4349                   fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ' ICR Set up:process-discrete xfer');
4350                END IF;
4351 
4352             END IF;
4353 
4354             IF l_tprice_option = -1 THEN
4355                l_stmt_num := 376;
4356                l_tprice_option := 0; /* Toggle it to 0 as 0 is used later */
4357             END IF;
4358             -- INVCONV sikhanna END
4359 
4360             l_stmt_num := 378;
4361 
4362            /* Process Internal Orders */
4363 
4364            IF ((i_ae_txn_rec.txn_src_type_id IN (7,8)) AND (l_tprice_option IN (1,2))) THEN
4365 
4366               IF (l_fob_point = 2) THEN
4367 
4368                   IF (i_ae_txn_rec.cost_group_id = l_shipping_cg_id) THEN /* This is the sending CG */
4369 
4370                        l_stmt_num := 380;
4371 
4372                        IF g_debug_flag = 'Y' THEN
4373                           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4374                                                    ': Receipt txn of Internal Order FOB Receipt - Sending Cost Group');
4375                        END IF;
4376 
4377                      IF (l_cg_exp_item <> 1) THEN
4378 
4379                        /* Debit COGS and credit Intransit */
4380 
4381                        l_stmt_num := 382;
4382                        -- Bug 5573993 - Derive the COGS account to break dependency on perpetual
4383                        -- code as if the trxn is not costed, mmt has incorrect account stamped
4384 
4385                        IF g_debug_flag = 'Y' THEN
4386                           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4387                                                                     'Deriving COGS account');
4388                        END IF;
4389 
4390                        l_stmt_num := 61;
4391 
4392                        l_cogs_account := get_intercompany_account (i_ae_txn_rec => i_ae_txn_rec,
4393                                                                    o_ae_err_rec => l_err_rec);
4394 
4395                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4396                            RAISE process_error;
4397                        END IF;
4398 
4399                        IF g_debug_flag = 'Y' THEN
4400                           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4401                                                                     'COGS Account ID: ' || l_cogs_account);
4402                        END IF;
4403 
4404                        -- Stamp all elemental accounts with the COGS account
4405                        l_acct_rec.mat_account := l_cogs_account;
4406                        l_acct_rec.mat_ovhd_account := l_cogs_account;
4407                        l_acct_rec.res_account := l_cogs_account;
4408                        l_acct_rec.osp_account := l_cogs_account;
4409                        l_acct_rec.ovhd_account := l_cogs_account;
4410                        l_acct_rec.account := l_cogs_account;
4411 
4412                        l_dr_flag := TRUE;
4413 
4414                        -- Debit COGS
4415                        -- ----------
4416                        l_stmt_num := 385;
4417 
4418                        CSTPAPBR.offset_accounts (i_ae_txn_rec,
4419                                                  i_ae_curr_rec,
4420                                                  2,    --- Acct Line Type
4421                                                  0,    --- Elemental Flag
4422                                                  0,    --- Ovhd Flag
4423                                                  l_dr_flag,
4424                                                  l_acct_rec,
4425                                                  l_ae_line_tbl,
4426                                                  l_err_rec);
4427                        -- check error
4428                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4429                            raise process_error;
4430                        END IF;
4431 
4432                        l_dr_flag := NOT l_dr_flag;
4433 
4434                        -- Credit Intransit
4435                        -- ----------------
4436                        l_stmt_num := 390;
4437 
4438                        CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4439                                                     i_ae_curr_rec,
4440                                                     "FALSE",   --- Exp Flag
4441                                                      null,      --- Exp Acct
4442                                                      l_dr_flag,
4443                                                      l_ae_line_tbl,
4444                                                      l_err_rec,
4445                                                      1);
4446                         -- check error
4447                         IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4448                             RAISE process_error;
4449                         END IF;
4450 
4451                      END IF; /*  IF (l_cg_exp_item <> 1) THEN */
4452 
4453                   ELSE /* This is the receiving CG */
4454 
4455                          l_stmt_num := 395;
4456 
4457                          IF g_debug_flag = 'Y' THEN
4458                            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4459                                                    ': Receipt txn of Internal Order FOB Receipt - Receiving Cost Group');
4460                          END IF;
4461 
4462                          /* Credit Intercompany Accrual Account */
4463 
4464                          SELECT nvl(expense_account_id,-1)
4465                          INTO   l_accrual_account
4466                          FROM   mtl_material_transactions
4467                          WHERE  transaction_id = i_ae_txn_rec.transaction_id;
4468 
4469                          l_stmt_num := 396;
4470                          -- Bug 5573993 - Derive the Intercompany account to break dependency on perpetual
4471                          -- code as if the trxn is not costed, mmt has incorrect or no account stamped
4472 
4473                          IF g_debug_flag = 'Y' THEN
4474                            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4475                                                                   'Deriving intecompany account');
4476                          END IF;
4477 
4478                          l_accrual_account := get_intercompany_account (i_ae_txn_rec => i_ae_txn_rec,
4479                                                                         o_ae_err_rec => l_err_rec);
4480 
4481                          if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
4482                            raise process_error;
4483                          end if;
4484 
4485                          IF g_debug_flag = 'Y' THEN
4486                            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4487                                                                     'Intercompany Account ID: ' || l_accrual_account);
4488                          END IF;
4489 
4490                          l_dr_flag := FALSE;
4491 
4492                          l_stmt_num := 397;
4493                          select count(transaction_id)
4494                          into l_mat_ovhd_exists
4495                          from mtl_pac_actual_cost_details
4496                          where transaction_id = i_ae_txn_rec.transaction_id
4497                          and pac_period_id = i_ae_txn_rec.accounting_period_id
4498                          and cost_group_id = i_ae_txn_rec.cost_group_id
4499                          and cost_element_id = 2
4500                          and level_type = 1;
4501 
4502                          if (l_mat_ovhd_exists > 0) then
4503                             l_stmt_num := 400;
4504                             ovhd_accounts(i_ae_txn_rec,
4505                                           i_ae_curr_rec,
4506                                           l_dr_flag,
4507                                           l_ae_line_tbl,
4508                                           l_err_rec);
4509                            -- check error
4510                            if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
4511                              raise process_error;
4512                            end if;
4513                          end if;
4514 
4515                            -- Credit Intercompany Accrual
4516                            l_stmt_num := 401;
4517 
4518                            SELECT  NVL(mmt.transfer_price,0)
4519                            INTO    l_transfer_price
4520                            FROM    mtl_material_transactions MMT
4521                            WHERE   MMT.transaction_id = i_ae_txn_rec.transaction_id;
4522 
4523                            IF g_debug_flag = 'Y' THEN
4524                               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4525                                 ': Cr Intercompany Accrual @ : ' || l_transfer_price);
4526                            END IF;
4527 
4528                            l_ae_line_rec.account := l_accrual_account;
4529                            l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity) * l_transfer_price;
4530                            l_ae_line_rec.resource_id := NULL;
4531                            l_ae_line_rec.cost_element_id := NULL;
4532                            l_ae_line_rec.ae_line_type := 2;
4533 
4534                            insert_account (i_ae_txn_rec,
4535                                            i_ae_curr_rec,
4536                                            l_dr_flag,
4537                                            l_ae_line_rec,
4538                                            l_ae_line_tbl,
4539                                            l_err_rec);
4540 
4541                            -- check error
4542                            IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4543                                RAISE process_error;
4544                            END IF;
4545 
4546                          IF (l_tprice_option = 2) THEN
4547                             l_stmt_num := 402;
4548 
4549                             IF g_debug_flag = 'Y' THEN
4550                                fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4551                                            ': CST: Transfer Price Option = Yes, Price As Incoming Cost');
4552                             END IF;
4553 
4554                           ELSIF (l_tprice_option = 1) THEN
4555                             l_stmt_num := 404;
4556                             IF g_debug_flag = 'Y' THEN
4557                                fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4558                                  ': CST: Transfer Price Option = Yes, Price Not As Incoming Cost');
4559                             END IF;
4560 
4561                            -- Debit/Credit Profit in Inventory
4562                            l_stmt_num := 412;
4563 
4564                            SELECT sum(nvl(transaction_cost,0))
4565                            INTO   l_mptcd_cost
4566                            FROM   mtl_pac_txn_cost_details
4567                            WHERE  transaction_id = i_ae_txn_rec.transaction_id
4568                            AND    pac_period_id = i_ae_txn_rec.accounting_period_id
4569                            AND    cost_group_id = i_ae_txn_rec.cost_group_id;
4570 
4571                            l_profit_in_inv_value := l_mptcd_cost - l_transfer_price;
4572 
4573                            IF (sign(l_profit_in_inv_value) = 1) THEN
4574                                 l_dr_flag := FALSE;
4575                             ELSIF (sign(l_profit_in_inv_value) = -1) THEN
4576                                 l_dr_flag := TRUE;
4577                             ELSE
4578                                 l_dr_flag := NULL;
4579                             END IF;
4580 
4581                             l_profit_in_inv_value := abs(l_profit_in_inv_value);
4582 
4583                             IF (l_dr_flag IS NOT NULL) THEN
4584 
4585                                     l_stmt_num := 417;
4586                                     SELECT nvl(profit_in_inv_account, -1)
4587                                     INTO   l_profit_in_inv_account
4588                                     FROM   mtl_interorg_parameters
4589                                     WHERE  from_organization_id = i_ae_txn_rec.xfer_organization_id
4590                                     AND    to_organization_id = i_ae_txn_rec.organization_id;
4591 
4592                                     IF (l_profit_in_inv_account = -1) THEN
4593                                        RAISE no_profit_in_inv_acct_error;
4594                                     END IF;
4595 
4596                                     /* Profit in Inventory entry */
4597                                     l_ae_line_rec.account := l_profit_in_inv_account;
4598                                     l_ae_line_rec.transaction_value := l_profit_in_inv_value  * abs(i_ae_txn_rec.primary_quantity);
4599                                     l_ae_line_rec.resource_id := NULL;
4600                                     l_ae_line_rec.cost_element_id := NULL;
4601                                     l_ae_line_rec.ae_line_type := 30;   -- Profit in Inventory
4602 
4603                                     l_stmt_num := 419;
4604 
4605                                     insert_account (i_ae_txn_rec,
4606                                                     i_ae_curr_rec,
4607                                                     l_dr_flag,
4608                                                     l_ae_line_rec,
4609                                                     l_ae_line_tbl,
4610                                                     l_err_rec);
4611 
4612                                     -- check error
4613                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4614                                         RAISE process_error;
4615                                     END IF;
4616 
4617                             END IF; /* (l_dr_flag IS NOT NULL) */
4618 
4619                          END IF; /* (l_tprice_option = 2) */
4620 
4621                         l_dr_flag := TRUE;
4622 
4623                        /* Debit Inventory - OnHand */
4624 
4625                         l_stmt_num := 420;
4626 
4627                         -- Debit On-hand (if expense, then use expense account)
4628                         -- -----------------------------------------------------
4629                         IF (l_exp_flg) THEN
4630                            CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4631                                                         i_ae_curr_rec,
4632                                                         l_exp_flg, -- Exp Flag
4633                                                         l_exp_account, -- Exp Acct
4634                                                         l_dr_flag,
4635                                                         l_ae_line_tbl,
4636                                                         l_err_rec);
4637                         ELSE
4638                            CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4639                                                         i_ae_curr_rec,
4640                                                         "FALSE", -- Exp Flag
4641                                                         null, -- Exp Acct
4642                                                         l_dr_flag,
4643                                                         l_ae_line_tbl,
4644                                                         l_err_rec);
4645                         END IF;
4646 
4647                         -- check error
4648                         IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4649                             RAISE process_error;
4650                         END IF;
4651 
4652                   END IF;
4653 
4654               ELSIF (l_fob_point = 1) THEN /* Internal Order FOBS */
4655 
4656                     l_stmt_num := 422;
4657 
4658                     IF g_debug_flag = 'Y' THEN
4659                            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4660                                                    ': Receipt txn of Internal Order FOB Shipment - Receiving Cost Group');
4661                     END IF;
4662 
4663 
4664                   IF (l_cg_exp_item <> 1) THEN
4665                      /* This can only be the receiving CG
4666                         Debit OnHand and Credit Intransit */
4667 
4668                     l_dr_flag := TRUE;
4669 
4670                     -- Debit On-hand (if expense, then use expense account)
4671                     -- -----------------------------------------------------
4672                     IF (l_exp_flg) THEN
4673                         CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4674                                                      i_ae_curr_rec,
4675                                                      l_exp_flg, -- Exp Flag
4676                                                      l_exp_account, -- Exp Acct
4677                                                      l_dr_flag,
4678                                                      l_ae_line_tbl,
4679                                                      l_err_rec);
4680                     ELSE
4681                         CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4682                                                      i_ae_curr_rec,
4683                                                      "FALSE", -- Exp Flag
4684                                                      null, -- Exp Acct
4685                                                      l_dr_flag,
4686                                                      l_ae_line_tbl,
4687                                                      l_err_rec);
4688                     END IF;
4689 
4690                     -- check error
4691                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4692                         RAISE process_error;
4693                     END IF;
4694 
4695                     l_dr_flag := not l_dr_flag;
4696 
4697                     -- Credit Intransit
4698                     -- ----------------
4699                     l_stmt_num := 424;
4700 
4701                     CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4702                                                  i_ae_curr_rec,
4703                                                  "FALSE",   --- Exp Flag
4704                                                   null,      --- Exp Acct
4705                                                  l_dr_flag,
4706                                                  l_ae_line_tbl,
4707                                                  l_err_rec,
4708                                                  1);
4709                     -- check error
4710                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4711                         RAISE process_error;
4712                     END IF;
4713 
4714                   END IF; /* IF (l_cg_exp_item <> 1) THEN */
4715 
4716               END IF;
4717 
4718                 -- INVCONV sikhanna : exclude logical txns. Taken care at the end.
4719            ELSIF (i_ae_txn_rec.txn_action_id not in (15,22)) THEN /* Ordinary Interorg receipts */
4720 
4721                 l_stmt_num := 426;
4722 
4723                 IF g_debug_flag = 'Y' THEN
4724                    fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4725                                                    ': Interorg Receipt transaction');
4726                 END IF;
4727 
4728                 IF ((i_ae_txn_rec.cost_group_id <> l_shipping_cg_id) AND l_fob_point = 2) THEN /* This is the receiving CG for FOBR */
4729 
4730                      l_stmt_num := 428;
4731 
4732                      IF g_debug_flag = 'Y' THEN
4733                         fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4734                                                    ': Receipt txn of FOB Receipt - Receiving Cost Group');
4735                      END IF;
4736 
4737                      -- Processing the process-discrete txns
4738                      -- INVCONV sikhanna
4739                      SELECT MOD(SUM(DECODE(process_enabled_flag,'Y',1,2)), 2)
4740                      INTO   l_pd_txfr_ind
4741                      FROM   MTL_PARAMETERS MP
4742                      WHERE  MP.ORGANIZATION_ID = i_ae_txn_rec.xfer_organization_id
4743                             OR MP.ORGANIZATION_ID    = i_ae_txn_rec.organization_id;
4744 
4745                      IF g_debug_flag = 'Y' THEN
4746                            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4747                                      ': Process-isc Ind: ' || l_pd_txfr_ind);
4748                      END IF;
4749 
4750                      /* Process OPM-Discrete transfers here as the accouting distribution template is same INVCONV*/
4751 
4752                       /* Credit Payables and debit OnHand Inventory */
4753 
4754                       l_dr_flag := TRUE;
4755 
4756                       -- Debit On-hand (if expense, then use expense account)
4757                       -- -----------------------------------------------------
4758                          IF (l_exp_flg) THEN
4759                             CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4760                                                         i_ae_curr_rec,
4761                                                         l_exp_flg, -- Exp Flag
4762                                                         l_exp_account, -- Exp Acct
4763                                                         l_dr_flag,
4764                                                         l_ae_line_tbl,
4765                                                         l_err_rec);
4766                          ELSE
4767                             CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4768                                                         i_ae_curr_rec,
4769                                                         "FALSE", -- Exp Flag
4770                                                         null, -- Exp Acct
4771                                                         l_dr_flag,
4772                                                         l_ae_line_tbl,
4773                                                         l_err_rec);
4774                          END IF;
4775 
4776                          -- check error
4777                          IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4778                              RAISE process_error;
4779                          END IF;
4780 
4781                          l_dr_flag := not l_dr_flag;
4782 
4783                          l_stmt_num := 430;
4784 
4785                          /* Absorb MOH if exists */
4786 
4787                          select count(transaction_id)
4788                          into l_mat_ovhd_exists
4789                          from mtl_pac_actual_cost_details
4790                          where transaction_id = i_ae_txn_rec.transaction_id
4791                          and pac_period_id = i_ae_txn_rec.accounting_period_id
4792                          and cost_group_id = i_ae_txn_rec.cost_group_id
4793                          and cost_element_id = 2
4794                          and level_type = 1;
4795 
4796                          l_stmt_num := 432;
4797                          if (l_mat_ovhd_exists > 0) then
4798                             ovhd_accounts(i_ae_txn_rec,
4799                                           i_ae_curr_rec,
4800                                           l_dr_flag,
4801                                           l_ae_line_tbl,
4802                                           l_err_rec);
4803                            -- check error
4804                            if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
4805                              raise process_error;
4806                            end if;
4807                          end if;
4808 
4809                          l_stmt_num := 434;
4810                          SELECT mip.interorg_payables_account
4811                          INTO   l_io_pay_acct
4812                          FROM   mtl_interorg_parameters mip
4813                          WHERE  mip.from_organization_id = i_ae_txn_rec.xfer_organization_id
4814                          AND    mip.to_organization_id = i_ae_txn_rec.organization_id;
4815 
4816                          l_stmt_num := 435;
4817 
4818                          SELECT sum(nvl(transaction_cost,0))
4819                          INTO l_mptcd_cost
4820                          FROM mtl_pac_txn_cost_details
4821                          WHERE transaction_id = i_ae_txn_rec.transaction_id
4822                          AND pac_period_id = i_ae_txn_rec.accounting_period_id
4823                          AND cost_group_id = i_ae_txn_rec.cost_group_id;
4824 
4825                          l_ae_line_rec.account := l_io_pay_acct;
4826                          l_ae_line_rec.transaction_value := (l_mptcd_cost * abs(i_ae_txn_rec.primary_quantity));
4827                          l_ae_line_rec.resource_id := NULL;
4828                          l_ae_line_rec.cost_element_id := NULL;
4829                          l_ae_line_rec.ae_line_type := 9;  -- Payables
4830 
4831                          l_stmt_num := 440;
4832 
4833                          /* Credit Payables */
4834                          insert_account (i_ae_txn_rec,
4835                                          i_ae_curr_rec,
4836                                          l_dr_flag,
4837                                          l_ae_line_rec,
4838                                          l_ae_line_tbl,
4839                                          l_err_rec);
4840                          -- check error
4841                          IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4842                              RAISE process_error;
4843                          END IF;
4844 
4845                 ELSIF ((i_ae_txn_rec.cost_group_id = l_shipping_cg_id) AND l_fob_point = 2) THEN /* This is the sending CG for FOBR */
4846 
4847                        l_stmt_num := 445;
4848 
4849                        IF g_debug_flag = 'Y' THEN
4850                         fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4851                                                    ': Receipt txn of FOB Receipt - Shipping Cost Group');
4852                        END IF;
4853 
4854                        SELECT mip.interorg_receivables_account,
4855                               mmt.transportation_dist_account,
4856                               mip.interorg_transfer_cr_account,
4857                               nvl(mip.interorg_profit_account, -1)
4858                        INTO   l_io_rcv_acct,
4859                               l_io_freight_acct,
4860                               l_io_txfr_cr_acct,
4861                               l_io_txfr_var_acct
4862                        FROM   mtl_interorg_parameters mip,mtl_material_transactions mmt
4863                        WHERE  mip.from_organization_id = i_ae_txn_rec.xfer_organization_id
4864                        AND    mip.to_organization_id = i_ae_txn_rec.organization_id
4865                        AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
4866 
4867                        IF (l_io_txfr_var_acct = -1) THEN
4868                               RAISE no_interorg_profit_acct_error;
4869                        END IF;
4870 
4871                        /* Debit Receivables and Credit Intransit */
4872 
4873                         /* Credit Intransit */
4874 
4875                         l_stmt_num := 450;
4876 
4877                         l_dr_flag := FALSE;
4878 
4879                         IF (l_cg_exp_item = 1) THEN
4880                            /* Credit Expense */
4881                            CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4882                                                        i_ae_curr_rec,
4883                                                        l_exp_flg, -- Exp Flag
4884                                                        l_exp_account, -- Exp Acct
4885                                                        l_dr_flag,
4886                                                        l_ae_line_tbl,
4887                                                        l_err_rec);
4888                         ELSE
4889                            /* Credit Inventory - Intransit */
4890                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
4891                                                     i_ae_curr_rec,
4892                                                     "FALSE",   --- Exp Flag
4893                                                     null,      --- Exp Acct
4894                                                     l_dr_flag,
4895                                                     l_ae_line_tbl,
4896                                                     l_err_rec,
4897                                                     1);
4898 
4899                         END IF;
4900 
4901                         -- check error
4902                         IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
4903                             RAISE process_error;
4904                         END IF;
4905 
4906                        l_stmt_num := 455;
4907 
4908                        SELECT TRANSFER_COST_FLAG
4909                        INTO l_transfer_cost_flag
4910                        FROM CST_LE_COST_TYPES
4911                        WHERE LEGAL_ENTITY = i_ae_txn_rec.legal_entity_id
4912                        AND COST_TYPE_ID = i_ae_txn_rec.cost_type_id;
4913 
4914                        l_stmt_num := 460;
4915 
4916                        SELECT nvl(max(cpp.pac_period_id), -1)
4917                        INTO   l_prev_period_id
4918                        FROM   cst_pac_periods cpp
4919                        WHERE  cpp.cost_type_id = i_ae_txn_rec.cost_type_id
4920                        AND    cpp.legal_entity = i_ae_txn_rec.legal_entity_id
4921                        AND    cpp.pac_period_id < i_ae_txn_rec.accounting_period_id;
4922 
4923                        l_stmt_num := 465;
4924 
4925                        SELECT NVL(MAX(cost_group_id),-1)
4926                        INTO   l_txfr_cost_group_id
4927                        FROM   cst_cost_group_assignments
4928                        WHERE  organization_id = i_ae_txn_rec.organization_id;
4929 
4930                        l_stmt_num := 470;
4931 
4932                        BEGIN
4933                          SELECT legal_entity
4934                          INTO l_txfr_legal_entity
4935                          FROM cst_cost_groups
4936                          WHERE cost_group_id = l_txfr_cost_group_id;
4937                        EXCEPTION
4938                        WHEN no_data_found THEN
4939                             l_txfr_legal_entity := NULL;
4940                        END;
4941 
4942                        /* Check for the same LE/CT combination */
4943                        l_stmt_num := 475;
4944 
4945                        SELECT count(*)
4946                        INTO   l_same_le_ct
4947                        FROM   cst_le_cost_types
4948                        WHERE  legal_entity = l_txfr_legal_entity
4949                        AND    cost_type_id = i_ae_txn_rec.cost_type_id;
4950 
4951                        l_stmt_num := 480;
4952 
4953                        l_ae_line_rec.account := l_io_rcv_acct;
4954 
4955                        l_dr_flag := TRUE;
4956 
4957                        /* Debit Receivables at CPIC/Perpetual/Prior Period PWAC */
4958                        l_stmt_num := 485;
4959 
4960                          IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0 AND l_transfer_cost_flag = 'Y') THEN
4961 
4962                         --    IF l_transfer_cost_flag = 'Y' THEN    /* PACP used */
4963 
4964                                    l_stmt_num := 490;
4965 
4966                                     /* Use CPIC cost */
4967                                     SELECT sum(NVL(CPIC.item_cost,0))
4968                                     INTO   l_pacp_pwac_cost
4969                                     FROM   CST_PAC_ITEM_COSTS CPIC
4970                                     WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
4971                                     AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
4972                                     AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
4973 
4974                                     IF g_debug_flag = 'Y' THEN
4975                                         fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
4976                                                                       ': Using PAC Absorption Cost: ' || l_pacp_pwac_cost);
4977                                     END IF;
4978 
4979                                    l_stmt_num := 492;
4980                                    SELECT nvl(transfer_percentage,0),nvl(transfer_cost,0),nvl(transportation_cost,0),primary_quantity
4981                                    INTO   l_txfr_percent,l_txfr_cost,l_trp_cost,l_txfr_txn_qty
4982                                    FROM   mtl_material_transactions
4983                                    WHERE  transaction_id = l_txfr_txn_id;
4984 
4985                                    IF (l_txfr_percent <> 0) THEN
4986                                        l_txfr_credit := (l_txfr_percent * l_pacp_pwac_cost / 100);
4987                                    ELSIF (l_txfr_cost <> 0) THEN
4988                                        l_txfr_credit := l_txfr_cost / abs(l_txfr_txn_qty);
4989                                    ELSE
4990                                        l_txfr_credit := 0;
4991                                    END IF;
4992 
4993                                    l_io_rcv_value := (l_pacp_pwac_cost +
4994                                                      (((l_trp_cost / abs(l_txfr_txn_qty))+ l_txfr_credit)));
4995 
4996                                    l_ae_line_rec.transaction_value := l_io_rcv_value * abs(i_ae_txn_rec.primary_quantity);
4997                                     l_ae_line_rec.resource_id := NULL;
4998                                     l_ae_line_rec.cost_element_id := NULL;
4999                                     l_ae_line_rec.ae_line_type := 10;  -- Receivables
5000 
5001                                     l_stmt_num := 495;
5002 
5003                                     /* Debit receivables */
5004                                     insert_account (i_ae_txn_rec,
5005                                                     i_ae_curr_rec,
5006                                                     l_dr_flag,
5007                                                     l_ae_line_rec,
5008                                                     l_ae_line_tbl,
5009                                                     l_err_rec);
5010                                     -- check error
5011                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5012                                         RAISE process_error;
5013                                     END IF;
5014 
5015                                    /* Create entries for transfer credit and freight expense */
5016 
5017                                    l_dr_flag := not l_dr_flag;
5018 
5019                                    l_ae_line_rec.account := l_io_txfr_cr_acct;
5020                                    l_ae_line_rec.transaction_value := l_txfr_credit * abs(i_ae_txn_rec.primary_quantity);
5021                                    l_ae_line_rec.resource_id := NULL;
5022                                    l_ae_line_rec.cost_element_id := NULL;
5023                                    l_ae_line_rec.ae_line_type := 11;   -- transfer credit
5024 
5025                                    l_stmt_num := 496;
5026 
5027                                    IF (l_ae_line_rec.transaction_value <> 0) THEN
5028 
5029                                        insert_account (i_ae_txn_rec,
5030                                                        i_ae_curr_rec,
5031                                                        l_dr_flag,
5032                                                        l_ae_line_rec,
5033                                                        l_ae_line_tbl,
5034                                                        l_err_rec);
5035                                        -- check error
5036                                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5037                                            RAISE process_error;
5038                                        END IF;
5039 
5040                                    END IF;
5041 
5042                                    l_stmt_num := 497;
5043 
5044                                    l_ae_line_rec.transaction_value := l_trp_cost;
5045                                    l_ae_line_rec.account := l_io_freight_acct;
5046                                    l_ae_line_rec.resource_id := NULL;
5047                                    l_ae_line_rec.cost_element_id := NULL;
5048                                    l_ae_line_rec.ae_line_type := 12;   -- freight account
5049 
5050                                    l_stmt_num := 498;
5051 
5052                                    /* Freight account */
5053                                    IF (l_ae_line_rec.transaction_value <> 0) THEN
5054 
5055                                        insert_account (i_ae_txn_rec,
5056                                                        i_ae_curr_rec,
5057                                                        l_dr_flag,
5058                                                        l_ae_line_rec,
5059                                                        l_ae_line_tbl,
5060                                                        l_err_rec);
5061 
5062                                        -- check error
5063                                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5064                                            RAISE process_error;
5065                                        END IF;
5066 
5067                                    END IF;
5068 
5069                          ELSE /* PACP not used */
5070 
5071                             IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity AND l_same_le_ct > 0 AND l_prev_period_id <> -1) THEN
5072 
5073                               l_stmt_num := 500;
5074                               l_use_prev_period_cost := 1;
5075 
5076                            /* Get prior period PWAC Cost */
5077                                 BEGIN
5078                                     SELECT nvl(CPIC.item_cost,0)
5079                                       INTO l_prev_period_pwac_cost
5080                                       FROM CST_PAC_ITEM_COSTS CPIC
5081                                      WHERE CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
5082                                        AND CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
5083                                        AND CPIC.PAC_PERIOD_ID     = l_prev_period_id;
5084                                 EXCEPTION
5085                                 when no_data_found then
5086                                     /* Use perpetual cost if prior period cost is not available */
5087                                     l_use_prev_period_cost := 0;
5088                                 END;
5089                             ELSE
5090                              /* Use perpetual cost if prior period cost is not available or if the cost groups are not in the same LE/CT. */
5091                                l_use_prev_period_cost := 0;
5092                             END IF;
5093 
5094                             IF (l_use_prev_period_cost = 1) THEN
5095 
5096                                    IF g_debug_flag = 'Y' THEN
5097                                         fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
5098                                                                       ': Using prior period PWAC cost: ' || l_prev_period_pwac_cost);
5099                                    END IF;
5100 
5101                                    l_stmt_num := 505;
5102 
5103                                    SELECT nvl(transfer_percentage,0),nvl(transfer_cost,0),nvl(transportation_cost,0),primary_quantity
5104                                    INTO   l_txfr_percent,l_txfr_cost,l_trp_cost,l_txfr_txn_qty
5105                                    FROM   mtl_material_transactions
5106                                    WHERE  transaction_id = l_txfr_txn_id;
5107 
5108                                    IF (l_txfr_percent <> 0) THEN
5109                                        l_txfr_credit := (l_txfr_percent * l_prev_period_pwac_cost / 100);
5110                                    ELSIF (l_txfr_cost <> 0) THEN
5111                                        l_txfr_credit := l_txfr_cost / abs(l_txfr_txn_qty);
5112                                    ELSE
5113                                        l_txfr_credit := 0;
5114                                    END IF;
5115 
5116                                    l_io_rcv_value := (l_prev_period_pwac_cost +
5117                                                      (((l_trp_cost / abs(l_txfr_txn_qty))+ l_txfr_credit)));
5118 
5119                                    l_ae_line_rec.transaction_value := l_io_rcv_value * abs(i_ae_txn_rec.primary_quantity);
5120                                    l_ae_line_rec.resource_id := NULL;
5121                                    l_ae_line_rec.cost_element_id := NULL;
5122                                    l_ae_line_rec.ae_line_type := 10;   -- Receivables
5123 
5124                                    l_stmt_num := 510;
5125 
5126                                    /* Debit receivables */
5127                                    insert_account (i_ae_txn_rec,
5128                                                    i_ae_curr_rec,
5129                                                    l_dr_flag,
5130                                                    l_ae_line_rec,
5131                                                    l_ae_line_tbl,
5132                                                    l_err_rec);
5133                                    -- check error
5134                                    IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5135                                        RAISE process_error;
5136                                    END IF;
5137 
5138                                    l_stmt_num := 515;
5139 
5140                                    /* Create entries for transfer credit and freight expense */
5141 
5142                                    l_dr_flag := not l_dr_flag;
5143 
5144                                    l_ae_line_rec.account := l_io_txfr_cr_acct;
5145                                    l_ae_line_rec.transaction_value := l_txfr_credit * abs(i_ae_txn_rec.primary_quantity);
5146                                    l_ae_line_rec.resource_id := NULL;
5147                                    l_ae_line_rec.cost_element_id := NULL;
5148                                    l_ae_line_rec.ae_line_type := 11;   -- transfer credit
5149 
5150                                    l_stmt_num := 520;
5151 
5152                                    IF (l_ae_line_rec.transaction_value <> 0) THEN
5153 
5154                                        insert_account (i_ae_txn_rec,
5155                                                        i_ae_curr_rec,
5156                                                        l_dr_flag,
5157                                                        l_ae_line_rec,
5158                                                        l_ae_line_tbl,
5159                                                        l_err_rec);
5160                                        -- check error
5161                                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5162                                            RAISE process_error;
5163                                        END IF;
5164 
5165                                    END IF;
5166 
5167                                    l_stmt_num := 525;
5168 
5169                                    l_ae_line_rec.transaction_value := l_trp_cost;
5170                                    l_ae_line_rec.account := l_io_freight_acct;
5171                                    l_ae_line_rec.resource_id := NULL;
5172                                    l_ae_line_rec.cost_element_id := NULL;
5173                                    l_ae_line_rec.ae_line_type := 12;   -- freight account
5174 
5175                                    l_stmt_num := 530;
5176 
5177                                    /* Freight account */
5178                                    IF (l_ae_line_rec.transaction_value <> 0) THEN
5179 
5180                                        insert_account (i_ae_txn_rec,
5181                                                        i_ae_curr_rec,
5182                                                        l_dr_flag,
5183                                                        l_ae_line_rec,
5184                                                        l_ae_line_tbl,
5185                                                        l_err_rec);
5186 
5187                                        -- check error
5188                                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5189                                            RAISE process_error;
5190                                        END IF;
5191 
5192                                    END IF;
5193 
5194                                   /* Transfer Variance */
5195                                   l_stmt_num := 535;
5196 
5197                                   SELECT sum(nvl(actual_cost,0))
5198                                   INTO l_mpacd_cost
5199                                   FROM mtl_pac_actual_cost_details
5200                                   WHERE transaction_id = i_ae_txn_rec.transaction_id
5201                                   AND pac_period_id = i_ae_txn_rec.accounting_period_id
5202                                   AND cost_group_id = i_ae_txn_rec.cost_group_id;
5203 
5204                                   l_stmt_num := 537;
5205 
5206                                   l_txfr_var_value := l_io_rcv_value - (l_mpacd_cost + l_txfr_credit + l_trp_cost/abs(l_txfr_txn_qty));
5207 
5208                                   IF (sign(l_txfr_var_value) = 1) THEN
5209                                      l_dr_flag := FALSE;
5210                                   ELSIF (sign(l_txfr_var_value) = -1) THEN
5211                                      l_dr_flag := TRUE;
5212                                   ELSE
5213                                      l_dr_flag := NULL;
5214                                   END IF;
5215 
5216                                   l_txfr_var_value := abs(l_txfr_var_value);
5217 
5218                                   IF (l_dr_flag IS NOT NULL) THEN
5219 
5220                                     IF (l_io_txfr_var_acct = -1) THEN
5221                                       RAISE no_interorg_profit_acct_error;
5222                                     END IF;
5223 
5224                                     /* Transfer variance entry */
5225                                     l_ae_line_rec.account := l_io_txfr_var_acct;
5226                                     l_ae_line_rec.transaction_value := l_txfr_var_value * abs(i_ae_txn_rec.primary_quantity);
5227                                     l_ae_line_rec.resource_id := NULL;
5228                                     l_ae_line_rec.cost_element_id := NULL;
5229                                     l_ae_line_rec.ae_line_type := 34;   -- Interorg profit in inventory
5230 
5231                                     insert_account (i_ae_txn_rec,
5232                                             i_ae_curr_rec,
5233                                             l_dr_flag,
5234                                             l_ae_line_rec,
5235                                             l_ae_line_tbl,
5236                                             l_err_rec);
5237 
5238                                     -- check error
5239                                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5240                                        RAISE process_error;
5241                                     END IF;
5242 
5243                                   END IF;
5244 
5245                          ELSE /* MTA Entries to be used */
5246 
5247                              l_stmt_num := 540;
5248 
5249                              IF g_debug_flag = 'Y' THEN
5250                                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
5251                                               ': Using MTA cost');
5252                              END IF;
5253 
5254                              /* Debit Receivables */
5255 
5256                             BEGIN
5257                              SELECT nvl(SUM(ABS(NVL(base_transaction_value, 0))),0)
5258                              INTO l_perp_ship_value
5259                              FROM mtl_transaction_accounts mta
5260                              WHERE mta.transaction_id = l_txfr_txn_id
5261                              and mta.organization_id = i_ae_txn_rec.xfer_organization_id
5262                              and mta.accounting_line_type IN (1,2,14)
5263                              and mta.base_transaction_value < 0;
5264                             EXCEPTION
5265                              WHEN no_data_found THEN
5266                                  l_perp_ship_value := 0;
5267                             END;
5268 
5269                              l_stmt_num := 550;
5270 
5271                              /* Select transfer cost and transportation cost from the transfer transaction (shipment txn).
5272                                 We also need the shipment transaction quantity since this is the quantity we'll
5273                                 need to divide by to get the unit transfer and transportation cost. */
5274                              SELECT nvl(transfer_percentage,0),nvl(transfer_cost,0),nvl(transportation_cost,0),primary_quantity
5275                              INTO l_txfr_percent,l_txfr_cost,l_trp_cost,l_txfr_txn_qty
5276                              FROM mtl_material_transactions
5277                              WHERE transaction_id = l_txfr_txn_id;
5278 
5279                              IF (l_txfr_percent <> 0) THEN
5280                                  l_txfr_credit := (l_txfr_percent * (l_perp_ship_value)/ (100 * abs(l_txfr_txn_qty)));
5281                              ELSIF (l_txfr_cost <> 0) THEN
5282                                  l_txfr_credit := l_txfr_cost / abs(l_txfr_txn_qty);
5283                              ELSE
5284                                  l_txfr_credit := 0;
5285                              END IF;
5286 
5287                              l_io_rcv_value :=  ((l_perp_ship_value + l_trp_cost) / abs(l_txfr_txn_qty)) + l_txfr_credit;
5288 
5289                              l_ae_line_rec.transaction_value := l_io_rcv_value  * abs(i_ae_txn_rec.primary_quantity);
5290                              l_ae_line_rec.account := l_io_rcv_acct;
5291                              l_ae_line_rec.resource_id := NULL;
5292                              l_ae_line_rec.cost_element_id := NULL;
5293                              l_ae_line_rec.ae_line_type := 10;  -- Receivables
5294 
5295                              l_stmt_num := 555;
5296 
5297                              insert_account (i_ae_txn_rec,
5298                                               i_ae_curr_rec,
5299                                               l_dr_flag,
5300                                               l_ae_line_rec,
5301                                               l_ae_line_tbl,
5302                                               l_err_rec);
5303                              -- check error
5304                              IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5305                                  RAISE process_error;
5306                              END IF;
5307 
5308                              /* Create entries for transfer credit and freight expense */
5309 
5310                              l_dr_flag := not l_dr_flag;
5311 
5312                              l_ae_line_rec.account := l_io_txfr_cr_acct;
5313                              l_ae_line_rec.transaction_value := l_txfr_credit * abs(i_ae_txn_rec.primary_quantity);
5314                              l_ae_line_rec.resource_id := NULL;
5315                              l_ae_line_rec.cost_element_id := NULL;
5316                              l_ae_line_rec.ae_line_type := 11;   -- transfer credit
5317 
5318                              l_stmt_num := 560;
5319 
5320                              /* transfer credit entry */
5321                              IF (l_ae_line_rec.transaction_value <> 0) THEN
5322 
5323                                  insert_account (i_ae_txn_rec,
5324                                                   i_ae_curr_rec,
5325                                                   l_dr_flag,
5326                                                   l_ae_line_rec,
5327                                                   l_ae_line_tbl,
5328                                                   l_err_rec);
5329                                  -- check error
5330                                  IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5331                                      RAISE process_error;
5332                                  END IF;
5333 
5334                              END IF;
5335 
5336                              /* l_trp_cost is the total freight charge for the shipment transaction quantity,
5337                                 so we need to convert it to the receipt transaction quantity. */
5338                              l_ae_line_rec.transaction_value := (l_trp_cost / abs(l_txfr_txn_qty)) * abs(i_ae_txn_rec.primary_quantity);
5339                              l_ae_line_rec.account := l_io_freight_acct;
5340                              l_ae_line_rec.resource_id := NULL;
5341                              l_ae_line_rec.cost_element_id := NULL;
5342                              l_ae_line_rec.ae_line_type := 12;   -- freight account
5343 
5344                              l_stmt_num := 565;
5345 
5346                              /* freight account entry */
5347                              IF (l_ae_line_rec.transaction_value <> 0) THEN
5348 
5349                                 insert_account (i_ae_txn_rec,
5350                                                   i_ae_curr_rec,
5351                                                   l_dr_flag,
5352                                                   l_ae_line_rec,
5353                                                   l_ae_line_tbl,
5354                                                   l_err_rec);
5355                                 -- check error
5356                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5357                                     RAISE process_error;
5358                                 END IF;
5359 
5360                              END IF;
5361 
5362                              /* Transfer Variance */
5363                              l_stmt_num := 567;
5364 
5365                              SELECT sum(nvl(actual_cost,0))
5366                              INTO l_mpacd_cost
5367                              FROM mtl_pac_actual_cost_details
5368                              WHERE transaction_id = i_ae_txn_rec.transaction_id
5369                              AND pac_period_id = i_ae_txn_rec.accounting_period_id
5370                              AND cost_group_id = i_ae_txn_rec.cost_group_id;
5371 
5372                              l_stmt_num := 570;
5373 
5374                              l_txfr_var_value := l_io_rcv_value - (l_mpacd_cost + l_txfr_credit + l_trp_cost/abs(l_txfr_txn_qty));
5375 
5376                              IF (sign(l_txfr_var_value) = 1) THEN
5377                                  l_dr_flag := FALSE;
5378                              ELSIF (sign(l_txfr_var_value) = -1) THEN
5379                                  l_dr_flag := TRUE;
5380                              ELSE
5381                                  l_dr_flag := NULL;
5382                              END IF;
5383 
5384                              l_txfr_var_value := abs(l_txfr_var_value);
5385 
5386                              IF (l_dr_flag IS NOT NULL) THEN
5387 
5388                                 IF (l_io_txfr_var_acct = -1) THEN
5389                                   RAISE no_interorg_profit_acct_error;
5390                                 END IF;
5391 
5392                                 /* Transfer variance entry */
5393                                 l_ae_line_rec.account := l_io_txfr_var_acct;
5394                                 l_ae_line_rec.transaction_value := l_txfr_var_value * abs(i_ae_txn_rec.primary_quantity);
5395                                 l_ae_line_rec.resource_id := NULL;
5396                                 l_ae_line_rec.cost_element_id := NULL;
5397                                 l_ae_line_rec.ae_line_type := 34;   -- Interorg profit in inventory
5398 
5399                                 insert_account (i_ae_txn_rec,
5400                                         i_ae_curr_rec,
5401                                         l_dr_flag,
5402                                         l_ae_line_rec,
5403                                         l_ae_line_tbl,
5404                                         l_err_rec);
5405 
5406                                 -- check error
5407                                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5408                                    RAISE process_error;
5409                                 END IF;
5410 
5411                              END IF; /* IF l_dr_flag IS NOT NULL */
5412 
5413                          END IF; /* IF (l_use_prev_period_cost = 1) THEN */
5414                     END IF;
5415 
5416                 ELSIF (l_fob_point = 1) THEN /* FOBS - Can only be the receiving CG */
5417 
5418                      l_stmt_num := 575;
5419 
5420                      IF g_debug_flag = 'Y' THEN
5421                        fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
5422                               ': Receipt txn of FOB Shipment - Receiving Cost Group');
5423                      END IF;
5424 
5425                      -- Processing the process-discrete txns
5426                      -- INVCONV sikhanna
5427                      SELECT MOD(SUM(DECODE(process_enabled_flag,'Y',1,2)), 2)
5428                      INTO   l_pd_txfr_ind
5429                      FROM   MTL_PARAMETERS MP
5430                      WHERE  MP.ORGANIZATION_ID = i_ae_txn_rec.xfer_organization_id
5431                             OR MP.ORGANIZATION_ID    = i_ae_txn_rec.organization_id;
5432 
5433                      IF g_debug_flag = 'Y' THEN
5434                            fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
5435                                      ': Process-isc Ind: ' || l_pd_txfr_ind);
5436                      END IF;
5437 
5438                      IF (l_cg_exp_item <> 1) THEN
5439 
5440                        /* Debit OnHand and Credit Intransit */
5441 
5442                        l_dr_flag := TRUE;
5443 
5444                        -- Debit On-hand (if expense, then use expense account)
5445                        -- -----------------------------------------------------
5446                        IF (l_exp_flg) THEN
5447                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
5448                                                       i_ae_curr_rec,
5449                                                       l_exp_flg, -- Exp Flag
5450                                                       l_exp_account, -- Exp Acct
5451                                                       l_dr_flag,
5452                                                       l_ae_line_tbl,
5453                                                       l_err_rec);
5454                        ELSE
5455                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
5456                                                       i_ae_curr_rec,
5457                                                       "FALSE", -- Exp Flag
5458                                                       null, -- Exp Acct
5459                                                       l_dr_flag,
5460                                                       l_ae_line_tbl,
5461                                                       l_err_rec);
5462                        END IF;
5463 
5464                        -- check error
5465                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5466                            RAISE process_error;
5467                        END IF;
5468 
5469                        l_dr_flag := not l_dr_flag;
5470 
5471                        -- Credit Intransit
5472                        -- ----------------
5473                        l_stmt_num := 580;
5474 
5475                        CSTPAPBR.inventory_accounts (i_ae_txn_rec,
5476                                                     i_ae_curr_rec,
5477                                                     "FALSE",   --- Exp Flag
5478                                                     null,      --- Exp Acct
5479                                                     l_dr_flag,
5480                                                     l_ae_line_tbl,
5481                                                     l_err_rec,
5482                                                     1);
5483                        -- check error
5484                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5485                            RAISE process_error;
5486                        END IF;
5487 
5488                      END IF; /* IF (l_cg_exp_item <> 1) THEN */
5489 
5490                 ELSIF (i_ae_txn_rec.txn_action_id = 3) THEN   /* Direct interorg receipt */
5491 
5492                        l_stmt_num := 585;
5493 
5494                        IF g_debug_flag = 'Y' THEN
5495                           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
5496                               ': Direct Interorg Receipt - Receiving Cost Group');
5497                        END IF;
5498 
5499                        /* Debit On-Hand and credit Payables */
5500 
5501                        l_dr_flag := TRUE;
5502 
5503                        -- Debit On-hand (if expense, then use expense account)
5504                        -- -----------------------------------------------------
5505                        IF (l_exp_flg) THEN
5506                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
5507                                                        i_ae_curr_rec,
5508                                                        l_exp_flg, -- Exp Flag
5509                                                        l_exp_account, -- Exp Acct
5510                                                        l_dr_flag,
5511                                                        l_ae_line_tbl,
5512                                                        l_err_rec);
5513                        ELSE
5514                           CSTPAPBR.inventory_accounts (i_ae_txn_rec,
5515                                                        i_ae_curr_rec,
5516                                                        "FALSE", -- Exp Flag
5517                                                        null, -- Exp Acct
5518                                                        l_dr_flag,
5519                                                        l_ae_line_tbl,
5520                                                        l_err_rec);
5521                        END IF;
5522 
5523                        -- check error
5524                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5525                            RAISE process_error;
5526                        END IF;
5527 
5528                        l_dr_flag := not l_dr_flag;
5529 
5530                          /* Absorb MOH if exists */
5531                          l_stmt_num := 587;
5532                          select count(transaction_id)
5533                          into l_mat_ovhd_exists
5534                          from mtl_pac_actual_cost_details
5535                          where transaction_id = i_ae_txn_rec.transaction_id
5536                          and pac_period_id = i_ae_txn_rec.accounting_period_id
5537                          and cost_group_id = i_ae_txn_rec.cost_group_id
5538                          and cost_element_id = 2
5539                          and level_type = 1;
5540 
5541                          l_stmt_num := 590;
5542                          if (l_mat_ovhd_exists > 0) then
5543                             ovhd_accounts(i_ae_txn_rec,
5544                                           i_ae_curr_rec,
5545                                           l_dr_flag,
5546                                           l_ae_line_tbl,
5547                                           l_err_rec);
5548                            -- check error
5549                            if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
5550                              raise process_error;
5551                            end if;
5552                          end if;
5553 
5554                        l_stmt_num := 592;
5555 
5556                        SELECT mip.interorg_payables_account
5557                        INTO   l_io_pay_acct
5558                        FROM   mtl_interorg_parameters mip
5559                        WHERE  mip.from_organization_id = i_ae_txn_rec.xfer_organization_id
5560                        AND    mip.to_organization_id = i_ae_txn_rec.organization_id;
5561 
5562                        l_stmt_num := 595;
5563 
5564 
5565                        SELECT sum(nvl(transaction_cost,0))
5566                        INTO l_mptcd_cost
5567                        FROM mtl_pac_txn_cost_details
5568                        WHERE transaction_id = i_ae_txn_rec.transaction_id
5569                        AND pac_period_id = i_ae_txn_rec.accounting_period_id
5570                        AND cost_group_id = i_ae_txn_rec.cost_group_id;
5571 
5572                        l_ae_line_rec.account := l_io_pay_acct;
5573                        l_ae_line_rec.transaction_value := (l_mptcd_cost * abs(i_ae_txn_rec.primary_quantity));
5574                        l_ae_line_rec.resource_id := NULL;
5575                        l_ae_line_rec.cost_element_id := NULL;
5576                        l_ae_line_rec.ae_line_type := 9;   -- Payables
5577 
5578                        l_stmt_num := 600;
5579 
5580                        -- Credit Payables
5581                        -- ---------------
5582                        insert_account (i_ae_txn_rec,
5583                                        i_ae_curr_rec,
5584                                        l_dr_flag,
5585                                        l_ae_line_rec,
5586                                        l_ae_line_tbl,
5587                                        l_err_rec);
5588                        -- check error
5589                        IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5590                            RAISE process_error;
5591                        END IF;
5592 
5593                 END IF; /* End for all ordinary interorg receipts */
5594 
5595            END IF; /* End for all receipts */
5596 
5597         END IF; /* END  Process Shipment transactions */
5598 
5599         /* INVCONV sikhanna - Create distributions for OPM-Discrete Logical txns other than Internal Orders */
5600         IF (i_ae_txn_rec.txn_src_type_id NOT IN (7,8) OR l_tprice_option <> 2) THEN
5601 
5602             /* Logical Receipt -- INVCONV sikhanna */ /* Do not process the logical txns for Internal Orders if ICR were setup */
5603             IF (i_ae_txn_rec.txn_action_id = 15 and l_fob_point = 1 ) THEN
5604 
5605                 l_stmt_num := 605;
5606 
5607                 IF g_debug_flag = 'Y' THEN
5608                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
5609                                   ': OPM-Discrete Logical Receipt');
5610                 END IF;
5611 
5612                 SELECT mip.interorg_payables_account,
5613                        mmt.transportation_dist_account
5614                 INTO   l_io_pay_acct,
5615                        l_io_freight_acct
5616                 FROM   mtl_interorg_parameters mip,
5617                        mtl_material_transactions mmt
5618                 WHERE  mip.from_organization_id = i_ae_txn_rec.xfer_organization_id
5619                 AND    mip.to_organization_id = i_ae_txn_rec.organization_id
5620                 AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
5621 
5622                 l_stmt_num := 610;
5623 
5624                 /* Debit Intransit */
5625 
5626                 l_dr_flag := TRUE;
5627 
5628                 IF (l_cg_exp_item = 1) THEN /* When Item is expense Bug:5337446 */
5629                    /* Debit Expense */
5630                    CSTPAPBR.inventory_accounts (i_ae_txn_rec,
5631                                                 i_ae_curr_rec,
5632                                                 l_exp_flg, -- Exp Flag
5633                                                 l_exp_account, -- Exp Acct
5634                                                 l_dr_flag,
5635                                                 l_ae_line_tbl,
5636                                                 l_err_rec);
5637                 ELSE
5638                    /* Debit Intransit */
5639                    CSTPAPBR.inventory_accounts (i_ae_txn_rec,
5640                                                 i_ae_curr_rec,
5641                                                 "FALSE", -- Exp Flag
5642                                                 null, -- Exp Acct
5643                                                 l_dr_flag,
5644                                                 l_ae_line_tbl,
5645                                                 l_err_rec,
5646                                                 1);
5647                 END IF;
5648 
5649                 -- check error
5650                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5651                  RAISE process_error;
5652                 END IF;
5653 
5654                 l_stmt_num := 615;
5655 
5656                 /* Create Credit entries */
5657 
5658                 l_dr_flag := not l_dr_flag;
5659 
5660                 SELECT nvl(transfer_price,0),
5661                        nvl(transportation_cost,0)
5662                 INTO   l_txfr_cost, -- transfer price
5663                        l_trp_cost   -- transportation cost
5664                 FROM   mtl_material_transactions mmt
5665                 WHERE  mmt.transaction_id = i_ae_txn_rec.transaction_id;
5666 
5667                 /* Credit freight account */
5668 
5669                 l_stmt_num := 620;
5670 
5671                 l_ae_line_rec.transaction_value := l_trp_cost;
5672                 l_ae_line_rec.account := l_io_freight_acct;
5673                 l_ae_line_rec.resource_id := NULL;
5674                 l_ae_line_rec.cost_element_id := NULL;
5675                 l_ae_line_rec.ae_line_type := 12; -- freight account
5676 
5677                 l_stmt_num := 625;
5678 
5679                 IF (l_ae_line_rec.transaction_value <> 0) THEN
5680 
5681                     insert_account (i_ae_txn_rec,
5682                                     i_ae_curr_rec,
5683                                     l_dr_flag,
5684                                     l_ae_line_rec,
5685                                     l_ae_line_tbl,
5686                                     l_err_rec);
5687                     -- check error
5688                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5689                      RAISE process_error;
5690                     END IF;
5691 
5692                 END IF;
5693 
5694                 l_stmt_num := 630;
5695 
5696                 /* Absorb MOH if exists */
5697 
5698                 select count(transaction_id)
5699                 into l_mat_ovhd_exists
5700                 from mtl_pac_actual_cost_details
5701                 where transaction_id = i_ae_txn_rec.transaction_id
5702                 and pac_period_id = i_ae_txn_rec.accounting_period_id
5703                 and cost_group_id = i_ae_txn_rec.cost_group_id
5704                 and cost_element_id = 2
5705                 and level_type = 1;
5706 
5707                 if (l_mat_ovhd_exists > 0) then
5708 
5709                    l_stmt_num := 632;
5710 
5711                    ovhd_accounts(i_ae_txn_rec,
5712                                  i_ae_curr_rec,
5713                                  l_dr_flag,
5714                                  l_ae_line_tbl,
5715                                  l_err_rec);
5716                   -- check error
5717                   if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
5718                     raise process_error;
5719                   end if;
5720                 end if;
5721 
5722                 SELECT sum(nvl(actual_cost,0))
5723                 INTO  l_txfr_cost
5724                 FROM  mtl_pac_actual_cost_details
5725                 WHERE transaction_id = i_ae_txn_rec.transaction_id
5726                 AND   pac_period_id = i_ae_txn_rec.accounting_period_id
5727                 AND   cost_group_id = i_ae_txn_rec.cost_group_id;
5728 
5729                 /* Subtract MOH if already absorbed */
5730                 l_mat_ovhd_cost := 0;
5731                 if (l_mat_ovhd_exists > 0) then
5732 
5733                    l_stmt_num := 635;
5734 
5735                    select sum(nvl(actual_cost,0))
5736                    into   l_mat_ovhd_cost
5737                    from mtl_pac_cost_subelements
5738                    where transaction_id = i_ae_txn_rec.transaction_id
5739                    and cost_group_id = i_ae_txn_rec.cost_group_id
5740                    and pac_period_id = i_ae_txn_rec.accounting_period_id
5741                    and cost_type_id = i_ae_txn_rec.cost_type_id
5742                    and cost_element_id = 2;
5743 
5744                    if l_mat_ovhd_cost is NULL then
5745                     /*Bug: 5456009 This will be the case when moh is not earned by moh rules.
5746                       MOH is transportation cost in the transfer, so no record in mpcs */
5747                      l_mat_ovhd_cost := 0;
5748                    end if;
5749 
5750                 end if;
5751 
5752                 /* Credit Payables */
5753 
5754                 /* MOH absorption */
5755                 l_ae_line_rec.transaction_value := ((l_txfr_cost - l_mat_ovhd_cost) * abs(i_ae_txn_rec.primary_quantity)) - l_trp_cost;
5756                 l_ae_line_rec.account := l_io_pay_acct;
5757                 l_ae_line_rec.resource_id := NULL;
5758                 l_ae_line_rec.cost_element_id := NULL;
5759                 l_ae_line_rec.ae_line_type := 9;  -- Payables
5760 
5761                 l_stmt_num := 637;
5762 
5763                 insert_account (i_ae_txn_rec,
5764                                 i_ae_curr_rec,
5765                                 l_dr_flag,
5766                                 l_ae_line_rec,
5767                                 l_ae_line_tbl,
5768                                 l_err_rec);
5769                 -- check error
5770                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5771                  RAISE process_error;
5772                 END IF;
5773 
5774                 l_stmt_num := 640;
5775 
5776             /* Logical Shipment -- INVCONV sikhanna */
5777             ELSIF (i_ae_txn_rec.txn_action_id = 22 and l_fob_point = 2) THEN
5778 
5779                 l_stmt_num := 645;
5780                 IF g_debug_flag = 'Y' THEN
5781                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
5782                                   ': OPM-Discrete Logical Shipment');
5783                 END IF;
5784 
5785                 SELECT mip.interorg_receivables_account,
5786                        mmt.transportation_dist_account,
5787                        mip.interorg_transfer_cr_account,
5788                        nvl(mip.interorg_profit_account, -1)
5789                 INTO   l_io_rcv_acct,
5790                        l_io_freight_acct,
5791                        l_io_txfr_cr_acct,
5792                        l_io_txfr_var_acct
5793                 FROM   mtl_interorg_parameters mip,mtl_material_transactions mmt
5794                 WHERE  mip.from_organization_id = i_ae_txn_rec.organization_id -- INVCONV condition is opposite
5795                 AND    mip.to_organization_id = i_ae_txn_rec.xfer_organization_id
5796                 AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
5797 
5798                 IF (l_io_txfr_var_acct = -1) THEN
5799                      RAISE no_interorg_profit_acct_error;
5800                 END IF;
5801 
5802                 l_stmt_num := 650;
5803 
5804                 SELECT nvl(transfer_price,0),
5805                        nvl(transportation_cost,0)
5806                 INTO   l_txfr_cost, -- transfer price
5807                        l_trp_cost   -- transportation cost
5808                 FROM   mtl_material_transactions mmt
5809                 WHERE  mmt.transaction_id = i_ae_txn_rec.transaction_id;
5810 
5811                 l_stmt_num := 655;
5812                 /* create debit entries */
5813 
5814                 l_dr_flag := true;
5815 
5816                 /* Debit Receivables */
5817                 l_ae_line_rec.transaction_value := l_txfr_cost  * abs(i_ae_txn_rec.primary_quantity);
5818                 l_ae_line_rec.account := l_io_rcv_acct;
5819                 l_ae_line_rec.resource_id := NULL;
5820                 l_ae_line_rec.cost_element_id := NULL;
5821                 l_ae_line_rec.ae_line_type := 10;  -- Receivables
5822 
5823                 l_stmt_num := 660;
5824 
5825                 insert_account (i_ae_txn_rec,
5826                                 i_ae_curr_rec,
5827                                 l_dr_flag,
5828                                 l_ae_line_rec,
5829                                 l_ae_line_tbl,
5830                                 l_err_rec);
5831                 -- check error
5832                 IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5833                  RAISE process_error;
5834                 END IF;
5835 
5836                 l_stmt_num := 665;
5837 
5838                 /* create credit entries */
5839 
5840                 l_dr_flag := NOT l_dr_flag;
5841 
5842                 l_ae_line_rec.transaction_value := l_trp_cost;
5843                 l_ae_line_rec.account := l_io_freight_acct;
5844                 l_ae_line_rec.resource_id := NULL;
5845                 l_ae_line_rec.cost_element_id := NULL;
5846                 l_ae_line_rec.ae_line_type := 12;   -- freight account
5847 
5848                 l_stmt_num := 670;
5849 
5850                 /* freight account entry */
5851                 IF (l_ae_line_rec.transaction_value <> 0) THEN
5852 
5853                     insert_account (i_ae_txn_rec,
5854                                     i_ae_curr_rec,
5855                                     l_dr_flag,
5856                                     l_ae_line_rec,
5857                                     l_ae_line_tbl,
5858                                     l_err_rec);
5859                     -- check error
5860                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5861                      RAISE process_error;
5862                     END IF;
5863 
5864                 END IF;
5865 
5866                 l_stmt_num := 675;
5867 
5868                 IF (l_cg_exp_item <> 1) THEN /* Create Credit entries only if item is not expense */
5869 
5870                     /* Removed the code for shipping from Exp subInv 5474899 */
5871                     /* Credit Intransit */
5872                     CSTPAPBR.inventory_accounts (i_ae_txn_rec,
5873                                                  i_ae_curr_rec,
5874                                                  "FALSE", -- Exp Flag
5875                                                  null, -- Exp Acct
5876                                                  l_dr_flag,
5877                                                  l_ae_line_tbl,
5878                                                  l_err_rec,
5879                                                  1);
5880 
5881                     -- check error
5882                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5883                      RAISE process_error;
5884                     END IF;
5885 
5886                 END IF; /* (l_cg_exp_item <> 1) */
5887 
5888                 l_stmt_num := 680;
5889 
5890                 SELECT NVL(CPIC.item_cost,0)
5891                 INTO   l_pwac_cost
5892                 FROM   CST_PAC_ITEM_COSTS CPIC
5893                 WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
5894                 AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
5895                 AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
5896 
5897                 l_profit_or_loss := l_trp_cost + (l_pwac_cost - l_txfr_cost) * abs(i_ae_txn_rec.primary_quantity);
5898 
5899                 IF l_profit_or_loss <> 0 THEN
5900 
5901                     IF l_profit_or_loss > 0 THEN
5902                         l_dr_flag := not l_dr_flag; -- If -ve then Debit interorg profit
5903                     END IF;
5904 
5905                     l_stmt_num := 685;
5906 
5907                     l_ae_line_rec.transaction_value := abs(l_profit_or_loss * i_ae_txn_rec.primary_quantity);
5908                     l_ae_line_rec.account := l_io_txfr_var_acct;
5909                     l_ae_line_rec.resource_id := NULL;
5910                     l_ae_line_rec.cost_element_id := NULL;
5911                     l_ae_line_rec.ae_line_type := 34;   -- interorg profit account
5912 
5913                     l_stmt_num := 690;
5914 
5915                     insert_account (i_ae_txn_rec,
5916                                     i_ae_curr_rec,
5917                                     l_dr_flag,
5918                                     l_ae_line_rec,
5919                                     l_ae_line_tbl,
5920                                     l_err_rec);
5921                     -- check error
5922                     IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
5923                      RAISE process_error;
5924                     END IF;
5925 
5926                 END IF; /* l_profit_or_loss <> 0 */
5927 
5928             END IF; /* END Logical Receipt -- INVCONV sikhanna */
5929 
5930         END IF; /* END (i_ae_txn_rec.txn_src_type_id NOT IN (7,8) OR l_tprice_option <> 2) */
5931 
5932         l_stmt_num := 700;
5933 
5934         IF g_debug_flag = 'Y' THEN
5935           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': return >>');
5936         END IF;
5937 
5938   EXCEPTION
5939 
5940     when no_mfca_acct_error then -- INVCONV
5941     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Error processing transaction ' || i_ae_txn_rec.transaction_id);
5942     o_ae_err_rec.l_err_num := 30005;
5943     o_ae_err_rec.l_err_code := 'CST_PAC_NO_MFCA_ACCTS';
5944     FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_MFCA_ACCTS');
5945     o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
5946 
5947     when no_interorg_profit_acct_error then
5948     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Error processing transaction ' || i_ae_txn_rec.transaction_id);
5949     o_ae_err_rec.l_err_num := 30005;
5950     o_ae_err_rec.l_err_code := 'CST_NO_INTERORG_PROFIT_ACCT';
5951     FND_MESSAGE.set_name('BOM', 'CST_NO_INTERORG_PROFIT_ACCT');
5952     o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
5953 
5954     when no_profit_in_inv_acct_error then
5955     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Error processing transaction ' || i_ae_txn_rec.transaction_id);
5956     o_ae_err_rec.l_err_num := 30005;
5957     o_ae_err_rec.l_err_code := 'CST_NO_PROFIT_INV_ACCT';
5958     FND_MESSAGE.set_name('BOM', 'CST_NO_PROFIT_INV_ACCT');
5959     o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
5960 
5961     when no_cg_acct_error then
5962     o_ae_err_rec.l_err_num := 30003;
5963     o_ae_err_rec.l_err_code := 'CST_PAC_NO_CG_ACCTS';
5964     FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_CG_ACCTS');
5965     o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
5966 
5967     when process_error then
5968     o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
5969     o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
5970     o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
5971 
5972     when others then
5973     o_ae_err_rec.l_err_num := SQLCODE;
5974     o_ae_err_rec.l_err_code := '';
5975     o_ae_err_rec.l_err_msg := 'CSTPAPBR.interorg_cost_txn' || to_char(l_stmt_num) ||
5976     substr(SQLERRM,1,180);
5977 
5978   END interorg_cost_txn;
5979 
5980 
5981 -- ===================================================================
5982 -- Cost Update Transactions.
5983 -- ===================================================================
5984 procedure pcu_cost_txn(
5985   i_ae_txn_rec                IN        CSTPALTY.cst_ae_txn_rec_type,
5986   i_ae_curr_rec               IN        CSTPALTY.cst_ae_curr_rec_type,
5987   l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
5988   o_ae_err_rec                OUT NOCOPY        CSTPALTY.cst_ae_err_rec_type
5989   --i_ae_txn_rec                IN              CSTPALBR.cst_ae_txn_rec_type,
5990   --i_ae_curr_rec               IN              CSTPALBR.cst_ae_curr_rec_type,
5991   --l_ae_line_tbl               IN OUT    CSTPALBR.cst_ae_line_tbl_type,
5992   --o_ae_err_rec                OUT             CSTPALBR.cst_ae_err_rec_type
5993 ) IS
5994   l_cost                                NUMBER;
5995   l_var                                 NUMBER;
5996   l_var_total                           NUMBER;
5997   l_acct_exist                                NUMBER;
5998   l_ele_exist                                 NUMBER;
5999   l_dr_flag                             BOOLEAN;
6000   l_loop_count                          NUMBER := 0;
6001   l_ae_line_rec                         CSTPALTY.cst_ae_line_rec_type;
6002   l_acct_rec                            CSTPALTY.cst_ae_acct_rec_type;
6003   l_err_rec                             CSTPALTY.cst_ae_err_rec_type;
6004   --l_ae_line_rec                       CSTPALBR.cst_ae_line_rec_type;
6005   --l_acct_rec                          CSTPALBR.cst_ae_acct_rec_type;
6006   --l_err_rec                           CSTPALBR.cst_ae_err_rec_type;
6007   l_txn_ce_bal_account                  NUMBER;  -- Bug 4586534
6008   l_stmt_num                                   NUMBER;
6009   l_value_change_flag                   NUMBER;
6010   l_onhand_var                          NUMBER;
6011   l_cost_method                         NUMBER;
6012   no_cg_acct_error                      EXCEPTION;
6013   no_mfca_acct_error                    EXCEPTION;
6014   no_txn_det_error                      EXCEPTION;
6015   process_error                                EXCEPTION;
6016 BEGIN
6017 
6018   IF g_debug_flag = 'Y' THEN
6019     fnd_file.put_line(fnd_file.log,'Pcu_cost_txn <<');
6020   END IF;
6021 
6022 -- Initialize variables.
6023 -- ---------------------
6024   l_stmt_num := 10;
6025   l_err_rec.l_err_num := 0;
6026   l_err_rec.l_err_code := '';
6027   l_err_rec.l_err_msg := '';
6028 
6029 
6030 -- Check if there are Fiscal Cat Accounts.
6031 -- ---------------------------------------
6032   l_stmt_num := 15;
6033    select count(legal_entity_id) /* Bug 4586534 */
6034   into l_acct_exist
6035   from mtl_fiscal_cat_accounts
6036   where legal_entity_id = i_ae_txn_rec.legal_entity_id
6037     and cost_type_id    = i_ae_txn_rec.cost_type_id
6038     and cost_group_id   = i_ae_txn_rec.cost_group_id
6039     and category_id     = i_ae_txn_rec.category_id;
6040 
6041   if (l_acct_exist = 0) then
6042     fnd_file.put_line(fnd_file.log,'Category: '||to_char(i_ae_txn_rec.category_id) || ' has no accounts defined');
6043     raise no_mfca_acct_error;
6044   end if;
6045 
6046 -- Get the accounts from MFCA.
6047 -- ---------------------------
6048   l_stmt_num := 20;
6049   select nvl(material_account,-1),
6050          nvl(material_overhead_account,-1),
6051          nvl(resource_account,-1),
6052          nvl(outside_processing_account,-1),
6053          nvl(overhead_account,-1)
6054     into l_acct_rec.mat_account,
6055          l_acct_rec.mat_ovhd_account,
6056          l_acct_rec.res_account,
6057          l_acct_rec.osp_account,
6058          l_acct_rec.ovhd_account
6059     from mtl_fiscal_cat_accounts
6060    where legal_entity_id = i_ae_txn_rec.legal_entity_id
6061      and cost_type_id    = i_ae_txn_rec.cost_type_id
6062      and cost_group_id   = i_ae_txn_rec.cost_group_id
6063      and category_id     = i_ae_txn_rec.category_id;
6064 
6065    l_stmt_num := 22;
6066    SELECT nvl(max(primary_cost_method),-1)
6067     INTO  l_cost_method
6068    FROM cst_le_cost_types clct
6069     WHERE clct.legal_entity = i_ae_txn_rec.legal_entity_id
6070       AND clct.cost_type_id = i_ae_txn_rec.cost_type_id;
6071 
6072 -- Check if there are Cost Details.
6073 -- --------------------------------
6074   l_stmt_num := 25;
6075   select count(transaction_id) /* Bug 4586534 */
6076   into l_ele_exist
6077   from mtl_pac_actual_cost_details mpacd
6078   where mpacd.transaction_id = i_ae_txn_rec.transaction_id
6079   and mpacd.pac_period_id = i_ae_txn_rec.accounting_period_id
6080   and mpacd.cost_group_id = i_ae_txn_rec.cost_group_id
6081   and rownum < 2; /* 4586534 added rownum filter for perf as its only existence check */
6082 
6083   if (l_ele_exist = 0) then
6084     raise no_txn_det_error;
6085   end if;
6086 
6087 -- Get the total variance.
6088 -- -----------------------
6089   l_stmt_num := 27;
6090   select sum(nvl(variance_amount,0))
6091   into l_var_total
6092   from mtl_pac_actual_cost_details mpacd
6093   where mpacd.transaction_id = i_ae_txn_rec.transaction_id
6094   and mpacd.pac_period_id = i_ae_txn_rec.accounting_period_id
6095   and mpacd.cost_group_id = i_ae_txn_rec.cost_group_id;
6096 
6097 -- Find out if this is a value change cost update
6098 -- -----------------------------------------------
6099   l_stmt_num := 30;
6100   select count (1)
6101   into l_value_change_flag
6102   from mtl_pac_txn_cost_details mptcd
6103   where mptcd.transaction_id = i_ae_txn_rec.transaction_id
6104   and mptcd.pac_period_id = i_ae_txn_rec.accounting_period_id
6105   and mptcd.cost_group_id = i_ae_txn_rec.cost_group_id
6106   and mptcd.value_change is not null
6107   and rownum < 2;
6108 
6109   if(l_cost_method <> 3) then
6110     l_value_change_flag := 0;
6111   end if;
6112 -- For each Cost Element.
6113 -- ----------------------
6114   FOR cost_element IN 1..5 loop
6115 l_txn_ce_bal_account := -1; -- bug4586534 added new. initializing to use it for all cost elements.
6116 -- Get the difference and the variance.
6117 -- ------------------------------------
6118     l_stmt_num := 30;
6119     select decode (l_value_change_flag,
6120                    0, (sum(nvl(new_cost,0)) - sum(nvl(prior_cost,0))),
6121                    sum (nvl(actual_cost, 0))),
6122            sum(nvl(variance_amount,0)),
6123 	   sum(nvl(onhand_variance_amount,0))
6124     into l_cost,
6125          l_var,
6126 	 l_onhand_var
6127     from mtl_pac_actual_cost_details
6128     where transaction_id = i_ae_txn_rec.transaction_id
6129     and pac_period_id = i_ae_txn_rec.accounting_period_id
6130     and cost_group_id = i_ae_txn_rec.cost_group_id
6131     and cost_element_id = cost_element;
6132 
6133 
6134 -- Process only if exists cost for the element.
6135 -- --------------------------------------------
6136     if (l_cost is not null) then
6137       l_stmt_num := 32;
6138        -- Bug 4586534. Get the balancing account.
6139        IF cost_element = 1 THEN
6140          l_txn_ce_bal_account := i_ae_txn_rec.mat_account;
6141        ELSIF cost_element = 2 THEN
6142          l_txn_ce_bal_account := i_ae_txn_rec.mat_ovhd_account;
6143        ELSIF cost_element = 3 THEN
6144          l_txn_ce_bal_account := i_ae_txn_rec.res_account;
6145        ELSIF cost_element = 4 THEN
6146          l_txn_ce_bal_account := i_ae_txn_rec.osp_account;
6147        ELSIF cost_element = 5 THEN
6148          l_txn_ce_bal_account := i_ae_txn_rec.ovhd_account;
6149        END IF;
6150          /*Bug 4586534. Condition added to stop creating distributions for a cost element
6151          if associated cost is zero and account is -1 (not provided) */
6152       IF (l_cost <> 0 OR (l_cost=0 AND l_txn_ce_bal_account <> -1)) THEN
6153 -- Get the corresponding Cost Element Account.
6154 -- -------------------------------------------
6155         l_stmt_num := 35;
6156         l_ae_line_rec.account := CSTPAPHK.get_account_id (
6157                                         i_ae_txn_rec.transaction_id,
6158                                         i_ae_txn_rec.legal_entity_id,
6159                                         i_ae_txn_rec.cost_type_id,
6160                                         i_ae_txn_rec.cost_group_id,
6161                                         l_dr_flag,
6162                                         l_ae_line_rec.ae_line_type,
6163                                         cost_element,
6164                                         NULL,
6165                                         i_ae_txn_rec.subinventory_code,
6166                                         "FALSE", ----i_exp_flag
6167                                         l_err_rec.l_err_num,
6168                                         l_err_rec.l_err_code,
6169                                         l_err_rec.l_err_msg);
6170        -- check error
6171        if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
6172        raise process_error;
6173        end if;
6174 
6175        l_ae_line_rec.cost_element_id := cost_element;
6176 
6177        if (l_ae_line_rec.account = -1) then
6178           l_stmt_num := 40;
6179           /* Bug# 4586534. Replaced select statement from dual to PL/SQL based logic.
6180           select decode(cost_element, 1, l_acct_rec.mat_account,
6181                                       2, l_acct_rec.mat_ovhd_account,
6182                                       3, l_acct_rec.res_account,
6183                                       4, l_acct_rec.osp_account,
6184                                       5, l_acct_rec.ovhd_account)
6185           into l_ae_line_rec.account
6186           from dual;
6187           */
6188           IF cost_element = 1 THEN
6189             l_ae_line_rec.account := l_acct_rec.mat_account;
6190           ELSIF cost_element = 2 THEN
6191             l_ae_line_rec.account := l_acct_rec.mat_ovhd_account;
6192           ELSIF cost_element = 3 THEN
6193             l_ae_line_rec.account := l_acct_rec.res_account;
6194           ELSIF cost_element = 4 THEN
6195             l_ae_line_rec.account := l_acct_rec.osp_account;
6196           ELSIF cost_element = 5 THEN
6197             l_ae_line_rec.account := l_acct_rec.ovhd_account;
6198           END IF;
6199        end if;
6200 
6201 
6202 -- Create AE line.
6203 -- ---------------
6204        l_stmt_num := 45;
6205        l_ae_line_rec.ae_line_type := 1;
6206 /* Propagating fix for bug 2287547 */
6207        /*l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity) * l_cost;*/
6208 
6209        if (l_value_change_flag = 0) then
6210 	           if ( i_ae_txn_rec.primary_quantity*l_cost) >=0 then
6211  	              l_dr_flag := "TRUE";
6212  	           else
6213  	              l_dr_flag := "FALSE";
6214  	           end if;
6215        l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity * l_cost);
6216 	        else
6217 
6218  	           if (l_cost - l_var) >= 0 then
6219  	              l_dr_flag := "TRUE";
6220        else
6221               l_dr_flag := "FALSE";
6222  	           end if;
6223 
6224  	           l_ae_line_rec.transaction_value := abs(l_cost - l_var);
6225 
6226        end if;
6227 
6228        CSTPAPBR.insert_account (i_ae_txn_rec,
6229                                i_ae_curr_rec,
6230                                l_dr_flag,
6231                                l_ae_line_rec,
6232                                l_ae_line_tbl,
6233                                l_err_rec);
6234 
6235        -- check error
6236        if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
6237        raise process_error;
6238        end if;
6239 
6240 -- Get the corresponding MSI Elemental Account
6241 -- -------------------------------------------
6242        l_stmt_num := 47;
6243     /*  Bug 4586534. Modified to PL/SQL logic based instead of using select from dual and
6244     moved up (stmt 32) to include validation that no distribution for the cost element would be
6245     done if cost is zero and no account is provided for the cost element.
6246 
6247        select decode(cost_element, 1, i_ae_txn_rec.mat_account,
6248                                    2, i_ae_txn_rec.mat_ovhd_account,
6249                                    3, i_ae_txn_rec.res_account,
6250                                    4, i_ae_txn_rec.osp_account,
6251                                    5, i_ae_txn_rec.ovhd_account)
6252        into l_ae_line_rec.account
6253        from dual;
6254       */
6255     l_ae_line_rec.account := l_txn_ce_bal_account; -- added for bug# 4586534
6256 
6257 -- Create AE line.
6258 -- ---------------
6259        l_stmt_num := 50;
6260        l_ae_line_rec.ae_line_type := 2;
6261 /* Propagating fix for bug 2287547 */
6262        /* l_ae_line_rec.transaction_value := (abs(i_ae_txn_rec.primary_quantity) * l_cost) - l_var;*/
6263        if (l_value_change_flag = 0) then
6264  if ( (i_ae_txn_rec.primary_quantity * l_cost - l_var) < 0) then
6265  	              l_dr_flag := "TRUE";
6266        else
6267 				l_dr_flag := "FALSE";
6268  	           end if;
6269 
6270  	          l_ae_line_rec.transaction_value :=abs((i_ae_txn_rec.primary_quantity * l_cost) - l_var);
6271 
6272  	        else
6273 
6274  	           if (l_cost) <  0 then
6275  	               l_dr_flag := "TRUE";
6276  	           else
6277  	               l_dr_flag := "FALSE";
6278  	           end if;
6279 
6280  	          l_ae_line_rec.transaction_value := abs(l_cost);
6281        end if;
6282 
6283        CSTPAPBR.insert_account (i_ae_txn_rec,
6284                                i_ae_curr_rec,
6285                                l_dr_flag,
6286                                l_ae_line_rec,
6287                                l_ae_line_tbl,
6288                                l_err_rec);
6289 
6290        -- check error
6291        if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
6292        raise process_error;
6293        end if;
6294 
6295         ELSE
6296          fnd_file.put_line(fnd_file.log,'No distributions created for Cost element ' || cost_element || ' as the associated cost is zero and Account is -1');
6297         END IF; -- for l_cost <> 0 or (l_cost=0 and account id exists)
6298         IF(l_onhand_var <> 0) THEN
6299 	  if (l_onhand_var > 0) then
6300 	   l_dr_flag := "TRUE";
6301 	  else
6302           l_dr_flag := "FALSE";
6303           end if;
6304           l_ae_line_rec.ae_line_type := 20;
6305 	  l_stmt_num := 65;
6306           l_ae_line_rec.account := CSTPAPHK.get_account_id (
6307                                         i_ae_txn_rec.transaction_id,
6308                                         i_ae_txn_rec.legal_entity_id,
6309                                         i_ae_txn_rec.cost_type_id,
6310                                         i_ae_txn_rec.cost_group_id,
6311                                         l_dr_flag,
6312                                         l_ae_line_rec.ae_line_type,
6313                                         cost_element,
6314                                         NULL,
6315                                         i_ae_txn_rec.subinventory_code,
6316                                         "FALSE", ----i_exp_flag
6317                                         l_err_rec.l_err_num,
6318                                         l_err_rec.l_err_code,
6319                                         l_err_rec.l_err_msg);
6320           -- check error
6321          if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
6322           raise process_error;
6323          end if;
6324          if(l_ae_line_rec.account = -1) then
6325 	  l_ae_line_rec.account := i_ae_txn_rec.expense_account_id;
6326 	 end if;
6327          l_ae_line_rec.transaction_value := abs(l_onhand_var);
6328          l_stmt_num := 70;
6329          CSTPAPBR.insert_account (i_ae_txn_rec,
6330                                i_ae_curr_rec,
6331                                l_dr_flag,
6332                                l_ae_line_rec,
6333                                l_ae_line_tbl,
6334                                l_err_rec);
6335 	 if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
6336           raise process_error;
6337          end if;
6338 	END IF;
6339       ELSE
6340       l_loop_count := l_loop_count + 1;
6341       IF g_debug_flag = 'Y' THEN
6342           fnd_file.put_line(fnd_file.log,'No cost for element ....');
6343       END IF;
6344     END IF;
6345   END LOOP;
6346 
6347   if l_var_total <> 0 then
6348 
6349 -- Check if there are COCA Accounts.
6350 -- ---------------------------------
6351      l_stmt_num := 35;
6352      select count(legal_entity_id) /* Bug No. 4586534 */
6353      into l_acct_exist
6354      from cst_org_cost_group_accounts
6355      where legal_entity_id = i_ae_txn_rec.legal_entity_id
6356        and cost_type_id    = i_ae_txn_rec.cost_type_id
6357        and cost_group_id   = i_ae_txn_rec.cost_group_id;
6358      if (l_acct_exist = 0) then
6359        raise no_cg_acct_error;
6360      end if;
6361 
6362 /*Check if there was anything inserted into the adjustment accounts at all.If not then drive everything to variance and debit the Material account */
6363 
6364    If l_loop_count = 5 then
6365 
6366        If l_var_total < 0 then
6367         l_dr_flag := "TRUE";
6368        else
6369         l_dr_flag := "FALSE";
6370        end If;
6371 
6372        l_ae_line_rec.ae_line_type := 2;
6373        l_ae_line_rec.transaction_value := abs(l_var_total);
6374 
6375     /* bug 4586534. changed to PL/SQL logic based instead of select from dual
6376        select i_ae_txn_rec.mat_account
6377        into l_ae_line_rec.account
6378        from dual;
6379       */
6380       l_ae_line_rec.account := i_ae_txn_rec.mat_account;
6381        CSTPAPBR.insert_account (i_ae_txn_rec,
6382                                i_ae_curr_rec,
6383                                l_dr_flag,
6384                                l_ae_line_rec,
6385                                l_ae_line_tbl,
6386                                l_err_rec);
6387 
6388        /*l_dr_flag := not l_dr_flag;*/
6389 
6390        -- check error
6391        if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
6392        raise process_error;
6393        end if;
6394 
6395    End If; /* end of posting the debit to adjustment account */
6396 
6397 -- Get Cost Variance Account.
6398 -- --------------------------
6399      l_stmt_num := 40;
6400      select nvl(cost_variance_account,-1)
6401        into l_ae_line_rec.account
6402        from cst_org_cost_group_accounts
6403      where legal_entity_id = i_ae_txn_rec.legal_entity_id
6404        and cost_type_id    = i_ae_txn_rec.cost_type_id
6405        and cost_group_id   = i_ae_txn_rec.cost_group_id;
6406 
6407 -- Create AE line.
6408 -- ---------------
6409      l_stmt_num := 55;
6410 
6411      l_ae_line_rec.ae_line_type := 13;
6412      l_ae_line_rec.transaction_value := abs(l_var_total);
6413      if (l_var_total > 0) then
6414 	   l_dr_flag := "TRUE";
6415      else
6416          l_dr_flag := "FALSE";
6417      end if;
6418 
6419      CSTPAPBR.insert_account (i_ae_txn_rec,
6420                              i_ae_curr_rec,
6421                              l_dr_flag,
6422                              l_ae_line_rec,
6423                              l_ae_line_tbl,
6424                              l_err_rec);
6425 
6426      -- check error
6427      if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
6428      raise process_error;
6429      end if;
6430 
6431   end if;
6432   IF g_debug_flag = 'Y' THEN
6433     fnd_file.put_line(fnd_file.log,'Pcu_cost_txn <<');
6434   END IF;
6435 
6436 EXCEPTION
6437 
6438   when no_cg_acct_error then
6439   o_ae_err_rec.l_err_num := 30004;
6440   o_ae_err_rec.l_err_code := 'CST_PAC_NO_CG_ACCTS';
6441   FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_CG_ACCTS');
6442   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
6443 
6444   when no_mfca_acct_error then
6445   o_ae_err_rec.l_err_num := 30005;
6446   o_ae_err_rec.l_err_code := 'CST_PAC_NO_MFCA_ACCTS';
6447   FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_MFCA_ACCTS');
6448   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
6449 
6450   when no_txn_det_error then
6451   o_ae_err_rec.l_err_num := 30006;
6452   o_ae_err_rec.l_err_code := 'CST_NO_TXN_DET';
6453   FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
6454   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
6455 
6456   when process_error then
6457   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
6458   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
6459   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
6460 
6461   when others then
6462   o_ae_err_rec.l_err_num := SQLCODE;
6463   o_ae_err_rec.l_err_code := '';
6464   o_ae_err_rec.l_err_msg := 'CSTPAPBR.pcu_cost_txn' || to_char(l_stmt_num) ||
6465   substr(SQLERRM,1,180);
6466 
6467 END pcu_cost_txn;
6468 
6469 -- ===================================================================
6470 -- Rest of inventory transactions.
6471 -- ===================================================================
6472 procedure inv_cost_txn(
6473   i_ae_txn_rec                IN        CSTPALTY.cst_ae_txn_rec_type,
6474   i_ae_curr_rec               IN        CSTPALTY.cst_ae_curr_rec_type,
6475   l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
6476   o_ae_err_rec                OUT NOCOPY        CSTPALTY.cst_ae_err_rec_type
6477 ) IS
6478   l_exp_sub                             NUMBER;
6479   l_dropship_type_code      NUMBER;
6480   l_elemental                             NUMBER;
6481   l_acct_line_type                      NUMBER;
6482   l_ovhd_absp                             NUMBER;
6483   l_mat_ovhd_exists                     NUMBER;
6484   l_stmt_num                              NUMBER;
6485   l_dr_flag                             BOOLEAN;
6486   l_exp_flag                            BOOLEAN;
6487   process_error                           EXCEPTION;
6488   l_err_rec                             CSTPALTY.cst_ae_err_rec_type;
6489   l_acct_rec                            CSTPALTY.cst_ae_acct_rec_type;
6490   l_count_so                            NUMBER;
6491   l_ref_om_line_id          NUMBER; -- Revenue / COGS Matching
6492 BEGIN
6493 
6494   IF g_debug_flag = 'Y' THEN
6495     fnd_file.put_line(fnd_file.log,'Inv_cost_txn <<');
6496   END IF;
6497 -- Initialize variables.
6498 -- ---------------------
6499   l_err_rec.l_err_num := 0;
6500   l_err_rec.l_err_code := '';
6501   l_err_rec.l_err_msg := '';
6502 
6503 -- Figure out expense subinventory.
6504 -- --------------------------------
6505   l_stmt_num := 10;
6506 
6507   select decode(asset_inventory,1,0,1)
6508   into l_exp_sub
6509   from mtl_secondary_inventories
6510   where secondary_inventory_name = i_ae_txn_rec.subinventory_code
6511   and organization_id = i_ae_txn_rec.organization_id;
6512 
6513   -- No accounting entries posted for any transaction involving an expense
6514   -- item or expense subinventory except in the case of PO receipt, PO
6515   -- return and PO delivery adjustments.
6516 
6517   if ((i_ae_txn_rec.exp_item = 1 or l_exp_sub = 1) and (i_ae_txn_rec.txn_src_type_id <>1)) then
6518     IF g_debug_flag = 'Y' THEN
6519       fnd_file.put_line(fnd_file.log,'Expense item or expense sub - No accounting ');
6520     END IF;
6521     return;
6522   end if;
6523 
6524   if ( l_exp_sub = 1 OR i_ae_txn_rec.exp_item = 1 ) then
6525     l_exp_flag := "TRUE";
6526     -- Expense account is derived in the Inventory_Accounts procedure
6527     l_acct_rec.account := -1;
6528 /* Bug 3123936: Propogation of 2896193 */
6529   else
6530     l_exp_flag := "FALSE";
6531     l_acct_rec.account := -1;
6532   end if;
6533 
6534 -- First post to the elemental inventory accounts.
6535 -- -----------------------------------------------
6536   if ((i_ae_txn_rec.txn_action_id = 27) or
6537       (i_ae_txn_rec.txn_action_id = 6 and i_ae_txn_rec.txn_src_type_id = 1)) then
6538      l_dr_flag := "TRUE";
6539   elsif ((i_ae_txn_rec.txn_action_id = 1) or
6540          (i_ae_txn_rec.txn_action_id = 6 and i_ae_txn_rec.txn_src_type_id = 13)) then
6541      l_dr_flag := "FALSE";
6542   elsif (i_ae_txn_rec.txn_action_id in (29,4,8)) then
6543      if i_ae_txn_rec.primary_quantity < 0 then
6544         l_dr_flag := "FALSE";
6545      elsif i_ae_txn_rec.primary_quantity > 0 then
6546         l_dr_flag := "TRUE";
6547      end if;
6548   end if;
6549 
6550   inventory_accounts(i_ae_txn_rec,
6551                      i_ae_curr_rec,
6552                      l_exp_flag,
6553                      l_acct_rec.account,
6554                      l_dr_flag,
6555                      l_ae_line_tbl,
6556                      l_err_rec);
6557   -- check error
6558   if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
6559     raise process_error;
6560   end if;
6561 
6562 -- Toggle the Debit Flag.
6563 -- ----------------------
6564   l_dr_flag := not l_dr_flag;
6565 
6566 -- Get acct_line_type, elemental flag, ovhd flag and acct_id.
6567 -- ----------------------------------------------------------
6568   l_acct_rec.mat_account := '';
6569   l_acct_rec.mat_ovhd_account := '';
6570   l_acct_rec.res_account := '';
6571   l_acct_rec.osp_account := '';
6572   l_acct_rec.ovhd_account := '';
6573 
6574   if (i_ae_txn_rec.txn_action_id in (4,8)) then
6575      IF g_debug_flag = 'Y' THEN
6576        fnd_file.put_line(fnd_file.log,'Cycle Count or Physical Inv Adjustment');
6577      END IF;
6578 
6579     /* cycle count adjustment and physical inventory adjustment */
6580     /* Use the distribution account id as stated in mmt. */
6581 
6582     l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
6583     l_acct_line_type := 2;
6584     l_elemental := 0;
6585     l_ovhd_absp := 0;
6586 
6587   elsif (i_ae_txn_rec.txn_action_id in (1,27,29) and i_ae_txn_rec.txn_src_type_id = 1) then
6588 
6589     /* PO Issue, Receipt, delivery adjustments. */
6590     IF g_debug_flag = 'Y' THEN
6591       fnd_file.put_line(fnd_file.log,'PO Issue, Receipt, delivery adjustments');
6592     END IF;
6593 
6594     l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
6595     l_elemental := 0;
6596     l_ovhd_absp := 1;
6597 
6598     /* Patchset J change: All External Drop ship PO Receipts will use the Clearing
6599     Account */
6600 
6601     select nvl(rt.dropship_type_code, 3)
6602     into l_dropship_type_code
6603     from rcv_transactions rt, mtl_material_transactions mmt
6604     where mmt.rcv_transaction_id = rt.transaction_id
6605     and mmt.transaction_id = i_ae_txn_rec.transaction_id;
6606 
6607     if (l_dropship_type_code = 1 or l_dropship_type_code = 2) then
6608      l_acct_line_type := 31; -- Clearing
6609     else
6610      l_acct_line_type := 5;
6611     end if;
6612 
6613   elsif (i_ae_txn_rec.txn_action_id in (1,27) and i_ae_txn_rec.txn_src_type_id in (2,12)) then
6614     l_elemental := 1;
6615     l_ovhd_absp := 0;
6616 
6617     l_stmt_num := 30;
6618    -- For RMAs only:
6619     -- Get the original sales order issue OM line ID, and check
6620     -- whether the original sales order issue was inserted into
6621     -- the Revenue / COGS Matching data model.
6622     SELECT max(ool.reference_line_id)
6623     INTO l_ref_om_line_id
6624     FROM oe_order_lines_all ool,
6625          cst_revenue_cogs_match_lines crcml
6626     WHERE ool.line_id = i_ae_txn_rec.om_line_id
6627     AND   ool.reference_line_id = crcml.cogs_om_line_id
6628     AND   crcml.pac_cost_type_id = i_ae_txn_rec.cost_type_id
6629     AND   i_ae_txn_rec.txn_src_type_id = 12;
6630 
6631     --Bug 16707186 fix
6632     IF l_ref_om_line_id IS NOT NULL THEN
6633 
6634       SELECT count(*)
6635         INTO l_count_so
6636 	FROM   mtl_material_transactions MMT,
6637            mtl_pac_actual_cost_details MPACD
6638          WHERE (MMT.transaction_action_id = 1 OR MMT.transaction_action_id = 7)
6639          AND    MMT.transaction_source_type_id = 2
6640          AND    EXISTS (SELECT 1
6641                 FROM cst_cost_group_assignments CCGA
6642                WHERE CCGA.cost_group_id = i_ae_txn_rec.cost_group_id
6643                  AND CCGA.organization_id = MMT.organization_id)
6644          AND    MMT.inventory_item_id = i_ae_txn_rec.inventory_item_id
6645          AND    MMT.trx_source_line_id = l_ref_om_line_id
6646          AND    MPACD.transaction_id = MMT.transaction_id
6647          AND    MPACD.cost_type_id = i_ae_txn_rec.cost_type_id;
6648 
6649     END IF;
6650 
6651     IF (i_ae_txn_rec.txn_src_type_id = 12 AND l_ref_om_line_id IS NOT NULL AND l_count_so <> 0) then
6652        /* RMAs with Deferred COGS */
6653     IF g_debug_flag = 'Y' THEN
6654          fnd_file.put_line(fnd_file.log,'RMA with Deferred COGS');
6655        END IF;
6656 
6657        l_stmt_num := 35;
6658        -- Create the credit distributions to COGS and/or Deferred COGS
6659        CST_RevenueCogsMatch_PVT.Process_PacRmaReceipt(
6660                   i_ae_txn_rec,
6661                   i_ae_curr_rec,
6662                   l_dr_flag,
6663                   l_ref_om_line_id,
6664                   l_ae_line_tbl,
6665                   l_err_rec);
6666 
6667        -- check error
6668        if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
6669           raise process_error;
6670        end if;
6671 
6672        -- Processing is complete for these RMAs, go to end of procedure
6673        GOTO inv_cost_txn_end;
6674     ELSIF (i_ae_txn_rec.txn_src_type_id = 2 AND i_ae_txn_rec.so_issue_acct_type = 2) THEN
6675 
6676        /* Sales order issue with Deferred COGS */
6677        IF g_debug_flag = 'Y' THEN
6678           fnd_file.put_line(fnd_file.log,'Sales order issue ');
6679        END IF;
6680 
6681        l_stmt_num := 40;
6682        -- Get the Deferred COGS account
6683        SELECT deferred_cogs_acct_id
6684        INTO l_acct_rec.account
6685        FROM cst_revenue_cogs_match_lines
6686        WHERE cogs_om_line_id = i_ae_txn_rec.om_line_id
6687        AND  pac_cost_type_id = i_ae_txn_rec.cost_type_id;
6688 
6689        l_acct_rec.mat_account := l_acct_rec.account;
6690        l_acct_rec.mat_ovhd_account := l_acct_rec.account;
6691        l_acct_rec.res_account := l_acct_rec.account;
6692        l_acct_rec.osp_account := l_acct_rec.account;
6693        l_acct_rec.ovhd_account := l_acct_rec.account;
6694 
6695        l_acct_line_type := 36; -- Deferred COGS line type
6696 
6697     ELSE
6698        /* Sales orders and RMAs with no deferred COGS */
6699        IF g_debug_flag = 'Y' THEN
6700          fnd_file.put_line(fnd_file.log,'Sales order, RMA and Rejection of RMA ');
6701     END IF;
6702 
6703     l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
6704     if (l_acct_rec.account = -1) then
6705 
6706          l_stmt_num := 45;
6707 
6708       select nvl(msi.cost_of_sales_account, mp.cost_of_sales_account)
6709       into l_acct_rec.account
6710       from mtl_system_items msi,
6711            mtl_parameters mp
6712       where msi.organization_id = i_ae_txn_rec.organization_id
6713       and msi.inventory_item_id = i_ae_txn_rec.inventory_item_id
6714       and mp.organization_id = msi.organization_id;
6715 
6716     end if;
6717 
6718     l_stmt_num := 50;
6719 
6720     l_acct_rec.mat_account := l_acct_rec.account;
6721     l_acct_rec.mat_ovhd_account := l_acct_rec.account;
6722     l_acct_rec.res_account := l_acct_rec.account;
6723     l_acct_rec.osp_account := l_acct_rec.account;
6724     l_acct_rec.ovhd_account := l_acct_rec.account;
6725 
6726        l_acct_line_type := 35;
6727     END IF;
6728 
6729   elsif (i_ae_txn_rec.txn_action_id in (1,27,29) and i_ae_txn_rec.txn_src_type_id = 3) then
6730 
6731     /* Account */
6732     IF g_debug_flag = 'Y' THEN
6733       fnd_file.put_line(fnd_file.log,'Account txn');
6734     END IF;
6735 
6736     l_acct_rec.account := i_ae_txn_rec.txn_src_id;
6737     l_acct_line_type := 2;
6738     l_elemental := 0;
6739     l_ovhd_absp := 0;
6740 
6741   elsif (i_ae_txn_rec.txn_action_id in (1,27,29) and i_ae_txn_rec.txn_src_type_id = 6) then
6742 
6743     /* Account Alias*/
6744     IF g_debug_flag = 'Y' THEN
6745       fnd_file.put_line(fnd_file.log,'Account Alias txn ');
6746     END IF;
6747 
6748     l_stmt_num := 60;
6749 
6750     select distribution_account
6751     into l_acct_rec.account
6752     from mtl_generic_dispositions
6753     where disposition_id = i_ae_txn_rec.txn_src_id
6754     and organization_id = i_ae_txn_rec.organization_id;
6755 
6756     l_acct_line_type := 2;
6757     l_elemental := 0;
6758     l_ovhd_absp := 0;
6759 
6760   elsif (i_ae_txn_rec.txn_action_id = 6 and i_ae_txn_rec.txn_src_type_id in (1, 13)) then
6761 
6762     /* Transfer to regular/consigned */
6763     IF g_debug_flag = 'Y' THEN
6764       fnd_file.put_line(fnd_file.log,'Transfer to regular/consigned');
6765     END IF;
6766 
6767     l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
6768     l_acct_line_type := 16;
6769     l_elemental := 0;
6770     l_ovhd_absp := 1;
6771 
6772   else
6773 
6774     /* Misc issue, receipt, default*/
6775     IF g_debug_flag = 'Y' THEN
6776       fnd_file.put_line(fnd_file.log,'Misc issue, receipt');
6777     END IF;
6778 
6779     l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
6780     l_acct_line_type := 2;
6781     l_elemental := 0;
6782     l_ovhd_absp := 0;
6783 
6784   end if;
6785 
6786 
6787 -- Then post to the offsetting accounts.
6788 -- -------------------------------------
6789   offset_accounts(i_ae_txn_rec,
6790                   i_ae_curr_rec,
6791                   l_acct_line_type,
6792                   l_elemental,
6793                   l_ovhd_absp,
6794                   l_dr_flag,
6795                   l_acct_rec,
6796                   l_ae_line_tbl,
6797                   l_err_rec);
6798   -- check error
6799   if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
6800      raise process_error;
6801   end if;
6802 
6803 
6804 -- If ovhd flag is on then call ovhd procedure.
6805 -- --------------------------------------------
6806   l_stmt_num := 70;
6807   if (l_ovhd_absp = 1) then
6808      select count(transaction_id) /* Changed for the bug No . 4586534 */
6809     into l_mat_ovhd_exists
6810     from mtl_pac_actual_cost_details
6811     where transaction_id = i_ae_txn_rec.transaction_id
6812     and pac_period_id = i_ae_txn_rec.accounting_period_id
6813     and cost_group_id = i_ae_txn_rec.cost_group_id
6814     and cost_element_id = 2
6815     and level_type = 1;
6816 
6817     if (l_mat_ovhd_exists > 0) then
6818        ovhd_accounts(i_ae_txn_rec,
6819                      i_ae_curr_rec,
6820                      l_dr_flag,
6821                      l_ae_line_tbl,
6822                      l_err_rec);
6823       -- check error
6824       if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
6825         raise process_error;
6826       end if;
6827 
6828 
6829     end if;
6830   end if;
6831 
6832 <<inv_cost_txn_end>>
6833   IF g_debug_flag = 'Y' THEN
6834     fnd_file.put_line(fnd_file.log,'Inv_cost_txn >> ');
6835   END IF;
6836 
6837 EXCEPTION
6838 
6839   when process_error then
6840   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
6841   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
6842   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
6843 
6844   when others then
6845   o_ae_err_rec.l_err_num := SQLCODE;
6846   o_ae_err_rec.l_err_code := '';
6847   o_ae_err_rec.l_err_msg := 'CSTPAPBR.inv_cost_txn' || to_char(l_stmt_num) ||
6848   substr(SQLERRM,1,180);
6849 
6850 END inv_cost_txn;
6851 
6852 
6853 -- ===================================================================
6854 -- Logical Transactions
6855 -- This procedure accounts for the logical transaction
6856 --
6857 -- 20-Jul-03   Anju   Creation
6858 -- ===================================================================
6859 procedure cost_logical_txn(
6860   i_ae_txn_rec                IN        CSTPALTY.cst_ae_txn_rec_type,
6861   i_ae_curr_rec               IN        CSTPALTY.cst_ae_curr_rec_type,
6862   l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
6863   o_ae_err_rec                OUT NOCOPY        CSTPALTY.cst_ae_err_rec_type
6864  ) IS
6865   l_exp_sub                             NUMBER;
6866   l_exp_acct                              NUMBER;
6867   l_elemental                             NUMBER;
6868   l_acct_line_type                      NUMBER;
6869   l_ovhd_absp                             NUMBER;
6870   l_stmt_num                              NUMBER;
6871   l_dr_flag                             BOOLEAN;
6872   l_exp_flag                            BOOLEAN;
6873   process_error                           EXCEPTION;
6874   l_err_rec                             CSTPALTY.cst_ae_err_rec_type;
6875   l_acct_rec                            CSTPALTY.cst_ae_acct_rec_type;
6876 
6877   l_ref_om_line_id          NUMBER; -- Revenue / COGS Matching
6878   /* Bug12914738 */
6879   l_acc_value NUMBER;
6880   l_ae_line_rec_std_var             CSTPALTY.cst_ae_line_rec_type;
6881   l_std_cost_adj_var_acct NUMBER;
6882   /* Bug12914738 */
6883 BEGIN
6884 
6885   IF g_debug_flag = 'Y' THEN
6886     fnd_file.put_line(fnd_file.log,'Cost_logical_txn <<');
6887   END IF;
6888 -- Initialize variables.
6889 -- ---------------------
6890   l_err_rec.l_err_num := 0;
6891   l_err_rec.l_err_code := '';
6892   l_err_rec.l_err_msg := '';
6893 
6894 -- First process the COGS Recognition Transaction since it does not hit Inventory accounts
6895 -- ---------------------------------------------------------------------------------------
6896   l_stmt_num := 5;
6897   IF (i_ae_txn_rec.txn_src_type_id = 2 AND i_ae_txn_rec.txn_action_id = 36) THEN
6898      CST_RevenueCogsMatch_PVT.Process_PacCogsRecTxn(
6899                                   i_ae_txn_rec,
6900                                   i_ae_curr_rec,
6901                                   l_ae_line_tbl,
6902                                   l_err_rec);
6903      -- check error
6904      if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
6905         raise process_error;
6906      else
6907         GOTO cost_logical_txn_end; -- End of processing for COGS Recognition Txns
6908      end if;
6909 
6910   END IF;
6911 
6912 
6913 -- Figure out expense subinventory.
6914 -- --------------------------------
6915   l_stmt_num := 10;
6916 
6917   if (i_ae_txn_rec.subinventory_code is not null) then
6918 
6919     select decode(asset_inventory,1,0,1)
6920     into l_exp_sub
6921     from mtl_secondary_inventories
6922     where secondary_inventory_name = i_ae_txn_rec.subinventory_code
6923     and organization_id = i_ae_txn_rec.organization_id;
6924   else
6925     l_exp_sub := 0;
6926   end if;
6927 
6928   -- No accounting entries posted for an expense
6929   -- item or expense subinventory against a logical SO Issue or
6930   -- Logical RMA Receipt
6931 
6932   if ((i_ae_txn_rec.exp_item = 1 or l_exp_sub = 1) and
6933        ((i_ae_txn_rec.txn_action_id = 26 and i_ae_txn_rec.txn_src_type_id = 12) OR
6934         (i_ae_txn_rec.txn_action_id = 7 and i_ae_txn_rec.txn_src_type_id = 2))) then
6935     IF g_debug_flag = 'Y' THEN
6936       fnd_file.put_line(fnd_file.log,'Expense item or expense sub - No accounting ');
6937     END IF;
6938     return;
6939   end if;
6940 
6941   if ( l_exp_sub = 1 OR i_ae_txn_rec.exp_item = 1 )  then
6942     l_exp_flag := "TRUE";
6943     -- Expense account is derived in the Inventory_Accounts procedure
6944     l_acct_rec.account := -1;
6945   else
6946     /* Bug 3123936: Prop og 2896193 */
6947     l_exp_flag := "FALSE";
6948     l_acct_rec.account := -1;
6949   end if;
6950 
6951 -- First post to the elemental inventory accounts.
6952 -- -----------------------------------------------
6953 -- for all logical transactions, -ve qty means a credit to Inventory
6954   if (i_ae_txn_rec.primary_quantity < 0) then
6955      l_dr_flag := "FALSE";
6956   else
6957      l_dr_flag := "TRUE";
6958   end if;
6959 
6960   inventory_accounts(i_ae_txn_rec,
6961                      i_ae_curr_rec,
6962                      l_exp_flag,
6963                      l_acct_rec.account,
6964                      l_dr_flag,
6965                      l_ae_line_tbl,
6966                      l_err_rec);
6967   -- check error
6968   if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
6969     raise process_error;
6970   end if;
6971 
6972 
6973 -- Toggle the Debit Flag.
6974 -- ----------------------
6975   l_dr_flag := not l_dr_flag;
6976 
6977 -- Get acct_line_type, elemental flag, ovhd flag and acct_id.
6978 -- ----------------------------------------------------------
6979   l_acct_rec.mat_account := '';
6980   l_acct_rec.mat_ovhd_account := '';
6981   l_acct_rec.res_account := '';
6982   l_acct_rec.osp_account := '';
6983   l_acct_rec.ovhd_account := '';
6984 
6985   l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
6986   l_ovhd_absp := 0;
6987 
6988   if (i_ae_txn_rec.txn_type_id in (19,39,69,22,23)) then
6989     l_acct_line_type := 31; -- clearing line type
6990     l_elemental := 0;
6991 
6992   elsif (i_ae_txn_rec.txn_type_id in (11,14))then
6993     l_acct_line_type := 2; -- account line type (I/C COGS)
6994     l_elemental := 1;
6995 
6996     l_acct_rec.mat_account := l_acct_rec.account;
6997     l_acct_rec.mat_ovhd_account := l_acct_rec.account;
6998     l_acct_rec.res_account := l_acct_rec.account;
6999     l_acct_rec.osp_account := l_acct_rec.account;
7000     l_acct_rec.ovhd_account := l_acct_rec.account;
7001 
7002   elsif (i_ae_txn_rec.txn_type_id in (10,13))then
7003     l_acct_line_type := 16; -- accrual line type (I/C accrual)
7004     l_elemental := 0;
7005   elsif (i_ae_txn_rec.txn_type_id in (30,16)) then -- Logical SO Issue or Logical RMA Receipt
7006 
7007     l_stmt_num := 30;
7008     -- For RMAs only:
7009     -- Get the original sales order issue OM line ID, and check
7010     -- whether the original sales order issue was inserted into
7011     -- the Revenue / COGS Matching data model.
7012     SELECT max(ool.reference_line_id)
7013     INTO l_ref_om_line_id
7014     FROM oe_order_lines_all ool,
7015          cst_revenue_cogs_match_lines crcml
7016     WHERE ool.line_id = i_ae_txn_rec.om_line_id
7017     AND   ool.reference_line_id = crcml.cogs_om_line_id
7018     AND   crcml.pac_cost_type_id = i_ae_txn_rec.cost_type_id
7019     AND   i_ae_txn_rec.txn_src_type_id = 12;
7020 
7021     IF (i_ae_txn_rec.txn_type_id = 16 AND l_ref_om_line_id IS NOT NULL) then
7022 
7023        /* RMAs with Deferred COGS */
7024        IF g_debug_flag = 'Y' THEN
7025          fnd_file.put_line(fnd_file.log,'RMA with Deferred COGS');
7026        END IF;
7027 
7028        l_stmt_num := 40;
7029        -- Create the credit distributions to COGS and/or Deferred COGS
7030        CST_RevenueCogsMatch_PVT.Process_PacRmaReceipt(
7031                   i_ae_txn_rec,
7032                   i_ae_curr_rec,
7033                   l_dr_flag,
7034                   l_ref_om_line_id,
7035                   l_ae_line_tbl,
7036                   l_err_rec);
7037 
7038        -- check error
7039        if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
7040           raise process_error;
7041        end if;
7042        /* Bug12914738 */
7043        /* Creating the Std Cost adjustment Variance accounting in the case tranfer price has changed
7044           between SO issue and RMA */
7045        if (l_ae_line_tbl.exists(1)) then
7046           l_stmt_num := 41;
7047 	  l_acc_value := 0;
7048           For i in l_ae_line_tbl.FIRST .. l_ae_line_tbl.LAST loop
7049               l_acc_value := l_acc_value + nvl(l_ae_line_tbl(i).accounted_dr,0) - nvl(l_ae_line_tbl(i).accounted_cr,0);
7050           end loop;
7051 
7052          if (l_acc_value <> 0) then
7053             l_ae_line_rec_std_var.transaction_value := abs(l_acc_value);
7054 	    l_ae_line_rec_std_var.resource_id := NULL;
7055             l_ae_line_rec_std_var.cost_element_id := null;
7056             l_ae_line_rec_std_var.ae_line_type := 37;
7057 	    if (l_acc_value < 0 ) then /*This is offsetting accounting so reverse */
7058               l_dr_flag := "TRUE";
7059 	    else
7060 	      l_dr_flag := "FALSE";
7061 	    end if;
7062             l_stmt_num := 45;
7063 	     l_std_cost_adj_var_acct:= CSTPAPHK.get_account_id (
7064                                         i_ae_txn_rec.transaction_id,
7065                                         i_ae_txn_rec.legal_entity_id,
7066                                         i_ae_txn_rec.cost_type_id,
7067                                         i_ae_txn_rec.cost_group_id,
7068                                         l_dr_flag,
7069                                         l_ae_line_rec_std_var.ae_line_type,
7070                                         NULL,
7071                                         NULL,
7072                                         i_ae_txn_rec.subinventory_code,
7073                                         "FALSE",
7074                                         l_err_rec.l_err_num,
7075                                         l_err_rec.l_err_code,
7076                                         l_err_rec.l_err_msg);
7077 	    if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7078                 raise process_error;
7079             end if;
7080 
7081 	    IF (l_std_cost_adj_var_acct = -1) THEN
7082 	    l_stmt_num := 47;
7083 	      select NVL(b.cost_of_sales_account,-1)
7084                 into l_std_cost_adj_var_acct
7085                from mtl_parameters b
7086               where organization_id = i_ae_txn_rec.organization_id;
7087             END IF;
7088 
7089 	    l_ae_line_rec_std_var.account := l_std_cost_adj_var_acct;
7090             l_stmt_num := 49;
7091 
7092 	    CSTPAPBR.insert_account (i_ae_txn_rec,
7093                          i_ae_curr_rec,
7094                          l_dr_flag,
7095                          l_ae_line_rec_std_var,
7096                          l_ae_line_tbl,
7097                          l_err_rec);
7098          -- check error
7099              if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7100                 raise process_error;
7101              end if;
7102 
7103          end if;/*End if l_acc_value <> 0 */
7104 	end if;
7105        /* END: Creating the Std Cost adjustment Variance accounting in the case tranfer price has changed
7106           between SO issue and RMA */
7107        /* Bug12914738 */
7108 
7109        -- Processing is complete for these RMAs, go to end of procedure
7110        GOTO cost_logical_txn_end;
7111     ELSIF (i_ae_txn_rec.txn_type_id = 30 AND i_ae_txn_rec.so_issue_acct_type = 2) THEN
7112 
7113        /* Sales order issue with Deferred COGS */
7114        IF g_debug_flag = 'Y' THEN
7115           fnd_file.put_line(fnd_file.log,'Sales order issue ');
7116        END IF;
7117 
7118        l_stmt_num := 50;
7119        -- Get the Deferred COGS account
7120        SELECT deferred_cogs_acct_id
7121        INTO l_acct_rec.account
7122        FROM cst_revenue_cogs_match_lines
7123        WHERE cogs_om_line_id = i_ae_txn_rec.om_line_id
7124        AND  pac_cost_type_id = i_ae_txn_rec.cost_type_id;
7125 
7126        l_acct_line_type := 36; -- Deferred COGS line type
7127 
7128     ELSE
7129        /* Logical Sales orders and RMAs with no deferred COGS */
7130        IF g_debug_flag = 'Y' THEN
7131          fnd_file.put_line(fnd_file.log,'Logical Sales order, RMA and Rejection of RMA ');
7132        END IF;
7133 
7134        l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
7135        l_acct_line_type := 35;
7136 
7137     END IF;
7138 
7139     l_acct_rec.mat_account := l_acct_rec.account;
7140     l_acct_rec.mat_ovhd_account := l_acct_rec.account;
7141     l_acct_rec.res_account := l_acct_rec.account;
7142     l_acct_rec.osp_account := l_acct_rec.account;
7143     l_acct_rec.ovhd_account := l_acct_rec.account;
7144 
7145     l_elemental := 1;
7146 
7147   else
7148     -- invalid transaction type
7149     raise process_error;
7150   end if;
7151 
7152 -- Then post to the offsetting accounts.
7153 -- -------------------------------------
7154   offset_accounts(i_ae_txn_rec,
7155                   i_ae_curr_rec,
7156                   l_acct_line_type,
7157                   l_elemental,
7158                   l_ovhd_absp,
7159                   l_dr_flag,
7160                   l_acct_rec,
7161                   l_ae_line_tbl,
7162                   l_err_rec);
7163   -- check error
7164   if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
7165      raise process_error;
7166   end if;
7167 
7168 <<cost_logical_txn_end>>
7169 
7170   IF g_debug_flag = 'Y' THEN
7171     fnd_file.put_line(fnd_file.log,'cost_logical_txn out ');
7172   END IF;
7173 
7174 EXCEPTION
7175 
7176   when process_error then
7177   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
7178   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
7179   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
7180 
7181   when others then
7182   o_ae_err_rec.l_err_num := SQLCODE;
7183   o_ae_err_rec.l_err_code := '';
7184   o_ae_err_rec.l_err_msg := 'CSTPAPBR.cost_logical_txn' ||
7185                     to_char(l_stmt_num) || substr(SQLERRM,1,180);
7186 
7187 END cost_logical_txn;
7188 
7189 
7190 -- ===================================================================
7191 -- Consigned Price Update Transaction
7192 -- This procedure accounts for consigned price update transaction
7193 --
7194 -- 20-Jul-03   Anju   Creation
7195 -- ===================================================================
7196 procedure cost_consigned_update_txn(
7197   i_ae_txn_rec                IN        CSTPALTY.cst_ae_txn_rec_type,
7198   i_ae_curr_rec               IN        CSTPALTY.cst_ae_curr_rec_type,
7199   l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
7200   o_ae_err_rec                OUT NOCOPY        CSTPALTY.cst_ae_err_rec_type
7201  ) IS
7202   l_exp_sub                             NUMBER;
7203   l_qty                     NUMBER;
7204   l_exp_acct                              NUMBER;
7205   l_ovhd_absp               NUMBER;
7206   l_elemental                             NUMBER;
7207   l_acct_line_type          NUMBER;
7208   l_stmt_num                              NUMBER;
7209   l_dr_flag                 BOOLEAN;
7210   l_ele_exist               NUMBER;
7211   l_cost                    NUMBER;
7212   process_error                           EXCEPTION;
7213   no_txn_det_error          EXCEPTION;
7214   l_err_rec                 CSTPALTY.cst_ae_err_rec_type;
7215   l_acct_rec                CSTPALTY.cst_ae_acct_rec_type;
7216   l_ae_line_rec             CSTPALTY.cst_ae_line_rec_type;
7217 
7218 BEGIN
7219 
7220   IF g_debug_flag = 'Y' THEN
7221     fnd_file.put_line(fnd_file.log,'Cost_consigned_txn <<');
7222   END IF;
7223 -- Initialize variables.
7224 -- ---------------------
7225   l_err_rec.l_err_num := 0;
7226   l_err_rec.l_err_code := '';
7227   l_err_rec.l_err_msg := '';
7228 
7229 --consigned price update transaction quantity is stored in mmt.quantity_adjusted
7230 --update i_ae_txn_rec.
7231 
7232   select nvl(quantity_adjusted, 0)
7233   into l_qty
7234   from mtl_material_transactions
7235   where transaction_id = i_ae_txn_rec.transaction_id;
7236 
7237 -- Figure out Retroactive price update account.
7238 -- ---------------------------------------------
7239   l_stmt_num := 10;
7240 
7241   select nvl(retroprice_adj_account_id,-1)
7242   into l_acct_rec.account
7243   from rcv_parameters
7244   where organization_id = i_ae_txn_rec.organization_id;
7245 
7246   if (i_ae_txn_rec.primary_quantity < 0) then
7247      l_dr_flag := "FALSE";
7248   else
7249      l_dr_flag := "TRUE";
7250   end if;
7251 
7252   l_acct_line_type := 31;
7253 
7254   select count(*)
7255   into l_ele_exist
7256   from mtl_pac_actual_cost_details mpacd
7257   where mpacd.transaction_id = i_ae_txn_rec.transaction_id
7258   and mpacd.pac_period_id = i_ae_txn_rec.accounting_period_id
7259   and mpacd.cost_group_id = i_ae_txn_rec.cost_group_id;
7260 
7261   if (l_ele_exist = 0) then
7262     raise no_txn_det_error;
7263   end if;
7264 
7265   l_stmt_num := 40;
7266 
7267   select sum(nvl(actual_cost,0))
7268   into l_cost
7269   from mtl_pac_actual_cost_details
7270   where transaction_id = i_ae_txn_rec.transaction_id
7271   and pac_period_id = i_ae_txn_rec.accounting_period_id
7272   and cost_group_id = i_ae_txn_rec.cost_group_id;
7273 
7274   l_stmt_num := 56;
7275 
7276   l_ae_line_rec.transaction_value := abs(l_qty) * l_cost;
7277   l_ae_line_rec.resource_id := NULL;
7278   l_ae_line_rec.cost_element_id := 1;
7279   l_ae_line_rec.ae_line_type := 31;
7280   l_ae_line_rec.account := l_acct_rec.account;
7281 
7282 
7283   insert_account (i_ae_txn_rec,
7284                   i_ae_curr_rec,
7285                   l_dr_flag,
7286                   l_ae_line_rec,
7287                   l_ae_line_tbl,
7288                   l_err_rec);
7289 
7290 
7291 
7292       -- check error
7293       if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7294       raise process_error;
7295       end if;
7296 
7297 -- Toggle the Debit Flag.
7298 -- ----------------------
7299   l_dr_flag := not l_dr_flag;
7300 
7301 -- Get acct_line_type, elemental flag, ovhd flag and acct_id.
7302 -- ----------------------------------------------------------
7303   l_acct_rec.mat_account := '';
7304   l_acct_rec.mat_ovhd_account := '';
7305   l_acct_rec.res_account := '';
7306   l_acct_rec.osp_account := '';
7307   l_acct_rec.ovhd_account := '';
7308 
7309   l_acct_rec.account := i_ae_txn_rec.dist_acct_id;
7310   l_elemental := 0;
7311   l_ovhd_absp := 0;
7312   l_acct_line_type := 16;
7313 
7314 -- Then post to the offsetting accounts.
7315 -- -------------------------------------
7316   offset_accounts(i_ae_txn_rec,
7317                   i_ae_curr_rec,
7318                   l_acct_line_type,
7319                   l_elemental,
7320                   l_ovhd_absp,
7321                   l_dr_flag,
7322                   l_acct_rec,
7323                   l_ae_line_tbl,
7324                   l_err_rec);
7325   -- check error
7326   if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
7327      raise process_error;
7328   end if;
7329 
7330 
7331   IF g_debug_flag = 'Y' THEN
7332     fnd_file.put_line(fnd_file.log,'Cost_consigned_update_txn >> ');
7333   END IF;
7334 
7335 EXCEPTION
7336 
7337   when process_error then
7338   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
7339   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
7340   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
7341 
7342   when no_txn_det_error then
7343   o_ae_err_rec.l_err_num := 30010;
7344   o_ae_err_rec.l_err_code := 'CST_NO_TXN_DET';
7345   FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
7346   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
7347 
7348   when others then
7349   o_ae_err_rec.l_err_num := SQLCODE;
7350   o_ae_err_rec.l_err_code := '';
7351   o_ae_err_rec.l_err_msg := 'CSTPAPBR.inv_cost_txn' || to_char(l_stmt_num) ||
7352   substr(SQLERRM,1,180);
7353 
7354 END cost_consigned_update_txn;
7355 
7356 
7357 
7358 
7359 procedure encumbrance_account(
7360   i_ae_txn_rec                IN        CSTPALTY.cst_ae_txn_rec_type,
7361   i_ae_curr_rec               IN        CSTPALTY.cst_ae_curr_rec_type,
7362   l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
7363   o_ae_err_rec                OUT NOCOPY       CSTPALTY.cst_ae_err_rec_type
7364 ) IS
7365   l_acct        NUMBER;
7366   l_acct_line_type NUMBER;
7367   l_ae_line_rec                 CSTPALTY.cst_ae_line_rec_type;
7368   l_stmt_num    NUMBER;
7369   l_dr_flag                             BOOLEAN;
7370   process_error exception;
7371   l_err_rec                             CSTPALTY.cst_ae_err_rec_type;
7372 BEGIN
7373   IF g_debug_flag = 'Y' THEN
7374     fnd_file.put_line(fnd_file.log,'Encumbrance_account <<');
7375   END IF;
7376 -- Initialize variables.
7377 -- ---------------------
7378   l_err_rec.l_err_num := 0;
7379   l_err_rec.l_err_code := '';
7380   l_err_rec.l_err_msg := '';
7381 
7382   l_stmt_num := 10;
7383 
7384   IF (i_ae_txn_rec.encum_amount < 0) THEN
7385     l_dr_flag := TRUE;
7386   ELSE
7387     l_dr_flag := FALSE;
7388   END IF;
7389 
7390       l_acct_line_type := 15;
7391       l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.encum_amount);
7392       l_ae_line_rec.resource_id := NULL;
7393       l_ae_line_rec.cost_element_id := NULL;
7394       l_ae_line_rec.actual_flag := 'E';
7395       l_ae_line_rec.encum_type_id := i_ae_txn_rec.encum_type_id;
7396       l_ae_line_rec.ae_line_type := 15;
7397       l_ae_line_rec.account := i_ae_txn_rec.encum_account;
7398       insert_account (i_ae_txn_rec,
7399                       i_ae_curr_rec,
7400                       l_dr_flag,
7401                       l_ae_line_rec,
7402                       l_ae_line_tbl,
7403                       l_err_rec);
7404 
7405       -- check error
7406       if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7407       raise process_error;
7408       end if;
7409 
7410   IF g_debug_flag = 'Y' THEN
7411     fnd_file.put_line(fnd_file.log,'Encumbrance_account <<');
7412   END IF;
7413 
7414 EXCEPTION
7415 
7416   when process_error then
7417   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
7418   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
7419   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
7420 
7421   when others then
7422   o_ae_err_rec.l_err_num := SQLCODE;
7423   o_ae_err_rec.l_err_code := '';
7424   o_ae_err_rec.l_err_msg := 'CSTPAPBR.inv_cost_txn' || to_char(l_stmt_num) ||
7425   substr(SQLERRM,1,180);
7426 
7427 END encumbrance_account;
7428 
7429 
7430 -- ===================================================================
7431 -- Inventory Accounts.
7432 -- ===================================================================
7433 
7434 -- Expense Account should be from Category Accounts assigned to
7435 -- Legal Entity-CG-CT and Item Category. Ignore the account passed
7436 -- since this used to be from Subinventory
7437 
7438 procedure inventory_accounts(
7439   i_ae_txn_rec          IN            CSTPALTY.cst_ae_txn_rec_type,
7440   i_ae_curr_rec         IN            CSTPALTY.cst_ae_curr_rec_type,
7441   i_exp_flag            IN            BOOLEAN,
7442   i_exp_account         IN      NUMBER,
7443   i_dr_flag             IN            BOOLEAN,
7444   l_ae_line_tbl         IN OUT NOCOPY  CSTPALTY.cst_ae_line_tbl_type,
7445   o_ae_err_rec          OUT NOCOPY           CSTPALTY.cst_ae_err_rec_type,
7446   i_intransit_flag      IN NUMBER
7447 ) IS
7448   l_cost                        NUMBER;
7449   l_ae_line_rec                 CSTPALTY.cst_ae_line_rec_type;
7450   l_ele_exist                         NUMBER;
7451   l_acct_exist                  NUMBER;
7452   l_stmt_num                          NUMBER := 0;
7453   l_api_name   CONSTANT VARCHAR2(30)    := 'CSTPAPBR.inventory_accounts';
7454 
7455   l_expense_account             NUMBER;
7456 
7457   l_err_rec                     CSTPALTY.cst_ae_err_rec_type;
7458   l_acct_rec                    CSTPALTY.cst_ae_acct_rec_type;
7459 
7460   /* Bug 3123936: Propogation of 2896193 */
7461   l_dr_flag                     BOOLEAN;
7462   l_var                         NUMBER;
7463 
7464   process_error                       EXCEPTION;
7465   no_cg_acct_error              EXCEPTION;
7466   no_mfca_acct_error            EXCEPTION;
7467   no_txn_det_error              EXCEPTION;
7468 BEGIN
7469 
7470   IF g_debug_flag = 'Y' THEN
7471     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': begin <<');
7472   END IF;
7473 -- initialize variables.
7474 -- ---------------------
7475   l_err_rec.l_err_num := 0;
7476   l_err_rec.l_err_code := '';
7477   l_err_rec.l_err_msg := '';
7478   l_cost := '';
7479 
7480   l_stmt_num := 15;
7481 
7482  select count(legal_entity_id) /* changed for bug no. 4586534 */
7483   into l_acct_exist
7484   from mtl_fiscal_cat_accounts
7485   where legal_entity_id = i_ae_txn_rec.legal_entity_id
7486     and cost_type_id    = i_ae_txn_rec.cost_type_id
7487     and cost_group_id   = i_ae_txn_rec.cost_group_id
7488     and category_id     = i_ae_txn_rec.category_id;
7489 
7490   if (l_acct_exist = 0) then
7491     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num ||
7492            ': Category : '||to_char(i_ae_txn_rec.category_id) ||' has no accounts defined');
7493     raise no_mfca_acct_error;
7494   end if;
7495 
7496   l_stmt_num := 20;
7497 
7498   select nvl(material_account,-1),
7499          nvl(material_overhead_account,-1),
7500          nvl(resource_account,-1),
7501          nvl(outside_processing_account,-1),
7502          nvl(overhead_account,-1),
7503          nvl(expense_account, -1)
7504     into l_acct_rec.mat_account,
7505          l_acct_rec.mat_ovhd_account,
7506          l_acct_rec.res_account,
7507          l_acct_rec.osp_account,
7508          l_acct_rec.ovhd_account,
7509          l_expense_account
7510     from mtl_fiscal_cat_accounts
7511   where legal_entity_id = i_ae_txn_rec.legal_entity_id
7512     and cost_type_id    = i_ae_txn_rec.cost_type_id
7513     and cost_group_id   = i_ae_txn_rec.cost_group_id
7514     and category_id     = i_ae_txn_rec.category_id;
7515 
7516   l_stmt_num := 25;
7517 
7518   /* PAC Enhancements for R12: Include intransit line type as well */
7519   if i_exp_flag then
7520     l_ae_line_rec.ae_line_type := 2;
7521   elsif (i_intransit_flag = 1) then
7522     l_ae_line_rec.ae_line_type := 14;
7523   else
7524     l_ae_line_rec.ae_line_type := 1;
7525   end if;
7526 
7527   l_stmt_num := 30;
7528 
7529   select count(transaction_id)
7530   into l_ele_exist
7531   from mtl_pac_actual_cost_details mpacd
7532   where mpacd.transaction_id = i_ae_txn_rec.transaction_id
7533   and mpacd.pac_period_id = i_ae_txn_rec.accounting_period_id
7534   and mpacd.cost_group_id = i_ae_txn_rec.cost_group_id
7535    and rownum < 2; /* 4586534 added rownum filter for perf as its only existence check */
7536 
7537   if (l_ele_exist = 0) then
7538     raise no_txn_det_error;
7539   end if;
7540 
7541   FOR cost_element IN 1..5 loop
7542 
7543     l_stmt_num := 40;
7544     select sum(nvl(actual_cost,0)),
7545     sum(nvl(variance_amount,0)) /* Bug 3123936: Prop of 2896193 */
7546     into l_cost, l_var
7547     from mtl_pac_actual_cost_details
7548     where transaction_id = i_ae_txn_rec.transaction_id
7549     and pac_period_id = i_ae_txn_rec.accounting_period_id
7550     and cost_group_id = i_ae_txn_rec.cost_group_id
7551     and cost_element_id = cost_element;
7552 
7553     if (l_cost is not null) then  /* IF THERE EXISTS A COST FOR THE COST ELEMENT */
7554 
7555       l_stmt_num := 45;
7556       l_ae_line_rec.account := CSTPAPHK.get_account_id (
7557                                         i_ae_txn_rec.transaction_id,
7558                                         i_ae_txn_rec.legal_entity_id,
7559                                         i_ae_txn_rec.cost_type_id,
7560                                         i_ae_txn_rec.cost_group_id,
7561                                         i_dr_flag,
7562                                         l_ae_line_rec.ae_line_type,
7563                                         cost_element,
7564                                         NULL,
7565                                         i_ae_txn_rec.subinventory_code,
7566                                         i_exp_flag,
7567                                         l_err_rec.l_err_num,
7568                                         l_err_rec.l_err_code,
7569                                         l_err_rec.l_err_msg);
7570       -- check error
7571       if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7572       raise process_error;
7573       end if;
7574 
7575       if (l_ae_line_rec.account = -1) then
7576 
7577           l_stmt_num := 50;
7578 
7579           if i_exp_flag then
7580              l_ae_line_rec.account := l_expense_account;
7581              IF g_debug_flag = 'Y' THEN
7582                FND_FILE.PUT_LINE(FND_FILE.LOG,  l_api_name || ': ' || l_stmt_num || ': Expense Account: '||to_char(l_expense_account));
7583              END IF;
7584           else
7585             /* changed for bug no. 4586534 */
7586              IF cost_element = 1 THEN
7587                 l_ae_line_rec.account :=  l_acct_rec.mat_account;
7588              ELSIF cost_element = 2 THEN
7589                 l_ae_line_rec.account :=  l_acct_rec.mat_ovhd_account;
7590              ELSIF cost_element = 3 THEN
7591                 l_ae_line_rec.account :=  l_acct_rec.res_account;
7592              ELSIF cost_element = 4 THEN
7593                 l_ae_line_rec.account :=  l_acct_rec.osp_account;
7594              ELSIF cost_element = 5 THEN
7595                 l_ae_line_rec.account :=  l_acct_rec.ovhd_account;
7596              END IF;
7597           end if;
7598 
7599       end if;
7600 
7601       l_stmt_num := 56;
7602 
7603 
7604       /* Bug 3213936: Propogation of 2896193
7605          Consider any variance when determining transaction_value */
7606       if( l_ae_line_rec.ae_line_type = 1 and i_ae_txn_rec.txn_action_id not in (3,21,12,15,22) ) then
7607         if((i_ae_txn_rec.primary_quantity * l_cost - l_var)>0) then
7608           l_dr_flag := "TRUE";
7609         else
7610           l_dr_flag := "FALSE";
7611         end if;
7612       else
7613         l_dr_flag := i_dr_flag;
7614       end if;
7615       l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity * l_cost - l_var);
7616       l_ae_line_rec.resource_id := NULL;
7617       l_ae_line_rec.cost_element_id := cost_element;
7618 
7619       IF g_debug_flag = 'Y' THEN
7620         fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': Insert element ' || cost_element);
7621       END IF;
7622 
7623       insert_account (i_ae_txn_rec,
7624                       i_ae_curr_rec,
7625                       l_dr_flag, --i_dr_flag,
7626                       l_ae_line_rec,
7627                       l_ae_line_tbl,
7628                       l_err_rec);
7629 
7630       -- check error
7631       if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7632         raise process_error;
7633       end if;
7634 
7635     else
7636       IF g_debug_flag = 'Y' THEN
7637         fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': No cost for element ' || cost_element);
7638       END IF;
7639     end if;
7640 
7641   end loop;
7642 
7643 /* Bug 3213936: Propogation of 2896193 : Start */
7644 
7645     select sum(nvl(variance_amount,0))
7646     into l_var
7647     from mtl_pac_actual_cost_details
7648     where transaction_id = i_ae_txn_rec.transaction_id
7649     and pac_period_id = i_ae_txn_rec.accounting_period_id
7650     and cost_group_id = i_ae_txn_rec.cost_group_id;
7651 
7652     IF (l_var <> 0 AND (NOT i_exp_flag)) THEN
7653             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Creating Variance Entry');
7654             l_stmt_num := 60;
7655             l_ae_line_rec.ae_line_type := 13;
7656 
7657             IF (l_var > 0)
7658             THEN l_dr_flag := "TRUE";
7659             ELSE l_dr_flag := "FALSE";
7660             END IF;
7661 
7662             l_stmt_num := 70;
7663 
7664             /* Bug 3123936: Propogation of 3367784
7665                Added filter on legal_entity_id and cost_Type_id */
7666 
7667             SELECT nvl(cost_variance_account,-1)
7668               INTO l_ae_line_rec.account
7669               FROM cst_org_cost_group_accounts
7670              WHERE cost_group_id=i_ae_txn_rec.cost_group_id
7671               AND  legal_entity_id = i_ae_txn_rec.legal_entity_id
7672               AND cost_type_id = i_ae_txn_rec.cost_type_id;
7673 
7674            l_stmt_num := 80;
7675             l_ae_line_rec.transaction_value := abs(l_var);
7676             l_ae_line_rec.resource_id := NULL;
7677             l_ae_line_rec.cost_element_id := NULL;
7678             insert_account (i_ae_txn_rec,
7679                             i_ae_curr_rec,
7680                             l_dr_flag,
7681                             l_ae_line_rec,
7682                             l_ae_line_tbl,
7683                             l_err_rec);
7684      END if;
7685 /* Bug 3213936 : End */
7686 
7687 
7688   IF g_debug_flag = 'Y' THEN
7689     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': return >>');
7690   END IF;
7691 
7692 EXCEPTION
7693 
7694   when process_error then
7695   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
7696   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
7697   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
7698 
7699   when no_cg_acct_error then
7700   o_ae_err_rec.l_err_num := 30007;
7701   o_ae_err_rec.l_err_code := 'CST_PAC_NO_CG_ACCTS';
7702   FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_CG_ACCTS');
7703   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
7704 
7705   when no_mfca_acct_error then
7706   o_ae_err_rec.l_err_num := 30008;
7707   o_ae_err_rec.l_err_code := 'CST_PAC_NO_MFCA_ACCTS';
7708   FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_MFCA_ACCTS');
7709   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
7710 
7711   when no_txn_det_error then
7712   o_ae_err_rec.l_err_num := 30009;
7713   o_ae_err_rec.l_err_code := 'CST_NO_TXN_DET';
7714   FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
7715   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
7716 
7717   when others then
7718   o_ae_err_rec.l_err_num := SQLCODE;
7719   o_ae_err_rec.l_err_code := '';
7720   o_ae_err_rec.l_err_msg := 'CSTPAPBR.inventory_accounts' || to_char(l_stmt_num) ||
7721   substr(SQLERRM,1,180);
7722 
7723 END inventory_accounts;
7724 
7725 -- ===================================================================
7726 -- Offset Accounts.
7727 -- ===================================================================
7728 procedure offset_accounts(
7729    i_ae_txn_rec     IN          CSTPALTY.cst_ae_txn_rec_type,
7730    i_ae_curr_rec    IN          CSTPALTY.cst_ae_curr_rec_type,
7731    --i_ae_txn_rec     IN        CSTPALBR.cst_ae_txn_rec_type,
7732    --i_ae_curr_rec    IN        CSTPALBR.cst_ae_curr_rec_type,
7733    i_acct_line_type IN          NUMBER,
7734    i_elemental      IN          NUMBER,
7735    i_ovhd_absp      IN          NUMBER,
7736    i_dr_flag        IN          BOOLEAN,
7737    i_ae_acct_rec    IN          CSTPALTY.cst_ae_acct_rec_type,
7738    l_ae_line_tbl    IN OUT NOCOPY      CSTPALTY.cst_ae_line_tbl_type,
7739    o_ae_err_rec     OUT NOCOPY          CSTPALTY.cst_ae_err_rec_type
7740    --i_ae_acct_rec    IN        CSTPALBR.cst_ae_acct_rec_type,
7741    --l_ae_line_tbl    IN OUT      CSTPALBR.cst_ae_line_tbl_type,
7742    --o_ae_err_rec     OUT       CSTPALBR.cst_ae_err_rec_type
7743 )IS
7744   l_ae_line_rec                 CSTPALTY.cst_ae_line_rec_type;
7745   --l_ae_line_rec               CSTPALBR.cst_ae_line_rec_type;
7746   l_ele_exist                   NUMBER;
7747   l_cost                            NUMBER;
7748   l_stmt_num                        NUMBER := 0;
7749   l_err_rec                     CSTPALTY.cst_ae_err_rec_type;
7750   l_acct_rec                    CSTPALTY.cst_ae_acct_rec_type;
7751   --l_err_rec                   CSTPALBR.cst_ae_err_rec_type;
7752   --l_acct_rec                  CSTPALBR.cst_ae_acct_rec_type;
7753   process_error                     EXCEPTION;
7754   no_txn_det_error              EXCEPTION;
7755   l_api_name   CONSTANT VARCHAR2(30)    := 'CSTPAPBR.offset_accounts';
7756 BEGIN
7757   IF g_debug_flag = 'Y' THEN
7758     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': begin << ');
7759     FND_FILE.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': Elemental flag: ' ||to_char(i_elemental));
7760   END IF;
7761 -- initialize variables.
7762 -- ---------------------
7763   l_err_rec.l_err_num := 0;
7764   l_err_rec.l_err_code := '';
7765   l_err_rec.l_err_msg := '';
7766 
7767   if(i_elemental = 1) then
7768 
7769     l_stmt_num := 10;
7770 
7771     select count(transaction_id)
7772     into l_ele_exist
7773     from mtl_pac_actual_cost_details
7774     where transaction_id = i_ae_txn_rec.transaction_id
7775     and pac_period_id = i_ae_txn_rec.accounting_period_id
7776     and cost_group_id = i_ae_txn_rec.cost_group_id
7777     and rownum < 2; /* 4586534 added rownum filter for perf as its only existence check */
7778 
7779     if (l_ele_exist = 0) then
7780       raise no_txn_det_error;
7781     end if;
7782 
7783     FOR cost_element IN 1..5 loop
7784 
7785 -- i_ovhd_absp indicates which level of material overhead we are
7786 -- absorbtion and therefore need to go in an absorption account.
7787 -- 2 means both levels and 1 means this level only.
7788 -- -------------------------------------------------------------
7789       l_stmt_num := 20;
7790 
7791       select sum(nvl(actual_cost,0))
7792       into l_cost
7793       from mtl_pac_actual_cost_details
7794       where transaction_id = i_ae_txn_rec.transaction_id
7795       and pac_period_id = i_ae_txn_rec.accounting_period_id
7796       and cost_group_id = i_ae_txn_rec.cost_group_id
7797       and cost_element_id = cost_element
7798       and (cost_element_id <> 2
7799            OR
7800            (cost_element_id = 2
7801             and level_type = decode(i_ovhd_absp,1,2,2,0,level_type)));
7802 
7803       if (l_cost is not null) then
7804 
7805         l_stmt_num := 30;
7806         l_ae_line_rec.account := CSTPAPHK.get_account_id (
7807                                         i_ae_txn_rec.transaction_id,
7808                                         i_ae_txn_rec.legal_entity_id,
7809                                         i_ae_txn_rec.cost_type_id,
7810                                         i_ae_txn_rec.cost_group_id,
7811                                         i_dr_flag,
7812                                         i_acct_line_type,
7813                                         cost_element,
7814                                         NULL,
7815                                         i_ae_txn_rec.subinventory_code,
7816                                         "FALSE", ---  i_exp_flag
7817                                         l_err_rec.l_err_num,
7818                                         l_err_rec.l_err_code,
7819                                         l_err_rec.l_err_msg);
7820         -- check error
7821         if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7822         raise process_error;
7823         end if;
7824 
7825         if (l_ae_line_rec.account = -1) then
7826             l_stmt_num := 40;
7827           /* Changed for Bug No 4586534
7828           select decode(cost_element, 1, i_ae_acct_rec.mat_account,
7829                                   2, i_ae_acct_rec.mat_ovhd_account,
7830                                   3, i_ae_acct_rec.res_account,
7831                                   4, i_ae_acct_rec.osp_account,
7832                                   5, i_ae_acct_rec.ovhd_account)
7833           into l_ae_line_rec.account
7834           from dual;
7835           */
7836           IF cost_element = 1 THEN
7837                      l_ae_line_rec.account := i_ae_acct_rec.mat_account;
7838                    ELSIF cost_element = 2 THEN
7839                      l_ae_line_rec.account := i_ae_acct_rec.mat_ovhd_account;
7840                    ELSIF cost_element = 3 THEN
7841                      l_ae_line_rec.account := i_ae_acct_rec.res_account;
7842                    ELSIF cost_element = 4 THEN
7843                      l_ae_line_rec.account := i_ae_acct_rec.osp_account;
7844                    ELSIF cost_element = 5 THEN
7845                      l_ae_line_rec.account := i_ae_acct_rec.ovhd_account;
7846           END IF;
7847         end if;
7848 
7849         l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity) * l_cost;
7850         l_ae_line_rec.resource_id := NULL;
7851         l_ae_line_rec.cost_element_id := cost_element;
7852         l_ae_line_rec.ae_line_type := i_acct_line_type;
7853 
7854         insert_account (i_ae_txn_rec,
7855                         i_ae_curr_rec,
7856                         i_dr_flag,
7857                         l_ae_line_rec,
7858                         l_ae_line_tbl,
7859                         l_err_rec);
7860         -- check error
7861         if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7862         raise process_error;
7863         end if;
7864 
7865       else
7866         IF g_debug_flag = 'Y' THEN
7867           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || 'No cost for element ' || cost_element);
7868         END IF;
7869       end if;
7870     end loop;
7871   else
7872     l_stmt_num := 50;
7873 
7874     select nvl(sum(nvl(actual_cost,0)),0)
7875     into l_cost
7876     from mtl_pac_actual_cost_details
7877     where transaction_id = i_ae_txn_rec.transaction_id
7878     and pac_period_id = i_ae_txn_rec.accounting_period_id
7879     and cost_group_id = i_ae_txn_rec.cost_group_id
7880     and (cost_element_id <> 2
7881          OR
7882          (cost_element_id = 2
7883           and level_type = decode(i_ovhd_absp,1,2,2,0,level_type)));
7884     if (l_cost is not null) then
7885 
7886        l_stmt_num := 60;
7887        l_ae_line_rec.account := CSTPAPHK.get_account_id (
7888                                         i_ae_txn_rec.transaction_id,
7889                                         i_ae_txn_rec.legal_entity_id,
7890                                         i_ae_txn_rec.cost_type_id,
7891                                         i_ae_txn_rec.cost_group_id,
7892                                         i_dr_flag,
7893                                         i_acct_line_type,
7894                                         NULL,    --- cost_element
7895                                         NULL,
7896                                         i_ae_txn_rec.subinventory_code,
7897                                         "FALSE", --- i_exp_flag
7898                                         l_err_rec.l_err_num,
7899                                         l_err_rec.l_err_code,
7900                                         l_err_rec.l_err_msg);
7901        -- check error
7902        if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7903          raise process_error;
7904        end if;
7905 
7906        if (l_ae_line_rec.account = -1) then
7907         l_ae_line_rec.account := i_ae_acct_rec.account;
7908        end if;
7909 
7910        l_stmt_num := 70;
7911 
7912        l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity) * l_cost;
7913        l_ae_line_rec.resource_id := NULL;
7914        l_ae_line_rec.cost_element_id := NULL;
7915        l_ae_line_rec.ae_line_type := i_acct_line_type;
7916 
7917        insert_account (i_ae_txn_rec,
7918                        i_ae_curr_rec,
7919                        i_dr_flag,
7920                        l_ae_line_rec,
7921                        l_ae_line_tbl,
7922                        l_err_rec);
7923        -- check error
7924        if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
7925           raise process_error;
7926        end if;
7927     else
7928       IF g_debug_flag = 'Y' THEN
7929         fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || 'No Cost');
7930       END IF;
7931     end if;
7932   end if;
7933   IF g_debug_flag = 'Y' THEN
7934     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': return >>');
7935   END IF;
7936 
7937   EXCEPTION
7938 
7939   when no_txn_det_error then
7940   o_ae_err_rec.l_err_num := 30010;
7941   o_ae_err_rec.l_err_code := 'CST_NO_TXN_DET';
7942   FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
7943   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
7944 
7945   when process_error then
7946   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
7947   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
7948   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
7949 
7950   when others then
7951   o_ae_err_rec.l_err_num := SQLCODE;
7952   o_ae_err_rec.l_err_code := '';
7953   o_ae_err_rec.l_err_msg := 'CSTPAPBR.offset_accounts' || to_char(l_stmt_num) ||
7954   substr(SQLERRM,1,180);
7955 
7956 end offset_accounts;
7957 
7958 -- ===================================================================
7959 -- Overhead.
7960 -- ===================================================================
7961 procedure ovhd_accounts(
7962   i_ae_txn_rec    IN     CSTPALTY.cst_ae_txn_rec_type,
7963   i_ae_curr_rec   IN     CSTPALTY.cst_ae_curr_rec_type,
7964   i_dr_flag       IN     BOOLEAN,
7965   l_ae_line_tbl   IN OUT NOCOPY CSTPALTY.cst_ae_line_tbl_type,
7966   o_ae_err_rec    OUT NOCOPY    CSTPALTY.cst_ae_err_rec_type
7967 )IS
7968   l_ae_line_rec         CSTPALTY.cst_ae_line_rec_type;
7969   l_err_rec             CSTPALTY.cst_ae_err_rec_type;
7970   l_res_id              NUMBER;
7971   l_cost                NUMBER;
7972   l_stmt_num            NUMBER;
7973   l_txn_cost_group_id   NUMBER;
7974   process_error         EXCEPTION;
7975 
7976   cursor mat_ovhds is
7977     select resource_id, actual_cost
7978     from mtl_pac_cost_subelements
7979     where transaction_id = i_ae_txn_rec.transaction_id
7980     and cost_group_id = i_ae_txn_rec.cost_group_id
7981     and pac_period_id = i_ae_txn_rec.accounting_period_id
7982     and cost_type_id = i_ae_txn_rec.cost_type_id
7983     and cost_element_id = 2;
7984 
7985 
7986 BEGIN
7987   IF g_debug_flag = 'Y' THEN
7988     fnd_file.put_line(fnd_file.log,'Ovhd_accounts << ');
7989   END IF;
7990 
7991 -- Initialize variables.
7992 -- ---------------------
7993   l_err_rec.l_err_num := 0;
7994   l_err_rec.l_err_code := '';
7995   l_err_rec.l_err_msg := '';
7996 
7997   BEGIN
7998     l_stmt_num := 5;
7999 
8000     SELECT NVL(cost_group_id, -1)
8001     INTO   l_txn_cost_group_id
8002     FROM   cst_cost_group_assignments
8003     WHERE  organization_id = i_ae_txn_rec.organization_id;
8004   EXCEPTION
8005     WHEN no_data_found THEN
8006       l_txn_cost_group_id := -1;
8007   END;
8008 
8009   open mat_ovhds;
8010 
8011   loop
8012     fetch mat_ovhds into l_res_id, l_cost;
8013     exit when mat_ovhds%NOTFOUND;
8014 
8015     l_stmt_num := 10;
8016     l_ae_line_rec.account := CSTPAPHK.get_account_id (
8017                                         i_ae_txn_rec.transaction_id,
8018                                         i_ae_txn_rec.legal_entity_id,
8019                                         i_ae_txn_rec.cost_type_id,
8020                                         i_ae_txn_rec.cost_group_id,
8021                                         i_dr_flag,
8022                                         l_ae_line_rec.ae_line_type,
8023                                         2,       --- cost_element
8024                                         NULL,
8025                                         i_ae_txn_rec.subinventory_code,
8026                                         "FALSE", --- i_exp_flag
8027                                         l_err_rec.l_err_num,
8028                                         l_err_rec.l_err_code,
8029                                         l_err_rec.l_err_msg);
8030     -- check error
8031     if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
8032       raise process_error;
8033     end if;
8034 
8035     if (l_ae_line_rec.account = -1) then
8036 
8037       if (i_ae_txn_rec.txn_action_id = 21 and l_txn_cost_group_id <> i_ae_txn_rec.cost_group_id) then
8038 
8039          -- Interorg shipment transaction, processed by receiving cost group: pick account from
8040          -- transfer organization
8041          l_stmt_num := 15;
8042 
8043          select nvl(absorption_account,-1)
8044          into l_ae_line_rec.account
8045          from bom_resources
8046          where resource_id = l_res_id
8047          and organization_id = i_ae_txn_rec.xfer_organization_id;
8048       else
8049          l_stmt_num := 20;
8050 
8051          select nvl(absorption_account,-1)
8052          into l_ae_line_rec.account
8053          from bom_resources
8054          where resource_id = l_res_id
8055          and organization_id = i_ae_txn_rec.organization_id;
8056       end if;
8057 
8058     end if;
8059 
8060     l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity) * l_cost;
8061     l_ae_line_rec.ae_line_type := 3;
8062     l_ae_line_rec.cost_element_id := 2;
8063     l_ae_line_rec.resource_id := l_res_id;
8064 
8065     l_stmt_num := 25;
8066     insert_account (i_ae_txn_rec,
8067                     i_ae_curr_rec,
8068                     i_dr_flag,
8069                     l_ae_line_rec,
8070                     l_ae_line_tbl,
8071                     l_err_rec);
8072 
8073     -- check error
8074     if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
8075        raise process_error;
8076     end if;
8077 
8078   end loop;
8079   IF g_debug_flag = 'Y' THEN
8080     fnd_file.put_line(fnd_file.log,'Ovhd_accounts >>');
8081   END IF;
8082 
8083   EXCEPTION
8084 
8085   when process_error then
8086   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
8087   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
8088   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
8089 
8090   when others then
8091   o_ae_err_rec.l_err_num := SQLCODE;
8092   o_ae_err_rec.l_err_code := '';
8093   o_ae_err_rec.l_err_msg := 'CSTPAPBR.ovhd_accounts' || to_char(l_stmt_num) ||
8094   substr(SQLERRM,1,180);
8095 
8096 END ovhd_accounts;
8097 
8098 -- ===================================================================
8099 -- Insert Account.
8100 -- ===================================================================
8101 PROCEDURE insert_account(
8102   i_ae_txn_rec          IN      CSTPALTY.cst_ae_txn_rec_type,
8103   i_ae_curr_rec         IN      CSTPALTY.cst_ae_curr_rec_type,
8104   --i_ae_txn_rec          IN            CSTPALBR.cst_ae_txn_rec_type,
8105   --i_ae_curr_rec         IN            CSTPALBR.cst_ae_curr_rec_type,
8106   i_dr_flag             IN      BOOLEAN,
8107   i_ae_line_rec         IN      CSTPALTY.cst_ae_line_rec_type,
8108   l_ae_line_tbl         IN OUT NOCOPY  CSTPALTY.cst_ae_line_tbl_type,
8109   o_ae_err_rec          OUT NOCOPY      CSTPALTY.cst_ae_err_rec_type)
8110   --i_ae_line_rec         IN            CSTPALBR.cst_ae_line_rec_type,
8111   --l_ae_line_tbl         IN OUT  CSTPALBR.cst_ae_line_tbl_type,
8112   --o_ae_err_rec          OUT           CSTPALBR.cst_ae_err_rec_type)
8113 IS
8114   l_err_rec                     CSTPALTY.cst_ae_err_rec_type;
8115   --l_err_rec                   CSTPALBR.cst_ae_err_rec_type;
8116   l_entered_value               NUMBER;
8117   l_accounted_value             NUMBER;
8118   l_exp_sub1                    NUMBER;
8119   l_stmt_num                         NUMBER := 0;
8120   next_record_avail             NUMBER;
8121   invalid_acct_error            EXCEPTION;
8122 
8123   l_api_name   CONSTANT VARCHAR2(30)    := 'CSTPAPBR.insert_account';
8124 
8125 BEGIN
8126   IF g_debug_flag = 'Y' THEN
8127     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': begin << ');
8128   END IF;
8129 
8130 -- Initialize variables.
8131 -- ---------------------
8132   l_err_rec.l_err_num := 0;
8133   l_err_rec.l_err_code := '';
8134   l_err_rec.l_err_msg := '';
8135 
8136   if (i_ae_line_rec.account = -1) then
8137     raise invalid_acct_error;
8138   end if;
8139 
8140   l_stmt_num := 10;
8141   next_record_avail := nvl(l_ae_line_tbl.LAST,0) ;
8142   l_ae_line_tbl.extend;
8143   next_record_avail := nvl(l_ae_line_tbl.LAST,0) ;
8144   l_ae_line_tbl(next_record_avail).ae_line_type := i_ae_line_rec.ae_line_type;
8145 
8146   l_stmt_num := 20;
8147   select
8148   meaning
8149   into
8150   l_ae_line_tbl(next_record_avail).description
8151   from
8152   mfg_lookups
8153   where lookup_type = 'CST_ACCOUNTING_LINE_TYPE'
8154   and lookup_code = l_ae_line_tbl(next_record_avail).ae_line_type;
8155 
8156   l_ae_line_tbl(next_record_avail).account :=
8157      i_ae_line_rec.account;
8158 
8159   l_stmt_num := 30;
8160    /* Bug 4586534. changed to PL/SQL logic based instead of select from dual
8161   select
8162   nvl(i_ae_curr_rec.alt_currency,i_ae_curr_rec.pri_currency)
8163   into l_ae_line_tbl(next_record_avail).currency_code
8164   from dual;
8165   */
8166   l_ae_line_tbl(next_record_avail).currency_code := nvl(i_ae_curr_rec.alt_currency,i_ae_curr_rec.pri_currency);
8167 
8168   l_stmt_num := 40;
8169   /*select
8170   decode(i_ae_curr_rec.alt_currency,
8171            i_ae_curr_rec.pri_currency,NULL,
8172          i_ae_curr_rec.currency_conv_date)
8173   into l_ae_line_tbl(next_record_avail).currency_conv_date
8174   from dual;
8175   */
8176 
8177   l_stmt_num := 50;
8178   /*select
8179   decode(i_ae_curr_rec.alt_currency,
8180   i_ae_curr_rec.pri_currency,NULL,
8181   decode(i_ae_curr_rec.currency_conv_rate,-1,NULL,i_ae_curr_rec.currency_conv_rate))
8182   into l_ae_line_tbl(next_record_avail).currency_conv_rate
8183   from dual;
8184    */
8185    IF i_ae_curr_rec.alt_currency = i_ae_curr_rec.pri_currency THEN
8186       l_ae_line_tbl(next_record_avail).currency_conv_date := NULL;
8187       l_ae_line_tbl(next_record_avail).currency_conv_rate := NULL;
8188       l_ae_line_tbl(next_record_avail).currency_conv_type := NULL;
8189 
8190    ELSE
8191       IF i_ae_curr_rec.currency_conv_rate = -1 THEN
8192          l_ae_line_tbl(next_record_avail).currency_conv_rate := NULL;
8193       ELSE
8194          l_ae_line_tbl(next_record_avail).currency_conv_rate := i_ae_curr_rec.currency_conv_rate;
8195       END IF;
8196 
8197       l_ae_line_tbl(next_record_avail).currency_conv_date := i_ae_curr_rec.currency_conv_date;
8198       l_ae_line_tbl(next_record_avail).currency_conv_type := i_ae_curr_rec.currency_conv_type;
8199    END IF;
8200 
8201   l_stmt_num := 60;
8202   /*select
8203   decode(i_ae_curr_rec.alt_currency,
8204          i_ae_curr_rec.pri_currency,NULL,
8205          i_ae_curr_rec.currency_conv_type)
8206   into l_ae_line_tbl(next_record_avail).currency_conv_type
8207   from dual;
8208   */
8209   l_stmt_num := 70;
8210   select decode(i_ae_curr_rec.alt_currency,NULL, NULL,
8211                 i_ae_curr_rec.pri_currency, NULL,
8212                 decode(c2.minimum_accountable_unit,
8213                        NULL,
8214                        round(i_ae_line_rec.transaction_value/nvl(i_ae_curr_rec.currency_conv_rate,1),
8215                        c2.precision),
8216                 round(i_ae_line_rec.transaction_value/nvl(i_ae_curr_rec.currency_conv_rate,1)
8217                       /c2.minimum_accountable_unit)
8218                       * c2.minimum_accountable_unit )),
8219          decode(c1.minimum_accountable_unit,
8220                 NULL, round(i_ae_line_rec.transaction_value, c1.precision),
8221                 round(i_ae_line_rec.transaction_value/c1.minimum_accountable_unit)
8222                 * c1.minimum_accountable_unit )
8223   into
8224       l_entered_value,
8225       l_accounted_value
8226   from
8227       fnd_currencies c1,
8228       fnd_currencies c2
8229   where
8230       c1.currency_code = i_ae_curr_rec.pri_currency
8231       and c2.currency_code = decode(i_ae_curr_rec.alt_currency, NULL,
8232                                                                 i_ae_curr_rec.pri_currency,
8233                                                                 i_ae_curr_rec.alt_currency);
8234 
8235 
8236 
8237   if (i_dr_flag) then
8238 
8239       l_ae_line_tbl(next_record_avail).entered_dr        := nvl(l_entered_value,l_accounted_value);
8240       l_ae_line_tbl(next_record_avail).entered_cr      := NULL;
8241       l_ae_line_tbl(next_record_avail).accounted_dr      := l_accounted_value;
8242       l_ae_line_tbl(next_record_avail).accounted_cr    := NULL;
8243 
8244       IF g_debug_flag = 'Y' THEN
8245         fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': Debit: ' ||
8246                                 l_ae_line_tbl(next_record_avail).description || ' ' || l_accounted_value);
8247       END IF;
8248   else
8249       l_ae_line_tbl(next_record_avail).entered_cr        := nvl(l_entered_value,l_accounted_value);
8250       l_ae_line_tbl(next_record_avail).entered_dr      := NULL;
8251       l_ae_line_tbl(next_record_avail).accounted_cr      := l_accounted_value;
8252       l_ae_line_tbl(next_record_avail).accounted_dr    := NULL;
8253 
8254       IF g_debug_flag = 'Y' THEN
8255         fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': Credit: ' ||
8256                                 l_ae_line_tbl(next_record_avail).description || ' ' || l_accounted_value);
8257       END IF;
8258 
8259   end if;
8260 
8261   l_ae_line_tbl(next_record_avail).source_table  := i_ae_txn_rec.source_table;
8262   l_ae_line_tbl(next_record_avail).source_id := i_ae_txn_rec.transaction_id;
8263 
8264   l_stmt_num := 80;
8265   /*
8266   select  decode(i_ae_txn_rec.primary_quantity,0,0,
8267   abs(i_ae_line_rec.transaction_value/i_ae_txn_rec.primary_quantity))
8268   into l_ae_line_tbl(next_record_avail).rate_or_amount
8269   from dual;
8270   */
8271 
8272   IF (i_ae_txn_rec.primary_quantity = 0) THEN
8273     l_ae_line_tbl(next_record_avail).rate_or_amount := 0;
8274   ELSE
8275     l_ae_line_tbl(next_record_avail).rate_or_amount := abs(i_ae_line_rec.transaction_value/i_ae_txn_rec.primary_quantity);
8276   END IF;
8277 
8278   IF g_debug_flag = 'Y' THEN
8279      fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': Rate or amount: ' ||
8280                                 l_ae_line_tbl(next_record_avail).rate_or_amount);
8281   END IF;
8282 
8283   l_ae_line_tbl(next_record_avail).basis_type    := 1;
8284   l_ae_line_tbl(next_record_avail).resource_id   :=
8285   i_ae_line_rec.resource_id;
8286   l_ae_line_tbl(next_record_avail).cost_element_id :=
8287   i_ae_line_rec.cost_element_id;
8288 
8289   l_ae_line_tbl(next_record_avail).activity_id   := NULL;
8290   l_ae_line_tbl(next_record_avail).repetitive_schedule_id := NULL;
8291   l_ae_line_tbl(next_record_avail).overhead_basis_factor         := NULL;
8292   l_ae_line_tbl(next_record_avail).basis_resource_id := NULL;
8293   l_ae_line_tbl(next_record_avail).actual_flag := i_ae_line_rec.actual_flag;
8294   l_ae_line_tbl(next_record_avail).encum_type_id := i_ae_line_rec.encum_type_id;
8295   l_ae_line_tbl(next_record_avail).wip_entity_id := i_ae_line_rec.wip_entity_id;
8296 
8297   l_ae_line_tbl(next_record_avail).reference1 := NULL;
8298   l_ae_line_tbl(next_record_avail).reference2 := i_ae_txn_rec.organization_id;
8299   l_ae_line_tbl(next_record_avail).reference3 := i_ae_txn_rec.transaction_id;
8300   l_ae_line_tbl(next_record_avail).reference4 := NULL;
8301   l_ae_line_tbl(next_record_avail).reference5 := NULL;
8302   l_ae_line_tbl(next_record_avail).reference6 := NULL;
8303   l_ae_line_tbl(next_record_avail).reference7 := NULL;
8304   l_ae_line_tbl(next_record_avail).reference8 := NULL;
8305   l_ae_line_tbl(next_record_avail).reference9 := NULL;
8306   l_ae_line_tbl(next_record_avail).reference10 := NULL;
8307 
8308   if (i_ae_txn_rec.txn_action_id = 3) then
8309     if (i_ae_txn_rec.cost_group_id = i_ae_txn_rec.xfer_cost_group_id) then
8310       select decode(asset_inventory,1,0,1)
8311       into l_exp_sub1
8312       from mtl_secondary_inventories
8313       where secondary_inventory_name = i_ae_txn_rec.subinventory_code
8314       and organization_id = i_ae_txn_rec.organization_id;
8315       if (l_exp_sub1 = 1) then
8316         l_ae_line_tbl(next_record_avail).reference2 := i_ae_txn_rec.xfer_organization_id;
8317       end if;
8318     end if;
8319   end if;
8320 
8321   IF g_debug_flag = 'Y' THEN
8322     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': return >> ');
8323   END IF;
8324 
8325   EXCEPTION
8326 
8327   when invalid_acct_error then
8328   o_ae_err_rec.l_err_num := 30011;
8329   o_ae_err_rec.l_err_code := 'CST_NO_TXN_INVALID_ACCOUNT';
8330   FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_INVALID_ACCOUNT');
8331   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
8332 
8333   when others then
8334   o_ae_err_rec.l_err_num := SQLCODE;
8335   o_ae_err_rec.l_err_code := '';
8336   o_ae_err_rec.l_err_msg := 'CSTPAPBR.insert_account' || to_char(l_stmt_num) ||
8337   substr(SQLERRM,1,180);
8338 
8339 end insert_account;
8340 
8341 -- ===================================================================
8342 -- Balance Account.
8343 -- ===================================================================
8344 procedure balance_account (
8345    l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
8346    o_ae_err_rec                OUT NOCOPY       CSTPALTY.cst_ae_err_rec_type)
8347   --(l_ae_line_tbl               IN OUT    CSTPALBR.cst_ae_line_tbl_type,
8348    --o_ae_err_rec                OUT       CSTPALBR.cst_ae_err_rec_type)
8349 IS
8350   l_ent_value                   NUMBER := 0;
8351   l_acc_value                   NUMBER := 0;
8352   l_last_rec                    NUMBER;
8353   l_stmt_num                    NUMBER := 0;
8354   l_api_name   CONSTANT VARCHAR2(30)    := 'CSTPAPBR.balance_account';
8355 BEGIN
8356   IF g_debug_flag = 'Y' THEN
8357     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': begin << ');
8358   END IF;
8359 
8360   if (l_ae_line_tbl.exists(1)) then
8361      l_stmt_num := 10;
8362      For i in l_ae_line_tbl.FIRST .. l_ae_line_tbl.LAST loop
8363        l_ent_value := l_ent_value + nvl(l_ae_line_tbl(i).entered_dr,0) - nvl(l_ae_line_tbl(i).entered_cr,0);
8364        l_acc_value := l_acc_value + nvl(l_ae_line_tbl(i).accounted_dr,0) - nvl(l_ae_line_tbl(i).accounted_cr,0);
8365      end loop;
8366 
8367      if (l_ent_value = 0 and l_acc_value = 0) then
8368         IF g_debug_flag = 'Y' THEN
8369            fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': return >> No balancing done.');
8370         END IF;
8371         return;
8372      end if;
8373 
8374      l_stmt_num := 20;
8375      l_last_rec := l_ae_line_tbl.LAST;
8376      if l_ae_line_tbl(l_last_rec).accounted_dr is not NULL then
8377        l_ae_line_tbl(l_last_rec).accounted_dr :=
8378         l_ae_line_tbl(l_last_rec).accounted_dr - l_acc_value;
8379        l_ae_line_tbl(l_last_rec).entered_dr :=
8380         l_ae_line_tbl(l_last_rec).entered_dr - l_ent_value;
8381         IF g_debug_flag = 'Y' THEN
8382            fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': adjust debit by: ' || l_acc_value);
8383         END IF;
8384      elsif l_ae_line_tbl(l_last_rec).accounted_cr is not NULL then
8385        l_ae_line_tbl(l_last_rec).accounted_cr :=
8386         l_ae_line_tbl(l_last_rec).accounted_cr + l_acc_value;
8387        l_ae_line_tbl(l_last_rec).entered_cr :=
8388         l_ae_line_tbl(l_last_rec).entered_cr + l_ent_value;
8389         IF g_debug_flag = 'Y' THEN
8390            fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': adjust credit by: ' || l_acc_value);
8391         END IF;
8392      end if;
8393   end if;
8394 
8395   IF g_debug_flag = 'Y' THEN
8396     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': return >> ');
8397   END IF;
8398 
8399 EXCEPTION
8400 
8401   when others then
8402   o_ae_err_rec.l_err_num := SQLCODE;
8403   o_ae_err_rec.l_err_code := '';
8404   o_ae_err_rec.l_err_msg := 'CSTPAPBR.balance_account' || to_char(l_stmt_num) ||
8405   substr(SQLERRM,1,180);
8406 
8407 END balance_account;
8408 
8409 /* currency stuff !! */
8410 /* dont have material ovhd variance account, so using material variance acct */
8411 
8412 -- ===================================================================
8413 -- Controls line creation for WIP transactions.
8414 -- ===================================================================
8415 procedure create_wip_ae_lines(
8416   --i_ae_txn_rec          IN     CSTPALBR.cst_ae_txn_rec_type,
8417   --o_ae_line_rec_tbl     OUT    CSTPALBR.cst_ae_line_tbl_type,
8418   --o_ae_err_rec          OUT    CSTPALBR.cst_ae_err_rec_type
8419   i_ae_txn_rec          IN     CSTPALTY.cst_ae_txn_rec_type,
8420   o_ae_line_rec_tbl     OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
8421   o_ae_err_rec          OUT NOCOPY    CSTPALTY.cst_ae_err_rec_type
8422 ) IS
8423   l_ae_line_tbl                CSTPALTY.cst_ae_line_tbl_type := CSTPALTY.cst_ae_line_tbl_type();
8424   l_ae_curr_rec                CSTPALTY.cst_ae_curr_rec_type;
8425 
8426   l_ae_enc_curr_rec            CSTPALTY.cst_ae_curr_rec_type;  -- BUG#9356654
8427 
8428   l_ae_line_rec                CSTPALTY.cst_ae_line_rec_type;
8429   l_ae_err_rec                 CSTPALTY.cst_ae_err_rec_type;
8430   --l_ae_line_tbl                CSTPALBR.cst_ae_line_tbl_type := CSTPALBR.cst_ae_line_tbl_type();
8431   --l_ae_curr_rec                CSTPALBR.cst_ae_curr_rec_type;
8432   --l_ae_line_rec                CSTPALBR.cst_ae_line_rec_type;
8433   --l_ae_err_rec                 CSTPALBR.cst_ae_err_rec_type;
8434   l_acct_id1                   NUMBER;
8435   l_acct_id2                   NUMBER;
8436   l_dr_flag                    BOOLEAN;
8437   l_stmt_num                   NUMBER;
8438   l_ele_exist                  NUMBER;
8439   l_actual_value               NUMBER;
8440   l_actual_cost                NUMBER;
8441   process_error                EXCEPTION;
8442   no_txn_det_error             EXCEPTION;
8443 
8444 -- Variables for eAM support in PAC
8445 
8446   l_return_status        VARCHAR(1)  := FND_API.G_RET_STS_SUCCESS;
8447   l_msg_return_status    VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
8448   l_msg_count            NUMBER := 0;
8449   l_msg_data             VARCHAR2(8000) := '';
8450   l_api_message          VARCHAR2(1000) := '';
8451   l_cost_element         NUMBER;
8452   l_maint_cost_category  NUMBER;
8453   l_operation_dept_id    NUMBER;
8454   l_owning_dept_id       NUMBER;
8455   l_enc_rev              NUMBER;  --HYU
8456 
8457   Cursor  get_resource_id (l_transaction_id     number ,
8458                            l_accounting_period_id number ,
8459                            l_cost_group_id      number ,
8460                            l_cost_element       number)
8461   IS
8462           select distinct resource_id /* added distinct for bug 3116821 */
8463           from   wip_pac_actual_cost_details
8464           where  transaction_id = l_transaction_id
8465           and    pac_period_id  = l_accounting_period_id
8466           and    cost_group_id  = l_cost_group_id
8467           and    cost_element_id = l_cost_element;
8468 
8469 BEGIN
8470   IF g_debug_flag = 'Y' THEN
8471     fnd_file.put_line(fnd_file.log,'Create_wip_lines << ');
8472     fnd_file.put_line(fnd_file.log,'Transaction type: '||to_char(i_ae_txn_rec.txn_type_id));
8473   END IF;
8474 
8475 -- Initialize local variables.
8476 -- ---------------------------
8477   l_ae_err_rec.l_err_num := 0;
8478   l_ae_err_rec.l_err_code := '';
8479   l_ae_err_rec.l_err_msg := '';
8480 
8481 -- Populate the Currency Record Type.
8482 -- ----------------------------------
8483 
8484   l_stmt_num := 10;
8485   select currency_code
8486   into l_ae_curr_rec.pri_currency
8487   from gl_sets_of_books
8488   where set_of_books_id = i_ae_txn_rec.set_of_books_id;
8489 
8490   l_ae_curr_rec.alt_currency := i_ae_txn_rec.currency_code;
8491   l_ae_curr_rec.currency_conv_date := i_ae_txn_rec.currency_conv_date;
8492   l_ae_curr_rec.currency_conv_type := i_ae_txn_rec.currency_conv_type;
8493   l_ae_curr_rec.currency_conv_rate := i_ae_txn_rec.currency_conv_rate;
8494 
8495   if (l_ae_curr_rec.alt_currency is not NULL and l_ae_curr_rec.currency_conv_rate = -1) then
8496     if (l_ae_curr_rec.alt_currency <> l_ae_curr_rec.pri_currency) then
8497       if (l_ae_curr_rec.currency_conv_type is NULL) then
8498         FND_PROFILE.get('CURRENCY_CONVERSION_TYPE', l_ae_curr_rec.currency_conv_type);
8499       end if;
8500       l_ae_curr_rec.currency_conv_rate := gl_currency_api.get_rate(i_ae_txn_rec.set_of_books_id,
8501                                                                 l_ae_curr_rec.alt_currency,
8502                                                                 i_ae_txn_rec.accounting_date,
8503                                                                 l_ae_curr_rec.currency_conv_type);
8504     end if;
8505   end if;
8506 
8507 -- Check if there are Cost Details.
8508 -- --------------------------------
8509   l_stmt_num := 20;
8510   select count(transaction_id)
8511   into l_ele_exist
8512   from wip_pac_actual_cost_details mpacd
8513   where mpacd.transaction_id = i_ae_txn_rec.transaction_id
8514   and mpacd.pac_period_id = i_ae_txn_rec.accounting_period_id
8515   and mpacd.cost_group_id = i_ae_txn_rec.cost_group_id
8516   and rownum < 2; /* 4586534 added rownum filter for perf as its only existence check */
8517 
8518   if (l_ele_exist = 0) then
8519     raise no_txn_det_error;
8520   end if;
8521 
8522   select
8523          we.wip_entity_id
8524   into
8525        l_ae_line_rec.wip_entity_id
8526   from wip_transactions wt,
8527        wip_entities we
8528   where we.wip_entity_id = wt.wip_entity_id
8529   and wt.transaction_id = i_ae_txn_rec.transaction_id;
8530 
8531    /* Check if there are Cost Details. */
8532 
8533     IF (i_ae_txn_rec.txn_type_id = 17) THEN
8534     /* eAM support in PAC Enh - Direct Items */
8535         l_stmt_num := 30;
8536 
8537         SELECT  nvl(actual_value ,0) ,
8538                 nvl(actual_cost ,0)
8539         INTO    l_actual_value,
8540                 l_actual_cost
8541         FROM    wip_pac_actual_cost_details
8542         WHERE   transaction_id = i_ae_txn_rec.transaction_id
8543         AND     pac_period_id = i_ae_txn_rec.accounting_period_id
8544         AND     cost_group_id = i_ae_txn_rec.cost_group_id;
8545 
8546         /* Get the cost element for direct item */
8547 
8548         CST_EAMCOST_PUB.get_CostEle_for_DirectItem (
8549                     p_api_version       =>  1.0,
8550                     x_return_status     =>  l_return_status,
8551                     x_msg_count         =>  l_msg_count,
8552                     x_msg_data          =>  l_msg_data,
8553                     p_txn_id            =>  i_ae_txn_rec.transaction_id,
8554                     p_mnt_or_mfg        =>  2,
8555                     p_pac_or_perp       =>  1,
8556                     x_cost_element_id   =>  l_cost_element
8557             );
8558 
8559         IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
8560             FND_FILE.put_line(FND_FILE.log, l_msg_data);
8561             l_api_message := 'get_CostEle_for_DirectItem returned unexpected error';
8562             FND_MESSAGE.set_name('BOM','CST_API_MESSAGE');
8563             FND_MESSAGE.set_token('TEXT', l_api_message);
8564             FND_MSG_pub.add;
8565             raise fnd_api.g_exc_unexpected_error;
8566         END IF;
8567 
8568         IF (g_debug_flag = 'Y') THEN
8569             FND_FILE.PUT_LINE(FND_FILE.LOG, 'mfg cost_element_id: '||
8570             to_char(l_cost_element));
8571         END IF;
8572 
8573         l_stmt_num := 40;
8574 
8575         /* Bug: 5604125 Get the account from WDJ for the direct item cost element */
8576         SELECT  decode(l_cost_element, 1, nvl(material_account,-1),
8577                 3, nvl(resource_account, -1),
8578                 4, nvl(outside_processing_account, -1), -1)
8579         INTO    l_ae_line_rec.account
8580         FROM    wip_discrete_jobs
8581         WHERE   wip_entity_id = l_ae_line_rec.wip_entity_id;
8582 
8583         /* Set Debit Flag properly.
8584         -- ------------------------   */
8585         l_dr_flag := "TRUE";
8586 
8587         /* Set the other fields we will need in Insert_Account procedure.
8588         -- --------------------------------------------------------------*/
8589 
8590         l_stmt_num := 70;
8591         l_ae_line_rec.ae_line_type := 7;
8592         l_ae_line_rec.cost_element_id := l_cost_element;
8593 
8594         l_stmt_num := 80;
8595         l_ae_line_rec.transaction_value := l_actual_value;
8596 
8597         /* Call Insert Account.
8598         -- --------------------*/
8599         l_stmt_num := 90;
8600         CSTPAPBR.insert_account (i_ae_txn_rec,
8601                     l_ae_curr_rec,
8602                     l_dr_flag,
8603                     l_ae_line_rec,
8604                     l_ae_line_tbl,
8605                     l_ae_err_rec);
8606 
8607         /* check error */
8608         IF(l_ae_err_rec.l_err_num<>0
8609            AND l_ae_err_rec.l_err_num IS NOT NULL) THEN
8610             raise process_error;
8611         END IF;
8612 
8613         /* Toggle Debit Flag.
8614         -- ------------------ */
8615         l_stmt_num := 100;
8616         l_dr_flag := not l_dr_flag;
8617 
8618         /* Set the fields we will need in next Insert_Account calling.
8619         -- ----------------------------------------------------------- */
8620         l_stmt_num := 110;
8621 
8622         l_ae_line_rec.ae_line_type := 5;
8623         /* Accounting line type for credit will be Receiving Inspection */
8624 
8625         SELECT  RCV.receiving_account_id
8626         INTO    l_ae_line_rec.account
8627         FROM    rcv_parameters RCV , wip_transactions WT
8628         WHERE   WT.transaction_id = i_ae_txn_rec.transaction_id
8629         AND     WT.organization_id = RCV.organization_id;
8630 
8631         /* Call Insert Account.
8632         -- -------------------- */
8633         l_stmt_num := 120;
8634         CSTPAPBR.insert_account (i_ae_txn_rec,
8635                     l_ae_curr_rec,
8636                     l_dr_flag,
8637                     l_ae_line_rec,
8638                     l_ae_line_tbl,
8639                     l_ae_err_rec);
8640 
8641         IF(l_ae_err_rec.l_err_num<>0
8642            AND l_ae_err_rec.l_err_num IS NOT NULL) THEN
8643             raise process_error;
8644         END IF;
8645 
8646     ELSE -- Normal processing
8647 
8648 -- For each Cost Element.
8649 -- ----------------------
8650   FOR cost_element IN 1..5 loop
8651 
8652 /*  Added cost element 5 to txn_type 1 to account for
8653     resouce based overheads
8654     Bug 2250142
8655 */
8656     if (i_ae_txn_rec.txn_type_id = 1 and (cost_element = 3 or cost_element = 5)) or
8657        (i_ae_txn_rec.txn_type_id = 2 and cost_element = 5) or
8658        (i_ae_txn_rec.txn_type_id = 3 and (cost_element = 4 or cost_element = 5)) or
8659        (i_ae_txn_rec.txn_type_id = 6)
8660     then
8661 
8662     Open get_resource_id( i_ae_txn_rec.transaction_id ,
8663                         i_ae_txn_rec.accounting_period_id ,
8664                         i_ae_txn_rec.cost_group_id ,
8665                         cost_element );
8666     loop
8667        fetch get_resource_id into l_ae_line_rec.resource_id ;
8668        exit  when get_resource_id%NOTFOUND;
8669 
8670 -- Get the cost.
8671 -- -------------
8672        If  i_ae_txn_rec.txn_type_id <> 2 then
8673        l_stmt_num := 130;
8674         select sum(nvl(actual_value,0)),
8675               sum(nvl(actual_cost,0))
8676         into l_actual_value,
8677             l_actual_cost
8678         from wip_pac_actual_cost_details
8679         where transaction_id = i_ae_txn_rec.transaction_id
8680         and pac_period_id = i_ae_txn_rec.accounting_period_id
8681         and cost_group_id = i_ae_txn_rec.cost_group_id
8682         and cost_element_id = cost_element
8683         and nvl(resource_id,-999) = nvl(l_ae_line_rec.resource_id,-999);
8684        Else
8685        l_stmt_num := 140;
8686         select  nvl(actual_value ,0) ,
8687                 nvl(actual_cost ,0)
8688         into    l_actual_value,
8689                 l_actual_cost
8690         from    wip_pac_actual_cost_details
8691         where   transaction_id = i_ae_txn_rec.transaction_id
8692         and     pac_period_id = i_ae_txn_rec.accounting_period_id
8693         and     cost_group_id = i_ae_txn_rec.cost_group_id
8694         and     cost_element_id = cost_element
8695         and     resource_id = l_ae_line_rec.resource_id ;
8696        End if ;
8697 
8698 -- Process only if exists cost for the element.
8699 -- --------------------------------------------
8700        if (i_ae_txn_rec.txn_type_id <> 6 and l_actual_value is not null) or
8701           (i_ae_txn_rec.txn_type_id = 6 and l_actual_cost is not null) then
8702 
8703 
8704 -- Get the corresponding Accounts.
8705 -- -------------------------------
8706 
8707           l_stmt_num := 145;
8708           l_ae_line_rec.account := CSTPAPHK.get_account_id (
8709                                         i_ae_txn_rec.transaction_id,
8710                                         i_ae_txn_rec.legal_entity_id,
8711                                         i_ae_txn_rec.cost_type_id,
8712                                         i_ae_txn_rec.cost_group_id,
8713                                         l_dr_flag,
8714                                         l_ae_line_rec.ae_line_type,
8715                                         cost_element,
8716                                         NULL,
8717                                         i_ae_txn_rec.subinventory_code,
8718                                         "FALSE", --- i_exp_flag
8719                                         l_ae_err_rec.l_err_num,
8720                                         l_ae_err_rec.l_err_code,
8721                                         l_ae_err_rec.l_err_msg);
8722           -- check error
8723           if(l_ae_err_rec.l_err_num<>0 and l_ae_err_rec.l_err_num is not null) then
8724           raise process_error;
8725           end if;
8726 
8727           if (l_ae_line_rec.account = -1) then
8728              l_stmt_num := 150;
8729              l_ae_line_rec.cost_element_id := cost_element;
8730              CSTPAPBR.get_accts (i_ae_txn_rec,
8731                                  l_ae_line_rec,
8732                                  l_ae_line_tbl,
8733                                  l_acct_id1,
8734                                  l_acct_id2,
8735                                  l_ae_err_rec);
8736              if (l_ae_err_rec.l_err_num <> 0 and l_ae_err_rec.l_err_num is not null) then
8737                  raise process_error;
8738              end if;
8739           end if;
8740 
8741 -- Set Debit Flag properly.
8742 -- ------------------------
8743           l_stmt_num := 160;
8744           if (i_ae_txn_rec.txn_type_id <> 6 and i_ae_txn_rec.primary_quantity > 0) or
8745              (i_ae_txn_rec.txn_type_id = 6 and l_actual_cost < 0) then
8746              l_dr_flag := "TRUE";
8747           else
8748              l_dr_flag := "FALSE";
8749           end if;
8750 
8751 -- Set the other fields we will need in Insert_Account procedure.
8752 -- --------------------------------------------------------------
8753           l_stmt_num := 170;
8754           l_ae_line_rec.ae_line_type := 7;
8755           l_ae_line_rec.account := l_acct_id1;
8756           l_ae_line_rec.cost_element_id := cost_element;
8757 
8758           l_stmt_num := 180;
8759           select decode(i_ae_txn_rec.txn_type_id, 6, abs(l_actual_cost), abs(l_actual_value))
8760           into l_ae_line_rec.transaction_value
8761           from dual;
8762 
8763 -- Call Insert Account.
8764 -- --------------------
8765           l_stmt_num := 190;
8766           CSTPAPBR.insert_account (i_ae_txn_rec,
8767                                   l_ae_curr_rec,
8768                                   l_dr_flag,
8769                                   l_ae_line_rec,
8770                                   l_ae_line_tbl,
8771                                   l_ae_err_rec);
8772 
8773           -- check error
8774           if(l_ae_err_rec.l_err_num<>0 and l_ae_err_rec.l_err_num is not null) then
8775           raise process_error;
8776           end if;
8777 
8778 -- Toggle Debit Flag.
8779 -- ------------------
8780           l_stmt_num := 200;
8781           l_dr_flag := not l_dr_flag;
8782 
8783 -- Set the fields we will need in next Insert_Account calling.
8784 -- -----------------------------------------------------------
8785           l_stmt_num := 210;
8786           select decode(i_ae_txn_rec.txn_type_id, 1, 4, 2, 3, 3, 4, 6, 8)
8787           into l_ae_line_rec.ae_line_type
8788           from dual;
8789 
8790           l_ae_line_rec.account := l_acct_id2;
8791 
8792 -- Call Insert Account.
8793 -- --------------------
8794           l_stmt_num := 220;
8795           CSTPAPBR.insert_account (i_ae_txn_rec,
8796                                   l_ae_curr_rec,
8797                                   l_dr_flag,
8798                                   l_ae_line_rec,
8799                                   l_ae_line_tbl,
8800                                   l_ae_err_rec);
8801 
8802           -- check error
8803           if(l_ae_err_rec.l_err_num<>0 and l_ae_err_rec.l_err_num is not null) then
8804           raise process_error;
8805           end if;
8806        end if;
8807 
8808       end loop ; -- get_resource_id
8809 
8810       close get_resource_id ;
8811 
8812     end if; -- i_ae_txn_rec.txn_type_id and cost_element combn check
8813   end loop; -- for cost_element 1 ..5
8814 
8815     END IF; -- eAM check
8816 
8817 -- Take care of rounding errors.
8818 -- -----------------------------
8819   l_stmt_num := 230;
8820   CSTPAPBR.balance_account (l_ae_line_tbl,
8821                            l_ae_err_rec);
8822 
8823   -- check error
8824   if(l_ae_err_rec.l_err_num <> 0 and l_ae_err_rec.l_err_num is not null) then
8825       raise process_error;
8826   end if;
8827 
8828 
8829 
8830 /********************************************************************************
8831 FP 12.1.3 bug 9356654 fix:
8832  ********************************************************************************/
8833 
8834 --{BUG#9356654
8835    select
8836    encumbrance_reversal_flag
8837    into
8838    l_enc_rev
8839    from
8840    mtl_parameters
8841    where organization_id = i_ae_txn_rec.organization_id;
8842 
8843   IF g_debug_flag = 'Y' THEN
8844    fnd_file.put_line(fnd_file.log,'Organization_id : '||(i_ae_txn_rec.organization_id));
8845    fnd_file.put_line(fnd_file.log,'l_enc_rev : '||(l_enc_rev));
8846    fnd_file.put_line(fnd_file.log,'transaction_id : '||(i_ae_txn_rec.transaction_id));
8847   END IF;
8848 
8849   if (l_enc_rev = 1 and nvl(i_ae_txn_rec.encum_amount,0) <> 0) THEN
8850        l_ae_enc_curr_rec.pri_currency := l_ae_curr_rec.pri_currency;
8851        BEGIN
8852        SELECT currency_code
8853           ,   currency_conversion_date
8854           ,   currency_conversion_type
8855           ,   currency_conversion_rate
8856         INTO
8857               l_ae_enc_curr_rec.alt_currency
8858              ,l_ae_enc_curr_rec.currency_conv_date
8859              ,l_ae_enc_curr_rec.currency_conv_type
8860              ,l_ae_enc_curr_rec.currency_conv_rate
8861         FROM wip_transaction_accounts
8862         WHERE transaction_id = i_ae_txn_rec.transaction_id
8863         AND   accounting_line_type = 15;
8864         EXCEPTION
8865           WHEN no_data_found THEN
8866            l_ae_enc_curr_rec := l_ae_curr_rec;
8867         END;
8868 
8869       encumbrance_account
8870                          (i_ae_txn_rec,
8871                           l_ae_enc_curr_rec,
8872                           l_ae_line_tbl,
8873                           l_ae_err_rec);
8874      -- check error;
8875      if(l_ae_err_rec.l_err_num <> 0 and l_ae_err_rec.l_err_num is not null) then
8876         raise process_error;
8877      end if;
8878 
8879   end if;
8880 
8881 --}
8882 
8883 -- Return the lines pl/sql table.
8884 -- ------------------------------
8885   l_stmt_num := 240;
8886   o_ae_line_rec_tbl := l_ae_line_tbl;
8887   IF g_debug_flag = 'Y' THEN
8888     fnd_file.put_line(fnd_file.log,'Create_wip_ae_lines >>');
8889   END IF;
8890 
8891 EXCEPTION
8892 
8893   when no_txn_det_error then
8894   o_ae_err_rec.l_err_num := 30012;
8895   o_ae_err_rec.l_err_code := 'CST_NO_TXN_DET';
8896   FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
8897   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
8898 
8899   when process_error then
8900   o_ae_err_rec.l_err_num := l_ae_err_rec.l_err_num;
8901   o_ae_err_rec.l_err_code := l_ae_err_rec.l_err_code;
8902   o_ae_err_rec.l_err_msg := l_ae_err_rec.l_err_msg;
8903 
8904   when others then
8905   o_ae_err_rec.l_err_num := SQLCODE;
8906   o_ae_err_rec.l_err_code := '';
8907   o_ae_err_rec.l_err_msg := 'CSTPAPBR.create_wip_ae_lines : ' || to_char(l_stmt_num) ||' : '||
8908   substr(SQLERRM,1,180);
8909 
8910 END create_wip_ae_lines;
8911 
8912 -- ===================================================================
8913 -- Get Account from the entities (Used in WIP transactions).
8914 -- ===================================================================
8915 procedure get_accts(
8916   i_ae_txn_rec                IN        CSTPALTY.cst_ae_txn_rec_type,
8917   i_ae_line_rec               IN        CSTPALTY.cst_ae_line_rec_type,
8918   l_ae_line_tbl               IN OUT NOCOPY    CSTPALTY.cst_ae_line_tbl_type,
8919   --i_ae_txn_rec                IN        CSTPALBR.cst_ae_txn_rec_type,
8920   --i_ae_line_rec               IN        CSTPALBR.cst_ae_line_rec_type,
8921   --l_ae_line_tbl               IN OUT    CSTPALBR.cst_ae_line_tbl_type,
8922   o_acct_id1                  OUT NOCOPY       NUMBER,
8923   o_acct_id2                  OUT NOCOPY       NUMBER,
8924   o_ae_err_rec                OUT NOCOPY       CSTPALTY.cst_ae_err_rec_type
8925   --o_ae_err_rec                OUT       CSTPALBR.cst_ae_err_rec_type
8926 ) IS
8927   l_stmt_num                            NUMBER;
8928   l_entity_type                         NUMBER;
8929   l_wip_entity_id                       NUMBER;
8930   l_line_id                             NUMBER;
8931   l_res_id                              NUMBER;
8932   l_acct_exist                                NUMBER;
8933   l_ae_err_rec                          CSTPALTY.cst_ae_err_rec_type;
8934   --l_ae_err_rec                          CSTPALBR.cst_ae_err_rec_type;
8935   no_dj_acct_error                      EXCEPTION;
8936   no_abs_acct_error                     EXCEPTION;
8937   no_wrs_acct_error                     EXCEPTION;
8938   no_wfs_acct_error                     EXCEPTION;
8939   process_error                         EXCEPTION;
8940 BEGIN
8941 
8942   IF g_debug_flag = 'Y' THEN
8943     fnd_file.put_line(fnd_file.log,'Get_accts <<');
8944   END IF;
8945 
8946   IF i_ae_txn_rec.txn_type_id <> 6 THEN
8947 
8948     -- Get Absorption Account.
8949     -- -----------------------
8950     l_stmt_num := 10;
8951  select count(resource_id)
8952     into l_acct_exist
8953     from bom_resources
8954     where resource_id = i_ae_line_rec.resource_id
8955     and organization_id = i_ae_txn_rec.organization_id
8956     and rownum < 2; /* 4586534 added rownum filter for perf as its only existence check */
8957 
8958     if (l_acct_exist = 0) then
8959       raise no_abs_acct_error;
8960     end if;
8961 
8962     l_stmt_num := 20;
8963     select nvl(absorption_account,-1)
8964     into o_acct_id2
8965     from bom_resources
8966     where resource_id = i_ae_line_rec.resource_id
8967     and organization_id = i_ae_txn_rec.organization_id;
8968 
8969   END IF; -- check for i_ae_txn_rec.txn_type_id <> 6
8970 
8971 
8972 -- Get Entity Type and Entity Id.
8973 -- ------------------------------
8974 -- Entity Types:
8975 -- 1 - Discrete Jobs
8976 -- 2 - Repetitive Assembly
8977 -- 3 - Closed Discrete Jobs
8978 -- 4 - Flow Schedule
8979 
8980   l_stmt_num := 30;
8981   select we.entity_type,
8982          we.wip_entity_id,
8983          wt.line_id
8984   into l_entity_type,
8985        l_wip_entity_id,
8986        l_line_id
8987   from wip_transactions wt,
8988        wip_entities we
8989   where we.wip_entity_id = wt.wip_entity_id
8990   and wt.transaction_id = i_ae_txn_rec.transaction_id;
8991 
8992   IF g_debug_flag = 'Y' THEN
8993     fnd_file.put_line(fnd_file.log, 'Entity Type: ' ||to_char(l_entity_type));
8994   END IF;
8995 
8996   /* Bug 6937298 - Modified the If condition below to include lot-based jobs
8997   if (i_ae_txn_rec.txn_type_id <> 6 and l_entity_type in (1,3, 6, 7)) or
8998      (i_ae_txn_rec.txn_type_id = 6 and l_entity_type in (1,3, 6, 7)) then   -- Include eAM jobs also*/
8999   if l_entity_type in (1,3,5,6,7,8) then
9000 
9001 -- Get Elemental Accounts from WDJ. For wip_txn_type = 6, get Elemental Variance Accounts, as well.
9002 -- ------------------------------------------------------------------------------------------------
9003      l_stmt_num := 40;
9004      select count(wip_entity_id) /* Changed for Bug No. 4586534 */
9005      into l_acct_exist
9006      from wip_discrete_jobs
9007      where wip_entity_id = l_wip_entity_id;
9008 
9009      if (l_acct_exist = 0) then
9010        raise no_dj_acct_error;
9011      end if;
9012 
9013      l_stmt_num := 50;
9014      select decode(i_ae_line_rec.cost_element_id,
9015                       1, nvl(material_account,-1),
9016                       2, nvl(material_overhead_account,-1),
9017                       3, nvl(resource_account,-1),
9018                       4, nvl(outside_processing_account,-1),
9019                       5, nvl(overhead_account,-1)),
9020             --decode(i_ae_txn_rec.wip_txn_type,
9021             decode(i_ae_txn_rec.txn_type_id,
9022                       6,decode(i_ae_line_rec.cost_element_id,
9023                                   1, nvl(material_variance_account,-1),
9024                                   2, nvl(material_variance_account,-1),
9025                                   3, nvl(resource_variance_account,-1),
9026                                   4, nvl(outside_proc_variance_account,-1),
9027                                   5, nvl(overhead_variance_account,-1)),
9028                         o_acct_id2)
9029      into o_acct_id1,
9030           o_acct_id2
9031      from wip_discrete_jobs
9032      where wip_entity_id = l_wip_entity_id;
9033   elsif l_entity_type = 2 then
9034 
9035 -- Get Elemental Accounts from WRS.
9036 -- --------------------------------
9037 
9038      l_stmt_num := 60;
9039     select count(wip_entity_id) /* Changed for bug No 4586534 */
9040      into l_acct_exist
9041      from wip_repetitive_schedules
9042      where wip_entity_id = l_wip_entity_id
9043      and line_id = l_line_id
9044      and rownum  < 2; /* 4586534 added rownum check as its only existence check */
9045 
9046      if (l_acct_exist = 0) then
9047        raise no_wrs_acct_error;
9048      end if;
9049 
9050      l_stmt_num := 70;
9051      select decode(i_ae_line_rec.cost_element_id,
9052                       1, nvl(material_account,-1),
9053                       2, nvl(material_overhead_account,-1),
9054                       3, nvl(resource_account,-1),
9055                       4, nvl(outside_processing_account,-1),
9056                       5, nvl(overhead_account,-1))
9057      into o_acct_id1
9058      from wip_repetitive_schedules
9059      where wip_entity_id = l_wip_entity_id
9060      and line_id = l_line_id
9061      and rownum = 1;
9062   elsif l_entity_type = 4 then
9063 
9064 -- Get Elemental Accounts from WFS.
9065 -- --------------------------------
9066 
9067      l_stmt_num := 80;
9068      select count(wip_entity_id) /* Changed For bug No. 4586534 */
9069      into l_acct_exist
9070      from wip_flow_schedules
9071      where wip_entity_id = l_wip_entity_id;
9072 
9073      if (l_acct_exist = 0) then
9074        raise no_wfs_acct_error;
9075      end if;
9076 
9077      l_stmt_num := 90;
9078      select decode(i_ae_line_rec.cost_element_id,
9079                       1, nvl(material_account,-1),
9080                       2, nvl(material_overhead_account,-1),
9081                       3, nvl(resource_account,-1),
9082                       4, nvl(outside_processing_account,-1),
9083                       5, nvl(overhead_account,-1)),
9084             decode(i_ae_txn_rec.txn_type_id,
9085                       6,decode(i_ae_line_rec.cost_element_id,
9086                                   1, nvl(material_variance_account,-1),
9087                                   2, nvl(material_variance_account,-1),
9088                                   3, nvl(resource_variance_account,-1),
9089                                   4, nvl(outside_proc_variance_account,-1),
9090                                   5, nvl(overhead_variance_account,-1)),
9091                         o_acct_id2)
9092      into o_acct_id1,
9093           o_acct_id2
9094      from wip_flow_schedules
9095      where wip_entity_id = l_wip_entity_id;
9096   end if;
9097   IF g_debug_flag = 'Y' THEN
9098     fnd_file.put_line(fnd_file.log,'Get_accts >>');
9099   END IF;
9100 
9101 EXCEPTION
9102 
9103   when no_abs_acct_error then
9104   o_ae_err_rec.l_err_num := 30013;
9105   o_ae_err_rec.l_err_code := 'CST_PAC_NO_TXN_ACCTS';
9106   FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_TXN_ACCTS');
9107   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
9108 
9109   when no_wrs_acct_error then
9110   o_ae_err_rec.l_err_num := 30014;
9111   o_ae_err_rec.l_err_code := 'CST_PAC_NO_TXN_ACCTS';
9112   FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_TXN_ACCTS');
9113   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
9114 
9115   when no_wfs_acct_error then
9116   o_ae_err_rec.l_err_num := 30015;
9117   o_ae_err_rec.l_err_code := 'CST_PAC_NO_TXN_ACCTS';
9118   FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_TXN_ACCTS');
9119   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
9120 
9121   when no_dj_acct_error then
9122   o_ae_err_rec.l_err_num := 30016;
9123   o_ae_err_rec.l_err_code := 'CST_PAC_NO_TXN_ACCTS';
9124   FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_TXN_ACCTS');
9125   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
9126 
9127   when process_error then
9128   o_ae_err_rec.l_err_num := l_ae_err_rec.l_err_num;
9129   o_ae_err_rec.l_err_code := l_ae_err_rec.l_err_code;
9130   o_ae_err_rec.l_err_msg := l_ae_err_rec.l_err_msg;
9131 
9132   when others then
9133   o_ae_err_rec.l_err_num := SQLCODE;
9134   o_ae_err_rec.l_err_code := '';
9135   o_ae_err_rec.l_err_msg := 'CSTPAPBR.get_accts ' || to_char(l_stmt_num) ||' '||
9136   substr(SQLERRM,1,180);
9137 
9138 END get_accts;
9139 
9140 -- ===================================================================
9141 -- WIP Accounts.
9142 -- ===================================================================
9143 procedure WIP_accounts(
9144    i_ae_txn_rec     IN          CSTPALTY.cst_ae_txn_rec_type,
9145    i_ae_curr_rec    IN          CSTPALTY.cst_ae_curr_rec_type,
9146    i_acct_line_type IN          NUMBER,
9147    i_ovhd_absp      IN          NUMBER,
9148    i_dr_flag        IN          BOOLEAN,
9149    i_ae_acct_rec    IN          CSTPALTY.cst_ae_acct_rec_type,
9150    l_ae_line_tbl    IN OUT NOCOPY      CSTPALTY.cst_ae_line_tbl_type,
9151    o_ae_err_rec     OUT NOCOPY          CSTPALTY.cst_ae_err_rec_type
9152 )IS
9153   l_ae_line_rec                 CSTPALTY.cst_ae_line_rec_type;
9154   l_ele_exist                   NUMBER;
9155   l_cost                        NUMBER;
9156   l_dr_flag                     BOOLEAN;
9157   l_wip_variance                NUMBER;
9158   l_stmt_num                    NUMBER;
9159   l_err_rec                     CSTPALTY.cst_ae_err_rec_type;
9160   l_acct_rec                    CSTPALTY.cst_ae_acct_rec_type;
9161   process_error                 EXCEPTION;
9162   no_txn_det_error              EXCEPTION;
9163 BEGIN
9164   IF g_debug_flag = 'Y' THEN
9165     fnd_file.put_line(fnd_file.log,'WIP_accounts << ');
9166   END IF;
9167 -- initialize variables.
9168 -- ---------------------
9169   l_err_rec.l_err_num := 0;
9170   l_err_rec.l_err_code := '';
9171   l_err_rec.l_err_msg := '';
9172 
9173      l_stmt_num := 10;
9174      SELECT COUNT(*)
9175      INTO   l_ele_exist
9176      FROM   MTL_PAC_ACTUAL_COST_DETAILS
9177      WHERE  transaction_id = i_ae_txn_rec.transaction_id
9178      AND    pac_period_id = i_ae_txn_rec.accounting_period_id
9179      AND    cost_group_id = i_ae_txn_rec.cost_group_id;
9180 
9181      IF (l_ele_exist = 0) THEN
9182        raise no_txn_det_error;
9183      END IF;
9184 
9185      FOR cost_element IN 1..5 LOOP
9186 
9187         ----------------------------------------------------------------------------
9188         -- wip_variance column value is NULL for txns otherthan Assembly Completion,
9189         -- Assembly Return,Assembly Scrap and Assembly Scrap returns.
9190 
9191         -- For Assembly Completion txn => wip_variance is non zero then,
9192         -- Following entries are created in WIP_accounts procedure
9193         --
9194         -- Account Name               Dr                   Cr
9195         -- ------------              ----                 ---
9196         -- WIP Variance               wip_variance
9197         -- WIP Valuation                                   wip_variance
9198 
9199         -- For Assembly Return txn => wip_variance is non zero then,
9200         -- Following entries are created  in WIP_accounts procedure
9201         --
9202         -- Account Name               Dr                   Cr
9203         -- ------------              ----                 ---
9204         -- WIP Variance                                   wip_variance
9205         -- WIP Valuation            wip_variance
9206         --
9207         -- For Assembly Scrap txn => wip_variance is non zero then,
9208         -- Following entries are created in WIP_accounts procedure
9209         --
9210         -- Account Name               Dr                   Cr
9211         -- ------------              ----                 ---
9212         -- WIP Valuation                                   wip_variance
9213         -- Scrap Account              0
9214         -- WIP variance Account       wip_variance
9215 
9216 
9217         -- For Scrap returns txn => wip_variance is non zero then,
9218         -- Following entries are created in WIP_accounts procedure
9219         --
9220         -- Account Name               Dr                   Cr
9221         -- ------------              ----                 ---
9222         -- WIP Valuation              wip_variance
9223         -- Scrap Account                                    0
9224         -- WIP variance Account                         wip_variance
9225         --
9226         ----------------------------------------------------------------------------
9227          -- i_ovhd_absp indicates which level of material overhead we are
9228         -- absorbtion and therefore need to go in an absorption account.
9229         -- 2 means both levels and 1 means this level only.
9230         -- -------------------------------------------------------------
9231               l_stmt_num := 20;
9232               SELECT SUM(nvl(actual_cost,0)) ,
9233                      SUM(nvl(wip_variance,0))
9234               INTO   l_cost,
9235                      l_wip_variance
9236               FROM   mtl_pac_actual_cost_details
9237               WHERe  transaction_id = i_ae_txn_rec.transaction_id
9238               AND    pac_period_id = i_ae_txn_rec.accounting_period_id
9239               AND cost_group_id = i_ae_txn_rec.cost_group_id
9240               AND cost_element_id = cost_element
9241               AND (cost_element_id <> 2
9242                    OR
9243                    (cost_element_id = 2
9244                     and level_type = decode(i_ovhd_absp,1,2,2,0,level_type)));
9245 
9246               IF (l_cost is not null) then
9247 
9248                 l_stmt_num := 30;
9249                 l_ae_line_rec.account := CSTPAPHK.get_account_id (
9250                                                 i_ae_txn_rec.transaction_id,
9251                                                 i_ae_txn_rec.legal_entity_id,
9252                                                 i_ae_txn_rec.cost_type_id,
9253                                                 i_ae_txn_rec.cost_group_id,
9254                                                 i_dr_flag,
9255                                                 i_acct_line_type,
9256                                                 cost_element,
9257                                                 NULL,
9258                                                 i_ae_txn_rec.subinventory_code,
9259                                                 "FALSE", ---  i_exp_flag
9260                                                 l_err_rec.l_err_num,
9261                                                 l_err_rec.l_err_code,
9262                                                 l_err_rec.l_err_msg);
9263                 -- check error
9264                 if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
9265                 raise process_error;
9266                 end if;
9267 
9268                 IF (l_ae_line_rec.account = -1) then
9269                    l_stmt_num := 40;
9270                    select decode(cost_element, 1, i_ae_acct_rec.mat_account,
9271                                           2, i_ae_acct_rec.mat_ovhd_account,
9272                                           3, i_ae_acct_rec.res_account,
9273                                           4, i_ae_acct_rec.osp_account,
9274                                           5, i_ae_acct_rec.ovhd_account)
9275                    into l_ae_line_rec.account
9276                    from dual;
9277                 END IF;
9278 
9279                 l_ae_line_rec.resource_id := NULL;
9280                 l_ae_line_rec.cost_element_id := cost_element;
9281                 l_ae_line_rec.ae_line_type := i_acct_line_type;
9282                 l_ae_line_rec.transaction_value := abs(i_ae_txn_rec.primary_quantity) * l_cost + l_wip_variance;
9283 
9284                 insert_account (i_ae_txn_rec,
9285                                 i_ae_curr_rec,
9286                                 i_dr_flag,
9287                                 l_ae_line_rec,
9288                                 l_ae_line_tbl,
9289                                 l_err_rec);
9290                 -- check error
9291                 IF(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) THEN
9292                    RAISE process_error;
9293                 END IF;
9294               ELSE
9295                  IF g_debug_flag = 'Y' THEN
9296                     fnd_file.put_line(fnd_file.log,'No Cost for element...');
9297                 END IF;
9298               END IF;
9299 
9300             ---------------------------------------------------------------------------------------
9301             -- Execute the below code only
9302             -- If wip_variance is less than zero and only for Discrete Jobs and Repetitiveschedules
9303             ---------------------------------------------------------------------------------------
9304             IF ( l_wip_variance is NOT NULL AND l_wip_variance < 0) THEN
9305                   ------------------------------------------------------------------------------------------------
9306                   -- Get WIP Variance Accounts only If txn is AssemblyCompletion/AssemblyReturn/Scrap/ScrapReturn
9307                   ------------------------------------------------------------------------------------------------
9308                     l_stmt_num := 58;
9309                    -------------------------------------------------------------------------
9310                    -- Get the Variance Accounts based Job type
9311                    -- If Job type is Repetitive then get from table WIP_REPETITIVE_SCHEDULES
9312                    -- If Job type is Discrete then get from table WIP_DISCRETE_JOBS
9313                    -------------------------------------------------------------------------
9314                    IF ((i_ae_txn_rec.wip_entity_type = 1) OR (i_ae_txn_rec.wip_entity_type = 3)) THEN
9315 
9316                        SELECT decode(cost_element, 1, material_variance_account,
9317                                                    2, material_variance_account, -- Using Material Variance acct for cost element 2 also
9318                                                    3, resource_variance_account,
9319                                                    4, outside_proc_variance_account,
9320                                                    5, overhead_variance_account)
9321                        INTO  l_ae_line_rec.account
9322                        FROM  WIP_DISCRETE_JOBS
9323                        WHERE organization_id = i_ae_txn_rec.organization_id
9324                        AND   wip_entity_id = i_ae_txn_rec.txn_src_id;
9325                    ELSE
9326                        SELECT decode(cost_element, 1, material_variance_account,
9327                                                    2, material_variance_account, -- Using Material Variance acct for cost element 2 also
9328                                                    3, resource_variance_account,
9329                                                    4, outside_proc_variance_account,
9330                                                    5, overhead_variance_account)
9331                        INTO  l_ae_line_rec.account
9332                        FROM  WIP_REPETITIVE_SCHEDULES
9333                        WHERE organization_id = i_ae_txn_rec.organization_id
9334                        AND   wip_entity_id = i_ae_txn_rec.txn_src_id;
9335                    END IF;
9336 
9337                    l_stmt_num := 59;
9338                    ----------------------------------------------------
9339                    -- Flip the i_dr_flag.
9340                    -- For Assembly Completion WIP valuation  Cr
9341                    --     Assembly Completion WIP Variance   Dr
9342                    -------- --------------------------------------------
9343                    l_dr_flag := NOT i_dr_flag;
9344 
9345                    -----------------------------------------------
9346                    -- Set the accounting line type to WIP Variance
9347                    -----------------------------------------------
9348                    l_ae_line_rec.ae_line_type := 8;
9349                    l_ae_line_rec.transaction_value := l_wip_variance;
9350                    l_ae_line_rec.resource_id := NULL;
9351                    l_ae_line_rec.cost_element_id := cost_element;
9352                    ---------------------------------------------------
9353                    -- Insert the transaction details into PL/SQL table
9354                    ---------------------------------------------------
9355                    insert_account (i_ae_txn_rec,
9356                                    i_ae_curr_rec,
9357                                    l_dr_flag,
9358                                    l_ae_line_rec,
9359                                    l_ae_line_tbl,
9360                                    l_err_rec);
9361                    -- check error
9362                    IF(l_err_rec.l_err_num<>0 AND l_err_rec.l_err_num IS NOT NULL) THEN
9363                      RAISE process_error;
9364                    END IF;
9365                END IF; -- End of IF l_wip_variance <> 0
9366     END LOOP;
9367 
9368   IF g_debug_flag = 'Y' THEN
9369     fnd_file.put_line(fnd_file.log,'WIP_accounts >>');
9370   END IF;
9371 
9372   EXCEPTION
9373 
9374   when no_txn_det_error then
9375   o_ae_err_rec.l_err_num := 30010;
9376   o_ae_err_rec.l_err_code := 'CST_NO_TXN_DET';
9377   FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
9378   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
9379 
9380   when process_error then
9381   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
9382   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
9383   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
9384 
9385   when others then
9386   o_ae_err_rec.l_err_num := SQLCODE;
9387   o_ae_err_rec.l_err_code := '';
9388   o_ae_err_rec.l_err_msg := 'CSTPAPBR.offset_accounts' || to_char(l_stmt_num) ||
9389   substr(SQLERRM,1,180);
9390 
9391 end WIP_accounts;
9392 
9393 -- ========================================================================
9394 -- GET Intercompany Account For Transfer Pricing.
9395 -- This Procedure uses the same logic as in CST_TPRICE_PVT.Adjust_acct
9396 -- to get the intercompany accrual account id
9397 -- ========================================================================
9398 Function Get_Intercompany_account(
9399    i_ae_txn_rec     IN          CSTPALTY.cst_ae_txn_rec_type,
9400    o_ae_err_rec     OUT NOCOPY  CSTPALTY.cst_ae_err_rec_type) RETURN NUMBER
9401 IS
9402    l_snd_sob_id     NUMBER;
9403    l_snd_curr       GL_SETS_OF_BOOKS.CURRENCY_CODE%TYPE;
9404    l_rcv_sob_id     NUMBER;
9405    l_rcv_coa_id     NUMBER;
9406    l_rcv_curr       GL_SETS_OF_BOOKS.CURRENCY_CODE%TYPE;
9407    l_curr_type      MTL_MATERIAL_TRANSACTIONS.CURRENCY_CONVERSION_TYPE%TYPE;
9408    l_conv_rate      NUMBER;
9409    l_conv_date      DATE;
9410    l_err_rec        CSTPALTY.cst_ae_err_rec_type;
9411    l_from_org       NUMBER;
9412    l_to_org         NUMBER;
9413    l_from_ou        NUMBER;
9414    l_to_ou          NUMBER;
9415    l_line_id        NUMBER;
9416    l_header_id      NUMBER;
9417    l_cust_id        NUMBER;
9418    l_order_type     OE_ORDER_HEADERS_ALL.ORDER_TYPE_ID%TYPE;
9419    l_ship_num       NUMBER;
9420    l_req_line       NUMBER;
9421    l_stmt_num       NUMBER;
9422    l_pd_txfr_ind    NUMBER;
9423    l_inv_ccid       NUMBER;
9424    l_concat_seg     GL_CODE_COMBINATIONS_KFV.CONCATENATED_SEGMENTS%TYPE;
9425    /* OPM CONV Bug: 5478266. Vars needed to get COGS account*/
9426    l_cogs_ccid      NUMBER;
9427    l_concat_id      VARCHAR2(2000);
9428    l_concat_desc    VARCHAR2(2000);
9429    l_msg_count      NUMBER;
9430    l_msg_data       VARCHAR2(2000);
9431    l_acct_ccid      NUMBER;
9432    l_txf_txn_id     NUMBER;
9433 
9434    process_error    EXCEPTION;
9435    inv_exp_acct_err EXCEPTION;
9436    no_txn_det_error EXCEPTION;
9437 
9438    l_shipping_cg_id NUMBER;
9439 
9440 BEGIN
9441 
9442   l_inv_ccid := -1;
9443 
9444   IF g_debug_flag = 'Y' THEN
9445     fnd_file.put_line(fnd_file.log,'Get_Intercompany_account << ');
9446   END IF;
9447 
9448   l_stmt_num := 5;
9449   SELECT MOD(SUM(DECODE(MP.process_enabled_flag,'Y',1,2)), 2)
9450     INTO l_pd_txfr_ind
9451     FROM mtl_parameters mp
9452    WHERE mp.organization_id = i_ae_txn_rec.organization_id
9453       OR mp.organization_id = i_ae_txn_rec.xfer_organization_id;
9454 
9455   IF g_debug_flag = 'Y' THEN
9456     FND_FILE.put_line(FND_FILE.LOG, 'l_pd_txfr_ind:' ||  l_pd_txfr_ind);
9457   END IF;
9458 
9459   l_stmt_num := 7;
9460 
9461   BEGIN
9462     SELECT cost_group_id
9463     INTO l_shipping_cg_id
9464     FROM cst_cost_group_assignments
9465     WHERE organization_id = (SELECT decode(i_ae_txn_rec.txn_action_id,
9466                                             21, organization_id,
9467                                             22, organization_id, -- Logical Shipping txn
9468                                             12, transfer_organization_id,
9469                                             15, transfer_organization_id, -- Logical Receipt txn
9470                                             3, decode(sign(i_ae_txn_rec.primary_quantity),
9471                                                         1, transfer_organization_id,
9472                                                             organization_id),
9473                                               NULL)
9474                             FROM mtl_material_transactions
9475                             WHERE transaction_id = i_ae_txn_rec.transaction_id);
9476     EXCEPTION
9477     WHEN no_data_found THEN
9478       l_shipping_cg_id := -1;
9479   END;
9480 
9481   IF g_debug_flag = 'Y' THEN
9482     FND_FILE.put_line(FND_FILE.LOG, 'l_shipping_cg_id:' ||  l_shipping_cg_id);
9483   END IF;
9484 
9485   if (i_ae_txn_rec.txn_action_id in (21, 22)) then
9486      l_from_org := i_ae_txn_rec.organization_id;
9487      l_to_org := i_ae_txn_rec.xfer_organization_id;
9488   elsif (i_ae_txn_rec.txn_action_id in (12, 15)) then
9489      l_from_org := i_ae_txn_rec.xfer_organization_id;
9490      l_to_org := i_ae_txn_rec.organization_id;
9491   end if;
9492 
9493   l_stmt_num := 10;
9494   CSTPAVCP.get_snd_rcv_rate  (I_TXN_ID     => i_ae_txn_rec.transaction_id,
9495                               I_FROM_ORG   => l_from_org,
9496                               I_TO_ORG     => l_to_org,
9497                               O_SND_SOB_ID => l_snd_sob_id,
9498                               O_SND_CURR   => l_snd_curr,
9499                               O_RCV_SOB_ID => l_rcv_sob_id,
9500                               O_RCV_CURR   => l_rcv_curr,
9501                               O_CURR_TYPE  => l_curr_type,
9502                               O_CONV_RATE  => l_conv_rate,
9503                               O_CONV_DATE  => l_conv_date,
9504                               O_Err_Num    => l_err_rec.l_err_num,
9505                               O_Err_Code   => l_err_rec.l_err_code,
9506                               O_Err_Msg    => l_err_rec.l_err_msg);
9507 
9508    if (l_err_rec.l_err_num <> 0) then
9509       raise process_error;
9510    end if;
9511 
9512   l_stmt_num := 20;
9513   SELECT chart_of_accounts_id
9514   INTO   l_rcv_coa_id
9515   FROM   gl_sets_of_books
9516   WHERE  set_of_books_id = l_rcv_sob_id;
9517 
9518   l_stmt_num := 30;
9519   SELECT to_number(org_information3)
9520   INTO   l_from_ou
9521   FROM   hr_organization_information
9522   WHERE  org_information_context = 'Accounting Information'
9523   AND    organization_id = l_from_org;
9524 
9525   l_stmt_num := 35;
9526   SELECT to_number(org_information3)
9527   INTO   l_to_ou
9528   FROM   hr_organization_information
9529   WHERE  org_information_context = 'Accounting Information'
9530   AND    organization_id = l_to_org;
9531 
9532   if (g_debug_flag = 'Y') then
9533       FND_FILE.put_line(FND_FILE.LOG, 'l_from_ou = ' || l_from_ou ||
9534                  ' l_to_ou = ' || l_to_ou || ' l_rcv_coa_id = ' || l_rcv_coa_id);
9535   end if;
9536 
9537   if (i_ae_txn_rec.txn_action_id in (15, 21)) then
9538      l_stmt_num := 40;
9539 
9540      SELECT MMT.trx_source_line_id, OEH.header_id, OEH.sold_to_org_id, OEH.order_type_id
9541      INTO   l_line_id, l_header_id, l_cust_id, l_order_type
9542      FROM   mtl_material_transactions MMT, oe_order_headers_all OEH, oe_order_lines_all OEL
9543      WHERE  MMT.transaction_id = i_ae_txn_rec.transaction_id
9544      AND    OEL.line_id = MMT.trx_source_line_id
9545      AND    OEL.header_id = OEH.header_id;
9546 
9547   elsif (i_ae_txn_rec.txn_action_id in (12, 22)) then
9548 
9549       l_stmt_num := 50;
9550 
9551       SELECT MMT.shipment_number, RT.requisition_line_id,
9552       -- For action id 22, transfer_transaction_id points to receiving txn, not the shipping txn,
9553       -- because it got created after goods are received, so goto else part...
9554       decode(l_pd_txfr_ind, 1, 0, nvl(MMT.transfer_transaction_id, 0))
9555       INTO l_ship_num, l_req_line, l_txf_txn_id
9556       FROM mtl_material_transactions MMT, rcv_transactions RT
9557       WHERE MMT.transaction_id = i_ae_txn_rec.transaction_id
9558       AND   RT.transaction_id = MMT.rcv_transaction_id;
9559 
9560 
9561      if (l_txf_txn_id <> 0) then
9562 
9563         l_stmt_num := 60;
9564 
9565         SELECT MMT.trx_source_line_id, OEH.header_id, OEH.sold_to_org_id, OEH.order_type_id
9566         INTO   l_line_id, l_header_id, l_cust_id, l_order_type
9567         FROM   mtl_material_transactions MMT, oe_order_headers_all OEH, oe_order_lines_all OEL
9568         WHERE  MMT.transaction_id = i_ae_txn_rec.xfer_transaction_id
9569         AND    MMT.trx_source_line_id = OEL.line_id
9570         AND    OEL.header_id = OEH.header_id;
9571 
9572      else
9573 
9574         l_stmt_num := 70;
9575 
9576         SELECT MMT.trx_source_line_id, OEH.header_id, OEH.sold_to_org_id, OEH.order_type_id
9577         INTO   l_line_id, l_header_id, l_cust_id, l_order_type
9578         FROM   mtl_material_transactions MMT, oe_order_headers_all OEH, oe_order_lines_all OEL
9579         WHERE  MMT.transaction_action_id = 21
9580         AND    MMT.transaction_source_type_id = 8
9581         AND    MMT.organization_id = l_from_org
9582         AND    MMT.inventory_item_id = i_ae_txn_rec.inventory_item_id
9583         AND    MMT.shipment_number = l_ship_num
9584         AND    MMT.trx_source_line_id = OEL.line_id
9585         AND    OEL.source_document_line_id = l_req_line
9586         AND    OEL.header_id = OEH.header_id
9587         AND    ROWNUM = 1;
9588 
9589      end if;
9590   end if;
9591 
9592   /* For Discrete-Discrete transfers, derive COGS account if called for
9593      Shipping CG else derive accrual account if called for receiving CG */
9594 
9595   /* OPM CONV Bug: 5478266. Code to get COGS account*/
9596   if (((l_pd_txfr_ind = 0) and (i_ae_txn_rec.cost_group_id = l_shipping_cg_id))
9597       OR ((l_pd_txfr_ind = 1) and (i_ae_txn_rec.txn_action_id in (21, 22)))) then
9598 
9599     l_stmt_num := 80;
9600 
9601     mo_global.set_policy_context('S',l_from_ou);
9602 
9603     if (OE_FLEX_COGS_PUB.start_process(1.0,
9604                                        l_line_id,
9605                                        l_cogs_ccid,
9606                                        l_concat_seg,
9607                                        l_concat_id,
9608                                        l_concat_desc,
9609                                        l_msg_count,
9610                                        l_msg_data) <> FND_API.g_ret_sts_success) then
9611       l_stmt_num := 90;
9612 
9613       SELECT nvl(MSI.cost_of_sales_account, MP.cost_of_sales_account)
9614       INTO   l_cogs_ccid
9615       FROM   mtl_system_items MSI, mtl_parameters MP
9616       WHERE  MSI.organization_id = l_from_org
9617       AND    MSI.inventory_item_id = i_ae_txn_rec.inventory_item_id
9618       AND    MP.organization_id = MSI.organization_id;
9619 
9620     end if;
9621 
9622     if (g_debug_flag = 'Y') then
9623       FND_FILE.put_line(FND_FILE.LOG, 'l_cogs_ccid = ' || l_cogs_ccid);
9624     end if;
9625 
9626     /* Return COGS Account */
9627     l_acct_ccid := l_cogs_ccid;
9628 
9629   end if;
9630 
9631   if (((l_pd_txfr_ind = 0) and (i_ae_txn_rec.cost_group_id <> l_shipping_cg_id))
9632       OR ((l_pd_txfr_ind = 1) and (i_ae_txn_rec.txn_action_id in (12, 15))))  then
9633 
9634     l_stmt_num := 100;
9635 
9636     IF NOT INV_WORKFLOW.call_generate_cogs(l_rcv_coa_id,
9637                                            l_cust_id,
9638                                            i_ae_txn_rec.inventory_item_id,
9639                                            l_header_id,
9640                                            l_line_id,
9641                                            l_order_type,
9642                                            l_to_ou,
9643                                            l_inv_ccid,
9644                                            l_concat_seg,
9645                                            l_err_rec.l_err_msg)
9646     THEN
9647        RAISE inv_exp_acct_err;
9648     END IF;
9649 
9650     if (g_debug_flag = 'Y') then
9651        FND_FILE.put_line(FND_FILE.LOG, 'l_inv_ccid = ' || l_inv_ccid);
9652     end if;
9653 
9654     /* Return IC Accrual Account */
9655     l_acct_ccid := l_inv_ccid;
9656 
9657   end if;
9658 
9659 
9660   IF g_debug_flag = 'Y' THEN
9661     FND_FILE.put_line(FND_FILE.LOG, 'l_acct_ccid = ' || l_acct_ccid);
9662     fnd_file.put_line(fnd_file.log,'Get_Intercompany_account >>');
9663   END IF;
9664 
9665   return l_acct_ccid;
9666 
9667   EXCEPTION
9668 
9669   when no_txn_det_error then
9670   o_ae_err_rec.l_err_num := 30010;
9671   o_ae_err_rec.l_err_code := 'CST_NO_TXN_DET';
9672   FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
9673   o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
9674 
9675   when process_error then
9676   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
9677   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
9678   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
9679 
9680   when others then
9681   o_ae_err_rec.l_err_num := SQLCODE;
9682   o_ae_err_rec.l_err_code := '';
9683   o_ae_err_rec.l_err_msg := 'CSTPAPBR.Get_Intercompany_account ' || to_char(l_stmt_num) ||
9684   substr(SQLERRM,1,180);
9685 END Get_Intercompany_account;
9686 
9687 
9688 /* --=======================================================================
9689 -- Internal Order Issue to Expense and Logical Expense Requisition Rcpt trxn
9690 -- This Procedure creates distributions for Internal Order Issue trxn 34-1-8
9691 -- and Logical Expense Requisition Receipt trxn 27-17-7
9692 -- Discrete-Discrete and OPM-Discrete X-fers are handled in the procedure
9693 
9694 -- Case I Dis-Dis, same CG, treat as SubInv transfer (Done)
9695 -- Internal Order Issue 34-1-8 :
9696 --   Dr. Expense @ PWAC
9697 --    Cr. Inv Val @ PWAC
9698 -- Logical Exp 27-17-7 :
9699 --   NO accounting
9700 
9701 -- Case II Dis-Dis, across CG. Sending CG
9702 -- Internal Order Issue 34-1-8 :
9703 --   Dr. Receivables Sending Org cost (from CPIC/Prior Prd PWAC/PerpMTA)
9704 --    Cr. Inv Val @ PWAC
9705 --    Cr. InterOrg Profit @ PWAC-(Prior Prd/PerpMTA)
9706 
9707 -- Case III Dis-Dis, across CG. Receiving CG
9708 -- Logical Exp 27-17-7 :
9709 --   Dr. Expense @ Sending Org cost (from CPIC/Prior Prd PWAC/PerpMTA)
9710 --    Cr. Payables @ Sending Org cost (from CPIC/Prior Prd PWAC/PerpMTA)
9711 
9712 -- Case IV Dis-OPM Xfer, Discrete is Sending CG (Done)
9713 -- Internal Order Issue 34-1-8 :
9714 --   Dr. Receivables @ Transfer Price
9715 --    Cr. Inv Val @ PWAC
9716 --    Cr. Inter-org Profit @ TP-PWAC
9717 
9718 -- Case V OPM-Dis Xfer, Discrete is Receiving CG (Done)
9719 -- Logical Exp 27-17-7 :
9720 --   Dr. Expense @ Transfer price
9721 --    Cr. Payables @ Transfer price
9722 
9723 -- =======================================================================*/
9724 PROCEDURE cost_internal_order_exp_txn(
9725   i_ae_txn_rec       IN             CSTPALTY.cst_ae_txn_rec_type,
9726   i_ae_curr_rec      IN             CSTPALTY.cst_ae_curr_rec_type,
9727   l_ae_line_tbl      IN OUT NOCOPY  CSTPALTY.cst_ae_line_tbl_type,
9728   o_ae_err_rec       OUT NOCOPY     CSTPALTY.cst_ae_err_rec_type
9729 )
9730 IS
9731   l_exp_sub                     NUMBER;
9732   l_exp_flag                    BOOLEAN;
9733   l_dr_flag                     BOOLEAN;
9734   l_stmt_num                    NUMBER := 0;
9735   l_acct_rec                    CSTPALTY.cst_ae_acct_rec_type;
9736   l_err_rec                     CSTPALTY.cst_ae_err_rec_type;
9737   l_ae_line_rec                 CSTPALTY.cst_ae_line_rec_type;
9738   l_txn_cost_group_id           NUMBER;
9739   l_txfr_txn_cost_group_id      NUMBER;
9740 
9741   l_mfa_exp_account             NUMBER;
9742   l_mmt_exp_account             NUMBER;
9743 
9744   l_io_rcv_acct                 NUMBER;
9745   l_io_pay_acct                 NUMBER;
9746   l_io_rcv_value                NUMBER;
9747   l_io_pay_value                NUMBER;
9748   l_io_txfr_cr_acct             NUMBER;
9749   l_io_txfr_var_acct            NUMBER;
9750   l_txfr_legal_entity           NUMBER;
9751   l_txfr_cost_group_id          NUMBER;
9752   l_same_le_ct                  NUMBER;
9753   l_mpacd_cost                  NUMBER;
9754   l_txfr_var_value              NUMBER;
9755   l_pwac_cost                   NUMBER;
9756   l_profit_or_loss              NUMBER;
9757 
9758   l_transfer_price              NUMBER;
9759   l_pd_txfr_ind                 NUMBER := 0;
9760 
9761   l_pacp_flag                   NUMBER;
9762   l_pacp_pwac_cost              NUMBER;
9763   l_prev_period_flag            NUMBER;
9764   l_prev_period_pwac_cost       NUMBER;
9765   l_perp_ship_flag              NUMBER;
9766   l_perp_ship_value             NUMBER;
9767   l_txfr_credit                 NUMBER;
9768 
9769   process_error                 EXCEPTION;
9770   no_mfca_acct_error            EXCEPTION;
9771   no_interorg_profit_acct_error EXCEPTION;
9772 
9773   l_api_name   CONSTANT VARCHAR2(240) := 'CSTPAPBR.cost_internal_order_exp_txn';
9774 
9775 BEGIN
9776 
9777   IF g_debug_flag = 'Y' THEN
9778     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': begin <<'
9779                       || ' transaction_id: ' || i_ae_txn_rec.transaction_id);
9780   END IF;
9781 
9782   -- Initialize variables.
9783   -- ---------------------
9784   l_err_rec.l_err_num := 0;
9785   l_err_rec.l_err_code := '';
9786   l_err_rec.l_err_msg := '';
9787 
9788   IF i_ae_txn_rec.exp_item = 1 THEN
9789 
9790     l_stmt_num := 10;
9791 
9792     IF g_debug_flag = 'Y' THEN
9793       fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': return >> ' ||
9794               '(No accounting for Expense item)');
9795     END IF;
9796 
9797     RETURN;
9798   END IF;
9799 
9800     l_stmt_num := 20;
9801 
9802     IF (i_ae_txn_rec.subinventory_code IS NULL) THEN
9803 
9804       l_stmt_num := 30;
9805 
9806       /* Get the expense sub or asset sub from the issue sub for logical exp req rcpt */
9807       SELECT decode(asset_inventory,1,0,1)
9808         INTO  l_exp_sub
9809         FROM  mtl_secondary_inventories
9810        WHERE secondary_inventory_name = i_ae_txn_rec.xfer_subinventory
9811          AND organization_id = i_ae_txn_rec.xfer_organization_id;
9812 
9813     ELSE
9814 
9815       l_stmt_num := 40;
9816 
9817       SELECT decode(asset_inventory,1,0,1)
9818       INTO  l_exp_sub
9819       FROM  mtl_secondary_inventories
9820       WHERE secondary_inventory_name = i_ae_txn_rec.subinventory_code
9821         AND   organization_id = i_ae_txn_rec.organization_id;
9822 
9823     END IF;
9824 
9825   IF (l_exp_sub = 1) THEN
9826     l_exp_flag := TRUE;
9827   ELSE
9828     l_exp_flag := FALSE;
9829   END IF;
9830 
9831   l_stmt_num := 50;
9832 
9833   BEGIN
9834     l_stmt_num := 60;
9835     SELECT nvl(expense_account, -1)
9836     INTO  l_mfa_exp_account
9837     FROM  mtl_fiscal_cat_accounts
9838     WHERE legal_entity_id = i_ae_txn_rec.legal_entity_id
9839     AND   cost_type_id    = i_ae_txn_rec.cost_type_id
9840     AND   cost_group_id   = i_ae_txn_rec.cost_group_id
9841     AND   category_id     = i_ae_txn_rec.category_id;
9842   EXCEPTION
9843     WHEN no_data_found THEN
9844       l_stmt_num := 70;
9845       fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num ||
9846               ': Category : '||to_char(i_ae_txn_rec.category_id) ||' has no accounts defined');
9847       RAISE no_mfca_acct_error;
9848   END;
9849 
9850 
9851   /* To determine whether this is a same cost group transfer or
9852      cross cost group transfer, we need to compare the transaction org
9853      cost group with the transfer transaction org cost group */
9854   BEGIN
9855     l_stmt_num := 80;
9856     SELECT NVL(cost_group_id, -1)
9857     INTO   l_txn_cost_group_id
9858     FROM   cst_cost_group_assignments
9859     WHERE  organization_id = i_ae_txn_rec.organization_id;
9860   EXCEPTION
9861     WHEN no_data_found THEN
9862       l_txn_cost_group_id := -1;
9863   END;
9864 
9865   l_txfr_txn_cost_group_id := NVL(i_ae_txn_rec.xfer_cost_group_id, -1);
9866 
9867   l_stmt_num := 90;
9868 
9869   IF g_debug_flag = 'Y' THEN
9870       fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
9871                         ': cost_group_id = ' || i_ae_txn_rec.cost_group_id ||
9872                         ': txn_cost_group_id ' || l_txn_cost_group_id ||
9873                         ': txfr_txn_cost_group_id ' || l_txfr_txn_cost_group_id);
9874   END IF;
9875 
9876 
9877   /* If the transaction org cost group = the transfer transaction org cost group,
9878      the transfer is within the same cost group */
9879   IF (l_txn_cost_group_id = l_txfr_txn_cost_group_id) THEN /* Same Cost Group Transfer */
9880 
9881       l_stmt_num := 100;
9882 
9883       IF g_debug_flag = 'Y' THEN
9884         fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Transfer Within Same Cost Group');
9885       END IF;
9886 
9887         -- Case I Dis-Dis, same CG, treat as SubInv transfer
9888         -- Internal Order Issue 34-1-8 :
9889         --   Dr. Expense @ PWAC
9890         --    Cr. Inv Val @ PWAC
9891         -- Logical Exp 27-17-7 :
9892         --   NO accounting
9893         IF (i_ae_txn_rec.txn_action_id = 1) then
9894           /* If the internal order issue is from expense sub then no accounting for Int Order Issue and Corresponding Logical Expense Requisition Receipt in case of Disc to Disc */
9895  	   IF(l_exp_flag) THEN
9896  	     IF g_debug_flag = 'Y' THEN
9897  	        fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': No Accounting for Internal Order Issue from Expense Sub ');
9898  	     END IF;
9899  	     RETURN;
9900  	   END IF;
9901 
9902             l_stmt_num := 110;
9903 
9904             IF g_debug_flag = 'Y' THEN
9905                 fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': 34-1-8 Dr Expense and Cr Inv');
9906             END IF;
9907 
9908             l_dr_flag := TRUE;
9909 
9910 
9911             /* Debit Expense account of the Logical Exp Req receipt at PWAC */
9912 
9913             SELECT NVL(CPIC.item_cost,0)
9914             INTO   l_pwac_cost
9915             FROM   CST_PAC_ITEM_COSTS CPIC
9916             WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
9917             AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
9918             AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
9919 
9920             /* Use the Logical trxn (27-17-7) to get the Expense acct
9921                of Receiving CG stamped in MMT in distribution_account_id.
9922                This cannot be NULL as it is stamped by INV Manager and not
9923                during Cost Processing in CSTPAVCP.Cost_LogicalSOReceipt */
9924             SELECT distribution_account_id
9925             INTO   l_mmt_exp_account
9926             FROM   mtl_material_transactions mmt
9927             WHERE  mmt.transaction_id = i_ae_txn_rec.xfer_transaction_id;
9928 
9929             l_stmt_num := 120;
9930 
9931             l_dr_flag := TRUE;
9932 
9933             l_ae_line_rec.transaction_value := l_pwac_cost * abs(i_ae_txn_rec.primary_quantity);
9934             l_ae_line_rec.account := l_mmt_exp_account;
9935             l_ae_line_rec.resource_id := NULL;
9936             l_ae_line_rec.cost_element_id := NULL;
9937             l_ae_line_rec.ae_line_type := 2;  -- Expense
9938 
9939             l_stmt_num := 130;
9940 
9941             insert_account (i_ae_txn_rec,
9942                             i_ae_curr_rec,
9943                             l_dr_flag,
9944                             l_ae_line_rec,
9945                             l_ae_line_tbl,
9946                             l_err_rec);
9947 
9948             l_stmt_num := 140;
9949 
9950             -- check error
9951             IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
9952               RAISE process_error;
9953             END IF;
9954 
9955             l_dr_flag := FALSE;
9956 
9957             -- Credit On-hand (if expense, then use expense account)
9958             -- -----------------------------------------------------
9959             IF (l_exp_flag) THEN
9960               CSTPAPBR.inventory_accounts (i_ae_txn_rec,
9961                                            i_ae_curr_rec,
9962                                            l_exp_flag, -- Exp Flag
9963                                            l_mfa_exp_account, -- Exp Acct
9964                                            l_dr_flag,
9965                                            l_ae_line_tbl,
9966                                            l_err_rec);
9967 
9968               l_stmt_num := 150;
9969             ELSE
9970               CSTPAPBR.inventory_accounts (i_ae_txn_rec,
9971                                            i_ae_curr_rec,
9972                                            "FALSE", -- Exp Flag
9973                                            null, -- Exp Acct
9974                                            l_dr_flag,
9975                                            l_ae_line_tbl,
9976                                            l_err_rec);
9977 
9978               l_stmt_num := 160;
9979             END IF;
9980 
9981             -- Check error
9982             if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
9983                raise process_error;
9984             end if;
9985 
9986         ELSIF (i_ae_txn_rec.txn_action_id = 17) then
9987           l_stmt_num := 170;
9988           /* If the internal order issue is from expense sub then no accounting for Int Order Issue and Corresponding Logical Expense Requisition Receipt in case of Disc to Disc */
9989           IF(l_exp_flag) THEN
9990             IF g_debug_flag = 'Y' THEN
9991               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': No Accounting for Logical Int Req Receipt for Internal Order Issue from Expense Sub ');
9992             END IF;
9993             RETURN;
9994           END IF;
9995 
9996             IF g_debug_flag = 'Y' THEN
9997               fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': 27-17-7 No accounting');
9998             END IF;
9999 
10000         END IF;
10001 
10002   ELSE /* Transfer across CG */
10003 
10004       l_stmt_num := 180;
10005 
10006       IF g_debug_flag = 'Y' THEN
10007         fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Transfer across Cost Group');
10008       END IF;
10009 
10010       /* Check if it is a transfer between Process and Discrete Orgs */
10011       SELECT MOD(SUM(DECODE(process_enabled_flag,'Y',1,2)), 2)
10012       INTO l_pd_txfr_ind
10013       FROM MTL_PARAMETERS MP
10014       WHERE MP.ORGANIZATION_ID = i_ae_txn_rec.xfer_organization_id
10015       OR MP.ORGANIZATION_ID    = i_ae_txn_rec.organization_id;
10016 
10017       l_stmt_num := 190;
10018 
10019       IF (l_pd_txfr_ind = 0) THEN /* Discrete-Discrete X-fer */
10020 
10021           l_stmt_num := 200;
10022 
10023           IF g_debug_flag = 'Y' THEN
10024             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Discrete-Discrete X-fer');
10025           END IF;
10026 
10027           IF (i_ae_txn_rec.txn_action_id = 1) THEN
10028           /* If the internal order issue is from expense sub then no accounting for Int Order Issue and Corresponding Logical Expense Requisition Receipt in case of Disc to Disc */
10029             IF(l_exp_flag) THEN
10030                 IF g_debug_flag = 'Y' THEN
10031                   fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': No Accounting for Logical Int Req Receipt for Internal Order Issue from Expense Sub ');
10032                 END IF;
10033               RETURN;
10034             END IF;
10035 
10036           -- Case II Dis-Dis, across CG. Sending CG
10037           -- Internal Order Issue 34-1-8 :
10038           --   Dr. Receivables Sending Org cost (from CPIC/Prior Prd PWAC/PerpMTA)
10039           --    Cr. Inv Val @ PWAC
10040           --    Cr. InterOrg Profit @ PWAC-(Prior Prd/PerpMTA)
10041 
10042               l_dr_flag := FALSE;
10043 
10044               /* Credit OnHand at PWAC Cost */
10045 
10046               -- Credit On-hand (if expense, then use expense account)
10047               -- -----------------------------------------------------
10048               IF (l_exp_flag) THEN
10049                 CSTPAPBR.inventory_accounts (i_ae_txn_rec,
10050                                              i_ae_curr_rec,
10051                                              l_exp_flag, -- Exp Flag
10052                                              l_mfa_exp_account, -- Exp Acct
10053                                              l_dr_flag,
10054                                              l_ae_line_tbl,
10055                                              l_err_rec);
10056 
10057                 l_stmt_num := 210;
10058               ELSE
10059                 CSTPAPBR.inventory_accounts (i_ae_txn_rec,
10060                                              i_ae_curr_rec,
10061                                              "FALSE", -- Exp Flag
10062                                              null, -- Exp Acct
10063                                              l_dr_flag,
10064                                              l_ae_line_tbl,
10065                                              l_err_rec);
10066 
10067                 l_stmt_num := 220;
10068               END IF;
10069 
10070                -- Check error
10071               if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
10072                  raise process_error;
10073               end if;
10074 
10075 
10076               /* Debit Receivables at estimated cost: This can be CPIC cost/previous period cost/MTA cost */
10077 
10078               /* Get relevant accounts */
10079               SELECT mip.interorg_receivables_account,
10080                      mip.interorg_transfer_cr_account,
10081                      nvl(mip.interorg_profit_account, -1)
10082               INTO   l_io_rcv_acct,
10083                      l_io_txfr_cr_acct,
10084                      l_io_txfr_var_acct
10085               FROM   mtl_interorg_parameters mip,
10086                      mtl_material_transactions mmt
10087               WHERE  mip.from_organization_id = i_ae_txn_rec.organization_id
10088               AND    mip.to_organization_id = i_ae_txn_rec.xfer_organization_id
10089               AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
10090 
10091               l_stmt_num := 230;
10092 
10093               get_pacp_priorPrd_mta_cost (i_ae_txn_rec            => i_ae_txn_rec,
10094                                           i_ae_curr_rec           => i_ae_curr_rec,
10095                                           l_ae_line_tbl           => l_ae_line_tbl,
10096                                           o_ae_err_rec            => l_err_rec,
10097                                           o_pacp_flag             => l_pacp_flag,
10098                                           o_pacp_pwac_cost        => l_pacp_pwac_cost,
10099                                           o_prev_period_flag      => l_prev_period_flag,
10100                                           o_prev_period_pwac_cost => l_prev_period_pwac_cost,
10101                                           o_perp_ship_flag        => l_perp_ship_flag,
10102                                           o_perp_ship_value       => l_perp_ship_value,
10103                                           o_txfr_credit           => l_txfr_credit);
10104 
10105               l_stmt_num := 240;
10106                -- Check error
10107               if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
10108                  raise process_error;
10109               end if;
10110 
10111               IF (l_pacp_flag = 1) THEN
10112                   l_stmt_num := 250;
10113                   IF g_debug_flag = 'Y' THEN
10114                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10115                                                ': Using PAC Absorption Cost: ');
10116                   END IF;
10117 
10118                   l_io_rcv_value := l_pacp_pwac_cost;
10119 
10120               ELSIF (l_prev_period_flag = 1) THEN
10121                   l_stmt_num := 260;
10122                   IF g_debug_flag = 'Y' THEN
10123                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10124                                                                    ': Using prior period PWAC cost: ');
10125                   END IF;
10126 
10127                   l_io_rcv_value := l_prev_period_pwac_cost;
10128 
10129               ELSIF (l_perp_ship_flag = 1) THEN
10130                   l_stmt_num := 270;
10131                   IF g_debug_flag = 'Y' THEN
10132                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10133                                                ': Using MTA cost');
10134                   END IF;
10135 
10136                   l_io_rcv_value := (l_perp_ship_value / abs(i_ae_txn_rec.primary_quantity));
10137 
10138               ELSE /* This is error situation, one of 3 cases above should be true */
10139 
10140                   l_stmt_num := 280;
10141 
10142                   IF g_debug_flag = 'Y' THEN
10143                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': PACP/PriorPrd/MTA value not returned');
10144                   END IF;
10145 
10146                   raise process_error;
10147               END IF; /* (l_pacp_flag = 1) */
10148 
10149               l_stmt_num := 290;
10150 
10151               l_dr_flag := TRUE;
10152 
10153               IF g_debug_flag = 'Y' THEN
10154                 fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10155                                            ': l_io_rcv_value = ' || l_io_rcv_value);
10156               END IF;
10157 
10158               l_ae_line_rec.account := l_io_rcv_acct;
10159               l_ae_line_rec.transaction_value := l_io_rcv_value * abs(i_ae_txn_rec.primary_quantity);
10160               l_ae_line_rec.resource_id := NULL;
10161               l_ae_line_rec.cost_element_id := NULL;
10162               l_ae_line_rec.ae_line_type := 10;  -- Receivables
10163 
10164               l_stmt_num := 300;
10165               insert_account (i_ae_txn_rec,
10166                               i_ae_curr_rec,
10167                               l_dr_flag,
10168                               l_ae_line_rec,
10169                               l_ae_line_tbl,
10170                               l_err_rec);
10171 
10172               l_stmt_num := 310;
10173               -- check error
10174               IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10175                   RAISE process_error;
10176               END IF;
10177 
10178               IF (l_pacp_flag <> 1) THEN /* In this case Inter-Org profit has to be hit */
10179 
10180                   l_stmt_num := 320;
10181                   SELECT sum(nvl(actual_cost,0))
10182                   INTO   l_mpacd_cost
10183                   FROM   mtl_pac_actual_cost_details
10184                   WHERE  transaction_id = i_ae_txn_rec.transaction_id
10185                   AND    pac_period_id = i_ae_txn_rec.accounting_period_id
10186                   AND    cost_group_id = i_ae_txn_rec.cost_group_id;
10187 
10188                   l_stmt_num := 330;
10189 
10190                   l_txfr_var_value := l_io_rcv_value - l_mpacd_cost;
10191 
10192                   IF (sign(l_txfr_var_value) = 1) THEN
10193                     l_dr_flag := FALSE;
10194                   ELSIF (sign(l_txfr_var_value) = -1) THEN
10195                     l_dr_flag := TRUE;
10196                   ELSE
10197                     l_dr_flag := NULL;
10198                   END IF;
10199 
10200                   l_txfr_var_value := abs(l_txfr_var_value);
10201 
10202                   IF (l_dr_flag IS NOT NULL) THEN
10203 
10204                      l_stmt_num := 340;
10205 
10206                      IF (l_io_txfr_var_acct = -1) THEN
10207                         RAISE no_interorg_profit_acct_error;
10208                      END IF;
10209 
10210                      /* Transfer variance entry */
10211                      l_ae_line_rec.account := l_io_txfr_var_acct;
10212                      l_ae_line_rec.transaction_value := l_txfr_var_value * abs(i_ae_txn_rec.primary_quantity);
10213                      l_ae_line_rec.resource_id := NULL;
10214                      l_ae_line_rec.cost_element_id := NULL;
10215                      l_ae_line_rec.ae_line_type := 34;   -- Interorg profit in inventory
10216 
10217                      l_stmt_num := 350;
10218 
10219                      insert_account (i_ae_txn_rec,
10220                                      i_ae_curr_rec,
10221                                      l_dr_flag,
10222                                      l_ae_line_rec,
10223                                      l_ae_line_tbl,
10224                                      l_err_rec);
10225 
10226                      l_stmt_num := 360;
10227                      -- check error
10228                      IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10229                         RAISE process_error;
10230                      END IF;
10231 
10232                   END IF; /* (l_dr_flag IS NOT NULL) */
10233 
10234               END IF; /* (l_pacp_flag <> 1) */
10235 
10236           ELSIF (i_ae_txn_rec.txn_action_id = 17) THEN
10237             /* If the internal order issue is from expense sub then no accounting for Int Order Issue and Corresponding Logical Expense Requisition Receipt in case of Disc to Disc */
10238             IF(l_exp_flag) THEN
10239                 IF g_debug_flag = 'Y' THEN
10240                   fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': No Accounting for Logical Int Req Receipt for Internal Order Issue from Expense Sub ');
10241                 END IF;
10242               RETURN;
10243             END IF;
10244 
10245           -- Case III Dis-Dis, across CG. Receiving CG
10246           -- Logical Exp 27-17-7 :
10247           --   Dr. Expense @ Sending Org cost (from CPIC/Prior Prd PWAC/PerpMTA)
10248           --    Cr. Payables @ Sending Org cost (from CPIC/Prior Prd PWAC/PerpMTA)
10249 
10250               l_stmt_num := 370;
10251               IF g_debug_flag = 'Y' THEN
10252                  fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': 27-17-7 Dr Exp and Cr Paybls');
10253               END IF;
10254 
10255               SELECT mip.interorg_payables_account,
10256                      distribution_account_id
10257               INTO   l_io_pay_acct,
10258                      l_mmt_exp_account
10259               FROM   mtl_interorg_parameters mip,
10260                      mtl_material_transactions mmt
10261               WHERE  mip.from_organization_id = i_ae_txn_rec.xfer_organization_id
10262               AND    mip.to_organization_id = i_ae_txn_rec.organization_id
10263               AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
10264 
10265               l_stmt_num := 380;
10266 
10267               get_pacp_priorPrd_mta_cost (i_ae_txn_rec            => i_ae_txn_rec,
10268                                           i_ae_curr_rec           => i_ae_curr_rec,
10269                                           l_ae_line_tbl           => l_ae_line_tbl,
10270                                           o_ae_err_rec            => l_err_rec,
10271                                           o_pacp_flag             => l_pacp_flag,
10272                                           o_pacp_pwac_cost        => l_pacp_pwac_cost,
10273                                           o_prev_period_flag      => l_prev_period_flag,
10274                                           o_prev_period_pwac_cost => l_prev_period_pwac_cost,
10275                                           o_perp_ship_flag        => l_perp_ship_flag,
10276                                           o_perp_ship_value       => l_perp_ship_value,
10277                                           o_txfr_credit           => l_txfr_credit);
10278 
10279               l_stmt_num := 390;
10280               -- Check error
10281               if (l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
10282                  raise process_error;
10283               end if;
10284 
10285               IF (l_pacp_flag = 1) THEN
10286                   l_stmt_num := 400;
10287                   IF g_debug_flag = 'Y' THEN
10288                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10289                                                ': Using PAC Absorption Cost: ');
10290                   END IF;
10291 
10292                   l_io_pay_value := l_pacp_pwac_cost;
10293 
10294               ELSIF (l_prev_period_flag = 1) THEN
10295                   l_stmt_num := 410;
10296                   IF g_debug_flag = 'Y' THEN
10297                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10298                                                                    ': Using prior period PWAC cost: ');
10299                   END IF;
10300 
10301                   l_io_pay_value := l_prev_period_pwac_cost;
10302 
10303               ELSIF (l_perp_ship_flag = 1) THEN
10304                   l_stmt_num := 420;
10305                   IF g_debug_flag = 'Y' THEN
10306                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10307                                                ': Using MTA cost');
10308                   END IF;
10309 
10310                   l_io_pay_value := (l_perp_ship_value / abs(i_ae_txn_rec.primary_quantity));
10311 
10312               ELSE /* This is error situation, one of 3 cases above should be true */
10313 
10314                   l_stmt_num := 430;
10315 
10316                   IF g_debug_flag = 'Y' THEN
10317                     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': PACP/PriorPrd/MTA value not returned');
10318                   END IF;
10319 
10320                   RAISE process_error;
10321 
10322               END IF; /* (l_pacp_flag = 1) */
10323 
10324               l_stmt_num := 440;
10325 
10326               l_dr_flag := FALSE;
10327 
10328               IF g_debug_flag = 'Y' THEN
10329                 fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10330                                            ': l_io_pay_value = ' || l_io_pay_value);
10331               END IF;
10332 
10333               l_ae_line_rec.account := l_io_pay_acct;
10334               l_ae_line_rec.transaction_value := (l_io_pay_value * abs(i_ae_txn_rec.primary_quantity));
10335               l_ae_line_rec.resource_id := NULL;
10336               l_ae_line_rec.cost_element_id := NULL;
10337               l_ae_line_rec.ae_line_type := 9;   -- Payables
10338 
10339               l_stmt_num := 450;
10340 
10341               -- Credit Payables
10342               -- ---------------
10343               insert_account (i_ae_txn_rec,
10344                               i_ae_curr_rec,
10345                               l_dr_flag,
10346                               l_ae_line_rec,
10347                               l_ae_line_tbl,
10348                               l_err_rec);
10349 
10350               l_stmt_num := 460;
10351               -- check error
10352               IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10353                  RAISE process_error;
10354               END IF;
10355 
10356               l_dr_flag := TRUE;
10357 
10358               l_stmt_num := 470;
10359 
10360               l_ae_line_rec.transaction_value := l_io_pay_value * abs(i_ae_txn_rec.primary_quantity);
10361               l_ae_line_rec.account := l_mmt_exp_account;
10362               l_ae_line_rec.resource_id := NULL;
10363               l_ae_line_rec.cost_element_id := NULL;
10364               l_ae_line_rec.ae_line_type := 2;  -- Expense
10365 
10366               l_stmt_num := 480;
10367 
10368               /* Debit Expense */
10369               insert_account (i_ae_txn_rec,
10370                               i_ae_curr_rec,
10371                               l_dr_flag,
10372                               l_ae_line_rec,
10373                               l_ae_line_tbl,
10374                               l_err_rec);
10375 
10376               l_stmt_num := 490;
10377 
10378               -- check error
10379               IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10380                  RAISE process_error;
10381               END IF;
10382 
10383           END IF; /* (i_ae_txn_rec.txn_action_id = 1) */
10384 
10385       ELSE /* OPM-Discrete X-fers */
10386 
10387           l_stmt_num := 500;
10388 
10389           IF g_debug_flag = 'Y' THEN
10390             fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Process-Discrete X-fer');
10391           END IF;
10392 
10393           IF (i_ae_txn_rec.txn_action_id = 1) then
10394           -- Case IV Dis-OPM Xfer, Discrete is Sending CG
10395           -- Internal Order Issue 34-1-8 :
10396           --   Dr. Receivables @ Transfer Price
10397           --    Cr. Inv Val @ PWAC
10398           --    Cr. Inter-org Profit @ TP-PWAC
10399 
10400               l_stmt_num := 510;
10401 
10402               IF g_debug_flag = 'Y' THEN
10403                   fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': 34-1-8 Dr Rcv and Cr Inv');
10404               END IF;
10405 
10406               /* Debit Receivables @ Transfer Price */
10407 
10408               SELECT mip.interorg_receivables_account,
10409                      nvl(mip.interorg_profit_account, -1),
10410                      nvl(mmt.transfer_price,0)
10411               INTO   l_io_rcv_acct,
10412                      l_io_txfr_var_acct,
10413                      l_transfer_price -- transfer price
10414               FROM   mtl_interorg_parameters mip,mtl_material_transactions mmt
10415               WHERE  mip.from_organization_id = i_ae_txn_rec.organization_id
10416               AND    mip.to_organization_id = i_ae_txn_rec.xfer_organization_id
10417               AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
10418 
10419               l_stmt_num := 520;
10420 
10421               IF (l_io_txfr_var_acct = -1) THEN
10422                  RAISE no_interorg_profit_acct_error;
10423               END IF;
10424 
10425               l_stmt_num := 530;
10426 
10427               SELECT NVL(CPIC.item_cost,0)
10428               INTO   l_pwac_cost
10429               FROM   CST_PAC_ITEM_COSTS CPIC
10430               WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
10431               AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
10432               AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
10433 
10434               l_dr_flag := TRUE;
10435 
10436               l_stmt_num := 540;
10437 
10438               l_ae_line_rec.transaction_value := l_transfer_price * abs(i_ae_txn_rec.primary_quantity); -- Receivables only at transfer price
10439               l_ae_line_rec.account := l_io_rcv_acct;
10440               l_ae_line_rec.resource_id := NULL;
10441               l_ae_line_rec.cost_element_id := NULL;
10442               l_ae_line_rec.ae_line_type := 10;  -- Receivables
10443 
10444               l_stmt_num := 550;
10445 
10446               insert_account (i_ae_txn_rec,
10447                               i_ae_curr_rec,
10448                               l_dr_flag,
10449                               l_ae_line_rec,
10450                               l_ae_line_tbl,
10451                               l_err_rec);
10452 
10453               l_stmt_num := 560;
10454 
10455               -- check error
10456               IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10457                  RAISE process_error;
10458               END IF;
10459 
10460 
10461               /* Credit On-hand @ PWAC (if shipping from expense sub, then use expense account) */
10462 
10463               l_dr_flag := FALSE;
10464 
10465               IF (l_exp_flag) THEN
10466                 CSTPAPBR.inventory_accounts (i_ae_txn_rec,
10467                                              i_ae_curr_rec,
10468                                              l_exp_flag, -- Exp Flag
10469                                              l_mfa_exp_account, -- Exp Acct
10470                                              l_dr_flag,
10471                                              l_ae_line_tbl,
10472                                              l_err_rec);
10473                 l_stmt_num := 570;
10474 
10475               ELSE
10476                 CSTPAPBR.inventory_accounts (i_ae_txn_rec,
10477                                              i_ae_curr_rec,
10478                                              "FALSE", -- Exp Flag
10479                                              null, -- Exp Acct
10480                                              l_dr_flag,
10481                                              l_ae_line_tbl,
10482                                              l_err_rec);
10483                 l_stmt_num := 580;
10484 
10485               END IF;
10486 
10487               -- check error
10488               IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10489                  RAISE process_error;
10490               END IF;
10491 
10492 
10493               l_profit_or_loss := l_transfer_price - l_pwac_cost;
10494 
10495               IF (l_profit_or_loss <> 0) THEN
10496 
10497                   IF l_profit_or_loss < 0 THEN
10498                       l_dr_flag := not l_dr_flag; -- If -ve then Debit interorg profit
10499                   END IF;
10500 
10501                   l_stmt_num := 590;
10502 
10503                   l_ae_line_rec.transaction_value := abs(l_profit_or_loss * i_ae_txn_rec.primary_quantity);
10504                   l_ae_line_rec.account := l_io_txfr_var_acct;
10505                   l_ae_line_rec.resource_id := NULL;
10506                   l_ae_line_rec.cost_element_id := NULL;
10507                   l_ae_line_rec.ae_line_type := 34;   -- interorg profit account
10508 
10509                   l_stmt_num := 600;
10510 
10511                   insert_account (i_ae_txn_rec,
10512                                   i_ae_curr_rec,
10513                                   l_dr_flag,
10514                                   l_ae_line_rec,
10515                                   l_ae_line_tbl,
10516                                   l_err_rec);
10517 
10518                   l_stmt_num := 610;
10519 
10520                   -- check error
10521                   IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10522                       RAISE process_error;
10523                   END IF;
10524 
10525               END IF; /* l_profit_or_loss <> 0 */
10526 
10527           ELSIF (i_ae_txn_rec.txn_action_id = 17) then
10528           -- Case V OPM-Dis Xfer, Discrete is Receiving CG
10529           -- Logical Exp Requisition Receipt 27-17-7 :
10530           --   Dr. Expense @ Transfer price
10531           --    Cr. Payables @ Transfer price
10532 
10533               l_stmt_num := 620;
10534 
10535               IF g_debug_flag = 'Y' THEN
10536                  fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': 27-17-7 Dr Exp and Cr Paybls');
10537               END IF;
10538 
10539               SELECT mip.interorg_payables_account,
10540                      distribution_account_id,
10541                      nvl(transfer_price,0)
10542               INTO   l_io_pay_acct,
10543                      l_mmt_exp_account,
10544                      l_transfer_price -- transfer price
10545               FROM   mtl_interorg_parameters mip,
10546                      mtl_material_transactions mmt
10547               WHERE  mip.from_organization_id = i_ae_txn_rec.xfer_organization_id
10548               AND    mip.to_organization_id = i_ae_txn_rec.organization_id
10549               AND    mmt.transaction_id = i_ae_txn_rec.transaction_id;
10550 
10551               l_stmt_num := 630;
10552 
10553               l_dr_flag := TRUE;
10554 
10555               l_ae_line_rec.transaction_value := l_transfer_price * abs(i_ae_txn_rec.primary_quantity);
10556               l_ae_line_rec.account := l_mmt_exp_account;
10557               l_ae_line_rec.resource_id := NULL;
10558               l_ae_line_rec.cost_element_id := NULL;
10559               l_ae_line_rec.ae_line_type := 2;  -- Expense
10560 
10561               l_stmt_num := 640;
10562 
10563               insert_account (i_ae_txn_rec,
10564                               i_ae_curr_rec,
10565                               l_dr_flag,
10566                               l_ae_line_rec,
10567                               l_ae_line_tbl,
10568                               l_err_rec);
10569 
10570               l_stmt_num := 650;
10571 
10572               -- check error
10573               IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10574                  RAISE process_error;
10575               END IF;
10576 
10577               l_dr_flag := FALSE;
10578 
10579               l_ae_line_rec.transaction_value := l_transfer_price * abs(i_ae_txn_rec.primary_quantity);
10580               l_ae_line_rec.account := l_io_pay_acct;
10581               l_ae_line_rec.resource_id := NULL;
10582               l_ae_line_rec.cost_element_id := NULL;
10583               l_ae_line_rec.ae_line_type := 9;  -- Payables
10584 
10585               l_stmt_num := 660;
10586 
10587               insert_account (i_ae_txn_rec,
10588                               i_ae_curr_rec,
10589                               l_dr_flag,
10590                               l_ae_line_rec,
10591                               l_ae_line_tbl,
10592                               l_err_rec);
10593 
10594               l_stmt_num := 670;
10595 
10596               -- check error
10597               IF (l_err_rec.l_err_num <> 0 AND l_err_rec.l_err_num IS NOT NULL) THEN
10598                  RAISE process_error;
10599               END IF;
10600 
10601           END IF; /* i_ae_txn_rec.txn_action_id = 1 */
10602 
10603       END IF; /* l_pd_txfr_ind = 0 */
10604 
10605   END IF; /* (l_txn_cost_group_id = l_txfr_txn_cost_group_id) */
10606 
10607   l_stmt_num := 680;
10608 
10609   IF g_debug_flag = 'Y' THEN
10610      fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': return >>');
10611   END IF;
10612 
10613   EXCEPTION
10614 
10615     when no_mfca_acct_error then -- INVCONV
10616     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Error processing transaction ' || i_ae_txn_rec.transaction_id);
10617     o_ae_err_rec.l_err_num := 30005;
10618     o_ae_err_rec.l_err_code := 'CST_PAC_NO_MFCA_ACCTS';
10619     FND_MESSAGE.set_name('BOM', 'CST_PAC_NO_MFCA_ACCTS');
10620     o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
10621 
10622     when no_interorg_profit_acct_error then
10623     fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': Error processing transaction ' || i_ae_txn_rec.transaction_id);
10624     o_ae_err_rec.l_err_num := 30005;
10625     o_ae_err_rec.l_err_code := 'CST_NO_INTERORG_PROFIT_ACCT';
10626     FND_MESSAGE.set_name('BOM', 'CST_NO_INTERORG_PROFIT_ACCT');
10627     o_ae_err_rec.l_err_msg := FND_MESSAGE.Get;
10628 
10629     when process_error then
10630     o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
10631     o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
10632     o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
10633 
10634     when others then
10635     o_ae_err_rec.l_err_num := SQLCODE;
10636     o_ae_err_rec.l_err_code := '';
10637     o_ae_err_rec.l_err_msg := l_api_name || ':' || to_char(l_stmt_num) ||
10638     substr(SQLERRM,1,180);
10639 
10640 END cost_internal_order_exp_txn;
10641 
10642 
10643 
10644 /* --======================================================================
10645 -- In case of a transfer across cost groups, the receivables needs to be
10646 -- valued at the cost at Shipping Org. Due to limitations in PAC, we get an
10647 -- estimate as follows:
10648 --
10649 -- If both CG as under same LE then
10650 --  |-- If PACP (Iterative PAC)is enabled, get the CPIC cost
10651 --  |-- Else get the Prior Period PWAC cost
10652 -- Else the cost at perpetual side (MTA transaction value)
10653 --
10654 -- For Internal Order to Non-Inventory (Expense) Destination we also call
10655 -- this procedure to get the value for Expense and Payables while creating
10656 -- distributions for Logical Expense Destination Receipt trxn.
10657 -- ======================================================================*/
10658 
10659 Procedure get_pacp_priorPrd_mta_cost (
10660   i_ae_txn_rec       IN             CSTPALTY.cst_ae_txn_rec_type,
10661   i_ae_curr_rec      IN             CSTPALTY.cst_ae_curr_rec_type,
10662   l_ae_line_tbl      IN OUT NOCOPY  CSTPALTY.cst_ae_line_tbl_type,
10663   o_ae_err_rec       OUT NOCOPY     CSTPALTY.cst_ae_err_rec_type,
10664   o_pacp_flag              OUT NOCOPY NUMBER,
10665   o_pacp_pwac_cost         OUT NOCOPY NUMBER,
10666   o_prev_period_flag       OUT NOCOPY NUMBER,
10667   o_prev_period_pwac_cost  OUT NOCOPY NUMBER,
10668   o_perp_ship_flag         OUT NOCOPY NUMBER,
10669   o_perp_ship_value        OUT NOCOPY NUMBER,
10670   o_txfr_credit            OUT NOCOPY NUMBER
10671 )
10672 IS
10673   l_stmt_num              NUMBER := 0;
10674   l_txfr_legal_entity     NUMBER;
10675   l_txfr_cost_group_id    NUMBER;
10676   l_same_le_ct            NUMBER;
10677   l_prev_period_id        NUMBER;
10678   l_txfr_percent          NUMBER;
10679   l_txfr_cost             NUMBER;
10680   l_use_prev_period_cost  NUMBER;
10681   l_transfer_cost_flag    VARCHAR2(1);
10682 
10683   l_err_rec               CSTPALTY.cst_ae_err_rec_type;
10684 
10685   l_api_name CONSTANT VARCHAR2(240) := 'CSTPAPBR.get_pacp_priorPrd_mta_cost';
10686 
10687 BEGIN
10688 
10689   IF g_debug_flag = 'Y' THEN
10690     fnd_file.put_line(fnd_file.log, l_api_name || ': ' || l_stmt_num || ': begin <<<'
10691                       || ' transaction_id: ' || i_ae_txn_rec.transaction_id);
10692   END IF;
10693 
10694   -- Initialize variables.
10695   -- ---------------------
10696   l_err_rec.l_err_num := 0;
10697   l_err_rec.l_err_code := '';
10698   l_err_rec.l_err_msg := '';
10699 
10700 
10701   /* Initialize the variables */
10702   o_pacp_flag             := 0;
10703   o_pacp_pwac_cost        := 0;
10704   o_prev_period_flag      := 0;
10705   o_prev_period_pwac_cost := 0;
10706   o_perp_ship_flag        := 0;
10707   o_perp_ship_value       := 0;
10708   o_txfr_credit           := 0;
10709 
10710   l_stmt_num := 100;
10711 
10712   /* In case of Logical Expense Req Receipt, for Across CG, Same LE/CT transfer,
10713      we need to pick the prior period cost of the sending org. */
10714 
10715   /* Get transfer credit information */
10716   SELECT nvl(transfer_percentage,0),nvl(transfer_cost,0)
10717   INTO   l_txfr_percent,l_txfr_cost
10718   FROM   mtl_material_transactions
10719   WHERE  transaction_id = i_ae_txn_rec.transaction_id;
10720 
10721   l_stmt_num := 110;
10722 
10723   /* Get prior period id */
10724   SELECT nvl(max(cpp.pac_period_id), -1)
10725   INTO  l_prev_period_id
10726   FROM  cst_pac_periods cpp
10727   WHERE cpp.cost_type_id = i_ae_txn_rec.cost_type_id
10728   AND   cpp.legal_entity = i_ae_txn_rec.legal_entity_id
10729   AND   cpp.pac_period_id < i_ae_txn_rec.accounting_period_id;
10730 
10731   l_stmt_num := 120;
10732 
10733   /* The flag selected below indicates if PACP is used or not */
10734   SELECT TRANSFER_COST_FLAG
10735   INTO   l_transfer_cost_flag
10736   FROM   CST_LE_COST_TYPES
10737   WHERE  LEGAL_ENTITY = i_ae_txn_rec.legal_entity_id
10738   AND    COST_TYPE_ID = i_ae_txn_rec.cost_type_id;
10739 
10740   l_stmt_num := 130;
10741 
10742   SELECT NVL(MAX(cost_group_id),-1)
10743   INTO   l_txfr_cost_group_id
10744   FROM   cst_cost_group_assignments
10745   WHERE  organization_id = i_ae_txn_rec.xfer_organization_id;
10746 
10747   l_stmt_num := 140;
10748 
10749  /* Get legal entity of the other cost group,if available */
10750   BEGIN
10751       SELECT legal_entity
10752       INTO l_txfr_legal_entity
10753       FROM cst_cost_groups
10754       WHERE cost_group_id = l_txfr_cost_group_id;
10755   EXCEPTION
10756   WHEN NO_DATA_FOUND THEN
10757       l_txfr_legal_entity := NULL;
10758   END;
10759 
10760   /* See if i_cost_type_id is attached to the transfer LE as well */
10761   l_stmt_num := 150;
10762   SELECT count(*)
10763   INTO   l_same_le_ct
10764   FROM   cst_le_cost_types
10765   WHERE  legal_entity = l_txfr_legal_entity
10766   AND    cost_type_id = i_ae_txn_rec.cost_type_id;
10767 
10768   /* Check for the same LE/CT combination */
10769   IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity
10770       AND l_same_le_ct > 0
10771       AND l_transfer_cost_flag = 'Y') THEN /* Begin (Same LE PACP available) */
10772 
10773     /* PACP used: The estimated cost is available in CPICD */
10774 
10775 
10776     o_pacp_flag := 1;
10777     IF (i_ae_txn_rec.txn_action_id = 17) THEN /* Logical Exp Req Rcpt, Take sending Cost Group Cost */
10778      l_stmt_num := 160;
10779        SELECT NVL(CPIC.item_cost,0)
10780          INTO   o_pacp_pwac_cost
10781         FROM   CST_PAC_ITEM_COSTS CPIC
10782          WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
10783            AND  CPIC.COST_GROUP_ID     = l_txfr_cost_group_id
10784            AND  CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
10785      ELSE
10786        l_stmt_num := 165;
10787          SELECT NVL(CPIC.item_cost,0)
10788             INTO   o_pacp_pwac_cost
10789          FROM   CST_PAC_ITEM_COSTS CPIC
10790         WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
10791           AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
10792           AND    CPIC.PAC_PERIOD_ID     = i_ae_txn_rec.accounting_period_id;
10793        END IF;
10794     IF g_debug_flag = 'Y' THEN
10795       fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10796                                  ': Using PAC Absorption Cost: ');
10797     END IF;
10798 
10799     IF (l_txfr_percent <> 0) THEN
10800        o_txfr_credit := (l_txfr_percent * o_pacp_pwac_cost / 100);
10801     ELSIF (l_txfr_cost <> 0) THEN
10802        o_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
10803     ELSE
10804        o_txfr_credit := 0;
10805     END IF;
10806 
10807  ELSE
10808 
10809     IF (i_ae_txn_rec.legal_entity_id = l_txfr_legal_entity
10810         AND l_same_le_ct > 0
10811         AND l_prev_period_id <> -1) THEN /* Begin (Same LE Prior Prd available) */
10812 
10813         l_stmt_num := 170;
10814         l_use_prev_period_cost := 1;
10815 
10816         /* Get prior period PWAC Cost */
10817         IF (i_ae_txn_rec.txn_action_id = 17) THEN /* Logical Exp Req Rcpt, Take sending Cost Group Cost */
10818             l_stmt_num := 180;
10819             BEGIN
10820               SELECT nvl(CPIC.item_cost,0)
10821               INTO   o_prev_period_pwac_cost
10822               FROM   CST_PAC_ITEM_COSTS CPIC
10823               WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
10824               AND    CPIC.COST_GROUP_ID     = l_txfr_cost_group_id
10825               AND    CPIC.PAC_PERIOD_ID     = l_prev_period_id;
10826             EXCEPTION
10827               WHEN NO_DATA_FOUND THEN
10828                 /* Use perpetual cost if prior period cost is not available */
10829                 l_use_prev_period_cost := 0;
10830             END;
10831         ELSE /* Get Prior Period PWAC cost */
10832             l_stmt_num := 190;
10833             BEGIN
10834               SELECT nvl(CPIC.item_cost,0)
10835               INTO   o_prev_period_pwac_cost
10836               FROM   CST_PAC_ITEM_COSTS CPIC
10837               WHERE  CPIC.INVENTORY_ITEM_ID = i_ae_txn_rec.inventory_item_id
10838               AND    CPIC.COST_GROUP_ID     = i_ae_txn_rec.cost_group_id
10839               AND    CPIC.PAC_PERIOD_ID     = l_prev_period_id;
10840             EXCEPTION
10841               WHEN NO_DATA_FOUND THEN
10842                 /* Use perpetual cost if prior period cost is not available */
10843                 l_use_prev_period_cost := 0;
10844             END;
10845         END IF;
10846 
10847     ELSE
10848 
10849         /* Use perpetual cost if prior period cost is not available
10850            or if the cost groups are not in the same LE/CT. */
10851         l_use_prev_period_cost := 0;
10852 
10853     END IF; /* End (Same LE Prior Prd available) */
10854 
10855     IF (l_use_prev_period_cost = 1) THEN
10856 
10857         o_prev_period_flag := 1;
10858 
10859         IF g_debug_flag = 'Y' THEN
10860           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10861                                                          ': Using prior period PWAC cost: ');
10862         END IF;
10863 
10864         l_stmt_num := 200;
10865 
10866         IF (l_txfr_percent <> 0) THEN
10867            o_txfr_credit := (l_txfr_percent * o_prev_period_pwac_cost / 100);
10868         ELSIF (l_txfr_cost <> 0) THEN
10869            o_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
10870         ELSE
10871            o_txfr_credit := 0;
10872         END IF;
10873 
10874     ELSE /* MTA Entries to be used */ /* or same LE/CT where there is no prior period cost */
10875         l_stmt_num := 210;
10876 
10877         o_perp_ship_flag := 1;
10878 
10879         IF g_debug_flag = 'Y' THEN
10880           fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num ||
10881                                      ': Using MTA cost');
10882         END IF;
10883 
10884         BEGIN
10885           IF (i_ae_txn_rec.txn_action_id = 17) THEN
10886 
10887               l_stmt_num := 220;
10888 
10889               SELECT nvl(SUM(ABS(NVL(base_transaction_value, 0))),0)
10890               INTO o_perp_ship_value
10891               FROM mtl_transaction_accounts mta
10892               WHERE mta.transaction_id = i_ae_txn_rec.transaction_id
10893               and mta.organization_id = i_ae_txn_rec.organization_id
10894               and mta.accounting_line_type IN (1,2,14)
10895               and mta.base_transaction_value > 0;
10896 
10897           ELSE
10898 
10899             l_stmt_num := 230;
10900 
10901             SELECT nvl(SUM(ABS(NVL(base_transaction_value, 0))),0)
10902             INTO o_perp_ship_value
10903             FROM mtl_transaction_accounts mta
10904             WHERE mta.transaction_id = i_ae_txn_rec.transaction_id
10905             and mta.organization_id = i_ae_txn_rec.organization_id
10906             and mta.accounting_line_type IN (1,2,14)
10907             and mta.base_transaction_value < 0;
10908 
10909           END IF;
10910 
10911         EXCEPTION
10912           WHEN no_data_found THEN
10913              l_stmt_num := 240;
10914              o_perp_ship_value := 0;
10915         END;
10916 
10917         l_stmt_num := 250;
10918 
10919         IF (l_txfr_percent <> 0) THEN
10920             o_txfr_credit := (l_txfr_percent * (o_perp_ship_value)/ (100 * abs(i_ae_txn_rec.primary_quantity)));
10921         elsif (l_txfr_cost <> 0) THEN
10922             o_txfr_credit := l_txfr_cost / abs(i_ae_txn_rec.primary_quantity);
10923         ELSE
10924             o_txfr_credit := 0;
10925         END IF;
10926 
10927     END IF; /* (l_use_prev_period_cost = 1) */
10928 
10929   END IF; /* End Same LE PACP available */
10930 
10931   l_stmt_num := 260;
10932   IF g_debug_flag = 'Y' THEN
10933      fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num
10934                               || ': o_pacp_flag :'             || o_pacp_flag
10935                               || ': o_pacp_pwac_cost :'        || o_pacp_pwac_cost
10936                               || ': o_prev_period_flag :'      || o_prev_period_flag
10937                               || ': o_prev_period_pwac_cost :' || o_prev_period_pwac_cost
10938                               || ': o_perp_ship_flag :'        || o_perp_ship_flag
10939                               || ': o_perp_ship_value :'       || o_perp_ship_value
10940                               || ': o_txfr_credit :'           || o_txfr_credit);
10941      fnd_file.put_line(fnd_file.log,l_api_name || ': ' || l_stmt_num || ': return >>>' );
10942   END IF;
10943 
10944 EXCEPTION
10945 
10946     when others then
10947     o_ae_err_rec.l_err_num := SQLCODE;
10948     o_ae_err_rec.l_err_code := '';
10949     o_ae_err_rec.l_err_msg := l_api_name || ' : ' || to_char(l_stmt_num) ||
10950     substr(SQLERRM,1,180);
10951 
10952 END get_pacp_priorPrd_mta_cost;
10953 
10954 
10955 PROCEDURE CompEncumbrance_IntOrdersExp (
10956             p_api_version     IN NUMBER,
10957     	    p_transaction_id  IN MTL_MATERIAL_TRANSACTIONS.TRANSACTION_ID%TYPE,
10958             p_req_line_id     IN PO_REQUISITION_LINES_ALL.REQUISITION_LINE_ID%TYPE,
10959             p_item_id         IN MTL_SYSTEM_ITEMS.INVENTORY_ITEM_ID%TYPE,
10960             p_organization_id IN MTL_PARAMETERS.ORGANIZATION_ID%TYPE,
10961             p_primary_qty     IN MTL_MATERIAL_TRANSACTIONS.PRIMARY_QUANTITY%TYPE,
10962             p_total_primary_qty   IN NUMBER,
10963             x_encumbrance_amount  OUT NOCOPY NUMBER,
10964             x_encumbrance_account OUT NOCOPY NUMBER,
10965             o_ae_err_rec          OUT NOCOPY CSTPALTY.cst_ae_err_rec_type
10966  ) IS
10967 
10968 l_doc_line_qty           PO_REQUISITION_LINES_ALL.QUANTITY%TYPE;
10969 l_doc_unit_price         PO_REQUISITION_LINES_ALL.UNIT_PRICE%TYPE;
10970 l_unit_price             PO_REQUISITION_LINES_ALL.UNIT_PRICE%TYPE;
10971 l_conversion_rate        NUMBER;
10972 l_primary_uom_code       MTL_UNITS_OF_MEASURE.UOM_CODE%TYPE;
10973 l_doc_uom_code           MTL_UNITS_OF_MEASURE.UOM_CODE%TYPE;
10974 l_uom_rate               NUMBER;
10975 l_doc_primary_qty        NUMBER;
10976 l_doc_rcv_qty            NUMBER;
10977 l_non_recoverable_tax    NUMBER;
10978 
10979 l_hook_used              NUMBER;
10980 l_loc_non_recoverable_tax    NUMBER;
10981 l_loc_recoverable_tax        NUMBER;
10982 l_Err_Num                NUMBER;
10983 l_Err_Code               NUMBER;
10984 process_error		EXCEPTION;
10985 
10986 l_stmt_num               NUMBER;
10987 l_api_name               VARCHAR2(100) := 'CompEncumbrance_IntOrdersExp';
10988 l_api_version            NUMBER := 1.0;
10989 
10990 l_err_rec                 CSTPALTY.cst_ae_err_rec_type;
10991 
10992 
10993 BEGIN
10994 -- Initialize local variables.
10995   l_err_rec.l_err_num := 0;
10996   l_err_rec.l_err_code := '';
10997   l_err_rec.l_err_msg := '';
10998 
10999   l_hook_used     :=0;
11000   l_stmt_num := 5;
11001 
11002   IF g_debug_flag= 'Y' THEN
11003     FND_FILE.PUT_LINE( FND_FILE.log, 'CompEncumbrance_IntOrdersExp <<< ');
11004     FND_FILE.PUT_LINE( FND_FILE.log, 'Req Line Identifier: '||p_req_line_id);
11005     FND_FILE.PUT_LINE( FND_FILE.log, 'Primary Quantity Encumbered before: '||p_total_primary_qty );
11006   END IF;
11007 
11008   /* 1. Get the budget_account, quantity ordered, and UOM from the Req */
11009   l_stmt_num := 10;
11010   SELECT um.uom_code,
11011          rl.quantity,
11012          rl.unit_price,
11013          rd.budget_account_id,
11014          nvl(rd.nonrecoverable_tax,0) /* Bug 6033153 */
11015   INTO   l_doc_uom_code,
11016          l_doc_line_qty,
11017          l_unit_price,
11018          x_encumbrance_account,
11019 	 l_non_recoverable_tax  /* Bug 6033153 */
11020   FROM   po_req_distributions_all rd,
11021          po_requisition_lines_all rl,
11022          mtl_units_of_measure um
11023   WHERE  rd.requisition_line_id   = p_req_line_id
11024   and    rd.requisition_line_id   = rl.requisition_line_id
11025   and    rl.UNIT_MEAS_LOOKUP_CODE = um.unit_of_measure;
11026 
11027   if g_debug_flag= 'Y' then
11028     fnd_file.put_line(fnd_file.log, 'Unit Price: '||l_unit_price||'Encumbrance Account: '||x_encumbrance_account);
11029   end if;
11030 
11031   /* Get UOM for this item/org from MSI */
11032   l_stmt_num := 30;
11033   SELECT primary_uom_code
11034   INTO   l_primary_uom_code
11035   FROM   mtl_system_items
11036   WHERE  organization_id   = p_organization_id
11037   AND    inventory_item_id = p_item_id;
11038 
11039   /* Convert the total_primary_quantity into source_doc_quantity */
11040   l_stmt_num := 40;
11041   INV_Convert.INV_UM_Conversion(
11042                           from_unit       => l_primary_uom_code,
11043                           to_unit         => l_doc_uom_code,
11044                           item_id         => p_item_id,
11045                           uom_rate        => l_uom_rate );
11046   IF ( l_uom_rate = -99999) THEN
11047     fnd_file.put_line(fnd_file.log,'Inv_Convert.inv_um_conversion() failed to get the UOM rate');
11048     RAISE process_error;
11049   END IF;
11050 
11051   if g_debug_flag= 'Y' then
11052     fnd_file.put_line(fnd_file.log, 'Primary UOM: '||l_primary_uom_code);
11053   end if;
11054   l_doc_rcv_qty     := p_total_primary_qty * l_uom_rate;
11055   l_doc_primary_qty := p_primary_qty * l_uom_rate;
11056 
11057   if g_debug_flag= 'Y' then
11058     fnd_file.put_line(fnd_file.log, 'Document Received Quantity: '||l_doc_rcv_qty||' Document Primary Quantity: '||l_doc_primary_qty);
11059   end if;
11060 
11061   /* The Requisition is always in the funtional currency */
11062   /* No need of currency conversion */
11063 
11064   IF ( l_doc_rcv_qty  >= l_doc_line_qty ) THEN
11065     x_encumbrance_amount := 0;
11066   ELSIF ( l_doc_rcv_qty + l_doc_primary_qty ) >= l_doc_line_qty THEN
11067     x_encumbrance_amount :=  l_unit_price * ( l_doc_line_qty - l_doc_rcv_qty )
11068                             +nvl(l_non_recoverable_tax,0)*( l_doc_line_qty - l_doc_rcv_qty )/l_doc_line_qty;
11069   ELSE
11070     x_encumbrance_amount :=  l_unit_price * l_doc_primary_qty
11071                             +nvl(l_non_recoverable_tax,0)*(l_doc_primary_qty/l_doc_line_qty);
11072   END IF;
11073 
11074 
11075   IF g_debug_flag= 'Y' THEN
11076     fnd_file.put_line(fnd_file.log, 'Encumbrance Amount: '||x_encumbrance_amount);
11077     FND_FILE.PUT_LINE( FND_FILE.log, 'CompEncumbrance_IntOrdersExp >>>');
11078   END IF;
11079 
11080 EXCEPTION
11081   when process_error then
11082   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
11083   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
11084   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
11085 
11086   when others then
11087   o_ae_err_rec.l_err_num := SQLCODE;
11088   o_ae_err_rec.l_err_code := '';
11089   o_ae_err_rec.l_err_msg := 'CompEncumbrance_IntOrdersExp' || to_char(l_stmt_num) ||
11090   substr(SQLERRM,1,180);
11091 
11092 END CompEncumbrance_IntOrdersExp;
11093 
11094 PROCEDURE CompEncumbrance_IntOrdersExp (
11095             p_api_version     IN NUMBER,
11096             p_transaction_id  IN MTL_MATERIAL_TRANSACTIONS.TRANSACTION_ID%TYPE,
11097             x_encumbrance_amount  OUT NOCOPY NUMBER,
11098             x_encumbrance_account OUT NOCOPY NUMBER,
11099             o_ae_err_rec          OUT NOCOPY CSTPALTY.cst_ae_err_rec_type
11100 ) IS
11101 
11102 l_total_primary_qty      NUMBER;
11103 l_primary_qty            MTL_MATERIAL_TRANSACTIONS.PRIMARY_QUANTITY%TYPE;
11104 l_organization_id        MTL_MATERIAL_TRANSACTIONS.ORGANIZATION_ID%TYPE;
11105 l_trx_source_line_id     MTL_MATERIAL_TRANSACTIONS.TRX_SOURCE_LINE_ID%TYPE;
11106 l_req_line_id            PO_REQUISITION_LINES_ALL.REQUISITION_LINE_ID%TYPE;
11107 l_item_id                MTL_MATERIAL_TRANSACTIONS.INVENTORY_ITEM_ID%TYPE;
11108 l_txn_action_id          MTL_MATERIAL_TRANSACTIONS.TRANSACTION_ACTION_ID%TYPE;
11109 l_txn_src_type_id MTL_MATERIAL_TRANSACTIONS.TRANSACTION_SOURCE_TYPE_ID%TYPE;
11110 l_rcv_txn_id             MTL_MATERIAL_TRANSACTIONS.RCV_TRANSACTION_ID%TYPE;
11111 l_txn_type_id            MTL_MATERIAL_TRANSACTIONS.TRANSACTION_TYPE_ID%TYPE;
11112 l_txn_date               MTL_MATERIAL_TRANSACTIONS.TRANSACTION_DATE%TYPE;
11113 
11114 l_stmt_num               NUMBER;
11115 l_api_name               VARCHAR2(100) := 'CompEncumbrance_IntOrdersExp';
11116 l_api_version            NUMBER := 1.0;
11117 l_err_rec                CSTPALTY.cst_ae_err_rec_type;
11118 process_error            EXCEPTION;
11119 
11120 BEGIN
11121 -- Initialize local variables.
11122   l_err_rec.l_err_num := 0;
11123   l_err_rec.l_err_code := '';
11124   l_err_rec.l_err_msg := '';
11125 
11126   l_stmt_num := 5;
11127   if g_debug_flag= 'Y' then
11128     fnd_file.put_line(fnd_file.log, 'CompEncumbrance_IntOrdersExp (T) <<');
11129   end if;
11130   l_stmt_num := 10;
11131 
11132   SELECT
11133     mmt.trx_source_line_id,
11134     mmt.primary_quantity,
11135     mmt.organization_id,
11136     mmt.inventory_item_id,
11137     mmt.transaction_action_id,
11138     mmt.transaction_source_type_id,
11139     mmt.transaction_type_id,
11140     mmt.rcv_transaction_id,
11141     mmt.transaction_date
11142   INTO
11143     l_trx_source_line_id,
11144     l_primary_qty,
11145     l_organization_id,
11146     l_item_id,
11147     l_txn_action_id,
11148     l_txn_src_type_id,
11149     l_txn_type_id,
11150     l_rcv_txn_id,
11151     l_txn_date
11152   FROM
11153     MTL_MATERIAL_TRANSACTIONS mmt
11154   WHERE
11155     transaction_id = p_transaction_id;
11156 
11157   --  Get total received quantity so far just before processing the current receipt transaction
11158   --  this is to ensure that encumbrance reversal accounting entries are generated for each receipt transaction as long as
11159   --  total receipt quantity of all the receipts is less than corresponding requisition quantity.
11160   l_stmt_num := 20;
11161   SELECT sum(primary_quantity)
11162   INTO   l_total_primary_qty
11163   from   mtl_material_transactions
11164   where  transaction_action_id      = l_txn_action_id
11165   and    transaction_source_type_id = l_txn_src_type_id
11166   and    transaction_type_id        = l_txn_type_id
11167   and    trx_source_line_id         = l_req_line_id
11168   and    organization_id            = l_organization_id
11169   and    ( transaction_date < l_txn_date or (transaction_date = l_txn_date and transaction_id < p_transaction_id));
11170 
11171   if g_debug_flag= 'Y' then
11172     fnd_file.put_line(fnd_file.log, 'Total Received Primary Qty just before current receipt transaction: '||l_total_primary_qty);
11173   end if;
11174 
11175 
11176   /* Get Requisition Line ID */
11177   /* For Internal Order Requisition Receipt, this is the MMT.TRX_SOURCE_LINE_ID
11178      Using above information, find the requisition_line_id */
11179   l_stmt_num := 25;
11180 
11181   IF ( l_txn_action_id       = 17
11182        AND l_txn_src_type_id = 7
11183        AND l_txn_type_id     = 27 ) THEN
11184     l_req_line_id := l_trx_source_line_id;
11185   ELSE
11186 
11187     /*
11188      * Internal Order Intransit Shipment (62, 21, 8),
11189      * Int Req Direct Org Xfr (95, 3, 7),
11190      * Int Order Direct Ship (54, 3, 8): TRX_SOURCE_LINE_ID = OE_ORDER_LINE_ID.LINE_ID
11191      * For Internal Req Intr Rcpt (61, 12, 7) and its adjustment (72, 29, 7):
11192      * TRANSACTION_SOURCE_ID = REQUISITION_HEADER_ID, also RCV_TRANSACTION_ID is populated
11193      * which provides the requisition_line_id from RCV_TRANSACTIONS  */
11194     IF ( ( l_txn_action_id   = 3
11195        AND l_txn_src_type_id = 7 ) OR
11196         ( l_txn_action_id    = 3
11197        AND l_txn_src_type_id = 8 ) OR
11198         ( l_txn_action_id           = 21
11199        AND l_txn_src_type_id = 8) ) THEN
11200       l_stmt_num := 27;
11201       SELECT
11202         oel.SOURCE_DOCUMENT_LINE_ID
11203       INTO
11204         l_req_line_id
11205       FROM
11206         OE_ORDER_LINES_ALL oel
11207       WHERE
11208           oel.LINE_ID          = l_trx_source_line_id;
11209 
11210     ELSIF ( ( l_txn_action_id = 12
11211        AND l_txn_src_type_id  = 7) OR
11212             ( l_txn_action_id = 29
11213        AND l_txn_src_type_id  = 7 ) ) THEN
11214        SELECT
11215          REQUISITION_LINE_ID
11216        INTO
11217          l_req_line_id
11218        FROM
11219          RCV_TRANSACTIONS
11220        WHERE
11221          TRANSACTION_ID = l_rcv_txn_id;
11222     ELSE
11223       RETURN;
11224     END IF;
11225   END IF;
11226   if g_debug_flag= 'Y' then
11227     fnd_file.put_line(fnd_file.log, 'Requisition Line ID: '||l_req_line_id);
11228   end if;
11229 
11230   l_primary_qty := abs(l_primary_qty);
11231   l_total_primary_qty := abs(l_total_primary_qty);
11232 
11233   if g_debug_flag= 'Y' then
11234     fnd_file.put_line(fnd_file.log, 'Total Received Primary Qty so far just before current receipt transaction: '||l_total_primary_qty);
11235   end if;
11236 
11237   l_stmt_num := 30;
11238   CompEncumbrance_IntOrdersExp (
11239             p_api_version         => 1.0,
11240 	    p_transaction_id      => p_transaction_id,
11241             p_req_line_id         => l_req_line_id,
11242             p_item_id             => l_item_id,
11243             p_organization_id     => l_organization_id,
11244             p_primary_qty         => l_primary_qty,
11245             p_total_primary_qty   => l_total_primary_qty,
11246 
11247             x_encumbrance_amount  => x_encumbrance_amount,
11248             x_encumbrance_account => x_encumbrance_account,
11249             o_ae_err_rec         => l_err_rec );
11250 
11251   if g_debug_flag= 'Y' then
11252     fnd_file.put_line(fnd_file.log, 'CompEncumbrance_IntOrdersExp (T) >>');
11253   end if;
11254     if(l_err_rec.l_err_num <> 0 and l_err_rec.l_err_num is not null) then
11255         raise process_error;
11256     end if;
11257 
11258 EXCEPTION
11259   when process_error then
11260   o_ae_err_rec.l_err_num := l_err_rec.l_err_num;
11261   o_ae_err_rec.l_err_code := l_err_rec.l_err_code;
11262   o_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
11263 
11264   when others then
11265   o_ae_err_rec.l_err_num := SQLCODE;
11266   o_ae_err_rec.l_err_code := '';
11267   o_ae_err_rec.l_err_msg := 'CompEncumbrance_IntOrdersExp' || to_char(l_stmt_num) ||
11268   substr(SQLERRM,1,180);
11269 
11270 END CompEncumbrance_IntOrdersExp;
11271 
11272 
11273 END CSTPAPBR;