DBA Data[Home] [Help]

PACKAGE BODY: APPS.CTO_ATP_INTERFACE_PK

Source


1 package body CTO_ATP_INTERFACE_PK AS
2 /* $Header: CTOATPIB.pls 115.70 2003/10/10 22:29:08 ksarkar ship $ */
3 
4  G_PKG_NAME CONSTANT VARCHAR2(30) := 'CTO_ATP_INTERFACE_PK';
5 /*
6 ** Steps for evaluate_om_shipset
7 ** -----------------------------
8 **
9 ** 1) Get ship set
10 ** 2) get additional info from OEOL, MSYI, BIC
11 ** 3) return ship set if no models or config items( atp may be doing this )
12 ** 4) eliminate mandatory components and stand alone standard items
13 ** 5) arrange data for sparse array traversal
14 ** 6) populate level
15 ** 7) populate parent ato
16 ** 8) unscramble array
17 ** 9) order information by top model line id, level
18 ** 10) check whether ship set involves sourcing and identify sources
19 ** 11) return if no sourcing found [Eliminate pure pto ?]
20 ** 12) filter/prepare structure for calling BOM API
21 ** 13) call BOM API
22 ** 14) prepare ship set for atp
23 ** 15) send the shipset and BOM to ATP.
24 **
25 */
26 
27 /*
28 ** This record holds data for each line_id for evaluating shipset
29 */
30 TYPE CTO_SHIPSET_TYPE IS RECORD (
31 ship_set_name        varchar2(200) ,
32 header_id            number ,
33 line_id              number,
34 top_model_line_id    number,
35 ato_line_id          number ,
36 link_to_line_id      number ,
37 inventory_item_id    oe_order_lines.inventory_item_id%type ,
38 item_type_code       oe_order_lines.item_type_code%type ,
39 wip_supply_type      bom_inventory_components.wip_supply_type%type ,
40 bom_item_type           mtl_system_items.bom_item_type%type ,
41 replenish_to_order_flag mtl_system_items.replenish_to_order_flag%type ,
42 pick_components_flag    mtl_system_items.pick_components_flag%type ,
43 base_item_id            mtl_system_items.base_item_id%type ,
44 build_in_wip_flag       mtl_system_items.build_in_wip_flag%type  ,
45 buy_model            varchar2(1) , /* added for procure config 'B', 'Y' , 'N' */
46 /* value 'B' has been introduced to flag the top buy model. value 'Y' has been ointroduced to flag all the buy model components excluding the top buy model. The attribute_05 = 'N' will be set for visible demand */
47 
48 configuration_exists varchar2(1), /* 'Y' , 'N' to indicate configuration exists, this data will be used to decide what to populate in visible demand flag */
49 plan_level           number(3) ,
50 parent_ato_line_id   number(9) ,
51 sourcing_org         number ,
52 ordered_quantity     number ,
53 bom_exists           boolean ,
54 mandatory_component  boolean ,
55 sourced_components    boolean ,
56 top_model_ato        boolean ,
57 location             number ,
58 error_code           number ,
59 error_message        varchar2(200) ,
60 unique_id            number(9) ,
61 auto_generated       boolean,  /* changes for navneet */
62 process_demand	     boolean,
63 atp_flag	     varchar2(1),	-- 2462661
64 atp_components_flag  varchar2(1),	-- 2462661
65 stored_atp_flag	     varchar2(1)	-- 2462661
66 ,mlmo_flag	     varchar2(1)	-- 2723674
67 );
68 
69 /*
70 ** This record holds processed information for atp requests from configurator
71 */
72 TYPE CZ_REQUESTS_TYPE IS RECORD (
73 ship_set_name                varchar2(200) ,
74 configurator_session_key     cz_atp_requests.configurator_session_key%type ,
75 seq_no                       cz_atp_requests.seq_no%type ,
76 Item_key                     cz_atp_requests.item_key%type ,
77 quantity                     cz_atp_requests.quantity%type ,
78 UOM_CODE                     cz_atp_requests.UOM_CODE%type ,
79 inv_org_id                   cz_atp_requests.inv_org_id%type ,
80 ship_To_date                 cz_atp_requests.ship_to_date%type  ,
81 inventory_item_id            mtl_system_items.inventory_item_id%type ,
82 self_code                    cz_atp_requests.item_key%type ,
83 parent_code                  cz_atp_requests.item_key%type ,
84 top_model_code               cz_atp_requests.item_key%type ,
85 config_item_id               cz_atp_requests.config_item_id%type , /* Added for MI */
86 parent_config_item_id        cz_atp_requests.parent_config_item_id%type , /* Added for MI */
87 line_id                      number ,
88 link_to_line_id              number ,
89 ato_line_id                  number ,
90 item_type_code               oe_order_lines.item_type_code%type ,
91 wip_supply_type              bom_inventory_components.wip_supply_type%type ,
92 bom_item_type                mtl_system_items.bom_item_type%type ,
93 replenish_to_order_flag      mtl_system_items.replenish_to_order_flag%type ,
94 pick_components_flag         mtl_system_items.pick_components_flag%type ,
95 base_item_id                 mtl_system_items.base_item_id%type ,
96 build_in_wip_flag            mtl_system_items.build_in_wip_flag%type  ,
97 top_model_ato                boolean ,
98 top_model_line_id            number ,
99 assigned                     boolean ,
100 location                     number ,
101 parent_location              number,
102 atp_flag		     varchar2(1),	--2462661
103 atp_components_flag	     varchar2(1)	--2462661
104 ) ;
105 
106 /*
107 ** This record holds status ( sourcing, success, start location ) for each shipset
108 */
109 TYPE SHIPSET_SUCCESS_REC_TYPE IS RECORD (
110 ship_set_name         varchar2(200) ,
111 success_status          boolean  ,
112 model_sourced         boolean ,
113 start_location        number ,
114 cto_start_location         number ,
115 reduced_start_location         number  ,
116 auto_generated           boolean,
117 process_demand	boolean
118 ) ;
119 
120 /* 2723674  -- record to hold SLSO lines for flushing demand */
121 TYPE SLSO_CTO_SHIPSET_TYPE IS RECORD (
122 ship_set_name        varchar2(200) ,
123 ato_line_id          number
124 );
125 slso_index	number  := 0;
126 rows_deleted	number ;
127 /* 2723674 end new record type creation */
128 
129 /*
130 ** This record holds autogenerated shipset names and whether to process demand for them or not
131 */
132 TYPE AUTO_SHIPSET_REC_TYPE IS RECORD (
133 set_name varchar2(200) ,
134 process_demand	boolean
135 );
136 
137 ginstance_id     number;
138 
139 NULL_MRP_ASSIGNMENT_SET EXCEPTION ;
140 INVALID_MRP_ASSIGNMENT_SET EXCEPTION ;
141 
142 TYPE CTO_SHIPSET_TBL_TYPE is table of CTO_SHIPSET_TYPE INDEX BY BINARY_INTEGER ;
143 TYPE CZ_REQUESTS_TBL_TYPE is table of CZ_REQUESTS_TYPE INDEX BY BINARY_INTEGER ;
144 TYPE SHIPSET_SUCCESS_TBL_TYPE is table of SHIPSET_SUCCESS_REC_TYPE INDEX BY BINARY_INTEGER ;
145 
146 TYPE AUTO_SHIPSET_TBL_TYPE is table of AUTO_SHIPSET_REC_TYPE INDEX BY BINARY_INTEGER ;
147 
148 /* 2723674 new table type creation */
149 TYPE SLSO_SHIPSET_TBL_TYPE is table of SLSO_CTO_SHIPSET_TYPE INDEX BY BINARY_INTEGER ;
150 /* 2723674 end new table type creation */
151 
152 g_requests_tab        CZ_REQUESTS_TBL_TYPE ;
153 g_shipset_status_tbl  SHIPSET_SUCCESS_TBL_TYPE ;
154 
155 /* common table to store information to be dumped into bom_cto_order_demand */
156 g_final_cto_shipset     CTO_SHIPSET_TBL_TYPE ; /* package global array */
157 g_cto_shipset     CTO_SHIPSET_TBL_TYPE ; /* package global array */
158 g_cto_sparse_shipset     CTO_SHIPSET_TBL_TYPE ; /* package global sparse array */
159 local_cto_shipset     CTO_SHIPSET_TBL_TYPE ; /* package global variable */
160 g_shipset      MRP_ATP_PUB.ATP_REC_TYP  ;
161 g_final_shipset      MRP_ATP_PUB.ATP_REC_TYP  ;
162 
163 g_auto_generated_shipset  AUTO_SHIPSET_TBL_TYPE ;
164 
165 g_expected_error_code   number ;
166 g_stmt_num Number ;
167 gUserId        number ;
168 gLoginId        number ;
169 gMrpAssignmentSet        number ;
170 gMrpAssignmentSetName        varchar2(80) ;
171 
172 slso_shipset	SLSO_SHIPSET_TBL_TYPE ; /* 2723674 */
173 
174 
175 /*********************************
176  API for data initialization
177 *********************************/
178 
179 PROCEDURE initialize_session_globals;
180 
181 PG_DEBUG Number := NVL(FND_PROFILE.value('ONT_DEBUG_LEVEL'), 0);
182 
183 PROCEDURE initialize_assignment_set( x_return_status out varchar2)  ;
184 
185 /*********************************
186  API for Business logic Processing
187 **********************************/
188 
189 PROCEDURE evaluate_shipset(
190   p_shipset            in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
191 , p_auto_generated     in    boolean
192 , p_process_demand     in    boolean
193 , p_atp_bom_rec        out  NOCOPY MRP_ATP_PUB.ATP_BOM_REC_TYP
194 , p_model_sourced      out  boolean
195 , x_return_status      out varchar2
196 );
197 
198 PROCEDURE populate_cz_shipset(
199   p_shipset                in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
200 , p_orig_shipsEt_tracker   in out NOCOPY CTO_SHIPSET_TBL_TYPE
201 , p_shipset_contains_model in out boolean
202 , x_return_status             out varchar2
203 );
204 
205 PROCEDURE populate_om_shipset(
206   p_shipset                in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
207 , p_orig_shipset_tracker   in out NOCOPY CTO_SHIPSET_TBL_TYPE
208 , p_shipset_contains_model in out boolean
209 , x_return_status             out varchar2
210 );
211 
212 PROCEDURE process_sourcing_chain(
213   p_top_location     number
214 , p_location         number
215 , p_org              number
216 , p_isPhantom        boolean
217 , p_isProcured       boolean
218 , p_basis_qty        number
219 , x_return_status             out varchar2
220 );
221 
222 PROCEDURE query_sourcing_org(
223   p_inventory_item_id    NUMBER
224 , p_organization_id      NUMBER
225 , p_sourcing_rule_exists out varchar2
226 , p_source_type          out NUMBER
227 , p_sourcing_org         out NUMBER
228 , p_transit_lead_time    out NUMBER
229 , x_return_status        out varchar2
230 );
231 
232 
233 /************************************************************
234  Convenience API for data relationship query and manipulation
235 *************************************************************/
236 
237 PROCEDURE propagate_ato_line_id(
238   p_location         number
239 )  ;
240 
241 PROCEDURE populate_plan_level(
242   p_t_bcol  in out NOCOPY CTO_SHIPSET_TBL_TYPE
243 );
244 
245 PROCEDURE populate_parent_ato(
246   p_t_bcol  in out NOCOPY CTO_SHIPSET_TBL_TYPE
247 , p_bcol_line_id in      oe_order_lines.line_id%type
248 );
249 
250 /*********************************************************************
251  API for saving state of shipset and reconstructing it on demand
252 ***********************************************************************/
253 
254 PROCEDURE save_shipset(
255   p_shipset in MRP_ATP_PUB.ATP_REC_TYP
256 , x_return_status out     varchar2
257 );
258 
259 PROCEDURE resurrect_shipset(
260   p_shipset in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
261 , p_success_flag  in     boolean
262 , x_return_status out     varchar2
263 , x_msg_count     out     number
264 , x_msg_data      out     varchar2
265 ) ;
266 
267 PROCEDURE populate_configuration_status ;
268 
269 PROCEDURE populate_visible_demand(
270   p_shipset    in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
271 , p_success_flag in boolean
272 , x_return_status out     varchar2
273 , x_msg_count     out     number
274 , x_msg_data      out     varchar2
275 );
276 
277 PROCEDURE reconstruct_shipset(
278   p_shipset    in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
279 , p_success_flag_tbl in MRP_ATP_PUB.SHIPSET_STATUS_REC_TYPE
280 , x_return_status out     varchar2
281 , x_msg_count     out     number
282 , x_msg_data      out     varchar2
283 );
284 
285 /*****************************
286  API for registering errors
287 ******************************/
288 
289 PROCEDURE register_error(
290   p_ship_set_name in varchar2
291 , p_line_id       in number
292 , p_error_code    in number
293 , p_action        in number
294 , p_status        in boolean
295 , x_return_status out varchar2
296 );
297 
298 PROCEDURE populate_error(
299   p_shipset   in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
300 , x_return_status out varchar2
301 );
302 
303 /*************************************************************
304  API for summarizing/manipulating information at Shipset Level
305 **************************************************************/
306 
307 PROCEDURE isAutoGeneratedShipset(
308   p_ship_set_name in varchar2,
309   x_auto_gen out boolean,
310   x_process_demand out boolean
311 );
312 
313 FUNCTION get_shipset_success_index(
314   p_ship_set_name in varchar2
315 )
316 return number ;
317 
318 PROCEDURE default_ship_set_name( p_shipset in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP ) ;
319 
320 
321 /***********************************************************
322 Convenience API for Displaying Information to aid debugging
323 ************************************************************/
324 
325 PROCEDURE show_contents( p_shipset_tracker IN CTO_SHIPSET_TBL_TYPE ) ;
326 
327 PROCEDURE print_shipset( p_shipset IN MRP_ATP_PUB.ATP_REC_TYP ) ;
328 
329 PROCEDURE print_shipset_capacity( p_shipset IN MRP_ATP_PUB.ATP_REC_TYP ) ;
330 
331 FUNCTION get_shipset_status(
332                             p_shipset_tbl  IN MRP_ATP_PUB.SHIPSET_STATUS_REC_TYPE,
333                             p_shipset_name IN varchar2
334                             ) RETURN VARCHAR2;
335 
336 FUNCTION get_shipset_source_flag(
337                                   p_shipset_name IN varchar2
338                                   ) RETURN BOOLEAN;
339 
340 
341 /**************************************************
342  Convenience API for record Cloning/Deletion
343 **************************************************/
344 
345 PROCEDURE remove_elements_from_bom_rec(
346  p_bom_rec  in out NOCOPY MRP_ATP_PUB.ATP_BOM_REC_TYP
347 );
348 
349 PROCEDURE remove_elements_from_atp_rec(
350  p_atp_rec  in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
351 );
352 
353 PROCEDURE Assign_Atp_Bom_Rec (
354 	p_src_atp_bom	IN OUT NOCOPY MRP_ATP_PUB.atp_bom_rec_typ,
355         p_src_location  in     number ,
356         p_des_atp_bom   IN OUT NOCOPY MRP_ATP_PUB.atp_bom_rec_typ ,
357 	x_return_status  out varchar2 ,
358         x_msg_data       out varchar2 ,
359         x_msg_count      out number ) ;
360 
361 
362 /**************************************
363 ** Doubts Resolved during Development *
364 ***************************************
365 ** 1) Can phantom model have a sourcing rule: Yes,
366 ** ignore the sourcing rule for the phantom and any items below it.
367 ** Use the sourcing org of the non phantom parent for phantom model and its descendants
368 ** 2) Chain Sourcing rules to get the final sourcing org.
369 ** 3) Provide WIP supply type in BOM structure to ATP for ATP check.
370 ** 4) Treat Multi-level and Multi-Org models in the same way.
371 */
372 
373 
374 /*
375 ** Issues Resolved
376 *****************
377 ** Need to provide expected error for null shipset name
378 ** Need to provide expected error for invalid validation org
379 **
380 */
381 
382 
383 /*
384 ** This procedure is the entry procedure to perform pre-atp related operations.
385 ** This procedure accepts one or more shipsets and provides the bom's for all components of an ATO model
386 ** in a sourced shipset. A shipset is considered to be sourced if it contains a sourced model.
387 ** Processing Steps
388 ** 1) separate shipset from group of shipsets ( on change of shipset name )
389 ** 2) evaluate one shipset at a time
390 ** 3) if error found abort any further processing, cleanup may be required
391 ** 4) if no error add enhanced shipset, supplementary BOM array to their respective collection.
392 ** 5) store the status of the shipset ( sourced, etc. ) in shipset_status structure
393 ** 6) return appropriate result to caller
394 */
395 PROCEDURE get_atp_bom(
396   p_shipset          in out   NOCOPY MRP_ATP_PUB.ATP_REC_TYP
397 , p_atp_bom_rec         out   NOCOPY MRP_ATP_PUB.ATP_BOM_REC_TYP
398 , x_return_status       out          varchar2
399 , x_msg_count           out          number
400 , x_msg_data            out          varchar2
401 )
402 is
403 l_stmt_num Number ;
404 l_return_status varchar2(1) ;
405 v_shipset          MRP_ATP_PUB.ATP_REC_TYP  ;
406 v_reduced_shipset          MRP_ATP_PUB.ATP_REC_TYP  ;
407 v_null_atp_rec		MRP_ATP_PUB.ATP_REC_TYP  ;
408 v_atp_bom_rec      MRP_ATP_PUB.ATP_BOM_REC_TYP ;
409 v_ship_set_name    varchar2(200) ;
410 v_prev_ship_set_name varchar2(200) ;
411 v_cto_shipset_count  number ;
412 v_bomrec_count  number ;
413 v_model_sourced  boolean ;
414 v_shipset_line_processed number ;
415 v_start_location      number ;
416  l_file_val varchar2(60) ;
417 v_auto_gen boolean;
418 v_process_demand boolean;
419 
420 BEGIN
421 
422    l_stmt_num := 10 ;
423 
424    x_return_status := FND_API.G_RET_STS_SUCCESS ;
425 
426    IF PG_DEBUG <> 0 THEN
427    	oe_debug_pub.add('***get_atp_bom: ' ||  ' Entered get_atp_bom' , 1);
428    	oe_debug_pub.add('get_atp_bom: ' ||  ' identifier ' || p_shipset.identifier.count || ' inventory ' ||  p_shipset.inventory_item_id.count || ' ship_set_name ' ||  p_shipset.ship_set_name.count, 3);
429 
430 	l_stmt_num := 40 ;
431 	print_shipset_capacity( p_shipset ) ;
432    	print_shipset( p_shipset ) ;
433    END IF;
434 
435    l_stmt_num := 80 ;
436 
437    /* initialize session specific variables */
438    initialize_session_globals ;
439 
440    gUserId := nvl(fnd_global.user_id, -1);
441    gLoginId := nvl(fnd_global.login_id, -1);
442 
443    /* get assignment set and check its validity */
444    initialize_assignment_set( x_return_status ) ;
445 
446    IF( x_return_status = FND_API.G_RET_STS_ERROR ) THEN
447        FOR l_error_loc IN
448            p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
449        LOOP
450            p_shipset.error_code(l_error_loc) := 65 ;
451        END LOOP ;
452        RAISE FND_API.G_EXC_ERROR ;
453    ELSIF( x_return_status = FND_API.G_RET_STS_UNEXP_ERROR ) THEN
454        RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
455    END IF ;
456 
457    l_stmt_num := 90 ;
458 
459    FOR v_init IN p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
460    LOOP
461       IF ( p_shipset.ship_set_name(v_init ) is null
462 		AND p_shipset.arrival_set_name(v_init ) is null) THEN
463 
464 	  IF PG_DEBUG <> 0 THEN
465 	  	oe_debug_pub.add('get_atp_bom: ' ||  'Both ship set and arrival set are null', 2);
466 	  END IF;
467 
468           p_shipset.ship_set_name(v_init) := p_shipset.identifier(v_init) ;
469 
470           g_auto_generated_shipset(g_auto_generated_shipset.count + 1 ).set_name := to_char( p_shipset.identifier(v_init)) ;
471 	  g_auto_generated_shipset(g_auto_generated_shipset.count ).process_demand := FALSE;
472 
473       ELSIF ( p_shipset.ship_set_name(v_init ) is null
474 		AND p_shipset.arrival_set_name(v_init ) is not null) THEN
475 
476 	  IF PG_DEBUG <> 0 THEN
477 	  	oe_debug_pub.add('get_atp_bom: ' ||  'Arrival set is not null', 2);
478 	  END IF;
479 
480           p_shipset.ship_set_name(v_init) := p_shipset.arrival_set_name(v_init) ;
481 
482           g_auto_generated_shipset(g_auto_generated_shipset.count + 1 ).set_name := p_shipset.arrival_set_name(v_init);
483 	  g_auto_generated_shipset(g_auto_generated_shipset.count ).process_demand := TRUE;
484 
485       END IF ;
486 
487    END LOOP ;
488 
489    IF PG_DEBUG <> 0 THEN
490    	oe_debug_pub.add('get_atp_bom: ' ||  'Printing p_shipset after populating set name' , 2 );
491 	print_shipset( p_shipset ) ;
492    END IF;
493 
494    IF PG_DEBUG <> 0 THEN
495    	oe_debug_pub.add('get_atp_bom: ' ||  'Going to print g_auto_generated_shipset ' || g_auto_generated_shipset.count , 2 );
496    END IF;
497 
498    FOR auto_gen_count IN 1..g_auto_generated_shipset.count
499    LOOP
500       	IF PG_DEBUG <> 0 THEN
501       		oe_debug_pub.add('get_atp_bom: ' ||  'Shipset name::'||g_auto_generated_shipset(auto_gen_count).set_name, 2);
502       	END IF;
503 	IF g_auto_generated_shipset(auto_gen_count).process_demand THEN
504 		IF PG_DEBUG <> 0 THEN
505 			oe_debug_pub.add('get_atp_bom: ' ||  'Process Demand flag::TRUE', 2);
506 		END IF;
507 	ELSE
508 		IF PG_DEBUG <> 0 THEN
509 			oe_debug_pub.add('get_atp_bom: ' ||  'Process Demand flag::FALSE', 2);
510 		END IF;
511 	END IF;
512    END LOOP ;
513 
514 
515    l_stmt_num := 100 ;
516    v_prev_ship_set_name := p_shipset.ship_set_name( nvl( p_shipset.ship_set_name.first, p_shipset.identifier.first)  ) ;
517    v_ship_set_name := p_shipset.ship_set_name( nvl( p_shipset.ship_set_name.first, p_shipset.identifier.first)  ) ;
518 
519    v_shipset_line_processed := 0;
520    v_start_location:= 1;
521 
522    IF PG_DEBUG <> 0 THEN
523    	oe_debug_pub.add('get_atp_bom: ' ||  'Iterate through each shipset element and evaluate shipset on change of shipset name ' , 1);
524    END IF;
525 
526    l_stmt_num := 120 ;
527 
528    FOR i IN p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
529    LOOP
530       v_prev_ship_set_name := v_ship_set_name ;
531       v_ship_set_name := nvl( p_shipset.ship_set_name(i), p_shipset.identifier(i)) ;
532 
533       l_stmt_num := 140 ;
534 
535       IF( v_ship_set_name = v_prev_ship_set_name ) THEN
536 
537          /*
538          ** copy p_shipset into v_shipset and evaluate one shipset at a time
539          */
540          l_stmt_num := 160 ;
541 
542          MRP_ATP_PVT.assign_atp_input_rec(
543             p_shipset,
544             i,
545             v_shipset,
546             l_return_status );
547 
548          v_shipset_line_processed := v_shipset_line_processed + 1;
549 
550       ELSE
551 
552          IF PG_DEBUG <> 0 THEN
553          	oe_debug_pub.add('get_atp_bom: ' ||  'Shipset name ' || v_ship_set_name , 1 );
554          	oe_debug_pub.add('get_atp_bom: ' ||  'Previous shipset name ' || v_prev_ship_set_name , 1 );
555          END IF;
556 
557          /*
558          ** call evaluate shipset for accumulated components
559          */
560 
561          IF PG_DEBUG <> 0 THEN
562          	oe_debug_pub.add('get_atp_bom: ' || 'Going to evaluate shipset ' ,1);
563          END IF;
564 
565          l_stmt_num := 180 ;
566 
567          /* add name of shipset to shipset table  */
568 
569          IF( v_shipset.ship_set_name.first is not null ) THEN
570             g_shipset_status_tbl(g_shipset_status_tbl.count + 1 ).ship_set_name := v_shipset.ship_set_name( v_shipset.ship_set_name.first );
571 
572             isAutoGeneratedShipset(
573 		v_shipset.ship_set_name( v_shipset.ship_set_name.first),
574 		v_auto_gen,
575 		v_process_demand);
576 
577 	    IF PG_DEBUG <> 0 THEN
578 	    	oe_debug_pub.add('get_atp_bom: ' || 'isAutoGeneratedShipset returned following values for shipset::'||v_shipset.ship_set_name( v_shipset.ship_set_name.first), 3);
579 	    END IF;
580 	    IF v_auto_gen THEN
581 		IF PG_DEBUG <> 0 THEN
582 			oe_debug_pub.add('get_atp_bom: ' || 'v_auto_gen::TRUE', 3);
583 		END IF;
584 	    ELSE
585 		IF PG_DEBUG <> 0 THEN
586 			oe_debug_pub.add('get_atp_bom: ' || 'v_auto_gen::FALSE', 3);
587 		END IF;
588 	    END IF;
589 	    IF v_process_demand THEN
590 		IF PG_DEBUG <> 0 THEN
591 			oe_debug_pub.add('get_atp_bom: ' || 'v_process_demand::TRUE', 3);
592 		END IF;
593 	    ELSE
594 		IF PG_DEBUG <> 0 THEN
595 			oe_debug_pub.add('get_atp_bom: ' || 'v_process_demand::FALSE', 3);
596 		END IF;
597 	    END IF;
598 
599 	    g_shipset_status_tbl(g_shipset_status_tbl.count).auto_generated := v_auto_gen;
600 	    g_shipset_status_tbl(g_shipset_status_tbl.count).process_demand := v_process_demand;
601 
602          END IF ;
603 
604          l_stmt_num := 200 ;
605 
606          evaluate_shipset(
607             v_shipset
608          , g_shipset_status_tbl(g_shipset_status_tbl.count).auto_generated
609 	 , g_shipset_status_tbl(g_shipset_status_tbl.count).process_demand
610          , v_atp_bom_rec
611          , v_model_sourced
612          , l_return_status
613          ) ;
614 
615          IF PG_DEBUG <> 0 THEN
616          	oe_debug_pub.add('get_atp_bom: ' || 'Evaluate shipset returned ' || l_return_status  ,1);
617          END IF;
618 
619 
620          /*
621          ** perform all cleanup operations in case of an expected error
622          ** no cleanup is required for unexpected error
623          */
624          IF( l_return_status = FND_API.G_RET_STS_ERROR ) THEN
625 
626             l_stmt_num := 205 ;
627 
628             /*
629             ** copy enhanced shipset returned by evaluate_shipset in to v_reduced_shipset
630             */
631             for k in v_shipset.ship_set_name.first..v_shipset.ship_set_name.last
632             LOOP
633                MRP_ATP_PVT.assign_atp_input_rec(
634                  v_shipset,
635                  k,
636                  v_reduced_shipset,
637                  l_return_status );
638             END LOOP ;
639 
640             l_stmt_num := 210 ;
641 
642             --remove_elements_from_atp_rec( p_shipset ) ;
643 	    p_shipset := v_null_atp_rec;
644 
645             l_stmt_num := 215 ;
646 
647             /*
648             ** add contents of v_reduced_shipset for current shipset
649             ** to accumulated collection of enhanced shipsets
650             */
651             for k in v_shipset.ship_set_name.first..v_shipset.ship_set_name.last
652             LOOP
653              MRP_ATP_PVT.assign_atp_input_rec(
654                  v_reduced_shipset,
655                  k,
656                  p_shipset,
657                  l_return_status );
658             END LOOP ;
659 
660             RAISE FND_API.G_EXC_ERROR ;
661 
662          ELSIF( l_return_status = FND_API.G_RET_STS_UNEXP_ERROR ) THEN
663 
664               RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
665          /*
666          ** END of cleanup operation for expected error
667          */
668          END IF ;
669 
670          l_stmt_num := 217 ;
671          /*
672          ** store information for model sourcing and start location in shipset status table
673          */
674          IF( v_shipset.ship_set_name.first is not null ) THEN
675            IF( v_model_sourced ) THEN
676              g_shipset_status_tbl(g_shipset_status_tbl.count ).model_sourced := TRUE ;
677              g_shipset_status_tbl(g_shipset_status_tbl.count ).start_location := v_start_location ;
678            ELSE
679              g_shipset_status_tbl(g_shipset_status_tbl.count ).model_sourced := FALSE ;
680              g_shipset_status_tbl(g_shipset_status_tbl.count ).start_location := v_start_location ;
681            END IF ;
682          END IF ;
683 
684          l_stmt_num := 220 ;
685 
686          IF( v_atp_bom_rec.assembly_identifier.count > 0 )
687          THEN
688              /*
689              ** accumulate bom provided
690              */
691              l_stmt_num := 240 ;
692 
693              FOR j IN v_atp_bom_rec.assembly_identifier.first..v_atp_bom_rec.assembly_identifier.last
694              LOOP
695 
696                 l_stmt_num := 260 ;
697 
698                 IF( v_atp_bom_rec.assembly_identifier.exists(j) ) THEN
699 
700                      l_stmt_num := 280 ;
701                      assign_atp_bom_rec(
702                                      v_atp_bom_rec
703                                    , j
704                                    , p_atp_bom_rec
705                                    , x_return_status
706                                    , x_msg_count
707                                    , x_msg_data ) ;
708                 END IF ;
709              END LOOP ;
710           END IF ;
711 
712           /*
713           ** copy transformed v_shipset into v_reduced_shipset
714           */
715           l_stmt_num := 300 ;
716 
717           FOR k IN v_shipset.ship_set_name.first..v_shipset.ship_set_name.last
718           LOOP
719 
720               l_stmt_num := 320 ;
721 
722               MRP_ATP_PVT.assign_atp_input_rec(
723                    v_shipset,
724                    k,
725                    v_reduced_shipset,
726                    l_return_status );
727 
728           END LOOP ;
729 
730           /*
731           ** remove bom records to initialize it
732           */
733           l_stmt_num := 340 ;
734 
735           remove_elements_from_bom_rec( v_atp_bom_rec ) ;
736 
737           /*
738           ** remove v_shipset records to intialize it
739           */
740           l_stmt_num := 360 ;
741 
742           --remove_elements_from_atp_rec( v_shipset ) ;
743 	  v_shipset := v_null_atp_rec;
744 
745           /*
746           ** copy current element encountered on change of shipset from p_shipset into v_shipset
747           */
748           l_stmt_num := 380 ;
749 
750           MRP_ATP_PVT.assign_atp_input_rec(
751             p_shipset,
752             i,
753             v_shipset,
754             l_return_status );
755 
756           v_shipset_line_processed := v_shipset_line_processed + 1;
757           v_start_location := v_shipset_line_processed ;
758 
759        END IF ;
760 
761        l_stmt_num := 400 ;
762 
763        g_cto_shipset.delete ;
764        g_cto_sparse_shipset.delete ;
765 
766    END LOOP ;
767 
768 
769    /*
770    ** repeat all the above iterations for the last shipset
771    ** if there is only one shipset to be processed this
772    ** is where all the processing takes place
773    */
774    l_stmt_num := 420 ;
775 
776    IF( v_shipset.ship_set_name.first is not null ) THEN
777 
778        g_shipset_status_tbl(g_shipset_status_tbl.count + 1 ).ship_set_name :=
779            v_shipset.ship_set_name( v_shipset.ship_set_name.first ) ;
780 
781 	isAutoGeneratedShipset(
782 		v_shipset.ship_set_name( v_shipset.ship_set_name.first),
783 		v_auto_gen,
784 		v_process_demand);
785 
786 	IF PG_DEBUG <> 0 THEN
787 		oe_debug_pub.add('get_atp_bom: ' || 'isAutoGeneratedShipset returned following values for shipset::'||v_shipset.ship_set_name( v_shipset.ship_set_name.first), 3);
788 	END IF;
789 
790 	IF v_auto_gen THEN
791 		IF PG_DEBUG <> 0 THEN
792 			oe_debug_pub.add('get_atp_bom: ' || 'v_auto_gen::TRUE', 3);
793 		END IF;
794 	ELSE
795 		IF PG_DEBUG <> 0 THEN
796 			oe_debug_pub.add('get_atp_bom: ' || 'v_auto_gen::FALSE', 3);
797 		END IF;
798 	END IF;
799 	IF v_process_demand THEN
800 		IF PG_DEBUG <> 0 THEN
801 			oe_debug_pub.add('get_atp_bom: ' || 'v_process_demand::TRUE', 3);
802 		END IF;
803 	ELSE
804 		IF PG_DEBUG <> 0 THEN
805 			oe_debug_pub.add('get_atp_bom: ' || 'v_process_demand::FALSE', 3);
806 		END IF;
807 	END IF;
808 
809 	g_shipset_status_tbl(g_shipset_status_tbl.count).auto_generated := v_auto_gen;
810 	g_shipset_status_tbl(g_shipset_status_tbl.count).process_demand := v_process_demand;
811 
812    END IF;
813 
814    l_stmt_num := 440 ;
815 
816    evaluate_shipset(
817             v_shipset
818           , g_shipset_status_tbl(g_shipset_status_tbl.count).auto_generated
819 	  , g_shipset_status_tbl(g_shipset_status_tbl.count).process_demand
820           , v_atp_bom_rec
821           , v_model_sourced
822           , l_return_status
823           ) ;
824 
825    IF PG_DEBUG <> 0 THEN
826    	oe_debug_pub.add('get_atp_bom: ' || 'Evaluate shipset returned ' || l_return_status  ,1);
827    END IF;
828 
829    IF( l_return_status = FND_API.G_RET_STS_ERROR ) THEN
830 
831             l_stmt_num := 445 ;
832 
833             FOR k IN v_shipset.ship_set_name.first..v_shipset.ship_set_name.last
834             LOOP
835                MRP_ATP_PVT.assign_atp_input_rec(
836                  v_shipset,
837                  k,
838                  v_reduced_shipset,
839                  l_return_status );
840             END LOOP ;
841 
842             l_stmt_num := 450 ;
843 
844             --remove_elements_from_atp_rec( p_shipset ) ;
845 	    p_shipset := v_null_atp_rec;
846 
847             l_stmt_num := 455 ;
848 
849             FOR k IN v_reduced_shipset.ship_set_name.first..v_reduced_shipset.ship_set_name.last
850             LOOP
851              MRP_ATP_PVT.assign_atp_input_rec(
852                  v_reduced_shipset,
853                  k,
854                  p_shipset,
855                  l_return_status );
856             END LOOP ;
857 
858             IF PG_DEBUG <> 0 THEN
859 		print_shipset( p_shipset ) ;
860              	oe_debug_pub.add('get_atp_bom: ' ||  ' going to raise expected error ' , 1 );
861             END IF;
862 
863             RAISE FND_API.G_EXC_ERROR ;
864 
865    ELSIF( l_return_status = FND_API.G_RET_STS_UNEXP_ERROR ) THEN
866               RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
867    END IF ;
868 
869    l_stmt_num := 460 ;
870 
871    /* add sourcing status of shipset to sourced shipsets */
872    IF( v_shipset.ship_set_name.first is not null ) THEN
873 
874      IF( v_model_sourced ) THEN
875        g_shipset_status_tbl(g_shipset_status_tbl.count ).model_sourced := TRUE ;
876        g_shipset_status_tbl(g_shipset_status_tbl.count ).start_location := v_start_location ;
877       ELSE
878        g_shipset_status_tbl(g_shipset_status_tbl.count ).model_sourced := FALSE ;
879        g_shipset_status_tbl(g_shipset_status_tbl.count ).start_location := v_start_location ;
880       END IF ;
881    END IF ;
882 
883    l_stmt_num := 480 ;
884 
885    IF( v_atp_bom_rec.assembly_identifier.count > 0 ) THEN
886    /*
887    ** copy atp_bom_rec into p_atp_bom_rec
888    */
889    IF PG_DEBUG <> 0 THEN
890    	oe_debug_pub.add('get_atp_bom: ' ||  ' Copy atp_bom_rec into p_atp_bom_rec ' || v_atp_bom_rec.assembly_identIFier.count , 2);
891    END IF;
892 
893    l_stmt_num := 500 ;
894 
895    FOR j IN v_atp_bom_rec.assembly_identifier.first..v_atp_bom_rec.assembly_identifier.last
896    LOOP
897       IF( v_atp_bom_rec.assembly_identifier.exists(j) ) THEN
898 
899           l_stmt_num := 520 ;
900           assign_atp_bom_rec(
904                        , x_return_status
901                          v_atp_bom_rec
902                        , j
903                        , p_atp_bom_rec
905                        , x_msg_count
906                        , x_msg_data ) ;
907       END IF ;
908    END LOOP ;
909 
910    END IF ;
911 
912    /*
913    ** copy transformed v_shipset into v_reduced_shipset
914    */
915    IF PG_DEBUG <> 0 THEN
916    	oe_debug_pub.add('get_atp_bom: ' ||  'Copy transformed v_shipset into v_reduced_shipset ' , 2 );
917    END IF;
918 
919    l_stmt_num := 540 ;
920 
921    FOR k IN v_shipset.ship_set_name.first..v_shipset.ship_set_name.last
922    LOOP
923              MRP_ATP_PVT.assign_atp_input_rec(
924                  v_shipset,
925                  k,
926                  v_reduced_shipset,
927                  l_return_status );
928    END LOOP ;
929 
930    l_stmt_num := 560 ;
931 
932    /* remove elements from p_shipset to initialize it */
933    /* For each new element added to the rec by ATP, we need
934       to modify this API. Instead, we are nullifying the atp rec */
935    --remove_elements_from_atp_rec( p_shipset ) ;
936    p_shipset := v_null_atp_rec;
937 
938    /*
939    ** Assign shipset to Send reduced shipset back to calling module
940    */
941 
942    l_stmt_num := 580 ;
943 
944    IF PG_DEBUG <> 0 THEN
945    	oe_debug_pub.add('get_atp_bom: ' ||  'v_reduced_shipset count ' || v_reduced_shipset.ship_set_name.count , 1 );
946    END IF;
947 
948    FOR k IN v_reduced_shipset.ship_set_name.first..v_reduced_shipset.ship_set_name.last
949    LOOP
950              MRP_ATP_PVT.assign_atp_input_rec(
951                  v_reduced_shipset,
952                  k,
953                  p_shipset,
954                  l_return_status );
955    END LOOP ;
956 
957   --
958   -- The auto-generated shipset names should be transparent to ATP. Hence,
959   -- we null them out before sending the reduced shipset to ATP. We will
960   -- re-generate them in the post-ATP call (bug 2598745)
961   --
962   IF PG_DEBUG <> 0 THEN
963   	oe_debug_pub.add('get_atp_bom: ' ||  'Going to null out auto-generated shipset names in pre-ATP call ' ,2 );
964   END IF;
965 
966   FOR v_auto_shipset IN p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
967   LOOP
968 
969       isAutoGeneratedShipset(
970 	p_shipset.ship_set_name(v_auto_shipset),
971 	v_auto_gen,
972 	v_process_demand);
973 
974       IF v_auto_gen THEN
975 	  IF PG_DEBUG <> 0 THEN
976 	  	oe_debug_pub.add('get_atp_bom: ' ||  'Nulled out shipset name '||p_shipset.ship_set_name(v_auto_shipset) ,3 );
977 	  END IF;
978           p_shipset.ship_set_name(v_auto_shipset) := null ;
979       END IF ;
980 
981   END LOOP ;	-- bug 2598745
982 
983 
984    /*
985    ** remove bom records to intialize it
986    */
987    l_stmt_num := 600 ;
988    remove_elements_from_bom_rec( v_atp_bom_rec ) ;
989 
990    /*
991    ** remove v_shipset records to intialize it
992    */
993    l_stmt_num := 620 ;
994    --remove_elements_from_atp_rec( v_shipset ) ;
995    v_shipset := v_null_atp_rec;
996 
997    /*
998    ** copy cto_shipset into final_cto_shipset for 2nd phase
999    */
1000    l_stmt_num := 640 ;
1001 
1002    IF PG_DEBUG <> 0 THEN
1003    	oe_debug_pub.add('get_atp_bom: ' ||  'Printing g_final_cto_shipset ' , 2 );
1004 	l_stmt_num := 660 ;
1005 	show_contents(g_final_cto_shipset) ;
1006    END IF;
1007 
1008    g_cto_shipset.delete ;
1009    g_cto_sparse_shipset.delete ;
1010 
1011    IF PG_DEBUG <> 0 THEN
1012    	oe_debug_pub.add('get_atp_bom: ' ||  ' Printing shipset before finishing get_atp_bom ' , 2 );
1013 	l_stmt_num := 680 ;
1014 	print_shipset( p_shipset ) ;
1015    END IF;
1016 
1017    x_return_status := FND_API.G_RET_STS_SUCCESS ;
1018 
1019    IF PG_DEBUG <> 0 THEN
1020    	oe_debug_pub.add('get_atp_bom: ' ||  ' Done get atp bom' , 1 );
1021    END IF;
1022 
1023 EXCEPTION
1024    WHEN FND_API.G_EXC_ERROR THEN
1025         IF PG_DEBUG <> 0 THEN
1026         	oe_debug_pub.add('get_atp_bom: ' || 'GET_ATP_BOM_PUB::exp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
1027 		oe_debug_pub.add('get_atp_bom: ' ||  ' Printing shipset to show exp error ' , 2 );
1028         END IF;
1029         x_return_status := FND_API.G_RET_STS_ERROR;
1030 
1031         print_shipset( p_shipset ) ;
1032 
1033         FND_MSG_PUB.Count_And_Get(
1034           p_count => x_msg_count
1035         , p_data  => x_msg_data
1036         );
1037 
1038    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1039         IF PG_DEBUG <> 0 THEN
1040         	oe_debug_pub.add('get_atp_bom: ' || 'GET_ATP_BOM_PUB::unexp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
1041         END IF;
1042         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1043         FND_MSG_PUB.Count_And_Get(
1044           p_count => x_msg_count
1045         , p_data  => x_msg_data
1046         );
1047 
1048    WHEN OTHERS THEN
1049         IF PG_DEBUG <> 0 THEN
1050         	oe_debug_pub.add('get_atp_bom: ' || 'GET_ATP_BOM_PUB::others::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
1051         END IF;
1052         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1056            , 'GET_ATP_BOM_PUB'
1053         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1054            FND_MSG_PUB.Add_Exc_Msg(
1055              G_PKG_NAME
1057            );
1058         END IF;
1059         FND_MSG_PUB.Count_And_Get(
1060           p_count => x_msg_count
1061         , p_data  => x_msg_data
1062         );
1063 
1064 END get_atp_bom ;
1065 
1066 
1067 
1068 /*
1069 ** This procedure evaluates the shipset for determining model source.
1070 ** This evaluation is done with the help of preprocessing procedures
1071 ** depending on the origin of the call( CZ or OM )
1072 ** This procedure returns a reduced shipset if the shipset is sourced.
1073 ** Processing Steps
1074 ** 1) evaluate source of call ( CZ or OM ) and type of action ( enquiry, schedule, reschedule, cancel, delete )
1075 ** 2) Retrieve relevant information required for processing according to the request
1076 ** 3) Return if no models found in the shipset
1077 ** 4) Filter out mandatory components(permanent) and standard/ATO items( temporary ) for model processing.
1078 ** 5) Populate plan level
1079 ** 6) Populate Parent ATO
1080 ** 7) Unscramble the data and reorder it
1081 ** 8) Evaluate sourcing for the entire shipset
1082 ** 9) If model not sourced check step 10 else check step 11
1083 ** 10) if model not multilevel return without any further processing
1084 ** 11) Populate Sparse shipset for BOM creation
1085 ** 12) Create BOM for as model is Multi-level/Multi-Org
1086 ** 13) Reinstate WIP information for phantom models and option classes to non phantom in BOM structure as they
1087 **     have been exploded in the Pre-ATP process. ATP should not be exploding these models and option classes again.
1088 ** 14) Identify components under ATO models to be eliminated from p_shipset as they are part of sourced shipset
1089 ** 15) Start eliminating components under ATO models from p_shipset
1090 ** 16) Send Enhanced shipset back to caller
1091 *************
1092 ** Caution **
1093 ** A shipset may be SMC( Ship Model Complete ) or NonSMC. In the case of NonSMC the PTO model
1094 ** may not be passed with the ATO models under it. The code needs to handle both scenarios from a
1095 ** Top Down Model processing perspective.
1096 */
1097 PROCEDURE evaluate_shipset(
1098   p_shipset            in out NOCOPY  MRP_ATP_PUB.ATP_REC_TYP
1099 , p_auto_generated     in   boolean
1100 , p_process_demand     in   boolean
1101 , p_atp_bom_rec        out  NOCOPY MRP_ATP_PUB.ATP_BOM_REC_TYP
1102 , p_model_sourced      out  boolean
1103 , x_return_status      out varchar2
1104 )
1105 is
1106 v_orig_shipset_tracker     CTO_SHIPSET_TBL_TYPE;
1107 v_raw_shipset_tracker     CTO_SHIPSET_TBL_TYPE ;
1108 v_dummy_shipset_tracker     CTO_SHIPSET_TBL_TYPE ;
1109 v_temp_tracker     CTO_SHIPSET_TYPE ;
1110 v_basis_qty          number ;
1111 v_phantom            boolean ;
1112 v_procured           boolean := FALSE;
1113 v_shipset_contains_models  boolean ;
1114 v_curr_line_id        number ;
1115 v_top_model_line_id        number ;
1116 l_validation_org      number ;
1117 l_return_status       varchar2(1) ;
1118 v_call_from           varchar2(10 ) ;
1119 v_action_code              varchar2(20) ; /* values 'RESCHED' , 'CANCEL' */
1120 v_action              number ;
1121 l_msg_data            varchar2(2000) ;
1122 l_msg_count           number ;
1123 x_msg_data            varchar2(2000) ;
1124 x_msg_count           number ;
1125 l_stmt_num            number ;
1126 v_cto_shipset_flag   FLAG_TBL_TYPE;
1127 v_shipset            MRP_ATP_PUB.ATP_REC_TYP ;
1128 v_shipset_index      number ;
1129 TYPE TABNUM is TABLE of NUMBER index by binary_integer ;
1130 l_pal_array          TABNUM ;
1131 l_pal_count          number ;
1132 l_pal_found          boolean ;
1133 v_lcs_size           number ;
1134 v_top_model_exists   boolean;
1135 v_multilevel_shipset boolean := FALSE ; /* BUG#1874380 */
1136 
1137 /* start 2723674 - Variable declaration */
1138 v_mlmo_ato_line_id      number;
1139 d_line_id		number; 	-- debug
1140 d_parent_ato_line_id 	number;		-- debug
1141 d_bom_item_type		number;		-- debug
1142 d_wip_supply_type 	number;		-- debug
1143 d_ato_line_id		number;		-- debug
1144 dj_line_id		number; 	-- debug
1145 dj_ato_line_id		number;		-- debug
1146 dj_mlmo_flag		varchar2(1);	-- debug
1147 dd_mlmo_flag		varchar2(1);	-- debug
1148 /* end 2723674 - Variable declaration */
1149 
1150 BEGIN
1151 
1152 
1153    l_stmt_num := 10 ;
1154 
1155    p_model_sourced := FALSE ;
1156    x_return_status := FND_API.G_RET_STS_SUCCESS ;
1157    local_cto_shipset.delete ;
1158 
1159    /*
1160    ** evaluate call from OM or CZ
1161    */
1162    l_stmt_num := 40 ;
1163 
1164    IF PG_DEBUG <> 0 THEN
1165    	oe_debug_pub.add('evaluate_shipset: ' ||  'Calling module is:: '|| to_char( p_shipset.calling_module.first ) , 2 );
1166    	oe_debug_pub.add('evaluate_shipset: ' ||  'Shipset count is::' || to_char( p_shipset.calling_module.count ) , 4 );
1167 	print_shipset(p_shipset) ;
1168    END IF;
1169 
1170 
1171    IF( p_shipset.calling_module( p_shipset.calling_module.first) = 660 ) THEN
1172        v_call_from := 'OM' ;
1173    ELSIF( p_shipset.calling_module( p_shipset.calling_module.first) = 708 ) THEN
1174        v_call_from := 'CZ' ;
1175    END IF ;
1176 
1177    /*
1178    ** evaluate action
1179    */
1180    l_stmt_num := 60 ;
1181    IF( p_shipset.Action(p_shipset.Action.first) = 100 ) THEN
1182        v_action_code  := 'ATP' ;
1186        v_action := 110 ;
1183        v_action := 100 ;
1184    ELSIF( p_shipset.Action(p_shipset.Action.first) = 110 ) THEN
1185        v_action_code := 'SCHED' ;
1187    ELSIF( p_shipset.Action(p_shipset.Action.first) = 120 ) THEN
1188        v_action_code := 'RESCHED' ;
1189        v_action := 120 ;
1190    END IF;
1191 
1192    IF PG_DEBUG <> 0 THEN
1193    	oe_debug_pub.add('evaluate_shipset: ' || 'Action is:: ' || v_action_code , 2 );
1194    END IF;
1195 
1196    /*
1197    ** populate om or cz shipset
1198    */
1199     l_stmt_num := 80 ;
1200    IF( v_call_from = 'OM'  )
1201    THEN
1202        IF PG_DEBUG <> 0 THEN
1203        	oe_debug_pub.add ('evaluate_shipset: ' ||  'Calling populate_om_shipset ' , 2 );
1204        END IF;
1205 
1206        l_stmt_num := 100  ;
1207        populate_om_shipset(
1208           p_shipset
1209         , v_orig_shipset_tracker
1210         , v_shipset_contains_models
1211         , x_return_status
1212         );
1213 
1214         IF PG_DEBUG <> 0 THEN
1215         	oe_debug_pub.add('evaluate_shipset: ' ||  'Returned from populate om shipset::'||x_return_status, 4);
1216         END IF;
1217 
1218         l_stmt_num := 120 ;
1219 
1220         IF( x_return_status = FND_API.G_RET_STS_ERROR ) THEN
1221             RAISE FND_API.G_EXC_ERROR ;
1222         ELSIF( x_return_status = FND_API.G_RET_STS_UNEXP_ERROR ) THEN
1223             RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1224         END IF ;
1225 
1226    ELSIF ( v_call_from = 'CZ' ) THEN
1227        IF PG_DEBUG <> 0 THEN
1228        	oe_debug_pub.add ('evaluate_shipset: ' ||  'Calling populate_cz_shipset ' , 2 );
1229        END IF;
1230 
1231        l_stmt_num := 160 ;
1232        populate_cz_shipset(
1233          p_shipset
1234        , v_orig_shipset_tracker
1235        , v_shipset_contains_models
1236        , x_return_status
1237        );
1238 
1239         IF PG_DEBUG <> 0 THEN
1240         	oe_debug_pub.add('evaluate_shipset: ' ||  'Returned from populate cz shipset:: '||x_return_status, 4);
1241         END IF;
1242        l_stmt_num := 180 ;
1243 
1244        IF( x_return_status = FND_API.G_RET_STS_ERROR ) THEN
1245             RAISE FND_API.G_EXC_ERROR ;
1246         ELSIF( x_return_status = FND_API.G_RET_STS_UNEXP_ERROR ) THEN
1247             RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1248         END IF ;
1249 
1250    END IF ;
1251 
1252    l_stmt_num := 190 ;
1253 
1254    IF( v_orig_shipset_tracker.count > 0 ) THEN
1255       FOR i IN v_orig_shipset_tracker.first..v_orig_shipset_tracker.last
1256       LOOP
1257          IF( v_orig_shipset_tracker.exists(i) ) THEN
1258              v_orig_shipset_tracker(i).auto_generated := p_auto_generated ;
1259 	     v_orig_shipset_tracker(i).process_demand := p_process_demand ;
1260          END IF ;
1261       END LOOP ;
1262    END IF ;
1263 
1264    show_contents( v_orig_shipset_tracker ) ;
1265 
1266    l_stmt_num := 200 ;
1267 
1268    /*
1269    ** return IF no models or config items in ship set
1270    */
1271    IF( v_shipset_contains_models = FALSE )
1272    THEN
1273        /*
1274        ** before reducing shipset populate the error codes encountered
1275        */
1276 
1277        IF PG_DEBUG <> 0 THEN
1278        	oe_debug_pub.add('evaluate_shipset: ' ||  'No models in shipset, going to populate error ' , 2  );
1279        END IF;
1280 
1281        l_stmt_num := 220 ;
1282 
1283        populate_error( p_shipset , l_return_status ) ;
1284 
1285        /*
1286        ** copy the shipset to global shipset
1287        */
1288        IF PG_DEBUG <> 0 THEN
1289        	oe_debug_pub.add('evaluate_shipset: ' ||  'Going to save shipset into global shipset ' , 2  );
1290        END IF;
1291 
1292        l_stmt_num := 240 ;
1293        save_shipset( p_shipset , x_return_status ) ;
1294        return ;
1295    END IF ;
1296 
1297    /*
1298    ** copy original shipset tracker excluding mandatory and stand alone standard items
1299    ** into v_raw_shipset_tracker.
1300    ** position tracker contents by lineid location for sparse index traversal
1301    */
1302    l_stmt_num := 260 ;
1303 
1304    FOR j IN v_orig_shipset_tracker.first..v_orig_shipset_tracker.last
1305    LOOP
1306       IF PG_DEBUG <> 0 THEN
1307       	oe_debug_pub.add ('evaluate_shipset: ' ||  v_orig_shipset_tracker(j).line_id || ' IT ' ||
1308                             v_orig_shipset_tracker(j).bom_item_type || ' ID ' ||
1309                             v_orig_shipset_tracker(j).base_item_id || ' WF  ' ||
1310                             v_orig_shipset_tracker(j).build_in_wip_flag || ' RF ' ||
1311                             v_orig_shipset_tracker(j).replenish_to_order_flag , 5 );
1312       END IF;
1313 
1314       l_stmt_num := 280 ;
1315 
1316       /*
1317       ** filter out mandatory components and ato items
1318       */
1319       IF( NOT (    ( v_orig_shipset_tracker(j).mandatory_component AND
1320                      v_orig_shipset_tracker(j).mandatory_component is not null
1321                    )
1322                 OR ( v_orig_shipset_tracker(j).bom_item_type = '4' AND
1323                      v_orig_shipset_tracker(j).base_item_id is not null AND
1324                      v_orig_shipset_tracker(j).build_in_wip_flag = 'Y' AND
1325                      v_orig_shipset_tracker(j).replenish_to_order_flag = 'Y'
1326                    )
1330               )
1327                 OR ( v_orig_shipset_tracker(j).bom_item_type = '4' AND
1328                      v_orig_shipset_tracker(j).top_model_line_id is null
1329                    )
1331          )
1332       THEN
1333           l_stmt_num := 300 ;
1334 
1335           v_raw_shipset_tracker(v_orig_shipset_tracker(j).line_id) := v_orig_shipset_tracker(j) ;
1336 
1337       END IF;
1338    END LOOP ;
1339 
1340 
1341    /*
1342    ** populate_plan_level for the ship set
1343    */
1344    IF PG_DEBUG <> 0 THEN
1345    	oe_debug_pub.add ('evaluate_shipset: ' ||  'Going to call populate plan level, shipset count is ' ||v_raw_shipset_tracker.count , 2 );
1346    END IF;
1347    l_stmt_num := 320 ;
1348 
1349    populate_plan_level(
1350      v_raw_shipset_tracker
1351    ) ;
1352 
1353    l_stmt_num := 340 ;
1354 
1355    show_contents(
1356      v_raw_shipset_tracker
1357    ) ;
1358 
1359    /*
1360    ** populate_parent_ato for the ship set
1361    */
1362    IF PG_DEBUG <> 0 THEN
1363    	oe_debug_pub.add('evaluate_shipset: ' ||  'Going to call populate parent ato ', 2  );
1364    END IF;
1365 
1366    l_stmt_num := 360 ;
1367 
1368    populate_parent_ato(
1369      v_raw_shipset_tracker
1370    , null ) ;
1371    IF PG_DEBUG <> 0 THEN
1372    	oe_debug_pub.add('evaluate_shipset: ' ||  'Populated parent ato ' , 4  );
1373    END IF;
1374 
1375    l_stmt_num := 380 ;
1376 
1377    show_contents(
1378      v_raw_shipset_tracker
1379    ) ;
1380 
1381    /*
1382    ** unscramble the data and reorder it
1383    */
1384    l_stmt_num := 400 ;
1385 
1386    FOR i IN v_raw_shipset_tracker.first..v_raw_shipset_tracker.last
1387    LOOP
1388       IF( v_raw_shipset_tracker.exists(i) )
1389       THEN
1390           v_lcs_size := local_cto_shipset.count + 1 ;
1391           local_cto_shipset(v_lcs_size ) := v_raw_shipset_tracker(i) ;
1392 
1393           /*
1394           ** A Multilevel Model will have atleast one element where
1395           ** ato_line_id <> parent_ato_line_id. The nvl function is used to take
1396           ** care of null values for pto items.
1397           */
1398           IF( nvl( local_cto_shipset(v_lcs_size).ato_line_id , local_cto_shipset(v_lcs_size).line_id ) <>
1399               nvl( local_cto_shipset(v_lcs_size).parent_ato_line_id , local_cto_shipset(v_lcs_size).line_id ))
1400           THEN
1401               v_multilevel_shipset := TRUE ; /* BUG#1874380 */
1402           END IF ;
1403 
1404       END IF ;
1405    END LOOP ;
1406 
1407    IF( v_multilevel_shipset ) THEN  /* BUG#1874380 */
1408        IF PG_DEBUG <> 0 THEN
1409        	oe_debug_pub.add('evaluate_shipset: ' ||  '*** Shipset Has atleast one Multilevel Model ' , 1 );
1410        END IF;
1411    ELSE
1412        IF PG_DEBUG <> 0 THEN
1413        	oe_debug_pub.add('evaluate_shipset: ' ||  '*** Shipset does not have Multilevel Model ' , 1 );
1414        END IF;
1415    END IF ;
1416 
1417 
1418    IF PG_DEBUG <> 0 THEN
1419    	oe_debug_pub.add ('evaluate_shipset: ' ||  ' **  Bubble Sort logic for aligning ship set' , 4  );
1420    END IF;
1421 
1422    /*
1423    **  Bubble Sort logic for aligning ship set ( replace this with quick sort later!! )
1424    **  sort by Top model line id , level
1425    */
1426    l_stmt_num := 440 ;
1427 
1428    FOR i IN 1..local_cto_shipset.count
1429    LOOP
1430       FOR j IN i +1 ..local_cto_shipset.count
1431       LOOP
1432          IF(  ( local_cto_shipset(i).top_model_line_id >
1433                 local_cto_shipset(j).top_model_line_id )
1434            OR ( ( local_cto_shipset(i).top_model_line_id =
1435                   local_cto_shipset(j).top_model_line_id ) AND
1436                 ( local_cto_shipset(i).plan_level >
1437                   local_cto_shipset(j).plan_level )
1438               )
1439            OR ( ( local_cto_shipset(i).top_model_line_id =
1440                   local_cto_shipset(j).top_model_line_id ) AND
1441                 ( local_cto_shipset(i).plan_level >
1442                   local_cto_shipset(j).plan_level ) AND
1443                 ( local_cto_shipset(i).line_id >
1444                   local_cto_shipset(j).line_id )
1445               )
1446             )
1447          THEN
1448               v_temp_tracker := local_cto_shipset(i) ;
1449               local_cto_shipset(i) := local_cto_shipset(j) ;
1450               local_cto_shipset(j) := v_temp_tracker ;
1451          END IF ;
1452       END LOOP ;
1453    END LOOP ;
1454 
1455 
1456    IF PG_DEBUG <> 0 THEN
1457    	oe_debug_pub.add ('evaluate_shipset: ' ||  'Going to process sourcing, shipset count is ' || local_cto_shipset.count , 2 );
1458    END IF;
1459    /*
1460    ** Check for each top model line whether multi-org and identify sources
1461    ** Also calculate model quantity per
1462    */
1463    l_stmt_num := 480 ;
1464 
1465    v_top_model_exists := FALSE ;
1466 
1467    FOR i IN 1..local_cto_shipset.count
1468    LOOP
1469       v_top_model_line_id := local_cto_shipset(i).top_model_line_id ;
1470       IF( v_top_model_line_id = local_cto_shipset(i).line_id ) THEN
1471           v_top_model_exists := TRUE ;
1472       END IF ;
1473    END LOOP ;
1474 
1475    l_stmt_num := 490 ;
1476    FOR i IN 1..local_cto_shipset.count
1477    LOOP
1478      /*
1482      */
1479      ** In the case of non SMC PTO models, the top model may not be passed with the components
1480      ** We need to check whether the top model is passed. If not, the ato model should be
1481      ** processed for sourcing individually.
1483      IF(
1484          ( local_cto_shipset(i).top_model_line_id  = local_cto_shipset(i).line_id AND
1485            v_top_model_exists = TRUE ) OR
1486          ( local_cto_shipset(i).ato_line_id = local_cto_shipset(i).line_id AND
1487            v_top_model_exists = FALSE )
1488        )
1489      THEN
1490          IF( local_cto_shipset(i).wip_supply_type = '6' )
1491          THEN
1492              v_phantom := TRUE ;
1493          ELSE
1494              v_phantom := FALSE ;
1495          END IF ;
1496 
1497          v_basis_qty := local_cto_shipset(i).ordered_quantity ;
1498 
1499          l_stmt_num := 500 ;
1500          IF( v_basis_qty <> 0 ) THEN
1501          	local_cto_shipset(i).ordered_quantity :=
1502                    local_cto_shipset(i).ordered_quantity / v_basis_qty ;
1503          ELSE
1504          	local_cto_shipset(i).ordered_quantity := 0 ;
1505          END IF ;
1506 
1507          IF PG_DEBUG <> 0 THEN
1508          	oe_debug_pub.add('evaluate_shipset: ' ||  'Going to call process_sourcing_chain ' , 2 );
1509          END IF;
1510 
1511          l_stmt_num := 520 ;
1512          process_sourcing_chain(
1513            i
1514          , i
1515          , local_cto_shipset(i).sourcing_org
1516          , v_phantom
1517          , v_procured
1518          , v_basis_qty
1519          , x_return_status
1520          ) ;
1521 
1522          l_stmt_num := 540 ;
1523 
1524          IF( x_return_status = FND_API.G_RET_STS_ERROR ) THEN
1525               l_stmt_num := 545 ;
1526               IF( local_cto_shipset.count > 0 ) THEN
1527               	l_stmt_num := 550 ;
1528               	FOR l_lcs_count IN local_cto_shipset.first..local_cto_shipset.last
1529               	LOOP
1530                   g_cto_shipset(g_cto_shipset.count + 1 ) := local_cto_shipset(l_lcs_count) ;
1531                   g_final_cto_shipset(g_final_cto_shipset.count + 1 ) := local_cto_shipset(l_lcs_count) ;
1532               	END LOOP ;
1533               END IF ;
1534 
1535               l_stmt_num := 555 ;
1536               IF PG_DEBUG <> 0 THEN
1537               	oe_debug_pub.add('evaluate_shipset: ' ||  'Going to call register error with error code::' || to_char( g_expected_error_code ) , 1 );
1538               END IF;
1539 
1540               register_error ( local_cto_shipset(i).ship_set_name ,
1541                                local_cto_shipset(i).line_id,
1542                                g_expected_error_code,
1543                                v_action,
1544                                false,
1545                                x_return_status);
1546 
1547               l_stmt_num := 557 ;
1548               populate_error( p_shipset , x_return_status ) ;
1549               RAISE FND_API.G_EXC_ERROR ;
1550          ELSIF( x_return_status = FND_API.G_RET_STS_UNEXP_ERROR ) THEN
1551               RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1552          END IF ;
1553 
1554          IF( local_cto_shipset(i).sourced_components )
1555          THEN
1556              p_model_sourced := TRUE ;
1557          END IF ;
1558 
1559      END IF ;
1560 
1561   END LOOP ;
1562 
1563    l_stmt_num := 560 ;
1564    IF( p_model_sourced ) THEN
1565      IF PG_DEBUG <> 0 THEN
1566      	oe_debug_pub.add('evaluate_shipset: ' ||  '***Model is sourced in this shipset ' , 1 );
1567      END IF;
1568    ELSE
1569      IF PG_DEBUG <> 0 THEN
1570      	oe_debug_pub.add('evaluate_shipset: ' ||  '***Model is not sourced in this shipset ' , 1 );
1571      END IF;
1572    END IF ;
1573 
1574    IF PG_DEBUG <> 0 THEN
1575    	oe_debug_pub.add('evaluate_shipset: ' ||  'Done sourcing, status is::' || x_return_status , 2 );
1576    	oe_debug_pub.add('evaluate_shipset: ' ||  'Done sourcing, shipset count is:: ' || local_cto_shipset.count  , 1 );
1577    END IF;
1578 
1579    l_stmt_num := 580 ;
1580    show_contents(
1581     local_cto_shipset
1582    ) ;
1583 
1584    -- 2723674 start fix
1585   l_stmt_num := 598;
1586 
1587    for i in local_cto_shipset.first..local_cto_shipset.last
1588    loop
1589       if( local_cto_shipset.exists(i) )
1590       then
1591       oe_debug_pub.add( ' Shipset exist i' , 1 ) ;	-- debug
1592            if((		( local_cto_shipset(i).line_id  <> nvl( local_cto_shipset(i).parent_ato_line_id , local_cto_shipset(i).line_id ))
1593             	AND	( local_cto_shipset(i).bom_item_type = 1)
1594             	AND	( nvl(local_cto_shipset(i).wip_supply_type, 1) <> 6)
1595              )
1596             	OR  	( local_cto_shipset(i).sourced_components =TRUE
1597             		AND	( local_cto_shipset(i).bom_item_type = 1)
1598             		AND	( nvl(local_cto_shipset(i).wip_supply_type, 1) <> 6)
1599             	)
1600             )
1601            then 	/* get ato_line_id */
1602            	d_line_id 		:= local_cto_shipset(i).line_id; -- debug
1603            	d_parent_ato_line_id 	:= local_cto_shipset(i).parent_ato_line_id;	-- debug
1604            	d_bom_item_type		:= local_cto_shipset(i).bom_item_type;		-- debug
1605            	d_wip_supply_type 	:= local_cto_shipset(i).wip_supply_type;	-- debug
1606            	-- d_sourced_components 	:= to_char(local_cto_shipset(i).sourced_components);	-- debug
1607            	d_ato_line_id		:= local_cto_shipset(i).ato_line_id;		-- debug
1611 
1608            	oe_debug_pub.add( ' Lid '||d_line_id||' p_ato_lid '||d_parent_ato_line_id, 1 ) ;			  -- debug
1609            	oe_debug_pub.add( ' IT '||d_bom_item_type||' WS '||d_wip_supply_type,1);				  -- debug
1610 
1612            	v_mlmo_ato_line_id :=local_cto_shipset(i).ato_line_id;
1613 
1614            	oe_debug_pub.add( ' atoLid '||d_ato_line_id||' mlmo_ato_lid '||v_mlmo_ato_line_id, 1 ) ;		  -- debug
1615 
1616            	for j in local_cto_shipset.first..local_cto_shipset.last
1617            	loop
1618            	if( local_cto_shipset.exists(j) )
1619            	then
1620 
1621            	oe_debug_pub.add( ' Shipset exist j' , 1 ) ;								  -- debug
1622 
1623            		if ( local_cto_shipset(j).ato_line_id = v_mlmo_ato_line_id )
1624            		then
1625 
1626            			dj_ato_line_id		:= local_cto_shipset(j).ato_line_id;				   -- debug
1627            			dj_line_id		:= local_cto_shipset(j).line_id;				   -- debug
1628            			oe_debug_pub.add( ' line id '||dj_line_id||' atoLid '||dj_ato_line_id||' mlmo_ato_lid '||v_mlmo_ato_line_id, 1 ) ;  -- debug
1629 
1630            			local_cto_shipset(j).mlmo_flag := 'Y';
1631 
1632            			dj_mlmo_flag		:= local_cto_shipset(j).mlmo_flag;				   -- debug
1633            			oe_debug_pub.add( ' mlmo_flag after settin to Y '||dj_mlmo_flag, 1 ) ;   		   -- debug
1634 
1635            		end if;
1636            	end if;
1637            	end loop;
1638            end if;
1639         end if ;
1640    end loop ;
1641  -- 2723674 end fix
1642 
1643   /*
1644   ** filter, prepare structure for calling BOM API
1645   ** remove pto and its pto components from pto-ato models
1646   */
1647    l_stmt_num := 600 ;
1648 
1649    IF PG_DEBUG <> 0 THEN
1650    	oe_debug_pub.add('evaluate_shipset: ' ||  ' ** Filter, prepare structure for calling BOM API ', 2 );
1651    	oe_debug_pub.add('evaluate_shipset: ' ||  ' ** Remove pto and its pto components from pto-ato models ' , 2 );
1652    	oe_debug_pub.add('evaluate_shipset: ' ||  ' ** Before filtering components ' || local_cto_shipset.count , 2 );
1653    END IF;
1654 
1655    l_stmt_num := 602 ;
1656 
1657    IF( local_cto_shipset.count > 0 ) THEN
1658    FOR i IN 1..local_cto_shipset.last
1659    LOOP
1660      IF( local_cto_shipset.exists(i) )
1661      THEN
1662         IF( local_cto_shipset(i).top_model_line_id  = local_cto_shipset(i).line_id )
1663         THEN
1664            IF PG_DEBUG <> 0 THEN
1665            	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, i is '|| i  , 5 );
1666            END IF;
1667            /* remove single source ATO , PTO */
1668            IF( local_Cto_shipset(i).sourced_components = FALSE )
1669            THEN
1670                IF PG_DEBUG <> 0 THEN
1671                	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, no sourced components ' , 2 );
1672                END IF;
1673                v_top_model_line_id := local_cto_shipset(i).line_id ;
1674 
1675                l_stmt_num := 640 ;
1676                IF( local_cto_shipset.count > 0 ) THEN
1677                FOR j IN 1..local_cto_shipset.last
1678                    /* Changed i to 1 to account for copied orders which may
1679                    ** not have all the order lines in the right order
1680                    */
1681                LOOP
1682                  IF( local_cto_shipset.exists(j) ) THEN
1683                  IF( local_cto_shipset(j).top_model_line_id = v_top_model_line_id )
1684                  THEN
1685                      IF PG_DEBUG <> 0 THEN
1686                      	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, j is '|| j  , 5 );
1687                      END IF;
1688                      /* remove only pto and its components before ato */
1689                      IF( local_cto_shipset(j).ato_line_id is null OR
1690                         ( local_cto_shipset(j).ato_line_id = local_cto_shipset(j).line_id AND
1691                            local_cto_shipset(j).item_type_code in (  'OPTION' , 'INCLUDED' )
1692                         ) /* special processing for ato items under PTO Models per BUG#1874380 */
1693                         ) THEN
1694                           IF PG_DEBUG <> 0 THEN
1695                           	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, deleted comp for line_id:: ' || local_cto_shipset(j).line_id, 2 );
1696                           END IF;
1697                           local_cto_shipset.delete(j) ;
1698                      END IF ;
1699 
1700                  END IF ;
1701                  END IF ;
1702                END LOOP ;
1703                END IF;
1704 
1705            ELSIF( local_cto_shipset(i).sourced_components = TRUE ) THEN
1706               IF PG_DEBUG <> 0 THEN
1707               	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, sourced components ' , 2 );
1708               END IF;
1709 
1710               /* remove PTO and PTO components from sourced PTO */
1711               v_top_model_line_id := local_cto_shipset(i).line_id ;
1712 
1713               l_stmt_num := 680 ;
1714               IF( local_cto_shipset.count > 0 ) THEN
1715               FOR j IN 1..local_cto_shipset.last
1716                    /* Changed i to 1 to account for copied orders which may
1717                    ** not have all the order lines in the right order
1718                    */
1719               LOOP
1720                  IF( local_cto_shipset.exists(j) ) THEN
1724                      	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, j is '|| j  , 5 );
1721                  IF( local_cto_shipset(j).top_model_line_id = v_top_model_line_id )
1722                  THEN
1723                      IF PG_DEBUG <> 0 THEN
1725                      END IF;
1726                      /* remove only pto and its components before ato */
1727                      IF( local_cto_shipset(j).ato_line_id is null OR
1728                         ( local_cto_shipset(j).ato_line_id = local_cto_shipset(j).line_id AND
1729                            local_cto_shipset(j).item_type_code in (  'OPTION' , 'INCLUDED' )
1730                         ) /* special processing for ato items under PTO Models per BUG#1874380 */
1731                       )
1732                      THEN
1733                          IF PG_DEBUG <> 0 THEN
1734                          	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, deleted comp for line_id::' || local_cto_shipset(j).line_id  , 2 );
1735                          END IF;
1736                          local_cto_shipset.delete(j) ;
1737                      /* set the top level ato to sourced components of top level model */
1738                      ELSIF( local_cto_shipset(j).ato_line_id = local_cto_shipset(j).line_id ) THEN
1739                          local_cto_shipset(j).sourced_components := TRUE ;
1740                      END IF ;
1741 
1742                  END IF ;
1743                  END IF ;
1744               END LOOP ;
1745               END IF;
1746 
1747            ELSE
1748               IF PG_DEBUG <> 0 THEN
1749               	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, null sourced components ', 2  );
1750               END IF;
1751            END IF ;
1752 
1753         ELSIF( local_cto_shipset(i).top_model_line_id is null AND
1754                local_cto_shipset(i).bom_item_type = 4 )
1755         THEN
1756               /* remove standard items and ato items */
1757 
1758               IF PG_DEBUG <> 0 THEN
1759               	oe_debug_pub.add('evaluate_shipset: ' ||  'Filtering PTO, removing standard item for line_id::' || local_cto_shipset(i).line_id , 2 );
1760               END IF;
1761 
1762               local_cto_shipset.delete(i) ;
1763 
1764         END IF ;
1765 
1766      END IF ;
1767    END LOOP ;
1768    END IF ;
1769 
1770 
1771    /* Fixed BUG 2405011
1772       New change made to get rid of pto options in a complex shipset.
1773       Initially this was in the original loop and added pto components into the
1774       final set before they were deleted from the original set.
1775       New strategy requires the collection in g_cto_shipset to be delayed
1776       till complete complex shipset processing is done.
1777    */
1778    l_stmt_num := 700 ;
1779    IF( local_cto_shipset.count > 0 ) THEN
1780    FOR i IN 1..local_cto_shipset.last
1781    LOOP
1782         IF( local_cto_shipset.exists(i))
1783         THEN
1784             l_stmt_num := 720 ;
1785 
1786               /* populate shipset for reference */
1787               IF PG_DEBUG <> 0 THEN
1788               	oe_debug_pub.add('evaluate_shipset: ' || 'g_cto_shipset count ='||to_char(g_cto_shipset.count),2);
1789               END IF;
1790               g_cto_shipset(g_cto_shipset.count + 1 ) := local_cto_shipset(i) ;
1791 
1792               IF PG_DEBUG <> 0 THEN
1793               	oe_debug_pub.add('evaluate_shipset: ' || 'Before creating sparse_shipset',4);
1794               	oe_debug_pub.add('evaluate_shipset: ' || 'Ading line Id '
1795                                ||to_char(local_cto_shipset(i).line_id) || ' to sparse shipset' ,2);
1796               END IF;
1797               /* sparse array creation */
1798               g_cto_sparse_shipset(local_cto_shipset(i).line_id) := local_cto_shipset(i) ;
1799 
1800              l_stmt_num := 740 ;
1801              /* populate final shipset for reference */
1802              g_final_cto_shipset(g_final_cto_shipset.count + 1) := local_cto_shipset(i) ;
1803 
1804              IF( g_cto_shipset.count = 1 ) THEN
1805 
1806                  IF PG_DEBUG <> 0 THEN
1807                  	oe_debug_pub.add('evaluate_shipset: ' ||  'Populating g_cto_shipset for shipset name ' || g_cto_shipset(1).ship_set_name , 2 );
1808                  END IF;
1809 
1810                  l_stmt_num := 760 ;
1811                  v_shipset_index := get_shipset_success_index( g_cto_shipset(1).ship_set_name ) ;
1812                  g_shipset_status_tbl(v_shipset_index).cto_start_location := g_final_cto_shipset.count ;
1813 
1814              END IF ;
1815              /* this is needed for error processing */
1816 	  -- 2723674 SLSO fix for storing SLSO in new structure
1817 	    if (nvl(local_cto_shipset(i).mlmo_flag, 'N') = 'N') then
1818 	 	-- populate slso_shipset from local_cto_shipset
1819 	 	slso_shipset(local_cto_shipset(i).ato_line_id).ship_set_name	:= local_cto_shipset(i).ship_set_name;
1820 	 	slso_shipset(local_cto_shipset(i).ato_line_id).ato_line_id	:= local_cto_shipset(i).ato_line_id;
1821 
1822 	 	oe_debug_pub.add( ' SLSO shipset name ' || slso_shipset(local_cto_shipset(i).ato_line_id).ship_set_name , 1 ) ;
1823 	 	oe_debug_pub.add( ' SLSO ato line id ' || slso_shipset(local_cto_shipset(i).ato_line_id).ato_line_id , 1 ) ;
1824 	     end if;
1825 	 -- 2723674 end fix
1826         END IF;
1827    END LOOP ;
1828    END IF; /* local cto shipset count > 0 */
1829 
1830    /*
1831    ** return if no models are sourced
1832    ** this call is delayed as we need to store cto_shipset information for non sourced models as well
1836    */
1833    ** Modifications: Consider multilevel single org ato models also as enhanced atp processing items
1834    ** provide bom for their processing.
1835    ** A true multi level model will have more than one unique parent ato's.
1837    l_stmt_num := 780 ;
1838 
1839    IF( p_model_sourced = FALSE AND  v_multilevel_shipset = FALSE  )
1840    THEN
1841        /*
1842        ** before reducing shipset populate the error codes encountered
1843        */
1844 
1845        IF PG_DEBUG <> 0 THEN
1846        	oe_debug_pub.add('evaluate_shipset: ' ||  'Returning as none of the models are sourced ' , 2  );
1847        END IF;
1848 
1849        l_stmt_num := 800 ;
1850        populate_error( p_shipset , l_return_status ) ;
1851 
1852        /*
1853        ** copy the shipset to global variable
1854        */
1855        l_stmt_num := 820 ;
1856        save_shipset( p_shipset , x_return_status ) ;
1857        local_cto_shipset.delete ;
1858        return ;
1859    END IF ;
1860 
1861    IF( p_model_sourced = FALSE )
1862    THEN
1863        /* added this statement as multilevel single org should work as multiorg*/
1864        p_model_sourced := TRUE ;
1865        IF PG_DEBUG <> 0 THEN
1866        	oe_debug_pub.add('evaluate_shipset: ' ||  ' Model is multilevel single org, it should behave as multiorg ' , 2 );
1867        END IF;
1868    END IF ;
1869 
1870    /*
1871    ** CALL BOM API
1872    ** make the structure contiguous before calling
1873    */
1874    IF PG_DEBUG <> 0 THEN
1875    	oe_debug_pub.add ('evaluate_shipset: ' ||  ' Before Create ATP BOM ' , 1 );
1876    END IF;
1877    l_stmt_num := 840 ;
1878 
1879    show_contents( g_cto_shipset ) ;
1880 
1881    IF PG_DEBUG <> 0 THEN
1882    	oe_debug_pub.add ('evaluate_shipset: ' ||  ' Display shipset contents before Create ATP BOM ' , 2 );
1883 	show_contents( g_cto_sparse_shipset ) ;
1884    END IF;
1885 
1886    l_stmt_num := 860 ;
1887    Create_Atp_Bom (
1888       p_atp_bom_rec
1889     , l_return_status
1890     , l_msg_data
1891     , l_msg_count
1892    );
1893 
1894    l_stmt_num := 880 ;
1895    IF( l_return_status = FND_API.G_RET_STS_ERROR ) THEN
1896        populate_error( p_shipset , l_return_status ) ;
1897        RAISE FND_API.G_EXC_ERROR ;
1898    ELSIF( l_return_status = FND_API.G_RET_STS_UNEXP_ERROR ) THEN
1899        RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1900    END IF ;
1901 
1902    IF PG_DEBUG <> 0 THEN
1903    	oe_debug_pub.add('evaluate_shipset: ' ||  ' Create ATP BOM information ' , 4 );
1904    END IF;
1905 
1906    l_stmt_num := 900 ;
1907    FOR k IN p_atp_bom_rec.assembly_identifier.first..p_atp_bom_rec.assembly_identifier.last
1908    LOOP
1909       IF( p_atp_bom_rec.assembly_identifier.exists(k) ) THEN
1910 
1911          IF PG_DEBUG <> 0 THEN
1912          	oe_debug_pub.add('evaluate_shipset: ' ||  ' Aident ' || p_atp_bom_rec.assembly_identifier(k) ||
1913                                 ' Aitem ' || p_atp_bom_rec.assembly_item_id(k) ||
1914                                 ' Cident ' || p_atp_bom_rec.component_identifier(k) ||
1915                                 ' Citem ' || p_atp_bom_rec.component_item_id(k) ||
1916                                 ' quant ' || p_atp_bom_rec.quantity(k) ||
1917                                 ' FLT ' || p_atp_bom_rec.fixed_lt(k) ||
1918                                 ' VLT ' || p_atp_bom_rec.variable_lt(k) ||
1919 				' PPLT ' || p_atp_bom_rec.pre_process_lt(k) ||
1920 				' ORG ' || p_atp_bom_rec.source_organization_id(k) ||
1921                                 ' WIP ' || p_atp_bom_rec.wip_supply_type(k) , 4 );
1922          END IF;
1923 
1924         l_stmt_num := 920 ;
1925         FOR l IN g_final_cto_shipset.first..g_final_cto_shipset.last
1926         LOOP
1927            IF( g_final_cto_shipset(l).line_id = p_atp_bom_rec.component_identifier(k) )
1928            THEN
1929                IF( g_final_cto_shipset(l).inventory_item_id = p_atp_bom_rec.component_item_id(k))
1930                THEN
1931                   IF( p_atp_bom_rec.wip_supply_type(k) = 6 )
1932                   THEN
1933                      IF(  g_final_cto_shipset(l).bom_item_type in ( 1 , 2 ) )
1934                      THEN
1935                         l_stmt_num := 940 ;
1936                         g_final_cto_shipset(l).wip_supply_type := -1 ;
1937                         p_atp_bom_rec.wip_supply_type(k) := -1 ;
1938                         IF PG_DEBUG <> 0 THEN
1939                         	oe_debug_pub.add('evaluate_shipset: ' ||  ' Changed wip to -1 for ' ||
1940                                    g_final_cto_shipset(l).line_id || ' iid ' ||
1941                                    g_final_cto_shipset(l).inventory_item_id  ||
1942                                    ' as it matched ' || p_atp_bom_rec.component_identifier(k) ||
1943                                    ' item ' || p_atp_bom_rec.component_item_id(k) , 5  );
1944                         END IF;
1945 
1946                      ELSE
1947                         l_stmt_num := 960 ;
1948                         g_final_cto_shipset(l).wip_supply_type := 6 ;
1949                         IF PG_DEBUG <> 0 THEN
1950                         	oe_debug_pub.add('evaluate_shipset: ' ||  ' Changed wip to 6 for ' || g_final_cto_shipset(l).line_id || ' iid ' || g_final_cto_shipset(l).inventory_item_id , 5 );
1951                         END IF;
1952                      END IF ;
1953                   END IF ;
1954                END IF ;
1958    END LOOP ;
1955           END IF ;
1956         END LOOP ;
1957       END IF ;
1959 
1960    IF PG_DEBUG <> 0 THEN
1961    	oe_debug_pub.add ('evaluate_shipset: ' ||  ' **Prepare ship set for ATP ' || g_cto_sparse_shipset.count ,2 );
1962    END IF;
1963    /*
1964    ** prepare ship set for ATP
1965    ** Identify and flag components in ship set that need to be inactivated
1966    ** as their models are sourced or if they are SLSO (bug 2723674)
1967    */
1968    l_stmt_num := 980 ;
1969    FOR i IN 1..p_shipset.identifier.count
1970    LOOP
1971       IF( g_cto_sparse_shipset.exists(p_shipset.identifier(i)) )
1972       THEN
1973         IF (g_cto_sparse_shipset(p_shipset.identifier(i)).mlmo_flag = 'Y') THEN	-- bug 2723674
1974            v_curr_line_id := g_cto_sparse_shipset(p_shipset.identifier(i)).line_id ;
1975 
1976            IF( g_cto_sparse_shipset(v_curr_line_id).inventory_item_id = p_shipset.inventory_item_id(i) ) THEN
1977              IF( NOT ( g_cto_sparse_shipset(v_curr_line_id).ato_line_id = v_curr_line_id ))
1978              THEN
1979                  v_cto_shipset_flag(i) := FALSE ;
1980              ELSE
1981                  v_cto_shipset_flag(i) := TRUE ;
1982              END IF ;
1983            ELSE
1984                  v_cto_shipset_flag(i) := FALSE ;
1985            END IF ;
1986         ELSE
1987 	   v_cto_shipset_flag(i) := TRUE ;
1988 	END IF;  -- bug 2723674
1989       ELSE
1990              v_cto_shipset_flag(i) := TRUE ;
1991 
1992       END IF ;
1993    END LOOP ;
1994 
1995    /*
1996    ** before reducing shipset populate the error codes encountered
1997    */
1998    l_stmt_num := 1000 ;
1999    populate_error( p_shipset , l_return_status ) ;
2000 
2001    /*
2002    ** copy the shipset to global variable
2003    */
2004    l_stmt_num := 1020 ;
2005    save_shipset( p_shipset , x_return_status ) ;
2006 
2007    /*
2008    ** prepare reduced shipset
2009    */
2010    l_stmt_num := 1040 ;
2011    FOR i IN v_cto_shipset_flag.first..v_cto_shipset_flag.last
2012    LOOP
2013       IF( v_cto_shipset_flag(i) ) THEN
2014         IF PG_DEBUG <> 0 THEN
2015         	oe_debug_pub.add ('evaluate_shipset: ' ||  'Line id ' || p_shipset.identifier(i) || ' is part of reduced shipset ', 2  );
2016         END IF;
2017                 l_stmt_num := 1060 ;
2018                 MRP_ATP_PVT.assign_atp_input_rec(p_shipset,
2019                                                  i,
2020                                                  v_shipset,
2021                                                  x_return_status );
2022                 IF PG_DEBUG <> 0 THEN
2023                 	oe_debug_pub.add('evaluate_shipset: ' || 'MRP API returns with '|| x_return_status , 5 );
2024                 END IF;
2025       ELSE
2026         IF PG_DEBUG <> 0 THEN
2027         	oe_debug_pub.add('evaluate_shipset: ' ||  'Line id ' || p_shipset.identifier(i) || ' is removed from reduced shipset ', 2  );
2028         END IF;
2029       END IF ;
2030    END LOOP ;
2031 
2032    IF PG_DEBUG <> 0 THEN
2033    	oe_debug_pub.add('evaluate_shipset: ' ||  '***Contents of reduced shipset are '  ,1 );
2034    END IF;
2035 
2036    l_stmt_num := 1080 ;
2037    FOR i IN v_shipset.identifier.first..v_shipset.identifier.last
2038    LOOP
2039       IF( v_shipset.identifier.exists(i) ) THEN
2040         IF PG_DEBUG <> 0 THEN
2041         	oe_debug_pub.add('evaluate_shipset: ' ||  '++++Line id:: ' || v_shipset.identifier(i) , 1  );
2042         END IF;
2043       END IF ;
2044    END LOOP ;
2045 
2046    /*
2047    ** send reduced shipset
2048    */
2049    p_shipset := v_shipset ;
2050 
2051 EXCEPTION
2052    WHEN FND_API.G_EXC_ERROR THEN
2053         IF PG_DEBUG <> 0 THEN
2054         	oe_debug_pub.add('evaluate_shipset: ' || ':exp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2055         END IF;
2056         x_return_status := FND_API.G_RET_STS_ERROR;
2057         FND_MSG_PUB.Count_And_Get(
2058           p_count => x_msg_count
2059         , p_data  => x_msg_data
2060         );
2061    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2062         IF PG_DEBUG <> 0 THEN
2063         	oe_debug_pub.add('evaluate_shipset: ' || ':unexp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2064         END IF;
2065         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2066         FND_MSG_PUB.Count_And_Get(
2067           p_count => x_msg_count
2068         , p_data  => x_msg_data
2069         );
2070    WHEN OTHERS THEN
2071         IF PG_DEBUG <> 0 THEN
2072         	oe_debug_pub.add('evaluate_shipset: ' || ':others::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2073         END IF;
2074         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2075         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2076            FND_MSG_PUB.Add_Exc_Msg(
2077              G_PKG_NAME
2078            , 'EVALUATE_SHIPSET'
2079            );
2080         END IF;
2081 END evaluate_shipset ;
2082 
2083 
2084 /*
2085 ** This procedure does the preprocessing and data collection for calls
2086 ** originating from CZ.
2087 ** CZ store information in CZ_ATP_REQUESTS table. The format of this information does
2088 ** not match with data structure required for evaluate shipset function. Hence this data
2092 */
2089 ** needs to be transformed from CZ_REQUEST_TYPE data structure to CTO_SHIPSET_TYPE data structure.
2090 ** Model/Component relationships ( link_to_line_id, ato_line_id) need to be established before
2091 ** transformation.
2093 PROCEDURE populate_cz_shipset(
2094   p_shipset                in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
2095 , p_orig_shipset_tracker   in out NOCOPY CTO_SHIPSET_TBL_TYPE
2096 , p_shipset_contains_model in out boolean
2097 , x_return_status             out varchar2
2098 )
2099 is
2100 v_orig_shipset_tracker     CTO_SHIPSET_TBL_TYPE ;
2101 v_raw_shipset_tracker     CTO_SHIPSET_TBL_TYPE ;
2102 v_dummy_shipset_tracker     CTO_SHIPSET_TBL_TYPE ;
2103 v_temp_tracker     CTO_SHIPSET_TYPE ;
2104 v_basis_qty          number ;
2105 v_phantom            boolean ;
2106 v_shipset_contains_models  boolean ;
2107 local_cto_shipset     CTO_SHIPSET_TBL_TYPE ; /* package global variable */
2108 v_curr_line_id        number ;
2109 v_top_model_line_id        number ;
2110 c_organization_id     number ;
2111 v_requests_tab        CZ_REQUESTS_TBL_TYPE ;
2112 v_index                     number(10) := 0 ;
2113 v_component_code      cz_atp_requests.item_key%type ;
2114 v_top_model_code      cz_atp_requests.item_key%type ;
2115 v_match_component_code      cz_atp_requests.item_key%type ;
2116 v_code_loc            number(10) ;
2117 v_parent_found        boolean ;
2118 v_parent_loc          number(10) ;
2119 	 cursor c1 (c_session_key cz_atp_requests.configurator_session_key%type )
2120 	   is
2121      select Item_key, quantity, UOM_CODE, ship_To_date
2122 		 from cz_atp_requests where configurator_session_key = c_session_key ;
2123 x_msg_count number ;
2124 x_msg_data  varchar2(2000) ;
2125 l_stmt_num  number ;
2126 v_bill_sequence_id   bom_bill_of_materials.bill_sequence_id%type  ;
2127 v_component_code_tbl  CZ_ATP_CALLBACK_UTIL.char30_arr ;
2128 l_validation_org     number ;
2129 v_config_item_id     number ;
2130 v_top_config_item_id number ;
2131 
2132 BEGIN
2133 
2134         l_stmt_num := 1 ;
2135         p_shipset_contains_model := FALSE ;
2136         g_requests_tab.delete ;
2137 
2138         IF PG_DEBUG <> 0 THEN
2139         	oe_debug_pub.add('populate_cz_shipset: ' ||  'Entered populate cz shipset ' , 1 );
2140         END IF;
2141 
2142         l_stmt_num := 4 ;
2143         FOR v_location IN p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
2144         LOOP
2145            IF( p_shipset.identifier(v_location) is not null ) THEN
2146                v_index := v_index + 1 ;
2147 
2148             l_stmt_num := 6 ;
2149             select
2150                  atp_request_id  /* line to be commented if noncz */
2151                  ,                 /* line to be commented if noncz */
2152                  configurator_session_key
2153                  , seq_no
2154                  , item_key
2155                  , quantity
2156                  , uom_code
2157                  , ship_to_date
2158                  , cz_atp_callback_util.inv_item_id_from_item_key( item_key )
2159                   /* line to be commented if noncz */
2160                  , config_item_id    /*BUG#2250621 Multiple Instantiation Code Change */
2161                  , parent_config_item_id /*BUG#2250621 Multiple Instantiation Code Change */
2162             into
2163                    g_requests_tab(v_index).line_id    /* line to be commented if noncz */
2164                  ,                                      /* line to be commented if noncz */
2165                  g_requests_tab(v_index).configurator_session_key
2166                  , g_requests_tab(v_index).seq_no
2167                  , g_requests_tab(v_index).item_key
2168 	         , g_requests_tab(v_index).quantity
2169 	         , g_requests_tab(v_index).UOM_CODE
2170 	         , g_requests_tab(v_index).ship_To_date
2171 	         , g_requests_tab(v_index).inventory_item_id
2172                  /* line to be commented if noncz */
2173 	         , g_requests_tab(v_index).config_item_id /*BUG#2250621 Multiple Instantiation Code Change */
2174                  , g_requests_tab(v_index).parent_config_item_id   /*BUG#2250621 Multiple Instantiation Code Change */
2175             from cz_atp_requests
2176             where atp_request_id = p_shipset.identifier(v_location) ;
2177             /* line to be commented if noncz */
2178 
2179             l_stmt_num := 10 ;
2180             g_requests_tab(v_index).inv_org_id := p_shipset.source_organization_id(v_location ) ;
2181 
2182             IF PG_DEBUG <> 0 THEN
2183             	oe_debug_pub.add('populate_cz_shipset: ' ||  'Ship from org is ' || g_requests_tab(v_index).inv_org_id , 2 );
2184             END IF;
2185 
2186             g_requests_tab(v_index).location := v_location ;
2187             g_requests_tab(v_index).ship_set_name := p_shipset.ship_set_name(v_location ) ;
2188             g_requests_tab(v_index).assigned := FALSE ;
2189 
2190  	    v_component_code := cz_atp_callback_util.component_code_from_item_key(
2191                                   g_requests_tab(v_index).item_key
2192                                  ) ;
2193 	    v_component_code_tbl := cz_atp_callback_util.component_code_tokens(
2194                                   v_component_code
2195                                 ) ;
2196             v_top_model_code := v_component_code_tbl( v_component_code_tbl.first ) ;
2200           END IF ; /* check whether identifier is null */
2197             v_top_config_item_id := cz_atp_callback_util.root_bom_config_item_id( g_requests_tab(v_index).configurator_session_key) ; /*BUG#2250621 Multiple Instantiation Code Change */
2198 
2199             l_stmt_num := 14 ;
2201 	END LOOP ;
2202 
2203         l_stmt_num := 20 ;
2204         FOR j IN 1..g_requests_tab.count
2205         LOOP
2206 
2207         IF PG_DEBUG <> 0 THEN
2208         	oe_debug_pub.add('populate_cz_shipset: ' ||  'Line_id  ' || g_requests_tab(j).line_id
2209                          || ' inventory id ' || g_requests_tab(j).inventory_item_id
2210                          || ' self ' || g_requests_tab(j).config_item_id
2211                          || ' parent ' || g_requests_tab(j).parent_config_item_id , 5 );
2212         END IF;
2213 
2214         END LOOP ;
2215 
2216         IF PG_DEBUG <> 0 THEN
2217         	oe_debug_pub.add('populate_cz_shipset: ' ||  'Fetched cz information ' ,  3 );
2218          END IF;
2219 
2220            l_validation_org := cz_atp_callback_util.validation_org_for_cfg_model( g_requests_tab(g_requests_tab.first).configurator_session_key ) ;
2221 
2222          IF PG_DEBUG <> 0 THEN
2223          	oe_debug_pub.add('populate_cz_shipset: ' ||  'Validation org is ' || l_validation_org , 3 );
2224          END IF;
2225 
2226          l_stmt_num := 24 ;
2227          IF( l_validation_org is null ) THEN
2228              FOR l_error_loc IN
2229                  p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
2230              LOOP
2231                 p_shipset.error_code(l_error_loc) := 69 ;
2232                 /* error code for Invalid Validation Org seeded in MFG_LOOKUPS
2233                 ** for TYPE 'MTL_DEMAND_INTERFACE_ERRORS'
2234                 */
2235              END LOOP ;
2236 
2237              RAISE FND_API.G_EXC_ERROR ;
2238          END IF ;
2239 
2240          --- We re storing the l_validation_org value in pkg variable,which will be used
2241          --- in BOM creation module to get the calendar code for validation org.
2242 
2243         CTO_ATP_INTERFACE_PK.G_OE_VALIDATION_ORG := l_validation_org;
2244 
2245          /*    BUG#2250621 Multiple Instantiation Code Change */
2246 	 /*
2247 	 ** populate link_to_line_id for each of the components
2248          ** Strategy: Find all children that could belong to a possible parent
2249          **           for each possible parent node(outer LOOP)
2250          **               for each possible child (inner LOOP)
2251          **                 if child of current parent
2252          **                    make it point to the current parent
2253          **
2254 	 */
2255 
2256          l_stmt_num := 28 ;
2257          FOR j IN 1..g_requests_tab.count
2258 	 LOOP
2259                /*      BUG#2250621 Multiple Instantiation Code Change */
2260 
2261                IF( g_requests_tab(j).config_item_id <> v_top_config_item_id ) THEN
2262                   v_config_item_id := g_requests_tab(j).config_item_id ;
2263                ELSE
2264                   v_config_item_id := g_requests_tab(j).config_item_id ;
2265                   v_top_model_line_id := g_requests_tab(j).line_id ;
2266                END IF ;
2267 
2268                l_stmt_num := 30 ;
2269 	       FOR k IN 1..g_requests_tab.count
2270 	       LOOP
2271                   IF( k <> j  ) THEN
2272                       IF( v_config_item_id = g_requests_tab(k).parent_config_item_id )
2273                       THEN
2274 			  v_parent_loc := j ;
2275 			  g_requests_tab(k).link_to_line_id := g_requests_tab(v_parent_loc).line_id ;
2276                           g_requests_tab(k).parent_location := g_requests_tab(v_parent_loc).location ;
2277                       END IF ;
2278                   END IF ;
2279 	       END LOOP ;
2280 	 END LOOP ;
2281 
2282          IF PG_DEBUG <> 0 THEN
2283          	oe_debug_pub.add('populate_cz_shipset: ' ||  'Done populating link_to_line_id ' , 4 );
2284          END IF;
2285 
2286          l_stmt_num := 34 ;
2287          FOR j IN 1..g_requests_tab.count
2288          LOOP
2289             g_requests_tab(j).top_model_line_id := v_top_model_line_id ;
2290             IF PG_DEBUG <> 0 THEN
2291         	oe_debug_pub.add('populate_cz_shipset: ' ||  'Line_id  ' || g_requests_tab(j).line_id
2292                          || ' inventory id ' || g_requests_tab(j).inventory_item_id
2293                          || ' self ' || g_requests_tab(j).config_item_id
2294                          || ' parent ' || g_requests_tab(j).parent_config_item_id
2295                          || ' link ' || g_requests_tab(j).link_to_line_id
2296                          || ' top ' || g_requests_tab(j).top_model_line_id
2297 , 4 );
2298             END IF;
2299          END LOOP ;
2300 
2301          IF PG_DEBUG <> 0 THEN
2302          	oe_debug_pub.add('populate_cz_shipset: ' ||  'Done populating top_model_line_id ' , 4 );
2303          END IF;
2304 
2305          l_stmt_num := 38 ;
2306          FOR j IN  1..g_requests_tab.count
2307 	 LOOP
2308            IF PG_DEBUG <> 0 THEN
2309            	oe_debug_pub.add('populate_cz_shipset: ' ||  'Going to query msyi with iid ' || g_requests_tab(j).inventory_item_id || ' org ' || l_validation_org  , 5 );
2310            END IF;
2311 
2312             l_stmt_num := 40 ;
2313 	    select MSYI.bom_item_type
2314                , MSYI.replenish_to_order_flag
2315                , MSYI.pick_components_flag
2319 	       , MSYI.atp_components_flag	--2462661
2316                , MSYI.base_item_id
2317                , MSYI.build_in_wip_flag
2318 	       , MSYI.atp_flag			--2462661
2320             into
2321 	         g_requests_tab(j).bom_item_type
2322                , g_requests_tab(j).replenish_to_order_flag
2323 	       , g_requests_tab(j).pick_components_flag
2324 	       , g_requests_tab(j).base_item_id
2325 	       , g_requests_tab(j).build_in_wip_flag
2326 	       , g_requests_tab(j).atp_flag
2327 	       , g_requests_tab(j).atp_components_flag
2328             from Mtl_system_items MSYI
2329 	    where MSYI.inventory_item_id = g_requests_tab(j).inventory_item_id
2330 	      and MSYI.organization_id  = l_validation_org ;
2331            /*
2332            ** find bill sequence id for parent item
2333            */
2334 
2335            IF PG_DEBUG <> 0 THEN
2336            	oe_debug_pub.add('populate_cz_shipset: ' ||  'Done msyi ' ,5  );
2337 
2338           	oe_debug_pub.add('populate_cz_shipset: ' ||  'Going to find bill_sequence_id with assemblyid ' || g_requests_tab(nvl( g_requests_tab(j).parent_location , j ) ).inventory_item_id || ' and  org ' || l_validation_org , 5  );
2339           END IF;
2340 
2341            BEGIN
2342 
2343            l_stmt_num := 42 ;
2344            select common_bill_sequence_id
2345              into v_bill_sequence_id
2346            from bom_bill_of_materials
2347            where assembly_item_id = g_requests_tab( nvl( g_requests_tab(j).parent_location, j ) ).inventory_item_id
2348              AND organization_id = l_validation_org ;
2349 
2350            EXCEPTION
2351            WHEN no_data_found THEN
2352                 IF PG_DEBUG <> 0 THEN
2353                 	oe_debug_pub.add('populate_cz_shipset: ' ||  'No data found for bill sequence id ' , 2 );
2354                 END IF;
2355                 v_bill_sequence_id := null ;
2356            WHEN others THEN
2357                RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2358            END ;
2359 
2360            l_stmt_num := 44 ;
2361            IF( g_requests_tab(j).bom_item_type = '1' ) THEN
2362                /*
2363                ** find wip supply type using parent bill sequence id and self inventory item id and
2364                ** item validation org
2365                */
2366               IF PG_DEBUG <> 0 THEN
2367               	oe_debug_pub.add('populate_cz_shipset: ' ||  'Going for wipsupplytype with billsequenceid ' || v_bill_sequence_id  || ' componentid ' || g_requests_tab(j).inventory_item_id  , 5 );
2368               END IF;
2369 
2370               BEGIN
2371                 l_stmt_num := 48 ;
2372                 select wip_supply_type
2373                  into g_requests_tab(j).wip_supply_type
2374                  from bom_inventory_components
2375                 where bill_sequence_id = v_bill_sequence_id
2376                  AND component_item_id = g_requests_tab(j).inventory_item_id
2377                  AND rownum < 1 ;
2378 
2379               EXCEPTION
2380               WHEN no_data_found THEN
2381                    IF PG_DEBUG <> 0 THEN
2382                    	oe_debug_pub.add('populate_cz_shipset: ' ||  'No data for wip supply ',2 );
2383                    END IF;
2384 
2385               WHEN others THEN
2386                    IF PG_DEBUG <> 0 THEN
2387                    	oe_debug_pub.add('populate_cz_shipset: ' ||  ' other error raise ' || SQLCODE ,2 );
2388                    	oe_debug_pub.add('populate_cz_shipset: ' ||  ' other error raise ' || SQLERRM ,2 );
2389                    END IF;
2390                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2391               END ;
2392 
2393            END IF ;
2394 	 END LOOP ;
2395 
2396          IF PG_DEBUG <> 0 THEN
2397          	oe_debug_pub.add('populate_cz_shipset: ' ||  'Done fetching msyi and bic information '  , 2 );
2398          END IF;
2399 
2400 	 /*
2401 	 ** propagate ato line id to all descendents
2402 	 */
2403 	 IF PG_DEBUG <> 0 THEN
2404 	 	oe_debug_pub.add('populate_cz_shipset: ' || ' ** propagate ato line id to all descendents ' ,2  );
2405 	 END IF;
2406 
2407          l_stmt_num := 52 ;
2408          FOR j IN 1..g_requests_tab.count
2409          LOOP
2410            IF PG_DEBUG <> 0 THEN
2411            	oe_debug_pub.add('populate_cz_shipset: ' ||  ' loc ' || j || ' -> ' || g_requests_tab(j).line_id || ' alid '
2412 			|| g_requests_tab(j).ato_line_id || ' link ' || g_requests_tab(j).link_to_line_id || ' top ' || g_Requests_Tab(j).top_model_line_id  , 5 );
2413            END IF;
2414          END LOOP ;
2415 
2416 	 IF PG_DEBUG <> 0 THEN
2417 	 	oe_debug_pub.add('populate_cz_shipset: ' || ' ** calling propagate ato line id to all descendents ' ,2  );
2418 	 END IF;
2419          propagate_ato_line_id( g_requests_tab.first ) ;
2420 
2421 	 IF PG_DEBUG <> 0 THEN
2422 	 	oe_debug_pub.add('populate_cz_shipset: ' || ' ** copying information to shipset tracker ' ,2  );
2423 	 END IF;
2424 
2425    l_stmt_num := 56 ;
2426    FOR j IN 1..g_requests_tab.count
2427    LOOP
2428       v_index := p_orig_shipset_tracker.count + 1 ;
2429 
2430       IF( g_requests_tab.exists(j)) THEN
2431 
2432       p_orig_shipset_tracker(v_index ).header_id := null ;
2433       /* cz call does not have header id */
2434       p_orig_shipset_tracker(v_index ).line_id           := g_requests_tab(j).line_id ;
2435       p_orig_shipset_tracker(v_index).top_model_line_id := g_requests_tab(j).top_model_line_id ;
2439       p_orig_shipset_tracker(v_index).item_type_code    := g_requests_tab(j).item_type_code ;
2436       p_orig_shipset_tracker(v_index).ato_line_id       := g_requests_tab(j).ato_line_id ;
2437       p_orig_shipset_tracker(v_index).link_to_line_id   := g_requests_tab(j).link_to_line_id ;
2438       p_orig_shipset_tracker(v_index).inventory_item_id := g_requests_tab(j).inventory_item_id ;
2440       p_orig_shipset_tracker(v_index).ordered_quantity  := g_requests_tab(j).quantity ;
2441       p_orig_shipset_tracker(v_index).wip_supply_type   := g_requests_tab(j).wip_supply_type ;
2442       p_orig_shipset_tracker(v_index).bom_item_type     := g_requests_tab(j).bom_item_type ;
2443       p_orig_shipset_tracker(v_index).replenish_to_order_flag := g_requests_tab(j).replenish_to_order_flag ;
2444       p_orig_shipset_tracker(v_index).pick_components_flag := g_requests_tab(j).pick_components_flag ;
2445       p_orig_shipset_tracker(v_index).base_item_id         := g_requests_tab(j).base_item_id ;
2446       p_orig_shipset_tracker(v_index).build_in_wip_flag    := g_requests_tab(j).build_in_wip_flag ;
2447       p_orig_shipset_tracker(v_index).sourcing_org         := g_requests_tab(j).inv_org_id ;
2448       p_orig_shipset_tracker(v_index).ordered_quantity     := g_requests_tab(j).quantity ;
2449       p_orig_shipset_tracker(v_index).top_model_ato        := g_requests_tab(j).top_model_ato ;
2450       p_orig_shipset_tracker(v_index).location             := g_requests_tab(j).location ;
2451       p_orig_shipset_tracker(v_index).ship_set_name := g_requests_tab(j).ship_set_name ;
2452       p_orig_shipset_tracker(v_index).sourced_components := FALSE ;
2453       p_orig_shipset_tracker(v_index).atp_flag             := g_requests_tab(j).atp_flag;
2454       p_orig_shipset_tracker(v_index).atp_components_flag  := g_requests_tab(j).atp_components_flag;
2455       /* Added for procure to order */
2456       p_orig_shipset_tracker(v_index).buy_model := 'N' ;
2457 
2458       p_orig_shipset_tracker(v_index).mlmo_flag := 'N';		-- 2723674 : Initializing MLMO flag
2459 
2460       /*
2461       **  mandatory components may cause a problem to calculate plan level and parent ato
2462       */
2463       l_stmt_num := 60 ;
2464       IF( p_orig_shipset_tracker(v_index).inventory_item_id <> p_shipset.inventory_item_id(j) ) THEN
2465           p_orig_shipset_tracker(v_index).mandatory_component := TRUE  ;
2466       END IF ;
2467 
2468       /*
2469       **  check whether any models exist
2470       */
2471       IF( p_orig_shipset_tracker(v_index).bom_item_type = '1' ) THEN
2472           p_shipset_contains_model := TRUE  ;
2473       END IF ;
2474 
2475       /*
2476       **  calculate stored_atp_flag for ATP (bug 2462661)
2477       **  Note: making a change to p_shipset here, which needs to be
2478       **  sent back to ATP in the reduced shipset structure
2479       */
2480       l_stmt_num := 62 ;
2481       IF( p_orig_shipset_tracker(v_index).atp_flag <> 'N' OR p_orig_shipset_tracker(v_index).atp_components_flag <> 'N') THEN
2482 	  p_shipset.attribute_06(p_orig_shipset_tracker(v_index).location ) := 'Y';
2483       ELSE
2484 	  p_shipset.attribute_06(p_orig_shipset_tracker(v_index).location ) := 'N';
2485       END IF ;
2486 
2487      END IF ;
2488    END LOOP ;
2489 
2490    l_stmt_num := 64 ;
2491    show_contents( p_orig_shipset_tracker ) ;
2492 
2493    IF PG_DEBUG <> 0 THEN
2494    	oe_debug_pub.add('populate_cz_shipset: ' ||  'Done populate cz shipset ' ,1 );
2495    END IF;
2496 
2497 
2498 EXCEPTION
2499    WHEN FND_API.G_EXC_ERROR THEN
2500         IF PG_DEBUG <> 0 THEN
2501         	oe_debug_pub.add('populate_cz_shipset: ' || 'POPULATE_CZ_SHIPSET::exp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2502         END IF;
2503         x_return_status := FND_API.G_RET_STS_ERROR;
2504         FND_MSG_PUB.Count_And_Get(
2505           p_count => x_msg_count
2506         , p_data  => x_msg_data
2507         );
2508    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2509         IF PG_DEBUG <> 0 THEN
2510         	oe_debug_pub.add('populate_cz_shipset: ' || 'POPULATE_CZ_SHIPSET::unexp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2511         END IF;
2512         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2513         FND_MSG_PUB.Count_And_Get(
2514           p_count => x_msg_count
2515         , p_data  => x_msg_data
2516         );
2517    WHEN OTHERS THEN
2518         IF PG_DEBUG <> 0 THEN
2519         	oe_debug_pub.add('populate_cz_shipset: ' || 'POPULATE_CZ_SHIPSET::others::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2520         END IF;
2521         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2522         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2523            FND_MSG_PUB.Add_Exc_Msg(
2524              G_PKG_NAME
2525            , 'POPULATE_CZ_SHIPSET'
2526            );
2527         END IF;
2528 END populate_cz_shipset ;
2529 
2530 
2531 /*
2532 ** This procedure does the preprocessing and data collection for calls
2533 ** originating from OM.
2534 */
2535 PROCEDURE populate_om_shipset(
2536   p_shipset                in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
2537 , p_orig_shipset_tracker   in out NOCOPY CTO_SHIPSET_TBL_TYPE
2538 , p_shipset_contains_model in out boolean
2539 , x_return_status             out varchar2
2540 )
2541 is
2542 l_stmt_num        number ;
2543 x_msg_count       number ;
2544 x_msg_data        varchar2(2000) ;
2545 l_validation_org  number ;
2546 BEGIN
2547 
2548    l_stmt_num := 1 ;
2549    IF PG_DEBUG <> 0 THEN
2553    p_shipset_contains_model := FALSE ;
2550    	oe_debug_pub.add('populate_om_shipset: ' ||  ' In populate om shipset ' , 1  );
2551    END IF;
2552    x_return_status := FND_API.G_RET_STS_SUCCESS ;
2554 
2555    /*
2556    ** Find Validation Organization
2557    */
2558    l_stmt_num := 4 ;
2559    BEGIN
2560       select nvl(master_organization_id,-99)	--bugfix 2646849: master_organization_id can be 0
2561       into   l_validation_org
2562       from   oe_order_lines_all oel,
2563              oe_system_parameters_all ospa
2564       where  oel.line_id = p_shipset.identifier(1)
2565         and  nvl(oel.org_id,-1) = nvl(ospa.org_id,-1) --bug 1531691
2566         and  oel.inventory_item_id = p_shipset.inventory_item_id(1) ;
2567    EXCEPTION
2568       WHEN NO_DATA_FOUND THEN
2569            l_validation_org := -99 ;	--bugfix 2646849: changed from null to -99
2570 
2571       WHEN OTHERS THEN
2572            x_return_status                := FND_API.G_RET_STS_UNEXP_ERROR;
2573            raise FND_API.G_EXC_UNEXPECTED_ERROR;
2574    END ;
2575 
2576    l_stmt_num := 6 ;
2577    IF( l_validation_org = -99 ) THEN 	--bugfix 2646849: changed from null to -99
2578          FOR l_error_loc IN
2579              p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
2580          LOOP
2581              p_shipset.error_code(l_error_loc) := 69 ;
2582              /* error code for Invalid Validation Org seeded in MFG_LOOKUPS
2583              ** for TYPE 'MTL_DEMAND_INTERFACE_ERRORS'
2584              */
2585          END LOOP ;
2586          RAISE FND_API.G_EXC_ERROR ;
2587 
2588    END IF ;
2589 
2590          --- We re storing the l_validation_org value in pkg variable,which will be used
2591          --- in BOM creation module to get the calendar code for validation org.
2592         CTO_ATP_INTERFACE_PK.G_OE_VALIDATION_ORG := l_validation_org;
2593 
2594    /*
2595    ** process shipset
2596    */
2597    l_stmt_num := 8 ;
2598 
2599    FOR j IN p_shipset.identifier.first..p_shipset.identifier.last
2600    LOOP
2601       BEGIN
2602            select OEOL.header_id
2603                 , OEOL.line_id
2604                 , OEOL.top_model_line_id
2605                 , OEOL.ato_line_id
2606                 , OEOL.link_to_line_id
2607                 , OEOL.inventory_item_id
2608                 , OEOL.item_type_code
2609                 , OEOL.ordered_quantity
2610                 , OEOL.ship_from_org_id
2611                 , decode( OEOL.line_id, OEOL.ato_line_id , null ,
2612                   BIC.wip_supply_type )
2613                 , MSYI.bom_item_type
2614                 , MSYI.replenish_to_order_flag
2615                 , MSYI.pick_components_flag
2616                 , MSYI.base_item_id
2617                 , MSYI.build_in_wip_flag
2618 		, MSYI.atp_flag			--2462661
2619 		, MSYI.atp_components_flag	--2462661
2620 		, 'N'				-- 2723674 : Initializing MLMO flag
2621            INTO
2622                   p_orig_shipset_tracker(j).header_id
2623                 , p_orig_shipset_tracker(j).line_id
2624                 , p_orig_shipset_tracker(j).top_model_line_id
2625                 , p_orig_shipset_tracker(j).ato_line_id
2626                 , p_orig_shipset_tracker(j).link_to_line_id
2627                 , p_orig_shipset_tracker(j).inventory_item_id
2628                 , p_orig_shipset_tracker(j).item_type_code
2629                 , p_orig_shipset_tracker(j).ordered_quantity
2630                 , p_orig_shipset_tracker(j).sourcing_org
2631                 , p_orig_shipset_tracker(j).wip_supply_type
2632                 , p_orig_shipset_tracker(j).bom_item_type
2633                 , p_orig_shipset_tracker(j).replenish_to_order_flag
2634                 , p_orig_shipset_tracker(j).pick_components_flag
2635                 , p_orig_shipset_tracker(j).base_item_id
2636                 , p_orig_shipset_tracker(j).build_in_wip_flag
2637 		, p_orig_shipset_tracker(j).atp_flag
2638 		, p_orig_shipset_tracker(j).atp_components_flag
2639 		, p_orig_shipset_tracker(j).mlmo_flag			-- 2723674 : Initializing MLMO flag
2640            from oe_order_lines_all OEOL , bom_inventory_components BIC , mtl_system_items MSYI
2641            where line_id = p_shipset.identifier(j)
2642              and OEOL.component_sequence_id = BIC.component_sequence_id(+)
2643              and MSYI.inventory_item_id = p_shipset.inventory_item_id(j)
2644              and MSYI.organization_id = l_validation_org
2645            order by line_id ;
2646 
2647            p_orig_shipset_tracker(j).location := j ;  /* note location in original shipset */
2648            p_orig_shipset_tracker(j).sourced_components := FALSE ;
2649            p_orig_shipset_tracker(j).ship_set_name := p_shipset.ship_set_name(j) ;
2650            p_orig_shipset_tracker(j).ordered_quantity := p_shipset.quantity_ordered(j) ;
2651            p_orig_shipset_tracker(j).sourcing_org := p_shipset.source_organization_id(j) ;
2652           /* Added for procure to order */
2653            p_orig_shipset_tracker(j).buy_model := 'N' ;
2654 
2655       EXCEPTION
2656       WHEN others THEN
2657            IF PG_DEBUG <> 0 THEN
2658            	oe_debug_pub.add('populate_om_shipset: ' ||  'Error ' || SQLCODE , 1 );
2659            	oe_debug_pub.add('populate_om_shipset: ' ||  'Error ' || SQLERRM , 1 );
2660            	oe_debug_pub.add('populate_om_shipset: ' ||  ' query ' || p_shipset.identifier(j)  || ' ' ||
2661                  p_shipset.demand_source_header_id(j) || ' ' || l_validation_org , 2 );
2662            END IF;
2663       END ;
2664 
2665       /*
2666       **  mandatory components may cause a problem to calculate plan level and parent ato
2670       THEN
2667       */
2668       l_stmt_num := 10 ;
2669       IF( p_orig_shipset_tracker(j).inventory_item_id <> p_shipset.inventory_item_id(j) )
2671           p_orig_shipset_tracker(j).mandatory_component := TRUE  ;
2672       END IF ;
2673 
2674       /*
2675       **  check whether any models exist
2676       */
2677       l_stmt_num := 12 ;
2678       IF( p_orig_shipset_tracker(j).bom_item_type = '1' )
2679       THEN
2680           p_shipset_contains_model := TRUE  ;
2681       END IF ;
2682 
2683       /*
2684       **  calculate stored_atp_flag for ATP processing (bug 2462661)
2685       **  Note: making a change to p_shipset here, which needs to be
2686       **  sent back to ATP in the reduced shipset structure
2687       */
2688       l_stmt_num := 13 ;
2689       IF ( p_orig_shipset_tracker(j).atp_flag <> 'N' OR p_orig_shipset_tracker(j).atp_components_flag <> 'N')
2690       THEN
2691 	  p_shipset.attribute_06(j) := 'Y';
2692       ELSE
2693 	  p_shipset.attribute_06(j) := 'N';
2694       END IF ;
2695    END LOOP ;
2696 
2697    l_stmt_num := 14 ;
2698    IF PG_DEBUG <> 0 THEN
2699    	oe_debug_pub.add('populate_om_shipset: ' ||  'Displaying contents after populate_om_shipset '  , 3 );
2700 	show_contents( p_orig_shipset_tracker ) ;
2701    END IF;
2702 
2703 EXCEPTION
2704    WHEN FND_API.G_EXC_ERROR THEN
2705         IF PG_DEBUG <> 0 THEN
2706         	oe_debug_pub.add('populate_om_shipset: ' || 'POPULATE_OM_SHIPSET::exp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2707         END IF;
2708         x_return_status := FND_API.G_RET_STS_ERROR;
2709         FND_MSG_PUB.Count_And_Get(
2710           p_count => x_msg_count
2711         , p_data  => x_msg_data
2712         );
2713    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2714         IF PG_DEBUG <> 0 THEN
2715         	oe_debug_pub.add('populate_om_shipset: ' || 'POPULATE_OM_SHIPSET::unexp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2716         END IF;
2717         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2718         FND_MSG_PUB.Count_And_Get(
2719           p_count => x_msg_count
2720         , p_data  => x_msg_data
2721         );
2722    WHEN OTHERS THEN
2723         IF PG_DEBUG <> 0 THEN
2724         	oe_debug_pub.add('populate_om_shipset: ' || 'POPULATE_OM_SHIPSET::others::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2725         END IF;
2726         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2727         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2728            FND_MSG_PUB.Add_Exc_Msg(
2729              G_PKG_NAME
2730            , 'POPULATE_OM_SHIPSET'
2731            );
2732         END IF;
2733 END populate_om_shipset ;
2734 
2735 
2736 /*
2737 ** This procedure propagates any sourcing across the heirarchy and
2738 ** calculates model per quantity
2739 */
2740 PROCEDURE process_sourcing_chain(
2741   p_top_location     number
2742 , p_location         number
2743 , p_org              number
2744 , p_isPhantom        boolean
2745 , p_isProcured       boolean
2746 , p_basis_qty        number
2747 , x_return_status             out varchar2
2748 )
2749 is
2750 v_new_org number ;
2751 v_model_line_id  number ;
2752 v_phantom   boolean ;
2753 v_basis_qty    number ;
2754 p_new_basis_qty    number ;
2755 v_transit_lead_time number ;
2756 v_sourcing_rule_exists varchar2(1) ;
2757 x_msg_data    varchar2(2000) ;
2758 x_msg_count   number ;
2759 l_stmt_num    number ;
2760 v_source_type number;
2761 v_procured    boolean := FALSE;
2762 l_make_buy_code   Number;
2763 x_exp_error_code  Number ;
2764 BEGIN
2765 
2766    IF PG_DEBUG <> 0 THEN
2767    	oe_debug_pub.add('process_sourcing_chain: ' ||  'Entered process sourcing chain ' , 1 );
2768    END IF;
2769 
2770    l_stmt_num := 1 ;
2771    v_new_org := p_org ;
2772    v_model_line_id := local_cto_shipset(p_location).line_id ;
2773    x_return_status := FND_API.G_RET_STS_SUCCESS ;
2774 
2775    IF PG_DEBUG <> 0 THEN
2776    	oe_debug_pub.add('process_sourcing_chain: ' ||  ' p_top_location is '|| to_char(p_top_location) , 5 );
2777    	oe_debug_pub.add('process_sourcing_chain: ' ||  ' p_location is '|| to_char(p_location) , 5 );
2778    END IF;
2779 
2780    IF PG_DEBUG <> 0 THEN
2781    	oe_debug_pub.add('process_sourcing_chain: ' ||  ' processing sourcing chain ' || p_location || ' bom it ' || local_cto_shipset(p_location).bom_item_type || ' line id ' || local_cto_shipset(p_location).line_id , 5 );
2782    END IF;
2783 
2784    l_stmt_num := 10 ;
2785    IF( NOT p_isPhantom AND NOT p_isProcured )
2786    THEN
2787        /*
2788        ** Evaluate sourcing rules only for Models
2789        */
2790        IF( local_cto_shipset(p_location).bom_item_type = '1'  AND
2791            local_cto_shipset(p_location).ato_line_id is not null /* Sushant added this check to not process PTO Models 05-28-02*/
2792          )
2793        THEN
2794            IF( gMrpAssignmentSet is not null )
2795            THEN
2796 
2797                l_stmt_num := 20 ;
2798                CTO_UTILITY_PK.get_model_sourcing_org(
2799                                          local_cto_shipset(p_location).inventory_item_id
2800                                        , local_cto_shipset(p_location).sourcing_org
2801                                        , v_sourcing_rule_exists
2802                                        , v_new_org
2806                                        , x_exp_error_code
2803                                        , v_source_type
2804                                        , v_transit_lead_time
2805                                        , x_return_status
2807                                        , local_cto_shipset(p_location).line_id
2808                                        , local_cto_shipset(p_location).ship_set_name
2809                                        ) ;
2810 
2811                IF PG_DEBUG <> 0 THEN
2812                	oe_debug_pub.add('process_sourcing_chain: ' ||  ' after get model source status ' || x_return_status , 4 );
2813                END IF;
2814 
2815                -- The expected error code from get_model_sourcing_org
2816                -- needs to be copied into g_expected_error_code for further processing
2817 
2818                g_expected_error_code := x_exp_error_code;
2819 
2820                l_stmt_num := 30 ;
2821                IF x_return_status = FND_API.G_RET_STS_ERROR
2822                THEN
2823                    RAISE FND_API.G_EXC_ERROR;
2824                ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
2825                THEN
2826                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2827                END IF;
2828            ELSE  /* assignment set is null */
2829               IF PG_DEBUG <> 0 THEN
2830               	oe_debug_pub.add('process_sourcing_chain: ' ||  '***$$$ Going to check Planning type attribute as assignment set is null ' , 3 );
2831               END IF;
2832 
2833                 --- When there is no sourcing rule defined we need to look at the
2834                 --- Planning_make_buy_code to determine the source_type
2835                 --- If the planning_make_buy_code is 1(Make) we can return as it is
2836                 --- If the planning_make_buy_code is 2(Buy) we need to set the p_source_type to 3 and return
2837                 --- so that the calling application will knwo this as buy model
2838 
2839                 SELECT planning_make_buy_code
2840                 INTO   l_make_buy_code
2841                 FROM   MTL_SYSTEM_ITEMS
2842                 WHERE  inventory_item_id = local_cto_shipset(p_location).inventory_item_id
2843                 AND    organization_id   = p_org;
2844 
2845                 IF l_make_buy_code = 2 THEN
2846                   v_source_type := 3;
2847                 END IF;
2848 
2849            END IF ;
2850 
2851        END IF ;
2852 
2853        l_stmt_num := 40 ;
2854        IF( v_source_type = '3' ) THEN
2855            local_cto_shipset(p_location).buy_model := 'B' ;
2856            local_cto_shipset(p_location).sourcing_org := v_new_org ;
2857            local_cto_shipset(p_top_location).sourced_components := TRUE ;
2858 	   local_cto_shipset(p_location).sourced_components := TRUE ;  --bug2803895
2859        ELSE
2860            local_cto_shipset(p_location).buy_model := 'N' ;
2861            IF( FND_API.to_boolean( v_sourcing_rule_exists ) )
2862            THEN
2863               local_cto_shipset(p_location).sourcing_org := v_new_org ;
2864               local_cto_shipset(p_top_location).sourced_components := TRUE ;
2865 	      local_cto_shipset(p_location).sourced_components := TRUE ;  --bug2803895
2866            END IF ;
2867        END IF ;
2868 
2869    ELSE
2870        /* Phantom properties have higher priority than procurement properties */
2871        IF( p_isPhantom ) THEN
2872            local_cto_shipset(p_location).wip_supply_type := '6' ;
2873            local_cto_shipset(p_location).buy_model := 'N' ;
2874        ELSIF( p_isProcured ) THEN
2875            local_cto_shipset(p_location).buy_model := 'Y' ;
2876        END IF ;
2877    END IF;
2878 
2879    l_stmt_num := 50 ;
2880    /*
2881    ** for each model child of top level model
2882    */
2883    FOR j IN 1..local_cto_shipset.count
2884    LOOP
2885            IF( v_model_line_id = local_cto_shipset(j).link_to_line_id )
2886            THEN
2887                l_stmt_num := 60 ;
2888                IF( local_cto_shipset(p_location).buy_model in('B','Y') ) THEN
2889                    v_procured := TRUE ;
2890                ELSE
2891                    v_procured := FALSE ;
2892                   IF( local_cto_shipset(j).wip_supply_type = '6')
2893                   THEN
2894                      v_phantom := TRUE ;
2895                   ELSE
2896                       v_phantom := FALSE ;
2897                   END IF ;
2898                END IF ;
2899 
2900                v_basis_qty := local_cto_shipset(j).ordered_quantity ;
2901 
2902                l_stmt_num := 70 ;
2903                IF( p_basis_qty <> 0 ) THEN
2904                   p_new_basis_qty := local_cto_shipset(j).ordered_quantity ; /* BUG#2424590*/
2905                   local_cto_shipset(j).ordered_quantity := local_cto_shipset(j).ordered_quantity / p_basis_qty ;
2906                ELSE
2907                   local_cto_shipset(j).ordered_quantity := 0 ;
2908                END IF ;
2909 
2910                /* all components should refer to parents new org */
2911                local_cto_shipset(j).sourcing_org := local_cto_shipset(p_location).sourcing_org ;
2912 
2913                l_stmt_num := 80 ;
2914                process_sourcing_chain(
2915                  p_top_location
2916                , j
2917                , v_new_org
2918                , v_phantom
2919                , v_procured
2923 
2920                , p_new_basis_qty  /* the basis qty should be w.r.t. top model qty BUG#2424590 basis qty should be w.r.t immediate parent , bom per qty concept */
2921                , x_return_status
2922                ) ;
2924                l_stmt_num := 90 ;
2925                IF x_return_status = FND_API.G_RET_STS_ERROR
2926                THEN
2927                    RAISE FND_API.G_EXC_ERROR;
2928                ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
2929                THEN
2930                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2931                END IF;
2932       END IF ;
2933    END LOOP ;
2934 EXCEPTION
2935    WHEN FND_API.G_EXC_ERROR THEN
2936         IF PG_DEBUG <> 0 THEN
2937         	oe_debug_pub.add('process_sourcing_chain: ' || 'PROCESS_SOURCING_CHAIN::exp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2938         END IF;
2939         x_return_status := FND_API.G_RET_STS_ERROR;
2940         FND_MSG_PUB.Count_And_Get(
2941           p_count => x_msg_count
2942         , p_data  => x_msg_data
2943         );
2944    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2945         IF PG_DEBUG <> 0 THEN
2946         	oe_debug_pub.add('process_sourcing_chain: ' || 'PROCESS_SOURCING_CHAIN::unexp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2947         END IF;
2948         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2949         FND_MSG_PUB.Count_And_Get(
2950           p_count => x_msg_count
2951         , p_data  => x_msg_data
2952         );
2953    WHEN OTHERS THEN
2954         IF PG_DEBUG <> 0 THEN
2955         	oe_debug_pub.add('process_sourcing_chain: ' || 'PROCESS_SOURCING_CHAIN::others::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
2956         END IF;
2957         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2958         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2959            FND_MSG_PUB.Add_Exc_Msg(
2960              G_PKG_NAME
2961            , 'PROCESS_SOURCING_CHAIN'
2962            );
2963         END IF;
2964 END process_sourcing_chain ;
2965 
2966 
2967 
2968 /*
2969 ** This procedure checks whether a model has been sourced.
2970 ** It also checks for circular sourcing and flags an error if it detects one.
2971 ** This procedure keeps on chaining sourcing rules till no more sourcing rules exist.
2972 */
2973 PROCEDURE get_model_sourcing_org(
2974   p_inventory_item_id    NUMBER
2975 , p_organization_id      NUMBER
2976 , p_sourcing_rule_exists out varchar2
2977 , p_sourcing_org         out NUMBER
2978 , p_source_type          out NUMBER
2979 , p_transit_lead_time    out NUMBER
2980 , x_return_status        out varchar2
2981 , p_line_id              in NUMBER
2982 , p_ship_set_name        in varchar2
2983 )
2984 IS
2985 v_sourcing_organization_id  number ;
2986 v_assignment_type   number ;
2987 CTO_MRP_ASSIGNMENT_SET  EXCEPTION;
2988 x_msg_data     varchar2(2000) ;
2989 x_msg_count    number ;
2990 l_stmt_num     number ;
2991 l_error_code   number ;
2992 v_organization_id number ;
2993 v_transit_lead_time number ;
2994 TYPE TABNUM is TABLE of NUMBER index by binary_integer ;
2995 v_orgs_tbl         TABNUM ;
2996 v_circular_sourcing boolean ;
2997 v_location          number := 0 ;
2998 v_sourcing_rule_exists varchar2(1) ;
2999 v_source_type       number;
3000 
3001 BEGIN
3002         l_stmt_num :=  1;
3003 
3004         p_sourcing_rule_exists := FND_API.G_FALSE ;
3005         p_transit_lead_time := 0 ;
3006         x_return_status := FND_API.G_RET_STS_SUCCESS ;
3007         v_organization_id := p_organization_id ;
3008         v_transit_lead_time := 0 ;
3009         v_circular_sourcing := FALSE ;
3010         v_orgs_tbl.delete ; /* reinitialize table to check circular sourcing */
3011 
3012         l_stmt_num := 10 ;
3013         <<OUTER>>
3014         WHILE( TRUE )
3015         LOOP
3016 
3017            l_stmt_num := 20 ;
3018            /*
3019            ** check whether the current org exists in the orgs array
3020            */
3021            FOR i IN 1..v_orgs_tbl.count
3022            LOOP
3023               IF( v_orgs_tbl(i) = v_organization_id )
3024               THEN
3025                  v_circular_sourcing := TRUE ;
3026                  v_location := i ;
3027                  exit OUTER ;
3028               END IF ;
3029            END LOOP ;
3030 
3031            v_orgs_tbl(v_orgs_tbl.count + 1 ) := v_organization_id ;
3032            l_stmt_num := 30 ;
3033            query_sourcing_org(
3034                 p_inventory_item_id
3035               , v_organization_id
3036               , v_sourcing_rule_exists
3037               , v_source_type
3038               , v_sourcing_organization_id
3039               , v_transit_lead_time
3040               , x_return_status
3041            ) ;
3042 
3043            l_stmt_num := 40 ;
3044            IF x_return_status = FND_API.G_RET_STS_ERROR
3045            THEN
3046                    RAISE FND_API.G_EXC_ERROR;
3047            ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
3048            THEN
3049                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3050            END IF;
3051 
3052            p_source_type := v_source_type ;
3053            IF( v_source_type = '3' ) THEN
3054                exit ;
3055            ELSE
3059                  exit ; /* always exit when no more sourcing rules to cascade */
3056                IF( FND_API.to_boolean( v_sourcing_rule_exists )  ) THEN
3057                    p_sourcing_rule_exists := 'T' ;
3058                ELSE
3060                END IF ;
3061            END IF ;
3062 
3063            l_stmt_num := 50 ;
3064            /* set the query organization id to current sourcing organization to
3065            ** cascade sourcing rules.
3066            ** e.g.  M1 <- D1 , D1 <- M2  =>  M1 <- M2
3067            */
3068            v_organization_id := v_sourcing_organization_id ;
3069            p_transit_lead_time := p_transit_lead_time + v_transit_lead_time ;
3070 
3071         END LOOP OUTER ;
3072 
3073         l_stmt_num := 60 ;
3074         IF( v_circular_sourcing )
3075         THEN
3076            g_expected_error_code := 66 ;
3077            IF PG_DEBUG <> 0 THEN
3078            	oe_debug_pub.add('get_model_sourcing_org: ' ||  '***Circular sourcing error ' , 1 );
3079            END IF;
3080            RAISE FND_API.G_EXC_ERROR;
3081         END IF ;
3082 
3083         p_sourcing_org := v_organization_id ;
3084         IF PG_DEBUG <> 0 THEN
3085         	oe_debug_pub.add('get_model_sourcing_org: ' || 'Sourcing org is ' || p_sourcing_org || ' lead time ' || to_char( p_transit_lead_time ) , 2 );
3086         END IF;
3087 
3088 EXCEPTION
3089    WHEN FND_API.G_EXC_ERROR THEN
3090         IF PG_DEBUG <> 0 THEN
3091         	oe_debug_pub.add('get_model_sourcing_org: ' || 'GET_MODEL_SOURCING_ORG::exp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
3092         END IF;
3093         x_return_status := FND_API.G_RET_STS_ERROR;
3094 
3095         FND_MSG_PUB.Count_And_Get(
3096           p_count => x_msg_count
3097         , p_data  => x_msg_data
3098         );
3099 
3100    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3101         IF PG_DEBUG <> 0 THEN
3102         	oe_debug_pub.add('get_model_sourcing_org: ' || 'GET_MODEL_SOURCING_ORG::unexp error::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
3103         END IF;
3104         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3105 
3106         FND_MSG_PUB.Count_And_Get(
3107           p_count => x_msg_count
3108         , p_data  => x_msg_data
3109         );
3110 
3111    WHEN OTHERS THEN
3112         IF PG_DEBUG <> 0 THEN
3113         	oe_debug_pub.add('get_model_sourcing_org: ' || 'GET_MODEL_SOURCING_ORG::others::'||to_char(l_stmt_num)||'::'||sqlerrm,1);
3114         END IF;
3115         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3116 
3117         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3118            FND_MSG_PUB.Add_Exc_Msg(
3119              G_PKG_NAME
3120            , 'GET_MODEL_SOURCING_ORG'
3121            );
3122         END IF;
3123 
3124 END get_model_sourcing_org ;
3125 
3126 
3127 /*
3128 ** This procedure checks for existence of any sourcing rules for a given Item.
3129 ** An Item will be considered sourced if the sourcing rule type is 'TRANSFER FROM'.
3130 ** This procedure flags an error if multiple sourcing rules exist for an Item.
3131 ** A no data found for sourcing rule query or a 'MAKE AT' sourcing rule is considered as END of sourcing chain.
3132 */
3133 PROCEDURE query_sourcing_org(
3134   p_inventory_item_id    NUMBER
3135 , p_organization_id      NUMBER
3136 , p_sourcing_rule_exists out varchar2
3137 , p_source_type          out NUMBER
3138 , p_sourcing_org         out NUMBER
3139 , p_transit_lead_time    out NUMBER
3140 , x_return_status        out varchar2
3141 )
3142 is
3143 v_sourcing_rule_id number ;
3144 l_stmt_num         number ;
3145 v_source_type      varchar2(1) ;
3146 v_sourcing_rule_count number;
3147 
3148 l_make_buy_code     number;
3149 BEGIN
3150      /*
3151      ** This routine should consider no data found or one make at sourcing rule
3152      ** as no sourcing rule exist.
3153      */
3154            l_stmt_num := 1 ;
3155            p_sourcing_rule_exists := FND_API.G_FALSE ;
3156            x_return_status := FND_API.G_RET_STS_SUCCESS ;
3157            p_transit_lead_time := 0 ;
3158 
3159            /*
3160            ** Fix for Bug 1610583
3161            ** Source Type values in MRP_SOURCES_V
3162            ** 1 = Transfer From, 2 = Make At, 3 = Buy From.
3163            */
3164            -- If the sourcing is defined in the org level the source_type
3165            -- will be null. Still we need to see that sourcing rule. So the condition
3166            -- Source_type <> 3 is replaced with nvl(source_type,1). When the source_type is
3167            -- Null it will be defaulted to 1(Transfer from).
3168            /* Please note the changes done for procuring config project */
3169            -- Since the buy sourcing needs to be supported the where condition for msv.source_type is removed
3170            -- from the following query.
3171 
3172            l_stmt_num := 10 ;
3173            BEGIN
3174               select distinct
3175                 source_organization_id,
3176                 sourcing_rule_id,
3177                 nvl(source_type,1) ,
3178                 nvl( avg_transit_lead_time , 0 )
3179               into
3180                 p_sourcing_org
3181               , v_sourcing_rule_id
3182               , v_source_type
3183               , p_transit_lead_time
3184               from mrp_sources_v msv
3188                 and nvl(effective_date,sysdate) <= nvl(disable_date, sysdate)
3185               where msv.assignment_set_id = gMrpAssignmentSet
3186                 and msv.inventory_item_id = p_inventory_item_id
3187                 and msv.organization_id = p_organization_id
3189                 and nvl(disable_date, sysdate+1) > sysdate;
3190 
3191               /*
3192               ** item is multi-org if sourcing rule is transfer from.
3193               */
3194               l_stmt_num := 20 ;
3195               p_source_type := v_source_type ;
3196 
3197               IF( v_source_type = 1 ) THEN
3198                   p_sourcing_rule_exists := FND_API.G_TRUE ;
3199               ELSIF v_source_type = 3 THEN
3200                   IF PG_DEBUG <> 0 THEN
3201                   	oe_debug_pub.add('query_sourcing_org: ' || 'Buy Sourcing rule exists...',4);
3202                   END IF;
3203               END IF ;
3204 
3205               IF PG_DEBUG <> 0 THEN
3206               	oe_debug_pub.add('query_sourcing_org: ' ||  '****$$$$ IID ' || p_inventory_item_id || ' in org ' ||
3207                       p_organization_id || ' is sourced from org ' || p_sourcing_org ||
3208                       ' type ' || v_source_type || ' $$$$****' , 5 );
3209               END IF;
3210 
3211            EXCEPTION
3212               WHEN NO_DATA_FOUND THEN
3213                 IF PG_DEBUG <> 0 THEN
3214                 	oe_debug_pub.add('query_sourcing_org: ' ||  ' came into no data when finding source ' || to_char(l_stmt_num ) , 1  );
3215                 END IF;
3216                 /* removed no sourcing flag as cascading of sourcing rules will
3217                 ** be continued till no more sourcing rules can be cascaded
3218                 ** add check for buy attribute for model
3219                 */
3220                 --- When there is no sourcing rule defined we need to look at the
3221                 --- Planning_make_buy_code to determine the source_type
3222                 --- If the planning_make_buy_code is 1(Make) we can return as it is
3223                 --- If the planning_make_buy_code is 2(Buy) we need to set the p_source_type to 3 and return
3224                 --- so that the calling application will knwo this as buy model
3225 
3226                 SELECT planning_make_buy_code
3227                 INTO   l_make_buy_code
3228                 FROM   MTL_SYSTEM_ITEMS
3229                 WHERE  inventory_item_id = p_inventory_item_id
3230                 AND    organization_id   = p_organization_id;
3231 
3232                 IF l_make_buy_code = 2 THEN
3233                   p_source_type := 3;
3234                 END IF;
3235 
3236               WHEN TOO_MANY_ROWS THEN
3237                 IF PG_DEBUG <> 0 THEN
3238                 	oe_debug_pub.add('query_sourcing_org: ' ||  ' came into too_many when finding source ' || to_char(l_stmt_num)  , 1  );
3239                 END IF;
3240               select count(*)
3241               into v_sourcing_rule_count
3242               from mrp_sources_v msv
3243               where msv.assignment_set_id = gMrpAssignmentSet
3244                 and msv.inventory_item_id = p_inventory_item_id
3245                 and msv.organization_id = p_organization_id
3246                 and nvl(msv.source_type,1) <> 3
3247                 and nvl(effective_date,sysdate) <= nvl(disable_date, sysdate)
3248                 and nvl(disable_date, sysdate+1) > sysdate;
3249 
3250                 IF( v_sourcing_rule_count > 0 ) THEN
3251                     x_return_status                := FND_API.G_RET_STS_ERROR;
3252                     g_expected_error_code := 66;
3253                 ELSE
3254                     p_source_type := 3 ;
3255                 END IF ;
3256 
3257               WHEN OTHERS THEN
3258                 IF PG_DEBUG <> 0 THEN
3259                 	oe_debug_pub.add('query_sourcing_org: ' ||  'QUERY_SOURCING_ORG::others:: ' ||
3260                                    to_char(l_stmt_num) || '::' ||
3261                                   ' came into others when finding source ' , 1  );
3262                 	oe_debug_pub.add('query_sourcing_org: ' ||  ' SQLCODE ' || SQLCODE , 1 );
3263                 	oe_debug_pub.add('query_sourcing_org: ' ||  ' SQLERRM ' || SQLERRM  , 1 );
3264                 	oe_debug_pub.add('query_sourcing_org: ' ||  ' came into others when finding source ' , 1  );
3265                 END IF;
3266 
3267                 x_return_status                := FND_API.G_RET_STS_UNEXP_ERROR;
3268            END ;
3269 END query_sourcing_org ;
3270 
3271 
3272 
3273 /*
3274 ** This procedure finds the level of the components in its heirarchy.
3275 ** The levels are numbered for ATO models and their components.
3276 ** However PTO models and PTO components are marked as level 0.
3277 */
3278 PROCEDURE populate_plan_level(
3279   p_t_bcol  in out NOCOPY CTO_SHIPSET_TBL_TYPE
3280 )
3281  is
3282  TYPE TABNUM is TABLE of NUMBER index by binary_integer ;
3283  v_raw_line_id TABNUM ;
3284  v_src_point   number ;
3285  j             number ;
3286  v_step        VARCHAR2(10) ;
3287  BEGIN
3288     /*
3289     ** Strategy: Resolve plan_level for each line item by setting it to 1 + plan_level of parent.
3290     ** use the link_to_line_id column to get to the parent. if parents plan_level is not yet
3291     ** resolved, go to its immediate ancestor recursively till you find a line item with
3292     ** plan_level set( Top level plan level is always set to zero ). When coming out of recursion
3293     ** set the plan_level of any ancestors that havent been resolved yet.
3297     */
3294     ** Implementation: As Pl/Sql does not support Stack built-in-datatype, an equivalent behavior
3295     ** can be achieved by storing items in a table( PUSH implementation) and retrieving them from
3296     ** the end of the table ( POP implmentation [LIFO] )
3298         v_step := 'Step B1' ;
3299     FOR i IN p_t_bcol.first..p_t_bcol.last
3300     LOOP
3301        IF( p_t_bcol.exists(i)  ) THEN
3302           v_src_point := i ;
3303           /*
3304           ** resolve plan level for item only if not yet resolved
3305           */
3306           WHILE( p_t_bcol(v_src_point).plan_level is null )
3307           LOOP
3308              IF PG_DEBUG <> 0 THEN
3309              	oe_debug_pub.add('populate_plan_level: ' ||  ' v_src_point ' || v_src_point , 5 );
3310              END IF;
3311 
3312              v_raw_line_id(v_raw_line_id.count + 1 ) := v_src_point  ;
3313 
3314              /* store each unresolved item in its heirarchy */
3315              IF( p_t_bcol(v_src_point).link_to_line_id is not null AND
3316                  p_t_bcol(v_src_point).ato_line_id is not null ) THEN
3317                  /* stop right at top ato line id in the case of pto-ato hybrid */
3318                  IF( p_t_bcol(v_src_point).line_id =
3319                      p_t_bcol(v_src_point).ato_line_id )
3320                  THEN
3321                     p_t_bcol(v_src_point).plan_level := 0 ;
3322                     exit ;
3323                  ELSE
3324                     v_src_point := p_t_bcol(v_src_point).link_to_line_id ;
3325                  END IF ;
3326 
3327              ELSE
3328                  /* assign level = 0 for single ato or single pto */
3329                  p_t_bcol(v_src_point).plan_level := 0 ;
3330                  exit ;
3331              END IF;
3332           END LOOP ;
3333 
3334         v_step := 'Step B2' ;
3335           j := v_raw_line_id.count ; /* total number of items to be resolved */
3336           WHILE( j >= 1 )
3337           LOOP
3338              p_t_bcol(v_raw_line_id(j)).plan_level := p_t_bcol(v_src_point).plan_level + 1;
3339              v_src_point := v_raw_line_id(j) ;
3340              j := j -1 ;
3341           END LOOP ;
3342 
3343           v_raw_line_id.delete ; /* remove all elements as they have been resolved */
3344 
3345        END IF ;
3346     END LOOP ;
3347 
3348  EXCEPTION
3349  WHEN others THEN
3350    IF PG_DEBUG <> 0 THEN
3351    	oe_debug_pub.add('populate_plan_level: ' ||  ' error ' || SQLCODE , 1 );
3352 
3353    	oe_debug_pub.add('populate_plan_level: ' ||  ' error ' || SQLERRM , 1 );
3354    END IF;
3355    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3356  END populate_plan_level ;
3357 
3358 
3359 
3360 /*
3361 ** This procedure populates the parent_ato_line_id for a component in a given model heirarchy.
3362 ** This information is populated only for components under an ATO model.
3363 */
3364 PROCEDURE populate_parent_ato(
3365   p_t_bcol  in out NOCOPY CTO_SHIPSET_TBL_TYPE
3366 , p_bcol_line_id in      oe_order_lines.line_id%type
3367 )
3368  is
3369  TYPE TABNUM is TABLE of NUMBER index by binary_integer ;
3370  v_raw_line_id TABNUM ;
3371  v_src_point   number ;
3372  v_prev_src_point   number ;
3373  j             number ;
3374  v_step        VARCHAR2(10) ;
3375  BEGIN
3376     /*
3377     ** Strategy: Resolve parent_ato for each line item by setting it to 1 + plan_level of parent.
3378     ** use the link_to_line_id column to get to the parent. if parents plan_level is not yet
3379     ** resolved, go to its immediate ancestor recursively till you find a line item with
3380     ** plan_level set( Top level plan level is always set to zero ). When coming out of recursion
3381     ** set the plan_level of any ancestors that havent been resolved yet.
3382     ** Implementation: As Pl/Sql does not support Stack built-in-datatype, an equivalent behavior
3383     ** can be achieved by storing items in a table( PUSH implementation) and retrieving them from
3384     ** the END of the table ( POP implmentation [LIFO] )
3385     */
3386 
3387     v_step := 'Step C1' ;
3388     FOR i IN p_t_bcol.first..p_t_bcol.last
3389     LOOP
3390        IF( p_t_bcol.exists(i)  ) THEN
3391           v_src_point := i ;
3392 
3393        IF( p_t_bcol(v_src_point).ato_line_id is not null ) THEN
3394           /* please note, here it stores the index which is the same as line_id due to sparse array*/
3395           IF PG_DEBUG <> 0 THEN
3396           	oe_debug_pub.add('populate_parent_ato: ' ||  ' processing ' || to_char( v_src_point ) , 4 );
3397           END IF;
3398           /*
3399           ** resolve parent ato line id for item.
3400           */
3401 
3402         v_step := 'Step C2' ;
3403           WHILE( p_t_bcol.exists(v_src_point) )
3404           LOOP
3405              v_raw_line_id(v_raw_line_id.count + 1 ) := v_src_point  ;
3406              IF( p_t_bcol(v_src_point).line_id =
3407                  p_t_bcol(v_src_point).ato_line_id )
3408              THEN
3409                 exit ;
3410              END IF ;
3411 
3412              /* store each unresolved item in its heirarchy */
3413              v_prev_src_point := v_src_point ;
3414              v_src_point := p_t_bcol(v_src_point).link_to_line_id ;
3415 
3416              IF PG_DEBUG <> 0 THEN
3417              	oe_debug_pub.add('populate_parent_ato: ' ||  'prev point ' || to_char( v_prev_src_point ) || ' bcol ' || to_char( p_bcol_line_id ) , 5 );
3418              END IF;
3419 
3420              IF( v_src_point is null  ) THEN
3421                 v_src_point := v_prev_src_point ;
3425              IF(  p_t_bcol(v_src_point).ato_line_id is null  or v_prev_src_point = p_bcol_line_id ) THEN
3422                 exit ;
3423              END IF ;
3424 
3426                 v_src_point := v_prev_src_point ;
3427                  /* break IF pto is on top of top level ato or
3428                     the current lineid is top level phantom ato
3429                  v_src_point := null ;
3430                  */
3431                  exit ;
3432              END IF ;
3433 
3434              IF( p_t_bcol(v_src_point).bom_item_type = '1' AND
3435                  p_t_bcol(v_src_point).ato_line_id is not null AND
3436                  nvl( p_t_bcol(v_src_point).wip_supply_type , 0 ) <> '6' ) THEN
3437                    exit ;
3438                   /* break if non phantom ato parent found */
3439              END IF ;
3440           END LOOP ;
3441 
3442           j := v_raw_line_id.count ; /* total number of items to be resolved */
3443 
3444         v_step := 'Step C3' ;
3445           WHILE( j >= 1 )
3446           LOOP
3447              p_t_bcol(v_raw_line_id(j)).parent_ato_line_id := v_src_point ;
3448              j := j -1 ;
3449           END LOOP ;
3450 
3451           /* remove all elements as they have been resolved */
3452           v_raw_line_id.delete ;
3453 
3454         END IF ; /* check whether ato_line_id is not null */
3455 
3456        END IF ;
3457     END LOOP ;
3458  END populate_parent_ato ;
3459 
3460 
3461 
3462 /*
3463 ** This procedure propagates the ato_line_id for a component in a given model heirarchy
3464 ** for calls originating from CZ.
3465 */
3466 PROCEDURE propagate_ato_line_id(
3467   p_location         number
3468 )
3469 is
3470 v_model_line_id  number ;
3471 BEGIN
3472    IF PG_DEBUG <> 0 THEN
3473    	oe_debug_pub.add('propagate_ato_line_id: ' ||  'Entered propagate_ato_line_id p_location ' || p_location , 2 );
3474    END IF;
3475 
3476    IF( p_location is null ) THEN
3477        return ;
3478    END IF ;
3479 
3480    v_model_line_id := g_requests_tab(p_location).line_id ;
3481    /*
3482    ** for each model child of top level model
3483    */
3484    FOR j IN g_requests_tab.first..g_requests_tab.last
3485    LOOP
3486       IF( g_requests_tab.exists(j)) THEN
3487       IF( v_model_line_id = g_requests_tab(j).link_to_line_id ) THEN
3488           IF( g_requests_tab(p_location).ato_line_id is not null ) THEN
3489 				  g_requests_tab(j).ato_line_id := g_requests_tab(p_location).ato_line_id ;
3490           ELSE
3491                    /*
3492                    ** check whether this item is an ATO MODEL!!
3493                    */
3494                    IF( g_requests_tab(j).bom_item_type = '1' AND
3495 		       g_requests_tab(j).replenish_to_order_flag = 'Y'
3496 		      ) THEN
3497 		      g_requests_tab(j).ato_line_id := g_requests_tab(j).line_id ;
3498                    END IF ;
3499           END IF;
3500 
3501       ELSIF( g_requests_tab(j).link_to_line_id is null ) THEN
3502                    IF( g_requests_tab(j).bom_item_type = '1' AND
3503 		       g_requests_tab(j).replenish_to_order_flag = 'Y'
3504 		      ) THEN
3505 		      g_requests_tab(j).ato_line_id := g_requests_tab(j).line_id ;
3506                    END IF ;
3507       END IF ;
3508      END IF ;
3509    END LOOP ;
3510 
3511    propagate_ato_line_id( g_requests_tab.next(p_location ) ) ;
3512 
3513 END propagate_ato_line_id ;
3514 
3515 
3516 
3517 /*
3518 ** This procedure is called in the post-atp phase for reconstructing the original shipset
3519 ** from the reduced shipset.
3520 ** A set of reduced shipset is passed with a supporting structure to indicate the atp success/failure status.
3521 ** This procedure reconstructs all the shipsets by consulting the saved shipset structure.
3522 */
3523 PROCEDURE reconstruct_shipset(
3524   p_shipset    in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
3525 , p_success_flag_tbl in   MRP_ATP_PUB.SHIPSET_STATUS_REC_TYPE
3526 , x_return_status out     varchar2
3527 , x_msg_count     out     number
3528 , x_msg_data      out     varchar2
3529 )
3530 is
3531 v_reduced_shipset    MRP_ATP_PUB.ATP_REC_TYP  ;
3532 v_reduced_shipset_init    MRP_ATP_PUB.ATP_REC_TYP  ;
3533 v_shipset_init    MRP_ATP_PUB.ATP_REC_TYP  ;
3534 v_null_atp_rec    MRP_ATP_PUB.ATP_REC_TYP  ;
3535 v_shipset_loc   number ;
3536 v_char         varchar2(1) := FND_API.G_TRUE ;
3537 v_success_status boolean ;
3538 l_stmt_num     number ;
3539 k              number:=0;
3540 v_auto_gen boolean;
3541 v_process_demand boolean;
3542 
3543 BEGIN
3544 
3545   l_stmt_num := 1 ;
3546 
3547   IF PG_DEBUG <> 0 THEN
3548   	oe_debug_pub.add('reconstruct_shipset: ' ||  'Before reconstruct shipset ' , 1 );
3549 	print_shipset( p_shipset ) ;
3550   	oe_debug_pub.add('reconstruct_shipset: ' ||  'Done printing before reconstruct shipset ' , 4 );
3551   	oe_debug_pub.add('reconstruct_shipset: ' ||  'Printing p_success_flag_tbl sent by ATP', 4);
3552   END IF;
3553 
3554   /* removed stmt FOR j IN g_shipset_status_tbl.first..g_shipset_status_tbl.last */
3555 
3556   FOR j IN p_success_flag_tbl.ship_set_name.first..p_success_flag_tbl.ship_set_name.last
3557    LOOP
3558 	IF PG_DEBUG <> 0 THEN
3559 		oe_debug_pub.add('reconstruct_shipset: ' || 'Contents of shipset status table::', 3);
3563    END LOOP;
3560 		oe_debug_pub.add('reconstruct_shipset: ' ||  p_success_flag_tbl.ship_set_name(j), 3);
3561 		oe_debug_pub.add('reconstruct_shipset: ' ||  p_success_flag_tbl.status(j), 3);
3562 	END IF;
3564   IF PG_DEBUG <> 0 THEN
3565   	oe_debug_pub.add('reconstruct_shipset: ' ||  'Done printing p_success_flag_tbl sent by ATP', 3);
3566   END IF;
3567 
3568 
3569 
3570   FOR j IN g_shipset_status_tbl.first..g_shipset_status_tbl.last
3571    LOOP
3572 	IF PG_DEBUG <> 0 THEN
3573 		oe_debug_pub.add('reconstruct_shipset: ' || 'Contents of g_shipset_status table::', 3);
3574 		oe_debug_pub.add('reconstruct_shipset: ' ||  g_shipset_status_tbl(j).ship_set_name, 3);
3575 	END IF;
3576    END LOOP;
3577   IF PG_DEBUG <> 0 THEN
3578   	oe_debug_pub.add('reconstruct_shipset: ' ||  'Done printing g_shipset_status_tbl sent by ATP', 3);
3579   END IF;
3580 
3581 
3582 
3583 
3584 
3585   l_stmt_num := 10 ;
3586   IF( p_shipset.group_ship_date.count = 1 AND p_shipset.group_ship_date(1) is null )
3587   THEN
3588      p_shipset.group_ship_date(1) := p_shipset.ship_date(1) ;
3589   END IF ;
3590 
3591    l_stmt_num := 20 ;
3592    FOR j IN g_shipset_status_tbl.first..g_shipset_status_tbl.last
3593    LOOP
3594        IF PG_DEBUG <> 0 THEN
3595        	oe_debug_pub.add('reconstruct_shipset: ' ||  'Processing shipset ' || g_shipset_status_tbl(j).ship_set_name , 2 );
3596        END IF;
3597 
3598        l_stmt_num := 30 ;
3599        FOR m IN p_success_flag_tbl.ship_set_name.first..p_success_flag_tbl.ship_set_name.last
3600        LOOP
3601           l_stmt_num := 40 ;
3602           IF( p_success_flag_tbl.ship_set_name(m) = g_shipset_status_tbl(j).ship_set_name ) THEN
3603               l_stmt_num := 50 ;
3604 	      IF( FND_API.to_boolean(p_success_flag_tbl.status(m)))
3605               THEN
3606               g_shipset_status_tbl(j).success_status := TRUE ;
3607               ELSE
3608               g_shipset_status_tbl(j).success_status := FALSE ;
3609               END IF ;
3610               exit ;
3611           END IF ;
3612        END LOOP ;
3613 
3614        l_stmt_num := 60 ;
3615        v_reduced_shipset := v_reduced_shipset_init ;
3616        FOR i IN p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
3617        LOOP
3618           l_stmt_num := 70 ;
3619           if( p_shipset.ship_set_name(i) is null ) then
3620               p_shipset.ship_set_name(i) := p_shipset.identifier(i) ;
3621 
3622              oe_debug_pub.add( ' initialized' || p_shipset.identifier(i), 5 ) ;
3623           end if ;
3624 
3625           oe_debug_pub.add( 'reconstruct_shipset: comparing ' || p_shipset.ship_set_name(i) || ' -> '
3626                            || g_shipset_status_tbl(j).ship_set_name , 5 ) ;
3627           IF( p_shipset.ship_set_name(i) = g_shipset_status_tbl(j).ship_set_name ) THEN
3628               MRP_ATP_PVT.assign_atp_input_rec(
3629                    p_shipset,
3630                    i,
3631                    v_reduced_shipset,
3632                    x_return_status );
3633 
3634              oe_debug_pub.add( ' copied ' || x_return_status , 5 ) ;
3635           END IF ;
3636        END LOOP ;
3637 
3638        l_stmt_num := 80 ;
3639 
3640        g_shipset := v_shipset_init ;
3641        IF PG_DEBUG <> 0 THEN
3642        	oe_debug_pub.add('reconstruct_shipset: ' ||  'Copying final shipset, sount is:: ' || g_final_shipset.ship_set_name.count ,5 );
3643        	oe_debug_pub.add('reconstruct_shipset: ' ||  'v_reduced_shipset count is:: ' || v_reduced_shipset.ship_set_name.count ,5 );
3644        END IF;
3645 
3646        FOR i IN g_final_shipset.ship_set_name.first..g_final_shipset.ship_set_name.last
3647        LOOP
3648           l_stmt_num := 90 ;
3649           IF( g_final_shipset.ship_set_name(i) = g_shipset_status_tbl(j).ship_set_name ) THEN
3650               MRP_ATP_PVT.assign_atp_input_rec(
3651                   g_final_shipset,
3652                   i,
3653                   g_shipset,
3654                   x_return_status );
3655           END IF ;
3656        END LOOP ;
3657 
3658        g_cto_shipset.delete ;
3659        g_cto_sparse_shipset.delete ;
3660 
3661        l_stmt_num := 100 ;
3662        FOR i IN g_final_cto_shipset.first..g_final_cto_shipset.last
3663        LOOP
3664 
3665           l_stmt_num := 110 ;
3666           IF( g_final_cto_shipset(i).ship_set_name = g_shipset_status_tbl(j).ship_set_name ) THEN
3667               g_cto_shipset(g_cto_shipset.count ) := g_final_cto_shipset(i) ;
3668               g_cto_sparse_shipset(g_final_cto_shipset(i).line_id ) := g_final_cto_shipset(i) ;
3669            IF PG_DEBUG <> 0 THEN
3670            	oe_debug_pub.add('reconstruct_shipset: ' ||  'Added line_id' || g_final_cto_shipset(i).line_id || ' to g_cto_shipset and g_final_cto_shipset',  1 );
3671            END IF;
3672           END IF ;
3673        END LOOP ;
3674 
3675        IF PG_DEBUG <> 0 THEN
3676        	oe_debug_pub.add('reconstruct_shipset: ' ||  ' CALLING RESURRECT shipset '  , 1 );
3677        END IF;
3678 
3679        l_stmt_num := 120 ;
3680        resurrect_shipset(
3681           v_reduced_shipset
3682         , g_shipset_status_tbl(j).success_status
3683         , x_return_status
3684         , x_msg_count
3685         , x_msg_data
3686       ) ;
3687 
3688        /* Added this call to populate configuration status
3689           to decide how to populate visible demand flag
3690        */
3694           v_reduced_shipset
3691           populate_configuration_status ;
3692 
3693           populate_visible_demand(
3695         , g_shipset_status_tbl(j).success_status
3696         , x_return_status
3697         , x_msg_count
3698         , x_msg_data
3699          ) ;
3700 
3701      /*
3702      ** copy the enhanced shipset data back to final shipset
3703      */
3704        l_stmt_num := 130 ;
3705        IF PG_DEBUG <> 0 THEN
3706        	oe_debug_pub.add('reconstruct_shipset: ' || 'Redecued shipset name count = '||v_reduced_shipset.ship_set_name.count,2);
3707        END IF;
3708 
3709        FOR i IN v_reduced_shipset.ship_set_name.first..v_reduced_shipset.ship_set_name.last
3710        LOOP
3711           l_stmt_num := 140 ;
3712           IF  g_shipset_status_tbl(j).ship_set_name = v_reduced_shipset.ship_set_name(i)  THEN
3713           k := k+1;
3714           v_shipset_loc := k + g_shipset_status_tbl(j).start_location - 1;
3715 
3716           IF PG_DEBUG <> 0 THEN
3717           	oe_debug_pub.add('reconstruct_shipset: ' ||  'start =' || g_shipset_status_tbl(j).start_location || ' loc =' || v_shipset_loc || ' k = ' || k  , 5 );
3718           END IF;
3719 
3720           g_final_shipset.Available_Quantity(v_shipset_loc) := v_reduced_shipset.available_quantity(k) ;
3721           g_final_shipset.Requested_Date_Quantity(v_shipset_loc) := v_reduced_shipset.requested_date_quantity(k) ;
3722           g_final_shipset.Group_Ship_Date(v_shipset_loc) := v_reduced_shipset.group_ship_date(k) ;
3723           g_final_shipset.Ship_Date(v_shipset_loc) := v_reduced_shipset.ship_date(k) ;
3724           g_final_shipset.Group_Arrival_Date(v_shipset_loc) := v_reduced_shipset.group_arrival_date(k) ;
3725           g_final_shipset.error_code(v_shipset_loc) := v_reduced_shipset.error_code(k) ;
3726           /* BUG#2158449 added as per navneet on 03-27-2002 */
3727           g_final_shipset.Arrival_Date(v_shipset_loc) :=v_reduced_shipset.arrival_date(k)  ;
3728           g_final_shipset.delivery_lead_time(v_shipset_loc) := v_reduced_shipset.delivery_lead_time(k) ;
3729           g_final_shipset.source_organization_id (v_shipset_loc) :=v_reduced_shipset.source_organization_id(k)  ;
3730           g_final_shipset.ship_method(v_shipset_loc) :=v_reduced_shipset.ship_method(k) ;
3731           g_final_shipset.END_pegging_id(v_shipset_loc) :=v_reduced_shipset.END_pegging_id(k)  ;
3732           g_final_shipset.attribute_05(v_shipset_loc) := v_reduced_shipset.attribute_05(k);
3733 	  END IF;
3734        END LOOP ;
3735 
3736        k := 0;
3737 
3738    END LOOP ;
3739 
3740    IF PG_DEBUG <> 0 THEN
3741    	oe_debug_pub.add('reconstruct_shipset: ' ||  'Done reconstruct shipset ' ,1 );
3742    END IF;
3743 
3744   print_shipset( g_final_shipset ) ;
3745 
3746 
3747   /* overwrite values from null shipset to original shipset location */
3748   for v_no_name in p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
3749   LOOP
3750       IF( p_shipset.ship_set_name(v_no_name) is null ) THEN
3751 
3752       FOR v_index IN g_final_shipset.ship_set_name.first..g_final_shipset.ship_set_name.last
3753       LOOP
3754 
3755          IF( p_shipset.identifier(v_no_name) = g_final_shipset.identifier(v_index) AND
3756              p_shipset.inventory_item_id(v_no_name) =
3757              g_final_shipset.inventory_item_id(v_index)
3758            ) THEN
3759 
3760              g_final_shipset.Available_Quantity(v_index) := p_shipset.available_quantity(v_no_name) ;
3761              g_final_shipset.Requested_Date_Quantity(v_index) := p_shipset.requested_date_quantity(v_no_name) ;
3762              g_final_shipset.Group_Ship_Date(v_index) := p_shipset.group_ship_date(v_no_name) ;
3763              g_final_shipset.Ship_Date(v_index) := p_shipset.ship_date(v_no_name) ;
3764              g_final_shipset.Group_Arrival_Date(v_index) := p_shipset.group_arrival_date(v_no_name) ;
3765              g_final_shipset.error_code(v_index) := p_shipset.error_code(v_no_name) ;
3766              g_final_shipset.attribute_05(v_index) := p_shipset.attribute_05(v_no_name);
3767 
3768          END IF ;
3769 
3770      END LOOP ;
3771 
3772      END IF ;
3773 
3774   END LOOP ;
3775 
3776 
3777    IF PG_DEBUG <> 0 THEN
3778    	oe_debug_pub.add('reconstruct_shipset: ' ||  'Going to null out shipset names ' ,3 );
3779    END IF;
3780   /* null out the auto generated shipset names */
3781 
3782   FOR v_auto_shipset IN g_final_shipset.ship_set_name.first..g_final_shipset.ship_set_name.last
3783   LOOP
3784 
3785       isAutoGeneratedShipset(
3786 	g_final_shipset.ship_set_name(v_auto_shipset),
3787 	v_auto_gen,
3788 	v_process_demand);
3789 
3790       IF v_auto_gen THEN
3791           g_final_shipset.ship_set_name(v_auto_shipset) := null ;
3792       END IF ;
3793 
3794       IF( g_final_shipset.error_code.exists(v_auto_shipset)) THEN
3795           IF( g_final_shipset.error_code(v_auto_shipset) is null ) THEN
3796               g_final_shipset.error_code(v_auto_shipset) := 0 ;
3797           END IF ;
3798       END IF ;
3799 
3800   END LOOP ;
3801 
3802    p_shipset := g_final_shipset ;
3803 
3804    l_stmt_num := 150 ;
3805    --remove_elements_from_atp_rec( v_reduced_shipset ) ;
3806    v_reduced_shipset := v_null_atp_rec;
3807 
3808    IF PG_DEBUG <> 0 THEN
3809    	oe_debug_pub.add('reconstruct_shipset: ' ||  ' Done null shipset copy ' ,4 );
3810    END IF;
3811 
3812    print_shipset( p_shipset ) ;
3813 
3814 EXCEPTION
3815  WHEN others THEN
3816    IF PG_DEBUG <> 0 THEN
3817    	oe_debug_pub.add('reconstruct_shipset: ' ||  ' error encountered in reconstruct_shipset at line ' || to_char(l_stmt_num ) , 1 );
3818    	oe_debug_pub.add('reconstruct_shipset: ' ||  ' error ' || SQLCODE , 1 );
3819    	oe_debug_pub.add('reconstruct_shipset: ' ||  ' error ' || SQLERRM , 1 );
3820    END IF;
3821    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3822 
3823 END reconstruct_shipset ;
3824 
3825 
3826 
3827 /*
3828 ** This procedure accepts a reduced shipset and resurrects it with the saved shipset information.
3829 ** This procedure also propagates information returned by ATP for each model to its child components
3830 ** depending on the status of the ATP result for the shipset.
3831 */
3832 /* BUGFIX 2406559 for this procedure at multiple places
3833 */
3834 PROCEDURE resurrect_shipset(
3835   p_shipset    in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
3836 , p_success_flag in boolean
3837 , x_return_status out     varchar2
3838 , x_msg_count     out     number
3839 , x_msg_data      out     varchar2
3840 )
3841 is
3842 v_new_available_quantity number ;
3843 v_new_requested_date_quantity number ;
3844 v_new_group_ship_date DATE ;
3845 v_new_ship_date DATE ;
3846 v_new_group_arrival_date  DATE ;
3847 v_shipset_loc        number ;
3848 v_curr_line_id        number ;
3849 l_stmt_num           number ;
3850 v_new_arrival_date     DATE ;
3851 v_new_delivery_lead_time  number ;
3852 v_new_source_organization_id  number ;
3853 v_new_ship_method  VARCHAR2(40) ;
3854 v_new_end_pegging_id  number ;
3855 v_action       number ;
3856 
3857 BEGIN
3858 
3859   l_stmt_num := 1 ;
3860   IF PG_DEBUG <> 0 THEN
3861   	oe_debug_pub.add('resurrect_shipset: ' ||  ' printing reduced shipset before resurrection ' , 2 );
3862   	print_shipset( p_shipset ) ;
3863   END IF;
3864   v_action := p_shipset.action(p_shipset.action.first) ;
3865 
3866   IF( p_success_flag ) THEN
3867    IF PG_DEBUG <> 0 THEN
3868    	oe_debug_pub.add('resurrect_shipset: ' ||  ' p_success_flag is true ' , 2 );
3869    END IF;
3870   ELSE
3871    IF PG_DEBUG <> 0 THEN
3872    	oe_debug_pub.add('resurrect_shipset: ' ||  ' p_success_flag is false ' , 2 );
3873    END IF;
3874   END IF ;
3875 
3876 
3877   l_stmt_num := 20 ;
3878    /* for each element in reduced shipset */
3879    FOR i IN 1..p_shipset.identifier.count
3880    LOOP
3881       /*
3882       ** check whether top ato model exists
3883       */
3884       l_stmt_num := 30 ;
3885 
3886       IF( g_cto_sparse_shipset.exists(p_shipset.identifier(i)) )
3887       THEN
3888 
3889         v_curr_line_id := g_cto_sparse_shipset(p_shipset.identifier(i)).line_id ;
3890 
3891        	IF( get_shipset_source_flag( g_cto_sparse_shipset(v_curr_line_id).ship_set_name )  = TRUE ) THEN
3892 
3893                l_stmt_num := 40 ;
3894                v_new_available_quantity := p_shipset.Available_Quantity(i) ;
3895                v_new_requested_date_quantity := p_shipset.Requested_Date_Quantity(i) ;
3896                v_new_group_ship_date := p_shipset.Group_Ship_Date(i) ;
3897                v_new_ship_date := p_shipset.Ship_Date(i) ;
3898                v_new_group_arrival_date := p_shipset.Group_Arrival_Date(i) ;
3899                /* BUG#2158449 added as per navneet on 03-27-2002 */
3900                v_new_arrival_date := p_shipset.Arrival_Date(i) ;
3901                v_new_delivery_lead_time := p_shipset.delivery_lead_time(i) ;
3902                v_new_source_organization_id := p_shipset.source_organization_id(i) ;
3903                v_new_ship_method := p_shipset.ship_method(i) ;
3904                v_new_end_pegging_id := p_shipset.end_pegging_id(i) ;
3905 
3906                /*
3907                ** register_error
3908                */
3909                l_stmt_num := 50 ;
3910                IF( p_shipset.error_code.exists(i) ) THEN
3911                   IF( p_shipset.error_code(i) is not null) THEN
3912 			IF( get_shipset_source_flag( g_cto_sparse_shipset(v_curr_line_id).ship_set_name )  = TRUE ) THEN
3913                			register_error( p_shipset.ship_set_name.first ,
3914                       			p_shipset.identifier(i) , p_shipset.error_code(i) ,
3915                       			v_action,
3916                       			p_success_flag,
3917                       			x_return_status ) ;
3918         		END IF ;
3919                    END IF;
3920            	END IF;
3921 
3922             	l_stmt_num := 60 ;
3923            	/*
3924            	** propagate information for all the lower level components of ato model
3925            	*/
3926            	FOR j IN g_cto_shipset.first..g_cto_shipset.last
3927            	LOOP
3928 
3929               		l_stmt_num := 70 ;
3930               		IF( g_cto_shipset(j).ato_line_id  = v_curr_line_id ) THEN
3931                   		v_shipset_loc := g_cto_shipset(j).location ;
3932 
3933                   		l_stmt_num := 80 ;
3934                   		/* check whether top level parent or its component */
3935                   		IF( g_cto_shipset(j).line_id <> v_curr_line_id ) THEN
3936 
3937                       		    l_stmt_num := 90 ;
3938                       		    IF( p_success_flag  ) THEN
3939 
3940                           		l_stmt_num := 100 ;
3941                           		g_shipset.Available_Quantity(v_shipset_loc) := v_new_available_quantity ;
3942                           		g_shipset.Requested_Date_Quantity(v_shipset_loc) := v_new_requested_date_quantity ;
3943                           		g_shipset.Group_Ship_Date(v_shipset_loc) := v_new_group_ship_date ;
3944                           		g_shipset.Ship_Date(v_shipset_loc) := v_new_ship_date ;
3945                           		g_shipset.Group_Arrival_Date(v_shipset_loc) := v_new_group_arrival_date ;
3946                           		/* BUG#2158449 added as per navneet on 03-27-2002 */
3947                           		g_shipset.Arrival_Date(v_shipset_loc) :=v_new_arrival_date;
3948                           		g_shipset.delivery_lead_time(v_shipset_loc) := v_new_delivery_lead_time ;
3949                           		g_shipset.source_organization_id (v_shipset_loc) :=v_new_source_organization_id  ;
3950                           		g_shipset.ship_method(v_shipset_loc) :=v_new_ship_method  ;
3951                           		g_shipset.end_pegging_id(v_shipset_loc) :=v_new_end_pegging_id ;
3952 
3953                       		     ELSE
3954 
3955                           		l_stmt_num := 110 ;
3956                           		g_shipset.Available_Quantity(v_shipset_loc) := null ;
3957                           		g_shipset.Requested_Date_Quantity(v_shipset_loc) := null ;
3958                           		g_shipset.Group_Ship_Date(v_shipset_loc) := null ;
3959                           		g_shipset.Ship_Date(v_shipset_loc) := null ;
3960                           		g_shipset.Group_Arrival_Date(v_shipset_loc) := null ;
3961                       		     END IF ;
3962                   		ELSE
3963 
3964                       		     l_stmt_num := 120 ;
3965                       		     /* do not nullify the information in the parent record */
3966                        		     g_shipset.Available_Quantity(v_shipset_loc) := v_new_available_quantity ;
3967                       		     g_shipset.Requested_Date_Quantity(v_shipset_loc) := v_new_requested_date_quantity ;
3968                       		     g_shipset.Group_Ship_Date(v_shipset_loc) := v_new_group_ship_date ;
3969                       		     g_shipset.Ship_Date(v_shipset_loc) := v_new_ship_date ;
3970                       		     g_shipset.Group_Arrival_Date(v_shipset_loc) := v_new_group_arrival_date ;
3971                         	     /* BUG#2158449 added as per navneet on 03-27-2002 */
3972                       		     g_shipset.Arrival_Date(v_shipset_loc) :=v_new_arrival_date  ;
3973                       		     g_shipset.delivery_lead_time(v_shipset_loc) := v_new_delivery_lead_time ;
3974                       		     g_shipset.source_organization_id (v_shipset_loc) :=v_new_source_organization_id  ;
3975                       		     g_shipset.ship_method(v_shipset_loc) :=v_new_ship_method  ;
3976                       		     g_shipset.end_pegging_id(v_shipset_loc) :=v_new_end_pegging_id;
3977 
3978                   		END IF ;
3979 
3980               		END IF ;
3981            	END LOOP ;
3982        	ELSE
3983 
3984            /* this code will be executed for non sourced shipsets  */
3985            /* Changes to support product substitution */
3986 
3987           l_stmt_num := 130 ;
3988           FOR j IN g_shipset.identifier.first..g_shipset.identifier.last
3989           LOOP
3990 
3991              l_stmt_num := 140 ;
3992              IF( g_shipset.identifier(j) = p_shipset.identifier(i) ) THEN
3993 
3994                       l_stmt_num := 150 ;
3995                       g_shipset.Available_Quantity(j) := p_shipset.available_quantity(i) ;
3996                       g_shipset.Requested_Date_Quantity(j) :=  p_shipset.requested_date_quantity(i) ;
3997 
3998                       g_shipset.Group_Ship_Date(j) := p_shipset.group_ship_date(i);
3999                       g_shipset.Ship_Date(j) := p_shipset.ship_date(i);
4000                       g_shipset.Group_Arrival_Date(j) := p_shipset.group_arrival_date(i) ;
4001                       g_shipset.error_code(j) := p_shipset.error_code(i) ;
4002 
4003                       l_stmt_num := 155 ;
4004                       g_shipset.Arrival_Date(j) := p_shipset.arrival_date(i)  ;
4005                       g_shipset.delivery_lead_time(j) := p_shipset.delivery_lead_time(i) ;
4006                       g_shipset.source_organization_id (j) := p_shipset.source_organization_id(i)  ;
4007                       g_shipset.ship_method(j) := p_shipset.ship_method(i)  ;
4008                       g_shipset.end_pegging_id(j) := p_shipset.end_pegging_id(i)  ;
4009                       /* BUG#2250630 product substitution code changes */
4010                       g_shipset.request_item_id (j) := p_shipset.request_item_id(i) ;
4011                       g_shipset.inventory_item_id(j) := p_shipset.inventory_item_id(i) ;
4012                       /* BUG#2250630 product substitution code changes */
4013               end IF ;
4014           END LOOP ;
4015 
4016        END IF ; /* if g_cto_sparse_shipset is a sourced shipset */
4017 
4018        ELSE
4019             /* shipset line does not exist */
4020            /* this code will be executed for standard items part of shipset */
4021            /* Changes to support product substitution */
4022 
4023             IF PG_DEBUG <> 0 THEN
4024             	oe_debug_pub.add('resurrect_shipset: ' ||  '  shipset line does not exist ' || p_shipset.identifier(i) , 5);
4025             END IF;
4026 
4027           l_stmt_num := 130 ;
4028           FOR j IN g_shipset.identifier.first..g_shipset.identifier.last
4029           LOOP
4030 
4031              l_stmt_num := 140 ;
4032              IF( g_shipset.identifier(j) = p_shipset.identifier(i) ) THEN
4033 
4034                       l_stmt_num := 150 ;
4035                       g_shipset.Available_Quantity(j) := p_shipset.available_quantity(i) ;
4036                       g_shipset.Requested_Date_Quantity(j) :=  p_shipset.requested_date_quantity(i) ;
4037                       g_shipset.Group_Ship_Date(j) := p_shipset.group_ship_date(i);
4038                       g_shipset.Ship_Date(j) := p_shipset.ship_date(i);
4039                       g_shipset.Group_Arrival_Date(j) := p_shipset.group_arrival_date(i) ;
4040                       g_shipset.error_code(j) := p_shipset.error_code(i) ;
4041                       g_shipset.Arrival_Date(j) := p_shipset.arrival_date(i)  ;
4042                       g_shipset.delivery_lead_time(j) := p_shipset.delivery_lead_time(i) ;
4043                       g_shipset.source_organization_id (j) := p_shipset.source_organization_id(i)  ;
4044                       g_shipset.ship_method(j) := p_shipset.ship_method(i)  ;
4045                       g_shipset.END_pegging_id(j) := p_shipset.end_pegging_id(i)  ;
4046 
4047                       /* BUG#2250630 product substitution code changes */
4048                       g_shipset.request_item_id (j) := p_shipset.request_item_id(i) ;
4049                       g_shipset.inventory_item_id(j) := p_shipset.inventory_item_id(i) ;
4050               END IF ;
4051           END LOOP ;
4052 
4053        END IF ; /* if element exists in g_cto_sparse_shipset */
4054 
4055    END LOOP ;
4056 
4057    l_stmt_num := 160 ;
4058    p_shipset := g_shipset ;
4059    populate_error( p_shipset , x_return_status ) ;
4060 
4061    l_stmt_num := 170 ;
4062 
4063    IF PG_DEBUG <> 0 THEN
4064   	oe_debug_pub.add('resurrect_shipset: ' ||  'After resurrect shipset ' , 1 );
4065    END IF;
4066 
4067    l_stmt_num := 180 ;
4068    print_shipset( p_shipset ) ;
4069 
4070 EXCEPTION
4071  WHEN others THEN
4072    IF PG_DEBUG <> 0 THEN
4073    	oe_debug_pub.add('resurrect_shipset: ' ||  ' error encountered in resurrect_shipset at line ' || to_char(l_stmt_num ) , 1 );
4074    	oe_debug_pub.add('resurrect_shipset: ' ||  ' error ' || SQLCODE , 1 );
4075    	oe_debug_pub.add('resurrect_shipset: ' ||  ' error ' || SQLERRM , 1 );
4076    END IF;
4077    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4078 END resurrect_shipset;
4079 
4080 
4081 
4082 PROCEDURE populate_configuration_status
4083 is
4084 type top_model_tab_type  is table of number index by binary_integer ;
4085 t_top_model_tab top_model_tab_type ;
4086 
4087 /* populate top model line id location with 0 for no config and 1 for config created */
4088 v_config_exists boolean ;
4089 l_stmt_num   number ;
4090 BEGIN
4091 
4092  IF PG_DEBUG <> 0 THEN
4093  	oe_debug_pub.add('populate_configuration_status: ' || 'Populating Configuration Status ..',4);
4094  END IF;
4095  l_stmt_num :=  1 ;
4096 
4097  IF( g_cto_shipset.count > 0 ) THEN
4098  FOR i IN g_cto_shipset.first..g_cto_shipset.last
4099  LOOP
4100 
4101     l_stmt_num :=  5 ;
4102     IF g_cto_shipset(i).ato_line_id is not null THEN
4103     IF( g_cto_shipset.exists(i) ) THEN
4104 
4105        l_stmt_num :=  10 ;
4106        IF ( t_top_model_tab.exists(g_cto_shipset(i).ato_line_id) ) THEN
4107 
4108             l_stmt_num :=  20 ;
4109             IF( t_top_model_tab(g_cto_shipset(i).ato_line_id) = 0 ) THEN
4110                 g_cto_shipset(i).configuration_exists := 'N' ;
4111             ELSE
4112                 g_cto_shipset(i).configuration_exists := 'Y' ;
4113             END IF ;
4114        ELSE
4115             l_stmt_num :=  30 ;
4116             v_config_exists := CTO_WORKFLOW.config_line_exists(g_cto_shipset(i).ato_line_id) ;
4117 
4118             l_stmt_num :=  40 ;
4119             IF( v_config_exists ) THEN
4120                 IF PG_DEBUG <> 0 THEN
4121                 	oe_debug_pub.add('populate_configuration_status: ' || 'Config item exists for this ato line id :: '||to_char(g_cto_shipset(i).ato_line_id),4);
4122                 END IF;
4123                 g_cto_shipset(i).configuration_exists := 'Y' ;
4124                 t_top_model_tab(g_cto_shipset(i).ato_line_id) := 1 ;
4125             ELSE
4126                 IF PG_DEBUG <> 0 THEN
4127                 	oe_debug_pub.add('populate_configuration_status: ' || 'Config item DOES NOT exist for this ato line id :: '||to_char(g_cto_shipset(i).ato_line_id),4);
4128                 END IF;
4129                 g_cto_shipset(i).configuration_exists := 'N' ;
4130                 t_top_model_tab(g_cto_shipset(i).ato_line_id) := 0 ;
4131             END IF ;
4132        END IF ;
4133     END IF ;
4134    END IF;
4135  END LOOP ;
4136 
4137  END IF ;
4138 
4139 EXCEPTION
4140  WHEN others THEN
4141    IF PG_DEBUG <> 0 THEN
4142    	oe_debug_pub.add('populate_configuration_status: ' ||  ' error encountered in populate_configuration_status at line ' || to_char(l_stmt_num ) , 1 );
4143    	oe_debug_pub.add('populate_configuration_status: ' ||  ' error ' || SQLCODE , 1 );
4144    	oe_debug_pub.add('populate_configuration_status: ' ||  ' error ' || SQLERRM , 1 );
4145    END IF;
4146    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4147 END populate_configuration_status ;
4148 
4149 
4150 
4151 PROCEDURE populate_visible_demand(
4152   p_shipset    in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
4153 , p_success_flag in boolean
4154 , x_return_status out     varchar2
4155 , x_msg_count     out     number
4156 , x_msg_data      out     varchar2
4157 )
4158 is
4159 l_stmt_num   number ;
4160 
4161 BEGIN
4162 
4163  IF PG_DEBUG <> 0 THEN
4164  	oe_debug_pub.add('populate_visible_demand: ' || 'Populating Visible demand Flag value..',4);
4165  END IF;
4166  l_stmt_num :=  1 ;
4167 
4168  IF( g_cto_shipset.count > 0 ) THEN
4169 
4170  FOR i IN g_cto_shipset.first..g_cto_shipset.last
4171  LOOP
4172 
4173  l_stmt_num :=  5 ;
4174  IF (g_cto_shipset.exists(i)) THEN
4175 
4176     l_stmt_num :=  10 ;
4177     IF( g_cto_shipset(i).configuration_exists = 'N' ) THEN
4178 
4179         l_stmt_num :=  20 ;
4180         IF PG_DEBUG <> 0 THEN
4181         	oe_debug_pub.add('populate_visible_demand: ' || 'Location ='||to_char(g_cto_shipset(i).location),5);
4182         END IF;
4183 
4184         IF ( g_cto_shipset(i).buy_model = 'Y' ) THEN
4185 
4186             l_stmt_num :=  30 ;
4187             p_shipset.attribute_05(g_cto_shipset(i).location) := 'N' ;
4188             IF PG_DEBUG <> 0 THEN
4189             	oe_debug_pub.add('populate_visible_demand: ' || 'Visible demand flag set to Y for line_id = '||to_char(g_cto_shipset(i).line_id),4);
4190             END IF;
4191 
4192         ELSE
4193             l_stmt_num :=  40 ;
4194             p_shipset.attribute_05(g_cto_shipset(i).location) := 'Y' ;
4195         END IF ;
4196 
4197     ELSE
4198         /* populate visible demand flag = 'N' if configuration exists */
4199 
4200         l_stmt_num :=  50 ;
4201         p_shipset.attribute_05(g_cto_shipset(i).location) := 'N' ;
4202     END IF ;
4203 
4204  END IF;
4205 
4206  END LOOP ;
4207 
4208  END IF ;
4209 
4210  l_stmt_num :=  70 ;
4211 
4212 EXCEPTION
4213  WHEN others THEN
4214    IF PG_DEBUG <> 0 THEN
4215    	oe_debug_pub.add('populate_visible_demand: ' ||  ' error encountered in Populate_visible_demand at line ' || to_char(l_stmt_num ) , 1 );
4216    	oe_debug_pub.add('populate_visible_demand: ' ||  ' error ' || SQLCODE , 1 );
4217    	oe_debug_pub.add('populate_visible_demand: ' ||  ' error ' || SQLERRM , 1 );
4218    END IF;
4219    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4220 END populate_visible_demand;
4221 
4222 
4223 
4224 PROCEDURE remove_elements_from_atp_rec(
4225  p_atp_rec in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
4226 )
4227 is
4228 BEGIN
4229     p_atp_rec.Row_Id.delete ;
4230     p_atp_rec.Instance_Id.delete ;
4231     p_atp_rec.Inventory_Item_Id.delete ;
4232     p_atp_rec.Inventory_Item_Name.delete ;
4233     p_atp_rec.Source_Organization_Id.delete ;
4234     p_atp_rec.Source_Organization_Code.delete ;
4235     p_atp_rec.Organization_Id.delete ;
4236     p_atp_rec.Identifier.delete ;
4237     p_atp_rec.Demand_Source_Header_Id.delete ;
4238     p_atp_rec.Demand_Source_Delivery.delete ;
4239     p_atp_rec.Demand_Source_Type.delete ;
4240     p_atp_rec.Scenario_Id.delete ;
4241     p_atp_rec.Calling_Module.delete ;
4242     p_atp_rec.Customer_Id.delete ;
4243     p_atp_rec.Customer_Site_Id.delete ;
4244     p_atp_rec.Destination_Time_Zone.delete ;
4245     p_atp_rec.Quantity_Ordered.delete ;
4246     p_atp_rec.Quantity_UOM.delete ;
4247     p_atp_rec.Requested_Ship_Date.delete ;
4248     p_atp_rec.Requested_Arrival_Date.delete ;
4249     p_atp_rec.Earliest_Acceptable_Date.delete ;
4250     p_atp_rec.Latest_Acceptable_Date.delete ;
4251     p_atp_rec.Delivery_Lead_Time.delete ;
4252     p_atp_rec.Freight_Carrier.delete ;
4253     p_atp_rec.Ship_Method.delete ;
4254     p_atp_rec.Demand_Class.delete ;
4255     p_atp_rec.Ship_Set_Name.delete ;
4256     p_atp_rec.Arrival_Set_Name.delete ;
4257     p_atp_rec.Override_Flag.delete ;
4258     p_atp_rec.Action.delete ;
4259     p_atp_rec.Ship_Date.delete ;
4260     p_atp_rec.Available_Quantity.delete ;
4261     p_atp_rec.Requested_Date_Quantity.delete ;
4262     p_atp_rec.Group_Ship_Date.delete ;
4263     p_atp_rec.Group_Arrival_Date.delete ;
4264     p_atp_rec.Vendor_Id.delete ;
4265     p_atp_rec.Vendor_Name.delete ;
4266     p_atp_rec.Vendor_Site_Id.delete ;
4267     p_atp_rec.Vendor_Site_Name.delete ;
4268     p_atp_rec.Insert_Flag.delete ;
4269     p_atp_rec.OE_Flag.delete ;
4270     p_atp_rec.Atp_Lead_Time.delete ;
4271     p_atp_rec.Error_Code.delete ;
4272     p_atp_rec.Message.delete ;
4273     p_atp_rec.End_Pegging_Id.delete ;
4274     p_atp_rec.Order_Number.delete ;
4275     p_atp_rec.Old_Source_Organization_Id.delete ;
4276     p_atp_rec.Old_Demand_Class.delete ;
4277 END remove_elements_from_atp_rec ;
4278 
4279 
4280 
4281 PROCEDURE remove_elements_from_bom_rec(
4282  p_bom_rec in out NOCOPY MRP_ATP_PUB.ATP_BOM_REC_TYP
4283 )
4284 is
4285 BEGIN
4286     p_bom_rec.assembly_identifier.delete ;
4287     p_bom_rec.assembly_item_id.delete ;
4288     p_bom_rec.component_identifier.delete ;
4289     p_bom_rec.component_item_id.delete ;
4290     p_bom_rec.quantity.delete ;
4291     p_bom_rec.fixed_lt.delete ;
4292     p_bom_rec.variable_lt.delete ;
4293     p_bom_rec.pre_process_lt.delete ;
4294 END remove_elements_from_bom_rec ;
4295 
4296 
4297 
4298 PROCEDURE isAutoGeneratedShipset(
4299   p_ship_set_name in varchar2,
4300   x_auto_gen out boolean,
4301   x_process_demand out boolean
4302 )
4303 is
4304 BEGIN
4305 
4306   x_auto_gen := FALSE;
4307   x_process_demand := FALSE;
4308 
4309   IF( g_auto_generated_shipset.count > 0 ) THEN
4310   FOR i IN g_auto_generated_shipset.first..g_auto_generated_shipset.last
4311   LOOP
4312 
4313     IF(p_ship_set_name = g_auto_generated_shipset(i).set_name ) THEN
4314   	IF PG_DEBUG <> 0 THEN
4315   		oe_debug_pub.add('isAutoGeneratedShipset: ' ||  'Shipset '||p_ship_set_name|| 'is an auto-generated shipset  '  ,3 );
4316   	END IF;
4317 	x_auto_gen := TRUE;
4318 	x_process_demand := g_auto_generated_shipset(i).process_demand;
4319 	exit;
4320     END IF ;
4321 
4322   END LOOP ;
4323 
4324   IF PG_DEBUG <> 0 THEN
4325   	oe_debug_pub.add('isAutoGeneratedShipset: ' ||  'Shipset '||p_ship_set_name|| 'is NOT an auto-generated shipset  '  ,3 );
4326   END IF;
4327   END IF ;
4328 
4329 END isAutoGeneratedShipset;
4330 
4331 
4332 
4333 FUNCTION get_shipset_success_index(
4334   p_ship_set_name in varchar2
4335 )
4336 return number
4337 is
4338 BEGIN
4339 
4340    FOR i IN g_shipset_status_tbl.first..g_shipset_status_tbl.last
4341    LOOP
4342       IF( p_ship_set_name = g_shipset_status_tbl(i).ship_set_name ) THEN
4343           return i ;
4344       END IF ;
4345    END LOOP ;
4346 
4347    return 0 ;
4348 END get_shipset_success_index ;
4349 
4350 
4351 /*
4352 ** This procedure registers errors in the g_cto_shipset structure for any components towards their Top level ATO models
4353 ** An error code of -99 is used to indicate OM not to display the lower level components
4354 ** of an Multi-level, Multi-org ATO model.
4355    --  Action_code 100 means Enquiry
4356    --  Action_code 110 means Scheduling
4357    --  Action_code 120 means Rescheduling
4358 */
4359 PROCEDURE register_error(
4360   p_ship_set_name       in varchar2
4361 , p_line_id       in number
4362 , p_error_code    in number
4363 , p_action        in number
4364 , p_status        in boolean
4365 , x_return_status out varchar2
4366 )
4367 is
4368 v_start_loc  number ;
4369 v_ato_line_id number ;
4370 v_pto_line_id number ;
4371 v_atp_group_error_code number := -99 ;
4372 v_sched_group_error_code number := 19 ;
4373 v_mask_error_code number := -100 ;
4374 v_error_code_registered boolean ;
4375 BEGIN
4376 
4377   IF( g_cto_shipset.count > 0 ) THEN
4378    FOR i IN g_cto_shipset.first..g_cto_shipset.last
4379    LOOP
4380 
4381       IF( g_cto_shipset(i).line_id = p_line_id ) THEN
4382 
4383           IF PG_DEBUG <> 0 THEN
4384           	oe_debug_pub.add('register_error: ' ||  ' line id ' || p_line_id || ' found in g_cto_shipset ' , 5 );
4385           END IF;
4386 
4387           v_ato_line_id := g_cto_shipset(i).ato_line_id ;
4388           v_pto_line_id := g_cto_shipset(i).top_model_line_id ;
4389 
4390           /*
4391           ** register error for ato line id
4392           */
4393           FOR j IN g_cto_shipset.first..g_cto_shipset.last
4394           LOOP
4395              IF( g_cto_shipset(j).line_id = v_ato_line_id ) THEN
4396                  g_cto_shipset(j).error_code := p_error_code ;
4397 
4398                  IF PG_DEBUG <> 0 THEN
4399                  	oe_debug_pub.add('register_error: ' ||  'Registered ato error ' || to_char( p_error_code )|| 'for line_id' || g_cto_shipset(j).line_id  , 2 );
4400                  END IF;
4401              ELSIF( g_cto_shipset(j).ato_line_id = v_ato_line_id ) THEN
4402 
4403                     /* Changes made for scheduling errors due to bug 2428252 */
4404 
4405                     IF( nvl(p_action , 100) <> 100 and p_status = false ) THEN
4406                        g_cto_shipset(j).error_code :=  v_sched_group_error_code  ;
4407 
4408                        IF PG_DEBUG <> 0 THEN
4409                        	oe_debug_pub.add('register_error: ' ||  'Registered group error ' || v_sched_group_error_code , 2 );
4410                        END IF;
4411                     ELSE
4412                        g_cto_shipset(j).error_code :=  v_atp_group_error_code  ;
4413 
4414                        IF PG_DEBUG <> 0 THEN
4415                        	oe_debug_pub.add('register_error: ' ||  'Registered group error ' || v_atp_group_error_code , 2 );
4416                        END IF;
4417 
4418                     END IF;
4419              END IF;
4420           END LOOP ;
4421 
4422           v_error_code_registered := true ;
4423           exit ;
4424 
4425       END IF ;
4426 
4427    END LOOP ;
4428    ELSE
4429 
4430      IF PG_DEBUG <> 0 THEN
4431      	oe_debug_pub.add('register_error: ' ||  'Register_error did not record anything ' , 2 );
4432      END IF;
4433    END IF ;
4434 
4435 END register_error ;
4436 
4437 
4438 /*
4439 ** This procedure actually populates the errors registered in the g_cto_shipset structure to the actual shipset.
4440 */
4441 PROCEDURE populate_error(
4442   p_shipset   in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP
4443 , x_return_status out varchar2
4444 )
4445 is
4446 v_start_loc  number ;
4447 v_ato_line_id number ;
4448 BEGIN
4449 
4450    IF( g_cto_shipset.count > 0 ) THEN
4451    FOR i IN g_cto_shipset.first..g_cto_shipset.last
4452    LOOP
4453 
4454       IF( g_cto_shipset(i).error_code is not null ) THEN
4455 
4456           IF PG_DEBUG <> 0 THEN
4457           	oe_debug_pub.add('populate_error: ' ||  'Populating error code ' || g_cto_shipset(i).error_code || ' at '  || i || ' for line id ' || g_cto_shipset(i).line_id || ' at location '  || g_cto_shipset(i).location , 2 );
4458           END IF;
4459 
4463    END LOOP ;
4460           p_shipset.error_code(g_cto_shipset(i).location) := g_cto_shipset(i).error_code ;
4461       END IF;
4462 
4464    ELSE
4465     IF PG_DEBUG <> 0 THEN
4466     	oe_debug_pub.add('populate_error: ' ||  'No errors to populate as g_cto_shipset count is ' || g_cto_shipset.count, 1 );
4467     END IF;
4468 
4469    END IF ;
4470 
4471 
4472 END populate_error;
4473 
4474 
4475 PROCEDURE save_shipset(
4476   p_shipset in MRP_ATP_PUB.ATP_REC_TYP
4477 , x_return_status out     varchar2
4478 )
4479 is
4480 BEGIN
4481    FOR i IN p_shipset.identifier.first..p_shipset.identifier.last
4482    LOOP
4483       IF( p_shipset.identifier.exists(i) ) THEN
4484         IF PG_DEBUG <> 0 THEN
4485         	oe_debug_pub.add('save_shipset: ' ||  ' +++++ident ' || p_shipset.identifier(i) || ' is part of reduced shipset ' , 2 );
4486         END IF;
4487         MRP_ATP_PVT.assign_atp_input_rec(p_shipset,
4488                                          i,
4489                                          g_final_shipset,
4490                                          x_return_status );
4491        END IF;
4492    END LOOP ;
4493 END save_shipset ;
4494 
4495 
4496 PROCEDURE show_contents( p_shipset_tracker CTO_SHIPSET_TBL_TYPE )
4497 is
4498 BEGIN
4499 
4500    IF( p_shipset_tracker.count > 0 ) THEN
4501    oe_debug_pub.add('show_contents: ' ||'Show contents of shipset:', 2);
4502    FOR i IN p_shipset_tracker.first..p_shipset_tracker.last
4503    LOOP
4504       IF( p_shipset_tracker.exists(i) ) THEN
4505       IF PG_DEBUG <> 0 THEN
4506       	oe_debug_pub.add('**i:'||to_char(i), 2);
4507 	oe_debug_pub.add('+++header_id:: ' || p_shipset_tracker(i).header_id, 2);
4508         oe_debug_pub.add('+++line_id::  ' || p_shipset_tracker(i).line_id , 2);
4509         oe_debug_pub.add('+++plan_level::  ' || p_shipset_tracker(i).plan_level, 2);
4510         oe_debug_pub.add('+++ato_line_id::  ' || p_shipset_tracker(i).ato_line_id, 2);
4511         oe_debug_pub.add('+++parent_ato_line_id::  ' || p_shipset_tracker(i).parent_ato_line_id , 2);
4512         oe_debug_pub.add('+++top_model_line_id::  ' || p_shipset_tracker(i).top_model_line_id , 2);
4513         oe_debug_pub.add('+++link_to_line_id::  ' || p_shipset_tracker(i).link_to_line_id  , 2);
4514         oe_debug_pub.add('+++srcorg::  ' || p_shipset_tracker(i).sourcing_org , 2);
4515         oe_debug_pub.add('+++wip_supply_type::  ' || p_shipset_tracker(i).wip_supply_type , 2);
4516         oe_debug_pub.add('+++ordered_qty::  ' || p_shipset_tracker(i).ordered_quantity  , 2);
4517         oe_debug_pub.add('+++ss_name::  ' || p_shipset_tracker(i).ship_set_name  , 2);
4518         oe_debug_pub.add('+++Buy Flag:: '||p_shipset_tracker(i).buy_model  , 2);
4519         oe_debug_pub.add('+++location::  '||p_shipset_tracker(i).location , 2);
4520         oe_debug_pub.add('+++atp_flag::  '||p_shipset_tracker(i).atp_flag , 2);
4521         oe_debug_pub.add('+++atp_components_flag::  '||p_shipset_tracker(i).atp_components_flag , 2);
4522         oe_debug_pub.add('+++stored_atp_flag::  '||p_shipset_tracker(i).stored_atp_flag , 2);
4523       END IF;
4524       IF( p_shipset_tracker(i).auto_generated ) THEN
4525                     IF PG_DEBUG <> 0 THEN
4526                     	oe_debug_pub.add('+++auto_generated::TRUE' , 1 );
4527                     END IF;
4528                 ELSE
4529                     IF PG_DEBUG <> 0 THEN
4530                     	oe_debug_pub.add('+++auto_generated::FALSE' , 1 );
4531                     END IF;
4532                 END IF ;
4533 		IF( p_shipset_tracker(i).process_demand ) THEN
4534                     IF PG_DEBUG <> 0 THEN
4535                     	oe_debug_pub.add('+++process_demand::TRUE' , 1 );
4536                     END IF;
4537                 ELSE
4538                     IF PG_DEBUG <> 0 THEN
4539                     	oe_debug_pub.add('+++process_demand::FALSE' , 1 );
4540                     END IF;
4541                 END IF ;
4542 		IF( p_shipset_tracker(i).sourced_components ) THEN
4543                     IF PG_DEBUG <> 0 THEN
4544                     	oe_debug_pub.add('+++sourced_components::TRUE' , 1 );
4545                     END IF;
4546                 ELSE
4547                     IF PG_DEBUG <> 0 THEN
4548                     	oe_debug_pub.add('+++sourced_components::FALSE' , 1 );
4549                     END IF;
4550                 END IF ;
4551       END IF ;
4552    END LOOP ;
4553    ELSE
4554     	IF PG_DEBUG <> 0 THEN
4555     		oe_debug_pub.add ('show_contents: ' ||  ' shipset tracker contents ' || p_shipset_tracker.count , 2 );
4556     	END IF;
4557    END IF ;
4558 
4559    IF PG_DEBUG <> 0 THEN
4560    	oe_debug_pub.add('show_contents: ' ||  ' Show contents of g_shipset_status_tbl ', 2 );
4561    END IF;
4562    IF( g_shipset_status_tbl.count > 0 ) THEN
4563    for i in g_shipset_status_tbl.first..g_shipset_status_tbl.last
4564    LOOP
4565       IF PG_DEBUG <> 0 THEN
4566       	oe_debug_pub.add('+++i:: '||to_char(i) ||  'ss_name ' || g_shipset_status_tbl(i).ship_set_name , 2 );
4567       END IF;
4568    END LOOP ;
4569    END IF ;
4570 
4571 END show_contents;
4572 
4573 
4574 PROCEDURE default_ship_set_name( p_shipset in out NOCOPY MRP_ATP_PUB.ATP_REC_TYP )
4575 is
4576 BEGIN
4577     IF( p_shipset.identIFier.count > 0 ) THEN
4578      FOR i IN p_shipset.identifier.first..p_shipset.identifier.last
4579      LOOP
4580           IF( p_shipset.ship_set_name(i) is null ) THEN
4581               p_shipset.ship_set_name(i) := 'CTODEFAULT' ;
4582           END IF ;
4583      END LOOP ;
4584     ELSE
4585    IF PG_DEBUG <> 0 THEN
4586    	oe_debug_pub.add('default_ship_set_name: ' ||  ' identIFier column of shipset is of count ' || p_shipset.identifier.count , 4);
4587    END IF;
4588 
4589   END IF ;
4590 
4591 END default_ship_set_name ;
4592 
4593 
4594 PROCEDURE print_shipset(
4595    p_shipset IN MRP_ATP_PUB.ATP_REC_TYP
4596  )
4597  is
4598  BEGIN
4599 
4600     IF PG_DEBUG <> 0 THEN
4601        	oe_debug_pub.add('***print_shipset: ' ||  'Going to print shipset:: ' , 2);
4602 
4603     END IF;
4604 
4605     IF( p_shipset.ship_set_name.count > 0 )
4606     THEN
4607 
4608        FOR j IN p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
4609        LOOP
4610 
4611           IF( p_shipset.ship_set_name.exists(j) ) THEN
4612               IF PG_DEBUG <> 0 THEN
4613               	oe_debug_pub.add('**Shipset Name ' || p_shipset.ship_set_name(j), 2 );
4614               END IF;
4615           END IF ;
4616 
4617           IF( p_shipset.inventory_item_id.exists(j) ) THEN
4618               IF PG_DEBUG <> 0 THEN
4619               	oe_debug_pub.add('+++Inventory_Item_Id:: ' || p_shipset.inventory_item_id(j) , 2 );
4620                 oe_debug_pub.add('+++Source_Organization_Id:: ' || p_shipset.source_organization_id(j) , 2 );
4621                 oe_debug_pub.add('+++Identifier:: ' || p_shipset.identifier(j), 2 );
4622               END IF;
4623               IF( p_shipset.error_code.count > 0 ) THEN
4624                   IF( p_shipset.error_code.exists(j) ) THEN
4625                       IF PG_DEBUG <> 0 THEN
4626                       	oe_debug_pub.add('+++Error Code:: ' || p_shipset.error_code(j) , 2 );
4627                       END IF;
4628                   END IF ;
4629               END IF ;
4630               IF( p_shipset.group_ship_date.count > 0 ) THEN
4631                   IF( p_shipset.group_ship_date.exists(j) ) THEN
4632                       IF PG_DEBUG <> 0 THEN
4633                       	oe_debug_pub.add('+++Group Ship Date:: ' || p_shipset.group_ship_date(j)  , 2 );
4634                       END IF;
4635                   END IF ;
4636               END IF ;
4637               IF( p_shipset.ship_date.count > 0 ) THEN
4638                   IF( p_shipset.ship_date.exists(j) ) THEN
4639                       IF PG_DEBUG <> 0 THEN
4640                       	oe_debug_pub.add('+++Ship Date:: ' || p_shipset.ship_date(j)  , 2 );
4641                       END IF;
4642                   END IF ;
4643               END IF ;
4644               IF( p_shipset.group_arrival_date.count > 0 ) THEN
4645                   IF( p_shipset.group_arrival_date.exists(j) ) THEN
4646                       IF PG_DEBUG <> 0 THEN
4647                       	oe_debug_pub.add('+++Group Arrival Date:: ' || p_shipset.group_arrival_date(j)  , 2 );
4648                       END IF;
4649                   END IF ;
4650               END IF ;
4651               IF( p_shipset.attribute_05.count > 0 ) THEN
4652                   IF( p_shipset.attribute_05.exists(j) ) THEN
4653                       IF PG_DEBUG <> 0 THEN
4654                       	oe_debug_pub.add('+++Attribute_05:: ' || p_shipset.attribute_05(j)  , 2 );
4655                       END IF;
4656                   END IF ;
4657               END IF ;
4658 	      IF( p_shipset.attribute_06.count > 0 ) THEN
4659                   IF( p_shipset.attribute_06.exists(j) ) THEN
4660                       IF PG_DEBUG <> 0 THEN
4661                       	oe_debug_pub.add('+++Attribute_06:: ' || p_shipset.attribute_06(j)  , 2 );
4662                       END IF;
4663                   END IF ;
4664               END IF ;
4665               IF( p_shipset.ato_delete_flag.count > 0 ) then
4666                   if( p_shipset.ato_delete_flag.exists(j) ) then
4667 			IF PG_DEBUG <> 0 THEN
4668                       	  oe_debug_pub.add( '+++Ato delete flag:: ' || p_shipset.ato_delete_flag(j)  , 2 ) ;
4669 			END IF;
4670                   END IF;
4671               END IF;
4672               IF( p_shipset.action.count > 0 ) then
4673                   if( p_shipset.action.exists(j) ) then
4674 			IF PG_DEBUG <> 0 THEN
4675                       	  oe_debug_pub.add( '+++Action:: ' || p_shipset.action(j)  , 2 ) ;
4676 			END IF;
4677                   END IF;
4678               END IF;
4679           END IF ;
4680       END LOOP ;
4681 
4682    ELSE
4683        IF PG_DEBUG <> 0 THEN
4684        	oe_debug_pub.add('print_shipset: ' ||  'Ship set name table of shipset record is ' || p_shipset.ship_set_name.count , 2 );
4685        END IF;
4686    END IF ;
4687 
4688    IF PG_DEBUG <> 0 THEN
4689        	oe_debug_pub.add('***print_shipset: ' ||  'Done printing shipset ' , 2 );
4690    END IF;
4691 
4692 END print_shipset ;
4693 
4694 
4695 PROCEDURE print_shipset_capacity( p_shipset IN MRP_ATP_PUB.ATP_REC_TYP )
4696 is
4697 BEGIN
4698 
4699     IF PG_DEBUG <> 0 THEN
4700     	oe_debug_pub.add('***print_shipset_capacity: ' ||  'Going to print shipset capacity ' , 5 );
4701     END IF;
4702 
4703     IF( p_shipset.ship_set_name.count > 0 ) THEN
4704         IF PG_DEBUG <> 0 THEN
4705         	oe_debug_pub.add('print_shipset_capacity: ' || ' record count   ...'||to_char(p_shipset.ship_set_name.count),5);
4706 
4707                	oe_debug_pub.add('print_shipset_capacity: ' ||  ' Row_Id ' || p_shipset.row_id.count  ||
4708                       ' Instance_Id ' || p_shipset.instance_id.count ||
4709                       ' Inventory_Item_Id ' || p_shipset.inventory_item_id.count ||
4710                       ' Inventory_Item_Name ' || p_shipset.inventory_item_name.count ||
4711                       ' Source_Organization_Id ' || p_shipset.source_organization_id.count ||
4712                       ' Source_Organization_Code ' || p_shipset.source_organization_code.count , 5);
4713 
4714                 	oe_debug_pub.add('print_shipset_capacity: ' ||  ' Organization_Id ' || p_shipset.organization_id.count ||
4715                       ' Identifier ' || p_shipset.identifier.count ||
4716                       ' Demand_Source_Header_Id ' || p_shipset.demand_source_header_id.count ||
4717                       ' Demand_Source_Delivery ' || p_shipset.demand_source_delivery.count  ||
4718                       ' Demand_Source_Type ' || p_shipset.demand_source_type.count ||
4719                       ' Scenario_Id ' || p_shipset.scenario_id.count , 5 );
4720 
4721                	oe_debug_pub.add('print_shipset_capacity: ' ||   ' Calling_Module ' || p_shipset.calling_module.count ||
4722                       ' Customer_Id ' || p_shipset.customer_id.count ||
4723                       ' Customer_Site_Id ' || p_shipset.customer_site_id.count ||
4724                       ' Destination_Time_Zone ' || p_shipset.destination_time_zone.count ||
4725                       ' Quantity_Ordered ' || p_shipset.quantity_ordered.count ||
4726                       ' Quantity_UOM ' || p_shipset.quantity_uom.count , 5 );
4727 
4728                	oe_debug_pub.add('print_shipset_capacity: ' ||   ' Requested_Ship_Date ' || p_shipset.requested_ship_date.count ||
4729                       ' Requested_Arrival_Date ' || p_shipset.requested_arrival_date.count ||
4730                       ' Earliest_Acceptable_Date ' || p_shipset.earliest_acceptable_date.count ||
4731                       ' Latest_Acceptable_Date ' || p_shipset.latest_acceptable_date.count ||
4732                       ' Delivery_Lead_Time ' || p_shipset.delivery_lead_time.count ||
4733                       ' Freight_Carrier ' || p_shipset.freight_carrier.count  , 5 );
4734 
4735                	oe_debug_pub.add('print_shipset_capacity: ' ||   ' Ship_Method ' || p_shipset.ship_method.count ||
4736                       ' Demand_Class ' || p_shipset.demand_class.count ||
4737                       ' Ship_Set_Name ' || p_shipset.ship_set_name.count ||
4738                       ' Arrival_Set_Name ' || p_shipset.arrival_set_name.count ||
4739                       ' Override_Flag ' || p_shipset.override_flag.count ||
4740                       ' Action ' || p_shipset.action.count  , 5 );
4741 
4742                	oe_debug_pub.add('print_shipset_capacity: ' ||   ' Ship_Date ' || p_shipset.ship_date.count ||
4743                       ' Available_Quantity ' || p_shipset.available_quantity.count ||
4744                       ' Requested_Date_Quantity ' || p_shipset.requested_date_quantity.count ||
4745                       ' Group_Ship_Date ' || p_shipset.group_ship_date.count ||
4746                       ' Group_Arrival_Date ' || p_shipset.group_arrival_date.count ||
4747                       ' Vendor_Id ' || p_shipset.vendor_id.count  , 5 );
4748 
4749                	oe_debug_pub.add('print_shipset_capacity: ' ||   ' Vendor_Name ' || p_shipset.vendor_name.count ||
4750                       ' Vendor_Site_Id ' || p_shipset.vendor_site_id.count ||
4751                       ' Vendor_Site_Name ' || p_shipset.vendor_site_name.count ||
4752                       ' Insert_Flag ' || p_shipset.insert_flag.count ||
4753                       ' OE_Flag ' || p_shipset.oe_flag.count ||
4754                       ' Atp_Lead_Time ' || p_shipset.atp_lead_time.count  , 5 );
4755                	oe_debug_pub.add('print_shipset_capacity: ' ||   ' Error_Code ' || p_shipset.error_code.count ||
4756                       ' Message ' || p_shipset.message.count ||
4757                       ' End_Pegging_Id ' || p_shipset.end_pegging_id.count ||
4758                       ' Order_Number ' || p_shipset.order_number.count ||
4759                       ' Old_Source_Organization_Id ' || p_shipset.old_source_organization_id.count ||
4760 		      ' ATO DELETE FLAG ' || p_shipset.ato_delete_flag.count ||
4761                       ' Old_Demand_Class ' || p_shipset.old_demand_class.count  , 5  );
4762                END IF;
4763 
4764      ELSE
4765         IF PG_DEBUG <> 0 THEN
4766         	oe_debug_pub.add('print_shipset_capacity: ' ||  ' ship set name table of shipset record is ' || p_shipset.ship_set_name.count  , 5 );
4767         END IF;
4768      END IF ;
4769 
4770      IF PG_DEBUG <> 0 THEN
4771     	oe_debug_pub.add('***print_shipset_capacity: ' ||  ' done printing shipset capacity ' , 5 );
4772      END IF;
4773 
4774 END print_shipset_capacity ;
4775 
4776 
4777 
4778 
4779 /*-------------------------------------------------------------------
4780   Name        : Create_ATP_BOM
4781   Description : This procedure creates a temporary BOM structure for all
4782 		ATO models in an ATP request ship-set
4783   Requires    : Global memory structure G_CTO_SPARSE_SHIPSET
4784   Returns     :
4785 -----------------------------------------------------------------------*/
4786 PROCEDURE Create_Atp_Bom (
4787 	p_atp_bom	in out	NOCOPY MRP_ATP_PUB.atp_bom_rec_typ,
4788         x_Return_Status	out    	varchar2 ,
4789         x_Msg_Data    	out    	varchar2 ,
4790         x_Msg_Count   	out    	number )
4791 IS
4792 
4793 	l_index		NUMBER;
4794 	l_parent_id	NUMBER;
4795 	l_link_id	NUMBER;
4796 	l_fixed_lt	NUMBER;
4797 	l_variable_lt	NUMBER;
4798 	l_preproc_lt	NUMBER;
4799 	l_eff_date	DATE;
4800 	l_disable_date	DATE;
4801 	l_atp_flag	VARCHAR2(1);
4802 	l_atp_comps_flag	VARCHAR2(1);
4803 	l_buy_flag	VARCHAR2(1);
4804 	i		NUMBER := 0;
4805 	lStmtNum	NUMBER;
4806 
4807         l_bom_item_type mtl_system_items.bom_item_type%type ;
4808 
4809 BEGIN
4810 	IF PG_DEBUG <> 0 THEN
4811 		oe_debug_pub.add('create_atp_bom: ' || 'Begin BOM creation', 2);
4812 
4813 		oe_debug_pub.add('create_atp_bom: ' || 'G_Cto_Sparse_Shipset.COUNT :: '||to_char(G_Cto_Sparse_Shipset.COUNT), 2);
4814 	END IF;
4815 	lStmtNum := 10;
4816 	x_return_status := FND_API.G_RET_STS_SUCCESS;
4817 
4818 	IF PG_DEBUG <> 0 THEN
4819 		oe_debug_pub.add('create_atp_bom: ' || 'Getting eff and dis date for optional comps', 4);
4820 	END IF;
4821 	lStmtNum := 20;
4822 	--
4823 	-- Optional comps are made effective from today to calendar_end_date
4824 	--
4825 
4826         -- Modified by Renga Kannan on 10/30/02
4827         -- The blind query on bom_calendar is modified with proper join
4828         -- The global variable used here G_OE_VALIDATION_ORG is set in the
4829         -- populate_om_shipset and populate_cz_shipset part of code
4830         -- We will get the calender dates based on the calendar code parameter
4831         -- set in the Oe validation org.
4832 
4833 	select min(calendar_date), max(calendar_date)
4834 	into l_eff_date, l_disable_date
4835 	from bom_calendar_dates cal,
4836              mtl_parameters mp
4837         where mp.organization_id = CTO_ATP_INTERFACE_PK.G_OE_VALIDATION_ORG
4838         and   mp.calendar_code   = cal.calendar_code
4839         and   mp.calendar_exception_set_id = cal.exception_set_id;
4840 
4841 
4842 	IF PG_DEBUG <> 0 THEN
4843 		oe_debug_pub.add('create_atp_bom: ' || 'l_eff_date :: '||to_char(l_eff_date), 4);
4844 
4845 		oe_debug_pub.add('create_atp_bom: ' || 'l_disable_date :: '||to_char(l_disable_date), 4);
4846 	END IF;
4847 
4848 	IF G_Cto_Sparse_Shipset.COUNT > 0 THEN
4849 		--
4850 		-- LOOP through all items in g_cto_sparse_shipset
4851 		-- Populate each item and its parent
4852 		--
4853 		l_index := G_Cto_Sparse_Shipset.FIRST;
4854 		LOOP
4855 		--
4856 		-- Insert all items into BOM structure except top level ATO models
4857 		--
4858 		IF PG_DEBUG <> 0 THEN
4859 			oe_debug_pub.add('create_atp_bom: ' || 'Processing line_id '||to_char(l_index), 2);
4860 		END IF;
4861 		lStmtNum := 30;
4862 		IF (g_cto_sparse_shipset.EXISTS(l_index)) THEN
4863 		 IF (g_cto_sparse_shipset(l_index).mlmo_flag = 'Y') THEN	--2723674
4864 		  l_parent_id := g_cto_sparse_shipset(l_index).Parent_Ato_Line_Id;
4865 		  l_link_id := g_cto_sparse_shipset(l_index).Link_To_Line_Id;
4866 		  l_buy_flag := g_cto_sparse_shipset(l_index).Buy_Model;
4867 		  IF PG_DEBUG <> 0 THEN
4868 		  	oe_debug_pub.add('**create_atp_bom: ' || 'l_parent_id is '||to_char(l_parent_id), 4);
4869 
4870 		  	oe_debug_pub.add('**create_atp_bom: ' || 'l_link_id is '||to_char(l_link_id), 4);
4871 
4872 		  	oe_debug_pub.add('**create_atp_bom: ' || 'l_buy_flag is '||l_buy_flag, 4);
4873 		  END IF;
4874 
4875 		  /* 'Make' components and top 'Buy' model */
4876 		  IF ((g_cto_sparse_shipset(l_index).Ato_Line_Id <> g_cto_sparse_shipset(l_index).Line_Id) AND (nvl(l_buy_flag, 'N') <> 'Y')) THEN
4877 
4878 			IF PG_DEBUG <> 0 THEN
4879 				oe_debug_pub.add('**create_atp_bom: ' || 'Not top ato model and not buy item, so insert into BOM', 2);
4880 			END IF;
4881 			i := p_atp_bom.assembly_identifier.count+1;
4882 			lStmtNum := 40;
4883 			IF PG_DEBUG <> 0 THEN
4884 				oe_debug_pub.add('**create_atp_bom: ' || 'Insert record # '||to_char(i), 2);
4885 			END IF;
4886 			Extend_Atp_Bom(p_atp_bom,
4887 					x_Return_Status,
4888 					x_Msg_Count,
4889 					x_Msg_Data);
4890 			IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4891 				IF PG_DEBUG <> 0 THEN
4892 					oe_debug_pub.add('**create_atp_bom: ' || 'Unexp error in Extend_ATP_BOM::'||sqlerrm,1);
4893 				END IF;
4894 				raise FND_API.G_EXC_UNEXPECTED_ERROR;
4895 			END IF;
4896 			IF PG_DEBUG <> 0 THEN
4897 				oe_debug_pub.add('**create_atp_bom: ' || 'After extend_atp', 5);
4898 			END IF;
4899 
4900 			lStmtNum := 50;
4901 			/*Populate this item's information*/
4902 			p_atp_bom.Component_Item_Id(i) := g_cto_sparse_shipset(l_index).Inventory_Item_Id;
4903 			p_atp_bom.Component_Identifier(i) := g_cto_sparse_shipset(l_index).Line_Id;
4904 			p_atp_bom.Quantity(i) := g_cto_sparse_shipset(l_index).Ordered_Quantity;
4905 			p_atp_bom.SMC_Flag(i) := 'N';
4906 
4907                         /* Sushant made changes for bug 2396739 */
4908 			p_atp_bom.source_organization_id(i) := g_cto_sparse_shipset(l_parent_id).sourcing_org;
4909 
4910 			lStmtNum := 60;
4911 			/*Populate parent model's information*/
4912 			p_atp_bom.Assembly_Item_Id(i) := g_cto_sparse_shipset(l_parent_id).Inventory_Item_Id;
4913 			p_atp_bom.Assembly_Identifier(i) := g_cto_sparse_shipset(l_parent_id).Line_Id;
4914 
4915 			lStmtNum := 70;
4916 			/*Populate the parent model's lead time on the item.
4917 			We will pass fixed, variable and pre-processing lead
4918 			times in the BOM and ATP will do the calculations*/
4919 			select nvl(fixed_lead_time, 0), nvl(variable_lead_time, 0), nvl(preprocessing_lead_time, 0)
4920 			into l_fixed_lt, l_variable_lt, l_preproc_lt
4921 			from mtl_system_items
4922 			where inventory_item_id = g_cto_sparse_shipset(l_parent_id).Inventory_Item_Id
4923 			and organization_id = g_cto_sparse_shipset(l_parent_id).Sourcing_Org;
4924 			IF PG_DEBUG <> 0 THEN
4925 				oe_debug_pub.add('**create_atp_bom: ' || 'After getting LT', 5);
4926 			END IF;
4927 			p_atp_bom.Fixed_LT(i) := l_fixed_lt;
4928 			p_atp_bom.Variable_LT(i) := l_variable_lt;
4929 			p_atp_bom.Pre_Process_LT(i) := l_preproc_lt;
4930 
4931 			lStmtNum := 75;
4932 			/*Populate atp_flag for each component and send to ATP (bug 2462661)*/
4933 			select atp_flag, atp_components_flag, bom_item_type
4934 			into l_atp_flag, l_atp_comps_flag, l_bom_item_type
4935 			from mtl_system_items
4936 			where inventory_item_id = g_cto_sparse_shipset(l_index).Inventory_Item_Id
4937 			and organization_id = g_cto_sparse_shipset(l_index).Sourcing_Org;
4938 			IF PG_DEBUG <> 0 THEN
4939 				oe_debug_pub.add('**create_atp_bom: ' || 'After getting atp flags', 5);
4940 			END IF;
4941                         /* Sushant added this on 01-14-2003 for atp issues of option classes */
4942 
4943 			IF (l_atp_flag <> 'N' OR l_atp_comps_flag <> 'N' ) THEN
4944 				p_atp_bom.atp_flag(i) := 'Y';
4945 			ELSE
4946 				p_atp_bom.atp_flag(i) := 'N';
4947 			END IF;
4948 
4949 			lStmtNum := 80;
4950 			/*Populate this item's effectivity and disable dates
4951 			These dates will be used by ATP engine to check the
4952 			effectivity of the item. For optional components, we
4953 			pass the calendar_start_date and calendar_end_date
4954 			(always effective within planning horizon)*/
4955 
4956 			p_atp_bom.Effective_Date(i) := l_eff_date;
4957 			p_atp_bom.Disable_Date(i) := l_disable_date;
4958 
4959 			/*Populate the ATP flag on the BOM for this item
4960 			 Here, the same item can be included more than once
4961 			 on the BOM (with a different op. seq.). Since there
4962 			 is no way of selecting the correct component, we
4963 			 are joining with rownum = 1*/
4964 
4965 			lStmtNum := 85;
4966 			select bic.check_atp
4967                              , bic.wip_supply_type
4968 			into p_atp_bom.atp_check(i)
4969                            , p_atp_bom.wip_supply_type(i)
4970 			from bom_bill_of_materials bbom,
4971 				bom_inventory_components bic
4972 			where bbom.assembly_item_id = g_cto_sparse_shipset(l_link_id).Inventory_Item_Id
4973 			and bbom.organization_id = g_cto_sparse_shipset(l_link_id).Sourcing_Org
4974 			and bbom.alternate_bom_designator is NULL
4975 			and bbom.common_bill_sequence_id = bic.bill_sequence_id
4976 			and bic.component_item_id = g_cto_sparse_shipset(l_index).Inventory_Item_Id
4977 			and rownum = 1;
4978 
4979 		  /* Top ato model is buy model, create dummy BOM */
4980 		  ELSIF ((g_cto_sparse_shipset(l_index).Ato_Line_Id = g_cto_sparse_shipset(l_index).Line_Id) AND (nvl(l_buy_flag, 'N') = 'B')) THEN
4981 
4982 			IF PG_DEBUG <> 0 THEN
4983 				oe_debug_pub.add('**create_atp_bom: ' || 'Top ato model is buy model, so insert dummy row in BOM', 2);
4984 			END IF;
4985 			i := p_atp_bom.assembly_identifier.count+1;
4986 			lStmtNum := 86;
4987 			IF PG_DEBUG <> 0 THEN
4988 				oe_debug_pub.add('**create_atp_bom: ' || 'Insert record# '||to_char(i), 2);
4989 			END IF;
4990 			Extend_Atp_Bom(p_atp_bom,
4991 					x_Return_Status,
4992 					x_Msg_Count,
4993 					x_Msg_Data);
4994 			IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4995 				IF PG_DEBUG <> 0 THEN
4996 					oe_debug_pub.add('**create_atp_bom: ' || 'Unexp error in Extend_ATP_BOM::'||sqlerrm,1);
4997 				END IF;
4998 				raise FND_API.G_EXC_UNEXPECTED_ERROR;
4999 			END IF;
5000 			IF PG_DEBUG <> 0 THEN
5001 				oe_debug_pub.add('**create_atp_bom: ' || 'After extend_atp', 5);
5002 			END IF;
5003 
5004 			lStmtNum := 87;
5005 			/*Populate dummy information. ATP will not use this
5006 			information. It will only use it to identify that
5007 			there is atleast one row in the BOM. Populating
5008 			component information for the top buy model, not
5009 			populating parent or any other information*/
5010 
5011                         p_atp_bom.Assembly_Identifier(i) := g_cto_sparse_shipset(l_index).Line_Id;
5012                         p_atp_bom.Assembly_Item_Id(i)    := g_cto_sparse_shipset(l_index).Inventory_Item_Id;
5013 			p_atp_bom.Component_Item_Id(i) := g_cto_sparse_shipset(l_index).Inventory_Item_Id;
5014 			p_atp_bom.Component_Identifier(i) := g_cto_sparse_shipset(l_index).Line_Id;
5015 			p_atp_bom.Quantity(i) := g_cto_sparse_shipset(l_index).Ordered_Quantity;
5016 		  END IF; /* Top level ato model*/
5017 
5018   		  lStmtNum := 90;
5019 		  /*If this item is a 'Make' model or OC, get
5020 		  its sourcing information and populate
5021 		  mandatory components from that org*/
5022 		  IF ((g_cto_sparse_shipset(l_index).Bom_Item_Type IN (1,2)) AND (nvl(l_buy_flag, 'N') = 'N')) THEN
5023 			IF PG_DEBUG <> 0 THEN
5024 				oe_debug_pub.add('**create_atp_bom: ' || 'Item is Make model or OC, get mand comps', 2);
5025 			END IF;
5026 			lStmtNum := 100;
5027 			Populate_Mandatory_Components(p_atp_bom,
5028 					l_index,
5029 					x_Return_Status,
5030 					x_Msg_Count,
5031 					x_Msg_Data);
5032 			IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5033 				IF PG_DEBUG <> 0 THEN
5034 					oe_debug_pub.add('**create_atp_bom: ' || 'Unexp error in Populate_Mandatory_Components::'||sqlerrm,1);
5035 				END IF;
5036 				raise FND_API.G_EXC_UNEXPECTED_ERROR;
5037 			ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
5038 				IF PG_DEBUG <> 0 THEN
5039 					oe_debug_pub.add('**create_atp_bom: ' || 'Exp error in Populate_Mandatory_Components::'||sqlerrm,1);
5040 				END IF;
5041 				raise FND_API.G_EXC_ERROR;
5042 			END IF;
5043 			IF PG_DEBUG <> 0 THEN
5044 				oe_debug_pub.add('**create_atp_bom: ' || 'After Populate_Mandatory_Components', 5);
5045 			END IF;
5046 	 	  END IF; /* 'Make' model or OC */
5047 		 END IF;	--2723674
5048 		  EXIT WHEN l_index = G_Cto_Sparse_Shipset.LAST;
5049 		  lStmtNum := 110;
5050 		END IF;
5051 		l_index := G_Cto_Sparse_Shipset.NEXT(l_index);
5052 		END LOOP; /* loop for all items in ship-set */
5053 		--
5054 		-- writing BOM to debug file
5055 		--
5056 		IF PG_DEBUG <> 0 THEN
5057 			oe_debug_pub.add('create_atp_bom: ' || 'BOM STRUCTURE::', 2);
5058 		END IF;
5059 		FOR i IN p_atp_bom.assembly_identifier.first..p_atp_bom.assembly_identifier.last
5060 		LOOP
5061 		lStmtNum := 120;
5062 			IF PG_DEBUG <> 0 THEN
5063 				oe_debug_pub.add('create_atp_bom: ' || to_char(p_atp_bom.Assembly_Identifier(i))||'  '||
5064 					to_char(p_atp_bom.Assembly_Item_Id(i))||'  '||
5065 					to_char(p_atp_bom.Component_Identifier(i))||'  '||
5066 					to_char(p_atp_bom.Component_Item_Id(i))||'  '||
5067 					to_char(p_atp_bom.Quantity(i))||'  '||
5068 					to_char(p_atp_bom.Fixed_LT(i))||'  '||
5069 					to_char(p_atp_bom.Variable_LT(i))||'  '||
5070 					to_char(p_atp_bom.Pre_Process_LT(i))||'  '||
5071 					to_char(p_atp_bom.Effective_Date(i))||'  '||
5072 					to_char(p_atp_bom.Disable_Date(i))||'  '||
5073 					to_char(p_atp_bom.atp_check(i)) ||'  '||
5074 					to_char(p_atp_bom.source_organization_id(i)) ||'  '||
5075 					p_atp_bom.SMC_Flag(i), 2);
5076 			END IF;
5077 		END LOOP; /* writing debug msg */
5078 
5079 	END IF; /* G_Cto_Sparse_Shipset.COUNT > 0 */
5080 
5081 EXCEPTION
5082 	WHEN FND_API.G_EXC_ERROR THEN
5083 		IF PG_DEBUG <> 0 THEN
5084 			oe_debug_pub.add('create_atp_bom: ' || 'Create_Atp_Bom::exp error::'||to_char(lStmtNum)||'::'||sqlerrm,1);
5085 		END IF;
5086 		x_return_status := FND_API.G_RET_STS_ERROR;
5087 		FND_MSG_PUB.Count_And_Get
5088         		(p_count => x_msg_count
5089         		,p_data  => x_msg_data
5090         		);
5091 	WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5092 		IF PG_DEBUG <> 0 THEN
5093 			oe_debug_pub.add('create_atp_bom: ' || 'Create_Atp_Bom::unexp error::'||to_char(lStmtNum)||'::'||sqlerrm,1);
5094 		END IF;
5095 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5096 		FND_MSG_PUB.Count_And_Get
5097         		(p_count => x_msg_count
5098         		,p_data  => x_msg_data
5099         		);
5100 	WHEN OTHERS THEN
5101 		IF PG_DEBUG <> 0 THEN
5102 			oe_debug_pub.add('create_atp_bom: ' || 'Create_Atp_Bom::others::'||to_char(lStmtNum)||'::'||sqlerrm,1);
5103 		END IF;
5104 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5105 		IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5106             		FND_MSG_PUB.Add_Exc_Msg
5107             			(G_PKG_NAME
5108             			,'Create_Atp_Bom'
5109             			);
5110         	END IF;
5111         	FND_MSG_PUB.Count_And_Get
5112         		(p_count => x_msg_count
5113         		,p_data  => x_msg_data
5114         		);
5115 
5116 END Create_Atp_Bom;
5117 
5118 
5119 
5120 PROCEDURE Extend_Atp_Bom (
5121 	p_atp_bom	IN OUT NOCOPY MRP_ATP_PUB.atp_bom_rec_typ,
5122 	x_return_status  out varchar2,
5123         x_msg_data       out varchar2,
5124         x_msg_count      out number)
5125 IS
5126 
5127 BEGIN
5128 
5129     --
5130     -- Current number of elements in BOM is 12
5131     -- This procedure needs to be updated each time a new element is
5132     -- added to the BOM
5133     --
5134     x_return_status := FND_API.G_RET_STS_SUCCESS;
5135     p_atp_bom.Assembly_Identifier.EXTEND;
5136     p_atp_bom.Assembly_Item_Id.EXTEND;
5137     p_atp_bom.Component_Identifier.EXTEND;
5138     p_atp_bom.Component_Item_Id.EXTEND;
5139     p_atp_bom.Quantity.EXTEND;
5140     p_atp_bom.Fixed_LT.EXTEND;
5141     p_atp_bom.Variable_LT.Extend;
5142     p_atp_bom.Pre_Process_LT.Extend;
5143     p_atp_bom.Effective_Date.Extend;
5144     p_atp_bom.Disable_Date.Extend;
5145     p_atp_bom.atp_check.Extend;
5146     p_atp_bom.wip_supply_type.Extend;
5147     p_atp_bom.SMC_Flag.Extend;
5148     p_atp_bom.source_organization_id.Extend;
5149     p_atp_bom.atp_flag.Extend;
5150 
5151 EXCEPTION
5152 	WHEN OTHERS THEN
5153 		IF PG_DEBUG <> 0 THEN
5154 			oe_debug_pub.add('Extend_Atp_Bom: ' || 'Extend_Atp_Bom::others::'||sqlerrm,1);
5155 		END IF;
5156 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5157 		IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5158             		FND_MSG_PUB.Add_Exc_Msg
5159             			(G_PKG_NAME
5160             			,'Extend_Atp_Bom'
5161             			);
5162         	END IF;
5163         	FND_MSG_PUB.Count_And_Get
5164         		(p_count => x_msg_count
5165         		,p_data  => x_msg_data
5166         		);
5167 END Extend_Atp_Bom;
5168 
5169 
5170 
5171 PROCEDURE Assign_Atp_Bom_Rec (
5172 	p_src_atp_bom	IN OUT NOCOPY MRP_ATP_PUB.atp_bom_rec_typ,
5173         p_src_location  in     number ,
5174         p_des_atp_bom   IN OUT MRP_ATP_PUB.atp_bom_rec_typ ,
5175 	x_return_status  out varchar2 ,
5176         x_msg_data       out varchar2 ,
5177         x_msg_count      out number )
5178 is
5179 v_des_location  number ;
5180 BEGIN
5181 
5182     --
5183     -- Current number of elements in BOM is 12
5184     -- This procedure needs to be updated each time a new element is
5185     -- added to the BOM
5186     --
5187     extend_atp_bom( p_des_atp_bom , x_return_status, x_msg_data, x_msg_count ) ;
5188     v_des_location := p_des_atp_bom.assembly_identifier.count ;
5189     p_des_atp_bom.assembly_identifier(v_des_location) := p_src_atp_bom.Assembly_Identifier(p_src_location ) ;
5190     p_des_atp_bom.assembly_item_id(v_des_location) := p_src_atp_bom.Assembly_Item_Id(p_src_location ) ;
5191     p_des_atp_bom.component_identifier(v_des_location) := p_src_atp_bom.Component_Identifier(p_src_location ) ;
5192     p_des_atp_bom.component_item_id(v_des_location) := p_src_atp_bom.Component_Item_Id(p_src_location) ;
5193     p_des_atp_bom.quantity(v_des_location) := p_src_atp_bom.Quantity(p_src_location) ;
5194     p_des_atp_bom.fixed_lt(v_des_location) := p_src_atp_bom.Fixed_LT(p_src_location) ;
5195     p_des_atp_bom.variable_lt(v_des_location) := p_src_atp_bom.Variable_LT(p_src_location) ;
5196     p_des_atp_bom.pre_process_lt(v_des_location) := p_src_atp_bom.Pre_Process_LT(p_src_location) ;
5197     p_des_atp_bom.effective_date(v_des_location) := p_src_atp_bom.Effective_Date(p_src_location) ;
5198     p_des_atp_bom.disable_date(v_des_location) := p_src_atp_bom.Disable_Date(p_src_location) ;
5199     p_des_atp_bom.wip_supply_type(v_des_location) := p_src_atp_bom.wip_supply_type(p_src_location) ;
5200     p_des_atp_bom.atp_check(v_des_location) := p_src_atp_bom.atp_check(p_src_location) ;
5201     p_des_atp_bom.smc_flag(v_des_location) := p_src_atp_bom.smc_flag(p_src_location) ;
5202     p_des_atp_bom.source_organization_id(v_des_location) := p_src_atp_bom.source_organization_id(p_src_location) ;
5203     p_des_atp_bom.atp_flag(v_des_location) := p_src_atp_bom.atp_flag(p_src_location) ;
5204 
5205 END assign_atp_bom_rec ;
5206 
5207 
5208 
5209 PROCEDURE Populate_Mandatory_Components(p_atp_bom 	IN OUT NOCOPY MRP_ATP_PUB.atp_bom_rec_typ,
5210 					p_index		IN NUMBER,
5211 					x_Return_Status OUT VARCHAR2,
5212 					x_Msg_Count	OUT NUMBER,
5213 					x_Msg_Data	OUT VARCHAR2)
5214 IS
5215 
5216 CURSOR c_mand_comps IS
5217 select bic.component_item_id component_item_id,
5218 bic.component_quantity component_quantity,
5219 bic.effectivity_date eff_date,
5220 bic.disable_date disable_date,
5221 bic.check_atp check_atp,
5222 bic.wip_supply_type
5223 from bom_bill_of_materials bbom,
5224 	bom_inventory_components bic
5225 where bbom.assembly_item_id = g_cto_sparse_shipset(p_index).Inventory_Item_Id
5226 and bbom.organization_id = g_cto_sparse_shipset(p_index).Sourcing_Org
5227 and bbom.alternate_bom_designator is NULL
5228 and bbom.common_bill_sequence_id = bic.bill_sequence_id
5229 and bic.optional = 2
5230 and nvl(bic.disable_date, sysdate) >= sysdate
5231 and bic.implementation_date is not null
5232 and bic.bom_item_type = 4;
5233 
5234 i		NUMBER := 0;
5235 j		NUMBER := 0;
5236 l_fixed_lt	NUMBER;
5237 l_variable_lt	NUMBER;
5238 l_preproc_lt	NUMBER;
5239 l_atp_flag	VARCHAR2(1);
5240 l_atp_comps_flag	VARCHAR2(1);
5241 lStmtNum	NUMBER;
5242 l_disable_date	DATE;
5243 l_model_item_id NUMBER;
5244 l_model_src_org NUMBER;
5245 l_model_line_id	NUMBER;		--model's line id
5246 l_new_model_line_id NUMBER;
5247 l_wip_supply_type NUMBER;
5248 l_parent_line_id NUMBER;	--parent ATO line id
5249 l_link_line_id 	NUMBER;		--immediate parent line id
5250 l_error_code	NUMBER;
5251 --x_return_status VARCHAR2(1);
5252 x_bill_id	NUMBER;
5253 
5254 BEGIN
5255 	IF PG_DEBUG <> 0 THEN
5256 		oe_debug_pub.add('Populate_Mandatory_Components: ' || 'Begin Populate_Mandatory_Components', 2);
5257 	END IF;
5258 	lStmtNum := 110;
5259 	x_return_status := FND_API.G_RET_STS_SUCCESS;
5260 	l_model_item_id := g_cto_sparse_shipset(p_index).Inventory_Item_Id;
5261 	l_model_src_org := g_cto_sparse_shipset(p_index).Sourcing_Org;
5262 	l_model_line_id := g_cto_sparse_shipset(p_index).Line_Id;
5263 
5264 	IF PG_DEBUG <> 0 THEN
5265 		oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Model item::'||to_char(l_model_item_id), 2);
5266 		oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Model src org::'||to_char(l_model_src_org), 2);
5267 		oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Model line_id::'||to_char(l_model_line_id), 2);
5268 	END IF;
5269 
5270 	/* In case of an option class or phantom model, assembly
5271 	information for SMC's should be that of the next non-phantom
5272 	parent model */
5273 	IF (g_cto_sparse_shipset(l_model_line_id).Ato_Line_Id <> g_cto_sparse_shipset(l_model_line_id).Line_Id) THEN
5274 		/* not top level ATO model */
5275 		lStmtNum := 111;
5276 		l_parent_line_id := g_cto_sparse_shipset(l_model_line_id).Parent_Ato_Line_id;
5277 		l_link_line_id := g_cto_sparse_shipset(l_model_line_id).Link_To_Line_id;
5278 		IF PG_DEBUG <> 0 THEN
5279 			oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Parent line_id::'||to_char(l_parent_line_id), 4);
5280 			oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Link_to_line_id::'||to_char(l_link_line_id), 4);
5281 		END IF;
5282 
5283 		lStmtNum := 112;
5284 		select bic.wip_supply_type
5285 		into l_wip_supply_type
5286 		from bom_bill_of_materials bbom,
5287 			bom_inventory_components bic
5288 		where bbom.assembly_item_id = g_cto_sparse_shipset(l_link_line_id).Inventory_Item_Id
5289 		and bbom.organization_id = g_cto_sparse_shipset(l_link_line_id).Sourcing_Org
5290 		and bbom.alternate_bom_designator is NULL
5291 		and bbom.common_bill_sequence_id = bic.bill_sequence_id
5292 		and bic.component_item_id = g_cto_sparse_shipset(l_model_line_id).Inventory_Item_Id
5293 		and rownum = 1;
5294 
5295 		IF nvl(l_wip_supply_type, -1) = 6 THEN
5296 			-- phantom model or option class
5297 			lStmtNum := 113;
5298 			IF PG_DEBUG <> 0 THEN
5299 				oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Phantom model or option class', 2);
5300 			END IF;
5301 			l_new_model_line_id := l_parent_line_id;
5302 		ELSE
5303 			lStmtNum := 114;
5304 			IF PG_DEBUG <> 0 THEN
5305 				oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Non-Phantom model', 2);
5306 			END IF;
5307 			l_new_model_line_id := l_model_line_id;
5308 		END IF;
5309 	ELSE  /* top level ATO model */
5310 		lStmtNum := 115;
5311 		IF PG_DEBUG <> 0 THEN
5312 			oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Top level ATO model', 2);
5313 		END IF;
5314 		l_new_model_line_id := l_model_line_id;
5315 	END IF;
5316 
5317 	IF PG_DEBUG <> 0 THEN
5318 		oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'Model line_id::'||to_char(l_model_line_id), 2);
5319 		oe_debug_pub.add('**Populate_Mandatory_Components: ' || 'New Model line_id::'||to_char(l_new_model_line_id), 2);
5320 	END IF;
5321 
5322 	lStmtNum := 116;
5323 	IF PG_DEBUG <> 0 THEN
5324 		oe_debug_pub.add('Populate_Mandatory_Components: ' || 'Getting eff and dis date for optional comps', 4);
5325 	END IF;
5326 
5327         -- Modified by Renga Kannan on 10/30/02
5328         -- The blind query on bom_calendar is modified with proper join
5329         -- The global variable used here G_OE_VALIDATION_ORG is set in the
5330         -- populate_om_shipset and populate_cz_shipset part of code
5331         -- We will get the calender dates based on the calendar code parameter
5332         -- set in the Oe validation org.
5333 
5334 	select max(calendar_date)
5335 	into l_disable_date
5336 	from bom_calendar_dates cal,
5337              mtl_parameters mp
5338         where mp.organization_id = CTO_ATP_INTERFACE_PK.G_OE_VALIDATION_ORG
5339         and   mp.calendar_code   = cal.calendar_code
5340         and   mp.calendar_exception_set_id = cal.exception_set_id;
5341 
5342 	IF PG_DEBUG <> 0 THEN
5343 		oe_debug_pub.add('Populate_Mandatory_Components: ' || 'l_disable_date :: '||to_char(l_disable_date), 4);
5344 	END IF;
5345 
5346 	FOR nxtrec IN c_mand_comps
5347 	LOOP
5348 		lStmtNum := 120;
5349 		i := p_atp_bom.assembly_identifier.count+1;
5350 		j := j + 1;
5351 		Extend_Atp_Bom(p_atp_bom,
5352 				x_Return_Status,
5353 				x_Msg_Count,
5354 				x_Msg_Data);
5355 
5356 		IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5357 			IF PG_DEBUG <> 0 THEN
5358 				oe_debug_pub.add('Populate_Mandatory_Components: ' || 'Unexp error in Extend_ATP_BOM::'||sqlerrm,1);
5359 			END IF;
5360 			raise FND_API.G_EXC_UNEXPECTED_ERROR;
5361 		END IF;
5362 		IF PG_DEBUG <> 0 THEN
5363 			oe_debug_pub.add('Populate_Mandatory_Components: ' || 'After extend_atp', 5);
5364 		END IF;
5365 
5366 		/*Populate this item's information*/
5367 		lStmtNum := 130;
5368 		p_atp_bom.Component_Item_Id(i) := nxtrec.component_item_id;
5369 		--
5370 		-- SMC's do not have line_id information.
5371 		-- However, ATP engine needs to distinguish between the
5372 		-- same SMC's existing more than once at different
5373 		-- levels in the model BOM
5374 		-- Hence, populating the immediate parent's line id
5375 		-- (model's line_id) as the component identifier
5376 		-- (Discussed with navneet)
5377 		--
5378 		p_atp_bom.Component_Identifier(i) := l_model_line_id;
5379 		p_atp_bom.Assembly_Item_Id(i) := g_cto_sparse_shipset(l_new_model_line_id).Inventory_Item_Id;
5380 		p_atp_bom.Assembly_Identifier(i) := l_new_model_line_id;
5381 		p_atp_bom.Quantity(i) := nxtrec.component_quantity;
5382 		p_atp_bom.Effective_date(i) := nxtrec.eff_date;
5383 		p_atp_bom.Disable_date(i) := nvl(nxtrec.disable_date, l_disable_date);
5384 		p_atp_bom.atp_check(i) := nxtrec.Check_ATP;
5385 		p_atp_bom.wip_supply_type(i) := nxtrec.wip_supply_type ;
5386 		p_atp_bom.SMC_Flag(i) := 'Y';
5387 		p_atp_bom.source_organization_id(i) := g_cto_sparse_shipset(p_index).sourcing_org ;
5388 
5389 		lStmtNum := 140;
5390 		--
5391 		-- Lead time required for all items (bug 2560915)
5392 		--
5393 		select nvl(fixed_lead_time, 0), nvl(variable_lead_time, 0), nvl(preprocessing_lead_time, 0)
5394 		into l_fixed_lt, l_variable_lt, l_preproc_lt
5395 		from mtl_system_items
5396 		where inventory_item_id = g_cto_sparse_shipset(l_new_model_line_id).Inventory_Item_Id
5397 		and organization_id = g_cto_sparse_shipset(l_new_model_line_id).Sourcing_Org;
5398 
5399 		lStmtNum := 150;
5400 		p_atp_bom.Fixed_LT(i) := l_fixed_lt;
5401 		p_atp_bom.Variable_LT(i) := l_variable_lt;
5402 		p_atp_bom.Pre_Process_LT(i) := l_preproc_lt;
5403 
5404 		--
5405 		-- Populate atp_flag for each component (bug 2462661)
5406 		--
5407 		lStmtNum := 160;
5408 		select atp_flag, atp_components_flag
5409 		into l_atp_flag, l_atp_comps_flag
5410 		from mtl_system_items
5411 		where inventory_item_id = nxtrec.component_item_id
5412 		and organization_id = g_cto_sparse_shipset(l_new_model_line_id).Sourcing_Org;
5413 
5414 		lStmtNum := 170;
5415 		IF (l_atp_flag <> 'N' OR l_atp_comps_flag <> 'N') THEN
5416 			p_atp_bom.atp_flag(i) := 'Y';
5417 		ELSE
5418 			p_atp_bom.atp_flag(i) := 'N';
5419 		END IF;
5420 
5421 	END LOOP;
5422 	IF PG_DEBUG <> 0 THEN
5423 		oe_debug_pub.add('Populate_Mandatory_Components: ' || 'added '||to_char(j)||' mand comps for item '||to_char(l_model_item_id)||' in org '||to_char(l_model_src_org), 2);
5424 	END IF;
5425 
5426 EXCEPTION
5427 	WHEN FND_API.G_EXC_ERROR THEN
5428 		IF PG_DEBUG <> 0 THEN
5429 			oe_debug_pub.add('Populate_Mandatory_Components: ' || 'Populate_Mandatory_Components::exp error::'||to_char(lStmtNum)||'::'||sqlerrm,1);
5430 		END IF;
5431 		x_return_status := FND_API.G_RET_STS_ERROR;
5432 
5433 	WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5434 		IF PG_DEBUG <> 0 THEN
5435 			oe_debug_pub.add('Populate_Mandatory_Components: ' || 'Populate_Mandatory_Components::unexp error::'||to_char(lStmtNum)||'::'||sqlerrm,1);
5436 		END IF;
5437 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5438 		FND_MSG_PUB.Count_And_Get
5439         		(p_count => x_msg_count
5440         		,p_data  => x_msg_data
5441         		);
5442 	WHEN OTHERS THEN
5443 		IF PG_DEBUG <> 0 THEN
5444 			oe_debug_pub.add('Populate_Mandatory_Components: ' || 'Populate_Mandatory_Components::others::'||to_char(lStmtNum)||'::'||sqlerrm,1);
5445 		END IF;
5446 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5447 		IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5448             		FND_MSG_PUB.Add_Exc_Msg
5449             			(G_PKG_NAME
5450             			,'Populate_Mandatory_Components'
5451             			);
5452         	END IF;
5453         	FND_MSG_PUB.Count_And_Get
5454         		(p_count => x_msg_count
5455         		,p_data  => x_msg_data
5456         		);
5457 
5458 END Populate_Mandatory_Components;
5459 
5460 
5461 
5462 
5463 --   The structure of BOM_CTO_ORDER_TABLE
5464 --   Table Name : Bom_Cto_order_lines_all
5465 --   Bcod_line_id                 ====> Unique sequence no (Primary Key)
5466 --   Oe_line_id                   ====> line_id from OE_order_lines_all table
5467 --   Ato_line_id                  ====> Top ato model line_id (Mainly used to flush the
5468 --                                         data with respect to a model)
5469 --   Inventory_item_id            ====> Inventory_item_id of the model/option class/
5470 --                                      option item
5471 --   Organization_id              ====> In case of model/option class/option item rows
5472 --                                      it will be having manufacturing org id
5473 --                                      In case of config item/ Ato item it will be having
5474 --                                      ship_from_org_id
5475 --   Required_date                ====> Demand date
5476 --   Required_qty                 ====> Demand Quantity
5477 --   Order_quantity_uom           ====> Uom of the item
5478 --   Parent_Demand_type           ====> If it is '1' THEN this row belongs to ATO MODEL
5479 --                                      if it is '2' THEN this row belongs to ATO ITEM
5480 --                                      (Mainly used in the flusing the data for all
5481 --                                       ATO item lines before cplanning collection )
5482 --   Header_id                    ====> Oe_order_headers_all header_id
5483 --   Forecast_visible             ====> If 'Y' it will be exploded by Forecast
5484 --                                      If 'N' it will be ignored by the Forecast exp
5485 --   Demand_visible               ====> If 'Y' this row is visible for planning.
5486 --                                      If 'N' this row will not be visible for planning.
5487 
5488 
5489 FUNCTION is_config_line_exists(p_ato_line_id  IN Number
5490                                 ) RETURN VARCHAR2;
5491 
5492 PROCEDURE Create_demand_line(
5493                              p_session_id        IN Number,
5494                              p_line_id           IN Number,
5495                              p_ato_line_id       IN Number,
5496                              p_inventory_item_id IN oe_order_lines_all.inventory_item_id%type,
5497                              p_org_id            IN Number,
5498                              p_forecast_flag     IN Varchar2,
5499                              p_demand_flag       IN Varchar2,
5500                              p_config_line       IN varchar2
5501                             );
5502 
5503 
5504 /*------------------------------------------------------------------------------
5505             Created by   : Renga Kannan
5506             Created Date : 26-sep-2000
5507             Purpose      : This procedure is being called from ATP when it
5508                            scheduling succeeds.The input for this procedure
5509                            is session_id for the pegging tree,reduced ship set
5510                            and the shared structure stored by the procedure
5511                            GET_ATP_BOM_PUB.
5512                            This procedure will get the required_qty and
5513                            required_date from ATP pegging tree table
5514                            MRP_ATP_DETAILS_TEMP and insert the entier row
5515                            into the table BOM_CTO_ORDER_DEMAND.
5516            Input         :
5517               p_ship_set   - This is the IN OUT Parameter. When it is called
5518                              from ATP it will have the Reduced Ship set in it.
5519                              And the end of this procedure it will be having
5520                              the full ship set(Transformed).
5521               p_success_flag - This will have 'Y' or 'N' based on the scheduling                               Succeeds or fails.
5522               p_session_id - session_id is used to identify the set of records
5523                              in the peggin tree
5524            Process       : The records in the table BOM_CTO_ORDER_DEMAND is
5525                            populated
5526            Output        :
5527               xreturn_status - Return FND_API.G_RET_STS_SUCCESS if the procedure
5528                                is executed successfully
5529                                Return FND_API.G_RET_STS_ERROR if the procedure
5530                                is completed with expected error
5531                                Return FND_API.G_RET_STS_UNEXP_ERROR if the
5532                                procedure is completed with unexpected error
5533 
5534 The Logic for CREATE_CTO_MODEL_DEMAND is as follows:
5535 1. Scan through the records in the Shared shipset
5536 2. For each record in the shared shipset find out the shipset name
5537 3. If the shipset is not sourced and the action is rescheduling
5538    THEN delete the information for this top model from BCOD table.
5539 
5540 ------------------------------------------------------------------------------*/
5541 
5542 /*****************************************************************************************
5543 09-JAN-2001
5544 1. When the shipset is having a ATO model for which the configuration item
5545    is alerady created THEN the only action possible is rescheduling.
5546 2. In this case delete the existing demand from bcod for this line_id.
5547 3. And insert the new demands for all the model,option class and option item
5548    including config item with the proper visible_forecast_flag and visible_demand_flag
5549 
5550 *******************************************************************************************/
5551 
5552 PROCEDURE CREATE_CTO_MODEL_DEMAND(
5553           p_shipset        IN OUT NOCOPY MRP_ATP_PUB.ATP_REC_TYP,
5554           p_session_id     IN  number,
5555           p_shipset_status IN  MRP_ATP_PUB.SHIPSET_STATUS_REC_TYPE,
5556           xreturn_status   OUT varchar2,
5557           xMsgCount        OUT number,
5558           xMsgData         OUT varchar2) is
5559         i                    number; -- Loop counter
5560         lStmtNum             number;
5561         l_prev_shipset       varchar2(200) := null;
5562         l_shipset_status     varchar2(1);
5563         l_shipset_sourced    boolean;
5564         l_vis_forecast_flag  varchar2(1);
5565         l_vis_demand_flag    varchar2(1);
5566         l_config_exists      varchar2(1);
5567         ATP_NO_RECORD_ERROR  EXCEPTION;
5568         l_line_id            number;
5569 
5570 	 -- new variables for 3189261
5571         l_par_line_id           number;
5572         l_par_inventory_item_id number;
5573         l_par_bom_item_type     number;
5574         l_required_date         date;
5575         l_required_qty          number;
5576         l_order_qty             number;
5577         l_order_quantity_uom    varchar2(3);
5578         l_record_count          number;
5579         l_header_id             oe_order_lines_all.header_id%type;
5580         x_lead_time             number;
5581         l_fixed_lead_time       number;
5582         l_variable_lead_time    number;
5583         l_stmt_num              number;
5584         -- end new variables for 3189261
5585 
5586 BEGIN
5587 
5588    lStmtNum  := 10;
5589    SAVEPOINT  create_cto_model_demand_begin;
5590 
5591    IF PG_DEBUG <> 0 THEN
5592    	oe_debug_pub.add('create_cto_model_demand: ' || 'Begin CREATE_CTO_MODEL_DEMAND module ....',1);
5593    	oe_debug_pub.add('create_cto_model_demand: ' || 'Session id passed by ATP = '||to_char(p_session_id),1);
5594    END IF;
5595 
5596    -- Get the user id and Login id by calling the FND api
5597    guserid  := nvl(FND_GLOBAL.user_id,-1);
5598    gloginid := nvl(FND_GLOBAL.login_id,-1);
5599 
5600    IF PG_DEBUG <> 0 THEN
5601    	oe_debug_pub.add('create_cto_model_demand: ' || 'Record count in the ATP Shipset = '||to_char(p_shipset.action.count),2);
5602    END IF;
5603 
5604    IF p_shipset.action.count = 0 THEN
5605      IF PG_DEBUG <> 0 THEN
5606      	oe_debug_pub.add('create_cto_model_demand: ' || 'There are no records to process in ATP Shipset ',2);
5607      END IF;
5608      raise FND_API.G_EXC_UNEXPECTED_ERROR;
5609    END IF;
5610 
5611 /*
5612    The following part of the code is added for the Scheduling bug in OM.
5613    When the model option class or option item is deleted OM will indicate this
5614    With ATO_DELETE_FLAG in shipset. In this case CTO will simply delete the demand for that
5615    line_id in bcod and return the shipset back to ATP. ATP introduced a new flag ATO_DELETE_FLAG
5616    in shipset for this. For this delete case ATP will not call out pre ATP procedure.
5617 
5618    The following are the decision points in this design.
5619 
5620    1. When the option class or Option item is getting deleted, OM will pas only that
5621       perticular line to ATP with the appropirate flag. So at any time the no of records in the
5622       shipset will be 1.
5623 
5624    2. The action code in this case will be 120.
5625 */
5626     IF PG_DEBUG <> 0 THEN
5627    	oe_debug_pub.add('ato delete flag check',2);
5628    	oe_debug_pub.add('ato delete flag count '|| p_shipset.ato_delete_flag.count,2);
5629    	oe_debug_pub.add('ato delete flag first '|| p_shipset.ato_delete_flag.first ,2);
5630     END IF;
5631 
5632     IF ( ( p_shipset.ato_delete_flag.count > 0 ) AND
5633      	( nvl (p_shipset.ato_delete_flag(p_shipset.ato_delete_flag.first),'N')
5634        	= 'Y' ))
5635     THEN
5636 
5637     IF PG_DEBUG <> 0 THEN
5638     	oe_debug_pub.add('create_cto_model_demand: ' || 'One of the ATO line is deleted...',5);
5639     	oe_debug_pub.add('create_cto_model_demand: ' || 'No of records in shipset ='||to_char(p_shipset.action.count),5);
5640     	oe_debug_pub.add('create_cto_model_demand: ' || 'Action code   = '||to_char(p_shipset.action(p_shipset.action.first)),5);
5641     	oe_debug_pub.add('create_cto_model_demand: ' || 'Line id = '||to_char(p_shipset.identifier(p_shipset.identifier.first)),5);
5642     END IF;
5643 
5644     --- Delete the row from Bom_cto_order_demand for the line_id
5648     WHERE  OE_LINE_ID = l_line_id;
5645     l_line_id := p_shipset.identifier(p_shipset.identifier.first);
5646     DELETE
5647     FROM  BOM_CTO_ORDER_DEMAND
5649 
5650     IF PG_DEBUG <> 0 THEN
5651     	oe_debug_pub.add('create_cto_model_demand: ' || 'No of records deleted..'||sql%rowcount,2);
5652     END IF;
5653 
5654     xreturn_status := FND_API.G_RET_STS_SUCCESS;
5655 
5656     IF PG_DEBUG <> 0 THEN
5657     	oe_debug_pub.add('create_cto_model_demand: ' || 'Returning the control to ATP..',2);
5658     END IF;
5659 
5660     return;
5661   END IF;
5662 
5663   IF PG_DEBUG <> 0 THEN
5664 	oe_debug_pub.add('ato delete flag check done',2);
5665   END IF;
5666 
5667    --  Action_code 100 means Enquiry
5668    --  Action_code 110 means Scheduling
5669    --  Actino_code 120 means Rescheduling
5670 
5671    IF PG_DEBUG <> 0 THEN
5672    	oe_debug_pub.add('create_cto_model_demand: ' || '....Scheduling Action code...'|| to_char( p_shipset.action(p_shipset.action.first)), 2);
5673    END IF;
5674 
5675    -- If the scheduling is succeded and the action is not enquiry THEN
5676    -- create the demand in BCOD table....
5677    IF p_shipset.action(p_shipset.action.first) <> 100 THEN
5678 
5679     -- 2723674  Flushing any existing demand for SLSO
5680       if slso_Shipset.COUNT > 0
5681       then
5682        	slso_index := slso_Shipset.FIRST;
5683        	oe_debug_pub.add('FIRST slso_index is '||to_char(slso_index), 2);
5684         loop
5685       	     lStmtNum := 30;
5686              if (slso_shipset.EXISTS(slso_index))
5687              then
5688 
5689                 oe_debug_pub.add('ato line id is '||to_char(slso_shipset(slso_index).ato_line_id), 2);
5690 
5691              	delete from bom_cto_order_demand
5692       		where ato_line_id = slso_shipset(slso_index).ato_line_id;
5693       		rows_deleted	:= sql%rowcount;
5694 
5695       		oe_debug_pub.add('Deleted '||to_char(rows_deleted)||' rows. ', 2);
5696 
5697       	     slso_index := slso_Shipset.NEXT(slso_index);
5698       	     oe_debug_pub.add('NEXT slso_index is '||to_char(slso_index), 2);
5699       	     EXIT WHEN slso_index IS NULL;
5700       	     end if;
5701       	end loop;
5702        end if;
5703       -- end 2723674
5704 
5705       -- Loop through the shared  Shipset. For each record in the shared
5706       -- shipset get the required quantity and required date from the ATP
5707       -- pegging tree table MRP_ATP_DETAILS_TEMP
5708 
5709       IF PG_DEBUG <> 0 THEN
5710       	oe_debug_pub.add('create_cto_model_demand: ' || 'Total no of records in the shared stuct...'|| to_char(g_final_cto_shipset.count), 2);
5711       END IF;
5712 
5713       lStmtNum  := 15;
5714 
5715       -- 2723674
5716 
5717       IF g_final_cto_shipset.count > 0 THEN
5718 
5719       -- Commented following lines as a part of 2723674 fix
5720 
5721       -- IF g_final_cto_shipset.count = 0 THEN
5722       --    IF PG_DEBUG <> 0 THEN
5723       --   	oe_debug_pub.add('create_cto_model_demand: ' || '.. CREATE_CTO_MODEL_DEMAND module raising exp error::' ||'There are no records to process in the shared'||' Structure.....',1);
5724       --    END IF;
5725       --   raise FND_API.G_EXC_UNEXPECTED_ERROR;
5726       -- END IF;
5727 
5728       --
5729       -- We need to auto-generate shipset names once again in the structure
5730       -- returned by ATP. We use this auto-generated shipset name to
5731       -- reconstruct the shipset (bug 2598745)
5732       -- The auto-generated shipset names are already stored in the global
5733       -- structure g_auto_generated_shipset and need not be stored again.
5734       --
5735       lStmtNum := 17;
5736       FOR v_init IN p_shipset.ship_set_name.first..p_shipset.ship_set_name.last
5737       LOOP
5738       	IF ( p_shipset.ship_set_name(v_init ) is null
5739 		AND p_shipset.arrival_set_name(v_init ) is null) THEN
5740 
5741 	  	IF PG_DEBUG <> 0 THEN
5742 	  		oe_debug_pub.add('create_cto_model_demand: ' ||  'Both ship set and arrival set are null', 3);
5743 	  	END IF;
5744           	p_shipset.ship_set_name(v_init) := p_shipset.identifier(v_init) ;
5745 
5746       	ELSIF ( p_shipset.ship_set_name(v_init ) is null
5747 		AND p_shipset.arrival_set_name(v_init ) is not null) THEN
5748 
5749 	  	IF PG_DEBUG <> 0 THEN
5750 	  		oe_debug_pub.add('create_cto_model_demand: ' ||  'Arrival set is not null', 3);
5751 	  	END IF;
5752           	p_shipset.ship_set_name(v_init) := p_shipset.arrival_set_name(v_init) ;
5753 
5754       	END IF ;
5755       END LOOP ;	-- bug 2598745
5756 
5757       lStmtNum := 20;
5758       IF PG_DEBUG <> 0 THEN
5759       	oe_debug_pub.add('create_cto_model_demand: ' || '..Begin looping the shared structure...',2);
5760       END IF;
5761 
5762       i := g_final_cto_shipset.first;
5763 
5764       IF PG_DEBUG <> 0 THEN
5765       	oe_debug_pub.add('create_cto_model_demand: ' || 'Creating Demand in BCOD table...',2);
5766       END IF;
5767 
5768       FOR i IN g_final_cto_shipset.first..g_final_cto_shipset.last
5769       LOOP
5770        IF g_final_cto_shipset(i).mlmo_flag = 'Y' THEN 	--2723674
5771          -- Check if the shipset is succeeded in the scheduling action
5772          lStmtNum := 30;
5773          -- Whenever shipset is changing get the source and status information
5774          -- of the shipset
5775         IF not(g_final_cto_shipset(i).auto_generated AND not(g_final_cto_shipset(i).process_demand)) THEN
5779             l_shipset_sourced := get_shipset_source_flag
5776          IF nvl(l_prev_shipset,'-99') <> g_final_cto_shipset(i).ship_set_name THEN
5777             l_shipset_status := get_shipset_status(p_shipset_status,
5778                                                 g_final_cto_shipset(i).ship_set_name);
5780                                          (g_final_cto_shipset(i).ship_set_name);
5781             l_prev_shipset := g_final_cto_shipset(i).ship_set_name;
5782 
5783             IF PG_DEBUG <> 0 THEN
5784             	oe_debug_pub.add('create_cto_model_demand: ' || 'shipset name ===> '||g_final_cto_shipset(i).ship_set_name,2);
5785             	oe_debug_pub.add('create_cto_model_demand: ' || 'shipset status ====>'||l_shipset_status,2);
5786             END IF;
5787 
5788             -- The following assignment is required cuz if the action for this shipset is
5789             -- scheduling THEN it will not go to the is_config_line_exists part at all
5790             l_vis_forecast_flag := 'Y';
5791             l_vis_demand_flag   := 'Y';
5792          END IF;
5793 
5794          lStmtNum  := 35;
5795 	 IF  (FND_API.to_boolean(l_shipset_status))  THEN
5796             -- Deleted the information from BCOD table if the line is the
5797             -- Top model ATO line,
5798             -- Delete the line only if the action is rescheduling
5799 
5800             IF g_final_cto_shipset(i).ato_line_id = g_final_cto_shipset(i).line_id AND
5801                p_shipset.action(p_shipset.action.first) = 120 THEN
5802 
5803                -- If the config line does not exists set both forecast_visible
5804                -- and demand visible flag to Y
5805                -- If config line exists THEN set demand_visible to N and
5806                -- Forecast visible to Y. Also insert a line for the config item
5807                -- in bcod.
5808 
5809                lStmtNum := 38;
5810                l_config_exists :=  is_config_line_exists(g_final_cto_shipset(i).ato_line_id);
5811 
5812                IF PG_DEBUG <> 0 THEN
5813                	oe_debug_pub.add('create_cto_model_demand: ' || 'Config line exists for this ato_line_id '|| g_final_cto_shipset(i).ato_line_id||' Is '||l_config_exists,2);
5814                END IF;
5815 
5816                lStmtNum := 40;
5817                IF PG_DEBUG <> 0 THEN
5818                	oe_debug_pub.add('create_cto_model_demand: ' || ' Deleting the Bcod information for the '|| ' Ato line_id...'||to_char(g_final_cto_shipset(i).ato_line_id), 2);
5819                END IF;
5820 
5821                DELETE FROM BOM_CTO_ORDER_DEMAND
5822                WHERE  ato_line_id = g_final_cto_shipset(i).ato_line_id;
5823 
5824                lStmtNum  := 43;
5825                IF l_config_exists = 'Y' THEN
5826                    l_vis_forecast_flag := 'Y';
5827                    l_vis_demand_flag   := 'N';
5828                    -- Insert the Config line demand in BCOD if the qty is non zero
5829                    -- And the shipset is sourced/multi org
5830                   -- Added one more condition to filter buy model components
5831                   -- If buy_model is set to 'Y' in g_cto_shipset we should
5832                   -- not store the demand. This change is done for
5833                   -- Procuring configurations
5834 
5835                    IF PG_DEBUG <> 0 THEN
5836                    	oe_debug_pub.add('create_cto_model_demand: ' || 'Buy model flag ='||nvl(g_final_cto_shipset(i).buy_model,'N'),2);
5837                    END IF;
5838 
5839                    IF (g_final_cto_shipset(i).ordered_quantity <> 0 AND
5840                       l_shipset_sourced  AND
5841                       nvl(g_final_cto_shipset(i).wip_supply_type,-1) <> 6) AND
5842                       nvl(g_final_cto_shipset(i).buy_model,'N') <> 'Y'     THEN
5843 
5844                       IF PG_DEBUG <> 0 THEN
5845                       	oe_debug_pub.add('create_cto_model_demand: ' || 'Inserting a row in bcod for config line...',1);
5846                       END IF;
5847 
5848                       Create_demand_line(p_session_id        => p_session_id,
5849                                          p_line_id           => g_final_cto_shipset(i).line_id,
5850                                          p_ato_line_id       => g_final_cto_shipset(i).ato_line_id,
5851                                          p_inventory_item_id => g_final_cto_shipset(i).inventory_item_id,
5852                                          p_org_id            => g_final_cto_shipset(i).sourcing_org,
5853                                          p_forecast_flag     => 'N',
5854                                          p_demand_flag       => 'Y',
5855                                          p_config_line       => 'Y');
5856 
5857                       IF PG_DEBUG <> 0 THEN
5858                       	oe_debug_pub.add('create_cto_model_demand: ' || 'Config line demand is inserted successfully..',1);
5859                       END IF;
5860 
5861                    END IF;
5862                ELSE
5863                    l_vis_forecast_flag := 'Y';
5864                    l_vis_demand_flag   := 'Y';
5865               END IF;
5866             END IF;
5867 
5868             -- Check whether the shipset is sourced one or not
5869             -- If it is sourced THEN create the demand in bcod table
5870             -- Otherwise proceed with the next record
5871             -- Added to not create the bcod data for cancelation
5872             IF PG_DEBUG <> 0 THEN
5873             	oe_debug_pub.add('create_cto_model_demand: ' || 'Ordered quantity...'||to_char(g_final_cto_shipset(i).ordered_quantity),2);
5874             END IF;
5875 
5876             --            Another fix is done for the Phantom item case. In the case of option item being phantom
5877             --            we need not store the demand, since neither planning or forecast is going to look into this
5878             --            entry. Apart from that we won't be getting any information for the phantom option item from
5879             --            the begging tree. So we skipp getting information and storing it in bcod for phantom option items
5880             --            This is as part of the bug fix #: 1531399
5881 
5882             IF PG_DEBUG <> 0 THEN
5883             	oe_debug_pub.add('create_cto_model_demand: ' || 'WIP_SUPPLY_TYPE = '||to_char(g_final_cto_shipset(i).wip_supply_type) ,2);
5884             END IF;
5885 
5886             IF PG_DEBUG <> 0 THEN
5887             	oe_debug_pub.add('create_cto_model_demand: ' || 'Buy model flag ='||nvl(g_final_cto_shipset(i).buy_model,'N'),2);
5888             END IF;
5889 
5890             IF     g_final_cto_shipset(i).ordered_quantity <> 0
5891                AND l_shipset_sourced
5892                -- 3189261 AND nvl(g_final_cto_shipset(i).wip_supply_type,-1) <> 6
5893                AND nvl(g_final_cto_shipset(i).buy_model,'N')      <> 'Y'  THEN
5894 
5895                 IF PG_DEBUG <> 0 THEN
5896                 	oe_debug_pub.add('create_cto_model_demand: ' || ' Shipset is sourced....', 2);
5897                 END IF;
5898 
5899                lStmtNum := 50;
5900                -- Get the information from ATP Pegging tree
5901                -- And insert the information to BCOD table
5902                IF PG_DEBUG <> 0 THEN
5903                	oe_debug_pub.add('create_cto_model_demand: ' || to_char(g_final_cto_shipset(i).line_id)||':'||
5904                                 to_char(g_final_cto_shipset(i).inventory_item_id )||':'||
5905                                 to_char(g_final_cto_shipset(i).sourcing_org)||':'||to_char(p_session_id),2);
5906                END IF;
5907 
5908                -- In the following call p_config_line is set to 'N' cuz this loop will
5909                -- Insert demands for Model,Option class and option item and not config lines.
5910 	       -- 3189261 : Add IF here
5911                IF nvl(g_final_cto_shipset(i).wip_supply_type,-1) <> 6 then
5912                  Create_demand_line(p_session_id        => p_session_id,
5913                                   p_line_id           => g_final_cto_shipset(i).line_id,
5914                                   p_ato_line_id       => g_final_cto_shipset(i).ato_line_id,
5915                                   p_inventory_item_id => g_final_cto_shipset(i).inventory_item_id,
5916                                   p_org_id            => g_final_cto_shipset(i).sourcing_org,
5917                                   p_forecast_flag     => l_vis_forecast_flag,
5918                                   p_demand_flag       => l_vis_demand_flag,
5919                                   p_config_line       => 'N');
5920 	       ELSE
5921                 -- 3189261 Derive the demand.
5922                 -- select parent details
5923 
5924 
5925                 begin
5926 
5927                  lStmtNum := 51;
5928 
5929                  IF PG_DEBUG <> 0 THEN
5930                      oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' line_id ' || g_final_cto_shipset(i).link_to_line_id , 1);
5931                  END IF;
5932 
5933 
5934                  select oel.line_id,
5935                         oel.inventory_item_id,
5936                         msi.bom_item_type
5937                  into   l_par_line_id,
5938                         l_par_inventory_item_id,
5939                         l_par_bom_item_type
5940                  from   oe_order_lines_all oel,
5941                         mtl_system_items msi
5942                  where  oel.line_id = g_final_cto_shipset(i).link_to_line_id
5943                  and    oel.inventory_item_id = msi.inventory_item_id
5944                  and    oel.ship_from_org_id = msi.organization_id;
5945 
5946 
5947                  IF PG_DEBUG <> 0 THEN
5948                          oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' line_id ' ||l_par_line_id ||
5949                                 ' inventory_item_id ' || l_par_inventory_item_id ||
5950                                 ' bom_item_type  ' || l_par_bom_item_type , 1);
5951                  END IF;
5952 
5953                 -- select uom of phantom item
5954 
5955                  lStmtNum := 52;
5956 
5957 		 IF PG_DEBUG <> 0 THEN
5958                     oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' line_id ' || g_final_cto_shipset(i).line_id , 1);
5959                  END IF;
5960 
5961                  select order_quantity_uom,ordered_quantity
5962                  into   l_order_quantity_uom,l_order_qty
5963                  from   oe_order_lines_all
5964                  where  line_id = g_final_cto_shipset(i).line_id;
5965 
5966                  IF PG_DEBUG <> 0 THEN
5967                     oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' order_quantity_uom ' || l_order_quantity_uom , 1);
5968                  END IF;
5969 
5970                  -- for bom_item_type = 2 ( Option Class ) and 1 ( Model)
5971                  -- get date from pegging tree
5972 
5973                  lStmtNum := 53;
5974 
5975                  IF PG_DEBUG <> 0 THEN
5976                          oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' session_id ' ||p_session_id ||
5977                                 ' component_identifier ' || g_final_cto_shipset(i).link_to_line_id ||
5978                                 ' inventory_item_id ' || l_par_inventory_item_id ||
5979                                 ' organization_id  ' || g_final_cto_shipset(i).sourcing_org , 1);
5980                  END IF;
5981 
5982                  select MAX(atp.supply_demand_date),          -- required_date
5983                         SUM(atp.supply_demand_quantity),      -- required_qty
5984                         MAX(oel.order_quantity_uom),          -- ordered_quantity_uom
5985                         MAX(oel.header_id),                   -- Header_id
5986                         COUNT(*)                              -- To get the no of rows selected
5987                                                              -- The above count(*) is added by renga on 12/22/00 to error out
5988                                                              -- in the case of zero rows.
5989                  into   l_required_date,
5990                         l_required_qty,
5991                         l_order_quantity_uom,
5992                         l_header_id,
5993                         l_record_count
5994 		 from   MRP_ATP_DETAILS_TEMP ATP,
5995                         OE_ORDER_LINES_ALL OEL
5996                  where  ATP.session_id             = p_session_id
5997                  and    ATP.component_identifier   = g_final_cto_shipset(i).link_to_line_id
5998                  and    ATP.inventory_item_id      = l_par_inventory_item_id
5999                  and    ATP.organization_id        = g_final_cto_shipset(i).sourcing_org
6000                  and    ATP.supply_demand_type     = 1 -- Demand
6001                  and    OEL.line_id                = ATP.component_identifier;
6002 
6003                  IF PG_DEBUG <> 0 THEN
6004                          oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' required_date ' ||l_required_date ||
6005                                 ' required_qty ' || l_required_qty ||
6006                                 ' order_quantity_uom ' || l_order_quantity_uom ||
6007                                 ' header_id ' || l_header_id , 1);
6008                  END IF;
6009 
6010                 -- additional massage of required date for Model
6011                 -- by offsetting it with lead time
6012                 if l_par_bom_item_type = 1 then
6013 
6014                         lStmtNum := 54;
6015 
6016                         select nvl(fixed_lead_time,0),
6017                                nvl(variable_lead_time,0)
6018                         into   l_fixed_lead_time,
6019                                l_variable_lead_time
6020                         from   mtl_system_items
6021                         where  inventory_item_id = l_par_inventory_item_id
6022                         and    organization_id   = g_final_cto_shipset(i).sourcing_org;
6023 
6024                         x_lead_time := l_fixed_lead_time  +
6025                                        l_variable_lead_time * l_required_qty;
6026 
6027                         IF PG_DEBUG <> 0 THEN
6028                          oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' Fixed LT ' ||l_fixed_lead_time ||
6029                                 ' Variable LT ' || l_variable_lead_time ||
6030                                 ' required_qty ' || l_required_qty ||
6031                                 ' lead_time ' || x_lead_time , 1);
6032                          END IF;
6033 
6034                 --  Calculate the offset date with CalENDer
6035 
6036 		 lStmtNum := 55;
6037 
6038                     IF PG_DEBUG <> 0 THEN
6039                      oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' Sourcing_org ' || g_final_cto_shipset(i).sourcing_org ||
6040                                 ' required_date ' || l_required_date ||
6041                                 ' lead_time ' || x_lead_time , 1);
6042                     END IF;
6043 
6044 
6045                     l_required_date := MSC_SATP_FUNC.src_date_offset (
6046                                         g_final_cto_shipset(i).sourcing_org,
6047                                         l_required_date,
6048                                         x_lead_time);
6049                 End if;
6050 
6051                 -- insert phantom item demand into bcod
6052 
6053                 lStmtNum := 56;
6054 
6055                 IF PG_DEBUG <> 0 THEN
6056                  oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: ' ||  ' oe_line_id ' || g_final_cto_shipset(i).line_id ||
6057                                 ' ato_line_id ' || g_final_cto_shipset(i).ato_line_id ||
6058                                 ' inventory_item_id ' || g_final_cto_shipset(i).inventory_item_id ||
6059                                 ' organization_id ' || g_final_cto_shipset(i).sourcing_org ||
6060                                 ' required_date ' || l_required_date ||
6061                                 ' required_qty ' || l_order_qty ||
6062                                 ' order_quantity_uom ' || l_order_quantity_uom ||
6063                                 ' header_id ' || g_final_cto_shipset(i).header_id ||
6064                                 ' forecast_visible ' || l_vis_forecast_flag ||
6065                                 ' demand_visible ' || l_vis_demand_flag , 1);
6066                 END IF;
6067 
6068 		INSERT INTO BOM_CTO_ORDER_DEMAND (
6069                       bcod_line_id,
6070                       oe_line_id,
6071                       ato_line_id,
6072                       inventory_item_id,
6073                       organization_id,
6074                       required_date,
6075                       required_qty,
6076                       order_quantity_uom,
6077                       parent_demand_type,
6078                       header_id,
6079                       forecast_visible,
6080                       demand_visible,
6081                       created_by,
6082                       last_updated_by,
6083                       last_updated_date,
6084                       last_update_login,
6085                       program_application_id,
6086                       program_update_date)
6087                VALUES(
6088                       BOM_CTO_ORDER_DEMAND_S1.nextval,           -- bcod_line_id
6089                       g_final_cto_shipset(i).line_id,            -- oe_line_id
6090                       g_final_cto_shipset(i).ato_line_id,        -- ato_line_id
6091                       g_final_cto_shipset(i).inventory_item_id,  -- Inventory_item_id
6092                       g_final_cto_shipset(i).sourcing_org,       -- organization_id
6093                       l_required_date,                           -- required_date
6094                       l_order_qty,                               -- required_qty
6095                       l_order_quantity_uom,                      -- ordered_quantity_uom
6096                       1,                                         -- parent_demand_type
6097                       g_final_cto_shipset(i).header_id,          -- header_id
6098                       l_vis_forecast_flag,                       -- forecast_visible
6099                       l_vis_demand_flag,                         -- demand_visible
6100                       guserid,                                   -- Created_by
6101                       guserid,                                   -- Last_updated_by
6102                       sysdate,                                   -- last_updated_date
6103                       gloginid,                                  -- Last update_login
6104                       null,                                      -- program_application_id
6105                       sysdate);                                  -- program_update_date
6106 		EXCEPTION
6107                 when no_data_found then
6108                         IF PG_DEBUG <> 0 THEN
6109                           oe_debug_pub.add( ' CREATE_CTO_MODEL_DEMAND: no_data_found in stmt # '||to_char(l_stmt_num) );
6110                         END IF;
6111 
6112                 when others then
6113                         IF PG_DEBUG <> 0 THEN
6114                           oe_debug_pub.add('CREATE_CTO_MODEL_DEMAND: unexp error in stmt # '||to_char(l_stmt_num)||' :: '|| sqlerrm);
6115                         END IF;
6116                 END;
6117 
6118             END IF;     -- end bugfix 3189261
6119 
6120           END IF ;
6121          END IF;
6122       END IF;
6123       END IF; 	-- 2723674
6124       END LOOP;
6125    END IF; -- 		2723674		g_final count > 0
6126    END IF; -- 		2723674		action code <> 100
6127 
6128    -- Call the procedure to reconstruct the Full ship set
6129    -- From Reduced ship set
6130    lStmtNum  := 70;
6131    IF PG_DEBUG <> 0 THEN
6132    	oe_debug_pub.add('create_cto_model_demand: ' || 'Calling reconstruct_shipset....',2);
6133    END IF;
6134    RECONSTRUCT_SHIPSET(
6135                        p_shipset,
6136                        p_shipset_status,
6137                        xreturn_status,
6138                        xmsgcount,
6139                        xmsgdata);
6140 
6141     IF PG_DEBUG <> 0 THEN
6142 	oe_debug_pub.add('create_cto_model_demand: ' || 'coming out of Reconstruct ship set with status ..'||xreturn_status,2);
6143     END IF;
6144 
6145    IF xreturn_status = FND_API.G_RET_STS_ERROR THEN
6146       IF PG_DEBUG <> 0 THEN
6147       	oe_debug_pub.add('create_cto_model_demand: ' || ' exp Error in Resurrect_shipset procedure..',1);
6148       END IF;
6149       raise FND_API.G_EXC_ERROR;
6150    ELSIF xreturn_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6151       IF PG_DEBUG <> 0 THEN
6152       	oe_debug_pub.add('create_cto_model_demand: ' || ' Unexp Error in Resurrect_shipset procedure..',1);
6153       END IF;
6154       raise FND_API.G_EXC_UNEXPECTED_ERROR;
6155    END IF;
6156    xreturn_status := FND_API.G_RET_STS_SUCCESS;
6157 
6158 EXCEPTION
6159            WHEN FND_API.G_EXC_ERROR THEN
6160                ROLLBACK to create_cto_model_demand_begin;
6161                IF PG_DEBUG <> 0 THEN
6162                	oe_debug_pub.add('create_cto_model_demand: ' || ' CREATE_CTO_MODEL_DEMAND :: exp error::'|| to_char(lStmtNum)||'::'||sqlerrm,1);
6163                END IF;
6164                xreturn_status := FND_API.G_RET_STS_ERROR;
6165                FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,'CREATE_CTO_MODEL_DEMAND');
6166                FND_MSG_PUB.Count_and_get(
6167                                          p_count => XMsgcount,
6168                                          p_data  => XMsgData);
6169            WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6170                ROLLBACK to create_cto_model_demand_begin;
6171                IF PG_DEBUG <> 0 THEN
6172                	oe_debug_pub.add('create_cto_model_demand: ' || ' CREATE_CTO_MODEL_DEMAND :: unexp error::'|| to_char(lStmtNum)||'::'||sqlerrm,1);
6173                END IF;
6174                xreturn_status := FND_API.G_RET_STS_UNEXP_ERROR;
6175                IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6176                   FND_MSG_PUB.Add_Exc_Msg(
6177                                            G_PKG_NAME
6178                                          , 'CREATE_CTO_MODEL_DEMAND'
6179                                            );
6180                END IF;
6181 
6182                FND_MSG_PUB.Count_and_get(
6183                                          p_count => XMsgCount,
6184                                          p_data  => xMsgData);
6185            WHEN OTHERS THEN
6186                ROLLBACK to create_cto_model_demand_begin;
6187                IF PG_DEBUG <> 0 THEN
6188                	oe_debug_pub.add('create_cto_model_demand: ' || ' CREATE_CTO_MODEL_DEMAND :: Other error ::'|| to_char(lStmtNum)||'::'||sqlerrm,1);
6189                END IF;
6190                xreturn_status := FND_API.G_RET_STS_UNEXP_ERROR;
6191                IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6192                   FND_MSG_PUB.Add_Exc_Msg(
6193                                            G_PKG_NAME
6194                                          , 'CREATE_CTO_MODEL_DEMAND'
6195                                            );
6196                END IF;
6197 
6198                FND_MSG_PUB.Count_and_get(
6199                                          p_count => XMsgCount,
6200                                          p_data  => XMsgData);
6201 END CREATE_CTO_MODEL_DEMAND;
6202 
6203 
6204 
6205 FUNCTION get_shipset_status(
6206                             p_shipset_tbl  IN MRP_ATP_PUB.SHIPSET_STATUS_REC_TYPE,
6207                             p_shipset_name IN varchar2) RETURN VARCHAR2 is
6208                             i  number := 0;
6209 BEGIN
6210       IF PG_DEBUG <> 0 THEN
6211       	oe_debug_pub.add('get_shipset_status: ' || ' in get_shipset_status ... record count...'|| to_char(p_shipset_tbl.ship_set_name.count), 3);
6212       	oe_debug_pub.add('get_shipset_status: ' || 'Param shipset name = '||p_shipset_name,3);
6213       END IF;
6214 
6215       IF p_shipset_tbl.ship_set_name.count = 0 THEN
6216          IF PG_DEBUG <> 0 THEN
6217          	oe_debug_pub.add('get_shipset_status: ' || ' UNEXP ERROR :: GET_SHIPSET_STATUS  '|| ' No records found in the shipset status structure ',2);
6218          END IF;
6219          raise FND_API.G_EXC_UNEXPECTED_ERROR;
6220       END IF;
6221 
6222       FOR i IN p_shipset_tbl.ship_set_name.first..p_shipset_tbl.ship_set_name.last
6223       LOOP
6224           IF PG_DEBUG <> 0 THEN
6225           	oe_debug_pub.add('get_shipset_status: ' || 'Loop shipset name ='||p_shipset_tbl.ship_set_name(i),3);
6226           END IF;
6227           IF p_shipset_tbl.ship_set_name(i) = p_shipset_name THEN
6228               return p_shipset_tbl.status(i);
6229           END IF;
6230       END LOOP;
6231       IF PG_DEBUG <> 0 THEN
6232       	oe_debug_pub.add('get_shipset_status: ' || ' UNEXP ERROR :: GET_SHIPSET_STATUS  '|| ' The shipset name is not found in the pl/sql record '|| ' Shipset Name : '||p_shipset_name,2);
6233       END IF;
6234       return 'F';
6235 END get_shipset_status;
6236 
6237 
6238 
6239 FUNCTION get_shipset_source_flag(p_shipset_name IN varchar2) RETURN BOOLEAN is
6240       i    number := 0;
6241 BEGIN
6242       IF PG_DEBUG <> 0 THEN
6243       	oe_debug_pub.add('get_shipset_source_flag: ' || 'get_shipset_source_flag :param value :'||p_shipset_name,3);
6244       END IF;
6245 
6246       IF g_shipset_status_tbl.count = 0 THEN
6247          IF PG_DEBUG <> 0 THEN
6248          	oe_debug_pub.add('get_shipset_source_flag: ' || ' UNEXP ERROR :: GET_SHIPSET_SOURCE_FLAG '|| ' No records found in the shipset structure ',2);
6249          END IF;
6250       END IF;
6251 
6252       FOR i IN g_shipset_status_tbl.first..g_shipset_status_tbl.last
6253       LOOP
6254         IF PG_DEBUG <> 0 THEN
6255         	oe_debug_pub.add('get_shipset_source_flag: ' || ' source flag shipset name ...'|| g_shipset_status_tbl(i).ship_set_name,3);
6256         END IF;
6257         IF g_shipset_status_tbl(i).ship_set_name = p_shipset_name THEN
6258 		IF PG_DEBUG <> 0 THEN
6259 			oe_debug_pub.add('get_shipset_source_flag: ' || 'return success from source_flag proc ...',2);
6260 		END IF;
6261           return g_shipset_status_tbl(i).model_sourced;
6262         END IF;
6263       END LOOP;
6264       IF PG_DEBUG <> 0 THEN
6265       	oe_debug_pub.add('get_shipset_source_flag: ' || ' UNEXP ERROR :: GET_SHIPSET_SOURCE_FLAG '||
6266                        ' The shipset name is not found in the pl/sql record '||
6267                        ' Shipsets Name :'||p_shipset_name,2);
6268       END IF;
6269       return false;
6270 END get_shipset_source_flag;
6271 
6272 
6273 
6274 FUNCTION is_config_line_exists(p_ato_line_id IN Number) RETURN VARCHAR2 is
6275          x_config_exists     varchar2(1);
6276 BEGIN
6277          IF PG_DEBUG <> 0 THEN
6278          	oe_debug_pub.add('is_config_line_exists: ' || 'Entering is_config_item_exists for ato_line_id '|| to_char(p_ato_line_id), 4);
6279          END IF;
6280 
6281          SELECT  'Y'
6282          INTO    x_config_exists
6283          FROM    OE_ORDER_LINES_ALL
6284          WHERE   ato_line_id  = p_ato_line_id
6285          AND     ITEM_TYPE_CODE = 'CONFIG';
6286 
6287          return x_config_exists;
6288 EXCEPTION
6289          WHEN NO_DATA_FOUND THEN
6290          IF PG_DEBUG <> 0 THEN
6291          	oe_debug_pub.add('is_config_line_exists: ' || 'Config line does not exists ..',1);
6292          END IF;
6293          x_config_exists := 'N';
6294          return x_config_exists;
6295          WHEN OTHERS THEN
6296          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6297 END is_config_line_exists;
6298 
6299 
6300 
6301 PROCEDURE create_demand_line(
6302                              p_session_id        IN Number,
6303                              p_line_id           IN Number,
6304                              p_ato_line_id       IN Number,
6305                              p_inventory_item_id IN oe_order_lines_all.inventory_item_id%type,
6306                              p_org_id            IN Number,
6307                              p_forecast_flag     IN Varchar2,
6308                              p_demand_flag       IN Varchar2,
6309                              p_config_line       IN varchar2) is
6310 
6311 
6312            l_required_date        date;
6313            l_required_qty         number;
6314            l_order_quantity_uom   varchar2(3);
6315            l_record_count         number;
6316            l_header_id            oe_order_lines_all.header_id%type;
6317            l_line_id              oe_order_lines_all.line_id%type;
6318            l_org_id               mtl_system_items.organization_id%type;
6319            l_inv_item_id          mtl_system_items.inventory_item_id%type;
6320            ATP_NO_RECORD_ERROR    EXCEPTION;
6321            l_stmt_no              number;
6322 BEGIN
6323 
6324    -- If the Demand creation is for Config line
6325    -- get the demand record for the model with shipping org
6326    -- This will give you a exact schedule date and insert the
6327    -- the config line dtls in bcod table
6328    -- If it is not for config line THEN whatever we are getting in
6329    -- parameters that are safe.
6330 
6331    IF PG_DEBUG <> 0 THEN
6332    	oe_debug_pub.add('create_demand_line: ' || 'Entering create_demand_line procedure...',1);
6333    END IF;
6334    l_stmt_no := 10;
6335    IF p_config_line = 'Y' THEN
6336    --  Select the config line info from oe_order_lines_all
6337      l_stmt_no := 20;
6338      SELECT line_id,
6339             inventory_item_id,
6340             ship_from_org_id
6341      INTO
6342             l_line_id,
6343             l_inv_item_id,
6344             l_org_id
6345      FROM   OE_ORDER_LINES_ALL
6346      WHERE
6347                 ato_line_id    = p_ato_line_id
6348             AND item_type_code = 'CONFIG';
6349    ELSE
6350       l_line_id     := p_line_id;
6351       l_inv_item_id := p_inventory_item_id;
6352       l_org_id      := p_org_id;
6353    END IF;
6354 
6355    --             bug fix #: 1531429
6356    --             Details
6357    --                      When we are getting the information from Pegging Tree we may get more than
6358    --                      one row for the combination of session_id,inventroy_item_id,supply_demand_type,
6359    --                      and the component_identifier. This is happening if the scheduling activity
6360    --                      succeeds due to both forward and backward scheduling. In this case we should
6361    --                      take the sum of the qty as the request qty and max of date as the request date.
6362 
6363 
6364          -- Get the requirement_date and requirement_qty from
6365          -- MRP_ATP_DETAILS_TEMP (Pegging Tree) table
6366 
6367 
6368    l_stmt_no := 30;
6369 
6370    SELECT /* added required date for BUG#2465370  */
6371           MAX(nvl( atp.required_date, atp.supply_demand_date)),          -- required_date
6372           SUM(atp.supply_demand_quantity),      -- required_qty
6373           MAX(oel.order_quantity_uom),          -- ordered_quantity_uom
6374           MAX(oel.header_id),                   -- Header_id
6375           COUNT(*)                              -- To get the no of rows selected
6376                                    -- The above count(*) is added by renga on 12/22/00 to error out
6377                                    -- in the case of zero rows.
6378    INTO
6379           l_required_date,
6380           l_required_qty,
6381           l_order_quantity_uom,
6382           l_header_id,
6383           l_record_count
6384    FROM
6385           MRP_ATP_DETAILS_TEMP ATP,
6386           OE_ORDER_LINES_ALL OEL
6387    WHERE
6388                ATP.session_id           = p_session_id
6389           AND  ATP.component_identifier = p_line_id
6390           AND  ATP.inventory_item_id    = p_inventory_item_id
6391           AND  ATP.organization_id      = l_org_id
6392           AND  ATP.supply_demand_type   = 1 -- Demand
6393           AND  ATP.component_identifier = OEL.line_id;
6394 
6395 
6396     IF PG_DEBUG <> 0 THEN
6397     	oe_debug_pub.add('create_demand_line: ' || to_char(p_line_id)||':'||
6398                      to_char(p_inventory_item_id )||':'||
6399                      to_char(l_org_id)||':'||
6400                      to_char(l_required_qty)||':'||to_char(l_required_date),2);
6401     	oe_debug_pub.add('create_demand_line: ' || '# of Records in the Pegging tree :'||
6402                      to_char(l_record_count));
6403     END IF;
6404 
6405     --   This check is added to see whether ATP pegging tree returned any rows or not
6406     --   If there no rows returned from ATP THEN we need to raise an exception
6407 
6408     IF l_record_count = 0 THEN
6409 
6410        IF PG_DEBUG <> 0 THEN
6411        	oe_debug_pub.add('create_demand_line: ' || ' NO records in the pegging tree..',1);
6412        END IF;
6413        raise ATP_NO_RECORD_ERROR;
6414 
6415     ELSE
6416 
6417     l_stmt_no := 40;
6418 
6419     INSERT INTO BOM_CTO_ORDER_DEMAND (
6423                       inventory_item_id,
6420                       bcod_line_id,
6421                       oe_line_id,
6422                       ato_line_id,
6424                       organization_id,
6425                       required_date,
6426                       required_qty,
6427                       order_quantity_uom,
6428                       parent_demand_type,
6429                       header_id,
6430                       forecast_visible,
6431                       demand_visible,
6432                       created_by,
6433                       last_updated_by,
6434                       last_updated_date,
6435                       last_update_login,
6436                       program_application_id,
6437                       program_update_date)
6438     VALUES(
6439                       BOM_CTO_ORDER_DEMAND_S1.nextval,           -- bcod_line_id
6440                       l_line_id,                                 -- oe_line_id
6441                       p_ato_line_id,                             -- ato_line_id
6442                       l_inv_item_id,                             -- Inventory_item_id
6443                       l_org_id,                                  -- organization_id
6444                       l_required_date,                           -- required_date
6445                       l_required_qty,                            -- required_qty
6446                       l_order_quantity_uom,                      -- ordered_quantity_uom
6447                       1,                                         -- parent_demand_type
6448                       l_header_id,                               -- header_id
6449                       p_forecast_flag,                           -- forecast_visible
6450                       p_demand_flag,                             -- demand_visible
6451                       guserid,                                   -- Created_by
6452                       guserid,                                   -- Last_updated_by
6453                       sysdate,                                   -- last_updated_date
6454                       gloginid,                                  -- Last update_login
6455                       null,                                      -- program_application_id
6456                       sysdate);                                  -- program_update_date
6457 
6458     END IF;
6459 
6460    EXCEPTION
6461       WHEN NO_DATA_FOUND THEN
6462         IF PG_DEBUG <> 0 THEN
6463         	oe_debug_pub.add('create_demand_line: ' || 'error at statement no #'||to_char(l_stmt_no),1);
6464          	oe_debug_pub.add('create_demand_line: ' || 'No Records found in the ATP Pegging Tree
6465                            for  line_id ..'||
6466                            to_char(l_line_id)||
6467                            'Item id ....'||
6468                             to_char(l_inv_item_id)
6469                             ||' And Sourcing org  ...'
6470                             ||to_char(l_org_id)||sqlerrm,1);
6471          END IF;
6472          raise FND_API.G_EXC_UNEXPECTED_ERROR;
6473       WHEN ATP_NO_RECORD_ERROR THEN
6474         IF PG_DEBUG <> 0 THEN
6475         	oe_debug_pub.add('create_demand_line: ' || 'error at statement no #'||to_char(l_stmt_no),1);
6476         END IF;
6477         raise FND_API.G_EXC_UNEXPECTED_ERROR;
6478       WHEN OTHERS THEN
6479         IF PG_DEBUG <> 0 THEN
6480         	oe_debug_pub.add('create_demand_line: ' || 'error at statement no #'||to_char(l_stmt_no),1);
6481         	oe_debug_pub.add('create_demand_line: ' || 'Error in select :: Other:: in the ATP Pegging Tree for  line_id ..'|| to_char(l_line_id)||
6482                            'Item id ....'||
6483                             to_char(l_inv_item_id)
6484                                  ||' And Sourcing org  ...'
6485                                  ||to_char(l_org_id)||sqlerrm,1);
6486         END IF;
6487           raise FND_API.G_EXC_UNEXPECTED_ERROR;
6488    END Create_demand_line;
6489 
6490 
6491 PROCEDURE ato_item_exp(p_item_id  mtl_system_items.inventory_item_id%type,
6492                        p_org_id   mtl_system_items.organization_id%type,
6493                        p_qty      number,
6494                        p_req_date date,
6495                        p_line_id  number,
6496                        p_header_id number,
6497                        x_return_status out varchar2,
6498                        x_msg_count  out number,
6499                        x_msg_data   out varchar2);
6500 
6501 
6502 PROCEDURE create_demand(p_item_id in number,
6503                         p_org_id in number,
6504                         p_req_date in date,
6505                         p_qty in number,
6506                         p_line_id in number,
6507                         p_header_id in number,
6508                         x_return_status out varchar2,
6509                         x_msg_count     out number,
6510                         x_msg_data      out varchar2);
6511 
6512 
6513 PROCEDURE  Is_item_sourced(p_item_id in number,
6514                          p_org_id in number,
6515                          x_source_flag   out varchar2,
6516                          x_return_status out varchar2,
6517                          x_msg_count     out number,
6518                          x_msg_data      out varchar2);
6519 
6520 
6521 /*****************************************************************
6522    Procedured Name    :  CREATE_CTO_ITEM_DEMAND
6523    Created By         :  Renga Kannan
6524    Created Date       :  10-oct-2000
6528                          all oe_order_lines and get all the Ato_item_lines.
6525    Purpose            :  This procedure will be called by planning
6526                          collection process. Before planning starts
6527                          collecting the data this proecedure scan thru
6529                          For all these Ato_items this will explode the bill
6530                          and create the demand in the proper manufacturing
6531                          organizations.
6532   Input               :
6533   Output              : The proper demand record stored in the
6534                         BOM_CTO_ORDER_DEMAND
6535 ********************************************************************/
6536 
6537 
6538 --- Modified for performance reason
6539 --- Removed the join with Oe_order_headers_all table
6540 --- and chagned the open_flag where from headers to line
6541 --- this will improve the performance very well
6542 
6543 PROCEDURE CREATE_CTO_ITEM_DEMAND(x_return_status out varchar2,
6544                                  x_msg_count     out number,
6545                                  x_msg_data      out varchar2) is
6546        cursor ato_item_lines is
6547        SELECT
6548               oeol.header_id,
6549               oeol.line_id,
6550               oeol.inventory_item_id,
6551               oeol.ship_from_org_id,
6552               oeol.ordered_quantity,
6553               oeol.schedule_ship_date,
6554               oeol.order_quantity_uom
6555       FROM    oe_order_lines_all oeol
6556       WHERE
6557               oeol.open_flag = 'Y'  /* SRS added for performance to retrieve only open orde
6558 rs */
6559       AND     oeol.ship_from_org_id is not null
6560       AND     nvl(oeol.visible_demand_flag,'N') = 'Y'
6561       AND    ( oeol.item_type_code = 'STANDARD' OR oeol.item_type_code = 'OPTION' )
6562                /* added item_code = 'OPTION' to support ATO ITEMs under PTO Models per
6563 BUG#1874380 */
6564       AND     oeol.line_id = oeol.ato_line_id
6565       AND     nvl(oeol.source_document_type_id,0) <> 10; -- This is confirmed with OM
6566 
6567       lStmtNum     Number := 0;
6568       l_source_flag Varchar2(1) := 'N';
6569 BEGIN
6570      SAVEPOINT create_cto_item_demand_begin;
6571 
6572 --   Since the ATO item explosion is execute during collection this above global varibale
6573 --   needs to be initialized here also.
6574 
6575      IF PG_DEBUG <> 0 THEN
6576      	oe_debug_pub.add('create_cto_item_demand: ' || ' Begin CREATE_CTO_ITEM_DEMAND module...',1);
6577      END IF;
6578      guserid  := nvl(FND_GLOBAL.user_id,-1);
6579      gloginid := nvl(FND_GLOBAL.login_id,-1);
6580 
6581      -- Flush the data from BCOD table which is being populated
6582      -- by the previous collection run.
6583      -- The parent_demand_type = 2 tells that those rows are ato item rows
6584     lStmtNum := 10;
6585     -- Get the instace id from MRP_AP_APPS_INSTANCES
6586     -- This table will allways have one row
6587 
6588     lStmtNum := 13;
6589    /* initialize assignment set */
6590    initialize_assignment_set( x_return_status ) ;
6591 
6592    IF( x_return_status = FND_API.G_RET_STS_ERROR ) THEN
6593        IF PG_DEBUG <> 0 THEN
6594        	oe_debug_pub.add('create_cto_item_demand: ' || 'initialize_assignment_set returned with Expected error',1);
6595        END IF;
6596        RAISE FND_API.G_EXC_ERROR ;
6597 
6598    ELSIF( x_return_status = FND_API.G_RET_STS_UNEXP_ERROR ) THEN
6599        IF PG_DEBUG <> 0 THEN
6600        	oe_debug_pub.add('create_cto_item_demand: ' || 'initialize_assignment_set returned with Unexpected error',1);
6601        END IF;
6602        RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
6603    END IF ;
6604 
6605     IF PG_DEBUG <> 0 THEN
6606     	oe_debug_pub.add('create_cto_item_demand: ' || 'Before getting the instance id ..',2);
6607     END IF;
6608     BEGIN
6609        SELECT instance_id
6610        INTO   ginstance_id
6611        FROM   MRP_AP_APPS_INSTANCES;
6612     EXCEPTION WHEN OTHERS THEN
6613        IF PG_DEBUG <> 0 THEN
6614        	oe_debug_pub.add('create_cto_item_demand: ' || 'UNEXP ::Error in getting instance id  '||sqlerrm||'::'||'line number'||to_char(lStmtNum), 1);
6615        END IF;
6616        raise FND_API.G_EXC_UNEXPECTED_ERROR;
6617     END;
6618 
6619     IF PG_DEBUG <> 0 THEN
6620     	oe_debug_pub.add('create_cto_item_demand: ' || 'Instance_id = '||to_char(ginstance_id),2);
6621     END IF;
6622 
6623     lStmtNum := 15;
6624     -- Delete all the lines in the bcod table where the
6625     -- parent_demand_type = '2'. This will delete all the rows
6626     -- corresponding to ato items.
6627 
6628     IF PG_DEBUG <> 0 THEN
6629     	oe_debug_pub.add('create_cto_item_demand: ' || 'Before deleting records from Bcod table..',2);
6630     END IF;
6631 
6632     DELETE
6633     FROM
6634     BOM_CTO_ORDER_DEMAND
6635     WHERE  parent_demand_type = '2'; ---  The rows belongs to ato items
6636 
6637     lStmtNum := 20;
6638 
6639     IF PG_DEBUG <> 0 THEN
6640     	oe_debug_pub.add('create_cto_item_demand: ' || 'Start looping thru oe_order_lines.....',2);
6641     END IF;
6642 
6643     FOR myrec IN ato_item_lines
6644     LOOP
6645         lStmtNum := 30;
6646 
6647         IF PG_DEBUG <> 0 THEN
6648         	oe_debug_pub.add('create_cto_item_demand: ' || 'Line_id           ='||to_char(myrec.line_id),2);
6649         	oe_debug_pub.add('create_cto_item_demand: ' || 'Header_id         ='||to_char(myrec.header_id),2);
6653         	oe_debug_pub.add('create_cto_item_demand: ' || 'Before getting sourcing flag..',2);
6650         	oe_debug_pub.add('create_cto_item_demand: ' || 'inventory_item_id ='||to_char(myrec.inventory_item_id),2);
6651         	oe_debug_pub.add('create_cto_item_demand: ' || 'Ship_from_org_id  ='||to_char(myrec.ship_from_org_id),2);
6652         	oe_debug_pub.add('create_cto_item_demand: ' || 'Ordered_qty       ='||to_char(myrec.ordered_quantity),2);
6654         END IF;
6655 
6656         is_item_sourced(myrec.inventory_item_id,
6657                       myrec.ship_from_org_id,
6658                       l_source_flag,
6659                       x_return_status,
6660                       x_msg_count,
6661                       x_msg_data);
6662 
6663         IF x_return_status = FND_API.G_RET_STS_ERROR THEN
6664            IF PG_DEBUG <> 0 THEN
6665            	oe_debug_pub.add('create_cto_item_demand: ' || 'Exp Error in is_item_sourced procedure for the item_id'
6666                             ||to_char(myrec.inventory_item_id)
6667                             ||' and org id...'||to_char(myrec.ship_from_org_id)
6668                             ,1);
6669            END IF;
6670            raise FND_API.G_EXC_ERROR;
6671         ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6672            IF PG_DEBUG <> 0 THEN
6673            	oe_debug_pub.add('create_cto_item_demand: ' || 'Unexp Error in is_item_sourced procedure for the item_id'
6674                             ||to_char(myrec.inventory_item_id)
6675                             ||' and org id...'||to_char(myrec.ship_from_org_id)
6676                             ,1);
6677            END IF;
6678            raise FND_API.G_EXC_UNEXPECTED_ERROR;
6679         END IF;
6680 
6681         IF PG_DEBUG <> 0 THEN
6682         	oe_debug_pub.add('create_cto_item_demand: ' || 'Source_flag   = '||l_source_flag,2);
6683         END IF;
6684 
6685         -- If the line is not fully cancelled and this Ato item is sourced
6686         -- THEN demand will be exploded in the Bcod table...
6687 
6688         IF myrec.ordered_quantity <> 0 AND l_source_flag = 'Y' THEN
6689 
6690            -- Insert the Configuration line into the demand table.....
6691            lStmtNum := 40;
6692 
6693            IF PG_DEBUG <> 0 THEN
6694            	oe_debug_pub.add('create_cto_item_demand: ' || 'Inserting Config line info into bcod...',2);
6695            END IF;
6696 
6697            INSERT INTO
6698               BOM_CTO_ORDER_DEMAND(
6699               bcod_line_id,
6700               oe_line_id,
6701               ato_line_id,
6702               inventory_item_id,
6703               organization_id,
6704               required_date,
6705               required_qty,
6706               order_quantity_uom,
6707               parent_demand_type,
6708               header_id,
6709               forecast_visible,
6710               demand_visible,
6711               created_by,
6712               last_updated_by,
6713               last_updated_date,
6714               last_update_login,
6715               program_application_id,
6716               program_update_date)
6717            VALUES (
6718               bom_cto_order_demand_s1.nextval,    --  bcod_line_id
6719               myrec.line_id,                      --  oe_line_id
6720               myrec.line_id,                      --  ato_line_id
6721               myrec.inventory_item_id,            --  inventory_item_id
6722               myrec.ship_from_org_id,             --  organization_id
6723               myrec.schedule_ship_date,           --  Required_date
6724               Round( myrec.ordered_quantity, 6 ) ,--  Ordered Quantity /* Decimal-Qty Support for Option Items */
6725               myrec.order_quantity_uom,           --  Order_quantity_uom
6726               2,                                  --  parent_demand_type
6727               myrec.header_id,                    --  Header_id
6728               'N',                                --  Forecast_visible
6729               'Y',                                --  Demand_visible
6730               guserid,                            --  Created_by
6731               guserid,                            --  Last_updated_by
6732               sysdate,                            --  Last_updated_date
6733               gloginid,                           --  Last_update_login
6734               null,                               --  program_application_id
6735               sysdate);                           --  Program_update_date
6736 
6737 	   /*  Call the ato_item_Exp procedure to explode the Ato_item bill and
6738     	   Store the model,option class and option item demands             */
6739            lStmtNum := 50;
6740 
6741            IF PG_DEBUG <> 0 THEN
6742            	oe_debug_pub.add('create_cto_item_demand: ' || 'Calling ato_item_exp procedure...',2);
6743            END IF;
6744 
6745            ato_item_exp(myrec.inventory_item_id,
6746                      myrec.ship_from_org_id,
6747                      myrec.ordered_quantity,
6748                      myrec.schedule_ship_date,
6749                      myrec.line_id,
6750                      myrec.header_id,
6751                      x_return_status,
6752                      x_msg_count,
6753                      x_msg_data);
6754 
6755            IF x_return_status = FND_API.G_RET_STS_ERROR THEN
6756               IF PG_DEBUG <> 0 THEN
6760                             ||'and line_id = '||to_char(myrec.line_id),1);
6757               	oe_debug_pub.add('create_cto_item_demand: ' || 'Exp Error in ato_item_exp procedure for the item_id'
6758                             ||to_char(myrec.inventory_item_id)
6759                             ||' and org id...'||to_char(myrec.ship_from_org_id)
6761               END IF;
6762               raise FND_API.G_EXC_ERROR;
6763            ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6764               IF PG_DEBUG <> 0 THEN
6765               	oe_debug_pub.add('create_cto_item_demand: ' || 'Unexp Error in ato_item_exp procedure for the item_id'
6766                             ||to_char(myrec.inventory_item_id)
6767                             ||' and org id...'||to_char(myrec.ship_from_org_id)
6768                             ||' and line_id = '||to_char(myrec.line_id),1);
6769               END IF;
6770               raise FND_API.G_EXC_UNEXPECTED_ERROR;
6771            END IF;
6772        END IF;
6773     END LOOP;
6774     x_return_status := FND_API.G_RET_STS_SUCCESS;
6775 
6776 EXCEPTION
6777            WHEN FND_API.G_EXC_ERROR THEN
6778                ROLLBACK to create_cto_item_demand_begin;
6779                IF PG_DEBUG <> 0 THEN
6780                	oe_debug_pub.add('create_cto_item_demand: ' || ' CREATE_CTO_ITEM_DEMAND :: exp error::'|| to_char(lStmtNum)||'::'||sqlerrm,1);
6781                END IF;
6782                x_return_status := FND_API.G_RET_STS_ERROR;
6783                FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,'CREATE_CTO_MODEL_DEMAND');
6784                FND_MSG_PUB.Count_and_get(
6785                                          p_count => X_Msg_count,
6786                                          p_data  => X_Msg_Data);
6787 
6788            WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6789                ROLLBACK to create_cto_item_demand_begin;
6790                IF PG_DEBUG <> 0 THEN
6791                	oe_debug_pub.add('create_cto_item_demand: ' || ' CREATE_CTO_ITEM_DEMAND :: unexp error::'|| to_char(lStmtNum)||'::'||sqlerrm,1);
6792                END IF;
6793                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6794                IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6795                   FND_MSG_PUB.Add_Exc_Msg(
6796                                            G_PKG_NAME
6797                                          , 'CREATE_CTO_MODEL_DEMAND'
6798                                            );
6799                END IF;
6800 
6801                FND_MSG_PUB.Count_and_get(
6802                                          p_count => X_Msg_Count,
6803                                          p_data  => x_Msg_Data);
6804 
6805            WHEN OTHERS THEN
6806                ROLLBACK to create_cto_item_demand_begin;
6807                IF PG_DEBUG <> 0 THEN
6808                	oe_debug_pub.add('create_cto_item_demand: ' || ' CREATE_CTO_ITEM_DEMAND :: Other error ::'|| to_char(lStmtNum)||'::'||sqlerrm,1);
6809                END IF;
6810                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6811                IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6812                   FND_MSG_PUB.Add_Exc_Msg(
6813                                            G_PKG_NAME
6814                                          , 'CREATE_CTO_MODEL_DEMAND'
6815                                            );
6816                END IF;
6817 
6818                FND_MSG_PUB.Count_and_get(
6819                                          p_count => X_Msg_Count,
6820                                          p_data  => X_Msg_Data);
6821 END CREATE_CTO_ITEM_DEMAND;
6822 
6823 
6824 
6825 /************************************************************************/
6826 PROCEDURE ato_item_exp(p_item_id  mtl_system_items.inventory_item_id%type,
6827                        p_org_id   mtl_system_items.organization_id%type,
6828                        p_qty      number,
6829                        p_req_date date,
6830                        p_line_id  number,
6831                        p_header_id number,
6832                        x_return_status out varchar2,
6833                        x_msg_count out number,
6834                        x_msg_data  out varchar2) is
6835 lStmtNum                  Number;
6836 l_request_date            date;
6837 x_lead_time               number;
6838 x_source_org              number;
6839 x_ret_status              varchar2(1);
6840 l_fixed_lead_time         number;
6841 l_variable_lead_time      number;
6842 x_rule_exists             varchar2(1);
6843 v_source_type             number;
6844 
6845        cursor inven_comp is
6846        SELECT component_item_id,component_quantity
6847        FROM   bom_inventory_components bic,
6848               bom_bill_of_materials bom,
6849               mtl_system_items mtl
6850        WHERE  bom.assembly_item_id = p_item_id
6851        AND    bom.organization_id  = x_source_org
6852        AND    bom.bill_sequence_id = bic.bill_sequence_id
6853        AND    bic.bom_item_type    = 4
6854        AND    mtl.inventory_item_id = bic.component_item_id
6855        AND    mtl.organization_id   = x_source_org
6856        AND    mtl.base_item_id is not null;
6857 BEGIN
6858 
6859     IF PG_DEBUG <> 0 THEN
6860     	oe_debug_pub.add('ato_item_exp: ' || 'In ato_item_exp module...',1);
6861     	oe_debug_pub.add('ato_item_exp: ' || 'p_item_id    ='||to_char(p_item_id),2);
6862     	oe_debug_pub.add('ato_item_exp: ' || 'P_org_id     ='||to_char(p_org_id),2);
6866     cto_atp_interface_pk.get_model_sourcing_org(p_item_id,
6863     	oe_debug_pub.add('ato_item_exp: ' || 'Getting the sourcing information....',2);
6864     END IF;
6865 
6867                                                 p_org_id,
6868                                                 x_rule_exists,
6869                                                 x_source_org,
6870                                                 v_source_type,
6871                                                 x_lead_time,
6872                                                 x_ret_status);
6873 
6874     IF x_ret_status = FND_API.G_RET_STS_ERROR THEN
6875        IF PG_DEBUG <> 0 THEN
6876        	oe_debug_pub.add('ato_item_exp: ' || 'get_model_sourcing_org returned expected error',1);
6877        END IF;
6878        raise FND_API.G_EXC_ERROR;
6879     ELSIF x_ret_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6880        IF PG_DEBUG <> 0 THEN
6881        	oe_debug_pub.add('ato_item_exp: ' || 'Get_model_sourcing_org returned unexpected error',1);
6882        END IF;
6883        raise FND_API.G_EXC_UNEXPECTED_ERROR;
6884     END IF;
6885 
6886     IF PG_DEBUG <> 0 THEN
6887     	oe_debug_pub.add('ato_item_exp: ' || 'Source_flag = '||x_rule_exists,2);
6888     END IF;
6889 
6890     IF  x_rule_exists = 'F'  THEN
6891           x_source_org := p_org_id;
6892           x_lead_time  := 0;
6893     END IF;
6894 
6895         select nvl(fixed_lead_time,0),nvl(variable_lead_time,0)
6896          into   l_fixed_lead_time,l_variable_lead_time
6897          from   mtl_system_items
6898          where  inventory_item_id = p_item_id
6899          and    organization_id   = x_source_org;
6900          x_lead_time := nvl(x_lead_time,0) + l_fixed_lead_time +
6901                         l_variable_lead_time * p_qty;
6902 
6903 --       Calculate the offset date with CalENDer
6904          IF PG_DEBUG <> 0 THEN
6905          	oe_debug_pub.add('ato_item_exp: ' || 'Calculating calendar offset....',2);
6906          END IF;
6907 
6908 	 -- Bugfix 3189261 : Changing date offset call
6909 
6910         /*l_request_date := MSC_CALENDAR.DATE_OFFSET(
6911                              x_source_org,
6912                              ginstance_id,
6913                              1,
6914                              p_req_date,
6915                              x_lead_time);*/
6916 
6917          l_request_date := MSC_SATP_FUNC.src_date_offset (
6918                            x_source_org,
6919                            p_req_date,
6920                            x_lead_time);
6921 
6922          IF PG_DEBUG <> 0 THEN
6923          	oe_debug_pub.add('ato_item_exp: ' || 'Calling Create_demand module...',2);
6924          END IF;
6925 
6926          create_demand(p_item_id,
6927                        x_source_org,
6928                        l_request_date,
6929                        p_qty,
6930                        p_line_id,
6931                        p_header_id,
6932                        x_return_status,
6933                        x_msg_count,
6934                        x_msg_data);
6935 
6936          IF PG_DEBUG <> 0 THEN
6937          	oe_debug_pub.add('ato_item_exp: ' || 'After create_demand Module...',2);
6938          END IF;
6939 
6940          FOR myrec IN inven_comp
6941          LOOP
6942 
6943               IF PG_DEBUG <> 0 THEN
6944               	oe_debug_pub.add('ato_item_exp: ' || 'Inside child record loop...',2);
6945               END IF;
6946               ato_item_exp(myrec.component_item_id,
6947                            x_source_org,
6948                            p_qty*myrec.component_quantity,
6949                            l_request_date,
6950                            p_line_id,
6951                            p_header_id,
6952                            x_return_status,
6953                            x_msg_count,
6954                            x_msg_data);
6955          END LOOP;
6956 END ato_item_exp;
6957 
6958 
6959 
6960 PROCEDURE create_demand(p_item_id in number,
6961                         p_org_id in number,
6962                         p_req_date in date,
6963                         p_qty in number,
6964                         p_line_id in number,
6965                         p_header_id in number,
6966                         x_return_status out varchar2,
6967                         x_msg_count out number,
6968                         x_msg_data out varchar2)  is
6969 BEGIN
6970      guserid  := nvl(FND_GLOBAL.user_id,-1);
6971      gloginid := nvl(FND_GLOBAL.login_id,-1);
6972 /* First level model and Option class rows are inserted in to the bcod table.*/
6973           insert into
6974               bom_cto_order_demand(
6975               bcod_line_id,
6976               oe_line_id,
6977               ato_line_id,
6978               inventory_item_id,
6979               organization_id,
6980               required_date,
6981               required_qty,
6982               order_quantity_uom,
6983               parent_demand_type,
6984               header_id,
6985               forecast_visible,
6986               demand_visible,
6987               created_by,
6988               last_updated_by,
6989               last_updated_date,
6990               last_update_login,
6991               program_application_id,
6992               program_update_date)
6993         select
6994               BOM_CTO_ORDER_DEMAND_S1.nextval,      -- bcod_line_id
7000               Round( p_qty*bic.component_quantity, 6) ,-- required_qty /* Decimal-Qty Support for Option Items */
6995               p_line_id,                            -- oe_line_id
6996               p_line_id,                            -- ato_line_id
6997               bic.component_item_id,                -- inventory_item_id
6998               p_org_id,                             -- organization_id
6999               p_req_date,                           -- Required_date
7001               mtl.primary_uom_code,                 -- Order_quantity_uom
7002               2,                                    -- Parent_demand_type
7003               p_header_id,                          -- Header_id
7004               'Y',                                  -- Forecast_visible
7005               'N',                                  -- Demand_visible
7006               guserid,                              -- Created_by
7007               guserid,                              -- Last_updated_by
7008               sysdate,                              -- Last_updated_date
7009               gloginid,                             -- Last_update_login
7010               null,                                 -- Program_application_id
7011               sysdate
7012         From
7013               bom_inventory_components bic,
7014               bom_bill_of_materials bom,
7015               mtl_system_items mtl
7016         where bom.assembly_item_id = p_item_id
7017         and   bom.organization_id  = p_org_id
7018         and   bom.bill_sequence_id = bic.bill_sequence_id
7019         and   bic.bom_item_type in (1,2)
7020         and   mtl.inventory_item_id= bic.component_item_id
7021         and   mtl.organization_id  = p_org_id;
7022 /* Insert the option items for the first level models and option classes */
7023           insert into
7024               bom_cto_order_demand(
7025               bcod_line_id,
7026               oe_line_id,
7027               ato_line_id,
7028               inventory_item_id,
7029               organization_id,
7030               required_date,
7031               required_qty,
7032               order_quantity_uom,
7033               parent_demand_type,
7034               header_id,
7035               forecast_visible,
7036               demand_visible,
7037               created_by,
7038               last_updated_by,
7039               last_updated_date,
7040               last_update_login,
7041               program_application_id,
7042               program_update_date)
7043    select
7044               BOM_CTO_ORDER_DEMAND_S1.nextval,      -- bcod_line_id
7045               p_line_id,                            -- oe_line_id
7046               p_line_id,                            -- ato_line_id
7047               bic.component_item_id,                -- inventory_item_id
7048               p_org_id,                             -- organization_id
7049               p_req_date,                           -- Required_date
7050               Round( p_qty*bic.component_quantity, 6) ,-- required_qty /* Decimal-Qty Support for Option Items */
7051               mtl.primary_uom_code,                 -- Order_quantity_uom
7052               2,                                    -- Parent_demand_type
7053               p_header_id,                          -- Header_id
7054               'Y',                                  -- Forecast_visible
7055               'N',                                  -- Demand_visible
7056               guserid,                              -- Created_by
7057               guserid,                              -- Last_updated_by
7058               sysdate,                              -- Last_updated_date
7059               gloginid,                             -- Last_update_login
7060               null,                                 -- Program_application_id
7061               sysdate
7062    from   bom_inventory_components bic,
7063           bom_bill_of_materials bom,
7064           mtl_system_items      mtl
7065    where  bom.assembly_item_id = p_item_id
7066    and    bom.organization_id =  p_org_id
7067    and    bom.bill_sequence_id = bic.bill_sequence_id
7068    and    bic.bom_item_type = 4
7069    and    bic.component_item_id  in (
7070           select
7071           bic2.component_item_id
7072    from   bom_bill_of_materials bom1,
7073           bom_bill_of_materials bom2,
7074           bom_inventory_components bic1,
7075           bom_inventory_components bic2
7076    where  bom1.assembly_item_id = p_item_id
7077    and    bom1.organization_id  = p_org_id
7078    and    bom1.bill_sequence_id = bic1.bill_sequence_id
7079    and    bic1.bom_item_type     in(1, 2)
7080    and    bom2.assembly_item_id = bic1.component_item_id
7081    and    bom2.organization_id = p_org_id
7082    and    bic2.bill_sequence_id = bom2.bill_sequence_id
7083    and    bic2.bom_item_type  = 4
7084    and    bic2.optional = 1)
7085    and    mtl.inventory_item_id  = bic.component_item_id
7089 
7086    and    mtl.organization_id    = p_org_id;
7087 END create_demand;
7088 
7090 
7091 /****************************************************************************
7092 
7093            For the given item and organization id this procedure will find
7094            whether it is sourced or not.
7095 
7096 *****************************************************************************/
7097 PROCEDURE is_item_sourced(
7098                         p_item_id in number,
7099                         p_org_id in number,
7100                         x_source_flag   out varchar2,
7101                         x_return_status out varchar2,
7102                         x_msg_count     out number,
7103                         x_msg_data      out varchar2)    is
7104 
7105         cursor atoitem is
7106                select component_item_id
7107                from   bom_bill_of_materials bom,
7108                       bom_inventory_components bic,
7109                       mtl_system_items mtl
7110                where  bom.assembly_item_id = p_item_id
7111                and    bom.organization_id  = p_org_id
7112                and    bom.bill_sequence_id = bic.bill_sequence_id
7113                and    bic.bom_item_type = 4
7114                and    mtl.inventory_item_id = bic.component_item_id
7115                and    mtl.organization_id   = p_org_id
7116                and    mtl.base_item_id      is not null;
7117                x_rule_exists              varchar2(1);
7118                x_source_org               number;
7119                x_lead_time                number;
7120                x_ret_status               varchar2(1);
7121                v_source_type              number;
7122 BEGIN
7123 
7124          IF PG_DEBUG <> 0 THEN
7125          	oe_debug_pub.add('is_item_sourced: ' || 'Entering is_item_sourced...',2);
7126          END IF;
7127 
7128          x_source_flag := 'N';
7129 
7130          IF PG_DEBUG <> 0 THEN
7131          	oe_debug_pub.add('is_item_sourced: ' || 'P_item_id    = '||to_char(p_item_id), 4);
7132          	oe_debug_pub.add('is_item_sourced: ' || 'p_org_id     = '||to_char(p_org_id), 4);
7133          END IF;
7134 
7135          cto_atp_interface_pk.get_model_sourcing_org(p_item_id,
7136                                                      p_org_id,
7137                                                      x_rule_exists,
7138                                                      x_source_org,
7139                                                      v_source_type,
7140                                                      x_lead_time,
7141                                                      x_ret_status);
7142 
7143          IF x_ret_status = FND_API.G_RET_STS_ERROR THEN
7144            IF PG_DEBUG <> 0 THEN
7145            	oe_debug_pub.add('is_item_sourced: ' || 'get_model_sourcing_org returned expected error',1);
7146            END IF;
7147            raise FND_API.G_EXC_ERROR;
7148          ELSIF x_ret_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7149            IF PG_DEBUG <> 0 THEN
7150            	oe_debug_pub.add('is_item_sourced: ' || 'Get_model_sourcing_org returned unexpected error',1);
7151            END IF;
7152            raise FND_API.G_EXC_UNEXPECTED_ERROR;
7153          END IF;
7154 
7155          IF PG_DEBUG <> 0 THEN
7156          	oe_debug_pub.add('is_item_sourced: ' || 'source_flag  ='||x_rule_exists, 4);
7157          END IF;
7158 
7159          IF x_rule_exists = 'T' THEN
7160              x_source_flag := 'Y';
7161          ELSE
7162             FOR myrec IN atoitem
7163             LOOP
7164                 is_item_sourced( myrec.component_item_id,
7165                                x_source_org,
7166                                x_source_flag,
7167                                x_return_status,
7168                                x_msg_count,
7169                                x_msg_data);
7170                 IF x_source_flag = 'Y' THEN
7171                       x_source_flag := 'Y';
7172                       exit;
7173                 END IF;
7174             END LOOP;
7175          END IF;
7176 END is_item_sourced;
7177 
7178 
7179 PROCEDURE initialize_session_globals
7180 IS
7181 BEGIN
7182 
7183       g_cto_shipset.delete ;
7184       gUserId := null ;
7185       gLoginId := null ;
7186       g_final_cto_shipset.delete ;
7187       g_cto_sparse_shipset.delete ;
7188       g_shipset_status_tbl.delete ;
7189       local_cto_shipset.delete ;
7190       g_auto_generated_shipset.delete;
7191       g_shipset := null  ;
7195 
7192       g_final_shipset := null ;
7193 
7194 END initialize_session_globals ;
7196 
7197 PROCEDURE initialize_assignment_set ( x_return_status out varchar2 )
7198 IS
7199 BEGIN
7200 
7201    x_return_status := FND_API.G_RET_STS_SUCCESS ;
7202 
7203    /*
7204    ** get MRP's default assignment set
7205    */
7206    g_stmt_num := 1 ;
7207    BEGIN
7208 
7209    gMrpAssignmentSet := to_number(FND_PROFILE.VALUE('MRP_DEFAULT_ASSIGNMENT_SET'));
7210    EXCEPTION
7211    WHEN others THEN
7212     raise invalid_mrp_assignment_set ;
7213    END ;
7214 
7215     g_stmt_num := 5 ;
7216 
7217    IF( gMrpAssignmentSet is null )
7218    THEN
7219                 IF PG_DEBUG <> 0 THEN
7220                 	oe_debug_pub.add('initialize_assignment_set: ' || '**$$ Default assignment set is null',  1);
7221                 END IF;
7222 
7223    ELSE
7224                 IF PG_DEBUG <> 0 THEN
7225                 	oe_debug_pub.add('initialize_assignment_set: ' || '**Default assignment set is '||to_char(gMrpAssignmentSet),1);
7226                 END IF;
7227 
7228                 g_stmt_num := 10 ;
7229 
7230                 BEGIN
7231 
7232                 select assignment_set_name into gMrpAssignmentSetName
7233                    from mrp_Assignment_sets
7234                 where assignment_set_id = gMrpAssignmentSet ;
7235 
7236                 EXCEPTION
7237                    WHEN no_data_found THEN
7238                        IF PG_DEBUG <> 0 THEN
7239                        	oe_debug_pub.add('initialize_assignment_set: ' ||  '**The assignment set pointed by the profile MRP_DEFAULT_ASSIGNMENT_SET does not exist in the database ' ,1);
7240                        END IF;
7241 
7242                        RAISE INVALID_MRP_ASSIGNMENT_SET ;
7243 
7244                    WHEN others THEN
7245                        RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
7246                 END ;
7247 
7248                 IF PG_DEBUG <> 0 THEN
7249                 	oe_debug_pub.add('initialize_assignment_set: ' || 'Default assignment set name is '|| gMrpAssignmentSetName ,1);
7250                 END IF;
7251    END IF;
7252 
7253 EXCEPTION
7254    WHEN INVALID_MRP_ASSIGNMENT_SET THEN
7255         x_return_status := FND_API.G_RET_STS_ERROR;
7256 
7257         IF PG_DEBUG <> 0 THEN
7258         	oe_debug_pub.add('initialize_assignment_set: ' || 'INITIALIZE_ASSIGNMENT_SET::INVALID ASSIGNMENT SET ::'||to_char(g_stmt_num)||'::'||sqlerrm,1);
7259         END IF;
7260 
7261    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7262         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7263         IF PG_DEBUG <> 0 THEN
7264         	oe_debug_pub.add('initialize_assignment_set: ' || 'INITIALIZE_ASSIGNMENT_SET::unexp error::'||to_char(g_stmt_num)||'::'||sqlerrm,1);
7265         END IF;
7266 
7267    WHEN OTHERS THEN
7268         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7269         IF PG_DEBUG <> 0 THEN
7270         	oe_debug_pub.add('initialize_assignment_set: ' || 'INITIALIZE_ASSIGNMENT_SET::others::'||to_char(g_stmt_num)||'::'||sqlerrm,1);
7271         END IF;
7272 
7273 END initialize_assignment_set ;
7274 
7275 
7276 
7277 END CTO_ATP_INTERFACE_PK;