DBA Data[Home] [Help]

PACKAGE BODY: APPS.CTO_OSS_SOURCE_PK

Source


1 PACKAGE BODY CTO_OSS_SOURCE_PK AS
2 /*$Header: CTOOSSPB.pls 120.11 2011/12/28 10:58:38 abhissri ship $ */
3 /*============================================================================+
4 |  Copyright (c) 1999 Oracle Corporation    Belmont, California, USA          |
5 |                        All rights reserved.                                 |
6 |                        Oracle Manufacturing                                 |
7 +=============================================================================+
8 |                                                                             |
9 | FILE NAME   : CTOOSSPB.pls                                                  |
10 | DESCRIPTION:                                                                |
11 |               Contains code for Option spefic sourcing processing. This     |
12 |               Pkg. contains two main functional code areas. One is called   |
13 |               During ATP to get the OSS orgs list. The other is called      |
14 |               during Auto create config process.                            |
15 |               Requisitions.						      |
16 |               This Package creates the following                            |
17 |               Procedures                                                    |
18 |               1. AUTO_CREATE_PUR_REQ_CR                                     |
19 |               2. POPULATE_REQ_INTERFACE                                     |
20 |               Functions                                                     |
21 |               1. GET_RESERVED_QTY                                           |
22 |               2. GET_NEW_ORDER_QTY                                          |
23 | HISTORY     :                                                               |
24 | 25-Aug-2003 : Renga Kannan          Initial version                         |
25 | 02-Oct-2003 : Renga Kannan          Modified all the code with the          |
26 |                                     New re-design                           |
27 | 14-Nov-2003 : Renga Kannan          Fixed all the bugs that are identified  |
28 |                                     during all the demos.                   |
29 | 02-MAR-2004 : Sushant Sawant        Fixed Bug 3472654 queries against       |
30 |                                      bcol_gt should be limited by           |
31 |				      ato_line_id as same config item         |
32 |				      could exist on multiple orders.         |
33 | 09-16-2004    Kiran Konada          bugfix3894241
34 |                                     used to_number( ) fn on NULL , for
35 |                                     8i compatability
36 |
37 | 09-22-2004    Kiran Konada          bugfix3891572
38 |                                     10G compatbility issue
39 |                                     always need to initialize nested table
40 | 12-02-2004    Renga Kannan          Bug Fix 3896824. added Special validation
41 |                                     for pre configuration case
42 | 18-APR-2005   Renga Kannan          Bug Fix 4112373.
43 |                                     Fixed 100% Sourcing rule creation in
44 |                                     prune_parent_oss procedure. 100% make at
45 |                                     rules were not getting created when the model
46 |                                     has a buy sourcing rule. Nvl caluse was missing
47 |                                     in the subquery. The bug is fixed.
48 |
49 | 26-Apr-2005   Renga Kannan          Fixed bug 4093235
50 |                                     OSS processing for multi level model with lower level
51 |                                     matched CIB 3 maodel was giving 'Invalid ship from org'
52 |                                     message all the time. This is due to code issues
53 |                                     in prune_parent_oss_config and get_order_sourcing_data
54 |                                     procedure. Fixed the issue.
55 |
56 =============================================================================*/
57 
58 g_pkg_name     CONSTANT  VARCHAR2(30) := 'CTO_OSS_SOURCE_PK';
59 
60 
61 /* This is the Package constant that is used to have indented debug logging */
62 PG_DEBUG Number := NVL(FND_PROFILE.value('ONT_DEBUG_LEVEL'), 0);
63 
64 
65 /* Forward declartion for the procedure.
66    This procedure is used during ATP call. This will get the organization
67    and vendors from the sourcing rule for OSS ato item.
68 */
69 
70 Procedure get_ato_item_orgs(
71                             p_assignment_id  IN  Number,
72                             x_return_status  OUT NOCOPY varchar2,
73                             x_msg_count      OUT NOCOPY Number,
74                             x_msg_data       OUT NOCOPY Varchar2
75                            );
76 
77 /*
78    Forward declartion for the procedure.
79    This procedure is used during ATP call. This will get the OSS orgs and
80    Vendors by processing the configuration for OSS.
81 */
82 
83 Procedure get_configurations_org(
84                                  x_return_status  OUT NOCOPY Varchar2,
85                                  x_msg_count      OUT NOCOPY Number,
86                                  x_msg_data       OUT NOCOPY Varchar2
87                                 );
88 
89 /*
90    Forward declartion for the procedure.
91    This procedure is used during Parent OSS pruning.
92    This will get called in the core OSS processing logic.
93    This will identify all the parent models for a oss configuration.
94 */
95 
96 Procedure update_parent_oss_line(p_parent_ato_line_id  In  Number,
97                                  x_return_status       OUT NOCOPY Varchar2,
98 				 x_msg_count           OUT NOCOPY Number,
99 				 x_msg_data            OUT NOCOPY Varchar2
100                                 );
101 
102 
103 /*
104 
105   Forward declartion for the procedure.
106   This is the core API to prune the sourcing rules for the OSS configuration.
107   This API will get called during ATP as well as Auto Create config.
108   This API will identify all the sourcing rules and prune them according
109   to the oss orgs/vendors list provided.
110 
111 */
112 
113 Procedure prune_oss_config(
114                            p_model_line_id  IN  Number,
115                            p_model_item_id  IN  Number,
116 			   p_config_item_id IN  Number,
117 			   p_calling_mode   IN  Varchar2,
118                            p_ato_line_id    IN  Number,
119 			   x_exp_error_code OUT NOCOPY Number,
120                            x_return_status  OUT NOCOPY Varchar2,
121                	           x_msg_count      OUT NOCOPY Number,
122               		   x_msg_data       OUT NOCOPY Varchar2
123 			  );
124 
125 /*
126 
127   Forward declartion for the procedure.
128   This is the core API to prune the Parent of OSS configuration.
129   This API will get called during ATP as well as Auto Create config process.
130   This API will get the valid orgs by looking at all the OSS child
131   and purne the sourcing tree accordingly.
132 
133 */
134 
135 Procedure prune_parent_oss_config(
136                                   p_model_line_id  IN  Number,
137 				  p_model_item_id  IN  Number,
138 				  p_calling_mode   IN  Varchar2,
139                                   p_ato_line_id    IN  Number,
140 				  x_exp_error_code OUT NOCOPY Number,
141 	                          x_return_status  OUT NOCOPY Varchar2,
142    		                  x_msg_count      OUT NOCOPY Number,
143 			          x_msg_data       OUT NOCOPY Varchar2
144 				 );
145 
146 /*
147 
148   Forward declartion for the procedure.
149   This API is used during Parent OSS pruning process.
150   This is more of a utility API to traverse the source
151   tree and update the valid nodes.
152 
153 */
154 
155 
156 
157 Procedure  update_Source_tree(p_line_id       IN Number,
158                               p_end_org       IN  Number,
159                               x_return_status OUT NOCOPY Varchar2,
160                               x_msg_data      OUT NOCOPY varchar2,
161                               x_msg_count     OUT NOCOPY Number
162                              );
163 
164 Procedure prune_item_level_rule(p_model_line_id   IN  Number,
165                                 p_model_item_id   IN  Number,
166 				x_rule_exists     OUT NOCOPY Varchar2,
167 				x_return_status   OUT NOCOPY Varchar2,
168 				x_msg_count       OUT NOCOPY Number,
169 				x_msg_data        OUT NOCOPY varchar2
170 			       );
171 
172 Procedure Find_leaf_node( p_model_line_id   IN  Number,
173                           p_source_org_id   IN  Number,
174 			  p_rcv_org_id      IN  Number,
175 			  x_return_status   OUT NOCOPY Varchar2,
176 			  x_msg_data        OUT NOCOPY Varchar2,
177 			  x_msg_count       OUT NOCOPY Number);
178 
179 
180 Procedure    Traverse_up_tree(p_model_line_id  IN  Number,
181                               p_source_org_id  IN  Number,
182 		              p_valid_flag     IN  Varchar2,
183 		              x_return_status  OUT NOCOPY Varchar2,
184 		              x_msg_count      OUT NOCOPY Varchar2,
185 		              x_msg_data       OUT NOCOPY Number);
186 
187 
188 
189 
190 
191 TYPE Number1_arr is TABLE of Number;
192 TYPE Varchar1_arr is TABLE of Varchar2(1);
193 
194 TYPE assg_rec is RECORD (assignment_id   Number1_arr := Number1_arr(),
195                          line_id         Number1_arr := Number1_arr()
196 			 );
197 
198 
199 
200 TYPE parent_ato_rec_type is RECORD  (
201 		                      line_id	       Number1_arr := number1_arr(),--bugfix3891572
202 			   	      option_specific  varchar1_arr := varchar1_arr()--bugfix3891572
203 			            );
204 
205 TYPE bcol_rec_type is RECORD  (
206                                line_id              Number,
207 		               parent_ato_line_id   Number,
208          	   	       ato_line_id          Number,
209               		       option_specific      Varchar2(1),
210 			       perform_match        Varchar2(1)
211 		              );
212 
213 --Bugfix 9148706: Indexing by LONG
214 --TYPE bcol_tbl_type is TABLE OF bcol_rec_type INDEX BY Binary_integer;
215 TYPE bcol_tbl_type is TABLE OF bcol_rec_type INDEX BY LONG;
216 
217 
218 Procedure get_sourcing_data(
219                             p_ato_line_id     IN  Number,
220 			    x_return_status   OUT NOCOPY Varchar2,
221 			    x_msg_data        OUT NOCOPY Varchar2,
222 			    x_msg_count       OUT NOCOPY Number);
223 
224 Procedure Process_order_for_oss (P_ato_line_id    IN  Number,
225                                  p_calling_mode   IN  Varchar2,
226                                  x_return_status  OUT NOCOPY Varchar2,
227 				 x_msg_data       OUT NOCOPY Varchar2,
228 				 x_msg_count      OUT NOCOPY Number);
229 
230 Procedure COPY_TO_BCOL_TEMP(
231                             p_ato_line_id   IN  Number,
232 			    x_return_status OUT NOCOPY Varchar2,
233 			    x_msg_data      OUT NOCOPY Varchar2,
234 			    x_msg_count     OUT NOCOPY Number);
235 Procedure Traverse_sourcing_chain(
236                                 p_item_id         IN    Number,
237                                 p_org_id          IN    Number,
238                                 p_line_id         IN    Number,
239                                 p_option_specific IN    Varchar,
240                                 p_ato_line_id     IN    Number, /* Renga Kannan
241 */
242                                 x_assg_list       IN OUT NOCOPY assg_rec,
243                                 x_return_status   OUT  NOCOPY  Varchar2,
244                                 x_msg_data        OUT  NOCOPY  Varchar2,
245                                 x_msg_count       OUT  NOCOPY  Varchar2);
246 
247 
248  Procedure Get_order_sourcing_data(
249                                 p_ato_line_id   IN   Number,
250                                 x_return_status OUT NOCOPY  Varchar2,
251                                 x_msg_count     OUT NOCOPY  Number,
252                                 x_msg_data      OUT NOCOPY  Varchar2);
253 
254  -- bug 13362916
255  /*
256  Procedure Print_source_gt(
257                            p_line_id   IN Number);
258 
259  Procedure Print_orglist_gt(p_line_id  IN Number);
260  */
261 
262 Procedure Print_source_gt;
263 Procedure Print_orglist_gt;
264 
265 TYPE g_assg_list_type is TABLE of Number index by binary_integer;
266 
267 g_assg_list             g_assg_list_type;
268 G_bcol_tbl		bcol_tbl_type;
269 G_parent_rec            parent_ato_rec_type;
270 G_tbl_index		Number :=1;
271 G_def_assg_set          Number;
272 
273 g_pg_level              Number;
274 
275 
276 --
277 -- Declaring a Stack data structure to catch circular sourcing
278 --
279 
280 Type source_org_stk is Table of number index by Binary_integer;
281 
282 G_Source_org_stk    Source_org_stk;
283 
284 --Bugfix 8894392
285 procedure del_from_msa (p_config_id in number)
286 is
287      pragma autonomous_transaction;
288 begin
289 
290      delete from mrp_sr_assignments
291      where assignment_set_id = G_def_assg_set
292      and inventory_item_id = p_config_id;
293 
294      If PG_DEBUG <> 0 Then
295         oe_debug_pub.add('DEL_FROM_MSA: Rows deleted =' || sql%rowcount,5);
296      End if;
297 
298      commit;
299 end del_from_msa;
300 --Bugfix 8894392
301 
302 /*
303 
304   This is procedure is called during Auto Create Config item Process. This will process
305   all oss configurations and give the list of orgs where bom should be created.
306 
307 */
308 
309 
310 Procedure Process_Oss_configurations(
311                  p_ato_line_id   IN           Number,
312                  p_mode          IN           Varchar2 DEFAULT 'ACC',
313                  x_return_status OUT   NOCOPY Varchar2,
314                  x_msg_count     OUT   NOCOPY Number,
315                  x_msg_data      OUT   NOCOPY Varchar) is
316 
317 
318 
319    l_perform_match		bom_cto_order_lines.perform_match%type;
320    l_reuse_config               bom_cto_order_lines.reuse_config%type;
321    l_oss_defined                Varchar2(1);
322    i				Number;
323    x_exp_error_code             Number;
324    l_stmt_num                   Number;
325    l_config_creation            Varchar2(1);
326    x_oss_exists                 Varchar2(1);
327    l_valid_ship_from_org        Varchar2(1);
328    l_option_specific            varchar2(1);
329 
330    /* This will get all the valid leaf nodes in the pruned tree to construct the
331       out variable.
332    */
333 
334 
335   l_count			number;
336 
337   -- Added by Renga Kannan on 11/30/04 for bug 3896824
338   -- Added the following variable declaration
339 
340   l_ship_from_org_id		oe_order_lines.ship_from_org_id%type;
341   l_program_id                  bom_cto_order_lines.program_id%type;
342   l_valid_preconfig_org         varchar2(1);
343 
344   -- End of change for bug 3896824 on 11/30/04
345 
346 BEGIN
347 
348 
349    oe_debug_pub.add('=========================================================================',1);
350    oe_debug_pub.add('                                                                         ',1);
351    oe_debug_pub.add('             START OPTION SPECIFIC SOURCE PROCESSING                     ',1);
352    oe_debug_pub.add('                                                                         ',1);
353    oe_debug_pub.add('             START TIME STAMP : '||to_char(sysdate,'hh:mi:ss')||'        ',1);
354    oe_debug_pub.add('                                                                         ',1);
355    oe_debug_pub.add('=========================================================================',1);
356 
357    g_pg_level := 3;
358    x_return_status := FND_API.G_RET_STS_SUCCESS;
359    l_Stmt_num := 10;
360 
361    If PG_DEBUG <> 0 Then
362       oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGIRATIONS: Insise PROCESS_OSS_CONFIGURATION API',5);
363    End if;
364 
365    /* Get the default assignment set into a global variable.
366       The global variable will be used in all other modules later
367    */
368 
369    l_stmt_num := 20;
370    G_def_assg_set := to_number(FND_PROFILE.VALUE('MRP_DEFAULT_ASSIGNMENT_SET'));
371 
372    IF PG_DEBUG <> 0 Then
373        oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: Default Assignment set = '
374                                             ||to_char(g_def_assg_set),5);
375    End if;
376 
377 
378    /* If the default assignment set is null, then nothing to process. We will return
379    */
380 
381    If g_def_assg_set is null Then
382       IF PG_DEBUG <> 0 Then
383          oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: Default assignment set is null',5);
384          oe_debug_pub.add('=========================================================================',1);
385          oe_debug_pub.add('                                                                         ',1);
386          oe_debug_pub.add('               END OPTION SPECIFIC SOURCE PROCESSING                     ',1);
387          oe_debug_pub.add('                                                                         ',1);
388          oe_debug_pub.add('               END TIME STAMP : '||to_char(sysdate,'hh:mi:ss')||'        ',1);
389          oe_debug_pub.add('                                                                         ',1);
390          oe_debug_pub.add('=========================================================================',1);
391       End if;
392       return;
393    End if;
394 
395 
396    /* Check to see if there is any option specific sourcing is defined in setup.
397       If option specific sourcing is not defined, nothing to be done
398    */
399    l_stmt_num := 30;
400    Begin
401       select 'Y'
402       into   l_oss_defined
403       from   dual
404       where  exists (select 'x'
405                     from   bom_cto_oss_components);
406    Exception when no_data_found then
407       l_oss_defined := 'N';
408    end;
409 
410    If l_oss_defined = 'N' Then
411       oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: No Option Specific Soucing setup exists in the system',5);
412       return;
413    End if;
414 
415    /* Check to see if the whole configuration is matched or not.
416       If the whole config is matched/Re-used, OSS processing is not
417       required to do anything for that configuration.
418    */
419 
420    /* Impact: Check to see if the item_bom_creation attribute is set to '3'.
421               only if the attribute is set to 3, we should not do anything.
422 	      In case, if the attribute is set to */
423 
424 
425    l_stmt_num := 35;
426    If PG_DEBUG <> 0 Then
427      oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIOS: CAlling mode = '||p_mode,5);
428    end if;
429 
430    If p_mode = 'UPG' then
431       copy_to_bcol_temp(
432                      p_ato_line_id    => p_ato_line_id,
433                      x_return_status  => x_return_status,
434                      x_msg_data       => x_msg_data,
435                      x_msg_count      => x_msg_count);
436    End if;
437 
438    l_stmt_num := 40;
439 
440    /* Changed the bom_cto_order_lines reference to bom_cto_order_lines_gt */
441 
442    -- Modified by Renga Kannan on 11/30/04 for bug 3896824
443    -- fetched two other columns ship_from_org_id and program_id into the variables
444    -- l_ship_from_org_id and l_program_id
445 
446    Select
447           nvl(perform_match,'N'),
448           nvl(reuse_config,'N'),
449           config_creation,
450 	  ship_from_org_id,
451 	  program_id
452    into   l_perform_match,
453           l_reuse_config,
454 	  l_config_creation,
455 	  l_ship_from_org_id,
456 	  l_program_id
457    from   bom_cto_order_lines_gt bcol
458    where  line_id = p_ato_line_id;
459 
460    -- End of change for bug 3896824 on 11/30/04
461 
462    IF PG_DEBUG <> 0 THEN
463       oe_debug_pub.add(lpad(' ',g_pg_level)|| 'PROCESS_OSS_CONFIGURATIONS: l_perform_match:'   || l_perform_match,3);
464       oe_debug_pub.add(lpad(' ',g_pg_level)|| 'PROCESS_OSS_CONFIGURATIONS: l_reuse_config:'    || l_reuse_config,3);
465       oe_debug_pub.add(lpad(' ',g_pg_level)|| 'PROCESS_OSS_CONFIGURATIONS: l_config_creation:' || l_config_creation,3);
466       oe_debug_pub.add(lpad(' ',g_pg_level)|| 'PROCESS_OSS_CONFIGURATIONS: l_ship_from_org_id:'|| l_ship_from_org_id,3);
467       oe_debug_pub.add(lpad(' ',g_pg_level)|| 'PROCESS_OSS_CONFIGURATIONS: l_program_id:'      || l_program_id,3);
468    END IF;
469 
470    if p_mode = 'ACC' then
471    -- Added this if as part of Bugfix 8894392. Because of this bypass, table bcos_gt is not populated.
472    -- For upgrade part, bcmo is deleted and recreated. Data in these two tables is used to figure out
473    -- the orgs where the BOM should get created. If there is no data in bcos_gt, data for BOM
474    -- doesn't get pruned for OSS resulting in config BOM getting created in all orgs irrespective of OSS settings.
475 
476    l_stmt_num := 41;
477 
478    If (l_perform_match = 'Y' or l_reuse_config ='Y') and l_config_creation = 3 Then
479 
480       -- Fixed the bug on 02/11/04
481       -- If the top most item is matched, we don't need to execute the whole
482       -- algorithm, Still we need to validate the ship from org. Adding the validation part here
483 
484       Begin
485 
486       /* Fixed the following sql to get the from orbitrary org istead of
487          going to specific ship from org */
488 
489       select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
490              option_specific_sourced
491       into   l_option_specific
492       from   mtl_system_items msi,
493              bom_cto_order_lines_gt bcol
494       where msi.inventory_item_id = bcol.config_item_id
495       and   line_id = p_ato_line_id
496       and   rownum =1; /* Bugfix 3472654 */
497 
498       if l_option_specific is not null then
499       select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
500              'Y'
501       into   l_valid_ship_from_org
502       from   bom_cto_order_lines_gt bcol,
503              mtl_system_items msi
504       where  line_id = p_ato_line_id
505       and    msi.inventory_item_id = bcol.config_item_id
506       and    msi.organization_id   = bcol.ship_from_org_id
507       and    msi.option_specific_sourced is not null
508       and   bcol.ship_from_org_id in
509              (select assg.organization_id
510 	      from   mrp_sr_assignments assg,
511 	             mrp_sr_receipt_org rcv,
512 		     mrp_sr_source_org  src
513 	      where  assg.inventory_item_id = bcol.config_item_id
514 	      and    assg.sourcing_rule_id = rcv.sourcing_rule_id
515 	      and    rcv.effective_date <= sysdate
516               and    nvl(rcv.disable_date,sysdate+1)>sysdate
517               and    rcv.SR_RECEIPT_ID = src.sr_receipt_id
518 	      union
519 	      select src.source_organization_id
520 	      from   mrp_sr_assignments assg,
521 	             mrp_sr_receipt_org rcv,
522 		     mrp_sr_source_org  src
523 	      where  assg.inventory_item_id = bcol.config_item_id
524 	      and    assg.sourcing_rule_id = rcv.sourcing_rule_id
525 	      and    rcv.effective_date <= sysdate
526               and    nvl(rcv.disable_date,sysdate+1)>sysdate
527               and    rcv.SR_RECEIPT_ID = src.sr_receipt_id);
528       End if;
529       Exception When no_data_found then
530          oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: Ship from org is not valid',1);
531          l_valid_ship_from_org := 'N';
532          CTO_MSG_PUB.cto_message('BOM','CTO_OSS_INVALID_SHIP_ORG');
533          raise FND_API.G_EXC_ERROR;
534       End;
535 
536       -- Added by Renga Kannan on 12/02/04 for bug # 3896824
537       -- The following validation is added for this bug.
538       -- If the top most model is matched for pre configuration case
539       -- we need to validate the pre config org is part of the manufacturing
540       -- or procuring org. This validation is added here
541      If l_option_specific is not null and
542         l_program_id = CTO_UTILITY_PK.PC_BOM_PROGRAM_ID Then
543         Begin
544            Select 'x'
545            into   l_valid_preconfig_org
546            from   bom_cto_order_lines_gt bcol,
547                   mtl_system_items msi
548            where  line_id = p_ato_line_id
549            and    msi.inventory_item_id = bcol.config_item_id
550            and    msi.organization_id   = bcol.ship_from_org_id
551            and    msi.option_specific_sourced is not null
552            and    bcol.ship_from_org_id in
553                    (select assg.organization_id org_id
554 	            from   mrp_sr_assignments assg,
555 	                   mrp_sr_receipt_org rcv,
556 		           mrp_sr_source_org  src
557 	            where  assg.inventory_item_id = bcol.config_item_id
558 	            and    assg.sourcing_rule_id = rcv.sourcing_rule_id
559 	            and    rcv.effective_date <= sysdate
560                     and    nvl(rcv.disable_date,sysdate+1)>sysdate
561                     and    rcv.SR_RECEIPT_ID = src.sr_receipt_id
562 	            and    src.source_type in (2,3)
563 	            union
564 	            select src.source_organization_id org_id
565 	            from   mrp_sr_assignments assg,
566 	                   mrp_sr_receipt_org rcv,
567 		           mrp_sr_source_org  src
568 	            where  assg.inventory_item_id = bcol.config_item_id
569 	            and    assg.sourcing_rule_id = rcv.sourcing_rule_id
570 	            and    rcv.effective_date <= sysdate
571                     and    nvl(rcv.disable_date,sysdate+1)>sysdate
572                     and    rcv.SR_RECEIPT_ID = src.sr_receipt_id
573 	            and    src.source_organization_id not in
574 	                 (Select assg.organization_id
575 		          from   mrp_sr_assignments assg,
576 		                 mrp_sr_receipt_org rcv,
577 			         mrp_sr_source_org  src
578 		          Where  assg.inventory_item_id = bcol.config_item_id
579 		          and    assg.sourcing_rule_id   = rcv.sourcing_rule_id
580 		          and    rcv.effective_date <=sysdate
581 		          and    nvl(rcv.disable_date,sysdate+1)>sysdate
582 		          and    rcv.sr_receipt_id = src.sr_receipt_id
583 		         )
584 	          );
585          if PG_DEBUG <> 0 then
586             oe_debug_pub.add(lpad(' ',g_pg_level)||
587 	                    'PROCESS_OSS_CONFIGURATIONS: Preconfiguration org is a valid manufacturing or Procuring org',3);
588          End if;
589          Exception when no_data_found then
590             if PG_DEBUG <> 0 then
591 	       oe_debug_pub.add(lpad(' ',g_pg_level)||
592 	                        'PROCESS_OSS_CONFIGURATIONS: Preconfiguration org is not a valid manufacturing or Procuring org',1);
593                CTO_MSG_PUB.cto_message('BOM','CTO_OSS_INVALID_PC_ORG');
594                raise FND_API.G_EXC_ERROR;
595 	    End if;
596          End;
597       End if;
598 
599       /* Modified by Renga Kannan on 03/16/2006 for bug #:4368474
600          If the top model is matched to a config , which is a oss cofig and
601 	 the CIB attribute for the config is 3, then The OSS API just returns the
602 	 call as it does not need to do anything more.
603 
604 	 But we don't update the bcol date with the oss flag. As we have not updated
605 	 the oss flag, later part of the program is copying the sourcing rule from model
606 	 assuming that this is not a oss config. To avoid this issue, we will flag all
607 	 the matched config with its oss value from mtl_system_items to bcol so that
608 	 we won't have this issue.
609 
610       */
611 
612       /* Commenting out this update sql as part of bugfix 8894392(FP:7520529).
613       update bom_cto_order_lines_gt bcolgt
614       set    option_specific = (select  option_specific_sourced
615                                         from    mtl_system_items
616 					where   inventory_item_id = bcolgt.config_item_id
617 					and     rownum = 1)
618       where config_item_id is not null
619       and   ato_line_id = p_ato_line_id;
620       */
621 
622       -- End of addition by Renga on 12/02/04 for bug 3896824
623 
624       IF PG_DEBUG <> 0 Then
625          oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: Config item is matched/re-used with attribute 3',5);
626 	 oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: Ending API with Success',5);
627          oe_debug_pub.add('=========================================================================',1);
628          oe_debug_pub.add('                                                                         ',1);
629          oe_debug_pub.add('               END OPTION SPECIFIC SOURCE PROCESSING                     ',1);
630          oe_debug_pub.add('                                                                         ',1);
631          oe_debug_pub.add('               END TIME STAMP : '||to_char(sysdate,'hh:mi:ss')||'        ',1);
632          oe_debug_pub.add('                                                                         ',1);
633          oe_debug_pub.add('=========================================================================',1);
634       End if;
635 
636       return;
637    end if;
638    end if; --if p_mode = 'ACC'  Bugfix 8894392
639 
640    delete /*+ INDEX (bom_cto_oss_source_gt BOM_CTO_OSS_SOURCE_GT_N1)  */
641    from bom_cto_oss_source_gt
642    where ato_line_id = p_ato_line_id;
643 
644    delete /*+ INDEX (bom_cto_oss_orgslist_gt BOM_CTO_OSS_ORGSLIST_GT_N1) */
645    from bom_cto_oss_orgslist_gt
646    where ato_line_id = p_ato_line_id;
647 
648    /* Make a call to an API which will process this order for OSS */
649 
650    l_stmt_num := 60;
651    update_oss_in_bcol(
652                     p_ato_line_id   => p_ato_line_id,
653   	            x_oss_exists    => x_oss_exists,
654 		    x_return_status => x_return_status,
655 		    x_msg_data      => x_msg_data,
656 		    x_msg_count     => x_msg_count);
657 
658    If x_return_status  = FND_API.G_RET_STS_ERROR Then
659       IF PG_DEBUG <> 0 Then
660          oe_debug_pub.add(lpad(' ',g_pg_level)||
661 	          'GET_OSS_ORGS_LIST: Exepected error occurred in update_oss_in_bcol API',5);
662        End if;
663        raise FND_API.G_EXC_ERROR;
664    elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
665        IF PG_DEBUG <> 0 Then
666           oe_debug_pub.add(lpad(' ',g_pg_level)||
667 	          'GET_OSS_ORGS_LIST: Un Exepected error occurred in update_oss_in_bcol API',5);
668        End if;
669        raise FND_API.G_EXC_UNEXPECTED_ERROR;
670    End if;
671 
672    -- Moved this update stmt from update_oss_in_bcol to here. Wanted to execute this update only for ACC.
673    -- Adding the p_mode condition as part of Bugfix 8894392. Didn't want to disturb the behaviour for ACC
674    -- although I don't think this will ever get executed for CIB = 3 and matched configs because of bypass
675    -- condition. For UPG, we are doing the complete processing again. So don't need this.
676    if p_mode = 'ACC' then
677       update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
678              bom_cto_order_lines_gt bcol
679       set    bcol.option_specific = (select msi.option_specific_sourced
680                                      from   mtl_system_items msi
681                                      where  msi.inventory_item_id = bcol.config_item_id
682 		                     and    rownum =1)
683       where  bcol.perform_match = 'Y'   /* We need to add config creation condition here */
684       and    bcol.config_creation = '3'
685       and    bcol.ato_line_id   = p_ato_line_id;
686 
687       l_count := sql%rowcount;
688    end if;
689 
690    IF PG_DEBUG <> 0 Then
691      oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: Number of matched configs with attribute settting 3 ='
692                                           ||l_count,5);
693    End if;
694 
695    l_stmt_num := 70;
696    If x_oss_exists = 'Y' Then
697 
698       l_stmt_num := 80;
699       Get_order_sourcing_data(
700                                 p_ato_line_id   => p_ato_line_id,
701                                 x_return_status => x_return_status,
702                                 x_msg_count     => x_msg_count,
703                                 x_msg_data      => x_msg_data);
704       If x_return_status  = FND_API.G_RET_STS_ERROR Then
705          IF PG_DEBUG <> 0 Then
706             oe_debug_pub.add(lpad(' ',g_pg_level)||
707 	          'GET_OSS_ORGS_LIST: Exepected error occurred in update_oss_in_bcol API',5);
708           End if;
709           raise FND_API.G_EXC_ERROR;
710       elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
711           IF PG_DEBUG <> 0 Then
712              oe_debug_pub.add(lpad(' ',g_pg_level)||
713 	          'GET_OSS_ORGS_LIST: Un Exepected error occurred in update_oss_in_bcol API',5);
714           End if;
715           raise FND_API.G_EXC_UNEXPECTED_ERROR;
716       End if;
717 
718 
719       l_stmt_num := 80;
720       Process_order_for_oss(
721                          p_ato_line_id   => p_ato_line_id,
722 			 P_calling_mode  => p_mode,  -- Bugfix 8894392. Need to pass the correct mode.
723 			 x_return_status => x_return_status,
724 			 x_msg_count     => x_msg_count,
725 			 x_msg_data      => x_msg_data);
726 
727       If x_return_status  = FND_API.G_RET_STS_ERROR Then
728          IF PG_DEBUG <> 0 Then
729             oe_debug_pub.add(lpad(' ',g_pg_level)||
730 	          'GET_OSS_ORGS_LIST: Exepected error occurred in update_oss_in_bcol API',5);
731           End if;
732           raise FND_API.G_EXC_ERROR;
733       elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
734           IF PG_DEBUG <> 0 Then
735              oe_debug_pub.add(lpad(' ',g_pg_level)||
736 	          'GET_OSS_ORGS_LIST: Un Exepected error occurred in update_oss_in_bcol API',5);
737           End if;
738           raise FND_API.G_EXC_UNEXPECTED_ERROR;
739       End if;
740 
741 
742 
743       l_stmt_num := 90;
744 
745       If PG_DEBUG <> 0 Then
746          oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: Before validating ship from org',5);
747       End if;
748 
749       /* Renga: Add Validation to ship from org check */
750 
751       If p_mode = 'ACC' then
752          update bom_cto_order_lines bcol
753          set    option_specific = (select /*+ INDEX (bcol_gt BOM_CTO_ORDER_LINES_GT_U1) */
754 	                                 decode(option_specific,'4','3',option_specific)
755                                    from   bom_cto_order_lines_gt bcol_gt
756 			           where  bcol_gt.line_id = bcol.line_id)
757          where  bcol.ato_line_id = p_ato_line_id;
758       elsif p_mode = 'UPG' then
759          update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_UPG_N4) */ bom_cto_order_lines_upg bcol
760          set    option_specific = (select /*+ INDEX (bcol_gt BOM_CTO_ORDER_LINES_GT_U1) */
761 	                                  decode(option_specific,'4','3',option_specific)
762                                    from   bom_cto_order_lines_gt bcol_gt
763                                    where  bcol_gt.line_id = bcol.line_id)
764          where  bcol.ato_line_id = p_ato_line_id;
765       end if;
766 
767       If PG_DEBUG <> 0 Then
768          oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: l_program_id:'|| l_program_id, 5);
769       END IF;
770 
771       IF (l_program_id <> cto_update_configs_pk.bac_program_id) THEN
772       -- Bugfix 8894392: If program id = 99, it means that the matched CIB = 3 config
773       -- was picked up from bac. This processing failed if a config is present only
774       -- on closed SO lines with a shipping org that is now invalid as per new OSS
775       -- setting. For example, consider a config C1 that is now present only on closed
776       -- SO lines. It has a shipping warehouse as M1. This config will also be present
777       -- in bac with organization_id = 207(M1) which is used in UPG processing as ship_from_org_id.
778       -- So when users wanted to make this org M1 invalid as per their OSS, the UEC complained
779       -- saying ship_from org not valid. So if a config is coming from bac, not performing
780       -- this validation check. Secondly, organization_id in bac can be any arbit org where
781       -- the config was created sometime in the past. I don't suppose we should rely on
782       -- bac's organization_id as ship_from_org_id. We will probably have to think about this later.
783 
784 	Begin
785 	  select 'Y'
786 	  into  /*+ INDEX(bcol BOM_CTO_ORDER_LINES_GT_U1) */
787 	        l_valid_ship_from_org
788           from   bom_cto_order_lines_gt bcol
789           where  line_id = p_ato_line_id
790 	  and (option_specific is null
791               or ship_from_org_id in (select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
792 	                              rcv_org_id
793                                       from   bom_cto_oss_source_gt oss_src
794 	    			      where  line_id = p_ato_line_id
795 				      and    valid_flag = 'Y'
796 				      union
797 				      select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
798 				             source_org_id
799                                       from   bom_cto_oss_source_gt oss_src
800 				      where  line_id = p_ato_line_id
801 				      and    valid_flag = 'Y'));
802 	Exception when no_data_found then
803           If PG_DEBUG <> 0 Then
804 	    oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS: Ship from org is not valid',5);
805 	  end if;
806           CTO_MSG_PUB.cto_message('BOM','CTO_OSS_INVALID_SHIP_ORG');
807           raise FND_API.G_EXC_ERROR;
808 	End;
809 
810       END IF;  -- IF (l_program_id <> cto_update_configs_pk.bac_program_id) Bugfix 8894392
811 
812       -- Added By Renga Kannan on 11/30/04  for bug #3896824
813       --
814       -- In the preconfig process, preconfiguration is allowed only in the
815       -- manufacturing/Procuring org for OSS cases. For OSS config, we allways
816       -- create bom only in the manufacturing or procuring orgs. When user trys
817       -- pre configure the ato item in the intermediate orgs, CTO should raise an
818       -- error stating that this org is not valid for pre configuration
819       -- We are adding this validation here and raise the appropriate error.
820 
821       -- The way the validation works is as follows. By looking at the pruned sourcing
822       -- tree from the temp table, this part of the code identify all the manufacturing/
823       -- Procuring org. Then we will verify that the ship from org is part of this orgs
824       -- list. If it is not part of the org list derived, then we will register an error
825 
826       -- The following sql will derive the list of manufacturing/procuring org
827       -- from the pruned sourcing tree
828 
829       -- This validation should be performed only for pre configuration cases. This should
830       -- not be performed for Upgrade and ACC.
831 
832 
833       If l_program_id = CTO_UTILITY_PK.PC_BOM_PROGRAM_ID Then
834          Begin
835             Select 'Y'
836             into  l_valid_preconfig_org
837             from
838             (Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
839                     distinct nvl(source_org_id,rcv_org_id)  org_id
840              from   bom_cto_oss_source_gt oss_src
841              where  line_id = p_ato_line_id
842              and    valid_flag in( 'P','Y')
843              and    source_type in (2,3)
844              union
845              select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
846                     distinct source_org_id org_id
847              from   bom_cto_oss_source_gt oss_src
848              where  line_id = p_ato_line_id
849              and    valid_flag in ('P','Y')
850              and    source_org_id not in (
851 				select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
852 				       rcv_org_id
853 				from   bom_cto_oss_source_gt oss_src
854 				where  line_id = p_ato_line_id
855 				and    valid_flag in( 'P','Y')))
856 	     Where org_id = l_ship_from_org_id
857 	     and   rownum = 1;
858 	     if PG_DEBUG<> 0 Then
859 	        oe_debug_pub.add(lpad(' ',g_pg_level)||
860 		                      'PROCESS_OSS_CONFIGURATIONS::The Preconfiguration org is valid manufacturing/procuring org',3);
861 	     end if;
862 
863           Exception when no_data_found then
864   	     if PG_DEBUG<> 0 Then
865 	        oe_debug_pub.add(lpad(' ',g_pg_level)||
866 		                      'PROCESS_OSS_CONFIGURATIONS::The Preconfiguration org is not a valid manufacturing/procuring org',3);
867 		oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS::Raising Expected error',1);
868 	     end if;
869 	     CTO_MSG_PUB.cto_message('BOM','CTO_OSS_INVALID_PC_ORG');
870              raise FND_API.G_EXC_ERROR;
871 	  End;
872       End if; /* l_program_id = CTO_UTILITY_PK.PC_BOM_PROGRAM_ID */
873 
874       -- End of change for bug 3896824 on 11/30/04
875 
876    End if; /* x_oss_exists = 'Y' */
877 
878 
879    If PG_DEBUG <> 0 Then
880       oe_debug_pub.add('=========================================================================',1);
881       oe_debug_pub.add('                                                                         ',1);
882       oe_debug_pub.add('               END OPTION SPECIFIC SOURCE PROCESSING                     ',1);
883       oe_debug_pub.add('                                                                         ',1);
884       oe_debug_pub.add('               END TIME STAMP : '||to_char(sysdate,'hh:mi:ss')||'        ',1);
885       oe_debug_pub.add('                                                                         ',1);
886       oe_debug_pub.add('=========================================================================',1);
887    End if;
888 
889 Exception
890 
891     WHEN FND_API.G_EXC_ERROR THEN
892        IF PG_DEBUG <> 0 THEN
893           oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS::exp error::'
894 			      ||to_char(l_stmt_num)
895 			      ||'::'||sqlerrm,1);
896        END IF;
897           x_return_status := FND_API.G_RET_STS_ERROR;
898 	  g_pg_level := g_pg_level - 3;
899           cto_msg_pub.count_and_get(
900                                     p_msg_count  => x_msg_count,
901                                     p_msg_data   => x_msg_data
902                                    );
903     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
904        IF PG_DEBUG <> 0 THEN
905           oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS::exp error::'
906 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
907        END IF;
908        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
909        g_pg_level := g_pg_level - 3;
910        cto_msg_pub.count_and_get(
911                                     p_msg_count  => x_msg_count,
912                                     p_msg_data   => x_msg_data
913                                    );
914     WHEN OTHERS THEN
915        IF PG_DEBUG <> 0 THEN
916           oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_OSS_CONFIGURATIONS::exp error::'
917 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
918        END IF;
919        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
920        g_pg_level := g_pg_level - 3;
921        cto_msg_pub.count_and_get(
922                                     p_msg_count  => x_msg_count,
923                                     p_msg_data   => x_msg_data
924                                    );
925 
926 END Process_Oss_configurations;
927 
928 
929 
930 
931 /*
932 
933 
934 
935 
936           *****************************  PRUNE_OSS_CONFIGURATIONS  ***************************************
937 
938 
939 
940 
941 */
942 
943 Procedure prune_oss_config(
944                            p_model_line_id  IN  Number,
945 		           p_model_item_id  IN  Number,
946 			   p_config_item_id IN  Number,
947 			   p_calling_mode   IN  Varchar2,
948                            p_ato_line_id    IN  Number,
949 			   x_exp_error_code OUT NOCOPY Number,
950                            x_return_status  OUT NOCOPY Varchar2,
951 	                   x_msg_count      OUT NOCOPY Number,
952 		           x_msg_data       OUT NOCOPY Varchar2
953 			  ) is
954 
955    l_comp_count		Number :=0;
956    l_vendor_count       Number :=0;
957    l_org_count          Number :=0;
958    l_valid_count        Number :=0;
959    l_stmt_num           Number :=0;
960 
961 
962 
963 
964 Begin
965 
966     g_pg_level := g_pg_level + 3;
967     x_return_status := FND_API.G_RET_STS_SUCCESS;
968     x_exp_error_code := 0;
969 
970     delete /*+ INDEX (bom_cto_oss_orgslist_gt BOM_CTO_OSS_ORGSLIST_GT_N1) */
971     from bom_cto_oss_orgslist_gt
972     where ato_line_id = p_ato_line_id;
973 
974     /* The following sql will find out how manny componets in this
975        configuration has OSS definition
976     */
977 
978     If PG_DEBUG <> 0 Then
979        oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Inside PRUNE_OSS_CONFIG API',5);
980     End if;
981 
982 
983     l_stmt_num := 10;
984 
985     select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N3) */
986            count(*)
987     into   l_comp_count
988     from   bom_cto_oss_components ossc,
989            bom_cto_order_lines_gt    bcol
990     where  ossc.model_item_id      = p_model_item_id
991     and    ossc.option_item_id     = bcol.inventory_item_id
992     and    bcol.parent_ato_line_id = p_model_line_id
993     and    exists (select 'x' from bom_cto_oss_orgs_list ossl
994                    where ossl.oss_comp_seq_id = ossc.oss_comp_seq_id);
995 
996 
997     If PG_DEBUG <> 0 then
998        oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: No of oss components for this model = '||l_comp_count,5);
999     End if;
1000 
1001     If l_comp_count > 0 then
1002 
1003         /* We need to find out the intersection orgs from the sourcing setup list.
1004 	   The intersection is found by looking the organization occurance with oss components count.
1005 	   For example if 5 components are part of oss, then all orgs which occur 5 times in the sql
1006 	   will be the commong orgs or intersection orgs.
1007 	*/
1008 
1009 	l_stmt_num := 20;
1010 
1011         Insert into bom_cto_oss_orgslist_gt
1012                   (
1013                     line_id,          /* Model Line id */
1014                     inventory_item_id,/* Model item id */
1015                     organization_id,  /* Organization Id */
1016                     ato_line_id     /* Ato line id */
1017                   )
1018 
1019         select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N3) */
1020                p_model_line_id Line_id,
1021                p_model_item_id inventory_item_id,
1022                ossl.organization_id organization_id,
1023                p_ato_line_id
1024 
1025         from   bom_cto_oss_components ossc,
1026                bom_cto_oss_orgs_list  ossl,
1027                bom_cto_order_lines_gt bcol
1028 
1029         where
1030                ossc.model_item_id       = p_model_item_id
1031         and    ossc.option_item_id      = bcol.inventory_item_id
1032         and    bcol.parent_ato_line_id  = p_model_line_id
1033         and    ossc.oss_comp_seq_id     = ossl.oss_comp_seq_id
1034 	and    ossl.organization_id is not null
1035 
1036         group by organization_id
1037 
1038         having count(*) = l_comp_count;
1039 
1040 	l_org_count  := sql%rowcount;
1041 
1042         If PG_DEBUG <> 0 Then
1043            oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: After first Insert',5);
1044 	   oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Number of of orgs inserted in temp table ='||l_org_count,5);
1045 	End if;
1046 
1047 
1048         /* We need to find out the intersection vendors from the sourcing setup list.
1049 	   The intersection is found by looking the vendor-vendor site occurance with oss components count.
1050 	   For example if 5 components are part of oss, then all vendor-vendor site which occur 5 times in
1051 	   the sql will be the commong orgs or intersection orgs.
1052 	*/
1053 
1054         l_stmt_num := 30;
1055 
1056         Insert into bom_cto_oss_orgslist_gt(
1057                line_id,
1058                inventory_item_id,
1059                vendor_id,
1060                vendor_site_code,
1061                ato_line_id )
1062 
1063 
1064         select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N3) */
1065                p_model_line_id line_id,
1066                p_model_item_id inventory_item_id,
1067                ossl.vendor_id vendor_id,
1068                decode(ossl.vendor_site_code,null,'-1',
1069                       ossl.vendor_site_code) vendor_site_code,
1070                p_ato_line_id   ato_line_id
1071 
1072         from   bom_cto_oss_components ossc,
1073                bom_cto_oss_orgs_list ossl,
1074                bom_cto_order_lines_gt bcol
1075 
1076         where
1077                bcol.parent_ato_line_id = p_model_line_id
1078         and    ossc.model_item_id      = p_model_item_id
1079         and    ossc.option_item_id     = bcol.inventory_item_id
1080         and    ossc.oss_comp_seq_id    = ossl.oss_comp_seq_id
1081 	and    ossl.vendor_id is not null
1082 
1083         group by vendor_id,
1084                  decode(vendor_site_code,null,'-1',vendor_site_code)
1085 
1086 
1087         having count(*) = l_comp_count;
1088 
1089 	l_vendor_count := sql%rowcount;
1090 
1091 	If PG_DEBUG <> 0 Then
1092            oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: After Second insert..',5);
1093 	   oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Number of Vendors inserted into temp = '||l_vendor_count);
1094 	End if;
1095 
1096         /* If there is no commong orgs, then CTO will raise an error and end the process
1097 	   Renga: Think about the case, where no orgs found but some valid vendors found.
1098 	          is it ok to go ahead in that case? check later
1099 	*/
1100 
1101         /* Impact: Here we need to populate the error code for ATP purpose  */
1102 
1103 
1104 	l_stmt_num := 40;
1105 
1106 
1107 	If l_vendor_count = 0 and l_org_count = 0 then
1108 
1109 	    If PG_DEBUG <> 0 then
1110                oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: No Intersection org found ',1);
1111 	       oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Model line id = '||p_model_line_id,1);
1112 	    end if;
1113 	    If p_calling_mode in ('ACC', 'UPG') Then  --Bugfix 8894392
1114 	       cto_msg_pub.cto_message('BOM','BOM_CTO_OSS_NO_COMMON_ORGS');
1115 	       raise FND_API.G_EXC_ERROR;
1116 	    Elsif p_calling_mode = 'ATP' Then
1117 	       x_exp_error_code := 350;
1118 	       g_pg_level := g_pg_level - 3;
1119 	       return;
1120 	    End if;
1121 
1122 	End if;
1123 
1124         /*
1125 
1126 	   Now it is the time to load all the valid model sourcing assignment into memory.
1127            All the assignment for model-org, for which org is not part of our common org
1128 	   list can be igonored. That means, we will first load all the assignments for which
1129 	   the organization is part of the commong org list.
1130 
1131 	   Also, we should load item level and customer level assignments as they don't have
1132 	   any organization.
1133 
1134 	   Renga: Here is the most important point, where we may need some decode to get
1135 	          correct rcv_org_id.In some global sourcing rule cases, it will be null.
1136 		  In those cases, we may need to substitue with assignment org id.
1137 
1138 	 */
1139 
1140         l_stmt_num := 50;
1141 
1142 
1143         /*
1144 	      NOTE: The above sql will get both item and customer level rule. also,
1145 	      get valid item org rules at once.
1146 
1147 	      Renga: We may need to tune the above query later.
1148         */
1149 
1150 	/* Identify all the rows which has the source org or vendor as part of the
1151 	   intersection list.
1152 	 */
1153 /*
1154         for org_rec in org_cur
1155         Loop
1156            oe_debug_pub.add('Temp Org id = '||org_rec.organization_id,1);
1157         End Loop;
1158 
1159         for src_rec in src_cur
1160         loop
1161            oe_debug_pub.add('Temp rcv org id = '||src_rec.rcv_org_id,1);
1162            oe_debug_pub.add('Temp Src org id = '||src_rec.source_org_id,1);
1163            oe_debug_pub.add('Temp Valid flag = '||src_rec.valid_flag,1);
1164         end loop;
1165 */
1166 
1167         l_stmt_num := 60;
1168 
1169 	Update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1170 	       bom_cto_oss_source_gt oss_src
1171 	set    oss_src.valid_flag = 'Y'
1172 	where oss_src.line_id = p_model_line_id
1173 	and   ((oss_src.source_org_id in
1174 	        (select /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
1175 		        organization_id
1176 		 from	bom_cto_oss_orgslist_gt oss_lis
1177 		 where  oss_lis.line_id = p_model_line_id)
1178                  or  (nvl(oss_src.vendor_id,-1),nvl(oss_src.vendor_site_code,-1)) in
1179 	         (select  /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
1180 		         nvl(vendor_id,-99),vendor_site_code
1181 		  from   bom_cto_oss_orgslist_gt oss_lis
1182 		  where    oss_lis.line_id = p_model_line_id)
1183               )
1184 	      )
1185 	and (oss_src.rcv_org_id is null or
1186              oss_src.rcv_org_id in (
1187                                    select  /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
1188                                           organization_id
1189 	                           from   bom_cto_oss_orgslist_gt oss_lis
1190 				   where  line_id = p_model_line_id)
1191             );
1192 
1193 
1194         If PG_DEBUG <> 0 Then
1195            oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Number of valid nodes in the pruned tree ='
1196 	                                        ||sql%rowcount,5);
1197 	End if;
1198 
1199 
1200 
1201 	/* Get all the organizations which are not part of model sourcing tree..
1202 	   And with the planning make buy code 1. That meand make models
1203 	 */
1204 
1205 
1206        /* Impact: Don't introdue make at rule for orgs, which exists as part of sourcing rule
1207           even if it is not valid */
1208         l_stmt_num := 70;
1209 
1210 
1211         insert into bom_cto_oss_source_gt
1212 	              (
1213 		       inventory_item_id,
1214 		       line_id,
1215 		       config_item_id,
1216 		       rcv_org_id,
1217 		       source_org_id,
1218 		       customer_id,
1219 		       ship_to_site_id,
1220 		       vendor_id,
1221 		       vendor_site_code,
1222 		       rank,
1223 		       allocation,
1224 		       reuse_flag,
1225 		       source_type,
1226 		       valid_flag,
1227 		       leaf_node
1228                       )
1229          Select /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
1230 	        p_model_item_id,
1231 	        p_model_line_id,
1232 		p_config_item_id,
1233 		oss_lis.organization_id,
1234 		oss_lis.organization_id,
1235 		null,
1236 		null,
1237 		null,
1238 		null,
1239 		1,
1240 		100,
1241                 'N',
1242 		2,       /* Make at source type */
1243 		'Y',     /* Valid flag          */
1244 		'Y'      /* Leaf  node          */
1245          from   bom_cto_oss_orgslist_gt oss_lis,
1246                 mtl_system_items msi
1247          where  oss_lis.organization_id not in
1248                (select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1249 	              nvl(rcv_org_id,-1)
1250                 from  bom_cto_oss_source_gt   oss_src
1251 		where oss_src.line_id = p_model_line_id
1252 	        union
1253 	        select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1254 		       nvl(source_org_id,-1)
1255 	        from   bom_cto_oss_source_gt oss_src
1256 		where  oss_src.line_id = p_model_line_id
1257                 and    valid_flag = 'Y'
1258 	       )
1259 	 and    oss_lis.line_id = p_model_line_id
1260          and    msi.inventory_item_id = oss_lis.inventory_item_id
1261          and    msi.organization_id   = oss_lis.organization_id
1262          and    msi.planning_make_buy_code = 1;
1263 
1264          /* Impact : Valid flag condition should be removed */
1265 
1266          IF PG_DEBUG <> 0 Then
1267    	     oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Number of 100% rules inserted ='
1268 	                                          ||sql%rowcount);
1269 	  End if;
1270 
1271 
1272          /* If no valid sourcs found after pruning, CTO should error out
1273 	 */
1274 
1275          l_stmt_num := 80;
1276 
1277 	 Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1278 	        count(*)
1279 	 into   l_valid_count
1280 	 from   bom_cto_oss_source_gt oss_src
1281 	 where  valid_flag = 'Y'
1282 	 and    line_id    = p_model_line_id;
1283 
1284          IF PG_DEBUG <> 0 Then
1285             oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Number of valid orgs = '
1286 	                                         ||l_valid_count);
1287 	 End if;
1288 
1289          If l_valid_count = 0 then
1290 
1291 	    If PG_DEBUG <> 0 then
1292 	      oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Purning model tree results wiht no valid orgs',5);
1293 	    End if;
1294             If p_calling_mode in ('ACC', 'UPG') Then  --Bugfix 8894392
1295 
1296 	       IF PG_DEBUG <> 0 Then
1297 		  oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: About to delete rules for config item:'|| p_config_item_id);
1298 	       End if;
1299 
1300 	       del_from_msa(p_config_item_id);  --Bugfix 8894392
1301 
1302 	       cto_msg_pub.cto_message('BOM','CTO_OSS_NO_VALID_TREE');
1303                raise FND_API.G_EXC_ERROR;
1304 	    Elsif p_calling_mode = 'ATP' Then
1305 	       x_exp_error_code := 370;
1306 	       g_pg_level := g_pg_level - 3;
1307 	       return;
1308 	    End if;
1309 
1310 	 end if;
1311 
1312         l_stmt_num := 90;
1313 
1314 	/* Identify and mark all the leaf nodes in the valid sourcing tree */
1315 
1316 	update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1317 	       bom_cto_oss_source_gt oss_src
1318 	set    leaf_node = 'Y'
1319 	where  leaf_node is null
1320 	and    line_id    = p_model_line_id
1321 	and    valid_flag = 'Y'
1322 	and    source_org_id not in (
1323 	                             select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1324 				            rcv_org_id
1325 				     from   bom_cto_oss_source_gt oss_src
1326 				     where  line_id = p_model_line_id
1327 				     and    valid_flag = 'Y');
1328 	/* Renga: Try converting this into a seperate procedure
1329 	          and re-use the code later
1330         */
1331 
1332         l_stmt_num := 100;
1333 --	Delete from bom_cto_oss_orgslist_gt;
1334 	/* Renga: Is it required to have delete here
1335 	*/
1336 
1337 	/* Renga: Things about match and re-use case for parent configs
1338 	*/
1339         If PG_DEBUG <> 0 Then
1340    	   oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG: Coming out of PRUNE_OSS_CONFIG API',5);
1341 	End if;
1342 
1343 
1344     End if;
1345 
1346     --Bugfix 13362916 Debug changes.
1347     IF PG_DEBUG <> 0 Then
1348       Print_source_gt;
1349       Print_orglist_gt;
1350     END IF;
1351 
1352     g_pg_level := g_pg_level - 3;
1353 
1354 Exception
1355 
1356     WHEN FND_API.G_EXC_ERROR THEN
1357        IF PG_DEBUG <> 0 THEN
1358           oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG::exp error::'
1359 			      ||to_char(l_stmt_num)
1360 			      ||'::'||sqlerrm,1);
1361        END IF;
1362           x_return_status := FND_API.G_RET_STS_ERROR;
1363 	  g_pg_level := g_pg_level - 3;
1364           cto_msg_pub.count_and_get(
1365                                     p_msg_count  => x_msg_count,
1366                                     p_msg_data   => x_msg_data
1367                                    );
1368     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1369        IF PG_DEBUG <> 0 THEN
1370           oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG::exp error::'
1371 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
1372        END IF;
1373        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1374        g_pg_level := g_pg_level - 3;
1375        cto_msg_pub.count_and_get(
1376                                  p_msg_count  => x_msg_count,
1377                                  p_msg_data   => x_msg_data
1378                                 );
1379     WHEN OTHERS THEN
1380        IF PG_DEBUG <> 0 THEN
1381           oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_OSS_CONFIG::exp error::'
1382 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
1383        END IF;
1384        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1385        g_pg_level := g_pg_level - 3;
1386        cto_msg_pub.count_and_get(
1387                                  p_msg_count  => x_msg_count,
1388                                  p_msg_data   => x_msg_data
1389                                 );
1390 
1391 End prune_oss_config;
1392 
1393 
1394 
1395 /* The following procedure will prune the OSS model sourcing with the list
1396    of organization specified
1397  */
1398 
1399 Procedure prune_parent_oss_config(
1400                                   p_model_line_id  IN  Number,
1401 				  p_model_item_id  IN  Number,
1402 				  p_calling_mode   IN  Varchar2,
1403                                   p_ato_line_id    IN  Number,
1404 				  x_exp_error_code OUT NOCOPY Number,
1405                                   x_return_status  OUT NOCOPY Varchar2,
1406                       	          x_msg_count      OUT NOCOPY Number,
1407                    		  x_msg_data       OUT NOCOPY Varchar2
1408 				 ) is
1409    l_oss_child_count    Number := 0;
1410    TYPE Source_org_tbl is Table of Number index by binary_integer;
1411    TYPE Rcv_org_tbl is Table of Number index by binary_integer;
1412    l_source_org_tbl     Source_org_tbl;
1413    l_rcv_org_tbl        rcv_org_tbl;
1414    l_valid_source_count Number := 0;
1415    l_item_rule_count    Number := 0;
1416    l_rule_exists        Varchar2(1);
1417    l_stmt_num           Number;
1418 
1419    l_option_specific    Varchar2(1);
1420 
1421    --Bugfix 13362916: Adding a new variable
1422    l_cnt                Number;
1423 Begin
1424 
1425    select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
1426           option_specific
1427    into   l_option_specific
1428    from   bom_cto_order_lines_gt
1429    where  line_id = p_model_line_id;
1430 
1431    If l_option_specific = '2' then
1432       update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1433            bom_cto_oss_source_gt oss_src
1434       set  valid_flag = 'N'
1435       where  line_id = p_model_line_id
1436       and    valid_flag is null;
1437       If PG_DEBUG <> 0 then
1438          oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Number of records updated in source table = '
1439                                               ||sql%rowcount,5);
1440       End if;
1441 
1442       update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1443              bom_cto_oss_source_gt oss_src
1444       set    valid_flag = null
1445       where  line_id  = p_model_line_id
1446       and    valid_flag = 'Y';
1447 
1448       If PG_DEBUG <> 0 then
1449          oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Number of records updated in source table = '
1450                                               ||sql%rowcount,5);
1451       End if;
1452    end if;
1453 
1454    g_pg_level := g_pg_level + 3;
1455    x_return_status := FND_API.G_RET_STS_SUCCESS;
1456    l_stmt_num := 10;
1457    /* Get the no of child that are oss for this parent model
1458    */
1459 
1460    If PG_DEBUG <> 0 Then
1461       oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Inside PRUNE_PARENT_OSS_CONFIG API',5);
1462       oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: p_calling_mode::' || p_calling_mode,5);
1463    End if;
1464 
1465 
1466    select /*+ INDEX(bcol BOM_CTO_ORDER_LINES_GT_N3) */
1467           count(*)
1468    into   l_oss_child_count
1469    from   bom_cto_order_lines_gt bcol
1470    where  parent_ato_line_id = p_model_line_id
1471    and    line_id <> p_model_line_id   /* We should igonre the current row */
1472    and    option_specific    in ('1','2','3')
1473    --Bugfix 13540153-FP(13360098)
1474    --and    not exists(select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1475    /*                        'x'
1476 		     from  bom_cto_oss_source_gt oss_src
1477 		     where line_id = bcol.line_id
1478 		     and   rcv_org_id is null
1479 		     and   nvl(valid_flag,'N') = 'Y'
1480                      and   option_specific = '3')*/;
1481 
1482    /*<This is sacred>
1483    Reason for commenting parts of the above sql:
1484    Consider a scenarion like:
1485    model1 (No OSS - OSS flag set to 3 by Update_parent_oss_line)
1486    .model2 (No OSS - OSS flag set to 3 by Update_parent_oss_line)
1487    ..model3 (OSS - OSS flag set to 1)
1488 
1489    The above sql would properly prune model2 using OSS of model3 but would skip
1490    pruning model1 using pruned tree for model2 and model3 combined. This results
1491    in wrong sourcing results being returned.
1492 
1493    The above commenting is done while inserting into bom_cto_oss_orgslist_gt
1494    also.
1495    */
1496 
1497    /* Get the intersection org from all the child oss configurations and
1498        load it to bom_cto_oss_orgslist_gt table
1499    */
1500 
1501 
1502    If PG_DEBUG <> 0 Then
1503       oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Number of oss child = '
1504                                            ||l_oss_child_count,5);
1505    End if;
1506    l_stmt_num := 20;
1507    If l_oss_child_count > 0 then
1508 
1509       l_stmt_num := 30;
1510       delete /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N1) */
1511       from bom_cto_oss_orgslist_gt oss_lis
1512       where ato_line_id = p_ato_line_id;
1513       l_stmt_num := 40;
1514 
1515       insert into bom_cto_oss_orgslist_gt(
1516                          Inventory_item_id,
1517 			 line_id,
1518 			 organization_id,
1519                          ato_line_id     )
1520       select
1521              p_model_item_id,
1522              p_model_line_id,
1523 	     organization_id,
1524              p_ato_line_id
1525       from   (select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) INDEX (bcol BOM_CTO_ORDER_LINES_GT_N3) */
1526                       oss_src.line_id line_id,
1527                       oss_src.rcv_org_id organization_id
1528               from   bom_cto_oss_source_gt oss_src,
1529                      bom_cto_order_lines_gt bcol
1530               where  bcol.parent_ato_line_id = p_model_line_id
1531               and    bcol.parent_ato_line_id <> bcol.line_id
1532               and    bcol.option_specific    in ('1','2','3')
1533               and    oss_src.line_id         = bcol.line_id
1534               and    oss_src.valid_flag      = 'Y'
1535               --Bugfix 13540153-FP(13360098): Refer to <This is sacred>.
1536 	      --and    not exists ( Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1537               /*                          'x'
1538                                  from   bom_cto_oss_source_gt oss_src1
1539                                  where oss_src1.line_id = oss_src.line_id
1540                                  and   bcol.option_specific = '3'
1541                                  and   nvl(valid_flag,'N') = 'Y'
1542                                  and   rcv_org_id is null)*/
1543               Union
1544               select  /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) INDEX (bcol BOM_CTO_ORDER_LINES_GT_N3) */
1545                       oss_src.line_id line_id,
1546                       oss_src.source_org_id organization_id
1547               from   bom_cto_oss_source_gt oss_src,
1548               bom_cto_order_lines_gt bcol
1549               where  bcol.parent_ato_line_id = p_model_line_id
1550               and    bcol.parent_ato_line_id <> bcol.line_id
1551               and    bcol.option_specific    in ('1','2','3')
1552               and    oss_src.line_id         = bcol.line_id
1553               and    oss_src.valid_flag      = 'Y'
1554               --Bugfix 13540153-FP(13360098): Refer to <This is sacred>.
1555 	      --and    not exists ( Select /*+ INDEX (oss_src1 BOM_CTO_OSS_SOURCE_GT_N2) */
1556               /*                           'x'
1557                                   from  bom_cto_oss_source_gt oss_src1
1558                                   where oss_src1.line_id = oss_src.line_id
1559                                   and   bcol.option_specific = '3'
1560                                   and   nvl(valid_flag,'N') = 'Y'
1561                                   and   rcv_org_id is null)*/
1562 	     )
1563 
1564               group by organization_id
1565               having count(*) = l_oss_child_count;
1566 
1567       --Bugfix 13362916
1568       l_cnt := sql%rowcount;
1569    Else
1570       IF PG_DEBUG <> 0 THEN
1571          oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: No oss child found...Updating in bcol',1);
1572       END IF;
1573 
1574       update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
1575              bom_cto_order_lines_gt bcol
1576       set    option_specific = null
1577       where  line_id = p_model_line_id;
1578 
1579       IF PG_DEBUG <> 0 THEN
1580          oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Rows updated = '||sql%rowcount,1);
1581       END IF;
1582 
1583       return;
1584    End if;
1585 
1586    l_stmt_num := 50;
1587    If PG_DEBUG <> 0 Then
1588        oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Number of intersection orgs = '
1589                                             ||l_cnt,5);
1590    End if;
1591 
1592    -- Bug 13362916
1593    -- If sql%rowcount = 0 then
1594    If l_cnt = 0 then
1595       IF PG_DEBUG <> 0 Then
1596          oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: No intersection orgs found',5);
1597       END IF;
1598       x_exp_error_code := 350;   /* No intersection orgs found */
1599       g_pg_level := g_pg_level - 3;
1600       return;
1601    end if;
1602 
1603    -- Bugfix 13362916
1604    If PG_DEBUG <> 0 Then
1605       Print_source_gt;
1606       Print_orglist_gt;
1607    End If;
1608 
1609    /* Check to see if there is a item level rule exists for the model
1610    */
1611    l_stmt_num := 60;
1612    Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1613           count(*)
1614    into   l_item_rule_count
1615    from   bom_cto_oss_source_gt oss_src
1616    where  line_id  = p_model_line_id
1617    and    customer_id is null
1618    and    rcv_org_id is null
1619    and    nvl(valid_flag,'Y') <> 'N';
1620 
1621    /* If there is an item level rule exists then, Item level rule should be
1622       pruned first
1623    */
1624 
1625 
1626    If PG_DEBUG <> 0 Then
1627       oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Item rule count = '||to_char(l_item_rule_count),5);
1628    end if;
1629 
1630    l_stmt_num := 70;
1631    If l_item_rule_count > 0 then
1632 
1633        l_stmt_num := 80;
1634        prune_item_level_rule(p_model_line_id   => p_model_line_id,
1635                              p_model_item_id   => p_model_item_id,
1636   		             x_rule_exists     => l_rule_exists,
1637 			     x_return_status   => x_return_status,
1638 			     x_msg_count       => x_msg_count,
1639 			     x_msg_data        => x_msg_data
1640 			    );
1641       If x_return_status  = FND_API.G_RET_STS_ERROR Then
1642          IF PG_DEBUG <> 0 Then
1643             oe_debug_pub.add(lpad(' ',g_pg_level)||
1644 	          'GET_OSS_ORGS_LIST: Exepected error occurred in update_oss_in_bcol API',5);
1645           End if;
1646           raise FND_API.G_EXC_ERROR;
1647       elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
1648           IF PG_DEBUG <> 0 Then
1649              oe_debug_pub.add(lpad(' ',g_pg_level)||
1650 	          'GET_OSS_ORGS_LIST: Un Exepected error occurred in update_oss_in_bcol API',5);
1651           End if;
1652           raise FND_API.G_EXC_UNEXPECTED_ERROR;
1653       End if;
1654 
1655       oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: '
1656                        ||' Item Rule exists after pruning',1);
1657     Else
1658        l_rule_exists := 'N';
1659       oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: '
1660                        ||' No Item Ruleafter pruning',1);
1661 
1662     End if;
1663    /* After pruing the item level sourcing, If still there are some
1664       valid nodes, the pruning for other nodes will be different */
1665    /* Identify all end nodes for the sourcing tree */
1666 
1667    /* Renga: Please modularise the following part of code
1668              for ease of maintenance
1669    */
1670 
1671    l_stmt_num := 90;
1672    If l_rule_exists = 'N' Then
1673       l_stmt_num := 100;
1674 
1675       Update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1676              bom_cto_oss_source_gt oss_src
1677       set    leaf_node = 'Y'
1678       where  line_id = p_model_line_id
1679       and    nvl(valid_flag,'Y')  <> 'N'
1680       and    (   source_type in (2,3)
1681             or source_org_id not in
1682 	                    (Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1683 			            rcv_org_id
1684 			     from   bom_cto_oss_source_gt oss_src
1685 			     where  line_id = p_model_line_id
1686                              and    nvl(valid_flag,'Y') <> 'N'
1687 			    )
1688 	  );
1689 
1690 
1691      /* Identify all the valid end nodes by comapring the source
1692         org with intersection org list.
1693         All the buy nodes are any way valid
1694       */
1695      l_stmt_num := 110;
1696 
1697 
1698      Update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1699             bom_cto_oss_source_gt
1700      set    valid_flag = 'Y'
1701      where  line_id  = p_model_line_id
1702      and    leaf_node = 'Y'
1703      and    nvl(valid_flag,'Y') <> 'N'
1704      and    (source_type = 3 or
1705             source_org_id in (select /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
1706 	                      organization_id
1707 	                      from   bom_cto_oss_orgslist_gt oss_list
1708 			      where  line_id = p_model_line_id
1709 			   )
1710 	 )
1711      Returning rcv_org_id,source_org_id  Bulk collect into l_source_org_tbl,l_rcv_org_tbl;
1712 
1713 
1714      If PG_DEBUG <> 0 Then
1715         oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Number of updated records ='||l_source_org_tbl.count,5);
1716      End if;
1717 
1718      If l_source_org_tbl.count <> 0 then
1719         For i in l_source_org_tbl.first..l_source_org_tbl.last
1720         Loop
1721 
1722           update_Source_tree(p_line_id       => p_model_line_id,
1723                        p_end_org       => l_source_org_tbl(i),
1724                        x_return_status => x_return_status,
1725                        x_msg_data      => x_msg_data,
1726                        x_msg_count     => x_msg_count);
1727           If x_return_status  = FND_API.G_RET_STS_ERROR Then
1728              IF PG_DEBUG <> 0 Then
1729                 oe_debug_pub.add(lpad(' ',g_pg_level)||
1730 	          'GET_OSS_ORGS_LIST: Exepected error occurred in update_oss_in_bcol API',5);
1731              End if;
1732              raise FND_API.G_EXC_ERROR;
1733           elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
1734              IF PG_DEBUG <> 0 Then
1735                 oe_debug_pub.add(lpad(' ',g_pg_level)||
1736 	          'GET_OSS_ORGS_LIST: Un Exepected error occurred in update_oss_in_bcol API',5);
1737              End if;
1738              raise FND_API.G_EXC_UNEXPECTED_ERROR;
1739           End if;
1740 
1741         End loop;
1742      End if;
1743 
1744      /* Mark all the parent lines as valid */
1745 
1746      /* At end all nodes with valid flags are valid nodes for trees  */
1747         -- Now we should mark all the nodes which is not part of the sourcing tree
1748         -- Create 100% make at rule based on planning make buy code
1749 
1750      -- Bug Fix 4112373
1751      -- Added debug print utility call to print the temp table information
1752      -- This will help in debugging
1753      If PG_DEBUG <> 0 Then
1754         print_source_gt;
1755         print_orglist_gt;
1756      End if;
1757 
1758 
1759    -- Bug Fix 4112373
1760      -- For the top model after pruning the source tree 100% sourcing rules need to be
1761      -- created in all the orgs where the top model exists and the lower level model is valid
1762      -- The following sql will be inserting the 100% rule for the top model
1763      -- The sub query in this sql will get all the list of orgs in the sourcing chain
1764      -- for this top model. Since either source org/recv org can be null, we need to have
1765      -- a nvl caluse for these select columns. Otherwise the not in comparison will not return
1766      -- any rows.
1767      -- Added the nvl clause in the subquery returun column
1768 
1769      If PG_DEBUG <> 0 Then
1770                 oe_debug_pub.add(lpad(' ',g_pg_level)||
1771 	          'PRUNE_PARENT_OSS: Before inserting 100% make at rules',5);
1772      End if;
1773      Insert into bom_cto_oss_source_gt
1774                            (
1775                             inventory_item_id,
1776 			    line_id,
1777 			    rcv_org_id,
1778                             source_org_id,
1779                             customer_id,
1780 			    ship_to_site_id,
1781 			    vendor_id,
1782 			    vendor_site_code,
1783 			    rank,
1784 			    allocation,
1785 			    reuse_flag,
1786 			    source_type,
1787 			    valid_flag,
1788 			    leaf_node
1789 			   )
1790 
1791     select  /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) INDEX(bcol BOM_CTO_ORDER_LINES_GT_U1*/
1792                               p_model_item_id,
1793                               p_model_line_id,
1794                               oss_lis.organization_id,
1795 	                      oss_lis.organization_id,
1796                               null,
1797 	                      null,
1798                	              null,
1799 	                      null,
1800 	                      1,
1801 	                      100,
1802 	                      null,
1803                               2,
1804 	                      'Y',
1805 	                      'Y'
1806     from   bom_cto_oss_orgslist_gt oss_lis,
1807            mtl_system_items msi,
1808   	 bom_cto_order_lines_gt bcol
1809     where
1810           bcol.line_id = p_model_line_id
1811     and   bcol.option_specific = '3'
1812     and   oss_lis.organization_id not in (
1813                   select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1814 		         nvl(source_org_id, -1)
1815                   from   bom_cto_oss_source_gt oss_src
1816                   where  valid_flag = 'Y'
1817                   and    line_id = p_model_line_id
1818                   union
1819                   select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1820 		         nvl(rcv_org_id,-1)
1821                   from   bom_cto_oss_source_gt oss_src
1822                   where  valid_flag = 'Y'
1823                   and    line_id    = p_model_line_id)
1824     and    oss_lis.line_id            = p_model_line_id
1825     and    oss_lis.organization_id    = msi.organization_id
1826     and    msi.inventory_item_id      = bcol.inventory_item_id
1827     and    msi.planning_make_buy_code = 1;
1828 
1829     /* By this time, we are done with all the valid nodes... */
1830 
1831     /* check to see if there is any valid node after pruning . If there is
1832        no valid nodes, CTO will fail with error message     */
1833 
1834 
1835 
1836     select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1837            count(*)
1838     into   l_valid_source_count
1839     from   bom_cto_oss_source_gt oss_src
1840     where  line_id = p_model_line_id
1841     and    valid_flag = 'Y';
1842 
1843     IF PG_DEBUG <> 0 Then
1844        oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: Number of valid nodes in the pruned tree ='
1845                                           ||l_valid_source_count,5);
1846     End if;
1847 
1848     If l_valid_source_count = 0 then
1849      IF PG_DEBUG <> 0 Then
1850         oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG: After  pruning there is no valid source node',4);
1851      End if;
1852      If p_calling_mode in ('ACC', 'UPG') Then  --Bugfix 8894392
1853         CTO_MSG_PUB.cto_message('BOM','CTO_OSS_NO_VALID_TREE');
1854 	raise FND_API.G_EXC_ERROR;
1855      elsif p_calling_mode = 'ATP' Then
1856         x_exp_error_code := 350;
1857      End if;
1858    End if;
1859  End if;
1860 
1861  -- bug 13362916
1862  IF PG_DEBUG <> 0 Then
1863    print_source_gt;
1864    Print_orglist_gt;
1865  END IF;
1866 
1867  g_pg_level := g_pg_level - 3;
1868  Exception
1869 
1870     WHEN FND_API.G_EXC_ERROR THEN
1871        IF PG_DEBUG <> 0 THEN
1872           oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG::exp error::'
1873 			      ||to_char(l_stmt_num)
1874 			      ||'::'||sqlerrm,1);
1875        END IF;
1876        x_return_status := FND_API.G_RET_STS_ERROR;
1877        g_pg_level := g_pg_level - 3;
1878        cto_msg_pub.count_and_get(
1879                                  p_msg_count  => x_msg_count,
1880                                  p_msg_data   => x_msg_data
1881                                 );
1882     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1883        IF PG_DEBUG <> 0 THEN
1884           oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG::exp error::'
1885 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
1886        END IF;
1887        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1888        g_pg_level := g_pg_level - 3;
1889        cto_msg_pub.count_and_get(
1890                                  p_msg_count  => x_msg_count,
1891                                  p_msg_data   => x_msg_data
1892                                 );
1893     WHEN OTHERS THEN
1894        IF PG_DEBUG <> 0 THEN
1895           oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_PARENT_OSS_CONFIG::exp error::'
1896 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
1897        END IF;
1898        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1899        g_pg_level := g_pg_level - 3;
1900        cto_msg_pub.count_and_get(
1901                                  p_msg_count  => x_msg_count,
1902                                  p_msg_data   => x_msg_data
1903                                 );
1904 
1905 End prune_parent_oss_config;
1906 
1907 
1908 
1909 /* This procedure will look at the pruned tree from
1910    bom_cto_oss_source_gt and create new
1911    sourcing rules and assignments
1912  */
1913 
1914 Procedure Create_oss_sourcing_rules (
1915                                       p_ato_line_id   IN  Number,
1916                                       p_mode          IN  Varchar2 DEFAULT 'ACC',
1917                                       p_changed_src   IN  Varchar2 DEFAULT null,
1918                                       x_return_status OUT NOCOPY Varchar2,
1919    		                      x_msg_count     OUT NOCOPY Number,
1920 			              x_msg_data      OUT NOCOPY Varchar2) is
1921 
1922    Cursor oss_model_lines is
1923    select line_id,
1924           inventory_item_id,
1925 	  config_item_id,
1926 	  option_specific,
1927 	  config_creation,
1928 	  perform_match,
1929 	  reuse_config
1930    from  bom_cto_order_lines
1931    where ato_line_id = p_Ato_line_id
1932    and   option_specific in ('1','2','3')
1933    and   p_mode = 'ACC'
1934    union
1935    select line_id,
1936           inventory_item_id,
1937           config_item_id,
1938           option_specific,
1939           config_creation,
1940           perform_match,
1941           reuse_config
1942    from  bom_cto_order_lines_upg
1943    where ato_line_id = p_Ato_line_id
1944    and   option_specific in ('1','2','3')
1945    and   p_mode = 'UPG'
1946    and   (p_changed_src = 'Y' or config_creation=3);
1947 
1948 
1949    Cursor source_tree_cur(p_line_id number,
1950                           p_config_item_id number) is
1951    Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
1952           oss_src.inventory_item_id inventory_item_id,
1953           oss_src.line_id line_id,
1954 	  oss_src.rcv_org_id rcv_org_id,
1955 	  oss_src.source_org_id source_org_id,
1956 	  oss_src.vendor_id vendor_id,
1957 	  oss_src.vendor_site_code vendor_site_code,
1958 	  oss_src.rank rank,
1959 	  oss_src.allocation allocation,
1960 	  oss_src.reuse_flag reuse_flag,
1961 	  oss_src.valid_flag valid_flag,
1962 	  oss_src.leaf_node leaf_node,
1963 	  oss_src.sr_receipt_id sr_receipt_id,
1964 	  oss_src.sr_source_id sr_source_id,
1965 	  oss_src.config_item_id config_item_id,
1966 	  oss_src.source_type source_type,
1967           src_asg.assignment_type assignment_type,
1968           src_asg.assignment_set_id assignment_set_id,
1969 	  src_asg.assignment_id assignment_id,
1970           src_asg.attribute1 attribute1,
1971 	  src_asg.attribute2 attribute2,
1972 	  src_asg.attribute3 attribute3,
1973 	  src_asg.attribute4 attribute4,
1974 	  src_asg.attribute5 attribute5,
1975 	  src_asg.attribute6 attribute6,
1976 	  src_asg.attribute7 attribute7,
1977 	  src_asg.attribute8 attribute8,
1978 	  src_asg.attribute9 attribute9,
1979 	  src_asg.attribute10 attribute10,
1980 	  src_asg.attribute11 attribute11,
1981 	  src_asg.attribute12 attribute12,
1982 	  src_asg.attribute13 attribute13,
1983 	  src_asg.attribute14 attribute14,
1984 	  src_asg.attribute15 attribute15,
1985 	  src_asg.attribute_category attribute_category,
1986 	  src_asg.category_id category_id,
1987 	  src_asg.category_set_id category_set_id,
1988 	  src_asg.customer_id customer_id,
1989 	  src_asg.organization_id organization_id,
1990 	  src_asg.secondary_inventory secondary_inventory,
1991 	  src_asg.ship_to_site_id ship_to_site_id,
1992 	  src_asg.sourcing_rule_type sourcing_rule_type,
1993 	  src_asg.sourcing_rule_id sourcing_rule_id
1994    from   bom_cto_oss_source_gt  oss_src,
1995           mrp_sr_assignments       src_asg
1996    where  oss_src.line_id   = p_line_id
1997    and    nvl(oss_src.reuse_flag,'Y') = 'N'
1998    and    valid_flag   = 'P'
1999    and    src_asg.assignment_id  = oss_src.assignment_id
2000    and    nvl(src_asg.organization_id,-1) not in (select nvl(organization_id,-1)
2001                                           from   mrp_sr_assignments src_asg1
2002 					  where  inventory_item_id = p_config_item_id
2003                                           and    assignment_set_id = G_def_assg_set)
2004 
2005 
2006    order  by oss_src.line_id,
2007           oss_src.assignment_id,
2008 	  oss_src.sr_receipt_id,
2009 	  oss_src.rank;
2010    l_cur_line_id   Number:=0;
2011    l_cur_assg_id   Number;
2012 
2013    --
2014    -- Bug 13362916
2015    -- Performance changes
2016    --
2017    Cursor oss_make_orgs_cur(p_line_id Number,
2018                             p_config_item_id Number
2019                            ) is
2020    select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2021           rcv_org_id,
2022           source_org_id,
2023           allocation,
2024           rank,
2025           config_item_id
2026    from   bom_cto_oss_source_gt oss_src
2027    where  line_id    = p_line_id
2028    and    valid_flag = 'P'
2029    and    leaf_node  = 'Y'
2030    and    assignment_id is null
2031    and    rcv_org_id    IS NULL
2032    UNION
2033    select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2034           rcv_org_id,
2035           source_org_id,
2036           allocation,
2037           rank,
2038           config_item_id
2039    from   bom_cto_oss_source_gt oss_src
2040    WHERE line_id        = p_line_id
2041    AND valid_flag     = 'P'
2042    AND leaf_node      = 'Y'
2043    AND assignment_id IS NULL
2044    AND RCV_ORG_ID         IS NOT NULL
2045    AND NOT EXISTS
2046              (SELECT /*+ INDEX (msa MRP_SR_ASSIGNMENTS_N3) */ 1
2047               FROM MRP_SR_ASSIGNMENTS msa
2048                WHERE INVENTORY_ITEM_ID   = p_config_item_id
2049                  AND ASSIGNMENT_SET_ID   = G_def_assg_set
2050                  AND ORGANIZATION_ID     = RCV_ORG_ID
2051                  AND Rownum              = 1
2052              );
2053 
2054    --
2055    -- Bug 13362916
2056    -- Performance changes
2057    --
2058    Cursor oss_reused_assg(p_line_id Number,p_config_item_id number) is
2059    select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2060           distinct assignment_id
2061    from   bom_cto_oss_source_gt oss_src
2062    where  line_id = p_line_id
2063    and    valid_flag = 'P'
2064    and    nvl(reuse_flag,'Y') = 'Y'
2065    and    rcv_org_id         is null
2066    UNION
2067    select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2068           distinct assignment_id
2069    FROM BOM_CTO_OSS_SOURCE_GT OSS_SRC
2070    WHERE LINE_ID           = p_line_id
2071    and VALID_FLAG          = 'P'
2072    and NVL(REUSE_FLAG,'Y') = 'Y'
2073    and RCV_ORG_ID         IS NOT NULL
2074    and NOT EXISTS
2075      (SELECT /*+ INDEX (msa MRP_SR_ASSIGNMENTS_N3) */ 1
2076      FROM MRP_SR_ASSIGNMENTS msa
2077      WHERE INVENTORY_ITEM_ID = p_config_item_id
2078      AND ASSIGNMENT_SET_ID   = G_def_assg_set
2079      AND ORGANIZATION_ID     = RCV_ORG_ID
2080      AND Rownum              = 1
2081      );
2082 
2083    l_temp_count  Number;
2084    l_assignment_set_id   Number;
2085 
2086 
2087 
2088    TYPE source_tree_rec_typ is RECORD  (
2089  				       INVENTORY_ITEM_ID  NUMBER,
2090 				       LINE_ID            NUMBER,
2091 				       SOURCE_RULE_ID     NUMBER,
2092 				       RCV_ORG_ID         NUMBER,
2093 				       SOURCE_ORG_ID      NUMBER,
2094 				       CUSTOMER_ID        NUMBER,
2095 				       SHIP_TO_SITE_ID    NUMBER,
2096 				       VENDOR_ID          NUMBER,
2097 				       VENDOR_SITE_CODE   VARCHAR2(30),
2098 				       RANK               NUMBER,
2099 				       ALLOCATION         NUMBER,
2100 				       REUSE_FLAG         VARCHAR2(1),
2101 				       SOURCE_TYPE        NUMBER,
2102 				       VALID_FLAG         VARCHAR2(1),
2103 				       LEAF_NODE          VARCHAR2(1),
2104 				       sr_receipt_id      Number,
2105 				       sr_source_id       Number,
2106 				       assignment_id      Number
2107 				      );
2108 
2109    TYPE source_tree_tbl is TABLE of source_tree_rec_typ index by binary_integer;
2110 
2111    TYPE number_tbl is TABLE of number;
2112    TYPE varchar150_tbl is TABLE of Varchar2(150);
2113    TYPE varchar30_tbl  is TABLE of Varchar2(30);
2114    TYPE Varchar10_tbl  is TABLE of Varchar2(10);
2115    l_source_tree_tbl    source_tree_tbl;
2116    l_rank_sum           Number :=0;
2117    l_rank               Number :=0;
2118    l_new_rank_seq       Number;
2119    i                    Number :=1;
2120    l_old_rank           Number;
2121    l_curr_rcv_org       Number;
2122    rcv_count            Number;
2123    asg_count            Number :=1;
2124    l_make_at_exists     Varchar2(1);
2125 
2126    l_sourcing_rule_rec        MRP_SOURCING_RULE_PUB.sourcing_rule_rec_type;
2127    l_sourcing_rule_val_rec    MRP_SOURCING_RULE_PUB.sourcing_rule_val_rec_type;
2128    l_receiving_org_tbl        MRP_SOURCING_RULE_PUB.receiving_org_tbl_type;
2129    l_receiving_org_val_tbl    MRP_SOURCING_RULE_PUB.receiving_org_val_tbl_type;
2130    l_shipping_org_tbl         MRP_SOURCING_RULE_PUB.shipping_org_tbl_type;
2131    l_shipping_org_val_tbl     MRP_SOURCING_RULE_PUB.shipping_org_val_tbl_type;
2132    x_sourcing_rule_rec        MRP_SOURCING_RULE_PUB.sourcing_rule_rec_type;
2133    x_sourcing_rule_val_rec    MRP_SOURCING_RULE_PUB.sourcing_rule_val_rec_type;
2134    x_receiving_org_tbl        MRP_SOURCING_RULE_PUB.receiving_org_tbl_type;
2135    x_receiving_org_val_tbl    MRP_SOURCING_RULE_PUB.receiving_org_val_tbl_type;
2136    x_shipping_org_tbl         MRP_SOURCING_RULE_PUB.shipping_org_tbl_type;
2137    x_shipping_org_val_tbl     MRP_SOURCING_RULE_PUB.shipping_org_val_tbl_type;
2138 
2139 
2140    /* This is for Assignment processing
2141    */
2142 
2143    lAssignmentRec	      MRP_Src_Assignment_PUB.Assignment_Rec_Type;
2144    lAssignmentTbl	      MRP_Src_Assignment_PUB.Assignment_Tbl_Type;
2145    lAssignmentSetRec	      MRP_Src_Assignment_PUB.Assignment_Set_Rec_Type;
2146    xAssignmentSetRec	      MRP_Src_Assignment_PUB.Assignment_Set_Rec_Type;
2147    xAssignmentSetValRec	      MRP_Src_Assignment_PUB.Assignment_Set_Val_Rec_Type;
2148    xAssignmentTbl	      MRP_Src_Assignment_PUB.Assignment_Tbl_Type;
2149    xAssignmentValTbl	      MRP_Src_Assignment_PUB.Assignment_Val_Tbl_Type;
2150 
2151    /* Declaration for bulk fetch */
2152 
2153    l_index  number;
2154    l_msg_data   Varchar2(2000);
2155    l_stmt_num   Number;
2156    l_vend_site_id Number;
2157 
2158    -- Bugfix 13362916
2159    TYPE l_sr_receipt_id_cache_typ is table of number index by long;
2160    l_sr_receipt_id_cache_tbl l_sr_receipt_id_cache_typ;
2161 
2162    SR_RECEIPT_ID_cachedLOC NUMBER := NULL;
2163 Begin
2164 
2165    l_stmt_num := 10;
2166    g_pg_level := 3;
2167    x_return_status := FND_API.G_RET_STS_SUCCESS;
2168 
2169    If PG_DEBUG <> 0 Then
2170       oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Inside Create OSS Sourcing Rule API',5);
2171    End if;
2172 
2173    l_stmt_num := 15;
2174    If p_mode = 'UPG' then
2175      select assignment_set_id
2176      into   G_def_assg_set
2177      from   mrp_assignment_sets
2178      where  assignment_set_name = 'CTO Configuration Updates';
2179    end if;
2180 
2181    For oss_model_lines_rec in oss_model_lines
2182    Loop
2183      l_stmt_num := 20;
2184      If PG_DEBUG <> 0 Then
2185         oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Line id         = '||oss_model_lines_rec.line_id,5);
2186         oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Config item     = '||oss_model_lines_rec.config_item_id,5);
2187         oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Option specific = '||oss_model_lines_rec.option_specific,5);
2188 
2189         -- bug 13362916
2190         print_source_gt;
2191         Print_orglist_gt;
2192      End if;
2193 
2194      l_receiving_org_tbl.delete;
2195      l_receiving_org_val_tbl.delete;
2196 
2197      --Bugfix 13362916
2198      l_sr_receipt_id_cache_tbl.delete;
2199 
2200      l_shipping_org_tbl.delete;
2201      l_shipping_org_val_tbl.delete;
2202      l_new_rank_seq := 0;
2203      rcv_count      := 1;
2204      i              := 1;
2205      l_old_rank     :=null;
2206 
2207      if p_mode = 'ACC' then  -- Bugfix 8894392
2208        If oss_model_lines_rec.config_creation = '3' and
2209           (oss_model_lines_rec.perform_match = 'Y' or oss_model_lines_rec.reuse_config = 'Y') Then
2210 	  If PG_DEBUG <> 0 Then
2211 	     oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Config item is matched and item attribute is 3',5);
2212 	     oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: No Need to create new sourcing rules',5);
2213 	  End if;
2214 	  l_stmt_num := 30;
2215           --Bugfix 13324638
2216           --exit; /* Start processing next record */
2217           goto loop_oss_model_lines;
2218        End if;
2219      end if;  -- Bugfix 8894392
2220 
2221      -- Reasoning for adding the p_mode condition here:
2222      -- Consider that the ATO Model has 2 sourcing rules:
2223      -- Org M1 has Make at rule and Org M2 has make at rule. The OSS says M1 so the rule that should get
2224      -- created for config is Make at M1. Suppose another SO finds a match to this config. The rule that
2225      -- should be used is Make at M1. We do not need the complete processing again. Now suppose the OSS is
2226      -- changed and OSS now says M2. Since the sourcing is changed, we need to create a new Make at M2 rule.
2227      -- Without this 'if p_mode' condition, the code will bypass creation of the new rule altogether when UEC
2228      -- is run. When UEC is run, p_mode will be UPG and the code will not exit from here but create the new
2229      -- rule.
2230 
2231      l_stmt_num := 40;
2232      If oss_model_lines_rec.config_creation = '3' then
2233 
2234         /* We need to create all the sourcing rules possible for this order lines */
2235 	/* Mark all the nodes with valid flag 'Y' to 'P'. P means rows that needs to be
2236 	   processed
2237 	*/
2238 	l_stmt_num := 50;
2239 
2240         update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2241 	       bom_cto_oss_source_gt oss_src
2242 	set    valid_flag = 'P'
2243 	where  line_id = oss_model_lines_rec.line_id
2244 	and    valid_flag = 'Y';
2245 
2246         IF PG_DEBUG <> 0 then
2247            oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Number of lines updated in bom_cto_oss_source_gt ='
2248                                                 ||sql%rowcount,5);
2249         End if;
2250 
2251      Else
2252         /* We need to create sourcing rule only for this order chain */
2253 	/* We need to find the order chain from bcso */
2254 
2255         l_stmt_num := 60;
2256 	update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2257 	       bom_cto_oss_source_gt oss_src
2258 	set    valid_flag = 'P'
2259 	where  line_id = oss_model_lines_rec.line_id
2260 	and    valid_flag = 'Y'
2261 	and    rcv_org_id in (select rcv_org_id
2262 	                      from   bom_cto_src_orgs
2263 			      where  line_id = oss_model_lines_rec.line_id
2264 			      and    organization_type is not null);
2265         IF PG_DEBUG <> 0 then
2266            oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Number of lines updated in bom_cto_oss_source_gt ='
2267                                                 ||sql%rowcount,5);
2268         End if;
2269 
2270 	update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2271 	       bom_cto_oss_source_gt oss_src
2272 	set    valid_flag = 'P'
2273 	where  line_id    = oss_model_lines_rec.line_id
2274 	and    valid_flag = 'Y'
2275 	and    rcv_org_id is null
2276 	and    exists (select rcv_org_id
2277 	               from   bom_cto_src_orgs
2278 		       where  line_id = oss_model_lines_rec.line_id
2279 		       and    organization_type is not null
2280 		       and    rcv_org_id not in (select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2281 		                                        rcv_org_id
2282 		                                 from   bom_cto_oss_source_gt oss_src
2283 						 where  line_id = oss_model_lines_rec.line_id
2284 						 and    valid_flag = 'P'));
2285 
2286        IF PG_DEBUG <> 0 then
2287            oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Number of lines updated in bom_cto_oss_source_gt ='
2288                                                 ||sql%rowcount,5);
2289         End if;
2290      End if;
2291 
2292      /* Mark all the rows that are not be re-used
2293      */
2294 
2295      l_stmt_num := 65;
2296      update /*+ INDEX (oss_src1 BOM_CTO_OSS_SOURCE_GT_N2) */
2297             bom_cto_oss_source_gt oss_src1
2298      set    reuse_flag = 'N'
2299      where  line_id    = oss_model_lines_rec.line_id
2300      and    valid_flag = 'P'
2301      and    (oss_src1.assignment_id is null or exists (select/*+ INDEX (oss_src2 BOM_CTO_OSS_SOURCE_GT_N2) */
2302                                                              'x'
2303                                                        from bom_cto_oss_source_gt oss_src2
2304                                                        where oss_src2.line_id = oss_src1.line_id
2305                                                        and   oss_src2.source_rule_id = oss_src1.source_rule_id
2306                                                        and   nvl(oss_src2.valid_flag,'N') = 'N'
2307 						      )
2308             );
2309 
2310       If PG_DEBUG <> 0 Then
2311          oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Number of records that are not re-used = '||
2312                                                 sql%rowcount,5);
2313       End if;
2314 
2315       l_stmt_num := 70;
2316       l_cur_line_id := 0;
2317       For source_tree_rec in source_tree_cur(oss_model_lines_rec.line_id,
2318                                              oss_model_lines_rec.config_item_id)
2319       Loop
2320             l_stmt_num := 80;
2321             if nvl(l_cur_line_id,-1) <> source_tree_rec.line_id then
2322                l_cur_line_id := source_tree_rec.line_id;
2323                l_cur_assg_id := null;
2324             End if;
2325 
2326 	    If PG_DEBUG <> 0 Then
2327                oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: Line id       = '
2328 	                                            ||source_tree_rec.line_id,5);
2329 	       oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: Rcv Org Id    = '
2330 						    ||source_tree_rec.rcv_org_id,5);
2331 	       oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: Source org id = '
2332 						    ||Source_tree_rec.source_org_id,5);
2333                oe_debug_pub.add(lpad(' ',g_pg_level)||'Old assignment set id = '||l_cur_assg_id,5);
2334                oe_debug_pub.add(lpad(' ',g_pg_level)||'New assignment set id = '||source_tree_rec.assignment_id,5);
2335                --Bugfix 13362916
2336                oe_debug_pub.add(lpad(' ',g_pg_level)||'SR_RECEIPT_ID = '||source_tree_rec.sr_receipt_id,5);
2337 	    End if;
2338 	    l_stmt_num := 90;
2339             if nvl(l_cur_assg_id,-1) <> source_tree_rec.assignment_id  then
2340 	       /* Now this is the time to process the sourcing rule creation.
2341 	          we need to call mrp api and create a valid sourcing rule now
2342 	       */
2343 
2344                If l_cur_assg_id is not null then
2345 	          l_stmt_num := 100;
2346 	          MRP_SOURCING_RULE_PUB.PROCESS_SOURCING_RULE(
2347 	                                      p_api_version_number    => 1.0,
2348 					      p_return_values         => FND_API.G_TRUE,
2349 					      p_sourcing_rule_rec     => l_sourcing_rule_rec,
2350 					      p_sourcing_rule_val_rec => l_sourcing_rule_val_rec,
2351 					      p_receiving_org_tbl     => l_receiving_org_tbl,
2352 					      p_receiving_org_val_tbl => l_receiving_org_val_tbl,
2353 					      p_shipping_org_tbl      => l_shipping_org_tbl,
2354 					      p_shipping_org_val_tbl  => l_shipping_org_val_tbl,
2355 					      x_sourcing_rule_rec     => x_sourcing_rule_rec,
2356 					      x_sourcing_rule_val_rec => x_sourcing_rule_val_rec,
2357 					      x_receiving_org_tbl     => x_receiving_org_tbl,
2358 					      x_receiving_org_val_tbl => x_receiving_org_val_tbl,
2359   				              x_shipping_org_tbl      => x_shipping_org_tbl,
2360 					      x_shipping_org_val_tbl  => x_shipping_org_val_tbl,
2361 					      x_return_status         => x_return_status,
2362 					      x_msg_count             => x_msg_count,
2363 					      x_msg_data              => x_msg_data);
2364 
2365                   FOR l_index IN 1..x_msg_count LOOP
2366                          l_msg_data := fnd_msg_pub.get(
2367                          p_msg_index => l_index,
2368                          p_encoded  => FND_API.G_FALSE);
2369                          oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: error : '||substr(l_msg_data,1,250));
2370                   END LOOP;
2371 
2372                   If x_return_status  = FND_API.G_RET_STS_ERROR Then
2373                      IF PG_DEBUG <> 0 Then
2374                         oe_debug_pub.add(lpad(' ',g_pg_level)||
2375 	                               'CREATE_OSS_SOURCING_RULES: Exepected error occurred in update_oss_in_bcol API',5);
2376                      End if;
2377                      raise FND_API.G_EXC_ERROR;
2378                   elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
2379                      IF PG_DEBUG <> 0 Then
2380                         oe_debug_pub.add(lpad(' ',g_pg_level)||
2381 	                         'CREATE_OSS_SOURCING_RULES: Un Exepected error occurred in update_oss_in_bcol API',5);
2382                      End if;
2383                      raise FND_API.G_EXC_UNEXPECTED_ERROR;
2384                   End if;
2385                   l_receiving_org_tbl.delete;
2386                   l_receiving_org_val_tbl.delete;
2387 
2388 		  --Bugfix 13362916
2389                   l_sr_receipt_id_cache_tbl.delete;
2390 
2391                   l_shipping_org_tbl.delete;
2392                   l_shipping_org_val_tbl.delete;
2393                   l_stmt_num := 110;
2394                   lAssignmentTbl(asg_count).sourcing_rule_id    := x_sourcing_rule_rec.sourcing_rule_id;
2395                   If PG_DEBUG <> 0 Then
2396                      oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: New Sourcing rule cretaed= '                                                          ||x_sourcing_rule_rec.sourcing_rule_id,5);
2397                   End if;
2398 	          Asg_count := asg_count + 1;
2399                   l_old_rank := null;
2400                End if; /* l_cur_assg_id is not null */
2401 
2402                /* Loading Assignment record for the current assignment */
2403 
2404                l_stmt_num := 120;
2405 
2406                If PG_DEBUG <> 0 Then
2407                   oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: Loading the assignment into assignment record ',5);
2408                   oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCNIG_RULES: assignment_type = '
2409                                                        ||source_tree_rec.assignment_type,5);
2410                   oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: inventory item id = '
2411                                                        ||oss_model_lines_rec.config_item_id,5);
2412                   oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: organization id = '
2413                                                        ||source_tree_rec.organization_id,5);
2414 
2415                End if;
2416 
2417                lAssignmentTbl(asg_count).assignment_set_id   := G_def_assg_set;
2418 	       lAssignmentTbl(asg_count).assignment_type     := source_tree_rec.assignment_type;
2419 	       lAssignmentTbl(asg_count).attribute1          := source_tree_rec.attribute1;
2420        	       lAssignmentTbl(asg_count).attribute2          := source_tree_rec.attribute2;
2421 	       lAssignmentTbl(asg_count).attribute3          := source_tree_rec.attribute3;
2422 	       lAssignmentTbl(asg_count).attribute4          := source_tree_rec.attribute4;
2423 	       lAssignmentTbl(asg_count).attribute5          := source_tree_rec.attribute5;
2424 	       lAssignmentTbl(asg_count).attribute6          := source_tree_rec.attribute6;
2425 	       lAssignmentTbl(asg_count).attribute7          := source_tree_rec.attribute7;
2426 	       lAssignmentTbl(asg_count).attribute8          := source_tree_rec.attribute8;
2427 	       lAssignmentTbl(asg_count).attribute9          := source_tree_rec.attribute9;
2428 	       lAssignmentTbl(asg_count).attribute10         := source_tree_rec.attribute10;
2429 	       lAssignmentTbl(asg_count).attribute11         := source_tree_rec.attribute11;
2430 	       lAssignmentTbl(asg_count).attribute12         := source_tree_rec.attribute12;
2431 	       lAssignmentTbl(asg_count).attribute13         := source_tree_rec.attribute13;
2432 	       lAssignmentTbl(asg_count).attribute14         := source_tree_rec.attribute14;
2433 	       lAssignmentTbl(asg_count).attribute15         := source_tree_rec.attribute15;
2434 	       lAssignmentTbl(asg_count).attribute_category  := source_tree_rec.attribute_category;
2435 	       lAssignmentTbl(asg_count).category_id         := source_tree_rec.category_id;
2436 	       lAssignmentTbl(asg_count).category_set_id     := source_tree_rec.category_set_id;
2437 	       lAssignmentTbl(asg_count).customer_id         := source_tree_rec.customer_id;
2438 	       lAssignmentTbl(asg_count).inventory_item_id   := oss_model_lines_rec.config_item_id; /* Config item id */
2439 	       lAssignmentTbl(asg_count).organization_id     := source_tree_rec.organization_id;
2440 	       lAssignmentTbl(asg_count).secondary_inventory := source_tree_rec.secondary_inventory;
2441 	       lAssignmentTbl(asg_count).ship_to_site_id     := source_tree_rec.ship_to_site_id;
2442 	       lAssignmentTbl(asg_count).sourcing_rule_type  := source_tree_rec.sourcing_rule_type;
2443       	       lAssignmentTbl(asg_count).operation           := MRP_Globals.G_OPR_CREATE;
2444 
2445 
2446 
2447 	       l_cur_assg_id := source_tree_rec.assignment_id;
2448 	       l_new_rank_seq := 0;
2449 	       rcv_count      := 1;
2450                i              := 1;
2451                l_old_rank     :=null;
2452 
2453 	       /* Renga: There is a change to re-use pruned sourcing again here .
2454 	                 We should take care of this later */
2455 
2456    	       /* Delete all the existing data from the record structure.
2457 	          This record structure will be populated with the new sourcing
2458 	          rule information
2459 	       */
2460 
2461 
2462 
2463 	       /* The following sql will populate the data for sourcing
2464 	          rule record type
2465 	       */
2466 	       l_stmt_num := 130;
2467 
2468                l_sourcing_rule_rec := MRP_SOURCING_RULE_PUB.G_MISS_SOURCING_RULE_REC;
2469 	       select attribute1,
2470 	              attribute2,
2471 		      attribute3,
2472 		      attribute4,
2473 		      attribute5,
2474 		      attribute6,
2475    		      attribute7,
2476 		      attribute8,
2477 		      attribute9,
2478 		      attribute10,
2479 		      attribute11,
2480 		      attribute12,
2481 		      attribute13,
2482 		      attribute14,
2483 		      attribute15,
2484 		      attribute_category,
2485 		      organization_id,
2486 		      planning_active,
2487 		      'CTO*'||bom_Cto_oss_source_rule_s1.nextval,
2488 		      Sourcing_rule_type,
2489                       MRP_Globals.G_OPR_CREATE,
2490 		      1
2491 		     -- mrp_sourcing_rules_s.nextval
2492 	       Into
2493 	              l_sourcing_rule_rec.attribute1,
2494 	              l_sourcing_rule_rec.attribute2,
2495 		      l_sourcing_rule_rec.attribute3,
2496 		      l_sourcing_rule_rec.attribute4,
2497 		      l_sourcing_rule_rec.attribute5,
2498 		      l_sourcing_rule_rec.attribute6,
2499    		      l_sourcing_rule_rec.attribute7,
2500 		      l_sourcing_rule_rec.attribute8,
2501 		      l_sourcing_rule_rec.attribute9,
2502 		      l_sourcing_rule_rec.attribute10,
2503 		      l_sourcing_rule_rec.attribute11,
2504 		      l_sourcing_rule_rec.attribute12,
2505 		      l_sourcing_rule_rec.attribute13,
2506 		      l_sourcing_rule_rec.attribute14,
2507 		      l_sourcing_rule_rec.attribute15,
2508 		      l_sourcing_rule_rec.attribute_category,
2509 		      l_sourcing_rule_rec.organization_id,
2510 		      l_sourcing_rule_rec.planning_active,
2511 		      l_sourcing_rule_rec.sourcing_rule_name,
2512 		      l_sourcing_rule_rec.Sourcing_rule_type,
2513 		      l_sourcing_rule_rec.Operation,
2514 		      l_sourcing_rule_rec.status
2515 		    --  l_sourcing_rule_rec.sourcing_rule_id
2516 	       From   mrp_sourcing_rules
2517 	       where  sourcing_rule_id = source_tree_rec.sourcing_rule_id;
2518 
2519 
2520                If PG_DEBUG <> 0 Then
2521                   oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Sourcing Rule name = '||
2522                                    l_sourcing_rule_rec.sourcing_rule_name,5);
2523                End if;
2524 	       l_sourcing_rule_rec.operation := 'CREATE';
2525 
2526             End if; /* nvl(l_cur_assg_id,-1) <> source_tree_rec.assignment_id  */
2527 
2528             l_stmt_num := 140;
2529 
2530             If PG_DEBUG <> 0 Then
2531                  oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Checking l_curr_rcv_org and sr_receipt_id',5);
2532                  oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: l_curr_rcv_org = '||l_curr_rcv_org,5);
2533                  oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: source_tree_rec.sr_receipt_id = '|| source_tree_rec.sr_receipt_id,5);
2534             End if;
2535 
2536             /*
2537             Bugfix 13362916: Commenting this if condition. Because of this condition, the variable rcv_count was not
2538             incremented from 1. This variable is subsequently used in populating l_shipping_org_tbl(i).receiving_org_index
2539             value. The value populated was rcv_count - 1 (=0). This field value is used as index value to access pl/sql
2540             tables in MRP code. Since the value of index variable was 0, MRP code was failing with error:
2541             MRP_API_INV_PARENT_INDEX
2542 
2543             Even after removing this if condition, the code was failing. Consider the following scenario:
2544             BOM structure:
2545             abmodel1
2546             .abitem
2547             .abmodel2
2548             ..abitem2
2549             ..abmodel3
2550             ...abitem3
2551 
2552             Consider a parent sourcing rule like:
2553             Organization: All Orgs
2554             Transfer from M1: 30%, Rank 1
2555             Transfer from M2: 50%, Rank 1
2556             Transfer from M3: 15%, Rank 1
2557             Transfer from D1: 5%, Rank 1
2558 
2559             Consider the OSS as M1.
2560 
2561             In this case, the rule tfr from M1 will be created for all 3 config items. A new rule would get created for abmodel1*.
2562             When the same rule is attempted for abmodel2*, because of the if condition, the collection l_receiving_org_tbl is
2563             not populated causing a zero index access in l_shipping_org_tbl.
2564 
2565             In second case, consider the OSS as M1 and M2.
2566 
2567             When the if condition was removed, the code was still failing in this scenario because then we were inserting same
2568             record twice in l_receiving_org_tbl. This is invalid as per MRP and fails with unique constraint violation.
2569             */
2570 
2571             --If nvl(l_curr_rcv_org,-1) <> source_tree_rec.sr_receipt_id Then
2572             IF l_sr_receipt_id_cache_tbl.EXISTS(source_tree_rec.sr_receipt_id) = FALSE THEN
2573 
2574                sr_receipt_id_cachedloc := NULL;
2575 
2576                -- l_receiving_org_tbl    :=    MRP_SOURCING_RULE_PUB.G_MISS_RECEIVING_ORG_TBL;
2577                --  l_receiving_org_val_tbl:=    MRP_SOURCING_RULE_PUB.G_MISS_RECEIVING_ORG_VAL_TBL;
2578                --  l_shipping_org_tbl     :=    MRP_SOURCING_RULE_PUB.G_MISS_SHIPPING_ORG_TBL;
2579                --  l_shipping_org_val_tbl :=    MRP_SOURCING_RULE_PUB.G_MISS_SHIPPING_ORG_VAL_TBL;
2580                --  rcv_count := 1;
2581 	       select attribute1,
2582 	              attribute2,
2583 		      attribute3,
2584 		      attribute4,
2585 		      attribute5,
2586 		      attribute6,
2587 		      attribute7,
2588 		      attribute8,
2589 		      attribute9,
2590 		      attribute10,
2591 		      attribute11,
2592 		      attribute12,
2593 		      attribute13,
2594 		      attribute14,
2595 		      attribute15,
2596 		      attribute_category,
2597 		      disable_date,
2598                       sysdate,
2599    		      receipt_organization_id,
2600 		      MRP_Globals.G_OPR_CREATE,
2601 		      l_sourcing_rule_rec.sourcing_rule_id
2602 	       into
2603                       l_receiving_org_tbl(rcv_count).attribute1,
2604    	              l_receiving_org_tbl(rcv_count).attribute2,
2605 		      l_receiving_org_tbl(rcv_count).attribute3,
2606 		      l_receiving_org_tbl(rcv_count).attribute4,
2607 		      l_receiving_org_tbl(rcv_count).attribute5,
2608 		      l_receiving_org_tbl(rcv_count).attribute6,
2609 		      l_receiving_org_tbl(rcv_count).attribute7,
2610 		      l_receiving_org_tbl(rcv_count).attribute8,
2611 		      l_receiving_org_tbl(rcv_count).attribute9,
2612 		      l_receiving_org_tbl(rcv_count).attribute10,
2613 		      l_receiving_org_tbl(rcv_count).attribute11,
2614 		      l_receiving_org_tbl(rcv_count).attribute12,
2615 		      l_receiving_org_tbl(rcv_count).attribute13,
2616 		      l_receiving_org_tbl(rcv_count).attribute14,
2617 		      l_receiving_org_tbl(rcv_count).attribute15,
2618 		      l_receiving_org_tbl(rcv_count).attribute_category,
2619 		      l_receiving_org_tbl(rcv_count).disable_date,
2620 		      l_receiving_org_tbl(rcv_count).effective_date,
2621 		      l_receiving_org_tbl(rcv_count).receipt_organization_id,
2622 		      l_receiving_org_tbl(rcv_count).operation,
2623 		      l_receiving_org_tbl(rcv_count).sourcing_rule_id
2624 	       from  mrp_sr_receipt_org
2625 	       where sr_receipt_id = source_tree_rec.sr_receipt_id;
2626 
2627                --Bugfix 13362916
2628                l_sr_receipt_id_cache_tbl(source_tree_rec.sr_receipt_id) := rcv_count;
2629 
2630   	       rcv_count := rcv_count + 1;
2631 	       l_curr_rcv_org := source_tree_rec.sr_receipt_id;
2632 
2633 	    -- End if; /* nvl(l_curr_rcv_org,-1) <> source_tree_rec.sr_receipt_id */
2634             ELSE
2635                sr_receipt_id_cachedloc := NULL;
2636                sr_receipt_id_cachedloc := l_sr_receipt_id_cache_tbl(source_tree_rec.sr_receipt_id);
2637             END IF; /* !l_sr_receipt_id_cache_tbl.EXISTS(sr_receipt_id) */
2638 
2639             If PG_DEBUG <> 0 Then
2640  	      oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Old Rank = '||l_old_rank);
2641 	      oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: New Rank = '||source_tree_rec.rank);
2642 	    End if;
2643 
2644             l_stmt_num := 150;
2645 
2646             if nvl(l_old_rank,-1) <> source_tree_rec.rank then
2647 
2648                l_stmt_num := 160;
2649 
2650                select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
2651 	              sum(allocation)
2652 	       into   l_rank_sum
2653 	       from   bom_cto_oss_source_gt oss_src
2654 	       where  line_id = source_tree_rec.line_id
2655 	       and    source_rule_id = source_tree_rec.sourcing_rule_id
2656 	       and    rank = source_tree_rec.rank
2657 	       and    valid_flag = 'P';
2658 
2659 	       If PG_DEBUG <> 0 Then
2660                   oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Rule id  = '||source_tree_rec.sourcing_rule_id);
2661 		  oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Rank     = '||source_tree_rec.rank);
2662  		  oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Rank Sum = '||l_rank_sum);
2663 	       End if;
2664 
2665                l_new_rank_seq := l_new_rank_seq + 1;
2666 	       l_old_rank     := source_tree_rec.rank;
2667 
2668 	    End if;
2669 
2670             l_stmt_num := 170;
2671 
2672             l_shipping_org_tbl(i).allocation_percent     := source_tree_rec.allocation/l_rank_sum*100;
2673 	    If PG_DEBUG <> 0 Then
2674   	       oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: New Allocation % = '
2675                                                     ||l_shipping_org_tbl(i).allocation_percent);
2676                oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: Vendor id = '
2677                                                     ||source_tree_rec.vendor_id,5);
2678                oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULE: source org id = '
2679                                                     ||source_tree_rec.source_org_id);
2680                oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING)RULE: Sr Source id ='
2681                                                     ||source_tree_rec.sr_source_id);
2682                --Bugfix 13362916
2683                oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING)RULE: Sr Receipt id ='
2684 			                            ||source_tree_rec.SR_RECEIPT_ID);
2685 	    End if;
2686 
2687 	    l_shipping_org_tbl(i).rank                   := l_new_rank_seq;
2688 	    l_shipping_org_tbl(i).source_type            := source_tree_rec.source_type;
2689 	    l_shipping_org_tbl(i).source_organization_id := source_tree_rec.source_org_id;
2690 	    l_shipping_org_tbl(i).vendor_id              := source_tree_rec.vendor_id;
2691 
2692             -- Bug 13362916
2693  	    IF sr_receipt_id_cachedloc IS NULL THEN
2694                l_shipping_org_tbl(i).receiving_org_index    := rcv_count - 1;
2695             ELSE
2696                l_shipping_org_tbl(i).receiving_org_index := sr_receipt_id_cachedloc;
2697 	    END IF;
2698 
2699 	    /* Renga Need to work for vendor site here */
2700 
2701 
2702 	    l_stmt_num := 180;
2703 
2704 	    select  attribute1,
2705 	            attribute2,
2706 		    attribute3,
2707 	   	    attribute4,
2708 		    attribute5,
2709 		    attribute6,
2710 		    attribute7,
2711 		    attribute8,
2712 		    attribute9,
2713 		    attribute10,
2714 		    attribute11,
2715 		    attribute12,
2716 		    attribute13,
2717 		    attribute14,
2718 		    attribute15,
2719 		    attribute_category,
2720 		    secondary_inventory,
2721 		    ship_method,
2722 		    MRP_Globals.G_OPR_CREATE,
2723 		    NVL(sr_receipt_id_cachedloc, rcv_count-1), --Bugfix 13362916
2724 		    vendor_site_id
2725 	     into
2726 	            l_shipping_org_tbl(i).attribute1,
2727 	            l_shipping_org_tbl(i).attribute2,
2728 		    l_shipping_org_tbl(i).attribute3,
2729 		    l_shipping_org_tbl(i).attribute4,
2730 		    l_shipping_org_tbl(i).attribute5,
2731 		    l_shipping_org_tbl(i).attribute6,
2732 		    l_shipping_org_tbl(i).attribute7,
2733 		    l_shipping_org_tbl(i).attribute8,
2734 		    l_shipping_org_tbl(i).attribute9,
2735 		    l_shipping_org_tbl(i).attribute10,
2736 		    l_shipping_org_tbl(i).attribute11,
2737 		    l_shipping_org_tbl(i).attribute12,
2738 		    l_shipping_org_tbl(i).attribute13,
2739 		    l_shipping_org_tbl(i).attribute14,
2740 		    l_shipping_org_tbl(i).attribute15,
2741 		    l_shipping_org_tbl(i).attribute_category,
2742 		    l_shipping_org_tbl(i).secondary_inventory,
2743 		    l_shipping_org_tbl(i).ship_method,
2744 		    l_shipping_org_tbl(i).operation,
2745 		    l_shipping_org_tbl(i).receiving_org_index,
2746 		    l_shipping_org_tbl(i).vendor_site_id
2747 	     from   mrp_sr_source_org
2748 	     where  sr_source_id = source_tree_rec.sr_source_id;
2749 
2750              oe_debug_pub.add('Vendor site id inserted = '||l_shipping_org_tbl(i).vendor_site_id,5);
2751 	     i := i + 1;
2752 
2753              --Bugfix 12917456
2754              sr_receipt_id_cachedloc := NULL;
2755       End Loop;
2756 
2757 
2758 
2759 
2760       /* this for the last record which will come out of loop
2761       */
2762 
2763 
2764       l_stmt_num := 190;
2765 
2766       If l_cur_line_id <> 0 then
2767          -- bug 13362916
2768          --
2769          If PG_DEBUG <> 0 Then
2770             oe_debug_pub.add(lpad(' ',g_pg_level)||'Before Calling the MRP API ');
2771             oe_debug_pub.add(lpad(' ',g_pg_level)||'Printing data of l_sourcing_rule_rec ');
2772             oe_debug_pub.add(lpad(' ',g_pg_level)||'==============================================');
2773             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Sourcing_Rule_Id :'||l_sourcing_rule_rec.Sourcing_Rule_Id );
2774             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute1 :'||l_sourcing_rule_rec.Attribute1 );
2775             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute10 :'||l_sourcing_rule_rec.Attribute10 );
2776             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute11 :'||l_sourcing_rule_rec.Attribute11 );
2777             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute12 :'||l_sourcing_rule_rec.Attribute12 );
2778             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute13 :'||l_sourcing_rule_rec.Attribute13 );
2779             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute14 :'||l_sourcing_rule_rec.Attribute14 );
2780             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute15 :'||l_sourcing_rule_rec.Attribute15 );
2781             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute2 :'||l_sourcing_rule_rec.Attribute2 );
2782             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute3 :'||l_sourcing_rule_rec.Attribute3 );
2783             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute4 :'||l_sourcing_rule_rec.Attribute4 );
2784             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute5 :'||l_sourcing_rule_rec.Attribute5 );
2785             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute6 :'||l_sourcing_rule_rec.Attribute6 );
2786             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute7 :'||l_sourcing_rule_rec.Attribute7 );
2787             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute8 :'||l_sourcing_rule_rec.Attribute8 );
2788             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute9 :'||l_sourcing_rule_rec.Attribute9 );
2789             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Attribute_Category :'||l_sourcing_rule_rec.Attribute_Category );
2790             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Created_By :'||l_sourcing_rule_rec.Created_By );
2791             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Creation_Date :'||l_sourcing_rule_rec.Creation_Date );
2792             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Description :'||l_sourcing_rule_rec.Description );
2793             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Last_Updated_By :'||l_sourcing_rule_rec.Last_Updated_By );
2794             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Last_Update_Date :'||l_sourcing_rule_rec.Last_Update_Date );
2795             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Last_Update_Login :'||l_sourcing_rule_rec.Last_Update_Login );
2796             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Organization_Id :'||l_sourcing_rule_rec.Organization_Id );
2797             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Planning_Active :'||l_sourcing_rule_rec.Planning_Active );
2798             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Program_Application_Id :'||l_sourcing_rule_rec.Program_Application_Id );
2799             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Program_Id :'||l_sourcing_rule_rec.Program_Id );
2800             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Program_Update_Date :'||l_sourcing_rule_rec.Program_Update_Date );
2801             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Request_Id :'||l_sourcing_rule_rec.Request_Id );
2802             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Sourcing_Rule_Name :'||l_sourcing_rule_rec.Sourcing_Rule_Name );
2803             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Sourcing_Rule_Type :'||l_sourcing_rule_rec.Sourcing_Rule_Type );
2804             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.Status :'||l_sourcing_rule_rec.Status );
2805             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.return_status :'||l_sourcing_rule_rec.return_status );
2806             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.db_flag :'||l_sourcing_rule_rec.db_flag );
2807             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_rec.operation :'||l_sourcing_rule_rec.operation );
2808             oe_debug_pub.add(lpad(' ',g_pg_level)||'==============================================');
2809 
2810             oe_debug_pub.add(lpad(' ',g_pg_level)||'l_sourcing_rule_val_rec.null_element :'|| l_sourcing_rule_val_rec.null_element);
2811 
2812             oe_debug_pub.add(lpad(' ',g_pg_level)||'Printing data of l_receiving_org_tbl count :'|| l_receiving_org_tbl.count);
2813             oe_debug_pub.add(lpad(' ',g_pg_level)||'--------------------------------------------------------------------------');
2814 
2815             FOR debug_cntr in 1..l_receiving_org_tbl.count LOOP
2816                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Sr_Receipt_Id :' || l_receiving_org_tbl(debug_cntr).Sr_Receipt_Id );
2817                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute1 :' || l_receiving_org_tbl(debug_cntr).Attribute1 );
2818                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute10 :' || l_receiving_org_tbl(debug_cntr).Attribute10 );
2819                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute11 :' || l_receiving_org_tbl(debug_cntr).Attribute11 );
2820                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute12 :' || l_receiving_org_tbl(debug_cntr).Attribute12 );
2821                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute13 :' || l_receiving_org_tbl(debug_cntr).Attribute13 );
2822                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute14 :' || l_receiving_org_tbl(debug_cntr).Attribute14 );
2823                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute15 :' || l_receiving_org_tbl(debug_cntr).Attribute15 );
2824                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute2 :' || l_receiving_org_tbl(debug_cntr).Attribute2 );
2825                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute3 :' || l_receiving_org_tbl(debug_cntr).Attribute3 );
2826                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute4 :' || l_receiving_org_tbl(debug_cntr).Attribute4 );
2827                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute5 :' || l_receiving_org_tbl(debug_cntr).Attribute5 );
2828                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute6 :' || l_receiving_org_tbl(debug_cntr).Attribute6 );
2829                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute7 :' || l_receiving_org_tbl(debug_cntr).Attribute7 );
2830                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute8 :' || l_receiving_org_tbl(debug_cntr).Attribute8 );
2831                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute9 :' || l_receiving_org_tbl(debug_cntr).Attribute9 );
2832                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Attribute_Category :' || l_receiving_org_tbl(debug_cntr).Attribute_Category );
2833                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Created_By :' || l_receiving_org_tbl(debug_cntr).Created_By );
2834                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Creation_Date :' || l_receiving_org_tbl(debug_cntr).Creation_Date );
2835                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Disable_Date :' || l_receiving_org_tbl(debug_cntr).Disable_Date );
2836                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Effective_Date :' || l_receiving_org_tbl(debug_cntr).Effective_Date );
2837                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Last_Updated_By :' || l_receiving_org_tbl(debug_cntr).Last_Updated_By );
2838                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Last_Update_Date :' || l_receiving_org_tbl(debug_cntr).Last_Update_Date );
2839                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Last_Update_Login :' || l_receiving_org_tbl(debug_cntr).Last_Update_Login );
2840                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Program_Application_Id :' || l_receiving_org_tbl(debug_cntr).Program_Application_Id );
2841                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Program_Id :' || l_receiving_org_tbl(debug_cntr).Program_Id );
2842                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Program_Update_Date :' || l_receiving_org_tbl(debug_cntr).Program_Update_Date );
2843                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Receipt_Organization_Id :' || l_receiving_org_tbl(debug_cntr).Receipt_Organization_Id );
2844                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Request_Id :' || l_receiving_org_tbl(debug_cntr).Request_Id );
2845                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').Sourcing_Rule_Id :' || l_receiving_org_tbl(debug_cntr).Sourcing_Rule_Id );
2846                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').return_status :' || l_receiving_org_tbl(debug_cntr).return_status );
2847                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').db_flag :' || l_receiving_org_tbl(debug_cntr).db_flag );
2848                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_tbl('||debug_cntr||').operation :' || l_receiving_org_tbl(debug_cntr).operation );
2849             END LOOP;
2850             oe_debug_pub.add(lpad(' ',g_pg_level)||'--------------------------------------------------------------------------');
2851 
2852             oe_debug_pub.add(lpad(' ',g_pg_level)||'Printing data of l_receiving_org_val_tbl count :'|| l_receiving_org_val_tbl.count);
2853             oe_debug_pub.add(lpad(' ',g_pg_level)||'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
2854 
2855             FOR debug_cntr2 in 1..l_receiving_org_val_tbl.count LOOP
2856                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_receiving_org_val_tbl('||debug_cntr2||').null_element :' || l_receiving_org_val_tbl(debug_cntr2).null_element );
2857             END LOOP;
2858 
2859             oe_debug_pub.add(lpad(' ',g_pg_level)||'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
2860 
2861             oe_debug_pub.add(lpad(' ',g_pg_level)||'Printing data of l_shipping_org_tbl count :'|| l_shipping_org_tbl.count);
2862             oe_debug_pub.add(lpad(' ',g_pg_level)||'**********************************************************************************');
2863 
2864             FOR debug_cntr3 in 1..l_shipping_org_tbl.count LOOP
2865                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Sr_Source_Id :' || l_shipping_org_tbl(debug_cntr3).Sr_Source_Id );
2866                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Allocation_Percent :' || l_shipping_org_tbl(debug_cntr3).Allocation_Percent );
2867                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute1 :' || l_shipping_org_tbl(debug_cntr3).Attribute1 );
2868                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute10 :' || l_shipping_org_tbl(debug_cntr3).Attribute10 );
2869                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute11 :' || l_shipping_org_tbl(debug_cntr3).Attribute11 );
2870                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute12 :' || l_shipping_org_tbl(debug_cntr3).Attribute12 );
2871                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute13 :' || l_shipping_org_tbl(debug_cntr3).Attribute13 );
2872                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute14 :' || l_shipping_org_tbl(debug_cntr3).Attribute14 );
2873                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute15 :' || l_shipping_org_tbl(debug_cntr3).Attribute15 );
2874                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute2 :' || l_shipping_org_tbl(debug_cntr3).Attribute2 );
2875                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute3 :' || l_shipping_org_tbl(debug_cntr3).Attribute3 );
2876                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute4 :' || l_shipping_org_tbl(debug_cntr3).Attribute4 );
2877                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute5 :' || l_shipping_org_tbl(debug_cntr3).Attribute5 );
2878                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute6 :' || l_shipping_org_tbl(debug_cntr3).Attribute6 );
2879                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute7 :' || l_shipping_org_tbl(debug_cntr3).Attribute7 );
2880                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute8 :' || l_shipping_org_tbl(debug_cntr3).Attribute8 );
2881                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute9 :' || l_shipping_org_tbl(debug_cntr3).Attribute9 );
2882                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Attribute_Category :' || l_shipping_org_tbl(debug_cntr3).Attribute_Category );
2883                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Created_By :' || l_shipping_org_tbl(debug_cntr3).Created_By );
2884                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Creation_Date :' || l_shipping_org_tbl(debug_cntr3).Creation_Date );
2885                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Last_Updated_By :' || l_shipping_org_tbl(debug_cntr3).Last_Updated_By );
2886                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Last_Update_Date :' || l_shipping_org_tbl(debug_cntr3).Last_Update_Date );
2887                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Last_Update_Login :' || l_shipping_org_tbl(debug_cntr3).Last_Update_Login );
2888                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Program_Application_Id :' || l_shipping_org_tbl(debug_cntr3).Program_Application_Id );
2889                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Program_Id :' || l_shipping_org_tbl(debug_cntr3).Program_Id );
2890                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Program_Update_Date :' || l_shipping_org_tbl(debug_cntr3).Program_Update_Date );
2891                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Rank :' || l_shipping_org_tbl(debug_cntr3).Rank );
2892                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Request_Id :' || l_shipping_org_tbl(debug_cntr3).Request_Id );
2893                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Secondary_Inventory :' || l_shipping_org_tbl(debug_cntr3).Secondary_Inventory );
2894                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Ship_Method :' || l_shipping_org_tbl(debug_cntr3).Ship_Method );
2895                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Source_Organization_Id :' || l_shipping_org_tbl(debug_cntr3).Source_Organization_Id );
2896                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Source_Type :' || l_shipping_org_tbl(debug_cntr3).Source_Type );
2897                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Sr_Receipt_Id :' || l_shipping_org_tbl(debug_cntr3).Sr_Receipt_Id );
2898                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Vendor_Id :' || l_shipping_org_tbl(debug_cntr3).Vendor_Id );
2899                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Vendor_Site_Id :' || l_shipping_org_tbl(debug_cntr3).Vendor_Site_Id );
2900                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').return_status :' || l_shipping_org_tbl(debug_cntr3).return_status );
2901                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').db_flag :' || l_shipping_org_tbl(debug_cntr3).db_flag );
2902                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').operation :' || l_shipping_org_tbl(debug_cntr3).operation );
2903                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_tbl('||debug_cntr3||').Receiving_Org_index :' || l_shipping_org_tbl(debug_cntr3).Receiving_Org_index );
2904             END LOOP;
2905 
2906             oe_debug_pub.add(lpad(' ',g_pg_level)||'**********************************************************************************');
2907 
2908             oe_debug_pub.add(lpad(' ',g_pg_level)||'Printing data of l_shipping_org_val_tbl count :'|| l_shipping_org_val_tbl.count);
2909             oe_debug_pub.add(lpad(' ',g_pg_level)||'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
2910 
2911             FOR debug_cntr4 in 1..l_shipping_org_val_tbl.count LOOP
2912                 oe_debug_pub.add(lpad(' ',g_pg_level)||'l_shipping_org_val_tbl('||debug_cntr4||').null_element :' || l_shipping_org_val_tbl(debug_cntr4).null_element );
2913             END LOOP;
2914 
2915             oe_debug_pub.add(lpad(' ',g_pg_level)||'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
2916          END If;
2917 
2918          MRP_SOURCING_RULE_PUB.PROCESS_SOURCING_RULE(
2919 	                                      p_api_version_number    => 1.0,
2920 					      p_return_values         => FND_API.G_TRUE,
2921 					      p_sourcing_rule_rec     => l_sourcing_rule_rec,
2922 					      p_sourcing_rule_val_rec => l_sourcing_rule_val_rec,
2923 					      p_receiving_org_tbl     => l_receiving_org_tbl,
2924 					      p_receiving_org_val_tbl => l_receiving_org_val_tbl,
2925 					      p_shipping_org_tbl      => l_shipping_org_tbl,
2926 					      p_shipping_org_val_tbl  => l_shipping_org_val_tbl,
2927 					      x_sourcing_rule_rec     => x_sourcing_rule_rec,
2928 					      x_sourcing_rule_val_rec => x_sourcing_rule_val_rec,
2929 					      x_receiving_org_tbl     => x_receiving_org_tbl,
2930 					      x_receiving_org_val_tbl => x_receiving_org_val_tbl,
2931   				              x_shipping_org_tbl      => x_shipping_org_tbl,
2932 					      x_shipping_org_val_tbl  => x_shipping_org_val_tbl,
2933 					      x_return_status         => x_return_status,
2934 					      x_msg_count             => x_msg_count,
2935 					      x_msg_data              => x_msg_data);
2936 
2937          FOR l_index IN 1..x_msg_count LOOP
2938              l_msg_data := fnd_msg_pub.get(
2939                       p_msg_index => l_index,
2940                       p_encoded  => FND_API.G_FALSE);
2941              oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: error : '||substr(l_msg_data,1,250));
2942          END LOOP;
2943 
2944          If x_return_status  = FND_API.G_RET_STS_ERROR Then
2945             IF PG_DEBUG <> 0 Then
2946                oe_debug_pub.add(lpad(' ',g_pg_level)||
2947 	                               'CREATE_OSS_SOURCING_RULES: Exepected error occurred in update_oss_in_bcol API',5);
2948             End if;
2949             raise FND_API.G_EXC_ERROR;
2950          elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
2951             IF PG_DEBUG <> 0 Then
2952                oe_debug_pub.add(lpad(' ',g_pg_level)||
2953 	                         'CREATE_OSS_SOURCING_RULES: Un Exepected error occurred in update_oss_in_bcol API',5);
2954             End if;
2955             raise FND_API.G_EXC_UNEXPECTED_ERROR;
2956          End if;
2957 
2958          If PG_DEBUG <> 0 Then
2959             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: New sourcing Rule created  = '
2960                                                  ||x_sourcing_rule_rec.sourcing_rule_id,5);
2961          End if;
2962 
2963          lAssignmentTbl(asg_count).sourcing_rule_id    := x_sourcing_rule_rec.sourcing_rule_id;
2964          l_assignment_set_id                           := lAssignmentTbl(asg_count).assignment_set_id;
2965          Asg_count := asg_count + 1;
2966          l_receiving_org_tbl.delete;
2967          l_receiving_org_val_tbl.delete;
2968          l_shipping_org_tbl.delete;
2969          l_shipping_org_val_tbl.delete;
2970          l_new_rank_seq := 0;
2971          rcv_count      := 1;
2972          i              := 1;
2973          l_old_rank     :=null;
2974 
2975       End if;
2976 
2977 
2978       /* Now this is the time to generate the assignments which we have created so far */
2979       /* Now we will create 100% make rule if anytning needs to be created. first we will check
2980          if we need to create 100% make at rule or not. When we have inserted a row for 100% make at
2981          we will insert with source_rule_id as null. Check if there is any row exists with that .
2982       */
2983 
2984       l_stmt_num := 200;
2985 
2986       If PG_DEBUG <> 0 Then
2987         oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: '||'Before make orgs loop ',5);
2988       End if;
2989 
2990       For oss_make_orgs in
2991 oss_make_orgs_cur(oss_model_lines_rec.line_id,oss_model_lines_rec.config_item_id)
2992       Loop
2993 
2994 
2995          oe_debug_pub.add('Inside make at rule loop ',5);
2996 	 l_stmt_num := 210;
2997 
2998          l_sourcing_rule_rec := MRP_SOURCING_RULE_PUB.G_MISS_SOURCING_RULE_REC;
2999          l_sourcing_rule_rec.organization_id := oss_make_orgs.rcv_org_id;
3000          l_sourcing_rule_rec.status := 1;
3001          l_sourcing_rule_rec.planning_active := 1;
3002          select 'CTO*'||bom_cto_oss_source_rule_s1.nextval
3003          into    l_sourcing_rule_rec.sourcing_rule_name
3004          from    dual;
3005 
3006          l_sourcing_rule_rec.Sourcing_rule_type := 1;
3007 
3008 /*
3009          select mrp_sourcing_rules_s.nextval
3010          into   l_sourcing_rule_rec.sourcing_rule_id
3011          from dual;
3012 */
3013 
3014          l_sourcing_rule_rec.operation := MRP_Globals.G_OPR_CREATE;
3015 
3016          l_receiving_org_tbl.delete;
3017 
3018 
3019          l_receiving_org_tbl(1).effective_date := sysdate;
3020 
3021          l_receiving_org_tbl(1).receipt_organization_id := oss_make_orgs.rcv_org_id;
3022        --  l_receiving_org_tbl(1).sourcing_rule_id        := l_sourcing_rule_rec.sourcing_rule_id;
3023          l_receiving_org_tbl(1).operation               := MRP_Globals.G_OPR_CREATE;
3024 
3025 
3026          l_shipping_org_tbl.delete;
3027 
3028 
3029 
3030 
3031          l_shipping_org_tbl(1).allocation_percent     := oss_make_orgs.allocation;
3032          l_shipping_org_tbl(1).rank                   := oss_make_orgs.rank;
3033          l_shipping_org_tbl(1).source_organization_id := oss_make_orgs.source_org_id;
3034          l_shipping_org_tbl(1).Source_type            := 2;
3035          l_shipping_org_tbl(1).receiving_org_index    := 1;
3036          l_shipping_org_tbl(1).operation              := MRP_Globals.G_OPR_CREATE;
3037 
3038 
3039 
3040          l_stmt_num := 220;
3041 
3042          MRP_SOURCING_RULE_PUB.PROCESS_SOURCING_RULE(
3043 	                                      p_api_version_number    => 1.0,
3044 					      p_return_values         => FND_API.G_TRUE,
3045 					      p_commit                => FND_API.G_FALSE,
3046 					      p_sourcing_rule_rec     => l_sourcing_rule_rec,
3047 					      p_sourcing_rule_val_rec => l_sourcing_rule_val_rec,
3048 					      p_receiving_org_tbl     => l_receiving_org_tbl,
3049 					      p_receiving_org_val_tbl => l_receiving_org_val_tbl,
3050 					      p_shipping_org_tbl      => l_shipping_org_tbl,
3051 					      p_shipping_org_val_tbl  => l_shipping_org_val_tbl,
3052 					      x_sourcing_rule_rec     => x_sourcing_rule_rec,
3053 					      x_sourcing_rule_val_rec => x_sourcing_rule_val_rec,
3054 					      x_receiving_org_tbl     => x_receiving_org_tbl,
3055 					      x_receiving_org_val_tbl => x_receiving_org_val_tbl,
3056   				              x_shipping_org_tbl      => x_shipping_org_tbl,
3057 					      x_shipping_org_val_tbl  => x_shipping_org_val_tbl,
3058 					      x_return_status         => x_return_status,
3059 					      x_msg_count             => x_msg_count,
3060 					      x_msg_data              => x_msg_data);
3061 
3062          If x_return_status  = FND_API.G_RET_STS_ERROR Then
3063             IF PG_DEBUG <> 0 Then
3064                oe_debug_pub.add(lpad(' ',g_pg_level)||
3065 	                               'CREATE_OSS_SOURCING_RULES: Exepected error occurred in update_oss_in_bcol API',5);
3066             End if;
3067             raise FND_API.G_EXC_ERROR;
3068          elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3069             IF PG_DEBUG <> 0 Then
3070                oe_debug_pub.add(lpad(' ',g_pg_level)||
3071 	                         'CREATE_OSS_SOURCING_RULES: Un Exepected error occurred in update_oss_in_bcol API',5);
3072             End if;
3073             raise FND_API.G_EXC_UNEXPECTED_ERROR;
3074          End if;
3075 
3076 
3077          If PG_DEBUG <> 0 Then
3078             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: Loading the assignment into assignment record ',5);
3079             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCNIG_RULES: assignment_type = '
3080                                                  ||'6',5);
3081             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: inventory item id = '
3082                                                  ||oss_model_lines_rec.config_item_id,5);
3083             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: organization id = '
3084                                                  ||oss_make_orgs.rcv_org_id,5);
3085 
3086 
3087          End if;
3088 
3089          lAssignmentTbl(asg_count).assignment_set_id   := G_def_assg_set;
3090          lAssignmentTbl(asg_count).assignment_type     := 6;
3091          lAssignmentTbl(asg_count).inventory_item_id   := oss_model_lines_rec.config_item_id;
3092          lAssignmentTbl(asg_count).organization_id     := oss_make_orgs.rcv_org_id;
3093          lAssignmentTbl(asg_count).sourcing_rule_type  := 1;
3094          lAssignmentTbl(asg_count).sourcing_rule_id    := x_sourcing_rule_rec.sourcing_rule_id;
3095          lAssignmentTbl(asg_count).operation           := MRP_Globals.G_OPR_CREATE;
3096 
3097          asg_count := asg_count + 1;
3098 
3099 
3100 
3101       End Loop;
3102 
3103       l_stmt_num := 230;
3104 
3105 
3106       If PG_DEBUG <> 0 Then
3107          oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: '||'Before reuse org loop ',5);
3108       End if;
3109 
3110       For oss_reused_assg_rec in
3111 oss_reused_assg(oss_model_lines_rec.line_id,oss_model_lines_rec.config_item_id)
3112       Loop
3113 
3114 
3115          If PG_DEBUG <> 0 Then
3116             oe_debug_pub.add(lpad(' ',g_pg_level)||'Inside Reuse assignments loop',5);
3117 	    oe_debug_pub.add(lpad(' ',g_pg_level)||'Assignment id = '||oss_reused_assg_rec.assignment_id,5);
3118 	 End if;
3119 
3120          l_Stmt_Num := 240;
3121 
3122          --
3123          -- bug 6617686
3124          -- The MRP API uses a  ASSIGNMENT_ID = p_Assignment_Id OR
3125          -- ASSIGNMENT_SET_ID = p_Assignment_Set_Id that leads to
3126          -- a full table scan on MRP_SR_ASSIGNMENTS and consequent
3127          -- performance issues. Since CTO does not pass ASSIGNMENT_SET_ID
3128          -- into the procedure, it is performance effective to directly
3129          -- query the MRP table
3130          -- ntungare
3131          --
3132          -- lAssignmentRec := MRP_Assignment_Handlers.Query_Row(oss_reused_assg_rec.assignment_id);
3133 
3134          SELECT  ASSIGNMENT_ID
3135          ,       ASSIGNMENT_SET_ID
3136          ,       ASSIGNMENT_TYPE
3137          ,       ATTRIBUTE1
3138          ,       ATTRIBUTE10
3139          ,       ATTRIBUTE11
3140          ,       ATTRIBUTE12
3141          ,       ATTRIBUTE13
3142          ,       ATTRIBUTE14
3143          ,       ATTRIBUTE15
3144          ,       ATTRIBUTE2
3145          ,       ATTRIBUTE3
3146          ,       ATTRIBUTE4
3147          ,       ATTRIBUTE5
3148          ,       ATTRIBUTE6
3149          ,       ATTRIBUTE7
3150          ,       ATTRIBUTE8
3151          ,       ATTRIBUTE9
3152          ,       ATTRIBUTE_CATEGORY
3153          ,       CATEGORY_ID
3154          ,       CATEGORY_SET_ID
3155          ,       CREATED_BY
3156          ,       CREATION_DATE
3157          ,       CUSTOMER_ID
3158          ,       INVENTORY_ITEM_ID
3159          ,       LAST_UPDATED_BY
3160          ,       LAST_UPDATE_DATE
3161          ,       LAST_UPDATE_LOGIN
3162          ,       ORGANIZATION_ID
3163          ,       PROGRAM_APPLICATION_ID
3164          ,       PROGRAM_ID
3165          ,       PROGRAM_UPDATE_DATE
3166          ,       REQUEST_ID
3167          ,       SECONDARY_INVENTORY
3168          ,       SHIP_TO_SITE_ID
3169          ,       SOURCING_RULE_ID
3170          ,       SOURCING_RULE_TYPE
3171          into    lAssignmentRec.ASSIGNMENT_ID
3172          ,       lAssignmentRec.ASSIGNMENT_SET_ID
3173          ,       lAssignmentRec.ASSIGNMENT_TYPE
3174          ,       lAssignmentRec.ATTRIBUTE1
3175          ,       lAssignmentRec.ATTRIBUTE10
3176          ,       lAssignmentRec.ATTRIBUTE11
3177          ,       lAssignmentRec.ATTRIBUTE12
3178          ,       lAssignmentRec.ATTRIBUTE13
3179          ,       lAssignmentRec.ATTRIBUTE14
3180          ,       lAssignmentRec.ATTRIBUTE15
3181          ,       lAssignmentRec.ATTRIBUTE2
3182          ,       lAssignmentRec.ATTRIBUTE3
3183          ,       lAssignmentRec.ATTRIBUTE4
3184          ,       lAssignmentRec.ATTRIBUTE5
3185          ,       lAssignmentRec.ATTRIBUTE6
3186          ,       lAssignmentRec.ATTRIBUTE7
3187          ,       lAssignmentRec.ATTRIBUTE8
3188          ,       lAssignmentRec.ATTRIBUTE9
3189          ,       lAssignmentRec.ATTRIBUTE_CATEGORY
3190          ,       lAssignmentRec.CATEGORY_ID
3191          ,       lAssignmentRec.CATEGORY_SET_ID
3192          ,       lAssignmentRec.CREATED_BY
3193          ,       lAssignmentRec.CREATION_DATE
3194          ,       lAssignmentRec.CUSTOMER_ID
3195          ,       lAssignmentRec.INVENTORY_ITEM_ID
3196          ,       lAssignmentRec.LAST_UPDATED_BY
3197          ,       lAssignmentRec.LAST_UPDATE_DATE
3198          ,       lAssignmentRec.LAST_UPDATE_LOGIN
3199          ,       lAssignmentRec.ORGANIZATION_ID
3200          ,       lAssignmentRec.PROGRAM_APPLICATION_ID
3201          ,       lAssignmentRec.PROGRAM_ID
3202          ,       lAssignmentRec.PROGRAM_UPDATE_DATE
3203          ,       lAssignmentRec.REQUEST_ID
3204          ,       lAssignmentRec.SECONDARY_INVENTORY
3205          ,       lAssignmentRec.SHIP_TO_SITE_ID
3206          ,       lAssignmentRec.SOURCING_RULE_ID
3207          ,       lAssignmentRec.SOURCING_RULE_TYPE
3208          FROM    MRP_SR_ASSIGNMENTS
3209          WHERE   ASSIGNMENT_ID = oss_reused_assg_rec.assignment_id;
3210 
3211          If PG_DEBUG <> 0 Then
3212             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: Loading the assignment into assignment record ',5);
3213             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCNIG_RULES: assignment_type = '
3214                                                  ||lAssignmentRec.Assignment_Type,5);
3215             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: inventory item id = '
3216                                                  ||oss_model_lines_rec.config_item_id,5);
3217             oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: organization id = '
3218                                                  ||lAssignmentRec.Organization_Id,5);
3219          End if;
3220 
3221          l_Stmt_Num := 245;
3222          lAssignmentTbl(asg_count).Assignment_Set_Id     := G_def_assg_set;
3223          lAssignmentTbl(asg_count).Assignment_Type       := lAssignmentRec.Assignment_Type;
3224          lAssignmentTbl(asg_count).Attribute1            := lAssignmentRec.Attribute1;
3225          lAssignmentTbl(asg_count).Attribute10           := lAssignmentRec.Attribute10;
3226          lAssignmentTbl(asg_count).Attribute11           := lAssignmentRec.Attribute11;
3227          lAssignmentTbl(asg_count).Attribute12           := lAssignmentRec.Attribute12;
3228          lAssignmentTbl(asg_count).Attribute13           := lAssignmentRec.Attribute13;
3229          lAssignmentTbl(asg_count).Attribute14           := lAssignmentRec.Attribute14;
3230          lAssignmentTbl(asg_count).Attribute15           := lAssignmentRec.Attribute15;
3231          lAssignmentTbl(asg_count).Attribute2            := lAssignmentRec.Attribute2;
3232          lAssignmentTbl(asg_count).Attribute3            := lAssignmentRec.Attribute3;
3233          lAssignmentTbl(asg_count).Attribute4            := lAssignmentRec.Attribute4;
3234          lAssignmentTbl(asg_count).Attribute5            := lAssignmentRec.Attribute5;
3235          lAssignmentTbl(asg_count).Attribute6            := lAssignmentRec.Attribute6;
3236          lAssignmentTbl(asg_count).Attribute7            := lAssignmentRec.Attribute7;
3237          lAssignmentTbl(asg_count).Attribute8            := lAssignmentRec.Attribute8;
3238          lAssignmentTbl(asg_count).Attribute9            := lAssignmentRec.Attribute9;
3239          lAssignmentTbl(asg_count).Attribute_Category    := lAssignmentRec.Attribute_Category;
3240          lAssignmentTbl(asg_count).Category_Id           := lAssignmentRec.Category_Id ;
3241          lAssignmentTbl(asg_count).Category_Set_Id       := lAssignmentRec.Category_Set_Id;
3242          lAssignmentTbl(asg_count).Created_By            := lAssignmentRec.Created_By;
3243          lAssignmentTbl(asg_count).Creation_Date         := lAssignmentRec.Creation_Date;
3244          lAssignmentTbl(asg_count).Customer_Id           := lAssignmentRec.Customer_Id;
3245          lAssignmentTbl(asg_count).Inventory_Item_Id     := oss_model_lines_rec.config_item_id;
3246          lAssignmentTbl(asg_count).Last_Updated_By       := lAssignmentRec.Last_Updated_By;
3247          lAssignmentTbl(asg_count).Last_Update_Date      := lAssignmentRec.Last_Update_Date;
3248          lAssignmentTbl(asg_count).Last_Update_Login     := lAssignmentRec.Last_Update_Login;
3249          lAssignmentTbl(asg_count).Organization_Id       := lAssignmentRec.Organization_Id;
3250          lAssignmentTbl(asg_count).Program_Application_Id:= lAssignmentRec.Program_Application_Id;
3251          lAssignmentTbl(asg_count).Program_Id            := lAssignmentRec.Program_Id;
3252          lAssignmentTbl(asg_count).Program_Update_Date   := lAssignmentRec.Program_Update_Date;
3253          lAssignmentTbl(asg_count).Request_Id            := lAssignmentRec.Request_Id;
3254          lAssignmentTbl(asg_count).Secondary_Inventory   := lAssignmentRec.Secondary_Inventory;
3255          lAssignmentTbl(asg_count).Ship_To_Site_Id       := lAssignmentRec.Ship_To_Site_Id;
3256          lAssignmentTbl(asg_count).Sourcing_Rule_Id      := lAssignmentRec.Sourcing_Rule_Id;
3257          lAssignmentTbl(asg_count).Sourcing_Rule_Type    := lAssignmentRec.Sourcing_Rule_Type;
3258          lAssignmentTbl(asg_count).return_status         := NULL;
3259          lAssignmentTbl(asg_count).db_flag               := NULL;
3260          lAssignmentTbl(asg_count).operation             := MRP_Globals.G_OPR_CREATE;
3261 
3262          asg_count := asg_count + 1;
3263 
3264 
3265       End Loop; /* oss_reused_assg_rec in oss_reused_assg(oss_model_lines_rec.line_id) */
3266 
3267     --Bugfix 13324638
3268      <<loop_oss_model_lines>>
3269      EXIT WHEN oss_model_lines%NOTFOUND;
3270 
3271    End Loop; /*oss_model_lines_rec in oss_model_lines */
3272 
3273 
3274 
3275 
3276 
3277 
3278    l_stmt_num := 250;
3279 
3280    If pg_debug <> 0 then
3281      oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: assignment count = '|| lAssignmentTbl.count,5);
3282    End if;
3283 
3284 
3285    If lAssignmentTbl.count <> 0 Then
3286    MRP_Src_Assignment_PUB.Process_Assignment
3287 		(   p_api_version_number	=> 1.0
3288 		,   x_return_status		=> x_return_status
3289 		,   x_msg_count 		=> x_msg_count
3290 		,   x_msg_data  		=> x_msg_data
3291 		,   p_Assignment_Set_rec 	=> lAssignmentSetRec
3292 		,   p_Assignment_tbl  		=> lAssignmentTbl
3293 		,   x_Assignment_Set_rec  	=> xAssignmentSetRec
3294 		,   x_Assignment_Set_val_rec	=> xAssignmentSetValRec
3295 		,   x_Assignment_tbl   		=> xAssignmentTbl
3296 		,   x_Assignment_val_tbl  	=> xAssignmentValTbl
3297 		);
3298     FOR l_index IN 1..x_msg_count LOOP
3299        l_msg_data := fnd_msg_pub.get(
3300                       p_msg_index => l_index,
3301                       p_encoded  => FND_API.G_FALSE);
3302        oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES: error : '||substr(l_msg_data,1,250));
3303    END LOOP;
3304    If x_return_status  = FND_API.G_RET_STS_ERROR Then
3305       IF PG_DEBUG <> 0 Then
3306          oe_debug_pub.add(lpad(' ',g_pg_level)||
3307 	                               'CREATE_OSS_SOURCING_RULES: Exepected error occurred in update_oss_in_bcol API',5);
3308       End if;
3309       raise FND_API.G_EXC_ERROR;
3310    elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3311       IF PG_DEBUG <> 0 Then
3312           oe_debug_pub.add(lpad(' ',g_pg_level)||
3313 	                         'CREATE_OSS_SOURCING_RULES: Un Exepected error occurred in update_oss_in_bcol API',5);
3314       End if;
3315       raise FND_API.G_EXC_UNEXPECTED_ERROR;
3316    End if;
3317 
3318    End if;
3319 
3320    If p_mode = 'UPG' then
3321 
3322       update mtl_system_items msi
3323       set    msi.option_specific_sourced = (select bcol.option_specific
3324                                             from   bom_cto_order_lines_upg bcol
3325                                             where  bcol.ato_line_id= p_ato_line_id
3326                                             and    bcol.config_item_id = msi.inventory_item_id
3327                                            )
3328          --Bugfix 12917456: Adding a distinct. This sql otherwise returns ORA-01427 error
3329          --when a top level config has the same child config appearing in its BOM multiple
3330          --times.
3331          where  msi.inventory_item_id in (select distinct config_item_id
3332                                        from   bom_cto_order_lines_upg
3333                                        where  ato_line_id = p_ato_line_id
3334                                        and    bom_item_type = 1
3335                                        and    option_specific in ('1','2','3')
3336                                       );
3337   elsif p_mode = 'ACC' Then
3338       update mtl_system_items msi
3339       set    msi.option_specific_sourced = (select bcol.option_specific
3340                                             from   bom_cto_order_lines bcol
3341                                             where  bcol.ato_line_id=p_ato_line_id
3342                                             and    bcol.config_item_id =msi.inventory_item_id
3343                                            )
3344       where  msi.inventory_item_id in (select config_item_id
3345                                        from   bom_cto_order_lines
3346                                        where  ato_line_id = p_ato_line_id
3347                                        and    bom_item_type = 1
3348                                        and    option_specific in ('1','2','3')
3349                                       );
3350 
3351   end if;
3352 
3353  Exception
3354 
3355     WHEN FND_API.G_EXC_ERROR THEN
3356        IF PG_DEBUG <> 0 THEN
3357           oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCNIG_RULES::exp error::'
3358 			      ||to_char(l_stmt_num)
3359 			      ||'::'||sqlerrm,1);
3360        END IF;
3361        x_return_status := FND_API.G_RET_STS_ERROR;
3362        g_pg_level := g_pg_level - 3;
3363        cto_msg_pub.count_and_get(
3364                                  p_msg_count  => x_msg_count,
3365                                  p_msg_data   => x_msg_data
3366                                 );
3367     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3368        IF PG_DEBUG <> 0 THEN
3369           oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES::exp error::'
3370 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
3371        END IF;
3372        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3373        g_pg_level := g_pg_level - 3;
3374        cto_msg_pub.count_and_get(
3375                                  p_msg_count  => x_msg_count,
3376                                  p_msg_data   => x_msg_data
3377                                 );
3378     WHEN OTHERS THEN
3379        IF PG_DEBUG <> 0 THEN
3380           oe_debug_pub.add(lpad(' ',g_pg_level)||'CREATE_OSS_SOURCING_RULES::exp error::'
3381 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
3382        END IF;
3383        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3384        g_pg_level := g_pg_level - 3;
3385        cto_msg_pub.count_and_get(
3386                                  p_msg_count  => x_msg_count,
3387                                  p_msg_data   => x_msg_data
3388                                 );
3389 
3390 End Create_oss_sourcing_rules;
3391 
3392 
3393 
3394 Procedure  update_Source_tree(p_line_id       IN Number,
3395                               p_end_org       IN  Number,
3396                               x_return_status OUT NOCOPY Varchar2 ,
3397                               x_msg_data      OUT NOCOPY Varchar2,
3398                               x_msg_count     OUT NOCOPY Number
3399                              ) is
3400 
3401   l_rcv_org_id   Number;
3402   TYPE org_id_tbl is TABLE of Number index by binary_integer;
3403 
3404   l_org_tbl      org_id_tbl;
3405   l_stmt_num     Number;
3406 
3407 
3408 Begin
3409 
3410   l_stmt_num := 10;
3411   g_pg_level := g_pg_level + 3;
3412 
3413   If PG_DEBUG <> 0 Then
3414      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_SOURCE_TREE: Inside Update Source Tree API',5);
3415      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_SOURCE_TREE: Line id ='||p_line_id,5);
3416      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_SOURCE_TREE: Org  id ='||p_end_org,5);
3417   End if;
3418   If p_line_id is null  and p_end_org is null then
3419     return;
3420   end if;
3421   l_stmt_num := 120;
3422 
3423   update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3424          bom_cto_oss_source_gt oss_src
3425   set    valid_flag = 'Y'
3426   where  source_org_id = p_end_org
3427   and    line_id       = p_line_id
3428   and    nvl(leaf_node,'N') <> 'Y'
3429   returning rcv_org_id bulk collect into l_org_tbl;
3430 
3431   If PG_DEBUG <> 0 Then
3432      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_SOURCE_TREE: Number parent orgs = '||l_org_tbl.count,5);
3433   End if;
3434 
3435 /* Need to work for Cutomer rules... */
3436 
3437   IF l_org_tbl.count <> 0 then
3438     For i in l_org_tbl.first..l_org_tbl.last
3439     Loop
3440        update_source_tree(p_line_id       => p_line_id,
3441                           p_end_org       => l_org_tbl(i),
3442                           x_return_status => x_return_status,
3443                           x_msg_data      => x_msg_data,
3444                           x_msg_count     => x_msg_count
3445 		         );
3446        If x_return_status  = FND_API.G_RET_STS_ERROR Then
3447           IF PG_DEBUG <> 0 Then
3448              oe_debug_pub.add(lpad(' ',g_pg_level)||
3449 	                               'UPDATE_SOURCE_TREE: Exepected error occurred in update_oss_in_bcol API',5);
3450           End if;
3451           raise FND_API.G_EXC_ERROR;
3452        elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3453           IF PG_DEBUG <> 0 Then
3454              oe_debug_pub.add(lpad(' ',g_pg_level)||
3455 	                         'UPDATE_SOURCE_TREE: Un Exepected error occurred in update_oss_in_bcol API',5);
3456           End if;
3457           raise FND_API.G_EXC_UNEXPECTED_ERROR;
3458        End if;
3459 
3460     End loop;
3461   End if;
3462 g_pg_level := g_pg_level - 3;
3463 End update_Source_tree;
3464 
3465 /* The following procedure will prune the item level rule for parent oss
3466 
3467    1. For parent oss configurations, If there is an item level rule, we should
3468       prune the item level rule first.
3469    2. Pruning the item level rule means, find the all leaf nodes in the item level
3470       rule and check if the leaf node org is part of the intersection list.
3471    3. If the leaf node is part of the intersection list, mark that node as valid
3472       and also mark all the parent nodes as valid. This will make the whole chain as
3473       valid one.
3474    4. This should be done for all the leaf nodes in the item level rule.
3475    5. After pruning the item level rule, check if any thing is marked as valid node.
3476    6. If nothing found as valid node, then this means the puring resulted in no valid
3477       item level sourcing chain.
3478    7. In that case, the reset of the sourcing tree should be pruned with usual logic.
3479    8. After pruing the item level rule, if we end up having some valid nodes, do the following.
3480    9. Mark all the leaf nodes as valid which are one of the following.
3481        a. The source type is buy(3).
3482        b. It is a leaf node and is a transfer type
3483        c. It is a leaf node with make at rule and the org is part of the intersection list.
3484    10. Once the valid leaf nodes are found, mark all its parent sourcing chain as valid
3485    11. At the end all the valid nodes will form the config sourcing tree.
3486 
3487 */
3488 
3489 Procedure prune_item_level_rule(p_model_line_id   IN  Number,
3490                                 p_model_item_id   IN  Number,
3491 				x_rule_exists     OUT NOCOPY Varchar2,
3492 				x_return_status   OUT NOCOPY Varchar2,
3493 				x_msg_count       OUT NOCOPY Number,
3494 				x_msg_data        OUT NOCOPY varchar2
3495 			       ) is
3496 
3497     /* The following cursor will get all the root nodes for item level
3498        sourcing tree
3499      */
3500 
3501     Cursor global_orgs_cur  is
3502     Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3503            source_org_id
3504     from   bom_cto_oss_source_gt oss_src
3505     where  customer_id is null
3506     and    rcv_org_id is null
3507     and    line_id    = p_model_line_id
3508     and    nvl(valid_flag,'Y') <> 'N';
3509 
3510     l_valid_count      Number;
3511     TYPE l_source_org_tbl_type is TABLE of Number;
3512     l_source_org_id    l_source_org_tbl_type;
3513     l_source_org_tbl   l_source_org_tbl_type;
3514 
3515 Begin
3516     /* For each root item level rule node find the leaf node
3517        and see if the leaf node is part of intersection org
3518 
3519        Renga: We should implement bulk fetch from cursor and then
3520               FOR all for select  to improve the performance. Revisit this part
3521     */
3522 
3523     For global_orgs_rec in global_orgs_cur
3524     Loop
3525        Begin
3526           /* The following sql may not be needed as this will
3527 	     be part of find_leaf_node itself
3528 	   */
3529           select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3530 	         source_org_id
3531 	  bulk collect into   l_source_org_id
3532           from   bom_cto_oss_source_gt oss_src
3533           where  rcv_org_id = global_orgs_rec.source_org_id
3534           and    line_id    = p_model_line_id
3535           and    nvl(valid_flag,'Y') <> 'N';
3536 
3537           /* find Leaf node is a recursive procedure to find the leaf node
3538 	  */
3539 
3540           For i in l_source_org_id.first..l_source_org_id.last
3541           Loop
3542 	  Find_leaf_node(p_model_line_id => p_model_line_id,
3543 	                 p_source_org_id => l_source_org_id(i),
3544 			 p_rcv_org_id    => global_orgs_rec.source_org_id,
3545 			 x_return_status => x_return_status,
3546 			 x_msg_count     => x_msg_count,
3547 			 x_msg_data      => x_msg_data);
3548           If x_return_status  = FND_API.G_RET_STS_ERROR Then
3549              IF PG_DEBUG <> 0 Then
3550                 oe_debug_pub.add(lpad(' ',g_pg_level)||
3551 	                               'PRUNE_ITEM_LEVEL_RULE: Exepected error occurred in update_oss_in_bcol API',5);
3552              End if;
3553              raise FND_API.G_EXC_ERROR;
3554           elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3555              IF PG_DEBUG <> 0 Then
3556                 oe_debug_pub.add(lpad(' ',g_pg_level)||
3557 	                         'PRUNE_ITEM_LEVEL_RULE: Un Exepected error occurred in update_oss_in_bcol API',5);
3558              End if;
3559              raise FND_API.G_EXC_UNEXPECTED_ERROR;
3560           End if;
3561           End loop;
3562 
3563        End;
3564 
3565     End Loop;
3566 
3567     Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3568            count(*)
3569     into   l_valid_count
3570     from   bom_cto_oss_source_gt oss_src
3571     where  line_id = p_model_line_id
3572     and    valid_flag ='Y';
3573 
3574     If l_valid_count > 0 then
3575        x_rule_exists := 'Y';
3576     else
3577        x_rule_exists := 'N';
3578     End if;
3579 
3580     If x_rule_exists = 'Y' then
3581 
3582        /* The following update will find all the
3583           valid leaf nodes.
3584           1. This will mark all the buy nodes as valid node.
3585           2  Mark all the end nodes which are of the type xfer as valid
3586           3. Mark all the make at nodes for whihc the org is part of intersection
3587              list as valid nodes.
3588        */
3589 
3590        Update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3591               bom_cto_oss_source_gt oss_src
3592        set    leaf_node  = 'Y',
3593               valid_flag = 'Y'
3594        where
3595        line_id = p_model_line_id
3596        and nvl(valid_flag,'Y') <> 'N'
3597        and
3598        (source_type = 3
3599         or (    Source_type = 2
3600 	    and source_org_id in (select /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
3601 	                          organization_id
3602 	                          from bom_cto_oss_orgslist_gt OSS_LIS
3603 				  where line_id = p_model_line_id)
3604 	    )
3605 	or (    source_type = 1
3606 	    and source_org_id not in
3607                         (Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3608 			        nvl(rcv_org_id,-1)
3609 		         from   bom_cto_oss_source_gt oss_src
3610 			 where  line_id = p_model_line_id
3611                          and    nvl(valid_flag,'Y') <> 'N')
3612            )
3613 	)
3614        returning rcv_org_id bulk collect into l_source_org_tbl;
3615 
3616 
3617        oe_debug_pub.add(lpad(' ',g_pg_level)||'Prune_item_level_rule Updated
3618                                                valid leaf nodes = '||l_source_org_tbl.count,1);
3619 
3620        /* For all the above leaf nodes, traverse the tree up
3621           and update all the parents as valid
3622        */
3623 
3624        For i in l_source_org_tbl.first..l_source_org_tbl.last
3625        Loop
3626            If l_source_org_tbl(i) is not null then
3627               Traverse_up_tree(p_model_line_id  => p_model_line_id,
3628                                p_source_org_id  => l_source_org_tbl(i),
3629   		               p_valid_flag     => 'Y',
3630 		               x_return_status  => x_return_status,
3631 		               x_msg_count      => x_msg_count,
3632 		               x_msg_data       => x_msg_data);
3633               If x_return_status  = FND_API.G_RET_STS_ERROR Then
3634                  IF PG_DEBUG <> 0 Then
3635                     oe_debug_pub.add(lpad(' ',g_pg_level)||
3636                               'PRUNE_ITEM_LEVEL_RULE: Exepected error occurred in update_oss_in_bcol API',5);
3637                  End if;
3638                  raise FND_API.G_EXC_ERROR;
3639               elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3640                  IF PG_DEBUG <> 0 Then
3641                     oe_debug_pub.add(lpad(' ',g_pg_level)||
3642                         'PRUNE_ITEM_LEVEL_RULE: Un Exepected error occurred in update_oss_in_bcol API',5);
3643                  End if;
3644                  raise FND_API.G_EXC_UNEXPECTED_ERROR;
3645               End if;
3646            End if;
3647 
3648        End loop;
3649     Else /* X_rule_exists */
3650        oe_debug_pub.add(lpad(' ',g_pg_level)||'PRUNE_ITEM_LEVEL_RULE: Updating bcol with option specific = 4',1);
3651 
3652        update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
3653               bom_cto_order_lines_gt bcol
3654        set    option_specific = 4
3655        where  line_id = p_model_line_id;
3656 
3657     End if; /* x_rule_exists = 'Y' */
3658 
3659     /* By this time, we have identified and marked all the valid nodes
3660        in the tree
3661     */
3662 
3663 
3664 End Prune_item_level_rule;
3665 
3666 
3667 Procedure Find_leaf_node( p_model_line_id   IN  Number,
3668                           p_source_org_id   IN  Number,
3669 			  p_rcv_org_id      IN  Number,
3670 			  x_return_status   OUT NOCOPY Varchar2,
3671 			  x_msg_data        OUT NOCOPY Varchar2,
3672 			  x_msg_count       OUT NOCOPY Number) is
3673    --l_source_org_id    Number;
3674    --l_source_type      Number;
3675 
3676    /*Bugfix 13362916 Changing the table type to refer to a record structure.
3677    TYPE v_num_type is table of number;
3678 
3679    l_source_org_id  v_num_type;
3680    l_source_type    v_num_type;
3681    */
3682 
3683    TYPE source_details_rec IS Record(l_source_org_id NUMBER,
3684                                      l_source_type   NUMBER);
3685 
3686    TYPE source_details_tab_typ IS TABLE OF source_details_rec INDEX BY BINARY_INTEGER;
3687    source_details_tab source_details_tab_typ;
3688 
3689    --Bugfix 13540153-FP(13360098)
3690    l_cnt  number;
3691 
3692 Begin
3693    g_pg_level := nvl(g_pg_level,0)+3;
3694 
3695    If PG_DEBUG <> 0 Then
3696       oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: Entering Find Leaf Node',5);
3697       oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: P_source_org_id = '||p_source_org_id,5);
3698       oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: P_rcv_org_id    = '||p_rcv_org_id,5);
3699       oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: p_model_line_id    = '||p_model_line_id,5);
3700    End if;
3701 
3702    -- bug 13362916
3703    -- select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3704    --       source_org_id,
3705    --       source_type
3706    --bulk collect into
3707    --       l_source_org_id,
3708    --       l_source_type
3709    --from   bom_cto_oss_source_gt oss_src
3710    --where  rcv_org_id = p_source_org_id
3711    --and    line_id    = p_model_line_id
3712    --and    nvl(valid_flag,'Y') <> 'N';
3713 
3714    SELECT /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3715       source_org_id,
3716       source_type
3717    BULK COLLECT INTO source_details_tab
3718    FROM   bom_cto_oss_source_gt oss_src
3719    WHERE  rcv_org_id = p_source_org_id
3720    AND    line_id    = p_model_line_id
3721    AND    nvl(valid_flag,'Y') <> 'N';
3722 
3723    IF PG_DEBUG <> 0 Then
3724       -- bug 13362916
3725       -- oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: Source Org count = '||l_source_org_id.count,5);
3726       -- oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: Source Type count   = '||l_source_type.count,5);
3727       oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: Source Details Tab count   = '||source_details_tab.count,5);
3728    End if;
3729 
3730 
3731    -- If l_source_type.count <> 0 then
3732    If source_details_tab.count <> 0 then
3733       --For i in l_source_type.first..l_source_type.last
3734       For i in 1..source_details_tab.count
3735       Loop
3736          IF PG_DEBUG <> 0 Then
3737            oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: source_details_tab.l_source_type   = '||source_details_tab(i).l_source_type,5);
3738            oe_debug_pub.add(lpad(' ',g_pg_level)||'FIND_LEAF_NODE: source_details_tab.l_source_org_id = '||source_details_tab(i).l_source_org_id,5);
3739          End if;
3740 
3741          -- If l_source_type(i) not in (2,3) then
3742          If source_details_tab(i).l_source_type not in (2,3) then
3743             Find_leaf_node(P_model_line_id  => p_model_line_id,
3744                            p_source_org_id  => source_details_tab(i).l_source_org_id,
3745                            p_rcv_org_id     => p_source_org_id,
3746                            x_return_status  => x_return_status,
3747                            x_msg_data       => x_msg_data,
3748                            x_msg_count      => x_msg_count);
3749 
3750             If x_return_status  = FND_API.G_RET_STS_ERROR Then
3751                IF PG_DEBUG <> 0 Then
3752                   oe_debug_pub.add(lpad(' ',g_pg_level)||
3753                                   'FIND_LEAF_NODE: Exepected error occurred in update_oss_in_bcol API',5);
3754                End if;
3755                raise FND_API.G_EXC_ERROR;
3756             elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3757                IF PG_DEBUG <> 0 Then
3758                   oe_debug_pub.add(lpad(' ',g_pg_level)||
3759                                   'FIND_LEAF_NODE: Un Exepected error occurred in update_oss_in_bcol API',5);
3760                End if;
3761                raise FND_API.G_EXC_UNEXPECTED_ERROR;
3762             End if;
3763          End if;
3764 
3765          /*Bugfix 13540153-FP(13360098): Changing rcv_org_id to source_org_id and adding an nvl.
3766            1. Changing rcv_org_id to source_org_id: Consider the sourcing data for parent non OSS model as:
3767            ================PRINTING BOM_CTO_OSS_SOURCE_GT==================
3768            Line_id --- Item id --- Rcv org --- src org --- customer --- vendor --- vend site --- rank --- alloc% --- src type --- reuse -- valid --- leaf --- sr_receipt_id ---
3769            -99326 --- 3087074 ---  --- 122 ---  ---  ---  --- 1 --- 40 --- 1 ---  ---  ---  --- 213011
3770            -99326 --- 3087074 ---  --- 164 ---  ---  ---  --- 1 --- 35 --- 1 ---  ---  ---  --- 213011
3771            -99326 --- 3087074 ---  --- 304 ---  ---  ---  --- 1 --- 25 --- 1 ---  ---  ---  --- 213011
3772            -99326 --- 3087074 --- 122 --- 122 ---  ---  ---  --- 1 --- 100 --- 2 ---  ---  ---  --- 212002
3773            -99326 --- 3087074 --- 164 --- 164 ---  ---  ---  --- 1 --- 100 --- 2 ---  ---  ---  --- 213002
3774            -99326 --- 3087074 --- 304 --- 122 ---  ---  ---  --- 1 --- 60 --- 1 ---  ---  ---  --- 213004
3775            -99326 --- 3087074 --- 304 --- 164 ---  ---  ---  --- 1 --- 40 --- 1 ---  ---  ---  --- 213004
3776            ============== End printing ===============
3777 
3778            Consider the orgs eligible as per OSS on lower level children as:
3779            ================PRINTING BOM_CTO_ORGSLIST_GT==================
3780            Line id --- Ato Line Id --- Item Id --- Org id --- vendor --- vend site ---Make Flag
3781            -99326 --- -99326 --- 3087074 ---  ---  ---  ---
3782            -99326 --- -99326 --- 3087074 --- 146 ---  ---  ---
3783            -99326 --- -99326 --- 3087074 --- 164 ---  ---  ---
3784            -99326 --- -99326 --- 3087074 --- 304 ---  ---  ---
3785            ============== End printing ===============
3786 
3787            The below update sql was updating the 'rcv_org = 304, src_org = 122' record as valid even though
3788            org 122 is not valid as per OSS. Furthermore, the idea of this API is to find leaf nodes. These
3789            leaves would be the source_org values and not rcv_org values.
3790 
3791            2. Reason for adding nvl:
3792            This was very strange. Without nvl, the record 'rcv_org = 304, src_org = 122' was still being marked
3793            as valid. Added an nvl to get around this problem.
3794          */
3795 
3796 	 update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3797                 bom_cto_oss_source_gt oss_src
3798          set    leaf_node  = 'Y',
3799                 valid_flag = 'Y'
3800          where  line_id   = p_model_line_id
3801          and    source_org_id = p_source_org_id
3802          and    rcv_org_id    = p_rcv_org_id
3803          and    nvl(valid_flag,'Y') <> 'N'
3804          --Bugfix 13540153-FP(13360098)
3805 	 --and    rcv_org_id   in (select /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
3806 	 and    source_org_id   in (select /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
3807                                            --Bugfix 13540153-FP(13360098): Adding an nvl.
3808 				           nvl(organization_id, -9999)
3809                                     from   bom_cto_oss_orgslist_gt oss_lis
3810                                     where  line_id = p_model_line_id);
3811 
3812          --Bugfix 13540153-FP(13360098)
3813          l_cnt := sql%rowcount;
3814 
3815 	 --If sql%rowcount <> 0 then
3816 	 If l_cnt <> 0 then
3817             Traverse_up_tree(p_model_line_id  => p_model_line_id,
3818                              p_source_org_id  => p_rcv_org_id,
3819                              p_valid_flag     => 'Y',
3820                              x_return_status  => x_return_status,
3821                              x_msg_count      => x_msg_count,
3822                              x_msg_data       => x_msg_data);
3823             If x_return_status  = FND_API.G_RET_STS_ERROR Then
3824                IF PG_DEBUG <> 0 Then
3825                   oe_debug_pub.add(lpad(' ',g_pg_level)||
3826                                        'FIND_LEAF_NODE: Exepected error occurred in update_oss_in_bcol API',5);
3827                End if;
3828                raise FND_API.G_EXC_ERROR;
3829             elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3830                IF PG_DEBUG <> 0 Then
3831                   oe_debug_pub.add(lpad(' ',g_pg_level)||
3832                                 'FIND_LEAF_NODE: Un Exepected error occurred in update_oss_in_bcol API',5);
3833                End if;
3834                raise FND_API.G_EXC_UNEXPECTED_ERROR;
3835             End if;
3836 
3837          End if;/* Sql%rowcount <> 0 then */
3838       End Loop;
3839    else
3840      IF PG_DEBUG <> 0 THEN
3841        oe_debug_pub.add('Inside else.. Find_leaf_node');
3842      END IF;
3843 
3844      update /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3845             bom_cto_oss_source_gt oss_src
3846      set    leaf_node  = 'Y',
3847             valid_flag = 'Y'
3848      where  line_id   = p_model_line_id
3849      and    source_org_id = p_source_org_id
3850      and    rcv_org_id    = p_rcv_org_id
3851      and    nvl(valid_flag,'Y') <> 'N'
3852      -- Not touching this rcv_org_id value as was done in the if block. The piece of code
3853      -- is fragile and we would take the issues as and when they come. This sql might be
3854      -- a potential red flag in the future.
3855      and    rcv_org_id   in (select /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
3856                                     --Bugfix 13540153-FP(13360098): Adding an nvl.
3857 			            nvl(organization_id, -9999)
3858                              from   bom_cto_oss_orgslist_gt oss_lis
3859                              where  line_id = p_model_line_id);
3860 
3861      --Bugfix 13540153-FP(13360098)
3862      l_cnt := sql%rowcount;
3863 
3864      --If sql%rowcount <> 0 then
3865      If l_cnt <> 0 then
3866        Traverse_up_tree(p_model_line_id  => p_model_line_id,
3867                         p_source_org_id  => p_rcv_org_id,
3868                         p_valid_flag     => 'Y',
3869                         x_return_status  => x_return_status,
3870                         x_msg_count      => x_msg_count,
3871                         x_msg_data       => x_msg_data);
3872        If x_return_status  = FND_API.G_RET_STS_ERROR Then
3873          IF PG_DEBUG <> 0 Then
3874             oe_debug_pub.add(lpad(' ',g_pg_level)||
3875                                     'FIND_LEAF_NODE: Exepected error occurred in update_oss_in_bcol API',5);
3876          End if;
3877          raise FND_API.G_EXC_ERROR;
3878        elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3879          IF PG_DEBUG <> 0 Then
3880             oe_debug_pub.add(lpad(' ',g_pg_level)||
3881                              'FIND_LEAF_NODE: Un Exepected error occurred in update_oss_in_bcol API',5);
3882          End if;
3883          raise FND_API.G_EXC_UNEXPECTED_ERROR;
3884        End if;
3885 
3886      End if;
3887    End if; /* l_source_type.count = 0 */
3888    g_pg_level := g_pg_level - 3;
3889 End Find_leaf_node;
3890 
3891 
3892 Procedure    Traverse_up_tree(p_model_line_id  IN  Number,
3893                               p_source_org_id  IN  Number,
3894 		              p_valid_flag     IN  Varchar2,
3895 		              x_return_status  OUT NOCOPY Varchar2,
3896 		              x_msg_count      OUT NOCOPY Varchar2,
3897 		              x_msg_data       OUT NOCOPY Number) is
3898    TYPE org_id_tbl is TABLE of Number;
3899 
3900    l_org_id_tbl      org_id_tbl;
3901    l_rcv_org_tbl     org_id_tbl;
3902    i                 Number;
3903 
3904 Begin
3905    g_pg_level := g_pg_level + 3;
3906 
3907    If PG_DEBUG <> 0 Then
3908       oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_UP_TREE: Entering Traverse up tree API',5);
3909       oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_UP_TREE: Model Line id =
3910 '||p_model_line_id,1);
3911       oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_UP_TREE: Source Org id =
3912 '||p_source_org_id,1);
3913 
3914    End if;
3915 
3916    update  /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
3917            bom_cto_oss_source_gt oss_src
3918    set     valid_flag = 'Y'
3919    where   line_id = p_model_line_id
3920    and     source_org_id = p_source_org_id
3921    and     nvl(valid_flag,'Y') <> 'N'
3922    and       source_type <> 2  /*Exclude make rules...*/
3923    returning source_org_id,rcv_org_id bulk collect into l_rcv_org_tbl, l_org_id_tbl;
3924 
3925    If PG_DEBUG <> 0 then
3926       oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_TREE_UP: Number of parents updated = '||l_org_id_tbl.count,5);
3927    end if;
3928 
3929    If l_org_id_tbl.count <> 0 Then
3930       For i in l_org_id_tbl.first..l_org_id_tbl.last
3931       Loop
3932 
3933          oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_UP_TREE: Rcv org id = '||l_rcv_org_tbl(i),1);
3934 	 oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_UP_TREE: Org id     = '||l_org_id_tbl(i),1);
3935 
3936          If l_rcv_org_tbl(i) <> l_org_id_tbl(i)
3937 	    and l_rcv_org_tbl(i) is not null then
3938 
3939 
3940          Traverse_up_tree(p_model_line_id  => p_model_line_id,
3941                           p_source_org_id  => l_org_id_tbl(i),
3942 	      	          p_valid_flag     => 'Y',
3943 		          x_return_status  => x_return_status,
3944 		          x_msg_count      => x_msg_count,
3945 		          x_msg_data       => x_msg_data);
3946          if x_return_status  = FND_API.G_RET_STS_ERROR Then
3947             IF PG_DEBUG <> 0 Then
3948                oe_debug_pub.add(lpad(' ',g_pg_level)||
3949 	                               'TRAVERSE_UP_TREE: Exepected error occurred in update_oss_in_bcol API',5);
3950             End if;
3951             raise FND_API.G_EXC_ERROR;
3952          elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
3953             IF PG_DEBUG <> 0 Then
3954                oe_debug_pub.add(lpad(' ',g_pg_level)||
3955 	                         'TRAVERSE_UP_TREE: Un Exepected error occurred in update_oss_in_bcol API',5);
3956             End if;
3957             raise FND_API.G_EXC_UNEXPECTED_ERROR;
3958          End if;
3959 
3960 
3961 	 null;
3962 
3963          End if;
3964       End Loop;
3965    End if;
3966    g_pg_level := g_pg_level - 3;
3967 End Traverse_up_tree;
3968 
3969 /*
3970 
3971 
3972 
3973    ********************************* DURING ATP ***************************************
3974 
3975                    The Following part of code is called during ATP
3976 
3977    ************************************************************************************
3978 
3979 
3980 
3981 
3982 */
3983 
3984 /*
3985      This procedure is called from match API during ATP. This will not have
3986      any input parameter. This looks at the data from bom_cto_order_lines_gt temp table
3987      and process all the OSS configurations to get the list of valid orgs and
3988      vendors.
3989 */
3990 
3991 Procedure  Get_OSS_Orgs_list(
3992                x_oss_orgs_list  OUT  NOCOPY CTO_OSS_SOURCE_PK.oss_orgs_list_rec_type,
3993                x_return_status  OUT  NOCOPY Varchar2,
3994                x_msg_data       OUT  NOCOPY Varchar2,
3995                x_msg_count      OUT  NOCOPY Number) is
3996 
3997   l_temp   number:=0;
3998   l_stmt_num number    := 0;
3999 Begin
4000 
4001    PG_DEBUG := 5;
4002    g_pg_level := 1;
4003 
4004    x_return_status := FND_API.G_RET_STS_SUCCESS;
4005    l_stmt_num := 10;
4006 
4007    oe_debug_pub.add('=========================================================================',1);
4008    oe_debug_pub.add('                                                                         ',1);
4009    oe_debug_pub.add('             START OPTION SPECIFIC SOURCE PROCESSING                     ',1);
4010    oe_debug_pub.add('                                                                         ',1);
4011    oe_debug_pub.add('             START TIME STAMP : '||to_char(sysdate,'hh:mi:ss')||'        ',1);
4012    oe_debug_pub.add('                                                                         ',1);
4013    oe_debug_pub.add('=========================================================================',1);
4014 
4015    If PG_DEBUG <> 0 Then
4016       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Begin GET_OSS_ORGS_LIST API',5);
4017    End if;
4018 
4019    delete from bom_cto_oss_source_gt ;
4020    If PG_DEBUG <> 0 Then
4021       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Number of records delete in source_gt = '
4022                                            ||sql%rowcount,5);
4023    end if;
4024 
4025    delete from bom_cto_oss_orgslist_gt;
4026 
4027    If PG_DEBUG <> 0 Then
4028       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Number of records delete in source_gt = '
4029                                            ||sql%rowcount,5);
4030    end if;
4031 
4032    g_def_assg_set := to_number(
4033                                FND_PROFILE.VALUE('MRP_DEFAULT_ASSIGNMENT_SET'));
4034 
4035   /* Check if there is a default assignment set defined.
4036      If there is no default assignment set specified,
4037      OSS is not supported and CTO need not do anything.
4038   */
4039 
4040 
4041   If PG_DEBUG <> 0 then
4042      oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Default Assignment set id = '
4043                                           ||g_def_assg_set);
4044   End if;
4045 
4046   If g_def_assg_set is null then
4047      If PG_DEBUG <> 0 Then
4048         oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: There is no default assignment set Specified',4);
4049 	oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Ending the call',4);
4050         oe_debug_pub.add('=========================================================================',1);
4051         oe_debug_pub.add('                                                                         ',1);
4052         oe_debug_pub.add('               END OPTION SPECIFIC SOURCE PROCESSING                     ',1);
4053         oe_debug_pub.add('                                                                         ',1);
4054         oe_debug_pub.add('                                                                         ',1);
4055         oe_debug_pub.add('               END TIME STAMP : '||to_char(sysdate,'hh:mi:ss')||'        ',1);
4056         oe_debug_pub.add('                                                                         ',1);
4057         oe_debug_pub.add('=========================================================================',1);
4058 
4059      End if;
4060      return;
4061   end if;
4062 
4063 
4064   /* Check to see if there is some OSS list specified for any model.
4065      Will check if there is some record exists in bom_cto_oss_components .
4066      If there is no record, then CTO need not do anything. This means the OSS orgs
4067      are not specified by user for any model. The following part of the code will
4068      check this.
4069   */
4070 
4071   l_stmt_num := 20;
4072 
4073   Declare
4074     l_check_flag varchar2(1);
4075   Begin
4076     Select 'X'
4077     into   l_check_flag
4078     from dual
4079     where exists (select 'x' from bom_cto_oss_components);
4080 
4081     If l_check_flag = 'X' then
4082       If PG_DEBUG <> 0 Then
4083          oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Some OSS Setup Exists',5);
4084       End if;
4085     end if;
4086   Exception when no_data_found then
4087     If PG_DEBUG <>0 then
4088        oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: No OSS orgs defied in the system',4);
4089        oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Ending the call',4);
4090        oe_debug_pub.add('=========================================================================',1);
4091        oe_debug_pub.add('                                                                         ',1);
4092        oe_debug_pub.add('               END OPTION SPECIFIC SOURCE PROCESSING                     ',1);
4093        oe_debug_pub.add('                                                                         ',1);
4094        oe_debug_pub.add('                                                                         ',1);
4095        oe_debug_pub.add('               END TIME STAMP : '||to_char(sysdate,'hh:mi:ss')||'        ',1);
4096        oe_debug_pub.add('                                                                         ',1);
4097        oe_debug_pub.add('=========================================================================',1);
4098 
4099     End if;
4100 
4101     return;
4102   End;
4103 
4104   /* The following is the procedure to get the organization for all the model configruations
4105   */
4106 
4107   l_stmt_num := 30;
4108 
4109   get_configurations_org(
4110                       x_return_status  => x_return_status,
4111                       x_msg_count      => x_msg_count,
4112                       x_msg_data       => x_msg_data
4113 		     );
4114 
4115   If x_return_status  = FND_API.G_RET_STS_ERROR Then
4116      IF PG_DEBUG <> 0 Then
4117         oe_debug_pub.add(lpad(' ',g_pg_level)||
4118 	          'GET_OSS_ORGS_LIST: Exepected error occurred in get_configurations_org API',5);
4119      End if;
4120      raise FND_API.G_EXC_ERROR;
4121   elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
4122      IF PG_DEBUG <> 0 Then
4123         oe_debug_pub.add(lpad(' ',g_pg_level)||
4124 	          'GET_OSS_ORGS_LIST: Un Exepected error occurred in get_configurations_org API',5);
4125      End if;
4126      raise FND_API.G_EXC_UNEXPECTED_ERROR;
4127   End if;
4128 
4129   /* The following is the procedure to get the organizations for all the ato items and
4130      matched configurations.
4131   */
4132 
4133   l_stmt_num := 40;
4134 
4135   get_ato_item_orgs(
4136                      p_assignment_id  => g_def_assg_set,
4137                      x_return_status  => x_return_status,
4138                      x_msg_count      => x_msg_count,
4139                      x_msg_data       => x_msg_data
4140 		    );
4141 
4142   If x_return_status  = FND_API.G_RET_STS_ERROR Then
4143      IF PG_DEBUG <> 0 Then
4144         oe_debug_pub.add(lpad(' ',g_pg_level)||
4145 	          'GET_OSS_ORGS_LIST: Exepected error occurred in get_configurations_org API',5);
4146      End if;
4147      raise FND_API.G_EXC_ERROR;
4148   elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
4149      IF PG_DEBUG <> 0 Then
4150         oe_debug_pub.add(lpad(' ',g_pg_level)||
4151 	          'GET_OSS_ORGS_LIST: Un Exepected error occurred in get_configurations_org API',5);
4152      End if;
4153      raise FND_API.G_EXC_UNEXPECTED_ERROR;
4154   End if;
4155 
4156 
4157 
4158   l_stmt_num := 50;
4159 
4160   select line_id,
4161 	 inventory_item_id,
4162 	 ato_line_id,
4163 	 organization_id,
4164 	 vendor_id,
4165 	 vendor_site_code,
4166          make_flag
4167   bulk collect into
4168          x_oss_orgs_list.line_id,
4169 	 x_oss_orgs_list.inventory_item_id,
4170 	 x_oss_orgs_list.ato_line_id,
4171 	 x_oss_orgs_list.org_id,
4172 	 x_oss_orgs_list.vendor_id,
4173 	 x_oss_orgs_list.vendor_site,
4174 	 x_oss_orgs_list.make_flag
4175   from   bom_cto_oss_orgslist_gt;
4176 
4177 
4178   If PG_DEBUG <> 0 Then
4179      If x_oss_orgs_list.line_id.count <> 0 Then
4180         For i in x_oss_orgs_list.line_id.first..x_oss_orgs_list.line_id.last
4181         Loop
4182            oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Line id     = '||x_oss_orgs_list.line_id(i),5);
4183            oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Org id      = '||x_oss_orgs_list.org_id(i),5);
4184            oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Vendor id   = '||x_oss_orgs_list.Vendor_id(i),5);
4185            oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Vendor Site = '||x_oss_orgs_list.vendor_site(i),5);
4186            oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Make Flag   = '||x_oss_orgs_list.make_flag(i),5);
4187         End loop;
4188       End if;
4189   End if;
4190 
4191   If PG_DEBUG <> 0 Then
4192      oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Number of records insert to output structure ='||sql%rowcount,4);
4193      oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_ORGS_LIST: Ending the call',4);
4194   End if;
4195 
4196 
4197  oe_debug_pub.add('=========================================================================',1);
4198  oe_debug_pub.add('                                                                         ',1);
4199  oe_debug_pub.add('               END OPTION SPECIFIC SOURCE PROCESSING                     ',1);
4200  oe_debug_pub.add('                                                                         ',1);
4201  oe_debug_pub.add('                                                                         ',1);
4202  oe_debug_pub.add('               END TIME STAMP : '||to_char(sysdate,'hh:mi:ss')||'        ',1);
4203  oe_debug_pub.add('                                                                         ',1);
4204  oe_debug_pub.add('=========================================================================',1);
4205 
4206 
4207 Exception
4208 
4209         WHEN FND_API.G_EXC_ERROR THEN
4210             IF PG_DEBUG <> 0 THEN
4211                 oe_debug_pub.add(lpad(' ',g_pg_level)||'Get_OSS_Orgs_list::exp error::'
4212 			      ||to_char(l_stmt_num)
4213 			      ||'::'||sqlerrm,1);
4214             END IF;
4215             x_return_status := FND_API.G_RET_STS_ERROR;
4216             cto_msg_pub.count_and_get(
4217                                  p_msg_count  => x_msg_count,
4218                                  p_msg_data   => x_msg_data
4219                                 );
4220         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4221             IF PG_DEBUG <> 0 THEN
4222                 oe_debug_pub.add(lpad(' ',g_pg_level)||'Get_OSS_Orgs_list::exp error::'
4223 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
4224             END IF;
4225             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4226             cto_msg_pub.count_and_get(
4227                                  p_msg_count  => x_msg_count,
4228                                  p_msg_data   => x_msg_data
4229                                 );
4230         WHEN OTHERS THEN
4231             IF PG_DEBUG <> 0 THEN
4232                 oe_debug_pub.add(lpad(' ',g_pg_level)||'Get_OSS_Orgs_list::exp error::'
4233 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
4234             END IF;
4235             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4236             cto_msg_pub.count_and_get(
4237                                  p_msg_count  => x_msg_count,
4238                                  p_msg_data   => x_msg_data
4239                                 );
4240 
4241 END Get_OSS_Orgs_list;
4242 
4243 
4244 /* This is the procdure to get the list of oss orgs for non matched configuration
4245    items
4246 */
4247 
4248 
4249 Procedure get_configurations_org(
4250                       x_return_status  OUT NOCOPY Varchar2,
4251                       x_msg_count      OUT NOCOPY Number,
4252                       x_msg_data       OUT NOCOPY Varchar2) is
4253 
4254    Cursor  Oss_top_models is
4255        Select /*+ INDEX (bcol1 BOM_CTO_ORDER_LINES_GT_N5) */
4256               distinct bcol1.ato_line_id
4257        from   bom_cto_order_lines_gt bcol1
4258        where  exists (select /*+ INDEX (bcol1 BOM_CTO_ORDER_LINES_GT_N3) */
4259                               'X'
4260                       from    bom_cto_oss_components ossc,
4261                               bom_cto_order_lines_gt bcol2
4262                        where  bcol2.parent_ato_line_id = bcol1.line_id
4263                        and    ossc.model_item_id   =  bcol1.inventory_item_id
4264                        and    ossc.option_item_id  =  bcol2.inventory_item_id)
4265        and     bcol1.bom_item_type = '1'
4266        and     bcol1.wip_supply_type <> 6;
4267 
4268       /* The following cursor will get all the 'Option Specific sourced' model lines.
4269       The model config itself can be either oss or any of its child may be oss. This
4270       will bring all those lines for processing
4271    */
4272 
4273    Cursor oss_models(p_ato_line_id Number) is
4274      select /*+ INDEX (bcol1 BOM_CTO_ORDER_LINES_GT_N1) */
4275             line_id,
4276             ato_line_id,
4277             option_specific,
4278 	    inventory_item_id,
4279 	    config_item_id,
4280 	    perform_match,
4281 	    config_creation
4282      from   bom_cto_order_lines_gt
4283      where  ato_line_id = p_ato_line_id
4284      and    option_specific in ('1','2','3')
4285      order  by plan_level desc;
4286 
4287 
4288    L_comp_count     Number := 0;
4289    l_org_count      Number := 0;
4290    l_vendor_count   Number := 0;
4291    x_oss_exists     Varchar2(1);
4292    x_exp_error_code Number;
4293 
4294    l_stmt_no      Number := 0;
4295 
4296 Begin
4297 
4298    g_pg_level := g_pg_level + 3;
4299    x_return_status := FND_API.G_RET_STS_SUCCESS;
4300 
4301 
4302    If PG_DEBUG <> 0 Then
4303       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG : Entering Model Process',5);
4304    End if;
4305 
4306    /* Get the all models which has new config items created and the configuration
4307       has some oss orgs defined. The cursor definition resolves all the condition.
4308     */
4309    l_stmt_no := 10;
4310 
4311    FOR oss_top_model_rec in oss_top_models
4312    LOOP
4313 
4314       If PG_DEBUG <> 0 then
4315 	 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG: ATO Line id ='
4316 	                                      ||oss_top_model_rec.ato_line_id,5);
4317          oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG: Processing top model line = '
4318                                               ||oss_top_model_rec.ato_line_id,5);
4319       End if;
4320 
4321 
4322       l_stmt_no := 20;
4323 
4324       update_oss_in_bcol(
4325                          p_ato_line_id   => oss_top_model_rec.ato_line_id,
4326 			 x_oss_exists    => x_oss_exists,
4327 			 x_return_status => x_return_status,
4328 			 x_msg_data      => x_msg_data,
4329 			 x_msg_count     => x_msg_count);
4330       If x_return_status  = FND_API.G_RET_STS_ERROR Then
4331          IF PG_DEBUG <> 0 Then
4332             oe_debug_pub.add(lpad(' ',g_pg_level)||
4333 	          'GET_OSS_ORGS_LIST: Exepected error occurred in update_oss_in_bcol API',5);
4334          End if;
4335          raise FND_API.G_EXC_ERROR;
4336       elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
4337          IF PG_DEBUG <> 0 Then
4338             oe_debug_pub.add(lpad(' ',g_pg_level)||
4339 	          'GET_OSS_ORGS_LIST: Un Exepected error occurred in update_oss_in_bcol API',5);
4340          End if;
4341          raise FND_API.G_EXC_UNEXPECTED_ERROR;
4342       End if;
4343 
4344 
4345       If X_oss_exists = 'Y' Then
4346 
4347         If PG_DEBUG <> 0 Then
4348 	   oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG: This order line has some oss models',5);
4349 	   oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG: Start processing OSS Model bottom-up',5);
4350         End if;
4351 
4352         get_sourcing_data(
4353                          p_ato_line_id     => oss_top_model_rec.ato_line_id,
4354 	                 x_return_status   => x_return_status,
4355 			 x_msg_data        => x_msg_data,
4356 			 x_msg_count       => x_msg_count);
4357 
4358         If x_return_status  = FND_API.G_RET_STS_ERROR Then
4359 
4360            IF PG_DEBUG <> 0 Then
4361               oe_debug_pub.add(lpad(' ',g_pg_level)||
4362 	                   'GET_OSS_ORGS_LIST: Exepected error occurred in get_sourcing_data API',5);
4363            End if;
4364            raise FND_API.G_EXC_ERROR;
4365         elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
4366            IF PG_DEBUG <> 0 Then
4367               oe_debug_pub.add(lpad(' ',g_pg_level)||
4368 	                'GET_OSS_ORGS_LIST: Un Exepected error occurred in get_sourcing_data API',5);
4369            End if;
4370            raise FND_API.G_EXC_UNEXPECTED_ERROR;
4371         End if; /* x_return_status  = FND_API.G_RET_STS_ERROR */
4372 
4373         Process_order_for_oss(
4374                          p_ato_line_id   =>  oss_top_model_rec.ato_line_id,
4375 			 p_calling_mode  =>  'ATP',
4376                          x_return_status => x_return_status,
4377                          x_msg_count     => x_msg_count,
4378                          x_msg_data      => x_msg_data);
4379 
4380         If x_return_status  = FND_API.G_RET_STS_ERROR Then
4381            IF PG_DEBUG <> 0 Then
4382               oe_debug_pub.add(lpad(' ',g_pg_level)||
4383 	                               'GET_CONFIGURATIONS_ORG: Exepected error occurred in update_oss_in_bcol API',5);
4384            End if;
4385            raise FND_API.G_EXC_ERROR;
4386         elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
4387            IF PG_DEBUG <> 0 Then
4388               oe_debug_pub.add(lpad(' ',g_pg_level)||
4389 	                         'GET_CONFIGURATIONS_ORG: Un Exepected error occurred in update_oss_in_bcol API',5);
4390            End if;
4391            raise FND_API.G_EXC_UNEXPECTED_ERROR;
4392         End if;
4393 
4394      Else
4395 
4396         IF PG_DEBUG <> 0 Then
4397            oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG: This order line does not have any OSS configuration',5);
4398 	End if;
4399      End if;
4400 
4401    END LOOP;
4402 
4403    delete from bom_cto_oss_orgslist_gt;
4404 
4405    l_stmt_no := 30;
4406    update bom_cto_oss_source_gt ossgt1
4407    set   reuse_flag = 'N'
4408    where  rcv_org_id is not null
4409    and    valid_flag = 'Y'
4410    and    not exists (select 'x'
4411                       from bom_cto_oss_source_gt ossgt2
4412                       where ossgt1.line_id = ossgt2.line_id
4413                       and   ossgt2.rcv_org_id = ossgt1.rcv_org_id
4414                       and   ossgt2.source_type = 2
4415                       and   ossgt2.valid_flag  = 'Y');
4416 
4417 
4418    l_stmt_no := 40;
4419    update bom_cto_oss_source_gt ossgt1
4420    set   reuse_flag = 'Y'
4421    where  rcv_org_id is not null
4422    and    valid_flag = 'Y'
4423    and    exists (select/*+ INDEX (ossgt2 BOM_CTO_OSS_SOURCE_GT_N2) */
4424                           'x'
4425                       from bom_cto_oss_source_gt ossgt2
4426                       where ossgt1.line_id = ossgt2.line_id
4427                       and   ossgt2.rcv_org_id = ossgt1.rcv_org_id
4428                       and   ossgt2.source_type = 2
4429                       and   ossgt2.valid_flag  = 'Y');
4430 
4431 
4432    l_stmt_no := 50;
4433    INSERT into bom_cto_oss_orgslist_gt(
4434          Inventory_item_id,
4435          line_id,
4436 	 ato_line_id,
4437          organization_id,
4438          vendor_id,
4439          vendor_site_code,
4440 	 make_flag)
4441    select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
4442            oss_src.inventory_item_id,
4443 	   oss_src.line_id,
4444            bcol.ato_line_id,
4445 	   oss_src.rcv_org_id,
4446 	 --  oss_src.vendor_id,
4447 	 --  oss_src.vendor_site_code,
4448            to_number(null), --3894241
4449            null,
4450 	   reuse_flag
4451     from   bom_cto_oss_source_gt oss_src,
4452            bom_cto_order_lines_gt bcol
4453     where  bcol.line_id = oss_src.line_id
4454     and    oss_error_code is null
4455     and    oss_src.valid_flag  = 'Y'
4456     and    oss_src.rcv_org_id is not null
4457     and    nvl(bcol.option_specific,'4') <> '4'
4458    union
4459     select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
4460            oss_src.inventory_item_id,
4461            oss_src.line_id,
4462 	   bcol.ato_line_id,
4463 	   oss_src.source_org_id,
4464 	  -- oss_src.vendor_id,
4465 	  -- oss_src.vendor_site_code,
4466            to_number(null), --3894241
4467            null,
4468 	   null
4469      from  bom_cto_oss_source_gt oss_src,
4470            bom_cto_order_lines_gt bcol
4471      where bcol.line_id = oss_src.line_id
4472      and   bcol.option_specific is not null
4473      and   oss_error_code is null
4474      and   oss_src.valid_flag = 'Y'
4475      and   oss_src.source_org_id is not null
4476      and   oss_src.source_org_id not in (select /*+ INDEX (oss_src1 BOM_CTO_OSS_SOURCE_GT_N2) */
4477                                                 rcv_org_id
4478                                          from   bom_cto_oss_source_gt oss_src1
4479                                          where  oss_src1.line_id = oss_src.line_id
4480                                          and    valid_flag = 'Y'
4481 					)
4482     and    nvl(bcol.option_specific,'4') <> '4'
4483    union
4484      select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
4485            oss_src.inventory_item_id,
4486            oss_src.line_id,
4487            bcol.ato_line_id,
4488            to_number(null),--3894241
4489            oss_src.vendor_id,
4490            oss_src.vendor_site_code,
4491            null
4492      from  bom_cto_oss_source_gt oss_src,
4493            bom_cto_order_lines_gt bcol
4494      where bcol.line_id = oss_src.line_id
4495      and   bcol.option_specific is not null
4496      and   oss_error_code is null
4497      and   oss_src.valid_flag = 'Y'
4498      and   oss_src.vendor_id is not null
4499      and    nvl(bcol.option_specific,'4') <> '4';
4500 
4501 
4502      g_pg_level := g_pg_level - 3;
4503 
4504      IF PG_DEBUG <> 0 THEN
4505         Print_source_gt;
4506         Print_orglist_gt;
4507      END IF;
4508 
4509 Exception
4510 
4511         WHEN FND_API.G_EXC_ERROR THEN
4512             IF PG_DEBUG <> 0 THEN
4513                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG: get_configurations_org::exp error::'
4514 			      ||to_char(l_stmt_no)
4515 			      ||'::'||sqlerrm,1);
4516             END IF;
4517 	    g_pg_level := g_pg_level - 3;
4518             x_return_status := FND_API.G_RET_STS_ERROR;
4519             cto_msg_pub.count_and_get(
4520                                  p_msg_count  => x_msg_count,
4521                                  p_msg_data   => x_msg_data
4522                                 );
4523         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4524             IF PG_DEBUG <> 0 THEN
4525                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG: get_configurations_org::exp error::'
4526 			      ||to_char(l_stmt_no)||'::'||sqlerrm,1);
4527             END IF;
4528 	    g_pg_level := g_pg_level - 3;
4529             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4530             cto_msg_pub.count_and_get(
4531                                  p_msg_count  => x_msg_count,
4532                                  p_msg_data   => x_msg_data
4533                                 );
4534         WHEN OTHERS THEN
4535             IF PG_DEBUG <> 0 THEN
4536                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_CONFIGURATIONS_ORG: get_configurations_org::exp error::'
4537 			      ||to_char(l_stmt_no)||'::'||sqlerrm,1);
4538             END IF;
4539 	    g_pg_level := g_pg_level - 3;
4540             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4541             cto_msg_pub.count_and_get(
4542                                  p_msg_count  => x_msg_count,
4543                                  p_msg_data   => x_msg_data
4544                                 );
4545 
4546 End get_configurations_org;
4547 
4548 
4549 /* This is the procdure to get the list of oss orgs for  matched configuration
4550    items and ato items.
4551 */
4552 
4553 
4554 Procedure get_ato_item_orgs(
4555                             p_assignment_id  IN  Number,
4556                             x_return_status  OUT NOCOPY varchar2,
4557                             x_msg_count      OUT NOCOPY Number,
4558                             x_msg_data       OUT NOCOPY Varchar2) is
4559   l_stmt_num    Number := 0;
4560 
4561 Begin
4562 
4563   g_pg_level := g_pg_level + 3;
4564   x_return_status := FND_API.G_RET_STS_SUCCESS;
4565 
4566   l_stmt_num := 10;
4567 
4568   If PG_DEBUG <> 0 Then
4569      oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ATO_ITEM_ORGS: Assignment set id = '||p_assignment_id);
4570   End if;
4571 
4572   /* The following sql will insert all the orgs and vendors from sourcing assignments
4573      and sourcing rules for ato item and matched configuration item
4574   */
4575 
4576   /* Renga Kannan: Changed ship_from_org_id reference to validation_org.
4577  *                  Here is the story. ATP team will not pass the ship
4578  *                  from org in the case of Global ATP. This has been decided
4579  *                  at the very end of our ST cycle and aggreed to pass null
4580  *                  value for ship from org in the case of Global ATP.
4581  *                  Since the ship from org id can be null, OSS code should not
4582  *                  depend on ship from org id in this API. But, ATP will
4583  *                  populate validtion org for the order line in all the cases.
4584  *                  Since we are using ship from org as an orbitrary org fo
4585  *                  getting option_specific_sourced flag value, We can use
4586  *                  validation_org instead.
4587  *                   */
4588 
4589 
4590 
4591 
4592   INSERT into bom_cto_oss_orgslist_gt(
4593          Inventory_item_id,
4594          line_id,
4595 	 ato_line_id,
4596          organization_id,
4597          vendor_id,
4598          vendor_site_code)
4599 
4600   select /*+ FULL(bcol) */
4601          bcol.config_item_id,
4602          bcol.line_id,
4603 	 bcol.ato_line_id,
4604          src.source_organization_id,
4605          src.VENDOR_ID,
4606          vend.VENDOR_SITE_CODE
4607 
4608   from   mrp_sr_receipt_org rcv,
4609          mrp_sr_source_org src,
4610          mrp_sr_assignments assg,
4611          bom_cto_order_lines_gt bcol,
4612          mtl_system_items msi,
4613          ap_supplier_sites_all vend
4614 
4615   where
4616          bcol.ato_line_id = bcol.line_id
4617   and    nvl(bcol.wip_supply_type,-1) <> 6
4618   and    bcol.top_model_line_id is null
4619   and    msi.inventory_item_id = bcol.config_item_id
4620   and    msi.organization_id  = bcol.validation_org
4621   and    msi.option_specific_sourced in('1','2','3')
4622   and    assg.assignment_set_id = p_assignment_id
4623   and    assg.customer_id is null
4624   and    assg.inventory_item_id = msi.inventory_item_id
4625   and    assg.sourcing_rule_id = rcv.sourcing_rule_id
4626   and    rcv.effective_date <= sysdate
4627   and    nvl(rcv.disable_date,sysdate+1)>sysdate
4628   and    rcv.SR_RECEIPT_ID = src.sr_receipt_id
4629   and    src.vendor_site_id = vend.vendor_site_id(+)
4630   and    not exists (select 'X'
4631                      from   mrp_sr_assignments
4632 		     where  inventory_item_id = bcol.config_item_id
4633 		     and    organization_id is null
4634 		     and    msi.option_specific_sourced = 3)
4635 UNION
4636   select /*+ FULL(bcol) */
4637          bcol.config_item_id,
4638          bcol.line_id,
4639 	 bcol.ato_line_id,
4640          assg.organization_id,
4641          to_number(null), --3894241
4642          null
4643 
4644   from   mrp_sr_assignments assg,
4645          bom_cto_order_lines_gt bcol,
4646          mtl_system_items msi
4647 
4648   where
4649          bcol.ato_line_id             =  bcol.line_id
4650   and    nvl(bcol.wip_supply_type,-1)<> 6
4651   and    bcol.top_model_line_id      is null
4652   and    msi.inventory_item_id        =  bcol.config_item_id
4653   and    msi.organization_id          =  bcol.validation_org
4654   and    msi.option_specific_sourced  in ('1','2','3')
4655   and    assg.assignment_set_id       =  p_assignment_id
4656   and    assg.customer_id             is null
4657   and    assg.inventory_item_id       =  msi.inventory_item_id
4658   and    not exists (select 'X'
4659                      from   mrp_sr_assignments
4660 		     where  inventory_item_id = bcol.config_item_id
4661 		     and    organization_id is null
4662 		     and    msi.option_specific_sourced = 3)
4663 
4664 UNION
4665   select /*+ FULL(bcol) */
4666          bcol.config_item_id,
4667          bcol.line_id,
4668 	 bcol.ato_line_id,
4669          src.source_organization_id,
4670          src.VENDOR_ID,
4671          vend.VENDOR_SITE_CODE
4672 
4673   from   mrp_sr_receipt_org rcv,
4674          mrp_sr_source_org src,
4675          mrp_sr_assignments assg,
4676          bom_cto_order_lines_gt bcol,
4677          mtl_system_items msi,
4678          ap_supplier_sites_all vend
4679 
4680   where
4681          bcol.config_item_id is not null
4682   and    bcol.top_model_line_id is not null
4683   and    (bcol.perform_match in ('Y','C') or bcol.reuse_config = 'Y')
4684   and    bcol.config_creation = '3'
4685   and    nvl(bcol.wip_supply_type,-1) <> 6
4686   and    msi.inventory_item_id = bcol.config_item_id
4687   and    msi.organization_id  = bcol.validation_org
4688   and    msi.option_specific_sourced in('1','2','3')
4689   and    assg.assignment_set_id = p_assignment_id
4690   and    assg.customer_id is null
4691   and    assg.inventory_item_id = msi.inventory_item_id
4692   and    assg.sourcing_rule_id = rcv.sourcing_rule_id
4693   and    rcv.effective_date <= sysdate
4694   and    nvl(rcv.disable_date,sysdate+1)>sysdate
4695   and    rcv.SR_RECEIPT_ID = src.sr_receipt_id
4696   and    src.vendor_site_id = vend.vendor_site_id(+)
4697   and    not exists (select 'X'
4698                      from   mrp_sr_assignments
4699 		     where  inventory_item_id = bcol.config_item_id
4700 		     and    organization_id is null
4701 		     and    msi.option_specific_sourced = 3)
4702 
4703 UNION
4704   select /*+ FULL(bcol) */
4705          bcol.config_item_id,
4706          bcol.line_id,
4707 	 bcol.ato_line_id,
4708          assg.organization_id,
4709          to_number(null),--bugfix3894241
4710          null
4711 
4712   from   mrp_sr_assignments assg,
4713          bom_cto_order_lines_gt bcol,
4714          mtl_system_items msi
4715 
4716   where
4717          bcol.config_item_id is not null
4718   and    bcol.top_model_line_id is not null
4719   and    (bcol.perform_match in ('Y','C') or bcol.reuse_config = 'Y')
4720   and    bcol.config_creation = '3'
4721   and    nvl(bcol.wip_supply_type,-1)<> 6
4722   and    bcol.top_model_line_id      is null
4723   and    msi.inventory_item_id        =  bcol.config_item_id
4724   and    msi.organization_id          =  bcol.validation_org
4725   and    msi.option_specific_sourced  in ('1','2','3')
4726   and    assg.assignment_set_id       =  p_assignment_id
4727   and    assg.customer_id             is null
4728   and    assg.inventory_item_id       =  msi.inventory_item_id
4729   and    not exists (select 'X'
4730                      from   mrp_sr_assignments
4731 		     where  inventory_item_id = bcol.config_item_id
4732 		     and    organization_id is null
4733 		     and    msi.option_specific_sourced = 3);
4734 
4735 
4736   If PG_DEBUG <> 0 Then
4737     oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ATO_ITEM_ORGS: Number of records inserted = '||sql%rowcount);
4738   End if;
4739 
4740   g_pg_level := g_pg_level - 3;
4741 
4742   --Bugfix 13362916
4743   IF PG_DEBUG <> 0 THEN
4744      Print_orglist_gt;
4745   END IF;
4746 
4747 Exception
4748 
4749         WHEN FND_API.G_EXC_ERROR THEN
4750             IF PG_DEBUG <> 0 THEN
4751                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ATO_ITEM_ORGS::exp error::'
4752 			      ||to_char(l_stmt_num)
4753 			      ||'::'||sqlerrm,1);
4754             END IF;
4755 	    g_pg_level := g_pg_level - 3;
4756             x_return_status := FND_API.G_RET_STS_ERROR;
4757             cto_msg_pub.count_and_get(
4758                                  p_msg_count  => x_msg_count,
4759                                  p_msg_data   => x_msg_data
4760                                 );
4761         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4762             IF PG_DEBUG <> 0 THEN
4763                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ATO_ITEM_ORGS::exp error::'
4764 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
4765             END IF;
4766 	    g_pg_level := g_pg_level - 3;
4767             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4768             cto_msg_pub.count_and_get(
4769                                  p_msg_count  => x_msg_count,
4770                                  p_msg_data   => x_msg_data
4771                                 );
4772         WHEN OTHERS THEN
4773             IF PG_DEBUG <> 0 THEN
4774                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ATO_ITEM_ORGS::exp error::'
4775 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
4776             END IF;
4777 	    g_pg_level := g_pg_level - 3;
4778             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4779             cto_msg_pub.count_and_get(
4780                                  p_msg_count  => x_msg_count,
4781                                  p_msg_data   => x_msg_data
4782                                 );
4783 
4784 End get_ato_item_orgs;
4785 
4786 Procedure update_oss_in_bcol(
4787                               p_ato_line_id   IN         Number,
4788 			      x_oss_exists    OUT NOCOPY Varchar2,
4789 			      x_return_status OUT NOCOPY Varchar2,
4790 			      x_msg_data      OUT NOCOPY Varchar2,
4791 			      x_msg_count     OUT NOCOPY Number) is
4792 
4793    l_parent_ato_line_id         Number;
4794 
4795    TYPE parent_ato_line_tbl_type    is TABLE of Number INDEX  BY Binary_integer;
4796 
4797    l_parent_ato_line_tbl  parent_ato_line_tbl_type;
4798    i		          Number:=0;
4799    l_stmt_num             Number ;
4800    l_rows_updated         Number ;  --Bugfix 6710393
4801 
4802 
4803    /* The following cursor will get all the line from bom_cto_order_lines table.
4804       This curosr is used for creating bcol cache.
4805     */
4806 
4807    Cursor bcol_cur is
4808      select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
4809             line_id,
4810             ato_line_id,
4811 	    parent_ato_line_id,
4812             option_specific,
4813 	    perform_match
4814      from   bom_cto_order_lines_gt bcol
4815      where  ato_line_id  = p_ato_line_id;
4816 
4817 
4818   Cursor oss_line_cur is
4819      select  /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
4820              line_id,
4821              ato_line_id,
4822 	     parent_ato_line_id,
4823 	     option_specific
4824      from    bom_cto_order_lines_gt bcol
4825      where   ato_line_id  = p_ato_line_id
4826      and     option_specific = '1'
4827      order  by plan_level desc;
4828 
4829   lCnt number;  -- Bugfix 8894392
4830 
4831 
4832 Begin
4833 
4834    x_return_status := FND_API.G_RET_STS_SUCCESS;
4835    g_pg_level := g_pg_level + 3;
4836 
4837    IF PG_DEBUG <> 0 Then
4838       oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL: In UPDATE_OSS_BCOL API',5);
4839    End if;
4840    x_oss_exists :='Y';
4841 
4842    l_stmt_num := 10;
4843 
4844    update /*+ INDEX (bcol1 BOM_CTO_ORDER_LINES_GT_N1) */
4845           bom_cto_order_lines_gt bcol1
4846    set    option_specific = '1'
4847    where
4848          ato_line_id = p_ato_line_id
4849    and   exists (select /*+ INDEX (bcol2 BOM_CTO_ORDER_LINES_GT_N3) */
4850                          'X'
4851                  from    bom_cto_oss_components ossc,
4852                          bom_cto_order_lines_gt bcol2,
4853                          bom_cto_oss_orgs_list ossl
4854                  where  bcol2.parent_ato_line_id = bcol1.line_id
4855                  and    ossc.model_item_id   =  bcol1.inventory_item_id
4856                  and    ossc.option_item_id  =  bcol2.inventory_item_id
4857                  and    ossl.oss_comp_Seq_id = ossc.oss_comp_seq_id)
4858    and     nvl(bcol1.wip_supply_type,-1) <> 6 /* Talk to Sushant Sawant */
4859    and     bcol1.bom_item_type = 1
4860    returning parent_ato_line_id bulk collect into l_parent_ato_line_tbl;
4861 
4862    --Bugfix 6710393
4863    --Swaped the positions of if block with oe_debug_pub and sql%rowcount
4864    --else sql%rowcount will get rowcount of sql executed in oe_debug_pub.add api() api
4865 
4866    l_rows_updated := sql%rowcount;
4867 
4868    If PG_DEBUG <> 0 Then
4869       oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL: Number of OSS configuratinos = '
4870                                          ||l_rows_updated,5);
4871    End if;
4872    If l_rows_updated = 0 then
4873      IF PG_DEBUG <> 0 Then
4874         oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL: No OSS configuration exists..',5);
4875      End if;
4876      x_oss_exists := 'N';
4877      g_pg_level   := g_pg_level - 3;
4878      return;
4879    End if;
4880 
4881    /* Get the whole bcol data into a local cache. we will traverse in the cache
4882       instead of going to database. This is a table of records and is sparsed
4883       by line id
4884    */
4885 
4886    IF PG_DEBUG <> 0 Then
4887       oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL: Caching BCOL Data',5);
4888    End if;
4889 
4890    l_stmt_num := 20;
4891 
4892    FOR bcol_rec in bcol_cur
4893    Loop
4894 
4895       g_bcol_tbl(bcol_rec.line_id).line_id            := bcol_rec.line_id;
4896       g_bcol_tbl(bcol_rec.line_id).parent_ato_line_id := bcol_rec.parent_ato_line_id;
4897       g_bcol_tbl(bcol_rec.line_id).ato_line_id        := bcol_rec.ato_line_id;
4898       g_bcol_tbl(bcol_rec.line_id).option_specific    := bcol_rec.option_specific;
4899       g_bcol_tbl(bcol_rec.line_id).perform_match      := bcol_rec.perform_match;
4900 
4901    End Loop;
4902 
4903 
4904 
4905    /* The following part of the code will mark all the rows that are to be
4906       processed */
4907 
4908    /* Impact: The following traversal also not required */
4909 
4910       /* For the top model no need to traverse */
4911 
4912 
4913    IF PG_DEBUG <> 0 Then
4914        oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL: Falgging OSS for parents',5);
4915    End if;
4916 
4917    l_stmt_num := 30;
4918 
4919    For oss_line_rec in oss_line_cur
4920    Loop
4921       If oss_line_rec.line_id <> oss_line_rec.ato_line_id then
4922          update_parent_oss_line(p_parent_ato_line_id => oss_line_rec.parent_ato_line_id,
4923                                 x_return_status      => x_return_status,
4924 	    		        x_msg_count          => x_msg_count,
4925 	 		        x_msg_data           => x_msg_data);
4926 
4927        End if;
4928    End Loop;
4929 
4930 
4931    l_stmt_num := 40;
4932    If g_parent_rec.line_id.count > 0 then
4933      FORALL i in g_parent_rec.line_id.first..g_parent_rec.line_id.last
4934       Update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
4935              bom_cto_order_lines_gt bcol
4936       set    option_specific = g_parent_rec.option_specific(i)
4937       where  line_id = g_parent_rec.line_id(i);
4938    end if;
4939 
4940    IF PG_DEBUG <> 0 Then
4941      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL: Number of parent records updated = '
4942                                           ||g_parent_rec.line_id.count,5);
4943    End if;
4944 
4945 
4946    /* The following update statement will update all the rows where config item is
4947       matched and the config the item attribute is set to 3. In these cases, the
4948       opiton speicific source will be taken from config item. That will replace
4949       the flag determined earlier.
4950    */
4951 
4952    l_stmt_num := 50;
4953    -- Moving this update outside this procedure. Part of Bugfix 8894392.
4954    -- Reasoning: This sql updates the option_specific flag for matched CIB = 3 configs based on the
4955    -- value in msi for this config. For UPG, we are doing the processing again, so we don't need this.
4956    -- Moved this outside because wanted to keep the behaviour same for ACC and we do not have the p_mode
4957    -- parameter in this procedure.
4958    --update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
4959    /*     bom_cto_order_lines_gt bcol
4960    set    bcol.option_specific = (select msi.option_specific_sourced
4961                                   from   mtl_system_items msi
4962  	                          where  msi.inventory_item_id = bcol.config_item_id
4963 			          and    rownum =1)
4964   where  bcol.perform_match = 'Y'*/   /* We need to add config creation condition here */
4965   /*and    bcol.config_creation = '3'
4966   and    bcol.ato_line_id   = p_ato_line_id;
4967 
4968    IF PG_DEBUG <> 0 Then
4969      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL: Number of matched configs with attribute settting 3 ='
4970                                           ||sql%rowcount,5);
4971    End if;*/
4972 
4973    l_stmt_num := 60;
4974 
4975    g_parent_rec.line_id.delete;
4976    g_parent_rec.option_specific.delete;
4977 
4978    g_pg_level := g_pg_level - 3;
4979 
4980 Exception
4981         WHEN FND_API.G_EXC_ERROR THEN
4982             IF PG_DEBUG <> 0 THEN
4983                 oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL::exp error::'
4984 			      ||to_char(l_stmt_num)
4985 			      ||'::'||sqlerrm,1);
4986             END IF;
4987 	    g_pg_level := g_pg_level - 3;
4988             x_return_status := FND_API.G_RET_STS_ERROR;
4989             cto_msg_pub.count_and_get(
4990                                  p_msg_count  => x_msg_count,
4991                                  p_msg_data   => x_msg_data
4992                                 );
4993         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4994             IF PG_DEBUG <> 0 THEN
4995                 oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL::exp error::'
4996 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
4997             END IF;
4998 	    g_pg_level := g_pg_level - 3;
4999             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5000             cto_msg_pub.count_and_get(
5001                                  p_msg_count  => x_msg_count,
5002                                  p_msg_data   => x_msg_data
5003                                 );
5004         WHEN OTHERS THEN
5005             IF PG_DEBUG <> 0 THEN
5006                 oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_OSS_IN_BCOL::exp error::'
5007 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
5008             END IF;
5009 	    g_pg_level := g_pg_level - 3;
5010             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5011             cto_msg_pub.count_and_get(
5012                                  p_msg_count  => x_msg_count,
5013                                  p_msg_data   => x_msg_data
5014                                 );
5015 End update_oss_in_bcol;
5016 
5017 
5018 /*
5019 
5020 
5021    This procedure will find all the parent oss by calling the same
5022    procedure in a re-cursive way. This way, it will identify all the parents
5023    for any given oss node.
5024 
5025    ********************   UPDATE_PARENT_OSS_LINE   ***********************
5026 
5027 
5028 
5029 */
5030 
5031 
5032 Procedure update_parent_oss_line(p_parent_ato_line_id  In  Number,
5033                                  x_return_status       OUT NOCOPY Varchar2,
5034 				 x_msg_count           OUT NOCOPY Number,
5035 				 x_msg_data            OUT NOCOPY Varchar2) is
5036 
5037    l_parent_ato_line_id		Number;
5038    l_stmt_num                   Number;
5039 Begin
5040 
5041     x_return_status := FND_API.G_RET_STS_SUCCESS;
5042     l_stmt_num := 10;
5043     g_pg_level := g_pg_level + 3;
5044 
5045    /* If the parent is already marked, we don't need to mark it again
5046       and we should go up in the tree . Other wise we will mark the node
5047       as oss parent and move up*/
5048 
5049    /* The following two statments record the node and its.
5050       g_parent_rec is a recor of tables and is used for bulk update
5051       later
5052    */
5053 
5054 
5055 
5056    If PG_DEBUG <> 0 Then
5057       oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE: Inside UPDATE_PARENT_OSS_LIEN API',5);
5058       oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE: Line id = '||g_bcol_tbl(p_parent_ato_line_id).line_id,5);
5059       oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE: oss = '||g_bcol_tbl(p_parent_ato_line_id).option_specific,5);
5060    End if;
5061 
5062    l_stmt_num := 20;
5063    g_parent_rec.line_id.extend(g_tbl_index);
5064    g_parent_rec.option_specific.extend(g_tbl_index);
5065    g_parent_rec.line_id(g_tbl_index)                :=  g_bcol_tbl(p_parent_ato_line_id).line_id;
5066 
5067 
5068    l_stmt_num := 30;
5069    If g_bcol_tbl(p_parent_ato_line_id).option_specific = '1' then
5070      g_parent_rec.option_specific(g_tbl_index)        :=  '2'; /* This indicates the parent is oss also*/
5071      g_bcol_tbl(p_parent_ato_line_id).option_specific :=  '2';
5072      g_tbl_index                                      := g_tbl_index + 1;
5073    else
5074      g_parent_rec.option_specific(g_tbl_index)        := '3'; /* This is to indicate the parent is not oss
5075                                                                 by itsefl */
5076      g_bcol_tbl(p_parent_ato_line_id).option_specific := '3';
5077      g_tbl_index                                      := g_tbl_index + 1;
5078    end if;
5079 
5080 
5081    /* This will get the parent of the current node */
5082 
5083    l_parent_ato_line_id := g_bcol_tbl(p_parent_ato_line_id).parent_ato_line_id;
5084 
5085    If PG_DEBUG <> 0 Then
5086      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE: l_parent_ato_line_id = '||l_parent_ato_line_id,5);
5087      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE: new line_id = '||g_bcol_tbl(l_parent_ato_line_id).line_id,5);
5088      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE: ato_line_id = '||g_bcol_tbl(l_parent_ato_line_id).ato_line_id,5);
5089      oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE: new oss = '||g_bcol_tbl(l_parent_ato_line_id).option_specific,5);
5090    END IF;
5091 
5092    /* If the parent of the current node is already processed by some other tree,
5093       then we need not traverse the tree up. Also, if the current one is the top most
5094       then also we don't need to traverse up
5095    */
5096 
5097    l_stmt_num := 40;
5098 
5099    If (g_bcol_tbl(l_parent_ato_line_id).line_id <>
5100        g_bcol_tbl(l_parent_ato_line_id).ato_line_id) and
5101        (g_bcol_tbl(l_parent_ato_line_id).option_specific is null) Then
5102 
5103        /* This is the recursive call to traverse up in the tree
5104        */
5105        l_stmt_num := 50;
5106        update_parent_oss_line(p_parent_ato_line_id  => l_parent_ato_line_id,
5107                               x_return_status       => x_return_status,
5108 			      x_msg_count           => x_msg_count,
5109 			      x_msg_data            => x_msg_data);
5110    Elsif g_bcol_tbl(l_parent_ato_line_id).line_id = g_bcol_tbl(l_parent_ato_line_id).ato_line_id then
5111 
5112       If g_bcol_tbl(l_parent_ato_line_id).option_specific = '1' then
5113         g_parent_rec.line_id.extend(g_tbl_index);
5114         g_parent_rec.option_specific.extend(g_tbl_index);
5115         g_parent_rec.line_id(g_tbl_index) :=g_bcol_tbl(l_parent_ato_line_id).line_id;
5116         g_parent_rec.option_specific(g_tbl_index)        :=  '2'; /* This indicates the parent is oss also*/
5117         g_bcol_tbl(l_parent_ato_line_id).option_specific :=  '2';
5118         g_tbl_index                                      := g_tbl_index + 1;
5119       --This flag is set as N while inserting records. The flag should stay as N unless updated
5120       --by the update sql in update_oss_in_bcol. Added an nvl to keep the old functionality.
5121       --elsif  g_bcol_tbl(l_parent_ato_line_id).option_specific is null then
5122       elsif  (nvl(g_bcol_tbl(l_parent_ato_line_id).option_specific, 'N') = 'N') then  --Bugfix 13540153-FP(13360098)
5123         g_parent_rec.line_id.extend(g_tbl_index);
5124         g_parent_rec.option_specific.extend(g_tbl_index);
5125         g_parent_rec.line_id(g_tbl_index) :=g_bcol_tbl(l_parent_ato_line_id).line_id;
5126         g_parent_rec.option_specific(g_tbl_index)        := '3'; /* This is to indicate the parent is not oss
5127                                                                 by itsefl */
5128         g_bcol_tbl(l_parent_ato_line_id).option_specific := '3';
5129         g_tbl_index                                      := g_tbl_index + 1;
5130       end if;
5131 
5132    End if;
5133 
5134   g_pg_level := g_pg_level  - 3;
5135 
5136 Exception
5137         WHEN FND_API.G_EXC_ERROR THEN
5138             IF PG_DEBUG <> 0 THEN
5139                 oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE::exp error::'
5140 			      ||to_char(l_stmt_num)
5141 			      ||'::'||sqlerrm,1);
5142             END IF;
5143 	    g_pg_level := g_pg_level - 3;
5144             x_return_status := FND_API.G_RET_STS_ERROR;
5145             cto_msg_pub.count_and_get(
5146                                  p_msg_count  => x_msg_count,
5147                                  p_msg_data   => x_msg_data
5148                                 );
5149         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5150             IF PG_DEBUG <> 0 THEN
5151                 oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE::exp error::'
5152 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
5153             END IF;
5154 	    g_pg_level := g_pg_level - 3;
5155             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5156             cto_msg_pub.count_and_get(
5157                                  p_msg_count  => x_msg_count,
5158                                  p_msg_data   => x_msg_data
5159                                 );
5160         WHEN OTHERS THEN
5161             IF PG_DEBUG <> 0 THEN
5162                 oe_debug_pub.add(lpad(' ',g_pg_level)||'UPDATE_PARENT_OSS_LINE::exp error::'
5163 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
5164             END IF;
5165 	    g_pg_level := g_pg_level - 3;
5166             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5167             cto_msg_pub.count_and_get(
5168                                  p_msg_count  => x_msg_count,
5169                                  p_msg_data   => x_msg_data
5170                                 );
5171 
5172 End Update_parent_oss_line;
5173 
5174 
5175 Procedure get_sourcing_data(
5176                             p_ato_line_id         IN  Number,
5177 			    x_return_status   OUT NOCOPY Varchar2,
5178 			    x_msg_data        OUT NOCOPY Varchar2,
5179 			    x_msg_count       OUT NOCOPY Number) is
5180    l_stmt_num   Number;
5181 
5182 begin
5183    g_pg_level := g_pg_level + 3;
5184    x_return_status := FND_API.G_RET_STS_SUCCESS;
5185    l_stmt_num := 10;
5186 
5187    IF PG_DEBUG <> 0 Then
5188       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_SOURCING_DATA: Inside GET_SOURCING_DATA API',5);
5189       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_SOURCING_DATA: Assignment set id ='||g_def_assg_set,5);
5190    End if;
5191 
5192    Insert into bom_cto_oss_source_gt
5193                    (
5194                     Inventory_item_id,
5195                     Line_id,
5196 		    ato_line_id,
5197                     config_item_id,
5198                     Rcv_org_id,
5199                     Source_org_id,
5200                     Customer_id,
5201                     Ship_to_site_id,
5202                     Vendor_id,
5203                     Vendor_site_code,
5204                     rank,
5205                     Allocation,
5206                     Source_type,
5207                     source_rule_id,
5208                     sr_receipt_id,
5209                     sr_source_id,
5210                     assignment_id
5211                    )
5212 
5213    select           /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
5214                     bcol.inventory_item_id,
5215                     bcol.line_id,
5216 		    p_ato_line_id,
5217                     null,
5218                     nvl(rcv.receipt_organization_id,assg.organization_id),
5219                     src.source_organization_id,
5220                     assg.customer_id,
5221                     assg.ship_to_site_id,
5222                     src.VENDOR_ID,
5223                     vend.VENDOR_SITE_code,
5224                     src.RANK,
5225                     src.ALLOCATION_PERCENT,
5226                     src.SOURCE_TYPE,
5227                     assg.sourcing_rule_id,
5228                     rcv.sr_receipt_id,
5229                     src.sr_source_id,
5230                     assg.assignment_id
5231 
5232    from
5233                     mrp_sr_receipt_org rcv,
5234                     mrp_sr_source_org src,
5235                     mrp_sr_assignments assg,
5236                     mrp_sourcing_rules rule,
5237                     po_vendor_sites_all vend,
5238 		    bom_cto_order_lines_gt bcol
5239    where
5240                     assg.assignment_set_id   = g_def_assg_set
5241 	      and   bcol.ato_line_id         = p_ato_line_id
5242 	      and   bcol.config_item_id      is null
5243               and   bcol.option_specific     in ('1','2','3')
5244 	      and   assg.inventory_item_id   = bcol.inventory_item_id
5245               and   assg.sourcing_rule_id    = rcv.sourcing_rule_id
5246               and   assg.sourcing_rule_id    = rule.sourcing_rule_id
5247               and   rule.planning_active     = 1
5248               and   rcv.effective_date      <= sysdate
5249               and   nvl(rcv.disable_date,sysdate+1)>sysdate
5250               and   rcv.SR_RECEIPT_ID        = src.sr_receipt_id
5251               and   src.vendor_site_id = vend.vendor_site_id(+);
5252 
5253 
5254    If PG_DEBUG <> 0 Then
5255       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_SOURCING_DATA: Number of records inserted in 1st sql ='||sql%rowcount,5);
5256       -- 13362916
5257       Print_source_gt;
5258    End if;
5259   Insert into bom_cto_oss_source_gt
5260                    (
5261                     Inventory_item_id,
5262                     Line_id,
5263 		    ato_line_id,
5264                     config_item_id,
5265                     Rcv_org_id,
5266                     Source_org_id,
5267                     Customer_id,
5268                     Ship_to_site_id,
5269                     Vendor_id,
5270                     Vendor_site_code,
5271                     rank,
5272                     Allocation,
5273                     Source_type,
5274                     source_rule_id,
5275                     sr_receipt_id,
5276                     sr_source_id,
5277                     assignment_id,
5278 		    valid_flag
5279                    )
5280 
5281    select       /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
5282                     bcol.inventory_item_id,
5283                     bcol.line_id,
5284 		    p_ato_line_id,
5285                     bcol.config_item_id,
5286                     nvl(rcv.receipt_organization_id,assg.organization_id),
5287                     src.source_organization_id,
5288                     assg.customer_id,
5289                     assg.ship_to_site_id,
5290                     src.VENDOR_ID,
5291                     vend.VENDOR_SITE_code,
5292                     src.RANK,
5293                     src.ALLOCATION_PERCENT,
5294                     src.SOURCE_TYPE,
5295                     assg.sourcing_rule_id,
5296                     rcv.sr_receipt_id,
5297                     src.sr_source_id,
5298                     assg.assignment_id,
5299 		    'Y'
5300 
5301    from
5302                     mrp_sr_receipt_org rcv,
5303                     mrp_sr_source_org src,
5304                     mrp_sr_assignments assg,
5305                     mrp_sourcing_rules rule,
5306                     po_vendor_sites_all vend,
5307 		    bom_cto_order_lines_gt bcol
5308    where
5309                     assg.assignment_set_id   = g_def_assg_set
5310 	      and   bcol.ato_line_id         = p_ato_line_id
5311 	      and   bcol.config_creation     = 3
5312 	      and   bcol.option_specific     in ('1','2','3')
5313 	      and   bcol.config_item_id      is not null
5314 	      and   assg.inventory_item_id   = bcol.config_item_id
5315               and   assg.sourcing_rule_id    = rcv.sourcing_rule_id
5316               and   assg.sourcing_rule_id    = rule.sourcing_rule_id
5317               and   rule.planning_active     = 1
5318               and   rcv.effective_date      <= sysdate
5319               and   nvl(rcv.disable_date,sysdate+1)>sysdate
5320               and   rcv.SR_RECEIPT_ID        = src.sr_receipt_id
5321               and   src.vendor_site_id = vend.vendor_site_id(+);
5322    If PG_DEBUG <> 0 Then
5323       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_SOURCING_DATA: Number of records inserted in 2nd sql ='||sql%rowcount,5);
5324       -- Bug 13362916
5325       Print_source_gt;
5326    End if;
5327 
5328 /*
5329 
5330    If p_config_creation = '3' Then
5331 
5332       Insert into bom_cto_oss_source_gt
5333 	           (
5334                     Inventory_item_id,
5335 		    Line_id,
5336 		    config_item_id,
5337 		    Rcv_org_id,
5338 		    Source_org_id,
5339 		    Customer_id,
5340 		    Ship_to_site_id,
5341 		    Vendor_id,
5342 		    Vendor_site_code,
5343                     rank,
5344 		    Allocation,
5345 		    Source_type,
5346 		    source_rule_id,
5347 		    sr_receipt_id,
5348 		    sr_source_id,
5349 		    assignment_id
5350 		   )
5351 
5352       select
5353 	            p_item_id,
5354 	            p_line_id,
5355 	            p_config_item_id,
5356 	            nvl(rcv.receipt_organization_id,assg.organization_id),
5357                     src.source_organization_id,
5358 	            assg.customer_id,
5359 	            assg.ship_to_site_id,
5360                     src.VENDOR_ID,
5361                     vend.VENDOR_SITE_code,
5362                     src.RANK,
5363                     src.ALLOCATION_PERCENT,
5364                     src.SOURCE_TYPE,
5365 	            assg.sourcing_rule_id,
5366 	            rcv.sr_receipt_id,
5367 	            src.sr_source_id,
5368 	            assg.assignment_id
5369 
5370       from
5371                     mrp_sr_receipt_org rcv,
5372                     mrp_sr_source_org src,
5373                     mrp_sr_assignments assg,
5374 	            mrp_sourcing_rules rule,
5375 	            po_vendor_sites_all vend
5376       where
5377 	            assg.assignment_set_id   = g_def_assg_set
5378 	      and   assg.inventory_item_id   = p_item_id
5379               and   assg.sourcing_rule_id    = rcv.sourcing_rule_id
5380 	      and   assg.sourcing_rule_id    = rule.sourcing_rule_id
5381 	      and   rule.planning_active     = 1
5382               and   rcv.effective_date      <= sysdate
5383               and   nvl(rcv.disable_date,sysdate+1)>sysdate
5384               and   rcv.SR_RECEIPT_ID        = src.sr_receipt_id
5385 	      and   src.vendor_site_id = vend.vendor_site_id(+);
5386 
5387       If PG_DEBUG <> 0 Then
5388          oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_SOURCING_DATA: Number of assignment records inserted = '
5389 	                                      ||sql%rowcount,5);
5390       End if;
5391 
5392    elsif p_config_creation in ('1','2') then
5393       null;
5394    End if;
5395 */
5396 
5397    g_pg_level := g_pg_level - 3;
5398 
5399 End get_sourcing_data;
5400 
5401 
5402 Procedure Process_order_for_oss (P_ato_line_id    IN  Number,
5403                                  P_calling_mode   IN  Varchar2,
5404                                  x_return_status  OUT NOCOPY Varchar2,
5405 				 x_msg_data       OUT NOCOPY Varchar2,
5406 				 x_msg_count      OUT NOCOPY Number) is
5407 
5408   l_stmt_num   Number;
5409 
5410 
5411   -- Bug Fix 4093235
5412   -- Added the condition to the cursor
5413   -- Cursor oss_models is not (perform_match = 'Y' and config_creation = '3').
5414   -- This way we will not pickup matched cib 3 model line for pruning process as
5415   -- the sourcing rule already pruned and the sourcing data is gathered from
5416   -- config item.
5417 
5418   -- Commenting as part of Bugfix 8894392
5419      /*cursor oss_models is
5420      select*/ /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
5421             /*line_id,
5422             ato_line_id,
5423             option_specific,
5424 	    inventory_item_id,
5425 	    config_item_id,
5426 	    perform_match,
5427 	    config_creation
5428      from   bom_cto_order_lines_gt bcol
5429      where  ato_line_id = p_ato_line_id
5430      and    option_specific in ('1','2','3')
5431      and   not (perform_match = 'Y' and config_creation = '3') -- 4093235
5432      order  by plan_level desc;*/
5433 
5434      -- Reasoning for adding this new cursor and commenting out the old one:
5435      -- The old cursor, because of the condition 'and   not (perform_match = 'Y' and config_creation = '3')'
5436      -- didn't pick up such configs for processing. After bugfix 8894392, we need to process matched CIB = 3
5437      -- configs also. So forked the cursor based on p_mode. For mode = ACC, the behaviour stays same as old
5438      -- cursor. For mode = UPG, the the cursor picks up matched CIB = 3 configs for processing.
5439 
5440      -- Adding this cursor as part of Bugfix 8894392
5441      cursor oss_models is
5442      select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
5443             line_id,
5444             ato_line_id,
5445             option_specific,
5446 	    inventory_item_id,
5447 	    config_item_id,
5448 	    perform_match,
5449 	    config_creation,
5450 	    plan_level
5451      from   bom_cto_order_lines_gt bcol
5452      where  ato_line_id = p_ato_line_id
5453      and    option_specific in ('1','2','3')
5454      --Bugfix 11858888: During ATP, if match profile is OFF, the perform_match flag stays
5455      --null. The cursor doesn't pick up any lines for OSS processing resulting in wrong
5456      --sourcing data returned to GOP.
5457         --
5458         -- bug 13324638
5459         -- The cursor should not pick the data if the parent is matched and CIB is 3.
5460         -- The current code is eliminating any child config lines that are matched, which is
5461         -- incorrect
5462         --
5463         -- and   not (nvl(perform_match,'N') = 'Y' and config_creation = '3') -- 4093235
5464      and NOT EXISTS (SELECT 1 from bom_cto_order_lines_gt bcol2
5465                          WHERE ato_line_id = p_ato_line_id
5466                            AND ato_line_id = line_id -- indicating parent
5467                            AND nvl(perform_match,'N') = 'Y'
5468                            AND config_creation = '3')
5469      and   p_calling_mode in ('ACC', 'ATP')  --Bugfix 8894392: Added mode ATP in the cursor
5470      union
5471      select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
5472             line_id,
5473             ato_line_id,
5474             option_specific,
5475 	    inventory_item_id,
5476 	    config_item_id,
5477 	    perform_match,
5478 	    config_creation,
5479 	    plan_level
5480      from   bom_cto_order_lines_gt bcol
5481      where  ato_line_id = p_ato_line_id
5482      and    option_specific in ('1','2','3')
5483      -- and   not (perform_match = 'Y' and config_creation = '3') -- 4093235
5484      -- Bugfix 8894392. In case of UPG, we need to prune the tree again. Otherwise
5485      -- the config BOM gets created in several orgs which are not valid as per OSS.
5486      and   p_calling_mode = 'UPG'
5487      order  by plan_level desc;
5488 
5489   x_exp_error_code Number :=0;
5490 
5491     --
5492     -- bug  13324638
5493     --
5494     CURSOR bcolgt_debug_cur IS
5495            SELECT ATO_LINE_ID,
5496              CONFIG_ITEM_ID,
5497              INVENTORY_ITEM_ID,
5498              LINE_ID,
5499              LINK_TO_LINE_ID,
5500              PARENT_ATO_LINE_ID,
5501              PERFORM_MATCH,
5502              PLAN_LEVEL,
5503              SHIP_FROM_ORG_ID,
5504              TOP_MODEL_LINE_ID,
5505              HEADER_ID,
5506              OPTION_SPECIFIC,
5507              REUSE_CONFIG,
5508              CONFIG_CREATION,
5509              VALIDATION_ORG
5510            FROM bom_cto_order_lines_gt;
5511 
5512 Begin
5513   x_return_status := FND_API.G_RET_STS_SUCCESS;
5514   l_stmt_num := 10;
5515   g_pg_level := g_pg_level + 3;
5516 
5517   If PG_DEBUG <> 0 Then
5518      oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Inside PROCESS_ORDER_FOR_OSS API',5);
5519      oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: P_ato_line_id:' || P_ato_line_id,5);
5520      oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: P_calling_mode:'|| P_calling_mode,5);
5521   End if;
5522  -- bug 13324638
5523   If PG_DEBUG <> 0 Then
5524         oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: =======================================================',5);
5525         oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Printing bom_cto_order_lines_gt data ' || P_ato_line_id,5);
5526         oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: =======================================================',5);
5527         oe_debug_pub.add(lpad(' ',g_pg_level)||'ATO_LINE_ID, CONFIG_ITEM_ID, INVENTORY_ITEM_ID, LINE_ID, LINK_TO_LINE_ID, '||
5528                                                'PARENT_ATO_LINE_ID, PERFORM_MATCH, PLAN_LEVEL, SHIP_FROM_ORG_ID, TOP_MODEL_LINE_ID, '||
5529                                                'HEADER_ID, OPTION_SPECIFIC, REUSE_CONFIG, VALIDATION_ORG',5);
5530 
5531         FOR bcolgtcur in bcolgt_debug_cur LOOP
5532             oe_debug_pub.add(lpad(' ',g_pg_level)||bcolgtcur.ATO_LINE_ID||','||
5533                                                    bcolgtcur.CONFIG_ITEM_ID||','||
5534                                                    bcolgtcur.INVENTORY_ITEM_ID||','||
5535                                                    bcolgtcur.LINE_ID||','||
5536                                                    bcolgtcur.LINK_TO_LINE_ID||','||
5537                                                    bcolgtcur.PARENT_ATO_LINE_ID||','||
5538                                                    bcolgtcur.PERFORM_MATCH||','||
5539                                                    bcolgtcur.PLAN_LEVEL||','||
5540                                                    bcolgtcur.SHIP_FROM_ORG_ID||','||
5541                                                    bcolgtcur.TOP_MODEL_LINE_ID||','||
5542                                                    bcolgtcur.HEADER_ID||','||
5543                                                    bcolgtcur.OPTION_SPECIFIC||','||
5544                                                    bcolgtcur.REUSE_CONFIG||','||
5545                                                    bcolgtcur.VALIDATION_ORG,5);
5546         END LOOP;
5547         oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: =======================================================',5);
5548   End if;
5549 
5550   l_stmt_num := 20;
5551 
5552   delete /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N1) */
5553   from bom_cto_oss_orgslist_gt oss_lis
5554   where ato_line_id = p_ato_line_id;
5555 
5556   For oss_model_rec in oss_models
5557   Loop
5558 
5559     If PG_DEBUG <> 0 Then
5560       oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Inside PROCESS_ORDER_FOR_OSS API....',5);
5561       oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: OSS process for line id = '
5562                                            ||oss_model_rec.line_id,5);
5563     End if;
5564 
5565 
5566     /* If the config item is matched and the item creation attribute is
5567        set to 3, then we can take the sourcing from config item sourcing.
5568        we don't need to prune the tree.
5569     */
5570 
5571     l_Stmt_num := 30;
5572 
5573 
5574     if oss_model_rec.option_specific in ('1','2') then
5575 
5576        IF PG_DEBUG <> 0 Then
5577           oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Calling Prune OSS Config..',5);
5578        End if;
5579 
5580        l_stmt_num := 70;
5581        prune_oss_config(
5582 	               p_model_line_id  => oss_model_rec.line_id,
5583 	               p_model_item_id  => oss_model_rec.inventory_item_id,
5584 		       p_config_item_id => oss_model_rec.config_item_id,
5585 		       p_calling_mode   => p_calling_mode,
5586                        p_ato_line_id    => p_ato_line_id,
5587 		       x_exp_error_code => x_exp_error_code,
5588 	               x_return_status  => x_return_status,
5589 		       x_msg_count      => x_msg_count,
5590 		       x_msg_data       => x_msg_data
5591 			 );
5592 
5593        If x_return_status  = FND_API.G_RET_STS_ERROR Then
5594           IF PG_DEBUG <> 0 Then
5595              oe_debug_pub.add(lpad(' ',g_pg_level)||
5596 	                      'GET_OSS_ORGS_LIST: Exepected error occurred in prune_oss_config API',5);
5597           End if;
5598           raise FND_API.G_EXC_ERROR;
5599 
5600        Elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
5601 
5602           IF PG_DEBUG <> 0 Then
5603              oe_debug_pub.add(lpad(' ',g_pg_level)||
5604 	                   'GET_OSS_ORGS_LIST: Un Exepected error occurred in prune_oss_config API',5);
5605           End if;
5606           raise FND_API.G_EXC_UNEXPECTED_ERROR;
5607 
5608        End if; /* x_return_status  = FND_API.G_RET_STS_ERROR */
5609 
5610 
5611        If x_exp_error_code <> 0 Then
5612           l_stmt_num :=80;
5613 
5614 	  update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
5615 	         bom_cto_order_lines_gt bcol
5616 	  set    oss_error_code = x_exp_error_code
5617 	  where  line_id = oss_model_rec.line_id;
5618 
5619           If PG_DEBUG <> 0 Then
5620 	     oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Setting current model error code tp 350',5);
5621 	  End if;
5622 
5623 	  If oss_model_rec.line_id <> oss_model_rec.ato_line_id then
5624 
5625 	     l_stmt_num := 90;
5626 
5627 	     update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
5628 	            bom_cto_order_lines_gt bcol
5629 	     Set    oss_error_code = 360
5630 	     where  line_id  = oss_model_rec.ato_line_id;
5631 	     exit;
5632 
5633 	     If PG_DEBUG <> 0 Then
5634 	        oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: setting parent model error code to 360',5);
5635 	     End if;
5636 	  End if; /* oss_model_rec.line_id <> oss_model_rec.ato_line_id */
5637 
5638        End if; /* x_exp_error_code <> 0 */
5639 
5640        If PG_DEBUG <> 0 Then
5641           oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: After prune oss config.',5);
5642        End if;
5643 
5644     End if; /* oss_model_rec.option_specific in ('1','2') */
5645 
5646     If PG_DEBUG <> 0 Then
5647        oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Option Specific = '
5648                                             ||oss_model_rec.option_specific,5);
5649        oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Exp Error Code = '
5650                                             || x_exp_error_code,5);
5651     End if;
5652 
5653     l_stmt_num := 100;
5654 
5655     If oss_model_rec.option_specific in ('2','3') and nvl(x_exp_error_code,0) = 0 then
5656 
5657        If PG_DEBUG <> 0 Then
5658           oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Before Prune Parent oss config API',5);
5659        End if;
5660        l_stmt_num := 110;
5661 
5662        Prune_parent_oss_config(
5663 	                       p_model_line_id  => oss_model_rec.line_id,
5664 	  	               p_model_item_id  => oss_model_rec.inventory_item_id,
5665 			       p_calling_mode   => p_calling_mode,
5666                                p_ato_line_id    => p_ato_line_id,
5667 			       x_exp_error_code => x_exp_error_code,
5668 	                       x_return_status  => x_return_status,
5669 			       x_msg_count      => x_msg_count,
5670 			       x_msg_data       => x_msg_data
5671 			      );
5672 
5673        If x_return_status  = FND_API.G_RET_STS_ERROR Then
5674           IF PG_DEBUG <> 0 Then
5675              oe_debug_pub.add(lpad(' ',g_pg_level)||
5676                      'PROCESS_ORDER_FOR_OSS: Exepected error occurred in prune_parent_oss_config API',5);
5677           End if;
5678           raise FND_API.G_EXC_ERROR;
5679        elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR Then
5680           IF PG_DEBUG <> 0 Then
5681               oe_debug_pub.add(lpad(' ',g_pg_level)||
5682                   'PROCESS_ORDER_FOR_OSS: Un Exepected error occurred in prune_parent_oss_config API',5);
5683           End if;
5684           raise FND_API.G_EXC_UNEXPECTED_ERROR;
5685        End if; /* x_return_status  = FND_API.G_RET_STS_ERROR */
5686 
5687        If x_exp_error_code <> 0 Then
5688           l_stmt_num := 120;
5689 	  update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
5690 	         bom_cto_order_lines_gt bcol
5691 	  set    oss_error_code = x_exp_error_code
5692 	  where  line_id = oss_model_rec.line_id;
5693 
5694           If PG_DEBUG <> 0 Then
5695 	     oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: Setting current model error code tp 350',5);
5696 	  End if;
5697 
5698     	  If oss_model_rec.line_id <> oss_model_rec.ato_line_id then
5699 	     l_stmt_num := 130;
5700 	     update /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
5701 	            bom_cto_order_lines_gt bcol
5702 	     set    oss_error_code = 360
5703 	     where  line_id  = oss_model_rec.ato_line_id;
5704              If PG_DEBUG <> 0 Then
5705  	        oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS: setting parent model error code to 360',5);
5706 	     End if;
5707 	  end if; /*oss_model_rec.line_id <> oss_model_rec.ato_line_id*/
5708        End if; /* x_exp_error_code */
5709 
5710 
5711     End if; /* oss_model_rec.option_specific in ('2','3') and x_exp_error_code = 0 */
5712 
5713   End Loop;
5714 
5715   g_pg_level := g_pg_level  - 3;
5716 
5717 Exception
5718         WHEN FND_API.G_EXC_ERROR THEN
5719             IF PG_DEBUG <> 0 THEN
5720                 oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS::exp error::'
5721 			      ||to_char(l_stmt_num)
5722 			      ||'::'||sqlerrm,1);
5723             END IF;
5724 	    g_pg_level := g_pg_level - 3;
5725             x_return_status := FND_API.G_RET_STS_ERROR;
5726             cto_msg_pub.count_and_get(
5727                                  p_msg_count  => x_msg_count,
5728                                  p_msg_data   => x_msg_data
5729                                 );
5730         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5731             IF PG_DEBUG <> 0 THEN
5732                 oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS::exp error::'
5733 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
5734             END IF;
5735 	    g_pg_level := g_pg_level - 3;
5736             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5737             cto_msg_pub.count_and_get(
5738                                  p_msg_count  => x_msg_count,
5739                                  p_msg_data   => x_msg_data
5740                                 );
5741         WHEN OTHERS THEN
5742             IF PG_DEBUG <> 0 THEN
5743                 oe_debug_pub.add(lpad(' ',g_pg_level)||'PROCESS_ORDER_FOR_OSS::exp error::'
5744 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
5745             END IF;
5746 	    g_pg_level := g_pg_level - 3;
5747             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5748             cto_msg_pub.count_and_get(
5749                                  p_msg_count  => x_msg_count,
5750                                  p_msg_data   => x_msg_data
5751                                 );
5752 
5753 End PROCESS_ORDER_FOR_OSS;
5754 
5755 Procedure COPY_TO_BCOL_TEMP(
5756                             p_ato_line_id   IN  Number,
5757 			    x_return_status OUT NOCOPY Varchar2,
5758 			    x_msg_data      OUT NOCOPY Varchar2,
5759 			    x_msg_count     OUT NOCOPY Number) is
5760    l_stmt_num   Number;
5761 Begin
5762 
5763    g_pg_level := g_pg_level + 3;
5764    x_return_status := FND_API.G_RET_STS_SUCCESS;
5765    l_stmt_num := 10;
5766 
5767    If pg_debug <> 0 Then
5768      oe_debug_pub.add(lpad(' ',g_pg_level)||'COPY_TO_BCOL_TEMP: Inside Copy to Bcol Temp API',5);
5769    end if;
5770 
5771    delete /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
5772    from bom_cto_order_lines_gt bcol
5773    where  ato_line_id = p_ato_line_id;
5774 
5775    INSERT into bom_cto_order_lines_gt(
5776 				      ATO_LINE_ID,
5777 				      BATCH_ID,
5778 				      BOM_ITEM_TYPE,
5779 				      COMPONENT_CODE,
5780 				      COMPONENT_SEQUENCE_ID,
5781 				      CONFIG_ITEM_ID,
5782 				      INVENTORY_ITEM_ID,
5783 				      ITEM_TYPE_CODE,
5784 				      LINE_ID,
5785 				      LINK_TO_LINE_ID,
5786 				      ORDERED_QUANTITY,
5787 				      ORDER_QUANTITY_UOM,
5788 				      PARENT_ATO_LINE_ID,
5789 				      PERFORM_MATCH,
5790 				      PLAN_LEVEL,
5791 				      SCHEDULE_SHIP_DATE,
5792 				      SHIP_FROM_ORG_ID,
5793 				      TOP_MODEL_LINE_ID,
5794 				      WIP_SUPPLY_TYPE,
5795 				      HEADER_ID,
5796 				      OPTION_SPECIFIC,
5797 				      REUSE_CONFIG,
5798 				      QTY_PER_PARENT_MODEL,
5799 				      CONFIG_CREATION,
5800 				      program_id	--Bugfix 8894392
5801 				     )
5802 			Select  /*+ INDEX (bcol_upg BOM_CTO_ORDER_LINES_UPG_N4) */
5803     				      ATO_LINE_ID,
5804 				      BATCH_ID,
5805 				      BOM_ITEM_TYPE,
5806 				      COMPONENT_CODE,
5807 				      COMPONENT_SEQUENCE_ID,
5808 				      CONFIG_ITEM_ID,
5809 				      INVENTORY_ITEM_ID,
5810 				      ITEM_TYPE_CODE,
5811 				      LINE_ID,
5812 				      LINK_TO_LINE_ID,
5813 				      ORDERED_QUANTITY,
5814 				      ORDER_QUANTITY_UOM,
5815 				      PARENT_ATO_LINE_ID,
5816 				      PERFORM_MATCH,
5817 				      PLAN_LEVEL,
5818 				      SCHEDULE_SHIP_DATE,
5819 				      SHIP_FROM_ORG_ID,
5820 				      TOP_MODEL_LINE_ID,
5821 				      WIP_SUPPLY_TYPE,
5822 				      HEADER_ID,
5823 				      OPTION_SPECIFIC,
5824 				      REUSE_CONFIG,
5825 				      QTY_PER_PARENT_MODEL,
5826 				      CONFIG_CREATION,
5827 				      program_id	--Bugfix 8894392
5828 
5829 			from          bom_cto_order_lines_upg bcol_upg
5830 			where         ato_line_id = p_ato_line_id;
5831 
5832 
5833    If PG_DEBUG <> 0 Then
5834       oe_debug_pub.add(lpad(' ',g_pg_level)||'COPY_TO_BCOL_TEMP: Number of lines inserted in to temp '
5835                                            || sql%rowcount,5);
5836    End if;
5837    g_pg_level := g_pg_level - 3;
5838 End COPY_TO_BCOL_TEMP;
5839 
5840 
5841 /*
5842     This procedure will get the order sourcing data
5843     by travelling the whole sourcing chain
5844 */
5845 
5846 Procedure Get_order_sourcing_data(
5847                                 p_ato_line_id   IN   Number,
5848                                 x_return_status OUT NOCOPY  Varchar2,
5849                                 x_msg_count     OUT NOCOPY  Number,
5850                                 x_msg_data      OUT NOCOPY  Varchar2) is
5851    cursor model_lines_cur is
5852           select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
5853 	         line_id,
5854                  inventory_item_id,
5855                  option_specific,
5856                  parent_ato_line_id,
5857 		 ato_line_id
5858           from   bom_cto_order_lines_gt bcol
5859           where  ato_line_id = p_ato_line_id
5860           and    nvl(wip_supply_type,-1) <> '6'
5861           and    bom_item_type = '1'
5862           and    option_specific  in ('1','2','3')
5863 	  and    config_creation <> '3'
5864           order by plan_level;
5865 
5866   Cursor mfg_orgs_cur(p_line_id Number) is
5867          select /*+ INDEX (oss_lis BOM_CTO_OSS_ORGSLIST_GT_N2) */
5868 	        organization_id
5869          from   bom_cto_oss_orgslist_gt oss_lis
5870          where  line_id = p_line_id;
5871 
5872   x_assg_list assg_rec;
5873   l_stmt_num   Number;
5874   l_parent_line_id    Number;
5875   lCnt number;  -- Bugfix 8894392
5876 Begin
5877 
5878    l_stmt_num := 10;
5879    g_pg_level := g_pg_level + 3;
5880    x_return_status := FND_API.G_RET_STS_SUCCESS;
5881 
5882    delete /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N1) */
5883    from bom_cto_oss_source_gt oss_src where ato_line_id = p_ato_line_id;
5884 
5885    If PG_DEBUG <> 0 Then
5886       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCNIG_DATA: Inside GET_ORDER_SOURCING_DATA API',5);
5887    End if;
5888 
5889    Insert
5890    into bom_cto_oss_orgslist_gt(
5891                                line_id,
5892                                organization_id,
5893                                ato_line_id
5894                               )
5895    select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_U1) */
5896           -1,
5897           ship_from_org_id,
5898           p_ato_line_id
5899    from   bom_cto_order_lines_gt bcol
5900    where  line_id = p_ato_line_id;
5901 
5902    l_stmt_num := 20;
5903 
5904    For model_lines_rec in model_lines_cur
5905    Loop
5906       If PG_DEBUG <> 0 Then
5907          oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCNIG_DATA: Processing model line = '||model_lines_rec.line_id,5);
5908 	 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCNIG_DATA: Processing model Item = '||model_lines_rec.inventory_item_id,5);
5909       End if;
5910 
5911       l_stmt_num := 30;
5912 
5913       If model_lines_rec.line_id = model_lines_rec.ato_line_id then
5914          l_parent_line_id := -1;
5915       else
5916          l_parent_line_id := model_lines_rec.parent_ato_line_id;
5917       End if;
5918       g_assg_list.delete;
5919       For mfg_orgs_rec in mfg_orgs_cur(l_parent_line_id)
5920       Loop
5921          If PG_DEBUG <> 0 Then
5922             oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCNIG_DATA: get sourcing chain from org = '||mfg_orgs_rec.organization_id,5);
5923          End if;
5924          l_stmt_num := 40;
5925          Traverse_sourcing_chain(
5926                                  p_item_id         => model_lines_rec.inventory_item_id,
5927                                  p_org_id          => mfg_orgs_rec.organization_id,
5928                                  p_line_id         => model_lines_rec.line_id,
5929                                  p_option_specific => model_lines_rec.option_specific,
5930                                  p_ato_line_id     => p_ato_line_id,
5931                                  x_assg_list       => x_assg_list,
5932                                  x_return_status   => x_return_status,
5933                                  x_msg_data        => x_msg_data,
5934                                  x_msg_count       => x_msg_count);
5935       End loop;
5936    End Loop;
5937 
5938    If PG_DEBUG <> 0 Then
5939             oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCNIG_DATA: after the loop on crsr model_lines_cur',5);
5940    End if;
5941 
5942    l_stmt_num := 41;
5943    if x_assg_list.assignment_id.count <> 0 Then
5944     if PG_DEBUG <> 0 Then
5945         oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA: Before inserting the assignments into temp table',5);
5946 	oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA: Assignment count = '||x_assg_list.assignment_id.count,1);
5947      end if;
5948 
5949      l_stmt_num := 50;
5950      FORALL i in x_assg_list.assignment_id.first..x_assg_list.assignment_id.last
5951       Insert into bom_cto_oss_source_gt
5952                    (
5953                     Inventory_item_id,
5954                     Line_id,
5955 		    ato_line_id,
5956                     config_item_id,
5957                     Rcv_org_id,
5958                     Source_org_id,
5959                     Customer_id,
5960                     Ship_to_site_id,
5961                     Vendor_id,
5962                     Vendor_site_code,
5963                     rank,
5964                     Allocation,
5965                     Source_type,
5966                     source_rule_id,
5967                     sr_receipt_id,
5968                     sr_source_id,
5969                     assignment_id
5970                    )
5971 
5972              select
5973                     assg.inventory_item_id,
5974                     x_assg_list.line_id(i),
5975 		    p_ato_line_id,
5976                     null,
5977                     nvl(rcv.receipt_organization_id,assg.organization_id),
5978                     src.source_organization_id,
5979                     assg.customer_id,
5980                     assg.ship_to_site_id,
5981                     src.VENDOR_ID,
5982                     vend.VENDOR_SITE_code,
5983                     src.RANK,
5984                     src.ALLOCATION_PERCENT,
5985                     src.SOURCE_TYPE,
5986                     assg.sourcing_rule_id,
5987                     rcv.sr_receipt_id,
5988                     src.sr_source_id,
5989                     assg.assignment_id
5990 
5991       from
5992                     mrp_sr_receipt_org rcv,
5993                     mrp_sr_source_org src,
5994                     mrp_sr_assignments assg,
5995                     mrp_sourcing_rules rule,
5996                     po_vendor_sites_all vend
5997       where
5998                     assg.assignment_set_id   = g_def_assg_set
5999               and   assg.assignment_id       = x_assg_list.assignment_id(i)
6000               and   assg.sourcing_rule_id    = rcv.sourcing_rule_id
6001               and   assg.sourcing_rule_id    = rule.sourcing_rule_id
6002               and   rule.planning_active     = 1
6003               and   rcv.effective_date      <= sysdate
6004               and   nvl(rcv.disable_date,sysdate+1)>sysdate
6005               and   rcv.SR_RECEIPT_ID        = src.sr_receipt_id
6006               and   src.vendor_site_id = vend.vendor_site_id(+);
6007    End if;
6008 
6009 
6010 
6011    IF PG_DEBUG <> 0 Then
6012       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA: Before inserting Model attribute 3 lines',5);
6013    End if;
6014 
6015    l_stmt_num := 60;
6016    Insert into bom_cto_oss_source_gt
6017                    (
6018                     Inventory_item_id,
6019                     Line_id,
6020 		    ato_line_id,
6021                     config_item_id,
6022                     Rcv_org_id,
6023                     Source_org_id,
6024                     Customer_id,
6025                     Ship_to_site_id,
6026                     Vendor_id,
6027                     Vendor_site_code,
6028                     rank,
6029                     Allocation,
6030                     Source_type,
6031                     source_rule_id,
6032                     sr_receipt_id,
6033                     sr_source_id,
6034                     assignment_id
6035                    )
6036 
6037    select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
6038                     bcol.inventory_item_id,
6039                     bcol.line_id,
6040 		    p_ato_line_id,
6041                     null,
6042                     nvl(rcv.receipt_organization_id,assg.organization_id),
6043                     src.source_organization_id,
6044                     assg.customer_id,
6045                     assg.ship_to_site_id,
6046                     src.VENDOR_ID,
6047                     vend.VENDOR_SITE_code,
6048                     src.RANK,
6049                     src.ALLOCATION_PERCENT,
6050                     src.SOURCE_TYPE,
6051                     assg.sourcing_rule_id,
6052                     rcv.sr_receipt_id,
6053                     src.sr_source_id,
6054                     assg.assignment_id
6055 
6056    from
6057                     mrp_sr_receipt_org rcv,
6058                     mrp_sr_source_org src,
6059                     mrp_sr_assignments assg,
6060                     mrp_sourcing_rules rule,
6061                     po_vendor_sites_all vend,
6062 		    bom_cto_order_lines_gt bcol
6063    where
6064                     assg.assignment_set_id   = g_def_assg_set
6065 	      and   bcol.ato_line_id         = p_ato_line_id
6066 	      and   bcol.config_creation     = 3
6067               and   (nvl(bcol.perform_match,'N') = 'N' or nvl(bcol.reuse_config,'N') = 'N')
6068 	      and   assg.inventory_item_id   = bcol.inventory_item_id
6069               and   assg.sourcing_rule_id    = rcv.sourcing_rule_id
6070               and   assg.sourcing_rule_id    = rule.sourcing_rule_id
6071               and   rule.planning_active     = 1
6072               and   rcv.effective_date      <= sysdate
6073               and   nvl(rcv.disable_date,sysdate+1)>sysdate
6074               and   rcv.SR_RECEIPT_ID        = src.sr_receipt_id
6075               and   src.vendor_site_id = vend.vendor_site_id(+);
6076 
6077    -- Bugfix 8894392
6078    lCnt := sql%rowcount;
6079    IF PG_DEBUG <> 0 THEN
6080      oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA: No. of rows inserted for CIB 3 model:'||lCnt,5);
6081    END IF;
6082    -- Bugfix 8894392
6083 
6084 
6085   -- Bug Fix 4093235
6086   -- When we load the sourcing data into the temp table
6087   -- from matched cib 3 config items, we need to flag all the
6088   -- legs in the sourcing as valid. The valid_flag column in added
6089   -- and passed 'Y' value for all rows.
6090 
6091   -- Commenting this sql as part of Bugfix 8894392.
6092   -- Reasoning: For CIB = 3 and Match/Reuse flag = Y, code will be bypassed from the
6093   -- validation in process_oss_configurations part for ACC. So this sql is not used in case
6094   -- the mode is ACC. For UPG, after the code changes, reuse_flag will always be N,
6095   -- so data will be populated only from the earlier sql.
6096   -- Secondly, this sql picked up data from config. Since we are pruning the tree again,
6097   -- we need the data from model and not config.
6098   /*
6099 
6100   l_stmt_num := 70;
6101   Insert into bom_cto_oss_source_gt
6102                    (
6103                     Inventory_item_id,
6104                     Line_id,
6105 		    ato_line_id,
6106                     config_item_id,
6107                     Rcv_org_id,
6108                     Source_org_id,
6109                     Customer_id,
6110                     Ship_to_site_id,
6111                     Vendor_id,
6112                     Vendor_site_code,
6113                     rank,
6114                     Allocation,
6115                     Source_type,
6116                     source_rule_id,
6117                     sr_receipt_id,
6118                     sr_source_id,
6119                     assignment_id,
6120 		    Valid_flag     /* 4093235 */
6121                    --)
6122 
6123    --select /*+ INDEX (bcol BOM_CTO_ORDER_LINES_GT_N1) */
6124      /*             bcol.inventory_item_id,
6125                     bcol.line_id,
6126 		    p_ato_line_id,
6127                     bcol.config_item_id,
6128                     nvl(rcv.receipt_organization_id,assg.organization_id),
6129                     src.source_organization_id,
6130                     assg.customer_id,
6131                     assg.ship_to_site_id,
6132                     src.VENDOR_ID,
6133                     vend.VENDOR_SITE_code,
6134                     src.RANK,
6135                     src.ALLOCATION_PERCENT,
6136                     src.SOURCE_TYPE,
6137                     assg.sourcing_rule_id,
6138                     rcv.sr_receipt_id,
6139                     src.sr_source_id,
6140                     assg.assignment_id,
6141 		    'Y'
6142 
6143    from
6144                     mrp_sr_receipt_org rcv,
6145                     mrp_sr_source_org src,
6146                     mrp_sr_assignments assg,
6147                     mrp_sourcing_rules rule,
6148                     po_vendor_sites_all vend,
6149 		    bom_cto_order_lines_gt bcol
6150    where
6151                     assg.assignment_set_id   = g_def_assg_set
6152 	      and   bcol.ato_line_id         = p_ato_line_id
6153 	      and   bcol.config_creation     = 3
6154               and   (nvl(bcol.perform_match,'N') = 'Y' or nvl(bcol.reuse_config,'N') = 'Y')
6155 	      and   assg.inventory_item_id   = bcol.config_item_id
6156               and   assg.sourcing_rule_id    = rcv.sourcing_rule_id
6157               and   assg.sourcing_rule_id    = rule.sourcing_rule_id
6158               and   rule.planning_active     = 1
6159               and   rcv.effective_date      <= sysdate
6160               and   nvl(rcv.disable_date,sysdate+1)>sysdate
6161               and   rcv.SR_RECEIPT_ID        = src.sr_receipt_id
6162               and   src.vendor_site_id = vend.vendor_site_id(+);*/
6163 
6164    g_pg_level := g_pg_level - 3;
6165 
6166 Exception
6167         WHEN FND_API.G_EXC_ERROR THEN
6168             IF PG_DEBUG <> 0 THEN
6169                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA::exp error::'
6170 			      ||to_char(l_stmt_num)
6171 			      ||'::'||sqlerrm,1);
6172             END IF;
6173 	    g_pg_level := g_pg_level - 3;
6174             x_return_status := FND_API.G_RET_STS_ERROR;
6175             cto_msg_pub.count_and_get(
6176                                  p_msg_count  => x_msg_count,
6177                                  p_msg_data   => x_msg_data
6178                                 );
6179         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6180             IF PG_DEBUG <> 0 THEN
6181                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA::exp error::'
6182 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
6183             END IF;
6184 	    g_pg_level := g_pg_level - 3;
6185             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6186             cto_msg_pub.count_and_get(
6187                                  p_msg_count  => x_msg_count,
6188                                  p_msg_data   => x_msg_data
6189                                 );
6190         WHEN OTHERS THEN
6191             IF PG_DEBUG <> 0 THEN
6192                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA::exp error::'
6193 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
6194             END IF;
6195 	    g_pg_level := g_pg_level - 3;
6196             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6197             cto_msg_pub.count_and_get(
6198                                  p_msg_count  => x_msg_count,
6199                                  p_msg_data   => x_msg_data
6200                                 );
6201 
6202 End Get_order_sourcing_data;
6203 
6204 /*
6205      The following procedure will travell the whole
6206      sourcing tree for a given org and item
6207 */
6208 
6209 Procedure Traverse_sourcing_chain(
6210                                 p_item_id         IN    Number,
6211 				p_org_id          IN    Number,
6212                                 p_line_id         IN    Number,
6213                                 p_option_specific IN    Varchar,
6214                                 p_ato_line_id     IN    Number,
6215 				x_assg_list       IN OUT NOCOPY assg_rec,
6216 				x_return_status   OUT NOCOPY    Varchar2,
6217 				x_msg_data        OUT NOCOPY   Varchar2,
6218 				x_msg_count       OUT NOCOPY   Varchar2) is
6219 
6220    --Fixed FP bug 5156690
6221    -- added another filter condition assignment_id is not null
6222    -- to ignore rules that are not defined as explicit sourcing rules
6223    cursor src_cur  is
6224      select
6225            source_organization_id,
6226            organization_id,
6227            sourcing_rule_id,
6228            nvl(source_type,1) source_type,
6229 	   assignment_type,
6230 	   assignment_id
6231      from  mrp_sources_v msv
6232      where msv.assignment_set_id = g_def_assg_set
6233        and msv.inventory_item_id = p_item_id
6234        and msv.organization_id = p_org_id
6235        and nvl(effective_date,sysdate) <= nvl(disable_date, sysdate)
6236        and nvl(disable_date, sysdate+1) > sysdate
6237        and assignment_id is not null;
6238     l_assg_id Number :=0;
6239     l_stmt_num Number;
6240 Begin
6241    l_stmt_num := 10;
6242    g_pg_level := g_pg_level + 3;
6243    x_return_status := FND_API.G_RET_STS_SUCCESS;
6244 
6245    IF PG_DEBUG <> 0 Then
6246       oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Inside Traverse_Souricng_Chain API',5);
6247    End if;
6248 
6249    --
6250    -- Added By Renga Kannan on 11/21/03
6251    -- implemented an algorithm to catch circular sourcing.
6252    --
6253 
6254    If G_source_org_stk.exists(p_org_id) then
6255      if PG_DEBUG <> 0 then
6256        oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Circular sourcing deducted..',5);
6257      end if;
6258      cto_msg_pub.cto_message('BOM','CTO_INVALID_SOURCING');
6259      raise FND_API.G_EXC_ERROR;
6260    Else
6261      -- Push the org to the stack
6262      G_source_org_stk(p_org_id) := p_org_id;
6263    End if;
6264 
6265    --
6266    -- End of adition on 11/21/03
6267    --
6268 
6269 
6270    l_stmt_num := 20;
6271    For src_rec in src_cur
6272    Loop
6273       If PG_DEBUG <> 0 Then
6274          oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Assignment Type = '||src_rec.assignment_type,1);
6275          oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Assignment id   = '||src_rec.assignment_id,1);
6276          oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Source org id   = '||src_rec.source_organization_id,1);
6277 	 oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Rcv org id      = '||src_rec.organization_id,1);
6278       End if;
6279       l_stmt_num := 30;
6280       If src_rec.assignment_type in (3,6) Then
6281          If not g_assg_list.exists(src_rec.assignment_id) and p_option_specific is not null Then
6282 
6283             oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Registering the assignment id ',1);
6284             l_stmt_num := 40;
6285 	    x_assg_list.assignment_id.extend(x_assg_list.assignment_id.count+1);
6286 	    x_assg_list.assignment_id(x_assg_list.assignment_id.last) := src_rec.assignment_id;
6287             x_assg_list.line_id.extend(x_assg_list.line_id.count+1);
6288             x_assg_list.line_id(x_assg_list.line_id.last) := p_line_id;
6289 	    g_assg_list(src_rec.assignment_id) := src_rec.assignment_id;
6290 	    oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Line id = '||x_assg_list.line_id(x_assg_list.line_id.last),1);
6291 	    oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: Assg id = '||x_assg_list.assignment_id(x_assg_list.assignment_id.last),1);
6292          End if;
6293          l_assg_id := src_rec.assignment_id;
6294          If src_rec.source_type in (2,3) Then
6295              l_stmt_num := 50;
6296 	     oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: End org = '||src_rec.organization_id,1);
6297              insert into bom_cto_oss_orgslist_gt(
6298                                                  line_id,
6299                                                  organization_id,
6300                                                  ato_line_id
6301                           			)
6302              values                             (
6303   					         p_line_id,
6304                                                  src_rec.organization_id,
6305                                                  p_ato_line_id
6306                                                 );
6307 
6308          Else
6309              If PG_DEBUG <> 0 Then
6310                 oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOOURCING_CHAIN: Before calling traverse sourcing chain recurrsive',5);
6311              End if;
6312              l_stmt_num := 60;
6313              Traverse_sourcing_chain(
6314                                 p_item_id         => p_item_id,
6315                                 p_org_id          => src_rec.source_organization_id,
6316                                 p_line_id         => p_line_id,
6317                                 p_option_specific => p_option_specific,
6318                                 p_ato_line_id     => p_ato_line_id,
6319                                 x_assg_list       => x_assg_list,
6320                                 x_return_status   => x_return_status,
6321                                 x_msg_data        => x_msg_data,
6322                                 x_msg_count       => x_msg_count);
6323 
6324 	 End if; /* l_assg_id <> src_rec.assignment_id */
6325 
6326       End if; /* src_rec.assignment_type in (3,6) */
6327    End Loop;
6328    If l_assg_id =0 Then
6329       If PG_DEBUG <> 0 Then
6330          oe_debug_pub.add(lpad(' ',g_pg_level)||'TRAVERSE_SOURCING_CHAIN: End of source chain',5);
6331       End if;
6332 
6333       insert into bom_cto_oss_orgslist_gt(
6334    				          line_id,
6335 				          organization_id,
6336                                           ato_line_id
6337 					 )
6338       values                             (
6339 					  p_line_id,
6340 					  p_org_id,
6341                                           p_ato_line_id
6342  					);
6343 
6344 
6345    end if;
6346 
6347    g_source_org_stk.delete(p_org_id);
6348 
6349    g_pg_level := g_pg_level - 3;
6350 End Traverse_sourcing_chain;
6351 
6352 
6353 Procedure query_oss_sourcing_org(
6354 			     p_line_id              IN  NUMBER,
6355 			     p_inventory_item_id    IN  NUMBER,
6356 			     p_organization_id      IN  NUMBER,
6357 			     x_sourcing_rule_exists OUT NOCOPY varchar2,
6358 			     x_source_type          OUT NOCOPY NUMBER,
6359 			     x_t_sourcing_info      OUT NOCOPY CTO_MSUTIL_PUB.SOURCING_INFO,
6360 			     x_exp_error_code       OUT NOCOPY NUMBER,
6361 			     x_return_status        OUT NOCOPY varchar2,
6362 			     x_msg_data	            OUT NOCOPY Varchar2,
6363 			     x_msg_count            OUT NOCOPY Number) is
6364 l_stmt_num            Number;
6365 i		      Number;
6366 l_buy_type            Varchar2(1) := 'N';
6367 
6368 Cursor  source_org_rule_cur is
6369         select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
6370 	       source_org_id,
6371 	       source_type
6372 	from   bom_cto_oss_source_gt oss_src
6373 	where  line_id = p_line_id
6374 	and    valid_flag = 'Y'
6375 	and    rcv_org_id = p_organization_id;
6376 
6377 Cursor  source_item_rule_cur is
6378         select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
6379 	       source_org_id,
6380 	       source_type
6381 	from   bom_cto_oss_source_gt oss_src
6382 	where  line_id = p_line_id
6383 	and    valid_flag = 'Y'
6384 	and    rcv_org_id is null;
6385 
6386 Begin
6387 
6388    l_stmt_num := 10;
6389    g_pg_level := g_pg_level + 3;
6390    x_return_status := FND_API.G_RET_STS_SUCCESS;
6391    i := 1;
6392 
6393    If PG_DEBUG <> 0 Then
6394       oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: Inside Query OSS Sourcing Org API',5);
6395       oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: p_line_id           = '||p_line_id,5);
6396       oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: p_org_id            = '||p_organization_id,5);
6397       oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: p_inventory_item_id = '||p_inventory_item_id,5);
6398    End if;
6399 
6400    For Source_org_rule_rec in Source_org_rule_cur
6401    Loop
6402       If Source_org_rule_rec.source_type = 3 and l_buy_type = 'N' Then
6403          x_t_sourcing_info.source_organization_id(i) := Source_org_rule_rec.source_org_id;
6404          x_t_sourcing_info.source_type(i)            := Source_org_rule_rec.source_type;
6405 
6406 	 If PG_DEBUG <> 0 Then
6407             oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: sourcing org  = '
6408 	                                         ||x_t_sourcing_info.source_organization_id(i),5);
6409             oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: sourcing Type = '
6410 	                                         ||x_t_sourcing_info.source_type(i),5);
6411 
6412 	 End if;
6413 	 i := i +1 ;
6414 	 l_buy_type := 'Y';
6415 
6416       elsif source_org_rule_rec.source_type in (1,2) then
6417          x_t_sourcing_info.source_organization_id(i) := Source_org_rule_rec.source_org_id;
6418          x_t_sourcing_info.source_type(i)            := Source_org_rule_rec.source_type;
6419 	 If PG_DEBUG <> 0 Then
6420             oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: sourcing org  = '
6421 	                                         ||x_t_sourcing_info.source_organization_id(i),5);
6422             oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: sourcing Type = '
6423 	                                         ||x_t_sourcing_info.source_type(i),5);
6424 
6425 	 End if;
6426 
6427 	 i := i + 1;
6428       End if;
6429    End Loop;
6430 
6431    If PG_DEBUG <> 0 Then
6432       oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: Number of orgs based on orgs rule ='
6433                                            ||x_t_sourcing_info.source_organization_id.count,5);
6434    End if;
6435 
6436    If x_t_sourcing_info.source_organization_id.count = 0 Then
6437 
6438       For Source_item_rule_rec in Source_item_rule_cur
6439       Loop
6440          If Source_item_rule_rec.source_type = 3 and l_buy_type = 'N' Then
6441             x_t_sourcing_info.source_organization_id(i) := Source_item_rule_rec.source_org_id;
6442             x_t_sourcing_info.source_type(i) := Source_item_rule_rec.source_type;
6443 	    If PG_DEBUG <> 0 Then
6444                oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: sourcing org  = '
6445 	                                            ||x_t_sourcing_info.source_organization_id(i),5);
6446                oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: sourcing Type = '
6447 	                                            ||x_t_sourcing_info.source_type(i),5);
6448 
6449    	    End if;
6450 
6451 	    i := i + 1;
6452 	    l_buy_type := 'Y';
6453          elsif Source_item_rule_rec.source_type in (1,2) then
6454             x_t_sourcing_info.source_organization_id(i) := Source_item_rule_rec.source_org_id;
6455             x_t_sourcing_info.source_type(i) := Source_item_rule_rec.source_type;
6456 	    If PG_DEBUG <> 0 Then
6457                oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: sourcing org  = '
6458 	                                            ||x_t_sourcing_info.source_organization_id(i),5);
6459                oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: sourcing Type = '
6460 	                                            ||x_t_sourcing_info.source_type(i),5);
6461 
6462 	    End if;
6463 
6464 	    i := i + 1;
6465          End if;
6466       End Loop;
6467 
6468       If PG_DEBUG <> 0 Then
6469          oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: Number of orgs based on item rule ='
6470                                            ||x_t_sourcing_info.source_organization_id.count,5);
6471       End if;
6472 
6473 
6474    End if;
6475 
6476    If x_t_sourcing_info.source_organization_id.count = 0 Then
6477       x_sourcing_rule_exists := 'N';
6478       Select planning_make_buy_code
6479       into   x_source_type
6480       from   mtl_system_items
6481       where  inventory_item_id = p_inventory_item_id
6482       and    organization_id   = p_organization_id;
6483       If PG_DEBUG <> 0 Then
6484          oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: No sourcing rule exists',5);
6485       End if;
6486    else
6487       If PG_DEBUG <> 0 Then
6488          oe_debug_pub.add(lpad(' ',g_pg_level)||'QUERY_OSS_SOURCING_ORG: Number of sourcin orgs = '
6489 	                                      ||x_t_sourcing_info.source_organization_id.count,5);
6490       end if;
6491       x_sourcing_rule_exists := 'Y';
6492    End if;
6493 
6494    g_pg_level := g_pg_level - 3;
6495 Exception
6496         WHEN FND_API.G_EXC_ERROR THEN
6497             IF PG_DEBUG <> 0 THEN
6498                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA::exp error::'
6499 			      ||to_char(l_stmt_num)
6500 			      ||'::'||sqlerrm,1);
6501             END IF;
6502 	    g_pg_level := g_pg_level - 3;
6503             x_return_status := FND_API.G_RET_STS_ERROR;
6504             cto_msg_pub.count_and_get(
6505                                  p_msg_count  => x_msg_count,
6506                                  p_msg_data   => x_msg_data
6507                                 );
6508         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6509             IF PG_DEBUG <> 0 THEN
6510                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA::exp error::'
6511 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
6512             END IF;
6513 	    g_pg_level := g_pg_level - 3;
6514             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6515             cto_msg_pub.count_and_get(
6516                                  p_msg_count  => x_msg_count,
6517                                  p_msg_data   => x_msg_data
6518                                 );
6519         WHEN OTHERS THEN
6520             IF PG_DEBUG <> 0 THEN
6521                 oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_ORDER_SOURCING_DATA::exp error::'
6522 			      ||to_char(l_stmt_num)||'::'||sqlerrm,1);
6523             END IF;
6524 	    g_pg_level := g_pg_level - 3;
6525             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6526             cto_msg_pub.count_and_get(
6527                                  p_msg_count  => x_msg_count,
6528                                  p_msg_data   => x_msg_data
6529                                 );
6530 End query_oss_sourcing_org;
6531 
6532 
6533 Procedure GET_OSS_BOM_ORGS(
6534                            p_line_id       IN  Number,
6535 			   x_orgs_list     OUT NOCOPY CTO_OSS_SOURCE_PK.orgs_list,
6536 			   x_return_status OUT NOCOPY Varchar2,
6537 			   x_msg_data      OUT NOCOPY Varchar2,
6538 			   x_msg_count     OUT NOCOPY Number) is
6539 l_stmt_num     Number;
6540 l_count        Number;
6541 l_source_org   number;
6542 l_rcv_org      number;
6543 l_valid_flag   varchar2(1);
6544 l_source_type  varchar2(1);
6545 l_line_id      number;
6546 
6547 Begin
6548    g_pg_level := nvl(g_pg_level,0) + 3;
6549    x_return_status := FND_API.G_RET_STS_SUCCESS;
6550    l_stmt_num := 10;
6551 
6552    oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_BOM_ORGS: Begin ',1);
6553 
6554    Select org_id
6555    bulk collect into x_orgs_list
6556    from
6557       (Select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
6558               distinct nvl(source_org_id,rcv_org_id)  org_id
6559        from   bom_cto_oss_source_gt oss_src
6560        where  line_id = p_line_id
6561        and    valid_flag in( 'P','Y')
6562        and    source_type in (2,3)
6563        union
6564        select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
6565               distinct source_org_id org_id
6566        from   bom_cto_oss_source_gt oss_src
6567        where  line_id = p_line_id
6568        and    valid_flag in ('P','Y')
6569        and    source_org_id not in (
6570 				select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
6571 				       rcv_org_id
6572 				from   bom_cto_oss_source_gt oss_src
6573 				where  line_id = p_line_id
6574 				and    valid_flag in( 'P','Y')));
6575    If PG_DEBUG <> 0 Then
6576       oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_BOM_ORGS: Number of orgs where bom should be created = '
6577                                            ||x_orgs_list.count,5);
6578    End if;
6579 
6580    oe_debug_pub.add(lpad(' ',g_pg_level)||'GET_OSS_BOM_ORGS: End ',1);
6581 
6582 End GET_OSS_BOM_ORGS;
6583 
6584 
6585 -- Bugfix 13362916
6586 Procedure Print_source_gt is
6587    cursor source_cur is
6588       select /*+ INDEX (oss_src BOM_CTO_OSS_SOURCE_GT_N2) */
6589              line_id,
6590              inventory_item_id,
6591 	     rcv_org_id,
6592 	     source_org_id,
6593 	     customer_id,
6594 	     vendor_id,
6595 	     vendor_site_code,
6596 	     rank,
6597 	     allocation,
6598 	     source_type,
6599 	     reuse_flag,
6600 	     valid_flag,
6601 	     leaf_node,
6602              SR_RECEIPT_ID -- bug 13362916
6603       from   bom_cto_oss_source_gt oss_src;
6604       --where  line_id = p_line_id;
6605 
6606 Begin
6607    oe_debug_pub.add('================PRINTING BOM_CTO_OSS_SOURCE_GT==================',5);
6608    --oe_debug_pub.add('================   Line id = '||p_line_id||'======================',5);
6609    oe_debug_pub.add('Line_id --- Item id --- Rcv org --- src org --- customer --- vendor --- vend site --- rank --- alloc% --- src type --- reuse -- valid --- leaf --- sr_receipt_id --- ',5);
6610    for source_rec in source_cur
6611    loop
6612       oe_debug_pub.add(source_rec.line_id|| ' --- '
6613                        ||source_rec.inventory_item_id||' --- '
6614                        ||source_rec.rcv_org_id||' --- '||source_rec.source_org_id
6615 		       ||' --- '||source_rec.customer_id||' --- '||source_rec.vendor_id
6616 		       ||' --- '||source_rec.vendor_site_code||' --- '||source_rec.rank
6617 		       ||' --- '||source_rec.allocation||' --- '||source_rec.source_type
6618 		       ||' --- '||source_rec.reuse_flag||' --- '||source_rec.valid_flag
6619 		       ||' --- '||source_rec.leaf_node ||' --- '||source_rec.SR_RECEIPT_ID,5);
6620    End Loop;
6621 
6622    oe_debug_pub.add('============== End printing ===============',5);
6623 End;
6624 
6625 --Bugfix 13362916
6626 Procedure Print_orglist_gt is
6627 Cursor  org_list_cur is
6628        select line_id,
6629               ato_line_id,
6630 	      inventory_item_id,
6631 	      organization_id,
6632 	      vendor_id,
6633 	      vendor_site_code,
6634 	      make_flag
6635 	from  bom_cto_oss_orgslist_gt;
6636 	--where line_id = p_line_id;
6637 begin
6638    oe_debug_pub.add('================PRINTING BOM_CTO_ORGSLIST_GT==================',5);
6639    --oe_debug_pub.add('================   Line id = '||p_line_id||'======================',5);
6640    oe_debug_pub.add('Line id --- Ato Line Id --- Item Id --- Org id --- vendor --- vend site ---Make Flag',5);
6641    for org_list_rec in org_list_cur
6642    Loop
6643       oe_debug_pub.add(org_list_rec.line_id||' --- '||org_list_rec.ato_line_id||' --- '||
6644                        org_list_rec.inventory_item_id||' --- '||org_list_rec.organization_id||' --- '||
6645 		       org_list_rec.vendor_id||' --- '||org_list_rec.vendor_site_code||' --- '||
6646 		       org_list_rec.make_flag,5);
6647    End Loop;
6648    oe_debug_pub.add('============== End printing ===============',5);
6649 End;
6650 END CTO_OSS_SOURCE_PK;