DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSTPACDP

Source


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