DBA Data[Home] [Help]

PACKAGE BODY: APPS.GMF_LOT_COSTING_PUB

Source


1 PACKAGE BODY GMF_LOT_COSTING_PUB AS
2 /*  $Header: GMFPLCRB.pls 120.84.12020000.9 2012/12/13 18:04:55 pvkanetk ship $ */
3 
4 --****************************************************************************************************
5 --*                                                                                                  *
6 --* Oracle Process Manufacturing                                                                     *
7 --* ============================                                                                     *
8 --*                                                                                                  *
9 --* Package GMF_LOT_COSTING_PUB                                                                      *
10 --* ---------------------------                                                                      *
11 --* This package contains a publically callable procedure ROLLUP_LOT_COSTS together with several     *
12 --* utility procedures that are called by it. For individual procedures' descriptions, see the       *
13 --* description in front of each one.                                                                *
14 --*                                                                                                  *
15 --* Author: Paul J Schofield, OPM Development EMEA                                                   *
16 --* Date:   September 2003                                                                           *
17 --*                                                                                                  *
18 --* HISTORY                                                                                          *
19 --* =======                                                                                          *
20 --* 12-Sep-2003     PJS     Removed p_debug_level parameter in favour of the GMF_CONC_DEBUG profile  *
21 --*                         option. Also created the audit trail in table GMF_LOT_COST_AUDIT and     *
22 --*                         removed all code that supported the GMF_RESOURCE_LOT_COST_TXNS table.    *
23 --*                                                                                                  *
24 --* 29-Sep-2003     PJS     Added code to support CREI/CRER, TRNI/TRNR, ADJI/ADJR, XFER, OMSO/OPSO   *
25 --*                         PIPH/PICY transactions                                                   *
26 --*                                                                                                  *
27 --* 09-Oct-2003     PJS     Change of direction on storing costs. Currently if a lot is replenished  *
28 --*                         the cost is updated by calculating an averaged cost of the new and old   *
29 --*                         quantities and amending the header and details as required. In the new   *
30 --*                         method we create a new set of costs that contain what the amended costs  *
31 --*                         would have had in them. The reason is so that the sub-ledger update can  *
32 --*                         post the cost of a consumption from a lot at the correct cost, based on  *
33 --*                         the transaction date.                                                    *
34 --*                                                                                                  *
35 --* 10-Oct-2003     PJS     More changes, to incorporate burdens on lot costs. If the new burdens    *
36 --*                         table (GMF_LOT_COST_BURDENS) has an entry for the whse/item/lot/cost     *
37 --*                         method code then the burden cost is incorporated into that specific lot's*
38 --*                         costs. If there is an entry against the whse/item/cost method code then  *
39 --*                         the costs are incorporated into all of the item's lots. A lot that has   *
40 --*                         specific burdens set up will thus have the item level burdens and the    *
41 --*                         lot-specific burdens against it in its resulting lot costs.              *
42 --*                                                                                                  *
43 --* 05-Nov-2003    umoogala Changes made to use cost_category_id instead of itemcost_class.	     *
44 --*                         Also, added lot_ctl = 1 condition to where clause while getting items for*
45 --*			    a passed itemcost_class.						     *
46 --*                                                                                                  *
47 --* 10-Nov-2003    PJS      If we encounter a consumption transaction (eg OMSO) but the lot has not  *
48 --*                         been costed, report an error and skip the transaction.                   *
49 --*                         Also fixed the cursors that retrieve uncosted transactions as a new      *
50 --*                         column has been added to the gmf_lot_costed_items table (co_code) and    *
51 --*                         this created a semi-cartesian join.                                      *
52 --*                                                                                                  *
53 --* 17-Nov-2003    PJS      Reinstate the code that handles acquisition costs as it somehow became   *
54 --*                         lost.                                                                    *
55 --*                                                                                                  *
56 --* 17-Nov-2003    umoogala Removed insert into gmf_lot_cost_audit table and replaced with procedure *
57 --*                         lot_cost_audit which prints to log file when debug profile is set to 3   *
58 --*			          or more.                                                                 *
59 --*			    Introduced and used debug level l_debug_level_none/low/medium/high       *
60 --*			    instead of using numbers.					             *
61 --*			    The reason for removing audit table is the issues with packaging.        *
62 --*			    (O/X)DF doesn't support nested table. XDF allows creation, but it doesn't*
63 --*			    support update or alterations to it. Also, Oracle db itself doesn't      *
64 --*			    support updates to  nested tables - we need to drop and recreate it.     *
65 --*                                                                                                  *
66 --* 21-Nov-2003    umoogala ic_item_mst.lot_costed_flag does not exist. To work around this loaded   *
67 --*		 	    all lot costed item from gmf_lot_costed_items_flag into lc_item_tab. If  *
68 --*			    item exists in this table then lot_costed_flag is set to 1 otherwise to 0*
69 --*			    Called new function is_item_lot_costed to do this in material_cursor.    *
70 --*                                                                                                  *
71 --* 30-Nov-2003    umoogala Now passing co_code to main routines ROLLUP_LOT_COSTS and removed        *
72 --*		 	    calendar_code and user params. Using co_code where ever cldr was used.   *
73 --*		 	    Also, using cm_mthd_mst.default_lot_cost_mthd for non-lot controlled items*
74 --*                                                                                                  *
75 --* 03-Dec-2003    PJS      Moved the code that sets the inventory transaction as 'costed' to after  *
76 --*                         the main CASE statement, and made it conditional on the return status of *
77 --*                         whatever procedure was called.                                           *
78 --*                                                                                                  *
79 --* 05-Dec-2003    umoogala Enabling process for trial runs. Added flag p_final_run_flag to args list*
80 --*                         Update lot_costed_ind when flag is set to Y.                             *
81 --*                                                                                                  *
82 --* 13-Jan-2004    PJS      Fixed the cursors to cater for the case where a recipe has no routing or *
83 --*                         no step dependencies set up.                                             *
84 --*                                                                                                  *
85 --* 21-Jan-2004    umoogala Bug 3388974: Fixed burdens cursor to pickup burdens.		     *
86 --*                         Skip global burden for item when lot specific burden is there.           *
87 --*                                                                                                  *
88 --* 27-Jan-2004    PJS      Bug 3388699: Amended cursor on cm_cmpt_mtl to retrieve analysis_code     *
89 --*                         and cost component class successfully when using company as the sole     *
90 --*                         criterion. Date ranging also added.                                      *
91 --*                                                                                                  *
92 --* 29-Jan-2004    PJS      Reworked burdens processing and acquisition cost processing to ensure    *
93 --*                         correct averaging is always performed. Rescinded some fixes from bug     *
94 --*                         3388974 as they were redundant and reinstated some code that was taken   *
95 --*                         out. Look for comments starting 3388974-2                                *
96 --*                                                                                                  *
97 --* 10-Feb-2004    PJS      Exchange rates catered for on TRNI/TRNR transactions if the movement is  *
98 --*                         between companies that use different currencies.                         *
99 --*                                                                                                  *
100 --* 11-Feb-2004    PJS      Set the actual_cost_ind in GME_BATCH_HEADER if the program is being run  *
101 --*                         in 'final' mode.                                                         *
102 --*                                                                                                  *
103 --* 12-Feb-2004    PJS      Various changes for bugs 3399618, 3401451, 3397388 and 3397408. These    *
104 --*                         reported different manifestations of the same problem of either adding   *
105 --*                         a duplicate row to the gmf_lot_cost_details table (the duplicate was a   *
106 --*                         burden row that shared the same keys) or a division by zero if there     *
107 --*                         was no onhand quantity.                                                  *
108 --*                                                                                                  *
109 --* 12-Feb-2004    PJS      Changes to prevent multiple updates to a cost header flagged as 'Final'. *
110 --*                         We now clone the row and the associated details so that the onhand_qty   *
111 --*                         can be maintained correctly. Also treat batch ingredient lines in the    *
112 --*                         same was ADJI/R transactions.                                            *
113 --*                                                                                                  *
114 --* 17-Feb-2004    PJS      Set the final flag in the cost heder if mode is not draft.               *
115 --*                                                                                                  *
116 --* 24-Feb-2004    PJS      Fixed indexing when writing burdens for a CREI                           *
117 --*                                                                                                  *
118 --* 25-Feb-2004    PJS      Added burden_ind to gmf_cost_type.                                       *
119 --*                                                                                                  *
120 --* 25-Feb-2004    PJS      Added handling for replenishing negative onhand quantities.              *
121 --*                                                                                                  *
122 --* 03-Mar-2004    umoogala Bug 3476508: setting status to show normal/warning/error on process req  *
123 --*			    screen.                                                                  *
124 --*			    Also, if any error is found in a trx (say, no rsrc cost), now we will    *
125 --*			    skip all trxns related to that item/lot/whse combination.                *
126 --*                                                                                                  *
127 --* 08-Mar-2004    PJS      Changes for bugs 3485915 (receipt uom conversion) and 3476427 (order of  *
128 --*                         parameters changed)                                                      *
129 --*                                                                                                  *
130 --* 09-Mar-2004    PJS      Ignore the orgn_code in the transaction when looking up resource costs   *
131 --*                         and use ic_whse_mst to find the correct one instead.                     *
132 --*                                                                                                  *
133 --* 11-Mar-2004    PJS      Changes to support lot cost adjustments (a pseudo transaction of type    *
134 --*                         'LADJ'). Also introduced logic to skip transactions for lots that have   *
135 --*                         failed costing attempts already.                                         *
136 --*                                                                                                  *
137 --* 15-Mar-2004    PJS      XFER txns do not use line types of +1 and -1. Use line id instead to     *
138 --*                         decide 1 => -1, 3 => +1                                                  *
139 --*                                                                                                  *
140 --* 17-Mar-2004    PJS      Various issues with cost adjustments. Altered cursors to respect delete  *
141 --*                         marks and retrieve the item's primary uom. Also avoided in/out clash in  *
142 --*                         call to create_header. No bug reference.                                 *
143 --*                                                                                                  *
144 --* 18-Mar-2004    umoogala Lot Cost Adjustment query fix. Removed ref to gmf_lot_cost_adjustment_dtls*
145 --*                                                                                                  *
146 --* 19-Mar-2004    PJS      Bug 3514108 - RMA fixes and Bug 3513668 - Requisitions                   *
147 --*                                                                                                  *
148 --* 22-Mar-2004    PJS      Reworked 3485915 and 3476427. Also tightened up error messaging for the  *
149 --*                         cases where clashing parameters are specified. 3486228 also fixed by     *
150 --*                         setting the transaction quantity to the residual quantity when a balance *
151 --*                         is flipped positive again.                                               *
152 --*                                                                                                  *
153 --* 24-Mar-2004    umoogala Lot Cost Adjustments. Updating onhand_qty in adjs table. Now trans_id in *
154 --*			    material txn table is -ve of cost_trans_id.				     *
155 --*			    Another change to clear old_cost record for each trx being processed.    *
156 --*                                                                                                  *
157 --* 25-Mar-2004    PJS      No bug reference. Do not use new_cost_tab elements as the target of an   *
158 --*                         OUT NOCOPY procedure parameter as a run time error results.              *
159 --*                                                                                                  *
160 --* 29-Mar-2004    PJS      BUG 3533452. Change process_batch procedure so that the same merge       *
161 --*                         procedure is called that other procedures call. Also gross up retrieved  *
162 --*                         cost by the yield quantity.                                              *
163 --*                                                                                                  *
164 --* 29-Mar-2004    umoogala Modified delete_lot_costs to first check item_id instead of category     *
165 --*                         since item overrides category.					     *
166 --*                                                                                                  *
167 --* 30-Mar-2004    PJS      Resolved an arry out of bounds error from 3533452.                       *
168 --*                                                                                                  *
169 --* 06-Apr-2004    PJS      Major changes to handle multiple yields from the same step and also to   *
170 --*                         handle to differing requirements of yields from terminal/non-terminal    *
171 --*                         steps. Also deleted procedure dump_batch_steps in favour of the lot_     *
172 --*                         cost_audit variant and tided up the debug lines. Batch changes cover     *
173 --*                         bug 3548217.                                                             *
174 --*                                                                                                  *
175 --* 06-Apr-2004    PJS      Fixed small problem with batches that possess a routing but no step      *
176 --*                         dependency chain. Now attach all inventory to final step.                *
177 --*                                                                                                  *
178 --* 07-Apr-2004    PJS      B3556291 - attach any unassociated ingredients to first step and any     *
179 --*                         unassociated products to final step                                      *
180 --*                                                                                                  *
181 --* 15-Apr-2004    PJS      Various issues with mixed mode accounting in the same batch. Anything    *
182 --*                         that is not a lot costed (co) product now has its costs subtracted from  *
183 --*                         the total costs accrued to date before the residual costs are shared     *
184 --*                         amongst the remaining lot costed products. Also tidied up a bit so that  *
185 --*                         presets no longer cause GSCC warnings.                                   *
186 --*                                                                                                  *
187 --* 16-Apr-2004    PJS      Sub Ledger Update has a problem with the costs generated for batch       *
188 --*                         yields if the cost(s) have been replenished. To get round this the       *
189 --*                         calculated costs are stored alongside the merged costs but with a        *
190 --*                         negated header ID. This is for bug 3578680 and now applies to all lots   *
191 --*                         that are replenished.                                                    *
192 --*                                                                                                  *
193 --* 22-Apr-2004    PJS      Rider to previous change. So as not to degrade the performance of the    *
194 --*                         SLU process a new column 'new_cost_ind' is being added to the gmf_       *
195 --*                         material_lot_cost_txns table. If a cost is updated then the indicator    *
196 --*                         will be set to 1. Null otherwise. This saves a select per transaction in *
197 --*                         the SLU as most of the time there will not be a new cost.                *
198 --*                                                                                                  *
199 --* 05-May-2004    PJS      Reworked the fix for bug 3578680 for TRNI etc.                           *
200 --*                                                                                                  *
201 --* 06-May-2004    umoogala Updating gmf_lot_cost_adjustments rows even in trial run for subledger to*
202 --*                         process. Only in final mode applied_ind is set to 'Y'. No bug was created*
203 --*                                                                                                  *
204 --* 25-May-2004    PJS      Bug 3643858. Include delete mark on LADJ cursor.  			     *
205 --*												     *
206 --* 19-Aug-2004    Dinesh   Bug# 3831782                                         	             *
207 --*        Added where clause in the queries in proc rollup_lot_costs to ignore the                  *
208 --*        Lot Cost Adjustment Records which has no Detail Records(i.e., NULL Adjustment Cost)       *
209 --*
210 --* 27-Nov-2004 Dinesh Vadivel Bug# 4004338
211 --*       Modified the basic INV_TRAN_CURSOR query to order the transactions from ic_tran_pnd table.
212 --*       Right now if we change a batch actual output qty from 100 to 150, the records in ic_tran_pnd table are
213 --*       in order as 100, 150 and -100 .
214 --*      Since our costing logic, needs them in 100,-100 and 150 order inv_tran_Cursor's order_by clause is modified
215 --*
216 --* 15-Dec-2004 Dinesh Vadivel Bug# 4053149
217 --*      Modified the process_batch to handle the various cases for the step dependencies
218 --*      and to create the virtual row appropriately. Also added new explosion_cursor_ss
219 --*
220 --*     Modified the process_reversals to populate the new_cost_ind. modified the call to the
221 --*     create_material_transaction to pass this new_cost_ind instead of NULL. This field is
222 --*     being used in SL update posting.
223 --*
224 --* 06-Jan-2004 Dinesh Vadivel Bug# 4095937
225 --*       The Lot Cost Process calculates the Exchg Rate as on the Receipt Header Date
226 --*        whereas the Subledger uses the "Exchg Rate Date". Modified the Lot Actual Cost Process
227 --*        to use the rcv_transactions.CURRENCY_CONVERSION_RATE as the Exchg Rate .
228 --*       This is how we are doing in Actual Costing and Subledger Update
229 --*
230 --*  20-Jan-2005 Girish Jha Bug 4094132
231 --*       We added a new filtering condition in the materials_cursor for handling reversals
232 --*
233 --* 28-Jan-2005 Dinesh Vadivel Bug 4057323 - Cost Allocation Factor Enhancement.
234 --*        Modified code especially in process_batch to use Cost Allocation Factor when the profile
235 --*        "GMF: Use Cost Alloc Factor in Lot Costing" is set to Yes. Also the cost Allocation factor will
236 --*         considered only under any of the following cases
237 --*       1. All the Products have to be yielded at one single step, not necessarily terminal step
238 --*       2. No Product should be associated to any step.
239 --*
240 --* 28-Jan-2005 Dinesh Vadivel - Bug 4149549 - Issue occurs only when debug_level is not 3.
241 --*      In rollup_lot_costs the initialization of old_cost.onhand_qty has been done inside the
242 --*      IF(l_debug_level >= debug_level_high)..... So the variable didn't get initialized when the
243 --*      debug level is not 3.
244 --*
245 --*   30-Jan-2005 Dinesh Vadivel Bug# 4152397
246 --*      Uncommented the code which fetches lot_id into l_lot_id for the lot_no
247 --*      entered on Lot ACP screen.
248 --*      Also, the inv_tran_cursor inside "ELSIF l_item_id IS NOT NULL .... "
249 --*      we have filter only those LADJ transactions for that particular Lot_id if any.
250 --*
251 --*   02-Feb-2005 Dinesh Vadivel Bug# 4130869
252 --*       Modified the Lot_Cost_Cursor to also filter by correct transaction_date
253 --*
254 --*  03-Feb-2005 - Bug 4144329 - Dinesh Vadivel - If there is no cost defined for Resource
255 --*            then don't stop by setting the cost as uncostable. Just give a warning and ignore
256 --*            the resources.
257 --*
258 --*  24-Feb-2005 Dinesh Vadivel Bug# 4177349
259 --*      When a batch is reversed to WIP, the Lot Cost process fails at process_reversals
260 --*      Now modified the code to handle the product yielding transactions.
261 --*      Added process_wip_batch for this purpose. More comments before the procedure
262 --*      definition
263 --*   24-Feb-2005 Dinesh Vadivel Bug# 4187891
264 --*      Cancellation of Inv Xfer has been modified such that it is considered
265 --*       as if it is an actual transfer where the source and destination warehouses are the same.
266 --*  24-Feb-2005 Dinesh Vadivel Bug# 4176690
267 --*      Added a new Date Field in the Lot Cost Process submission screen
268 --*      All the transactions only upto the Date entered will be considered for Lot Costing.
269 --*  13-Mar-2005 Dinesh Vadivel Bug# 4227784
270 --*    - Both these bugs have same code change of replacing l_total_item_qty to actual_line_qty
271 --*      in process_batch()
272 --*    - Added NVL clause in process_reversals to avoid "ORA error- Cannot insert NULL into
273 --*      into TOTAL_TRANS_COST".
274 --*    - Moved the old_cost.onhand_qty into ELSE part of lot_cost_cursor%FOUND in
275 --*      rollup_lot_costs(). This is to initialize if there is no record in gmf_lot_costs
276 --*  19-Apr-2005 Dinesh Bug# 4165614
277 --*     Passed correct shipped date to book the receive transaction in case of internal orders
278 --*     Modified argument passing date for process_movements in process_receipts()
279 --*  28-Apr-2005 WmJohn Harris Bug# 4307381  --  procedure process_batch :
280 --*     more detailed debug info for 2 msgs 'Unable to convert from ' ... correct subscript in 2nd msg
281 --*  23-May-2005 Dinesh Vadivel - Bug 4320765 - Modified item_cost_cursor to support warehouse association
282 --*     functionality for the Alternate Cost Method
283 --*  31-May-2005 Dinesh Vadivel - Bug 4320765(Part B)
284 --*     Modified "Explosion_Cursor" to avoid duplicate rows
285 --*  01-Jun-2005 Sukarna Reddy INCONV changes for release 12.
286 --*  07-Jun-2006 Anand Thiyagarajan Bug#5285726
287 --*     Modified Code to remove the references to trans_id in gmf_material_lot_cost_txns table
288 --*     which caused the lot cost process after a final run to calculate wrong costs
289 --*  07-Jun-2006 Anand Thiyagarajan Bug#5287514
290 --*     Modified code to call process_receipts procedure for Purchase Order Return to Vendors
291 --*     and for PO Receipt corrections, which are similar to the Purchase Order receipts with a +ve or -ve signs
292 --*  26-Jul-2006 Anand Thiyagarajan Bug#5412410
293 --*     Modified code in process_lot_split and process_lot_translate to correct the code pertaining to call of
294 --*     merge_costs procedure and also to correct the value of total trans cost and unit cost being passed to the
295 --*     create_material_transaction and create_lot_header procedures.
296 --*  14-Aug-2006 Anand Thiyagarajan Bug#5463200
297 --*     Modified Code in the Special Charges query to remove po_line_locations_all, changed the
298 --*     value for include_in_acquisition_cost to "I", used estimated_amount instead of actual
299 --*     and included MMT table join with RCV_TRANSACTION_ID instead of INV_TRANSACTION_ID
300 --*   2-Aug-2007 Venkat Ch. Bug 6320304/5953977 - Non-recoverable taxes ME, as part of this
301 --*     added unit of nonrecoverable tax to the unit cost in process_receipt().
302 --*   30-Jun-2008 Bug 7215069
303 --*      Changed ordering for Receipt transactions in inv_tran_cursor. If transactions exist for multiple
304 --*      document types with same transaction date then first process the receipt transactions.
305 --*   26-Aug-2008 Pramod B.H. Bug#7306720
306 --*      Modified the cursor component_class_cursor to handle the issue of LACP considering incorrect
307 --*      cost component class.
308 --*   12-Mar-2009 Hari Luthra Bug # 7317270
309 --*      Modified materials_cursor and many other cursors as part of F.P. fix for bug 7312497
310 --*   10-Apr-2009 Hari Luthra Bug # 7173679
311 --*	 Modified delete statment in delete_lot_costs to improve the performance as part of F.P. fix for 11i bug 7159210
312 --*	21-May-2009 Bug 7249505 HARI LUTHRA
313 --*	HALUTHRA BUG 7249505 In case of return to vendor process the transaction as an adjustment
314 --*    21-MAY-2009 Bug 6165255 and BUG 8330088 HARI LUTHRA
315 --*	 Modified the cursor unassociated_ings_cursor and unassociated_prds_cursor so as to include ingrediant transactions
316 --*	 which are non lot controlled and also to take the primary qty and primary UOM of item from mtl_material_transactions
317 --*	and mtl_system_items_b instead of transaction_qty and transaction_uom to avoid the dual_uom issues.
318 --*    27-MAY-2009 Hari Luthra Bug 5473138/8533290
319 --*	 Modified item_cost_cursor , item_cost_detail_cursor to avoid issues with cost warehouse assosciation
320 --*	 Also modified query which picks up all the transactions on the basis of the parameters fed while running Lot Cost Process
321 --*	 Made changes in this query to handle phantom item issues.
322 --*      Also modified transaction_type cursor defination to synchronize the changes with the changes made in the query to pick all transaction records
323 --*    27-JUL-2009 Parag Kanetkar Bug 8687115
324 --*      Process transactions only for legal entity on request. Delete preliminary lot costs also for
325 --*      legal entity on request.
326 --*    27-JUL-2009 Parag Kanetkar Bug 8730374 All items belonging to submitted items cost category get processed
327 --*      even if request is submitted for just one item. Moreover Procedure ReLoad_Lot_Costed_Items_gt does not load
328 --*      Items if assigned by category. Modified ReLoad_Lot_Costed_Items_gt to use cost category set up of lot costed items.
329 --*    30-jul-2009 LCMOPM dev, bug 8642337 LCM-OPM Integration, Modified code to consider LC adjustments in lot cost calculations
330 --*      1) Added one union query to inv_tran_cursor cursor to load LC adjustments
331 --*      2) created new procedure process LC adjustments to process actual LC adjustments
332 --*      3) Added one union to the existsing query in get special charges procedure to process ELCs
333 --*    22-FEB-2010 Parag Kanetkar Bug 9356358 / 12.1 Bug 9400419 . Transfer_price in MMT is already in receiving org currency.
334 --*       Use transfer price in MMT to create incoming lot cost in process_movement for receipts of transfers
335 --*       across different legal entities.
336 --*    03-MAY-2010 Bug 9669214  Parag Kanetkar - Create Negative header_id cost details in
337 --*       process_pdtxf_cost.
338 --*    03-MAY-2010 Bug 9616762  Front port of Bug 4601607 to 12.1 - Don't ignore batch even if any of the
339 --*             actual step qty is zero. Also made use of same logic in computing
340 --*             cost_alloc factor for products, for products yielded in intermediate steps
341 --*             as well as for products yielded in terminal step
342 --*    03-MAY-2010 Bug 9616762 Parag Kanetkar -- Additional changes for this bug to exclude 50: Container Pack
343 --*                                              51: Container Unpack
344 --*    09-SEP-2011 Bug 13000758 Saptagirish Pabolu -- Added the condition "IF l_debug_level >= l_debug_level_medium THEN"
345 --*                                                   to avoid unnecessary messages in the log file.
346 --*    23-DEC-2011 Bug13386258 / Bug13038249: Modified LACP code to improve LACP Performance.
347 --*    29-DEC-2011 Bug 13528789 Pramod B.H
348 --*     Procedure process_reversals2() modified to handle the issue of gmf_material_lot_cost_txns.TOTAL_TRANS_COST
349 --*     incorrectly updated with positive amount for WIP Completion Return transaction.
350 --*    06-Jan-2012 Bug 13038249 Parag Kanetkar
351 --*     - Modifications to CURSOR unassociated_ings_cursor (specific to batches with routing steps) to group
352 --*     transactions for non lot costed ingredients.
353 --*     - Modified copy_uncostable_record() procedure to copy routing_id, burden_ind values to l_uncostable_tab.
354 --*     - Modified copy_to_transrow() procedure to copy routing_id, burden_ind values to transaction_row.
355 --*    08-Aug-2012 Bug 13808832 Pramod B.H
356 --*      Modified procedure rollup_lot_costs() to give a call to process_wip_batch() for WIP batch yield (as earlier)
357 --*      based on an internal profile value.
358 --****************************************************************************************************
359 
360 -- Bug 13038249 Grupo viz changes so far.
361 -- 1) Storing fiscal policy cost component, analysis code and Lot cost allocation profile. Do not query database all the time.
362 -- 2) Routing_id fetched in INV_TRAN cursor itself. Do not query DB.
363 -- 3) item_cost_detail_cursor fetches Total item cost as well. Do not use item_cost_cursor to get total item cost
364 -- 4) Eliminate querying and deleting of zero cost lot cost details.
365 -- 5) Load only current LE Organizations
366 -- 6) Genesis Bug Create matl txn adding burdens
367 
368 -- C3 Changes.
369 -- 7) Load only lot costs from prior 60 days.
370 -- C4 Changes.
371 -- 8) Burden indicater.
372 -- 9) inv_convert only when different
373 -- 10) inv_tran_cursor, no mtl_system_items in lot cost adjustment query section.
374 -- 11) Removing Le_id join for process_org_gt table
375 -- 12) suggesting following
376 --CREATE INDEX gmf_lot_costed_items_gt_N1 ON gmf_lot_costed_items_gt
377 -- (inventory_item_id,organization_id) ;
378 -- CREATE INDEX GMF_PROCESS_ORGN_GT_N1 ON GMF_PROCESS_ORGANIZATIONS_GT
379 -- (organization_id) ;
380 -- 13) org colleciton
381 -- 14) item_gt colleciton
382 -- 15) prinitng organ_code and item_no in the messages
383 -- 1a changes (Bug13386258)
384 --   While calculating cost of a batch yield txn of a batch, Avoid refering to
385 --   other txns(which can involve UOM conversions) of the batch when We are in terminal Step.
386 -- 1b Changes (Bug13386258)
387 --   Avoid calls to UOM conversion routines when: (b) value of cost allocation profile in lot
388 --   costing is set to honor values from the batch
389 -- 1d changes (Bug13386258)
390 -- Record uncostable batch. Check if batch is uncostable first.
391 -- Added mtl_default_category_sets mdc, mtl_category_sets mcs  when loading lot costed items.
392 -- 1d_v1 Changes (Bug13386258)
393 --   Modified Load_Lot_Costed_Items_gt() to split the insert to 3 inserts
394 
395   --**********************************************************************************************
396   --*                                                                                            *
397   --* The procedures in this package share a common set of data structures. The most complex one *
398   --* being the l_step_tab table defined as being of type gmf_step_tab. This is a database type  *
399   --* that has nested tables in it. Once a batch has been exploded, its steps will be ordered    *
400   --* correctly in this table so that the costs of a step can be rolled forward into subsequent  *
401   --* steps until we fall off the end of the routing.                                            *
402   --*                                                                                            *
403   --**********************************************************************************************
404 
405 
406 
407     /* INVCONV sschinch Constants representing Transaction source type id */
408     PURCHASE_ORDER      CONSTANT PLS_INTEGER  := 1;
409     SALES_ORDER         CONSTANT PLS_INTEGER  := 2;
410     ACCOUNT             CONSTANT PLS_INTEGER  := 3;
411     MOVE_ORDER          CONSTANT PLS_INTEGER  := 4;  -- B 6859710
412     BATCH               CONSTANT PLS_INTEGER  := 5;
413     ACCOUNT_ALIAS       CONSTANT PLS_INTEGER  := 6;
414     INTERNAL_REQ        CONSTANT PLS_INTEGER  := 7;
415     INTERNAL_ORDER      CONSTANT PLS_INTEGER  := 8;
416     CYCLE_COUNT         CONSTANT PLS_INTEGER  := 9;
417     PHYSICAL_INVENTORY  CONSTANT PLS_INTEGER  :=10;
418     RMA                 CONSTANT PLS_INTEGER  :=12;
419     INVENTORY           CONSTANT PLS_INTEGER  :=13;
420 
421 
422     /* INVCONV sschinch Transaction Actions */
423 
424     LOT_COST_ADJUSTMENT         CONSTANT PLS_INTEGER :=0;
425     ISSUE_FROM_STORES           CONSTANT PLS_INTEGER := 1;
426     DIRECT_ORG_TRANSFER         CONSTANT PLS_INTEGER := 3;
427     CYCLE_COUNT_ADJUSTMENT      CONSTANT PLS_INTEGER := 4;
428     OWNERSHIP_TRANSFER          CONSTANT PLS_INTEGER := 6;
429     PHYSICAL_INVENTORY_ADJST    CONSTANT PLS_INTEGER := 8;
430     INTRANSIT_RECEIPT           CONSTANT PLS_INTEGER := 12;
431     LOGICAL_INTRANSIT_RECEIPT   CONSTANT PLS_INTEGER := 15;
432     INTRANSIT_SHIPMENT          CONSTANT PLS_INTEGER := 21;
433     LOGICAL_INTRANSIT_SHIPMENT  CONSTANT PLS_INTEGER := 22;
434     RECEIPT_INTO_STORES         CONSTANT PLS_INTEGER := 27;
435     DELIVERY_ADJUSTMENTS        CONSTANT PLS_INTEGER := 29; /* ANTHIYAG Bug#5287514 07-Jun-2006 */
436     LOT_SPLIT                   CONSTANT PLS_INTEGER := 40;
437     LOT_MERGE                   CONSTANT PLS_INTEGER := 41;
438     LOT_TRANSLATE               CONSTANT PLS_INTEGER := 42;
439     LC_ADJUSTMENT               CONSTANT PLS_INTEGER := 50; -- LCM-OPM Integration
440 
441     /* INVCONV sschinch FOB flags */
442     FOB_SHIPPING   CONSTANT PLS_INTEGER := 1;
443     FOB_RECEIVING  CONSTANT PLS_INTEGER := 2;
444 
445     l_calendar_code    cm_cldr_hdr.calendar_code%TYPE;
446     l_default_cost_type_id NUMBER; /* INVCONV sschinch */
447     l_le_id          NUMBER;
448     l_cost_type_id      NUMBER;
449 
450     --Use this to get the costof non-lot controlled items
451     l_default_lot_cstype_id  NUMBER;
452     l_cost_method_code  cm_mthd_mst.cost_mthd_code%TYPE;
453     l_trans_start_date  cm_mthd_mst.trans_start_date%TYPE;
454 
455     --l_cost_class       mtl_categories_v.category_concat_segs%TYPE;
456     l_cost_category_id  mtl_categories_v.category_id%TYPE;
457 
458     l_lot_no             mtl_lot_numbers.LOT_NUMBER%TYPE; /* INVCONV sschinch */
459 
460     l_debug_level      PLS_INTEGER;
461     l_user             fnd_user.user_name%TYPE;
462     l_return_status    VARCHAR2(1) := 'S';
463 
464     l_final_run_date DATE; -- Bug 4176690
465 
466 
467     l_debug_level_none     PLS_INTEGER;
468     l_debug_level_low      PLS_INTEGER;
469     l_debug_level_medium   PLS_INTEGER;
470     l_debug_level_high     PLS_INTEGER;
471 
472     TYPE l_cost_tab_type IS TABLE OF SYSTEM.gmf_cost_type INDEX BY PLS_INTEGER;
473 
474 -- Bug 13038249 Grupo viz changes starts
475 
476     l_orgn_code      VARCHAR2(4)  := NULL;
477     l_item_no        VARCHAR2(50) := NULL;
478 
479     TYPE l_total_alt_cost IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
480     total_alt_cost_tab   l_total_alt_cost ;
481     org_coll_size      PLS_INTEGER;  /* Number of rows in  org_collection */
482 
483     TYPE uncostable_batches_tab IS TABLE OF gme_batch_header.batch_id%TYPE INDEX BY PLS_INTEGER;
484     l_uncostable_batches_tab        uncostable_batches_tab;
485 
486 TYPE org_coll_typ IS RECORD
487 (
488       organization_id              mtl_parameters.organization_id%TYPE
489     , organization_code            mtl_parameters.organization_code%TYPE
490     , process_enabled_flag         mtl_parameters.process_enabled_flag%TYPE
491     , le_id                        hr_organization_information.org_information2%TYPE
492     , ou_id                        hr_organization_information.org_information3%TYPE
493 );
494   TYPE org_coll_tbl IS TABLE OF org_coll_typ INDEX BY PLS_INTEGER;
495   org_coll_tab        org_coll_tbl;
496 
497      /* Bug 13386258-VC */
498    TYPE batch_details_typ IS RECORD
499    (
500       batch_id                     gme_batch_header.batch_id%TYPE
501     , step_tab                     SYSTEM.gmf_step_tab
502    );
503    TYPE batch_details_tbl IS TABLE OF batch_details_typ INDEX BY PLS_INTEGER;
504    c_batch_details        batch_details_tbl;
505    /* Bug 13386258-VC End */
506 
507 TYPE period_typ IS RECORD
508 (
509       period_id              gmf_period_statuses.period_id%TYPE
510     , final_date             gmf_period_statuses.CALENDAR_CODE%TYPE
511     , start_date             gmf_period_statuses.start_date%TYPE
512     , end_date               gmf_period_statuses.end_date%TYPE
513 );
514 
515   TYPE period_tbl IS TABLE OF period_typ INDEX BY VARCHAR2(30);
516   period_tab        period_tbl;
517 
518   -- Alternate Period cost caching
519 TYPE Alter_cost_typ IS RECORD
520 (
521       inventory_item_id            mtl_system_items.Inventory_item_id%type  ,
522       organization_id              mtl_parameters.organization_id%TYPE      ,
523       period_id                    gmf_period_statuses.period_id%TYPE       ,
524       cost_details                 SYSTEM.gmf_cost_tab                      ,
525       total_cost                   NUMBER
526 ) ;
527     TYPE Alter_cost_tbl IS TABLE OF Alter_cost_typ INDEX BY VARCHAR2(90);
528     Alter_cost_tab        Alter_cost_tbl;
529 
530     TYPE itemno_tab    IS TABLE OF VARCHAR2(50) INDEX BY BINARY_INTEGER;
531     l_itemno_tab       itemno_tab;
532 
533 -- Bug 13038249 Grupo viz changes Ends
534 
535     old_cost           gmf_lot_costs%ROWTYPE;
536     old_cost_tab       l_cost_tab_type; -- Existing lot costs of lot being rolled up
537 
538     new_cost           gmf_lot_costs%ROWTYPE;
539     new_cost_tab       l_cost_tab_type; -- New lot costs of lot being rolled up
540 
541     ing_cost           gmf_lot_costs%ROWTYPE;
542     ing_cost_tab       l_cost_tab_type; -- Batch ingredient cost
543 
544     prd_cost           gmf_lot_costs%ROWTYPE;
545     prd_cost_tab       l_cost_tab_type; -- Batch product cost
546 
547     cur_cost_tab       l_cost_tab_type; -- Current costs of batch step
548 
549     res_cost           NUMBER;
550     item_unit_cost     NUMBER;
551     lot_unit_cost      NUMBER;
552     lot_total_cost     NUMBER;
553 
554 
555     l_rate_type_code   VARCHAR2(4);
556     l_from_ccy_code    VARCHAR2(4);
557     l_to_ccy_code      VARCHAR2(4);
558     l_exchange_rate    NUMBER;
559     l_mul_div_sign     NUMBER;
560     l_error_status     NUMBER;
561 
562     l_lot_number  mtl_lot_numbers.lot_number%type; /* INVCONV sschinch */
563     l_source_lot_number mtl_lot_numbers.lot_number%type; /* INVCONV sschinch */
564     l_orgn_id NUMBER;
565 
566     l_batch_no         gme_batch_header.batch_no%TYPE; /* Bug 4297815 */
567     l_base_ccy_code VARCHAR2(4);  /* Bug 4038722 Dinesh Vadivel */
568 
569 
570     l_tmp  BOOLEAN; -- Bug 3476508
571     i      PLS_INTEGER; -- Loop counter to sweep through uncosted inventory transactions
572     j      PLS_INTEGER;
573     k      PLS_INTEGER;
574     l      PLS_INTEGER;
575 
576     dummy  NUMBER;
577     l_item_id          mtl_item_flexfields.inventory_item_id%TYPE;
578     l_batchstep_id     NUMBER;
579     l_total_qty        NUMBER;
580     TYPE num_tab       IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
581     TYPE char_tab      IS TABLE OF VARCHAR2(80) INDEX BY VARCHAR2(80); /* INVCONV sschinch */
582 
583     TYPE org_tab       IS TABLE OF VARCHAR2(4) INDEX BY BINARY_INTEGER;
584     l_org_tab          org_tab;
585 
586     l_cost_mthd_code   cm_mthd_mst.cost_mthd_code%type;
587     l_default_cost_mthd cm_mthd_mst.cost_mthd_code%type;
588 
589     l_step_lev         num_tab;
590     l_step_tab         SYSTEM.gmf_step_tab;
591     l_skip_this_batch  BOOLEAN ;
592     l_skip_this_txn    BOOLEAN ;
593     l_cost_accrued     BOOLEAN ;
594     l_batch_status     NUMBER;
595     l_cost_factor      NUMBER;
596     l_step_index       NUMBER;
597 
598     receipt_qty         NUMBER;
599     receipt_unit_cost   NUMBER;
600     receipt_uom         VARCHAR2(3);
601     receipt_ccy         VARCHAR2(4);
602     l_residual_qty      NUMBER;
603     l_flg_ind           VARCHAR2(1);
604 
605     component_class_id  cm_cmpt_mst.cost_cmpntcls_id%TYPE;
606     cost_analysis_code  cm_alys_mst.cost_analysis_code%TYPE;
607 
608     /*l_uncostable_lots_tab num_tab; INVCONV sschinch*/
609       /*l_uncostable_lots_tab char_tab;  jboppana */
610 
611     TYPE lot_uncostable_lots_tab IS TABLE OF mtl_item_flexfields.inventory_item_id%TYPE INDEX BY VARCHAR2(150);
612     l_uncostable_lots_tab        lot_uncostable_lots_tab;
613 
614 
615     l_cost_alloc_profile NUMBER; -- Bug 4057323
616 
617     TYPE burdens_rec IS RECORD
618     (
619       LOT_BURDEN_LINE_ID              NUMBER(15)
620     , RESOURCES                       VARCHAR2(32)
621     , COST_CMPNTCLS_ID                NUMBER(10)
622     , COST_ANALYSIS_CODE              CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE --bug14480540
623     , BURDEN_FACTOR                   NUMBER
624     , BURDEN_COST                     NUMBER
625     );
626 
627 
628     TYPE burdens_tab IS TABLE OF burdens_rec;
629     l_burdens_tab       burdens_tab;
630     l_burden_cost       NUMBER;
631     l_burden_costs_tab  l_cost_tab_type;
632     l_burdens_total     NUMBER;
633 
634     l_acqui_cost_tab    l_cost_tab_type;
635     l_acquisitions_total NUMBER;
636 
637     l_onhand_qty         NUMBER;
638     -- umoogala
639     TYPE lot_costed_items_tab IS TABLE OF mtl_item_flexfields.inventory_item_id%TYPE INDEX BY VARCHAR2(64);
640     lc_items_tab        lot_costed_items_tab;
641 
642     -- WHO columns -- umoogala 05-Dec-2003
643     l_user_id		fnd_user.user_id%TYPE;
644     l_login_id		NUMBER;
645     l_prog_appl_id  	NUMBER;
646     l_program_id	NUMBER;
647     l_request_id	NUMBER;
648 
649     l_routing           NUMBER;
650     l_dep_steps         NUMBER;
651    -- l_step_items        NUMBER;     /* No where used */
652     l_prior_step_id     NUMBER;
653 
654     l_final_run_flag	  NUMBER(1);   -- 1 for final run and 0 for trial run
655     -- Bug 13038249
656     l_mtl_analysis_code  CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE; --bug14480540
657     l_mtl_cmpntcls_id    NUMBER;
658     cost_alloc_profile   NUMBER;
659     l_dtl_inserted       NUMBER;
660 
661 
662 /* FORWARD DECLARATIONS INVCONV sschinch */
663 
664     --**********************************************************************************************
665     --*                                                                                            *
666     --* These next few declarations are for the main driving query. The rollup_lot_costs procedure *
667     --* is callable in several ways, and, for efficiency, each type of invocation has a dedicated  *
668     --* cursor. Each one returns rows of transaction_type (defined below). The rollup_lot_costs    *
669     --* procedure examines the parameters passed in and sets up a cursor appropriate to them.      *
670     --*
671     --* 27-MAY-2009 HARI LUTHRA Bug 8533290/5473138 Modified the transaction_type record defination*
672     --* so as to include the three new selece columns in rollup_lot_costs procedure		   *
673     --* PK Bug 13038249 Add batch_status,routing_id.
674     --**********************************************************************************************
675 
676     TYPE transaction_type  IS RECORD
677     ( doc_id            NUMBER
678     , transaction_source_type_id  NUMBER(5)
679     , inventory_item_id           NUMBER
680     , line_id           NUMBER
681     , line_type         NUMBER
682     , lot_number         VARCHAR2(80)
683     , trans_date        DATE
684     , transaction_id          NUMBER
685     , trans_qty         NUMBER
686     , trans_um          VARCHAR2(4)
687     , orgn_id           NUMBER
688     , source            NUMBER(1)
689     , reverse_id        NUMBER
690     , transaction_action_id NUMBER(5)
691     , transfer_price     NUMBER
692     , transportation_cost NUMBER
693     , fob_point          NUMBER(1)
694     , transfer_transaction_id NUMBER
695     , transaction_cost     NUMBER
696     , transfer_orgn_id   NUMBER
697     , phantom_trans_date  DATE
698     , phantom_type       NUMBER
699     , pair_type          NUMBER
700     , oc1                DATE
701     , oc2                NUMBER
702     , oc3                NUMBER
703     , routing_id         NUMBER    /* Bug 13038249  Grupo */
704     , burden_ind         NUMBER    /* Bug 13038249  Grupo */
705     );
706 
707     TYPE inv_tran_cursor_type IS REF CURSOR RETURN transaction_type;
708 
709     transaction_row transaction_type;
710     /* B9894310 */
711     TYPE uncostable_tab IS TABLE OF transaction_type INDEX BY BINARY_INTEGER;
712     l_uncostable_tab       uncostable_tab;
713     n_uncostable_tab       uncostable_tab;
714     uc_count        INTEGER := 0 ;
715     nuc_count       INTEGER := 0 ;
716     l_incr_iter     NUMBER:= 0; /* Incremental Lot costing Profile */
717 
718     l_process_wip_batch NUMBER:= 0; /*Bug 13808832*/
719 
720     /* INVCONV sschinch */
721     TYPE child_lots_rec  IS RECORD
722     ( lot_number VARCHAR2(80),
723       trans_qty  NUMBER,
724       trans_date DATE
725     );
726     child_lot_row  child_lots_rec;
727 
728 
729     --**********************************************************************************************
730     --*                                                                                            *
731     --* This, rather complex, cursor explodes the batch routing and orders the steps in an order   *
732     --* suitable for the rollup. This is not necessarily the same as the ordering implied by the   *
733     --* step dependencies. The terminal step(s) of the routing are found and then a heirarchical   *
734     --* sub-query generates the base rows (that are added to by other parts of the query) ordered  *
735     --* by distance from the terminal step(s).                                                     *
736     --*                                                                                            *
737     --* The explosion handles linear, converging, diverging and parallel route sections and works  *
738     --* with any arbitrary routing step dependence topology, with the important exception of loops *
739     --* as this introduces a 'feedback' loop.                                                      *
740     --*                                                                                            *
741     --* Each row retrieved consists of a nested table of step-related data, which itself consists  *
742     --* of several nested tables so that we have a resulting structure as follows:                 *
743     --*                                                                                            *
744     --* Table of steps                                                                             *
745     --*   Each step consists of step_id, step_qty, step_uom, output_qty                            *
746     --*                         table of inherited costs,                                          *
747     --*                         table of current costs                                             *
748     --*                         table of step costs (= inherited + current)                        *
749     --*                         table of material txns                                             *
750     --*                         table of resource txns                                             *
751     --*                         table of steps that follow this step                               *
752     --*                                                                                            *
753     --*   Each inherited cost consists of cost component class, cost analysis code, level and cost *
754     --*                                                                                            *
755     --*   Each current cost has the same format as an inherited cost                               *
756     --*                                                                                            *
757     --*   Each material transaction consists of data from ic_tran_pnd and a cost table which is    *
758     --*   in the same format as the inherited costs                                                *
759     --*                                                                                            *
760     --*   Each resource transaction consists of data from gme_resource_txns and a cost table which *
761     --*   which is in the same format as the inherited costs                                       *
762     --*                                                                                            *
763     --*   Each step dependency consists of inherited step qty, step_qty_uom and the index of the   *
764     --*   dependent step in the structure.                                                         *
765     --*                                                                                            *
766     --*   This entire structure is dumped to the gmf_lot_cost_audit table when a lot's cost has    *
767     --*   been generated.                                                                          *
768     --**********************************************************************************************
769     --* NOTE: The columns in gmf_step_dependencies are a bit ambiguous. The dep_step_id is better  *
770     --* thought of as 'prior_step_id' as it is the one on which the (current) batchstep_id depends *
771     --**********************************************************************************************
772 
773 
774     CURSOR explosion_cursor
775     ( p_batch_id NUMBER )
776     IS
777       SELECT  max(grsd.seq)
778       , SYSTEM.gmf_step_type
779         ( grsd.dep_step_id, gbs.actual_step_qty, gbs.step_qty_um, 0
780         , CAST
781           ( MULTISET
782             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
783               FROM   DUAL
784             ) AS SYSTEM.gmf_cost_tab
785           )
786         , CAST
787           ( MULTISET
788             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
789               FROM   DUAL
790             ) AS SYSTEM.gmf_cost_tab
791           )
792         , CAST
793           ( MULTISET
794             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
795               FROM   DUAL
796             ) AS SYSTEM.gmf_cost_tab
797           )
798         , NULL, NULL
799         , CAST
800           ( MULTISET
801             (
802               SELECT SYSTEM.gmf_dependency_type(a.batchstep_id, b.actual_step_qty, b.step_qty_um, NULL)
803               FROM   gme_batch_step_dependencies a, gme_batch_steps b
804               WHERE  a.batch_id = p_batch_id and a.dep_step_id = grsd.dep_step_id
805               AND    a.batchstep_id = b.batchstep_id
806               AND    a.batch_id = b.batch_id
807             ) AS SYSTEM.gmf_dependency_tab
808           )
809         )
810       FROM
811       (
812         SELECT MAX(level) seq, dep_step_id, batchstep_id
813         FROM   gme_batch_step_dependencies
814         START WITH batch_id = p_batch_id
815         AND   batchstep_id NOT IN (SELECT dep_step_id FROM gme_batch_step_dependencies WHERE batch_id=p_batch_id)
816         CONNECT BY PRIOR dep_step_id = batchstep_id AND batch_id = PRIOR batch_id
817         GROUP BY dep_step_id, batchstep_id
818       ) grsd
819       , gme_batch_steps gbs
820       WHERE gbs.batch_id = p_batch_id
821       AND   gbs.batchstep_id = grsd.dep_step_id
822       GROUP BY  grsd.dep_step_id, gbs.actual_step_qty, gbs.step_qty_um
823       UNION ALL
824       SELECT 0
825       , SYSTEM.gmf_step_type
826         ( g.batchstep_id, g.actual_step_qty, g.step_qty_um, 0
827         , CAST
828           ( MULTISET
829             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
830               FROM   DUAL
831             ) AS SYSTEM.gmf_cost_tab
832           )
833         , CAST
834           ( MULTISET
835             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
836               FROM   DUAL
837             ) AS SYSTEM.gmf_cost_tab
838           )
839         , CAST
840           ( MULTISET
841             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
842               FROM   DUAL
843             ) AS SYSTEM.gmf_cost_tab
844           )
845         , NULL, NULL
846         , CAST
847           ( MULTISET
848             (
849               SELECT SYSTEM.gmf_dependency_type(NULL, NULL, NULL, NULL)
850               FROM   DUAL
851             ) AS SYSTEM.gmf_dependency_tab
852           )
853         )
854       FROM
855       ( SELECT DISTINCT/*Bug 4320765*/ gbsd.batchstep_id, gbs2.actual_step_qty, gbs2.step_qty_um
856         FROM gme_batch_step_dependencies gbsd
857       ,      gme_batch_steps gbs2
858       WHERE  gbsd.batch_id = p_batch_id
859       AND    gbs2.batch_id = p_batch_id
860       AND    gbsd.batchstep_id NOT IN
861              (SELECT dep_step_id from gme_batch_step_dependencies where batch_id = p_batch_id)
862       AND    gbsd.batchstep_id = gbs2.batchstep_id ) g
863       ORDER BY 1 desc;
864     --**********************************************************************************************
865     --                                                                                             *
866     -- This cursor does pretty much the same thing as above, but caters for the situation where    *
867     -- the recipe does not have a routing. We simply create a ficticious step (0) and attach all   *
868     -- materials and their transactions and costs to it. The _nr suffix means 'No Routing'         *
869     --                                                                                             *
870     --**********************************************************************************************
871 
872     CURSOR explosion_cursor_nr
873     IS
874       SELECT 0
875       , SYSTEM.gmf_step_type
876         ( 1, 0, NULL, 0
877         , CAST
878           ( MULTISET
879             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
880               FROM   DUAL
881             ) AS SYSTEM.gmf_cost_tab
882           )
883         , CAST
884           ( MULTISET
885             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
886               FROM   DUAL
887             ) AS SYSTEM.gmf_cost_tab
888           )
889         , CAST
890           ( MULTISET
891             ( SELECT SYSTEM.gmf_cost_type( 0, ' ', 0, 0, 0)
892               FROM   DUAL
893             ) AS SYSTEM.gmf_cost_tab
894           )
895         , NULL, NULL
896         , CAST
897           ( MULTISET
898             (
899               SELECT SYSTEM.gmf_dependency_type(NULL, NULL, NULL, NULL)
900               FROM   DUAL
901             ) AS SYSTEM.gmf_dependency_tab
902           )
903         )
904       FROM DUAL;
905 
906 
907    --**********************************************************************************************
908     --*                                                                                                                                                         *
909     --* Cursor to retrieve step from a SINGLE STEP BATCH which obviously                            *
910     --* will not have any dependency associated                                                                              *
911     --*                                                                                                                                                         *
912     --**********************************************************************************************
913       CURSOR explosion_cursor_ss(p_batch_id NUMBER)
914       IS
915          SELECT 0,
916           SYSTEM.gmf_step_type
917              (gbs.batchstep_id,
918               gbs.actual_step_qty,
919               gbs.step_qty_um,
920               0,
921               CAST (MULTISET (SELECT SYSTEM.gmf_cost_type (0, ' ', 0, 0, 0)
922                                 FROM DUAL) AS SYSTEM.gmf_cost_tab
923                    ),
924               CAST (MULTISET (SELECT SYSTEM.gmf_cost_type (0, ' ', 0, 0, 0)
925                                 FROM DUAL) AS SYSTEM.gmf_cost_tab
926                    ),
927               CAST (MULTISET (SELECT SYSTEM.gmf_cost_type (0, ' ', 0, 0, 0)
928                                 FROM DUAL) AS SYSTEM.gmf_cost_tab
929                    ),
930               NULL,
931               NULL,
932               CAST
933                  (MULTISET (SELECT SYSTEM.gmf_dependency_type (NULL, NULL, NULL, NULL)
934                               FROM DUAL) AS SYSTEM.gmf_dependency_tab
935                  )
936              )
937          FROM gme_batch_steps gbs
938          WHERE gbs.batch_id = p_batch_id  ;
939 
940 
941     --**********************************************************************************************
942     --*                                                                                            *
943     --* Cursor to retrieve steps that have not been set up in a dependency chain                   *
944     --*                                                                                            *
945     --**********************************************************************************************
946 
947     CURSOR steps_cursor (p_batch_id NUMBER)
948     IS
949      SELECT batchstep_no, batchstep_id
950      FROM   gme_batch_steps
951      WHERE  batch_id = p_batch_id
952      ORDER  by batchstep_no;
953 
954     --***************************************************************************************************************
955     --*                                                                                            *
956     --* Cursor to retrieve all material transactions for a given batch step.                       *
957     --*                                                                                            *
958     --* This cursor would ideally be nested inside the above cursor, but SQL syntax does not allow *
959     --* an order by clause in a subquery, and we need to have the transactions ordered by date     *
960     --*                                                                                            *
961     --* umoogala: ic_item_mst.lot_costed_flag does not exist. To work around this loaded all lot   *
962     --* costed item from gmf_lot_costed_items_flag into lc_item_tab. If item exists in this table  *
963     --* then lot_costed_flag is set to 1 otherwise to 0.                                           *
964     --*
965     --* Girish - Bug 4094132 Modified this materials cursor NOT IN clause to add  lot_id and reverse_id
966     --*  Since we are filtering the query using "NOT IN" the product yielded into multiple lots
967     --*  is not costed correctly. So added this condition.
968     --*
969     --* Dinesh Vadivel - Bug 4057323 - Added Cost Allocation Factor value in the select clause
970     --* prasad marada bug 7409599 getting distinct resource costs for the cost component class,
971     --*               analysis code combination, modified cursor resource_cost_cursor
972     --**************************************************************************************************************
973 
974 CURSOR materials_cursor
975     ( p_batch_id        NUMBER
976     , p_batchstep_id    NUMBER
977     )
978     IS
979       SELECT SYSTEM.gmf_matl_type
980              (mmt.transaction_id,
981               hoi.org_information2,
982               mmt.organization_id,
983               mmt.inventory_item_id,
984               mtln.lot_number,
985               gmd.line_type,
986               NVL(mtln.primary_quantity, mmt.primary_quantity), -- B9131983 used NVL
987               iimb.primary_uom_code,
988               mmt.transaction_date,
989 	            decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id, 1, NULL, 0, 0),
990               gmd.contribute_step_qty_ind,
991               0,
992               gmd.plan_qty,
993               gmd.actual_qty,
994               gmd.dtl_um,
995                CAST (MULTISET (SELECT SYSTEM.gmf_cost_type (0, ' ', 0, 0, 0)
996                                 FROM DUAL) AS SYSTEM.gmf_cost_tab
997                    ), -- Bug 7317270,
998               gmd.cost_alloc
999              )
1000       FROM  mtl_system_items_b iimb,
1001             mtl_material_transactions mmt,
1002             gme_batch_step_items gbsi,
1003             gme_material_details gmd,
1004             mtl_transaction_lot_numbers mtln,
1005             mtl_parameters mp,
1006             hr_organization_information hoi,
1007             gme_transaction_pairs gtp
1008       WHERE gbsi.batch_id = p_batch_id
1009       AND   gbsi.batchstep_id = p_batchstep_id
1010       AND   mp.organization_id = mmt.organization_id
1011       AND   hoi.organization_id = mmt.organization_id
1012       AND   hoi.org_information_context = 'Accounting Information'
1013       AND   gbsi.material_detail_id = mmt.trx_source_line_id
1014       AND   mmt.transaction_id = mtln.transaction_id (+)
1015       AND   mmt.transaction_source_type_id = 5  /* Production */
1016       AND   mmt.transaction_quantity   <> 0
1017       AND   mmt.inventory_item_id      = iimb.inventory_item_id
1018       AND   mmt.organization_id        = iimb.organization_id
1019       AND   mmt.transaction_date      <= l_final_run_date
1020       AND   gmd.batch_id               = p_batch_id
1021       AND   gmd.material_detail_id     = gbsi.material_detail_id
1022       AND   mmt.transaction_id = gtp.transaction_id1 (+)
1023       --AND   NOT (mmt.inventory_item_id = transaction_row.inventory_item_id -- Bug 13386258-VC
1024       --           AND mmt.transaction_id  <> transaction_row.transaction_id
1025       --           AND mtln.lot_number = transaction_row.lot_number
1026       --           AND gtp.transaction_id2 IS NOT NULL )
1027        ORDER BY mmt.transaction_date, gmd.line_type,
1028                  DECODE (gmd.line_type,1, DECODE ((  ABS (DECODE (mmt.transaction_quantity, 0, 1, mmt.transaction_quantity))
1029                              / DECODE (mmt.transaction_quantity, 0, 1, mmt.transaction_quantity)
1030                             ),
1031                             1, mmt.transaction_id,
1032                             DECODE (gtp.transaction_id2,
1033                                     NULL, mmt.transaction_id,
1034                                     gtp.transaction_id2 + .5
1035                                    )
1036                            ),
1037                    mmt.transaction_id
1038                    ), mtln.lot_number ;        -- B9131983
1039 
1040     /*HALUTHRA BUG 6165255 changed from mmt.transaction_quantity to mmt.primary_quantity and from
1041 mmt.transaction_uom to iimb.primary_uom_code to avoid dual uom issue*/
1042     /* HALUTHRA BUG 8330088 Added the join condition in mtln case so as to include the records for non lot controlled
1043     items . also changed from mtln.transaction_date to Decode(mtln.transaction_date,null,mmt.transaction_date,mtln.transaction_date)
1044     so that date gets populated in case the item is non lot controlled. */
1045 
1046     CURSOR unassociated_ings_cursor
1047     ( p_batch_id       NUMBER
1048     )
1049     IS
1050       SELECT SYSTEM.gmf_matl_type
1051              ( transaction_id,
1052                org_information2,
1053                organization_id,
1054                inventory_item_id,
1055                lot_number,
1056                line_type,
1057                primary_quantity,
1058                primary_uom_code,
1059                transaction_date,
1060 	             lot_costed_flag,
1061                contribute_step_qty_ind,
1062                0,
1063                plan_qty,
1064                actual_qty,
1065                dtl_um,
1066                 CAST (MULTISET (SELECT SYSTEM.gmf_cost_type (0, ' ', 0, 0, 0)
1067                                 FROM DUAL) AS SYSTEM.gmf_cost_tab
1068                    ), -- Bug 7317270
1069                cost_alloc
1070              )
1071        FROM (
1072        SELECT
1073 	       --      decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id,
1074          --      mmt.transaction_id,
1075                MIN(mmt.transaction_id) as transaction_id,
1076          --      ) as transaction_id,
1077                hoi.org_information2,
1078                mmt.organization_id,
1079                mmt.inventory_item_id,
1080 	             decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id,
1081                mtln.lot_number,
1082                'DUMMY'
1083                ) as lot_number,
1084                gmd.line_type,
1085                SUM( NVL(mtln.primary_quantity, mmt.primary_quantity) ) as primary_quantity, -- B9131983 used NVL
1086                iimb.primary_uom_code, --mmt.transaction_uom,
1087 	             decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id,
1088                Decode(mtln.transaction_date,null,mmt.transaction_date,mtln.transaction_date),
1089                TRUNC(mmt.transaction_date)
1090                ) as transaction_date,
1091 	             decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id, 1, NULL, 0, 0) as lot_costed_flag,
1092                gmd.contribute_step_qty_ind,
1093                0,
1094                gmd.plan_qty,
1095                gmd.actual_qty,
1096                gmd.dtl_um, gmd.cost_alloc
1097       FROM  mtl_system_items_b iimb,
1098             mtl_material_transactions mmt,
1099             gme_material_details gmd,
1100             mtl_transaction_lot_numbers mtln,
1101             mtl_parameters mp,
1102             hr_organization_information hoi
1103       WHERE mmt.transaction_source_type_id = 5
1104       AND   gmd.line_type IN (-1,2)
1105       AND   mtln.transaction_quantity(+) <> 0
1106       AND   mmt.transaction_id = mtln.transaction_id(+)
1107       AND   mmt.organization_id = hoi.organization_id
1108       AND   hoi.org_information_context = 'Accounting Information'
1109       AND   mmt.organization_id = mp.organization_id
1110       AND   iimb.inventory_item_id = mmt.inventory_item_id
1111       AND   iimb.organization_id  = mmt.organization_id
1112       AND   gmd.batch_id = p_batch_id
1113       AND   mmt.trx_source_line_id = gmd.material_detail_id
1114       AND   gmd.material_detail_id NOT IN
1115             (SELECT material_detail_id FROM gme_batch_step_items
1116              WHERE  batch_id = p_batch_id)
1117         GROUP BY
1118 	  --           decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id,
1119     --           mmt.transaction_id,
1120     --           -999999 transaction_id) ,
1121                hoi.org_information2,
1122                mmt.organization_id,
1123                mmt.inventory_item_id,
1124 	             decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id,
1125                mtln.lot_number,
1126                'DUMMY'
1127                )  ,
1128                gmd.line_type,
1129                iimb.primary_uom_code, --mmt.transaction_uom,
1130 	             decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id,
1131                Decode(mtln.transaction_date,null,mmt.transaction_date,mtln.transaction_date),
1132                TRUNC(mmt.transaction_date)
1133                )  ,
1134 	             decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id, 1, NULL, 0, 0),
1135                gmd.contribute_step_qty_ind,
1136                0 ,
1137                gmd.plan_qty,
1138                gmd.actual_qty,
1139                gmd.dtl_um,
1140                gmd.cost_alloc
1141             ) ;
1142 
1143 
1144     /*HALUTHRA BUG 6165255 changed from mmt.transaction_quantity to mmt.primary_quantity and from
1145 mmt.transaction_uom to iimb.primary_uom_code to avoid dual uom issue*/
1146     /* HALUTHRA BUG 8330088 Added the join condition in mtln case so as to include the records for non lot controlled
1147     items . also changed from mtln.transaction_date to Decode(mtln.transaction_date,null,mmt.transaction_date,mtln.transaction_date)
1148     so that date gets populated in case the item is non lot controlled. */
1149 
1150      CURSOR unassociated_prds_cursor
1151     ( p_batch_id       NUMBER
1152     )
1153     IS
1154       SELECT SYSTEM.gmf_matl_type
1155              ( mmt.transaction_id,
1156                hoi.org_information2,
1157                mmt.organization_id,
1158                mmt.inventory_item_id,
1159                mtln.lot_number,
1160                gmd.line_type,
1161                NVL(mtln.primary_quantity, mmt.primary_quantity), -- B9131983 used NVL
1162                iimb.primary_uom_code, --mmt.transaction_uom,
1163                Decode(mtln.transaction_date,null,mmt.transaction_date,mtln.transaction_date),--mtln.transaction_date,
1164 	             decode(is_item_lot_costed(iimb.organization_id,iimb.inventory_item_id), iimb.inventory_item_id, 1, NULL, 0, 0),
1165                gmd.contribute_step_qty_ind,
1166                0,
1167                gmd.plan_qty,
1168                gmd.actual_qty,
1169                gmd.dtl_um,
1170                   CAST (MULTISET (SELECT SYSTEM.gmf_cost_type (0, ' ', 0, 0, 0)
1171                                 FROM DUAL) AS SYSTEM.gmf_cost_tab
1172                    ), -- Bug 7317270
1173                gmd.cost_alloc
1174              )
1175       FROM  mtl_system_items_b iimb,
1176             mtl_material_transactions mmt,
1177             gme_material_details gmd,
1178             mtl_transaction_lot_numbers mtln,
1179             hr_organization_information hoi
1180       WHERE mmt.transaction_source_type_id = 5
1181       AND   gmd.line_type = 1
1182       AND   mtln.transaction_quantity(+) <> 0
1183       AND   mmt.transaction_id = mtln.transaction_id(+)
1184       AND   mmt.organization_id = hoi.organization_id
1185       AND   hoi.org_information_context = 'Accounting Information'
1186       AND   iimb.inventory_item_id = mmt.inventory_item_id
1187       AND   iimb.organization_id  = mmt.organization_id
1188       AND   gmd.batch_id = p_batch_id
1189       AND   mmt.trx_source_line_id = gmd.material_detail_id
1190       AND   gmd.material_detail_id NOT IN
1191             (SELECT material_detail_id FROM gme_batch_step_items
1192              WHERE  batch_id = p_batch_id);
1193 
1194 
1195 CURSOR materials_cursor_nr
1196     ( p_batch_id        NUMBER
1197     )
1198     IS
1199       SELECT SYSTEM.gmf_matl_type
1200              ( mmt.transaction_id,
1201                l_le_id,
1202                mmt.organization_id,
1203                mmt.inventory_item_id,
1204                mtln.lot_number,
1205                gme.line_type,
1206                NVL(mtln.primary_quantity, mmt.primary_quantity), -- B9131983 used NVL
1207                iimb.primary_uom_code,
1208                mmt.transaction_date,
1209 	             decode(is_item_lot_costed(mmt.organization_id,iimb.inventory_item_id), iimb.inventory_item_id, 1, NULL, 0, 0),
1210                gme.contribute_step_qty_ind,
1211                0,
1212                gme.plan_qty,
1213                gme.actual_qty,
1214                gme.dtl_um,
1215                   CAST (MULTISET (SELECT SYSTEM.gmf_cost_type (0, ' ', 0, 0, 0)
1216                                 FROM DUAL) AS SYSTEM.gmf_cost_tab
1217                    ), -- Bug 7317270
1218                gme.cost_alloc
1219              )
1220       FROM  mtl_system_items_b iimb,
1221             mtl_material_transactions mmt,
1222             gme_material_details gme,
1223             mtl_transaction_lot_numbers mtln,
1224             gme_transaction_pairs gtp
1225       WHERE mmt.trx_source_line_id = gme.material_detail_id
1226            AND   mmt.transaction_source_type_id = 5  /* Rajesh B8290451 */
1227            AND   mmt.transaction_source_id=gme.batch_id-- added by Francisco 23 Feb 2009
1228            AND   gme.batch_id = p_batch_id
1229            AND   mmt.transaction_quantity <> 0
1230            AND   mmt.inventory_item_id = iimb.inventory_item_id
1231            AND   mmt.organization_id = iimb.organization_id
1232            AND   mmt.transaction_date <= l_final_run_date
1233            AND   mmt.transaction_id = mtln.transaction_id (+)
1234            AND   mmt.transaction_id = gtp.transaction_id1 (+)
1235            AND   gtp.batch_id (+) = p_batch_id
1236            --AND   NOT (mmt.inventory_item_id = transaction_row.inventory_item_id -- Bug 13386258-VC
1237            --      AND gme.line_type = transaction_row.line_type
1238            --      AND mmt.transaction_id  <> transaction_row.transaction_id
1239            --      AND mtln.lot_number = transaction_row.lot_number
1240 	--	            AND gtp.transaction_id2 IS NOT NULL )
1241       ORDER BY mmt.transaction_date, gme.line_type,
1242                    DECODE (gme.line_type,
1243                    1, DECODE ((  ABS (DECODE (mmt.transaction_quantity, 0, 1, mmt.transaction_quantity))
1244                              / DECODE (mmt.transaction_quantity, 0, 1, mmt.transaction_quantity)
1245                             ),
1246                             1, mmt.transaction_id,
1247                             DECODE (gtp.transaction_id2,
1248                                     NULL, mmt.transaction_id,
1249                                     gtp.transaction_id2 + .5
1250                                    )
1251                            ),
1252                    mmt.transaction_id
1253                    ), mtln.lot_number;       -- B9131983
1254 
1255 
1256     --**********************************************************************************************
1257     --*                                                                                            *
1258     --* Cursor to retrieve all resource transactions for a given batch step.                       *
1259     --*                                                                                            *
1260     --* This cursor should also be nested inside the above cursor, but SQL syntax does not allow   *
1261     --* an order by clause in a subquery, and we need to have the transactions ordered by date     *
1262     --*                                                                                            *
1263     --**********************************************************************************************
1264 
1265 
1266    CURSOR resources_cursor
1267     ( p_batch_id         NUMBER
1268     , p_batchstep_id     NUMBER
1269     )
1270     IS
1271       SELECT SYSTEM.gmf_rsrc_type
1272              ( grt.poc_trans_id,
1273                grt.organization_id,
1274                grt.resources,
1275                grt.resource_usage,
1276                grt.trans_qty_um,
1277                grt.trans_date,
1278                0,
1279                   CAST (MULTISET (SELECT SYSTEM.gmf_cost_type (0, ' ', 0, 0, 0)
1280                                 FROM DUAL) AS SYSTEM.gmf_cost_tab
1281                    ) -- Bug 7317270
1282              )
1283       FROM   gme_resource_txns grt,
1284              gme_batch_step_resources gbsr
1285       WHERE  gbsr.batch_id = p_batch_id
1286       AND    gbsr.batchstep_id = p_batchstep_id
1287       AND    gbsr.batchstep_resource_id = grt.line_id
1288       AND    grt.doc_type = 'PROD'
1289       AND    grt.doc_id = p_batch_id
1290       AND    grt.completed_ind = 1
1291       AND    grt.resource_usage <> 0
1292       ORDER BY grt.trans_date;
1293 
1294     --**********************************************************************************************
1295     --*                                                                                            *
1296     --* Cursors to retrieve the cost of a lot in the organization specified.                          *
1297     --*                                                                                            *
1298     --**********************************************************************************************
1299 
1300 -- Bug 13038249 Grupo viz changes starts
1301 
1302  CURSOR lot_cost_cursor_1
1303     ( p_orgn_id     NUMBER
1304     , p_item_id     NUMBER
1305     , p_lot_number  VARCHAR2
1306     , p_trans_date DATE
1307     , p_cost_type_id NUMBER
1308     )
1309     IS
1310       SELECT *
1311       FROM  gmf_lot_costs glc
1312       WHERE glc.lot_number        = p_lot_number
1313       AND   glc.inventory_item_id = p_item_id
1314       AND   glc.organization_id   = p_orgn_id
1315       AND   glc.cost_type_id      = p_cost_type_id
1316       AND   glc.cost_date >= sysdate-60
1317       AND   glc.cost_date        <= NVL(p_trans_date, glc.cost_date)
1318       ORDER BY header_id desc
1319     ;
1320 -- Bug 13038249 Grupo viz changes Ends
1321 
1322  CURSOR lot_cost_cursor
1323     ( p_orgn_id     NUMBER
1324     , p_item_id     NUMBER
1325     , p_lot_number  VARCHAR2
1326     , p_trans_date DATE
1327     , p_cost_type_id NUMBER
1328     )
1329     IS
1330       SELECT *
1331       FROM  gmf_lot_costs glc
1332       WHERE glc.lot_number        = p_lot_number
1333       AND   glc.inventory_item_id = p_item_id
1334       AND   glc.organization_id   = p_orgn_id
1335       AND   glc.cost_type_id      = p_cost_type_id
1336       AND   glc.cost_date        <= NVL(p_trans_date, glc.cost_date)
1337       ORDER BY header_id desc
1338     ;
1339 
1340     CURSOR lot_cost_detail_cursor
1341     ( p_header_id  NUMBER )
1342     IS
1343       SELECT SYSTEM.gmf_cost_type
1344              ( glcd.cost_cmpntcls_id,
1345                glcd.cost_analysis_code,
1346                glcd.cost_level,
1347                glcd.component_cost,
1348                0    -- B9131983 Burden_ind
1349              )
1350       FROM  gmf_lot_cost_details glcd
1351       WHERE glcd.header_id = p_header_id;
1352 
1353     --**********************************************************************************************
1354     --*                                                                                            *
1355     --* Cursors to retrieve the standard cost of an item in the organization specified.               *
1356     --* umoogala: passing co_code instead of calendar_code. Also, joined with cldr_hdr.            *
1357     --* Dinesh Vadivel - Bug 4320765 - Modified item_cost_cursor to support warehouse association
1358     --*   functionality for the Alternate Cost Method
1359     --* Hari Luthra - Bug 8533290/5473138 Modified item_cost_cursor and item_cost_detail_cursor to avoid issues in case of
1360     --* multiple cost warehouse assosciation. Added an outer join with cstw.eff_start_date and
1361     --* cstw.eff_end_date as well.
1362     --**********************************************************************************************
1363 
1364  CURSOR item_cost_cursor
1365     ( p_le_id	    	 NUMBER
1366     , p_cost_type_id NUMBER
1367     , p_orgn_id      NUMBER
1368     , p_item_id      VARCHAR2
1369     , p_date         DATE
1370     )
1371     IS
1372       SELECT sum(cst.cmpnt_cost)
1373       FROM   cm_cmpt_dtl cst,
1374              gmf_period_statuses gps
1375       WHERE  gps.legal_entity_id  = p_le_id
1376       AND    gps.cost_type_id     = p_cost_type_id
1377       AND    gps.start_date      <= p_date
1378       AND    gps.end_date        >= p_date
1379       AND    gps.period_id        = cst.period_id
1380       AND    cst.organization_id  = (SELECT NVL (cstw.cost_organization_id, invw.organization_id)
1381                                                  FROM cm_whse_asc cstw, mtl_parameters invw
1382                                                  WHERE cstw.organization_id(+) = invw.organization_id
1383                                                     AND invw.organization_id = p_orgn_id
1384                                                     AND NVL(cstw.eff_start_date(+),p_date) <= p_date -- HALUTHRA Bug 5473138/8533290
1385                                                     AND NVL(cstw.eff_end_date(+),p_date) >= p_date   -- HALTURHA Bug 5473138/8533290
1386                                                     AND cstw.delete_mark (+) = 0)
1387       AND    cst.inventory_item_id = p_item_id
1388       AND    cst.delete_mark = 0
1389       AND    gps.delete_mark = 0;
1390 
1391 
1392  -- Bug 8867177  rollup all costs at this level
1393 CURSOR item_cost_detail_cursor
1394     ( p_le_id		      NUMBER
1395     , p_cost_type_id  NUMBER
1396     , p_orgn_id       NUMBER
1397     , p_item_id       VARCHAR2
1398     , p_date             DATE
1399     , p_period_id     NUMBER
1400     )
1401     IS
1402       SELECT SYSTEM.gmf_cost_type
1403              (cstdtl.cost_cmpntcls_id,
1404               cstdtl.cost_analysis_code,
1405               cstdtl.cost_level,
1406               cstdtl.cmpnt_cost,
1407               0
1408              ) ,
1409              SUM(NVL(cstdtl.cmpnt_cost,0)) OVER (PARTITION BY cost_level ) total_cost   /* Groupo */
1410 	  FROM
1411  	       (SELECT cst.cost_cmpntcls_id,
1412  	               cst.cost_analysis_code,
1413  	               0 cost_level,
1414  	               SUM(cst.cmpnt_cost) cmpnt_cost
1415       FROM   cm_cmpt_dtl cst
1416       WHERE  cst.cost_type_id = p_cost_type_id
1417       AND    cst.period_id = p_period_id
1418       AND    cst.organization_id =
1419                         (SELECT NVL (cstw.cost_organization_id, invw.organization_id)
1420                                                 FROM cm_whse_asc cstw, mtl_parameters invw
1421                                                 WHERE cstw.organization_id(+) = invw.organization_id
1422                                                   AND invw.organization_id = p_orgn_id
1423                                                   AND NVL(cstw.eff_start_date(+),p_date) <= p_date -- HALUTHRA Bug 5473138/8533290
1424                                                   AND NVL(cstw.eff_end_date(+),p_date) >= p_date -- HALUTHRA Bug 5473138/8533290
1425                                                   AND cstw.delete_mark (+) = 0)
1426       AND    cst.inventory_item_id = p_item_id
1427       AND    cst.delete_mark = 0
1428 	  Group by cst.cost_cmpntcls_id, cst.cost_analysis_code ) cstdtl
1429     ;
1430 
1431  -- Bug 8867177  rollup all costs at this level
1432 CURSOR item_cost_detail_cursor1
1433     ( p_le_id		    NUMBER
1434     , p_cost_type_id NUMBER
1435     , p_orgn_id      NUMBER
1436     , p_item_id          VARCHAR2
1437     , p_date             DATE
1438     )
1439     IS
1440       SELECT SYSTEM.gmf_cost_type
1441              (cstdtl.cost_cmpntcls_id,
1442               cstdtl.cost_analysis_code,
1443               cstdtl.cost_level,
1444               cstdtl.cmpnt_cost,
1445               0
1446              )
1447 	  FROM
1448  	       (SELECT cst.cost_cmpntcls_id,
1449  	               cst.cost_analysis_code,
1450  	               0 cost_level,
1451  	               SUM(cst.cmpnt_cost) cmpnt_cost
1452       FROM   cm_cmpt_dtl cst,
1453              gmf_period_statuses gps
1454       WHERE  gps.legal_entity_id = p_le_id
1455        AND   gps.cost_type_id = p_cost_type_id
1456        AND   gps.start_date <= p_date
1457       AND    gps.end_date >= p_date
1458       AND    cst.cost_type_id = p_cost_type_id
1459       AND    gps.period_id = cst.period_id
1460       AND    cst.organization_id =
1461                         (SELECT NVL (cstw.cost_organization_id, invw.organization_id)
1462                                                 FROM cm_whse_asc cstw, mtl_parameters invw
1463                                                 WHERE cstw.organization_id(+) = invw.organization_id
1464                                                   AND invw.organization_id = p_orgn_id
1465                                                   AND NVL(cstw.eff_start_date(+),p_date) <= p_date -- HALUTHRA Bug 5473138/8533290
1466                                                   AND NVL(cstw.eff_end_date(+),p_date) >= p_date -- HALUTHRA Bug 5473138/8533290
1467                                                   AND cstw.delete_mark (+) = 0)
1468       AND    cst.inventory_item_id = p_item_id
1469       AND    cst.delete_mark = 0
1470       AND    gps.delete_mark = 0
1471 	  Group by cst.cost_cmpntcls_id, cst.cost_analysis_code ) cstdtl
1472     ;
1473     --**********************************************************************************************
1474     --*                                                                                            *
1475     --* Cursor to retrieve the cost of a resource.                                                 *
1476     --* umoogala: passing co_code instead of calendar_code. Also, joined with cldr_hdr.            *
1477     --*                                                                                            *
1478     --**********************************************************************************************
1479 
1480     -- Bug 7409599 added distinct below
1481 
1482    CURSOR resource_cost_cursor
1483     ( p_le_id         NUMBER
1484     , p_cost_type_id   NUMBER
1485     , p_resources      VARCHAR2
1486     , p_orgn_id        NUMBER
1487     , p_date             DATE
1488     , p_batch_id         NUMBER
1489     , p_batchstep_id     NUMBER
1490     )
1491     IS
1492     SELECT  SYSTEM.gmf_cost_type
1493                (gct.cost_cmpntcls_id,
1494                 gct.cost_analysis_code,
1495                 0,
1496                 gct.nominal_cost,
1497                 0)
1498     FROM (SELECT DISTINCT gbsr.cost_cmpntcls_id,  --used the distinct bug 7409599, pmarada
1499                 gbsr.cost_analysis_code,
1500                 0,
1501                 cst.nominal_cost,
1502                 0
1503           FROM  cm_rsrc_dtl cst
1504                ,gmf_period_statuses gps
1505                ,gme_batch_step_resources gbsr
1506          WHERE  gps.legal_entity_id = p_le_id
1507            AND    gps.cost_type_id = p_cost_type_id
1508            AND    gps.start_date <= p_date
1509            AND    gps.end_date >= p_date
1510            AND    cst.period_id = gps.period_id
1511            AND    cst.organization_id = p_orgn_id
1512            AND    cst.resources = p_resources
1513            AND    cst.cost_type_id = p_cost_type_id -- Bug 9686215 12.0 FP for 9646416
1514            AND    cst.delete_mark = 0
1515            AND    gps.delete_mark = 0
1516            AND    gbsr.batch_id = p_batch_id
1517            AND    gbsr.batchstep_id = p_batchstep_id
1518            AND    gbsr.resources = p_resources
1519          ) gct ;
1520 
1521     --**********************************************************************************************
1522     --*                                                                                            *
1523     --* Cursor to retrieve the default cost component class and analysis code for a received item  *
1524     --* umoogala: replaced itemcost_class with cost_category_id 				   *
1525     --*                                                                                            *
1526     --* Bug 3388699: Added the IS NULL and date clauses for retrieval using company                *
1527     --**********************************************************************************************
1528 
1529     CURSOR component_class_cursor
1530     ( p_le_id NUMBER
1531      ,p_item_id    NUMBER
1532      ,p_orgn_id     NUMBER
1533      ,p_date       DATE
1534     )
1535     IS
1536       SELECT a.mtl_cmpntcls_id,
1537              a.mtl_analysis_code, 1
1538         FROM cm_cmpt_mtl a
1539       WHERE (legal_entity_id = p_le_id OR legal_entity_id IS NULL)
1540         AND inventory_item_id = p_item_id
1541         AND (organization_id = p_orgn_id OR organization_id IS NULL)
1542         AND p_date BETWEEN eff_start_date AND eff_end_date
1543         AND a.delete_mark = 0
1544       UNION
1545       SELECT b.mtl_cmpntcls_id, b.mtl_analysis_code, 2
1546       FROM cm_cmpt_mtl b
1547       WHERE (legal_entity_id = p_le_id OR legal_entity_id IS NULL)
1548         AND (organization_id = p_orgn_id OR organization_id IS NULL)
1549         AND p_date BETWEEN eff_start_date AND eff_end_date
1550         AND delete_mark = 0
1551         AND cost_category_id IN
1552             ( SELECT category_id -- cost_category_id  Bug#7306720
1553                 FROM mtl_item_categories mic,
1554                      gmf_process_organizations_gt gpo
1555               WHERE  mic.inventory_item_id = p_item_id
1556                    AND mic.organization_id = gpo.organization_id
1557                    AND (mic.organization_id = p_orgn_id OR p_orgn_id IS NULL)
1558             )
1559       UNION
1560       SELECT d.mtl_cmpntcls_id, d.mtl_analysis_code, 3
1561       FROM gmf_fiscal_policies d
1562       WHERE d.legal_entity_id = p_le_id
1563       ORDER BY 3;
1564 
1565     -- Cursor to retrieve item-specific and lot-specific burdens. If burdens are defined at
1566     -- at both levels the lot-specific ones take priority. The cursor might need a bit of
1567     -- explaining. The first select (the first branch of the union) selects lot-specific
1568     -- burdens. The second branch of the union first finds all burdens that are item specific
1569     -- that do not have a matching one that is lot-specific. In this context 'matching' means
1570     -- that the two burdens have the same resource, cost analysis code and cost component class ID.
1571 
1572     -- The result is the set of burdens (which could easily be empty) that needs to be incorporated
1573     -- in the lot cost.
1574 
1575     -- Not that this partially repeals some changes made for bug 3388974
1576 
1577     -- Bug 3388974-2 This cursor reinstated.
1578 
1579      CURSOR burdens_cursor
1580     ( p_item_id      NUMBER
1581     , p_orgn_id      NUMBER
1582     , p_lot_number   VARCHAR2
1583     , p_cost_type_id NUMBER
1584     , p_trans_date   DATE
1585     )
1586     IS
1587         SELECT lot_burden_line_id,
1588                resources,
1589                cost_cmpntcls_id,
1590                cost_analysis_code,
1591                burden_factor,
1592                0
1593         FROM   gmf_lot_cost_burdens
1594         WHERE  inventory_item_id = p_item_id
1595         AND    organization_id = p_orgn_id
1596         AND    lot_number = p_lot_number
1597         AND    cost_type_id = p_cost_type_id
1598         AND    delete_mark = 0
1599         AND    start_date <= p_trans_date
1600         AND    nvl(end_date, p_trans_date) >= p_trans_date
1601       UNION
1602         SELECT lot_burden_line_id,
1603                resources,
1604                cost_cmpntcls_id,
1605                cost_analysis_code,
1606                burden_factor, 0
1607       	FROM   gmf_lot_cost_burdens
1608       	WHERE  inventory_item_id = p_item_id
1609       	AND    organization_id = p_orgn_id
1610       	AND    lot_number IS NULL
1611       	AND    cost_type_id = p_cost_type_id
1612       	AND    delete_mark = 0
1613       	AND    start_date <= p_trans_date
1614       	AND    nvl(end_date, p_trans_date) >= p_trans_date
1615       	AND    (resources, cost_cmpntcls_id, cost_analysis_code)
1616                NOT IN
1617                  (SELECT resources, cost_cmpntcls_id, cost_analysis_code
1618                   FROM   gmf_lot_cost_burdens
1619                   WHERE  inventory_item_id = p_item_id
1620                   AND    organization_id = p_orgn_id
1621                   AND    lot_number = p_lot_number
1622                   AND    cost_type_id = p_cost_type_id
1623                   AND    delete_mark = 0
1624       	          AND    start_date <= p_trans_date
1625       	          AND    nvl(end_date, p_trans_date) >= p_trans_date
1626                  )
1627      ORDER BY 3,4 ;
1628     -- End 3388974-2
1629 
1630     --**********************************************************************************************
1631     --*                                                                                            *
1632     --* Cursor to retrieve the cost of a resource.                                                 *
1633     --* umoogala: passing co_code instead of calendar_code. Also, joined with cldr_hdr.            *
1634     --*                                                                                            *
1635     --**********************************************************************************************
1636 
1637 
1638     CURSOR burden_cost_cursor
1639     ( p_le_id	             NUMBER
1640     , p_cost_type_id       NUMBER
1641     , p_resources          VARCHAR2
1642     , p_orgn_id            NUMBER
1643     , p_cost_cmpntcls_id   NUMBER
1644     , p_cost_analysis_code VARCHAR2
1645     , p_date               DATE
1646     )
1647     IS
1648       SELECT cst.nominal_cost
1649       FROM   cm_rsrc_dtl cst,
1650              gmf_period_statuses gps
1651       WHERE  gps.legal_entity_id = p_le_id
1652       AND    gps.cost_type_id = p_cost_type_id
1653       AND    gps.start_date <= p_date
1654       AND    gps.end_date >= p_date
1655       AND    cst.period_id = gps.period_id
1656       AND    cst.resources = p_resources
1657       AND    cst.organization_id = p_orgn_id
1658       AND    cst.delete_mark = 0
1659       AND    gps.delete_mark = 0;
1660 
1661 
1662     CURSOR lot_costed_items
1663       IS
1664       SELECT g.inventory_item_id,g.organization_id
1665       FROM  gmf_lot_costed_items_gt g
1666       ORDER BY organization_id,inventory_item_id
1667      ;
1668 
1669 
1670     --**********************************************************************************************
1671     --*      Cursor to get the Batch Step ID which has Step Material Associations
1672     --*
1673     --**********************************************************************************************
1674     CURSOR associated_batch_steps_cursor(p_batch_id NUMBER)
1675     IS
1676     SELECT DISTINCT gbsi.batchstep_id
1677        FROM gme_material_details gmd, gme_batch_step_items gbsi
1678      WHERE gmd.material_detail_id = gbsi.material_detail_id
1679            AND gmd.batch_id = gbsi.batch_id
1680            AND gmd.line_type = 1
1681            AND gmd.batch_id = p_batch_id;
1682 
1683 PROCEDURE Incremental_Lot_costing ( return_code OUT NOCOPY VARCHAR2) ;
1684 
1685 PROCEDURE copy_uncostable_record ;
1686 
1687 PROCEDURE copy_to_transrow ;
1688 
1689 PROCEDURE organization_item_code
1690   ( p_orgn_id   IN NUMBER,
1691     p_item_id   IN NUMBER,
1692     p_orgn_code OUT NOCOPY VARCHAR2,
1693     p_item_no   OUT NOCOPY VARCHAR2
1694   );
1695 
1696 /* Bug 13386258-VC */
1697 PROCEDURE display_c_batch_details
1698      ( p_batch_id	NUMBER
1699      )   ;
1700 
1701     --**********************************************************************************************
1702     --*                                                                                            *
1703     --* Function to check whether item is lot costed or not. This fucntion is used in cursor       *
1704     --* materials_cursor.									                                                         *
1705     --*                                                                                            *
1706     --* Bug 9212497 - OPM LOT ACTUAL COSTING PERFORMANCE ISSUE                                     *
1707     --**********************************************************************************************
1708 /* INVCONV sschinch modified to add orgn id as additional parameter*/
1709 FUNCTION is_item_lot_costed1
1710 ( p_orgn_id IN NUMBER,
1711   p_item_id IN NUMBER
1712 )
1713 RETURN NUMBER
1714 IS
1715 BEGIN
1716    RETURN lc_items_tab(p_item_id||'-'||p_orgn_id);
1717 END;
1718 
1719     --**********************************************************************************************
1720     --*                                                                                            *
1721     --* Function to check whether item is present in _GT table, if not get it through query        *
1722     --*                                                                                            *
1723     --* Bug 9212497 - OPM LOT ACTUAL COSTING PERFORMANCE ISSUE                                     *
1724     --**********************************************************************************************
1725 FUNCTION is_item_lot_costed
1726 ( p_orgn_id IN NUMBER,
1727   p_item_id IN NUMBER
1728 )
1729 RETURN NUMBER
1730 IS
1731 l_inv_id NUMBER;
1732 BEGIN
1733 
1734      IF lc_items_tab.EXISTS(p_item_id||'-'||p_orgn_id) THEN
1735         l_inv_id:= p_item_id ;
1736      ELSE
1737         l_inv_id := 0;
1738      END IF;
1739 
1740  --  fnd_file.put_line(fnd_file.log,'Item id is ' || l_inv_id||'-'||p_orgn_id );
1741 
1742  IF l_inv_id = 0 AND l_item_id IS NOT NULL THEN
1743       SELECT distinct inventory_item_id into l_inv_id FROM
1744       (
1745        SELECT
1746            msi.inventory_item_id
1747       FROM gmf_lot_costed_items lci,
1748            mtl_system_items_b msi
1749       WHERE lci.legal_entity_id = l_le_id
1750         AND lci.delete_mark = 0 /* ANTHIYAG Bug#5279681 06-Jun-2006 */
1751         AND msi.lot_control_code = 2
1752         AND lci.inventory_item_id = msi.inventory_item_id
1753         AND msi.inventory_asset_flag = 'Y'
1754         AND msi.process_costing_enabled_flag = 'Y'
1755         AND lci.cost_type_id = l_cost_type_id
1756         AND msi.inventory_item_id = p_item_id
1757         AND msi.organization_id = p_orgn_id
1758      UNION
1759       SELECT msi.inventory_item_id
1760       FROM mtl_item_categories mic,
1761            gmf_lot_costed_items g,
1762            mtl_system_items_b msi
1763      WHERE g.cost_category_id = mic.category_id
1764           AND g.legal_entity_id = l_le_id
1765           AND g.delete_mark = 0
1766           AND msi.lot_control_code = 2
1767           AND msi.organization_id = mic.organization_id
1768           AND mic.inventory_item_id = msi.inventory_item_id
1769           AND msi.inventory_asset_flag = 'Y'
1770           AND msi.process_costing_enabled_flag = 'Y'
1771           AND g.cost_type_id = l_cost_type_id
1772           AND msi.inventory_item_id = p_item_id
1773           AND msi.organization_id = p_orgn_id  )  ;
1774 
1775    --  fnd_file.put_line(fnd_file.log,'After Query in is_item_lot_costed ' || l_inv_id ||'--' ||p_item_id||'-'||p_orgn_id);
1776    -- insert into table
1777 	 lc_items_tab(p_item_id||'-'||p_orgn_id) := p_item_id;
1778 
1779   END IF;
1780 
1781 
1782    -- Return the value
1783    RETURN l_inv_id;
1784 
1785 EXCEPTION
1786   WHEN NO_DATA_FOUND THEN
1787    IF l_debug_level >= l_debug_level_medium THEN
1788       fnd_file.put_line(fnd_file.log,'Item is not lot costed ' || p_item_id||'-'||p_orgn_id );
1789    END IF;
1790     RETURN null;
1791   WHEN OTHERS THEN
1792     fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in is_item_lot_costed');
1793     RETURN null;
1794 END;
1795 
1796 /*=========================================================
1797   PROCEDURE : ReLoad_Lot_Costed_Items_gt
1798 
1799   DESCRIPTION
1800     This procedure loads global temporary tables for lot costed process
1801     organizations items.
1802   AUTHOR : Rajesh Patangya
1803 
1804   HISTORY
1805     When the process is run only for product item, then Lot_Costed_Items_gt table
1806     have only product items, this does not contain ingradient items in
1807     Lot_Costed_Items_gt table. When material cursor try to find the costed_flag for
1808     ingradients, it does not find it and further process assumes that the ingradient is
1809     not lot costed.
1810     1. Delete Lot_Costed_Items_gt table.
1811     2. Load all the lot costed items in Lot_Costed_Items_gt table.
1812     3. Reindex the array.
1813  ==========================================================*/
1814 
1815  PROCEDURE ReLoad_Lot_Costed_Items_gt(p_le_id        IN NUMBER,
1816                                    x_return_status OUT NOCOPY NUMBER
1817                                    ) IS
1818 --    l_le_id NUMBER;  B 8687115 already declared global Not used.
1819    procedure_name VARCHAR2(100);
1820  BEGIN
1821    procedure_name := 'Reload Lot Costed Items GT';
1822    IF l_debug_level >= l_debug_level_medium THEN
1823        fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
1824      END IF;
1825 
1826    DELETE FROM GMF_LOT_COSTED_ITEMS_GT ;
1827 
1828 -- Bug 8730374 Modified the insert to load items assigned by category as well
1829 
1830    INSERT
1831       INTO GMF_LOT_COSTED_ITEMS_GT
1832       (
1833           organization_id,
1834           inventory_item_id,
1835           primary_uom_code
1836       )
1837       SELECT
1838            msi.organization_id,
1839            msi.inventory_item_id,
1840            msi.primary_uom_code
1841       FROM gmf_lot_costed_items lci,
1842            mtl_system_items_b msi,
1843            gmf_process_organizations_gt gpo
1844       WHERE lci.legal_entity_id = l_le_id
1845         AND lci.delete_mark = 0 /* ANTHIYAG Bug#5279681 06-Jun-2006 */
1846         AND gpo.organization_id = msi.organization_id
1847         AND msi.lot_control_code = 2
1848         AND lci.inventory_item_id = msi.inventory_item_id
1849         AND msi.inventory_asset_flag = 'Y'
1850         AND msi.process_costing_enabled_flag = 'Y'
1851         AND lci.cost_type_id = l_cost_type_id
1852     UNION
1853       SELECT
1854         mic.organization_id,    /*ANTHIYAG Bug#5279681 06-Jun-2006 */
1855         mic.inventory_item_id,  /*ANTHIYAG Bug#5279681 06-Jun-2006 */
1856         i.primary_uom_code
1857       FROM mtl_item_categories mic,
1858            gmf_lot_costed_items g,
1859            mtl_system_items_b i,
1860            gmf_process_organizations_gt gpo
1861      WHERE g.cost_category_id = mic.category_id
1862           AND g.legal_entity_id = l_le_id
1863           AND g.delete_mark = 0
1864           AND i.lot_control_code = 2
1865           AND gpo.organization_id = i.organization_id
1866           AND i.organization_id = mic.organization_id
1867           AND mic.inventory_item_id = i.inventory_item_id
1868           AND i.inventory_asset_flag = 'Y'
1869           AND i.process_costing_enabled_flag = 'Y'
1870           AND g.cost_type_id = l_cost_type_id;
1871 
1872     /* Build index for organization id and item id*/
1873       FOR Cur_lc_items IN lot_costed_items
1874       LOOP
1875 	 lc_items_tab(Cur_lc_items.inventory_item_id||'-'||cur_lc_items.organization_id) := Cur_lc_items.inventory_item_id;
1876       END LOOP;
1877 
1878     x_return_status := 0;
1879     IF l_debug_level >= l_debug_level_medium THEN
1880        fnd_file.put_line
1881        (fnd_file.log,'Leaving Procedure: '||procedure_name);
1882      END IF;
1883 
1884   EXCEPTION
1885     WHEN OTHERS THEN
1886      fnd_file.put_line
1887      (fnd_file.log,'Procedure failed: '||procedure_name);
1888       x_return_status := -1;
1889 END;
1890 
1891 /**********************************************************************************************
1892  *                                                                                            *
1893  * Procedures to merge a table of costs with the new_cost_tab. If mode = 'C' (= Combine) any  *
1894  * matches between the costs_table and new_cost_tab are simply added together. If mode = 'A'  *
1895  * (= Average) the cost_qty and new_qty are used to perform weighted averaging. The result is *
1896  * placed in new_cost_tab in both modes, and new_cost.unit_cost is the sum its costs.         *
1897  *                                                                                            *
1898  * Combining costs is used for adding burdens or acquisition costs to a new cost before it is *
1899  * either written to the database or used to update an existing cost. The update will use the *
1900  * Averaging mode.                                                                            *
1901  * History                                                                                    *
1902  * LCMOPM Dev 4-Aug-2009 LCM-OPM Integration, bug 8642337 Added new merge mode V for          *
1903  *        value adjustment. multiply the component cost by 1 and add to unit cost             *
1904 --**********************************************************************************************/
1905 
1906 PROCEDURE merge_costs
1907 ( costs_table     IN OUT NOCOPY l_cost_tab_type
1908 , cost_qty        IN NUMBER
1909 , new_qty         IN NUMBER
1910 , merge_mode      IN VARCHAR2
1911 )
1912 IS
1913   k              NUMBER;
1914   l              NUMBER;
1915   divisor        NUMBER;
1916   l_cost_accrued BOOLEAN := FALSE;
1917   procedure_name VARCHAR2(100);
1918 BEGIN
1919 
1920   procedure_name := 'Merge Costs';
1921 
1922   IF l_debug_level >= l_debug_level_medium THEN
1923        fnd_file.put_line
1924        (fnd_file.log,'Entered Procedure: '||procedure_name);
1925   END IF;
1926 
1927   IF l_debug_level >= l_debug_level_high THEN
1928     fnd_file.put_line(fnd_file.log,'Cost_qty = '||cost_qty||', new_qty = '||new_qty);
1929 
1930     fnd_file.put_line(fnd_file.log,'Before merge new_cost_tab is:');
1931 
1932     FOR k IN 1 .. new_cost_tab.COUNT
1933     LOOP
1934       fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
1935       fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
1936       fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||new_cost_tab(k).cost_level);
1937       fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
1938       fnd_file.put_line(fnd_file.log,'====================================');
1939     END LOOP;
1940 
1941     IF costs_table.EXISTS(1) THEN
1942       fnd_file.put_line(fnd_file.log,'Before merge costs_tab is:');
1943       FOR k IN 1 .. costs_table.COUNT
1944       LOOP
1945         fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||costs_table(k).cost_cmpntcls_id);
1946         fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||costs_table(k).cost_analysis_code);
1947         fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||costs_table(k).cost_level);
1948         fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||costs_table(k).component_cost);
1949         fnd_file.put_line(fnd_file.log,'====================================');
1950       END LOOP;
1951     ELSE
1952       fnd_file.put_line(fnd_file.log,'No costs to merge');
1953     END IF;
1954   END IF; -- End printing cost tabs for debug mode
1955 
1956   IF costs_table.EXISTS(1) THEN
1957     -- If this is an averaging of costs we need to do his first before combining the results
1958     IF merge_mode IN ('A','V') THEN --IF merge_mode = 'A'  -- AF
1959       divisor := new_qty + cost_qty;
1960       IF divisor = 0 THEN
1961         divisor := 1;
1962       END IF;
1963 
1964     IF l_debug_level >= l_debug_level_high THEN
1965       fnd_file.put_line(fnd_file.log,'Divisor is  '||divisor||'cost qty = '||cost_qty||' new qty= '||new_qty);
1966     END IF;
1967 
1968       IF merge_mode ='A' THEN
1969         FOR k in 1 .. costs_table.COUNT
1970         LOOP
1971           costs_table(k).component_cost := costs_table(k).component_cost * cost_qty / divisor;
1972          END LOOP;
1973       END IF;
1974 
1975       FOR k in 1 .. new_cost_tab.COUNT
1976       LOOP
1977          -- LCM-OPM Integration, added merge mode V (Value adjustment) for LC adjustments.
1978         IF merge_mode ='A' THEN
1979           new_cost_tab(k).component_cost := new_cost_tab(k).component_cost * new_qty / divisor;
1980         ELSE --Merge mode IS V value adjustment
1981           new_cost_tab(k).component_cost := new_cost_tab(k).component_cost * 1 / divisor;
1982         END IF;
1983         -- AF
1984       END LOOP;
1985 
1986       IF l_debug_level >= l_debug_level_high THEN
1987         fnd_file.put_line(fnd_file.log,'Cost_qty = '||cost_qty||', new_qty = '||new_qty);
1988         fnd_file.put_line(fnd_file.log,'After averaging new_cost_tab is:');
1989 
1990         FOR k IN 1 .. new_cost_tab.COUNT
1991         LOOP
1992           fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
1993           fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
1994           fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||new_cost_tab(k).cost_level);
1995           fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
1996           fnd_file.put_line(fnd_file.log,'====================================');
1997         END LOOP;
1998 
1999         fnd_file.put_line(fnd_file.log,'After averaging costs_tab is:');
2000         FOR k IN 1 .. costs_table.COUNT
2001         LOOP
2002           fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||costs_table(k).cost_cmpntcls_id);
2003           fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||costs_table(k).cost_analysis_code);
2004           fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||costs_table(k).cost_level);
2005           fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||costs_table(k).component_cost);
2006           fnd_file.put_line(fnd_file.log,'====================================');
2007         END LOOP;
2008       END IF;
2009     END IF; -- merge_mode IN ('A','V')
2010 
2011     FOR k IN 1 .. costs_table.COUNT
2012     LOOP
2013       FOR l in 1..new_cost_tab.COUNT
2014       LOOP
2015         l_cost_accrued := FALSE;
2016         IF  (new_cost_tab(l).cost_cmpntcls_id = costs_table(k).cost_cmpntcls_id)
2017         AND (new_cost_tab(l).cost_analysis_code = costs_table(k).cost_analysis_code)
2018         AND (new_cost_tab(l).cost_level = costs_table(k).cost_level) THEN
2019           new_cost_tab(l).component_cost :=  new_cost_tab(l).component_cost + costs_table(k).component_cost;
2020           l_cost_accrued := TRUE;
2021           EXIT;
2022         END IF;
2023       END LOOP;
2024 
2025       IF NOT l_cost_accrued THEN
2026         l := new_cost_tab.count+1;
2027         new_cost_tab(l) := SYSTEM.gmf_cost_type
2028                            ( costs_table(k).cost_cmpntcls_id
2029                            , costs_table(k).cost_analysis_code
2030                            , 0
2031                            , costs_table(k).component_cost
2032                            , 0
2033                            );
2034 
2035       END IF;
2036     END LOOP;
2037   END IF;
2038 
2039   new_cost.unit_cost := 0;
2040 
2041   FOR k IN 1 .. new_cost_tab.COUNT
2042   LOOP
2043     new_cost.unit_cost := new_cost.unit_cost + new_cost_tab(k).component_cost;
2044   END LOOP;
2045 
2046   IF l_debug_level >= l_debug_level_high THEN
2047     fnd_file.put_line(fnd_file.log,'After merge new_cost_tab is:');
2048 
2049     FOR k IN 1 .. new_cost_tab.COUNT
2050     LOOP
2051       fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
2052       fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
2053       fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||new_cost_tab(k).cost_level);
2054       fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
2055       fnd_file.put_line(fnd_file.log,'====================================');
2056     END LOOP;
2057     fnd_file.put_line(fnd_file.log,'After merging, new unit cost is: '||new_cost.unit_cost);
2058   END IF;
2059 
2060   IF l_debug_level >= l_debug_level_medium THEN
2061        fnd_file.put_line
2062        (fnd_file.log,'Leaving Procedure: '||procedure_name);
2063      END IF;
2064 END;
2065 
2066     --**********************************************************************************************
2067     --*                                                                                            *
2068     --* Procedure load and cost burdens. If any burdens are located, they are costed and the       *
2069     --* l_burdens_tab and l_burdens_costs_tab tables are populated                                 *
2070     --*                                                                                            *
2071     --**********************************************************************************************
2072 
2073 -- 3388974-2 This procedure reinstated and its replacement deleted.
2074 
2075 PROCEDURE process_burdens
2076 IS
2077   i         NUMBER;
2078   j         NUMBER;
2079   factor    NUMBER;
2080   cost      NUMBER;
2081   procedure_name VARCHAR2(100);
2082 BEGIN
2083 
2084   procedure_name := 'Process Burdens';
2085 
2086    IF l_debug_level >= l_debug_level_medium THEN
2087        fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
2088    END IF;
2089 
2090   l_return_status := 'S';
2091   l_burdens_total := 0;
2092   l_burden_costs_tab.delete;
2093 
2094 -- Bug 13038249 Grupo viz changes starts
2095   IF transaction_row.burden_ind = 0 THEN
2096    IF l_debug_level >= l_debug_level_medium THEN
2097        fnd_file.put_line(fnd_file.log,'No Burden defined for '|| transaction_row.inventory_item_id  || '-' ||
2098        transaction_row.orgn_id );
2099    END IF;
2100    RETURN;
2101   END IF;
2102 -- Bug 13038249 Grupo viz changes Ends
2103 
2104   OPEN  burdens_cursor ( transaction_row.inventory_item_id
2105                        , transaction_row.orgn_id
2106                        , transaction_row.lot_number
2107                        , l_cost_type_id
2108                        , transaction_row.trans_date
2109                        );
2110 
2111 
2112   FETCH burdens_cursor BULK COLLECT into l_burdens_tab;
2113 
2114   CLOSE burdens_cursor;
2115 
2116   IF  l_burdens_tab.EXISTS(1) THEN
2117 
2118     IF l_debug_level >= l_debug_level_high THEN
2119       fnd_file.put_line(fnd_file.log,
2120             'Burdens found for item: ' || transaction_row.inventory_item_id ||
2121   	        ' orgn: ' || l_org_tab(transaction_row.orgn_id) ||
2122             ' cost type: ' || l_cost_method_code ||
2123 		        ' lot: ' || transaction_row.lot_number ||
2124 		        ' trans_date: ' || to_char(transaction_row.trans_date));
2125 
2126       fnd_file.put_line( fnd_file.log, 'Burdens Table is:');
2127       FOR i IN 1.. l_burdens_tab.COUNT
2128       LOOP
2129         fnd_file.put_line( fnd_file.log, 'Resources['||i||']:'||l_burdens_tab(i).resources);
2130         fnd_file.put_line( fnd_file.log, 'CCC ID   ['||i||']:'||l_burdens_tab(i).cost_cmpntcls_id);
2131         fnd_file.put_line( fnd_file.log, 'C/A Code ['||i||']:'||l_burdens_tab(i).cost_analysis_code);
2132         fnd_file.put_line( fnd_file.log, 'B/Factor ['||i||']:'||l_burdens_tab(i).burden_factor);
2133       END LOOP;
2134     END IF;
2135 
2136     -- Retrieve the cost for each resource. If we can't find a cost for this cost method
2137     -- then we cannot carry on.
2138 
2139     FOR i IN 1..l_burdens_tab.COUNT
2140     LOOP
2141       l_burden_cost := 0;
2142 
2143       -- umoogala: using co_code and default_cost_mthd to get costs for non-lot controlled items.
2144       -- was calendar_code and cost_mthd_code
2145 
2146       IF l_debug_level >= l_debug_level_high THEN
2147         fnd_file.put_line( fnd_file.log,
2148           'Searching for resource cost using LE: ' || l_le_id ||
2149           ' mthd: ' || l_default_cost_type_id ||
2150 	        ' rsrc: ' || l_burdens_tab(i).resources ||
2151           ' orgn: ' || l_org_tab(transaction_row.orgn_id) ||
2152           ' CCC/ID: ' || l_burdens_tab(i).cost_cmpntcls_id ||
2153           ' A/Code: ' ||l_burdens_tab(i).cost_analysis_code ||
2154           ' date: '   || transaction_row.trans_date);
2155       END IF;
2156 /* INVCONV sschinch */
2157       OPEN burden_cost_cursor
2158            ( l_le_id
2159            , l_default_cost_type_id
2160            , l_burdens_tab(i).resources
2161            , transaction_row.orgn_id
2162            , l_burdens_tab(i).cost_cmpntcls_id
2163            , l_burdens_tab(i).cost_analysis_code
2164            , transaction_row.trans_date
2165            );
2166 
2167       l_burden_cost := NULL;
2168 
2169       FETCH burden_cost_cursor INTO l_burden_cost;
2170 
2171       CLOSE burden_cost_cursor;
2172 
2173       IF l_burden_cost IS NOT NULL THEN
2174 
2175         IF l_debug_level >= l_debug_level_high THEN
2176           fnd_file.put_line
2177           (fnd_file.log, 'Found cost: '|| l_burden_cost);
2178         END IF;
2179 
2180         l_burdens_tab(i).burden_cost := l_burdens_tab(i).burden_factor * l_burden_cost;
2181 
2182       ELSE
2183 
2184         fnd_file.put_line
2185         (  fnd_file.log
2186         ,  'ERROR: Unable to locate a burden cost. Resource: '
2187         || l_burdens_tab(i).resources
2188         || ', analysis code: '
2189         || l_burdens_tab(i).cost_analysis_code
2190         || ', component class ID: '
2191         || to_char(l_burdens_tab(i).cost_cmpntcls_id)
2192         );
2193 
2194         l_return_status := 'E';
2195         RETURN;
2196       END IF;
2197 
2198       l_burden_costs_tab(i) := SYSTEM.gmf_cost_type
2199                              ( l_burdens_tab(i).cost_cmpntcls_id
2200                              , l_burdens_tab(i).cost_analysis_code
2201                              , 0
2202                              , l_burdens_tab(i).burden_cost
2203                              , 1
2204                              );
2205 
2206 
2207       l_burdens_total := l_burdens_total + l_burden_costs_tab(i).component_cost;
2208     END LOOP;
2209   END IF;
2210 
2211   IF l_debug_level >= l_debug_level_high THEN
2212     fnd_file.put_line(fnd_file.log,'At end of process_burdens, the burden costs are:');
2213     FOR i IN 1 .. l_burden_costs_tab.COUNT
2214     LOOP
2215       fnd_file.put_line(fnd_file.log,'CCC/ID['||i||']: '||l_burden_costs_tab(i).cost_cmpntcls_id);
2216       fnd_file.put_line(fnd_file.log,'A/Code['||i||']: '||l_burden_costs_tab(i).cost_analysis_code);
2217       fnd_file.put_line(fnd_file.log,'Level ['||i||']: '||l_burden_costs_tab(i).cost_level);
2218       fnd_file.put_line(fnd_file.log,'C/Cost['||i||']: '||l_burden_costs_tab(i).component_cost);
2219       fnd_file.put_line(fnd_file.log,'====================================');
2220     END LOOP;
2221   END IF;
2222 
2223   IF l_debug_level >= l_debug_level_medium THEN
2224        fnd_file.put_line
2225        (fnd_file.log,'Leaving Procedure: '||procedure_name);
2226      END IF;
2227 
2228 EXCEPTION
2229   WHEN OTHERS THEN
2230 
2231     fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
2232     l_return_status := 'E';
2233 
2234 END process_burdens;
2235 
2236     --**********************************************************************************************
2237     --*                                                                                            *
2238     --* Procedure to create a cost header for a new lot in gmf_lot_costs                           *
2239     --*                                                                                            *
2240     --**********************************************************************************************
2241 
2242 
2243 PROCEDURE create_cost_header
2244 ( p_item_id          IN NUMBER
2245 , p_lot_number       IN VARCHAR2
2246 , p_orgn_id          IN NUMBER
2247 , p_cost_type_id     IN NUMBER
2248 , p_unit_cost        IN NUMBER
2249 , p_cost_date        IN DATE
2250 , p_onhand_qty       IN NUMBER
2251 , p_doc_id           IN NUMBER
2252 , p_trx_src_type_id  IN NUMBER
2253 , p_txn_act_id       IN NUMBER
2254 , x_header_id       OUT NOCOPY NUMBER
2255 , x_unit_cost       OUT NOCOPY NUMBER
2256 , x_onhand_qty      OUT NOCOPY NUMBER
2257 , x_return_status   OUT NOCOPY VARCHAR2
2258 )
2259 IS
2260   loop_count NUMBER;
2261   procedure_name VARCHAR2(100);
2262 BEGIN
2263   procedure_name := 'Create Cost Header';
2264   IF l_debug_level >= l_debug_level_medium THEN
2265     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
2266   END IF;
2267 
2268   IF p_unit_cost IS NULL THEN
2269     fnd_file.put_line
2270     (fnd_file.log,'ERROR: Could not create cost header for Lot Number '||to_char(transaction_row.lot_number)||' as no cost was available');
2271     fnd_file.put_line
2272     (fnd_file.log,'      Transaction type/ID was '||transaction_row.transaction_source_type_id||'/'||to_char(transaction_row.transaction_id));
2273     fnd_file.put_line(fnd_file.log, 'p_unit_cost  = '||p_unit_cost);
2274     x_return_status := 'E';
2275     RETURN;
2276   END IF;
2277 
2278   IF l_debug_level >= l_debug_level_medium THEN
2279     fnd_file.put_line(fnd_file.log,'Inside INSERT HEADER');
2280     fnd_file.put_line(fnd_file.log,'Item ID      = '||p_item_id);
2281     fnd_file.put_line(fnd_file.log,'Lot Number       = '||p_lot_number);
2282     fnd_file.put_line(fnd_file.log,'Unit Cost    = '||p_unit_cost);
2283     fnd_file.put_line(fnd_file.log,'Cost Date    = '||p_cost_date);
2284     fnd_file.put_line(fnd_file.log,'Whse Code    = '||p_orgn_id);
2285     fnd_file.put_line(fnd_file.log,'Cost Method  = '||p_cost_type_id);
2286     fnd_file.put_line(fnd_file.log,'Onhand Qty   = '||p_onhand_qty);
2287     fnd_file.put_line(fnd_file.log,'Doc Type     = '||p_trx_src_type_id||','||p_txn_act_id);
2288     fnd_file.put_line(fnd_file.log,'Doc ID       = '||p_doc_id);
2289   END IF;
2290 
2291   INSERT INTO gmf_lot_costs
2292   ( header_id
2293   , inventory_item_id
2294   , lot_number
2295   , organization_id
2296   , cost_type_id
2297   , unit_cost
2298   , cost_date
2299   , onhand_qty
2300   , last_trx_source_type_id
2301   , last_trx_action_id
2302   , last_costing_doc_id
2303   , creation_date
2304   , created_by
2305   , last_update_date
2306   , last_updated_by
2307   , delete_mark
2308   , request_id
2309   , program_application_id
2310   , program_id
2311   , program_update_date
2312   , final_cost_flag
2313   )
2314   VALUES
2315   (
2316     gmf_cost_header_id_s.nextval
2317   , p_item_id
2318   , p_lot_number
2319   , p_orgn_id
2320   , p_cost_type_id
2321   , p_unit_cost
2322   , p_cost_date
2323   , p_onhand_qty
2324   , p_trx_src_type_id
2325   , p_txn_act_id
2326   , p_doc_id
2327   , SYSDATE
2328   , l_user_id
2329   , SYSDATE
2330   , l_user_id
2331   , 0
2332   , l_request_id
2333   , l_prog_appl_id
2334   , l_program_id
2335   , SYSDATE
2336   , l_final_run_flag
2337   )
2338   RETURNING header_id, unit_cost, onhand_qty
2339   INTO      x_header_id, x_unit_cost, x_onhand_qty;
2340 
2341   x_return_status := 'S';
2342 
2343   IF l_debug_level >= l_debug_level_medium THEN
2344        fnd_file.put_line
2345        (fnd_file.log,'Leaving Procedure: '||procedure_name);
2346      END IF;
2347 
2348 EXCEPTION
2349 
2350   WHEN OTHERS THEN
2351     fnd_file.put_line
2352     (fnd_file.log,'ERROR: Unable to create cost header');
2353     fnd_file.put_line
2354     (fnd_file.log,SQLERRM || ' in ' || procedure_name);
2355 
2356     x_return_status := 'E';
2357 
2358 END create_cost_header;
2359 
2360 
2361     --**********************************************************************************************
2362     --*                                                                                            *
2363     --* Procedure to create a new lot cost detail row in gmf_lot_cost_details                      *
2364     --*                                                                                            *
2365     --**********************************************************************************************
2366 
2367 
2368 PROCEDURE create_cost_detail
2369 ( p_header_id          IN NUMBER
2370 , p_component_class_id IN NUMBER
2371 , p_cost_analysis_code IN VARCHAR2
2372 , p_cost_level         IN VARCHAR2
2373 , p_component_cost     IN NUMBER
2374 , p_burden_ind         IN NUMBER
2375 , x_return_status     OUT NOCOPY VARCHAR2
2376 )
2377 IS
2378 
2379   loop_count NUMBER;
2380   procedure_name VARCHAR2(100);
2381 
2382 BEGIN
2383   procedure_name := 'Create Cost Detail';
2384   IF l_debug_level >= l_debug_level_medium THEN
2385     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
2386     fnd_file.put_line(fnd_file.log,'Inside INSERT DETAIL');
2387     fnd_file.put_line(fnd_file.log,'Header ID           = '||p_header_id);
2388     fnd_file.put_line(fnd_file.log,'Component Class ID  = '||p_component_class_id);
2389     fnd_file.put_line(fnd_file.log,'Analysis Code       = '||p_cost_analysis_code);
2390     fnd_file.put_line(fnd_file.log,'Cost Level          = '||p_cost_level);
2391     fnd_file.put_line(fnd_file.log,'Burden Ind          = '||p_burden_ind);
2392     fnd_file.put_line(fnd_file.log,'Cost                = '||p_component_cost);
2393   END IF;
2394 
2395     INSERT INTO gmf_lot_cost_details
2396     ( header_id
2397     , detail_id
2398     , cost_cmpntcls_id
2399     , cost_analysis_code
2400     , cost_level
2401     , component_cost
2402     , burden_ind
2403     , creation_date
2404     , created_by
2405     , last_update_date
2406     , last_updated_by
2407     , delete_mark
2408     , request_id
2409     , program_application_id
2410     , program_id
2411     , program_update_date
2412     , final_cost_flag
2413     )
2414     VALUES
2415     ( p_header_id
2416     , gmf_cost_detail_id_s.nextval
2417     , p_component_class_id
2418     , p_cost_analysis_code
2419     , p_cost_level
2420     , p_component_cost
2421     , p_burden_ind
2422     , sysdate
2423     , l_user_id
2424     , sysdate
2425     , l_user_id
2426     , 0
2427     , l_request_id
2428     , l_prog_appl_id
2429     , l_program_id
2430     , SYSDATE
2431     , l_final_run_flag
2432     );
2433 
2434 
2435   x_return_status := 'S';
2436   IF l_debug_level >= l_debug_level_medium THEN
2437        fnd_file.put_line
2438        (fnd_file.log,'Leaving Procedure: '||procedure_name);
2439      END IF;
2440 
2441 EXCEPTION
2442 
2443   WHEN OTHERS THEN
2444     fnd_file.put_line
2445     (fnd_file.log,'ERROR: Unable to create cost detail');
2446     fnd_file.put_line
2447     (fnd_file.log,SQLERRM || ' in ' || procedure_name);
2448 
2449     x_return_status := 'E';
2450 
2451 END create_cost_detail;
2452 
2453     --**********************************************************************************************
2454     --*                                                                                            *
2455     --* Procedure to clone the costs for a lot/organization                                           *
2456     --*                                                                                            *
2457     --**********************************************************************************************
2458 
2459 PROCEDURE clone_costs
2460 IS
2461   old_header_id  NUMBER;
2462   new_header_id  NUMBER;
2463   i              NUMBER;
2464   discard        NUMBER;
2465   procedure_name VARCHAR2(100);
2466 BEGIN
2467 
2468   procedure_name := 'Clone Costs';
2469   IF l_debug_level >= l_debug_level_medium THEN
2470        fnd_file.put_line
2471        (fnd_file.log,'Entered Procedure: '||procedure_name);
2472   END IF;
2473 
2474   create_cost_header
2475   ( old_cost.inventory_item_id /*bug 5309434*/
2476   , old_cost.lot_number
2477   , old_cost.organization_id
2478   , old_cost.cost_type_id
2479   , old_cost.unit_cost
2480   , old_cost.cost_date
2481   , old_cost.onhand_qty
2482   ,old_cost.last_trx_source_type_id
2483   ,old_cost.last_trx_action_id
2484   , old_cost.last_costing_doc_id
2485   , new_header_id
2486   , discard
2487   , discard
2488   , l_return_status
2489   );
2490 
2491   old_cost.header_id := new_header_id;
2492 
2493   IF l_return_status = 'S' THEN
2494     FOR i in 1 .. old_cost_tab.COUNT
2495     LOOP
2496       create_cost_detail
2497       ( new_header_id
2498       , old_cost_tab(i).cost_cmpntcls_id
2499       , old_cost_tab(i).cost_analysis_code
2500       , 0
2501       , old_cost_tab(i).component_cost
2502       , 0
2503       , l_return_status
2504       );
2505     END LOOP;
2506   END IF;
2507   IF l_debug_level >= l_debug_level_medium THEN
2508        fnd_file.put_line
2509        (fnd_file.log,'Leaving Procedure: '||procedure_name);
2510   END IF;
2511 
2512 EXCEPTION
2513 
2514   WHEN OTHERS THEN
2515   fnd_file.put_line(fnd_file.log
2516   , 'ERROR: Could not clone costs '||SQLERRM
2517   );
2518 
2519   l_return_status := 'U';
2520 END clone_costs;
2521 
2522 
2523     --**********************************************************************************************
2524     --*                                                                                            *
2525     --* Procedure to create a new linking transaction in gmf_material_lot_cost_txns                *
2526     --*                                                                                            *
2527     --**********************************************************************************************
2528 PROCEDURE create_material_transaction
2529 ( p_header_id        IN NUMBER
2530 , p_cost_type_id     IN NUMBER
2531 , p_trans_date       IN DATE
2532 , p_trans_qty        IN NUMBER
2533 , p_trans_um         IN VARCHAR2
2534 , p_total_cost       IN NUMBER
2535 , p_trans_id         IN NUMBER
2536 , p_unit_cost        IN NUMBER
2537 , p_onhand_qty       IN NUMBER
2538 , p_old_unit_cost    IN NUMBER
2539 , p_old_onhand_qty   IN NUMBER
2540 , p_new_cost_ind     IN NUMBER
2541 , p_lot_number       IN VARCHAR2
2542 , x_return_status   OUT NOCOPY VARCHAR2
2543 )
2544 IS
2545   loop_count NUMBER;
2546   procedure_name VARCHAR2(100);
2547 BEGIN
2548   procedure_name := 'Create Material Transaction';
2549   IF l_debug_level >= l_debug_level_medium THEN
2550     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
2551     fnd_file.put_line(fnd_file.log,'Creating material transaction with these values:');
2552     fnd_file.put_line(fnd_file.log,'Header ID      :'||p_header_id);
2553     fnd_file.put_line(fnd_file.log,'Cost Method    :'||p_cost_type_id);
2554     fnd_file.put_line(fnd_file.log,'Date           :'||p_trans_date);
2555     fnd_file.put_line(fnd_file.log,'Trans Qty      :'||p_trans_qty);
2556     fnd_file.put_line(fnd_file.log,'UoM            :'||p_trans_um);
2557     fnd_file.put_line(fnd_file.log,'Total Cost     :'||p_total_cost);
2558     fnd_file.put_line(fnd_file.log,'Trans ID       :'||p_trans_id);
2559     fnd_file.put_line(fnd_file.log,'Unit Cost      :'||p_unit_cost);
2560     fnd_file.put_line(fnd_file.log,'Onhand Qty     :'||p_onhand_qty);
2561     fnd_file.put_line(fnd_file.log,'Old Unit Cost  :'||p_old_unit_cost);
2562     fnd_file.put_line(fnd_file.log,'Old Onhand Qty :'||p_old_onhand_qty);
2563     fnd_file.put_line(fnd_file.log,'New Cost Ind   :'||p_new_cost_ind);
2564     fnd_file.put_line(fnd_file.log,'Lot Number   :'||p_lot_number);
2565   END IF;
2566 
2567   INSERT INTO gmf_material_lot_cost_txns
2568   ( cost_trans_id
2569   , cost_header_id
2570   , cost_type_id
2571   , cost_trans_date
2572   , cost_trans_qty
2573   , cost_trans_um
2574   , total_trans_cost
2575   , transaction_id
2576   , new_unit_cost
2577   , new_onhand_qty
2578   , old_unit_cost
2579   , old_onhand_qty
2580   , creation_date
2581   , created_by
2582   , last_update_date
2583   , last_updated_by
2584   , request_id
2585   , program_application_id
2586   , program_id
2587   , program_update_date
2588   , final_cost_flag
2589   , new_cost_ind
2590   , lot_number
2591   )
2592   VALUES
2593   ( gmf_cost_trans_id_s.nextval
2594   , p_header_id
2595   , p_cost_type_id
2596   , p_trans_date
2597   , p_trans_qty
2598   , p_trans_um
2599   , p_total_cost
2600   , decode(p_trans_id, -9, (-1*gmf_cost_trans_id_s.currval),p_trans_id)
2601   , p_unit_cost
2602   , p_onhand_qty
2603   , p_old_unit_cost
2604   , p_old_onhand_qty
2605   , sysdate
2606   , l_user_id
2607   , sysdate
2608   , l_user_id
2609   , l_request_id
2610   , l_prog_appl_id
2611   , l_program_id
2612   , SYSDATE
2613   , l_final_run_flag
2614   , p_new_cost_ind
2615   , p_lot_number
2616   );
2617   x_return_status := 'S';
2618 
2619   IF l_debug_level >= l_debug_level_medium THEN
2620      fnd_file.put_line(fnd_file.log,'Leaving Procedure: '||procedure_name);
2621   END IF;
2622 
2623 EXCEPTION
2624   WHEN OTHERS THEN
2625     fnd_file.put_line
2626     (fnd_file.log,'ERROR: Unable to create material cost transaction');
2627     fnd_file.put_line
2628     (fnd_file.log,SQLERRM || ' in ' || procedure_name);
2629 
2630     x_return_status := 'E';
2631 
2632 END create_material_transaction;
2633 
2634 
2635     --**********************************************************************************************
2636     --*                                                                                            *
2637     --* Procedures to handle received lots.                                                        *
2638     --*                                                                                            *
2639     --* The costs come from the PO together with any associated Special Charges and burdens.       *
2640     --* If this is the first receipt for this lot in this warehouse the costs will be set up.      *
2641     --*                                                                                            *
2642     --* If there is already a cost for this lot and warehouse then the costs will be revised       *
2643     --* by averaging the new cost with the old cost by using the original and revised quantities.  *
2644     --*                                                                                            *
2645     --* Returns update the cost using an identical formula                                         *
2646     --*                                                                                            *
2647     --* HISTORY                                                                                    *
2648     --*                                                                                            *
2649     --*   Bug#5463200 Anand Thiyagarajan  14-Aug-2006                                              *
2650     --*     Modified Code in the Special Charges query to remove po_line_locations_all, changed the*
2651     --*     value for include_in_acquisition_cost to "I", used estimated_amount instead of actual  *
2652     --*     and included MMT table join with RCV_TRANSACTION_ID instead of INV_TRANSACTION_ID      *
2653     --*    Andrea 4-Aug-2009 LCM-OPM Integrations, bug 8642337, added Unin all query to fetch      *
2654     --*      Estimated LC adjustments for LCM enabled Po lines.                                    *
2655     --**********************************************************************************************
2656 
2657 PROCEDURE get_special_charges IS
2658 
2659     /* Begin 3824810 sschinch */
2660     TYPE acquistion_rec IS RECORD
2661     ( COST_CMPNTCLS_ID               NUMBER(15)
2662     , COST_ANALYSIS_CODE             CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE -- bug14480540
2663     , COMPONENT_COST                 NUMBER
2664     , RECEIPT_UOM                    VARCHAR2(3)
2665     , LCM_FLAG                       NUMBER
2666      );
2667 
2668    TYPE acq_tab_type IS TABLE OF acquistion_rec	 INDEX BY PLS_INTEGER;
2669 
2670         CURSOR acquisition_cursor IS
2671         SELECT  rc.cost_component_class_id
2672                 ,rc.cost_analysis_code
2673                 ,nvl(rca.estimated_amount, rca.actual_amount)/mmt.transaction_quantity /* ANTHIYAG Bug#5463200 14-Aug-2006 */
2674                 ,uom.uom_code
2675                 ,0  lcm_flag
2676          FROM   rcv_transactions t,
2677                 po_rcv_charges rc,
2678                 po_rcv_charge_allocations rca,
2679                 mtl_units_of_measure uom,
2680                 mtl_material_transactions mmt, /* ANTHIYAG Bug#5463200 14-Aug-2006 */
2681                 po_line_locations_all pll -- AF
2682          WHERE  mmt.transaction_id = transaction_row.transaction_id /* ANTHIYAG Bug#5463200 14-Aug-2006 */
2683          AND    t.transaction_id = mmt.rcv_transaction_id
2684          AND    t.shipment_header_id = rc.shipment_header_id
2685          AND    t.shipment_line_id  = rca.shipment_line_id
2686          AND    rc.charge_id = rca.charge_id
2687          AND    t.unit_of_measure = uom.unit_of_measure
2688          AND    rc.include_in_acquisition_cost = 'I' /* ANTHIYAG Bug#5463200 14-Aug-2006 */
2689          AND    t.po_line_location_id = pll.line_location_id  -- AF
2690          AND    NVL(pll.lcm_flag,'N') = 'N'                   -- AF
2691          -- AF
2692       UNION ALL  /* Estimated LC adjustments for LCM enabled PO lines */
2693          SELECT  glat.cost_cmpntcls_id
2694                 ,glat.cost_analysis_code
2695                 ,(nvl(glat.new_landed_cost,0) - nvl(glat.prior_landed_cost,0)) / glat.Primary_quantity
2696                 ,glat. primary_uom_code
2697                 ,1  lcm_flag
2698            FROM
2699                  gmf_lc_adj_transactions glat,
2700 		 mtl_material_transactions mmt,
2701                  rcv_transactions rt ,
2702                  po_line_locations_all pll
2703           WHERE
2704                  mmt.transaction_id      = transaction_row.transaction_id
2705             AND  glat.rcv_transaction_id = mmt.rcv_transaction_id
2706             AND  rt.transaction_id       = glat.rcv_transaction_id
2707             AND  (glat.lc_adjustment_flag = 0 OR glat.adjustment_num = 0)
2708             AND  glat.cost_acquisition_flag = 'I'
2709             AND  glat.component_type IN ('ITEM PRICE','CHARGE')
2710             AND  rt.po_line_location_id  = pll.line_location_id
2711             AND  NVL(pll.lcm_flag,'N')   = 'Y';
2712             -- AF
2713 
2714          l_acq_tab            acq_tab_type;
2715          l_from_uom           VARCHAR2(25); --(This variable stores po or receipt unit of measure)
2716          l_cmpntcls_id        NUMBER;
2717          l_cost_analysis_code CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE; -- bug14480540
2718          l_component_cost     NUMBER;
2719          l_acquisition_cost NUMBER;
2720          procedure_name VARCHAR2(100);
2721 
2722         /* End 3824810 sschinch */
2723 BEGIN
2724 
2725   procedure_name := 'Process Special Charges';
2726   IF l_debug_level >= l_debug_level_medium THEN
2727      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
2728   END IF;
2729 
2730   l_acquisitions_total := 0;
2731 
2732   OPEN acquisition_cursor;
2733   FETCH acquisition_cursor BULK COLLECT INTO l_acq_tab;
2734   CLOSE acquisition_cursor;
2735 
2736 
2737     IF l_acq_tab.EXISTS(1) THEN
2738     FOR i IN 1 .. l_acq_tab.COUNT
2739     LOOP
2740         /* For non-LCM charges only invoke uom conversion */
2741        l_from_uom := l_acq_tab(i).receipt_uom;
2742       IF (transaction_row.trans_um <> l_from_uom AND l_acq_tab(i).lcm_flag = 0) THEN
2743          l_acquisition_cost :=
2744                INV_CONVERT.INV_UM_CONVERT(ITEM_ID       => transaction_row.inventory_item_id
2745                                          ,PRECISION     => 5
2746                                          ,ORGANIZATION_ID => transaction_row.orgn_id
2747                                          ,LOT_NUMBER     => transaction_row.lot_number
2748                                          ,FROM_QUANTITY => l_acq_tab(i).component_cost
2749                                          ,FROM_UNIT     => transaction_row.trans_um
2750                                          ,TO_UNIT       => l_from_uom
2751                                          ,FROM_NAME     => NULL
2752                                          ,TO_NAME       => NULL
2753                                            );
2754         l_acq_tab(i).component_cost := l_acquisition_cost;
2755 
2756       	IF ((l_acquisition_cost = -99999) OR (SIGN(l_acq_tab(i).component_cost)<> SIGN (l_acquisition_cost)))
2757       	THEN
2758            fnd_file.put_line
2759                  (fnd_file.log,'ERROR: Unable to convert from '
2760                  ||transaction_row.trans_um
2761                  ||' to '||l_from_uom||' for transaction ID '
2762                  ||transaction_row.transaction_id
2763                  ||' aqui component cost '|| l_acq_tab(i).component_cost
2764                  ||' Reurned Value '||l_acquisition_cost
2765                  );
2766                  l_return_status := 'E';
2767                  RETURN;
2768 
2769         END IF;
2770       END IF;
2771 
2772       /******** Bug  4038722 - Dinesh Vadivel - Start **********/
2773 
2774       /*   Convert to Base Currency, if Acquisition Cost(i.e., Receipt )
2775        **  currency is different.
2776        **  receipt_ccy and l_exchange_rate would have been calculated
2777        **  in process_receipts itself. So we can just use it as they are global variables.
2778        */
2779       IF ( l_base_ccy_code <>  receipt_ccy AND l_acq_tab(i).lcm_flag = 0) THEN /* Check if the receipt currency is the same as the base currency */
2780 
2781          IF l_debug_level >= l_debug_level_medium THEN
2782               fnd_file.put_line   (fnd_file.log,'
2783                  In Acquisition Costs() : Converting component_cost : '||
2784                  NVL(l_acq_tab(i).component_cost,0)||' Receipt Currency : '||receipt_ccy||
2785                  ' to Base Currency : '||l_base_ccy_code||'. New component_cost : '||
2786                  NVL(l_acq_tab(i).component_cost,0) * l_exchange_rate||'. Exchange Rate is : '||l_exchange_rate);
2787           END IF;
2788 
2789           l_acq_tab(i).component_cost :=  NVL(l_acq_tab(i).component_cost,0) * l_exchange_rate;
2790 
2791         END IF;
2792 
2793       /******** Bug  4038722 - Dinesh Vadivel - End **********/
2794 
2795       l_acquisitions_total := l_acquisitions_total + NVL(l_acq_tab(i).component_cost,0);
2796 
2797       l_cmpntcls_id := l_acq_tab(i).cost_cmpntcls_id;
2798       l_cost_analysis_Code := l_acq_tab(i).cost_analysis_code;
2799       l_component_cost := NVL(l_acq_tab(i).component_cost,0);
2800 
2801 
2802 
2803       l_acqui_cost_tab(i) := SYSTEM.gmf_cost_type(l_cmpntcls_id,
2804       			                   l_cost_analysis_code,
2805      			                   0,
2806      			                   l_component_cost,
2807      			                   0);
2808 
2809        /*End Bug 3824810 sschinch */
2810 
2811     END LOOP;
2812   END IF;
2813 
2814     IF l_debug_level >= l_debug_level_medium THEN
2815       fnd_file.put_line(fnd_file.log,'At end of get_special_charges, the costs are:');
2816       FOR i IN 1 .. l_acqui_cost_tab.COUNT
2817       LOOP
2818         fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||l_acqui_cost_tab(i).cost_cmpntcls_id);
2819         fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||l_acqui_cost_tab(i).cost_analysis_code);
2820         fnd_file.put_line(fnd_file.log,'Level ['||k||']: '||l_acqui_cost_tab(i).cost_level);
2821         fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||l_acqui_cost_tab(i).component_cost);
2822         fnd_file.put_line(fnd_file.log,'====================================');
2823 
2824       END LOOP;
2825     END IF;
2826 
2827     IF l_debug_level >= l_debug_level_medium THEN
2828        fnd_file.put_line
2829        (fnd_file.log,'Leaving Procedure: '||procedure_name);
2830     END IF;
2831 
2832 EXCEPTION
2833     WHEN OTHERS THEN
2834       fnd_file.put_line(fnd_file.log,procedure_name||':'||SQLERRM);
2835 END get_special_charges;
2836 
2837 --*************************************************************************************
2838 --*    Procedure Name : PROCESS_ADJUSTMENT
2839 --*
2840 --*     Description :
2841 --*                Procedure to handle adjusted lots (ADJI/ADJR)
2842 --*
2843 --* HISTORY
2844 --*
2845 --*   27-Nov-2004 Dinesh Vadivel Bug# 4004338
2846 --*        Added cost_type_code in where clause of the select query which returns the header_id
2847 --*        from the gmf_material_lot_cost_txns. The issue arises if we have the same item in two different lot cost methods
2848 --*        and try to run the Lot Actual Cost Process
2849 --*************************************************************************************
2850 
2851  PROCEDURE process_adjustment
2852 IS
2853   loop_count NUMBER;
2854   l_header_id gmf_lot_costs.header_id%type;
2855   procedure_name VARCHAR2(100);
2856 BEGIN
2857 
2858   procedure_name := 'Process Adjustment';
2859   IF l_debug_level >= l_debug_level_medium THEN
2860      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
2861      fnd_file.put_line
2862     (fnd_file.log,'Updating lot cost header with trans_qty of '||transaction_row.trans_qty);
2863   END IF;
2864 
2865   -- This procedure is used if the lot being adjusted has a lot cost. Lots being created
2866   -- via ADJx transactions are handled by the process_creation procedure (above).
2867 
2868   -- Adjustments to lots are handled at the prevailing lot costs. They only affect the quantities
2869   -- not the costs themselves.
2870 
2871   -- If the existing cost is marked as 'Final' and we are running in test mode we need
2872   -- to clone the header and its details to prevent multiple decrements from the same transaction.
2873 
2874   -- This change is done to provide teh undo script for the current run of lot
2875   -- costing in final mode requested by MACSTEEL
2876 
2877   IF  old_cost.final_cost_flag = 1 AND old_cost.request_id <> l_request_id  --  l_final_run_flag = 0
2878   THEN
2879     clone_costs;
2880   END IF;
2881 
2882   UPDATE gmf_lot_costs
2883   SET    onhand_qty = onhand_qty + transaction_row.trans_qty
2884   ,      last_update_date = sysdate
2885   WHERE  header_id = old_cost.header_id;
2886 
2887   IF SQL%ROWCOUNT = 1 THEN
2888     IF l_debug_level >= l_debug_level_medium THEN
2889       fnd_file.put_line
2890       (fnd_file.log,'Creating new cost transaction');
2891     END IF;
2892 
2893     l_header_id := old_cost.header_id;
2894 
2895     create_material_transaction
2896     ( l_header_id
2897     , l_cost_type_id /* INVCONV sschinch */
2898     , transaction_row.trans_date
2899     , transaction_row.trans_qty
2900     , transaction_row.trans_um
2901     , transaction_row.trans_qty * old_cost.unit_cost
2902     , transaction_row.transaction_id
2903     , old_cost.unit_cost
2904     , old_cost.onhand_qty + transaction_row.trans_qty
2905     , old_cost.unit_cost
2906     , old_cost.onhand_qty
2907     , NULL
2908     ,transaction_row.lot_number
2909     , l_return_status
2910     );
2911 
2912     IF l_return_status <> 'S' THEN
2913       RETURN;
2914     END IF;
2915   ELSE
2916     l_return_status := 'E';
2917     RETURN;
2918   END IF;
2919   IF l_debug_level >= l_debug_level_medium THEN
2920        fnd_file.put_line
2921        (fnd_file.log,'Leaving Procedure: '||procedure_name);
2922      END IF;
2923 
2924 EXCEPTION
2925   WHEN OTHERS THEN
2926        fnd_file.put_line
2927        (fnd_file.log,'Failed in procedure process_adjustment with error');
2928        fnd_file.put_line
2929        (fnd_file.log,SQLERRM);
2930        l_return_status := 'U';
2931 
2932 END process_adjustment;
2933 
2934 
2935 
2936 --********************************************************************************************************
2937     --*    Procedure Name : PROCESS_REVERSALS
2938     --*
2939     --*     Description :
2940     --*               Procedure to reversals for products  - Uday Moogala - Bug 4004338
2941     --*
2942     --*   Description :   Assume three records returned by inv_tran_cursor with trans_qty as 100,-100 and 75.
2943     --*    When we encounter -100 record we have to reverse the transaction and set the unit_cost of this record to
2944     --*           Zero  -  if there is no cost prior to 100 record
2945     --*           X$     - where X$ is the cost of the record that is prior to 100 if any exists.
2946     --*          This X$ will be set one and only if we get 100 and -100 adjacent.
2947     --*
2948     --*   Dinesh -No Bug# - Earlier new_cost_ind was not set up properly.Now corrected along with 4053149
2949     --*          NEW_COST_IND in gmf_material_lot_cost_txns is used in the Subledger Posting.
2950     --*          If this indicator is set to 1 then that means whatever cost pointed by
2951     --*          header_id in gmf_lot_cost_details is the AVERAGE COST and not the actual
2952     --*          TRANSACTION COST. The Actual Transaction cost is under the negative of header_id.
2953     --*          So, depending on this NEW_COST_IND, the SL will decide on posting at the cost of
2954     --*          header_id or negative of header_id (i.e., -header_id)
2955     --*
2956     --*  Dinesh 4227784 - Issue due to the above changes. Since we have added gmf_material_lot_cost_txns
2957     --*          to the get_previous_costs_cur CURSOR, while querying for the prev-prev transaction
2958     --*          it will ignore the reversal transactions if any, because the reversal transaction will
2959     --*          not have the exact header_id as in gmf_lot_costs. Rather it will have the same header_id
2960     --*          as that of its original transaction.
2961     --*          So removed this table and added seperately to get new_cost_ind
2962     --*
2963     --**********************************************************************************************
2964 
2965 
2966 PROCEDURE process_reversals
2967 IS
2968 /* INVCONV sschinch modifications */
2969 
2970   CURSOR get_previous_costs_cur(
2971            p_item_id   NUMBER,
2972            p_lot_number VARCHAR2,
2973            p_orgn_id      NUMBER,
2974            p_cost_type_id NUMBER,
2975            p_cost_date DATE
2976            )
2977   IS
2978    SELECT *
2979      FROM (
2980            SELECT last_trx_source_type_id,  --last_costing_doc_type prev_doc_type INVCONV sschinch,
2981                   last_trx_action_id,     --last_costing_doc_id   prev_doc_id INVCONV sschinch,
2982                   header_id             prev_header_id,
2983                   unit_cost             prev_unit_cost,
2984                   RANK () OVER (PARTITION BY glc.inventory_item_id, glc.organization_id, glc.cost_type_id, glc.lot_number
2985                                     ORDER BY glc.cost_date DESC, glc.header_id DESC) lot_cost_rank
2986              FROM gmf_lot_costs glc
2987             WHERE glc.inventory_item_id     = p_item_id
2988               AND glc.lot_number  = p_lot_number
2989               AND glc.organization_id = p_orgn_id
2990               AND glc.cost_type_id = p_cost_type_id
2991               AND glc.cost_date <= p_cost_date
2992           )
2993      WHERE lot_cost_rank < 3
2994      ORDER BY lot_cost_rank
2995     ;
2996 
2997   CURSOR get_cost_details_cur(p_header_id NUMBER)
2998   IS
2999     SELECT cost_cmpntcls_id,
3000            cost_analysis_code,
3001            cost_level,
3002            component_cost,
3003            burden_ind,
3004            cost_origin,
3005            frozen_ind
3006       FROM gmf_lot_cost_details
3007      WHERE header_id = p_header_id
3008   ;
3009 
3010 /* Bug 4227784 - Dinesh Added this as we removed txns table from above query */
3011   CURSOR get_material_lot_cost_txns(p_header_id NUMBER)
3012   IS
3013     SELECT   gmlct.new_cost_ind
3014        FROM  gmf_material_lot_cost_txns gmlct
3015      WHERE   gmlct.cost_header_id = p_header_id
3016     ORDER BY cost_trans_id DESC
3017   ;
3018 
3019   l_prev_cost_cnt    NUMBER := 0;
3020   l_prev_header_id   gmf_lot_costs.header_id%TYPE;
3021   l_prev_prev_header_id   gmf_lot_costs.header_id%TYPE := NULL;
3022   l_prev_unit_cost   gmf_lot_costs.unit_cost%TYPE;
3023   l_cost_header_id   gmf_material_lot_cost_txns.cost_header_id%TYPE;
3024 
3025   l_onhand_qty       gmf_lot_costs.onhand_qty%TYPE;
3026   l_header_id        gmf_lot_costs.header_id%TYPE;
3027   l_unit_cost        gmf_lot_costs.unit_cost%TYPE;
3028   l_prev_trans_unit_cost  gmf_lot_cost_details.component_cost%TYPE;
3029   l_cmpnt_cost       gmf_lot_cost_details.component_cost%TYPE;
3030   l_prev_new_cost_ind   gmf_material_lot_cost_txns.new_cost_ind%TYPE;          /* Dinesh No Bug# */
3031   procedure_name VARCHAR2(100);
3032 
3033 BEGIN
3034 
3035   procedure_name := 'Process Reversals';
3036   IF l_debug_level >= l_debug_level_medium THEN
3037      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
3038   END IF;
3039 
3040     --
3041   -- Running this loop only twice to see two previous cost rows.
3042   -- Cursor will return more than 2 rows in some cases, so had to put EXIT
3043   -- after 2nd iteration
3044   --
3045   /* INVCONV sschich modifications */
3046   FOR i in get_previous_costs_cur(transaction_row.inventory_item_id,
3047                                   transaction_row.lot_number,
3048                                   transaction_row.orgn_id,
3049                                   l_cost_type_id,
3050                                   transaction_row.trans_date
3051                                  )
3052   LOOP
3053       -- if following variable has count > 1 then there are previous costs before
3054       -- original yield.
3055       l_prev_cost_cnt := l_prev_cost_cnt + 1;
3056 
3057 
3058       IF l_prev_cost_cnt = 1
3059       AND (i.last_trx_source_type_id  <> 5 --transaction_row.transaction_source_type_id
3060            OR i.last_trx_action_id <> transaction_row.transaction_action_id)
3061       THEN
3062         -- Other types of txns after original yield. So, process this reversal
3063         -- as regular adjustment. This will result in incorrect avg costs. known issue
3064         IF l_debug_level >= l_debug_level_medium THEN
3065           fnd_file.put_line
3066           (fnd_file.log,'Reversals: Processing reversal as regular adjustment');
3067         END IF;
3068 
3069         process_adjustment;
3070         RETURN;
3071 
3072       ELSIF l_prev_cost_cnt = 1         -- 1st iteration
3073       AND   i.last_trx_source_type_id = 5
3074       AND   i.last_trx_action_id = transaction_row.transaction_action_id
3075       THEN
3076         -- set cost and onhand qty to zero assuming no previous costs.
3077         -- if prev costs exists, then we'll use those costs and qty, but will
3078         -- get set in the else block below in the 2nd iteration.
3079         IF l_debug_level >= l_debug_level_low THEN
3080           fnd_file.put_line
3081           (fnd_file.log, 'Reversals: This txn is (pure) reversal of previous txn');
3082         END IF;
3083 
3084         l_prev_header_id      := i.prev_header_id;
3085         l_cost_header_id      := i.prev_header_id; /* will be used to insert in material txns table */
3086         l_prev_unit_cost      := 0;
3087         --
3088         -- new_cost_ind is needed as Subledger depends on this flag to get the
3089         -- correct transaction costs.
3090         --
3091         -- l_prev_new_cost_ind   := i.prev_new_cost_ind;   /* Dinesh - No Bug #  Removed for 4227784*/
3092         /* Bug 4227784 Added instead of above line*/
3093         OPEN get_material_lot_cost_txns(l_prev_header_id);
3094         FETCH get_material_lot_cost_txns INTO l_prev_new_cost_ind;
3095         CLOSE get_material_lot_cost_txns;
3096       ELSE         -- 2nd iteration
3097         --
3098         -- Will come here if there any old costs before original yield.
3099         --
3100         IF l_debug_level >= l_debug_level_low THEN
3101           fnd_file.put_line
3102           (fnd_file.log,'Reversals: costs exists prior to original yield');
3103         END IF;
3104         l_prev_header_id      := i.prev_header_id;
3105         l_prev_prev_header_id := i.prev_header_id;
3106         l_prev_unit_cost      := i.prev_unit_cost;
3107         --
3108         -- new_cost_ind is needed as Subledger depends on this flag to get the
3109         -- correct transaction costs.
3110         --
3111         --l_prev_new_cost_ind   := i.prev_new_cost_ind; /*Dinesh - No Bug # - Removed for 4227784*/
3112         /* Bug 4227784 Added instead of above line*/
3113         OPEN get_material_lot_cost_txns(l_prev_header_id);
3114         FETCH get_material_lot_cost_txns INTO l_prev_new_cost_ind;
3115         CLOSE get_material_lot_cost_txns;
3116 
3117         EXIT;
3118       END IF;
3119   END LOOP;
3120 
3121 
3122   IF l_debug_level >= l_debug_level_low THEN
3123     fnd_file.put_line
3124      (fnd_file.log,'Reversals: setting unit cost to ' || l_prev_unit_cost ||
3125                    ' and qty to ' || (old_cost.onhand_qty + transaction_row.trans_qty));
3126   END IF;
3127 
3128   IF l_prev_cost_cnt = 0 THEN
3129         fnd_file.put_line
3130               (fnd_file.log,'ERROR: Failed to retrieve previous costs in Process Reversals');
3131         RETURN;
3132   END IF;
3133 
3134   create_cost_header
3135   ( p_item_id         => transaction_row.inventory_item_id
3136   , p_lot_number      => transaction_row.lot_number
3137   , p_orgn_id         => transaction_row.orgn_id
3138   , p_cost_type_id    => l_cost_type_id
3139   , p_unit_cost       => l_prev_unit_cost
3140   , p_cost_date       => transaction_row.trans_date
3141   , p_onhand_qty      => old_cost.onhand_qty + transaction_row.trans_qty
3142   , p_trx_src_type_id => transaction_row.transaction_source_type_id
3143   , p_doc_id          => transaction_row.doc_id
3144   , p_txn_act_id      => transaction_row.transaction_action_id
3145   , x_header_id      => l_header_id
3146   , x_unit_cost      => l_unit_cost
3147   , x_onhand_qty     => l_onhand_qty
3148   , x_return_status  => l_return_status
3149   );
3150 
3151 
3152   IF l_return_status = 'S' THEN
3153         IF l_debug_level >= l_debug_level_low THEN
3154           fnd_file.put_line
3155           (fnd_file.log,'Reversals: Creating new cost detail row');
3156         END IF;
3157 
3158     FOR j in get_cost_details_cur(l_prev_header_id)
3159     LOOP
3160 
3161         IF l_prev_cost_cnt = 1 THEN
3162           l_cmpnt_cost := 0;
3163         ELSIF l_prev_cost_cnt > 1 THEN
3164           -- carrying forward costs before original yield
3165           l_cmpnt_cost := NVL(j.component_cost,0);
3166         END IF;
3167 
3168         create_cost_detail
3169         ( p_header_id          => l_header_id
3170         , p_component_class_id => j.cost_cmpntcls_id
3171         , p_cost_analysis_code => j.cost_analysis_code
3172         , p_cost_level         => j.cost_level
3173         , p_component_cost     => l_cmpnt_cost
3174         , p_burden_ind         => j.burden_ind
3175         , x_return_status      => l_return_status
3176         );
3177 
3178     END LOOP;
3179 
3180     IF l_return_status = 'S' THEN
3181 
3182       IF l_debug_level >= l_debug_level_medium THEN
3183         fnd_file.put_line
3184         (fnd_file.log,'Reversals: Creating new material cost transaction. the l_prev_prev_header_id is  '||l_prev_prev_header_id||' and l_cost_header_id '||l_cost_header_id);
3185       END IF;
3186 
3187       SELECT NVL(SUM(component_cost),0)
3188         INTO l_prev_trans_unit_cost
3189         FROM gmf_lot_cost_details
3190        WHERE header_id = DECODE(NVL(l_prev_prev_header_id, 0), 0, l_cost_header_id, -l_cost_header_id);
3191 
3192       create_material_transaction
3193       ( l_cost_header_id
3194       , l_cost_type_id    /*INVCONV sschinch */
3195       , transaction_row.trans_date
3196       , transaction_row.trans_qty
3197       , transaction_row.trans_um
3198       , transaction_row.trans_qty * l_prev_trans_unit_cost --old_cost.unit_cost
3199       , transaction_row.transaction_id
3200       , l_unit_cost
3201       , old_cost.onhand_qty + transaction_row.trans_qty
3202       , old_cost.unit_cost
3203       , old_cost.onhand_qty
3204       ,  l_prev_new_cost_ind
3205       ,  transaction_row.lot_number
3206       , l_return_status
3207       );
3208 
3209       IF l_return_status <> 'S' THEN
3210         RETURN;
3211       END IF;
3212 
3213     ELSE
3214       RETURN;
3215     END IF;
3216   END IF;
3217 
3218 EXCEPTION
3219   WHEN OTHERS
3220   THEN fnd_file.put_line
3221        (fnd_file.log,'Failed in procedure process_reversals with error');
3222        fnd_file.put_line
3223        (fnd_file.log,SQLERRM);
3224        l_return_status := 'U';
3225 END process_reversals;
3226 
3227 --************************************************************************************************
3228 --*  Procedure Name : PROCESS_REVERSALS2
3229 --*
3230 --*  Desc: New Procedure for handling batch product reversal transactions
3231 --*   In process_reversals procedure we check whether the cost record in gmf_lot_costs
3232 --*   just prior to reversal transaction is its Original transaction.
3233 --*   If so, we will leap frog that record so as to reverse the effect of original transaction
3234 --*   in the average cost of the lot. gmf_material_lot_cost_txns will be pointed to the original
3235 --*   transaction record, so that subledger posts the entry accordingly. BUT we are not handling
3236 --*   the case if original transactions and reversal transactions don't come next to each other.
3237 --*   we process it as adjustment and it is a known issue.
3238 --*
3239 --*   To handle this issue, changed the complete logic of process_reversals.
3240 --*   No more leap frogging. We directly get the original transaction header_id and get the orig
3241 --*   transaction cost. With this we average it with the latest average cost available in order t
3242 --*   reverse the orig trx from the average cost.
3243 --*   If the current onhand_qty(before reversal) is less than or equal to the reversal trx qty
3244 --*   then we consider it as adjustment.
3245 --*
3246 --* Handled in 11.5.10 as part of Bug 4769118
3247 --*
3248 --*  Bug 9239944 Rajesh and Parag For MAcsteel
3249 --*  Procedure to Modified to handle WIP product returns, which are not reversals
3250 --*  WIP product returns identified as transaction_row.transaction_source_type_id = 5
3251 --*  AND transaction_row.transaction_action_id = 32
3252 --*  AND transaction_row.transaction_type_id = 17   and trans_row.reverse_id IS NULL
3253 --*  Find out the latest WIP completion record for same item, lot for the batch
3254 --*  Material line. If there is none, then we'll return Error
3255 --*  If found then create reversals with this transaction header
3256 --*  Bug 13528789 Pramod B.H.
3257 --*  Procedure modified to handle the issue of gmf_material_lot_cost_txns.TOTAL_TRANS_COST
3258 --*  incorrectly updated with positive amount for WIP Completion Return transaction.
3259 --************************************************************************************************
3260 
3261 
3262 PROCEDURE process_reversals2
3263 IS
3264 
3265   -- Bug 9239944
3266   CURSOR get_last_wipcompletion
3267   IS
3268       SELECT mmt.transaction_id
3269       FROM  gmf_lot_costs glc, gmf_material_lot_cost_txns gmt,
3270             mtl_material_transactions mmt
3271       WHERE glc.lot_number        = transaction_row.lot_number
3272       AND   glc.inventory_item_id = transaction_row.inventory_item_id
3273       AND   glc.organization_id   = transaction_row.orgn_id
3274       AND   glc.cost_type_id      = l_cost_type_id
3275       AND   glc.cost_date        <= NVL(transaction_row.trans_date, glc.cost_date)
3276       AND   gmt.cost_header_id    =  glc.header_id
3277       AND   mmt.transaction_id    = gmt.transaction_id
3278       AND   mmt.transaction_source_type_id = 5
3279       AND   mmt.transaction_action_id = 31
3280       AND   mmt.transaction_source_id = transaction_row.doc_id
3281       AND   mmt.trx_source_line_id = transaction_row.line_id
3282       ORDER By transaction_date desc ;
3283 
3284   CURSOR get_orig_trx(p_orig_trans_id NUMBER)
3285   IS
3286   SELECT DECODE(NVL(txns.new_cost_ind,0), 0, txns.cost_header_id, -txns.cost_header_id), txns.new_cost_ind
3287   FROM gmf_material_lot_cost_txns txns
3288   WHERE txns.transaction_id = p_orig_trans_id
3289     AND txns.cost_type_id = l_cost_type_id    -- PK 9069363 added cost_type_id and order by
3290   Order by cost_header_id desc;
3291 
3292   l_orig_trx_header_id gmf_lot_costs.header_id%TYPE;
3293   l_orig_trx_new_cost_ind NUMBER;
3294   l_orig_trx_trans_cost NUMBER;
3295   orig_trx_cost_tab l_cost_tab_type;
3296   l_txn_cost_tab    l_cost_tab_type; -- PK  Bug 14487964 14541507
3297 
3298   l_onhand_qty       gmf_lot_costs.onhand_qty%TYPE;
3299   l_header_id        gmf_lot_costs.header_id%TYPE;
3300   l_unit_cost        gmf_lot_costs.unit_cost%TYPE;
3301   l_cmpnt_cost       gmf_lot_cost_details.component_cost%TYPE;
3302   i NUMBER;
3303   procedure_name VARCHAR2(100);
3304   orig_completion_transid  NUMBER;       -- Bug 9239944
3305 
3306 
3307 BEGIN
3308    procedure_name := 'Process Reversals2';
3309     IF l_debug_level >= l_debug_level_medium  THEN     -- B9131983
3310      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
3311      fnd_file.put_line(fnd_file.log,'Reversals2 getting orig_trx for txn_id '||transaction_row.reverse_id);
3312     END IF;
3313 
3314   -- Bug 9239944
3315     IF transaction_row.reverse_id IS NULL THEN
3316       -- WIP completion Return for products
3317       orig_completion_transid := 0 ;
3318 
3319       IF l_debug_level >= l_debug_level_medium THEN
3320         fnd_file.put_line
3321         (fnd_file.log,'WIP Product Return transaction ID = '||to_char(transaction_row.transaction_id)
3322          || ' Batch No(Batch_ID) '||l_batch_no||'('||to_char(transaction_row.doc_id)||')'
3323          || ' Material Line Id ' ||  transaction_row.line_id
3324          || ' Quanity ' || transaction_row.trans_qty ||' UOM '||transaction_row.trans_um  );
3325 
3326       END IF;
3327 
3328       OPEN get_last_wipcompletion ;
3329       FETCH get_last_wipcompletion INTO orig_completion_transid ;
3330 
3331       IF(get_last_wipcompletion%NOTFOUND) THEN
3332         CLOSE get_last_wipcompletion;
3333         fnd_file.put_line(fnd_file.log,'Reversals2: Error in locating the Last WIP completion ');
3334         l_return_status := 'E';
3335         RETURN;
3336       END IF;
3337       CLOSE get_last_wipcompletion;
3338 
3339     ELSE
3340       -- it means that it is a TRUE Reversals
3341       orig_completion_transid := transaction_row.reverse_id ;
3342 
3343     END IF;
3344 
3345     OPEN get_orig_trx(orig_completion_transid);
3346     FETCH get_orig_trx INTO l_orig_trx_header_id, l_orig_trx_new_cost_ind;
3347 
3348     IF(get_orig_trx%NOTFOUND) THEN
3349       CLOSE get_orig_trx;
3350       fnd_file.put_line(fnd_file.log,'Reversals2: Error in locating the original trx '||transaction_row.reverse_id||' of transaction'||transaction_row.transaction_id);
3351       l_return_status := 'E';
3352       RETURN;
3353     END IF;
3354     CLOSE get_orig_trx;
3355 
3356     IF l_debug_level >= l_debug_level_medium  THEN   -- B9131983
3357       fnd_file.put_line(fnd_file.log,'After get_orig_trx cost header_id  '||l_orig_trx_header_id);
3358     END IF;
3359 
3360     /* l_orig_trx_header_id directly points to trx cost. and it can be negative */
3361     OPEN  lot_cost_detail_cursor(l_orig_trx_header_id);
3362     FETCH lot_cost_detail_cursor BULK COLLECT INTO orig_trx_cost_tab;
3363 
3364     IF(orig_trx_cost_tab.COUNT = 0) THEN
3365       CLOSE lot_cost_detail_cursor;
3366       fnd_file.put_line(fnd_file.log,'Reversals2: Error in locating the cost detail for orig_trx '||transaction_row.reverse_id);
3367       l_return_status := 'E';
3368       RETURN;
3369     END IF;
3370     CLOSE lot_cost_detail_cursor;
3371 
3372     /*Bug 13528789 - Moved this code here i.e out of IF...ELSE...condition below */
3373     l_orig_trx_trans_cost := 0;
3374     FOR i in 1..orig_trx_cost_tab.COUNT
3375     LOOP
3376         l_orig_trx_trans_cost := l_orig_trx_trans_cost + orig_trx_cost_tab(i).component_cost;
3377         orig_trx_cost_tab(i).cost_level := 0;  -- B13935001 , B13909479
3378     END LOOP;
3379 
3380     new_cost := old_cost;
3381     new_cost_tab := old_cost_tab;
3382 
3383     IF (old_cost.onhand_qty + transaction_row.trans_qty <= 0) THEN
3384         /* Replicate process_adjustments without create_material_transactions */
3385   -- This change is done to provide teh undo script for the current run of lot
3386   -- costing in final mode requested by MACSTEEL
3387 
3388         IF  old_cost.final_cost_flag = 1 AND old_cost.request_id <> l_request_id  --  l_final_run_flag = 0
3389         THEN
3390           clone_costs;
3391         END IF;
3392 
3393         UPDATE gmf_lot_costs
3394         SET    onhand_qty = onhand_qty + transaction_row.trans_qty,
3395         last_update_date = sysdate
3396         WHERE  header_id = old_cost.header_id;
3397 
3398         IF(SQL%ROWCOUNT = 1) THEN
3399           l_return_status := 'S';
3400         ELSE
3401           fnd_file.put_line(fnd_file.log,'Reversals2: Error in updating gmf_lot_costs for transaction_id: '||to_char(transaction_row.transaction_id));
3402           l_return_status := 'E';
3403           RETURN;
3404         END IF;
3405 
3406         l_header_id :=  ABS(l_orig_trx_header_id);  /*Bug 13528789 */
3407     ELSE
3408 
3409       l_txn_cost_tab := orig_trx_cost_tab; -- PK Bug 14487964 14541507
3410       merge_costs(orig_trx_cost_tab, transaction_row.trans_qty, new_cost.onhand_qty, 'A');
3411       create_cost_header
3412       ( p_item_id         => transaction_row.inventory_item_id
3413       , p_lot_number      => transaction_row.lot_number
3414       , p_orgn_id         => transaction_row.orgn_id
3415       , p_cost_type_id    => l_cost_type_id
3416       , p_unit_cost      => new_cost.unit_cost
3417       , p_cost_date      => transaction_row.trans_date
3418       , p_onhand_qty     => old_cost.onhand_qty + transaction_row.trans_qty
3419       , p_trx_src_type_id => transaction_row.transaction_source_type_id
3420       , p_txn_act_id       => transaction_row.transaction_action_id
3421       , p_doc_id          => transaction_row.doc_id
3422       , x_header_id      => l_header_id
3423       , x_unit_cost      => l_unit_cost
3424       , x_onhand_qty     => l_onhand_qty
3425       , x_return_status  => l_return_status
3426       );
3427 
3428 
3429       IF l_return_status = 'S' THEN
3430         IF l_debug_level >= l_debug_level_medium THEN
3431           fnd_file.put_line(fnd_file.log,'Reversals2: Creating new cost detail row');
3432         END IF;
3433 
3434         FOR i in 1..new_cost_tab.COUNT
3435         LOOP
3436           create_cost_detail
3437           ( p_header_id          => l_header_id
3438           , p_component_class_id => new_cost_tab(i).cost_cmpntcls_id
3439           , p_cost_analysis_code => new_cost_tab(i).cost_analysis_code
3440           , p_cost_level         => new_cost_tab(i).cost_level
3441           , p_component_cost     => new_cost_tab(i).component_cost
3442           , p_burden_ind         => new_cost_tab(i).burden_ind
3443           , x_return_status      => l_return_status
3444           );
3445         END LOOP;
3446 
3447         -- PK Bug 14487964 14541507 Also create negative details START.
3448 
3449         IF l_orig_trx_new_cost_ind = 1 THEN
3450 
3451           FOR i in 1..l_txn_cost_tab.COUNT
3452           LOOP
3453             create_cost_detail
3454             ( p_header_id          => -l_header_id
3455             , p_component_class_id => l_txn_cost_tab(i).cost_cmpntcls_id
3456             , p_cost_analysis_code => l_txn_cost_tab(i).cost_analysis_code
3457             , p_cost_level         => l_txn_cost_tab(i).cost_level
3458             , p_component_cost     => l_txn_cost_tab(i).component_cost
3459             , p_burden_ind         => l_txn_cost_tab(i).burden_ind
3460             , x_return_status      => l_return_status
3461             );
3462           END LOOP;
3463 
3464 
3465         END IF;
3466 
3467         -- Bug 14487964 14541507 Also create negative details END.
3468 
3469 
3470       END IF;
3471     END IF; /* End of if old_cost.onhand + trans_qty <= 0 */
3472 
3473     IF l_return_status = 'S' THEN
3474 
3475       /*Bug 13528789 - (commented) Moved this code above
3476       l_orig_trx_trans_cost := 0;
3477       FOR i in 1..orig_trx_cost_tab.COUNT
3478       LOOP
3479         l_orig_trx_trans_cost := l_orig_trx_trans_cost + orig_trx_cost_tab(i).component_cost;
3480       END LOOP;
3481       */
3482 
3483       IF l_debug_level >= l_debug_level_medium THEN
3484         fnd_file.put_line
3485         (fnd_file.log,'Reversals2: Creating new material cost transaction. The orig_header_id is '||l_orig_trx_header_id||' and l_header_id '||l_header_id);
3486       END IF;
3487       -- PK Bug 8730374 pass ABS when creating material transaction.
3488       create_material_transaction
3489       ( l_header_id  --ABS(l_orig_trx_header_id)     /*Bug 13528789 */
3490       , l_cost_type_id
3491       , transaction_row.trans_date
3492       , transaction_row.trans_qty
3493       , transaction_row.trans_um
3494       , transaction_row.trans_qty * l_orig_trx_trans_cost
3495       , transaction_row.transaction_id
3496       , new_cost.unit_cost
3497       , old_cost.onhand_qty + transaction_row.trans_qty
3498       , old_cost.unit_cost
3499       , old_cost.onhand_qty
3500       , l_orig_trx_new_cost_ind
3501       , transaction_row.lot_number
3502       , l_return_status
3503       );
3504 
3505 
3506       IF l_return_status <> 'S' THEN
3507         fnd_file.put_line(fnd_file.log,'Reversals2: Error in creating material_transaction for '||to_char(transaction_row.transaction_id));
3508         RETURN;
3509       END IF;
3510 
3511     ELSE
3512       fnd_file.put_line(fnd_file.log,'Reversals2: Error in creating Cost Header/Detail for transaction '||to_char(transaction_row.transaction_id));
3513       RETURN;
3514     END IF;
3515    IF l_debug_level >= l_debug_level_medium THEN
3516        fnd_file.put_line
3517        (fnd_file.log,'Leaving Procedure: '||procedure_name);
3518      END IF;
3519 
3520  EXCEPTION
3521  WHEN OTHERS THEN fnd_file.put_line
3522    (fnd_file.log,'Failed in procedure process_reversals2 with error');
3523     fnd_file.put_line(fnd_file.log,SQLERRM);
3524     l_return_status := 'U';
3525  END PROCESS_REVERSALS2;
3526 
3527     --**********************************************************************************************
3528     --*                                                                                            *
3529     --* Procedure to handle movements (TRNI/TRNR)                                                  *
3530     --*                                                                                            *
3531     --**********************************************************************************************
3532 
3533 PROCEDURE process_movement
3534 ( p_line_type   NUMBER
3535 , p_source_orgn NUMBER
3536 , p_target_orgn NUMBER
3537 , p_source_le   NUMBER
3538 , p_target_le   NUMBER
3539 , p_trans_date DATE
3540 )
3541 IS
3542   l_header_id    NUMBER;
3543   l_unit_cost    NUMBER;
3544   l_onhand_qty   NUMBER;
3545   l_method       VARCHAR2(10);
3546   l_ccc_id       NUMBER;
3547   --Bug 14749657
3548   --Expand l_a_code size from VARCHAR2(4) to CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE for POSCO fileds widen project
3549   l_a_code       CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE;
3550   l_no_of_rows   NUMBER;
3551   i              NUMBER;
3552   retval         NUMBER;
3553   l_msg_data     VARCHAR2(100);
3554   l_msg_count    NUMBER;
3555   l_var_return_status VARCHAR2(2);
3556   l_total_cost    NUMBER;
3557 
3558   l_src_qty       NUMBER;
3559   l_src_uom       VARCHAR2(3);
3560   l_cost_ratio    NUMBER;
3561   procedure_name VARCHAR2(100);
3562 
3563   --Bug 9561690
3564   l_source_ou     BINARY_INTEGER;
3565   l_target_ou     BINARY_INTEGER;
3566   l_prd_accrued  BOOLEAN := FALSE; -- Genesis
3567   l              NUMBER;
3568   l_org_code      VARCHAR2(100) := NULL;
3569   l_item_no        VARCHAR2(100) := NULL;
3570 
3571   CURSOR get_src_qty_uom IS
3572      select  NVL(mtln.primary_quantity, mmt.primary_quantity), -- B9131983 used NVL
3573             lcig.primary_uom_code
3574      from   mtl_material_transactions mmt,
3575             mtl_transaction_lot_numbers mtln,
3576             gmf_lot_costed_items_gt lcig
3577      where  mmt.transaction_id = transaction_row.transfer_transaction_id
3578        AND  mmt.transaction_id = mtln.transaction_id
3579        AND  mmt.inventory_item_id = lcig.inventory_item_id
3580        AND  mmt.organization_id   = lcig.organization_id;
3581 
3582 BEGIN
3583    procedure_name := 'Process Movement';
3584    IF l_debug_level >= l_debug_level_medium THEN
3585        fnd_file.put_line
3586        (fnd_file.log,'Entered Procedure: '||procedure_name);
3587    END IF;
3588      -- This procedure is only called when the source and target organizations differ.
3589 
3590   -- If this is a debit on the source organization (line type = -1) this is equivalent to an adjustment
3591   -- and we must assume that there is a cost there already (in old_cost and old_cost_tab).
3592 
3593   -- If it is the credit on a target organization then we have to locate the costs in the source organization
3594   -- and create or modify the cost in the target organization with them.
3595 
3596   -- A complication here is that TRNI/TRNR transaction pairs can be for different companies, and this means
3597   -- the the company in the sending transaction might not use the same currency as the company in the
3598   -- receiving transaction.
3599 
3600   IF p_line_type = -1 THEN
3601     -- This is the debit on the source organization.
3602     process_adjustment;
3603 
3604   ELSE
3605     -- This is the credit on the target organization. Retrieve the costs from the source organization.
3606     -- This section also caters for line type 0 (used in PORC transactions for internal orders)
3607 
3608     IF l_debug_level >= l_debug_level_medium THEN
3609       fnd_file.put_line(fnd_file.log,'At start of Process Movement, new_cost_tab is:');
3610       IF new_cost_tab.EXISTS(1) THEN
3611         FOR k IN 1 .. new_cost_tab.COUNT
3612         LOOP
3613           fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
3614           fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
3615           fnd_file.put_line(fnd_file.log,'Level ['||k||']: '||new_cost_tab(k).cost_level);
3616           fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
3617           fnd_file.put_line(fnd_file.log,'====================================');
3618         END LOOP;
3619       ELSE
3620         fnd_file.put_line(fnd_file.log,'EMPTY');
3621       END IF;
3622     END IF;
3623     -- As there is a chance that the two organizations could be in separate companies we need to do something
3624     -- to ensure we can locate a cost for the sending transaction. If the two companies involved are the same
3625     -- we can simply query the gmf_lot_costs table. If not we have to do dig around for costs.
3626 
3627 -- Bug 13038249 Grupo viz changes start
3628         l_source_ou := org_coll_tab(p_source_orgn).ou_id  ;
3629         l_target_ou := org_coll_tab(p_target_orgn).ou_id  ;
3630 
3631         IF l_debug_level >= l_debug_level_medium THEN
3632           fnd_file.put_line(fnd_file.log,'l_source_ou =' || l_source_ou || ' l_target_ou=' || l_target_ou);
3633         END IF;
3634 
3635     --Begin Bug 9561690
3636 /*    SELECT to_number(org_information3)
3637     INTO   l_source_ou
3638     FROM   hr_organization_information
3639     WHERE  organization_id          =  p_source_orgn
3640     AND    org_information_context  = 'Accounting Information';
3641 
3642     SELECT to_number(org_information3)
3643     INTO   l_target_ou
3644     FROM   hr_organization_information
3645     WHERE  organization_id         =  p_target_orgn
3646     AND    org_information_context = 'Accounting Information';
3647     --End Bug 9561690
3648 
3649  */
3650 -- Bug 13038249 Grupo viz changes END
3651 
3652       --Bug 9561690 Added OR condition.
3653       IF ( (p_source_le <> p_target_le) OR (l_source_ou <> l_target_ou) ) THEN
3654 
3655          -- Bug Bug 9356358 / 12.1 Bug 9400419 Query transfer price and create cost with default material component
3656          SELECT transfer_price INTO l_unit_cost
3657          FROM mtl_material_transactions
3658          WHERE transaction_id = transaction_row.transaction_id;
3659 
3660        --   Bug 14392602 Use cost component set up at Item level before Fiscal policy
3661        /*  SELECT mtl_cmpntcls_id, mtl_analysis_code INTO l_ccc_id, l_a_code
3662          FROM   gmf_fiscal_policies
3663          WHERE  legal_entity_id = p_target_le;      */
3664 
3665          OPEN component_class_cursor
3666            (l_le_id, transaction_row.inventory_item_id, transaction_row.orgn_id,transaction_row.trans_date);
3667          FETCH component_class_cursor INTO l_ccc_id, l_a_code, dummy;
3668          CLOSE component_class_cursor;
3669 
3670          IF new_cost_tab.EXISTS(1) THEN
3671             new_cost_tab.delete;
3672          END IF;
3673 
3674          new_cost_tab(1) := SYSTEM.gmf_cost_type ( l_ccc_id, l_a_code, 0, l_unit_cost, 0);
3675          new_cost.unit_cost := l_unit_cost;     /* Bug 11929896 */
3676 
3677           prd_cost := new_cost;     /* Bug 11929896 */
3678           prd_cost_tab := new_cost_tab;    /* Bug 11929896 */
3679 
3680 
3681          IF l_debug_level >= l_debug_level_medium THEN
3682              fnd_file.put_line(fnd_file.log,'Intercompany transfer Transaction_id '||transaction_row.transaction_id||' Transfer Price '||l_unit_cost);
3683              fnd_file.put_line(fnd_file.log,'Fiscal Policy cost component '||l_ccc_id||' Analysis Code '||l_a_code||' Origin Le '||p_source_le||' Destination Le '||p_target_le);
3684          END IF;
3685 
3686          -- PK Bug Bug 9356358 / 12.1 Bug 9400419 Comment out Old code
3687 
3688 /*        IF (1 = gmf_cmcommon.Get_Process_Item_Cost(p_api_version               => 1.0
3689                                                    ,p_init_msg_list             => 'T'
3690                                                    ,x_return_status             => l_var_return_status
3691                                                    ,x_msg_count                 => l_msg_count
3692                                                   ,x_msg_data                  => l_msg_data
3693                                                   ,p_inventory_item_id         => transaction_row.inventory_item_id
3694                                                   ,p_organization_id           =>  p_source_orgn
3695                                                   ,p_transaction_date          => p_trans_date
3696                                                   ,p_detail_flag               => 4
3697                                                   ,p_cost_method               => l_method
3698                                                   ,p_cost_component_class_id   =>l_ccc_id
3699                                                   ,p_cost_analysis_code        =>  l_a_code
3700                                                   ,x_total_cost                => l_total_cost
3701                                                   ,x_no_of_rows                => l_no_of_rows
3702                                                   ,p_lot_number                => transaction_row.lot_number
3703                                                  ,p_transaction_id            => NULL
3704                                                 ) ) THEN
3705           IF new_cost_tab.EXISTS(1) THEN
3706             new_cost_tab.delete;
3707           END IF;
3708 
3709           FOR i IN 1..l_no_of_rows
3710           LOOP
3711             gmf_cmcommon.get_multiple_cmpts_cost
3712                              (v_index             => i
3713                              ,v_cost_cmpntcls_id  => l_ccc_id
3714                              ,v_cost_analysis_code=> l_a_code
3715                              ,v_cmpnt_amt         => l_unit_cost
3716                              ,v_retrieve_ind      => 4
3717                              ,v_status            => retval
3718                              );
3719             IF retval <> 0  THEN
3720               l_return_status := 'E';
3721               fnd_file.put_line
3722               (fnd_file.log,'ERROR: Failed to retrieve single cost component in source organization '||l_org_tab(p_source_orgn));
3723               RETURN;
3724             ELSE
3725               -- Procedure doesn't return burden ind so coerce it to zero.
3726               new_cost_tab(i) := SYSTEM.gmf_cost_type ( l_ccc_id, l_a_code, 0, l_unit_cost, 0);
3727             END IF;
3728           END LOOP;
3729         ELSE
3730           IF new_cost_tab.EXISTS(1) THEN
3731             new_cost_tab.delete;
3732           END IF;
3733           OPEN  component_class_cursor( p_target_le,transaction_row.inventory_item_id,p_target_orgn,p_trans_date);
3734           FETCH component_class_cursor INTO l_ccc_id,l_a_code,dummy;
3735           IF (component_class_cursor%NOTFOUND) THEN
3736             l_return_status := 'E';
3737             fnd_file.put_line
3738             (fnd_file.log,'ERROR: Failed to retrieve cost multiple components in source organization '||l_org_tab(p_source_orgn));
3739             CLOSE component_class_cursor;
3740             RETURN;
3741           END IF;
3742         END IF;  */
3743         -- PK Bug Bug 9356358 / 12.1 Bug 9400419 Comment out Old code. Else below is for IF (p_source_le <> p_target_le) old code for within company.
3744        ELSE -- Else part of (p_source_le <> p_target_le)
3745             new_cost.header_id := NULL;
3746 
3747 -- Bug 13038249 Grupo viz changes fetch only recent data first
3748 
3749             OPEN  lot_cost_cursor_1(p_source_orgn, transaction_row.inventory_item_id, transaction_row.lot_number, p_trans_date,l_cost_type_id );
3750             FETCH lot_cost_cursor_1 INTO new_cost;
3751             CLOSE lot_cost_cursor_1;
3752 
3753             IF new_cost.header_id IS NULL THEN
3754 
3755               OPEN  lot_cost_cursor(p_source_orgn, transaction_row.inventory_item_id, transaction_row.lot_number, p_trans_date,l_cost_type_id );
3756               FETCH lot_cost_cursor INTO new_cost;
3757               CLOSE lot_cost_cursor;
3758 
3759             END IF;
3760 
3761             IF new_cost.header_id IS NULL THEN
3762              l_org_code := NULL;
3763              l_item_no := NULL;
3764             organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_org_code, l_item_no) ;
3765               fnd_file.put_line
3766               ( fnd_file.log,'ERROR: Unable to locate cost header for organization: '||l_org_tab(p_source_orgn)
3767               ||  ', item ID: '||transaction_row.inventory_item_id||'('||l_item_no||')'
3768               ||', lot Number: '||transaction_row.lot_number
3769               );
3770               l_return_status := 'E';
3771               RETURN;
3772             END IF;
3773 
3774             OPEN  lot_cost_detail_cursor ( new_cost.header_id );
3775             FETCH lot_cost_detail_cursor BULK COLLECT INTO new_cost_tab;
3776             CLOSE lot_cost_detail_cursor;
3777           END IF;
3778 
3779 
3780           IF l_debug_level >= l_debug_level_medium THEN
3781             fnd_file.put_line(fnd_file.log,'After reading costs from source organization, new_cost_tab is:');
3782             FOR k IN 1 .. new_cost_tab.COUNT
3783             LOOP
3784               fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
3785               fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
3786               fnd_file.put_line(fnd_file.log,'Level ['||k||']: '||new_cost_tab(k).cost_level);
3787               fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
3788               fnd_file.put_line(fnd_file.log,'====================================');
3789             END LOOP;
3790           END IF;
3791 
3792 
3793           IF NOT new_cost_tab.EXISTS(1) THEN
3794           l_org_code := NULL;
3795           l_item_no := NULL;
3796             organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_org_code, l_item_no) ;
3797             fnd_file.put_line
3798             ( fnd_file.log,'ERROR: Unable to locate source cost details for organization: '||l_org_tab(p_source_orgn)
3799             ||', inventory item ID: '||transaction_row.inventory_item_id||'('||l_item_no||')'
3800             ||', lot number: '||transaction_row.lot_number /* INVCONV sschinch */
3801             );
3802             l_return_status := 'E';
3803             RETURN;
3804           END IF;
3805 
3806           -- B13935001 Added this IF. get_src_qty_uom cursor is required only for transfers within LE and OU.
3807           IF ( (p_source_le = p_target_le) AND (l_source_ou = l_target_ou) ) THEN
3808 
3809             OPEN get_src_qty_uom;
3810             FETCH get_src_qty_uom INTO l_src_qty, l_src_uom;
3811             IF get_src_qty_uom%NOTFOUND THEN
3812                 fnd_file.put_line( fnd_file.log,'ERROR: Unable to locate source primary quantity and primary uom for the transfer transaction id: '||
3813                    transaction_row.transfer_transaction_id);
3814                   l_return_status := 'E';
3815                CLOSE get_src_qty_uom;
3816                RETURN;
3817             END IF;
3818             CLOSE get_src_qty_uom;
3819 
3820             IF l_src_uom <> transaction_row.trans_um THEN
3821               IF l_debug_level >= l_debug_level_medium THEN
3822                 fnd_file.put_line( fnd_file.log,'Source primary uom: '||l_src_uom||' and Receiving primary uom: '||transaction_row.trans_um||
3823                    ' are different');
3824               END IF;
3825 
3826               IF transaction_row.trans_qty <> 0 THEN
3827                  l_cost_ratio := (l_src_qty * -1)/transaction_row.trans_qty;
3828 
3829                IF l_debug_level >= l_debug_level_medium THEN
3830                  fnd_file.put_line(fnd_file.log,'Source primary qty: '||l_src_qty||', Receiving primary qty: '||transaction_row.trans_qty||
3831                  ', cost ratio: '||l_cost_ratio);
3832                END IF;
3833 
3834               ELSE
3835                  l_cost_ratio := 0;
3836 
3837                IF l_debug_level >= l_debug_level_medium THEN
3838                   fnd_file.put_line(fnd_file.log,'Transaction qty is zero, so making cost ratio as zero');
3839                END IF;
3840 
3841               END IF;
3842 
3843               FOR i IN 1 .. new_cost_tab.COUNT
3844               LOOP
3845                 new_cost_tab(i).component_cost := new_cost_tab(i).component_cost * l_cost_ratio;
3846               END LOOP;
3847 
3848             ELSE  -- l_src_uom <> transaction_row.trans_um
3849               IF l_debug_level >= l_debug_level_medium THEN
3850                 fnd_file.put_line( fnd_file.log,'Source primary uom: '||l_src_uom||' and Receiving primary uom: '||transaction_row.trans_um||
3851                  ' are same');
3852               END IF;
3853               l_cost_ratio := 1;
3854             END IF;
3855           ELSE  -- ( (p_source_le = p_target_le) AND (l_source_ou = l_target_ou) )
3856             IF l_debug_level >= l_debug_level_medium THEN
3857                 fnd_file.put_line( fnd_file.log,'Transfer Across LE and/or OU. No need to check source org cost or UOM.');
3858             END IF;
3859             l_cost_ratio := 1;
3860 
3861           END IF; -- B13935001
3862 
3863           process_burdens;
3864 
3865           IF l_return_status <> 'S' THEN
3866             RETURN;
3867           END IF;
3868 
3869           -- At this point old_cost_tab holds the costs of the lot in the target whse (if any exist),
3870           -- new_cost_tab holds the costs of the lot in the source whse (these must exist) and
3871           -- l_burdens_cost_tab holds the costs of any burdens set up against the target whse (if any exist).
3872           -- We now need to see if the two organizations belong to companies that share a common
3873           -- currency. If we find that they don't then we have to convert the source costs to the currency
3874           -- used in the target before we start the merging them.
3875 
3876           -- PK Bug Bug 9356358 / 12.1 Bug 9400419 this code is not required. Transfer_price in MMT is already in receiving org currency.
3877 /*          IF p_source_le <> p_target_le THEN
3878             SELECT s.base_currency_code,t.base_currency_code
3879             INTO   l_from_ccy_code, l_to_ccy_code
3880             FROM   gmf_fiscal_policies s,
3881                    gmf_fiscal_policies t
3882             WHERE  s.legal_entity_id = p_source_le
3883             AND    t.legal_entity_id  = p_target_le;
3884 
3885             IF l_from_ccy_code <> l_to_ccy_code THEN
3886 
3887               l_exchange_rate := gl_currency_api.get_closest_rate(x_from_currency => l_from_ccy_code,
3888                                                                x_to_currency   => l_to_ccy_code,
3889                                                                x_conversion_date => transaction_row.trans_date,
3890                                                                x_max_roll_days   => 0);
3891 
3892 
3893 
3894               IF l_error_status <> 0 THEN
3895                 fnd_file.put_line
3896                 ( fnd_file.log
3897                  , 'ERROR: Unable to find exchange rate from '||l_from_ccy_code
3898                  ||' to '||l_to_ccy_code
3899                  ||' on '||transaction_row.trans_date
3900                 );
3901                 l_return_status := 'E';
3902                 RETURN;
3903               END IF;
3904 
3905              FOR i IN 1 .. new_cost_tab.COUNT
3906               LOOP
3907                 new_cost_tab(i).component_cost := new_cost_tab(i).component_cost * l_exchange_rate;
3908               END LOOP;
3909             END IF; -- l_from_ccy_code <> l_to_ccy_code
3910           END IF;  -- p_source_le <> p_target_le */
3911 
3912           -- PK Bug Bug 9356358 / 12.1 Bug 9400419 this code is not required. Transfer_price in MMT is already in receiving org currency.
3913 
3914           -- PK Genesis Bug. We want to update all costs in prd costs to level 1 for interorg transfers. Only Burdens will be level 0.
3915           prd_cost := new_cost;
3916           prd_cost_tab := new_cost_tab;
3917 
3918             FOR i IN 1..prd_cost_tab.COUNT
3919             LOOP
3920               prd_cost_tab(i).cost_level :=  1;
3921             END LOOP;
3922 
3923           IF l_burdens_total <> 0 THEN
3924             IF l_debug_level >= l_debug_level_medium THEN
3925               fnd_file.put_line
3926                 (fnd_file.log,'Combining burden costs');
3927             END IF;
3928 
3929             merge_costs( l_burden_costs_tab
3930                        , 1
3931                        , 1
3932                       , 'C'
3933                        );
3934             -- PK Bug 12954269 new_cost.unit_cost already burdens added from merge_costs above. Commenting out next line.
3935             -- new_cost.unit_cost := new_cost.unit_cost + l_burdens_total;
3936 
3937           /*  Genesis Bug Just for burdens for prd_cost_tab only do not call merge costs use similar code
3938             Replacement code below This is copied from merge costs for C mode with the difference we need to add to prd_cost_tab as well
3939             we have copied prd cost before merging and updated level to 1
3940             We also need to create negative details all the time */
3941 
3942             IF l_burden_costs_tab.EXISTS(1) THEN  -- Can be eliminated? we would not come here unless burdens exist
3943 
3944               IF l_debug_level >= l_debug_level_high THEN
3945 
3946                 fnd_file.put_line(fnd_file.log,'Before merge prd_cost_tab is:');
3947                 FOR k IN 1 .. prd_cost_tab.COUNT
3948                 LOOP
3949                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||prd_cost_tab(k).cost_cmpntcls_id);
3950                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||prd_cost_tab(k).cost_analysis_code);
3951                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||prd_cost_tab(k).cost_level);
3952                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||prd_cost_tab(k).component_cost);
3953                   fnd_file.put_line(fnd_file.log,'====================================');
3954                 END LOOP;
3955 
3956                 fnd_file.put_line(fnd_file.log,'Before merge l_burden_costs_tab is:');
3957                 FOR k IN 1 .. l_burden_costs_tab.COUNT
3958                 LOOP
3959                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||l_burden_costs_tab(k).cost_cmpntcls_id);
3960                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||l_burden_costs_tab(k).cost_analysis_code);
3961                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||l_burden_costs_tab(k).cost_level);
3962                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||l_burden_costs_tab(k).component_cost);
3963                   fnd_file.put_line(fnd_file.log,'====================================');
3964                 END LOOP;
3965 
3966               END IF; -- End printing cost tabs for debug mode
3967 
3968                 FOR k IN 1 .. l_burden_costs_tab.COUNT
3969                 LOOP
3970                   -- Do we even need next loop. level in prd_cost_tab is updated to 1. Only level 0 costs are burdens.
3971                   -- So match is not going to be found. But can two resources have overhead under same component?
3972                   -- If so it is better to have this check. does not hurt to check anyways.
3973 
3974                   FOR l in 1..prd_cost_tab.COUNT
3975                   LOOP
3976                     l_prd_accrued := FALSE;
3977                     IF  prd_cost_tab(l).cost_cmpntcls_id = l_burden_costs_tab(k).cost_cmpntcls_id
3978                     AND prd_cost_tab(l).cost_analysis_code = l_burden_costs_tab(k).cost_analysis_code
3979                     AND prd_cost_tab(l).cost_level = l_burden_costs_tab(k).cost_level
3980                     THEN
3981                       prd_cost_tab(l).component_cost :=  prd_cost_tab(l).component_cost + l_burden_costs_tab(k).component_cost;
3982                       l_prd_accrued := TRUE;
3983                       EXIT;
3984                     END IF;
3985                   END LOOP;
3986 
3987                   IF NOT l_prd_accrued THEN
3988                     l := prd_cost_tab.count+1;
3989                     prd_cost_tab(l) := SYSTEM.gmf_cost_type
3990                                        ( l_burden_costs_tab(k).cost_cmpntcls_id
3991                                        , l_burden_costs_tab(k).cost_analysis_code
3992                                        , 0
3993                                        , l_burden_costs_tab(k).component_cost
3994                                        , 0
3995                                        );
3996 
3997                   END IF;
3998 
3999                 END LOOP;
4000               END IF; -- l_burden_costs_tab.EXISTS(1)
4001 
4002               IF l_debug_level >= l_debug_level_high THEN
4003 
4004                 fnd_file.put_line(fnd_file.log,'After Burden merge prd_cost_tab is:');
4005 
4006                 FOR k IN 1 .. prd_cost_tab.COUNT
4007                 LOOP
4008                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||prd_cost_tab(k).cost_cmpntcls_id);
4009                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||prd_cost_tab(k).cost_analysis_code);
4010                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||prd_cost_tab(k).cost_level);
4011                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||prd_cost_tab(k).component_cost);
4012                   fnd_file.put_line(fnd_file.log,'====================================');
4013                 END LOOP;
4014               END IF;
4015 
4016 
4017           END IF; -- l_burdens_total <> 0
4018 
4019           -- End Genesis Bug.
4020 
4021           -- We need to preserve the 'new' cost so that we can write its details separately to the
4022           -- merged costs. This preserved cost is also what is used when we write the new material
4023           -- lot cost transaction. Bug 3578680
4024           -- Gnesis Bug Moved next two lines up
4025           -- prd_cost := new_cost;
4026           -- prd_cost_tab := new_cost_tab;
4027           -- Genesis bug.
4028 
4029           IF l_debug_level >= l_debug_level_medium THEN
4030             fnd_file.put_line
4031              (fnd_file.log,'Aggregating old costs with new costs');
4032           END IF;
4033 
4034           merge_costs( old_cost_tab
4035                , old_cost.onhand_qty
4036                , transaction_row.trans_qty
4037                , 'A'
4038                );
4039           create_cost_header
4040            ( p_item_id         => transaction_row.inventory_item_id
4041             , p_lot_number      => transaction_row.lot_number
4042             , p_orgn_id         => transaction_row.orgn_id
4043             , p_cost_type_id    =>  l_cost_type_id
4044             , p_unit_cost       => new_cost.unit_cost
4045             , p_cost_date       => transaction_row.trans_date
4046             , p_onhand_qty      => transaction_row.trans_qty + old_cost.onhand_qty
4047             , p_trx_src_type_id => transaction_row.transaction_source_type_id
4048             , p_txn_act_id       => transaction_row.transaction_action_id
4049             , p_doc_id          => transaction_row.doc_id
4050             , x_header_id       => l_header_id
4051             , x_unit_cost       => l_unit_cost
4052             , x_onhand_qty      => l_onhand_qty
4053             , x_return_status   => l_return_status
4054           );
4055 
4056           IF l_return_status = 'S' THEN
4057             IF l_debug_level >= l_debug_level_medium THEN
4058               fnd_file.put_line
4059                 (fnd_file.log,'Creating new cost detail rows');
4060             END IF;
4061 
4062             -- Bug 3388974
4063             new_cost.header_id := l_header_id;
4064             new_cost.unit_cost := l_unit_cost;
4065             new_cost.onhand_qty := l_onhand_qty;
4066 
4067             FOR i IN 1..new_cost_tab.COUNT
4068             LOOP
4069               create_cost_detail
4070                ( l_header_id
4071                 , new_cost_tab(i).cost_cmpntcls_id
4072                 , new_cost_tab(i).cost_analysis_code
4073                 , 0
4074                 , new_cost_tab(i).component_cost
4075                 , 0
4076                 , l_return_status
4077                );
4078 
4079              IF l_return_status <> 'S' THEN
4080                RETURN;
4081              END IF;
4082            END LOOP;
4083 
4084            IF l_debug_level >= l_debug_level_medium THEN
4085              fnd_file.put_line
4086                (fnd_file.log,'Creating new material cost transaction');
4087            END IF;
4088 
4089   /* Genesis Bug Always create negative details
4090 
4091            IF NOT old_cost_tab.EXISTS(1) THEN
4092              IF l_debug_level >= l_debug_level_high THEN
4093                fnd_file.put_line (fnd_file.log,'AAAAA');
4094              END IF;
4095 
4096              create_material_transaction
4097              ( new_cost.header_id
4098                , l_cost_type_id
4099                , transaction_row.trans_date
4100                , transaction_row.trans_qty
4101                , transaction_row.trans_um
4102                , new_cost.onhand_qty * new_cost.unit_cost
4103                , transaction_row.transaction_id
4104                , new_cost.unit_cost
4105                , transaction_row.trans_qty
4106                , NULL
4107                , NULL
4108                , NULL
4109                ,transaction_row.lot_number
4110                , l_return_status
4111              );
4112 
4113            ELSE
4114       Genesis Bug Always create negative details  */
4115 
4116              IF l_debug_level >= l_debug_level_high THEN
4117                fnd_file.put_line (fnd_file.log,'BBBBB');
4118              END IF;
4119              create_material_transaction
4120              ( new_cost.header_id
4121                , l_cost_type_id
4122                , transaction_row.trans_date
4123                , transaction_row.trans_qty
4124                , transaction_row.trans_um
4125                , transaction_row.trans_qty * (prd_cost.unit_cost + nvl(l_burdens_total, 0)) -- Bug 3578680
4126                , transaction_row.transaction_id
4127                , new_cost.unit_cost
4128                , old_cost.onhand_qty + transaction_row.trans_qty
4129                , old_cost.unit_cost
4130                , old_cost.onhand_qty
4131                , 1
4132                ,transaction_row.lot_number
4133                , l_return_status
4134              );
4135 
4136              -- Bug 3578680
4137              FOR i IN 1..prd_cost_tab.COUNT
4138              LOOP
4139                create_cost_detail
4140                ( -l_header_id
4141                , prd_cost_tab(i).cost_cmpntcls_id
4142                , prd_cost_tab(i).cost_analysis_code
4143                , prd_cost_tab(i).cost_level     -- Genesis instead of hard coded 0
4144                , prd_cost_tab(i).component_cost
4145                , 0
4146                , l_return_status
4147               );
4148 
4149               IF l_return_status <> 'S' THEN
4150                 RETURN;
4151               END IF;
4152             END LOOP;
4153            -- END IF; Genesis Bug Always create negative details
4154           IF l_return_status <> 'S' THEN
4155             RETURN;
4156           END IF;
4157          END IF; /* get cost */
4158      END IF; /* Line type */
4159    IF l_debug_level >= l_debug_level_medium THEN
4160        fnd_file.put_line
4161        (fnd_file.log,'Leaving Procedure: '||procedure_name);
4162      END IF;
4163   EXCEPTION
4164   WHEN OTHERS THEN
4165   fnd_file.put_line
4166        (fnd_file.log,'Failed in procedure Process Movement with error');
4167        fnd_file.put_line
4168        (fnd_file.log,SQLERRM);
4169        l_return_status := 'U';
4170 
4171 END process_movement;
4172 
4173 --********************************************************************************************************
4174 --*    Procedure Name : PROCESS_RECEIPT
4175 --*
4176 --*     Description :
4177 --*               Procedure to handle Purchase Order Receipt.
4178 --*
4179 --* HISTORY
4180 --*
4181 --*   06-Jan-2004 Dinesh Vadivel Bug# 4095937
4182 --*       The Lot Cost Process calculates the Exchg Rate as on the Receipt Header Date
4183 --*        whereas the Subledger uses the "Exchg Rate Date". Modified the Lot Actual Cost Process
4184 --*        to use the rcv_transactions.CURRENCY_CONVERSION_RATE as the Exchg Rate .
4185 --*   2-Aug-2007 Bug 6320304/5953977 - Non-recoverable taxes ME, as part of this added unit of
4186 --*        nonrecoverable tax to the unit cost.
4187 --*
4188 --********************************************************************************************************
4189 
4190 PROCEDURE process_receipt
4191 IS
4192   document_code   rcv_transactions.source_document_code%TYPE;
4193   source_orgn_id    NUMBER;
4194   target_orgn_id    NUMBER;
4195   source_le_id      NUMBER;
4196   target_le_id      NUMBER;
4197   l_shipped_date DATE;
4198   procedure_name VARCHAR2(100);
4199   l_rcv_transaction_id NUMBER;
4200 BEGIN
4201       procedure_name := 'Process Receipt';
4202      IF l_debug_level >= l_debug_level_medium THEN
4203        fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
4204        fnd_file.put_line
4205       (fnd_file.log,'PO Receipt found: transaction ID = '||to_char(transaction_row.transaction_id));
4206         fnd_file.put_line
4207         (fnd_file.log,' Retrieving receipt details');
4208       END IF;
4209 
4210       -- The pointers in the inventory transaction link to the receipt as follows:
4211       -- DOC_ID is the SHIPMENT_HEADER_ID in rcv_shipment_headers
4212       -- LINE_ID is the TRANSACTION_ID in rcv_transactions
4213 
4214       -- From the row in rcv_transactions the po_unit_price (expressed in price_um so
4215       -- we need to convert it to item_um) is the source of the cost. There is also the
4216       -- received qty (expressed in recv_um so it might need converting too, although the
4217       -- OPM inventory transaction should have the number we need already). There are
4218       -- pointers in the receiving transaction to the po_header, po_line and the receipt
4219       -- line too.
4220 
4221       -- The cost component class for the lot cost details is found in table cm_cmpt_mtl
4222       -- with a default from the fiscal policy if there isn't an entry in the table.
4223 
4224       -- Finally we have to keep in mind that a PORC transaction could be for a -ve quantity
4225       -- if goods are being returned to a vendor. In this respect, we shouldn't ever encounter
4226       -- the situation where uncosted goods are being sent back as we must have received them
4227       -- beforehand.
4228 
4229       -- Now using source_doc_unit_of_measure instead of unit_of_measure
4230 
4231       -- B3514108, added source_document_code to select statement so we can see if this PORC is
4232       -- actually an RMA. Note that source_doc_unit_of_measure is NULL for an RMA so we have to
4233       -- perform an outer join.
4234 
4235 	/* Bug 6320304/5953977        SELECT t.po_unit_price, */
4236       SELECT  t.po_unit_price + DECODE(nvl(pda.quantity_ordered,0),0,0, (nvl(pda.nonrecoverable_tax,0)/pda.quantity_ordered)),
4237                t.currency_code,
4238                t.quantity,
4239                u.uom_code,
4240                t.source_document_code,
4241                NVL(t.currency_conversion_rate,1),
4242                t.transaction_id
4243       INTO    receipt_unit_cost,
4244               receipt_ccy,
4245               receipt_qty,
4246               receipt_uom,
4247               document_code,
4248               l_exchange_rate,
4249               l_rcv_transaction_id
4250       FROM    rcv_transactions t, mtl_units_of_measure u, mtl_material_transactions mmt -- jboppana
4251       		, po_distributions_all pda
4252       WHERE   t.source_doc_unit_of_measure = u.unit_of_measure(+)
4253       AND     t.transaction_id = mmt.rcv_transaction_id
4254     --  AND     mmt.transaction_source_id = transaction_row.doc_id
4255       AND    t.po_distribution_id = pda.po_distribution_id (+)  /* Bug 6320304/5953977 */
4256       AND     mmt.transaction_id = transaction_row.transaction_id     ;
4257 
4258       -- If we're processing an RMA treat it as an adjustment
4259       -- and make a swift exit.
4260       /******** Bug  4038722 - Dinesh Vadivel - Convert to Base Currency, if Receipt currency is different **********/
4261       /*     Added the query to pick up the base currency in the main lot_cost_rollup procedure  */
4262       IF l_debug_level >= l_debug_level_high THEN
4263         fnd_file.put_line
4264         (fnd_file.log,' receipt details found rcv transaction id'||l_rcv_transaction_id);
4265       END IF;
4266 
4267       IF ( l_base_ccy_code <>  receipt_ccy)  THEN /* Check if the receipt currency is the same as the base currency */
4268         IF l_debug_level >= l_debug_level_medium THEN
4269           fnd_file.put_line   (fnd_file.log,' Converting Receipt_unit_cost : '||receipt_unit_cost||' Receipt Currency : '||receipt_ccy||
4270                                ' to Base Currency : '||l_base_ccy_code||'. New_receipt_unit_cost : '||receipt_unit_cost * l_exchange_rate||'. Exchange Rate is : '||l_exchange_rate);
4271         END IF;
4272 
4273         receipt_unit_cost := receipt_unit_cost * l_exchange_rate;
4274 
4275 
4276      END IF;
4277 
4278       /******** Bug  4038722 - Dinesh Vadivel - Convert to Base Currency, if Receipt currency is different - End **********/
4279 
4280 
4281       IF document_code = 'RMA' THEN
4282         IF l_debug_level >= l_debug_level_medium THEN
4283           fnd_file.put_line
4284           (fnd_file.log,' Processing PORC txn as an RMA');
4285         END IF;
4286         process_adjustment;
4287         RETURN;
4288       END IF;
4289 
4290       -- End of B3514108 changes
4291 
4292       -- B3513668 If this is an internal order treat it as a transfer or movement
4293 
4294       IF document_code = 'REQ' THEN
4295         SELECT
4296           mmt.organization_id,
4297           mmt.transfer_organization_id,
4298           hoi1.org_information2,
4299           hoi2.org_information2,
4300           r.shipped_date
4301         INTO
4302           target_orgn_id,source_orgn_id, source_le_id, target_le_id, l_shipped_date
4303         FROM
4304           rcv_transactions t,
4305           mtl_material_transactions mmt,
4306           rcv_shipment_headers r,
4307           rcv_shipment_lines rsl,
4308           po_headers_all poh,
4309           hr_organization_information hoi1,
4310           hr_organization_information hoi2
4311         WHERE
4312                 t.source_document_code = 'REQ'
4313         AND     t.transaction_id = mmt.rcv_transaction_id
4314         AND     mmt.transaction_id = transaction_row.transaction_id
4315         AND     mmt.organization_id = hoi2.organization_id
4316         AND     mmt.transfer_organization_id = hoi1.organization_id
4317         AND     hoi1.org_information_context = 'Accounting Information'
4318         AND     hoi2.org_information_context = 'Accounting Information'
4319         AND     t.shipment_header_id = r.shipment_header_id
4320         AND     r.receipt_source_code in ('INTERNAL ORDER')
4321         AND     t.shipment_line_id = rsl.shipment_line_id
4322         AND     t.po_header_id = poh.po_header_id (+);
4323 
4324          /* INVCONV sschinch */
4325          -- process_movement (1, source_whse, target_whse, source_co, target_co,l_shipped_date);
4326             process_movement (1, source_orgn_id, target_orgn_id, source_le_id, target_le_id,l_shipped_date);
4327 
4328         RETURN;
4329       END IF;
4330 
4331       /* Inter org transfers -- Intransit*/
4332       IF document_code = 'INVENTORY' THEN
4333         SELECT
4334           mmt.organization_id,
4335           mmt.transfer_organization_id,
4336           hoi1.org_information2,
4337           hoi2.org_information2,
4338           r.shipped_date
4339         INTO
4340           target_orgn_id,source_orgn_id, source_le_id, target_le_id, l_shipped_date
4341         FROM
4342           rcv_transactions t,
4343           mtl_material_transactions mmt,
4344           rcv_shipment_headers r,
4345           rcv_shipment_lines rsl,
4346           po_headers_all poh,
4347           hr_organization_information hoi1,
4348           hr_organization_information hoi2
4349         WHERE
4350                 t.source_document_code = 'INVENTORY'
4351         AND     t.transaction_id = mmt.rcv_transaction_id
4352         AND     mmt.transaction_id = transaction_row.transaction_id
4353         AND     mmt.organization_id = hoi2.organization_id
4354         AND     mmt.transfer_organization_id = hoi1.organization_id
4355         AND     hoi1.org_information_context = 'Accounting Information'
4356         AND     hoi2.org_information_context = 'Accounting Information'
4357         AND     t.shipment_header_id = r.shipment_header_id
4358         AND     r.receipt_source_code in ('INVENTORY')
4359         AND     t.shipment_line_id = rsl.shipment_line_id
4360         AND     t.po_header_id = poh.po_header_id (+);
4361 
4362          /* INVCONV sschinch */
4363          -- process_movement (1, source_whse, target_whse, source_co, target_co,l_shipped_date);
4364             process_movement (1, source_orgn_id, target_orgn_id, source_le_id, target_le_id,l_shipped_date);
4365 
4366         RETURN;
4367       END IF;
4368 
4369       -- If we reach here the PORC transaction is for a true PO Receipt
4370       IF receipt_unit_cost IS NULL THEN
4371         fnd_file.put_line
4372         (fnd_file.log,'ERROR: Could not retrieve PO unit price for transaction ID '||to_char(transaction_row.transaction_id));
4373         l_return_status := 'E';
4374         RETURN;
4375       END IF;
4376 
4377       IF l_debug_level >= l_debug_level_medium THEN
4378         fnd_file.put_line
4379         (fnd_file.log,'Received '||transaction_row.trans_qty ||' '||transaction_row.trans_um
4380          ||' at a unit price of '||l_base_ccy_code||' '||to_char(receipt_unit_cost)); /* Bug 4038722 - Changed the receipt_ccy to l_base_ccy_code. */
4381       END IF;
4382 
4383       IF transaction_row.trans_um <> receipt_uom THEN
4384          -- convert unit cost of receipt to a unit cost that is in the OPM transaction uom
4385          -- BUG 3485915 Reversed the uom parameters in the call
4386          -- B9131983
4387       IF l_debug_level >= l_debug_level_medium THEN
4388  		       fnd_file.put_line
4389 		       (fnd_file.log,'Before UOM Conversion for lot_unit_cost: convert from '
4390 		       ||transaction_row.trans_um
4391 		       ||' to '||receipt_uom
4392 		       ||' for item ID '
4393 		       ||transaction_row.inventory_item_id
4394 		       ||' for Lot Number '
4395 		       ||transaction_row.lot_number
4396 		       ||' Receipt Unit Cost '
4397 		       ||receipt_unit_cost
4398 		       );
4399       END IF;
4400         lot_unit_cost := 0 ;
4401         lot_unit_cost :=
4402                INV_CONVERT.INV_UM_CONVERT(ITEM_ID       => transaction_row.inventory_item_id
4403                                           ,LOT_NUMBER    => transaction_row.lot_number
4404                                           ,ORGANIZATION_ID => transaction_row.orgn_id
4405                                           ,PRECISION     => 5
4406                                           ,FROM_QUANTITY => receipt_unit_cost
4407                                           ,FROM_UNIT     => transaction_row.trans_um
4408                                           ,TO_UNIT       => receipt_uom
4409                                           ,FROM_NAME     => NULL
4410                                           ,TO_NAME       => NULL
4411                                          );
4412 
4413         IF lot_unit_cost = -99999  THEN  -- B9131983
4414           fnd_file.put_line
4415           (fnd_file.log,'ERROR: Could not convert PO receipt uom for transaction ID '||to_char(transaction_row.transaction_id));
4416           l_return_status := 'E';
4417           RETURN;
4418         END IF;
4419 
4420       ELSE
4421         lot_unit_cost := receipt_unit_cost;
4422       END IF;
4423 
4424       IF l_debug_level >= l_debug_level_medium THEN
4425         fnd_file.put_line(fnd_file.log,'Retrieving burdens for receipt');
4426       END IF;
4427 
4428       process_burdens;
4429 
4430       IF   l_return_status <> 'S' THEN
4431         RETURN;
4432       END IF;
4433 
4434       IF l_debug_level >= l_debug_level_medium THEN
4435         fnd_file.put_line(fnd_file.log,'Retrieving Freight and special charges for receipt');
4436       END IF;
4437 
4438       get_special_charges;
4439 
4440       IF l_return_status <> 'S' THEN
4441         RETURN;
4442       END IF;
4443 
4444       IF l_debug_level >= l_debug_level_medium THEN
4445         fnd_file.put_line
4446         (fnd_file.log,'Retrieving component class and analysis code for cost details');
4447       END IF;
4448 
4449       OPEN component_class_cursor
4450            (l_le_id, transaction_row.inventory_item_id, transaction_row.orgn_id,transaction_row.trans_date);
4451       FETCH component_class_cursor INTO component_class_id, cost_analysis_code, dummy;
4452       CLOSE component_class_cursor;
4453 
4454       IF l_debug_level >= l_debug_level_medium  THEN
4455         fnd_file.put_line
4456         (fnd_file.log,'Setting up lot cost of PO receipt');
4457         fnd_file.put_line
4458         (fnd_file.log,'Lot cost of PO Receipt is: ' || to_char(lot_unit_cost));
4459         fnd_file.put_line
4460         (fnd_file.log,'Initialising new_cost_tab(1)' || to_char(lot_unit_cost));
4461       END IF;
4462 
4463       new_cost_tab(1) :=  SYSTEM.gmf_cost_type
4464                           ( component_class_id
4465                           , cost_analysis_code
4466                           , 0
4467                           , lot_unit_cost
4468                           , 0
4469                           );
4470 
4471       new_cost.unit_cost := lot_unit_cost;
4472 
4473       IF NOT old_cost_tab.EXISTS(1) THEN
4474         -- No costing data for this lot/organization exists
4475 
4476         IF l_burdens_total <> 0 THEN
4477           IF l_debug_level >= l_debug_level_medium THEN
4478             fnd_file.put_line
4479             (fnd_file.log,'Merging burden costs');
4480           END IF;
4481 
4482           merge_costs( l_burden_costs_tab
4483                      , 0
4484                      , transaction_row.trans_qty
4485                      , 'C'
4486                      );
4487 
4488        END IF;
4489 
4490        IF l_acquisitions_total <> 0 THEN
4491          IF l_debug_level >= l_debug_level_medium THEN
4492             fnd_file.put_line
4493             (fnd_file.log,'Merging acquisition costs');
4494          END IF;
4495 
4496          merge_costs( l_acqui_cost_tab
4497                      , 0
4498                      , transaction_row.trans_qty
4499                      , 'C'
4500                      );
4501 
4502       END IF;
4503 
4504       lot_unit_cost := lot_unit_cost + l_burdens_total + l_acquisitions_total;
4505 
4506       l_onhand_qty := transaction_row.trans_qty;
4507 
4508       IF l_debug_level >= l_debug_level_medium THEN
4509           fnd_file.put_line
4510           (fnd_file.log,'Finished setting up costs for PO Receipt. Lot unit cost is:'||to_char(lot_unit_cost));
4511       END IF;
4512     ELSE
4513       IF l_debug_level >= l_debug_level_medium THEN
4514         fnd_file.put_line
4515           (fnd_file.log,'Merging old costs with new cost');
4516       END IF;
4517 
4518       IF l_burdens_total <> 0 THEN
4519         IF l_debug_level >= l_debug_level_medium THEN
4520             fnd_file.put_line
4521             (fnd_file.log,'Merging burden costs');
4522         END IF;
4523 
4524         merge_costs( l_burden_costs_tab
4525                     , transaction_row.trans_qty
4526                     , old_cost.onhand_qty
4527                     , 'C'
4528                     );
4529 
4530       END IF;
4531 
4532       IF l_acquisitions_total <> 0  THEN
4533         IF l_debug_level >= l_debug_level_medium THEN
4534            fnd_file.put_line (fnd_file.log,'Merging Special Charges');
4535         END IF;
4536 
4537           merge_costs( l_acqui_cost_tab
4538                      , transaction_row.trans_qty
4539                      , old_cost.onhand_qty
4540                      , 'C'
4541                      );
4542 
4543       END IF;
4544 
4545         -- Bug 3578680
4546         prd_cost := new_cost;
4547         prd_cost_tab := new_cost_tab;
4548 
4549         merge_costs( old_cost_tab
4550                    , old_cost.onhand_qty
4551                    , transaction_row.trans_qty
4552                    , 'A'
4553                    );
4554 
4555         IF l_debug_level >= l_debug_level_medium THEN
4556           fnd_file.put_line(fnd_file.log,'Lot_unit_cost        = '||lot_unit_cost);
4557           fnd_file.put_line(fnd_file.log,'l_burdens_total      = '||l_burdens_total);
4558           fnd_file.put_line(fnd_file.log,'l_acquisitions_total = '||l_acquisitions_total);
4559           fnd_file.put_line(fnd_file.log,'old_onhand           = '||old_cost.onhand_qty);
4560           fnd_file.put_line(fnd_file.log,'old_unit_cost        = '||old_cost.unit_cost);
4561           fnd_file.put_line(fnd_file.log,'trans_qty            = '||transaction_row.trans_qty);
4562         END IF;
4563 
4564         lot_unit_cost := new_cost.unit_cost;
4565 
4566         IF l_debug_level >= l_debug_level_medium THEN
4567           fnd_file.put_line
4568           (fnd_file.log,'Finished setting up revised costs for PO Receipt. Lot unit cost is:'||to_char(lot_unit_cost));
4569         END IF;
4570 
4571          l_onhand_qty := transaction_row.trans_qty + old_cost.onhand_qty;
4572 
4573       END IF;
4574 
4575       create_cost_header
4576       ( transaction_row.inventory_item_id
4577       , transaction_row.lot_number
4578       , transaction_row.orgn_id
4579       , l_cost_type_id
4580       , lot_unit_cost
4581       , transaction_row.trans_date
4582       , l_onhand_qty
4583       , transaction_row.doc_id
4584       , transaction_row.transaction_source_type_id
4585       , transaction_row.transaction_action_id
4586       , new_cost.header_id
4587       , new_cost.unit_cost
4588       , new_cost.onhand_qty
4589       , l_return_status
4590       );
4591 
4592       IF l_return_status = 'S' THEN
4593       -- If header creation success then
4594         FOR i in 1 .. new_cost_tab.COUNT
4595         LOOP
4596           create_cost_detail
4597           ( new_cost.header_id
4598           , new_cost_tab(i).cost_cmpntcls_id
4599           , new_cost_tab(i).cost_analysis_code
4600           , 0
4601           , new_cost_tab(i).component_cost
4602           , 0
4603           , l_return_status
4604           );
4605 
4606           IF l_return_status <> 'S' THEN
4607             RETURN;
4608           END IF;
4609 
4610         END LOOP;
4611 
4612         IF l_return_status = 'S' THEN
4613           IF NOT old_cost_tab.EXISTS(1) THEN
4614             create_material_transaction
4615             ( new_cost.header_id
4616             , l_cost_type_id
4617             , transaction_row.trans_date
4618             , transaction_row.trans_qty
4619             , transaction_row.trans_um
4620             , new_cost.onhand_qty * new_cost.unit_cost
4621             , transaction_row.transaction_id
4622             , new_cost.unit_cost
4623             , transaction_row.trans_qty
4624             , NULL
4625             , NULL
4626             , NULL
4627             , transaction_row.lot_number
4628             , l_return_status
4629             );
4630           ELSE
4631             -- Bug 3578680 Write the cost details with a negated header
4632             FOR i in 1 .. prd_cost_tab.COUNT
4633             LOOP
4634               create_cost_detail
4635               ( -new_cost.header_id
4636               , prd_cost_tab(i).cost_cmpntcls_id
4637               , prd_cost_tab(i).cost_analysis_code
4638               , 0
4639               , prd_cost_tab(i).component_cost
4640               , 0
4641               , l_return_status
4642               );
4643 
4644               IF l_return_status <> 'S' THEN
4645                 RETURN;
4646               END IF;
4647 
4648             END LOOP;
4649 
4650             create_material_transaction
4651             ( new_cost.header_id
4652             , l_cost_type_id
4653             , transaction_row.trans_date
4654             , transaction_row.trans_qty
4655             , transaction_row.trans_um
4656             , prd_cost.unit_cost*transaction_row.trans_qty
4657             , transaction_row.transaction_id
4658             , new_cost.unit_cost
4659             , new_cost.onhand_qty
4660             , old_cost.unit_cost
4661             , old_cost.onhand_qty
4662             , 1
4663             ,transaction_row.lot_number
4664             , l_return_status
4665             );
4666 
4667           END IF;
4668 
4669         END IF;
4670       END IF;
4671      IF l_debug_level >= l_debug_level_medium THEN
4672        fnd_file.put_line
4673        (fnd_file.log,'Leaving Procedure: '||procedure_name);
4674      END IF;
4675 END process_receipt;
4676 
4677 
4678     --**********************************************************************************************
4679     --*                                                                                            *
4680     --* Lot Cost audit- printing to log file.                                                      *
4681     --*                                                                                            *
4682     --**********************************************************************************************
4683 
4684 
4685 PROCEDURE lot_cost_audit
4686      ( p_item_id	NUMBER
4687      , p_lot_number  	VARCHAR2
4688      , p_orgn_id	NUMBER
4689      , p_batch_id	NUMBER
4690      , p_date_costed	DATE
4691      , p_steps		SYSTEM.GMF_STEP_TAB
4692      )
4693 IS
4694 
4695   l_item_id       NUMBER;
4696   l_lot_number      VARCHAR2(80);
4697   l_batch_id      NUMBER;
4698   l_date_costed   DATE;
4699   i               NUMBER;
4700   j               NUMBER;
4701   k               NUMBER;
4702   l               NUMBER;
4703   l_item_no       VARCHAR2(2000);
4704 --  l_batch_no      VARCHAR2(32);
4705   l_plant_code    VARCHAR2(4);
4706   l_resources     VARCHAR2(32);
4707   l_cost_analysis_code          CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE; -- bug14480540
4708   l_cost_component_class_code   VARCHAR2(16);
4709   procedure_name VARCHAR2(100);
4710 
4711 
4712 BEGIN
4713    procedure_name := 'Lot Cost Audit';
4714 
4715     -- Retrieve data in a form that the user will understand
4716     SELECT item_number INTO l_item_no FROM mtl_item_flexfields
4717     WHERE inventory_item_id = p_item_id AND organization_id = p_orgn_id;
4718 
4719     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
4720     fnd_file.put_line
4721     (fnd_file.log, 'Lot cost breakdown for item '
4722     ||l_item_no
4723     ||' lot '
4724     ||l_lot_number
4725     ||' created by batch '
4726     ||l_plant_code
4727     ||' '
4728     ||l_batch_no
4729     ||' in organization '
4730     ||p_orgn_id
4731     ||' on '
4732     ||to_char(p_date_costed)
4733     );
4734 
4735     fnd_file.put_line
4736     (fnd_file.log, '==============================================================================================');
4737 
4738       --
4739       -- umoogala 26-dec-03: program is failing with ORA error when there are no batch steps
4740       --
4741        IF NOT l_step_tab.EXISTS(1) THEN
4742          fnd_file.put_line(fnd_file.log,'No batch steps to print.');
4743          RETURN;
4744        END IF;
4745 
4746       fnd_file.put_line(fnd_file.log,' ');
4747 
4748       FOR i IN 1..l_step_tab.count
4749       LOOP
4750         fnd_file.put_line
4751         (fnd_file.log,'Dump of step index '||to_char(i));
4752         fnd_file.put_line
4753         (fnd_file.log,'---------------------');
4754         fnd_file.put_line
4755         (fnd_file.log,'Step ID         = '||to_char(l_step_tab(i).current_step_id));
4756         fnd_file.put_line
4757         (fnd_file.log,'Step qty        = '
4758         ||to_char(ROUND(l_step_tab(i).step_qty,2))||' '||l_step_tab(i).step_qty_uom);
4759         fnd_file.put_line
4760         (fnd_file.log,'Output qty      = '
4761         ||to_char(ROUND(l_step_tab(i).output_qty,2))||' '||l_step_tab(i).step_qty_uom);
4762         fnd_file.put_line
4763         (fnd_file.log,'  ');
4764 
4765         fnd_file.put_line
4766         (fnd_file.log,'  Current Costs');
4767         fnd_file.put_line
4768         (fnd_file.log,'  -------------');
4769 
4770         IF l_step_tab(i).current_costs.EXISTS(1) THEN
4771           FOR j IN 1..l_step_tab(i).current_costs.count
4772           LOOP
4773             fnd_file.put_line
4774             (fnd_file.log,'  Component Class ID     = '
4775             ||to_char(l_step_tab(i).current_costs(j).cost_cmpntcls_id));
4776             fnd_file.put_line
4777             (fnd_file.log,'  Cost Analysis Code     = '
4778             ||l_step_tab(i).current_costs(j).cost_analysis_code);
4779             fnd_file.put_line
4780             (fnd_file.log,'  Cost Level             = '
4781             ||to_char(l_step_tab(i).current_costs(j).cost_level));
4782             fnd_file.put_line
4783             (fnd_file.log,'  Component Cost         = '
4784             ||to_char(ROUND(l_step_tab(i).current_costs(j).component_cost,2)));
4785             fnd_file.put_line(fnd_file.log,' ');
4786           END LOOP;
4787         ELSE
4788           fnd_file.put_line(fnd_file.log,'  This step has no current costs');
4789         END IF;
4790 
4791         fnd_file.put_line
4792         (fnd_file.log,'  Inherited Costs');
4793         fnd_file.put_line
4794         (fnd_file.log,'  ---------------');
4795         IF l_step_tab(i).inherited_costs.EXISTS(1) THEN
4796           FOR j IN 1..l_step_tab(i).inherited_costs.count
4797           LOOP
4798             fnd_file.put_line
4799             (fnd_file.log,'  Component Class ID     = '
4800             ||to_char(l_step_tab(i).inherited_costs(j).cost_cmpntcls_id));
4801             fnd_file.put_line
4802             (fnd_file.log,'  Cost Analysis Code     = '
4803             ||l_step_tab(i).inherited_costs(j).cost_analysis_code);
4804             fnd_file.put_line
4805             (fnd_file.log,'  Cost Level             = '
4806             ||to_char(l_step_tab(i).inherited_costs(j).cost_level));
4807             fnd_file.put_line
4808             (fnd_file.log,'  Component Cost         = '
4809             ||to_char(ROUND(l_step_tab(i).inherited_costs(j).component_cost,2)));
4810             fnd_file.put_line(fnd_file.log,' ');
4811           END LOOP;
4812         ELSE
4813           fnd_file.put_line(fnd_file.log,'  This step has no inherited costs');
4814         END IF;
4815 
4816         fnd_file.put_line
4817         (fnd_file.log,'  Step Costs');
4818         fnd_file.put_line
4819         (fnd_file.log,'  ---------------');
4820         IF l_step_tab(i).step_costs.EXISTS(1) THEN
4821           FOR j IN 1..l_step_tab(i).step_costs.count
4822           LOOP
4823             fnd_file.put_line
4824             (fnd_file.log,'  Component Class ID     = '
4825             ||to_char(l_step_tab(i).step_costs(j).cost_cmpntcls_id));
4826             fnd_file.put_line
4827             (fnd_file.log,'  Cost Analysis Code     = '
4828             ||l_step_tab(i).step_costs(j).cost_analysis_code);
4829             fnd_file.put_line
4830             (fnd_file.log,'  Cost Level             = '
4831             ||to_char(l_step_tab(i).step_costs(j).cost_level));
4832             fnd_file.put_line
4833             (fnd_file.log,'  Component Cost         = '
4834             ||to_char(ROUND(l_step_tab(i).step_costs(j).component_cost,2)));
4835             fnd_file.put_line(fnd_file.log,' ');
4836           END LOOP;
4837         ELSE
4838           fnd_file.put_line(fnd_file.log,'  This step has no step costs - THIS SHOULD NEVER HAPPEN');
4839         END IF;
4840 
4841 
4842         fnd_file.put_line(fnd_file.log,' ');
4843         fnd_file.put_line
4844         (fnd_file.log,'  Subsequent steps');
4845         fnd_file.put_line
4846         (fnd_file.log,'  ----------------');
4847         IF l_step_tab(i).dependencies(1).step_index IS NULL THEN
4848           fnd_file.put_line(fnd_file.log,'  This is a terminal step');
4849         ELSE
4850           FOR j IN 1..l_step_tab(i).dependencies.count
4851           LOOP
4852             fnd_file.put_line
4853             (fnd_file.log,'  Step index      = '||to_char(l_step_tab(i).dependencies(j).step_index));
4854             fnd_file.put_line
4855             (fnd_file.log,'  Step ID         = '||to_char(l_step_tab(i).dependencies(j).batchstep_id));
4856             fnd_file.put_line
4857             (fnd_file.log,'  Step qty        = '
4858             ||to_char(ROUND(l_step_tab(i).dependencies(j).step_qty,2))
4859             ||l_step_tab(i).dependencies(j).step_qty_uom);
4860             fnd_file.put_line(fnd_file.log,' ');
4861           END LOOP;
4862         END IF;
4863 
4864         fnd_file.put_line(fnd_file.log,' ');
4865         fnd_file.put_line
4866         (fnd_file.log,'  Materials');
4867         fnd_file.put_line
4868         (fnd_file.log,'  ---------');
4869 
4870         IF l_step_tab(i).materials.EXISTS(1) THEN
4871           FOR j in 1..l_step_tab(i).materials.count
4872           LOOP
4873             fnd_file.put_line
4874             (fnd_file.log,'  Trans ID      = '||to_char(l_step_tab(i).materials(j).trans_id));
4875             fnd_file.put_line
4876             (fnd_file.log,'  Legal Entity       = '||l_step_tab(i).materials(j).legal_entity_id);
4877             fnd_file.put_line
4878             (fnd_file.log,'  Organization      = '||l_org_tab(l_step_tab(i).materials(j).organization_id));
4879             fnd_file.put_line
4880             (fnd_file.log,'  Item ID       = '||to_char(l_step_tab(i).materials(j).item_id));
4881             fnd_file.put_line
4882             (fnd_file.log,'  Lot Number        = '||to_char(l_step_tab(i).materials(j).lot_number));
4883             fnd_file.put_line
4884             (fnd_file.log,'  Line type     = '||to_char(l_step_tab(i).materials(j).line_type));
4885             fnd_file.put_line
4886             (fnd_file.log,'  Trans Qty     = '||to_char(l_step_tab(i).materials(j).trans_qty)
4887             ||l_step_tab(i).materials(j).trans_um);
4888             fnd_file.put_line
4889             (fnd_file.log,'  Trans Date    = '
4890             ||to_char(l_step_tab(i).materials(j).trans_date,'DD-Mon-YYYY HH24:MI:SS'));
4891             fnd_file.put_line
4892             (fnd_file.log,'  Lot Cost Flag = '||to_char(l_step_tab(i).materials(j).lot_costed_flag));
4893             fnd_file.put_line
4894             (fnd_file.log,'  Contribution  = '||to_char(l_step_tab(i).materials(j).step_contribution));
4895             fnd_file.put_line
4896             (fnd_file.log,'  Trans Cost    = '||to_char(ROUND(l_step_tab(i).materials(j).trans_cost,2)));
4897             fnd_file.put_line
4898             (fnd_file.log,' ');
4899             fnd_file.put_line
4900             (fnd_file.log,'    Cost Details for material transaction '
4901             ||to_char(l_step_tab(i).materials(j).trans_id));
4902             fnd_file.put_line
4903             (fnd_file.log,'    ----------------------------------------------');
4904 
4905             IF l_step_tab(i).materials(j).cost_details.EXISTS(1) THEN
4906               FOR k IN 1..l_step_tab(i).materials(j).cost_details.count
4907               LOOP
4908                 fnd_file.put_line
4909                 (fnd_file.log,'    Component Class ID     = '
4910                 ||to_char(l_step_tab(i).materials(j).cost_details(k).cost_cmpntcls_id));
4911                 fnd_file.put_line
4912                 (fnd_file.log,'    Cost Analysis Code     = '
4913                 ||l_step_tab(i).materials(j).cost_details(k).cost_analysis_code);
4914                 fnd_file.put_line
4915                 (fnd_file.log,'    Cost Level             = '
4916                 ||to_char(l_step_tab(i).materials(j).cost_details(k).cost_level));
4917                 fnd_file.put_line
4918                 (fnd_file.log,'    Component Cost         = '
4919                 ||to_char(ROUND(l_step_tab(i).materials(j).cost_details(k).component_cost,2)));
4920                 fnd_file.put_line(fnd_file.log,' ');
4921               END LOOP;
4922             ELSE
4923               fnd_file.put_line(fnd_file.log,'    No costs exist for this material');
4924             END IF;
4925           END LOOP;
4926         ELSE
4927           fnd_file.put_line(fnd_file.log,'  This step has no associated materials');
4928         END IF;
4929 
4930         fnd_file.put_line(fnd_file.log,' ');
4931         fnd_file.put_line
4932         (fnd_file.log,'  Resources');
4933         fnd_file.put_line
4934         (fnd_file.log,'  ---------');
4935 
4936         IF l_step_tab(i).resources.EXISTS(1) THEN
4937           FOR j in 1..l_step_tab(i).resources.count
4938           LOOP
4939             fnd_file.put_line
4940             (fnd_file.log,'  Trans ID      = '||to_char(l_step_tab(i).resources(j).trans_id));
4941             fnd_file.put_line
4942             (fnd_file.log,'  Orgn Id     = '||l_org_tab(l_step_tab(i).resources(j).organization_id));
4943             fnd_file.put_line
4944             (fnd_file.log,'  Resource      = '||l_step_tab(i).resources(j).resources);
4945             fnd_file.put_line
4946             (fnd_file.log,'  Resource Usage= '||to_char(l_step_tab(i).resources(j).resource_usage)
4947             ||l_step_tab(i).resources(j).trans_um);
4948             fnd_file.put_line
4949             (fnd_file.log,'  Trans Date    = '
4950             ||to_char(l_step_tab(i).resources(j).trans_date,'DD-Mon-YYYY HH24:MI:SS'));
4951             fnd_file.put_line
4952             (fnd_file.log,'  Trans Cost    = '||to_char(ROUND(l_step_tab(i).resources(j).trans_cost,2)));
4953             fnd_file.put_line
4954             (fnd_file.log,' ');
4955             fnd_file.put_line
4956             (fnd_file.log,'    Cost Details for resource transaction '
4957             ||to_char(l_step_tab(i).resources(j).trans_id));
4958             fnd_file.put_line
4959             (fnd_file.log,'    ---------------------------------------------');
4960 
4961             IF l_step_tab(i).resources(j).cost_details.EXISTS(1) THEN
4962               FOR k IN 1..l_step_tab(i).resources(j).cost_details.count
4963               LOOP
4964                 fnd_file.put_line
4965                 (fnd_file.log,'    Component Class ID     = '
4966                 ||to_char(l_step_tab(i).resources(j).cost_details(k).cost_cmpntcls_id));
4967                 fnd_file.put_line
4968                 (fnd_file.log,'    Cost Analysis Code     = '
4969                 ||l_step_tab(i).resources(j).cost_details(k).cost_analysis_code);
4970                 fnd_file.put_line
4971                 (fnd_file.log,'    Cost Level             = '
4972                 ||to_char(l_step_tab(i).resources(j).cost_details(k).cost_level));
4973                 fnd_file.put_line
4974                 (fnd_file.log,'    Component Cost         = '
4975                 ||to_char(ROUND(l_step_tab(i).resources(j).cost_details(k).component_cost,2)));
4976                 fnd_file.put_line(fnd_file.log,' ');
4977               END LOOP;
4978             ELSE
4979               fnd_file.put_line(fnd_file.log,'    This resource has no costs');
4980             END IF;
4981           END LOOP;
4982         ELSE
4983           fnd_file.put_line(fnd_file.log,'  This step has no associated resources');
4984         END IF;
4985       END LOOP;
4986        fnd_file.put_line
4987        (fnd_file.log,'Leaving Procedure: '||procedure_name);
4988 
4989 END lot_cost_audit;
4990 
4991 --********************************************************************************************************
4992     --*    Procedure Name : PROCESS_BATCH
4993     --*
4994     --*     Description :
4995     --*               Procedure to handle manufactured lots.
4996     --*
4997     --* HISTORY
4998     --*
4999     --*  15-Dec-2004 Dinesh Vadivel Bug# 4053149
5000     --*    Earlier we were handling only two cases in the step dependency chain
5001     --*     1. if no routing call the explosion_cursor_nr.
5002     --*     2. if there is routing and dependent_steps == 0 then we will create n-1
5003     --*          virtual rows in gme_batch_step_dependencies
5004     --*    Now changed the code to handle the following cases if there is routing present
5005     --*     1. All the steps have Dependency defined - Directly call the explosion_cursor
5006     --*     2. Single Step Batch. Obviously No Dependency - So call explosion_cursor_ss
5007     --*     3. All steps are Independent. Here create n-1 virtual rows in dependency table
5008     --*         and then call the explosion_cursor
5009     --*     4. Few steps have dependency defined by user and Few are independent
5010     --*           Here, we have to add virtual rows in dependency table only for those
5011     --*           independent rows. For this we use a query and find the Min(batchstep_id)
5012     --*           and insert these independent rows above it.
5013     --*
5014     --*  Bug 4094132 - 20-Jan-2005 - Girish Jha Added a condition to set the new local variable
5015     --*                               l_step_output_qty to 0 or Null.
5016     --*
5017     --* 03-Feb-2005 - Bug 4144329 - Dinesh Vadivel - If there is no cost defined for Resource
5018     --*            then don't stop by setting the cost as uncostable. Just give a warning and ignore
5019     --*            the resources.
5020     --* 12-MAR-2009  Hari Luthra Bug 7317270
5021     --*             FETCH of the individual cusrsor have initialization like
5022     --*                l_step_tab(i).resources := SYSTEM.gmf_rsrc_tab();
5023 --********************************************************************************************************
5024 
5025 PROCEDURE process_batch
5026 IS
5027   l_cost_factor         NUMBER;
5028   l_tran_qty            NUMBER;
5029   l_um_type             VARCHAR2(25);
5030   ing_tab               SYSTEM.gmf_matl_tab;
5031   prd_tab               SYSTEM.gmf_matl_tab;
5032   l_new_cost            NUMBER;
5033   l_step_output_qty     NUMBER;      -- Bug 4094132 Added
5034   l_count               NUMBER;      -- Bug 4057323
5035   l_unassociated_prds   NUMBER;      -- Bug 4057323
5036   l_temp_qty            NUMBER :=1;  -- B9131983
5037   l_temp_trans_qty      NUMBER :=1;  -- B9131983
5038   l_actual_line_qty     NUMBER :=1;  -- B9131983
5039   l_odd_even            NUMBER := NULL ;  -- B9239944
5040   l_total_materials     NUMBER := NULL ;  -- B9239944
5041 -- Bug 13386258
5042   k_period_id           NUMBER := 0 ;
5043   k_date                varchar2(30) := NULL;
5044   l_new_batch           NUMBER := 1 ;     -- Bug 13386258-VC. Cache batch steps details
5045   l_prd_accrued  BOOLEAN := FALSE; -- PK Bug 13729144
5046 
5047     /***** Bug 4227784 - Dinesh - The Below explanation is correct. But
5048      if we use the total_item_qty obtained from the below cursor, then the
5049      case in which the same product is allocated in multiple lines may give
5050      wrong cost. So we have to use actual_line_qty instead of
5051      total_item_qty *****/
5052 
5053     /***** Bug 4057323 - Dinesh - Added the below cursor *****/
5054     -- Eg: Consider we have two products P1 and P2 having cost allocation factor
5055     -- of 0.7 and 0.3 respectively.  P1 has 10LB which is yielded into two lots L1(8LB)
5056     -- L2(2LB) . P2 has 20LB yielded into one single Lot. Let us assume Total Ingredient
5057     -- Cost is 300$
5058     -- In the above case when we use the Cost Allocation Factor, the product P1 gets 210$ (300 * 0.7)
5059     -- and P2 gets 90$ (300 * 0.3). Now we need to approtion the cost of P1 into its Lots L1 and L2.
5060     -- It is done as L1 gets 210$ * 8/(8+2) = 168$ and L2 gets 210$ * 2/(2+8) = 42$ . This is total cost the
5061     -- lots L1 and L2 gets. So unit price of these lots will become as 21$ each.
5062     -- Here the denominator (8+2) is obtained using the following newly added query.
5063 
5064   /***** Bug 4053149  Dinesh Vadivel - Start *****/
5065   l_min_dep_step_id       NUMBER;
5066   l_cur_step_id           NUMBER;
5067   l_independent_steps_cnt NUMBER;
5068   procedure_name VARCHAR2(100);
5069 
5070   TYPE l_independent_steps_type IS TABLE OF
5071         gme_batch_steps.batchstep_id%TYPE INDEX BY BINARY_INTEGER;
5072   l_independent_steps  l_independent_steps_type;
5073 
5074   -- we'll add order by...doesn't harm. since data is not setup properly, we'll assume
5075   -- steps are in order, at least.
5076 
5077   /* Cursor to get the details of Independent Steps.
5078   ** i.e., Steps without any dependency defined */
5079   CURSOR independent_steps_cursor(p_batch_id NUMBER)
5080   IS
5081      SELECT batchstep_id
5082        FROM gme_batch_steps
5083       WHERE batch_id = p_batch_id
5084         AND batchstep_id NOT IN
5085              (
5086                SELECT batchstep_id
5087                  FROM gme_batch_step_dependencies
5088                 WHERE batch_id = p_batch_id
5089                 UNION ALL
5090                SELECT dep_step_id
5091                  FROM gme_batch_step_dependencies
5092                 WHERE batch_id = p_batch_id
5093              )
5094       ORDER BY batchstep_id
5095   ;
5096 
5097    /***** Bug 4053149  Dinesh Vadivel - End *****/
5098    x_mtl_analysis_code  CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE;    /* B9131983 and bug14480540*/
5099    x_mtl_cmpntcls_id    NUMBER;         /* B9131983 */
5100 BEGIN
5101 
5102    procedure_name := 'Process Batch';
5103    IF l_debug_level >= l_debug_level_medium THEN
5104        fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
5105    END IF;
5106 
5107    l_skip_this_batch  := FALSE;
5108    l_skip_this_txn    := FALSE;
5109    l_cost_accrued     := FALSE;
5110 
5111    /* B9131983 If cost is ZERO or component is not available
5112       select the default value from fiscal policy and use it in cost details */
5113       -- Bug 13038249 Grupo viz changes starts
5114 
5115       x_mtl_analysis_code := l_mtl_analysis_code;
5116       x_mtl_cmpntcls_id   := l_mtl_cmpntcls_id;
5117       -- Bug 13038249 Grupo viz changes Ends
5118 
5119       -- We're in business  . Will reach here only if batch_status is in (3,4)
5120 
5121       IF l_debug_level >= l_debug_level_medium THEN
5122         fnd_file.put_line
5123         (fnd_file.log,'Batch is eligible for lot costing');
5124       END IF;
5125 
5126      l_skip_this_batch := FALSE;
5127 
5128       -- If this is a product line then we need to perform a complete explosion of the batch
5129       -- steps. This will retrieve the steps in the correct sequence dependencies together
5130       -- with all associated material and resource transactions.
5131 
5132      IF transaction_row.line_type = 1 THEN -- Don't explode for byproducts
5133        IF l_debug_level >= l_debug_level_medium THEN
5134          fnd_file.put_line
5135          (fnd_file.log,'About to explode batch');
5136        END IF;
5137 
5138        -- Explode the batch
5139 
5140        /*  SELECT nvl(routing_id,0) INTO l_routing
5141        FROM   gme_batch_header
5142        WHERE  batch_type = 0
5143        AND    batch_id = transaction_row.doc_id; -- Bug 13038249  */
5144 
5145        l_routing := transaction_row.routing_id;  -- Bug 13038249
5146 
5147        l_new_batch := 1;
5148       -- check cache batch details exist for this batchh
5149       /* Bug 13386258-VC */
5150        IF c_batch_details.EXISTS(transaction_row.doc_id) THEN
5151               l_new_batch := 0;
5152           --    display_c_batch_details(transaction_row.doc_id);
5153        END IF;
5154 
5155 
5156        /* Bug 13386258-VC */
5157        IF (l_new_batch = 1) THEN
5158        -- {
5159        IF l_routing = 0 THEN   /* Start if of l_routing = 0 */
5160        IF l_debug_level >= l_debug_level_medium THEN
5161         fnd_file.put_line(fnd_file.log,' Opening explosion_cursor_nr cursor');
5162        END IF;
5163 
5164          OPEN  explosion_cursor_nr;
5165          FETCH explosion_cursor_nr BULK COLLECT INTO l_step_lev, l_step_tab;
5166          CLOSE explosion_cursor_nr;
5167 
5168          -- Bug 9752492  passing transaction_row.orgn_id rather than l_orgn_id
5169 
5170           GMD_API_GRP.FETCH_PARM_VALUES (P_orgn_id => transaction_row.orgn_id,
5171                              P_parm_name => 'FM_YIELD_TYPE',
5172                              P_parm_value  => l_um_type,
5173                              X_return_status => l_return_status);
5174           IF l_return_status = 'E' THEN
5175            fnd_file.put_line(fnd_file.log,'Error Cannot find the value for the FM_YIELD_TYPE' );
5176            -- Bug 13038249 Mark the batch as uncostable
5177            l_uncostable_batches_tab(transaction_row.doc_id) := transaction_row.doc_id;
5178            RETURN;
5179           ELSE          -- B9131983
5180 	    IF l_debug_level >= l_debug_level_medium THEN -- Bug#13000758
5181            	fnd_file.put_line(fnd_file.log,'The value for the FM_YIELD_TYPE is SET as '||l_um_type );
5182 	    END IF;
5183         END IF;
5184 
5185          SELECT uom_code INTO l_step_tab(1).step_qty_uom
5186          FROM   mtl_units_of_measure
5187          WHERE  uom_class = l_um_type AND base_uom_flag = 'Y';
5188 
5189        ELSE
5190        IF l_debug_level >= l_debug_level_medium THEN
5191          fnd_file.put_line(fnd_file.log,' Entering else part of l_routing=0');
5192        END IF;
5193 
5194          SELECT count(*) INTO l_dep_steps
5195          FROM   gme_batch_step_dependencies
5196          WHERE  batch_id = transaction_row.doc_id;
5197 
5198          /******* Dinesh Vadivel - Bug 4053149 - Start ********/
5199 
5200          OPEN independent_steps_cursor(transaction_row.doc_id);
5201          FETCH independent_steps_cursor BULK COLLECT INTO l_independent_steps;
5202          CLOSE independent_steps_cursor;
5203 
5204          l_independent_steps_cnt :=  NVL(l_independent_steps.COUNT,0)  ;
5205 
5206          IF l_debug_level >= l_debug_level_medium THEN
5207              fnd_file.put_line(fnd_file.log,
5208                 '  Before all the CASES...' ||
5209                 '# of dep steps : '|| l_dep_steps || '...' ||
5210                 '# of independent steps : '|| l_independent_steps_cnt );
5211               fnd_file.put_line(fnd_file.log,
5212                 ' Independent steps(ids), if any, are: ' );
5213               FOR i in 1.. l_independent_steps_cnt LOOP
5214                  fnd_file.put_line(fnd_file.log, ' i = '||i||'. step_id --> '||l_independent_steps(i));
5215               END LOOP;
5216          END IF;
5217 
5218 
5219          /*************  CASE 1 ******************/
5220          --  All the steps have Dependency defined
5221          IF (l_independent_steps_cnt = 0 AND  l_dep_steps > 0) THEN
5222 
5223            IF l_debug_level >= l_debug_level_medium THEN
5224               fnd_file.put_line(fnd_file.log,'  Opening the Normal Explosion Cursor at CASE 1. All dependency already defined ');
5225             END IF;
5226 
5227             OPEN  explosion_cursor (transaction_row.doc_id);
5228             FETCH explosion_cursor BULK COLLECT INTO l_step_lev, l_step_tab;
5229             CLOSE explosion_cursor;
5230 
5231            /*************  CASE 2 ******************/
5232           --  Single Step Batch. No Dependency
5233          ELSIF (l_independent_steps_cnt = 1  AND l_dep_steps = 0) THEN
5234 
5235              IF l_debug_level >= l_debug_level_medium THEN
5236                 fnd_file.put_line(fnd_file.log,'  Opening the Single Step Explosion Cursor at CASE 2');
5237              END IF;
5238 
5239               OPEN  explosion_cursor_ss (transaction_row.doc_id);
5240               FETCH explosion_cursor_ss BULK COLLECT INTO l_step_lev, l_step_tab;
5241               CLOSE explosion_cursor_ss;
5242 
5243          /*************  CASE 3 ******************/
5244          --  All steps are Independent
5245          ELSIF ( l_independent_steps_cnt > 1 AND l_dep_steps = 0) THEN
5246 
5247              IF l_debug_level >= l_debug_level_medium THEN
5248                 fnd_file.put_line(fnd_file.log,
5249                    ' Inside CASE 3 : All steps are independent. So we are
5250                      building virtual dependency chain. ');
5251              END IF;
5252 
5253               i:= 0;
5254               l_prior_step_id := NULL;
5255 
5256               FOR step_row IN steps_cursor(transaction_row.doc_id)
5257               LOOP
5258                    IF i > 0 THEN
5259                      INSERT INTO gme_batch_step_dependencies
5260                      ( batch_id
5261                      , batchstep_id
5262                      , dep_step_id
5263                      , standard_delay
5264                      , dep_type
5265                      , created_by
5266                      , creation_date
5267                      , last_updated_by
5268                      , last_update_date
5269                      )
5270                      VALUES
5271                      ( transaction_row.doc_id
5272                      , step_row.batchstep_id
5273                      , l_prior_step_id
5274                      , 0
5275                      , 0
5276                      , -1
5277                      , SYSDATE
5278                      , -1
5279                      , SYSDATE
5280                      );
5281                    END IF;
5282 
5283                    i := i+1;
5284                    l_prior_step_id := step_row.batchstep_id;
5285              END LOOP;
5286 
5287              OPEN  explosion_cursor (transaction_row.doc_id);
5288              FETCH explosion_cursor BULK COLLECT INTO l_step_lev, l_step_tab;
5289              CLOSE explosion_cursor;
5290 
5291              DELETE FROM gme_batch_step_dependencies
5292              WHERE  batch_id = transaction_row.doc_id;
5293          -- END OF CASE 3
5294 
5295 
5296          /*************  CASE 4 ******************/
5297          -- Few steps have dependency defined and Few are independent.
5298          ELSIF(l_independent_steps_cnt > 0 AND  l_dep_steps > 0) THEN
5299 
5300              IF l_debug_level >= l_debug_level_medium THEN
5301                  fnd_file.put_line(fnd_file.log,
5302                    ' Inside CASE 4 : Few steps are independent. So we are
5303                      building virtual dependency chain only for those
5304                      independent rows. ');
5305              END IF;
5306 
5307              SELECT MIN(dep_step_id)
5308                INTO l_min_dep_step_id
5309                FROM gme_batch_step_dependencies
5310               START WITH batch_id =  transaction_row.doc_id
5311                 AND batchstep_id NOT IN (SELECT dep_step_id
5312                                            FROM gme_batch_step_dependencies
5313                                           WHERE batch_id =  transaction_row.doc_id)
5314              CONNECT BY PRIOR dep_step_id = batchstep_id
5315                 AND batch_id = PRIOR batch_id;
5316 
5317              IF l_debug_level >= l_debug_level_medium THEN
5318                 fnd_file.put_line(fnd_file.log,'  CASE 4 : The minimum step id is '||l_min_dep_step_id);
5319              END IF;
5320 
5321              i:= 0;
5322              l_cur_step_id := l_min_dep_step_id;
5323 
5324              /*  Eg:  We have records 10,20,30,40,50 and 30,40,50 have dependency existing.
5325              **       There will be two records in the gme_batch_step_Dependency table
5326              **       as   batchstep_id : 40  (dep_step_id : 30)
5327              **       and batchstep_id : 50  (dep_step_id : 40)
5328              **
5329              **       In the below insert, we will add two new records as
5330              **       batchstep_id : 30      (dep_step_id : 20)
5331              **       batchstep_id : 20 (dep_step_id : 10).
5332              */
5333 
5334 
5335              FOR i IN 1..l_independent_steps_cnt
5336              LOOP
5337                INSERT INTO gme_batch_step_dependencies
5338                      ( batch_id
5339                      , batchstep_id
5340                      , dep_step_id
5341                      , standard_delay
5342                      , dep_type
5343                      , created_by
5344                      , creation_date
5345                      , last_updated_by
5346                      , last_update_date
5347                      )
5348                VALUES
5349                      ( transaction_row.doc_id
5350                      , l_cur_step_id
5351                      , l_independent_steps(i)
5352                      , 0
5353                      , 0
5354                      , -1
5355                      , SYSDATE
5356                      , -1
5357                      , SYSDATE
5358                      );
5359                l_cur_step_id :=  l_independent_steps(i);
5360              END LOOP;
5361 
5362              OPEN  explosion_cursor (transaction_row.doc_id);
5363              FETCH explosion_cursor BULK COLLECT INTO l_step_lev, l_step_tab;
5364              CLOSE explosion_cursor;
5365 
5366              /* Delete only those records which were inserted above.
5367              **  Eg:  We have records 10,20,30,40,50 and 30,40,50 have dependency existing.
5368              **       There will be two records in the gme_batch_step_Dependency table
5369              **       as   40  (dep_step_id : 30)
5370              **       and 50  (dep_step_id : 40)
5371              **
5372              **       In the above insert, we have added two new records as
5373              **       30 (dep_step_id : 20)
5374              **       20 (dep_step_id : 10).
5375              **Now we need to delete these two newly inserted records.
5376              */
5377 
5378              FOR i IN 1 .. l_independent_steps_cnt
5379              LOOP
5380                 DELETE FROM gme_batch_step_dependencies
5381                 WHERE batch_id = transaction_row.doc_id
5382                   AND dep_step_id = l_independent_steps(i);
5383              END LOOP;
5384 
5385           /* ELSE is not used, because code will not reach ELSE. */
5386 
5387          END IF; /* Ending all the Cases */
5388 
5389       /******* Dinesh Vadivel - Bug 4053149 - End ********/
5390 
5391      END IF;  /* End if of l_routing = 0 */
5392 
5393        IF NOT l_step_tab.EXISTS(1) THEN
5394          fnd_file.put_line
5395         (fnd_file.log,'ERROR: Could not explode steps of Batch No(Batch_Id) '||l_batch_no||'('||to_char(transaction_row.doc_id)||')');
5396          l_return_status := 'E'; /* Bug 4297815 - Set Return Status to E (WARNING)*/
5397          l_tmp := FALSE;
5398          -- Bug 13038249 Mark the batch as uncostable
5399          l_uncostable_batches_tab(transaction_row.doc_id) := transaction_row.doc_id;
5400          RETURN;
5401        END IF;
5402 
5403        IF l_debug_level >= l_debug_level_medium THEN
5404          fnd_file.put_line
5405          (fnd_file.log,'After explosion, '||to_char(l_step_tab.count)||' steps loaded');
5406        END IF;
5407 
5408        -- Bug 13038249 moved code for l_cost_alloc_profile to outside the loop originally starting at line 5486
5409        /***** Bug 4057323 - To check whether all the Products are yielded in one single step - Start *****/
5410         IF l_debug_level >= l_debug_level_high THEN
5411             fnd_file.put_line (fnd_file.log, 'Cost Allocation Factor : Just before entering the cost allocation factor code , l_cost_alloc_profile value is '||l_cost_alloc_profile);
5412         END IF;
5413 
5414          IF (l_cost_alloc_profile = 1) THEN  /* Start if of l_cost_alloc_profile =1 */
5415               IF l_debug_level >= l_debug_level_high  THEN
5416                    fnd_file.put_line (fnd_file.log, ' Cost Allocation Factor :  l_Step_tab.COUNT is '||l_step_tab.COUNT);
5417                END IF;
5418               IF (l_step_tab.COUNT = 1) THEN
5419                 IF l_debug_level >= l_debug_level_high THEN
5420                    fnd_file.put_line (fnd_file.log, ' Cost Allocation Factor : Go ahead as there is only one total step ');
5421                  END IF;
5422                  -- Go ahead... There is only one step available. So all products should be attached to this step only.
5423                  l_cost_alloc_profile := 1;
5424               ELSIF (l_step_tab.COUNT > 1) THEN
5425                   IF l_debug_level >= l_debug_level_high THEN
5426                       fnd_file.put_line (fnd_file.log, ' Cost Allocation Factor :  Wait, there are many steps. So have to process others. Before the distinct batchstep_id area');
5427                   END IF;
5428 
5429                  /* Check whether the number of Distinct steps having step material association is 1.
5430                      If not don't use Cost Allocation Factor */
5431                  SELECT COUNT (DISTINCT batchstep_id)
5432                    INTO l_count
5433                    FROM gme_material_details gmd, gme_batch_step_items gbsi
5434                   WHERE gmd.material_detail_id = gbsi.material_detail_id
5435                     AND gmd.batch_id = gbsi.batch_id
5436                     AND gmd.line_type = 1
5437                     AND gmd.batch_id = transaction_row.doc_id;
5438 
5439                      IF l_debug_level >= l_debug_level_high THEN
5440                         fnd_file.put_line (fnd_file.log, ' Cost Allocation Factor : After the distinct batchstep_id area. the l_count is '||l_count);
5441                       END IF;
5442 
5443                  IF (NVL (l_count, 0) = 1) THEN
5444                      IF l_debug_level >= l_debug_level_high THEN
5445                         fnd_file.put_line (fnd_file.log, ' Cost Allocation Factor : There are some Step Matl Association. So check any unassociated products ');
5446                       END IF;
5447 
5448                     -- If l_count is 1 then there is only one step having step material association
5449                     /* Get Unassociated Products */
5450                     /* Check whether there is/are any unassociated products */
5451 
5452 
5453                       SELECT COUNT (DISTINCT mmt.inventory_item_id)
5454                       INTO l_unassociated_prds
5455                       FROM mtl_material_transactions mmt, gme_material_details gmd
5456                      WHERE mmt.transaction_source_type_id = 5
5457                        AND mmt.transaction_source_id = gmd.batch_id
5458                        AND gmd.line_type = 1
5459                        AND mmt.transaction_quantity <> 0
5460                        AND gmd.batch_id =transaction_row.doc_id
5461                        AND mmt.trx_source_line_id = gmd.material_detail_id
5462                        AND gmd.material_detail_id NOT IN (
5463                                 SELECT material_detail_id
5464                                   FROM gme_batch_step_items
5465                                  WHERE batch_id = transaction_row.doc_id);
5466 
5467                        IF l_debug_level >= l_debug_level_high THEN
5468                            fnd_file.put_line (fnd_file.log, ' Cost Allocation Factor : The number of unassociated products are '||l_unassociated_prds);
5469                        END IF;
5470 
5471                     IF (l_unassociated_prds = 0) THEN
5472                       IF l_debug_level >= l_debug_level_high THEN
5473                         fnd_file.put_line (fnd_file.log, ' Cost Allocation Factor : unassociated prds are zero. so Go Ahead');
5474                       END IF;
5475 
5476                        -- Go ahead and use the profile
5477                        l_cost_alloc_profile := 1;
5478                     ELSE
5479 
5480                        /***** Important Case *****/
5481                        -- At this place we have some products attached to a step,
5482                        -- and some unassociated products.
5483                        -- Although there is a chance that all the products which have explicit step association
5484                        -- is on terminal step  and our logic considers the unassociated products
5485                        -- to be attached with the terminal step, BUT WE DONOT SUPPORT THIS CASE
5486                          fnd_file.put_line
5487                              ( fnd_file.log, ' NOTE : The profile GMF: Use Cost Alloc Factor in Lot Costing has been set to ''Yes''.
5488                                   Since few products have step association defined and few does not, this profile will NOT be used.');
5489                          l_cost_alloc_profile := 0;
5490                      END IF;
5491 
5492                  ELSIF (NVL (l_count, 0) = 0)  THEN
5493                      IF l_debug_level >= l_debug_level_high THEN
5494                          fnd_file.put_line (fnd_file.log, ' Cost Allocation Factor : No Step material Associations. So Go Ahead ');
5495                       END IF;
5496 
5497                     -- Go Ahead.. The Number of steps having step material Association is zero.
5498                     -- So all steps should have been attached to a single step by default which is the terminal step
5499                     l_cost_alloc_profile := 1;
5500 
5501                  ELSE    -- The Products are associated with multiple steps. So don't use the profile
5502                     fnd_file.put_line
5503                          ( fnd_file.log, ' NOTE : The profile GMF: Use Cost Alloc Factor in Lot Costing has been set to ''Yes''.
5504                                 Since all the products are not yielded in a single step this profile will NOT be used.');
5505                     l_cost_alloc_profile := 0;
5506                  END IF;                     /* End if of   IF( NVL(l_count,0) = 1) */
5507               END IF;                           /* End of IF (l_step_tab.COUNT = 1) */
5508            END IF;  /* End if of l_cost_alloc_profile =1 */
5509 
5510         /***** Bug 4057323 - To check whether all the Products are yielded in one single step  - End *****/
5511 
5512            /* Bug 13386258-VC */
5513            IF (l_cost_alloc_profile = 1) THEN -- cache the batch
5514             c_batch_details(transaction_row.doc_id).batch_id:= transaction_row.doc_id;
5515             c_batch_details(transaction_row.doc_id).step_tab:= l_step_tab;
5516 
5517 	    IF l_debug_level >= l_debug_level_high THEN
5518           fnd_file.put_line
5519           (fnd_file.log,'START STEP_TAB CACHING FOR BATCH_ID = '
5520           || c_batch_details(transaction_row.doc_id).batch_id  );
5521 	    END IF;
5522 
5523            END IF;
5524         ELSE
5525             l_step_tab:=c_batch_details(transaction_row.doc_id).step_tab;
5526             l_cost_alloc_profile := 1;
5527             IF l_debug_level >= l_debug_level_medium THEN
5528               fnd_file.put_line(fnd_file.log,' Restored details of '||l_step_tab.count||' steps from the cache');
5529             END IF;
5530         --}
5531         END IF;
5532 
5533        -- Load the transactions and cost them individually
5534 
5535        FOR i in 1..l_step_tab.count
5536        LOOP
5537 
5538           /**** Bug 5368082 Skip the Batch and set the Lot as uncostable if any of the step_qty is zero ****/
5539          /* Added l_routing <> 0. When there is no routing, still l_step_tab will
5540           * have one record. In that case we should not check for step_qty = 0 check */
5541         /* Bug 9616762  Front port of 4601607 - Don't skip the batch even if the step qty = 0 */
5542         /*
5543          IF (l_routing <> 0 AND l_step_tab(i).step_qty = 0) THEN
5544 	        l_skip_this_batch := TRUE;
5545 
5546            fnd_file.put_line
5547            ( fnd_file.log,'ERROR: The Actual Step Qty of one of the steps in Batch_Id '||transaction_row.doc_id||' is zero. The Batch will be skipped from being costed.');
5548 
5549            l_return_status := 'E';
5550            l_uncostable_lots_tab(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number) := transaction_row.inventory_item_id;
5551            l_tmp := FALSE;
5552 
5553            RETURN;
5554          END IF; */
5555          /**** Bug 5368082 Skip the Lot if any of the step_qty is zero ****/
5556 
5557          IF l_routing <> 0  THEN   /* Start  l_routing <> 0 */
5558             -- Load all resource transactions for each step and calculate the cost of each
5559 
5560            IF l_debug_level >= l_debug_level_medium THEN
5561             fnd_file.put_line
5562             (fnd_file.log,'Loading resource transactions for step ID '
5563             ||to_char(l_step_tab(i).current_step_id)
5564             );
5565            END IF;
5566 
5567            /* Bug 13386258-VC */
5568            IF (l_new_batch = 1) THEN
5569            -- {
5570 
5571 	         l_step_tab(i).resources := SYSTEM.gmf_rsrc_tab();  -- Bug 7317270
5572            OPEN  resources_cursor (transaction_row.doc_id, l_step_tab(i).current_step_id);
5573            FETCH resources_cursor BULK COLLECT INTO l_step_tab(i).resources;
5574            CLOSE resources_cursor;
5575 
5576             /* Bug 13386258-VC */
5577             IF (l_cost_alloc_profile = 1) THEN -- cache the batch
5578               c_batch_details(transaction_row.doc_id).step_tab(i).resources:=l_step_tab(i).resources;
5579 	      IF l_debug_level >= l_debug_level_high THEN
5580           fnd_file.put_line
5581           (fnd_file.log,'START RESOURCE_TAB CACHING, Number of Resources = '
5582           || c_batch_details(transaction_row.doc_id).step_tab(i).resources.count );
5583 	      END IF;
5584 
5585            IF l_debug_level >= l_debug_level_medium THEN
5586              fnd_file.put_line
5587              (fnd_file.log,'Loaded '
5588              ||to_char(l_step_tab(i).resources.count)||' resource txns'
5589              );
5590            END IF;
5591 
5592             END IF;
5593 
5594          ELSE
5595 
5596            IF l_debug_level >= l_debug_level_medium THEN
5597              fnd_file.put_line
5598              (fnd_file.log,'Restored '
5599              ||to_char(l_step_tab(i).resources.count)||' resource txns'
5600              );
5601            -- }
5602            END IF;
5603 
5604            END IF;
5605 
5606            FOR j in 1..l_step_tab(i).resources.count
5607            LOOP
5608 
5609              res_cost := NULL;
5610 	         OPEN resource_cost_cursor ( l_le_id
5611                , l_default_cost_type_id
5612                , l_step_tab(i).resources(j).resources
5613                , l_step_tab(i).resources(j).organization_id
5614                , l_step_tab(i).resources(j).trans_date
5615                , transaction_row.doc_id
5616                , l_step_tab(i).current_step_id
5617                );
5618              FETCH resource_cost_cursor BULK COLLECT INTO l_step_tab(i).resources(j).cost_details;
5619              CLOSE resource_cost_cursor;
5620 
5621              IF NOT l_step_tab(i).resources(j).cost_details.EXISTS(1) THEN
5622              fnd_file.put_line
5623                (fnd_file.log, 'Warning: The resource '
5624                   ||l_step_tab(i).resources(j).resources
5625                   ||' transaction is ignored as the resource has no cost'
5626                );
5627 
5628              ELSE
5629 
5630                FOR k IN 1..l_step_tab(i).resources(j).cost_details.count
5631                LOOP
5632 
5633                  l_step_tab(i).resources(j).trans_cost := l_step_tab(i).resources(j).trans_cost
5634                   + l_step_tab(i).resources(j).resource_usage
5635                    *l_step_tab(i).resources(j).cost_details(k).component_cost;
5636 
5637                  l_cost_accrued := FALSE;
5638 
5639                  FOR l IN 1..l_step_tab(i).current_costs.count
5640                  LOOP
5641                    IF   l_step_tab(i).resources(j).cost_details(k).cost_cmpntcls_id
5642                        =l_step_tab(i).current_costs(l).cost_cmpntcls_id
5643                    AND  l_step_tab(i).resources(j).cost_details(k).cost_analysis_code
5644                        =l_step_tab(i).current_costs(l).cost_analysis_code
5645                    AND  l_step_tab(i).resources(j).cost_details(k).cost_level
5646                        =l_step_tab(i).current_costs(l).cost_level
5647                    THEN
5648                      l_step_tab(i).current_costs(l).component_cost :=
5649                        l_step_tab(i).current_costs(l).component_cost
5650                      + l_step_tab(i).resources(j).resource_usage
5651                       *l_step_tab(i).resources(j).cost_details(k).component_cost;
5652                      l_cost_accrued := TRUE;
5653                      EXIT;
5654                    END IF;
5655                  END LOOP;
5656 
5657                  IF NOT l_cost_accrued THEN
5658                    IF l_step_tab(i).current_costs(1).cost_analysis_code = ' ' THEN
5659                      l_step_tab(i).current_costs(1) :=
5660                        l_step_tab(i).resources(j).cost_details(k);
5661                      l_step_tab(i).current_costs(1).component_cost :=
5662                        l_step_tab(i).resources(j).resource_usage
5663                       *l_step_tab(i).resources(j).cost_details(k).component_cost;
5664                    ELSE
5665                      l_step_tab(i).current_costs.EXTEND;
5666                      l := l_step_tab(i).current_costs.count;
5667                      l_step_tab(i).current_costs(l) :=
5668                        l_step_tab(i).resources(j).cost_details(k);
5669                      l_step_tab(i).current_costs(l).component_cost :=
5670                        l_step_tab(i).resources(j).resource_usage
5671                       *l_step_tab(i).resources(j).cost_details(k).component_cost;
5672                    END IF;
5673                  END IF;
5674                END LOOP;
5675              END IF;
5676            END LOOP;
5677          END IF;
5678 
5679          -- Now load the inventory transactions. If the batch didn't have a routing, or the
5680          -- routing it possessed only had a single step, all inventory is attached to the
5681          -- first (only) step in the steps table.
5682 
5683          -- If there was a routing but no associations were set up, we attach all inventory
5684          -- to the last step of the chain, so that the products absorb all of the costs.
5685 
5686          -- If we have a routing and dependencies we attach the inventory to the
5687          -- correct step.
5688 
5689 
5690          IF l_step_tab.COUNT = 1  THEN
5691 
5692            /* Bug 13386258-VC */
5693            IF (l_new_batch = 1) THEN
5694            -- {
5695            l_step_tab(i).materials := SYSTEM.gmf_matl_tab();  -- Bug 7317270
5696            -- Only come in here for single step routings
5697             IF l_debug_level >= l_debug_level_medium THEN    -- B9131983
5698              fnd_file.put_line(fnd_file.log,' Opening materials_cursor_nr cursor: single step routings');
5699             END IF;
5700 
5701            OPEN  materials_cursor_nr (transaction_row.doc_id);
5702            FETCH materials_cursor_nr BULK COLLECT INTO l_step_tab(i).materials ;
5703            CLOSE materials_cursor_nr;
5704             /* Bug 13386258-VC */
5705             IF (l_cost_alloc_profile = 1) THEN -- cache the batch
5706 
5707               c_batch_details(transaction_row.doc_id).step_tab(i).materials:=l_step_tab(i).materials;
5708 
5709 	      IF l_debug_level >= l_debug_level_high THEN
5710         fnd_file.put_line
5711           (fnd_file.log,'START materials_NR CACHING, Number of Materials = '
5712           || c_batch_details(transaction_row.doc_id).step_tab(i).materials.count );
5713 	      END IF;
5714 
5715             END IF;
5716 
5717            END IF;
5718            --  }
5719          ELSE
5720 
5721            /* Bug 13386258-VC */
5722            IF (l_new_batch = 1) THEN
5723            -- {
5724            l_step_tab(i).materials := SYSTEM.gmf_matl_tab();  -- Bug 7317270
5725            -- Get here if dependenies exist, so attach inventory to the correct step.
5726            IF l_debug_level >= l_debug_level_medium THEN    -- B9131983
5727             fnd_file.put_line(fnd_file.log,' Opening materials_cursor ');
5728            END IF;
5729 
5730            OPEN  materials_cursor (transaction_row.doc_id, l_step_tab(i).current_step_id);
5731            FETCH materials_cursor BULK COLLECT INTO l_step_tab(i).materials;
5732            CLOSE materials_cursor;
5733 
5734            -- B3556291
5735            -- Now need to see if there were any lines that were not associated to a step. If so,
5736            -- associate the ingredients with the first step and the products with the last step
5737 
5738            IF i = 1 THEN
5739             IF l_debug_level >= l_debug_level_medium THEN    -- B9131983
5740              fnd_file.put_line(fnd_file.log,' Opening unassociated_ings_cursor cursor: Only One Ingrad');
5741             END IF;
5742 
5743              OPEN unassociated_ings_cursor (transaction_row.doc_id);
5744              FETCH unassociated_ings_cursor BULK COLLECT INTO ing_tab;
5745              CLOSE unassociated_ings_cursor;
5746 
5747              FOR j IN 1..ing_tab.COUNT
5748              LOOP
5749                l_step_tab(i).materials.EXTEND;
5750                l_step_tab(i).materials(l_step_tab(i).materials.COUNT) := ing_tab(j);
5751              END LOOP;
5752            END IF;
5753 
5754            IF i = l_step_tab.COUNT THEN
5755             IF l_debug_level >= l_debug_level_medium THEN    -- B9131983
5756               fnd_file.put_line(fnd_file.log,' Opening unassociated_prds_cursor cursor');
5757             END IF;
5758 
5759              OPEN unassociated_prds_cursor (transaction_row.doc_id);
5760              FETCH unassociated_prds_cursor BULK COLLECT INTO prd_tab;
5761              CLOSE unassociated_prds_cursor;
5762 
5763              FOR j IN 1..prd_tab.COUNT
5764              LOOP
5765                l_step_tab(i).materials.EXTEND;
5766                l_step_tab(i).materials(l_step_tab(i).materials.COUNT) := prd_tab(j);
5767              END LOOP;
5768            END IF;
5769            -- B3556291 end
5770 
5771             /* Bug 13386258-VC */
5772             IF (l_cost_alloc_profile = 1) THEN -- cache the batch
5773 
5774               c_batch_details(transaction_row.doc_id).step_tab(i).materials:= l_step_tab(i).materials;
5775 	      IF l_debug_level >= l_debug_level_high THEN
5776         fnd_file.put_line
5777           (fnd_file.log,'START materials CACHING, Number of Materials = '
5778           || c_batch_details(transaction_row.doc_id).step_tab(i).materials.count );
5779               END IF;
5780             END IF;
5781            END IF;
5782            --}
5783 
5784          END IF;
5785 
5786          -- If transactions have been reversed then don't attempt to cost them. Simply set both transactions'
5787          -- quantities to zero, so that they'll be ignored.
5788 
5789          IF l_routing = 0 THEN    /* l_routing = 0 */
5790            l_step_tab(1).step_qty := 0;
5791 
5792            FOR j IN 1..l_step_tab(1).materials.COUNT
5793            LOOP
5794 
5795              IF l_step_tab(1).materials(j).step_contribution = 'Y'
5796              AND l_step_tab(1).materials(j).line_type = -1
5797              AND l_step_tab(1).materials(j).trans_qty <> 0
5798              THEN
5799 
5800             IF l_step_tab(1).materials(j).trans_um <> l_step_tab(1).step_qty_uom THEN
5801 
5802                l_tran_qty := 0;       -- B9131983
5803                l_tran_qty :=
5804                INV_CONVERT.INV_UM_CONVERT(ITEM_ID       => l_step_tab(1).materials(j).item_id
5805                                          ,LOT_NUMBER    => l_step_tab(1).materials(j).lot_number
5806                                          ,PRECISION     => 5
5807                                          ,ORGANIZATION_ID => transaction_row.orgn_id
5808                                          ,FROM_QUANTITY => -l_step_tab(1).materials(j).trans_qty
5809                                          ,FROM_UNIT     => l_step_tab(1).materials(j).trans_um
5810                                          ,TO_UNIT       => l_step_tab(1).step_qty_uom
5811                                          ,FROM_NAME     => NULL
5812                                          ,TO_NAME       => NULL
5813                                            );
5814             -- IF l_tran_qty < 0 PK Bug 9069363 check for -99999
5815              IF l_tran_qty = -99999  THEN      -- B9131983
5816 
5817           l_orgn_code := NULL;
5818           l_item_no := NULL;
5819           organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_orgn_code, l_item_no) ;
5820 
5821      			 fnd_file.put_line
5822 			     (fnd_file.log,'ERROR: Unable to convert from '
5823 			     ||l_step_tab(1).materials(j).trans_um
5824 			     ||' to '||l_step_tab(1).step_qty_uom||' for transaction ID '
5825 			     ||l_step_tab(1).materials(j).trans_id
5826 		       ||' for item ID '
5827 		       ||l_step_tab(1).materials(j).item_id
5828            ||'('||l_item_no||')'
5829 		       ||' for lot Number '
5830 		       ||l_step_tab(1).materials(j).lot_number
5831 		       ||' qty '
5832 		       ||l_step_tab(1).materials(j).trans_qty
5833 		       ||' Returned Value '||l_tran_qty
5834 			     );
5835                  l_return_status := 'E';
5836                  l_tmp := FALSE;
5837                  -- Bug 13038249 Mark the batch as uncostable
5838                  l_uncostable_batches_tab(transaction_row.doc_id) := transaction_row.doc_id;
5839                  RETURN;
5840              END IF;
5841            ELSE
5842               l_tran_qty := -l_step_tab(1).materials(j).trans_qty ;
5843            END IF;
5844 
5845                l_step_tab(1).step_qty := l_step_tab(1).step_qty + l_tran_qty;
5846              END IF;
5847            END LOOP;
5848          END IF;   /*  l_routing = 0 */
5849 
5850 		IF (l_cost_alloc_profile <> 1) THEN /*B13386258(b)- Start if of l_cost_alloc_profile <> 1 */
5851         -- Only consider lot costed products for output_qty Start
5852          l_step_tab(i).output_qty := 0;
5853 
5854          FOR j IN 1..l_step_tab(i).materials.COUNT
5855          LOOP
5856            IF l_step_tab(i).materials(j).line_type = 1
5857            AND l_step_tab(i).materials(j).lot_costed_flag = 1 THEN
5858 
5859              /***** Bug 4094132 - Girish Jha - Begin *****/
5860            -- Bug 13386258-VC
5861     	      IF ((l_step_tab(i).materials(j).trans_id = transaction_row.transaction_id AND transaction_row.reverse_id IS NOT NULL) OR
5862     	          (l_step_tab(i).materials(j).trans_id = NVL(transaction_row.reverse_id, 0))) THEN  -- Bug 13386258-VC.
5863                 l_step_output_qty := 0;
5864 
5865   	      ELSE
5866 	        l_step_output_qty := NULL;
5867  	      END IF;
5868               /***** Bug 4094132 - Girish Jha - End *****/
5869             IF l_step_tab(i).materials(j).trans_um <> l_step_tab(i).step_qty_uom THEN
5870 
5871               l_tran_qty := 0;   -- B9131983
5872               l_tran_qty :=
5873                INV_CONVERT.INV_UM_CONVERT(ITEM_ID       => l_step_tab(i).materials(j).item_id
5874                                          ,LOT_NUMBER    => l_step_tab(i).materials(j).lot_number
5875                                          ,PRECISION     => 5
5876                                          ,ORGANIZATION_ID => transaction_row.orgn_id
5877                                          ,FROM_QUANTITY => nvl(l_step_output_qty, l_step_tab(i).materials(j).trans_qty)
5878                                          ,FROM_UNIT     => l_step_tab(i).materials(j).trans_um
5879                                          ,TO_UNIT       => l_step_tab(i).step_qty_uom
5880                                          ,FROM_NAME     => NULL
5881                                          ,TO_NAME       => NULL
5882                                            );
5883             IF l_tran_qty = -99999  THEN    -- B9131983
5884 
5885               l_orgn_code := NULL;
5886               l_item_no := NULL;
5887               organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_orgn_code, l_item_no) ;
5888 
5889  		          fnd_file.put_line
5890 		         (fnd_file.log,'ERROR: Unable to convert from '
5891 		         ||l_step_tab(i).materials(j).trans_um
5892 		         ||' to '||l_step_tab(i).step_qty_uom||' for transaction ID '
5893 		         ||l_step_tab(i).materials(j).trans_id
5894 		         ||' for item ID '
5895 		         ||l_step_tab(i).materials(j).item_id
5896              ||'('||l_item_no||')'
5897 		         ||' for Lot Number '
5898 		         ||l_step_tab(i).materials(j).lot_number
5899 		         ||' qty (nvl) '
5900 		         ||l_step_output_qty||' nvl part2 '||l_step_tab(i).materials(j).trans_qty
5901 		         ||' Returned Value '||l_tran_qty
5902 		         );
5903 
5904                l_return_status := 'E';
5905                l_tmp := FALSE;
5906                -- Bug 13038249 Mark the batch as uncostable
5907                l_uncostable_batches_tab(transaction_row.doc_id) := transaction_row.doc_id;
5908                RETURN;
5909              END IF;
5910 
5911              ELSE
5912                 l_tran_qty := nvl(l_step_output_qty, l_step_tab(i).materials(j).trans_qty) ;
5913              END IF;
5914 
5915              l_step_tab(i).output_qty := l_step_tab(i).output_qty + l_tran_qty;
5916            END IF;
5917          END LOOP;
5918            -- Only consider lot costed products for output_qty Ends
5919 		END IF; /*B13386258(b)- End if of l_cost_alloc_profile <> 1 */
5920 
5921          IF l_debug_level >= l_debug_level_medium THEN
5922            fnd_file.put_line
5923            (fnd_file.log,'Loaded '||to_char(l_step_tab(i).materials.count)||' material txns');
5924          END IF;
5925 
5926          FOR j in 1..l_step_tab(i).materials.count
5927          LOOP
5928            l_skip_this_txn   := FALSE; -- PK Avoid incorrect log messages
5929 
5930            -- This if condition will ignore reversal record processing, it is already set to ZERO
5931            IF l_step_tab(i).materials(j).trans_qty <> 0 THEN
5932              -- Calculate the cost of ingredients from the cost of the lot consumed.
5933              -- If the lot being consumed has not been costed, perhaps because the
5934              -- batch that produced it has not yet been certified, we skip this batch.
5935 
5936              -- The transactions are sorted by date and then by line type. This should
5937              -- give the correct ordering of transactions for the rollup.
5938 
5939              -- If item is not lot controlled or is not lot costed
5940              -- use the standard (period-based) cost from cm_cmpt_dtl,
5941              -- otherwise use the lot cost
5942              -- B9131983
5943              IF l_step_tab(i).materials(j).line_type IN (-1,2) -- Treat byproducts as negative ingredients
5944              OR l_step_tab(i).materials(j).line_type = 1 AND l_step_tab(i).materials(j).lot_costed_flag = 0
5945              THEN
5946                -- This is an ingredient attached to this step
5947 
5948                ing_cost.unit_cost := NULL;
5949 
5950                IF l_step_tab(i).materials(j).lot_costed_flag = 0 THEN
5951 	         -- umoogala: using co_code and default_cost_mthd to get costs for non-lot controlled items.
5952 	         -- was calendar_code and cost_mthd_code
5953 
5954           /* Bug 13038249  Grupo Merged the SUM query to detail query start */
5955                 IF  total_alt_cost_tab.EXISTS(1)  THEN
5956                 total_alt_cost_tab.delete;
5957                 END IF;
5958 
5959             k_period_id := 0 ;
5960             SELECT to_char(TRUNC(l_step_tab(i).materials(j).trans_date),'J')
5961             into k_date from dual  ;
5962 
5963       IF period_tab.EXISTS(k_date) THEN
5964      --    fnd_file.put_line(fnd_file.log,'period_id = '||
5965      --    period_tab(k_date).period_id );
5966          k_period_id := period_tab(k_date).period_id;
5967       ELSE
5968       --   fnd_file.put_line(fnd_file.log,'period_id = DID NOT FIND') ;
5969          k_period_id := NULL;
5970 
5971       END IF;
5972 
5973      --       fnd_file.put_line
5974      --            (fnd_file.log,  'Line 5805 = ' || l_default_cost_type_id   || '*' || l_le_id  || '*'  ||
5975      --            TO_char(l_step_tab(i).materials(j).trans_date,'DD-MON-YYYY HH24:MI:SS')   );
5976 
5977     /* Query or get the value from caching */
5978      IF Alter_cost_tab.EXISTS(l_step_tab(i).materials(j).item_id  ||'-'||
5979                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id) THEN
5980 
5981        l_step_tab(i).materials(j).cost_details := Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
5982                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).cost_details
5983                            ;
5984 
5985        total_alt_cost_tab(1) := Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
5986                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).total_cost ;
5987 
5988        --          fnd_file.put_line
5989        --          (fnd_file.log, ' TESTING RAJESH inventory_item_id GOT IT FROM CAHCE -- '
5990         --          ||Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
5991         --               l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).inventory_item_id
5992         --          || ' TOTAL COST = ' ||   total_alt_cost_tab(1)
5993         --          );
5994 
5995      ELSE
5996                  OPEN item_cost_detail_cursor
5997                        ( l_le_id
5998                        , l_default_cost_type_id
5999                        , l_step_tab(i).materials(j).organization_id
6000                        , l_step_tab(i).materials(j).item_id
6001                        , l_step_tab(i).materials(j).trans_date
6002                        , k_period_id
6003                        );
6004                  FETCH item_cost_detail_cursor BULK COLLECT INTO l_step_tab(i).materials(j).cost_details,
6005                  total_alt_cost_tab;
6006                  CLOSE item_cost_detail_cursor;
6007 
6008                  -- Alternate Period cost caching
6009                  Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6010                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).cost_details
6011                          :=  l_step_tab(i).materials(j).cost_details  ;
6012 
6013                  Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6014                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).inventory_item_id
6015                          :=   l_step_tab(i).materials(j).item_id  ;
6016 
6017                  Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6018                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).organization_id
6019                          :=   l_step_tab(i).materials(j).organization_id ;
6020 
6021                  Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6022                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).period_id
6023                          :=   k_period_id ;
6024 
6025                 IF  total_alt_cost_tab.EXISTS(1)  THEN
6026 
6027                 Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6028                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).total_cost
6029                          :=   total_alt_cost_tab(1) ;
6030                  ELSE
6031 
6032                 Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6033                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).total_cost
6034                          :=   NULL  ;
6035 
6036                  END IF;
6037 
6038        --          fnd_file.put_line
6039        --          (fnd_file.log, ' TESTING RAJESH inventory_item_id 5812 -- '
6040        --           ||Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6041        --                l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).inventory_item_id
6042         --           || ' TOTAL COST = ' ||    Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6043         --               l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).total_cost
6044         --          );
6045 
6046      END IF;  /* Query or get the value from cahcing */
6047 
6048 	         -- umoogala: using co_code and default_cost_mthd to get costs for non-lot controlled items.
6049 	         -- was calendar_code and cost_mthd_code
6050 
6051                 IF  total_alt_cost_tab.EXISTS(1)  THEN
6052                  ing_cost.unit_cost := total_alt_cost_tab(1);
6053                 ELSE
6054                   ing_cost.unit_cost := NULL ;
6055                 END IF;
6056 
6057                /*  OPEN item_cost_cursor
6058                        ( l_le_id
6059                        , l_default_cost_type_id
6060                        , l_step_tab(i).materials(j).organization_id
6061                        , l_step_tab(i).materials(j).item_id
6062                        , l_step_tab(i).materials(j).trans_date
6063                        );
6064                  FETCH item_cost_cursor INTO ing_cost.unit_cost;
6065                  CLOSE item_cost_cursor;
6066                  */
6067 
6068           /* Bug 13038249  Grupo Merged the SUM query to detail query start */
6069 
6070 
6071                  /* can item_cost_cursor be replaced with something like below
6072                  FOR k IN 1 .. l_step_tab(i).materials(j).COUNT
6073                  LOOP
6074                    ing_cost.unit_cost := NVL(ing_cost.unit_cost , 0) + ???
6075                  END LOOP;  */
6076 
6077                  IF ing_cost.unit_cost IS NULL THEN
6078                    l_skip_this_batch := TRUE;
6079                    l_skip_this_txn   := TRUE; -- PK Avoid incorrect log messages
6080                  END IF;
6081 
6082                ELSE
6083 --                IF l_debug_level >= l_debug_level_medium THEN     -- B9131983
6084 --          			 fnd_file.put_line
6085 --		     	     (fnd_file.log,' If Item Lot costed then Org: '
6086 --			         || l_step_tab(i).materials(j).organization_id
6087 --			         ||' transaction ID '
6088 --			         ||l_step_tab(i).materials(j).trans_id
6089 --		           ||' item ID '
6090 --		           ||l_step_tab(i).materials(j).item_id
6091 --	             ||' lot Number '
6092 --		           ||l_step_tab(i).materials(j).lot_number
6093 --		           ||' Date '
6094 --		           ||l_step_tab(i).materials(j).trans_date
6095 --               ||' Cpst TYPE ' || l_cost_type_id
6096 --			          );
6097 --                END IF;
6098 
6099                  ing_cost.header_id := NULL;
6100                  -- Bug 13038249 Query recent costs first
6101                  IF l_step_tab(i).materials(j).trans_date >= sysdate-60 THEN
6102 
6103                    OPEN  lot_cost_cursor_1 ( l_step_tab(i).materials(j).organization_id
6104                                          , l_step_tab(i).materials(j).item_id
6105                                          , l_step_tab(i).materials(j).lot_number
6106                                          , l_step_tab(i).materials(j).trans_date      -- Bug 4130869 Added Date field
6107                                          , l_cost_type_id
6108                                          );
6109                    FETCH lot_cost_cursor_1 INTO ing_cost;
6110                    CLOSE lot_cost_cursor_1;
6111                  END IF;
6112 
6113                  IF ing_cost.header_id IS NULL THEN
6114 
6115                    OPEN  lot_cost_cursor ( l_step_tab(i).materials(j).organization_id
6116                                          , l_step_tab(i).materials(j).item_id
6117                                          , l_step_tab(i).materials(j).lot_number
6118                                          , l_step_tab(i).materials(j).trans_date      -- Bug 4130869 Added Date field
6119                                          , l_cost_type_id
6120                                          );
6121                    FETCH lot_cost_cursor INTO ing_cost;
6122                    CLOSE lot_cost_cursor;
6123                  END IF;
6124 
6125                  IF ing_cost.header_id IS NOT NULL THEN
6126                    OPEN  lot_cost_detail_cursor
6127                          ( ing_cost.header_id );
6128                    FETCH lot_cost_detail_cursor BULK COLLECT INTO l_step_tab(i).materials(j).cost_details;
6129                    CLOSE lot_cost_detail_cursor;
6130                  ELSE
6131                    l_skip_this_batch := TRUE;
6132                    l_skip_this_txn   := TRUE; -- PK Avoid incorrect log messages
6133                  END IF;
6134 
6135                END IF;
6136 
6137                -- IF l_skip_this_batch PK Avoid incorrect log messages
6138 
6139                IF l_skip_this_txn  THEN
6140 
6141                  fnd_file.put_line
6142                  (fnd_file.log, 'ERROR: Cannot calculate cost of lot '
6143                   ||to_char(transaction_row.lot_number)
6144                   ||' because material transaction '
6145                   ||to_char(l_step_tab(i).materials(j).trans_id)
6146                   ||' cannot be costed'
6147                  );
6148 
6149                ELSE
6150                  FOR k IN 1..l_step_tab(i).materials(j).cost_details.count
6151                  LOOP
6152                    l_step_tab(i).materials(j).trans_cost := l_step_tab(i).materials(j).trans_cost
6153                      + -1*l_step_tab(i).materials(j).trans_qty * l_step_tab(i).materials(j).cost_details(k).component_cost;
6154                    l_cost_accrued := FALSE;
6155 
6156                    FOR l IN 1..l_step_tab(i).current_costs.count
6157                    LOOP
6158                      IF   l_step_tab(i).materials(j).cost_details(k).cost_cmpntcls_id
6159                          =l_step_tab(i).current_costs(l).cost_cmpntcls_id
6160                      AND  l_step_tab(i).materials(j).cost_details(k).cost_analysis_code
6161                          =l_step_tab(i).current_costs(l).cost_analysis_code
6162                      AND  l_step_tab(i).materials(j).cost_details(k).cost_level
6163                          =l_step_tab(i).current_costs(l).cost_level
6164                      THEN
6165                        l_step_tab(i).current_costs(l).component_cost :=
6166                           l_step_tab(i).current_costs(l).component_cost
6167                         + -1*l_step_tab(i).materials(j).trans_qty
6168                             *l_step_tab(i).materials(j).cost_details(k).component_cost;
6169                        l_cost_accrued := TRUE;
6170                        EXIT;
6171                      END IF;
6172                    END LOOP;
6173 
6174                    IF NOT l_cost_accrued THEN
6175                      IF l_step_tab(i).current_costs(1).cost_analysis_code = ' ' THEN
6176                        l_step_tab(i).current_costs(1) := l_step_tab(i).materials(j).cost_details(k);
6177                        l_step_tab(i).current_costs(1).component_cost :=
6178                          -l_step_tab(i).materials(j).trans_qty * l_step_tab(i).materials(j).cost_details(k).component_cost;
6179                      ELSE
6180                        l_step_tab(i).current_costs.EXTEND;
6181                        l := l_step_tab(i).current_costs.count;
6182                        l_step_tab(i).current_costs(l) :=
6183                          l_step_tab(i).materials(j).cost_details(k);
6184                        l_step_tab(i).current_costs(l).component_cost :=
6185                          -1*l_step_tab(i).materials(j).trans_qty
6186                            *l_step_tab(i).materials(j).cost_details(k).component_cost;
6187                      END IF;
6188                    END IF;
6189                  END LOOP;
6190                END IF;
6191              ELSE
6192                -- This is a product/coproduct that is yielded at this step. If the item
6193                -- being yielded uses standard costing we extract the cost so that we can
6194                -- work out the cost of this transaction and then include it in the total
6195                -- for this step. Any items that use lot costing have these costs calculated
6196                -- after we've rolled up the costs into the step from where the lot is yielded
6197 
6198                IF l_step_tab(i).materials(j).lot_number IS NULL /* INVCONV sshchinch */
6199                OR l_step_tab(i).materials(j).lot_costed_flag = 0 THEN
6200 	               -- umoogala: using co_code and default_cost_mthd to get costs for non-lot controlled items.
6201 	               -- was calendar_code and cost_mthd_code
6202 
6203           /* Bug 13038249 Grupo Merged the SUM query to detail query start */
6204 
6205                 IF  total_alt_cost_tab.EXISTS(1)  THEN
6206                 total_alt_cost_tab.delete;
6207                 END IF;
6208 
6209             k_period_id := 0 ;
6210             select to_char(TRUNC(l_step_tab(i).materials(j).trans_date),'J')
6211             into k_date from dual  ;
6212 
6213       IF period_tab.EXISTS(k_date) THEN
6214      --    fnd_file.put_line(fnd_file.log,'period_id = '||
6215      --    period_tab(k_date).period_id );
6216          k_period_id := period_tab(k_date).period_id;
6217       ELSE
6218      --    fnd_file.put_line(fnd_file.log,'period_id = DID NOT FIND') ;
6219          k_period_id := NULL;
6220 
6221       END IF;
6222 
6223      --       fnd_file.put_line
6224      --            (fnd_file.log,  'Line 6087 = ' || l_default_cost_type_id   || '*' || l_le_id  || '*'  ||
6225      --            TO_char(l_step_tab(i).materials(j).trans_date,'DD-MON-YYYY HH24:MI:SS')   );
6226 
6227     /* Query or get the value from caching */
6228      IF Alter_cost_tab.EXISTS(l_step_tab(i).materials(j).item_id  ||'-'||
6229                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id) THEN
6230 
6231        l_step_tab(i).materials(j).cost_details := Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6232                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).cost_details
6233                            ;
6234 
6235        total_alt_cost_tab(1) := Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6236                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).total_cost ;
6237 
6238         --         fnd_file.put_line
6239         --         (fnd_file.log, ' TESTING RAJESH inventory_item_id GOT IT FROM CAHCE -- '
6240         --          ||Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6241         --               l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).inventory_item_id
6242         --          || ' TOTAL COST = ' ||   total_alt_cost_tab(1)
6243         --          );
6244 
6245      ELSE
6246 
6247                  OPEN item_cost_detail_cursor
6248                  ( l_le_id
6249                  , l_default_cost_type_id
6250                  , l_step_tab(i).materials(j).organization_id
6251                  , l_step_tab(i).materials(j).item_id
6252                  , l_step_tab(i).materials(j).trans_date
6253                  , k_period_id
6254                  );
6255                  FETCH item_cost_detail_cursor BULK COLLECT INTO l_step_tab(i).materials(j).cost_details,
6256                  total_alt_cost_tab;
6257                  CLOSE item_cost_detail_cursor;
6258 
6259 
6260                  -- Alternate Period cost caching
6261                  Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6262                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).cost_details
6263                          :=   l_step_tab(i).materials(j).cost_details  ;
6264 
6265                  Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6266                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).inventory_item_id
6267                          :=   l_step_tab(i).materials(j).item_id  ;
6268 
6269                  Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6270                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).organization_id
6271                          :=   l_step_tab(i).materials(j).organization_id ;
6272 
6273                  Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6274                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).period_id
6275                          :=   k_period_id ;
6276 
6277                 IF  total_alt_cost_tab.EXISTS(1)  THEN
6278 
6279                 Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6280                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).total_cost
6281                          :=   total_alt_cost_tab(1) ;
6282                  ELSE
6283 
6284                 Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6285                        l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).total_cost
6286                          :=   NULL  ;
6287 
6288                  END IF;
6289 
6290      --            fnd_file.put_line
6291      --            (fnd_file.log, ' TESTING RAJESH inventory_item_id 6103 -- '
6292      --             ||Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6293      --                  l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).inventory_item_id
6294      --              || ' TOTAL COST = ' ||    Alter_cost_tab(l_step_tab(i).materials(j).item_id  ||'-'||
6295       --                 l_step_tab(i).materials(j).organization_id ||'-'|| k_period_id).total_cost
6296       --            );
6297 
6298      END IF;  /* Query or get the value from cahcing */
6299 
6300 	               -- umoogala: using co_code and default_cost_mthd to get costs for non-lot controlled items.
6301 	               -- was calendar_code and cost_mthd_code
6302              /*    OPEN item_cost_cursor
6303                  ( l_le_id
6304                  , l_default_cost_type_id
6305                  , l_step_tab(i).materials(j).organization_id
6306                  , l_step_tab(i).materials(j).item_id
6307                  , l_step_tab(i).materials(j).trans_date
6308                  );
6309                  FETCH item_cost_cursor INTO prd_cost.unit_cost;
6310                  CLOSE item_cost_cursor;
6311                */
6312 
6313                 IF  total_alt_cost_tab.EXISTS(1)  THEN
6314                  prd_cost.unit_cost := total_alt_cost_tab(1);
6315                 ELSE
6316                   prd_cost.unit_cost := NULL ;
6317                 END IF;
6318 
6319           /* Bug 13038249 Grupo Merged the SUM query to detail query Ends */
6320 
6321                  IF NOT l_step_tab(i).materials(j).cost_details.EXISTS(1) THEN
6322                  l_orgn_code := NULL;
6323                  l_item_no := NULL;
6324                  organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_orgn_code, l_item_no) ;
6325                    fnd_file.put_line
6326                    (fnd_file.log, 'ERROR: Cannot calculate cost of Lot Number '
6327                     ||to_char(transaction_row.lot_number)
6328                     ||' because item ID / Lot Number '
6329                     ||to_char(l_step_tab(i).materials(j).item_id)
6330                     ||'/'
6331                     ||'('||l_item_no||')'
6332                     ||to_char(l_step_tab(i).materials(j).lot_number)
6333                     ||' cannot be costed'
6334                    );
6335                    l_skip_this_batch := TRUE;
6336                  ELSE
6337                    FOR k IN 1..l_step_tab(i).materials(j).cost_details.count
6338                    LOOP
6339                      l_step_tab(i).materials(j).trans_cost := l_step_tab(i).materials(j).trans_cost
6340                      + -1*l_step_tab(i).materials(j).trans_qty * l_step_tab(i).materials(j).cost_details(k).component_cost;
6341 
6342                      l_cost_accrued := FALSE;
6343 
6344                      FOR l IN 1..l_step_tab(i).current_costs.count
6345                      LOOP
6346                        IF   l_step_tab(i).materials(j).cost_details(k).cost_cmpntcls_id
6347                            =l_step_tab(i).current_costs(l).cost_cmpntcls_id
6348                        AND  l_step_tab(i).materials(j).cost_details(k).cost_analysis_code
6349                            =l_step_tab(i).current_costs(l).cost_analysis_code
6350                        AND  l_step_tab(i).materials(j).cost_details(k).cost_level
6351                            =l_step_tab(i).current_costs(l).cost_level
6352                        THEN
6353                          l_step_tab(i).current_costs(l).component_cost :=
6354                            l_step_tab(i).current_costs(l).component_cost
6355                          + -1*l_step_tab(i).materials(j).trans_qty * l_step_tab(i).materials(j).cost_details(k).component_cost;
6356                          l_cost_accrued := TRUE;
6357                          EXIT;
6358                        END IF;
6359                      END LOOP;
6360 
6361                      IF NOT l_cost_accrued THEN
6362                        IF l_step_tab(i).current_costs(1).cost_analysis_code = ' ' THEN
6363                          l_step_tab(i).current_costs(1) :=
6364                            l_step_tab(i).materials(j).cost_details(k);
6365                          l_step_tab(i).current_costs(1).component_cost :=
6366                            l_step_tab(i).materials(j).trans_qty * l_step_tab(i).materials(j).cost_details(k).component_cost;
6367                        ELSE
6368                          l_step_tab(i).current_costs.EXTEND;
6369                          l := l_step_tab(i).current_costs.count;
6370                          l_step_tab(i).current_costs(l) :=
6371                            l_step_tab(i).materials(j).cost_details(k);
6372                          l_step_tab(i).current_costs(l).component_cost :=
6373                           -1*l_step_tab(i).materials(j).trans_qty * l_step_tab(i).materials(j).cost_details(k).component_cost;
6374                        END IF;
6375                      END IF;
6376                    END LOOP;
6377                  END IF;
6378                END IF;
6379              END IF;
6380            END IF; -- If trans_qty <> 0
6381         END LOOP;
6382 
6383         IF l_debug_level >= l_debug_level_medium THEN
6384           fnd_file.put_line
6385           (fnd_file.log,'Setting up step dependency index and quantities');
6386         END IF;
6387 
6388         -- Set up the step_index field in the 'next steps' nested tables
6389 
6390         FOR j IN 1..l_step_tab(i).dependencies.count
6391         LOOP
6392           FOR k IN 1..l_step_tab.count
6393           LOOP
6394             IF l_step_tab(k).current_step_id = l_step_tab(i).dependencies(j).batchstep_id
6395             THEN
6396 
6397               IF l_debug_level >= l_debug_level_medium THEN
6398                 fnd_file.put_line
6399                 (fnd_file.log,'Setting ('||to_char(i)||','||to_char(j)||') to '||to_char(k));
6400               END IF;
6401 
6402                l_step_tab(i).dependencies(j).step_index := k;
6403 
6404               EXIT;
6405             END IF;
6406           END LOOP;
6407         END LOOP;
6408 
6409         IF l_debug_level >= l_debug_level_medium THEN
6410           fnd_file.put_line
6411           (fnd_file.log,'End of transaction load/costing loop for iteration '||to_char(i));
6412         END IF;
6413 
6414       END LOOP; -- End of loop that retrieves and costs the transactions
6415 
6416       -- Start of the actual rollup
6417 
6418       IF l_skip_this_batch THEN
6419         -- For one reason or another we cannot cost this lot.
6420         fnd_file.put_line
6421         ( fnd_file.log,'ERROR: Batch No(Batch_ID) '||l_batch_no||'('
6422         ||to_char(transaction_row.doc_id)||') was skipped because of missing cost(s)');
6423         l_return_status := 'E';
6424     --    l_uncostable_lots_tab(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number) := transaction_row.inventory_item_id;
6425     --    copy_uncostable_record ;    /* B9894310 */
6426         l_tmp := FALSE;
6427         -- Bug 13038249 Mark the batch as uncostable
6428         l_uncostable_batches_tab(transaction_row.doc_id) := transaction_row.doc_id;
6429 
6430         RETURN;
6431       ELSE
6432 
6433 
6434         -- At this stage we have a complete explosion of the batch in terms of materials
6435         -- consumed and yielded, and also resources expended, together with all costs for
6436         -- each consumption and expenditure. We now need to traverse the routing
6437         -- to roll up the costs from the starting step(s) to the step where the lot is yielded.
6438 
6439         -- The explosion of the routing honours the fact that steps can converge and also
6440         -- diverge into any arbitrary topology. The only thing that is not allowed is a feedback
6441         -- loop.
6442 
6443         -- Traversing the routing is simplified by the explosion method, which orders the steps
6444         -- in the correct sequence. We can start at the first node (step) and examine the
6445         -- nested table in it to see what the next step/steps is/are. If there is only one 'next'
6446         -- step the costs accumulated in the current step are passed on. If there is more than
6447         -- one next step the costs accumulated so far are passed on in direct proportion to
6448         -- their actual step quantities. If there are no next steps we have reached a terminal
6449         -- step in the routing.
6450 
6451         -- Note that if anything is yielded at the current step, the cost of it is subtracted
6452         -- and only the residual costs are rolled into the next steps.
6453 
6454         -- We have the following values for each step, stored in table l_step_tab:
6455 
6456         -- current_step_id (ie the batchstep_id of the current step)
6457         -- current_costs   (ie all costs incurred at this step)
6458         -- inherited_costs (ie all of the costs from prior steps)
6459         -- dependencies    (ie a list of steps that directly follow on from this step)
6460 
6461         -- Each element of the dependencies list has the following components
6462 
6463         -- batchstep_id       (the ID of the step)
6464         -- step_qty (the material quantity inherited from prior steps)
6465         -- step_qty_uom       (the unit of measure of the above)
6466         -- step_index         (this is the index of the step in l_step_tab)
6467 
6468         IF l_debug_level >= l_debug_level_low THEN
6469           fnd_file.put_line
6470         ( fnd_file.log,'Transaction costs complete. Starting rollup for Batch No(Batch_ID) '||l_batch_no||'('
6471         ||to_char(transaction_row.doc_id)||')');
6472         END IF;
6473 
6474         FOR i in 1..l_step_tab.count
6475         LOOP
6476           IF l_debug_level >= l_debug_level_medium THEN
6477             fnd_file.put_line
6478             (fnd_file.log,'Inside rollup loop for step index '||to_char(i));
6479           END IF;
6480 
6481           -- We need to set up the step_costs table. This holds the combined inherited costs (that is
6482           -- those that have come from prior steps) and the current costs (ie those that have been
6483           -- introduced at this step. This is done so that any yields from this step can be calculated
6484           -- a bit more easily.
6485 
6486           new_cost_tab.delete;
6487           cur_cost_tab.delete;
6488 
6489           FOR j IN 1.. l_step_tab(i).inherited_costs.COUNT
6490           LOOP
6491             new_cost_tab(j) := l_step_tab(i).inherited_costs(j);
6492           END LOOP;
6493 
6494           FOR j IN 1..l_step_tab(i).current_costs.COUNT
6495           LOOP
6496             cur_cost_tab(j) := l_step_tab(i).current_costs(j);
6497           END LOOP;
6498 
6499           merge_costs ( cur_cost_tab
6500                       , 1
6501                       , 1
6502                       , 'C'
6503                       );
6504 
6505           FOR j IN 1..new_cost_tab.COUNT
6506           LOOP
6507             l_step_tab(i).step_costs(j) := new_cost_tab(j);
6508             l_step_tab(i).step_costs.EXTEND;
6509           END LOOP;
6510 
6511           -- Get rid of the unused last entry
6512 
6513           l_step_tab(i).step_costs.DELETE(l_step_tab(i).step_costs.COUNT);
6514 
6515           -- See if we've yielded anything that is lot_costed in this step.
6516           -- We've yielded something. Find its cost and, if needed,  update the
6517           -- lot costing tables. If there isn't an existing cost then we have to
6518           -- create a new one.
6519 
6520           FOR j IN 1..l_step_tab(i).materials.count  -- Main Processing
6521           LOOP
6522 
6523        /*           fnd_file.put_line
6524                 ( fnd_file.log,'#TITAN#2a## (i/j='||i||'/'||j||') line_type/lot_costed_flag/step_index/transaction_id/trans_id/LotNum1/LotNum2 = '
6525                            ||l_step_tab(i).materials(j).line_type||' /'||l_step_tab(i).materials(j).lot_costed_flag
6526                            ||' /'||l_step_tab(i).dependencies(1).step_index
6527                            ||' /'||transaction_row.transaction_id ||' /'|| l_step_tab(i).materials(j).trans_id
6528                            ||' /'||l_step_tab(i).materials(j).lot_number||' /'||transaction_row.lot_number
6529                            );
6530        */
6531 
6532             /* B13386258 - change for Terminal Step */
6533             IF  l_step_tab(i).materials(j).line_type = 1 -- Ignore byproducts
6534             AND l_step_tab(i).materials(j).lot_costed_flag = 1
6535             and ((l_step_tab(i).dependencies(1).step_index IS NOT NULL )
6536 			      OR (l_step_tab(i).dependencies(1).step_index IS NULL  -- Terminal Step
6537                        and transaction_row.transaction_id = l_step_tab(i).materials(j).trans_id
6538                        AND l_step_tab(i).materials(j).lot_number = transaction_row.lot_number)
6539 				       )
6540             THEN
6541               new_cost.onhand_qty := l_step_tab(i).materials(j).trans_qty;
6542               new_cost.unit_cost := 0;
6543               new_cost_tab.delete;
6544               old_cost.onhand_qty := 0;
6545               old_cost_tab.delete;
6546               cur_cost_tab.delete; /* Bug 3533452 */
6547 
6548               IF l_debug_level >= l_debug_level_medium THEN
6549                 fnd_file.put_line(fnd_file.log,'Setting up new_cost_tab');
6550               END IF;
6551 
6552               -- Bug 3548217
6553               IF l_step_tab(i).step_qty_uom <> l_step_tab(i).materials(j).trans_um THEN
6554               l_tran_qty := 0;   -- B9131983
6555               l_tran_qty :=
6556                INV_CONVERT.INV_UM_CONVERT(ITEM_ID       => l_step_tab(i).materials(j).item_id
6557                                          ,LOT_NUMBER    => l_step_tab(i).materials(j).lot_number
6558                                          ,PRECISION     => 5
6559                                          ,ORGANIZATION_ID => transaction_row.orgn_id
6560                                          ,FROM_QUANTITY => l_step_tab(i).materials(j).trans_qty
6561                                          ,FROM_UNIT     => l_step_tab(i).materials(j).trans_um
6562                                          ,TO_UNIT       => l_step_tab(i).step_qty_uom
6563                                          ,FROM_NAME     => NULL
6564                                          ,TO_NAME       => NULL
6565                                            );
6566                 -- PK Bug 9069363 do not return for all negative quatities.
6567                 IF l_tran_qty = -99999 THEN      -- B9131983
6568                   fnd_file.put_line
6569                   (fnd_file.log,'ERROR: Unable to convert to step qty uom for transaction ID '
6570                   ||l_step_tab(i).materials(j).trans_id
6571                   );
6572                   l_return_status := 'E';
6573                   l_tmp := FALSE;
6574                   -- Bug 13038249 Mark the batch as uncostable
6575                   l_uncostable_batches_tab(transaction_row.doc_id) := transaction_row.doc_id;
6576                   RETURN;
6577                 END IF;
6578               ELSE
6579                 l_tran_qty := l_step_tab(i).materials(j).trans_qty;
6580               END IF;
6581 
6582               -- Bug 3548217
6583               -- If we're on a terminal step we have to set up the cost factor
6584               -- so that all costs will be absorbed by the products yielded. If
6585               -- the step is higher up the chain the we use the step qty instead so that all
6586               -- residual costs are passed on to the next steps.
6587               /** Bug 9616762  Front port of 4601607 No need for different logic for products yielded
6588                 * in terminal step or non-terminal steps
6589                 */
6590 
6591              --  IF l_step_tab(i).dependencies(1).step_index IS NULL THEN
6592 
6593                  /***** Bug 4094132 - Girish Jha - Begin *****/
6594                   IF (l_step_tab(i).materials(j).trans_id = transaction_row.transaction_id AND transaction_row.reverse_id IS NOT NULL) THEN
6595                     l_step_output_qty := 0;
6596                   ELSE
6597                     l_step_output_qty := NULL;
6598                   END IF;
6599                   /***** Bug 4094132 - Girish Jha - End *****/
6600 
6601                 -- We're on a terminal step
6602 
6603                 /***** Bug 4057323 - Use Cost Allocation Factor Depending on Profile value - Start *****/
6604                 IF ( l_cost_alloc_profile = 1) THEN
6605                   l_cost_factor := l_step_tab(i).materials(j).cost_alloc;
6606                 ELSE  /* Else approtion the cost by using the quantities as the ratio */
6607                     /* Quantity in the batch step is not zero then */
6608                   IF l_step_tab(i).output_qty<>0 THEN   /*Condition Added - Bug 5985680, pmarada  */
6609                     l_cost_factor := (l_tran_qty /l_step_tab(i).output_qty);
6610                   ELSE
6611                     l_cost_factor := 0;
6612                     fnd_file.put_line (fnd_file.log,' Setting Cost Allocation Factor to zero as output_qty is zero.');
6613                   END IF;
6614                 END IF;
6615                 /***** Bug 4057323 - Use Cost Allocation Factor Depending on Profile value - End *****/
6616 
6617                 IF l_debug_level >= l_debug_level_high THEN
6618                    fnd_file.put_line (fnd_file.log,' Cost Allocation Factor :  For the transaction of '||l_step_tab(i).materials(j).trans_id
6619                                                 || ' the new cost_factor is '|| l_cost_factor);
6620                  END IF;
6621 
6622                 -- Reduce remaining output_qty for next time around
6623 
6624                 -- Bug 4094132 Added NVL(l_step_output_qty, .....
6625                 -- l_step_tab(i).output_qty := l_step_tab(i).output_qty - l_tran_qty;
6626                 l_step_tab(i).output_qty := l_step_tab(i).output_qty - NVL(l_step_output_qty,l_tran_qty);
6627 
6628               /******* Bug 9616762  Front port of 4601607 - Use the same logic as above even for products
6629                * yielded in intermediate steps. As far as our logic is concerned all products
6630                * will be yielded in one step.
6631                *******/
6632 
6633               -- ELSE  /* If not terminal step */
6634                   /***** Bug 4057323 - Use Cost Allocation Factor Depending on Profile value - Start *****/
6635               --     IF ( l_cost_alloc_profile = 1) THEN
6636               --      l_cost_factor := l_step_tab(i).materials(j).cost_alloc;
6637               --    ELSE  /* Else approtion the cost by using the quantities as the ratio of step quantities*/
6638               --      IF l_step_tab(i).step_qty <> 0 THEN  /*Condition Added - Bug 5985680, pmarada  */
6639               --        l_cost_factor := l_tran_qty / l_step_tab(i).step_qty;
6640               --      ELSE
6641               --        l_cost_factor := 0;
6642               --        fnd_file.put_line (fnd_file.log,' Setting Cost Allocation Factor to zero as step_qty is zero.');
6643               --      END IF;
6644               --    END IF;
6645                   /***** Bug 4057323 - Use Cost Allocation Factor Depending on Profile value - End *****/
6646               -- END IF;
6647               /******* Bug 9616762  Front port of 4601607 - End */
6648 
6649               IF l_debug_level >= l_debug_level_low THEN
6650                 fnd_file.put_line
6651                 (fnd_file.log,'Cost factor for yield costs calculation is '||l_cost_factor);
6652 
6653                 fnd_file.put_line(fnd_file.log,'Step costs before are');
6654                 FOR jj IN 1..l_step_tab(i).step_costs.COUNT
6655                 LOOP
6656                   fnd_file.put_line
6657                   (fnd_file.log,l_step_tab(i).step_costs(jj).component_cost);
6658                 END LOOP;
6659               END IF;
6660 
6661               -- Now calculate the costs of the yielded lot and reduce the step costs by the same amounts
6662 
6663               FOR l IN 1 ..l_step_tab(i).step_costs.COUNT
6664               LOOP
6665                 new_cost_tab(l) := l_step_tab(i).step_costs(l);
6666 
6667                 /***** Bug 4057323 - Use Cost Allocation Factor Depending on Profile value - Start *****/
6668                 IF ( l_cost_alloc_profile = 0 ) THEN
6669                   new_cost_tab(l).component_cost := l_step_tab(i).step_costs(l).component_cost * l_cost_factor;              -- Bug 4057323
6670                   new_cost.unit_cost := new_cost.unit_cost + new_cost_tab(l).component_cost;
6671                   l_step_tab(i).step_costs(l).component_cost := l_step_tab(i).step_costs(l).component_cost * (1-l_cost_factor);
6672 
6673                 ELSE
6674                  /* B 4057323 - The following change is to apportion the cost into multiple lots depending on LOT QUANTITIES
6675                      if the product is yielded into Multiple Lots */
6676                      /* Bug 4227784 - Replaced l_total_item_qty by the line_actual_qty. This is fail if there are mulitple lines for same product */
6677 
6678                   IF(l_step_tab(i).materials(j).actual_line_qty = 0) THEN /* Bug 4227784. To avoid Divide by zero error */
6679                     new_cost_tab(l).component_cost := 0;
6680                   ELSE
6681 
6682               IF l_step_tab(i).materials(j).line_um <> l_step_tab(i).materials(j).trans_um THEN
6683 
6684                /* Rajesh Patangya B9131983  Starts */
6685               l_actual_line_qty :=
6686                INV_CONVERT.INV_UM_CONVERT(ITEM_ID       => l_step_tab(i).materials(j).item_id
6687                                          ,LOT_NUMBER    => l_step_tab(i).materials(j).lot_number
6688                                          ,PRECISION     => 5
6689                                          ,ORGANIZATION_ID => transaction_row.orgn_id
6690                                          ,FROM_QUANTITY => l_step_tab(i).materials(j).actual_line_qty
6691                                          ,FROM_UNIT     => l_step_tab(i).materials(j).line_um
6692                                          ,TO_UNIT       => l_step_tab(i).materials(j).trans_um
6693                                          ,FROM_NAME     => NULL
6694                                          ,TO_NAME       => NULL
6695                                            );
6696                 -- PK Bug 9069363 do not return for all negative quatities.
6697                 IF l_actual_line_qty = -99999 THEN
6698                   fnd_file.put_line
6699                   (fnd_file.log,'ERROR: Unable to convert to Actual Line qty uom for transaction ID '
6700                   ||l_step_tab(i).materials(j).trans_id
6701                   );
6702                   l_return_status := 'E';
6703                   l_tmp := FALSE;
6704                   -- Bug 13038249 Mark the batch as uncostable
6705                   l_uncostable_batches_tab(transaction_row.doc_id) := transaction_row.doc_id;
6706                   RETURN;
6707                 END IF;
6708 
6709               ELSE
6710               l_actual_line_qty :=  l_step_tab(i).materials(j).actual_line_qty;
6711 
6712               END IF;
6713 
6714 				       IF l_actual_line_qty = 0 THEN  /* divide by Zero */
6715 				          l_actual_line_qty := 1 ;
6716 				       END IF;
6717         -- Bug 9887438 - LOT COSTING WITH INCORRECT RESULTS WHERE COST ALLOCATION FACTOR USED
6718         -- no need to store the converted values, as this is in a loop and hence incorrect cost
6719         -- calculations
6720        --         l_step_tab(i).materials(j).actual_line_qty := l_actual_line_qty ;
6721         --        l_step_tab(i).materials(j).line_um := l_step_tab(i).step_qty_uom ;
6722        -- End  9887438
6723                /* Rajesh Patangya B9131983  Ends  */
6724                 IF l_debug_level >= l_debug_level_medium THEN -- Bug#13000758
6725 				       fnd_file.put_line
6726                   (fnd_file.log, 'Actual Line Qty = ' || l_actual_line_qty  || '-line_um-'
6727                   || l_step_tab(i).materials(j).line_um  || '-Trans_um-'
6728                   || l_step_tab(i).materials(j).trans_um
6729                   ||'-CompoNENT-'|| l_step_tab(i).step_costs(l).component_cost ||'-factor-'||
6730                   l_cost_factor ||'-trans_qty-'||
6731                   l_step_tab(i).materials(j).trans_qty ||'-actual-'||
6732                    l_step_tab(i).materials(j).actual_line_qty
6733                    );
6734 		  END IF;
6735      --               new_cost_tab(l).component_cost := l_step_tab(i).step_costs(l).component_cost * l_cost_factor * l_step_tab(i).materials(j).trans_qty / l_step_tab(i).materials(j).actual_line_qty; --l_total_item_qty;
6736                     new_cost_tab(l).component_cost := l_step_tab(i).step_costs(l).component_cost * l_cost_factor * l_step_tab(i).materials(j).trans_qty / l_actual_line_qty ; -- /* 11842889  */
6737                   END IF;
6738 
6739                   new_cost.unit_cost := new_cost.unit_cost + new_cost_tab(l).component_cost;
6740                 END IF;
6741 
6742                 /***** Bug 4057323 - Use Cost Allocation Factor Depending on Profile value - End *****/
6743 
6744               END LOOP;
6745               -- End 3548217
6746 
6747               IF l_debug_level >= l_debug_level_low THEN
6748 
6749                 fnd_file.put_line(fnd_file.log,'Step costs after are');
6750 
6751                 FOR jj IN 1..l_step_tab(i).step_costs.COUNT
6752                 LOOP
6753                   fnd_file.put_line
6754                   (fnd_file.log,l_step_tab(i).step_costs(jj).component_cost);
6755                 END LOOP;
6756 
6757                 fnd_file.put_line(fnd_file.log,'New costs after are');
6758 
6759                 FOR jj IN 1..new_cost_tab.COUNT
6760                 LOOP
6761                   fnd_file.put_line
6762                   (fnd_file.log,new_cost_tab(jj).component_cost);
6763                 END LOOP;
6764 
6765               END IF;
6766 
6767               -- See if this transaction is the transaction that has yielded the lot being costed
6768               -- Reworked the following loops for bug 3548217
6769               -- Rajesh Patangya B9131983
6770               IF transaction_row.transaction_id = l_step_tab(i).materials(j).trans_id
6771               AND l_step_tab(i).materials(j).lot_number = transaction_row.lot_number THEN
6772                 -- It was. -- See if any burdens should apply to this yield
6773 
6774                 process_burdens;
6775 
6776                 IF l_return_status <> 'S' THEN
6777                   fnd_file.put_line
6778                   (fnd_file.log,'ERROR: Processing Burdens ');
6779                   -- Bug 13038249 Mark the batch as uncostable
6780                   l_uncostable_batches_tab(transaction_row.doc_id) := transaction_row.doc_id;
6781                   RETURN;
6782                 END IF;
6783 
6784                 -- PK Genesis Bug 13729144. Begin We want to update all costs in prd costs to level 1 Only Burdens will be level 0.
6785 
6786                 prd_cost := new_cost;
6787                 prd_cost_tab := new_cost_tab;
6788 
6789                 FOR i IN 1..prd_cost_tab.COUNT
6790                 LOOP
6791                   prd_cost_tab(i).cost_level :=  1;
6792                 END LOOP;
6793 
6794                 IF l_burdens_total <> 0 THEN
6795                   -- Burdens are held per unit, we need to gross them up to the product output quantity
6796 
6797                   FOR l IN 1..l_burden_costs_tab.count
6798                   LOOP
6799                     l_burden_costs_tab(l).component_cost := l_burden_costs_tab(l).component_cost * transaction_row.trans_qty;
6800                     IF l_debug_level >= l_debug_level_medium THEN
6801                       fnd_file.put_line
6802                       (fnd_file.log,'l_burden_costs_tab['||l||'] is '||l_burden_costs_tab(l).component_cost);
6803                     END IF;
6804                   END LOOP;
6805 
6806                   merge_costs ( l_burden_costs_tab
6807                               , 1
6808                               , 1
6809                               , 'C'
6810                               );
6811 
6812                   IF l_burden_costs_tab.EXISTS(1) THEN
6813 
6814                     IF l_debug_level >= l_debug_level_high THEN
6815 
6816                       fnd_file.put_line(fnd_file.log,'Before merge prd_cost_tab is:');
6817                       FOR k IN 1 .. prd_cost_tab.COUNT
6818                       LOOP
6819                         fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||prd_cost_tab(k).cost_cmpntcls_id);
6820                         fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||prd_cost_tab(k).cost_analysis_code);
6821                         fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||prd_cost_tab(k).cost_level);
6822                         fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||prd_cost_tab(k).component_cost);
6823                         fnd_file.put_line(fnd_file.log,'====================================');
6824                       END LOOP;
6825 
6826                       fnd_file.put_line(fnd_file.log,'Before merge l_burden_costs_tab is:');
6827                       FOR k IN 1 .. l_burden_costs_tab.COUNT
6828                       LOOP
6829                         fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||l_burden_costs_tab(k).cost_cmpntcls_id);
6830                         fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||l_burden_costs_tab(k).cost_analysis_code);
6831                         fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||l_burden_costs_tab(k).cost_level);
6832                         fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||l_burden_costs_tab(k).component_cost);
6833                         fnd_file.put_line(fnd_file.log,'====================================');
6834                       END LOOP;
6835 
6836                     END IF; -- End printing cost tabs for debug mode
6837 
6838                     FOR k IN 1 .. l_burden_costs_tab.COUNT
6839                     LOOP
6840                     -- Do we even need next loop. level in prd_cost_tab is updated to 1. Only level 0 costs are burdens.
6841                     -- So match is not going to be found. But can two resources have overhead under same component?
6842                     -- If so it is better to have this check. does not hurt to check anyways.
6843 
6844                     FOR l in 1..prd_cost_tab.COUNT
6845                     LOOP
6846                       l_prd_accrued := FALSE;
6847                       IF  prd_cost_tab(l).cost_cmpntcls_id = l_burden_costs_tab(k).cost_cmpntcls_id
6848                         AND prd_cost_tab(l).cost_analysis_code = l_burden_costs_tab(k).cost_analysis_code
6849                         AND prd_cost_tab(l).cost_level = l_burden_costs_tab(k).cost_level
6850                       THEN
6851                         prd_cost_tab(l).component_cost :=  prd_cost_tab(l).component_cost + l_burden_costs_tab(k).component_cost;
6852                         l_prd_accrued := TRUE;
6853                         EXIT;
6854                       END IF;
6855                     END LOOP;
6856 
6857                     IF NOT l_prd_accrued THEN
6858                       l := prd_cost_tab.count+1;
6859                       prd_cost_tab(l) := SYSTEM.gmf_cost_type
6860                                          ( l_burden_costs_tab(k).cost_cmpntcls_id
6861                                          , l_burden_costs_tab(k).cost_analysis_code
6862                                          , 0
6863                                          , l_burden_costs_tab(k).component_cost
6864                                          , 0
6865                                          );
6866 
6867                     END IF;
6868 
6869                   END LOOP;
6870                 END IF;  -- l_burden_costs_tab.EXISTS(1) THEN
6871 
6872                 IF l_debug_level >= l_debug_level_high THEN
6873 
6874                   fnd_file.put_line(fnd_file.log,'After Burden merge prd_cost_tab is:');
6875 
6876                   FOR k IN 1 .. prd_cost_tab.COUNT
6877                   LOOP
6878                     fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||prd_cost_tab(k).cost_cmpntcls_id);
6879                     fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||prd_cost_tab(k).cost_analysis_code);
6880                     fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||prd_cost_tab(k).cost_level);
6881                     fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||prd_cost_tab(k).component_cost);
6882                     fnd_file.put_line(fnd_file.log,'====================================');
6883                   END LOOP;
6884                 END IF;
6885 
6886               END IF;  -- l_burdens_total <> 0   now moved down as in process_movement
6887                 -- PK Genesis Bug 13729144. End
6888 
6889                 -- If we retrieved any, incorporate ('c' = Combine) their costs into the cost accumulated so far
6890 
6891 
6892 
6893                 old_cost.header_id := NULL;
6894 
6895                 -- Bug 13038249 Query recent costs first
6896                 OPEN lot_cost_cursor_1
6897                    ( l_step_tab(i).materials(j).organization_id
6898                    , l_step_tab(i).materials(j).item_id
6899                    , l_step_tab(i).materials(j).lot_number
6900                    , l_step_tab(i).materials(j).trans_date      -- Bug 4130869 Added Date field
6901                    ,l_cost_type_id
6902                    );
6903                 FETCH lot_cost_cursor_1 INTO old_cost;
6904                 CLOSE lot_cost_cursor_1;
6905 
6906                 IF old_cost.header_id IS NULL THEN
6907                   OPEN lot_cost_cursor
6908                      ( l_step_tab(i).materials(j).organization_id
6909                      , l_step_tab(i).materials(j).item_id
6910                      , l_step_tab(i).materials(j).lot_number
6911                      , l_step_tab(i).materials(j).trans_date      -- Bug 4130869 Added Date field
6912                      ,l_cost_type_id
6913                      );
6914                   FETCH lot_cost_cursor INTO old_cost;
6915                   CLOSE lot_cost_cursor;
6916                 END IF;
6917 
6918                 IF old_cost.header_id IS NOT NULL THEN
6919                   -- A cost already exists, retrieve it together with the details,
6920 
6921                   OPEN lot_cost_detail_cursor
6922                        ( old_cost.header_id );
6923                   FETCH lot_cost_detail_cursor BULK COLLECT INTO old_cost_tab;
6924                   CLOSE lot_cost_detail_cursor;
6925 
6926                   FOR k IN 1 .. old_cost_tab.COUNT
6927                   LOOP
6928                     old_cost_tab(k).component_cost := old_cost_tab(k).component_cost * old_cost.onhand_qty;
6929                   END LOOP;
6930 
6931                   -- Before merging, we need to preserve the 'new' cost so that sub-ledger has access to the
6932                   -- details. Bug 3578680
6933 
6934 -- B 13729144                  prd_cost_tab := new_cost_tab;
6935 -- B 13729144                  prd_cost := new_cost;
6936 
6937                   -- Now merge the old and new costs
6938                   /*  PK B6853640 Do not assign zero comment out next line
6939                   new_cost.unit_cost := 0; */
6940 
6941                   merge_costs ( old_cost_tab
6942                               , 1
6943                               , 1
6944                               , 'C'
6945                               );
6946 
6947                   l_new_cost := new_cost.unit_cost;
6948                   IF l_debug_level >= l_debug_level_low THEN
6949                      fnd_file.put_line   (fnd_file.log, 'Before create_cost_header in process_batch ');
6950                      fnd_file.put_line   (fnd_file.log, 'new unit cost: '||new_cost.unit_cost);
6951                      fnd_file.put_line   (fnd_file.log, 'l_new unit cost: '||l_new_cost);
6952                      fnd_file.put_line   (fnd_file.log, 'trans qty: '||transaction_row.trans_qty);
6953                      fnd_file.put_line   (fnd_file.log, 'onhand qty: '||old_cost.onhand_qty);
6954                      fnd_file.put_line   (fnd_file.log, 'new_cost_tab count: '||new_cost_tab.count);
6955                      fnd_file.put_line   (fnd_file.log, 'prd_cost_tab count: '||new_cost_tab.count);
6956                   END IF;
6957 
6958                   -- At this stage we have all the header and cost component information we need
6959                   -- to store the costs in the database.
6960 
6961                    --bug 8880554 added below if condition to avoid divisor is equal to zero error
6962                    -- Rajesh Patangya B9131983
6963                   IF NVL((transaction_row.trans_qty+old_cost.onhand_qty),0) = 0 THEN
6964                     l_temp_qty := 1;
6965                   ELSE
6966                     l_temp_qty :=(transaction_row.trans_qty+old_cost.onhand_qty);
6967                   END IF;
6968 
6969                   IF NVL(transaction_row.trans_qty,0) = 0 THEN
6970                      l_temp_trans_qty := 1;
6971                   ELSE
6972                    l_temp_trans_qty:= transaction_row.trans_qty;
6973                   END IF;
6974                     -- end for bug  8880554
6975 
6976                   -- PK Bug 9069363
6977 
6978                   IF l_temp_qty IS NULL THEN
6979                      l_temp_qty := 1;
6980                   END IF;
6981 
6982                   IF l_temp_trans_qty IS NULL THEN
6983                      l_temp_trans_qty := 1;
6984                   END IF;
6985 
6986                   -- PK Bug 9069363
6987 
6988 
6989         /* B9131983
6990          IF  l_step_tab(i).materials(j).lot_number = transaction_row.lot_number THEN
6991              if 2 product lots Lot Number should be same */
6992 
6993                IF l_debug_level >= l_debug_level_low THEN
6994                   fnd_file.put_line
6995                   (fnd_file.log,'Before Create Header Call 1');
6996                END IF;
6997 
6998                    create_cost_header
6999                   ( l_step_tab(i).materials(j).item_id
7000                   , l_step_tab(i).materials(j).lot_number
7001                   , l_step_tab(i).materials(j).organization_id
7002                   , l_cost_type_id
7003                   , l_new_cost/l_temp_qty      -- B9131983
7004                   , l_step_tab(i).materials(j).trans_date
7005                   , old_cost.onhand_qty + l_step_tab(i).materials(j).trans_qty
7006                   , transaction_row.doc_id
7007                   , transaction_row.transaction_source_type_id
7008                   , transaction_row.transaction_action_id
7009                   , new_cost.header_id
7010                   , dummy
7011                   , new_cost.onhand_qty
7012                   , l_return_status
7013                   );
7014 
7015 
7016                   IF l_return_status ='S' THEN   /* Success of header */
7017 
7018                     l_dtl_inserted := 0; --  Bug 13038249
7019 
7020                     FOR k IN 1.. new_cost_tab.count
7021                     LOOP    /* +ve Details */
7022 
7023                      /* B9131983  If cost is ZERO or component is not available
7024                          enter the default row in cost details */
7025                       IF new_cost_tab(k).cost_cmpntcls_id = 0 THEN
7026                          new_cost_tab(k).cost_cmpntcls_id := x_mtl_cmpntcls_id;
7027                       END IF;
7028 
7029                        IF NVL(rtrim(ltrim(new_cost_tab(k).cost_analysis_code)),'X') = 'X' THEN
7030                          new_cost_tab(k).cost_analysis_code := x_mtl_analysis_code;
7031                        END IF;
7032 
7033                 IF l_debug_level >= l_debug_level_low THEN
7034                   fnd_file.put_line(fnd_file.log,'After Replacing component/analysis/level to fiscal policy');
7035                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
7036                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
7037                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||new_cost_tab(k).cost_level);
7038                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
7039                   fnd_file.put_line(fnd_file.log,'====================================');
7040                 END IF;
7041 
7042                       IF (new_cost_tab(k).component_cost <> 0 OR (l_dtl_inserted = 0 AND k = new_cost_tab.count )) THEN   --  Bug 13038249 Modified condition and restored IF
7043 
7044                         create_cost_detail
7045                         ( new_cost.header_id
7046                         , new_cost_tab(k).cost_cmpntcls_id
7047                         , new_cost_tab(k).cost_analysis_code
7048                         , new_cost_tab(k).cost_level
7049                         , new_cost_tab(k).component_cost/l_temp_qty    -- B9131983
7050                         , 0
7051                         , l_return_status
7052                         );
7053 
7054                          procedure_name := 'Process Batch';
7055 
7056                         IF l_return_status <> 'S' THEN
7057                           RETURN;
7058                         END IF;
7059                       END IF;      -- B9131983 Bug 13038249
7060                       l_dtl_inserted := 1; --  Bug 13038249
7061                     END LOOP;   /* +ve Details */
7062                     l_dtl_inserted := 0; --  Bug 13038249
7063                     FOR k IN 1 .. prd_cost_tab.COUNT
7064                     LOOP       /* -ve Details */
7065 
7066                       -- Write the 'new' costs with a -ve header ID Bug 3578680
7067 
7068                       /* B9131983 If cost is ZERO or component is not available
7069                          enter the default row in cost details */
7070                       IF prd_cost_tab(k).cost_cmpntcls_id = 0 THEN
7071                          prd_cost_tab(k).cost_cmpntcls_id := x_mtl_cmpntcls_id;
7072                       END IF;
7073 
7074                        IF NVL(rtrim(ltrim(prd_cost_tab(k).cost_analysis_code)),'X') = 'X' THEN
7075                          prd_cost_tab(k).cost_analysis_code := x_mtl_analysis_code;
7076                       END IF;
7077 
7078                 IF l_debug_level >= l_debug_level_low THEN
7079                   fnd_file.put_line(fnd_file.log,'After Replacing component/analysis/level to fiscal policy');
7080                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||prd_cost_tab(k).cost_cmpntcls_id);
7081                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||prd_cost_tab(k).cost_analysis_code);
7082                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||prd_cost_tab(k).cost_level);
7083                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||prd_cost_tab(k).component_cost);
7084                   fnd_file.put_line(fnd_file.log,'====================================');
7085                 END IF;
7086 
7087                       IF ( prd_cost_tab(k).component_cost <> 0 OR (l_dtl_inserted = 0 AND k = prd_cost_tab.COUNT )) THEN  -- B9131983 Bug 13038249 Modified condition and restored IF
7088 
7089                         create_cost_detail
7090                         ( -new_cost.header_id
7091                         , prd_cost_tab(k).cost_cmpntcls_id
7092                         , prd_cost_tab(k).cost_analysis_code
7093                         , prd_cost_tab(k).cost_level
7094                         , prd_cost_tab(k).component_cost/l_temp_trans_qty      -- B9131983
7095                         , 0
7096                         , l_return_status
7097                         );
7098 
7099                         IF l_return_status <> 'S' THEN
7100 
7101                           RETURN;
7102                         END IF;
7103                         l_dtl_inserted := 1; --  Bug 13038249
7104                       END IF;   -- B9131983 Bug 13038249
7105                     END LOOP;    /* -ve Details */
7106 
7107                     create_material_transaction
7108                     ( new_cost.header_id
7109                     , l_cost_type_id
7110                     , l_step_tab(i).materials(j).trans_date
7111                     , l_step_tab(i).materials(j).trans_qty
7112                     , l_step_tab(i).materials(j).trans_um
7113                     , prd_cost.unit_cost
7114                     , l_step_tab(i).materials(j).trans_id
7115                     , new_cost.unit_cost/l_temp_qty     -- B9131983
7116                     , new_cost.onhand_qty
7117                     , old_cost.unit_cost
7118                     , old_cost.onhand_qty
7119                     , 1
7120                     ,transaction_row.lot_number
7121                     , l_return_status
7122                     );
7123 
7124                     IF l_debug_level >= l_debug_level_low THEN
7125                        fnd_file.put_line (fnd_file.log, 'Completed inserts into tables in process_batch ');
7126                     END IF;
7127 
7128                     IF l_return_status <> 'S' THEN
7129 
7130                       RETURN;
7131                     END IF;
7132 
7133                   ELSE    /* Success of header */
7134                     RETURN;
7135                   END IF;  /* Success of header */
7136 
7137           /*    END IF;     if 2 product lots Lot Number should be same */
7138 
7139 
7140                 ELSE    /*   IF old_cost.header_id IS NOT NULL THEN */
7141 
7142                 IF   l_debug_level >= l_debug_level_high THEN
7143                   fnd_file.put_line(fnd_file.log,'No cost currently exists, create one and all associated details and transactions');
7144                 END IF;
7145 
7146                   -- No cost currently exists, create one and all associated details
7147                   -- and transactions. Again, this is only for the invoking transaction
7148 
7149                   IF l_debug_level >= l_debug_level_high  THEN
7150                     fnd_file.put_line
7151                     ( fnd_file.log,'New cost tab has '||to_char(new_cost_tab.count)||' entries:');
7152 
7153                     FOR k IN 1 .. new_cost_tab.COUNT
7154                     LOOP
7155                       fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
7156                       fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
7157                       fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||new_cost_tab(k).cost_level);
7158                       fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
7159                       fnd_file.put_line(fnd_file.log,'====================================');
7160                     END LOOP;
7161                   END IF;
7162 
7163                   /*  B9131983 Rajesh Patangya Starts */
7164                     --bug 8880554 added below if condition to avoid divisor is equal to zero error
7165                   IF NVL((transaction_row.trans_qty+old_cost.onhand_qty),0) = 0 THEN
7166                     l_temp_qty := 1;
7167                   ELSE
7168                     l_temp_qty :=(transaction_row.trans_qty+old_cost.onhand_qty);
7169                   END IF;
7170                   -- NOTE: Dowe need to use l_temp_qty, confirm with prasad and venkat
7171                   -- Prd_tab should be used to isnert the negative cost details records for subledger
7172                   -- posting purposes only, what does it means is that we need to enter the batch cost to book the entries
7173 
7174                   IF NVL(transaction_row.trans_qty,0) = 0 THEN
7175                      l_temp_trans_qty := 1;
7176                   ELSE
7177                    l_temp_trans_qty:= transaction_row.trans_qty;
7178                   END IF;
7179                     -- end for bug  8880554
7180 
7181                   -- PK Bug 9069363
7182 
7183                   IF l_temp_qty IS NULL THEN
7184                      l_temp_qty := 1;
7185                   END IF;
7186 
7187                   IF l_temp_trans_qty IS NULL THEN
7188                      l_temp_trans_qty := 1;
7189                   END IF;
7190 
7191                   -- PK Bug 9069363
7192                   /*  B9131983 Rajesh Patangya Ends */
7193 
7194                   IF l_debug_level >= l_debug_level_high  THEN
7195                     fnd_file.put_line(fnd_file.log,' New total cost is : '||new_cost.unit_cost);
7196                     fnd_file.put_line(fnd_file.log,' New unit cost is  : '||new_cost.unit_cost/l_temp_trans_qty);
7197 
7198                   END IF;
7199 
7200           /* B9131983
7201          IF  l_step_tab(i).materials(j).lot_number = transaction_row.lot_number THEN
7202               if 2 product lots Lot Number should be same */
7203 
7204                IF l_debug_level >= l_debug_level_low THEN
7205                   fnd_file.put_line
7206                   (fnd_file.log,'Before Create Header Call 2');
7207                END IF;
7208 
7209                   create_cost_header
7210                   ( l_step_tab(i).materials(j).item_id
7211                   , l_step_tab(i).materials(j).lot_number   /* INCONV SSCHINCH */
7212                   , l_step_tab(i).materials(j).organization_id
7213                   , l_cost_type_id
7214                   , new_cost.unit_cost/l_temp_trans_qty        -- B9131983
7215                   , l_step_tab(i).materials(j).trans_date
7216                   , l_step_tab(i).materials(j).trans_qty
7217                   , transaction_row.doc_id
7218                   , transaction_row.transaction_source_type_id
7219                   , transaction_row.transaction_action_id
7220                   , new_cost.header_id
7221                   , dummy
7222                   , new_cost.onhand_qty
7223                   , l_return_status
7224                   );
7225 
7226                   IF l_return_status = 'S' THEN    /* l_return_status = 'S' */
7227 
7228                     l_dtl_inserted := 0; --  Bug 13038249
7229 
7230                     FOR k IN 1..new_cost_tab.count
7231                     LOOP       /* +ve Details */
7232 
7233                      IF ( new_cost_tab(k).component_cost <> 0 OR (l_dtl_inserted = 0 AND k = new_cost_tab.count ) ) THEN   -- Bug 13038249 Modified condition and restored IF
7234                        /* B9131983 If cost is ZERO or component is not available
7235                          enter the default row in cost details */
7236                       IF new_cost_tab(k).cost_cmpntcls_id = 0 THEN
7237                          new_cost_tab(k).cost_cmpntcls_id := x_mtl_cmpntcls_id;
7238                       END IF;
7239 
7240                        IF NVL(rtrim(ltrim(new_cost_tab(k).cost_analysis_code)),'X') = 'X' THEN
7241                          new_cost_tab(k).cost_analysis_code := x_mtl_analysis_code;
7242                        END IF;
7243 
7244                 IF l_debug_level >= l_debug_level_low THEN
7245                   fnd_file.put_line(fnd_file.log,'After Replacing component/analysis/level to fiscal policy');
7246                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
7247                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
7248                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||new_cost_tab(k).cost_level);
7249                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
7250                   fnd_file.put_line(fnd_file.log,'====================================');
7251                 END IF;
7252 
7253                         create_cost_detail
7254                         ( new_cost.header_id
7255                         , new_cost_tab(k).cost_cmpntcls_id
7256                         , new_cost_tab(k).cost_analysis_code
7257                         , 0
7258                         , new_cost_tab(k).component_cost/l_temp_qty    -- B9131983
7259                         , 0
7260                         , l_return_status
7261                         );
7262                       -- Bug 9069363 create zero cost detail as well.
7263                       -- END IF;
7264 
7265                       IF l_return_status <> 'S' THEN
7266                         RETURN;
7267                       END IF;
7268 
7269              -- Becuase there is no previous Header record present
7270              -- and hence Perpetual and transaction csot are same
7271              -- Juse need to negate the header_id and all other inforamtion will be same
7272              -- Genesis Bug 13729144 Now these negative details should still be created looping though prd cost tab as cost level is now different
7273              -- Moved to a separate Loop
7274 /*                        create_cost_detail
7275                         ( -1*new_cost.header_id
7276                         , new_cost_tab(k).cost_cmpntcls_id
7277                         , new_cost_tab(k).cost_analysis_code
7278                         , 0
7279                         , new_cost_tab(k).component_cost/l_temp_qty   -- B9131983
7280                         , 0
7281                         , l_return_status
7282                         );  */
7283                       -- Bug 9069363 create zero cost detail as well.
7284                         l_dtl_inserted := 1; --  Bug 13038249
7285                       END IF;  -- Bug 13038249
7286 
7287 /*                      IF l_return_status <> 'S' THEN
7288                         RETURN;
7289 
7290 
7291                       END IF;  Bug 13729144 Not needed since call is commented  */
7292 
7293 
7294                     END LOOP;    /* +ve Details */
7295 
7296                     -- Bug 13729144 negative details separately since cost level could be 1
7297 
7298                     l_dtl_inserted := 0;
7299 
7300                     FOR k IN 1 .. prd_cost_tab.COUNT
7301                     LOOP       /* -ve Details */
7302 
7303                       -- Write the 'new' costs with a -ve header ID Bug 3578680
7304 
7305                       /* B9131983 If cost is ZERO or component is not available
7306                          enter the default row in cost details */
7307                       IF prd_cost_tab(k).cost_cmpntcls_id = 0 THEN
7308                          prd_cost_tab(k).cost_cmpntcls_id := x_mtl_cmpntcls_id;
7309                       END IF;
7310 
7311                        IF NVL(rtrim(ltrim(prd_cost_tab(k).cost_analysis_code)),'X') = 'X' THEN
7312                          prd_cost_tab(k).cost_analysis_code := x_mtl_analysis_code;
7313                       END IF;
7314 
7315                 IF l_debug_level >= l_debug_level_low THEN
7316                   fnd_file.put_line(fnd_file.log,'After Replacing component/analysis/level to fiscal policy');
7317                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||prd_cost_tab(k).cost_cmpntcls_id);
7318                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||prd_cost_tab(k).cost_analysis_code);
7319                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||prd_cost_tab(k).cost_level);
7320                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||prd_cost_tab(k).component_cost);
7321                   fnd_file.put_line(fnd_file.log,'====================================');
7322                 END IF;
7323 
7324                       IF ( prd_cost_tab(k).component_cost <> 0 OR (l_dtl_inserted = 0 AND k = prd_cost_tab.COUNT )) THEN  -- B9131983 Bug 13038249 Modified condition and restored IF
7325 
7326                         create_cost_detail
7327                         ( -new_cost.header_id
7328                         , prd_cost_tab(k).cost_cmpntcls_id
7329                         , prd_cost_tab(k).cost_analysis_code
7330                         , prd_cost_tab(k).cost_level
7331                         , prd_cost_tab(k).component_cost/l_temp_trans_qty      -- B9131983
7332                         , 0
7333                         , l_return_status
7334                         );
7335 
7336                         IF l_return_status <> 'S' THEN
7337 
7338                           RETURN;
7339                         END IF;
7340                         l_dtl_inserted := 1; --  Bug 13038249
7341                       END IF;   -- B9131983 Bug 13038249
7342                     END LOOP;    /* -ve Details */
7343 
7344 
7345 
7346                   END IF;    /* l_return_status = 'S' */
7347 
7348                   create_material_transaction
7349                   ( new_cost.header_id
7350                   , l_cost_type_id
7351                   , l_step_tab(i).materials(j).trans_date
7352                   , l_step_tab(i).materials(j).trans_qty
7353                   , l_step_tab(i).materials(j).trans_um
7354                   , new_cost.unit_cost
7355                   , l_step_tab(i).materials(j).trans_id
7356                   , new_cost.unit_cost/l_temp_trans_qty     -- B9131983
7357                   , l_step_tab(i).materials(j).trans_qty
7358                   , NULL
7359                   , NULL
7360                   , 1 -- B 13729144 always new_cost_ind
7361                   ,transaction_row.lot_number
7362                   , l_return_status
7363                   );
7364 
7365                   IF l_return_status <> 'S' THEN
7366 
7367                     RETURN;
7368                   END IF;
7369 
7370               /*  END IF;   if 2 product lots Lot Number should be same */
7371 
7372                 END IF;  /*  IF old_cost.header_id IS NOT NULL THEN*/
7373 
7374               END IF;
7375 
7376             END IF;
7377 
7378           END LOOP;   /*  -- Main Processing */
7379 
7380           IF l_debug_level >= l_debug_level_medium THEN
7381             fnd_file.put_line
7382             (fnd_file.log,'After yield check');
7383           END IF;
7384 
7385           -- If we are not on a terminal step, we have to roll the costs into
7386           -- the next step(s) in the dependency chain. Also, if the route divides
7387           -- at this point we need to aportion the costs held in this step between
7388           -- all subsequent steps.
7389           IF l_step_tab(i).dependencies(1).step_index IS NOT NULL THEN
7390 
7391             IF l_debug_level >= l_debug_level_medium  THEN
7392               fnd_file.put_line
7393               (fnd_file.log,'Before quantity accumulation');
7394             END IF;
7395 
7396             l_total_qty := 0;
7397 
7398             FOR j in 1..l_step_tab(i).dependencies.count
7399             LOOP
7400                -- B9131983 need to check why step_qty is NULL on some steps. using NVL at the moment
7401               l_total_qty := l_total_qty + NVL(l_step_tab(i).dependencies(j).step_qty,0);
7402             END LOOP;
7403 
7404             IF l_debug_level >= l_debug_level_high THEN
7405 
7406               fnd_file.put_line
7407               (fnd_file.log,'After quantity accumulation, total_qty is'
7408               ||to_char(l_total_qty,'999999999.99')
7409               );
7410             END IF;
7411 
7412 
7413             FOR j IN 1..l_step_tab(i).dependencies.count
7414             LOOP
7415 
7416                /* Bug 9616762  Front port of 4601607 Start
7417                * If all the dependent steps have 0 step_qty. So roll the costs by
7418                * apportioning equally into the number of dependent steps so that
7419                * all cost gets carried over
7420                */
7421                IF l_total_qty = 0 THEN
7422                   l_cost_factor := 1/l_step_tab(i).dependencies.count;
7423                ELSE
7424                   l_cost_factor := NVL(l_step_tab(i).dependencies(j).step_qty, 0) / l_total_qty;
7425                END IF;
7426               /* Bug 9616762  Front port of 4601607 End */
7427 
7428                -- B9131983 need to check why step_qty is NULL on some steps. using NVL at the moment
7429               /* 9616762  Front port of 4601607 old code below replaced as above */
7430            /* IF l_total_qty <> 0 THEN
7431                 l_cost_factor := NVL(l_step_tab(i).dependencies(j).step_qty, 0) / l_total_qty;
7432               ELSE
7433                 l_cost_factor := 0;
7434                 IF l_debug_level >= l_debug_level_medium THEN
7435                    fnd_file.put_line
7436                     (fnd_file.log,'Assigning cost factor as zero');
7437                 END IF;
7438               END IF;
7439            */
7440            -- Bug 9616762  Front port of 4601607 End commenting out old code
7441 
7442               IF l_debug_level >= l_debug_level_medium THEN
7443                 fnd_file.put_line
7444                 (fnd_file.log,'Cost factor is '||to_char(l_cost_factor,'9999.99'));
7445               END IF;
7446 
7447               -- Roll current costs into next step(s). We loop through all costs accumulated
7448               -- and apply the above factor to each cost when rolling them forward
7449 
7450               -- Find index of step to roll costs into
7451 
7452               l_step_index := l_step_tab(i).dependencies(j).step_index;
7453 
7454               FOR k IN 1..l_step_tab(i).step_costs.count
7455               LOOP
7456                  -- B9131983
7457                 IF (NVL(l_step_tab(i).step_costs(k).cost_analysis_code, ' ') <> ' ')
7458                 THEN
7459 
7460                   IF l_debug_level >= l_debug_level_medium THEN
7461                     fnd_file.put_line
7462                     (fnd_file.log,'Rolling costs of step '
7463                     ||to_char(i)||' into step '||to_char(l_step_index));
7464                    END IF;
7465 
7466                    -- Now roll current costs into the next step
7467 
7468                    l_cost_accrued := FALSE;
7469 
7470                    FOR l IN 1..l_step_tab(l_step_index).inherited_costs.count
7471                    LOOP
7472                      IF   l_step_tab(i).step_costs(k).cost_cmpntcls_id
7473                          =l_step_tab(l_step_index).inherited_costs(l).cost_cmpntcls_id
7474                      AND  l_step_tab(i).step_costs(k).cost_analysis_code
7475                          =l_step_tab(l_step_index).inherited_costs(l).cost_analysis_code
7476                      AND  l_step_tab(i).step_costs(k).cost_level
7477                          =l_step_tab(l_step_index).inherited_costs(l).cost_level
7478                      THEN
7479                        l_step_tab(l_step_index).inherited_costs(l).component_cost :=
7480                          l_step_tab(l_step_index).inherited_costs(l).component_cost
7481                         +l_step_tab(i).step_costs(k).component_cost * l_cost_factor;
7482 
7483                        l_cost_accrued := TRUE;
7484                        EXIT;
7485                      END IF;
7486                    END LOOP;
7487 
7488                    -- If we didn't find a match, create a new cost in the target steps
7489                    -- inherited costs
7490 
7491                    IF NOT l_cost_accrued THEN
7492                      -- B9131983 use NVL here as well ??
7493                      IF l_debug_level >= l_debug_level_medium THEN
7494                        fnd_file.put_line (fnd_file.log,'cost_analysis_code '||l_step_tab(l_step_index).inherited_costs(1).cost_analysis_code||'.');
7495                      END IF;
7496                      IF l_step_tab(l_step_index).inherited_costs(1).cost_analysis_code = ' ' THEN
7497                        l_step_tab(l_step_index).inherited_costs(1) :=
7498                          l_step_tab(i).step_costs(k);
7499                        l_step_tab(l_step_index).inherited_costs(1).component_cost :=
7500                          l_step_tab(i).step_costs(k).component_cost * l_cost_factor;
7501                      ELSE
7502                        l_step_tab(l_step_index).inherited_costs.EXTEND;
7503                        l := l_step_tab(l_step_index).inherited_costs.count;
7504                        l_step_tab(l_step_index).inherited_costs(l) := l_step_tab(i).step_costs(k);
7505                        l_step_tab(l_step_index).inherited_costs(l).component_cost :=
7506                        l_step_tab(i).step_costs(k).component_cost * l_cost_factor;
7507                      END IF;
7508                    END IF;
7509                  END IF;
7510                END LOOP;
7511              END LOOP;
7512            END IF;                     -- If we are not in terminal step
7513          END LOOP; -- End of outer rollup loop
7514        END IF;-- End of conditional skip
7515      END IF;-- End of if this is an output
7516 
7517  /*    IF l_debug_level >= l_debug_level_high THEN
7518        lot_cost_audit
7519 	     ( transaction_row.inventory_item_id
7520 	     , transaction_row.lot_number
7521 	     , transaction_row.orgn_id
7522 	     , transaction_row.doc_id
7523 	     , transaction_row.trans_date
7524 	     , l_step_tab
7525 	     );
7526      END IF;
7527   */
7528      -- Finally, mark the invoking transaction as costed
7529      -- Also, mark the batch as having participated in actual costing - PJS 11Feb04
7530 
7531     IF l_final_run_flag = 1 THEN -- umoogala 05-Dec-2003
7532        /*UPDATE mtl_transaction_lot_numbers
7533        SET    lot_cost_ind 	      = 1,
7534 	      --request_id              = l_request_id,
7535 	      --program_application_id  = l_prog_appl_id,
7536 	      --program_id              = l_program_id,
7537 	      last_update_date     = sysdate
7538        WHERE  transaction_id = transaction_row.transaction_id;*/
7539 
7540        UPDATE gme_batch_header
7541        SET    actual_cost_ind = 1
7542        WHERE  batch_id = transaction_row.doc_id;
7543 
7544      END IF;
7545     IF l_debug_level >= l_debug_level_medium THEN
7546        fnd_file.put_line
7547        (fnd_file.log,'Entered Procedure: '||procedure_name);
7548      END IF;
7549 
7550 END process_batch;
7551 
7552 
7553 
7554     --**********************************************************************************************
7555     --*                                                                                            *
7556     --* Procedure to handle created lots (CREI/CRER)                                               *
7557     --*                                                                                            *
7558     --**********************************************************************************************
7559 
7560 PROCEDURE process_creation
7561 IS
7562   l_header_id    NUMBER;
7563   l_unit_cost    NUMBER;
7564   l_onhand_qty   NUMBER;
7565   procedure_name VARCHAR2(100);
7566 BEGIN
7567 
7568   procedure_name := 'Process Creation';
7569   IF l_debug_level >= l_debug_level_medium THEN
7570      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
7571   END IF;
7572 
7573   -- We assume that no lot cost exists, as the lot is being created by this transaction.
7574   -- The only costs we can determine come from burdens.
7575 
7576   process_burdens;
7577 
7578   IF l_return_status = 'S' THEN
7579     create_cost_header
7580     ( p_item_id              => transaction_row.inventory_item_id
7581     , p_lot_number           => transaction_row.lot_number  /* INVCONV sschinch */
7582     , p_orgn_id              => transaction_row.orgn_id         /* INVCONV sschinch */
7583     , p_cost_type_id         => l_cost_type_id
7584     , p_unit_cost            => NVL(l_burdens_total,0)
7585     , p_cost_date            => transaction_row.trans_date
7586     , p_onhand_qty           => transaction_row.trans_qty
7587     , p_trx_src_type_id      => transaction_row.transaction_source_type_id  /* INVCONV sschinch */
7588     , p_txn_act_id           => transaction_row.transaction_action_id   /* INVCONV sschinch */
7589     , p_doc_id               => transaction_row.doc_id
7590     , x_header_id            => l_header_id
7591     , x_unit_cost            => l_unit_cost
7592     , x_onhand_qty           => l_onhand_qty
7593     , x_return_status        => l_return_status
7594     );
7595 
7596     -- Bug 3388974-2
7597     new_cost.header_id := l_header_id;
7598     new_cost.unit_cost := l_unit_cost;
7599     new_cost.onhand_qty:= l_onhand_qty;
7600 
7601     IF l_return_status = 'S' THEN
7602 
7603       IF l_burdens_total <> 0 THEN
7604         IF l_debug_level >= l_debug_level_medium THEN
7605           fnd_file.put_line
7606           (fnd_file.log,'Processing' || l_burden_costs_tab.count ||' burdens for CREI transaction ID '||transaction_row.transaction_id);
7607         END IF;
7608 
7609         FOR i IN 1..l_burden_costs_tab.COUNT
7610         LOOP
7611           create_cost_detail
7612           ( l_header_id
7613           , l_burden_costs_tab(i).cost_cmpntcls_id
7614           , l_burden_costs_tab(i).cost_analysis_code
7615           , 0
7616           , l_burden_costs_tab(i).component_cost
7617           , 1
7618           , l_return_status
7619           );
7620 
7621           IF l_return_status <> 'S' THEN
7622             EXIT;
7623           END IF;
7624         END LOOP;
7625 
7626       ELSE
7627 
7628         IF l_debug_level >= l_debug_level_medium THEN
7629           fnd_file.put_line
7630           (fnd_file.log,'No burdens found, retrieving default component class and analysis code for cost details');
7631         END IF;
7632 
7633 
7634         OPEN component_class_cursor
7635              (l_le_id, transaction_row.inventory_item_id, transaction_row.orgn_id,transaction_row.trans_date); /* INVCONV sschinch */
7636         FETCH component_class_cursor INTO component_class_id, cost_analysis_code, dummy;
7637         CLOSE component_class_cursor;
7638 
7639         create_cost_detail
7640         ( l_header_id
7641         , component_class_id
7642         , cost_analysis_code
7643         , 0
7644         , 0.00
7645         , 0
7646         , l_return_status
7647         );
7648 
7649       END IF; -- end of cost details creation
7650     ELSE
7651       RETURN; --BUG 3476508  Some kind of problem with burdens
7652     END IF; -- end of header creation
7653 
7654     IF l_return_status = 'S' THEN
7655 
7656       create_material_transaction
7657       ( l_header_id
7658       , l_cost_type_id
7659       , transaction_row.trans_date
7660       , transaction_row.trans_qty
7661       , transaction_row.trans_um
7662       , transaction_row.trans_qty * l_burdens_total
7663       , transaction_row.transaction_id
7664       , l_burdens_total
7665       , transaction_row.trans_qty
7666       , NULL
7667       , NULL
7668       , NULL
7669       ,transaction_row.lot_number
7670       , l_return_status
7671       );
7672 
7673       IF l_return_status <> 'S' THEN
7674         RETURN;
7675       END IF;
7676 
7677     ELSE
7678       RETURN;
7679     END IF;
7680   END IF;
7681   IF l_debug_level >= l_debug_level_medium THEN
7682        fnd_file.put_line
7683        (fnd_file.log,'Leaving Procedure: '||procedure_name);
7684      END IF;
7685 
7686 EXCEPTION
7687   WHEN OTHERS THEN
7688      fnd_file.put_line
7689      (fnd_file.log,'Failed in procedure process_creation with error');
7690      fnd_file.put_line(fnd_file.log,SQLERRM);
7691        l_return_status := 'U';
7692 END process_creation;
7693 
7694     --**********************************************************************************************
7695     --*                                                                                            *
7696     --* Procedure to handle sales orders (OMSO/OPSO)                                               *
7697     --*                                                                                            *
7698     --**********************************************************************************************
7699 
7700 PROCEDURE process_sales_order
7701 IS
7702   loop_count NUMBER;
7703   procedure_name VARCHAR2(100);
7704 BEGIN
7705 
7706   procedure_name := 'Process Sales Order';
7707   IF l_debug_level >= l_debug_level_medium THEN
7708      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
7709   END IF;
7710   -- This is a debit on the source organization so treat this as an adjustment. Returns are handled
7711   -- as PORC transactions.
7712 
7713   process_adjustment;
7714 
7715   IF l_debug_level >= l_debug_level_medium THEN
7716      fnd_file.put_line(fnd_file.log,'Leaving Procedure: '||procedure_name);
7717   END IF;
7718 
7719 END process_sales_order;
7720 
7721 
7722 
7723     --**********************************************************************************************
7724     --*                                                                                            *
7725     --* Procedure to handle cycle counts (PIPH/PICY)                                               *
7726     --*                                                                                            *
7727     --**********************************************************************************************
7728 
7729 
7730 PROCEDURE process_cycle_count
7731 IS
7732   loop_count NUMBER;
7733   procedure_name VARCHAR2(100);
7734 BEGIN
7735 
7736   procedure_name := 'Process cycle count';
7737   IF l_debug_level >= l_debug_level_medium THEN
7738      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
7739   END IF;
7740 
7741   -- For costing purposes this is equivalent to an adjustment
7742 
7743   process_adjustment;
7744   IF l_debug_level >= l_debug_level_medium THEN
7745      fnd_file.put_line(fnd_file.log,'Leaving Procedure: '||procedure_name);
7746   END IF;
7747 
7748 END process_cycle_count;
7749 
7750 -- AF
7751 
7752 /************************************************************************
7753 NAME
7754         process_lc_adjustments
7755 
7756 
7757 DESCRIPTION
7758         This function Process all Actual LC adjustments for the item and lot
7759         and calculate lot cost also insert a record in lot cost adjustments table
7760 
7761 AUTHOR
7762    Andrea  4-Aug-09, LCM-OPM Integration, bug 8642337
7763    Parag Kanetkar Bug 10159100 Prorate quantities
7764 
7765 HISTORY
7766 *************************************************************************/
7767 PROCEDURE process_lc_adjustments IS
7768 
7769   CURSOR c_adjustments_cursor IS
7770   SELECT
7771          glat.cost_cmpntcls_id,
7772          glat.cost_analysis_code,
7773          (nvl(glat.new_landed_cost,0) - nvl(glat.prior_landed_cost,0)) * (mtln.primary_quantity /rt.primary_quantity ) adjustment_amt,
7774          nvl(glat.new_landed_cost,0),
7775          glat.adj_transaction_id,
7776          glat.ship_header_id,
7777          glat.ship_line_id,
7778          glat.adjustment_num,
7779          glat.organization_id,
7780          glat.inventory_item_id,
7781          glat.rcv_transaction_id,
7782          glat.charge_line_type_code,
7783          glat.component_type,
7784          glat.component_name,
7785          glat.transaction_date,
7786          glat.primary_quantity,
7787          glat.primary_uom_code,
7788          glat.lc_adjustment_flag,
7789          mtln.lot_number,  -- Bug 13906192
7790          0 as applied_amt, -- Bug 10159100
7791          mtln.primary_quantity as received_qty -- Bug 13906192
7792    FROM
7793          gmf_lc_adj_transactions glat,
7794          rcv_transactions rt,
7795          mtl_material_transactions mmt, -- Bug 13906192
7796          mtl_transaction_lot_numbers mtln   -- Bug 13906192
7797   WHERE
7798          glat.adj_transaction_id = transaction_row.transaction_id
7799     AND  glat.rcv_transaction_id = transaction_row.doc_id
7800     AND  glat.ship_line_id       = transaction_row.line_id
7801     AND  (lc_adjustment_flag  = 1 OR glat.adjustment_num > 0 )
7802     AND  glat.rcv_transaction_id = rt.transaction_id
7803     AND  rt.transaction_id       = mmt.rcv_transaction_id     -- Bug 13906192
7804     AND  mmt.transaction_id      = mtln.transaction_id        -- Bug 13906192
7805     AND  glat.component_type IN ('ITEM PRICE','CHARGE')
7806     AND  mtln.lot_number         = transaction_row.lot_number;  -- Bug 13906192
7807 
7808  TYPE adjustments_cursor IS TABLE OF c_adjustments_cursor%ROWTYPE;
7809  l_adjustments_cursor adjustments_cursor;
7810 
7811   new_unit_cost   NUMBER;
7812   l_new_lc        NUMBER;
7813   procedure_name VARCHAR2(100);
7814 
7815   CURSOR cur_lca_count (cp_adj_transaction_id NUMBER,
7816                         cp_lot_number gmf_lc_lot_cost_adjs.lot_number%TYPE) IS
7817   SELECT 1  FROM gmf_lc_lot_cost_adjs
7818    WHERE adj_transaction_id = cp_adj_transaction_id
7819      AND lot_number         = cp_lot_number  ;
7820 
7821    l_count NUMBER := 0;
7822 
7823 BEGIN
7824     procedure_name := 'process_lc_adjustments';
7825     IF l_debug_level >= l_debug_level_medium THEN
7826         fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
7827         fnd_file.put_line(fnd_file.log,'INSIDE LC Actual adjustments for adjustment transaction ID/Rcv Transaction Id/Shipment line ID '
7828                                         ||transaction_row.transaction_id ||'/'||transaction_row.doc_id ||'/'|| transaction_row.line_id);
7829     fnd_file.put_line(fnd_file.log,'transaction_row.transaction_id: '||transaction_row.transaction_id);
7830     fnd_file.put_line(fnd_file.log,'Entered transaction_row.doc_id: '||transaction_row.doc_id);
7831     fnd_file.put_line(fnd_file.log,'Entered transaction_row.line_id: '||transaction_row.line_id);
7832     END IF;
7833 
7834     -- Load adjustment cost details
7835     OPEN c_adjustments_cursor;
7836     FETCH c_adjustments_cursor BULK COLLECT INTO l_adjustments_cursor;
7837     CLOSE c_adjustments_cursor;
7838 
7839     IF l_debug_level >= l_debug_level_medium THEN
7840         fnd_file.put_line(fnd_file.log,'Opened the c_adjustments_cursor');
7841     END IF;
7842 
7843     FOR i IN 1 .. l_adjustments_cursor.COUNT
7844     LOOP
7845 
7846         IF l_debug_level >= l_debug_level_medium THEN
7847             fnd_file.put_line(fnd_file.log,'Add the gmf_cost_type to new_cost_tab');
7848         END IF;
7849 
7850         -- PK Bug 10159100 Code to prorate quantities Begins
7851 
7852         l_adjustments_cursor(i).applied_amt := l_adjustments_cursor(i).adjustment_amt;
7853 
7854         IF l_adjustments_cursor(i).received_qty > old_cost.onhand_qty THEN
7855           IF old_cost.onhand_qty <= 0 THEN
7856             l_adjustments_cursor(i).applied_amt := 0;
7857           ELSE
7858             l_adjustments_cursor(i).applied_amt := l_adjustments_cursor(i).adjustment_amt * old_cost.onhand_qty / l_adjustments_cursor(i).received_qty;
7859              IF l_debug_level >= l_debug_level_medium THEN
7860                fnd_file.put_line(fnd_file.log,' Onhand Qty ' || old_cost.onhand_qty);
7861                fnd_file.put_line(fnd_file.log,' Received Qty ' || l_adjustments_cursor(i).received_qty);
7862                fnd_file.put_line(fnd_file.log,' Adjustment Amt ' || l_adjustments_cursor(i).adjustment_amt);
7863               fnd_file.put_line(fnd_file.log,' Applied Amt ' || l_adjustments_cursor(i).applied_amt);
7864              END IF;
7865           END IF;
7866 
7867         END IF;
7868 
7869         IF l_adjustments_cursor(i).applied_amt <> 0 THEN
7870         -- PK Bug 10159100 Code to prorate quantities ends
7871 
7872         new_cost_tab(i) := SYSTEM.gmf_cost_type(l_adjustments_cursor(i).cost_cmpntcls_id ,
7873                                                 l_adjustments_cursor(i).cost_analysis_code,
7874                                                 0,
7875                                                 l_adjustments_cursor(i).applied_amt,  -- PK Bug 10159100 l_adjustments_cursor(i).adjustment_amt,
7876                                                 0);
7877 
7878         IF l_debug_level >= l_debug_level_medium THEN
7879             fnd_file.put_line(fnd_file.log,'new_cost_tab('||i ||').cost_cmpntcls_id: ' || new_cost_tab(i).cost_cmpntcls_id);
7880             fnd_file.put_line(fnd_file.log,'new_cost_tab('||i ||').cost_analysis_code: ' || new_cost_tab(i).cost_analysis_code);
7881             fnd_file.put_line(fnd_file.log,'new_cost_tab('||i ||').cost_level: ' || new_cost_tab(i).cost_level);
7882             fnd_file.put_line(fnd_file.log,'new_cost_tab('||i ||').component_cost: ' || new_cost_tab(i).component_cost);
7883             fnd_file.put_line(fnd_file.log,'new_cost_tab('||i ||').burden_ind: ' || new_cost_tab(i).burden_ind);
7884         END IF;
7885 
7886         -- Now merge the costs adjustments just loaded with the existing costs loaded in the rollup_lot_costs
7887         -- procedure. pass Merge type as V value adjustment
7888 
7889         merge_costs ( old_cost_tab
7890                     , 0
7891                     , old_cost.onhand_qty
7892                     , 'V');
7893 
7894         -- Write the adjusted costs to the database
7895           create_cost_header
7896             ( transaction_row.inventory_item_id
7897             , transaction_row.lot_number
7898             , transaction_row.orgn_id
7899             , l_cost_type_id
7900             , new_cost.unit_cost
7901             , transaction_row.trans_date
7902             , old_cost.onhand_qty
7903             , transaction_row.transaction_id
7904             , transaction_row.transaction_source_type_id
7905             , transaction_row.transaction_action_id
7906             , new_cost.header_id
7907             , new_unit_cost
7908             , new_cost.onhand_qty
7909             , l_return_status);
7910 
7911         -- If that worked OK, create the new cost details
7912         IF l_return_status = 'S' THEN
7913             FOR j IN 1..new_cost_tab.COUNT LOOP
7914 
7915                 create_cost_detail
7916                     (new_cost.header_id
7917                    , new_cost_tab(j).cost_cmpntcls_id
7918                    , new_cost_tab(j).cost_analysis_code
7919                    , 0
7920                    , new_cost_tab(j).component_cost
7921                    , 0
7922                    , l_return_status);
7923 
7924                 IF l_return_status <> 'S'THEN
7925                     RETURN;
7926                 END IF;
7927             END LOOP;
7928 
7929             -- Finally create a transaction for the adjustment
7930             new_cost.unit_cost := new_unit_cost;
7931             IF NOT old_cost_tab.EXISTS(1) THEN
7932               create_material_transaction
7933                 ( new_cost.header_id
7934                 , l_cost_type_id
7935                 , transaction_row.trans_date
7936                 , transaction_row.trans_qty
7937                 , transaction_row.trans_um
7938                 , new_cost.onhand_qty * new_cost.unit_cost
7939                 ,-9             -- trans_id
7940                 , new_cost.unit_cost
7941                 , transaction_row.trans_qty
7942                 , NULL
7943                 , NULL
7944                 , NULL
7945                 ,transaction_row.lot_number
7946                 , l_return_status);
7947             ELSE
7948               create_material_transaction
7949                 ( new_cost.header_id
7950                 , l_cost_type_id
7951                 , transaction_row.trans_date
7952                 , transaction_row.trans_qty
7953                 , transaction_row.trans_um
7954                 , new_cost.onhand_qty * new_cost.unit_cost - old_cost.onhand_qty * old_cost.unit_cost
7955                 , -9    -- trans_id
7956                 , new_cost.unit_cost
7957                 , new_cost.onhand_qty
7958                 , old_cost.unit_cost
7959                 , old_cost.onhand_qty
7960                 , NULL
7961                 , transaction_row.lot_number
7962                 , l_return_status);
7963             END IF;
7964           END IF;
7965 
7966           END IF; -- PK Bug 10159100  do not create LC records if applied_amt is zero
7967 
7968            fnd_file.put_line(fnd_file.log,'adj_transaction_id: ' || l_adjustments_cursor(i).adj_transaction_id);
7969 
7970            OPEN cur_lca_count (l_adjustments_cursor(i).adj_transaction_id, transaction_row.lot_number);
7971            FETCH cur_lca_count INTO l_count;
7972            CLOSE cur_lca_count;
7973 
7974        /* If LC lot cost adjustments exists update else insert adjustment record  */
7975          IF l_count >0 THEN
7976 
7977             UPDATE  gmf_lc_lot_cost_adjs lca SET
7978                     lca.lot_costed_flag        = l_final_run_flag
7979                   , lca.last_update_date       = sysdate
7980                   , lca.last_updated_by        = l_user_id
7981                   , lca.last_update_login      = l_user_id
7982                   , lca.program_application_id = l_prog_appl_id
7983                   , lca.program_id             = l_program_id
7984                   , lca.request_id             = l_request_id
7985               WHERE
7986                     lca.adj_transaction_id = l_adjustments_cursor(i).adj_transaction_id
7987                 AND lca.lot_number         = transaction_row.lot_number  ;
7988 
7989          ELSE
7990              INSERT INTO gmf_lc_lot_cost_adjs(
7991                     lc_adjustment_id,         --01
7992                     adj_transaction_id,       --02
7993                     adjustment_quantity,      --03
7994                     costed_quantity,          --04
7995                     total_quantity,           --05
7996                     quantity_uom_code,        --06
7997                     unit_base_price,          --07
7998                     base_amount,              --08
7999                     trans_amount,             --09
8000                     base_adj_amount,          --10
8001                     trans_adj_amount,         --11
8002                     cost_type_id,             --12
8003                     accounted_flag,           --13
8004                     final_posting_date,       --14
8005                     lot_number,               --15
8006                     lot_costed_flag ,         --16
8007                     onhand_quantity,          --17
8008                     old_cost_header_id,       --18
8009                     new_cost_header_id,       --19
8010                     creation_date,            --20
8011                     created_by,               --21
8012                     last_update_date,         --22
8013                     last_updated_by,          --23
8014                     last_update_login,        --24
8015                     request_id,               --25
8016                     program_application_id,   --26
8017                     program_id,               --27
8018                     program_udpate_date       --28
8019                    )
8020              VALUES(
8021                     gmf_lc_actual_adjs_s.NEXTVAL,               --01
8022                     l_adjustments_cursor(i).adj_transaction_id, --02
8023                     new_cost.onhand_qty,                        --03
8024                     new_cost.onhand_qty,                        --04
8025                     new_cost.onhand_qty,                        --05
8026                     transaction_row.trans_um,                   --06
8027                     new_cost.unit_cost,                         --07
8028                     l_adjustments_cursor(i).adjustment_amt,     --08  base_amount
8029                     l_adjustments_cursor(i).adjustment_amt,     --09  trans_amount
8030                     l_adjustments_cursor(i).applied_amt,     --10  base_adj_amount Bug 10159100 changed to applied_amt
8031                     l_adjustments_cursor(i).applied_amt,     --11  trans_adj_amount Bug 10159100 changed to applied_amt
8032                     l_cost_type_id,                             --12
8033                     'N',                                        --13
8034                     NULL,                                       --14
8035                     transaction_row.lot_number,                 --15
8036                     l_final_run_flag,                            --16
8037                     new_cost.onhand_qty,                        --17
8038                     old_cost.header_id,                         --18
8039                     new_cost.header_id,                         --19
8040                     SYSDATE,                                    --20
8041                     l_user_id,                                  --21
8042                     SYSDATE,                                    --22
8043                     l_user_id,                                  --23
8044                     0,                                          --24
8045                     l_request_id,                               --25
8046                     l_prog_appl_id,                             --26
8047                     l_program_id,                               --27
8048                     SYSDATE                                     --28
8049                    );
8050 
8051          END IF;
8052 
8053     END LOOP;
8054 
8055     IF l_debug_level >= l_debug_level_medium THEN
8056        fnd_file.put_line(fnd_file.log,'Leaving Procedure: '|| procedure_name );
8057    END IF;
8058 
8059 EXCEPTION
8060   WHEN OTHERS THEN
8061     fnd_file.put_line(fnd_file.log,'ERROR: Unable to Process Actual LC Adjustment ');
8062     fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
8063     l_return_status := 'E';
8064 
8065 END process_lc_adjustments;
8066 
8067     --**********************************************************************************************
8068     --*                                                                                            *
8069     --* Procedure to delete previously costed rows in the trail run.                               *
8070     --*                                                                                            *
8071     --**********************************************************************************************
8072 
8073 
8074 PROCEDURE delete_lot_costs
8075 IS
8076 
8077     TYPE lot_cost_cursor_type IS REF CURSOR;
8078     Cur_lc_header lot_cost_cursor_type;
8079 
8080     TYPE header_ids_tab IS TABLE OF gmf_lot_costs.header_id%TYPE
8081     	INDEX BY BINARY_INTEGER;
8082     l_header_ids_tab       header_ids_tab;
8083     l_empty_header_ids_tab header_ids_tab;
8084 
8085 
8086     TYPE rowids_tab IS TABLE OF rowid INDEX BY BINARY_INTEGER;
8087     l_rowids_tab       rowids_tab;
8088     l_empty_rowids_tab rowids_tab;
8089 
8090 
8091     l_rows_to_delete	PLS_INTEGER;
8092     l_indx_from		PLS_INTEGER;
8093     l_indx_to		PLS_INTEGER;
8094     l_max_loop_cnt	PLS_INTEGER;
8095     l_remaining_rows	PLS_INTEGER;
8096 
8097     l_matl_rows_deleted PLS_INTEGER;
8098     l_cdtl_rows_deleted PLS_INTEGER;
8099     procedure_name VARCHAR2(100);
8100 
8101 BEGIN
8102   procedure_name := 'Delete Lot Costs';
8103   IF l_debug_level >= l_debug_level_medium THEN
8104      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
8105   END IF;
8106 
8107   l_rows_to_delete	:= 1000;
8108   l_matl_rows_deleted   := 0;
8109   l_cdtl_rows_deleted   := 0;
8110 
8111   /* umoogala 29-Mar-2004
8112   ** Since item_id overrides itemcost_class check for item_id first
8113   */
8114 
8115     -- Open a cursor that retrieves only the item/lot/sublot specified
8116     OPEN Cur_lc_header FOR
8117     	SELECT glc.header_id, glc.rowid
8118 	      FROM  gmf_lot_costs glc,
8119 	            gmf_lot_costed_items_gt gpo
8120        WHERE  glc.organization_id   = gpo.organization_id
8121          AND  glc.inventory_item_id = gpo.inventory_item_id
8122 	       AND  glc.cost_type_id      = l_cost_type_id
8123 	       AND  gpo.organization_id IN (select organization_id from gmf_process_organizations_gt
8124 	                                     where legal_entity_id = l_le_id)/* Bug 8687115 */
8125 	       AND  glc.final_cost_flag 	= 0
8126 	       --AND  glc.inventory_item_id = l_item_id  /*jboppana*/
8127 	       AND  glc.lot_number 		    = DECODE(l_lot_no, NULL, glc.lot_number, l_lot_no)
8128 	       ;
8129 
8130   FETCH Cur_lc_header BULK COLLECT INTO l_header_ids_tab, l_rowids_tab;
8131   CLOSE Cur_lc_header;
8132 
8133   IF l_header_ids_tab.EXISTS(1) THEN
8134 
8135     --
8136     -- umoogala: delete and commit for every 10000 rows
8137     -- All the logic here is to avoid 'ORA-22160: element at index [n] does not exist'
8138     -- error when trying to delete non-existent elememnt.
8139     --
8140 
8141     l_indx_from      := l_header_ids_tab.FIRST;
8142     l_remaining_rows := l_header_ids_tab.count;
8143 
8144     IF l_header_ids_tab.count <= l_rows_to_delete THEN
8145       l_indx_to      := l_header_ids_tab.count;
8146       l_max_loop_cnt := 1;
8147 
8148     ELSE
8149       l_indx_to      := l_rows_to_delete;
8150       l_max_loop_cnt := ceil(l_header_ids_tab.count/l_rows_to_delete);
8151     END IF;
8152 
8153 
8154     fnd_file.put_line(fnd_File.LOG, '#of rows to delete in cost header: ' || l_header_ids_tab.count);
8155     fnd_file.put_line(fnd_File.LOG, 'l_max_loop_cnt: ' || l_max_loop_cnt);
8156 
8157     FOR i in 1..l_max_loop_cnt
8158     LOOP
8159         -- Delete all material trx info
8160         FORALL indx IN l_indx_from..l_indx_to
8161           DELETE FROM gmf_material_lot_cost_txns
8162           WHERE cost_header_id in l_header_ids_tab(indx);
8163 
8164   	      l_matl_rows_deleted := l_matl_rows_deleted + SQL%ROWCOUNT;
8165 
8166         -- Delete all cost details
8167         FORALL indx IN l_indx_from..l_indx_to
8168           DELETE FROM gmf_lot_cost_details
8169           WHERE abs(header_id) in l_header_ids_tab(indx);
8170 
8171           l_cdtl_rows_deleted := l_cdtl_rows_deleted + SQL%ROWCOUNT;
8172 
8173         COMMIT;
8174 
8175         l_remaining_rows := l_header_ids_tab.COUNT - (i * l_rows_to_delete);
8176 
8177         EXIT WHEN (l_header_ids_tab.count <= l_rows_to_delete) OR
8178                   (l_remaining_rows < 0);
8179 
8180 
8181         IF l_remaining_rows <= l_rows_to_delete THEN
8182           l_indx_from := l_indx_to + 1;
8183           l_indx_to   := l_header_ids_tab.COUNT;
8184 
8185         ELSE
8186           l_indx_from := l_indx_to + 1;
8187           l_indx_to   := l_indx_to + l_rows_to_delete;
8188         END IF;
8189 
8190     END LOOP;
8191 
8192     -- Now delete all rows from main lot costs table
8193     FORALL indx IN l_rowids_tab.FIRST..l_rowids_tab.LAST
8194       DELETE FROM gmf_lot_costs
8195       WHERE rowid in l_rowids_tab(indx);
8196 
8197     fnd_file.put_line(fnd_File.LOG, '  ' || l_matl_rows_deleted || ' rows deleted from gmf_material_lot_cost_txns.');
8198     fnd_file.put_line(fnd_File.LOG, '  ' || l_cdtl_rows_deleted || ' rows deleted from gmf_lot_cost_details.');
8199     fnd_file.put_line(fnd_File.LOG, '  ' || SQL%ROWCOUNT || ' rows deleted from gmf_lot_costs.');
8200 
8201     COMMIT;
8202 
8203     -- remove old rows and release memory.
8204     l_header_ids_tab	:= l_empty_header_ids_tab;
8205     l_rowids_tab	:= l_empty_rowids_tab;
8206 
8207   END IF;
8208 
8209 /***** Bug 4094132 -  Added the following Delete - Start  *****/
8210 -- Delete the residual transactions for which header is final costed
8211 -- but because of reversal, one more transaction got created for the same header.
8212 
8213 DELETE
8214   FROM gmf_material_lot_cost_txns t
8215  WHERE cost_type_id = l_cost_type_id
8216   AND t.final_cost_flag = 0 -- Bug 7173679
8217   AND EXISTS (
8218           SELECT 1
8219             FROM gmf_lot_costs glc,
8220                  gmf_process_organizations_gt gpo
8221            WHERE glc.organization_id = gpo.organization_id
8222              AND glc.header_id = t.cost_header_id
8223              AND glc.cost_type_id = t.cost_type_id
8224              AND glc.final_cost_flag = 1
8225              );
8226 
8227   l_matl_rows_deleted := l_matl_rows_deleted + SQL%ROWCOUNT;
8228 
8229  IF l_matl_rows_deleted = 0 THEN
8230     fnd_file.put_line(fnd_File.LOG, '  No rows found to delete.');
8231  END IF;
8232 
8233  COMMIT;
8234  /***** Bug 4094132 -  Added the above Delete  - End *****/
8235   IF l_debug_level >= l_debug_level_medium THEN
8236      fnd_file.put_line(fnd_file.log,'Leaving Procedure: '||procedure_name);
8237   END IF;
8238 
8239 END delete_lot_costs;
8240 
8241     --**********************************************************************************************
8242     --*                                                                                            *
8243     --* Procedure to process Lot cost Adjustment rows                                              *
8244     --*                                                                                            *
8245     --**********************************************************************************************
8246 
8247 PROCEDURE process_lot_cost_adjustments
8248 IS
8249   CURSOR adjustments_cursor IS
8250   SELECT SYSTEM.gmf_cost_type
8251 	 ( lcad.cost_cmpntcls_id
8252          , lcad.cost_analysis_code
8253          , 0
8254          , lcad.adjustment_cost
8255          , 0
8256 	 )
8257   FROM  gmf_lot_cost_adjustment_dtls lcad
8258   WHERE lcad.adjustment_id = transaction_row.doc_id
8259   AND   lcad.delete_mark = 0;
8260 
8261   new_unit_cost   NUMBER;
8262   procedure_name VARCHAR2(100);
8263 
8264 BEGIN
8265   procedure_name := 'Process Lot Cost Adjustments';
8266   IF l_debug_level >= l_debug_level_medium THEN
8267      fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
8268   END IF;
8269 
8270   OPEN  adjustments_cursor;
8271   FETCH adjustments_cursor BULK COLLECT INTO new_cost_tab;
8272   CLOSE adjustments_cursor;
8273 
8274   -- Now merge the costs adjustments just loaded with the existing costs loaded in the rollup_lot_costs
8275   -- procedure.
8276 
8277    merge_costs ( old_cost_tab
8278   , 0
8279   , old_cost.onhand_qty
8280   , 'C'
8281   );
8282 
8283   -- Write the adjusted costs to the database
8284 /* INVCONV sschinch changes done to parameter*/
8285   create_cost_header
8286   ( transaction_row.inventory_item_id
8287   , transaction_row.lot_number
8288   , transaction_row.orgn_id
8289   , l_cost_type_id
8290   , new_cost.unit_cost
8291   , transaction_row.trans_date
8292   , old_cost.onhand_qty
8293   , transaction_row.doc_id
8294   , transaction_row.transaction_source_type_id
8295   ,transaction_row.transaction_action_id
8296   , new_cost.header_id
8297   , new_unit_cost
8298   , new_cost.onhand_qty
8299   , l_return_status
8300   ); -- PJS 17-Mar-2004 no bug reference
8301 
8302   -- If that worked OK, create the new cost details
8303 
8304   IF l_return_status = 'S' THEN
8305     FOR i IN 1..new_cost_tab.COUNT
8306     LOOP
8307       create_cost_detail
8308       ( new_cost.header_id
8309       , new_cost_tab(i).cost_cmpntcls_id
8310       , new_cost_tab(i).cost_analysis_code
8311       , 0
8312       , new_cost_tab(i).component_cost
8313       , 0
8314       , l_return_status
8315       );
8316 
8317       IF l_return_status <> 'S' THEN
8318         RETURN;
8319       END IF;
8320     END LOOP;
8321 
8322     -- Finally create a transaction for the adjustment
8323 
8324     new_cost.unit_cost := new_unit_cost;
8325 
8326     IF NOT old_cost_tab.EXISTS(1) THEN
8327       create_material_transaction
8328       ( new_cost.header_id
8329       , l_cost_type_id
8330       , transaction_row.trans_date
8331       , transaction_row.trans_qty
8332       , transaction_row.trans_um
8333       , new_cost.onhand_qty * new_cost.unit_cost
8334       , -9	-- trans_id
8335       , new_cost.unit_cost
8336       , transaction_row.trans_qty
8337       , NULL
8338       , NULL
8339       , NULL
8340       ,transaction_row.lot_number
8341       , l_return_status
8342       );
8343 
8344     ELSE
8345       create_material_transaction
8346       ( new_cost.header_id
8347       , l_cost_type_id /* INVCONV sschinch*/
8348       , transaction_row.trans_date
8349       , transaction_row.trans_qty
8350       , transaction_row.trans_um
8351       , new_cost.onhand_qty * new_cost.unit_cost - old_cost.onhand_qty * old_cost.unit_cost
8352       , -9	-- trans_id
8353       , new_cost.unit_cost
8354       , new_cost.onhand_qty
8355       , old_cost.unit_cost
8356       , old_cost.onhand_qty
8357       , NULL
8358       ,transaction_row.lot_number
8359       , l_return_status
8360       );
8361     END IF;
8362   END IF;
8363 
8364   IF l_debug_level >= l_debug_level_medium THEN
8365        fnd_file.put_line
8366        (fnd_file.log,'Leaving Procedure: '||procedure_name);
8367      END IF;
8368 
8369 END process_lot_cost_adjustments;
8370 
8371 
8372 --********************************************************************************************************
8373 --*    Procedure Name : PROCESS_WIP_BATCH
8374 --*
8375 --*     Description :
8376 --*               Procedure to Process the PROD transaction records, whose current
8377 --*       status is not Completed or Closed. Basically if we complete a batch and then
8378 --*       for example we yield 20LB product. Now we reverted the batch to WIP State
8379 --*       introducing a new -20 LB record. At this point, If we run the Lot Actual Cost Process
8380 --*       we don't cost these two transactions. Rather we copy the previous cost of the Lot if any
8381 --*       or 0$ to both these +20LB and -20LB transaction.
8382 --*
8383 --********************************************************************************************************
8384 
8385 PROCEDURE process_wip_batch
8386 IS
8387    procedure_name VARCHAR2(100);
8388    x_mtl_analysis_code  CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE;    /* B9131983 and bug14480540*/
8389    x_mtl_cmpntcls_id    NUMBER;         /* B9131983 */
8390 
8391 BEGIN
8392   procedure_name := 'Process WIP Batch';
8393   IF l_debug_level >= l_debug_level_medium THEN
8394      fnd_file.put_line (fnd_file.log,' Entering process_wip_batch');
8395    END IF;
8396 
8397    /* B9131983 If cost is ZERO or component is not available
8398       select the default value from fiscal policy and use it in cost details */
8399    SELECT mtl_analysis_code, mtl_cmpntcls_id
8400      INTO x_mtl_analysis_code, x_mtl_cmpntcls_id
8401      FROM GMF_FISCAL_POLICIES WHERE legal_entity_id = l_le_id ;
8402 
8403   old_cost_tab.delete;
8404 
8405   OPEN  lot_cost_cursor (transaction_row.orgn_id,
8406                          transaction_row.inventory_item_id,
8407                          transaction_row.lot_number,
8408                          transaction_row.trans_date,
8409                          l_cost_type_id);
8410   FETCH lot_cost_cursor INTO old_cost;
8411 
8412   IF lot_cost_cursor%FOUND THEN
8413      IF l_debug_level >= l_debug_level_high THEN
8414        fnd_file.put_line
8415        (fnd_file.log,'Reading existing costs for header ID '||old_cost.header_id);
8416      END IF;
8417 
8418       OPEN  lot_cost_detail_cursor (old_cost.header_id);
8419       FETCH lot_cost_detail_cursor BULK COLLECT INTO old_cost_tab;
8420       CLOSE lot_cost_detail_cursor;
8421 
8422      END IF;
8423      CLOSE lot_cost_cursor;
8424 
8425      IF old_cost_tab.EXISTS(1) THEN
8426        IF l_debug_level >= l_debug_level_high THEN
8427          fnd_file.put_line
8428          (fnd_file.log,'Lot Cost before this transaction is '||to_char(old_cost.unit_cost,'999999999.99'));
8429        END IF;
8430 
8431        -- At this stage we have all the header and cost component information we need
8432        -- to store the costs in the database.
8433 
8434        create_cost_header
8435        ( transaction_row.inventory_item_id
8436        , transaction_row.lot_number /* INVCONV sschinch */
8437        , transaction_row.orgn_id    /* INVCONV sschinch */
8438        , l_cost_type_id             /*INVCONV sschinch */
8439        , old_cost.unit_cost     -- Carry Forward the Cost
8440        , transaction_row.trans_date
8441        , old_cost.onhand_qty + transaction_row.trans_qty
8442        , transaction_row.doc_id
8443        , transaction_row.transaction_source_type_id
8444        ,transaction_row.transaction_action_id
8445        , new_cost.header_id
8446        , dummy
8447        , new_cost.onhand_qty
8448        , l_return_status
8449        );
8450 
8451        IF l_return_status ='S' THEN
8452          l_dtl_inserted := 0; --  Bug 13038249
8453          FOR k IN 1.. old_cost_tab.count
8454          LOOP
8455 
8456          IF ( old_cost_tab(k).component_cost <> 0 OR (l_dtl_inserted = 0 AND k = old_cost_tab.count )) THEN  -- Bug 13038249 Modified condition and restored IF This is redundant though
8457           /* B9131983 If cost is ZERO or component is not available
8458              enter the default row in cost details */
8459           IF old_cost_tab(k).cost_cmpntcls_id = 0 THEN
8460              old_cost_tab(k).cost_cmpntcls_id := x_mtl_cmpntcls_id;
8461           END IF;
8462 
8463            IF NVL(rtrim(ltrim(old_cost_tab(k).cost_analysis_code)),'X') = 'X' THEN
8464              old_cost_tab(k).cost_analysis_code := x_mtl_analysis_code;
8465           END IF;
8466 
8467                 IF l_debug_level >= l_debug_level_low THEN
8468                   fnd_file.put_line(fnd_file.log,'WIP:After Replacing component/analysis/level to fiscal policy');
8469                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||old_cost_tab(k).cost_cmpntcls_id);
8470                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||old_cost_tab(k).cost_analysis_code);
8471                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||old_cost_tab(k).cost_level);
8472                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||old_cost_tab(k).component_cost);
8473                   fnd_file.put_line(fnd_file.log,'====================================');
8474                 END IF;
8475 
8476              create_cost_detail
8477              ( new_cost.header_id
8478              , old_cost_tab(k).cost_cmpntcls_id
8479              , old_cost_tab(k).cost_analysis_code
8480              , old_cost_tab(k).cost_level
8481              , old_cost_tab(k).component_cost
8482              , 0
8483              , l_return_status
8484              );
8485 
8486            IF l_return_status <> 'S' THEN
8487              RETURN;
8488            END IF;
8489            l_dtl_inserted := 1; --  Bug 13038249
8490        END IF;       -- B9131983 Bug 13038249
8491        END LOOP;
8492 
8493        l_dtl_inserted := 0; --  Bug 13038249
8494        FOR k IN 1 .. old_cost_tab.COUNT
8495        LOOP
8496        -- Write the 'new' costs with a -ve header ID . This is because, there is some cost
8497        -- before this transaction. So this transaction must have ideally resulted in the change of
8498        -- cost. So we may need to store the transaction cost under the -header_id
8499        -- Although this looks like duplicate entry, process_reversals may expect this record.
8500 
8501        -- Bug 13038249 Modified condition and restored IF This is redundant though
8502        IF ( old_cost_tab(k).component_cost <> 0 OR (l_dtl_inserted = 0 AND k = old_cost_tab.COUNT )) THEN
8503 
8504           /* B9131983 If cost is ZERO or component is not available
8505              enter the default row in cost details */
8506           IF old_cost_tab(k).cost_cmpntcls_id = 0 THEN
8507              old_cost_tab(k).cost_cmpntcls_id := x_mtl_cmpntcls_id;
8508           END IF;
8509 
8510            IF NVL(rtrim(ltrim(old_cost_tab(k).cost_analysis_code)),'X') = 'X' THEN
8511              old_cost_tab(k).cost_analysis_code := x_mtl_analysis_code;
8512           END IF;
8513 
8514                 IF l_debug_level >= l_debug_level_low THEN
8515                   fnd_file.put_line(fnd_file.log,'WIP:After Replacing component/analysis/level to fiscal policy');
8516                   fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||old_cost_tab(k).cost_cmpntcls_id);
8517                   fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||old_cost_tab(k).cost_analysis_code);
8518                   fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||old_cost_tab(k).cost_level);
8519                   fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||old_cost_tab(k).component_cost);
8520                   fnd_file.put_line(fnd_file.log,'====================================');
8521                 END IF;
8522 
8523          create_cost_detail
8524          ( -new_cost.header_id
8525          , old_cost_tab(k).cost_cmpntcls_id
8526          , old_cost_tab(k).cost_analysis_code
8527          , old_cost_tab(k).cost_level
8528          , old_cost_tab(k).component_cost
8529          , 0
8530          , l_return_status
8531          );
8532 
8533          IF l_return_status <> 'S' THEN
8534            RETURN;
8535          END IF;
8536          l_dtl_inserted := 1; --  Bug 13038249
8537        END IF;      -- B9131983 Bug 13038249
8538      END LOOP;
8539 
8540      create_material_transaction
8541      ( new_cost.header_id
8542       , l_cost_type_id     /*INVCONV sschinch */
8543       , transaction_row.trans_date
8544       , transaction_row.trans_qty
8545       , transaction_row.trans_um
8546       , old_cost.unit_cost  * transaction_row.trans_qty
8547       , transaction_row.transaction_id
8548       , old_cost.unit_cost
8549       ,  new_cost.onhand_qty -- Same as old_cost.onhand_qty+transaction_row.trans_qty
8550       , old_cost.unit_cost
8551       , old_cost.onhand_qty
8552       , 1
8553       ,transaction_row.lot_number
8554       , l_return_status
8555       );
8556 
8557         IF l_return_status <> 'S' THEN
8558           RETURN;
8559         END IF;
8560       ELSE
8561         RETURN;
8562       END IF;
8563     ELSE
8564       -- No cost currently exists, create one and all associated details
8565       -- and transactions. Again, this is only for the invoking transaction
8566       IF   l_debug_level >= l_debug_level_high THEN
8567         fnd_file.put_line( fnd_file.log,' Previous Cost is NULL');
8568       END IF;
8569 
8570        create_cost_header
8571        ( transaction_row.inventory_item_id
8572        , transaction_row.lot_number  /* INVCONV sschinch */
8573        , transaction_row.orgn_id     /* INVCONV sschinch */
8574        , l_cost_type_id             /*INVCONV sschinch */
8575        , 0     -- No Cost to Carry Forward. So Set to 0$
8576        , transaction_row.trans_date
8577        , transaction_row.trans_qty
8578        , transaction_row.doc_id
8579        , transaction_row.transaction_source_type_id
8580        ,transaction_row.transaction_action_id
8581        , new_cost.header_id
8582        , dummy
8583        , new_cost.onhand_qty
8584        , l_return_status
8585        );
8586 
8587        IF l_return_status <> 'S' THEN
8588           RETURN;
8589        END IF;
8590 
8591         OPEN component_class_cursor
8592              (l_le_id, transaction_row.inventory_item_id,transaction_row.orgn_id, transaction_row.trans_date);
8593         FETCH component_class_cursor INTO component_class_id, cost_analysis_code, dummy;
8594         CLOSE component_class_cursor;
8595 
8596         create_cost_detail
8597         ( new_cost.header_id
8598         , component_class_id
8599         , cost_analysis_code
8600         , 0
8601         , 0.00
8602         , 0
8603         , l_return_status
8604         );
8605 
8606        IF l_return_status <> 'S' THEN
8607           RETURN;
8608        END IF;
8609 
8610        create_material_transaction
8611       ( new_cost.header_id
8612        , l_cost_type_id  /* INVCONV sschinch */
8613        , transaction_row.trans_date
8614        , transaction_row.trans_qty
8615        , transaction_row.trans_um
8616        , 0 -- Total Cost is also 0$
8617        , transaction_row.transaction_id
8618        , 0 -- Carrying Forward 0$
8619        ,  new_cost.onhand_qty -- Same as old_cost.onhand_qty+transaction_row.trans_qty
8620        , 0 -- No Old Cost
8621        , 0  -- No Old Qty
8622        , NULL
8623        ,transaction_row.lot_number
8624        , l_return_status
8625        );
8626 
8627       IF l_return_status <> 'S' THEN
8628           RETURN;
8629        END IF;
8630 
8631     END IF;
8632     IF l_debug_level >= l_debug_level_medium THEN
8633       fnd_file.put_line (fnd_file.log,' Leaving process_wip_batch');
8634      END IF;
8635 
8636 END process_wip_batch;
8637 
8638 
8639 /*=========================================================
8640   PROCEDURE : perform_weighted_average
8641 
8642   DESCRIPTION  This procedure performs weighted avererage of
8643                individual lots
8644   AUTHOR : Sukarna Reddy  INVCONV June 2005
8645  ==========================================================*/
8646 
8647 PROCEDURE perform_weighted_average
8648 ( costs_table      IN OUT NOCOPY l_cost_tab_type
8649  ,trans_qty        IN NUMBER
8650  ,total_qty        IN NUMBER
8651  )
8652 IS
8653   k              NUMBER;
8654   l              NUMBER;
8655   divisor        NUMBER;
8656   l_new_row   NUMBER;
8657   procedure_name VARCHAR2(100);
8658 BEGIN
8659 
8660   procedure_name := 'Weighted Average';
8661 
8662   IF l_debug_level >= l_debug_level_high THEN
8663     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
8664     fnd_file.put_line(fnd_file.log,'trans_qty = '||trans_qty);
8665     fnd_file.put_line(fnd_file.log,'Previous Copy of new_cost_tab is:');
8666 
8667     FOR k IN 1 .. new_cost_tab.COUNT
8668     LOOP
8669       fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
8670       fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
8671       fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||new_cost_tab(k).cost_level);
8672       fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
8673       fnd_file.put_line(fnd_file.log,'====================================');
8674     END LOOP;
8675 
8676     IF costs_table.EXISTS(1) THEN
8677       fnd_file.put_line(fnd_file.log,'Before Average costs_tab is:');
8678       FOR k IN 1 .. costs_table.COUNT
8679       LOOP
8680         fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||costs_table(k).cost_cmpntcls_id);
8681         fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||costs_table(k).cost_analysis_code);
8682         fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||costs_table(k).cost_level);
8683         fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||costs_table(k).component_cost);
8684         fnd_file.put_line(fnd_file.log,'====================================');
8685       END LOOP;
8686     ELSE
8687       fnd_file.put_line(fnd_file.log,'No costs to merge');
8688     END IF;
8689   END IF;
8690 
8691   IF costs_table.EXISTS(1) THEN
8692 
8693       divisor := total_qty;
8694       IF divisor = 0 THEN
8695         divisor := 1;
8696       END IF;
8697 
8698       IF l_debug_level >= l_debug_level_high THEN
8699         fnd_file.put_line(fnd_file.log,'Divisor is  '||divisor);
8700       END IF;   /* Bug 10415925 */
8701 
8702       FOR k in 1 .. costs_table.COUNT
8703       LOOP
8704         costs_table(k).component_cost := costs_table(k).component_cost * trans_qty / divisor;
8705       END LOOP;
8706 
8707       IF l_debug_level >= l_debug_level_high THEN
8708         fnd_file.put_line(fnd_file.log,' new_qty = '||trans_qty);
8709 
8710        -- fnd_file.put_line(fnd_file.log,'After averaging new_cost_tab is:');
8711 
8712         fnd_file.put_line(fnd_file.log,'After averaging costs_tab is:');
8713         FOR k IN 1 .. costs_table.COUNT
8714         LOOP
8715           fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||costs_table(k).cost_cmpntcls_id);
8716           fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||costs_table(k).cost_analysis_code);
8717           fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||costs_table(k).cost_level);
8718           fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||costs_table(k).component_cost);
8719           fnd_file.put_line(fnd_file.log,'====================================');
8720         END LOOP;
8721       END IF;
8722 
8723    IF (new_cost_tab.COUNT = 0) THEN
8724          l_new_row := 1;
8725          FOR k IN 1..costs_table.COUNT
8726            LOOP
8727             new_cost_tab(l_new_row) := SYSTEM.gmf_cost_type(costs_table(k).cost_cmpntcls_id,costs_table(k).cost_analysis_code,costs_table(k).cost_level,costs_table(k).component_cost,0);
8728             l_new_row := l_new_row + 1;
8729           END LOOP;
8730      ELSE
8731        merge_costs(costs_table,
8732                       0,
8733                       0,
8734                      'C'
8735                      );
8736    END IF;
8737 
8738       fnd_file.put_line(fnd_file.log,'After averaging new_cost_tab is:');
8739       FOR k IN 1 .. new_cost_tab.COUNT
8740       LOOP
8741           fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||new_cost_tab(k).cost_cmpntcls_id);
8742           fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||new_cost_tab(k).cost_analysis_code);
8743           fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||new_cost_tab(k).cost_level);
8744           fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||new_cost_tab(k).component_cost);
8745           fnd_file.put_line(fnd_file.log,'====================================');
8746       END LOOP;
8747       END IF;
8748 
8749      IF l_debug_level >= l_debug_level_medium THEN
8750        fnd_file.put_line
8751        (fnd_file.log,'Leaving Procedure: '||procedure_name);
8752      END IF;
8753 EXCEPTION
8754     WHEN OTHERS THEN
8755       fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
8756 END perform_weighted_average;
8757 
8758 
8759 /*=====================================================================
8760   PROCEDURE : get_new_cost
8761 
8762   DESCRIPTION This procedure sums up all the component costs by grouping
8763               component class and analysis code
8764   AUTHOR : Sukarna Reddy  INVCONV
8765 ======================================================================*/
8766 
8767   PROCEDURE get_new_cost (p_cost_tab IN OUT NOCOPY  l_cost_tab_type,
8768                           x_new_cost_tab OUT NOCOPY l_cost_tab_type,
8769                           x_total_cost   OUT NOCOPY NUMBER
8770                           )
8771   IS
8772     l_cost_table SYSTEM.gmf_cost_tab := new SYSTEM.gmf_cost_tab();
8773     CURSOR final_cmpnt_cur IS
8774      SELECT SYSTEM.gmf_cost_type(nct.cost_cmpntcls_id,
8775             nct.cost_analysis_code,
8776             nct.cost_level,
8777             sum(nct.component_cost),
8778             nct.burden_ind)
8779      FROM TABLE ( cast(l_cost_Table AS SYSTEM.gmf_cost_tab) ) nct
8780      GROUP BY nct.cost_cmpntcls_id,nct.cost_analysis_code,nct.cost_level,nct.burden_ind;
8781 
8782     procedure_name VARCHAR2(100);
8783 
8784   BEGIN
8785    procedure_name := 'Get New Cost';
8786   IF l_debug_level >= l_debug_level_medium THEN
8787     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
8788   END IF;
8789 
8790    IF (p_cost_tab.COUNT > 0) THEN
8791     FOR i IN 1..p_cost_tab.COUNT LOOP
8792       l_cost_table.extend;
8793       l_cost_table(i) := SYSTEM.gmf_cost_type(p_cost_tab(i).cost_cmpntcls_id,
8794                                       p_cost_tab(i).cost_analysis_code,
8795                                       p_cost_tab(i).cost_level,
8796                                       p_cost_tab(i).component_cost,
8797                                       p_cost_tab(i).burden_ind);
8798     END LOOP;
8799      OPEN final_cmpnt_cur;
8800      FETCH final_cmpnt_cur BULK COLLECT INTO x_new_cost_tab;
8801      CLOSE final_cmpnt_cur;
8802    END IF;
8803 
8804 
8805    SELECT SUM(nct.component_cost)
8806    INTO x_total_cost
8807    FROM TABLE ( CAST(l_cost_table AS SYSTEM.gmf_cost_tab) ) nct;
8808 
8809    IF l_debug_level >= l_debug_level_high THEN
8810      fnd_file.put_line(fnd_file.log,'After weighted average new_cost_tab is:');
8811      FOR k IN 1 .. new_cost_tab.COUNT
8812      LOOP
8813        fnd_file.put_line(fnd_file.log,'CCC/ID['||k||']: '||x_new_cost_tab(k).cost_cmpntcls_id);
8814        fnd_file.put_line(fnd_file.log,'A/Code['||k||']: '||x_new_cost_tab(k).cost_analysis_code);
8815        fnd_file.put_line(fnd_file.log,'Level['||k||'] : '||x_new_cost_tab(k).cost_level);
8816        fnd_file.put_line(fnd_file.log,'C/Cost['||k||']: '||x_new_cost_tab(k).component_cost);
8817        fnd_file.put_line(fnd_file.log,'====================================');
8818      END LOOP;
8819      fnd_file.put_line(fnd_file.log,'After merging, new unit cost is: '||x_total_cost);
8820    END IF;
8821    IF l_debug_level >= l_debug_level_medium THEN
8822        fnd_file.put_line
8823        (fnd_file.log,'Leaving Procedure: '||procedure_name);
8824      END IF;
8825 EXCEPTION
8826     WHEN OTHERS THEN
8827       fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
8828 END get_new_cost;
8829 
8830 /*=========================================================
8831   PROCEDURE : Load_lot_costed_items_gt
8832 
8833   DESCRIPTION
8834     This procedure loads global temporary tables with process
8835     organizations and lot costed items.
8836   AUTHOR : Sukarna Reddy  INVCONV June 2005
8837 
8838   HISTORY
8839     jboppana bug 5241052
8840       added inventory_asset_flag and process_costing_enabled_flag to the insert query
8841     ANTHIYAG Bug#5279681
8842       Modified Query to correct the Query which fetches item codes based on Category
8843       Codes and also to add delete_mark check for the first query
8844     Pramod Bug 13386258 - Re-org the code
8845  ==========================================================*/
8846 
8847  PROCEDURE Load_Lot_Costed_Items_gt(p_le_id        IN NUMBER,
8848                                    p_orgn_id      IN NUMBER,
8849                                    p_item_id      IN NUMBER,
8850                                    p_category_id  IN NUMBER,
8851                                    x_return_status OUT NOCOPY NUMBER
8852                                    ) IS
8853 
8854    l_from_orgn_code    VARCHAR2(4) := NULL;
8855    l_row_count         NUMBER;
8856    ll_return_status    NUMBER;
8857 
8858    CURSOR get_process_org IS
8859    SELECT organization_code,
8860           organization_id
8861     FROM  gmf_process_organizations_gt
8862    ORDER BY organization_code;
8863 
8864    procedure_name VARCHAR2(100);
8865  BEGIN
8866 
8867    procedure_name := 'Load_Lot_Costed_Items_gt';
8868 
8869    fnd_file.put_line(fnd_file.log,'Loading GT Items: '|| to_char(sysdate,'DD-MON-YYYY HH24:MI:SS') );
8870 
8871    IF (p_le_id IS NOT NULL) THEN
8872 
8873     l_le_id := p_le_id;
8874     l_row_count := 0 ;
8875 
8876      -- Always load all organizations. There may be transfers across orgs
8877     gmf_organizations_pkg.get_process_organizations(p_legal_entity_id => l_le_id
8878                                                    ,p_From_Orgn_Code => NULL
8879                                                    ,p_To_Orgn_Code  => NULL
8880                                                    ,x_Row_Count     => l_row_count
8881                                                    ,x_Return_Status => ll_return_status
8882                                                    );
8883 
8884     IF (ll_return_status <> 0) THEN
8885       x_return_status := ll_return_status;
8886       RETURN;
8887     END IF;
8888 
8889     /* Build index for organization id */
8890     FOR cur_rec IN get_process_org LOOP
8891       l_org_tab(cur_rec.organization_id) := cur_rec.organization_code;
8892     END LOOP;
8893 
8894    INSERT
8895       INTO GMF_LOT_COSTED_ITEMS_GT
8896       (
8897           organization_id,
8898           inventory_item_id,
8899           primary_uom_code
8900       )
8901       SELECT
8902            msi.organization_id,
8903            msi.inventory_item_id,
8904            msi.primary_uom_code
8905       FROM gmf_lot_costed_items lci,
8906            mtl_system_items_b msi,
8907            gmf_process_organizations_gt gpo
8908       WHERE lci.legal_entity_id = l_le_id
8909         AND lci.delete_mark = 0 /* ANTHIYAG Bug#5279681 06-Jun-2006 */
8910         AND gpo.organization_id = msi.organization_id
8911         AND msi.lot_control_code = 2
8912         AND lci.inventory_item_id = msi.inventory_item_id
8913         AND msi.inventory_asset_flag = 'Y'
8914         AND msi.process_costing_enabled_flag = 'Y'
8915         AND lci.inventory_item_id = NVL(p_item_id,lci.inventory_item_id)
8916         AND lci.cost_type_id = l_cost_type_id
8917         AND
8918          (
8919            (
8920               p_item_id IS NULL
8921               AND p_category_id IS NULL
8922            )
8923           OR
8924           (
8925               p_item_id IS NOT NULL
8926           )
8927          );
8928 
8929    INSERT
8930       INTO GMF_LOT_COSTED_ITEMS_GT
8931       (
8932           organization_id,
8933           inventory_item_id,
8934           primary_uom_code
8935       )
8936       SELECT
8937         i.organization_id,    /*ANTHIYAG Bug#5279681 06-Jun-2006 */
8938         i.inventory_item_id,  /*ANTHIYAG Bug#5279681 06-Jun-2006 */
8939         i.primary_uom_code
8940       FROM mtl_item_categories mic,
8941            mtl_default_category_sets mdc,
8942            mtl_category_sets mcs,
8943            gmf_lot_costed_items g,
8944            mtl_system_items_b i,
8945            gmf_process_organizations_gt gpo
8946      WHERE g.cost_category_id = mic.category_id
8947           AND g.legal_entity_id = l_le_id
8948           AND mdc.functional_area_id = 19
8949           AND mdc.category_set_id = mcs.category_set_id
8950 	  AND mic.category_set_id = mcs.category_set_id
8951           AND g.delete_mark = 0
8952           AND i.lot_control_code = 2
8953           AND gpo.organization_id = i.organization_id
8954           AND i.organization_id = mic.organization_id
8955           AND mic.inventory_item_id = i.inventory_item_id
8956           AND i.inventory_asset_flag = 'Y'
8957           AND i.process_costing_enabled_flag = 'Y'
8958           AND g.cost_type_id = l_cost_type_id
8959           AND g.cost_category_id = NVL(p_category_id,g.cost_category_id)
8960           AND
8961            (
8962             (
8963                p_item_id IS NULL
8964                AND p_category_id IS NULL
8965             )
8966             OR
8967             (
8968                p_category_id IS NOT NULL
8969             )
8970            )
8971 	  AND not exists (  -- Exclude item/orgs from 1st insert
8972 	     select 1
8973 	     from GMF_LOT_COSTED_ITEMS_GT lcig
8974 	     where lcig.inventory_item_id = i.inventory_item_id
8975 	     and lcig.organization_id = i.organization_id
8976 	  );
8977 
8978     /* Bug 8730374 added this union to load one item if assigned by cost category */
8979     INSERT
8980 	INTO GMF_LOT_COSTED_ITEMS_GT
8981 	(
8982           organization_id,
8983           inventory_item_id,
8984           primary_uom_code
8985 	)
8986        SELECT
8987         mic.organization_id,    /*ANTHIYAG Bug#5279681 06-Jun-2006 */
8988         mic.inventory_item_id,  /*ANTHIYAG Bug#5279681 06-Jun-2006 */
8989         i.primary_uom_code
8990        FROM mtl_item_categories mic,
8991             mtl_default_category_sets mdc,
8992             mtl_category_sets mcs,
8993            gmf_lot_costed_items g,
8994            mtl_system_items_b i,
8995            gmf_process_organizations_gt gpo
8996        WHERE g.cost_category_id = mic.category_id
8997           AND g.legal_entity_id = l_le_id
8998           AND mdc.functional_area_id = 19
8999           AND mdc.category_set_id = mcs.category_set_id
9000 	  AND mic.category_set_id = mcs.category_set_id
9001           AND g.delete_mark = 0
9002           AND i.lot_control_code = 2
9003           AND gpo.organization_id = i.organization_id
9004           AND i.organization_id = mic.organization_id
9005           AND mic.inventory_item_id = i.inventory_item_id
9006           AND i.inventory_asset_flag = 'Y'
9007           AND i.process_costing_enabled_flag = 'Y'
9008           AND g.cost_type_id = l_cost_type_id
9009 	  AND mic.inventory_item_id = p_item_id
9010 	  and not exists (  -- Exclude item/orgs from 1st, 2nd inserts
9011 	     select 1
9012 	     from GMF_LOT_COSTED_ITEMS_GT lcig
9013 	     where lcig.inventory_item_id = mic.inventory_item_id
9014 	     and lcig.organization_id = mic.organization_id
9015 	  );
9016 
9017     x_return_status := 0;
9018 
9019    ELSE
9020        fnd_file.put_line
9021        (fnd_file.log,'ERROR: Legal entity is NULL in '||procedure_name);
9022        x_return_status := -1;
9023    END IF;
9024 
9025     IF l_debug_level >= l_debug_level_medium THEN
9026        fnd_file.put_line
9027        (fnd_file.log,'Leaving Procedure: '||procedure_name);
9028      END IF;
9029 
9030 EXCEPTION
9031     WHEN OTHERS THEN
9032       fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
9033       x_return_status := -1;
9034 END Load_Lot_Costed_Items_gt;
9035 
9036   /*=========================================================
9037     PROCEDURE : Process_lot_split
9038 
9039     DESCRIPTION
9040       This procedure process lot split transactions.
9041     AUTHOR : Sukarna Reddy  INVCONV
9042    ==========================================================*/
9043 
9044   PROCEDURE process_lot_split IS
9045     l_old_cost gmf_lot_costs%ROWTYPE;
9046     l_old_cost_tab l_cost_tab_type;
9047     l_parent_lot_number VARCHAR2(80);
9048     i NUMBER;
9049     l_new_cost  NUMBER;
9050     procedure_name VARCHAR2(100);
9051   BEGIN
9052    procedure_name := 'Process Lot Split';
9053    IF l_debug_level >= l_debug_level_medium THEN
9054     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
9055    END IF;
9056 
9057     IF (transaction_row.transaction_id = transaction_row.transfer_transaction_id)
9058     THEN    /* This is a parent lot from which split happened */
9059     -- Check if cost for this Lot exist
9060       IF old_cost_tab.EXISTS(1) THEN
9061         process_adjustment;
9062       ELSE
9063         process_creation;
9064       END IF;
9065     ELSE  /* This is a new lot created from the parent lot */
9066       --check if there is cost from parent lot At this point
9067       SELECT  mln.lot_number
9068         INTO  l_parent_lot_number
9069         FROM  mtl_transaction_lot_numbers mln,
9070               mtl_material_transactions mmt
9071        WHERE  mmt.transaction_id = transaction_row.transfer_transaction_id
9072             AND mmt.transaction_id = mln.transaction_id;
9073 
9074        OPEN lot_cost_cursor(transaction_row.orgn_id,
9075                             transaction_row.inventory_item_id,
9076                             l_parent_lot_number,
9077                             transaction_row.trans_date,
9078                             l_cost_type_id);
9079        FETCH lot_cost_cursor INTO l_old_cost;
9080 
9081        IF lot_cost_cursor%FOUND THEN
9082          IF   l_debug_level >= l_debug_level_high THEN
9083             fnd_file.put_line
9084             (fnd_file.log,'Reading existing costs for header ID '||l_old_cost.header_id);
9085          END IF;
9086 
9087          OPEN  lot_cost_detail_cursor (l_old_cost.header_id);
9088          FETCH lot_cost_detail_cursor BULK COLLECT INTO l_old_cost_tab;
9089          CLOSE lot_cost_detail_cursor;
9090 
9091          IF (l_old_cost_tab.EXISTS(1)) THEN
9092            merge_costs(l_old_cost_tab,
9093                        old_cost.onhand_qty,
9094                        transaction_row.trans_qty,
9095                        'C'
9096                       );
9097             merge_costs(old_cost_tab,
9098                         transaction_row.trans_qty, /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9099                         old_cost.onhand_qty, /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9100                        'A'
9101                        );
9102           END IF;
9103           CLOSE lot_cost_cursor;
9104        ELSE
9105          CLOSE lot_cost_cursor;
9106          -- if we are here that means there is no incomming cost from the parent lot
9107          -- check if current child lot has prior cost
9108          IF (old_cost_tab.EXISTS(1)) THEN
9109            merge_costs(old_cost_tab,
9110                        transaction_row.trans_qty, /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9111                        old_cost.onhand_qty, /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9112                        'A'
9113                       );
9114          ELSE
9115            -- If we are hear then this is a new transaction
9116            process_creation;
9117            RETURN;
9118          END IF;
9119        END IF;
9120         --
9121        IF (new_cost_tab.EXISTS(1)) THEN   -- jboppana
9122          l_new_cost := new_cost.unit_cost;
9123          create_cost_header
9124           (p_item_id               => transaction_row.inventory_item_id
9125            ,p_lot_number           => transaction_row.lot_number
9126            ,p_orgn_id              => transaction_row.orgn_id
9127            ,p_cost_type_id         => l_cost_type_id
9128            ,p_unit_cost            => l_new_cost
9129            ,p_cost_date            => transaction_row.trans_date
9130            ,p_onhand_qty           => transaction_row.trans_qty + NVL(old_cost.onhand_qty,0) /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9131            ,p_trx_src_type_id      => transaction_row.transaction_source_type_id
9132            ,p_txn_act_id           => transaction_row.transaction_action_id
9133            ,p_doc_id               => transaction_row.doc_id
9134            ,x_header_id            => new_cost.header_id
9135            ,x_unit_cost            => new_cost.unit_cost
9136            ,x_onhand_qty           => new_cost.onhand_qty
9137            ,x_return_status        => l_return_status
9138           );
9139 
9140           FOR i IN 1..new_cost_tab.COUNT
9141           LOOP
9142             create_cost_detail
9143                  ( new_cost.header_id
9144                   ,new_cost_tab(i).cost_cmpntcls_id
9145                   , new_cost_tab(i).cost_analysis_code
9146                   ,0
9147                   , new_cost_tab(i).component_cost
9148                   ,0
9149                   , l_return_status
9150                  );
9151           END LOOP;
9152           IF l_return_status = 'S' THEN
9153              create_material_transaction
9154             ( new_cost.header_id
9155              ,l_cost_type_id
9156              ,transaction_row.trans_date
9157               ,transaction_row.trans_qty
9158               ,transaction_row.trans_um
9159               ,transaction_row.trans_qty * nvl(new_cost.unit_cost,0) /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9160               ,transaction_row.transaction_id
9161               ,nvl(new_cost.unit_cost,0) /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9162               ,transaction_row.trans_qty
9163               ,NULL
9164               ,NULL
9165               ,NULL
9166               ,transaction_row.lot_number
9167               , l_return_status
9168              );
9169           END IF;
9170         END IF;
9171       END IF;
9172     IF l_debug_level >= l_debug_level_medium THEN
9173        fnd_file.put_line
9174        (fnd_file.log,'Leaving Procedure: '||procedure_name);
9175      END IF;
9176   END process_lot_split;
9177 
9178   /* INVCONV sschinch New procedure*/
9179 
9180   /*=========================================================
9181     PROCEDURE : Process_lot_merge
9182     DESCRIPTION
9183       This procedure processes lot merge transactions for costing.
9184     AUTHOR : Sukarna Reddy Jun 2005 INVCONV
9185    ==========================================================*/
9186 
9187   PROCEDURE process_lot_merge IS
9188 
9189   CURSOR child_lots_cursor IS
9190   SELECT mtln.lot_number,
9191          ABS(mtln.primary_quantity),
9192          mtln.transaction_date
9193     FROM mtl_transaction_lot_numbers mtln, mtl_material_transactions mmt
9194    WHERE mmt.transfer_transaction_id = transaction_row.transfer_transaction_id
9195         AND  mmt.transaction_id <> transaction_row.transfer_transaction_id
9196         AND  mmt.transaction_id = mtln.transaction_id  ;
9197 
9198 
9199   l_unit_cost  NUMBER;
9200   l_total_cost NUMBER;
9201   l_cost_tab   l_cost_tab_type;
9202   l_old_cost   gmf_lot_costs%rowtype;
9203   l_new_cost_tab l_cost_tab_type;
9204   procedure_name VARCHAR2(100);
9205 
9206 
9207 
9208   BEGIN
9209     procedure_name := 'Process Merged Lot';
9210    IF l_debug_level >= l_debug_level_medium THEN
9211     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
9212    END IF;
9213 
9214     IF (transaction_row.transaction_id = transaction_row.transfer_transaction_id)
9215     THEN
9216       -- This is a merged lot and there may be or may not be any prior cost for this lot
9217       -- Get the cost from child lots which are the sources of costs for this transaction
9218       -- and perform weighted average. We need to get the child lots first.
9219         OPEN child_lots_cursor;
9220         FETCH child_lots_cursor INTO child_lot_row;
9221         WHILE child_lots_cursor%FOUND
9222         LOOP
9223          OPEN lot_cost_cursor(transaction_row.orgn_id,
9224                               transaction_row.inventory_item_id,
9225                               child_lot_row.lot_number,
9226                               child_lot_row.trans_date,
9227                               l_cost_type_id);
9228          FETCH lot_cost_cursor INTO l_old_cost;
9229 
9230          OPEN  lot_cost_detail_cursor ( l_old_cost.header_id );
9231          FETCH lot_cost_detail_cursor BULK COLLECT INTO l_cost_tab;
9232          CLOSE lot_cost_detail_cursor;
9233 
9234          IF (l_cost_tab.EXISTS(1)) THEN
9235            perform_weighted_average(l_cost_tab,
9236                                     child_lot_row.trans_qty,
9237                                     transaction_row.trans_qty
9238                                     );
9239          ELSE
9240             fnd_file.put_line
9241                 (fnd_File.LOG,'PROCEDURE Process_Lot_Merge '||'Warning : Child Lot :'||child_lot_row.lot_number||' has no cost will be using 0 cost ');
9242          END IF;
9243 
9244          CLOSE lot_cost_cursor;
9245          FETCH child_lots_cursor INTO child_lot_row;
9246        END LOOP;
9247        CLOSE child_lots_cursor;
9248 
9249        IF (old_cost_tab.EXISTS(1)) THEN
9250           perform_weighted_average(old_cost_tab,
9251                                    old_cost.onhand_qty,
9252                                    transaction_row.trans_qty
9253                                    );
9254           merge_costs(old_cost_tab,
9255                       0,
9256                       0,
9257                      'C'
9258                      );
9259       END IF;
9260 
9261        get_new_cost(p_cost_tab => new_cost_tab,
9262                     x_new_cost_tab => l_new_cost_tab,
9263                     x_total_cost   => l_unit_cost
9264                    );
9265        new_cost_tab := l_new_cost_tab;
9266        lot_unit_cost := l_unit_cost;
9267 
9268       IF (new_cost_tab.EXISTS(1)) THEN
9269         create_cost_header
9270           ( transaction_row.inventory_item_id
9271           , transaction_row.lot_number
9272           , transaction_row.orgn_id
9273           , l_cost_type_id
9274           , lot_unit_cost
9275           , transaction_row.trans_date
9276           , transaction_row.trans_qty
9277           , transaction_row.doc_id
9278           ,transaction_row.transaction_source_type_id
9279           ,transaction_row.transaction_action_id
9280           , new_cost.header_id
9281           , new_cost.unit_cost
9282           , new_cost.onhand_qty
9283           , l_return_status
9284          );
9285 
9286       IF l_return_status = 'S' THEN
9287 
9288         FOR i in 1 .. new_cost_tab.COUNT
9289         LOOP
9290           create_cost_detail
9291           ( new_cost.header_id
9292           , new_cost_tab(i).cost_cmpntcls_id
9293           , new_cost_tab(i).cost_analysis_code
9294           , 0
9295           , new_cost_tab(i).component_cost
9296           , 0
9297           , l_return_status
9298           );
9299 
9300           IF l_return_status <> 'S' THEN
9301             RETURN;
9302           END IF;
9303         END LOOP;
9304 
9305         create_material_transaction
9306             ( new_cost.header_id
9307             , l_cost_type_id
9308             , transaction_row.trans_date
9309             , transaction_row.trans_qty
9310             , transaction_row.trans_um
9311             , new_cost.onhand_qty * new_cost.unit_cost
9312             , transaction_row.transaction_id
9313             , new_cost.unit_cost
9314             , new_cost.onhand_qty
9315             , NULL
9316             , NULL
9317             , NULL
9318             ,transaction_row.lot_number
9319             , l_return_status
9320             );
9321       END IF; /* l_return_status = 'S' */
9322      END IF;
9323     ELSE
9324       -- This is a child lot and consumed lot. Consider this as an adjustment.
9325       process_adjustment;
9326     END IF;
9327     IF l_debug_level >= l_debug_level_medium THEN
9328        fnd_file.put_line(fnd_file.log,'Leaving Procedure: '||procedure_name);
9329      END IF;
9330 EXCEPTION
9331     WHEN OTHERS THEN
9332       fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
9333 END process_lot_merge;
9334   /*=========================================================
9335     PROCEDURE : Process_lot_translate
9336     DESCRIPTION
9337       This procedure processes lot translate transaction.
9338     AUTHOR : Sukarna Reddy june 2005
9339    ==========================================================*/
9340 
9341 
9342   PROCEDURE process_lot_translate IS
9343     l_old_cost  gmf_lot_costs%rowtype;
9344     l_cost_tab  l_cost_tab_type;
9345     l_lot_number  VARCHAR2(80);
9346     l_trans_date  DATE;
9347     i PLS_INTEGER;
9348     l_new_cost NUMBER;
9349     procedure_name VARCHAR2(100);
9350   BEGIN
9351     procedure_name := 'Process Lot Translate';
9352    IF l_debug_level >= l_debug_level_medium THEN
9353     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
9354    END IF;
9355 
9356     IF (transaction_row.transaction_id = transaction_row.transfer_transaction_id) THEN
9357       process_adjustment;
9358     ELSE
9359       SELECT    lot_number,
9360                 transaction_date
9361                INTO l_lot_number,
9362                     l_trans_date
9363       FROM   mtl_transaction_lot_numbers
9364       WHERE transaction_id = transaction_row.transfer_transaction_id;
9365 
9366       IF (old_cost_tab.EXISTS(1)) THEN
9367         merge_costs(old_cost_tab,
9368               old_cost.onhand_qty,
9369               transaction_row.trans_qty,
9370               'C'
9371              );
9372       END IF;
9373 
9374        -- Get the parent lot info to retrive cost
9375 
9376       OPEN lot_cost_cursor(transaction_row.orgn_id,
9377                            transaction_row.inventory_item_id,
9378                            l_lot_number,
9379                            l_trans_date,
9380                            l_cost_type_id);
9381       FETCH lot_cost_cursor INTO l_old_cost;
9382       CLOSE lot_cost_cursor;
9383       IF (l_old_cost.header_id > 0) THEN
9384         OPEN  lot_cost_detail_cursor ( l_old_cost.header_id);
9385         FETCH lot_cost_detail_cursor BULK COLLECT INTO l_cost_tab;
9386         CLOSE lot_cost_detail_cursor;
9387       END IF;
9388 
9389       IF (l_cost_tab.EXISTS(1)) THEN
9390         IF (old_cost_tab.EXISTS(1)) THEN
9391           merge_costs(l_cost_tab,
9392                 transaction_row.trans_qty, /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9393                 old_cost.onhand_qty, /* ANTHIYAG Bug#5412410 26-Jul-2006 */
9394                'A'
9395                );
9396         ELSE
9397           merge_costs(l_cost_tab,
9398                 transaction_row.trans_qty,
9399                 0,
9400                'A'
9401                );
9402         END IF;
9403       ELSE
9404         IF (NOT new_cost_tab.EXISTS(1)) THEN
9405           -- There is not cost for this lot
9406           --Treat this as creation of inventory
9407           process_creation;
9408           RETURN;
9409         END IF;
9410       END IF;
9411 
9412       l_new_cost := new_cost.unit_cost;
9413 
9414       IF (new_cost_tab.EXISTS(1)) THEN
9415         create_cost_header
9416           ( transaction_row.inventory_item_id
9417           , transaction_row.lot_number
9418           , transaction_row.orgn_id
9419           , l_cost_type_id
9420           , l_new_cost
9421           , transaction_row.trans_date
9422           , transaction_row.trans_qty + NVL(old_cost.onhand_qty,0)
9423           , transaction_row.doc_id
9424           ,transaction_row.transaction_source_type_id
9425           ,transaction_row.transaction_action_id
9426           , new_cost.header_id
9427           , new_cost.unit_cost
9428           , new_cost.onhand_qty
9429           , l_return_status
9430          );
9431 
9432        IF (l_return_status = 'S') THEN
9433          FOR i in 1 .. new_cost_tab.COUNT
9434          LOOP
9435            create_cost_detail
9436                ( new_cost.header_id
9437                , new_cost_tab(i).cost_cmpntcls_id
9438                , new_cost_tab(i).cost_analysis_code
9439                , 0
9440                , new_cost_tab(i).component_cost
9441                , 0
9442                , l_return_status
9443               );
9444 
9445            IF l_return_status <> 'S' THEN
9446                 RETURN;
9447            END IF;
9448            create_material_transaction
9449               ( new_cost.header_id
9450               , l_cost_type_id
9451               , transaction_row.trans_date
9452               , transaction_row.trans_qty
9453               , transaction_row.trans_um
9454               , new_cost.onhand_qty * new_cost.unit_cost
9455               , transaction_row.transaction_id
9456               , new_cost.unit_cost
9457               , new_cost.onhand_qty
9458               , NULL
9459               , NULL
9460               , NULL
9461               ,transaction_row.lot_number
9462               , l_return_status
9463               );
9464         END LOOP;
9465       END IF;
9466     END IF;
9467     END IF;
9468     IF l_debug_level >= l_debug_level_medium THEN
9469        fnd_file.put_line
9470        (fnd_file.log,'Leaving Procedure: '||procedure_name);
9471      END IF;
9472 EXCEPTION
9473     WHEN OTHERS THEN
9474       fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
9475 END process_lot_translate;
9476 
9477 
9478   /*=========================================================
9479   PROCEDURE : process_consigned_inventory
9480 
9481   DESCRIPTION
9482     This procedure will process consigned inventory transactions
9483   AUTHOR : Sukarna Reddy  INVCONV June 2005
9484  ==========================================================*/
9485 
9486  PROCEDURE process_consigned_inventory IS
9487    l_ccc_id NUMBER;
9488    --Bug 14749657
9489    --Expand l_a_code size from VARCHAR2(4) to CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE for POSCO fileds widen project
9490    l_a_code CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE;
9491    l_new_cost   NUMBER;
9492    procedure_name VARCHAR2(100);
9493  BEGIN
9494      procedure_name := 'Process Consigned Inventory';
9495      IF l_debug_level >= l_debug_level_medium THEN
9496        fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
9497      END IF;
9498 
9499      OPEN component_class_cursor
9500     ( l_le_id
9501      ,transaction_row.inventory_item_id
9502      ,transaction_row.orgn_id
9503      ,transaction_row.trans_date
9504     );
9505     FETCH component_class_cursor INTO l_ccc_id,l_a_code,dummy;
9506     CLOSE component_class_cursor;
9507 
9508      IF (l_ccc_id IS NOT NULL) THEN
9509        new_cost_tab(1):= SYSTEM.gmf_cost_type(l_ccc_id,l_a_code,0,transaction_row.transaction_cost,0);
9510        new_cost.unit_cost := transaction_row.transaction_cost;
9511      ELSE
9512        RETURN;
9513      END IF;
9514 
9515      IF (old_cost_tab.EXISTS(1)) THEN
9516        merge_costs(old_cost_tab,
9517                    old_cost.onhand_qty,
9518                    transaction_row.trans_qty,
9519                    'A'
9520                    );
9521      END IF;
9522      IF (new_cost_tab.EXISTS(1)) THEN
9523        l_new_cost := new_cost.unit_cost;
9524         create_cost_header
9525           ( transaction_row.inventory_item_id
9526           , transaction_row.lot_number
9527           , transaction_row.orgn_id
9528           , l_cost_type_id
9529           , l_new_cost
9530           , transaction_row.trans_date
9531           , transaction_row.trans_qty + NVL(old_cost.onhand_qty,0)
9532           , transaction_row.doc_id
9533           ,transaction_row.transaction_source_type_id
9534           ,transaction_row.transaction_action_id
9535           , new_cost.header_id
9536           , new_cost.unit_cost
9537           , new_cost.onhand_qty
9538           , l_return_status
9539          );
9540 
9541        IF (l_return_status = 'S') THEN
9542          FOR i in 1 .. new_cost_tab.COUNT
9543          LOOP
9544            create_cost_detail
9545                ( new_cost.header_id
9546                , new_cost_tab(i).cost_cmpntcls_id
9547                , new_cost_tab(i).cost_analysis_code
9548                , 0
9549                , new_cost_tab(i).component_cost
9550                , 0
9551                , l_return_status
9552               );
9553 
9554            IF l_return_status <> 'S' THEN
9555                 RETURN;
9556            END IF;
9557            create_material_transaction
9558               ( new_cost.header_id
9559               , l_cost_type_id
9560               , transaction_row.trans_date
9561               , transaction_row.trans_qty
9562               , transaction_row.trans_um
9563               , new_cost.onhand_qty * new_cost.unit_cost
9564               , transaction_row.transaction_id
9565               , new_cost.unit_cost
9566               , new_cost.onhand_qty
9567               , NULL
9568               , NULL
9569               , NULL
9570               ,transaction_row.lot_number
9571               , l_return_status
9572               );
9573         END LOOP;
9574       END IF;
9575     END IF;
9576 
9577      IF l_debug_level >= l_debug_level_medium THEN
9578        fnd_file.put_line
9579        (fnd_file.log,'Leaving Procedure: '||procedure_name);
9580      END IF;
9581 EXCEPTION
9582     WHEN OTHERS THEN
9583       fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
9584 END process_consigned_inventory;
9585 
9586  /*=========================================================
9587   PROCEDURE : process_pdtxf_cost
9588 
9589   DESCRIPTION
9590     This procedure will process process discrete transfers
9591   AUTHOR : Sukarna Reddy  INVCONV June 2005
9592  ==========================================================*/
9593 
9594   PROCEDURE process_pdtxf_cost IS
9595     l_cost_tab l_cost_tab_type;
9596     x_total_cost NUMBER;
9597     l_new_cost NUMBER;
9598     l_trp_cost NUMBER;
9599     l_txf_price NUMBER;
9600     l_ccc_id NUMBER;
9601     --Bug 14749657
9602     --Expand l_a_code size from VARCHAR2(4) to CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE for POSCO fileds widen project
9603     l_a_code CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE;
9604     l_trans_id NUMBER;
9605     l_header_id NUMBER;
9606     l_cost_count  NUMBER := 0;  -- PK Bug 9669214
9607 
9608     l_cmpntcls_id NUMBER;
9609     l_cost_analysis_code CM_ALYS_MST.COST_ANALYSIS_CODE%TYPE; -- bug14480540
9610     l_cost_level         NUMBER(1);
9611     l_component_cost     NUMBER;
9612     l_burden_ind         NUMBER(1);
9613     procedure_name VARCHAR2(100);
9614 
9615     CURSOR cur_get_default_cmpt(p_le_id NUMBER,
9616                                 p_trp_cost  NUMBER) IS
9617             SELECT default_ovh_cmpntcls_id,
9618                                 default_ovh_analysis_code,
9619                                 0,
9620                                 p_trp_cost,
9621                                 1
9622                            FROM  gmf_fiscal_policies
9623             WHERE legal_entity_id = p_le_id  ;
9624   BEGIN
9625 
9626     procedure_name := 'process_pdtxf_cost';
9627    IF l_debug_level >= l_debug_level_medium THEN
9628     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
9629    END IF;
9630 
9631     OPEN  component_class_cursor(l_le_id,
9632                                  transaction_row.inventory_item_id,
9633                                  transaction_row.orgn_id,
9634                                  transaction_row.trans_date);
9635     FETCH component_class_cursor INTO l_ccc_id,l_a_code,dummy;
9636     CLOSE component_class_cursor;
9637 
9638     IF(transaction_row.trans_qty <> 0) THEN
9639        l_new_cost := transaction_row.transfer_price;
9640        l_trp_cost := transaction_row.transportation_cost/transaction_row.trans_qty;
9641     ELSE
9642        fnd_file.put_line
9643                (fnd_file.log,'ERROR Procedure : '||procedure_name||' Transaction qty is zero.Cannot proceed');
9644        l_return_status := 'E';
9645         RETURN;
9646      END IF;
9647 
9648     IF (old_cost_tab.EXISTS(1)) THEN
9649       IF (l_ccc_id IS NOT NULL) THEN
9650         l_cost_tab(1) := SYSTEM.gmf_cost_type(l_ccc_id,l_a_code,0,l_new_cost,0);
9651       ELSE
9652         l_return_status := 'E';
9653         -- PK added this message as above
9654         fnd_file.put_line
9655           (fnd_file.log,'ERROR Procedure : '||procedure_name||' l_ccc_id has value NULL. Cannot proceed');
9656 
9657         RETURN;
9658       END IF;
9659 
9660     IF( l_trp_cost <> 0) AND nvl(transaction_row.fob_point,-1) = 1 THEN /* ANTHIYAG Bug#5550911 21-Sep-2006 */
9661        OPEN cur_get_default_cmpt(l_le_id, l_trp_cost);
9662        FETCH cur_get_default_cmpt  INTO l_cmpntcls_id,l_cost_analysis_code,l_cost_level,l_component_cost,l_burden_ind;
9663        CLOSE cur_get_default_cmpt;
9664 
9665        IF(l_cmpntcls_id IS NULL OR l_cost_analysis_code IS NULL) THEN
9666            fnd_file.put_line (fnd_file.log,'Procedure : '||procedure_name||' Overhead default component/analysis code in fiscal policies table is not defined. Ignoring the transportation cost');
9667        ELSE
9668           l_cost_tab(2) := SYSTEM.gmf_cost_type(l_cmpntcls_id,l_cost_analysis_code,l_cost_level,l_component_cost,l_burden_ind);
9669        END IF;
9670     END IF;
9671 
9672       l_onhand_qty := old_cost.onhand_qty + transaction_row.trans_qty;
9673 
9674       merge_costs(l_cost_tab,
9675                   0,
9676                   0,
9677                  'C'
9678                  );
9679 
9680       merge_costs(old_cost_tab,
9681                   old_cost.onhand_qty,
9682                   transaction_row.trans_qty,
9683                   'A'
9684                  );
9685       l_new_cost := new_cost.unit_cost;
9686     ELSE
9687       -- This is a new lot with new cost in the target organization
9688       IF (l_ccc_id IS NOT NULL) THEN
9689         l_onhand_qty := transaction_row.trans_qty;
9690         new_cost_tab(1) := SYSTEM.gmf_cost_type(l_ccc_id,l_a_code,0,l_new_cost,0);
9691       ELSE
9692         l_return_status := 'E';
9693         fnd_file.put_line
9694           (fnd_file.log,'ERROR Procedure : '||procedure_name||' loc2 l_ccc_id has value NULL. Cannot proceed');
9695         RETURN;
9696       END IF;
9697     END IF;
9698 
9699 
9700 
9701     create_cost_header
9702             ( transaction_row.inventory_item_id
9703             , transaction_row.lot_number
9704             , transaction_row.orgn_id
9705             , l_cost_type_id
9706             , l_new_cost
9707             , transaction_row.trans_date
9708             , l_onhand_qty
9709             , transaction_row.doc_id
9710             , transaction_row.transaction_source_type_id
9711             , transaction_row.transaction_action_id
9712             , new_cost.header_id
9713             , new_cost.unit_cost
9714             , new_cost.onhand_qty
9715             , l_return_status
9716             );
9717     IF (l_return_status = 'S') THEN
9718       FOR i IN 1..new_cost_tab.COUNT
9719       LOOP
9720         create_cost_detail
9721                    ( new_cost.header_id
9722                    , new_cost_tab(i).cost_cmpntcls_id
9723                    , new_cost_tab(i).cost_analysis_code
9724                    , 0
9725                    , new_cost_tab(i).component_cost
9726                    , 0
9727                    , l_return_status
9728                    );
9729         IF l_return_status <> 'S' THEN
9730           RETURN;
9731         END IF;
9732       END LOOP;
9733 
9734       -- create data with original transaction cost.
9735       -- PK Bug 9669214 Added this IF.
9736 
9737        IF (l_cost_tab.EXISTS(1)) THEN
9738          l_cost_count := l_cost_tab.COUNT;
9739        ELSE
9740          l_cost_count := 0;
9741        END IF;
9742 
9743      -- PK Bug 9669214 added IF else  endif statement main changes in ELSE IF is old code.
9744 
9745      IF (l_cost_count >= 1) THEN
9746 
9747       FOR i IN 1..l_cost_tab.COUNT
9748       LOOP
9749         create_cost_detail
9750                    ( -new_cost.header_id
9751                     , l_cost_tab(i).cost_cmpntcls_id
9752                     , l_cost_tab(i).cost_analysis_code
9753                     , 0
9754                     , l_cost_tab(i).component_cost
9755                     , l_cost_tab(i).burden_ind
9756                     , l_return_status
9757                    );
9758         IF l_return_status <> 'S' THEN
9759           RETURN;
9760         END IF;
9761       END LOOP;
9762      -- Bug PK Bug 9669214 If l_cost_tab has no records copy positive headers as negative.
9763      ELSE
9764 
9765       FOR i IN 1..new_cost_tab.COUNT
9766       LOOP
9767         create_cost_detail
9768                    ( -new_cost.header_id
9769                    , new_cost_tab(i).cost_cmpntcls_id
9770                    , new_cost_tab(i).cost_analysis_code
9771                    , 0
9772                    , new_cost_tab(i).component_cost
9773                    , 0
9774                    , l_return_status
9775                    );
9776         IF l_return_status <> 'S' THEN
9777           RETURN;
9778         END IF;
9779       END LOOP;
9780 
9781      END IF;  -- Bug PK Bug 9669214
9782 
9783       IF l_debug_level >= l_debug_level_medium THEN
9784         fnd_file.put_line
9785                (fnd_file.log,'Procedure : '||procedure_name||' Creating cost transaction');
9786       END IF;
9787 
9788       create_material_transaction
9789               ( new_cost.header_id
9790                ,l_cost_type_id
9791                ,transaction_row.trans_date
9792                ,transaction_row.trans_qty
9793                ,transaction_row.trans_um
9794                ,new_cost.onhand_qty * new_cost.unit_cost
9795                ,transaction_row.transaction_id
9796                ,new_cost.unit_cost
9797                ,new_cost.onhand_qty  /* ANTHIYAG Bug#5550911 21-Sep-2006 */
9798                ,old_cost.unit_cost--  NULL                        --jboppana
9799                ,old_cost.onhand_qty             --NULL jboppana
9800                ,1
9801                ,transaction_row.lot_number
9802                ,l_return_status
9803               );
9804     END IF;
9805     IF l_debug_level >= l_debug_level_medium THEN
9806        fnd_file.put_line(fnd_file.log,'Leaving Procedure: '||procedure_name);
9807     END IF;
9808 EXCEPTION
9809     WHEN OTHERS THEN
9810       fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
9811 END process_pdtxf_cost;
9812 
9813 
9814 
9815 /* Process process discrete transfers */
9816 
9817 /*=========================================================
9818   PROCEDURE : process_pd_transfers
9819 
9820   DESCRIPTION
9821     This procedure will process - discrete to process transfers
9822   AUTHOR : Sukarna Reddy  INVCONV June 2005
9823  ==========================================================*/
9824 
9825    PROCEDURE process_pd_transfer IS
9826      l_header_id NUMBER;
9827      l_trans_id NUMBER;
9828      procedure_name VARCHAR2(100);
9829    BEGIN
9830    procedure_name := 'Process Pd Transfer';
9831    IF l_debug_level >= l_debug_level_medium THEN
9832     fnd_file.put_line(fnd_file.log,'Entered Procedure: '||procedure_name);
9833    END IF;
9834 
9835      IF new_cost_tab.EXISTS(1) THEN
9836        new_cost_tab.delete;
9837      END IF;
9838 
9839      IF (( transaction_row.fob_point = FOB_SHIPPING AND
9840             transaction_row.transaction_action_id  = LOGICAL_INTRANSIT_RECEIPT
9841            )
9842            OR
9843           (transaction_row.fob_point = FOB_RECEIVING AND
9844              transaction_row.transaction_action_id  = INTRANSIT_RECEIPT
9845              )
9846            OR
9847            (
9848              transaction_row.transaction_action_id = DIRECT_ORG_TRANSFER
9849            )
9850          )
9851      THEN
9852        process_pdtxf_cost;
9853      ELSIF (transaction_row.fob_point = FOB_SHIPPING AND /*jboppana changed source_type_id to fob_point*/
9854             transaction_row.transaction_action_id = INTRANSIT_RECEIPT
9855            )
9856      THEN
9857        -- This transaction is a physical receipt and should not be costed since there is a
9858        -- Logical intransit receipt which was created earlier and costed. We need to point the cost to previous
9859        -- logical intransit receipt.
9860 
9861        SELECT transaction_id
9862          INTO l_trans_id
9863          FROM  mtl_material_transactions
9864         WHERE  transfer_transaction_id = transaction_row.transfer_transaction_id
9865          AND transaction_action_id = 15;
9866 
9867        SELECT cost_header_id
9868           INTO l_header_id
9869           FROM gmf_material_lot_cost_txns gmlc
9870          WHERE  transaction_id = l_trans_id
9871            AND lot_number = transaction_row.lot_number
9872            AND cost_type_id = l_cost_type_id;
9873 
9874        SELECT * INTO new_cost
9875           FROM  gmf_lot_costs
9876          WHERE header_id = l_header_id;
9877 
9878        create_material_transaction
9879          ( p_header_id      => l_header_id
9880           ,p_cost_type_id   => l_cost_type_id
9881           ,p_trans_date     => transaction_row.trans_date
9882           ,p_trans_qty      => transaction_row.trans_qty
9883           ,p_trans_um       => transaction_row.trans_um
9884           ,p_total_cost     => transaction_row.trans_qty * new_cost.unit_cost
9885           ,p_trans_id       => transaction_row.transaction_id
9886           ,p_unit_cost      => new_cost.unit_cost
9887           ,p_onhand_qty     => old_cost.onhand_qty
9888           ,p_old_unit_cost  => NULL
9889           ,p_old_onhand_qty => NULL
9890           ,p_new_cost_ind   => 1
9891           ,p_lot_number     => transaction_row.lot_number
9892           ,x_return_status  => l_return_status
9893          );
9894      END IF;
9895       IF l_debug_level >= l_debug_level_medium THEN
9896        fnd_file.put_line(fnd_file.log,'Leaving Procedure: '||procedure_name);
9897      END IF;
9898 EXCEPTION
9899   WHEN OTHERS THEN
9900     fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
9901     l_return_status := 'E';
9902 END process_pd_transfer;
9903 
9904 
9905 --*************************************************************************************
9906 --*                                                                                   *
9907 --* This is the main procedure for the rollup, and is the only one that can be called *
9908 --* globally. It is written as a concurrent request.                                  *
9909 --*                                                                                   *
9910 --* The mandatory parameters are:                                                     *
9911 --*                                                                                   *
9912 --* p_co_code               This is used to locate costs for items that are not       *
9913 --*                         costed by lot and also to locate resource costs           *
9914 --*                                                                                   *
9915 --* p_cost_method_code      This is also used to locate item and resource costs       *
9916 --*                                                                                   *
9917 --* p_user                  This is used to tag any rows inserted or updated with     *
9918 --*                         the identity of the user (obsoleted)                      *
9919 --*                                                                                   *
9920 --* The optional parameters are:                                                      *
9921 --*                                                                                   *
9922 --* p_item_no               If specified, only this item's uncosted lots will be      *
9923 --*                         costed                                                    *
9924 --*                                                                                   *
9925 --* p_lot_no                If specified, only this the lot number belonging to the   *
9926 --*                         item specified will be costed. Note: This does not imply  *
9927 --*                         that a range of lots sharing this lot_no will be costed.  *
9928 --*                         If the lot_no exists for the item only that lot will be   *
9929 --*                         processed.                                                *
9930 --*                                                                                   *
9931 --* p_sublot_no             If specified only the lot identified by the concatenation *
9932 --*                         of p_item_no/p_lot_no/p_sublot_no will be costed          *
9933 --*                                                                                   *
9934 --* p_final_run_flag        Indicates this is a final or trial run.                   *
9935 --*                         Valid values are Y and N.                                 *
9936 --*                 								      *
9937 --* HISTORY                                                                           *
9938 --*   19-Aug-2004 Dinesh Vadivel Bug# 3831782                                         *
9939 --*        Added where clause in the queries to ignore the Lot Cost Adjustment Records*
9940 --*        which has no Detail Records(i.e., NULL Adjustment Cost)                    *
9941 --*
9942 --*    26-Nov-2004 Uday Moogala Bug# 4004338
9943 --*       Added Reverse_id in the select clause of INV_TRAN_CURSOR.
9944 --*       Also added new DECODE(...) order by clause in the INV_TRAN_CURSOR.
9945 --*
9946 --*   27-Nov-2004 Dinesh Vadivel Bug# 4004338
9947 --*      Modified the if condition to set the lot_costed_ind if it is final_run to the
9948 --*      reversal records of the product..
9949 --*      Eg : If we records as 100, -100 and 150 then during Final run we are setting
9950 --*              lot_costed_ind to 1 only for the rows 100 and 150.
9951 --*              So modified the code here to set for the -100 transaction record also.
9952 --*
9953 --*   30-Jan-2005 Dinesh Vadivel Bug# 4152397
9954 --*      Uncommented the code which fetches lot_id into l_lot_id for the lot_no
9955 --*      entered on Lot ACP screen.
9956 --*      Also, the inv_tran_cursor inside "ELSIF l_item_id IS NOT NULL .... "
9957 --*      we have filter only those LADJ transactions for that particular Lot_id if any.
9958 --*
9959 --*  24-Feb-2005 Dinesh Vadivel Bug# 4177349
9960 --*      When a batch is reversed to WIP, the Lot Cost process fails at process_reversals
9961 --*      So added check to verify the batch status before calling process_reversals
9962 --*
9963 --*   24-Feb-2005 Dinesh Vadivel Bug# 4187891
9964 --*      Cancellation of Inv Xfer has been modified such that it is considered
9965 --*      as if it is an actual transfer where the source and destination organizations are the same.
9966 --*
9967 --*   24-Feb-2005 Dinesh Vadivel Bug# 4176690
9968 --*       Added a new Date field in the Lot Cost Process Submission Screen.
9969 --*       Lot Cost Process will consider only those transactions upto this date
9970 --*
9971 --*   11-Aug-2006 Anand Thiyagarajan Bug#5460458
9972 --*       Modified code to ignore the PO receipt transactions for consigned items unless received
9973 --*       by Transfer to Regular Options. Also Modified Code to consider the Consigned inventory transactions
9974 --*   18-dec-2006 bug 5705311, Modified the select query to fetch category_id for the cost class, pmarada
9975 --*   30-Jun-2008 Bug 7215069
9976 --*      Change ordering for Receipt transactions. If transactions exist for multiple
9977 --*      document types with same transaction date then first process the receipt transactions.
9978 --*	21-May-2009 Bug 7249505
9979 --*	HALUTHRA BUG 7249505 In case of return to vendor process the transaction as an adjustment
9980 --*     27-May-2009 Bug 5473138/8533290
9981 --*	HALUTHRA Bug 5473138/8533290 Modified select statement to cover the case for phantom batches.
9982 --*     03-Aug-2009 LCMOPM dev, bug 8642337, LCM-OPM Integration, Added one more query to load LC
9983 --*                 adjustments invoking process_lc_adjustments to process LC adjustment transactions
9984 --*     12-Aug-2011 Bug12866252 added l_le_id condition in the lcm-opm transactions loading query
9985 --*************************************************************************************
9986 
9987 PROCEDURE rollup_lot_costs
9988 ( errbuf            OUT NOCOPY VARCHAR2
9989 , retcode           OUT NOCOPY VARCHAR2
9990 , p_le_id   	       IN NUMBER
9991 , p_cost_type_id     IN NUMBER
9992 , p_final_run_flag   IN VARCHAR2
9993 , p_structure_id     IN NUMBER
9994 , p_category_id     IN NUMBER
9995 , p_orgn_id          IN NUMBER
9996 , p_item_id        IN NUMBER
9997 , p_lot_no           IN VARCHAR2
9998 , p_final_run_date IN VARCHAR2
9999 )
10000 IS
10001   inv_tran_cursor      inv_tran_cursor_type;
10002 
10003   l_return_code        VARCHAR2(1);
10004   l_trans_date         DATE;
10005   x_return_status      NUMBER(1) := 0;
10006   l_source_orgn_id     NUMBER;
10007   l_source_le_id       NUMBER;
10008   procedure_name       VARCHAR2(100);
10009   r_return_status      NUMBER:= 0;
10010   l_code_version       VARCHAR2(2000);
10011   /* Bug  9894310 */
10012   new_uncostable_cnt   INTEGER := 0;
10013   prev_uncostable_cnt  INTEGER := 0;
10014 
10015 CURSOR file_version IS
10016 SELECT text
10017 FROM user_source
10018 WHERE name = 'GMF_LOT_COSTING_PUB'
10019   AND type = 'PACKAGE BODY'
10020   AND text like '%$Header%'
10021   AND line < 20;
10022 
10023 -- Bug 13038249 Grupo viz changes starts
10024 CURSOR cur_org_coll IS
10025 SELECT /*+ index(hoi,HR_ORGANIZATION_INFORMATIO_FK1) */
10026     mp.organization_id,
10027     mp.organization_code,
10028     mp.process_enabled_flag,
10029     ou.org_information2 le_id,
10030     ou.org_information3 ou_id
10031 FROM mtl_parameters mp,
10032      hr_organization_information ou
10033 WHERE ou.org_information_context  = 'Accounting Information'
10034   AND mp.organization_id          =  ou.organization_id
10035 ORDER BY organization_id  ;
10036 
10037 CURSOR cur_altcost_period IS
10038 SELECT period_id,
10039 TO_CHAR( (TRUNC(start_date) + (just_a_column -1 )),'J') final_date,
10040 TRUNC(start_date) start_date,
10041 TRUNC(end_date) end_date
10042 FROM gmf_period_statuses c,
10043    (SELECT ROWNUM just_a_column
10044          FROM dual
10045          CONNECT BY LEVEL <= 7400 )
10046 WHERE cost_type_id = l_default_cost_type_id
10047   AND legal_entity_id = l_le_id
10048   AND delete_mark = 0
10049   AND DECODE(TO_CHAR(start_date,'DD'),1,1,1) <= just_a_column
10050   AND (TRUNC(end_date)-trunc(start_date)+1)  >= just_a_column
10051   AND start_date >= l_trans_start_date
10052 ORDER BY 2 ;
10053 -- Bug 13038249 Grupo viz changes ends
10054 
10055 BEGIN
10056     /* Moved various initialisations here to avoid GSCC warnings and errors */
10057     procedure_name         := 'Rollup Lot Costs';
10058     l_debug_level          := TO_NUMBER(FND_PROFILE.VALUE( 'GMF_CONC_DEBUG' ));
10059     l_debug_level_none     := 0;
10060     l_debug_level_low      := 1;
10061     l_debug_level_medium   := 2;
10062     l_debug_level_high     := 3;
10063     l_return_code          := NULL; -- Bug 3476508
10064 
10065     l_tmp                  := TRUE;
10066     l_final_run_date       := NVL(fnd_date.canonical_to_date(p_final_run_date),SYSDATE); -- Bug 4176690
10067     l_le_id                := p_le_id;
10068     l_cost_type_id         := p_cost_type_id;
10069     l_cost_category_id     := p_category_id;
10070     l_item_id              := p_item_id;
10071     l_lot_no               := p_lot_no;
10072     l_orgn_id              := p_orgn_id;
10073 
10074     l_cost_alloc_profile := NVL( FND_PROFILE.VALUE('GMF_LOT_COST_ALLOC'), 0);
10075     cost_alloc_profile := l_cost_alloc_profile; -- Bug 13038249 l_cost_alloc_profile may get updated later.
10076 
10077     -- Run Incremental costing based on profile (how many times)
10078     l_incr_iter := NVL( FND_PROFILE.VALUE('GMF_LOTCOST_ITERATION'), 0);
10079 
10080     /*Bug 13808832 - This profile is an internal profile and is not currently shipped, i.e By default WIP batch txns are not processed */
10081     l_process_wip_batch:= NVL( FND_PROFILE.VALUE('GMF_PROCESS_WIP_BATCH_LACP'), 0);
10082     /*Bug 13808832 - end*/
10083 
10084     -- umoogala 05-Dec-2003
10085     IF  p_final_run_flag = 'Y' THEN
10086         l_final_run_flag := 1;
10087     ELSE
10088         l_final_run_flag := 0;
10089     END IF;
10090 
10091     l_login_id      := FND_GLOBAL.LOGIN_ID;
10092     l_prog_appl_id  := FND_GLOBAL.PROG_APPL_ID;
10093     l_program_id    := FND_GLOBAL.CONC_PROGRAM_ID;
10094     l_request_id    := FND_GLOBAL.CONC_REQUEST_ID;
10095     l_user_id       := FND_PROFILE.VALUE('USER_ID');
10096     SELECT user_name INTO l_user
10097     FROM   fnd_user
10098     WHERE  user_id = l_user_id;
10099 
10100     /* INVCONV sschinch Modified for convergence */
10101 
10102       SELECT m1.default_lot_cost_type_id
10103            , m1.trans_start_date
10104            , m1.cost_mthd_code
10105            , m2.cost_mthd_code
10106       INTO   l_default_cost_type_id,
10107              l_trans_start_date,
10108              l_cost_mthd_code,
10109              l_default_cost_mthd
10110       FROM   cm_mthd_mst m1,
10111              cm_mthd_mst m2
10112       WHERE  m1.cost_type_id = l_cost_type_id
10113       AND    m2.cost_type_id = m1.default_lot_cost_type_id;
10114 
10115      /******** Bug  4038722 - Dinesh Vadivel - Start **********/
10116      /*Get the Base Currency for the company. Later if needed check whether the unit cost is in
10117       the base currency and if not, convert it to the base currency */
10118      /* INVCONV sschinch */
10119       -- Bug 13038249
10120       SELECT mtl_analysis_code, mtl_cmpntcls_id, base_currency_code
10121        INTO l_mtl_analysis_code, l_mtl_cmpntcls_id , l_base_ccy_code
10122       FROM  GMF_FISCAL_POLICIES
10123       WHERE legal_entity_id = l_le_id ;
10124 
10125       fnd_file.put_line(fnd_file.log,'Fiscal Policy material component class = '||l_mtl_cmpntcls_id||' Analysis Code '||l_mtl_analysis_code);
10126 
10127       /******** Bug  4038722 - Dinesh Vadivel - End**********/
10128 
10129       -- PK print file version as well
10130       OPEN file_version;
10131       FETCH file_version INTO l_code_version;
10132       CLOSE file_version;
10133 
10134       fnd_file.put_line(fnd_file.log,'Code version '||l_code_version);
10135       fnd_file.put_line(fnd_file.log,'Parameters for this run are:');
10136       fnd_file.put_line(fnd_file.log,'Legal Entity Id    = '||p_le_id);
10137       fnd_file.put_line(fnd_file.log,'Cost Type = '||l_cost_mthd_code);
10138       fnd_file.put_line(fnd_file.log,'Default Cost Type = '||l_default_cost_mthd);
10139       fnd_file.put_line(fnd_file.log,'Rate Type Code   = '||l_rate_type_code);
10140       fnd_file.put_line(fnd_file.log,'Base Currency Code   = '||l_base_ccy_code);
10141       fnd_file.put_line(fnd_file.log,'trans start date = '||l_trans_start_date);
10142       fnd_file.put_line(fnd_file.log,'Item Category Id  = '||p_category_id);
10143       fnd_file.put_line(fnd_file.log,'Item Id      = '||p_item_id);
10144       fnd_file.put_line(fnd_file.log,'Lot Number       = '||p_lot_no);
10145       fnd_file.put_line(fnd_file.log,'Debug Level      = '||to_char(l_debug_level));
10146       fnd_file.put_line(fnd_file.log,'User             = '||l_user);
10147       fnd_file.put_line(fnd_file.log,'Final Date = '||to_char(l_final_run_date,'DD-MON-YYYY HH24:MI:SS'));
10148       fnd_file.put_line(fnd_file.log,'Final Run?       = '|| p_final_run_flag);
10149       fnd_file.put_line(fnd_file.log,'Cost Alloc Factor Profile   = '|| l_cost_alloc_profile);
10150       fnd_file.put_line(fnd_file.log,'GMF: Maximum Number of Iterations for Lot costing = '|| l_incr_iter || ' '|| to_char(sysdate,'DD-MON-YYYY HH24:MI:SS') );
10151 
10152       fnd_file.put_line(fnd_file.log,'l_process_wip_batch = '|| l_process_wip_batch); /*Bug 13808832*/
10153 
10154 -- Bug 13038249 Grupo viz changes start
10155    FOR Cur_org_info IN cur_org_coll
10156    LOOP
10157     org_coll_tab(Cur_org_info.organization_id) := Cur_org_info ;
10158    /*
10159       fnd_file.put_line(fnd_file.log,'organization_id    = '||
10160       org_coll_tab(Cur_org_info.organization_id).organization_id || '---'||
10161             org_coll_tab(Cur_org_info.organization_id).organization_code || '---'||
10162       org_coll_tab(Cur_org_info.organization_id).le_id || '---'||
10163       org_coll_tab(Cur_org_info.organization_id).ou_id  );
10164    */
10165    END LOOP;
10166 
10167    org_coll_size := org_coll_tab.COUNT;
10168 
10169    FOR Cur_altcost_info IN Cur_altcost_period
10170    LOOP
10171    period_tab(Cur_altcost_info.final_date) := Cur_altcost_info ;
10172    /*
10173       fnd_file.put_line(fnd_file.log,'period_id = '||
10174       period_tab(Cur_altcost_info.final_date).period_id || '---'||
10175       period_tab(Cur_altcost_info.final_date).start_date || '---'||
10176       period_tab(Cur_altcost_info.final_date).end_date || '---'||
10177       period_tab(Cur_altcost_info.final_date).final_date  );
10178    */
10179    END LOOP ;
10180 
10181 -- Bug 13038249 Grupo viz changes End
10182 
10183       -- BUG 3476427
10184       -- If this is a final run, and the user has entered limiting parameters then
10185       -- let them know that they will be ignored.
10186 
10187       IF  l_final_run_flag = 1
10188       AND
10189       (   p_category_id IS NOT NULL
10190        OR p_item_id IS NOT NULL
10191        OR p_lot_no IS NOT NULL
10192        OR p_orgn_id IS NOT NULL
10193       )
10194       THEN
10195         fnd_file.put_line
10196         ( fnd_file.log, 'WARNING : Rollup submitted in final mode, limiting parameters will be ignored');
10197 
10198         l_cost_category_id := NULL;
10199         l_item_id := NULL;
10200         l_lot_no := NULL;
10201         l_orgn_id := NULL;
10202         l_tmp := FALSE;
10203       ELSE
10204         IF p_item_id IS NOT NULL AND p_category_id IS NOT NULL THEN
10205           fnd_file.put_line
10206           ( fnd_file.log, 'WARNING : Lot cost process should consider Item ONLY ! IF BOTH Cost Category and item are specified');
10207           l_cost_category_id := NULL;
10208           l_tmp := FALSE;
10209         END IF;
10210 
10211         -- PK Bug 6822310 always make l_orgn_id as NULL.
10212         IF p_orgn_id IS NOT NULL THEN
10213          fnd_file.put_line
10214         ( fnd_file.log, 'WARNING : Lot cost process should consider all lot transactions. Setting Organization_code to NULL');
10215         l_orgn_id := NULL;
10216         l_tmp := FALSE;
10217         END IF;
10218 
10219       END IF;
10220 
10221        -- There are two possibilities for the costing. A lot might be manufactured
10222       -- or bought in. Occasionally a lot might be bought in and later added to by a
10223       -- production batch or vice versa. No matter how the lot is created and/or replenished
10224       -- the sole criterion to decide whether to cost an item's lots using lot costing will
10225       -- be an entry for the item, or its cost_class, in gmf_lot_costed_items
10226 
10227       -- In the case of a lot that is an output of a production batch, we need
10228       -- to explode it and find out what lots went into its manufacture.
10229 
10230       -- If the lot is not from a production batch, then there is no explosion.
10231 
10232       -- In either case, once we have all the transactions that we need, we just have
10233       -- to work through them. The transactions are ordered by date and then by type and
10234       -- if transactions have been back-dated the algorithm will work, but might produce
10235       -- a wrong answer.
10236 
10237       -- For each replenishment we must add a new row to (or update the existing
10238       -- row in) GMF_LOT_COSTS, add new rows to (or update the existing rows in)
10239       -- GMF_LOT_COST_DETAILS and finally create a row in GMF_LOT_COST_TRANSACTIONS
10240       -- that links the inventory transaction to the row in GMF_LOT_COSTS.
10241 
10242       -- For each consumption we leave the GMF_LOT_COSTS and GMF_LOT_COST_DETAILS
10243       -- rows alone and just create the GMF_LOT_COST_TRANSACTIONS row.
10244 
10245       --
10246       -- umoogala 21-Nov-2003
10247       -- The reason for not using BULK COLLECT is, I want the table to be
10248       -- indexed by item_id so that I can easily lookup.
10249 
10250        /* INVCONV sschinch Load Lot costed items and process organizations */
10251            Load_Lot_Costed_Items_gt(p_le_id        => l_le_id,
10252                                    p_orgn_id      => l_orgn_id,
10253                                    p_item_id      => l_item_id,
10254                                    p_category_id  => l_cost_category_id,
10255                                    x_return_status => x_return_status
10256                                   );
10257          IF (x_return_status <> 0) THEN
10258            fnd_file.put_line
10259           (fnd_file.log,'ERROR Procedure : '||procedure_name||' Load_Lot_Costed_Items_gt returned error. Cannot proceed');
10260            l_return_status := 'E';
10261            RETURN;
10262          END IF;
10263 
10264       /* FOR Cur_lc_items IN lot_costed_items (l_co_code, l_cost_method_code) INVCONV sshinch*/
10265       /* sschinch INVCONV indexed by organization id and item id */
10266       FOR Cur_lc_items IN lot_costed_items
10267       LOOP
10268 	      lc_items_tab(Cur_lc_items.inventory_item_id||'-'||cur_lc_items.organization_id) := Cur_lc_items.inventory_item_id;
10269       END LOOP;
10270 
10271       -- umoogala 05-Dec-2003
10272 
10273       fnd_file.put_line(fnd_file.log,'Removing rows of previous trial run: '|| to_char(sysdate,'DD-MON-YYYY HH24:MI:SS') );
10274       delete_lot_costs;
10275 
10276       -- Cursors to retrieve all transactions for items that are costed by lot
10277       -- that have not yet been processed. The above type is used to store the
10278       -- rows that are returned. There are several cursors depending on how the
10279       -- user has launched the rollup:
10280       --
10281       -- A full rollup will retrieve all transactions for all items/cost classes
10282       -- listed in the gmf_lot_costed_items table. Note that this retrieval will not
10283       -- possess a date range.
10284       --
10285       -- An item-specific rollup will only retrieve transactions for the item specified.
10286       -- If the item has a date range in the gmf_lot_costed_items table this will be
10287       -- used. This option also allows a user to specify an optional lot and sublot.
10288       --
10289       -- A cost class-specific rollup will only retrieved transactions for items in the
10290       -- cost class specified. If the cost class possesses a date range in gmf_lot_costed_items
10291       -- this will be used.
10292 
10293 
10294       --
10295       -- umoogala: replaced itemcost_class with cost_category_id and
10296       -- l_cost_class with l_cost_category_id.
10297       -- Also, added lot_ctl = 1 condition
10298       -- to where clause
10299       --
10300 
10301       -- Since the cursors below were written, a new column (co_code) has been added to the gmf_lot_costed_items table
10302       -- with the result that it is now possible to have rows differing only in this column.
10303 
10304       -- Cursors now possess a third branch that is 'union all'd' into the result set to retrieve the lot cost
10305       -- adjustments. These are disguised as transactions of type LADJ.
10306 
10307     -- Open a cursor that retrieves every uncosted lot costed item's transactions
10308 	  --
10309 	  -- umoogala: replaced itemcost_class with cost_category_id
10310 	  --
10311   /* INVCONV sschinch. Removed all queries and recreated a single query to handle all situations */
10312   /* HALUTHRA Bug 8533290/5473138  In Case of phantom batches the ingrediant transactions were being given a higher prefernce when compared to
10313      product transaction. As a result of this , during first iteration since there was no cost for the item , the item was being set
10314      uncostable. Hence on second iteration for product transaction , the transaction was getting skipped since this item had been given
10315      uncostable tag.
10316 
10317      Now modifying the query to take product transaction first for phantom items.Doing this by a small trick
10318      Added two more variables, gme.phantom_type phantom_type and DECODE( NVL(gme.phantom_type,0),1,(mmt.transaction_date + (1/(2*24 * 3600)) ),
10319                                     mmt.transaction_date) phantom_trans_date
10320      Basically we are adding .5 second to the transaction date for ingrediant transaction of the phantom item so that product transaction is picked
10321      up before the ingrediant transaction.
10322      In the order by clause if the item a phantom item , then we order by phantom_trans_date otherwise we order by trans_date
10323 
10324 dbms_output.put_line(' ');     Now once the product transaction gets picked up before the ingrediant transaction for a phantom item, there are still couple more of issues.
10325      In gme_transaction_pairs the transaction_id2 is can be for actual reversal transaction , and also for phantom item there can be a transaction_id2 which
10326      stores the other transaction for item .Eg if for a phantom item LCIMT2 there are two transactions
10327       Transaction 1 having transaction_ID = 887 which is a product transaction
10328       Transaction 2 havign transaction_id = 890 which is an ingrediant transaction.
10329      Now in GTP there will be a transaction_id2 for both the transacions but they have pair_type =2 which will be 1 for pure reversal transactions.
10330      Since we dont check for pair_type we mistake the transaction_id2 in this case as the reverse_id. Therefore changing the clause for reverse_id choice.
10331 
10332 
10333   Making three changes in this query
10334   Change 1 : changed reverse_id from nvl(gtp.transaction_id2,NULL) as reverse_id, to Decode(nvl(gtp.pair_type,0),1,gtp.transaction_id2,NULL) as reverse_id,
10335   Change 2 : Added three more columns to be selected in the select clause
10336 		DECODE( NVL(gme.phantom_type,0),1,(mmt.transaction_date + (1/(2*24 * 3600)) ),
10337                                     mmt.transaction_date) phantom_trans_date
10338 		gme.phantom_type phantom_type
10339 		gtp.pair_type pair_type
10340   Change 3 : Changed the order by clause from  ORDER BY 7,decode(transaction_action_id,27,-1,transaction_source_type_id)... to
10341       ORDER BY decode(phantom_type,1,phantom_trans_date,trans_date),decode(transaction_action_id,27,-1,transaction_source_type_id)...
10342  */
10343 
10344  -- PK Bug 9069363 use in line query for gme_transaction_pairs we do not want to consider pair_type 2 records since such records are created for
10345  -- phantom batches even when there is no reversals causing duplicate selection of data.
10346 
10347 -- Bug 13038249 Grupo viz changes starts
10348 
10349     fnd_file.put_line(fnd_File.LOG,'Reading uncosted transactions: '|| to_char(sysdate,'DD-MON-YYYY HH24:MI:SS') );
10350 
10351 -- Perf B14616815 Inv_tran_cursor query is now split into two one for specific lot and another for all lots.
10352 -- One item all lots would still be part of first query. Works as gmf_lot_costed_items_gt would have one item.
10353 
10354     IF l_lot_no IS NULL THEN
10355 
10356     OPEN inv_tran_cursor FOR
10357       SELECT         a.doc_id,
10358                      a.transaction_source_type_id,
10359                      a.inventory_item_id,
10360                      a.line_id,
10361                      a.line_type,
10362                      a.lot_number,
10363                      a.trans_date,
10364                      a.transaction_id,
10365                      a.trans_qty,
10366                      trans_um,
10367                      orgn_id,
10368                      source,
10369                      reverse_id,
10370                      transaction_action_id,
10371                      transfer_price,
10372                      transportation_cost,
10373                      fob_point,
10374                      transfer_transaction_id,
10375                      transaction_cost,
10376                      transfer_orgn_id,
10377                      phantom_trans_date,
10378                      phantom_type,
10379                      pair_type,
10380                      oc1,
10381                      oc2,
10382                      oc3,
10383                      routing_id,
10384                      DECODE(NVL(lcb.item_id,0),0,0,1) AS burden_ind
10385         FROM   ( SELECT DISTINCT cb.organization_id, cb.inventory_item_id as item_id
10386 		             FROM gmf_lot_cost_burdens cb,
10387                       gmf_process_organizations_gt gpo
10388                 WHERE cb.cost_type_id = l_cost_type_id
10389                   AND cb.organization_id = gpo.organization_id
10390 			         )lcb,
10391 -- Bug 13038249 Grupo viz changes End
10392              (SELECT
10393                      mmt.transaction_source_id  as doc_id,
10394                      mmt.transaction_source_type_id,
10395                      mmt.inventory_item_id,
10396                      mmt.trx_source_line_id as line_id,
10397                      NVL(gme.line_type,0) as line_type,
10398                      mtln.lot_number,
10399                      mmt.transaction_date as trans_date,
10400                      mmt.transaction_id as transaction_id,
10401                      mtln.primary_quantity as trans_qty,
10402                      lcig.primary_uom_code as trans_um,
10403                      mmt.organization_id as orgn_id,
10404                      1 source,
10405                      Decode(nvl(gtp.pair_type,0),1,gtp.transaction_id2,NULL) as reverse_id, --nvl(gtp.transaction_id2,NULL) as reverse_id,
10406                      mmt.transaction_action_id,
10407                      nvl(mmt.transfer_price,0) as transfer_price,
10408                      nvl(mmt.transportation_cost,0) as transportation_cost,
10409                      mmt.fob_point,
10410                      mmt.transfer_transaction_id,
10411                      NVL(mmt.transaction_cost,0) as transaction_cost,
10412                      mmt.transfer_organization_id as transfer_orgn_id,
10413                      DECODE( NVL(gme.phantom_type,0),1,(mmt.transaction_date + (1/(2*24 * 3600)) ),
10414                                     mmt.transaction_date) phantom_trans_date,
10415                      gme.phantom_type phantom_type,
10416                      gtp.pair_type pair_type,
10417                      decode(gme.phantom_type,1,DECODE( NVL(gme.phantom_type,0),1,(mmt.transaction_date + (1/(2*24 * 3600)) ),
10418                                     mmt.transaction_date),mmt.transaction_date) as oc1,
10419                      decode(mmt.transaction_action_id,27,-1,mmt.transaction_source_type_id) as oc2,
10420                      DECODE(gme.line_type,1, DECODE((ABS(DECODE(mtln.primary_quantity, 0, 1,mtln.primary_quantity))/DECODE(mtln.primary_quantity, 0, 1, mtln.primary_quantity)),
10421                                          1, mmt.transaction_id ,
10422                                          DECODE(Decode(nvl(gtp.pair_type,0),1,gtp.transaction_id2,NULL), NULL, mmt.transaction_id, (Decode(nvl(gtp.pair_type,0),1,gtp.transaction_id2,NULL)+.5))),mmt.transaction_id) as oc3,
10423                      NVL(gbh.routing_id, 0) as routing_id
10424                    FROM mtl_material_transactions mmt,
10425                         gme_material_details gme,
10426                         mtl_transaction_lot_numbers mtln,
10427                         (Select pair_type, transaction_id1, transaction_id2 From gme_transaction_pairs where pair_type = 1) gtp, -- PK Bug 9069363
10428                         gmf_process_organizations_gt gpo,
10429                         gme_batch_header gbh,
10430                         gmf_lot_costed_items_gt lcig
10431                    WHERE
10432                          gpo.organization_id = mmt.organization_id
10433              --      AND   gpo.legal_entity_id = l_le_id  -- B 8687115
10434              --      AND   mmt.transaction_date >= NVL(l_trans_start_date, mmt.transaction_date)  changed for performance as below B14616815
10435                    AND   mmt.transaction_date >= l_trans_start_date
10436                    AND   mmt.transaction_date <= l_final_run_date
10437                    AND   mmt.trx_source_line_id = gme.material_detail_id
10438                    AND   gme.batch_id = gbh.batch_id
10439                    AND   mmt.transaction_id = gtp.transaction_id1 (+)
10440                    AND   mmt.transaction_id = mtln.transaction_id
10441             -- Perf B14616815        AND   mmt.organization_id = NVL(l_orgn_id,mmt.organization_id)
10442                    AND   mmt.inventory_item_id = lcig.inventory_item_id
10443                    AND   mmt.organization_id   = lcig.organization_id
10444                    AND  mmt.transaction_source_type_id = 5
10445             -- Perf B14616815        AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
10446                    AND  NOT EXISTS (SELECT 1
10447                                     FROM GMF_MATERIAL_LOT_COST_TXNS gmlct
10448                                    WHERE gmlct.transaction_id = mmt.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
10449                                    AND   gmlct.cost_type_id = l_cost_type_id
10450                                    AND   gmlct.lot_number  = mtln.lot_number
10451                                   AND   gmlct.final_cost_flag = 1)
10452                    AND   gme.phantom_type IN (select (decode(phantom_type,1,1,0))
10453                                 from gme_material_details gme1 where mmt.trx_source_line_id = gme1.material_detail_id)
10454                  UNION ALL
10455                  SELECT
10456                      mmt.transaction_source_id  as doc_id,
10457                      mmt.transaction_source_type_id,
10458                      mmt.inventory_item_id,
10459                      mmt.trx_source_line_id,
10460                      0 as line_type,
10461                      mtln.lot_number,
10462                      mmt.transaction_date as trans_date,
10463                      mmt.transaction_id as transaction_id,
10464                      mtln.primary_quantity as trans_qty,
10465                      lcig.primary_uom_code as trans_um,
10466                      mmt.organization_id,
10467                      2 source,
10468                      NULL as reverse_id,
10469                      mmt.transaction_action_id,
10470                      nvl(mmt.transfer_price,0),
10471                      nvl(mmt.transportation_cost,0),
10472                      mmt.fob_point,
10473                      mmt.transfer_transaction_id,
10474                      NVL(mmt.transaction_cost,0),
10475                      mmt.transfer_organization_id,
10476                     mmt.transaction_date as phantom_trans_date,
10477                      -1 as phantom_type,
10478                      null,
10479                      mmt.transaction_date as oc1,
10480                      decode(mmt.transaction_action_id,27,-1,mmt.transaction_source_type_id) as oc2,
10481                      mmt.transaction_id as oc3,
10482                      0 as routing_id
10483                    FROM mtl_material_transactions mmt,
10484                         mtl_transaction_lot_numbers mtln,
10485                         gmf_process_organizations_gt gpo,
10486                         gmf_lot_costed_items_gt lcig
10487                    WHERE
10488                          gpo.organization_id = mmt.organization_id
10489               --     AND   gpo.legal_entity_id = l_le_id  -- B 8687115
10490               --      AND   mmt.transaction_date >= NVL(l_trans_start_date, mmt.transaction_date)  changed for performance as below B14616815
10491                    AND   mmt.transaction_date >= l_trans_start_date
10492                    AND   mmt.transaction_date <= l_final_run_date
10493                    AND   mmt.transaction_id = mtln.transaction_id
10494           -- Perf B14616815   AND   mmt.organization_id = NVL(l_orgn_id,mmt.organization_id)
10495                    AND   mmt.inventory_item_id = lcig.inventory_item_id
10496                    AND   mmt.organization_id   = lcig.organization_id
10497                    AND   mmt.organization_id = NVL(mmt.owning_organization_id, mmt.organization_id) /* ANTHIYAG Bug#5460458 11-Aug-2006 */
10498                    AND   NVL(mmt.owning_tp_type,2) = 2                                              /* ANTHIYAG Bug#5460458 11-Aug-2006 */
10499                    AND   mmt.transaction_source_type_id <> 5
10500                    AND   mmt.transaction_action_id NOT IN (15,22,6,2,50,51,28,36) /* PK added subinv Xfer Bug 9616762 Ignore container pack unpack Staging Xfers */
10501           -- Perf B14616815   AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
10502                    AND   NOT EXISTS (SELECT 1
10503                                     FROM GMF_MATERIAL_LOT_COST_TXNS gmlct
10504                                    WHERE gmlct.transaction_id = mmt.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
10505                                    AND   gmlct.cost_type_id = l_cost_type_id
10506                                    AND   gmlct.lot_number  = mtln.lot_number
10507                                    AND   gmlct.final_cost_flag = 1)
10508                 UNION ALL
10509                   SELECT
10510                      mmt.transaction_source_id  as doc_id,
10511                      mmt.transaction_source_type_id,
10512                      mmt.inventory_item_id,
10513                      mmt.trx_source_line_id,
10514                      0 as line_type,
10515                      mtln.lot_number,
10516                      mmt.transaction_date as trans_date,
10517                      mmt.transaction_id as transaction_id,
10518                      mtln.primary_quantity as trans_qty,
10519                      lcig.primary_uom_code as trans_um,
10520                      mmt.organization_id,
10521                      2 source,
10522                      NULL as reverse_id,
10523                      mmt.transaction_action_id,
10524                      nvl(mmt.transfer_price,0),
10525                      nvl(mmt.transportation_cost,0),
10526                      mmt.fob_point,
10527                      mmt.transfer_transaction_id,
10528                      NVL(mmt.transaction_cost,0),
10529                      mmt.transfer_organization_id,
10530                     mmt.transaction_date as phantom_trans_date,
10531                      -1 as phantom_type,
10532                      null,
10533                      mmt.transaction_date as oc1,
10534                      decode(mmt.transaction_action_id,27,-1,mmt.transaction_source_type_id) as oc2,
10535                      mmt.transaction_id as oc3,
10536                      0 as routing_id
10537                    FROM mtl_material_transactions mmt,
10538                         mtl_transaction_lot_numbers mtln,
10539                         gmf_process_organizations_gt gpo,
10540                         gmf_lot_costed_items_gt lcig
10541                    WHERE
10542                          gpo.organization_id = mmt.owning_organization_id
10543              --      AND   gpo.legal_entity_id = l_le_id  -- B 8687115
10544                     --      AND   mmt.transaction_date >= NVL(l_trans_start_date, mmt.transaction_date)  changed for performance as below B14616815
10545                    AND   mmt.transaction_date >= l_trans_start_date
10546                    AND   mmt.transaction_date <= l_final_run_date
10547                    AND   mmt.owning_tp_type    = 2
10548                    AND   mmt.transaction_id = mtln.transaction_id
10549            -- Perf B14616815  AND   mmt.owning_organization_id = NVL(l_orgn_id,mmt.owning_organization_id)
10550                    AND   mmt.transaction_source_type_id = 1
10551                    AND   mmt.transaction_action_id = 6
10552                    AND   mmt.inventory_item_id = lcig.inventory_item_id
10553                    AND   mmt.organization_id   = lcig.organization_id
10554            -- Perf B14616815  AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
10555                    AND  NOT EXISTS (SELECT 1
10556                                     FROM GMF_MATERIAL_LOT_COST_TXNS gmlct
10557                                    WHERE gmlct.transaction_id = mmt.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
10558                                    AND   gmlct.cost_type_id = l_cost_type_id
10559                                    AND   gmlct.lot_number  = mtln.lot_number
10560                                    AND   gmlct.final_cost_flag = 1)
10561                   UNION ALL  /*sschinch INVCONV this query will pickup logical shipments and receipts */
10562                     SELECT
10563                      mmt.transaction_source_id  as doc_id,
10564                      mmt.transaction_source_type_id,
10565                      mmt.inventory_item_id,
10566                      mmt.trx_source_line_id,
10567                      0 as line_type,
10568                      mtln.lot_number,
10569                      mmt.transaction_date as trans_date,
10570                      mmt.transaction_id as transaction_id,
10571                      mtln.primary_quantity as trans_qty,
10572                      lcig.primary_uom_code as trans_um,
10573                      mmt.organization_id,
10574                      2 source,
10575                      NULL as reverse_id,
10576                      mmt.transaction_action_id,
10577                      nvl(mmt.transfer_price,0),
10578                      nvl(mmt.transportation_cost,0),
10579                      mmt.fob_point,
10580                      mmt.transfer_transaction_id,
10581                      NVL(mmt.transaction_cost,0),
10582                      mmt.transfer_organization_id,
10583                      mmt.transaction_date as phantom_trans_date,
10584                      -1 as phantom_type,
10585                      null,
10586                      mmt.transaction_date as oc1,
10587                      decode(mmt.transaction_action_id,27,-1,mmt.transaction_source_type_id) as oc2,
10588                      mmt.transaction_id as oc3,
10589                      0 as routing_id
10590                    FROM mtl_material_transactions mmt,
10591                         mtl_transaction_lot_numbers mtln,
10592                         gmf_process_organizations_gt gpo,
10593                         gmf_lot_costed_items_gt lcig
10594                    WHERE
10595                          gpo.organization_id = mmt.organization_id
10596            --        AND   gpo.legal_entity_id = l_le_id  -- B 8687115
10597                     --      AND   mmt.transaction_date >= NVL(l_trans_start_date, mmt.transaction_date)  changed for performance as below B14616815
10598                    AND   mmt.transaction_date >= l_trans_start_date
10599                    AND   mmt.transaction_date <= l_final_run_date
10600                    AND   mmt.transfer_transaction_id = mtln.transaction_id
10601           -- Perf B14616815   AND   mmt.organization_id = NVL(l_orgn_id,mmt.organization_id)
10602                    AND   mmt.inventory_item_id = lcig.inventory_item_id
10603                    AND   mmt.organization_id   = lcig.organization_id
10604                    AND   mmt.transaction_source_type_id IN (8,7,13)
10605                    AND   mmt.transaction_action_id IN (15,22)
10606           -- Perf B14616815   AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
10607                    AND   NOT EXISTS (SELECT 1
10608                                      FROM GMF_MATERIAL_LOT_COST_TXNS gmlct
10609                                      WHERE gmlct.transaction_id = mmt.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
10610                                      AND   gmlct.cost_type_id = l_cost_type_id
10611                                      AND   gmlct.lot_number  = mtln.lot_number
10612                                      AND   gmlct.final_cost_flag = 1)
10613                  UNION ALL
10614                    SELECT
10615                      glca.adjustment_id doc_id,
10616                      0 transaction_source_type_id,
10617                      glca.inventory_item_id,
10618                      glca.adjustment_id line_id,
10619                      0 as line_type ,
10620                      glca.lot_number ,
10621                      glca.adjustment_date trans_date ,
10622                      -9 transaction_id,
10623                      0  trans_qty,
10624                      glci.primary_uom_code trans_um,
10625                      glca.organization_id,
10626                      3 source,
10627                      NULL as reverse_id,
10628                      0 as transaction_action_id,
10629                      0 as transfer_price,
10630                      0 as transportation_cost,
10631                      0 as fob_point,
10632                      0 as transfer_transaction_id,
10633                      0 as transaction_cost,
10634                      0 as transfer_transaction_id,
10635                      glca.adjustment_date as phantom_trans_date,
10636                      -1 as phantom_type,
10637                      null,
10638                      glca.adjustment_date as oc1,
10639                      0 as oc2,
10640                      -9 as oc3,
10641                      0 as routing_id
10642                    FROM  gmf_lot_cost_adjustments glca,
10643           --               mtl_system_items_b iimb,
10644                          gmf_lot_costed_items_gt glci
10645                    WHERE glca.applied_ind       = 'N'
10646                  --  AND   glca.adjustment_date  >= NVL(l_trans_start_date, glca.adjustment_date)  changed for performance as below B14616815
10647                    AND   glca.adjustment_date >= l_trans_start_date
10648                    AND   glca.legal_entity_id   = l_le_id
10649                    AND   glca.cost_type_id      = l_cost_type_id
10650                    AND   glca.delete_mark       = 0
10651            --        AND   iimb.inventory_item_id = glca.inventory_item_id
10652             --       AND   glca.organization_id   = iimb.organization_id
10653            -- Perf B14616815  AND   glca.organization_id   = NVL(l_orgn_id,glca.organization_id)
10654                    AND   glca.inventory_item_id = glci.inventory_item_id
10655                    AND   glca.organization_id   = glci.organization_id
10656                    AND   glca.adjustment_date   <= l_final_run_date
10657            -- Perf B14616815  AND   glca.lot_number = nvl(p_lot_no,glca.lot_number)
10658                   AND EXISTS
10659                        (SELECT 1 FROM gmf_lot_cost_adjustment_dtls
10660                         WHERE adjustment_id = glca.adjustment_id
10661                         AND   delete_mark = 0
10662                        )
10663                     --LCM-OPM Integration, Load Actual LC adjustment transactions, AF
10664                 UNION ALL
10665                    SELECT
10666 		          glat.rcv_transaction_id doc_id,
10667                           50 transaction_source_type_id,
10668                           glat.inventory_item_id,
10669                           glat.ship_line_id line_id,
10670                           0 as line_type ,
10671                           mtln.lot_number,     -- Bug 13906192
10672                           decode(sign(glat.transaction_date-rt.transaction_date), 1, glat.transaction_date, rt.transaction_date) trans_date,
10673                           glat.adj_transaction_id transaction_id,
10674                           glat.primary_quantity,
10675                           glat.primary_uom_code trans_um,
10676                           glat.organization_id,
10677                           4 source,
10678                           NULL as reverse_id,
10679                           0 as transaction_action_id,
10680                           0 as transfer_price,
10681                           0 as transportation_cost,
10682                           0 as fob_point,
10683                           0 as transfer_transaction_id,
10684                           0 as transaction_cost,
10685                           0 as transfer_transaction_id,
10686                           glat.transaction_date as phantom_trans_date,
10687                           -1 as phantom_type,
10688                           NULL,
10689                           decode(sign(glat.transaction_date-rt.transaction_date), 1, glat.transaction_date, rt.transaction_date) oc1,
10690                           0 as oc2,
10691                          -9 as oc3,
10692                           0 as routing_id
10693                    FROM
10694 		                      gmf_lc_adj_transactions glat,   -- from import process of LCM
10695                           rcv_transactions rt,
10696                           mtl_material_transactions mmt,    -- Bug 13906192
10697                           mtl_transaction_lot_numbers mtln, -- Bug 13906192
10698                           gmf_process_organizations_gt gpo,
10699                           gmf_lot_costed_items_gt glci
10700                    WHERE   glat.transaction_date >= l_trans_start_date
10701           --                glat.transaction_date >= NVL(l_trans_start_date,glat.transaction_date) performance changed as above B14616815
10702           --           AND  glat.legal_entity_id   = l_le_id    -- bug 12866252
10703                      AND  rt.transaction_id      = mmt.rcv_transaction_id        -- Bug 13906192
10704                      AND  mmt.transaction_id     = mtln.transaction_id           -- Bug 13906192
10705             -- Perf B14616815   AND  mtln.lot_number        = NVL(p_lot_no,mtln.lot_number) -- Bug 13906192
10706                      AND  rt.transaction_id      = glat.rcv_transaction_id
10707                      AND  gpo.organization_id    = glat.organization_id
10708             -- Perf B14616815   AND  glat.organization_id   = NVL(l_orgn_id,glat.organization_id)
10709                      AND  glci.organization_id   = glat.organization_id
10710                      AND  glci.inventory_item_id = glat.inventory_item_id
10711                      AND  glat.transaction_date  <= l_final_run_date
10712                      AND  glat.event_type         IN (16,17)
10713                      AND  glat.component_type IN ('ITEM PRICE','CHARGE')
10714                      AND  glat.cost_acquisition_flag = 'I'
10715                      AND  (glat.lc_adjustment_flag = 1 OR glat.adjustment_num > 0)  /* Load only actual lc adj */
10716                      AND  NOT EXISTS (SELECT 1
10717                                         FROM  gmf_lc_lot_cost_adjs    gllca
10718                                        WHERE  gllca.adj_transaction_id = glat.adj_transaction_id
10719                                          AND  gllca.cost_type_id       = l_cost_type_id
10720                                          AND  gllca.lot_number         = mtln.lot_number   -- Bug 13906192
10721                                          AND  gllca.lot_costed_flag    = 1)
10722                 -- End LCM-OPM Integration, AF
10723                 )   a
10724                 WHERE  a.orgn_id   = lcb.organization_id  (+)
10725                   AND  a.inventory_item_id = lcb.item_id  (+)
10726                 ORDER BY 24, 25, 5, 26, 6 ;
10727                 --ORDER BY 7,2,5  /*Bug 7215069 - Changed ordering for Receipt into Stores*/
10728                 -- oc1, oc2, line_type,  oc3, lot_number    -- B9131983
10729       ELSE   -- l_lot_no IS NOT NULL in ELSE
10730 
10731       OPEN inv_tran_cursor FOR
10732       SELECT         a.doc_id,
10733                      a.transaction_source_type_id,
10734                      a.inventory_item_id,
10735                      a.line_id,
10736                      a.line_type,
10737                      a.lot_number,
10738                      a.trans_date,
10739                      a.transaction_id,
10740                      a.trans_qty,
10741                      trans_um,
10742                      orgn_id,
10743                      source,
10744                      reverse_id,
10745                      transaction_action_id,
10746                      transfer_price,
10747                      transportation_cost,
10748                      fob_point,
10749                      transfer_transaction_id,
10750                      transaction_cost,
10751                      transfer_orgn_id,
10752                      phantom_trans_date,
10753                      phantom_type,
10754                      pair_type,
10755                      oc1,
10756                      oc2,
10757                      oc3,
10758                      routing_id,
10759                      DECODE(NVL(lcb.item_id,0),0,0,1) AS burden_ind
10760         FROM   ( SELECT DISTINCT cb.organization_id, cb.inventory_item_id as item_id
10761 		             FROM gmf_lot_cost_burdens cb,
10762                       gmf_process_organizations_gt gpo
10763                 WHERE cb.cost_type_id = l_cost_type_id
10764                   AND cb.organization_id = gpo.organization_id
10765 			         )lcb,
10766 -- Bug 13038249 Grupo viz changes End
10767              (SELECT
10768                      mmt.transaction_source_id  as doc_id,
10769                      mmt.transaction_source_type_id,
10770                      mmt.inventory_item_id,
10771                      mmt.trx_source_line_id as line_id,
10772                      NVL(gme.line_type,0) as line_type,
10773                      mtln.lot_number,
10774                      mmt.transaction_date as trans_date,
10775                      mmt.transaction_id as transaction_id,
10776                      mtln.primary_quantity as trans_qty,
10777                      lcig.primary_uom_code as trans_um,
10778                      mmt.organization_id as orgn_id,
10779                      1 source,
10780                      Decode(nvl(gtp.pair_type,0),1,gtp.transaction_id2,NULL) as reverse_id, --nvl(gtp.transaction_id2,NULL) as reverse_id,
10781                      mmt.transaction_action_id,
10782                      nvl(mmt.transfer_price,0) as transfer_price,
10783                      nvl(mmt.transportation_cost,0) as transportation_cost,
10784                      mmt.fob_point,
10785                      mmt.transfer_transaction_id,
10786                      NVL(mmt.transaction_cost,0) as transaction_cost,
10787                      mmt.transfer_organization_id as transfer_orgn_id,
10788                      DECODE( NVL(gme.phantom_type,0),1,(mmt.transaction_date + (1/(2*24 * 3600)) ),
10789                                     mmt.transaction_date) phantom_trans_date,
10790                      gme.phantom_type phantom_type,
10791                      gtp.pair_type pair_type,
10792                      decode(gme.phantom_type,1,DECODE( NVL(gme.phantom_type,0),1,(mmt.transaction_date + (1/(2*24 * 3600)) ),
10793                                     mmt.transaction_date),mmt.transaction_date) as oc1,
10794                      decode(mmt.transaction_action_id,27,-1,mmt.transaction_source_type_id) as oc2,
10795                      DECODE(gme.line_type,1, DECODE((ABS(DECODE(mtln.primary_quantity, 0, 1,mtln.primary_quantity))/DECODE(mtln.primary_quantity, 0, 1, mtln.primary_quantity)),
10796                                          1, mmt.transaction_id ,
10797                                          DECODE(Decode(nvl(gtp.pair_type,0),1,gtp.transaction_id2,NULL), NULL, mmt.transaction_id, (Decode(nvl(gtp.pair_type,0),1,gtp.transaction_id2,NULL)+.5))),mmt.transaction_id) as oc3,
10798                      NVL(gbh.routing_id, 0) as routing_id
10799                    FROM mtl_material_transactions mmt,
10800                         gme_material_details gme,
10801                         mtl_transaction_lot_numbers mtln,
10802                         (Select pair_type, transaction_id1, transaction_id2 From gme_transaction_pairs where pair_type = 1) gtp, -- PK Bug 9069363
10803                         gmf_process_organizations_gt gpo,
10804                         gme_batch_header gbh,
10805                         gmf_lot_costed_items_gt lcig
10806                    WHERE
10807                          gpo.organization_id = mmt.organization_id
10808              --      AND   gpo.legal_entity_id = l_le_id  -- B 8687115
10809              --      AND   mmt.transaction_date >= NVL(l_trans_start_date, mmt.transaction_date)  changed for performance as below B14616815
10810                    AND   mmt.transaction_date >= l_trans_start_date
10811                    AND   mmt.transaction_date <= l_final_run_date
10812                    AND   mmt.trx_source_line_id = gme.material_detail_id
10813                    AND   gme.batch_id = gbh.batch_id
10814                    AND   mmt.transaction_id = gtp.transaction_id1 (+)
10815                    AND   mmt.transaction_id = mtln.transaction_id
10816            -- Perf B14616815  AND   mmt.organization_id = NVL(l_orgn_id,mmt.organization_id)
10817                    AND   mmt.inventory_item_id = lcig.inventory_item_id
10818                    AND   mmt.organization_id   = lcig.organization_id
10819                    AND  mmt.transaction_source_type_id = 5
10820            -- Perf B14616815  AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
10821                    AND   mtln.lot_number = l_lot_no
10822                    AND  NOT EXISTS (SELECT 1
10823                                     FROM GMF_MATERIAL_LOT_COST_TXNS gmlct
10824                                    WHERE gmlct.transaction_id = mmt.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
10825                                    AND   gmlct.cost_type_id = l_cost_type_id
10826                                    AND   gmlct.lot_number  = mtln.lot_number
10827                                   AND   gmlct.final_cost_flag = 1)
10828                    AND   gme.phantom_type IN (select (decode(phantom_type,1,1,0))
10829                                 from gme_material_details gme1 where mmt.trx_source_line_id = gme1.material_detail_id)
10830                  UNION ALL
10831                  SELECT
10832                      mmt.transaction_source_id  as doc_id,
10833                      mmt.transaction_source_type_id,
10834                      mmt.inventory_item_id,
10835                      mmt.trx_source_line_id,
10836                      0 as line_type,
10837                      mtln.lot_number,
10838                      mmt.transaction_date as trans_date,
10839                      mmt.transaction_id as transaction_id,
10840                      mtln.primary_quantity as trans_qty,
10841                      lcig.primary_uom_code as trans_um,
10842                      mmt.organization_id,
10843                      2 source,
10844                      NULL as reverse_id,
10845                      mmt.transaction_action_id,
10846                      nvl(mmt.transfer_price,0),
10847                      nvl(mmt.transportation_cost,0),
10848                      mmt.fob_point,
10849                      mmt.transfer_transaction_id,
10850                      NVL(mmt.transaction_cost,0),
10851                      mmt.transfer_organization_id,
10852                     mmt.transaction_date as phantom_trans_date,
10853                      -1 as phantom_type,
10854                      null,
10855                      mmt.transaction_date as oc1,
10856                      decode(mmt.transaction_action_id,27,-1,mmt.transaction_source_type_id) as oc2,
10857                      mmt.transaction_id as oc3,
10858                      0 as routing_id
10859                    FROM mtl_material_transactions mmt,
10860                         mtl_transaction_lot_numbers mtln,
10861                         gmf_process_organizations_gt gpo,
10862                         gmf_lot_costed_items_gt lcig
10863                    WHERE
10864                          gpo.organization_id = mmt.organization_id
10865               --     AND   gpo.legal_entity_id = l_le_id  -- B 8687115
10866                     --      AND   mmt.transaction_date >= NVL(l_trans_start_date, mmt.transaction_date)  changed for performance as below B14616815
10867                    AND   mmt.transaction_date >= l_trans_start_date
10868                    AND   mmt.transaction_date <= l_final_run_date
10869                    AND   mmt.transaction_id = mtln.transaction_id
10870           -- Perf B14616815   AND   mmt.organization_id = NVL(l_orgn_id,mmt.organization_id)
10871                    AND   mmt.inventory_item_id = lcig.inventory_item_id
10872                    AND   mmt.organization_id   = lcig.organization_id
10873                    AND   mmt.organization_id = NVL(mmt.owning_organization_id, mmt.organization_id) /* ANTHIYAG Bug#5460458 11-Aug-2006 */
10874                    AND   NVL(mmt.owning_tp_type,2) = 2                                              /* ANTHIYAG Bug#5460458 11-Aug-2006 */
10875                    AND   mmt.transaction_source_type_id <> 5
10876                    AND   mmt.transaction_action_id NOT IN (15,22,6,2,50,51,28,36) /* PK added subinv Xfer Bug 9616762 Ignore container pack unpack Staging Xfers */
10877            -- Perf B14616815  AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
10878                    AND   mtln.lot_number = l_lot_no
10879                    AND   NOT EXISTS (SELECT 1
10880                                     FROM GMF_MATERIAL_LOT_COST_TXNS gmlct
10881                                    WHERE gmlct.transaction_id = mmt.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
10882                                    AND   gmlct.cost_type_id = l_cost_type_id
10883                                    AND   gmlct.lot_number  = mtln.lot_number
10884                                    AND   gmlct.final_cost_flag = 1)
10885                 UNION ALL
10886                   SELECT
10887                      mmt.transaction_source_id  as doc_id,
10888                      mmt.transaction_source_type_id,
10889                      mmt.inventory_item_id,
10890                      mmt.trx_source_line_id,
10891                      0 as line_type,
10892                      mtln.lot_number,
10893                      mmt.transaction_date as trans_date,
10894                      mmt.transaction_id as transaction_id,
10895                      mtln.primary_quantity as trans_qty,
10896                      lcig.primary_uom_code as trans_um,
10897                      mmt.organization_id,
10898                      2 source,
10899                      NULL as reverse_id,
10900                      mmt.transaction_action_id,
10901                      nvl(mmt.transfer_price,0),
10902                      nvl(mmt.transportation_cost,0),
10903                      mmt.fob_point,
10904                      mmt.transfer_transaction_id,
10905                      NVL(mmt.transaction_cost,0),
10906                      mmt.transfer_organization_id,
10907                     mmt.transaction_date as phantom_trans_date,
10908                      -1 as phantom_type,
10909                      null,
10910                      mmt.transaction_date as oc1,
10911                      decode(mmt.transaction_action_id,27,-1,mmt.transaction_source_type_id) as oc2,
10912                      mmt.transaction_id as oc3,
10913                      0 as routing_id
10914                    FROM mtl_material_transactions mmt,
10915                         mtl_transaction_lot_numbers mtln,
10916                         gmf_process_organizations_gt gpo,
10917                         gmf_lot_costed_items_gt lcig
10918                    WHERE
10919                          gpo.organization_id = mmt.owning_organization_id
10920              --      AND   gpo.legal_entity_id = l_le_id  -- B 8687115
10921                     --      AND   mmt.transaction_date >= NVL(l_trans_start_date, mmt.transaction_date)  changed for performance as below B14616815
10922                    AND   mmt.transaction_date >= l_trans_start_date
10923                    AND   mmt.transaction_date <= l_final_run_date
10924                    AND   mmt.owning_tp_type    = 2
10925                    AND   mmt.transaction_id = mtln.transaction_id
10926            -- Perf B14616815  AND   mmt.owning_organization_id = NVL(l_orgn_id,mmt.owning_organization_id)
10927                    AND   mmt.transaction_source_type_id = 1
10928                    AND   mmt.transaction_action_id = 6
10929                    AND   mmt.inventory_item_id = lcig.inventory_item_id
10930                    AND   mmt.organization_id   = lcig.organization_id
10931            -- Perf B14616815  AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
10932                    AND   mtln.lot_number = l_lot_no
10933                    AND  NOT EXISTS (SELECT 1
10934                                     FROM GMF_MATERIAL_LOT_COST_TXNS gmlct
10935                                    WHERE gmlct.transaction_id = mmt.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
10936                                    AND   gmlct.cost_type_id = l_cost_type_id
10937                                    AND   gmlct.lot_number  = mtln.lot_number
10938                                    AND   gmlct.final_cost_flag = 1)
10939                   UNION ALL  /*sschinch INVCONV this query will pickup logical shipments and receipts */
10940                     SELECT
10941                      mmt.transaction_source_id  as doc_id,
10942                      mmt.transaction_source_type_id,
10943                      mmt.inventory_item_id,
10944                      mmt.trx_source_line_id,
10945                      0 as line_type,
10946                      mtln.lot_number,
10947                      mmt.transaction_date as trans_date,
10948                      mmt.transaction_id as transaction_id,
10949                      mtln.primary_quantity as trans_qty,
10950                      lcig.primary_uom_code as trans_um,
10951                      mmt.organization_id,
10952                      2 source,
10953                      NULL as reverse_id,
10954                      mmt.transaction_action_id,
10955                      nvl(mmt.transfer_price,0),
10956                      nvl(mmt.transportation_cost,0),
10957                      mmt.fob_point,
10958                      mmt.transfer_transaction_id,
10959                      NVL(mmt.transaction_cost,0),
10960                      mmt.transfer_organization_id,
10961                      mmt.transaction_date as phantom_trans_date,
10962                      -1 as phantom_type,
10963                      null,
10964                      mmt.transaction_date as oc1,
10965                      decode(mmt.transaction_action_id,27,-1,mmt.transaction_source_type_id) as oc2,
10966                      mmt.transaction_id as oc3,
10967                      0 as routing_id
10968                    FROM mtl_material_transactions mmt,
10969                         mtl_transaction_lot_numbers mtln,
10970                         gmf_process_organizations_gt gpo,
10971                         gmf_lot_costed_items_gt lcig
10972                    WHERE
10973                          gpo.organization_id = mmt.organization_id
10974            --        AND   gpo.legal_entity_id = l_le_id  -- B 8687115
10975                     --      AND   mmt.transaction_date >= NVL(l_trans_start_date, mmt.transaction_date)  changed for performance as below B14616815
10976                    AND   mmt.transaction_date >= l_trans_start_date
10977                    AND   mmt.transaction_date <= l_final_run_date
10978                    AND   mmt.transfer_transaction_id = mtln.transaction_id
10979           -- Perf B14616815   AND   mmt.organization_id = NVL(l_orgn_id,mmt.organization_id)
10980                    AND   mmt.inventory_item_id = lcig.inventory_item_id
10981                    AND   mmt.organization_id   = lcig.organization_id
10982                    AND   mmt.transaction_source_type_id IN (8,7,13)
10983                    AND   mmt.transaction_action_id IN (15,22)
10984            -- Perf B14616815  AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
10985                    AND   mtln.lot_number = l_lot_no
10986                    AND   NOT EXISTS (SELECT 1
10987                                      FROM GMF_MATERIAL_LOT_COST_TXNS gmlct
10988                                      WHERE gmlct.transaction_id = mmt.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
10989                                      AND   gmlct.cost_type_id = l_cost_type_id
10990                                      AND   gmlct.lot_number  = mtln.lot_number
10991                                      AND   gmlct.final_cost_flag = 1)
10992                  UNION ALL
10993                    SELECT
10994                      glca.adjustment_id doc_id,
10995                      0 transaction_source_type_id,
10996                      glca.inventory_item_id,
10997                      glca.adjustment_id line_id,
10998                      0 as line_type ,
10999                      glca.lot_number ,
11000                      glca.adjustment_date trans_date ,
11001                      -9 transaction_id,
11002                      0  trans_qty,
11003                      glci.primary_uom_code trans_um,
11004                      glca.organization_id,
11005                      3 source,
11006                      NULL as reverse_id,
11007                      0 as transaction_action_id,
11008                      0 as transfer_price,
11009                      0 as transportation_cost,
11010                      0 as fob_point,
11011                      0 as transfer_transaction_id,
11012                      0 as transaction_cost,
11013                      0 as transfer_transaction_id,
11014                      glca.adjustment_date as phantom_trans_date,
11015                      -1 as phantom_type,
11016                      null,
11017                      glca.adjustment_date as oc1,
11018                      0 as oc2,
11019                      -9 as oc3,
11020                      0 as routing_id
11021                    FROM  gmf_lot_cost_adjustments glca,
11022           --               mtl_system_items_b iimb,
11023                          gmf_lot_costed_items_gt glci
11024                    WHERE glca.applied_ind       = 'N'
11025                  --  AND   glca.adjustment_date  >= NVL(l_trans_start_date, glca.adjustment_date)  changed for performance as below B14616815
11026                    AND   glca.adjustment_date >= l_trans_start_date
11027                    AND   glca.legal_entity_id   = l_le_id
11028                    AND   glca.cost_type_id      = l_cost_type_id
11029                    AND   glca.delete_mark       = 0
11030            --        AND   iimb.inventory_item_id = glca.inventory_item_id
11031             --       AND   glca.organization_id   = iimb.organization_id
11032            -- Perf B14616815  AND   glca.organization_id   = NVL(l_orgn_id,glca.organization_id)
11033                    AND   glca.inventory_item_id = glci.inventory_item_id
11034                    AND   glca.organization_id   = glci.organization_id
11035                    AND   glca.adjustment_date   <= l_final_run_date
11036            -- Perf B14616815  AND   glca.lot_number = nvl(p_lot_no,glca.lot_number)
11037                    AND   glca.lot_number = l_lot_no
11038                   AND EXISTS
11039                        (SELECT 1 FROM gmf_lot_cost_adjustment_dtls
11040                         WHERE adjustment_id = glca.adjustment_id
11041                         AND   delete_mark = 0
11042                        )
11043                     --LCM-OPM Integration, Load Actual LC adjustment transactions, AF
11044                 UNION ALL
11045                    SELECT
11046 		          glat.rcv_transaction_id doc_id,
11047                           50 transaction_source_type_id,
11048                           glat.inventory_item_id,
11049                           glat.ship_line_id line_id,
11050                           0 as line_type ,
11051                           mtln.lot_number,     -- Bug 13906192
11052                           decode(sign(glat.transaction_date-rt.transaction_date), 1, glat.transaction_date, rt.transaction_date) trans_date,
11053                           glat.adj_transaction_id transaction_id,
11054                           glat.primary_quantity,
11055                           glat.primary_uom_code trans_um,
11056                           glat.organization_id,
11057                           4 source,
11058                           NULL as reverse_id,
11059                           0 as transaction_action_id,
11060                           0 as transfer_price,
11061                           0 as transportation_cost,
11062                           0 as fob_point,
11063                           0 as transfer_transaction_id,
11064                           0 as transaction_cost,
11065                           0 as transfer_transaction_id,
11066                           glat.transaction_date as phantom_trans_date,
11067                           -1 as phantom_type,
11068                           NULL,
11069                           decode(sign(glat.transaction_date-rt.transaction_date), 1, glat.transaction_date, rt.transaction_date) oc1,
11070                           0 as oc2,
11071                          -9 as oc3,
11072                           0 as routing_id
11073                    FROM
11074 		                      gmf_lc_adj_transactions glat,   -- from import process of LCM
11075                           rcv_transactions rt,
11076                           mtl_material_transactions mmt,    -- Bug 13906192
11077                           mtl_transaction_lot_numbers mtln, -- Bug 13906192
11078                           gmf_process_organizations_gt gpo,
11079                           gmf_lot_costed_items_gt glci
11080                    WHERE   glat.transaction_date >= l_trans_start_date
11081           --                glat.transaction_date >= NVL(l_trans_start_date,glat.transaction_date) performance changed as above B14616815
11082           --           AND  glat.legal_entity_id   = l_le_id    -- bug 12866252
11083                      AND  rt.transaction_id      = mmt.rcv_transaction_id        -- Bug 13906192
11084                      AND  mmt.transaction_id     = mtln.transaction_id           -- Bug 13906192
11085              -- Perf B14616815  AND   mtln.lot_number = nvl(p_lot_no,mtln.lot_number)
11086                      AND   mtln.lot_number = l_lot_no
11087                      AND  rt.transaction_id      = glat.rcv_transaction_id
11088                      AND  gpo.organization_id    = glat.organization_id
11089             -- Perf B14616815   AND  glat.organization_id   = NVL(l_orgn_id,glat.organization_id)
11090                      AND  glci.organization_id   = glat.organization_id
11091                      AND  glci.inventory_item_id = glat.inventory_item_id
11092                      AND  glat.transaction_date  <= l_final_run_date
11093                      AND  glat.event_type         IN (16,17)
11094                      AND  glat.component_type IN ('ITEM PRICE','CHARGE')
11095                      AND  glat.cost_acquisition_flag = 'I'
11096                      AND  (glat.lc_adjustment_flag = 1 OR glat.adjustment_num > 0)  /* Load only actual lc adj */
11097                      AND  NOT EXISTS (SELECT 1
11098                                         FROM  gmf_lc_lot_cost_adjs    gllca
11099                                        WHERE  gllca.adj_transaction_id = glat.adj_transaction_id
11100                                          AND  gllca.cost_type_id       = l_cost_type_id
11101                                          AND  gllca.lot_number         = mtln.lot_number   -- Bug 13906192
11102                                          AND  gllca.lot_costed_flag    = 1)
11103                 -- End LCM-OPM Integration, AF
11104                 )   a
11105                 WHERE  a.orgn_id   = lcb.organization_id  (+)
11106                   AND  a.inventory_item_id = lcb.item_id  (+)
11107                 ORDER BY 24, 25, 5, 26, 6 ;
11108                 --ORDER BY 7,2,5  /*Bug 7215069 - Changed ordering for Receipt into Stores*/
11109                 -- oc1, oc2, line_type,  oc3, lot_number    -- B9131983
11110 
11111       END IF;   -- l_lot_no IS NULL
11112 
11113       FETCH inv_tran_cursor INTO transaction_row;
11114 
11115       fnd_file.put_line(fnd_File.LOG,'Processing uncosted transactions: '|| to_char(sysdate,'DD-MON-YYYY HH24:MI:SS') );
11116 
11117       WHILE inv_tran_cursor%FOUND
11118       LOOP
11119 
11120         SAVEPOINT inv_tran;
11121         /* ----------Macsteel change Starts Bug  9894310  ---  */
11122         Incremental_Lot_costing(l_return_code) ;
11123         FETCH inv_tran_cursor INTO transaction_row;
11124       END LOOP;
11125       CLOSE inv_tran_cursor;
11126 
11127    FOR j IN 1..l_incr_iter LOOP
11128 
11129      l_tmp := TRUE ;
11130      prev_uncostable_cnt := l_uncostable_tab.COUNT ;
11131 
11132      l_uncostable_lots_tab.delete;
11133      l_uncostable_batches_tab.delete; -- Bug 13038249
11134      fnd_file.put_line(fnd_file.log,'Total Uncostable Records = '|| prev_uncostable_cnt || ' for Pass = ' || j  || ' ' || to_char(sysdate,'DD-MON-YYYY HH24:MI:SS') );
11135 
11136       IF  uc_count > 0  THEN
11137          IF  nuc_count > 0 THEN
11138            n_uncostable_tab.delete;
11139            nuc_count := 0 ; -- Making it ready for another pass
11140           END IF;
11141 
11142       n_uncostable_tab := l_uncostable_tab ;  -- copy all uncostable records to new tab
11143       nuc_count  := 0 ;
11144       l_uncostable_tab.delete;
11145       uc_count := 0 ; -- Making it ready for another pass
11146       END IF;
11147 
11148       FOR i IN 1.. n_uncostable_tab.COUNT LOOP
11149 
11150         nuc_count  := i ;
11151         copy_to_transrow ;
11152 
11153         SAVEPOINT inv_tran;
11154         Incremental_Lot_costing(l_return_code) ;
11155 
11156       END LOOP;
11157 
11158      new_uncostable_cnt := l_uncostable_tab.COUNT ;
11159 
11160      IF ((new_uncostable_cnt >= prev_uncostable_cnt) OR (new_uncostable_cnt = 0) OR (prev_uncostable_cnt = 0)) THEN
11161        fnd_file.put_line(fnd_file.log,
11162        'EXITING Becuase Previous Uncostable Records:  ' || prev_uncostable_cnt || ' = New Uncostable Records: ' || new_uncostable_cnt );
11163        EXIT;
11164      END IF;
11165 
11166     END LOOP;  -- Incremental costing 10 times
11167    /* ----------Macsteel change Ends Bug  9894310  ---  */
11168 
11169       IF l_return_code = 'F' OR l_tmp = FALSE THEN
11170         l_tmp := fnd_concurrent.set_completion_status('WARNING','Errors found during processing.'||
11171 			' Please check the log file for details.');
11172       ELSE
11173         l_tmp := fnd_concurrent.set_completion_status('NORMAL','Process completed successfully.');
11174       END IF;
11175 
11176       fnd_file.put_line
11177       (fnd_file.log,'Lot Cost Rollup finished at '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SS'));
11178 
11179       COMMIT;
11180 
11181 EXCEPTION
11182     WHEN OTHERS THEN
11183       fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in ' || procedure_name);
11184       l_tmp := fnd_concurrent.set_completion_status('ERROR',sqlerrm || ' in ' || procedure_name);
11185       ROLLBACK;
11186 END rollup_lot_costs;
11187 
11188 --*************************************************************************************
11189 --* Incremental_Lot_costing
11190 --* Bug  9894310
11191 -- We have moved the processing code from rollup Lot cost after selecting FROM
11192 -- INV_TRAN_CURSOR as it is in this procedure
11193 -- This is done to call the similar processing code multiple times based on the iterations
11194 -- defined or Hard Coded
11195 --*************************************************************************************
11196 
11197 PROCEDURE Incremental_Lot_costing ( return_code OUT NOCOPY VARCHAR2) IS
11198 
11199   l_return_code  VARCHAR2(1);
11200   l_source_orgn_id NUMBER;
11201   l_source_le_id NUMBER;
11202 
11203  /*  Bug 12704358 */
11204   CURSOR batch_status_cursor(p_batch_id NUMBER)
11205   IS
11206   SELECT batch_status
11207   FROM gme_batch_header
11208   WHERE batch_id = p_batch_id
11209   AND NVL(ACTUAL_CMPLT_DATE,l_final_run_date) <= l_final_run_date;
11210 
11211 BEGIN
11212 
11213     l_return_code := 'T' ;
11214 
11215     -- code As it is from the Rollup_lot_cost
11216 
11217         /* Reset the profile variable to the correct one in every loop*/
11218         l_cost_alloc_profile := cost_alloc_profile; -- Bug 13038249 do not query again from DB
11219 
11220         IF ( transaction_row.fob_point = FOB_SHIPPING AND
11221             transaction_row.transaction_action_id  = LOGICAL_INTRANSIT_RECEIPT
11222            )
11223         THEN
11224           /*here transaction_row.trans_qty  will be negative as we take the value from the source org in the inv trans cursor
11225            making the qunatity as positive as we know that this is a receipt and the qunatity will be positive*/
11226           transaction_row.trans_qty := abs(transaction_row.trans_qty);
11227         END IF;
11228 
11229 
11230         -- Forget all data from last time round
11231 
11232         old_cost := NULL;
11233         IF old_cost_tab.exists(1) THEN old_cost_tab.delete; END IF;
11234         IF new_cost_tab.exists(1) THEN new_cost_tab.delete; END IF;
11235         IF l_burdens_tab.exists(1) THEN l_burdens_tab.delete; END IF;
11236         IF l_acqui_cost_tab.exists(1) THEN l_acqui_cost_tab.delete; END IF;
11237 
11238         IF   l_debug_level >= l_debug_level_high THEN
11239 
11240         l_orgn_code := NULL;
11241         l_item_no := NULL;
11242         organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_orgn_code, l_item_no) ;
11243 
11244           fnd_file.put_line
11245           (fnd_file.log,'Inside inv_tran_cursor, Item = ' || transaction_row.inventory_item_id ||'('||l_item_no||')'||
11246 	  		' Lot = '||transaction_row.lot_number ||
11247 	  		' transaction_id = '||to_char(transaction_row.transaction_id) ||
11248 	  		' organization = '|| to_char(transaction_row.orgn_id) ||'('||l_org_tab(transaction_row.orgn_id)||')'||
11249 	  		' reverse_id = '||to_char(transaction_row.reverse_id) ||
11250 	  		' trans_date = '||to_char(transaction_row.trans_date, 'DD-MON-YYYY HH24:MI:SS') ||
11251 			  ' doc type = '||transaction_row.transaction_source_type_id ||
11252         ' action type = '||transaction_row.transaction_action_id ||
11253 			  ' Qty = ' || transaction_row.trans_qty || '  ' || transaction_row.trans_um ||
11254 			  ' source = ' || transaction_row.source ||
11255         ' transfer_org = ' || transaction_row.transfer_orgn_id ||
11256         ' line id = ' || transaction_row.line_id);
11257 
11258           fnd_file.put_line
11259           (fnd_File.LOG,'Loading existing cost for lot_id '||to_char(transaction_row.lot_number)||' in organization '
11260          		||l_org_tab(transaction_row.orgn_id)||' Timestamp '||to_char(sysdate, 'DD-MON-YYYY HH24:MI:SS'));
11261         END IF;
11262 
11263         l_lot_number := transaction_row.lot_number;
11264 
11265        IF NOT l_uncostable_lots_tab.EXISTS(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number)
11266         THEN
11267            IF   l_debug_level >= l_debug_level_high THEN
11268                fnd_file.put_line(fnd_file.log,' Yes. Current Lot '||transaction_row.lot_number||' is Costable.');
11269            END IF;
11270 
11271          -- Bug 4130869 Added Date field as NULL.. Because, the Date field has no significance here.
11272          -- We have to delete all the records irrespective of trans_date.
11273         /* INVCONV sschinch Commented to replace parameters
11274           OPEN  lot_cost_cursor (transaction_row.whse_code, transaction_row.inventory_item_id, transaction_row.lot_id,NULL);
11275         */
11276         /* INVCONV sschinch */
11277 
11278 
11279         OPEN lot_cost_cursor(transaction_row.orgn_id,
11280                              transaction_row.inventory_item_id,
11281                              transaction_row.lot_number,
11282                              NULL,
11283                              l_cost_type_id);
11284         FETCH lot_cost_cursor INTO old_cost;
11285 
11286         IF lot_cost_cursor%FOUND THEN
11287           IF   l_debug_level >= l_debug_level_high THEN
11288             fnd_file.put_line
11289             (fnd_file.log,'Reading existing costs for header ID '||old_cost.header_id);
11290           END IF;
11291 
11292           OPEN  lot_cost_detail_cursor (old_cost.header_id);
11293           FETCH lot_cost_detail_cursor BULK COLLECT INTO old_cost_tab;
11294           CLOSE lot_cost_detail_cursor;
11295         ELSE
11296           old_cost.onhand_qty := 0;
11297         END IF;
11298 
11299         CLOSE lot_cost_cursor;
11300 
11301         IF old_cost_tab.EXISTS(1) THEN
11302           IF   l_debug_level >= l_debug_level_high THEN
11303             fnd_file.put_line
11304                 (fnd_file.log,'Lot Cost before this transaction is '||to_char(old_cost.unit_cost,'999999999.99'));
11305           END IF;
11306         ELSE
11307             /* Bug 4227784 - This has to be moved up. We can have a case, where we have header
11308                but not details. So old_cost_tab can be null, even though old_cost has some record.
11309                In that case, we should not initialize onhand_qty to zero*/
11310                 --  old_cost.onhand_qty := 0;
11311           IF   l_debug_level >= l_debug_level_high THEN
11312              fnd_file.put_line(fnd_file.log,'Previous cost was NULL');
11313           END IF;
11314         END IF;
11315 
11316           IF   l_debug_level >= l_debug_level_high THEN
11317             fnd_file.put_line(fnd_file.log,' Checking if the Current Lot '||transaction_row.lot_number||' is Costable ?');
11318 
11319           END IF;
11320 
11321         /*IF NOT l_uncostable_lots_tab.EXISTS(transaction_row.lot_id)*/
11322         /*IF is_lot_costable(transaction_row.orgn_id,transaction_row.inventory_item_id,transaction_row.lot_number) IS NOT NULL*/
11323 
11324            --
11325 	        -- umoogala: replaced CASE stmt with IF..ELSE..END IF
11326 	        -- CASE stmt is not supported in 8i Db
11327 	        --
11328           -- If the onhand balance for this lot is negative before this transaction is processed
11329           -- we have to process it differently.
11330           -- If the onhand balance will still be negative (or 0) after this transaction has been
11331           -- processed, we simply treat this as an adjustment at the curent cost. Transactions that
11332           -- flip the balance positive are treated as if they had been split into two quantities. The
11333           -- first of which updates the balance to zero at the old cost. The residue of the trans_qty
11334           -- is then used to create a new cost. The 'feature' though is that the new cost is created as
11335           -- the entire quantity has been used in the calculations but the onhand it is set against is
11336           -- the difference beween the old and new quantities. Got that?
11337 
11338           -- PK Bug 12913227 Bypassing residual quantity check for LC adjustments.
11339 
11340           IF (transaction_row.transaction_source_type_id <> LC_ADJUSTMENT) THEN
11341 
11342             l_residual_qty := old_cost.onhand_qty + transaction_row.trans_qty;
11343 
11344           END IF;
11345 
11346           IF   (old_cost.onhand_qty < 0 AND  l_residual_qty <= 0  AND transaction_row.transaction_source_type_id <> LC_ADJUSTMENT) THEN  -- PK Bug 12913227
11347             IF   l_debug_level >= l_debug_level_high  THEN
11348               fnd_file.put_line
11349               (fnd_file.log,'Onhand balance is currently -ve and will remain -ve. Processing txn as an ADJI');
11350             END IF;
11351 
11352             process_adjustment;
11353 
11354           ELSE
11355             IF  (old_cost.onhand_qty < 0 AND  l_residual_qty > 0 AND transaction_row.transaction_source_type_id <> LC_ADJUSTMENT ) THEN -- PK Bug 12913227
11356 
11357               IF   l_debug_level >= l_debug_level_high THEN
11358                 fnd_file.put_line
11359                 (fnd_file.log,'Onhand balance is currently -ve and will go +ve. Clearing old balance to zero');
11360               END IF;
11361 
11362               old_cost.onhand_qty := 0;
11363             ELSE
11364               IF   (transaction_row.transaction_source_type_id <> LC_ADJUSTMENT) THEN -- PK Bug 12913227
11365 		IF l_debug_level >= l_debug_level_medium THEN -- Bug#13000758
11366                 fnd_file.put_line
11367                 (fnd_file.log,'Onhand balance is currently +ve. Processing normally');
11368 		END IF;
11369               ELSE -- PK Bug 12913227
11370 		IF l_debug_level >= l_debug_level_medium THEN -- Bug#13000758
11371                 fnd_file.put_line
11372                 (fnd_file.log,'Ignore Prior Onhand balance for LCM Adjustments. Processing normally');
11373               END IF;
11374               END IF;
11375 
11376               l_residual_qty := transaction_row.trans_qty;
11377             END IF;
11378 
11379 
11380 
11381             /*IF transaction_row.transaction_source_type_id = 'PORC'*/
11382             IF (transaction_row.transaction_source_type_id IN (INTERNAL_REQ,INTERNAL_ORDER,INVENTORY)
11383                 AND transaction_row.transaction_action_id IN (INTRANSIT_RECEIPT,LOGICAL_INTRANSIT_RECEIPT)
11384                )
11385             THEN
11386               -- Bug 13038249 Grupo viz changes
11387                l_flg_ind:= org_coll_tab(transaction_row.transfer_orgn_id).process_enabled_flag ;
11388                 -- fnd_file.put_line(fnd_file.log,'l_flg_ind ' || l_flg_ind);
11389 
11390               IF (l_flg_ind = 'Y') THEN
11391                 process_receipt;
11392               ELSE
11393                 process_pd_transfer;
11394               END IF;
11395 
11396             ELSIF ( transaction_row.transaction_source_type_id = PURCHASE_ORDER  -- jboppana
11397                     AND transaction_row.transaction_action_id = RECEIPT_INTO_STORES)
11398             THEN
11399                  process_receipt;
11400             /*ELSIF transaction_row.doc_type = 'PROD' INVCONV sshchinch*/
11401             ELSIF (transaction_row.transaction_source_type_id = BATCH) THEN
11402 
11403                 SELECT batch_no INTO l_batch_no FROM gme_batch_header
11404                 WHERE batch_id = transaction_row.doc_id;
11405 
11406                 OPEN batch_status_cursor(transaction_row.doc_id);
11407                 FETCH batch_status_cursor INTO l_batch_status;
11408                 IF( batch_status_cursor % NOTFOUND) THEN
11409                   l_batch_status := 0;
11410                 END IF;
11411                 CLOSE batch_status_cursor;
11412 
11413 
11414                 IF (transaction_row.line_type IN (-1,2)) THEN
11415                   process_adjustment;
11416 
11417                 ELSIF transaction_row.line_type = 1       /* Bug 4004338 Uday Moogala - Added */
11418                    AND   transaction_row.trans_qty < 0
11419                    AND   transaction_row.reverse_id IS NOT NULL
11420                 THEN
11421 
11422                   -- If PROD and reversal, then see if there are any txns between the
11423                   -- original yeild and this reversal. If there is none, then we'll
11424                   -- create new header and details row(s) with unit cost prior to the
11425                   -- original yield. Set it to zero, if there are no prior costs.
11426                     process_reversals2;
11427 
11428                 ELSIF transaction_row.line_type = 1
11429                    AND   transaction_row.trans_qty < 0
11430                    AND   transaction_row.reverse_id IS NULL
11431                 THEN
11432                    -- Bug 9239944 Wip Completion return
11433                    IF l_batch_status IN (-1,3,4)  THEN           /* Bug 12704358  */
11434                   -- If PROD and WIP Completion Return and not a reversal.
11435                     process_reversals2;
11436 
11437                    ELSE
11438                      l_orgn_code := NULL;
11439                      l_item_no := NULL;
11440                      organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_orgn_code, l_item_no) ;
11441                      fnd_file.put_line(fnd_file.log,'WARNING: Batch status is not -1 or 3 or 4 Setting Lot for the orgn/Item/Lot '||transaction_row.orgn_id||'('||l_orgn_code||')'||'/'||transaction_row.inventory_item_id
11442                                         ||'('||l_item_no||')'||'/'||transaction_row.lot_number||' as uncostable');
11443                      l_uncostable_lots_tab(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number) := transaction_row.inventory_item_id;
11444                      copy_uncostable_record ;    /* B9894310 */
11445                      goto DONT_PROCESS_WIPBATCH ;
11446                    END IF;
11447 
11448                 ELSE
11449 
11450                 -- This is a product line of some kind. If the batch has been uncertified the usually positive
11451                 -- transaction quantity will be negative. This is similar to a PO return.
11452 
11453                 -- The costs come from both standard and lot-costed items, together with any resource costs
11454                 -- and burdens. If the lot being yielded does not have a cost in this organization, the procedure
11455                 -- will set them up.
11456 
11457                 -- If there is already a cost for this lot and organization then the costs will be updated
11458                 -- by averaging the new cost with the old cost by using the original and revised quantities.
11459 
11460                 /****** Bug 4177349 - Start ******/
11461 
11462                 --The Status as on Final Date is Completed . So Process Normally through process_batch*/
11463                 IF l_batch_status IN (3,4)  THEN    /* Bug 12704358 B14197262 removed -1   */
11464                   -- Bug 13038249 check if batch is costable first.
11465                  IF  l_uncostable_batches_tab.EXISTS(transaction_row.doc_id) THEN
11466                     IF   l_debug_level >= l_debug_level_high THEN
11467                          fnd_file.put_line(fnd_file.log,' No. Current batch_id '||transaction_row.doc_id||' is NOT Costable.');
11468                     END IF;
11469                     -- B 13038249 next if condition may be redunduant
11470                     IF NOT l_uncostable_lots_tab.EXISTS(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number) THEN
11471                       l_uncostable_lots_tab(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number) := transaction_row.inventory_item_id;
11472                     END IF;
11473                     copy_uncostable_record ;
11474                     goto DONT_PROCESS_WIPBATCH ;
11475                  ELSE
11476                     IF   l_debug_level >= l_debug_level_high THEN
11477                          fnd_file.put_line(fnd_file.log,' Yes. For now Current batch_id '||transaction_row.doc_id||' is Costable.');
11478                     END IF;
11479                     process_batch;
11480                  END IF;
11481                  -- Bug 13038249 check if batch is costable first End.
11482                 ELSIF l_batch_status = -1 THEN -- B14197262 Added this ELSIF
11483 
11484                      IF   l_debug_level >= l_debug_level_high THEN
11485                          fnd_file.put_line(fnd_file.log,' 2 Cancelled batch. Calling process_wip_batch');
11486                     END IF;
11487 
11488                     process_wip_batch;
11489 
11490                 ELSE  /* The Status as on Final Date is not completed. So copy the previous cost for this record or 0$ */
11491                   IF (l_process_wip_batch = 1) THEN /*Bug 13808832 - works as before for WIP batches*/
11492                         /* The Status as on Final Date is not completed. So copy the previous cost for this record or 0$ */
11493                         IF l_debug_level >= l_debug_level_high THEN
11494                         fnd_file.put_line(fnd_file.log,' The Current Batch status is not 3 or 4 ');
11495                         END IF;
11496                         process_wip_batch;
11497                   ELSE
11498                           -- Bug 9284024 Macsteel. Decided not to process wip batches product yield
11499                           -- for item, lot combination.
11500                           l_orgn_code := NULL;
11501                           l_item_no := NULL;
11502                           organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_orgn_code, l_item_no) ;
11503                           fnd_file.put_line(fnd_file.log,'WARNING: Batch status is not 3 or 4 Setting Lot for the orgn/Item/Lot '||transaction_row.orgn_id||'('||l_orgn_code||')'||'/'||transaction_row.inventory_item_id
11504                                             ||'('||l_item_no||')'||'/'||transaction_row.lot_number||' as uncostable');
11505                           l_uncostable_lots_tab(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number) := transaction_row.inventory_item_id;
11506                           copy_uncostable_record ;    /* B9894310 */
11507                           goto DONT_PROCESS_WIPBATCH ;
11508                           -- process_wip_batch;
11509                   END IF;
11510                 END IF;
11511 
11512                 IF l_step_tab.EXISTS(1) THEN
11513                    l_step_tab.DELETE;
11514                 END IF;
11515               END IF;
11516               /* Consigned Inventory Transfer to Regular*/
11517             ELSIF (transaction_row.transaction_source_type_id = PURCHASE_ORDER
11518                    AND transaction_row.transaction_action_id = OWNERSHIP_TRANSFER
11519                --  AND transaction_row.transaction_cost > 0 /* B14081108 */
11520                    AND transaction_row.trans_qty >= 0
11521                   )
11522               THEN
11523                 process_consigned_inventory;
11524             ELSIF (transaction_row.transaction_source_type_id
11525                         IN (ACCOUNT,ACCOUNT_ALIAS,
11526                             CYCLE_COUNT,
11527                             PHYSICAL_INVENTORY,
11528                             INVENTORY,
11529                             MOVE_ORDER)            -- B 6859710 Added MOVE_ORDER
11530                      AND transaction_row.transaction_action_id IN (ISSUE_FROM_STORES,
11531                                                                  CYCLE_COUNT_ADJUSTMENT,
11532                                                                  RECEIPT_INTO_STORES,PHYSICAL_INVENTORY_ADJST) -- B 8616761 Added PHYSICAL_INVENTORY_ADJST
11533                   )
11534            THEN
11535              -- Because of the way that creations and adjustments can be entered there might
11536              -- or might not be a cost already. If there is a cost treat all such transactions
11537              -- as adjustments. If there isn't one treat them as creations
11538 
11539              IF old_cost_tab.EXISTS(1) THEN
11540                process_adjustment;
11541              ELSE
11542                process_creation;
11543              END IF;
11544               /*ELSIF transaction_row.doc_type IN ('TRNI','TRNR')*/
11545            ELSIF (transaction_row.transaction_source_type_id IN (INTERNAL_ORDER,INTERNAL_REQ,INVENTORY) AND
11546                   transaction_row.transaction_action_id = DIRECT_ORG_TRANSFER AND
11547                   transaction_row.trans_qty > 0
11548                  )THEN
11549             /* INVCONV sschinch */
11550             SELECT  transfer_organization_id,
11551                     hoi.org_information2
11552               INTO l_source_orgn_id,
11553                    l_source_le_id
11554               FROM   mtl_material_transactions mmt,
11555                      hr_organization_information hoi
11556              WHERE  mmt.transaction_id = transaction_row.transaction_id
11557                   AND hoi.organization_id = mmt.transfer_organization_id
11558                   AND hoi.org_information_context = 'Accounting Information';
11559 
11560               -- Bug 13038249 Grupo viz changes
11561                l_flg_ind:= org_coll_tab(transaction_row.transfer_orgn_id).process_enabled_flag ;
11562                 --fnd_file.put_line(fnd_file.log,'l_flg_ind ' || l_flg_ind);
11563 
11564               IF (l_flg_ind = 'Y') THEN
11565                  process_movement( transaction_row.line_type
11566                                 , l_source_orgn_id
11567                                 , transaction_row.orgn_id
11568                                 , l_source_le_id
11569                                 , l_le_id
11570                                 , transaction_row.trans_date
11571                                 );
11572               ELSE
11573                  process_pd_transfer;
11574               END IF;
11575 
11576         	    /*ELSIF transaction_row.doc_type = 'XFER' INVCONV sschinch*/
11577 	          ELSIF (transaction_row.transaction_source_type_id IN (INTERNAL_ORDER,INTERNAL_REQ,INVENTORY) AND
11578                    transaction_row.transaction_action_id  = DIRECT_ORG_TRANSFER AND
11579                    transaction_row.trans_qty < 0
11580 	                )
11581 	           THEN
11582                 process_adjustment;
11583              ELSIF (transaction_row.transaction_source_type_id IN (INTERNAL_ORDER,INTERNAL_REQ,INVENTORY) AND
11584                    transaction_row.transaction_action_id  = INTRANSIT_SHIPMENT)  -- JBOPPANA
11585 	           THEN
11586                 process_adjustment;
11587                /*ELSIF transaction_row.doc_type IN ('OMSO','OPSO') */
11588             ELSIF transaction_row.transaction_source_type_id IN (SALES_ORDER,INTERNAL_ORDER)
11589                 AND transaction_row.transaction_action_id IN (ISSUE_FROM_STORES,LOGICAL_INTRANSIT_SHIPMENT)
11590               THEN
11591                 process_adjustment;
11592 
11593 /* ANTHIYAG Bug#5287514 07-Jun-2006 Start */
11594 /*HALUTHRA BUG 7249505 STARTS : In case of return to vendor process the transaction as an adjustment */
11595            /*
11596             ELSIF transaction_row.transaction_source_type_id = PURCHASE_ORDER
11597                 AND transaction_row.transaction_action_id IN (ISSUE_FROM_STORES, DELIVERY_ADJUSTMENTS)
11598               THEN
11599                 process_receipt;
11600                 */
11601               ELSIF transaction_row.transaction_source_type_id = PURCHASE_ORDER
11602                 AND transaction_row.transaction_action_id = ISSUE_FROM_STORES THEN
11603                 process_adjustment;
11604                -- process_receipt;
11605              ELSIF transaction_row.transaction_source_type_id = PURCHASE_ORDER
11606                 AND transaction_row.transaction_action_id =DELIVERY_ADJUSTMENTS
11607               THEN
11608                 process_receipt;
11609 
11610 /*HALUTHRA BUG 7249505 ENDS */
11611 /* ANTHIYAG Bug#5287514 07-Jun-2006 End */
11612 
11613             ELSIF transaction_row.transaction_source_type_id = RMA
11614                 AND transaction_row.transaction_action_id = ISSUE_FROM_STORES
11615               THEN
11616                 process_adjustment;
11617             ELSIF transaction_row.transaction_source_type_id = RMA
11618                 AND transaction_row.transaction_action_id = RECEIPT_INTO_STORES
11619               THEN
11620                 process_receipt;
11621 
11622                /*ELSIF transaction_row.doc_type = 'LADJ'*/
11623             ELSIF (transaction_row.transaction_source_type_id = LOT_COST_ADJUSTMENT)
11624               THEN
11625                 process_lot_cost_adjustments;
11626                 /* INVCONV sschinch */
11627             ELSIF (transaction_row.transaction_source_type_id = INVENTORY AND
11628                 transaction_row.transaction_action_id = LOT_SPLIT) THEN
11629                 process_lot_split;
11630             ELSIF (transaction_row.transaction_source_type_id = INVENTORY AND
11631                    transaction_row.transaction_action_id = LOT_MERGE) THEN
11632                 process_lot_merge;
11633             ELSIF (transaction_row.transaction_source_type_id = INVENTORY AND
11634                    transaction_row.transaction_action_id = LOT_TRANSLATE
11635                   ) THEN
11636               process_lot_translate;
11637             -- AF   LCM-OPM Integration
11638             ELSIF transaction_row.transaction_source_type_id = LC_ADJUSTMENT THEN
11639             -- Bug  10159100 Logic to prorate implemented. No changes here
11640                  IF   l_debug_level >= l_debug_level_high THEN
11641                   fnd_file.put_line (fnd_file.log,'Call process_lc_adjustments for :' || transaction_row.transaction_id);
11642                 END IF;
11643                 process_lc_adjustments();
11644             -- AF
11645             END IF;
11646 
11647               IF l_residual_qty <> transaction_row.trans_qty THEN
11648                 -- The transaction that has been processed flipped a negative onhand balance back to positive
11649                 -- so we need to adjust the onhand balance in the header to the residual balance
11650 
11651                 IF   l_debug_level >= l_debug_level_high THEN
11652                   fnd_file.put_line
11653                   (fnd_file.log,'Onhand balance has flipped from -ve to +ve. Setting onhand qty to residual qty');
11654                 END IF;
11655 
11656                 -- Bug 12394608 Need to use 'AND inventory_item_id = transaction_row.inventory_item_id' as well
11657 
11658                 UPDATE gmf_lot_costs
11659                 SET    onhand_qty = l_residual_qty
11660                 WHERE  header_id = (SELECT max(header_id)
11661                                     FROM   gmf_lot_costs
11662                                     WHERE organization_id = transaction_row.orgn_id
11663                                     AND lot_number = transaction_row.lot_number
11664                                     AND inventory_item_id = transaction_row.inventory_item_id)
11665                 RETURNING header_id INTO new_cost.header_id;
11666 
11667                 -- B3486228 Also set the transaction qty to the residual
11668                 UPDATE gmf_material_lot_cost_txns
11669                    SET    new_onhand_qty = l_residual_qty
11670                  WHERE  transaction_id = transaction_row.transaction_id /* ANTHIYAG Bug#5285726 07-Jun-2006 */
11671                   AND   cost_header_id = new_cost.header_id;
11672               END IF;
11673             END IF;
11674 
11675             -- For anything other than production transactions we can
11676             -- set the rows to 'costed'. 'PROD' transactions are dealt with
11677             -- in their own procedure above
11678 
11679             IF  l_return_status = 'S' THEN
11680               IF l_final_run_flag = 1	THEN -- umoogala  05-Dec-2003
11681                 -- This is a lot cost adjustment (source is 3)
11682                 -- PK Bug 6697946 If old_cost.header_id is NULL Use new_cost.header_id
11683                 -- RDP MAcsteel 10415925
11684                 IF transaction_row.source = 3 THEN
11685                   IF   l_debug_level >= l_debug_level_high THEN
11686                     fnd_file.put_line
11687                    (fnd_file.log,'FINAL Mode old '||old_cost.header_id||' new '||new_cost.header_id||' Onhand '||new_cost.onhand_qty||' adj '||transaction_row.doc_id);
11688                    END IF;
11689 
11690 
11691                   UPDATE gmf_lot_cost_adjustments
11692                   SET  applied_ind = 'Y',
11693           		       old_cost_header_id = NVL(old_cost.header_id, new_cost.header_id),
11694 		                 new_cost_header_id = new_cost.header_id,
11695 		                 onhand_qty	  = new_cost.onhand_qty
11696                   WHERE  adjustment_id = transaction_row.doc_id
11697                     AND  inventory_item_id = transaction_row.inventory_item_id
11698                     AND  lot_number = transaction_row.lot_number
11699                     AND  organization_id = transaction_row.orgn_id ;
11700 
11701                 END IF;
11702 
11703               -- for all sources this needs to be updated
11704               UPDATE gmf_lot_costs
11705               SET    final_cost_flag = 1
11706               WHERE  header_id = new_cost.header_id;
11707 
11708             ELSE
11709 	   	    	  IF transaction_row.source = 3 THEN
11710 	   	    	    -- PK Bug 6697946 If old_cost.header_id is NULL Use new_cost.header_id
11711 			          UPDATE gmf_lot_cost_adjustments
11712 			             SET  old_cost_header_id = NVL(old_cost.header_id, new_cost.header_id),
11713 			                  new_cost_header_id = new_cost.header_id,
11714 			                  onhand_qty	  = new_cost.onhand_qty
11715 			            WHERE adjustment_id = transaction_row.doc_id;
11716 		          END IF;
11717             END IF;
11718             COMMIT;
11719           ELSE
11720             -- Add this lot to the list of lots that can't be costed
11721             l_orgn_code := NULL;
11722             l_item_no := NULL;
11723             organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_orgn_code, l_item_no) ;
11724 
11725             fnd_file.put_line(fnd_file.log,'WARNING: Setting Lot for the orgn/Item/Lot '||transaction_row.orgn_id||'('||l_orgn_code||')'||'/'||transaction_row.inventory_item_id||'('||l_item_no||')'||'/'||transaction_row.lot_number||' as uncostable');
11726             l_uncostable_lots_tab(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number) := transaction_row.inventory_item_id;
11727             copy_uncostable_record ;    /* B9894310 */
11728             l_tmp := FALSE;
11729 	          l_return_code := 'F';
11730             ROLLBACK TO SAVEPOINT inv_tran;
11731           END IF;
11732 
11733          ELSE
11734              IF   l_debug_level >= l_debug_level_high THEN
11735                l_orgn_code := NULL;
11736                l_item_no := NULL;
11737                organization_item_code (transaction_row.orgn_id, transaction_row.inventory_item_id, l_orgn_code, l_item_no) ;
11738                fnd_file.put_line(fnd_file.log,'Current orgn/Item/Lot '||transaction_row.orgn_id||'('||l_orgn_code||')'||'/'||transaction_row.inventory_item_id||'('||l_item_no||')'||'/'||transaction_row.lot_number||' is Not Costable');
11739                 fnd_file.put_line(fnd_file.log,' So skipping the transaction: '||transaction_row.transaction_id);
11740                 -- Bug 13038249 we came here because lot is already uncostable commenting out next one line
11741                 -- l_uncostable_lots_tab(transaction_row.orgn_id||'-'||transaction_row.inventory_item_id||'-'||transaction_row.lot_number) := transaction_row.inventory_item_id;
11742                 copy_uncostable_record ;    /* B9894310 */
11743               END IF;
11744          END IF; -- skip this txn
11745         <<DONT_PROCESS_WIPBATCH>>
11746 
11747        return_code := l_return_code ;
11748 
11749 END Incremental_Lot_costing;
11750 
11751 --**********************************************************************************************
11752     --*                                                                                            *
11753     --* Procedure to get organization_code and item_no                                             *
11754     --* Bug  13059282
11755     --* Modified by Pramod for Grupo and TITAN                                                                       *
11756 --**********************************************************************************************
11757 PROCEDURE organization_item_code
11758   ( p_orgn_id   IN NUMBER,
11759     p_item_id   IN NUMBER,
11760     p_orgn_code OUT NOCOPY VARCHAR2,
11761     p_item_no   OUT NOCOPY VARCHAR2
11762   ) IS
11763 
11764 BEGIN
11765 
11766   p_orgn_code := l_org_tab(p_orgn_id);
11767 
11768   IF NOT l_itemno_tab.exists(p_item_id) THEN
11769 
11770    SELECT SUBSTR(msi.concatenated_segments,1,50) INTO p_item_no
11771      FROM mtl_system_items_kfv msi
11772    WHERE  msi.inventory_item_id = p_item_id
11773     AND  msi.organization_id = p_orgn_id  ;
11774 
11775      l_itemno_tab(p_item_id) := p_item_no;
11776 
11777    ELSE
11778      p_item_no := l_itemno_tab(p_item_id) ;
11779    END IF ;
11780 
11781 EXCEPTION
11782   WHEN NO_DATA_FOUND THEN
11783     p_item_no := NULL ;
11784   WHEN OTHERS THEN
11785     p_item_no := NULL ;
11786     fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in organization_item_code');
11787 END organization_item_code;
11788 
11789     --**********************************************************************************************
11790     --*                                                                                            *
11791     --* Procedure to copy uncsotable records                                                       *
11792     --* Bug  9894310                                                                               *
11793     --**********************************************************************************************
11794 PROCEDURE copy_uncostable_record IS
11795 BEGIN
11796 
11797   IF l_incr_iter > 0 THEN
11798 
11799   uc_count := uc_count + 1 ;
11800   l_uncostable_tab(uc_count).doc_id                     :=  transaction_row.doc_id                ;
11801   l_uncostable_tab(uc_count).transaction_source_type_id :=  transaction_row.transaction_source_type_id ;
11802   l_uncostable_tab(uc_count).inventory_item_id          :=  transaction_row.inventory_item_id        ;
11803   l_uncostable_tab(uc_count).line_id                    :=  transaction_row.line_id                  ;
11804   l_uncostable_tab(uc_count).line_type                  :=  transaction_row.line_type               ;
11805   l_uncostable_tab(uc_count).lot_number                 :=  transaction_row.lot_number               ;
11806   l_uncostable_tab(uc_count).trans_date                 := transaction_row.trans_date                ;
11807   l_uncostable_tab(uc_count).transaction_id             := transaction_row.transaction_id            ;
11808   l_uncostable_tab(uc_count).trans_qty                  := transaction_row.trans_qty                 ;
11809   l_uncostable_tab(uc_count).trans_um                   := transaction_row.trans_um                  ;
11810   l_uncostable_tab(uc_count).orgn_id                    := transaction_row.orgn_id                   ;
11811   l_uncostable_tab(uc_count).source                     := transaction_row.source                    ;
11812   l_uncostable_tab(uc_count).reverse_id                 := transaction_row.reverse_id                ;
11813   l_uncostable_tab(uc_count).transaction_action_id      := transaction_row.transaction_action_id     ;
11814   l_uncostable_tab(uc_count).transfer_price             := transaction_row.transfer_price            ;
11815   l_uncostable_tab(uc_count).transportation_cost        := transaction_row.transportation_cost       ;
11816   l_uncostable_tab(uc_count).fob_point                  := transaction_row.fob_point                 ;
11817   l_uncostable_tab(uc_count).transfer_transaction_id    := transaction_row.transfer_transaction_id   ;
11818   l_uncostable_tab(uc_count).transaction_cost           := transaction_row.transaction_cost          ;
11819   l_uncostable_tab(uc_count).transfer_orgn_id           := transaction_row.transfer_orgn_id          ;
11820   l_uncostable_tab(uc_count).phantom_trans_date         := transaction_row.phantom_trans_date        ;
11821   l_uncostable_tab(uc_count).phantom_type               := transaction_row.phantom_type              ;
11822   l_uncostable_tab(uc_count).pair_type                  := transaction_row.pair_type                 ;
11823   l_uncostable_tab(uc_count).oc1                        := transaction_row.oc1                       ;
11824   l_uncostable_tab(uc_count).oc2                        := transaction_row.oc2                       ;
11825   l_uncostable_tab(uc_count).oc3                        := transaction_row.oc3                       ;
11826   l_uncostable_tab(uc_count).routing_id                 := transaction_row.routing_id                ;
11827   l_uncostable_tab(uc_count).burden_ind                 := transaction_row.burden_ind                ;
11828 
11829 
11830 
11831    fnd_file.put_line
11832   (fnd_File.LOG,'Loading in copy_uncostable_record for lot_id '||to_char(transaction_row.lot_number)||' Trasaction_id '
11833   ||to_char(transaction_row.transaction_id) );
11834  ELSE
11835    NULL ;
11836  END IF;
11837 EXCEPTION
11838   WHEN OTHERS THEN
11839     fnd_file.put_line(fnd_file.log,'ERROR: '||substr(sqlerrm,1,100) || ' in copy_uncostable_record');
11840 END copy_uncostable_record;
11841 
11842     --**********************************************************************************************
11843     --*                                                                                            *
11844     --* Procedure to copy back to copy_to_transrow                                                 *
11845     --* Bug  9894310                                                                               *
11846     --**********************************************************************************************
11847 PROCEDURE copy_to_transrow IS
11848 BEGIN
11849 
11850  transaction_row.doc_id                     := n_uncostable_tab(nuc_count).doc_id                     ;
11851  transaction_row.transaction_source_type_id := n_uncostable_tab(nuc_count).transaction_source_type_id ;
11852  transaction_row.inventory_item_id          := n_uncostable_tab(nuc_count).inventory_item_id          ;
11853  transaction_row.line_id                    := n_uncostable_tab(nuc_count).line_id                    ;
11854  transaction_row.line_type                  := n_uncostable_tab(nuc_count).line_type                  ;
11855  transaction_row.lot_number                 := n_uncostable_tab(nuc_count).lot_number             ;
11856  transaction_row.trans_date                 := n_uncostable_tab(nuc_count).trans_date             ;
11857  transaction_row.transaction_id             := n_uncostable_tab(nuc_count).transaction_id         ;
11858  transaction_row.trans_qty                  := n_uncostable_tab(nuc_count).trans_qty              ;
11859  transaction_row.trans_um                   := n_uncostable_tab(nuc_count).trans_um               ;
11860  transaction_row.orgn_id                    := n_uncostable_tab(nuc_count).orgn_id                ;
11861  transaction_row.source                     := n_uncostable_tab(nuc_count).source                 ;
11862  transaction_row.reverse_id                 := n_uncostable_tab(nuc_count).reverse_id             ;
11863  transaction_row.transaction_action_id      := n_uncostable_tab(nuc_count).transaction_action_id  ;
11864  transaction_row.transfer_price             := n_uncostable_tab(nuc_count).transfer_price         ;
11865  transaction_row.transportation_cost        := n_uncostable_tab(nuc_count).transportation_cost    ;
11866  transaction_row.fob_point                  := n_uncostable_tab(nuc_count).fob_point              ;
11867  transaction_row.transfer_transaction_id    := n_uncostable_tab(nuc_count).transfer_transaction_id;
11868  transaction_row.transaction_cost           := n_uncostable_tab(nuc_count).transaction_cost       ;
11869  transaction_row.transfer_orgn_id           := n_uncostable_tab(nuc_count).transfer_orgn_id       ;
11870  transaction_row.phantom_trans_date         := n_uncostable_tab(nuc_count).phantom_trans_date     ;
11871  transaction_row.phantom_type               := n_uncostable_tab(nuc_count).phantom_type           ;
11872  transaction_row.pair_type                  := n_uncostable_tab(nuc_count).pair_type              ;
11873  transaction_row.oc1                        := n_uncostable_tab(nuc_count).oc1                    ;
11874  transaction_row.oc2                        := n_uncostable_tab(nuc_count).oc2                    ;
11875  transaction_row.oc3                        := n_uncostable_tab(nuc_count).oc3                    ;
11876  transaction_row.routing_id                 := n_uncostable_tab(nuc_count).routing_id; -- Bug 13038249
11877  transaction_row.burden_ind                 := n_uncostable_tab(nuc_count).burden_ind; -- Bug 13038249
11878 
11879 
11880    fnd_file.put_line
11881   (fnd_File.LOG,'Loading in copy_to_transrow for lot_id '||to_char(transaction_row.lot_number)||' Trasaction_id '
11882   ||to_char(transaction_row.transaction_id) );
11883 
11884 END copy_to_transrow;
11885 
11886     --**********************************************************************************************
11887     --*                                                                                            *
11888     --* Bug 13386258-VC. Display Cached batch steps details                                                 *
11889     --*                                                                                            *
11890     --*********************************************************************************************
11891 
11892 PROCEDURE display_c_batch_details
11893      ( p_batch_id	NUMBER
11894      )
11895 IS
11896 
11897   i               NUMBER;
11898   j               NUMBER;
11899   k               NUMBER;
11900   l               NUMBER;
11901   procedure_name VARCHAR2(100);
11902 
11903 
11904 BEGIN
11905    procedure_name := 'display cached batch details';
11906 
11907       fnd_file.put_line
11908       (fnd_file.log,'Entered Procedure: '||procedure_name);
11909 
11910       IF NOT c_batch_details.EXISTS(p_batch_id) THEN
11911         fnd_file.put_line(fnd_file.log,'Details of batch-id:'||p_batch_id||' are not cached');
11912         RETURN;
11913       END IF;
11914 
11915       -- print details from c_batch_details(p_batch_id) for debugging purpose
11916 
11917 
11918       IF   l_debug_level >= l_debug_level_high THEN
11919          fnd_file.put_line(fnd_file.log,' ');
11920 
11921          FOR i IN 1..c_batch_details(p_batch_id).step_tab.count
11922          LOOP
11923            fnd_file.put_line
11924            (fnd_file.log,'Dump of step index '||to_char(i));
11925            fnd_file.put_line
11926            (fnd_file.log,'---------------------');
11927            fnd_file.put_line
11928               (fnd_file.log,'Step ID         = '||to_char(c_batch_details(p_batch_id).step_tab(i).current_step_id));
11929            fnd_file.put_line
11930            (fnd_file.log,'Step qty        = '
11931            ||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).step_qty,2))||' '||c_batch_details(p_batch_id).step_tab(i).step_qty_uom);
11932            fnd_file.put_line
11933            (fnd_file.log,'Output qty      = '
11934            ||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).output_qty,2))||' '||c_batch_details(p_batch_id).step_tab(i).step_qty_uom);
11935            fnd_file.put_line
11936            (fnd_file.log,'  ');
11937 
11938            fnd_file.put_line
11939            (fnd_file.log,'  Current Costs');
11940            fnd_file.put_line
11941            (fnd_file.log,'  -------------');
11942 
11943            IF c_batch_details(p_batch_id).step_tab(i).current_costs.EXISTS(1) THEN
11944              FOR j IN 1..c_batch_details(p_batch_id).step_tab(i).current_costs.count
11945              LOOP
11946                fnd_file.put_line
11947                (fnd_file.log,'  Component Class ID     = '
11948                ||to_char(c_batch_details(p_batch_id).step_tab(i).current_costs(j).cost_cmpntcls_id));
11949                fnd_file.put_line
11950                (fnd_file.log,'  Cost Analysis Code     = '
11951                ||c_batch_details(p_batch_id).step_tab(i).current_costs(j).cost_analysis_code);
11952                fnd_file.put_line
11953                (fnd_file.log,'  Cost Level             = '
11954                ||to_char(c_batch_details(p_batch_id).step_tab(i).current_costs(j).cost_level));
11955                fnd_file.put_line
11956                (fnd_file.log,'  Component Cost         = '
11957                ||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).current_costs(j).component_cost,2)));
11958                fnd_file.put_line(fnd_file.log,' ');
11959              END LOOP;
11960            ELSE
11961              fnd_file.put_line(fnd_file.log,'  This step has no current costs');
11962            END IF;
11963 
11964            fnd_file.put_line
11965            (fnd_file.log,'  Inherited Costs');
11966            fnd_file.put_line
11967            (fnd_file.log,'  ---------------');
11968            IF c_batch_details(p_batch_id).step_tab(i).inherited_costs.EXISTS(1) THEN
11969              FOR j IN 1..c_batch_details(p_batch_id).step_tab(i).inherited_costs.count
11970              LOOP
11971                fnd_file.put_line
11972                (fnd_file.log,'  Component Class ID     = '
11973                ||to_char(c_batch_details(p_batch_id).step_tab(i).inherited_costs(j).cost_cmpntcls_id));
11974                fnd_file.put_line
11975                (fnd_file.log,'  Cost Analysis Code     = '
11976                ||c_batch_details(p_batch_id).step_tab(i).inherited_costs(j).cost_analysis_code);
11977                fnd_file.put_line
11978                (fnd_file.log,'  Cost Level             = '
11979                ||to_char(c_batch_details(p_batch_id).step_tab(i).inherited_costs(j).cost_level));
11980                fnd_file.put_line
11981                (fnd_file.log,'  Component Cost         = '
11982                ||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).inherited_costs(j).component_cost,2)));
11983                fnd_file.put_line(fnd_file.log,' ');
11984              END LOOP;
11985            ELSE
11986              fnd_file.put_line(fnd_file.log,'  This step has no inherited costs');
11987            END IF;
11988 
11989            fnd_file.put_line
11990            (fnd_file.log,'  Step Costs');
11991            fnd_file.put_line
11992            (fnd_file.log,'  ---------------');
11993            IF c_batch_details(p_batch_id).step_tab(i).step_costs.EXISTS(1) THEN
11994              FOR j IN 1..c_batch_details(p_batch_id).step_tab(i).step_costs.count
11995              LOOP
11996                fnd_file.put_line
11997                (fnd_file.log,'  Component Class ID     = '
11998                ||to_char(c_batch_details(p_batch_id).step_tab(i).step_costs(j).cost_cmpntcls_id));
11999                fnd_file.put_line
12000                (fnd_file.log,'  Cost Analysis Code     = '
12001                ||c_batch_details(p_batch_id).step_tab(i).step_costs(j).cost_analysis_code);
12002                fnd_file.put_line
12003                (fnd_file.log,'  Cost Level             = '
12004                ||to_char(c_batch_details(p_batch_id).step_tab(i).step_costs(j).cost_level));
12005                fnd_file.put_line
12006                (fnd_file.log,'  Component Cost         = '
12007                ||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).step_costs(j).component_cost,2)));
12008                fnd_file.put_line(fnd_file.log,' ');
12009              END LOOP;
12010            ELSE
12011              fnd_file.put_line(fnd_file.log,'  This step has no step costs');
12012            END IF;
12013 
12014 
12015            fnd_file.put_line(fnd_file.log,' ');
12016            fnd_file.put_line
12017            (fnd_file.log,'  Subsequent steps');
12018            fnd_file.put_line
12019            (fnd_file.log,'  ----------------');
12020            IF c_batch_details(p_batch_id).step_tab(i).dependencies(1).step_index IS NULL THEN
12021              fnd_file.put_line(fnd_file.log,'  This is a terminal step');
12022            ELSE
12023              FOR j IN 1..c_batch_details(p_batch_id).step_tab(i).dependencies.count
12024              LOOP
12025                fnd_file.put_line
12026                (fnd_file.log,'  Step index      = '||to_char(c_batch_details(p_batch_id).step_tab(i).dependencies(j).step_index));
12027                fnd_file.put_line
12028                (fnd_file.log,'  Step ID         = '||to_char(c_batch_details(p_batch_id).step_tab(i).dependencies(j).batchstep_id));
12029                fnd_file.put_line
12030                (fnd_file.log,'  Step qty        = '
12031                ||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).dependencies(j).step_qty,2))
12032                ||c_batch_details(p_batch_id).step_tab(i).dependencies(j).step_qty_uom);
12033                fnd_file.put_line(fnd_file.log,' ');
12034              END LOOP;
12035            END IF;
12036 
12037            fnd_file.put_line(fnd_file.log,' ');
12038            fnd_file.put_line
12039            (fnd_file.log,'  Materials');
12040            fnd_file.put_line
12041            (fnd_file.log,'  ---------');
12042 
12043            IF c_batch_details(p_batch_id).step_tab(i).materials.EXISTS(1) THEN
12044              FOR j in 1..c_batch_details(p_batch_id).step_tab(i).materials.count
12045              LOOP
12046                fnd_file.put_line
12047                (fnd_file.log,'  Trans ID      = '||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).trans_id));
12048                fnd_file.put_line
12049                (fnd_file.log,'  Legal Entity       = '||c_batch_details(p_batch_id).step_tab(i).materials(j).legal_entity_id);
12050                fnd_file.put_line
12051                (fnd_file.log,'  Organization      = '||l_org_tab(c_batch_details(p_batch_id).step_tab(i).materials(j).organization_id));
12052                fnd_file.put_line
12053                (fnd_file.log,'  Item ID       = '||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).item_id));
12054                fnd_file.put_line
12055                (fnd_file.log,'  Lot Number        = '||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).lot_number));
12056                fnd_file.put_line
12057                (fnd_file.log,'  Line type     = '||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).line_type));
12058                fnd_file.put_line
12059                (fnd_file.log,'  Trans Qty     = '||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).trans_qty)
12060                ||c_batch_details(p_batch_id).step_tab(i).materials(j).trans_um);
12061                fnd_file.put_line
12062                (fnd_file.log,'  Trans Date    = '
12063                ||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).trans_date,'DD-Mon-YYYY HH24:MI:SS'));
12064                fnd_file.put_line
12065                (fnd_file.log,'  Lot Cost Flag = '||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).lot_costed_flag));
12066                fnd_file.put_line
12067                (fnd_file.log,'  Contribution  = '||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).step_contribution));
12068                fnd_file.put_line
12069                (fnd_file.log,'  Trans Cost    = '||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).materials(j).trans_cost,2)));
12070                fnd_file.put_line
12071                (fnd_file.log,' ');
12072                fnd_file.put_line
12073                (fnd_file.log,'    Cost Details for material transaction '
12074                ||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).trans_id));
12075                fnd_file.put_line
12076                (fnd_file.log,'    ----------------------------------------------');
12077 
12078                IF c_batch_details(p_batch_id).step_tab(i).materials(j).cost_details.EXISTS(1) THEN
12079                  FOR k IN 1..c_batch_details(p_batch_id).step_tab(i).materials(j).cost_details.count
12080                  LOOP
12081                    fnd_file.put_line
12082                    (fnd_file.log,'    Component Class ID     = '
12083                    ||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).cost_details(k).cost_cmpntcls_id));
12084                    fnd_file.put_line
12085                    (fnd_file.log,'    Cost Analysis Code     = '
12086                    ||c_batch_details(p_batch_id).step_tab(i).materials(j).cost_details(k).cost_analysis_code);
12087                    fnd_file.put_line
12088                    (fnd_file.log,'    Cost Level             = '
12089                    ||to_char(c_batch_details(p_batch_id).step_tab(i).materials(j).cost_details(k).cost_level));
12090                    fnd_file.put_line
12091                    (fnd_file.log,'    Component Cost         = '
12092                    ||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).materials(j).cost_details(k).component_cost,2)));
12093                    fnd_file.put_line(fnd_file.log,' ');
12094                  END LOOP;
12095                ELSE
12096                  fnd_file.put_line(fnd_file.log,'    No costs exist for this material');
12097                END IF;
12098              END LOOP;
12099            ELSE
12100              fnd_file.put_line(fnd_file.log,'  This step has no associated materials');
12101            END IF;
12102 
12103            fnd_file.put_line(fnd_file.log,' ');
12104            fnd_file.put_line
12105            (fnd_file.log,'  Resources');
12106            fnd_file.put_line
12107            (fnd_file.log,'  ---------');
12108 
12109            IF c_batch_details(p_batch_id).step_tab(i).resources.EXISTS(1) THEN
12110              FOR j in 1..c_batch_details(p_batch_id).step_tab(i).resources.count
12111              LOOP
12112                fnd_file.put_line
12113                (fnd_file.log,'  Trans ID      = '||to_char(c_batch_details(p_batch_id).step_tab(i).resources(j).trans_id));
12114                fnd_file.put_line
12115                (fnd_file.log,'  Orgn Id     = '||l_org_tab(c_batch_details(p_batch_id).step_tab(i).resources(j).organization_id));
12116                fnd_file.put_line
12117                (fnd_file.log,'  Resource      = '||c_batch_details(p_batch_id).step_tab(i).resources(j).resources);
12118                fnd_file.put_line
12119                (fnd_file.log,'  Resource Usage= '||to_char(c_batch_details(p_batch_id).step_tab(i).resources(j).resource_usage)
12120                ||c_batch_details(p_batch_id).step_tab(i).resources(j).trans_um);
12121                fnd_file.put_line
12122                (fnd_file.log,'  Trans Date    = '
12123                ||to_char(c_batch_details(p_batch_id).step_tab(i).resources(j).trans_date,'DD-Mon-YYYY HH24:MI:SS'));
12124                fnd_file.put_line
12125                (fnd_file.log,'  Trans Cost    = '||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).resources(j).trans_cost,2)));
12126                fnd_file.put_line
12127                (fnd_file.log,' ');
12128                fnd_file.put_line
12129                (fnd_file.log,'    Cost Details for resource transaction '
12130                ||to_char(c_batch_details(p_batch_id).step_tab(i).resources(j).trans_id));
12131                fnd_file.put_line
12132                (fnd_file.log,'    ---------------------------------------------');
12133 
12134                IF c_batch_details(p_batch_id).step_tab(i).resources(j).cost_details.EXISTS(1) THEN
12135                  FOR k IN 1..c_batch_details(p_batch_id).step_tab(i).resources(j).cost_details.count
12136                  LOOP
12137                    fnd_file.put_line
12138                    (fnd_file.log,'    Component Class ID     = '
12139                    ||to_char(c_batch_details(p_batch_id).step_tab(i).resources(j).cost_details(k).cost_cmpntcls_id));
12140                    fnd_file.put_line
12141                    (fnd_file.log,'    Cost Analysis Code     = '
12142                    ||c_batch_details(p_batch_id).step_tab(i).resources(j).cost_details(k).cost_analysis_code);
12143                    fnd_file.put_line
12144                    (fnd_file.log,'    Cost Level             = '
12145                    ||to_char(c_batch_details(p_batch_id).step_tab(i).resources(j).cost_details(k).cost_level));
12146                    fnd_file.put_line
12147                    (fnd_file.log,'    Component Cost         = '
12148                    ||to_char(ROUND(c_batch_details(p_batch_id).step_tab(i).resources(j).cost_details(k).component_cost,2)));
12149                    fnd_file.put_line(fnd_file.log,' ');
12150                  END LOOP;
12151                ELSE
12152                  fnd_file.put_line(fnd_file.log,'    This resource has no costs');
12153                END IF;
12154              END LOOP;
12155            ELSE
12156              fnd_file.put_line(fnd_file.log,'  This step has no associated resources');
12157            END IF;
12158          END LOOP;
12159       END IF;
12160 
12161       fnd_file.put_line
12162       (fnd_file.log,'Leaving Procedure: '||procedure_name);
12163 
12164 END display_c_batch_details;
12165 
12166 END GMF_LOT_COSTING_PUB;