DBA Data[Home] [Help]

PACKAGE BODY: APPS.CST_TPRICE_PVT

Source


1 PACKAGE BODY CST_TPRICE_PVT AS
2 /* $Header: CSTVTPAB.pls 120.5.12010000.2 2010/02/26 08:16:25 lchevala ship $ */
3 
4 G_PKG_NAME CONSTANT VARCHAR2(30) := 'CST_TPRICE_PVT';
5 G_DEBUG    CONSTANT VARCHAR(1) := NVL(FND_PROFILE.value('MRP_DEBUG'), 'N');
6 
7 procedure Adjust_Acct(
8   P_API_VERSION         IN      NUMBER,
9   P_INIT_MSG_LIST       IN      VARCHAR2,
10   P_COMMIT              IN      VARCHAR2,
11   P_VALIDATION_LEVEL    IN      NUMBER,
12   P_TPRICE_OPTION       IN      NUMBER,
13   P_TXF_PRICE           IN      NUMBER,
14   P_TXN_ID              IN      NUMBER,
15   P_COST_GRP_ID         IN      NUMBER,
16   P_TXF_COST_GRP        IN      NUMBER,
17   P_ITEM_ID             IN      NUMBER,
18   P_TXN_DATE            IN      DATE,
19   P_QTY                 IN      NUMBER,
20   P_SUBINV              IN      VARCHAR2,
21   P_TXF_SUBINV          IN      VARCHAR2,
22   P_TXN_ORG_ID          IN      NUMBER,
23   P_TXF_ORG_ID          IN      NUMBER,
24   P_TXF_TXN_ID          IN      NUMBER,
25   P_TXF_COST            IN      NUMBER,
26   P_TXN_ACT_ID          IN      NUMBER,
27   P_TXN_SRC_ID          IN      NUMBER,
28   P_SRC_TYPE_ID         IN      NUMBER,
29   P_FOB_POINT           IN      NUMBER,
30   P_USER_ID             IN      NUMBER,
31   P_LOGIN_ID            IN      NUMBER,
32   P_REQ_ID              IN      NUMBER,
33   P_PRG_APPL_ID         IN      NUMBER,
34   P_PRG_ID              IN      NUMBER,
35   X_RETURN_STATUS       OUT NOCOPY VARCHAR2,
36   X_MSG_COUNT           OUT NOCOPY NUMBER,
37   X_MSG_DATA            OUT NOCOPY VARCHAR2,
38   X_ERROR_NUM           OUT NOCOPY NUMBER,
39   X_ERROR_CODE          OUT NOCOPY VARCHAR2,
40   X_ERROR_MESSAGE       OUT NOCOPY VARCHAR2
41 ) IS
42   l_api_name    CONSTANT VARCHAR2(30) := 'Adjust_Acct';
43   l_api_version CONSTANT NUMBER       := 1.0;
44 
45   l_from_org    NUMBER;
46   l_to_org      NUMBER;
47   l_std_from_org NUMBER;
48   l_std_to_org  NUMBER;
49   l_from_ou     NUMBER;
50   l_to_ou       NUMBER;
51   l_from_cg     NUMBER;
52   l_to_cg       NUMBER;
53   l_from_layer  NUMBER;
54   l_to_layer    NUMBER;
55   l_from_subinv MTL_SECONDARY_INVENTORIES.SECONDARY_INVENTORY_NAME%TYPE;
56   l_to_subinv   MTL_SECONDARY_INVENTORIES.SECONDARY_INVENTORY_NAME%TYPE;
57   l_snd_uom     MTL_UNITS_OF_MEASURE.UOM_CODE%TYPE;
58   l_rcv_uom     MTL_UNITS_OF_MEASURE.UOM_CODE%TYPE;
59   l_snd_qty     NUMBER;
60   l_rcv_qty     NUMBER;
61   l_snd_sob_id  NUMBER;
62   l_snd_curr    GL_SETS_OF_BOOKS.CURRENCY_CODE%TYPE;
63   l_rcv_sob_id  NUMBER;
64   l_rcv_coa_id  NUMBER;
65   l_rcv_curr    GL_SETS_OF_BOOKS.CURRENCY_CODE%TYPE;
66   l_curr_type   GL_DAILY_CONVERSION_TYPES.CONVERSION_TYPE%TYPE;
67   l_conv_rate   NUMBER;
68   l_conv_date   DATE;
69   l_acct        NUMBER;
70   l_om_ccid     NUMBER;
71   l_inv_ccid    NUMBER;
72   l_rcv_count   NUMBER;
73   l_pay_count   NUMBER;
74   l_rcv_sum     NUMBER;
75   l_pay_sum     NUMBER;
76   l_line_id     NUMBER;
77   l_header_id   NUMBER;
78   l_cust_id     NUMBER;
79   l_order_type  NUMBER;
80   l_ship_num    MTL_MATERIAL_TRANSACTIONS.SHIPMENT_NUMBER%TYPE;
81   l_req_line    NUMBER;
82   l_prf         NUMBER;
83   l_cost_element NUMBER;
84   l_elem_cost   NUMBER;
85   l_err_num     NUMBER;
86   l_err_code    MTL_MATERIAL_TRANSACTIONS.ERROR_CODE%TYPE;
87   l_err_msg     MTL_MATERIAL_TRANSACTIONS.ERROR_EXPLANATION%TYPE;
88   l_stmt_num    NUMBER;
89   l_concat_id   VARCHAR2(2000);
90   l_concat_seg  VARCHAR2(2000);
94   l_acct_done   NUMBER;
91   l_concat_desc VARCHAR2(2000);
92   l_msg_count   NUMBER;
93   l_msg_data    VARCHAR2(2000);
95   l_txf_txn_id  NUMBER;
96   l_from_exp_item  NUMBER;
97   process_error EXCEPTION;
98   prf_inv_acct_err EXCEPTION;
99   inv_exp_acct_err EXCEPTION;
100 
101   l_pd_txfr_ind  NUMBER; -- OPM INVCONV process/discrete xfer indicator
102   l_io_invoicing NUMBER := to_number(fnd_profile.value('INV_INTERCOMPANY_INVOICE_INTERNAL_ORDER'));
103 
104 BEGIN
105 
106   -- Standard start of API savepoint
107   SAVEPOINT Adjust_Acct_PVT;
108 
109   -- Standard call to check for call compatibility
110   l_stmt_num := 10;
111   if NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name, G_PKG_NAME) then
112      raise FND_API.g_exc_unexpected_error;
113   end if;
114 
115   -- Initialize message list if p_init_msg_list is set to TRUE
116   l_stmt_num := 20;
117   if FND_API.to_Boolean(p_init_msg_list) then
118      FND_MSG_PUB.initialize;
119   end if;
120 
121   -- Initialize API return status and local variables
122   l_stmt_num := 30;
123   x_return_status := FND_API.g_ret_sts_success;
124   l_err_num := 0;
125   l_err_code := '';
126   l_err_msg := '';
127 
128   /* OPM INVCONV sschinch check if this is a process discrete transfer */
129   SELECT MOD(SUM(DECODE(MP.process_enabled_flag,'Y',1,2)), 2)
130     INTO l_pd_txfr_ind
131     FROM mtl_parameters mp
132    WHERE mp.organization_id = p_txn_org_id
133       OR mp.organization_id = p_txf_org_id;
134 
135 
136   if (G_DEBUG = 'Y') then
137      FND_FILE.put_line(FND_FILE.LOG, 'In procedure CST_TPRICE_PVT.Adjust_Acct');
138      FND_FILE.put_line(FND_FILE.LOG, 'p_tprice_option = ' || p_tprice_option);
139      FND_FILE.put_line(FND_FILE.LOG, 'p_txf_price = ' || p_txf_price);
140      FND_FILE.put_line(FND_FILE.LOG, 'l_pd_txfr_ind = ' || l_pd_txfr_ind);
141      FND_FILE.put_line(FND_FILE.LOG, 'p_txn_act_id = ' || p_txn_act_id);
142      FND_FILE.put_line(FND_FILE.LOG, 'Internal Order invoicing = ' || l_io_invoicing);
143   end if;
144 
145   if (p_txn_act_id in (21, 22)) then
146      l_from_org := p_txn_org_id;
147      l_to_org := p_txf_org_id;
148      l_from_cg := p_cost_grp_id;
149      l_to_cg := p_txf_cost_grp;
150      l_from_subinv := p_subinv;
151      l_to_subinv := p_txf_subinv;
152   elsif (p_txn_act_id in (12, 15)) then
153      l_from_org := p_txf_org_id;
154      l_to_org := p_txn_org_id;
155      l_from_cg := p_txf_cost_grp;
156      l_to_cg := p_cost_grp_id;
157      l_from_subinv := p_txf_subinv;
158      l_to_subinv := p_subinv;
159   end if;
160 
161   l_stmt_num := 40;
162 
163   if (l_pd_txfr_ind = 0) OR
164      (l_pd_txfr_ind = 1 AND (p_txn_act_id = 21 or p_txn_act_id = 22))
165   then
166       SELECT count(*), sum(base_transaction_value)
167       INTO l_rcv_count, l_rcv_sum
168       FROM mtl_transaction_accounts
169       WHERE transaction_id = p_txn_id
170       AND organization_id = l_from_org
171       AND accounting_line_type = 10;
172   end if;
173 
174   l_stmt_num := 50;
175 
176   if (l_pd_txfr_ind = 0) OR
177      (l_pd_txfr_ind = 1 AND (p_txn_act_id = 15 or p_txn_act_id = 12))
178   then
179       SELECT count(*), sum(base_transaction_value)
180       INTO l_pay_count, l_pay_sum
181       FROM mtl_transaction_accounts
182       WHERE transaction_id = p_txn_id
183       AND organization_id = l_to_org
184       AND accounting_line_type = 9;
185   end if;
186 
187   if (G_DEBUG = 'Y') then
188      FND_FILE.put_line(FND_FILE.LOG, 'l_rcv_sum = ' || l_rcv_sum);
189      FND_FILE.put_line(FND_FILE.LOG, 'l_pay_sum = ' || l_pay_sum);
190   end if;
191 
192 
193   /* OPM INVCONV: process/discrete xfers
194    * Moved the following stmt here from inside the IF block
195    */
196   l_stmt_num := 70;
197 
198   SELECT to_number(org_information3)
199   INTO   l_from_ou
200   FROM   hr_organization_information
201   WHERE  org_information_context = 'Accounting Information'
202   AND    organization_id = l_from_org;
203 
204   l_stmt_num := 75;
205   SELECT to_number(org_information3)
206   INTO   l_to_ou
207   FROM   hr_organization_information
208   WHERE  org_information_context = 'Accounting Information'
209   AND    organization_id = l_to_org;
210 
211 
212   if (G_DEBUG = 'Y') then
213     FND_FILE.put_line(FND_FILE.LOG,
214 	'p_src_type_id/l_from_ou/l_to_ou/l_io_invoicing/p_txn_act_id/l_rcv_count: ' ||
215 	p_src_type_id ||'/'|| l_from_ou ||'/'|| l_to_ou ||'/'|| l_io_invoicing ||'/'||
216 	p_txn_act_id ||'/'|| l_rcv_count );
217   end if;
218 
219   -- The adjustment should be done after the accounting for both sending and
220   -- receiving orgs is done, i.e., the interorg receivable and payable exist
221   if ((l_pd_txfr_ind = 0 and l_rcv_count >= 1 and l_pay_count >= 1 and p_tprice_option <> 0)
222       OR
223       ((l_pd_txfr_ind  =  1) and
224        (p_src_type_id  in (7, 8)) and
225        (l_from_ou      <> l_to_ou) and
226        (l_io_invoicing =  1) and
227        ((p_txn_act_id  in (21, 22) and l_rcv_count >= 1) OR
228         (p_txn_act_id  in (12, 15) and l_pay_count >= 1)))
229      )
230   then
231 
232      l_stmt_num := 60;
233 
234      l_std_from_org := CSTPAVCP.standard_cost_org(l_from_org);
235      l_std_to_org := CSTPAVCP.standard_cost_org(l_to_org);
236 
237      -- Get the currency information for sending and receiving orgs
238      l_stmt_num := 80;
239 
240      CSTPAVCP.get_snd_rcv_rate(p_txn_id, l_from_org, l_to_org,
241                 l_snd_sob_id, l_snd_curr, l_rcv_sob_id, l_rcv_curr,
242                 l_curr_type, l_conv_rate, l_conv_date,
246         raise process_error;
243                 l_err_num, l_err_code, l_err_msg);
244 
245      if (l_err_num<>0) then
247      end if;
248 
249      if (l_conv_date is null) then
250         l_conv_date := p_txn_date;
251      end if;
252 
253      l_stmt_num := 90;
254 
255      SELECT chart_of_accounts_id
256      INTO l_rcv_coa_id
257      FROM gl_sets_of_books
258      WHERE set_of_books_id = l_rcv_sob_id;
259 
260      if (G_DEBUG = 'Y') then
261         FND_FILE.put_line(FND_FILE.LOG, 'l_snd_curr = ' || l_snd_curr);
262         FND_FILE.put_line(FND_FILE.LOG, 'l_rcv_curr = ' || l_rcv_curr);
263         FND_FILE.put_line(FND_FILE.LOG, 'l_conv_rate = ' || l_conv_rate);
264         FND_FILE.put_line(FND_FILE.LOG, 'l_rcv_coa_id = ' || l_rcv_coa_id);
265      end if;
266 
267      -- Get the primary unit of measure for sending and receiving orgs
268      l_stmt_num := 100;
269 
270      CSTPAVCP.get_snd_rcv_uom(p_item_id, l_from_org, l_to_org, l_snd_uom, l_rcv_uom,
271                   l_err_num, l_err_code, l_err_msg);
272 
273      -- Get the sending and receiving primary quantity
274      l_stmt_num := 110;
275 
276      if (l_from_org = p_txn_org_id) then
277         l_snd_qty := -1 * abs(p_qty); -- doing this since for action_id = 22, qty is +ve in MMT
278         l_rcv_qty := inv_convert.inv_um_convert
279                                 (p_item_id, NULL, -1 * l_snd_qty,
280                                  l_snd_uom, l_rcv_uom, NULL, NULL);
281      else
282         l_rcv_qty := p_qty;
283         l_snd_qty := inv_convert.inv_um_convert
284                                 (p_item_id, NULL, -1 * l_rcv_qty,
285                                  l_rcv_uom, l_snd_uom, NULL, NULL);
286      end if;
287 
288      if (l_err_num <> 0) then
289         raise process_error;
290      end if;
291 
292      if (G_DEBUG = 'Y') then
293         FND_FILE.put_line(FND_FILE.LOG, 'l_snd_uom = ' || l_snd_uom);
294         FND_FILE.put_line(FND_FILE.LOG, 'l_rcv_uom = ' || l_rcv_uom);
295         FND_FILE.put_line(FND_FILE.LOG, 'l_snd_qty = ' || l_snd_qty);
296         FND_FILE.put_line(FND_FILE.LOG, 'l_rcv_qty = ' || l_rcv_qty);
297      end if;
298 
299      -- Get the internal order information from the shipment transaction
300      -- OPM INVCONV: added action id 15
301      --
302      if (p_txn_act_id in (15, 21)) then
303         l_stmt_num := 120;
304 
305         SELECT MMT.trx_source_line_id, OEH.header_id, OEH.sold_to_org_id, OEH.order_type_id
306         INTO l_line_id, l_header_id, l_cust_id, l_order_type
307         FROM mtl_material_transactions MMT, oe_order_headers_all OEH, oe_order_lines_all OEL
308         WHERE MMT.transaction_id = p_txn_id
309         AND   OEL.line_id = MMT.trx_source_line_id
310         AND   OEL.header_id = OEH.header_id;
311      --
312      -- OPM INVCONV: added action id 15
313      --
314      elsif (p_txn_act_id in (12, 22)) then
315         l_stmt_num := 130;
316 
317         SELECT MMT.shipment_number, RT.requisition_line_id,
318 	       -- Following change has been made since for action id 22, transfer_transaction_id
319 	       -- points to receiving txn, not the shipping txn, because it got created after
320 	       -- goods are received.
321 	       decode(l_pd_txfr_ind, 1, 0, nvl(MMT.transfer_transaction_id, 0))
322         INTO l_ship_num, l_req_line, l_txf_txn_id
323         FROM mtl_material_transactions MMT, rcv_transactions RT
324         WHERE MMT.transaction_id = p_txn_id
325         AND   RT.transaction_id = MMT.rcv_transaction_id;
326 
327         /* Bug 3482782: for R11i.10, we can find the sending txn based on the txf_txn_id;
328            for R11i.9, we add 'ROWNUM = 1' to handle the situation where one delivery for
329            an internal sales order line has multiple deliver details for the same item. */
330         if (l_txf_txn_id <> 0) then
331            l_stmt_num := 135;
332 
333            SELECT MMT.trx_source_line_id, OEH.header_id, OEH.sold_to_org_id, OEH.order_type_id
334            INTO l_line_id, l_header_id, l_cust_id, l_order_type
335            FROM mtl_material_transactions MMT, oe_order_headers_all OEH, oe_order_lines_all OEL
336            WHERE MMT.transaction_id = l_txf_txn_id
337            AND   MMT.trx_source_line_id = OEL.line_id
338            AND   OEL.header_id = OEH.header_id;
339         else
340            l_stmt_num := 140;
341 
342            SELECT MMT.trx_source_line_id, OEH.header_id, OEH.sold_to_org_id, OEH.order_type_id
343            INTO l_line_id, l_header_id, l_cust_id, l_order_type
344            FROM mtl_material_transactions MMT, oe_order_headers_all OEH, oe_order_lines_all OEL
345            WHERE MMT.transaction_action_id = 21
346            AND   MMT.transaction_source_type_id = 8
347            AND   MMT.organization_id = l_from_org
348            AND   MMT.inventory_item_id = p_item_id
349            AND   MMT.shipment_number = l_ship_num
350            AND   MMT.trx_source_line_id = OEL.line_id
351            AND   OEL.source_document_line_id = l_req_line
352            AND   OEL.header_id = OEH.header_id
353            AND   ROWNUM = 1;
354         end if;
355      end if;
356 
357      if (G_DEBUG = 'Y') then
358         FND_FILE.put_line(FND_FILE.LOG, 'l_line_id = ' || l_line_id);
359         FND_FILE.put_line(FND_FILE.LOG, 'l_header_id = ' || l_header_id);
360         FND_FILE.put_line(FND_FILE.LOG, 'l_cust_id = ' || l_cust_id);
361         FND_FILE.put_line(FND_FILE.LOG, 'l_order_type = ' || l_order_type);
362      end if;
363 
364      -- Get the COGS account for the sending org from OE workflow, if not succeeded,
365      -- default it to the item COGS
366      l_stmt_num := 150;
367 
368      /* Commenting out the set_org_context API and using an equivalent another as the former would
369         be obsoleted with R12 as part of MOAC Uptake */
370 
374       * Get COGS only for logical intransit shipment.
371      /*  FND_CLIENT_INFO.set_org_context(l_from_ou); */
372 
373      /* OPM INVCONV: Process/Discrete xfer
375       */
376      if (l_pd_txfr_ind = 0) or
377         (l_pd_txfr_ind = 1 and p_txn_act_id in (21, 22))
378      then
379        mo_global.set_policy_context('S',l_from_ou);
380 
381        if (OE_FLEX_COGS_PUB.start_process(1.0, l_line_id, l_om_ccid, l_concat_seg, l_concat_id,
382                             l_concat_desc, l_msg_count, l_msg_data) <> FND_API.g_ret_sts_success) then
383           l_stmt_num := 160;
384 
385           SELECT nvl(MSI.cost_of_sales_account, MP.cost_of_sales_account)
386           INTO l_om_ccid
387           FROM mtl_system_items MSI, mtl_parameters MP
388           WHERE MSI.organization_id = l_from_org
389           AND   MSI.inventory_item_id = p_item_id
390           AND   MP.organization_id = MSI.organization_id;
391        end if;
392 
393        if (G_DEBUG = 'Y') then
394           FND_FILE.put_line(FND_FILE.LOG, 'l_om_ccid = ' || l_om_ccid);
395        end if;
396 
397      end if;
398 
399      /* OPM INVCONV: Process/Discrete xfer
400       * Get IC Expense account only for logical intransit receipt.
401       */
402      if (l_pd_txfr_ind = 0) or
403         (l_pd_txfr_ind = 1 and p_txn_act_id in (12, 15))
404      then
405 
406        -- Get the intercompany expense account from INV workflow
407        l_stmt_num := 170;
408 
409        if NOT
410           INV_WORKFLOW.call_generate_cogs(l_rcv_coa_id, l_cust_id, p_item_id, l_header_id, l_line_id,
411                        l_order_type, l_to_ou, l_inv_ccid, l_concat_seg, l_msg_data, l_to_org)
412        then
413           raise inv_exp_acct_err;
414        end if;
415 
416        if (G_DEBUG = 'Y') then
417           FND_FILE.put_line(FND_FILE.LOG, 'l_inv_ccid = ' || l_inv_ccid);
418        end if;
419 
420      end if;
421 
422      /* PAC Enhancements for R12: Stamp the COGS and Intercompany accrual accounts in MMT */
423      l_stmt_num := 175;
424 
425      UPDATE mtl_material_transactions
426      SET distribution_account_id = l_om_ccid,
427          expense_account_id = l_inv_ccid
428      WHERE transaction_id = p_txn_id;
429 
430 
431      -- First adjustment: delete interorg receivable and payable.  They are not needed as the
432      -- intercompany receivable and payable will be generated from intercompany invoice processors
433      if (G_DEBUG = 'Y') then
434         FND_FILE.put_line(FND_FILE.LOG, 'Start first adjustment');
435      end if;
436 
437      l_stmt_num := 180;
438 
439      /*
440       * OPM INVCONV: Process/Discrete xfer
441       * -- Delete InterOrg Profit Account (LineType 34) also.
442       */
443 
444      DELETE FROM mtl_transaction_accounts
445      WHERE transaction_id = p_txn_id
446      AND accounting_line_type in (9,10,34);
447 
448      -- Second adjustment: debit elemental COGS for the sending org
449      if (G_DEBUG = 'Y') then
450         FND_FILE.put_line(FND_FILE.LOG, 'Start second adjustment');
451      end if;
452 
453      l_stmt_num := 185;
454 
455      SELECT decode(inventory_asset_flag, 'Y', 0, 1)
456      INTO l_from_exp_item
457      FROM mtl_system_items_b
458      WHERE organization_id = l_from_org
459      AND inventory_item_id = p_item_id;
460 
461      /* Bug 3551024: For an expense item in the sending org, no COGS accounting */
462      /* OPM INVCONV: Process/Discrete Xfer
463       * COGS only for Logical Intransit Shipment Txn
464       */
465      if ((l_from_exp_item <> 1) and
466          ((l_pd_txfr_ind = 0) or (l_pd_txfr_ind = 1 and p_txn_act_id in (21, 22)))
467 	)
468      then
469 
470      l_acct_done := 0;
471 
472      FOR l_cost_element IN 1..5 loop
473          l_elem_cost := NULL;
474 
475          -- If the sending org is standard costing, get costs from CIC, otherwise from MCACD
476          if (l_std_from_org <> 0) then
477             l_stmt_num := 190;
478 
479             /* Bug 3239084: use cst_item_cost_details from the costing org to support cost sharing */
480             SELECT sum(item_cost)
481             INTO l_elem_cost
482             FROM cst_item_cost_details
483             WHERE inventory_item_id = p_item_id
484             AND   cost_element_id = l_cost_element
485             AND   cost_type_id = 1
486             AND   organization_id =
487                   (select cost_organization_id
488                    from mtl_parameters
489                    where organization_id = l_from_org);
490          else
491             l_stmt_num := 200;
492 
493             SELECT layer_id
494             INTO l_from_layer
495             FROM cst_quantity_layers
496             WHERE inventory_item_id = p_item_id
497             AND organization_id = l_from_org
498             AND cost_group_id = l_from_cg;
499 
500             l_stmt_num := 210;
501 
502             SELECT sum(actual_cost)
503             INTO l_elem_cost
504             FROM mtl_cst_actual_cost_details
505             WHERE transaction_id = p_txn_id
506             AND organization_id = l_from_org
507             AND cost_element_id = l_cost_element
508             AND layer_id = l_from_layer;
509          end if;
510 
511          l_stmt_num := 220;
512 
513          if (l_elem_cost is not NULL) then
514             l_acct_done := 1;
515 
516             if (l_std_from_org <> 0) then    -- To see if there is COGS account from Hook
520             else
517                l_acct := CSTPSCHK.std_get_account_id(l_from_org, p_txn_id, -1*sign(l_snd_qty), 2,
518                                   l_cost_element, null, l_from_subinv, l_from_cg,
519                                   0, 1, l_err_num, l_err_code, l_err_msg);
521 	       /*BUG 8881927 ADDED 'l_from_cg' */
522                l_acct := CSTPACHK.get_account_id(l_from_org, p_txn_id, -1*sign(l_snd_qty), 2,
523                                   l_cost_element, null, l_from_subinv,
524                                   0, 1, l_err_num, l_err_code, l_err_msg,l_from_cg);
525             end if;
526 
527             if (l_err_num <> 0) then
528                raise process_error;
529             end if;
530 
531             if (l_acct = -1) then
532                l_acct := l_om_ccid;
533             end if;
534 
535             l_stmt_num := 230;
536 
537             CSTPACDP.insert_account(l_from_org, p_txn_id, p_item_id, -1*l_elem_cost*l_snd_qty,
538                            -1*l_snd_qty, l_acct, l_snd_sob_id, 2, l_cost_element, NULL, p_txn_date,
539                            p_txn_src_id, p_src_type_id, l_snd_curr, NULL, NULL,
540                            NULL, NULL, 1, p_user_id, p_login_id, p_req_id,
541                            p_prg_appl_id, p_prg_id, l_err_num, l_err_code, l_err_msg);
542 
543             if (l_err_num <> 0) then
544                raise process_error;
545             end if;
546          end if;
547      end loop;
548 
549      -- If no cost is defined, create zero debit entry for COGS
550      if (l_acct_done = 0) then
551         CSTPACDP.insert_account(l_from_org, p_txn_id, p_item_id, 0,
552                        -1*l_snd_qty, l_om_ccid, l_snd_sob_id, 2, 1, NULL, p_txn_date,
553                        p_txn_src_id, p_src_type_id, l_snd_curr, NULL, NULL,
554                        NULL, NULL, 1, p_user_id, p_login_id, p_req_id,
555                        p_prg_appl_id, p_prg_id, l_err_num, l_err_code, l_err_msg);
556      end if;
557 
558      end if; -- if (l_from_exp_item <> 1)
559 
560      -- Third adjustment: credit intercompany expense for the receiving org
561      if (G_DEBUG = 'Y') then
562         FND_FILE.put_line(FND_FILE.LOG, 'Start third adjustment');
563      end if;
564 
565      /* OPM INVCONV: Process/Discrete Xfer
566       * IC Expense only for Logical Intransit Receipt Txn
567       */
568      if ((l_pd_txfr_ind = 0) or (l_pd_txfr_ind = 1 and p_txn_act_id in (12, 15)))
569      then
570 
571        l_stmt_num := 240;
572 
573        if (l_std_to_org <> 0) then    -- To see if there is intercompany expense account from Hook
574           l_acct := CSTPSCHK.std_get_account_id(l_to_org, p_txn_id, -1*sign(p_txf_price), 2,
575                              null, null, l_to_subinv, l_to_cg,
576                              0, 2, l_err_num, l_err_code, l_err_msg);
577        else
578           /*BUG 8881927 ADDED 'l_to_cg' */
579           l_acct := CSTPACHK.get_account_id(l_to_org, p_txn_id, -1*sign(p_txf_price), 2,
580                              null, null, l_to_subinv,
581                              0, 2, l_err_num, l_err_code, l_err_msg,l_to_cg);
582        end if;
583 
584        if (l_err_num <> 0) then
585           raise process_error;
586        end if;
587 
588        if (l_acct = -1) then
589           l_acct := l_inv_ccid;
590        end if;
591 
592        l_stmt_num := 250;
593 
594        CSTPACDP.insert_account(l_to_org, p_txn_id, p_item_id, -1*p_txf_price*l_rcv_qty,
595                              -1*l_rcv_qty, l_acct, l_rcv_sob_id, 2, NULL, NULL, p_txn_date,
596                              p_txn_src_id, p_src_type_id, l_rcv_curr, l_snd_curr, l_conv_date,
597                              l_conv_rate, l_curr_type, 1, p_user_id, p_login_id, p_req_id,
598                              p_prg_appl_id, p_prg_id, l_err_num, l_err_code, l_err_msg);
599 
600        if (l_err_num <> 0) then
601           raise process_error;
602        end if;
603      end if;
604 
605      -- If the option is not to treat transfer price as incoming cost, need fourth adjustment:
606      -- debit/credit profit in inventory for the receiving org
607      if (p_tprice_option = 1) then
608         if (G_DEBUG = 'Y') then
609            FND_FILE.put_line(FND_FILE.LOG, 'Start fourth adjustment');
610         end if;
611 
612         l_stmt_num := 260;
613 
614         SELECT -1.0*nvl(sum(MTA.base_transaction_value),0)
615         INTO l_prf
616         FROM mtl_transaction_accounts MTA
617         WHERE organization_id = l_to_org
618         AND transaction_id = p_txn_id;
619 
620         if (G_DEBUG = 'Y') then
621            FND_FILE.put_line(FND_FILE.LOG, 'l_prf = ' || l_prf);
622         end if;
623 
624         if (l_prf <> 0) then
625            l_stmt_num := 270;
626 
627            if (l_std_to_org <> 0) then   -- To see if there is profit in inventory account from Hook
628               l_acct := CSTPSCHK.std_get_account_id(l_to_org, p_txn_id, sign(l_prf), 30, null,
629                                  null, l_to_subinv, l_to_cg, 0, 2, l_err_num, l_err_code, l_err_msg);
630            else
631 	      /*BUG 8881927 ADDED 'l_to_cg' */
632               l_acct := CSTPACHK.get_account_id(l_to_org, p_txn_id, sign(l_prf), 30, null,
633                                  null, l_to_subinv, 0, 2, l_err_num, l_err_code, l_err_msg,l_to_cg);
634            end if;
635 
636            if (l_err_num <> 0) then
637               raise process_error;
638            end if;
639 
640            if (l_acct = -1) then
641               l_stmt_num := 280;
642 
643               SELECT profit_in_inv_account
644               INTO l_acct
645               FROM mtl_interorg_parameters
646               WHERE from_organization_id = l_from_org
647               AND to_organization_id = l_to_org;
648 
649               if (l_acct is NULL) then
650                   raise prf_inv_acct_err;
651               end if;
652            end if;
653 
654            l_stmt_num := 290;
655 
656            CSTPACDP.insert_account(l_to_org, p_txn_id, p_item_id, l_prf, l_rcv_qty, l_acct,
657                           l_rcv_sob_id, 30, NULL, NULL, p_txn_date, p_txn_src_id, p_src_type_id,
658                           l_rcv_curr, l_snd_curr, l_conv_date, l_conv_rate, l_curr_type, 1,
659                           p_user_id, p_login_id, p_req_id, p_prg_appl_id, p_prg_id,
660                           l_err_num, l_err_code, l_err_msg);
661 
662            if (l_err_num <> 0) then
663               raise process_error;
664            end if;
665         end if;
666      end if;
667 
668   end if;
669 
670   -- Standard check of p_commit
671   if FND_API.To_Boolean(p_commit) then
672      COMMIT WORK;
673   end if;
674 
675   -- Standard call to get message count and message info
676   FND_MSG_PUB.Count_And_Get(p_count => x_msg_count,
677                             p_data  => x_msg_data);
678 
679   if (G_DEBUG = 'Y') then
680      FND_FILE.put_line(FND_FILE.LOG, 'Out of procedure CST_TPRICE_PVT.Adjust_Acct');
681   end if;
682 
683 EXCEPTION
684 
685   when inv_exp_acct_err then
686   rollback to Adjust_Acct_PVT;
687   x_return_status := FND_API.g_ret_sts_error;
688   x_error_num := 9999;
689   x_error_code := 'CST_TPRICE_INV_ACCT_ERROR';
690   FND_MESSAGE.set_name('BOM', 'CST_TPRICE_INV_ACCT_ERROR');
691   x_error_message := FND_MESSAGE.Get;
692   FND_MSG_PUB.Count_And_Get(p_count => x_msg_count,
693                             p_data  => x_msg_data);
694 
695   when prf_inv_acct_err then
696   rollback to Adjust_Acct_PVT;
697   x_return_status := FND_API.g_ret_sts_error;
698   x_error_num := 9999;
699   x_error_code := 'CST_NO_PROFIT_INV_ACCT';
700   FND_MESSAGE.set_name('BOM', 'CST_NO_PROFIT_INV_ACCT');
701   x_error_message := FND_MESSAGE.Get;
702   FND_MSG_PUB.Count_And_Get(p_count => x_msg_count,
703                             p_data  => x_msg_data);
704 
705   when process_error then
706   rollback to Adjust_Acct_PVT;
707   x_return_status := FND_API.g_ret_sts_error;
708   x_error_num := l_err_num;
709   x_error_code := l_err_code;
710   x_error_message := l_err_msg;
711   FND_MSG_PUB.Count_And_Get(p_count => x_msg_count,
712                             p_data  => x_msg_data);
713 
714   when others then
715   rollback to Adjust_Acct_PVT;
716   x_return_status := FND_API.g_ret_sts_error;
717   x_error_num := SQLCODE;
718   x_error_message := 'CST_TPRICE.adjust_acct(' || to_char(l_stmt_num) || ') ' ||
719                      substr(SQLERRM,1,180);
720   FND_MSG_PUB.Count_And_Get(p_count => x_msg_count,
721                             p_data  => x_msg_data);
722 
723 END Adjust_Acct;
724 
725 END CST_TPRICE_PVT;