DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSTPAPBR

Source


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