DBA Data[Home] [Help]

PACKAGE BODY: APPS.CTO_CONFIG_ITEM_PK

Source


1 package body CTO_CONFIG_ITEM_PK as
2 /* $Header: CTOCITMB.pls 120.27.12020000.3 2012/08/09 15:16:20 ntungare ship $ */
3 
4 /*----------------------------------------------------------------------------+
5 | Copyright (c) 1993 Oracle Corporation    Belmont, California, USA
6 |                       All rights reserved.
7 |                       Oracle Manufacturing
8 |
9 |FILE NAME   : CTOCITMB.pls
10 |
11 |DESCRIPTION : Creates new inventory item for CTO orders. Performs
12 |              the same functions as BOMLDCIB.pls and INVPRCIB.pls
13 |              for streamlined CTO supported with new OE architecture.
14 |
15 |HISTORY     : Created based on BOMLDCIB.pls  and INVPRCIB.pls
16 |              Created on 09-JUL-1999  by Usha Arora
17 |              01-31-2000 James Chiu   Modify mandatory components APIs
18 |	       02-14-2000 Sajani Sheth Code changes for visible demand flag update
19 |	       02-15-2000 Sajani Sheth Added code to support WIP parameter
20 |				       "Respond to sales order changes"
21 |	       06-01-2000 Sajani Sheth Added code to support
22 |				Multilevel/Multi-org CTO functionality
23 |              11-06-2000 Kiran Konada Added code to support
24 |                              Multilevel/Multi-org CTO functionality
25 |                              of changing visible demand flag IN
26 |                             BOM_CTO_ORDER_DEMAND table while creation
27 |                            of Configuration item
28 |              01/03/2001 Renga Kannan  Removed the raise statements in the
29 |                                       exception block. This fix is done as
30 |                                       part of the bug fix #1509712
31 |              02/02/2001 Renga Kannan  Added code to work for Change order
32 |                                       In the case of model cancelation delink should not
33 |                                       check for workflow status. Code is changed on 02/02/2001
34 |              02/03/2001 Renga Kannan  Modifed the code in delink_item.
35 |              03/13/2001 Renga Kannan  Added code for the notification part
36 |                                       This change is part of the Change order project
37 |              04/04/2001 Renga Kannan  Modified the link_item code for the performance issue.
38 |                                       This is part of the bug fix # 1690095. The process order call in
39 |                                       in link_item code is replaced with direct update statement.
40 |              05/30/2001 Sushant Sawant Modified the load_mandatory_comps and
41 |                                       load_mandatory_components procedure
42 |                                       for bug fix 1518894.
43 |              06-10-2001 SBhaskar     Bugfix 1811007
44 |                             Added code to calculate weight/volume
45 |                             of the configuration item in the shipping org.
46 |	       07-18-2001 Kundan Sarkar Bugfix 1876997 .
47 |				Add code to improve performance.
48 |				Modify query to replace an expensive view
49 |				(wsh_delivery_line_status_v) with its base tables.
50 |
51 |              08-24-2001 Sushant Sawant: BUG #1957336
52 |                             Added a new functionality for preconfigure bom.
53 |
54 |              08-31-01    Renga Kannan
55 |			      Modified the create_item procedure to call
56 |                             CTO_CUSTOM_LIST_PRICE_PK.get_list_price to calculate
57 |                             the price list for configuration items.
58 |                             For More details please look at Procuring config Phse I
59 |                             Design document.
60 |
61 |              09-05-2001 Kundan Sarkar Bugfix 1988939 ( Base Bug 1962820 ).
62 |				Truncate effectivity_date for correct selection of
63 |				eligible rows during bom_explosion.
64 |
65 |              09-11-2001 Kundan Sarkar Bugfix 1988946 ( Base bug 1968318 ).
66 |				No longer checking activity_label while selecting
67 |				activity_status_code of an item.
68 |
69 |              09-25-2001 Kundan Sarkar Bugfix 2034342 ( Base bug 1985793 ).
70 |				Checking greater of sysdate and calendar date while
71 |				checking for item effectivity so that planning will get
72 |				components effective till sysdate.
73 |
74 |              09-27-2001 Kundan Sarkar Bugfix 2034342 ( Base bug 1998386 ).
75 |				Passing PRIMARY_QUANTITY_UOM to ATP
76 |
77 |              10-02-2001 Kundan Sarkar Bugfix 2041612 ( Base bugs 2034419 and 1997355)
78 |				Calling MRP API MRP_OM_API_PK to insert the config items
79 |				line id during linking and delinking of config item.
80 |
81 |              10-29-2001 Kundan Sarkar Bugfix 2133816 ( Base bug 2047428 ).
82 |				Loading Mandatory components even if Top Model ATP components
83 |				flag is set to 'N'.Christine of Planning team has confirmed
84 |				that we should honor the ATP components flag of top model and
85 |				explosion need not take place if the ATP components flag on
86 |				model is set to 'N'. Fixed the code accordingly .
87 |
88 |              Modified on 08-JAN-2002 by Sushant Sawant: BUG #2172057
89 |                             Added a restriction for numbering method to be used while creating
90 |				configuration due to preconfigure bom limitation.
91 |
92 |              01-28-2002 Kundan Sarkar Bugfix 2202633 ( Base bug 2197842 ).
93 |					Config item created is not assigned to Purchasing default
94 |					category and hence cannot be entered in a purchase order.
95 |
96 |              01-28-2002 Kundan Sarkar Bugfix 2186114 ( Base bug 2162912 ).
97 |					In load_mandatory_comps and load_mandatory_components :
98 |					1) Truncate disable_date to correctly compare it with calendar_date.
99 |					2) Replace >= with > while comparing disable_date and calendar_date.
100 |					3) Replace to_date and to_char conversion functions for eff_date and
101 |					disable_date check since they are compared with calendar_date which
102 |					does not have timestamp.
103 |
104 |              03-08-2002 Sushant Sawant BUG#2234858
105 |					 Added new functionality for Drop Shipment
106 |                                        Changed functions link_item
107 |                                        Changed functions delink_item
108 |
109 |
110 |              03-25-2002 Renga Kannan  Removed the custom API call for list price.
111 |                                       This custom API will be called as part of List price rollup
112 |
113 |
114 |
115 |              03/31/2002 Renga Kannan  Bugfix 2288258 : Make the update visible_demand_flag statement more restrictive
116 					while it is performing update during delink action of ML/MO or BUY Model.
117 |
118 |              04/08/2002 Sushant Sawant BugFix 2300006 : Multiple Instantiation Usability Issues.
119 |                                        This fix will align each configuration next to the model
120 |                                        it corresponds to.
121 |
122 |              04-09-2002 Kundan Sarkar Bugfix 2292466 ( Base bug 2267646 ).
123 |                                       Added NVL function .
124 |
125 |              04-18-2002 Kundan Sarkar Bugfix 2337353 ( Base bug 2157740 ).
126 |					Copying Base Model attachement to Config item.
127 |
128 |
129 |              05-14-2002 Renga Kannan  Removed the attribute list_price_per_unit
130 |                                       from copying it to config item in mtl_system_items
131 |                                       Look at the bug details 2370307
132 |
133 |
134 |              06-03-2002 Sushant Sawant Bugfix 2401654 [duplicate of 2400948
135 |                                        and bug 2378556 ]
136 |                                        Changed query in load_mandatory_components
137 |                                        made changes to get only standard
138 |                                        mandatory components.
139 |
140 |              06-20-2002 Sushant Sawant Bugfix 2420865(a.k.a.BUG2428214)
141 |                                        get_mandatory_components was fixed to
142 |                                        handle arrival sets. In case of Arrival Sets
143 |                                        requested_arrival_date is populated instead of
144 |                                        requested_ship_date.
145 |
146 |              08-29-2002 Kundan Sarkar  Bugfix 2458338 ( Base bug 2395525 ).
147 |					 Not copying sales and Mktg category set
148 |					 from base model to config item.
149 |
150 |              08-29-2002 Kundan Sarkar  Bugfix 2454401 ( Base bug 2425667 ).
151 |                                        Infinite Loops in load_mandatory_comps and
152 |                                        load_mandatory_components during splitting of
153 |                                        an ATO model within a PTO with Tools - Debug
154 |                                        set to off.
155 |
156 |              08-29-2002 Kundan Sarkar  Bugfix 2541088 ( Base bug 2457514 ).
157 |					 Config item description picks up model desc.
158 |					 of base language instead of base and installed
159 |					 languages.
160 |
161 |              09-05-2002 Kundan Sarkar  Bugfix 2547219 ( Base bug 2461574 ).
162 |                                        Configured items are not inheriting transaction
163 |                                        defaults subinventory location from the model.
164 |
165 |              09-25-2002 Kundan Sarkar  Bugfix 2587307 ( Base bug 2576422 ).
166 |                                        Configured item weights are incorrect for MLMO
167 |                                        structure.
168 |
169 |              11-27-2002 Kundan Sarkar  Bugfix 2503104
170 |                                        Passing model's user_item_description to config
171 |                                        item .
172 |
173 |              12-09-2002 Kundan Sarkar  Bugfix 2701338 ( Base bug 2652379 )
174 |                                        Incorrect numbering sequence of configured items
175 |                                        in a multi - level structure.
176 |
177 |	      12-26-2002  Kiran Konada	bugfix 2727983
178 |					web_status filed is being copied for configuration item
179 |					from its base model.This field is a mandatory one for
180 					inventory from 11.5.9 onwards
181 |
182 |
183 |	     12-31-2002  Kiran Konada	bugfix2730055
184 |					insert into MTL_ITEM_REVISIONS was changed to MTL_ITEM_REVISIONS_B
185 |
186 |
187 |            01-23-2003 Kundan Sarkar   Bugfix 2503104
188 |                                       Revert fix as OM will populate user_item_description
189 |					of config item
190 |                                       Bugfix 2764811 ( In branch  2745590 )
191 |					Remove reference on flow schedule when config item
192 |					is delinked.
193 |
194 |            02-03-2003 Kundan Sarkar	Bugfix 2781022 ( In branch 2663450 )
195 |                                       New custom hook to generate custom item numbers for
196 |				        pre configured items and autocreated configurations.
197 |
198 |					Bugfix 2784045 (  no bug logged for main ...
199 |					Propagate this fix to main with fix of 2781022 )
200 |                                       Error in delink configuration when no routing is
201 |					defined for the model.
202 |
203 |
204 |	     02/04/2003 Kiran Konada
205 |						Added a new paramter to pass conifg/ato item id
206 |						to start_work_flow
207 |						bugfix 2782394
208 |
209 |              Modified on 14-FEB-2003  By Kundan Sarkar
210 |                                         Bugfix 2804321 : Propagating customer bugfix 2774570
211 |                                         and 2786934 to main.
212 |
213 |              Modified on 24-MAR-2003  By Kundan Sarkar
214 |                                         Bugfix 2867676 : Propagating customer bugfix 2858080
215 |                                         to main.
216 |
217 |              Modified on 14-APR-2003  By Kundan Sarkar
218 |                                         Bugfix 2904203 : Propagating customer bugfix 2898851
219 |                                         to main.
220 |
221 |              08-JUL-2003 Sushant Sawant Replicated Bugfix 2897132
222 |                                         (a.k.a. 2913695[actual aru]) to J Main as 3037613
223 |                                        Fixed bug related to get_mandatory_components
224 |                                        Following scenarios were addressed
225 |                                        1) pass correct line id for mandatory components
226 |                                        2) handle cancel case properly
227 |                                        3) handle delete case properly (respect params)
228 |                                        4) handle reschedule case properly
229 |
230 |
231 |              Modified on 14-MAR-2003 By Sushant Sawant
232 |                                         Decimal-Qty Support for Option Items.
233 |                                         Replicated 3037613 for mandatory
234 |                                         components
235 |                                         ( a.k.a 2913695, 2897132)
236 |
237 |              17-JUL-2003 Sushant Sawant Replicated Bugfix 2483920
238 |                                         using 3056491
239 |
240 |              30-DEC-2003 Kiran Konada   Bugfix
241 |                                         3340844
242 |                                         inserted value into revision_label
243 |                                         of mtl_item_revisions_b
244 |
245 |                                         3338108
246 |                                         a)  In 11.5.9 in the dynamic sql part
247 |                                         of inserting into mtl_item_revisions_b
248 |                                         we were inserting same revision_id for
249 |                                         an item in all orgs. Revision_id was a
250 |                                         null column
251 |
252 |                                         As items team has decide to create a
253 |                                         unique index on it, the revision_id needs
254 |                                         to be different in each org
255 |
256 |                                         b)  where condition for insert into mtl_item_revisions_tl
257 |                                         is also changed to get the corresponding revisionId
258 |                                         info from table mtl_item_revisions_b table
259 |
260 |                                         c) fixed by sawant
261 |                                         it is decided to leave blank the uom_code and revision_id
262 |                                         in mtl_cross_references
263 |
264 |              30-DEC-2003 Sushant Sawant Fixed Bug# 3358194
265 |                                         Weight/Vol calculations were not updated if model has no base uom.
266 |
267 |
268 |               ssawant   15-JAN-04   Bugfix 3379296
269 |               changes evaluate_atp_attributes for model attribute Y, N scenario.
270 |
271 |               KKONADA   02-03-2004   bugfix 2828588
272 |               Inserted the configuration data into table MTL_ABC_ASSIGNMENTS
273 |
274 |
275 |
276 |               KKONADA   03-02-2004  front port bugfix#3473737
277 |
278 |                                     branch Bugfix 3463999 : Updating config_id in bcol to null
279 |					  if config is matched.
280 |
281 |
282 |              KKONADA   03-15-2004   Corrected the comments
283 |                          bug#3340844 was entered for 3338108 at few places , corrected
284 |                         the error
285 |
286 |
287 |              Kkkonada  03-26-2004  Kiran Konada
288 |                        Bugfix 3536085
289 |                        assignment_group_id and inventory_item_id should be unique
290 |
291 |
292 |              Modified on 26-Mar-2004 By Sushant Sawant
293 |                                         Fixed Bug#3484511
294 |                                         all queries referencing oe_system_parameters_all
295 |                                         should be replaced with a function call to oe_sys_parameters.value
296 |
297 |
298 |              Modified on 23-Jun-2006  by Kiran Konada
299 |                                      Revreted bugfix 3473737, branch fix 3463999
300 |
301 |             modified on 01-jul-2004	kiran konada
302 |                                    	aru 3737772 (for FP 3473737)
303 |                                       added pconfigid parameter to ATO_WEIGHT_VOL api
304 |                                       and changed the de_code statment in wt_vol api
305 |
306 |
307 |
308 |              Modified on 18-APR-2005 By Sushant Sawant
309 |                                         Fixed Bug#4172300
310 |                                         Cost in validation org is not copied properly from model to config item.
311 |
312 |              Modified on 05-Jul-2005 by Renga Kannan
313 |                                         Change for MOAC
314 |                                         As per OM
315 s recommendation changing the process order Public API call to Group API call
316 for Link item
317 |
318 |
319 |             Modified on 08-Aug-2005  Kiran Konada
320 |                                      bug# 4539578
321 |                                      In R12, mtl_cross_references datamodel has been changed to
322 |				       mtl_cross_references_b and mtl_cross_references_tl
323 |
324 |             Modified on 15-Sep-2005  Renga Kannan
325 |                                      Made Code changes for ATG Performance
326 |                                      Project
327 |
328 |             Modified on 03-feb-2006  Kiran Konada
329 |                                      bugfix FP 4861996
330 |                                      should check if a specific item is present
331 |                                      in specificy category set
332 |
333 |               07-Mar-2006		Kiran Konada
334 |                                      performance  bug#4905845
335 |					Removed comments to reduce shared memory
336 |
337 +-----------------------------------------------------------------------------*/
338 
339 
340 PG_DEBUG Number := NVL(FND_PROFILE.value('ONT_DEBUG_LEVEL'), 0);
341 
342 g_atp_flag varchar2(1) := 'N' ;
343 
344 TYPE ATTRIB_NAME_TAB_TYPE is table of mtl_item_attributes.attribute_name%type index by binary_integer ;
345 
346 TYPE CONTROL_LEVEL_TAB_TYPE is table of mtl_item_attributes.control_level%type index by binary_integer ;
347 
348     g_attribute_name_tab    ATTRIB_NAME_TAB_TYPE ;
349     g_control_level_tab     CONTROL_LEVEL_TAB_TYPE ;
350 
351 
352 procedure create_preconfigured_item( p_line_id in number
353                          , p_model_id       in number
354                          , p_config_id       in number
355                          , p_lItemType        in varchar2 );
356 
357 
358 -- bugfix 2706981: forward declaration
359 FUNCTION check_dup_item(
360     pSegment1 	varchar2,
361     pSegment2 	varchar2,
362     pSegment3 	varchar2,
363     pSegment4 	varchar2,
364     pSegment5 	varchar2,
365     pSegment6 	varchar2,
366     pSegment7 	varchar2,
367     pSegment8 	varchar2,
368     pSegment9 	varchar2,
369     pSegment10 	varchar2,
370     pSegment11 	varchar2,
371     pSegment12 	varchar2,
372     pSegment13 	varchar2,
373     pSegment14 	varchar2,
374     pSegment15 	varchar2,
375     pSegment16 	varchar2,
376     pSegment17 	varchar2,
377     pSegment18 	varchar2,
378     pSegment19 	varchar2,
379     pSegment20 	varchar2)
380 RETURN number;
381 
382 -- bugfix 3026929: forward declaration
383 PROCEDURE populate_item_revision(pConfigId	number,
384 				 pModelId	number,
385 				 pLineId	number,
386 				 xReturnStatus	OUT NOCOPY varchar2);
387 
388 --Bugfix 9223554
389 PROCEDURE update_wt_vol_tbl(p_tbl_type number,
390                             p_line_id  number)
391 is
392    l_line_id            number := p_line_id;
393    l_ato_line_id        number;
394    i                    number;
395 begin
396    IF PG_DEBUG <> 0 THEN
397      oe_debug_pub.add('update_wt_vol_tbl: p_tbl_type:' || p_tbl_type);
398      oe_debug_pub.add('update_wt_vol_tbl: p_line_id:' || p_line_id);
399    END IF;
400 
401    if p_tbl_type = 1 then
402      --Mark all parents of this line as not eligible for processing.
403      loop
404         if not g_wt_tbl.exists(l_line_id) then
405           g_wt_tbl(l_line_id) := l_line_id;
406         end if;
407 
408 	select parent_ato_line_id, ato_line_id
409         into l_line_id, l_ato_line_id
410         from bom_cto_order_lines
411         where line_id = l_line_id;
412 
413         exit when l_line_id = l_ato_line_id;
414      end loop;
415 
416      --Inserting the top model line in the table.
417      if not g_wt_tbl.exists(l_line_id) then
418        g_wt_tbl(l_line_id) := l_line_id;
419      end if;
420 
421      IF PG_DEBUG <> 0 THEN
422        i := g_wt_tbl.first;
423        while i is not null
424        loop
425          IF PG_DEBUG <> 0 THEN
426 	   oe_debug_pub.add('update_wt_vol_tbl: Index' || i || ':' || g_wt_tbl(i));
427 	 END IF;
428 	 i := g_wt_tbl.next(i);
429        end loop;
430      END IF;
431 
432    elsif p_tbl_type = 2 then
433      --Mark all parents of this line as not eligible for processing.
434      loop
435         if not g_vol_tbl.exists(l_line_id) then
436           g_vol_tbl(l_line_id) := l_line_id;
437         end if;
438 
439 	select parent_ato_line_id, ato_line_id
440         into l_line_id, l_ato_line_id
441         from bom_cto_order_lines
442         where line_id = l_line_id;
443 
444         exit when l_line_id = l_ato_line_id;
445      end loop;
446 
447      --Inserting the top model line in the table.
448      if not g_vol_tbl.exists(l_line_id) then
449        g_vol_tbl(l_line_id) := l_line_id;
450      end if;
451 
452      IF PG_DEBUG <> 0 THEN
453        i := g_vol_tbl.first;
454        while i is not null
455        loop
456          IF PG_DEBUG <> 0 THEN
457 	   oe_debug_pub.add('update_wt_vol_tbl: Index' || i || ':' || g_vol_tbl(i));
458 	 END IF;
459 	 i := g_vol_tbl.next(i);
460        end loop;
461      END IF;
462    end if;
463 end update_wt_vol_tbl;
464 
465 
466 function get_attribute_control( p_attribute_name in varchar2)
467 return number
468 is
469 v_attribute_name varchar2(100);
470 begin
471    v_attribute_name := UPPER( p_attribute_name );
472 
473    for i in 1..g_attribute_name_tab.count
474    loop
475        if( g_attribute_name_tab(i) = v_attribute_name ) then
476            return g_control_level_tab(i);
477        end if;
478 
479    end loop ;
480 
481    return 0;
482 end get_attribute_control;
483 
484 
485 
486 function get_atp_flag
487 return char
488 is
489   v_atp_flag   Varchar2(1);
490 begin
491   v_atp_flag := g_atp_flag;
492   g_atp_flag := null;
493   return v_atp_flag ;
494 
495 /*
496 ** g_atp_flag has to be returned in a tricky way to avoid the sql statement from
497 ** using the value before evaluate_atp_attributes call
498 */
499 
500 end get_atp_flag;
501 
502 
503 function evaluate_atp_attributes( p_atp_flag in mtl_system_items_b.atp_flag%type
504                   , p_atp_components_flag in mtl_system_items_b.atp_components_flag%type )
505 return char
506 is
507 v_atp_components_flag   mtl_system_items_b.atp_components_flag%type ;
508 begin
509 
510     /* Note: Although a lot of similarity exists for some combination of flag values
511              the results are bound to change based on new functionality/bug fix issues.
512              This code will be easier to maintain if the logic appears more like a
513              truth table
514              The variable g_atp_flag is set to avoid making another call to get the
515              value for the second column.
516     */
517 
518    if( p_atp_flag = 'N' ) then
519 
520        if( p_atp_components_flag = 'N' ) then
521 
522            g_atp_flag := 'N' ;
523            v_atp_components_flag := 'N' ;
524 
525        elsif( p_atp_components_flag = 'Y' ) then
526 
527            g_atp_flag := 'Y' ;
528            v_atp_components_flag := 'Y' ;
529 
530        elsif( p_atp_components_flag = 'R' ) then
531 
532            g_atp_flag := 'Y' ;
533            v_atp_components_flag := 'R' ;
534 
535        elsif( p_atp_components_flag = 'C' ) then
536 
537            g_atp_flag := 'Y' ;
538            v_atp_components_flag := 'C' ;
539 
540        end if;
541 
542    elsif( p_atp_flag = 'Y') then
543 
544        if( p_atp_components_flag = 'N' ) then
545 
546            g_atp_flag := 'Y' ;
547            v_atp_components_flag := 'N' ;  -- fixed bug 3379296
548 
549        elsif( p_atp_components_flag = 'Y' ) then
550 
551            g_atp_flag := 'Y' ;
552            v_atp_components_flag := 'Y' ;
553 
554        elsif( p_atp_components_flag = 'R' ) then
555 
556            g_atp_flag := 'Y' ;
557            v_atp_components_flag := 'R' ;
558 
559        elsif( p_atp_components_flag = 'C' ) then
560 
561            g_atp_flag := 'Y' ;
562            v_atp_components_flag := 'C' ;
563 
564        end if;
565 
566 
567 
568    elsif( p_atp_flag = 'R' ) then
569 
570        if( p_atp_components_flag = 'N' ) then
571 
572            g_atp_flag := 'N' ;
573            v_atp_components_flag := 'N' ;  /* Doubtful please check again */
574 
575        elsif( p_atp_components_flag = 'Y' ) then
576 
577            g_atp_flag := 'Y' ;
578            v_atp_components_flag := 'Y' ;
579 
580        elsif( p_atp_components_flag = 'R' ) then
581 
582            g_atp_flag := 'Y' ;
583            v_atp_components_flag := 'R' ;
584 
585        elsif( p_atp_components_flag = 'C' ) then
586 
587            g_atp_flag := 'Y' ;
588            v_atp_components_flag := 'C' ;
589 
590        end if;
591 
592 
593 
594 
595    elsif( p_atp_flag = 'C' ) then
596 
597        if( p_atp_components_flag = 'N' ) then
598 
599            g_atp_flag := 'Y' ;
600            v_atp_components_flag := 'N' ;
601 
602        elsif( p_atp_components_flag = 'Y' ) then
603 
604            g_atp_flag := 'Y' ;
605            v_atp_components_flag := 'Y' ;
606 
607        elsif( p_atp_components_flag = 'R' ) then
608 
609            g_atp_flag := 'Y' ;
610            v_atp_components_flag := 'R' ;
611 
612        elsif( p_atp_components_flag = 'C' ) then
613 
614            g_atp_flag := 'Y' ;
615            v_atp_components_flag := 'C' ;
616 
617        end if;
618 
619 
620   end if ;
621 
622 
623 
624  oe_debug_pub.add( ' ************* EVALUATE_ATP_ATTRIBUTES ***  p_atp_flag ' || p_atp_flag
625                 || ' p_atp_components_flag ' || p_atp_components_flag
626                 || ' result ' || ' g_atp_flag ' || g_atp_flag
627                 || ' v_atp_components_flag ' || v_atp_components_flag  , 1 ) ;
628 
629 
630 
631 
632   return v_atp_components_flag  ;
633 
634 
635 
636 end evaluate_atp_attributes ;
637 
638 
639 
640 
641 
642 function get_cost_group( pOrgId  in number,
643                          pLineID in number)
644 return integer is
645 
646 l_cst_grp   number;
647 
648 begin
649 
650     /*--------------------------------------------+
651         This is a function to get cost_group_id
652         for using in insert to cst_quantity_layers
653     +---------------------------------------------*/
654 
655     select  nvl(costing_group_id,1)
656     into    l_cst_grp
657     from    pjm_project_parameters ppp
658     where   ppp.project_id = ( select  project_id
659                                from    oe_order_lines_all ol
660                                where   ol.line_id = pLineId )
661     and    ppp.organization_id = pOrgId;
662 
663     return(l_cst_grp);
664 
665 exception
666     when no_data_found then
667 	IF PG_DEBUG <> 0 THEN
668 		oe_debug_pub.add('get_cost_group: ' || 'ERROR: Could not fetch the cost_group_id from pjm_project_parameters (NDF)',1);
669 	END IF;
670 	return(0);
671 
672 end get_cost_group;
673 
674 
675 function get_validation_org ( opunit in number,
676                               profile_val in number)
677 return integer
678 is
679 
680 	lValue          fnd_profile_option_values.profile_option_value%type;
681 	loldvalue       fnd_profile_option_values.profile_option_value%type;
682 	lValidOrg       number;
683 
684 
685   	cursor validation_org is
686   	select  distinct POV.profile_option_value
687   	from    fnd_profile_options PO	,
688           	fnd_profile_option_values POV,
689           	fnd_responsibility FR,
690           	fnd_profile_options PO2,
691           	fnd_profile_option_values POV2
692    	where  PO.profile_option_name = 'SO_ORGANIZATION_ID'
693    	and    POV.application_id = PO.application_id
694    	and    POV.profile_option_id = PO.profile_option_id
695    	and    POV.level_id = 10003
696    	and    FR.application_id = POV.level_value_application_id
697    	and    FR.responsibility_id = POV.level_value
698    	and    PO2.profile_option_name = 'ORG_ID'
699    	and    POV2.application_id = PO2.application_id
700    	and    POV2.profile_option_id = PO2.profile_option_id
701    	and    POV2.level_id = 10003
702    	and    POV2.profile_option_value = to_char(opunit)
703    	and    POV2.level_value_Application_id = 660         -- ONT
704    	and    FR.application_id = POV2.level_value_application_id
705    	and    FR.responsibility_id = POV2.level_value;
706 
707  	multiorg_error      EXCEPTION;
708 
709 begin
710 
711    lOldValue := 0;
712    /*------------------------------------------------------+
713      Get the site level values for so_organization_id
714      for the OE responsibility of operating unit opunit.
715      Only one row should be returned. If no row is returned,
716      use the profile value (so_organization_id) of this
717      responsibility.
718    +-------------------------------------------------------*/
719 
720    open validation_org;
721 
722    fetch validation_org into lvalue;
723    lOldValue := lValue;
724 
725    while validation_org%found
726    loop
727        fetch validation_org into lvalue;
728        if validation_org%rowcount > 1 then
729           if lOldValue <> lvalue then
730                raise multiorg_error;
731           end if;
732        end if;
733    end loop;
734    if validation_org%rowcount = 0 then
735       lValidOrg :=  profile_val;
736    else
737       lValidOrg := to_number(lOldvalue);
738    end if;
739    close validation_org;
740    return (lValidOrg);
741 
742 exception
743   when others then
744      return(0);
745 
746 end get_validation_org;
747 
748 
749 
750 function flow_schedule_exists(
751          pLineId          in     number    ,
752          xErrorMessage    out NOCOPY   varchar2  ,
753          xMessageName     out NOCOPY   varchar2  ,
754          xTableName       out NOCOPY    varchar2  )
755 return boolean
756 is
757 
758     lWipEntityId   number;
759 
760 begin
761 
762     select wip_entity_id
763     into   lWipEntityId
764     from   wip_flow_schedules   wfs,
765            oe_order_lines_all   oel,
766            oe_order_headers_all oeh,
767            oe_transaction_types_all ota,
768            oe_transaction_types_tl  otl,
769            mtl_sales_orders     mso
770     where  wfs.demand_source_line   = oel.line_id    --config line id
771     and    oel.line_id              = pLineId
772     and    oeh.header_id            = oel.header_id
773     and    oeh.order_type_id        = ota.transaction_type_id
774     and    ota.transaction_type_code='ORDER'
775     and    ota.transaction_type_id  = otl.transaction_type_id
776     and    oeh.order_number         = mso.segment1
777     and    otl.name                 = mso.segment2
778     and    otl.language 	    = (select language_code
779 					from fnd_languages
780 					where installed_flag = 'B')
781     and    mso.sales_order_id       = wfs.demand_source_header_id
782     and    oel.inventory_item_id    = wfs.primary_item_id
783     and rownum = 1;
784 
785     IF PG_DEBUG <> 0 THEN
786     	oe_debug_pub.add ('flow_schedule_exists: ' || 'Flow Schedule Exists!', 1);
787     END IF;
788 
789     return(true);  -- Flow Schedule  exists
790 
791 exception
792     when no_data_found then
793 
794          IF PG_DEBUG <> 0 THEN
795          	oe_debug_pub.add ('flow_schedule_exists: ' || 'Flow Schedules does not exist ', 1);
796          END IF;
797          return(false);     -- Flow Schedule does not exist
798 
799     when  others then
800         xErrorMessage := 'ERROR: Flow Schedule exists for this line, Cannot delink';
801         xMessageName  := 'CTO_DELINK_ITEM_ERROR';
802         xTableName    := 'WIP_FLOW_SCHEDULES';
803         return(false);
804 
805 end flow_schedule_exists;
806 
807 ----------------------------------------
808 
809 function work_orders_exist (
810          pLineId          in     number    ,
811          xErrorMessage    out NOCOPY   varchar2  ,
812          xMessageName     out NOCOPY   varchar2  ,
813          xTableName       out NOCOPY   varchar2  )
814 return boolean
815 is
816 
817 lReserveId   number;
818 
819 begin
820 
821     select reservation_id
822     into   lReserveId
823     from   mtl_reservations     mr,
824            oe_order_lines_all   oel,
825            oe_order_headers_all oeh,
826            oe_transaction_types_all ota,
827            oe_transaction_types_tl  otl,
828            mtl_sales_orders     mso
829     where  mr.demand_source_line_id = oel.line_id    --config line id
830     and    oel.line_id              = pLineId
831     and    oeh.header_id            = oel.header_id
832     and    oeh.order_type_id        = ota.transaction_type_id
833     and    ota.transaction_type_code = 'ORDER'
834     and    ota.transaction_type_id  = otl.transaction_type_id
835     and    oeh.order_number         = mso.segment1
836     and    otl.name                 = mso.segment2
837     and    otl.language 	    = (select language_code
838 					from fnd_languages
839 					where installed_flag = 'B')
840     and    mso.sales_order_id       = mr.demand_source_header_id
841     and    mr.demand_source_type_id = decode(oeh.source_document_type_id, 10,
842 						INV_RESERVATION_GLOBAL.g_source_type_internal_ord,
843                                              	INV_RESERVATION_GLOBAL.g_source_type_oe)	--bugfix 1799874
844     and    mr.reservation_quantity  > 0
845     and supply_source_type_id     = INV_RESERVATION_GLOBAL.g_source_type_wip
846     and rownum = 1;
847 
848     IF PG_DEBUG <> 0 THEN
849     	oe_debug_pub.add ('work_orders_exist: ' || 'Work orders exist! Call wip_holds api', 2);
850     END IF;
851 
852     return(true);  -- reservation exists
853 
854 exception
855     when no_data_found then
856 
857          IF PG_DEBUG <> 0 THEN
858          	oe_debug_pub.add ('work_orders_exist: ' || 'Work orders do not exist, do not call wip holds api ', 2);
859          END IF;
860          return(false);     -- Reservation does not exist
861 
862     when  others then
863         xErrorMessage := 'ERROR: Failed in work_orders_exist ';
864         xMessageName  := 'CTO_DELINK_ITEM_ERROR';
865         xTableName    := 'MTL_RESERVATIONS';
866        return(false);
867 
868 end work_orders_exist;
869 
870 
871 
872 function delink_item (
873          pModelLineId     in     number    ,
874          pConfigId        in     number    ,
875          xErrorMessage    out NOCOPY    varchar2  ,
876          xMessageName     out NOCOPY   varchar2  ,
877          xTableName       out NOCOPY   varchar2  )
878 return integer
879 is
880 
881     lStmtNumber    		number;
882     lConfigLineId  		number;
883     lreserveId     		number;
884     lWfStat        		varchar2(80);
885     lCancel_flag   		varchar2(1) := 'N';
886     lCount         		number;
887     l_source_type  		number;
888 
889     -- The following flag is added by Renga Kannan on 03/13/2001
890 
891     l_resv_flag    		varchar2(1) := 'N';
892     pchgtype       		CTO_CHANGE_ORDER_PK.change_table_type;
893     l_order_number 		OE_ORDER_HEADERS_ALL.order_number%TYPE;
894     l_ml_mo_flag   		varchar2(1) := 'N';
895 
896     pAutoUnreserve 		varchar2(1);
897 
898     l_return_status           	varchar2(1) := FND_API.G_RET_STS_SUCCESS;
899     l_msg_count               	number;
900     l_msg_data                	varchar2(2000);
901     l_option_line_id	      	number;
902 
903     i 				number;
904     x				number;
905     y				varchar(1);
906     z				varchar(30);
907 
908     lOrgId			number;
909     lSalesOrderId		number;
910     lHeaderId			number;
911     lSourceCode			varchar2(30);
912 
913     l_rsv       		INV_RESERVATION_GLOBAL.mtl_reservation_rec_type;
914     l_rsv_id    		number;
915     l_dummy_sn  		INV_RESERVATION_GLOBAL.serial_number_tbl_type;
916     l_status    		varchar2(1);
917     lShipConfirmed		varchar2(10);
918 
919     --Begin Bugfix 1997355
920 
921     index_counter_delink	number;
922     p_config_line_arr_delink	MRP_OM_API_PK.line_id_tbl;
923     l_return_status_delink 	varchar2(1);
924     vLineId			number;
925 
926      --End Bugfix  1997355
927 
928      -- begin bugfix 2745590
929      lcfm_routing_flag		number;
930      lflow_schedule_exist	number;
931      -- end bugfix 2745590
932 
933     CURSOR reservations IS
934         select distinct reservation_id
935     	from   mtl_reservations     mr,
936            oe_order_lines_all   oel,
937            oe_order_headers_all oeh,
938            oe_transaction_types_all ota,
939            oe_transaction_types_tl  otl,
940            mtl_sales_orders     mso
941     	where  mr.demand_source_line_id = oel.line_id    --config line id
942     	and    oel.line_id              = lConfigLineId
943     	and    oeh.header_id            = oel.header_id
944     	and    oeh.order_type_id        = ota.transaction_type_id
945     	and    ota.transaction_type_code='ORDER'
946     	and    ota.transaction_type_id  = otl.transaction_type_id
947     	and    oeh.order_number         = mso.segment1
948     	and    otl.name                 = mso.segment2
949         and    otl.language 	        = (select language_code
950 	 				from fnd_languages
951 					where installed_flag = 'B')
952     	and    mso.sales_order_id       = mr.demand_source_header_id
953         and    mr.demand_source_type_id = decode(oeh.source_document_type_id, 10,
954 							INV_RESERVATION_GLOBAL.g_source_type_internal_ord,
955                                              		INV_RESERVATION_GLOBAL.g_source_type_oe)	--bugfix 1799874
956     	and    mr.reservation_quantity  > 0;
957 
958    --Begin Bugfix 1997355
959 
960    CURSOR delink_lines IS
961     	select line_id
962     	from oe_order_lines_all
963     	where ato_line_id = pModelLineId;
964 
965    --End Bugfix 1997355
966 
967       v_source_type_code   oe_order_lines_all.source_type_code%type ; /* BUG#2234858 */
968 
969       -- Added by Renga Kannan on 05/13/02
970       -- for bug fix 2366241
971 
972       l_header_id     oe_order_lines_all.header_id%type;
973 
974       v_aps_version   number := 0;
975 
976 
977       v_model_line_activity    varchar2(100) ;
978 
979 
980       v_appl_name  varchar2(200) ;
981       v_error_name varchar2(2000) ;
982 
983       --
984       -- bug 14474393
985       --
986       l_bypassatp_profile_value VARCHAR2(100);
987 BEGIN
988 
989    lShipConfirmed := 'FALSE';
990 
991 
992 
993    lStmtNumber := 400;
994    -- BUG#2234858
995    -- Modified by Sushant Sawant 02/14/2002. The source type for the model
996    -- needs to be retrieved to distinguish between drop shipped orders(external)
997    -- and regular orders( internal )
998 
999    select source_type_code into v_source_type_code
1000    from   oe_order_lines_all
1001    where  line_id = pModelLineId ;
1002 
1003 
1004 
1005 
1006    lStmtNumber := 500;
1007    -- Modified by Renga Kannan 02/03/2001. The condition checking for
1008    -- Cancelled_flag is removed . because delink is called even in the case of
1009    -- Cancelled lines.
1010 
1011    select line_id, ship_from_org_id,header_id
1012    into   lConfigLineId, lOrgId,l_header_id
1013    from   oe_order_lines_all oel
1014    where  ato_line_id = pModelLineId
1015    and    inventory_item_id = pConfigid
1016    and    item_type_code    = 'CONFIG';
1017 
1018    BEGIN
1019       --
1020       -- Do not allow delink if config line is ship confirmed
1021       -- A shipment line can be in 'open','packed','confirmed','in-transit'
1022       -- or 'closed' status. Allow delink only if 'OPEN'
1023       --
1024       lStmtNumber := 505;
1025 
1026       -- begin bugfix 2001824:
1027       -- To check if something has been "shipped" , we should check for released_status.
1028       -- If the released_status is either 'Y' (staged) or 'C' (closed), we should consider
1029       -- the line as shipped (partial or fully)
1030 
1031       select distinct 'TRUE'
1032       into   lShipConfirmed
1033       from   wsh_delivery_details_ob_grp_v wdd -- Added By Renga Kannan on 11/03/03 for wsh data model changes
1034       where  wdd.source_line_id = lConfigLineId
1035       and    wdd.source_code = 'OE'
1036       and    wdd.released_status in ('Y', 'C');		-- Staged [Y], Closed [C]
1037 
1038       if (lShipConfirmed = 'TRUE' ) then
1039    	IF PG_DEBUG <> 0 THEN
1040    		oe_debug_pub.add ('delink_item: ' || 'Error : Config Line has been ship confirmed. Can not delink',1);
1041    	END IF;
1042 	xMessageName    := 'CTO_DELINK_SHIPPING_ERROR';
1043 	raise FND_API.G_EXC_ERROR;
1044       end if;
1045 
1046    EXCEPTION
1047    	when no_data_found then
1048    		IF PG_DEBUG <> 0 THEN
1049    			oe_debug_pub.add ('delink_item: ' || 'Config Line does not exist in shipping yet. Delink allowed',2);
1050    		END IF;
1051 
1052    END;
1053    -- end bugfix 2001824
1054 
1055 
1056    lStmtNumber := 510;
1057    lSourceCode := fnd_profile.value('ONT_SOURCE_CODE');
1058    IF PG_DEBUG <> 0 THEN
1059    	oe_debug_pub.add ('delink_item: ' || 'lSourceCode is '||lSourceCode, 2);
1060    END IF;
1061 
1062    --
1063    -- Get header id, other info for calling Reservation API
1064    --
1065 
1066    select mso.sales_order_id,
1067         oel.ship_from_org_id,
1068 	oeh.header_id
1069    into   lSalesOrderId,	--header id fro rsv api
1070 	  lOrgId, 		--ship from org id
1071 	  lHeaderId		-- header id in oeh, for wip api
1072    from   oe_order_lines_all oel,
1073           oe_order_headers_all oeh,
1074 	  oe_transaction_types_tl oet,
1075           mtl_sales_orders mso,
1076           mtl_system_items msi
1077    where  oel.line_id = lConfigLineId
1078    and    item_type_code = 'CONFIG'
1079    and    oeh.header_id = oel.header_id
1080    and    oet.transaction_type_id = oeh.order_type_id
1081    and    mso.segment1 = to_char(oeh.order_number)
1082    and    mso.segment2 = oet.name
1083    and    oet.language = (select language_code
1084 			from fnd_languages
1085 			where installed_flag = 'B')
1086    and    mso.segment3 = lSourceCode
1087    and    oel.inventory_item_id = pConfigId
1088    and    msi.inventory_item_id = oel.inventory_item_id
1089    and    msi.organization_id = oel.ship_from_org_id
1090    and    msi.base_item_id is not NULL;
1091 
1092    IF PG_DEBUG <> 0 THEN
1093    	oe_debug_pub.add ('delink_item: ' || 'Before WIP parameter check', 2);
1094 
1095    	oe_debug_pub.add ('delink_item: ' || 'Config Line_id is ' || lConfigLineId, 2);
1096 
1097    	oe_debug_pub.add ('delink_item: ' || 'Config item_id is ' || pConfigId, 2);
1098 
1099    	oe_debug_pub.add ('delink_item: ' || 'org id is ' || lOrgId, 2);
1100 
1101    	oe_debug_pub.add ('delink_item: ' || 'Sales Order id is ' || lSalesOrderId, 2);
1102    END IF;
1103 
1104    -- begin bugfix 2745590 :  Check for routing flag : 1 = Flow Routing
1105 
1106 	lStmtNumber := 511;
1107 	-- 2784045 : Select stmt is enclosed in block to handle exception
1108    begin
1109 	select 	nvl(cfm_routing_flag, 2)
1110 	into 	lcfm_routing_flag
1111 	from 	bom_operational_routings
1112 	where 	assembly_item_id  = pConfigid
1113 	and 	organization_id  = lOrgId
1114 	and 	alternate_routing_designator is NULL;
1115    exception
1116    	 when no_data_found then
1117               lcfm_routing_flag :=2;
1118    end;
1119 	IF PG_DEBUG <> 0 THEN
1120 		oe_debug_pub.add ('delink_item: Routing flag ( if 1, flow routing ) = '||to_char(lcfm_routing_flag), 2);
1121         END IF;
1122 
1123 	-- Check if flow schedule exist
1124 	-- 2784045 : This 'if' condition is decoupled from flow_sch_exist to make the code more efficient
1125    if ( lcfm_routing_flag = 1 ) then
1126 	select 	count(*)
1127 	into 	lflow_schedule_exist
1128 	from 	wip_flow_schedules
1129 	where 	demand_source_line = to_char(lConfigLineId)
1130     	and 	primary_item_id    = pConfigid
1131     	and 	demand_source_type = 2 ;
1132 
1133     	IF PG_DEBUG <> 0 THEN
1134     		oe_debug_pub.add ('delink_item: Flow Schedule exist ( if > 0 ) = '||to_char(lflow_schedule_exist), 2);
1135         END IF;
1136 
1137     	-- Call MRP API unlink_order_line if flow schedule exist and routing is flow_routing
1138     	-- 2784045 if ( lcfm_routing_flag = 1 ) and ( lflow_schedule_exist > 0 ) then
1139     	if ( lflow_schedule_exist > 0 ) then
1140     		MRP_Flow_Schedule_PUB.unlink_order_line
1141     			(
1142 			p_api_version_number 	=> 1.0,
1143 			x_return_status 	=> l_status,
1144 			x_msg_count         	=> l_msg_count,
1145 			x_msg_data          	=> l_msg_data,
1146 			p_assembly_item_id	=> pConfigid ,
1147 			p_line_id		=> lConfigLineId
1148 			);
1149 
1150 		if l_status = FND_API.G_RET_STS_ERROR then
1151 			IF PG_DEBUG <> 0 THEN
1152      				oe_debug_pub.add ('delink_item: ' || 'Error: Failed in MRP_Flow_Schedule_PUB.unlink_order_line with expected error.', 1);
1153      			END IF;
1154 
1155                         /* Bugfix 2454788 */
1156 			raise FND_API.G_EXC_ERROR;
1157 
1158   		elsif l_status = FND_API.G_RET_STS_UNEXP_ERROR then
1159   			IF PG_DEBUG <> 0 THEN
1160      				oe_debug_pub.add ('delink_item: ' || 'Error: Failed in MRP_Flow_Schedule_PUB.unlink_order_line with unexpected error.', 1);
1161      			END IF;
1162      			xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1163 			raise FND_API.G_EXC_UNEXPECTED_ERROR;
1164 
1165 		elsif l_status = fnd_api.g_ret_sts_success then
1166 			IF PG_DEBUG <> 0 THEN
1167                			oe_debug_pub.add ('delink_item: ' || 'Removed config line reference from flow schedule', 2);
1168                 	END IF;
1169                 	-- Check for l_resv_flag. If it is "N" , set it to "Y" .
1170     			-- The Flag is set to Yes, so that we can call the notification later.
1171                         If l_resv_flag = 'N' then
1172                                	l_resv_flag := 'Y';
1173                         end if;
1174 		end if;
1175   	end if;		-- flow_sch > 0
1176   end if;		-- 2784045 cfm_routing_flag = 1
1177 
1178 	-- end bugfix 2745590
1179 
1180    lStmtNumber := 515;
1181 
1182 --
1183 -- will allow delink even if flow schedules exist
1184 --
1185 
1186 
1187    /*--------------------------------------------------------+
1188  	Get the Auto Unreserve flag
1189    +--------------------------------------------------------*/
1190 	--
1191 	-- The auto unreserve flag was being passed by OE in 11.0.
1192 	-- Auto Unrsv flag = 'N' => do not delink
1193 	-- Auto Unrsv flag = 'Y' => perform delink
1194 	-- The flag was always being passed as 'Y', so there is no
1195 	-- purpose of having a flag
1196 	--
1197 
1198 	/*----------------------------------+
1199 	   Get the WIP parameter
1200 	   "Respond to sales order changes"
1201 	+-----------------------------------*/
1202 	lStmtNumber := 517;
1203 
1204 
1205         /* BUG#2234858 Sushant Added this condition for drop ship project */
1206 
1207         if( v_source_type_code = 'INTERNAL' ) then
1208 
1209 	--call wip api if there are no work orders
1210 	if work_orders_exist( lConfigLineId,
1211          		xErrorMessage,
1212          		xMessageName,
1213          		xTableName)
1214         then
1215    		IF PG_DEBUG <> 0 THEN
1216    			oe_debug_pub.add ('delink_item: ' || 'work orders exist', 2);
1217    		END IF;
1218 
1219                 -- Added by Renga Kannan on 03/13/2001
1220                 -- The Flag is to Yes, So that we can call the notification later in this
1221                 -- Function.
1222                 l_resv_flag := 'Y';
1223 
1224 		WIP_SO_RESERVATIONS.respond_to_change_order(
1225 				p_org_id	=> lOrgId,
1226 				p_header_id	=> lSalesOrderId,
1227 				p_line_id	=> lConfigLineId,
1228 				x_status	=> l_return_status,
1229 				x_msg_count	=> l_msg_count,
1230     				x_msg_data	=> l_msg_data);
1231 
1232   		if l_return_status = FND_API.G_RET_STS_ERROR then
1233      			IF PG_DEBUG <> 0 THEN
1234      				oe_debug_pub.add ('delink_item: ' || 'Error: Failed in wip_so_reservations.respond_to_change_order with expected error.', 1);
1235      			END IF;
1236 
1237                         /* Bugfix 2454788 */
1238 			raise FND_API.G_EXC_ERROR;
1239 
1240   		elsif l_return_status = FND_API.G_RET_STS_UNEXP_ERROR then
1241      			IF PG_DEBUG <> 0 THEN
1242      				oe_debug_pub.add ('delink_item: ' || 'Error: Failed in wip_so_reservations.respond_to_change_order with unexpected error.', 1);
1243      			END IF;
1244 			xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1245 			raise FND_API.G_EXC_UNEXPECTED_ERROR;
1246   		end if;
1247 
1248 	else
1249 		IF PG_DEBUG <> 0 THEN
1250 			oe_debug_pub.add ('delink_item: ' || 'WOS does not exist', 2);
1251 		END IF;
1252 		l_return_status := FND_API.G_RET_STS_SUCCESS;
1253 	end if;
1254 
1255 	IF PG_DEBUG <> 0 THEN
1256 		oe_debug_pub.add ('delink_item: ' || 'Resp to SO changes returned' || l_return_status, 2);
1257 
1258 		oe_debug_pub.add ('delink_item: ' || 'Resp to SO changes returned with msg : ' || l_msg_data, 2);
1259 	END IF;
1260 
1261 	lStmtNumber := 518;
1262 	if (l_return_status = FND_API.G_RET_STS_SUCCESS) then
1263 
1264 		OPEN reservations;
1265     		LOOP
1266     			FETCH reservations into l_rsv.reservation_id;
1267     			EXIT when reservations%NOTFOUND;
1268 
1269 			-- call INV delete_reservations API
1270      			INV_RESERVATION_PUB.delete_reservation
1271 				(
1272         			p_api_version_number  => 1.0
1273 	      			, p_init_msg_lst      => fnd_api.g_true
1274 	   			, x_return_status     => l_status
1275 	   			, x_msg_count         => l_msg_count
1276 	   			, x_msg_data          => l_msg_data
1277 			   	, p_rsv_rec           => l_rsv
1278    				, p_serial_number     => l_dummy_sn
1279 				);
1280 
1281 			IF PG_DEBUG <> 0 THEN
1282 				oe_debug_pub.add ('delink_item: ' || 'After deleting rsv, rsv_id = '||to_char(l_rsv.reservation_id)||', l_status = '||l_status, 2);
1283 			END IF;
1284 			IF l_status = fnd_api.g_ret_sts_success THEN
1285 				IF PG_DEBUG <> 0 THEN
1286 					oe_debug_pub.add ('delink_item: ' || 'reservations deleted', 2);
1287 				END IF;
1288                                 -- Added by Renga Kannan on 03/13/2001
1289                                 -- The Flag is to Yes, So that we can call the notification later in this
1290                                 -- Function.
1291                                 l_resv_flag := 'Y';
1292 
1293 			ELSE
1294        				IF l_msg_count = 1 THEN
1295 					IF PG_DEBUG <> 0 THEN
1296 						oe_debug_pub.add ('delink_item: ' || 'Error in rsv delete', 1);
1297 
1298 						oe_debug_pub.add ('delink_item: ' || 'l_msg_data ='||l_msg_data, 1);
1299 					END IF;
1300 
1301 					CLOSE reservations;
1302 
1303 					xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1304 					raise FND_API.G_EXC_ERROR;
1305 
1306         			ELSE
1307   					FOR l_index IN 1..l_msg_count LOOP
1308 					  IF PG_DEBUG <> 0 THEN
1309 					  	oe_debug_pub.add ('delink_item: ' || 'Error in rsv delete', 1);
1310 
1311 					  	oe_debug_pub.add ('delink_item: ' || 'l_msg_data ='||l_msg_data, 1);
1312 					  END IF;
1313    					END LOOP;
1314 
1315 					CLOSE reservations;
1316 
1317 					xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1318 					raise FND_API.G_EXC_ERROR;
1319         			END IF;
1320      			END IF;
1321     		END LOOP;
1322     		CLOSE reservations;
1323         	IF PG_DEBUG <> 0 THEN
1324         		oe_debug_pub.add ('delink_item: ' || 'After reservations cursor loop', 2);
1325         	END IF;
1326 
1327 	end if; /* end check l_return_status */
1328 
1329            -- rkaza. ireq project. 05/13/2005.
1330            -- Delete any records in req interface for this config line.
1331 
1332            cto_change_order_pk.delete_from_req_interface(
1333                 p_line_id => lConfigLineId,
1334                 p_item_id => pConfigId,
1335                 x_return_status => l_return_status ) ;
1336 
1337            if l_Return_Status = FND_API.G_RET_STS_UNEXP_ERROR then
1338               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1339            elsif l_Return_Status = FND_API.G_RET_STS_ERROR then
1340               raise FND_API.G_EXC_ERROR;
1341            end if;
1342 
1343         end if ; /* source_type_code = 'INTERNAL' */
1344 
1345 
1346 
1347 
1348 
1349 
1350                 /* BUG#2234858 Sushant added the end if statement for drop ship project */
1351 
1352 
1353 		--delink
1354 
1355                 -- Added by Renga Kannan on 02/02/2001
1356                 -- Before Checking for workflow status for model check if the
1357                 -- the model line is canceled. If canceled we should not check the
1358                 -- work flow status as the status is already modified by OM.
1359 
1360 		/*-----------------------------------------+
1361 		Getting the activity status code
1362 		+-----------------------------------------*/
1363                 SELECT  nvl(cancelled_flag,'N')
1364                 INTO    lcancel_flag   --- Reusing the variable
1365                 FROM    OE_ORDER_LINES_ALL
1366                 WHERE   line_id = pModelLineId;
1367 
1368    		lStmtNumber := 520;
1369 
1370                 IF lcancel_flag <> 'Y'  THEN
1371                    IF PG_DEBUG <> 0 THEN
1372                    	oe_debug_pub.add('delink_item: ' || 'The model line is not cancelled',2);
1373                    END IF;
1374 
1375 
1376                    begin
1377 
1378 
1379 		   select activity_status
1380     		   into   lWfStat
1381     		   from   wf_item_activity_statuses was
1382     		   where  was.item_type      = 'OEOL'
1383     		   and    was.item_key       = to_char(pModelLineId)
1384 		   and was.activity_status = 'NOTIFIED'
1385     		   and    was.process_activity in
1386 			(SELECT wpa.instance_id
1387 			FROM  wf_process_activities wpa
1388 	 		WHERE wpa.activity_name = 'WAIT_FOR_CTO');
1389 
1390 
1391                    v_model_line_activity := 'WAIT_FOR_CTO' ;
1392 
1393 
1394                    exception
1395                    when no_data_found then
1396 
1397 
1398                         v_aps_version := msc_atp_global.get_aps_version  ;
1399 
1400                         oe_debug_pub.add('link_config: ' || 'APS version::'|| v_aps_version , 2);
1401 
1402                         if( v_aps_version <> 10 ) then
1403 
1404                            raise no_data_found ;
1405 
1406                         else
1407 
1408                             oe_debug_pub.add('link_config: ' || 'Model Line workflow need not be at WAIT_FOR_CTO as APS version::'|| v_aps_version , 2);
1409 
1410                             v_model_line_activity := 'NOT_AT_WAIT_FOR_CTO' ;
1411 
1412 
1413                         end if ;
1414 
1415                    when others then
1416 
1417                        raise ;
1418 
1419                   end ;
1420 
1421 
1422                 ELSE
1423                    IF PG_DEBUG <> 0 THEN
1424                    	oe_debug_pub.add('delink_item: ' || 'Model Line is cancelled..',2);
1425                    END IF;
1426                 END IF;
1427 
1428     		IF PG_DEBUG <> 0 THEN
1429     			oe_debug_pub.add ('delink_item: ' || 'calling OE_CONFIG_UTIL.delink_config',2);
1430     		END IF;
1431 
1432     		/*------------------------------------------------------+
1433        			We are not calling prcess_order api directly because
1434        			OM needs to differentiate a delink request by this
1435        			function and a delete line request from form and
1436        			over-ride or honor security constraints accordingly.
1437        			That can be done by the private api but not with
1438        			the public process order api
1439      		+------------------------------------------------------*/
1440 
1441                 lstmtNumber := 523;
1442 
1443                 -- This Part of the code is added by Renga Kannan on 03/13/2001
1444                 -- This needs to be passed to the notification
1445                 -- procedure later in this function.
1446 
1447                 l_ml_mo_flag := 'Y' ; /* Notifications should be sent for delink configuration in all scenarios */
1448 
1449     		lStmtNumber := 530;
1450 
1451 
1452 
1453 
1454 
1455     		OE_CONFIG_UTIL.delink_config(
1456 				p_line_id	=> lConfigLineId,
1457 				x_return_status	=> l_return_status);
1458 
1459     		IF PG_DEBUG <> 0 THEN
1460     			oe_debug_pub.add ('delink_item: ' || 'oe_config_util.delink_config returned' || l_return_status ,1);
1461     		END IF;
1462 
1463                 IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1464 
1465                   IF PG_DEBUG <> 0 THEN
1466                         oe_debug_pub.add('delink_item: ' || 'Expected error in OE_CONFIG_UTIL.delink_config procedure....',3);
1467                   END IF;
1468 
1469                  /* Bugfix 2454788 */
1470                   raise FND_API.G_EXC_ERROR;
1471 
1472 
1473                 ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1474     			IF PG_DEBUG <> 0 THEN
1475     				oe_debug_pub.add ('delink_item: ' || 'Unexpected Error in oe_config_util.delink_config.');
1476     			END IF;
1477 
1478 		  	xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1479                   	raise FND_API.G_EXC_UNEXPECTED_ERROR;
1480 
1481     		END IF;
1482 
1483 
1484 
1485     		oe_globals.g_cascading_request_logged := TRUE;
1486 
1487 
1488                 /* BUG#2234858
1489                 **  Need to allow the following functionality for non drop shipped items
1490                 */
1491 
1492                 if( v_source_type_code = 'INTERNAL' ) then
1493 
1494 
1495 
1496     		--
1497     		-- update visible demand flag to 'Y' for model and options
1498     		--
1499 
1500     		IF PG_DEBUG <> 0 THEN
1501     			oe_debug_pub.add ('delink_item: ' || 'delink : visible demand flag : before selecting options ', 2);
1502     		END IF;
1503 
1504     		lStmtNumber    := 533;
1505 
1506                 --- Added by Renga Kannan on 02/02/2001
1507                 --- We have decided not to call process order API to change
1508                 --- Flag setting. Instead we will update the flags in OE_ORDER_LINES_ALL
1509                 --- directly. This decision is taken during change order project.
1510                 --- Updating OE_ORDE_LINES_ALL directly instead of calling Process order
1511                 --- API was recommended by Rajeev Bellamkonda from OM team. Date 02/02/2001
1512 
1513 
1514     		IF PG_DEBUG <> 0 THEN
1515     			oe_debug_pub.add ('delink_item: ' || 'l_ml_mo_flag = '|| l_ml_mo_flag, 2);
1516     		END IF;
1517 
1518 
1519 
1520                 /* Changes related to Patchset J */
1521 		-- Fixed bug 5470466
1522 		-- We should not update visible demand flag to 'Y' for
1523 		-- unscheduled orders. Added a condition to check
1524 		-- if the line is scheduled or not
1525 
1526                    --
1527                    -- Bug 14474393
1528                    -- The visible demand flag has to be updated only if the profile
1529                    -- OM:Bypass ATP is not Y
1530                    --
1531 		   FND_PROFILE.Get('ONT_BYPASS_ATP', l_bypassatp_profile_value);
1532 
1533                    IF PG_DEBUG <> 0 THEN
1534                         oe_debug_pub.add('delink_item: Value of the profile ONT_BYPASS_ATP :' ||l_bypassatp_profile_value ,3);
1535                    END IF;
1536 
1537                    IF (NVL(l_bypassatp_profile_value,'N') <> 'Y') THEN
1538                        UPDATE  OE_ORDER_LINES_ALL
1539                          SET     visible_demand_flag = 'Y'
1540                        WHERE   ato_line_id = pModelLineId
1541                        and     header_id = l_header_id
1542                        and     open_flag = 'Y'
1543 		       and     schedule_status_code is not null; -- 5470466
1544                    END IF;
1545 
1546 
1547                 end if ; /* source_type_code = 'INTERNAL' */
1548 
1549                 /* BUG#2234858 Modified by Sushant for Drop SHip project */
1550 
1551 
1552 
1553 
1554 
1555     		lStmtNumber := 540;
1556                 -- What do we do in case of cancellation
1557 
1558 
1559                 oe_debug_pub.add( 'model line status ' || v_model_line_activity , 1 ) ;
1560 
1561 
1562 
1563 
1564                 IF lcancel_flag <> 'Y' and v_model_line_activity = 'WAIT_FOR_CTO'   THEN --- If it is not cancellation
1565     		   wf_engine.CompleteActivityInternalName('OEOL',
1566                                                          to_char(pModelLineId),
1567                                                          'WAIT_FOR_CTO',
1568                                                          'DE_LINK');
1569 	        END IF;
1570 
1571 
1572 
1573 
1574 
1575 
1576 
1577 	--
1578 	-- deleting from bom_cto_order_lines and bom_cto_src_orgs
1579 	-- for this top ato line id
1580 	--
1581 
1582 	lStmtNumber := 550;
1583 
1584         /* BUG#2234858 Sushant Modified this code for Drop Ship Project */
1585 
1586         /* Changes for Patchset J
1587            added reference to package msc_atp_global
1588            pre-req aru 3111677
1589         */
1590 
1591         v_aps_version := msc_atp_global.get_aps_version  ;
1592 
1593         if( v_aps_version <> 10) then
1594 		oe_debug_pub.add('delink_item: ' || 'APS version::'|| v_aps_version , 2);
1595 
1596         if( v_source_Type_code = 'INTERNAL') then
1597 
1598 
1599 
1600         IF lcancel_flag <> 'Y' THEN --- If it is not cancellation
1601 
1602            lStmtNumber := 570;
1603 
1604            --to delete the row corresponding to CID in BCOD table
1605            delete from bom_cto_order_demand
1606            where ato_line_id=pModelLineId
1607            and   inventory_item_id=pConfigId;
1608 
1609            --updating the demand_visible flag to yes in BCOD table CID removal
1610            lStmtNumber := 580;
1611 
1612            update bom_cto_order_demand
1613            set demand_visible = 'Y'
1614            where ato_line_id =pModelLineId;
1615 
1616         END IF;
1617 
1618 
1619 
1620         end if ;
1621 
1622         else
1623 
1624 		oe_debug_pub.add('delink_item: ' || 'APS version::'|| v_aps_version , 2);
1625 
1626         end if; /* reference to get_aps_version */
1627 
1628 
1629 
1630 
1631         /* BUG#2234858 Sushant Modified this code for Drop Ship Project */
1632 
1633         -- The following block of code is added by Renga Kannan on 03/13/2001.
1634         -- When Customer delinks an config item that needs to be notified to the
1635         -- Planner if Reservation exists for this item. But this de-link code
1636         -- Can be get called from the change_order package itself. In this case the
1637         -- Change order pkg will send the notification. This procedure need not do anything.
1638         -- This will be accomplished by the following logic. Whenever Delink_function is called
1639         -- from CTO_CHANGE_ORDER_PK the package variable CHANGE_ORDER_PK_STATE is set to 1. Otherwise
1640         -- it is defaulted to 0. So in this function we will call the notification only if this
1641         -- Pkg variable value is zero and l_resv_flag is set to 'Y'. This flag is set to 'Y' in the
1642         -- Begining part of this function.
1643 
1644         lStmtNumber := 590;
1645 
1646         /* BUG#2234858 Sushant Modified this code for Drop Ship Project
1647         ** No notifications should be sent for drop shipped items
1648         */
1649         IF ( CTO_CHANGE_ORDER_PK.change_order_pk_state = 0
1650         and ( l_resv_flag = 'Y' or l_ml_mo_flag = 'Y')
1651         and v_source_type_code = 'INTERNAL' )
1652         THEN
1653 
1654            IF PG_DEBUG <> 0 THEN
1655            	oe_debug_pub.add('delink_item: ' || 'Delink Action performed, Need to call notification...',2);
1656            END IF;
1657            lStmtNumber := 600;
1658            SELECT Order_number
1659            INTO   l_order_number
1660            FROM   OE_ORDER_HEADERS_ALL A,
1661                   OE_ORDER_LINES_ALL   B
1662            WHERE  B.line_id = pModelLineId
1663            AND    A.Header_id  = B.Header_Id;
1664 
1665            IF PG_DEBUG <> 0 THEN
1666            	oe_debug_pub.add('delink_item: ' || 'Calling Notification Pkg',2);
1667 
1668            	oe_debug_pub.add('delink_item: ' || 'Order Number = '||to_char(l_order_number),3);
1669 
1670            	oe_debug_pub.add('delink_item: ' || 'Line id      = '||to_char(pModelLineId),3);
1671            END IF;
1672 
1673            pchgtype(1).change_type := CTO_CHANGE_ORDER_PK.DELINK_ACTION;
1674            pchgtype(1).old_value   := null;
1675            pchgtype(1).new_value   := null;
1676 
1677            lStmtNumber := 610;
1678            CTO_CHANGE_ORDER_PK.Start_work_flow(
1679                                porder_no       => l_order_number,
1680                                pline_no        => pModelLineId,
1681                                pchgtype        => pchgtype,
1682                                pmlmo_flag      => l_ml_mo_flag,
1683 			       pConfig_Id      => pConfigId,	 --bugfix 2782394
1684 			       puser_name      => null, -- Added for bug 14065108
1685                                x_return_status => l_return_status,
1686                                x_msg_count     => l_msg_count,
1687                                x_msg_data      => l_msg_data);
1688 
1689 
1690             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1691                IF PG_DEBUG <> 0 THEN
1692                	oe_debug_pub.add('delink_item: ' || 'Expected error in change_notify procedure....',1);
1693                END IF;
1694 
1695 
1696                  /* Bugfix 2454788 */
1697                raise FND_API.G_EXC_ERROR;
1698 
1699             ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1700                IF PG_DEBUG <> 0 THEN
1701                	oe_debug_pub.add('delink_item: ' || 'Unexpected error occurred in change_notify...',1);
1702                END IF;
1703 	       xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1704                raise FND_API.G_EXC_UNEXPECTED_ERROR;
1705 
1706             END IF;
1707 
1708         END IF;
1709 
1710         --Begin Bugfix 1997355
1711 
1712         lStmtNumber := 611;
1713 
1714 	index_counter_delink := 1;
1715 	OPEN delink_lines;
1716         /* creating array of lines where ato_line_id = model line id */
1717 	LOOP
1718 		FETCH delink_lines into vLineId;
1719 		EXIT WHEN delink_lines%NOTFOUND;
1720 		p_config_line_arr_delink(index_counter_delink) := vLineId ;
1721 		index_counter_delink := index_counter_delink + 1 ;
1722 	END LOOP;
1723 
1724 
1725 
1726 	/* Since oe_config_util.delink_config deleted lConfigLineId from oe_order_lines_all before
1727 	   opening delink_lines cursor , lConfigLineId needs to be passed explicitely */
1728 
1729 	p_config_line_arr_delink(index_counter_delink) := lConfigLineId;
1730 	CLOSE delink_lines;
1731 
1732 	MRP_OM_API_PK.MRP_OM_Interface
1733 			(p_line_tbl		=> p_config_line_arr_delink,
1734 			 x_return_status	=> l_return_status_delink);
1735 
1736     	IF PG_DEBUG <> 0 THEN
1737     		oe_debug_pub.add ('delink_item: ' || 'Return status from MRP_OM_Interface - Delink: '||l_return_status_delink,2);
1738     	END IF;
1739 
1740   	if l_return_status_delink = FND_API.G_RET_STS_ERROR then
1741      		IF PG_DEBUG <> 0 THEN
1742      			oe_debug_pub.add ('delink_item: ' || 'Failed in MRP_OM_API_PK.mrp_om_interface with expected error.', 1);
1743      		END IF;
1744 
1745                  /* Bugfix 2454788 */
1746 
1747 		raise FND_API.G_EXC_ERROR;
1748 
1749   	elsif l_return_status_delink = FND_API.G_RET_STS_UNEXP_ERROR then
1750      		IF PG_DEBUG <> 0 THEN
1751      			oe_debug_pub.add ('delink_item: ' || 'Failed in MRP_OM_API_PK.mrp_om_interface with unexpected error.', 1);
1752      		END IF;
1753 
1754 
1755 		xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1756 		raise FND_API.G_EXC_UNEXPECTED_ERROR;
1757 
1758   	end if;
1759 
1760     	--End Bugfix 1997355
1761 
1762 	return(1);
1763 
1764     EXCEPTION
1765 
1766 	when NO_DATA_FOUND then
1767 		IF PG_DEBUG <> 0 THEN
1768 			oe_debug_pub.add('delink_item: ' || 'ERROR: CTOCITMB delink::ndf::'||to_char(lStmtNumber)||sqlerrm, 1);
1769 		END IF;
1770                 if lStmtNumber = 520 then
1771                    xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1772                    xErrorMessage := 'ERROR: CTOCITMB.delink:'||to_char(lStmtNumber)||
1773                                      ':' || 'WAIT_FOR_CTO activity is not Notified' ;
1774 		else
1775                    xErrorMessage := 'ERROR: CTOCITMB.delink:'||to_char(lStmtNumber)||
1776                                    ':' || 'Can not find Config Line';
1777                    xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1778 		end if;
1779                 return(0);
1780 
1781 
1782 	when FND_API.G_EXC_ERROR then
1783 
1784              if( xMessageName is null ) then /* xMessageName can be not null in case of CTO_DELINK_SHIPPING_ERROR */
1785                  /* Bugfix 2454788 */
1786                  CTO_MSG_PUB.Count_And_Get(
1787                   p_msg_count => lCount,
1788                   p_msg_data  => xErrorMessage
1789                 );
1790                 IF PG_DEBUG <> 0 THEN
1791                 	oe_debug_pub.add ('delink_item: ' || 'ERROR: CTOCITMB.delink:' || to_char(lStmtNumber)|| xErrorMessage,1);
1792                 END IF;
1793 
1794 
1795                 /* Bugfix 2454788 */
1796                 /* The current Delink Architecture is dependent on passing the error message in the xMessageName variable.*/
1797 
1798                 if( xErrorMessage is not null ) then
1799                     fnd_message.parse_encoded( xErrorMessage , v_appl_name, v_error_name ) ;
1800 
1801 
1802                     IF PG_DEBUG <> 0 THEN
1803                 	oe_debug_pub.add ('delink_item: ' || 'ERROR: CTOCITMB.delink: appl_name ' || v_appl_name ,1);
1804                 	oe_debug_pub.add ('delink_item: ' || 'ERROR: CTOCITMB.delink: error_name ' || v_error_name ,1);
1805                     END IF;
1806 
1807 
1808                     xErrorMessage := v_error_name;
1809                     xMessageName := v_error_name;
1810 
1811                 else
1812 
1813 
1814                     xMessageName := 'CTO_DELINK_ITEM_ERROR' ;
1815                     IF PG_DEBUG <> 0 THEN
1816                 	oe_debug_pub.add ('delink_item: ' || 'ERROR: CTOCITMB.delink: xErrorMessage is null ' ,1);
1817                 	oe_debug_pub.add ('delink_item: ' || 'ERROR: CTOCITMB.delink: xMessageName is now ' || xMessageName,1);
1818                     END IF;
1819 
1820 
1821                 end if ;
1822 
1823              else
1824                     IF PG_DEBUG <> 0 THEN
1825                 	oe_debug_pub.add ('delink_item: ' || 'ERROR: CTOCITMB.delink: xMessageName was already set ' || xMessageName,1);
1826                     END IF;
1827              end if;
1828 
1829              return(0);
1830 
1831 
1832 	when FND_API.G_EXC_UNEXPECTED_ERROR then
1833                  CTO_MSG_PUB.Count_And_Get(
1834                   p_msg_count => lCount,
1835                   p_msg_data  => xErrorMessage
1836                 );
1837                 IF PG_DEBUG <> 0 THEN
1838                 	oe_debug_pub.add ('delink_item: ' || 'ERROR: CTOCITMB.delink:' || to_char(lStmtNumber)|| xErrorMessage,1);
1839                 END IF;
1840                 return(0);
1841 
1842 
1843 	when OTHERS then
1844 		IF PG_DEBUG <> 0 THEN
1845 			oe_debug_pub.add('delink_item: ' || 'delink::others::'||sqlerrm, 1);
1846 		END IF;
1847 		xMessageName    := 'CTO_DELINK_ITEM_ERROR';
1848                 xErrorMessage := 'ERROR: CTOCITMB.delink:'||to_char(lStmtNumber)||
1849                                    ':' || substrb(sqlerrm,1,150);
1850                  CTO_MSG_PUB.Count_And_Get(
1851                   p_msg_count => lCount,
1852                   p_msg_data  => xErrorMessage
1853                 );
1854                 return(0);
1855 
1856 end delink_item;
1857 
1858 
1859 function load_mandatory_comps(
1860          pLineId          in     number,
1861          pReqDate         in     oe_order_lines_all.schedule_ship_date%type,
1862          xGrpId           out NOCOPY   number,
1863          xErrorMessage    out NOCOPY   varchar2  ,
1864          xMessageName     out NOCOPY   varchar2  ,
1865          xTableName       out NOCOPY   varchar2  )
1866 return integer
1867 is
1868 
1869 	level_number          		number := 0;
1870 	v_max_level_number    		number := 0;
1871 	lStmtNumber           		number := 0;
1872 	lGrpId                		number;
1873 	rowcount              		number := 1;
1874 	proc_error            		exception ;
1875 
1876 	v_msi_fixed_lead_time       	mtl_system_items.fixed_lead_time%type ;
1877 	v_msi_variable_lead_time    	mtl_system_items.variable_lead_time%type ;
1878 
1879 	/*Bugfix 2047428 */
1880 	latpcompflag            	mtl_system_items.atp_components_flag%type;
1881 	/* End Bugfix 2047428 */
1882 
1883 	-- Bugfix 2425667
1884 	lrowcount number;
1885 	-- Bugfix 2425667
1886 
1887 	-- 3893281 : New debug cursor and variables
1888         cursor debug_bet is
1889         select bet.top_bill_sequence_id,
1890                bet.bill_sequence_id,
1891                bet.organization_id,
1892                bet.sort_order,
1893                bet.plan_level,
1894                bet.line_id,
1895                substrb(msi.concatenated_segments,1,50)
1896         from   bom_explosion_temp bet,mtl_system_items_kfv msi
1897         where  bet.group_id = xGrpId
1898         and    bet.organization_id = msi.organization_id
1899         and    bet.component_item_id = msi.inventory_item_id;
1900 
1901         d_top_bseq_id           number;
1902         d_bseq_id               number;
1903         d_org_id                number;
1904         d_sort_order            varchar2(240);
1905         d_plan_level            number;
1906         d_line_id               number;
1907         d_item                  varchar2(50);
1908 
1909         -- 3893281
1910 
1911 begin
1912 
1913     /*---------------------------------------------------+
1914        Insert the Model row details from oe_order_lines
1915        with plan_level =0
1916        The lines have a common group_id, have line_id reference
1917        of oe_order_lines. Top_bill_sequence_id and bill_sequence_id
1918        are not needed however, as they are not null coloumns,
1919        all rows have model's bill_sequence_id in these fields.
1920     +----------------------------------------------------*/
1921 
1922 
1923 
1924         IF PG_DEBUG <> 0 THEN
1925         	oe_debug_pub.add ('load_mandatory_comps: ' || ' load mandatory comps ' ,1);
1926         END IF;
1927 
1928         if( pReqDate is not null ) then
1929         IF PG_DEBUG <> 0 THEN
1930         	oe_debug_pub.add ('load_mandatory_comps: ' || 'parameters passed ' || pLineId || ' date ' || to_char( pReqDate ) ,1);
1931         END IF;
1932         else
1933         IF PG_DEBUG <> 0 THEN
1934         	oe_debug_pub.add ('load_mandatory_comps: ' || 'parameters passed date null ' || pLineId  ,1);
1935         END IF;
1936         end if ;
1937 
1938 
1939 
1940      /* Bugfix 2047428 */
1941     lStmtNumber := 599;
1942     select      NVL(msi.atp_components_flag,'N')
1943     into        latpcompflag
1944     from        mtl_system_items msi , oe_order_lines_all oel
1945     where       oel.inventory_item_id   =       msi.inventory_item_id
1946     and         oel.ship_from_org_id    =       msi.organization_id
1947     and         oel.line_id             =       pLineId
1948     and         oel.ordered_quantity    >       0;
1949 
1950     If  latpcompflag in  ('N' , 'R' ) then  -- bugfix 3779636
1951         IF PG_DEBUG <> 0 THEN
1952         	oe_debug_pub.add ('load_mandatory_comps: ' || 'Exiting Load Mandatory Comps at model level as ATP Component flag is N or R ',1);               -- bugfix 3779636
1953         END IF;
1954         return(1);
1955     else
1956 
1957      	/* End Bugfix 2047428 */
1958     	select bom_explosion_temp_s.nextval
1959     	into   xGrpId
1960     	from dual;
1961 
1962     	-- bugfix 1902818: Added nvl to oel.component_sequence_id.
1963     	-- For ATO items, this will be null.
1964 
1965     	lStmtNumber := 600;
1966 	-- Bugfix 1998386 This function multiplies the ordered quantity
1967         	-- with conversion factor and returns converted quantity
1968         	-- if the order UOM is different from primary UOM of the item.
1969         	-- If ordered UOM and primary UOM are same , the function
1970         	-- returns the ordered quantity.
1971 
1972 
1973     	insert into bom_explosion_temp(
1974         	top_bill_sequence_id,
1975         	bill_sequence_id,
1976         	organization_id,
1977         	sort_order,
1978         	component_sequence_id,
1979         	component_item_id,
1980         	plan_level,
1981         	extended_quantity,
1982         	primary_uom_code,		-- 1998386
1983         	top_item_id,
1984         	line_id,
1985         	group_id)
1986    	select
1987         	nvl(oel.component_sequence_id,1),       -- Top bill sequence id  --1902818
1988         	nvl(oel.component_sequence_id,1),       -- Bill_sequence_id	 --1902818
1989         	oel.ship_from_org_id,
1990         	2,                               -- Sort Order --BUG no 1288823 modification
1991         	nvl(oel.component_sequence_id,1), 	--1902818
1992         	oel.inventory_item_id,
1993         	0,                               -- Plan level
1994         	-- 1998386
1995         	CTO_UTILITY_PK.convert_uom(
1996 			oel.order_quantity_uom,
1997 			msi.primary_uom_code,
1998 			oel.ordered_quantity,
1999 			oel.inventory_item_id),
2000         	msi.primary_uom_code,
2001         	oel.inventory_item_id,
2002         	oel.Line_Id,
2003         	xGrpId
2004    	from
2005         	oe_order_lines_all  oel,
2006         	mtl_system_items msi
2007         where oel.line_id        = pLineId
2008         and oel.inventory_item_id = msi.inventory_item_id
2009         and oel.ship_from_org_id = msi.organization_id
2010         and   oel.ordered_quantity > 0;
2011 
2012         if sql%rowcount  =  0 then
2013             IF PG_DEBUG <> 0 THEN
2014             	oe_debug_pub.add ('load_mandatory_comps: ' || 'Model Row not found',1);
2015             END IF;
2016             raise proc_error;
2017         end if;
2018 
2019         IF PG_DEBUG <> 0 THEN
2020         	oe_debug_pub.add('load_mandatory_comps: ' || 'Model Rows : ' || sql%rowcount, 2);
2021         END IF;
2022 
2023         /*------------------------------------------------+
2024            Insert all oe_order_lines row for this model
2025            with corrosponding plan_levels
2026         +------------------------------------------------*/
2027         rowcount := 1 ;
2028         while rowcount > 0 LOOP
2029 
2030             level_number := level_number + 1;
2031 
2032             lStmtNumber := 610;
2033             insert into bom_explosion_temp(
2034                top_bill_sequence_id,
2035                bill_sequence_id,
2036                organization_id,
2037                sort_order,
2038                component_sequence_id,
2039                component_item_id,
2040                plan_level,
2041                extended_quantity,
2042                primary_uom_code,		-- Bugfix 1998386
2043                top_item_id,
2044                line_id,
2045                group_id)
2046 	   -- 3893281 : Commenting Old SELECT
2047 	   /*
2048            select
2049                bet.top_bill_sequence_id,
2050                bet.bill_sequence_id,
2051                oel.ship_from_org_id, --changed from org_id,
2052                2,                    -- BUG no 1288823 modification
2053                oel.component_sequence_id,
2054                oel.inventory_item_id,
2055                level_number,
2056 
2057                -- Bugfix 1998386 This function multiplies the ordered quantity
2058                -- with conversion factor and returns converted quantity
2059                -- if the order UOM is different from primary UOM of the item .
2060                -- If ordered UOM and primary UOM are same,the function
2061                -- returns the ordered quantity.
2062 
2063                CTO_UTILITY_PK.convert_uom(
2064 			oel.order_quantity_uom,
2065 			msi.primary_uom_code,
2066 			oel.ordered_quantity,
2067 			oel.inventory_item_id),
2068                msi.primary_uom_code,
2069                bet.top_item_id,
2070                oel.line_id,
2071                xGrpId
2072            from
2073                  oe_order_lines_all  oel,
2074                  bom_explosion_temp  bet,
2075                  mtl_system_items    msi
2076 	   where oel.ato_line_id   = pLineId
2077 	   and oel.line_id <> pLineId
2078            and oel.inventory_item_id = msi.inventory_item_id
2079            and oel.ship_from_org_id = msi.organization_id
2080            and   oel.ordered_quantity  > 0
2081            and   nvl(oel.cancelled_flag,'N') <> 'Y'
2082            and   oel.link_to_line_id   = bet.line_id
2083            and   oel.item_type_code <> 'CONFIG'
2084            and   bet.group_id          = xGrpId
2085            and   bet.plan_level        = level_number -1 ;
2086 	   */
2087 	   select
2088                bet.top_bill_sequence_id,
2089                bet.bill_sequence_id,
2090                oel.ship_from_org_id, --changed from org_id,
2091                2,                    -- BUG no 1288823 modification
2092                oel.component_sequence_id,
2093                oel.inventory_item_id,
2094                level_number,
2095 
2096                -- Bugfix 1998386 This function multiplies the ordered quantity
2097                -- with conversion factor and returns converted quantity
2098                -- if the order UOM is different from primary UOM of the item .
2099                -- If ordered UOM and primary UOM are same,the function
2100                -- returns the ordered quantity.
2101 
2102                CTO_UTILITY_PK.convert_uom(
2103                         oel.order_quantity_uom,
2104                         msi.primary_uom_code,
2105                         oel.ordered_quantity,
2106                         oel.inventory_item_id),
2107                msi.primary_uom_code,
2108                bet.top_item_id,
2109                oel.line_id,
2110                xGrpId
2111 	   from
2112                  oe_order_lines_all  oel,
2113                  bom_explosion_temp  bet,
2114                  mtl_system_items    msi,
2115                  bom_bill_of_materials bbm,
2116                  bom_inventory_components bic
2117            where oel.ato_line_id   = pLineId
2118            and   oel.line_id <> pLineId
2119            and   oel.inventory_item_id = msi.inventory_item_id
2120            and   oel.ship_from_org_id = msi.organization_id
2121            and   oel.ordered_quantity  > 0
2122            and   nvl(oel.cancelled_flag,'N') <> 'Y'
2123            and   oel.link_to_line_id   = bet.line_id
2124            and   oel.item_type_code <> 'CONFIG'  /* BUG 2483920 */
2125            and   bet.group_id          = xGrpId
2126            and   bet.plan_level        = level_number -1
2127            and   bet.component_item_id = bbm.assembly_item_id
2128            and   bet.organization_id = bbm.organization_id
2129            and   bbm.alternate_bom_designator is null
2130            and   bbm.common_bill_sequence_id = bic.bill_sequence_id
2131            and   bic.component_item_id = msi.inventory_item_id
2132            and   bic.optional = 1
2133            and   ( msi.bom_item_type in (1,2)
2134                         OR (msi.bom_item_type = 4 and bic.wip_supply_type = 6 )
2135                         OR (msi.bom_item_type = 4 and msi.replenish_to_order_flag = 'Y' ));
2136            -- 3893281
2137            rowcount := SQL%ROWCOUNT;
2138 
2139            IF PG_DEBUG <> 0 THEN
2140            	oe_debug_pub.add ('load_mandatory_comps: ' || 'Row Count : '   || rowcount, 2);
2141            END IF;
2142 
2143         END LOOP;
2144 
2145 	-- 3893281 following code is to debug BET at this point
2146            open debug_bet;
2147            oe_debug_pub.add ('BET picture after Model and its children are inserted ..' ,1);
2148            loop
2149            fetch debug_bet
2150            into d_top_bseq_id,
2151                 d_bseq_id,
2152                 d_org_id,
2153                 d_sort_order,
2154                 d_plan_level,
2155                 d_line_id,
2156                 d_item;
2157            exit when debug_bet%NOTFOUND;
2158            IF PG_DEBUG <> 0 THEN
2159                 oe_debug_pub.add (' Top Bill Seq id '||d_top_bseq_id|| ' Bill Seq Id '||d_bseq_id
2160                                   ||' Org '||d_org_id ||' Sort Order '||d_sort_order
2161                                   || ' Plan level '||d_plan_level||' Line Id '||d_line_id
2162                                   || ' Item '||d_item,1);
2163            END IF;
2164            end loop;
2165            close debug_bet;
2166          -- 3893281
2167 
2168          v_max_level_number := level_number ; /* added by sushant to track max level number */
2169 
2170          IF PG_DEBUG <> 0 THEN
2171            	oe_debug_pub.add ('load_mandatory_comps: ' || 'Group_id  : '|| xGrpId, 2);
2172          END IF;
2173 
2174 
2175 
2176        /*
2177        ** BUG no 1288823, new query block added
2178        */
2179 
2180        begin
2181 
2182        select msi.fixed_lead_time , nvl(msi.variable_lead_time,0)
2183           into v_msi_fixed_lead_time, v_msi_variable_lead_time
2184        from mtl_system_items msi , bom_explosion_temp be
2185        where  be.organization_id   = msi.organization_id
2186        and   be.component_item_id = msi.inventory_item_id
2187        and   be.line_id = pLineId
2188        and   be.group_id = xGrpId;      -- bugfix 1876997
2189 
2190        exception
2191 
2192        when no_data_found then
2193         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ';
2194         return(0);
2195 
2196        when others then
2197         xErrorMessage := 'ERROR: CTOCITMB:'||to_char(lStmtNumber)||': other error ';
2198         IF PG_DEBUG <> 0 THEN
2199         	oe_debug_pub.add('load_mandatory_comps: ' || 'ERROR: others exception while executing stmt ' ||to_char(lStmtNumber),1);
2200 
2201         	oe_debug_pub.add('load_mandatory_comps: ' || 'ERROR message : '||sqlerrm,1);
2202         END IF;
2203         return(0);
2204 
2205        end ;
2206 
2207 
2208         /*------------------------------------------------------------+
2209            Start Explosion of all items copied above.
2210         +-----------------------------------------------------------*/
2211 
2212         level_number := 0;
2213         IF PG_DEBUG <> 0 THEN
2214         	oe_debug_pub.add('load_mandatory_comps: ' || 'Explosion loop' , 2);
2215         END IF;
2216 
2217         << Explode_loop >>
2218         LOOP
2219 
2220         lStmtNumber := 620;
2221 	-- Bug 1985793 Selecting greater of sysdate and calendar date
2222             -- while checking for item effectivity so that planning will get
2223             -- components effective till sysdate.
2224 	--apps performance bugfix 4905845, sql id 16103327
2225         insert into bom_explosion_temp(
2226            top_bill_sequence_id,
2227            bill_sequence_id,
2228            organization_id,
2229            sort_order,
2230            component_sequence_id,
2231            component_item_id,
2232            plan_level,
2233            extended_quantity,
2234            primary_uom_code,		-- 1998386
2235            top_item_id,
2236            component_quantity,
2237            check_atp,
2238            atp_components_flag,
2239            atp_flag,
2240            bom_item_type,
2241            assembly_item_id,
2242            parent_bom_item_type,
2243            line_id,
2244            wip_supply_type,	-- 3254039
2245            group_id)
2246       select
2247            -1 , --  2897132
2248            bic.bill_sequence_id,
2249            be.organization_id,
2250            evaluate_order( msi2.atp_flag, msi2.atp_components_flag , msi2.bom_item_type ) ,  /* BUG # 1518894, 1288823 */
2251                                  -- 1288823
2252            bic.component_sequence_id,
2253            bic.component_item_id,
2254            level_number + 1,
2255            be.extended_quantity * bic.component_quantity,
2256            msi2.primary_uom_code,		-- 1998386
2257            be.top_item_id,
2258            bic.component_quantity,
2259            bic.check_atp,
2260                   -- 2378556
2261            msi2.atp_components_flag,
2262            msi2.atp_flag,
2263            msi2.bom_item_type,
2264 
2265            bom.assembly_item_id,
2266            msi.bom_item_type,
2267            be.line_id ,  -- 2897132
2268            nvl(bic.wip_supply_type, msi2.wip_supply_type),		-- 3254039 , 3298244
2269 	   xGrpId
2270        from
2271            bom_calendar_dates       cal,
2272            mtl_system_items         msi,         /* PARENT */
2273            mtl_system_items         msi2,        /* CHILD */
2274            bom_inventory_components bic,
2275            eng_revised_items        eri,
2276            bom_bill_of_materials    bom,
2277            mtl_parameters           mp,
2278            bom_explosion_temp       be
2279        where be.sort_order <> 3  -- 1288823
2280        and   be.group_id = xGrpId
2281        and   nvl(be.plan_level,0) = level_number
2282        and   be.organization_id   = bom.organization_id
2283        and   be.component_item_id = bom.assembly_item_id
2284        and   bic.component_quantity <> 0
2285        and   bic.revised_item_sequence_id = eri.revised_item_sequence_id (+)
2286        and   bic.component_item_id = msi2.inventory_item_id  -- 1518894
2287        and   bom.organization_id = msi2.organization_id     -- 1518894
2288        and   bom.alternate_bom_designator is null
2289        and   bic.bill_sequence_id = bom.common_bill_sequence_id
2290        and   be.organization_id   = msi.organization_id
2291        and   be.component_item_id = msi.inventory_item_id --BUG#2378556
2292        and   mp.organization_id   = be.organization_id
2293        and   cal.calendar_code    = mp.calendar_code
2294        and   cal.exception_set_id = mp.calendar_exception_set_id
2295        and   cal.calendar_date =
2296                  ( select c.calendar_date
2297                    from   bom_calendar_dates C
2298                    where  C.calendar_code = mp.calendar_code
2299                    and    c.exception_set_id = mp.calendar_exception_set_id
2300                    and    C.seq_num =
2301                       (select c2.prior_seq_num -
2302                         ceil( nvl( v_msi_fixed_lead_time,0)+
2303                                  (be.extended_quantity  *
2304                          v_msi_variable_lead_time ))
2305                        from bom_calendar_dates c2
2306                        where c2.calendar_code = mp.calendar_code
2307                        and   c2.exception_set_id = mp.calendar_exception_set_id
2308                        and   c2.calendar_date = trunc(pReqDate)
2309                        )
2310                   )
2311 
2312             --  2162912
2313             and	  TRUNC(bic.effectivity_date) <= greatest(nvl(cal.calendar_date,sysdate),sysdate)
2314             and   nvl(TRUNC(bic.disable_date),(nvl(cal.calendar_date,sysdate) + 1)) > nvl(cal.calendar_date,sysdate)
2315 
2316             and   bic.effectivity_date =
2317                    (select
2318                          max(effectivity_date)
2319                     from bom_inventory_components bic1,
2320                          eng_revised_items eri
2321                     where bic1.bill_sequence_id = bic.bill_sequence_id
2322                     and   bic1.component_item_id = bic.component_item_id
2323                     and   bic1.revised_item_sequence_id =
2324                           eri.revised_item_sequence_id (+)
2325                     and   (decode(bic1.implementation_date, NULL,
2326                             bic1.old_component_sequence_id,
2327                             bic1.component_sequence_id) =
2328                             decode(bic.implementation_date, NULL,
2329                                    bic.old_component_sequence_id,
2330                                    bic.component_sequence_id)
2331                            OR
2332                            bic1.operation_seq_num = bic.operation_seq_num)
2333 
2334             --  2162912
2335             and   TRUNC(bic1.effectivity_date) <= greatest(nvl(cal.calendar_date,sysdate),sysdate)
2336             and   nvl(TRUNC(bic1.disable_date),(nvl(cal.calendar_date,sysdate) + 1)) > nvl(cal.calendar_date,sysdate)
2337             --  2162912
2338             and   ( nvl(eri.status_type,6) IN (4,6,7))
2339             and not exists
2340                      (select
2341                           'X'
2342                       from bom_inventory_components bicn, eng_revised_items eri1
2343                       where bicn.bill_sequence_id + 0 = bic.bill_sequence_id
2344                       and   bicn.old_component_sequence_id =
2345                             bic.component_sequence_id
2346                       and   bicn.acd_type in (2,3)
2347                       and   eri1.revised_item_sequence_id = bicn.revised_item_sequence_id
2348                       and    trunc(bicn.disable_date) <= cal.calendar_date
2349                       and   ( nvl(eri1.status_type,6) in (4,6,7))
2350              )
2351                    )
2352             and   bic.optional = 2        /* NOT OPTIONAL */
2353             and   msi2.bom_item_type = 4 /* 2400948 */
2354                  --  Model or Option Class or ATO ITEM * * BUG#2378556 commented for bug 3314297 mandatory comps should be
2355                  --  exploded for standard items
2356 
2357             and   msi.pick_components_flag <> 'Y' ;
2358 
2359             -- start bugfix 2425667
2360             lrowcount := SQL%ROWCOUNT;
2361             IF PG_DEBUG <> 0 THEN
2362             	oe_debug_pub.add ('load_mandatory_comps: ' || 'Row Count : '   || lrowcount, 2);
2363             END IF;
2364             -- oe_debug_pub.add('Row Count : '   || SQL%ROWCOUNT);
2365             -- end bugfix 2425667
2366 
2367 
2368             /* continue exploding atleast till max level number */
2369             -- start bugfix 2425667
2370             -- IF 		SQL%ROWCOUNT = 0
2371             IF  lrowcount = 0
2372             -- end bugfix 2425667
2373             	AND 	level_number >= v_max_level_number THEN
2374                EXIT Explode_loop;
2375             END IF;
2376 
2377             level_number := level_number + 1;
2378 
2379         END LOOP;
2380     END IF; -- bugfix 2047428
2381 
2382 
2383     return(1);
2384 
2385 
2386 exception
2387 
2388     when no_data_found then
2389         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ';
2390         return(0);
2391     when proc_error then
2392         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': Model row not found ';
2393         return(0);
2394     when others then	--bugfix 1902818: added others excpn
2395         xErrorMessage := 'load_mandatory_comps:(others excepn)::'||to_char(lStmtNumber)||'::'||substrb(sqlerrm,1,150);
2396         return(0);
2397 
2398 end load_mandatory_comps;
2399 
2400 
2401 -- 3779636
2402 function load_mandatory_comps_pds(
2403          pLineId          in     number,
2404          pReqDate         in     oe_order_lines_all.schedule_ship_date%type,
2405          xGrpId           out NOCOPY   number,
2406          xErrorMessage    out NOCOPY   varchar2  ,
2407          xMessageName     out NOCOPY   varchar2  ,
2408          xTableName       out NOCOPY   varchar2  )
2409 return integer
2410 is
2411 
2412 	level_number          		number := 0;
2413 	v_max_level_number    		number := 0;
2414 	lStmtNumber           		number := 0;
2415 	lGrpId                		number;
2416 	rowcount              		number := 1;
2417 	proc_error            		exception ;
2418 
2419 	v_msi_fixed_lead_time       	mtl_system_items.fixed_lead_time%type ;
2420 	v_msi_variable_lead_time    	mtl_system_items.variable_lead_time%type ;
2421 	latpcompflag            	mtl_system_items.atp_components_flag%type;
2422 	lrowcount 			number;
2423 begin
2424 
2425     /*---------------------------------------------------+
2426        Insert the Model row details from oe_order_lines
2427        with plan_level =0
2428        The lines have a common group_id, have line_id reference
2429        of oe_order_lines. Top_bill_sequence_id and bill_sequence_id
2430        are not needed however, as they are not null coloumns,
2431        all rows have model's bill_sequence_id in these fields.
2432     +----------------------------------------------------*/
2433 
2434 
2435 
2436         IF PG_DEBUG <> 0 THEN
2437         	oe_debug_pub.add ('load_mandatory_comps_pds: ' || ' load mandatory comps PDS ' ,1);
2438         END IF;
2439 
2440         if( pReqDate is not null ) then
2441         IF PG_DEBUG <> 0 THEN
2442         	oe_debug_pub.add ('load_mandatory_comps_pds: ' || 'parameters passed ' || pLineId || ' date ' || to_char( pReqDate ) ,1);
2443         END IF;
2444         else
2445         IF PG_DEBUG <> 0 THEN
2446         	oe_debug_pub.add ('load_mandatory_comps_pds: ' || 'parameters passed date null ' || pLineId  ,1);
2447         END IF;
2448         end if ;
2449 
2450     lStmtNumber := 599;
2451     select      NVL(msi.atp_components_flag,'N')
2452     into        latpcompflag
2453     from        mtl_system_items msi , oe_order_lines_all oel
2454     where       oel.inventory_item_id   =       msi.inventory_item_id
2455     and         oel.ship_from_org_id    =       msi.organization_id
2456     and         oel.line_id             =       pLineId
2457     and         oel.ordered_quantity    >       0;
2458 
2459     If latpcompflag in ( 'N' , 'R' ) then	-- 3779636
2460         IF PG_DEBUG <> 0 THEN
2461         	oe_debug_pub.add ('load_mandatory_comps_pds: ' || 'Exiting Load Mandatory Comps PDS at model level as ATP Component flag is N or R',1);  -- 3779636
2462         END IF;
2463         return(1);
2464     else
2465 
2466     	select bom_explosion_temp_s.nextval
2467     	into   xGrpId
2468     	from dual;
2469 
2470 
2471     	lStmtNumber := 600;
2472     	insert into bom_explosion_temp(
2473         	top_bill_sequence_id,
2474         	bill_sequence_id,
2475         	organization_id,
2476         	sort_order,
2477         	component_sequence_id,
2478         	component_item_id,
2479         	plan_level,
2480         	extended_quantity,
2481         	primary_uom_code,
2482         	top_item_id,
2483         	line_id,
2484         	group_id)
2485    	select
2486         	nvl(oel.component_sequence_id,1),
2487         	nvl(oel.component_sequence_id,1),
2488         	oel.ship_from_org_id,
2489         	2,
2490         	nvl(oel.component_sequence_id,1),
2491         	oel.inventory_item_id,
2492         	0,
2493         	CTO_UTILITY_PK.convert_uom(
2494 			oel.order_quantity_uom,
2495 			msi.primary_uom_code,
2496 			oel.ordered_quantity,
2497 			oel.inventory_item_id),
2498         	msi.primary_uom_code,
2499         	oel.inventory_item_id,
2500         	oel.Line_Id,
2501         	xGrpId
2502    	from
2503         	oe_order_lines_all  oel,
2504         	mtl_system_items msi
2505         where oel.line_id        = pLineId
2506         and oel.inventory_item_id = msi.inventory_item_id
2507         and oel.ship_from_org_id = msi.organization_id
2508         and   oel.ordered_quantity > 0;
2509 
2510         if sql%rowcount  =  0 then
2511             IF PG_DEBUG <> 0 THEN
2512             	oe_debug_pub.add ('load_mandatory_comps_pds: ' || 'Model Row not found',1);
2513             END IF;
2514             raise proc_error;
2515         end if;
2516 
2517         IF PG_DEBUG <> 0 THEN
2518         	oe_debug_pub.add('load_mandatory_comps_pds: ' || 'Model Rows : ' || sql%rowcount, 2);
2519         END IF;
2520 
2521         /*------------------------------------------------+
2522            Insert all oe_order_lines row for this model
2523            with corrosponding plan_levels
2524         +------------------------------------------------*/
2525         rowcount := 1 ;
2526         while rowcount > 0 LOOP
2527 
2528             level_number := level_number + 1;
2529 
2530             lStmtNumber := 610;
2531             insert into bom_explosion_temp(
2532                top_bill_sequence_id,
2533                bill_sequence_id,
2534                organization_id,
2535                sort_order,
2536                component_sequence_id,
2537                component_item_id,
2538                plan_level,
2539                extended_quantity,
2540                primary_uom_code,
2541                top_item_id,
2542                line_id,
2543                group_id)
2544            select
2545                bet.top_bill_sequence_id,
2546                bet.bill_sequence_id,
2547                oel.ship_from_org_id,
2548                2,
2549                oel.component_sequence_id,
2550                oel.inventory_item_id,
2551                level_number,
2552                CTO_UTILITY_PK.convert_uom(
2553 			oel.order_quantity_uom,
2554 			msi.primary_uom_code,
2555 			oel.ordered_quantity,
2556 			oel.inventory_item_id),
2557                msi.primary_uom_code,
2558                bet.top_item_id,
2559                oel.line_id,
2560                xGrpId
2561            from
2562                  oe_order_lines_all  oel,
2563                  bom_explosion_temp  bet,
2564                  mtl_system_items    msi
2565 	   where oel.ato_line_id   = pLineId
2566 	   and oel.line_id <> pLineId
2567            and oel.inventory_item_id = msi.inventory_item_id
2568            and oel.ship_from_org_id = msi.organization_id
2569 	   and msi.bom_item_type in ( 1, 2 )   -- only sub-models and option classes
2570            and   oel.ordered_quantity  > 0
2571            and   nvl(oel.cancelled_flag,'N') <> 'Y'
2572            and   oel.link_to_line_id   = bet.line_id
2573            and   oel.item_type_code <> 'CONFIG'
2574            and   bet.group_id          = xGrpId
2575            and   bet.plan_level        = level_number -1 ;
2576 
2577            rowcount := SQL%ROWCOUNT;
2578 
2579            IF PG_DEBUG <> 0 THEN
2580            	oe_debug_pub.add ('load_mandatory_comps_pds: ' || 'Row Count : '   || rowcount, 2);
2581            END IF;
2582 
2583         END LOOP;
2584 
2585             v_max_level_number := level_number ;
2586 
2587            IF PG_DEBUG <> 0 THEN
2588            	oe_debug_pub.add ('load_mandatory_comps_pds: ' || 'Group_id  : '|| xGrpId, 2);
2589            END IF;
2590 
2591 
2592        begin
2593 
2594        select msi.fixed_lead_time , nvl(msi.variable_lead_time,0)
2595           into v_msi_fixed_lead_time, v_msi_variable_lead_time
2596        from mtl_system_items msi , bom_explosion_temp be
2597        where  be.organization_id   = msi.organization_id
2598        and   be.component_item_id = msi.inventory_item_id
2599        and   be.line_id = pLineId
2600        and   be.group_id = xGrpId;
2601 
2602        exception
2603 
2604        when no_data_found then
2605         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ';
2606         return(0);
2607 
2608        when others then
2609         xErrorMessage := 'ERROR: CTOCITMB:'||to_char(lStmtNumber)||': other error ';
2610         IF PG_DEBUG <> 0 THEN
2611         	oe_debug_pub.add('load_mandatory_comps_pds: ' || 'ERROR: others exception while executing stmt ' ||to_char(lStmtNumber),1);
2612 
2613         	oe_debug_pub.add('load_mandatory_comps_pds: ' || 'ERROR message : '||sqlerrm,1);
2614         END IF;
2615         return(0);
2616 
2617        end ;
2618 
2619 
2620         /*------------------------------------------------------------+
2621            Start Explosion of all items copied above.
2622         +-----------------------------------------------------------*/
2623 
2624         level_number := 0;
2625         IF PG_DEBUG <> 0 THEN
2626         	oe_debug_pub.add('load_mandatory_comps_pds: ' || 'Explosion loop' , 2);
2627         END IF;
2628 
2629         << Explode_loop >>
2630         LOOP
2631 
2632         lStmtNumber := 620;
2633 	--apps perf bug#4905845,sql id 16103462
2634 	--reduce comments to decrease shared memory
2635         insert into bom_explosion_temp(
2636            top_bill_sequence_id,
2637            bill_sequence_id,
2638            organization_id,
2639            sort_order,
2640            component_sequence_id,
2641            component_item_id,
2642            plan_level,
2643            extended_quantity,
2644            primary_uom_code,
2645            top_item_id,
2646            component_quantity,
2647            check_atp,
2648            atp_components_flag,
2649            atp_flag,
2650            bom_item_type,
2651            assembly_item_id,
2652            parent_bom_item_type,
2653            line_id,
2654            wip_supply_type,
2655            group_id)
2656       select
2657            -1 ,
2658            bic.bill_sequence_id,
2659            be.organization_id,
2660            evaluate_order( msi2.atp_flag, msi2.atp_components_flag , msi2.bom_item_type ) ,
2661            bic.component_sequence_id,
2662            bic.component_item_id,
2663            level_number + 1,
2664            be.extended_quantity * bic.component_quantity,
2665            msi2.primary_uom_code,
2666            be.top_item_id,
2667            bic.component_quantity,
2668            bic.check_atp,
2669            msi2.atp_components_flag,
2670            msi2.atp_flag,
2671            msi2.bom_item_type,
2672            bom.assembly_item_id,
2673            msi.bom_item_type,
2674            be.line_id ,
2675            nvl(bic.wip_supply_type, msi2.wip_supply_type),
2676            xGrpId
2677        from
2678            bom_calendar_dates       cal,
2679            mtl_system_items         msi,
2680            mtl_system_items         msi2,
2681            bom_inventory_components bic,
2682            eng_revised_items        eri,
2683            bom_bill_of_materials    bom,
2684            mtl_parameters           mp,
2685            bom_explosion_temp       be
2686        where be.sort_order <> 3
2687        and   be.group_id = xGrpId
2688        and   nvl(be.plan_level,0) = level_number
2689        and   be.organization_id   = bom.organization_id
2690        and   be.component_item_id = bom.assembly_item_id
2691        and   bic.component_quantity <> 0
2692        and   bic.revised_item_sequence_id = eri.revised_item_sequence_id (+)
2693        and   bic.component_item_id = msi2.inventory_item_id
2694        and   bom.organization_id = msi2.organization_id
2695        and   bom.alternate_bom_designator is null
2696        and   bic.bill_sequence_id = bom.common_bill_sequence_id
2697        and   be.organization_id   = msi.organization_id
2698        and   be.component_item_id = msi.inventory_item_id
2699        and   mp.organization_id   = be.organization_id
2700        and   cal.calendar_code    = mp.calendar_code
2701        and   cal.exception_set_id = mp.calendar_exception_set_id
2702        and   cal.calendar_date =
2703                  ( select c.calendar_date
2704                    from   bom_calendar_dates C
2705                    where  C.calendar_code = mp.calendar_code
2706                    and    c.exception_set_id = mp.calendar_exception_set_id
2707                    and    C.seq_num =
2708                       (select c2.prior_seq_num -
2709                         ceil( nvl( v_msi_fixed_lead_time,0)+
2710                                  (be.extended_quantity  *
2711                          v_msi_variable_lead_time ))
2712                        from bom_calendar_dates c2
2713                        where c2.calendar_code = mp.calendar_code
2714                        and   c2.exception_set_id = mp.calendar_exception_set_id
2715                        and   c2.calendar_date = trunc(pReqDate)
2716                        )
2717                   )
2718             and	  TRUNC(bic.effectivity_date) <= greatest(nvl(cal.calendar_date,sysdate),sysdate)
2719             and   nvl(TRUNC(bic.disable_date),(nvl(cal.calendar_date,sysdate) + 1)) > nvl(cal.calendar_date,sysdate)
2720             and   bic.effectivity_date =
2721                    (select
2722                          max(effectivity_date)
2723                     from bom_inventory_components bic1,
2724                          eng_revised_items eri
2725                     where bic1.bill_sequence_id = bic.bill_sequence_id
2726                     and   bic1.component_item_id = bic.component_item_id
2727                     and   bic1.revised_item_sequence_id =
2728                           eri.revised_item_sequence_id (+)
2729                     and   (decode(bic1.implementation_date, NULL,
2730                             bic1.old_component_sequence_id,
2731                             bic1.component_sequence_id) =
2732                             decode(bic.implementation_date, NULL,
2733                                    bic.old_component_sequence_id,
2734                                    bic.component_sequence_id)
2735                            OR
2736                            bic1.operation_seq_num = bic.operation_seq_num)
2737             and   TRUNC(bic1.effectivity_date) <= greatest(nvl(cal.calendar_date,sysdate),sysdate)
2738             and   nvl(TRUNC(bic1.disable_date),(nvl(cal.calendar_date,sysdate) + 1)) > nvl(cal.calendar_date,sysdate)
2739             and   ( nvl(eri.status_type,6) IN (4,6,7))
2740             and not exists
2741                      (select
2742                           'X'
2743                       from bom_inventory_components bicn, eng_revised_items eri1
2744                       where bicn.bill_sequence_id + 0 = bic.bill_sequence_id
2745                       and   bicn.old_component_sequence_id =
2746                             bic.component_sequence_id
2747                       and   bicn.acd_type in (2,3)
2748                       and   eri1.revised_item_sequence_id = bicn.revised_item_sequence_id
2749                       and    trunc(bicn.disable_date) <= cal.calendar_date
2750                       and   ( nvl(eri1.status_type,6) in (4,6,7))
2751              )
2752                    )
2753             and   bic.optional = 2
2754             and   msi2.bom_item_type = 4
2755 	    and   msi.bom_item_type in (1,2) /*Model or Option Class */
2756 	    and   msi.pick_components_flag <> 'Y' ;
2757 
2758 
2759             lrowcount := SQL%ROWCOUNT;
2760             IF PG_DEBUG <> 0 THEN
2761             	oe_debug_pub.add ('load_mandatory_comps_pds: ' || 'Row Count : '   || lrowcount, 2);
2762             END IF;
2763 
2764 
2765 
2766 
2767             /* continue exploding atleast till max level number */
2768 
2769             IF  lrowcount = 0
2770             	AND 	level_number >= v_max_level_number THEN
2771                EXIT Explode_loop;
2772             END IF;
2773 
2774             level_number := level_number + 1;
2775 
2776         END LOOP;
2777     END IF;
2778 
2779 
2780     return(1);
2781 
2782 
2783 exception
2784 
2785     when no_data_found then
2786         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ';
2787         return(0);
2788     when proc_error then
2789         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': Model row not found ';
2790         return(0);
2791     when others then	--bugfix 1902818: added others excpn
2792         xErrorMessage := 'load_mandatory_comps_pds:(others excepn)::'||to_char(lStmtNumber)||'::'||substrb(sqlerrm,1,150);
2793         return(0);
2794 
2795 end load_mandatory_comps_pds;
2796 
2797 -- 3779636
2798 
2799 
2800 function evaluate_order( p_atp_flag mtl_system_items.atp_flag%type
2801                        , p_atp_comp mtl_system_items.atp_components_flag%type
2802                        , p_item_type mtl_system_items.bom_item_type%type )
2803 return number
2804 is
2805 begin
2806    if( p_atp_comp in ( 'C' , 'Y' ) AND p_atp_flag in ( 'R' , 'C')) then
2807       return 1 ; /* do explode and required */
2808 
2809    elsif( p_atp_comp = 'Y' AND p_atp_flag = 'N' and p_item_type = '4') then
2810       return 2 ; /* do explode and not required */
2811 
2812    else
2813       return 3 ; /* do not explode and required */
2814 
2815    end if ;
2816 
2817 end evaluate_order ;
2818 
2819 
2820 function load_mandatory_components(
2821          p_ship_set             in     MRP_ATP_PUB.ATP_Rec_Typ,
2822          p_model_index          in     number,
2823 	 pReqDate               in     oe_order_lines_all.schedule_ship_date%type,
2824          xGrpId                 out NOCOPY   number,
2825          xErrorMessage          out NOCOPY   varchar2  ,
2826          xMessageName           out NOCOPY   varchar2  ,
2827          xTableName             out NOCOPY   varchar2  )
2828 return integer
2829 is
2830 
2831 	level_number          		number := 0;
2832 	v_max_level_number          	number := 0;
2833 	lStmtNumber           		number := 0;
2834 	lGrpId                		number;
2835 	lGroupid2             		number;
2836 	rowcount              		number := 1;
2837 	proc_error            		exception ;
2838 	i                     		number;
2839 
2840 	v_msi_fixed_lead_time       	mtl_system_items.fixed_lead_time%type ;
2841 	v_msi_variable_lead_time    	mtl_system_items.variable_lead_time%type ;
2842 
2843 	/*Bugfix 2047428 */
2844 	latpcompflag            	mtl_system_items.atp_components_flag%type;
2845 	/* End Bugfix 2047428 */
2846 
2847 	-- 3893281 : New debug cursor and variables
2848         cursor debug_bet is
2849         select bet.top_bill_sequence_id,
2850                bet.bill_sequence_id,
2851                bet.organization_id,
2852                bet.sort_order,
2853                bet.plan_level,
2854                bet.line_id,
2855                substrb(msi.concatenated_segments,1,50)
2856         from   bom_explosion_temp bet,mtl_system_items_kfv msi
2857         where  bet.group_id = xGrpId
2858         and    bet.organization_id = msi.organization_id
2859         and    bet.component_item_id = msi.inventory_item_id;
2860 
2861         d_top_bseq_id           number;
2862         d_bseq_id               number;
2863         d_org_id                number;
2864         d_sort_order            varchar2(240);
2865         d_plan_level            number;
2866         d_line_id               number;
2867         d_item                  varchar2(50);
2868 
2869         -- 3893281
2870 
2871 begin
2872 
2873 
2874 
2875 
2876         IF PG_DEBUG <> 0 THEN
2877         	oe_debug_pub.add ('load_mandatory_components: ' || ' load mandatory components ' ,1);
2878         END IF;
2879 
2880          if( pReqDate is not null ) then
2881         IF PG_DEBUG <> 0 THEN
2882         	oe_debug_pub.add ('load_mandatory_components: ' || 'parameters passed index ' || p_model_index || ' date ' || to_char( pReqDate ) ,1);
2883         END IF;
2884          else
2885         IF PG_DEBUG <> 0 THEN
2886         	oe_debug_pub.add ('load_mandatory_components: ' || 'parameters passed index date null ' || p_model_index  ,1);
2887         END IF;
2888         end if ;
2889 
2890 
2891 
2892 	/* Bugfix 2047428 */
2893         lStmtNumber := 599;
2894         select  NVL(msi.atp_components_flag,'N')
2895         into    latpcompflag
2896         from    mtl_system_items msi
2897         where   msi.inventory_item_id  =  p_ship_set.inventory_item_id(p_model_index)
2898         and     msi.organization_id    =  p_ship_set.source_organization_id(p_model_index);
2899 
2900 
2901     	If latpcompflag in ( 'N'  , 'R' ) then  -- bugfix 3779636
2902         	IF PG_DEBUG <> 0 THEN
2903         		oe_debug_pub.add ('load_mandatory_components: ' || 'Exiting Load Mandatory Components at model level as ATP Component flag is N or R ',1);    -- bugfix 3779636
2904         	END IF;
2905         	return(1);
2906 
2907     	else
2908 
2909      		/* End Bugfix 2047428 */
2910 		select bom_explosion_temp_s.nextval
2911 		into  lGroupid2
2912 		from dual;
2913 
2914         	select bom_explosion_temp_s.nextval
2915         	into   xGrpId
2916         	from dual;
2917 
2918     		lStmtNumber := 600;
2919 
2920 
2921      		i := p_ship_set.inventory_item_id.FIRST;
2922 
2923      		-- use bom_explosion_temp table to store the ship set for later join
2924      		while i is not null
2925      		loop
2926        			insert into bom_explosion_temp(
2927 		 		top_bill_sequence_id,
2928 		 		bill_sequence_id,
2929 		 		organization_id,
2930 		 		component_item_id,
2931 		 		plan_level,
2932 		 		extended_quantity,
2933 		 		primary_uom_code,		-- Bugfix 1998386
2934 		 		sort_order,
2935 		 		group_id)
2936 		 	-- Bugfix 1998386 Change this to select statement to select
2937 		 	-- primary_uom_code from mtl_system_items.
2938 		 	-- Also CONVERT_UOM function is used to convert
2939 		 	-- p_ship_set.quantity_ordered(i) if the ordered UOM
2940 		 	-- is different from primary UOM.
2941 		 	/*values (
2942 		 		1,
2943 		 		1,
2944 		 		p_ship_set.source_organization_id(i),
2945 		 		p_ship_set.inventory_item_id(i),
2946 		 		1,
2947 		 		p_ship_set.quantity_ordered(i),
2948 		 		2,                -- BUG no 1288823 modification
2949 		 		lGroupid2);*/
2950 		 	select
2951 		 		1,
2952 		 		1,
2953 		 		p_ship_set.source_organization_id(i),
2954 		 		p_ship_set.inventory_item_id(i),
2955 		 		1,
2956 		 		CTO_UTILITY_PK.convert_uom(
2957 						p_ship_set.quantity_uom(i),
2958 						msi.primary_uom_code,
2959 						p_ship_set.quantity_ordered(i),
2960 						p_ship_set.inventory_item_id(i)),
2961          	 		msi.primary_uom_code,
2962 		 		2,                -- BUG no 1288823 modification
2963 		 		lGroupid2
2964 		 	from mtl_system_items msi
2965 		 	where msi.inventory_item_id = p_ship_set.inventory_item_id(i)
2966 		 	and   msi.organization_id = p_ship_set.source_organization_id(i);
2967 
2968 	 		i := p_ship_set.inventory_item_id.NEXT(i);
2969 
2970 	 	end loop;
2971 
2972 
2973 
2974     		-- insert the top model into bom_explosion_temp
2975     		insert into bom_explosion_temp(
2976         		top_bill_sequence_id,
2977         		bill_sequence_id,
2978         		organization_id,
2979         		sort_order,
2980         		component_item_id,
2981         		plan_level,
2982         		extended_quantity,
2983         		primary_uom_code,		-- Bugfix 1998386
2984         		top_item_id,
2985         		line_id,
2986         		group_id)
2987 
2988         	-- Bugfix 1998386 Change this to select statement to
2989         	-- select primary_uom_code from mtl_system_items.
2990         	-- Also CONVERT_UOM function is used to convert
2991         	-- p_ship_set.quantity_ordered(i) if the ordered UOM is
2992         	-- different from primary UOM.
2993 
2994                 	select
2995 		 		1,
2996 		 		1,
2997 		 		p_ship_set.source_organization_id(p_model_index),
2998 		 		2,                -- BUG no 1288823 modification
2999 		 		p_ship_set.inventory_item_id(p_model_index),
3000 		 		0,
3001 		 		CTO_UTILITY_PK.convert_uom(
3002 						p_ship_set.quantity_uom(p_model_index),
3003 						msi.primary_uom_code,
3004 						p_ship_set.quantity_ordered(p_model_index),
3005 						p_ship_set.inventory_item_id(p_model_index)),
3006          	 		msi.primary_uom_code,
3007          	 		p_ship_set.inventory_item_id(p_model_index),
3008 		 		1,
3009 		 		xGrpId
3010 		 	from mtl_system_items msi
3011 		 	where msi.inventory_item_id = p_ship_set.inventory_item_id(p_model_index)
3012 		 	and   msi.organization_id = p_ship_set.source_organization_id(p_model_index);
3013 
3014 
3015         /*------------------------------------------------+
3016            Insert all selections for the top model
3017            with corrosponding plan_levels
3018         +------------------------------------------------*/
3019         	rowcount := 1 ;
3020 
3021         	while rowcount > 0
3022 		LOOP
3023 
3024             	level_number := level_number + 1;
3025 
3026             	lStmtNumber := 610;
3027             	insert into bom_explosion_temp(
3028                		top_bill_sequence_id,
3029                		bill_sequence_id,
3030                		organization_id,
3031                		sort_order,
3032                		component_item_id,
3033                		plan_level,
3034                		extended_quantity,
3035                		primary_uom_code,		-- Bugfix 1998386
3036                		top_item_id,
3037                		line_id,
3038                		group_id)
3039            	select
3040                		1,
3041                		1,
3042                		bet.organization_id,
3043 	       		2,                -- BUG no 1288823 modification
3044                		bic.component_item_id,
3045                		level_number,
3046                		/* bet2.extended_quantity,*/
3047 
3048                		-- Bugfix 1998386 This function multiplies the ordered quantity
3049                		-- with conversion factor and returns the converted
3050                		-- quantity if the order UOM is different from primary UOM of the item.
3051                		-- If ordered UOM and primary UOM are same
3052                		-- the function returns the ordered quantity.
3053 
3054                		CTO_UTILITY_PK.convert_uom(
3055 					bet2.primary_uom_code,
3056 					msi.primary_uom_code,
3057 					bet2.extended_quantity,
3058 					bic.component_item_id),
3059                		msi.primary_uom_code,
3060                		bet.top_item_id,
3061                		1,
3062                		xGrpId
3063            	from
3064 			bom_bill_of_materials    bom,
3065 			bom_inventory_components bic,
3066 			mtl_system_items msi,					-- bugfix 1998386
3067 			bom_explosion_temp  bet,
3068                  	bom_explosion_temp  bet2        /* ship set */
3069            	where bet.group_id          = xGrpId
3070            	and   bet.plan_level        = level_number -1
3071            	and   bic.component_item_id = msi.inventory_item_id 		-- bugfix 1998386
3072 	   	and   bet.component_item_id = bom.assembly_item_id
3073 	   	and   bet.organization_id   = bom.organization_id
3074 	   	and   bet.organization_id   = msi.organization_id 		-- bugfix 1998386
3075 	   	and   bom.alternate_bom_designator is null
3076 	   	and   bom.common_bill_sequence_id = bic.bill_sequence_id
3077 	   	and   bic.component_item_id = bet2.component_item_id
3078 	   	and   bet2.group_id         = lGroupid2
3079 		-- bugfix 3893281 : Add following filter conditions to get
3080                 -- sub models , option classes , ato items and phantom option items
3081                 and   bic.optional = 1
3082                 and   ( msi.bom_item_type in (1,2)
3083                         OR (msi.bom_item_type = 4 and bic.wip_supply_type = 6 )
3084                         OR (msi.bom_item_type = 4 and msi.replenish_to_order_flag = 'Y' ));
3085                 -- end bugfix 3893281
3086 
3087            	rowcount := SQL%ROWCOUNT;
3088 
3089            	IF PG_DEBUG <> 0 THEN
3090            		oe_debug_pub.add ('load_mandatory_components: ' || 'Row Count : '   || rowcount, 2);
3091            	END IF;
3092 
3093         	END LOOP;
3094 
3095 		-- 3893281 following code is to debug BET at this point
3096                 open debug_bet;
3097                 oe_debug_pub.add ('BET picture after Model and its children are inserted ..' ,1);
3098                 loop
3099                 fetch debug_bet
3100                 into d_top_bseq_id,
3101                      d_bseq_id,
3102                      d_org_id,
3103                      d_sort_order,
3104                      d_plan_level,
3105                      d_line_id,
3106                      d_item;
3107                 exit when debug_bet%NOTFOUND;
3108                 IF PG_DEBUG <> 0 THEN
3109                      oe_debug_pub.add (' Top Bill Seq id '||d_top_bseq_id|| ' Bill Seq Id '||d_bseq_id
3110                                   ||' Org '||d_org_id ||' Sort Order '||d_sort_order
3111                                   || ' Plan level '||d_plan_level||' Line Id '||d_line_id
3112                                   || ' Item '||d_item,1);
3113                 END IF;
3114                 end loop;
3115                 close debug_bet;
3116                 -- 3893281
3117 
3118 
3119 
3120         v_max_level_number := level_number ; /* change made by sushant to track max level number */
3121 
3122         IF PG_DEBUG <> 0 THEN
3123         	oe_debug_pub.add ('load_mandatory_components: ' || 'Group_id  : '|| xGrpId, 2);
3124         END IF;
3125 
3126         delete from bom_explosion_temp
3127 		where group_id = lGroupid2;
3128 
3129        /*
3130        ** BUG no 1288823, new query block added
3131        */
3132 
3133        begin
3134           select msi.fixed_lead_time , nvl(msi.variable_lead_time,0)
3135           into   v_msi_fixed_lead_time, v_msi_variable_lead_time
3136           from   mtl_system_items msi , bom_explosion_temp be
3137           where  be.organization_id   = msi.organization_id
3138           and    be.component_item_id = msi.inventory_item_id
3139           and    be.component_item_id = p_ship_set.inventory_item_id(p_model_index);
3140 
3141        exception
3142 
3143           when no_data_found then
3144         	xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ';
3145         	return(0);
3146           when others then
3147         	xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': other error ';
3148         	return(0);
3149        end ;
3150 
3151         /*------------------------------------------------------------+
3152            Start Explosion of all items copied above.
3153         +-----------------------------------------------------------*/
3154 
3155         level_number := 0;
3156         IF PG_DEBUG <> 0 THEN
3157         	oe_debug_pub.add('load_mandatory_components: ' || 'Explosion loop' , 2);
3158         END IF;
3159 
3160         << Explode_loop >>
3161         LOOP
3162 
3163         lStmtNumber := 620;
3164 	-- Bug 1985793 Selecting greater of sysdate and calendar date
3165             -- while checking for item effectivity so that planning will get
3166             -- components effective till sysdate.
3167 	--apps performance bug#4905845, sql id 16103671
3168         insert into bom_explosion_temp(
3169            top_bill_sequence_id,
3170            bill_sequence_id,
3171            organization_id,
3172            sort_order,
3173            component_sequence_id,
3174            component_item_id,
3175            plan_level,
3176            extended_quantity,
3177            primary_uom_code,		--  1998386
3178            top_item_id,
3179            component_quantity,
3180            check_atp,
3181            atp_components_flag,
3182            atp_flag,
3183            bom_item_type,
3184            assembly_item_id,
3185            parent_bom_item_type,
3186            line_id,
3187            group_id)
3188       select
3189            be.top_bill_sequence_id,
3190            bic.bill_sequence_id,
3191            be.organization_id,
3192            evaluate_order( msi2.atp_flag, msi2.atp_components_flag , msi2.bom_item_type ) ,  /* BUG# 1518894 , 1288823  */
3193            bic.component_sequence_id,
3194            bic.component_item_id,
3195            level_number + 1,
3196            be.extended_quantity * bic.component_quantity,
3197            msi2.primary_uom_code,
3198            be.top_item_id,
3199            bic.component_quantity,
3200            bic.check_atp,
3201 	        --2378556
3202            msi2.atp_components_flag,
3203            msi2.atp_flag,
3204            msi2.bom_item_type,
3205 
3206            bom.assembly_item_id,
3207            msi.bom_item_type,
3208            NULL,
3209            xGrpId
3210        from
3211            bom_calendar_dates       cal,
3212            mtl_system_items         msi,         /* PARENT */
3213            mtl_system_items         msi2,         /* CHILD [BUG#1518894] */
3214            bom_inventory_components bic,
3215            eng_revised_items        eri,
3216            bom_bill_of_materials    bom,
3217            mtl_parameters           mp,
3218            bom_explosion_temp       be
3219        where be.sort_order <> 3
3220        and   be.group_id = xGrpId
3221        and   nvl(be.plan_level,0) = level_number
3222        and   be.organization_id   = bom.organization_id
3223        and   be.component_item_id = bom.assembly_item_id
3224        and   bic.component_quantity <> 0
3225        and   bic.revised_item_sequence_id = eri.revised_item_sequence_id (+)
3226        and   bic.component_item_id = msi2.inventory_item_id  -- 1518894
3227        and   bom.organization_id = msi2.organization_id      --1518894
3228        and   be.organization_id   = msi.organization_id
3229        and   be.component_item_id = msi.inventory_item_id
3230        and   bom.alternate_bom_designator is null
3231        and   bic.bill_sequence_id = bom.common_bill_sequence_id
3232        and   mp.organization_id   = be.organization_id
3233        and   cal.calendar_code    = mp.calendar_code
3234        and   cal.exception_set_id = mp.calendar_exception_set_id
3235        and   cal.calendar_date =
3236                  ( select c.calendar_date
3237                    from   bom_calendar_dates C
3238                    where  C.calendar_code = mp.calendar_code
3239                    and    c.exception_set_id = mp.calendar_exception_set_id
3240                    and    C.seq_num =
3241                       (select c2.prior_seq_num -
3242                         ceil(nvl(v_msi_fixed_lead_time,0)+
3243                                  (be.extended_quantity  *
3244                          nvl(v_msi_variable_lead_time,0)))
3245                        from bom_calendar_dates c2
3246                        where c2.calendar_code = mp.calendar_code
3247                        and   c2.exception_set_id = mp.calendar_exception_set_id
3248                        and   c2.calendar_date = trunc(pReqDate)
3249                        )
3250                   )
3251               --  2162912
3252             and	  TRUNC(bic.effectivity_date) <= greatest(nvl(cal.calendar_date,sysdate),sysdate)
3253             and   nvl(TRUNC(bic.disable_date),(nvl(cal.calendar_date,sysdate) + 1)) > nvl(cal.calendar_date,sysdate)
3254 
3255             and   bic.effectivity_date =
3256                    (select
3257                          max(effectivity_date)
3258                     from bom_inventory_components bic1,
3259                          eng_revised_items eri
3260                     where bic1.bill_sequence_id = bic.bill_sequence_id
3261                     and   bic1.component_item_id = bic.component_item_id
3262                     and   bic1.revised_item_sequence_id =
3263                           eri.revised_item_sequence_id (+)
3264                     and   (decode(bic1.implementation_date, NULL,
3265                             bic1.old_component_sequence_id,
3266                             bic1.component_sequence_id) =
3267                             decode(bic.implementation_date, NULL,
3268                                    bic.old_component_sequence_id,
3269                                    bic.component_sequence_id)
3270                            OR
3271                            bic1.operation_seq_num = bic.operation_seq_num)
3272                  --  2162912
3273             and   TRUNC(bic1.effectivity_date) <= greatest(nvl(cal.calendar_date,sysdate),sysdate)
3274             and   nvl(TRUNC(bic1.disable_date),(nvl(cal.calendar_date,sysdate) + 1)) > nvl(cal.calendar_date,sysdate)
3275 
3276             and   ( nvl(eri.status_type,6) IN (4,6,7))
3277             and not exists
3278                      (select
3279                           'X'
3280                       from bom_inventory_components bicn, eng_revised_items eri1
3281                       where bicn.bill_sequence_id + 0 = bic.bill_sequence_id
3282                       and   bicn.old_component_sequence_id =
3283                             bic.component_sequence_id
3284                       and   bicn.acd_type in (2,3)
3285                       and   eri1.revised_item_sequence_id =
3286                               bicn.revised_item_sequence_id
3287                       and    trunc(bicn.disable_date) <= cal.calendar_date
3288                       and   ( nvl(eri1.status_type,6) in (4,6,7))
3289              )
3290                    )
3291             and   bic.optional = 2        /* NOT OPTIONAL */
3292             and   msi2.bom_item_type = 4 /* BUGFIX 2400948 */
3293                  --  Model or Option Class or ATO ITEM * * BUG#2378556  bug 3314297 mandatory comps should be exploded for
3294                  --   standard items
3295             and   msi.pick_components_flag <> 'Y' ;
3296 
3297             IF PG_DEBUG <> 0 THEN
3298             	oe_debug_pub.add('load_mandatory_components: ' || 'Row Count : '   || SQL%ROWCOUNT);
3299             END IF;
3300 
3301             /* continue exploding atleast till max level number */
3302             IF SQL%ROWCOUNT = 0 AND level_number >= v_max_level_number THEN
3303                EXIT Explode_loop;
3304             END IF;
3305 
3306             level_number := level_number + 1;
3307         END LOOP;
3308      END IF;     -- Bugfix 2047428
3309 
3310      return(1);
3311 
3312 exception
3313 
3314     when no_data_found then
3315         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ';
3316         return(0);
3317     when proc_error then
3318         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': Model row not found ';
3319         return(0);
3320     when others then	--bugfix 1902818: added others excpn
3321         xErrorMessage := 'load_mandatory_components:(others excepn)::'||to_char(lStmtNumber)||'::'||substrb(sqlerrm,1,150);
3322         return(0);
3323 
3324 end load_mandatory_components;
3325 
3326 
3327 
3328 
3329 -- 3779636
3330 function load_mandatory_components_pds(
3331          p_ship_set             in     MRP_ATP_PUB.ATP_Rec_Typ,
3332          p_model_index          in     number,
3333 	 pReqDate               in     oe_order_lines_all.schedule_ship_date%type,
3334          xGrpId                 out NOCOPY   number,
3335          xErrorMessage          out NOCOPY   varchar2  ,
3336          xMessageName           out NOCOPY   varchar2  ,
3337          xTableName             out NOCOPY   varchar2  )
3338 return integer
3339 is
3340 
3341 	level_number          		number := 0;
3342 	v_max_level_number          	number := 0;
3343 	lStmtNumber           		number := 0;
3344 	lGrpId                		number;
3345 	lGroupid2             		number;
3346 	rowcount              		number := 1;
3347 	proc_error            		exception ;
3348 	i                     		number;
3349 
3350 	v_msi_fixed_lead_time       	mtl_system_items.fixed_lead_time%type ;
3351 	v_msi_variable_lead_time    	mtl_system_items.variable_lead_time%type ;
3352 	latpcompflag            	mtl_system_items.atp_components_flag%type;
3353 
3354 
3355 begin
3356 
3357 
3358 
3359 
3360         IF PG_DEBUG <> 0 THEN
3361         	oe_debug_pub.add ('load_mandatory_components_pds: ' || ' load mandatory components PDS ' ,1);
3362         END IF;
3363 
3364          if( pReqDate is not null ) then
3365         IF PG_DEBUG <> 0 THEN
3366         	oe_debug_pub.add ('load_mandatory_components_pds: ' || 'parameters passed index ' || p_model_index || ' date ' || to_char( pReqDate ) ,1);
3367         END IF;
3368          else
3369         IF PG_DEBUG <> 0 THEN
3370         	oe_debug_pub.add ('load_mandatory_components_pds: ' || 'parameters passed index date null ' || p_model_index  ,1);
3371         END IF;
3372         end if ;
3373 
3374 
3375         lStmtNumber := 599;
3376         select  NVL(msi.atp_components_flag,'N')
3377         into    latpcompflag
3378         from    mtl_system_items msi
3379         where   msi.inventory_item_id  =  p_ship_set.inventory_item_id(p_model_index)
3380         and     msi.organization_id    =  p_ship_set.source_organization_id(p_model_index);
3381 
3382 
3383     	If latpcompflag in ( 'N' , 'R' )  then	-- 3779636
3384         	IF PG_DEBUG <> 0 THEN
3385         		oe_debug_pub.add ('load_mandatory_components_pds: ' || 'Exiting Load Mandatory Components at model level as ATP Component flag is N or R',1);	-- 3779636
3386         	END IF;
3387         	return(1);
3388 
3389     	else
3390 		select bom_explosion_temp_s.nextval
3391 		into  lGroupid2
3392 		from dual;
3393 
3394         	select bom_explosion_temp_s.nextval
3395         	into   xGrpId
3396         	from dual;
3397 
3398     		lStmtNumber := 600;
3399 
3400 
3401      		i := p_ship_set.inventory_item_id.FIRST;
3402 
3403      		-- use bom_explosion_temp table to store the ship set for later join
3404      		while i is not null
3405      		loop
3406        			insert into bom_explosion_temp(
3407 		 		top_bill_sequence_id,
3408 		 		bill_sequence_id,
3409 		 		organization_id,
3410 		 		component_item_id,
3411 		 		plan_level,
3412 		 		extended_quantity,
3413 		 		primary_uom_code,
3414 		 		sort_order,
3415 		 		group_id)
3416 		 	select
3417 		 		1,
3418 		 		1,
3419 		 		p_ship_set.source_organization_id(i),
3420 		 		p_ship_set.inventory_item_id(i),
3421 		 		1,
3422 		 		CTO_UTILITY_PK.convert_uom(
3423 						p_ship_set.quantity_uom(i),
3424 						msi.primary_uom_code,
3425 						p_ship_set.quantity_ordered(i),
3426 						p_ship_set.inventory_item_id(i)),
3427          	 		msi.primary_uom_code,
3428 		 		2,
3429 		 		lGroupid2
3430 		 	from mtl_system_items msi
3431 		 	where msi.inventory_item_id = p_ship_set.inventory_item_id(i)
3432 		 	and   msi.organization_id = p_ship_set.source_organization_id(i);
3433 
3434 	 		i := p_ship_set.inventory_item_id.NEXT(i);
3435 
3436 	 	end loop;
3437 
3438 
3439 
3440     		-- insert the top model into bom_explosion_temp
3441     		insert into bom_explosion_temp(
3442         		top_bill_sequence_id,
3443         		bill_sequence_id,
3444         		organization_id,
3445         		sort_order,
3446         		component_item_id,
3447         		plan_level,
3448         		extended_quantity,
3449         		primary_uom_code,
3450         		top_item_id,
3451         		line_id,
3452         		group_id)
3453                 	select
3454 		 		1,
3455 		 		1,
3456 		 		p_ship_set.source_organization_id(p_model_index),
3457 		 		2,                -- BUG no 1288823 modification
3458 		 		p_ship_set.inventory_item_id(p_model_index),
3459 		 		0,
3460 		 		CTO_UTILITY_PK.convert_uom(
3461 						p_ship_set.quantity_uom(p_model_index),
3462 						msi.primary_uom_code,
3463 						p_ship_set.quantity_ordered(p_model_index),
3464 						p_ship_set.inventory_item_id(p_model_index)),
3465          	 		msi.primary_uom_code,
3466          	 		p_ship_set.inventory_item_id(p_model_index),
3467 		 		1,
3468 		 		xGrpId
3469 		 	from mtl_system_items msi
3470 		 	where msi.inventory_item_id = p_ship_set.inventory_item_id(p_model_index)
3471 		 	and   msi.organization_id = p_ship_set.source_organization_id(p_model_index);
3472 
3473 
3474         /*------------------------------------------------+
3475            Insert all selections for the top model
3476            with corrosponding plan_levels
3477         +------------------------------------------------*/
3478         	rowcount := 1 ;
3479 
3480         	while rowcount > 0
3481 		LOOP
3482 
3483             	level_number := level_number + 1;
3484 
3485             	lStmtNumber := 610;
3486             	insert into bom_explosion_temp(
3487                		top_bill_sequence_id,
3488                		bill_sequence_id,
3489                		organization_id,
3490                		sort_order,
3491                		component_item_id,
3492                		plan_level,
3493                		extended_quantity,
3494                		primary_uom_code,
3495                		top_item_id,
3496                		line_id,
3497                		group_id)
3498            	select
3499                		1,
3500                		1,
3501                		bet.organization_id,
3502 	       		2,
3503                		bic.component_item_id,
3504                		level_number,
3505                		CTO_UTILITY_PK.convert_uom(
3506 					bet2.primary_uom_code,
3507 					msi.primary_uom_code,
3508 					bet2.extended_quantity,
3509 					bic.component_item_id),
3510                		msi.primary_uom_code,
3511                		bet.top_item_id,
3512                		1,
3513                		xGrpId
3514            	from
3515 			bom_bill_of_materials    bom,
3516 			bom_inventory_components bic,
3517 			mtl_system_items msi,
3518 			bom_explosion_temp  bet,
3519                  	bom_explosion_temp  bet2
3520            	where bet.group_id          = xGrpId
3521            	and   bet.plan_level        = level_number -1
3522            	and   bic.component_item_id = msi.inventory_item_id
3523 		and   msi.bom_item_type in ( 1, 2 )     /* Only Sub-models and Option Classes */
3524 	   	and   bet.component_item_id = bom.assembly_item_id
3525 	   	and   bet.organization_id   = bom.organization_id
3526 	   	and   bet.organization_id   = msi.organization_id
3527 	   	and   bom.alternate_bom_designator is null
3528 	   	and   bom.common_bill_sequence_id = bic.bill_sequence_id
3529 	   	and   bic.component_item_id = bet2.component_item_id
3530 	   	and   bet2.group_id         = lGroupid2;
3531 
3532            	rowcount := SQL%ROWCOUNT;
3533 
3534            	IF PG_DEBUG <> 0 THEN
3535            		oe_debug_pub.add ('load_mandatory_components_pds: ' || 'Row Count : '   || rowcount, 2);
3536            	END IF;
3537 
3538         	END LOOP;
3539 
3540 
3541 
3542         v_max_level_number := level_number ;
3543 
3544         IF PG_DEBUG <> 0 THEN
3545         	oe_debug_pub.add ('load_mandatory_components_pds: ' || 'Group_id  : '|| xGrpId, 2);
3546         END IF;
3547 
3548         delete from bom_explosion_temp
3549 		where group_id = lGroupid2;
3550 
3551        begin
3552           select msi.fixed_lead_time , nvl(msi.variable_lead_time,0)
3553           into   v_msi_fixed_lead_time, v_msi_variable_lead_time
3554           from   mtl_system_items msi , bom_explosion_temp be
3555           where  be.organization_id   = msi.organization_id
3556           and    be.component_item_id = msi.inventory_item_id
3557           and    be.component_item_id = p_ship_set.inventory_item_id(p_model_index);
3558 
3559        exception
3560 
3561           when no_data_found then
3562         	xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ';
3563         	return(0);
3564           when others then
3565         	xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': other error ';
3566         	return(0);
3567        end ;
3568 
3569         /*------------------------------------------------------------+
3570            Start Explosion of all items copied above.
3571         +-----------------------------------------------------------*/
3572 
3573         level_number := 0;
3574         IF PG_DEBUG <> 0 THEN
3575         	oe_debug_pub.add('load_mandatory_components_pds: ' || 'Explosion loop' , 2);
3576         END IF;
3577 
3578         << Explode_loop >>
3579         LOOP
3580 
3581         lStmtNumber := 620;
3582 	--appsperf bug#4905845, sql id 16103821
3583 	--removed comments to reduce shared memory
3584         insert into bom_explosion_temp(
3585            top_bill_sequence_id,
3586            bill_sequence_id,
3587            organization_id,
3588            sort_order,
3589            component_sequence_id,
3590            component_item_id,
3591            plan_level,
3592            extended_quantity,
3593            primary_uom_code,
3594            top_item_id,
3595            component_quantity,
3596            check_atp,
3597            atp_components_flag,
3598            atp_flag,
3599            bom_item_type,
3600            assembly_item_id,
3601            parent_bom_item_type,
3602            line_id,
3603            group_id)
3604       select
3605            be.top_bill_sequence_id,
3606            bic.bill_sequence_id,
3607            be.organization_id,
3608            evaluate_order( msi2.atp_flag, msi2.atp_components_flag , msi2.bom_item_type ) ,
3609            bic.component_sequence_id,
3610            bic.component_item_id,
3611            level_number + 1,
3612            be.extended_quantity * bic.component_quantity,
3613            msi2.primary_uom_code,
3614            be.top_item_id,
3615            bic.component_quantity,
3616            bic.check_atp,
3617            msi2.atp_components_flag,
3618            msi2.atp_flag,
3619            msi2.bom_item_type,
3620            bom.assembly_item_id,
3621            msi.bom_item_type,
3622            NULL,
3623            xGrpId
3624        from
3625            bom_calendar_dates       cal,
3626            mtl_system_items         msi,
3627            mtl_system_items         msi2,
3628            bom_inventory_components bic,
3629            eng_revised_items        eri,
3630            bom_bill_of_materials    bom,
3631            mtl_parameters           mp,
3632            bom_explosion_temp       be
3633        where be.sort_order <> 3
3634        and   be.group_id = xGrpId
3635        and   nvl(be.plan_level,0) = level_number
3636        and   be.organization_id   = bom.organization_id
3637        and   be.component_item_id = bom.assembly_item_id
3638        and   bic.component_quantity <> 0
3639        and   bic.revised_item_sequence_id = eri.revised_item_sequence_id (+)
3640        and   bic.component_item_id = msi2.inventory_item_id
3641        and   bom.organization_id = msi2.organization_id
3642        and   be.organization_id   = msi.organization_id
3643        and   be.component_item_id = msi.inventory_item_id
3644        and   bom.alternate_bom_designator is null
3645        and   bic.bill_sequence_id = bom.common_bill_sequence_id
3646        and   mp.organization_id   = be.organization_id
3647        and   cal.calendar_code    = mp.calendar_code
3648        and   cal.exception_set_id = mp.calendar_exception_set_id
3649        and   cal.calendar_date =
3650                  ( select c.calendar_date
3651                    from   bom_calendar_dates C
3652                    where  C.calendar_code = mp.calendar_code
3653                    and    c.exception_set_id = mp.calendar_exception_set_id
3654                    and    C.seq_num =
3655                       (select c2.prior_seq_num -
3656                         ceil(nvl(v_msi_fixed_lead_time,0)+
3657                                  (be.extended_quantity  *
3658                          nvl(v_msi_variable_lead_time,0)))
3659                        from bom_calendar_dates c2
3660                        where c2.calendar_code = mp.calendar_code
3661                        and   c2.exception_set_id = mp.calendar_exception_set_id
3662                        and   c2.calendar_date = trunc(pReqDate)
3663                        )
3664                   )
3665             and	  TRUNC(bic.effectivity_date) <= greatest(nvl(cal.calendar_date,sysdate),sysdate)
3666             and   nvl(TRUNC(bic.disable_date),(nvl(cal.calendar_date,sysdate) + 1)) > nvl(cal.calendar_date,sysdate)
3667             and   bic.effectivity_date =
3668                    (select
3669                          max(effectivity_date)
3670                     from bom_inventory_components bic1,
3671                          eng_revised_items eri
3672                     where bic1.bill_sequence_id = bic.bill_sequence_id
3673                     and   bic1.component_item_id = bic.component_item_id
3674                     and   bic1.revised_item_sequence_id =
3675                           eri.revised_item_sequence_id (+)
3676                     and   (decode(bic1.implementation_date, NULL,
3677                             bic1.old_component_sequence_id,
3678                             bic1.component_sequence_id) =
3679                             decode(bic.implementation_date, NULL,
3680                                    bic.old_component_sequence_id,
3681                                    bic.component_sequence_id)
3682                            OR
3683                            bic1.operation_seq_num = bic.operation_seq_num)
3684             and   TRUNC(bic1.effectivity_date) <= greatest(nvl(cal.calendar_date,sysdate),sysdate)
3685             and   nvl(TRUNC(bic1.disable_date),(nvl(cal.calendar_date,sysdate) + 1)) > nvl(cal.calendar_date,sysdate)
3686             and   ( nvl(eri.status_type,6) IN (4,6,7))
3687             and not exists
3688                      (select
3689                           'X'
3690                       from bom_inventory_components bicn, eng_revised_items eri1
3691                       where bicn.bill_sequence_id + 0 = bic.bill_sequence_id
3692                       and   bicn.old_component_sequence_id =
3693                             bic.component_sequence_id
3694                       and   bicn.acd_type in (2,3)
3695                       and   eri1.revised_item_sequence_id =
3696                               bicn.revised_item_sequence_id
3697                       and    trunc(bicn.disable_date) <= cal.calendar_date
3698                       and   ( nvl(eri1.status_type,6) in (4,6,7))
3699              )
3700                    )
3701             and   bic.optional = 2
3702             and   msi2.bom_item_type = 4
3703 	    and   msi.bom_item_type in (1,2) /*Model or Option Class */
3704 	    and   msi.pick_components_flag <> 'Y' ;
3705 
3706             IF PG_DEBUG <> 0 THEN
3707             	oe_debug_pub.add('load_mandatory_components_pds: ' || 'Row Count : '   || SQL%ROWCOUNT);
3708             END IF;
3709 
3710 
3711             IF SQL%ROWCOUNT = 0 AND level_number >= v_max_level_number THEN
3712                EXIT Explode_loop;
3713             END IF;
3714 
3715             level_number := level_number + 1;
3716         END LOOP;
3717      END IF;
3718 
3719      return(1);
3720 
3721 exception
3722 
3723     when no_data_found then
3724         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ';
3725         return(0);
3726     when proc_error then
3727         xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': Model row not found ';
3728         return(0);
3729     when others then	--bugfix 1902818: added others excpn
3730         xErrorMessage := 'load_mandatory_components_pds:(others excepn)::'||to_char(lStmtNumber)||'::'||substrb(sqlerrm,1,150);
3731         return(0);
3732 
3733 end load_mandatory_components_pds;
3734 
3735 function  Get_Mandatory_Components(
3736          p_ship_set            in           MRP_ATP_PUB.ATP_Rec_Typ,
3737 	 p_organization_id     in           number,
3738 	 p_inventory_item_id   in           number,
3739          x_smc_rec             out  NOCOPY  MRP_ATP_PUB.ATP_Rec_Typ,
3740          xErrorMessage         out  NOCOPY        varchar2,
3741          xMessageName          out  NOCOPY        varchar2,
3742          xTableName            out  NOCOPY        varchar2  )
3743 return integer
3744 is
3745 
3746     lStatus     varchar2(1);
3747     lGrpId      number;
3748     lLineId     number;
3749     lAtpLt      number;
3750     lFixedLt    number;
3751     lVarLt      number;
3752     i           number;
3753     chk         number;
3754     lStmtNumber number;
3755 	lItem_id    number;
3756 	lOrg_id     number;
3757 	lItem_type  number;
3758 	lModelIndex number;
3759 
3760     proc_error  exception;
3761 
3762 	-- query mandatory comps by group id and null line id
3763     cursor mandatory_comps is
3764     select component_item_id cid,
3765            component_sequence_id cseq,
3766            component_quantity cq,
3767            extended_quantity  eq,
3768            primary_uom_code   uom,		-- Bugfix 1998386
3769            plan_level         pl,
3770            line_id,                              -- Bugfix 2897132
3771            wip_supply_type                       -- Bugfix 3254039
3772     from   bom_explosion_temp be
3773     where  be.group_id = lGrpId
3774     and    ( be.line_id  is null or top_bill_sequence_id = -1 ) -- Bugfix 2897132
3775     and    be.sort_order <> 2 ;
3776     --BUG 1288823 modification to send restricted components to atp
3777 
3778 v_request_date  DATE ;
3779 
3780 
3781 l_inv_ctp      varchar2(1);   -- bugfix 3779636
3782 
3783 
3784 begin
3785 
3786    IF PG_DEBUG <> 0 THEN
3787    	oe_debug_pub.add('Get_Mandatory_Components: ' || ' Entered Get Mandatory Components ',1);
3788    END IF;
3789 
3790 
3791    -- Bugfix 3779636
3792 
3793      lStmtNumber := 699;
3794 
3795      l_inv_ctp := fnd_profile.value('INV_CTP');
3796 
3797      IF PG_DEBUG <> 0 THEN
3798         oe_debug_pub.add('Get_Mandatory_Components: INV_CTP ( 4 = PDS ): ' || l_inv_ctp,1);
3799      END IF;
3800 
3801 
3802 
3803      lStmtNumber := 700;
3804 
3805 
3806 
3807         lModelIndex := 1;
3808 
3809      if ( p_organization_id is null and p_inventory_item_id is null) then
3810         -- the calling application is OM
3811 
3812 
3813          if( p_ship_set.requested_ship_date.exists(lModelIndex)) then
3814 
3815               IF PG_DEBUG <> 0 THEN
3816               	oe_debug_pub.add('Get_Mandatory_Components: ' || ' requested ship date exists ',1);
3817               END IF;
3818 
3819 
3820               if( p_ship_set.requested_ship_date(lModelIndex) is not null ) then
3821                     IF PG_DEBUG <> 0 THEN
3822                     	oe_debug_pub.add('Get_Mandatory_Components: ' || ' requested ship date exists ' || p_ship_set.requested_ship_date(lModelIndex) ,1);
3823                     END IF;
3824 
3825               else
3826 
3827                      IF PG_DEBUG <> 0 THEN
3828                      	oe_debug_pub.add('Get_Mandatory_Components: ' || ' requested ship date is null '  ,1);
3829                      END IF;
3830               end if ;
3831 
3832          end if ;
3833 
3834 
3835 
3836          if( p_ship_set.requested_arrival_date.exists(lModelIndex)) then
3837 
3838               IF PG_DEBUG <> 0 THEN
3839               	oe_debug_pub.add('Get_Mandatory_Components: ' || ' requested arrival date exists ',1);
3840               END IF;
3841 
3842 
3843               if( p_ship_set.requested_arrival_date(lModelIndex) is not null ) then
3844                     IF PG_DEBUG <> 0 THEN
3845                     	oe_debug_pub.add('Get_Mandatory_Components: ' || ' requested arrival date exists ' || p_ship_set.requested_arrival_date(lModelIndex) ,1);
3846                     END IF;
3847 
3848               else
3849 
3850                      IF PG_DEBUG <> 0 THEN
3851                      	oe_debug_pub.add('Get_Mandatory_Components: ' || ' requested arrival date is null '  ,1);
3852                      END IF;
3853               end if ;
3854 
3855          end if ;
3856 
3857 
3858          v_request_date := nvl( p_ship_set.requested_ship_date(lModelIndex),
3859                         p_ship_set.requested_arrival_date(lModelIndex)) ;
3860 
3861 
3862          -- Bugfix 2897132
3863          if( p_ship_set.quantity_ordered(lModelIndex) <> 0 ) then
3864 
3865          -- Bugfix 3779636
3866             if l_inv_ctp = 4 then
3867 
3868                 lStatus := load_mandatory_comps_pds(p_ship_set.identifier(lModelIndex),
3869                         v_request_date,
3870                         lGrpId,
3871                         xErrorMessage,
3872                         xMessageName,
3873                         xTableName);
3874                 if lStatus <> 1 then
3875                    IF PG_DEBUG <> 0 THEN
3876                         oe_debug_pub.add('Get_Mandatory_Components: ' ||
3877                               ' Load_mandatory_comps_pds returned with Error ',1);
3878                    END IF;
3879                    raise proc_error;
3880                 end if;
3881 
3882              else
3883 
3884 
3885 
3886 
3887 	         lStatus := load_mandatory_comps(p_ship_set.identifier(lModelIndex),
3888 			v_request_date,
3889 			lGrpId,
3890 			xErrorMessage,
3891 			xMessageName,
3892 			xTableName);
3893 
3894       	         if lStatus <> 1 then
3895        		    IF PG_DEBUG <> 0 THEN
3896        			oe_debug_pub.add('Get_Mandatory_Components: ' || ' Load_mandatory_comps returned with Error ',1);
3897        		    END IF;
3898 
3899 		    raise proc_error;
3900 	         end if;
3901 
3902 
3903              end if ;  -- INV_CTP chk
3904 
3905              -- bugfix 3779636
3906 
3907 
3908 
3909          else
3910 
3911 
3912                 oe_debug_pub.add(' Load_mandatory_comps returned without processing
3913                                    as model qty is 0  ',1);
3914                 return 1 ;
3915 
3916          end if;
3917 
3918 
3919 
3920      else
3921         -- the calling application is Configurator
3922         i := p_ship_set.inventory_item_id.FIRST;
3923 
3924 	while i is not null
3925 	     	loop
3926            	-- find the top model record
3927            	if p_ship_set.source_organization_id(i) = p_organization_id and
3928 			p_ship_set.inventory_item_id(i) = p_inventory_item_id then
3929 			lModelIndex := i;
3930 		      	exit;
3931 		end if;
3932 		i := p_ship_set.inventory_item_id.NEXT(i);
3933          	end loop;
3934 
3935 
3936 
3937 
3938                 IF PG_DEBUG <> 0 THEN
3939                 	oe_debug_pub.add('Get_Mandatory_Components: ' || ' will be calling load_mandatory_components ' , 2);
3940                 END IF;
3941 
3942      v_request_date := nvl( p_ship_set.requested_ship_date(lModelIndex),
3943                         p_ship_set.requested_arrival_date(lModelIndex)) ;
3944 
3945 
3946          -- Bugfix 2897132
3947          if( p_ship_set.quantity_ordered(lModelIndex) <> 0 ) then
3948 
3949 
3950          -- Bugfix 3779636
3951             if l_inv_ctp = 4 then
3952 
3953               IF PG_DEBUG <> 0 THEN
3954                 oe_debug_pub.add('Get_Mandatory_Components: ' ||
3955                                  ' now calling load_mandatory_components_pds ' , 2);
3956               END IF;
3957 
3958               lStatus := load_mandatory_components_pds(p_ship_set,
3959                                 lModelIndex,
3960                                 v_request_date,
3961                                      lGrpId,
3962                                      xErrorMessage,
3963                                      xMessageName,
3964                                      xTableName);
3965               IF PG_DEBUG <> 0 THEN
3966                  oe_debug_pub.add('Get_Mandatory_Components: ' ||
3967                                          'Returned from load_mandatory_components_pds with '||lStatus, 2);
3968               END IF;
3969 
3970               if lStatus <> 1 then
3971                         IF PG_DEBUG <> 0 THEN
3972                                 oe_debug_pub.add('Get_Mandatory_Components: ' ||
3973                                             ' Load_mandatory_components_pds returned with Error ',1);
3974                         END IF;
3975                         raise proc_error;
3976               end if;
3977 
3978             else
3979 
3980 
3981 
3982 
3983                 IF PG_DEBUG <> 0 THEN
3984                 	oe_debug_pub.add('Get_Mandatory_Components: ' || ' now calling load_mandatory_components ' , 2);
3985                 END IF;
3986 
3987 
3988 
3989          	lStatus := load_mandatory_components(p_ship_set,
3990                                 lModelIndex,
3991 				v_request_date,
3992                                      lGrpId,
3993                                      xErrorMessage,
3994                                      xMessageName,
3995                                      xTableName);
3996 		IF PG_DEBUG <> 0 THEN
3997 			oe_debug_pub.add('Get_Mandatory_Components: ' || 'Returned from load_mand_comps with '||lStatus, 2);
3998 		END IF;
3999          	if lStatus <> 1 then
4000             		IF PG_DEBUG <> 0 THEN
4001             			oe_debug_pub.add('Get_Mandatory_Components: ' || ' Load_mandatory_components returned with Error ',1);
4002             		END IF;
4003             		raise proc_error;
4004          	end if;
4005 
4006              end if ;  -- INV_CTP chk
4007 
4008              -- bugfix 3779636
4009 
4010 
4011 
4012 
4013          else
4014 
4015 
4016                 oe_debug_pub.add(' Load_mandatory_components returned without processing
4017                                    as model qty is 0  ',1);
4018                 return 1 ;
4019 
4020          end if;
4021 
4022 
4023      	end if;
4024 
4025      -- for calculating ATP lead time
4026 	 lOrg_id := p_ship_set.source_organization_id(lModelIndex);
4027 	 lItem_id := p_ship_set.inventory_item_id(lModelIndex);
4028      select nvl(fixed_lead_time,0),
4029             nvl(variable_lead_time,0)
4030      into   lFixedLt,
4031             lVarLt
4032      from   mtl_system_items msi
4033      where  msi.inventory_item_id = lItem_id
4034      and    msi.organization_id   = lOrg_id;
4035 
4036      lStmtNumber := 710;
4037 
4038      lAtpLt := ceil( lFixedLt + (p_ship_set.Quantity_Ordered(lModelIndex)  * lVarLt));
4039 
4040 
4041      select count(*)
4042      into chk
4043      from bom_explosion_temp
4044      where group_id = lGrpId
4045     and ( Line_id is null or top_bill_sequence_id = -1) ; /* BugFix 2897132 */
4046 
4047      IF PG_DEBUG <> 0 THEN
4048      	oe_debug_pub.add ('Get_Mandatory_Components: ' || ' Lines exploded ' || chk, 1);
4049      END IF;
4050 
4051      lStmtNumber := 730;
4052 
4053      IF PG_DEBUG <> 0 THEN
4054      	oe_debug_pub.add ('Get_Mandatory_Components: ' || 'Going to Print Mandatory Comps ', 1);
4055      END IF;
4056 
4057 
4058      i := 1;
4059      for nxtrec in mandatory_comps
4060      loop
4061                 /*-------------------------------------------+
4062                   Call MRP's API to extend x_smc_rec and copy
4063                   p_model_rec as default
4064                 *-------------------------------------------*/
4065 
4066 
4067                 lStmtNumber := 740;
4068 		IF PG_DEBUG <> 0 THEN
4069 			oe_debug_pub.add ('Get_Mandatory_Components: ' || '        Before calling MRP API::i='||to_char(i),1);
4070 		END IF;
4071                 MRP_ATP_PVT.assign_Atp_input_rec(p_ship_set,
4072                                                  lModelIndex,
4073                                                  x_smc_rec,
4074                                                  lStatus );
4075 		IF PG_DEBUG <> 0 THEN
4076 			oe_debug_pub.add ('Get_Mandatory_Components: ' || '        MRP API returns with '||lStatus, 2);
4077 		END IF;
4078 
4079                 if  lStatus  <> FND_API.G_RET_STS_SUCCESS then
4080                     xErrorMessage := 'assign_Atp_input_rec returned with Error';
4081                     raise proc_error;
4082                 end if;
4083 
4084                 x_smc_rec.inventory_item_id(i) := nxtrec.cid ;
4085                 x_smc_rec.quantity_ordered(i)  := nxtrec.eq ;
4086                 x_smc_rec.quantity_uom(i)      := nxtrec.uom ;		-- Bugfix 1998386
4087                 x_smc_rec.atp_lead_time(i)     := lAtpLt;
4088 
4089                 -- Bugfix 2897132
4090                 if( nxtrec.line_id is not null ) then
4091                 x_smc_rec.identifier(i) := nxtrec.line_id;
4092                 end if ;
4093 
4094                 x_smc_rec.attribute_01(i)     := nxtrec.wip_supply_type;	-- bugfix 3254039
4095 
4096 
4097                 IF PG_DEBUG <> 0 THEN
4098                 	oe_debug_pub.add('Get_Mandatory_Components: ' ||  'Mand comp ' || i ||
4099                                   ' inv ' || x_smc_rec.inventory_item_id(i) ||
4100                                   ' eq ' || x_smc_rec.quantity_ordered(i) ||
4101                                   ' id ' || x_smc_rec.identifier(i)  ||
4102                                   ' wip supply type ' || x_smc_rec.attribute_01(i) , 1 ) ;		-- bugfix 3254039
4103 
4104                 END IF;
4105 
4106 
4107 
4108 
4109                 i := i + 1;
4110      end loop;
4111 
4112 
4113  IF PG_DEBUG <> 0 THEN
4114  	oe_debug_pub.add ('Get_Mandatory_Components: ' || 'Done Print Mandatory Comps ', 1);
4115  END IF;
4116 
4117 
4118      /*------------------------------------+
4119         Clean up bom_explosion_temp table
4120      +------------------------------------*/
4121      lStmtNumber := 750;
4122 
4123      delete from bom_explosion_temp
4124      where group_id = lGrpId;
4125 
4126      return(1);
4127 
4128   exception
4129   when no_data_found then
4130        xErrorMessage := 'CTOCITMB:'||to_char(lStmtNumber)||': No Data Found ' ;
4131        return (0);
4132   when proc_error then
4133        xErrorMessage := 'CTOCITMB :'|| to_char(lStmtNumber) || ': '  || xErrorMessage;
4134        return(0);
4135   when others then
4136        IF PG_DEBUG <> 0 THEN
4137        	oe_debug_pub.add ('Get_Mandatory_Components: ' || 'Others exception is get_mand_comp::'||substrb(sqlerrm,1,150));
4138        END IF;
4139        return(0);
4140 
4141 end Get_Mandatory_Components;
4142 
4143 
4144 
4145 /*-----------------------------------------------------+
4146   Creates configuration item in mtl_system_items.
4147   This function does not check the validity of
4148   data in oe_order_lines. The validity checks
4149   like 'check that config row does not exists already'
4150   are implemented in activity wrapper / batch program
4151 +-----------------------------------------------------*/
4152 
4153 --- The following procedure is modified by Renga Kannan on 08/31/01
4154 --- While instering the rows into MTL_SYSTEM_ITEMS one new function
4155 --- is called for list_price field. CTO_CUSTOM_LIST_PRICE_PK.get_list_price
4156 --- is called to get the list price thru custom calculation. if it is null then
4157 --- it will be copied from its model list price. This change is doen as part of
4158 --- Procuring config Phase I -- Patchset G
4159 
4160 --  We are removing the custom API call as part of patchset H. Instead this API will get called
4161 --  from list price rollup. List price rollup will be called in later part of the program.
4162 --  We don't copy the list price from Patchset H. It will be done by list price rollup program.
4163 --  Fixed by Renga Kannan on 05/14/02
4164 
4165 
4166 FUNCTION Create_Item(
4167         pModelId        in      number,
4168         pLineId         in      number,
4169         pConfigId       in out NOCOPY     number, /* NOCOPY Project */
4170 	xMsgCount	 out NOCOPY   number,
4171         xMsgData        out NOCOPY   varchar2,
4172         p_mode          in     varchar2 default 'AUTOCONFIG' )
4173 RETURN integer
4174 IS
4175 
4176         lCiDel              varchar2(1) ;
4177         lItemType           varchar2(30);
4178 
4179         lNumberMethod       number ;
4180         lStmtNumber         number ;
4181         lFndSize            number ;
4182         app_col_ind         number ;
4183         lNextNum            number ;
4184         lOrderNum           number ;
4185         lLineNum            number ;
4186         lDeliveryNum        number ;
4187         lValidationOrg      number ;
4188         lTempvar            number ;
4189         lProfileVal         number ;
4190         lOpUnit             number ;
4191 	lRcvOrg		    number;
4192 
4193 	lReturnStatus	    varchar2(1);
4194 
4195         lConfigSegName      fnd_id_flex_segments.segment_name%type;
4196         app_column          fnd_id_flex_segments.segment_name%type;
4197         new_item_num        mtl_system_items.segment1%Type ;
4198 
4199         lCreateRules    varchar2(1);
4200         lStatus         number;
4201 
4202         type lSegType is table of
4203         mtl_system_items_interface.segment1%type
4204         index by binary_integer;
4205 
4206         seg               lSegType      ;
4207 
4208         l_model_tab    oe_order_pub.Line_tbl_type;
4209 
4210         -- Start Bugfix 2157740
4211 	cursor 		c_get_org_id  	is
4212 	select 		msi.organization_id	src_org_id
4213 	from            mtl_system_items msi
4214 	where  		msi.inventory_item_id = pConfigId
4215 	and		not exists
4216 		(SELECT  	'x'
4217          	 FROM   	FND_ATTACHED_DOCUMENTS
4218          	 WHERE  	pk1_value   = to_char(msi.organization_id)	-- 2774571
4219          	 AND		pk2_value   = to_char(msi.inventory_item_id)	-- 2774571
4220          	 AND    	entity_name = 'MTL_SYSTEM_ITEMS');
4221 
4222 
4223 	v_src_org_id		mtl_system_items.organization_id%type;
4224 	l_document_id    	Number;
4225 
4226 
4227 	-- End bugfix 2157740
4228 
4229         /* Sushant removed declaration of lDupItem cursor as it is not used anymore .
4230             bug 2706981 was used to provide an alternate logic
4231         */
4232 
4233 
4234         v_program_id              bom_cto_order_lines.program_id%type ;
4235         x_return_status           varchar2(1);
4236 
4237         lOptionNum	    	  number ;		-- 2652379 : new variable
4238         v_ato_line_id  number ;
4239         l_ind_cnt number;  --Bugfix 8305535
4240         sqlcnt    number;  --Bugfix 8305535
4241 BEGIN
4242 
4243 	lReturnStatus	:= fnd_api.G_RET_STS_SUCCESS;
4244 
4245         gUserId          := nvl(Fnd_Global.USER_ID, -1);
4246         gLoginId         := nvl(Fnd_Global.LOGIN_ID, -1);
4247         lCiDel           := FND_PROFILE.Value('BOM:CONFIG_ITEM_DELIMITER');
4248 
4249 
4250 
4251        /* BUG #1957336 Change for preconfigure bom by Sushant Sawant */
4252        /* Added by Sushant for checking preconfigure bom module populated records */
4253        -- bugfix 2267646 Added nvl
4254 
4255         select NVL(program_id,0) , ato_line_id into v_program_id , v_ato_line_id
4256           from bom_cto_order_lines where line_id = pLineId ;
4257 
4258 
4259        /* BUG #1957336 Change for preconfigure bom by Sushant Sawant */
4260         /* Changes for patchset J
4261         if( v_program_id = CTO_UTILITY_PK.PC_BOM_PROGRAM_ID ) then
4262         */
4263 
4264 
4265         oe_debug_pub.add('Create_Item: ' ||  'p_mode ' || p_mode ,2);
4266 
4267         if( p_mode = 'PRECONFIG' ) then
4268 
4269             lValidationOrg := CTO_UTILITY_PK.PC_BOM_VALIDATION_ORG ;
4270 
4271         else
4272 
4273             IF PG_DEBUG <> 0 THEN
4274             	oe_debug_pub.add('Create_Item: ' ||  'Before getting validation org',2);
4275             END IF;
4276 
4277             /*
4278             BUG:3484511
4279             ------------------------
4280             select nvl(master_organization_id,-99)	-- bugfix 2646849: master_organization_id can be 0
4281             into   lValidationOrg
4282             from   oe_order_lines_all oel,
4283               oe_system_parameters_all ospa
4284             where  oel.line_id = pLineid
4285             and    nvl(oel.org_id, -1) = nvl(ospa.org_id, -1) --bug 1531691
4286             and    oel.inventory_item_id = pModelId;
4287             */
4288 
4289 
4290 
4291            IF PG_DEBUG <> 0 THEN
4292                 oe_debug_pub.add('Create_Item: ' ||  'Going to fetch Validation Org ' ,2);
4293            END IF;
4294 
4295 
4296            select nvl( oe_sys_parameters.value( 'MASTER_ORGANIZATION_ID' , oel.org_id) , -99)
4297               into lValidationOrg from oe_order_lines_all oel
4298            where oel.line_id = pLineId ;
4299 
4300 
4301         end if ;
4302 
4303 
4304         if lVAlidationOrg = -99 then			-- bugfix 2646849
4305       	 	cto_msg_pub.cto_message('BOM','CTO_VALIDATION_ORG_NOT_SET');
4306 		raise FND_API.G_EXC_ERROR;
4307         end if;
4308 
4309         IF PG_DEBUG <> 0 THEN
4310         	oe_debug_pub.add('Create_Item: ' ||  'Validation Org is :' ||  lValidationOrg,2);
4311         END IF;
4312 
4313 
4314 	--
4315 	-- If the config item is a matched item, enable the matched item
4316 	-- in all orgs. If it is a new item, create a new item in all orgs
4317 	--
4318 
4319 	IF PG_DEBUG <> 0 THEN
4320 		oe_debug_pub.add('Create_Item: ' || 'config_item_id::'||to_char(pConfigId),2);
4321 	END IF;
4322 
4323         IF (pConfigId is NULL) THEN
4324 
4325 		-- config id is not populated in config_item_id
4326 		-- generate a new config item
4327 
4328         	/*--------------------------------------------------+
4329         	Check to see if the config_segment_name exists in
4330 		bom_parameters for the given organization.
4331         	BOM_parameters are organization dependent, we will use
4332         	BOM_parameter settings in the OE validation org.
4333         	+---------------------------------------------------*/
4334 
4335         	lStmtNumber   := 15;
4336         	select  config_segment_name,
4337                 	config_number_method_type
4338         	into    lConfigSegName,
4339                 	lNumberMethod
4340         	from    bom_parameters
4341         	where   organization_id = lValidationOrg;
4342 
4343 
4344         	if lConfigSegName is NULL then
4345 			cto_msg_pub.cto_message('BOM','CTO_CONFIG_SEGMENT_ERROR');
4346 			raise FND_API.G_EXC_ERROR;
4347         	end if;
4348 
4349         	IF PG_DEBUG <> 0 THEN
4350         		oe_debug_pub.add('Create_Item: ' ||  'config Segment is : ' || lConfigSegName,2 );
4351 
4352         		oe_debug_pub.add('Create_Item: ' ||  'Number Method  is : ' || lNumberMethod,2 );
4353         	END IF;
4354 
4355                 /* BUG #2172057 Change for preconfigure bom by Sushant Sawant */
4356                 if( v_program_id = CTO_UTILITY_PK.PC_BOM_PROGRAM_ID ) then
4357                     -- 2663450 : Change the NumberMethod only when it is <> 4.
4358 		  if lNumberMethod <> 4 then
4359                     lNumberMethod := 1 ;
4360 		  end if;
4361 
4362         	    IF PG_DEBUG <> 0 THEN
4363         	    	oe_debug_pub.add('Create_Item: ' ||  'Number Method  Changed due to preconfigure bom restriction of append with sequence number: ' ||
4364 				lNumberMethod,2 );
4365         	    END IF;
4366 
4367                 end if ;
4368 
4369         	lStmtNumber   := 20;
4370 		-- Bugfix 1736339 : sql%notfound will not be raised in case of SELECT, instead NO_DATA_FOUND
4371 		--	  	    exception is raised.
4372 		--                  Added outer join to the query so that we'll default the maximum_size to 40
4373 		--                  Merged stmt# 40 in the same query.
4374 		--		    Since lConfigSegName is available, we will use this instead of joining bom_parameters.
4375 
4376         	select  nvl(fv.maximum_size,40), fs.application_column_name
4377         	into    lFndSize, app_column
4378         	from    -- bom_parameters p,
4379         		fnd_id_flex_segments fs,
4380             		fnd_flex_value_sets fv
4381         	where   --p.organization_id    = lValidationOrg and
4382  		       fs.id_flex_code      = 'MSTK'
4383         	and    fs.id_flex_num       = 101
4384         	and    fs.segment_name      = lConfigSegName		--p.config_segment_name
4385         	and    fs.application_id    = 401   -- INV
4386         	and    fs.flex_value_set_id = fv.flex_value_set_id(+);
4387 
4388 
4389         	app_col_ind := to_number( substrb(app_column,8,length( app_column) -7 ));
4390 
4391  	       	lStmtNumber   := 30;
4392 
4393        	 	select
4394                 	segment1,
4395                 	segment2,
4396                 	segment3,
4397                 	segment4,
4398                 	segment5,
4399                 	segment6,
4400                 	segment7,
4401                 	segment8,
4402                 	segment9,
4403                 	segment10,
4404                 	segment11,
4405                 	segment12,
4406                 	segment13,
4407                 	segment14,
4408                 	segment15,
4409                 	segment16,
4410                 	segment17,
4411                 	segment18,
4412                 	segment19,
4413                 	segment20
4414         	into
4415                 	seg(1),
4416                 	seg(2),
4417                 	seg(3),
4418                 	seg(4),
4419                 	seg(5),
4420                 	seg(6),
4421                 	seg(7),
4422                 	seg(8),
4423                 	seg(9),
4424                 	seg(10),
4425                 	seg(11),
4426                 	seg(12),
4427                 	seg(13),
4428                 	seg(14),
4429                 	seg(15),
4430                 	seg(16),
4431                 	seg(17),
4432                 	seg(18),
4433                 	seg(19),
4434                 	seg(20)
4435         	from   mtl_system_items msi
4436         	where  inventory_item_id = pModelId
4437         	and    organization_id   = lValidationOrg;
4438 
4439         	lStmtNumber   := 50;
4440 
4441         	if lNumberMethod = 1 then
4442                 	select mtl_system_items_B_S.nextval
4443                 	into   lNextNum
4444                 	from dual;
4445 
4446 			-- bugfix 1933740 : Replaced seg(1) with seg(app_col_ind)
4447 
4448                 	select ( substrb(seg(app_col_ind),1, decode(greatest(lFndSize,40),40,lFndSize -1-length(lNextNum),39 - length(lNextNum))) || lCiDel || to_char(lNextNum))
4449                 	into new_item_num
4450                 	from dual;
4451 
4452         	elsif lNumberMethod = 2 then
4453                 	select to_char(mtl_system_items_B_S.nextval)
4454                 	into   new_item_num
4455                 	from dual;
4456 
4457                 -- 2652379 : When numbering method is 3 , i.e Replace with order num, line num;
4458                 -- item numbering segment ( Part num , item num , etc. ) is replaced with order
4459                 -- number + line_number + shipment number. For model under model , item number gets
4460                 -- appended with a sequence since child and parent model are having same number
4461                 -- ( i.e. same order# + line# + shipment# ) .  As we select model with plan level
4462                 -- descending to create corresponding config items , the model at lowest level
4463                 -- ( i.e. having highest plan level ) will get order# + line# + shipment# as its
4464                 -- config item number while for all higher level models it will be  seq. appended
4465                 -- ( i.e. order# + line# + shipment# + seq ).
4466                 -- E.g. For an order # 1000 , line# 1 , shipment# 1 a Bill structure like
4467                 --		M1
4468                 --		.. M2
4469                 --		....M3
4470                 -- will get 1000*1*1 for M3 , 1000*1*1*seq1 for M2 and 1000*1*1*seq2 for M1
4471                 -- After this fix , a new column option_number will be appended so that config
4472                 -- items at different plan level will have unique config item number.
4473                 -- Also this will eliminate the need for checking duplicate config items while
4474                 -- traversing the bill as long as option_number is NOT NULL. Under new numbering
4475                 -- scheme , for the above example , config items will have following numbers :
4476                 -- 1000*1*1 for M1 , 1000*1*1*1 for M2 and 1000*1*1*2 for M3
4477                 -- where M1 , being at top level does not have option number , while M2 and M3
4478                 -- have option# 1 and 2 resp.
4479                 -- Also , in this fix decode on lDeliveryNum to check NULL condition is removed
4480                 -- since this column is defined as NOT NULL in database.
4481 
4482 
4483         	elsif lNumberMethod = 3 then
4484                 	lStmtNumber   := 60;
4485                 	select oeh.order_number,
4486                         	oel.line_number,
4487                         	oel.shipment_number,
4488                         	oel.option_number 		-- 2652379 : new column
4489                 	into   lOrderNum,
4490                         	lLineNum,
4491                         	lDeliveryNum,
4492                         	lOptionNum			-- 2652379 : new variable
4493                 	from   oe_order_lines_all oel,
4494                         	oe_order_headers_all oeh
4495                 	where  oel.line_id = pLineId
4496                 	and    oel.header_id = oeh.header_id;
4497 
4498                 	-- 2652379 Changed decode
4499                 	/*
4500                 	select decode(lDeliveryNum, NULL, lOrderNum || lCiDel || lLineNum,
4501                 					  lOrderNum || lCiDel || lLineNum || lCiDel|| lDeliveryNum )
4502                         into new_item_num
4503                         from dual;
4504                         */
4505                 	select decode (lOptionNum, NULL, lOrderNum || lCiDel || lLineNum || lCiDel|| lDeliveryNum,
4506                 					 lOrderNum || lCiDel || lLineNum || lCiDel|| lDeliveryNum || lCiDel || lOptionNum )
4507                 	into new_item_num
4508                 	from dual;
4509                 	-- end fix 2652379
4510         	else
4511                 	/*--------------------------+
4512                 	Call to user Defined Method
4513                 	+--------------------------*/
4514                 	 -- bugfix 2663450: call custom api ..
4515 
4516                         if( v_program_id = CTO_UTILITY_PK.PC_BOM_PROGRAM_ID ) then
4517                                 new_item_num := CTO_CUSTOM_CONFIG_NUMBER.user_item_number
4518                                         (       model_item_id   =>      pModelId,
4519                                                 model_line_id   =>      NULL,
4520                                                 param1          =>      NULL,
4521                                                 param2          =>      NULL,
4522                                                 param3          =>      NULL,
4523                                                 param4          =>      NULL,
4524                                                 param5          =>      NULL
4525                                         );
4526                                 IF PG_DEBUG <> 0 THEN
4527                             	oe_debug_pub.add( 'Precfg case - Custom Item number is  ' || new_item_num ,2 );
4528                             	END IF;
4529                         else
4530                 		new_item_num := BOMPCFGI.user_item_number(pLineId);
4531                 		IF PG_DEBUG <> 0 THEN
4532                 			oe_debug_pub.add('Create_Item: ' ||  'Item number is  ' || new_item_num ,2 );
4533                 		END IF;
4534                 	end if;
4535 		end if;  -- End check for number method
4536 
4537         	seg(app_col_ind) := new_item_num;
4538 
4539         	lStmtNumber   := 70;
4540         	/*-------------------------------------------------+
4541         	if a item with name new_item_num already exists in
4542         	table, append it with a sequence_number to make it
4543         	unique
4544         	+--------------------------------------------------*/
4545 
4546 		-- bugfix 2706981
4547 		-- Replaced the loop (commented below) with function check_dup_item
4548 		-- which dynamically builts the query to check duplicate item names.
4549 		--
4550 
4551 		if check_dup_item(seg(1), seg(2), seg(3), seg(4), seg(5),
4552 		                     seg(6), seg(7), seg(8), seg(9), seg(10),
4553 		                     seg(11), seg(12), seg(13), seg(14), seg(15),
4554 		                     seg(16), seg(17), seg(18), seg(19), seg(20)) = 1
4555                 then
4556                 	IF PG_DEBUG <> 0 THEN
4557                 	   oe_debug_pub.add( 'Create_Item: '|| 'generating unique name' ,2 );
4558 			END IF;
4559                 	select to_char(mtl_system_items_B_S.nextval)
4560                 	into lNextNum
4561                 	from dual;
4562                 	seg(app_col_ind) := new_item_num || lCiDel || lNextNum;
4563 		end if;
4564 
4565 		/* bugfix 2706981: commented out and replaced with above check_dup_item
4566 		   see above.
4567 
4568         	  for nxt_rec in lDupItem
4569         	  loop
4570 
4571                 	IF PG_DEBUG <> 0 THEN
4572                 		oe_debug_pub.add('Create_Item: ' ||  'generating unique name' ,2 );
4573                 	END IF;
4574                 	select to_char(mtl_system_items_B_S.nextval)
4575                 	into lNextNum
4576                 	from dual;
4577                 	seg(app_col_ind) := new_item_num || lCiDel || lNextNum;
4578                 	exit;
4579         	  end loop;
4580 		*/
4581 
4582         	IF PG_DEBUG <> 0 THEN
4583         		oe_debug_pub.add('Create_Item: ' ||  'item numbering segment is '||  app_col_ind ,2 );
4584 
4585         		oe_debug_pub.add('Create_Item: ' ||  'new_item number is  ' || seg(app_col_ind) ,2 );
4586         	END IF;
4587 
4588         	select to_char(mtl_system_items_b_S.nextval) into pConfigId from dual;
4589 
4590 		IF PG_DEBUG <> 0 THEN
4591 			oe_debug_pub.add('Create_Item: ' ||  'new pConfigId is '||to_char(pConfigId),2);
4592 		END IF;
4593 
4594 	ELSE
4595 		-- config id is populated in config_item_id
4596 		-- use the matched item
4597 
4598 		select distinct
4599 			segment1,
4600 			segment2,
4601                 	segment3,
4602                 	segment4,
4603                 	segment5,
4604                 	segment6,
4605                 	segment7,
4606                 	segment8,
4607                 	segment9,
4608                 	segment10,
4609                 	segment11,
4610                 	segment12,
4611                 	segment13,
4612                 	segment14,
4613                 	segment15,
4614                 	segment16,
4615                 	segment17,
4616                 	segment18,
4617                 	segment19,
4618                 	segment20
4619         	into
4620                 	seg(1),
4621                 	seg(2),
4622                 	seg(3),
4623                 	seg(4),
4624                 	seg(5),
4625                 	seg(6),
4626                 	seg(7),
4627                 	seg(8),
4628                 	seg(9),
4629                 	seg(10),
4630                 	seg(11),
4631                 	seg(12),
4632                 	seg(13),
4633                 	seg(14),
4634                 	seg(15),
4635                 	seg(16),
4636                 	seg(17),
4637                 	seg(18),
4638                 	seg(19),
4639                 	seg(20)
4640         	from   mtl_system_items msi
4641         	where  inventory_item_id = pConfigId;
4642 
4643 		IF PG_DEBUG <> 0 THEN
4644 			oe_debug_pub.add('Create_Item: ' ||  'matched pConfigId is '||to_char(pConfigId),2);
4645 		END IF;
4646 
4647 	END IF; -- matched/new item
4648 
4649 
4650 
4651         -- rkaza. 10/21/2004. bug 3860077. Item type should be null if profile is null.
4652 	lItemType :=  FND_PROFILE.Value('BOM:CONFIG_ITEM_TYPE');
4653 
4654         /*-----------------------------------------------------------+
4655         Insert a row into the  mtl_system_items table.
4656         +------------------------------------------------------------*/
4657 
4658         --xTableName := 'MTL_SYSTEM_ITEMS';
4659         lStmtNumber := 80;
4660 
4661         /* need to add attribute controlled statement for preconfigured item */
4662         oe_debug_pub.add('Create_Item: ' ||  'p_mode is '||  p_mode ,2);
4663 
4664         if( p_mode = 'AUTOCONFIG' OR pLineId <> v_ato_line_id ) then
4665 
4666         oe_debug_pub.add('Create_Item: ' ||  'came into AUTOCONFIG ' ,2);
4667 /*
4668 tracking_quantity_ind TRACK,
4669   4  ont_pricing_qty_source PRCQTY, approval_status
4670 */
4671 
4672         --appsperf bug#	4905845, sql id 16104136
4673 	--decrease comments within sql to max extent to reduce shared memory
4674 
4675         -- Bug 9223457.added additional attribute columns added in 12.1
4676         -- for mtl_sys_items.pdube
4677 
4678 	insert into mtl_system_items_b
4679                 (inventory_item_id,
4680                 organization_id,
4681                 last_update_date,
4682                 last_updated_by,
4683                 creation_date,
4684                 created_by,
4685                 last_update_login,
4686                 summary_flag,
4687                 enabled_flag,
4688                 start_date_active,
4689                 end_date_active,
4690                 description,
4691                 buyer_id,
4692                 accounting_rule_id,
4693                 invoicing_rule_id,
4694                 segment1,
4695                 segment2,
4696                 segment3,
4697                 segment4,
4698                 segment5,
4699                 segment6,
4700                 segment7,
4701                 segment8,
4702                 segment9,
4703                 segment10,
4704                 segment11,
4705                 segment12,
4706                 segment13,
4707                 segment14,
4708                 segment15,
4709                 segment16,
4710                 segment17,
4711                 segment18,
4712                 segment19,
4713                 segment20,
4714                 attribute_category,
4715                 attribute1,
4716                 attribute2,
4717                 attribute3,
4718                 attribute4,
4719                 attribute5,
4720                 attribute6,
4721                 attribute7,
4722                 attribute8,
4723                 attribute9,
4724                 attribute10,
4725                 attribute11,
4726                 attribute12,
4727                 attribute13,
4728                 attribute14,
4729                 attribute15,
4730                 attribute16,  -- Bug 9223457
4731                 attribute17,
4732                 attribute18,
4733                 attribute19,
4734                 attribute20,
4735                 attribute21,
4736                 attribute22,
4737                 attribute23,
4738                 attribute24,
4739                 attribute25,
4740                 attribute26,
4741                 attribute27,
4742                 attribute28,
4743                 attribute29,
4744                 attribute30,
4745                 purchasing_item_flag,
4746                 shippable_item_flag,
4747                 customer_order_flag,
4748                 internal_order_flag,
4749                 service_item_flag,
4750                 inventory_item_flag,
4751                 eng_item_flag,
4752                 inventory_asset_flag,
4753                 purchasing_enabled_flag,
4754                 customer_order_enabled_flag,
4755                 internal_order_enabled_flag,
4756                 so_transactions_flag,
4757                 mtl_transactions_enabled_flag,
4758                 stock_enabled_flag,
4759                 bom_enabled_flag,
4760                 build_in_wip_flag,
4761                 revision_qty_control_code,
4762                 item_catalog_group_id,
4763                 catalog_status_flag,
4764                 returnable_flag,
4765                 default_shipping_org,
4766                 collateral_flag,
4767                 taxable_flag,
4768                 allow_item_desc_update_flag,
4769                 inspection_required_flag,
4770                 receipt_required_flag,
4771                 market_price,
4772                 hazard_class_id,
4773                 rfq_required_flag,
4774                 qty_rcv_tolerance,
4775                 un_number_id,
4776                 price_tolerance_percent,
4777                 asset_category_id,
4778                 rounding_factor,
4779                 unit_of_issue,
4780                 enforce_ship_to_location_code,
4781                 allow_substitute_receipts_flag,
4782                 allow_unordered_receipts_flag,
4783                 allow_express_delivery_flag,
4784                 days_early_receipt_allowed,
4785                 days_late_receipt_allowed,
4786                 receipt_days_exception_code,
4787                 receiving_routing_id,
4788                 invoice_close_tolerance,
4789                 receive_close_tolerance,
4790                 auto_lot_alpha_prefix,
4791                 start_auto_lot_number,
4792                 lot_control_code,
4793                 shelf_life_code,
4794                 shelf_life_days,
4795                 serial_number_control_code,
4796                 start_auto_serial_number,
4797                 auto_serial_alpha_prefix,
4798                 source_type,
4799                 source_organization_id,
4800                 source_subinventory,
4801                 expense_account,
4802                 encumbrance_account,
4803                 restrict_subinventories_code,
4804                 unit_weight,
4805                 weight_uom_code,
4806                 volume_uom_code,
4807                 unit_volume,
4808                 restrict_locators_code,
4809                 location_control_code,
4810                 shrinkage_rate,
4811                 acceptable_early_days,
4812                 planning_time_fence_code,
4813                 demand_time_fence_code,
4814                 lead_time_lot_size,
4815                 std_lot_size,
4816                 cum_manufacturing_lead_time,
4817                 overrun_percentage,
4818                 acceptable_rate_increase,
4819                 acceptable_rate_decrease,
4820                 cumulative_total_lead_time,
4821                 planning_time_fence_days,
4822                 demand_time_fence_days,
4823                 end_assembly_pegging_flag,
4824                 planning_exception_set,
4825                 bom_item_type,
4826                 pick_components_flag,
4827                 replenish_to_order_flag,
4828                 base_item_id,
4829                 atp_components_flag,
4830                 atp_flag,
4831                 fixed_lead_time,
4832                 variable_lead_time,
4833                 wip_supply_locator_id,
4834                 wip_supply_type,
4835                 wip_supply_subinventory,
4836                 primary_uom_code,
4837                 primary_unit_of_measure,
4838                 allowed_units_lookup_code,
4839                 cost_of_sales_account,
4840                 sales_account,
4841                 default_include_in_rollup_flag,
4842                 inventory_item_status_code,
4843                 inventory_planning_code,
4844                 planner_code,
4845                 planning_make_buy_code,
4846                 fixed_lot_multiplier,
4847                 rounding_control_type,
4848                 carrying_cost,
4849                 postprocessing_lead_time,
4850                 preprocessing_lead_time,
4851                 full_lead_time,
4852                 order_cost,
4853                 mrp_safety_stock_percent,
4854                 mrp_safety_stock_code,
4855                 min_minmax_quantity,
4856                 max_minmax_quantity,
4857                 minimum_order_quantity,
4858                 fixed_order_quantity,
4859                 fixed_days_supply,
4860                 maximum_order_quantity,
4861                 atp_rule_id,
4862                 picking_rule_id,
4863                 reservable_type,
4864                 positive_measurement_error,
4865                 negative_measurement_error,
4866                 engineering_ecn_code,
4867                 engineering_item_id,
4868                 engineering_date,
4869                 service_starting_delay,
4870                 vendor_warranty_flag,
4871                 serviceable_component_flag,
4872                 serviceable_product_flag,
4873                 base_warranty_service_id,
4874                 payment_terms_id,
4875                 preventive_maintenance_flag,
4876                 primary_specialist_id,
4877                 secondary_specialist_id,
4878                 serviceable_item_class_id,
4879                 time_billable_flag,
4880                 material_billable_flag,
4881                 expense_billable_flag,
4882                 prorate_service_flag,
4883                 coverage_schedule_id,
4884                 service_duration_period_code,
4885                 service_duration,
4886                 max_warranty_amount,
4887                 response_time_period_code,
4888                 response_time_value,
4889                 new_revision_code,
4890                 tax_code,
4891                 must_use_approved_vendor_flag,
4892                 safety_stock_bucket_days,
4893                 auto_reduce_mps,
4894                 costing_enabled_flag,
4895                 invoiceable_item_flag,
4896                 invoice_enabled_flag,
4897                 outside_operation_flag,
4898                 outside_operation_uom_type,
4899                 auto_created_config_flag,
4900                 cycle_count_enabled_flag,
4901                 item_type,
4902                 model_config_clause_name,
4903                 ship_model_complete_flag,
4904                 mrp_planning_code,
4905                 repetitive_planning_flag,
4906                 return_inspection_requirement,
4907                 effectivity_control,
4908                 request_id,
4909                 program_application_id,
4910                 program_id,
4911                 program_update_date,
4912 		comms_nl_trackable_flag,               -- bugfix 2200256
4913 		default_so_source_type,
4914 		create_supply_flag,
4915 			-- 2336548
4916 		lot_status_enabled,
4917 		default_lot_status_id,
4918 		serial_status_enabled,
4919 		default_serial_status_id,
4920 		lot_split_enabled,
4921 		lot_merge_enabled,
4922 		bulk_picked_flag,
4923 
4924 			-- 2400609
4925 		FINANCING_ALLOWED_FLAG,
4926  		EAM_ITEM_TYPE ,
4927  		EAM_ACTIVITY_TYPE_CODE,
4928  		EAM_ACTIVITY_CAUSE_CODE,
4929  		EAM_ACT_NOTIFICATION_FLAG,
4930  		EAM_ACT_SHUTDOWN_STATUS,
4931  		SUBSTITUTION_WINDOW_CODE,
4932  		SUBSTITUTION_WINDOW_DAYS,
4933  		PRODUCT_FAMILY_ITEM_ID,
4934  		CHECK_SHORTAGES_FLAG,
4935  		PLANNED_INV_POINT_FLAG,
4936  		OVER_SHIPMENT_TOLERANCE,
4937  		UNDER_SHIPMENT_TOLERANCE,
4938  		OVER_RETURN_TOLERANCE,
4939  		UNDER_RETURN_TOLERANCE,
4940  		PURCHASING_TAX_CODE,
4941  		OVERCOMPLETION_TOLERANCE_TYPE,
4942  		OVERCOMPLETION_TOLERANCE_VALUE,
4943  		INVENTORY_CARRY_PENALTY,
4944  		OPERATION_SLACK_PENALTY,
4945  		UNIT_LENGTH,
4946  		UNIT_WIDTH,
4947  		UNIT_HEIGHT,
4948  		LOT_TRANSLATE_ENABLED,
4949  		CONTAINER_ITEM_FLAG,
4950  		VEHICLE_ITEM_FLAG,
4951  		DIMENSION_UOM_CODE,
4952  		SECONDARY_UOM_CODE,
4953  		MAXIMUM_LOAD_WEIGHT,
4954  		MINIMUM_FILL_PERCENT,
4955  		CONTAINER_TYPE_CODE,
4956  		INTERNAL_VOLUME,
4957  		EQUIPMENT_TYPE,
4958  		INDIVISIBLE_FLAG,
4959  		GLOBAL_ATTRIBUTE_CATEGORY,
4960  		GLOBAL_ATTRIBUTE1,
4961  		GLOBAL_ATTRIBUTE2,
4962  		GLOBAL_ATTRIBUTE3,
4963  		GLOBAL_ATTRIBUTE4,
4964  		GLOBAL_ATTRIBUTE5,
4965  		GLOBAL_ATTRIBUTE6,
4966  		GLOBAL_ATTRIBUTE7,
4967  		GLOBAL_ATTRIBUTE8,
4968  		GLOBAL_ATTRIBUTE9,
4969  		GLOBAL_ATTRIBUTE10,
4970 		DUAL_UOM_CONTROL,
4971  		DUAL_UOM_DEVIATION_HIGH,
4972  		DUAL_UOM_DEVIATION_LOW,
4973                 CONTRACT_ITEM_TYPE_CODE,
4974  		SUBSCRIPTION_DEPEND_FLAG,
4975  		SERV_REQ_ENABLED_CODE,
4976  		SERV_BILLING_ENABLED_FLAG,
4977  		RELEASE_TIME_FENCE_CODE,	-- 2898851
4978  		RELEASE_TIME_FENCE_DAYS,	-- 2898851
4979  		DEFECT_TRACKING_ON_FLAG,        -- 2858080
4980  		SERV_IMPORTANCE_LEVEL,
4981 
4982 	        WEB_STATUS ,  --2727983
4983                 tracking_quantity_ind,   --Attribute for Item in patchset J
4984                 ont_pricing_qty_source,
4985                 approval_status ,
4986                 vmi_minimum_units,
4987                 vmi_minimum_days,
4988                 vmi_maximum_units,
4989                 vmi_maximum_days,
4990                 vmi_fixed_order_quantity,
4991                 so_authorization_flag,
4992                 consigned_flag,
4993                 asn_autoexpire_flag,
4994                 vmi_forecast_type,
4995                 forecast_horizon,
4996                 days_tgt_inv_supply,
4997                 days_tgt_inv_window,
4998                 days_max_inv_supply,
4999                 days_max_inv_window,
5000                 critical_component_flag,
5001                 drp_planned_flag,
5002                 exclude_from_budget_flag,
5003                 convergence,
5004                 continous_transfer,
5005                 divergence,
5006 
5007 		--r12 4574899
5008 		lot_divisible_flag,
5009 		grade_control_flag,
5010 		child_lot_flag,
5011                 child_lot_validation_flag,
5012 		copy_lot_attribute_flag,
5013 		parent_child_generation_flag,  --Bugfix 8821149
5014 		lot_substitution_enabled,      --Bugfix 8821149
5015 		recipe_enabled_flag,
5016                 process_quality_enabled_flag,
5017 		process_execution_enabled_flag,
5018 	        process_costing_enabled_flag,
5019 		hazardous_material_flag,
5020 		preposition_point,
5021 		repair_program,
5022 		outsourced_assembly
5023 
5024 
5025                 )
5026         select distinct
5027                 pConfigId,
5028                 m.organization_id,
5029                 sysdate,
5030                 gUserId,
5031                 sysdate,
5032                 gUserId,
5033                 gLoginId ,
5034                 m.summary_flag,
5035                 m.enabled_flag,
5036                 m.start_date_active,
5037                 m.end_date_active,
5038                 m.description,
5039                 m.buyer_id,
5040                 m.accounting_rule_id,
5041                 m.invoicing_rule_id,
5042                 seg(1),
5043                 seg(2),
5044                 seg(3),
5045                 seg(4),
5046                 seg(5),
5047                 seg(6),
5048                 seg(7),
5049                 seg(8),
5050                 seg(9),
5051                 seg(10),
5052                 seg(11),
5053                 seg(12),
5054                 seg(13),
5055                 seg(14),
5056                 seg(15),
5057                 seg(16),
5058                 seg(17),
5059                 seg(18),
5060                 seg(19),
5061                 seg(20),
5062                 m.attribute_category,
5063                 m.attribute1,
5064                 m.attribute2,
5065                 m.attribute3,
5066                 m.attribute4,
5067                 m.attribute5,
5068                 m.attribute6,
5069                 m.attribute7,
5070                 m.attribute8,
5071                 m.attribute9,
5072                 m.attribute10,
5073                 m.attribute11,
5074                 m.attribute12,
5075                 m.attribute13,
5076                 m.attribute14,
5077                 m.attribute15,
5078                 m.attribute16,  -- Bug 9223457
5079                 m.attribute17,
5080                 m.attribute18,
5081                 m.attribute19,
5082                 m.attribute20,
5083                 m.attribute21,
5084                 m.attribute22,
5085                 m.attribute23,
5086                 m.attribute24,
5087                 m.attribute25,
5088                 m.attribute26,
5089                 m.attribute27,
5090                 m.attribute28,
5091                 m.attribute29,
5092                 m.attribute30,
5093                 'Y',
5094                 'Y',
5095                 'Y',
5096                 'Y',
5097                 m.service_item_flag,
5098                 'Y',
5099                 m.eng_item_flag,
5100                 m.inventory_asset_flag,
5101                 'Y',
5102                 'Y',
5103                 'Y',
5104                 'Y',
5105                 'Y',
5106                 'Y',
5107                 'Y',
5108                 'Y',
5109                 m.revision_qty_control_code,
5110                 m.item_catalog_group_id,
5111                 m.catalog_status_flag,
5112                 m.returnable_flag,
5113                 m.default_shipping_org,
5114                 m.collateral_flag,
5115                 m.taxable_flag,
5116                 m.allow_item_desc_update_flag,
5117                 m.inspection_required_flag,
5118                 m.receipt_required_flag,
5119                 m.market_price,
5120                 m.hazard_class_id,
5121                 m.rfq_required_flag,
5122                 m.qty_rcv_tolerance,
5123                 m.un_number_id,
5124                 m.price_tolerance_percent,
5125                 m.asset_category_id,
5126                 m.rounding_factor,
5127                 m.unit_of_issue,
5128                 m.enforce_ship_to_location_code,
5129                 m.allow_substitute_receipts_flag,
5130                 m.allow_unordered_receipts_flag,
5131                 m.allow_express_delivery_flag,
5132                 m.days_early_receipt_allowed,
5133                 m.days_late_receipt_allowed,
5134                 m.receipt_days_exception_code,
5135                 m.receiving_routing_id,
5136                 m.invoice_close_tolerance,
5137                 m.receive_close_tolerance,
5138                 m.auto_lot_alpha_prefix,
5139                 m.start_auto_lot_number,
5140                 m.lot_control_code,
5141                 m.shelf_life_code,
5142                 m.shelf_life_days,
5143                 m.serial_number_control_code,
5144                 m.start_auto_serial_number,
5145                 m.auto_serial_alpha_prefix,
5146                 m.source_type,
5147                 m.source_organization_id,
5148                 m.source_subinventory,
5149                 m.expense_account,
5150                 m.encumbrance_account,
5151                 m.restrict_subinventories_code,
5152 		--  2301167 : we will calculate the unit weight/vol later..
5153                 null,
5154                 null,
5155                 null,
5156                 null,
5157 
5158                 m.restrict_locators_code,
5159                 m.location_control_code,
5160                 m.shrinkage_rate,
5161                 m.acceptable_early_days,
5162                 m.planning_time_fence_code,
5163                 m.demand_time_fence_code,
5164                 m.lead_time_lot_size,
5165                 m.std_lot_size,
5166                 m.cum_manufacturing_lead_time,
5167                 m.overrun_percentage,
5168                 m.acceptable_rate_increase,
5169                 m.acceptable_rate_decrease,
5170                 m.cumulative_total_lead_time,
5171                 m.planning_time_fence_days,
5172                 m.demand_time_fence_days,
5173                 m.end_assembly_pegging_flag,
5174                 m.planning_exception_set,
5175                 4,                                 -- BOM_ITEM_TYPE : standard
5176                 'N',
5177                 'Y',
5178                 pModelId,
5179                 evaluate_atp_attributes( m.atp_flag, m.atp_components_flag ),
5180                 get_atp_flag,
5181                 m.fixed_lead_time,
5182                 m.variable_lead_time,
5183                 m.wip_supply_locator_id,
5184                 m.wip_supply_type,
5185                 m.wip_supply_subinventory,
5186                 m.primary_uom_code,
5187                 m.primary_unit_of_measure,
5188                 m.allowed_units_lookup_code,
5189                 m.cost_of_sales_account,
5190                 m.sales_account,
5191                 'Y',
5192                 m.inventory_item_status_code,
5193                 m.inventory_planning_code,
5194                 m.planner_code,
5195                 m.planning_make_buy_code,
5196                 m.fixed_lot_multiplier,
5197                 m.rounding_control_type,
5198                 m.carrying_cost,
5199                 m.postprocessing_lead_time,
5200                 m.preprocessing_lead_time,
5201                 m.full_lead_time,
5202                 m.order_cost,
5203                 m.mrp_safety_stock_percent,
5204                 m.mrp_safety_stock_code,
5205                 m.min_minmax_quantity,
5206                 m.max_minmax_quantity,
5207                 m.minimum_order_quantity,
5208                 m.fixed_order_quantity,
5209                 m.fixed_days_supply,
5210                 m.maximum_order_quantity,
5211                 m.atp_rule_id,
5212                 m.picking_rule_id,
5213                 1,
5214                 m.positive_measurement_error,
5215                 m.negative_measurement_error,
5216                 m.engineering_ecn_code,
5217                 m.engineering_item_id,
5218                 m.engineering_date,
5219                 m.service_starting_delay,
5220                 m.vendor_warranty_flag,
5221                 m.serviceable_component_flag,
5222                 m.serviceable_product_flag,
5223                 m.base_warranty_service_id,
5224                 m.payment_terms_id,
5225                 m.preventive_maintenance_flag,
5226                 m.primary_specialist_id,
5227                 m.secondary_specialist_id,
5228                 m.serviceable_item_class_id,
5229                 m.time_billable_flag,
5230                 m.material_billable_flag,
5231                 m.expense_billable_flag,
5232                 m.prorate_service_flag,
5233                 m.coverage_schedule_id,
5234                 m.service_duration_period_code,
5235                 m.service_duration,
5236                 m.max_warranty_amount,
5237                 m.response_time_period_code,
5238                 m.response_time_value,
5239                 m.new_revision_code,
5240                 m.tax_code,
5241                 m.must_use_approved_vendor_flag,
5242                 m.safety_stock_bucket_days,
5243                 m.auto_reduce_mps,
5244                 m.costing_enabled_flag,
5245                 m.invoiceable_item_flag,             -- 'N' Changed for international dropship
5246                 m.invoice_enabled_flag,              -- 'N' Changed on OM's request
5247                 m.outside_operation_flag,
5248                 m.outside_operation_uom_type,
5249                 'Y',
5250                 m.cycle_count_enabled_flag,
5251                 lItemType,
5252                 m.model_config_clause_name,
5253                 m.ship_model_complete_flag,
5254                 m.mrp_planning_code,                 -- earlier it was always from one org only
5255                 m.repetitive_planning_flag,          -- earlier it was always from one org only
5256                 m.return_inspection_requirement,
5257                 nvl(m.effectivity_control, 1),
5258                 null,
5259                 null,
5260                 null,
5261                 sysdate,
5262 		m.comms_nl_trackable_flag,               --  2200256
5263 		nvl(m.default_so_source_type,'INTERNAL'),
5264 		nvl(m.create_supply_flag, 'Y'),
5265 			-- begin bugfix 2336548
5266 		m.lot_status_enabled,
5267 		m.default_lot_status_id,
5268 		m.serial_status_enabled,
5269 		m.default_serial_status_id,
5270 		m.lot_split_enabled,
5271 		m.lot_merge_enabled,
5272 		m.bulk_picked_flag,
5273 			-- end bugfix 2336548
5274 			-- begin bugfix 2400609
5275 		m.FINANCING_ALLOWED_FLAG,
5276  		m.EAM_ITEM_TYPE ,
5277  		m.EAM_ACTIVITY_TYPE_CODE,
5278  		m.EAM_ACTIVITY_CAUSE_CODE,
5279  		m.EAM_ACT_NOTIFICATION_FLAG,
5280  		m.EAM_ACT_SHUTDOWN_STATUS,
5281  		m.SUBSTITUTION_WINDOW_CODE,
5282  		m.SUBSTITUTION_WINDOW_DAYS,
5283  		null, --m.PRODUCT_FAMILY_ITEM_ID, 5385901
5284  		m.CHECK_SHORTAGES_FLAG,
5285  		m.PLANNED_INV_POINT_FLAG,
5286  		m.OVER_SHIPMENT_TOLERANCE,
5287  		m.UNDER_SHIPMENT_TOLERANCE,
5288  		m.OVER_RETURN_TOLERANCE,
5289  		m.UNDER_RETURN_TOLERANCE,
5290  		m.PURCHASING_TAX_CODE,
5291  		m.OVERCOMPLETION_TOLERANCE_TYPE,
5292  		m.OVERCOMPLETION_TOLERANCE_VALUE,
5293  		m.INVENTORY_CARRY_PENALTY,
5294  		m.OPERATION_SLACK_PENALTY,
5295  		m.UNIT_LENGTH,
5296  		m.UNIT_WIDTH,
5297  		m.UNIT_HEIGHT,
5298  		m.LOT_TRANSLATE_ENABLED,
5299  		m.CONTAINER_ITEM_FLAG,
5300  		m.VEHICLE_ITEM_FLAG,
5301  		m.DIMENSION_UOM_CODE,
5302  		m.SECONDARY_UOM_CODE,
5303  		m.MAXIMUM_LOAD_WEIGHT,
5304  		m.MINIMUM_FILL_PERCENT,
5305  		m.CONTAINER_TYPE_CODE,
5306  		m.INTERNAL_VOLUME,
5307  		m.EQUIPMENT_TYPE,
5308  		m.INDIVISIBLE_FLAG,
5309  		m.GLOBAL_ATTRIBUTE_CATEGORY,
5310  		m.GLOBAL_ATTRIBUTE1,
5311  		m.GLOBAL_ATTRIBUTE2,
5312  		m.GLOBAL_ATTRIBUTE3,
5313  		m.GLOBAL_ATTRIBUTE4,
5314  		m.GLOBAL_ATTRIBUTE5,
5315  		m.GLOBAL_ATTRIBUTE6,
5316  		m.GLOBAL_ATTRIBUTE7,
5317  		m.GLOBAL_ATTRIBUTE8,
5318  		m.GLOBAL_ATTRIBUTE9,
5319  		m.GLOBAL_ATTRIBUTE10,
5320      		m.DUAL_UOM_CONTROL,
5321  		m.DUAL_UOM_DEVIATION_HIGH,
5322  		m.DUAL_UOM_DEVIATION_LOW,
5323                 m.CONTRACT_ITEM_TYPE_CODE,
5324  		m.SUBSCRIPTION_DEPEND_FLAG,
5325  		m.SERV_REQ_ENABLED_CODE,
5326  		m.SERV_BILLING_ENABLED_FLAG,
5327  		m.RELEASE_TIME_FENCE_CODE,	  -- 2898851
5328  		m.RELEASE_TIME_FENCE_DAYS,	  -- 2898851
5329  		m.DEFECT_TRACKING_ON_FLAG,        -- 2858080
5330  		m.SERV_IMPORTANCE_LEVEL,
5331 			 -- end bugfix 2400609
5332 	        m.web_status ,                    --   2727983
5333                 nvl( tracking_quantity_ind , 'P' ),
5334                 nvl( m.ont_pricing_qty_source, 'P') ,
5335                 m.approval_status,
5336                 m.vmi_minimum_units,
5337                 m.vmi_minimum_days,
5338                 m.vmi_maximum_units,
5339                 m.vmi_maximum_days,
5340                 m.vmi_fixed_order_quantity,
5341                 m.so_authorization_flag,
5342                 m.consigned_flag,
5343                 m.asn_autoexpire_flag,
5344                 m.vmi_forecast_type,
5345                 m.forecast_horizon,
5346                 m.days_tgt_inv_supply,
5347                 m.days_tgt_inv_window,
5348                 m.days_max_inv_supply,
5349                 m.days_max_inv_window,
5350                 m.critical_component_flag,
5351                 m.drp_planned_flag,
5352                 m.exclude_from_budget_flag,
5353                 m.convergence,
5354                 m.continous_transfer,
5355                 m.divergence,
5356 		   -- r12,4574899
5357 		nvl(m.lot_divisible_flag, 'N'),  --Bugfix 6343429
5358 		'N',
5359 		/* Bugfix 8821149: Will populate these values from model.
5360 		'N',
5361 	        'N',
5362 		'N',
5363 		*/
5364 		m.child_lot_flag,
5365 		m.child_lot_validation_flag,
5366 		m.copy_lot_attribute_flag,
5367 		m.parent_child_generation_flag,
5368 		m.lot_substitution_enabled,
5369 		-- End Bugfix 8821149
5370 		'N',
5371 		'N',
5372 		'N',
5373 		'N',
5374 		'N',
5375 		'N',
5376 		3,
5377 		2
5378 
5379         from
5380                 mtl_system_items_b  m,               -- Model
5381                 bom_cto_src_orgs        bcso,
5382                 bom_cto_order_lines     bcol
5383         where  m.inventory_item_id = pModelId
5384         and bcso.model_item_id = pModelId
5385         and bcso.line_id = pLineId
5386         and bcol.line_id = bcso.line_id
5387 	and m.organization_id = bcso.organization_id
5388         and NOT EXISTS
5389                 (select NULL
5390                 from mtl_system_items_b
5391                 where inventory_item_id = pConfigId
5392                 and organization_id = m.organization_id);
5393 
5394         sqlcnt := sql%rowcount; -- Added for bug 8305535
5395 
5396 	IF PG_DEBUG <> 0 THEN
5397 		oe_debug_pub.add('Create_Item: ' || 'after insert:mtl_system_items_b',2);
5398 
5399         	oe_debug_pub.add('Create_Item: ' || 'Inserted '||sqlcnt||' rows in mtl_system_items_b',2);
5400         END IF;
5401 		-- added as part of bugfix 1811007
5402 
5403 		 --Start Bugfix 8305535
5404  	         if ( sqlcnt > 0) then
5405  	            IF PG_DEBUG <> 0 THEN
5406  	                 oe_debug_pub.add('Create_Item: ' || 'Going to insert in pl/sql table for project Genesis',2);
5407  	            END IF;
5408 
5409  	            l_ind_cnt := CTO_MSUTIL_PUB.cfg_tbl_var.count;
5410  	            CTO_MSUTIL_PUB.cfg_tbl_var(l_ind_cnt + 1) := pConfigId;
5411  	         end if;
5412  	         --End Bugfix 8305535
5413 
5414 
5415 
5416         else
5417 
5418                oe_debug_pub.add('Create_Item: ' || 'going to call create_precon figured item ' || ' line ' || pLineId ||
5419                  ' Model ' || pModelId ||
5420                  ' Config ' || pConfigId ||
5421                  ' item type ' || lItemType  ,2);
5422 
5423 
5424             create_preconfigured_item( pLineId, pModelId, pConfigId , lItemType ) ;
5425 
5426         end if;
5427 
5428 
5429 	-- start bugfix 2157740
5430 
5431 	lStmtNumber := 81;
5432 
5433 	IF PG_DEBUG <> 0 THEN
5434 		oe_debug_pub.add('Create_Item: ' ||  'Opening cursor c_get_org_id.. ' ,3);
5435 	END IF;
5436 
5437 	BEGIN
5438 	FOR v_get_org_id in c_get_org_id
5439 	LOOP
5440         IF PG_DEBUG <> 0 THEN
5441         	oe_debug_pub.add('Create_Item: ' ||  'Fetched Org Id '|| v_get_org_id.src_org_id,3);
5442         END IF;
5443 	fnd_attached_documents2_pkg.copy_attachments (
5444 	 			X_from_entity_name 		=>     'MTL_SYSTEM_ITEMS',
5445                         	X_from_pk1_value 		=>	v_get_org_id.src_org_id,
5446                         	X_from_pk2_value 		=>	pModelId,
5447                         	X_from_pk3_value 		=>	NULL,
5448                         	X_from_pk4_value 		=>	NULL,
5449                         	X_from_pk5_value 		=>	NULL,
5450                         	X_to_entity_name 		=>	'MTL_SYSTEM_ITEMS',
5451                         	X_to_pk1_value 			=>	v_get_org_id.src_org_id,
5452                         	X_to_pk2_value 			=>	pConfigId,
5453                         	X_to_pk3_value 			=>	NULL,
5454                         	X_to_pk4_value 			=>	NULL,
5455                         	X_to_pk5_value 			=>	NULL,
5456                         	X_created_by 			=>	fnd_global.USER_ID,
5457                         	X_last_update_login 		=>	fnd_global.USER_ID,
5458                         	X_program_application_id 	=>	fnd_global.PROG_APPL_ID,
5459                         	X_program_id 			=>	fnd_global.CONC_REQUEST_ID,
5460                         	X_request_id 			=>	fnd_global.USER_ID,
5461                         	X_automatically_added_flag 	=>	NULL
5462                         	);
5463         IF PG_DEBUG <> 0 THEN
5464         	oe_debug_pub.add('Create_Item: ' || 'Done copy_attachment ',3);
5465         END IF;
5466         END LOOP;
5467         END;
5468 	-- End bugfix 2157740
5469 
5470 
5471         --
5472         -- Modified the weight/volume calculation logic for supporting MLMO
5473         --
5474 
5475 	-- bugfix 2301167: Added the global status check (gWtStatus and gVolStatus)
5476 	-- This is done to prevent calculation of upper level config's if lower level
5477 	-- configs have errored out.
5478 
5479 	--Bugfix 9223554: Will not use these global variables.
5480 	/*
5481 	IF PG_DEBUG <> 0 THEN
5482 		oe_debug_pub.add ('Create_Item: ' || 'gWtStatus = '||CTO_CONFIG_ITEM_PK.gWtStatus|| ' and gVolStatus = '||CTO_CONFIG_ITEM_PK.gVolStatus,1);
5483 	END IF;
5484 
5485 	if CTO_CONFIG_ITEM_PK.gWtStatus = 0 or		-- 0 means success
5486 	   CTO_CONFIG_ITEM_PK.gVolStatus = 0
5487 	then
5488 	*/
5489 
5490 	if ( not g_wt_tbl.exists(pLineId)
5491 	     OR
5492 	     not g_vol_tbl.exists(pLineId)
5493 	   ) then
5494 
5495 	-- begin bugfix 1811007
5496 
5497 	DECLARE
5498 
5499         	o_weight_uom	MTL_SYSTEM_ITEMS.weight_uom_code%TYPE;
5500 		o_volume_uom	MTL_SYSTEM_ITEMS.weight_uom_code%TYPE;
5501         	o_weight    	number  := 0;
5502         	o_volume   	number  := 0;
5503         	x_status   	number  := 0;
5504         	lShippingOrg   	number  ;
5505 
5506 	BEGIN
5507 
5508 		lStmtNumber := 90.1;
5509 
5510 		-- Get the weight and volume UOM of the model from the OE:Validation org.
5511 
5512 		select i.weight_uom_code, i.volume_uom_code
5513 		into   o_weight_uom, o_volume_uom
5514 		from   mtl_system_items i,
5515 	               bom_cto_order_lines l
5516 		where  l.line_id = pLineId		-- model line id
5517 		and    l.inventory_item_id = i.inventory_item_id
5518 		and    i.organization_id = lValidationOrg;
5519 
5520 
5521 
5522 		-- If weight UOM is not defined for the base model, then we will get the
5523 		-- base UOM of the uom class of the components.
5524 		-- Assumption is that weight of all the options belong to the same class.
5525 		-- Eg. C_OPTION 7 (kg) and C_OPTION 9 (Lbs).
5526 
5527         	IF o_weight_uom IS NULL THEN
5528 	  	     IF PG_DEBUG <> 0 THEN
5529 	  	     	oe_debug_pub.add('Create_Item: ' || '=>Weight UOM is null for the base model.',2);
5530 
5531 	  	     	oe_debug_pub.add('Create_Item: ' || '=>Trying to get the base UOM ...',2);
5532 	  	     END IF;
5533 
5534 	 	     begin
5535 			select  uom_code
5536 	       		into    o_weight_uom
5537 	       		from    mtl_units_of_measure
5538 	       		where   uom_class = (select uom1.uom_class
5539 			          from   mtl_units_of_measure uom1,
5540 				         mtl_system_items i,
5541 				         bom_cto_order_lines l
5542 			          where  l.parent_ato_line_id = pLineId
5543 						-- mbsk: replaced ato_line_id with parent_ato_line_id
5544         		          and    l.item_type_code not in ('INCLUDED', 'CONFIG')
5545 			          and    l.inventory_item_id = i.inventory_item_id
5546 			          and    i.organization_id = lValidationOrg
5547 			          and    i.weight_uom_code is not null
5548 			          and    i.weight_uom_code = uom1.uom_code
5549 			          and    rownum = 1 )
5550 	       		and     base_uom_flag = 'Y';
5551 	      	     exception
5552  	       		when no_data_found then
5553 		     		null;
5554 	      	     end;
5555 		END IF;
5556 
5557 		-- If volume UOM is not defined for the base model, then we will get the
5558 		-- base UOM for the uom class of the components.
5559 		-- Assumption is that volume of all the options belong to the same class.
5560 		-- Eg. C_OPTION 7 (cubic meter) and C_OPTION 9 (cubic feet).
5561 
5562        		IF o_volume_uom IS NULL THEN
5563 	  	     IF PG_DEBUG <> 0 THEN
5564 	  	     	oe_debug_pub.add('Create_Item: ' || '=>Volume UOM is null for the base model.',2);
5565 
5566 	  	     	oe_debug_pub.add('Create_Item: ' || '=>Trying to get the base UOM ...',2);
5567 	  	     END IF;
5568 
5569 	      	     begin
5570 	       		select  uom_code
5571 	       		into    o_volume_uom
5572 	       		from    mtl_units_of_measure
5573 	       		where   uom_class = (select uom1.uom_class
5574 			          from   mtl_units_of_measure uom1,
5575 				         mtl_system_items i,
5576 				         bom_cto_order_lines l
5577 			          where  l.parent_ato_line_id = pLineId
5578 						-- mbsk: replaced ato_line_id with parent_ato_line_id
5579         		          and    l.item_type_code not in ('INCLUDED', 'CONFIG')
5580 			          and    l.inventory_item_id = i.inventory_item_id
5581 			          and    i.organization_id = lValidationOrg
5582 			          and    i.volume_uom_code is not null
5583 			          and    i.volume_uom_code = uom1.uom_code
5584 			          and    rownum = 1 )
5585 	       		and     base_uom_flag = 'Y';
5586 	      	     exception
5587  	       		when no_data_found then
5588 		     		null;
5589 	      	     end;
5590 		END IF;
5591 
5592 
5593 
5594 		-- We need to call Ato_Weight_Volume API only once because Weight/Vol will be
5595 		-- calculated on the basis of Shipping Orgn. It will be same for all other organizations.
5596 		-- We will update the wt/vol for other orgs after the IF clause.
5597 
5598 		-- If the lower level config's wt or vol was not calculated, then, we should not calculate
5599 		-- the wt or vol for the top level config.
5600 
5601 
5602 		IF PG_DEBUG <> 0 THEN
5603 			oe_debug_pub.add('Create_Item: ' || 'Calling Ato_Weight_Volume API with following parameters..',2);
5604 
5605 			oe_debug_pub.add('Create_Item: ' || '=>Line Id   : '||pLineId,2);
5606 
5607 			oe_debug_pub.add('Create_Item: ' || '=>OE Validn Orgn Id   : '||lValidationOrg,2);
5608 
5609 			oe_debug_pub.add('Create_Item: ' || '=>Weight UOM: '||o_weight_uom,2);
5610 
5611 			oe_debug_pub.add('Create_Item: ' || '=>Volume UOM: '||o_volume_uom,2);
5612 		END IF;
5613 
5614 		Ato_Weight_Volume(
5615                 	p_ato_line_id	=> pLineId,
5616                 	p_orgn_id    	=> lValidationOrg,
5617                 	weight_uom      => o_weight_uom,
5618                 	weight          => o_weight,
5619                 	volume_uom      => o_volume_uom,
5620                 	volume          => o_volume,
5621                 	status          => x_status,
5622 			pConfigId       => pConfigId);--3737772 (FP 3473737)
5623 
5624 		if x_status <> 0 then
5625 	   		raise FND_API.G_EXC_ERROR;
5626 		end if;
5627 
5628 		IF PG_DEBUG <> 0 THEN
5629 			oe_debug_pub.add('Create_Item: ' || 'Successfully executed  Ato_Weight_Volume API with following outputs..',2);
5630 
5631 			oe_debug_pub.add('Create_Item: ' || '=>Weight : '||o_weight||' '||o_weight_uom,2);
5632 
5633 			oe_debug_pub.add('Create_Item: ' || '=>Volume : '||o_volume||' '||o_volume_uom,2);
5634 		END IF;
5635 
5636 
5637 		-- Update the config's weight and volume.
5638 
5639 		--  begin bugfix 2905835: Before updating the weight in other organizations, we will convert
5640 		--  the weight of the config in the model's weight UOM in that organization.
5641 
5642 		lStmtNumber := 95;
5643 
5644 		-- bugfix 4143695: Update MSI only if the weight UOM is not null.
5645 		-- Added IF clause
5646 		-- bugfix 5623437:
5647 		-- Added "and a.unit_weight is null" condition so that weight/vol is updated
5648 		-- only if it is not already calculated.
5649 
5650 		if (o_weight_uom is not null) then
5651 		   update mtl_system_items a
5652 		   set    (unit_weight, weight_uom_code) =
5653 		  	(select CTO_UTILITY_PK.convert_uom(
5654 						o_weight_uom,
5655 						nvl( b.weight_uom_code, o_weight_uom) ,  -- bug# 3358194
5656 						o_weight,
5657 						b.inventory_item_id)
5658 			 	,nvl(b.weight_uom_code, o_weight_uom)  -- bug# 3358194
5659 		   	from   mtl_system_items b
5660 		   	where  b.inventory_item_id = a.base_item_id
5661 		   	and    b.organization_id = a.organization_id)
5662 		   where  a.inventory_item_id = pConfigId
5663 		   and    a.unit_weight is null;   -- bugfix 5623437
5664 		end if;
5665 
5666 		if (o_volume_uom is not null) then 		-- begin bugfix 4143695
5667 		   update mtl_system_items a
5668 		   set    (unit_volume, volume_uom_code) =
5669 		  	(select CTO_UTILITY_PK.convert_uom(
5670 						o_volume_uom,
5671 						nvl(b.volume_uom_code, o_volume_uom),  -- bug# 3358194
5672 						o_volume,
5673 						b.inventory_item_id)
5674 			 	,nvl(b.volume_uom_code, o_volume_uom)  -- bug# 3358194
5675 		   	from   mtl_system_items b
5676 		   	where  b.inventory_item_id = a.base_item_id
5677 		   	and    b.organization_id = a.organization_id)
5678 		   where  a.inventory_item_id = pConfigId
5679     		   and    a.unit_volume is null;			-- bugfix 5623437;
5680 		end if;
5681 
5682 
5683 		--  end bugfix 2905835
5684 
5685         END;
5686 	end if; 	/* end check CTO_CONFIG_ITEM_PK.gWtStatus */
5687 
5688         -- end bugfix 1811007
5689 
5690 
5691        oe_debug_pub.add('Create_Item: ' || 'going to insert:mtl_system_items_tl',2);
5692 
5693         /*----------------------------------------+
5694         R11.5 MLS
5695         +----------------------------------------*/
5696 
5697         lStmtNumber := 90;
5698 
5699         insert into mtl_system_items_tl (
5700                 inventory_item_id,
5701                 organization_id,
5702                 language,
5703                 source_lang,
5704                 description,
5705                 last_update_date,
5706                 last_updated_by,
5707                 creation_date,
5708                 created_by,
5709                 last_update_login
5710                 )
5711         -- bug 12809414. Added hint
5712         select /*+ LEADING(BCSO) */ distinct
5713                 pConfigId,
5714                 m.organization_id,
5715                 l.language_code,
5716                 userenv('LANG'),
5717                 m.description,
5718                 sysdate,
5719                 gUserId,                              --last_updated_by
5720                 sysdate,
5721                 gUserId,                              --created_by
5722                 gLoginId                              --last_update_login
5723 /*
5724 commented for reintroduction of bcso
5725         from
5726                 mtl_system_items_tl m, 				-- 2457514
5727                 fnd_languages  l
5728         where  m.inventory_item_id = pModelId
5729         and  l.installed_flag In ('I', 'B')
5730         and  l.language_code  = m.language			-- 2457514
5731         and  NOT EXISTS
5732                 (select NULL
5733                 from  mtl_system_items_tl  t
5734                 where  t.inventory_item_id = pConfigId
5735                 and  t.organization_id = m.organization_id
5736                 and  t.language = m.language );
5737 */
5738         from
5739                 -- bugfix 2457514 mtl_system_items_b  m,            -- Model
5740                 mtl_system_items_tl m,                          -- 2457514
5741                 bom_cto_src_orgs bcso,
5742                 fnd_languages  l
5743         where
5744 	--m.inventory_item_id = pModelId  -- bugfix 12809414
5745         m.inventory_item_id    = bcso.model_item_id
5746         and bcso.model_item_id = pModelId
5747         and bcso.line_id = pLineId
5748 	and m.organization_id = bcso.organization_id
5749         and  l.installed_flag In ('I', 'B')
5750         and  l.language_code  = m.language                      -- 2457514
5751         and  NOT EXISTS
5752                 (select NULL
5753                 from  mtl_system_items_tl  t
5754                 where  t.inventory_item_id = pConfigId
5755                 and  t.organization_id = m.organization_id
5756                 and  t.language = l.language_code );
5757 
5758 
5759 	IF PG_DEBUG <> 0 THEN
5760 		oe_debug_pub.add('Create_Item: ' || 'after insert:mtl_system_items_tl',2);
5761 	END IF;
5762 
5763 
5764 
5765 
5766 
5767       /*
5768       **
5769       ** This section has been moved to CTO_ITEM_PK
5770       **
5771 
5772 
5773 
5774         --
5775         -- create sourcing rules if necessary
5776         --
5777 
5778 	lStmtNumber := 100;
5779 
5780 	FOR v_src_rule IN c_copy_src_rules LOOP
5781                 --
5782                 -- call API to copy sourcing rules from model item
5783                 -- to config item
5784                 --
5785 		IF PG_DEBUG <> 0 THEN
5786 			oe_debug_pub.add ('Create_Item: ' || 'Copying src rule for cfg item '||to_char(pConfigId)||' in org '||to_char(v_src_rule.rcv_org_id), 2);
5787 		END IF;
5788 		CTO_UTILITY_PK.Create_Sourcing_Rules(
5789 				pModelItemId	=> pModelId,
5790 				pConfigId	=> pConfigId,
5791 				pRcvOrgId	=> v_src_rule.rcv_org_id,
5792 				x_return_status	=> lReturnStatus,
5793 				x_msg_count	=> xMsgCount,
5794 				x_msg_data	=> xMsgData);
5795 
5796 		IF (lReturnStatus = fnd_api.G_RET_STS_ERROR) THEN
5797 			IF PG_DEBUG <> 0 THEN
5798 				oe_debug_pub.add ('Create_Item: ' || 'Create_Sourcing_Rules returned with expected error.');
5799 			END IF;
5800 	   		raise FND_API.G_EXC_ERROR;
5801 
5802 		ELSIF (lReturnStatus = fnd_api.G_RET_STS_UNEXP_ERROR) THEN
5803 			IF PG_DEBUG <> 0 THEN
5804 				oe_debug_pub.add ('Create_Item: ' || 'Create_Sourcing_Rules returned with unexp error.');
5805 			END IF;
5806 	   		raise FND_API.G_EXC_UNEXPECTED_ERROR;
5807 
5808 		END IF;
5809 
5810         END LOOP;
5811 
5812 
5813         --
5814         -- update bom_cto_order_lines with new config item
5815         --
5816 
5817 	lStmtNumber := 120;
5818         lStatus := CTO_UTILITY_PK.Update_Order_Lines(
5819                         pLineId		=> pLineId,
5820                         pModelId	=> pModelId,
5821                         pConfigId	=> pConfigId);
5822 
5823         IF lStatus <> 1 THEN
5824                 IF PG_DEBUG <> 0 THEN
5825                 	oe_debug_pub.add ('Create_Item: ' || 'Failed in  Update_Order_Lines function',1);
5826                 END IF;
5827 	    	raise FND_API.G_EXC_ERROR;
5828         END IF;
5829 
5830 
5831 
5832         --
5833         -- update bom_cto_src_orgs with new config item
5834         --
5835 
5836 	lStmtNumber := 130;
5837         lStatus := CTO_UTILITY_PK.Update_Src_Orgs(
5838                         pLineId		=> pLineId,
5839                         pModelId	=> pModelId,
5840                         pConfigId	=> pConfigId);
5841 
5842         IF lStatus <> 1 THEN
5843                 IF PG_DEBUG <> 0 THEN
5844                 	oe_debug_pub.add ('Create_Item: ' || 'Failed in  Update_Src_Orgs function',1);
5845                 END IF;
5846 	    	raise FND_API.G_EXC_ERROR;
5847         END IF;
5848 
5849 
5850 
5851 
5852 
5853        */
5854 
5855 
5856 
5857 
5858 
5859 
5860 
5861 
5862 
5863         --
5864         -- create item data in related tables
5865         --
5866 
5867 	lStmtNumber := 140;
5868         lStatus := create_item_data(
5869                         pModelId,
5870                         pConfigId,
5871                         pLineId,
5872                         p_mode );
5873 
5874         if lStatus <> 1 then
5875                 IF PG_DEBUG <> 0 THEN
5876                 	oe_debug_pub.add ('Create_Item: ' || 'Failed in create_data function',1);
5877                 END IF;
5878 		raise FND_API.G_EXC_ERROR;
5879         end if;
5880 
5881         IF PG_DEBUG <> 0 THEN
5882         	oe_debug_pub.add ('Create_Item: ' || 'Success in create_data function',1);
5883         END IF;
5884 
5885         return(1);
5886 
5887 EXCEPTION
5888 
5889         WHEN NO_DATA_FOUND THEN
5890 		IF PG_DEBUG <> 0 THEN
5891 			oe_debug_pub.add ('Create_Item: ' || 'ERROR: create_item::ndf::'||to_char(lStmtNumber)||sqlerrm,1);
5892 		END IF;
5893 		CTO_MSG_PUB.Count_And_Get(
5894                   p_msg_count => xMsgCount,
5895                   p_msg_data  => xMsgData
5896                 );
5897                 return(0);
5898 
5899 	WHEN FND_API.G_EXC_ERROR THEN
5900 		IF PG_DEBUG <> 0 THEN
5901 			oe_debug_pub.add ('Create_Item: ' || 'ERROR: create_item::exp error::'||to_char(lStmtNumber)||sqlerrm,1);
5902 		END IF;
5903 		CTO_MSG_PUB.Count_And_Get(
5904                   p_msg_count => xMsgCount,
5905                   p_msg_data  => xMsgData
5906                 );
5907                 return(0);
5908 
5909 	WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5910 		IF PG_DEBUG <> 0 THEN
5911 			oe_debug_pub.add ('Create_Item: ' || 'ERROR: create_item::unexp error::'||to_char(lStmtNumber)||sqlerrm,1);
5912 		END IF;
5913 		CTO_MSG_PUB.Count_And_Get(
5914                   p_msg_count => xMsgCount,
5915                   p_msg_data  => xMsgData
5916                 );
5917                 return(0);
5918 
5919         WHEN OTHERS THEN
5920 		IF PG_DEBUG <> 0 THEN
5921 			oe_debug_pub.add ('Create_Item: ' || 'ERROR: create_item::others::'||to_char(lStmtNumber)||sqlerrm,1);
5922 		END IF;
5923                 CTO_MSG_PUB.Count_And_Get(
5924                   p_msg_count => xMsgCount,
5925                   p_msg_data  => xMsgData
5926                 );
5927                 return(0);
5928 
5929 END Create_Item;
5930 
5931 --begin bugfix 1811007
5932 
5933 PROCEDURE Ato_Weight_Volume(
5934                 p_ato_line_id   IN      NUMBER,
5935                 p_orgn_id       IN      NUMBER,
5936                 weight_uom      IN OUT NOCOPY  VARCHAR2, /* NOCOPY Project */
5937                 weight          OUT NOCOPY    NUMBER,
5938                 volume_uom      IN OUT NOCOPY  VARCHAR2, /* NOCOPY Project */
5939                 volume          OUT NOCOPY    NUMBER,
5940                 status          IN OUT  NOCOPY  NUMBER,  /* NOCOPY Project */
5941 		pConfigId       IN      NUMBER)--3737772 (FP 3473737)
5942 is
5943 
5944   -- The two cursors ato_weight and ato_volume have the
5945   -- same WHERE clause. The only difference is weight vs. volume.
5946   -- CTO_UTILITY_PK.convert_uom is a wrapper for INV_CONVERT.inv_um_convert to translate
5947   -- -9999 (error value) into 0 (no weight/volume).
5948 
5949 
5950   -- We need to figure out qty-per and then weight of each component in the shipping org,
5951   -- and that's why l.ordered_quantity/l_model.ordered_quantity which gives us qty-per.
5952   -- Weight and Volume calculation is calculated on the basis of Shipping Org.
5953 
5954   -- This API is called for each ATO model in a multi-level ATO configuration starting from the bottom most.
5955   -- The line-Id passed is the line id of each ATO model.
5956   -- The weight of the each independent ATO configuration is calculated starting from bottom to determine the
5957   -- weight of that configuration. This then is added to the next higher level ATO to calculate the weight
5958   -- of the next higher configuration and so on until the weight of the top model ATO is calculated.
5959 
5960   -- bugfix 2301167: added inventory_item_id in the select list as well as in group by clause.
5961   -- This will make sure that item specific conversions take precedence over standard conversions.
5962   -- Also, for inter-class conversions, item-id is required.
5963   -- Also added "weight_uom_code is not null" and "unit_weight is not null" condition to filter out
5964   -- items for which these have not been defined.
5965 
5966 
5967   CURSOR ato_weight(x_a_line_id NUMBER) IS
5968      SELECT SUM( NVL(msi.unit_weight, 0) *
5969                  CTO_UTILITY_PK.convert_uom(l.order_quantity_uom,
5970                                             msi.primary_uom_code,
5971                                             Round( ( l.ordered_quantity / l_model.ordered_quantity), 7) ,  /* Support Decimal-Qty for Option Items */
5972                                             l.inventory_item_id) ) weight,
5973             msi.weight_uom_code  uom,
5974 	    msi.inventory_item_id
5975        FROM bom_cto_order_lines  l,
5976             bom_cto_order_lines  l_model,
5977             mtl_system_items    msi
5978       WHERE (l.parent_ato_line_id = x_a_line_id
5979 			-- MLMO:replaced ato_line_id with parent_ato_line_id  and added OR condn
5980 		or
5981             l.line_id = x_a_line_id)
5982         --  Bugfix 2576422
5983 	--  joining l_model.line_id to l.parent_ato_line_id so that
5984 	--  qty per will be calculated correctly for multi-level configuration
5985 	--  AND l_model.line_id = l.top_model_line_id
5986 	--  MLMO:replaced x_a_line_id with l.top_model_line_id
5987 	AND l_model.line_id = l.parent_ato_line_id
5988 	-- End bugfix 2576422
5989 	AND l.item_type_code not in ('INCLUDED', 'CONFIG')
5990         AND msi.inventory_item_id = decode(l.config_item_id, null,l.inventory_item_id,
5991 	                                   --3737772 (FP 3473737)
5992 	                                   pConfigId,l.inventory_item_id, l.config_item_id )
5993 							-- MLMO: added decode/config_item_id condn
5994         AND msi.organization_id = p_orgn_id
5995 	AND msi.weight_uom_code is not null
5996 	AND nvl(msi.unit_weight, 0) <> 0 	-- bugfix 2905835: changed "is not null" to <> 0
5997       GROUP BY msi.inventory_item_id,
5998 	       msi.weight_uom_code;
5999 
6000 
6001 
6002   CURSOR ato_volume(x_a_line_id NUMBER) IS
6003      SELECT SUM( NVL(msi.unit_volume, 0) *
6004                  CTO_UTILITY_PK.convert_uom(l.order_quantity_uom,
6005                                             msi.primary_uom_code,
6006                                             Round( ( l.ordered_quantity / l_model.ordered_quantity) , 7) , /* Support Decimal-Qty for Option Items */
6007                                             l.inventory_item_id) ) volume,
6008             msi.volume_uom_code  uom,
6009 	    msi.inventory_item_id
6010        FROM bom_cto_order_lines  l,
6011             bom_cto_order_lines  l_model,
6012             mtl_system_items    msi
6013       WHERE (l.parent_ato_line_id = x_a_line_id		-- MLMO: replaced ato_line_id with parent_ato_line_id
6014 		or
6015             l.line_id = x_a_line_id)
6016         --  Bugfix 2576422
6017 	--  joining l_model.line_id to l.parent_ato_line_id so that
6018 	--  qty per will be calculated correctly for multi-level configuration
6019 	--  AND l_model.line_id = l.top_model_line_id
6020 	-- MLMO:replaced x_a_line_id with l.top_model_line_id
6021 	AND l_model.line_id = l.parent_ato_line_id
6022 	-- End bugfix 2576422
6023 	AND l.item_type_code not in ('INCLUDED', 'CONFIG')
6024         AND msi.inventory_item_id = decode(l.config_item_id, null, l.inventory_item_id,
6025 	                                    --3737772 (FP 3473737)
6026 	                                    pConfigId,l.inventory_item_id,l.config_item_id )
6027 							-- MLMO: added decode/config_item_id condn
6028         AND msi.organization_id = p_orgn_id
6029 	AND msi.volume_uom_code is not null
6030 	AND nvl(msi.unit_volume,0) <> 0		-- bugfix 2905835 : changed "is not null" to <> 0
6031       GROUP BY msi.inventory_item_id,
6032 	       msi.volume_uom_code;
6033 
6034   lStmtNumber    number := 0;
6035   lMsgCount	 number;
6036   lMsgData	 varchar2(2000);
6037   lweight 	 number := 0;
6038   lvolume 	 number := 0;
6039 
6040 
6041 BEGIN
6042 
6043         weight := 0;
6044         volume := 0;
6045         status := 0;
6046 
6047 
6048     	IF PG_DEBUG <> 0 THEN
6049     		oe_debug_pub.add ('Create_Item: ' || 'Inside Ato_Weight_Volume API with p_ato_line_id = '||p_ato_line_id,1);
6050 		oe_debug_pub.add ('Create_Item: ' || 'Inside Ato_Weight_Volume API with pConfigId = '||pConfigId,1);
6051 
6052     	END IF;
6053 
6054 	lStmtNumber := 1205;
6055 
6056         -- bugfix 2301167: Calculate the config's wt if the previous calculation of lower level config was
6057 	-- successful. gWtStatus would be set to -1 if calculation was not successful.
6058 
6059 	--Bugfix 9223554: Changed the logic.
6060 	--if CTO_CONFIG_ITEM_PK.gWtStatus = 0 then
6061 	if not g_wt_tbl.exists(p_ato_line_id) then
6062 
6063     	   IF PG_DEBUG <> 0 THEN
6064     	   	oe_debug_pub.add ('Create_Item: ' || '===>weight_uom = '||weight_uom,1);
6065     	   END IF;
6066 
6067            FOR aw IN ato_weight(p_ato_line_id)
6068 	   loop
6069 
6070     		IF PG_DEBUG <> 0 THEN
6071     			oe_debug_pub.add ('Create_Item: ' || '===>aw.weight = '||aw.weight||' uom = '||aw.uom||' for item_id '||aw.inventory_item_id,1);
6072     		END IF;
6073 		-- Bugfix 2301167: Added item_id as a parameter so that item specific conversions take precedence if defined.
6074 		lweight := CTO_UTILITY_PK.convert_uom(
6075 						from_uom	=> aw.uom,
6076 						to_uom		=> weight_uom,
6077 						quantity	=> aw.weight,
6078 						item_id		=> aw.inventory_item_id);
6079 		--Bugfix 9214765: Changing the if condition
6080 		--if lweight = 0 then
6081 		IF PG_DEBUG <> 0 THEN
6082     		  oe_debug_pub.add ('Create_Item: lweight:' || lweight, 1);
6083     		END IF;
6084 
6085 		if lweight = -99999 then
6086     			IF PG_DEBUG <> 0 THEN
6087     				oe_debug_pub.add ('Create_Item: ' || '===>convert_uom returned -99999(error). Not calculating weight. Check if all conversions have been defined.', 1);
6088     			END IF;
6089 			-- we don't want to update the weight of the config incorrectly, so set it to 0.
6090 			weight := 0;
6091 
6092 			-- Also, we shouldn't calculate the wt for the top config since the lower level's config
6093 			-- weight has been updated to 0
6094 
6095 			--Bugfix 9223554
6096 		        --CTO_CONFIG_ITEM_PK.gWtStatus := -1;	 -- -1 means error
6097 			update_wt_vol_tbl(p_tbl_type => 1,
6098 			                  p_line_id  => p_ato_line_id);
6099 
6100 			exit;
6101 		end if;
6102                 weight := weight + lweight;
6103 
6104           end loop;
6105         end if;
6106 
6107 
6108     	IF PG_DEBUG <> 0 THEN
6109     		oe_debug_pub.add ('Create_Item: ' || '---------------------',1);
6110     	END IF;
6111 
6112 	lStmtNumber := 1210;
6113 
6114         -- bugfix 2301167: Calculate the config's wt if the previous calculation of lower level config was
6115 	-- successful. gWtStatus would be set to -1 if calculation was not successful.
6116 
6117 	--Bugfix 9223554: Changed the logic.
6118 	--if CTO_CONFIG_ITEM_PK.gVolStatus = 0 then
6119 	if not g_vol_tbl.exists(p_ato_line_id) then
6120 
6121     	   IF PG_DEBUG <> 0 THEN
6122     	   	oe_debug_pub.add ('Create_Item: ' || '===>volume_uom = '||volume_uom,1);
6123     	   END IF;
6124 
6125            FOR av IN ato_volume(p_ato_line_id)
6126 	   loop
6127     		IF PG_DEBUG <> 0 THEN
6128     			oe_debug_pub.add ('Create_Item: ' || '===>av.volume = '||av.volume||' uom = '||av.uom||' for item_id '||av.inventory_item_id,1);
6129     		END IF;
6130 		-- Bugfix 2301167: Added item_id as a parameter so that item specific conversions take precedence if defined.
6131 		lvolume := CTO_UTILITY_PK.convert_uom(
6132 						from_uom	=> av.uom,
6133 						to_uom		=> volume_uom,
6134 						quantity	=> av.volume,
6135 						item_id		=> av.inventory_item_id);
6136 		--Bugfix 9214765: Changing the if condition
6137 		--if lvolume = 0 then
6138 		IF PG_DEBUG <> 0 THEN
6139     		  oe_debug_pub.add ('Create_Item: lvolume:' || lvolume, 1);
6140     		END IF;
6141 
6142 		if lvolume = -99999 then
6143     			IF PG_DEBUG <> 0 THEN
6144     				oe_debug_pub.add ('Create_Item: ' || '===>convert_uom returned -99999(error). Not calculating volume. Check if all conversions have been defined.', 1);
6145     			END IF;
6146 			-- we don't want to update the volume of the config incorrectly, so set it to 0.
6147 			volume := 0;
6148 
6149 			-- Also, we shouldn't calculate the vol for the top config since the lower level's config
6150 			-- volume has been updated to 0
6151 
6152 			--Bugfix 9223554
6153 		        --CTO_CONFIG_ITEM_PK.gVolStatus := -1;	 -- -1 means error
6154 			update_wt_vol_tbl(p_tbl_type => 2,
6155 			                  p_line_id  => p_ato_line_id);
6156 
6157 			exit;
6158 		end if;
6159                 volume := volume + lvolume;
6160 
6161            end loop;
6162 	end if;
6163 
6164 EXCEPTION
6165 WHEN OTHERS THEN
6166     IF PG_DEBUG <> 0 THEN
6167     	oe_debug_pub.add ('Create_Item: ' || 'ERROR: Ato_Weight_Volume::others::'||to_char(lStmtNumber)||':-'||sqlerrm,1);
6168     END IF;
6169     CTO_MSG_PUB.Count_And_Get(
6170                   p_msg_count => lMsgCount,
6171                   p_msg_data  => lMsgData
6172                 );
6173     status := -1;
6174 
6175 End Ato_Weight_Volume;
6176 
6177 --end bugfix 1811007
6178 
6179 
6180 
6181 FUNCTION Create_Item_Data(
6182         pModelId         in     number,
6183         pConfigId        in     number,
6184         pLineId          in     number,
6185         p_mode          in     varchar2 default 'AUTOCONFIG' )
6186 return integer
6187 is
6188 
6189    lValidationOrg    number;
6190    lShipFromOrg	     number;
6191    lStmtNumber       number;
6192    l_cost_group_id   number;
6193    lProfileVal       number;
6194    lOpUnit           number;
6195    l_layer_id	     number;
6196    lMsgCount	     number;
6197    lMsgData	     varchar2(2000);
6198    x_err_num	     number;
6199    x_msg_name	     varchar2(240);
6200    xReturnStatus     VARCHAR2(1);  --Bugfix 6063990
6201 
6202    -- x_item_rev_seq    number; bugfix 3026929
6203 
6204    multiorg_error    exception;
6205    v_program_id      bom_cto_order_lines.program_id%type ;
6206 
6207    l_cto_cost_type_id	number;
6208 
6209    lCnt              number;  --Bugfix 6717614
6210 
6211    CURSOR c_layer IS
6212    select distinct
6213       MP1.organization_id org_id,
6214       DECODE(MP1.ORGANIZATION_ID, lShipFromOrg ,l_cost_group_id,1) cost_group_id
6215 /*
6216 commented due to reintroduction of bcso
6217    from mtl_parameters mp1,
6218         cst_item_costs c,
6219         mtl_system_items msi
6220    where c.organization_id       = mp1.organization_id
6221    and c.inventory_item_id     = pModelId
6222    and C.COST_TYPE_ID          =  2     -- Average Costing
6223    and msi.organization_id = mp1.organization_id
6224    and MP1.Primary_cost_method = 2     -- Create only in Avg costing org
6225    and NOT EXISTS
6226    	(select NULL
6227         from cst_quantity_layers
6228         where inventory_item_id = pConfigId
6229         and organization_id = mp1.organization_id);
6230 */
6231    from mtl_parameters mp1,
6232         mtl_parameters mp2,
6233         cst_item_costs c,
6234         bom_cto_src_orgs        bcso
6235    where c.organization_id       = mp1.organization_id
6236    and c.inventory_item_id     = pModelId
6237    and C.COST_TYPE_ID          =  2     -- Average Costing
6238    and bcso.model_item_id = pModelId
6239    and bcso.line_id = pLineId
6240    and MP2.organization_id     = bcso.organization_id
6241    and ((mp1.organization_id = bcso.organization_id) OR
6242         (mp1.organization_id = mp2.master_organization_id))
6243    and MP1.Primary_cost_method = 2     -- Create only in Avg costing org
6244    and NOT EXISTS
6245         (select NULL
6246         from cst_quantity_layers
6247         where inventory_item_id = pConfigId
6248         and organization_id = mp1.organization_id);
6249 
6250 
6251 v_material_cost             cst_item_costs.material_cost%type := 0 ;
6252 v_material_overhead_cost    cst_item_costs.material_overhead_cost%type := 0 ;
6253 v_resource_cost             cst_item_costs.resource_cost%type := 0 ;
6254 v_outside_processing_cost   cst_item_costs.outside_processing_cost%type := 0 ;
6255 v_overhead_cost             cst_item_costs.overhead_cost%type := 0 ;
6256 
6257 
6258 v_item_cost             cst_item_costs.item_cost%type := 0 ;
6259 v_cto_cost_type_name    cst_cost_types.cost_type%type;
6260 v_item_cost_frozen      cst_item_costs.item_cost%type;  --Bugfix 6363308
6261 l_cost_update           number;                         --Bugfix 6363308
6262 
6263 --Bugfix 6363308
6264 CURSOR get_orgs_with_frozen_cost ( p_config_item_id NUMBER)
6265 IS
6266   SELECT  organization_id
6267         , item_cost
6268     FROM  cst_item_costs
6269     WHERE inventory_item_id = p_config_item_id
6270     AND   cost_type_id = 1;
6271 
6272 v_organization_id NUMBER;
6273 --Bugfix 6363308
6274 
6275 
6276  Type number_tbl_type IS TABLE OF number INDEX BY BINARY_INTEGER;
6277 
6278 
6279  TYPE cicd_summary_rec_tab is record (
6280                                cost_organization_id     number_tbl_type,
6281                                cost_type_id             number_tbl_type,
6282                                material_cost            number_tbl_type,
6283                                material_overhead_cost   number_tbl_type,
6284                                resource_cost            number_tbl_type,
6285                                outside_processing_cost  number_tbl_type,
6286                                overhead_cost            number_tbl_type,
6287                                item_cost                number_tbl_type ) ;
6288 
6289 
6290 
6291 
6292  l_rt_cicd_summary cicd_summary_rec_tab ;
6293 
6294  --kkonada R12
6295  --for mtl_cross_references_b
6296  --bug# 4539578
6297 
6298   TYPE org_id			IS TABLE OF mtl_cross_references_b.organization_id%type;
6299   TYPE cross_reference_type     IS TABLE OF mtl_cross_references_b.cross_reference_type%type;
6300   TYPE cross_reference          IS TABLE OF mtl_cross_references_b.cross_reference%type;
6301   TYPE org_independent_flag     IS TABLE OF mtl_cross_references_b.org_independent_flag%type;
6302 
6303 
6304   t_organization_id		org_id;
6305   t_cross_ref_type		cross_reference_type;
6306   t_cross_ref			cross_reference;
6307   t_org_independent_flag	org_independent_flag;
6308 
6309 
6310 BEGIN
6311 
6312 
6313         /* added by Sushant to check preconfigure bom populated records */
6314         select nvl( program_id , 0 ) into v_program_id
6315           from bom_cto_order_lines
6316           where line_id = pLineId ;
6317 
6318 
6319     	lStmtNumber := 200;
6320 
6321         if ( v_program_id = CTO_UTILITY_PK.PC_BOM_PROGRAM_ID ) then
6322 
6323 		lValidationOrg := CTO_UTILITY_PK.PC_BOM_VALIDATION_ORG ;
6324 
6325         else
6326 
6327                /*
6328                 BUG:3484511
6329                 -------------
6330 
6331 		select nvl(master_organization_id, -99)		--bugfix 2646849: added nvl.
6332 		into   lValidationOrg
6333 		from   oe_order_lines_all oel,
6334 		       oe_system_parameters_all ospa
6335        		where  oel.line_id = pLineid
6336        		and    nvl(oel.org_id, -1) = nvl(ospa.org_id, -1) --bug 1531691
6337        		and    oel.inventory_item_id = pModelId;
6338                 */
6339 
6340 
6341                IF PG_DEBUG <> 0 THEN
6342                 oe_debug_pub.add('Create_Item: ' ||  'Going to fetch Validation Org ' ,2);
6343                END IF;
6344 
6345 
6346               select nvl( oe_sys_parameters.value( 'MASTER_ORGANIZATION_ID' , oel.org_id) , -99)
6347                 into lValidationOrg from oe_order_lines_all oel
6348                where oel.line_id = pLineId ;
6349 
6350 
6351         end if ;
6352 
6353 
6354     	IF PG_DEBUG <> 0 THEN
6355     		oe_debug_pub.add('Create_Item: ' ||  'After getting validation org',2);
6356     	END IF;
6357 
6358     	if lVAlidationOrg = -99 then				-- bugfix 2646849
6359       		cto_msg_pub.cto_message('BOM','CTO_VALIDATION_ORG_NOT_SET');
6360 		RAISE FND_API.G_EXC_ERROR;
6361     	end if;
6362 
6363 
6364 	/*------------------------------------------+
6365            Create rows for config items
6366            in the MTL_PENDING_ITEM_STATUS
6367         +------------------------------------------*/
6368 
6369         lStmtNumber := 205;
6370 
6371         IF PG_DEBUG <> 0 THEN
6372         	oe_debug_pub.add ('Create_Item: ' ||  'Validation Org is :' || lValidationOrg ,2);
6373         END IF;
6374 
6375         insert into MTL_PENDING_ITEM_STATUS (
6376                 inventory_item_id,
6377                 organization_id,
6378                 status_code,
6379                 effective_date,
6380                 pending_flag,
6381                 last_update_date,
6382                 last_updated_by,
6383                 creation_date,
6384                 created_by,
6385                 last_update_login,
6386                 program_application_id,
6387                 program_id,
6388                 program_update_date,
6389                 request_id)
6390         select distinct
6391                 pConfigId,
6392                 m.organization_id,
6393                 m.inventory_item_status_code,
6394                 sysdate,
6395                 'N',
6396                 sysdate,
6397                 gUserId,
6398                 sysdate,
6399                 gUserId,
6400                 gLoginId,
6401                 null,
6402                 null,
6403                 sysdate,
6404                 null                    --  req_id
6405         from   mtl_system_items m,
6406                bom_cto_src_orgs        bcso
6407         where  m.inventory_item_id = pModelId
6408         and bcso.model_item_id = pModelId
6409         and bcso.line_id = pLineId
6410         and m.organization_id = bcso.organization_id
6411         and NOT EXISTS
6412                 (select NULL
6413                 from MTL_PENDING_ITEM_STATUS
6414                 where inventory_item_id = pConfigId
6415                 and organization_id = m.organization_id);
6416 
6417 
6418 
6419 	IF PG_DEBUG <> 0 THEN
6420 		oe_debug_pub.add('Create_Item: ' || 'after insert:MTL_PENDING_ITEM_STATUS',2);
6421 	END IF;
6422 
6423         /*-------------------------------------------+
6424           Insert Item revision information
6425         +-------------------------------------------*/
6426 
6427         lStmtNumber := 210;
6428 
6429 	/* Begin Bugfix 6063990: Commenting out this part as this will no longer be used.
6430            We will call populate_item_revision procedure directly
6431 
6432 	--
6433 	-- bugfix 3026929 : To avoid dependencies with INV/BOM, we made this code change.
6434 	-- We will first try to insert into MIR. If this fails, we will insert into MIR_b and _tl using dynamic stmt.
6435 	-- We need to use dynamic stmt to avoid compilation errors in pre-I instances.
6436 	-- We did not use ALL_OBJECTS since this *may* not accessible from APPS schema.
6437 	-- This change supersedes bugfix 2730055
6438 	--
6439 
6440 	DECLARE
6441 		non_key_preserved_error		EXCEPTION;
6442 		PRAGMA exception_init (non_key_preserved_error, -1779);
6443 		xReturnStatus			VARCHAR2(1);
6444 	BEGIN
6445 
6446         insert into mtl_item_revisions
6447               (inventory_item_id,
6448                organization_id,
6449                revision,
6450                last_update_date,
6451                last_updated_by,
6452                creation_date,
6453                created_by,
6454                last_update_login,
6455                implementation_date,
6456                effectivity_date
6457               )
6458         select distinct
6459                pConfigId,
6460                m.organization_id,
6461                mp1.starting_revision,
6462                sysdate,
6463                gUserId,                     -- last_updated_by
6464                sysdate,
6465                gUserId,                     -- created_by
6466                gLoginId,                    -- last_update_login
6467                sysdate,
6468                sysdate
6469         from
6470                 mtl_system_items m,
6471                 bom_cto_src_orgs        bcso,
6472 		mtl_parameters mp1
6473         where  m.inventory_item_id = pModelId
6474         and bcso.model_item_id = pModelId
6475         and bcso.line_id = pLineId
6476         and m.organization_id = bcso.organization_id
6477 	and mp1.organization_id = bcso.organization_id
6478         and NOT EXISTS
6479                 (select NULL
6480                  from MTL_ITEM_REVISIONS
6481                 where inventory_item_id = pConfigId
6482                 and organization_id = m.organization_id);
6483 
6484 
6485 
6486         IF PG_DEBUG <> 0 THEN
6487 		oe_debug_pub.add('Create_Item_Data: ' || 'after insert:MTL_ITEM_REVISIONS',2);
6488 	END IF;
6489 
6490 	EXCEPTION
6491 		when NON_KEY_PRESERVED_ERROR then
6492 
6493         		IF PG_DEBUG <> 0 THEN
6494 			   oe_debug_pub.add('Create_item_data: NON_KEY_PRESERVED_ERROR exception. Hence, calling populate_item_revision.. ',2);
6495 			   oe_debug_pub.add('Create_item_data: NON_KEY_PRESERVED_ERROR exception. model Model ' || pModelId ,2);
6496 			   oe_debug_pub.add('Create_item_data: NON_KEY_PRESERVED_ERROR exception. model Line ' || pLineId ,2);
6497 			   oe_debug_pub.add('Create_item_data: NON_KEY_PRESERVED_ERROR exception. model Config ' || pConfigId ,2);
6498 			END IF;
6499 
6500 			populate_item_revision (pConfigId, pModelId, pLineId, xReturnStatus);
6501 
6502 			IF (xReturnStatus <> fnd_api.G_RET_STS_SUCCESS) THEN
6503 			   oe_debug_pub.add('Create_item_data : failed in populate_item_revision : '||substrb(sqlerrm,1,60),2);
6504                            raise FND_API.G_EXC_UNEXPECTED_ERROR;
6505 
6506 			ELSE
6507         		   IF PG_DEBUG <> 0 THEN
6508 				oe_debug_pub.add('Create_item_data: done populate_item_revision successfully.',2);
6509 			   END IF;
6510 			END IF;
6511 
6512 		when OTHERS then
6513 			oe_debug_pub.add('Create_item_data: Failed while inserting into MTL_ITEM_REVISIONS: '||substrb(sqlerrm,1,60),2);
6514 			raise FND_API.G_EXC_UNEXPECTED_ERROR;
6515 	END;*/
6516 
6517 	 populate_item_revision (pConfigId, pModelId, pLineId, xReturnStatus);  --Bugfix 6063990
6518 
6519         IF (xReturnStatus <> fnd_api.G_RET_STS_SUCCESS) THEN
6520 	  oe_debug_pub.add('Create_item_data : failed in populate_item_revision : '||substrb(sqlerrm,1,60),2);
6521           raise FND_API.G_EXC_UNEXPECTED_ERROR;
6522 
6523 	ELSE
6524     	  IF PG_DEBUG <> 0 THEN
6525 	    oe_debug_pub.add('Create_item_data: done populate_item_revision successfully.',2);
6526 	  END IF;
6527 	END IF;
6528         --End Bugfix 6063990.
6529 
6530       	/*----------------------------------------------------------+
6531          Insert cost records for config items
6532          The cost organization id is either the organization id
6533          or the master organization id
6534       	+----------------------------------------------------------*/
6535 
6536 
6537         lStmtNumber := 213;
6538 
6539         /* FIX to avoid rolled up cost of model from being included during cost rollup */
6540         /* Fix for bug 4172300. cost in validation org is not copied properly from model to config item */
6541 
6542 	--apps perf bug#	4905845, sql id 16104498
6543         select C.organization_id,
6544                C.cost_type_id,
6545                nvl(sum(decode( cicd.cost_element_id, 1 , nvl(cicd.item_cost, 0 ) )) , 0 ),
6546                nvl( sum(decode( cicd.cost_element_id,2 , nvl( cicd.item_cost, 0 ) )) , 0 ),
6547                nvl( sum(decode( cicd.cost_element_id,3 , nvl( cicd.item_cost, 0 ) )) , 0 ),
6548                nvl( sum(decode( cicd.cost_element_id,4 , nvl( cicd.item_cost, 0 ) )) , 0 ),
6549                nvl( sum(decode( cicd.cost_element_id,5 , nvl( cicd.item_cost, 0 ) ))  , 0 )
6550 BULK COLLECT INTO
6551                l_rt_cicd_summary.cost_organization_id,
6552                l_rt_cicd_summary.cost_type_id,
6553                l_rt_cicd_summary.material_cost,
6554                l_rt_cicd_summary.material_overhead_cost,
6555                l_rt_cicd_summary.resource_cost,
6556                l_rt_cicd_summary.outside_processing_cost,
6557                l_rt_cicd_summary.overhead_cost
6558         from
6559                 mtl_parameters        MP1,
6560                 cst_item_costs        C,
6561                 cst_item_cost_details CICD,
6562                 mtl_system_items      S --  4172300
6563         where   S.organization_id   = C.organization_id
6564         and     S.inventory_item_id = C.inventory_item_id
6565         and     C.organization_id   = MP1.organization_id
6566         and     C.inventory_item_id = pModelId
6567         and     C.inventory_item_id = S.inventory_item_id
6568         and     C.COST_TYPE_ID  IN ( MP1.primary_cost_method, MP1.avg_rates_cost_type_id)
6569         and     C.inventory_item_id = CICD.inventory_item_id(+)
6570         and     C.organization_id  = CICD.organization_id(+)
6571         and     C.cost_type_id = CICD.cost_type_id(+)
6572         and     CICD.rollup_source_type(+) = 1      -- User Defined
6573         --4172300
6574         and     MP1.organization_id in ( select distinct MP2.cost_organization_id
6575                                              from mtl_parameters mp2, mtl_parameters mp3, bom_cto_src_orgs bcso
6576                                             where bcso.model_item_id = pModelId
6577                                               and bcso.line_id = pLineId
6578                                               and MP3.organization_id = bcso.organization_id
6579                                               and ((mp2.organization_id = bcso.organization_id) OR
6580                                                   (mp2.organization_id = mp3.master_organization_id))
6581                                          )
6582         and NOT EXISTS
6583                 (select NULL
6584                 from CST_ITEM_COSTS
6585                 where inventory_item_id = pConfigId
6586                 and organization_id = mp1.cost_organization_id
6587                 and cost_type_id  in (mp1.primary_cost_method, mp1.avg_rates_cost_type_id))
6588         group by C.organization_id, C.cost_type_id; -- 4172300
6589 
6590 
6591         lStmtNumber := 214;
6592 
6593 
6594 
6595        if( l_rt_cicd_summary.cost_organization_id.count > 0 ) then
6596            for i in l_rt_cicd_summary.cost_organization_id.first..l_rt_cicd_summary.cost_organization_id.last
6597            loop
6598 
6599                oe_debug_pub.add( i || ') ' || 'Cost Header Info: ' ||
6600                          ' cst org ' || l_rt_cicd_summary.cost_organization_id(i) ||
6601                          ' cst id ' || l_rt_cicd_summary.cost_type_id(i) ||
6602                          ' m cost ' || l_rt_cicd_summary.material_cost(i) ||
6603                          ' moh cost ' || l_rt_cicd_summary.material_overhead_cost(i) ||
6604                          ' rsc cost ' || l_rt_cicd_summary.resource_cost(i) ||
6605                          ' osp cost ' || l_rt_cicd_summary.outside_processing_cost(i) ||
6606                          ' ovh cost ' || l_rt_cicd_summary.overhead_cost(i) , 1 );
6607 
6608 
6609 
6610                l_rt_cicd_summary.item_cost(i) := l_rt_cicd_summary.material_cost(i) + l_rt_cicd_summary.material_overhead_cost(i)
6611                                         + l_rt_cicd_summary.resource_cost(i) + l_rt_cicd_summary.outside_processing_cost(i)
6612                                         + l_rt_cicd_summary.overhead_cost(i) ;
6613 
6614 
6615 
6616                oe_debug_pub.add( ' item cost ' || l_rt_cicd_summary.item_cost(i) , 1 );
6617 
6618 
6619             end loop ;
6620 
6621 
6622         else
6623 
6624 
6625                oe_debug_pub.add( ' no new item cost records for  ' || pConfigId  , 1 );
6626 
6627         end if;
6628 
6629       	/*-------------------------------------------------------+
6630         Insert a row into the cst_item_costs_table
6631       	+------------------------------------------------------- */
6632 
6633       	lStmtNumber := 220;
6634 
6635       	insert into CST_ITEM_COSTS
6636             	(inventory_item_id,
6637              	organization_id,
6638              	cost_type_id,
6639              	last_update_date,
6640              	last_updated_by,
6641              	creation_date,
6642              	created_by,
6643              	last_update_login,
6644              	inventory_asset_flag,
6645              	lot_size,
6646              	based_on_rollup_flag,
6647              	shrinkage_rate,
6648              	defaulted_flag,
6649              	cost_update_id,
6650              	pl_material,
6651              	pl_material_overhead,
6652              	pl_resource,
6653              	pl_outside_processing,
6654              	pl_overhead,
6655              	tl_material,
6656              	tl_material_overhead,
6657              	tl_resource,
6658              	tl_outside_processing,
6659              	tl_overhead,
6660              	material_cost,
6661              	material_overhead_cost,
6662              	resource_cost,
6663              	outside_processing_cost ,
6664              	overhead_cost,
6665              	pl_item_cost,
6666              	tl_item_cost,
6667              	item_cost,
6668              	unburdened_cost ,
6669              	burden_cost,
6670              	attribute_category,
6671              	attribute1,
6672              	attribute2,
6673              	attribute3,
6674              	attribute4,
6675              	attribute5,
6676              	attribute6,
6677              	attribute7,
6678              	attribute8,
6679              	attribute9,
6680              	attribute10,
6681              	attribute11,
6682              	attribute12,
6683              	attribute13,
6684              	attribute14,
6685              	attribute15
6686             	)
6687       	select distinct
6688 		pConfigId,                -- INVENTORY_ITEM_ID
6689              	mp1.cost_organization_id,
6690              	c.cost_type_id,
6691              	sysdate,                  -- last_update_date
6692              	-1,                       -- last_updated_by
6693              	sysdate,                  -- creation_date
6694              	-1,                       -- created_by
6695              	-1,                       -- last_update_login
6696              	C.inventory_asset_flag,
6697              	C.lot_size,
6698              	C.based_on_rollup_flag,
6699              	C.shrinkage_rate,
6700              	C.defaulted_flag,
6701              	NULL,                     -- cost_update_id
6702              	0,                        -- C.pl_material,
6703              	0,                        -- C.pl_material_overhead,
6704              	0,                        -- C.pl_resource,
6705              	0,                        -- C.pl_outside_processing,
6706              	0,                        -- C.pl_overhead,
6707              	v_material_cost,          -- C.tl_material,
6708              	v_material_overhead_cost, -- C.tl_material_overhead,
6709              	v_resource_cost,          -- C.tl_resource,
6710              	v_outside_processing_cost, -- C.tl_outside_processing,
6711              	v_overhead_cost,          --C.tl_overhead,
6712              	v_material_cost,            -- material_cost
6713              	v_material_overhead_cost,   -- material_overhead_cost
6714              	v_resource_cost,            -- resource_cost
6715              	v_outside_processing_cost,  -- outside_processing_cost
6716              	v_overhead_cost,            -- overhead_cost
6717              	0,                        -- C.pl_item_cost,
6718              	v_item_cost,              -- C.tl_item_cost,
6719              	v_item_cost,              -- C.item_cost,
6720              	0,                        -- C.unburdened_cost ,
6721              	v_material_overhead_cost, -- C.burden_cost,  /* check with rixin */
6722              	C.attribute_category,
6723              	C.attribute1,
6724              	C.attribute2,
6725              	C.attribute3,
6726              	C.attribute4,
6727              	C.attribute5,
6728              	C.attribute6,
6729              	C.attribute7,
6730              	C.attribute8,
6731              	C.attribute9,
6732              	C.attribute10,
6733              	C.attribute11,
6734              	C.ATTRIBUTE12,
6735              	C.attribute13,
6736              	C.attribute14,
6737              	C.attribute15
6738         from
6739                 mtl_parameters MP1,
6740                 cst_item_costs C,
6741                 mtl_system_items S,
6742                 bom_cto_src_orgs bcso
6743         where  S.organization_id   = C.organization_id
6744         and    S.inventory_item_id = C.inventory_item_id
6745         and    C.inventory_item_id = pModelId
6746         and    C.inventory_item_id = S.inventory_item_id
6747         and bcso.model_item_id = pModelId
6748         and bcso.line_id = pLineId
6749         and    C.cost_type_id  in ( mp1.primary_cost_method, mp1.avg_rates_cost_type_id)
6750         and    C.organization_id   = MP1.organization_id
6751 	and    mp1.organization_id = bcso.organization_id
6752         and NOT EXISTS
6753                 (select NULL
6754                 from CST_ITEM_COSTS
6755                 where inventory_item_id = pConfigId
6756                 and organization_id = mp1.organization_id
6757                 and cost_type_id  in (mp1.primary_cost_method, mp1.avg_rates_cost_type_id));
6758 
6759 
6760         --Bugfix 6717614
6761         lCnt := sql%rowcount;
6762         --Bugfix 6717614
6763 
6764 	IF PG_DEBUG <> 0 THEN
6765 		oe_debug_pub.add('Create_Item: ' || ' config item ' || pConfigId || ' after insert:CST_ITEM_COSTS inserted '|| to_char(lCnt),2);
6766 	END IF;
6767 
6768 
6769         if( l_rt_cicd_summary.cost_type_id.count> 0 ) then
6770         FORALL j IN 1..l_rt_cicd_summary.cost_type_id.last
6771               UPDATE cst_item_costs
6772                  set material_cost = l_rt_cicd_summary.material_cost(j),
6773                      material_overhead_cost = l_rt_cicd_summary.material_overhead_cost(j),
6774                      resource_cost = l_rt_cicd_summary.resource_cost(j),
6775                      outside_processing_cost = l_rt_cicd_summary.outside_processing_cost(j),
6776                      overhead_cost = l_rt_cicd_summary.overhead_cost(j),
6777                      tl_material = l_rt_cicd_summary.material_cost(j),
6778                      tl_material_overhead = l_rt_cicd_summary.material_overhead_cost(j),
6779                      tl_resource = l_rt_cicd_summary.resource_cost(j),
6780                      tl_outside_processing = l_rt_cicd_summary.outside_processing_cost(j),
6781                      tl_overhead = l_rt_cicd_summary.overhead_cost(j),
6782                      tl_item_cost = l_rt_cicd_summary.item_cost(j),
6783                      item_cost = l_rt_cicd_summary.item_cost(j),
6784                      burden_cost = l_rt_cicd_summary.material_overhead_cost(j)
6785               where inventory_item_id = pConfigId
6786                 and organization_id = l_rt_cicd_summary.cost_organization_id(j)
6787                 and cost_type_id = l_rt_cicd_summary.cost_type_id(j) ;
6788 
6789 
6790 	IF PG_DEBUG <> 0 THEN
6791 		oe_debug_pub.add('Create_Item: ' || 'after update:CST_ITEM_COSTS '|| to_char(sql%rowcount),2);
6792 	END IF;
6793          else
6794 
6795              oe_debug_pub.add( 'No update required to CST_ITEM_COSTS as no new records inserted ' , 1 ) ;
6796 
6797          end if;
6798 
6799 
6800 
6801 
6802 
6803 
6804 
6805 	/* For standard costing orgs, we will copy model's user-defined
6806 	cost in Frozen to the config in CTO cost type. */
6807 
6808        /* begin bugfix 4057651, default CTO cost type id = 7 if it does not exist */
6809         begin
6810 
6811 	   select cost_type_id into l_cto_cost_type_id
6812              from cst_cost_types
6813             where cost_type = 'CTO' ;
6814 
6815         exception
6816         when no_data_found then
6817 
6818 	   IF PG_DEBUG <> 0 THEN
6819 		oe_debug_pub.add('Create_Item: ' || ' no_data_found error CTO cost type id does not exist',2);
6820 		oe_debug_pub.add('Create_Item: ' || ' defaulting CTO cost type id = 7 ',2);
6821 	   END IF;
6822 
6823            l_cto_cost_type_id := 7 ;
6824 
6825            begin
6826                 select cost_type into v_cto_cost_type_name
6827                   from cst_cost_types
6828                  where cost_type_id = l_cto_cost_type_id  ;
6829 
6830 	         IF PG_DEBUG <> 0 THEN
6831 		    oe_debug_pub.add('Create_Item: ' || ' cost type id =  ' || l_cto_cost_type_id ||
6832                                      '  has cost_type =  -' || v_cto_cost_type_name  || '- delimiter(-cost-)' ,2);
6833 	          END IF;
6834            exception
6835            when no_data_found then
6836 	         IF PG_DEBUG <> 0 THEN
6837 		    oe_debug_pub.add('Create_Item: ' || ' no_data_found error for cost type id = 7 ',2);
6838 	          END IF;
6839                  cto_msg_pub.cto_message('BOM','CTO_COST_NOT_FOUND');
6840                  raise  FND_API.G_EXC_ERROR;
6841            when others then
6842 
6843               raise  FND_API.G_EXC_UNEXPECTED_ERROR;
6844            end ;
6845 
6846         when others then
6847            raise  FND_API.G_EXC_UNEXPECTED_ERROR;
6848         end ;
6849        /* end bugfix 4057651, default CTO cost type id = 7 if it does not exist */
6850 
6851 
6852 
6853       	insert into CST_ITEM_COSTS
6854             	(inventory_item_id,
6855              	organization_id,
6856              	cost_type_id,
6857              	last_update_date,
6858              	last_updated_by,
6859              	creation_date,
6860              	created_by,
6861              	last_update_login,
6862              	inventory_asset_flag,
6863              	lot_size,
6864              	based_on_rollup_flag,
6865              	shrinkage_rate,
6866              	defaulted_flag,
6867              	cost_update_id,
6868              	pl_material,
6869              	pl_material_overhead,
6870              	pl_resource,
6871              	pl_outside_processing,
6872              	pl_overhead,
6873              	tl_material,
6874              	tl_material_overhead,
6875              	tl_resource,
6876              	tl_outside_processing,
6877              	tl_overhead,
6878              	material_cost,
6879              	material_overhead_cost,
6880              	resource_cost,
6881              	outside_processing_cost ,
6882              	overhead_cost,
6883              	pl_item_cost,
6884              	tl_item_cost,
6885              	item_cost,
6886              	unburdened_cost ,
6887              	burden_cost,
6888              	attribute_category,
6889              	attribute1,
6890              	attribute2,
6891              	attribute3,
6892              	attribute4,
6893              	attribute5,
6894              	attribute6,
6895              	attribute7,
6896              	attribute8,
6897              	attribute9,
6898              	attribute10,
6899              	attribute11,
6900              	attribute12,
6901              	attribute13,
6902              	attribute14,
6903              	attribute15
6904             	)
6905       	select distinct
6906 		pConfigId,                -- INVENTORY_ITEM_ID
6907              	mp1.cost_organization_id,
6908              	l_cto_cost_type_id, 	  -- CTO cost_type_id,
6909              	sysdate,                  -- last_update_date
6910              	-1,                       -- last_updated_by
6911              	sysdate,                  -- creation_date
6912              	-1,                       -- created_by
6913              	-1,                       -- last_update_login
6914              	C.inventory_asset_flag,
6915              	C.lot_size,
6916              	C.based_on_rollup_flag,
6917              	C.shrinkage_rate,
6918              	C.defaulted_flag,
6919              	NULL,                     -- cost_update_id
6920              	0,                        -- C.pl_material,
6921              	0,                        -- C.pl_material_overhead,
6922              	0,                        -- C.pl_resource,
6923              	0,                        -- C.pl_outside_processing,
6924              	0,                        -- C.pl_overhead,
6925              	v_material_cost,          -- C.tl_material,
6926              	v_material_overhead_cost, -- C.tl_material_overhead,
6927              	v_resource_cost,          -- C.tl_resource,
6928              	v_outside_processing_cost, -- C.tl_outside_processing,
6929              	v_overhead_cost,           -- C.tl_overhead,
6930              	v_material_cost,           -- material cost
6931              	v_material_overhead_cost,  -- material overhead cost
6932              	v_resource_cost,           -- resource cost
6933              	v_outside_processing_cost, -- outside processing cost
6934              	v_overhead_cost,           -- overhead cost
6935              	0,                       -- C.pl_item_cost,
6936              	v_item_cost,               -- C.tl_item_cost,
6937              	v_item_cost,               -- total item cost
6938              	0,                         -- C.unburdened_cost ,
6939              	v_material_overhead_cost,   -- C.burden_cost,
6940              	C.attribute_category,
6941              	C.attribute1,
6942              	C.attribute2,
6943              	C.attribute3,
6944              	C.attribute4,
6945              	C.attribute5,
6946              	C.attribute6,
6947              	C.attribute7,
6948              	C.attribute8,
6949              	C.attribute9,
6950              	C.attribute10,
6951              	C.attribute11,
6952              	C.ATTRIBUTE12,
6953              	C.attribute13,
6954              	C.attribute14,
6955              	C.attribute15
6956         from
6957                 mtl_parameters MP1,
6958                 cst_item_costs C,
6959                 mtl_system_items S,
6960                 bom_cto_src_orgs bcso
6961         where  S.organization_id   = C.organization_id
6962         and    S.inventory_item_id = C.inventory_item_id
6963         and    C.inventory_item_id = pModelId
6964         and    C.inventory_item_id = S.inventory_item_id
6965         and bcso.model_item_id = pModelId
6966         and bcso.line_id = pLineId
6967         and    C.cost_type_id  = mp1.primary_cost_method
6968         and    C.cost_type_id  = 1
6969         and    C.organization_id   = MP1.organization_id
6970 	and    mp1.organization_id = bcso.organization_id
6971         and NOT EXISTS
6972                 (select NULL
6973                 from CST_ITEM_COSTS
6974                 where inventory_item_id = pConfigId
6975                 and organization_id = mp1.organization_id
6976                 and cost_type_id = l_cto_cost_type_id);
6977 
6978 
6979 
6980 	IF PG_DEBUG <> 0 THEN
6981 		oe_debug_pub.add('Create_Item: ' || ' config item ' || pConfigId || ' after insert UD cost into CTO cost type inserted '|| to_char(sql%rowcount),2);
6982 
6983 	END IF;
6984 
6985 
6986         if( l_rt_cicd_summary.cost_type_id.count > 0 ) then
6987         FORALL j IN 1..l_rt_cicd_summary.cost_type_id.last
6988               UPDATE cst_item_costs
6989                  set material_cost = l_rt_cicd_summary.material_cost(j),
6990                      material_overhead_cost = l_rt_cicd_summary.material_overhead_cost(j),
6991                      resource_cost = l_rt_cicd_summary.resource_cost(j),
6992                      outside_processing_cost = l_rt_cicd_summary.outside_processing_cost(j),
6993                      overhead_cost = l_rt_cicd_summary.overhead_cost(j),
6994                      tl_material = l_rt_cicd_summary.material_cost(j),
6995                      tl_material_overhead = l_rt_cicd_summary.material_overhead_cost(j),
6996                      tl_resource = l_rt_cicd_summary.resource_cost(j),
6997                      tl_outside_processing = l_rt_cicd_summary.outside_processing_cost(j),
6998                      tl_overhead = l_rt_cicd_summary.overhead_cost(j),
6999                      tl_item_cost = l_rt_cicd_summary.item_cost(j),
7000                      item_cost = l_rt_cicd_summary.item_cost(j),
7001                      burden_cost = l_rt_cicd_summary.material_overhead_cost(j)
7002               where inventory_item_id = pConfigId
7003                 and organization_id = l_rt_cicd_summary.cost_organization_id(j)
7004                 and cost_type_id = l_cto_cost_type_id  ;
7005 
7006 
7007 	IF PG_DEBUG <> 0 THEN
7008 		oe_debug_pub.add('Create_Item: ' || 'after update:cst_item_costs for CTO cost type  '||to_char(sql%rowcount),2);
7009 	END IF;
7010 
7011          else
7012 
7013              oe_debug_pub.add( 'No update required to CST_ITEM_COSTS for CTO cost type as no new records inserted ' , 1 ) ;
7014 
7015          end if;
7016 
7017 
7018 
7019 
7020 
7021       	/*------ ----------------------------------------------+
7022          Insert rows into the cst_item_cost_details table
7023       	+-----------------------------------------------------*/
7024 
7025       	lStmtNumber := 230;
7026 
7027       	insert into cst_item_cost_details
7028             	(inventory_item_id,
7029              	cost_type_id,
7030              	last_update_date,
7031              	last_updated_by,
7032              	creation_date,
7033              	created_by,
7034              	last_update_login,
7035              	organization_id,
7036              	operation_sequence_id,
7037              	operation_seq_num,
7038              	department_id,
7039              	level_type,
7040              	activity_id,
7041              	resource_seq_num,
7042              	resource_id,
7043              	resource_rate,
7044              	item_units,
7045              	activity_units,
7046              	usage_rate_or_amount,
7047              	basis_type,
7048              	basis_resource_id,
7049              	basis_factor,
7050              	net_yield_or_shrinkage_factor,
7051              	item_cost,
7052              	cost_element_id,
7053              	rollup_source_type,
7054              	activity_context,
7055              	attribute_category,
7056              	attribute1,
7057              	attribute2,
7058              	attribute3,
7059              	attribute4,
7060              	attribute5,
7061              	attribute6,
7062              	attribute7,
7063              	attribute8,
7064              	attribute9,
7065              	attribute10,
7066              	attribute11,
7067              	attribute12,
7068              	attribute13,
7069              	attribute14,
7070              	attribute15
7071             	)
7072       	select distinct
7073 		pConfigId,                   -- inventory_item_id
7074              	c.cost_type_id,
7075              	sysdate,                     -- last_update_date
7076              	-1,                          -- last_updated_by
7077              	sysdate,                     -- creation_date
7078              	-1,                          -- created_by
7079              	-1,                          -- last_update_login
7080              	mp1.cost_organization_id,
7081              	c.operation_sequence_id,
7082              	c.operation_seq_num,
7083              	c.department_id,
7084              	c.level_type,
7085              	c.activity_id,
7086              	c.resource_seq_num,
7087              	c.resource_id,
7088              	c.resource_rate,
7089              	c.item_units,
7090              	c.activity_units,
7091              	c.usage_rate_or_amount,
7092              	c.basis_type,
7093              	c.basis_resource_id,
7094              	c.basis_factor,
7095              	c.net_yield_or_shrinkage_factor,
7096              	c.item_cost,
7097              	c.cost_element_id,
7098              	C.rollup_source_type,
7099              	C.activity_context,
7100              	C.attribute_category,
7101              	C.attribute1,
7102              	C.attribute2,
7103              	C.attribute3,
7104              	C.attribute4,
7105              	C.attribute5,
7106              	C.attribute6,
7107              	C.attribute7,
7108              	C.attribute8,
7109              	C.attribute9,
7110              	C.attribute10,
7111              	C.attribute11,
7112              	C.attribute12,
7113              	C.attribute13,
7114              	C.attribute14,
7115              	C.attribute15
7116         from
7117                 mtl_parameters        MP1,
7118                 cst_item_cost_details C,
7119                 mtl_system_items      S,
7120                 bom_cto_src_orgs        bcso
7121         where   S.organization_id   = C.organization_id
7122         and     S.inventory_item_id = C.inventory_item_id
7123         and     bcso.model_item_id = pModelId
7124         and     bcso.line_id = pLineId
7125         and     C.organization_id   = MP1.organization_id
7126         and     C.inventory_item_id = pModelId
7127         and     C.inventory_item_id = S.inventory_item_id
7128         and     C.rollup_source_type = 1      -- User Defined
7129         and     C.COST_TYPE_ID  IN ( MP1.primary_cost_method, MP1.avg_rates_cost_type_id)
7130 	and     mp1.organization_id = bcso.organization_id
7131         and NOT EXISTS
7132                 (select NULL
7133                 from cst_item_cost_details
7134                 where inventory_item_id = pConfigId
7135                 and organization_id = mp1.organization_id
7136                 and COST_TYPE_ID  IN (MP1.primary_cost_method, MP1.avg_rates_cost_type_id));
7137 
7138 
7139 
7140 	IF PG_DEBUG <> 0 THEN
7141 		oe_debug_pub.add('Create_Item: ' || 'after insert:cst_item_cost_details inserted '||to_char(sql%rowcount),2);
7142 	END IF;
7143 
7144 
7145 
7146 
7147 
7148 	/* For standard costing orgs, we will copy model's user-defined
7149 	cost in Frozen to the config in CTO cost type. */
7150 
7151       	insert into cst_item_cost_details
7152             	(inventory_item_id,
7153              	cost_type_id,
7154              	last_update_date,
7155              	last_updated_by,
7156              	creation_date,
7157              	created_by,
7158              	last_update_login,
7159              	organization_id,
7160              	operation_sequence_id,
7161              	operation_seq_num,
7162              	department_id,
7163              	level_type,
7164              	activity_id,
7165              	resource_seq_num,
7166              	resource_id,
7167              	resource_rate,
7168              	item_units,
7169              	activity_units,
7170              	usage_rate_or_amount,
7171              	basis_type,
7172              	basis_resource_id,
7173              	basis_factor,
7174              	net_yield_or_shrinkage_factor,
7175              	item_cost,
7176              	cost_element_id,
7177              	rollup_source_type,
7178              	activity_context,
7179              	attribute_category,
7180              	attribute1,
7181              	attribute2,
7182              	attribute3,
7183              	attribute4,
7184              	attribute5,
7185              	attribute6,
7186              	attribute7,
7187              	attribute8,
7188              	attribute9,
7189              	attribute10,
7190              	attribute11,
7191              	attribute12,
7192              	attribute13,
7193              	attribute14,
7194              	attribute15
7195             	)
7196       	select distinct
7197 		pConfigId,                   -- inventory_item_id
7198              	l_cto_cost_type_id, 	     -- CTO cost_type_id,
7199              	sysdate,                     -- last_update_date
7200              	-1,                          -- last_updated_by
7201              	sysdate,                     -- creation_date
7202              	-1,                          -- created_by
7203              	-1,                          -- last_update_login
7204              	mp1.cost_organization_id,
7205              	c.operation_sequence_id,
7206              	c.operation_seq_num,
7207              	c.department_id,
7208              	c.level_type,
7209              	c.activity_id,
7210              	c.resource_seq_num,
7211              	c.resource_id,
7212              	c.resource_rate,
7213              	c.item_units,
7214              	c.activity_units,
7215              	c.usage_rate_or_amount,
7216              	c.basis_type,
7217              	c.basis_resource_id,
7218              	c.basis_factor,
7219              	c.net_yield_or_shrinkage_factor,
7220              	c.item_cost,
7221              	c.cost_element_id,
7222              	C.rollup_source_type,
7223              	C.activity_context,
7224              	C.attribute_category,
7225              	C.attribute1,
7226              	C.attribute2,
7227              	C.attribute3,
7228              	C.attribute4,
7229              	C.attribute5,
7230              	C.attribute6,
7231              	C.attribute7,
7232              	C.attribute8,
7233              	C.attribute9,
7234              	C.attribute10,
7235              	C.attribute11,
7236              	C.attribute12,
7237              	C.attribute13,
7238              	C.attribute14,
7239              	C.attribute15
7240         from
7241                 mtl_parameters        MP1,
7242                 cst_item_cost_details C,
7243                 mtl_system_items      S,
7244                 bom_cto_src_orgs        bcso
7245         where   S.organization_id   = C.organization_id
7246         and     S.inventory_item_id = C.inventory_item_id
7247         and     bcso.model_item_id = pModelId
7248         and     bcso.line_id = pLineId
7249         and     C.organization_id   = MP1.organization_id
7250         and     C.inventory_item_id = pModelId
7251         and     C.inventory_item_id = S.inventory_item_id
7252         and     C.rollup_source_type = 1      -- User Defined
7253         and     C.COST_TYPE_ID = MP1.primary_cost_method
7254         and     C.cost_type_id = 1
7255 	and     mp1.organization_id = bcso.organization_id
7256         and NOT EXISTS
7257                 (select NULL
7258                 from cst_item_cost_details
7259                 where inventory_item_id = pConfigId
7260                 and organization_id = mp1.organization_id
7261                 and COST_TYPE_ID = l_cto_cost_type_id);
7262 
7263 
7264 
7265 
7266 	IF PG_DEBUG <> 0 THEN
7267 		oe_debug_pub.add('Create_Item: ' || 'after insert UD cost into CICD for CTO cost type inserted '||to_char(sql%rowcount),2);
7268 	END IF;
7269 
7270 
7271         --Bugfix 6717614
7272         if lCnt = 0 then
7273            IF PG_DEBUG <> 0 THEN
7274 		oe_debug_pub.add('Create_Item: ' || 'No need to populate csc and cec as no new record inserted in cic and cicd',2);
7275 	   END IF;
7276         else
7277 
7278         --begin Bugfix 6363308
7279         lStmtNumber := 231;
7280         IF PG_DEBUG <> 0 THEN
7281 		oe_debug_pub.add('Create_Item: ' || 'going to populate data in csc and cec if frozen cost of config is not zero',2);
7282 	END IF;
7283 
7284         OPEN get_orgs_with_frozen_cost(pconfigId);
7285            loop
7286 
7287               FETCH get_orgs_with_frozen_cost INTO  v_organization_id
7288                                                   , v_item_cost_frozen;
7289               EXIT WHEN get_orgs_with_frozen_cost%NOTFOUND;
7290 
7291               IF PG_DEBUG <> 0 THEN
7292 		        oe_debug_pub.add('Create_Item: ' || 'fetched org '||to_char(v_organization_id),2);
7293                         oe_debug_pub.add('Create_Item: ' || 'frozen cost for config '||to_char(pConfigId) || 'is: ' || to_char(v_item_cost_frozen),2);
7294 	      END IF;
7295 
7296               IF (v_item_cost_frozen <> 0) THEN
7297 
7298                     lStmtNumber := 232;
7299                     IF PG_DEBUG <> 0 THEN
7300 		        oe_debug_pub.add('Create_Item: ' || 'Came inside if. v_item_cost_frozen is not zero',2);
7301                         oe_debug_pub.add('Create_Item: ' || 'Fetching value from sequence',2);
7302 	            END IF;
7303 
7304                     Select cst_lists_s.nextval
7305                       into l_cost_update
7306                         From DUAL;
7307 
7308                     lStmtNumber := 233;
7309 
7310                     UPDATE CST_ITEM_COSTS
7311                       SET cost_update_id = l_cost_update
7312                       WHERE  ORGANIZATION_ID = v_organization_id
7313                       AND  INVENTORY_ITEM_ID = pConfigId
7314                       AND  COST_TYPE_ID = 1;
7315 
7316                     IF PG_DEBUG <> 0 THEN
7317                        oe_debug_pub.add('Create_Item: ' || 'Updated cost_update_id to value ' || to_char(l_cost_update),2);
7318                     END IF;
7319 
7320                    lStmtNumber := 234;
7321                    IF PG_DEBUG <> 0 THEN
7322         	        oe_debug_pub.add('Create_Item: ' || 'Inserting records in csc and cec',2);
7323                    END IF;
7324 
7325                    INSERT INTO cst_standard_costs
7326                            (cost_update_id,
7327                             organization_id,
7328                             inventory_item_id,
7329                             last_update_date,
7330                             last_updated_by,
7331                             creation_date,
7332                             created_by,
7333                             last_update_login,
7334                             standard_cost_revision_date,
7335                             standard_cost
7336                            )
7337                    SELECT l_cost_update,
7338                           v_organization_id,
7339                           pConfigId,
7340                           SYSDATE,
7341                           -1,
7342                           SYSDATE,
7343                           -1,
7344                           -1,
7345                           SYSDATE,
7346                           NVL(SUM(item_cost),0)
7347                    FROM cst_item_cost_details
7348                    WHERE ORGANIZATION_ID = v_organization_id
7349                    AND  INVENTORY_ITEM_ID = pConfigId
7350                    AND  COST_TYPE_ID = 1;
7351 
7352                    IF PG_DEBUG <> 0 THEN
7353         	        oe_debug_pub.add('Create_Item: ' || 'after insert:cst_standard_costs ' || sql%rowcount ,2);
7354                    END IF;
7355 
7356                    lStmtNumber := 235;
7357 
7358                    INSERT INTO cst_elemental_costs
7359                            (cost_update_id,
7360                             organization_id,
7361                             inventory_item_id,
7362                             cost_element_id,
7363                             last_update_date,
7364                             last_updated_by,
7365                             creation_date,
7366                             created_by,
7367                             last_update_login,
7368                             standard_cost
7369                            )
7370                    SELECT l_cost_update,
7371                           v_organization_id,
7372                           pConfigId,
7373                           cost_element_id,
7374                           SYSDATE,
7375                           -1,
7376                           SYSDATE,
7377                           -1,
7378                           -1,
7379                           NVL(SUM(item_cost),0)
7380                    FROM cst_item_cost_details
7381                    WHERE ORGANIZATION_ID = v_organization_id
7382                    AND  INVENTORY_ITEM_ID = pConfigId
7383                    AND  COST_TYPE_ID = 1
7384                    GROUP BY cost_element_id;
7385 
7386                    IF PG_DEBUG <> 0 THEN
7387         	        oe_debug_pub.add('Create_Item: ' || 'after insert:cst_elemental_costs ' || sql%rowcount ,2);
7388                    END IF;
7389                  END IF;  -- v_item_cost_frozen <> 0
7390            end loop;
7391 
7392            CLOSE get_orgs_with_frozen_cost;
7393         --End Bugfix 6363308
7394         end if;  --lCnt = 0
7395 
7396 
7397      	/*------------------------------------------------+
7398           check the following statement
7399           If the project id is not specified on the line,
7400           l_cost_group_id should be 1
7401      	+-------------------------------------------------*/
7402 
7403 	lStmtNumber := 240;
7404 
7405 	--
7406 	-- Currently, project mfg is not fully supported for ML ATO
7407 	-- Currently, if a project is specified on the model line,
7408 	-- we get the cost group id for this project in the ship-from-org
7409 	-- This may not work correctly for mulit-sourcing, if different
7410 	-- projects are assigned to different orgs.
7411 	--
7412 	select ship_from_org_id
7413 	into lShipFromOrg
7414 	from bom_cto_order_lines bcol
7415 	where line_id = pLineID;
7416 
7417 	l_cost_group_id := get_cost_group(lShipFromOrg, pLineID);
7418 
7419 	if (l_cost_group_id = 0) then
7420                 l_cost_group_id := 1;
7421 	end if;
7422 
7423 
7424      -- Bugfix 1573941 : Added IF condition to check the profile : "CST:Average costing option"
7425      -- Only when this profile is set to "Inventory and WIP", should the records be inserted in CQL.
7426      -- 1 = Inventory Only
7427      -- 2 = Inventory and WIP
7428 
7429 	IF ( nvl(fnd_profile.value('CST_AVG_COSTING_OPTION'), '1') = '2' ) THEN
7430 	  FOR v_layer in c_layer
7431 	  LOOP
7432 
7433 	  --
7434 	  -- This costing API will insert a row into cst_quantity_layers
7435 	  -- for a unique layer_id and the given parameters.
7436 	  -- It will return 0 if failed, layer_id if succeeded
7437 	  --
7438 	  l_layer_id := cstpaclm.create_layer (
7439   		i_org_id => v_layer.org_id,
7440   		i_item_id => pConfigId,
7441   		i_cost_group_id => v_layer.cost_group_id,
7442   		i_user_id => gUserId,
7443   		i_request_id => NULL,
7444   		i_prog_id => NULL,
7445   		i_prog_appl_id => NULL,
7446   		i_txn_id => -1,
7447   		o_err_num => x_err_num,
7448   		o_err_code => x_msg_name,
7449   		o_err_msg => lMsgData
7450 		);
7451 
7452 	  IF (l_layer_id = 0) THEN
7453 		IF PG_DEBUG <> 0 THEN
7454 			oe_debug_pub.add('Create_Item: ' || 'CST function create_layer returned with error '||to_char(x_err_num)||', '||x_msg_name||', '||
7455 				lMsgData||'for '||to_char(v_layer.org_id)||', '||to_char(v_layer.cost_group_id),1);
7456 		END IF;
7457 		RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7458 	  ELSE
7459 		IF PG_DEBUG <> 0 THEN
7460 			oe_debug_pub.add('Create_Item: ' || 'Inserted row into cql for '||to_char(l_layer_id)||', '||to_char(v_layer.org_id)||', '||
7461 				to_char(v_layer.cost_group_id),1);
7462 		END IF;
7463 	  END IF;
7464 
7465 	  END LOOP;
7466 	END IF; /* End bugfix 1573941 */
7467 
7468 	IF PG_DEBUG <> 0 THEN
7469 		oe_debug_pub.add('Create_Item: ' || 'after insert:cst_quantity_layers ',2);
7470 	END IF;
7471 
7472 
7473       	/*--------------------------------------------------------+
7474         Insert rows into the mtl_desc_elem_val_interface table
7475         Descriptive elements are not organization controlled
7476 	Using validation org to get values
7477       	+---------------------------------------------------------*/
7478 
7479       	lStmtNumber := 250;
7480 
7481       	insert into MTL_DESCR_ELEMENT_VALUES
7482          	(inventory_item_id,
7483              	element_name,
7484              	last_update_date,
7485              	last_updated_by,
7486              	last_update_login,
7487              	creation_date,
7488              	created_by,
7489              	element_value,
7490              	default_element_flag,
7491              	element_sequence,
7492              	program_application_id,
7493              	program_id,
7494              	program_update_date,
7495              	request_id
7496             	)
7497       	select distinct
7498 		pConfigId,                -- Inventory_item_id
7499              	E.element_name,           -- element_name
7500              	sysdate,                  -- last_update_date
7501              	gUserId,                  -- last_updated_by
7502              	gLoginId,                 -- last_update_login
7503              	sysdate,                  -- creation_date
7504              	gUserId,                  -- created_by
7505              	D.element_value,          -- element_value
7506              	E.default_element_flag,   -- default_element_flag
7507              	E.element_sequence,       -- element_sequence
7508              	NULL,                     -- program_application_id
7509              	NULL,                     -- program_id
7510              	SYSDATE,                  -- program_update_date
7511              	NULL                      -- request_id
7512       	from   mtl_system_items  s,
7513              	mtl_descr_element_values D,
7514              	mtl_descriptive_elements E
7515       	where  D.inventory_item_id     = S.inventory_item_id
7516       	and    s.inventory_item_id     = pModelid
7517       	and    s.organization_id       = lValidationOrg
7518       	and    E.item_catalog_group_id = S.item_catalog_group_id
7519       	and    E.element_name          = D.element_name
7520 	and NOT EXISTS
7521                 (select NULL
7522                 from mtl_descr_element_values
7523                 where inventory_item_id = pConfigId
7524                 and organization_id = lValidationOrg);
7525 
7526 	IF PG_DEBUG <> 0 THEN
7527 		oe_debug_pub.add('Create_Item: ' || 'after insert: MTL_DESCR_ELEMENT_VALUES',2);
7528 	END IF;
7529 
7530 
7531       	/*--------------------------------------+
7532           Insert into mtl_item_categories
7533       	+--------------------------------------*/
7534 
7535       	lStmtNumber := 260;
7536 
7537         --bugfix  4861996
7538 	--added ic1.category_set_id = ic.category_set_id condition
7539 
7540       	insert into MTL_ITEM_CATEGORIES
7541             	(inventory_item_id,
7542             	 category_set_id,
7543              	category_id,
7544              	last_update_date,
7545              	last_updated_by,
7546              	creation_date,
7547              	created_by,
7548              	last_update_login,
7549              	request_id,
7550              	program_application_id,
7551              	program_id,
7552              	program_update_date,
7553              	organization_id
7554              	)
7555       	select distinct
7556              	pConfigId,
7557              	ic.category_set_id,
7558              	ic.category_id,
7559              	sysdate,                  -- last_update_date
7560              	gUserId,                  -- last_updated_by
7561              	sysdate,                  --creation_date
7562              	gUserId,                  -- created_by
7563              	gLoginId,                 -- last_update_login
7564              	NULL,                     -- request_id
7565              	NULL,                     -- program_application_id
7566              	NULL,                     -- program_id
7567              	SYSDATE,                  -- program_update_date
7568              	ic.organization_id
7569         from
7570                 mtl_item_categories ic,
7571                 bom_cto_src_orgs        bcso
7572         where   ic.inventory_item_id = pModelId                         --bugfix 2706981: swapped the positions
7573         and     ic.organization_id = bcso.organization_id
7574         and     bcso.model_item_id = ic.inventory_item_id       --bugfix 2706981: replaced pModelId with col join
7575         and     bcso.line_id = pLineId                          --                as in bugfix 2215274
7576         --
7577         -- bugfix 2619501 (butler mfg):
7578         -- We will call custom hook to see which category_set needs to be inserted.
7579         -- If the custom hook returns 1 for a particular category_set_id, then, we will insert that category set.
7580         -- By default, the custom hook will return 1 for all category sets except sales and mktg category set.
7581         --
7582         -- and    ic.category_set_id <> 5       -- bugfix 2395525
7583         and    CTO_CUSTOM_CATEGORY_PK.Copy_Category (ic.category_set_id , ic.organization_id) = 1
7584         --
7585         -- end bugfix 2619501:
7586         --
7587         and NOT EXISTS
7588                 (select NULL
7589                 from  MTL_ITEM_CATEGORIES ic1                   -- bugfix 2706981: added alias
7590                 where ic1.inventory_item_id = pConfigId
7591                 and   ic1.organization_id = bcso.organization_id
7592 	        and   ic1.category_set_id = ic.category_set_id
7593 		);
7594 
7595 
7596 
7597 
7598 
7599 
7600 
7601 	IF PG_DEBUG <> 0 THEN
7602 		oe_debug_pub.add('Create_Item: ' || 'after insert: MTL_ITEM_CATEGORIES',2);
7603 	END IF;
7604 
7605 	-- Start Bugfix 2197842
7606 
7607 	lStmtNumber := 261;
7608 
7609 	insert into MTL_ITEM_CATEGORIES
7610             	(inventory_item_id,
7611             	 category_set_id,
7612              	category_id,
7613              	last_update_date,
7614              	last_updated_by,
7615              	creation_date,
7616              	created_by,
7617              	last_update_login,
7618              	request_id,
7619              	program_application_id,
7620              	program_id,
7621              	program_update_date,
7622              	organization_id
7623              	)
7624          select distinct
7625              	pConfigId,
7626              	mcsb.category_set_id,
7627              	mcsb.default_category_id,
7628              	sysdate,                  -- last_update_date
7629              	gUserId,                  -- last_updated_by
7630              	sysdate,                  --creation_date
7631              	gUserId,                  -- created_by
7632              	gLoginId,                 -- last_update_login
7633              	NULL,                     -- request_id
7634              	NULL,                     -- program_application_id
7635              	NULL,                     -- program_id
7636              	SYSDATE,                  -- program_update_date
7637              	ic.organization_id
7638         from
7639                 mtl_item_categories             ic,
7640                 mtl_category_sets_b             mcsb,
7641                 mtl_default_category_sets       mdcs,
7642                 bom_cto_src_orgs                bcso
7643         where   pModelId = ic.inventory_item_id
7644         and     ic.organization_id = bcso.organization_id
7645         and     bcso.model_item_id = pModelId
7646         and     bcso.line_id = pLineId
7647         and     mcsb.category_set_id = mdcs.category_set_id
7648         and     mdcs.functional_area_id = 2
7649         and     NOT EXISTS
7650                 (
7651                         select NULL
7652                         from MTL_ITEM_CATEGORIES
7653                         where inventory_item_id = pConfigId
7654                         and organization_id = bcso.organization_id
7655                         and category_set_id = mcsb.category_set_id
7656                 );
7657 
7658 
7659 
7660 
7661 
7662 	IF PG_DEBUG <> 0 THEN
7663 		oe_debug_pub.add('Create_Item: ' || 'after insert: MTL_ITEM_CATEGORIES FOR DEFAULT CATEGORIES',2);
7664 	END IF;
7665 
7666 	-- End Bugfix 2197842
7667 
7668       	/*----------------------------------------------------+
7669         Copy related items into MTL_RELATED_ITEMS table
7670       	+----------------------------------------------------*/
7671 
7672       	lStmtNumber := 270;
7673 
7674       	insert into MTL_RELATED_ITEMS
7675            	(
7676              	inventory_item_id,
7677              	related_item_id,
7678              	relationship_type_id,
7679              	reciprocal_flag,
7680              	last_update_date,
7681              	last_updated_by,
7682              	creation_date,
7683              	created_by,
7684              	last_update_login,
7685              	request_id,
7686              	program_application_id,
7687              	program_id,
7688              	program_update_date,
7689              	organization_id
7690             	)
7691       	select distinct
7692              	pConfigId,
7693              	ri.related_item_id,
7694              	ri.relationship_type_id,
7695              	ri.reciprocal_flag,
7696              	sysdate,                  -- last_update_date
7697              	gUserId,                  -- last_updated_by
7698              	sysdate,                  --creation_date
7699              	gUserId,                  -- created_by
7700              	gLoginId,                 -- last_update_login
7701              	NULL,                     -- request_id
7702              	NULL,                     -- program_application_id
7703              	NULL,                     -- program_id
7704              	SYSDATE,                  -- program_update_date
7705              	ri.organization_id
7706         from  mtl_related_items ri,
7707                 bom_cto_src_orgs        bcso
7708         where ri.inventory_item_id = pModelId
7709         and bcso.model_item_id = pModelId
7710         and bcso.line_id = pLineId
7711         and   ri.organization_id   = bcso.organization_id
7712         and NOT EXISTS
7713                 (select NULL
7714                 from mtl_related_items
7715                 where inventory_item_id = pConfigId
7716                 and organization_id = bcso.organization_id);
7717 
7718 
7719 
7720 
7721 
7722 	IF PG_DEBUG <> 0 THEN
7723 		oe_debug_pub.add('Create_Item: ' || 'after insert:mtl_related_items',2);
7724 	END IF;
7725 
7726        	/*--------------------------------------------------+
7727            Copy substitute inventories
7728        	+--------------------------------------------------*/
7729 
7730        	lStmtNumber := 280;
7731 
7732        	insert into mtl_item_sub_inventories
7733            	(
7734              	inventory_item_id,
7735              	organization_id,
7736              	secondary_inventory,
7737              	last_update_date,
7738              	last_updated_by,
7739              	creation_date,
7740              	created_by,
7741              	last_update_login,
7742              	primary_subinventory_flag ,
7743              	picking_order,
7744              	min_minmax_quantity,
7745              	max_minmax_quantity,
7746              	inventory_planning_code,
7747              	fixed_lot_multiple,
7748              	minimum_order_quantity,
7749              	maximum_order_quantity,
7750              	source_type,
7751              	source_organization_id,
7752              	source_subinventory,
7753              	attribute_category,
7754              	attribute1,
7755              	attribute2,
7756              	attribute3,
7757              	attribute4,
7758              	attribute5,
7759              	attribute6,
7760              	attribute7,
7761              	attribute8,
7762              	attribute9,
7763              	attribute10,
7764              	attribute11,
7765              	attribute12,
7766              	attribute13,
7767              	attribute14,
7768              	attribute15,
7769              	request_id,
7770              	program_application_id ,
7771              	program_id,
7772              	program_update_date,
7773              	encumbrance_account
7774              	)
7775        	select distinct
7776              	pConfigId,
7777              	isi.ORGANIZATION_ID,
7778              	isi.SECONDARY_INVENTORY,
7779              	sysdate,                    -- last_update_date
7780              	gUserId,                    -- last_updated_by
7781              	sysdate,                    -- creation_date
7782              	gUserId,                    -- created_by
7783              	gLoginId,                   -- last_update_login
7784              	isi.PRIMARY_SUBINVENTORY_FLAG ,
7785              	isi.PICKING_ORDER,
7786              	isi.MIN_MINMAX_QUANTITY,
7787              	isi.MAX_MINMAX_QUANTITY,
7788              	isi.INVENTORY_PLANNING_CODE,
7789              	isi.FIXED_LOT_MULTIPLE,
7790              	isi.MINIMUM_ORDER_QUANTITY,
7791              	isi.MAXIMUM_ORDER_QUANTITY,
7792              	isi.SOURCE_TYPE,
7793              	isi.SOURCE_ORGANIZATION_ID,
7794              	isi.SOURCE_SUBINVENTORY,
7795              	isi.ATTRIBUTE_CATEGORY,
7796              	isi.ATTRIBUTE1,
7797              	isi.ATTRIBUTE2,
7798              	isi.ATTRIBUTE3,
7799              	isi.ATTRIBUTE4,
7800              	isi.ATTRIBUTE5,
7801              	isi.ATTRIBUTE6,
7802              	isi.ATTRIBUTE7,
7803              	isi.ATTRIBUTE8,
7804              	isi.ATTRIBUTE9,
7805              	isi.ATTRIBUTE10,
7806              	isi.ATTRIBUTE11,
7807              	isi.ATTRIBUTE12,
7808              	isi.ATTRIBUTE13,
7809              	isi.ATTRIBUTE14,
7810              	isi.ATTRIBUTE15,
7811              	NULL,                       -- request_id
7812              	NULL,                       -- program_application_id
7813              	NULL,                       -- program_id
7814              	SYSDATE,                    -- program_update_date
7815              	isi.ENCUMBRANCE_ACCOUNT
7816         from
7817                 mtl_item_sub_inventories isi,
7818                 bom_cto_src_orgs        bcso
7819         where isi.organization_id   = bcso.organization_id
7820         and   isi.inventory_item_id = pModelId
7821         and bcso.model_item_id = pModelId
7822         and bcso.line_id = pLineId
7823         and NOT EXISTS
7824                 (select NULL
7825                 from mtl_item_sub_inventories
7826                 where inventory_item_id = pConfigId
7827                 and organization_id = bcso.organization_id);
7828 
7829 
7830 
7831 
7832 	IF PG_DEBUG <> 0 THEN
7833 		oe_debug_pub.add('Create_Item: ' || 'after insert:mtl_item_sub_inventories',2);
7834 	END IF;
7835 
7836        	/*--------------------------------------+
7837           Copy secondary locators
7838        	+--------------------------------------*/
7839 
7840        	lStmtNumber := 290;
7841 
7842        	insert into mtl_secondary_locators
7843            	(
7844              	inventory_item_id,
7845              	organization_id,
7846              	secondary_locator,
7847              	primary_locator_flag,
7848              	picking_order,
7849              	subinventory_code,
7850              	last_update_date,
7851              	last_updated_by,
7852              	creation_date,
7853              	created_by,
7854              	last_update_login,
7855              	request_id,
7856              	program_application_id,
7857              	program_id,
7858              	program_update_date
7859            	)
7860        	select distinct
7861              	pConfigId,
7862              	sl.organization_id,
7863              	sl.secondary_locator,
7864              	sl.primary_locator_flag,
7865              	sl.picking_order,
7866              	sl.subinventory_code,
7867              	sysdate,                     -- last_update_date
7868              	gUserId,                     -- last_updated_by
7869              	sysdate,                     -- creation_date
7870              	gUserId,                     -- created_by
7871              	gLoginId,                    -- last_update_login
7872              	NULL,                        -- request_id
7873              	NULL,                        -- program_application_id
7874              	NULL,                        -- program_id
7875              	SYSDATE                      -- program_update_date
7876         from
7877                 mtl_secondary_locators sl,
7878                 bom_cto_src_orgs        bcso
7879         where  sl.organization_id = bcso.organization_id
7880         and   pModelId = sl.inventory_item_id
7881         and bcso.model_item_id = pModelId
7882         and bcso.line_id = pLineId
7883         and NOT EXISTS
7884                 (select NULL
7885                 from mtl_secondary_locators
7886                 where inventory_item_id = pConfigId
7887                 and organization_id = bcso.organization_id);
7888 
7889 
7890 
7891 
7892 	IF PG_DEBUG <> 0 THEN
7893 		oe_debug_pub.add('Create_Item: ' || 'after insert: mtl_secondary_locators',2);
7894 	END IF;
7895 
7896       	/*----------------------------------------+
7897             Copy cross references
7898       	+----------------------------------------*/
7899 
7900       	lStmtNumber := 300;
7901 
7902        --start bugfix 4539578
7903 
7904 
7905           SELECT DISTINCT
7906             		CR.ORGANIZATION_ID
7907            	       ,CR.CROSS_REFERENCE_TYPE
7908                        ,CR.CROSS_REFERENCE
7909             	       ,CR.ORG_INDEPENDENT_FLAG
7910           BULK COLLECT INTO
7911 	               t_organization_id,
7912 		       t_cross_ref_type,
7913 		       t_cross_ref,
7914 		       t_org_independent_flag
7915           FROM   MTL_CROSS_REFERENCES_B CR,
7916                  BOM_CTO_SRC_ORGS       BCSO
7917           WHERE  (CR.ORGANIZATION_ID = bcso.ORGANIZATION_ID OR  CR.ORGANIZATION_ID IS NULL)
7918           AND    CR.INVENTORY_ITEM_ID = PMODELID
7919           AND    BCSO.MODEL_ITEM_ID = PMODELID
7920           AND    BCSO.LINE_ID = PLINEID
7921           AND  NOT EXISTS
7922                  ( SELECT NULL
7923                    FROM MTL_CROSS_REFERENCES_B
7924                    WHERE INVENTORY_ITEM_ID = PCONFIGID
7925                    AND ( ORGANIZATION_ID = bcso.ORGANIZATION_ID  OR ORGANIZATION_ID IS NULL) -- bugfix 1960994: added OR condition
7926                        );
7927 
7928         IF t_cross_ref_type.count <> 0 THEN
7929 
7930 	 FORALL i IN 1..t_cross_ref_type.count
7931            INSERT INTO MTL_CROSS_REFERENCES_B
7932                             (
7933                               INVENTORY_ITEM_ID
7934                              ,ORGANIZATION_ID
7935                              ,CROSS_REFERENCE_TYPE
7936                              ,CROSS_REFERENCE
7937                              ,ORG_INDEPENDENT_FLAG
7938                              ,LAST_UPDATE_DATE
7939                              ,LAST_UPDATED_BY
7940                              ,CREATION_DATE
7941                              ,CREATED_BY
7942                              ,LAST_UPDATE_LOGIN
7943                              ,REQUEST_ID
7944                              ,PROGRAM_APPLICATION_ID
7945                              ,PROGRAM_ID
7946                              ,PROGRAM_UPDATE_DATE
7947                              ,SOURCE_SYSTEM_ID
7948                              ,OBJECT_VERSION_NUMBER
7949                              ,UOM_CODE
7950                              ,REVISION_ID
7951                              ,CROSS_REFERENCE_ID
7952                              ,EPC_GTIN_SERIAL
7953                              ,ATTRIBUTE1
7954                              ,ATTRIBUTE2
7955                              ,ATTRIBUTE3
7956                              ,ATTRIBUTE4
7957                              ,ATTRIBUTE5
7958                              ,ATTRIBUTE6
7959                              ,ATTRIBUTE7
7960                              ,ATTRIBUTE8
7961                              ,ATTRIBUTE9
7962                              ,ATTRIBUTE10
7963                              ,ATTRIBUTE11
7964                              ,ATTRIBUTE12
7965                              ,ATTRIBUTE13
7966                              ,ATTRIBUTE14
7967                              ,ATTRIBUTE15
7968                              ,ATTRIBUTE_CATEGORY
7969                            )
7970                      VALUES
7971 		        (
7972                          pConfigId
7973                         ,t_organization_id(i)
7974                         ,t_cross_ref_type(i)
7975   			,t_cross_ref(i)
7976   			,t_org_independent_flag(i)
7977   			,SYSDATE
7978   			,GUSERID
7979   			,SYSDATE
7980   			,GUSERID
7981                         ,GLOGINID
7982                         ,NULL       --REQUEST_ID
7983                         ,NULL       --PROGRAM_APPLICATION_ID
7984   			,NULL       --PROGRAM_ID
7985   			,SYSDATE    --PROGRAM_UPDATE_DATE
7986 		        ,NULL       --SOURCE_SYSTEM_ID
7987   			,1          --OBJECT_VERSION_NUMBER
7988   			,NULL       --UOM_CODE      due to ER#3215422. do not copy uom_code and revision_id attribute for mtl_cross_references
7989   			,NULL       --REVISION_ID   due to ER#3215422. do not copy uom_code and revision_id attribute for mtl_cross_references
7990   			,MTL_CROSS_REFERENCES_B_S.NEXTVAL --CROSS_REFERENCE_ID
7991   			,0          --EPC_GTIN_SERIAL
7992   			,NULL       --ATTRIBUTE1
7993   			,NULL       --ATTRIBUTE2
7994   			,NULL       --ATTRIBUTE3
7995   			,NULL       --ATTRIBUTE4
7996   			,NULL       --ATTRIBUTE5
7997   			,NULL       --ATTRIBUTE6
7998   			,NULL       --ATTRIBUTE7
7999  		        ,NULL       --ATTRIBUTE8
8000  		        ,NULL       --ATTRIBUTE9
8001   			,NULL       --ATTRIBUTE10
8002   			,NULL       --ATTRIBUTE11
8003   			,NULL       --ATTRIBUTE12
8004   			,NULL       --ATTRIBUTE13
8005  			,NULL       --ATTRIBUTE14
8006   			,NULL       --ATTRIBUTE15
8007   			,NULL       --ATTRIBUTE_CATEGORY
8008 		       );
8009 
8010           IF PG_DEBUG <> 0 THEN
8011 		oe_debug_pub.add('Create_Item: ' || 'after insert:mtl_cross_references_b',2);
8012 		oe_debug_pub.add('Create_Item: ' || '# of inserted rows mtl_cross_references_b'||sql%rowcount,2);
8013 
8014 	  END IF;
8015 
8016           INSERT INTO mtl_cross_references_tl (
8017              last_update_login
8018             ,description
8019             ,creation_date
8020             ,created_by
8021             ,last_update_date
8022             ,last_updated_by
8023             ,cross_reference_id
8024             ,language
8025             ,source_lang)
8026          SELECT
8027             gloginid,
8028             mtl.description,
8029             sysdate,
8030             guserid,
8031             sysdate,
8032             guserid,
8033             mtl_cross.cross_reference_id,
8034             l.language_code,
8035             userenv('lang')
8036          FROM  fnd_languages l,
8037 	       mtl_cross_references_b mtl_cross,
8038 	       mtl_system_items_tl mtl
8039          WHERE mtl_cross.inventory_item_id = pConfigId
8040 	 AND   mtl_cross.inventory_item_id = mtl.inventory_item_id
8041 	 AND   mtl_cross.organization_id   = mtl.organization_id
8042          AND   l.language_code  = mtl.language
8043 	 AND   l.installed_flag in ('I', 'B')
8044          AND  NOT EXISTS  (SELECT null
8045                            FROM   mtl_cross_references_tl t
8046                            WHERE  t.cross_reference_id = mtl_cross.cross_reference_id
8047                            AND    t.language = l.language_code);
8048 
8049 
8050 
8051 	IF PG_DEBUG <> 0 THEN
8052 		oe_debug_pub.add('Create_Item: ' || 'after insert:mtl_cross_references_tl',2);
8053 		oe_debug_pub.add('Create_Item: ' || '# of inserted rows mtl_cross_references_tl'||sql%rowcount,2);
8054 
8055 	END IF;
8056 
8057 
8058 	END IF;--cross_reference_type
8059 
8060       --end bugfix 4539578
8061 
8062 	-- start bugfix 2461574
8063 
8064 	/*--------------------------------------+
8065           Copy Subinventory Defaults
8066        	+--------------------------------------*/
8067 
8068        	lStmtNumber := 301;
8069 
8070        	insert into mtl_item_sub_defaults
8071            	(
8072              	inventory_item_id,
8073              	organization_id,
8074              	subinventory_code,
8075              	default_type,
8076              	last_update_date,
8077              	last_updated_by,
8078              	creation_date,
8079              	created_by,
8080              	last_update_login,
8081              	request_id,
8082              	program_application_id,
8083              	program_id,
8084              	program_update_date
8085            	)
8086        	select distinct
8087              	pConfigId,
8088              	sd.organization_id,
8089                	sd.subinventory_code,
8090                	sd.default_type,
8091              	sysdate,                     -- last_update_date
8092              	gUserId,                     -- last_updated_by
8093              	sysdate,                     -- creation_date
8094              	gUserId,                     -- created_by
8095              	gLoginId,                    -- last_update_login
8096              	NULL,                        -- request_id
8097              	NULL,                        -- program_application_id
8098              	NULL,                        -- program_id
8099              	SYSDATE                      -- program_update_date
8100         from
8101                 mtl_item_sub_defaults sd,
8102                 bom_cto_src_orgs        bcso
8103         where   sd.organization_id = bcso.organization_id
8104         and     sd.inventory_item_id = pModelId
8105         and     bcso.model_item_id = pModelId
8106         and     bcso.line_id = pLineId
8107         and NOT EXISTS
8108                 (select NULL
8109                 from mtl_item_sub_defaults
8110                 where inventory_item_id = pConfigId
8111                 and organization_id = bcso.organization_id);
8112 
8113 
8114 	IF PG_DEBUG <> 0 THEN
8115 		oe_debug_pub.add('Create_Item: ' || 'after insert: mtl_item_sub_defaults',2);
8116 	END IF;
8117 
8118 	-- end 2461574
8119 	-- start 2786934
8120 
8121         /*--------------------------------------+
8122           Copy Locator Defaults
8123         +--------------------------------------*/
8124 
8125         lStmtNumber := 302;
8126         insert into mtl_item_loc_defaults
8127                 (
8128                 inventory_item_id,
8129                 organization_id,
8130                 locator_id,
8131                 default_type,
8132                 subinventory_code,
8133                 last_update_date,
8134                 last_updated_by,
8135                 creation_date,
8136                 created_by,
8137                 last_update_login,
8138                 request_id,
8139                 program_application_id,
8140                 program_id,
8141                 program_update_date
8142                 )
8143         select distinct
8144                 pConfigId,
8145                 ld.organization_id,
8146                 ld.locator_id,
8147                 ld.default_type,
8148                 ld.subinventory_code,
8149                 sysdate,                     -- last_update_date
8150                 gUserId,                     -- last_updated_by
8151                 sysdate,                     -- creation_date
8152                 gUserId,                     -- created_by
8153                 gLoginId,                    -- last_update_login
8154                 NULL,                        -- request_id
8155                 NULL,                        -- program_application_id
8156                 NULL,                        -- program_id
8157                 SYSDATE                      -- program_update_date
8158         from
8159                 mtl_item_loc_defaults   ld,
8160                 bom_cto_src_orgs        bcso
8161         where   ld.organization_id      =       bcso.organization_id
8162         and     ld.inventory_item_id    =       bcso.model_item_id
8163         and     bcso.model_item_id      =       pModelId
8164         and     bcso.line_id            =       pLineId
8165         and NOT EXISTS
8166                 (select NULL
8167                 from mtl_item_loc_defaults
8168                 where inventory_item_id = pConfigId
8169                 and   organization_id = ld.organization_id);
8170 
8171 
8172 
8173         IF PG_DEBUG <> 0 THEN
8174                 oe_debug_pub.add('Create_Item: ' || 'after insert: mtl_item_loc_defaults',2);
8175         END IF;
8176 
8177         -- end 2786934
8178 
8179 	--satrt bugfix 2828588
8180 	--added for 11.5.10 , patchset-J
8181 	lStmtNumber := 310;
8182         INSERT INTO mtl_abc_assignments
8183                 (
8184                 inventory_item_id,
8185                 assignment_group_id,
8186                 abc_class_id,
8187                 last_update_date,
8188                 last_updated_by,
8189                 creation_date,
8190                 created_by
8191                 )
8192         select  pConfigId,
8193                 maa.assignment_group_id,
8194                 maa.abc_class_id,
8195 		sysdate,
8196 		gUserId,
8197 		sysdate,
8198 		gUserId
8199         FROM  mtl_abc_assignments maa
8200 	WHERE maa.inventory_item_id = pModelId
8201 	--bugfix3536085 not exists condition
8202 	AND   NOT EXISTS
8203 	      (SELECT 'X'
8204 	       FROM mtl_abc_assignments
8205 	       WHERE inventory_item_id = pConfigId
8206 	       AND   assignment_group_id = maa.assignment_group_id );
8207 
8208 
8209 
8210 
8211 
8212 
8213          IF PG_DEBUG <> 0 THEN
8214 
8215                 oe_debug_pub.add('Create_Item: ' || 'inserted '||sql%rowcount||' in mtl_abc_assignments',2);
8216 
8217                 oe_debug_pub.add('Create_Item: ' || 'after insert: mtl_abc_assignments',2);
8218         END IF;
8219 
8220 	--end bugfix 2828588
8221 
8222 	-- Begin Bugfix 9288619
8223 	/*--------------------------------------+
8224           Copy Customer Item Cross References
8225         +--------------------------------------*/
8226 
8227 	DECLARE
8228 	   CURSOR cust_id_xref_cur IS
8229 	     SELECT xref.customer_item_id, Max(xref.preference_number) pref_number
8230 	     FROM mtl_customer_item_xrefs xref
8231 	     WHERE customer_item_id IN (
8232 					 SELECT ref1.customer_item_id
8233 					 FROM mtl_customer_item_xrefs ref1
8234 					 WHERE ref1.inventory_item_id = pModelId
8235 				       )
8236 	     GROUP BY customer_item_id;
8237 
8238 	BEGIN
8239 	     FOR cust_id_xref_rec IN cust_id_xref_cur LOOP
8240 		IF PG_DEBUG <> 0 THEN
8241 		     oe_debug_pub.add('Create_Item: '||'Customer Item Id:'|| cust_id_xref_rec.customer_item_id, 5);
8242 		     oe_debug_pub.add('Create_Item: '||'Rank:'|| cust_id_xref_rec.pref_number, 5);
8243 		END IF;
8244 
8245 		INSERT
8246 		INTO   mtl_customer_item_xrefs
8247 		(
8248 			Customer_Item_Id      ,
8249 			Inventory_Item_Id     ,
8250 			Master_Organization_Id,
8251 			Preference_Number     ,
8252 			Inactive_Flag         ,
8253 			Last_Update_Date      ,
8254 			Last_Updated_By       ,
8255 			Creation_Date         ,
8256 			Created_By            ,
8257 			Last_Update_Login     ,
8258 			Attribute_Category    ,
8259 			Attribute1            ,
8260 			Attribute2            ,
8261 			Attribute3            ,
8262 			Attribute4            ,
8263 			Attribute5            ,
8264 			Attribute6            ,
8265 			Attribute7            ,
8266 			Attribute8            ,
8267 			Attribute9            ,
8268 			Attribute10           ,
8269 			Attribute11           ,
8270 			Attribute12           ,
8271 			Attribute13           ,
8272 			Attribute14           ,
8273 			Attribute15
8274 		)
8275 	        select
8276 		        cust_id_xref_rec.customer_item_id,
8277 			pConfigId                        ,
8278 			xref.Master_Organization_Id      ,
8279 			cust_id_xref_rec.pref_number + 1 ,
8280 			xref.Inactive_Flag               ,
8281 			sysdate                          ,
8282 			gUserId                          ,
8283 			sysdate                          ,
8284 			gUserId                          ,
8285 			gLoginId                         ,
8286 			xref.Attribute_Category          ,
8287 			xref.Attribute1                  ,
8288 			xref.Attribute2                  ,
8289 			xref.Attribute3                  ,
8290 			xref.Attribute4                  ,
8291 			xref.Attribute5                  ,
8292 			xref.Attribute6                  ,
8293 			xref.Attribute7                  ,
8294 			xref.Attribute8                  ,
8295 			xref.Attribute9                  ,
8296 			xref.Attribute10                 ,
8297 			xref.Attribute11                 ,
8298 			xref.Attribute12                 ,
8299 			xref.Attribute13                 ,
8300 			xref.Attribute14                 ,
8301 			xref.Attribute15
8302 		FROM   mtl_customer_item_xrefs xref
8303 		WHERE  inventory_item_id = pModelId
8304 		AND    customer_item_id  = cust_id_xref_rec.customer_item_id
8305 		AND NOT EXISTS ( SELECT 'EXISTS'
8306 				 FROM mtl_customer_item_xrefs ref1
8307 				 WHERE inventory_item_id = pConfigId
8308 				 AND customer_item_id = cust_id_xref_rec.customer_item_id
8309 			       );
8310 	     END LOOP;
8311 	END;
8312 
8313 	IF PG_DEBUG <> 0 THEN
8314 	     oe_debug_pub.add('Create_Item: ' || 'after insert: mtl_customer_item_xrefs',2);
8315         END IF;
8316 	-- End Bugfix 9288619
8317 
8318 
8319       	return(1);
8320 
8321   EXCEPTION
8322 
8323 	WHEN NO_DATA_FOUND THEN
8324 		IF PG_DEBUG <> 0 THEN
8325 			oe_debug_pub.add ('Create_Item: ' || 'ERROR: create_item_data::ndf::'||to_char(lStmtNumber)||sqlerrm,1);
8326 		END IF;
8327 		CTO_MSG_PUB.Count_And_Get(
8328                   p_msg_count => lMsgCount,
8329                   p_msg_data  => lMsgData
8330                 );
8331                 return(0);
8332 
8333 	WHEN FND_API.G_EXC_ERROR THEN
8334 		IF PG_DEBUG <> 0 THEN
8335 			oe_debug_pub.add ('Create_Item: ' || 'ERROR: create_item_data::exp error::'||to_char(lStmtNumber)||sqlerrm,1);
8336 		END IF;
8337 		CTO_MSG_PUB.Count_And_Get(
8338                   p_msg_count => lMsgCount,
8339                   p_msg_data  => lMsgData
8340                 );
8341                 return(0);
8342 
8343 	WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8344 		IF PG_DEBUG <> 0 THEN
8345 			oe_debug_pub.add ('Create_Item: ' || 'ERROR: create_item_data::unexp error::'||to_char(lStmtNumber)||sqlerrm,1);
8346 		END IF;
8347 		CTO_MSG_PUB.Count_And_Get(
8348                   p_msg_count => lMsgCount,
8349                   p_msg_data  => lMsgData
8350                 );
8351                 return(0);
8352 
8353 
8354      	WHEN OTHERS THEN
8355 		IF PG_DEBUG <> 0 THEN
8356 			oe_debug_pub.add ('Create_Item: ' || 'create_item_data::others::'||to_char(lStmtNumber)||sqlerrm,1);
8357 		END IF;
8358         	CTO_MSG_PUB.Count_And_Get(
8359                   p_msg_count => lMsgCount,
8360                   p_msg_data  => lMsgData
8361                 );
8362         	return(0);
8363 
8364 END create_item_data;
8365 
8366 
8367 FUNCTION link_item(
8368          pOrgId          in     number   ,
8369          pModelId        in     number   ,
8370          pConfigId       in     number   ,
8371          pLineId         in     number   ,
8372          xMsgCount	 out NOCOPY   number,
8373          xMsgData        out NOCOPY   varchar2)
8374 RETURN integer
8375 is
8376 
8377     lStmtNumber    	      	number;
8378     lItemName 		      	Varchar2(2000);
8379     l_model_tab               	oe_order_pub.Line_Tbl_Type;
8380     l_line_rec                	oe_order_pub.Line_Rec_type;
8381 
8382     l_msg_count               	number;
8383     l_msg_data                	varchar2(2000);
8384     l_return_status           	varchar2(1) := FND_API.G_RET_STS_SUCCESS;
8385     l_header_rec              	oe_order_pub.Header_Rec_Type;
8386     l_header_val_rec          	oe_order_pub.Header_Val_Rec_Type  ;
8387     l_Header_Adj_tbl          	oe_order_pub.Header_Adj_Tbl_Type;
8388     l_Header_Adj_val_tbl      	oe_order_pub.Header_Adj_Val_Tbl_Type;
8389     l_Header_price_Att_tbl    	oe_order_pub.Header_Price_Att_Tbl_Type;
8390     l_Header_Adj_Att_tbl      	oe_order_pub.Header_Adj_Att_Tbl_Type;
8391     l_Header_Adj_Assoc_tbl    	oe_order_pub.Header_Adj_Assoc_Tbl_Type;
8392     l_Header_Scredit_tbl      	oe_order_pub.Header_Scredit_Tbl_Type;
8393     l_Header_Scredit_val_tbl  	oe_order_pub.Header_Scredit_Val_Tbl_Type;
8394     l_line_tbl                	oe_order_pub.Line_Tbl_Type;
8395     l_line_val_tbl            	oe_order_pub.Line_Val_Tbl_Type;
8396     l_Line_Adj_tbl            	oe_order_pub.Line_Adj_Tbl_Type;
8397     l_Line_Adj_val_tbl        	oe_order_pub.Line_Adj_Val_Tbl_Type;
8398     l_Line_price_Att_tbl      	oe_order_pub.Line_Price_Att_Tbl_Type;
8399     l_Line_Adj_Att_tbl        	oe_order_pub.Line_Adj_Att_Tbl_Type;
8400     l_Line_Adj_Assoc_tbl      	oe_order_pub.Line_Adj_Assoc_Tbl_Type;
8401     l_Line_Scredit_tbl        	oe_order_pub.Line_Scredit_Tbl_Type  ;
8402     l_Line_Scredit_val_tbl    	oe_order_pub.Line_Scredit_Val_Tbl_Type;
8403     l_Lot_Serial_tbl          	oe_order_pub.Lot_Serial_Tbl_Type;
8404     l_Lot_Serial_val_tbl      	oe_order_pub.Lot_Serial_Val_Tbl_Type;
8405     l_action_request_tbl      	oe_order_pub.Request_Tbl_Type;
8406 
8407     l_config_line_id		number;
8408     l_config_id                 number;
8409     lLinkLineId			number;
8410     l_stat                      number;
8411 
8412     --Begin Bugfix 1997355
8413 
8414     index_counter_link		number;
8415     p_config_line_arr_link	MRP_OM_API_PK.line_id_tbl;
8416     l_return_status_link 	varchar2(1);
8417 
8418     --End Bugfix 1997355
8419 
8420 
8421     gUserId 			number;
8422     gLoginId 			number;
8423 
8424     l_upd_line_tbl            	oe_order_pub.Line_Tbl_Type;
8425     l_upd_line_rec             	oe_order_pub.Line_Rec_type;
8426     l_option_line_id            number;
8427     i   			number;
8428     x   			number;
8429     y   			varchar(1);
8430     z   			varchar(30);
8431     l_vdflag    		varchar(1);
8432     l_header_id                 oe_order_lines_all.header_id%type;
8433 
8434 --
8435 --bugfix 2840801
8436 --
8437     l_x_hold_result_out		Varchar2(30);
8438     l_x_hold_return_status	Varchar2(30);
8439     l_x_error_msg_count		Number;
8440     l_x_error_msg		Varchar2(2000);
8441     l_hold_source_rec		OE_Holds_PVT.Hold_Source_REC_type;
8442 
8443     v_schedule_status_code      oe_order_lines_all.schedule_status_code%type ;
8444     v_booked_flag               oe_order_lines_all.booked_flag%type ;
8445     v_hold_id                   number ;
8446 
8447     --
8448     -- bug 14474393
8449     --
8450     l_bypassatp_profile_value VARCHAR2(100);
8451 
8452 BEGIN
8453 
8454    gUserId := nvl(Fnd_Global.USER_ID,-1);
8455    gLoginId := nvl(Fnd_Global.LOGIN_ID,-1);
8456 
8457 
8458     --
8459     -- check if config item already exists
8460     --
8461     IF (CTO_MATCH_AND_RESERVE.config_line_exists(
8462 				p_model_line_id		=> pLineId,
8463                                 x_config_line_id	=> l_config_line_id,
8464                                 x_config_item_id	=> l_config_id)  = TRUE)
8465     THEN
8466             IF PG_DEBUG <> 0 THEN
8467             	oe_debug_pub.add('link_item: ' || 'Config Line Exists, do not link', 1);
8468             END IF;
8469             return(1);
8470     END IF;
8471 
8472 
8473     IF PG_DEBUG <> 0 THEN
8474     	oe_debug_pub.add ('link_item: ' || 'calling  oe_line_util.query_row , 2');
8475     END IF;
8476 
8477     Select concatenated_segments
8478     into   l_model_tab(1).ordered_item
8479     from   mtl_system_items_b_kfv
8480     where  inventory_item_id = pConfigId
8481     and    organization_id   = pOrgId;
8482 
8483     lStmtNumber    := 395;
8484     l_line_rec := oe_line_util.query_row(pLineId);
8485 
8486    /*-----------------------------------------------------------+
8487     Setting visible demand flag to 'N' for the selected model and options.
8488    +-----------------------------------------------------------*/
8489     --
8490     -- selecting all rows to be updated into l_upd_line_tbl
8491     --
8492     IF PG_DEBUG <> 0 THEN
8493     	oe_debug_pub.add ('link_item: ' || 'link : visible demand flag : before selecting options ', 2);
8494     END IF;
8495 
8496 
8497    /* BUG#2234858 Sushant added this for Drop Ship project
8498    ** Need to provide this functionality for non drop shipped items only
8499    */
8500 
8501     IF PG_DEBUG <> 0 THEN
8502     	oe_debug_pub.add('link_item: ' || 'Before visible demand flag souce type code = '||l_line_rec.source_type_code,1);
8503     END IF;
8504     if( l_line_rec.source_type_code = 'INTERNAL') then
8505 
8506 
8507 
8508     lStmtNumber    := 380;
8509 
8510     -- The following update statement is added by Renga Kannan on 04/04/2001.
8511     -- The previous process order API is replaced with this update statement for performance purpose.
8512 
8513 
8514     IF PG_DEBUG <> 0 THEN
8515     	oe_debug_pub.add('link_item: ' || 'Before updating visible demand Flag...',1);
8516 
8517     	oe_debug_pub.add('link_item: ' || ' ATO line id = '||to_char(plineid),1);
8518     END IF;
8519 
8520     UPDATE OE_ORDER_LINES_ALL
8521     SET  visible_demand_flag = 'N'
8522     WHERE ato_line_id = pLineId;
8523 
8524    IF PG_DEBUG <> 0 THEN
8525    	oe_debug_pub.add('link_item: ' || 'after updating visible demand flag..',1);
8526 
8527    	oe_debug_pub.add('link_item: ' || 'No of rows updated = '||sql%rowcount,1);
8528    END IF;
8529 
8530     -- End of adition on 04/04/2001
8531 
8532     end if ;
8533    /* BUG#2234858 Sushant added this for Drop Ship project */
8534 
8535     l_model_tab(1)                        := OE_ORDER_PUB.G_MISS_LINE_REC;
8536     l_model_tab(1).operation              := OE_Globals.G_OPR_CREATE;
8537     l_model_tab(1).inventory_item_id      := pConfigId;
8538     l_model_tab(1).item_type_code         := OE_Globals.G_ITEM_CONFIG;
8539     l_model_tab(1).ordered_quantity       := l_line_rec.ordered_quantity;
8540     l_model_tab(1).order_quantity_uom     := l_line_rec.order_quantity_uom;
8541     l_model_tab(1).ship_from_org_id       := l_line_rec.ship_from_org_id ;
8542     l_model_tab(1).org_id                 := l_line_rec.org_id ;
8543     l_model_tab(1).source_type_code       := l_line_rec.source_type_code;
8544     l_model_tab(1).request_date           := l_line_rec.request_date;
8545     l_model_tab(1).schedule_status_code   := l_line_rec.schedule_status_code;
8546     l_model_tab(1).schedule_ship_date     := l_line_rec.schedule_ship_date;
8547 
8548 
8549    /* BUG#2234858 Sushant added this for Drop Ship project */
8550     if( l_line_rec.source_type_code = 'INTERNAL' ) then
8551          --
8552          -- Bug 14474393
8553          -- The visible demand flag has to be updated only if the profile
8554          -- OM:Bypass ATP is not Y
8555          --
8556 	 FND_PROFILE.Get('ONT_BYPASS_ATP', l_bypassatp_profile_value);
8557 
8558          IF PG_DEBUG <> 0 THEN
8559               oe_debug_pub.add('link_item: Value of the profile ONT_BYPASS_ATP :' ||l_bypassatp_profile_value ,3);
8560          END IF;
8561 
8562          IF (NVL(l_bypassatp_profile_value,'N') <> 'Y') THEN
8563             l_model_tab(1).visible_demand_flag    := 'Y';
8564          ELSE  --profile is set to Yes.
8565 	    l_model_tab(1).visible_demand_flag    := 'N';
8566 	 END IF;
8567 
8568     else
8569 
8570     l_model_tab(1).visible_demand_flag    := 'N';
8571     end if ;
8572    /* BUG#2234858 Sushant added this for Drop Ship project */
8573 
8574 
8575     l_model_tab(1).header_id              := l_line_rec.header_id;
8576 /*
8577  commented as per gayatri
8578     l_model_tab(1).config_header_id       := l_line_rec.config_header_id;
8579     l_model_tab(1).config_rev_nbr         := l_line_rec.config_rev_nbr;
8580 */
8581     l_model_tab(1).model_group_number     := l_line_rec.model_group_number;
8582     l_model_tab(1).line_number            := l_line_rec.line_number;
8583     l_model_tab(1).shipment_number        := l_line_rec.shipment_number;
8584     l_model_tab(1).link_to_line_id        := l_line_rec.line_id;
8585     l_model_tab(1).ato_line_id            := l_line_rec.line_id;
8586     l_model_tab(1).top_model_line_id      := l_line_rec.top_model_line_id;
8587 					--changed to top_model_line_id from line_id to support Pto-ATO models
8588     l_model_tab(1).component_code         := l_line_rec.component_code || pConfigId;
8589     l_model_tab(1).option_flag            := 'Y';
8590     --l_model_tab(1).ordered_item           := lItemName;
8591     l_model_tab(1).item_identifier_type   := 'INT';   -- Must pass for validation
8592 
8593     l_model_tab(1).change_reason := 'SYSTEM';   -- bug 3854182 for order versioning
8594 
8595 /* bugfix 2887782 : copy the context and attribute cols from base model */
8596     l_model_tab(1).context       :=  l_line_rec.context;
8597     l_model_tab(1).attribute1    :=  l_line_rec.attribute1;
8598     l_model_tab(1).attribute2    :=  l_line_rec.attribute2;
8599     l_model_tab(1).attribute3    :=  l_line_rec.attribute3;
8600     l_model_tab(1).attribute4    :=  l_line_rec.attribute4;
8601     l_model_tab(1).attribute5    :=  l_line_rec.attribute5;
8602     l_model_tab(1).attribute6    :=  l_line_rec.attribute6;
8603     l_model_tab(1).attribute7    :=  l_line_rec.attribute7;
8604     l_model_tab(1).attribute8    :=  l_line_rec.attribute8;
8605     l_model_tab(1).attribute9    :=  l_line_rec.attribute9;
8606     l_model_tab(1).attribute10   :=  l_line_rec.attribute10;
8607     l_model_tab(1).attribute11   :=  l_line_rec.attribute11;
8608     l_model_tab(1).attribute12   :=  l_line_rec.attribute12;
8609     l_model_tab(1).attribute13   :=  l_line_rec.attribute13;
8610     l_model_tab(1).attribute14   :=  l_line_rec.attribute14;
8611     l_model_tab(1).attribute15   :=  l_line_rec.attribute15;
8612     --Bugfix 6633913: Copying these attributes to config line
8613     l_model_tab(1).attribute16   :=  l_line_rec.attribute16;
8614     l_model_tab(1).attribute17   :=  l_line_rec.attribute17;
8615     l_model_tab(1).attribute18   :=  l_line_rec.attribute18;
8616     l_model_tab(1).attribute19   :=  l_line_rec.attribute19;
8617     l_model_tab(1).attribute20   :=  l_line_rec.attribute20;
8618 
8619 /*
8620     l_model_tab(1).option_number            := '0';
8621     This line commented as per gayatri to fix bug#2300006 to handle usability issues for multiple
8622     instantiation.
8623 */
8624     l_model_tab(1).option_number := l_line_rec.option_number ; /* Fix for BUG#2300006 */
8625 
8626     l_model_tab(1).component_number            := '0';/* Fix for BUG#2300006  added later on 04-16-2002 after talking to gayatri*/
8627 
8628 
8629 
8630     l_model_tab(1).dep_plan_required_flag := l_line_rec.dep_plan_required_flag;
8631 
8632 
8633     IF PG_DEBUG <> 0 THEN
8634     	oe_debug_pub.add ('link_item: ' || 'linking  item ' || l_model_tab(1).ordered_item, 2);
8635     END IF;
8636 
8637     lStmtNumber    := 400;
8638     /*
8639     -- bugfix 2503104: Get the user_item_description information also
8640 
8641     select  nvl(component_code, oel.inventory_item_id) || '-'|| to_char(pConfigId),
8642             substrb(user_item_Description,1,240)
8643     into    l_model_tab(1).component_code,
8644             l_model_tab(1).user_item_description
8645     from    oe_order_lines_all oel
8646     where   oel.line_id = pLineId
8647     and     oel.ship_from_org_id = pOrgId;
8648     */
8649     -- bugfix 2503104: Revert this fix as OM will now populate
8650     -- user_item_description of config item. This will eliminate
8651     -- any compile time dependency between OM and CTO for this issue.
8652 
8653     select  nvl(component_code, oel.inventory_item_id) || '-'|| to_char(pConfigId)
8654     into    l_model_tab(1).component_code
8655     from    oe_order_lines_all oel
8656     where   oel.line_id = pLineId
8657     and     oel.ship_from_org_id = pOrgId;
8658 
8659     lStmtNumber    := 410;
8660 
8661     IF PG_DEBUG <> 0 THEN
8662     	oe_debug_pub.add ('link_item: ' || 'calling  oe_order_grp.Process_order', 2);
8663     END IF;
8664     -- Change for MOAC
8665     -- Changing the public process order API to group API
8666     oe_order_grp.Process_Order (
8667        p_api_version_number         =>1.0,
8668        x_return_status              => l_return_status,
8669        x_msg_count                  => l_msg_count,
8670        x_msg_data                   => l_msg_data,
8671        p_line_tbl                   => l_model_tab,
8672        x_header_rec                 => l_header_rec,
8673        x_header_val_rec             => l_header_val_rec,
8674        x_Header_Adj_tbl             => l_header_Adj_tbl,
8675        x_Header_Adj_val_tbl         => l_header_Adj_val_tbl,
8676        x_Header_price_Att_tbl       => l_header_price_att_tbl,
8677        x_Header_Adj_Att_tbl         => l_header_adj_att_tbl,
8678        x_Header_Adj_Assoc_tbl       => l_header_adj_Assoc_tbl,
8679        x_Header_Scredit_tbl         => l_header_Scredit_tbl,
8680        x_Header_Scredit_val_tbl     => l_header_scredit_val_tbl,
8681        x_line_tbl                   => l_line_tbl,
8682        x_line_val_tbl               => l_line_val_tbl,
8683        x_Line_Adj_tbl               => l_line_adj_tbl,
8684        x_Line_Adj_val_tbl           => l_line_adj_val_tbl,
8685        x_Line_price_Att_tbl         => l_line_price_Att_tbl,
8686        x_Line_Adj_Att_tbl           => l_line_adj_att_tbl,
8687        x_Line_Adj_Assoc_tbl         => l_line_adj_Assoc_tbl,
8688        x_Line_Scredit_tbl           => l_line_scredit_tbl,
8689        x_Line_Scredit_val_tbl       => l_line_scredit_val_tbl,
8690        x_Lot_Serial_tbl             => l_lot_serial_tbl,
8691        x_Lot_Serial_val_tbl         => l_lot_serial_val_tbl,
8692        x_action_request_tbl         => l_action_request_tbl
8693     );
8694 
8695     IF PG_DEBUG <> 0 THEN
8696     	oe_debug_pub.add ('link_item: ' || 'Process_order returned ' || l_return_status, 1);
8697 
8698     	oe_debug_pub.add ('link_item: ' || 'Process_order returned ' || l_msg_data, 2);
8699     END IF;
8700 
8701     if (l_return_status = FND_API.G_RET_STS_ERROR) then
8702 	IF PG_DEBUG <> 0 THEN
8703 		oe_debug_pub.add ('link_item: ' || 'Process_order returned expected error :'||l_msg_data,1);
8704 	END IF;
8705 	raise FND_API.G_EXC_ERROR;
8706 
8707     elsif (l_return_status = FND_API.G_RET_STS_ERROR) then
8708 	IF PG_DEBUG <> 0 THEN
8709 		oe_debug_pub.add ('link_item: ' || 'Process_order returned unexpected error :'||l_msg_data,1);
8710 	END IF;
8711 	raise FND_API.G_EXC_UNEXPECTED_ERROR;
8712 
8713     end if;
8714 
8715     oe_globals.g_cascading_request_logged := TRUE;
8716 
8717 
8718 
8719    /* BUG#2234858 Sushant added this for Drop Ship project */
8720     if( l_line_rec.source_type_code = 'INTERNAL' ) then
8721 
8722 
8723 
8724 
8725     lStmtNumber    := 420;
8726     l_stat := chk_model_in_bcod(pLineId);
8727 
8728    --set demand_visible flag in BCOD to 'N' so that only CID demand is visible to
8729    --planning and not of BOM
8730 
8731     if(l_stat = 1)then
8732 
8733 
8734 
8735 
8736 
8737       /* Not Required for Patcshet J */
8738       /*
8739       lStmtNumber    := 430;
8740       update bom_cto_order_demand
8741       set demand_visible = 'N'
8742       where ato_line_id =pLineId;
8743 
8744       --adding the CID row to BCOD table
8745 
8746       lStmtNumber    := 440;
8747       insert into bom_cto_order_demand(
8748         bcod_line_id,
8749         oe_line_id,
8750         ato_line_id,
8751         inventory_item_id,
8752         organization_id,
8753         required_date,
8754         required_qty,
8755         order_quantity_uom,
8756         parent_demand_type,
8757         header_id,
8758         forecast_visible,
8759         demand_visible,
8760         created_by,
8761         last_updated_by,
8762         last_updated_date,
8763         last_update_login,
8764         program_application_id,
8765         program_update_date
8766        )
8767       select
8768          BOM_CTO_ORDER_DEMAND_S1.nextval,
8769          line_id,
8770          pLineId,
8771          pConfigId,
8772          pOrgId,
8773          schedule_ship_date,
8774          ordered_quantity,
8775          order_quantity_uom,
8776          1,
8777          header_id,
8778          'N',
8779          'Y',
8780          gUserId,
8781          gUserId,
8782          sysdate,
8783          gLoginId,
8784          null,
8785          sysdate
8786       from oe_order_lines_all
8787       where ato_line_id=pLineId
8788       and   inventory_item_id=pConfigId;
8789 
8790       */
8791 
8792       null ;
8793 
8794 
8795   elsif(l_stat =0) then
8796    -- do nothing
8797      IF PG_DEBUG <> 0 THEN
8798      	oe_debug_pub.add ('link_item: ' || 'no model line present in bcod table to create a CID,not an error', 3);
8799      END IF;
8800 
8801   end if;
8802 
8803 
8804 
8805   end if ;
8806    /* BUG#2234858 Sushant added this for Drop Ship project */
8807 
8808 
8809 
8810     --Begin Bugfix 1997355
8811 
8812     lStmtNumber    := 450;
8813     select line_id, header_id	-- bugfix 2840801 : added header_id
8814     into   l_config_line_id, l_header_id
8815     from   oe_order_lines_all oel
8816     where  ato_line_id = pLineId
8817     and    inventory_item_id = pConfigId
8818     and    item_type_code    = 'CONFIG';
8819 
8820     index_counter_link:= 1;
8821     p_config_line_arr_link(index_counter_link) := l_config_line_id ;
8822 
8823     IF PG_DEBUG <> 0 THEN
8824     	oe_debug_pub.add('link_item: ' || 'Start MRP package for link', 1);
8825     END IF;
8826 
8827     MRP_OM_API_PK.MRP_OM_Interface
8828 		(p_line_tbl		=> p_config_line_arr_link,
8829 		 x_return_status	=> l_return_status_link);
8830 
8831     IF PG_DEBUG <> 0 THEN
8832     	oe_debug_pub.add ('link_item: ' || 'Return status from MRP_OM_Interface - Link: '||l_return_status_link,2);
8833     END IF;
8834 
8835     if l_return_status_link = FND_API.G_RET_STS_ERROR then
8836      		IF PG_DEBUG <> 0 THEN
8837      			oe_debug_pub.add ('link_item: ' || 'Failed in MRP_OM_API_PK.mrp_om_interface with expected error.', 1);
8838      		END IF;
8839 		raise FND_API.G_EXC_ERROR;
8840 
8841     elsif l_return_status_link = FND_API.G_RET_STS_UNEXP_ERROR then
8842      		IF PG_DEBUG <> 0 THEN
8843      			oe_debug_pub.add ('link_item: ' || 'Failed in MRP_OM_API_PK.mrp_om_interface with unexpected error.', 1);
8844      		END IF;
8845 		raise FND_API.G_EXC_UNEXPECTED_ERROR;
8846     end if;
8847 
8848     -- End Bugfix 1997355
8849 
8850     --
8851     -- Begin bugfix 2840801
8852     -- If any optional components are dropped while creating bill for config item,
8853     -- we will put a hold on the config-line.
8854     -- The global variable gApplyHold is set in CTOCBOMB.pls
8855     --
8856 
8857     if (CTO_CONFIG_BOM_PK.gApplyHold = 'Y') then
8858      	IF PG_DEBUG <> 0 THEN
8859 	   oe_debug_pub.add ('CTOCITMB:Need to put hold on config line '||l_config_line_id);
8860 	END IF;
8861 
8862 	/* Put the config line on Hold. But first, check to see if a hold already exists
8863            on this line  */
8864 
8865 	OE_HOLDS_PUB.Check_Holds (
8866 		 p_api_version 		=> 1.0
8867 		,p_line_id 		=> l_config_line_id
8868 		,x_result_out 		=> l_x_hold_result_out
8869 		,x_return_status 	=> l_x_hold_return_status
8870 		,x_msg_count 		=> l_x_error_msg_count
8871 		,x_msg_data 		=> l_x_error_msg);
8872 
8873         IF (l_x_hold_return_status = FND_API.G_RET_STS_ERROR) THEN
8874      		IF PG_DEBUG <> 0 THEN
8875                    oe_debug_pub.add('CTOCITMB:Failed in Check Holds with expected error.' ,1);
8876 		END IF;
8877                 raise FND_API.G_EXC_ERROR;
8878 
8879         ELSIF (l_x_hold_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
8880      		IF PG_DEBUG <> 0 THEN
8881                    oe_debug_pub.add('CTOCITMB:Failed in Check Holds with unexpected error.' ,1);
8882 		END IF;
8883                 raise FND_API.G_EXC_UNEXPECTED_ERROR;
8884 
8885         ELSE
8886      		IF PG_DEBUG <> 0 THEN
8887                    oe_debug_pub.add('CTOCITMB:Success in Check Holds.' ,1);
8888      		END IF;
8889 		if l_x_hold_result_out = FND_API.G_FALSE then
8890 
8891 
8892 
8893                    select schedule_status_code , booked_flag into v_schedule_Status_code , v_booked_flag
8894                      from oe_order_lines_all
8895                     where line_id = pLineId ;
8896 
8897 
8898 
8899                     if( v_schedule_status_code = 'SCHEDULED' and v_booked_flag = 'Y' ) then
8900 
8901                         v_hold_id := 55 ;
8902 
8903      		        IF PG_DEBUG <> 0 THEN
8904                            oe_debug_pub.add('CTOCITMB: Going to apply normal hold .' ,1);
8905      		        END IF;
8906 
8907                     else
8908 
8909                         v_hold_id :=  61 ;    /* New Hold Id for Create Supply Activity Hold , Should be changed from 1063 to 61 after Gayatri gives the script*/
8910 
8911      		        IF PG_DEBUG <> 0 THEN
8912                            oe_debug_pub.add('CTOCITMB: Going to apply create supply activity hold .' ,1);
8913      		        END IF;
8914 
8915                     end if;
8916 
8917 
8918 
8919 
8920   		    IF PG_DEBUG <> 0 THEN
8921                        oe_debug_pub.add('CTOCITMB:Calling OM api to apply hold.' ,1);
8922      		    END IF;
8923 
8924 		    l_hold_source_rec.hold_entity_code   := 'O';
8925                     l_hold_source_rec.hold_id            := v_hold_id ;
8926                     l_hold_source_rec.hold_entity_id     := l_header_id;
8927                     l_hold_source_rec.header_id          := l_header_id;
8928                     l_hold_source_rec.line_id            := l_config_line_id;
8929 
8930 		    OE_Holds_PUB.Apply_Holds (
8931 				   p_api_version        => 1.0
8932                                ,   p_hold_source_rec    => l_hold_source_rec
8933                                ,   x_return_status      => l_return_status
8934                                ,   x_msg_count          => l_msg_count
8935                                ,   x_msg_data           => l_msg_data);
8936 
8937         	    IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
8938      		        IF PG_DEBUG <> 0 THEN
8939                 	   oe_debug_pub.add('CTOCITMB:Failed in Apply Holds with expected error.' ,1);
8940      		        END IF;
8941                 	raise FND_API.G_EXC_ERROR;
8942 
8943         	    ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
8944      		        IF PG_DEBUG <> 0 THEN
8945                 	   oe_debug_pub.add('CTOCITMB:Failed in Apply Holds with unexpected error.' ,1);
8946      		        END IF;
8947                 	raise FND_API.G_EXC_UNEXPECTED_ERROR;
8948 		    END IF;
8949 
8950      		    IF PG_DEBUG <> 0 THEN
8951                        oe_debug_pub.add('CTOCITMB: An Exception Hold applied to config line.' ,1);
8952      		    END IF;
8953     		    cto_msg_pub.cto_message('BOM','CTO_ORDER_LINE_EXCPN_HOLD');
8954 
8955 		else
8956      		    IF PG_DEBUG <> 0 THEN
8957                        oe_debug_pub.add('CTOCITMB: Line is already on Hold.' ,1);
8958      		    END IF;
8959 		end if;
8960         END IF;
8961     end if;
8962     --
8963     -- End bugfix 2840801
8964     --
8965 
8966 
8967     return (1);
8968 
8969     EXCEPTION
8970 
8971 	WHEN NO_DATA_FOUND THEN
8972 		IF PG_DEBUG <> 0 THEN
8973 			oe_debug_pub.add ('link_item: ' || 'ERROR: link_item::ndf::'||to_char(lStmtNumber)||'::'||sqlerrm, 1);
8974 		END IF;
8975 		CTO_MSG_PUB.Count_And_Get(
8976                   p_msg_count => l_Msg_Count,
8977                   p_msg_data  => l_Msg_Data
8978                 );
8979 		return(0);
8980 
8981 	WHEN FND_API.G_EXC_ERROR THEN
8982 		IF PG_DEBUG <> 0 THEN
8983 			oe_debug_pub.add ('link_item: ' || 'ERROR: link_item::exp error::'||to_char(lStmtNumber)||sqlerrm, 1);
8984 		END IF;
8985 		CTO_MSG_PUB.Count_And_Get(
8986                   p_msg_count => l_Msg_Count,
8987                   p_msg_data  => l_Msg_Data
8988                 );
8989                 return(0);
8990 
8991 	WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8992 		IF PG_DEBUG <> 0 THEN
8993 			oe_debug_pub.add ('link_item: ' || 'ERROR: link_item::unexp error::'||to_char(lStmtNumber)||sqlerrm, 1);
8994 		END IF;
8995 		CTO_MSG_PUB.Count_And_Get(
8996                   p_msg_count => l_Msg_Count,
8997                   p_msg_data  => l_Msg_Data
8998                 );
8999                 return(0);
9000 
9001         WHEN OTHERS THEN
9002 		IF PG_DEBUG <> 0 THEN
9003 			oe_debug_pub.add ('link_item: ' || 'ERROR: link_item::others::'||to_char(lStmtNumber)||'::'||sqlerrm, 1);
9004 		END IF;
9005 		CTO_MSG_PUB.Count_And_Get(
9006                   p_msg_count => l_Msg_Count,
9007                   p_msg_data  => l_Msg_Data
9008                 );
9009                 return(0);
9010  END link_item;
9011 
9012 /*****************************************************************************
9013 chk_model_in_bcod
9014 bcod implies BOM CTO ORDER DEMAND
9015  checks if model is there in BOM CTO ORDER DEMAND table before the creation of row for configured item in the BOM CTO ORDER DEMAND table. This function is being called from link_item()function
9016 *****************************************************************************/
9017 
9018 FUNCTION chk_model_in_bcod(
9019                             pLineId in number)
9020 RETURN integer
9021 is
9022   l_dummy number;
9023   BEGIN
9024        select oe_line_id into l_dummy
9025        from bom_cto_order_demand
9026        where oe_line_id=pLineId;
9027        return (1);
9028   EXCEPTION
9029    when no_data_found then
9030       return (0);
9031 END chk_model_in_bcod;
9032 
9033 
9034 --
9035 -- bugfix 2706981: Created a new API for performance reasons.
9036 -- This API would check if a duplicate item name exists.
9037 -- It returns 1 if duplicate exists. Else, it returns 0.
9038 --
9039 
9040 FUNCTION check_dup_item(
9041     pSegment1 	varchar2,
9042     pSegment2 	varchar2,
9043     pSegment3 	varchar2,
9044     pSegment4 	varchar2,
9045     pSegment5 	varchar2,
9046     pSegment6 	varchar2,
9047     pSegment7 	varchar2,
9048     pSegment8 	varchar2,
9049     pSegment9 	varchar2,
9050     pSegment10 	varchar2,
9051     pSegment11 	varchar2,
9052     pSegment12 	varchar2,
9053     pSegment13 	varchar2,
9054     pSegment14 	varchar2,
9055     pSegment15 	varchar2,
9056     pSegment16 	varchar2,
9057     pSegment17 	varchar2,
9058     pSegment18 	varchar2,
9059     pSegment19 	varchar2,
9060     pSegment20 	varchar2)
9061 RETURN number is
9062 
9063     sql_str		varchar2(2000);
9064     lcursor		integer;
9065     xdummy      	number;
9066     rows_processed 	number;
9067 
9068 BEGIN
9069 
9070     IF PG_DEBUG <> 0 THEN
9071        oe_debug_pub.add( 'Check_dup_item: '|| 'Checking for duplicate item name.. ' ,2 );
9072     END IF;
9073 
9074     sql_str := 'select 1 from mtl_system_items msi where 1=1 ';
9075 
9076     if pSegment1 is not null then
9077 	sql_str := sql_str||' and msi.segment1 = :pSegment1';
9078     end if;
9079 
9080     if pSegment2 is not null then
9081 	sql_str := sql_str||' and msi.segment2 = :pSegment2';
9082     end if;
9083 
9084     if pSegment3 is not null then
9085 	sql_str := sql_str||' and msi.segment3 = :pSegment3';
9086     end if;
9087 
9088     if pSegment4 is not null then
9089 	sql_str := sql_str||' and msi.segment4 = :pSegment4';
9090     end if;
9091 
9092     if pSegment5 is not null then
9093 	sql_str := sql_str||' and msi.segment5 = :pSegment5';
9094     end if;
9095 
9096     if pSegment6 is not null then
9097 	sql_str := sql_str||' and msi.segment6 = :pSegment6';
9098     end if;
9099 
9100     if pSegment7 is not null then
9101 	sql_str := sql_str||' and msi.segment7 = :pSegment7';
9102     end if;
9103 
9104     if pSegment8 is not null then
9105 	sql_str := sql_str||' and msi.segment8 = :pSegment8';
9106     end if;
9107 
9108     if pSegment9 is not null then
9109 	sql_str := sql_str||' and msi.segment9 = :pSegment9';
9110     end if;
9111 
9112     if pSegment10 is not null then
9113 	sql_str := sql_str||' and msi.segment10 = :pSegment10';
9114     end if;
9115 
9116     if pSegment11 is not null then
9117 	sql_str := sql_str||' and msi.segment11 = :pSegment11';
9118     end if;
9119 
9120     if pSegment12 is not null then
9121 	sql_str := sql_str||' and msi.segment12 = :pSegment12';
9122     end if;
9123 
9124     if pSegment13 is not null then
9125 	sql_str := sql_str||' and msi.segment13 = :pSegment13';
9126     end if;
9127 
9128     if pSegment14 is not null then
9129 	sql_str := sql_str||' and msi.segment14 = :pSegment14';
9130     end if;
9131 
9132     if pSegment15 is not null then
9133 	sql_str := sql_str||' and msi.segment15 = :pSegment15';
9134     end if;
9135 
9136     if pSegment16 is not null then
9137 	sql_str := sql_str||' and msi.segment16 = :pSegment16';
9138     end if;
9139 
9140     if pSegment17 is not null then
9141 	sql_str := sql_str||' and msi.segment17 = :pSegment17';
9142     end if;
9143 
9144     if pSegment18 is not null then
9145 	sql_str := sql_str||' and msi.segment18 = :pSegment18';
9146     end if;
9147 
9148     if pSegment19 is not null then
9149 	sql_str := sql_str||' and msi.segment19 = :pSegment19';
9150     end if;
9151 
9152     if pSegment20 is not null then
9153 	sql_str := sql_str||' and msi.segment20 = :pSegment20';
9154     end if;
9155 
9156 
9157     IF PG_DEBUG <> 0 THEN
9158         oe_debug_pub.add( 'Check_dup_item: '||'sql_str : '||sql_str, 3);
9159     END IF;
9160 
9161     lcursor := dbms_sql.open_cursor;
9162 
9163     dbms_sql.parse(lcursor, sql_str, dbms_sql.native);
9164 
9165     if pSegment1 is not null then
9166         dbms_sql.bind_variable(lcursor,'pSegment1', pSegment1);
9167     end if;
9168     if pSegment2 is not null then
9169         dbms_sql.bind_variable(lcursor,'pSegment2', pSegment2);
9170     end if;
9171     if pSegment3 is not null then
9172         dbms_sql.bind_variable(lcursor,'pSegment3', pSegment3);
9173     end if;
9174     if pSegment4 is not null then
9175         dbms_sql.bind_variable(lcursor,'pSegment4', pSegment4);
9176     end if;
9177     if pSegment5 is not null then
9178         dbms_sql.bind_variable(lcursor,'pSegment5', pSegment5);
9179     end if;
9180     if pSegment6 is not null then
9181         dbms_sql.bind_variable(lcursor,'pSegment6', pSegment6);
9182     end if;
9183     if pSegment7 is not null then
9184         dbms_sql.bind_variable(lcursor,'pSegment7', pSegment7);
9185     end if;
9186     if pSegment8 is not null then
9187         dbms_sql.bind_variable(lcursor,'pSegment8', pSegment8);
9188     end if;
9189     if pSegment9 is not null then
9190         dbms_sql.bind_variable(lcursor,'pSegment9', pSegment9);
9191     end if;
9192     if pSegment10 is not null then
9193         dbms_sql.bind_variable(lcursor,'pSegment10', pSegment10);
9194     end if;
9195     if pSegment11 is not null then
9196         dbms_sql.bind_variable(lcursor,'pSegment11', pSegment11);
9197     end if;
9198     if pSegment12 is not null then
9199         dbms_sql.bind_variable(lcursor,'pSegment12', pSegment12);
9200     end if;
9201     if pSegment13 is not null then
9202         dbms_sql.bind_variable(lcursor,'pSegment13', pSegment13);
9203     end if;
9204     if pSegment14 is not null then
9205         dbms_sql.bind_variable(lcursor,'pSegment14', pSegment14);
9206     end if;
9207     if pSegment15 is not null then
9208         dbms_sql.bind_variable(lcursor,'pSegment15', pSegment15);
9209     end if;
9210     if pSegment16 is not null then
9211         dbms_sql.bind_variable(lcursor,'pSegment16', pSegment16);
9212     end if;
9213     if pSegment17 is not null then
9214         dbms_sql.bind_variable(lcursor,'pSegment17', pSegment17);
9215     end if;
9216     if pSegment18 is not null then
9217         dbms_sql.bind_variable(lcursor,'pSegment18', pSegment18);
9218     end if;
9219     if pSegment19 is not null then
9220         dbms_sql.bind_variable(lcursor,'pSegment19', pSegment19);
9221     end if;
9222     if pSegment20 is not null then
9223         dbms_sql.bind_variable(lcursor,'pSegment20', pSegment20);
9224     end if;
9225 
9226     dbms_sql.define_column(lcursor, 1, xdummy);
9227 
9228     rows_processed := dbms_sql.execute_and_fetch(lcursor);
9229 
9230     dbms_sql.close_cursor(lcursor);
9231 
9232     if rows_processed > 0 then
9233         IF PG_DEBUG <> 0 THEN
9234            oe_debug_pub.add( 'Check_dup_item: '||'Duplicate item name found.');
9235 	END IF;
9236 	return 1;
9237     else
9238         IF PG_DEBUG <> 0 THEN
9239            oe_debug_pub.add( 'Check_dup_item: '||'Duplicate item name NOT found.');
9240         END IF;
9241 	return 0;
9242     end if;
9243 
9244 END check_dup_item;
9245 
9246 
9247 --
9248 -- bugfix 3026929: local API for dynamic insert.
9249 --
9250 
9251 PROCEDURE populate_item_revision(pConfigId	number,
9252 				 pModelId	number,
9253 				 pLineId	number,
9254 				 xReturnStatus	OUT NOCOPY varchar2) is
9255 
9256 	--sql_stmt 	varchar2(4000);  Bugfix 6063990
9257 	stmtnum		number;
9258 	x_item_rev_seq	number ;
9259 
9260 BEGIN
9261 	xReturnStatus	:= fnd_api.G_RET_STS_SUCCESS;
9262 
9263 	stmtnum := 1;
9264 
9265 	IF PG_DEBUG <> 0 THEN    --Bugfix 6063990
9266                 oe_debug_pub.add('Inside populate_item_revision');
9267 	END IF;
9268 	--sql_stmt := 'SELECT MTL_ITEM_REVISIONS_B_S.nextval FROM dual';
9269 
9270 	--IF PG_DEBUG <> 0 THEN
9271          --       oe_debug_pub.add(sql_stmt,3);
9272 	--END IF;
9273 
9274 	--EXECUTE IMMEDIATE sql_stmt INTO x_item_rev_seq ;
9275 
9276 	--IF PG_DEBUG <> 0 THEN
9277             --    oe_debug_pub.add(' x_item_rev_seq = '|| x_item_rev_seq ,1);
9278 
9279       --  END IF;
9280 
9281 --3340844 joing to mp2 and distinct has been removed
9282 	stmtnum := 2;
9283 	/*Bugfix 5851244: As Release 11.5.10 was shipped as one SCM patch,
9284           we are assuming that customers will always be on ITEMS 11.5.10 data model i.e. they will have tables _B and _TL.
9285           So changing dynamic sql into static sql */
9286 
9287 	--sql_stmt :=
9288 	 insert into mtl_item_revisions_b
9289               (inventory_item_id,
9290                organization_id,
9291                revision,
9292                last_update_date,
9293                last_updated_by,
9294                creation_date,
9295                created_by,
9296                last_update_login,
9297                implementation_date,
9298                effectivity_date,
9299 	       OBJECT_VERSION_NUMBER,
9300 	       REVISION_ID,
9301 	       REVISION_LABEL --3340844
9302               )
9303         select -- distinct
9304                	pConfigId,		      --Bugfix 5851244: Removing bind variables to make sql static
9305                	mp1.organization_id,
9306                 mp1.starting_revision,
9307                 sysdate,
9308                 gUserId,                     -- last_updated_by
9309                 sysdate,
9310                 gUserId,                     -- created_by
9311                 gLoginId,                    -- last_update_login
9312                 sysdate,
9313                 sysdate,
9314 	        1,                           --would be 1 for initial creation of item
9315 	         MTL_ITEM_REVISIONS_B_S.nextval, -- 3338108 --:x_item_rev_seq --revision_id is generated from sequence
9316                	mp1.starting_revision --3340844
9317          from
9318                mtl_parameters mp1,
9319                mtl_system_items m
9320         where  m.inventory_item_id = pConfigId
9321           and  m.organization_id = mp1.organization_id
9322           and NOT EXISTS
9323                 (select NULL
9324                 from MTL_ITEM_REVISIONS_B
9325                 where inventory_item_id = pConfigId
9326                 and organization_id = mp1.organization_id);
9327 
9328 
9329 
9330 	/*IF PG_DEBUG <> 0 THEN
9331                 oe_debug_pub.add(sql_stmt,3);
9332 	END IF;*/   --Bugfix 6063990
9333 
9334 	--EXECUTE IMMEDIATE sql_stmt USING pConfigId, gUserId, gUserId, gLoginId, x_item_rev_seq, pModelId, pLineId, pConfigId;
9335 	--EXECUTE IMMEDIATE sql_stmt USING pConfigId, gUserId, gUserId, gLoginId,pModelId, pLineId, pConfigId;
9336 	--EXECUTE IMMEDIATE sql_stmt USING pConfigId, gUserId, gUserId, gLoginId,pConfigId,pConfigId;      Bugfix 6063990
9337 
9338 	IF PG_DEBUG <> 0 THEN
9339                 oe_debug_pub.add('Inserted into mtl_item_revisions_b for item ' || pConfigId || ' rows ' || SQL%ROWCOUNT );
9340 	END IF;
9341 
9342 
9343 
9344         --insert into _tl table so that item is visible in revisions form
9345 	--for multi-lingual support
9346 
9347 	stmtnum := 3;
9348 
9349 
9350 --sql_stmt :=
9351 	insert into mtl_item_revisions_tl (
9352                 inventory_item_id,
9353                 organization_id,
9354 		revision_id,
9355                 language,
9356                 source_lang,
9357                 description,
9358                 last_update_date,
9359                 last_updated_by,
9360                 creation_date,
9361                 created_by,
9362                 last_update_login
9363                 )
9364         select  distinct pConfigId,                   --Bugfix 6063990: Removing bind variables to make sql static
9365                 mp1.organization_id,
9366 		mr.revision_id,
9367                 l.language_code,
9368                 userenv('LANG'),
9369                 m.description,
9370                 sysdate,
9371                 gUserId,                              --last_updated_by
9372                 sysdate,
9373                 gUserId,                              --created_by
9374                 gLoginId                              --last_update_login
9375         from
9376                 mtl_parameters mp1,
9377                 mtl_system_items_tl m,
9378                 bom_cto_src_orgs bcso,
9379                 fnd_languages  l,
9380 		mtl_item_revisions_b mr, --3338108
9381                 mtl_parameters mp2  --4109427
9382         where  m.inventory_item_id = pModelId
9383         and bcso.model_item_id = m.inventory_item_id
9384         and bcso.line_id = pLineId
9385         and m.organization_id   = mp1.organization_id
9386         and mp2.organization_id = bcso.organization_id   --4109427
9387 	and ((mp1.organization_id = bcso.organization_id) --4109427
9388              or (mp1.organization_id = mp2.master_organization_id))  --4109427
9389         and  l.installed_flag In ('I', 'B')
9390         and  l.language_code  = m.language
9391 	and  mr.inventory_item_id = pConfigId  --3338108
9392 	and  mr.organization_id =  mp1.organization_id --3338108
9393         and  NOT EXISTS
9394                 (select NULL
9395                 from  mtl_item_revisions_tl  t
9396                 where  t.inventory_item_id = pConfigId
9397                 and  t.organization_id = mp1.organization_id
9398                 and  t.revision_id = mr.revision_id   --3338108
9399                 and  t.language = l.language_code );
9400 
9401 
9402 
9403 	/*IF PG_DEBUG <> 0 THEN
9404                 oe_debug_pub.add(sql_stmt,3);
9405 	END IF;*/   --Bugfix 6063990
9406 
9407 	--EXECUTE IMMEDIATE sql_stmt USING pConfigId, x_item_rev_seq, gUserId, gUserId, gLoginId, pModelId, pLineId, pConfigId, x_item_rev_seq;
9408 
9409 	--EXECUTE IMMEDIATE sql_stmt USING pConfigId,gUserId, gUserId, gLoginId, pModelId, pLineId, pConfigId,pConfigId;   Bugfix 6063990
9410 
9411 	IF PG_DEBUG <> 0 THEN
9412                 oe_debug_pub.add('Inserted into mtl_system_items_tl.');
9413 	END IF;
9414 
9415 EXCEPTION
9416 	when OTHERS then
9417 		oe_debug_pub.add ('Failed in dynamic query : '||sqlerrm);
9418 		xReturnStatus := fnd_api.G_RET_STS_UNEXP_ERROR;
9419 
9420 END populate_item_revision;
9421 
9422 
9423 
9424 
9425 
9426 procedure create_preconfigured_item( p_line_id in number
9427                          , p_model_id       in number
9428                          , p_config_id       in number
9429                          , p_lItemtype       in varchar2 )
9430 is
9431 
9432 	l_ind_cnt number;  --Bugfix 8305535
9433 	sqlcnt1   number;  --Bugfix 8305535
9434 
9435 BEGIN
9436 
9437        oe_debug_pub.add( 'Entered Create Preconfigured Item ' , 1 ) ;
9438 
9439 
9440        select substr( attribute_name, instr( attribute_name, '.' )+ 1 ) , control_level
9441        BULK COLLECT
9442        INTO g_attribute_name_tab, g_control_level_tab
9443        from mtl_item_attributes
9444        where control_level = 1 ;
9445 
9446 
9447       if( get_attribute_control( 'atp_flag' ) = 1 ) then
9448           oe_debug_pub.add( 'ATP flag is master controlled ' , 1 );
9449       else
9450           oe_debug_pub.add( 'ATP flag is org controlled ' , 1 );
9451 
9452       end if;
9453 
9454       if( get_attribute_control( 'atp_components_flag' ) = 1 ) then
9455           oe_debug_pub.add( 'ATP components flag is master controlled ' , 1 );
9456       else
9457           oe_debug_pub.add( 'ATP components flag is org controlled ' , 1 );
9458 
9459       end if;
9460 
9461 
9462       if( get_attribute_control( 'market_price' ) = 1 ) then
9463           oe_debug_pub.add( 'market_price is master controlled ' , 1 );
9464       else
9465           oe_debug_pub.add( 'market_price flag is org controlled ' , 1 );
9466 
9467       end if;
9468 
9469 
9470 
9471 
9472        oe_debug_pub.add( 'Entered Create Preconfigured Item ' , 1 ) ;
9473 
9474         insert into mtl_system_items_b
9475                 (inventory_item_id,
9476                 organization_id,
9477                 last_update_date,
9478                 last_updated_by,
9479                 creation_date,
9480                 created_by,
9481                 last_update_login,
9482                 summary_flag,
9483                 enabled_flag,
9484                 start_date_active,
9485                 end_date_active,
9486                 description,
9487                 buyer_id,
9488                 accounting_rule_id,
9489                 invoicing_rule_id,
9490                 segment1,
9491                 segment2,
9492                 segment3,
9493                 segment4,
9494                 segment5,
9495                 segment6,
9496                 segment7,
9497                 segment8,
9498                 segment9,
9499                 segment10,
9500                 segment11,
9501                 segment12,
9502                 segment13,
9503                 segment14,
9504                 segment15,
9505                 segment16,
9506                 segment17,
9507                 segment18,
9508                 segment19,
9509                 segment20,
9510                 attribute_category,
9511                 attribute1,
9512                 attribute2,
9513                 attribute3,
9514                 attribute4,
9515                 attribute5,
9516                 attribute6,
9517                 attribute7,
9518                 attribute8,
9519                 attribute9,
9520                 attribute10,
9521                 attribute11,
9522                 attribute12,
9523                 attribute13,
9524                 attribute14,
9525                 attribute15,
9526                 attribute16,  -- Bug 9223457
9527                 attribute17,
9528                 attribute18,
9529                 attribute19,
9530                 attribute20,
9531                 attribute21,
9532                 attribute22,
9533                 attribute23,
9534                 attribute24,
9535                 attribute25,
9536                 attribute26,
9537                 attribute27,
9538                 attribute28,
9539                 attribute29,
9540                 attribute30,
9541                 purchasing_item_flag,
9542                 shippable_item_flag,
9543                 customer_order_flag,
9544                 internal_order_flag,
9545                 service_item_flag,
9546                 inventory_item_flag,
9547                 eng_item_flag,
9548                 inventory_asset_flag,
9549                 purchasing_enabled_flag,
9550                 customer_order_enabled_flag,
9551                 internal_order_enabled_flag,
9552                 so_transactions_flag,
9553                 mtl_transactions_enabled_flag,
9554                 stock_enabled_flag,
9555                 bom_enabled_flag,
9556                 build_in_wip_flag,
9557                 revision_qty_control_code,
9558                 item_catalog_group_id,
9559                 catalog_status_flag,
9560                 returnable_flag,
9561                 default_shipping_org,
9562                 collateral_flag,
9563                 taxable_flag,
9564                 allow_item_desc_update_flag,
9565                 inspection_required_flag,
9566                 receipt_required_flag,
9567                 market_price,
9568                 hazard_class_id,
9569                 rfq_required_flag,
9570                 qty_rcv_tolerance,
9571                 un_number_id,
9572                 price_tolerance_percent,
9573                 asset_category_id,
9574                 rounding_factor,
9575                 unit_of_issue,
9576                 enforce_ship_to_location_code,
9577                 allow_substitute_receipts_flag,
9578                 allow_unordered_receipts_flag,
9579                 allow_express_delivery_flag,
9580                 days_early_receipt_allowed,
9581                 days_late_receipt_allowed,
9582                 receipt_days_exception_code,
9583                 receiving_routing_id,
9584                 invoice_close_tolerance,
9585                 receive_close_tolerance,
9586                 auto_lot_alpha_prefix,
9587                 start_auto_lot_number,
9588                 lot_control_code,
9589                 shelf_life_code,
9590                 shelf_life_days,
9591                 serial_number_control_code,
9592                 start_auto_serial_number,
9593                 auto_serial_alpha_prefix,
9594                 source_type,
9595                 source_organization_id,
9596                 source_subinventory,
9597                 expense_account,
9598                 encumbrance_account,
9599                 restrict_subinventories_code,
9600                 unit_weight,
9601                 weight_uom_code,
9602                 volume_uom_code,
9603                 unit_volume,
9604                 restrict_locators_code,
9605                 location_control_code,
9606                 shrinkage_rate,
9607                 acceptable_early_days,
9608                 planning_time_fence_code,
9609                 demand_time_fence_code,
9610                 lead_time_lot_size,
9611                 std_lot_size,
9612                 cum_manufacturing_lead_time,
9613                 overrun_percentage,
9614                 acceptable_rate_increase,
9615                 acceptable_rate_decrease,
9616                 cumulative_total_lead_time,
9617                 planning_time_fence_days,
9618                 demand_time_fence_days,
9619                 end_assembly_pegging_flag,
9620                 planning_exception_set,
9621                 bom_item_type,
9622                 pick_components_flag,
9623                 replenish_to_order_flag,
9624                 base_item_id,
9625                 atp_components_flag,
9626                 atp_flag,
9627                 fixed_lead_time,
9628                 variable_lead_time,
9629                 wip_supply_locator_id,
9630                 wip_supply_type,
9631                 wip_supply_subinventory,
9632                 primary_uom_code,
9633                 primary_unit_of_measure,
9634                 allowed_units_lookup_code,
9635                 cost_of_sales_account,
9636                 sales_account,
9637                 default_include_in_rollup_flag,
9638                 inventory_item_status_code,
9639                 inventory_planning_code,
9640                 planner_code,
9641                 planning_make_buy_code,
9642                 fixed_lot_multiplier,
9643                 rounding_control_type,
9644                 carrying_cost,
9645                 postprocessing_lead_time,
9646                 preprocessing_lead_time,
9647                 full_lead_time,
9648                 order_cost,
9649                 mrp_safety_stock_percent,
9650                 mrp_safety_stock_code,
9651                 min_minmax_quantity,
9652                 max_minmax_quantity,
9653                 minimum_order_quantity,
9654                 fixed_order_quantity,
9655                 fixed_days_supply,
9656                 maximum_order_quantity,
9657                 atp_rule_id,
9658                 picking_rule_id,
9659                 reservable_type,
9660                 positive_measurement_error,
9661                 negative_measurement_error,
9662                 engineering_ecn_code,
9663                 engineering_item_id,
9664                 engineering_date,
9665                 service_starting_delay,
9666                 vendor_warranty_flag,
9667                 serviceable_component_flag,
9668                 serviceable_product_flag,
9669                 base_warranty_service_id,
9670                 payment_terms_id,
9671                 preventive_maintenance_flag,
9672                 primary_specialist_id,
9673                 secondary_specialist_id,
9674                 serviceable_item_class_id,
9675                 time_billable_flag,
9676                 material_billable_flag,
9677                 expense_billable_flag,
9678                 prorate_service_flag,
9679                 coverage_schedule_id,
9680                 service_duration_period_code,
9681                 service_duration,
9682                 max_warranty_amount,
9683                 response_time_period_code,
9684                 response_time_value,
9685                 new_revision_code,
9686                 tax_code,
9687                 must_use_approved_vendor_flag,
9688                 safety_stock_bucket_days,
9689                 auto_reduce_mps,
9690                 costing_enabled_flag,
9691                 invoiceable_item_flag,
9692                 invoice_enabled_flag,
9693                 outside_operation_flag,
9694                 outside_operation_uom_type,
9695                 auto_created_config_flag,
9696                 cycle_count_enabled_flag,
9697                 item_type,
9698                 model_config_clause_name,
9699                 ship_model_complete_flag,
9700                 mrp_planning_code,
9701                 repetitive_planning_flag,
9702                 return_inspection_requirement,
9703                 effectivity_control,
9704                 request_id,
9705                 program_application_id,
9706                 program_id,
9707                 program_update_date,
9708 		comms_nl_trackable_flag,               -- bugfix 2200256
9709 		default_so_source_type,
9710 		create_supply_flag,
9711 			-- begin bugfix 2336548
9712 		lot_status_enabled,
9713 		default_lot_status_id,
9714 		serial_status_enabled,
9715 		default_serial_status_id,
9716 		lot_split_enabled,
9717 		lot_merge_enabled,
9718 		bulk_picked_flag,
9719 			-- end bugfix 2336548
9720 			-- begin bugfix 2400609
9721 		FINANCING_ALLOWED_FLAG,
9722  		EAM_ITEM_TYPE ,
9723  		EAM_ACTIVITY_TYPE_CODE,
9724  		EAM_ACTIVITY_CAUSE_CODE,
9725  		EAM_ACT_NOTIFICATION_FLAG,
9726  		EAM_ACT_SHUTDOWN_STATUS,
9727  		SUBSTITUTION_WINDOW_CODE,
9728  		SUBSTITUTION_WINDOW_DAYS,
9729  		PRODUCT_FAMILY_ITEM_ID,
9730  		CHECK_SHORTAGES_FLAG,
9731  		PLANNED_INV_POINT_FLAG,
9732  		OVER_SHIPMENT_TOLERANCE,
9733  		UNDER_SHIPMENT_TOLERANCE,
9734  		OVER_RETURN_TOLERANCE,
9735  		UNDER_RETURN_TOLERANCE,
9736  		PURCHASING_TAX_CODE,
9737  		OVERCOMPLETION_TOLERANCE_TYPE,
9738  		OVERCOMPLETION_TOLERANCE_VALUE,
9739  		INVENTORY_CARRY_PENALTY,
9740  		OPERATION_SLACK_PENALTY,
9741  		UNIT_LENGTH,
9742  		UNIT_WIDTH,
9743  		UNIT_HEIGHT,
9744  		LOT_TRANSLATE_ENABLED,
9745  		CONTAINER_ITEM_FLAG,
9746  		VEHICLE_ITEM_FLAG,
9747  		DIMENSION_UOM_CODE,
9748  		SECONDARY_UOM_CODE,
9749  		MAXIMUM_LOAD_WEIGHT,
9750  		MINIMUM_FILL_PERCENT,
9751  		CONTAINER_TYPE_CODE,
9752  		INTERNAL_VOLUME,
9753  		EQUIPMENT_TYPE,
9754  		INDIVISIBLE_FLAG,
9755  		GLOBAL_ATTRIBUTE_CATEGORY,
9756  		GLOBAL_ATTRIBUTE1,
9757  		GLOBAL_ATTRIBUTE2,
9758  		GLOBAL_ATTRIBUTE3,
9759  		GLOBAL_ATTRIBUTE4,
9760  		GLOBAL_ATTRIBUTE5,
9761  		GLOBAL_ATTRIBUTE6,
9762  		GLOBAL_ATTRIBUTE7,
9763  		GLOBAL_ATTRIBUTE8,
9764  		GLOBAL_ATTRIBUTE9,
9765  		GLOBAL_ATTRIBUTE10,
9766 		DUAL_UOM_CONTROL,
9767  		DUAL_UOM_DEVIATION_HIGH,
9768  		DUAL_UOM_DEVIATION_LOW,
9769                 CONTRACT_ITEM_TYPE_CODE,
9770  		SUBSCRIPTION_DEPEND_FLAG,
9771  		SERV_REQ_ENABLED_CODE,
9772  		SERV_BILLING_ENABLED_FLAG,
9773  		RELEASE_TIME_FENCE_CODE,	-- 2898851
9774  		RELEASE_TIME_FENCE_DAYS,	-- 2898851
9775  		DEFECT_TRACKING_ON_FLAG,        -- 2858080
9776  		SERV_IMPORTANCE_LEVEL,
9777 			 -- end bugfix 2400609
9778 	        WEB_STATUS ,   --bugfix 2727983
9779                 tracking_quantity_ind,   /* Additional Attributes for Item in patchset J */
9780                 ont_pricing_qty_source,
9781                 approval_status ,
9782                 vmi_minimum_units,
9783                 vmi_minimum_days,
9784                 vmi_maximum_units,
9785                 vmi_maximum_days,
9786                 vmi_fixed_order_quantity,
9787                 so_authorization_flag,
9788                 consigned_flag,
9789                 asn_autoexpire_flag,
9790                 vmi_forecast_type,
9791                 forecast_horizon,
9792                 days_tgt_inv_supply,
9793                 days_tgt_inv_window,
9794                 days_max_inv_supply,
9795                 days_max_inv_window,
9796                 critical_component_flag,
9797                 drp_planned_flag,
9798                 exclude_from_budget_flag,
9799                 convergence,
9800                 continous_transfer,
9801                 divergence,
9802 			--begin r12,4574899
9803 		lot_divisible_flag,
9804 		grade_control_flag,
9805 		child_lot_flag,
9806                 child_lot_validation_flag,
9807 		copy_lot_attribute_flag,
9808 		parent_child_generation_flag,  --Bugfix 8821149
9809 		lot_substitution_enabled,      --Bugfix 8821149
9810 		recipe_enabled_flag,
9811                 process_quality_enabled_flag,
9812 		process_execution_enabled_flag,
9813 	        process_costing_enabled_flag,
9814 		hazardous_material_flag,
9815 		preposition_point,
9816 		repair_program,
9817 		outsourced_assembly
9818 			-- end rl2,4574899
9819 
9820                 )
9821         select /*+ USE_NL(MP1) */
9822                distinct
9823                 p_config_id,
9824                 m.organization_id,
9825                 sysdate,
9826                 gUserId,          -- last_updated_by
9827                 sysdate,
9828                 gUserId,          -- created_by
9829                 gLoginId ,        -- last_update_login
9830                 decode( get_attribute_control( 'summary_flag') , 1 , config.summary_flag, m.summary_flag),
9831                 decode( get_attribute_control( 'enabled_flag' ) , 1 , config.enabled_flag , m.enabled_flag),
9832                 decode( get_attribute_control( 'start_date_active'), 1 , config.start_date_active, m.start_date_active) ,
9833                 decode( get_attribute_control( 'end_date_active'), 1 , config.end_date_active, m.end_date_active) ,
9834                 decode( get_attribute_control( 'description' ) , 1 , config.description, m.description) ,
9835                 decode( get_attribute_control( 'buyer_id') , 1 , config.buyer_id, m.buyer_id) ,
9836                 decode( get_attribute_control( 'accounting_rule_id' ) , 1 , config.accounting_rule_id, m.accounting_rule_id) ,
9837                 decode( get_attribute_control( 'invoicing_rule_id' ) , 1 , config.invoicing_rule_id, m.invoicing_rule_id) ,
9838                 config.segment1,
9839                 config.segment2,
9840                 config.segment3,
9841                 config.segment4,
9842                 config.segment5,
9843                 config.segment6,
9844                 config.segment7,
9845                 config.segment8,
9846                 config.segment9,
9847                 config.segment10,
9848                 config.segment11,
9849                 config.segment12,
9850                 config.segment13,
9851                 config.segment14,
9852                 config.segment15,
9853                 config.segment16,
9854                 config.segment17,
9855                 config.segment18,
9856                 config.segment19,
9857                 config.segment20,
9858                 decode( get_attribute_control( 'attribute_category'), 1 , config.attribute_category, m.attribute_category),
9859                 m.attribute1,
9860                 m.attribute2,
9861                 m.attribute3,
9862                 m.attribute4,
9863                 m.attribute5,
9864                 m.attribute6,
9865                 m.attribute7,
9866                 m.attribute8,
9867                 m.attribute9,
9868                 m.attribute10,
9869                 m.attribute11,
9870                 m.attribute12,
9871                 m.attribute13,
9872                 m.attribute14,
9873                 m.attribute15,
9874                 m.attribute16,  -- Bug 9223457
9875                 m.attribute17,
9876                 m.attribute18,
9877                 m.attribute19,
9878                 m.attribute20,
9879                 m.attribute21,
9880                 m.attribute22,
9881                 m.attribute23,
9882                 m.attribute24,
9883                 m.attribute25,
9884                 m.attribute26,
9885                 m.attribute27,
9886                 m.attribute28,
9887                 m.attribute29,
9888                 m.attribute30,
9889                 'Y',		-- purchasing_item_flag,
9890                 'Y',                              -- Shippable Flag
9891                 'Y',            -- CUSTOMER_ORDER_FLAG
9892                 'Y',               -- INTERNAL_ORDER_FLAG
9893                 decode( get_attribute_control( 'service_item_flag' ), 1, config.service_item_flag , m.service_item_flag) ,
9894                 'Y',                              -- INVENTORY_ITEM_FLAG
9895                 decode( get_attribute_control( 'eng_item_flag' ) , 1 , config.eng_item_flag , m.eng_item_flag) ,
9896                 decode( get_attribute_control( 'inventory_asset_flag' ) , 1 , config.inventory_asset_flag , m.inventory_asset_flag) ,
9897                 'Y',		-- purchasing_enabled_flag,
9898                 'Y',            -- CUSTOMER_ORDER_ENABLED_FLAG
9899                 'Y',            -- INTERNAL_ORDER_ENABLED_FLAG
9900                 'Y',                        -- SO_TRANSACTIONS_FLAG
9901                 'Y',                     -- MTL_TRANSACTIONS_ENABLED_FLAG
9902                 'Y',                               -- STOCK_ENABLED_FLAG
9903                 'Y',                               -- BOM_ENABLED_FLAG
9904                 'Y',                               -- BUILD_IN_WIP_FLAG
9905                 decode( get_attribute_control( 'revision_qty_control_code' ) , 1 , config.revision_qty_control_code , m.revision_qty_control_code) ,
9906                 decode( get_attribute_control( 'item_catalog_group_id' ) , 1 , config.item_catalog_group_id, m.item_catalog_group_id) ,  -- check, earlier it was always from mfg org
9907                 decode( get_attribute_control( 'catalog_status_flag' ) , 1 , config.catalog_status_flag, m.catalog_status_flag) ,
9908                 decode( get_attribute_control( 'returnable_flag' ) , 1 , config.returnable_flag, m.returnable_flag) ,
9909                 decode( get_attribute_control( 'default_shipping_org' ) , 1, config.default_shipping_org, m.default_shipping_org),
9910                 decode( get_attribute_control( 'collateral_flag') , 1 , config.collateral_flag , m.collateral_flag) ,
9911                 decode( get_attribute_control( 'taxable_flag' ) , 1 , config.taxable_flag, m.taxable_flag) ,
9912                 decode( get_attribute_control( 'allow_item_desc_update_flag' ) , 1, config.allow_item_desc_update_flag, m.allow_item_desc_update_flag),
9913                 decode( get_attribute_control( 'inspection_required_flag' ), 1 , config.inspection_required_flag , m.inspection_required_flag),
9914                 decode( get_attribute_control( 'receipt_required_flag' ), 1, config.receipt_required_flag, m.receipt_required_flag) ,
9915                 decode( get_attribute_control( 'market_price' ) , 1 , config.market_price, m.market_price) ,
9916                 decode( get_attribute_control( 'hazard_class_id' ), 1 , config.hazard_class_id, m.hazard_class_id),
9917                 decode( get_attribute_control( 'rfq_required_flag'), 1 , config.rfq_required_flag, m.rfq_required_flag),
9918                 decode( get_attribute_control( 'qty_rcv_tolerance'), 1, config.qty_rcv_tolerance, m.qty_rcv_tolerance),
9919                 decode( get_attribute_control( 'un_number_id' ), 1 , config.un_number_id, m.un_number_id),
9920                 decode( get_attribute_control( 'price_tolerance_percent'), 1 , config.price_tolerance_percent, m.price_tolerance_percent) ,
9921                 decode( get_attribute_control( 'asset_category_id') , 1 , config.asset_category_id, m.asset_category_id) ,
9922                 decode( get_attribute_control( 'rounding_factor' ) , 1 , config.rounding_factor, m.rounding_factor) ,
9923                 decode( get_attribute_control( 'unit_of_issue') , 1 , config.unit_of_issue, m.unit_of_issue) ,
9924                 decode( get_attribute_control( 'enforce_ship_to_location_code' ) , 1 , config.enforce_ship_to_location_code , m.enforce_ship_to_location_code),
9925                 decode( get_attribute_control( 'allow_substitute_receipts_flag' ) , 1 , config.allow_substitute_receipts_flag, m.allow_substitute_receipts_flag) ,
9926                 decode( get_attribute_control( 'allow_unordered_receipts_flag' ) , 1 , config.allow_unordered_receipts_flag, m.allow_unordered_receipts_flag) ,
9927                 decode( get_attribute_control( 'allow_express_delivery_flag' ) ,1 , config.allow_express_delivery_flag, m.allow_express_delivery_flag) ,
9928                 decode( get_attribute_control( 'days_early_receipt_allowed') , 1, config.days_early_receipt_allowed, m.days_early_receipt_allowed) ,
9929                 decode( get_attribute_control( 'days_late_receipt_allowed' ) , 1 , config.days_late_receipt_allowed , m.days_late_receipt_allowed) ,
9930                 decode( get_attribute_control( 'receipt_days_exception_code')  , 1 , config.receipt_days_exception_code, m.receipt_days_exception_code) ,
9931                 decode( get_attribute_control( 'receiving_routing_id' ) , 1 , config.receiving_routing_id, m.receiving_routing_id),
9932                 decode( get_attribute_control( 'invoice_close_tolerance'), 1, config.invoice_close_tolerance, m.invoice_close_tolerance) ,
9933                 decode( get_attribute_control( 'receive_close_tolerance') , 1 , config.receive_close_tolerance , m.receive_close_tolerance) ,
9934                 decode( get_attribute_control( 'auto_lot_alpha_prefix') , 1, config.auto_lot_alpha_prefix, m.auto_lot_alpha_prefix) ,
9935                 decode( get_attribute_control( 'start_auto_lot_number') , 1, config.start_auto_lot_number, m.start_auto_lot_number) ,
9936                 decode( get_attribute_control( 'lot_control_code') ,1 , config.lot_control_code, m.lot_control_code) ,
9937                 decode( get_attribute_control( 'shelf_life_code'), 1 , config.shelf_life_code, m.shelf_life_code) ,
9938                 decode( get_attribute_control( 'shelf_life_days') , 1, config.shelf_life_days, m.shelf_life_days) ,
9939                 decode( get_attribute_control( 'serial_number_control_code' ) ,1,  config.serial_number_control_code, m.serial_number_control_code) ,
9940                 decode( get_attribute_control( 'start_auto_serial_number' ) , 1 , config.start_auto_serial_number, m.start_auto_serial_number) ,
9941                 decode( get_attribute_control( 'auto_serial_alpha_prefix') ,1 , config.auto_serial_alpha_prefix, m.auto_serial_alpha_prefix) ,
9942                 decode( get_attribute_control( 'source_type' ) ,1 , config.source_type, m.source_type) ,
9943                 decode( get_attribute_control( 'source_organization_id') , 1 , config.source_organization_id, m.source_organization_id) ,
9944                 decode( get_attribute_control( 'source_subinventory') ,1 , config.source_subinventory, m.source_subinventory) ,
9945                 decode( get_attribute_control( 'expense_account') , 1, config.expense_account, m.expense_account) ,
9946                 decode( get_attribute_control( 'encumbrance_account') , 1 , config.encumbrance_account, m.encumbrance_account) ,
9947                 decode( get_attribute_control( 'restrict_subinventories_code' ) , 1 , config.restrict_subinventories_code, m.restrict_subinventories_code) ,
9948 		-- bugfix 2301167 : we will calculate the unit weight/vol later..
9949                 null,		-- m.unit_weight,
9950                 null,		-- m.weight_uom_code,
9951                 null,		-- m.volume_uom_code,
9952                 null,		-- m.unit_volume,
9953 		-- end bugfix 2301167
9954                 decode( get_attribute_control( 'restrict_locators_code'), 1, config.restrict_locators_code, m.restrict_locators_code) ,
9955                 decode( get_attribute_control( 'location_control_code') , 1 , config.location_control_code, m.location_control_code) ,
9956                 decode( get_attribute_control( 'shrinkage_rate' ) , 1, config.shrinkage_rate, m.shrinkage_rate) ,
9957                 decode( get_attribute_control( 'acceptable_early_days') , 1 , config.acceptable_early_days, m.acceptable_early_days) ,
9958                 decode( get_attribute_control( 'planning_time_fence_code' ) , 1 , config.planning_time_fence_code, m.planning_time_fence_code) ,
9959                 decode( get_attribute_control( 'demand_time_fence_code') , 1 , config.demand_time_fence_code,  m.demand_time_fence_code) ,
9960                 decode( get_attribute_control( 'lead_time_lot_size') ,1, config.lead_time_lot_size, m.lead_time_lot_size) ,
9961                 decode( get_attribute_control( 'std_lot_size' ) , 1, config.std_lot_size, m.std_lot_size) ,
9962                 decode( get_attribute_control( 'cum_manufacturing_lead_time' ) , 1 , config.cum_manufacturing_lead_time, m.cum_manufacturing_lead_time) ,
9963                 decode( get_attribute_control( 'overrun_percentage') , 1, config.overrun_percentage, m.overrun_percentage) ,
9964                 decode( get_attribute_control( 'acceptable_rate_increase'), 1, config.acceptable_rate_increase, m.acceptable_rate_increase) ,
9965                 decode( get_attribute_control( 'acceptable_rate_decrease') , 1 , config.acceptable_rate_decrease, m.acceptable_rate_decrease) ,
9966                 decode( get_attribute_control( 'cumulative_total_lead_time' ) , 1 , config.cumulative_total_lead_time, m.cumulative_total_lead_time) ,
9967                 decode( get_attribute_control( 'planning_time_fence_days' ) , 1, config.planning_time_fence_days, m.planning_time_fence_days) ,
9968                 decode( get_attribute_control( 'demand_time_fence_days') , 1, config.demand_time_fence_days, m.demand_time_fence_days) ,
9969                 decode( get_attribute_control( 'end_assembly_pegging_flag') ,1 , config.end_assembly_pegging_flag , m.end_assembly_pegging_flag) ,
9970                 decode( get_attribute_control( 'planning_exception_set' ) , 1 , config.planning_exception_set, m.planning_exception_set) ,
9971                 4,                                 -- BOM_ITEM_TYPE : standard
9972                 'N',                               -- PICK_COMPONENTS_FLAG
9973                 'Y',                               -- REPLENISH_TO_ORDER_FLAG
9974                 p_model_id,                          -- Base Model ID
9975                 decode( get_attribute_control( 'atp_components_flag') , 1, config.atp_components_flag, evaluate_atp_attributes( m.atp_flag, m.atp_components_flag )) ,
9976                 decode( get_attribute_control( 'atp_flag') , 1, config.atp_flag, get_atp_flag) ,
9977                 decode( get_attribute_control( 'fixed_lead_time') ,1 , config.fixed_lead_time, m.fixed_lead_time) ,
9978                 decode( get_attribute_control( 'variable_lead_time') , 1 , config.variable_lead_time, m.variable_lead_time) ,
9979                 decode( get_attribute_control( 'wip_supply_locator_id' ) , 1, config.wip_supply_locator_id, m.wip_supply_locator_id) ,
9980                 decode( get_attribute_control( 'wip_supply_type' ) , 1 , config.wip_supply_type , m.wip_supply_type) ,
9981                 decode( get_attribute_control( 'wip_supply_subinventory' ) , 1 , config.wip_supply_subinventory, m.wip_supply_subinventory) ,
9982                 decode( get_attribute_control( 'primary_uom_code' ) , 1 , config.primary_uom_code, m.primary_uom_code) ,
9983                 decode( get_attribute_control( 'primary_unit_of_measure' ) , 1 , config.primary_unit_of_measure, m.primary_unit_of_measure) ,
9984                 decode( get_attribute_control( 'allowed_units_lookup_code' ) , 1 , config.allowed_units_lookup_code, m.allowed_units_lookup_code) ,
9985                 decode( get_attribute_control( 'cost_of_sales_account' ) , 1 , config.cost_of_sales_account, m.cost_of_sales_account) ,
9986                 decode( get_attribute_control( 'sales_account' ) , 1, config.sales_account, m.sales_account) ,
9987                 'Y',                        -- DEFAULT_INCLUDE_IN_ROLLUP_FLAG
9988                 decode( get_attribute_control( 'inventory_item_status_code' ) , 1 , config.inventory_item_status_code, m.inventory_item_status_code) ,
9989                 decode( get_attribute_control( 'inventory_planning_code') , 1, config.inventory_planning_code, m.inventory_planning_code) ,
9990                 decode( get_attribute_control( 'planner_code') , 1 , config.planner_code, m.planner_code) ,
9991                 decode( get_attribute_control( 'planning_make_buy_code' ) , 1 , config.planning_make_buy_code, m.planning_make_buy_code) ,
9992                 decode( get_attribute_control( 'fixed_lot_multiplier' ) , 1 , config.fixed_lot_multiplier, m.fixed_lot_multiplier) ,
9993                 decode( get_attribute_control( 'rounding_control_type' ) , 1, config.rounding_control_type, m.rounding_control_type) ,
9994                 decode( get_attribute_control( 'carrying_cost' ) ,1 , config.carrying_cost, m.carrying_cost) ,
9995                 decode( get_attribute_control( 'postprocessing_lead_time') , 1, config.postprocessing_lead_time, m.postprocessing_lead_time) ,
9996                 decode( get_attribute_control( 'preprocessing_lead_time' ) , 1 , config.preprocessing_lead_time, m.preprocessing_lead_time) ,
9997                 decode( get_attribute_control( 'full_lead_time') , 1,  config.full_lead_time, m.full_lead_time) ,
9998                 decode( get_attribute_control( 'order_cost') , 1, config.order_cost, m.order_cost) ,
9999                 decode( get_attribute_control( 'mrp_safety_stock_percent') , 1, config.mrp_safety_stock_percent, m.mrp_safety_stock_percent) ,
10000                 decode( get_attribute_control( 'mrp_safety_stock_code' ) , 1,  config.mrp_safety_stock_code, m.mrp_safety_stock_code) ,
10001                 decode( get_attribute_control( 'min_minmax_quantity' ) , 1, config.min_minmax_quantity, m.min_minmax_quantity) ,
10002                 decode( get_attribute_control( 'max_minmax_quantity' ) , 1 , config.max_minmax_quantity, m.max_minmax_quantity) ,
10003                 decode( get_attribute_control( 'minimum_order_quantity' ) , 1 , config.minimum_order_quantity , m.minimum_order_quantity) ,
10004                 decode( get_attribute_control( 'fixed_order_quantity' ) , 1 , config.fixed_order_quantity, m.fixed_order_quantity) ,
10005                 decode( get_attribute_control( 'fixed_days_supply' ) , 1 , config.fixed_days_supply, m.fixed_days_supply) ,
10006                 decode( get_attribute_control( 'maximum_order_quantity' ) , 1, config.maximum_order_quantity, m.maximum_order_quantity) ,
10007                 decode( get_attribute_control( 'atp_rule_id' ) , 1, config.atp_rule_id, m.atp_rule_id) ,
10008                 decode( get_attribute_control( 'picking_rule_id' ) , 1, config.picking_rule_id, m.picking_rule_id) ,
10009                 1,                                      -- m.reservable_type
10010                 decode( get_attribute_control( 'positive_measurement_error' ) , 1, config.positive_measurement_error, m.positive_measurement_error) ,
10011                 decode( get_attribute_control( 'negative_measurement_error' ) , 1, config.negative_measurement_error, m.negative_measurement_error) ,
10012                 decode( get_attribute_control( 'engineering_ecn_code' ) , 1 , config.engineering_ecn_code, m.engineering_ecn_code) ,
10013                 decode( get_attribute_control( 'engineering_item_id' ) , 1 , config.engineering_item_id, m.engineering_item_id) ,
10014                 decode( get_attribute_control( 'engineering_date' ) , 1, config.engineering_date, m.engineering_date) ,
10015                 decode( get_attribute_control( 'service_starting_delay') , 1 , config.service_starting_delay, m.service_starting_delay) ,
10016                 decode( get_attribute_control( 'vendor_warranty_flag') , 1 , config.vendor_warranty_flag, m.vendor_warranty_flag) ,
10017                 decode( get_attribute_control( 'serviceable_component_flag' ) , 1, config.serviceable_component_flag , m.serviceable_component_flag) ,
10018                 decode( get_attribute_control( 'serviceable_product_flag' ) , 1, config.serviceable_product_flag , m.serviceable_product_flag) ,
10019                 decode( get_attribute_control( 'base_warranty_service_id' ) ,1 , config.base_warranty_service_id, m.base_warranty_service_id) ,
10020                 decode( get_attribute_control( 'payment_terms_id' ) , 1 , config.payment_terms_id, m.payment_terms_id) ,
10021                 decode( get_attribute_control( 'preventive_maintenance_flag') , 1,  config.preventive_maintenance_flag, m.preventive_maintenance_flag) ,
10022                 decode( get_attribute_control( 'primary_specialist_id') , 1 , config.primary_specialist_id, m.primary_specialist_id),
10023                 decode( get_attribute_control( 'secondary_specialist_id') , 1 , config.secondary_specialist_id, m.secondary_specialist_id) ,
10024                 decode( get_attribute_control( 'serviceable_item_class_id') , 1, config.serviceable_item_class_id, m.serviceable_item_class_id) ,
10025                 decode( get_attribute_control( 'time_billable_flag' ) , 1 , config.time_billable_flag, m.time_billable_flag) ,
10026                 decode( get_attribute_control( 'material_billable_flag' ) , 1, config.material_billable_flag, m.material_billable_flag) ,
10027                 decode( get_attribute_control( 'expense_billable_flag' ) , 1 , config.expense_billable_flag , m.expense_billable_flag) ,
10028                 decode( get_attribute_control( 'prorate_service_flag' ) , 1, config.prorate_service_flag, m.prorate_service_flag) ,
10029                 decode( get_attribute_control( 'coverage_schedule_id' ) , 1,  config.coverage_schedule_id, m.coverage_schedule_id) ,
10030                 decode( get_attribute_control( 'service_duration_period_code' ) , 1, config.service_duration_period_code, m.service_duration_period_code) ,
10031                 decode( get_attribute_control( 'service_duration') , 1,  config.service_duration, m.service_duration) ,
10032                 decode( get_attribute_control( 'max_warranty_amount' ) , 1 , config.max_warranty_amount, m.max_warranty_amount) ,
10033                 decode( get_attribute_control( 'response_time_period_code' ) , 1, config.response_time_period_code, m.response_time_period_code) ,
10034                 decode( get_attribute_control( 'response_time_value') , 1, config.response_time_value, m.response_time_value) ,
10035                 decode( get_attribute_control( 'new_revision_code' ) , 1 , config.new_revision_code, m.new_revision_code) ,
10036                 decode( get_attribute_control( 'tax_code') , 1, config.tax_code, m.tax_code) ,
10037                 decode( get_attribute_control( 'must_use_approved_vendor_flag' ) , 1, config.must_use_approved_vendor_flag, m.must_use_approved_vendor_flag) ,
10038                 decode( get_attribute_control( 'safety_stock_bucket_days' ) , 1, config.safety_stock_bucket_days, m.safety_stock_bucket_days) ,
10039                 decode( get_attribute_control( 'auto_reduce_mps') , 1, config.auto_reduce_mps, m.auto_reduce_mps) ,
10040                 decode( get_attribute_control( 'costing_enabled_flag' ) , 1, config.costing_enabled_flag, m.costing_enabled_flag) ,
10041                 decode( get_attribute_control( 'invoiceable_item_flag' ) , 1, config.invoiceable_item_flag, m.invoiceable_item_flag ) ,
10042                 decode( get_attribute_control( 'invoice_enabled_flag' ) , 1 , config.invoice_enabled_flag, m.invoice_enabled_flag ) ,
10043                 decode( get_attribute_control( 'outside_operation_flag') , 1, config.outside_operation_flag, m.outside_operation_flag) ,
10044                 decode( get_attribute_control( 'outside_operation_uom_type' ) , 1, config.outside_operation_uom_type, m.outside_operation_uom_type) ,
10045                 'Y',                                 -- auto created config flag
10046                 decode( get_attribute_control( 'cycle_count_enabled_flag') , 1 , config.cycle_count_enabled_flag, m.cycle_count_enabled_flag) ,
10047                 p_lItemType,
10048                 decode( get_attribute_control( 'model_config_clause_name') ,1 , config.model_config_clause_name, m.model_config_clause_name) ,
10049                 decode( get_attribute_control( 'ship_model_complete_flag') ,1 , config.ship_model_complete_flag, m.ship_model_complete_flag) ,
10050                 decode( get_attribute_control( 'mrp_planning_code' ) , 1 , config.mrp_planning_code, m.mrp_planning_code) ,                 -- earlier it was always from one org only
10051                 decode( get_attribute_control( 'repetitive_planning_flag' ) , 1, config.repetitive_planning_flag, m.repetitive_planning_flag) ,   -- earlier it was always from one org only
10052                 decode( get_attribute_control( 'return_inspection_requirement' ) , 1 , config.return_inspection_requirement, m.return_inspection_requirement) ,
10053                 nvl( decode( get_attribute_control( 'effectivity_control') , 1, config.effectivity_control, m.effectivity_control) , 1),
10054                 null,                               -- req_id
10055                 null,                               -- prg_appid
10056                 null,                               -- prg_id
10057                 sysdate,
10058 		decode( get_attribute_control( 'comms_nl_trackable_flag') , 1, config.comms_nl_trackable_flag, m.comms_nl_trackable_flag) ,               -- bugfix 2200256
10059 		nvl( decode( get_attribute_control( 'default_so_source_type') , 1 , config.default_so_source_type, m.default_so_source_type) ,'INTERNAL'),
10060 		nvl( decode( get_attribute_control( 'create_supply_flag') , 1, config.create_supply_flag, m.create_supply_flag) , 'Y'),
10061 			-- begin bugfix 2336548
10062 		decode( get_attribute_control( 'lot_status_enabled') , 1, config.lot_status_enabled, m.lot_status_enabled) ,
10063 		decode( get_attribute_control( 'default_lot_status_id' ) , 1, config.default_lot_status_id, m.default_lot_status_id) ,
10064 		decode( get_attribute_control( 'serial_status_enabled') , 1, config.serial_status_enabled, m.serial_status_enabled) ,
10065 		decode( get_attribute_control( 'default_serial_status_id') ,1 , config.default_serial_status_id, m.default_serial_status_id) ,
10066 		decode( get_attribute_control( 'lot_split_enabled') , 1, config.lot_split_enabled, m.lot_split_enabled) ,
10067 		decode( get_attribute_control( 'lot_merge_enabled') ,1 , config.lot_merge_enabled, m.lot_merge_enabled) ,
10068 		decode( get_attribute_control( 'bulk_picked_flag' ) , 1 , config.bulk_picked_flag, m.bulk_picked_flag) ,
10069 			-- end bugfix 2336548
10070 			-- begin bugfix 2400609
10071 		decode( get_attribute_control( 'financing_allowed_flag') , 1, config.financing_allowed_flag, m.FINANCING_ALLOWED_FLAG) ,
10072  		decode( get_attribute_control( 'eam_item_type') , 1 , config.eam_item_type, m.EAM_ITEM_TYPE ) ,
10073  		decode( get_attribute_control( 'eam_activity_type_code') , 1 , config.eam_activity_type_code, m.EAM_ACTIVITY_TYPE_CODE) ,
10074  		decode( get_attribute_control( 'eam_activity_cause_code') , 1, config.eam_activity_cause_code, m.EAM_ACTIVITY_CAUSE_CODE) ,
10075  		decode( get_attribute_control( 'eam_act_notification_flag') , 1, config.eam_act_notification_flag, m.EAM_ACT_NOTIFICATION_FLAG) ,
10076  		decode( get_attribute_control( 'eam_act_shutdown_status') , 1, config.eam_act_shutdown_status, m.EAM_ACT_SHUTDOWN_STATUS) ,
10077  		decode( get_attribute_control( 'substitution_window_code') , 1, config.substitution_window_code, m.SUBSTITUTION_WINDOW_CODE) ,
10078  		decode( get_attribute_control( 'substitution_window_days') , 1, config.substitution_window_days, m.SUBSTITUTION_WINDOW_DAYS) ,
10079  		null, --5385901 decode( get_attribute_control( 'product_family_item_id') , 1, config.product_family_item_id, m.PRODUCT_FAMILY_ITEM_ID) ,
10080  		decode( get_attribute_control( 'check_shortages_flag') , 1, config.check_shortages_flag, m.CHECK_SHORTAGES_FLAG) ,
10081  		decode( get_attribute_control( 'planned_inv_point_flag') , 1, config.planned_inv_point_flag, m.PLANNED_INV_POINT_FLAG) ,
10082  		decode( get_attribute_control( 'over_shipment_tolerance') , 1, config.over_shipment_tolerance, m.OVER_SHIPMENT_TOLERANCE) ,
10083  		decode( get_attribute_control( 'under_shipment_tolerance') , 1, config.under_shipment_tolerance, m.UNDER_SHIPMENT_TOLERANCE) ,
10084  		decode( get_attribute_control( 'over_return_tolerance') , 1, config.over_return_tolerance, m.OVER_RETURN_TOLERANCE) ,
10085  		decode( get_attribute_control( 'under_return_tolerance') , 1, config.under_return_tolerance, m.UNDER_RETURN_TOLERANCE) ,
10086  		decode( get_attribute_control( 'purchasing_tax_code') , 1, config.purchasing_tax_code, m.PURCHASING_TAX_CODE) ,
10087  		decode( get_attribute_control( 'overcompletion_tolerance_type') , 1, config.overcompletion_tolerance_type, m.OVERCOMPLETION_TOLERANCE_TYPE) ,
10088  		decode( get_attribute_control( 'overcompletion_tolerance_value') , 1, config.overcompletion_tolerance_value, m.OVERCOMPLETION_TOLERANCE_VALUE) ,
10089  		decode( get_attribute_control( 'inventory_carry_penalty'), 1, config.inventory_carry_penalty, m.INVENTORY_CARRY_PENALTY) ,
10090  		decode( get_attribute_control( 'operation_slack_penalty') ,1, config.operation_slack_penalty, m.OPERATION_SLACK_PENALTY) ,
10091  		decode( get_attribute_control( 'unit_length') , 1, config.unit_length, m.UNIT_LENGTH) ,
10092  		decode( get_attribute_control( 'unit_width' ) , 1, config.unit_width, m.UNIT_WIDTH) ,
10093  		decode( get_attribute_control( 'unit_height') , 1, config.unit_height, m.UNIT_HEIGHT) ,
10094  		decode( get_attribute_control( 'lot_translate_enabled') , 1, config.lot_translate_enabled, m.LOT_TRANSLATE_ENABLED) ,
10095  		decode( get_attribute_control( 'container_item_flag') , 1, config.container_item_flag, m.CONTAINER_ITEM_FLAG) ,
10096  		decode( get_attribute_control( 'vehicle_item_flag') , 1, config.vehicle_item_flag, m.VEHICLE_ITEM_FLAG) ,
10097  		decode( get_attribute_control( 'dimension_uom_code') , 1, config.dimension_uom_code, m.DIMENSION_UOM_CODE) ,
10098  		decode( get_attribute_control( 'secondary_uom_code') , 1, config.secondary_uom_code, m.SECONDARY_UOM_CODE) ,
10099  		decode( get_attribute_control( 'maximum_load_weight') , 1, config.maximum_load_weight, m.MAXIMUM_LOAD_WEIGHT) ,
10100  		decode( get_attribute_control( 'minimum_fill_percent') , 1, config.minimum_fill_percent, m.MINIMUM_FILL_PERCENT) ,
10101  		decode( get_attribute_control( 'container_type_code') , 1, config.container_type_code, m.CONTAINER_TYPE_CODE) ,
10102  		decode( get_attribute_control( 'internal_volume') , 1, config.internal_volume, m.INTERNAL_VOLUME) ,
10103  		decode( get_attribute_control( 'equipment_type') , 1,  config.equipment_type , m.EQUIPMENT_TYPE) ,
10104  		decode( get_attribute_control( 'indivisible_flag') , 1, config.indivisible_flag, m.INDIVISIBLE_FLAG) ,
10105  		decode( get_attribute_control( 'global_attribute_category'), 1, config.global_attribute_category, m.GLOBAL_ATTRIBUTE_CATEGORY) ,
10106  		m.GLOBAL_ATTRIBUTE1,
10107  		m.GLOBAL_ATTRIBUTE2,
10108  		m.GLOBAL_ATTRIBUTE3,
10109  		m.GLOBAL_ATTRIBUTE4,
10110  		m.GLOBAL_ATTRIBUTE5,
10111  		m.GLOBAL_ATTRIBUTE6,
10112  		m.GLOBAL_ATTRIBUTE7,
10113  		m.GLOBAL_ATTRIBUTE8,
10114  		m.GLOBAL_ATTRIBUTE9,
10115  		m.GLOBAL_ATTRIBUTE10,
10116      		decode( get_attribute_control( 'dual_uom_control') , 1, config.dual_uom_control, m.DUAL_UOM_CONTROL) ,
10117  		decode( get_attribute_control( 'dual_uom_deviation_high') , 1, config.dual_uom_deviation_high, m.DUAL_UOM_DEVIATION_HIGH) ,
10118  		decode( get_attribute_control( 'dual_uom_deviation_low') , 1, config.dual_uom_deviation_low, m.DUAL_UOM_DEVIATION_LOW) ,
10119                 decode( get_attribute_control( 'contract_item_type_code') , 1, config.contract_item_type_code, m.CONTRACT_ITEM_TYPE_CODE) ,
10120  		decode( get_attribute_control( 'subscription_depend_flag') , 1 , config.subscription_depend_flag, m.SUBSCRIPTION_DEPEND_FLAG) ,
10121  		decode( get_attribute_control( 'serv_req_enabled_code' ) , 1, config.serv_req_enabled_code, m.SERV_REQ_ENABLED_CODE) ,
10122  		decode( get_attribute_control( 'serv_billing_enabled_flag') , 1, config.serv_billing_enabled_flag, m.SERV_BILLING_ENABLED_FLAG) ,
10123  		decode( get_attribute_control( 'release_time_fence_code') , 1, config.release_time_fence_code, m.RELEASE_TIME_FENCE_CODE) ,
10124  		decode( get_attribute_control( 'release_time_fence_days' ) ,1, config.release_time_fence_days, m.RELEASE_TIME_FENCE_DAYS) ,
10125  		decode( get_attribute_control( 'defect_tracking_on_flag') , 1, config.defect_tracking_on_flag, m.DEFECT_TRACKING_ON_FLAG) ,
10126  		decode( get_attribute_control( 'serv_importance_level'), 1, config.serv_importance_level, m.SERV_IMPORTANCE_LEVEL) ,
10127 	        decode( get_attribute_control( 'web_status') , 1, config.web_status, m.web_status) ,          --  bugfix 2727983
10128                 decode( get_attribute_control( 'tracking_quantity_ind' ) , 1 , config.tracking_quantity_ind, nvl( m.tracking_quantity_ind , 'P' )),
10129                 decode( get_attribute_control( 'ont_pricing_qty_source' ) , 1 , config.ont_pricing_qty_source, nvl( m.ont_pricing_qty_source, 'P')) ,
10130                 decode( get_attribute_control( 'approval_status' ) , 1 , config.approval_status, m.approval_status) ,
10131                 decode( get_attribute_control( 'vmi_minimum_units' ) , 1, config.vmi_minimum_units, m.vmi_minimum_units) ,
10132                 decode( get_attribute_control( 'vmi_minimum_days' ) ,1 , config.vmi_minimum_days, m.vmi_minimum_days) ,
10133                 decode( get_attribute_control( 'vmi_maximum_units' ) , 1 , config.vmi_maximum_units, m.vmi_maximum_units) ,
10134                 decode( get_attribute_control( 'vmi_maximum_days' ) , 1 , config.vmi_maximum_days, m.vmi_maximum_days ) ,
10135                 decode( get_attribute_control( 'vmi_fixed_order_quantity' ) , 1 , config.vmi_fixed_order_quantity, m.vmi_fixed_order_quantity) ,
10136                 decode( get_attribute_control( 'so_authorization_flag' ) , 1, config.so_authorization_flag, m.so_authorization_flag ) ,
10137                 decode( get_attribute_control( 'consigned_flag' ) , 1, config.consigned_flag, m.consigned_flag) ,
10138                 decode( get_attribute_control( 'asn_autoexpire_flag' ) , 1 , config.asn_autoexpire_flag, m.asn_autoexpire_flag ) ,
10139                 decode( get_attribute_control( 'vmi_forecast_type' ) , 1 , config.vmi_forecast_type, m.vmi_forecast_type) ,
10140                 decode( get_attribute_control( 'forecast_horizon' ) , 1, config.forecast_horizon, m.forecast_horizon ) ,
10141                 decode( get_attribute_control( 'days_tgt_inv_supply' ) , 1, config.days_tgt_inv_supply, m.days_tgt_inv_supply ) ,
10142                 decode( get_attribute_control( 'days_tgt_inv_window' ) , 1 , config.days_tgt_inv_window, m.days_tgt_inv_window ) ,
10143                 decode( get_attribute_control( 'days_max_inv_supply' ) , 1, config.days_max_inv_supply, m.days_max_inv_supply ) ,
10144                 decode( get_attribute_control( 'days_max_inv_window' ) , 1 , config.days_max_inv_window, m.days_max_inv_window ) ,
10145                 decode( get_attribute_control( 'critical_component_flag' ) , 1, config.critical_component_flag, m.critical_component_flag) ,
10146                 decode( get_attribute_control( 'drp_planned_flag' ) ,1 , config.drp_planned_flag, m.drp_planned_flag ) ,
10147                 decode( get_attribute_control( 'exclude_from_budget_flag' ) , 1 , config.exclude_from_budget_flag, m.exclude_from_budget_flag) ,
10148                 decode( get_attribute_control( 'convergence' ) , 1 , config.convergence, m.convergence ) ,
10149                 decode( get_attribute_control( 'continous_transfer' ) , 1, config.continous_transfer, m.continous_transfer ) ,
10150                 decode( get_attribute_control( 'divergence' ) , 1 , config.divergence, m.divergence ),
10151 		  --begin r12,4574899
10152 		nvl(m.lot_divisible_flag, 'N'),  --Bugfix 6343429
10153 		'N',
10154 		/* Bugfix 8821149: Will populate these values from model.
10155 		'N',
10156 	        'N',
10157 		'N',
10158 		*/
10159 		decode( get_attribute_control( 'child_lot_flag' ) , 1 , config.child_lot_flag, m.child_lot_flag),
10160 		decode( get_attribute_control( 'child_lot_validation_flag' ) , 1 , config.child_lot_validation_flag, m.child_lot_validation_flag),
10161 		decode( get_attribute_control( 'copy_lot_attribute_flag' ) , 1 , config.copy_lot_attribute_flag, m.copy_lot_attribute_flag),
10162 		decode( get_attribute_control( 'parent_child_generation_flag' ) , 1 , config.parent_child_generation_flag, m.parent_child_generation_flag),
10163 		decode( get_attribute_control( 'lot_substitution_enabled' ) , 1 , config.lot_substitution_enabled, m.lot_substitution_enabled),
10164 		-- End Bugfix 8821149
10165 		'N',
10166 		'N',
10167 		'N',
10168 		'N',
10169 		'N',
10170 		'N',
10171 		3,   --repair_program
10172 		2   --outsourced_assembly
10173 		 --end r12,4574899
10174         from
10175                 mtl_system_items_b  m,               -- Model
10176                 mtl_system_items_b  config,
10177                 bom_cto_order_lines     bcol,
10178                 bom_cto_src_orgs        bcso
10179         where  m.inventory_item_id = p_model_id
10180         and bcso.model_item_id = p_model_id
10181         and bcso.line_id = p_line_id
10182         and bcso.line_id = bcol.line_id
10183         and bcol.config_item_id = config.inventory_item_id
10184         and config.organization_id = bcol.ship_from_org_id
10185         and m.organization_id = bcso.organization_id
10186         and NOT EXISTS
10187                 (select NULL
10188                 from mtl_system_items_b
10189                 where inventory_item_id = p_config_id
10190                 and organization_id = bcso.organization_id);
10191 
10192 
10193 
10194  	      --Start Bugfix 8305535
10195  	      sqlcnt1 := sql%rowcount;
10196  	      if ( sqlcnt1 > 0) then
10197  	         IF PG_DEBUG <> 0 THEN
10198  	              oe_debug_pub.add('Create_Item: ' || 'Going to insert in pl/sql table for project Genesis',2);
10199  	         END IF;
10200  	              l_ind_cnt := CTO_MSUTIL_PUB.cfg_tbl_var.count;
10201  	              CTO_MSUTIL_PUB.cfg_tbl_var(l_ind_cnt + 1) := p_config_id;
10202  	         end if;
10203  	      --End Bugfix 8305535
10204 
10205 exception
10206 when others then
10207     null ;
10208 
10209 end create_preconfigured_item ;
10210 
10211 
10212 
10213 
10214 
10215 end CTO_CONFIG_ITEM_PK;
10216