DBA Data[Home] [Help]

PACKAGE BODY: APPS.GMP_BOM_ROUTING_PKG

Source


1 PACKAGE BODY GMP_BOM_ROUTING_PKG AS
2 /* $Header: GMPBMRTB.pls 120.67.12020000.13 2013/05/15 10:19:35 vkinduri ship $ */
3 
4 /*
5 REM+==========================================================================+
6 REM+                                                                          +
7 REM+                              ROADMAP                                     +
8 REM+ In R12.0 No OPM warehouse exists as part of convergence and hence        +
9 REM+ msc_st_sub_inventories will be populated by MSCCLAAB.pls package         +
10 REM+                                                                          +
11 REM+ The transfer of formula and routing data from OPM to APS is achieved in  +
12 REM+ two phases, by calling the 'extract_effectivities' procedure. The first  +
13 REM+ phase is to create and populate a set of PL/SQL tables and collections   +
14 REM+ with data from the OPM formulation tables. The second phase takes these  +
15 REM+ data and populates the APS tables. These two phases are carried out by   +
16 REM+ the 'retrieve effectivities' and 'export_effectivities' procedures. As   +
17 REM+ can be inferred from the procedures' names, the starting point is the    +
18 REM+ OPM effectivties.                                                        +
19 REM+                                                                          +
20 REM+ Because a formula (and for that matter a routing) can be used many times +
21 REM+ in several effectivities, the retrieval phase attempts to minimise the   +
22 REM+ memory requirements by only storing a formula (or routing) in the PL/SQL +
23 REM+ structures once, and setting up links to them. At the end of the first   +
24 REM+ phase the following structures will be in place:                         +
25 REM+                                                                          +
26 REM+ APS_EFFECTIVITIES: Containing organisational and warehouse data and a    +
27 REM+                    link to to the OPM effectivity. (Each OPM effectivity +
28 REM+                    can be exploded into several APS effectivties if the  +
29 REM+                    organisation or routing (or both) is/are null         +
30 REM+                                                                          +
31 REM+ OPM_EFFECTIVITIES: Containing effectivity data (dates, preferences etc)  +
32 REM+                    with links to: formula_headers, routing_headers (if a +
33 REM+                    routing was specified in this effectivity), formula/  +
34 REM+                    routing/materials (ditto). Also contains the index of +
35 REM+                    the effective item in the formula_details structure.  +
36 REM+                                                                          +
37 REM+ FORMULA_HEADERS:   Contains formula_no/version/description and links to  +
38 REM+                    the formula's details in the formula_details structure+
39 REM+                                                                          +
40 REM+ ROUTING_HEADERS:   Contains routing_no/version/description and links to  +
41 REM+                    the routing's details in the routing_details structure+
42 REM+                                                                          +
43 REM+                                                                          +
44 REM+ FORMULA_DETAILS:   Contains the formula's details.                       +
45 REM+                                                                          +
46 REM+ ROUTING_DETAILS:   Contains the routing's details.                       +
47 REM+                                                                          +
48 REM+ MATERIAL_ASSOCS:   Contains the routing/material associations.           +
49 REM+                                                                          +
50 REM+ Several other auxiliary structures are used as stepping stones to build  +
51 REM+ and populate the above structures, the names of each should assist in    +
52 REM+ determining their use. All structures are instantiated when the extract- +
53 REM+ ion process starts, and destroyed at completion.                         +
54 REM+                                                                          +
55 REM+ If the retrieval phase was successful, the export phase starts. This     +
56 REM+ works by traversing the above structures in a manner similar to the way  +
57 REM+ the retrieval phase gathered the data from the database: it starts with  +
58 REM+ the APS effectivities and follows the pointers to the other structures   +
59 REM+ to locate the data to be passed to APS. Several tables are written to.   +
60 REM+                                                                          +
61 REM+ Broadly, formulation data is written to the _BOM and _BOM_COMPONENTS     +
62 REM+ tables and APS effectivity data is writen to the _PROCESS_EFFECTIVITY    +
63 REM+ and GMP_FORM_EFF tables. The writing of routing data is somewhat more    +
64 REM+ complex. Basic routing header and detail data is written to the _ROUTINGS+
65 REM+ AND The _ROUTING_OPERATIONS tables. Each detail row (ie each step) then  +
66 REM+ has its detail (ie the individual activities) written to the _OPERATION_ +
67 REM+ RESOURCE_SEQS table. Each activity is given a sequence number within the +
68 REM+ step in which it occurs. Each activity's detail (ie each resource used   +
69 REM+ in the activity in the step (are you still with me?) is then written to  +
70 REM+ the _OPERATION_RESOURCES table. The final complication is that alternate +
71 REM+ resources are also written to the _OPERATION_RESOURCES table, one for    +
72 REM+ each alternative. Each row written has the original resource and a diff- +
73 REM+ erent alternative. If there are no alternatives, only one row is written +
74 REM+ and the alternative is not specified. Got all that?                      +
75 REM+                                                                          +
76 REM+ For further processing details, see the individual procedure headers.    +
77 REM+                                                                          +
78 REM+ All externally callable routines share the same interface:               +
79 REM+                                                                          +
80 REM+ LINK_NAME       IN    VARCHAR2                                           +
81 REM+ RETURN_STATUS   OUT   BOOLEAN                                            +
82 REM+                                                                          +
83 REM+ Each procedure returns a status of TRUE if everything worked OK or FALSE +
84 REM+ if there was a problem.                                                  +
85 REM+                                                                          +
86 REM+                                                                          +
87 REM+ P.J.Schofield OPM Development, 13th August 1999.                         +
88 REM+                                                                          +
89 REM+==========================================================================+
90 
91 REM+==========================================================================+
92 REM| PACKAGE DATA BLOCK                                                       |
93 REM|                                                                          |
94 REM| Originally these were all defined with the %TYPE of the database column  |
95 REM| they represented, but as the APS server will not contain any OPM tables  |
96 REM| these have, sadly, to be replaced with scalar datatypes.                 |
97 REM+==========================================================================+
98 
99 REM Create composite types
100 REM ======================
101 
102 REM This type is used to create a holding area for data retreived by one of the
103 REM four cursors in the retrieve_effectivities procedure. Most of the values
104 REM are filled in by the cursors. The remainder are filled in by the procedures
105 REM which retrieve and validate formulae and routings.
106 */
107 
108   TYPE ref_cursor_typ IS REF CURSOR;
109   null_value                 VARCHAR2(2)     := NULL;
110   routing_dtl_cursor         VARCHAR2(32767) := NULL;
111   validation_statement       VARCHAR2(32767) := NULL;
112   invalid_string_value       EXCEPTION;
113   invalid_gmp_uom_profile    EXCEPTION;
114   source_call_failure        EXCEPTION; /*  B8230710 */
115 
116 TYPE gmp_buffer_typ IS RECORD
117 (
118   fmeff_id            PLS_INTEGER,  /* OPM Effectivity ID         */
119   aps_fmeff_id        PLS_INTEGER,  /* APS Effectivity ID - B2989806  */
120   inventory_item_id   PLS_INTEGER,      /* OPM Effectivity Item ID    */
121   formula_id          PLS_INTEGER,  /* Formula ID                 */
122   organization_id     PLS_INTEGER,  /* ID for the Plant           */
123   start_date          DATE,        /* Effectivity Start Date     */
124   end_date            DATE,        /* Effectivity End Date       */
125   inv_min_qty         NUMBER,      /* Effectivity Minimum Qty    */
126   inv_max_qty         NUMBER,      /* Effectivity Maximum Qty    */
127   preference          PLS_INTEGER,      /* Effectivity Preference     B3437281 */
128   primary_uom_code    VARCHAR2(3), /* Primary UOM of the Item    */
129   organization_code   VARCHAR2(3), /* Resource or Material Whse  */
130   routing_id          PLS_INTEGER,  /* Routing ID. Could be NULL  */
131   routing_no          VARCHAR2(32),/* Associated Routing No      */
132   routing_vers        PLS_INTEGER,   /* Associated Routing Version */
133   routing_desc        VARCHAR2(240),/* Associated Routing DEsc'n  */ --bug14480509 change to 240.
134   routing_uom         VARCHAR2(3), /* UOM from the Routing       */ -- akaruppa previously routing_um
135   routing_qty         NUMBER,      /* Qty from the Routing       */
136   prod_factor         NUMBER, /*B2870041 factor to convert prod to rout um */
137   product_index       PLS_INTEGER, /*B2870041 index of the product line */
138   recipe_id           PLS_INTEGER,  /* 1830940 New GMD Changes Recipe ID */
139   recipe_no           VARCHAR2(32), /* B5584507 */
140   recipe_version      PLS_INTEGER,    /* B5584507 */
141   rtg_hdr_location    PLS_INTEGER,   /* index link to routing header */
142   calculate_step_quantity NUMBER,
143   category_id         PLS_INTEGER,
144   setup_id            PLS_INTEGER,
145   seq_dpnd_class      VARCHAR2(100)
146 );
147 effectivity           gmp_buffer_typ; -- Record
148 -- Charter 14013503 vijay Induri 5/11/2012
149 TYPE gmp_buffer_tbl IS TABLE OF gmp_buffer_typ
150 INDEX BY BINARY_INTEGER;
151 gmp_effectivity_tab        gmp_buffer_tbl;  -- table of records
152 
153 TYPE gmp_formula_header_typ IS RECORD
154 (
155   formula_id          PLS_INTEGER,
156   valid_flag          PLS_INTEGER,
157   start_dtl_loc       PLS_INTEGER,
158   end_dtl_loc         PLS_INTEGER,
159   total_output        NUMBER, /* B2870041 total output for all prod/byp */
160   total_uom           VARCHAR2(3) /*B2870041 um used to calculate qty */ -- akaruppa changed total_um to total_uom
161 );
162 TYPE gmp_formula_header_tbl IS TABLE OF gmp_formula_header_typ
163 INDEX BY BINARY_INTEGER;
164 formula_header_tab        gmp_formula_header_tbl;
165 
166 TYPE gmp_formula_detail_typ IS RECORD
167 (
168   formula_id          PLS_INTEGER,
169   formula_no          VARCHAR2(32),
170   formula_vers        PLS_INTEGER,
171   formula_desc1       VARCHAR2(240),--bug14480509
172   x_formulaline_id    PLS_INTEGER,
173   line_type           PLS_INTEGER,
174   inventory_item_id   PLS_INTEGER,
175   formula_qty         NUMBER,
176   scrap_factor        NUMBER,
177   scale_type          PLS_INTEGER,
178   contribute_yield_ind VARCHAR2(1),      /* B2657068 Rajesh Patangya */
179   contribute_step_qty_ind PLS_INTEGER,      /* NAMIT_ASQC */
180   phantom_type        PLS_INTEGER,
181   primary_uom_code    VARCHAR2(3), -- akaruppa previously aps_um
182   detail_uom          VARCHAR2(3), /*B2870041 formula um */ -- akaruppa previously orig_um
183   bom_scale_type      PLS_INTEGER,
184   primary_qty         NUMBER,
185   scale_multiple      NUMBER,        /* B2657068 Rajesh Patangya */
186   scale_rounding_variance PLS_INTEGER,    /* B2657068 Rajesh Patangya */
187   rounding_direction  PLS_INTEGER,          /* B2657068 Rajesh Patangya */
188   release_type        PLS_INTEGER,
189   /*B5176291 - Item substitution changes - start*/
190 --  formula_line_id     NUMBER,
191   original_item_flag  PLS_INTEGER,
192   start_date          DATE,
193   end_date            DATE,
194   formula_line_id     PLS_INTEGER,
195   preference          PLS_INTEGER
196   /* Bug: 6087535 Vpedarla 23-07-07 FP :11.5.10 - 12.0.3 : ITEM SUBSTITUTION EFFECTIVITY IS NOT COLLECTED. */
197 --  lead_stdate         DATE,
198 --  lead_enddate        DATE,
199 --  lead_pref           NUMBER,
200 --  replace_uom         VARCHAR2(3),
201 --  actual_end_date     DATE,
202 --  actual_end_flag     NUMBER,
203   /*B5176291 - Item substitution changes - end*/
204 --  release_type        NUMBER
205 );
206 TYPE gmp_formula_detail_tbl IS TABLE OF gmp_formula_detail_typ
207 INDEX BY BINARY_INTEGER;
208 formula_detail_tab   gmp_formula_detail_tbl ;
209 
210 TYPE gmp_formula_detail_count_typ IS RECORD
211 (
212   formula_id          PLS_INTEGER,
213   formula_dtl_count   PLS_INTEGER
214 );
215 TYPE gmp_formula_detail_count_tbl IS TABLE OF gmp_formula_detail_count_typ
216 INDEX BY BINARY_INTEGER;
217 formula_dtl_count_rec     gmp_formula_detail_count_typ ;
218 
219 TYPE gmp_formula_orgn_count_typ IS RECORD
220 (
221   formula_id          PLS_INTEGER,
222   organization_id     PLS_INTEGER,
223   orgn_count          PLS_INTEGER,  /* Count of formula details */
224   valid_flag          PLS_INTEGER
225 );
226 TYPE gmp_formula_orgn_count_tbl IS TABLE OF gmp_formula_orgn_count_typ
227 INDEX BY BINARY_INTEGER;
228 formula_orgn_count_tab  gmp_formula_orgn_count_tbl;
229 
230 /*B5176291 - Item substitution changes - start*/
231 /* Bug: 6087535 Vpedarla 23-07-07 FP :11.5.10 - 12.0.3 : ITEM SUBSTITUTION EFFECTIVITY IS NOT COLLECTED. */
232 prev_detail_tab                    gmp_formula_detail_tbl ;
233 orig_detail_tab                     gmp_formula_detail_tbl ;
234 temp_detail_tab                    gmp_formula_detail_tbl ;
235 subst_tab                          gmp_formula_detail_tbl ;
236 /*B5176291 - Item substitution changes - end*/
237 
238 TYPE gmp_routing_header_typ IS RECORD
239 (
240   routing_id          PLS_INTEGER,
241   organization_id     PLS_INTEGER,
242   valid_flag          PLS_INTEGER,
243   generic_start_loc   PLS_INTEGER,
244   generic_end_loc     PLS_INTEGER,
245   orgn_start_loc      PLS_INTEGER,
246   orgn_end_loc        PLS_INTEGER,
247   step_start_loc      PLS_INTEGER,
248   step_end_loc        PLS_INTEGER,
249   usage_start_loc     PLS_INTEGER,
250   usage_end_loc       PLS_INTEGER,
251   stpdep_start_loc    PLS_INTEGER,
252   stpdep_end_loc      PLS_INTEGER
253 );
254 TYPE gmp_routing_header_tbl IS TABLE OF gmp_routing_header_typ
255 INDEX BY BINARY_INTEGER;
256 rtg_org_hdr_tab      gmp_routing_header_tbl;
257 
258 TYPE gmp_routing_detail_typ IS RECORD
259 (
260   routing_id          PLS_INTEGER,
261   organization_id     PLS_INTEGER,
262 /* NAMIT_RD */
263   routingstep_no      PLS_INTEGER,
264   seq_dep_ind         PLS_INTEGER, /*B2870041 sequence dependent indicator */
265   prim_rsrc_ind_order PLS_INTEGER,
266   resources           VARCHAR2(16),
267 /* NAMIT_OC */
268   prim_rsrc_ind       PLS_INTEGER,
269   capacity_constraint PLS_INTEGER,
270   min_capacity        NUMBER,
271   max_capacity        NUMBER,
272   schedule_ind        PLS_INTEGER,
273   routingstep_id      PLS_INTEGER,
274   x_routingstep_id    PLS_INTEGER,
275   step_qty            NUMBER,
276   minimum_transfer_qty NUMBER,
277   oprn_desc           VARCHAR2(240),--bug14480509
278   oprn_id             PLS_INTEGER,   /* SGIDUGU - Seq Dep changes */
279   oprn_no             VARCHAR2(32),
280   process_qty_uom     VARCHAR2(3), -- akaruppa previously process_qty_um
281   activity            VARCHAR2(16),
282   oprn_line_id        PLS_INTEGER,
283   resource_count      PLS_INTEGER,
284   resource_usage      NUMBER,
285   resource_usage_uom  VARCHAR2(3), -- akaruppa previously usage_um
286   scale_type          PLS_INTEGER,
287   offset_interval     NUMBER,
288   resource_id         PLS_INTEGER,
289   x_resource_id       PLS_INTEGER,   /* B1177070 added encoded key */
290   rtg_scale_type      PLS_INTEGER,
291   activity_factor     NUMBER,       /* GMD New Additional Columns */
292   process_qty         NUMBER,       /* GMD New Additional Columns */
293   material_ind        PLS_INTEGER, /*B2870041 material indicator for next/prior*/
294   schedule_flag       PLS_INTEGER,  /*B2870041 default value for APS*/
295   mat_found           PLS_INTEGER,   /* Indicator is any activity is scheduled in operation. */
296   include_rtg_row     PLS_INTEGER,    /* Do Not Plan Resource rows will have value 0 */
297   break_ind           PLS_INTEGER,   /* Flag denoting whether activity is breakable or not. */
298   o_min_capacity      NUMBER,  /* Overrides */
299   o_max_capacity      NUMBER,  /* Overrides */
300   o_resource_usage    NUMBER,  /* Overrides */
301   o_activity_factor   NUMBER,  /* Overrides */
302   o_process_qty       NUMBER,   /* Overrides */
303   o_step_qty          NUMBER,   /* Overrides */
304 /* Rajesh Added */
305   is_sds_rout         PLS_INTEGER,   /* B4918786 SDS */
306   is_unique           PLS_INTEGER,   /* B4918786 SDS */
307   is_nonunique        PLS_INTEGER,   /* B4918786 SDS */
308   setup_id            PLS_INTEGER    /* B4918786 SDS */
309 );
310 TYPE gmp_routing_detail_tbl IS TABLE OF gmp_routing_detail_typ
311 INDEX BY BINARY_INTEGER;
312 rtg_org_dtl_tab    gmp_routing_detail_tbl;
313 
314 /* B4918786 SDS */
315 TYPE gmp_sds_typ IS RECORD
316 (
317   oprn_id             PLS_INTEGER,
318   category_id         PLS_INTEGER,
319   seq_dpnd_class      VARCHAR2(100),
320   resources           VARCHAR2(16),
321   resource_id         PLS_INTEGER,
322   setup_id            PLS_INTEGER
323 );
324 TYPE gmp_sds_tbl IS TABLE OF gmp_sds_typ INDEX BY BINARY_INTEGER;
325 sds_tab    gmp_sds_tbl;
326 sds_tab_init gmp_sds_tbl;
327 
328 TYPE gen_routing_detail_typ IS RECORD
329 (
330   routing_id          PLS_INTEGER,
331   routingstep_no      PLS_INTEGER,
332 /* NAMIT_RD */
333   seq_dep_ind         PLS_INTEGER, /*B2870041 sequence dependent indicator */
334   prim_rsrc_ind_order PLS_INTEGER,
335   resources           VARCHAR2(16),
336   routingstep_id      PLS_INTEGER,
337   oprn_no             VARCHAR2(32),
338   oprn_line_id        PLS_INTEGER,
339   activity            VARCHAR2(16),
340   prim_rsrc_ind       PLS_INTEGER,
341   offset_interval     NUMBER,
342   uom_code            VARCHAR2(3), /* NAMIT_RD */
343   capacity_constraint NUMBER -- akaruppa added to check if resource is chargeable
344 );
345 TYPE gen_routing_detail_tbl IS TABLE OF gen_routing_detail_typ
346 INDEX BY BINARY_INTEGER;
347 rtg_gen_dtl_tab       gen_routing_detail_tbl;
348 
349 TYPE gmp_alt_resource_typ IS RECORD
350 (
351   prim_resource_id    PLS_INTEGER,
352   alt_resource_id     PLS_INTEGER,
353   min_capacity        NUMBER,  /* SGIDUGU - min capacity for alternate rsrc */
354   max_capacity        NUMBER,  /* SGIDUGU - max capacity for alternate rsrc */
355   runtime_factor      NUMBER,  /* B2353759,alternate runtime_factor */
356   preference          PLS_INTEGER, /* B5688153 Prod spec alternates Rajesh Patangya */
357   inventory_item_id   PLS_INTEGER  /* B5688153 Prod spec alternates Rajesh Patangya */
358 );
359 TYPE gmp_alt_resource_tbl IS TABLE OF gmp_alt_resource_typ
360 INDEX BY BINARY_INTEGER;
361 rtg_alt_rsrc_tab       gmp_alt_resource_tbl;
362 
363 TYPE gmp_material_assoc_typ IS RECORD
364 (
365   formula_id          PLS_INTEGER,
366   recipe_id           PLS_INTEGER,
367   line_type           PLS_INTEGER,
368   line_no             PLS_INTEGER,
369   x_formulaline_id    PLS_INTEGER,   /* B1177070 added encoded key */
370   x_routingstep_id    PLS_INTEGER,  /* B1177070 added encoded key */
371 /* NAMIT_MTQ */
372   inventory_item_id   PLS_INTEGER,
373   routingstep_no      PLS_INTEGER,
374   uom_conv_factor     NUMBER,
375   min_trans_qty       NUMBER,
376   min_delay           NUMBER,
377   max_delay           NUMBER
378 );
379 TYPE gmp_material_assoc_tbl IS TABLE OF gmp_material_assoc_typ
380 INDEX BY BINARY_INTEGER;
381 mat_assoc_tab    gmp_material_assoc_tbl;
382 
383 /* NAMIT_CR Define Step Dependency Record Type */
384 
385 TYPE gmp_opr_stpdep_typ IS RECORD
386 (
387   routing_id          PLS_INTEGER,
388   x_dep_routingstep_id PLS_INTEGER,
389   x_routingstep_id    PLS_INTEGER,
390   dep_type            PLS_INTEGER,
391   standard_delay      NUMBER,
392   max_delay           NUMBER,
393   transfer_pct        NUMBER,
394   dep_routingstep_no  PLS_INTEGER,
395   routingstep_no      PLS_INTEGER,
396   chargeable_ind      PLS_INTEGER
397 );
398  TYPE gmp_opr_stepdep_tab IS TABLE OF gmp_opr_stpdep_typ
399  INDEX BY BINARY_INTEGER;
400 gmp_opr_stpdep_tbl gmp_opr_stepdep_tab;
401 
402 /* GMD New Declaration of PL/SQL Tables for Activity and Resources Overrides */
403 TYPE recipe_orgn_override_typ IS RECORD
404 (
405   routing_id          PLS_INTEGER,
406   organization_id     PLS_INTEGER,
407   routingstep_id      PLS_INTEGER,
408   oprn_line_id        PLS_INTEGER,
409   recipe_id           PLS_INTEGER,
410   activity_factor     NUMBER,
411   resources           VARCHAR2(16),
412   resource_usage      NUMBER,
413   process_qty         NUMBER,
414 /* NAMIT_OC */
415   min_capacity        NUMBER,
416   max_capacity        NUMBER
417 );
418 TYPE recipe_orgn_override_tbl IS TABLE OF recipe_orgn_override_typ
419 INDEX BY BINARY_INTEGER;
420 rcp_orgn_override    recipe_orgn_override_tbl;
421 
422 TYPE recipe_override_typ IS RECORD
423 (
424   routing_id          PLS_INTEGER,
425   routingstep_id      PLS_INTEGER,
426   recipe_id           PLS_INTEGER,
427   step_qty            NUMBER
428 );
429 TYPE recipe_override_tbl IS TABLE OF recipe_override_typ
430 INDEX BY BINARY_INTEGER;
431 recipe_override      recipe_override_tbl;
432 
433 TYPE gmp_routing_step_offsets_typ IS RECORD
434 (
435 organization_id	PLS_INTEGER,
436 fmeff_id 	PLS_INTEGER,
437 formula_id	PLS_INTEGER,
438 routingstep_id	PLS_INTEGER,
439 start_offset	NUMBER,
440 end_offset	NUMBER,
441 formulaline_id	PLS_INTEGER
442 );
443 TYPE rtgstep_offsets_tbl IS TABLE OF gmp_routing_step_offsets_typ
444 INDEX BY BINARY_INTEGER ;
445 rstep_offsets	rtgstep_offsets_tbl;
446 
447 --  Vpedarla 7391495
448 TYPE oper_leadtime_percent_typ IS RECORD
449 (
450 Organization_id NUMBER,
451 fmeff_id 	PLS_INTEGER,
452 formula_id	PLS_INTEGER,
453 routing_id  PLS_INTEGER,
454 routingstep_id	PLS_INTEGER,
455 start_offset	NUMBER,
456 end_offset	NUMBER
457 );
458 TYPE oper_leadtime_tbl IS TABLE OF oper_leadtime_percent_typ
459 INDEX BY BINARY_INTEGER ;
460 oper_leadtime_percent	oper_leadtime_tbl;
461 --  venu 7391495
462 
463 /* SGIDUGU Seq Dep Table Definition */
464 TYPE gmp_sequence_typ IS RECORD
465 (
466   oprn_id      PLS_INTEGER,
467   category_id  PLS_INTEGER,
468   seq_dep_id   PLS_INTEGER
469 );
470 
471 seq_rec  gmp_sequence_typ;
472 
473 TYPE gmp_setup_tbl  IS TABLE OF gmp_sequence_typ INDEX BY BINARY_INTEGER;
474 setupid_tab   gmp_setup_tbl ;
475 
476 /* End of SGIDUGU Seq Dep Table Definition */
477 
478 
479               /* Global Scalar values follow
480                  =========================== */
481 
482 l_debug                  VARCHAR2(1) := NVL(FND_PROFILE.VALUE('GMP_DEBUG_ENABLED'),'N'); -- BUG: 8420747
483 
484 
485 s		                 PLS_INTEGER := 1;
486 sd_index                  INTEGER := 0 ;  -- B4918786 (RDP) SDS
487 p_location                VARCHAR2(300) := NULL;
488 
489 g_aps_eff_id              PLS_INTEGER := 0;  /* Global Aps Effectivity ID */
490 aps_fmeff_id              PLS_INTEGER := 0 ; /* Generated effectivity Id */
491 x_aps_fmeff_id            PLS_INTEGER := 0 ; /* encoded effectivity Id */
492 
493 g_fm_dtl_start_loc        PLS_INTEGER := 0; /* Start detail location */
494 g_fm_dtl_end_loc          PLS_INTEGER := 0;  /* End detail location */
495 g_fm_hdr_loc              PLS_INTEGER := 1;  /* Starting for formula header */
496 g_formula_orgn_count_tab  PLS_INTEGER := 1;  /* Starting for formula orgn detail */
497 g_rstep_loc	 	  PLS_INTEGER := 1 ;  /* global rtg offset location */
498 g_curr_rstep_loc	  PLS_INTEGER  := -1 ; /* current r step offsetp locn */
499 g_prev_formula_id 	  PLS_INTEGER := -1 ;
500 g_prev_locn 		  PLS_INTEGER := 1;
501 g_dep_index               PLS_INTEGER := 1;
502 
503 effectivity_size          PLS_INTEGER;  /* Number of rows in validity rules 14013503 */
504 alt_rsrc_size             PLS_INTEGER;  /* Number of rows in formula_headers */
505 formula_headers_size      PLS_INTEGER;  /* Number of rows in formula_headers */
506 formula_details_size      PLS_INTEGER;  /* Number of rows in formula_details */
507 formula_orgn_size  	  PLS_INTEGER;  /* Number of detail rows for formula */
508 routing_headers_size      PLS_INTEGER;  /* Number of rows in routing_headers */
509 rtg_org_dtl_size      	  PLS_INTEGER;  /* Number of rows in routing_org_details */
510 rtg_gen_dtl_size          PLS_INTEGER;  /* Number of rows in generic routing_det */
511 material_assocs_size      PLS_INTEGER;  /* Number of rows in material_assocs */
512 /* NAMIT_CR */
513 setup_size                PLS_INTEGER;  /* Number of rows in Seq Dep Cursor */
514 /* SGIDUGU_seq_dep */
515 opr_stpdep_size           PLS_INTEGER := 1;  /* Number of rows in step dependency */
516 
517 recipe_orgn_over_size     PLS_INTEGER;  /* No. of rows in recipe orgn override */
518 recipe_override_size      PLS_INTEGER;  /* Number of rows in recipe override */
519 rtg_offsets_size 	  PLS_INTEGER := 1;  /* Number of rows in rtg offsets tbl */
520 oper_leadtime_size        PLS_INTEGER := 1;  -- Vpedarla 7391495
521 effectivity_cnt           INTEGER := 0 ; -- Charter 14013503 vijay Induri 5/11/2012
522 
523 current_date_time         DATE;	    /* For consistency writes */
524 instance_id               PLS_INTEGER;
525 delimiter   VARCHAR2(1);  /* Used when filling in comment columns on BOM and ROUTING Tables */
526 l_in_str_org              VARCHAR2(8000) := null ;   /* B3491625 */
527 
528 at_apps_link              VARCHAR2(128); /* Database link to APPS server from Planning server  */
529 g_instance_id             PLS_INTEGER;       /* Instance Id from Planning server  */
530 v_cp_enabled              BOOLEAN := FALSE;
531 g_mat_assoc               PLS_INTEGER;  /* Glabal counter for materail assiciation */
532 g_gmp_uom_class           VARCHAR2(10); /* UOM Class */
533 g_setup_id                PLS_INTEGER; /* hold he last setup_id */
534 
535 /* These variables store the MTQ related values that is last inserted. */
536 g_old_formula_id          PLS_INTEGER ; /* B3970993 */
537 g_old_recipe_id           PLS_INTEGER ; /* B3970993 */
538 g_old_rtg_id              PLS_INTEGER ; /* B3970993 */
539 g_old_rtgstep_id          PLS_INTEGER ; /* B3970993 */
540 g_old_aps_item_id         PLS_INTEGER ; /* B3970993 */
541 g_mtq_loc                 PLS_INTEGER ; /* B3970993 */
542 g_min_mtq                 NUMBER ; /* B3970993 */
543 
544 /*B5176291 - Item substitution changes - start*/
545 loop_ctr                PLS_INTEGER;
546 k                       PLS_INTEGER;
547 g_formline_id           PLS_INTEGER;
548 l_counter               PLS_INTEGER;
549 ae_date                 DATE;
550 ae_flag                 BOOLEAN;
551 chg_stdate              BOOLEAN;
552 v_gmd_seq               VARCHAR2(4000) := NULL;
553 v_gmd_formula_lineid    INTEGER := 0;
554 gmd_formline_cnt        INTEGER := 0 ;
555 op_formline_cnt         INTEGER := 0 ;
556 get_sign                INTEGER;
557 /*B5176291 - Item substitution changes - end*/
558 
559 /* bug:	6918852 Vpedarla created new global variabelt control precision in inventory uom conversion */
560 conv_precision          INTEGER := 9 ;
561 
562 /* ---------------------------  Global declarations ------------------------ */
563 TYPE sr_instance_id IS TABLE OF msc_st_boms.sr_instance_id%TYPE INDEX BY BINARY_INTEGER;
564 bom_sr_instance_id  sr_instance_id;
565 bomc_sr_instance_id sr_instance_id;
566 pef_sr_instance_id sr_instance_id;
567 rtg_sr_instance_id sr_instance_id;
568 or_sr_instance_id sr_instance_id;
569 opr_sr_instance_id sr_instance_id;
570 rs_sr_instance_id sr_instance_id;
571 oc_sr_instance_id sr_instance_id;
572 /* NAMIT_MTQ */
573 itm_mtq_sr_instance_id sr_instance_id;
574 /* NAMIT_CR */
575 opr_stpdep_sr_instance_id sr_instance_id;
576 
577 TYPE organization_id IS TABLE OF msc_st_boms.organization_id%TYPE INDEX BY BINARY_INTEGER;
578 bom_organization_id  organization_id;
579 bomc_organization_id organization_id;
580 pef_organization_id organization_id;
581 rtg_organization_id organization_id;
582 oc_organization_id organization_id;
583 gt_organization_id organization_id;
584 /* NAMIT_MTQ */
585 itm_mtq_organization_id       organization_id ;
586 opr_stpdep_organization_id    organization_id ;
587 opr_organization_id   organization_id ;
588 or_organization_id    organization_id ;
589 rs_organization_id    organization_id ;
590 
591 TYPE bill_sequence_id IS TABLE OF msc_st_boms.bill_sequence_id%TYPE INDEX BY BINARY_INTEGER;
592 bom_bill_sequence_id bill_sequence_id;
593 bomc_bill_sequence_id bill_sequence_id;
594 pef_bill_sequence_id bill_sequence_id;
595 oc_bill_sequence_id bill_sequence_id;
596 
597 TYPE last_update_date IS TABLE OF msc_st_boms.last_update_date%TYPE INDEX BY BINARY_INTEGER;
598 bom_last_update_date last_update_date ;
599 bomc_last_update_date last_update_date ;
600 pef_last_update_date last_update_date ;
601 rtg_last_update_date last_update_date ;
602 or_last_update_date last_update_date ;
603 opr_last_update_date last_update_date ;
604 opr_stpdep_last_update_date last_update_date ;  /* 7363807 */
605 itm_mtq_last_update_date last_update_date ;  /* 7363807 */
606 rs_last_update_date last_update_date ;
607 oc_last_update_date last_update_date ;
608 
609 TYPE creation_date IS TABLE OF msc_st_boms.creation_date%TYPE INDEX BY BINARY_INTEGER;
610 bom_creation_date creation_date ;
611 bomc_creation_date creation_date ;
612 pef_creation_date creation_date ;
613 rtg_creation_date creation_date ;
614 or_creation_date creation_date ;
615 opr_creation_date creation_date ;
616 opr_stpdep_creation_date creation_date ;  /* 7363807 */
617 itm_mtq_creation_date  creation_date ;  /* 7363807 */
618 rs_creation_date creation_date ;
619 oc_creation_date creation_date ;
620 
621 TYPE effectivity_date IS TABLE OF msc_st_process_effectivity.effectivity_date%TYPE
622 INDEX BY BINARY_INTEGER;
623 pef_effectivity_date effectivity_date   ;
624 bomc_effectivity_date effectivity_date   ;
625 opr_effectivity_date effectivity_date   ;
626 
627 TYPE routing_sequence_id IS TABLE OF msc_st_routings.routing_sequence_id%TYPE
628 INDEX BY BINARY_INTEGER;
629 rtg_routing_sequence_id routing_sequence_id   ;
630 pef_routing_sequence_id routing_sequence_id   ;
631 or_routing_sequence_id routing_sequence_id   ;
632 opr_routing_sequence_id routing_sequence_id   ;
633 rs_routing_sequence_id routing_sequence_id   ;
634 oc_routing_sequence_id routing_sequence_id   ;
635 /* NAMIT_MTQ */
636 itm_mtq_routing_sequence_id routing_sequence_id   ;
637 /* NAMIT_CR */
638 opr_stpdep_routing_sequence_id routing_sequence_id   ;
639 
640 TYPE uom_code IS TABLE OF msc_st_bom_components.uom_code%TYPE INDEX BY BINARY_INTEGER;
641 bomc_uom_code  uom_code  ;
642 rtg_uom_code   uom_code ;
643 or_uom_code    uom_code ;
644 opr_uom_code   uom_code ;
645 
646 TYPE assembly_item_id IS TABLE OF msc_st_boms.assembly_item_id%TYPE
647 INDEX BY BINARY_INTEGER;
648 bom_assembly_item_id assembly_item_id ;
649 rtg_assembly_item_id assembly_item_id ;
650 
651 TYPE component_sequence_id IS TABLE OF msc_st_bom_components.component_sequence_id%TYPE
652 INDEX BY BINARY_INTEGER;
653 bomc_component_sequence_id component_sequence_id;
654 oc_component_sequence_id component_sequence_id;
655 
656 TYPE operation_sequence_id IS TABLE OF msc_st_operation_resources.operation_sequence_id%TYPE
657 INDEX BY BINARY_INTEGER;
658 or_operation_sequence_id  operation_sequence_id   ;
659 opr_operation_sequence_id operation_sequence_id   ;
660 rs_operation_sequence_id  operation_sequence_id   ;
661 oc_operation_sequence_id  operation_sequence_id   ;
662 
663 -- Bug 7391495 Vpedarla
664 TYPE operation_leadtime_per IS TABLE OF msc_st_routing_operations.operation_lead_time_percent%TYPE
665 INDEX BY BINARY_INTEGER;
666 opr_lead_time_percent     operation_leadtime_per  ;
667 
668 TYPE resource_seq_num IS TABLE OF msc_st_operation_resources.resource_seq_num%TYPE
669 INDEX BY BINARY_INTEGER;
670 or_resource_seq_num resource_seq_num   ;
671 rs_resource_seq_num resource_seq_num   ;
672 /* SGIDUGU - Seq Dep */
673 TYPE setup_id_typ IS TABLE OF gmp_sequence_types.seq_dep_id%TYPE INDEX BY BINARY_INTEGER;
674 or_setup_id     setup_id_typ   ;
675 gt_setup_id     setup_id_typ   ;
676 --
677 TYPE seq_dep_class_typ IS TABLE OF ic_item_mst.seq_dpnd_class%TYPE INDEX BY
678 BINARY_INTEGER;
679 gt_seq_dep_class     seq_dep_class_typ   ;
680 --
681 TYPE oprn_no_typ IS TABLE OF gmd_operations.oprn_no%TYPE INDEX BY
682 BINARY_INTEGER;
683 gt_oprn_no     oprn_no_typ ;
684 
685 /* End of changes SGIDUGU - Seq Dep */
686 
687 /* -------------------------------  BOM declarations --------------------------- */
688 TYPE alternate_bom_designator IS TABLE OF msc_st_boms.alternate_bom_designator%TYPE
689 INDEX BY BINARY_INTEGER;
690 bom_alternate_bom_designator alternate_bom_designator ;
691 
692 TYPE specific_assembly_comment IS TABLE OF msc_st_boms.specific_assembly_comment%TYPE
693 INDEX BY BINARY_INTEGER;
694 bom_specific_assembly_comment specific_assembly_comment ;
695 
696 TYPE scaling_type IS TABLE OF msc_st_boms.scaling_type%TYPE
697 INDEX BY BINARY_INTEGER;
698 bom_scaling_type scaling_type ;
699 
700 TYPE assembly_quantity IS TABLE OF msc_st_boms.assembly_quantity%TYPE
701 INDEX BY BINARY_INTEGER;
702 bom_assembly_quantity assembly_quantity ;
703 
704 TYPE uom IS TABLE OF msc_st_boms.uom%TYPE INDEX BY BINARY_INTEGER;
705 bom_uom uom ;
706 
707 /* NAMIT_CR For Step Material Assoc */
708 TYPE seq_num IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
709 bom_op_seq_number seq_num;
710 
711 /* NAMIT_OC For ingredients contribute_to_step_qty will
712         store 1 for YES and 0 for NO */
713 TYPE contribute_to_step_qty_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
714 bomc_contribute_to_step_qty contribute_to_step_qty_type;
715 
716 bom_index INTEGER := 0 ;   /* BOM Global counter */
717 
718 /* ---------------------------  BOM Components declarations ------------------------ */
719 TYPE Inventory_item_id IS TABLE OF msc_st_bom_components.Inventory_item_id%TYPE
720 INDEX BY BINARY_INTEGER;
721 bomc_Inventory_item_id Inventory_item_id;
722 
723 TYPE using_assembly_id IS TABLE OF msc_st_bom_components.using_assembly_id%TYPE
724 INDEX BY BINARY_INTEGER;
725 bomc_using_assembly_id using_assembly_id    ;
726 
727 TYPE component_type IS TABLE OF msc_st_bom_components.component_type%TYPE
728 INDEX BY BINARY_INTEGER;
729 bomc_component_type component_type    ;
730 
731 TYPE bc_scaling_type IS TABLE OF msc_st_bom_components.scaling_type%TYPE
732 INDEX BY BINARY_INTEGER;
733 bomc_scaling_type  bc_scaling_type;
734 
735 TYPE usage_quantity IS TABLE OF msc_st_bom_components.usage_quantity%TYPE
736 INDEX BY BINARY_INTEGER;
737 bomc_usage_quantity usage_quantity ;
738 
739 TYPE operation_offset_percent IS TABLE OF msc_st_bom_components.operation_offset_percent%TYPE
740 INDEX BY BINARY_INTEGER;
741 bomc_opr_offset_percent  operation_offset_percent ;
742 
743 TYPE optional_component IS TABLE OF msc_st_bom_components.optional_component%TYPE
744 INDEX BY BINARY_INTEGER;
745 bomc_optional_component  optional_component ;
746 
747 TYPE wip_supply_type IS TABLE OF msc_st_bom_components.wip_supply_type%TYPE
748 INDEX BY BINARY_INTEGER;
749 bomc_wip_supply_type wip_supply_type ;
750 
751 TYPE scale_multiple IS TABLE OF msc_st_bom_components.scale_multiple%TYPE
752 INDEX BY BINARY_INTEGER;
753 bomc_scale_multiple  scale_multiple;
754 
755 TYPE scale_rounding_variance IS TABLE OF msc_st_bom_components.scale_rounding_variance%TYPE
756 INDEX BY BINARY_INTEGER;
757 bomc_scale_rounding_variance scale_rounding_variance ;
758 
759 TYPE rounding_direction IS TABLE OF msc_st_bom_components.rounding_direction%TYPE
760 INDEX BY BINARY_INTEGER;
761 bomc_rounding_direction  rounding_direction ;
762 
763 /*B5176291 - Item substitution changes - start*/
764 TYPE b_disable_date IS TABLE OF msc_st_bom_components.disable_date%TYPE
765 INDEX BY BINARY_INTEGER;
766 bomc_disable_date  b_disable_date ;
767 /*B5176291 - Item substitution changes - end*/
768 
769 bomc_index INTEGER := 0 ;   /* BOM component Global counter */
770 
771 /* ---------------------------  Effectivity declarations ------------------------ */
772 TYPE process_sequence_id IS TABLE OF msc_st_process_effectivity.process_sequence_id%TYPE
773 INDEX BY BINARY_INTEGER;
774 pef_process_sequence_id process_sequence_id   ;
775 
776 TYPE item_id IS TABLE OF msc_st_process_effectivity.item_id%TYPE INDEX BY BINARY_INTEGER;
777 pef_item_id item_id   ;
778 
779 TYPE disable_date IS TABLE OF msc_st_process_effectivity.disable_date%TYPE
780 INDEX BY BINARY_INTEGER;
781 pef_disable_date disable_date   ;
782 
783 TYPE minimum_quantity IS TABLE OF msc_st_process_effectivity.minimum_quantity%TYPE
784 INDEX BY BINARY_INTEGER;
785 pef_minimum_quantity minimum_quantity   ;
786 
787 TYPE maximum_quantity IS TABLE OF msc_st_process_effectivity.maximum_quantity%TYPE
788 INDEX BY BINARY_INTEGER;
789 pef_maximum_quantity maximum_quantity   ;
790 
791 TYPE preference IS TABLE OF msc_st_process_effectivity.preference%TYPE
792 INDEX BY BINARY_INTEGER;
793 pef_preference preference ;
794 
795  -- Bug: 8715318 Vpedarla uncommented the below line.
796  TYPE recipe IS TABLE OF msc_st_process_effectivity.recipe%TYPE
797  INDEX BY BINARY_INTEGER;
798  pef_recipe recipe ;
799 
800 pef_index INTEGER := 0 ;   /* Process Effectivity Global counter */
801 
802 /* -------------------------------  Routng declarations  --------------------------- */
803 TYPE routing_comment IS TABLE OF msc_st_routings.routing_comment%TYPE
804 INDEX BY BINARY_INTEGER;
805 rtg_routing_comment routing_comment ;
806 empty_rtg_comment routing_comment ;
807 
808 TYPE alt_routing_designator  IS TABLE OF msc_st_routings.alternate_routing_designator%TYPE
809 INDEX BY BINARY_INTEGER;
810 rtg_alt_routing_designator alt_routing_designator   ;
811 l_12137_count number;  -- Biogen
812 l_apps_ver    number;  -- Biogen
813 TYPE routing_quantity IS TABLE OF msc_st_routings.routing_quantity%TYPE
814 INDEX BY BINARY_INTEGER;
815 rtg_routing_quantity routing_quantity   ;
816 
817 /* NAMIT_CR For Calculate Step Dependency Flag */
818 TYPE auto_step_qty_flag IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
819 rtg_auto_step_qty_flag auto_step_qty_flag  ;
820 
821 rtg_index INTEGER := 0 ;   /* Routing Global counter */
822 
823 /* -------------------------- Routng operations declarations  ------------------------ */
824 TYPE resource_id IS TABLE OF msc_st_operation_resources.resource_id%TYPE
825 INDEX BY BINARY_INTEGER;
826 or_resource_id resource_id   ;
827 gt_resource_id resource_id   ;
828 
829 TYPE alternate_number IS TABLE OF msc_st_operation_resources.alternate_number%TYPE
830 INDEX BY BINARY_INTEGER;
831 or_alternate_number alternate_number   ;
832 
833 TYPE principal_flag IS TABLE OF msc_st_operation_resources.principal_flag%TYPE
834 INDEX BY BINARY_INTEGER;
835 or_principal_flag principal_flag   ;
836 
837 TYPE basis_type IS TABLE OF msc_st_operation_resources.basis_type%TYPE
838 INDEX BY BINARY_INTEGER;
839 or_basis_type basis_type   ;
840 
841 TYPE resource_usage IS TABLE OF msc_st_operation_resources.resource_usage%TYPE
842 INDEX BY BINARY_INTEGER;
843 or_resource_usage resource_usage   ;
844 
845 TYPE max_resource_units IS TABLE OF msc_st_operation_resources.max_resource_units%TYPE
846 INDEX BY BINARY_INTEGER;
847 or_max_resource_units max_resource_units   ;
848 
849 TYPE resource_units IS TABLE OF msc_st_operation_resources.resource_units%TYPE
850 INDEX BY BINARY_INTEGER;
851 or_resource_units resource_units   ;
852 
853 TYPE or_orig_rs_seq_num_typ IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
854 or_orig_rs_seq_num or_orig_rs_seq_num_typ ;
855 or_break_ind or_orig_rs_seq_num_typ;
856 
857 or_index PLS_INTEGER := 0 ;   /* Operation Resource Global counter */
858 
859 /* -------------------------- Operations declarations  ------------------------ */
860 TYPE operation_seq_num IS TABLE OF msc_st_routing_operations.operation_seq_num%TYPE
861 INDEX BY BINARY_INTEGER;
862 opr_operation_seq_num operation_seq_num   ;
863 
864 TYPE operation_description IS TABLE OF msc_st_routing_operations.operation_description%TYPE
865 INDEX BY BINARY_INTEGER;
866 opr_operation_description operation_description   ;
867 
868 TYPE mtransfer_quantity IS TABLE OF msc_st_routing_operations.minimum_transfer_quantity%TYPE
869 INDEX BY BINARY_INTEGER;
870 opr_mtransfer_quantity mtransfer_quantity   ;
871 
872 TYPE department_id IS TABLE OF msc_st_routing_operations.department_id%TYPE
873 INDEX BY BINARY_INTEGER;
874 opr_department_id department_id ;
875 rs_department_id department_id ;
876 
877 TYPE department_code IS TABLE OF msc_st_routing_operations.department_code%TYPE
878 INDEX BY BINARY_INTEGER;
879 opr_department_code department_code ;
880 
881 TYPE activity_group_id IS TABLE OF msc_st_operation_resource_seqs.activity_group_id%TYPE
882 INDEX BY BINARY_INTEGER;
883 rs_activity_group_id activity_group_id ;
884 
885 TYPE schedule_flag IS TABLE OF msc_st_operation_resource_seqs.activity_group_id%TYPE
886 INDEX BY BINARY_INTEGER;
887 rs_schedule_flag schedule_flag ;
888 
889 /* NAMIT_MTQ */
890 TYPE operation_seq_id IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
891 itm_mtq_from_op_seq_id operation_seq_id ;
892 /* NAMIT_CR */
893 opr_stpdep_frm_seq_id operation_seq_id ;
894 opr_stpdep_to_seq_id operation_seq_id ;
895 
896 /* NAMIT_CR */
897 TYPE dependency_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
898 opr_stpdep_dependency_type dependency_type ;
899 
900 TYPE minimum_time_offset IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
901 itm_mtq_min_time_offset minimum_time_offset ;
902 itm_mtq_max_time_offset minimum_time_offset ;
903 /* NAMIT_CR */
904 opr_stpdep_min_time_offset minimum_time_offset ;
905 opr_stpdep_max_time_offset minimum_time_offset ;
906 
907 /* NAMIT_CR */
908 TYPE transfer_pct IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
909 opr_stpdep_trans_pct transfer_pct ;
910 
911 TYPE from_op_seq_num IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
912 itm_mtq_frm_op_seq_num from_op_seq_num ;
913 /* NAMIT_CR */
914 opr_stpdep_frm_op_seq_num from_op_seq_num ;
915 opr_stpdep_to_op_seq_num from_op_seq_num ;
916 
917 /* NAMIT_CR */
918 TYPE apply_to_charges IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
919 opr_stpdep_app_to_chrg apply_to_charges ;
920 
921 TYPE from_item_id IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
922 itm_mtq_from_item_id from_item_id ;
923 
924 TYPE minimum_transfer_qty IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
925 itm_mtq_min_tran_qty minimum_transfer_qty ;
926 
927 /* NAMIT_OC */
928 TYPE min_capacity IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
929 or_minimum_capacity min_capacity ;
930 or_maximum_capacity min_capacity ;
931 
932 /* NAMIT_ASQC */
933 TYPE step_qty_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
934 opr_step_qty step_qty_type;
935 
936 TYPE step_qty_uom_type IS TABLE OF VARCHAR2(3) INDEX BY BINARY_INTEGER;
937 opr_step_qty_uom step_qty_uom_type;
938 
939 opr_index PLS_INTEGER := 0 ;   /* Operation Global counter */
940 rs_index  PLS_INTEGER := 0 ;   /* Operation Global counter */
941 oc_index  PLS_INTEGER := 0 ;   /* Operation component Global counter */
942 /* NAMIT_MTQ */
943 mtq_index PLS_INTEGER := 0 ;   /* MTQ Global counter */
944 
945 
946 /*
947 REM+=========================================================================+
948 REM| PROCEDURE NAME                                                          |
949 REM|    retrieve_effectivities                                               |
950 REM|                                                                         |
951 REM| TYPE                                                                    |
952 REM|    Private                                                              |
953 REM|                                                                         |
954 REM| DESCRIPTION                                                             |
955 REM|    The majority of the logic in this procedure is contained in the four |
956 REM|    cursors. There is one for each type of effectivity as follows:       |
957 REM|      case 1: Effectivity has an organisation and a routing              |
958 REM|      case 2: Effectivity has an organisation but no routing             |
959 REM|      case 3: Effectivity has no organisation but has a routing          |
960 REM|      case 4: Effectivity has no organisation and no routing             |
961 REM|    Depending on each case above, the retrieval logic differs slightly   |
962 REM|    as does the source of some the values which come back. To make it    |
963 REM|    simpler to understand (and maintain) each case was dealt             |
964 REM|    with on its own. By putting as much logic as possible in the SQL it  |
965 REM|    was hoped to optimise the database accesses and make the code itself |
966 REM|    simpler.                                                             |
967 REM|                                                                         |
968 REM| INPUT PARAMETERS                                                        |
969 REM|    none                                                                 |
970 REM|                                                                         |
971 REM| OUTPUT PARAMETERS                                                       |
972 REM|    return_status  TRUE=> OK                                             |
973 REM|                                                                         |
974 REM| INPUT/OUTPUT PARAMETERS                                                 |
975 REM|    None                                                                 |
976 REM|                                                                         |
977 REM| HISTORY                                                                 |
978 REM|    Created 12th July 1999 by P.J.Schofield (OPM Development Oracle UK)  |
979 REM|    04/03/2000 - Using organization_id from gmp_item_aps , instead of    |
980 REM|                 organization_id from sy_orgn_mst, Bug# 1252322          |
981 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
982 REM| 01/14/2003   Sridhar Gidugu  - Using gia.uom_code instead of gia.item_um|
983 REM|                                Bug# 2739627                             |
984 REM| 06/02/2003   Sridhar Gidugu  - B2989806 - Forward port fix for          |
985 REM|                                Bug#2916619 - Rewrote Effectivity Cursor |
986 REM+=========================================================================+
987 */
988 PROCEDURE retrieve_effectivities
989 (
990   return_status  OUT NOCOPY BOOLEAN
991 )
992 IS
993 cur_effectivity     ref_cursor_typ; -- Charter 14013503 vijay Induri 5/11/2012
994   c_formula_effectivity   ref_cursor_typ;
995   cur_routing_hdr     ref_cursor_typ;
996   cur_routing_dtl     ref_cursor_typ;
997   cur_formula_dtl     ref_cursor_typ;
998   cur_formula_hdr     ref_cursor_typ;
999   cur_mat_assoc       ref_cursor_typ;
1000   cur_alt_resource    ref_cursor_typ;
1001   c_recipe_override   ref_cursor_typ;
1002   c_recipe_orgn       ref_cursor_typ;
1003   c_uom_conv          ref_cursor_typ; /*B2870041 uom conversion cursor*/
1004   cur_oper_lead_time  ref_cursor_typ;  -- Vpedarla 7391495
1005   cur_rtg_offsets     ref_cursor_typ;
1006   cur_opr_stpdep      ref_cursor_typ;
1007   seq_dep_dtl         ref_cursor_typ;
1008   setup_id_dtl        ref_cursor_typ;
1009   uom_code_ref        ref_cursor_typ;
1010   source_call_ref     ref_cursor_typ ; /*B8230710*/
1011 
1012   retrieval_cursor        VARCHAR2(32767) ;
1013   effectivity_cursor      VARCHAR2(32767) ;
1014   formula_hdr_cursor      VARCHAR2(32767) ;
1015   formula_dtl_cursor      VARCHAR2(32767) ;
1016   routing_hdr_cursor      VARCHAR2(32767) ;
1017   mat_assoc_cursor        VARCHAR2(32767) ;
1018   recipe_orgn_statement   VARCHAR2(32767) ;
1019   recipe_statement        VARCHAR2(32767) ;
1020   uom_conv_cursor         VARCHAR2(32767) ; /*B2870041 hold sql for uom conv*/
1021   oper_lead_time_cur_stmt VARCHAR2(32767) ;  -- Vpedarla 7391495
1022   rtg_offset_cur_stmt     VARCHAR2(32767) ;
1023   statement_alt_resource  VARCHAR2(32767) ;
1024   opr_stpdep_cursor       VARCHAR2(32767) ;
1025   seq_dep_cursor          VARCHAR2(32767) ;
1026   setup_id_cursor         VARCHAR2(32767) ;
1027   uom_code_cursor         VARCHAR2(32767) ;
1028 
1029   valid                   BOOLEAN ;
1030   routing_valid           BOOLEAN ;
1031 
1032   old_fmeff_id            PLS_INTEGER ;
1033   old_organization_id     PLS_INTEGER ;
1034   old_formula_id          PLS_INTEGER ;
1035   mat_start_indx          PLS_INTEGER ;
1036   mat_end_indx            PLS_INTEGER ;
1037   eff_counter		  INTEGER ;
1038   s                       INTEGER ;
1039   j                       PLS_INTEGER ; /*B2870041 for loop index*/
1040 
1041   temp_total_qty          NUMBER ; /*B2870041 temp var to calculate total output*/
1042   v_matl_qty              NUMBER ; /*B2870041 cursor var to get uom conv qty */
1043   spl_cnt                 NUMBER ;
1044   end_index               PLS_INTEGER ; /*B2870041 for loop index*/
1045   old_route               PLS_INTEGER ; /*B2870041 for loop index*/
1046   old_orgn_id             PLS_INTEGER ; /*B2870041 for loop index*/
1047   old_step                PLS_INTEGER ; /*B2870041 for loop index*/
1048   ri                      PLS_INTEGER ; /*B2870041 for loop index*/
1049   found                   NUMBER ; /*B2870041 for loop index*/
1050   first_step_row          PLS_INTEGER ; /*B2870041 for loop index*/
1051   found_chrg_rsrc         PLS_INTEGER ;
1052   chrg_activity           VARCHAR2(16) ;
1053   l_gmp_um_code           VARCHAR2(25) ;
1054   v_dummy                 NUMBER ;   /* B8230710 hold the statistics */
1055 
1056   -- Bug:6087535 Vpedarla 23-07-07 added for item substituion
1057   loop_ctr                PLS_INTEGER;
1058   k                       PLS_INTEGER;
1059   orig_start_date         DATE;
1060   substcount              PLS_INTEGER;
1061   enddatenull             BOOLEAN:=FALSE;
1062   nullenddatefound        BOOLEAN := FALSE;
1063   l_eff_counter           NUMBER;            -- Vpedarla Bug: 8230710
1064   source_call             VARCHAR2(32767) ;  -- Vpedarla Bug: 8230710
1065   source_call_result      NUMBER;           -- Vpedarla Bug: 8230710
1066   V_process_Org              Varchar2(1000); --vkinduri Bug: 14196938
1067   l_process_org_cnt          NUMBER;         --vkinduri Bug: 14196938
1068 
1069 BEGIN
1070   retrieval_cursor        := NULL;
1071   effectivity_cursor      := NULL;
1072   formula_hdr_cursor      := NULL;
1073   formula_dtl_cursor      := NULL;
1074   routing_hdr_cursor      := NULL;
1075   mat_assoc_cursor        := NULL;
1076   recipe_orgn_statement   := NULL;
1077   recipe_statement        := NULL;
1078   uom_conv_cursor         := NULL; /*B2870041 hold sql for uom conv*/
1079   rtg_offset_cur_stmt     := NULL;
1080   statement_alt_resource  := NULL;
1081   opr_stpdep_cursor       := NULL;
1082   seq_dep_cursor          := NULL;
1083   setup_id_cursor         := NULL;
1084   uom_code_cursor         := NULL;
1085   oper_lead_time_cur_stmt := NULL ; -- Vpedarla 7391495
1086   valid                   := FALSE;
1087   routing_valid           := FALSE;
1088   old_fmeff_id            := 0 ;
1089   old_organization_id     := 0 ;
1090   old_formula_id          := 0 ;
1091   mat_start_indx          := 0 ;
1092   mat_end_indx            := 0 ;
1093   eff_counter		  := 0;
1094   s                       := 1 ;
1095   j                       := 0; /*B2870041 for loop index*/
1096   temp_total_qty          := 0; /*B2870041 temp var to calculate total output*/
1097   v_matl_qty              := 0; /*B2870041 cursor var to get uom conv qty */
1098   spl_cnt                 := 1 ;
1099   end_index               := 0; /*B2870041 for loop index*/
1100   old_route               := 0; /*B2870041 for loop index*/
1101   old_orgn_id             := 0; /*B2870041 for loop index*/
1102   old_step                := 0; /*B2870041 for loop index*/
1103   ri                      := 0; /*B2870041 for loop index*/
1104   found                   := 0; /*B2870041 for loop index*/
1105   first_step_row          := 0; /*B2870041 for loop index*/
1106   found_chrg_rsrc         := 0;
1107   chrg_activity           := NULL;
1108   l_gmp_um_code           := NULL;
1109 
1110   g_fm_dtl_start_loc      := 0; /* Start detail location */
1111   g_fm_dtl_end_loc        := 0; /* End detail location */
1112   g_fm_hdr_loc            := 1; /* Starting for formula header */
1113   g_formula_orgn_count_tab := 1; /* Starting for formula orgn detail */
1114 
1115   g_rstep_loc             := 1 ;
1116   g_curr_rstep_loc        := -1 ;
1117   g_prev_formula_id       := -1 ;
1118   g_prev_locn             := 1;
1119 
1120   enddatenull             := FALSE;
1121   nullenddatefound        := FALSE;
1122   v_dummy                 := 0; /* B8230710 */
1123   l_process_org_cnt       := 0;
1124   V_process_Org           := NULL;
1125 
1126     dbms_session.free_unused_user_memory;
1127 
1128      /* populate the org_string    */
1129      IF gmp_calendar_pkg.org_string(g_instance_id) THEN
1130         NULL ;
1131      ELSE
1132         RAISE invalid_string_value  ;
1133      END IF;
1134 
1135     l_in_str_org := gmp_calendar_pkg.g_in_str_org ;  /* B3491625 */
1136 
1137 
1138     LOG_MESSAGE('l_in_str_org'||'  '||l_in_str_org);
1139     time_stamp;
1140 
1141     /* Bug: 8230710 Call the remote procedure to populate gmp_form_eff with new
1142        effective validity rules. this will avoid insert over DB link for each effectivity */
1143        -- Bug: 9572568 Vpedrla Made changes to the procedure call populate_eff ;
1144            LOG_MESSAGE(' Calling populate_eff ');
1145     source_call := 'begin apps.gmp_utility.populate_eff'||at_apps_link||'( :p1 , :p2  ); end ; ';
1146     Execute immediate source_call  using in l_in_str_org , out source_call_result ;  -- Bug: 9572568 Vpedrla
1147     IF  source_call_result = 2 THEN
1148         RAISE source_call_failure;
1149     END IF;
1150 
1151     LOG_MESSAGE('after the Source call: populate_eff = ' || source_call_result );
1152     time_stamp;
1153 
1154     /* B2870041 changed cursor to retrieve the just the routing qty no uom
1155        conv, added uom conv of the product to the routing uom for a qty of 1
1156        to get the factor. The factor will be used later. added product index
1157        to allow access when we are writing out the routing */
1158     /* The query is being modified to incorporate changes for 1830940 */
1159     /* B2989806 Added inline tables and outer joins to select aps_fmeff_id */
1160 
1161 /* NAMIT UOM Changes */
1162 
1163 /* bug: 6710684 Vpedarla making changes to take the profile value from source server
1164            and also made changes to use procedure get_profile_value */
1165 --       commented the below code line
1166 --      l_gmp_um_code   := fnd_profile.VALUE('BOM:HOUR_UOM_CODE');
1167         l_gmp_um_code   := get_profile_value('BOM:HOUR_UOM_CODE', at_apps_link );
1168 /* bug: 6710684 end of changes */
1169 
1170     IF l_gmp_um_code IS NOT NULL THEN
1171 /* Get the UOM code and UOM Class corresponding to "BOM: UOM Hour" Profile */
1172 /* akaruppa - sy_uoms_mst replaced with the mtl_units_of_measure */
1173        uom_code_cursor := ' select uom_class '
1174                       ||' from mtl_units_of_measure'||at_apps_link
1175                       ||' where uom_code = :gmp_um_code ';
1176 
1177        OPEN uom_code_ref FOR uom_code_cursor USING l_gmp_um_code;
1178        FETCH uom_code_ref INTO g_gmp_uom_class;
1179        CLOSE uom_code_ref;
1180     ELSE
1181          RAISE invalid_gmp_uom_profile  ;
1182     END IF;
1183     IF (g_gmp_uom_class IS NULL) THEN
1184          RAISE invalid_gmp_uom_profile  ;
1185     END IF;
1186 
1187     gmp_debug_message('gmp_uom_class - '|| g_gmp_uom_class);
1188         /* B3837959 MMK Issue, Database link added for form_eff */
1189      effectivity_cursor :=
1190  ' SELECT eff.recipe_validity_rule_id, '
1191         ||' nvl(gfe.aps_fmeff_id,-1),eff.inventory_item_id, '
1192         ||' eff.formula_id,eff.organization_id, '
1193         ||' eff.start_date, eff.end_date, eff.inv_min_qty, '
1194         ||' eff.inv_max_qty, eff.preference, eff.primary_uom_code, '
1195         ||' eff.wcode, eff.routing_id, '
1196         ||' eff.routing_no, eff.routing_vers, eff.routing_desc, '
1197         ||' eff.routing_uom, eff.routing_qty, '
1198         ||' eff.prd_fct  , eff.prd_ind, '
1199         ||' eff.recipe_id, eff.recipe_no, eff.recipe_version, eff.rhdr_loc, '
1200 /* NAMIT_CR Get Calculate Step Dependency Checkbox*/
1201 /* SGIDUGU - added Category id and Setup Id */
1202         ||' decode(eff.calculate_step_quantity,0,2,1) calculate_step_quantity, '
1203         ||' scat.category_id, NULL, '
1204         ||' scat.category_concat_segs '
1205         ||'FROM (  '
1206         ||' SELECT /*+ ORDERED USE_NL(gmd_recipe_validity_rules,fm_form_mst,mtl_system_items)*/  ffe.recipe_validity_rule_id, ffe.inventory_item_id, '
1207         ||' grb.formula_id, ffe.organization_id, '
1208 	      ||' ffe.start_date, ffe.end_date, ffe.inv_min_qty, '
1209 	      ||' ffe.inv_max_qty, ffe.preference, msi.primary_uom_code, '
1210         ||' mp.organization_code wcode , grb.routing_id, '
1211 	      ||' frh.routing_no, frh.routing_vers, frh.routing_desc, '
1212         ||' frh.routing_uom, frh.routing_qty, ' /*B2870041*/
1213         ||' DECODE(frh.routing_uom,msi.primary_uom_code ,1, '
1214         ||'        inv_convert.inv_um_convert'||at_apps_link
1215         ||'                 ( ffe.inventory_item_id, '
1216         ||'                   NULL, '
1217         ||'                   ffe.organization_id, '
1218         /* bug: 6918852 Vpedarla 04-Apr-2008  used the global variable for precision*/
1219        /* ||'                   NULL, '  */
1220         ||                    conv_precision || ' , '
1221         ||'                   1, '
1222         ||'                   msi.primary_uom_code , '   /* primary */
1223         ||'                   frh.routing_uom , '   /* routing um */
1224         ||'                   NULL , '
1225         ||'                   NULL '
1226         ||'                 ) '
1227         ||'         ) prd_fct, -1 prd_ind, '
1228         ||' grb.recipe_id, grb.recipe_no, grb.recipe_version , '
1229 	      ||' 0 rhdr_loc, '
1230        /* NAMIT_CR, SGIDUGU - Seq dep Id */
1231         ||' grb.calculate_step_quantity '
1232         ||' FROM  gmd_recipes_b'||at_apps_link||' grb,'
1233         ||'       gmd_recipe_validity_rules'||at_apps_link||' ffe,'
1234         ||'       fm_form_mst'||at_apps_link||' ffm,'
1235         ||'       fm_rout_hdr'||at_apps_link||' frh,'
1236       	||'       mtl_parameters'||at_apps_link||' mp,'
1237       	||'       mtl_system_items'||at_apps_link||' msi,'
1238        	||'       hr_organization_units'||at_apps_link||' hou,'
1239         ||'       gmd_status_b'||at_apps_link||' gs1,'
1240         ||'       gmd_status_b'||at_apps_link||' gs2,'
1241         ||'       gmd_status_b'||at_apps_link||' gs3,'
1242         ||'       gmd_status_b'||at_apps_link||' gs4 '
1243         ||' WHERE grb.delete_mark = 0 '
1244         ||'   AND grb.recipe_id = ffe.recipe_id '
1245         ||'   AND grb.recipe_status = gs1.status_code '
1246         ||'   AND gs1.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1247         ||'   AND gs1.delete_mark = 0 '
1248         ||'   AND ffe.delete_mark = 0 '
1249         ||'   AND ffe.validity_rule_status = gs2.status_code '
1250         ||'   AND gs2.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1251         ||'   AND gs2.delete_mark = 0 '
1252         ||'   AND frh.delete_mark = 0 '
1253         ||'   AND ffm.delete_mark = 0 '
1254         ||'   AND nvl(hou.date_to,SYSDATE) >= SYSDATE '
1255         ||'   AND hou.organization_id = mp.organization_id '
1256         ||'   AND frh.inactive_ind = 0 '
1257         ||'   AND ffm.inactive_ind = 0 '
1258         ||'   AND grb.routing_id IS NOT NULL '
1259         ||'   AND ffe.organization_id IS NOT NULL '
1260         ||'   AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
1261         ||'   AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE '
1262         ||'   AND ffe.organization_id = mp.organization_id '
1263         ||'   AND grb.formula_id = ffm.formula_id '
1264         ||'   AND ffm.formula_status = gs3.status_code '
1265         ||'   AND gs3.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1266         ||'   AND gs3.delete_mark = 0 '
1267         ||'   AND grb.routing_id =  frh.routing_id '
1268         ||'   AND frh.routing_status =  gs4.status_code '
1269         ||'   AND gs4.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1270         ||'   AND gs4.delete_mark = 0 '
1271         ||'   AND msi.organization_id =  ffe.organization_id '
1272         ||'   AND msi.inventory_item_id =  ffe.inventory_item_id '
1273         ||'   AND msi.recipe_enabled_flag = ''Y'' '
1274         ||'   AND msi.process_execution_enabled_flag = ''Y'' '
1275         ||'   AND mp.process_enabled_flag = ''Y'' '
1276 /*B5161655 - Changed the where clause to pick up even when the formula belongs to a differnt organization
1277 from the validity rules */
1278         ||'   AND EXISTS ( SELECT /*+DRIVING_SITE(FM_MATL_DTL)*/ 1 '
1279         ||'          FROM  fm_matl_dtl'||at_apps_link||' '
1280         ||'          WHERE formula_id = grb.formula_id '
1281         ||'          AND line_type = 1 '
1282         ||'          AND inventory_item_id = msi.inventory_item_id '
1283         ||'          AND msi.organization_id = ffe.organization_id '
1284         ||'          AND inventory_item_id = ffe.inventory_item_id ) '
1285         ||' UNION ALL '
1286         ||' SELECT /*+ ORDERED USE_NL(gmd_recipe_validity_rules,fm_form_mst,mtl_system_items)*/ ffe.recipe_validity_rule_id, ffe.inventory_item_id, '
1287         ||' grb.formula_id, ffe.organization_id, '
1288         ||' ffe.start_date, ffe.end_date, ffe.inv_min_qty, '
1289         ||' ffe.inv_max_qty, ffe.preference, msi.primary_uom_code, '
1290         ||' mp.organization_code wcode , to_number(null) , '
1291         ||' NULL, to_number(null), NULL, '
1292         ||' NULL, to_number(null), to_number(null) prd_fct, -1 prd_ind, '
1293         ||' grb.recipe_id, grb.recipe_no, grb.recipe_version , '
1294         ||' 0 rhdr_loc, '
1295 /* NAMIT_CR,SGIDUGU */
1296         ||' 0 calculate_step_quantity '
1297         ||' FROM  gmd_recipes_b'||at_apps_link||' grb,'
1298         ||'       gmd_recipe_validity_rules'||at_apps_link||' ffe,'
1299         ||'       fm_form_mst'||at_apps_link||' ffm, '
1300         ||'       mtl_parameters'||at_apps_link||' mp, '
1301 	||'       mtl_system_items'||at_apps_link||' msi, '
1302 	||'       hr_organization_units'||at_apps_link||' hou,'
1303 	||'       gmd_status_b'||at_apps_link||' gs1,'
1304 	||'       gmd_status_b'||at_apps_link||' gs2,'
1305 	||'       gmd_status_b'||at_apps_link||' gs3 '
1306         ||' WHERE  grb.delete_mark = 0 '
1307         ||'   AND grb.recipe_id = ffe.recipe_id '
1308         ||'   AND grb.recipe_status = gs1.status_code '
1309         ||'   AND gs1.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1310         ||'   AND gs1.delete_mark = 0 '
1311         ||'   AND ffe.delete_mark = 0 '
1312         ||'   AND ffe.validity_rule_status = gs2.status_code '
1313         ||'   AND gs2.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1314         ||'   AND gs2.delete_mark = 0 '
1315         ||'   AND ffm.delete_mark = 0 '
1316         ||'   AND nvl(hou.date_to,SYSDATE) >= SYSDATE '
1317         ||'   AND hou.organization_id = mp.organization_id '
1318         ||'   AND ffm.inactive_ind = 0 '
1319         ||'   AND grb.routing_id IS NULL '
1320         ||'   AND ffe.organization_id IS NOT NULL '
1321         ||'   AND ffe.organization_id = mp.organization_id '
1322         ||'   AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
1323         ||'   AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE '
1324         ||'   AND grb.formula_id = ffm.formula_id '
1325         ||'   AND ffm.formula_status = gs3.status_code '
1326         ||'   AND gs3.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1327         ||'   AND gs3.delete_mark = 0 '
1328         ||'   AND msi.organization_id =  ffe.organization_id '
1329         ||'   AND msi.inventory_item_id =  ffe.inventory_item_id '
1330         ||'   AND msi.recipe_enabled_flag = ''Y'' '
1331         ||'   AND msi.process_execution_enabled_flag = ''Y'' '
1332         ||'   AND mp.process_enabled_flag = ''Y'' '
1333 /*B5161655 - Changed the where clause to pick up even when the formula belongs to a differnt organization
1334 from the validity rules */
1335         ||'   AND EXISTS ( SELECT /*+DRIVING_SITE(FM_MATL_DTL)*/ 1 '
1336         ||'          FROM  fm_matl_dtl'||at_apps_link||' '
1337         ||'          WHERE formula_id = grb.formula_id '
1338         ||'          AND line_type = 1 '
1339         ||'          AND inventory_item_id = msi.inventory_item_id '
1340         ||'          AND msi.organization_id = ffe.organization_id '
1341         ||'          AND inventory_item_id = ffe.inventory_item_id ) '
1342         ||' UNION ALL '
1343         ||' SELECT /*+ ORDERED USE_NL(gmd_recipe_validity_rules,fm_form_mst,mtl_system_items)*/ ffe.recipe_validity_rule_id, ffe.inventory_item_id, '
1344         ||' grb.formula_id, msi.organization_id, '
1345         ||' ffe.start_date, ffe.end_date, ffe.inv_min_qty, '
1346         ||' ffe.inv_max_qty, ffe.preference, msi.primary_uom_code, '
1347         ||' mp.organization_code wcode , grb.routing_id, '
1348         ||' frh.routing_no, frh.routing_vers, frh.routing_desc, '
1349         ||' frh.routing_uom, frh.routing_qty,' /*B2870041*/
1350         ||' DECODE(frh.routing_uom,msi.primary_uom_code ,1, '
1351         ||'        inv_convert.inv_um_convert'||at_apps_link
1352         ||'                 (ffe.inventory_item_id, '
1353         ||'                  NULL, '
1354         ||'                  msi.organization_id, '
1355       /* bug: 6918852 Vpedarla 04-Apr-2008  used the global variable for precision*/
1356        /* ||'                   NULL, '  */
1357         ||                    conv_precision || ' , '
1358         ||'                  1, '
1359         ||'                  msi.primary_uom_code , '   /* primary */
1360         ||'                  frh.routing_uom , '   /* routing um */
1361         ||'                  NULL , '
1362         ||'                  NULL '
1363         ||'                 ) '
1364         ||'         ) prd_fct, -1 prd_ind, '
1365         ||' grb.recipe_id, grb.recipe_no, grb.recipe_version ,'
1366 	||' 0 rhdr_loc, '
1367 /* NAMIT_CR,SGIDUGU */
1368         ||' grb.calculate_step_quantity '
1369 	||' FROM  gmd_recipes_b'||at_apps_link||' grb,'
1370 	||'       gmd_recipe_validity_rules'||at_apps_link||' ffe,'
1371 	||'       fm_form_mst'||at_apps_link||' ffm,'
1372 	||'       fm_rout_hdr'||at_apps_link||' frh,'
1373 	||'       mtl_parameters'||at_apps_link||' mp,'
1374 	||'       mtl_system_items'||at_apps_link||' msi,'
1375 	||'       hr_organization_units'||at_apps_link||' hou,'
1376 	||'       gmd_status_b'||at_apps_link||' gs1,'
1377 	||'       gmd_status_b'||at_apps_link||' gs2,'
1378 	||'       gmd_status_b'||at_apps_link||' gs3,'
1379 	||'       gmd_status_b'||at_apps_link||' gs4 '
1380         ||' WHERE grb.delete_mark = 0 '
1381         ||'   AND grb.recipe_id = ffe.recipe_id '
1382         ||'   AND grb.recipe_status = gs1.status_code '
1383         ||'   AND gs1.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1384         ||'   AND gs1.delete_mark = 0 '
1385         ||'   AND ffe.delete_mark = 0 '
1386         ||'   AND ffe.validity_rule_status = gs2.status_code '
1387         ||'   AND gs2.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1388         ||'   AND gs2.delete_mark = 0 '
1389         ||'   AND frh.delete_mark = 0 '
1390         ||'   AND ffm.delete_mark = 0 '
1391         ||'   AND nvl(hou.date_to,SYSDATE) >= SYSDATE '
1392         ||'   AND hou.organization_id = mp.organization_id '
1393         ||'   AND frh.inactive_ind = 0 '
1394         ||'   AND ffm.inactive_ind = 0 '
1395         ||'   AND grb.routing_id IS NOT NULL '
1396         ||'   AND ffe.organization_id IS NULL '
1397         ||'   AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
1398         ||'   AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE '
1399         ||'   AND grb.formula_id = ffm.formula_id '
1400         ||'   AND ffm.formula_status = gs3.status_code '
1401         ||'   AND gs3.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1402         ||'   AND gs3.delete_mark = 0 '
1403         ||'   AND grb.routing_id =  frh.routing_id '
1404         ||'   AND frh.routing_status =  gs4.status_code '
1405         ||'   AND gs4.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1406         ||'   AND gs4.delete_mark = 0 '
1407 	||'   AND mp.organization_id = msi.organization_id '
1408         ||'   AND mp.process_enabled_flag = ''Y'' '
1409         ||'   AND msi.inventory_item_id =  ffe.inventory_item_id '
1410 	||'   AND msi.recipe_enabled_flag = ''Y'' '
1411 	||'   AND msi.process_execution_enabled_flag = ''Y'' '
1412 /*B5161655 - Changed the where clause to pick up even when the formula belongs to a differnt organization
1413 from the validity rules */
1414         ||'   AND EXISTS ( SELECT /*+DRIVING_SITE(FM_MATL_DTL)*/ 1 '
1415         ||'          FROM  fm_matl_dtl'||at_apps_link||' '
1416         ||'          WHERE formula_id = grb.formula_id '
1417         ||'          AND line_type = 1 '
1418         ||'          AND inventory_item_id = msi.inventory_item_id '
1419         ||'          AND msi.organization_id = nvl(ffe.organization_id,msi.organization_id) '
1420         ||'          AND inventory_item_id = ffe.inventory_item_id ) '
1421         ||' UNION ALL '
1422         ||' SELECT /*+ ORDERED USE_NL(gmd_recipe_validity_rules,fm_form_mst,mtl_system_items)*/ ffe.recipe_validity_rule_id, ffe.inventory_item_id, '
1423         ||' grb.formula_id, msi.organization_id, '
1424         ||' ffe.start_date, ffe.end_date, ffe.inv_min_qty, '
1425         ||' ffe.inv_max_qty, ffe.preference, msi.primary_uom_code, '
1426         ||' mp.organization_code wcode , to_number(null) , '
1427         ||' NULL, to_number(null), NULL, '
1428         ||' NULL, to_number(null), to_number(null) prd_fct, -1 prd_ind, ' /*B2870041*/
1429         ||' grb.recipe_id, grb.recipe_no, grb.recipe_version ,'
1430         ||' 0 rhdr_loc, '
1431 /* NAMIT_CR,SGIDUGU */
1432         ||' 0 calculate_step_quantity '
1433         ||' FROM  gmd_recipes_b'||at_apps_link||' grb,'
1434         ||'       gmd_recipe_validity_rules'||at_apps_link||' ffe,'
1435         ||'       mtl_parameters'||at_apps_link||' mp, '
1436         ||'       fm_form_mst'||at_apps_link||' ffm, '
1437         ||'       mtl_system_items'||at_apps_link||' msi,'
1438         ||'       hr_organization_units'||at_apps_link||' hou,'
1439         ||'       gmd_status_b'||at_apps_link||' gs1,'
1440         ||'       gmd_status_b'||at_apps_link||' gs2,'
1441         ||'       gmd_status_b'||at_apps_link||' gs3 '
1442         ||' WHERE grb.delete_mark = 0 '
1443         ||'   AND grb.recipe_id = ffe.recipe_id '
1444         ||'   AND grb.recipe_status = gs1.status_code '
1445         ||'   AND gs1.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1446         ||'   AND gs1.delete_mark = 0 '
1447         ||'   AND ffe.delete_mark = 0 '
1448         ||'   AND ffe.validity_rule_status = gs2.status_code '
1449         ||'   AND gs2.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1450         ||'   AND gs2.delete_mark = 0 '
1451         ||'   AND ffm.delete_mark = 0 '
1452         ||'   AND nvl(hou.date_to,SYSDATE) >= SYSDATE '
1453         ||'   AND hou.organization_id = mp.organization_id '
1454         ||'   AND ffm.inactive_ind = 0 '
1455         ||'   AND grb.routing_id IS NULL '
1456         ||'   AND ffe.organization_id IS NULL '
1457         ||'   AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
1458         ||'   AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE '
1459         ||'   AND grb.formula_id = ffm.formula_id '
1460         ||'   AND ffm.formula_status = gs3.status_code '
1461         ||'   AND gs3.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1462         ||'   AND gs3.delete_mark = 0 '
1463         ||'   AND msi.organization_id = mp.organization_id '
1464         ||'   AND mp.process_enabled_flag = ''Y'' '  ;
1465      IF l_in_str_org  IS NOT NULL THEN
1466          effectivity_cursor := effectivity_cursor
1467         ||'   AND msi.organization_id ' || l_in_str_org ;
1468      END IF;
1469 
1470          effectivity_cursor := effectivity_cursor
1471 	   ||'   AND msi.inventory_item_id = ffe.inventory_item_id '
1472 	||'   AND msi.recipe_enabled_flag = ''Y'' '
1473 	||'   AND msi.process_execution_enabled_flag = ''Y'' '
1474 /*B5161655 - Changed the where clause to pick up even when the formula belongs to a differnt organization
1475 from the validity rules */
1476         ||'   AND EXISTS ( SELECT /*+DRIVING_SITE(FM_MATL_DTL)*/ 1 '
1477         ||'          FROM  fm_matl_dtl'||at_apps_link||' '
1478         ||'          WHERE formula_id = grb.formula_id '
1479         ||'          AND line_type = 1 '
1480         ||'          AND inventory_item_id = msi.inventory_item_id '
1481         ||'          AND msi.organization_id = nvl(ffe.organization_id,msi.organization_id) '
1482         ||'          AND inventory_item_id = ffe.inventory_item_id )  ) eff,'
1483         ||'( SELECT /*+DRIVING_SITE(gmp_form_eff)*/ organization_id, fmeff_id, '
1484         ||'             max(aps_fmeff_id) aps_fmeff_id '
1485         ||'             FROM gmp_form_eff'||at_apps_link||' '
1486         ||'      WHERE organization_id is NOT NULL '
1487         ||'      GROUP BY organization_id, fmeff_id '
1488         ||'    ) gfe, '
1489          -- B4918786 (RDP) SDS Changes
1490         ||' (SELECT /*+ DRIVING_SITE(mic) DRIVING_SITE(cat) */ mic.category_concat_segs, '
1491         ||'         mic.category_id, '
1492         ||'         mic.organization_id,'
1493         ||'         mic.inventory_item_id '
1494         ||'    FROM mtl_item_categories_v'||at_apps_link|| ' mic, '
1495         ||'         mtl_default_category_sets_fk_v'||at_apps_link|| ' cat '
1496         ||'   WHERE mic.category_set_id = cat.category_set_id '
1497         ||'     AND cat.functional_area_id = 14 '
1498         ||' ) scat '
1499         ||'WHERE eff.organization_id = gfe.organization_id (+) '
1500         ||' AND (eff.organization_id IS NULL OR eff.organization_id ' || l_in_str_org ||')'
1501         ---#6358324 KBANDDYO restricting the collections from collecting unwanted inv_organizations
1502         ||' AND eff.recipe_validity_rule_id = gfe.fmeff_id (+) '
1503         ||' AND eff.inventory_item_id = scat.inventory_item_id (+) '
1504         ||' AND eff.organization_id = scat.organization_id (+)'
1505 	      ||' ORDER BY 4,5  ' ;
1506 
1507     gmp_debug_message('Started at '|| TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS'));
1508     gmp_debug_message('effectivity_cursor -'||effectivity_cursor);
1509 
1510     formula_hdr_cursor :=
1511         ' SELECT unique ffm.formula_id, 0, 0, 0, -1, NULL '
1512         ||' FROM fm_form_mst'||at_apps_link||' ffm, '
1513         ||'      gmd_recipes_b'||at_apps_link||' grb, '
1514         ||'      gmd_recipe_validity_rules'||at_apps_link||' ffe, '
1515         ||'      hr_organization_units'||at_apps_link||' hou, '
1516         ||'      gmd_status_b'||at_apps_link||' gs '
1517         ||' WHERE grb.recipe_id = ffe.recipe_id '
1518         ||'   AND ffe.validity_rule_status = gs.status_code '
1519         ||'   AND ( ffe.organization_id is NULL or ffe.organization_id = hou.organization_id)'
1520    ----B#6358324 KBANDDYO restricting the collections from collecting unwanted inv_organizations
1521    ----B#6489338 Added the next 1 conditions as below
1522         ||'   AND  hou.organization_id '|| l_in_str_org
1523         ||'   AND gs.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
1524         ||'   AND gs.delete_mark = 0 '
1525         ||'   AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE '
1526         ||'   AND ffm.formula_id = grb.formula_id '
1527         ||'   AND ffe.delete_mark = 0 '
1528         ||'   AND ffm.delete_mark = 0 '
1529         ||' ORDER BY formula_id  ' ;
1530 
1531     gmp_debug_message('formula_hdr_cursor -'||formula_hdr_cursor);
1532     OPEN cur_formula_hdr FOR formula_hdr_cursor;
1533     FETCH cur_formula_hdr BULK COLLECT INTO formula_header_tab;
1534     formula_headers_size := formula_header_tab.COUNT;
1535 
1536 /* PennColor Bug: 8230710
1537     LOOP
1538       FETCH cur_formula_hdr INTO formula_header_tab(formula_headers_size);
1539       EXIT WHEN cur_formula_hdr%NOTFOUND;
1540       formula_headers_size := formula_headers_size + 1;
1541     END LOOP;
1542 */
1543     CLOSE cur_formula_hdr;
1544    -- formula_headers_size := formula_headers_size -1 ;
1545     time_stamp;
1546     log_message('Formula Header size is = ' || to_char(formula_headers_size)) ;
1547 
1548 /*B2870041 added the original um from the line and the primary um of the item
1549 but in the OPM uom format. We will use this later for conversions */
1550     /* B2657068 Rajesh Patangya */
1551     /* B2954076 Rajesh Patangya, cursor modified */
1552 
1553 -- =========== Formula detail selection,item substitution processing start ========================
1554 /* Logic for item substitution - The formula details along if any substitutes defined for the ingredients
1555 are fetched from GMD view - gmd_material_effectivities_vw. The query also fetches the lead preference
1556 (immediate next row prefernce), lead start date and lead end dates for each susbtitute defined for the
1557 ingredient. A flag - original_item_flag to determine whether the item is an original item or substitue
1558 item is used in processing each record. The logic primarliy does 4 functions -
1559                 - Fill the leading spaces with the origial item whereever needed
1560                 - Fill in the gaps with the original item where ever applicable
1561                 - Change the validity start/end dates of the substitue items based on their preference
1562                 - Fill in the gaps with the susbstute items whereever applicable
1563 A flag "ae_flag" which corresponds to the actual end date flag is set to fill the gaps with the substitue
1564 items whereever applicable. To understand this, lets take an simple example where ingredient say "A" in
1565 a formula is replaced with
1566                     ----------------------------------------------------
1567                     ingredient |        validity period    | preference |
1568                                | Start date  |  Enddate    |            |
1569                     -----------------------------------------------------
1570                  - B          | 14/06/2006  |    -        |    2       |
1571                  - C          | 02/08/2006  | 15/10/2006  |    1       |
1572                    ------------------------------------------------------
1573 In this case there will be 3 rows based on preference
1574                  - B from 14/06/2006 to 02/08/2006,
1575                  - C from 02/08/2006 to 15/10/2006, and
1576                  - B from 15/10/2006 to -            (then remaining period)
1577 So the ae_flag is set for the substitute "B" to track that this substitute is to be replaced after
1578 15/10/2006.
1579 And then for each ingredient/substitute their effective date and disbale date are passed on to
1580 msc_st_bom_components table with a component sequence id generated uniquely for the substitute items
1581 alone*/
1582 
1583 
1584     /*B5176291 - initialise the tables - start*/
1585     prev_detail_tab(1) := NULL;
1586     orig_detail_tab(1) := NULL;
1587     ae_date := NULL;
1588     formula_details_size := 0;
1589 
1590     /*Fetch current value of formula_line id into a global value . Maintiain a
1591     global counter that is incremented everytime */
1592 
1593      v_gmd_seq := 'SELECT MAX(formulaline_id) FROM fm_matl_dtl'||at_apps_link ;
1594      EXECUTE IMMEDIATE v_gmd_seq INTO v_gmd_formula_lineid ;
1595 
1596     /*B5176291 - initialise the tables - end*/
1597     /*B547601 added order original_item_flag in partition stmt */
1598     /* bug: 6087535 Vpedarla 23-07-07 FP :11.5.10 - 12.0.3 : ITEM SUBSTITUTION EFFECTIVITY IS NOT COLLECTED.
1599     Changed the formula_dtl_cursor and later processing for better item substitution funtionality.
1600     */
1601     -- Bug # 14196938 vkinduri, code change starts
1602     Begin
1603     V_process_Org :=  ' SELECT count(*) from mtl_parameters'||At_Apps_Link
1604                     ||' WHERE organization_id' || L_In_Str_Org
1605                     ||' AND process_enabled_flag = ''Y'' ';
1606     gmp_debug_message('V_process_Org -'||V_process_Org);
1607     EXECUTE IMMEDIATE V_process_Org Into l_process_org_cnt ;
1608     IF l_process_org_cnt = 0 Then
1609     Raise Invalid_String_Value;
1610     END IF ;
1611     Exception
1612      WHEN invalid_string_value THEN
1613       Log_Message('Organization string is Invalid or not a process organization');
1614       --Raise_Application_Error (-20001,'Organization string is Invalid or not a process organization');
1615       --Return_Status := False;
1616       return;
1617     END;
1618     -- Bug # 14196938 vkinduri, code change ends
1619     formula_dtl_cursor :=
1620          '  SELECT ffm.formula_id, '
1621        ||'  ffm.formula_no, '
1622        ||'  ffm.formula_vers, '
1623        ||'  ffm.formula_desc1, '
1624        ||'  ((fmd.formulaline_id * 2) + 1) x_formulaline_id, '
1625        ||'  fmd.line_type, '
1626        ||'  fmd.item_id inventory_item_id, '
1627        ||'  decode(fmd.original_item_flag,1,fmd.qty,(( fmd.sub_replace_qty / fmd.sub_original_qty) * fmd.line_item_qty)) formula_qty, '
1628        /*B5176291 - for substitute items fmd.qty will be null, in those case subsittute qty should be used*/
1629        ||'  fmd.scrap_factor , '
1630        ||'  fmd.scale_type, '
1631        ||'  fmd.contribute_yield_ind, '
1632                    ||'  decode(fmd.line_type, -1, decode(nvl(fmd.contribute_step_qty_ind, '''||'N'||''''||'),'    -- venu
1633                    ||    ''''||'Y'||''''||',1,2), 1) contribute_step_qty_ind,'                                    -- venu
1634        ||'  DECODE(fmd.phantom_type,0,null,6) phantom_type, '
1635        ||'  msi.primary_uom_code, '  -- venu
1636        ||'  fmd.item_um detail_uom, '  -- venu
1637   -- Bug: 7348022 Vpedarla changed below line of code
1638        ||'  DECODE(fmd.scale_type,2,4,fmd.scale_type) bom_scale_type, '
1639   --       ||'  DECODE(fmd.scale_type,0,0,1,2) bom_scale_type, '
1640        ||'  DECODE(fmd.item_um,msi.primary_uom_code,decode(fmd.original_item_flag,1,fmd.qty,((fmd.sub_replace_qty / fmd.sub_original_qty) * fmd.line_item_qty)), '
1641        ||'        inv_convert.inv_um_convert'||at_apps_link
1642        ||'                  ( fmd.item_id, '
1643        ||'                   NULL,msi.organization_id, '
1644       /* bug: 6918852 Vpedarla 04-Apr-2008  used the global variable for precision*/
1645        /* ||'                   NULL, '  */
1646         ||                    conv_precision || ' , '
1647        ||'                   decode(fmd.original_item_flag,1,fmd.qty,((fmd.sub_replace_qty / fmd.sub_original_qty) * fmd.line_item_qty)), '
1648        ||'                   fmd.item_um , '
1649        ||'                   msi.primary_uom_code , '
1650        ||'                   NULL ,NULL )) primary_qty, '
1651        ||'  DECODE(fmd.item_um,msi.primary_uom_code, decode(fmd.original_item_flag,1,fmd.scale_multiple,((fmd.sub_replace_qty / fmd.sub_original_qty) * fmd.line_item_qty)), '
1652        ||'        inv_convert.inv_um_convert'||at_apps_link
1653        ||'                  ( fmd.item_id, '
1654        ||'                   NULL,msi.organization_id, '
1655         ||                    conv_precision || ' , '
1656        ||'                   decode(fmd.original_item_flag,1,fmd.scale_multiple,((fmd.sub_replace_qty / fmd.sub_original_qty) * fmd.line_item_qty)), '
1657        ||'                   fmd.item_um , '
1658        ||'                   msi.primary_uom_code , '
1659        ||'                   NULL ,NULL )) scale_multiple, '
1660      /*  ||'  fmd.scale_multiple, '  */ /* Bug 8529867 Vpedarla */
1661        ||'  (fmd.scale_rounding_variance * 100) scale_rounding_variance, '   -- venu multipied it by 100
1662        ||'  decode(fmd.rounding_direction,1,2,2,1,fmd.rounding_direction) ,'
1663        ||'  fmd.release_type, '
1664        ||'  fmd.original_item_flag, '
1665        ||'  fmd.start_date, '
1666        ||'  fmd.end_date, '
1667        ||'  fmd.formulaline_id formula_line_id , '
1668        ||'  fmd.preference  '
1669 --       ||'  null actual_end_date ,'
1670 --       ||'  0 actual_end_flag '
1671        ||'  FROM  gmd_material_effectivities_vw'||at_apps_link||' fmd,'
1672        ||'        fm_form_mst'||at_apps_link||' ffm, '
1673        ||'        mtl_system_items'||at_apps_link||' msi '
1674        ||'  WHERE msi.inventory_item_id = fmd.item_id  '
1675        ||'  AND msi.organization_id = fmd.organization_id '
1676        ||'  AND ffm.formula_id = fmd.formula_id '
1677        ||'  AND ffm.formula_id IN ( select /*+ DRIVING_SITE(grb) DRIVING_SITE(ffe) DRIVING_SITE(gs) DRIVING_SITE(hou) */ unique grb.formula_id '
1678 -- #6358324 KBANDDYO restricting the collections from collecting unwanted inv_organizations
1679        ||'                  FROM  gmd_recipes_b'||at_apps_link ||' grb,  '
1680        ||'                      gmd_recipe_validity_rules'||at_apps_link ||'  ffe, '
1681        ||'                      hr_organization_units'||at_apps_link ||'  hou, '
1682        ||'                      gmd_status_b'||at_apps_link ||' gs '
1683        ||'                  WHERE grb.recipe_id = ffe.recipe_id '
1684     ----B#6489338 Added the next 3 conditions as below and commented the above clause
1685        ||'                  AND ( ffe.organization_id is NULL or ffe.organization_id = hou.organization_id )'
1686        ||'                  AND  hou.organization_id '|| l_in_str_org
1687        ||'                  AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
1688        ||'                  AND ffe.validity_rule_status = gs.status_code    '
1689        ||'                  AND gs.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ')'
1690        ||'                  AND gs.delete_mark = 0   '
1691        ||'                  AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE   '
1692        ||'                  AND ffe.delete_mark = 0 )'
1693        ||'  AND ffm.delete_mark = 0 '
1694        ----B#6489338 Added the next condition as below
1695        --||'  AND msi.organization_id '|| l_in_str_org --vkinduri, Bug # 14837859
1696        ||'  AND nvl(fmd.qty,fmd.sub_replace_qty)  <> 0'
1697        ||'  AND ( fmd.qty <> 0 OR (( fmd.sub_replace_qty / fmd.sub_original_qty) * fmd.line_item_qty) <> 0) '
1698        ||'  ORDER BY ffm.formula_id ,fmd.line_type, fmd.formulaline_id, ' /* PennColor 13Feb fmd.line_no, vpedarla Bug: 7652265 */
1699        ||'  fmd.original_item_flag desc,fmd.start_date,fmd.preference ';
1700 
1701     gmp_debug_message('formula_dtl_cursor -'||formula_dtl_cursor);
1702 	OPEN cur_formula_dtl FOR formula_dtl_cursor;
1703 	-- { L1
1704 	LOOP
1705 	FETCH cur_formula_dtl INTO temp_detail_tab(1);
1706 
1707 	-- Bug 5955251 Vpedarla 29-Mar-2007  COLLECTION ISSUE CREATES DUMMY CO-PRODUCT CREATION IN ODS
1708 	EXIT WHEN cur_formula_dtl%NOTFOUND;
1709 	--end of bug 5955251
1710 	   -- { 1
1711 	   IF  (temp_detail_tab(1).original_item_flag = 1 ) THEN         /* If original item */
1712 	     -- { 2
1713        --  log_message ('In Loop 2');
1714 	     IF substcount <> 0 THEN              --processing the previous original item's substituion records
1715 	        FOR i in 1..substcount
1716 	        -- { L2
1717 	        loop
1718 	           -- { 3
1719            --     log_message ( 'In Loop 3' );
1720 	           IF (subst_tab(i).start_date <= NVL(subst_tab(i).end_date,subst_tab(i).start_date)) THEN
1721 	              FOR j in 1..substcount
1722 	              -- { L3
1723 	              LOOP
1724 	                  -- { 4
1725              --   log_message ( 'In Loop 4' );
1726  	                  IF ((I <> J) and
1727 	                      (subst_tab(j).start_date <= NVL(subst_tab(j).end_date,subst_tab(j).start_date))) THEN
1728 	                        -- { 5
1729                -- log_message ( 'In Loop 5' );
1730 
1731 	                        IF (subst_tab(i).preference < subst_tab(j).preference) THEN
1732 	                             -- { 6
1733                   --              log_message ( 'In Loop 6' );
1734 
1735 	                             IF ((subst_tab(j).start_date >= subst_tab(i).start_date) and
1736 	                                  (subst_tab(i).end_date IS NULL)) THEN
1737 		       		                subst_tab(j).end_date := (subst_tab(j).start_date - 1/1440) ;
1738                     --               log_message ( 'In Loop 6-1' );
1739 
1740 	                             ELSIF ((subst_tab(j).start_date < subst_tab(i).start_date) and
1741 	                                      (subst_tab(i).end_date IS NULL) AND
1742 		                                ((subst_tab(j).end_date >= subst_tab(i).start_date) OR
1743 		                                (subst_tab(j).end_date IS NULL))) THEN
1744 	       		                     subst_tab(j).end_date := (subst_tab(i).start_date - 1/1440) ;
1745                       --             log_message ( 'In Loop 6-2' );
1746 
1747 	                             ELSIF ((subst_tab(j).start_date < subst_tab(i).end_date) and
1748 	                                    (subst_tab(j).end_date <= subst_tab(i).end_date) and
1749 	                                    (subst_tab(i).start_date <= subst_tab(j).start_date)) THEN
1750 	                                subst_tab(j).end_date := (subst_tab(j).start_date - 1/1440) ;
1751                         --           log_message ( 'In Loop 6-3' );
1752 
1753 	                             ELSIF (subst_tab(j).start_date > subst_tab(i).start_date)  and
1754 	                                  (subst_tab(j).start_date < subst_tab(i).end_date)  and
1755 	                                   (subst_tab(j).end_date > subst_tab(i).end_date) THEN
1756 		                         	   subst_tab(j).start_date := subst_tab(i).end_date + 1/1440 ;
1757                           --         log_message ( 'In Loop 6-4' );
1758 
1759 	                             ELSIF (subst_tab(i).start_date > subst_tab(j).start_date)  and
1760 	                                    (subst_tab(i).start_date < subst_tab(j).end_date)  and
1761 	                                    (subst_tab(i).end_date > subst_tab(j).end_date)  THEN
1762 	                        		 subst_tab(j).end_date := subst_tab(i).start_date - 1/1440 ;
1763                          --          log_message ( 'In Loop 6-5' );
1764 
1765                                 --Swapna Bug#5975883 below eslif condition was added
1766 	                             ELSIF(subst_tab(i).start_date <= subst_tab(j).start_date) and
1767                                       (subst_tab(i).end_date is NOT NULL) and (subst_tab(j).start_date < subst_tab(i).end_date) and
1768 	                	                 (subst_tab(j).end_date is NULL) THEN
1769 	                                 subst_tab(j).start_date := subst_tab(i).end_date + 1/1440;
1770                          --          log_message ( 'In Loop 6-6' );
1771 
1772 	                             ELSIF(subst_tab(i).start_date > subst_tab(j).start_date)  and
1773 	                	                 (subst_tab(j).end_date is NULL)
1774 	                	                 and (subst_tab(i).end_date is NOT NULL) THEN
1775 	                                 subst_tab(j).end_date := subst_tab(i).start_date - 1/1440;
1776 	                                 substcount := substcount + 1;	--Swapna Bug#5975883
1777 	                                 subst_tab(substcount) := subst_tab(j);
1778 	                           		 subst_tab(substcount).start_date := subst_tab(i).end_date +1/1440 ;
1779 	                                 subst_tab(substcount).end_date := NULL;
1780                           --         log_message ( 'In Loop 6-7' );
1781 
1782 	                             ELSIF (subst_tab(i).start_date > subst_tab(j).start_date) and
1783 		                               (subst_tab(i).end_date < subst_tab(j).end_date) THEN
1784 	                                 substcount := substcount + 1;
1785 	                                 subst_tab(substcount) := subst_tab(j);
1786 	                                 subst_tab(substcount).start_date := subst_tab(i).end_date +1/1440 ;
1787 	                                 subst_tab(substcount).end_date := subst_tab(j).end_date;
1788 	                                 subst_tab(j).end_date := subst_tab(i).start_date - 1/1440 ;
1789                           --         log_message ( 'In Loop 6-8' );
1790 
1791 	                            END IF;
1792 	                            -- } 6
1793 	                        END IF;
1794 	                        -- } 5
1795 	                   END IF;
1796 	                   -- } 4
1797 	                END LOOP;
1798 	                -- } L3
1799 	            END IF;
1800 	            -- } 3
1801 	        END LOOP;
1802 	        -- } L2
1803 	      /* insert processed substitutes now */
1804 	        FOR k in 1..substcount
1805 	        LOOP
1806 	           formula_details_size := formula_details_size + 1 ;
1807 	           formula_detail_tab(formula_details_size) := subst_tab(k) ;
1808 	           formula_detail_tab(formula_details_size).x_formulaline_id := NULL;
1809 	        END LOOP;
1810 	        /*insert trailing records if there is no substitue which has a null end date*/
1811 	        enddatenull := FALSE; -- Bug: 6030499 Vpedarla forward port of 11.5.9 bug 6047372.
1812       FOR k in 1..substcount
1813 	        LOOP
1814 	        If subst_tab(k).end_date is null then
1815 	           enddatenull := TRUE;
1816 	        ELSIF subst_tab(k).end_date > NVL(orig_detail_tab(1).start_date,sysdate) THEN
1817 	           orig_detail_tab(1).start_date:= subst_tab(k).end_date + 1/1440 ;
1818 	        END IF;
1819 	     END LOOP;
1820 	     IF NOT(enddatenull) THEN
1821 	        formula_details_size := formula_details_size + 1 ;
1822 	        formula_detail_tab(formula_details_size) := orig_detail_tab(1) ;
1823 	        formula_detail_tab(formula_details_size).inventory_item_id := orig_detail_tab(1).inventory_item_id;
1824 	        formula_detail_tab(formula_details_size).x_formulaline_id := NULL;
1825 	        formula_detail_tab(formula_details_size).end_date:=NULL;
1826 	     END IF;
1827 	  END IF; /* end of substitute record processing */
1828 	  -- } 2 substcount <> 0
1829 	     orig_detail_tab(1) := temp_detail_tab(1) ;
1830 		 prev_detail_tab(1) := temp_detail_tab(1) ;
1831 	        /* This is to insert original item */
1832 	     IF  nvl(temp_detail_tab(1).end_date,(SYSDATE +1)) > sysdate THEN
1833 	        formula_details_size := formula_details_size + 1 ;
1834 		    formula_detail_tab(formula_details_size) := temp_detail_tab (1) ;
1835 		    formula_detail_tab(formula_details_size).end_date := temp_detail_tab(1).end_date - 1/1440 ;
1836 	      END IF ;
1837 	        substcount := 0;
1838 	        orig_start_date:=orig_detail_tab(1).end_date;
1839 	   -- } IF of 1
1840 	   ELSE /* ELSE of original item - not an original item */
1841 	   -- { ELSE of 1
1842 	       IF substcount > 0 THEN /* from the second record onwards */
1843 	       /* comparing with prevoious record to check if there is any gap so that we insert
1844 	          the original item record in the gap. */
1845 	          IF ( temp_detail_tab(1).start_date > orig_start_date ) and NOT(nullenddatefound) THEN
1846 	         /* store the previous record' end date */
1847 	               substcount := substcount + 1 ;
1848 	               subst_tab(substcount) := orig_detail_tab(1) ;
1849 	               subst_tab(substcount).inventory_item_id := orig_detail_tab(1).inventory_item_id;
1850 	               subst_tab(substcount).x_formulaline_id := NULL;
1851 	               subst_tab(substcount).start_date := orig_start_date;
1852 	               subst_tab(substcount).end_date := temp_detail_tab (1).start_date - 1/1440;
1853 	               subst_tab(substcount).preference:=99999;
1854 	           END IF;
1855 	       END IF;
1856 	       substcount := substcount + 1 ;
1857 	       subst_tab(substcount) := temp_detail_tab (1) ;
1858 	       IF (temp_detail_tab (1).end_date IS NULL) AND NOT (nullenddatefound)  THEN
1859 	          nullenddatefound := TRUE;
1860 	       ELSIF (temp_detail_tab(1).end_date > orig_start_date ) AND NOT (nullenddatefound) THEN
1861 	          orig_start_date := temp_detail_tab(1).end_date +1/1440;
1862 	       END IF;
1863 
1864 	   END IF;
1865 	   -- }  1
1866 	EXIT WHEN cur_formula_dtl%NOTFOUND;
1867 	END LOOP;
1868 	-- } L1
1869 CLOSE cur_formula_dtl;
1870 
1871  -- IF (l_debug = 'Y') THEN
1872 --  j := 1 ;
1873 --  FOR j IN 1..formula_details_size
1874 --  LOOP
1875 --log_message (
1876 --formula_detail_tab(j).formula_id  || '*' ||
1877 --formula_detail_tab(j).formula_no || '*' ||
1878 --formula_detail_tab(j).formula_vers || '*' ||
1879 -- formula_detail_tab(j).formula_desc1  || '*' ||
1880 --formula_detail_tab(j).x_formulaline_id || '*' ||
1881 --formula_detail_tab(j).line_type  || '*' ||
1882 --formula_detail_tab(j).inventory_item_id  || '*' ||
1883 --formula_detail_tab(j).formula_qty  || '*' ||
1884 -- formula_detail_tab(j).scrap_factor || '*' ||
1885 -- formula_detail_tab(j).scale_type || '*' ||
1886 --formula_detail_tab(j).contribute_yield_ind || '*' ||
1887 --formula_detail_tab(j).contribute_step_qty_ind || '*' ||
1888 --formula_detail_tab(j).phantom_type  || '*' ||
1889 --formula_detail_tab(j).primary_uom_code || '*' ||
1890 --formula_detail_tab(j).detail_uom || '*' ||
1891 --formula_detail_tab(j).bom_scale_type || '*' ||
1892 --formula_detail_tab(j).primary_qty  || '*' ||
1893 --formula_detail_tab(j).scale_multiple || '*' ||
1894 --formula_detail_tab(j).scale_rounding_variance  || '*' ||
1895 --formula_detail_tab(j).rounding_direction  || '*' ||
1896 --formula_detail_tab(j).release_type || '*' ||
1897 --formula_detail_tab(j).original_item_flag  || '*' ||
1898 --TO_CHAR(formula_detail_tab(j).start_date ,'DD-MON-YYYY HH24:MI:SS') || '*' ||
1899 --TO_CHAR(formula_detail_tab(j).end_date ,'DD-MON-YYYY HH24:MI:SS') || '*' ||
1900 --formula_detail_tab(j).formula_line_id  ) ;
1901 -- || '*' || formula_detail_tab(j).preference );
1902 --  END LOOP ;
1903 --  J := 1;
1904  -- END IF;
1905 
1906     time_stamp ;
1907     log_message('Formula detail size is = ' || to_char(formula_details_size)) ;
1908 -- =========== Formula detail selection,item substitution processing end ========================
1909 
1910 -- =========== rtg offset data selection start ========================
1911    rtg_offset_cur_stmt := ' SELECT '||
1912 		' gro.organization_id, '||
1913 		' gro.fmeff_id, '||
1914 		' gro.formula_id, '||
1915 		' gro.routingstep_id, '||
1916 		' gro.start_offset, '||
1917 		' gro.end_offset, '||
1918 		' (rsm.formulaline_id *2 )+ 1'||
1919 		' FROM '||
1920 		'      gmd_recipe_step_materials'||at_apps_link||' rsm, '||
1921 		'      gmp_routing_offsets'||at_apps_link||' gro '||
1922 		' WHERE '||
1923 		'       gro.recipe_id = rsm.recipe_id '||
1924 		'   AND gro.routingstep_id = rsm.routingstep_id '||
1925 		' ORDER BY gro.formula_id,gro.organization_id, rsm.formulaline_id ' ;
1926 
1927     gmp_debug_message('rtg_offset_cur_stmt -'||rtg_offset_cur_stmt);
1928 
1929     OPEN cur_rtg_offsets  FOR rtg_offset_cur_stmt ;
1930     FETCH cur_rtg_offsets BULK COLLECT INTO rstep_offsets;
1931 
1932     rtg_offsets_size := rstep_offsets.COUNT;
1933 
1934 /* PennColor Bug: 8230710
1935     LOOP
1936       FETCH cur_rtg_offsets INTO rstep_offsets(rtg_offsets_size);
1937       EXIT WHEN cur_rtg_offsets%NOTFOUND;
1938 
1939       rtg_offsets_size := rtg_offsets_size + 1;
1940     END LOOP;
1941 */
1942 
1943     CLOSE cur_rtg_offsets;
1944 
1945  --   rtg_offsets_size := rtg_offsets_size -1 ;
1946     time_stamp ;
1947     log_message('Routing Offsets size is = ' || to_char(rtg_offsets_size)) ;
1948 
1949 -- =========== rtg offset data selection end ========================
1950 
1951 -- Vpedarla 7391495
1952 -- =========== rtg operation lead time selection start ========================
1953    oper_lead_time_cur_stmt := ' SELECT '||
1954                 ' gro.organization_id, '||
1955                 ' gro.fmeff_id, '||
1956                 ' gro.formula_id, '||
1957                 ' gro.routing_id, '||
1958                 ' gro.routingstep_id, '||
1959                 ' gro.start_offset, '||
1960                 ' gro.end_offset '||
1961                 ' FROM gmp_routing_offsets'||at_apps_link||' gro '||
1962                 ' ORDER BY gro.fmeff_id, gro.organization_id , gro.routingstep_id ' ;
1963 
1964     gmp_debug_message('oper_lead_time_cur_stmt -'||oper_lead_time_cur_stmt);
1965   oper_leadtime_size := 1;
1966     OPEN cur_oper_lead_time  FOR oper_lead_time_cur_stmt ;
1967     FETCH cur_oper_lead_time BULK COLLECT INTO oper_leadtime_percent;
1968     oper_leadtime_size := oper_leadtime_percent.COUNT;
1969 /*  PennColor Bug: 8230710
1970     LOOP
1971       FETCH cur_oper_lead_time INTO oper_leadtime_percent(oper_leadtime_size);
1972       EXIT WHEN cur_oper_lead_time%NOTFOUND;
1973         oper_leadtime_size := oper_leadtime_size + 1;
1974     END LOOP;
1975 */
1976     CLOSE cur_oper_lead_time;
1977 
1978    -- oper_leadtime_size := oper_leadtime_size -1 ;
1979     time_stamp ;
1980     log_message('Routing operations Lead time size is = ' || to_char(oper_leadtime_size)) ;
1981 
1982 -- =========== rtg operation lead time selection end ========================
1983 
1984 
1985 
1986     -- Validate formula for uom conversion, for planned items
1987     validate_formula ;
1988 
1989     routing_hdr_cursor :=
1990                      ' SELECT unique frh.routing_id, mp.organization_id, '
1991 /* NAMIT_CR 2 more zeros added for Linking Step Dependency to Routing Header */
1992                    ||'        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 '
1993                    ||' FROM mtl_parameters'||at_apps_link||' mp, '
1994                    ||'      fm_rout_hdr'||at_apps_link||' frh, '
1995                    ||'      gmd_recipes_b'||at_apps_link||' grb, '
1996                    ||'      gmd_recipe_validity_rules'||at_apps_link||' ffe, '
1997 		   ||'      hr_organization_units'||at_apps_link||' hou,'
1998                    ||'      gmd_status_b'||at_apps_link||' gs '
1999                    ||' WHERE grb.recipe_id = ffe.recipe_id '
2000                    ||'   AND ffe.validity_rule_status = gs.status_code '
2001                    ||'   AND gs.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ',' ||'''400'''|| ') '
2002                    ||'   AND gs.delete_mark = 0 '
2003                    ||'   AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE '
2004                    ||'   AND frh.routing_id = grb.routing_id '
2005                    ||'   AND nvl(hou.date_to,SYSDATE) >= SYSDATE '
2006                    ||'   AND hou.organization_id = mp.organization_id '
2007                    ||'   AND nvl(ffe.organization_id, mp.organization_id) = mp.organization_id'
2008 		   ||'   AND mp.process_enabled_flag = ''Y''' ;
2009             IF l_in_str_org  IS NOT NULL THEN
2010                routing_hdr_cursor := routing_hdr_cursor
2011 	           ||' AND mp.organization_id ' || l_in_str_org ;
2012             END IF;
2013 
2014          routing_hdr_cursor := routing_hdr_cursor
2015                    ||'   AND ffe.delete_mark = 0 '
2016                    ||'   AND frh.delete_mark = 0 '
2017                    ||'   AND frh.inactive_ind = 0 '
2018                    ||' ORDER BY frh.routing_id, mp.organization_id ' ;
2019 
2020     gmp_debug_message('routing_hdr_cursor -'||routing_hdr_cursor);
2021 
2022     OPEN cur_routing_hdr FOR routing_hdr_cursor;
2023     LOOP
2024       FETCH cur_routing_hdr INTO rtg_org_hdr_tab(routing_headers_size);
2025       EXIT WHEN cur_routing_hdr%NOTFOUND;
2026       routing_headers_size := routing_headers_size + 1;
2027     END LOOP;
2028     CLOSE cur_routing_hdr;
2029     routing_headers_size := routing_headers_size -1 ;
2030     time_stamp ;
2031     log_message('Routing Header size is = ' || to_char(routing_headers_size)) ;
2032     /* 2582849 minimum_transfer_qty selected */
2033 
2034     /*  Select All the Sequence Dependent Changeover for oprn_id <> -1 */
2035    setup_id_cursor :=
2036           ' SELECT oprn_id, '
2037               ||'  category_id,   '
2038               ||'  seq_dep_id   '
2039               ||'  FROM  gmp_sequence_types'||at_apps_link||' gst  '
2040               ||'  WHERE oprn_id <> -1  '
2041               ||'  ORDER BY oprn_id,category_id  ' ;
2042 
2043      setup_size := 1;
2044      OPEN setup_id_dtl FOR setup_id_cursor;
2045      LOOP
2046         FETCH setup_id_dtl INTO setupid_tab(setup_size);
2047         EXIT WHEN setup_id_dtl%NOTFOUND;
2048         setup_size := setup_size + 1;
2049      END LOOP;
2050      CLOSE setup_id_dtl;
2051      setup_size := setup_size - 1;
2052      time_stamp ;
2053      log_message('Sequence Dependent Changeover size is = ' || to_char(setup_size)) ;
2054 
2055     routing_dtl_cursor :=
2056           ' SELECT /*+ DRIVING_SITE(crd) DRIVING_SITE(frd) DRIVING_SITE(fom) DRIVING_SITE(goa) DRIVING_SITE(gor) */ frd.routing_id, '
2057               ||'  crd.organization_id, '
2058               ||'  frd.routingstep_no,  '
2059               ||'  NVL(goa.sequence_dependent_ind,0), '
2060               /* This will ensure that ordering will always have primary first */
2061               ||'  DECODE(gor.prim_rsrc_ind, 1,1,2,2,0,3), '
2062               ||'  gor.resources, '
2063               ||'  gor.prim_rsrc_ind, '
2064               ||'  decode(crd.capacity_constraint,1,1,2), '
2065               ||'  crd.min_capacity, '
2066               ||'  crd.max_capacity, '
2067               ||'  crd.schedule_ind, '
2068               ||'  frd.routingstep_id, '
2069               ||'  ((frd.routingstep_id * 2) + 1) x_routingstep_id,  '
2070               ||'  frd.step_qty, '
2071               ||'  NVL(frd.minimum_transfer_qty,0) minimum_transfer_qty, '
2072               ||'  fom.oprn_desc,  '
2073               ||'  fom.oprn_id,  '     /* SGIDUGU Seq Dep */
2074               ||'  fom.oprn_no,  '
2075               ||'  fom.process_qty_uom,'  -- akaruppa previously sou2.uom_code
2076               ||'  goa.activity, '
2077               ||'  goa.oprn_line_id, '
2078               ||'  gor.resource_count, '
2079               ||'  gor.resource_usage, '
2080               ||'  gor.resource_usage_uom, '  -- akaruppa previously gor.usage_um
2081               ||'  gor.scale_type,'
2082               ||'  goa.offset_interval, '
2083               ||'  crd.resource_id, '
2084               ||'  ((crd.resource_id * 2) + 1) x_resource_id, '
2085               ||'  DECODE(gor.scale_type,0,2,1,1,2,3) , ' /* B2967464 */
2086               ||'  goa.activity_factor, '
2087               ||'  gor.process_qty, '
2088               ||'  NVL(goa.material_ind,0), '
2089               ||'  1 , '
2090               ||'  SUM(NVL(goa.material_ind,0))  OVER (PARTITION BY '
2091               ||'  frd.routing_id, crd.organization_id, frd.routingstep_no) mat_found,'
2092               ||'  1, ' /* flag for including rows */
2093               ||'  decode(goa.break_ind,NULL,2,0,2,1,1) brk_ind'
2094               ||' ,-1, -1, -1, -1, -1, -1, '
2095          -- B4918786 (RDP) SDS
2096               ||' (SUM(DECODE(NVL(goa.sequence_dependent_ind,0),1,1,0)) OVER '
2097               ||' (PARTITION BY '
2098               ||' frd.routing_id, crd.organization_id)) is_sds_rout,'
2099               ||' DECODE(NVL(goa.sequence_dependent_ind,0),1,DECODE(gor.prim_rsrc_ind,1,1,0),0) is_unique, '
2100               ||' DECODE(NVL(goa.sequence_dependent_ind,0),1,0,DECODE(gor.prim_rsrc_ind,1,1,0)) is_nonunique, '
2101               ||' NULL setup_id '
2102               ||' FROM  cr_rsrc_dtl'||at_apps_link||' crd, '
2103               ||'       fm_rout_dtl'||at_apps_link||' frd, '
2104               ||'       gmd_operations'||at_apps_link||' fom, '
2105               ||'       gmd_operation_activities'||at_apps_link||' goa, '
2106               ||'       gmd_operation_resources'||at_apps_link||' gor, '
2107 	      ||'       hr_organization_units'||at_apps_link||' hou1, '
2108               ||'       mtl_units_of_measure'||at_apps_link||' mum, '
2109 /*sowmya added - operation process qty should be verified with the uom master*/
2110               ||'       mtl_units_of_measure'||at_apps_link||' mum2 '
2111 	   ----B#6489338 Added the next where condition as below
2112               ||' WHERE frd.routing_id in (  SELECT /*+ DRIVING_SITE(grb) DRIVING_SITE(ffe) DRIVING_SITE(gs) */ distinct routing_id '
2113               ||'            FROM  gmd_recipes'||at_apps_link ||' grb ,'
2114               ||'                  gmd_recipe_validity_rules'||at_apps_link ||' ffe ,'
2115               ||'                  hr_organization_units'||at_apps_link ||' hou ,'
2116               ||'                  gmd_status_b'||at_apps_link ||' gs '
2117               ||'            WHERE grb.recipe_id = ffe.recipe_id'
2118               ||'              AND ffe.validity_rule_status = gs.status_code '
2119               ||'              AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
2120               ||'              AND gs.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ')'
2121               ||'              AND gs.delete_mark = 0 '
2122               ||'              AND ( ffe.organization_id is NULL or ffe.organization_id = hou.organization_id )'
2123               ||'              AND hou.organization_id '|| l_in_str_org
2124               ||'              AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE  )'
2125 	      ||'   AND frd.oprn_id = fom.oprn_id '
2126               ||'   AND fom.oprn_id = goa.oprn_id '
2127               ||'   AND goa.oprn_line_id = gor.oprn_line_id '
2128 /* NAMIT_RD */
2129               ||'   AND crd.resources = gor.resources '
2130      ----B#6489338 Added the next 2 conditions as below
2131               ||'   AND hou1.organization_id '|| l_in_str_org
2132 	      ||'   AND crd.organization_id = hou1.organization_id'
2133               ||'   AND mum.uom_code = gor.resource_usage_uom '
2134               ||'   AND mum2.uom_code = fom.process_qty_uom ' --sowmya added
2135               ||'   AND fom.delete_mark = 0 '
2136               ||'   AND goa.activity_factor > 0 '
2137               ||'   AND mum.uom_class = :gmp_uom_class '
2138 /* NAMIT_RD */
2139               ||' ORDER BY  '
2140               ||'         1, 2, 3, 4, 5, 6 ';
2141 /*
2142               ||' ORDER BY frd.routing_id,  '
2143               ||'          crd.orgn_code,  '
2144               ||'          frd.routingstep_no, '
2145               ||'          NVL(goa.sequence_dependent_ind,0) DESC, '
2146               ||'          goa.offset_interval,'
2147               ||'          goa.activity, '
2148               ||'          goa.oprn_line_id, '
2149               ||'          gor.prim_rsrc_ind, '
2150               ||'          gor.resources '; */
2151 
2152     gmp_debug_message('routing_dtl_cursor -'||routing_dtl_cursor);
2153     OPEN cur_routing_dtl FOR routing_dtl_cursor USING g_gmp_uom_class;
2154     LOOP
2155       FETCH cur_routing_dtl INTO rtg_org_dtl_tab(rtg_org_dtl_size);
2156       EXIT WHEN cur_routing_dtl%NOTFOUND;
2157       /*B2870041 The activities have to be properly marked for the schedule
2158          flag. This only needs to be done once for the route. The index is
2159          copied for ease of use. */
2160 
2161       ri := rtg_org_dtl_size;
2162 
2163       /* since the select includes orgn_code we need to track when the route
2164          org or step changes. If any of them change this means the step has
2165          changed. when there is a new step the process needs to reset. The
2166          new values are saved and the first row of the step is saved to be
2167          used to loop later. found will be used to indicate that an activity
2168          has the material ind set to 1 */
2169 
2170       IF old_route <> rtg_org_dtl_tab(ri).routing_id OR
2171          old_orgn_id <> rtg_org_dtl_tab(ri).organization_id OR
2172          old_step <> rtg_org_dtl_tab(ri).routingstep_no THEN
2173 
2174         found := 0;
2175         /* NAMIT_OC */
2176         found_chrg_rsrc := 0;
2177         chrg_activity   := NULL;
2178         first_step_row := ri;
2179 
2180         old_route := rtg_org_dtl_tab(ri).routing_id;
2181         old_orgn_id := rtg_org_dtl_tab(ri).organization_id;
2182         old_step := rtg_org_dtl_tab(ri).routingstep_no;
2183 
2184       END IF;
2185       /* if we found an activity with the material ind = 1 and one has not
2186          found yet we need to go back and set all the activities before this
2187          one in the step as PRIOR. the rows are looped though using the first
2188          step row index to the row before this current row. All rows with
2189          the material ind = 1 will have the schedule flag set to 1 as part
2190          of the query by default. If no activity has a material ind = 1
2191          all of the activities will be considered as schedule_flag=1
2192          by default in the query */
2193 
2194       IF rtg_org_dtl_tab(ri).material_ind = 1 AND found = 0 THEN
2195         found := 1;
2196         IF first_step_row < ri THEN
2197           end_index := ri -1;
2198           FOR j IN first_step_row..end_index
2199           LOOP
2200             rtg_org_dtl_tab(j).schedule_flag := 3;
2201           END LOOP;
2202         END IF;
2203 
2204       /* if the material ind is 0 but another activity was found with 1
2205          then this row will be considered as NEXT. */
2206       ELSIF rtg_org_dtl_tab(ri).material_ind = 0 AND found = 1 THEN
2207         rtg_org_dtl_tab(ri).schedule_flag := 4;
2208       END IF;
2209 
2210         /* NAMIT_OC */
2211 
2212         /* If an operation has been found to have more than one activity with chargeable resources the first
2213         activity will be used and all other activities will have the scale type changed to be linear. If any
2214         activity found with chargeable resource, other activities in the operation having resource with
2215         scale_type "By Charge", will be changed to scale_type "Proportional" */
2216 
2217       IF rtg_org_dtl_tab(ri).mat_found = 0 OR rtg_org_dtl_tab(ri).material_ind = 1
2218       THEN
2219 
2220         IF rtg_org_dtl_tab(ri).rtg_scale_type = 3
2221         AND rtg_org_dtl_tab(ri).capacity_constraint = 1
2222         AND found_chrg_rsrc = 0 THEN
2223           found_chrg_rsrc := 1;
2224           chrg_activity := rtg_org_dtl_tab(ri).activity;
2225         /* if the rtg_scale_type is 3 but another activity was found with 3
2226            then this row will be assigned scale_type = 1. */
2227         ELSIF rtg_org_dtl_tab(ri).rtg_scale_type = 3
2228         AND rtg_org_dtl_tab(ri).capacity_constraint = 1
2229         AND found_chrg_rsrc = 1
2230         AND chrg_activity <> rtg_org_dtl_tab(ri).activity THEN
2231           rtg_org_dtl_tab(ri).rtg_scale_type := 1;
2232           rtg_org_dtl_tab(ri).scale_type := 1;
2233         END IF;
2234       END IF;
2235 
2236       rtg_org_dtl_size := rtg_org_dtl_size + 1;
2237 
2238     END LOOP;
2239     CLOSE cur_routing_dtl;
2240     rtg_org_dtl_size := rtg_org_dtl_size -1 ;
2241     time_stamp ;
2242     log_message('Routing Org detail size is = ' || to_char(rtg_org_dtl_size)) ;
2243 
2244      /* New GMD Changes - B1830940 */
2245     validation_statement := 'SELECT  /*+ DRIVING_SITE(frd) DRIVING_SITE(fom) DRIVING_SITE(goa)'
2246               ||' DRIVING_SITE(gor) DRIVING_SITE(crm) DRIVING_SITE(mum) */ '
2247               ||'  frd.routing_id, '
2248               ||'  frd.routingstep_no, '
2249               ||'  NVL(goa.sequence_dependent_ind,0), '
2250               ||'  DECODE(gor.prim_rsrc_ind, 1,1,2,2,0,3), ' /* This will ensure that ordering will
2251                                                                 always have primary firsr*/
2252               ||'  gor.resources, '
2253               ||'  frd.routingstep_id, '
2254               ||'  fom.oprn_no, '
2255               ||'  goa.oprn_line_id, '
2256               ||'  goa.activity, '
2257               ||'  gor.prim_rsrc_ind, '
2258               ||'  goa.offset_interval, '
2259               ||'  gor.resource_usage_uom, ' -- akaruppa changed sou.uom_code to gor.resource_usage_uom
2260               ||'  decode(crm.capacity_constraint,1,1,2) ' -- akaruppa added to check if resource is chargeable, used for invalidating the routing if chargeable resource is not defined at the org level
2261               ||' FROM  fm_rout_dtl'||at_apps_link||' frd, '
2262               ||'       gmd_operations'||at_apps_link||' fom, '
2263               ||'       gmd_operation_activities'||at_apps_link||' goa, '
2264               ||'       gmd_operation_resources'||at_apps_link||' gor, '
2265               ||'       cr_rsrc_mst'||at_apps_link||' crm, '
2266               ||'       mtl_units_of_measure'||at_apps_link||' mum '
2267              ----B#6489338 Added the next where condition as below
2268               ||' WHERE frd.routing_id in (  SELECT /*+ DRIVING_SITE(grb) DRIVING_SITE(ffe) DRIVING_SITE(gs) */ distinct grb.routing_id '
2269               ||'            FROM  gmd_recipes'||at_apps_link ||' grb ,'
2270               ||'                  gmd_recipe_validity_rules'||at_apps_link ||' ffe ,'
2271               ||'                  hr_organization_units'||at_apps_link ||' hou ,'
2272               ||'                  gmd_status_b'||at_apps_link ||' gs '
2273               ||'            WHERE grb.recipe_id = ffe.recipe_id'
2274               ||'              AND ffe.validity_rule_status = gs.status_code '
2275               ||'              AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
2276               ||'              AND gs.status_type IN (' ||'''700'''|| ',' ||'''900'''|| ')'
2277               ||'              AND gs.delete_mark = 0 '
2278               ||'              AND ( ffe.organization_id is NULL or ffe.organization_id = hou.organization_id )'
2279               ||'              AND hou.organization_id '|| l_in_str_org
2280               ||'              AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE  )'
2281               ||'   AND frd.oprn_id = fom.oprn_id '
2282               ||'   AND fom.oprn_id = goa.oprn_id '
2283               ||'   AND gor.resources = crm.resources '
2284               ||'   AND fom.delete_mark = 0'
2285               ||'   AND goa.oprn_line_id = gor.oprn_line_id '
2286               ||'   AND mum.uom_code = gor.resource_usage_uom '
2287               ||'   AND mum.uom_class = :gmp_uom_class '
2288               ||' ORDER BY 1, 2, 3, 4, 5 ' ;
2289 
2290 /*              ||' ORDER BY frd.routing_id, '
2291               ||'          frd.routingstep_no, '
2292               ||'          fom.oprn_no, '
2293               ||'          NVL(goa.sequence_dependent_ind,0) DESC, '
2294               ||'          goa.offset_interval,'
2295               ||'          goa.activity,'
2296               ||'          goa.oprn_line_id,'
2297               ||'          decode(gor.prim_rsrc_ind,1,1,2), '
2298               ||'          gor.resources ' ;*/
2299 
2300     gmp_debug_message('validation_statement -'||validation_statement);
2301     OPEN cur_routing_dtl FOR validation_statement USING g_gmp_uom_class;
2302     FETCH cur_routing_dtl BULK COLLECT INTO rtg_gen_dtl_tab;
2303     rtg_gen_dtl_size := rtg_gen_dtl_tab.count;
2304 /* PennColor Bug: 8230710
2305     LOOP
2306       FETCH cur_routing_dtl INTO rtg_gen_dtl_tab(rtg_gen_dtl_size);
2307       EXIT WHEN cur_routing_dtl%NOTFOUND;
2308       rtg_gen_dtl_size := rtg_gen_dtl_size + 1;
2309     END LOOP;
2310 */
2311 
2312     CLOSE cur_routing_dtl;
2313 --    rtg_gen_dtl_size := rtg_gen_dtl_size -1 ;
2314     time_stamp ;
2315     log_message('Generic Routing size is = ' || to_char(rtg_gen_dtl_size)) ;
2316 
2317             recipe_orgn_statement := ' SELECT /*+ DRIVING_SITE(grb) DRIVING_SITE(gs) */ '
2318                ||'  grb.routing_id, gc.organization_id, '
2319                ||'  gc.routingstep_id, gc.oprn_line_id, gc.recipe_id, '
2320                ||'  gc.activity_factor, '
2321                ||'  gc.resources, gc.resource_usage, gc.process_qty, '
2322                ||'  gc.min_capacity, gc.max_capacity  '
2323                ||' FROM gmd_recipes'||at_apps_link||' grb, '
2324                ||'      gmd_status_b'||at_apps_link||' gs, ' /* B5114783*/
2325                ||' ( '
2326                ||' SELECT  /*+ DRIVING_SITE(goa) DRIVING_SITE(gor) */ '
2327                ||'  gor.recipe_id, '
2328                ||'  gor.organization_id, '
2329                ||'  gor.oprn_line_id, '
2330                ||'  gor.routingstep_id, '
2331                ||'  goa.activity_factor, '
2332                ||'  gor.resources, '
2333                ||'  gor.resource_usage , '
2334                ||'  gor.process_qty,  '
2335                ||'  gor.min_capacity, '
2336                ||'  gor.max_capacity  '
2337                ||' FROM  gmd_recipe_orgn_activities'||at_apps_link||' goa, '
2338                ||'       gmd_recipe_orgn_resources'||at_apps_link||' gor '
2339                ||' WHERE gor.recipe_id = goa.recipe_id '
2340                ||'   AND gor.organization_id = goa.organization_id '
2341                ||'   AND gor.oprn_line_id = goa.oprn_line_id '
2342                ||'   AND gor.routingstep_id = goa.routingstep_id '
2343                ||' UNION ALL '
2344                ||' SELECT /*+ DRIVING_SITE(goa) */ goa.recipe_id, '
2345                ||'  goa.organization_id, '
2346                ||'  goa.oprn_line_id, '
2347                ||'  goa.routingstep_id, '
2348                ||'  goa.activity_factor,  '
2349                ||'  NULL resources,  '
2350                ||'  -1 resource_usage, '
2351                ||'  -1 process_qty, '
2352                ||'  -1 min_capacity, '
2353                ||'  -1 max_capacity '
2354                ||' FROM  gmd_recipe_orgn_activities'||at_apps_link||' goa '
2355                ||' WHERE NOT EXISTS( SELECT /*+ DRIVING_SITE(gor) */ 1 '
2356                ||'       FROM gmd_recipe_orgn_resources'||at_apps_link||' gor '
2357                ||'       WHERE gor.recipe_id = goa.recipe_id '
2358                ||'         AND gor.organization_id = goa.organization_id '
2359                ||'         AND gor.oprn_line_id = goa.oprn_line_id '
2360                ||'         AND gor.routingstep_id = goa.routingstep_id ) '
2361                ||' UNION ALL '
2362                ||' SELECT /*+ DRIVING_SITE(gor) */ gor.recipe_id, '
2363                ||'  gor.organization_id, '
2364                ||'  gor.oprn_line_id, '
2365                ||'  gor.routingstep_id, '
2366                ||'  -1 activity_factor, '
2367                ||'  gor.resources, '
2368                ||'  gor.resource_usage , '
2369                ||'  gor.process_qty,  '
2370                ||'  gor.min_capacity, '
2371                ||'  gor.max_capacity '
2372                ||' FROM  gmd_recipe_orgn_resources'||at_apps_link||' gor  '
2373                ||' WHERE NOT EXISTS( SELECT /*+ DRIVING_SITE(goa) */ 1 '
2374                ||'       FROM gmd_recipe_orgn_activities'||at_apps_link||' goa'
2375                ||'       WHERE goa.recipe_id = gor.recipe_id '
2376                ||'         AND goa.organization_id = gor.organization_id '
2377                ||'         AND goa.oprn_line_id = gor.oprn_line_id '
2378                ||'         AND goa.routingstep_id = gor.routingstep_id ) '
2379                ||' ) gc '
2380                ||' WHERE grb.recipe_id = gc.recipe_id '
2381                ||'   AND grb.delete_mark = 0 '
2382             /* B5114783 start */
2383                ||'   AND grb.recipe_status =  gs.status_code '
2384                ||'   AND gs.status_type IN (' ||'''700''' || ',' || '''900''' || ',' || '''400'''|| ') '
2385                ||'   AND gs.delete_mark = 0 '
2386             /* B5114783 End */
2387               ||'     AND grb.recipe_id  in (  SELECT /*+ DRIVING_SITE(grb) DRIVING_SITE(ffe) DRIVING_SITE(gs) */ distinct  grb.recipe_id '
2388               ||'            FROM  gmd_recipes'||at_apps_link ||' grb ,'
2389               ||'                  gmd_recipe_validity_rules'||at_apps_link ||' ffe ,'
2390               ||'                  hr_organization_units'||at_apps_link ||' hou ,'
2391               ||'                  gmd_status_b'||at_apps_link ||' gs '
2392               ||'            WHERE grb.recipe_id = ffe.recipe_id'
2393               ||'              AND ffe.validity_rule_status = gs.status_code '
2394               ||'              AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
2395               ||'              AND gs.status_type IN  (' ||'''700''' || ',' || '''900''' || ',' || '''400'''|| ') '
2396               ||'              AND gs.delete_mark = 0 '
2397               ||'              AND ( ffe.organization_id is NULL or ffe.organization_id = hou.organization_id )'
2398               ||'              AND hou.organization_id '|| l_in_str_org
2399               ||'              AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE  )'
2400                ||' ORDER BY 1,2,3,4,5 ' ;
2401 
2402     gmp_debug_message('recipe_orgn_statement -'||recipe_orgn_statement);
2403     OPEN c_recipe_orgn FOR recipe_orgn_statement;
2404 /* PennColor Bug: 8230710
2405     LOOP
2406       FETCH c_recipe_orgn INTO rcp_orgn_override(recipe_orgn_over_size);
2407       EXIT WHEN c_recipe_orgn%NOTFOUND;
2408       recipe_orgn_over_size := recipe_orgn_over_size + 1;
2409     END LOOP;
2410   */
2411    FETCH c_recipe_orgn BULK COLLECT INTO rcp_orgn_override;
2412    recipe_orgn_over_size := rcp_orgn_override.COUNT;
2413 
2414     CLOSE c_recipe_orgn;
2415 
2416     --recipe_orgn_over_size := recipe_orgn_over_size -1 ;
2417     time_stamp ;
2418     log_message('recipe_orgn_over_size is= '|| to_char(recipe_orgn_over_size));
2419 
2420     recipe_statement :=
2421                  ' SELECT /*+ DRIVING_SITE(grb) DRIVING_SITE(gs) DRIVING_SITE(grs) DRIVING_SITE(frd)  */ '
2422                ||'    grb.routing_id, grs.routingstep_id, grs.recipe_id, '
2423                ||'        grs.step_qty '
2424                ||' FROM gmd_recipes'||at_apps_link||' grb, '
2425                ||'      gmd_status_b'||at_apps_link||' gs, ' /* B5114783*/
2426                ||'      gmd_recipe_routing_steps'||at_apps_link||' grs, '
2427                ||'      fm_rout_dtl'||at_apps_link||' frd '
2428                ||' WHERE grb.recipe_id = grs.recipe_id '
2429                ||'   AND grb.delete_mark = 0 '
2430             /* B5114783 start */
2431                ||'   AND grb.recipe_status =  gs.status_code '
2432                ||'   AND gs.status_type IN (' ||'''700''' || ',' || '''900''' || ',' || '''400'''|| ') '
2433                ||'   AND gs.delete_mark = 0 '
2434             /* B5114783 End */
2435              /* PennColor Bug: 8230710 */
2436                         ----B#6489338 Added the next where condition as below
2437               ||'     AND  grs.recipe_id  in (  SELECT /*+ DRIVING_SITE(grb) DRIVING_SITE(ffe) DRIVING_SITE(gs) */ distinct  grb.recipe_id '
2438               ||'            FROM  gmd_recipes'||at_apps_link ||' grb ,'
2439               ||'                  gmd_recipe_validity_rules'||at_apps_link ||' ffe ,'
2440               ||'                  hr_organization_units'||at_apps_link ||' hou ,'
2441               ||'                  gmd_status_b'||at_apps_link ||' gs '
2442               ||'            WHERE grb.recipe_id = ffe.recipe_id'
2443               ||'              AND ffe.validity_rule_status = gs.status_code '
2444               ||'              AND ffe.recipe_use IN (''0'',''1'') '    /* B10075785 */
2445               ||'              AND gs.status_type IN  (' ||'''700''' || ',' || '''900''' || ',' || '''400'''|| ') '
2446               ||'              AND gs.delete_mark = 0 '
2447               ||'              AND ( ffe.organization_id is NULL or ffe.organization_id = hou.organization_id )'
2448               ||'              AND hou.organization_id '|| l_in_str_org
2449               ||'              AND nvl(ffe.end_date,(SYSDATE + 1)) > SYSDATE  )'
2450               ||'              AND grb.routing_id = frd.routing_id  '
2451               ||'              AND  grs.routingstep_id = frd.routingstep_id '
2452       -- Bug: 8354988 removed the below condition as routing step qty and recipe override though equal
2453       --  will not imply the same meaning. recipe overrides are the scaled values basing on formula total output and routing qty
2454            --   ||'       AND grs.step_qty <> frd.step_qty '
2455     --  Bug: 8354988  modified the order by clause from 1,2,3 to 1,3,2
2456                ||' ORDER BY 1,3,2 ' ;
2457 
2458     gmp_debug_message('recipe_statement -'||recipe_statement);
2459     OPEN c_recipe_override FOR recipe_statement ;
2460      FETCH c_recipe_override BULK COLLECT INTO recipe_override;
2461 /* PennColor Bug: 8230710
2462     LOOP
2463       FETCH c_recipe_override INTO recipe_override(recipe_override_size);
2464       EXIT WHEN c_recipe_override%NOTFOUND;
2465       recipe_override_size := recipe_override_size + 1;
2466     END LOOP;
2467  */
2468     CLOSE c_recipe_override;
2469  --   recipe_override_size := recipe_override_size -1 ;
2470     recipe_override_size := recipe_override.COUNT;
2471     time_stamp ;
2472     log_message('recipe Override size is = '||to_char(recipe_override_size)) ;
2473 
2474      /* Alternate Resource selection   */
2475      /* B5688153, Rajesh Patangya prod spec alt*/
2476         statement_alt_resource :=
2477                      ' SELECT pcrd.resource_id, acrd.resource_id, '
2478                    ||' acrd.min_capacity, acrd.max_capacity, '
2479                    ||' cam.runtime_factor, '
2480 /*prod spec alt*/  ||' nvl(cam.preference,-1), nvl(prod.inventory_item_id,-1)   '
2481                    ||' FROM  cr_rsrc_dtl'||at_apps_link||' acrd, '
2482                    ||'       cr_rsrc_dtl'||at_apps_link||' pcrd, '
2483                    ||'       cr_ares_mst'||at_apps_link||' cam, '
2484                    ||'       gmp_altresource_products'||at_apps_link||' prod'
2485                    ||' WHERE cam.alternate_resource = acrd.resources '
2486                    ||'   AND cam.primary_resource = pcrd.resources '
2487                    ||'   AND acrd.organization_id = pcrd.organization_id '
2488                    ||'   AND cam.primary_resource = prod.primary_resource(+) '
2489                    ||'   AND cam.alternate_resource = prod.alternate_resource(+) '
2490                    ||'   AND acrd.delete_mark = 0  '
2491                    ||'   AND pcrd.delete_mark = 0  '
2492                    ||' ORDER BY pcrd.resource_id, '
2493                    ||' DECODE(cam.preference,NULL,cam.runtime_factor,cam.preference),'
2494                    ||' prod.inventory_item_id ' ;
2495 
2496     gmp_debug_message('statement_alt_resource -'||statement_alt_resource);
2497 
2498     OPEN cur_alt_resource FOR statement_alt_resource ;
2499     LOOP
2500       FETCH cur_alt_resource INTO rtg_alt_rsrc_tab(alt_rsrc_size);
2501       EXIT WHEN cur_alt_resource%NOTFOUND;
2502       alt_rsrc_size := alt_rsrc_size + 1;
2503     END LOOP;
2504     CLOSE cur_alt_resource;
2505     alt_rsrc_size := alt_rsrc_size -1 ;
2506     time_stamp ;
2507     log_message('Alternate Routing size is = ' || to_char(alt_rsrc_size)) ;
2508 
2509 /* NAMIT_CR Get Step Dependency data */
2510 
2511    opr_stpdep_cursor := ' SELECT /*+ DRIVING_SITE(frd1) DRIVING_SITE(frd2) DRIVING_SITE(frdp) */  '
2512               ||'          frdp.routing_id, '
2513               ||'          ((frd2.routingstep_id * 2) + 1) x_dep_routingstep_id, '
2514               ||'          ((frd1.routingstep_id * 2) + 1) x_routingstep_id, '
2515               ||'          decode(frdp.dep_type,0,1,2) dependency_type, '
2516               ||'          frdp.standard_delay, '
2517               ||'          frdp.max_delay, '
2518               ||'          frdp.transfer_pct, '
2519               ||'          frdp.dep_routingstep_no, '
2520               ||'          frdp.routingstep_no, '
2521               ||'          decode(nvl(frdp.chargeable_ind, 0),0,2,1,1) '
2522               ||'      FROM '
2523               ||'          fm_rout_dtl'||at_apps_link||' frd1, '
2524               ||'          fm_rout_dtl'||at_apps_link||' frd2, '
2525               ||'          fm_rout_dep'||at_apps_link||' frdp '
2526               ||'      WHERE '
2527               ||'          frd1.routing_id = frdp.routing_id '
2528               ||'          AND frd1.routingstep_no = frdp.routingstep_no '
2529               ||'          AND frd2.routing_id = frdp.routing_id '
2530               ||'          AND frd2.routingstep_no = frdp.dep_routingstep_no '
2531               ||'      ORDER BY 1,3,2 ' ;
2532 
2533     OPEN cur_opr_stpdep FOR opr_stpdep_cursor ;
2534     FETCH cur_opr_stpdep BULK COLLECT INTO gmp_opr_stpdep_tbl;
2535     opr_stpdep_size := gmp_opr_stpdep_tbl.COUNT;
2536 
2537 /* PennColor Bug: 8230710
2538     LOOP
2539       FETCH cur_opr_stpdep INTO gmp_opr_stpdep_tbl(opr_stpdep_size);
2540       EXIT WHEN cur_opr_stpdep%NOTFOUND;
2541       opr_stpdep_size := opr_stpdep_size + 1;
2542     END LOOP;
2543 */
2544 
2545     CLOSE cur_opr_stpdep;
2546 --    opr_stpdep_size := opr_stpdep_size -1 ;
2547     time_stamp ;
2548     log_message('Operation Step Dependency size is = ' || to_char(opr_stpdep_size)) ;
2549 
2550     /* ------------------------------------------------------- */
2551     /* PROCESSING STARTS AFTER SELECTION OF THE DATA IN MEMORY */
2552     /* ------------------------------------------------------- */
2553 
2554     -- Link the routing header and detail
2555     link_routing ;
2556 
2557     -- Link the routing header and detail overrides
2558     link_override_routing ;
2559 
2560 /* Now spool the routing Header data for debugging */
2561   gmp_debug_message('Routing org details for debug purpose ');
2562   gmp_debug_message('RTG_ID Org Valid GStart GEnd OStart OEnd StStart StEND UsgSt UsgEnd StpDepSt StpDepEnd ');
2563   IF (l_debug = 'Y') THEN
2564   For spl_cnt in 1..rtg_org_hdr_tab.COUNT
2565   LOOP
2566      gmp_debug_message ( rtg_org_hdr_tab(spl_cnt).routing_id ||'*'||
2567      rtg_org_hdr_tab(spl_cnt).organization_id    ||'**'||
2568      rtg_org_hdr_tab(spl_cnt).valid_flag         ||'**'||
2569      rtg_org_hdr_tab(spl_cnt).generic_start_loc  ||'**'||
2570      rtg_org_hdr_tab(spl_cnt).generic_end_loc    ||'**'||
2571      rtg_org_hdr_tab(spl_cnt).orgn_start_loc     ||'**'||
2572      rtg_org_hdr_tab(spl_cnt).orgn_end_loc       ||'**'||
2573      rtg_org_hdr_tab(spl_cnt).step_start_loc     ||'**'||
2574      rtg_org_hdr_tab(spl_cnt).step_end_loc       ||'**'||
2575      rtg_org_hdr_tab(spl_cnt).usage_start_loc    ||'**'||
2576      rtg_org_hdr_tab(spl_cnt).usage_end_loc      ||'**'||
2577      rtg_org_hdr_tab(spl_cnt).stpdep_start_loc   ||'**'||
2578      rtg_org_hdr_tab(spl_cnt).stpdep_end_loc );
2579   END LOOP ;
2580   END IF;
2581 
2582 
2583     /*  New GMD Changes B1830940 */
2584     /*  B2800311, APS SHOULD SEE INGREDIENTS RELEASED AS AUTO-BY-STEP */
2585     /* B3054460 OPM/APS TO CATER FOR CHANGE TO TIME PHASED PLANNING OF
2586        MANUAL CONSUMPTION TYPE
2587     */
2588 
2589     mat_assoc_cursor  :=
2590                 ' SELECT  /*+ DRIVING_SITE(r) DRIVING_SITE(frm) DRIVING_SITE(fmd) DRIVING_SITE(frd) '
2591               ||'  DRIVING_SITE(msi) */  fmd.formula_id, frm.recipe_id, '
2592               ||'  DECODE(fmd.line_type, 1,1,2,2,-1,3), fmd.line_no, '
2593               ||'  ((frm.formulaline_id * 2) + 1) x_formulaline_id, '
2594               ||'  ((frm.routingstep_id * 2) + 1) x_routingstep_id, '
2595 /* NAMIT_MTQ */
2596               ||'   fmd.inventory_item_id, frd.routingstep_no, '
2597               || '   DECODE(fmd.detail_uom, msi.primary_uom_code, 1, ' -- akaruppa previously DECODE(fmd.item_um, gia.item_um,1,
2598               ||'        inv_convert.inv_um_convert'||at_apps_link  -- akaruppa previously GMICUOM.uom_conversion
2599               ||'                 (fmd.inventory_item_id, '
2600               ||'                  NULL, '
2601               ||'                  msi.organization_id, '
2602       /* bug: 6918852 Vpedarla 04-Apr-2008  used the global variable for precision*/
2603            /* ||'                   NULL, '  */
2604               ||                    conv_precision || ' , '
2605               ||'                  1, '
2606               ||'                  fmd.detail_uom , '
2607               ||'                  msi.primary_uom_code , '
2608               ||'                  NULL , '
2609               ||'                  NULL '
2610               ||'                 ) '
2611               ||'         ) uom_conv_factor, '
2612               /*Sowmya - As per Latest FDD changes - Changes as per Matt's review comments-
2613               Fetch the conversion factor for a unit item, needed for converting the MTQ.
2614               MTQ value to be passed as promary UOM.*/
2615               ||'   decode(fmd.line_type, 1, frm.minimum_transfer_qty, null) minimum_transfer_qty, '
2616               ||'   decode(fmd.line_type, 1, frm.minimum_delay, null) minimum_delay, '
2617               ||'   decode(fmd.line_type, 1, frm.maximum_delay, null) maximum_delay '
2618               ||' FROM gmd_recipes'||at_apps_link||' r ,' /* added for asqc flg*/
2619 	      ||' gmd_recipe_step_materials'||at_apps_link||' frm, '
2620               ||'       fm_matl_dtl'||at_apps_link||' fmd, '
2621 /* NAMIT_MTQ */
2622               ||'       fm_rout_dtl'||at_apps_link||' frd,  '
2623               ||'       mtl_system_items'||at_apps_link||' msi'
2624               ||' WHERE fmd.formulaline_id = frm.formulaline_id '
2625               ||'   AND msi.organization_id = fmd.organization_id '
2626               ||'   AND frm.recipe_id = r.recipe_id '  /* B3054460 */
2627               ||'   AND (fmd.release_type in (1,2,3) OR '  /* B3054460 */
2628               ||' NVL(r.calculate_step_quantity,0) = 1 ) '  /* xfer for ASQC */
2629 /* NAMIT_MTQ */
2630               ||'   AND frd.routingstep_id = frm.routingstep_id '
2631               ||'   AND msi.inventory_item_id = fmd.inventory_item_id '
2632 /* B3970993 nsinghi. Changed order by clause from 1,2,3,4,5 to 1,2,3,6,7 */
2633               ||' ORDER BY 1,2,3,4,6,7 ';
2634         /*      ||' ORDER BY 1,2,3,6,7 ';
2635  B7461010 change order by clause to consider the product formulaline come
2636   first, if multiple co-products are present Or same product is used multiple
2637 times as coproducts */
2638 
2639     gmp_debug_message('mat_assoc_cursor -'||mat_assoc_cursor);
2640     OPEN cur_mat_assoc FOR mat_assoc_cursor ;
2641     FETCH cur_mat_assoc BULK COLLECT INTO mat_assoc_tab;
2642     material_assocs_size := mat_assoc_tab.COUNT;
2643 /* PennColor Bug: 8230710
2644     LOOP
2645       FETCH cur_mat_assoc INTO mat_assoc_tab(material_assocs_size);
2646       EXIT WHEN cur_mat_assoc%NOTFOUND;
2647       material_assocs_size := material_assocs_size + 1;
2648     END LOOP;
2649 */
2650     CLOSE cur_mat_assoc;
2651 --    material_assocs_size := material_assocs_size -1 ;
2652     time_stamp ;
2653     log_message('Material assoc size is = ' || to_char(material_assocs_size)) ;
2654     -- Charter 14013503 vijay Induri 5/11/2012
2655     OPEN cur_effectivity FOR effectivity_cursor ;
2656     FETCH cur_effectivity BULK COLLECT INTO gmp_effectivity_tab;
2657     effectivity_size := gmp_effectivity_tab.COUNT;
2658     CLOSE cur_effectivity;
2659     time_stamp ;
2660     log_message('Effectivity size is = ' || to_char(effectivity_size)) ;
2661 
2662  -- The cursor for effectivity opened and then the details processed
2663  l_eff_counter := 0;
2664  FOR effectivity_cnt IN 1..effectivity_size
2665 
2666   LOOP
2667   effectivity.fmeff_id                := gmp_effectivity_tab(effectivity_cnt).fmeff_id                  ;
2668   effectivity.aps_fmeff_id            := gmp_effectivity_tab(effectivity_cnt).aps_fmeff_id              ;
2669   effectivity.inventory_item_id       := gmp_effectivity_tab(effectivity_cnt).inventory_item_id         ;
2670   effectivity.formula_id              := gmp_effectivity_tab(effectivity_cnt).formula_id                ;
2671   effectivity.organization_id         := gmp_effectivity_tab(effectivity_cnt).organization_id           ;
2672   effectivity.start_date              := gmp_effectivity_tab(effectivity_cnt).start_date                ;
2673   effectivity.end_date                := gmp_effectivity_tab(effectivity_cnt).end_date                  ;
2674   effectivity.inv_min_qty             := gmp_effectivity_tab(effectivity_cnt).inv_min_qty               ;
2675   effectivity.inv_max_qty             := gmp_effectivity_tab(effectivity_cnt).inv_max_qty               ;
2676   effectivity.preference              := gmp_effectivity_tab(effectivity_cnt).preference                ;
2677   effectivity.primary_uom_code        := gmp_effectivity_tab(effectivity_cnt).primary_uom_code          ;
2678   effectivity.organization_code       := gmp_effectivity_tab(effectivity_cnt).organization_code         ;
2679   effectivity.routing_id              := gmp_effectivity_tab(effectivity_cnt).routing_id                ;
2680   effectivity.routing_no              := gmp_effectivity_tab(effectivity_cnt).routing_no                ;
2681   effectivity.routing_vers            := gmp_effectivity_tab(effectivity_cnt).routing_vers              ;
2682   effectivity.routing_desc            := gmp_effectivity_tab(effectivity_cnt).routing_desc              ;
2683   effectivity.routing_uom             := gmp_effectivity_tab(effectivity_cnt).routing_uom               ;
2684   effectivity.routing_qty             := gmp_effectivity_tab(effectivity_cnt).routing_qty               ;
2685   effectivity.prod_factor             := gmp_effectivity_tab(effectivity_cnt).prod_factor               ;
2686   effectivity.product_index           := gmp_effectivity_tab(effectivity_cnt).product_index             ;
2687   effectivity.recipe_id               := gmp_effectivity_tab(effectivity_cnt).recipe_id                 ;
2688   effectivity.recipe_no               := gmp_effectivity_tab(effectivity_cnt).recipe_no                 ;
2689   effectivity.recipe_version          := gmp_effectivity_tab(effectivity_cnt).recipe_version            ;
2690   effectivity.rtg_hdr_location        := gmp_effectivity_tab(effectivity_cnt).rtg_hdr_location          ;
2691   effectivity.calculate_step_quantity := gmp_effectivity_tab(effectivity_cnt).calculate_step_quantity   ;
2692   effectivity.category_id             := gmp_effectivity_tab(effectivity_cnt).category_id               ;
2693   effectivity.setup_id                := gmp_effectivity_tab(effectivity_cnt).setup_id                  ;
2694   effectivity.seq_dpnd_class          := gmp_effectivity_tab(effectivity_cnt).seq_dpnd_class            ;
2695 
2696 -- END LOOP;
2697  -- The cursor for effectivity opened and then the details processed
2698 -- l_eff_counter := 0;
2699 -- OPEN c_formula_effectivity FOR effectivity_cursor;
2700 
2701 --  LOOP
2702 --  FETCH c_formula_effectivity INTO effectivity;
2703 --  EXIT WHEN c_formula_effectivity%NOTFOUND;
2704   l_eff_counter := l_eff_counter + 1;
2705    IF ((effectivity.formula_id <> old_formula_id) OR
2706        (effectivity.organization_id <> old_organization_id) OR
2707        (effectivity.fmeff_id <> old_fmeff_id)
2708       )  THEN   /* Old values */
2709 
2710 gmp_debug_message('processing fmeff_id -'|| effectivity.fmeff_id||' aps_fmeff_id -'||effectivity.aps_fmeff_id);
2711     valid := check_formula(effectivity.organization_id, effectivity.formula_id);
2712 
2713     IF not valid THEN
2714       log_message('check_formula returned Invalid for Organization_id '||
2715          effectivity.organization_id||' formula_id '||effectivity.formula_id) ;
2716     END IF;
2717 
2718    /* routing check for effectivity */
2719     IF (valid) AND effectivity.routing_id IS NOT NULL THEN
2720         /* Locate_org_routing through Bsearch */
2721         gmp_debug_message('calling find_routing_header');
2722          valid := find_routing_header (effectivity.routing_id,
2723                                        effectivity.organization_id);
2724 
2725        IF not valid THEN
2726         log_message('find_routing_header returned Invalid for Organization_id '||
2727            effectivity.organization_id||' routing_id '||effectivity.routing_id) ;
2728        END IF;
2729 
2730        IF (valid) AND effectivity.rtg_hdr_location > 0 AND
2731                       effectivity.routing_qty >= 0 THEN
2732 
2733            g_setup_id  := NULL; -- B4918786 (RDP) SDS
2734            sd_index    := 0 ;  -- B4918786 (RDP) SDS
2735 
2736          gmp_debug_message('calling validate_routing - '||effectivity.fmeff_id);
2737          IF (l_debug = 'Y') then
2738               time_stamp;
2739          END IF;
2740 
2741            validate_routing( effectivity.routing_id,
2742                              effectivity.organization_id,
2743                              effectivity.rtg_hdr_location,
2744                              routing_valid);
2745 
2746          IF (l_debug = 'Y') then
2747               time_stamp;
2748          END IF;
2749 
2750             IF (routing_valid) THEN /* Valid routing  */
2751                   valid := TRUE ;
2752              ELSE
2753                   valid := FALSE ;
2754                  LOG_MESSAGE('validate_routing returned Invalid for Organization_id '||
2755                    effectivity.organization_id||' routing_id '||effectivity.routing_id||
2756                    ' rtg_hdr_location '||effectivity.rtg_hdr_location||
2757                    'fmeff_id '||effectivity.fmeff_id) ;
2758 
2759              END IF;  /* Valid routing  */
2760 
2761        END IF ;   /* routing header location */
2762 
2763        /*B2870041 this logic will get the total output qty in the routing uom
2764           if the formula or route fails validation the effectivity is skipped*/
2765        IF (valid) THEN
2766 
2767          /* if the total output was already calculated for this formula in
2768             the routing um there is no need to do it again */
2769          IF formula_header_tab(g_fm_hdr_loc).total_uom <>
2770               effectivity.routing_uom  OR
2771             formula_header_tab(g_fm_hdr_loc).total_uom IS NULL THEN
2772 
2773            /* if the factor was not calculated then the uom conversion failed
2774               and if it failed the effectivity can not be used */
2775            IF effectivity.prod_factor <= 0 THEN
2776              valid := FALSE;
2777             gmp_debug_message(' Not valid because effectivity.prod_factor is '
2778                    ||effectivity.prod_factor);
2779            ELSE
2780              /* reset the total ouput accumulator and loop through all of the
2781                 material details to find all products and byproducts */
2782              temp_total_qty := 0;
2783 
2784             gmp_debug_message('Before Formula Dtl Loop - '||effectivity.fmeff_id);
2785                  IF (l_debug = 'Y') then
2786                       time_stamp;
2787                  END IF;
2788 
2789              FOR j IN g_fm_dtl_start_loc..g_fm_dtl_end_loc
2790              LOOP
2791 
2792                /* if the line is either a product or byproduct then we need
2793                   to process it */
2794                IF formula_detail_tab(j).line_type > 0 THEN
2795 
2796                  /* if the item is the same as the item in the effectivity
2797                     we have the factor to get the item from base uom to the
2798                     route uom */
2799                  IF (formula_detail_tab(j).inventory_item_id = effectivity.inventory_item_id)
2800                  THEN
2801                    temp_total_qty := temp_total_qty +
2802                      (effectivity.prod_factor *
2803                       formula_detail_tab(j).primary_qty);
2804                  /* if the item is different but the item base uom is the
2805                     same as the route the primary_qty will be used */
2806                  ELSIF
2807                    formula_detail_tab(j).inventory_item_id <> effectivity.inventory_item_id AND
2808                    formula_detail_tab(j).primary_uom_code = effectivity.routing_uom
2809                  THEN
2810                    temp_total_qty := temp_total_qty +
2811                       formula_detail_tab(j).primary_qty;
2812                  /* if the item is different but the item base uom is the
2813                     same as the route the primary_qty will be used */
2814                  ELSIF
2815                    formula_detail_tab(j).inventory_item_id <> effectivity.inventory_item_id AND
2816                    formula_detail_tab(j).detail_uom = effectivity.routing_uom
2817                  THEN
2818                    temp_total_qty := temp_total_qty +
2819                       formula_detail_tab(j).formula_qty;
2820                  /* no uom can be matched or the item is not the same as the
2821                     product thus a uom conversion will need to be done. If the
2822                     qty is 0 there is no need to do the conversion */
2823                  ELSIF formula_detail_tab(j).formula_qty > 0 THEN
2824                    uom_conv_cursor :=
2825                        'SELECT '
2826 		     ||'  inv_convert.inv_um_convert'||at_apps_link  -- akaruppa previously GMICUOM.uom_conversion
2827 		     ||'  (:pitem, '
2828 		     ||'   NULL, '
2829 		     ||'   :orgid, '
2830 	      /* bug: 6918852 Vpedarla 04-Apr-2008  used the global variable for precision*/
2831                   /* ||'   NULL, '  */
2832                      ||    conv_precision || ' , '
2833 		     ||'   :pqty, '
2834 		     ||'   :pfrom_um, '
2835 		     ||'   :pto_um , '
2836 		     ||'   NULL , '
2837 		     ||'   NULL '
2838 		     ||'   ) '
2839                      ||'   FROM dual';
2840                    v_matl_qty := -1;
2841                    OPEN c_uom_conv FOR uom_conv_cursor USING
2842                      formula_detail_tab(j).inventory_item_id,
2843                      effectivity.organization_id, --sowmya added.
2844                      formula_detail_tab(j).primary_qty,
2845                      formula_detail_tab(j).primary_uom_code,
2846                      effectivity.routing_uom;
2847 
2848                    FETCH c_uom_conv INTO v_matl_qty;
2849                    CLOSE c_uom_conv;
2850 
2851                    /* as long as the qty is >0 then the uom conversion was
2852                       successful. If negative then it failed so reject the
2853                       effectivity and stop the current loop */
2854                    IF v_matl_qty > 0 THEN
2855                      temp_total_qty := temp_total_qty + v_matl_qty;
2856                    ELSE
2857                      valid := FALSE;
2858                     gmp_debug_message(' Existing v_matl_qty = '||v_matl_qty);
2859                      EXIT;
2860                    END IF;
2861                  END IF;
2862                END IF;
2863              END LOOP;
2864 
2865              gmp_debug_message('After Formula Dtl Loop - '||effectivity.fmeff_id);
2866                  IF (l_debug = 'Y') then
2867                       time_stamp;
2868                  END IF;
2869 
2870              /* if there was no failure and the qty is >0 save the values in
2871                 the formula header */
2872              IF (valid) AND temp_total_qty > 0 THEN
2873                formula_header_tab(g_fm_hdr_loc).total_output :=
2874                  temp_total_qty;
2875                formula_header_tab(g_fm_hdr_loc).total_uom :=
2876                  effectivity.routing_uom;
2877              ELSE
2878                log_message(' Not updating formula_header_tab temp_total_qty = '||temp_total_qty);
2879              END IF;
2880            END IF;
2881          END IF;
2882        END IF;
2883 
2884 
2885     END IF;   /* routing check for effectivity */
2886 
2887     IF valid THEN
2888       g_curr_rstep_loc := find_routing_offsets(effectivity.formula_id,
2889                                effectivity.organization_id);
2890 
2891       gmp_debug_message('Before Export Effectivities - '||effectivity.fmeff_id);
2892          IF (l_debug = 'Y') then
2893               time_stamp;
2894          END IF;
2895       export_effectivities (valid);
2896       gmp_debug_message('After Export Effectivities - '||effectivity.fmeff_id);
2897          IF (l_debug = 'Y') then
2898               time_stamp;
2899          END IF;
2900     END IF ;
2901 
2902    END IF ;   /* Old Values */
2903 
2904     old_formula_id      := effectivity.formula_id ;
2905     old_organization_id := effectivity.organization_id ;
2906     old_fmeff_id        := effectivity.fmeff_id ;
2907     valid               := FALSE ;
2908     routing_valid       := FALSE ;
2909 
2910     /*  Vpedarla   added B3837959 MMK Issue, Bulk insert after every 1000 effectivities */
2911     eff_counter         := eff_counter + 1 ;
2912 
2913     IF (mod(eff_counter,1000) = 0)  THEN /* Every 1000 effectivity */
2914       /* If all is OK, Bulk Insert the data into MSC tables */
2915        log_message('Before MSC Inserts' ) ;
2916        time_stamp ;
2917        msc_inserts(valid);
2918           IF NOT (valid) THEN
2919              log_message('Error encountered in MSC_INSERTS');
2920           END IF;
2921     END IF ; /* Every 1000 effectivity */
2922 
2923   END LOOP;
2924 --  CLOSE c_formula_effectivity;  -- charter modification.
2925 
2926    time_stamp ;
2927    log_message('Formula effectivity completed. Before MSC Inserts l_eff_counter = '||l_eff_counter ) ;
2928   /* If all is OK, Bulk Insert the data into MSC tables */
2929    msc_inserts(valid);
2930        IF valid THEN
2931           COMMIT;
2932         ELSE
2933            log_message('Invalid after MSC Inserts' ) ;
2934            NULL ;
2935        END IF;
2936 
2937    time_stamp ;
2938    log_message('After MSC Inserts ' ) ;
2939 
2940    IF NOT (valid) THEN
2941       log_message('Error encountered in MSC_INSERTS');
2942    ELSE
2943       write_setups_and_transitions(at_apps_link,valid) ;  /* Seq Dependencies */  /* bug: 6710684 Vpedarla */
2944       IF NOT (valid) THEN
2945          log_message('Error encountered in write_setups_and_transitions');
2946       ELSE
2947          COMMIT;
2948       END IF;
2949    END IF;
2950 
2951          /* Free memory used by PL/SQL tables used by program  */
2952            IF formula_header_tab.COUNT > 0 THEN
2953            formula_header_tab.delete ;
2954            END IF;
2955            IF formula_header_tab.COUNT > 0 THEN
2956            formula_header_tab.delete ;
2957            END IF;
2958            IF formula_detail_tab.COUNT > 0 THEN
2959            formula_detail_tab.delete ;
2960            END IF;
2961            IF formula_orgn_count_tab.COUNT > 0 THEN
2962            formula_orgn_count_tab.delete ;
2963            END IF;
2964            IF rtg_org_hdr_tab.COUNT > 0 THEN
2965            rtg_org_hdr_tab.delete ;
2966            END IF;
2967            IF rtg_org_dtl_tab.COUNT > 0 THEN
2968            rtg_org_dtl_tab.delete ;
2969            END IF;
2970            IF oper_leadtime_percent.COUNT > 0 THEN
2971            oper_leadtime_percent.delete ;
2972            END IF;
2973            IF rtg_gen_dtl_tab.COUNT > 0 THEN
2974            rtg_gen_dtl_tab.delete ;
2975            END IF;
2976            IF rtg_alt_rsrc_tab.COUNT > 0 THEN
2977            rtg_alt_rsrc_tab.delete ;
2978            END IF;
2979            IF mat_assoc_tab.COUNT > 0 THEN
2980            mat_assoc_tab.delete;
2981            END IF;
2982            IF rcp_orgn_override.COUNT > 0 THEN
2983            rcp_orgn_override.delete ;
2984            END IF;
2985            IF recipe_override.COUNT > 0 THEN
2986            recipe_override.delete ;
2987            END IF;
2988            IF rstep_offsets.COUNT > 0 THEN
2989            rstep_offsets.delete ;
2990            END IF;
2991 
2992           dbms_session.free_unused_user_memory;
2993 
2994           SELECT st.VALUE INTO v_dummy from V$MYSTAT st, V$STATNAME sn
2995           WHERE st.STATISTIC# = sn.STATISTIC#
2996           AND sn.NAME in ('session pga memory max');
2997           log_message('Session pga memory max = ' || to_char(v_dummy) );
2998 
2999           SELECT st.VALUE INTO v_dummy from V$MYSTAT st, V$STATNAME sn
3000           where st.STATISTIC# = sn.STATISTIC#
3001           and sn.NAME in ('session pga memory');
3002           log_message('Session pga memory = ' || TO_CHAR(v_dummy) );
3003 
3004   log_message('End of process' ) ;
3005   time_stamp ;
3006   return_status := TRUE;
3007 
3008   EXCEPTION
3009     /* PennColor Bug: 8230710 */
3010     WHEN source_call_failure THEN
3011         log_message('Source Call Failed ' );
3012         return_status := FALSE;
3013     WHEN invalid_string_value  THEN
3014         log_message('Organization string is Invalid ' );
3015         return_status := FALSE;
3016     WHEN invalid_gmp_uom_profile THEN
3017         log_message('Profile "BOM: UOM for Hour" is Invalid ' );
3018         return_status := FALSE;
3019     WHEN OTHERS THEN
3020 	log_message('Error retrieving effectivities: '||sqlerrm);
3021 	return_status := FALSE;
3022 
3023 END retrieve_effectivities;
3024 
3025 /*
3026 REM+=========================================================================+
3027 REM| PROCEDURE NAME                                                          |
3028 REM|    link_override_routing                                                |
3029 REM| DESCRIPTION                                                             |
3030 REM|    Link the override based on routing and organization code             |
3031 REM|                                                                         |
3032 REM| HISTORY                                                                 |
3033 REM| 08/23/2002   Created Rajesh Patangya                                    |
3034 REM|                                                                         |
3035 REM+=========================================================================+
3036 */
3037 PROCEDURE link_override_routing IS
3038   i              PLS_INTEGER ;
3039   j              PLS_INTEGER ;
3040   k              PLS_INTEGER ;
3041   lgr_loc        PLS_INTEGER ;
3042   lgr_start_loc  PLS_INTEGER ;
3043   lgr_end_loc    PLS_INTEGER ;
3044   lorg_loc       PLS_INTEGER ;
3045   old_routing_id PLS_INTEGER ;
3046   gen_start_pos  PLS_INTEGER ;
3047   org_start_pos  PLS_INTEGER ;
3048   start_gen_pos_written PLS_INTEGER  ;
3049   start_org_pos_written PLS_INTEGER  ;
3050 
3051 BEGIN
3052   i               := 1 ;
3053   j               := 1 ;
3054   k               := 1 ;
3055   lgr_loc         := 0 ;
3056   lgr_start_loc   := 0 ;
3057   lgr_end_loc     := 0 ;
3058   lorg_loc        := 0 ;
3059   old_routing_id  := 0 ;
3060   gen_start_pos   := 1 ;
3061   org_start_pos   := 1 ;
3062   start_gen_pos_written := 0 ;
3063   start_org_pos_written := 0 ;
3064 
3065   -- gmp_putline(' Begin Link Override Rtg ','a');
3066   FOR i IN 1..routing_headers_size
3067   LOOP
3068    IF rtg_org_hdr_tab(i).routing_id = old_routing_id THEN /* old rtg */
3069 
3070         rtg_org_hdr_tab(i).step_start_loc := lgr_start_loc ;
3071         rtg_org_hdr_tab(i).step_end_loc   := lgr_end_loc ;
3072    ELSE
3073         start_gen_pos_written := 0 ;
3074         FOR j IN gen_start_pos..recipe_override_size
3075         LOOP
3076         IF recipe_override(j).routing_id = rtg_org_hdr_tab(i).routing_id THEN
3077             IF start_gen_pos_written = 0 THEN
3078                 lgr_start_loc := j ;  /* Used for other org in org header */
3079                 rtg_org_hdr_tab(i).step_start_loc := j ;
3080                 start_gen_pos_written := 1 ;
3081             END IF ;
3082             IF j = recipe_override_size THEN
3083                 rtg_org_hdr_tab(i).step_end_loc := j ;
3084                 lgr_end_loc     := j ;
3085             END IF ;
3086 
3087         ELSIF recipe_override(j).routing_id > rtg_org_hdr_tab(i).routing_id
3088         THEN
3089 
3090             IF start_gen_pos_written <> 1 THEN
3091                 rtg_org_hdr_tab(i).step_start_loc := -1 ;
3092                 rtg_org_hdr_tab(i).step_end_loc   := -1 ;
3093                 lgr_start_loc   := - 1;
3094                 lgr_end_loc     := - 1;
3095             ELSE
3096                 lgr_end_loc   := j - 1;
3097                 rtg_org_hdr_tab(i).step_end_loc := lgr_end_loc ;
3098             END IF ;
3099             gen_start_pos := j ;
3100             EXIT ;
3101 
3102         /* ELSE - no need to write, continue looping. */
3103         END IF ;
3104      END LOOP ;   /* Generic loop */
3105    END IF ;   /* old rtg */
3106 
3107      --  For organization recipe
3108      start_org_pos_written := 0 ;
3109      FOR k IN org_start_pos..recipe_orgn_over_size
3110      LOOP
3111       IF rcp_orgn_override(k).routing_id = rtg_org_hdr_tab(i).routing_id AND
3112          rcp_orgn_override(k).organization_id  = rtg_org_hdr_tab(i).organization_id THEN
3113 
3114           IF start_org_pos_written = 0 THEN
3115               rtg_org_hdr_tab(i).usage_start_loc := k ;
3116               start_org_pos_written := 1 ;
3117           END IF ;
3118           IF k = recipe_orgn_over_size THEN
3119               rtg_org_hdr_tab(i).usage_end_loc := k ;
3120           END IF ;
3121 
3122       ELSIF (rcp_orgn_override(k).routing_id>rtg_org_hdr_tab(i).routing_id) OR
3123           (
3124           (rcp_orgn_override(k).routing_id = rtg_org_hdr_tab(i).routing_id) AND
3125           (rcp_orgn_override(k).organization_id  > rtg_org_hdr_tab(i).organization_id)
3126           )  THEN
3127 
3128             IF start_org_pos_written <> 1 THEN
3129               rtg_org_hdr_tab(i).usage_start_loc := -1 ;
3130               rtg_org_hdr_tab(i).usage_end_loc :=  -1 ;
3131             ELSE
3132               rtg_org_hdr_tab(i).usage_end_loc := k - 1 ;
3133             END IF ;
3134             org_start_pos := k ;
3135             EXIT ;
3136 
3137       /* ELSE - no need to write, continue looping. */
3138       END IF ;
3139      END LOOP ;   /* recipe organization loop */
3140 
3141      old_routing_id := rtg_org_hdr_tab(i).routing_id ;
3142   END LOOP ;  /* routing header loop */
3143 
3144   -- gmp_putline(' End Link Override Rtg ','a');
3145 END link_override_routing;
3146 
3147 /*
3148 REM+=========================================================================+
3149 REM| FUNCTION NAME                                                           |
3150 REM|    find_routing_header                                                  |
3151 REM| DESCRIPTION                                                             |
3152 REM|                                                                         |
3153 REM| HISTORY                                                                 |
3154 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
3155 REM+=========================================================================+
3156 */
3157 FUNCTION find_routing_header ( prouting_id      IN PLS_INTEGER,
3158                                porganization_id IN PLS_INTEGER)
3159                                RETURN BOOLEAN IS
3160 
3161 routing_header_loc   PLS_INTEGER ;
3162 BEGIN
3163 routing_header_loc   := 0 ;
3164       routing_header_loc := bsearch_routing (prouting_id,
3165                                              porganization_id);
3166 
3167        IF routing_header_loc > 0 THEN  /* routing header location */
3168 
3169           IF (rtg_org_hdr_tab(routing_header_loc).valid_flag < 0) OR
3170              (rtg_org_hdr_tab(routing_header_loc).generic_start_loc < 0) OR
3171              (rtg_org_hdr_tab(routing_header_loc).orgn_start_loc < 0)  THEN
3172 
3173                 effectivity.rtg_hdr_location :=  -1 ;
3174                 return FALSE ;
3175            ELSE
3176                 effectivity.rtg_hdr_location := routing_header_loc ;
3177                 return TRUE ;
3178           END IF ;
3179        ELSE
3180                 log_message('Bsearch returned negative for Routing Plant ');
3181                 effectivity.rtg_hdr_location :=  -1 ;
3182                 return FALSE ;
3183        END IF ; /* routing header location */
3184 
3185 END find_routing_header;
3186 
3187 /*
3188 REM+=========================================================================+
3189 REM| PROCEDURE NAME                                                          |
3190 REM|    link_routing                                                         |
3191 REM| DESCRIPTION                                                             |
3192 REM|                                                                         |
3193 REM| HISTORY                                                                 |
3194 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
3195 REM+=========================================================================+
3196 */
3197 PROCEDURE link_routing IS
3198   i              PLS_INTEGER ;
3199   j              PLS_INTEGER ;
3200   k              PLS_INTEGER ;
3201   lgr_loc        PLS_INTEGER ;
3202   lgr_start_loc  PLS_INTEGER ;
3203   lgr_end_loc    PLS_INTEGER ;
3204   lorg_loc       PLS_INTEGER ;
3205   old_routing_id PLS_INTEGER ;
3206   gen_start_pos  PLS_INTEGER ;
3207   org_start_pos  PLS_INTEGER ;
3208   start_gen_pos_written PLS_INTEGER  ;
3209   start_org_pos_written PLS_INTEGER  ;
3210 /* NAMIT_CR To link step dependency to routing header */
3211   lstpdep_start_loc  PLS_INTEGER ;
3212   lstpdep_end_loc    PLS_INTEGER ;
3213   stpdep_start_pos  PLS_INTEGER ;
3214   start_stpdep_pos_written PLS_INTEGER  ;
3215 
3216 BEGIN
3217   -- gmp_putline(' Start Link Rtg ','a');
3218   i              := 1 ;
3219   j              := 1 ;
3220   k              := 1 ;
3221   lgr_loc        := 0 ;
3222   lgr_start_loc  := 0 ;
3223   lgr_end_loc    := 0 ;
3224   lorg_loc       := 0 ;
3225   old_routing_id := 0 ;
3226   gen_start_pos  := 1 ;
3227   org_start_pos  := 1 ;
3228   start_gen_pos_written := 0 ;
3229   start_org_pos_written := 0 ;
3230   lstpdep_start_loc  := 0 ;
3231   lstpdep_end_loc    := 0 ;
3232   stpdep_start_pos   := 1 ;
3233   start_stpdep_pos_written := 0 ;
3234 
3235 
3236   FOR i IN 1..routing_headers_size
3237   LOOP
3238    IF rtg_org_hdr_tab(i).routing_id = old_routing_id THEN /* old rtg */
3239 
3240         rtg_org_hdr_tab(i).generic_start_loc := lgr_start_loc ;
3241         rtg_org_hdr_tab(i).generic_end_loc   := lgr_end_loc ;
3242 /* NAMIT_CR Link the Step Dependency to the routing header */
3243         rtg_org_hdr_tab(i).stpdep_start_loc := lstpdep_start_loc ;
3244         rtg_org_hdr_tab(i).stpdep_end_loc   := lstpdep_end_loc ;
3245 
3246    ELSE
3247         start_gen_pos_written := 0 ;
3248         FOR j IN gen_start_pos..rtg_gen_dtl_size
3249         LOOP
3250         IF rtg_gen_dtl_tab(j).routing_id = rtg_org_hdr_tab(i).routing_id THEN
3251             IF start_gen_pos_written = 0 THEN
3252                 lgr_start_loc := j ;  /* Used for other org in org header */
3253                 rtg_org_hdr_tab(i).generic_start_loc := j ;
3254                 start_gen_pos_written := 1 ;
3255             END IF ;
3256             IF j = rtg_gen_dtl_size THEN
3257                 rtg_org_hdr_tab(i).generic_end_loc := j ;
3258                 lgr_end_loc     := j ;
3259             END IF ;
3260 
3261         ELSIF rtg_gen_dtl_tab(j).routing_id > rtg_org_hdr_tab(i).routing_id
3262         THEN
3263 
3264             IF start_gen_pos_written <> 1 THEN
3265                 rtg_org_hdr_tab(i).generic_start_loc := -1 ;
3266                 rtg_org_hdr_tab(i).generic_end_loc   := -1 ;
3267                 lgr_start_loc   := - 1;
3268                 lgr_end_loc     := - 1;
3269             ELSE
3270                 lgr_end_loc   := j - 1;
3271                 rtg_org_hdr_tab(i).generic_end_loc := lgr_end_loc ;
3272             END IF ;
3273             gen_start_pos := j ;
3274             EXIT ;
3275 
3276         /* ELSE - no need to write, continue looping. */
3277         END IF ;
3278      END LOOP ;   /* Generic loop */
3279 /* NAMIT_CR Code To Link Step Dependency to Routing Header Start */
3280 
3281      start_stpdep_pos_written := 0 ;
3282      FOR j IN stpdep_start_pos..opr_stpdep_size
3283      LOOP
3284         IF gmp_opr_stpdep_tbl(j).routing_id = rtg_org_hdr_tab(i).routing_id THEN
3285             IF start_stpdep_pos_written = 0 THEN
3286                 lstpdep_start_loc := j ;  /* Used for other routes in route header */
3287                 rtg_org_hdr_tab(i).stpdep_start_loc := j ;
3288                 start_stpdep_pos_written := 1 ;
3289             END IF ;
3290             IF j = opr_stpdep_size THEN
3291                 rtg_org_hdr_tab(i).stpdep_end_loc := j ;
3292                 lstpdep_end_loc     := j ;
3293             END IF ;
3294 
3295         ELSIF gmp_opr_stpdep_tbl(j).routing_id > rtg_org_hdr_tab(i).routing_id
3296         THEN
3297 
3298             IF start_stpdep_pos_written <> 1 THEN
3299                 rtg_org_hdr_tab(i).stpdep_start_loc := -1 ;
3300                 rtg_org_hdr_tab(i).stpdep_end_loc   := -1 ;
3301                 lstpdep_start_loc   := - 1;
3302                 lstpdep_end_loc     := - 1;
3303             ELSE
3304                 lstpdep_end_loc   := j - 1;
3305                 rtg_org_hdr_tab(i).stpdep_end_loc := lstpdep_end_loc ;
3306             END IF ;
3307             stpdep_start_pos := j ;
3308             EXIT ;
3309         /* ELSE - no need to write, continue looping. */
3310 
3311 	 -- Vpedarla Bug: 8936327 Added the below condition
3312 	ELSIF j = opr_stpdep_size THEN
3313                 lstpdep_start_loc   := - 1;
3314                 lstpdep_end_loc     := - 1;
3315         END IF ;
3316 
3317      END LOOP ;   /* Step Dependency loop */
3318 
3319 /* NAMIT_CR Code To Link Step Dependency to Routing Header End */
3320 
3321    END IF ;   /* old rtg */
3322 
3323      --  For organization routing
3324      start_org_pos_written := 0 ;
3325      For k IN org_start_pos..rtg_org_dtl_size
3326      LOOP
3327       IF rtg_org_dtl_tab(k).routing_id = rtg_org_hdr_tab(i).routing_id AND
3328          rtg_org_dtl_tab(k).organization_id  = rtg_org_hdr_tab(i).organization_id THEN
3329 
3330           IF start_org_pos_written = 0 THEN
3331               rtg_org_hdr_tab(i).orgn_start_loc := k ;
3332               start_org_pos_written := 1 ;
3333           END IF ;
3334           IF k = rtg_org_dtl_size THEN
3335               rtg_org_hdr_tab(i).orgn_end_loc := k ;
3336           END IF ;
3337 
3338       ELSIF (rtg_org_dtl_tab(k).routing_id > rtg_org_hdr_tab(i).routing_id) OR
3339           (
3340           (rtg_org_dtl_tab(k).routing_id = rtg_org_hdr_tab(i).routing_id) AND
3341           (rtg_org_dtl_tab(k).organization_id  > rtg_org_hdr_tab(i).organization_id)
3342           )  THEN
3343 
3344             IF start_org_pos_written <> 1 THEN
3345               rtg_org_hdr_tab(i).orgn_start_loc := -1 ;
3346               rtg_org_hdr_tab(i).orgn_end_loc :=  -1 ;
3347             ELSE
3348               rtg_org_hdr_tab(i).orgn_end_loc := k - 1 ;
3349             END IF ;
3350             org_start_pos := k ;
3351             EXIT ;
3352 
3353       /* ELSE - no need to write, continue looping. */
3354       END IF ;
3355      END LOOP ;   /* Organization loop */
3356 
3357      old_routing_id := rtg_org_hdr_tab(i).routing_id ;
3358 
3359   END LOOP ;  /* routing header loop */
3360 
3361   -- gmp_putline(' End Link Rtg ','a');
3362 END link_routing;
3363 
3364 /*
3365 REM+=========================================================================+
3366 REM| PROCEDURE NAME                                                          |
3367 REM|    validate_routing                                                     |
3368 REM| DESCRIPTION                                                             |
3369 REM|   1. ALL Items in effectivity needs to be convertible to Routing UOM    |
3370 REM|   2. ALL details are present in mtl_system_items with appropriate flags |
3371 REM|                                                                         |
3372 REM| HISTORY                                                                 |
3373 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
3374 REM+=========================================================================+
3375 */
3376 PROCEDURE validate_routing (prouting_id     IN PLS_INTEGER ,
3377                            porganization_id IN PLS_INTEGER,
3378                            pheader_loc      IN PLS_INTEGER,
3379                            prout_valid      OUT NOCOPY BOOLEAN)
3380 IS
3381 
3382   uom_statement           VARCHAR2(9000) ;
3383   old_routingstep_id      PLS_INTEGER ;
3384   old_oprn_no             VARCHAR2(32) ;
3385   old_activity            PLS_INTEGER  ;
3386   i                       INTEGER ;
3387   j                       INTEGER ;
3388   start_genric_count      PLS_INTEGER ;
3389   end_genric_count        PLS_INTEGER ;
3390   start_orgn_count        PLS_INTEGER ;
3391   end_orgn_count          PLS_INTEGER ;
3392   rtg_org_loc             PLS_INTEGER ;
3393   prim_rsrc_cnt           PLS_INTEGER ;
3394   p_uom_qty               NUMBER ;
3395   rtg_valid               BOOLEAN;
3396   found_match             BOOLEAN;
3397   k                       INTEGER;
3398   step_start_index        INTEGER;
3399   step_end_index          INTEGER;
3400   usage_start_index       INTEGER;
3401   usage_end_index         INTEGER;
3402   prev_routingstep_id     NUMBER;   -- B4918786 (RDP) SDS
3403   l_setup_id              NUMBER;   -- B4918786 (RDP) SDS
3404   rtg_recipe_valid        BOOLEAN ;  -- Bug: 8409692 vpedarla
3405 
3406 BEGIN
3407 
3408   uom_statement           := NULL ;
3409   old_routingstep_id      := 0 ;
3410   old_oprn_no             := ' ' ;
3411   old_activity            := -1 ;
3412   i                       := 1 ;
3413   j                       := 1 ;
3414   start_genric_count      := 0 ;
3415   end_genric_count        := 0 ;
3416   start_orgn_count        := 0 ;
3417   end_orgn_count          := 0 ;
3418   rtg_org_loc             := 0 ;
3419   p_uom_qty               := -1 ;
3420   found_match             := TRUE ;
3421   prev_routingstep_id     := NULL ; -- B4918786 (RDP) SDS
3422   l_setup_id              := NULL;  -- B4918786 (RDP) SDS
3423 
3424   prim_rsrc_cnt      := 0 ;
3425   rtg_org_loc        := pheader_loc;
3426   start_genric_count := rtg_org_hdr_tab(rtg_org_loc).generic_start_loc;
3427   end_genric_count   := rtg_org_hdr_tab(rtg_org_loc).generic_end_loc;
3428   start_orgn_count   := rtg_org_hdr_tab(rtg_org_loc).orgn_start_loc;
3429   end_orgn_count     := rtg_org_hdr_tab(rtg_org_loc).orgn_end_loc;
3430   sds_tab  := sds_tab_init ;
3431 
3432 -- Overrides Rajesh {
3433    rtg_valid                 := TRUE ;
3434    k                         := 1;
3435    rtg_recipe_valid          := TRUE ;  -- Bug: 8409692 vpedarla
3436 
3437 
3438 
3439    step_start_index :=
3440       rtg_org_hdr_tab(effectivity.rtg_hdr_location).step_start_loc ;
3441    step_end_index :=
3442       rtg_org_hdr_tab(effectivity.rtg_hdr_location).step_end_loc ;
3443    usage_start_index :=
3444       rtg_org_hdr_tab(effectivity.rtg_hdr_location).usage_start_loc ;
3445    usage_end_index :=
3446       rtg_org_hdr_tab(effectivity.rtg_hdr_location).usage_end_loc ;
3447 
3448 -- Changes for Overrides Rajesh }
3449 
3450    gmp_debug_message('start_genric_count='||start_genric_count||
3451                ' end_genric_count='||end_genric_count||
3452                ' start_orgn_count='||start_orgn_count||
3453                ' end_orgn_count='||end_orgn_count||
3454                ' step_start_index='||step_start_index||
3455                ' step_end_index='||step_end_index||
3456                ' usage_start_index='||usage_start_index||
3457                ' usage_end_index='||usage_end_index
3458                );
3459 
3460    /* Generic routing check */
3461    IF (start_genric_count > 0) AND (end_genric_count > 0 ) THEN
3462    FOR i IN start_genric_count..end_genric_count
3463    LOOP
3464      /* { */
3465       IF (rtg_gen_dtl_tab(i).routing_id = prouting_id) THEN
3466 
3467          /* If operation, activity, step change then */
3468          IF (i = start_genric_count) OR
3469             (rtg_gen_dtl_tab(i).routingstep_id <> old_routingstep_id) OR
3470             (rtg_gen_dtl_tab(i).oprn_no <> old_oprn_no) OR
3471             (rtg_gen_dtl_tab(i).oprn_line_id <>  old_activity)  THEN
3472               prim_rsrc_cnt := 0 ;
3473           END IF ;
3474 
3475           IF rtg_gen_dtl_tab(i).prim_rsrc_ind = 1 THEN
3476              prim_rsrc_cnt := prim_rsrc_cnt + 1 ;
3477           END IF ;
3478 
3479           /* If no primary/ multiple primary resource exit and invalidate
3480              the rtg_header for all the organization */
3481           IF prim_rsrc_cnt <> 1 THEN
3482               --  Routing INVALID We should not do any further processing
3483               prim_rsrc_cnt := 0 ;
3484               invalidate_rtg_all_org(prouting_id) ;
3485               rtg_valid := FALSE;
3486               EXIT ;
3487           END IF ;
3488 
3489           old_routingstep_id := rtg_gen_dtl_tab(i).routingstep_id ;
3490           old_oprn_no        := rtg_gen_dtl_tab(i).oprn_no ;
3491           old_activity       := rtg_gen_dtl_tab(i).oprn_line_id ;
3492 
3493        /* organization check */
3494        IF (start_orgn_count > 0) AND (end_orgn_count > 0 ) AND
3495              (start_orgn_count <= end_orgn_count) THEN
3496        prev_routingstep_id := NULL ;
3497        FOR j IN start_orgn_count..end_orgn_count
3498        LOOP
3499         /* {{ */
3500          IF (rtg_org_dtl_tab(j).organization_id = porganization_id)  AND
3501             (rtg_org_dtl_tab(j).routing_id = prouting_id) THEN
3502 
3503          /* ------------ B4918786 (RDP) STARTS ----------------------*/
3504 
3505          IF (rtg_org_dtl_tab(j).routingstep_id <> nvl(prev_routingstep_id,-1)) THEN
3506 
3507            IF (rtg_org_dtl_tab(j).is_unique = 1) AND (effectivity.category_id > 0) THEN
3508 
3509                l_setup_id := bsearch_setupid(rtg_org_dtl_tab(j).oprn_id,
3510                                  effectivity.category_id);
3511 
3512                IF l_setup_id > 0 THEN
3513                   rtg_org_dtl_tab(j).setup_id := l_setup_id ;
3514                ELSE
3515                  /* The actual SDS changeover data is not established */
3516                  rtg_org_dtl_tab(j).setup_id := NULL ;
3517                END IF;
3518 
3519                gmp_debug_message(' Effectivity ' || effectivity.fmeff_id ||
3520                            ' rouiting ' || rtg_org_dtl_tab(j).routing_id ||
3521                            ' rouitingstep ' || rtg_org_dtl_tab(j).routingstep_id ||
3522                            ' Category ' || effectivity.category_id ||
3523                            ' Setup Id = ' || l_setup_id);
3524 
3525            END IF;
3526 
3527            prev_routingstep_id := rtg_org_dtl_tab(j).routingstep_id ;
3528          END IF;
3529 
3530          IF (rtg_org_dtl_tab(j).is_unique = 1) AND (effectivity.category_id > 0) THEN
3531             sd_index := sd_index + 1 ;
3532             sds_tab(sd_index).oprn_id        := rtg_org_dtl_tab(j).oprn_id ;
3533             sds_tab(sd_index).category_id    := effectivity.category_id    ;
3534             sds_tab(sd_index).seq_dpnd_class := effectivity.seq_dpnd_class ;
3535             sds_tab(sd_index).resources      := rtg_org_dtl_tab(j).resources ;
3536             sds_tab(sd_index).resource_id    := rtg_org_dtl_tab(j).resource_id ;
3537             sds_tab(sd_index).setup_id       := rtg_org_dtl_tab(j).setup_id ;
3538          END IF;
3539          /* ------------ B4918786 (RDP) ENDS -----------------------*/
3540 
3541           /* -------- Get step qty override (RDP) ------------------*/
3542 
3543             rtg_org_dtl_tab(j).o_resource_usage  := -1 ;
3544             rtg_org_dtl_tab(j).o_activity_factor := -1 ;
3545             rtg_org_dtl_tab(j).o_step_qty        := -1 ;
3546             rtg_org_dtl_tab(j).o_process_qty     := -1 ;
3547             rtg_org_dtl_tab(j).o_max_capacity    := -1 ;
3548             rtg_org_dtl_tab(j).o_min_capacity    := -1 ;
3549 
3550             IF (step_start_index > 0) AND (step_end_index > 0) THEN
3551             k := 1 ;
3552             FOR k IN step_start_index..step_end_index
3553             LOOP
3554              IF (effectivity.recipe_id =
3555                    recipe_override(k).recipe_id) THEN
3556 
3557              IF (rtg_org_dtl_tab(j).routing_id =
3558                    recipe_override(k).routing_id) AND
3559                 (rtg_org_dtl_tab(j).routingstep_id =
3560                    recipe_override(k).routingstep_id) THEN
3561 
3562                  rtg_org_dtl_tab(j).o_step_qty :=
3563                     recipe_override(k).step_qty ;
3564                  EXIT ;
3565              END IF ;
3566             ELSIF (effectivity.recipe_id < recipe_override(k).recipe_id) THEN
3567      --     ELSE     Bug: 8409692 Vpedarla modified else to elseif
3568                  rtg_org_dtl_tab(j).o_step_qty := -1 ;
3569                  EXIT ;
3570             END IF; /* Get step qty override */
3571 
3572             END LOOP ;   /* Step Qty Override */
3573             END IF; /* Get step qty override */
3574            /* -------- step qty override Ends (RDP) ------------------*/
3575 
3576             IF (rtg_gen_dtl_tab(i).routingstep_id =
3577                                rtg_org_dtl_tab(j).routingstep_id) AND
3578                (rtg_gen_dtl_tab(i).oprn_line_id =
3579                                rtg_org_dtl_tab(j).oprn_line_id) AND
3580                (rtg_gen_dtl_tab(i).resources =
3581                                 rtg_org_dtl_tab(j).resources) THEN
3582 
3583       /* ------------ Override Calculation Code start ----------------------*/
3584 
3585             IF ((usage_start_index > 0) AND (usage_end_index > 0)) THEN
3586             k := 1 ;
3587               FOR k IN usage_start_index..usage_end_index
3588               LOOP
3589                /* { */
3590                IF (rtg_org_dtl_tab(j).routing_id =
3591                       rcp_orgn_override(k).routing_id) AND
3592                      (rtg_org_dtl_tab(j).organization_id =
3593                       rcp_orgn_override(k).organization_id) AND
3594                      (rtg_org_dtl_tab(j).routingstep_id =
3595                       rcp_orgn_override(k).routingstep_id) AND
3596                      (rtg_org_dtl_tab(j).oprn_line_id =
3597                       rcp_orgn_override(k).oprn_line_id) AND
3598                      (effectivity.recipe_id =
3599                       rcp_orgn_override(k).recipe_id) THEN
3600 
3601                    -- Activity factor override
3602                      rtg_org_dtl_tab(j).o_activity_factor :=
3603                                rcp_orgn_override(k).activity_factor;
3604                    -- Resource Overrides
3605                  /* { */
3606                  IF (rtg_org_dtl_tab(j).resources =
3607                          rcp_orgn_override(k).resources) THEN
3608 
3609                      rtg_org_dtl_tab(j).o_resource_usage :=
3610                             rcp_orgn_override(k).resource_usage;
3611                    -- Process Qty override
3612                      rtg_org_dtl_tab(j).o_process_qty :=
3613                              rcp_orgn_override(k).process_qty ;
3614                    -- Min / Max Capacity Overrides
3615                      rtg_org_dtl_tab(j).o_min_capacity :=
3616                              rcp_orgn_override(k).min_capacity ;
3617                      rtg_org_dtl_tab(j).o_max_capacity :=
3618                              rcp_orgn_override(k).max_capacity ;
3619                  END IF ; /* } Resource Overrides */
3620 
3621                END IF ;  /* }check for routing/step/oprn/recipe */
3622 
3623               END LOOP;  /* Override Loop Ends here */
3624             END IF; /* } check for Override presence */
3625 
3626     IF l_debug = 'Y' THEN
3627      gmp_debug_message ('ROU_ID '||
3628      rtg_org_dtl_tab(j).routing_id ||'*'||' REC_ID '||
3629      effectivity.recipe_id ||'*'||' PR_IND '||
3630      rtg_org_dtl_tab(j).prim_rsrc_ind      ||'*'||' RS_ID '||
3631      rtg_org_dtl_tab(j).routingstep_id         ||' RES '||
3632      rtg_org_dtl_tab(j).resources     ||'* '||
3633      rtg_org_dtl_tab(j).resource_usage      ||' *'||
3634      rtg_org_dtl_tab(j).o_resource_usage      ||' AF '||
3635      rtg_org_dtl_tab(j).activity_factor      ||' *'||
3636      rtg_org_dtl_tab(j).o_activity_factor      ||' SQ '||
3637      rtg_org_dtl_tab(j).step_qty      ||' *'||
3638      rtg_org_dtl_tab(j).o_step_qty     ||' PQ '||
3639      rtg_org_dtl_tab(j).process_qty      ||' *'||
3640      rtg_org_dtl_tab(j).o_process_qty      ||' MNC '||
3641      rtg_org_dtl_tab(j).min_capacity   ||' *'||
3642      rtg_org_dtl_tab(j).o_min_capacity   ||' MXC '||
3643      rtg_org_dtl_tab(j).max_capacity   ||' *'||
3644      rtg_org_dtl_tab(j).o_max_capacity);
3645      END IF;
3646 
3647             IF (rtg_org_dtl_tab(j).prim_rsrc_ind = 1) THEN
3648 
3649                      IF (rtg_org_dtl_tab(j).o_resource_usage = -1 ) THEN
3650 
3651                        IF (rtg_org_dtl_tab(j).resource_usage = 0) THEN
3652                         rtg_valid := FALSE ;
3653         		rtg_org_hdr_tab(rtg_org_loc).valid_flag := -1 ;
3654                         log_message('Recipe ' || effectivity.recipe_id ||' '||
3655                                rtg_org_dtl_tab(j).resources|| ' has usage 0');
3656                         EXIT ;
3657                        END IF;
3658                      ELSIF (rtg_org_dtl_tab(j).o_resource_usage = 0) THEN
3659                      --   rtg_valid := FALSE ;    /* bug: 8409692 Vpedarla */
3660                           rtg_recipe_valid  := FALSE ;
3661                         log_message('Recipe ' || effectivity.recipe_id ||' '||
3662                                rtg_org_dtl_tab(j).resources|| ' has override usage 0');
3663                         EXIT ;
3664                      END IF ;
3665 
3666                      IF (rtg_org_dtl_tab(j).o_activity_factor = -1 ) THEN
3667                        IF (rtg_org_dtl_tab(j).activity_factor = 0) THEN
3668                         rtg_valid := FALSE ;
3669         		rtg_org_hdr_tab(rtg_org_loc).valid_flag := -1 ;
3670                         log_message('Recipe ' || effectivity.recipe_id ||
3671                          ' has ZERO activity factor');
3672                         EXIT;
3673                        END IF;
3674                      ELSIF (rtg_org_dtl_tab(j).o_activity_factor = 0) THEN
3675                      --   rtg_valid := FALSE ;    /* bug: 8409692 Vpedarla */
3676                           rtg_recipe_valid  := FALSE ;
3677                         log_message('Recipe ' || effectivity.recipe_id ||
3678                          ' has ZERO Override activity factor');
3679                         EXIT ;
3680                      END IF ;
3681 
3682                      IF (rtg_org_dtl_tab(j).o_step_qty = -1 ) THEN
3683                        IF (rtg_org_dtl_tab(j).step_qty = 0) THEN
3684                         rtg_valid := FALSE ;
3685         		rtg_org_hdr_tab(rtg_org_loc).valid_flag := -1 ;
3686                         log_message('Recipe ' || effectivity.recipe_id ||
3687                          ' has ZERO step qty');
3688                         EXIT ;
3689                        END IF;
3690                      ELSIF (rtg_org_dtl_tab(j).o_step_qty = 0) THEN
3691                      --   rtg_valid := FALSE ;    /* bug: 8409692 Vpedarla */
3692                           rtg_recipe_valid  := FALSE ;
3693                         log_message('Recipe ' || effectivity.recipe_id ||
3694                          ' has ZERO override step qty');
3695                         EXIT ;
3696                      END IF ;
3697             END IF;  /* For primary resource chack */
3698 
3699 
3700 
3701       /* ------------ Override Calculation Code start ----------------------*/
3702 
3703                 IF ((rtg_org_dtl_tab(j).prim_rsrc_ind = 1
3704 		     OR rtg_org_dtl_tab(j).capacity_constraint = 1) -- akaruppa added to check that chargeable resources are not defined as 'Do Not Plan'
3705                     AND (rtg_org_dtl_tab(j).schedule_ind = 3)) THEN
3706 
3707                     rtg_valid := FALSE;
3708         	    rtg_org_hdr_tab(rtg_org_loc).valid_flag := -1 ;
3709                     log_message('Primary Resource or Chargeable Resource '||rtg_org_dtl_tab(j).resources||
3710                         ' is defined as Do Not Plan '); -- akaruppa added "Chargeable Resource"
3711                     EXIT;
3712                 ELSIF (rtg_org_dtl_tab(j).prim_rsrc_ind <> 1
3713                     AND rtg_org_dtl_tab(j).schedule_ind = 3) THEN
3714 
3715                     start_orgn_count := j + 1 ;
3716                     rtg_org_dtl_tab(j).include_rtg_row := 0;
3717                     EXIT;
3718                 ELSE
3719                     rtg_valid := TRUE ;
3720         	    rtg_org_hdr_tab(rtg_org_loc).valid_flag := 1 ;
3721                     start_orgn_count := j + 1 ;
3722                     EXIT ;
3723                 END IF;
3724 
3725             ELSE
3726               -- Make the rtg invalid ONLY if the Primary or Auxilary
3727               -- resources for any activity is missing
3728 	      -- akaruppa make rtg invalid if a secondary resource which is chargeable is not defined at org level
3729               IF ((rtg_gen_dtl_tab(i).prim_rsrc_ind <> 0)
3730 	          OR ((rtg_gen_dtl_tab(i).prim_rsrc_ind = 0)
3731 		      AND (rtg_gen_dtl_tab(i).capacity_constraint = 1))) THEN
3732                  rtg_valid := FALSE ;
3733                 log_message('Missing Plant Resource '||rtg_gen_dtl_tab(i).resources);
3734       -- gmp_putline('Missing resource ' || rtg_org_dtl_tab(j).resources,'a');
3735               END IF ;
3736               EXIT;
3737             END IF ;
3738 
3739          END IF;
3740          /* }} */
3741 
3742        END LOOP;   /* Orgnization  Loop */
3743        ELSE
3744           -- If there are no organization details , the rtg is invalid
3745           rtg_valid := FALSE ;
3746        END IF;  /* organization check */
3747 
3748          IF ( rtg_valid = FALSE or rtg_recipe_valid = FALSE ) THEN
3749        --  IF  rtg_valid = FALSE  THEN   /* bug: 8409692 Vpedarla */
3750             EXIT ;
3751          END IF ;
3752      END IF;
3753      /* } */
3754    END LOOP ;  /* Generic Loop */
3755    ELSE
3756         -- If no generic routing details present, make routing invalid
3757         rtg_valid := FALSE ;
3758         invalidate_rtg_all_org(prouting_id) ;
3759    END IF;  /* Generic routing check */
3760 
3761    IF rtg_valid THEN
3762         rtg_org_hdr_tab(rtg_org_loc).valid_flag := 1 ;
3763    ELSE
3764         rtg_org_hdr_tab(rtg_org_loc).valid_flag := -1 ;
3765    END IF ;
3766 
3767 --    Bug: 8409692 Vpedarla modified the below code
3768 --     prout_valid := rtg_valid ;
3769    IF rtg_recipe_valid THEN
3770         prout_valid := rtg_valid ;
3771    ELSE
3772         prout_valid := rtg_recipe_valid ;
3773    END IF ;
3774 
3775 END validate_routing ;
3776 
3777 /*
3778 REM+=========================================================================+
3779 REM| PROCEDURE NAME                                                          |
3780 REM|    invalidate_rtg_all_org                                               |
3781 REM| DESCRIPTION                                                             |
3782 REM|                                                                         |
3783 REM| HISTORY                                                                 |
3784 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
3785 REM+=========================================================================+
3786 */
3787 PROCEDURE invalidate_rtg_all_org (p_routing_id IN PLS_INTEGER) IS
3788 
3789   i INTEGER ;
3790 BEGIN
3791   i := 1;
3792    FOR i IN 1..routing_headers_size
3793    LOOP
3794      IF rtg_org_hdr_tab(i).routing_id = p_routing_id THEN
3795            rtg_org_hdr_tab(i).valid_flag := -1 ;
3796      ELSIF rtg_org_hdr_tab(i).routing_id > p_routing_id THEN
3797            EXIT ;
3798      /* ELSE
3799            NULL ;  */
3800      END IF;
3801    END LOOP ;
3802 END invalidate_rtg_all_org;
3803 
3804 /*
3805 REM+=========================================================================+
3806 REM| PROCEDURE NAME                                                          |
3807 REM|    validate_formula                                                     |
3808 REM| DESCRIPTION                                                             |
3809 REM|                                                                         |
3810 REM|   Note that we are going to structure the formula retrieval query       |
3811 REM|   so that only the formulae used in Effectivities are fetched           |
3812 REM|   so trying to validate all at once does not cause any extra work       |
3813 REM|  Summary : Two validations need to be performed                         |
3814 REM|   1. ALL details can be converted to primary UOM                        |
3815 REM|   2. ALL details are present in mtl_system_items with appropriate flags |
3816 REM|                                                                         |
3817 REM| It is now determined that the check for gmp_item_aps and flags therein  |
3818 REM| should NOT be done here , so would be made immediately before inserting |
3819 REM| rows. The same may also be achieved by joining to gmp_item_aps table    |
3820 REM| while getting formula details                                           |
3821 REM|                                                                         |
3822 REM| HISTORY                                                                 |
3823 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
3824 REM+=========================================================================+
3825 */
3826 PROCEDURE validate_formula IS
3827 
3828 i                  INTEGER ;
3829 j                  INTEGER ;
3830 current_dtl_cnt    INTEGER ;
3831 start_pos_written  PLS_INTEGER ;
3832 detail_found       PLS_INTEGER ;
3833 uom_success        BOOLEAN ;
3834 
3835 BEGIN
3836    --  gmp_putline(' Begin validate_formula ','a');
3837 i                  := 1 ;
3838 j                  := 1 ;
3839 current_dtl_cnt    := 1 ;
3840 start_pos_written  := 0 ;
3841 detail_found       := 0 ;
3842 uom_success        := FALSE ;
3843 
3844    FOR i IN 1..formula_headers_size
3845    LOOP
3846      uom_success       := TRUE ;
3847      start_pos_written := 0 ;
3848      detail_found      := 0 ;
3849 
3850      FOR j IN current_dtl_cnt..formula_details_size
3851      LOOP
3852        IF formula_detail_tab(j).formula_id = formula_header_tab(i).formula_id
3853        THEN
3854            detail_found  := 1 ;
3855            IF formula_detail_tab(j).primary_qty < 0  THEN
3856 		uom_success := FALSE ;
3857            ELSE
3858 		uom_success := TRUE ;
3859            END IF;
3860 
3861            --  store the starting detail position
3862            IF start_pos_written = 0 THEN
3863               formula_header_tab(i).start_dtl_loc := j ;
3864               start_pos_written := 1 ;
3865            END IF;
3866 
3867            --  store the ending detail position, if it is the last row
3868            IF j = formula_details_size THEN
3869               formula_header_tab(i).end_dtl_loc := j ;
3870            END IF ;
3871 
3872        ELSIF formula_detail_tab(j).formula_id >
3873                           formula_header_tab(i).formula_id THEN
3874 
3875            --  store the ending detail position
3876            IF start_pos_written <> 1 THEN
3877               formula_header_tab(i).start_dtl_loc := -1 ;
3878               formula_header_tab(i).end_dtl_loc := -1 ;
3879            ELSE
3880               formula_header_tab(i).end_dtl_loc := j - 1 ;
3881            END IF ;
3882            current_dtl_cnt := j ;
3883            EXIT ;
3884 
3885        /* ELSE - no need to write else as it simply has to continue looping. */
3886        END IF ;
3887 
3888      END LOOP ;   /* formula_details_size  */
3889 
3890      IF (detail_found = 1) THEN
3891          IF (uom_success) THEN
3892            formula_header_tab(i).valid_flag := 1 ;
3893          ELSE
3894            formula_header_tab(i).valid_flag := -1 ;
3895            formula_header_tab(i).start_dtl_loc := -1 ;
3896            formula_header_tab(i).end_dtl_loc := -1 ;
3897    	   log_message(
3898                   'UOM Conversion falied for formula ' ||
3899    		  to_char(formula_header_tab(i).formula_id)
3900                   );
3901           END IF ;
3902      ELSE
3903            formula_header_tab(i).valid_flag := -1 ;
3904            formula_header_tab(i).start_dtl_loc := -1 ;
3905            formula_header_tab(i).end_dtl_loc := -1 ;
3906 
3907   /* 	   log_message(
3908                   'Formula detail not found for formula ' ||
3909    		  to_char(formula_header_tab(i).formula_id)
3910                   );
3911   */
3912      END IF ;
3913 
3914    END LOOP ;   /* Formula header loop */
3915 
3916    /* Now validate the formula for all the organizations */
3917    validate_formula_for_orgn ;
3918 
3919    --  gmp_putline(' End validate_formula ','a');
3920 END validate_formula ;
3921 
3922 /*
3923 REM+=========================================================================+
3924 REM| PROCEDURE NAME                                                          |
3925 REM|    validate_formula_for_orgn                                            |
3926 REM| DESCRIPTION                                                             |
3927 REM|    This procedure contains SQL query, but getting executed only once.   |
3928 REM| HISTORY                                                                 |
3929 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
3930 REM| 08/27/2002   Rajesh Patangya - Voltek Customer Fix B2362810             |
3931 REM+=========================================================================+
3932 */
3933 PROCEDURE validate_formula_for_orgn IS
3934 
3935     cur_formula_orgn_count          ref_cursor_typ;
3936     c_formula_dtl_count             ref_cursor_typ;
3937     formula_orgn_count_cursor       VARCHAR2(32767) ;
3938     formula_dtl_count_cursor        VARCHAR2(32767) ;
3939     fm_dtl_orgn_cnt                 INTEGER ;
3940     i                               INTEGER ;
3941 
3942 BEGIN
3943       --  gmp_putline(' start of validate_formula_for_org ','a');
3944 
3945     formula_orgn_count_cursor       := NULL ;
3946     formula_dtl_count_cursor        := NULL ;
3947     fm_dtl_orgn_cnt                 := 1 ;
3948     i                               := 1 ;
3949 
3950    formula_orgn_count_cursor :=
3951                      ' SELECT fmd.formula_id, '
3952                    ||'       msi.organization_id, count(*), 0 '
3953                    ||' FROM  fm_matl_dtl'||at_apps_link||' fmd, '
3954                    ||'       fm_form_mst'||at_apps_link||' ffm, '
3955                    ||'       mtl_system_items'||at_apps_link||' msi, '
3956                    ||'       mtl_parameters'||at_apps_link||' mp '
3957                    ||' WHERE ffm.formula_id = fmd.formula_id '
3958                    ||'   AND ffm.delete_mark = 0 '
3959                    ||'   AND fmd.qty <> 0 '  /* 2362810 Voltek Fix */
3960                    ||'   AND fmd.inventory_item_id = msi.inventory_item_id '
3961 		   ||'   AND msi.recipe_enabled_flag = ''Y'' '
3962                    ||'   AND msi.organization_id = mp.organization_id '
3963 		   ||'   AND mp.process_enabled_flag = ''Y'' ';
3964 
3965 	IF l_in_str_org  IS NOT NULL THEN
3966             formula_orgn_count_cursor := formula_orgn_count_cursor
3967 		   ||' AND mp.organization_id ' || l_in_str_org ;
3968         END IF;
3969 
3970          formula_orgn_count_cursor := formula_orgn_count_cursor
3971                    ||'   AND ( '
3972                    ||'       ( fmd.line_type = -1 ) '
3973                    ||'     OR '
3974                    ||'       ( fmd.line_type IN (1,2) AND '
3975                    ||'         msi.process_execution_enabled_flag = ''Y'' ) '
3976                    ||'       ) '  -- akaruppa added to verify that the products are process execution enabled
3977                    ||' GROUP BY fmd.formula_id, '
3978                    ||'          msi.organization_id, 0 '
3979                    ||' ORDER BY fmd.formula_id, '
3980                    ||'          msi.organization_id ' ;
3981 
3982        -- Get counts for the formulae
3983        formula_dtl_count_cursor :=
3984                      ' SELECT fmd.formula_id, count(*) '
3985                    ||' FROM  fm_matl_dtl'||at_apps_link||' fmd, '
3986                    ||'       fm_form_mst'||at_apps_link||' ffm '
3987                    ||' WHERE ffm.formula_id = fmd.formula_id '
3988                    ||'   AND ffm.delete_mark = 0 '
3989                    ||'   AND fmd.qty <> 0 '   /* 2362810 Voltek Fix */
3990                    ||' GROUP BY fmd.formula_id '
3991 		   ||' ORDER BY fmd.formula_id ' ; /* 4722080 Added Order by */
3992 
3993     OPEN cur_formula_orgn_count FOR formula_orgn_count_cursor;
3994     FETCH cur_formula_orgn_count BULK COLLECT INTO formula_orgn_count_tab;
3995     formula_orgn_size := formula_orgn_count_tab.count;
3996 /* PennColor Bug: 8230710
3997     LOOP
3998     FETCH cur_formula_orgn_count INTO formula_orgn_count_tab(formula_orgn_size);
3999     EXIT WHEN cur_formula_orgn_count%NOTFOUND;
4000 
4001     formula_orgn_size := formula_orgn_size + 1 ;
4002     END LOOP;
4003     */
4004 
4005     CLOSE cur_formula_orgn_count;
4006     --formula_orgn_size := formula_orgn_size -1 ;
4007     time_stamp ;
4008     log_message('Formula Orgn size is = ' || to_char(formula_orgn_size)) ;
4009 
4010     OPEN c_formula_dtl_count FOR formula_dtl_count_cursor ;
4011     FETCH c_formula_dtl_count INTO formula_dtl_count_rec ;
4012      WHILE c_formula_dtl_count%FOUND
4013      LOOP
4014 
4015        FOR i IN fm_dtl_orgn_cnt..formula_orgn_size
4016        LOOP
4017         IF formula_dtl_count_rec.formula_id =
4018                       formula_orgn_count_tab(i).formula_id THEN
4019 
4020              IF formula_dtl_count_rec.formula_dtl_count =
4021                       formula_orgn_count_tab(i).orgn_count THEN
4022                  formula_orgn_count_tab(i).valid_flag := 1 ;
4023              ELSE
4024                  formula_orgn_count_tab(i).valid_flag := -1 ;
4025                  gmp_debug_message(' formula org check failed for formula '||formula_dtl_count_rec.formula_id);
4026              END IF ;
4027 
4028         ELSIF formula_dtl_count_rec.formula_id <
4029                  formula_orgn_count_tab(i).formula_id THEN
4030                  fm_dtl_orgn_cnt := i ;
4031               EXIT ;
4032 
4033         /*  ELSE NULL ;  */
4034         END IF ;
4035        END LOOP ;
4036 
4037      /* Get the next record */
4038      FETCH c_formula_dtl_count INTO formula_dtl_count_rec ;
4039      END LOOP ;
4040      CLOSE c_formula_dtl_count ;
4041      --   gmp_putline(' End of validate_formula_for_org ','a');
4042 
4043 END validate_formula_for_orgn;
4044 
4045 /*
4046 REM+=========================================================================+
4047 REM| FUNCTION NAME                                                           |
4048 REM|    check_formula                                                        |
4049 REM| DESCRIPTION                                                             |
4050 REM|                                                                         |
4051 REM| HISTORY                                                                 |
4052 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
4053 REM+=========================================================================+
4054 */
4055 -- akaruppa removed pplant_code IN parameter
4056 FUNCTION check_formula ( porganization_id IN PLS_INTEGER,
4057                          pformula_id IN PLS_INTEGER) return BOOLEAN IS
4058 
4059 i                 INTEGER ;
4060 p_organization_id PLS_INTEGER ;
4061 p_formula_id      PLS_INTEGER ;
4062 
4063 BEGIN
4064 
4065 i                 := 1 ;
4066 p_organization_id := porganization_id;
4067 p_formula_id      := pformula_id;
4068 
4069 FOR i in g_fm_hdr_loc..formula_headers_size
4070 LOOP
4071 
4072 	IF  formula_header_tab(i).formula_id = pformula_id THEN
4073 	    IF formula_header_tab(i).valid_flag = 1 THEN
4074 		-- Note down formula_header location to be used
4075 		-- while writing the bom
4076 
4077 		g_fm_dtl_start_loc := formula_header_tab(i).start_dtl_loc ;
4078 		g_fm_dtl_end_loc := formula_header_tab(i).end_dtl_loc ;
4079         gmp_debug_message('start -'||g_fm_dtl_start_loc||' end -'||g_fm_dtl_end_loc);
4080 	        IF check_formula_for_organization (p_organization_id ,
4081                                                    p_formula_id) THEN -- akaruppa removed p_plant_code
4082 		  g_fm_hdr_loc := i ;
4083                   return TRUE ;
4084 
4085                 ELSE
4086 		  g_fm_hdr_loc := i ;
4087 		  gmp_debug_message(' check_formula_for_organization failed');
4088                   return FALSE ;
4089 
4090                 END IF;
4091             ELSE
4092 		  g_fm_hdr_loc := i ;
4093 		  gmp_debug_message(' check_formula valid_flag failed');
4094                   return FALSE ;
4095 	    END IF ;  /* Header validation */
4096 	ELSIF formula_header_tab(i).formula_id > pformula_id THEN
4097 		g_fm_hdr_loc := i ;
4098 		gmp_debug_message(' check_formula failed');
4099 		return FALSE ;
4100         /* ELSE
4101              NULL ;   */
4102 	END IF ;
4103 END LOOP ;
4104 return FALSE ;
4105 END check_formula ;
4106 
4107 /*
4108 REM+=========================================================================+
4109 REM| FUNCTION NAME                                                           |
4110 REM|    check_formula_for_organization                                       |
4111 REM| DESCRIPTION                                                             |
4112 REM|                                                                         |
4113 REM| HISTORY                                                                 |
4114 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
4115 REM+=========================================================================+
4116 */
4117 FUNCTION check_formula_for_organization (
4118                          porganization_id IN PLS_INTEGER,
4119                          pformula_id IN PLS_INTEGER) return BOOLEAN IS
4120 i            INTEGER;
4121 BEGIN
4122 i            := 1 ;
4123 
4124 FOR i IN g_formula_orgn_count_tab..formula_orgn_count_tab.COUNT
4125 LOOP
4126   IF formula_orgn_count_tab(i).formula_id = pformula_id THEN
4127 
4128         IF formula_orgn_count_tab(i).organization_id = porganization_id THEN
4129           IF formula_orgn_count_tab(i).valid_flag = 1 THEN
4130              g_formula_orgn_count_tab := i ;
4131              return TRUE ;
4132           ELSE
4133              g_formula_orgn_count_tab := i ;
4134              return FALSE ;
4135           END IF;
4136         ELSIF formula_orgn_count_tab(i).organization_id > porganization_id THEN
4137              g_formula_orgn_count_tab := i ;
4138              return FALSE ;
4139            /* ELSE
4140                  NULL ;  */
4141         END IF;  /* Organizatin ID */
4142   ELSIF formula_orgn_count_tab(i).formula_id > pformula_id THEN
4143            g_formula_orgn_count_tab := i ;
4144            return FALSE ;
4145   /* ELSE
4146     NULL ;  */
4147   END IF;
4148 END LOOP ;
4149    return FALSE ;
4150 END check_formula_for_organization ;
4151 
4152 /*
4153 REM+=========================================================================+
4154 REM| FUNCTION NAME                                                           |
4155 REM|    bsearch_routing                                                      |
4156 REM| DESCRIPTION                                                             |
4157 REM|                                                                         |
4158 REM| HISTORY                                                                 |
4159 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
4160 REM+=========================================================================+
4161 */
4162 FUNCTION bsearch_routing (p_routing_id      IN PLS_INTEGER ,
4163 			  p_organization_id IN PLS_INTEGER)
4164 			RETURN INTEGER IS
4165 
4166 top     INTEGER ;
4167 bottom  INTEGER ;
4168 mid     INTEGER ;
4169 
4170 BEGIN
4171      top    := 1;
4172      bottom := routing_headers_size ;
4173      mid    := -1 ;
4174    WHILE  (top <= bottom )
4175     LOOP
4176      mid := top + ( ( bottom - top ) / 2 );
4177 
4178      IF p_routing_id < rtg_org_hdr_tab(mid).routing_id OR
4179   	(p_routing_id = rtg_org_hdr_tab(mid).routing_id AND
4180 	 p_organization_id < rtg_org_hdr_tab(mid).organization_id ) THEN
4181 	bottom := mid -1 ;
4182      ELSIF
4183 	p_routing_id > rtg_org_hdr_tab(mid).routing_id OR
4184         (p_routing_id = rtg_org_hdr_tab(mid).routing_id AND
4185          p_organization_id > rtg_org_hdr_tab(mid).organization_id ) THEN
4186 	top := mid + 1 ;
4187      ELSE
4188 	-- We can do the checking for the validity etc here
4189 	-- OR just return the location to calling function and
4190 	-- let the calling function do rest of the work
4191 	RETURN mid ;
4192      END IF ;
4193     END LOOP;
4194     -- Not found
4195     Return -1 ;
4196 END bsearch_routing ;
4197 /*
4198 REM+=========================================================================+
4199 REM| FUNCTION NAME                                                           |
4200 REM|    bsearch_setupid  SGIDUGU                                             |
4201 REM| DESCRIPTION                                                             |
4202 REM|                                                                         |
4203 REM| HISTORY                                                                 |
4204 REM+=========================================================================+
4205 */
4206 FUNCTION bsearch_setupid (p_oprn_id       IN PLS_INTEGER ,
4207                           p_category_id   IN PLS_INTEGER
4208                          ) RETURN INTEGER IS
4209 top     INTEGER ;
4210 bottom  INTEGER ;
4211 mid     INTEGER ;
4212 
4213 BEGIN
4214      top    := 1;
4215      bottom := setup_size ;
4216      mid    := -1 ;
4217 --
4218    WHILE  (top <= bottom )
4219     LOOP
4220      mid := top + ( ( bottom - top ) / 2 );
4221 
4222      IF p_oprn_id < setupid_tab(mid).oprn_id OR
4223   	(p_oprn_id = setupid_tab(mid).oprn_id AND
4224 	 p_category_id < setupid_tab(mid).category_id ) THEN
4225 	bottom := mid -1 ;
4226      ELSIF
4227 	p_oprn_id > setupid_tab(mid).oprn_id OR
4228         (p_oprn_id = setupid_tab(mid).oprn_id AND
4229          p_category_id > setupid_tab(mid).category_id ) THEN
4230 	top := mid + 1 ;
4231      ELSE
4232 	RETURN setupid_tab(mid).seq_dep_id ;
4233      END IF ;
4234     END LOOP;
4235     -- Not found
4236     Return -1 ;
4237 END bsearch_setupid ;
4238 /*
4239 REM+=========================================================================+
4240 REM| PROCEDURE NAME                                                          |
4241 REM|    write_process_effectivities                                          |
4242 REM|                                                                         |
4243 REM| TYPE                                                                    |
4244 REM|    Private                                                              |
4245 REM|                                                                         |
4246 REM| USAGE                                                                   |
4247 REM|    This procedure creates the effectivty rows in gmp_form_eff and       |
4248 REM|    msc_process_effectivities                                            |
4249 REM|                                                                         |
4250 REM| DESCRIPTION                                                             |
4251 REM|                                                                         |
4252 REM|                                                                         |
4253 REM| INPUT PARAMETERS                                                        |
4254 REM|    None                                                                 |
4255 REM|                                                                         |
4256 REM| OUTPUT PARAMETERS                                                       |
4257 REM|    return_status    TRUE => OK                                          |
4258 REM|                                                                         |
4259 REM| INPUT/OUTPUT PARAMETERS                                                 |
4260 REM|    None                                                                 |
4261 REM|                                                                         |
4262 REM| HISTORY                                                                 |
4263 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
4264 REM|  06/02/2003   Sridhar Gidugu  Checked aps_fmeff_id before               |
4265 REM|                               inserts - B2989806                        |
4266 REM+=========================================================================+
4267 */
4268 PROCEDURE write_process_effectivity
4269 (
4270   p_x_aps_fmeff_id   IN PLS_INTEGER,
4271   p_aps_fmeff_id     IN PLS_INTEGER,
4272   return_status      OUT NOCOPY BOOLEAN
4273 )
4274 IS
4275   statement_form_eff  VARCHAR2(9000);
4276   loop_index          INTEGER;
4277   routing_id          PLS_INTEGER;
4278   temp_recipe         VARCHAR2(240);
4279 
4280 BEGIN
4281   statement_form_eff  := NULL ;
4282   temp_recipe         := NULL ;
4283 
4284 /* B2989806  Added IF condition below */
4285 IF effectivity.aps_fmeff_id = -1 THEN
4286 
4287     statement_form_eff :=
4288 	          'INSERT INTO gmp_form_eff'||at_apps_link
4289 		   ||' ( '
4290 		   ||'  aps_fmeff_id,organization_id,fmeff_id, '
4291                    ||'  formula_id, routing_id, '
4292 		   ||'  creation_date, created_by, last_update_date, '
4293                    ||'  last_updated_by '
4294 		   ||' ) '
4295 		   ||' VALUES '
4296 		   ||' ( :p1,:p2,:p3,:p4,:p5,:p6,:p7,:p8,:p9)';
4297 
4298              /* This aps_fmeff_id the next sequence ID, but not multiplied by
4299                 2 and added by 1 */
4300     EXECUTE IMMEDIATE statement_form_eff USING
4301 		   p_aps_fmeff_id,
4302 		   effectivity.organization_id, -- akaruppa added
4303 		   effectivity.fmeff_id,
4304 		   effectivity.formula_id,
4305 		   effectivity.routing_id,
4306 		   current_date_time,
4307 		   0,
4308 		   current_date_time,
4309 		   0;
4310 
4311 END IF ; /* New effectivity row to be created */
4312 
4313         /* Process Effectivity Bulk Insert assignment */
4314 
4315            pef_index := pef_index + 1 ;
4316 
4317            -- Bug: 8715318 Vpedarla uncommented the below line
4318 	   /* B5584507 */
4319 	   temp_recipe :=
4320 	   effectivity.recipe_no || delimiter || to_char(effectivity.recipe_version) ;
4321 
4322            pef_process_sequence_id(pef_index) :=   p_x_aps_fmeff_id ;
4323            pef_item_id(pef_index) :=  effectivity.inventory_item_id ;
4324            pef_organization_id(pef_index) :=  effectivity.organization_id ;
4325            pef_effectivity_date(pef_index) :=  effectivity.start_date ;
4326 
4327            IF effectivity.end_date IS NOT NULL THEN
4328                 pef_disable_date(pef_index) :=  effectivity.end_date ;
4329            ELSE
4330                 pef_disable_date(pef_index) := null_value ;
4331            END IF;
4332 
4333            pef_minimum_quantity(pef_index) :=  effectivity.inv_min_qty ;
4334            pef_maximum_quantity(pef_index) :=  effectivity.inv_max_qty ;
4335            pef_preference(pef_index)       :=  effectivity.preference ;
4336            pef_routing_sequence_id(pef_index) :=  p_x_aps_fmeff_id ;
4337            pef_bill_sequence_id(pef_index)    :=  p_x_aps_fmeff_id ;
4338            pef_sr_instance_id(pef_index) :=  instance_id ;
4339 
4340            -- Bug: 8715318 Vpedarla uncommented the below line
4341 	   pef_recipe(pef_index)         :=  temp_recipe ;  /* B5584507 */
4342            -- pef_deleted_flag(pef_index)     := 2;
4343            pef_last_update_date(pef_index) := current_date_time ;
4344            -- bom_last_updated_by(pef_index)  := 0 ;
4345            pef_creation_date(pef_index)    := current_date_time ;
4346            -- pef_created_by(pef_index)       := 0;
4347 
4348   return_status := TRUE;
4349 
4350   EXCEPTION
4351     WHEN OTHERS THEN
4352        log_message('Write Process Effectivity Raised Exception: '||sqlerrm);
4353        log_message(to_char(effectivity.fmeff_id));
4354        return_status := FALSE;
4355 END write_process_effectivity;
4356 
4357 /*
4358 REM+=========================================================================+
4359 REM| PROCEDURE NAME                                                          |
4360 REM|    write_bom_components                                                 |
4361 REM|                                                                         |
4362 REM| TYPE                                                                    |
4363 REM|    Private                                                              |
4364 REM|                                                                         |
4365 REM| USAGE                                                                   |
4366 REM|    This procedure creates the bill of material components in msc_boms   |
4367 REM|                                                                         |
4368 REM| DESCRIPTION                                                             |
4369 REM|                                                                         |
4370 REM|                                                                         |
4371 REM| INPUT PARAMETERS                                                        |
4372 REM|    None                                                                 |
4373 REM|                                                                         |
4374 REM| OUTPUT PARAMETERS                                                       |
4375 REM|    return_status    TRUE => OK                                          |
4376 REM|                                                                         |
4377 REM| INPUT/OUTPUT PARAMETERS                                                 |
4378 REM|    None                                                                 |
4379 REM|                                                                         |
4380 REM| HISTORY                                                                 |
4381 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
4382 REM|  08/27/2002 - B2098058 Alternate_bomdesignator is being passed          |
4383 REM|               as the eff_id because 1. Alt_rtg_desgn is now required    |
4384 REM|               per explanation in the bug 2.alt_rtg_desgn should be same |
4385 REM|               alt_bom_desgn for the bom in the same eff 3.OPM has       |
4386 REM|               has no way to determine primary bom/rtg from alternate    |
4387 REM|                                                                         |
4388 REM+=========================================================================+
4389 */
4390 PROCEDURE write_bom_components
4391 (
4392   p_x_aps_fmeff_id   IN PLS_INTEGER,
4393   return_status      OUT NOCOPY BOOLEAN
4394 )
4395 IS
4396   temp_assembly_comment   VARCHAR2(240) ;
4397   primary_bom_written     PLS_INTEGER ;
4398   p_primary_qty           NUMBER ;
4399   loop_index              PLS_INTEGER;
4400   l_scale_type            INTEGER;
4401   l_offset_loc            NUMBER ;
4402   l_offset                NUMBER ;
4403   l_line_type             INTEGER;
4404   rtgstpno_loc            NUMBER;
4405   temp_alt_bom_desig      VARCHAR2(40) ;  /* B5584507 */
4406 
4407 BEGIN
4408 
4409   temp_assembly_comment   := NULL ;
4410   primary_bom_written     := 0 ;
4411   p_primary_qty           := 0 ;
4412   l_offset_loc            := 0 ;
4413   l_offset                := 0 ;
4414   l_line_type             := 0 ;
4415   rtgstpno_loc            := -1;
4416   temp_alt_bom_desig      := NULL ;  /* B5584507 */
4417 
4418   -- ABHAY write the code to get the offset percentages here.
4419   -- The code will loop through the formula_detail_tab  from
4420   -- g_fm_dtl_start_loc to g_fm_dtl_end_loc and update the field offset
4421   FOR loop_index IN g_fm_dtl_start_loc..g_fm_dtl_end_loc
4422   LOOP
4423 
4424      /* Do write a row for the primary produc */
4425 
4426    IF (effectivity.inventory_item_id = formula_detail_tab(loop_index).inventory_item_id) AND
4427       (formula_detail_tab(loop_index).line_type = 1)  THEN
4428       IF primary_bom_written = 0 THEN
4429          /* WRITE_BOM : Do write a row for the primary product   */
4430         BEGIN
4431         /*B2870041 save the index of the product it will be used when writing
4432            the route and its details */
4433         effectivity.product_index := loop_index;
4434 
4435        temp_assembly_comment :=substr(
4436 	 formula_detail_tab(loop_index).formula_no ||delimiter||
4437 	 to_char(formula_detail_tab(loop_index).formula_vers) ||delimiter||
4438 	 formula_detail_tab(loop_index).formula_desc1,1,240) ;/**bug14480509 field widening enhancement-michael add substr(str,1,240) here  **/
4439 
4440         /* Bug: 8608111 Vpedarla Uncommented below code */
4441          /* B5584507 */
4442 	 temp_alt_bom_desig :=
4443          substr( formula_detail_tab(loop_index).formula_no,1, (10 - length(delimiter||to_char(formula_detail_tab(loop_index).formula_vers)))) ||delimiter||  to_char(formula_detail_tab(loop_index).formula_vers) ;
4444 
4445          /* BOM Bulk Insert assignments */
4446 
4447          bom_index := bom_index + 1 ;
4448          bom_bill_sequence_id(bom_index) := p_x_aps_fmeff_id ;
4449          bom_sr_instance_id(bom_index)   := instance_id ;
4450          bom_organization_id(bom_index)  := effectivity.organization_id ;
4451          bom_assembly_item_id(bom_index) := effectivity.inventory_item_id ;
4452          -- bom_assembly_type(bom_index)    := 1 ;
4453 
4454          /* Bug: 8608111 Vpedarla commented below code to pass temp_alt_bom_desig*/
4455          IF l_12137_count <> 0 and l_apps_ver <> 6 THEN -- Biogen start
4456 	        bom_alternate_bom_designator(bom_index)  := effectivity.recipe_no || delimiter || to_char(effectivity.recipe_version) || delimiter ||
4457 	                                              formula_detail_tab(loop_index).formula_no || delimiter|| to_char(formula_detail_tab(loop_index).formula_vers);
4458 				 ELSE
4459           bom_alternate_bom_designator(bom_index)  := p_x_aps_fmeff_id ;
4460          END IF; -- Biogen end
4461          -- bom_alternate_bom_designator(bom_index)  := temp_alt_bom_desig ;
4462 
4463          -- bom_alternate_bom_designator(bom_index)  := temp_alt_bom_desig ;
4464          bom_specific_assembly_comment(bom_index) :=  temp_assembly_comment ;
4465 
4466        /* Bug: 7385050 Vpedarla added the below condition to send the scale_type 2 for proportional and 0 for fixed  */
4467         IF formula_detail_tab(loop_index).scale_type = 0 THEN
4468                 l_scale_type := 0 ;
4469         ELSIF formula_detail_tab(loop_index).scale_type = 1 THEN
4470                 l_scale_type := 2 ;
4471         ELSE
4472                  /* scale type of other than 0,1,2 is not supported */
4473                  l_scale_type := formula_detail_tab(loop_index).scale_type ;
4474         END IF ;
4475         bom_scaling_type(bom_index)    := l_scale_type ;
4476       --   bom_scaling_type(bom_index)    :=
4477        --                      formula_detail_tab(loop_index).bom_scale_type ;
4478        /* Bug: 7385050 Vpedarla */
4479 
4480          bom_assembly_quantity(bom_index)  :=
4481                             formula_detail_tab(loop_index).primary_qty ;
4482          bom_uom(bom_index)  := formula_detail_tab(loop_index).primary_uom_code ;
4483 /* NAMIT_CR For Step Material Assoc */
4484 /* Used enhanced binary search to get the location for routing
4485     step number of product. */
4486 
4487             rtgstpno_loc :=
4488                enh_bsearch_stpno (effectivity.formula_id, effectivity.recipe_id,
4489                   effectivity.inventory_item_id); -- akaruppa changed effectivity.item_id to effectivity.inventory_item_id
4490 
4491 --
4492 
4493             IF(rtgstpno_loc > 0) THEN
4494                bom_op_seq_number(bom_index) := mat_assoc_tab(rtgstpno_loc).routingstep_no;
4495             ELSE
4496                 bom_op_seq_number(bom_index) := null_value ;
4497             END IF;
4498 
4499 --         bom_op_seq_number(bom_index) := formula_detail_tab(loop_index).routingstep_no;
4500 
4501          -- bom_deleted_flag(bom_index)     := 2;
4502          bom_last_update_date(bom_index) := current_date_time ;
4503          -- bom_last_updated_by(bom_index)  := 0 ;
4504          bom_creation_date(bom_index)    := current_date_time ;
4505          -- bom_created_by(bom_index)       := 0;
4506 
4507          EXCEPTION
4508               WHEN OTHERS THEN
4509               log_message('Error writing to msc_st_boms: '||sqlerrm);
4510               /* B3837959 MMK Issue, set return status to false */
4511               return_status := FALSE;
4512          END;
4513           primary_bom_written := 1 ;
4514       -- 01/17/2003 Rajesh Patangya B2740767
4515       ELSE      /* Primary BOM written */
4516            /* Primary product written and now co-prod is same as prod
4517               note that the co-products always contribute to yield and
4518               scale type can only be fixed or linear per restrictions in GMD
4519            */
4520 	-- Now get the offset percentage
4521 	-- -------------------------------
4522 	IF g_curr_rstep_loc > 0  AND
4523 	(formula_detail_tab(loop_index).release_type between  1 AND 3)
4524 	THEN
4525 	 l_offset_loc := get_offsets( effectivity.formula_id,
4526 				effectivity.organization_id, -- akaruppa previously effectivity.plant_code
4527 		formula_detail_tab(loop_index).x_formulaline_id ) ;
4528 	 IF l_offset_loc < 0 THEN
4529           IF formula_detail_tab(loop_index).line_type < 0 THEN
4530                 l_offset := 0 ;
4531           ELSE
4532                 l_offset := 100 ;
4533           END IF ;
4534 	 ELSE
4535           IF formula_detail_tab(loop_index).line_type < 0 THEN
4536                 l_offset := rstep_offsets(l_offset_loc).start_offset ;
4537           ELSE
4538                 l_offset := rstep_offsets(l_offset_loc).end_offset ;
4539           END IF ;
4540 
4541 	 END IF ;
4542 	ELSE
4543 	  IF formula_detail_tab(loop_index).line_type < 0 THEN
4544 		l_offset := 0 ;
4545 	  ELSE
4546 		l_offset := 100 ;
4547 	  END IF ;
4548 	END IF ;
4549 	-- -------------------------------
4550 
4551          BEGIN   /* co-product */
4552          /* BOM Component Bulk Insert assignments */
4553 
4554 /* VPEDARLA BUG: 7348022 */
4555     /* B2657068 Scailing type decision Rajesh Patangya */
4556     /*  Scale type in material detail 0-Fixed, 1-proportional 2-Integer */
4557 
4558     IF formula_detail_tab(loop_index).contribute_yield_ind = 'Y' THEN
4559        IF formula_detail_tab(loop_index).scale_type = 0 THEN
4560           l_scale_type := 0 ;
4561        ELSIF formula_detail_tab(loop_index).scale_type = 1 THEN
4562           l_scale_type := 1 ;
4563        ELSIF  formula_detail_tab(loop_index).scale_type = 2 THEN
4564           l_scale_type := 4 ;
4565        ELSE
4566          /* scale type of other than 0,1,2 is not supported */
4567          l_scale_type := formula_detail_tab(loop_index).scale_type ;
4568        END IF ;
4569     ELSE
4570        IF formula_detail_tab(loop_index).scale_type = 0 THEN
4571           l_scale_type := 2 ;
4572        ELSIF formula_detail_tab(loop_index).scale_type = 1 THEN
4573           l_scale_type := 3 ;
4574        ELSIF  formula_detail_tab(loop_index).scale_type = 2 THEN
4575           l_scale_type := 5 ;
4576        ELSE
4577          /* scale type of other than 0,1,2 is not supported */
4578          l_scale_type := formula_detail_tab(loop_index).scale_type ;
4579        END IF ;
4580     END IF ;           /* IF contribute_yield_ind */
4581 
4582 /*  VPEDARLA Bug: 7348022 */
4583 
4584          bomc_index := bomc_index + 1 ;
4585          bomc_component_sequence_id(bomc_index) := formula_detail_tab(loop_index).x_formulaline_id ;
4586          bomc_sr_instance_id(bomc_index)   := instance_id ;
4587          bomc_organization_id(bomc_index)  := effectivity.organization_id ;
4588          bomc_Inventory_item_id(bomc_index) := formula_detail_tab(loop_index).inventory_item_id ;
4589          bomc_using_assembly_id(bomc_index) := effectivity.inventory_item_id ;
4590          bomc_bill_sequence_id(bomc_index) := p_x_aps_fmeff_id ;
4591          bomc_component_type(bomc_index) := 10 ;  /* for co-proudcts */
4592          bomc_scaling_type(bomc_index) := l_scale_type; /* Scailing type for APS */
4593            -- bomc_change_notice(i)  == null
4594            -- bomc_revision(i),  == null
4595          bomc_uom_code(bomc_index) := formula_detail_tab(loop_index).primary_uom_code ;
4596          bomc_usage_quantity(bomc_index) :=  (-1 * formula_detail_tab(loop_index).primary_qty) ;
4597         -- Bug: 8624603 Vpedarla
4598         bomc_effectivity_date(bomc_index) := trunc(effectivity.start_date) ;
4599       --   bomc_effectivity_date(bomc_index) := current_date_time ;
4600          /* NAMIT_OC For ingredients contribute_to_step_qty will
4601          store 1 for YES and 0 for NO */
4602          bomc_disable_date(bomc_index) := null_value;
4603          /*B5176291 - item substitution changes - disabale date should eb initalised to null */
4604          bomc_contribute_to_step_qty(bomc_index) := formula_detail_tab(loop_index).contribute_step_qty_ind;
4605            -- bomc_disable_date := null_value,
4606            -- bomc_from_unit_number := null_value,
4607            -- bomc_to_unit_number := null_value,
4608            -- bomc_use_up_code := null_value,
4609            -- bomc_suggested_effectivity_date := null_value,
4610            -- bomc_driving_item_id := null_value,
4611          IF l_offset IS NOT NULL THEN
4612            bomc_opr_offset_percent(bomc_index) := l_offset; /* offset percentage */
4613          ELSE
4614            bomc_opr_offset_percent(bomc_index) := null_value ;
4615          END IF;
4616 
4617          bomc_optional_component(bomc_index) := 2 ;
4618            -- bomc_old_effectivity_date := null_value,
4619          bomc_wip_supply_type(bomc_index) := formula_detail_tab(loop_index).phantom_type ;
4620            -- bomc_planning_factor := null_value,
4621            -- bomc_atp_flag := 1,
4622            -- bomc_component_yield_factor := 1,
4623            -- deleted_flag := 2,
4624          bomc_last_update_date(bomc_index) := current_date_time ;
4625            -- bomc_last_updated_by(bomc_index)  := 0 ;
4626          bomc_creation_date(bomc_index)    := current_date_time ;
4627            -- bomc_created_by(bomc_index)       := 0;
4628          IF  formula_detail_tab(loop_index).scale_multiple IS NOT NULL THEN
4629            bomc_scale_multiple(bomc_index) := formula_detail_tab(loop_index).scale_multiple ;
4630          ELSE
4631            bomc_scale_multiple(bomc_index) := null_value;
4632          END IF;
4633          IF formula_detail_tab(loop_index).scale_rounding_variance IS NOT NULL THEN
4634            bomc_scale_rounding_variance(bomc_index) :=
4635                      formula_detail_tab(loop_index).scale_rounding_variance ;
4636          ELSE
4637            bomc_scale_rounding_variance(bomc_index) := null_value;
4638          END IF;
4639          IF formula_detail_tab(loop_index).rounding_direction IS NOT NULL THEN
4640            bomc_rounding_direction(bomc_index) :=
4641                          formula_detail_tab(loop_index).rounding_direction ;
4642          ELSE
4643            bomc_rounding_direction(bomc_index) := null_value ;
4644          END IF;
4645 
4646          EXCEPTION
4647               WHEN OTHERS THEN
4648               log_message('Error co-products to msc_st_bom_comp: '||sqlerrm);
4649               /* B3837959 MMK Issue, set return status to false */
4650               return_status := FALSE;
4651          END ;   /* co-product */
4652 
4653       END IF ;  /* Primary BOM written */
4654 
4655    ELSE
4656 
4657      /* Do write all formula detail lines except primary product */
4658 
4659        IF formula_detail_tab(loop_index).line_type = -1 THEN
4660            p_primary_qty := formula_detail_tab(loop_index).primary_qty;
4661            /* B2559881, scrap_factor introduced */
4662            p_primary_qty := p_primary_qty *
4663             (1 + nvl(formula_detail_tab(loop_index).scrap_factor,0));
4664        ELSE
4665            p_primary_qty := (-1) * formula_detail_tab(loop_index).primary_qty;
4666            /* B2559881, scrap_factor introduced */
4667            p_primary_qty := p_primary_qty *
4668             (1 + nvl(formula_detail_tab(loop_index).scrap_factor,0));
4669        END IF;
4670 
4671        /* B3452524, If co-prodcut or by product is not same as product then
4672           component type should be 10 */
4673 
4674 	IF formula_detail_tab(loop_index).line_type = 1 THEN
4675 	  l_line_type := 10 ;
4676 	ELSE
4677 	  l_line_type := formula_detail_tab(loop_index).line_type ;
4678 	END IF ;
4679 
4680 
4681     /* B2657068 Scailing type decision Rajesh Patangya */
4682     /*  Scale type in material detail 0-Fixed, 1-proportional 2-Integer */
4683 
4684     IF formula_detail_tab(loop_index).contribute_yield_ind = 'Y' THEN
4685        IF formula_detail_tab(loop_index).scale_type = 0 THEN
4686           l_scale_type := 0 ;
4687        ELSIF formula_detail_tab(loop_index).scale_type = 1 THEN
4688           l_scale_type := 1 ;
4689        ELSIF  formula_detail_tab(loop_index).scale_type = 2 THEN
4690           l_scale_type := 4 ;
4691        ELSE
4692          /* scale type of other than 0,1,2 is not supported */
4693          l_scale_type := formula_detail_tab(loop_index).scale_type ;
4694        END IF ;
4695     ELSE
4696        IF formula_detail_tab(loop_index).scale_type = 0 THEN
4697           l_scale_type := 2 ;
4698        ELSIF formula_detail_tab(loop_index).scale_type = 1 THEN
4699           l_scale_type := 3 ;
4700        ELSIF  formula_detail_tab(loop_index).scale_type = 2 THEN
4701           l_scale_type := 5 ;
4702        ELSE
4703          /* scale type of other than 0,1,2 is not supported */
4704          l_scale_type := formula_detail_tab(loop_index).scale_type ;
4705        END IF ;
4706     END IF ;           /* IF contribute_yield_ind */
4707 	-- Now get the offsets
4708 	-- -------------------------------
4709 	IF g_curr_rstep_loc > 0  AND
4710 	(formula_detail_tab(loop_index).release_type between 1 AND 3)
4711 	THEN
4712 	 l_offset_loc := get_offsets( effectivity.formula_id,
4713 				effectivity.organization_id, -- akaruppa previously effectivity.plant_code
4714 		formula_detail_tab(loop_index).x_formulaline_id ) ;
4715 	 IF l_offset_loc < 0 THEN
4716           IF formula_detail_tab(loop_index).line_type < 0 THEN
4717                 l_offset := 0 ;
4718           ELSE
4719                 l_offset := 100 ;
4720           END IF ;
4721 	 ELSE
4722           IF formula_detail_tab(loop_index).line_type < 0 THEN
4723                 l_offset := rstep_offsets(l_offset_loc).start_offset ;
4724           ELSE
4725                 l_offset := rstep_offsets(l_offset_loc).end_offset ;
4726           END IF ;
4727 
4728 	 END IF ;
4729 	ELSE
4730 	  IF formula_detail_tab(loop_index).line_type < 0 THEN
4731 		l_offset := 0 ;
4732 	  ELSE
4733 		l_offset := 100 ;
4734 	  END IF ;
4735 	END IF ;
4736 	-- -----------------------------------
4737 
4738    /* B3267522, Rajesh Patangya Do not insert ingredients, if ingredient is same
4739       as product (single level circular reference) */
4740 
4741     IF (effectivity.inventory_item_id = formula_detail_tab(loop_index).inventory_item_id) AND
4742       (formula_detail_tab(loop_index).line_type = -1)  THEN
4743       NULL ;
4744     ELSE
4745         IF (trunc(nvl(formula_detail_tab(loop_index).end_date,current_date_time + 1)) >  trunc(current_date_time)) THEN /* Currently active rows should only be passed*/
4746                  /* BOM Component Bulk Insert assignments */
4747                  bomc_index := bomc_index + 1 ;
4748 
4749                 /*B5176291 - item subtitution changes - start*/
4750                 /*For sustitutes the formula line id will be null. component sequence id in
4751                 msc_st_bom_components is a primary key. So the max value from gmd formula line sequence
4752                 is fetched and global counter value is added to it.Then odd value is passed on to APS. */
4753                  IF formula_detail_tab(loop_index).x_formulaline_id IS NOT NULL THEN
4754                         bomc_component_sequence_id(bomc_index) := formula_detail_tab(loop_index).x_formulaline_id ;
4755                  ELSE
4756                         gmd_formline_cnt := gmd_formline_cnt + 1;
4757                         bomc_component_sequence_id(bomc_index) := (( v_gmd_formula_lineid + gmd_formline_cnt ) * 2) + 1;
4758                  END IF;
4759                 /*B5176291 - item subtitution changes - end*/
4760 
4761                  bomc_sr_instance_id(bomc_index)   := instance_id ;
4762                  bomc_organization_id(bomc_index)  := effectivity.organization_id ;
4763                  bomc_Inventory_item_id(bomc_index) := formula_detail_tab(loop_index).inventory_item_id ;
4764                  -- RDP B2445746, replace component aps_item_id to product aps_item_id
4765                  bomc_using_assembly_id(bomc_index) := effectivity.inventory_item_id ;
4766                  bomc_bill_sequence_id(bomc_index) := p_x_aps_fmeff_id ;
4767                  bomc_component_type(bomc_index) := l_line_type ;
4768                  bomc_scaling_type(bomc_index) := l_scale_type; /* Scailing type for APS */
4769                    -- bomc_change_notice(i)  == null
4770                    -- bomc_revision(i),  == null
4771                  bomc_uom_code(bomc_index) := formula_detail_tab(loop_index).primary_uom_code ;
4772                  bomc_usage_quantity(bomc_index) :=  p_primary_qty ;
4773 
4774                 /*B5176291 - Item substitution - start*/
4775                 IF ( formula_detail_tab(loop_index).end_date IS NULL
4776                 AND formula_detail_tab(loop_index).start_date IS NULL ) THEN
4777                        -- Bug: 8624603 Vpedarla
4778                        bomc_effectivity_date(bomc_index) := trunc(effectivity.start_date) ;
4779                       --  bomc_effectivity_date(bomc_index) := trunc(current_date_time) ;
4780                         bomc_disable_date(bomc_index) := null_value;
4781                 ELSE
4782                         IF formula_detail_tab(loop_index).start_date IS NULL THEN
4783                            -- Bug: 8624603 Vpedarla
4784                             bomc_effectivity_date(bomc_index) := trunc(effectivity.start_date) ;
4785                              --   bomc_effectivity_date(bomc_index) := trunc(current_date_time);
4786                         ELSE
4787                         /*B5176291 - Truncate the time from the date part and then pass on
4788                         this to APS. Round up the day when the time is not equal to 00:00 and pass
4789                         on ( date + 1 ) */
4790 
4791                         -- Vpedarla Bug:6087535 commented below line
4792                            bomc_effectivity_date(bomc_index) := trunc(formula_detail_tab(loop_index).start_date);
4793                         /*
4794                                 IF (loop_index > 1) AND (bomc_index > 1 ) AND (
4795                                  ( formula_detail_tab(loop_index).formula_id =
4796                                            formula_detail_tab(loop_index - 1).formula_id ) AND
4797                                  ( formula_detail_tab(loop_index).formula_line_id =
4798                                            formula_detail_tab(loop_index - 1).formula_line_id ) AND
4799                                  (( bomc_disable_date(bomc_index - 1) >
4800                                          trunc(formula_detail_tab(loop_index).start_date) )
4801                                    OR
4802                                    (
4803                                    ((sign(formula_detail_tab(loop_index - 1).end_date -
4804                                         trunc(formula_detail_tab(loop_index - 1).end_date)) = 1 ) AND
4805                                    (current_date_time > formula_detail_tab(loop_index - 1).end_date))
4806                                    )
4807                                  )) THEN
4808                                            bomc_effectivity_date(bomc_index) :=
4809                                                  trunc(formula_detail_tab(loop_index).start_date) + 1;
4810                                                  log_message('--- 3 truncating the start date +1 '|| to_char(formula_detail_tab(loop_index).start_date, 'dd-mon-yy hh24:mi:ss'));
4811                                 ELSE
4812                                            bomc_effectivity_date(bomc_index) :=
4813                                                   trunc(formula_detail_tab(loop_index).start_date);
4814                                                   log_message('--- 4 truncating the start date  '|| to_char(formula_detail_tab(loop_index).start_date, 'dd-mon-yy hh24:mi:ss'));
4815                                 END IF;
4816 
4817                          */
4818                         END IF;
4819 
4820                         -- Rajesh Patangya 5058669, If Null then do not use it
4821                         IF formula_detail_tab(loop_index).end_date IS NOT NULL THEN
4822                         -- vpedarla  Bug:6087535 commented below lines
4823                         -- commented back the half fix for 6087535 for charter bug 11854449
4824                         -- bomc_disable_date(bomc_index) := trunc(formula_detail_tab(loop_index).end_date);
4825                                  get_sign := sign(formula_detail_tab(loop_index).end_date
4826                                       - trunc(formula_detail_tab(loop_index).end_date));
4827                                 IF ( get_sign = 1 ) THEN
4828                                         bomc_disable_date(bomc_index) := trunc(formula_detail_tab(loop_index).end_date) + 1;
4829                                    --   log_message('--- 3 truncating the end date +1 '|| to_char(formula_detail_tab(loop_index).end_date, 'dd-mon-yy hh24:mi:ss'));
4830                                 ELSE
4831                                         bomc_disable_date(bomc_index) := trunc(formula_detail_tab(loop_index).end_date);
4832                                     --  log_message('--- 3 truncating the start date  '|| to_char(formula_detail_tab(loop_index).end_date, 'dd-mon-yy hh24:mi:ss'));
4833                                 END IF;
4834                         ELSE
4835                                 bomc_disable_date(bomc_index) := null_value;
4836                         END IF;
4837                 END IF;
4838                 /*B5176291 - Item substitution - end*/
4839 
4840                  /* NAMIT_OC For ingredients contribute_to_step_qty will
4841                  store 1 for YES and 0 for NO */
4842                  bomc_contribute_to_step_qty(bomc_index) := formula_detail_tab(loop_index).contribute_step_qty_ind;
4843                    -- bomc_disable_date := null_value,
4844                    -- bomc_from_unit_number := null_value,
4845                    -- bomc_to_unit_number := null_value,
4846                    -- bomc_use_up_code := null_value,
4847                    -- bomc_suggested_effectivity_date := null_value,
4848                    -- bomc_driving_item_id := null_value,
4849                  IF l_offset IS NOT NULL THEN
4850                    bomc_opr_offset_percent(bomc_index) := l_offset; /* offset percentage */
4851                  ELSE
4852                    bomc_opr_offset_percent(bomc_index) := null_value ;
4853                  END IF;
4854 
4855                  bomc_optional_component(bomc_index) := 2 ;
4856                    -- bomc_old_effectivity_date := null_value,
4857                  bomc_wip_supply_type(bomc_index) := formula_detail_tab(loop_index).phantom_type ;
4858                    -- bomc_planning_factor := null_value,
4859                    -- bomc_atp_flag := 1,
4860                    -- bomc_component_yield_factor := 1,
4861                    -- deleted_flag := 2,
4862 
4863                  bomc_last_update_date(bomc_index) := current_date_time ;
4864                    -- bomc_last_updated_by(bomc_index)  := 0 ;
4865                  bomc_creation_date(bomc_index)    := current_date_time ;
4866                    -- bomc_created_by(bomc_index)       := 0;
4867                  /* B2657068 Rajesh Patangya */
4868                  IF  formula_detail_tab(loop_index).scale_multiple IS NOT NULL THEN
4869                    bomc_scale_multiple(bomc_index) := formula_detail_tab(loop_index).scale_multiple ;
4870                  ELSE
4871                    bomc_scale_multiple(bomc_index) := null_value;
4872                  END IF;
4873                  IF formula_detail_tab(loop_index).scale_rounding_variance IS NOT NULL THEN
4874                    bomc_scale_rounding_variance(bomc_index) :=
4875                              formula_detail_tab(loop_index).scale_rounding_variance ;
4876                  ELSE
4877                    bomc_scale_rounding_variance(bomc_index) := null_value;
4878                  END IF;
4879                  IF formula_detail_tab(loop_index).rounding_direction IS NOT NULL THEN
4880                    bomc_rounding_direction(bomc_index) := formula_detail_tab(loop_index).rounding_direction ;
4881                  ELSE
4882                    bomc_rounding_direction(bomc_index) := null_value ;
4883                  END IF;
4884         END IF; /* Currently active rows should only be passed*/
4885     END IF;   /* Circular reference */
4886 
4887    END IF;
4888 
4889   END LOOP;
4890   return_status := TRUE;
4891 
4892   EXCEPTION
4893     WHEN OTHERS THEN
4894 	log_message('Error writing to msc_st_bom_components: '||sqlerrm);
4895 	return_status := FALSE;
4896 END write_bom_components;
4897 
4898 /*
4899 REM+=========================================================================+
4900 REM| PROCEDURE NAME                                                          |
4901 REM|    write_routing                                                        |
4902 REM|                                                                         |
4903 REM| TYPE                                                                    |
4904 REM|    Private                                                              |
4905 REM|                                                                         |
4906 REM| USAGE                                                                   |
4907 REM|    This procedure creates a routing in msc_routings                     |
4908 REM|                                                                         |
4909 REM| DESCRIPTION                                                             |
4910 REM|                                                                         |
4911 REM|                                                                         |
4912 REM| INPUT PARAMETERS                                                        |
4913 REM|    table_index      index into aps_effectivities structure for current  |
4914 REM|                     effectivity.                                        |
4915 REM|                                                                         |
4916 REM| OUTPUT PARAMETERS                                                       |
4917 REM|    return_status    TRUE => OK                                          |
4918 REM|                                                                         |
4919 REM| INPUT/OUTPUT PARAMETERS                                                 |
4920 REM|    None                                                                 |
4921 REM|                                                                         |
4922 REM| HISTORY                                                                 |
4923 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
4924 REM|  08/27/2002   B2098058 Alternate_routing_designator is being passed     |
4925 REM|               as the eff_id because 1. Alt_rtg_desgn is now required    |
4926 REM|               per explanation in the bug 2.alt_rtg_desgn should be same |
4927 REM|               alt_bom_desgn for the bom in the same eff 3.OPM has       |
4928 REM|               has no way to determine primary bom/rtg from alternate    |
4929 REM|                                                                         |
4930 REM+=========================================================================+
4931 */
4932 PROCEDURE write_routing
4933 (
4934   p_x_aps_fmeff_id   IN PLS_INTEGER,
4935   return_status      OUT NOCOPY BOOLEAN
4936 )
4937 IS
4938   p_routing_details  VARCHAR2(128);
4939   temp_alt_rtg_desig VARCHAR2(40);   /* B5584507 */
4940   v_routing_qty      NUMBER;
4941 BEGIN
4942   p_routing_details  := NULL;
4943   temp_alt_rtg_desig := NULL;  /* B5584507 */
4944   v_routing_qty      := 0;
4945 
4946   IF effectivity.rtg_hdr_location > 0 AND effectivity.product_index > 0 THEN
4947 
4948     p_routing_details := effectivity.routing_no ||delimiter||
4949                          to_char(effectivity.routing_vers) ||delimiter||
4950                          effectivity.routing_desc ;
4951 
4952     /*B2870041 The routing qty needs to be represented as the product of the
4953        effectivity so APS can scale the route correctly. If the product has
4954        fixed scaling then the route will need to use the full product qty
4955        as the route will not get scaled according to total output in APS.
4956        Otherwise the quantity will be the product qty scaled to match
4957        the total output if represented as the routing qty*/
4958     IF formula_detail_tab(effectivity.product_index).scale_type = 0 THEN
4959       v_routing_qty :=
4960         formula_detail_tab(effectivity.product_index).primary_qty;
4961     ELSE
4962       v_routing_qty := (effectivity.routing_qty /
4963         formula_header_tab(g_fm_hdr_loc).total_output) *
4964         formula_detail_tab(effectivity.product_index).primary_qty;
4965     END IF;
4966 
4967     /* B2870041 report the uom as the primary uom of the product in the discrete
4968       form. The quantity is the scaled version of the product to match the
4969       routing qty as explained previously */
4970 
4971           /* Routing Bulk insert assignments */
4972             rtg_index := rtg_index + 1 ;
4973 
4974 
4975 
4976         /* Bug: 8608111 Vpedarla Uncommented below code */
4977          /* B5584507 */
4978 	 temp_alt_rtg_desig := substr( effectivity.routing_no, 1,
4979                        10- length(delimiter||to_char(to_char(effectivity.routing_vers))))
4980         ||delimiter|| to_char(effectivity.routing_vers) ;
4981 
4982 
4983             rtg_routing_sequence_id(rtg_index) := p_x_aps_fmeff_id ;
4984             rtg_sr_instance_id(rtg_index) := instance_id ;
4985               -- rtg_routing_type(rtg_index) := 1 ;
4986             rtg_routing_comment(rtg_index) := p_routing_details ;
4987 
4988 	    -- Bug: 8608111 Vpedarla commented the below code to pass temp_alt_rtg_desig
4989 	    IF l_12137_count <> 0 and l_apps_ver <> 6 THEN -- VKINDURI Biogen start
4990 	      rtg_alt_routing_designator(rtg_index) := effectivity.recipe_no || delimiter || to_char(effectivity.recipe_version) || delimiter || effectivity.routing_no || delimiter || to_char(effectivity.routing_vers);
4991 	    ELSE
4992 	      rtg_alt_routing_designator(rtg_index) := p_x_aps_fmeff_id ; /* B2098058 */
4993 	    END IF; -- VKINDURI Biogen end
4994 
4995            --  rtg_alt_routing_designator(rtg_index) := temp_alt_rtg_desig;
4996 
4997 	    -- p_x_aps_fmeff_id ; /* B2098058 */
4998               -- project_id :=  null_value ;
4999               -- task_id :=  null_value ;
5000               -- line_id :=  null_value ;
5001             /*B2870041*/
5002             rtg_uom_code(rtg_index) := formula_detail_tab(effectivity.product_index).primary_uom_code ;
5003               -- cfm_routing_flag := null_value ;
5004               -- ctp_flag := null_value ;
5005             /*B2870041*/
5006             rtg_routing_quantity(rtg_index) := v_routing_qty ;
5007             rtg_assembly_item_id(rtg_index) := effectivity.inventory_item_id ;
5008             rtg_organization_id(rtg_index) := effectivity.organization_id ;
5009 /* NAMIT_CR Calculate Step Quantities */
5010             rtg_auto_step_qty_flag(rtg_index) := effectivity.calculate_step_quantity ;
5011 
5012               -- deleted_flag  = 2 ;
5013             rtg_last_update_date(rtg_index) := current_date_time ;
5014             rtg_creation_date(rtg_index) := current_date_time ;
5015 
5016     return_status := TRUE;
5017   ELSE
5018     return_status := FALSE;
5019   END IF ;
5020 
5021 /* NAMIT_CR Write Step Dependency Data. */
5022   write_step_dependency(p_x_aps_fmeff_id);
5023 
5024   EXCEPTION
5025     WHEN OTHERS THEN
5026 	log_message('Error writing to msc_st_routings: '||sqlerrm);
5027 	return_status := FALSE;
5028 END write_routing;
5029 
5030 /*
5031 REM+=========================================================================+
5032 REM| PROCEDURE NAME                                                          |
5033 REM|    write_routing_operations                                             |
5034 REM|                                                                         |
5035 REM| TYPE                                                                    |
5036 REM|    Private                                                              |
5037 REM|                                                                         |
5038 REM| DESCRIPTION                                                             |
5039 REM|    This procedure writes operation/resource/activity details to the MSC |
5040 REM|    tables and also caters for alternate resources.                      |
5041 REM|                                                                         |
5042 REM| INPUT PARAMETERS                                                        |
5043 REM|    table_index: index of APS effectivity in aps_effectivities           |
5044 REM|                                                                         |
5045 REM| OUTPUT PARAMETERS                                                       |
5046 REM|    return_status  TRUE=> OK                                             |
5047 REM|                                                                         |
5048 REM| INPUT/OUTPUT PARAMETERS                                                 |
5049 REM|    None                                                                 |
5050 REM|                                                                         |
5051 REM| HISTORY                                                                 |
5052 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
5053 REM|  12/12/2002   Abhay Satpute   - Resource Load Changes Bug# 2710139      |
5054 REM|  01/22/2003   Rajesh Patangya - Resource Unit = resource_count B2761278 |
5055 REM|  05/23/2003   Sridhar Gidugu  - Populating Activity Group Id using      |
5056 REM|                                 Resource Id bug#2975261                 |
5057 REM+=========================================================================+
5058 */
5059 PROCEDURE write_routing_operations
5060 (
5061   p_x_aps_fmeff_id   IN PLS_INTEGER,
5062   return_status      OUT NOCOPY BOOLEAN
5063 )
5064 IS
5065 
5066   start_index          INTEGER;
5067   end_index            INTEGER;
5068   loop_index           INTEGER;
5069   k                    INTEGER;
5070   alt_cnt              INTEGER;
5071   previous_id          PLS_INTEGER;
5072   previous_activity    PLS_INTEGER ;
5073   seq_no               INTEGER;
5074   statement_no         INTEGER;
5075   v_counter            INTEGER;
5076   alternates_inserted  VARCHAR2(1);
5077   v_alternate          PLS_INTEGER;
5078   t_scale_type         PLS_INTEGER;
5079 
5080   f_step_qty           NUMBER ;
5081   f_resource_usage     NUMBER ;
5082   f_activity_factor    NUMBER ;
5083   f_process_qty        NUMBER ;
5084 /* NAMIT_OC */
5085   f_min_capacity       NUMBER ;
5086   f_max_capacity       NUMBER ;
5087 
5088   calculated_resource_usage NUMBER ;
5089 
5090   prod_scale_factor    NUMBER ; /*B2870041 contains factor to scale usage */
5091   l_prod_scale_factor  NUMBER ;
5092   temp_min_xfer_qty    NUMBER ; /*B2870041*/
5093 
5094 /* For SDS Enhancement */
5095    l_seq_dep_class     VARCHAR2(8);
5096    orig_rs_seq_num     NUMBER ;
5097    u_setup_id          PLS_INTEGER ;
5098 
5099    oprn_leadtime_start  INTEGER ;  -- Vpedarla 7391495
5100    oprn_leadtime_end    INTEGER ;  -- Vpedarla 7391495
5101    lead_loop            INTEGER ;  -- Vpedarla 7391495
5102 
5103 BEGIN
5104 
5105   k                     := 0;
5106   alt_cnt               := 0;
5107   previous_id           := 0;
5108   previous_activity     := -1;
5109   seq_no                := 1;
5110   statement_no          := 0;
5111   v_counter             := 0;
5112   alternates_inserted   := 'N';
5113   v_alternate           := 0;
5114   t_scale_type          := -1;
5115 
5116   f_step_qty            := 0;
5117   f_resource_usage      := 0;
5118   f_activity_factor     := 0;
5119   f_process_qty         := 0;
5120 /* NAMIT_OC */
5121   f_min_capacity        := 0;
5122   f_max_capacity        := 999999;
5123 
5124   calculated_resource_usage := 0;
5125 
5126   prod_scale_factor     := 1; /*B2870041 contains factor to scale usage */
5127   l_prod_scale_factor   := 1;
5128   temp_min_xfer_qty     := 0; /*B2870041*/
5129 
5130   orig_rs_seq_num      := 0;
5131   u_setup_id           := NULL ;
5132 
5133 -- Vpedarla 7391495
5134    oprn_leadtime_start := 0;
5135    oprn_leadtime_end   := 0;
5136    lead_loop           := 0;
5137 
5138   statement_no := 0 ;
5139 
5140   start_index := rtg_org_hdr_tab(effectivity.rtg_hdr_location).orgn_start_loc ;
5141   end_index :=  rtg_org_hdr_tab(effectivity.rtg_hdr_location).orgn_end_loc ;
5142 
5143 --
5144   /*B2870041 If the product has fixed scaling the route will need to be scaled
5145      to match the total output not the product. If the product is linear
5146      then the route can be scaled in APS. The routing qty in the route header
5147      was modified to match the original routing qty. The product qty
5148       was scaled to match that value, thus the factor will always be 1 */
5149 
5150   statement_no := 10 ;
5151   /* B3145206, No matter what scale type it is, factor needs to be calculated */
5152     l_prod_scale_factor := formula_header_tab(g_fm_hdr_loc).total_output/
5153         effectivity.routing_qty;
5154   IF formula_detail_tab(effectivity.product_index).scale_type = 0 THEN
5155  	prod_scale_factor := l_prod_scale_factor ;
5156   ELSE
5157     	prod_scale_factor := 1;
5158   END IF;
5159 
5160   IF (start_index > 0) AND (end_index > 0) THEN
5161 
5162   FOR loop_index IN start_index..end_index
5163   LOOP
5164   /* Write only non Do Not Plan rows and rows in which usage UOM
5165   and GMP UOM for Hours profile have Time as base UOM class. */
5166   IF (rtg_org_dtl_tab(loop_index).include_rtg_row = 1) THEN
5167     t_scale_type  := rtg_org_dtl_tab(loop_index).rtg_scale_type ;
5168 
5169     -- Routing Step insertion
5170     IF rtg_org_dtl_tab(loop_index).routingstep_id <> previous_id THEN
5171 
5172     /*B4359481 - sowsubra - CHG: MTQ VALUES ARE NOT COLLECTED - Uncommented this part of code */
5173     /*This piece of code was commented as part of APS, as the step dependency was sent as part of
5174     APS K enhancement.*/
5175 
5176      IF  rtg_org_dtl_tab(loop_index).step_qty = 0 THEN
5177        temp_min_xfer_qty := 0 ;
5178      ELSE
5179    -- B6873825 Rajesh Patangya
5180   --   temp_min_xfer_qty :=  rtg_org_dtl_tab(loop_index).minimum_transfer_qty ;
5181      temp_min_xfer_qty := (effectivity.routing_qty *
5182        formula_detail_tab(effectivity.product_index).primary_qty *
5183        rtg_org_dtl_tab(loop_index).minimum_transfer_qty) /
5184        (formula_header_tab(g_fm_hdr_loc).total_output *
5185         rtg_org_dtl_tab(loop_index).step_qty);
5186      END IF;
5187 
5188      /*B2870041 the mtq quantity needs to be represented as a value based on the
5189         product not the step qty. Since a formula can have multiple products
5190         in different effectivities the calculation will use the relationhip
5191         of the mtq based on the step qty as applied to the product */
5192 
5193 -- Vpedarla Bug: 7391495
5194    search_operation_leadtime(effectivity.fmeff_id ,effectivity.organization_id ,
5195                oprn_leadtime_start, oprn_leadtime_end );
5196 
5197   IF ( oprn_leadtime_start > 0 ) THEN
5198       statement_no := 11 ;
5199    FOR i IN oprn_leadtime_start..oprn_leadtime_end
5200    LOOP
5201          statement_no := 12;
5202      IF (oper_leadtime_percent(i).routingstep_id = rtg_org_dtl_tab(loop_index).routingstep_id ) THEN
5203            lead_loop  := i ;
5204            EXIT ;
5205      END IF;
5206      IF (oper_leadtime_percent(i).routingstep_id > rtg_org_dtl_tab(loop_index).routingstep_id) THEN
5207            EXIT ;
5208      END IF;
5209    END LOOP ;
5210   END IF;
5211 -- Vpedarla Bug: 7391495
5212 
5213     statement_no := 15 ;
5214 
5215   statement_no := 20 ;
5216     -- Routing Step Bulk insert assignments
5217        opr_index := opr_index + 1 ;
5218        opr_operation_sequence_id(opr_index) :=
5219                                       rtg_org_dtl_tab(loop_index).x_routingstep_id ;
5220        opr_routing_sequence_id(opr_index) := p_x_aps_fmeff_id ;
5221        opr_operation_seq_num(opr_index) := rtg_org_dtl_tab(loop_index).routingstep_no ;
5222        opr_sr_instance_id(opr_index) := instance_id ;
5223        opr_operation_description(opr_index) := rtg_org_dtl_tab(loop_index).oprn_desc ;
5224        opr_effectivity_date(opr_index) := current_date_time ;
5225 
5226         -- Vpedarla 7391495
5227        IF (lead_loop > 0) THEN
5228         opr_lead_time_percent(opr_index) := oper_leadtime_percent(lead_loop).start_offset ;
5229        ELSE
5230         opr_lead_time_percent(opr_index) := NULL;
5231        END IF;
5232 
5233          -- disable_date,from_unit_number, to_unit_number, := null ;
5234          -- option_dependent_flag := 1,
5235          -- operation_type := null_value ;
5236        opr_mtransfer_quantity(opr_index) := temp_min_xfer_qty;   /*B2870041*/
5237 
5238        /* NAMIT_ASQC
5239        l_prod_scale_factor is Scale factor based on fm total output qty and rtg qty.
5240        (l_prod_scale_factor = formula_header_tab.total_output/effectivity.routing_qty)
5241        Also added the Step Qty UOM. Discrete UOM is obtained from Sy_UOM_Mst */
5242 --       opr_step_qty(opr_index) := l_prod_scale_factor * rtg_org_dtl_tab(loop_index).step_qty;
5243 
5244       /* Step Qty calculated In correctly */
5245 
5246      -- Bug: 8354988 Vpedarla added if condition to check for recipe step qty overrides
5247       IF (rtg_org_dtl_tab(loop_index).o_step_qty  > 0) THEN
5248          opr_step_qty(opr_index) := prod_scale_factor * rtg_org_dtl_tab(loop_index).o_step_qty / l_prod_scale_factor  ;
5249       ELSE
5250          opr_step_qty(opr_index) := prod_scale_factor * rtg_org_dtl_tab(loop_index).step_qty;
5251       END IF;
5252    --    opr_step_qty(opr_index) := prod_scale_factor * rtg_org_dtl_tab(loop_index).step_qty;
5253 
5254        opr_step_qty_uom(opr_index) := rtg_org_dtl_tab(loop_index).process_qty_uom;
5255          -- yield := null_value ; /*  B2365684 rtg_org_dtl_tab(loop_index).step_qty, */
5256 
5257        opr_department_id(opr_index) := (effectivity.organization_id * 2) + 1 ;
5258        opr_organization_id(opr_index) := effectivity.organization_id  ;
5259        opr_department_code(opr_index) := effectivity.organization_code ;
5260          --  operation_lead_time_percent,cumulative_yield, := null ;
5261          -- reverse_cumulative_yield,net_planning_percent, := null;
5262          -- setup_duration,tear_down_duration, := null ;
5263        /*B2870041*/
5264        opr_uom_code(opr_index) := formula_detail_tab(effectivity.product_index).primary_uom_code ;
5265          -- standard_operation_code := null_value,
5266        opr_last_update_date(opr_index) := current_date_time ;
5267        opr_creation_date(opr_index) := current_date_time ;
5268 
5269      previous_id := rtg_org_dtl_tab(loop_index).routingstep_id;
5270      previous_activity := -1;
5271      seq_no := 0;
5272     END IF;   /* routing Step Insertion */
5273 
5274     --  Activity Insertion
5275   statement_no := 30 ;
5276     IF rtg_org_dtl_tab(loop_index).oprn_line_id <> previous_activity THEN
5277 
5278        seq_no := seq_no + 1;
5279 
5280      /*B2870041 modified the population of schedule_flag. The value of this will
5281         come from the plsql table and not hardcoded to 1. The value was set
5282         in the proceesing for the routing details */
5283 
5284      /* B2975261 - Populating activity group id using resource id */
5285 
5286      /* Operation resource seqs Bulk Insert assignments */
5287        rs_index := rs_index + 1 ;
5288        rs_operation_sequence_id(rs_index) := rtg_org_dtl_tab(loop_index).x_routingstep_id ;
5289 --       rs_resource_seq_num(rs_index) := seq_no ;
5290        /* B3596028 */
5291        rs_resource_seq_num(rs_index) := rtg_org_dtl_tab(loop_index).seq_dep_ind ;
5292        rs_sr_instance_id(rs_index)   := instance_id ;
5293        rs_department_id(rs_index)    := (effectivity.organization_id * 2) + 1 ;
5294          -- rs_resource_offset_percent := null_value ;
5295        /*B2870041 */
5296        rs_schedule_flag(rs_index)    := rtg_org_dtl_tab(loop_index).schedule_flag ;
5297        rs_routing_sequence_id(rs_index) := p_x_aps_fmeff_id ;
5298        rs_organization_id(rs_index)    := effectivity.organization_id ;
5299          -- deleted_flag := 2 ;
5300        rs_last_update_date(rs_index) := current_date_time ;
5301        rs_creation_date(rs_index) := current_date_time ;
5302        rs_activity_group_id(rs_index) := rtg_org_dtl_tab(loop_index).x_resource_id ;
5303 
5304       previous_activity := rtg_org_dtl_tab(loop_index).oprn_line_id;
5305 
5306     END IF;  /* End if for Activity Change */
5307 
5308     /*
5309     New Changes for Alternate Resources Begin : Bug# 1319610
5310     The following code depends on the ordering of prim_rsrc_ind, right
5311     now the Secondary Resources are Not considered, and the
5312     Primary_rsrc_indicator will have
5313               1 for Primary Resource
5314               2 for a Auxilary Resource.
5315               0 for a Secondary Resource.
5316               The Logic in brief goes like this :
5317               The Resources are inserted as usual and then a check is made
5318               to find if the resource is a Primary resource and if it has
5319               any alternates,
5320               the Alternate Resources are inserted. Then the groups secondaries
5321               are inserted.
5322      Insert the Resources : Bug# 1319610
5323      mfc 12-01-99 changed scale type to 0>2 1>1
5324      */
5325 
5326       f_step_qty  := 0;
5327       f_activity_factor    := 0 ;
5328       f_process_qty        := 0 ;
5329       f_resource_usage     := 0 ;
5330 /* NAMIT_OC */
5331       f_min_capacity       := 0 ;
5332       f_max_capacity       := 999999;
5333 
5334       -- Get process_qty,activity_factor override
5335       statement_no := 80 ;
5336       IF (rtg_org_dtl_tab(loop_index).o_step_qty  > 0) THEN
5337      /* B3145206, Note that the overriden qty is with respect to the total o/p
5338         and not with respect to just the product, let's have an example
5339         rtg_qty = 40 , step_qty = 40 , but when used
5340         in a formula having prod = 40 and by-prod = 40 , the recipe step_qty
5341         field will show 80 , if the user overrides it to be 40, then this new
5342         overriden qty 40 is wrt to total o/p of 80  */
5343 
5344          f_step_qty := rtg_org_dtl_tab(loop_index).o_step_qty / l_prod_scale_factor  ;
5345       ELSE
5346          f_step_qty := rtg_org_dtl_tab(loop_index).step_qty ;
5347       END IF;
5348 
5349       IF rtg_org_dtl_tab(loop_index).o_resource_usage > 0 THEN
5350          f_resource_usage := rtg_org_dtl_tab(loop_index).o_resource_usage ;
5351       ELSE
5352          f_resource_usage := rtg_org_dtl_tab(loop_index).resource_usage ;
5353       END IF ;
5354 
5355       IF (rtg_org_dtl_tab(loop_index).o_activity_factor > 0) THEN
5356          f_activity_factor := rtg_org_dtl_tab(loop_index).o_activity_factor;
5357       ELSE
5358          f_activity_factor := rtg_org_dtl_tab(loop_index).activity_factor;
5359       END IF ;
5360 
5361       IF (rtg_org_dtl_tab(loop_index).o_process_qty > 0) THEN
5362          f_process_qty := rtg_org_dtl_tab(loop_index).o_process_qty;
5363       ELSE
5364          f_process_qty := rtg_org_dtl_tab(loop_index).process_qty;
5365       END IF ;
5366 
5367 /* NAMIT_OC */
5368 
5369       IF (rtg_org_dtl_tab(loop_index).o_min_capacity > 0) THEN
5370          f_min_capacity := rtg_org_dtl_tab(loop_index).o_min_capacity ;
5371       ELSE
5372          f_min_capacity := rtg_org_dtl_tab(loop_index).min_capacity ;
5373       END IF ;
5374 
5375       IF (rtg_org_dtl_tab(loop_index).o_max_capacity > 0) THEN
5376          f_max_capacity := rtg_org_dtl_tab(loop_index).o_max_capacity ;
5377       ELSE
5378          f_max_capacity := rtg_org_dtl_tab(loop_index).max_capacity ;
5379       END IF ;
5380 
5381          --  SPECIAL !!! process_qty ZERO than take final step_qty */
5382          IF f_process_qty = 0 THEN
5383             f_process_qty := f_step_qty;
5384          END IF ;
5385 
5386   statement_no := 90 ;
5387 
5388   /* NAMIT_OC If the resource is scaled as fixed or By Charge, the resource
5389   usage will be treated as if it were fixed. If the scale type of the resource
5390   is "By Charge" then  the usage defined is that of the charge and will not need
5391   to be scaled in any way, except of course by the number of charges. */
5392 
5393       IF ((rtg_org_dtl_tab(loop_index).scale_type = 0) OR
5394           (rtg_org_dtl_tab(loop_index).scale_type = 2)) THEN
5395          -- fixed scaling
5396          calculated_resource_usage := ((f_resource_usage * f_activity_factor));
5397       ELSE
5398          /*B2870041 the scale factor was added to ensure the usages match
5399             what is represented by the product in the routing qty */
5400 
5401          calculated_resource_usage := ((f_step_qty / f_process_qty) *
5402            (f_resource_usage * f_activity_factor) * prod_scale_factor ) ;
5403       END IF; /* fixed scaling */
5404 
5405   statement_no := 100 ;
5406      /* Bulk insert assignments for operation_resources */
5407 	/* OR insert # 1 */
5408       orig_rs_seq_num := orig_rs_seq_num + 1;
5409 
5410       or_index := or_index + 1 ;
5411       or_operation_sequence_id(or_index) :=
5412                        rtg_org_dtl_tab(loop_index).x_routingstep_id ;
5413 --      or_resource_seq_num(or_index) := seq_no ;
5414       /* B3596028 */
5415       or_resource_seq_num(or_index) := rtg_org_dtl_tab(loop_index).seq_dep_ind ;
5416       or_resource_id(or_index) := rtg_org_dtl_tab(loop_index).x_resource_id ;
5417       or_alternate_number(or_index) := 0 ;
5418       or_basis_type(or_index) := t_scale_type ;
5419       or_resource_usage(or_index) := ( calculated_resource_usage
5420                    * rtg_org_dtl_tab(loop_index).resource_count ) ;
5421       or_max_resource_units(or_index) := rtg_org_dtl_tab(loop_index).resource_count ;
5422       or_resource_units(or_index) := rtg_org_dtl_tab(loop_index).resource_count ;
5423       or_uom_code(or_index) := rtg_org_dtl_tab(loop_index).resource_usage_uom ;
5424       or_sr_instance_id(or_index) := instance_id ;
5425       or_routing_sequence_id(or_index) := p_x_aps_fmeff_id ;
5426       or_organization_id(or_index) := effectivity.organization_id ;
5427       or_minimum_capacity(or_index) := nvl(f_min_capacity,0) ;
5428       or_maximum_capacity(or_index) := nvl(f_max_capacity,9999999) ;
5429       or_last_update_date(or_index) := current_date_time ;
5430       or_creation_date(or_index) := current_date_time ;
5431       or_orig_rs_seq_num(or_index) := orig_rs_seq_num;
5432       or_break_ind(or_index) := rtg_org_dtl_tab(loop_index).break_ind;
5433 
5434       /* For Primary Rsrc Principal flag = 1, for Aux and Sec Rsrcs Principal Flag = 2*/
5435       IF (rtg_org_dtl_tab(loop_index).prim_rsrc_ind = 1) THEN
5436 
5437          or_principal_flag(or_index) := rtg_org_dtl_tab(loop_index).prim_rsrc_ind ;
5438       ELSE
5439          or_principal_flag(or_index) := 2 ;
5440 
5441       END IF;  /* End of primary Resource Check */
5442 
5443      /* ------------ B4918786 (RDP) STARTS ------------------*/
5444      statement_no := 110 ;
5445      u_setup_id   := NULL ;
5446      or_setup_id(or_index) := null_value ;
5447      IF (rtg_org_dtl_tab(loop_index).is_sds_rout >= 1) THEN
5448        IF (rtg_org_dtl_tab(loop_index).is_unique = 1) THEN
5449            or_setup_id(or_index) := rtg_org_dtl_tab(loop_index).setup_id ;
5450        ELSE
5451           IF (rtg_org_dtl_tab(loop_index).is_nonunique = 1) THEN
5452            -- If the resource is not unique then it should get the setup_id of
5453            -- unique resource present anywhere in a route
5454              bsearch_unique(rtg_org_dtl_tab(loop_index).resource_id,
5455                             effectivity.category_id,
5456                             u_setup_id);
5457             or_setup_id(or_index) := u_setup_id ;
5458             rtg_org_dtl_tab(loop_index).setup_id := u_setup_id;
5459           ELSE
5460             -- It is niether unique nor nonunique
5461             or_setup_id(or_index)  := null_value;
5462             rtg_org_dtl_tab(loop_index).setup_id := null_value;
5463           END IF;
5464 
5465        END IF; /* is_unique */
5466 
5467      END IF; /* Is sds route */
5468 
5469      IF rtg_org_dtl_tab(loop_index).prim_rsrc_ind = 1 THEN
5470        -- This assignment will ensure that , alternate will use this setup
5471        rtg_org_dtl_tab(loop_index).setup_id := or_setup_id(or_index) ;
5472      END IF;
5473 
5474     /*
5475      log_message(
5476      ' WEff ' || effectivity.fmeff_id
5477      ||' Rt '|| rtg_org_dtl_tab(loop_index).routing_id
5478      ||' Op '|| rtg_org_dtl_tab(loop_index).routingstep_no
5479      ||' SdInd '|| rtg_org_dtl_tab(loop_index).seq_dep_ind
5480      ||' ' || rtg_org_dtl_tab(loop_index).resources
5481      ||' Setup ' ||  rtg_org_dtl_tab(loop_index).setup_id
5482      ||' U ' || rtg_org_dtl_tab(loop_index).is_unique
5483      ||' SDX ' || sd_index
5484      ||' NU ' || rtg_org_dtl_tab(loop_index).is_nonunique
5485      ||' NuSet ' || u_setup_id
5486      ||' OS ' || or_setup_id(or_index) );
5487     */
5488      /* ------------ B4918786 (RDP) ENDS ----------------- */
5489 
5490        /*
5491         Now the check if the above resource inserted is a Primary. If it is
5492         Primary then find its Alternates if existing, and then insert its rows
5493         into msc_st_operation_resources table. Also keep track of number of
5494         times alternates are inserted. 1319610
5495        */
5496 
5497   statement_no := 120 ;
5498         IF rtg_org_dtl_tab(loop_index).prim_rsrc_ind = 1 THEN
5499 
5500           --  Reset the Counters and the Flags
5501            v_counter := 0;
5502            alternates_inserted := 'N';
5503            v_alternate  := 0;
5504            k  := 0;
5505 
5506         --  Open the Alternate resource Cursor, for the above Primary Resource
5507         alt_cnt := 1 ;
5508         FOR alt_cnt IN 1..alt_rsrc_size
5509         LOOP
5510 
5511       /* B5688153 Prod spec alternates Rajesh Patangya */
5512             IF ( rtg_alt_rsrc_tab(alt_cnt).prim_resource_id =
5513                       rtg_org_dtl_tab(loop_index).resource_id
5514              AND (rtg_alt_rsrc_tab(alt_cnt).inventory_item_id = -1 OR
5515                   rtg_alt_rsrc_tab(alt_cnt).inventory_item_id = effectivity.inventory_item_id)) THEN
5516 
5517              orig_rs_seq_num := orig_rs_seq_num + 1;
5518             /* B2353759, alternate runtime_factor considered */
5519                v_alternate := v_alternate + 1;
5520 
5521             /* Bulk insert assignments for operation_resources, Alternate resources */
5522 		/* OR insert # 2 */
5523              or_index := or_index + 1 ;
5524              or_operation_sequence_id(or_index) :=
5525                         rtg_org_dtl_tab(loop_index).x_routingstep_id ;
5526 --             or_resource_seqor_operation_sequence_id_num(or_index) := seq_no ;
5527              /* B3596028 */
5528              or_resource_seq_num(or_index) := rtg_org_dtl_tab(loop_index).seq_dep_ind ;
5529              or_resource_id(or_index) :=
5530                   ((rtg_alt_rsrc_tab(alt_cnt).alt_resource_id * 2) + 1) ;
5531              or_alternate_number(or_index) := v_alternate ;
5532              or_principal_flag(or_index) := 1;  /* Taking Principal flag as 1 for Alternates */
5533              or_basis_type(or_index) := t_scale_type ;
5534              or_resource_usage(or_index) := ( calculated_resource_usage
5535                           * rtg_org_dtl_tab(loop_index).resource_count
5536                           * rtg_alt_rsrc_tab(alt_cnt).runtime_factor ) ;
5537              or_max_resource_units(or_index) := rtg_org_dtl_tab(loop_index).resource_count ;
5538              /* B2761278 */
5539              or_resource_units(or_index) := rtg_org_dtl_tab(loop_index).resource_count ;
5540              or_uom_code(or_index) := rtg_org_dtl_tab(loop_index).resource_usage_uom ;
5541                -- or_deleted_flag(or_index) := 2 ;
5542              or_sr_instance_id(or_index) := instance_id ;
5543              or_routing_sequence_id(or_index) := p_x_aps_fmeff_id ;
5544              or_organization_id(or_index) := effectivity.organization_id ;
5545              /* SGIDUGU added min capacity and max capacity inserts */
5546              or_minimum_capacity(or_index) :=
5547                        nvl(rtg_alt_rsrc_tab(alt_cnt).min_capacity,0) ;
5548              or_maximum_capacity(or_index) :=
5549                        nvl(rtg_alt_rsrc_tab(alt_cnt).max_capacity,999999) ;
5550 
5551              or_orig_rs_seq_num(or_index) := orig_rs_seq_num;
5552              or_break_ind(or_index) := rtg_org_dtl_tab(loop_index).break_ind;
5553 
5554              /* ------------ B4918786 (RDP) STARTS ------------------*/
5555                statement_no := 125 ;
5556                IF  rtg_org_dtl_tab(loop_index).setup_id IS NOT NULL THEN
5557                  or_setup_id(or_index) := rtg_org_dtl_tab(loop_index).setup_id ;
5558                ELSE
5559                  or_setup_id(or_index) := null_value ;
5560                END IF;
5561                /*
5562      log_message(
5563      ' -- A sds? ' ||
5564      rtg_org_dtl_tab(loop_index).is_sds_rout    ||' OS ' ||
5565      or_setup_id(or_index) ||' RTg '||
5566      rtg_org_dtl_tab(loop_index).routing_id ||' Opr '||
5567      rtg_org_dtl_tab(loop_index).routingstep_no ||' S_IND '||
5568      rtg_org_dtl_tab(loop_index).seq_dep_ind || ' RS  ' ||
5569      rtg_org_dtl_tab(loop_index).setup_id ||
5570      ' Prev ' || p_setup_id  ||
5571      ' GS ' || g_setup_id ) ;
5572                */
5573 
5574      /* ------------ B4918786 (RDP) ENDS ----------------- */
5575 
5576              or_last_update_date(or_index) := current_date_time ;
5577                -- or_last_updated_by(or_index) := 0 ;
5578              or_creation_date(or_index) := current_date_time ;
5579                -- or_created_by(or_index) := 0 ;
5580 
5581                /* Increment counter to check the number of times the
5582                   alternates are inserted */
5583 
5584                alternates_inserted := 'Y'; /* Inserted alternates */
5585 
5586 	    ELSIF ( rtg_alt_rsrc_tab(alt_cnt).prim_resource_id >
5587                       rtg_org_dtl_tab(loop_index).resource_id ) THEN
5588                EXIT ;
5589             ELSE
5590                NULL ;
5591             END IF;  /* End if for alternate resource and orgn code match */
5592 	END LOOP;  /* Alternate loop */
5593 
5594         END IF;  /* End if for Check in Primary Resource Indicator */
5595 
5596       /*
5597       Now check if the resource is a Auxilary resource and if the
5598       alternates are inserted, if both the conditions are satisfied,
5599       then loop thru the number of times the alternate resources are inserted
5600       and insert the Auxilary resources.
5601       This will take care of the combinations that has to come with the
5602       alternate resources. 1319610
5603       08/10/00 - Bug# 1388757 Changed != to <> as per the Standards
5604       */
5605 
5606   statement_no := 130 ;
5607      IF ( rtg_org_dtl_tab(loop_index).prim_rsrc_ind <> 1) AND
5608         (alternates_inserted = 'Y')
5609      THEN
5610          for k in 1 ..v_alternate
5611          LOOP
5612 
5613             /* Bulk insert assignments operation_resources, Alternate resources */
5614 		/* OR insert # 3 */
5615              or_index := or_index + 1 ;
5616 
5617              or_operation_sequence_id(or_index) := rtg_org_dtl_tab(loop_index).x_routingstep_id ;
5618 --             or_resource_seq_num(or_index) := seq_no ;
5619             /* B3596028 */
5620              or_resource_seq_num(or_index) := rtg_org_dtl_tab(loop_index).seq_dep_ind ;
5621              or_resource_id(or_index) := rtg_org_dtl_tab(loop_index).x_resource_id ;
5622              or_alternate_number(or_index) := k ;
5623             /* K will determine the no. of times altenates are used */
5624              or_principal_flag(or_index) := 2 ;
5625             /* Principal flag as 2 for Secondary Resources */
5626              or_basis_type(or_index) := t_scale_type ;
5627              or_resource_usage(or_index) := ( calculated_resource_usage
5628                     * rtg_org_dtl_tab(loop_index).resource_count ) ;
5629 
5630              or_max_resource_units(or_index) := rtg_org_dtl_tab(loop_index).resource_count ;
5631              /* B2761278 */
5632              or_resource_units(or_index) := rtg_org_dtl_tab(loop_index).resource_count ;
5633              or_uom_code(or_index) := rtg_org_dtl_tab(loop_index).resource_usage_uom ;
5634                -- or_deleted_flag(or_index) := 2 ;
5635              or_sr_instance_id(or_index) := instance_id ;
5636              or_routing_sequence_id(or_index) := p_x_aps_fmeff_id ;
5637              or_organization_id(or_index) := effectivity.organization_id ;
5638              /* SGIDUGU - Added min capacity and max capacity inserts */
5639              or_minimum_capacity(or_index) := nvl(f_min_capacity,0) ;
5640              or_maximum_capacity(or_index) := nvl(f_max_capacity,9999999) ;
5641              or_break_ind(or_index) := rtg_org_dtl_tab(loop_index).break_ind;
5642              or_setup_id(or_index) := null_value ;
5643              or_last_update_date(or_index) := current_date_time ;
5644                -- or_last_updated_by(or_index) := 0 ;
5645              or_creation_date(or_index) := current_date_time ;
5646                -- or_created_by(or_index) := 0 ;
5647              or_orig_rs_seq_num(or_index) := orig_rs_seq_num;
5648 
5649          END LOOP; /* End loop of the v_counter */
5650      END IF;  /* End if condition for the secondary resource flag */
5651 
5652   END IF; /* End if condition for include rtg row check */
5653          -- To nullify the override effect for next recipe to use
5654          rtg_org_dtl_tab(loop_index).o_step_qty := -1 ;
5655          rtg_org_dtl_tab(loop_index).o_process_qty := -1 ;
5656          rtg_org_dtl_tab(loop_index).o_activity_factor := -1 ;
5657          rtg_org_dtl_tab(loop_index).o_resource_usage := -1 ;
5658          rtg_org_dtl_tab(loop_index).o_max_capacity   := -1 ;
5659          rtg_org_dtl_tab(loop_index).o_min_capacity   := -1 ;
5660 
5661 /*
5662      log_message(
5663      ' Effect ' || effectivity.fmeff_id || ' is_sds_rout ' ||
5664      rtg_org_dtl_tab(loop_index).is_sds_rout    ||' Setup ' ||
5665      rtg_org_dtl_tab(loop_index).setup_id    ||' RTg '||
5666      rtg_org_dtl_tab(loop_index).routing_id ||' Oprn '||
5667      rtg_org_dtl_tab(loop_index).routingstep_no ||' SDS_IND '||
5668      rtg_org_dtl_tab(loop_index).seq_dep_ind );
5669 
5670      log_message(
5671      rtg_org_hdr_tab(effectivity.rtg_hdr_location).valid_flag || ' ***' ||
5672      rtg_org_dtl_tab(loop_index).routing_id ||'*'||
5673      effectivity.recipe_id ||'*'||
5674      rtg_org_dtl_tab(loop_index).routingstep_id         ||' Us '||
5675      rtg_org_dtl_tab(loop_index).resource_usage      ||' *'||
5676      rtg_org_dtl_tab(loop_index).o_resource_usage      ||' AF '||
5677      rtg_org_dtl_tab(loop_index).activity_factor      ||' *'||
5678      rtg_org_dtl_tab(loop_index).o_activity_factor      ||' SQ '||
5679      rtg_org_dtl_tab(loop_index).step_qty      ||' *'||
5680      rtg_org_dtl_tab(loop_index).o_step_qty     ||' PQ '||
5681      rtg_org_dtl_tab(loop_index).process_qty      ||' *'||
5682      rtg_org_dtl_tab(loop_index).o_process_qty      ||' M '||
5683      rtg_org_dtl_tab(loop_index).min_capacity   ||' *'||
5684      rtg_org_dtl_tab(loop_index).o_min_capacity   ||' X '||
5685      rtg_org_dtl_tab(loop_index).max_capacity   ||' *'||
5686      rtg_org_dtl_tab(loop_index).o_max_capacity);
5687 */
5688   END LOOP;  /* End loop of the Main */
5689   END IF ;  /* Positive counter value check */
5690 
5691   return_status := TRUE;
5692 
5693   EXCEPTION
5694   WHEN OTHERS THEN
5695 	log_message('Write routing operations failed at statement '
5696                      ||to_char(statement_no)||': '||sqlerrm);
5697 	return_status := FALSE;
5698 
5699 END write_routing_operations;
5700 
5701 /*
5702 REM+=========================================================================+
5703 REM| PROCEDURE NAME                                                          |
5704 REM|    write_operation_componenets                                          |
5705 REM|                                                                         |
5706 REM| TYPE                                                                    |
5707 REM|    Private                                                              |
5708 REM|                                                                         |
5709 REM| DESCRIPTION                                                             |
5710 REM|     Writes routing/material associations to the MSC tables              |
5711 REM|                                                                         |
5712 REM| INPUT PARAMETERS                                                        |
5713 REM|    table_index: index of APS effectivity in aps_effectivities           |
5714 REM|                                                                         |
5715 REM| OUTPUT PARAMETERS                                                       |
5716 REM|    return_status  TRUE=> OK                                             |
5717 REM|                                                                         |
5718 REM| INPUT/OUTPUT PARAMETERS                                                 |
5719 REM|    None                                                                 |
5720 REM|                                                                         |
5721 REM| HISTORY                                                                 |
5722 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
5723 REM+=========================================================================+
5724 */
5725 PROCEDURE write_operation_components
5726 (
5727   p_x_aps_fmeff_id   IN  PLS_INTEGER,
5728   precipe_id         IN  PLS_INTEGER,
5729   return_status      OUT NOCOPY BOOLEAN
5730 )
5731 IS
5732 
5733   i              INTEGER ;
5734   record_written BOOLEAN ;  /* B3562488 */
5735   already_done   INTEGER ;  /* 3562488  */
5736   stmt_no        NUMBER ;
5737   found_product  BOOLEAN ;
5738   write_row      BOOLEAN ;
5739 
5740 BEGIN
5741   i              := 1;
5742   record_written := FALSE ;    /* B3562488 */
5743   already_done   := 0 ;        /* 3562488  */
5744   stmt_no        := 0;
5745   found_product  := FALSE;
5746   write_row      := TRUE;
5747 
5748   -- write routing/material associations to msc_operation_components
5749          gmp_debug_message('Enter --- > ' || g_mat_assoc
5750           || ' Effec ' || effectivity.formula_id
5751           || ' Material ' || mat_assoc_tab(g_mat_assoc).formula_id || ' Recipe ' || precipe_id ) ;
5752 
5753 stmt_no := 0;
5754    FOR i in g_mat_assoc..material_assocs_size
5755    LOOP
5756 
5757      IF  effectivity.formula_id > mat_assoc_tab(i).formula_id THEN
5758        gmp_debug_message(i || ' --- ' ||
5759           effectivity.formula_id || ' > ' || mat_assoc_tab(i).formula_id ) ;
5760           NULL ;   /* Keep on looping */
5761 
5762      ELSIF effectivity.formula_id < mat_assoc_tab(i).formula_id THEN
5763             /* B3562488 */
5764             IF record_written = TRUE THEN
5765                g_mat_assoc := already_done ;
5766             END IF ;
5767 
5768            gmp_debug_message('Exit ' || g_mat_assoc || ' ***'
5769              || effectivity.formula_id || ' < ' || mat_assoc_tab(i).formula_id ) ;
5770 
5771            EXIT;
5772      ELSIF effectivity.formula_id = mat_assoc_tab(i).formula_id THEN
5773             /* B3562488 */
5774         IF record_written = FALSE THEN
5775            already_done := i ;
5776            record_written := TRUE ;
5777 --         log_message('Written ' || already_done);
5778         END IF ;
5779 
5780 stmt_no := 10;
5781         IF mat_assoc_tab(i).recipe_id = precipe_id THEN
5782 
5783            /*  Do Not write material association
5784                       for the product line */
5785 
5786            IF mat_assoc_tab(i).inventory_item_id = effectivity.inventory_item_id THEN
5787 	      IF NOT (found_product )THEN
5788 	         found_product := TRUE ;
5789 		 write_row := FALSE ;
5790 	      ELSE
5791                  write_row := TRUE ;
5792               END IF ;
5793            ELSE
5794               write_row := TRUE ;
5795            END IF ;
5796 
5797            /*  operation components bulk insert */
5798            IF (write_row ) THEN
5799               oc_index := oc_index + 1 ;
5800 
5801               oc_operation_sequence_id(oc_index) := mat_assoc_tab(i).x_routingstep_id ;
5802 
5803               /*B5176291 - Item substitution changes - start*/
5804               IF mat_assoc_tab(i).x_formulaline_id IS NOT NULL THEN
5805                  oc_component_sequence_id(oc_index) := mat_assoc_tab(i).x_formulaline_id ;
5806               ELSE
5807                  op_formline_cnt := op_formline_cnt + 1;
5808                  oc_component_sequence_id(oc_index) := (( v_gmd_formula_lineid + op_formline_cnt ) * 2) + 1;
5809               END IF;
5810               /*B5176291 - Item substitution changes - end*/
5811 
5812               oc_sr_instance_id(oc_index) := instance_id ;
5813               oc_bill_sequence_id(oc_index) := p_x_aps_fmeff_id  ;
5814               oc_routing_sequence_id(oc_index) := p_x_aps_fmeff_id ;
5815               oc_organization_id(oc_index) := effectivity.organization_id ;
5816               -- deleted_flag := 2,
5817               oc_last_update_date(oc_index) := current_date_time ;
5818               oc_creation_date(oc_index) := current_date_time ;
5819 
5820            END IF;
5821 
5822 stmt_no := 20;
5823 /* NAMIT_MTQ */
5824            IF ((mat_assoc_tab(i).min_trans_qty IS NOT NULL) OR
5825                    (mat_assoc_tab(i).min_delay IS NOT NULL)) THEN
5826 
5827               IF (mat_assoc_tab(i).min_trans_qty IS NULL) THEN
5828                  mat_assoc_tab(i).min_trans_qty := 0;
5829               END IF;
5830 
5831               mtq_index := mtq_index + 1;
5832 
5833    /* nsinghi B3970993 Start */
5834    /* If either the org, recipe, formula, rtg, step or item changes; re-initialize
5835    the globals. Write this MTQ row, and let the g_min_mtq be the mtq qty from this
5836    row. Also store the index of mtq row in global variable.
5837    The first MTQ row for a item belonging to a step is always written. If the same
5838    item is defined in the same step, then the row having minimum MTQ will be written. */
5839 
5840    /*
5841    1) If the MTQ is defined as null in one row, but min and/or max delay
5842    is defined for that row. Another row for the same item in same step, if MTQ is
5843    not null, but min and/or max delay are null, then which row to transfer. The code
5844    will transfer the row that has null MTQ.
5845    2) If MTQ is defined for same item multiple times in same step, and if all the
5846    MTQ values are equal, then the first row where MTQ is found will be sent. */
5847 
5848               IF g_old_recipe_id <> mat_assoc_tab(i).recipe_id OR
5849                  g_old_formula_id <> mat_assoc_tab(i).formula_id OR
5850                  g_old_rtg_id <> p_x_aps_fmeff_id OR
5851                  g_old_rtgstep_id <> mat_assoc_tab(i).x_routingstep_id OR
5852                  g_old_aps_item_id <> mat_assoc_tab(i).inventory_item_id
5853               THEN
5854                  g_old_recipe_id := mat_assoc_tab(i).recipe_id;
5855                  g_old_formula_id := mat_assoc_tab(i).formula_id;
5856                  g_old_rtg_id := p_x_aps_fmeff_id;
5857                  g_old_rtgstep_id := mat_assoc_tab(i).x_routingstep_id;
5858                  g_old_aps_item_id := mat_assoc_tab(i).inventory_item_id;
5859                  g_mtq_loc := mtq_index;
5860                  g_min_mtq := mat_assoc_tab(i).min_trans_qty;
5861 
5862                  itm_mtq_from_op_seq_id(mtq_index) :=  mat_assoc_tab(i).x_routingstep_id;
5863                  itm_mtq_routing_sequence_id(mtq_index) := p_x_aps_fmeff_id ;
5864                  itm_mtq_sr_instance_id(mtq_index) := instance_id ;
5865                  itm_mtq_from_item_id(mtq_index) := mat_assoc_tab(i).inventory_item_id ;
5866                  itm_mtq_organization_id(mtq_index) := effectivity.organization_id ;
5867                  itm_mtq_min_tran_qty(mtq_index) := mat_assoc_tab(i).min_trans_qty * mat_assoc_tab(i).uom_conv_factor;
5868                  itm_mtq_min_time_offset(mtq_index) := mat_assoc_tab(i).min_delay;
5869                  itm_mtq_max_time_offset(mtq_index) := mat_assoc_tab(i).max_delay;
5870                  itm_mtq_frm_op_seq_num(mtq_index) := mat_assoc_tab(i).routingstep_no;
5871                  itm_mtq_last_update_date(mtq_index) := current_date_time;
5872                  itm_mtq_creation_date(mtq_index) := current_date_time;
5873               END IF;
5874 
5875      /* If an item is yielded in the same step multiple times and if MTQ value is associated
5876       to that item multiple times, then write row that has min MTQ. */
5877 
5878               IF g_old_recipe_id = mat_assoc_tab(i).recipe_id AND
5879                  g_old_formula_id = mat_assoc_tab(i).formula_id AND
5880                  g_old_rtg_id = p_x_aps_fmeff_id AND
5881                  g_old_rtgstep_id = mat_assoc_tab(i).x_routingstep_id AND
5882                  g_old_aps_item_id = mat_assoc_tab(i).inventory_item_id AND
5883                  g_mtq_loc <> mtq_index
5884               THEN
5885                  log_message('Item : '||mat_assoc_tab(i).inventory_item_id||' in recipe : '||mat_assoc_tab(i).recipe_id
5886                  ||' is associated multiple times in step '||mat_assoc_tab(i).routingstep_no
5887                  ||' with MTQ/Min/Max Delay defined. Row with Minimum/Null MTQ will be considered. ');
5888                  IF mat_assoc_tab(i).min_trans_qty < g_min_mtq THEN
5889                     itm_mtq_from_op_seq_id(g_mtq_loc) :=  mat_assoc_tab(i).x_routingstep_id;
5890                     itm_mtq_routing_sequence_id(g_mtq_loc) := p_x_aps_fmeff_id ;
5891                     itm_mtq_sr_instance_id(g_mtq_loc) := instance_id ;
5892                     itm_mtq_from_item_id(g_mtq_loc) := mat_assoc_tab(i).inventory_item_id ;
5893                     itm_mtq_organization_id(g_mtq_loc) := effectivity.organization_id ;
5894                     itm_mtq_min_tran_qty(g_mtq_loc) := mat_assoc_tab(i).min_trans_qty * mat_assoc_tab(i).uom_conv_factor;
5895                     itm_mtq_min_time_offset(g_mtq_loc) := mat_assoc_tab(i).min_delay;
5896                     itm_mtq_max_time_offset(g_mtq_loc) := mat_assoc_tab(i).max_delay;
5897                     itm_mtq_frm_op_seq_num(g_mtq_loc) := mat_assoc_tab(i).routingstep_no;
5898                  END IF;
5899                  mtq_index := mtq_index - 1;
5900               END IF;
5901         /* nsinghi B3970993 End */
5902 
5903            END IF;
5904 stmt_no := 30;
5905 
5906 --       log_message(i || ' - RCP - ' || p_x_aps_fmeff_id || ' ** ' || mat_assoc_tab(i).recipe_id ) ;
5907 
5908         END IF ;   /* Recipe check */
5909 
5910      END IF ;   /* formula check */
5911    END LOOP ;
5912 
5913   return_status := TRUE;
5914 
5915   EXCEPTION
5916   WHEN OTHERS THEN
5917 	log_message('Error writing operation components: '||sqlerrm);
5918 	return_status := FALSE;
5919 END write_operation_components;
5920 
5921 /*
5922 REM+=========================================================================+
5923 REM| PROCEDURE NAME                                                          |
5924 REM|    export_effectivities                                                 |
5925 REM|                                                                         |
5926 REM| TYPE                                                                    |
5927 REM|    Private                                                              |
5928 REM|                                                                         |
5929 REM| DESCRIPTION                                                             |
5930 REM|    This procedure is called after all of the effectivities have been    |
5931 REM|    validated and extracted. It exports the data gathered to APS using   |
5932 REM|    the defined table mappings.                                          |
5933 REM|                                                                         |
5934 REM| INPUT PARAMETERS                                                        |
5935 REM|    None                                                                 |
5936 REM|                                                                         |
5937 REM| OUTPUT PARAMETERS                                                       |
5938 REM|    return_status    TRUE => OK                                          |
5939 REM|                                                                         |
5940 REM| INPUT/OUTPUT PARAMETERS                                                 |
5941 REM|    None                                                                 |
5942 REM|                                                                         |
5943 REM| HISTORY                                                                 |
5944 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
5945 REM|  06/02/2003   Sridhar Gidugu  - Added code to check aps fm eff          |
5946 REM|                                 B2989806                                |
5947 REM+=========================================================================+
5948 */
5949 PROCEDURE export_effectivities
5950 (
5951   return_status            OUT NOCOPY BOOLEAN
5952 )
5953 IS
5954   p_status       BOOLEAN;
5955   b_status       BOOLEAN;
5956   r_status       BOOLEAN;
5957   ro_status      BOOLEAN;
5958   oc_status      BOOLEAN;
5959   cursor c_vcp_release_12137 is
5960   select count(*)
5961   from ad_applied_patches
5962   where patch_name = '13833266';
5963 
5964 BEGIN
5965 
5966         /* Finally generate the effectivity_id */
5967 
5968         /* B2989806
5969            If aps_fmeff_id from gmp_form_eff was null,
5970            then create a new one.  Otherwise, use original.
5971         */
5972 
5973         /*
5974           g_aps_eff_id := g_aps_eff_id + 1 ;
5975           aps_fmeff_id := g_aps_eff_id ;
5976           x_aps_fmeff_id := (aps_fmeff_id * 2 ) + 1 ;
5977           B2989806
5978         */
5979 
5980         IF effectivity.aps_fmeff_id = -1 THEN
5981          g_aps_eff_id := g_aps_eff_id + 1 ;
5982          aps_fmeff_id := g_aps_eff_id ;
5983         ELSE
5984          aps_fmeff_id := effectivity.aps_fmeff_id ;
5985         END IF ;
5986 
5987         x_aps_fmeff_id := (aps_fmeff_id * 2 ) + 1 ;
5988      begin  -- Biogen start
5989         --use this code to find out if vcp release is 12.1.3.7
5990         Open C_Vcp_Release_12137;
5991         fetch c_vcp_release_12137 into l_12137_count;
5992         close c_vcp_release_12137;
5993      exception
5994         When No_Data_Found Then
5995             log_message('MSC vcp release not found:: '||sqlerrm);
5996      end ;  -- Biogen end
5997 
5998      begin  -- Biogen start
5999         --use this code to find out apps version. for 12.2.2 apps version is 6  and dont use biogen change.
6000         select apps_ver
6001         into   l_apps_ver
6002         from msc_apps_instances
6003         where instance_id = g_instance_id;
6004      exception
6005         When No_Data_Found Then
6006             log_message('MSC apps version not found:: '||sqlerrm);
6007      end ;  -- Biogen end
6008 
6009     /* B3837959 MMK Issue, Handling of return status */
6010     write_process_effectivity(x_aps_fmeff_id, aps_fmeff_id, p_status);
6011     return_status := p_status ;
6012 
6013     IF return_status = TRUE THEN
6014       write_bom_components(x_aps_fmeff_id, b_status);
6015       return_status := b_status ;
6016 
6017       IF (effectivity.routing_id IS NOT NULL) AND (b_status = TRUE) THEN
6018         write_routing(x_aps_fmeff_id, r_status);
6019         return_status := r_status ;
6020 
6021         IF return_status = TRUE THEN
6022           write_routing_operations(x_aps_fmeff_id,ro_status);
6023           return_status := ro_status ;
6024 
6025           IF return_status = TRUE THEN
6026              write_operation_components(x_aps_fmeff_id,
6027                                         effectivity.recipe_id,
6028                                         oc_status);
6029 
6030              return_status := oc_status ;
6031              IF return_status = FALSE THEN
6032                log_message('write_operation_components Returned FALSE');
6033              END IF;
6034           ELSE
6035             log_message('write_routing_operations Returned FALSE');
6036             return_status := FALSE;
6037           END IF;
6038 
6039         ELSE
6040           log_message('write_routing Returned FALSE');
6041           return_status := FALSE;
6042         END IF;
6043 
6044       ELSE
6045           IF return_status = FALSE THEN
6046             log_message('write_bom_components Returned FALSE');
6047           END IF;
6048       END IF;
6049 
6050     ELSE
6051       log_message('write_process_effectivity Returned FALSE');
6052       return_status := FALSE;
6053     END IF;
6054 
6055 EXCEPTION
6056   WHEN OTHERS THEN
6057     log_message('Export Effectivities Raised Exception: '||sqlerrm);
6058     log_message(to_char(effectivity.fmeff_id));
6059     return_status := FALSE;
6060 END export_effectivities;
6061 
6062 /*
6063 REM+=========================================================================+
6064 REM| PROCEDURE NAME                                                          |
6065 REM|    extract_effectivities                                                |
6066 REM|                                                                         |
6067 REM| TYPE                                                                    |
6068 REM|    Public                                                               |
6069 REM|                                                                         |
6070 REM| USAGE                                                                   |
6071 REM|    This is the 'main' procedure for extracting effectivities and then   |
6072 REM|    exploding the ones which are valid for export to APS                 |
6073 REM|                                                                         |
6074 REM| DESCRIPTION                                                             |
6075 REM|                                                                         |
6076 REM|                                                                         |
6077 REM| INPUT PARAMETERS                                                        |
6078 REM|    None                                                                 |
6079 REM|                                                                         |
6080 REM| OUTPUT PARAMETERS                                                       |
6081 REM|    return_status                                                        |
6082 REM|                                                                         |
6083 REM| INPUT/OUTPUT PARAMETERS                                                 |
6084 REM|    None                                                                 |
6085 REM|                                                                         |
6086 REM| HISTORY                                                                 |
6087 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
6088 REM| 04/21/2004   - Navin Sinha - B3577871 -ST:OSFME2: collections failing   |
6089 REM|                               in planning data pull.                    |
6090 REM|                               Added handling of NO_DATA_FOUND Exception.|
6091 REM|                               And return the return_status as TRUE.     |
6092 REM+=========================================================================+
6093 */
6094 PROCEDURE extract_effectivities
6095 (
6096   at_apps_link     IN VARCHAR2,
6097   delimiter_char   IN VARCHAR2,
6098   instance         IN INTEGER,
6099   run_date         IN DATE,
6100   return_status    IN OUT NOCOPY BOOLEAN
6101 )
6102 IS
6103   valid            BOOLEAN;
6104   setup_failure    EXCEPTION;
6105   extract_failure  EXCEPTION;
6106   export_failure   EXCEPTION;
6107 
6108   retrieval_cursor        VARCHAR2(4096) ;
6109 BEGIN
6110 g_aps_eff_id              := 0; /* Global Aps Effectivity ID */
6111 aps_fmeff_id              := 0 ;/* Generated effectivity Id */
6112 x_aps_fmeff_id            := 0 ;/* encoded effectivity Id */
6113 retrieval_cursor          := NULL ;
6114 
6115 
6116 log_message ('extract effectivities started '|| l_debug );
6117 
6118   IF return_status THEN
6119     v_cp_enabled := TRUE;
6120   ELSE
6121     v_cp_enabled := FALSE;
6122   END IF;
6123 
6124 
6125   /* B2104059 GMP-APS ENHANCEMENT FOR GMD FORMULA SECURITY FUNCTIONALITY  */
6126   /* Disable the security  */
6127 
6128     retrieval_cursor := ' begin gmd_p_fs_context.set_additional_attr'
6129                         || at_apps_link || ';end;'   ;
6130     EXECUTE IMMEDIATE retrieval_cursor ;
6131 
6132   /* Before we do anything we need to create/setup/size a few things */
6133 
6134   g_instance_id := instance ;
6135 
6136   setup(at_apps_link, delimiter_char, instance, run_date, valid);
6137 
6138   IF NOT valid THEN
6139     RAISE setup_failure;
6140   END IF;
6141 
6142   /* If all is OK, extract the effectivities to PL/SQL tables */
6143   retrieve_effectivities(valid);
6144 
6145   IF NOT valid THEN
6146     RAISE extract_failure;
6147   END IF;
6148 
6149   return_status := TRUE;
6150 
6151   EXCEPTION
6152 	WHEN setup_failure THEN
6153 	    /* Initial setup failed */
6154 		log_message('Effectivity extract setup failure');
6155 		return_status := FALSE;
6156 
6157 	WHEN extract_failure THEN
6158 	    /* Effectivity extraction failed */
6159 		log_message('Effectivity extraction failed');
6160 		return_status := FALSE;
6161 
6162 	WHEN export_failure THEN
6163 	    /* Effectivity export failed */
6164 		log_message('Effectivity export failed');
6165 		return_status := FALSE;
6166 
6167         WHEN NO_DATA_FOUND THEN  /* B3577871 */
6168                 log_message(' NO_DATA_FOUND exception raised in Procedure: Gmp_bom_routing_pkg.Extract_effectivities ' );
6169                 return_status := TRUE;
6170 	WHEN OTHERS THEN
6171 		log_message('Untrapped effectivity extraction error');
6172 		log_message(sqlerrm);
6173         return_status := FALSE;
6174 
6175 END extract_effectivities;
6176 
6177 /*
6178 REM+=========================================================================+
6179 REM| PROCEDURE NAME                                                          |
6180 REM|    setup                                                                |
6181 REM|                                                                         |
6182 REM| TYPE                                                                    |
6183 REM|    Private                                                              |
6184 REM|                                                                         |
6185 REM| USAGE                                                                   |
6186 REM|    This procedure sets up and initilialises various program structures  |
6187 REM|    for the extraction of effectivities                                  |
6188 REM|                                                                         |
6189 REM| DESCRIPTION                                                             |
6190 REM|                                                                         |
6191 REM|                                                                         |
6192 REM| INPUT PARAMETERS                                                        |
6193 REM|    apps_link_name VARCHAR2 - database link to APPS database instance    |
6194 REM|    delimtiter_char VARCHAR2 - used to links strings together            |
6195 REM|    instance       INTEGER - the GMP instance ID                         |
6196 REM|    run_date       DATE    - the run date                                |
6197 REM|                                                                         |
6198 REM| OUTPUT PARAMETERS                                                       |
6199 REM|    return_status: TRUE => OK                                            |
6200 REM|                                                                         |
6201 REM| INPUT/OUTPUT PARAMETERS                                                 |
6202 REM|    None                                                                 |
6203 REM|                                                                         |
6204 REM| HISTORY                                                                 |
6205 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
6206 REM+=========================================================================+
6207 */
6208 PROCEDURE setup
6209 (
6210   apps_link_name IN VARCHAR2,
6211   delimiter_char IN VARCHAR2,
6212   instance       IN INTEGER,
6213   run_date       IN DATE,
6214   return_status  OUT NOCOPY BOOLEAN
6215 )
6216 IS
6217   stat           VARCHAR2(1024);
6218   statement_no   INTEGER;
6219 
6220 BEGIN
6221   /* Construct link names */
6222 
6223   IF apps_link_name IS NOT NULL
6224   THEN
6225     at_apps_link := apps_link_name;
6226   ELSE
6227     at_apps_link := ' ';
6228   END IF;
6229 
6230   /* select maximum aps_effectivity ID */
6231 
6232   stat := ' SELECT max(APS_FMEFF_ID) from gmp_form_eff'||at_apps_link ;
6233   EXECUTE IMMEDIATE stat INTO g_aps_eff_id ;
6234 
6235   IF NVL(g_aps_eff_id,0) = 0 THEN
6236      g_aps_eff_id := 1 ;
6237   END IF ;
6238 
6239   /* Everything starts with index # of 1 */
6240 
6241   alt_rsrc_size             := 1;
6242   formula_headers_size      := 1;
6243   formula_details_size      := 1;
6244   formula_orgn_size         := 1;
6245   routing_headers_size      := 1;
6246   rtg_org_dtl_size          := 1;
6247   rtg_gen_dtl_size          := 1;
6248   material_assocs_size      := 1;
6249   recipe_orgn_over_size     := 1;
6250   recipe_override_size      := 1;
6251   g_mat_assoc      	    := 1;
6252   opr_stpdep_size           := 1;
6253   g_dep_index               := 1;
6254 
6255 /* These variables store the MTQ related values that is last inserted. */
6256   g_old_formula_id          := -1; /* B3970993 */
6257   g_old_recipe_id           := -1; /* B3970993 */
6258   g_old_rtg_id              := -1; /* B3970993 */
6259   g_old_rtgstep_id          := -1; /* B3970993 */
6260   g_old_aps_item_id         := -1; /* B3970993 */
6261   g_mtq_loc                 := -1; /* B3970993 */
6262   g_min_mtq                 := -1; /* B3970993 */
6263 
6264   current_date_time := run_date;
6265 
6266   instance_id := instance;
6267 
6268   delimiter := delimiter_char;
6269 
6270   /* Initialize the counter values for bulk inserts */
6271    bom_index   := 0 ;
6272    bomc_index  := 0 ;
6273    or_index    := 0 ;
6274    rtg_index   := 0 ;
6275    opr_index   := 0 ;
6276    rs_index    := 0 ;
6277    oc_index    := 0 ;
6278    mtq_index   := 0 ;
6279 
6280   return_status := TRUE;
6281 
6282    /* Get the directory specified in the 'utl_file_dir' in init.ora file */
6283    BEGIN
6284        SELECT substrb(translate(ltrim(value),',',' '), 1,
6285                      instr(translate(ltrim(value),',',' '),' ') - 1)
6286         INTO p_location
6287         FROM v$parameter
6288        WHERE name = 'utl_file_dir';
6289   EXCEPTION
6290     WHEN OTHERS THEN
6291 	return_status := FALSE;
6292 	log_message('directory select failed ');
6293 	log_message(sqlerrm);
6294     END ;
6295 
6296     IF p_location IS NOT NULL THEN
6297       -- gmp_putline ( 'file opened ' || p_location  || ' at '
6298       --    || TO_CHAR ( SYSDATE, 'DD-MON-YYYY HH24:MI:SS' ), 'w' );
6299       -- gmp_putline ('  ' , 'a' );
6300       null ;
6301     END IF ;
6302 
6303   EXCEPTION
6304     WHEN OTHERS THEN
6305 	return_status := FALSE;
6306 	log_message('Setup failed at statement ');
6307 	log_message(sqlerrm);
6308 
6309 END setup;
6310 /*
6311 REM+=========================================================================+
6312 REM| FUNCTION NAME                                                           |
6313 REM|    find_routing_offsets                                                  |
6314 REM| DESCRIPTION                                                             |
6315 REM|                                                                         |
6316 REM| HISTORY                                                                 |
6317 REM+=========================================================================+
6318 */
6319 FUNCTION find_routing_offsets (p_formula_id    	IN PLS_INTEGER,
6320                                p_organization_id	IN PLS_INTEGER) -- akaruppa previously p_plant_code(VARCHAR2)
6321                                RETURN NUMBER IS
6322 
6323 i 	    PLS_INTEGER ;
6324 retvar 	    PLS_INTEGER ;
6325 
6326 BEGIN
6327 i 	    := 1 ;
6328 retvar 	    := -1 ;
6329 IF p_formula_id = g_prev_formula_id THEN
6330  retvar := g_prev_locn ;
6331  IF g_prev_locn > 0 THEN
6332    g_rstep_loc := g_prev_locn ;
6333  END IF ;
6334 ELSE
6335 g_prev_formula_id := p_formula_id ;
6336 FOR i in g_rstep_loc..rtg_offsets_size
6337 LOOP
6338   IF rstep_offsets(i).formula_id = p_formula_id THEN
6339     IF rstep_offsets(i).organization_id = p_organization_id THEN
6340       retvar := i ;
6341       g_rstep_loc := i ;
6342       EXIT ;
6343     ELSIF rstep_offsets(i).organization_id > p_organization_id THEN
6344       g_rstep_loc := i ;
6345       retvar := -1 ;
6346       EXIT ;
6347     ELSE  /* continue looping */
6348       NULL ;
6349     END IF ;
6350   ELSIF rstep_offsets(i).formula_id < p_formula_id THEN
6351     NULL ;  /* Continue looping */
6352   ELSE -- rstep_offsets(i).formula_id > p_formula_id THEN
6353    retvar := -1 ;
6354    g_rstep_loc := i ;
6355    EXIT ;
6356   END IF;
6357 END LOOP ;
6358 
6359 END IF ; /* different formula_id */
6360  g_prev_locn := retvar ;
6361  return retvar ;
6362 END find_routing_offsets;
6363 
6364 /*
6365 REM+=========================================================================+
6366 REM| FUNCTION NAME                                                           |
6367 REM|    get_offsets                                                          |
6368 REM| DESCRIPTION                                                             |
6369 REM|                                                                         |
6370 REM| HISTORY                                                                 |
6371 REM+=========================================================================+
6372 */
6373 FUNCTION get_offsets( p_formula_id		IN PLS_INTEGER,
6374 			p_organization_id	IN PLS_INTEGER, -- akaruppa previously p_plant_code(VARCHAR2)
6375 			p_formulaline_id	IN PLS_INTEGER )
6376 			RETURN NUMBER IS
6377 
6378 i 	    PLS_INTEGER ;
6379 retvar 	    PLS_INTEGER ;
6380 
6381 BEGIN
6382 i 	    := 1 ;
6383 retvar 	    := -1 ;
6384 
6385 FOR i in g_rstep_loc..rtg_offsets_size
6386 LOOP
6387   IF rstep_offsets(i).formula_id = p_formula_id THEN
6388     IF rstep_offsets(i).organization_id = p_organization_id THEN
6389       IF rstep_offsets(i).formulaline_id = p_formulaline_id THEN
6390 	retvar := i ;
6391 	g_rstep_loc := i+1 ;
6392 	EXIT ;
6393       ELSIF
6394 	rstep_offsets(i).formulaline_id > p_formulaline_id THEN
6395 	retvar := -1 ;
6396 	g_rstep_loc := i ;
6397 	EXIT ;
6398       ELSE
6399 	NULL ; /* continue looping */
6400       END IF ;
6401     ELSIF rstep_offsets(i).organization_id > p_organization_id THEN
6402       g_rstep_loc := i ;
6403       retvar := -1 ;
6404       EXIT ;
6405     ELSE  /* continue looping */
6406       NULL ;
6407     END IF ;
6408   ELSIF rstep_offsets(i).formula_id < p_formula_id THEN
6409     NULL ;  /* Continue looping */
6410   ELSE -- rstep_offsets(i).formula_id > p_formula_id THEN
6411    retvar := -1 ;
6412    g_rstep_loc := i ;
6413    EXIT ;
6414   END IF;
6415 END LOOP ;
6416  return retvar ;
6417 END get_offsets;
6418 
6419 /*
6420 REM+=========================================================================+
6421 REM| PROCEDURE NAME                                                          |
6422 REM|    Report_Error                                                         |
6423 REM|                                                                         |
6424 REM| TYPE                                                                    |
6425 REM|    Private                                                              |
6426 REM|                                                                         |
6427 REM| USAGE                                                                   |
6428 REM|                                                                         |
6429 REM|                                                                         |
6430 REM| DESCRIPTION                                                             |
6431 REM|    This is a general error reporting procedure which logs errors for    |
6432 REM|    the effectivity extraction.                                          |
6433 REM|                                                                         |
6434 REM| INPUT PARAMETERS                                                        |
6435 REM|    i_error_id    number    -  The error id for the failure              |
6436 REM|    i_error_code  varchar2  -  The error code for the message            |
6437 REM|    i_param_1     varchar2  -  Error parameter 1                         |
6438 REM|    i_param_2     varchar2  -  Error parameter 2                         |
6439 REM|    i_param_3     varchar2  -  Error parameter 3                         |
6440 REM|    i_param_4     varchar2  -  Error parameter 4                         |
6441 REM|                                                                         |
6442 REM| OUTPUT PARAMETERS                                                       |
6443 REM|    None                                                                 |
6444 REM|                                                                         |
6445 REM| INPUT/OUTPUT PARAMETERS                                                 |
6446 REM|    None                                                                 |
6447 REM|                                                                         |
6448 REM| HISTORY                                                                 |
6449 REM|    Created 9th July 1999 by P.J.Schofield (OPM Development Oracle UK)   |
6450 REM|                                                                         |
6451 REM+=========================================================================+
6452 */
6453 PROCEDURE Report_Error
6454 (
6455   i_error_id       IN VARCHAR2,
6456   i_error_code     IN VARCHAR2,
6457   i_param_1        IN VARCHAR2,
6458   i_param_2        IN VARCHAR2,
6459   i_param_3        IN VARCHAR2,
6460   i_param_4        IN VARCHAR2
6461 )
6462 IS
6463 BEGIN
6464   /* This is temporary, AOL standards for error reporting will be needed */
6465   RAISE_APPLICATION_ERROR ( i_error_id, i_error_code );
6466 END Report_Error;
6467 
6468 /*
6469 REM+=========================================================================+
6470 REM| PROCEDURE NAME                                                          |
6471 REM|    gmp_putline                                                          |
6472 REM| HISTORY                                                                 |
6473 REM|    Created by Rajesh Patangya (OPM Development Oracle US)               |
6474 REM+=========================================================================+
6475 */
6476    /* Define a function that open the log file, makes entry into the log file
6477       and close the log file */
6478    PROCEDURE gmp_putline (
6479        v_text                    IN       VARCHAR2,
6480        v_mode                    IN       VARCHAR2 )
6481    IS
6482       LOG   UTL_FILE.file_type;
6483   BEGIN
6484 
6485     IF p_location IS NOT NULL THEN
6486       LOG    :=
6487              UTL_FILE.fopen ( p_location, 'GMPBMRTB.log', v_mode );
6488       UTL_FILE.put_line ( LOG, v_text );
6489       UTL_FILE.fflush ( LOG );
6490       UTL_FILE.fclose ( LOG );
6491     ELSE
6492       NULL ;
6493     END IF;
6494 END gmp_putline;
6495 
6496 /*
6497 REM+=========================================================================+
6498 REM| PROCEDURE NAME                                                          |
6499 REM|    log_message                                                          |
6500 REM|                                                                         |
6501 REM| TYPE                                                                    |
6502 REM|    Private                                                              |
6503 REM| USAGE                                                                   |
6504 REM|                                                                         |
6505 REM| DESCRIPTION                                                             |
6506 REM|    This is a general error reporting procedure which logs errors to a   |
6507 REM|    file or to the screen (for debug)                                    |
6508 REM|                                                                         |
6509 REM| PARAMETERS                                                              |
6510 REM|    string    IN VARCHAR2                                                |
6511 REM|                                                                         |
6512 REM| HISTORY                                                                 |
6513 REM|  07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052   |
6514 REM+=========================================================================+
6515 */
6516 
6517 PROCEDURE LOG_MESSAGE(pBUFF  IN  VARCHAR2) IS
6518 BEGIN
6519   IF v_cp_enabled THEN
6520      IF fnd_global.conc_request_id > 0  THEN
6521          FND_FILE.PUT_LINE( FND_FILE.LOG, pBUFF);
6522      ELSE
6523          null;
6524      END IF;
6525    ELSE
6526          null;
6527    END IF;
6528 
6529   EXCEPTION
6530      WHEN OTHERS THEN
6531         RETURN;
6532 END LOG_MESSAGE;
6533 
6534 /*
6535 REM+=========================================================================+
6536 REM| PROCEDURE NAME                                                          |
6537 REM|    time_stamp                                                           |
6538 REM| DESCRIPTION                                                             |
6539 REM|                                                                         |
6540 REM| HISTORY                                                                 |
6541 REM| 07/14/2002   Rajesh Patangya - Reorgnized the complete code B2314052    |
6542 REM+=========================================================================+
6543 */
6544 PROCEDURE time_stamp IS
6545 
6546   cur_time VARCHAR2(25) ;
6547 BEGIN
6548   cur_time := NULL ;
6549 
6550    SELECT to_char(sysdate,'MM/DD/YYYY HH24:MI:SS')
6551    INTO cur_time FROM sys.dual ;
6552 
6553    log_message(cur_time);
6554   EXCEPTION
6555     WHEN OTHERS THEN
6556         log_message('Failure occured in time_stamp');
6557         log_message(sqlerrm);
6558       RAISE;
6559 END time_stamp ;
6560 
6561 /*
6562 REM+=========================================================================+
6563 REM| PROCEDURE NAME                                                          |
6564 REM|    msc_inserts                                                          |
6565 REM| DESCRIPTION                                                             |
6566 REM|    All the Bulk insert to MSC tables for OPM data                       |
6567 REM|                                                                         |
6568 REM| HISTORY                                                                 |
6569 REM| 03/12/2004   Created Rajesh Patangya                                    |
6570 REM|                                                                         |
6571 REM+=========================================================================+
6572 */
6573 PROCEDURE msc_inserts
6574 (
6575   return_status  OUT NOCOPY BOOLEAN
6576 )
6577 IS
6578 
6579 stmt_no   NUMBER ;
6580 i         integer ;
6581 v_dummy   NUMBER; /* Penncolor -- Vpedarla Bug: 8230710 */
6582 
6583 BEGIN
6584 stmt_no   := 0 ;
6585 i         := 1;
6586 v_dummy   := 0; /* Penncolor -- Vpedarla Bug: 8230710 */
6587 
6588 /* --------------------------- Process Effectivity Insert ------------------------- */
6589 
6590      stmt_no := 901 ;
6591      i := 1 ;
6592 
6593      IF pef_organization_id.FIRST > 0 THEN
6594      FORALL i IN pef_organization_id.FIRST..pef_organization_id.LAST
6595       INSERT INTO msc_st_process_effectivity (
6596              process_sequence_id,
6597              item_id,
6598              organization_id,
6599              effectivity_date,
6600              disable_date,
6601              minimum_quantity,
6602              maximum_quantity,
6603              preference,
6604              routing_sequence_id,
6605              bill_sequence_id,
6606              sr_instance_id,
6607              item_process_cost, line_id,
6608              total_product_cycle_time, primary_line_flag, production_line_rate,
6609 	     -- Bug: 8715318 Vpedarla uncommented the below line
6610              -- recipe,      /* B5584507 */
6611              deleted_flag,
6612              last_update_date,
6613              last_updated_by,
6614              creation_date,
6615              created_by )
6616              VALUES
6617              (
6618              pef_process_sequence_id(i) ,
6619              pef_item_id(i) ,
6620              pef_organization_id(i) ,
6621              pef_effectivity_date(i) ,
6622              pef_disable_date(i) ,
6623              pef_minimum_quantity(i) ,
6624              pef_maximum_quantity(i) ,
6625              pef_preference(i) ,
6626              pef_routing_sequence_id(i) ,
6627              pef_bill_sequence_id(i) ,
6628              pef_sr_instance_id(i) ,
6629              null_value, null_value,
6630              null_value, null_value, null_value,
6631 	     -- Bug: 8715318 Vpedarla uncommented the below line
6632              -- pef_recipe(i),                    /* B5584507 */
6633              2     ,                           /* Deleted Flag */
6634              pef_last_update_date(i) ,
6635              0  ,                              /* Last Updated By */
6636              pef_creation_date(i)    ,
6637              0                                /* Created By */
6638              ) ;
6639      END IF;
6640 /* -------------------------------  BOM Insert --------------------------- */
6641      stmt_no := 902 ;
6642      i := 1 ;              /* B3837959 MMK Issue */
6643 
6644      IF bom_organization_id.FIRST > 0 THEN
6645      FORALL i IN bom_organization_id.FIRST..bom_organization_id.LAST
6646        INSERT INTO msc_st_boms (
6647 	 bill_sequence_id,
6648          sr_instance_id,
6649          organization_id,
6650 	 assembly_item_id,
6651          assembly_type,
6652          alternate_bom_designator,
6653 	 specific_assembly_comment,
6654          scaling_type,
6655          assembly_quantity,
6656 	 uom,
6657 /* NAMIT_CR */
6658          operation_seq_num,
6659          deleted_flag,
6660          last_update_date,
6661          last_updated_by,
6662          creation_date,
6663          created_by )
6664 	 VALUES
6665          (
6666          bom_bill_sequence_id(i),
6667          bom_sr_instance_id(i)  ,
6668          bom_organization_id(i) ,
6669          bom_assembly_item_id(i),
6670          1   ,                             /* Assembly Type  */
6671          bom_alternate_bom_designator(i),
6672          bom_specific_assembly_comment(i),
6673          bom_scaling_type(i)     ,
6674          bom_assembly_quantity(i),
6675          bom_uom(i)              ,
6676 /* NAMIT_CR */
6677          bom_op_seq_number(i)    ,
6678 	 2     ,                           /* Deleted Flag */
6679          bom_last_update_date(i) ,
6680          0  ,                              /* Last Updated By */
6681          bom_creation_date(i)    ,
6682          0                                 /* Created By */
6683          ) ;
6684       END IF;
6685 
6686 
6687 /* --------------------------- BOM Components Insert Stars ------------------------- */
6688 
6689      stmt_no := 903 ;
6690      i := 1 ;
6691      IF bomc_organization_id.FIRST > 0 THEN
6692      FORALL i IN bomc_organization_id.FIRST..bomc_organization_id.LAST
6693         INSERT INTO msc_st_bom_components
6694        (
6695        component_sequence_id,
6696        sr_instance_id,
6697        organization_id,
6698        Inventory_item_id,
6699        using_assembly_id,
6700        bill_sequence_id,
6701        component_type,
6702        scaling_type,
6703        change_notice,
6704        revision,
6705        uom_code,
6706        usage_quantity,
6707        effectivity_date,
6708        /* NAMIT_ASQC */
6709        contribute_to_step_qty,
6710        disable_date,
6711        from_unit_number,
6712        to_unit_number,
6713        use_up_code,
6714        suggested_effectivity_date,
6715        driving_item_id,
6716        operation_offset_percent,
6717        optional_component,
6718        old_effectivity_date,
6719        wip_supply_type,
6720        planning_factor,
6721        atp_flag,
6722        component_yield_factor,
6723        deleted_flag,
6724        last_update_date,
6725        last_updated_by,
6726        creation_date,
6727        created_by,
6728        scale_multiple,
6729        scale_rounding_variance,
6730        rounding_direction
6731        )
6732        VALUES
6733        (
6734        bomc_component_sequence_id(i),
6735        bomc_sr_instance_id(i),
6736        bomc_organization_id(i),
6737        bomc_Inventory_item_id(i),
6738        bomc_using_assembly_id(i),
6739        bomc_bill_sequence_id(i),
6740        bomc_component_type(i),
6741        bomc_scaling_type(i),
6742        null_value,
6743        null_value,
6744        bomc_uom_code(i),
6745        bomc_usage_quantity(i),
6746        bomc_effectivity_date(i),
6747         /* NAMIT_ASQC */
6748        bomc_contribute_to_step_qty(i),
6749        bomc_disable_date(i), /* B5176291 - Item substitution changes */
6750        null_value,
6751        null_value,
6752        null_value,
6753        null_value,
6754        null_value,
6755        bomc_opr_offset_percent(i),
6756        bomc_optional_component(i),
6757        null_value,
6758        bomc_wip_supply_type(i),
6759        null_value,
6760        1,                                /* atp flag */
6761        1,                                /* component_yield_factor */
6762        2     ,                           /* Deleted Flag */
6763        bomc_last_update_date(i) ,
6764        0  ,                              /* Last Updated By */
6765        bomc_creation_date(i)    ,
6766        0 ,                               /* Created By */
6767        bomc_scale_multiple(i),
6768        bomc_scale_rounding_variance(i),
6769        bomc_rounding_direction(i)
6770        );
6771       END IF;
6772 
6773 /* --------------------------- Routing Insert Stars ------------------------- */
6774      stmt_no := 904 ;
6775      i := 1 ;
6776      IF rtg_organization_id.FIRST > 0 THEN
6777      FORALL i IN rtg_organization_id.FIRST..rtg_organization_id.LAST
6778           INSERT INTO msc_st_routings (
6779            routing_sequence_id,
6780            sr_instance_id,
6781            routing_type,
6782            routing_comment,
6783            alternate_routing_designator,
6784            project_id,
6785            task_id,
6786            line_id,
6787            uom_code,
6788            cfm_routing_flag,
6789            ctp_flag,
6790            routing_quantity,
6791            assembly_item_id,
6792            organization_id,
6793            auto_step_qty_flag,
6794            deleted_flag,
6795            last_update_date,
6796            last_updated_by,
6797            creation_date,
6798            created_by )
6799            VALUES (
6800            rtg_routing_sequence_id(i),
6801            rtg_sr_instance_id(i),
6802            1 ,                                    /* routing_type */
6803            rtg_routing_comment(i),
6804            rtg_alt_routing_designator(i),
6805            null_value,
6806            null_value,
6807            null_value,
6808            rtg_uom_code(i),
6809            null_value,
6810            null_value,
6811            rtg_routing_quantity(i),
6812            rtg_assembly_item_id(i),
6813            rtg_organization_id(i),
6814            rtg_auto_step_qty_flag(i),
6815            2,                                   /* Deleted Flag */
6816            rtg_last_update_date(i),             /* Last Update Date */
6817            0,
6818            rtg_creation_date(i),                /* Creation Date */
6819            0 ) ;
6820       END IF;
6821 
6822 /* ----------------------- Operation Resource  Insert --------------------- */
6823      stmt_no := 905 ;
6824      i := 1 ;
6825 
6826   /*
6827      for i in or_operation_sequence_id.FIRST..or_operation_sequence_id.LAST
6828      loop
6829         log_message('========');
6830         log_message('Op seq id '||or_operation_sequence_id(i)) ;
6831         log_message('Res Seq Num '||or_resource_seq_num(i)) ;
6832         log_message('Resource id '||or_resource_id(i)) ;
6833         log_message(' Alternate Num ' ||or_alternate_number(i));
6834         log_message( 'principal flag '||or_principal_flag(i));
6835         log_message( 'Basis type '||or_basis_type(i));
6836         log_message( 'resource usage '||or_resource_usage(i));
6837         log_message( 'Max rsrc units '||or_max_resource_units(i)) ;
6838         log_message(' rsrc units '||or_resource_units(i)) ;
6839         log_message(' uom code '||or_uom_code(i)) ;
6840         log_message('Min capacity '||or_minimum_capacity(i)) ;
6841         log_message('Max capacity '||or_maximum_capacity(i)) ;
6842         log_message('Setup Id '||or_setup_id(i)) ;
6843         log_message('========');
6844      end loop;
6845     */
6846 
6847 --
6848      IF or_operation_sequence_id.FIRST > 0 THEN
6849      FORALL i IN or_operation_sequence_id.FIRST..or_operation_sequence_id.LAST
6850         INSERT INTO msc_st_operation_resources (
6851         operation_sequence_id,
6852         resource_seq_num,
6853         resource_id,
6854         alternate_number,
6855         principal_flag,
6856         basis_type,
6857         resource_usage,
6858         max_resource_units,
6859         resource_units,
6860         uom_code,
6861         deleted_flag,
6862         sr_instance_id,
6863         routing_sequence_id,
6864         organization_id,
6865         minimum_capacity,
6866         maximum_capacity,
6867         setup_id,
6868         orig_resource_seq_num,
6869         breakable_activity_flag,
6870         last_update_date,
6871         last_updated_by,
6872         creation_date,
6873         created_by )
6874         VALUES (
6875         or_operation_sequence_id(i) ,
6876         or_resource_seq_num(i) ,
6877         or_resource_id(i) ,
6878         or_alternate_number(i) ,
6879         or_principal_flag(i) ,
6880         or_basis_type(i) ,
6881         or_resource_usage(i) ,
6882         or_max_resource_units(i) ,
6883         or_resource_units(i) ,
6884         or_uom_code(i) ,
6885         2,
6886         or_sr_instance_id(i) ,
6887         or_routing_sequence_id(i) ,
6888         or_organization_id(i),
6889         or_minimum_capacity(i),
6890         or_maximum_capacity(i),
6891         or_setup_id(i),
6892         or_orig_rs_seq_num(i),
6893         or_break_ind(i),
6894         or_last_update_date(i) ,
6895         0,
6896         or_creation_date(i) ,
6897         0 );
6898       END IF;
6899 /* ----------------------- Operations Insert --------------------- */
6900      stmt_no := 906 ;
6901      i := 1 ;
6902      IF opr_operation_sequence_id.FIRST > 0 THEN
6903      FORALL i IN opr_operation_sequence_id.FIRST..opr_operation_sequence_id.LAST
6904        INSERT INTO msc_st_routing_operations (
6905        operation_sequence_id,
6906        routing_sequence_id,
6907        operation_seq_num,
6908        sr_instance_id,
6909        operation_description,
6910        effectivity_date,
6911        disable_date,
6912        from_unit_number,
6913        to_unit_number,
6914        option_dependent_flag,
6915        operation_type,
6916        minimum_transfer_quantity,
6917        yield,
6918 /* NAMIT_ASQC */
6919        step_quantity,
6920        step_quantity_uom,
6921        department_id,
6922        department_code,
6923        operation_lead_time_percent,
6924        cumulative_yield,
6925        reverse_cumulative_yield,
6926        net_planning_percent,
6927        setup_duration,
6928        tear_down_duration,
6929        uom_code,
6930        organization_id,
6931        standard_operation_code,
6932        deleted_flag,
6933        last_update_date,
6934        last_updated_by,
6935        creation_date,
6936        created_by )
6937        VALUES
6938        (
6939        opr_operation_sequence_id(i),
6940        opr_routing_sequence_id(i),
6941        opr_operation_seq_num(i),
6942        opr_sr_instance_id(i),
6943        opr_operation_description(i),
6944        opr_effectivity_date(i),
6945        null_value,
6946        null_value,
6947        null_value,
6948        1,
6949        null_value,
6950        opr_mtransfer_quantity(i),    /*B2870041*/
6951        null_value, /*  B2365684 rtg_org_dtl_tab(loop_index).step_qty, */
6952 /* NAMIT_ASQC */
6953        opr_step_qty(i),
6954        opr_step_qty_uom(i),
6955        opr_department_id(i),
6956        opr_department_code(i),
6957        opr_lead_time_percent(i),  -- Bug: 7391495 Vpedarla
6958        null_value,
6959        null_value,
6960        null_value,
6961        null_value,
6962        null_value,
6963        opr_uom_code(i),
6964        opr_organization_id(i),
6965        null_value ,
6966        2,                    /* Deleted Flag */
6967        opr_last_update_date(i),
6968        0,
6969        opr_creation_date(i),
6970        0 ) ;
6971       END IF;
6972 
6973 /* ----------------------- Operation Sequence Insert --------------------- */
6974      stmt_no := 907 ;
6975      i := 1 ;
6976      IF rs_operation_sequence_id.FIRST > 0 THEN
6977      FORALL i IN rs_operation_sequence_id.FIRST..rs_operation_sequence_id.LAST
6978        INSERT INTO msc_st_operation_resource_seqs (
6979        operation_sequence_id,
6980        resource_seq_num,
6981        sr_instance_id,
6982        department_id,
6983        resource_offset_percent,
6984        schedule_flag,
6985        routing_sequence_id,
6986        organization_id,
6987        deleted_flag,
6988        last_update_date,
6989        last_updated_by,
6990        creation_date,
6991        created_by,
6992        activity_group_id )
6993        VALUES (
6994        rs_operation_sequence_id(i),
6995        rs_resource_seq_num(i),
6996        rs_sr_instance_id(i),
6997        rs_department_id(i),
6998        null_value,
6999        rs_schedule_flag(i),
7000        rs_routing_sequence_id(i),
7001        rs_organization_id(i),
7002        2,                         /* deleted flag */
7003        rs_last_update_date(i),
7004        0,
7005        rs_creation_date(i),
7006        0 ,
7007        rs_activity_group_id(i)
7008        ) ;
7009       END IF;
7010 
7011 /* ----------------------- Operation Component Insert --------------------- */
7012      stmt_no := 908 ;
7013      i := 1 ;
7014   /*
7015      for i in oc_operation_sequence_id.FIRST..oc_operation_sequence_id.LAST
7016      loop
7017         log_message('========');
7018         log_message('Op seq id '||oc_operation_sequence_id(i)) ;
7019         log_message('Comp seq id '||oc_component_sequence_id(i)) ;
7020         log_message('Instance '||oc_sr_instance_id(i)) ;
7021         log_message('Bill Seq '||oc_bill_sequence_id(i)) ;
7022         log_message('Routing Seq '||oc_routing_sequence_id(i)) ;
7023         log_message('Org Id '||oc_organization_id(i)) ;
7024      end loop;
7025   */
7026      IF oc_operation_sequence_id.FIRST > 0 THEN
7027      FORALL i IN oc_operation_sequence_id.FIRST..oc_operation_sequence_id.LAST
7028       INSERT INTO msc_st_operation_components (
7029       operation_sequence_id, component_sequence_id, sr_instance_id,
7030       bill_sequence_id, routing_sequence_id, organization_id,
7031       deleted_flag, last_update_date, last_updated_by,
7032       creation_date, created_by )
7033       VALUES (
7034       oc_operation_sequence_id(i),
7035       oc_component_sequence_id(i),
7036       oc_sr_instance_id(i),
7037       oc_bill_sequence_id(i),
7038       oc_routing_sequence_id(i),
7039       oc_organization_id(i),
7040       2,
7041       oc_last_update_date(i),
7042       0,
7043       oc_creation_date(i),
7044       0   ) ;
7045 
7046       END IF;
7047 
7048 /* ----------------------- MTQ Insert --------------------- */
7049 /*  NAMIT_MTQ */
7050 
7051      stmt_no := 909 ;
7052      i := 1 ;
7053      IF itm_mtq_from_op_seq_id.FIRST > 0 THEN
7054      FORALL i IN itm_mtq_from_op_seq_id.FIRST..itm_mtq_from_op_seq_id.LAST
7055        INSERT INTO msc_st_operation_networks(
7056        from_op_seq_id,
7057        routing_sequence_id,
7058        dependency_type,
7059        transition_type,
7060        plan_id,
7061        sr_instance_id,
7062        deleted_flag,
7063        from_item_id,
7064        organization_id,
7065        minimum_transfer_qty,
7066        minimum_time_offset,
7067        maximum_time_offset,
7068        last_update_date,
7069        last_updated_by,
7070        creation_date,
7071        created_by,
7072        from_op_seq_num,
7073        planning_pct    -- Bug: 6407973 KBANDDYO Added the column to send default value of 100
7074        ) VALUES
7075        (
7076         itm_mtq_from_op_seq_id(i),
7077         itm_mtq_routing_sequence_id(i),
7078         5, /* MTQ with Hardlink */
7079         1, /* Primary */
7080         -1,
7081         itm_mtq_sr_instance_id(i),
7082         2,
7083         itm_mtq_from_item_id(i),
7084         itm_mtq_organization_id(i),
7085         itm_mtq_min_tran_qty(i),
7086         itm_mtq_min_time_offset(i),
7087         itm_mtq_max_time_offset(i),
7088         itm_mtq_last_update_date(i),
7089         0,
7090         itm_mtq_creation_date(i),
7091         0,
7092         itm_mtq_frm_op_seq_num(i),
7093 	100           -- Bug:  6407973 KBANDDYO added the column to send default value of 100
7094        );
7095 
7096       END IF;
7097 
7098 /* ----------------------- Step Dependency Insert --------------------- */
7099 /* NAMIT_CR */
7100 
7101      stmt_no := 910 ;
7102      i := 1 ;
7103      IF opr_stpdep_frm_seq_id.FIRST > 0 THEN
7104      FORALL i IN opr_stpdep_frm_seq_id.FIRST..opr_stpdep_frm_seq_id.LAST
7105        INSERT INTO msc_st_operation_networks(
7106        from_op_seq_id,
7107        to_op_seq_id,
7108        routing_sequence_id,
7109        dependency_type,
7110        transition_type,
7111        plan_id,
7112        sr_instance_id,
7113        deleted_flag,
7114        minimum_time_offset,
7115        maximum_time_offset,
7116        transfer_pct,
7117 -- Bug:  6407973 KBANDDYO inserting into column planning_pct along with transfer_pct
7118        planning_pct,
7119        last_update_date,
7120        last_updated_by,
7121        creation_date,
7122        created_by,
7123        from_op_seq_num,
7124        to_op_seq_num,
7125        apply_to_charges,
7126        organization_id
7127        ) VALUES
7128        (
7129         opr_stpdep_frm_seq_id(i),
7130         opr_stpdep_to_seq_id(i),
7131         opr_stpdep_routing_sequence_id(i),
7132         opr_stpdep_dependency_type(i),
7133         1, /* Transition Type, 1 = Primary*/
7134         -1, /* Plan Id */
7135         opr_stpdep_sr_instance_id(i),
7136         2, /* Deleted Flag */
7137         opr_stpdep_min_time_offset(i),
7138         opr_stpdep_max_time_offset(i),
7139         opr_stpdep_trans_pct(i),
7140 	opr_stpdep_trans_pct(i),   -- Bug: 6407973 KBANDDYO
7141         opr_stpdep_last_update_date(i), /* 7363807 */
7142         0,
7143         opr_stpdep_creation_date(i), /* 7363807 */
7144         0,
7145         opr_stpdep_frm_op_seq_num(i),
7146         opr_stpdep_to_op_seq_num(i),
7147         opr_stpdep_app_to_chrg(i),
7148         opr_stpdep_organization_id(i)
7149        );
7150 
7151       END IF;
7152 
7153        -- Vpedarla Bug: 8230710
7154 
7155        SELECT st.VALUE INTO v_dummy from V$MYSTAT st, V$STATNAME sn
7156        WHERE st.STATISTIC# = sn.STATISTIC#
7157        AND sn.NAME in ('session pga memory max');
7158        log_message('MSC_INSERTS-Before Freeing up. Session pga memory max = ' || to_char(v_dummy) );
7159 
7160        SELECT st.VALUE INTO v_dummy from V$MYSTAT st, V$STATNAME sn
7161        where st.STATISTIC# = sn.STATISTIC#
7162        and sn.NAME in ('session pga memory');
7163        log_message('MSC_INSERTS-Before Freeing up. Session pga memory = ' || TO_CHAR(v_dummy) );
7164 
7165               bom_organization_id.delete  ;
7166               bomc_organization_id.delete ;
7167               pef_organization_id.delete ;
7168               rtg_organization_id.delete ;
7169               oc_organization_id.delete ;
7170               opr_organization_id.delete   ;
7171               or_organization_id.delete ;
7172               rs_organization_id.delete ;
7173 
7174               bom_bill_sequence_id.delete ;
7175               bomc_bill_sequence_id.delete ;
7176               pef_bill_sequence_id.delete ;
7177               oc_bill_sequence_id.delete ;
7178 
7179               bom_last_update_date.delete ;
7180               bomc_last_update_date.delete ;
7181               pef_last_update_date.delete ;
7182               rtg_last_update_date.delete ;
7183               or_last_update_date.delete ;
7184               opr_last_update_date.delete ;
7185               rs_last_update_date.delete ;
7186               oc_last_update_date.delete ;
7187 
7188               bom_creation_date.delete ;
7189               bomc_creation_date.delete ;
7190               pef_creation_date.delete ;
7191               rtg_creation_date.delete ;
7192               or_creation_date.delete ;
7193               opr_creation_date.delete ;
7194               rs_creation_date.delete ;
7195               oc_creation_date.delete ;
7196 
7197               pef_effectivity_date.delete ;
7198               bomc_effectivity_date.delete ;
7199               opr_effectivity_date.delete ;
7200               opr_lead_time_percent.delete ;
7201 
7202               rtg_routing_sequence_id.delete ;
7203               pef_routing_sequence_id.delete ;
7204               or_routing_sequence_id.delete ;
7205               opr_routing_sequence_id.delete ;
7206               rs_routing_sequence_id.delete ;
7207               oc_routing_sequence_id.delete ;
7208 
7209 	     -- Bug: 8715318 Vpedarla
7210               pef_recipe.delete ;
7211 
7212               bomc_uom_code.delete;
7213               rtg_uom_code.delete;
7214               or_uom_code.delete ;
7215               opr_uom_code.delete;
7216 
7217               bom_assembly_item_id.delete ;
7218               rtg_assembly_item_id.delete ;
7219 
7220               bomc_component_sequence_id.delete ;
7221               oc_component_sequence_id.delete ;
7222 
7223               or_operation_sequence_id.delete  ;
7224               opr_operation_sequence_id.delete ;
7225               rs_operation_sequence_id.delete ;
7226               oc_operation_sequence_id.delete ;
7227 
7228               or_resource_seq_num.delete ;
7229               rs_resource_seq_num.delete ;
7230 
7231             /* -- Pef Variable Initialization -- */
7232               pef_process_sequence_id.delete ;
7233               pef_item_id.delete ;
7234               pef_disable_date.delete ;
7235               pef_minimum_quantity.delete ;
7236               pef_maximum_quantity.delete ;
7237               pef_preference.delete ;
7238               pef_index := 0 ;
7239 
7240             /* -- BOM Variable Initialization -- */
7241               bom_alternate_bom_designator.delete ;
7242               bom_specific_assembly_comment.delete ;
7243               bom_scaling_type.delete ;
7244               bom_assembly_quantity.delete ;
7245               bom_uom.delete ;
7246               bom_index := 0 ;
7247 
7248             /* -- BOMC Variable Initialization -- */
7249               bomc_Inventory_item_id.delete ;
7250               bomc_using_assembly_id.delete ;
7251               bomc_component_type.delete ;
7252               bomc_scaling_type.delete ;
7253               bomc_usage_quantity.delete ;
7254               bomc_opr_offset_percent.delete  ;
7255               bomc_optional_component.delete  ;
7256               bomc_wip_supply_type.delete ;
7257               bomc_scale_multiple.delete  ;
7258               bomc_scale_rounding_variance.delete ;
7259               bomc_rounding_direction.delete  ;
7260               bomc_index := 0 ;
7261 
7262             /* -- Rtg Variable Initialization -- */
7263               rtg_routing_comment.delete ;
7264               rtg_alt_routing_designator.delete ;
7265               rtg_routing_quantity.delete ;
7266               rtg_index := 0 ;
7267 
7268             /* -- Or Variable Initialization -- */
7269               or_resource_id.delete ;
7270               or_alternate_number.delete ;
7271               or_principal_flag.delete ;
7272               or_basis_type.delete ;
7273               or_resource_usage.delete ;
7274               or_max_resource_units.delete ;
7275               or_resource_units.delete ;
7276               or_index := 0 ;
7277 
7278             /* -- Opr Variable Initialization -- */
7279               opr_operation_seq_num.delete ;
7280               opr_operation_description.delete ;
7281               opr_mtransfer_quantity.delete ;
7282               opr_department_id.delete ;
7283               rs_department_id.delete ;
7284               opr_department_code.delete ;
7285               rs_activity_group_id.delete ;
7286               rs_schedule_flag.delete ;
7287               opr_index := 0 ;
7288               rs_index := 0 ;
7289               oc_index := 0 ;
7290 
7291               -- bug: 6851919 Vpedarla
7292               mtq_index := 0;
7293               g_dep_index := 1 ;
7294               opr_stpdep_frm_seq_id.delete;
7295               opr_stpdep_to_seq_id.delete;
7296               opr_stpdep_routing_sequence_id.delete;
7297               opr_stpdep_dependency_type.delete;
7298               opr_stpdep_app_to_chrg.delete;
7299               opr_stpdep_sr_instance_id.delete;
7300               opr_stpdep_organization_id.delete;
7301               opr_stpdep_frm_op_seq_num.delete;
7302               opr_stpdep_to_op_seq_num.delete;
7303               opr_stpdep_trans_pct.delete;
7304               opr_stpdep_min_time_offset.delete;
7305               opr_stpdep_max_time_offset.delete;
7306 
7307               itm_mtq_frm_op_seq_num.delete;
7308               itm_mtq_max_time_offset.delete;
7309               itm_mtq_min_tran_qty.delete;
7310               itm_mtq_organization_id.delete;
7311               itm_mtq_from_item_id.delete;
7312               itm_mtq_sr_instance_id.delete;
7313               itm_mtq_routing_sequence_id.delete;
7314               itm_mtq_from_op_seq_id.delete;
7315 
7316         dbms_session.free_unused_user_memory;
7317 
7318       -- Vpedarla Bug: 8230710
7319       SELECT st.VALUE INTO v_dummy from V$MYSTAT st, V$STATNAME sn
7320       WHERE st.STATISTIC# = sn.STATISTIC#
7321         AND sn.NAME in ('session pga memory max');
7322       log_message('MSC_INSERTS-After Freeing up2. Session pga memory max = ' || to_char(v_dummy) );
7323 
7324       SELECT st.VALUE INTO v_dummy from V$MYSTAT st, V$STATNAME sn
7325       where st.STATISTIC# = sn.STATISTIC#
7326         and sn.NAME in ('session pga memory');
7327       log_message('MSC_INSERTS-After Freeing up2. Session pga memory = ' || TO_CHAR(v_dummy) );
7328 
7329         return_status := TRUE ;
7330 EXCEPTION
7331    WHEN OTHERS THEN
7332     log_message('Error in MSC Inserts : '||stmt_no || ':' || sqlerrm);
7333     return_status := FALSE;
7334 
7335 END msc_inserts ;
7336 
7337 /*
7338 REM+=========================================================================+
7339 REM| PROCEDURE NAME                                                          |
7340 REM|    write_setups_and_transitions                                         |
7341 REM|                                                                         |
7342 REM| DESCRIPTION                                                             |
7343 REM|    This procedure inserts rows into msc_st_resource_setups and          |
7344 REM|    msc_st_setup_transitions                                             |
7345 REM|                                                                         |
7346 REM| HISTORY                                                                 |
7347 REM|  02/03/2006 B4918786 Rajesh Patangya Rewrite for SDS Enhancement        |
7348 REM|   MSC_RESOURCE_SETUPS unique key is ON                                  |
7349 REM|   Instance_id,resource_id,organization_id and setup_id                  |
7350 REM|   02-20-2007 B5741664 Added a join with mtl_parameters to filter        |
7351 REM|    rows for process organization - similar to 11.5.10 code              |
7352 REM|   08-01-2008 B6710684 Vpedarla Added one more in parameter              |
7353 REM |  08-06-2011 B11692192 Modified the cursor Fact_tran for performance    |
7354 REM+=========================================================================+
7355 */
7356 PROCEDURE write_setups_and_transitions
7357 (
7358   at_apps_link   IN VARCHAR2,
7359   return_status  OUT NOCOPY BOOLEAN
7360 )  IS
7361 
7362    l_profile            VARCHAR2(4);
7363    stmt_no              NUMBER;
7364    Zero_tran            VARCHAR2(32767);
7365    fact_tran            VARCHAR2(32767);
7366    rsrc_setup           VARCHAR2(32767);
7367 BEGIN
7368 
7369    Zero_tran   := NULL ;
7370    fact_tran   := NULL ;
7371    rsrc_setup  := NULL ;
7372 
7373 LOG_MESSAGE('write_setups_and_transitions started ' );
7374 time_stamp;
7375 /* bug: 6710684 Vpedarla making changes to take the profile value from source server
7376            and also made changes to use procedure get_profile_value */
7377 --       commented the below code line
7378 --      l_profile       := FND_PROFILE.VALUE('BOM:HOUR_UOM_CODE');
7379         l_profile       := get_profile_value('BOM:HOUR_UOM_CODE' , at_apps_link);
7380 
7381      -- ZERO Transitions  (Alternate Resources are considered)
7382      stmt_no := 910 ;
7383      Zero_tran := ' INSERT INTO msc_st_setup_transitions ( '
7384      ||'    resource_id,          '
7385      ||'    organization_id,     '
7386      ||'    from_setup_id,        '
7387      ||'    to_setup_id,         '
7388      ||'    transition_time,      '
7389      ||'    transition_penalty,   '
7390      ||'    transition_uom,       '
7391      ||'    sr_instance_id,     '
7392      ||'    deleted_flag )   '
7393      ||' SELECT  '
7394      ||'    ((a.resource_id * 2) + 1),'
7395      ||'    a.organization_id, '
7396      ||'    a.seq_dep_id, '
7397      ||'    b.seq_dep_id, '
7398      ||'    0 setup_time, '
7399      ||'    0 penalty_factor, '
7400      ||'    :profile, '
7401      ||'    :instance1 , '
7402      ||'    2 '
7403      ||' FROM ( '
7404      ||' SELECT '
7405      ||' rd.organization_id, '
7406      ||' s.category_id, '
7407      ||' s.seq_dep_id, '
7408      ||' o.oprn_id, '
7409      ||' rd.resource_id, '
7410      ||' count(o.oprn_id) OVER (PARTITION BY rd.organization_id,s.category_id,rd.resource_id) CNT '
7411      ||' FROM '
7412      ||'     cr_rsrc_dtl'||at_apps_link||' rd, '
7413      ||'     gmp_sequence_types'||at_apps_link||' s, '
7414      ||'     gmd_operation_resources'||at_apps_link||' r, '
7415      ||'     gmd_operation_activities'||at_apps_link||' a, '
7416      ||'     gmd_operations'||at_apps_link||' o '
7417      ||' WHERE o.oprn_id = a.oprn_id '
7418      ||' AND a.oprn_line_id = r.oprn_line_id '
7419      ||' AND a.sequence_dependent_ind = 1 '
7420      ||' AND r.prim_rsrc_ind = 1 '
7421      ||' AND r.resources = rd.resources '
7422      ||' AND o.oprn_id = s.oprn_id ' ;
7423 
7424      IF l_in_str_org  IS NOT NULL THEN
7425         Zero_tran := Zero_tran
7426         ||'   AND rd.organization_id ' || l_in_str_org  ;
7427      END IF;
7428 
7429      Zero_tran := Zero_tran
7430      ||' UNION ALL '
7431      ||' SELECT '
7432      ||' rd.organization_id, '
7433      ||' s.category_id, '
7434      ||' s.seq_dep_id, '
7435      ||' o.oprn_id, '
7436      ||' rd.alt_resource_id, '
7437      ||' count(o.oprn_id) OVER (PARTITION BY rd.organization_id,s.category_id,rd.alt_resource_id) CNT '
7438      ||' FROM '
7439      ||'     gmp_sequence_types'||at_apps_link||' s, '
7440      ||'     gmd_operation_resources'||at_apps_link||' r, '
7441      ||'     gmd_operation_activities'||at_apps_link||' a, '
7442      ||'     gmd_operations'||at_apps_link||' o, '
7443      ||' (SELECT pcrd.resource_id prim_resource_id, '
7444      ||'         pcrd.resources prim_resources,  '
7445      ||'         acrd.resource_id alt_resource_id,  '
7446      ||'         acrd.resources alt_resources, '
7447      ||'         acrd.organization_id  '
7448      ||'                     FROM  cr_rsrc_dtl'||at_apps_link||' acrd,  '
7449      ||'                           cr_rsrc_dtl'||at_apps_link||' pcrd,  '
7450      ||'                           cr_ares_mst'||at_apps_link||' cam  '
7451      ||'                     WHERE cam.alternate_resource = acrd.resources  '
7452      ||'                       AND cam.primary_resource = pcrd.resources  '
7453      ||'                       AND acrd.organization_id = pcrd.organization_id  ' ;
7454 
7455      IF l_in_str_org  IS NOT NULL THEN
7456         Zero_tran := Zero_tran
7457         ||'   AND acrd.organization_id ' || l_in_str_org  ;
7458      END IF;
7459 
7460      Zero_tran := Zero_tran
7461      ||'                       AND acrd.delete_mark = 0   '
7462      ||'                     ORDER BY pcrd.resource_id ) rd '
7463      ||' WHERE o.oprn_id = a.oprn_id '
7464      ||' AND a.oprn_line_id = r.oprn_line_id '
7465      ||' AND a.sequence_dependent_ind = 1 '
7466      ||' AND r.prim_rsrc_ind = 1 '
7467      ||' AND o.oprn_id = s.oprn_id '
7468      ||' AND r.resources = rd.prim_resources '
7469      ||' ) a, '
7470      ||' ( '
7471      ||' SELECT '
7472      ||' rd.organization_id, '
7473      ||' s.category_id, '
7474      ||' s.seq_dep_id, '
7475      ||' o.oprn_id, '
7476      ||' rd.resource_id, '
7477      ||' count(o.oprn_id) OVER (PARTITION BY rd.organization_id,s.category_id,rd.resource_id) CNT '
7478      ||' FROM '
7479      ||'     cr_rsrc_dtl'||at_apps_link||' rd, '
7480      ||'     gmp_sequence_types'||at_apps_link||' s, '
7481      ||'     gmd_operation_resources'||at_apps_link||' r, '
7482      ||'     gmd_operation_activities'||at_apps_link||' a, '
7483      ||'     gmd_operations'||at_apps_link||' o '
7484      ||' WHERE o.oprn_id = a.oprn_id '
7485      ||' AND a.oprn_line_id = r.oprn_line_id '
7486      ||' AND a.sequence_dependent_ind = 1 '
7487      ||' AND r.prim_rsrc_ind = 1 '
7488      ||' AND r.resources = rd.resources '
7489      ||' AND o.oprn_id = s.oprn_id ' ;
7490 
7491      IF l_in_str_org  IS NOT NULL THEN
7492       Zero_tran := Zero_tran
7493       ||'   AND rd.organization_id ' || l_in_str_org  ;
7494      END IF;
7495 
7496      Zero_tran := Zero_tran
7497      ||' UNION ALL '
7498      ||' SELECT '
7499      ||' rd.organization_id, '
7500      ||' s.category_id, '
7501      ||' s.seq_dep_id, '
7502      ||' o.oprn_id, '
7503      ||' rd.alt_resource_id, '
7504      ||' count(o.oprn_id) OVER (PARTITION BY rd.organization_id,s.category_id,rd.alt_resource_id) CNT '
7505      ||' FROM '
7506      ||'     gmp_sequence_types'||at_apps_link||' s, '
7507      ||'     gmd_operation_resources'||at_apps_link||' r, '
7508      ||'     gmd_operation_activities'||at_apps_link||' a, '
7509      ||'     gmd_operations'||at_apps_link||' o, '
7510      ||' (SELECT pcrd.resource_id prim_resource_id, '
7511      ||'         pcrd.resources prim_resources,  '
7512      ||'         acrd.resource_id alt_resource_id,  '
7513      ||'         acrd.resources alt_resources, '
7514      ||'         acrd.organization_id  '
7515      ||'                     FROM  cr_rsrc_dtl'||at_apps_link||' acrd,  '
7516      ||'                           cr_rsrc_dtl'||at_apps_link||' pcrd,  '
7517      ||'                           cr_ares_mst'||at_apps_link||' cam  '
7518      ||'                     WHERE cam.alternate_resource = acrd.resources  '
7519      ||'                       AND cam.primary_resource = pcrd.resources  '
7520      ||'                       AND acrd.organization_id = pcrd.organization_id  ' ;
7521 
7522      IF l_in_str_org  IS NOT NULL THEN
7523         Zero_tran := Zero_tran
7524         ||'   AND acrd.organization_id ' || l_in_str_org  ;
7525      END IF;
7526 
7527      Zero_tran := Zero_tran
7528      ||'                       AND acrd.delete_mark = 0   '
7529      ||'                     ORDER BY pcrd.resource_id ) rd '
7530      ||' WHERE o.oprn_id = a.oprn_id '
7531      ||' AND a.oprn_line_id = r.oprn_line_id '
7532      ||' AND a.sequence_dependent_ind = 1 '
7533      ||' AND r.prim_rsrc_ind = 1 '
7534      ||' AND o.oprn_id = s.oprn_id '
7535      ||' AND r.resources = rd.prim_resources '
7536      ||' ORDER BY 1,2,4,3 '
7537      ||' ) b '
7538      ||' WHERE a.organization_id = b.organization_id '
7539      ||'   AND a.category_id = b.category_id '
7540      ||'   AND a.resource_id = b.resource_id '
7541      ||'   AND a.cnt = b.cnt '
7542      ||'   AND a.seq_dep_id <> b.seq_dep_id '
7543      ||'   AND a.cnt > 1 ' ;
7544 
7545   -- Bug: 8293879 Vpedarla
7546   -- As per information from APS, Plan would be generating the un finished SDS matrix with
7547   --  zero setup time and zero transiton penality. So, OPM need not specifically insert the records.
7548   --  EXECUTE IMMEDIATE Zero_tran USING l_profile, instance_id ;
7549 
7550      -- Fact Transitions (Alternate Resources are considered)
7551      stmt_no := 920 ;
7552 
7553      -- Added check for OPM organizations Bug 10011540
7554      -- Vpedarla bug: 8293879 Modified the cursor below
7555      -- rpatangy bug: 11692192 Modified the cursor Fact_tran for performance
7556 
7557      Fact_tran := ' INSERT INTO msc_st_setup_transitions ( '
7558      ||'   resource_id, '
7559      ||'   organization_id, '
7560      ||'   from_setup_id, '
7561      ||'   to_setup_id, '
7562      ||'   transition_time, '
7563      ||'   transition_penalty, '
7564      ||'   transition_uom, '
7565      ||'   sr_instance_id, '
7566      ||'   deleted_flag ) '
7567      ||' SELECT unique  '
7568      ||'   a.resource_id, '
7569      ||'   a.organization_id, '
7570      ||'   a.from_seq_dep_id, '
7571      ||'   a.to_seq_dep_id, '
7572      ||'   a.setup_time, '
7573      ||'   a.penalty_factor, '
7574      ||'   a.uom_code, '
7575      ||'   a.sr_instance_id, '
7576      ||'   a.deleted_flag '
7577      ||' FROM (  '
7578      ||' SELECT unique  '
7579      ||'   b.resource_id, '
7580      ||'   b.organization_id, '
7581      ||'   a.from_seq_dep_id, '
7582      ||'   a.to_seq_dep_id, '
7583      ||'   a.setup_time, '
7584      ||'   a.penalty_factor, '
7585      ||'   b.uom_code, '
7586      ||'   b.sr_instance_id, '
7587      ||'   b.deleted_flag '
7588      ||' FROM gmp_sequence_dependencies'||at_apps_link||' a, '
7589      ||'     (select /*+ noparallel(msc_st_operation_resources) */ unique mt.RESOURCE_ID, mt.ORGANIZATION_ID,'
7590      ||'       mt.SETUP_ID , mt.deleted_flag, mt.sr_instance_id, mt.UOM_CODE '
7591      ||'      FROM mtl_parameters'||at_apps_link|| ' mp, '
7592      ||'       MSC_ST_OPERATION_RESOURCES mt '
7593      ||'      WHERE mp.organization_id = mt.organization_id AND '
7594      ||'            mt.sr_instance_id = :instance1 AND '
7595      ||'            mt.setup_id is not null AND '
7596      ||'            mp.process_enabled_flag = '|| ''''||'Y'||'''' ||' )  b '
7597      ||' WHERE  b.setup_id = a.from_seq_dep_id  ' ;
7598 
7599      IF l_in_str_org  IS NOT NULL THEN
7600         Fact_tran := Fact_tran
7601         ||'   AND b.organization_id ' || l_in_str_org  ;
7602      END IF;
7603 
7604         Fact_tran := Fact_tran
7605      ||' ) a , '
7606      ||'  (select /*+ noparallel(msc_st_operation_resources) */ '
7607      ||'  unique mt.RESOURCE_ID, mt.ORGANIZATION_ID,'
7608      ||'       mt.SETUP_ID , mt.deleted_flag, mt.sr_instance_id, mt.UOM_CODE '
7609      ||'      FROM MSC_ST_OPERATION_RESOURCES mt '
7610      ||'      WHERE mt.sr_instance_id = :instance2  '  ;
7611 
7612      IF l_in_str_org  IS NOT NULL THEN
7613         Fact_tran := Fact_tran
7614         ||'   AND mt.organization_id ' || l_in_str_org  ;
7615      END IF;
7616 
7617         Fact_tran := Fact_tran
7618      ||'         AND mt.setup_id is not null ) c  '
7619      ||' WHERE  c.setup_id = a.to_seq_dep_id  '
7620      ||'        AND c.RESOURCE_ID = c.RESOURCE_ID '
7621      ||'        AND c.organization_id = a.organization_id ' ;
7622 
7623      EXECUTE IMMEDIATE Fact_tran USING instance_id , instance_id ;
7624 
7625    -- Resource Setups (Alternate Resources are considered)
7626    stmt_no := 930 ;
7627    rsrc_setup := ' INSERT INTO msc_st_resource_setups ( '
7628     ||'   resource_id,      '
7629     ||'   organization_id,  '
7630     ||'   sr_instance_id,   '
7631     ||'   setup_id,         '
7632     ||'   setup_code,       '
7633     ||'   setup_description,'
7634     ||'   deleted_flag   ) '
7635     ||'SELECT unique  '
7636     ||'   mst.resource_id, '
7637     ||'   mst.organization_id, '
7638     ||'   mst.sr_instance_id, '
7639     ||'   gst.SEQ_DEP_ID , '
7640     ||'   mc.CONCATENATED_SEGMENTS, '
7641     ||'   mc.CONCATENATED_SEGMENTS, '
7642     ||'   2 '
7643     ||' FROM gmp_sequence_types'||at_apps_link||' gst, '
7644     ||'     MTL_CATEGORIES_B_KFV'||at_apps_link||' mc, '
7645     ||'    ( SELECT unique mt.organization_id, mt.resource_id, '
7646     ||'      mt.transition_uom,mt.sr_instance_id, '
7647     ||'      mt.deleted_flag , mt.from_setup_id, mt.to_setup_id '
7648     ||'      FROM mtl_parameters'||at_apps_link|| ' mp, '
7649     ||'       msc_st_setup_transitions mt  '
7650     ||'      WHERE mp.organization_id = mt.organization_id AND '
7651     ||'       mp.process_enabled_flag = '|| ''''||'Y'||'''' ||' )  mst '
7652     ||' WHERE gst.oprn_id <> -1 '
7653     ||'  AND mc.category_id = gst.category_id  '
7654     ||'  AND mst.sr_instance_id = :instance1 '
7655     ||'  AND (gst.seq_dep_id = mst.from_setup_id OR '
7656     ||'       gst.seq_dep_id = mst.to_setup_id )  ' ;
7657 	-- B5741664 Added a join with mtl_parameters to filter rows for process
7658 	-- organization - similar to 11.5.10 code
7659     IF l_in_str_org  IS NOT NULL THEN
7660       rsrc_setup := rsrc_setup
7661       ||'     AND mst.organization_id ' || l_in_str_org  ;
7662     END IF;
7663 
7664 LOG_MESSAGE(' Resource setups started ' );
7665 time_stamp;
7666 
7667 -- Bug: 8578876 Vpedarla
7668 --        EXECUTE IMMEDIATE rsrc_setup USING instance_id ;
7669 
7670    rsrc_setup := ' INSERT INTO msc_st_resource_setups ( '
7671     ||'   resource_id,      '
7672     ||'   organization_id,  '
7673     ||'   sr_instance_id,   '
7674     ||'   setup_id,         '
7675     ||'   setup_code,       '
7676     ||'   setup_description,'
7677     ||'   deleted_flag   ) '
7678     ||'   SELECT /*+DRIVING_SITE(mtl) DRIVING_SITE(gmp) DRIVING_SITE(gao) DRIVING_SITE(gmd) DRIVING_SITE(crd) */ unique  '
7679     ||'   ((crd.resource_id*2) + 1) resource_id , '
7680     ||'   crd.ORGANIZATION_ID , '
7681     ||'   :instance1 , '
7682     ||'   gmp.SEQ_DEP_ID , '
7683     ||'   mtl.CONCATENATED_SEGMENTS, '
7684     ||'   mtl.CONCATENATED_SEGMENTS, '
7685     ||'   2 '
7686     ||' FROM gmp_sequence_types'||at_apps_link||' gmp, '
7687     ||'     MTL_CATEGORIES_B_KFV'||at_apps_link||' mtl, '
7688     ||'     gmd_operation_activities'||at_apps_link||' gao, '
7689     ||'     gmd_operation_resources'||at_apps_link||' gmd, '
7690     ||'     cr_rsrc_dtl'||at_apps_link||' crd '
7691     ||'  WHERE gmp.oprn_id = gao.oprn_id  '
7692     ||'  AND gmp.oprn_id <> -1 '
7693     ||'  AND gao.OPRN_LINE_ID = gmd.OPRN_LINE_ID '
7694     ||'  AND gao.sequence_dependent_ind = 1 '
7695     ||'  AND gmd.prim_rsrc_ind = 1 '
7696     ||'  AND mtl.category_id = gmp.category_id  '
7697     ||'  AND crd.RESOURCES   = gmd.RESOURCES ' ;
7698 
7699     IF l_in_str_org  IS NOT NULL THEN
7700       rsrc_setup := rsrc_setup
7701       ||'  AND crd.ORGANIZATION_ID ' || l_in_str_org  ;
7702     END IF;
7703 
7704      EXECUTE IMMEDIATE rsrc_setup USING instance_id ;
7705 
7706      return_status := TRUE ;
7707 
7708 EXCEPTION
7709     WHEN NO_DATA_FOUND THEN
7710       NULL ;
7711       return_status := TRUE ;
7712     WHEN OTHERS THEN
7713       log_message('Write setups and Transitions Failed: '||sqlerrm||'-'||stmt_no);
7714       return_status := FALSE ;
7715 
7716 END write_setups_and_transitions ;
7717 
7718 /*
7719 REM+=========================================================================+
7720 REM| PROCEDURE NAME                                                          |
7721 REM|    write_step_dependency                                                |
7722 REM|                                                                         |
7723 REM| TYPE                                                                    |
7724 REM|    Private                                                              |
7725 REM|                                                                         |
7726 REM| USAGE                                                                   |
7727 REM|    This procedure inserts rows for step dependency                      |
7728 REM|                                                                         |
7729 REM| DESCRIPTION                                                             |
7730 REM|                                                                         |
7731 REM|                                                                         |
7732 REM| INPUT PARAMETERS                                                        |
7733 REM|    None                                                                 |
7734 REM|                                                                         |
7735 REM| OUTPUT PARAMETERS                                                       |
7736 REM|    None                                                                 |
7737 REM|                                                                         |
7738 REM| INPUT/OUTPUT PARAMETERS                                                 |
7739 REM|    None                                                                 |
7740 REM|                                                                         |
7741 REM| HISTORY                                                                 |
7742 REM|  06/16/2004   Namit Singhi created                                      |
7743 REM+=========================================================================+
7744 */
7745 
7746 PROCEDURE write_step_dependency (
7747   p_x_aps_fmeff_id   IN PLS_INTEGER
7748 )
7749 IS
7750 
7751   stpdep_start_index   INTEGER;
7752   stpdep_end_index     INTEGER;
7753   dep_index            NUMBER ;
7754 
7755 BEGIN
7756 
7757    dep_index := g_dep_index ;
7758 
7759 /* Get index for Routing Step Dependency */
7760   stpdep_start_index :=
7761       rtg_org_hdr_tab(effectivity.rtg_hdr_location).stpdep_start_loc ;
7762   stpdep_end_index :=
7763       rtg_org_hdr_tab(effectivity.rtg_hdr_location).stpdep_end_loc ;
7764 
7765   IF ((stpdep_start_index > 0) AND (stpdep_end_index > 0) AND
7766       (stpdep_end_index >= stpdep_start_index)) THEN
7767    FOR stpdp_cnt IN stpdep_start_index..stpdep_end_index
7768    LOOP
7769     opr_stpdep_frm_seq_id(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).x_dep_routingstep_id;
7770     opr_stpdep_to_seq_id(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).x_routingstep_id;
7771     opr_stpdep_routing_sequence_id(dep_index) := p_x_aps_fmeff_id ;
7772     opr_stpdep_dependency_type(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).dep_type;
7773     opr_stpdep_sr_instance_id(dep_index) := instance_id ;
7774     opr_stpdep_min_time_offset(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).standard_delay;
7775     opr_stpdep_max_time_offset(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).max_delay;
7776     opr_stpdep_trans_pct(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).transfer_pct;
7777     opr_stpdep_frm_op_seq_num(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).dep_routingstep_no;
7778     opr_stpdep_to_op_seq_num(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).routingstep_no;
7779     opr_stpdep_app_to_chrg(dep_index) := gmp_opr_stpdep_tbl(stpdp_cnt).chargeable_ind;
7780     opr_stpdep_organization_id(dep_index) := effectivity.organization_id;
7781     opr_stpdep_creation_date(dep_index) := current_date_time ;
7782     opr_stpdep_last_update_date(dep_index) := current_date_time ;
7783     dep_index := dep_index + 1;
7784    END LOOP;  /* Step Dependency loop */
7785   END IF;
7786     g_dep_index := dep_index ;
7787 
7788 END write_step_dependency ;
7789 
7790 /*
7791 REM+=========================================================================+
7792 REM| FUNCTION NAME                                                           |
7793 REM|    enh_bsearch_stpno                                                    |
7794 REM|                                                                         |
7795 REM| TYPE                                                                    |
7796 REM|    Private                                                              |
7797 REM|                                                                         |
7798 REM| USAGE                                                                   |
7799 REM|    This function returns the location in mat_assoc_tab                  |
7800 REM|    for given recipe, formula and formulaline_id                         |
7801 REM|                                                                         |
7802 REM| DESCRIPTION                                                             |
7803 REM|                                                                         |
7804 REM|                                                                         |
7805 REM| INPUT PARAMETERS                                                        |
7806 REM|    l_formula_id   IN NUMBER                                             |
7807 REM|    l_recipe_id IN NUMBER                                                |
7808 REM|    l_item_id IN NUMBER                                                  |
7809 REM|                                                                         |
7810 REM| OUTPUT PARAMETERS                                                       |
7811 REM|    INTEGER - Location in mat_assoc_tab                                  |
7812 REM|                                                                         |
7813 REM| INPUT/OUTPUT PARAMETERS                                                 |
7814 REM|    None                                                                 |
7815 REM|                                                                         |
7816 REM| HISTORY                                                                 |
7817 REM|  06/16/2004   Namit Singhi created                                      |
7818 REM+=========================================================================+
7819 */
7820 
7821 FUNCTION enh_bsearch_stpno ( l_formula_id   IN PLS_INTEGER,
7822                              l_recipe_id    IN PLS_INTEGER,
7823                              l_item_id      IN PLS_INTEGER)
7824 		RETURN INTEGER IS
7825 
7826 top     INTEGER ;
7827 bottom  INTEGER ;
7828 mid     INTEGER ;
7829 loop_direction NUMBER;
7830 ret_loc     INTEGER ;
7831 formula_start INTEGER ;
7832 
7833 BEGIN
7834      top    := 1;
7835      bottom := material_assocs_size ;
7836      mid    := -1 ;
7837      ret_loc   := -1 ;
7838      loop_direction := 0;
7839      formula_start := -1;
7840 
7841    WHILE  (top <= bottom )
7842     LOOP
7843      mid := top + ( ( bottom - top ) / 2 );
7844 --
7845      IF l_formula_id < mat_assoc_tab(mid).formula_id THEN
7846 	bottom := mid - 1 ;
7847      ELSIF l_formula_id > mat_assoc_tab(mid).formula_id THEN
7848 	top := mid + 1 ;
7849      ELSE
7850 	ret_loc := mid ;
7851         EXIT;
7852      END IF ;
7853     END LOOP;
7854 
7855     IF ret_loc > 0 THEN
7856         IF ret_loc = 1 THEN
7857             formula_start := 1 ;
7858         ELSE  /* ret_loc > 1*/
7859         LOOP
7860             ret_loc := ret_loc - 1;
7861             IF ret_loc = 1 THEN
7862                 formula_start := 1 ;
7863                 EXIT;
7864             ELSIF mat_assoc_tab(ret_loc).formula_id <> l_formula_id THEN
7865                 formula_start := ret_loc + 1;
7866                 EXIT;
7867             END IF;
7868         END LOOP;
7869         END IF;
7870     ELSE
7871         RETURN -1 ;
7872     END IF;
7873 
7874     ret_loc := formula_start;
7875 
7876     WHILE(ret_loc <= material_assocs_size) LOOP
7877     IF(mat_assoc_tab(ret_loc).formula_id > l_formula_id) THEN
7878         RETURN -1;
7879     ELSIF mat_assoc_tab(ret_loc).recipe_id = l_recipe_id AND
7880     mat_assoc_tab(ret_loc).inventory_item_id = l_item_id THEN
7881         RETURN ret_loc;
7882     ELSE
7883         ret_loc := ret_loc + 1;
7884     END IF;
7885     END LOOP;
7886 
7887     RETURN -1;
7888 
7889 EXCEPTION WHEN OTHERS THEN
7890    log_message(' Error in gmp_bom_routing_pkg.enh_bsearch_stpno: '||SQLERRM);
7891    RETURN -1;
7892 END enh_bsearch_stpno ;
7893 
7894 
7895 /*
7896 REM+=========================================================================+
7897 REM| PROCEDURE NAME                                                          |
7898 REM|    bsearch_unique                                                       |
7899 REM|                                                                         |
7900 REM| DESCRIPTION                                                             |
7901 REM|    This procedure finds the unique setup id for the combination passed  |
7902 REM|                                                                         |
7903 REM| HISTORY                                                                 |
7904 REM|  02/10/2006 B4918786 Rajesh Patangya Rewrite for SDS Enhancement        |
7905 REM|                                                                         |
7906 REM+=========================================================================+
7907 */
7908 PROCEDURE bsearch_unique (p_resource_id   IN PLS_INTEGER ,
7909                           p_category_id   IN PLS_INTEGER ,
7910                           p_setup_id      OUT NOCOPY PLS_INTEGER
7911                          ) IS
7912 i  INTEGER;
7913 BEGIN
7914   i := 1 ;
7915    FOR i IN 1..SD_INDEX LOOP
7916      IF  (sds_tab(i).resource_id = p_resource_id) AND
7917          (sds_tab(i).category_id = p_category_id) THEN
7918              p_setup_id := sds_tab(i).setup_id ;
7919              EXIT ;
7920      ELSE
7921              p_setup_id := NULL ;
7922      END IF ;
7923    END LOOP;
7924 EXCEPTION
7925    WHEN OTHERS THEN
7926    log_message('Error in bsearch_unique ' || sqlerrm);
7927    p_setup_id := NULL ;
7928 END bsearch_unique ;
7929 
7930 /* ========================================================================= */
7931 PROCEDURE extract_items
7932 (
7933   at_apps_link  IN VARCHAR2,
7934   instance      IN INTEGER,
7935   run_date      IN DATE,
7936   return_status IN OUT NOCOPY BOOLEAN
7937 )
7938 IS
7939   c_item_cursor           ref_cursor_typ;
7940 
7941   retrieval_cursor        VARCHAR2(4096);
7942   insert_statement        VARCHAR2(4096);
7943 
7944   TYPE gmp_item_aps_typ  IS RECORD (
7945     item_no               VARCHAR2(32),
7946     item_id               PLS_INTEGER,
7947     category_id           NUMBER,   /* SGIDUGU */
7948     seq_dep_id            NUMBER,   /* SGIDUGU */
7949     seq_dpnd_class        VARCHAR2(8),   /* SGIDUGU */
7950     item_um               VARCHAR2(4),
7951     uom_code              VARCHAR2(3),
7952     lot_control           PLS_INTEGER,
7953     item_desc1            VARCHAR2(70),
7954     aps_item_id           PLS_INTEGER,
7955     organization_id       PLS_INTEGER,
7956     whse_code             VARCHAR2(4),
7957     replen_ind            PLS_INTEGER,
7958     consum_ind            PLS_INTEGER,
7959     plant_code            VARCHAR2(4),
7960     creation_date         DATE,
7961     created_by            PLS_INTEGER,
7962     last_update_date      DATE,
7963     last_updated_by       PLS_INTEGER,
7964     last_update_login     PLS_INTEGER ) ;
7965 
7966   gmp_item_aps_rec        gmp_item_aps_typ;
7967 
7968   i                       NUMBER ;
7969 
7970 BEGIN
7971   i                       := 0;
7972 
7973   IF return_status THEN
7974     v_cp_enabled := TRUE;
7975   ELSE
7976     v_cp_enabled := FALSE;
7977   END IF;
7978 
7979    /* populate the org_string   */
7980 
7981      IF gmp_calendar_pkg.org_string(instance) THEN
7982         NULL ;
7983      ELSE
7984         RAISE invalid_string_value  ;
7985      END IF;
7986 
7987     l_in_str_org := gmp_calendar_pkg.g_in_str_org ; /* 3491625 */
7988 
7989   retrieval_cursor := 'DELETE FROM gmp_item_aps'||at_apps_link;
7990   EXECUTE IMMEDIATE retrieval_cursor;
7991 
7992   COMMIT;
7993 
7994 /*   New Changes - Using mtl_organization_id from ic_whse_mst , instead of
7995  *        organization_id from sy_orgn_mst , Bug# 1252322
7996  *        */
7997 
7998   /* SGIDUGU - code added for Seq Dep */
7999   retrieval_cursor :=
8000                 'SELECT iim.item_no, iim.item_id,nvl(iim.seq_category_id,-1), '
8001                 ||'  t.seq_dep_id, '
8002                 ||'  iim.seq_dpnd_class , '
8003                 ||'  iim.item_um, mum.uom_code,iim.lot_ctl, iim.item_desc1, '
8004                 ||'  msi.inventory_item_id, '
8005                 ||'  iwm.mtl_organization_id, '
8006                 ||'  pwe.whse_code, decode(sum(pwe.replen_ind), 0, 0, 1), '
8007                 ||'  decode(sum(pwe.consum_ind), 0, 0, 1), '
8008                 ||'  pwe.plant_code, iim.creation_date, iim.created_by, '
8009                 ||'  iim.last_update_date,iim.last_updated_by, NULL '
8010                 ||'  FROM  ic_item_mst'||at_apps_link||' iim,'
8011                 ||'        sy_uoms_mst'||at_apps_link||' sou,'   /* B1540127 */
8012                 ||'        ps_whse_eff'||at_apps_link||' pwe,'
8013                 ||'        ic_whse_mst'||at_apps_link||' iwm,'
8014                 ||'        mtl_system_items'||at_apps_link||' msi,'
8015                 ||'        mtl_units_of_measure'||at_apps_link||' mum, '
8016                 ||'        sy_orgn_mst'||at_apps_link||' som, '
8017                 ||'        (SELECT category_id,seq_dep_id  '      /* SGIDUGU */
8018                 ||'         FROM gmp_sequence_types'||at_apps_link   /* SGIDUGU
8019 */
8020                 ||'         WHERE oprn_id = -1 '       /* SGIDUGU */
8021                 ||'        ) t '
8022                 ||'  WHERE iim.delete_mark = 0 '
8023                 ||'    AND som.delete_mark = 0 '
8024                 ||'    AND iim.inactive_ind = 0 '
8025                 ||'    AND iim.item_no = msi.segment1 '
8026                 ||'    AND iwm.mtl_organization_id = msi.organization_id '
8027                 ||'    AND pwe.plant_code = som.orgn_code '
8028                 ||'    AND pwe.whse_code = iwm.whse_code '
8029                 ||'    AND sou.unit_of_measure = mum.unit_of_measure '
8030                 ||'    AND sou.delete_mark = 0 ' ;
8031               IF l_in_str_org  IS NOT NULL THEN
8032                 retrieval_cursor := retrieval_cursor
8033                 ||'    AND iwm.mtl_organization_id ' || l_in_str_org ;
8034              END IF;
8035                 retrieval_cursor := retrieval_cursor
8036                 ||'    AND iim.item_um = sou.um_code '
8037                 ||'    AND iim.experimental_ind = 0 '
8038                 ||'    AND iim.seq_category_id = t.category_id (+) ' /* SGIDUGU
8039 */
8040                 ||'    AND ( '
8041                 ||'          pwe.whse_item_id IS NULL OR '
8042                 ||'          pwe.whse_item_id = iim.whse_item_id OR '
8043                 ||'          ( '
8044                 ||'            pwe.whse_item_id = iim.item_id AND '
8045                 ||'            iim.item_id <> iim.whse_item_id '
8046                 ||'          ) '
8047                 ||'        ) '
8048                 ||' GROUP BY '
8049                 ||'   iim.item_id, iim.item_no,iim.seq_category_id,t.seq_dep_id, '
8050                 ||'   iim.seq_dpnd_class, '
8051                 ||'   iim.item_desc1, iim.item_um, '
8052                 ||'   iim.lot_ctl, pwe.whse_code, '
8053                 ||'   pwe.plant_code, mum.uom_code, msi.inventory_item_id, '
8054                 ||'   iwm.mtl_organization_id, '
8055                 ||'   iim.creation_date, iim.created_by, iim.last_update_date, '
8056                 ||'   iim.last_updated_by ' ;
8057 
8058   OPEN c_item_cursor FOR retrieval_cursor;
8059 
8060   /* SGIDUGU - added inserts for Category Id and Seq Dep Id */
8061   insert_statement :=
8062                 'INSERT INTO gmp_item_aps'||at_apps_link||' '
8063                 ||' ( '
8064                 ||'  item_no, item_id,category_id,seq_dep_id,seq_dpnd_class, '
8065                 ||'  item_um, uom_code, '
8066                 ||'  lot_control, item_desc1, '
8067                 ||'  aps_item_id, organization_id, whse_code, replen_ind,'
8068                 ||'  consum_ind,  plant_code, creation_date, created_by, '
8069                 ||'  last_update_date, last_updated_by, last_update_login '
8070                 ||' ) '
8071                 ||'  VALUES '
8072                 ||' (:p1,:p2,:p3,:p4,:p5,:p6, '
8073                 ||'  :p7,:p8,:p9,:p10,'
8074                 ||'  :p11,:p12,:p13,:p14, '
8075                 ||'  :p15,:p16,:p17,:p18,:p19,:p20)';
8076 
8077   FETCH c_item_cursor
8078   INTO  gmp_item_aps_rec;
8079 
8080   WHILE c_item_cursor%FOUND
8081   LOOP
8082     EXECUTE IMMEDIATE insert_statement USING
8083                  gmp_item_aps_rec.item_no,
8084                  gmp_item_aps_rec.item_id,
8085                  gmp_item_aps_rec.category_id,  /* SGIDUGU */
8086                  gmp_item_aps_rec.seq_dep_id,  /* SGIDUGU */
8087                  gmp_item_aps_rec.seq_dpnd_class,  /* SGIDUGU */
8088                  gmp_item_aps_rec.item_um,
8089                  gmp_item_aps_rec.uom_code,
8090                  gmp_item_aps_rec.lot_control,
8091                  gmp_item_aps_rec.item_desc1,
8092                  gmp_item_aps_rec.aps_item_id,
8093                  gmp_item_aps_rec.organization_id,
8094                  gmp_item_aps_rec.whse_code,
8095                  gmp_item_aps_rec.replen_ind,
8096                  gmp_item_aps_rec.consum_ind,
8097                  gmp_item_aps_rec.plant_code,
8098                  run_date,
8099                  gmp_item_aps_rec.created_by,
8100                  run_date,
8101                  gmp_item_aps_rec.last_updated_by,
8102                  0;
8103 
8104     i := i + 1;
8105 
8106     IF i = 500 then
8107       COMMIT;
8108       i := 0;
8109     END IF;
8110     FETCH c_item_cursor
8111     INTO gmp_item_aps_rec;
8112 
8113   END LOOP;
8114 
8115   COMMIT;
8116 
8117   CLOSE c_item_cursor;
8118 
8119   return_status := TRUE;
8120 
8121   EXCEPTION
8122     WHEN invalid_string_value  THEN
8123       log_message('Organization string is Invalid ' );
8124       return_status := FALSE;
8125     WHEN NO_DATA_FOUND THEN /* B3577871 */
8126       log_message(' NO_DATA_FOUND exception raised in Procedure: Gmp_bom_routing_pkg.Extract_Items ' );
8127       return_status := TRUE;
8128     WHEN OTHERS THEN
8129       log_message('Item extraction failed with error '||sqlerrm);
8130       return_status := FALSE;
8131 
8132 END Extract_Items;
8133 
8134 /*
8135 REM+=========================================================================+
8136 REM| FUNCTION NAME                                                           |
8137 REM|    get_profile_value                                                    |
8138 REM| DESCRIPTION                                                             |
8139 REM|    This procedure is created to get the profile value from the source or|
8140 REM|     destination server basing on the dblink                             |
8141 REM| HISTORY                                                                 |
8142 REM|    Vpedarla B6710684 created this procedure                             |
8143 REM+=========================================================================+
8144 */
8145 FUNCTION get_profile_value(
8146   profile_name            IN VARCHAR2,
8147   pdblink                 IN VARCHAR2) return VARCHAR2 IS
8148   uom_code_dblink         VARCHAR2(32767) ; /* bug: 6710684 Vpedarla */
8149   uom_code_ref            ref_cursor_typ ;
8150   l_gmp_um_code           VARCHAR2(32767);
8151 BEGIN
8152 LOG_MESSAGE(' GMP_BOM_ROUTING_PKG.get_profile_value called for profile '||profile_name||' with dblink '||pdblink);
8153 
8154 uom_code_dblink := 'select fnd_profile.VALUE'||pdblink||'('''||profile_name||''')'||' from dual ';
8155 
8156      OPEN uom_code_ref for uom_code_dblink ;
8157          FETCH uom_code_ref INTO l_gmp_um_code;
8158      CLOSE uom_code_ref;
8159      RETURN l_gmp_um_code ;
8160 END get_profile_value;
8161 
8162 /*
8163 REM+=========================================================================+
8164 REM| FUNCTION NAME                                                           |
8165 REM|    search_operation_leadtime                                           |
8166 REM| DESCRIPTION                                                             |
8167 REM|                                                                         |
8168 REM| HISTORY                                                                 |
8169 REM| 04/12/2008 Vpedarla  Created new procedure for bug 7391495              |
8170 REM+=========================================================================+
8171 */
8172 PROCEDURE search_operation_leadtime (p_fmeff_id IN PLS_INTEGER ,
8173 			  p_organization_id IN NUMBER,
8174               top OUT NOCOPY INTEGER,
8175               bottom OUT NOCOPY INTEGER) IS
8176 i INTEGER ;
8177 BEGIN
8178      top    := -1;
8179      bottom := -1 ;
8180 
8181    i := 1  ;
8182    FOR i IN 1..oper_leadtime_size
8183    LOOP
8184 
8185      IF (top = -1 AND oper_leadtime_percent(i).fmeff_id = p_fmeff_id
8186           AND  oper_leadtime_percent(i).organization_id = p_organization_id )   THEN
8187            top := i;
8188      END IF;
8189      IF (top <> -1 AND oper_leadtime_percent(i).fmeff_id = p_fmeff_id
8190           AND  oper_leadtime_percent(i).organization_id = p_organization_id )   THEN
8191             bottom := i;
8192      END IF;
8193      IF oper_leadtime_percent(i).fmeff_id > p_fmeff_id THEN
8194            EXIT ;
8195      END IF;
8196    END LOOP ;
8197    IF ( top = -1 ) THEN
8198     bottom := -1;
8199    END IF;
8200 
8201 END search_operation_leadtime ;
8202 
8203 /*
8204 REM+=========================================================================+
8205 REM| FUNCTION NAME                                                           |
8206 REM|    gmp_debug_message                                                    |
8207 REM| DESCRIPTION                                                             |
8208 REM|    This procedure is created to enable more debug messages              |
8209 REM| HISTORY                                                                 |
8210 REM|    Vpedarla Bug: 8420747 created this procedure                         |
8211 REM+=========================================================================+
8212 */
8213 PROCEDURE gmp_debug_message(pBUFF  IN  VARCHAR2) IS
8214 BEGIN
8215    IF (l_debug = 'Y') then
8216         LOG_MESSAGE(pBUFF);
8217    END IF;
8218 END gmp_debug_message;
8219 
8220 END GMP_BOM_ROUTING_PKG;