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