DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSTPACDP

Source


1 PACKAGE BODY CSTPACDP AS
2 /* $Header: CSTACDPB.pls 120.64.12010000.5 2009/01/24 03:15:57 ipineda ship $ */
3 
4 --
5 -- OPM INVCONV  umoogala  Process-Discrete Xfers Enh.
6 -- Added following global variable for Interorg Profit Account
7 -- Accounting Line Type
8 --
9 G_INTERORG_PROFIT_ACCT  BINARY_INTEGER := 34;
10 
11 G_DEBUG                 VARCHAR2(10)   := fnd_profile.value('MRP_DEBUG');
12 
13 
14 --Local procedure
15 PROCEDURE balance_account_txn_type(
16   i_org_id              IN          NUMBER,
17   i_txn_id              IN          NUMBER,
18   i_txn_type_id         IN          NUMBER,
19   O_Error_Num           OUT NOCOPY  NUMBER,
20   O_Error_Code          OUT NOCOPY  VARCHAR2,
21   O_Error_Message       OUT NOCOPY  VARCHAR2);
22 
23 
24 -- PROCEDURE
25 --  cost_txn			This processor writes cost distributions.
26 --
27 --
28 procedure cost_txn(
29   I_ORG_ID		IN	NUMBER,
30   I_TXN_ID		IN 	NUMBER,
31   I_LAYER_ID		IN	NUMBER,
32   I_FOB_POINT		IN	NUMBER,
33   I_EXP_ITEM		IN	NUMBER,
34   I_COMM_ISS_FLAG	IN	NUMBER,
35   I_FLOW_SCHEDULE	IN	NUMBER,
36   I_USER_ID		IN	NUMBER,
37   I_LOGIN_ID    	IN	NUMBER,
38   I_REQ_ID		IN	NUMBER,
39   I_PRG_APPL_ID		IN	NUMBER,
40   I_PRG_ID		IN 	NUMBER,
41   I_TPRICE_OPTION       IN      NUMBER,
42   I_TXF_PRICE           IN      NUMBER,
43   O_Error_Num		OUT NOCOPY	NUMBER,
44   O_Error_Code		OUT NOCOPY	VARCHAR2,
45   O_Error_Message	OUT NOCOPY	VARCHAR2
46 )IS
47   l_item_id	NUMBER;
48   l_cost_grp_id NUMBER;
49   l_txfr_cost_grp NUMBER;
50   l_txn_org_id	NUMBER;
51   l_txn_date	DATE;
52   l_p_qty	NUMBER;
53   l_subinv	VARCHAR2(10);
54   l_txf_org_id	NUMBER;
55   l_qty_adj	NUMBER;
56   l_txf_cost 	NUMBER;
57   l_trp_cost	NUMBER;
58   l_trp_acct	NUMBER;
59   l_txn_act_id	NUMBER;
60   l_txn_src_id	NUMBER;
61   l_txf_subinv	VARCHAR2(10);
62   l_txf_txn_id  NUMBER;
63   l_src_type_id	NUMBER;
64   l_dist_acct	NUMBER;
65   l_mat_acct	NUMBER;
66   l_mat_ovhd_acct NUMBER;
67   l_res_acct	NUMBER;
68   l_osp_acct	NUMBER;
69   l_ovhd_acct	NUMBER;
70   l_pri_curr	VARCHAR2(15);
71   l_alt_curr  	VARCHAR2(10);
72   l_conv_date	DATE;
73   l_conv_rate	NUMBER;
74   l_conv_type	VARCHAR2(30);
75   l_sob_id	NUMBER;
76   l_operating_unit NUMBER;
77 
78   l_purch_encum_flag FINANCIALS_SYSTEM_PARAMS_ALL.PURCH_ENCUMBRANCE_FLAG%TYPE;
79   l_enc_rev	NUMBER;
80   l_enc_amount	NUMBER;
81   l_enc_acct	NUMBER;
82 
83   l_err_num	NUMBER;
84   l_err_code	VARCHAR2(240);
85   l_err_msg	VARCHAR2(240);
86   l_stmt_num	NUMBER;
87   l_debug       VARCHAR2(80);
88 
89   l_return_status VARCHAR2(1);
90   l_return_message       VARCHAR2(1000);
91   l_msg_count   NUMBER;
92   l_msg_data    VARCHAR2(240);
93 
94   -- new variable for dropshipment
95   l_logical_txn NUMBER;
96   l_txn_type_id NUMBER;
97 
98   -- new variables for Revenue COGS Matching
99   l_so_issue_accounting NUMBER;
100   l_cogs_percentage     NUMBER;
101   l_cogs_om_line_id     NUMBER;
102 
103   l_onhand_var_acct  NUMBER;/*LCM*/
104 
105   process_error	EXCEPTION;
106 
107 /* NL Changes */
108     l_sql_stmt               VARCHAR2(8000);
109     l_cse_installed          BOOLEAN;
110     l_cse_hook_used          NUMBER;
111     l_status                 VARCHAR2(1);
112     l_industry               VARCHAR2(1);
113     l_schema                 VARCHAR2(30);
114     l_nl_trackable           VARCHAR2(1);
115     l_asset_creation_code    VARCHAR2(30);
116     CST_FAILED_CSE_CALL      EXCEPTION;
117 /* NL Changes */
118 /* Bug 6405593*/
119 l_hook_used                  NUMBER;
120 l_loc_non_recoverable_tax    NUMBER;
121 l_loc_recoverable_tax        NUMBER;
122 l_total_dist_amount          NUMBER;
123 l_rcv_transaction_id         NUMBER;
124 l_loc_po_distribution_id     NUMBER;
125 /* Bug 6405593*/
126 
127   /* OPM INVCONV sschinch/umoogala changes*/
128   l_pd_txfr_price   NUMBER;  -- Transfer Price
129   l_pd_txfr_ind     NUMBER;  -- Process-Discrete Xfer Flag
130   -- End OPM INVCONV umoogala
131 
132 
133 BEGIN
134   -- initialize local variables
135   l_err_num := 0;
136   l_err_code := '';
137   l_err_msg := '';
138   l_hook_used:=0;
139 
140   l_debug := fnd_profile.value('MRP_DEBUG');
141 
142 
143   IF (l_debug = 'Y' ) THEN
144     FND_FILE.put_line( FND_FILE.log, 'CSTPACDP.Cost_txn << ' );
145   END IF;
146 
147 
148   -- Populate local variables
149 
150   l_stmt_num := 10;
151 
152   --
153   -- OPM INVCONV umoogala  Added column to fetch transfer price from mmt.
154   --
155   select
156         inventory_item_id, organization_id,
157 	nvl(cost_group_id,1), nvl(transfer_cost_group_id,1),
158         transaction_date,
159         primary_quantity, subinventory_code,
160         transfer_organization_id,
161         quantity_adjusted, nvl(transfer_cost,0), nvl(transportation_cost,0),
162         nvl(transportation_dist_account,-1),
163         transaction_action_id,
164         nvl(transaction_source_id,-1),
165         transfer_subinventory,
166         nvl(transfer_transaction_id,-1),
167         transaction_source_type_id, nvl(distribution_account_id,-1),
168         nvl(material_account, -1), nvl(material_overhead_account, -1),
169 	nvl(resource_account, -1), nvl(outside_processing_account, -1),
170 	nvl(overhead_account, -1),
171 	nvl(encumbrance_account, -1), nvl(encumbrance_amount, 0),
172         currency_code,
173         nvl(currency_conversion_date,transaction_date),
174         nvl(currency_conversion_rate,-1) , currency_conversion_type,
175         logical_transaction,
176         transaction_type_id,
177         transfer_price, -- OPM INVCONV umoogala
178         trx_source_line_id, -- COGS OM Line ID
179         nvl(so_issue_account_type,1), -- 1=COGS, 2=Deferred COGS
180         cogs_recognition_percent,
181 	rcv_transaction_id,
182 	nvl(expense_account_id,-1)
183   into
184 	l_item_id,
185 	l_txn_org_id,
186 	l_cost_grp_id,
187 	l_txfr_cost_grp,
188 	l_txn_date,
189 	l_p_qty,
190 	l_subinv,
191 	l_txf_org_id,
192 	l_qty_adj,
193 	l_txf_cost,
194 	l_trp_cost,
195 	l_trp_acct,
196 	l_txn_act_id,
197 	l_txn_src_id,
198 	l_txf_subinv,
199 	l_txf_txn_id,
200 	l_src_type_id,
201 	l_dist_acct,
202 	l_mat_acct,
203 	l_mat_ovhd_acct,
204 	l_res_acct,
205 	l_osp_acct,
206 	l_ovhd_acct,
207 	l_enc_acct,
208 	l_enc_amount,
209 	l_alt_curr,
210 	l_conv_date,
211 	l_conv_rate,
212 	l_conv_type,
213     l_logical_txn,
214     l_txn_type_id,
215     l_pd_txfr_price,  -- OPM INVCONV umoogala
216     l_cogs_om_line_id,
217     l_so_issue_accounting,
218     l_cogs_percentage,
219     l_rcv_transaction_id,
220     l_onhand_var_acct
221   from mtl_material_transactions
222   where transaction_id = i_txn_id;
223 
224 /* NL Changes */
225   l_stmt_num := 12;
226     -----------------------------
227     -- Get Installation Status --
228     -----------------------------
229 
230   l_cse_installed := FND_INSTALLATION.GET_APP_INFO ( 'CSE',
231                                                     l_status,
232                                                     l_industry,
233                                                     l_schema);
234 
235    /* Check if item is NL trackable and depreciable (asset_creation_code) */
236   SELECT   nvl(comms_nl_trackable_flag, 'N'), asset_creation_code
237   INTO     l_nl_trackable, l_asset_creation_code
238   FROM     mtl_system_items
239   WHERE    inventory_item_id = l_item_id
240   AND      organization_id =  i_org_id;
241 
242 /* Write out log warning if Item is NL trackable or depreciable and
243  * NL is not installed */
244 
245   IF ( (l_nl_trackable = 'Y' OR l_asset_creation_code IS NOT NULL) AND l_status = 'N') THEN
246     l_err_msg := 'WARNING: Item NL Trackable/Depreciable but NL not Installed';
247     IF (l_debug = 'Y' ) THEN
248       FND_FILE.PUT_LINE(FND_FILE.LOG, l_err_msg);
249       FND_FILE.PUT_LINE(FND_FILE.LOG, 'NL Trackable Flag: ' || l_nl_trackable);
250       FND_FILE.PUT_LINE(FND_FILE.LOG, 'NL Depreciable Flag: ' || l_asset_creation_code);
251     END IF;
252   END IF;
253 
254   IF (l_status = 'I') THEN
255 
256     l_stmt_num := 13;
257     CSE_COST_DISTRIBUTION_STUB.cost_distribution (i_txn_id,
258                                                   l_cse_hook_used,
259                                                   l_err_num,
260                                                   l_err_code,
261                                                   l_err_msg);
262     IF (l_debug = 'Y' ) THEN
263       fnd_file.put_line(fnd_file.log,'Debug: CSE cost distribution hook : ' || to_char(l_cse_hook_used));
264     END IF;
265 
266     IF (l_err_num <> 0) THEN
267      RAISE CST_FAILED_CSE_CALL;
268     END IF;
269 
270   END IF;
271 
272 
273   if( l_cse_hook_used = 1 ) then
274     return;
275   end if;
276 
277 
278 
279   l_stmt_num := 15;
280 
281   select decode(encumbrance_reversal_flag,1,1,2,0,0)
282   into   l_enc_rev
283   from   mtl_parameters
284   where  organization_id = i_org_id;
285 
286 
287   -- Figure out currency stuff.
288 
289   l_stmt_num := 20;
290 
291 /* The following query will be made to refer to cst_organization_definitions
292    as an impact of the HR-PROFILE option */
293 
294   select set_of_books_id,
295          operating_unit
296   into l_sob_id,
297        l_operating_unit
298   /*from org_organization_definitions */
299   from cst_organization_definitions
300   where organization_id = i_org_id;
301 
302   l_stmt_num := 30;
303 
304   select currency_code
305   into l_pri_curr
306   from gl_sets_of_books
307   where set_of_books_id = l_sob_id;
308 
309   if (l_alt_curr is not NULL and l_conv_rate = -1) then
310     if (l_alt_curr <> l_pri_curr) then
311 
312       if (l_conv_type is NULL) then
313         FND_PROFILE.get('CURRENCY_CONVERSION_TYPE', l_conv_type);
314       end if;
315 
316       l_stmt_num := 40;
317 
318       l_conv_rate := gl_currency_api.get_rate(l_sob_id,l_alt_curr,l_txn_date,
319 					   l_conv_type);
320     end if;
321   end if;
322 
323   /*******************************************************************
324    ** I break down the inventory transactions into 5 categories:    **
325    ** 1) WIP transactions - all wip related transactions such as    **
326    **			    wip issue and completions               **
327    ** 2) subinventory transfers					    **
328    ** 3) interorg transfers					    **
329    ** 4) Average cost update					    **
330    ** 5) Add if clause for drop ship/global procure                 **
331    ** 6) rest of inventory transactions				    **
332    *******************************************************************/
333   if (i_comm_iss_flag = 1) THEN
334  -- call dedicated function for common issue to wip txn here.
335     CSTPACDP.comm_iss_to_wip(I_TXN_ID => i_txn_id,
336 			     I_COMM_ISS_FLAG => i_comm_iss_flag,
337 			     I_FLOW_SCHEDULE => i_flow_schedule,
338 			     I_ORG_ID => i_org_id,
339 			     I_ITEM_ID => l_item_id,
340 			     I_COST_GRP_ID => l_cost_grp_id,
341 			     I_TXFR_COST_GRP => l_txfr_cost_grp,
342 			     I_TXN_DATE => l_txn_date,
343 			     I_P_QTY => l_p_qty,
344 			     I_SUBINV => l_subinv,
345 			     I_SOB_ID => l_sob_id,
346 			     I_PRI_CURR => l_pri_curr,
347 			     I_ALT_CURR => l_alt_curr,
348 			     I_CONV_DATE => l_conv_date,
349 			     I_CONV_RATE => l_conv_rate,
350 			     I_CONV_TYPE => l_conv_type,
351 			     I_EXP_ITEM => i_exp_item,
352 			     I_TXF_SUBINV => l_txf_subinv,
353 			     I_TXN_ACT_ID => l_txn_act_id,
354 			     I_TXN_SRC_ID => l_txn_src_id,
355 			     I_SRC_TYPE_ID => l_src_type_id,
356 			     I_USER_ID => i_user_id,
357 			     I_LOGIN_ID => i_login_id,
358 			     I_REQ_ID => i_req_id,
359 			     I_PRG_APPL_ID => i_prg_appl_id,
360 			     I_PRG_ID => i_prg_id,
361 			     O_Error_Num => l_err_num,
362 			     O_Error_Code => l_err_code,
363 			     O_Error_Message => l_err_msg);
364 
365 
366 
367     elsif (l_src_type_id = 5 and l_txn_act_id <> 2) then
368     -- WIP transaction
369     -- These always occur in base currency.
370     CSTPACDP.wip_cost_txn(i_org_id, i_txn_id, l_cost_grp_id, l_item_id,
371 			l_txn_date, l_p_qty, l_subinv, l_txn_act_id,
372 			l_txn_src_id, l_src_type_id,
373 			l_dist_acct,
374 			l_sob_id, l_pri_curr,
375 			i_exp_item,i_flow_schedule,i_user_id, i_login_id,
376 			i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
377 			l_err_code, l_err_msg);
378     -- check error;
379 
380     if(l_err_num <> 0) then
381       raise process_error;
382     end if;
383 
384   /* Changes for VMI. Adding Planning Transfer transaction */
385   elsif (l_txn_act_id in (2,5,28,55)) then
386     -- Subinventory transfer,VMI Planning transfer,staging transfer,cost group transfer for WMS mobile
387     CSTPACDP.sub_cost_txn(i_org_id, i_txn_id, l_cost_grp_id, l_txfr_cost_grp,
388 			l_item_id,
389 			l_txn_date, l_p_qty, l_subinv, l_txf_subinv,
390 			l_txf_txn_id, l_txn_act_id,
391 			l_txn_src_id, l_src_type_id,
392 			l_sob_id, l_pri_curr, l_alt_curr,
393 			l_conv_date, l_conv_rate, l_conv_type,
394 			i_exp_item, i_user_id, i_login_id,
395 			i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
396 			l_err_code, l_err_msg);
397     -- check error;
398 
399     if(l_err_num <> 0) then
400       raise process_error;
401     end if;
402 
403   elsif (l_txn_act_id in (3,12,21,15,22)) then
404     -- OPM INVCONV umoogala: Added 15 (Logical Itr Receipt) and 22 (Logical Itr Shipment) actions.
405 
406     -- Interorg transfers
407 
408     /* TPRICE: If the transfer pricing option is yes, set transfer credit to be zero */
409     if (i_tprice_option <> 0) then
410        l_txf_cost := 0;
411     end if;
412 
413     -- OPM INVCONV umoogala: Added new parameter (l_pd_txfr_price) to send transfer_price
414     CSTPACDP.interorg_cost_txn(i_org_id, i_txn_id, l_cost_grp_id,
415 			l_txfr_cost_grp, l_item_id,
416 			l_txn_date, l_p_qty, l_subinv,
417 			l_txn_org_id, l_txf_org_id,
418 			l_txf_txn_id, l_txf_cost, l_trp_cost,
419 			l_trp_acct, l_txn_act_id,
420 			l_txn_src_id, l_src_type_id,
421 			i_fob_point, i_exp_item, l_pd_txfr_price, i_user_id, i_login_id,
422 			i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
423 			l_err_code, l_err_msg);
424     -- check error;
425 
426     if(l_err_num <> 0) then
427       raise process_error;
428     end if;
429 
430     /* TPRICE: If the transfer pricing option is yes, need to do accounting adjustment here */
431     --
432     -- Bug 5172278.
433     -- For Process/Discrete Xfers, tp option is set to 2.
434     -- For direct xfer do not call the following procedure
435     --
436     if ( /*(i_exp_item = 0) AND */ /* removed for bug #5569128 */
437         (i_tprice_option <> 0) AND
438         ((l_txn_act_id = 21 AND l_src_type_id = 8 AND i_fob_point = 1) OR
439          (l_txn_act_id = 12 AND l_src_type_id = 7 AND i_fob_point = 2) OR
440 	 (l_txn_act_id = 15 AND l_src_type_id = 7) OR
441 	 (l_txn_act_id = 22 AND l_src_type_id = 8))
442        )
443     then
444         CST_TPRICE_PVT.Adjust_Acct(1.0, p_tprice_option => i_tprice_option, p_txf_price => i_txf_price,
445                         p_txn_id => i_txn_id, p_cost_grp_id => l_cost_grp_id,
446 			p_txf_cost_grp => l_txfr_cost_grp, p_item_id => l_item_id,
447 			p_txn_date => l_txn_date, p_qty => l_p_qty, p_subinv => l_subinv,
448                         p_txf_subinv => l_txf_subinv, p_txn_org_id => l_txn_org_id,
449                         p_txf_org_id => l_txf_org_id, p_txf_txn_id => l_txf_txn_id,
450                         p_txf_cost => l_txf_cost, p_txn_act_id => l_txn_act_id,
451 			p_txn_src_id => l_txn_src_id, p_src_type_id => l_src_type_id,
452 			p_fob_point => i_fob_point, p_user_id => i_user_id, p_login_id => i_login_id,
453                         p_req_id => i_req_id, p_prg_appl_id => i_prg_appl_id,
454                         p_prg_id => i_prg_id, x_return_status => l_return_status,
455                         x_msg_count => l_msg_count, x_msg_data => l_msg_data, x_error_num => l_err_num,
456                         x_error_code => l_err_code, x_error_message => l_err_msg);
457 
458         if (l_err_num <> 0) then
459            raise process_error;
460         end if;
461     end if;
462 
463   elsif (l_txn_act_id = 24) then /*Removed i_txn_src_type_id = 15 for bug 6030287*/
464     -- Average Cost Update
465     CSTPACDP.avcu_cost_txn(i_org_id, i_txn_id, l_cost_grp_id, l_item_id,
466 			l_txn_date, l_qty_adj, l_txn_act_id,
467 			l_txn_src_id, l_src_type_id,
468 			l_mat_acct, l_mat_ovhd_acct, l_res_acct,
469 			l_osp_acct, l_ovhd_acct,
470 			l_sob_id, l_pri_curr, l_alt_curr,
471 			l_conv_date, l_conv_rate, l_conv_type,
472 			i_exp_item,l_onhand_var_acct, i_user_id, i_login_id,
473 			i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
474 			l_err_code, l_err_msg);
475     -- check error;
476 
477     if(l_err_num <> 0) then
478       raise process_error;
479     end if;
480 
481   -- the following 2 elsifs are added for dropshipment project
482   elsif (l_logical_txn = 1) then
483     -- 11i10 - dropshipment project added logical transaction types
484     -- this function handles all new logical transactions
485     CSTPACDP.logical_cost_txn(i_org_id, i_txn_id, l_cost_grp_id, l_item_id,
486                       l_txn_date, l_p_qty, l_subinv, l_txn_act_id,
487                       l_txn_src_id, l_src_type_id, l_txn_type_id, l_dist_acct,
488                       l_sob_id, l_pri_curr, l_alt_curr,
489                       l_conv_date, l_conv_rate, l_conv_type,
490                       l_so_issue_accounting, l_cogs_percentage, l_cogs_om_line_id,
491                       i_exp_item, i_user_id, i_login_id,
492                       i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
493                       l_err_code, l_err_msg);
494     if(l_err_num <> 0) then
495       raise process_error;
496     end if;
497   elsif (l_txn_act_id = 25 and l_src_type_id = 1) then
498     -- 11i10 - Retroactive Price Update for consigned
499     CSTPACDP.consigned_update_cost_txn(i_org_id, i_txn_id, l_cost_grp_id, l_item_id,
500                         l_txn_date, l_p_qty, l_subinv, l_txn_act_id,
501                         l_txn_src_id, l_src_type_id, l_dist_acct,
502                         l_sob_id, l_pri_curr, l_alt_curr,
503                         l_conv_date, l_conv_rate, l_conv_type,
504                         i_exp_item, i_user_id, i_login_id,
505                         i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
506                         l_err_code, l_err_msg);
507     if(l_err_num <> 0) then
508       raise process_error;
509     end if;
510   else
511     -- Rest of inventory transactions
512     CSTPACDP.inv_cost_txn(i_org_id, i_txn_id, l_cost_grp_id, l_item_id,
513 			l_txn_date, l_p_qty, l_subinv, l_txn_act_id,
514 			l_txn_src_id, l_src_type_id,l_dist_acct,
515 			l_sob_id, l_pri_curr, l_alt_curr,
516 			l_conv_date, l_conv_rate, l_conv_type,
517             l_so_issue_accounting, l_cogs_percentage, l_cogs_om_line_id,
518 			i_exp_item, i_user_id, i_login_id,
519 			i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
520 			l_err_code, l_err_msg);
521     -- check error;
522 
523     if(l_err_num <> 0) then
524       raise process_error;
525     end if;
526 
527   end if;
528 
529   -- Take care of rounding errors.
530 
531 /* FIX bug 668528 -> change i_org_id to l_txn_org_id so it will balance the account own by that txns */
532 --  balance_account(l_txn_org_id, i_txn_id,l_err_num, l_err_code, l_err_msg);
533 --BUG#6732955
534   balance_account_txn_type(
535   i_org_id              => l_txn_org_id,
536   i_txn_id              => i_txn_id,
537   i_txn_type_id         => l_txn_type_id,
538   O_Error_Num           => l_err_num,
539   O_Error_Code          => l_err_code,
540   O_Error_Message       => l_err_msg);
541 
542 
543   -- check error
544 
545   if(l_err_num <> 0) then
546       raise process_error;
547   end if;
548 
549 
550   if (l_txn_act_id in (3,12,21)) then
551     balance_account(l_txf_org_id, i_txn_id,l_err_num, l_err_code, l_err_msg);
552 
553   if(l_err_num <> 0) then
554       raise process_error;
555     end if;
556 
557   end if;
558 
559 
560 
561   /*
562    * Encumrbance amount would be computed within costing for all cases except
563    * PO
564    * Use CSTPAVCP.CompEncumbrance_IntOrdersExp to get the amount and the
565    * account
566    * This is used for transactions that are sourced from Internal Requisitions
567    * or Internal Orders
568    * For Internal Order Intransit transactions, encumbrance is reversed against
569    * based on FOB point. Encumbrance is reversed when ownership changes
570    * If FOB point is receipt, it is reversed for the Receipt transaction
571    * otherwise, if it the FOB point is shipment, it is reversed against the
572    * shipment transaction
573    * Also, for Internal Order intransit transfer transactions, if the transfer
574    * is either for an expense item (in the shipment organization) or from an
575    * an expense subinventory, no reversal of encumbrace takes place.
576    * This is required since no reservation takes place in the above case.
577    */
578 
579   /* Compute Encumbrance Amount and create the reversal entry,
580      if applicable */
581   BEGIN
582    SELECT nvl(req_encumbrance_flag,'N') /*nvl(purch_encumbrance_flag, 'N')Bug 6469694*/
583    INTO   l_purch_encum_flag
584    FROM   FINANCIALS_SYSTEM_PARAMS_ALL
585    WHERE  set_of_books_id = l_sob_id
586    AND    org_id          = l_operating_unit;
587   EXCEPTION
588    WHEN no_data_found THEN
589      l_purch_encum_flag := 'N';
590   END;
591 
592   /* Explanation:
593      For Shipment transaction and FOB shipment, check if the worker is launched
594      by the receipt organization. Same for Receipt organization and FOB receipt
595      : Ensure that the worker belongs to the receipt organization
596      In these cases, the workers of the transfer orgs do the accounting in those
597      organizations */
598 
599   IF ( l_enc_rev = 1 AND l_purch_encum_flag = 'Y') THEN
600     IF ( ( ( l_txn_act_id = 21 and l_src_type_id = 8 and i_fob_point = 1 ) AND
601            ( l_txn_org_id <> i_org_id ) ) OR
602          ( l_txn_act_id = 3 and l_src_type_id in (7,8) and l_p_qty > 0 ) OR
603          ( l_txn_act_id = 29 and l_src_type_id = 7 ) OR
604          ( ( l_txn_act_id = 12 and l_src_type_id = 7 and i_fob_point = 2 ) AND
605               l_txn_org_id = i_org_id ) ) THEN
606       IF (l_debug = 'Y' ) THEN
607         FND_FILE.put_line( FND_FILE.log, 'Encumbrance Reversal' );
608       END IF;
609       CSTPAVCP.CompEncumbrance_IntOrdersExp (
610         p_api_version         => 1.0,
611         p_transaction_id      => i_txn_id,
612         x_encumbrance_amount  => l_enc_amount,
613         x_encumbrance_account => l_enc_acct,
614         x_return_status       => l_return_status,
615         x_return_message      => l_return_message );
616 
617       IF (l_debug = 'Y' ) THEN
618         FND_FILE.put_line( FND_FILE.log, 'Encumbrance Amount: '||l_enc_amount||', Encumbrance Account: '||l_enc_acct );
619       END IF;
620       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
621         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
622       END IF;
623       /* Create the encumbrance for this transaction against the receipt
624          organization */
625       CSTPACDP.encumbrance_account(i_org_id, i_txn_id, l_item_id,
626                         -1*l_enc_amount, l_p_qty, l_enc_acct,
627                         l_sob_id, l_txn_date,
628                         l_txn_src_id, l_src_type_id,
629                         l_pri_curr, l_alt_curr,
630                         l_conv_date, l_conv_rate, l_conv_type,
631                         i_user_id, i_login_id,
632                         i_req_id, i_prg_appl_id, i_prg_id,
633                         l_err_num, l_err_code, l_err_msg);
634       if(l_err_num <> 0) then
635         raise process_error;
636       end if;
637     END IF;
638   END IF;
639   /* For PO related transactions, use encumbrance amount and account
640      on MMT */
641   IF ((l_src_type_id = 1 and (l_enc_rev = 1 and l_enc_amount <> 0))) THEN
642 
643     /* Bug 6030861 :Added hook call to override the recoverable and Non-Recoverable
644                     taxes for ENCUMBRANCE_REVERSAL event */
645       IF l_debug = 'Y' THEN
646 	   fnd_file.put_line(fnd_file.log, 'Getting Po_Distribution_id for Receving transaction :'||l_rcv_transaction_id);
647       END IF;
648 
649 	l_stmt_num := 43;
650 	SELECT po_distribution_id
651 	  INTO l_loc_po_distribution_id
652 	  FROM rcv_transactions
653 	 WHERE transaction_id =l_rcv_transaction_id;
654 
655       IF l_debug = 'Y' THEN
656 	   fnd_file.put_line(fnd_file.log, 'Calling CST_Common_hooks.Get_NRtax_amount for PO_DIST :'||l_loc_po_distribution_id);
657       END IF;
658 
659 	l_stmt_num := 45;
660 	 l_hook_used := CST_Common_hooks.Get_NRtax_amount(
661 	                I_ACCT_TXN_ID        =>i_txn_id,
662 	                I_SOURCE_DOC_TYPE    =>'PO',
663 	                I_SOURCE_DOC_ID      =>l_loc_po_distribution_id,
664 	                I_ACCT_SOURCE        =>'MMT',
665 	                I_USER_ID            =>i_user_id,
666 	                I_LOGIN_ID           =>i_login_id,
667 	                I_REQ_ID             =>i_req_id,
668 	                I_PRG_APPL_ID        =>i_prg_appl_id,
669 	                I_PRG_ID             =>i_prg_id,
670 	                O_DOC_NR_TAX         =>l_loc_non_recoverable_tax,
671 	                O_DOC_REC_TAX        =>l_loc_recoverable_tax,
672 	                O_Err_Num            =>l_err_num,
673 	                O_Err_Code           =>l_err_code,
674 	                O_Err_Msg            =>l_err_msg
675 				   );
676         IF l_hook_used <>0 THEN
677 
678 	 IF (l_err_num <> 0) THEN
679 	      -- Error occured
680               IF l_debug = 'Y' THEN
681                    fnd_file.put_line(fnd_file.log, 'Error getting Enc Tax in CST_Common_hooks.Get_NRtax_amount at statement :'||l_stmt_num);
682                    fnd_file.put_line(fnd_file.log, 'Error Code :  '||l_err_code||' Error Message : '||l_err_msg);
683 	      END IF;
684               RAISE process_error;
685 	    END IF;
686 
687 	IF l_debug = 'Y' THEN
688            fnd_file.put_line(fnd_file.log,'Hook Used  CST_Common_hooks.Get_NRtax_amount :'|| l_hook_used ||
689 	                     ' l_loc_recoverable_tax : '||l_loc_recoverable_tax||
690                              ' l_loc_non_recoverable_tax : '||l_loc_non_recoverable_tax);
691         END IF;
692 
693 	l_stmt_num := 46;
694 
695 		select  pod.quantity_ordered * (poll.price_override) + nvl(pod.nonrecoverable_tax,0)
696 		  into  l_total_dist_amount
697 		  from  po_line_locations_all poll,
698 			po_distributions_all  pod
699 		 where  poll.po_header_id         =pod.po_header_id
700 		   and  poll.line_location_id     =pod.line_location_id
701 		   and  pod.po_distribution_id    =l_loc_po_distribution_id;
702 
703 	l_stmt_num := 47;
704 
705          l_enc_amount:=nvl(l_enc_amount,0)+(nvl(l_enc_amount,0) * nvl(l_loc_non_recoverable_tax,0))/l_total_dist_amount;
706 
707        END IF;
708 
709 
710       /* Bug 6030861 :Added hook call to override the recoverable and Non-Recoverable
711                       taxes for ENCUMBRANCE_REVERSAL event */
712 
713     CSTPACDP.encumbrance_account(i_org_id, i_txn_id, l_item_id,
714                         -1*l_enc_amount, l_p_qty, l_enc_acct,
715                         l_sob_id, l_txn_date,
716                         l_txn_src_id, l_src_type_id,
717                         l_pri_curr, l_alt_curr,
718                         l_conv_date, l_conv_rate, l_conv_type,
719                         i_user_id, i_login_id,
720                         i_req_id, i_prg_appl_id, i_prg_id,
721                         l_err_num, l_err_code, l_err_msg);
722   END IF;
723     if(l_err_num <> 0) then
724         raise process_error;
725     end if;
726 
727 
728   l_stmt_num := 50;
729   -- Update the transaction costed date
730 
731   Update mtl_cst_actual_cost_details
732   set transaction_costed_date = sysdate
733   where transaction_id = i_txn_id
734   and transaction_costed_date is NULL;
735 
736   IF (l_debug = 'Y' ) THEN
737     FND_FILE.put_line( FND_FILE.log, 'CSTPACDP.Cost_Txn >>');
738   END IF;
739 
740  EXCEPTION
741 
742 /* NL Changes */
743     WHEN CST_FAILED_CSE_CALL THEN
744       O_Error_Num   := 20001;
745       O_Error_Code  := SUBSTR('CSTACDPB.cost_txn('
746                             || to_char(l_stmt_num)
747                             || '): '
748                             || 'FAILED CSE Package Call. '
749                             || l_err_msg,1,240);
750       O_Error_Message := substr(l_err_msg,1,240) ;
751 /* NL Changes */
752 
753  when gl_currency_api.NO_RATE then
754  rollback;
755  O_error_num := 9999;
756  O_error_code := 'CST_NO_GL_RATE';
757  FND_MESSAGE.set_name('BOM', 'CST_NO_GL_RATE');
758  O_error_message := FND_MESSAGE.Get;
759 
760 
761  when process_error then
762  rollback;
763  O_error_num := l_err_num;
764  O_error_code := l_err_code;
765  O_error_message := l_err_msg;
766 
767  when others then
768  rollback;
769  O_error_num := SQLCODE;
770  O_error_message := 'CSTPACDP.cost_txn' || to_char(l_stmt_num) ||
771 		     substr(SQLERRM,1,180);
772 
773 END cost_txn;
774 
775 procedure comm_iss_to_wip(
776   I_TXN_ID              IN      NUMBER,
777   I_COMM_ISS_FLAG       IN      NUMBER,
778   I_FLOW_SCHEDULE	IN	NUMBER,
779   I_ORG_ID              IN      NUMBER,
780   I_ITEM_ID             IN      NUMBER,
781   I_COST_GRP_ID         IN      NUMBER,
782   I_TXFR_COST_GRP       IN      NUMBER,
783   I_TXN_DATE            IN      DATE,
784   I_P_QTY               IN      NUMBER,
785   I_SUBINV              IN      VARCHAR2,
786   I_SOB_ID              IN      NUMBER,
787   I_PRI_CURR            IN      VARCHAR2,
788   I_ALT_CURR            IN      VARCHAR2,
789   I_CONV_DATE           IN      DATE,
790   I_CONV_RATE           IN      NUMBER,
791   I_CONV_TYPE           IN      VARCHAR2,
792   I_EXP_ITEM            IN      NUMBER,
793   I_TXF_SUBINV          IN      VARCHAR2,
794   I_TXN_ACT_ID          IN      NUMBER,
795   I_TXN_SRC_ID          IN      NUMBER,
796   I_SRC_TYPE_ID         IN      NUMBER,
797   I_USER_ID             IN      NUMBER,
798   I_LOGIN_ID            IN      NUMBER,
799   I_REQ_ID              IN      NUMBER,
800   I_PRG_APPL_ID         IN      NUMBER,
801   I_PRG_ID              IN      NUMBER,
802   O_Error_Num           OUT NOCOPY     NUMBER,
803   O_Error_Code          OUT NOCOPY     VARCHAR2,
804   O_Error_Message       OUT NOCOPY     VARCHAR2
805 ) is
806 
807   l_subinv              VARCHAR2(10);
808   l_cost_grp_id         NUMBER;
809   l_exp_sub1            NUMBER;
810   l_exp_acct1           NUMBER;
811   l_exp_sub2            NUMBER;
812   l_exp_acct2           NUMBER;
813   l_exp_sub             NUMBER;
814   l_exp_acct            NUMBER;
815   l_qty                 NUMBER;
816   l_wip_qty     	NUMBER;
817   l_acct_class 		VARCHAR2(10);
818   l_mat_acct    	NUMBER;
819   l_mat_ovhd_acct 	NUMBER;
820   l_res_acct    	NUMBER;
821   l_osp_acct    	NUMBER;
822   l_ovhd_acct   	NUMBER;
823   l_exp_job     	NUMBER;
824   l_ovhd_absp   	NUMBER;
825   l_stmt_num            NUMBER;
826   l_msg_count           NUMBER;
827   l_return_status       VARCHAR2(11);
828   l_msg_data            VARCHAR2(2000);
829   l_wms_flg             NUMBER;
830   process_error         EXCEPTION;
831   l_action_id           NUMBER; /* Bug#4259926 */
832   l_err_num     NUMBER;
833   l_err_code    VARCHAR2(240);
834   l_err_msg     VARCHAR2(240);
835 
836 
837  BEGIN
838 
839   -- initialize local variables
840   l_err_num := 0;
841   l_err_code := '';
842   l_err_msg := '';
843 
844 
845  -- If the item is expense, it has no cost, hence no distributions are
846  -- performed for the item.
847 
848   if (i_exp_item = 1) then
849     return;
850   end if;
851 
852  -- Figure out expense subinventory for both from and to.
853  -- Not sure if this is applicable in the case of a common issue to WIP.
854 
855  -- The CITW averaging and acocunting follow the rules below.
856  -- Issuing Common Sub	 Job		(assumed) Proj Sub  Costing/Acct
857  -- *********************************************************************
858  -- * Asset		 Asset		 Asset			Yes	*
859  -- * Asset		 Expense	 Asset			Yes	*
860  -- * Expense		 Asset		 Asset			Yes	*
861  -- * Expense		 Expense	 N/A			No	*
862  -- *********************************************************************
863  -- Note that in reality there is no project sub in between. The assmpn
864  -- of asset is being made. This will enable us to use the Cost group
865  -- layer for reaveraging and accounts for distbn to the Project.
866  -- The Exp - Exp case involves no entry. This is analogous to the exp - exp
867  -- subinv transfer case.
868 
869   if(wms_install.check_install(l_return_status, l_msg_count, l_msg_data,i_org_id)) then
870      l_wms_flg := 1;
871   else
872      l_wms_flg := 0;
873   end if;
874 
875   l_stmt_num := 10;
876 
877   select decode(asset_inventory,1,0,1)
878   into l_exp_sub1
879   from mtl_secondary_inventories
880   where secondary_inventory_name = i_subinv
881   and organization_id = i_org_id;
882 
883   if (l_wms_flg = 0) then
884      select nvl(expense_account, -1)
885      into l_exp_acct1
886      from mtl_secondary_inventories
887      where secondary_inventory_name = i_subinv
888      and organization_id = i_org_id;
889   else
890      if (i_cost_grp_id = 1) then
891       /* Need to change cst_avg_dist_accts_v to include expense_account */
892        select nvl(expense_account,-1)
893        into l_exp_acct1
894        from mtl_parameters
895        where  organization_id = i_org_id;
896      else
897        select nvl(expense_account, -1)
898        into l_exp_acct1
899        from cst_cost_group_accounts
900        where cost_group_id = i_cost_grp_id
901        and organization_id = i_org_id;
902      end if;
903   end if;
904 
905 
906   l_stmt_num := 20;
907 
908   If (i_flow_schedule <> 1) THEN
909 
910   select decode(class_type,1,0,3,0,1), -1
911   into l_exp_sub2, l_exp_acct2
912   from wip_discrete_jobs wdj,
913        wip_accounting_classes wac
914   where
915 	wdj.wip_entity_id 	=	I_TXN_SRC_ID 	and
916 	wdj.class_code 		=	wac.class_code	and
917 	wdj.organization_id	=	wac.organization_id and
918   	wdj.organization_id = i_org_id;
919 
920  else
921 
922   l_stmt_num := 22;
923 
924   select decode(class_type,1,0,3,0,1), -1
925   into l_exp_sub2, l_exp_acct2
926   from wip_flow_schedules wdj,
927        wip_accounting_classes wac
928   where
929         wdj.wip_entity_id       =       I_TXN_SRC_ID    and
930         wdj.class_code          =       wac.class_code  and
931         wdj.organization_id     =       wac.organization_id and
932         wdj.organization_id = i_org_id;
933 
934  End If;
935 
936 
937   -- If expense to expense transfer then no accounting entries.
938   if (l_exp_sub1 = 1 and l_exp_sub2 = 1) then
939     return;
940   end if;
941 
942 
943  -- In a comm issue to wip txn, we have to perform distributions for both
944  -- subs initially, treating the txn first as a sub txfr. Then we have to
945  -- do distbns for the Issue part of the txn, treating it as a wip issue.
946 
947 
948  /* -- First do the subinv txfr transaction part -- */
949  -- Note that the Project sub is always an asset sub, so the
950  -- l_exp_sub2 parameter must always reflect asset. This is the case
951  -- even if the job is an expense job. So hard code it to Zero to
952  -- indicate this.
953 
954   -- Do distribution for both subs.
955   FOR i in 1..2 loop
956     if (i = 1) then
957       l_qty := i_p_qty;
958       l_exp_sub := l_exp_sub1;
959       l_exp_acct := l_exp_acct1;
960       l_subinv := i_subinv;
961       l_cost_grp_id := i_cost_grp_id;
962     else
963       l_qty := -1 * i_p_qty;
964       l_exp_sub := 0;
965       l_exp_acct := l_exp_acct2;
966       l_subinv := i_txf_subinv;
967       l_cost_grp_id := i_txfr_cost_grp;
968     end if;
969 
970  -- The transaction_source_type_id that gets passed in for this txn
971  -- will correspond to a WIP issue (=5). However, the value that is
972  -- used for a subinv txf is 13. This is hard coded into the call to
973  -- the inventory_accounts() procedure.
974 
975     inventory_accounts(i_org_id, i_txn_id,i_comm_iss_flag,
976 			2, l_cost_grp_id,
977                         i_item_id, l_qty,
978                         i_sob_id,i_txn_date, i_txn_src_id, 13,
979                         i_exp_item, l_exp_sub, l_exp_acct, l_subinv, 0, NULL,
980                         i_pri_curr, i_alt_curr, i_conv_date,
981                         i_conv_rate, i_conv_type,
982                         i_user_id, i_login_id, i_req_id,
983                         i_prg_appl_id, i_prg_id,
984                         l_err_num, l_err_code, l_err_msg);
985 
986     -- check error
987     if (l_err_num <> 0) then
988     raise process_error;
989     end if;
990 
991 
992   end loop;
993 
994  --
995  -- Now do the wip issue part
996  --
997 
998   -- There are no cost distributions for expense items.
999   if (i_exp_item = 1) then
1000     return;
1001   end if;
1002 
1003 
1004  -- From the standpoint of CITW, the project sub is always an asset, hence
1005  -- the l_exp_sub parameter must be set to 0. This will ensure that the
1006  -- correct acct line type is picked in the inventory_accounts() function.
1007 
1008  	l_exp_sub := 0;
1009 
1010   -- Figure out accts, expense subinventory and expense job flags.
1011 
1012   l_stmt_num := 50;
1013 
1014   IF (i_flow_schedule <> 1) THEN
1015 
1016   select material_account, material_overhead_account, resource_account,
1017         outside_processing_account, overhead_account, class_code
1018   into l_mat_acct, l_mat_ovhd_acct, l_res_acct, l_osp_acct, l_ovhd_acct,
1019         l_acct_class
1020   from wip_discrete_jobs
1021   where organization_id = i_org_id
1022   and wip_entity_id = i_txn_src_id;
1023 
1024  ELSE
1025 
1026    l_stmt_num := 55;
1027 
1028    select material_account, material_overhead_account, resource_account,
1029         outside_processing_account, overhead_account, class_code
1030   into l_mat_acct, l_mat_ovhd_acct, l_res_acct, l_osp_acct, l_ovhd_acct,
1031         l_acct_class
1032   from wip_flow_schedules
1033   where organization_id = i_org_id
1034   and wip_entity_id = i_txn_src_id;
1035 
1036  END IF;
1037 
1038   l_stmt_num := 60;
1039 
1040   select decode(class_type, 4,1,0)
1041   into l_exp_job
1042   from wip_accounting_classes
1043   where class_code = l_acct_class
1044   and organization_id = i_org_id;
1045 
1046 
1047   -- Debit/Credit WIP accounts
1048 
1049   -- the transaction quantity from the WIP point of view is the opposite
1050   -- of inventory.
1051   l_wip_qty := -1 * i_p_qty;
1052 
1053    /* Bug#4259926 Added if statement to pass correct transaction_action_id
1054    value for distribute_account and inventory_accounts procedures. */
1055      if (i_txn_act_id = 27 and i_src_type_id = 5 and i_comm_iss_flag =1) then
1056          l_action_id := 27;
1057      else
1058          l_action_id := 1;
1059      end if;
1060   /* Bug#4259926 replaced 1 with l_action_id  */
1061   distribute_accounts(i_org_id, i_txn_id,i_comm_iss_flag,i_txfr_cost_grp,l_action_id,
1062 		      i_item_id, l_wip_qty,7, 1, l_ovhd_absp,
1063                         NULL, l_mat_acct, l_mat_ovhd_acct, l_res_acct,
1064                         l_osp_acct, l_ovhd_acct,
1065                         i_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
1066                         i_subinv, NULL, i_pri_curr, NULL, NULL, NULL, NULL,
1067                         i_user_id, i_login_id,
1068                         i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
1069                         l_err_code, l_err_msg);
1070 
1071  -- check error
1072 
1073     if(l_err_num <> 0) then
1074       raise process_error;
1075     end if;
1076 
1077  -- Here the accounting that gets done for the Inventory side
1078  -- actually gets done for the txfr CG, since the issue is from the
1079  -- txfr CG to the Job. Hence pass i_txfr_cost_grp in the call to
1080  -- inventory_accounts().
1081     /* Bug#4259926 replaced 1 with l_action_id  */
1082     inventory_accounts(i_org_id, i_txn_id, i_comm_iss_flag,
1083 			l_action_id, i_txfr_cost_grp,
1084                         i_item_id, i_p_qty,
1085                         i_sob_id,i_txn_date, i_txn_src_id, i_src_type_id,
1086                         i_exp_item, l_exp_sub, l_exp_acct, i_subinv,0,NULL,
1087                         i_pri_curr, NULL, NULL, NULL, NULL,
1088                         i_user_id, i_login_id, i_req_id,
1089                         i_prg_appl_id, i_prg_id,
1090                         l_err_num, l_err_code, l_err_msg);
1091 
1092     if(l_err_num <> 0) then
1093     raise process_error;
1094     end if;
1095 
1096 
1097 
1098  EXCEPTION
1099 
1100  when process_error then
1101  rollback;
1102  O_error_num := l_err_num;
1103  O_error_code := l_err_code;
1104  O_error_message := l_err_msg;
1105 
1106  when others then
1107 
1108  rollback;
1109  O_error_num := SQLCODE;
1110  O_error_message := 'CSTPACDP.comm_iss_to_wip (' || to_char(l_stmt_num) ||
1111                     '): ' ||  substr(SQLERRM,1,180);
1112 
1113  END comm_iss_to_wip;
1114 
1115 procedure wip_cost_txn(
1116   I_ORG_ID		IN	NUMBER,
1117   I_TXN_ID		IN 	NUMBER,
1118   I_COST_GRP_ID		IN	NUMBER,
1119   I_ITEM_ID		IN	NUMBER,
1120   I_TXN_DATE		IN	DATE,
1121   I_P_QTY		IN	NUMBER,
1122   I_SUBINV		IN	VARCHAR2,
1123   I_TXN_ACT_ID		IN	NUMBER,
1124   I_TXN_SRC_ID		IN	NUMBER,
1125   I_SRC_TYPE_ID		IN	NUMBER,
1126   I_DIST_ACCT		IN	NUMBER,
1127   I_SOB_ID		IN	NUMBER,
1128   I_PRI_CURR		IN	VARCHAR2,
1129   I_EXP_ITEM		IN	NUMBER,
1130   I_FLOW_SCHEDULE	IN	NUMBER,
1131   I_USER_ID		IN	NUMBER,
1132   I_LOGIN_ID    	IN	NUMBER,
1133   I_REQ_ID		IN	NUMBER,
1134   I_PRG_APPL_ID		IN	NUMBER,
1135   I_PRG_ID		IN 	NUMBER,
1136   O_Error_Num		OUT NOCOPY	NUMBER,
1137   O_Error_Code		OUT NOCOPY	VARCHAR2,
1138   O_Error_Message	OUT NOCOPY	VARCHAR2
1139 )IS
1140   l_exp_sub	NUMBER;
1141   l_exp_acct	NUMBER;
1142   l_exp_job	NUMBER;
1143   l_ovhd_absp   NUMBER;
1144   l_mat_ovhd_exists NUMBER;
1145   l_wip_qty 	NUMBER;
1146   l_cost	NUMBER;
1147   l_acct	NUMBER;
1148   l_mat_acct	NUMBER;
1149   l_mat_ovhd_acct NUMBER;
1150   l_res_acct	NUMBER;
1151   l_osp_acct	NUMBER;
1152   l_ovhd_acct	NUMBER;
1153   l_mat_var_acct NUMBER;
1154   l_res_var_acct NUMBER;
1155   l_osp_var_acct NUMBER;
1156   l_ovhd_var_acct NUMBER;
1157   l_acct_class VARCHAR2(10);
1158   l_stmt_num 	NUMBER;
1159   l_err_num	NUMBER;
1160   l_err_code	VARCHAR2(240);
1161   l_err_msg	VARCHAR2(240);
1162   l_msg_count   NUMBER;
1163   l_return_status VARCHAR2(11);
1164   l_msg_data    VARCHAR2(2000);
1165   l_wms_flg     NUMBER;
1166   process_error	EXCEPTION;
1167   l_debug       VARCHAR2(80);
1168 
1169 BEGIN
1170   -- Initialize variables;
1171   l_err_num := 0;
1172   l_err_code := '';
1173   l_err_msg := '';
1174   l_ovhd_absp := 0;
1175   l_mat_ovhd_exists := '';
1176   l_debug := fnd_profile.value('MRP_DEBUG');
1177 
1178   -- There are no cost distributions for expense items.
1179   if (i_exp_item = 1) then
1180     return;
1181   end if;
1182 
1183   -- Figure out accts, expense subinventory and expense job flags.
1184 
1185   l_stmt_num := 10;
1186 
1187  IF (i_flow_schedule <>1) THEN
1188 
1189   select material_account, material_overhead_account, resource_account,
1190 	outside_processing_account, overhead_account, class_code
1191   into l_mat_acct, l_mat_ovhd_acct, l_res_acct, l_osp_acct, l_ovhd_acct,
1192 	l_acct_class
1193   from wip_discrete_jobs
1194   where organization_id = i_org_id
1195   and wip_entity_id = i_txn_src_id;
1196 
1197  ELSE
1198 
1199     --
1200     -- cfm scrap
1201     --
1202     IF (i_txn_act_id = 30 AND i_dist_acct = -1) THEN
1203        SELECT material_account, material_overhead_account, resource_account,
1204               outside_processing_account, overhead_account, class_code,
1205               material_variance_account, resource_variance_account,
1206               outside_proc_variance_account, overhead_variance_account
1207        INTO l_mat_acct, l_mat_ovhd_acct, l_res_acct,
1208 	    l_osp_acct, l_ovhd_acct, l_acct_class,
1209             l_mat_var_acct, l_res_var_acct,
1210 	    l_osp_var_acct, l_ovhd_var_acct
1211        FROM wip_flow_schedules
1212        WHERE organization_id = i_org_id
1213        AND wip_entity_id = i_txn_src_id;
1214 
1215     ELSE
1216        SELECT material_account, material_overhead_account, resource_account,
1217               outside_processing_account, overhead_account, class_code
1218        INTO l_mat_acct, l_mat_ovhd_acct, l_res_acct, l_osp_acct, l_ovhd_acct,
1219             l_acct_class
1220        FROM wip_flow_schedules
1221        WHERE organization_id = i_org_id
1222        AND wip_entity_id = i_txn_src_id;
1223     END IF;
1224 
1225 END IF;
1226 
1227   l_stmt_num := 20;
1228 
1229   select decode(class_type, 4,1,0)
1230   into l_exp_job
1231   from wip_accounting_classes
1232   where class_code = l_acct_class
1233   and organization_id = i_org_id;
1234 
1235   if(wms_install.check_install(l_return_status, l_msg_count, l_msg_data,i_org_id)) then
1236      l_wms_flg := 1;
1237   else
1238      l_wms_flg := 0;
1239   end if;
1240 
1241   l_stmt_num := 30;
1242   -- Scrap transactions do not have inventory impact!!
1243   if (i_txn_act_id <> 30) then
1244     select decode(asset_inventory,1,0,1)
1245     into l_exp_sub
1246     from mtl_secondary_inventories
1247     where secondary_inventory_name = i_subinv
1248     and organization_id = i_org_id;
1249 
1250     if (l_wms_flg = 0) then
1251       select nvl(expense_account, -1)
1252       into l_exp_acct
1253       from mtl_secondary_inventories
1254       where secondary_inventory_name = i_subinv
1255       and organization_id = i_org_id;
1256     else
1257       if (i_cost_grp_id = 1) then
1258         /* Need to change cst_avg_dist_accts_v to include expense_account */
1259         select nvl(expense_account,-1)
1260         into l_exp_acct
1261         from mtl_parameters
1262         where  organization_id = i_org_id;
1263       else
1264         select nvl(expense_account, -1)
1265         into l_exp_acct
1266         from cst_cost_group_accounts
1267         where cost_group_id = i_cost_grp_id
1268         and organization_id = i_org_id;
1269       end if;
1270     end if;
1271 
1272     -- Transactions between expense subinventories and expense jobs are
1273     -- not distributed.
1274     if (l_exp_sub = 1 and l_exp_job = 1) then
1275       return;
1276     end if;
1277 
1278   end if;
1279 
1280   -- Debit/Credit WIP accounts
1281 
1282   -- the transaction quantity from the WIP point of view is the opposite
1283   -- of inventory.
1284   l_wip_qty := -1 * i_p_qty;
1285 
1286   -- Material overhead absorption happens for the assembly completion
1287   -- and asembly return transactions.
1288   if (i_txn_act_id in (31,32)) then
1289     l_ovhd_absp := 1;
1290   end if;
1291 
1292  -- If the txn is not a CITW txn, pass 0 for the comm_iss_flag
1293 
1294   distribute_accounts(i_org_id, i_txn_id,0,i_cost_grp_id,i_txn_act_id,
1295 		      i_item_id, l_wip_qty,7, 1, l_ovhd_absp,
1296 			NULL, l_mat_acct, l_mat_ovhd_acct, l_res_acct,
1297 			l_osp_acct, l_ovhd_acct,
1298 			i_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
1299 			i_subinv, NULL, i_pri_curr, NULL, NULL, NULL, NULL,
1300 			i_user_id, i_login_id,
1301 			i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
1302 			l_err_code, l_err_msg);
1303 
1304  -- check error
1305 
1306     if(l_err_num <> 0) then
1307       fnd_file.put_line(fnd_file.log, 'failed after distribute_accounts');
1308       raise process_error;
1309     end if;
1310 
1311     if (i_txn_act_id = 30) then  /* WIP Scrap */
1312        if (i_flow_schedule = 1 AND i_dist_acct = -1 ) then
1313           cfm_scrap_dist_accounts(i_org_id, i_txn_id,0,i_cost_grp_id,i_txn_act_id,
1314 			i_item_id, i_p_qty,2, 1, 0,
1315 			i_dist_acct, l_mat_var_acct, NULL, l_res_var_acct,
1316 			l_osp_var_acct, l_ovhd_var_acct,
1317 			i_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
1318 			i_subinv, NULL, i_pri_curr, NULL, NULL, NULL, NULL,
1319 			i_user_id, i_login_id,
1320 			i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
1321 			l_err_code, l_err_msg);
1322        else
1323        distribute_accounts(i_org_id, i_txn_id,0,i_cost_grp_id,i_txn_act_id,
1324 			i_item_id, i_p_qty,2, 1, 0,
1325 			i_dist_acct, i_dist_acct, i_dist_acct, i_dist_acct,
1326 			i_dist_acct, i_dist_acct,
1327 			i_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
1328 			i_subinv, NULL, i_pri_curr, NULL, NULL, NULL, NULL,
1329 			i_user_id, i_login_id,
1330 			i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
1331 			l_err_code, l_err_msg);
1332       end if;
1333 
1334  -- check error
1335 
1336     if(l_err_num <> 0) then
1337     raise process_error;
1338     end if;
1339 
1340   else
1341     -- All the other wip transactions have inventory impact.
1342     inventory_accounts(i_org_id, i_txn_id,2,
1343 		        i_txn_act_id,i_cost_grp_id,
1344 			i_item_id, i_p_qty,
1345 			i_sob_id,i_txn_date, i_txn_src_id, i_src_type_id,
1346 			i_exp_item, l_exp_sub, l_exp_acct, i_subinv,0,NULL,
1347 			i_pri_curr, NULL, NULL, NULL, NULL,
1348 			i_user_id, i_login_id, i_req_id,
1349 			i_prg_appl_id, i_prg_id,
1350 			l_err_num, l_err_code, l_err_msg);
1351 
1352     if(l_err_num <> 0) then
1353         fnd_file.put_line(fnd_file.log, 'failed after inv_accounts');
1354     raise process_error;
1355     end if;
1356 
1357 
1358     if (l_ovhd_absp = 1) then
1359 
1360       l_stmt_num := 40;
1361 
1362       /* assembly completion or assembly return */
1363       select count(*)
1364       into l_mat_ovhd_exists
1365       from mtl_cst_actual_cost_details
1366       where transaction_id = i_txn_id
1367       and organization_id = i_org_id
1368       and cost_element_id = 2
1369       and level_type = 1;
1370 
1371       if (l_mat_ovhd_exists > 0) then
1372         ovhd_accounts(i_org_id, i_txn_id, i_item_id, -1 * i_p_qty, i_sob_id,
1373 			i_txn_date,i_txn_src_id, i_src_type_id,
1374 			i_subinv, NULL,
1375 			i_pri_curr, NULL, NULL, NULL, NULL,
1376 			i_user_id, i_login_id, i_req_id,
1377                         i_prg_appl_id, i_prg_id,
1378 			l_err_num, l_err_code, l_err_msg);
1379 
1380 
1381     if(l_err_num <> 0) then
1382           fnd_file.put_line(fnd_file.log, 'failed after ovhd_accounts');
1383     raise process_error;
1384     end if;
1385 
1386       end if;
1387     end if;
1388   end if;
1389 
1390  EXCEPTION
1391 
1392  when process_error then
1393   fnd_file.put_line(fnd_file.log,'process raised');
1394  rollback;
1395  O_error_num := l_err_num;
1396  O_error_code := l_err_code;
1397  O_error_message := l_err_msg;
1398 
1399  when others then
1400    fnd_file.put_line(fnd_file.log,'other exceptions  raised');
1401  rollback;
1402  O_error_num := SQLCODE;
1403  O_error_message := 'CSTPACDP.wip_cost_txn (' || to_char(l_stmt_num) ||
1404                     '): ' ||  substr(SQLERRM,1,180);
1405 
1406 END wip_cost_txn;
1407 
1408 procedure sub_cost_txn(
1409   I_ORG_ID		IN	NUMBER,
1410   I_TXN_ID		IN 	NUMBER,
1411   I_COST_GRP_ID		IN	NUMBER,
1412   I_TXFR_COST_GRP	IN	NUMBER,
1413   I_ITEM_ID		IN	NUMBER,
1414   I_TXN_DATE		IN	DATE,
1415   I_P_QTY		IN	NUMBER,
1416   I_SUBINV		IN	VARCHAR2,
1417   I_TXF_SUBINV		IN	VARCHAR2,
1418   I_TXF_TXN_ID		IN	NUMBER,
1419   I_TXN_ACT_ID		IN	NUMBER,
1420   I_TXN_SRC_ID		IN	NUMBER,
1421   I_SRC_TYPE_ID		IN	NUMBER,
1422   I_SOB_ID		IN	NUMBER,
1423   I_PRI_CURR		IN	VARCHAR2,
1424   I_ALT_CURR		IN	VARCHAR2,
1425   I_CONV_DATE		IN	DATE,
1426   I_CONV_RATE		IN	NUMBER,
1427   I_CONV_TYPE		IN	VARCHAR2,
1428   I_EXP_ITEM		IN	NUMBER,
1429   I_USER_ID		IN	NUMBER,
1430   I_LOGIN_ID    	IN	NUMBER,
1431   I_REQ_ID		IN	NUMBER,
1432   I_PRG_APPL_ID		IN	NUMBER,
1433   I_PRG_ID		IN 	NUMBER,
1434   O_Error_Num		OUT NOCOPY	NUMBER,
1435   O_Error_Code		OUT NOCOPY	VARCHAR2,
1436   O_Error_Message	OUT NOCOPY	VARCHAR2
1437 )IS
1438   l_subinv		VARCHAR2(10);
1439   l_cost_grp_id		NUMBER;
1440   l_exp_sub1	 	NUMBER;
1441   l_exp_acct1		NUMBER;
1442   l_exp_sub2		NUMBER;
1443   l_exp_acct2		NUMBER;
1444   l_exp_sub		NUMBER;
1445   l_exp_acct		NUMBER;
1446   l_qty			NUMBER;
1447   l_stmt_num		NUMBER;
1448   process_error		EXCEPTION;
1449   l_err_num	NUMBER;
1450   l_err_code	VARCHAR2(240);
1451   l_err_msg	VARCHAR2(240);
1452   l_msg_count           NUMBER;
1453   l_return_status       VARCHAR2(11);
1454   l_msg_data            VARCHAR2(2000);
1455   l_wms_flg             NUMBER;
1456 
1457 BEGIN
1458   -- Initialize variables;
1459   l_err_num := 0;
1460   l_err_code := '';
1461   l_err_msg := '';
1462 
1463   -- There are no cost distributions for expense items.
1464   if (i_exp_item = 1) then
1465     return;
1466   end if;
1467 
1468   if(wms_install.check_install(l_return_status, l_msg_count, l_msg_data,i_org_id)) then
1469      l_wms_flg := 1;
1470   else
1471      l_wms_flg := 0;
1472   end if;
1473 
1474 
1475   -- Figure out expense subinventory for both from and to.
1476 
1477   l_stmt_num := 10;
1478 
1479   select decode(asset_inventory,1,0,1)
1480   into l_exp_sub1
1481   from mtl_secondary_inventories
1482   where secondary_inventory_name = i_subinv
1483   and organization_id = i_org_id;
1484 
1485   l_stmt_num := 20;
1486   select decode(asset_inventory,1,0,1)
1487   into l_exp_sub2
1488   from mtl_secondary_inventories
1489   where secondary_inventory_name = nvl(i_txf_subinv,i_subinv)
1490   and organization_id = i_org_id;
1491 
1492   if (l_wms_flg = 0) then
1493      select nvl(expense_account, -1)
1494      into l_exp_acct1
1495      from mtl_secondary_inventories
1496      where secondary_inventory_name = i_subinv
1497      and organization_id = i_org_id;
1498 
1499      select nvl(expense_account, -1)
1500      into l_exp_acct2
1501      from mtl_secondary_inventories
1502      where secondary_inventory_name = i_txf_subinv
1503      and organization_id = i_org_id;
1504 
1505   else
1506       if (i_cost_grp_id = 1) then
1507         /* Need to change cst_avg_dist_accts_v to include expense_account */
1508         select nvl(expense_account,-1)
1509         into l_exp_acct1
1510         from mtl_parameters
1511         where  organization_id = i_org_id;
1512       else
1513         select nvl(expense_account, -1)
1514         into l_exp_acct1
1515         from cst_cost_group_accounts
1516         where cost_group_id = i_cost_grp_id
1517         and organization_id = i_org_id;
1518       end if;
1519 
1520       if (i_txfr_cost_grp = 1) then
1521         select nvl(expense_account,-1)
1522         into l_exp_acct2
1523         from mtl_parameters
1524         where  organization_id = i_org_id;
1525       else
1526         select nvl(expense_account, -1)
1527         into l_exp_acct2
1528         from cst_cost_group_accounts
1529         where cost_group_id = i_txfr_cost_grp
1530         and organization_id = i_org_id;
1531       end if;
1532   end if;
1533 
1534   -- If expense to expense transfer then no accounting entries.
1535   if (l_exp_sub1 = 1 and l_exp_sub2 = 1) then
1536     return;
1537   end if;
1538 
1539   -- Do distribution for both subs.
1540   FOR i in 1..2 loop
1541     if (i = 1) then
1542       l_qty := i_p_qty;
1543       l_exp_sub := l_exp_sub1;
1544       l_exp_acct := l_exp_acct1;
1545       l_subinv := i_subinv;
1546       l_cost_grp_id := i_cost_grp_id;
1547     else
1548       l_qty := -1 * i_p_qty;
1549       l_exp_sub := l_exp_sub2;
1550       l_exp_acct := l_exp_acct2;
1551       l_subinv := i_txf_subinv;
1552       l_cost_grp_id := i_txfr_cost_grp;
1553     end if;
1554 
1555     inventory_accounts(i_org_id, i_txn_id,2,
1556 			i_txn_act_id,l_cost_grp_id,
1557 			i_item_id, l_qty,
1558 			i_sob_id,i_txn_date, i_txn_src_id, i_src_type_id,
1559 			i_exp_item, l_exp_sub, l_exp_acct, l_subinv, 0, NULL,
1560 			i_pri_curr, i_alt_curr, i_conv_date,
1561 			i_conv_rate, i_conv_type,
1562 			i_user_id, i_login_id, i_req_id,
1563                         i_prg_appl_id, i_prg_id,
1564 			l_err_num, l_err_code, l_err_msg);
1565 
1566     -- check error
1567     if (l_err_num <> 0) then
1568     raise process_error;
1569     end if;
1570 
1571 
1572   end loop;
1573 
1574  EXCEPTION
1575 
1576  when process_error then
1577  rollback;
1578  O_error_num := l_err_num;
1579  O_error_code := l_err_code;
1580  O_error_message := l_err_msg;
1581 
1582  when others then
1583 
1584  rollback;
1585  O_error_num := SQLCODE;
1586  O_error_message := 'CSTPACDP.sub_cost_txn' || to_char(l_stmt_num) ||
1587                      substr(SQLERRM,1,180);
1588 
1589 END sub_cost_txn;
1590 
1591 procedure inv_cost_txn(
1592   I_ORG_ID		IN	NUMBER,
1593   I_TXN_ID		IN 	NUMBER,
1594   I_COST_GRP_ID		IN	NUMBER,
1595   I_ITEM_ID		IN	NUMBER,
1596   I_TXN_DATE		IN	DATE,
1597   I_P_QTY		IN	NUMBER,
1598   I_SUBINV		IN	VARCHAR2,
1599   I_TXN_ACT_ID		IN	NUMBER,
1600   I_TXN_SRC_ID		IN	NUMBER,
1601   I_SRC_TYPE_ID		IN	NUMBER,
1602   I_DIST_ACCT		IN	NUMBER,
1603   I_SOB_ID		IN	NUMBER,
1604   I_PRI_CURR		IN	VARCHAR2,
1605   I_ALT_CURR		IN	VARCHAR2,
1606   I_CONV_DATE		IN	DATE,
1607   I_CONV_RATE		IN	NUMBER,
1608   I_CONV_TYPE		IN	VARCHAR2,
1609   I_SO_ACCOUNTING   IN  NUMBER,
1610   I_COGS_PERCENTAGE IN  NUMBER,
1611   I_COGS_OM_LINE_ID IN  NUMBER,
1612   I_EXP_ITEM		IN	NUMBER,
1613   I_USER_ID		IN	NUMBER,
1614   I_LOGIN_ID    	IN	NUMBER,
1615   I_REQ_ID		IN	NUMBER,
1616   I_PRG_APPL_ID		IN	NUMBER,
1617   I_PRG_ID		IN 	NUMBER,
1618   O_Error_Num		OUT NOCOPY	NUMBER,
1619   O_Error_Code		OUT NOCOPY	VARCHAR2,
1620   O_Error_Message	OUT NOCOPY	VARCHAR2
1621 )IS
1622   l_exp_sub	NUMBER;
1623   l_exp_acct	NUMBER;
1624   l_elemental	NUMBER;
1625   l_acct_line_type	NUMBER;
1626   l_ovhd_absp	NUMBER;
1627   l_mat_ovhd_exists NUMBER;
1628   l_acct	NUMBER;
1629   l_mat_acct	NUMBER;
1630   l_mat_ovhd_acct	NUMBER;
1631   l_res_acct	NUMBER;
1632   l_osp_acct	NUMBER;
1633   l_ovhd_acct	NUMBER;
1634   l_stmt_num 	NUMBER;
1635   process_error	EXCEPTION;
1636   l_err_num	NUMBER;
1637   l_err_code	VARCHAR2(240);
1638   l_err_msg	VARCHAR2(240);
1639   l_msg_count           NUMBER;
1640   l_return_status       VARCHAR2(11);
1641   l_msg_data            VARCHAR2(2000);
1642   l_wms_flg             NUMBER;
1643   l_drop_ship_type_code NUMBER; -- 11i10 requires clearing account on all drop
1644                                 -- shipments whether old accounting or new
1645   l_def_cogs_acct_id    NUMBER; -- Revenue COGS Matching
1646   l_cogs_acct_id        NUMBER;
1647   l_ref_om_line_id      NUMBER;
1648 
1649   l_txfr_org_id NUMBER;
1650   l_txfr_txn_id NUMBER;
1651   l_debug       VARCHAR2(1);
1652 
1653   --
1654   -- Start INVCONV umoogala: Process/Discrete Xfers
1655   -- Bug 5349860: Internal Order issues to exp. Book receivables with transfer price.
1656   --
1657   l_transfer_price     NUMBER;
1658   l_pd_xfer_ind        VARCHAR2(1) := 'N';
1659   l_cost               NUMBER;
1660   l_io_profit_acct     NUMBER;
1661   l_io_receivable_acct NUMBER;
1662   l_value              NUMBER;
1663 
1664 BEGIN
1665   -- Initialize variables;
1666   l_err_num := 0;
1667   l_err_code := '';
1668   l_err_msg := '';
1669 
1670   l_debug := fnd_profile.value('MRP_DEBUG');
1671     -- Figure out expense subinventory.
1672 
1673   if(wms_install.check_install(l_return_status, l_msg_count, l_msg_data,i_org_id)) then
1674      l_wms_flg := 1;
1675   else
1676      l_wms_flg := 0;
1677   end if;
1678 
1679   l_stmt_num := 10;
1680 
1681   select decode(asset_inventory,1,0,1)
1682   into l_exp_sub
1683   from mtl_secondary_inventories
1684   where secondary_inventory_name = i_subinv
1685   and organization_id = i_org_id;
1686 
1687   if (l_wms_flg = 0) then
1688     l_stmt_num := 11;
1689     select nvl(expense_account, -1)
1690     into l_exp_acct
1691     from mtl_secondary_inventories
1692     where secondary_inventory_name = i_subinv	AND
1693        	  organization_id = i_org_id;
1694   else
1695     if (i_cost_grp_id = 1) then
1696       /* Need to change cst_avg_dist_accts_v to include expense_account */
1697       l_stmt_num := 12;
1698       select nvl(expense_account,-1)
1699       into l_exp_acct
1700       from mtl_parameters
1701       where  organization_id = i_org_id;
1702     else
1703       l_stmt_num := 13;
1704       select nvl(expense_account, -1)
1705       into l_exp_acct
1706       from cst_cost_group_accounts
1707       where cost_group_id = i_cost_grp_id   AND
1708             organization_id = i_org_id;
1709     end if;
1710   end if;
1711 
1712   --
1713   -- Bug 5349860: determine whether this is a Process/Discrete IO issue to expense
1714   --
1715   IF (i_src_type_id = 8 and i_txn_act_id = 1)
1716   THEN
1717     SELECT decode(MOD(SUM(DECODE(MP.process_enabled_flag,'Y',1,2)), 2), 1, 'Y', 'N')
1718       INTO l_pd_xfer_ind
1719       FROM mtl_parameters mp, mtl_material_transactions mmt
1720      WHERE mmt.transaction_id = i_txn_id
1721        AND (mp.organization_id = mmt.organization_id
1722             OR mp.organization_id = mmt.transfer_organization_id);
1723   END IF;
1724   -- End Bug 5349860
1725 
1726   -- No accounting entries posted for any transaction involving an expense
1727   -- item or expense subinventory except in the case of PO receipt, PO
1728   -- return and PO delivery adjustments, and consigned ownership transactions
1729   -- as they are costed similar to PO receipts - bug 2815163
1730   --
1731   -- Bug 5349860: Exculde Process/Discrete IO issue to expense. Added 4th condition.
1732   --
1733 
1734   if ((i_exp_item = 1 or l_exp_sub = 1) and
1735       (i_src_type_id <>1) and
1736       (i_txn_act_id <> 6) and
1737       (NOT (l_pd_xfer_ind = 'Y' and i_txn_act_id = 1 and i_src_type_id = 8)))
1738   then
1739     fnd_file.put_line(fnd_file.log, 'expense item or sub (not PO source or not consigned). So, no accounting for txn: ' || i_txn_id);
1740     return;
1741   elsif (i_exp_item = 1 and l_exp_acct = -1) then
1742 
1743     l_stmt_num := 20;
1744 
1745     select nvl(expense_account, -1)
1746     into l_exp_acct
1747     from mtl_system_items
1748     where inventory_item_id = i_item_id
1749     and organization_id = i_org_id;
1750   end if;
1751 
1752   /*********************************************************************
1753    ** First post to the elemental inventory accounts.                 **
1754    *********************************************************************/
1755 
1756   --
1757   -- Bug 5349860: determine whether this is a Process/Discrete IO issue to expense
1758   --
1759   /****************************************************************************************
1760   * Bug#5485052: Added conditions for i_exp_item and l_exp_sub in both the IF conditions  *
1761   * so that for expense items from asset or expense subinventories, the following entries *
1762   * doesnt get booked                                                                     *
1763   ****************************************************************************************/
1764   if (l_pd_xfer_ind = 'Y' and i_txn_act_id = 1 and i_src_type_id = 8 AND l_exp_sub = 1 and i_exp_item = 0) /* Bug#5485052 ANTHIYAG 23-Aug-2006 */
1765   then
1766    fnd_file.put_line(fnd_file.log, 'Process/Discrete Internal Order issue for Asset Item from Expense Sub-Inventory.');
1767 
1768     distribute_accounts(i_org_id, i_txn_id,
1769                  0,i_cost_grp_id,i_txn_act_id,
1770                  i_item_id, i_p_qty, 2,
1771                  0, 0, l_exp_acct, NULL, NULL, NULL, NULL,NULL,
1772                  i_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
1773                  i_subinv, 1, i_pri_curr,
1774                  NULL, NULL, NULL, NULL,
1775                  i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
1776                  l_err_num, l_err_code, l_err_msg);
1777 
1778     if (l_err_num <> 0) then
1779       raise process_error;
1780     end if;
1781   elsif (l_pd_xfer_ind = 'N') OR
1782         (l_pd_xfer_ind = 'Y' and i_txn_act_id = 1 and i_src_type_id = 8 AND l_exp_sub = 0 and i_exp_item = 0) /* Bug#5485052 ANTHIYAG 23-Aug-2006 */
1783   then
1784     fnd_file.put_line(fnd_file.log, 'Posting elemental inv accounts');
1785 
1786     inventory_accounts(i_org_id, i_txn_id, 2,
1787 		        i_txn_act_id,i_cost_grp_id,
1788 			i_item_id, i_p_qty,
1789 			i_sob_id,i_txn_date, i_txn_src_id, i_src_type_id,
1790 			i_exp_item, l_exp_sub, l_exp_acct, i_subinv, 0, NULL,
1791 			i_pri_curr, i_alt_curr, i_conv_date,
1792 			i_conv_rate, i_conv_type,
1793 			i_user_id, i_login_id, i_req_id,
1794                         i_prg_appl_id, i_prg_id,
1795 			l_err_num, l_err_code, l_err_msg);
1796   end if;
1797 
1798    -- check error
1799       if(l_err_num<>0) then
1800       raise process_error;
1801       end if;
1802 
1803   /*********************************************************************
1804    ** Then post to the offsetting accounts.		              **
1805    *********************************************************************/
1806   if (i_txn_act_id in (4,8)) then
1807     /* cycle count adjustment and physical inventory adjustment */
1808     /* Use the distribution account id as stated in mmt. */
1809     l_acct := i_dist_acct;
1810 
1811     l_acct_line_type := 2;
1812     l_elemental := 0;
1813     l_ovhd_absp := 0;
1814   elsif (i_txn_act_id in (1,27,29) and i_src_type_id = 1) then
1815     /* PO Issue, Reciept, delivery adjustments. */
1816     l_acct := i_dist_acct;
1817     if (l_acct = -1) then
1818 
1819       l_stmt_num := 30;
1820 
1821       select ap_accrual_account
1822       into l_acct
1823       from mtl_parameters
1824       where organization_id = i_org_id;
1825     end if;
1826 
1827     l_stmt_num := 35;
1828     select rt.dropship_type_code
1829     into l_drop_ship_type_code
1830     from rcv_transactions rt, mtl_material_transactions mmt
1831     where rt.transaction_id = mmt.rcv_transaction_id
1832     and mmt.transaction_id = i_txn_id;
1833 
1834     if (l_drop_ship_type_code = 1 OR  l_drop_ship_type_code = 2) then
1835       l_acct_line_type := 31; -- As of 11i10, external drop shipments should always use the clearing acct.
1836     else
1837       l_acct_line_type := 5; -- Receiving Inspection
1838     end if;
1839 
1840     l_elemental := 0;
1841     l_ovhd_absp := 1;
1842   elsif (i_txn_act_id in (1,27,29) and i_src_type_id in (2,12)) then
1843     /* Sales order issue, RMA and Rejection of RMA */
1844     l_acct := i_dist_acct;
1845     if (l_acct = -1) then
1846 
1847       l_stmt_num := 40;
1848 
1849       select nvl(msi.cost_of_sales_account, mp.cost_of_sales_account)
1850       into l_acct
1851       from mtl_system_items msi,
1852            mtl_parameters mp
1853       where msi.organization_id = i_org_id
1854       and msi.inventory_item_id = i_item_id
1855       and mp.organization_id = msi.organization_id;
1856     end if;
1857 
1858     l_cogs_acct_id := l_acct;
1859     l_acct_line_type := 35; -- COGS
1860 
1861     IF (i_src_type_id = 2 AND i_so_accounting = 2) THEN
1862        /* Deferred COGS Accounting for this sales order */
1863 
1864        l_stmt_num := 43;
1865 
1866        SELECT deferred_cogs_account
1867        INTO l_def_cogs_acct_id
1868        FROM mtl_parameters
1869        WHERE organization_id = i_org_id;
1870 
1871        /* Only call Insert_OneSoIssue if the percentage is NULL. */
1872        IF (i_cogs_percentage IS NULL) THEN
1873 
1874           l_stmt_num := 45;
1875           /* Record this sales order issue for COGS deferral by *
1876            * inserting into the Revenue / COGS matching tables  */
1877           CST_RevenueCogsMatch_PVT.Insert_OneSoIssue(
1878                               p_api_version      => 1,
1879                               p_user_id          => i_user_id,
1880                               p_login_id         => i_login_id,
1881                               p_request_id       => i_req_id,
1882                               p_pgm_app_id       => i_prg_appl_id,
1883                               p_pgm_id           => i_prg_id,
1884                               x_return_status    => l_return_status,
1885                               p_cogs_om_line_id  => i_cogs_om_line_id,
1886                               p_cogs_acct_id     => l_cogs_acct_id,
1887                               p_def_cogs_acct_id => l_def_cogs_acct_id,
1888                               p_mmt_txn_id       => i_txn_id,
1889                               p_organization_id  => i_org_id,
1890                               p_item_id          => i_item_id,
1891                               p_transaction_date => i_txn_date,
1892                               p_cost_group_id    => i_cost_grp_id,
1893                               p_quantity         => (-1*i_p_qty)); -- track issue quantities as positive values
1894 
1895           IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1896              l_err_num := -1;
1897              FND_MSG_PUB.count_and_get
1898              (  p_count   => l_msg_count,
1899                 p_data    => l_msg_data,
1900                 p_encoded => FND_API.g_false
1901              );
1902              IF (l_msg_count = 1) THEN
1903                 l_err_msg := substr(l_msg_data,1,240);
1904              ELSE
1905                 l_err_msg := 'Failure in procedure CST_RevenueCogsMatch_PVT.Insert_OneSoIssue()';
1906              END IF;
1907              raise process_error;
1908           END IF;
1909        END IF; -- COGS Percentage is NULL
1910 
1911        l_acct := l_def_cogs_acct_id; -- pass deferred COGS account to distribute_accounts()
1912        l_acct_line_type := 36; -- Deferred COGS
1913 
1914     ELSIF (i_src_type_id = 12) THEN
1915 
1916        l_stmt_num := 47;
1917        -- Get the original sales order issue OM line ID, and check
1918        -- whether the original sales order issue was inserted into
1919        -- the Revenue / COGS Matching data model.
1920        SELECT max(ool.reference_line_id)
1921        INTO l_ref_om_line_id
1922        FROM oe_order_lines_all ool,
1923             cst_revenue_cogs_match_lines crcml
1924        WHERE ool.line_id = i_cogs_om_line_id
1925        AND   ool.reference_line_id = crcml.cogs_om_line_id
1926        AND   crcml.pac_cost_type_id IS NULL;
1927 
1928     END IF; -- Deferred COGS Accounting or RMA receipt
1929 
1930     l_stmt_num := 50;
1931 
1932     select l_acct, l_acct, l_acct, l_acct, l_acct
1933     into l_mat_acct, l_mat_ovhd_acct, l_res_acct, l_osp_acct, l_ovhd_acct
1934     from dual;
1935 
1936     l_elemental := 1;
1937     l_ovhd_absp := 0;
1938   elsif (i_txn_act_id in (1,27,29) and i_src_type_id = 3) then
1939     /* Account */
1940     l_acct := i_txn_src_id;
1941     l_acct_line_type := 2;
1942     l_elemental := 0;
1943     l_ovhd_absp := 0;
1944   elsif (i_txn_act_id in (1,27,29) and i_src_type_id = 6) then
1945     /* Account Alias*/
1946 
1947     l_stmt_num := 60;
1948 
1949     select distribution_account
1950     into l_acct
1951     from mtl_generic_dispositions
1952     where disposition_id = i_txn_src_id
1953     and organization_id = i_org_id;
1954 
1955     l_acct_line_type := 2;
1956     l_elemental := 0;
1957     l_ovhd_absp := 0;
1958   elsif (i_txn_act_id = 6 ) then
1959    /* Change of ownership transaction */
1960        l_acct := i_dist_acct;
1961        l_acct_line_type := 16;
1962        l_elemental :=0;
1963        l_ovhd_absp :=1;
1964   elsif (i_txn_act_id = 1 and i_src_type_id = 8) then
1965     -- Internal Order Issue to Expense
1966     -- Debit Interorg Receivables Account
1967     -- Alcatel Enhancements
1968 
1969     -- Internal Order Issue to Expense could be to the same organization
1970     -- In this case, use the Charge account itself as the clearing
1971     -- account
1972 
1973     --
1974     -- Start INVCONV umoogala: Process/Discrete Xfers
1975     -- Bug 5349860: Book receivables with transfer price
1976     -- Modified following sql get transfer price and flag to
1977     -- determine whether this is a process/discrete xfer.
1978     --
1979 
1980     l_stmt_num := 65;
1981     SELECT MMT.TRANSFER_ORGANIZATION_ID,
1982            MMT.TRANSFER_TRANSACTION_ID,
1983 	   MMT.TRANSFER_PRICE,
1984 	   CASE
1985 	     WHEN mpx.process_enabled_flag <> mp.process_enabled_flag
1986 	     THEN
1987 	       'Y'
1988 	     ELSE
1989 	       'N'
1990 	   END
1991     INTO   l_txfr_org_id,
1992            l_txfr_txn_id,
1993 	   l_transfer_price,
1994 	   l_pd_xfer_ind
1995     FROM   MTL_MATERIAL_TRANSACTIONS mmt,
1996            MTL_PARAMETERS mp, MTL_PARAMETERS mpx
1997     WHERE  mmt.transaction_id = i_txn_id
1998       AND  mp.organization_id  = mmt.organization_id
1999       AND  mpx.organization_id = mmt.transfer_organization_id;
2000 
2001     IF l_txfr_txn_id IS NULL THEN
2002       l_acct := i_dist_acct;
2003       l_acct_line_type := 2;
2004     ELSE
2005       IF i_org_id <> l_txfr_org_id THEN
2006         l_stmt_num := 70;
2007         SELECT interorg_receivables_account,
2008 	       interorg_profit_account     -- Bug 5349860: umoogala
2009         INTO   l_acct,
2010                l_io_profit_acct  -- Bug 5349860: umoogala
2011         FROM   MTL_INTERORG_PARAMETERS
2012         WHERE  from_organization_id = i_org_id
2013         AND    to_organization_id   = l_txfr_org_id;
2014 
2015 	l_acct_line_type := 10;
2016 
2017       ELSE
2018         SELECT DISTRIBUTION_ACCOUNT_ID
2019         INTO   l_acct
2020         FROM   MTL_MATERIAL_TRANSACTIONS
2021         WHERE  transaction_id = l_txfr_txn_id;
2022 
2023 	l_acct_line_type := 2;
2024       END IF;
2025     END IF;
2026 
2027     IF l_debug = 'Y' THEN
2028       FND_FILE.put_line(FND_FILE.log, 'Internal Order Issue to Expense');
2029       FND_FILE.put_line(FND_FILE.log, 'Debit Account - Receivables: '||to_char(l_acct) || ' InterOrg profit: ' || l_io_profit_acct);
2030     END IF;
2031 
2032     l_elemental := 0;
2033     l_ovhd_absp := 0;
2034 
2035   else
2036     /* Misc issue, receipt, default*/
2037     l_acct := i_dist_acct;
2038     l_acct_line_type := 2;
2039     l_elemental := 0;
2040     l_ovhd_absp := 0;
2041   end if;
2042 
2043   fnd_file.put_line(fnd_file.log, 'distribute_accounts()');
2044 
2045   -- Now for RMA receipts that reference an original sales order line ID
2046   IF (i_src_type_id = 12 AND i_txn_act_id = 27 AND l_ref_om_line_id IS NOT NULL) THEN
2047 
2048      /* This procedure will create the offsetting credits *
2049       * split accordingly between COGS and Deferred COGS. */
2050      CST_RevenueCogsMatch_PVT.Process_RmaReceipt(
2051                   x_return_status   => l_return_status,
2052                   x_msg_count       => l_msg_count,
2053                   x_msg_data        => l_msg_data,
2054                   p_rma_om_line_id  => i_cogs_om_line_id,
2055                   p_cogs_om_line_id => l_ref_om_line_id,
2056                   p_cost_type_id    => 2,
2057                   p_txn_quantity    => (-1*i_p_qty),
2058                   p_cogs_percentage => i_cogs_percentage,
2059                   p_organization_id => i_org_id,
2060                   p_transaction_id  => i_txn_id,
2061                   p_item_id         => i_item_id,
2062                   p_sob_id          => i_sob_id,
2063                   p_txn_date        => i_txn_date,
2064                   p_txn_src_id      => i_txn_src_id,
2065                   p_src_type_id     => i_src_type_id,
2066                   p_pri_curr        => i_pri_curr,
2067                   p_alt_curr        => i_alt_curr,
2068                   p_conv_date       => i_conv_date,
2069                   p_conv_rate       => i_conv_rate,
2070                   p_conv_type       => i_conv_type,
2071                   p_user_id         => i_user_id,
2072                   p_login_id        => i_login_id,
2073                   p_req_id          => i_req_id,
2074                   p_prg_appl_id     => i_prg_appl_id,
2075                   p_prg_id          => i_prg_id);
2076 
2077      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2078         l_err_num := -1;
2079         IF (l_msg_count = 1) THEN
2080            l_err_msg := substr(l_msg_data,1,240);
2081         ELSE
2082            l_err_msg := 'Failure in procedure CST_RevenueCogsMatch_PVT.Process_RmaReceipt()';
2083         END IF;
2084         raise process_error;
2085      END IF;
2086 
2087   ELSE -- For all non-RMA transactions, as well as RMAs that don't reference deferred COGS sales orders:
2088 
2089     -- Start INVCONV umoogala: Process/Discrete Xfer.
2090     -- Bug 5349860: Internal Order issues to exp.
2091     -- Book Payables with transfer price. Added following IF condition
2092     -- to handle this case.
2093     --
2094     IF l_pd_xfer_ind = 'N'
2095     THEN
2096 
2097       distribute_accounts(i_org_id, i_txn_id,0,i_cost_grp_id,i_txn_act_id,
2098                     i_item_id, -1 * i_p_qty,l_acct_line_type, l_elemental,
2099                     l_ovhd_absp,l_acct, l_mat_acct, l_mat_ovhd_acct, l_res_acct,
2100                     l_osp_acct, l_ovhd_acct,
2101                     i_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
2102                     i_subinv, NULL, i_pri_curr, i_alt_curr, i_conv_date,
2103                     i_conv_rate, i_conv_type, i_user_id, i_login_id,
2104                     i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
2105                     l_err_code, l_err_msg, i_cogs_om_line_id);
2106 
2107       -- check error
2108       if(l_err_num <> 0) then
2109          raise process_error;
2110       end if;
2111 
2112     ELSIF l_pd_xfer_ind = 'Y'
2113     THEN
2114 
2115       --
2116       -- Book Receivalbes with transfer price
2117       -- Book Interorg Profit with diff of transfer price and cost
2118       --
2119       fnd_file.put_line(fnd_file.log,'Process/Discrete IO Issue to Exp Destination transaction');
2120 
2121       l_stmt_num := 50;
2122 
2123       l_io_receivable_acct := l_acct;
2124 
2125       l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
2126           			sign(-1 * i_p_qty),
2127           			l_acct_line_type,
2128           			NULL, NULL, i_subinv,
2129           			0, NULL, l_err_num, l_err_code,
2130           			l_err_msg);
2131 
2132             -- check error
2133         if(l_err_num<>0) then
2134           raise process_error;
2135         end if;
2136 
2137       if (l_acct = -1) then
2138       	l_acct := l_io_receivable_acct;
2139       end if;
2140 
2141       l_stmt_num := 60;
2142 
2143       fnd_file.put_line(fnd_file.log, 'booking receivables: ' || (-1 * i_p_qty * l_transfer_price) ||' ' || i_pri_curr);
2144 
2145       insert_account(i_org_id, i_txn_id, i_item_id, -1 * i_p_qty * l_transfer_price,
2146           	-1 * i_p_qty, l_acct, i_sob_id, l_acct_line_type,
2147           	NULL, NULL,
2148           	i_txn_date, i_txn_src_id, i_src_type_id,
2149           	i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
2150           	1, i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
2151           	l_err_num, l_err_code, l_err_msg);
2152 
2153           -- check error
2154       if(l_err_num<>0) then
2155         raise process_error;
2156       end if;
2157 
2158       --
2159       -- Book InterOrg Profit
2160       --
2161       fnd_file.put_line(fnd_file.log, 'booking interorg profit if necessary');
2162 
2163 
2164       l_stmt_num := 70;
2165       SELECT SUM(NVL(base_transaction_value,0))
2166        INTO l_value
2167        FROM mtl_transaction_accounts
2168       WHERE transaction_id  = i_txn_id
2169         AND organization_id = i_org_id
2170       ;
2171 
2172       fnd_file.put_line(fnd_file.log,'InterOrg Profit amount: ' || l_value);
2173 
2174       IF l_value <> 0
2175       THEN
2176 
2177         l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
2178             			-1, l_acct_line_type,
2179             			NULL, NULL, i_subinv,
2180             			0, NULL, l_err_num, l_err_code,
2181             			l_err_msg);
2182 
2183         if (l_err_num <> 0) then
2184           raise process_error;
2185         end if;
2186 
2187         if (l_acct = -1) then
2188           l_acct := l_io_profit_acct;
2189         end if;
2190 
2191 
2192         insert_account(i_org_id, i_txn_id, i_item_id, -1 * l_value,
2193                        sign(-1 * l_value) * abs(i_p_qty), l_acct, i_sob_id,
2194                        34, NULL, NULL,
2195                        i_txn_date, i_txn_src_id, i_src_type_id,
2196           	       i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
2197                        1, i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
2198                        l_err_num, l_err_code, l_err_msg);
2199 
2200         if (l_err_num <> 0) then
2201           raise process_error;
2202         end if;
2203 
2204       END IF;
2205 
2206 
2207     END IF;
2208     -- End bug 5349860
2209 
2210   END IF; -- RMA Receipt with Deferred COGS reference
2211 
2212   l_stmt_num := 70;
2213 
2214   if (l_ovhd_absp = 1) then
2215     select count(*)
2216     into l_mat_ovhd_exists
2217     from mtl_cst_actual_cost_details
2218     where transaction_id = i_txn_id
2219     and organization_id = i_org_id
2220     and cost_element_id = 2
2221     and level_type = 1;
2222 
2223     if (l_mat_ovhd_exists > 0) then
2224 
2225       fnd_file.put_line(fnd_file.log, 'ovhd_accounts()');
2226       ovhd_accounts(i_org_id, i_txn_id, i_item_id, -1 * i_p_qty, i_sob_id,
2227 			i_txn_date,i_txn_src_id, i_src_type_id,
2228 			i_subinv, NULL,
2229 			i_pri_curr, i_alt_curr, i_conv_date,
2230 			i_conv_rate, i_conv_type,
2231 			i_user_id, i_login_id, i_req_id,
2232                         i_prg_appl_id, i_prg_id,
2233 			l_err_num, l_err_code, l_err_msg);
2234   -- check error
2235 
2236   if(l_err_num <> 0) then
2237   raise process_error;
2238   end if;
2239 
2240     end if;
2241   end if;
2242 
2243  EXCEPTION
2244 
2245  when process_error then
2246  rollback;
2247  O_error_num := l_err_num;
2248  O_error_code := l_err_code;
2249  O_error_message := l_err_msg;
2250 
2251  when others then
2252 
2253  rollback;
2254  O_error_num := SQLCODE;
2255  O_error_message := 'CSTPACDP.inv_cost_txn (' || to_char(l_stmt_num) ||
2256                      '): ' || substr(SQLERRM,1,180);
2257 
2258 END inv_cost_txn;
2259 
2260 -- OPM INVCONV umoogala: Added new parameter i_txf_price for transfer_price
2261 procedure interorg_cost_txn(
2262   I_ORG_ID		IN	NUMBER,
2263   I_TXN_ID		IN 	NUMBER,
2264   I_COST_GRP_ID		IN	NUMBER,
2265   I_TXFR_COST_GRP	IN	NUMBER,
2266   I_ITEM_ID		IN	NUMBER,
2267   I_TXN_DATE		IN	DATE,
2268   I_TXN_QTY		IN	NUMBER,
2269   I_SUBINV		IN	VARCHAR2,
2270   I_TXN_ORG_ID		IN	NUMBER,
2271   I_TXF_ORG_ID		IN	NUMBER,
2272   I_TXF_TXN_ID		IN	NUMBER,
2273   I_TXF_COST		IN	NUMBER,
2274   I_TRP_COST		IN	NUMBER,
2275   I_TRP_ACCT		IN	NUMBER,
2276   I_TXN_ACT_ID		IN	NUMBER,
2277   I_TXN_SRC_ID		IN	NUMBER,
2278   I_SRC_TYPE_ID		IN	NUMBER,
2279   I_FOB_POINT		IN	NUMBER,
2280   I_EXP_ITEM		IN	NUMBER,
2281   I_TXF_PRICE		IN	NUMBER,  -- OPM INVCONV umoogala
2282   I_USER_ID		IN	NUMBER,
2283   I_LOGIN_ID    	IN	NUMBER,
2284   I_REQ_ID		IN	NUMBER,
2285   I_PRG_APPL_ID		IN	NUMBER,
2286   I_PRG_ID		IN 	NUMBER,
2287   O_Error_Num		OUT NOCOPY	NUMBER,
2288   O_Error_Code		OUT NOCOPY	VARCHAR2,
2289   O_Error_Message	OUT NOCOPY	VARCHAR2
2290 )IS
2291   l_intransit	NUMBER;
2292   l_exp_sub	NUMBER;
2293   l_exp_acct	NUMBER;
2294   l_snd_rcv 	NUMBER;
2295   l_io_inv_acct	NUMBER;
2296   l_io_txfr_acct NUMBER;
2297   l_io_rec_acct	NUMBER;
2298   l_io_pay_acct	NUMBER;
2299   l_io_ppv_acct	NUMBER;
2300   l_intansit	NUMBER;
2301   l_snd_uom	VARCHAR2(3);
2302   l_rcv_uom	VARCHAR2(3);
2303   l_snd_sob_id	NUMBER;
2304   l_snd_curr	VARCHAR2(10);
2305   l_rcv_sob_id	NUMBER;
2306   l_rcv_curr	VARCHAR2(10);
2307   l_curr_type	VARCHAR2(30);
2308   l_conv_rate	NUMBER;
2309   l_conv_date	DATE;
2310   l_std_mat_acct	NUMBER;
2311   l_std_mat_ovhd_acct	NUMBER;
2312   l_std_res_acct	NUMBER;
2313   l_std_osp_acct	NUMBER;
2314   l_std_ovhd_acct	NUMBER;
2315   l_value	NUMBER;
2316   l_ppv		NUMBER;
2317   l_acct	NUMBER;
2318   l_from_org	NUMBER;
2319   l_to_org	NUMBER;
2320   l_from_cost_grp NUMBER;
2321   l_to_cost_grp NUMBER;
2322   l_cg_id       NUMBER;
2323   l_from_layer  NUMBER;
2324   l_to_layer    NUMBER;
2325   l_subinv	VARCHAR2(10);
2326   l_std_from_org NUMBER;
2327   l_std_to_org	NUMBER;
2328   l_std_org	NUMBER;
2329   l_std_exp_acct NUMBER;
2330   l_std_exp_item NUMBER;
2331   l_std_exp_sub NUMBER;
2332   l_snd_qty	NUMBER;
2333   l_rcv_qty	NUMBER;
2334   l_err_num	NUMBER;
2335   l_err_code	VARCHAR2(240);
2336   l_err_msg	VARCHAR2(240);
2337   l_stmt_num	NUMBER;
2338   l_msg_count           NUMBER;
2339   l_return_status       VARCHAR2(11);
2340   l_msg_data            VARCHAR2(2000);
2341   l_wms_flg             NUMBER;
2342   process_error	EXCEPTION;
2343   -- Changes for PJM for Standard Costing
2344   l_pjm_flg             NUMBER;
2345   l_debug               VARCHAR2(1);
2346 
2347  --Changes for MOH Absorption
2348   l_earn_moh    NUMBER;
2349   moh_rules_error EXCEPTION;
2350 
2351   -- OPM INVCONV umoogala
2352   l_io_profit_acct  NUMBER;  -- Inter-Org Profit Account
2353   l_pd_txfr_ind     NUMBER;  -- Process-Discrete Xfer Flag
2354   -- End OPM INVCONV umoogala
2355 
2356   --
2357   -- OPM INVCONV umoogala
2358   -- Bug 5233635; Should not book InterOrg Profit when Intercompany Invoicing is enabled.
2359   --
2360   l_io_invoicing          NUMBER;
2361   l_org_ou                NUMBER;
2362   l_txf_org_ou            NUMBER;
2363 
2364   l_exp                   NUMBER;
2365   l_acct_line_type        NUMBER;
2366 
2367   l_inv_ap_accrual_acct   NUMBER; /* Bug#5471471 ANTHIYAG 18-Aug-2006 */
2368 
2369   l_moh_offset_acct       NUMBER; -- Bug 5677953
2370 
2371 BEGIN
2372   -- initialize local variables
2373   l_err_num := 0;
2374   l_err_code := '';
2375   l_err_msg := '';
2376 
2377   l_exp_sub := 0;
2378   l_exp_acct := -1;
2379 
2380   /*Commented for Bug 3460659*/
2381   /*
2382   l_std_exp_item := 0;
2383   l_std_exp_sub  := 0;
2384   */
2385 
2386   l_std_exp_item := -1; /*Added for Bug 3460659*/
2387   l_std_exp_sub := -1;  /*Added for Bug 3460659*/
2388 
2389   l_std_exp_acct := -1;
2390   l_intransit := 0;
2391   l_subinv := i_subinv;
2392 
2393   l_earn_moh := 1;
2394   l_return_status := fnd_api.g_ret_sts_success;
2395   l_msg_count := 0;
2396 
2397 
2398     fnd_file.put_line(fnd_file.log,'interorg_cost_txn(..)');
2399     fnd_file.put_line(fnd_file.log, 'transaction_id:' || i_txn_id);
2400     fnd_file.put_line(fnd_file.log,'org_id:' || i_org_id);
2401     fnd_file.put_line(fnd_file.log,'i_txn_org_id:' || i_txn_org_id);
2402     fnd_file.put_line(fnd_file.log,'i_txf_org_id:' || i_txf_org_id);
2403     fnd_file.put_line(fnd_file.log,'i_txf_txn_id:' || i_txf_txn_id);
2404     fnd_file.put_line(fnd_file.log,'i_exp_item:' || i_exp_item);
2405     fnd_file.put_line(fnd_file.log,'i_fob_point:' || i_fob_point);
2406 
2407     /* OPM INVCONV sschinch check if this is a process discrete transfer */
2408     SELECT MOD(SUM(DECODE(MP.process_enabled_flag,'Y',1,2)), 2)
2409       INTO l_pd_txfr_ind
2410       FROM mtl_parameters mp
2411      WHERE mp.organization_id = i_txn_org_id
2412         OR mp.organization_id = i_txf_org_id;
2413 
2414     SELECT      nvl(ap_accrual_account, -1)
2415     INTO        l_inv_ap_accrual_acct
2416     FROM        mtl_parameters
2417     WHERE       organization_id = i_org_id;
2418 
2419     IF l_pd_txfr_ind = 1
2420     THEN
2421       --
2422       -- Bug 5233635; Should not book InterOrg Profit when Intercompany Invoicing is enabled.
2423       --
2424       l_stmt_num := 11;
2425 
2426       SELECT to_number(org_information3)
2427       INTO   l_org_ou
2428       FROM   hr_organization_information
2429       WHERE  org_information_context = 'Accounting Information'
2430       AND    organization_id = i_txn_org_id;
2431 
2432       l_stmt_num := 12;
2433       SELECT to_number(org_information3)
2434       INTO   l_txf_org_ou
2435       FROM   hr_organization_information
2436       WHERE  org_information_context = 'Accounting Information'
2437       AND    organization_id = i_txf_org_id;
2438 
2439       l_io_invoicing := to_number(fnd_profile.value('INV_INTERCOMPANY_INVOICE_INTERNAL_ORDER'));
2440       fnd_file.put_line(fnd_file.LOG, 'SrcTypeID/ActID = ' || i_src_type_id ||'/'|| i_txn_act_id ||
2441         ' OU/xferOU: ' || l_org_ou ||'/'|| l_txf_org_ou ||
2442         ' Internal Order invoicing: ' || l_io_invoicing);
2443 
2444     END IF;
2445     --
2446     -- End Bug 5233635
2447     --
2448   -- First figure out expense item flags
2449   l_stmt_num := 5;
2450   if (i_exp_item = 1) then
2451     select nvl(expense_account, -1)
2452     into l_exp_acct
2453     from mtl_system_items
2454     where inventory_item_id = i_item_id
2455     and organization_id = i_org_id;
2456   end if;
2457 
2458   if(wms_install.check_install(l_return_status, l_msg_count, l_msg_data,i_org_id)) then
2459      l_wms_flg := 1;
2460   else
2461      l_wms_flg := 0;
2462   end if;
2463 
2464 
2465   -- Figure out sending and receiving expense subs.
2466   l_stmt_num := 10;
2467 
2468   if (i_org_id = i_txn_org_id) then
2469     --
2470     -- umoogala  Bug 4864821  INVCONV: process/discrete xfer enh.
2471     -- Added the following IF condition since for logical intransit receipt (action_id 15)
2472     -- SubInv could be null. In this case we'll consider this as asset sub.
2473     --
2474     if l_subinv is null
2475     then
2476       l_exp_sub := 0;
2477 
2478     else
2479       select decode(asset_inventory,1,0,1)
2480       into l_exp_sub
2481       from mtl_secondary_inventories
2482       where secondary_inventory_name = l_subinv
2483       and organization_id = i_org_id;
2484 
2485       if (l_wms_flg = 0) then
2486          select nvl(expense_account, l_exp_acct)
2487          into l_exp_acct
2488          from mtl_secondary_inventories
2489          where secondary_inventory_name = l_subinv
2490          and organization_id = i_org_id;
2491       else
2492          if (i_cost_grp_id = 1) then
2493            /* Need to change cst_avg_dist_accts_v to include expense_account */
2494            select nvl(expense_account,l_exp_acct)
2495            into l_exp_acct
2496            from mtl_parameters
2497            where  organization_id = i_org_id;
2498          else
2499            select nvl(expense_account, l_exp_acct)
2500            into l_exp_acct
2501            from cst_cost_group_accounts
2502            where cost_group_id = i_cost_grp_id
2503            and organization_id = i_org_id;
2504          end if;
2505       end if;
2506     end if;
2507   end if;
2508 
2509   /********************************************************************
2510    ** Figure out some local variables:  			     **
2511    ** 1) l_snd_rcv flag to determine if this is a sending or         **
2512    **    receiving transaction.					     **
2513    ** 2) l_from_org and l_to_org 				     **
2514    ** 3) l_snd_qty and l_rcv_qty				     **
2515    ** 4) set of books id and currency for sending and receiving org  **
2516    ** 5) interorg accounts					     **
2517    ** 6) Figure out if stardard org distribution is requried and if  **
2518    **    so, get stardard org accts.                                 **
2519    ********************************************************************/
2520 
2521   --
2522   -- Figure out the send/receiving flag. 1 is send and 2 is recv.
2523   -- OPM INVCONV umoogala: Added 15 (Logical Itr Receipt) and 22 (Logical Itr Shipment) actions.
2524   --
2525   if ((i_txn_act_id = 3 and i_txn_qty <0) or
2526       (i_txn_act_id = 21 and i_org_id = i_txn_org_id) or
2527       (i_txn_act_id = 12 and i_org_id = i_txf_org_id) or
2528       (i_txn_act_id = 22 and i_org_id = i_txn_org_id)) then  -- OPM INVCONV umoogala
2529     l_snd_rcv := 1;
2530   elsif ((i_txn_act_id = 3 and i_txn_qty > 0) or
2531          (i_txn_act_id = 21 and i_org_id = i_txf_org_id) or
2532          (i_txn_act_id = 12 and i_org_id = i_txn_org_id) or
2533          (i_txn_act_id = 15 and i_org_id = i_txn_org_id)) then  -- OPM INVCONV umoogala
2534     l_snd_rcv := 2;
2535   end if;
2536 
2537     fnd_file.put_line(fnd_file.log,'l_snd_rcv:' || l_snd_rcv);
2538 
2539 
2540   if (l_snd_rcv = 1) then
2541     if (i_org_id = i_txn_org_id or i_txn_act_id = 3) then
2542       l_from_org := i_txn_org_id;
2543       l_to_org := i_txf_org_id;
2544       l_from_cost_grp := i_cost_grp_id;
2545       l_to_cost_grp := i_txfr_cost_grp;
2546       --
2547       -- Start Bug 5702988
2548       -- This is Logical Intransit Shipment. Assume it as as asset sub.
2549       --
2550       if i_txn_act_id = 22
2551       then
2552         l_exp_sub := 0;
2553         l_subinv := NULL;
2554       end if;
2555       -- End Bug 5702988
2556     else
2557       l_from_org := i_txf_org_id;
2558       l_to_org := i_txn_org_id;
2559       l_from_cost_grp := i_txfr_cost_grp;
2560       l_to_cost_grp := i_cost_grp_id;
2561       -- if the header is not for my org, then don't know subinventory.
2562       -- Assume asset.
2563       l_exp_sub := 0;
2564       l_subinv := NULL;
2565     end if;
2566   elsif (l_snd_rcv = 2) then
2567     if (i_org_id = i_txn_org_id or i_txn_act_id = 3) then
2568       l_from_org := i_txf_org_id;
2569       l_to_org := i_txn_org_id;
2570       l_from_cost_grp := i_txfr_cost_grp;
2571       l_to_cost_grp := i_cost_grp_id;
2572       --
2573       -- Start Bug 5702988
2574       -- This is Logical Intransit Receipt. Assume it as as asset sub.
2575       --
2576       if i_txn_act_id = 15
2577       then
2578         l_exp_sub := 0;
2579         l_subinv := NULL;
2580       end if;
2581       -- End Bug 5702988
2582     else
2583       l_from_org := i_txn_org_id;
2584       l_to_org := i_txf_org_id;
2585       l_from_cost_grp := i_cost_grp_id;
2586       l_to_cost_grp := i_txfr_cost_grp;
2587       -- if the header is not for my org, then don't know subinventory.
2588       -- Assume asset.
2589       l_exp_sub := 0;
2590       l_subinv := NULL;
2591     end if;
2592   end if;
2593 
2594     fnd_file.put_line(fnd_file.log,'l_from_org:' || l_from_org);
2595     fnd_file.put_line(fnd_file.log,'l_to_org:' || l_to_org);
2596 
2597 
2598 
2599   CSTPAVCP.get_snd_rcv_rate(i_txn_id, l_from_org, l_to_org,
2600 		l_snd_sob_id, l_snd_curr, l_rcv_sob_id, l_rcv_curr,
2601 		l_curr_type, l_conv_rate, l_conv_date,
2602 		l_err_num, l_err_code, l_err_msg);
2603 
2604   if (l_err_num<>0) then
2605     raise process_error;
2606   end if;
2607 
2608   CSTPAVCP.get_snd_rcv_uom(i_item_id, l_from_org, l_to_org, l_snd_uom, l_rcv_uom,
2609 		  l_err_num, l_err_code, l_err_msg);
2610 
2611   if (l_from_org = i_txn_org_id) then
2612     l_snd_qty := i_txn_qty;
2613     l_rcv_qty := inv_convert.inv_um_convert
2614 				(i_item_id, NULL, -1 * l_snd_qty,
2615 			  	 l_snd_uom, l_rcv_uom, NULL, NULL);
2616   else
2617     l_rcv_qty :=i_txn_qty;
2618     l_snd_qty := inv_convert.inv_um_convert
2619 				(i_item_id, NULL, -1 * l_rcv_qty,
2620 			  	 l_rcv_uom, l_snd_uom, NULL, NULL);
2621   end if;
2622 
2623   if (l_err_num<>0) then
2624     raise process_error;
2625   end if;
2626 
2627 
2628   -- Figure out accts.
2629 
2630   l_stmt_num := 30;
2631 
2632   -- Modified for fob stamping project
2633 
2634   -- OPM INVCONV umoogala: added new column interorg_profit_account
2635   -- for process-discrete transfers
2636   select nvl(MMT.intransit_account, MIP.intransit_inv_account),
2637 	MIP.interorg_transfer_cr_account,
2638 	MIP.interorg_receivables_account,
2639 	MIP.interorg_payables_account,
2640 	MIP.interorg_price_var_account,
2641   MIP.interorg_profit_account  -- OPM INVCONV umoogala
2642   into
2643 	l_io_inv_acct,
2644 	l_io_txfr_acct,
2645 	l_io_rec_acct,
2646 	l_io_pay_acct,
2647 	l_io_ppv_acct,
2648   l_io_profit_acct  -- OPM INVCONV umoogala
2649   from mtl_interorg_parameters MIP, mtl_material_transactions MMT
2650   where MIP.from_organization_id = l_from_org
2651   and MIP.to_organization_id = l_to_org
2652   and MMT.transaction_id = i_txn_id;
2653 
2654   -- Standard org computation
2655   l_std_from_org := CSTPAVCP.standard_cost_org(l_from_org);
2656   l_std_to_org := CSTPAVCP.standard_cost_org(l_to_org);
2657 
2658   l_stmt_num := 40;
2659 
2660   /* Bug 2926258 - changed default to -1 to support org_id=0 */
2661   select decode(l_std_from_org, 1, l_from_org,
2662                 decode(l_std_to_org,1,l_to_org,-1))
2663   into l_std_org
2664   from dual;
2665 
2666   -- For direct interorg transfers, we only care about the transction org id.
2667   --
2668   -- OPM INVCONV umoogala: For process-discrete xfer, txn_org will always be
2669   -- standard costing org. Transfer to standard costing org will be processed
2670   -- by standard cost worker.
2671   --
2672   if ((i_txn_act_id = 3 and i_txn_org_id = i_org_id) OR
2673       (l_pd_txfr_ind = 1))  -- OPM INVCONV umoogala
2674   then
2675     l_std_org := -1;  /* Bug 2926258 */
2676   end if;
2677 
2678   /* Bug 2926258 - changed default to -1 to support org_id=0 */
2679   if (l_std_org <> -1) then
2680     l_stmt_num := 45;
2681     select decode(inventory_asset_flag, 'Y', 0,1), nvl(expense_account, -1)
2682     into l_std_exp_item, l_std_exp_acct
2683     from mtl_system_items
2684     where inventory_item_id = i_item_id
2685     and organization_id = l_std_org;
2686 
2687     -- if the standard org is the transaction org id then need to find out
2688     -- expense sub flag and inventory accounts.  Otherwise we use intransit
2689     -- accounts.
2690 
2691     if (l_std_org = i_txn_org_id) then
2692 
2693       if(wms_install.check_install(l_return_status, l_msg_count, l_msg_data,l_std_org)) then
2694           l_wms_flg := 1;
2695       else
2696           l_wms_flg := 0;
2697       end if;
2698 
2699       /* Changes for PJM for Standard costing */
2700       l_stmt_num := 47;
2701       SELECT decode(nvl(cost_group_accounting,0),0,0,
2702              decode(nvl(project_reference_enabled,0),1,1,0))
2703       INTO l_pjm_flg
2704       FROM MTL_PARAMETERS
2705       WHERE organization_id = l_std_org;
2706 
2707       l_stmt_num := 50;
2708       l_subinv :=  i_subinv;
2709 
2710       select decode(asset_inventory,1,0,1)
2711       into l_std_exp_sub
2712       from mtl_secondary_inventories msi
2713       where msi.organization_id = l_std_org
2714       and secondary_inventory_name = l_subinv;
2715 
2716       /* Changes for PJM for Standard Costing */
2717       if (l_wms_flg = 0 AND l_pjm_flg = 0) then
2718          select material_account, material_overhead_account, resource_account,
2719                 outside_processing_account, overhead_account,
2720                 nvl(expense_account, l_std_exp_acct)
2721          into   l_std_mat_acct, l_std_mat_ovhd_acct, l_std_res_acct,
2722                 l_std_osp_acct, l_std_ovhd_acct,
2723                 l_std_exp_acct
2724          from mtl_secondary_inventories msi
2725          where msi.organization_id = l_std_org
2726          and secondary_inventory_name = l_subinv;
2727       else
2728          select decode(l_std_org,l_from_org,l_from_cost_grp,l_to_cost_grp)
2729          into l_cg_id
2730          from dual;
2731 
2732          if (l_cg_id = 1) then
2733             select material_account, material_overhead_account, resource_account,
2734                  outside_processing_account, overhead_account,
2735                  nvl(expense_account, l_std_exp_acct)
2736             into l_std_mat_acct, l_std_mat_ovhd_acct, l_std_res_acct,
2737                  l_std_osp_acct, l_std_ovhd_acct,
2738                  l_std_exp_acct
2739             from   mtl_parameters
2740             where  organization_id = l_std_org;
2741          else
2742             select material_account, material_overhead_account, resource_account,
2743                 outside_processing_account, overhead_account,
2744                 nvl(expense_account, l_std_exp_acct)
2745             into   l_std_mat_acct, l_std_mat_ovhd_acct, l_std_res_acct,
2746                 l_std_osp_acct, l_std_ovhd_acct,
2747                 l_std_exp_acct
2748             from cst_cost_group_accounts
2749             where organization_id = l_std_org
2750             and cost_group_id = l_cg_id;
2751          end if;
2752       end if;
2753     end if;
2754   end if;
2755 
2756   /*******************************************************************
2757    ** Do sending org distribution 				    **
2758    *******************************************************************/
2759      fnd_file.put_line(fnd_file.log,'Sending org distributions');
2760 
2761   if (l_from_org = i_org_id) and (i_txn_act_id = 22)  -- OPM INVCONV  umoogala
2762   then
2763     /* INVCONV Bug#5476804 ANTHIYAG 21-Aug-2006 Start */
2764     /***********************************************************************************************
2765     * When the Intercompany Invoicing is disabled for a Expense Item, the Receivables and          *
2766     * Inter-Org profit accounts need to be booked, Otherwise if Intercompany Invoicing is enabled  *
2767     * for a Expense Item, No need for creating any bookings at all                                 *
2768     ***********************************************************************************************/
2769     IF  i_exp_item = 1 AND l_pd_txfr_ind = 1 AND nvl(l_io_invoicing,2) = 1 AND i_src_type_id = 8 AND l_org_ou <> l_txf_org_ou THEN /* INVCONV Bug#5498133/5498041 ANTHIYAG 30-Aug-2006 */
2770       fnd_file.put_line(fnd_file.log, 'Process/Discrete Xfer: No bookings will be created, since this is an Expense item and Invoicing is enabled');
2771       RETURN;
2772     END IF;
2773     /* INVCONV Bug#5476804 ANTHIYAG 21-Aug-2006 End */
2774      --
2775      -- OPM INVCONV umooogala
2776      -- Logical Intransit Shipment: new txn type introduced for process-discrete
2777      -- transfers enh to book intransit entries.
2778      --
2779     /* Book Intransit, Inter org receivables,freight expense,Transfer variance */
2780 
2781     /* Credit Freight Expense */
2782     if (i_trp_cost <> 0)
2783     then
2784 	  -- shipping txn, trp_cost
2785 	  l_value := i_trp_cost;
2786 
2787 	l_acct := CSTPACHK.get_account_id(l_from_org, i_txn_id, -1, 12,
2788 			    NULL, NULL, l_subinv,0, l_snd_rcv,
2789 			    l_err_num, l_err_code, l_err_msg);
2790 
2791 	if (l_err_num <> 0) then
2792 	  raise process_error;
2793 	end if;
2794 
2795 	if (l_acct = -1) then
2796 	  l_acct := i_trp_acct;
2797 	end if;
2798 
2799 	fnd_file.put_line(fnd_file.log,'Freight account: ' || l_value);
2800 
2801 	insert_account(l_from_org, i_txn_id, i_item_id, -1*l_value,
2802 		      l_snd_qty, l_acct, l_snd_sob_id, 12, NULL, NULL,
2803 		      i_txn_date, i_txn_src_id, i_src_type_id,
2804 		      l_snd_curr, NULL, NULL, NULL, NULL,1,
2805 		      i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
2806 		      l_err_num, l_err_code, l_err_msg);
2807 
2808 	if (l_err_num <> 0) then
2809 	  raise process_error;
2810 	end if;
2811     end if;
2812 
2813     /* Debit sending org Receivables. */
2814     l_stmt_num := 60;
2815 
2816     --
2817     -- Bug 5346202: InterOrg Receivables at Transfer Price only.
2818     -- Freight should not be added.
2819     -- l_value := i_trp_cost + (i_txf_price * l_snd_qty);
2820     --
2821     l_value := i_txf_price * l_snd_qty;
2822 
2823     fnd_file.put_line(fnd_file.log,'Receivables amount: ' || l_value);
2824 
2825     l_acct := CSTPACHK.get_account_id(l_from_org, i_txn_id, 1, 10,
2826                         NULL, NULL, l_subinv,0, l_snd_rcv,
2827                         l_err_num, l_err_code, l_err_msg);
2828 
2829     if (l_err_num <> 0) then
2830       raise process_error;
2831     end if;
2832 
2833     if (l_acct = -1) then
2834       l_acct := l_io_rec_acct;
2835     end if;
2836 
2837     /*  use -1*l_snd_qty */
2838     insert_account(l_from_org, i_txn_id, i_item_id,  l_value,
2839                   -1 * l_snd_qty, l_acct, l_snd_sob_id, 10, NULL, NULL,
2840                   i_txn_date, i_txn_src_id, i_src_type_id,
2841                   l_snd_curr, NULL, NULL, NULL, NULL,1,
2842                   i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
2843                   l_err_num, l_err_code, l_err_msg);
2844 
2845     if (l_err_num <> 0) then
2846       raise process_error;
2847     end if;
2848 
2849     /* Book intransit accounts */
2850     --
2851     -- Bug 5332813: Process/Discrete Xfer
2852     -- Do not post zero entry for expense item.
2853     --
2854     if i_exp_item = 0
2855     then
2856 
2857       fnd_file.put_line(fnd_file.log,'Intransit account: with item cost' );
2858 
2859       /*
2860        --
2861        -- Bug 4900652
2862        -- Book intransit with item cost
2863        --
2864       distribute_accounts(l_from_org,i_txn_id,0,i_cost_grp_id,i_txn_act_id,
2865                           i_item_id, l_snd_qty, 14,
2866                           1, 0, l_io_inv_acct, NULL, NULL, NULL, NULL,NULL,
2867                           l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
2868                           l_subinv, l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
2869                           i_user_id, i_login_id, i_prg_appl_id, i_prg_id, i_req_id,
2870                           l_err_num, l_err_code, l_err_msg);
2871       */
2872 
2873       /* Credit Sending Org' intransit. */
2874       l_intransit := 1;
2875       inventory_accounts(l_from_org, i_txn_id, 2, i_txn_act_id, i_cost_grp_id,
2876           	       i_item_id, -1 * l_snd_qty,
2877           	       l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
2878           	       i_exp_item, l_exp_sub, l_exp_acct, l_subinv, l_intransit,
2879           	       l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
2880           	       i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
2881           	       l_err_num, l_err_code, l_err_msg);
2882 
2883       if (l_err_num <> 0) then
2884         raise process_error;
2885       end if;
2886 
2887     end if;
2888 
2889     --
2890     -- Bug 5233635: Should not book InterOrg Profit If Intercompany Invoicing is enabled.
2891     -- Following flag is set at the beginning of the procedure.
2892     -- Bug 5345102: incorrect variables i_txn_src_id was used instead of using i_src_type_id
2893     --
2894     if  (i_src_type_id = 8)
2895     and (l_org_ou <> l_txf_org_ou)
2896     and (l_io_invoicing = 1)
2897     and (i_txn_act_id = 21) -- Bug 5351724
2898     and (i_exp_item = 1)    -- Bug 5348953: No IC Invoicing for expense items
2899     then
2900       NULL;
2901       fnd_file.put_line(fnd_file.log, 'No need to book InterOrg Profit since InterCompany Invoicing ' ||
2902         'is enabled.');
2903     else
2904       /* Book InterOrg profit  */
2905       SELECT SUM(NVL(base_transaction_value,0))
2906        INTO l_value
2907        FROM mtl_transaction_accounts
2908       WHERE transaction_id       = i_txn_id
2909         AND organization_id      = l_from_org
2910       ;
2911 
2912        fnd_file.put_line(fnd_file.log,'InterOrg Profit amount: ' || l_value);
2913 
2914       l_acct := CSTPACHK.get_account_id(l_from_org, i_txn_id, 1, 34,
2915                            NULL, NULL, l_subinv,0, l_snd_rcv,
2916                            l_err_num, l_err_code, l_err_msg);
2917 
2918       if (l_err_num <> 0) then
2919         raise process_error;
2920       end if;
2921 
2922       if (l_acct = -1) then
2923         l_acct := l_io_profit_acct;
2924       end if;
2925 
2926 
2927       insert_account(l_from_org, i_txn_id, i_item_id, -1 * l_value,
2928                      l_snd_qty, l_acct, l_snd_sob_id,
2929                      G_INTERORG_PROFIT_ACCT, NULL, NULL,
2930                      i_txn_date, i_txn_src_id, i_src_type_id,
2931                      l_snd_curr, NULL, NULL, NULL, NULL,1,
2932                      i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
2933                      l_err_num, l_err_code, l_err_msg);
2934 
2935       if (l_err_num <> 0) then
2936         raise process_error;
2937       end if;
2938 
2939     end if;
2940 
2941   /* END OPM INVCONV sschinch/umoogala */
2942 
2943   elsif ((l_from_org = i_org_id and
2944       (i_txn_act_id <> 3 or (i_txn_act_id = 3 and l_from_org = i_txn_org_id))) or
2945       (l_from_org = l_std_org)) then
2946 
2947     /* sending org distribution */
2948     -- Set intransit flag. Intransit account is used when FOB is receipt.
2949     -- for bug 774662, if txn is direct interorg transfer don't set l_intransit
2950     --
2951     -- OPM INVCONV umoogala/sschinch
2952     if (l_pd_txfr_ind = 0)
2953     then
2954       -- NOT a process-discrete xfer
2955       if (i_fob_point = 2) and (i_txn_act_id <>3) then
2956         l_intransit := 1;
2957       else
2958         l_intransit := 0;
2959       end if;
2960     else
2961       --
2962       -- OPM INVCONV umoogala/sschinch  process-discrete xfer
2963       -- Intransit entries are booked by new logical intranshit shipment (22)
2964       -- logical intransit receipt (15)
2965       -- On the process org side distributions done using SLA.
2966       --
2967       l_intransit := 0;
2968     end if;
2969 
2970     -- First do the distributions for ownership change
2971     if ((i_txn_act_id = 3 and i_txn_qty < 0) or
2972 	(i_txn_act_id = 21 and i_fob_point = 1) or
2973 	(i_txn_act_id = 12 and i_fob_point = 2)) then
2974       if ((l_from_org = i_org_id) and (i_exp_item = 0)) then /* Avg Costing */
2975         /* Credit inventory at sending org's cost */
2976         inventory_accounts(l_from_org, i_txn_id,2,i_txn_act_id,l_from_cost_grp,
2977 			i_item_id, l_snd_qty,
2978 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
2979 			i_exp_item, l_exp_sub, l_exp_acct, l_subinv, l_intransit,
2980 			l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
2981 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
2982 			l_err_num, l_err_code, l_err_msg);
2983 
2984         if (l_err_num <> 0) then
2985           raise process_error;
2986         end if;
2987 
2988      elsif ((l_from_org = i_org_id) and (i_exp_item = 1)) then /* Avg Costing for expense item*/
2989        /* Credit sending organization's expense account */
2990 
2991        --
2992        -- Bug 5348953: Process/Discrete Xfers
2993        -- For these transfers, do not book to expense account.
2994        -- Receivalbes and InterOrg Profit Accounts will get booked
2995        --
2996        if l_pd_txfr_ind = 1
2997        then
2998         /* INVCONV Bug#5465239 ANTHIYAG 18-Aug-2006 Start */
2999         /***********************************************************************************************
3000         * When the Intercompany Invoicing is disabled for a Expense Item, the Receivables and          *
3001         * Inter-Org profit accounts need to be booked, Otherwise if Intercompany Invoicing is enabled  *
3002         * for a Expense Item, No need for creating any bookings at all                                 *
3003         ***********************************************************************************************/
3004 
3005          IF nvl(l_io_invoicing,2) = 1 AND i_src_type_id IN (7, 8) AND l_org_ou <> l_txf_org_ou THEN /* INVCONV Bug#5498133/5498041 ANTHIYAG 30-Aug-2006 */
3006            fnd_file.put_line(fnd_file.log, 'Process/Discrete Xfer: No bookings will be created, since this is a Expense item and Invoicing is enabled');
3007            RETURN;
3008          ELSE
3009 	         fnd_file.put_line(fnd_file.log, 'Process/Discrete Xfer: Not booking to expense. Receivables and InterOrg Profit Accounts will get booked');
3010          END IF;
3011          /* INVCONV Bug#5465239 ANTHIYAG 18-Aug-2006 End */
3012        else
3013          distribute_accounts(l_from_org, i_txn_id,
3014                      0,i_cost_grp_id,i_txn_act_id,
3015                      i_item_id, l_snd_qty, 2,
3016                      0, 0, l_exp_acct, NULL, NULL, NULL, NULL,NULL,
3017                      l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3018                      l_subinv, l_snd_rcv, l_snd_curr,
3019                      NULL, NULL, NULL, NULL,
3020                      i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3021                      l_err_num, l_err_code, l_err_msg);
3022 
3023          if (l_err_num <> 0) then
3024            raise process_error;
3025          end if;
3026        end if;
3027 
3028      elsif (l_std_exp_item = 0) then /* Std Costing Org */
3029         if (l_intransit = 1) then
3030           distribute_accounts(l_from_org,i_txn_id,0,i_cost_grp_id,i_txn_act_id,
3031 			i_item_id, l_snd_qty, 14,
3032 			0, 0, l_io_inv_acct, NULL, NULL, NULL, NULL,NULL,
3033 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3034 			l_subinv, l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
3035 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3036 			l_err_num, l_err_code, l_err_msg);
3037 
3038           if (l_err_num <> 0) then
3039             raise process_error;
3040           end if;
3041 
3042 	else
3043           if (l_std_exp_sub = 1) then
3044           distribute_accounts(l_from_org,i_txn_id,0,i_cost_grp_id,i_txn_act_id,
3045 		        i_item_id, l_snd_qty, 2,
3046 			0, 0, l_std_exp_acct, NULL, NULL, NULL, NULL,NULL,
3047 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3048 			l_subinv, l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
3049 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3050 			l_err_num, l_err_code, l_err_msg);
3051 
3052             if (l_err_num <> 0) then
3053               raise process_error;
3054             end if;
3055 
3056 	  else
3057             distribute_accounts(l_from_org, i_txn_id,
3058 			0,i_cost_grp_id,i_txn_act_id,
3059 		        i_item_id, l_snd_qty, 1,
3060 			1, 0, NULL, l_std_mat_acct, l_std_mat_ovhd_acct,
3061 			l_std_res_acct, l_std_osp_acct, l_std_ovhd_acct,
3062 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3063 			l_subinv, l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
3064 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3065 			l_err_num, l_err_code, l_err_msg);
3066 
3067             if (l_err_num <> 0) then
3068               raise process_error;
3069             end if;
3070 
3071 	  end if;
3072 	end if;
3073       end if;
3074 
3075 --check txf cost< 0
3076       if (i_txf_cost <> 0) then
3077         --l_value := i_txf_cost; /* Comment out for bug 2827548 and add the following */
3078         if (i_txn_act_id = 12 and i_fob_point = 2) then
3079           -- receipt txn, txf_cost is in rec org's currency so need to convert back
3080           l_value := i_txf_cost / l_conv_rate;
3081         else
3082           l_value := i_txf_cost;
3083         end if;
3084 
3085         fnd_file.put_line(fnd_file.log,'Transfer Credit account: ' || l_value);
3086 
3087         /* Credit sending org transfer credit */
3088         l_acct := CSTPACHK.get_account_id(l_from_org, i_txn_id, -1, 11,
3089 					NULL, NULL, l_subinv,0, l_snd_rcv,
3090 					l_err_num, l_err_code, l_err_msg);
3091 
3092         if (l_err_num <> 0) then
3093           raise process_error;
3094         end if;
3095 
3096         if (l_acct = -1) then
3097 	  l_acct := l_io_txfr_acct;
3098         end if;
3099 
3100         insert_account(l_from_org, i_txn_id, i_item_id, -1 * l_value,
3101 			l_snd_qty, l_acct, l_snd_sob_id, 11, NULL, NULL,
3102 			i_txn_date, i_txn_src_id, i_src_type_id,
3103 			l_snd_curr, NULL, NULL, NULL, NULL,1,
3104 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3105 			l_err_num, l_err_code, l_err_msg);
3106 
3107         if (l_err_num <> 0) then
3108           raise process_error;
3109         end if;
3110 
3111       end if;
3112 
3113       --
3114       -- OPM INVCONV umoogala/sschinch Process-Discrete Xfer Enh.
3115       -- book friegt expense only for direct transfer in case
3116       -- of process discrete transfer. For other cases, it is done during intransit
3117       -- entries using new Logical Txns (action_id = 15, 22)
3118       --
3119       if ((i_trp_cost <> 0) and
3120           ((i_fob_point = 2 and l_pd_txfr_ind = 0) or  -- OPM INVCONV
3121            (i_txn_act_id = 3)
3122           ))
3123       then
3124         --l_value := i_trp_cost; /* Comment out for bug 2827548 and add the following */
3125         if (i_txn_act_id = 12 and i_fob_point = 2) then
3126           -- receipt txn, trp_cost is in rec org's currency so need to convert back
3127           l_value := i_trp_cost / l_conv_rate;
3128         else
3129           l_value := i_trp_cost;
3130         end if;
3131 
3132         fnd_file.put_line(fnd_file.log,'Freight account: ' || l_value);
3133 
3134         l_acct := CSTPACHK.get_account_id(l_from_org, i_txn_id, -1, 12,
3135   					NULL, NULL, l_subinv,0, l_snd_rcv,
3136 					l_err_num, l_err_code, l_err_msg);
3137 
3138         if (l_err_num <> 0) then
3139           raise process_error;
3140         end if;
3141 
3142         if (l_acct = -1) then
3143 	  l_acct := i_trp_acct;
3144         end if;
3145 
3146         insert_account(l_from_org, i_txn_id, i_item_id, -1 * l_value,
3147 			l_snd_qty, l_acct, l_snd_sob_id, 12, NULL, NULL,
3148 			i_txn_date, i_txn_src_id, i_src_type_id,
3149 			l_snd_curr, NULL, NULL, NULL, NULL,1,
3150 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3151 			l_err_num, l_err_code, l_err_msg);
3152 
3153         if (l_err_num <> 0) then
3154           raise process_error;
3155         end if;
3156 
3157       end if;
3158 
3159       /* Debit sending org Receivables. */
3160 
3161       --
3162       -- OPM INVCONV umoogala/sschinch  Process-Discrete Xfers Enh.
3163       -- For p-d xfers, book Inter org receivables (IOR) only in case of direct
3164       -- transfers and intransit shipment with fob shipping.
3165       --
3166       if ((i_txn_act_id = 3 and l_pd_txfr_ind = 1) or
3167           (i_txn_act_id = 21 and i_fob_point = 1 and l_pd_txfr_ind = 1) or
3168           (l_pd_txfr_ind = 0)) then
3169 
3170         l_stmt_num := 60;
3171 
3172         --
3173         -- OPM INVCONV umoogala/sschinch
3174         -- For process-discrete transfers book IOR with transfer_price
3175         -- which is stamped on mmt row
3176         --
3177         if (l_pd_txfr_ind = 0)
3178         then
3179           select nvl(sum(base_transaction_value),0)
3180           into l_value
3181           from mtl_transaction_accounts
3182           where transaction_id = i_txn_id
3183           and organization_id = l_from_org;
3184         else
3185           l_value := i_txf_price * l_snd_qty;  -- OPM INVCONV umoogala/sschinch
3186         end if;
3187 
3188         fnd_file.put_line(fnd_file.log,'receivables account: ' || l_value);
3189 
3190         l_acct := CSTPACHK.get_account_id(l_from_org, i_txn_id, 1, 10,
3191                                           NULL, NULL, l_subinv,0, l_snd_rcv,
3192                                           l_err_num, l_err_code, l_err_msg);
3193 
3194         if (l_err_num <> 0) then
3195           raise process_error;
3196         end if;
3197 
3198         if (l_acct = -1) then
3199           l_acct := l_io_rec_acct;
3200         end if;
3201 
3202         /* Bug 3169767: use -1*l_snd_qty */
3203         insert_account(l_from_org, i_txn_id, i_item_id, -1 * l_value,
3204                           -1 * l_snd_qty, l_acct, l_snd_sob_id, 10, NULL, NULL,
3205                           i_txn_date, i_txn_src_id, i_src_type_id,
3206                           l_snd_curr, NULL, NULL, NULL, NULL,1,
3207                           i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3208                           l_err_num, l_err_code, l_err_msg);
3209 
3210         if (l_err_num <> 0) then
3211           raise process_error;
3212         end if;
3213       end if; -- OPM INVCONV umoogala/sschinch
3214 
3215       --
3216       -- OPM INVCONV umoogala/sschinch
3217       -- Again for p-d xfers, book InterOrg profit for direct transfers and
3218       -- Intransit Shipment with FOB Shipping.
3219       -- InterOrg profit is a new account introduced for this project on
3220       -- shipping networks form under transfer price tab.
3221       --
3222       IF ((l_pd_txfr_ind = 1 and i_txn_act_id = 3) OR
3223           (l_pd_txfr_ind = 1 and i_txn_act_id = 21 and i_fob_point = 1))
3224       THEN
3225 
3226         --
3227         -- Bug 5233635: Should not book InterOrg Profit If Intercompany Invoicing is enabled.
3228 	-- Following flag is set at the beginning of the procedure.
3229         --
3230         IF  (i_src_type_id = 8)
3231 	AND (l_org_ou <> l_txf_org_ou)
3232         AND (l_io_invoicing = 1)
3233         AND (i_txn_act_id = 21) -- Bug 5351724
3234 	AND (i_exp_item = 0)    -- Bug 5348953: No IC Invoicing for expense items
3235         THEN
3236           NULL;
3237           fnd_file.put_line(fnd_file.log, 'No need to book InterOrg Profit since InterCompany Invoicing ' ||
3238             'is enabled.');
3239         ELSE
3240 
3241           /* Book interorg profit  */
3242           SELECT SUM(NVL(base_transaction_value,0))
3243             INTO l_value
3244             FROM mtl_transaction_accounts
3245            WHERE transaction_id       = i_txn_id
3246              AND organization_id      = l_from_org
3247           ;
3248 
3249           fnd_file.put_line(fnd_file.log,'InterOrg profit account: ' || l_value);
3250 
3251           l_acct := CSTPACHK.get_account_id(l_from_org, i_txn_id, 1,
3252                                             G_INTERORG_PROFIT_ACCT,
3253                                             NULL, NULL, l_subinv,0, l_snd_rcv,
3254                                             l_err_num, l_err_code, l_err_msg);
3255 
3256           if (l_err_num <> 0) then
3257             raise process_error;
3258           end if;
3259 
3260           if (l_acct = -1) then
3261             l_acct := l_io_profit_acct;
3262           end if;
3263 
3264           insert_account(l_from_org, i_txn_id, i_item_id, -1 * l_value,
3265                           -1 * l_snd_qty, l_acct, l_snd_sob_id,
3266                           G_INTERORG_PROFIT_ACCT, NULL, NULL,
3267                           i_txn_date, i_txn_src_id, i_src_type_id,
3268                           l_snd_curr, NULL, NULL, NULL, NULL,1,
3269                           i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3270                           l_err_num, l_err_code, l_err_msg);
3271 
3272           if (l_err_num <> 0) then
3273             raise process_error;
3274           end if;
3275         END IF;
3276 
3277       END IF;
3278       -- End OPM INVCONV umoogala
3279 
3280     elsif (i_txn_act_id = 21 and i_fob_point = 2) then
3281       /* Sending org, no ownership change. */
3282       if ((l_from_org = i_org_id) and (i_exp_item = 0)) then /* Average cost org */
3283         /* Credit inventory at sending org's cost */
3284         l_intransit := 0;
3285         inventory_accounts(l_from_org, i_txn_id,2,i_txn_act_id,l_from_cost_grp,
3286 			i_item_id, l_snd_qty,
3287 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3288 			i_exp_item, l_exp_sub, l_exp_acct, l_subinv, l_intransit,
3289 			l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
3290 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3291 			l_err_num, l_err_code, l_err_msg);
3292 
3293         if (l_err_num <> 0) then
3294           raise process_error;
3295         end if;
3296 
3297 
3298         /* Debit Sending Org' intransit. */
3299         l_intransit := 1;
3300         inventory_accounts(l_from_org, i_txn_id,2,
3301 		        i_txn_act_id,l_to_cost_grp,
3302 			i_item_id, -1 * l_snd_qty,
3303 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3304 			i_exp_item, l_exp_sub, l_exp_acct, l_subinv, l_intransit,
3305 			l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
3306 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3307 			l_err_num, l_err_code, l_err_msg);
3308 
3309         if (l_err_num <> 0) then
3310           raise process_error;
3311         end if;
3312 
3313       elsif ((l_std_exp_item = 0) and (i_exp_item = 0)) then  /* Standard cost org */
3314         /* Credit inventory at sending org's cost */
3315         if (l_std_exp_sub = 1) then
3316           distribute_accounts(l_from_org, i_txn_id,
3317 			0,i_cost_grp_id,i_txn_act_id,
3318 			i_item_id, l_snd_qty, 2,
3319 			0, 0, l_std_exp_acct, NULL, NULL, NULL, NULL,NULL,
3320 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3321 			l_subinv, l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
3322 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3323 			l_err_num, l_err_code, l_err_msg);
3324 
3325           if (l_err_num <> 0) then
3326             raise process_error;
3327           end if;
3328 
3329 	else
3330           distribute_accounts(l_from_org, i_txn_id,
3331 			0,i_cost_grp_id,i_txn_act_id,
3332 			i_item_id, l_snd_qty, 1,
3333 			1, 0, NULL, l_std_mat_acct, l_std_mat_ovhd_acct,
3334 			l_std_res_acct, l_std_osp_acct, l_std_ovhd_acct,
3335 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3336 			l_subinv, l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
3337 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3338 			l_err_num, l_err_code, l_err_msg);
3339 
3340           if (l_err_num <> 0) then
3341             raise process_error;
3342           end if;
3343 
3344 	end if;
3345 
3346         /* Debit Sending Org' intransit. */
3347         distribute_accounts(l_from_org, i_txn_id,
3348 			0,i_cost_grp_id,i_txn_act_id,
3349 			i_item_id, -1 * l_snd_qty, 14,
3350 			0, 0, l_io_inv_acct, NULL, NULL, NULL, NULL,NULL,
3351 			l_snd_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3352 			l_subinv, l_snd_rcv, l_snd_curr, NULL, NULL, NULL, NULL,
3353 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3354 			l_err_num, l_err_code, l_err_msg);
3355 
3356         if (l_err_num <> 0) then
3357           raise process_error;
3358         end if;
3359 
3360       end if;
3361     end if;
3362 
3363   end if;  -- Sending org distribution
3364 
3365   /*******************************************************************
3366    ** Do receiving org distribution 				    **
3367    *******************************************************************/
3368   fnd_file.put_line(fnd_file.log,'Receiving org distributions');
3369 
3370   /* Begin OPM INVCONV sschinch  Logical Intransit Receipt FOB Shipping*/
3371   if (i_txn_act_id = 15)
3372   then
3373      --
3374      -- OPM INVCONV umooogala
3375      -- Logical Intransit Receipt: new txn type introduced for process-discrete
3376      -- transfers enh to book intransit entries.
3377      --
3378 
3379     --
3380     -- Dr Intransit
3381     -- Amount should be (transfer_price + freight)
3382     -- actual_cost in mcacd is (transfer_price + freight)
3383     --
3384     fnd_file.put_line(fnd_file.log,'Dr intransit account @ org cost');
3385 
3386     /* Dr Receiving Org's intransit. */
3387     l_intransit := 1;
3388 
3389     /*
3390      * Bug 5400954: Intransit Account is getting Debited.
3391      * Fix: Replaced following call with inventory_accounts call.
3392     distribute_accounts(l_to_org, i_txn_id,
3393                         0,i_cost_grp_id,i_txn_act_id,
3394                         i_item_id, -1*l_rcv_qty, 14,
3395                         0, 0, l_io_inv_acct, NULL, NULL, NULL, NULL,NULL,
3396                         l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3397                         l_subinv, l_snd_rcv, l_rcv_curr,
3398                         null, null, null, null,
3399                         i_user_id, i_login_id, i_prg_appl_id, i_prg_id, i_req_id,
3400                         l_err_num, l_err_code, l_err_msg);
3401 
3402     */
3403     inventory_accounts(l_to_org, i_txn_id,2,i_txn_act_id,i_cost_grp_id,
3404                     i_item_id, l_rcv_qty,
3405                     l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3406                     i_exp_item, l_exp_sub, l_exp_acct, l_subinv, l_intransit,
3407                     l_snd_rcv, l_rcv_curr, l_snd_curr,                          /* INVCONV Bug#5462346/5462244 ANTHIYAG 22-Aug-2006 */
3408                     l_conv_date, l_conv_rate, l_curr_type,                      /* INVCONV Bug#5462346/5462244 ANTHIYAG 22-Aug-2006 */
3409                     i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3410                     l_err_num, l_err_code, l_err_msg);
3411 
3412     if (l_err_num <> 0) then
3413       raise process_error;
3414     end if;
3415 
3416     --
3417     -- Cr InterOrg Payables with transfer price.
3418     --
3419     l_value := i_txf_price * l_rcv_qty;
3420 
3421     fnd_file.put_line(fnd_file.log,'Cr payable. amount(l_value):' || -1 * l_value);
3422 
3423     /* INVCONV Bug#5471471 ANTHIYAG 18-Aug-2006 Start */
3424     /************************************************************************************
3425     * If Intercompany invoicing is enabled, then the booking should be made against the *
3426     * Inventory AP Accrual account rather than the Inter-Org Payables Account           *
3427     ************************************************************************************/
3428 
3429     IF nvl(l_io_invoicing, 2) =  1 AND i_src_type_id = 7 AND l_org_ou <> l_txf_org_ou THEN /* INVCONV Bug#5498133/5498041 ANTHIYAG 30-Aug-2006 */
3430       l_acct := CSTPACHK.get_account_id(l_to_org, i_txn_id, -1, 2,
3431                           NULL, NULL, l_subinv,0, l_snd_rcv,
3432                           l_err_num, l_err_code, l_err_msg);
3433 
3434       if (l_err_num <> 0) then
3435         raise process_error;
3436       end if;
3437 
3438       if (l_acct = -1) then
3439         l_acct := l_inv_ap_accrual_acct;
3440       end if;
3441 
3442       insert_account(l_to_org, i_txn_id, i_item_id, -1 * l_value,
3443                     -1 * l_rcv_qty, l_acct, l_rcv_sob_id, 2, NULL, NULL,
3444                     i_txn_date, i_txn_src_id, i_src_type_id,
3445                     l_rcv_curr, l_snd_curr,
3446                     l_conv_date, l_conv_rate, l_curr_type,1,
3447                     i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3448                     l_err_num, l_err_code, l_err_msg);
3449 
3450       if (l_err_num <> 0) then
3451         raise process_error;
3452       end if;
3453 
3454     ELSE
3455       /* INVCONV Bug#5471471 ANTHIYAG 18-Aug-2006 End */
3456 
3457       l_acct := CSTPACHK.get_account_id(l_to_org, i_txn_id, -1, 9,
3458                           NULL, NULL, l_subinv,0, l_snd_rcv,
3459                           l_err_num, l_err_code, l_err_msg);
3460 
3461       if (l_err_num <> 0) then
3462         raise process_error;
3463       end if;
3464 
3465       if (l_acct = -1) then
3466         l_acct := l_io_pay_acct;
3467       end if;
3468 
3469       insert_account(l_to_org, i_txn_id, i_item_id, -1 * l_value,
3470                     -1 * l_rcv_qty, l_acct, l_rcv_sob_id, 9, NULL, NULL,
3471                     i_txn_date, i_txn_src_id, i_src_type_id,
3472                     l_rcv_curr, l_snd_curr,
3473                     l_conv_date, l_conv_rate, l_curr_type,1,
3474                     i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3475                     l_err_num, l_err_code, l_err_msg);
3476 
3477       if (l_err_num <> 0) then
3478         raise process_error;
3479       end if;
3480     END IF;
3481 
3482     --
3483     -- Cr Inter-org freight charge
3484     --
3485     if (i_trp_cost <> 0) then
3486 
3487       l_value := i_trp_cost;
3488 
3489       l_acct := CSTPACHK.get_account_id(l_to_org, i_txn_id, -1, 12,
3490                           NULL, NULL, l_subinv,0, l_snd_rcv,
3491                         l_err_num, l_err_code, l_err_msg);
3492 
3493       if (l_err_num <> 0) then
3494         raise process_error;
3495       end if;
3496 
3497       if (l_acct = -1) then
3498         l_acct := i_trp_acct;
3499       end if;
3500 
3501       fnd_file.put_line(fnd_file.log,'Cr Freight. amount(l_value):' || -1 * l_value);
3502       insert_account(l_to_org, i_txn_id, i_item_id, -1 * l_value,
3503                     l_rcv_qty, l_acct, l_rcv_sob_id, 12, NULL, NULL,
3504                     i_txn_date, i_txn_src_id, i_src_type_id,
3505                     l_rcv_curr, l_snd_curr,
3506                     l_conv_date, l_conv_rate, l_curr_type,1,
3507                     i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3508                     l_err_num, l_err_code, l_err_msg);
3509 
3510       if (l_err_num <> 0) then
3511         raise process_error;
3512       end if;
3513 
3514     end if;
3515 
3516 
3517     --
3518     -- Bug 5021305
3519     -- Overhead Absorption.
3520     --
3521     if (l_exp_sub = 0 and i_exp_item = 0)
3522     then
3523 
3524       if(l_earn_moh <>  0)
3525       then
3526         ovhd_accounts(l_to_org, i_txn_id, i_item_id, -1 * l_rcv_qty, l_rcv_sob_id,
3527                       i_txn_date, i_txn_src_id, i_src_type_id, l_subinv,
3528                       l_snd_rcv, l_rcv_curr,
3529                       l_snd_curr, l_conv_date, l_conv_rate, l_curr_type,
3530                       i_user_id, i_login_id, i_prg_appl_id, i_prg_id, i_req_id,
3531                       l_err_num, l_err_code, l_err_msg);
3532 
3533         if (l_err_num <> 0)
3534         then
3535           raise process_error;
3536         end if;
3537 
3538      end if;
3539     end if;
3540     --
3541     -- End Bug 5021305
3542     -- Overhead Absorption.
3543 /*
3544     -- ??? do we need all this? I think this is only for std orgs, right?
3545     select nvl(sum(mctcd.transaction_cost),0)
3546       into l_ppv
3547       from mtl_cst_txn_cost_details mctcd
3548      where mctcd.transaction_id = i_txn_id
3549        and mctcd.organization_id = l_std_org
3550     ;
3551 
3552     fnd_file.put_line(fnd_file.log,'ppv:' || l_ppv);
3553 
3554     l_stmt_num := 90;
3555 
3556     -- If MOH Absorption is overridden, the this level moh costs have to be driven to ppv.
3557     -- Also modified code so only this level moh is absorbed. bug 2277950
3558 
3559     -- Changes for MOH Absorption Rules engine
3560     cst_mohRules_pub.apply_moh(
3561                             1.0,
3562                             p_organization_id => l_std_org,
3563                             p_earn_moh =>l_earn_moh,
3564                             p_txn_id => i_txn_id,
3565                             p_item_id => i_item_id,
3566                             x_return_status => l_return_status,
3567                             x_msg_count => l_msg_count,
3568                             x_msg_data => l_msg_data);
3569 
3570     IF l_return_status <> FND_API.g_ret_sts_success
3571     THEN
3572        CST_UTILITY_PUB.writelogmessages
3573                         ( p_api_version   => 1.0,
3574                           p_msg_count     => l_msg_count,
3575                           p_msg_data      => l_msg_data,
3576                           x_return_status => l_return_status);
3577        RAISE moh_rules_error;
3578     END IF;
3579 
3580     l_stmt_num := 95;
3581 
3582     IF (l_earn_moh = 0)
3583     THEN
3584       select l_ppv -  nvl(sum(mcacd.actual_cost),0)
3585       into l_ppv
3586       from mtl_cst_actual_cost_details mcacd
3587       where mcacd.transaction_id = i_txn_id
3588       and mcacd.organization_id = l_std_org;
3589 
3590     ELSE
3591       select l_ppv - nvl(sum(mcacd.actual_cost),0)
3592       into l_ppv
3593       from mtl_cst_actual_cost_details mcacd
3594       where mcacd.transaction_id = i_txn_id
3595       and mcacd.organization_id = l_std_org
3596       and not (cost_element_id =  2
3597       and level_type = 1);
3598       -- Earn only this level MOH
3599     END IF;
3600 
3601     fnd_file.put_line(fnd_file.log,'ppv:' || l_ppv);
3602 
3603 
3604     -- Changes for PJM for Standard Costing
3605     l_stmt_num := 95;
3606     update mtl_material_transactions
3607        set variance_amount = l_ppv * abs(l_rcv_qty)
3608      where transaction_id = i_txn_id
3609        and organization_id = l_std_org;
3610 
3611     if (l_ppv <> 0)
3612     then
3613       l_acct := CSTPACHK.get_account_id(l_to_org, i_txn_id, sign(l_ppv), 6,
3614                           NULL, NULL, l_subinv,0, l_snd_rcv,
3615                           l_err_num, l_err_code, l_err_msg);
3616 
3617       if (l_err_num <> 0) then
3618         raise process_error;
3619       end if;
3620 
3621       if (l_acct = -1) then
3622         l_acct := l_io_ppv_acct;
3623       end if;
3624 
3625       -- change l_from_org to l_std_org so PPV from std org will be used
3626       insert_account(l_std_org, i_txn_id, i_item_id,
3627                     l_ppv * abs(l_rcv_qty),
3628                     l_rcv_qty, l_acct, l_rcv_sob_id, 6, NULL, NULL,
3629                     i_txn_date, i_txn_src_id, i_src_type_id,
3630                     l_rcv_curr, l_snd_curr, l_conv_date, l_conv_rate,
3631                     l_curr_type,1,
3632                     i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3633                     l_err_num, l_err_code, l_err_msg);
3634 
3635       if (l_err_num <> 0) then
3636         raise process_error;
3637       end if;
3638 
3639     end if;
3640 
3641 */
3642     /* End OPM INVCONV umoogala/sschinch */
3643 
3644   elsif ((l_to_org = i_org_id) or (l_to_org = l_std_org))
3645   then
3646     -- Set intransit flag. Intransit account is used when FOB is shipment.
3647     -- for bug 774662, if it is direct interorg transfer, don't set intransit to 1
3648     if (i_fob_point = 1) and (i_txn_act_id <> 3)then
3649       l_intransit := 1;
3650     else
3651       l_intransit := 0;
3652     end if;
3653 
3654     fnd_file.put_line(fnd_file.log,'l_intransit:' || l_intransit);
3655 
3656 
3657     -- First do the distributions for ownership change
3658     if ((i_txn_act_id = 3 and i_txn_qty > 0) or
3659 	(i_txn_act_id = 21 and i_fob_point = 1) or
3660 	(i_txn_act_id = 12 and i_fob_point = 2)) then
3661       if (l_to_org = i_org_id) then /* Avg Costing */
3662         /* Debit inventory or intransit at receiving org's cost */
3663            fnd_file.put_line(fnd_file.log,'Calling Inventory acct');
3664 
3665         --
3666         -- OPM INVCONV umoogala
3667         -- Added if/else block to process process-discrete transfers
3668         --
3669         if (l_pd_txfr_ind = 0)
3670         then
3671           inventory_accounts(l_to_org, i_txn_id,2,i_txn_act_id, l_to_cost_grp,
3672                           i_item_id, l_rcv_qty,
3673                           l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3674                           i_exp_item, l_exp_sub, l_exp_acct, l_subinv, l_intransit,
3675                           l_snd_rcv, l_rcv_curr, l_snd_curr,
3676                           l_conv_date, l_conv_rate, l_curr_type,
3677                           i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3678                           l_err_num, l_err_code, l_err_msg);
3679           if (l_err_num <> 0) then
3680             raise process_error;
3681           end if;
3682 
3683         else
3684           --
3685           -- OPM INVCONV umoogala
3686           -- This is a process discrete transfer.
3687           -- ** For Direct and Intransit Rct with FOB Receipt book
3688           --    Inv. Val and IOP accounts with transfer price only.
3689           -- ** For Intransit Rct with FOB Shipping book
3690           --    Inv. Val and Intransit accounts with (transfer price + freight)
3691           --
3692           if (i_txn_act_id = 3 and i_txn_qty > 0) or
3693              (i_txn_act_id = 12 and i_fob_point = 2)
3694           then
3695             l_value := l_rcv_qty * i_txf_price;
3696 
3697             if (l_to_cost_grp = 1) then
3698               select nvl(material_account,-1), nvl(material_overhead_account,-1)
3699               into   l_acct, l_moh_offset_acct
3700               from   mtl_parameters
3701               where  organization_id = i_org_id;
3702             else
3703               select nvl(material_account,-1), nvl(material_overhead_account,-1)
3704               into   l_acct, l_moh_offset_acct
3705               from   cst_cost_group_accounts
3706               where  cost_group_id = l_to_cost_grp
3707               and    organization_id = i_org_id;
3708             end if;
3709 
3710             --
3711             -- Bug 5453173/5453410/5450819/5450615:
3712             --
3713             select decode(i_exp_item, 0, nvl(l_exp_sub,0), i_exp_item)
3714             into l_exp
3715             from dual;
3716 
3717             if (l_intransit = 1 and i_exp_item = 0) then
3718               l_acct_line_type := 14;
3719             elsif (l_exp = 1) then
3720               l_acct_line_type := 2;
3721 	      l_acct := l_exp_acct;
3722             else
3723               l_acct_line_type := 1;
3724             end if;
3725 
3726             if (l_debug = 'Y') then
3727             fnd_file.put_line(fnd_file.log, 'l_acct_line_type:' || l_acct_line_type);
3728             end if;
3729 
3730 
3731             --
3732             -- Bug 5391121: incorrect sign for qty
3733             -- was: -1 * l_rcv_qty, now: l_rcv_qty
3734             --
3735            fnd_file.put_line(fnd_file.log,'calling insert_account for booking INV with value/qty: ' || l_value ||'/'||l_rcv_qty);
3736             insert_account(l_to_org, i_txn_id, i_item_id, l_value,
3737                               l_rcv_qty, l_acct, l_rcv_sob_id, l_acct_line_type, NULL, NULL,
3738                               i_txn_date, i_txn_src_id, i_src_type_id,
3739                               l_rcv_curr, l_snd_curr,
3740                               l_conv_date, l_conv_rate, l_curr_type,1,
3741                               i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3742                               l_err_num, l_err_code, l_err_msg);
3743 
3744             if (l_err_num <> 0) then
3745               raise process_error;
3746             end if;
3747 
3748           end if;
3749         end if; -- OPM INVCONV umoogala
3750 
3751       else /* Std Costing Org */
3752            fnd_file.put_line(fnd_file.log,'Calling distribute accts');
3753 
3754 	if (l_intransit = 1 and l_std_exp_item = 0) then
3755           distribute_accounts(l_to_org, i_txn_id,
3756 			0,i_cost_grp_id,i_txn_act_id,
3757 			i_item_id, l_rcv_qty, 14,
3758 			0, 0, l_io_inv_acct, NULL, NULL, NULL, NULL,NULL,
3759 			l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3760 			l_subinv, l_snd_rcv, l_rcv_curr,
3761 			l_snd_curr, l_conv_date, l_conv_rate, l_curr_type,
3762 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3763 			l_err_num, l_err_code, l_err_msg);
3764           if (l_err_num <> 0) then
3765            raise process_error;
3766           end if;
3767 
3768 	else
3769            fnd_file.put_line(fnd_file.log,'Calling distribute accts(2)');
3770 
3771           if (l_std_exp_item = 1 or l_std_exp_sub = 1) then
3772             distribute_accounts(l_to_org, i_txn_id,
3773 			0,i_cost_grp_id,i_txn_act_id,
3774 			i_item_id, l_rcv_qty, 2,
3775 			0, 0, l_std_exp_acct, NULL, NULL, NULL, NULL,NULL,
3776 			l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3777 			l_subinv, l_snd_rcv, l_rcv_curr,
3778 			l_snd_curr, l_conv_date, l_conv_rate, l_curr_type,
3779 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3780 			l_err_num, l_err_code, l_err_msg);
3781             if (l_err_num <> 0) then
3782               raise process_error;
3783             end if;
3784 
3785           else
3786                fnd_file.put_line(fnd_file.log,'Calling distribute accts(3)');
3787 
3788             distribute_accounts(l_to_org, i_txn_id,
3789 				0,i_cost_grp_id,i_txn_act_id,
3790 				i_item_id, l_rcv_qty, 1,
3791 			1, 0, NULL, l_std_mat_acct, l_std_mat_ovhd_acct,
3792 			l_std_res_acct, l_std_osp_acct, l_std_ovhd_acct,
3793 			l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
3794 			l_subinv, l_snd_rcv, l_rcv_curr,
3795 			l_snd_curr, l_conv_date, l_conv_rate, l_curr_type,
3796 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3797 			l_err_num, l_err_code, l_err_msg);
3798             if (l_err_num <> 0) then
3799              raise process_error;
3800             end if;
3801 
3802           end if;
3803         end if;
3804 
3805         if (l_std_exp_sub <> 1 and  l_std_exp_item <> 1) then
3806                 /* bug 2469829 ppv should be calculated only for asset items
3807                    into asset subinventories */
3808 		/* Not expense so PPV exists. */
3809           -- PPV equal to total transaction_cost - total actual cost excluding
3810           -- material overhead.
3811 
3812 	  l_stmt_num:=80;
3813 
3814           select nvl(sum(mctcd.transaction_cost),0)
3815           into l_ppv
3816 	  from mtl_cst_txn_cost_details mctcd
3817           where mctcd.transaction_id = i_txn_id
3818           and mctcd.organization_id = l_std_org;
3819 
3820            fnd_file.put_line(fnd_file.log,'ppv:' || l_ppv);
3821 
3822 	  l_stmt_num := 90;
3823 
3824         /* If MOH Absorption is overridden, the this level moh costs
3825            have to be driven to ppv.*/
3826         /* Also modified code so only this level moh is absorbed. bug 2277950*/
3827 
3828          /* Changes for MOH Absorption Rules engine */
3829          cst_mohRules_pub.apply_moh(
3830                               1.0,
3831                               p_organization_id => l_std_org,
3832                               p_earn_moh =>l_earn_moh,
3833                               p_txn_id => i_txn_id,
3834                               p_item_id => i_item_id,
3835                               x_return_status => l_return_status,
3836                               x_msg_count => l_msg_count,
3837                               x_msg_data => l_msg_data);
3838 
3839          IF l_return_status <> FND_API.g_ret_sts_success THEN
3840 
3841          CST_UTILITY_PUB.writelogmessages
3842                           ( p_api_version   => 1.0,
3843                             p_msg_count     => l_msg_count,
3844                             p_msg_data      => l_msg_data,
3845                             x_return_status => l_return_status);
3846          RAISE moh_rules_error;
3847          END IF;
3848 
3849          l_stmt_num := 95;
3850 
3851          IF(l_earn_moh = 0) THEN
3852           select l_ppv -  nvl(sum(mcacd.actual_cost),0)
3853           into l_ppv
3854           from mtl_cst_actual_cost_details mcacd
3855           where mcacd.transaction_id = i_txn_id
3856           and mcacd.organization_id = l_std_org;
3857 
3858          else
3859 
3860            select l_ppv - nvl(sum(mcacd.actual_cost),0)
3861           into l_ppv
3862           from mtl_cst_actual_cost_details mcacd
3863           where mcacd.transaction_id = i_txn_id
3864           and mcacd.organization_id = l_std_org
3865           and not (cost_element_id =  2
3866           and level_type = 1);
3867           /* Earn only this level MOH */
3868          END IF;
3869 
3870            fnd_file.put_line(fnd_file.log,'ppv:' || l_ppv);
3871 
3872 
3873 	  /* Changes for PJM for Standard Costing */
3874 	  l_stmt_num := 95;
3875 	  update mtl_material_transactions
3876 		set variance_amount = l_ppv * abs(l_rcv_qty)
3877 		where transaction_id = i_txn_id
3878 		and organization_id = l_std_org;
3879 
3880           if (l_ppv <> 0) then
3881             l_acct := CSTPACHK.get_account_id(l_to_org, i_txn_id, sign(l_ppv), 6,
3882 					NULL, NULL, l_subinv,0, l_snd_rcv,
3883 					l_err_num, l_err_code, l_err_msg);
3884 
3885             if (l_err_num <> 0) then
3886               raise process_error;
3887             end if;
3888 
3889             if (l_acct = -1) then
3890 	      l_acct := l_io_ppv_acct;
3891             end if;
3892 
3893 /* FIX bug 668528 -> change l_from_org to l_std_org so PPV from std org will be used */
3894             insert_account(l_std_org, i_txn_id, i_item_id,
3895 			l_ppv * abs(l_rcv_qty),
3896 			l_rcv_qty, l_acct, l_rcv_sob_id, 6, NULL, NULL,
3897 			i_txn_date, i_txn_src_id, i_src_type_id,
3898 			l_rcv_curr, l_snd_curr, l_conv_date, l_conv_rate,
3899 			l_curr_type,1,
3900 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3901 			l_err_num, l_err_code, l_err_msg);
3902 
3903             if (l_err_num <> 0) then
3904               raise process_error;
3905             end if;
3906 
3907           end if;
3908         end if;
3909       end if;
3910 
3911       -- Overhead Absorption.
3912       if ((l_to_org = i_org_id and l_exp_sub = 0 and i_exp_item = 0) or
3913           (l_to_org = l_std_org and l_std_exp_sub <> 1 and l_std_exp_item <> 1)) then
3914 
3915        if(l_earn_moh <>  0) then
3916         ovhd_accounts(l_to_org, i_txn_id, i_item_id, -1 * l_rcv_qty, l_rcv_sob_id,
3917 		 	i_txn_date, i_txn_src_id, i_src_type_id, l_subinv,
3918 			l_snd_rcv, l_rcv_curr,
3919 			l_snd_curr, l_conv_date, l_conv_rate, l_curr_type,
3920 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3921 			l_err_num, l_err_code, l_err_msg);
3922         if (l_err_num <> 0) then
3923           raise process_error;
3924         end if;
3925 
3926 	--
3927 	-- Bug 5677953
3928 	-- FOr process/discete xfer, offset moh since we have booked
3929 	-- inventory valuation account was booked at transfer price.
3930 	--
3931 	if l_pd_txfr_ind = 1
3932 	then
3933 
3934           select nvl(sum(base_transaction_value), 0)
3935             into l_value
3936             from mtl_transaction_accounts
3937            where transaction_id = i_txn_id
3938              and organization_id = l_to_org
3939              and accounting_line_type = 3
3940              and cost_element_id = 2;
3941 
3942           if l_value <> 0
3943           then
3944             insert_account(l_to_org, i_txn_id, i_item_id, -1 * l_value,
3945                         l_rcv_qty, l_moh_offset_acct, l_rcv_sob_id, 1, NULL, NULL,
3946                         i_txn_date, i_txn_src_id, i_src_type_id,
3947                         l_rcv_curr, l_snd_curr,
3948                         l_conv_date, l_conv_rate, l_curr_type,1,
3949                         i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
3950                         l_err_num, l_err_code, l_err_msg);
3951 
3952             if (l_err_num <> 0) then
3953               raise process_error;
3954             end if;
3955 
3956           end if;
3957 	end if; /* if l_pd_txfr_ind = 1 */
3958 	-- End bug 5677953
3959 
3960        end if;
3961       end if;
3962 
3963       -- Credit Interorg Payables
3964 
3965       l_stmt_num := 100;
3966 
3967       --
3968       -- OPM INVCONV umoogala
3969       -- For process-discrete xfers, use transfer price.
3970       --
3971       if (l_pd_txfr_ind = 0)
3972       then
3973         select sum(base_transaction_value)
3974         into l_value
3975         from mtl_transaction_accounts
3976         where transaction_id = decode(i_txn_act_id, 3, i_txf_txn_id,i_txn_id)
3977         and organization_id = l_from_org
3978         and accounting_line_type = 10;
3979 
3980         l_value := l_value * l_conv_rate;
3981       elsif ((l_pd_txfr_ind = 1) and
3982              ((i_txn_act_id = 3  and i_txn_qty > 0) or
3983               (i_txn_act_id = 12 and i_fob_point = 2)))
3984       then
3985         -- process-discrete transfer
3986         l_value := l_rcv_qty * i_txf_price;
3987       end if;
3988 
3989       /* INVCONV Bug#5476815 ANTHIYAG 21-Aug-2006 Start */
3990       /************************************************************************************
3991       * If Intercompany invoicing is enabled, then the booking should be made against the *
3992       * Inventory AP Accrual account rather than the Inter-Org Payables Account           *
3993       ************************************************************************************/
3994       IF nvl(l_io_invoicing, 2) =  1 AND i_src_type_id IN (7, 8) AND l_org_ou <> l_txf_org_ou THEN /* INVCONV Bug#5498133/5498041 ANTHIYAG 30-Aug-2006 */
3995         l_acct := CSTPACHK.get_account_id(l_to_org, i_txn_id, -1, 2,
3996                             NULL, NULL, l_subinv,0, l_snd_rcv,
3997                             l_err_num, l_err_code, l_err_msg);
3998 
3999         if (l_err_num <> 0) then
4000           raise process_error;
4001         end if;
4002 
4003         if (l_acct = -1) then
4004           l_acct := l_inv_ap_accrual_acct;
4005         end if;
4006 
4007         insert_account(l_to_org, i_txn_id, i_item_id, -1 * l_value,
4008                       -1 * l_rcv_qty, l_acct, l_rcv_sob_id, 2, NULL, NULL,
4009                       i_txn_date, i_txn_src_id, i_src_type_id,
4010                       l_rcv_curr, l_snd_curr,
4011                       l_conv_date, l_conv_rate, l_curr_type,1,
4012                       i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4013                       l_err_num, l_err_code, l_err_msg);
4014 
4015         if (l_err_num <> 0) then
4016           raise process_error;
4017         end if;
4018 
4019         fnd_file.put_line(fnd_file.log,'Inter Company Accrual amount(l_value):' || l_value || ' acct: ' || l_acct);
4020 
4021       ELSE
4022         /* INVCONV Bug#5476815 ANTHIYAG 21-Aug-2006 End */
4023 
4024         l_acct := CSTPACHK.get_account_id(l_to_org, i_txn_id, -1, 9,
4025                             NULL, NULL, l_subinv,0, l_snd_rcv,
4026                             l_err_num, l_err_code, l_err_msg);
4027         if (l_err_num <> 0) then
4028           raise process_error;
4029         end if;
4030 
4031         if (l_acct = -1) then
4032 	        l_acct := l_io_pay_acct;
4033         end if;
4034 
4035         fnd_file.put_line(fnd_file.log,'Payable amount(l_value):' || l_value || ' acct: ' || l_acct);
4036 
4037         /* Bug 3169767: use -1*l_rcv_qty */
4038         insert_account(l_to_org, i_txn_id, i_item_id, -1 * l_value,
4039   			-1 * l_rcv_qty, l_acct, l_rcv_sob_id, 9, NULL, NULL,
4040   			i_txn_date, i_txn_src_id, i_src_type_id,
4041   			l_rcv_curr, l_snd_curr,
4042   			l_conv_date, l_conv_rate, l_curr_type,1,
4043   			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4044   			l_err_num, l_err_code, l_err_msg);
4045         if (l_err_num <> 0) then
4046           raise process_error;
4047         end if;
4048       END IF;
4049 
4050 
4051       /* Transportation credit if FOB is shipment. */
4052       -- bug 774662 check if it is direct interorg txfer don't do the credit
4053       if (l_pd_txfr_ind = 0)
4054         then
4055         if ((i_trp_cost <> 0) and (i_fob_point = 1)) and (i_txn_act_id <> 3) then
4056           /* transportation credit need to be converted to receiving currency
4057              for fob point shipment, because the credit will be in the
4058              receiving org*/
4059           if (l_to_org <> i_txn_org_id) then
4060             l_value := i_trp_cost * l_conv_rate;
4061           end if;
4062 
4063           l_acct := CSTPACHK.get_account_id(l_to_org, i_txn_id, -1, 12,
4064                                           NULL, NULL, l_subinv,0, l_snd_rcv,
4065                                           l_err_num, l_err_code, l_err_msg);
4066 
4067           if (l_err_num <> 0) then
4068             raise process_error;
4069           end if;
4070 
4071           if (l_acct = -1) then
4072             l_acct := i_trp_acct;
4073           end if;
4074 
4075           insert_account(l_to_org, i_txn_id, i_item_id, -1 * l_value,
4076                           l_rcv_qty, l_acct, l_rcv_sob_id, 12, NULL, NULL,
4077                           i_txn_date, i_txn_src_id, i_src_type_id,
4078                           l_rcv_curr, l_snd_curr,
4079                           l_conv_date, l_conv_rate, l_curr_type,1,
4080                           i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4081                           l_err_num, l_err_code, l_err_msg);
4082           if (l_err_num <> 0) then
4083             raise process_error;
4084           end if;
4085 
4086         end if;
4087       end if;  -- OPM INVCONV umoogala
4088 
4089     elsif (i_txn_act_id = 12 and i_fob_point = 1) then
4090       /* Receiving org, no ownership change. */
4091       if (l_to_org = i_org_id and i_exp_item = 0) then /* Average Cost Org */
4092         /* Debit Inventory at Rcv org's cost */
4093 
4094         l_intransit := 0;
4095 
4096         --
4097         -- OPM INVCONV umoogala
4098         -- Added if/else block
4099         --
4100 	-- Bug 4900652: Booking Inv Val. and Intransit Acct with current Avg. Cost
4101 	-- to make it consistent with Discrete/Discrete Org.
4102         -- if (l_pd_txfr_ind = 0)
4103         -- then
4104           inventory_accounts(l_to_org, i_txn_id,2,i_txn_act_id,l_to_cost_grp,
4105                           i_item_id, l_rcv_qty,
4106                           l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
4107                           i_exp_item, l_exp_sub, l_exp_acct, l_subinv, l_intransit,
4108                           l_snd_rcv, l_rcv_curr, null,
4109                           null, null, null,
4110                           i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4111                           l_err_num, l_err_code, l_err_msg);
4112 
4113           if (l_err_num <> 0) then
4114             raise process_error;
4115           end if;
4116 
4117 	/*
4118         else
4119           --
4120           -- OPM INVCONV  Process-Discrete Transfers
4121           -- For process-discrete transfer book Inv. Valuation account with
4122           -- (transfer price + freight).
4123           --
4124 
4125           l_value := (i_txf_price * l_rcv_qty) + i_trp_cost;
4126 
4127           if (l_to_cost_grp = 1) then
4128             select nvl(material_account,-1)
4129             into   l_acct
4130             from   mtl_parameters
4131             where  organization_id = i_org_id;
4132           else
4133             select nvl(material_account,-1)
4134             into   l_acct
4135             from   cst_cost_group_accounts
4136             where  organization_id = i_org_id
4137             and    cost_group_id = l_to_cost_grp;
4138           end if;
4139 
4140           insert_account(l_to_org, i_txn_id, i_item_id, -1 * l_value,
4141                             -1 * l_rcv_qty, l_acct, l_rcv_sob_id, 1, NULL, NULL,
4142                             i_txn_date, i_txn_src_id, i_src_type_id,
4143                             l_rcv_curr, NULL,
4144                             NULL, NULL, NULL, 1,
4145                             i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4146                             l_err_num, l_err_code, l_err_msg);
4147 
4148           if (l_err_num <> 0) then
4149             raise process_error;
4150           end if;
4151 
4152         end if;  -- OPM INVCONV umoogala
4153 	*/
4154 
4155         /* Credit Receiving Org's intransit. */
4156         l_intransit := 1;
4157 
4158         --
4159         -- OPM INVCONV umoogala
4160         -- Added if/else block
4161         --
4162 	-- Bug 4900652: Booking Inv Val. and Intransit Acct with current Avg. Cost
4163 	-- to make it consistent with Discrete/Discrete Org.
4164         -- if (l_pd_txfr_ind = 0)
4165         -- then
4166           inventory_accounts(l_to_org, i_txn_id,2,i_txn_act_id,l_from_cost_grp,
4167                           i_item_id, -1 * l_rcv_qty,
4168                           l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
4169                           i_exp_item, l_exp_sub, l_exp_acct, l_subinv, l_intransit,
4170                           l_snd_rcv, l_rcv_curr, null,
4171                           null, null, null,
4172                           i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4173                           l_err_num, l_err_code, l_err_msg);
4174 
4175           if (l_err_num <> 0) then
4176             raise process_error;
4177           end if;
4178 	/*
4179         else
4180           --
4181           -- OPM INVCONV umoogala
4182           -- For process-discrete transfer book Inv. Valuation account with
4183           -- (transfer price + freight).
4184           --
4185 
4186           l_value := (i_txf_price * l_rcv_qty) + i_trp_cost;
4187 
4188           if (l_to_cost_grp = 1) then
4189             select nvl(material_account,-1)
4190             into   l_acct
4191             from   mtl_parameters
4192             where  organization_id = i_org_id;
4193           else
4194             select nvl(material_account,-1)
4195             into   l_acct
4196             from   cst_cost_group_accounts
4197             where  organization_id = i_org_id
4198             and    cost_group_id = l_to_cost_grp;
4199           end if;
4200 
4201           insert_account(l_to_org, i_txn_id, i_item_id, l_value,
4202               l_rcv_qty, l_acct, l_rcv_sob_id, 14,
4203               NULL, NULL,
4204               i_txn_date, i_txn_src_id, i_src_type_id,
4205               l_rcv_curr, NULL, NULL, NULL, NULL,
4206               1, i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
4207               l_err_num, l_err_code, l_err_msg);
4208 
4209           if (l_err_num <> 0) then
4210             raise process_error;
4211           end if;
4212 
4213         end if; -- OPM INVCONV umoogala
4214 	*/
4215 
4216       elsif (l_std_exp_item = 0) then /* Standard Cost Org */
4217 
4218            fnd_file.put_line(fnd_file.log,'Std org: Dr inv at receiving org cost');
4219 
4220 
4221         /* Debit Invenoty at Rcv org's cost */
4222         if (l_std_exp_sub = 1) then
4223           distribute_accounts(l_to_org, i_txn_id,
4224 			0,i_cost_grp_id,i_txn_act_id,
4225 			i_item_id, l_rcv_qty, 2,
4226 			0, 0, l_std_exp_acct, NULL, NULL, NULL, NULL,NULL,
4227 			l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
4228 			l_subinv, l_snd_rcv, l_rcv_curr,
4229 			null, null, null, null,
4230 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4231 			l_err_num, l_err_code, l_err_msg);
4232           if (l_err_num <> 0) then
4233             raise process_error;
4234           end if;
4235 
4236         else
4237           distribute_accounts(l_to_org, i_txn_id,
4238 			0,i_cost_grp_id,i_txn_act_id,
4239 			i_item_id, l_rcv_qty, 1,
4240 			1, 0, NULL, l_std_mat_acct, l_std_mat_ovhd_acct,
4241 			l_std_res_acct, l_std_osp_acct, l_std_ovhd_acct,
4242 			l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
4243 			l_subinv, l_snd_rcv, l_rcv_curr,
4244 			null, null, null, null,
4245 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4246 			l_err_num, l_err_code, l_err_msg);
4247           if (l_err_num <> 0) then
4248             raise process_error;
4249           end if;
4250 
4251         end if;
4252         /* Debit Receiving Org's intransit. */
4253         distribute_accounts(l_to_org, i_txn_id,
4254 			0,i_cost_grp_id,i_txn_act_id,
4255 			i_item_id, -1 * l_rcv_qty, 14,
4256 			0, 0, l_io_inv_acct, NULL, NULL, NULL, NULL,NULL,
4257 			l_rcv_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
4258 			l_subinv, l_snd_rcv, l_rcv_curr,
4259 			null, null, null, null,
4260 			i_user_id, i_login_id, i_req_id, i_prg_appl_id, i_prg_id,
4261 			l_err_num, l_err_code, l_err_msg);
4262         if (l_err_num <> 0) then
4263           raise process_error;
4264         end if;
4265 
4266       end if;
4267     end if;
4268   end if;
4269 
4270   EXCEPTION
4271 
4272  when process_error then
4273  rollback;
4274  O_error_num := l_err_num;
4275  O_error_code := l_err_code;
4276  O_error_message := l_err_msg;
4277  when moh_rules_error then
4278       rollback;
4279       o_error_num := 9999;
4280       o_error_code := 'CST_RULES_ERROR';
4281       FND_MESSAGE.set_name('BOM', 'CST_RULES_ERROR');
4282       o_error_message := FND_MESSAGE.Get;
4283  when others then
4284 
4285  rollback;
4286  O_error_num := SQLCODE;
4287  O_error_message := 'CSTPACDP.interorg_cost_txn(' || to_char(l_stmt_num) || ') '||
4288                      substr(SQLERRM,1,180);
4289 
4290 end interorg_cost_txn;
4291 
4292 procedure avcu_cost_txn(
4293   I_ORG_ID		IN	NUMBER,
4294   I_TXN_ID		IN 	NUMBER,
4295   I_COST_GRP_ID		IN	NUMBER,
4296   I_ITEM_ID		IN	NUMBER,
4297   I_TXN_DATE		IN	DATE,
4298   I_QTY			IN	NUMBER,
4299   I_TXN_ACT_ID		IN	NUMBER,
4300   I_TXN_SRC_ID		IN	NUMBER,
4301   I_SRC_TYPE_ID		IN	NUMBER,
4302   I_MAT_ACCT		IN	NUMBER,
4303   I_MAT_OVHD_ACCT	IN	NUMBER,
4304   I_RES_ACCT		IN	NUMBER,
4305   I_OSP_ACCT		IN	NUMBER,
4306   I_OVHD_ACCT		IN	NUMBER,
4307   I_SOB_ID		IN	NUMBER,
4308   I_PRI_CURR		IN	VARCHAR2,
4309   I_ALT_CURR		IN	VARCHAR2,
4310   I_CONV_DATE		IN	DATE,
4311   I_CONV_RATE		IN	NUMBER,
4312   I_CONV_TYPE		IN	VARCHAR2,
4313   I_EXP_ITEM		IN	NUMBER,
4314   I_ONHAND_VAR_ACCT     IN      NUMBER,
4315   I_USER_ID		IN	NUMBER,
4316   I_LOGIN_ID    	IN	NUMBER,
4317   I_REQ_ID		IN	NUMBER,
4318   I_PRG_APPL_ID		IN	NUMBER,
4319   I_PRG_ID		IN 	NUMBER,
4320   O_Error_Num		OUT NOCOPY	NUMBER,
4321   O_Error_Code		OUT NOCOPY	VARCHAR2,
4322   O_Error_Message	OUT NOCOPY	VARCHAR2
4323 )IS
4324   l_acct_exist		NUMBER;
4325   l_acct		NUMBER;
4326   l_cost		NUMBER;
4327   l_var			NUMBER;
4328   l_ele_exist		NUMBER;
4329   l_inv_mat_acct	NUMBER;
4330   l_inv_mat_ovhd_acct	NUMBER;
4331   l_inv_res_acct	NUMBER;
4332   l_inv_osp_acct	NUMBER;
4333   l_inv_ovhd_acct	NUMBER;
4334   l_onhand_var          NUMBER;/*LCM*/
4335   l_err_num	NUMBER;
4336   l_err_code	VARCHAR2(240);
4337   l_err_msg	VARCHAR2(240);
4338   l_stmt_num	NUMBER;
4339   l_debug       VARCHAR2(1);
4340   process_error	EXCEPTION;
4341   no_acct_error EXCEPTION;
4342   no_txn_det_error EXCEPTION;
4343 BEGIN
4344   -- initialize local variables
4345   l_ele_exist := 0;
4346   l_err_num := 0;
4347   l_err_code := '';
4348   l_err_msg := '';
4349   l_debug := fnd_profile.value('MRP_DEBUG');
4350 
4351   l_stmt_num := 2;
4352   if (i_qty = 0 and l_debug = 'Y') THEN
4353     FND_MESSAGE.set_name('BOM', 'CST_UPDATE_ZERO_QTY');
4354     fnd_file.put_line(fnd_file.log, 'CSTPACDP.avcu_cost_txn: ' || FND_MESSAGE.Get);
4355   end if;
4356 
4357   l_stmt_num := 5;
4358 
4359   if (i_cost_grp_id = 1) then
4360     select count(*)
4361     into   l_acct_exist
4362     from   mtl_parameters
4363     where  organization_id = i_org_id;
4364   else
4365     select count(*)
4366     into   l_acct_exist
4367     from   cst_cost_group_accounts
4368     where  organization_id = i_org_id
4369     and    cost_group_id = i_cost_grp_id;
4370   end if;
4371 
4372   if (l_acct_exist = 0) then
4373     raise no_acct_error;
4374   end if;
4375 
4376   l_stmt_num := 10;
4377 
4378   if (i_cost_grp_id = 1) then
4379     select nvl(material_account,-1),
4380            nvl(material_overhead_account,-1),
4381            nvl(resource_account,-1),
4382            nvl(outside_processing_account,-1),
4383            nvl(overhead_account,-1)
4384     into   l_inv_mat_acct,
4385            l_inv_mat_ovhd_acct,
4386            l_inv_res_acct,
4387            l_inv_osp_acct,
4388            l_inv_ovhd_acct
4389     from   mtl_parameters
4390     where  organization_id = i_org_id;
4391   else
4392     select nvl(material_account,-1),
4393            nvl(material_overhead_account,-1),
4394            nvl(resource_account,-1),
4395            nvl(outside_processing_account,-1),
4396            nvl(overhead_account,-1)
4397     into   l_inv_mat_acct,
4398            l_inv_mat_ovhd_acct,
4399            l_inv_res_acct,
4400            l_inv_osp_acct,
4401            l_inv_ovhd_acct
4402     from   cst_cost_group_accounts
4403     where  organization_id = i_org_id
4404     and    cost_group_id = i_cost_grp_id;
4405   end if;
4406 
4407   l_stmt_num := 20;
4408 
4409   select count(*)
4410   into l_ele_exist
4411   from mtl_cst_actual_cost_details
4412   where transaction_id = i_txn_id
4413   and organization_id = i_org_id;
4414 
4415   if (l_ele_exist = 0) then
4416     raise no_txn_det_error;
4417   end if;
4418 
4419  -- Initially, average cost update was designed such that no variance
4420  -- would be generated. However, there were some inconsistencies with this
4421  -- approach. Average cost update will now generate variances for some cases.
4422  -- For eg., when a value change is performed that drives Onhand value
4423  -- negative, the onhand value is driven to zero and the residue is
4424  -- written off to the avg cost variance account.
4425  -- Based on this the accounting rules are :
4426  --
4427  -- 	Adjustment acct 	(prior - New) * Qty - Variance
4428  --	Inventory		(New - Prior) * Qty
4429  -- 	Variance Acct		 Variance
4430  -- All these value are based on MCACD.
4431 
4432 
4433   FOR cost_element IN 1..5 loop
4434     l_cost := NULL;
4435     -- The difference between new cost and prior cost is the impact to
4436     -- inventory. If new cost is higher then it's a debit to inventory
4437     -- else it is a credit to inventory.
4438 
4439     l_stmt_num := 30;
4440 
4441     select (sum(new_cost) - sum(prior_cost)),sum(variance_amount)
4442            ,sum(onhand_variance_amount)
4443     into l_cost,l_var,l_onhand_var
4444     from mtl_cst_actual_cost_details
4445     where transaction_id = i_txn_id
4446     and organization_id = i_org_id
4447     and cost_element_id = cost_element;
4448 
4449 
4450     if (l_cost is not NULL) then
4451 
4452      if (l_cost <> 0) then -- 4706781 This applies to INV accounting only
4453 
4454       -- First post to inventory.
4455       l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
4456 				sign(l_cost), 1,
4457 				cost_element, NULL, NULL,
4458 				0, NULL, l_err_num, l_err_code,
4459 				l_err_msg);
4460 
4461       -- check error
4462 	 if (l_err_num<>0) then
4463          raise process_error;
4464  	 end if;
4465 
4466       if (l_acct = -1) then
4467 
4468 	l_stmt_num := 40;
4469 
4470         select decode(cost_element, 1, l_inv_mat_acct,
4471 				  2, l_inv_mat_ovhd_acct,
4472 				  3, l_inv_res_acct,
4473 				  4, l_inv_osp_acct,
4474 				  5, l_inv_ovhd_acct)
4475         into l_acct
4476         from dual;
4477       end if;
4478 
4479       insert_account(i_org_id, i_txn_id, i_item_id, i_qty * l_cost,
4480 		sign(i_qty * l_cost) * abs(i_qty)/*modified for bug#4005770*//*i_qty*/, l_acct, i_sob_id, 1,
4481 		cost_element, NULL,
4482 		i_txn_date, i_txn_src_id, i_src_type_id,
4483 		i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
4484 		1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
4485 		l_err_num, l_err_code, l_err_msg);
4486 
4487 	-- check error
4488          if (l_err_num<>0) then
4489          raise process_error;
4490          end if;
4491 
4492      end if; -- (l_cost <> 0)
4493 
4494      if (l_cost <> 0 OR l_var <> 0 OR l_onhand_var <> 0) then -- 4706781 Adj has to be posted if variance exists
4495 
4496       -- Second post to adjustment.
4497       l_cost := -1 * l_cost;
4498       l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
4499 				sign((i_qty * l_cost) - l_var-l_onhand_var), 2,
4500 				cost_element, NULL, NULL,
4501 				0, NULL, l_err_num, l_err_code,
4502 				l_err_msg);
4503 	-- check error
4504          if (l_err_num<>0) then
4505          raise process_error;
4506          end if;
4507 
4508       l_stmt_num := 50;
4509 
4510 /* Added the decode for BUG: 1107767. Avg cost update through the interface needs all the accounts
4511    in MMT to be specified, even if only the material cost element is getting affected */
4512 
4513       if (l_acct = -1) then
4514         select decode(cost_element, 1, i_mat_acct,
4515 				  2, decode(i_mat_ovhd_acct,-1, i_mat_acct, i_mat_ovhd_acct),
4516                                   3, decode(i_res_acct,-1, i_mat_acct, i_res_acct),
4517 				  4, decode(i_osp_acct,-1, i_mat_acct, i_osp_acct),
4518 				  5, decode(i_ovhd_acct,-1, i_mat_acct, i_ovhd_acct))
4519         into l_acct
4520         from dual;
4521       end if;
4522 
4523       insert_account(i_org_id, i_txn_id, i_item_id, (i_qty * l_cost) - l_var-l_onhand_var,
4524 		i_qty, l_acct, i_sob_id, 2,
4525 		cost_element, NULL,
4526 		i_txn_date, i_txn_src_id, i_src_type_id,
4527 		i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
4528 		1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
4529 		l_err_num, l_err_code, l_err_msg);
4530 
4531 	-- check error
4532          if (l_err_num<>0) then
4533          raise process_error;
4534          end if;
4535 
4536      end if; -- (l_cost <> 0 and l_var <> 0)
4537      if (l_onhand_var <> 0) then
4538 
4539       l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
4540                                 sign(l_onhand_var), 20,
4541                                 cost_element, NULL, NULL,
4542                                 0, NULL, l_err_num, l_err_code,
4543                                 l_err_msg);
4544 
4545           -- check error
4546       if(l_err_num<>0) then
4547       raise process_error;
4548       end if;
4549 
4550       if (l_acct = -1) then
4551         l_acct := I_ONHAND_VAR_ACCT;
4552       end if;
4553 
4554       insert_account(i_org_id, i_txn_id, i_item_id, l_onhand_var,
4555                 i_qty, l_acct, i_sob_id, 20,
4556                 cost_element, NULL,
4557                 i_txn_date, i_txn_src_id, i_src_type_id,
4558                 i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
4559                 1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
4560                 l_err_num, l_err_code, l_err_msg);
4561 
4562       -- check error
4563       if(l_err_num<>0) then
4564       raise process_error;
4565       end if;
4566 
4567      end if;
4568 
4569     end if; -- (l_cost is not NULL)
4570 
4571   end loop;
4572 
4573 
4574  -- Now Post one consolidated variance entry
4575 
4576   l_stmt_num := 60;
4577 
4578   select nvl(sum(variance_amount),0)
4579   into l_var
4580   from mtl_cst_actual_cost_details cacd
4581   where transaction_id = i_txn_id
4582   and organization_id = i_org_id;
4583 
4584   if (l_var <> 0) then
4585 
4586       l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
4587                                 sign(l_var), 13,
4588                                 NULL, NULL, NULL,
4589                                 0, NULL, l_err_num, l_err_code,
4590                                 l_err_msg);
4591 
4592           -- check error
4593       if(l_err_num<>0) then
4594       raise process_error;
4595       end if;
4596 
4597     if (l_acct = -1) then
4598       if (i_cost_grp_id = 1) then
4599         select nvl(average_cost_var_account,-1)
4600         into   l_acct
4601         from   mtl_parameters
4602         where  organization_id = i_org_id;
4603       else
4604         select nvl(average_cost_var_account,-1)
4605         into   l_acct
4606         from   cst_cost_group_accounts
4607         where  organization_id = i_org_id
4608         and    cost_group_id = i_cost_grp_id;
4609       end if;
4610     end if;
4611 
4612     insert_account(i_org_id, i_txn_id, i_item_id, l_var,
4613                 i_qty, l_acct, i_sob_id, 13,
4614                 NULL, NULL,
4615                 i_txn_date, i_txn_src_id, i_src_type_id,
4616                 i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
4617                 1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
4618                 l_err_num, l_err_code, l_err_msg);
4619 
4620       -- check error
4621       if(l_err_num<>0) then
4622       raise process_error;
4623       end if;
4624 
4625  end if;
4626 
4627   EXCEPTION
4628 
4629  when process_error then
4630  rollback;
4631  O_error_num := l_err_num;
4632  O_error_code := l_err_code;
4633  O_error_message := l_err_msg;
4634 
4635  when no_acct_error then
4636  rollback;
4637  O_error_num := 9999;
4638  O_error_code := 'CST_NO_CG_ACCTS';
4639  FND_MESSAGE.set_name('BOM', 'CST_NO_CG_ACCTS');
4640  O_error_message := FND_MESSAGE.Get;
4641 
4642  when no_txn_det_error then
4643  rollback;
4644  O_error_num := 9999;
4645  O_error_code := 'CST_NO_TXN_DET';
4646  FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
4647  O_error_message := FND_MESSAGE.Get;
4648 
4649  when others then
4650  rollback;
4651  O_error_num := SQLCODE;
4652  O_error_message := 'CSTPACDP.avcu_cost_txn' || to_char(l_stmt_num) ||
4653                      substr(SQLERRM,1,180);
4654 
4655 END avcu_cost_txn;
4656 
4657 -- New procedure for dropshipment project
4658 procedure logical_cost_txn(
4659   I_ORG_ID              IN          NUMBER,
4660   I_TXN_ID              IN          NUMBER,
4661   I_COST_GRP_ID         IN          NUMBER,
4662   I_ITEM_ID             IN          NUMBER,
4663   I_TXN_DATE            IN          DATE,
4664   I_P_QTY               IN          NUMBER,
4665   I_SUBINV              IN          VARCHAR2,
4666   I_TXN_ACT_ID          IN          NUMBER,
4667   I_TXN_SRC_ID          IN          NUMBER,
4668   I_SRC_TYPE_ID         IN          NUMBER,
4669   I_TXN_TYPE_ID         IN          NUMBER,
4670   I_DIST_ACCT           IN          NUMBER,
4671   I_SOB_ID              IN          NUMBER,
4672   I_PRI_CURR            IN          VARCHAR2,
4673   I_ALT_CURR            IN          VARCHAR2,
4674   I_CONV_DATE           IN          DATE,
4675   I_CONV_RATE           IN          NUMBER,
4676   I_CONV_TYPE           IN          VARCHAR2,
4677   I_SO_ACCOUNTING       IN          NUMBER,
4678   I_COGS_PERCENTAGE     IN          NUMBER,
4679   I_COGS_OM_LINE_ID     IN          NUMBER,
4680   I_EXP_ITEM            IN          NUMBER,
4681   I_USER_ID             IN          NUMBER,
4682   I_LOGIN_ID            IN          NUMBER,
4683   I_REQ_ID              IN          NUMBER,
4684   I_PRG_APPL_ID         IN          NUMBER,
4685   I_PRG_ID              IN          NUMBER,
4686   O_Error_Num           OUT NOCOPY  NUMBER,
4687   O_Error_Code          OUT NOCOPY  VARCHAR2,
4688   O_Error_Message       OUT NOCOPY  VARCHAR2
4689 )IS
4690   l_exp_sub          NUMBER;
4691   l_exp              NUMBER;
4692   l_exp_acct         NUMBER;
4693   l_acct_line_type   NUMBER;
4694   l_acct             NUMBER;
4695   l_stmt_num         NUMBER;
4696   process_error      EXCEPTION;
4697   l_err_num          NUMBER;
4698   l_err_code         VARCHAR2(240);
4699   l_err_msg          VARCHAR2(240);
4700   l_msg_count        NUMBER;
4701   l_return_status    VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
4702   l_msg_data         VARCHAR2(2000);
4703   l_cost_method      NUMBER;
4704 
4705   l_def_cogs_acct_id    NUMBER; -- Revenue COGS Matching
4706   l_ref_om_line_id      NUMBER;
4707   l_elemental           NUMBER := 0; -- For the offsetting distribution, most are not elemental
4708 
4709 BEGIN
4710 
4711   l_stmt_num := 10;
4712 
4713   -- No distributions for Logical sales order issues and
4714   -- Logical RMA receipts if the item is an expense item
4715   -- or if it comes from/into an expense subinventory
4716   if ( (i_src_type_id in (2,12)) AND
4717        (i_exp_item = 1) ) then
4718     -- no accounting
4719     return;
4720   end if;
4721 
4722   l_stmt_num := 15;
4723 
4724 
4725   -- For all of the other transaction types, expense items
4726   -- will be accounted for as if they are inventory items.
4727   -- We just need to provide the expense account for expense items.
4728   if (i_exp_item = 1) then
4729     if (i_cost_grp_id = 1) then
4730       select nvl(expense_account,-1)
4731       into l_exp_acct
4732       from mtl_parameters
4733       where  organization_id = i_org_id;
4734     else
4735       select nvl(expense_account, -1)
4736       into l_exp_acct
4737       from cst_cost_group_accounts
4738       where cost_group_id = i_cost_grp_id   AND
4739             organization_id = i_org_id;
4740     end if;
4741   else
4742     l_exp_acct := -1;
4743   end if;
4744 
4745   l_stmt_num := 20;
4746   -- hit up MP for some values
4747   select primary_cost_method, -- Standard costing orgs will have to call the new inventory_accounts_std() procedure
4748          deferred_cogs_account
4749   into l_cost_method,
4750        l_def_cogs_acct_id
4751   from mtl_parameters
4752   where organization_id = i_org_id;
4753 
4754   /*******************************************************************************
4755    ** Set the account and accounting line type for the offsetting distribution. **
4756    *******************************************************************************/
4757   l_acct := i_dist_acct;
4758 
4759   if (i_txn_type_id in (19,39,69,22,23)) then
4760     l_acct_line_type := 31; -- clearing
4761   elsif (i_txn_type_id in (11,14)) then
4762     l_acct_line_type := 2; -- account (I/C COGS)
4763   elsif (i_txn_type_id in (10,13)) then
4764     l_acct_line_type := 16; -- accrual
4765   elsif (i_txn_type_id in (16,30)) then -- Logical RMA Receipt or Logical SO Issue
4766     l_acct_line_type := 35; -- New COGS line type
4767 
4768     l_stmt_num := 30;
4769     -- Get the original sales order issue OM line ID, and check
4770     -- whether the original sales order issue was inserted into
4771     -- the Revenue / COGS Matching data model.
4772     SELECT max(ool.reference_line_id)
4773     INTO l_ref_om_line_id
4774     FROM oe_order_lines_all ool,
4775          cst_revenue_cogs_match_lines crcml
4776     WHERE ool.line_id = i_cogs_om_line_id
4777     AND   ool.reference_line_id = crcml.cogs_om_line_id
4778     AND   crcml.pac_cost_type_id IS NULL
4779     AND   i_txn_type_id = 16;
4780   elsif (i_txn_act_id = 36 AND i_src_type_id = 2) then
4781   -- COGS Recognition Event
4782 
4783       l_stmt_num := 40;
4784      /* This procedure will move an appropriate balance between *
4785       * the Deferred COGS and COGS accounts.                    */
4786      CST_RevenueCogsMatch_PVT.Process_CogsRecognitionTxn(
4787                   x_return_status   => l_return_status,
4788                   x_msg_count       => l_msg_count,
4789                   x_msg_data        => l_msg_data,
4790                   p_cogs_om_line_id => i_cogs_om_line_id,
4791                   p_transaction_id  => i_txn_id,
4792                   p_txn_quantity    => i_p_qty,
4793                   p_organization_id => i_org_id,
4794                   p_item_id         => i_item_id,
4795                   p_sob_id          => i_sob_id,
4796                   p_txn_date        => i_txn_date,
4797                   p_txn_src_id      => i_txn_src_id,
4798                   p_src_type_id     => i_src_type_id,
4799                   p_pri_curr        => i_pri_curr,
4800                   p_alt_curr        => i_alt_curr,
4801                   p_conv_date       => to_date(i_conv_date, 'YYYY/MM/DD'),
4802                   p_conv_rate       => i_conv_rate,
4803                   p_conv_type       => i_conv_type,
4804                   p_user_id         => i_user_id,
4805                   p_login_id        => i_login_id,
4806                   p_req_id          => i_req_id,
4807                   p_prg_appl_id     => i_prg_appl_id,
4808                   p_prg_id          => i_prg_id);
4809 
4810      -- check return status
4811      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4812         l_err_num := -1;
4813         IF (l_msg_count = 1) THEN
4814            l_err_msg := substr(l_msg_data,1,240);
4815         ELSE
4816            l_err_msg := 'Failure in procedure CST_RevenueCogsMatch_PVT.Process_CogsRecognitionTxn()';
4817         END IF;
4818         raise process_error;
4819      ELSE
4820         RETURN;
4821      END IF;
4822 
4823   else
4824     -- invalid transaction type
4825     l_err_msg := 'Invalid Transaction type passed to logical_cost_txn';
4826     l_err_num := 9999;
4827     raise process_error;
4828   end if;
4829 
4830   l_stmt_num := 50;
4831   -- Make the inventory distributions
4832   if (l_cost_method = 1) then
4833     inventory_accounts_std(i_org_id, i_txn_id, 2,
4834                         i_txn_act_id,i_cost_grp_id,
4835                         i_item_id, i_p_qty,
4836                         i_sob_id,i_txn_date, i_txn_src_id, i_src_type_id,
4837                         i_exp_item, 0 /*l_exp_sub*/, l_exp_acct, i_subinv, 0, NULL,
4838                         i_pri_curr, i_alt_curr, i_conv_date,
4839                         i_conv_rate, i_conv_type,
4840                         i_user_id, i_login_id, i_req_id,
4841                         i_prg_appl_id, i_prg_id,
4842                         l_err_num, l_err_code, l_err_msg);
4843   else
4844     -- i_p_qty will always determine Dr. vs. Cr.
4845     -- even the txn_action of 11 (logical PO Deliver adj) will work for this
4846     -- because a positive qty means debit, a negative qty means credit
4847     inventory_accounts(i_org_id, i_txn_id, 2,
4848                         i_txn_act_id,i_cost_grp_id,
4849                         i_item_id, i_p_qty,
4850                         i_sob_id,i_txn_date, i_txn_src_id, i_src_type_id,
4851                         i_exp_item, 0 /*l_exp_sub*/, l_exp_acct, i_subinv, 0, NULL,
4852                         i_pri_curr, i_alt_curr, i_conv_date,
4853                         i_conv_rate, i_conv_type,
4854                         i_user_id, i_login_id, i_req_id,
4855                         i_prg_appl_id, i_prg_id,
4856                         l_err_num, l_err_code, l_err_msg);
4857   end if;
4858 
4859   -- check error
4860   if(l_err_num<>0) then
4861     raise process_error;
4862   end if;
4863 
4864 
4865   IF (i_txn_type_id = 30 AND i_so_accounting = 2) THEN
4866      /* Deferred COGS Accounting for this logical sales order */
4867 
4868      /* Only call Insert_OneSoIssue if the percentage is NULL. */
4869      IF (i_cogs_percentage IS NULL) THEN
4870 
4871         l_stmt_num := 60;
4872         /* Record this sales order issue for COGS deferral by *
4873          * inserting into the Revenue / COGS matching tables  */
4874         CST_RevenueCogsMatch_PVT.Insert_OneSoIssue(
4875                         p_api_version      => 1,
4876                         p_user_id          => i_user_id,
4877                         p_login_id         => i_login_id,
4878                         p_request_id       => i_req_id,
4879                         p_pgm_app_id       => i_prg_appl_id,
4880                         p_pgm_id           => i_prg_id,
4881                         x_return_status    => l_return_status,
4882                         p_cogs_om_line_id  => i_cogs_om_line_id,
4883                         p_cogs_acct_id     => l_acct,
4884                         p_def_cogs_acct_id => l_def_cogs_acct_id,
4885                         p_mmt_txn_id       => i_txn_id,
4886                         p_organization_id  => i_org_id,
4887                         p_item_id          => i_item_id,
4888                         p_transaction_date => i_txn_date,
4889                         p_cost_group_id    => i_cost_grp_id,
4890                         p_quantity         => (-1*i_p_qty)); /* track issue quantities as positive values */
4891 
4892         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4893            l_err_num := -1;
4894            FND_MSG_PUB.count_and_get
4895            (  p_count   => l_msg_count,
4896               p_data    => l_msg_data,
4897               p_encoded => FND_API.g_false
4898            );
4899            IF (l_msg_count = 1) THEN
4900               l_err_msg := substr(l_msg_data,1,240);
4901            ELSE
4902               l_err_msg := 'Failure in procedure CST_RevenueCogsMatch_PVT.Insert_OneSoIssue()';
4903            END IF;
4904            raise process_error;
4905         END IF;
4906      END IF; /* COGS Percentage is NULL */
4907 
4908      l_acct := l_def_cogs_acct_id; /* pass deferred COGS account to distribute_accounts() */
4909      l_acct_line_type := 36; -- Deferred COGS
4910      l_elemental := 1;  -- For Deferred COGS sales orders we want elemental call to distribute_accounts()
4911 
4912   END IF; /* Deferred COGS Accounting */
4913 
4914   l_stmt_num := 70;
4915 
4916   /*********************************************************************
4917    **  Now post to the offsetting accounts.                           **
4918    *********************************************************************/
4919 
4920   -- Now for RMA receipts that reference an original sales order line ID
4921   IF (i_txn_type_id = 16 AND l_ref_om_line_id IS NOT NULL) THEN
4922 
4923      /* This procedure will create the offsetting credits *
4924       * split accordingly between COGS and Deferred COGS. */
4925      CST_RevenueCogsMatch_PVT.Process_RmaReceipt(
4926                   x_return_status   => l_return_status,
4927                   x_msg_count       => l_msg_count,
4928                   x_msg_data        => l_msg_data,
4929                   p_rma_om_line_id  => i_cogs_om_line_id,
4930                   p_cogs_om_line_id => l_ref_om_line_id,
4931                   p_cost_type_id    => 2,
4932                   p_txn_quantity    => (-1*i_p_qty),
4933                   p_cogs_percentage => i_cogs_percentage,
4934                   p_organization_id => i_org_id,
4935                   p_transaction_id  => i_txn_id,
4936                   p_item_id         => i_item_id,
4937                   p_sob_id          => i_sob_id,
4938                   p_txn_date        => i_txn_date,
4939                   p_txn_src_id      => i_txn_src_id,
4940                   p_src_type_id     => i_src_type_id,
4941                   p_pri_curr        => i_pri_curr,
4942                   p_alt_curr        => i_alt_curr,
4943                   p_conv_date       => i_conv_date,
4944                   p_conv_rate       => i_conv_rate,
4945                   p_conv_type       => i_conv_type,
4946                   p_user_id         => i_user_id,
4947                   p_login_id        => i_login_id,
4948                   p_req_id          => i_req_id,
4949                   p_prg_appl_id     => i_prg_appl_id,
4950                   p_prg_id          => i_prg_id);
4951 
4952      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4953         l_err_num := -1;
4954         FND_MSG_PUB.count_and_get
4955         (  p_count   => l_msg_count,
4956            p_data    => l_msg_data,
4957            p_encoded => FND_API.g_false
4958         );
4959         IF (l_msg_count = 1) THEN
4960            l_err_msg := substr(l_msg_data,1,240);
4961         ELSE
4962            l_err_msg := 'Failure in procedure CST_RevenueCogsMatch_PVT.Process_RmaReceipt()';
4963         END IF;
4964         raise process_error;
4965      END IF;
4966 
4967   ELSE -- For all non-RMA transactions, as well as RMAs that don't reference deferred COGS sales orders:
4968 
4969      distribute_accounts(i_org_id, i_txn_id,2,i_cost_grp_id,i_txn_act_id,
4970                         i_item_id, -1 * i_p_qty,l_acct_line_type, l_elemental,
4971                         0 /*l_ovhd_absp*/,l_acct, l_acct, l_acct, l_acct,
4972                         l_acct, l_acct,
4973                         i_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
4974                         i_subinv, NULL, i_pri_curr, i_alt_curr, i_conv_date,
4975                         i_conv_rate, i_conv_type, i_user_id, i_login_id,
4976                         i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
4977                         l_err_code, l_err_msg, i_cogs_om_line_id);
4978 
4979   END IF; -- IF RMA Receipt with deferred COGS reference
4980 
4981   -- check error
4982   if(l_err_num<>0) then
4983     raise process_error;
4984   end if;
4985 
4986 EXCEPTION
4987 
4988  when process_error then
4989  rollback;
4990  O_error_num := SQLCODE;
4991  O_error_message := 'CSTPACDP.logical_cost_txn (' || to_char(l_stmt_num) ||
4992                     '): ' || substr(SQLERRM,1,180);
4993 
4994  when others then
4995 
4996  rollback;
4997  O_error_num := SQLCODE;
4998  O_error_message := 'CSTPACDP.logical_cost_txn (' || to_char(l_stmt_num) ||
4999                      '): ' || substr(SQLERRM,1,180);
5000 
5001 END logical_cost_txn;
5002 
5003 -- New procedure for dropshipment project
5004 procedure consigned_update_cost_txn(
5005   I_ORG_ID              IN          NUMBER,
5006   I_TXN_ID              IN          NUMBER,
5007   I_COST_GRP_ID         IN          NUMBER,
5008   I_ITEM_ID             IN          NUMBER,
5009   I_TXN_DATE            IN          DATE,
5010   I_P_QTY               IN          NUMBER,
5011   I_SUBINV              IN          VARCHAR2,
5012   I_TXN_ACT_ID          IN          NUMBER,
5013   I_TXN_SRC_ID          IN          NUMBER,
5014   I_SRC_TYPE_ID         IN          NUMBER,
5015   I_DIST_ACCT           IN          NUMBER,
5016   I_SOB_ID              IN          NUMBER,
5017   I_PRI_CURR            IN          VARCHAR2,
5018   I_ALT_CURR            IN          VARCHAR2,
5019   I_CONV_DATE           IN          DATE,
5020   I_CONV_RATE           IN          NUMBER,
5021   I_CONV_TYPE           IN          VARCHAR2,
5022   I_EXP_ITEM            IN          NUMBER,
5023   I_USER_ID             IN      NUMBER,
5024   I_LOGIN_ID            IN      NUMBER,
5025   I_REQ_ID              IN      NUMBER,
5026   I_PRG_APPL_ID         IN      NUMBER,
5027   I_PRG_ID              IN      NUMBER,
5028   O_Error_Num           OUT NOCOPY  NUMBER,
5029   O_Error_Code          OUT NOCOPY  VARCHAR2,
5030   O_Error_Message       OUT NOCOPY  VARCHAR2
5031 )IS
5032   l_stmt_num         NUMBER;
5033   process_error      EXCEPTION;
5034   l_err_num          NUMBER;
5035   l_err_code         VARCHAR2(240);
5036   l_err_msg          VARCHAR2(240);
5037   l_msg_count        NUMBER;
5038   l_return_status    VARCHAR2(11);
5039   l_msg_data         VARCHAR2(2000);
5040 
5041   l_retro_acct_id    NUMBER;
5042   l_qty_adj          NUMBER;
5043   l_txn_cost         NUMBER;
5044 BEGIN
5045 
5046   -- Initialize
5047   l_stmt_num := 10;
5048 
5049   -- Get the retro price update account from RCV_PARAMETERS
5050   select nvl(retroprice_adj_account_id,-1)
5051   into l_retro_acct_id
5052   from rcv_parameters
5053   where organization_id = i_org_id;
5054 
5055   if (l_retro_acct_id = -1) then
5056     l_err_num := SQLCODE;
5057     l_err_msg := 'CSTPACDP.consigned_update_cost_txn (10): '||
5058                  'RCV_PARAMETERS.RETROOPRICE_ADJ_ACCOUNT_ID must not be NULL';
5059     raise process_error;
5060   end if;
5061 
5062   l_stmt_num := 20;
5063   -- For the consigned update transaction, mmt.quantity_adjusted is
5064   -- used in the distributions since primary quantity and transaction
5065   -- quantity are 0.
5066   select quantity_adjusted, transaction_cost
5067   into l_qty_adj, l_txn_cost
5068   from mtl_material_transactions
5069   where transaction_id = i_txn_id;
5070 
5071   l_stmt_num := 30;
5072   -- The supplier accrual account is stored in MMT.dist_acct_id
5073   -- Accounting line type 32 = Retroactive Price Adjustment Transaction
5074   insert_account(i_org_id, i_txn_id, i_item_id, l_qty_adj * l_txn_cost,
5075                  l_qty_adj, l_retro_acct_id, i_sob_id, 32,
5076                  1 /*cost_element*/, NULL,
5077                  i_txn_date, i_txn_src_id, i_src_type_id,
5078                  i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
5079                  1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
5080                  l_err_num, l_err_code, l_err_msg);
5081   -- check error
5082   if(l_err_num<>0) then
5083     raise process_error;
5084   end if;
5085 
5086 
5087   l_stmt_num := 40;
5088   distribute_accounts(i_org_id, i_txn_id,2,i_cost_grp_id,i_txn_act_id,
5089                         i_item_id, -1 * l_qty_adj,16, 0 /*l_elemental*/,
5090                         0 /*l_ovhd_absp*/,i_dist_acct, i_dist_acct, i_dist_acct,
5091                         i_dist_acct, i_dist_acct, i_dist_acct,
5092                         i_sob_id, i_txn_date, i_txn_src_id, i_src_type_id,
5093                         i_subinv, NULL, i_pri_curr, i_alt_curr, i_conv_date,
5094                         i_conv_rate, i_conv_type, i_user_id, i_login_id,
5095                         i_req_id, i_prg_appl_id, i_prg_id, l_err_num,
5096                         l_err_code, l_err_msg);
5097 
5098   -- check error
5099   if(l_err_num<>0) then
5100     raise process_error;
5101   end if;
5102 
5103 EXCEPTION
5104 
5105  when process_error then
5106  rollback;
5107  O_error_num := l_err_num;
5108  --O_error_code := l_err_code;
5109  O_error_message := l_err_msg;
5110 
5111  when others then
5112 
5113  rollback;
5114  O_error_num := SQLCODE;
5115  O_error_message := 'CSTPACDP.consigned_update_cost_txn (' || to_char(l_stmt_num) ||
5116                      '): ' || substr(SQLERRM,1,180);
5117 
5118 END consigned_update_cost_txn;
5119 
5120 -- New procedure for dropshipment project
5121 procedure inventory_accounts_std(
5122   I_ORG_ID		IN	NUMBER,
5123   I_TXN_ID		IN 	NUMBER,
5124   I_COMM_ISS_FLAG       IN      NUMBER,
5125   I_COST_TXN_ACTION_ID  IN      NUMBER,
5126   I_COST_GRP_ID		IN	NUMBER,
5127   I_ITEM_ID		IN	NUMBER,
5128   I_P_QTY		IN	NUMBER,
5129   I_SOB_ID		IN	NUMBER,
5130   I_TXN_DATE		IN	DATE,
5131   I_TXN_SRC_ID		IN	NUMBER,
5132   I_SRC_TYPE_ID		IN	NUMBER,
5133   I_EXP_ITEM		IN	NUMBER,
5134   I_EXP_SUBINV		IN	NUMBER,
5135   I_EXP_ACCT		IN	NUMBER,
5136   I_SUBINV		IN	VARCHAR2,
5137   I_INTRANSIT		IN	NUMBER,
5138   I_SND_RCV		IN	NUMBER,
5139   I_PRI_CURR		IN	VARCHAR2,
5140   I_ALT_CURR		IN	VARCHAR2,
5141   I_CONV_DATE		IN	DATE,
5142   I_CONV_RATE		IN	NUMBER,
5143   I_CONV_TYPE		IN	VARCHAR2,
5144   I_USER_ID		IN	NUMBER,
5145   I_LOGIN_ID    	IN	NUMBER,
5146   I_REQ_ID		IN	NUMBER,
5147   I_PRG_APPL_ID		IN	NUMBER,
5148   I_PRG_ID		IN 	NUMBER,
5149   O_Error_Num		OUT NOCOPY	NUMBER,
5150   O_Error_Code		OUT NOCOPY	VARCHAR2,
5151   O_Error_Message	OUT NOCOPY	VARCHAR2
5152 )IS
5153   l_cost	NUMBER;
5154   l_acct	NUMBER;
5155   l_acct_line_type NUMBER;
5156   l_var		NUMBER;
5157   l_exp		NUMBER;
5158   l_layer_id 	NUMBER;
5159   l_cost_grp_id NUMBER;
5160   l_mat_acct	NUMBER;
5161   l_mat_ovhd_acct NUMBER;
5162   l_res_acct	NUMBER;
5163   l_osp_acct	NUMBER;
5164   l_ovhd_acct	NUMBER;
5165   l_ele_exist	NUMBER;
5166   l_stmt_num 	NUMBER;
5167   l_err_num	NUMBER;
5168   l_err_code	VARCHAR2(240);
5169   l_err_msg	VARCHAR2(240);
5170   process_error	EXCEPTION;
5171   no_acct_error EXCEPTION;
5172   no_txn_det_error EXCEPTION;
5173   l_wms_flg       NUMBER;
5174   l_pjm_flg       NUMBER;
5175   l_ele_acct      NUMBER;
5176   l_return_status VARCHAR2(1);
5177   l_msg_count     NUMBER;
5178   l_msg_data      VARCHAR2(8000);
5179   l_ccga_count    NUMBER;
5180 BEGIN
5181   -- initialize variables;
5182   l_err_num := 0;
5183   l_err_code := '';
5184   l_err_msg := '';
5185   l_cost := '';
5186 
5187   l_stmt_num := 10;
5188 
5189   select decode(i_exp_item, 0, nvl(i_exp_subinv,0), i_exp_item)
5190   into l_exp
5191   from dual;
5192 
5193   if (i_intransit = 1 and i_exp_item = 0) then
5194     l_exp := 0;
5195     l_acct_line_type := 14;
5196   elsif (l_exp = 1) then
5197     l_acct_line_type := 2;
5198   else
5199     l_acct_line_type := 1;
5200   end if;
5201 
5202   l_stmt_num := 20;
5203 
5204   l_cost_grp_id := i_cost_grp_id;
5205   l_layer_id := -1;
5206   select count(*)
5207   into l_ele_exist
5208   from mtl_cst_actual_cost_details mcacd
5209   where mcacd.transaction_id = i_txn_id
5210   and mcacd.organization_id = i_org_id
5211   and mcacd.layer_id = l_layer_id;
5212 
5213   if (l_ele_exist = 0) then
5214     raise no_txn_det_error;
5215   end if;
5216 
5217   l_stmt_num := 30;
5218   if(wms_install.check_install(l_return_status, l_msg_count, l_msg_data, i_org_id)) then
5219     l_wms_flg := 1;
5220   else
5221     l_wms_flg := 0;
5222   end if;
5223 
5224   l_stmt_num := 40;
5225   SELECT decode(nvl(cost_group_accounting,0),0,0,
5226          decode(nvl(project_reference_enabled,0),1,1,0))
5227   INTO l_pjm_flg
5228   FROM MTL_PARAMETERS
5229   WHERE organization_id = i_org_id;
5230 
5231   /* Changes for PJM Standard Costing. */
5232   if (l_wms_flg=0 AND l_pjm_flg=0 AND i_subinv is not null) then
5233     l_stmt_num := 50;
5234 
5235     SELECT
5236            nvl(material_account, -1),
5237            nvl(material_overhead_account, -1),
5238            nvl(resource_account, -1),
5239            nvl(outside_processing_account, -1),
5240            nvl(overhead_account, -1)
5241     INTO l_mat_acct, l_mat_ovhd_acct, l_res_acct, l_osp_acct, l_ovhd_acct
5242     FROM mtl_secondary_inventories
5243     WHERE organization_id = i_org_id
5244     and  secondary_inventory_name = i_subinv;
5245   else
5246     if (l_cost_grp_id = 1) then
5247       l_stmt_num := 70;
5248 
5249       SELECT
5250             nvl(material_account, -1),
5251             nvl(material_overhead_account, -1),
5252             nvl(resource_account, -1),
5253             nvl(outside_processing_account, -1),
5254             nvl(overhead_account, -1)
5255       INTO l_mat_acct, l_mat_ovhd_acct, l_res_acct, l_osp_acct, l_ovhd_acct
5256       FROM mtl_parameters
5257       WHERE organization_id = i_org_id;
5258     else
5259       l_stmt_num := 80;
5260 
5261       SELECT count(*)
5262       INTO l_ccga_count
5263       FROM cst_cost_group_accounts
5264       WHERE cost_group_id = l_cost_grp_id
5265       AND organization_id = i_org_id;
5266 
5267       if (l_ccga_count = 0) then
5268         l_err_num := 9999;
5269         l_err_msg := 'CSTPACDP.inventory_accounts_std(80): Unabled to obtain elemental accounts';
5270         raise process_error;
5271       end if;
5272 
5273       l_stmt_num := 90;
5274       SELECT
5275             nvl(material_account, -1),
5276             nvl(material_overhead_account, -1),
5277             nvl(resource_account, -1),
5278             nvl(outside_processing_account, -1),
5279             nvl(overhead_account, -1)
5280       INTO l_mat_acct, l_mat_ovhd_acct, l_res_acct, l_osp_acct, l_ovhd_acct
5281       FROM cst_cost_group_accounts
5282       WHERE organization_id = i_org_id
5283       AND cost_group_id = l_cost_grp_id;
5284     end if;
5285   end if;
5286 
5287   FOR cost_element IN 1..5 loop
5288     l_cost := NULL;
5289 
5290     l_stmt_num := 100;
5291 
5292     select sum(actual_cost), nvl(sum(variance_amount),0)
5293     into l_cost, l_var
5294     from mtl_cst_actual_cost_details
5295     where transaction_id = i_txn_id
5296     and organization_id = i_org_id
5297     and layer_id = l_layer_id
5298     and cost_element_id = cost_element;
5299 
5300     if (l_cost is not NULL) then
5301       l_acct := CSTPSCHK.std_get_account_id(i_org_id, i_txn_id, sign(i_p_qty), l_acct_line_type,
5302                                             cost_element, 0, i_subinv, l_cost_grp_id, l_exp,
5303                                             i_snd_rcv, l_err_num, l_err_code, l_err_msg);
5304       -- check error
5305       if(l_err_num<>0) then
5306         raise process_error;
5307       end if;
5308 
5309       if (l_acct = -1) then
5310         l_stmt_num := 110;
5311         select decode(l_exp, 1, i_exp_acct,
5312         decode(cost_element, 1, l_mat_acct,
5313 				  2, l_mat_ovhd_acct,
5314 				  3, l_res_acct,
5315 				  4, l_osp_acct,
5316 				  5, l_ovhd_acct))
5317         into l_acct
5318         from dual;
5319       end if;
5320 
5321        l_stmt_num := 120;
5322     -- Only insert into mta if the cost elemental detail exist in cacd.
5323       insert_account(i_org_id, i_txn_id, i_item_id, i_p_qty * l_cost - l_var,
5324 		i_p_qty, l_acct, i_sob_id, l_acct_line_type,
5325 		cost_element, NULL,
5326 		i_txn_date, i_txn_src_id, i_src_type_id,
5327 		i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
5328 		1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
5329 		l_err_num, l_err_code, l_err_msg);
5330 
5331       -- check error
5332       if(l_err_num<>0) then
5333       raise process_error;
5334       end if;
5335     end if;
5336   end loop;
5337 
5338   EXCEPTION
5339 
5340  when process_error then
5341  rollback;
5342  O_error_num := l_err_num;
5343  O_error_code := l_err_code;
5344  O_error_message := l_err_msg;
5345 
5346  when no_acct_error then
5347  rollback;
5348  O_error_num := 9999;
5349  O_error_code := 'CST_NO_CG_ACCTS';
5350  FND_MESSAGE.set_name('BOM', 'CST_NO_CG_ACCTS');
5351  O_error_message := FND_MESSAGE.Get;
5352 
5353  when no_txn_det_error then
5354  rollback;
5355  O_error_num := 9999;
5356  O_error_code := 'CST_NO_TXN_DET';
5357  FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
5358  O_error_message := FND_MESSAGE.Get;
5359 
5360  when others then
5361 
5362  rollback;
5363  O_error_num := SQLCODE;
5364  O_error_message := 'CSTPACDP.inventory_accounts' || to_char(l_stmt_num) ||
5365                      substr(SQLERRM,1,180);
5366 
5367 END inventory_accounts_std;
5368 
5369 procedure inventory_accounts(
5370   I_ORG_ID		IN	NUMBER,
5371   I_TXN_ID		IN 	NUMBER,
5372   I_COMM_ISS_FLAG       IN      NUMBER,
5373   I_COST_TXN_ACTION_ID  IN      NUMBER,
5374   I_COST_GRP_ID		IN	NUMBER,
5375   I_ITEM_ID		IN	NUMBER,
5376   I_P_QTY		IN	NUMBER,
5377   I_SOB_ID		IN	NUMBER,
5378   I_TXN_DATE		IN	DATE,
5379   I_TXN_SRC_ID		IN	NUMBER,
5380   I_SRC_TYPE_ID		IN	NUMBER,
5381   I_EXP_ITEM		IN	NUMBER,
5382   I_EXP_SUBINV		IN	NUMBER,
5383   I_EXP_ACCT		IN	NUMBER,
5384   I_SUBINV		IN	VARCHAR2,
5385   I_INTRANSIT		IN	NUMBER,
5386   I_SND_RCV		IN	NUMBER,
5387   I_PRI_CURR		IN	VARCHAR2,
5388   I_ALT_CURR		IN	VARCHAR2,
5389   I_CONV_DATE		IN	DATE,
5390   I_CONV_RATE		IN	NUMBER,
5391   I_CONV_TYPE		IN	VARCHAR2,
5392   I_USER_ID		IN	NUMBER,
5393   I_LOGIN_ID    	IN	NUMBER,
5394   I_REQ_ID		IN	NUMBER,
5395   I_PRG_APPL_ID		IN	NUMBER,
5396   I_PRG_ID		IN 	NUMBER,
5397   O_Error_Num		OUT NOCOPY	NUMBER,
5398   O_Error_Code		OUT NOCOPY	VARCHAR2,
5399   O_Error_Message	OUT NOCOPY	VARCHAR2
5400 )IS
5401   l_cost	NUMBER;
5402   l_acct	NUMBER;
5403   l_acct_line_type NUMBER;
5404   l_var		NUMBER;
5405   l_exp		NUMBER;
5406   l_layer_id 	NUMBER;
5407   l_cost_grp_id NUMBER;
5408   l_mat_acct	NUMBER;
5409   l_mat_ovhd_acct NUMBER;
5410   l_res_acct	NUMBER;
5411   l_osp_acct	NUMBER;
5412   l_ovhd_acct	NUMBER;
5413   l_ele_exist	NUMBER;
5414   l_acct_exist  NUMBER;
5415   l_stmt_num 	NUMBER;
5416   l_err_num	NUMBER;
5417   l_debug       VARCHAR2(1);
5418   l_err_code	VARCHAR2(240);
5419   l_err_msg	VARCHAR2(240);
5420 -- borrow / payback
5421   l_txn_type	NUMBER;
5422   l_txn_action_id  NUMBER;
5423   l_to_layer_id NUMBER;
5424   l_payback_var NUMBER;
5425 -- borrow / payback end
5426   process_error	EXCEPTION;
5427   no_acct_error EXCEPTION;
5428   no_txn_det_error EXCEPTION;
5429 BEGIN
5430   -- initialize variables;
5431   l_err_num := 0;
5432   l_err_code := '';
5433   l_err_msg := '';
5434   l_cost := '';
5435 
5436   l_stmt_num := 10;
5437 
5438   fnd_file.put_line(fnd_file.log, 'In Inventory_accounts');
5439 
5440   l_debug := fnd_profile.value('MRP_DEBUG');
5441 
5442   select decode(i_exp_item, 0, nvl(i_exp_subinv,0), i_exp_item)
5443   into l_exp
5444   from dual;
5445 
5446   -- Currently for an average cost org the inventory valuation accout and
5447   -- the intransit account is the same.
5448   l_stmt_num := 15;
5449 
5450   if (i_cost_grp_id = 1) then
5451     select count(*)
5452     into   l_acct_exist
5453     from   mtl_parameters
5454     where  organization_id = i_org_id;
5455   else
5456     select count(*)
5457     into   l_acct_exist
5458     from   cst_cost_group_accounts
5459     where  organization_id = i_org_id
5460     and    cost_group_id = i_cost_grp_id;
5461   end if;
5462 
5463   if (l_acct_exist = 0) then
5464     raise no_acct_error;
5465   end if;
5466 
5467   l_stmt_num := 20;
5468 
5469   -- Fix bug 894256 the intransit CG accounts can be from common or project CG
5470   l_cost_grp_id := i_cost_grp_id;
5471 
5472   l_stmt_num := 23;
5473 
5474   if (l_cost_grp_id = 1) then
5475     select nvl(material_account,-1),
5476            nvl(material_overhead_account,-1),
5477            nvl(resource_account,-1),
5478            nvl(outside_processing_account,-1),
5479            nvl(overhead_account,-1)
5480     into   l_mat_acct,
5481            l_mat_ovhd_acct,
5482            l_res_acct,
5483            l_osp_acct,
5484            l_ovhd_acct
5485     from   mtl_parameters
5486     where  organization_id = i_org_id;
5487   else
5488     select nvl(material_account,-1),
5489            nvl(material_overhead_account,-1),
5490            nvl(resource_account,-1),
5491            nvl(outside_processing_account,-1),
5492            nvl(overhead_account,-1)
5493     into   l_mat_acct,
5494            l_mat_ovhd_acct,
5495            l_res_acct,
5496            l_osp_acct,
5497            l_ovhd_acct
5498     from   cst_cost_group_accounts
5499     where  organization_id = i_org_id
5500     and    cost_group_id = l_cost_grp_id;
5501   end if;
5502 
5503   l_stmt_num := 25;
5504 
5505   select layer_id
5506   into l_layer_id
5507   from cst_quantity_layers
5508   where inventory_item_id = i_item_id
5509   and organization_id = i_org_id
5510   and cost_group_id = i_cost_grp_id;
5511 
5512   if (l_debug = 'Y') then
5513   fnd_file.put_line(fnd_file.log, 'layer_id:' || l_layer_id);
5514   end if;
5515 
5516 
5517   if (i_intransit = 1 and i_exp_item = 0) then
5518     l_exp := 0;
5519     l_acct_line_type := 14;
5520   elsif (l_exp = 1) then
5521     l_acct_line_type := 2;
5522   else
5523     l_acct_line_type := 1;
5524   end if;
5525 
5526   if (l_debug = 'Y') then
5527   fnd_file.put_line(fnd_file.log, 'l_acct_line_type:' || l_acct_line_type);
5528   end if;
5529 
5530 
5531   l_stmt_num := 30;
5532 
5533   select count(*)
5534   into l_ele_exist
5535   from mtl_cst_actual_cost_details mcacd
5536   where mcacd.transaction_id = i_txn_id
5537   and mcacd.organization_id = i_org_id
5538   and mcacd.layer_id = l_layer_id
5539   and nvl(mcacd.transaction_action_id,-99) =
5540   nvl(decode(i_comm_iss_flag,1,
5541 	     i_cost_txn_action_id,mcacd.transaction_action_id),-99);
5542 
5543 -- borrow payback
5544   l_stmt_num := 35;
5545   select mmt.transaction_type_id, mmt.transaction_action_id
5546     into l_txn_type, l_txn_action_id
5547     from mtl_material_transactions mmt
5548     where mmt.transaction_id = i_txn_id;
5549 -- borrow payback end
5550 
5551 
5552 
5553   if (l_ele_exist = 0) then
5554     raise no_txn_det_error;
5555   end if;
5556 
5557   FOR cost_element IN 1..5 loop
5558     l_cost := NULL;
5559 
5560       if (l_debug = 'Y') then
5561         fnd_file.put_line(fnd_file.log, 'in cost element loop');
5562       end if;
5563 
5564 
5565     l_stmt_num := 40;
5566 
5567 
5568     select sum(actual_cost), nvl(sum(variance_amount),0)
5569     into l_cost, l_var
5570     from mtl_cst_actual_cost_details
5571     where transaction_id = i_txn_id
5572     and organization_id = i_org_id
5573     and layer_id = l_layer_id
5574     and cost_element_id = cost_element
5575     and nvl(transaction_action_id,-99) =
5576         nvl(decode(i_comm_iss_flag,1,
5577              i_cost_txn_action_id,transaction_action_id),-99);
5578 
5579 
5580     if (l_cost is not NULL) then
5581 
5582         if (l_debug = 'Y') then
5583           fnd_file.put_line(fnd_file.log, 'org-txn-i_p_qty-cost_element-i_subinv');
5584           fnd_file.put_line(fnd_file.log, i_org_id || '-' || i_txn_id || '-' ||
5585                            i_p_qty || '-' || i_subinv);
5586         end if;
5587       l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
5588 				sign(i_p_qty), l_acct_line_type,
5589 				cost_element, NULL, i_subinv,
5590 				l_exp, i_snd_rcv,
5591 				l_err_num, l_err_code, l_err_msg);
5592 
5593       -- check error
5594       if(l_err_num<>0) then
5595       raise process_error;
5596       end if;
5597 
5598       if (l_debug = 'Y') then
5599        fnd_file.put_line(fnd_file.log, 'Calling Insert Account');
5600       end if;
5601 
5602 
5603       if (l_acct = -1) then
5604 	l_stmt_num := 50;
5605         select decode(l_exp, 1, i_exp_acct,
5606 		decode(cost_element, 1, l_mat_acct,
5607 				  2, l_mat_ovhd_acct,
5608 				  3, l_res_acct,
5609 				  4, l_osp_acct,
5610 				  5, l_ovhd_acct))
5611         into l_acct
5612         from dual;
5613       end if;
5614 
5615        l_stmt_num := 56;
5616     -- Only insert into mta if the cost elemental detail exist in cacd.
5617       insert_account(i_org_id, i_txn_id, i_item_id, i_p_qty * l_cost - l_var,
5618 		i_p_qty, l_acct, i_sob_id, l_acct_line_type,
5619 		cost_element, NULL,
5620 		i_txn_date, i_txn_src_id, i_src_type_id,
5621 		i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
5622 		1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
5623 		l_err_num, l_err_code, l_err_msg);
5624 
5625       -- check error
5626       if(l_err_num<>0) then
5627       raise process_error;
5628       end if;
5629     end if;
5630   end loop;
5631 
5632   l_stmt_num := 60;
5633 
5634   select nvl(sum(variance_amount),0)
5635     into l_var
5636     from mtl_cst_actual_cost_details cacd
5637     where transaction_id = i_txn_id
5638     and organization_id = i_org_id
5639     and layer_id = l_layer_id
5640     and nvl(transaction_action_id,-99) =
5641         nvl(decode(i_comm_iss_flag,1,
5642                 i_cost_txn_action_id,transaction_action_id),-99);
5643 
5644 
5645   if (l_exp <> 1 and l_var <> 0) then /* Not expense */
5646        -- Insert into variance account.
5647     l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
5648    				sign(i_p_qty), 13,
5649 				NULL, NULL, i_subinv,
5650 				0, i_snd_rcv, l_err_num, l_err_code,
5651 				l_err_msg);
5652              -- check error
5653        if(l_err_num<>0) then
5654        raise process_error;
5655        end if;
5656 
5657     l_stmt_num := 70;
5658 
5659     if (l_acct = -1) then
5660       if (i_cost_grp_id = 1) then
5661         select nvl(average_cost_var_account,-1)
5662         into   l_acct
5663         from   mtl_parameters
5664         where  organization_id = i_org_id;
5665       else
5666         select nvl(average_cost_var_account,-1)
5667         into   l_acct
5668         from   cst_cost_group_accounts
5669         where  cost_group_id = i_cost_grp_id
5670         and    organization_id = i_org_id;
5671       end if;
5672     end if;
5673 
5674     insert_account(i_org_id, i_txn_id, i_item_id, l_var,
5675 	 	i_p_qty, l_acct, i_sob_id, 13,
5676 		NULL, NULL,
5677 		i_txn_date, i_txn_src_id, i_src_type_id,
5678 		i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
5679 		1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
5680 		l_err_num, l_err_code, l_err_msg);
5681 
5682          -- check error
5683          if(l_err_num<>0) then
5684          raise process_error;
5685          end if;
5686   end if;
5687 
5688 -- borrow payback
5689   if (l_txn_type = 68) and (l_txn_action_id = 2) then
5690      l_stmt_num := 80;
5691      select nvl(max(layer_id), l_layer_id)
5692        into l_to_layer_id
5693        from mtl_cst_actual_cost_details
5694        where transaction_id = i_txn_id
5695        and layer_id <> l_layer_id;
5696 
5697      if ((l_layer_id = l_to_layer_id) OR ((l_layer_id <> l_to_layer_id) and (i_p_qty < 0))) then
5698                                              -- do credit / debit
5699                                              -- only do dist. for var on payback side
5700 
5701         for cost_element in 1..5 loop
5702            l_stmt_num := 100;
5703            select nvl(sum(payback_variance_amount),0)
5704              into l_payback_var
5705              from mtl_cst_actual_cost_details cacd
5706              where transaction_id = i_txn_id
5707              and organization_id = i_org_id
5708              and layer_id = l_layer_id
5709              and cost_element_id = cost_element;
5710 
5711       l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
5712 				sign(i_p_qty), 13,
5713 				cost_element, NULL, i_subinv,
5714 				0, i_snd_rcv,
5715 				l_err_num, l_err_code, l_err_msg);
5716       -- check error
5717       if(l_err_num<>0) then
5718       raise process_error;
5719       end if;
5720 
5721            if (l_acct = -1 AND i_cost_grp_id <> 1) then
5722            -- if cost_grp_id is 1, then payback variance accountS would remain -1, since
5723            -- there are no borrow payback variance account defined in mtl_parameters
5724               l_stmt_num := 110;
5725               if cost_element = 1 then
5726                  select nvl(payback_mat_var_account,-1)
5727                    into l_acct
5728                    from cst_cost_group_accounts
5729                   where organization_id = i_org_id
5730                     and cost_group_id = i_cost_grp_id;
5731               elsif cost_element = 2 then
5732                  select nvl(payback_moh_var_account,-1)
5733                    into l_acct
5734                    from cst_cost_group_accounts
5735                   where organization_id = i_org_id
5736                     and cost_group_id = i_cost_grp_id;
5737               elsif cost_element = 3 then
5738                  select nvl(payback_res_var_account,-1)
5739                    into l_acct
5740                    from cst_cost_group_accounts
5741                   where organization_id = i_org_id
5742                     and cost_group_id = i_cost_grp_id;
5743               elsif cost_element = 4 then
5744                  select nvl(payback_osp_var_account,-1)
5745                    into l_acct
5746                    from cst_cost_group_accounts
5747                   where organization_id = i_org_id
5748                     and cost_group_id = i_cost_grp_id;
5749               elsif cost_element = 5 then
5750                  select nvl(payback_ovh_var_account,-1)
5751                    into l_acct
5752                    from cst_cost_group_accounts
5753                   where organization_id = i_org_id
5754                     and cost_group_id = i_cost_grp_id;
5755               end if;  -- end selecting different variance account
5756            end if; -- end if l_acct <> -1 then use hook account
5757 
5758            l_stmt_num := 120;
5759 	   if (l_payback_var <>0) then
5760               insert_account(i_org_id, i_txn_id, i_item_id, -1 * i_p_qty * l_payback_var,
5761 	     	   i_p_qty, l_acct, i_sob_id, 13,
5762 		   cost_element, NULL,
5763 		   i_txn_date, i_txn_src_id, i_src_type_id,
5764 		   i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
5765 		   1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
5766 		   l_err_num, l_err_code, l_err_msg);
5767                  -- check error
5768               if(l_err_num<>0) then
5769               raise process_error;
5770               end if;
5771            end if;
5772         end loop;  -- done looping cost elements
5773      end if; -- different layer
5774    end if;
5775 -- borrow payback / need to modify cst_avg_dist_accts_v for the new account
5776 
5777 
5778 
5779   EXCEPTION
5780 
5781  when process_error then
5782  rollback;
5783    if (l_debug = 'Y') then
5784    fnd_file.put_line (fnd_file.log, 'Inventory_accounts' || to_char(l_stmt_num)
5785                                      || substr(SQLERRM,1,180));
5786    end if;
5787    O_error_num := l_err_num;
5788    O_error_code := l_err_code;
5789    O_error_message := l_err_msg;
5790 
5791  when no_acct_error then
5792  rollback;
5793  O_error_num := 9999;
5794  O_error_code := 'CST_NO_CG_ACCTS';
5795  FND_MESSAGE.set_name('BOM', 'CST_NO_CG_ACCTS');
5796  O_error_message := FND_MESSAGE.Get;
5797 
5798  when no_txn_det_error then
5799  rollback;
5800  O_error_num := 9999;
5801  O_error_code := 'CST_NO_TXN_DET';
5802  FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
5803  O_error_message := FND_MESSAGE.Get;
5804 
5805  when others then
5806 
5807  rollback;
5808  if (l_debug = 'Y') then
5809  fnd_file.put_line (fnd_file.log, 'Inventory_accounts' || to_char(l_stmt_num)
5810                                      || substr(SQLERRM,1,180)) ;
5811  end if;
5812  O_error_num := SQLCODE;
5813  O_error_message := 'CSTPACDP.inventory_accounts' || to_char(l_stmt_num) ||
5814                      substr(SQLERRM,1,180);
5815 
5816 END inventory_accounts;
5817 
5818 procedure distribute_accounts(
5819   I_ORG_ID		IN	NUMBER,
5820   I_TXN_ID		IN 	NUMBER,
5821   I_COMM_ISS_FLAG	IN	NUMBER,
5822   I_COST_GRP_ID		IN	NUMBER,
5823   I_COST_TXN_ACTION_ID	IN	NUMBER,
5824   I_ITEM_ID		IN	NUMBER,
5825   I_P_QTY		IN	NUMBER,
5826   I_ACCT_LINE_TYPE	IN	NUMBER,
5827   I_ELEMENTAL		IN	NUMBER,
5828   I_OVHD_ABSP		IN	NUMBER,
5829   I_ACCT		IN	NUMBER,
5830   I_MAT_ACCT		IN	NUMBER,
5831   I_MAT_OVHD_ACCT	IN	NUMBER,
5832   I_RES_ACCT		IN	NUMBER,
5833   I_OSP_ACCT		IN	NUMBER,
5834   I_OVHD_ACCT		IN	NUMBER,
5835   I_SOB_ID		IN	NUMBER,
5836   I_TXN_DATE		IN	DATE,
5837   I_TXN_SRC_ID		IN	NUMBER,
5838   I_SRC_TYPE_ID		IN	NUMBER,
5839   I_SUBINV		IN	VARCHAR2,
5840   I_SND_RCV		IN	NUMBER,
5841   I_PRI_CURR		IN	VARCHAR2,
5842   I_ALT_CURR		IN	VARCHAR2,
5843   I_CONV_DATE		IN	DATE,
5844   I_CONV_RATE		IN	NUMBER,
5845   I_CONV_TYPE		IN	VARCHAR2,
5846   I_USER_ID		IN	NUMBER,
5847   I_LOGIN_ID    	IN	NUMBER,
5848   I_REQ_ID		IN	NUMBER,
5849   I_PRG_APPL_ID		IN	NUMBER,
5850   I_PRG_ID		IN 	NUMBER,
5851   O_Error_Num		OUT NOCOPY	NUMBER,
5852   O_Error_Code		OUT NOCOPY	VARCHAR2,
5853   O_Error_Message	OUT NOCOPY	VARCHAR2,
5854   I_COGS_OM_LINE_ID     IN      NUMBER
5855 )IS
5856   l_ele_exist   NUMBER;
5857   l_acct	NUMBER;
5858   l_cost	NUMBER;
5859   l_stmt_num	NUMBER;
5860   l_layer_id	NUMBER;
5861   process_error	EXCEPTION;
5862   no_txn_det_error EXCEPTION;
5863   l_err_num	NUMBER;
5864   l_err_code	VARCHAR2(240);
5865   l_err_msg	VARCHAR2(240);
5866   l_debug       VARCHAR2(1);
5867   -- dropshipment project addition
5868   l_cost_method NUMBER;
5869   l_ce_round_cost NUMBER;/* Bug 6030328*/
5870 
5871   l_elemental_cost      number_table;
5872   l_return_status       VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
5873   l_msg_count           NUMBER;
5874   l_msg_data            VARCHAR2(2000);
5875 
5876 BEGIN
5877   -- initialize variables;
5878   l_err_num := 0;
5879   l_err_code := '';
5880   l_err_msg := '';
5881   l_debug := fnd_profile.value('MRP_DEBUG');
5882 
5883 
5884     fnd_file.put_line(fnd_file.log,'In distribute_accounts');
5885     fnd_file.put_line(fnd_file.log,'i_org_id:' || i_org_id );
5886     fnd_file.put_line(fnd_file.log,'i_txn_id:' || i_txn_id);
5887     fnd_file.put_line(fnd_file.log,'i_comm_iss_flag:' || i_comm_iss_flag);
5888     fnd_file.put_line(fnd_file.log,'i_acct_linr_type:' || i_acct_line_type);
5889     fnd_file.put_line(fnd_file.log,'i_elemental:' || i_elemental);
5890     fnd_file.put_line(fnd_file.log,'i_ovhd_absp:' || i_ovhd_absp);
5891 
5892 
5893     -- For the logical txns introduced in the dropshipment project, standard costing
5894     -- orgs will also call the average cost distribution processor. A few changes
5895     -- are needed to distribute_accounts() because of this so l_cost_method stores
5896     -- the costing method.
5897     l_stmt_num := 0;
5898     select primary_cost_method
5899     into l_cost_method
5900     from mtl_parameters
5901     where organization_id = i_org_id;
5902 
5903   if(i_elemental = 1) then
5904 
5905 
5906     l_stmt_num := 5;
5907 
5908  -- For regular WIP txns this is not reqd. However for a CITW txn we need
5909  -- to select layer_id and join to it in mcacd.
5910 
5911     if (i_comm_iss_flag = 1) then
5912       select layer_id into l_layer_id
5913       from cst_quantity_layers where
5914       inventory_item_id = i_item_id and
5915       organization_id = i_org_id and
5916       cost_group_id = i_cost_grp_id;
5917     end if;
5918 
5919     l_stmt_num := 10;
5920 
5921  -- Join on layer_id and transaction_action_id only for a CITW transaction.
5922 
5923     select count(*)
5924     into l_ele_exist
5925     from mtl_cst_actual_cost_details
5926     where transaction_id = i_txn_id
5927     and organization_id = i_org_id
5928     and decode(i_comm_iss_flag,1,l_layer_id,layer_id) = layer_id
5929     and nvl(decode(i_comm_iss_flag,1,i_cost_txn_action_id,transaction_action_id),-99)=
5930 					nvl(transaction_action_id,-99);
5931 
5932     if (l_ele_exist = 0) then
5933       raise no_txn_det_error;
5934     end if;
5935 
5936     FOR cost_element IN 1..5 loop
5937       l_cost := NULL;
5938 
5939       -- i_ovhd_absp indicates which level of material overhead we are
5940       -- absorbtion and therefore need to go in an absorption account.
5941       -- 2 means both levels and 1 means this level only.
5942 
5943       l_stmt_num := 20;
5944 
5945       select sum(actual_cost)
5946       into l_elemental_cost(cost_element)
5947       from mtl_cst_actual_cost_details cacd
5948       where transaction_id = i_txn_id
5949       and organization_id = i_org_id
5950       and cost_element_id = cost_element
5951     and decode(i_comm_iss_flag,1,l_layer_id,layer_id) = layer_id and
5952  nvl(decode(i_comm_iss_flag,1,i_cost_txn_action_id,transaction_action_id),-99)=
5953                                         nvl(transaction_action_id,-99)
5954       and (cost_element_id <> 2
5955            OR
5956            (cost_element_id = 2
5957             and level_type = decode(i_ovhd_absp,1,2,2,0,level_type)));
5958 
5959     fnd_file.put_line(fnd_file.log,'actual_cost:' || l_elemental_cost(cost_element) );
5960 
5961       if (l_elemental_cost(cost_element) is not null) then
5962 
5963         -- dropshipment changes
5964         if (l_cost_method <> 1) then
5965           l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
5966 				sign(i_p_qty),
5967 				i_acct_line_type,
5968 				cost_element, NULL, i_subinv,
5969 				0, i_snd_rcv, l_err_num, l_err_code,
5970 				l_err_msg);
5971         else
5972           l_acct := CSTPSCHK.std_get_account_id(i_org_id, i_txn_id, sign(i_p_qty),
5973                          i_acct_line_type, cost_element, 0, i_subinv, i_cost_grp_id,
5974                          0, i_snd_rcv, l_err_num, l_err_code, l_err_msg);
5975         end if;
5976 
5977     -- check error
5978       if(l_err_num<>0) then
5979       raise process_error;
5980       end if;
5981 
5982         if (l_acct = -1) then
5983 
5984 	  l_stmt_num := 40;
5985           select decode(cost_element, 1, i_mat_acct,
5986 				  2, i_mat_ovhd_acct,
5987 				  3, i_res_acct,
5988 				  4, i_osp_acct,
5989 				  5, i_ovhd_acct)
5990           into l_acct
5991           from dual;
5992         end if;
5993 
5994 	fnd_file.put_line(fnd_file.log, 'ElementID: ' || cost_element || ' Amt: ' || (i_p_qty * l_elemental_cost(cost_element)) ||
5995 					' Acct: ' || l_acct);
5996 
5997         insert_account(i_org_id, i_txn_id, i_item_id, i_p_qty * l_elemental_cost(cost_element),
5998 		i_p_qty, l_acct, i_sob_id, i_acct_line_type,
5999 		cost_element, NULL,
6000 		i_txn_date, i_txn_src_id, i_src_type_id,
6001 		i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
6002 		1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
6003 		l_err_num, l_err_code, l_err_msg);
6004 
6005 	      -- check error
6006       if(l_err_num<>0) then
6007       raise process_error;
6008       end if;
6009 
6010       else
6011          l_elemental_cost(cost_element) := 0;
6012       end if;
6013     end loop;
6014 
6015     -- OM transactions (Sales Order, RMAs) are always distributed elementally
6016     -- which is why this IF statement falls here and not below.
6017     IF (i_acct_line_type = 36) THEN
6018 
6019 	  l_stmt_num := 45;
6020       -- The following call to Record_SoIssueCost will insert the unit
6021       -- costs of the sales order issue into CRCML so that subsequent
6022       -- COGS recognition events can use these same unit costs.
6023 
6024       CST_RevenueCogsMatch_PVT.Record_SoIssueCost(
6025                 p_api_version        => 1.0,
6026                 p_init_msg_list      => FND_API.G_TRUE,
6027                 p_user_id            => i_user_id,
6028                 p_login_id           => i_login_id,
6029                 p_request_id         => i_req_id,
6030                 p_pgm_app_id         => i_prg_appl_id,
6031                 p_pgm_id             => i_prg_id,
6032                 x_return_status      => l_return_status,
6033                 x_msg_count          => l_msg_count,
6034                 x_msg_data           => l_msg_data,
6035                 p_cogs_om_line_id    => I_COGS_OM_LINE_ID,
6036                 p_pac_cost_type_id   => NULL,
6037                 p_unit_material_cost => l_elemental_cost(1),
6038                 p_unit_moh_cost      => l_elemental_cost(2),
6039                 p_unit_resource_cost => l_elemental_cost(3),
6040                 p_unit_op_cost       => l_elemental_cost(4),
6041                 p_unit_overhead_cost => l_elemental_cost(5),
6042                 p_unit_cost          => l_elemental_cost(1) + l_elemental_cost(2) + l_elemental_cost(3) + l_elemental_cost(4) + l_elemental_cost(5),
6043                 p_txn_quantity       => i_p_qty);
6044 
6045       IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6046          l_err_num := -1;
6047          FND_MSG_PUB.count_and_get
6048          (  p_count   => l_msg_count,
6049             p_data    => l_msg_data,
6050             p_encoded => FND_API.g_false
6051          );
6052          IF (l_msg_count = 1) THEN
6053             l_err_msg := substr(l_msg_data,1,240);
6054          ELSE
6055             l_err_msg := 'Failure in procedure CST_RevenueCogsMatch_PVT.Record_SoIssueCost()';
6056          END IF;
6057          raise process_error;
6058       END IF;
6059 
6060 	  l_stmt_num := 48;
6061       /* Set the costed flag to YES in cst_cogs_events for this sales order issue */
6062       UPDATE cst_cogs_events
6063       SET costed = NULL,
6064           last_update_date = sysdate,
6065           last_updated_by = i_user_id,
6066           last_update_login = i_login_id,
6067           request_id = i_req_id
6068       WHERE cogs_om_line_id = i_cogs_om_line_id
6069       AND   mmt_transaction_id = i_txn_id;
6070 
6071     END IF; -- Deferred COGS Accounting
6072   else
6073 
6074     l_stmt_num := 50;
6075 
6076     /* Bug 6030328 the cost should be rounded at elemental level*/
6077     l_cost := 0;
6078     l_ce_round_cost :=0;
6079     FOR cost_element IN 1..5 LOOP
6080     BEGIN
6081     select decode(c1.minimum_accountable_unit,
6082                   null,ROUND((nvl(sum(actual_cost),0)*abs(i_p_qty)),c1.precision)/abs(i_p_qty),
6083 		  (ROUND((nvl(sum(actual_cost),0)*abs(i_p_qty))/c1.minimum_accountable_unit)*c1.minimum_accountable_unit)/abs(i_p_qty))
6084     into l_ce_round_cost
6085     from mtl_cst_actual_cost_details cacd,
6086          cst_organization_definitions cod,
6087 	 fnd_currencies c1,
6088 	 gl_sets_of_books gsb
6089     where cacd.transaction_id = i_txn_id
6090     and cacd.organization_id = i_org_id
6091     and c1.currency_code = gsb.currency_code
6092     and gsb.set_of_books_id = cod.set_of_books_id
6093     and cod.organization_id = i_org_id
6094     and cacd.cost_element_id = cost_element
6095     and (cost_element_id <> 2
6096          OR
6097          (cost_element_id = 2
6098           and level_type = decode(i_ovhd_absp,1,2,2,0,level_type)))
6099     group by c1.minimum_accountable_unit,
6100              c1.precision;
6101     EXCEPTION
6102       WHEN NO_DATA_FOUND THEN
6103          l_ce_round_cost := 0;
6104     END;
6105     l_cost := l_cost + l_ce_round_cost;
6106     END LOOP;
6107 
6108     fnd_file.put_line(fnd_file.log,'actual_cost(60):' || l_cost );
6109 
6110 
6111     l_stmt_num := 60;
6112 
6113     if (l_cost_method <> 1) then
6114       l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
6115 				sign(i_p_qty),
6116 				i_acct_line_type,
6117 				NULL, NULL, i_subinv,
6118 				0, i_snd_rcv, l_err_num, l_err_code,
6119 				l_err_msg);
6120     else
6121       l_acct := CSTPSCHK.std_get_account_id(i_org_id, i_txn_id, sign(i_p_qty),
6122                          i_acct_line_type, NULL, 0, i_subinv, i_cost_grp_id,
6123                          0, i_snd_rcv, l_err_num, l_err_code, l_err_msg);
6124     end if;
6125 
6126           -- check error
6127       if(l_err_num<>0) then
6128       raise process_error;
6129       end if;
6130 
6131     if (l_acct = -1) then
6132     	l_acct := i_acct;
6133     end if;
6134 
6135     l_stmt_num := 70;
6136 
6137     fnd_file.put_line(fnd_file.log, 'Insert account()');
6138 
6139     insert_account(i_org_id, i_txn_id, i_item_id, i_p_qty * l_cost,
6140 		i_p_qty, l_acct, i_sob_id, i_acct_line_type,
6141 		NULL, NULL,
6142 		i_txn_date, i_txn_src_id, i_src_type_id,
6143 		i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
6144 		1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
6145 		l_err_num, l_err_code, l_err_msg);
6146 
6147         -- check error
6148       if(l_err_num<>0) then
6149       raise process_error;
6150       end if;
6151 
6152   end if;
6153 
6154   -- return normally
6155   O_Error_Num := 0;
6156 
6157   EXCEPTION
6158 
6159  when process_error then
6160  rollback;
6161  O_error_num := l_err_num;
6162  O_error_code := l_err_code;
6163  O_error_message := l_err_msg;
6164 
6165  when no_txn_det_error then
6166  rollback;
6167  O_error_num := 9999;
6168  O_error_code := 'CST_NO_TXN_DET';
6169  FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
6170  O_error_message := FND_MESSAGE.Get;
6171 
6172  when others then
6173  rollback;
6174  O_error_num := SQLCODE;
6175  O_error_message := 'CSTPACDP.distribute_accounts' || to_char(l_stmt_num) ||
6176                      substr(SQLERRM,1,180);
6177 
6178 end distribute_accounts;
6179 
6180 procedure cfm_scrap_dist_accounts(
6181   I_ORG_ID		IN	NUMBER,
6182   I_TXN_ID		IN 	NUMBER,
6183   I_COMM_ISS_FLAG	IN	NUMBER,
6184   I_COST_GRP_ID		IN	NUMBER,
6185   I_COST_TXN_ACTION_ID	IN	NUMBER,
6186   I_ITEM_ID		IN	NUMBER,
6187   I_P_QTY		IN	NUMBER,
6188   I_ACCT_LINE_TYPE	IN	NUMBER,
6189   I_ELEMENTAL		IN	NUMBER,
6190   I_OVHD_ABSP		IN	NUMBER,
6191   I_ACCT		IN	NUMBER,
6192   I_MAT_ACCT		IN	NUMBER,
6193   I_MAT_OVHD_ACCT	IN	NUMBER,
6194   I_RES_ACCT		IN	NUMBER,
6195   I_OSP_ACCT		IN	NUMBER,
6196   I_OVHD_ACCT		IN	NUMBER,
6197   I_SOB_ID		IN	NUMBER,
6198   I_TXN_DATE		IN	DATE,
6199   I_TXN_SRC_ID		IN	NUMBER,
6200   I_SRC_TYPE_ID		IN	NUMBER,
6201   I_SUBINV		IN	VARCHAR2,
6202   I_SND_RCV		IN	NUMBER,
6203   I_PRI_CURR		IN	VARCHAR2,
6204   I_ALT_CURR		IN	VARCHAR2,
6205   I_CONV_DATE		IN	DATE,
6206   I_CONV_RATE		IN	NUMBER,
6207   I_CONV_TYPE		IN	VARCHAR2,
6208   I_USER_ID		IN	NUMBER,
6209   I_LOGIN_ID    	IN	NUMBER,
6210   I_REQ_ID		IN	NUMBER,
6211   I_PRG_APPL_ID		IN	NUMBER,
6212   I_PRG_ID		IN 	NUMBER,
6213   O_Error_Num		OUT NOCOPY	NUMBER,
6214   O_Error_Code		OUT NOCOPY	VARCHAR2,
6215   O_Error_Message	OUT NOCOPY	VARCHAR2
6216 )IS
6217   l_ele_exist   NUMBER;
6218   l_acct	NUMBER;
6219   l_cost	NUMBER;
6220   l_stmt_num	NUMBER;
6221   l_layer_id	NUMBER;
6222   process_error	EXCEPTION;
6223   no_txn_det_error EXCEPTION;
6224   l_err_num	NUMBER;
6225   l_err_code	VARCHAR2(240);
6226   l_err_msg	VARCHAR2(240);
6227 BEGIN
6228   -- initialize variables;
6229   l_err_num := 0;
6230   l_err_code := '';
6231   l_err_msg := '';
6232 
6233      l_stmt_num := 10;
6234      -- Join on layer_id and transaction_action_id only for a CITW transaction.
6235      select count(*)
6236      into l_ele_exist
6237      from mtl_cst_actual_cost_details
6238      where transaction_id = i_txn_id
6239      and organization_id = i_org_id;
6240 
6241     if (l_ele_exist = 0) then
6242       raise no_txn_det_error;
6243     end if;
6244 
6245     FOR cost_element IN 1..5 loop
6246       l_cost := NULL;
6247       -- i_ovhd_absp indicates which level of material overhead we are
6248       -- absorbtion and therefore need to go in an absorption account.
6249       -- 2 means both levels and 1 means this level only.
6250       l_stmt_num := 20;
6251 
6252       if (cost_element = 1) then
6253          select sum(actual_cost)
6254          into l_cost
6255          from mtl_cst_actual_cost_details cacd
6256          where transaction_id = i_txn_id
6257          and organization_id = i_org_id
6258          and (level_type = 2
6259              or (level_type = 1 and cost_element_id =1));
6260       elsif (cost_element <> 2) then
6261          select sum(actual_cost)
6262          into l_cost
6263          from mtl_cst_actual_cost_details cacd
6264          where transaction_id = i_txn_id
6265          and organization_id = i_org_id
6266          and level_type = 1
6267          and cost_element_id = cost_element;
6268       end if;
6269 
6270       if (l_cost is not null AND cost_element <> 2) then
6271          l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
6272 				sign(i_p_qty),
6273 				i_acct_line_type,
6274 				cost_element, NULL, i_subinv,
6275 				0, i_snd_rcv, l_err_num, l_err_code,
6276 				l_err_msg);
6277          -- check error
6278          if(l_err_num<>0) then
6279             raise process_error;
6280          end if;
6281 
6282          l_stmt_num := 40;
6283          select decode(cost_element, 1, i_mat_acct,
6284 		    		     2, i_mat_ovhd_acct,
6285 				     3, i_res_acct,
6286 				     4, i_osp_acct,
6287 				     5, i_ovhd_acct)
6288          into l_acct
6289          from dual;
6290 
6291          insert_account(i_org_id, i_txn_id, i_item_id, i_p_qty * l_cost,
6292 		        i_p_qty, l_acct, i_sob_id, i_acct_line_type,
6293 		        cost_element, NULL,
6294 		        i_txn_date, i_txn_src_id, i_src_type_id,
6295 		        i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
6296 		        1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
6297 		        l_err_num, l_err_code, l_err_msg);
6298 
6299 	  -- check error
6300           if(l_err_num<>0) then
6301              raise process_error;
6302           end if;
6303       end if;
6304     end loop;
6305 
6306   EXCEPTION
6307      when process_error then
6308      rollback;
6309      O_error_num := l_err_num;
6310      O_error_code := l_err_code;
6311      O_error_message := l_err_msg;
6312 
6313      when no_txn_det_error then
6314      rollback;
6315      O_error_num := 9999;
6316      O_error_code := 'CST_NO_TXN_DET';
6317      FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_DET');
6318      O_error_message := FND_MESSAGE.Get;
6319 
6320      when others then
6321      rollback;
6322      O_error_num := SQLCODE;
6323      O_error_message := 'CSTPACDP.cfm_scrap_dist_accounts' || to_char(l_stmt_num) ||
6324                      substr(SQLERRM,1,180);
6325 end cfm_scrap_dist_accounts;
6326 
6327 procedure ovhd_accounts(
6328   I_ORG_ID		IN	NUMBER,
6329   I_TXN_ID		IN 	NUMBER,
6330   I_ITEM_ID		IN	NUMBER,
6331   I_P_QTY		IN	NUMBER,
6332   I_SOB_ID		IN	NUMBER,
6333   I_TXN_DATE		IN	DATE,
6334   I_TXN_SRC_ID		IN	NUMBER,
6335   I_SRC_TYPE_ID		IN	NUMBER,
6336   I_SUBINV		IN	VARCHAR2,
6337   I_SND_RCV		IN	NUMBER,
6338   I_PRI_CURR		IN	VARCHAR2,
6339   I_ALT_CURR		IN	VARCHAR2,
6340   I_CONV_DATE		IN	DATE,
6341   I_CONV_RATE		IN	NUMBER,
6342   I_CONV_TYPE		IN	VARCHAR2,
6343   I_USER_ID		IN	NUMBER,
6344   I_LOGIN_ID    	IN	NUMBER,
6345   I_REQ_ID		IN	NUMBER,
6346   I_PRG_APPL_ID		IN	NUMBER,
6347   I_PRG_ID		IN 	NUMBER,
6348   O_Error_Num		OUT NOCOPY	NUMBER,
6349   O_Error_Code		OUT NOCOPY	VARCHAR2,
6350   O_Error_Message	OUT NOCOPY	VARCHAR2
6351 )IS
6352   l_res_id	NUMBER;
6353   l_cost	NUMBER;
6354   l_acct	NUMBER;
6355   l_stmt_num 	NUMBER;
6356   process_error	EXCEPTION;
6357   l_err_num	NUMBER;
6358   l_err_code	VARCHAR2(240);
6359   l_err_msg	VARCHAR2(240);
6360 
6361   cursor mat_ovhds is
6362     select resource_id, actual_cost
6363     from mtl_actual_cost_subelement
6364     where transaction_id = i_txn_id
6365     and organization_id = i_org_id
6366     and cost_element_id = 2;
6367 BEGIN
6368   -- Initialize variables;
6369   l_err_num := 0;
6370   l_err_code := '';
6371   l_err_msg := '';
6372 
6373   open mat_ovhds;
6374 
6375   loop
6376     fetch mat_ovhds into l_res_id, l_cost;
6377     exit when mat_ovhds%NOTFOUND;
6378 
6379     l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
6380 				sign(i_p_qty), 3,
6381 				2, l_res_id, i_subinv,
6382 				0, i_snd_rcv, l_err_num, l_err_code,
6383 				l_err_msg);
6384           -- check error
6385       if(l_err_num<>0) then
6386       raise process_error;
6387       end if;
6388 
6389     if (l_acct = -1) then
6390 
6391       l_stmt_num := 10;
6392 
6393       select nvl(absorption_account,-1)
6394       into l_acct
6395       from bom_resources
6396       where resource_id = l_res_id
6397       and organization_id = i_org_id;
6398 
6399     end if;
6400 
6401 
6402     insert_account(i_org_id, i_txn_id, i_item_id, i_p_qty * l_cost,
6403 		i_p_qty, l_acct, i_sob_id, 3,
6404 		2, l_res_id,
6405 		i_txn_date, i_txn_src_id, i_src_type_id,
6406 		i_pri_curr, i_alt_curr, i_conv_date, i_conv_rate, i_conv_type,
6407 		1,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
6408 		l_err_num, l_err_code, l_err_msg);
6409 
6410       -- check error
6411       if(l_err_num<>0) then
6412       raise process_error;
6413       end if;
6414 
6415 
6416   end loop;
6417 
6418   EXCEPTION
6419 
6420  when process_error then
6421  rollback;
6422  O_error_num := l_err_num;
6423  O_error_code := l_err_code;
6424  O_error_message := l_err_msg;
6425 
6426  when others then
6427 
6428  rollback;
6429  O_error_num := SQLCODE;
6430  O_error_message := 'CSTPACDP.ovhd_accounts' || to_char(l_stmt_num) ||
6431                      substr(SQLERRM,1,180);
6432 
6433 END ovhd_accounts;
6434 
6435 
6436 procedure encumbrance_account(
6437   I_ORG_ID		IN	NUMBER,
6438   I_TXN_ID		IN 	NUMBER,
6439   I_ITEM_ID		IN	NUMBER,
6440   I_ENC_AMOUNT		IN	NUMBER,
6441   I_P_QTY		IN	NUMBER,
6442   I_ENC_ACCT		IN	NUMBER,
6443   I_SOB_ID		IN	NUMBER,
6444   I_TXN_DATE		IN	DATE,
6445   I_TXN_SRC_ID		IN	NUMBER,
6446   I_SRC_TYPE_ID		IN	NUMBER,
6447   I_PRI_CURR		IN	VARCHAR2,
6448   I_ALT_CURR		IN	VARCHAR2,
6449   I_CONV_DATE		IN	DATE,
6450   I_CONV_RATE		IN	NUMBER,
6451   I_CONV_TYPE		IN	VARCHAR2,
6452   I_USER_ID		IN	NUMBER,
6453   I_LOGIN_ID    	IN	NUMBER,
6454   I_REQ_ID		IN	NUMBER,
6455   I_PRG_APPL_ID		IN	NUMBER,
6456   I_PRG_ID		IN 	NUMBER,
6457   O_Error_Num		OUT NOCOPY	NUMBER,
6458   O_Error_Code		OUT NOCOPY	VARCHAR2,
6459   O_Error_Message	OUT NOCOPY	VARCHAR2
6460 )IS
6461   l_acct	NUMBER;
6462   l_acct_line_type NUMBER;
6463   l_act_flag    NUMBER; -- Flag to indicate the insert_account if encumbrance
6464                         -- is being used.
6465   l_stmt_num 	NUMBER;
6466   l_err_num	NUMBER;
6467   l_err_code	VARCHAR2(240);
6468   l_err_msg	VARCHAR2(240);
6469   process_error	EXCEPTION;
6470 BEGIN
6471   -- initialize variables;
6472   l_err_num := 0;
6473   l_err_code := '';
6474   l_err_msg := '';
6475 
6476   l_acct_line_type := 15;
6477 
6478   l_act_flag := 0;    -- 0 means that encumbrance is being used
6479 
6480   l_stmt_num := 10;
6481 
6482   l_acct := CSTPACHK.get_account_id(i_org_id, i_txn_id,
6483 				sign(i_enc_amount), l_acct_line_type,
6484 				NULL, NULL, NULL,
6485 				0, NULL, l_err_num, l_err_code,
6486 				l_err_msg);
6487 
6488   -- check error
6489   if(l_err_num<>0) then
6490      raise process_error;
6491   end if;
6492 
6493   if (l_acct = -1) then
6494     l_acct := i_enc_acct;
6495   end if;
6496 
6497 
6498   insert_account(i_org_id, i_txn_id, i_item_id, i_enc_amount,
6499 		i_p_qty, l_acct, i_sob_id, l_acct_line_type,
6500 		NULL, NULL,
6501 		i_txn_date, i_txn_src_id, i_src_type_id, i_pri_curr, i_alt_curr,
6502 		i_conv_date, i_conv_rate, i_conv_type,
6503 		l_act_flag,i_user_id, i_login_id, i_req_id, i_prg_appl_id,i_prg_id,
6504 		l_err_num, l_err_code, l_err_msg);
6505 
6506   -- check error
6507   if(l_err_num<>0) then
6508      raise process_error;
6509   end if;
6510 
6511 
6512  EXCEPTION
6513 
6514  when process_error then
6515  rollback;
6516  O_error_num := l_err_num;
6517  O_error_code := l_err_code;
6518  O_error_message := l_err_msg;
6519 
6520  when others then
6521  rollback;
6522  O_error_num := SQLCODE;
6523  O_error_message := 'CSTPACDP.encumbrance_account' || to_char(l_stmt_num) ||
6524                      substr(SQLERRM,1,180);
6525 
6526 END encumbrance_account;
6527 
6528 /*============================================================================+
6529 |    Function to create specific entries in mtl_transaction account given
6530 |    item cost and quantity, etc.
6531 |    Also used in creating encumbrance entries.
6532 |    act_flg    1 = Actual
6533 |               0 = Encumbrance
6534 +============================================================================*/
6535 
6536 PROCEDURE insert_account(
6537   I_ORG_ID		IN	NUMBER,
6538   I_TXN_ID		IN	NUMBER,
6539   I_ITEM_ID		IN	NUMBER,
6540   I_VALUE		IN	NUMBER,
6541   I_QTY			IN	NUMBER,
6542   I_ACCT		IN	NUMBER,
6543   I_SOB_ID		IN	NUMBER,
6544   I_ACCT_LINE_TYPE	IN	NUMBER,
6545   I_COST_ELEMENT_ID	IN	NUMBER,
6546   I_RESOURCE_ID		IN	NUMBER,
6547   I_TXN_DATE		IN	DATE,
6548   I_TXN_SRC_ID		IN	NUMBER,
6549   I_SRC_TYPE_ID		IN	NUMBER,
6550   I_PRI_CURR		IN	VARCHAR2,
6551   I_ALT_CURR		IN	VARCHAR2,
6552   I_CONV_DATE		IN	DATE,
6553   I_CONV_RATE		IN	NUMBER,
6554   I_CONV_TYPE		IN	VARCHAR2,
6555   I_ACT_FLAG            IN      NUMBER,
6556   I_USER_ID		IN	NUMBER,
6557   I_LOGIN_ID    	IN	NUMBER,
6558   I_REQ_ID		IN	NUMBER,
6559   I_PRG_APPL_ID		IN	NUMBER,
6560   I_PRG_ID		IN 	NUMBER,
6561   O_Error_Num		OUT NOCOPY	NUMBER,
6562   O_Error_Code		OUT NOCOPY	VARCHAR2,
6563   O_Error_Message	OUT NOCOPY	VARCHAR2
6564 )IS
6565   l_operating_unit NUMBER;
6566   l_req_enc_id     NUMBER;
6567   l_po_enc_id      NUMBER;
6568   l_stmt_num 	   NUMBER;
6569   l_err_num	   NUMBER;
6570   l_err_code	   VARCHAR2(240);
6571   l_err_msg	   VARCHAR2(240);
6572   invalid_acct_error EXCEPTION;
6573   l_debug          VARCHAR2(1);
6574 
6575   /*Bug #2755069 */
6576   l_txn_type	   NUMBER;
6577   l_ussgl_tc       VARCHAR(30);
6578   l_enc_rev 	   NUMBER;
6579 
6580 BEGIN
6581   -- Initialize variables;
6582   l_err_num := 0;
6583   l_err_code := '';
6584   l_err_msg := '';
6585 
6586   fnd_file.put_line(fnd_file.log, 'In insert accounts');
6587 
6588   if (i_acct = -1) then
6589     raise invalid_acct_error;
6590   end if;
6591 
6592   /* Bug #2755069 */
6593 
6594   l_stmt_num := 1;
6595   select decode(encumbrance_reversal_flag,1,1,2,0,0)
6596   into   l_enc_rev
6597   from   mtl_parameters
6598   where  organization_id = i_org_id;
6599 
6600   l_stmt_num := 2;
6601   IF(l_enc_rev = 1) THEN
6602      SELECT transaction_type_id
6603      INTO l_txn_type
6604      FROM mtl_material_transactions
6605      WHERE transaction_id = i_txn_id;
6606 
6607      IF( (l_txn_type = 18) OR (l_txn_type = 36) OR (l_txn_type = 71) ) THEN
6608 
6609        IF(i_acct_line_type = 1 AND i_cost_element_id = 1) THEN
6610          l_stmt_num := 3;
6611          SELECT rsl.ussgl_transaction_code
6612          INTO l_ussgl_tc
6613          FROM mtl_material_transactions mmt,
6614               rcv_transactions rt,
6615 	      rcv_shipment_lines rsl
6616          WHERE mmt.transaction_id = i_txn_id
6617 	 AND   mmt.rcv_transaction_id = rt.transaction_id
6618 	 AND   rt.shipment_line_id = rsl.shipment_line_id;
6619 
6620        ELSIF (i_acct_line_type = 15) THEN
6621 
6622          l_stmt_num := 4;
6623          SELECT POD.ussgl_transaction_code
6624          INTO l_ussgl_tc
6625          FROM mtl_material_transactions mmt,
6626               rcv_transactions rt,
6627               po_distributions_all pod
6628          WHERE mmt.transaction_id = i_txn_id
6629          AND   mmt.rcv_transaction_id = rt.transaction_id
6630          AND   pod.po_distribution_id = rt.po_distribution_id;
6631        END IF;
6632      END IF;
6633   END IF; /* IF(l_enc_rev = 1) */
6634 
6635   FND_FILE.put_line(FND_FILE.log,'l_ussgl_tc : '||l_ussgl_tc);
6636 
6637 
6638 /* The following select statement will be made to refer to cst_organization_definitions as an impact of the HR-PROFILE option */
6639   l_stmt_num := 5;
6640   if (i_act_flag = 0) then
6641     SELECT NVL(operating_unit,-1)
6642     INTO l_operating_unit
6643     /*FROM org_organization_definitions */
6644     FROM  cst_organization_definitions
6645     WHERE organization_id = i_org_id;
6646 
6647     l_stmt_num := 6;
6648     SELECT REQ_ENCUMBRANCE_TYPE_ID,PURCH_ENCUMBRANCE_TYPE_ID
6649     INTO l_req_enc_id, l_po_enc_id
6650     FROM FINANCIALS_SYSTEM_PARAMS_ALL
6651     WHERE SET_OF_BOOKS_ID = i_sob_id AND
6652       NVL(org_id,-999) = DECODE(l_operating_unit,-1,-999,l_operating_unit);
6653 
6654   end if;
6655 
6656   l_stmt_num := 10;
6657 
6658   insert into mtl_transaction_accounts (
6659         inv_sub_ledger_id, /* R12 - SLA Distribution Link */
6660 	transaction_id,
6661 	reference_account,
6662 	last_update_date,
6663 	last_updated_by,
6664 	creation_date,
6665 	created_by,
6666 	last_update_login,
6667 	request_id,
6668 	program_application_id,
6669 	program_id,
6670 	program_update_date,
6671  	inventory_item_id,
6672 	organization_id,
6673 	transaction_date,
6674 	transaction_value,
6675 	accounting_line_type,
6676 	base_transaction_value,
6677 	basis_type,
6678 	contra_set_id,
6679 	resource_id,
6680 	cost_element_id,
6681 	currency_code,
6682 	currency_conversion_date,
6683 	currency_conversion_rate,
6684 	currency_conversion_type,
6685 	primary_quantity,
6686 	rate_or_amount,
6687 	transaction_source_type_id,
6688 	transaction_source_id,
6689 	encumbrance_type_id,
6690         ussgl_transaction_code)
6691   select
6692         CST_INV_SUB_LEDGER_ID_S.NEXTVAL,
6693 	i_txn_id,
6694 	i_acct,
6695 	sysdate,
6696 	i_user_id,
6697 	sysdate,
6698 	i_user_id, i_login_id,
6699 	i_req_id,
6700 	i_prg_appl_id,
6701 	i_prg_id,
6702 	sysdate,
6703 	i_item_id,
6704 	i_org_id,
6705 	to_date(i_txn_date,'DD-MM-RR'),
6706 	decode(i_alt_curr,NULL, NULL,
6707 	       i_pri_curr, NULL,
6708 	       decode(c2.minimum_accountable_unit,
6709 			NULL, round(i_value/i_conv_rate, c2.precision),
6710 			round(i_value/i_conv_rate
6711 			/c2.minimum_accountable_unit)
6712 			* c2.minimum_accountable_unit )),
6713 	i_acct_line_type,
6714 	decode(c1.minimum_accountable_unit,
6715 			NULL, round(i_value, c1.precision),
6716 			round(i_value/c1.minimum_accountable_unit)
6717 			* c1.minimum_accountable_unit ),
6718 	1,
6719 	1,
6720 	i_resource_id,
6721 	i_cost_element_id,
6722 	decode(i_alt_curr, i_pri_curr, NULL, i_alt_curr),
6723       	decode(i_alt_curr, i_pri_curr, NULL, i_conv_date),
6724       	decode(i_alt_curr, i_pri_curr, NULL, i_conv_rate),
6725 	decode(i_alt_curr, i_pri_curr, NULL, i_conv_type),
6726 	decode(i_acct_line_type,
6727 		1, i_qty, --inventory
6728 		14, i_qty, --intransit inventory
6729 		3, i_qty, -- overhead absorption
6730 		abs(i_qty) * decode(sign(i_value), 0, sign(i_qty), sign(i_value))
6731 	),	-- primary quantity
6732 	decode(i_qty, 0,0,
6733 		decode(i_acct_line_type,
6734 			1, i_value/i_qty,
6735 			14, i_value/i_qty,
6736 			3, i_value/i_qty,
6737 			abs(i_value/i_qty)
6738 			)
6739 	), -- rate_or_amount
6740 	i_src_type_id,
6741 	nvl(i_txn_src_id,-1),
6742 	decode(i_act_flag,1,NULL,0,decode(i_src_type_id,
6743                                         1,l_po_enc_id,
6744                                         7,l_req_enc_id,
6745                                         8,l_req_enc_id,
6746                                         NULL),NULL),
6747         decode(l_enc_rev,1,
6748 	   decode(l_txn_type,
6749                 18, decode(i_acct_line_type,
6750 				1, decode(i_cost_element_id,1, l_ussgl_tc,NULL),
6751 				15, l_ussgl_tc, NULL),
6752                 36, decode(i_acct_line_type,
6753                                 1, decode(i_cost_element_id,1, l_ussgl_tc,NULL),
6754                                 15, l_ussgl_tc, NULL),
6755                 71, decode(i_acct_line_type,
6756                                 1, decode(i_cost_element_id,1, l_ussgl_tc,NULL),
6757                                 15, l_ussgl_tc, NULL),
6758                 NULL),
6759 	   NULL)
6760   from
6761 	fnd_currencies c1,
6762 	fnd_currencies c2
6763   where
6764 	c1.currency_code = i_pri_curr
6765     and c2.currency_code = decode(i_alt_curr, NULL, i_pri_curr, i_alt_curr);
6766 
6767   EXCEPTION
6768 
6769  when invalid_acct_error then
6770  rollback;
6771  O_error_num := 9999;
6772  O_error_code := 'CST_NO_TXN_INVALID_ACCOUNT';
6773  FND_MESSAGE.set_name('BOM', 'CST_NO_TXN_INVALID_ACCOUNT');
6774  O_error_message := FND_MESSAGE.Get;
6775 
6776  when others then
6777  rollback;
6778  O_error_num := SQLCODE;
6779  O_error_message := 'CSTPACDP.insert_account (' || to_char(l_stmt_num)
6780                      || ') ' || substr(SQLERRM,1,180);
6781   FND_FILE.put_line(FND_FILE.log,'DS - others Exception, sqlcode = '||SQLCODE);
6782 
6783 end insert_account;
6784 
6785 procedure balance_account(
6786   I_ORG_ID		IN	NUMBER,
6787   I_TXN_ID		IN	NUMBER,
6788   O_Error_Num		OUT NOCOPY	NUMBER,
6789   O_Error_Code		OUT NOCOPY	VARCHAR2,
6790   O_Error_Message	OUT NOCOPY	VARCHAR2
6791 )IS
6792   l_stmt_num	NUMBER;
6793   l_rowid	ROWID;
6794   l_base_value	NUMBER;
6795   l_value	NUMBER;
6796   l_debug       VARCHAR2(1);
6797 
6798 BEGIN
6799 
6800   l_stmt_num := 10;
6801     fnd_file.put_line(fnd_file.log,'In balance account!!!!');
6802 
6803   select nvl(sum(base_transaction_value),0), nvl(sum(transaction_value),0)
6804   into l_base_value, l_value
6805   from mtl_transaction_accounts
6806   where transaction_id = i_txn_id
6807   and organization_id = i_org_id;
6808 
6809   if (l_base_value = 0 and l_value = 0) then
6810     return;
6811   end if;
6812 
6813   fnd_file.put_line(fnd_file.log, 'Adjustment Amount: '||l_base_value);
6814   l_stmt_num := 20;
6815 
6816 -- Bug# 4052277 (propagation of 4015478).
6817 -- Added condition in where clause to check that balance amount should not be
6818 -- adjusted against records with Accounting line type of 9 and 10 since these
6819 -- pertain to INTERORG Transfers where the Transaction value of Inter Org
6820 -- Receivables (Actg Line type 9) should always match the value of Inter Org
6821 -- Payables (Actg Line type 10).
6822 
6823    /* Exclude the encumbrance line as well */
6824 
6825 	SELECT MAX(rowid)
6826 	INTO   l_rowid
6827 	FROM   mtl_transaction_accounts
6828 	WHERE  transaction_id = i_txn_id
6829 	AND    organization_id = i_org_id
6830 	AND    accounting_line_type NOT IN (9,10,15); /* this condition added for 4052277 */
6831 
6832   l_stmt_num := 30;
6833   fnd_file.put_line(fnd_file.log,'Adjusting Balance value against record ' || l_rowid);
6834 
6835 
6836   update mtl_transaction_accounts
6837   set transaction_value = decode(transaction_value, NULL,
6838 				decode(l_value, 0, NULL,-1*l_value),
6839 				transaction_value - l_value),
6840       base_transaction_value = base_transaction_value - l_base_value
6841   where rowid = l_rowid;
6842 
6843   EXCEPTION
6844 
6845  when others then
6846 
6847  rollback;
6848  O_error_num := SQLCODE;
6849  O_error_message := 'CSTPACDP.balance_account (' || to_char(l_stmt_num) ||
6850                      ') ' || substr(SQLERRM,1,180);
6851 
6852 END balance_account;
6853 
6854 /*
6855 |-------------------------------------------------------------------------
6856 |    OPM INVCONV umoogala 15-Apr-2005  Process-Discrete Xfers Enh.
6857 |
6858 |    added new function cost_process_discrete_trx to cost transfer
6859 |    involving Discrete Standard Costing Orgs only. This new routine will
6860 |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6861 |    be called from cmlmcw.lpc.
6862 |
6863 |    Note: Process Mfg. Orgs are always marked as Standard Costing Orgs.
6864 |
6865 |    All the entries are for the transaction org only i.e., discrete
6866 |    costing orgs.
6867 |    All the intransit entries are booked using new logical transaction
6868 |    types:
6869 |           22: Logical Intransit Shipment
6870 |           15: Logical Intransit Receipt
6871 |-------------------------------------------------------------------------
6872 |    This code is almost same as inltcp.lpc code for interorg transfers.
6873 |    All the code which is not needed for Process-Discrete Transfers is
6874 |    removed. The main code section are like this:
6875 |
6876 |    case (txn_action_id)
6877 |      when 3 and 21
6878 |      then
6879 |       ... Direct xfers (incomming and outgoing) and intransit shipments
6880 |      when 12
6881 |      then
6882 |       ... Intransit receipts
6883 |      when 22
6884 |      then
6885 |       ... Logical Intransit Shipments
6886 |      when 15
6887 |      then
6888 |       ... Logical Intransit receipts
6889 |    End case (txn_action_id)
6890 |
6891 | History
6892 |  03/27/05      umoogala     Bug 4432078  OPM INVCONV  Process-Discrete Transfers Enh.
6893 |  12/20/05      umoogala     Bug 4885752/4658325
6894 |                               Quantities are not recorded with a proper sign in mta.
6895 |                               Fixed calls to book accounts to send correct sign.
6896 |  11-Aug-2006   ANTHIYAG     Bug#5459157
6897 |                               Modified Code to remove the quantity from being multipled
6898 |                               with the value fetched from MTA table, since the value already
6899 |                               has the quantity accounted for.
6900 |  08-Sep-2006   ANTHIYAG     Bug#5352186
6901 |                               Modifications to pass the Currency details in the receipt organization
6902 |                               distributin entries which refer to Freight Charges and Transfer price
6903 |                               details which are fetched from the sending organization's setup data.
6904 |-------------------------------------------------------------------------
6905 */
6906 
6907 PROCEDURE cost_process_discrete_trx(
6908   trans_id      IN BINARY_INTEGER
6909 , prg_appid     IN BINARY_INTEGER
6910 , prg_id        IN BINARY_INTEGER
6911 , req_id        IN BINARY_INTEGER
6912 , user_id       IN BINARY_INTEGER
6913 , login_id      IN BINARY_INTEGER
6914 
6915 , O_Error_Num     OUT NOCOPY VARCHAR2
6916 , O_Error_Code    OUT NOCOPY VARCHAR2
6917 , O_Error_Message OUT NOCOPY VARCHAR2
6918 )
6919 IS
6920   alt_curr           VARCHAR2(15);
6921   conv_date          DATE;
6922   conv_type          VARCHAR2(30);
6923   error_code         VARCHAR2(241);
6924   error_msg          VARCHAR2(241);
6925   flow_sch           VARCHAR2(2);
6926   pri_curr           VARCHAR2(15);
6927   rec_curr           VARCHAR2(15);
6928   rec_uom            VARCHAR2(3);
6929   send_curr          VARCHAR2(15);
6930   send_uom           VARCHAR2(15);
6931   subinv             VARCHAR2(11);
6932   txf_subinv         VARCHAR2(11);
6933   wms_return_status  VARCHAR2(11);
6934   wms_msg_data       VARCHAR2(2000);
6935   buffer             VARCHAR2(80);
6936   i_conv_type        VARCHAR2(241);
6937   interorg_conv_type VARCHAR2(241);
6938   mesg               VARCHAR2(241);
6939   price_err_code     VARCHAR2(241);
6940   price_err_expl     VARCHAR2(241);
6941 
6942   txn_date           DATE;
6943 
6944   conv_rate          NUMBER;
6945   enc_amt            NUMBER;
6946   interorg_xfr_price NUMBER;
6947   sending_org_cost   NUMBER;
6948   interorg_profit    NUMBER;
6949   ppv_amt            NUMBER;
6950   p_qty              NUMBER;
6951   prior_cost         NUMBER;
6952   rprior_cost        NUMBER;
6953   new_cost           NUMBER;
6954   qty_adj            NUMBER;
6955   txf_cost           NUMBER;
6956   trp_cost           NUMBER;
6957   recamt             NUMBER;
6958   payamt             NUMBER;
6959   rec_item_cost      NUMBER;
6960   send_item_cost     NUMBER;
6961   send_p_qty         NUMBER;
6962   rec_p_qty          NUMBER;
6963   send_txf_cost      NUMBER;
6964   send_trp_cost      NUMBER;
6965   tmp_act_cost       NUMBER;
6966   txf_price          NUMBER;
6967   txn_cost           NUMBER;
6968   act_cost           NUMBER;
6969   ract_cost          NUMBER;
6970   item_cost          NUMBER;
6971   uom_conv_rate      NUMBER;
6972   l_txn_cost         NUMBER;
6973   l_rec_txn_cost     NUMBER;
6974   var_amt            NUMBER;
6975 
6976   interorg_profit_acct     BINARY_INTEGER;
6977   org_opm_flag             BINARY_INTEGER;
6978   pd_txf_flag              BINARY_INTEGER;
6979   txf_org_opm_flag         BINARY_INTEGER;
6980   acc_line_type            BINARY_INTEGER;
6981   acct_period_id           BINARY_INTEGER;
6982   alias_acct               BINARY_INTEGER;
6983   cstud_id                 BINARY_INTEGER;
6984   txn_act_id               BINARY_INTEGER;
6985   txn_src_id               BINARY_INTEGER;
6986   enc_acct                 BINARY_INTEGER;
6987   sob_id                   BINARY_INTEGER;
6988   enc_rev                  BINARY_INTEGER;
6989   flow_sch_flg             BINARY_INTEGER;
6990   fob_pt                   BINARY_INTEGER;
6991   iexp_flg                 BINARY_INTEGER;
6992   exp_flg                  BINARY_INTEGER;
6993   rexp_flg                 BINARY_INTEGER;
6994   exp_acct                 BINARY_INTEGER;
6995   rexp_acct                BINARY_INTEGER;
6996   exp_sub                  BINARY_INTEGER;
6997   rexp_sub                 BINARY_INTEGER;
6998   io_inv_acct              BINARY_INTEGER;
6999   io_txfr_cr_acct          BINARY_INTEGER;
7000   io_rec_acct              BINARY_INTEGER;
7001   io_pay_acct              BINARY_INTEGER;
7002   io_ppv_acct              BINARY_INTEGER;
7003   item_id                  BINARY_INTEGER;
7004   org_id                   BINARY_INTEGER;
7005   l_cost_method            BINARY_INTEGER;
7006   l_trf_cost_method        BINARY_INTEGER;
7007   l_drop_ship_type_code    BINARY_INTEGER;
7008   l_stmt_num               BINARY_INTEGER;
7009   l_trf_organization_id    BINARY_INTEGER;
7010   op_seq_num               BINARY_INTEGER;
7011   mv_id                    BINARY_INTEGER;
7012   p_cst_type               BINARY_INTEGER;
7013   inv_acct                 BINARY_INTEGER;
7014   ap_acct                  BINARY_INTEGER;
7015   cog_acct                 BINARY_INTEGER;
7016   acv_acct                 BINARY_INTEGER;
7017   ppv_acct                 BINARY_INTEGER;
7018   pjm_flg                  BINARY_INTEGER;
7019   txf_pjm_flg              BINARY_INTEGER;
7020   return_code              BINARY_INTEGER;
7021   err_num                  BINARY_INTEGER;
7022   ccga_count               BINARY_INTEGER;
7023   send_inv_acct            BINARY_INTEGER;
7024   rec_inv_acct             BINARY_INTEGER;
7025   send_p_cst_type          BINARY_INTEGER;
7026   rec_p_cst_type           BINARY_INTEGER;
7027   send_sob_id              BINARY_INTEGER;
7028   rec_sob_id               BINARY_INTEGER;
7029   src_line_id              BINARY_INTEGER;
7030   src_type_id              BINARY_INTEGER;
7031   dist_acct                BINARY_INTEGER;
7032   trp_acct                 BINARY_INTEGER;
7033   tprice_option            BINARY_INTEGER;
7034   txf_org_id               BINARY_INTEGER;
7035   txn_type_id              BINARY_INTEGER;
7036   wms_flg                  BINARY_INTEGER;
7037   txf_wms_flg              BINARY_INTEGER;
7038   wms_msg_count            BINARY_INTEGER;
7039   abs_ovhd                 BINARY_INTEGER;
7040   mvi                      BINARY_INTEGER;
7041   ent_type                 BINARY_INTEGER;
7042   lot_based_job            BINARY_INTEGER;
7043   realoc_yld_cost          BINARY_INTEGER;
7044   tnx_type                 BINARY_INTEGER;
7045   zero_cost_flag           BINARY_INTEGER;
7046   comp_txn_id              BINARY_INTEGER;
7047   txf_txn_id               BINARY_INTEGER;
7048   CG_ID                    BINARY_INTEGER;
7049   TXF_CG_ID                BINARY_INTEGER;
7050   mv_idmvi                 BINARY_INTEGER;
7051 
7052   from_org                 BINARY_INTEGER;
7053   to_org                   BINARY_INTEGER;
7054   from_cost_grp            BINARY_INTEGER;
7055   to_cost_grp              BINARY_INTEGER;
7056 
7057   l_return_status	         VARCHAR2(80);
7058   l_return_code		         BINARY_INTEGER;
7059   l_msg_count		           BINARY_INTEGER;
7060   l_msg_data		           VARCHAR2(2000);
7061 
7062   l_debug                  VARCHAR2(1);
7063 
7064   snd_rcv                  BINARY_INTEGER;
7065   mat_acct                 BINARY_INTEGER;
7066   mat_ovhd_acct            BINARY_INTEGER;
7067   res_acct                 BINARY_INTEGER;
7068   osp_acct                 BINARY_INTEGER;
7069   ovhd_acct                BINARY_INTEGER;
7070   user_acct_id             BINARY_INTEGER;
7071   l_earn_moh               BINARY_INTEGER := 1;
7072 
7073   errexit                  exception;
7074 
7075   /* SLA Event Creation */
7076   l_trx_info              CST_XLA_PVT.t_xla_inv_trx_info;
7077 
7078   moh_rules_error         EXCEPTION;
7079 
7080   --
7081   -- Bug 5233635; Should not book InterOrg Profit when Intercompany Invoicing is enabled.
7082   --
7083   l_io_invoicing          NUMBER;
7084   l_org_ou                NUMBER;
7085   l_txf_org_ou            NUMBER;
7086 
7087   /* INVCONV ANTHIYAG Bug#5352186 09-Sep-2006 Start */
7088   l_snd_sob_id	          NUMBER;
7089   l_snd_curr	            VARCHAR2(10);
7090   l_rcv_sob_id	          NUMBER;
7091   l_rcv_curr	            VARCHAR2(10);
7092   l_curr_type	            VARCHAR2(30);
7093   l_conv_rate	            NUMBER;
7094   l_conv_date	            DATE;
7095   /* INVCONV ANTHIYAG Bug#5352186 09-Sep-2006 End */
7096 
7097 begin
7098   fnd_file.put_line(fnd_file.log, 'cost_process_discrete_trx <<<');
7099   l_debug := fnd_profile.value('MRP_DEBUG');
7100 
7101   /* Check if the organization_id and/or transfer_organization_id
7102      are average or FIFO/LIFO orgs. If so, error out this transaction, since it is just meant
7103      for Standard Costing
7104   */
7105   l_stmt_num := 10;
7106 
7107 
7108   select
7109         inventory_item_id, organization_id,
7110         transaction_date,
7111         primary_quantity, subinventory_code, nvl(cost_group_id,0),
7112         transfer_organization_id, nvl(prior_cost,0), nvl(new_cost,0),
7113         nvl(transaction_cost,0), nvl(actual_cost,0),
7114         quantity_adjusted, nvl(transfer_cost,0), nvl(transportation_cost,0),
7115         nvl(transportation_dist_account,-1),
7116         nvl(flow_schedule, 'N'),
7117         DECODE(completion_transaction_id,NULL,0,-1,0,completion_transaction_id),
7118         cost_update_id, transaction_action_id,
7119         decode( transaction_source_type_id, 16, -1, nvl(transaction_source_id,-1) ),
7120         transfer_subinventory, nvl(transfer_cost_group_id,0),
7121         nvl(transfer_transaction_id,0), trx_source_line_id,
7122         transaction_source_type_id, nvl(distribution_account_id,-1),
7123         acct_period_id, operation_seq_num,
7124         currency_code,
7125         nvl(currency_conversion_date,transaction_date),
7126         nvl(currency_conversion_rate,-1) , currency_conversion_type,
7127         nvl(encumbrance_account,-1), nvl(encumbrance_amount,0),
7128         nvl(variance_amount,0), movement_id,
7129         transaction_type_id, nvl(transfer_price, 0)
7130     INTO
7131         item_id, org_id, txn_date,
7132         p_qty, subinv, cg_id,
7133         txf_org_id, prior_cost, new_cost,
7134         txn_cost, act_cost,
7135         qty_adj, txf_cost, trp_cost,
7136         trp_acct,
7137         flow_sch,
7138         comp_txn_id,
7139         cstud_id, txn_act_id,
7140         txn_src_id,
7141         txf_subinv, txf_cg_id,
7142         txf_txn_id, src_line_id,
7143         src_type_id, dist_acct,
7144         acct_period_id, op_seq_num,
7145         alt_curr, conv_date,
7146         conv_rate, conv_type,
7147         enc_acct, enc_amt,
7148         var_amt, mv_idmvi,
7149         txn_type_id, interorg_xfr_price
7150      from mtl_material_transactions
7151      where transaction_id = trans_id;
7152 
7153 
7154   -- code to stop profile is at the end of this procedure.
7155   --
7156     fnd_file.put_line(fnd_file.log, 'src/act: ' || src_type_id ||'/'|| txn_act_id);
7157 
7158     fnd_file.put_line(fnd_file.log, 'txnId/Org: ' || trans_id || '/' || org_id ||
7159           ', xferOrg: ' || txf_org_id || ' XferPrice: ' || interorg_xfr_price ||
7160           ', p_qty: ' || p_qty);
7161     fnd_file.put_line(fnd_file.log, 'cg_id: ' || cg_id);
7162 
7163     --
7164     -- Bug 5233635; Should not book InterOrg Profit when Intercompany Invoicing is enabled.
7165     --
7166     l_stmt_num := 11;
7167 
7168     SELECT to_number(org_information3)
7169     INTO   l_org_ou
7170     FROM   hr_organization_information
7171     WHERE  org_information_context = 'Accounting Information'
7172     AND    organization_id = org_id;
7173 
7174     l_stmt_num := 12;
7175     SELECT to_number(org_information3)
7176     INTO   l_txf_org_ou
7177     FROM   hr_organization_information
7178     WHERE  org_information_context = 'Accounting Information'
7179     AND    organization_id = txf_org_id;
7180 
7181     l_io_invoicing := to_number(fnd_profile.value('INV_INTERCOMPANY_INVOICE_INTERNAL_ORDER'));
7182     fnd_file.put_line(fnd_file.LOG, 'OU/xferOU: ' || l_org_ou ||'/'||l_txf_org_ou||
7183       ' Internal Order invoicing: ' || l_io_invoicing);
7184 
7185     --
7186     -- End Bug 5233635
7187     --
7188 
7189     /*----------------------------------------------------------------------+
7190      -- set WMS flag
7191      +----------------------------------------------------------------------*/
7192     if(wms_install.check_install(l_return_status, l_msg_count, l_msg_data,org_id))
7193     then
7194       wms_flg := 1;
7195     else
7196       wms_flg := 0;
7197     end if;
7198 
7199 
7200     /*----------------------------------------------------------------------+
7201      -- set PJM flag
7202      +----------------------------------------------------------------------*/
7203     /* Changes for PJM Standard Costing. */
7204     /*Check if cost group accounting has been enabled. */
7205     l_stmt_num := 55;
7206 
7207     SELECT decode(nvl(cost_group_accounting,0),0,0,
7208              decode(nvl(project_reference_enabled,0),1,1,0))
7209       INTO pjm_flg
7210       FROM MTL_PARAMETERS
7211      WHERE organization_id = org_id;
7212 
7213     fnd_file.put_line(fnd_file.log, 'wms_flg: ' || wms_flg || ' pjm_flg: ' || pjm_flg);
7214 
7215     l_stmt_num := 60;
7216 
7217     /* Not sure what to do here!
7218      *
7219     switch(inltcl(trans_id, prg_appid, prg_id, req_id, user_id, login_id))
7220     {
7221     case 0:
7222         break;
7223 
7224     case 1:
7225         if (txn_act_id == 3)
7226         {
7227             l_stmt_num := 70;
7228 
7229             EXEC SQL select
7230                 primary_cost_method
7231             INTO
7232                 :rec_p_cst_type
7233             from mtl_parameters
7234             where organization_id = :txf_org_id;
7235 
7236             l_stmt_num := 80;
7237         }
7238 
7239         goto update_cost;
7240 
7241     case -1:
7242 
7243        if (afdname((text *)'BOM', (text *)'CST_ERR_LOCAL_COSTING'));
7244        else
7245           DISCARD afdstring((text *)'CST_ERR_LOCAL_COSTING');
7246        afdget(msgbuf1,241);
7247        vchfroms(err_expl,msgbuf1);
7248        vchfroms(err_code,msgbuf1);
7249        DISCARD NLSSCPY(errbuf, msgbuf1);
7250        afdclear();
7251 
7252 
7253        raise errexit;
7254 
7255     }
7256     */
7257 
7258    /*------------------------------------------------------------+
7259     |  Initialize flow schedule flag to 0.  Set it to 1 if the
7260     |  column flow_schedule is set to 'Y' in mmt.
7261     +------------------------------------------------------------*/
7262 
7263     if flow_sch = 'Y'
7264     then
7265       flow_sch_flg := 1;
7266     else
7267       flow_sch_flg := 0;
7268     end if;
7269 
7270 
7271     l_stmt_num := 125;
7272     CST_Utility_PUB.get_zeroCostIssue_flag (
7273       p_api_version	=> 1.0,
7274       x_return_status	=> l_return_status,
7275       x_msg_count	=> l_msg_count,
7276       x_msg_data	=> l_msg_data,
7277       p_txn_id		=> trans_id,
7278       x_zero_cost_flag	=> zero_cost_flag
7279       );
7280 
7281     l_stmt_num := 130;
7282 
7283     select
7284         primary_cost_method,
7285         material_account, ap_accrual_account, cost_of_sales_account,
7286         purchase_price_var_account, average_cost_var_account,
7287         decode(encumbrance_reversal_flag,1,1,2,0,0),
7288         material_account, material_overhead_account, resource_account,
7289         outside_processing_account, overhead_account,
7290         expense_account
7291     INTO
7292         p_cst_type,
7293         inv_acct, ap_acct, cog_acct,
7294         ppv_acct, acv_acct,
7295         enc_rev,
7296         mat_acct, mat_ovhd_acct, res_acct,
7297         osp_acct, ovhd_acct,
7298         exp_acct
7299      from mtl_parameters
7300      where organization_id = org_id;
7301 
7302 
7303     /*----------------------------------------------------------------------+
7304      | Get SOB and base currency
7305      +----------------------------------------------------------------------*/
7306     l_stmt_num := 140;
7307     select cod.set_of_books_id, sob.currency_code
7308       INTO sob_id, pri_curr
7309       FROM cst_organization_definitions cod, gl_sets_of_books sob
7310      WHERE cod.organization_id = org_id
7311        AND sob.set_of_books_id = cod.set_of_books_id;
7312 
7313 
7314     /*----------------------------------------------------------------------+
7315      | Set expense flags
7316      +----------------------------------------------------------------------*/
7317     l_stmt_num := 160;
7318     select decode(inventory_asset_flag,'Y',0,1), nvl(expense_account, -1)
7319       INTO iexp_flg, exp_acct
7320       FROM mtl_system_items_b
7321      WHERE inventory_item_id = item_id
7322        AND organization_id = org_id;
7323 
7324 
7325     --
7326     -- Bug 4748551: Added if block to set SubInv as Asset SubInv when
7327     -- SubInv column is NULL. For intransit transfers, receiving SubInv
7328     -- may be null.
7329     --
7330     l_stmt_num := 170;
7331     if subinv is null
7332     then
7333       exp_sub := 0;
7334       exp_flg := 0;
7335     else
7336       --
7337       -- Bug 5337293: retain exp flag setting of item for intransit transfers
7338       --
7339       SELECT DECODE(txn_act_id, 3, DECODE(iexp_flg,1,1,DECODE(asset_inventory,1,0,1)),
7340                                 iexp_flg),
7341              DECODE(asset_inventory,1,0,1)
7342         INTO exp_flg, exp_sub
7343         FROM mtl_secondary_inventories
7344        WHERE secondary_inventory_name = subinv
7345          AND organization_id = org_id;
7346     end if;
7347 
7348     fnd_file.put_line(fnd_file.log, 'iexp_flag/exp_flg/exp_sub: ' || iexp_flg ||'/'|| exp_flg || '/'|| exp_sub);
7349 
7350 
7351     /*----------------------------------------------------------------------+
7352      | Get elemental accounts
7353      +----------------------------------------------------------------------*/
7354     l_stmt_num := 180;
7355     if (wms_flg = 0)
7356     then
7357 
7358        --
7359        -- Bug 4748551: Added if block to set SubInv as Asset SubInv when
7360        -- SubInv column is NULL. For intransit transfers, receiving SubInv
7361        -- may be null.
7362        --
7363        l_stmt_num := 190;
7364        if subinv is null
7365        then
7366          -- l_stmt_num := 191;
7367          -- We've already populated accounts from mtl_parameters above (stmt 130)
7368          NULL;
7369        else
7370          l_stmt_num := 192;
7371          select material_account, material_overhead_account, resource_account,
7372                 outside_processing_account, overhead_account,
7373                 nvl(expense_account, exp_acct)
7374            into mat_acct, mat_ovhd_acct, res_acct,
7375                 osp_acct, ovhd_acct,
7376                 exp_acct
7377            from mtl_secondary_inventories msi
7378           where msi.organization_id = org_id
7379             and secondary_inventory_name = subinv;
7380        end if;
7381     else
7382      if (cg_id = 1)
7383      then
7384         l_stmt_num := 210;
7385 
7386         select material_account, material_overhead_account, resource_account,
7387                outside_processing_account, overhead_account,
7388                nvl(expense_account, exp_acct)
7389           into mat_acct, mat_ovhd_acct, res_acct,
7390                osp_acct, ovhd_acct,
7391                exp_acct
7392           from mtl_parameters
7393          where organization_id = org_id;
7394 
7395      else
7396          l_stmt_num := 230;
7397 
7398          select count(*)
7399            INTO ccga_count
7400            FROM cst_cost_group_accounts
7401 	        WHERE cost_group_id = cg_id
7402             AND organization_id = org_id;
7403 
7404          l_stmt_num := 240;
7405          if (ccga_count = 0)
7406          then
7407             FND_MESSAGE.set_name('BOM', 'CST_NO_CG_ACCTS');
7408             O_Error_Num     := NULL;
7409             O_Error_Code    := 'CST_NO_CG_ACCTS';
7410             O_Error_Message := FND_MESSAGE.get;
7411             raise errexit;
7412          else
7413             l_stmt_num := 250;
7414 
7415             select material_account, material_overhead_account, resource_account,
7416                    outside_processing_account, overhead_account,
7417                    nvl(expense_account, exp_acct)
7418               into mat_acct, mat_ovhd_acct, res_acct,
7419                    osp_acct, ovhd_acct,
7420                    exp_acct
7421               from cst_cost_group_accounts
7422              where organization_id = org_id
7423                and cost_group_id = cg_id;
7424 
7425          end if; /* if (ccga_count = 0) */
7426 
7427       end if; /* if (cg_id = 1) */
7428     end if; /* if (!wms_flg ) */
7429 
7430 
7431     /*----------------------------------------------------------------------+
7432      |Insert standard cost into mtl_cst_actual_cost_details
7433      +----------------------------------------------------------------------*/
7434     fnd_file.put_line(fnd_file.log, 'inserting into mcacd');
7435 
7436     l_stmt_num := 280;
7437     CSTPSISC.ins_std_cost(org_id, item_id, trans_id,
7438                           txn_act_id, src_type_id,
7439                           iexp_flg, exp_sub, txn_cost,
7440                           act_cost, prior_cost,
7441                           user_id, login_id, req_id,
7442                           prg_appid, prg_id, O_Error_Num,
7443                           O_Error_Code, O_Error_Message);
7444 
7445     if  O_Error_Code is not null or O_Error_Message is not null
7446     then
7447       raise errexit;
7448     end if;
7449 
7450 
7451     /*----------------------------------------------------------------------+
7452     |Call standard cost distribution hook. Return code of 1 means that the
7453     |hook has been used.
7454     +----------------------------------------------------------------------*/
7455 
7456     l_stmt_num := 290;
7457     l_return_code := CSTPSCHK.std_cost_dist_hook( org_id, trans_id,
7458                     user_id, login_id, req_id, prg_appid,
7459                     prg_id, O_Error_Num, O_Error_Code, O_Error_Message);
7460 
7461     if (O_Error_Num <> 0)
7462     then
7463         raise errexit;
7464     end if;
7465 
7466     if (l_return_code = 1)        /* Hook has been used */
7467     then
7468       goto update_cost;
7469     end if;
7470 
7471 
7472     /*----------------------------------------------------------------------+
7473      --
7474      -- Figure out the send/receiving flag. 1 is send and 2 is recv.
7475      -- Also, set from/to orgs and cost groups.
7476      -- OPM INVCONV umoogala: Added 15 (Logical Itr Receipt) and 22 (Logical Itr Shipment) actions.
7477      --
7478      -- Bug 5349860: Now processing Internal Order Issue to Expense Detination txn
7479      --              and its logical receiving txn.
7480      +----------------------------------------------------------------------*/
7481     l_stmt_num := 300;
7482     if (txn_act_id in (3, 21) and p_qty <0) or (txn_act_id = 22) or
7483        (txn_act_id = 1 and src_type_id = 8)
7484     then
7485       snd_rcv := 1;
7486       from_org      := org_id;
7487       to_org        := txf_org_id;
7488       from_cost_grp := cg_id;
7489       to_cost_grp   := txf_cg_id;
7490     elsif (txn_act_id in (3, 12, 15) and p_qty > 0) or
7491           (txn_act_id = 17 and src_type_id = 7)
7492     then
7493       snd_rcv := 2;
7494       from_org      := txf_org_id;
7495       to_org        := org_id;
7496       from_cost_grp := txf_cg_id;
7497       to_cost_grp   := cg_id;
7498     end if;
7499 
7500     fnd_file.put_line(fnd_file.log,'snd_rcv: ' || snd_rcv);
7501     fnd_file.put_line(fnd_file.log,'from_org: ' || from_org);
7502     fnd_file.put_line(fnd_file.log,'to_org: ' || to_org);
7503 
7504     /* INVCONV ANTHIYAG Bug#5352186 09-Sep-2006 Start */
7505     CSTPAVCP.get_snd_rcv_rate(trans_id, from_org, to_org,
7506     l_snd_sob_id, l_snd_curr, l_rcv_sob_id, l_rcv_curr,
7507     l_curr_type, l_conv_rate, l_conv_date,
7508     O_Error_Num, O_Error_Code, O_Error_Message);
7509 
7510     if (O_Error_Num <> 0) then
7511         raise errexit;
7512     end if;
7513     fnd_file.put(fnd_file.log, 'fromOrg/SOB/Curr: ' || from_org ||'/'|| l_snd_sob_id ||'/'|| l_snd_curr);
7514     fnd_file.put(fnd_file.log, 'toOrg/SOB/Curr: '  || to_org ||'/'|| l_rcv_sob_id ||'/'|| l_rcv_curr);
7515     fnd_file.put(fnd_file.log, 'currType/Rate/Date: ' || l_curr_type ||'/'|| l_conv_rate ||'/'|| l_conv_date);
7516 
7517     /* umoogala Bug#5708387 13-dec-2006
7518      * l_conv_date is not returned from above call. Using txn date
7519      */
7520     l_conv_date := nvl(l_conv_date, txn_date);
7521 
7522     fnd_file.put(fnd_file.log, 'currDate: ' || l_conv_date );
7523     /* INVCONV ANTHIYAG Bug#5352186 09-Sep-2006 End */
7524 
7525     /*----------------------------------------------------------------------+
7526      | Get sending and reciving uom
7527      +----------------------------------------------------------------------*/
7528     l_stmt_num := 460;
7529     select send.primary_uom_code, rec.primary_uom_code
7530       INTO send_uom, rec_uom
7531       FROM mtl_system_items_b send, mtl_system_items_b rec
7532      WHERE send.inventory_item_id = item_id
7533        AND send.organization_id = from_org
7534        AND rec.inventory_item_id = item_id
7535        AND rec.organization_id = to_org;
7536 
7537     /*----------------------------------------------------------------------+
7538      * Transfer Pricing
7539      * For process-discrete transfers, transfer price is always incomming cost
7540      * which is stamped on MMT row. We've already queried this above.
7541      *
7542      * Transfer cost is n/a for these type of transfers.
7543      * Freight Charges are nothing but transportation charges.
7544      +----------------------------------------------------------------------*/
7545 
7546     tprice_option := 2;
7547     txf_price     := interorg_xfr_price; /* from MMT */
7548     txf_cost      := 0;  /* is N/A */
7549 
7550 
7551     /*----------------------------------------------------------------------+
7552      | Get inter-org accounts from Shipping Networks
7553      | Do not set FOB for Direct Org xfers
7554      +----------------------------------------------------------------------*/
7555     l_stmt_num := 470;
7556     SELECT
7557         case when txn_act_id <> 3 then nvl(mmt.fob_point, mip.fob_point) end case,
7558         decode(txn_act_id,3,mp.material_account,
7559                nvl(mmt.intransit_account, mip.intransit_inv_account)),  /* used only for interorg shipment */
7560         mip.interorg_transfer_cr_account,
7561         mip.interorg_receivables_account,
7562         mip.interorg_payables_account,
7563         mip.interorg_price_var_account,
7564         mp.average_cost_var_account,
7565         mip.interorg_profit_account  /* interorg profit account for process-discrete xfers only */
7566     INTO
7567         fob_pt,
7568         io_inv_acct,
7569         io_txfr_cr_acct,
7570         io_rec_acct,
7571         io_pay_acct,
7572         io_ppv_acct,
7573         acv_acct,                  /* overwrite sending acv_acct */
7574         interorg_profit_acct
7575     FROM mtl_interorg_parameters mip, mtl_parameters mp, mtl_material_transactions mmt
7576     WHERE mip.from_organization_id = from_org
7577     AND mip.to_organization_id = to_org
7578     AND mp.organization_id = org_id
7579     AND mmt.transaction_id = trans_id;
7580 
7581     fnd_file.put_line(fnd_file.log, 'FOB = ' || fob_pt);
7582 
7583 
7584     /*----------------------------------------------------------------------+
7585      | Get act_cost and item_cost. It is used only in AX code
7586      +----------------------------------------------------------------------*/
7587     SELECT decode(act_cost, 0, nvl(item_cost,0), act_cost), nvl(item_cost, 0)
7588       INTO act_cost, item_cost
7589       FROM cst_item_costs_for_gl_view
7590      WHERE inventory_item_id = item_id
7591        AND organization_id = org_id;
7592 
7593     fnd_file.put_line(fnd_file.log, 'act_cost: ' || act_cost || ' item_cost: ' || item_cost);
7594 
7595 
7596     /*----------------------------------------------------------------------+
7597      * Get PPV Account only for receiving transactions
7598      +----------------------------------------------------------------------*/
7599 
7600     if (txn_act_id = 12) OR (txn_act_id = 3 and p_qty > 0)
7601     then
7602 
7603       if (NOT wms_flg = 1)
7604       then
7605         if(subinv is not null)
7606         then
7607           l_stmt_num := 610;
7608 
7609           if (pjm_flg = 1)
7610           then
7611              if (cg_id = 1)
7612              then
7613                 SELECT nvl(purchase_price_var_account, io_ppv_acct)
7614                   INTO io_ppv_acct
7615                   FROM mtl_parameters
7616                  WHERE organization_id = org_id;
7617              else
7618                 l_stmt_num := 612;
7619 
7620                 SELECT count(*)
7621                   INTO ccga_count
7622                   FROM cst_cost_group_accounts
7623                  WHERE cost_group_id   = cg_id
7624                    AND organization_id = org_id;
7625 
7626                 l_stmt_num := 614;
7627                 if (ccga_count = 0)
7628                 then
7629                   FND_MESSAGE.set_name('BOM', 'CST_NO_CG_ACCTS');
7630                   O_Error_Num     := NULL;
7631                   O_Error_Code    := 'CST_NO_CG_ACCTS';
7632                   O_Error_Message := FND_MESSAGE.get;
7633                   raise errexit;
7634                 else
7635                    l_stmt_num := 616;
7636                    SELECT nvl(purchase_price_var_account, io_ppv_acct)
7637                      INTO io_ppv_acct
7638                      FROM cst_cost_group_accounts
7639                     WHERE cost_group_id = cg_id
7640                       AND organization_id = org_id ;
7641                 end if;
7642              end if; /* if (cg_id <> 1) */
7643           end if; /* if (pjm_flg) */
7644         end if; /* if(subinv.len > 0) */
7645       /* end of if (NOT wms_flg) */
7646       else
7647          if (cg_id <> 1)
7648          then
7649             l_stmt_num := 640;
7650             SELECT count(*)
7651               INTO ccga_count
7652               FROM cst_cost_group_accounts
7653              WHERE cost_group_id = cg_id
7654                AND organization_id = org_id;
7655 
7656             l_stmt_num := 650;
7657             if (ccga_count = 0)
7658             then
7659               FND_MESSAGE.set_name('BOM', 'CST_NO_CG_ACCTS');
7660               O_Error_Num     := NULL;
7661               O_Error_Code    := 'CST_NO_CG_ACCTS';
7662               O_Error_Message := FND_MESSAGE.get;
7663               raise errexit;
7664            else
7665              if (subinv is  not null)
7666              then
7667                 l_stmt_num := 655;
7668                 SELECT nvl(purchase_price_var_account, io_ppv_acct)
7669                   INTO io_ppv_acct
7670                   FROM cst_cost_group_accounts
7671                  WHERE cost_group_id = cg_id
7672                    AND organization_id = org_id ;
7673              end if; /* if(subinv.len > 0) */
7674            end if; /* if (ccga_count = 0) */
7675         end if; /* if (cg_id <> 1) */
7676       end if; /* if (wms_flg) */
7677     end if; /* if (txn_act_id = 12) OR (txn_act_id = 3 and p_qty > 0) */
7678 
7679 
7680 
7681     --
7682     -- Bug 5021305: Added following to see overhead rules are setup or not.
7683     -- Bug 5349860: umoogala - exclude issue/receipt (to exp destination) txn.
7684     --
7685 
7686     fnd_file.put_line(fnd_file.log, 'Verifying whether to earn MOH or not...');
7687 
7688     IF (txn_act_id <> 1) AND (txn_act_id <> 17) AND
7689        (NOT exp_flg = 1) AND (NOT exp_sub = 1)
7690     THEN
7691 
7692       /*----------------------------------------------------------------------+
7693        | Verify whether to earn MOH or not
7694        +----------------------------------------------------------------------*/
7695       cst_mohRules_pub.apply_moh(
7696                            1.0,
7697                            p_organization_id => org_id,
7698                            p_earn_moh        => l_earn_moh,
7699                            p_txn_id          => trans_id,
7700                            p_item_id         => item_id,
7701                            x_return_status   => l_return_status,
7702                            x_msg_count       => l_msg_count,
7703                            x_msg_data        => l_msg_data);
7704 
7705       IF l_return_status <> FND_API.g_ret_sts_success
7706       THEN
7707 
7708         CST_UTILITY_PUB.writelogmessages
7709                          ( p_api_version   => 1.0,
7710                            p_msg_count     => l_msg_count,
7711                            p_msg_data      => l_msg_data,
7712                            x_return_status => l_return_status);
7713         RAISE moh_rules_error;
7714       END IF;
7715 
7716       fnd_file.put_line(fnd_file.log, 'l_earn_moh: ' || l_earn_moh);
7717 
7718     END IF;
7719 
7720     <<MAIN_CASE_FOR_ACTION_ID>>
7721     case
7722       --
7723       -- Bug 5349860: Added Internal Order issue to Expense destination
7724       --
7725       when (txn_act_id = 3 or txn_act_id = 21 or txn_act_id = 1)
7726       then
7727       -- {
7728         /*----------------------------------------------------------------------+
7729          | Direct Interorg Shipment and Intransit shipment
7730          +----------------------------------------------------------------------*/
7731 
7732 	  --
7733 	  -- Bug 5349860: umoogala 12/Jun/06
7734 	  -- Added Internal order issue to expense destination txn to the following
7735 	  -- if condition
7736 	  --
7737           l_stmt_num := 480;
7738           if ((txn_act_id = 3 AND p_qty < 0) OR (fob_pt = 1) OR
7739 	      (txn_act_id = 1))
7740           then
7741           --{
7742 
7743             /* Direct interorg between discrete orgs or fob ship */
7744             fnd_file.put_line(fnd_file.log, 'Processing shipment txn. FOB = ' || fob_pt);
7745 
7746             /*----------------------------------------------------------------------+
7747              |Sending Organization : Do accounting for asset subinventory only
7748              +----------------------------------------------------------------------*/
7749             if (NOT exp_sub = 1)
7750             then
7751                if (NOT exp_flg = 1)
7752                then
7753 
7754                 /* Cr Sending Org @sending org cost */
7755 
7756 
7757                     /*----------------------------------------------------------------------+
7758                      | Cr Inv. Val @ sending org cost
7759                      +----------------------------------------------------------------------*/
7760                     fnd_file.put_line(fnd_file.log, 'Cr Inv Val @ sending org cost');
7761 
7762                     CSTPACDP.distribute_accounts(
7763                         org_id, trans_id,
7764                         0, cg_id , txn_act_id,
7765                         item_id, p_qty, 1,
7766                         1, 0, NULL, mat_acct, mat_ovhd_acct,
7767                         res_acct, osp_acct, ovhd_acct,
7768                         sob_id, txn_date, txn_src_id, src_type_id,
7769                         subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
7770                         user_id, login_id, req_id, prg_appid, prg_id,
7771                         O_Error_Num, O_Error_Code, O_Error_Message);
7772 
7773                     if (O_Error_Num <> 0) then
7774                       raise errexit;
7775                     end if;
7776 
7777                end if;
7778 
7779             /*----------------------------------------------------------------------+
7780              |Sending organization: Do the accounting for transfer from expense sub
7781              |if profile is set to allow this transfer.  No accounting is done for
7782              |expense items.
7783              +----------------------------------------------------------------------*/
7784             else
7785               if (NOT iexp_flg = 1)
7786               then
7787 
7788 
7789                 /*----------------------------------------------------------------------+
7790                  | Dr Exp Acct
7791                  +----------------------------------------------------------------------*/
7792                 fnd_file.put_line(fnd_file.log, 'Dr Exp Acct');
7793                 l_stmt_num := 750;
7794 
7795 		--
7796 		-- Bug 5331207:
7797 		-- Shipping from expense destination fails with ORA-1400
7798 		-- We were calling distribute_accounts with elemental flag 1.
7799 		-- To book to exp account only, now sending elemental flag 0.
7800 		--
7801                 CSTPACDP.distribute_accounts(
7802                     org_id, trans_id, 0, cg_id , txn_act_id,
7803                     item_id, p_qty, 2, 0, 0,
7804                     exp_acct, NULL, NULL, NULL, NULL, NULL,
7805                     sob_id, txn_date, txn_src_id, src_type_id,
7806                     subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
7807                     user_id, login_id, req_id, prg_appid, prg_id,
7808                     O_Error_Num, O_Error_Code, O_Error_Message);
7809 
7810                 if (O_Error_Num <> 0) then
7811                   raise errexit;
7812                 end if;
7813               end if;
7814 
7815             end if;
7816 
7817             /* Cr Sending Org Freight Exp account */
7818             if (txn_act_id = 3) and (trp_cost <> 0)
7819             then
7820 
7821                     user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
7822                                        -1, 12, 1, 0,
7823                                        subinv, cg_id, 0, snd_rcv,
7824                                        O_Error_Num, O_Error_Code, O_Error_Message);
7825 
7826                     if (O_Error_Num <> 0) then
7827                       raise errexit;
7828                     end if;
7829 
7830                     if user_acct_id > 0
7831                     then
7832                       trp_acct := user_acct_id;
7833                     end if;
7834 
7835 
7836                     /*----------------------------------------------------------------------+
7837                      | Cr Freight Acct
7838                      +----------------------------------------------------------------------*/
7839                     fnd_file.put_line(fnd_file.log, 'Cr Freight Acct with ' || trp_cost || ' ' || pri_curr);
7840                     CSTPACDP.insert_account
7841                        (org_id, trans_id, item_id, -1 * trp_cost,
7842                         p_qty, trp_acct, sob_id, 12, NULL, NULL,
7843                         txn_date, txn_src_id, src_type_id,
7844                         pri_curr, NULL, NULL, NULL, NULL,1,
7845                         user_id, login_id, req_id, prg_appid, prg_id,
7846                         O_Error_Num, O_Error_Code, O_Error_Message);
7847 
7848                     if (O_Error_Num <> 0) then
7849                       raise errexit;
7850                     end if;
7851 
7852             end if;
7853 
7854 
7855             /*----------------------------------------------------------------------+
7856              | Dr Receivalbes with transfer price
7857              +----------------------------------------------------------------------*/
7858             user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
7859                                1, 10, 1, 0,
7860                                subinv, cg_id, 0, snd_rcv,
7861                                O_Error_Num, O_Error_Code, O_Error_Message);
7862 
7863             if (O_Error_Num <> 0) then
7864               raise errexit;
7865             end if;
7866 
7867             if user_acct_id > 0
7868             then
7869               io_rec_acct := user_acct_id;
7870             end if;
7871 
7872 
7873             fnd_file.put_line(fnd_file.log, 'Dr Receivables with ' || (txf_price * p_qty) || ' ' || pri_curr);
7874             CSTPACDP.insert_account
7875                (org_id, trans_id, item_id, -1 * txf_price * p_qty,
7876                 -1 * p_qty, io_rec_acct, sob_id, 10, NULL, NULL,
7877                 txn_date, txn_src_id, src_type_id,
7878                 pri_curr, NULL, NULL, NULL, NULL,1,
7879                 user_id, login_id, req_id, prg_appid, prg_id,
7880                 O_Error_Num, O_Error_Code, O_Error_Message);
7881 
7882             if (O_Error_Num <> 0) then
7883               raise errexit;
7884             end if;
7885 
7886             /*----------------------------------------------------------------------+
7887              | Dr InterOrg Profit Account with (transfer_price - sending org cost) * qty.
7888              +----------------------------------------------------------------------*/
7889 
7890            l_stmt_num := 761;
7891 
7892            --
7893            -- Bug 5233635: Should not book InterOrg Profit If Intercompany Invoicing is enabled.
7894 	   -- Following flag is set at the beginning of the procedure.
7895            --
7896            if  (src_type_id = 8)
7897 	   and (l_org_ou <> l_txf_org_ou)
7898            and (l_io_invoicing = 1)
7899            and (txn_act_id = 21) -- Bug 5351724
7900            and (iexp_flg   = 0)    -- Bug 5348953: No IC Invoicing for expense items
7901            then
7902              NULL;
7903              fnd_file.put_line(fnd_file.log, 'No need to book InterOrg Profit since InterCompany Invoicing ' ||
7904                'is enabled.');
7905            else
7906 	     /*
7907 	      * Replaced with following SQL
7908              if (txn_act_id = 3)
7909              then
7910                interorg_profit := txf_price - (item_cost + (trp_cost/abs(p_qty)));
7911              else
7912                interorg_profit := txf_price - item_cost;
7913              end if;
7914 	      */
7915 
7916              SELECT SUM(NVL(base_transaction_value,0))
7917               INTO interorg_profit
7918               FROM mtl_transaction_accounts
7919              WHERE transaction_id  = trans_id
7920                AND organization_id = org_id
7921              ;
7922 
7923              fnd_file.put_line(fnd_file.log, 'InterOrg Profit = ' || nvl(interorg_profit, 0) || ' ' || pri_curr);
7924 
7925              if (interorg_profit <> 0)
7926              then
7927 
7928                user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
7929                                   -1, G_INTERORG_PROFIT_ACCT, 1, 0,
7930                                   subinv, cg_id, 0, snd_rcv,
7931                                   O_Error_Num, O_Error_Code, O_Error_Message);
7932 
7933                if (O_Error_Num <> 0) then
7934                  raise errexit;
7935                end if;
7936 
7937                if user_acct_id > 0
7938                then
7939                  interorg_profit_acct := user_acct_id;
7940                end if;
7941 
7942                fnd_file.put_line(fnd_file.log, 'Cr InterOrg Profit with ' || nvl(interorg_profit,0) || ' ' || pri_curr);
7943                CSTPACDP.insert_account
7944                   (org_id, trans_id, item_id, -1 * interorg_profit,
7945                    p_qty, interorg_profit_acct, sob_id,
7946                    G_INTERORG_PROFIT_ACCT, NULL, NULL,
7947                    txn_date, txn_src_id, src_type_id,
7948                    pri_curr, NULL, NULL, NULL, NULL,1,
7949                    user_id, login_id, req_id, prg_appid, prg_id,
7950                    O_Error_Num, O_Error_Code, O_Error_Message);
7951 
7952                if (O_Error_Num <> 0) then
7953                  raise errexit;
7954                end if;
7955 
7956              end if;
7957 	   end if; -- Bug 5233635
7958 
7959           /* end of: if ((txn_act_id = 3 AND p_qty < 0) OR fob_pt = 1) */
7960 
7961 
7962           --}
7963 
7964           /*----------------------------------------------------------------------+
7965            |FOB point Delivery
7966            +----------------------------------------------------------------------*/
7967 
7968           elsif (txn_act_id = 21 AND fob_pt = 2)
7969           then
7970           --{
7971 
7972             fnd_file.put_line(fnd_file.log, 'Processing intransit shipment. FOB = ' || fob_pt);
7973 
7974             --
7975 	    -- Bug 5332792: No accounting for shipment txn with FOB Receipt for expense item
7976 	    -- Added 2nd condition (NOT iexp_flg = 1) to the IF stmt below.
7977 	    -- So, for expense item transfered using FOB Receipt/Shipment following is the
7978 	    -- acctg:
7979 	    -- Dr Receivalbes
7980 	    --   Cr InterOrg Profit.
7981 	    -- For FOB Shipment: above is done using shipment txn.
7982 	    -- For FOB Receipt:  above is done using logical intransit shipment.
7983 	    --
7984             if (NOT exp_sub = 1) and (NOT iexp_flg = 1)
7985             then
7986                 /*----------------------------------------------------------------------+
7987                  |FOB point Delivery: Do the accounting for asset subinventory
7988                  +----------------------------------------------------------------------*/
7989 
7990                   /* Cr Sending Orgs Inv. Valuation account */
7991 
7992                   if item_cost > 0
7993                   then
7994 
7995                     /*----------------------------------------------------------------------+
7996                      | Cr Inv. Val @ sending org cost
7997                      +----------------------------------------------------------------------*/
7998                     fnd_file.put_line(fnd_file.log, 'Cr Inv Val @ sending org cost');
7999                     CSTPACDP.distribute_accounts(
8000                         org_id, trans_id,
8001                         0, cg_id , txn_act_id,
8002                         item_id, p_qty, 1,
8003                         1, 0, NULL, mat_acct, mat_ovhd_acct,
8004                         res_acct, osp_acct, ovhd_acct,
8005                         sob_id, txn_date, txn_src_id, src_type_id,
8006                         subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
8007                         user_id, login_id, req_id, prg_appid, prg_id,
8008                         O_Error_Num, O_Error_Code, O_Error_Message);
8009 
8010                     if (O_Error_Num <> 0) then
8011                       raise errexit;
8012                     end if;
8013 
8014                     /* Dr Sending Orgs intransit */
8015 
8016 
8017                     /*----------------------------------------------------------------------+
8018                      | Dr Intransit Acct @ org cost
8019                      +----------------------------------------------------------------------*/
8020                     fnd_file.put_line(fnd_file.log, 'Dr Intransit Acct @ org cost');
8021                     CSTPACDP.distribute_accounts(
8022                         org_id, trans_id, 0, cg_id , txn_act_id,
8023                         item_id, -1 * p_qty, 14, 0, 0,
8024                         io_inv_acct, NULL, NULL, NULL, NULL,NULL,
8025                         sob_id, txn_date, txn_src_id, src_type_id,
8026                         subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
8027                         user_id, login_id, req_id, prg_appid, prg_id,
8028                         O_Error_Num, O_Error_Code, O_Error_Message);
8029 
8030                     if (O_Error_Num <> 0) then
8031                       raise errexit;
8032                     end if;
8033 
8034                   else
8035                      -- Item Cost is zero, so directly call insert_acct with 0 cost.
8036 
8037                     /*----------------------------------------------------------------------+
8038                      | Cr Inv. Val @ with zero cost
8039                      +----------------------------------------------------------------------*/
8040                     user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8041                                        -1, 1, 1, 0,
8042                                        subinv, cg_id, 0, snd_rcv,
8043                                        O_Error_Num, O_Error_Code, O_Error_Message);
8044 
8045                     if (O_Error_Num <> 0) then
8046                       raise errexit;
8047                     end if;
8048 
8049                     if user_acct_id > 0
8050                     then
8051                       mat_acct := user_acct_id;
8052                     end if;
8053 
8054                     fnd_file.put_line(fnd_file.log, 'Cr Inv Val');
8055                     CSTPACDP.insert_account
8056                        (org_id, trans_id, item_id, 0,
8057                         p_qty, mat_acct, sob_id, 1, NULL, NULL,
8058                         txn_date, txn_src_id, src_type_id,
8059                         pri_curr, NULL, NULL, NULL, NULL,1,
8060                         user_id, login_id, req_id, prg_appid, prg_id,
8061                         O_Error_Num, O_Error_Code, O_Error_Message);
8062 
8063                     if (O_Error_Num <> 0) then
8064                       raise errexit;
8065                     end if;
8066 
8067                     /*----------------------------------------------------------------------+
8068                      | Dr Intransit Acct with zero cost
8069                      +----------------------------------------------------------------------*/
8070                     user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8071                                        -1, 14, 1, 0,
8072                                        subinv, cg_id, 0, snd_rcv,
8073                                        O_Error_Num, O_Error_Code, O_Error_Message);
8074 
8075                     if (O_Error_Num <> 0) then
8076                       raise errexit;
8077                     end if;
8078 
8079                     if user_acct_id > 0
8080                     then
8081                       io_inv_acct := user_acct_id;
8082                     end if;
8083 
8084                     fnd_file.put_line(fnd_file.log, 'Dr Intransit Acct');
8085                     CSTPACDP.insert_account
8086                        (org_id, trans_id, item_id, 0,
8087                         -1 * p_qty, io_inv_acct, sob_id, 14, NULL, NULL,
8088                         txn_date, txn_src_id, src_type_id,
8089                         pri_curr, NULL, NULL, NULL, NULL,1,
8090                         user_id, login_id, req_id, prg_appid, prg_id,
8091                         O_Error_Num, O_Error_Code, O_Error_Message);
8092 
8093                     if (O_Error_Num <> 0) then
8094                       raise errexit;
8095                     end if;
8096 
8097                   end if; /* if item_cost > 0 */
8098 
8099             else
8100 
8101              /*----------------------------------------------------------------------+
8102               |FOB point Delivery:  Do the accounting for transfer from expense sub
8103               |if the profile is set to allow this transfer
8104               +----------------------------------------------------------------------*/
8105               if (NOT iexp_flg = 1)
8106               then
8107 
8108                 l_stmt_num := 790;
8109 
8110 
8111                 /*----------------------------------------------------------------------+
8112                  | Cr Exp Acct
8113                  +----------------------------------------------------------------------*/
8114                 fnd_file.put_line(fnd_file.log, 'Cr Exp Acct');
8115                 CSTPACDP.distribute_accounts(
8116                     org_id, trans_id, 0, cg_id , txn_act_id,
8117                     item_id, p_qty, 2, 0, 0,
8118                     exp_acct, NULL, NULL, NULL, NULL, NULL,
8119                     sob_id, txn_date, txn_src_id, src_type_id,
8120                     subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
8121                     user_id, login_id, req_id, prg_appid, prg_id,
8122                     O_Error_Num, O_Error_Code, O_Error_Message);
8123 
8124                 if (O_Error_Num <> 0) then
8125                   raise errexit;
8126                 end if;
8127 
8128 
8129                 /*----------------------------------------------------------------------+
8130                  | Dr Intransit Acct @ org cost
8131                  +----------------------------------------------------------------------*/
8132                 fnd_file.put_line(fnd_file.log, 'Dr Intransit Acct @ org cost');
8133                 CSTPACDP.distribute_accounts(
8134                     org_id, trans_id,
8135                     0, cg_id , txn_act_id,
8136                     item_id, -1 * p_qty, 14,
8137                     0, 0, io_inv_acct, NULL, NULL, NULL, NULL,NULL,
8138                     sob_id, txn_date, txn_src_id, src_type_id,
8139                     subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
8140                     user_id, login_id, req_id, prg_appid, prg_id,
8141                     O_Error_Num, O_Error_Code, O_Error_Message);
8142 
8143                 if (O_Error_Num <> 0) then
8144                   raise errexit;
8145                 end if;
8146 
8147               end if; /* if (NOT iexp_flg = 1) */
8148             end if; /* if (NOT exp_sub = 1) and (NOT iexp_flg = 1) */
8149           --}
8150 
8151           /* OPM INVCONV  umoogala Process-Discrete Xfers Enh. */
8152           elsif (txn_act_id = 3 AND p_qty > 0)
8153           then
8154           --{
8155             /* This is a direct transfer from process mfg. org. For
8156              * discrete-discrete xfers, this is always done by sending
8157              * orgs worker. But, for process discrete transfers since
8158              * the sending org is process org it will not be done. So,
8159              * only for process-discrete xfers we've to handle this
8160              * condition.
8161              *
8162              * Reason for adding this is:
8163              *  Currently, for direct interorg xfers, only shipping side of
8164              *  the txn is queried and using this txn receiving side is costed.
8165              *  This is ok for discrete-to-discrete transfers.
8166              *
8167              *  But, for process-to-discrete transfers, shipping side is
8168              *  process mfg org, which will never get picked up. As a result
8169              *  of this, receiving side i.e., discrete org receiving txn is
8170              *  not getting costed.
8171              *
8172              *  Now, since the receiving txn for direct interorg xfer is comming in
8173              *  we need to process it accordingly. Existing code would not work
8174              *  as it assumes that we are processing shipping row.
8175              *
8176              *  Following code will handle above scenario.
8177              *
8178              *  Following are the accouting entries:
8179              *  For Asset Inv:
8180              *    Dr. Inv Valuation @ Org Cost
8181              *        Cr InterOrg Payables with Transfer Price
8182              *    Dr. PPV, if any
8183              *
8184              *  For Exp:
8185              *    Dr. Exp Acct @ Transfer Price
8186              *        Cr InterOrg Payables with Transfer Price
8187              */
8188 
8189             fnd_file.put_line(fnd_file.log, 'processing direct interorg receiving txn.');
8190 
8191 
8192             /*----------------------------------------------------------------------+
8193              | Cr receiving org payable @ transfer price
8194              +----------------------------------------------------------------------*/
8195 
8196             /* Amount to account */
8197             payamt := interorg_xfr_price * p_qty;
8198 
8199 
8200             user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8201                                -1, 9, 1, 0,
8202                                subinv, cg_id, 0, snd_rcv,
8203                                O_Error_Num, O_Error_Code, O_Error_Message);
8204 
8205             if (O_Error_Num <> 0) then
8206               raise errexit;
8207             end if;
8208 
8209             if user_acct_id > 0
8210             then
8211               io_pay_acct := user_acct_id;
8212             end if;
8213 
8214             fnd_file.put_line(fnd_file.log, 'Cr Payables with ' || payamt || ' ' || pri_curr);
8215             CSTPACDP.insert_account
8216                (org_id, trans_id, item_id, -1 * payamt,
8217                 -1 * p_qty, io_pay_acct, sob_id, 9, NULL, NULL,
8218                 txn_date, txn_src_id, src_type_id,
8219                 pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, 1,   /* INVCONV ANTHIYAG Bug#5352186 09-Sep-2006 */
8220                 user_id, login_id, req_id, prg_appid, prg_id,
8221                 O_Error_Num, O_Error_Code, O_Error_Message);
8222 
8223             if (O_Error_Num <> 0) then
8224               raise errexit;
8225             end if;
8226 
8227 
8228             /*----------------------------------------------------------------------+
8229              | Dr Inv. Valuations or Expense Account
8230              +----------------------------------------------------------------------*/
8231 
8232             if (exp_flg = 1) /* receiving org expense item */
8233             then
8234 
8235               /* Dr expense account for direct transfer */
8236 
8237               user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8238                                  -1, 2, 1, 0,
8239                                  subinv, cg_id, 0, snd_rcv,
8240                                  O_Error_Num, O_Error_Code, O_Error_Message);
8241 
8242               if (O_Error_Num <> 0) then
8243                 raise errexit;
8244               end if;
8245 
8246               if user_acct_id > 0
8247               then
8248                 exp_acct := user_acct_id;
8249               end if;
8250 
8251               /*----------------------------------------------------------------------+
8252                | Dr Exp Acct
8253                +----------------------------------------------------------------------*/
8254               fnd_file.put_line(fnd_file.log, 'Dr Exp Acct');
8255               CSTPACDP.insert_account
8256                  (org_id, trans_id, item_id, interorg_xfr_price * p_qty,
8257                   p_qty, exp_acct, sob_id, 2, NULL, NULL,
8258                   txn_date, txn_src_id, src_type_id,
8259                   pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, 1,   /* umoogala Bug#5708387 13-dec-2006 */
8260                   user_id, login_id, req_id, prg_appid, prg_id,
8261                   O_Error_Num, O_Error_Code, O_Error_Message);
8262 
8263               if (O_Error_Num <> 0) then
8264                 raise errexit;
8265               end if;
8266 
8267               goto update_cost;
8268                -- This is the final debit.
8269                -- No PPV and OH absorption are used.
8270                -- break;
8271 
8272             else  /* Asset Item */
8273 
8274               /* Dr Inv. Valuation @ org cost */
8275 
8276               /*----------------------------------------------------------------------+
8277                | Dr Inv. Val @ org cost
8278                +----------------------------------------------------------------------*/
8279               fnd_file.put_line(fnd_file.log, 'Dr Inv Val @ org cost');
8280               CSTPACDP.distribute_accounts(
8281                   org_id, trans_id,
8282                   0, cg_id , txn_act_id,
8283                   item_id, p_qty, 1,
8284                   1, 0, NULL, mat_acct, mat_ovhd_acct,
8285                   res_acct, osp_acct, ovhd_acct,
8286                   sob_id, txn_date, txn_src_id, src_type_id,
8287                   subinv, snd_rcv, pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, /* umoogala Bug#5708387 13-dec-2006 */
8288                   user_id, login_id, req_id, prg_appid, prg_id,
8289                   O_Error_Num, O_Error_Code, O_Error_Message);
8290 
8291               if (O_Error_Num <> 0) then
8292                 raise errexit;
8293               end if;
8294 
8295             end if;  -- if (exp_flg = 1)
8296 
8297 
8298             /* receiving overhead absorption */
8299             /* Earn MOH only for asset subinventory */
8300 
8301             if (NOT exp_flg = 1) AND (NOT exp_sub = 1) AND (l_earn_moh <> 0)
8302             then
8303 
8304               /*----------------------------------------------------------------------+
8305                | Booking Overhead Acct
8306                +----------------------------------------------------------------------*/
8307                 fnd_file.put_line(fnd_file.log, 'Booking Overhead Acct');
8308                 CSTPACDP.ovhd_accounts(org_id, trans_id, item_id,
8309                               -1 * p_qty, sob_id,
8310                               txn_date, txn_src_id, src_type_id, subinv,
8311                               snd_rcv, pri_curr,
8312                               l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, /* umoogala Bug#5708387 13-dec-2006 */
8313                               user_id, login_id, req_id, prg_appid, prg_id,
8314                               O_Error_Num, O_Error_Code, O_Error_Message);
8315 
8316                 if (O_Error_Num <> 0) then
8317                   raise errexit;
8318                 end if;
8319 
8320             end if;
8321 
8322 
8323             /*----------------------------------------------------------------------+
8324              | Dr receiving Inter-org PPV
8325              +----------------------------------------------------------------------*/
8326 
8327             /* Changes for PJM Standard Costing - Update MMT with PPV amount*/
8328 
8329 
8330             UPDATE mtl_material_transactions
8331                SET variance_amount = (SELECT -1 * nvl(sum(base_transaction_value),0)
8332                                         FROM mtl_transaction_accounts mta
8333                                        WHERE mta.transaction_id = trans_id
8334                                          AND mta.organization_id = org_id
8335                                          AND encumbrance_type_id is null)
8336              WHERE transaction_id = trans_id
8337             RETURNING variance_amount INTO ppv_amt;
8338 
8339 
8340             user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8341                                -1, 6, 1, 0,
8342                                subinv, cg_id, 0, snd_rcv,
8343                                O_Error_Num, O_Error_Code, O_Error_Message);
8344 
8345             if (O_Error_Num <> 0) then
8346               raise errexit;
8347             end if;
8348 
8349             if user_acct_id > 0
8350             then
8351               io_ppv_acct := user_acct_id;
8352             end if;
8353 
8354             fnd_file.put_line(fnd_file.log, 'Dr PPV with ' || ppv_amt || ' ' || pri_curr);
8355             CSTPACDP.insert_account
8356                (org_id, trans_id, item_id, ppv_amt,
8357                 p_qty, io_ppv_acct, sob_id, 6, NULL, NULL,
8358                 txn_date, txn_src_id, src_type_id,
8359                 pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, 1, /* umoogala Bug#5708387 13-dec-2006 */
8360                 user_id, login_id, req_id, prg_appid, prg_id,
8361                 O_Error_Num, O_Error_Code, O_Error_Message);
8362 
8363             if (O_Error_Num <> 0) then
8364               raise errexit;
8365             end if;
8366 
8367           end if;
8368           --}
8369       -- }
8370       -- end of 'when (txn_act_id = 3 or txn_act_id = 21)'
8371 
8372       when ((txn_act_id = 12) /* Intransit receipt */
8373             OR
8374 	    (txn_act_id = 17))
8375       then
8376       -- {
8377 
8378 
8379           l_stmt_num := 840;
8380 
8381           <<FOB_POINT>>
8382           case
8383             when (fob_pt = 1)  /* FOB Shipping */
8384             then
8385             -- {
8386 
8387               /* Receiving Organization */
8388 
8389                   /* Receiving is std/exp, std/not exp */
8390 
8391                   /*----------------------------------------------------------------------+
8392                    | Dr Inv Val @rec org cost if Item is an Asset Item
8393                    +----------------------------------------------------------------------*/
8394 		  --
8395 		  -- Bug 5401272: If receiving into exp sub, then book to
8396 		  -- expense account.
8397 		  --
8398                   if ((exp_flg = 1 or exp_sub = 1) AND NOT iexp_flg = 1)
8399                   then
8400                   -- {
8401 
8402                     /*----------------------------------------------------------------------+
8403                      | Dr Exp Acct
8404                      +----------------------------------------------------------------------*/
8405                     -- receiving asset item into exp sub
8406                     l_stmt_num := 910;
8407 
8408                     user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8409                                        -1, 2, 1, 0,
8410                                        subinv, cg_id, 0, snd_rcv,
8411                                        O_Error_Num, O_Error_Code, O_Error_Message);
8412 
8413                     if (O_Error_Num <> 0) then
8414                       raise errexit;
8415                     end if;
8416 
8417                     if user_acct_id > 0
8418                     then
8419                       exp_acct := user_acct_id;
8420                     end if;
8421 
8422                     fnd_file.put_line(fnd_file.log, 'Dr Exp Acct');
8423                     CSTPACDP.distribute_accounts(
8424                         org_id, trans_id,
8425                         0, cg_id , txn_act_id,
8426                         item_id, p_qty, 2,
8427                         0, 0, exp_acct, NULL, NULL,
8428                         NULL, NULL, NULL,
8429                         sob_id, txn_date, txn_src_id, src_type_id,
8430                         subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
8431                         user_id, login_id, req_id, prg_appid, prg_id,
8432                         O_Error_Num, O_Error_Code, O_Error_Message);
8433 
8434                     if (O_Error_Num <> 0) then
8435                       raise errexit;
8436                     end if;
8437 
8438                   -- if (exp_flg = 1 AND NOT iexp_flg = 1)
8439                   -- }
8440 
8441                   elsif (NOT exp_flg = 1)
8442                   then
8443                   -- {
8444                     /* receiving is not expense */
8445 
8446 
8447                     /*----------------------------------------------------------------------+
8448                      | Dr Inv. Val @ org cost
8449                      +----------------------------------------------------------------------*/
8450                     fnd_file.put_line(fnd_file.log, 'Dr Inv Val @ org cost');
8451                     CSTPACDP.distribute_accounts(
8452                         org_id, trans_id,
8453                         0, cg_id , txn_act_id,
8454                         item_id, p_qty, 1,
8455                         1, 0, NULL, mat_acct, mat_ovhd_acct,
8456                         res_acct, osp_acct, ovhd_acct,
8457                         sob_id, txn_date, txn_src_id, src_type_id,
8458                         subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
8459                         user_id, login_id, req_id, prg_appid, prg_id,
8460                         O_Error_Num, O_Error_Code, O_Error_Message);
8461 
8462                     if (O_Error_Num <> 0) then
8463                       raise errexit;
8464                     end if;
8465 
8466                   end if; -- if (NOT exp_flg = 1)
8467                   -- }
8468 
8469                   /* Cr Receiving Org Intransit only for asset item */
8470                   -- {
8471                   if (NOT iexp_flg = 1)
8472                   then
8473 
8474                     user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8475                                        -1, 14, 1, 0,
8476                                        subinv, cg_id, 0, snd_rcv,
8477                                        O_Error_Num, O_Error_Code, O_Error_Message);
8478 
8479                     if (O_Error_Num <> 0) then
8480                       raise errexit;
8481                     end if;
8482 
8483                     if user_acct_id > 0
8484                     then
8485                       io_inv_acct := user_acct_id;
8486                     end if;
8487 
8488                     /*----------------------------------------------------------------------+
8489                      | Cr Intransit Acct @ org cost
8490                      +----------------------------------------------------------------------*/
8491                     fnd_file.put_line(fnd_file.log, 'Cr Intransit Acct @ org cost');
8492                     CSTPACDP.distribute_accounts(
8493                         org_id, trans_id,
8494                         0, cg_id , txn_act_id,
8495                         item_id, -1 * p_qty, 14,
8496                         0, 0, io_inv_acct, NULL, NULL, NULL, NULL,NULL,
8497                         sob_id, txn_date, txn_src_id, src_type_id,
8498                         subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
8499                         user_id, login_id, req_id, prg_appid, prg_id,
8500                         O_Error_Num, O_Error_Code, O_Error_Message);
8501 
8502                     if (O_Error_Num <> 0) then
8503                       raise errexit;
8504                     end if;
8505 
8506                   end if; -- if (NOT iexp_flg = 1)
8507                   -- }
8508               -- }
8509             when ((fob_pt = 2) OR                /* FOB point Delivery */
8510 	          (txn_act_id = 17))             -- Bug 5349860: umoogala. Added this line
8511             then
8512             -- {
8513 
8514 
8515               /*----------------------------------------------------------------------+
8516                | Cr receiving org payable
8517                +----------------------------------------------------------------------*/
8518 
8519               l_stmt_num := 930;
8520 
8521               payamt := -1.0 * txf_price * p_qty;
8522 
8523 
8524               user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8525                                  -1, 9, 1, 0,
8526                                  subinv, cg_id, 0, snd_rcv,
8527                                  O_Error_Num, O_Error_Code, O_Error_Message);
8528 
8529               if (O_Error_Num <> 0) then
8530                 raise errexit;
8531               end if;
8532 
8533               if user_acct_id > 0
8534               then
8535                 io_pay_acct := user_acct_id;
8536               end if;
8537 
8538               fnd_file.put_line(fnd_file.log, 'Cr Payables with ' || payamt || ' ' || pri_curr);
8539               CSTPACDP.insert_account
8540                  (org_id, trans_id, item_id, payamt,
8541                   -1 * p_qty, io_pay_acct, sob_id, 9, NULL, NULL,
8542                   txn_date, txn_src_id, src_type_id,
8543                   pri_curr, l_snd_curr,l_conv_date, l_conv_rate, l_curr_type, 1,   /* INVCONV ANTHIYAG Bug#5352186 09-Sep-2006 End */
8544                   user_id, login_id, req_id, prg_appid, prg_id,
8545                   O_Error_Num, O_Error_Code, O_Error_Message);
8546 
8547               if (O_Error_Num <> 0) then
8548                 raise errexit;
8549               end if;
8550 
8551 
8552               /* receiving org inv. valuation acct */
8553 	      --
8554 	      -- Bug 5349860: For Logical Expense Requisition Receipt (action id 17) we
8555 	      --              should always Dr Expense Account. Added (txn_act_id <> 17)
8556 	      --              condition to the following IF stmt
8557 	      --
8558               if (txn_act_id <> 17) AND (NOT exp_flg = 1)
8559               then
8560 
8561                 l_stmt_num := 931;
8562                 /*----------------------------------------------------------------------+
8563                  | Dr Inv. Val @ org cost
8564                  +----------------------------------------------------------------------*/
8565                 fnd_file.put_line(fnd_file.log, 'Dr Inv Val @ org cost');
8566                 CSTPACDP.distribute_accounts(
8567                     org_id, trans_id,
8568                     0, cg_id , txn_act_id,
8569                     item_id, p_qty, 1,
8570                     1, 0, NULL, mat_acct, mat_ovhd_acct,
8571                     res_acct, osp_acct, ovhd_acct,
8572                     sob_id, txn_date, txn_src_id, src_type_id,
8573                     subinv, snd_rcv, pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, /* umoogala Bug#5708387 13-dec-2006 */
8574                     user_id, login_id, req_id, prg_appid, prg_id,
8575                     O_Error_Num, O_Error_Code, O_Error_Message);
8576 
8577                 if (O_Error_Num <> 0) then
8578                   raise errexit;
8579                 end if;
8580 
8581 
8582                 /* receiving overhead absorption */
8583 
8584                 /*----------------------------------------------------------------------+
8585                  | Booking Overhead Acct
8586                  +----------------------------------------------------------------------*/
8587 	        IF (txn_act_id <> 17) AND -- Bug 5349860
8588 		   (exp_flg = 0) AND (exp_sub = 0) AND (l_earn_moh <> 0)
8589 	        THEN
8590                   fnd_file.put_line(fnd_file.log, 'Booking Overhead Acct');
8591                   CSTPACDP.ovhd_accounts(org_id, trans_id, item_id,
8592                                 -1 * p_qty, sob_id,
8593                                 txn_date, txn_src_id, src_type_id, subinv,
8594                                 snd_rcv, pri_curr,
8595                   	        l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, /* umoogala Bug#5708387 13-dec-2006 */
8596                                 user_id, login_id, req_id, prg_appid, prg_id,
8597                                 O_Error_Num, O_Error_Code, O_Error_Message);
8598 
8599                   if (O_Error_Num <> 0) then
8600                     raise errexit;
8601                   end if;
8602                 END IF;
8603 
8604 
8605                 /*----------------------------------------------------------------------+
8606                  | Dr PPV
8607                  +----------------------------------------------------------------------*/
8608 
8609 		IF (txn_act_id <> 17) -- Bug 5349860
8610 		THEN
8611 
8612                   UPDATE mtl_material_transactions
8613                      SET variance_amount = (SELECT -1 * nvl(sum(base_transaction_value),0)
8614                                               FROM mtl_transaction_accounts mta
8615                                              WHERE mta.transaction_id = trans_id
8616                                                AND mta.organization_id = org_id
8617                                                AND encumbrance_type_id is null)
8618                    WHERE transaction_id = trans_id
8619                   RETURNING variance_amount INTO ppv_amt;
8620 
8621 
8622                   user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8623                                      -1, 6, 1, 0,
8624                                      subinv, cg_id, 0, snd_rcv,
8625                                      O_Error_Num, O_Error_Code, O_Error_Message);
8626 
8627                   if (O_Error_Num <> 0) then
8628                     raise errexit;
8629                   end if;
8630 
8631                   if user_acct_id > 0
8632                   then
8633                     io_ppv_acct := user_acct_id;
8634                   end if;
8635 
8636                   fnd_file.put_line(fnd_file.log, 'Dr PPV with ' || ppv_amt || ' ' || pri_curr);
8637                   CSTPACDP.insert_account
8638                      (org_id, trans_id, item_id, ppv_amt,
8639                       p_qty, io_ppv_acct, sob_id, 6, NULL, NULL,
8640                       txn_date, txn_src_id, src_type_id,
8641                       pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, 1, /* umoogala Bug#5708387 13-dec-2006 */
8642                       user_id, login_id, req_id, prg_appid, prg_id,
8643                       O_Error_Num, O_Error_Code, O_Error_Message);
8644 
8645                   if (O_Error_Num <> 0) then
8646                     raise errexit;
8647                   end if;
8648 
8649 		END IF;
8650 
8651               else
8652 
8653                 /*----------------------------------------------------------------------+
8654                  | Dr Exp Acct
8655                  +----------------------------------------------------------------------*/
8656 
8657                 l_stmt_num := 920;
8658 
8659                 user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8660                                    -1, 2, 1, 0,
8661                                    subinv, cg_id, 0, snd_rcv,
8662                                    O_Error_Num, O_Error_Code, O_Error_Message);
8663 
8664                 if (O_Error_Num <> 0) then
8665                   raise errexit;
8666                 end if;
8667 
8668                 if user_acct_id > 0
8669                 then
8670                   exp_acct := user_acct_id;
8671                 end if;
8672 
8673                 fnd_file.put_line(fnd_file.log, 'Dr Exp Acct');
8674                 CSTPACDP.insert_account
8675                    (org_id, trans_id, item_id, -1 * payamt,
8676                     p_qty, exp_acct, sob_id, 2, NULL, NULL,
8677                     txn_date, txn_src_id, src_type_id,
8678                     pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, 1, /* umoogala Bug#5708387 13-dec-2006 */
8679                     user_id, login_id, req_id, prg_appid, prg_id,
8680                     O_Error_Num, O_Error_Code, O_Error_Message);
8681 
8682                 if (O_Error_Num <> 0) then
8683                   raise errexit;
8684                 end if;
8685 
8686               end if;  -- if (NOT exp_flg = 1)
8687 
8688               -- break;
8689               -- }
8690             end case FOB_POINT;
8691       -- }
8692 
8693 
8694       /* OPM INVCONV  umoogala
8695        * Added following two new logical txns created for
8696        * process-discrete transfers. These logical txns are created
8697        * using INV_LOGICAL_TRANSACTIONS_PUB. create_opm_disc_logical_trx
8698        * Any issues with data should be fixed in above procedure.
8699        *
8700        * Txn Action 22 (Logical Intransit Shipment):
8701        *  Transfer is from Discrete Org to Process Org.
8702        *  For source Inventory (13) and Type Logical Intransit Shipment (60)
8703        *      source Int Req (8)    and Type Logical Intransit Shipment (65)
8704        *
8705        * Txn Action 15 (Logical  Intransit Receipt):
8706        *  Transfer is from Process Org to Discrete Org.
8707        *  For source Inventory (13) and Type Logical Intransit Receipt (59)
8708        *      source Int Req (7)    and Type Logical Intransit Receipt (76)
8709        */
8710 
8711       when (txn_act_id = 22)  /* Logical In-transit Shipment */
8712       then
8713       -- {
8714         /*
8715          * Discrete -> Process Transfer with FOB Receiving
8716          *
8717          * Here sending Org is Std Costing Discrete Org and Receiving Org
8718          * is Process Mfg(OPM) Org.
8719          * This transaction, for sending org, gets created during In-transit
8720          * Receipt(12) in Receiving Org.
8721          *
8722          * Following accounts needs to be booked:
8723          *
8724          * FOB = Receiving(2)
8725          *
8726          * Account                    Dr               Cr
8727          * ------------------------   --------------   ---------------
8728          *   Inter-Org Receivables    Transfer Price
8729          *   In-transit Inventory                      Sending Org Cost
8730          *   Freight Expense A/c                       Freight
8731          *   InterOrg Profit          (Org Cost +
8732          *                             Freight -
8733          *                             Transfer Price)
8734          * ------------------------------------------------------------
8735          *
8736          * Transfer Price  : is in MMT. variable  interorg_xfr_price holds it
8737          * Sending Org Cost: query it
8738          * Freight         : Trp Cost in MMT. varaible trp_cost holds it
8739          */
8740 
8741 
8742 
8743         /*----------------------------------------------------------------------+
8744          | Cr In-transit Inventory with Sending Org Cost for Asset Items Only
8745 	 |
8746 	 | Bug 5332764: Exp Item with FOB Rct, shipment is not accounted. So, no
8747 	 | need to book intransit entry.
8748          +----------------------------------------------------------------------*/
8749          if (NOT iexp_flg = 1)  -- Bug 5332764.
8750          then
8751           user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8752                              -1, 14, 1, 0,
8753                              subinv, cg_id, 0, snd_rcv,
8754                              O_Error_Num, O_Error_Code, O_Error_Message);
8755 
8756           if (O_Error_Num <> 0) then
8757             raise errexit;
8758           end if;
8759 
8760           if user_acct_id > 0
8761           then
8762             io_inv_acct := user_acct_id;
8763           end if;
8764 
8765           fnd_file.put_line(fnd_file.log, 'Cr Intransit Acct @ org cost');
8766           CSTPACDP.distribute_accounts(
8767               org_id, trans_id,
8768               0, cg_id , txn_act_id,
8769               item_id, -1 * p_qty, 14,
8770               0, 0, io_inv_acct, NULL, NULL, NULL, NULL,NULL,
8771               sob_id, txn_date, txn_src_id, src_type_id,
8772               subinv, snd_rcv, pri_curr, NULL, NULL, NULL, NULL,
8773               user_id, login_id, req_id, prg_appid, prg_id,
8774               O_Error_Num, O_Error_Code, O_Error_Message);
8775 
8776           if (O_Error_Num <> 0) then
8777             raise errexit;
8778           end if;
8779         end if;
8780 
8781         /*----------------------------------------------------------------------+
8782          | Cr Sending Org Freight absorption account
8783          +----------------------------------------------------------------------*/
8784         if(trp_cost <> 0)
8785         then
8786 
8787           user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8788                              -1, 12, 1, 0,
8789                              subinv, cg_id, 0, snd_rcv,
8790                              O_Error_Num, O_Error_Code, O_Error_Message);
8791 
8792           if (O_Error_Num <> 0) then
8793             raise errexit;
8794           end if;
8795 
8796           if user_acct_id > 0
8797           then
8798             trp_acct := user_acct_id;
8799           end if;
8800 
8801           fnd_file.put_line(fnd_file.log, 'Cr Freight Acct with ' || trp_cost || ' ' || pri_curr);
8802           CSTPACDP.insert_account
8803              (org_id, trans_id, item_id, -1 * trp_cost,
8804               p_qty, trp_acct, sob_id, 12, NULL, NULL,
8805               txn_date, txn_src_id, src_type_id,
8806               pri_curr, NULL, NULL, NULL, NULL,1,
8807               user_id, login_id, req_id, prg_appid, prg_id,
8808               O_Error_Num, O_Error_Code, O_Error_Message);
8809 
8810           if (O_Error_Num <> 0) then
8811             raise errexit;
8812           end if;
8813 
8814         end if;
8815 
8816         /*----------------------------------------------------------------------+
8817          | Dr Sending Org Receivable with Transfer Price
8818          +----------------------------------------------------------------------*/
8819 
8820         user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8821                            1, 10, 1, 0,
8822                            subinv, cg_id, 0, snd_rcv,
8823                            O_Error_Num, O_Error_Code, O_Error_Message);
8824 
8825         if (O_Error_Num <> 0) then
8826           raise errexit;
8827         end if;
8828 
8829         if user_acct_id > 0
8830         then
8831           io_rec_acct := user_acct_id;
8832         end if;
8833 
8834 
8835         fnd_file.put_line(fnd_file.log, 'Dr Receivalbes with ' || (interorg_xfr_price * p_qty) || ' ' || pri_curr);
8836         CSTPACDP.insert_account
8837            (org_id, trans_id, item_id, interorg_xfr_price * p_qty,
8838             p_qty, io_rec_acct, sob_id, 10, NULL, NULL,
8839             txn_date, txn_src_id, src_type_id,
8840             pri_curr, NULL, NULL, NULL, NULL,1,
8841             user_id, login_id, req_id, prg_appid, prg_id,
8842             O_Error_Num, O_Error_Code, O_Error_Message);
8843 
8844         if (O_Error_Num <> 0) then
8845           raise errexit;
8846         end if;
8847 
8848         /*----------------------------------------------------------------------+
8849          | Dr Book interorg profit
8850          +----------------------------------------------------------------------*/
8851         --
8852         -- Bug 5233635: Should not book InterOrg Profit If Intercompany Invoicing is enabled.
8853 	-- Following flag is set at the beginning of the procedure.
8854         --
8855         if  (src_type_id = 8)
8856         and (l_org_ou <> l_txf_org_ou)
8857         and (l_io_invoicing = 1)
8858         and (txn_act_id = 21) -- Bug 5351724
8859         and (iexp_flg   = 0)    -- Bug 5348953: No IC Invoicing for expense items
8860         then
8861           NULL;
8862           fnd_file.put_line(fnd_file.log, 'No need to book InterOrg Profit since InterCompany Invoicing ' ||
8863             'is enabled.');
8864         else
8865 
8866           -- interorg_profit := interorg_xfr_price - (item_cost + (trp_cost/p_qty));
8867           SELECT SUM(NVL(base_transaction_value,0))
8868            INTO interorg_profit
8869            FROM mtl_transaction_accounts
8870           WHERE transaction_id  = trans_id
8871             AND organization_id = org_id
8872           ;
8873 
8874           if (interorg_profit <> 0)
8875           then
8876 
8877             user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8878                                -1, G_INTERORG_PROFIT_ACCT, 1, 0,
8879                                subinv, cg_id, 0, snd_rcv,
8880                                O_Error_Num, O_Error_Code, O_Error_Message);
8881 
8882             if (O_Error_Num <> 0) then
8883               raise errexit;
8884             end if;
8885 
8886             if user_acct_id > 0
8887             then
8888               interorg_profit_acct := user_acct_id;
8889             end if;
8890 
8891             fnd_file.put_line(fnd_file.log, 'Dr InterOrg Profit with ' || nvl(interorg_profit,0) || ' ' || pri_curr);
8892             CSTPACDP.insert_account
8893                (org_id, trans_id, item_id, -1 * interorg_profit, /* ANTHIYAG Bug#5459157 11-Aug-2006 */
8894                 p_qty, interorg_profit_acct, sob_id,
8895                 G_INTERORG_PROFIT_ACCT, NULL, NULL,
8896                 txn_date, txn_src_id, src_type_id,
8897                 pri_curr, NULL, NULL, NULL, NULL,1,
8898                 user_id, login_id, req_id, prg_appid, prg_id,
8899                 O_Error_Num, O_Error_Code, O_Error_Message);
8900 
8901             if (O_Error_Num <> 0) then
8902               raise errexit;
8903             end if;
8904           end if;
8905         end if; -- Bug 5233635
8906 
8907       -- }
8908 
8909       when (txn_act_id = 15) /* Logical In-transit Receipt */
8910       then
8911       -- {
8912         /*
8913          * Process -> Discrete Transfer with FOB Shipping
8914          *
8915          * Here sending Org is Process Mfg(OPM) Org and Receiving Org
8916          * is Std Costing Discrete Org.
8917          * This transaction, for receving org, gets created during In-transit
8918          * Shipment(12) in Sending Org i.e., discrete org
8919          *
8920          * Following accounts needs to be booked:
8921          *
8922          * FOB = Shipping(1)
8923          *
8924          * Account                    Dr               Cr
8925          * ------------------------   --------------   ----------------
8926          *   In-transit Inventory     Recv. Org Cost
8927          *   Inter-Org Payables                        Transfer Price
8928          *   Freight Expense A/c                       Freight
8929          *   InterOrg Profit          (Transfer Price +
8930          *                             Freight -
8931          *                             Recv. Org Cost)
8932          * ------------------------------------------------------------
8933          *
8934          * Transfer Price  : is in MMT. variable  interorg_xfr_price holds it
8935          * Recv Org Cost   : query it
8936          * Freight         : Trp Cost in MMT. varaible trp_cost holds it
8937          */
8938 
8939         /*----------------------------------------------------------------------+
8940          | Cr receiving org payable with transfer price
8941          +----------------------------------------------------------------------*/
8942 
8943         payamt := -1.0 * txf_price * p_qty;
8944 
8945         user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8946                            -1, 9, 1, 0,
8947                            subinv, cg_id, 0, snd_rcv,
8948                            O_Error_Num, O_Error_Code, O_Error_Message);
8949 
8950         if (O_Error_Num <> 0) then
8951           raise errexit;
8952         end if;
8953 
8954         if user_acct_id > 0
8955         then
8956           io_pay_acct := user_acct_id;
8957         end if;
8958 
8959         fnd_file.put_line(fnd_file.log, 'Cr Payables with ' || payamt || ' ' || pri_curr);
8960         CSTPACDP.insert_account
8961            (org_id, trans_id, item_id, payamt,
8962             -1 * p_qty, io_pay_acct, sob_id, 9, NULL, NULL,
8963             txn_date, txn_src_id, src_type_id,
8964             pri_curr, l_snd_curr,l_conv_date, l_conv_rate, l_curr_type, 1,   /* INVCONV ANTHIYAG Bug#5352186 09-Sep-2006 End */
8965             user_id, login_id, req_id, prg_appid, prg_id,
8966             O_Error_Num, O_Error_Code, O_Error_Message);
8967 
8968         if (O_Error_Num <> 0) then
8969           raise errexit;
8970         end if;
8971 
8972 
8973         /*----------------------------------------------------------------------+
8974          | Cr freight
8975          +----------------------------------------------------------------------*/
8976         if (trp_cost <> 0)
8977         then
8978 
8979           user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
8980                              -1, 12, 1, 0,
8981                              subinv, cg_id, 0, snd_rcv,
8982                              O_Error_Num, O_Error_Code, O_Error_Message);
8983 
8984           if (O_Error_Num <> 0) then
8985             raise errexit;
8986           end if;
8987 
8988           if user_acct_id > 0
8989           then
8990             trp_acct := user_acct_id;
8991           end if;
8992 
8993           fnd_file.put_line(fnd_file.log, 'Cr Freight Acct with ' || trp_cost || ' ' || pri_curr);
8994           CSTPACDP.insert_account
8995              (org_id, trans_id, item_id, -1 * trp_cost,
8996               p_qty, trp_acct, sob_id, 12, NULL, NULL,
8997               txn_date, txn_src_id, src_type_id,
8998               pri_curr, l_snd_curr,l_conv_date, l_conv_rate, l_curr_type, 1, /* INVCONV ANTHIYAG Bug#5352186 09-Sep-2006 End */
8999               user_id, login_id, req_id, prg_appid, prg_id,
9000               O_Error_Num, O_Error_Code, O_Error_Message);
9001 
9002           if (O_Error_Num <> 0) then
9003             raise errexit;
9004           end if;
9005 
9006         end if; -- if (trp_cost <> 0)
9007 
9008 
9009         /*----------------------------------------------------------------------+
9010          | Dr Variance amount
9011          +----------------------------------------------------------------------*/
9012         if (var_amt <> 0)
9013         then
9014 
9015           user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
9016                              -1, 13, 1, 0,
9017                              subinv, cg_id, 0, snd_rcv,
9018                              O_Error_Num, O_Error_Code, O_Error_Message);
9019 
9020           if (O_Error_Num <> 0) then
9021             raise errexit;
9022           end if;
9023 
9024           if user_acct_id > 0
9025           then
9026             acv_acct := user_acct_id;
9027           end if;
9028 
9029           fnd_file.put_line(fnd_file.log, 'Cr variance account with ' || var_amt || ' ' || pri_curr);
9030           CSTPACDP.insert_account
9031              (org_id, trans_id, item_id, var_amt,
9032               p_qty, acv_acct, sob_id, 13, NULL, NULL,
9033               txn_date, txn_src_id, src_type_id,
9034               pri_curr, l_snd_curr,l_conv_date, l_conv_rate, l_curr_type, 1, /* umoogala Bug#5708387 13-dec-2006 */
9035               user_id, login_id, req_id, prg_appid, prg_id,
9036               O_Error_Num, O_Error_Code, O_Error_Message);
9037 
9038           if (O_Error_Num <> 0) then
9039             raise errexit;
9040           end if;
9041 
9042         end if; /* if (var_amt <> 0) */
9043 
9044         /*----------------------------------------------------------------------+
9045          | Dr Expense Account
9046          +----------------------------------------------------------------------*/
9047 	--
9048 	-- Bug 5401272: check items exp flag instead of exp_flag which is based
9049 	-- on item/subInv combination.
9050 	-- Book Exp Account for Exp Items Only
9051 	-- Asset to exp sub, still should got to intransit account
9052 	--
9053         if (iexp_flg = 1)
9054         then
9055 
9056             user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
9057                                -1, 2, 1, 0,
9058                                subinv, cg_id, 0, snd_rcv,
9059                                O_Error_Num, O_Error_Code, O_Error_Message);
9060 
9061             if (O_Error_Num <> 0) then
9062               raise errexit;
9063             end if;
9064 
9065             if user_acct_id > 0
9066             then
9067               exp_acct := user_acct_id;
9068             end if;
9069 
9070             fnd_file.put_line(fnd_file.log, 'Dr Exp Acct');
9071             CSTPACDP.insert_account
9072                (org_id, trans_id, item_id, trp_cost,
9073                 p_qty, exp_acct, sob_id, 2, NULL, NULL,
9074                 txn_date, txn_src_id, src_type_id,
9075                 pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, 1, /* umoogala Bug#5708387 13-dec-2006 */
9076                 user_id, login_id, req_id, prg_appid, prg_id,
9077                 O_Error_Num, O_Error_Code, O_Error_Message);
9078 
9079             if (O_Error_Num <> 0) then
9080               raise errexit;
9081             end if;
9082 
9083             /* For this 2 cases, this is the final debit. */
9084             /* No PPV and OH absorption are used. */
9085             goto update_cost;
9086         end if;
9087 
9088         /*----------------------------------------------------------------------+
9089          | Dr intransit @ receiving org cost
9090          +----------------------------------------------------------------------*/
9091 
9092         user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
9093                            -1, 14, 1, 0,
9094                            subinv, cg_id, 0, snd_rcv,
9095                            O_Error_Num, O_Error_Code, O_Error_Message);
9096 
9097         if (O_Error_Num <> 0) then
9098           raise errexit;
9099         end if;
9100 
9101         if user_acct_id > 0
9102         then
9103           io_inv_acct := user_acct_id;
9104         end if;
9105 
9106         fnd_file.put_line(fnd_file.log, 'Dr Intransit Acct @ org cost');
9107         CSTPACDP.distribute_accounts(
9108             org_id, trans_id,
9109             0, cg_id , txn_act_id,
9110             item_id, p_qty, 14,
9111             0, 0, io_inv_acct, NULL, NULL, NULL, NULL,NULL,
9112             sob_id, txn_date, txn_src_id, src_type_id,
9113             subinv, snd_rcv, pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, /* umoogala Bug#5708387 13-dec-2006 */
9114             user_id, login_id, req_id, prg_appid, prg_id,
9115             O_Error_Num, O_Error_Code, O_Error_Message);
9116 
9117         if (O_Error_Num <> 0) then
9118           raise errexit;
9119         end if;
9120 
9121 
9122         /*----------------------------------------------------------------------+
9123          | Dr Overhead Obsorption account
9124          +----------------------------------------------------------------------*/
9125 
9126 	/* receiving overhead absorption */
9127 	/* Earn MOH only for asset subinventory */
9128 
9129 	fnd_file.put_line(fnd_file.log, '-- Booking Overhead Acct: exp_flg/exp_sub: ' || exp_flg ||'/'||exp_sub ||
9130 	  ' bookMOH Obsorption?: ' || l_earn_moh);
9131 
9132 	if (exp_flg = 0) AND (exp_sub = 0) AND (l_earn_moh <> 0)
9133 	then
9134 
9135 	  /*----------------------------------------------------------------------+
9136 	   | Booking Overhead Acct
9137 	   +----------------------------------------------------------------------*/
9138 	  fnd_file.put_line(fnd_file.log, '-- Booking Overhead Acct');
9139 	  CSTPACDP.ovhd_accounts(org_id, trans_id, item_id,
9140 			-1 * p_qty, sob_id,
9141 			txn_date, txn_src_id, src_type_id, subinv,
9142 			snd_rcv, pri_curr,
9143                         l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, /* umoogala Bug#5708387 13-dec-2006 */
9144 			user_id, login_id, req_id, prg_appid, prg_id,
9145 			O_Error_Num, O_Error_Code, O_Error_Message);
9146 
9147 	  if (O_Error_Num <> 0) then
9148 	    raise errexit;
9149 	  end if;
9150 
9151 	end if;
9152 
9153         /*----------------------------------------------------------------------+
9154          | Dr PPV with above amount
9155          +----------------------------------------------------------------------*/
9156         -- ppv_amt := (item_cost * p_qty) - ((interorg_xfr_price * p_qty) + trp_cost);
9157 
9158 	UPDATE mtl_material_transactions
9159 	   SET variance_amount = (SELECT -1 * nvl(sum(base_transaction_value),0)
9160 				    FROM mtl_transaction_accounts mta
9161 				   WHERE mta.transaction_id = trans_id
9162 				     AND mta.organization_id = org_id
9163 				     AND encumbrance_type_id is null)
9164 	 WHERE transaction_id = trans_id
9165 	RETURNING variance_amount INTO ppv_amt;
9166 
9167 
9168         if (ppv_amt <> 0)
9169         then
9170 
9171           user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
9172                              -1, 6, 1, 0,
9173                              subinv, cg_id, 0, snd_rcv,
9174                              O_Error_Num, O_Error_Code, O_Error_Message);
9175 
9176           if (O_Error_Num <> 0) then
9177             raise errexit;
9178           end if;
9179 
9180           if user_acct_id > 0
9181           then
9182             io_ppv_acct := user_acct_id;
9183           end if;
9184 
9185           fnd_file.put_line(fnd_file.log, 'Dr PPV with ' || ppv_amt || ' ' || pri_curr);
9186           CSTPACDP.insert_account
9187              (org_id, trans_id, item_id, ppv_amt,
9188               p_qty, io_ppv_acct, sob_id, 6, NULL, NULL,
9189               txn_date, txn_src_id, src_type_id,
9190               pri_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, 1, /* umoogala Bug#5708387 13-dec-2006 */
9191               user_id, login_id, req_id, prg_appid, prg_id,
9192               O_Error_Num, O_Error_Code, O_Error_Message);
9193 
9194           if (O_Error_Num <> 0) then
9195             raise errexit;
9196           end if;
9197         end if;
9198 
9199       -- }
9200       -- End of when (txn_act_id = 15) -- Logical In-transit Receipt
9201       --
9202 
9203     end case MAIN_CASE_FOR_ACTION_ID;
9204 
9205 
9206     /*----------------------------------------------------------------------+
9207      | Process encumbrance if PO or REQ or INTERNAL ORDER
9208      +----------------------------------------------------------------------*/
9209     if (src_type_id = 7 OR src_type_id = 8)
9210     then
9211         if (enc_rev is not null AND enc_amt <> 0)
9212         then
9213 
9214             user_acct_id := CSTPSCHK.std_get_account_id( org_id, trans_id,
9215                                -1, 15, 1, 0,
9216                                subinv, cg_id, 0, snd_rcv,
9217                                O_Error_Num, O_Error_Code, O_Error_Message);
9218 
9219             if (O_Error_Num <> 0) then
9220               raise errexit;
9221             end if;
9222 
9223             if user_acct_id > 0
9224             then
9225               enc_acct := user_acct_id;
9226             end if;
9227 
9228             fnd_file.put_line(fnd_file.log, 'Cr Enc Acct with ' || enc_amt || ' ' || pri_curr);
9229             CSTPACDP.insert_account
9230                (org_id, trans_id, item_id, -1*enc_amt,
9231                 p_qty, enc_acct, sob_id, 15, NULL, NULL,
9232                 txn_date, txn_src_id, src_type_id,
9233                 pri_curr, NULL, NULL, NULL, NULL,1,
9234                 user_id, login_id, req_id, prg_appid, prg_id,
9235                 O_Error_Num, O_Error_Code, O_Error_Message);
9236 
9237             if (O_Error_Num <> 0) then
9238               raise errexit;
9239             end if;
9240 
9241         end if;
9242     end if;
9243 
9244     <<update_cost>>
9245     /* Transfer Pricing */
9246     /* Perform accounting adjustment */
9247     /*
9248      * - OPM INVCONV process/discrete Xfer Enh.
9249      * Added 15 and 22 action ids. These gets created only for process/discrete
9250      * transfers.
9251      */
9252     if ( ((txn_act_id = 21 AND src_type_id = 8 AND fob_pt = 1) OR
9253           (txn_act_id = 12 AND src_type_id = 7 AND fob_pt = 2) OR
9254 	  (txn_act_id = 15 AND src_type_id = 7) OR
9255 	  (txn_act_id = 22 AND src_type_id = 8))
9256          AND tprice_option <> 0 )
9257     then
9258     -- {
9259        fnd_file.put_line(FND_FILE.LOG, 'Start calling CST_TPRICE_PVT.Adjust_Acct');
9260 
9261        CST_TPRICE_PVT.Adjust_Acct(1.0, p_tprice_option => tprice_option,
9262                               p_txf_price => txf_price, p_txn_id => trans_id,
9263                               p_cost_grp_id => cg_id, p_txf_cost_grp => txf_cg_id,
9264                               p_item_id => item_id, p_txn_date => txn_date,
9265                               p_qty => p_qty, p_subinv => subinv, p_txf_subinv => txf_subinv,
9266                               p_txn_org_id => org_id, p_txf_org_id => txf_org_id,
9267                               p_txf_txn_id => txf_txn_id, p_txf_cost => txf_cost,
9268                               p_txn_act_id => txn_act_id, p_txn_src_id => txn_src_id,
9269                               p_src_type_id => src_type_id, p_fob_point => fob_pt,
9270                               p_user_id => user_id, p_login_id => login_id, p_req_id => req_id,
9271                               p_prg_appl_id => prg_appid, p_prg_id => prg_id,
9272                               x_return_status => l_return_status, x_msg_count => l_msg_count,
9273                               x_msg_data => l_msg_data, x_error_num => O_Error_Num,
9274                               x_error_code => O_Error_Code, x_error_message => O_Error_Message);
9275 
9276         if O_Error_Num <> 0
9277         then
9278           raise errexit;
9279         end if;
9280 
9281         /* Begin fix for bug#3406184: After Transfer Pricing Accounting,
9282            added code to call balance account to adjust any rounding errors */
9283 
9284         l_stmt_num := 942;
9285 
9286         fnd_file.put_line(fnd_file.log, 'Balancing Accounts');
9287         CSTPACDP.balance_account(org_id, trans_id, O_Error_Num, O_Error_Code, O_Error_Message);
9288 
9289         if O_Error_Num <> 0
9290         then
9291           raise errexit;
9292         end if;
9293         /* End Fix for bug#3406184 */
9294     -- }
9295     end if;
9296 
9297     /* For wip_scrap and periodic cost update, we don't update actual cost. */
9298     if  (txn_act_id <> 30 AND txn_act_id <> 50 AND txn_act_id <> 51
9299     AND  txn_act_id <> 52 AND txn_act_id <> 40 AND txn_act_id <> 41
9300 	  AND  txn_act_id <> 42 AND txn_act_id <> 43
9301     AND  NOT (txn_act_id = 24 AND src_type_id = 14))
9302     then
9303     -- {
9304 	l_stmt_num := 950;
9305 
9306         /* Change for AX team
9307         Begin
9308         */
9309         -- fnd_file.put_line(fnd_file.log, '[AX]FINAL');
9310         -- fnd_file.put_line(fnd_file.log, '===================================================');
9311 
9312         uom_conv_rate := 1;
9313 
9314         l_stmt_num := 951;
9315         if act_cost = 0 then
9316           act_cost := item_cost;
9317 
9318         end if;
9319 
9320         -- fnd_file.put_line(fnd_file.log, '[AX]item_id = %ld', (long)item_id);
9321         -- fnd_file.put_line(fnd_file.log, '[AX]send_uom = %s', send_uom.arr);
9322         -- fnd_file.put_line(fnd_file.log, '[AX]rec_uom = %s', rec_uom.arr);
9323         -- fnd_file.put_line(fnd_file.log, '[AX]Actual Cost = %6.2f', (float)act_cost);
9324         -- fnd_file.put_line(fnd_file.log, '===================================================');
9325 
9326         if (txn_act_id = 3 OR txn_act_id = 21 )
9327         then
9328         -- {
9329 
9330             l_stmt_num := 952;
9331             -- fnd_file.put_line(fnd_file.log, '---------------------------------------------------');
9332             -- fnd_file.put_line(fnd_file.log, '[AX]Transafer Cost = %6.2f', (float)txf_cost);
9333             -- fnd_file.put_line(fnd_file.log, '[AX]Transport Cost = %6.2f', (float)trp_cost);
9334             -- fnd_file.put_line(fnd_file.log, '[AX]Primary Qty = %6.2f', (float)p_qty);
9335 
9336             -- fnd_file.put_line(fnd_file.log, '---------------------------------------------------');
9337             -- fnd_file.put_line(fnd_file.log, '[AX]Curr conv rate = %6.2f', (float)conv_rate);
9338             -- fnd_file.put_line(fnd_file.log, '[AX in f]UOM conv rate = %6.2f', (float)uom_conv_rate);
9339 
9340         /* commented for bug 2865814 -- these messages raise signal 8 error.
9341             -- fnd_file.put_line(fnd_file.log, '---------------------------------------------------');
9342             -- fnd_file.put_line(fnd_file.log, '[AXcstavcpb-formula(sending record)]The Txn cost shld be = %6.2f',
9343                    (float)(((act_cost*abs(p_qty))+txf_cost+trp_cost)/abs(p_qty)));
9344             -- fnd_file.put_line(fnd_file.log, '[AXcstavcpb-formula(recving record)]The Rcv Txn cost shld be = %6.2f',
9345                    (float)(((((act_cost*abs(p_qty))+txf_cost+trp_cost)/abs(p_qty))*conv_rate)/(uom_conv_rate)));
9346         */
9347         -- }
9348         else
9349         -- {
9350           if (txn_act_id = 12 )
9351           then
9352           -- {
9353             l_stmt_num := 953;
9354 
9355             -- fnd_file.put_line(fnd_file.log, '[AX]Sendg Transaction Cost = %6.2f', (float)txn_cost);
9356             -- fnd_file.put_line(fnd_file.log, '[AX*]Rcvg Transaction Cost = %6.2f', (float)txn_cost*conv_rate);
9357             -- fnd_file.put_line(fnd_file.log, '[AX]Curr conv rate = %6.2f', (float)conv_rate);
9358             -- fnd_file.put_line(fnd_file.log, '[AX]UOM conv rate = %6.2f', (float)uom_conv_rate);
9359             -- }
9360           end if;
9361         -- }
9362         end if;
9363 
9364         l_stmt_num := 954;
9365         UPDATE mtl_material_transactions
9366            SET transaction_cost = (((txf_price*abs(p_qty))+nvl(trp_cost,0))/abs(p_qty))
9367          WHERE transaction_id = trans_id;
9368 
9369 	l_stmt_num := 957;
9370         UPDATE mtl_material_transactions
9371         SET costed_flag = NULL,
9372             transaction_group_id = NULL,
9373             request_id = req_id,
9374             program_application_id = prg_appid,
9375             program_id = prg_id,
9376             program_update_date = sysdate,
9377             actual_cost = decode(zero_cost_flag, 1, 0, nvl(item_cost,0))
9378         WHERE transaction_id = trans_id;
9379 
9380         /* Create Events in SLA */
9381         l_trx_info.TRANSACTION_ID       := trans_id;
9382         l_trx_info.TXN_ACTION_ID        := txn_act_id;
9383         l_trx_info.TXN_ORGANIZATION_ID  := org_id;
9384         l_trx_info.TXN_SRC_TYPE_ID      := src_type_id;
9385         l_trx_info.TXFR_ORGANIZATION_ID := txf_org_id;
9386         l_trx_info.FOB_POINT            := fob_pt;
9387         l_trx_info.TRANSACTION_DATE     := txn_date;
9388         l_trx_info.PRIMARY_QUANTITY     := p_qty;
9389 
9390         IF tprice_option <> 0 THEN
9391           l_trx_info.TP := 'Y';
9392         END IF;
9393 
9394         /* umoogala: For process/discrete xfers attribute should always
9395          * be 'SAME'. Process Org transactions are never picked-up in
9396          * cost processor.
9397         IF txn_act_id = 3 THEN
9398           IF p_qty < 0 THEN
9399             l_trx_info.attribute := 'SAME';
9400           ELSE
9401             l_trx_info.attribute := 'TRANSFER';
9402           END IF;
9403         END IF;
9404         */
9405         l_trx_info.attribute := 'SAME';
9406 
9407         l_stmt_num := 960;
9408 
9409         CST_XLA_PVT.Create_INVXLAEvent (
9410               p_api_version       => 1.0,
9411               p_init_msg_list     => FND_API.G_FALSE,
9412               p_commit            => FND_API.G_FALSE,
9413               p_validation_level  => FND_API.G_VALID_LEVEL_FULL,
9414               x_return_status     => l_return_status,
9415               x_msg_count         => l_msg_count,
9416               x_msg_data          => l_msg_data,
9417               p_trx_info          => l_trx_info
9418             );
9419         IF l_return_status <> 'S' THEN
9420           o_error_num     := -1;
9421           o_error_code    := 'Error raising SLA Event for transaction: '||to_char(trans_id);
9422           o_error_message := 'CSTPLCIN.COST_INV_TXN:('||l_stmt_num||'): '||o_error_code;
9423           RAISE FND_API.g_exc_unexpected_error;
9424         END IF;
9425 
9426 
9427     -- }
9428     end if;
9429 
9430 
9431     COMMIT;
9432 
9433     fnd_file.put_line(fnd_file.log, 'cost_process_discrete_trx >>>');
9434 
9435 EXCEPTION
9436  when moh_rules_error then
9437       rollback;
9438 
9439       o_error_num := 9999;
9440       o_error_code := 'CST_RULES_ERROR';
9441       FND_MESSAGE.set_name('BOM', 'CST_RULES_ERROR');
9442       o_error_message := FND_MESSAGE.Get;
9443 
9444   when errexit
9445   then
9446     fnd_file.put_line(fnd_file.log,'in errexit exception');
9447     ROLLBACK;
9448 
9449     O_Error_Num     := NVL(O_Error_Num, -1);
9450     O_Error_Message := 'CSTPACDP.cost_process_discrete_trx:' || '(' || l_stmt_num || '):' ||
9451                          substr(O_Error_Message, 1, 180);
9452 
9453 
9454     UPDATE mtl_material_transactions
9455     SET    costed_flag = 'E',
9456            error_code = substrb(O_Error_Code,1,240),
9457            error_explanation = substrb(O_Error_Message,1,240),
9458            request_id = req_id,
9459            program_application_id = prg_appid,
9460            program_id = prg_id,
9461            program_update_date = sysdate
9462     WHERE  transaction_id = trans_id;
9463 
9464     l_stmt_num := 1020;
9465     COMMIT;
9466 
9467   WHEN FND_API.g_exc_unexpected_error THEN
9468     rollback;
9469 
9470     UPDATE mtl_material_transactions
9471     SET    costed_flag = 'E',
9472            error_code = substrb(O_Error_Code,1,240),
9473            error_explanation = substrb(O_Error_Message,1,240),
9474            request_id = req_id,
9475            program_application_id = prg_appid,
9476            program_id = prg_id,
9477            program_update_date = sysdate
9478     WHERE  transaction_id = trans_id;
9479     commit;
9480 
9481   when others
9482   then
9483     fnd_file.put_line(fnd_file.log,'other exception raised');
9484     rollback;
9485 
9486     O_error_num  := SQLCODE;
9487     O_error_code := SQLCODE;
9488     O_error_message := 'CSTPACDP.cost_process_discrete_trx:' || '(' || to_char(l_stmt_num) || '): '|| substr(SQLERRM,1,180);
9489 
9490     UPDATE mtl_material_transactions
9491     SET    costed_flag = 'E',
9492            error_code = substrb(O_Error_Code,1,240),
9493            error_explanation = substrb(O_Error_Message,1,240),
9494            request_id = req_id,
9495            program_application_id = prg_appid,
9496            program_id = prg_id,
9497            program_update_date = sysdate
9498     WHERE  transaction_id = trans_id;
9499 
9500     COMMIT;
9501 
9502 end cost_process_discrete_trx;
9503 
9504 
9505 
9506 --BUG#6732955
9507 PROCEDURE balance_account_txn_type(
9508   i_org_id              IN          NUMBER,
9509   i_txn_id              IN          NUMBER,
9510   i_txn_type_id         IN          NUMBER,
9511   O_Error_Num           OUT NOCOPY  NUMBER,
9512   O_Error_Code          OUT NOCOPY  VARCHAR2,
9513   O_Error_Message       OUT NOCOPY  VARCHAR2
9514 )IS
9515   l_stmt_num    NUMBER;
9516   l_rowid       ROWID;
9517   l_base_value  NUMBER;
9518   l_value       NUMBER;
9519   l_mta_rec     mtl_transaction_accounts%ROWTYPE;
9520   l_debug       VARCHAR2(1);
9521 BEGIN
9522 
9523   IF G_DEBUG = 'Y' THEN
9524     fnd_file.put_line(fnd_file.log,'balance_account_txn_type +');
9525     fnd_file.put_line(fnd_file.log,'  i_txn_type_id:'||i_txn_type_id);
9526     fnd_file.put_line(fnd_file.log,'  i_txn_id     :'||i_txn_id);
9527     fnd_file.put_line(fnd_file.log,'  i_org_id     :'||i_org_id);
9528   END IF;
9529 
9530   IF (i_txn_type_id <> 16) OR (i_txn_type_id IS NULL) THEN
9531 
9532     IF G_DEBUG = 'Y' THEN
9533       fnd_file.put_line(fnd_file.log,'Calling balance_account');
9534     END IF;
9535 
9536     balance_account(
9537       I_ORG_ID              => i_org_id,
9538       I_TXN_ID              => i_txn_id,
9539       O_Error_Num           => O_Error_Num,
9540       O_Error_Code          => O_Error_Code,
9541       O_Error_Message       => O_Error_Message);
9542 
9543   ELSE
9544 
9545     IF G_DEBUG = 'Y' THEN
9546       fnd_file.put_line(fnd_file.log,'  Case Logical RMA');
9547     END IF;
9548 
9549 
9550     l_stmt_num := 10;
9551 
9552     SELECT nvl(sum(a.base_transaction_value),0)
9553           ,nvl(sum(a.transaction_value),0)
9554           ,MAX(DECODE(a.accounting_line_type,1,
9555                                 a.TRANSACTION_ID))
9556           ,MAX(NVL(b.cost_of_sales_account,-1))   --adj_acct MP COGS
9557           ,MAX(DECODE(a.accounting_line_type,1,
9558                                 a.LAST_UPDATE_DATE))
9559           ,MAX(DECODE(a.accounting_line_type,1,
9560                                 a.LAST_UPDATED_BY))
9561           ,MAX(DECODE(a.accounting_line_type,1,
9562                                 a.CREATION_DATE))
9563           ,MAX(DECODE(a.accounting_line_type,1,
9564                                 a.CREATED_BY))
9565           ,MAX(DECODE(a.accounting_line_type,1,
9566                                 a.LAST_UPDATE_LOGIN))
9567           ,MAX(DECODE(a.accounting_line_type,1,
9568                                 a.INVENTORY_ITEM_ID))
9569           ,MAX(DECODE(a.accounting_line_type,1,
9570                                 a.ORGANIZATION_ID))
9571           ,MAX(DECODE(a.accounting_line_type,1,
9572                                 a.TRANSACTION_DATE))
9573           ,MAX(DECODE(a.accounting_line_type,1,
9574                                 a.TRANSACTION_SOURCE_ID))
9575           ,MAX(DECODE(a.accounting_line_type,1,
9576                                 a.TRANSACTION_SOURCE_TYPE_ID))
9577           ,MAX(DECODE(a.accounting_line_type,1,
9578                                 a.TRANSACTION_VALUE))
9579           ,MAX(DECODE(a.accounting_line_type,1,
9580                                 a.PRIMARY_QUANTITY))
9581           ,MAX(DECODE(a.accounting_line_type,1,
9582                                 a.GL_BATCH_ID))
9583           ,MAX(DECODE(a.accounting_line_type,1,
9584                                 a.ACCOUNTING_LINE_TYPE))
9585           ,MAX(DECODE(a.accounting_line_type,1,
9586                                 a.BASE_TRANSACTION_VALUE))
9587           ,MAX(DECODE(a.accounting_line_type,1,
9588                                 a.CONTRA_SET_ID))
9589           ,MAX(DECODE(a.accounting_line_type,1,
9590                                 a.RATE_OR_AMOUNT))
9591           ,MAX(DECODE(a.accounting_line_type,1,
9592                                 a.BASIS_TYPE))
9593           ,MAX(DECODE(a.accounting_line_type,1,
9594                                 a.RESOURCE_ID))
9595           ,MAX(DECODE(a.accounting_line_type,1,
9596                                 a.COST_ELEMENT_ID))
9597           ,MAX(DECODE(a.accounting_line_type,1,
9598                                 a.ACTIVITY_ID))
9599           ,MAX(DECODE(a.accounting_line_type,1,
9600                                 a.CURRENCY_CODE))
9601           ,MAX(DECODE(a.accounting_line_type,1,
9602                                 a.CURRENCY_CONVERSION_DATE))
9603           ,MAX(DECODE(a.accounting_line_type,1,
9604                                 a.CURRENCY_CONVERSION_TYPE))
9605           ,MAX(DECODE(a.accounting_line_type,1,
9606                                 a.CURRENCY_CONVERSION_RATE))
9607           ,MAX(DECODE(a.accounting_line_type,1,
9608                                 a.REQUEST_ID))
9609           ,MAX(DECODE(a.accounting_line_type,1,
9610                                 a.PROGRAM_APPLICATION_ID))
9611           ,MAX(DECODE(a.accounting_line_type,1,
9612                                 a.PROGRAM_ID))
9613           ,MAX(DECODE(a.accounting_line_type,1,
9614                                 a.PROGRAM_UPDATE_DATE))
9615           ,MAX(DECODE(a.accounting_line_type,1,
9616                                 a.ENCUMBRANCE_TYPE_ID))
9617           ,MAX(DECODE(a.accounting_line_type,1,
9618                                 a.REPETITIVE_SCHEDULE_ID))
9619           ,MAX(DECODE(a.accounting_line_type,1,
9620                                 a.USSGL_TRANSACTION_CODE))
9621       INTO l_base_value
9622           ,l_value
9623           ,l_mta_rec.TRANSACTION_ID
9624           ,l_mta_rec.REFERENCE_ACCOUNT
9625           ,l_mta_rec.LAST_UPDATE_DATE
9626           ,l_mta_rec.LAST_UPDATED_BY
9627           ,l_mta_rec.CREATION_DATE
9628           ,l_mta_rec.CREATED_BY
9629           ,l_mta_rec.LAST_UPDATE_LOGIN
9630           ,l_mta_rec.INVENTORY_ITEM_ID
9631           ,l_mta_rec.ORGANIZATION_ID
9632           ,l_mta_rec.TRANSACTION_DATE
9633           ,l_mta_rec.TRANSACTION_SOURCE_ID
9634           ,l_mta_rec.TRANSACTION_SOURCE_TYPE_ID
9635           ,l_mta_rec.TRANSACTION_VALUE
9636           ,l_mta_rec.PRIMARY_QUANTITY
9637           ,l_mta_rec.GL_BATCH_ID
9638           ,l_mta_rec.ACCOUNTING_LINE_TYPE
9639           ,l_mta_rec.BASE_TRANSACTION_VALUE
9640           ,l_mta_rec.CONTRA_SET_ID
9641           ,l_mta_rec.RATE_OR_AMOUNT
9642           ,l_mta_rec.BASIS_TYPE
9643           ,l_mta_rec.RESOURCE_ID
9644           ,l_mta_rec.COST_ELEMENT_ID
9645           ,l_mta_rec.ACTIVITY_ID
9646           ,l_mta_rec.CURRENCY_CODE
9647           ,l_mta_rec.CURRENCY_CONVERSION_DATE
9648           ,l_mta_rec.CURRENCY_CONVERSION_TYPE
9649           ,l_mta_rec.CURRENCY_CONVERSION_RATE
9650           ,l_mta_rec.REQUEST_ID
9651           ,l_mta_rec.PROGRAM_APPLICATION_ID
9652           ,l_mta_rec.PROGRAM_ID
9653           ,l_mta_rec.PROGRAM_UPDATE_DATE
9654           ,l_mta_rec.ENCUMBRANCE_TYPE_ID
9655           ,l_mta_rec.REPETITIVE_SCHEDULE_ID
9656           ,l_mta_rec.USSGL_TRANSACTION_CODE
9657       FROM mtl_transaction_accounts  a,
9658            mtl_parameters            b
9659      WHERE a.transaction_id  = i_txn_id
9660        AND a.organization_id = i_org_id
9661        AND b.organization_id = a.organization_id;
9662 
9663 
9664     IF (l_base_value = 0 and l_value = 0) THEN
9665        RETURN;
9666     END IF;
9667 
9668     IF G_DEBUG = 'Y' THEN
9669       fnd_file.put_line(fnd_file.log, 'Sum of base        amount: '||l_base_value);
9670       fnd_file.put_line(fnd_file.log, 'Sum of transaction amount: '||l_value);
9671     END IF;
9672 
9673     l_stmt_num := 20;
9674 
9675     IF G_DEBUG = 'Y' THEN
9676       fnd_file.put_line(fnd_file.log, 'Creating the difference in adjustment account');
9677     END IF;
9678 
9679 
9680     l_mta_rec.accounting_line_type   := 37;
9681     l_mta_rec.base_transaction_value := -1 * l_base_value;
9682     l_mta_rec.transaction_value      := -1 * l_value;
9683 
9684 
9685 
9686     IF l_base_value <> 0 AND l_mta_rec.primary_quantity <> 0 THEN
9687       l_mta_rec.rate_or_amount       := l_mta_rec.base_transaction_value
9688                                        /l_mta_rec.primary_quantity;
9689     END IF;
9690 
9691     l_mta_rec.PRIMARY_QUANTITY       := -1 * l_mta_rec.PRIMARY_QUANTITY;
9692 
9693 
9694     INSERT INTO mtl_transaction_accounts
9695     ( TRANSACTION_ID
9696      ,REFERENCE_ACCOUNT
9697      ,LAST_UPDATE_DATE
9698      ,LAST_UPDATED_BY
9699      ,CREATION_DATE
9700      --
9701      ,CREATED_BY
9702      ,LAST_UPDATE_LOGIN
9703      ,INVENTORY_ITEM_ID
9704      ,ORGANIZATION_ID
9705      ,TRANSACTION_DATE
9706      --
9707      ,TRANSACTION_SOURCE_ID
9708      ,TRANSACTION_SOURCE_TYPE_ID
9709      ,TRANSACTION_VALUE
9710      ,PRIMARY_QUANTITY
9711      ,GL_BATCH_ID
9712      --
9713      ,ACCOUNTING_LINE_TYPE
9714      ,BASE_TRANSACTION_VALUE
9715      ,CONTRA_SET_ID
9716      ,RATE_OR_AMOUNT
9717      ,BASIS_TYPE
9718      --
9719      ,RESOURCE_ID
9720      ,COST_ELEMENT_ID
9721      ,ACTIVITY_ID
9722      ,CURRENCY_CODE
9723      ,CURRENCY_CONVERSION_DATE
9724      --
9725      ,CURRENCY_CONVERSION_TYPE
9726      ,CURRENCY_CONVERSION_RATE
9727      ,REQUEST_ID
9728      ,PROGRAM_APPLICATION_ID
9729      ,PROGRAM_ID
9730      --
9731      ,PROGRAM_UPDATE_DATE
9732      ,ENCUMBRANCE_TYPE_ID
9733      ,REPETITIVE_SCHEDULE_ID
9734      ,GL_SL_LINK_ID
9735      ,USSGL_TRANSACTION_CODE
9736      --
9737      ,INV_SUB_LEDGER_ID              )
9738      SELECT
9739             l_mta_rec.TRANSACTION_ID
9740            ,l_mta_rec.REFERENCE_ACCOUNT
9741            ,l_mta_rec.LAST_UPDATE_DATE
9742            ,l_mta_rec.LAST_UPDATED_BY
9743            ,l_mta_rec.CREATION_DATE
9744            --
9745            ,l_mta_rec.CREATED_BY
9746            ,l_mta_rec.LAST_UPDATE_LOGIN
9747            ,l_mta_rec.INVENTORY_ITEM_ID
9748            ,l_mta_rec.ORGANIZATION_ID
9749            ,l_mta_rec.TRANSACTION_DATE
9750            --
9751            ,l_mta_rec.TRANSACTION_SOURCE_ID
9752            ,l_mta_rec.TRANSACTION_SOURCE_TYPE_ID
9753            ,l_mta_rec.TRANSACTION_VALUE
9754            ,l_mta_rec.PRIMARY_QUANTITY
9755            ,l_mta_rec.GL_BATCH_ID
9756            --
9757            ,l_mta_rec.ACCOUNTING_LINE_TYPE
9758            ,l_mta_rec.BASE_TRANSACTION_VALUE
9759            ,l_mta_rec.CONTRA_SET_ID
9760            ,l_mta_rec.RATE_OR_AMOUNT
9761            ,l_mta_rec.BASIS_TYPE
9762            --
9763            ,l_mta_rec.RESOURCE_ID
9764            ,l_mta_rec.COST_ELEMENT_ID
9765            ,l_mta_rec.ACTIVITY_ID
9766            ,l_mta_rec.CURRENCY_CODE
9767            ,l_mta_rec.CURRENCY_CONVERSION_DATE
9768            --
9769            ,l_mta_rec.CURRENCY_CONVERSION_TYPE
9770            ,l_mta_rec.CURRENCY_CONVERSION_RATE
9771            ,l_mta_rec.REQUEST_ID
9772            ,l_mta_rec.PROGRAM_APPLICATION_ID
9773            ,l_mta_rec.PROGRAM_ID
9774            --
9775            ,l_mta_rec.PROGRAM_UPDATE_DATE
9776            ,l_mta_rec.ENCUMBRANCE_TYPE_ID
9777            ,l_mta_rec.REPETITIVE_SCHEDULE_ID
9778            ,l_mta_rec.GL_SL_LINK_ID
9779            ,l_mta_rec.USSGL_TRANSACTION_CODE
9780             --
9781            ,CST_INV_SUB_LEDGER_ID_S.NEXTVAL
9782      FROM DUAL;
9783 
9784   END IF;
9785   IF G_DEBUG = 'Y' THEN
9786     fnd_file.put_line(fnd_file.log,'balance_account_txn_type -');
9787   END IF;
9788 EXCEPTION
9789 
9790  WHEN OTHERS then
9791 
9792    ROLLBACK;
9793    O_error_num := SQLCODE;
9794    O_error_message := 'CSTPACDP.balance_account_txn_type (' || to_char(l_stmt_num) ||
9795                      ') ' || substr(SQLERRM,1,180);
9796 
9797 END balance_account_txn_type;
9798 
9799 
9800 
9801 
9802 END CSTPACDP;
9803