DBA Data[Home] [Help]

PACKAGE BODY: APPS.GMD_OUTBOUND_APIS_PUB

Source


1 PACKAGE BODY GMD_OUTBOUND_APIS_PUB AS
2 /*  $Header: GMDOAPIB.pls 120.14 2006/07/05 17:00:17 plowe noship $ */
3 --****************************************************************************************
4 --* FILE:      GMDOAPIB.pls                                                              *
5 --*                                                                                      *
6 --* CONTENTS:  Public level outbound GMD Quality API package                             *
7 --*                                                                                      *
8 --* AUTHOR:    Paul Schofield, OPM Development EMEA                                      *
9 --*                                                                                      *
10 --* DATE:      May 20th 2003                                                             *
11 --*                                                                                      *
12 --* VERSION    CHANGED BY         CHANGE                                                 *
13 --* =======    ==========         ======                                                 *
14 --* 20May03    P.J.Schofield      New file                                               *
15 --* 17Jun03    K.Y.Hunt           Merged in 3 new APIs                                   *
16 --* 03Jul03    P.J.Schofield      Added sampling groups API                              *
17 --* 04Jul03    P.J.Schofield      Added user_name parameters for logging                 *
18 --* 28Aug03    Chetan Nagar       Added mini-pack K specific features.                   *
19 --* 18Dec03    Brenda Stone       Bug 3124627 Fetch Sample Groups; Changed               *
20 --*                               EVENT_ID to SAMPLING_EVENT_ID in where clause          *
21 --* 18Dec03    Brenda Stone       Bug 3124643 Fetch Sample Groups; Changed               *
22 --*                               r.recipe_no to r.recipe_vers                         *
23 --* 26Dec03    Brenda Stone       Bug 3133163;  Default test method was not retrieved,   *
24 --*                               Added wildcard (+) since the default test methods's    *
25 --*                               user_id is not in FND_USERS table.                     *
26 --* 29Dec03    Brenda Stone       Bug 3124620; Fetch_Sample_Groups, Changed where clause *
27 --*                               to validate the orgn_code                              *                                        *
28 --* 29Dec03    Brenda Stone       Bug 3124643;  Fetch_Sample_Groups, Changed r.recipe_no*
29 --*                               to r.recipe_version                                   *
30 --* 31Dec03    Brenda Stone       Bug 3124653; Fetch_Sample_Groups. Added code to        *
31 --*                               retrieve Sample Groups by customer_ship_to_location &  *
32 --*                               customer_ship_to_location_id                           *
33 --* 15Jan04    Brenda Stone       Added mini-pack L specific features                    *
34 --* 24Feb04    Brenda Stone       Bug 3394055; Added L columns to results and            *
35 --*                               composite results                                      *
36 --* 05May04    SaiKiran           Bug 3704049; Added 'delayed_lot_entry' to the column   *
37 --*                               lists in 'FETCH_SPEC_VRS' procedure for WIP, Supplier  *
38 --*                               and Inventory VRs                                      *
39 --* 20Sep04    Brenda Stone       Bug 3704049; Added auto_sample_ind field for WIP,      *
40 --*                               Supplier and Inventory VRs                             *
41 --* 02May05  Saikiran Vankadari   Convergence changes done for fetch_spec_vrs() procedure.*
42 --*           Changed all references of OPM Inventory tables to Discrete inventory tables*
43 --* 20Jun05    S Feinstein        Convergence changes done for fetch samples and fetch   *
44 --*                               sample groups.
45 --* 26Aug05   Saikiran Vankadari  Convergence changes done for fetch test methods procedure*
46 --* 10Oct05   RLNAGARA            Bug # 4548546 - Added code to fetch records using revision*
47 --* 10Nov05   RLNAGARA            Bug # 4616835 - Changed all the references of TYPE     *
48 --*                               objects to SYSTEM schema.                              *
49 --* 12Jun06   PLOWE               Bug # 5346713                                          *
50 --* 22Jun06   PLOWE               Bug # 5284242 - add 'system.' for xdf tab_types        *
51 --* 26Jun06   PLOWE               Bug # 5335829 rework                                   *
52 --* 03Jul06   PLOWE               Bug # 5346713 rework                                   *
53 --* 03Jul06   PLOWE               Bug # 5346480 rework                                   *
54 --****************************************************************************************
55 --*                                                                                      *
56 --* COPYRIGHT (c) Oracle Corporation 2003                                                *
57 --*                                                                                      *
58 --****************************************************************************************
59 
60 
61 
62 
63 -- Small setup routine for the rest of the package called internally.
64 
65 FUNCTION initialized_ok
66 ( p_user_name     IN VARCHAR2)
67 RETURN BOOLEAN
68 IS
69   l_user_id NUMBER;
70 BEGIN
71 
72   SELECT user_id INTO l_user_id
73   FROM   fnd_user
74   WHERE  user_name = UPPER(p_user_name)
75   AND    SYSDATE BETWEEN start_date AND NVL(end_date,sysdate+1);
76 
77   FND_GLOBAL.apps_initialize(l_user_id, NULL, NULL, 0);
78 
79   gme_debug.log_initialize('GMD_OUTBOUND_API');
80 
81   RETURN TRUE;
82 
83 EXCEPTION
84   WHEN NO_DATA_FOUND
85   THEN
86     gmd_api_pub.log_message('GMD_INVALID_USER_NAME', 'p_user_name', p_user_name);
87     RETURN FALSE;
88   WHEN OTHERS
89   THEN
90     gmd_api_pub.log_message('GMD_API_ERROR','Initialization failed', substr(SQLERRM,1,100));
91     RETURN FALSE;
92 END initialized_ok;
93 
94 PROCEDURE fetch_test_methods
95 ( p_api_version            IN NUMBER
96 , p_init_msg_list          IN VARCHAR2 DEFAULT FND_API.G_FALSE
97 , p_user_name              IN VARCHAR2
98 , p_from_test_method_code  IN VARCHAR2 DEFAULT NULL
99 , p_to_test_method_code    IN VARCHAR2 DEFAULT NULL
100 , p_test_method_id         IN NUMBER   DEFAULT NULL
101 , p_test_kit_organization_id IN NUMBER DEFAULT NULL  --INVCONV
102 --, p_test_kit_item_no       IN VARCHAR2 DEFAULT NULL --INVCONV
103 --, p_test_kit_item_id       IN NUMBER   DEFAULT NULL --INVCONV
104 , p_test_kit_inv_item_id       IN NUMBER   DEFAULT NULL --INVCONV
105 , p_resource               IN VARCHAR2 DEFAULT NULL
106 , p_delete_mark            IN NUMBER   DEFAULT NULL
107 , p_from_last_update_date  IN DATE     DEFAULT NULL
108 , p_to_last_update_date    IN DATE     DEFAULT NULL
109 , x_test_methods_table     OUT NOCOPY system.gmd_test_methods_tab_type -- 5284242
110 , x_return_status          OUT NOCOPY VARCHAR2
111 , x_msg_count              OUT NOCOPY NUMBER
112 , x_msg_data               OUT NOCOPY VARCHAR2
113 )
114 IS
115   sql_statement            VARCHAR2(2000);
116   column_list              VARCHAR2(2000);
117   table_list               VARCHAR2(2000);
118   where_clause             VARCHAR2(2000);
119   into_clause              VARCHAR2(2000);
120   using_clause             VARCHAR2(2000);
121   execution_string         VARCHAR2(5000);
122   row_count                NUMBER;
123   i                        NUMBER;
124   l_api_name               VARCHAR2(100) := 'fetch_test_methods';
125 BEGIN
126 
127 
128   IF NOT FND_API.Compatible_API_CALL
129     (gmd_outbound_apis_pub.api_version, p_api_version, l_api_name,'GMD_OUTBOUND_APIS_PUB')
130   OR NOT initialized_ok(p_user_name)
131   THEN
132     Raise FND_API.G_EXC_UNEXPECTED_ERROR;
133   ELSE
134     gme_debug.put_line('Starting FETCH_TESTS');
135 
136     /*  Initialize message list if p_int_msg_list is set TRUE.  */
137 
138     IF FND_API.to_boolean(p_init_msg_list)
139     THEN
140       FND_MSG_PUB.Initialize;
141     END IF;
142 
143     -- Start construction the select.
144 
145     gme_debug.put_line('Constructing select statement');
146 
147     sql_statement := 'SELECT ';
148 
149     column_list := 'system.gmd_test_method_rec_type( gtm.test_method_id, gtm.test_method_code, ' -- 5284242
150                    ||'gtm.test_method_desc, gtm.test_qty, gtm.test_qty_uom, gtm.delete_mark, '
151                    ||'gtm.display_precision, gtm.test_duration, gtm.days, gtm.hours, '
152                    ||'gtm.minutes, gtm.seconds, gtm.test_replicate, gtm.resources, '
153                    ||'gtm.test_kit_organization_id, NULL, '  --INVCONV
154                    ||'gtm.test_kit_inv_item_id, NULL, gtm.text_code, gtm.attribute1, '
155                    ||'gtm.attribute2, gtm.attribute3, gtm.attribute4, gtm.attribute5, '
156                    ||'gtm.attribute6, gtm.attribute7, gtm.attribute8, gtm.attribute9, '
157                    ||'gtm.attribute10, gtm.attribute11, gtm.attribute12, gtm.attribute13, '
158                    ||'gtm.attribute14, gtm.attribute15, gtm.attribute16, gtm.attribute17, '
159                    ||'gtm.attribute18, gtm.attribute19, gtm.attribute20, gtm.attribute21, '
160                    ||'gtm.attribute22, gtm.attribute23, gtm.attribute24, gtm.attribute25, '
161                    ||'gtm.attribute26, gtm.attribute27, gtm.attribute28, gtm.attribute29, '
162                    ||'gtm.attribute30, gtm.attribute_category, gtm.creation_date, '
163                    ||'gtm.created_by, fu1.user_name, gtm.last_updated_by, fu2.user_name, '
164                    ||'gtm.last_update_date, gtm.last_update_login) ';
165 
166     table_list :='FROM gmd_test_methods gtm, fnd_user fu1, fnd_user fu2 ';
167 
168 
169     -- We now have the first three strings built. Here comes the good bit: building the where and using
170     -- clauses and inserting the bind variables and values.
171 
172 
173  -- Bug 3133163;  Default test method was not retrieved, Added wildcard (+)
174  --               since the default test methods's user_id is not in FND_USERS
175  --               table.
176    where_clause := 'WHERE fu1.user_id (+) = gtm.created_by '||
177              ' AND fu2.user_id (+) = gtm.last_updated_by  '||
178              'AND 1=:dummy ';
179     using_clause := 'USING 1 ';
180 
181     -- Work down the parameter list and append conditions, bind variables and bind values.
182 
183     IF p_from_test_method_code IS NOT NULL
184     THEN
185       gmd_outbound_apis_pub.g_from_test_method_code := p_from_test_method_code;
186       where_clause := where_clause||'AND gtm.test_method_code >= :from_test_method_code ';
187       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_test_method_code ';
188     END IF;
189 
190     IF p_to_test_method_code IS NOT NULL
191     THEN
192       gmd_outbound_apis_pub.g_to_test_method_code := p_to_test_method_code;
193       where_clause := where_clause||'AND gtm.test_method_code <= :to_test_method_code ';
194       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_test_method_code ';
195     END IF;
196 
197     IF p_test_method_id IS NOT NULL
198     THEN
199       gmd_outbound_apis_pub.g_test_method_id := p_test_method_id;
200       where_clause := where_clause||'AND gtm.test_method_id = :test_method_id ';
201       using_clause := using_clause||', gmd_outbound_apis_pub.g_test_method_id ';
202     END IF;
203 
204     IF p_test_kit_organization_id IS NOT NULL --INVCONV
205     THEN
206       gmd_outbound_apis_pub.g_test_kit_organization_id := p_test_kit_organization_id;
207       where_clause := where_clause||'AND gtm.test_kit_organization_id = :test_kit_organization_id ';
208       using_clause := using_clause||', gmd_outbound_apis_pub.g_test_kit_organization_id ';
209     END IF;
210 
211     IF p_test_kit_inv_item_id IS NOT NULL  --INVCONV
212     THEN
213       gmd_outbound_apis_pub.g_test_kit_inv_item_id := p_test_kit_inv_item_id;
214       where_clause := where_clause||'AND gtm.test_kit_item_id = :test_kit_inv_item_id ';
215       using_clause := using_clause||', gmd_outbound_apis_pub.g_test_kit_inv_item_id ';
216     END IF;
217 
218     IF p_resource IS NOT NULL
219     THEN
220       gmd_outbound_apis_pub.g_resource := p_resource;
221       where_clause := where_clause||'AND gtm.resources = :resources ';
222       using_clause := using_clause||', gmd_outbound_apis_pub.g_resource ';
223     END IF;
224 
225     IF p_delete_mark IS NOT NULL
226     THEN
227       gmd_outbound_apis_pub.g_delete_mark := p_delete_mark;
228       where_clause := where_clause||'AND gtm.delete_mark = :delete_mark ';
229       using_clause := using_clause||', gmd_outbound_apis_pub.g_delete_mark';
230     END IF;
231 
232     IF p_from_last_update_date IS NOT NULL
233     THEN
234       gmd_outbound_apis_pub.g_from_last_update_date := p_from_last_update_date;
235       where_clause := where_clause||'AND gtm.last_update_date >= :from_last_update_date ';
236       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_last_update_date ';
237     END IF;
238 
239     IF p_to_last_update_date IS NOT NULL
240     THEN
241       gmd_outbound_apis_pub.g_to_last_update_date := p_to_last_update_date;
242       where_clause := where_clause||'AND gtm.last_update_date <= :to_last_update_date';
243       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_last_update_date';
244     END IF;
245 
246     -- That's more or less the job done. We just need to tell the system where to store the data
247 
248     into_clause := ' BULK COLLECT INTO gmd_outbound_apis_pub.g_test_methods_table ';
249 
250     execution_string := 'BEGIN EXECUTE IMMEDIATE '
251                        ||''''
252                        ||sql_statement||column_list||table_list||where_clause
253                        ||''''
254                        ||into_clause
255                        ||using_clause
256                        ||'; END;';
257 
258     gme_debug.put_line('The sql statement is:');
259     i:= 1;
260     LOOP
261       gme_debug.put_line(substr(execution_string, i, 100));
262       EXIT WHEN i> LENGTH(execution_string);
263       i := i+100;
264     END LOOP;
265     gme_debug.put_line('Executing string');
266 
270 
267     EXECUTE IMMEDIATE execution_string;
268 
269     -- Main retrieval done, now just fill in any blanks
271     IF g_test_methods_table.count > 0
272     THEN
273       FOR i in 1.. g_test_methods_table.count
274       LOOP
275 
276         IF g_test_methods_table(i).test_kit_organization_id IS NOT NULL THEN   --INVCONV
277           gme_debug.put_line('Retrieving test kit organization for test kit organization id:'
278                           ||to_char(g_test_methods_table(i).test_kit_organization_id));
279           SELECT organization_code INTO g_test_methods_table(i).test_kit_organization_code
280           FROM mtl_parameters
281           WHERE organization_id = g_test_methods_table(i).test_kit_organization_id;
282         END IF;
283 
284         IF g_test_methods_table(i).test_kit_inv_item_id IS NOT NULL THEN  --INVCONV
285           gme_debug.put_line('Retrieving test kit item for test kit inv item id:'
286                           ||to_char(g_test_methods_table(i).test_kit_inv_item_id));
287           SELECT concatenated_segments INTO g_test_methods_table(i).test_kit_item_number
288           FROM mtl_system_items_b_kfv
289           WHERE organization_id = g_test_methods_table(i).test_kit_organization_id
290           AND inventory_item_id = g_test_methods_table(i).test_kit_inv_item_id;
291         END IF;
292 
293       END LOOP;
294     END IF;
295 
296     gme_debug.put_line('Returning table to caller');
297     x_test_methods_table := gmd_outbound_apis_pub.g_test_methods_table;
298     x_return_status := FND_API.G_RET_STS_SUCCESS;
299 
300   END IF;
301 
302   FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
303   gme_debug.put_line('Finished');
304 EXCEPTION
305   WHEN OTHERS
306   THEN
307     gme_debug.put_line('EXCEPTION : '||substr(SQLERRM,1,100));
308     gmd_api_pub.log_message('GMD_API_ERROR','Fetch_Test_Methods', 'Exception',substr(SQLERRM,1,100));
309     FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
310     x_return_status := FND_API.G_RET_STS_ERROR;
311 
312 END fetch_test_methods;
313 
314 
315 PROCEDURE fetch_tests
316 ( p_api_version            IN NUMBER
317 , p_init_msg_list          IN VARCHAR2 DEFAULT FND_API.G_FALSE
318 , p_user_name              IN VARCHAR2
319 , p_from_test_code         IN VARCHAR2 DEFAULT NULL
320 , p_to_test_code           IN VARCHAR2 DEFAULT NULL
321 , p_from_test_method_code  IN VARCHAR2 DEFAULT NULL
322 , p_to_test_method_code    IN VARCHAR2 DEFAULT NULL
323 , p_test_id                IN NUMBER   DEFAULT NULL
324 , p_test_method_id         IN NUMBER   DEFAULT NULL
325 , p_test_class             IN VARCHAR2 DEFAULT NULL
326 , p_test_type              IN VARCHAR2 DEFAULT NULL
327 , p_priority               IN VARCHAR2 DEFAULT NULL
328 , p_delete_mark            IN NUMBER   DEFAULT NULL
329 , p_from_last_update_date  IN DATE     DEFAULT NULL
330 , p_to_last_update_date    IN DATE     DEFAULT NULL
331 , x_tests_table            OUT NOCOPY system.gmd_qc_tests_tab_type
332 , x_return_status          OUT NOCOPY VARCHAR2
333 , x_msg_count              OUT NOCOPY NUMBER
334 , x_msg_data               OUT NOCOPY VARCHAR2
335 )
336 IS
337   CURSOR test_values_cursor (p_test_id NUMBER) IS
338   SELECT system.gmd_qc_test_value_rec_type
339   ( gtv.test_value_id, gtv.min_num, gtv.max_num, gtv.display_label_numeric_range, gtv.test_value_desc
340   , gtv.value_char, gtv.text_range_seq, gtv.expression_ref_test_id, gtv.text_code, gtv.attribute_category
341   , gtv.attribute1, gtv.attribute2, gtv.attribute3, gtv.attribute4, gtv.attribute5, gtv.attribute6, gtv.attribute7
342   , gtv.attribute8, gtv.attribute9, gtv.attribute10, gtv.attribute11, gtv.attribute12, gtv.attribute13, gtv.attribute14
343   , gtv.attribute15, gtv.attribute16, gtv.attribute17, gtv.attribute18, gtv.attribute19, gtv.attribute20
344   , gtv.attribute21, gtv.attribute22, gtv.attribute23, gtv.attribute24, gtv.attribute25, gtv.attribute26
345   , gtv.attribute27, gtv.attribute28, gtv.attribute29, gtv.attribute30, gtv.creation_date, gtv.created_by
346   , fu1.user_name, gtv.last_update_date, gtv.last_updated_by, fu2.user_name
347   , gtv.last_update_login
348   )
349   FROM    gmd_qc_test_values gtv, fnd_user fu1, fnd_user fu2
350   WHERE   gtv.test_id = p_test_id
351   AND     fu1.user_id = gtv.created_by
352   AND     fu2.user_id = gtv.last_updated_by;
353 
354   CURSOR customers_cursor (p_test_id NUMBER) IS
355   SELECT system.gmd_customer_test_rec_type
356   ( gct.cust_id, ocm.cust_no, gct.report_precision, gct.cust_test_display, gct.text_code
357   , gct.creation_date, gct.created_by, fu1.user_name, gct.last_update_date
358   , gct.last_updated_by, fu2.user_name, gct.last_update_login
359   )
360   FROM   gmd_customer_tests gct, op_cust_mst ocm, fnd_user fu1, fnd_user fu2
361   WHERE  gct.test_id = p_test_id
362   AND    gct.cust_id = ocm.cust_id
363   AND    gct.created_by = fu1.user_id
364   AND    gct.last_updated_by = fu2.user_id;
365 
366   sql_statement            VARCHAR2(4000);
367   column_list              VARCHAR2(4000);
368   table_list               VARCHAR2(4000);
369   where_clause             VARCHAR2(4000);
370   into_clause              VARCHAR2(4000);
371   using_clause             VARCHAR2(4000);
372   execution_string         VARCHAR2(9000);
373   row_count                NUMBER;
374   i                        NUMBER;
375   l_api_name               VARCHAR2(100) := 'fetch_tests';
376 BEGIN
377 
378   IF NOT FND_API.Compatible_API_CALL
379     (gmd_outbound_apis_pub.api_version, p_api_version, l_api_name,'GMD_OUTBOUND_APIS_PUB')
380   OR NOT initialized_ok(p_user_name)
381   THEN
382     Raise FND_API.G_EXC_UNEXPECTED_ERROR;
383   ELSE
384     /*  Initialize message list if p_int_msg_list is set TRUE.  */
385     IF FND_API.to_boolean(p_init_msg_list)
386     THEN
387       FND_MSG_PUB.Initialize;
388     END IF;
389 
393                  ||'  gqt.test_id, gqt.test_code, gqt.test_desc, gqt.test_method_id, gtm.test_method_code'
390     sql_statement := 'SELECT ';
391 
392     column_list := 'system.gmd_qc_test_rec_type('
394                  ||', gqt.test_oprn_line_id, gqt.test_provider_code, gqt.test_class, gqt.test_type'
395                  ||', gqt.test_unit, gqt.min_value_num, gqt.max_value_num, gqt.exp_error_type, gqt.below_spec_min'
396                  ||', gqt.above_spec_min, gqt.below_spec_max, gqt.above_spec_max, gqt.below_min_action_code'
397                  ||', gqt.above_min_action_code, gqt.below_max_action_code, gqt.above_max_action_code'
398                  ||', gqt.expression, gqt.display_precision, gqt.report_precision, gqt.priority, gqt.test_oprn_id'
399                  ||', gqt.delete_mark, gqt.text_code, gqt.attribute_category, gqt.attribute1, gqt.attribute2'
400                  ||', gqt.attribute3, gqt.attribute4, gqt.attribute5, gqt.attribute6, gqt.attribute7, gqt.attribute8'
401                  ||', gqt.attribute9, gqt.attribute10, gqt.attribute11, gqt.attribute12, gqt.attribute13, gqt.attribute14'
402                  ||', gqt.attribute15, gqt.attribute16, gqt.attribute17, gqt.attribute18, gqt.attribute19, gqt.attribute20'
403                  ||', gqt.attribute21, gqt.attribute22, gqt.attribute23, gqt.attribute24, gqt.attribute25, gqt.attribute26'
404                  ||', gqt.attribute27, gqt.attribute28, gqt.attribute29, gqt.attribute30, gqt.creation_date'
405                  ||', gqt.created_by, fu1.user_name, gqt.last_update_date, gqt.last_updated_by'
406                  ||', fu2.user_name, gqt.last_update_login'
407                  ||', system.gmd_qc_test_values_tab_type (NULL), system.gmd_customer_tests_tab_type(NULL)' -- 5284242
408                  ||')';
409 
410     table_list := ' FROM gmd_qc_tests gqt, gmd_test_methods gtm, fnd_user fu1, fnd_user fu2';
411 
412     where_clause := ' WHERE gqt.test_method_id = gtm.test_method_id'
413                   ||' AND fu1.user_id=gqt.created_by'
414                   ||' AND fu2.user_id=gqt.last_updated_by'
415                   ||' AND 1=:dummy ';
416 
417     using_clause := ' USING 1';
418 
419     IF p_from_test_method_code IS NOT NULL
420     THEN
421       gmd_outbound_apis_pub.g_from_test_method_code := p_from_test_method_code;
422       where_clause := where_clause ||' AND gtm.test_method_code >= :from_test_method_code';
423       using_clause := using_clause ||', gmd_outbound_apis_pub.g_from_test_method_code';
424     END IF;
425 
426     IF p_to_test_method_code IS NOT NULL
427     THEN
428       gmd_outbound_apis_pub.g_to_test_method_code := p_to_test_method_code;
429       where_clause := where_clause ||' AND gtm.test_method_code <= :to_test_method_code';
430       using_clause := using_clause ||', gmd_outbound_apis_pub.g_to_test_method_code';
431     END IF;
432 
433     IF p_from_test_code IS NOT NULL
434     THEN
435       gmd_outbound_apis_pub.g_from_test_code := p_from_test_code;
436       where_clause := where_clause||' AND gqt.test_code >= :from_test_code';
437       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_test_code';
438     END IF;
439 
440     IF p_to_test_code IS NOT NULL
441     THEN
442       gmd_outbound_apis_pub.g_to_test_code := p_to_test_code;
443       where_clause := where_clause||' AND gqt.test_code <= :to_test_code';
444       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_test_code';
445     END IF;
446 
447 
448     IF p_test_method_id IS NOT NULL
449     THEN
450       gmd_outbound_apis_pub.g_test_method_id := p_test_method_id;
451       where_clause := where_clause||' AND gqt.test_method_id = :test_method_id';
452       using_clause := using_clause||', gmd_outbound_apis_pub.g_test_method_id';
453     END IF;
454 
455 
456     IF p_test_id IS NOT NULL
457     THEN
458       gmd_outbound_apis_pub.g_test_id := p_test_id;
459       where_clause := where_clause||' AND gqt.test_id = :test_id';
460       using_clause := using_clause||', gmd_outbound_apis_pub.g_test_id';
461     END IF;
462 
463 
464     IF p_test_class IS NOT NULL
465     THEN
466       gmd_outbound_apis_pub.g_test_class := p_test_class;
467       where_clause := where_clause||' AND gqt.test_class = :test_class';
468       using_clause := using_clause||', gmd_outbound_apis_pub.g_test_class';
469     END IF;
470 
471 
472     IF p_test_type IS NOT NULL
473     THEN
474       gmd_outbound_apis_pub.g_test_type := p_test_type;
475       where_clause := where_clause||' AND gqt.test_type = :test_type';
476       using_clause := using_clause||', gmd_outbound_apis_pub.g_test_type';
477     END IF;
478 
479 
480     IF p_priority IS NOT NULL
481     THEN
482       gmd_outbound_apis_pub.g_priority := p_priority;
483       where_clause := where_clause||' AND gqt.test_type = :priority';
484       using_clause := using_clause||', gmd_outbound_apis_pub.g_priority';
485     END IF;
486 
487 
488     IF p_delete_mark IS NOT NULL
489     THEN
490       gmd_outbound_apis_pub.g_delete_mark := p_delete_mark;
491       where_clause := where_clause||' AND gqt.delete_mark = :delete_mark';
492       using_clause := using_clause||', gmd_outbound_apis_pub.g_delete_mark';
493     END IF;
494 
495 
496     IF p_from_last_update_date IS NOT NULL
497     THEN
498       gmd_outbound_apis_pub.g_from_last_update_date := p_from_last_update_date;
499       where_clause := where_clause||' AND gqt.last_update_date >= :from_last_update_date';
500       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_last_update_date';
501     END IF;
502 
503     IF p_to_last_update_date IS NOT NULL
504     THEN
505       gmd_outbound_apis_pub.g_to_last_update_date := p_to_last_update_date;
506       where_clause := where_clause||' AND gqt.last_update_date <= :to_last_update_date';
507       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_last_update_date';
508     END IF;
509 
510  -- That's more or less the job done. We just need to tell the system where to store the data
511 
512     into_clause := ' BULK COLLECT INTO gmd_outbound_apis_pub.g_tests_table ';
513 
514     execution_string := 'BEGIN EXECUTE IMMEDIATE '
515                        ||''''
516                        ||sql_statement||column_list||table_list||where_clause
517                        ||''''
518                        ||into_clause
519                        ||using_clause
520                        ||'; END;';
521 
522     EXECUTE IMMEDIATE execution_string;
526       FOR i IN 1..g_tests_table.count
523 
524     IF g_tests_table.count <> 0
525     THEN
527       LOOP
528         OPEN test_values_cursor(g_tests_table(i).test_id);
529         FETCH test_values_cursor BULK COLLECT INTO g_tests_table(i).test_values;
530         CLOSE test_values_cursor;
531 
532         OPEN customers_cursor(g_tests_table(i).test_id);
533         FETCH customers_cursor BULK COLLECT INTO g_tests_table(i).customer_tests;
534         CLOSE customers_cursor;
535       END LOOP;
536     END IF;
537 
538     x_tests_table := gmd_outbound_apis_pub.g_tests_table;
539     x_return_status := FND_API.G_RET_STS_SUCCESS;
540   END IF;
541 
542   FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
543 
544 EXCEPTION
545   WHEN OTHERS
546   THEN
547     FND_MESSAGE.SET_NAME('GMD',SQLCODE);
548     FND_MSG_PUB.Add;
549     FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
550     x_return_status := FND_API.G_RET_STS_ERROR;
551 
552 END fetch_tests;
553 
554 
555 PROCEDURE FETCH_SPEC_VRS
556 ( p_api_version            IN NUMBER
557 , p_init_msg_list          IN VARCHAR2 DEFAULT FND_API.G_FALSE
558 , p_user_name              IN VARCHAR2
559 
560 -- Parameters relating to specifications
561 
562 , p_from_spec_name	   IN VARCHAR2 DEFAULT NULL
563 , p_to_spec_name    	   IN VARCHAR2 DEFAULT NULL
564 , p_spec_id		   IN NUMBER   DEFAULT NULL
565 , p_spec_version    	   IN NUMBER   DEFAULT NULL
566 , p_from_grade_code        IN VARCHAR2 DEFAULT NULL
567 , p_to_grade_code          IN VARCHAR2 DEFAULT NULL
568 , p_from_item_number       IN VARCHAR2 DEFAULT NULL
569 , p_to_item_number  	   IN VARCHAR2 DEFAULT NULL
570 , p_inventory_item_id      IN NUMBER   DEFAULT NULL
571 , p_from_revision          IN VARCHAR2 DEFAULT NULL  -- RLNAGARA Bug # 4548546
572 , p_to_revision            IN VARCHAR2 DEFAULT NULL  -- RLNAGARA Bug # 4548546
573 , p_from_spec_last_update  IN DATE     DEFAULT NULL
574 , p_to_spec_last_update    IN DATE     DEFAULT NULL
575 , p_spec_status            IN NUMBER   DEFAULT NULL
576 , p_owner_organization_code IN VARCHAR2 DEFAULT NULL
577 , p_spec_delete_mark       IN NUMBER   DEFAULT NULL
578 
579 -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
580 , p_overlay_ind            IN VARCHAR2 DEFAULT NULL
581 , p_spec_type              IN VARCHAR2 DEFAULT NULL
582 , p_base_spec_id           IN NUMBER   DEFAULT NULL
583 , p_base_spec_name         IN VARCHAR2 DEFAULT NULL
584 , p_base_spec_version      IN NUMBER   DEFAULT NULL
585 -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
586 
587 -- Parameters relating to spec tests
588 
589 , p_test_code		   IN VARCHAR2 DEFAULT NULL
590 , p_test_id  		   IN NUMBER   DEFAULT NULL
591 , p_test_method_code	   IN VARCHAR2 DEFAULT NULL
592 , p_test_method_id	   IN NUMBER   DEFAULT NULL
593 , p_test_qty_uom		   IN VARCHAR2 DEFAULT NULL
594 , p_test_priority	   IN VARCHAR2 DEFAULT NULL
595 , p_from_test_last_update  IN DATE     DEFAULT NULL
596 , p_to_test_last_update	   IN DATE     DEFAULT NULL
597 , p_test_delete_mark       IN NUMBER   DEFAULT NULL
598 -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
599 , p_from_base_ind          IN VARCHAR2 DEFAULT NULL
600 , p_exclude_ind            IN VARCHAR2 DEFAULT NULL
601 , p_modified_ind           IN VARCHAR2 DEFAULT NULL
602 , p_calc_uom_conv_ind      IN VARCHAR2 DEFAULT NULL
603 , p_to_qty_uom             IN VARCHAR2 DEFAULT NULL
604 -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
605 
606 -- Parameters relating to wip spec validity rules
607 
608 , p_wip_vr_status	   IN NUMBER   DEFAULT NULL
609 , p_wip_vr_organization_code  IN VARCHAR2 DEFAULT NULL
610 , p_wip_vr_batch_orgn_code IN VARCHAR2 DEFAULT NULL
611 , p_wip_vr_batch_no        IN VARCHAR2 DEFAULT NULL
612 , p_wip_vr_batch_id        IN NUMBER   DEFAULT NULL
613 , p_wip_vr_recipe_no       IN VARCHAR2 DEFAULT NULL
614 , p_wip_vr_recipe_version  IN NUMBER   DEFAULT NULL
615 , p_wip_vr_recipe_id       IN NUMBER   DEFAULT NULL
616 , p_wip_vr_formula_no      IN VARCHAR2 DEFAULT NULL
617 , p_wip_vr_formula_version IN NUMBER   DEFAULT NULL
618 , p_wip_vr_formula_id      IN NUMBER   DEFAULT NULL
619 , p_wip_vr_formulaline_no  IN NUMBER   DEFAULT NULL
620 , p_wip_vr_formulaline_id  IN NUMBER   DEFAULT NULL
621 , p_wip_vr_line_type       IN NUMBER   DEFAULT NULL
622 , p_wip_vr_routing_no      IN VARCHAR2 DEFAULT NULL
623 , p_wip_vr_routing_version IN NUMBER   DEFAULT NULL
624 , p_wip_vr_routing_id      IN NUMBER   DEFAULT NULL
625 , p_wip_vr_step_no         IN NUMBER   DEFAULT NULL
626 , p_wip_vr_step_id         IN NUMBER   DEFAULT NULL
627 , p_wip_vr_operation_no    IN VARCHAR2 DEFAULT NULL
628 , p_wip_vr_operation_version IN NUMBER   DEFAULT NULL
629 , p_wip_vr_operation_id    IN NUMBER   DEFAULT NULL
630 , p_wip_vr_start_date	   IN DATE     DEFAULT NULL
631 , p_wip_vr_end_date	   IN DATE     DEFAULT NULL
632 , p_wip_vr_coa_type	   IN VARCHAR2 DEFAULT NULL
633 , p_wip_vr_sampling_plan   IN VARCHAR2 DEFAULT NULL
634 , p_wip_vr_sampling_plan_id IN NUMBER   DEFAULT NULL
635 , p_wip_vr_delete_mark	   IN NUMBER   DEFAULT NULL
636 , p_wip_vr_from_last_update IN DATE     DEFAULT NULL
637 , p_wip_vr_to_last_update	 IN DATE     DEFAULT NULL
638 
639 -- Parameters relating to customer spec validity rules
640 , p_cust_vr_start_date     IN DATE     DEFAULT NULL
641 , p_cust_vr_end_date       IN DATE     DEFAULT NULL
642 , p_cust_vr_status         IN NUMBER   DEFAULT NULL
643 , p_cust_vr_organization_code IN VARCHAR2 DEFAULT NULL
644 , p_cust_vr_org_id         IN NUMBER   DEFAULT NULL
645 , p_cust_vr_coa_type       IN VARCHAR2 DEFAULT NULL
646 , p_cust_vr_customer       IN VARCHAR2 DEFAULT NULL
647 , p_cust_vr_customer_id	   IN NUMBER   DEFAULT NULL
648 , p_cust_vr_order_number   IN NUMBER   DEFAULT NULL
652 , p_cust_vr_order_line_id  IN NUMBER   DEFAULT NULL
649 , p_cust_vr_order_id       IN NUMBER   DEFAULT NULL
650 , p_cust_vr_order_type     IN NUMBER   DEFAULT NULL
651 , p_cust_vr_order_line_no  IN NUMBER   DEFAULT NULL
653 , p_cust_vr_ship_to_location IN VARCHAR2 DEFAULT NULL
654 , p_cust_vr_ship_to_site_id  IN NUMBER   DEFAULT NULL
655 , p_cust_vr_operating_unit IN VARCHAR
656 , p_cust_vr_delete_mark    IN NUMBER   DEFAULT NULL
657 , p_cust_vr_from_last_update IN DATE     DEFAULT NULL
658 , p_cust_vr_to_last_update IN DATE     DEFAULT NULL
659 
660 -- Parameters relating to supplier spec validity rules
661 , p_supl_vr_start_date     IN DATE     DEFAULT NULL
662 , p_supl_vr_end_date       IN DATE     DEFAULT NULL
663 , p_supl_vr_status         IN NUMBER   DEFAULT NULL
664 , p_supl_vr_organization_code IN VARCHAR2 DEFAULT NULL
665 , p_supl_vr_org_id         IN NUMBER   DEFAULT NULL
666 , p_supl_vr_coa_type       IN VARCHAR2 DEFAULT NULL
667 , p_supl_vr_supplier       IN VARCHAR2 DEFAULT NULL
668 , p_supl_vr_supplier_id    IN NUMBER   DEFAULT NULL
669 , p_supl_vr_po_number      IN NUMBER   DEFAULT NULL
670 , p_supl_vr_po_id          IN NUMBER   DEFAULT NULL
671 , p_supl_vr_po_line_no     IN NUMBER   DEFAULT NULL
672 , p_supl_vr_po_line_id     IN NUMBER   DEFAULT NULL
673 , p_supl_vr_supplier_site  IN VARCHAR2 DEFAULT NULL
674 , p_supl_vr_supplier_site_id IN NUMBER   DEFAULT NULL
675 , p_supl_vr_operating_unit IN VARCHAR2 DEFAULT NULL
676 , p_supl_vr_delete_mark         IN NUMBER   DEFAULT NULL
677 , p_supl_vr_from_last_update    IN DATE     DEFAULT NULL
678 , p_supl_vr_to_last_update IN DATE     DEFAULT NULL
679 
680 -- Parameters relating to inventory spec validity rules
681 , p_inv_vr_start_date     IN DATE     DEFAULT NULL
682 , p_inv_vr_end_date       IN DATE     DEFAULT NULL
683 , p_inv_vr_status         IN NUMBER   DEFAULT NULL
684 , p_inv_vr_organization_code IN VARCHAR2 DEFAULT NULL
685 , p_inv_vr_coa_type       IN VARCHAR2 DEFAULT NULL
686 , p_inv_vr_item_number    IN VARCHAR2 DEFAULT NULL
687 , p_inv_vr_inventory_item_id  IN NUMBER   DEFAULT NULL
688 , p_inv_vr_parent_lot_number  IN VARCHAR2 DEFAULT NULL
689 , p_inv_vr_lot_number      IN VARCHAR2 DEFAULT NULL
690 , p_inv_vr_subinventory      IN VARCHAR2 DEFAULT NULL
691 , p_inv_vr_locator    IN VARCHAR2   DEFAULT NULL
692 , p_inv_vr_locator_id    IN NUMBER   DEFAULT NULL
693 , p_inv_vr_sampling_plan  IN VARCHAR2 DEFAULT NULL
694 , p_inv_vr_sampling_plan_id IN NUMBER   DEFAULT NULL
695 , p_inv_vr_delete_mark         IN NUMBER   DEFAULT NULL
696 , p_inv_vr_from_last_update    IN DATE     DEFAULT NULL
697 , p_inv_vr_to_last_update IN DATE     DEFAULT NULL
698 
699 -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
700 -- Parameters relating to monitor spec
701 , p_mon_vr_status                IN NUMBER   DEFAULT NULL
702 , p_mon_vr_rule_type             IN VARCHAR2 DEFAULT NULL
703 , p_mon_vr_lct_organization_code IN VARCHAR2 DEFAULT NULL
704 , p_mon_vr_subinventory          IN VARCHAR2 DEFAULT NULL
705 , p_mon_vr_locator_id            IN NUMBER DEFAULT NULL
706 , p_mon_vr_locator               IN VARCHAR2 DEFAULT NULL
707 , p_mon_vr_rsr_organization_code IN VARCHAR2 DEFAULT NULL
708 , p_mon_vr_resources             IN VARCHAR2 DEFAULT NULL
709 , p_mon_vr_resource_instance_id  IN NUMBER   DEFAULT NULL
710 , p_mon_vr_sampling_plan         IN VARCHAR2 DEFAULT NULL
711 , p_mon_vr_sampling_plan_id      IN NUMBER   DEFAULT NULL
712 , p_mon_vr_start_date            IN DATE     DEFAULT NULL
713 , p_mon_vr_end_date              IN DATE     DEFAULT NULL
714 , p_mon_vr_from_last_update_date IN DATE     DEFAULT NULL
715 , p_mon_vr_to_last_update_date   IN DATE     DEFAULT NULL
716 , p_mon_vr_delete_mark           IN NUMBER   DEFAULT NULL
717 -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
718 
719 -- Return parameters
720 
721 , x_specifications_tbl     OUT NOCOPY system.gmd_specifications_tab_type
722 , x_return_status     	   OUT NOCOPY VARCHAR2
723 , x_msg_count          	   OUT NOCOPY NUMBER
724 , x_msg_data               OUT NOCOPY VARCHAR2
725 )
726 IS
727   sql_statement            VARCHAR2(32000);
728 
729   main_column_list         VARCHAR2(10000);
730   spec_test_column_list    VARCHAR2(10000);
731   cust_vr_column_list      VARCHAR2(10000);
732   inv_vr_column_list       VARCHAR2(10000);
733   supl_vr_column_list      VARCHAR2(10000);
734   wip_vr_column_list       VARCHAR2(10000);
735   mon_vr_column_list       VARCHAR2(10000);
736 
737   main_table_list          VARCHAR2(10000);
738   spec_test_table_list     VARCHAR2(10000);
739   cust_vr_table_list       VARCHAR2(10000);
740   inv_vr_table_list        VARCHAR2(10000);
741   supl_vr_table_list       VARCHAR2(10000);
742   wip_vr_table_list        VARCHAR2(10000);
743   mon_vr_table_list        VARCHAR2(10000);
744 
745   main_where_clause        VARCHAR2(20000);
746   spec_test_where_clause   VARCHAR2(10000);
747   cust_vr_where_clause     VARCHAR2(10000);
748   inv_vr_where_clause      VARCHAR2(10000);
749   supl_vr_where_clause     VARCHAR2(10000);
750   wip_vr_where_clause      VARCHAR2(10000);
751   mon_vr_where_clause      VARCHAR2(10000);
752 
753   main_using_clause        VARCHAR2(10000);
754   spec_test_using_clause   VARCHAR2(10000);
755   wip_vr_using_clause      VARCHAR2(10000);
756   inv_vr_using_clause      VARCHAR2(10000);
757   supl_vr_using_clause     VARCHAR2(10000);
758   cust_vr_using_clause     VARCHAR2(10000);
759   mon_vr_using_clause     VARCHAR2(10000);
760 
761   main_into_clause         VARCHAR2(10000);
762   main_execution_string    VARCHAR2(30000);
763 
764   row_count                NUMBER;
765   i                        NUMBER;
766   j                        NUMBER;
767   l_api_name               VARCHAR2(100) := 'fetch_spec_vrs';
768 
769 BEGIN
770 
771 
772   IF NOT FND_API.Compatible_API_CALL
776 
773     (gmd_outbound_apis_pub.api_version, p_api_version, l_api_name,'GMD_OUTBOUND_APIS_PUB')
774   OR NOT initialized_ok(p_user_name)
775   THEN
777     Raise FND_API.G_EXC_UNEXPECTED_ERROR;
778   ELSE
779 
780     /*  Initialize message list if p_int_msg_list is set TRUE.  */
781     IF FND_API.to_boolean(p_init_msg_list)
782     THEN
783       FND_MSG_PUB.Initialize;
784     END IF;
785 
786     -- Build the query to retrieve all base rows from gmd_specifications together with
787     -- linked nested rowsets from all of the other tables..
788     --
789     -- The code looks worse than it is. All the next few sections do is build SQL
790     -- clauses that are then glued together and executed dynamically with binds,
791     -- depending on what has been specified in the parameters.
792     --
793     -- By doing one large BULK COLLECT using the constructed statement, almost everything
794     -- is retrieved in a single hit, including all of the nested data. All that we need to
795     -- do afterwards is fill in the gaps where a direct retrieval is not possible due to
796     -- nullable columns where we cannot make outer joins.
797     --
798     -- The end result will resemble:
799 
800     /* SELECT gmd_specifications_rec_type
801               ( <gmd_specification table columns>
802               , CAST
803                 ( MULTISET
804                   ( gmd_spec_test_rec_type
805                     ( SELECT <gmd_spec_tests table columns>
806                       FROM   <gmd_spec_tests table set>
807                       WHERE  <gmd_spec_tests conditions with bind variables> ***
808                   AS system.gmd_spec_tests_tab_type
809                 )
810               , CAST
811                 ( MULTISET
812                   ( gmd_cust_spec_vrs_rec_type
813                     ( SELECT <gmd_cust_spec_vrs table columns>
814                       FROM   <gmd_cust_spec_crs table set>
815                       WHERE  <gmd_cust_spec_vrs conditions with bind variables> ***
816                   AS system.gmd_cust_spec_vrs_tab_type
817                 )
818               , CAST
819                 ( MULTISET
820                   ( gmd_wip_spec_vrs_rec_type
821                     ( SELECT <gmd_wip_spec_vrs table columns>
822                       FROM   <gmd_wip_spec_crs table set>
823                       WHERE  <gmd_wip_spec_vrs conditions with bind variables> ***
824                   AS system.gmd_wip_spec_vrs_tab_type
825                 )
826               , CAST
827                 ( MULTISET
828                   ( gmd_supl_spec_vrs_rec_type
829                     ( SELECT <gmd_supl_spec_vrs table columns>
830                       FROM   <gmd_supl_spec_crs table set>
831                       WHERE  <gmd_supl_spec_vrs conditions with bind variables> ***
832                   AS system.gmd_supl_spec_vrs_tab_type
833                 )
834               , CAST
835                 ( MULTISET
836                   ( gmd_inv_spec_vrs_rec_type
837                     ( SELECT <gmd_inv_spec_vrs table columns>
838                       FROM   <gmd_inv_spec_crs table set>
839                       WHERE  <gmd_inv_spec_vrs conditions with bind variables> ***
840                   AS system.gmd_inv_spec_vrs_tab_type
841                 )
842               )
843        FROM
844             <main table list>
845        WHERE
846               <specification constraining conditions with binds> ****
847     */
848 
849     -- The lines marked *** are the slightly complicated ones to construct as the conditions
850     -- have to have the appropriate bind variables embedded.
851 
852     -- The whole statement is then put into an EXECUTE IMMEDIATE statement to pass it
853     -- to the database. Here goes......
854 
855 
856     -- GMD_SPECIFICATIONS basic clauses
857 
858     main_column_list := '  gs.spec_id, gs.spec_name, gs.spec_vers, gs.spec_desc'
859                       ||', gs.inventory_item_id, NULL, gs.grade_code, gs.revision, gs.spec_status, gstat.description'
860                       ||', gs.owner_organization_id, gs.owner_id, fu3.user_name'
861                       ||', gs.sample_inv_trans_ind'
862                       -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
863                       ||', gs.overlay_ind, gs.spec_type, gs.base_spec_id'
864                       -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
865                       ||', gs.delete_mark, gs.text_code'
866                       ||', gs.attribute_category, gs.attribute1, gs.attribute2, gs.attribute3'
867                       ||', gs.attribute4, gs.attribute5, gs.attribute6, gs.attribute7'
868                       ||', gs.attribute8, gs.attribute9, gs.attribute10, gs.attribute11'
869                       ||', gs.attribute12, gs.attribute13, gs.attribute14, gs.attribute15'
870                       ||', gs.attribute16, gs.attribute17, gs.attribute18, gs.attribute19'
871                       ||', gs.attribute20, gs.attribute21, gs.attribute22, gs.attribute23'
872                       ||', gs.attribute24, gs.attribute25, gs.attribute26, gs.attribute27'
873                       ||', gs.attribute28, gs.attribute29, gs.attribute30, gs.creation_date'
874                       ||', gs.created_by, fu1.user_name, gs.last_update_date'
875                       ||', gs.last_updated_by, fu2.user_name, gs.last_update_login';
876 
877     main_table_list := ' gmd_specifications gs, fnd_user fu1, fnd_user fu2, fnd_user fu3, gmd_status gstat';
878 
879     main_where_clause := ' gs.created_by = fu1.user_id'
880                        ||' AND gs.last_updated_by = fu2.user_id'
881                        ||' AND gs.owner_id = fu3.user_id'
882                        ||' AND to_char(gs.spec_status) = gstat.status_code'
883                        ||' AND 1=:dummy1 ';
884 
885     main_using_clause := '1';
886 
887     -- GMD_SPEC_TESTS basic clauses
888 
892                            ||', gst.test_qty, gst.test_qty_uom, gst.min_value_num, gst.target_value_num'
889     spec_test_column_list := '  system.gmd_spec_test_rec_type' -- 5284247
890                            ||'( gst.test_id, gqt.test_code, gst.test_method_id, gtm.test_method_code'
891                            ||', gst.seq'
893                            ||', gst.max_value_num, gst.min_value_char, gst.target_value_char'
894                            ||', gst.max_value_char, gst.test_replicate, gst.check_result_interval'
895                            ||', gst.print_on_coa_ind, gst.use_to_control_step, gst.out_of_spec_action'
896                            ||', gst.exp_error_type, gst.below_spec_min, gst.above_spec_min, gst.below_spec_max'
897                            ||', gst.above_spec_max, gst.below_min_action_code, gst.above_min_action_code'
898                            ||', gst.optional_ind, gst.display_precision, gst.report_precision'
899                            ||', gst.test_priority, gst.retest_lot_expiry_ind, gst.print_spec_ind'
900                            ||', gst.print_result_ind, gst.below_max_action_code, gst.above_max_action_code'
901                            ||', gst.test_display'
902                            -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
903                            ||', gst.days, gst.hours, gst.minutes, gst.seconds, gst.from_base_ind'
904                            ||', gst.exclude_ind, gst.modified_ind, gst.calc_uom_conv_ind, gst.to_qty_uom'
905                            -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
906                            ||', gst.text_code'
907                            ||', gst.attribute_category, gst.attribute1, gst.attribute2, gst.attribute3'
908                            ||', gst.attribute4, gst.attribute5, gst.attribute6, gst.attribute7'
909                            ||', gst.attribute8, gst.attribute9, gst.attribute10, gst.attribute11'
910                            ||', gst.attribute12, gst.attribute13, gst.attribute14, gst.attribute15'
911                            ||', gst.attribute16, gst.attribute17, gst.attribute18, gst.attribute19'
912                            ||', gst.attribute20, gst.attribute21, gst.attribute22, gst.attribute23'
913                            ||', gst.attribute24, gst.attribute25, gst.attribute26, gst.attribute27'
914                            ||', gst.attribute28, gst.attribute29, gst.attribute30, gst.creation_date'
915                            ||', gst.created_by, fu4.user_name, gst.last_update_date'
916                            ||', gst.last_updated_by, fu5.user_name, gst.last_update_login'
917                            ||')';
918 
919    spec_test_table_list := ' gmd_spec_tests gst, fnd_user fu4, fnd_user fu5, gmd_test_methods gtm, gmd_qc_tests gqt';
920 
921    spec_test_where_clause := ' gst.created_by = fu4.user_id AND gst.last_updated_by=fu5.user_id '
922                            ||' AND   gst.test_id = gqt.test_id AND gst.test_method_id=gtm.test_method_id'
923                            ||' AND   gst.spec_id = gs.spec_id AND 1=:dummy2 ';
924 
925    spec_test_using_clause := '1';
926 
927    -- GMD_CUSTOMER_SPEC_VRS basic clauses
928 
929    cust_vr_column_list := '  system.gmd_cust_spec_vr_rec_type' -- 5284247
930                         ||'( cvr.spec_vr_id, cvr.organization_id, cvr.cust_id, NULL, cvr.order_id, NULL'
931                         ||', cvr.order_line, cvr.order_line_id, cvr.ship_to_site_id, NULL, cvr.org_id'
932                         ||', NULL, cvr.spec_vr_status, NULL, cvr.start_date, cvr.end_date, cvr.sampling_plan_id'
933                         ||', NULL, cvr.sample_inv_trans_ind, cvr.lot_optional_on_sample, cvr.coa_type'
934                         ||', cvr.coa_at_ship_ind, cvr.coa_at_invoice_ind, cvr.coa_req_from_supl_ind'
935                         ||', cvr.delete_mark, cvr.text_code, cvr.attribute_category'
936                         ||', cvr.attribute1, cvr.attribute2, cvr.attribute3, cvr.attribute4, cvr.attribute5'
937                         ||', cvr.attribute6, cvr.attribute7, cvr.attribute8, cvr.attribute9, cvr.attribute10'
938                         ||', cvr.attribute11, cvr.attribute12, cvr.attribute13, cvr.attribute14, cvr.attribute15'
939                         ||', cvr.attribute16, cvr.attribute17, cvr.attribute18, cvr.attribute19, cvr.attribute20'
940                         ||', cvr.attribute21, cvr.attribute22, cvr.attribute23, cvr.attribute24, cvr.attribute25'
941                         ||', cvr.attribute26, cvr.attribute27, cvr.attribute28, cvr.attribute29, cvr.attribute30'
942                         ||', cvr.creation_date, cvr.created_by, fu6.user_name, cvr.last_update_date'
943                         ||', cvr.last_updated_by, fu7.user_name, cvr.last_update_login'
944                         ||')';
945    cust_vr_table_list := ' gmd_customer_spec_vrs cvr, fnd_user fu6, fnd_user fu7, oe_order_headers_all oeh';
946 
947    cust_vr_where_clause := ' cvr.created_by = fu6.user_id AND cvr.last_updated_by = fu7.user_id'
948                          ||' AND cvr.spec_id = gs.spec_id AND cvr.order_id = oeh.header_id(+) AND 1=:dummy3';
949 
950    cust_vr_using_clause := '1';
951 
952    -- GMD_WIP_SPEC_VRS basic clauses
953    --  Bug 3704090; Added fields delayed_lot_entry and auto_sample_ind
954 
955    wip_vr_column_list := ' system.gmd_wip_spec_vr_rec_type' --5284247
956                        ||'( wvr.spec_vr_id, wvr.organization_id, wvr.batch_id, gbh.batch_no, wvr.recipe_id, wvr.recipe_no'
957                        ||', wvr.recipe_version, wvr.formula_id, wvr.formula_no, wvr.formula_vers, wvr.routing_id'
958                        ||', wvr.routing_no, wvr.routing_vers, wvr.step_id, wvr.step_no, wvr.oprn_id, wvr.oprn_no'
959                        ||', wvr.oprn_vers, wvr.charge, wvr.spec_vr_status, NULL, wvr.start_date, wvr.end_date'
960                        ||', wvr.sampling_plan_id, NULL, wvr.sample_inv_trans_ind, wvr.lot_optional_on_sample'
961 					   ||', wvr.delayed_lot_entry, wvr.auto_sample_ind'
962                        ||', wvr.control_lot_attrib_ind, wvr.out_of_spec_lot_status_id, wvr.in_spec_lot_status_id'
963                        ||', wvr.coa_type, wvr.control_batch_step_ind, wvr.coa_at_ship_ind, wvr.coa_at_invoice_ind'
967                        ||', wvr.attribute11, wvr.attribute12, wvr.attribute13, wvr.attribute14, wvr.attribute15'
964                        ||', wvr.coa_req_from_supl_ind, wvr.delete_mark, wvr.text_code, wvr.attribute_category'
965                        ||', wvr.attribute1, wvr.attribute2, wvr.attribute3, wvr.attribute4, wvr.attribute5'
966                        ||', wvr.attribute6, wvr.attribute7, wvr.attribute8, wvr.attribute9, wvr.attribute10'
968                        ||', wvr.attribute16, wvr.attribute17, wvr.attribute18, wvr.attribute19, wvr.attribute20'
969                        ||', wvr.attribute21, wvr.attribute22, wvr.attribute23, wvr.attribute24, wvr.attribute25'
970                        ||', wvr.attribute26, wvr.attribute27, wvr.attribute28, wvr.attribute29, wvr.attribute30'
971                        ||', wvr.creation_date, wvr.created_by, fu8.user_name, wvr.last_update_date'
972                        ||', wvr.last_updated_by, fu9.user_name, wvr.last_update_login'
973                        ||')';
974 
975    wip_vr_table_list := ' gmd_wip_spec_vrs wvr, fnd_user fu8, fnd_user fu9, gme_batch_header gbh';
976 
977    wip_vr_where_clause := ' wvr.created_by = fu8.user_id AND wvr.last_updated_by = fu9.user_id'
978                         ||' AND wvr.batch_id= gbh.batch_id(+) and gbh.batch_type(+) = 0'
979                         ||' AND wvr.spec_id = gs.spec_id and 1=:dummy4';
980 
981    wip_vr_using_clause := '1';
982 
983    -- GMD_SUPPLIER_SPEC_VRS basic clauses
984    --  Bug 3704090; Added fields delayed_lot_entry and auto_sample_ind
985 
986    supl_vr_column_list := ' system.gmd_supl_spec_vr_rec_type' -- 5284247
987                         ||'( svr.po_header_id, NULL, svr.spec_vr_id, svr.organization_id, svr.supplier_id'
988                         ||', v.segment1, svr.supplier_site_id, NULL, svr.org_id, svr.po_line_id'
989                         ||', NULL, svr.spec_vr_status, NULL, svr.start_date, svr.end_date'
990                         ||', svr.sampling_plan_id, NULL, svr.sample_inv_trans_ind, svr.lot_optional_on_sample'
991 			||', svr.delayed_lot_entry, svr.auto_sample_ind'
992                         ||', svr.coa_type, svr.coa_at_ship_ind, svr.coa_at_invoice_ind, svr.coa_req_from_supl_ind'
993 		       	||', svr.out_of_spec_lot_status_id, svr.in_spec_lot_status_id'
994                         ||', svr.delete_mark, svr.text_code, svr.attribute_category, svr.attribute1'
995                         ||', svr.attribute2, svr.attribute3, svr.attribute4, svr.attribute5, svr.attribute6'
996                         ||', svr.attribute7, svr.attribute8, svr.attribute9, svr.attribute10, svr.attribute11'
997                         ||', svr.attribute12, svr.attribute13, svr.attribute14, svr.attribute15, svr.attribute16'
998                         ||', svr.attribute17, svr.attribute18, svr.attribute19, svr.attribute20, svr.attribute21'
999                         ||', svr.attribute22, svr.attribute23, svr.attribute24, svr.attribute25, svr.attribute26'
1000                         ||', svr.attribute27, svr.attribute28, svr.attribute29, svr.attribute30, svr.creation_date'
1001                         ||', svr.created_by, fu10.user_name, svr.last_update_date, svr.last_updated_by'
1002                         ||', fu11.user_name, svr.last_update_login'
1003                         ||')';
1004 
1005    supl_vr_table_list := ' gmd_supplier_spec_vrs svr, fnd_user fu10, fnd_user fu11, po_vendors v';
1006 
1007    supl_vr_where_clause := ' svr.created_by = fu10.user_id AND svr.last_updated_by = fu11.user_id'
1008                          ||' AND svr.spec_id = gs.spec_id AND v.vendor_id = svr.supplier_id AND 1=:dummy5';
1009    supl_vr_using_clause := '1';
1010 
1011    -- GMD_INVENTORY_SPEC_VRS basic clauses
1012    --  Bug 3704090; Added fields delayed_lot_entry and auto_sample_ind
1013 
1014    inv_vr_column_list := ' system.gmd_inv_spec_vr_rec_type' -- 5284247
1015                        ||'( ivr.spec_vr_id, ivr.organization_id, ivr.parent_lot_number, ivr.lot_number'
1016                        ||', ivr.subinventory, ivr.locator_id, ivr.spec_vr_status, NULL, ivr.start_date'
1017                        ||', ivr.end_date, ivr.sampling_plan_id, NULL, ivr.sample_inv_trans_ind'
1018                        ||', ivr.control_lot_attrib_ind, ivr.lot_optional_on_sample'
1019                        ||', ivr.delayed_lot_entry, ivr.auto_sample_ind'
1020                        ||', ivr.in_spec_lot_status_id'
1021                        ||', ivr.out_of_spec_lot_status_id, ivr.control_batch_step_ind, ivr.coa_type'
1022                        ||', ivr.coa_at_ship_ind, ivr.coa_at_invoice_ind, ivr.coa_req_from_supl_ind'
1023                        ||', ivr.delete_mark, ivr.text_code, ivr.attribute_category, ivr.attribute1'
1024                        ||', ivr.attribute2, ivr.attribute3, ivr.attribute4, ivr.attribute5, ivr.attribute6'
1025                        ||', ivr.attribute7, ivr.attribute8, ivr.attribute9, ivr.attribute10, ivr.attribute11'
1026                        ||', ivr.attribute12, ivr.attribute13, ivr.attribute14, ivr.attribute15'
1027                        ||', ivr.attribute16, ivr.attribute17, ivr.attribute18, ivr.attribute19'
1028                        ||', ivr.attribute20, ivr.attribute21, ivr.attribute22, ivr.attribute23'
1029                        ||', ivr.attribute24, ivr.attribute25, ivr.attribute26, ivr.attribute27'
1030                        ||', ivr.attribute28, ivr.attribute29, ivr.attribute30, ivr.creation_date'
1031                        ||', ivr.created_by, fu12.user_name, ivr.last_update_date, ivr.last_updated_by'
1032                        ||', fu13.user_name, ivr.last_update_login'
1033                        ||')';
1034    inv_vr_table_list := ' gmd_inventory_spec_vrs ivr, fnd_user fu12, fnd_user fu13';
1035 
1036    inv_vr_where_clause := ' ivr.created_by = fu12.user_id AND ivr.last_updated_by = fu13.user_id'
1037                         ||' AND ivr.spec_id = gs.spec_id AND 1=:dummy6';
1038    inv_vr_using_clause := '1';
1039 
1040    -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
1041    -- GMD_MONITORING_SPEC_VRS basic clauses
1042 
1043    mon_vr_column_list := ' system.gmd_mon_spec_vr_rec_type' -- 5284247
1044                        ||'( mvr.spec_vr_id, mvr.spec_id, mvr.rule_type, mvr.locator_organization_id, mvr.subinventory'
1048                        ||', mvr.attribute_category, mvr.attribute1, mvr.attribute2, mvr.attribute3'
1045                        ||', mvr.locator_id, mvr.resources, mvr.resource_organization_id, mvr.resource_instance_id'
1046                        ||', mvr.spec_vr_status, NULL, mvr.start_date, mvr.end_date'
1047                        ||', mvr.sampling_plan_id, NULL, mvr.delete_mark, mvr.text_code'
1049                        ||', mvr.attribute4, mvr.attribute5, mvr.attribute6, mvr.attribute7, mvr.attribute8'
1050                        ||', mvr.attribute9, mvr.attribute10, mvr.attribute11, mvr.attribute12, mvr.attribute13'
1051                        ||', mvr.attribute14, mvr.attribute15, mvr.attribute16, mvr.attribute17, mvr.attribute18'
1052                        ||', mvr.attribute19, mvr.attribute20, mvr.attribute21, mvr.attribute22, mvr.attribute23'
1053                        ||', mvr.attribute24, mvr.attribute25, mvr.attribute26, mvr.attribute27, mvr.attribute28'
1054                        ||', mvr.attribute29, mvr.attribute30, mvr.creation_date, mvr.created_by'
1055                        ||', mvr.last_updated_by, mvr.last_update_date, mvr.last_update_login'
1056                        ||')';
1057    mon_vr_table_list := ' gmd_monitoring_spec_vrs mvr, fnd_user fu14, fnd_user fu15';
1058 
1059    mon_vr_where_clause := ' mvr.created_by = fu14.user_id AND mvr.last_updated_by = fu15.user_id'
1060                         ||' AND mvr.spec_id = gs.spec_id AND 1=:dummy7';
1061    mon_vr_using_clause := '1';
1062    -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
1063 
1064 
1065     -- Now comes the fun. We need to add conditions that restrict which rows are retrieved
1066     -- from each table by inspecting the parameters that are passed in and build a list of
1067     -- bind variable values as we go along.
1068 
1069     -- We start with the base table itself, GMD_SPECIFICATIONS
1070 
1071     IF p_from_spec_name IS NOT NULL
1072     THEN
1073       g_from_spec_name := p_from_spec_name;
1074       main_where_clause := main_where_clause||' AND gs.spec_name >= :from_spec_name';
1075       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_spec_name';
1076     END IF;
1077 
1078     IF p_to_spec_name IS NOT NULL
1079     THEN
1080       g_to_spec_name := p_to_spec_name;
1081       main_where_clause := main_where_clause||' AND gs.spec_name <= :to_spec_name';
1082       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_spec_name';
1083     END IF;
1084 
1085     IF p_spec_id IS NOT NULL
1086     THEN
1087       gmd_outbound_apis_pub.g_spec_id := p_spec_id;
1088       main_where_clause := main_where_clause||' AND gs.spec_id = :spec_id';
1089       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_spec_id';
1090     END IF;
1091 
1092     IF p_spec_version IS NOT NULL
1093     THEN
1094       g_spec_version := p_spec_version;
1095       main_where_clause := main_where_clause||' AND gs.spec_vers = :spec_version';
1096       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_spec_version';
1097     END IF;
1098 
1099     IF p_from_grade_code IS NOT NULL
1100     THEN
1101       g_from_grade_code := p_from_grade_code;
1102       main_where_clause := main_where_clause||' AND gs.grade_code >= :from_grade_code';
1103       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_grade_code';
1104     END IF;
1105 
1106     IF p_to_grade_code IS NOT NULL
1107     THEN
1108       g_to_grade_code := p_to_grade_code;
1109       main_where_clause := main_where_clause||' AND gs.grade <= :to_grade_code';
1110       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_grade_code';
1111     END IF;
1112 
1113     IF p_inventory_item_id IS NOT NULL
1114     THEN
1115       g_inventory_item_id := p_inventory_item_id;
1116       main_where_clause := main_where_clause|| ' AND gs.inventory_item_id = :inventory_item_id';
1117       main_using_clause := main_using_clause|| ', gmd_outbound_apis_pub.g_inventory_item_id';
1118     END if;
1119 
1120 -- Start RLNAGARA Bug # 4548546
1121     IF p_from_revision IS NOT NULL
1122     THEN
1123       g_from_revision := p_from_revision;
1124       main_where_clause := main_where_clause||' AND gs.revision >= :from_revision';
1125       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_revision';
1126     END IF;
1127 
1128     IF p_to_revision IS NOT NULL
1129     THEN
1130       g_to_revision := p_to_revision;
1131       main_where_clause := main_where_clause||' AND gs.revision <= :to_revision';
1132       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_revision';
1133     END IF;
1134 -- End RLNAGARA Bug # 4548546
1135 
1136     IF p_from_item_number IS NOT NULL and p_to_item_number IS NOT NULL
1137     THEN
1138       g_from_item_number := p_from_item_number;
1139       g_to_item_number := p_to_item_number;
1140       main_where_clause := main_where_clause
1141                         ||' AND gs.inventory_item_id IN (SELECT inventory_item_id FROM mtl_system_items_b_kfv'
1142                         ||' WHERE concatenated_segments BETWEEN :from_item_number AND :to_item_number'
1143 			||' AND organization_id = gs.owner_organization_id)';
1144       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_item_number, gmd_outbound_apis_pub.g_to_item_number';
1145     ELSIF p_from_item_number IS NOT NULL
1146     THEN
1147       g_from_item_number := p_from_item_number;
1148       main_where_clause := main_where_clause
1149                         ||' AND gs.inventory_item_id IN (SELECT inventory_item_id FROM mtl_system_items_b_kfv'
1150                         ||' WHERE concatenated_segments >= :from_item_number'
1151 			||' AND organization_id = gs.owner_organization_id)';
1152       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_item_number';
1153     ELSIF p_to_item_number IS NOT NULL
1154     THEN
1155       g_to_item_number := p_to_item_number;
1156       main_where_clause := main_where_clause
1160       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_item_number';
1157                         ||' AND gs.inventory_item_id IN (SELECT inventory_item_id FROM mtl_system_items_b_kfv'
1158                         ||' WHERE concatenated_segments <= :to_item_number'
1159 			||' AND organization_id = gs.owner_organization_id)';
1161     END IF;
1162 
1163     IF p_owner_organization_code IS NOT NULL
1164     THEN
1165       g_owner_organization_code := p_owner_organization_code;
1166       main_where_clause := main_where_clause|| ' AND gs.owner_organization_id = (SELECT organization_id'
1167                                         || ' FROM mtl_organizations WHERE organization_code = :owner_organization_code)';
1168       main_using_clause := main_using_clause|| ', gmd_outbound_apis_pub.g_owner_organization_code';
1169     END IF;
1170 
1171     IF p_spec_status IS NOT NULL
1172     THEN
1173       g_spec_status := p_spec_status;
1174       main_where_clause := main_where_clause|| ' AND gs.spec_status = :spec_status';
1175       main_using_clause := main_using_clause|| ', gmd_outbound_apis_pub.g_spec_status';
1176     END IF;
1177 
1178     IF p_spec_delete_mark IS NOT NULL
1179     THEN
1180       g_spec_delete_mark := p_spec_delete_mark;
1181       main_where_clause := main_where_clause|| ' AND gs.delete_mark = :delete_mark';
1182       main_using_clause := main_using_clause|| ', gmd_outbound_apis_pub.g_spec_delete_mark';
1183     END IF;
1184 
1185     IF p_from_spec_last_update IS NOT NULL
1186     THEN
1187       gmd_outbound_apis_pub.g_from_spec_last_update := p_from_spec_last_update;
1188       main_where_clause := main_where_clause||' AND gs.last_update_date >= :from_spec_last_update';
1189       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_spec_last_update';
1190     END IF;
1191 
1192     IF p_to_spec_last_update IS NOT NULL
1193     THEN
1194       gmd_outbound_apis_pub.g_to_spec_last_update := p_to_spec_last_update;
1195       main_where_clause := main_where_clause||' AND gs.last_update_date <= :to_spec_last_update';
1196       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_spec_last_update';
1197     END IF;
1198 
1199     -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
1200     IF p_overlay_ind IS NOT NULL
1201     THEN
1202       gmd_outbound_apis_pub.g_overlay_ind := p_overlay_ind;
1203       main_where_clause := main_where_clause||' AND gs.overlay_ind = :overlay_ind';
1204       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_overlay_ind';
1205     END IF;
1206 
1207     IF p_spec_type IS NOT NULL
1208     THEN
1209       gmd_outbound_apis_pub.g_spec_type := p_spec_type;
1210       main_where_clause := main_where_clause||' AND gs.spec_type = :spec_type';
1211       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_spec_type';
1212     END IF;
1213 
1214     IF p_base_spec_id IS NOT NULL
1215     THEN
1216       gmd_outbound_apis_pub.g_base_spec_id := p_base_spec_id;
1217       main_where_clause := main_where_clause||' AND gs.base_spec_id = :base_spec_id';
1218       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_base_spec_id';
1219     END IF;
1220 
1221     IF p_base_spec_name IS NOT NULL OR p_base_spec_version IS NOT NULL
1222     THEN
1223       main_table_list := main_table_list || ', gmd_specifications_b gsp';
1224       main_where_clause := main_where_clause || ' AND gsp.spec_id = gs.base_spec_id';
1225 
1226       IF p_base_spec_name IS NOT NULL
1227       THEN
1228         gmd_outbound_apis_pub.g_base_spec_name := p_base_spec_name;
1229         main_where_clause := main_where_clause||' AND gsp.spec_name = :base_spec_name';
1230         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_base_spec_name';
1231       END IF;
1232 
1233       IF p_base_spec_version IS NOT NULL
1234       THEN
1235         gmd_outbound_apis_pub.g_base_spec_name := p_base_spec_name;
1236         main_where_clause := main_where_clause||' AND gsp.spec_vers = :base_spec_version';
1237         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_base_spec_version';
1238       END IF;
1239 
1240     END IF;
1241     -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
1242 
1243     -- Now sort out retrieval from GMD_SPEC_TESTS
1244 
1245     IF p_test_code IS NOT NULL OR p_test_id IS NOT NULL OR p_test_method_code IS NOT NULL
1246     OR p_test_method_id IS NOT NULL OR p_test_qty_uom IS NOT NULL OR p_test_priority IS NOT NULL
1247     OR p_from_test_last_update IS NOT NULL OR p_to_test_last_update IS NOT NULL
1248     OR p_test_delete_mark IS NOT NULL
1249     -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
1250     OR p_from_base_ind IS NOT NULL OR p_exclude_ind IS NOT NULL OR p_modified_ind IS NOT NULL
1251     OR p_calc_uom_conv_ind IS NOT NULL OR p_to_qty_uom IS NOT NULL
1252     -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
1253     THEN
1254 
1255       -- Add the tables to the main list and join to it.
1256 
1257       main_where_clause := main_where_clause
1258                         ||' AND gs.spec_id IN '
1259                         ||' (SELECT mgst.spec_id FROM gmd_spec_tests mgst '
1260                         ||'  WHERE 1=1';
1261 
1262       -- Now work down the parameters that apply against this table and add to the
1263       -- main where clause and the spec_test where clause.
1264 
1265       IF p_test_code IS NOT NULL
1266       THEN
1267         gmd_outbound_apis_pub.g_test_code := p_test_code;
1268         main_table_list := main_table_list||', gmd_qc_tests_b gqtb';
1269         main_where_clause := main_where_clause||' AND gqtb.test_code = :p_test_code';
1270         main_where_clause := main_where_clause||' AND mgst.test_id = gqtb.test_id';
1271         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_test_code';
1272 
1273         spec_test_where_clause := spec_test_where_clause||' AND gst.test_id=gqtb.test_id';
1277       THEN
1274       END IF;
1275 
1276       IF p_test_id IS NOT NULL
1278         gmd_outbound_apis_pub.g_test_id := p_test_id;
1279         main_where_clause := main_where_clause||' AND mgst.test_id = :p_test_id';
1280         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_test_id';
1281 
1282         spec_test_where_clause := spec_test_where_clause||' AND gst.test_id = :p_test_id';
1283         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_test_id';
1284       END IF;
1285 
1286       IF p_test_method_id IS NOT NULL
1287       THEN
1288         gmd_outbound_apis_pub.g_test_method_id := p_test_method_id;
1289         main_where_clause := main_where_clause||' AND gtm.test_method_id = :p_test_method_id';
1290         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_test_method_id';
1291 
1292         spec_test_where_clause := spec_test_where_clause||' AND gtm.test_method_id = :p_test_method_id';
1293         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_test_method_id';
1294       END IF;
1295 
1296       IF p_test_method_code IS NOT NULL
1297       THEN
1298         -- Need to link in the test methods table if the test method code parameter is passed
1299 
1300         main_table_list := main_table_list||', gmd_test_methods mgtm';
1301 
1302         gmd_outbound_apis_pub.g_test_method_code := p_test_method_code;
1303         main_where_clause := main_where_clause||' AND mgtm.test_method_code = :p_test_method_code'
1304                                               ||' AND mgtm.test_method_id = mgst.test_method_id';
1305         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_test_method_code';
1306 
1307         spec_test_where_clause := spec_test_where_clause||' AND gtm.test_method_code = :p_test_method_code';
1308         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_test_method_code';
1309 
1310       END IF;
1311 
1312       IF p_test_qty_uom IS NOT NULL
1313       THEN
1314         gmd_outbound_apis_pub.g_test_qty_uom := p_test_qty_uom;
1315         main_where_clause := main_where_clause||' AND mgst.test_qty_uom = :p_test_qty_uom';
1316         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_test_qty_uom';
1317 
1318         spec_test_where_clause := spec_test_where_clause||' AND gst.test_qty_uom = :p_test_qty_uom';
1319         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_test_qty_uom';
1320       END IF;
1321 
1322       IF p_test_priority IS NOT NULL
1323       THEN
1324         gmd_outbound_apis_pub.g_test_priority := p_test_priority;
1325         main_where_clause := main_where_clause||' AND mgst.test_priority = :p_test_priority';
1326         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_test_priority';
1327 
1328         spec_test_where_clause := spec_test_where_clause||' AND gst.test_priority = :p_test_priority';
1329         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_test_priority';
1330       END IF;
1331 
1332       IF p_from_test_last_update IS NOT NULL
1333       THEN
1334         gmd_outbound_apis_pub.g_from_test_last_update := p_from_test_last_update;
1335         main_where_clause := main_where_clause||' AND mgst.last_update_date >= :p_from_test_last_update';
1336         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_test_last_update';
1337 
1338         spec_test_where_clause := spec_test_where_clause||' AND gst.last_update_date >= :p_from_test_last_update';
1339         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_from_test_last_update';
1340       END IF;
1341 
1342       IF p_to_test_last_update IS NOT NULL
1343       THEN
1344         gmd_outbound_apis_pub.g_to_test_last_update := p_to_test_last_update;
1345         main_where_clause := main_where_clause||' AND mgst.to_update_date <= :p_to_test_last_update';
1346         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_test_last_update';
1347 
1348         spec_test_where_clause := spec_test_where_clause||' AND gst.to_update_date <= :p_to_test_last_update';
1349         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_to_test_last_update';
1350       END IF;
1351 
1352       IF p_test_delete_mark IS NOT NULL
1353       THEN
1354         gmd_outbound_apis_pub.g_test_delete_mark := p_test_delete_mark;
1355         main_where_clause := main_where_clause||' AND mgst.delete_mark = :p_test_delete_mark';
1356         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_test_delete_mark';
1357 
1358         spec_test_where_clause := spec_test_where_clause||' AND gst.delete_mark = :p_test_delete_mark';
1359         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_test_delete_mark';
1360       END IF;
1361 
1362       -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
1363       IF p_from_base_ind IS NOT NULL
1364       THEN
1365         gmd_outbound_apis_pub.g_from_base_ind := p_from_base_ind;
1366         main_where_clause := main_where_clause||' AND mgst.from_base_ind = :p_from_base_ind';
1367         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_base_ind';
1368 
1369         spec_test_where_clause := spec_test_where_clause||' AND gst.from_base_ind = :p_from_base_ind';
1370         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_from_base_ind';
1371       END IF;
1372 
1373       IF p_exclude_ind IS NOT NULL
1374       THEN
1375         gmd_outbound_apis_pub.g_exclude_ind := p_exclude_ind;
1376         main_where_clause := main_where_clause||' AND mgst.exclude_ind = :p_exclude_ind';
1377         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_exclude_ind';
1378 
1379         spec_test_where_clause := spec_test_where_clause||' AND gst.exclude_ind = :p_exclude_ind';
1380         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_exclude_ind';
1381       END IF;
1382 
1386         main_where_clause := main_where_clause||' AND mgst.modified_ind = :p_modified_ind';
1383       IF p_modified_ind IS NOT NULL
1384       THEN
1385         gmd_outbound_apis_pub.g_modified_ind := p_modified_ind;
1387         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_modified_ind';
1388 
1389         spec_test_where_clause := spec_test_where_clause||' AND gst.modified_ind = :p_modified_ind';
1390         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_modified_ind';
1391       END IF;
1392 
1393       IF p_calc_uom_conv_ind IS NOT NULL
1394       THEN
1395         gmd_outbound_apis_pub.g_calc_uom_conv_ind := p_calc_uom_conv_ind;
1396         main_where_clause := main_where_clause||' AND mgst.calc_uom_conv_ind = :p_calc_uom_conv_ind';
1397         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_calc_uom_conv_ind';
1398 
1399         spec_test_where_clause := spec_test_where_clause||' AND gst.calc_uom_conv_ind = :p_calc_uom_conv_ind';
1400         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_calc_uom_conv_ind';
1401       END IF;
1402 
1403       IF p_to_qty_uom IS NOT NULL
1404       THEN
1405         gmd_outbound_apis_pub.g_to_qty_uom := p_to_qty_uom;
1406         main_where_clause := main_where_clause||' AND mgst.to_qty_uom = :p_to_qty_uom';
1407         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_qty_uom';
1408 
1409         spec_test_where_clause := spec_test_where_clause||' AND gst.to_qty_uom = :p_to_qty_uom';
1410         spec_test_using_clause := spec_test_using_clause||', gmd_outbound_apis_pub.g_to_qty_uom';
1411       END IF;
1412 
1413       -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
1414 
1415       main_where_clause := main_where_clause||' ) ';
1416 
1417     END IF;
1418 
1419     -- Now sort out the retrieval from GMD_WIP_SPEC_VRS
1420 
1421 
1422     IF p_wip_vr_status IS NOT NULL OR p_wip_vr_organization_code IS NOT NULL OR p_wip_vr_batch_orgn_code IS NOT NULL
1423     OR p_wip_vr_batch_no IS NOT NULL OR p_wip_vr_batch_id IS NOT NULL OR p_wip_vr_recipe_no IS NOT NULL
1424     OR p_wip_vr_recipe_version IS NOT NULL OR p_wip_vr_recipe_id IS NOT NULL OR p_wip_vr_formula_no IS NOT NULL
1425     OR p_wip_vr_formula_version IS NOT NULL OR p_wip_vr_formula_id IS NOT NULL OR p_wip_vr_formulaline_no IS NOT NULL
1426     OR p_wip_vr_formulaline_id IS NOT NULL OR p_wip_vr_line_type IS NOT NULL OR p_wip_vr_routing_no IS NOT NULL
1427     OR p_wip_vr_routing_version IS NOT NULL OR p_wip_vr_routing_id IS NOT NULL OR p_wip_vr_step_no IS NOT NULL
1428     OR p_wip_vr_step_id IS NOT NULL OR p_wip_vr_operation_no IS NOT NULL OR p_wip_vr_operation_version IS NOT NULL
1429     OR p_wip_vr_operation_id IS NOT NULL OR p_wip_vr_start_date	IS NOT NULL OR p_wip_vr_end_date IS NOT NULL
1430     OR p_wip_vr_coa_type IS NOT NULL OR p_wip_vr_sampling_plan IS NOT NULL OR p_wip_vr_sampling_plan_id IS NOT NULL
1431     OR p_wip_vr_delete_mark IS NOT NULL OR p_wip_vr_from_last_update IS NOT NULL OR p_wip_vr_to_last_update IS NOT NULL
1432     THEN
1433       -- Add the table to the main list and join to it.
1434 
1435       main_where_clause := main_where_clause
1436                         ||' AND gs.spec_id IN'
1437                         ||' (SELECT mwvr.spec_id'
1438                         ||'  FROM gmd_wip_spec_vrs mwvr'
1439                         ||'  WHERE 1=1';
1440 
1441       IF p_wip_vr_status IS NOT NULL
1442       THEN
1443         gmd_outbound_apis_pub.g_wip_vr_status := p_wip_vr_status;
1444         main_where_clause := main_where_clause||' AND mwvr.spec_vr_status = :wip_spec_vr_status';
1445         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_status';
1446 
1447         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.spec_vr_status = :wip_spec_vr_status';
1448         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_status';
1449 
1450       END IF;
1451 
1452       IF p_wip_vr_organization_code IS NOT NULL
1453       THEN
1454         gmd_outbound_apis_pub.g_wip_vr_organization_code := p_wip_vr_organization_code;
1455         main_where_clause := main_where_clause||' AND mwvr.organization_id = (SELECT organization_id '
1456  	                            ||'FROM mtl_organizations WHERE organization_code = :wip_vr_organization_code)';
1457         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_organization_code';
1458 
1459         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.organization_id = (SELECT organization_id '
1460 	                            ||'FROM mtl_organizations WHERE organization_code = :wip_vr_organization_code)';
1461         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_organization_code';
1462 
1463       END IF;
1464 
1465       IF p_wip_vr_batch_orgn_code IS NOT NULL
1466       THEN
1467         gmd_outbound_apis_pub.g_wip_vr_batch_orgn_code := p_wip_vr_batch_orgn_code;
1468         main_where_clause := main_where_clause||' AND mwvr.batch_id IN'
1469                                               ||' (SELECT batch_id FROM gme_batch_header '
1470                                               ||'  WHERE organization_id = (SELECT organization_id FROM'
1471 				   ||' mtl_parameters WHERE organization_code = :wip_vr_batch_orgn_code) )';
1472         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_batch_orgn_code';
1473 
1474         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.batch_id IN'
1475                                               ||' (SELECT batch_id FROM gme_batch_header '
1476                                               ||'  WHERE organization_id = (SELECT organization_id FROM'
1477 			           ||' mtl_parameters WHERE organization_code = :wip_vr_batch_orgn_code) )';
1478         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_batch_orgn_code';
1479       END IF;
1480 
1481       IF p_wip_vr_batch_no IS NOT NULL
1482       THEN
1483         gmd_outbound_apis_pub.g_wip_vr_batch_no := p_wip_vr_batch_no;
1487                                               ||'  AND batch_type = 0)';
1484         main_where_clause := main_where_clause||' AND mwvr.batch_id IN'
1485                                               ||' (SELECT batch_id FROM gme_batch_header '
1486                                               ||'  WHERE batch_no = :wip_vr_batch_no '
1488         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_batch_no';
1489 
1490         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.batch_id IN'
1491                                               ||' (SELECT batch_id FROM gme_batch_header '
1492                                               ||'  WHERE batch_no = :wip_vr_batch_no '
1493                                               ||'  AND batch_type = 0)';
1494         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_batch_no';
1495       END IF;
1496 
1497       IF p_wip_vr_batch_id IS NOT NULL
1498       THEN
1499         gmd_outbound_apis_pub.g_wip_vr_batch_id := p_wip_vr_batch_id;
1500         main_where_clause := main_where_clause||' AND mwvr.batch_id = :wip_vr_batch_id';
1501         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_batch_id';
1502 
1503         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.batch_id = :wip_vr_batch_id';
1504         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_batch_id';
1505       END IF;
1506 
1507       IF p_wip_vr_recipe_no IS NOT NULL
1508       THEN
1509         gmd_outbound_apis_pub.g_wip_vr_recipe_no := p_wip_vr_recipe_no;
1510         main_where_clause := main_where_clause||' AND mwvr.recipe_no = :wip_vr_recipe_no';
1511         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_recipe_no';
1512 
1513         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.recipe_no = :wip_vr_recipe_no';
1514         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_recipe_no';
1515       END IF;
1516 
1517       IF p_wip_vr_recipe_version IS NOT NULL
1518       THEN
1519         gmd_outbound_apis_pub.g_wip_vr_recipe_version := p_wip_vr_recipe_version;
1520         main_where_clause := main_where_clause||' AND mwvr.recipe_version = :wip_vr_recipe_version';
1521         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_recipe_version';
1522 
1523         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.recipe_version = :wip_vr_recipe_version';
1524         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_recipe_version';
1525       END IF;
1526 
1527       IF p_wip_vr_recipe_id IS NOT NULL
1528       THEN
1529         gmd_outbound_apis_pub.g_wip_vr_recipe_id := p_wip_vr_recipe_id;
1530         main_where_clause := main_where_clause||' AND mwvr.recipe_id = :wip_vr_recipe_id';
1531         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_recipe_id';
1532 
1533         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.recipe_id = :wip_vr_recipe_id';
1534         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_recipe_id';
1535       END IF;
1536 
1537       IF p_wip_vr_formula_no IS NOT NULL
1538       THEN
1539         gmd_outbound_apis_pub.g_wip_vr_formula_no := p_wip_vr_formula_no;
1540         main_where_clause := main_where_clause||' AND mwvr.formula_no = :wip_vr_formula_no';
1541         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formula_no';
1542 
1543         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.formula_no = :wip_vr_formula_no';
1544         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formula_no';
1545       END IF;
1546 
1547       IF p_wip_vr_formula_version IS NOT NULL
1548       THEN
1549         gmd_outbound_apis_pub.g_wip_vr_formula_version := p_wip_vr_formula_version;
1550         main_where_clause := main_where_clause||' AND mwvr.formula_vers = :wip_vr_formula_version';
1551         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formula_version';
1552 
1553         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.formula_vers = :wip_vr_formula_version';
1554         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formula_version';
1555       END IF;
1556 
1557       IF p_wip_vr_formula_id IS NOT NULL
1558       THEN
1559         gmd_outbound_apis_pub.g_wip_vr_formula_id := p_wip_vr_formula_id;
1560         main_where_clause := main_where_clause||' AND mwvr.formula_id = :wip_vr_formula_id';
1561         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formula_id';
1562 
1563         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.formula_id = :wip_vr_formula_id';
1564         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formula_id';
1565       END IF;
1566 
1567       IF p_wip_vr_formulaline_no IS NOT NULL
1568       THEN
1569         gmd_outbound_apis_pub.g_wip_vr_formulaline_no := p_wip_vr_formulaline_no;
1570         main_where_clause := main_where_clause||' AND mwvr.formulaline_no = :wip_vr_formulaline_no';
1571         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formulaline_no';
1572 
1573         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.formulaline_no = :wip_vr_formulaline_no';
1574         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formulaline_no';
1575       END IF;
1576 
1577       IF p_wip_vr_formulaline_id IS NOT NULL
1578       THEN
1579         gmd_outbound_apis_pub.g_wip_vr_formulaline_id := p_wip_vr_formulaline_id;
1580         main_where_clause := main_where_clause||' AND mwvr.formulaline_id = :wip_vr_formulaline_id';
1581         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formulaline_id';
1582 
1583         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.formulaline_id = :wip_vr_formulaline_id';
1584         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_formulaline_id';
1585       END IF;
1586 
1587       IF p_wip_vr_routing_no IS NOT NULL
1591         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_routing_no';
1588       THEN
1589         gmd_outbound_apis_pub.g_wip_vr_formula_no := p_wip_vr_routing_no;
1590         main_where_clause := main_where_clause||' AND mwvr.routing_no = :wip_vr_routing_no';
1592 
1593         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.routing_no = :wip_vr_routing_no';
1594         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_routing_no';
1595       END IF;
1596 
1597       IF p_wip_vr_routing_version IS NOT NULL
1598       THEN
1599         gmd_outbound_apis_pub.g_wip_vr_routing_version := p_wip_vr_routing_version;
1600         main_where_clause := main_where_clause||' AND mwvr.routing_vers = :wip_vr_routing_version';
1601         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_routing_version';
1602 
1603         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.routing_vers = :wip_vr_routing_version';
1604         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_routing_version';
1605       END IF;
1606 
1607       IF p_wip_vr_routing_id IS NOT NULL
1608       THEN
1609         gmd_outbound_apis_pub.g_wip_vr_routing_id := p_wip_vr_routing_id;
1610         main_where_clause := main_where_clause||' AND mwvr.routing_id = :wip_vr_routing_id';
1611         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_routing_id';
1612 
1613         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.routing_id = :wip_vr_routing_id';
1614         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_routing_id';
1615       END IF;
1616 
1617       IF p_wip_vr_step_no IS NOT NULL
1618       THEN
1619         gmd_outbound_apis_pub.g_wip_vr_step_no := p_wip_vr_step_no;
1620         main_where_clause := main_where_clause||' AND mwvr.step_no = :wip_vr_step_no';
1621         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_step_no';
1622 
1623         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.step_no = :wip_vr_step_no';
1624         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_step_no';
1625       END IF;
1626 
1627       IF p_wip_vr_step_id IS NOT NULL
1628       THEN
1629         gmd_outbound_apis_pub.g_wip_vr_step_id := p_wip_vr_step_id;
1630         main_where_clause := main_where_clause||' AND mwvr.step_id = :wip_vr_step_id';
1631         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_step_id';
1632 
1633         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.step_id = :wip_vr_step_id';
1634         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_step_id';
1635       END IF;
1636 
1637       IF p_wip_vr_operation_no IS NOT NULL
1638       THEN
1639         gmd_outbound_apis_pub.g_wip_vr_operation_no := p_wip_vr_operation_no;
1640         main_where_clause := main_where_clause||' AND mwvr.oprn_no = :wip_vr_operation_no';
1641         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_operation_no';
1642 
1643         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.oprn_no = :wip_vr_operation_no';
1644         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_operation_no';
1645       END IF;
1646 
1647       IF p_wip_vr_operation_version IS NOT NULL
1648       THEN
1649         gmd_outbound_apis_pub.g_wip_vr_operation_version := p_wip_vr_operation_version;
1650         main_where_clause := main_where_clause||' AND mwvr.oprn_vers = :wip_vr_operation_version';
1651         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_operation_version';
1652 
1653         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.oprn_vers = :wip_vr_operation_version';
1654         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_operation_version';
1655       END IF;
1656 
1657       IF p_wip_vr_operation_id IS NOT NULL
1658       THEN
1659         gmd_outbound_apis_pub.g_wip_vr_operation_id := p_wip_vr_operation_id;
1660         main_where_clause := main_where_clause||' AND mwvr.oprn_id = :wip_vr_operation_id';
1661         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_operation_id';
1662 
1663         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.oprn_id = :wip_vr_operation_id';
1664         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_operation_id';
1665       END IF;
1666 
1667       IF p_wip_vr_start_date IS NOT NULL
1668       THEN
1669         gmd_outbound_apis_pub.g_wip_vr_start_date := p_wip_vr_start_date;
1670         main_where_clause := main_where_clause||' AND mwvr.start_date >= :wip_vr_start_date';
1671         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_start_date';
1672 
1673         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.start_date >= :wip_vr_start_date';
1674         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_start_date';
1675       END IF;
1676 
1677       IF p_wip_vr_end_date IS NOT NULL
1678       THEN
1679         gmd_outbound_apis_pub.g_wip_vr_end_date := p_wip_vr_end_date;
1680         main_where_clause := main_where_clause||' AND mwvr.end_date <= :wip_vr_end_date';
1681         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_end_date';
1682 
1683         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.end_date <= :wip_vr_end_date';
1684         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_end_date';
1685       END IF;
1686 
1687       IF p_wip_vr_coa_type IS NOT NULL
1688       THEN
1689         gmd_outbound_apis_pub.g_wip_vr_coa_type := p_wip_vr_coa_type;
1690         main_where_clause := main_where_clause||' AND mwvr.coa_type = :wip_vr_coa_type';
1691         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_coa_type';
1692 
1693         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.coa_type = :wip_vr_coa_type';
1697       IF p_wip_vr_sampling_plan_id IS NOT NULL
1694         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_coa_type';
1695       END IF;
1696 
1698       THEN
1699         gmd_outbound_apis_pub.g_wip_vr_sampling_plan_id := p_wip_vr_sampling_plan_id;
1700         main_where_clause := main_where_clause||' AND mwvr.sampling_plan_id = :wip_vr_sampling_plan_id';
1701         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_sampling_plan_id';
1702 
1703         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.sampling_plan_id = :wip_vr_sampling_plan_id';
1704         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_sampling_plan_id';
1705       END IF;
1706 
1707       IF p_wip_vr_delete_mark IS NOT NULL
1708       THEN
1709         gmd_outbound_apis_pub.g_wip_vr_delete_mark := p_wip_vr_delete_mark;
1710         main_where_clause := main_where_clause||' AND mwvr.delete_mark >= :wip_vr_delete_mark';
1711         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_delete_mark';
1712 
1713         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.delete_mark >= :wip_vr_delete_mark';
1714         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_delete_mark';
1715       END IF;
1716 
1717       IF p_wip_vr_from_last_update IS NOT NULL
1718       THEN
1719         gmd_outbound_apis_pub.g_wip_vr_from_last_update := p_wip_vr_from_last_update;
1720         main_where_clause := main_where_clause||' AND mwvr.last_update_date >= :wip_vr_from_last_update';
1721         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_from_last_update';
1722 
1723         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.last_update_date >= :wip_vr_from_last_update';
1724         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_from_last_update';
1725       END IF;
1726 
1727       IF p_wip_vr_to_last_update IS NOT NULL
1728       THEN
1729         gmd_outbound_apis_pub.g_wip_vr_to_last_update := p_wip_vr_to_last_update;
1730         main_where_clause := main_where_clause||' AND mwvr.last_update_date <= :wip_vr_to_last_update';
1731         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_vr_to_last_update';
1732 
1733         wip_vr_where_clause := wip_vr_where_clause||' AND wvr.last_update_date <= :wip_vr_to_last_update';
1734         wip_vr_using_clause := wip_vr_using_clause||', gmd_outbound_apis_pub.g_wip_vr_to_last_update';
1735       END IF;
1736 
1737       main_where_clause := main_where_clause||' ) ';
1738 
1739     END IF;
1740 
1741     -- Now sort out the retrieval from GMD_CUSTOMER_SPEC_VRS
1742 
1743     IF p_cust_vr_start_date IS NOT NULL OR p_cust_vr_end_date IS NOT NULL OR p_cust_vr_status IS NOT NULL
1744     OR p_cust_vr_organization_code IS NOT NULL OR p_cust_vr_org_id IS NOT NULL OR p_cust_vr_coa_type IS NOT NULL
1745     OR p_cust_vr_customer IS NOT NULL OR p_cust_vr_customer_id IS NOT NULL OR p_cust_vr_order_number IS NOT NULL
1746     OR p_cust_vr_order_id IS NOT NULL OR p_cust_vr_order_type IS NOT NULL OR p_cust_vr_order_line_no IS NOT NULL
1747     OR p_cust_vr_order_line_id IS NOT NULL OR p_cust_vr_ship_to_location IS NOT NULL
1748     OR p_cust_vr_ship_to_site_id IS NOT NULL OR p_cust_vr_operating_unit IS NOT NULL
1749     OR p_cust_vr_delete_mark    IS NOT NULL OR p_cust_vr_from_last_update IS NOT NULL
1750     OR p_cust_vr_to_last_update IS NOT NULL
1751     THEN
1752       -- Add the table to the list and join to it.
1753 
1754 
1755       main_where_clause := main_where_clause
1756                         ||' AND gs.spec_id IN'
1757                         ||' (SELECT mcvr.spec_id FROM gmd_customer_spec_vrs mcvr'
1758                         ||'  WHERE 1=1';
1759 
1760       IF p_cust_vr_start_date IS NOT NULL
1761       THEN
1762         g_cust_vr_start_date := p_cust_vr_start_date;
1763         main_where_clause := main_where_clause||' AND mcvr.start_date >= :cust_vr_start_date';
1764         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_start_date';
1765 
1766         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.start_date >= :cust_vr_start_date';
1767         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_start_date';
1768       END IF;
1769 
1770       IF p_cust_vr_end_date IS NOT NULL
1771       THEN
1772         g_cust_vr_end_date := p_cust_vr_end_date;
1773         main_where_clause := main_where_clause||' AND mcvr.end_date <= :cust_vr_end_date';
1774         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_end_date';
1775 
1776         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.end_date <= :cust_vr_end_date';
1777         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_end_date';
1778       END IF;
1779 
1780       IF p_cust_vr_status IS NOT NULL
1781       THEN
1782         g_cust_vr_status := p_cust_vr_status;
1783         main_where_clause := main_where_clause||' AND mcvr.spec_vr_status = :cust_vr_status';
1784         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_status';
1785 
1786         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.spec_vr_status = :cust_vr_status';
1787         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_status';
1788       END IF;
1789 
1790       IF p_cust_vr_organization_code IS NOT NULL
1791       THEN
1792         g_cust_vr_organization_code := p_cust_vr_organization_code;
1793         main_where_clause := main_where_clause||' AND mcvr.organization_id = (SELECT organization_id '
1794 	                          ||'FROM mtl_organizations WHERE organization_code = :cust_vr_organization_code)';
1795         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_organization_code';
1796 
1797         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.organization_id = (SELECT organization_id '
1798 	                         ||'FROM mtl_organizations WHERE organization_code = :cust_vr_organization_code)';
1802       IF p_cust_vr_org_id IS NOT NULL
1799         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_organization_code';
1800       END IF;
1801 
1803       THEN
1804         g_cust_vr_org_id := p_cust_vr_org_id;
1805         main_where_clause := main_where_clause||' AND mcvr.org_id = :cust_vr_org_id';
1806         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_org_id';
1807 
1808         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.org_id = :cust_vr_org_id';
1809         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_org_id';
1810       END IF;
1811 
1812       IF p_cust_vr_coa_type IS NOT NULL
1813       THEN
1814         g_cust_vr_coa_type := p_cust_vr_coa_type;
1815         main_where_clause := main_where_clause||' AND mcvr.coa_type = :cust_vr_coa_type';
1816         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_coa_type';
1817 
1818         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.coa_type = :cust_vr_coa_type';
1819         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_coa_type';
1820       END IF;
1821 
1822       IF p_cust_vr_customer_id IS NOT NULL
1823       THEN
1824         g_cust_vr_customer_id := p_cust_vr_customer_id;
1825         main_where_clause := main_where_clause||' AND mcvr.cust_id = :cust_vr_customer_id';
1826         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_customer_id';
1827 
1828         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.cust_id = :cust_vr_customer_id';
1829         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_customer_id';
1830       END IF;
1831 
1832       IF p_cust_vr_customer IS NOT NULL
1833       THEN
1834         g_cust_vr_customer := p_cust_vr_customer;
1835         main_where_clause := main_where_clause||' AND mcvr.cust_id IN'
1836                                               ||' (SELECT hzca.cust_account_id'
1837                                               ||'  FROM hz_parties hzp, hz_cust_accounts_all hzca'
1838                                               ||'  WHERE hzp.party_id = hzca.party_id AND'
1839                                               ||'  UPPER(hzp.party_name)'
1840                                               ||'  LIKE UPPER(:cust_vr_customer)'
1841                                               ||' )';
1842         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_customer';
1843 
1844         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.cust_id IN'
1845                                                     ||' (SELECT hzca.cust_account_id'
1846                                                     ||'  FROM hz_parties hzp, hz_cust_accounts_all hzca'
1847                                                     ||'  WHERE hzp.party_id = hzca.party_id AND'
1848                                                     ||'  UPPER(hzp.party_name)'
1849                                                     ||'  LIKE UPPER(:cust_vr_customer)'
1850                                                     ||' )';
1851         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_customer';
1852       END IF;
1853 
1854       IF p_cust_vr_order_id IS NOT NULL
1855       THEN
1856         g_cust_vr_order_id := p_cust_vr_order_id;
1857         main_where_clause := main_where_clause||' AND mcvr.order_id = :cust_vr_order_id';
1858         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_id';
1859 
1860         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.order_id = :cust_vr_order_id';
1861         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_id';
1862       END IF;
1863 
1864       IF p_cust_vr_order_number IS NOT NULL
1865       THEN
1866         g_cust_vr_order_number := p_cust_vr_order_number;
1867         main_where_clause := main_where_clause||' AND mcvr.order_id = (select header_id '
1868                                               ||' from oe_order_headers_all'
1869                                               ||' where order_number = :cust_vr_order_number)';
1870         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_number';
1871 
1872         cust_vr_where_clause := cust_vr_where_clause||' AND oeh.order_number = :cust_vr_order_number';
1873         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_number';
1874       END IF;
1875 
1876 
1877       IF p_cust_vr_delete_mark IS NOT NULL
1878       THEN
1879         g_cust_vr_delete_mark := p_cust_vr_delete_mark;
1880         main_where_clause := main_where_clause||' AND mcvr.delete_mark = :cust_vr_delete_mark';
1881         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_delete_mark';
1882 
1883         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.delete_mark = :cust_vr_delete_mark';
1884         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_delete_mark';
1885       END IF;
1886 
1887       IF p_cust_vr_from_last_update IS NOT NULL
1888       THEN
1889         g_cust_vr_from_last_update := p_cust_vr_from_last_update;
1890         main_where_clause := main_where_clause||' AND mcvr.last_update_date >= :cust_vr_from_last_update';
1891         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_from_last_update';
1892 
1893         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.last_update_date >= :cust_vr_from_last_update';
1894         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_from_last_update';
1895       END IF;
1896 
1897       IF p_cust_vr_to_last_update IS NOT NULL
1898       THEN
1899         g_cust_vr_to_last_update := p_cust_vr_to_last_update;
1900         main_where_clause := main_where_clause||' AND mcvr.last_update_date <= :cust_vr_to_last_update';
1901         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_to_last_update';
1902 
1906 
1903         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.last_update_date <= :cust_vr_to_last_update';
1904         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_to_last_update';
1905       END IF;
1907       IF p_cust_vr_order_line_id IS NOT NULL
1908       THEN
1909         g_cust_vr_order_line_id := p_cust_vr_order_line_id;
1910         main_where_clause := main_where_clause||' AND mcvr.order_line_id = :cust_vr_order_line_id';
1911         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_line_id';
1912 
1913         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.order_line_id = :cust_vr_order_line_id';
1914         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_line_id';
1915       END IF;
1916 
1917       IF p_cust_vr_order_line_no IS NOT NULL
1918       THEN
1919         g_cust_vr_order_line_no := p_cust_vr_order_line_no;
1920         main_where_clause := main_where_clause||' AND mcvr.order_line = :cust_vr_order_line_no';
1921         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_line_no';
1922 
1923         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.order_line = :cust_vr_order_line_no';
1924         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_line_no';
1925       END IF;
1926 
1927       IF p_cust_vr_order_type IS NOT NULL
1928       THEN
1929         g_cust_vr_order_type := p_cust_vr_order_type;
1930         main_where_clause := main_where_clause||' AND  mcvr.order_id IN (select header_id '
1931                                               ||' FROM oe_order_headers_all h, oe_transaction_types_all t'
1932                                               ||' WHERE h.order_type_id = t.transaction_type_id'
1933                                               ||' AND   t.transaction_type_code = :cust_vr_order_type)';
1934         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_type';
1935 
1936         cust_vr_table_list := cust_vr_table_list||', oe_transaction_types_all tta';
1937         cust_vr_where_clause := cust_vr_where_clause||' AND oeh.order_type_id = tta.transaction_type_id'
1938                                                     ||' AND tta.transaction_type_code = :cust_vr_order_type';
1939         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_order_type';
1940       END IF;
1941 
1942       IF p_cust_vr_ship_to_site_id IS NOT NULL
1943       THEN
1944         g_cust_vr_ship_to_site_id := p_cust_vr_ship_to_site_id;
1945         main_where_clause := main_where_clause||' AND mcvr.ship_to_site_id = :cust_vr_ship_to_site_id';
1946         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_ship_to_site_id';
1947 
1948         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.ship_to_site_id = :cust_vr_ship_to_site_id';
1949         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_ship_to_site_id';
1950       END IF;
1951 
1952       IF p_cust_vr_ship_to_location IS NOT NULL
1953       THEN
1954         g_cust_vr_ship_to_location := p_cust_vr_ship_to_location;
1955         main_where_clause := main_where_clause||' AND mcvr.ship_to_site_use_id = '
1956                                               ||' (SELECT site_use_id'
1957                                               ||'  FROM   hz_cust_site_uses_all'
1958                                               ||'  WHERE  location = :cust_vr_ship_to_location)';
1959         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_ship_to_location';
1960 
1961         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.ship_to_site_use_id = '
1962                                                     ||'   (SELECT site_use_id'
1963                                                     ||'    FROM   hz_cust_site_uses_all'
1964                                                     ||'    WHERE  location = :cust_vr_ship_to_location)';
1965         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_ship_to_location';
1966       END IF;
1967 
1968       IF p_cust_vr_operating_unit IS NOT NULL
1969       THEN
1970         g_cust_vr_operating_unit := p_cust_vr_operating_unit;
1971         main_where_clause := main_where_clause||' AND mcvr.org_id  = '
1972                                               ||' (SELECT organization_id'
1973                                               ||'  FROM   hr_operating_units'
1974                                               ||'  WHERE  name = :cust_vr_operating_unit)';
1975         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_cust_vr_operating_unit';
1976 
1977         cust_vr_where_clause := cust_vr_where_clause||' AND cvr.org_id  = '
1978                                               ||' (SELECT organization_id'
1979                                               ||'  FROM   hr_operating_units'
1980                                               ||'  WHERE  name = :cust_vr_operating_unit)';
1981         cust_vr_using_clause := cust_vr_using_clause||', gmd_outbound_apis_pub.g_cust_vr_operating_unit';
1982 
1983       END IF;
1984 
1985       main_where_clause := main_where_clause||' ) ';
1986 
1987     END IF;
1988 
1989     -- Sort out the GMD_SUPPLIER_SPEC_VRS parameters
1990 
1991     IF p_supl_vr_start_date IS NOT NULL OR p_supl_vr_end_date IS NOT NULL OR p_supl_vr_status IS NOT NULL
1992     OR p_supl_vr_organization_code IS NOT NULL OR p_supl_vr_org_id IS NOT NULL OR p_supl_vr_coa_type IS NOT NULL
1993     OR p_supl_vr_supplier IS NOT NULL OR p_supl_vr_supplier_id IS NOT NULL OR p_supl_vr_po_number IS NOT NULL
1994     OR p_supl_vr_po_id IS NOT NULL OR p_supl_vr_po_line_no IS NOT NULL OR p_supl_vr_po_line_id IS NOT NULL
1995     OR p_supl_vr_supplier_site IS NOT NULL OR p_supl_vr_supplier_site_id IS NOT NULL
1996     OR p_supl_vr_operating_unit IS NOT NULL OR p_supl_vr_delete_mark IS NOT NULL
1997     OR p_supl_vr_from_last_update IS NOT NULL OR p_supl_vr_to_last_update IS NOT NULL
1998     THEN
1999 
2000       -- Include the table in the list, and join to it.
2001 
2002 
2006                            ||'   WHERE  msvr.supplier_id = mpv.vendor_id';
2003       main_where_clause := main_where_clause
2004                            ||' AND gs.spec_id IN'
2005                            ||' ( SELECT spec_id fROM gmd_supplier_spec_vrs msvr, po_vendors mpv'
2007 
2008       IF p_supl_vr_start_date IS NOT NULL
2009       THEN
2010         g_supl_vr_start_date := p_supl_vr_start_date;
2011         main_where_clause := main_where_clause||' AND msvr.start_date >= :supl_vr_start_date';
2012         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_start_date';
2013 
2014         supl_vr_where_clause := supl_vr_where_clause||' AND svr.start_date >= :supl_vr_start_date';
2015         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_start_date';
2016       END IF;
2017 
2018       IF p_supl_vr_end_date IS NOT NULL
2019       THEN
2020         g_supl_vr_end_date := p_supl_vr_end_date;
2021         main_where_clause := main_where_clause||' AND msvr.end_date <= :supl_vr_end_date';
2022         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_end_date';
2023 
2024         supl_vr_where_clause := supl_vr_where_clause||' AND svr.end_date >= :supl_vr_end_date';
2025         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_end_date';
2026       END IF;
2027 
2028       IF p_supl_vr_status IS NOT NULL
2029       THEN
2030         g_supl_vr_status := p_supl_vr_status;
2031         main_where_clause := main_where_clause||' AND msvr.spec_vr_status = :supl_vr_status';
2032         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_status';
2033 
2034         supl_vr_where_clause := supl_vr_where_clause||' AND svr.status = :supl_vr_status';
2035         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_status';
2036       END IF;
2037 
2038       IF p_supl_vr_organization_code IS NOT NULL
2039       THEN
2040         g_supl_vr_organization_code := p_supl_vr_organization_code;
2041         main_where_clause := main_where_clause||' AND msvr.organization_id = (SELECT organization_id '
2042 	                     ||'FROM mtl_organizations WHERE organization_code = :supl_vr_organization_code)';
2043         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_organization_code';
2044 
2045         supl_vr_where_clause := supl_vr_where_clause||' AND svr.organization_id = (SELECT organization_id '
2046 	                     ||'FROM mtl_organizations WHERE organization_code = :supl_vr_organization_code)';
2047         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_organization_code';
2048       END IF;
2049 
2050       IF p_supl_vr_org_id IS NOT NULL
2051       THEN
2052         g_supl_vr_org_id := p_supl_vr_org_id;
2053         main_where_clause := main_where_clause||' AND msvr.org_id = :supl_vr_org_id';
2054         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_org_id';
2055 
2056         supl_vr_where_clause := supl_vr_where_clause||' AND svr.org_id = :supl_vr_org_id';
2057         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_org_id';
2058       END IF;
2059 
2060       IF p_supl_vr_coa_type IS NOT NULL
2061       THEN
2062         g_supl_vr_coa_type := p_supl_vr_coa_type;
2063         main_where_clause := main_where_clause||' AND msvr.coa_type = :supl_vr_coa_type';
2064         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_coa_type';
2065 
2066         supl_vr_where_clause := supl_vr_where_clause||' AND svr.coa_type = :supl_vr_coa_type';
2067         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_coa_type';
2068       END IF;
2069 
2070       IF p_supl_vr_coa_type IS NOT NULL
2071       THEN
2072         g_supl_vr_coa_type := p_supl_vr_coa_type;
2073         main_where_clause := main_where_clause||' AND msvr.coa_type = :supl_vr_coa_type';
2074         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_coa_type';
2075 
2076         supl_vr_where_clause := supl_vr_where_clause||' AND svr.coa_type = :supl_vr_coa_type';
2077         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_coa_type';
2078       END IF;
2079 
2080       IF p_supl_vr_supplier_id IS NOT NULL
2081       THEN
2082         g_supl_vr_supplier_id := p_supl_vr_supplier_id;
2083         main_where_clause := main_where_clause||' AND msvr.supplier_id = :supl_vr_supplier_id';
2084         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_supplier_id';
2085 
2086         supl_vr_where_clause := supl_vr_where_clause||' AND svr.supplier_id = :supl_vr_supplier_id';
2087         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_supplier_id';
2088       END IF;
2089 
2090       IF p_supl_vr_supplier IS NOT NULL
2091       THEN
2092         g_supl_vr_supplier := p_supl_vr_supplier;
2093         main_where_clause := main_where_clause||' AND mpv.segment1 = :supl_vr_supplier';
2094         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_supplier';
2095 
2096         supl_vr_where_clause := supl_vr_where_clause||' AND v.segment1 = :supl_vr_supplier';
2097         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_supplier';
2098       END IF;
2099 
2100       IF p_supl_vr_po_number IS NOT NULL
2101       THEN
2102         g_supl_vr_po_number := p_supl_vr_po_number;
2103         main_where_clause := main_where_clause||' AND  msvr.po_line_id IN'
2104                                               ||' (SELECT pla.po_line_id FROM po_headers_all pha, po_lines_all pla'
2105                                               ||'  WHERE  pha.segment1 = :supl_vr_po_number'
2106                                               ||'  AND    pha.po_header_id = pla.po_header_id)';
2107         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_po_number';
2108 
2112                                               ||'  AND    pha.po_header_id = pla.po_header_id)';
2109         supl_vr_where_clause := supl_vr_where_clause||' AND  svr.po_line_id IN'
2110                                               ||' (SELECT pla.po_line_id FROM po_headers_all pha, po_lines_all pla'
2111                                               ||'  WHERE  pha.segment1 = :supl_vr_po_number'
2113         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_po_number';
2114       END IF;
2115 
2116       IF p_supl_vr_po_id IS NOT NULL
2117       THEN
2118         g_supl_vr_po_id := p_supl_vr_po_id;
2119         main_where_clause := main_where_clause||' AND  msvr.po_line_id IN'
2120                                               ||' (SELECT po_line_id FROM po_lines_all'
2121                                               ||'  WHERE  po_header_id = :supl_vr_po_id)';
2122         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_po_id';
2123 
2124         supl_vr_where_clause := supl_vr_where_clause||' AND  svr.po_line_id IN'
2125                                               ||' (SELECT po_line_id FROM po_lines_all'
2126                                               ||'  WHERE  po_header_id = :supl_vr_po_id)';
2127         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_po_id';
2128       END IF;
2129 
2130       IF p_supl_vr_po_line_no IS NOT NULL
2131       THEN
2132         g_supl_vr_po_line_no := p_supl_vr_po_line_no;
2133         main_where_clause := main_where_clause||' AND  msvr.po_line_id IN'
2134                                               ||' (SELECT po_line_id FROM po_lines_all'
2135                                               ||'  WHERE  line_num = :supl_vr_po_line_no)';
2136         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_po_line_no';
2137 
2138         supl_vr_where_clause := supl_vr_where_clause||' AND  svr.po_line_id IN'
2139                                               ||' (SELECT po_line_id FROM po_lines_all'
2140                                               ||'  WHERE  line_num = :supl_vr_po_line_no)';
2141         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_po_line_no';
2142       END IF;
2143 
2144       IF p_supl_vr_po_line_id IS NOT NULL
2145       THEN
2146         g_supl_vr_po_line_id := p_supl_vr_po_line_id;
2147         main_where_clause := main_where_clause||' AND  msvr.po_line_id =:supl_vr_po_line_id';
2148         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_po_line_id';
2149 
2150         supl_vr_where_clause := supl_vr_where_clause||' AND  svr.po_line_id = :supl_vr_po_line_id';
2151         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_po_line_id';
2152       END IF;
2153 
2154       IF p_supl_vr_supplier_site_id IS NOT NULL
2155       THEN
2156         g_supl_vr_supplier_site_id := p_supl_vr_supplier_site_id;
2157         main_where_clause := main_where_clause||' AND  msvr.supplier_site_id =:supl_vr_supplier_site_id';
2158         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_supplier_site_id';
2159 
2160         supl_vr_where_clause := supl_vr_where_clause||' AND  svr.supplier_site_id = :supl_vr_supplier_site_id';
2161         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_supplier_site_id';
2162       END IF;
2163 
2164       IF p_supl_vr_supplier_site IS NOT NULL
2165       THEN
2166         g_supl_vr_supplier_site := p_supl_vr_supplier_site;
2167         main_where_clause := main_where_clause||' AND  msvr.supplier_site_id = '
2168                                               ||' (SELECT vendor_site_id FROM po_vendor_sites_all'
2169                                               ||'  WHERE  vendor_site_code = :supl_vr_supplier_site';
2170         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_supplier_site';
2171 
2172         supl_vr_where_clause := supl_vr_where_clause||' AND  svr.supplier_site_id = '
2173                                               ||' (SELECT vendor_site_id FROM po_vendor_sites_all'
2174                                               ||'  WHERE  vendor_site_code = :supl_vr_supplier_site';
2175         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_supplier_site';
2176       END IF;
2177 
2178       IF p_supl_vr_operating_unit IS NOT NULL
2179       THEN
2180         g_supl_vr_operating_unit := p_supl_vr_operating_unit;
2181         main_where_clause := main_where_clause||' AND msvr.org_id  = '
2182                                               ||' (SELECT organization_id'
2183                                               ||'  FROM   hr_operating_units'
2184                                               ||'  WHERE  name = :supl_vr_operating_unit)';
2185         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_operating_unit';
2186 
2187         supl_vr_where_clause := supl_vr_where_clause||' AND svr.org_id  = '
2188                                               ||' (SELECT organization_id'
2189                                               ||'  FROM   hr_operating_units'
2190                                               ||'  WHERE  name = :supl_vr_operating_unit)';
2191         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_operating_unit';
2192 
2193       END IF;
2194 
2195       IF p_supl_vr_from_last_update IS NOT NULL
2196       THEN
2197         g_supl_vr_from_last_update := p_supl_vr_from_last_update;
2198         main_where_clause := main_where_clause||' AND msvr.last_update_date >= :supl_vr_from_last_update';
2199         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_from_last_update';
2200 
2201         supl_vr_where_clause := supl_vr_where_clause||' AND svr.last_update_date >= :supl_vr_from_last_update';
2202         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_from_last_update';
2203       END IF;
2204 
2205       IF p_supl_vr_to_last_update IS NOT NULL
2206       THEN
2207         g_supl_vr_to_last_update := p_supl_vr_to_last_update;
2211         supl_vr_where_clause := supl_vr_where_clause||' AND svr.last_update_date <= :supl_vr_to_last_update';
2208         main_where_clause := main_where_clause||' AND msvr.last_update_date <= :supl_vr_to_last_update';
2209         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_to_last_update';
2210 
2212         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_to_last_update';
2213       END IF;
2214 
2215       IF p_supl_vr_delete_mark IS NOT NULL
2216       THEN
2217         g_supl_vr_delete_mark := p_supl_vr_delete_mark;
2218         main_where_clause := main_where_clause||' AND  msvr.delete_mark =:supl_vr_delete_mark';
2219         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supl_vr_delete_mark';
2220 
2221         supl_vr_where_clause := supl_vr_where_clause||' AND  svr.delete_mark = :supl_vr_delete_mark';
2222         supl_vr_using_clause := supl_vr_using_clause||', gmd_outbound_apis_pub.g_supl_vr_delete_mark';
2223       END IF;
2224 
2225       main_where_clause := main_where_clause||' ) ';
2226 
2227     END IF;
2228 
2229     -- Sort out the GMD_INVENTORY_SPEC_VRS table's needs
2230 
2231     IF p_inv_vr_start_date IS NOT NULL OR p_inv_vr_end_date IS NOT NULL OR p_inv_vr_status IS NOT NULL
2232     OR p_inv_vr_organization_code IS NOT NULL OR p_inv_vr_coa_type IS NOT NULL
2233     OR p_inv_vr_item_number IS NOT NULL OR p_inv_vr_inventory_item_id IS NOT NULL OR p_inv_vr_parent_lot_number IS NOT NULL
2234     OR p_inv_vr_lot_number IS NOT NULL OR p_inv_vr_subinventory IS NOT NULL OR p_inv_vr_locator IS NOT NULL
2235     OR p_inv_vr_locator_id IS NOT NULL OR p_inv_vr_sampling_plan IS NOT NULL OR p_inv_vr_sampling_plan_id IS NOT NULL
2236     OR p_inv_vr_delete_mark IS NOT NULL OR p_inv_vr_from_last_update IS NOT NULL OR p_inv_vr_to_last_update IS NOT NULL
2237     THEN
2238       -- Include the table in the list, and join to it.
2239 
2240       main_where_clause := main_where_clause
2241                         ||' AND gs.spec_id IN'
2242                         ||' (SELECT mivr.spec_id FROM gmd_inventory_spec_vrs mivr'
2243                         ||'  WHERE 1=1';
2244 
2245 
2246       IF p_inv_vr_start_date IS NOT NULL
2247       THEN
2248         g_inv_vr_start_date := p_inv_vr_start_date;
2249         main_where_clause := main_where_clause||' AND mivr.start_date >= :inv_vr_start_date';
2250         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_start_date';
2251 
2252         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.start_date >= :inv_vr_start_date';
2253         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_start_date';
2254       END IF;
2255 
2256       IF p_inv_vr_end_date IS NOT NULL
2257       THEN
2258         g_inv_vr_end_date := p_inv_vr_end_date;
2259         main_where_clause := main_where_clause||' AND mivr.end_date <= :inv_vr_end_date';
2260         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_end_date';
2261 
2262         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.end_date <= :inv_vr_end_date';
2263         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_end_date';
2264       END IF;
2265 
2266       IF p_inv_vr_status IS NOT NULL
2267       THEN
2268         g_inv_vr_status := p_inv_vr_status;
2269         main_where_clause := main_where_clause||' AND mivr.spec_vr_status = :inv_vr_status';
2270         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_status';
2271 
2272         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.status = :inv_vr_status';
2273         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_status';
2274       END IF;
2275 
2276       IF p_inv_vr_organization_code IS NOT NULL
2277       THEN
2278         g_inv_vr_organization_code := p_inv_vr_organization_code;
2279         main_where_clause := main_where_clause||' AND mivr.organization_id = (SELECT organization_id '
2280      	                ||'FROM mtl_organizations WHERE organization_code = :inv_vr_organization_code)';
2281         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_organization_code';
2282 
2283         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.organization_id = (SELECT organization_id '
2284      	                ||'FROM mtl_organizations WHERE organization_code = :inv_vr_organization_code)';
2285         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_organization_code';
2286       END IF;
2287 
2288       IF p_inv_vr_inventory_item_id IS NOT NULL
2289       THEN
2290         g_inv_vr_inventory_item_id := p_inv_vr_inventory_item_id;
2291         main_where_clause := main_where_clause||' AND mivr.lot_number IN'
2292                                               ||' (SELECT lot_number FROM mtl_lot_numbers'
2293                                               ||'  WHERE inventory_item_id = :inv_vr_inventory_item_id)';
2294         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_inventory_item_id';
2295 
2296         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.lot_number IN'
2297                                               ||' (SELECT lot_number FROM mtl_lot_numbers'
2298                                               ||'  WHERE inventory_item_id = :inv_vr_inventory_item_id)';
2299         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_inventory_item_id';
2300       END IF;
2301 
2302       IF p_inv_vr_item_number IS NOT NULL
2303       THEN
2304         g_inv_vr_item_number := p_inv_vr_item_number;
2305         main_where_clause := main_where_clause||' AND mivr.lot_number IN'
2306                                               ||' (SELECT l.lot_number FROM mtl_lot_numbers l'
2307                                               ||'  WHERE l.organization_id IN (SELECT organization_id FROM'
2308                                               ||'  mtl_system_items_b_kfv WHERE concatenated_segments = :inv_vr_item_number'
2309                                               ||'  AND inventory_item_id = l.inventory_item_id))';
2313         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.lot_number IN'
2310 
2311         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_item_number';
2312 
2314                                                ||' (SELECT l.lot_number FROM mtl_lot_numbers l'
2315                                               ||'  WHERE l.organization_id IN (SELECT organization_id FROM'
2316                                               ||'  mtl_system_items_b_kfv WHERE concatenated_segments = :inv_vr_item_number'
2317                                               ||'  AND inventory_item_id = l.inventory_item_id))';
2318         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_item_number';
2319       END IF;
2320 
2321       IF p_inv_vr_parent_lot_number IS NOT NULL
2322       THEN
2323         g_inv_vr_parent_lot_number := p_inv_vr_parent_lot_number;
2324         main_where_clause := main_where_clause||' AND mivr.parent_lot_number = :inv_vr_parent_lot_number';
2325         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_parent_lot_number';
2326 
2327         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.parent_lot_number = :inv_vr_parent_lot_number';
2328         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_parent_lot_number';
2329       END IF;
2330 
2331       IF p_inv_vr_lot_number IS NOT NULL
2332       THEN
2333         g_inv_vr_lot_number := p_inv_vr_lot_number;
2334         main_where_clause := main_where_clause||' AND mivr.lot_number = :inv_vr_lot_number';
2335         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_lot_number';
2336 
2337         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.lot_number = :inv_vr_lot_number';
2338         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_lot_number';
2339       END IF;
2340 
2341       IF p_inv_vr_subinventory IS NOT NULL
2342       THEN
2343         g_inv_vr_subinventory := p_inv_vr_subinventory;
2344         main_where_clause := main_where_clause||' AND mivr.subinventory = :inv_vr_subinventory';
2345         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_subinventory';
2346 
2347         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.subinventory = :inv_vr_subinventory';
2348         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_subinventory';
2349       END IF;
2350 
2351       IF p_inv_vr_locator IS NOT NULL
2352       THEN
2353         g_inv_vr_locator := p_inv_vr_locator;
2354         main_where_clause := main_where_clause||' AND mivr.locator_id = (SELECT inventory_location_id '
2355 	                        ||'FROM mtl_item_locations_kfv WHERE concatenated_segments = :inv_vr_locator '
2356 				||'AND organization_id = mivr.organization_id)';
2357         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_locator';
2358 
2359         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.locator_id = (SELECT inventory_location_id '
2360 	                        ||'FROM mtl_item_locations_kfv WHERE concatenated_segments = :inv_vr_locator '
2361 				||'AND organization_id = ivr.organization_id)';
2362         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_locator';
2363       END IF;
2364 
2365       IF p_inv_vr_locator_id IS NOT NULL
2366       THEN
2367         g_inv_vr_locator_id := p_inv_vr_locator_id;
2368         main_where_clause := main_where_clause||' AND mivr.locator_id = :inv_vr_locator_id';
2369         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_locator_id';
2370 
2371         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.locator_id = :inv_vr_locator_id';
2372         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_locator_id';
2373       END IF;
2374 
2375       IF p_inv_vr_sampling_plan_id IS NOT NULL
2376       THEN
2377         g_inv_vr_sampling_plan_id := p_inv_vr_sampling_plan_id;
2378         main_where_clause := main_where_clause||' AND mivr.sampling_plan_id = :inv_vr_sampling_plan_id';
2379         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_sampling_plan_id';
2380 
2381         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.sampling_plan_id = :inv_vr_sampling_plan_id';
2382         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_sampling_plan_id';
2383       END IF;
2384 
2385       IF p_inv_vr_from_last_update IS NOT NULL
2386       THEN
2387         g_inv_vr_from_last_update := p_inv_vr_from_last_update;
2388         main_where_clause := main_where_clause||' AND mivr.last_update_date >= :inv_vr_from_last_update';
2389         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_from_last_update';
2390 
2391         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.last_update_date >= :inv_vr_from_last_update';
2392         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_from_last_update';
2393       END IF;
2394 
2395       IF p_inv_vr_to_last_update IS NOT NULL
2396       THEN
2397         g_inv_vr_to_last_update := p_inv_vr_to_last_update;
2398         main_where_clause := main_where_clause||' AND mivr.last_update_date <= :inv_vr_to_last_update';
2399         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_to_last_update';
2400 
2401         inv_vr_where_clause := inv_vr_where_clause||' AND ivr.last_update_date <= :inv_vr_to_last_update';
2402         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_to_last_update';
2403       END IF;
2404 
2405       IF p_inv_vr_delete_mark IS NOT NULL
2406       THEN
2407         g_inv_vr_delete_mark := p_inv_vr_delete_mark;
2408         main_where_clause := main_where_clause||' AND  mivr.delete_mark =:inv_vr_delete_mark';
2409         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inv_vr_delete_mark';
2410 
2411         inv_vr_where_clause := inv_vr_where_clause||' AND  ivr.delete_mark = :inv_vr_delete_mark';
2415       main_where_clause := main_where_clause||' ) ';
2412         inv_vr_using_clause := inv_vr_using_clause||', gmd_outbound_apis_pub.g_inv_vr_delete_mark';
2413       END IF;
2414 
2416 
2417    END IF;
2418 
2419     -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
2420 
2421     -- Sort out the GMD_MONITORING_SPEC_VRS table's needs
2422 
2423     IF p_mon_vr_status                IS NOT NULL OR
2424        p_mon_vr_rule_type             IS NOT NULL OR
2425        p_mon_vr_lct_organization_code IS NOT NULL OR
2426        p_mon_vr_subinventory          IS NOT NULL OR
2427        p_mon_vr_locator               IS NOT NULL OR
2428        p_mon_vr_locator_id            IS NOT NULL OR
2429        p_mon_vr_rsr_organization_code IS NOT NULL OR
2430        p_mon_vr_resources             IS NOT NULL OR
2431        p_mon_vr_resource_instance_id  IS NOT NULL OR
2432        p_mon_vr_sampling_plan_id      IS NOT NULL OR
2433        p_mon_vr_start_date            IS NOT NULL OR
2434        p_mon_vr_end_date              IS NOT NULL OR
2435        p_mon_vr_from_last_update_date IS NOT NULL OR
2436        p_mon_vr_to_last_update_date   IS NOT NULL OR
2437        p_mon_vr_delete_mark           IS NOT NULL
2438     THEN
2439       -- Include the table in the list, and join to it.
2440 
2441       main_where_clause := main_where_clause
2442                         ||' AND gs.spec_id IN'
2443                         ||' (SELECT mmvr.spec_id FROM gmd_monitoring_spec_vrs mmvr'
2444                         ||'  WHERE 1=1';
2445 
2446 
2447       IF p_mon_vr_start_date IS NOT NULL
2448       THEN
2449         g_mon_vr_start_date := p_mon_vr_start_date;
2450         main_where_clause := main_where_clause||' AND mmvr.start_date >= :mon_vr_start_date';
2451         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_start_date';
2452 
2453         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.start_date >= :mon_vr_start_date';
2454         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_start_date';
2455       END IF;
2456 
2457       IF p_mon_vr_end_date IS NOT NULL
2458       THEN
2459         g_mon_vr_end_date := p_mon_vr_end_date;
2460         main_where_clause := main_where_clause||' AND mmvr.end_date <= :mon_vr_end_date';
2461         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_end_date';
2462 
2463         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.end_date <= :mon_vr_end_date';
2464         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_end_date';
2465       END IF;
2466 
2467       IF p_mon_vr_status IS NOT NULL
2468       THEN
2469         g_mon_vr_status := p_mon_vr_status;
2470         main_where_clause := main_where_clause||' AND mmvr.spec_vr_status = :mon_vr_status';
2471         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_status';
2472 
2473         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.spec_vr_status = :mon_vr_status';
2474         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_status';
2475       END IF;
2476 
2477       IF p_mon_vr_rule_type IS NOT NULL
2478       THEN
2479         g_mon_vr_rule_type := p_mon_vr_rule_type;
2480         main_where_clause := main_where_clause||' AND mmvr.rule_type = :mon_vr_rule_type';
2481         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_rule_type';
2482 
2483         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.rule_type = :mon_vr_rule_type';
2484         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_rule_type';
2485       END IF;
2486 
2487       IF p_mon_vr_lct_organization_code IS NOT NULL
2488       THEN
2489         g_mon_vr_lct_organization_code := p_mon_vr_lct_organization_code;
2490         main_where_clause := main_where_clause||' AND mmvr.locator_organization_id = (SELECT organization_id'
2491                                  ||' FROM mtl_organizations WHERE organization_code = :mon_vr_lct_organization_code)';
2492         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_lct_organization_code';
2493 
2494         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.locator_organization_id = (SELECT organization_id'
2495                                  ||' FROM mtl_organizations WHERE organization_code = :mon_vr_lct_organization_code)';
2496         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_lct_organization_code';
2497       END IF;
2498 
2499       IF p_mon_vr_subinventory IS NOT NULL
2500       THEN
2501         g_mon_vr_subinventory := p_mon_vr_subinventory;
2502         main_where_clause := main_where_clause||' AND mmvr.subinventory = :mon_vr_subinventory';
2503         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_subinventory';
2504 
2505         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.whse_code = :mon_vr_subinventory';
2506         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_subinventory';
2507       END IF;
2508 
2509       IF p_mon_vr_locator_id IS NOT NULL
2510       THEN
2511         g_mon_vr_locator_id := p_mon_vr_locator_id;
2512         main_where_clause := main_where_clause||' AND mmvr.locator_id = :mon_vr_locator_id';
2513         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_locator_id';
2514 
2515         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.locator_id = :mon_vr_locator_id';
2516         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_locator_id';
2517       END IF;
2518 
2519       IF p_mon_vr_locator IS NOT NULL
2520       THEN
2521         g_mon_vr_locator := p_mon_vr_locator;
2522         main_where_clause := main_where_clause||' AND mmvr.locator_id = (SELECT inventory_location_id'
2523                                               ||' FROM mtl_item_locations_kfv WHERE'
2524                                               ||' concatenated_segments = :mon_vr_locator'
2528         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.locator_id = (SELECT inventory_location_id'
2525                                               ||' AND organization_id = mmvr.locator_organization_id)';
2526         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_locator';
2527 
2529                                               ||' FROM mtl_item_locations_kfv WHERE'
2530                                               ||' concatenated_segments = :mon_vr_locator'
2531                                               ||' AND organization_id = mvr.locator_organization_id)';
2532         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_locator';
2533       END IF;
2534 
2535       IF p_mon_vr_rsr_organization_code IS NOT NULL
2536       THEN
2537         g_mon_vr_rsr_organization_code := p_mon_vr_rsr_organization_code;
2538         main_where_clause := main_where_clause||' AND mmvr.resource_organization_id = (SELECT organization_id'
2539                                     ||' FROM mtl_organizations WHERE organization_code = :mon_vr_rsr_organization_code)';
2540         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_rsr_organization_code';
2541 
2542         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.resource_organization_id = (SELECT organization_id'
2543                                     ||' FROM mtl_organizations WHERE organization_code = :mon_vr_rsr_organization_code)';
2544         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_rsr_organization_code';
2545       END IF;
2546 
2547       IF p_mon_vr_resources IS NOT NULL
2548       THEN
2549         g_mon_vr_resources := p_mon_vr_resources;
2550         main_where_clause := main_where_clause||' AND mmvr.resources = :mon_vr_resources';
2551         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_resources';
2552 
2553         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.resources = :mon_vr_resources';
2554         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_resources';
2555       END IF;
2556 
2557       IF p_mon_vr_resource_instance_id IS NOT NULL
2558       THEN
2559         g_mon_vr_resource_instance_id := p_mon_vr_resource_instance_id;
2560         main_where_clause := main_where_clause||' AND mmvr.resource_instance_id = :mon_vr_resource_instance_id';
2561         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_resource_instance_id';
2562 
2563         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.resource_instance_id = :mon_vr_resource_instance_id';
2564         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_resource_instance_id';
2565       END IF;
2566 
2567       IF p_mon_vr_sampling_plan_id IS NOT NULL
2568       THEN
2569         g_mon_vr_sampling_plan_id := p_mon_vr_sampling_plan_id;
2570         main_where_clause := main_where_clause||' AND mmvr.sampling_plan_id = :mon_vr_sampling_plan_id';
2571         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_sampling_plan_id';
2572 
2573         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.sampling_plan_id = :mon_vr_sampling_plan_id';
2574         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_sampling_plan_id';
2575       END IF;
2576 
2577       IF p_mon_vr_from_last_update_date IS NOT NULL
2578       THEN
2579         g_mon_vr_from_last_update_date := p_mon_vr_from_last_update_date;
2580         main_where_clause := main_where_clause||' AND mmvr.last_update_date >= :mon_vr_from_last_update_date';
2581         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_from_last_update_date';
2582 
2583         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.last_update_date >= :mon_vr_from_last_update_date';
2584         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_from_last_update_date';
2585       END IF;
2586 
2587       IF p_mon_vr_to_last_update_date IS NOT NULL
2588       THEN
2589         g_mon_vr_to_last_update_date := p_mon_vr_to_last_update_date;
2590         main_where_clause := main_where_clause||' AND mmvr.last_update_date <= :mon_vr_to_last_update_date';
2591         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_to_last_update_date';
2592 
2593         mon_vr_where_clause := mon_vr_where_clause||' AND mvr.last_update_date <= :mon_vr_to_last_update_date';
2594         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_to_last_update_date';
2595       END IF;
2596 
2597       IF p_mon_vr_delete_mark IS NOT NULL
2598       THEN
2599         g_mon_vr_delete_mark := p_mon_vr_delete_mark;
2600         main_where_clause := main_where_clause||' AND  mmvr.delete_mark =:mon_vr_delete_mark';
2601         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_mon_vr_delete_mark';
2602 
2603         mon_vr_where_clause := mon_vr_where_clause||' AND  mvr.delete_mark = :mon_vr_delete_mark';
2604         mon_vr_using_clause := mon_vr_using_clause||', gmd_outbound_apis_pub.g_mon_vr_delete_mark';
2605       END IF;
2606 
2607       main_where_clause := main_where_clause||' ) ';
2608 
2609    END IF;
2610   -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
2611 
2612 
2613    sql_statement := 'SELECT system.gmd_specification_rec_type'   -- 5284247
2614                   ||'('||main_column_list
2615                   ||', CAST'
2616                   ||'  ( MULTISET'
2617                   ||'    ( SELECT '  ||spec_test_column_list
2618                   ||'        FROM   '||spec_test_table_list
2619                   ||'        WHERE  '||spec_test_where_clause
2620                   ||'    ) AS system.gmd_spec_tests_tab_type' -- 5284242
2621                   ||'  )'
2622                   ||', CAST'
2623                   ||'  ( MULTISET'
2624                   ||'    ( SELECT '  ||cust_vr_column_list
2625                   ||'        FROM   '||cust_vr_table_list
2626                   ||'        WHERE  '||cust_vr_where_clause
2630                   ||'  ( MULTISET'
2627                   ||'    ) AS system.gmd_cust_spec_vrs_tab_type' -- 5284242
2628                   ||'  )'
2629                   ||', CAST'
2631                   ||'    ( SELECT '  ||wip_vr_column_list
2632                   ||'        FROM   '||wip_vr_table_list
2633                   ||'        WHERE  '||wip_vr_where_clause
2634                   ||'    ) AS system.gmd_wip_spec_vrs_tab_type' -- 5284242
2635                   ||'  )'
2636                   ||', CAST'
2637                   ||'  ( MULTISET'
2638                   ||'    ( SELECT '  ||supl_vr_column_list
2639                   ||'        FROM   '||supl_vr_table_list
2640                   ||'        WHERE  '||supl_vr_where_clause
2641                   ||'    ) AS system.gmd_supl_spec_vrs_tab_type' -- 5284242
2642                   ||'  )'
2643                   ||', CAST'
2644                   ||'  ( MULTISET'
2645                   ||'    ( SELECT '  ||inv_vr_column_list
2646                   ||'        FROM   '||inv_vr_table_list
2647                   ||'        WHERE  '||inv_vr_where_clause
2648                   ||'    ) AS system.gmd_inv_spec_vrs_tab_type' -- 5284242
2649                   ||'  )'
2650                   -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
2651                   ||', CAST'
2652                   ||'  ( MULTISET'
2653                   ||'    ( SELECT '  ||mon_vr_column_list
2654                   ||'        FROM   '||mon_vr_table_list
2655                   ||'        WHERE  '||mon_vr_where_clause
2656                   ||'    ) AS system.gmd_mon_spec_vrs_tab_type'   -- 5284242
2657                   ||'  )'
2658                   -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
2659                   ||')'
2660                   ||' FROM ' ||main_table_list
2661                   ||' WHERE '||main_where_clause;
2662 
2663     -- That's more or less the job done. We just need to tell the system where to store the data
2664     -- and assemble the USING clause. Note thatthe main_using_clause is last in the list as it
2665     -- appears last in the query.
2666 
2667     main_into_clause := ' BULK COLLECT INTO gmd_outbound_apis_pub.g_specifications_table ';
2668 
2669     main_using_clause := ' USING '||spec_test_using_clause
2670                        ||','||cust_vr_using_clause
2671                        ||','||wip_vr_using_clause
2672                        ||','||supl_vr_using_clause
2673                        ||','||inv_vr_using_clause
2674                        -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
2675                        ||','||mon_vr_using_clause
2676                        -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
2677                        ||','||main_using_clause;
2678 
2679 
2680     main_execution_string := 'BEGIN EXECUTE IMMEDIATE '
2681                        ||''''
2682                        ||sql_statement
2683                        ||''''
2684                        ||main_into_clause
2685                        ||main_using_clause
2686                        ||'; END;';
2687 
2688 
2689     gme_debug.put_line('The sql statement is:');
2690     i:= 1;
2691     LOOP
2692       gme_debug.put_line(substr(main_execution_string, i, 100));
2693       EXIT WHEN i> LENGTH(main_execution_string);
2694       i := i+100;
2695     END LOOP;
2696     gme_debug.put_line('Executing string');
2697 
2698      -- problem here
2699      -- check the s=rec type on d/b with the list here
2700 
2701     EXECUTE IMMEDIATE main_execution_string;
2702     gme_debug.put_line('SQL string execution comleted');
2703     -- Main retrieval done, now just fill in any blanks
2704 
2705     IF g_specifications_table.count > 0
2706     THEN
2707       FOR i in 1.. g_specifications_table.count
2708       LOOP
2709         IF g_specifications_table(i).owner_organization_id IS NOT NULL
2710         AND g_specifications_table(i).inventory_item_id IS NOT NULL THEN
2711           SELECT concatenated_segments INTO g_specifications_table(i).item_number
2712           FROM   mtl_system_items_b_kfv
2713           WHERE organization_id = g_specifications_table(i).owner_organization_id
2714           AND inventory_item_id = g_specifications_table(i).inventory_item_id;
2715         END IF;
2716       END LOOP;
2717     END IF;
2718 
2719     x_specifications_tbl := gmd_outbound_apis_pub.g_specifications_table;
2720     x_return_status := FND_API.G_RET_STS_SUCCESS;
2721 
2722   END IF;
2723   FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2724 
2725 EXCEPTION
2726   WHEN OTHERS
2727   THEN
2728 
2729     FND_MESSAGE.SET_NAME('GMD',SQLCODE);
2730     FND_MSG_PUB.Add;
2731     FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2732     x_return_status := FND_API.G_RET_STS_ERROR;
2733 END fetch_spec_vrs;
2734 
2735 
2736 PROCEDURE fetch_results
2737 ( p_api_version                IN NUMBER
2738 , p_init_msg_list              IN VARCHAR2 DEFAULT FND_API.G_FALSE
2739 , p_user_name                  IN VARCHAR2
2740 , p_orgn_code                  IN VARCHAR2 DEFAULT NULL
2741 , p_from_sample_no             IN VARCHAR2 DEFAULT NULL
2742 , p_to_sample_no               IN VARCHAR2 DEFAULT NULL
2743 , p_sample_id                  IN NUMBER   DEFAULT NULL
2744 , p_from_result_date           IN DATE     DEFAULT NULL
2745 , p_to_result_date             IN DATE     DEFAULT NULL
2746 , p_sample_disposition         IN VARCHAR2 DEFAULT NULL
2747 , p_in_spec_ind                IN VARCHAR2 DEFAULT NULL
2748 , p_qc_lab_orgn_code           IN VARCHAR2 DEFAULT NULL
2749 , p_evaluation_ind             IN VARCHAR2 DEFAULT NULL
2750 , p_tester                     IN VARCHAR2 DEFAULT NULL
2751 , p_tester_id                  IN NUMBER   DEFAULT NULL
2752 , p_test_provider_id           IN NUMBER   DEFAULT NULL
2753 , p_delete_mark                IN NUMBER   DEFAULT NULL
2757 , p_planned_resource_instance  IN NUMBER   DEFAULT NULL
2754 , p_from_last_update_date      IN DATE     DEFAULT NULL
2755 , p_to_last_update_date        IN DATE     DEFAULT NULL
2756 , p_planned_resource           IN VARCHAR2 DEFAULT NULL
2758 , p_actual_resource            IN VARCHAR2 DEFAULT NULL
2759 , p_actual_resource_instance   IN NUMBER   DEFAULT NULL
2760 , p_from_planned_result_date   IN DATE     DEFAULT NULL
2761 , p_to_planned_result_date     IN DATE     DEFAULT NULL
2762 , p_from_test_by_date          IN DATE     DEFAULT NULL
2763 , p_to_test_by_date            IN DATE     DEFAULT NULL
2764 , p_reserve_sample_id          IN NUMBER   DEFAULT NULL
2765 , x_results_table              OUT NOCOPY system.gmd_results_tab_type
2766 , x_return_status              OUT NOCOPY VARCHAR2
2767 , x_msg_count                  OUT NOCOPY NUMBER
2768 , x_msg_data                   OUT NOCOPY VARCHAR2
2769 )
2770 IS
2771 
2772   sql_statement            VARCHAR2(2000);
2773   column_list              VARCHAR2(2000);
2774   table_list               VARCHAR2(2000);
2775   where_clause             VARCHAR2(2000);
2776   into_clause              VARCHAR2(2000);
2777   using_clause             VARCHAR2(2000);
2778   execution_string         VARCHAR2(10000);
2779   row_count                NUMBER;
2780   i                        NUMBER;
2781 
2782 
2783   CURSOR item_cursor (cp_inventory_item_id NUMBER) IS
2784   SELECT concatenated_segments FROM mtl_system_items_kfv
2785   WHERE  inventory_item_id = cp_inventory_item_id;
2786 
2787   l_api_name               VARCHAR2(100) := 'fetch_results';
2788 
2789 BEGIN
2790 
2791   gme_debug.put_line('Enter GMD_OUTBOUND_APIS_PUB.FETCH_RESULTS API');
2792 
2793   IF NOT FND_API.Compatible_API_CALL
2794     (gmd_outbound_apis_pub.api_version, p_api_version, l_api_name,'GMD_OUTBOUND_APIS_PUB')
2795   OR NOT initialized_ok(p_user_name)
2796   THEN
2797     Raise FND_API.G_EXC_UNEXPECTED_ERROR;
2798   ELSE
2799     gme_debug.put_line('Starting FETCH_RESULTS processing');
2800     /*  Initialize message list if p_int_msg_list is set TRUE.  */
2801     IF FND_API.to_boolean(p_init_msg_list)
2802     THEN
2803       FND_MSG_PUB.Initialize;
2804     END IF;
2805 
2806     -- Start construction the select.
2807 
2808     gme_debug.put_line('Begin constructing SELECT statement');
2809     sql_statement := 'SELECT ';
2810 
2811 
2812     column_list := 'system.gmd_results_rec_type( r.update_instance_id, r.result_id, '   -- 5346480 add system.
2813                    ||'r.sample_id, gs.sample_no, r.test_id, gt.test_code, '
2814                    ||'r.test_replicate_cnt, r.lab_organization_id, r.result_value_num, '
2815                    ||'r.result_date, r.test_kit_inv_item_id, NULL, '
2816                    ||'r.test_kit_lot_number , r.tester, r.tester_id, '
2817                    ||'r.test_provider_id, r.ad_hoc_print_on_coa_ind, r.seq, '
2818                    ||'r.result_value_char, r.test_provider_code, r.assay_retest, '
2819                    ||'gsr.in_spec_ind, gesd.disposition, gsr.evaluation_ind, '
2820                    ||'r.planned_resource, r.planned_resource_instance, '
2821                    ||'r.actual_resource, r.actual_resource_instance, '
2822                    ||'r.planned_result_date, r.test_by_date, '
2823                    ||'r.delete_mark, r.text_code, r.attribute_category, r.attribute1, '
2824                    ||'r.attribute2, r.attribute3, r.attribute4, r.attribute5, '
2825                    ||'r.attribute6, r.attribute7, r.attribute8, r.attribute9, '
2826                    ||'r.attribute10, r.attribute11, r.attribute12, r.attribute13, '
2827                    ||'r.attribute14, r.attribute15, r.attribute16, r.attribute17, '
2828                    ||'r.attribute18, r.attribute19, r.attribute20, r.attribute21, '
2829                    ||'r.attribute22, r.attribute23, r.attribute24, r.attribute25, '
2830                    ||'r.attribute26, r.attribute27, r.attribute28, r.attribute29, '
2831                    ||'r.attribute30,  r.creation_date, '
2832                    ||'r.created_by, fu1.user_name, r.last_updated_by, fu2.user_name, '
2833                    ||'r.last_update_date, r.last_update_login, '
2834                    ||'r.test_qty, r.test_qty_uom, '
2835                    ||'r.reserve_sample_id, r.consumed_qty, '
2836                    ||'r.parent_result_id, r.test_method_id )';
2837 
2838 
2839     table_list := 'FROM gmd_results r, gmd_samples gs, gmd_qc_tests_b gt,'
2840                   ||'gmd_spec_results gsr, gmd_event_spec_disp gesd,'
2841                   ||'fnd_user fu1, fnd_user fu2 ';
2842 
2843 
2844     -- We now have the first three strings built. Here comes the good bit: building the where and us
2845     -- clauses and inserting the bind variables and values.
2846 
2847     -- We will not make any attempt to guess what the user had in mind, so if (for example) they spe
2848     -- an item_id and an item_no and these two don't stack up, so be it. The query will simply fail
2849     -- return anything.
2850 
2851     where_clause := 'WHERE r.sample_id = gs.sample_id and r.test_id = gt.test_id '
2852                   ||'AND r.result_id = gsr.result_id '
2853                   ||'AND gsr.event_spec_disp_id = gesd.event_spec_disp_id '
2854                   ||'AND gesd.spec_used_for_lot_attrib_ind = ''''Y''''  '
2855                   ||'AND fu1.user_id = r.created_by AND fu2.user_id = r.last_updated_by and 1=:dummy ';
2856 
2857     using_clause := ' USING 1 ';
2858 
2859     -- Work down the parameter list and append conditions, bind variables and bind values.
2860 
2861     IF p_orgn_code IS NOT NULL     /*NSRIVAST, INVCONV*/
2862     THEN
2863       gmd_outbound_apis_pub.g_orgn_code := p_orgn_code;
2864       where_clause := where_clause||'AND gs.organization_id = (SELECT organization_id'
2865                                   || ' FROM mtl_organizations WHERE organization_code = :orgn_code)';
2866       using_clause := using_clause||', gmd_outbound_apis_pub.g_orgn_code ';
2867     END IF;
2868 
2872       where_clause := where_clause||'AND gs.sample_no >= :sample_no ';
2869     IF p_from_sample_no IS NOT NULL
2870     THEN
2871       gmd_outbound_apis_pub.g_from_sample_no := p_from_sample_no;
2873       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_sample_no ';
2874     END IF;
2875 
2876     IF p_to_sample_no IS NOT NULL
2877     THEN
2878       gmd_outbound_apis_pub.g_to_sample_no := p_to_sample_no;
2879       where_clause := where_clause||'AND gs.sample_no <= :sample_no ';
2880 	      using_clause := using_clause||', gmd_outbound_apis_pub.g_to_sample_no ';
2881 	    END IF;
2882 
2883 	    IF p_sample_id IS NOT NULL
2884 	    THEN
2885 	      gmd_outbound_apis_pub.g_sample_id := p_sample_id;
2886       where_clause := where_clause||'AND gs.sample_id = :sample_id ';
2887       using_clause := using_clause||', gmd_outbound_apis_pub.g_sample_id ';
2888     END IF;
2889 
2890     IF p_from_result_date IS NOT NULL
2891     THEN
2892       gmd_outbound_apis_pub.g_from_result_date := p_from_result_date ;
2893       where_clause := where_clause||'AND r.result_date >= :from_result_date ';
2894       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_result_date ';
2895     END IF;
2896 
2897     IF p_to_result_date IS NOT NULL
2898     THEN
2899       gmd_outbound_apis_pub.g_to_result_date := p_to_result_date ;
2900       where_clause := where_clause||'AND r.result_date <= :to_result_date ';
2901       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_result_date ';
2902     END IF;
2903 
2904     IF p_sample_disposition IS NOT NULL
2905     THEN
2906       gmd_outbound_apis_pub.g_sample_disposition := p_sample_disposition ;
2907       where_clause := where_clause||'AND gesd.disposition = :disposition ';
2908       using_clause := using_clause||', gmd_outbound_apis_pub.g_sample_disposition ';
2909     END IF;
2910 
2911     IF p_in_spec_ind IS NOT NULL
2912     THEN
2913       gmd_outbound_apis_pub.g_in_spec_ind := p_in_spec_ind;
2914       where_clause := where_clause||'AND gsr.in_spec_ind = :in_spec_ind ';
2915       using_clause := using_clause||', gmd_outbound_apis_pub.g_in_spec_ind ';
2916     END IF;
2917 
2918     IF p_qc_lab_orgn_code IS NOT NULL  /*NSRIVAST, INVCONV*/
2919     THEN
2920       gmd_outbound_apis_pub.g_qc_lab_orgn_code := p_qc_lab_orgn_code ;
2921       where_clause := where_clause||'AND r.lab_organization_id = (SELECT organization_id'
2922                                   || ' FROM mtl_organizations WHERE organization_code = :lab_orgn_code)';
2923       using_clause := using_clause||', gmd_outbound_apis_pub.g_qc_lab_orgn_code ';
2924     END IF;
2925 
2926     IF p_evaluation_ind IS NOT NULL
2927     THEN
2928       gmd_outbound_apis_pub.g_evaluation_ind := p_evaluation_ind ;
2929       where_clause := where_clause||'AND gsr.evaluation_ind = :evaluation_ind ';
2930       using_clause := using_clause||', gmd_outbound_apis_pub.g_evaluation_ind ';
2931     END IF;
2932 
2933     -- BUG 3078568 target fnd_user for tester
2934     IF p_tester IS NOT NULL
2935     THEN
2936       table_list := table_list ||', fnd_user fu3 ';
2937       where_clause := where_clause ||'AND r.tester_id = fu3.user_id ';
2938 
2939       gmd_outbound_apis_pub.g_tester := p_tester;
2940       where_clause := where_clause||'AND fu3.user_name = :tester ';
2941       using_clause := using_clause||', gmd_outbound_apis_pub.g_tester ';
2942     END IF;
2943 
2944     IF p_tester_id IS NOT NULL
2945     THEN
2946       gmd_outbound_apis_pub.g_tester_id := p_tester_id ;
2947       where_clause := where_clause||'AND r.tester_id = :tester_id ';
2948       using_clause := using_clause||', gmd_outbound_apis_pub.g_tester_id ';
2949     END IF;
2950 
2951     IF p_test_provider_id IS NOT NULL
2952     THEN
2953       gmd_outbound_apis_pub.g_test_provider_id := p_test_provider_id ;
2954       where_clause := where_clause||'AND r.test_provider_id = :test_provider_id ';
2955       using_clause := using_clause||', gmd_outbound_apis_pub.g_test_provider_id ';
2956     END IF;
2957 
2958     IF p_delete_mark IS NOT NULL
2959     THEN
2960       gmd_outbound_apis_pub.g_delete_mark := p_delete_mark ;
2961       where_clause := where_clause||'AND r.delete_mark = :delete_mark ';
2962       using_clause := using_clause||', gmd_outbound_apis_pub.g_delete_mark ';
2963     END IF;
2964 
2965     IF p_from_last_update_date IS NOT NULL
2966     THEN
2967       gmd_outbound_apis_pub.g_from_last_update_date := p_from_last_update_date;
2968       where_clause := where_clause||'AND r.last_update_date >= :from_last_update_date ';
2969       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_last_update_date ';
2970     END IF;
2971 
2972     IF p_to_last_update_date IS NOT NULL
2973     THEN
2974       gmd_outbound_apis_pub.g_to_last_update_date := p_to_last_update_date;
2975       where_clause := where_clause||'AND r.last_update_date <= :to_last_update_date ';
2976       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_last_update_date ';
2977     END IF;
2978 
2979     IF p_planned_resource IS NOT NULL
2980     THEN
2981       gmd_outbound_apis_pub.g_planned_resource := p_planned_resource;
2982       where_clause := where_clause||'AND gsr.planned_resource = :planned_resource ';
2983       using_clause := using_clause||', gmd_outbound_apis_pub.g_planned_resource ';
2984     END IF;
2985 
2986     IF p_planned_resource_instance IS NOT NULL
2987     THEN
2988       gmd_outbound_apis_pub.g_planned_resource_instance := p_planned_resource_instance;
2989       where_clause := where_clause||'AND gsr.planned_resource_instance = :planned_resource_instance ';
2990       using_clause := using_clause||', gmd_outbound_apis_pub.g_planned_resource_instance ';
2991     END IF;
2992 
2993     IF p_actual_resource IS NOT NULL
2994     THEN
2995       gmd_outbound_apis_pub.g_actual_resource := p_actual_resource;
2999 
2996       where_clause := where_clause||'AND gsr.actual_resource = :actual_resource ';
2997       using_clause := using_clause||', gmd_outbound_apis_pub.g_actual_resource ';
2998     END IF;
3000     IF p_actual_resource_instance IS NOT NULL
3001     THEN
3002       gmd_outbound_apis_pub.g_actual_resource_instance := p_actual_resource_instance;
3003       where_clause := where_clause||'AND gsr.actual_resource_instance = :actual_resource_instance ';
3004       using_clause := using_clause||', gmd_outbound_apis_pub.g_actual_resource_instance ';
3005     END IF;
3006 
3007     IF p_from_planned_result_date IS NOT NULL
3008     THEN
3009       gmd_outbound_apis_pub.g_from_planned_result_date := p_from_planned_result_date;
3010       where_clause := where_clause||'AND gsr.planned_result_date >= :from_planned_result_date ';
3011       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_planned_result_date ';
3012     END IF;
3013 
3014     IF p_to_planned_result_date IS NOT NULL
3015     THEN
3016       gmd_outbound_apis_pub.g_to_planned_result_date := p_to_planned_result_date;
3017       where_clause := where_clause||'AND gsr.planned_result_date <= :to_planned_result_date ';
3018       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_planned_result_date ';
3019     END IF;
3020 
3021     IF p_from_test_by_date IS NOT NULL
3022     THEN
3023       gmd_outbound_apis_pub.g_from_test_by_date := p_from_test_by_date;
3024       where_clause := where_clause||'AND gsr.test_by_date >= :from_test_by_date ';
3025       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_test_by_date ';
3026     END IF;
3027 
3028     IF p_to_test_by_date IS NOT NULL
3029     THEN
3030       gmd_outbound_apis_pub.g_to_test_by_date := p_to_test_by_date;
3031       where_clause := where_clause||'AND gsr.test_by_date <= :to_test_by_date ';
3032       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_test_by_date ';
3033     END IF;
3034 
3035     IF p_reserve_sample_id IS NOT NULL
3036     THEN
3037       gmd_outbound_apis_pub.g_reserve_sample_id := p_reserve_sample_id;
3038       where_clause := where_clause||'AND r.reserve_sample_id = :reserve_sample_id ';
3039       using_clause := using_clause||', gmd_outbound_apis_pub.g_reserve_sample_id ';
3040     END IF;
3041 
3042     -- That's more or less the job done. We just need to tell the system where to store the data
3043 
3044     into_clause := ' BULK COLLECT INTO gmd_outbound_apis_pub.g_results_table ';
3045 
3046 
3047     execution_string := 'BEGIN EXECUTE IMMEDIATE '
3048                        ||''''
3049                        ||sql_statement||column_list||table_list||where_clause
3050                        ||''''
3051                        ||into_clause
3052                        ||using_clause
3053                        ||'; END;';
3054 
3055     gme_debug.put_line('The sql statement is:');
3056     i:= 1;
3057     LOOP
3058       gme_debug.put_line(substr(execution_string, i, 100));
3059       EXIT WHEN i> LENGTH(execution_string);
3060       i := i+100;
3061     END LOOP;
3062     gme_debug.put_line('Executing string');
3063 
3064     EXECUTE IMMEDIATE execution_string;
3065 
3066     FOR i IN 1..g_results_table.COUNT
3067     LOOP
3068 
3069       IF g_results_table(i).test_kit_inv_item_id IS NOT NULL
3070       THEN
3071         gme_debug.put_line('select from mtl_system_items_kfv using inventory_item_id of '|| g_results_table(i).test_kit_inv_item_id);
3072         SELECT  concatenated_segments
3073 	  INTO  g_results_table(i).test_kit_inv_item_number
3074           FROM  mtl_system_items_kfv
3075          WHERE  inventory_item_id = g_results_table(i).test_kit_inv_item_id
3076          and rownum = 1; -- 5346480 rework
3077       END IF;
3078 
3079       -- BUG 3078568 - populate tester from tester_id
3080       IF g_results_table(i).tester_id IS NOT NULL
3081       THEN
3082         gme_debug.put_line('select from fnd_user using user_id of '
3083                           || g_results_table(i).tester_id);
3084         SELECT user_name
3085 	  INTO g_results_table(i).tester
3086           FROM fnd_user
3087          WHERE user_id = g_results_table(i).tester_id;
3088       END IF;
3089 
3090     END LOOP;
3091 
3092 
3093     gme_debug.put_line('Returning table to caller');
3094     x_results_table := gmd_outbound_apis_pub.g_results_table;
3095     x_return_status := FND_API.G_RET_STS_SUCCESS;
3096 
3097   END IF;
3098 
3099   FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3100   gme_debug.put_line('Finished FETCH_RESULTS');
3101 
3102 EXCEPTION
3103   WHEN OTHERS
3104   THEN
3105     FND_MESSAGE.SET_NAME('GMD',SQLCODE);
3106     FND_MSG_PUB.Add;
3107     FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3108     x_return_status := FND_API.G_RET_STS_ERROR;
3109 
3110 END fetch_results;
3111 
3112 
3113 PROCEDURE fetch_composite_results
3114 ( p_api_version                  IN NUMBER
3115 , p_init_msg_list                IN VARCHAR2 DEFAULT FND_API.G_FALSE
3116 , p_user_name                    IN VARCHAR2
3117 , p_sampling_event_id            IN NUMBER   DEFAULT NULL
3118 , p_composite_result_disposition IN VARCHAR2 DEFAULT NULL
3119 , p_from_item_number             IN VARCHAR2 DEFAULT NULL /*NSRIVAST, INVCONV*/
3120 , p_to_item_number               IN VARCHAR2 DEFAULT NULL /*NSRIVAST, INVCONV*/
3121 , p_inventory_item_id            IN NUMBER   DEFAULT NULL /*NSRIVAST, INVCONV*/
3122 , p_from_lot_number              IN VARCHAR2 DEFAULT NULL /*NSRIVAST, INVCONV*/
3123 , p_to_lot_number                IN VARCHAR2 DEFAULT NULL /*NSRIVAST, INVCONV*/
3124 , p_lot_number                   IN VARCHAR2 DEFAULT NULL /*NSRIVAST, INVCONV*/
3125 , p_from_last_update_date        IN DATE     DEFAULT NULL
3126 , p_to_last_update_date          IN DATE     DEFAULT NULL
3127 , p_delete_mark                  IN NUMBER   DEFAULT NULL
3131 , x_msg_data                     OUT NOCOPY VARCHAR2
3128 , x_composite_results_table      OUT NOCOPY system.gmd_composite_results_tab_type
3129 , x_return_status                OUT NOCOPY VARCHAR2
3130 , x_msg_count                    OUT NOCOPY NUMBER
3132 )
3133 IS
3134 
3135   sql_statement            VARCHAR2(2000);
3136   column_list              VARCHAR2(2000);
3137   table_list               VARCHAR2(2000);
3138   where_clause             VARCHAR2(2000);
3139   into_clause              VARCHAR2(2000);
3140   using_clause             VARCHAR2(2000);
3141   execution_string         VARCHAR2(10000);
3142   row_count                NUMBER;
3143   i                        NUMBER;
3144   l_api_name               VARCHAR2(100) := 'fetch_composite_results';
3145 
3146 BEGIN
3147 
3148   gme_debug.put_line('Enter GMD_OUTBOUND_APIS_PUB.FETCH_COMPOSITE_RESULTS API');
3149 
3150   IF NOT FND_API.Compatible_API_CALL
3151     (gmd_outbound_apis_pub.api_version, p_api_version, l_api_name,'GMD_OUTBOUND_APIS_PUB')
3152   OR NOT initialized_ok(p_user_name)
3153   THEN
3154     Raise FND_API.G_EXC_UNEXPECTED_ERROR;
3155   ELSE
3156 
3157     gme_debug.put_line('Starting FETCH_COMPOSITE_RESULTS processing');
3158     /*  Initialize message list if p_int_msg_list is set TRUE.  */
3159     IF FND_API.to_boolean(p_init_msg_list)
3160     THEN
3161       FND_MSG_PUB.Initialize;
3162     END IF;
3163 
3164     -- Start construction the select.
3165 
3166     gme_debug.put_line('Begin constructing SELECT statement');
3167     sql_statement := 'SELECT ';
3168 
3169     IF (p_from_item_number IS NOT NULL OR p_to_item_number IS NOT NULL or p_inventory_item_id is NOT NULL) AND
3170        (p_lot_number is NOT NULL)
3171     THEN
3172       column_list := 'system.gmd_composite_results_rec_type( gcr.composite_result_id, gcr.test_id, '
3173                    ||'gcr.mean, gcr.mode_num, gcr.mode_char, gcr.low_num, gcr.high_num , '
3174                    ||'gcr.range, gcr.standard_deviation, gcr.sample_total, '
3175                    ||'gcr.sample_cnt_used, gcr.non_validated_result, '
3176                    ||'gcr.high_char, gcr.low_char, gcr.median_char, gcr.median_num, '
3177                    ||'gcr.composite_spec_disp_id, gcr.in_spec_ind, gcr.wf_response, '
3178                    ||'gcr.value_in_report_precision, gcsd.disposition, '
3179                    ||'gse.inventory_item_id, msi.concatenated_segments, gse.lot_number,  '
3180                    ||'gcr.delete_mark, gcr.text_code, gcr.attribute_category, gcr.attribute1, '
3181                    ||'gcr.attribute2, gcr.attribute3, gcr.attribute4, gcr.attribute5, '
3182                    ||'gcr.attribute6, gcr.attribute7, gcr.attribute8, gcr.attribute9, '
3183                    ||'gcr.attribute10, gcr.attribute11, gcr.attribute12, gcr.attribute13, '
3184                    ||'gcr.attribute14, gcr.attribute15, gcr.attribute16, gcr.attribute17, '
3185                    ||'gcr.attribute18, gcr.attribute19, gcr.attribute20, gcr.attribute21, '
3186                    ||'gcr.attribute22, gcr.attribute23, gcr.attribute24, gcr.attribute25, '
3187                    ||'gcr.attribute26, gcr.attribute27, gcr.attribute28, gcr.attribute29, '
3188                    ||'gcr.attribute30,  gcr.creation_date, gcr.created_by, fu1.user_name, '
3189                    ||'gcr.last_update_date, gcr.last_updated_by, fu2.user_name, '
3190                    ||'gcr.last_update_login, gcr.PARENT_COMPOSITE_RESULT_ID) ';       -- 5346713
3191 
3192       table_list := 'FROM gmd_composite_results gcr, gmd_sampling_events gse, gmd_event_spec_disp gesd, '
3193                   ||'gmd_composite_spec_disp gcsd, mtl_system_items_kfv msi, '
3194                   ||'fnd_user fu1, fnd_user fu2 ';
3195 
3196 
3197       where_clause := 'WHERE gse.sampling_event_id = gesd.sampling_event_id '
3198                   ||'AND gesd.event_spec_disp_id = gcsd.event_spec_disp_id '
3199                   ||'AND gcsd.composite_spec_disp_id = gcr.composite_spec_disp_id '
3200                   ||'AND gse.inventory_item_id = msi.inventory_item_id '
3201                   ||'AND fu1.user_id = gcr.created_by AND fu2.user_id = gcr.last_updated_by and 1=:dummy ';
3202 
3203     ELSIF (p_from_item_number IS NOT NULL OR p_to_item_number IS NOT NULL or p_inventory_item_id is NOT NULL)
3204     THEN
3205       column_list := 'system.gmd_composite_results_rec_type( gcr.composite_result_id, gcr.test_id, '   -- 5284247
3206                    ||'gcr.mean, gcr.mode_num, gcr.mode_char, gcr.low_num, gcr.high_num , '
3207                    ||'gcr.range, gcr.standard_deviation, gcr.sample_total, '
3208                    ||'gcr.sample_cnt_used, gcr.non_validated_result, '
3209                    ||'gcr.high_char, gcr.low_char, gcr.median_char, gcr.median_num, '
3210                    ||'gcr.composite_spec_disp_id, gcr.in_spec_ind, gcr.wf_response, '
3211                    ||'gcr.value_in_report_precision, gcsd.disposition, '
3212                    ||'gse.inventory_item_id, msi.concatenated_segments,  gse.lot_number, '
3213                    ||'gcr.delete_mark, gcr.text_code, gcr.attribute_category, gcr.attribute1, '
3214                    ||'gcr.attribute2, gcr.attribute3, gcr.attribute4, gcr.attribute5, '
3215                    ||'gcr.attribute6, gcr.attribute7, gcr.attribute8, gcr.attribute9, '
3216                    ||'gcr.attribute10, gcr.attribute11, gcr.attribute12, gcr.attribute13, '
3217                    ||'gcr.attribute14, gcr.attribute15, gcr.attribute16, gcr.attribute17, '
3218                    ||'gcr.attribute18, gcr.attribute19, gcr.attribute20, gcr.attribute21, '
3219                    ||'gcr.attribute22, gcr.attribute23, gcr.attribute24, gcr.attribute25, '
3220                    ||'gcr.attribute26, gcr.attribute27, gcr.attribute28, gcr.attribute29, '
3221                    ||'gcr.attribute30,  gcr.creation_date, gcr.created_by, fu1.user_name, '
3222                    ||'gcr.last_update_date, gcr.last_updated_by, fu2.user_name, '
3223                    ||'gcr.last_update_login, gcr.PARENT_COMPOSITE_RESULT_ID) ';       -- 5346713
3224 
3225       table_list := 'FROM gmd_composite_results gcr, gmd_sampling_events gse, gmd_event_spec_disp gesd, '
3229 
3226                    ||'gmd_composite_spec_disp gcsd, mtl_system_items_kfv msi, '
3227                    ||'fnd_user fu1, fnd_user fu2 ';
3228 
3230       where_clause := 'WHERE gse.sampling_event_id = gesd.sampling_event_id '
3231                    ||'AND gesd.event_spec_disp_id = gcsd.event_spec_disp_id '
3232                    ||'AND gcsd.composite_spec_disp_id = gcr.composite_spec_disp_id '
3233                    ||'AND gse.inventory_item_id = msi.inventory_item_id '
3234                    ||'AND fu1.user_id = gcr.created_by AND fu2.user_id = gcr.last_updated_by and 1=:dummy ';
3235 
3236     ELSIF (p_lot_number is NOT NULL)
3237     THEN
3238       column_list := 'system.gmd_composite_results_rec_type( gcr.composite_result_id, gcr.test_id, ' -- 5284247
3239                    ||'gcr.mean, gcr.mode_num, gcr.mode_char, gcr.low_num, gcr.high_num , '
3240                    ||'gcr.range, gcr.standard_deviation, gcr.sample_total, '
3241                    ||'gcr.sample_cnt_used, gcr.non_validated_result, '
3242                    ||'gcr.high_char, gcr.low_char, gcr.median_char, gcr.median_num, '
3243                    ||'gcr.composite_spec_disp_id, gcr.in_spec_ind, gcr.wf_response, '
3244                    ||'gcr.value_in_report_precision, gcsd.disposition, '
3245                    ||'gse.inventory_item_id, NULL, gse.lot_number, '
3246                    ||'gcr.delete_mark, gcr.text_code, gcr.attribute_category, gcr.attribute1, '
3247                    ||'gcr.attribute2, gcr.attribute3, gcr.attribute4, gcr.attribute5, '
3248                    ||'gcr.attribute6, gcr.attribute7, gcr.attribute8, gcr.attribute9, '
3249                    ||'gcr.attribute10, gcr.attribute11, gcr.attribute12, gcr.attribute13, '
3250                    ||'gcr.attribute14, gcr.attribute15, gcr.attribute16, gcr.attribute17, '
3251                    ||'gcr.attribute18, gcr.attribute19, gcr.attribute20, gcr.attribute21, '
3252                    ||'gcr.attribute22, gcr.attribute23, gcr.attribute24, gcr.attribute25, '
3253                    ||'gcr.attribute26, gcr.attribute27, gcr.attribute28, gcr.attribute29, '
3254                    ||'gcr.attribute30,  gcr.creation_date, gcr.created_by, fu1.user_name, '
3255                    ||'gcr.last_update_date, gcr.last_updated_by, fu2.user_name, '
3256                    ||'gcr.last_update_login, gcr.PARENT_COMPOSITE_RESULT_ID) ';       -- 5347613
3257 
3258       table_list := 'FROM gmd_composite_results gcr, gmd_sampling_events gse, gmd_event_spec_disp gesd,'
3259                    ||'gmd_composite_spec_disp gcsd, mtl_system_items_kfv msi, '
3260                    ||'fnd_user fu1, fnd_user fu2 ';
3261 
3262 
3263       where_clause := 'WHERE gse.sampling_event_id = gesd.sampling_event_id '
3264                    ||'AND gesd.event_spec_disp_id = gcsd.event_spec_disp_id '
3265                    ||'AND gcsd.composite_spec_disp_id = gcr.composite_spec_disp_id '
3266                    ||'AND gse.inventory_item_id = msi.inventory_item_id ' -- 5346713 rework -  space missing from end of statement
3267                    ||'AND fu1.user_id = gcr.created_by AND fu2.user_id = gcr.last_updated_by and 1=:dummy ';
3268 
3269 
3270     ELSE
3271       column_list := 'system.gmd_composite_results_rec_type( gcr.composite_result_id, gcr.test_id, ' -- 5284247
3272                    ||'gcr.mean, gcr.mode_num, gcr.mode_char, gcr.low_num, gcr.high_num , '
3273                    ||'gcr.range, gcr.standard_deviation, gcr.sample_total, '
3274                    ||'gcr.sample_cnt_used, gcr.non_validated_result, '
3275                    ||'gcr.high_char, gcr.low_char, gcr.median_char, gcr.median_num, '
3276                    ||'gcr.composite_spec_disp_id, gcr.in_spec_ind, gcr.wf_response, '
3277                    ||'gcr.value_in_report_precision, gcsd.disposition, '
3278                    ||'gse.inventory_item_id, NULL, gse.lot_number, '
3279                    ||'gcr.delete_mark, gcr.text_code, gcr.attribute_category, gcr.attribute1, '
3280                    ||'gcr.attribute2, gcr.attribute3, gcr.attribute4, gcr.attribute5, '
3281                    ||'gcr.attribute6, gcr.attribute7, gcr.attribute8, gcr.attribute9, '
3282                    ||'gcr.attribute10, gcr.attribute11, gcr.attribute12, gcr.attribute13, '
3283                    ||'gcr.attribute14, gcr.attribute15, gcr.attribute16, gcr.attribute17, '
3284                    ||'gcr.attribute18, gcr.attribute19, gcr.attribute20, gcr.attribute21, '
3285                    ||'gcr.attribute22, gcr.attribute23, gcr.attribute24, gcr.attribute25, '
3286                    ||'gcr.attribute26, gcr.attribute27, gcr.attribute28, gcr.attribute29, '
3287                    ||'gcr.attribute30,  gcr.creation_date, gcr.created_by, fu1.user_name, '
3288                    ||'gcr.last_update_date, gcr.last_updated_by, fu2.user_name, '
3289                    ||'gcr.last_update_login, gcr.PARENT_COMPOSITE_RESULT_ID) ';       -- 5346713
3290 
3291 
3292       table_list := 'FROM gmd_composite_results gcr, gmd_sampling_events gse, gmd_event_spec_disp gesd, '
3293                   ||'gmd_composite_spec_disp gcsd, '
3294                   ||'fnd_user fu1, fnd_user fu2 ';
3295 
3296 
3297       where_clause := 'WHERE gse.sampling_event_id = gesd.sampling_event_id '
3298                   ||'AND gesd.event_spec_disp_id = gcsd.event_spec_disp_id '
3299                   ||'AND gcsd.composite_spec_disp_id = gcr.composite_spec_disp_id '
3300                   ||'AND fu1.user_id = gcr.created_by AND fu2.user_id = gcr.last_updated_by and 1=:dummy ';
3301 
3302 
3303     END IF;
3304     using_clause := ' USING 1 ';
3305 
3306     -- Work down the parameter list and append conditions, bind variables and bind values.
3307 
3308     IF p_sampling_event_id IS NOT NULL
3309     THEN
3310       gmd_outbound_apis_pub.g_sampling_event_id := p_sampling_event_id;
3311       where_clause := where_clause||'AND gse.sampling_event_id = :sampling_event_id ';
3312       using_clause := using_clause||', gmd_outbound_apis_pub.g_sampling_event_id ';
3313     END IF;
3314 
3315     IF p_composite_result_disposition IS NOT NULL
3316     THEN
3317       gmd_outbound_apis_pub.g_disposition := p_composite_result_disposition;
3318       where_clause := where_clause||'AND gcsd.disposition = :disposition ';
3322     -- BUG 3078607 correct using variable
3319       using_clause := using_clause||', gmd_outbound_apis_pub.g_disposition ';
3320     END IF;
3321 -- pal
3323     IF p_from_item_number IS NOT NULL  /*NSRIVAST, INVCONV*/
3324     THEN
3325       gmd_outbound_apis_pub.g_from_item_no := p_from_item_number;
3326       where_clause := where_clause||'AND msi.inventory_item_id IN ( SELECT distinct inventory_item_id FROM mtl_system_items_kfv'
3327                                   ||' WHERE concatenated_segments >= :from_item_number'
3328                                   ||' AND organization_id = gse.organization_id)';    -- 5346713 rework added org id
3329 
3330       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_item_no  ';
3331     END IF;
3332 
3333     IF p_to_item_number IS NOT NULL   /*NSRIVAST, INVCONV*/
3334     THEN
3335       gmd_outbound_apis_pub.g_to_item_no := p_to_item_number;
3336       where_clause := where_clause||'AND msi.inventory_item_id IN ( SELECT distinct inventory_item_id FROM mtl_system_items_kfv'
3337                                   ||' WHERE concatenated_segments <= :to_item_number'
3338                                   ||' AND organization_id = gse.organization_id)';   -- 5346713 rework added org id
3339       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_item_no ';
3340     END IF;
3341 
3342     IF p_inventory_item_id IS NOT NULL  /*NSRIVAST, INVCONV*/
3343     THEN
3344       gmd_outbound_apis_pub.g_inventory_item_id := p_inventory_item_id;
3345       where_clause := where_clause||'AND msi.inventory_item_id = :inventory_item_id '
3346                                   ||' AND msi.organization_id = gse.organization_id';   -- 5346713 rework added org id
3347       using_clause := using_clause||', gmd_outbound_apis_pub.g_inventory_item_id ';
3348     END IF;
3349 
3350     IF p_from_lot_number IS NOT NULL   /*NSRIVAST, INVCONV*/
3351     THEN
3352       gmd_outbound_apis_pub.g_from_lot_number := p_from_lot_number;
3353       where_clause := where_clause||'AND gse.lot_number >= :from_lot_number ';
3354       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_lot_number ';
3355     END IF;
3356 
3357     IF p_to_lot_number IS NOT NULL   /*NSRIVAST, INVCONV*/
3358     THEN
3359       gmd_outbound_apis_pub.g_to_lot_number := p_to_lot_number;
3360       where_clause := where_clause||'AND gse.lot_number <= :to_lot_number ';
3361       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_lot_number ';
3362     END IF;
3363 
3364     -- BUG 3078607 lot_id from gse not ilm
3365     IF p_lot_number IS NOT NULL    /*NSRIVAST, INVCONV*/
3366     THEN
3367       gmd_outbound_apis_pub.g_lot_number := p_lot_number;
3368       where_clause := where_clause||'AND gse.lot_number = :lot_number ';
3369       using_clause := using_clause||', gmd_outbound_apis_pub.g_lot_number ';
3370     END IF;
3371 
3372     IF p_from_last_update_date IS NOT NULL
3373     THEN
3374       gmd_outbound_apis_pub.g_from_last_update_date := p_from_last_update_date;
3375       where_clause := where_clause||'AND gcr.last_update_date >= :from_last_update_date ';
3376       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_last_update_date ';
3377     END IF;
3378 
3379     IF p_to_last_update_date IS NOT NULL
3380     THEN
3381       gmd_outbound_apis_pub.g_to_last_update_date := p_to_last_update_date;
3382       where_clause := where_clause||'AND gcr.last_update_date <= :to_last_update_date '; -- BUG 3078683
3383       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_last_update_date ';
3384     END IF;
3385 
3386     IF p_delete_mark IS NOT NULL
3387     THEN
3388       gmd_outbound_apis_pub.g_delete_mark := p_delete_mark ;
3389       where_clause := where_clause||'AND gcr.delete_mark = :delete_mark ';
3390       using_clause := using_clause||', gmd_outbound_apis_pub.g_delete_mark ';
3391     END IF;
3392 
3393     -- That's more or less the job done. We just need to tell the system where to store the data
3394 
3395     into_clause := ' BULK COLLECT INTO gmd_outbound_apis_pub.g_composite_results_table ';
3396 
3397     execution_string := 'BEGIN EXECUTE IMMEDIATE '
3398                        ||''''
3399                        ||sql_statement||column_list||table_list||where_clause
3400                        ||''''
3401                        ||into_clause
3402                        ||using_clause
3403                        ||'; END;';
3404 
3405     gme_debug.put_line('The sql statement is:');
3406     i:= 1;
3407     LOOP
3408       gme_debug.put_line(substr(execution_string, i, 100));
3409       EXIT WHEN i> LENGTH(execution_string);
3410       i := i+100;
3411     END LOOP;
3412     gme_debug.put_line('Executing string');
3413 
3414     EXECUTE IMMEDIATE execution_string;
3415    -- gme_debug.put_line('here 1 ');
3416     FOR i IN 1..g_composite_results_table.count
3417     LOOP
3418       --gme_debug.put_line('inside internal loop  i = ' || i);
3419       IF g_composite_results_table(i).inventory_item_id IS NOT NULL AND g_composite_results_table(i).item_number IS NULL
3420       THEN
3421         gme_debug.put_line('select from mtl_system_items_kfv  using '|| g_composite_results_table(i).inventory_item_id);
3422         SELECT concatenated_segments INTO g_composite_results_table(i).item_number
3423         FROM   mtl_system_items_kfv msi
3424         WHERE  inventory_item_id = g_composite_results_table(i).inventory_item_id
3425         and rownum = 1 ; -- 5346713 rework
3426       END IF;
3427 
3428       /*IF g_composite_results_table(i).lot_number IS NOT NULL
3429       THEN
3430           gme_debug.put_line('select from mtl_lot_numbers using '|| g_composite_results_table(i).lot_number);
3431           SELECT lot_number INTO g_composite_results_table(i).lot_number
3432           FROM mtl_lot_numbers
3433           WHERE lot_number = g_composite_results_table(i).lot_number;
3434       END IF;*/
3435 
3436 
3437     END LOOP;
3438 
3439 
3440     gme_debug.put_line('Returning table to caller');
3441     x_composite_results_table := gmd_outbound_apis_pub.g_composite_results_table;
3442     x_return_status := FND_API.G_RET_STS_SUCCESS;
3443 
3444   END IF;
3445 
3446 
3447   FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3448   gme_debug.put_line('Finished FETCH_COMPOSITE_RESULTS');
3449 
3450 EXCEPTION
3451   WHEN OTHERS
3452   THEN
3453     FND_MESSAGE.SET_NAME('GMD',SQLCODE);
3454     FND_MSG_PUB.Add;
3458 END fetch_composite_results;
3455     FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3456     x_return_status := FND_API.G_RET_STS_ERROR;
3457 
3459 
3460 
3461 PROCEDURE fetch_samples
3462 ( p_api_version                  IN NUMBER
3463 , p_init_msg_list                IN VARCHAR2 DEFAULT FND_API.G_FALSE
3464 , p_user_name                    IN VARCHAR2
3465 , p_organization_id              IN NUMBER   DEFAULT NULL
3466 , p_from_sample_no               IN VARCHAR2 DEFAULT NULL
3467 , p_to_sample_no                 IN VARCHAR2 DEFAULT NULL
3468 , p_sample_id	                 IN NUMBER   DEFAULT NULL
3469 , p_sampling_event_id            IN NUMBER   DEFAULT NULL
3470 , p_from_item_number             IN VARCHAR2 DEFAULT NULL /*bug 4165704, INVCONV*/
3471 , p_to_item_number               IN VARCHAR2 DEFAULT NULL /*bug 4165704, INVCONV*/
3472 , p_inventory_item_id            IN NUMBER   DEFAULT NULL /*bug 4165704, INVCONV*/
3473 , p_revision                     IN VARCHAR2 DEFAULT NULL /*bug 4165704, INVCONV*/
3474 , p_from_lot_number              IN VARCHAR2 DEFAULT NULL /*bug 4165704, INVCONV*/
3475 , p_to_lot_number                IN VARCHAR2 DEFAULT NULL /*bug 4165704, INVCONV*/
3476 , p_parent_lot_number            IN VARCHAR2 DEFAULT NULL /*bug 4165704, INVCONV*/
3477 --, p_lot_number                   IN VARCHAR2 DEFAULT NULL /*bug 4165704, INVCONV*/
3478 , p_priority		      	 IN VARCHAR2 DEFAULT NULL
3479 , p_spec_name		     	 IN VARCHAR2 DEFAULT NULL
3480 , p_spec_vers		     	 IN VARCHAR2 DEFAULT NULL
3481 , p_spec_id		         IN NUMBER   DEFAULT NULL
3482 , p_source		         IN VARCHAR2 DEFAULT NULL
3483 , p_from_date_drawn	    	 IN DATE     DEFAULT NULL
3484 , p_to_date_drawn	      	 IN DATE     DEFAULT NULL
3485 , p_from_expiration_date	 IN DATE     DEFAULT NULL
3486 , p_to_expiration_date		 IN DATE     DEFAULT NULL
3487 , p_from_date_received           IN DATE     DEFAULT NULL
3488 , p_to_date_received             IN DATE     DEFAULT NULL
3489 , p_from_date_required           IN DATE     DEFAULT NULL
3490 , p_to_date_required             IN DATE     DEFAULT NULL
3491 , p_resources                    IN VARCHAR2 DEFAULT NULL
3492 , p_instance_id                  IN NUMBER   DEFAULT NULL
3493 , p_from_retrieval_date          IN DATE     DEFAULT NULL
3494 , p_to_retrieval_date            IN DATE     DEFAULT NULL
3495 , p_sample_type                  IN VARCHAR2 DEFAULT NULL
3496 , p_ss_id                        IN NUMBER   DEFAULT NULL
3497 , p_ss_organization_id           IN VARCHAR2 DEFAULT NULL
3498 , p_ss_no                        IN VARCHAR2 DEFAULT NULL
3499 , p_variant_id                   IN NUMBER   DEFAULT NULL
3500 , p_variant_no                   IN NUMBER   DEFAULT NULL
3501 , p_time_point_id                IN NUMBER   DEFAULT NULL
3502 , p_source_subinventory	       	 IN VARCHAR2 DEFAULT NULL
3503 , p_source_locator_id	    	 IN NUMBER   DEFAULT NULL
3504 , p_grade_code		         IN VARCHAR2 DEFAULT NULL
3505 , p_sample_disposition		 IN VARCHAR2 DEFAULT NULL
3506 , p_storage_subinventory       	 IN VARCHAR2 DEFAULT NULL
3507 , p_storage_locator_id		 IN NUMBER   DEFAULT NULL
3508 , p_lab_organization_id 	 IN VARCHAR2 DEFAULT NULL
3509 , p_external_id			 IN VARCHAR2 DEFAULT NULL
3510 , p_sampler			 IN VARCHAR2 DEFAULT NULL
3511 , p_lot_retest_ind  		 IN VARCHAR2 DEFAULT NULL
3512 , p_subinventory	     	 IN VARCHAR2 DEFAULT NULL
3513 , p_locator_id               	 IN NUMBER   DEFAULT NULL
3514 , p_wip_plant_code 		 IN VARCHAR2 DEFAULT NULL
3515 , p_wip_batch_no		 IN VARCHAR2 DEFAULT NULL
3516 , p_wip_batch_id		 IN NUMBER   DEFAULT NULL
3517 , p_wip_recipe_no		 IN VARCHAR2 DEFAULT NULL
3518 , p_wip_recipe_version		 IN NUMBER   DEFAULT NULL
3519 , p_wip_recipe_id		 IN NUMBER   DEFAULT NULL
3520 , p_wip_formula_no		 IN VARCHAR2 DEFAULT NULL
3521 , p_wip_formula_version          IN NUMBER   DEFAULT NULL
3522 , p_wip_formula_id	     	 IN NUMBER   DEFAULT NULL
3523 , p_wip_formulaline	    	 IN NUMBER   DEFAULT NULL
3524 , p_wip_formulaline_id		 IN NUMBER   DEFAULT NULL
3525 , p_wip_line_type	      	 IN NUMBER   DEFAULT NULL
3526 , p_wip_routing_no	     	 IN VARCHAR2 DEFAULT NULL
3527 , p_wip_routing_vers  		 IN NUMBER   DEFAULT NULL
3528 , p_wip_routing_id		 IN NUMBER   DEFAULT NULL
3529 , p_wip_batchstep_no 		 IN NUMBER   DEFAULT NULL
3533 , p_wip_oprn_id		       	 IN NUMBER   DEFAULT NULL
3530 , p_wip_batchstep_id		 IN NUMBER   DEFAULT NULL
3531 , p_wip_oprn_no			 IN VARCHAR2 DEFAULT NULL
3532 , p_wip_oprn_vers	      	 IN NUMBER   DEFAULT NULL
3534 , p_cust_name		     	 IN VARCHAR2 DEFAULT NULL
3535 , p_cust_id		         IN NUMBER   DEFAULT NULL
3536 , p_org_id		       	 IN NUMBER   DEFAULT NULL
3537 , p_cust_ship_to_site_id	 IN NUMBER   DEFAULT NULL
3538 , p_cust_order		    	 IN VARCHAR2 DEFAULT NULL
3539 , p_cust_order_id	      	 IN NUMBER   DEFAULT NULL
3540 , p_cust_order_type	    	 IN VARCHAR2 DEFAULT NULL
3541 , p_cust_order_line	    	 IN NUMBER   DEFAULT NULL
3542 , p_cust_order_line_id		 IN NUMBER   DEFAULT NULL
3543 , p_supplier		      	 IN VARCHAR2 DEFAULT NULL
3544 , p_supplier_id		       	 IN NUMBER   DEFAULT NULL
3545 , p_supplier_site_id		 IN NUMBER   DEFAULT NULL
3546 , p_supplier_po		         IN VARCHAR2 DEFAULT NULL
3547 , p_supplier_po_id		     IN NUMBER   DEFAULT NULL
3548 , p_supplier_po_line		 IN NUMBER   DEFAULT NULL
3549 , p_supplier_po_line_id		 IN NUMBER   DEFAULT NULL
3550 , p_from_last_update_date	     IN DATE     DEFAULT NULL
3551 , p_to_last_update_date          IN DATE     DEFAULT NULL
3552 , p_retain_as                    IN VARCHAR2 DEFAULT NULL
3553 , p_delete_mark		        	 IN NUMBER   DEFAULT NULL
3554 , x_samples_table                OUT NOCOPY system.gmd_samples_tab_type -- 5335829
3555 , x_return_status                OUT NOCOPY VARCHAR2
3556 , x_msg_count                    OUT NOCOPY NUMBER
3557 , x_msg_data                     OUT NOCOPY VARCHAR2
3558 
3559 )
3560 IS
3561   -- BUG 3078013 increase size of variables
3562   sql_statement            VARCHAR2(2000);
3563   column_list              VARCHAR2(4000);
3564   table_list               VARCHAR2(2000);
3565   where_clause             VARCHAR2(4000);
3566   into_clause              VARCHAR2(4000);
3567   using_clause             VARCHAR2(4000);
3568   execution_string         VARCHAR2(12000);
3569   row_count                NUMBER;
3570   i                        NUMBER;
3571 
3572   l_api_name               VARCHAR2(100) := 'fetch_samples';
3573 
3574   l_ss_table_included      VARCHAR2(10);
3575 
3579 
3576   G_PKG_NAME           CONSTANT  VARCHAR2(30):='GMD_OUTBOUND_APIS_PUB';
3577 
3578 BEGIN
3580 --  dbms_output.put_line('Enter GMD_OUTBOUND_APIS_PUB.FETCH_SAMPLES API');
3581   -- (gmd_outbound_apis_pub.api_version, p_api_version, l_api_name,'GMD_OUTBOUND_APIS_PUB')
3582   IF NOT FND_API.Compatible_API_CALL
3583     (gmd_outbound_apis_pub.api_version, p_api_version , l_api_name , G_PKG_NAME)
3584   THEN
3585 
3586 --    dbms_output.put_line('api version error');
3587     Raise FND_API.G_EXC_UNEXPECTED_ERROR;
3588 
3589   ELSIF NOT initialized_ok(p_user_name)
3590 
3591   THEN
3592 
3593 --    dbms_output.put_line('user name error');
3594     Raise FND_API.G_EXC_UNEXPECTED_ERROR;
3595   ELSE
3596 
3597 --    dbms_output.put_line('Starting FETCH_SAMPLES processing');
3598     gme_debug.put_line('Starting FETCH_SAMPLES processing');
3599     /*  Initialize message list if p_int_msg_list is set TRUE.  */
3600     IF FND_API.to_boolean(p_init_msg_list)
3601     THEN
3602       FND_MSG_PUB.Initialize;
3603     END IF;
3604 
3605     -- Start construction for the select.
3606 
3607 --    dbms_output.put_line('Begin constructing SELECT statement');
3608     gme_debug.put_line('Begin constructing SELECT statement');
3609     sql_statement := 'SELECT ';
3610 
3611     -- BUG 3077905 Retrieve sample_disposition from gmd_sample_spec_disp not gmd_samples
3612 
3613     column_list := 'system.gmd_samples_rec_type( gs.sampling_event_id, gs.step_no, gs.step_id, ' -- 5335829
3614                    ||'gs.sample_id, gs.sample_no, gs.sample_desc, '
3615                    ||'gesd.spec_id, null, null , '
3616                    ||'gs.lab_organization_id, gs.inventory_item_id, gs.revision, null, '
3617                    ||'gs.locator_id, gs.expiration_date, '
3618                    ||'gs.lot_number, gs.parent_lot_number, '
3619                    ||'gs.batch_id, null, null, '
3620                    ||'gs.recipe_id, null, null, '
3621                    ||'gs.formula_id, null, null, gs.formulaline_id, null, null, '
3622                    ||'gs.routing_id, null, null, gs.oprn_id, null, null, null,null, gs.charge, '
3623                    ||'gs.cust_id, null, gs.order_id, null, null, gs.order_line_id, null, gs.org_id, null, '
3624                    ||'gs.supplier_id, null, gs.sample_qty, gs.sample_qty_uom, gs.source, '
3625                    ||'gs.sampler_id, null, gs.date_drawn, gs.source_comment, '
3626                    ||'gs.storage_subinventory, gs.storage_locator_id, gs.external_id, '
3627                    ||'gs.sample_approver_id, gs.inv_approver_id, '
3628                    ||'gs.priority, gs.sample_inv_trans_ind, '
3629                    ||'gs.supplier_site_id, null, gs.subinventory, gs.organization_id, '
3630                    ||'gs.po_header_id, null, gs.po_line_id, null, '
3631                    ||'gs.receipt_id, null, gs.receipt_line_id, null, '
3632                    ||'gssd.disposition, gs.ship_to_site_id, null, '
3633                    ||'gs.supplier_lot_no, gs.lot_retest_ind, gs.sample_instance, '
3634                    ||'gs.source_subinventory, gs.source_locator_id, '
3635                    ||'gs.date_received, gs.date_required, gs.resources, gs.instance_id, '
3636                    ||'gs.retrieval_date, gs.sample_type, gs.time_point_id, gs.variant_id, '
3637                    ||'gs.delete_mark, gs.text_code, gs.attribute_category, gs.attribute1, '
3638                    ||'gs.attribute2, gs.attribute3, gs.attribute4, gs.attribute5, '
3639                    ||'gs.attribute6, gs.attribute7, gs.attribute8, gs.attribute9, '
3640                    ||'gs.attribute10, gs.attribute11, gs.attribute12, gs.attribute13, '
3641                    ||'gs.attribute14, gs.attribute15, gs.attribute16, gs.attribute17, '
3642                    ||'gs.attribute18, gs.attribute19, gs.attribute20, gs.attribute21, '
3643                    ||'gs.attribute22, gs.attribute23, gs.attribute24, gs.attribute25, '
3644                    ||'gs.attribute26, gs.attribute27, gs.attribute28, gs.attribute29, '
3645                    ||'gs.attribute30,  gs.creation_date, gs.created_by, fu1.user_name, '
3646                    ||'gs.last_update_date, gs.last_updated_by, fu2.user_name, '
3647                    ||'gs.last_update_login, gs.retain_as, gs.remaining_qty) ';
3648 
3649     table_list := 'FROM gmd_samples gs, gmd_event_spec_disp gesd,gmd_sample_spec_disp gssd, '
3650                 ||'fnd_user fu1, fnd_user fu2 ';
3651 
3652 
3653     -- BUG 3077905 Retrieve sample_disposition from gmd_sample_spec_disp not gmd_samples
3654     where_clause := 'WHERE gs.sampling_event_id = gesd.sampling_event_id and '
3655                   ||' gesd.event_spec_disp_id = gssd.event_spec_disp_id and '
3656                   ||' gs.sample_id = gssd.sample_id and '
3657                   ||' gesd.spec_used_for_lot_attrib_ind = ''''Y'''' and '
3658                   ||' fu1.user_id = gs.created_by AND fu2.user_id = gs.last_updated_by and 1=:dummy ';
3659 
3660     using_clause := ' USING 1 ';
3661 --    dbms_output.put_line('Before test for organization id not null');
3662 
3663     -- Work down the parameter list and append conditions, bind variables and bind values.
3664     -- ===================================================================================
3665 
3666     IF p_organization_id IS NOT NULL
3667     THEN
3668       gmd_outbound_apis_pub.g_orgn_id := p_organization_id;
3669       where_clause := where_clause||'AND gs.organization_id = :organization_id ';
3670       using_clause := using_clause||', gmd_outbound_apis_pub.g_orgn_id ';
3671     END IF;
3672 --    dbms_output.put_line('after organization id test for null');
3673 
3674     IF p_from_sample_no IS NOT NULL
3675     THEN
3676       gmd_outbound_apis_pub.g_from_sample_no := p_from_sample_no;
3677       where_clause := where_clause||'AND gs.sample_no >= :from_sample_no ';
3678       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_sample_no ';
3679     END IF;
3680 
3681     IF p_to_sample_no IS NOT NULL
3682     THEN
3683       gmd_outbound_apis_pub.g_to_sample_no := p_to_sample_no;
3687 
3684       where_clause := where_clause||'AND gs.sample_no <= :to_sample_no ';
3685       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_sample_no ';
3686     END IF;
3688     IF p_sample_id IS NOT NULL
3689     THEN
3690       gmd_outbound_apis_pub.g_sample_id := p_sample_id;
3691       where_clause := where_clause||'AND gs.sample_id = :sample_id ';
3692       using_clause := using_clause||', gmd_outbound_apis_pub.g_sample_id ';
3693     END IF;
3694 
3695     IF p_sampling_event_id IS NOT NULL
3696     THEN
3697       gmd_outbound_apis_pub.g_sampling_event_id := p_sampling_event_id;
3698       where_clause := where_clause||'AND gs.sampling_event_id = :sampling_event_id ';
3699       using_clause := using_clause||', gmd_outbound_apis_pub.g_sampling_event_id ';
3700     END IF;
3701 
3702 -- 5335829 rework
3703 
3704    IF p_from_item_number IS NOT NULL and p_to_item_number IS NOT NULL
3705     THEN
3706       g_from_item_number := p_from_item_number;
3707       g_to_item_number := p_to_item_number;
3708       where_clause := where_clause
3709                         ||' AND gs.inventory_item_id IN (SELECT inventory_item_id FROM mtl_system_items_b_kfv'
3710                         ||' WHERE concatenated_segments BETWEEN :from_item_number AND :to_item_number'
3711 			 	||' AND organization_id = gs.organization_id)';   -- 5335829 rework - owner_organization_id is not a valid column name
3712       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_item_number, gmd_outbound_apis_pub.g_to_item_number';
3713     ELSIF p_from_item_number IS NOT NULL
3714     THEN
3715       g_from_item_number := p_from_item_number;
3716       where_clause := where_clause
3717                         ||' AND gs.inventory_item_id IN (SELECT inventory_item_id FROM mtl_system_items_b_kfv'
3718                         ||' WHERE concatenated_segments >= :from_item_number'
3719 				 	||' AND organization_id = gs.organization_id)';   -- 5335829 rework - owner_organization_id is not a valid column name
3720       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_item_number';
3721     ELSIF p_to_item_number IS NOT NULL
3722     THEN
3723       g_to_item_number := p_to_item_number;
3724       where_clause := where_clause
3725                         ||' AND gs.inventory_item_id IN (SELECT inventory_item_id FROM mtl_system_items_b_kfv'
3726                         ||' WHERE concatenated_segments <= :to_item_number'
3727 			 	||' AND organization_id = gs.organization_id)';   -- 5335829 rework - owner_organization_id is not a valid column name
3728       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_item_number';
3729     END IF; --  IF p_from_item_number IS NOT NULL and p_to_item_number IS NOT NULL
3730 -- 5335829 end of rework
3731            -- Bug 4165704 invconv: this code should not be needed anymore
3732            --table_list := table_list ||', ic_item_mst iim ';
3733            --IF p_from_item_number IS NOT NULL THEN
3734            --where_clause := where_clause||'AND iim.item_number >= :from_item_number ';
3735            --END IF;
3736            --IF p_to_item_number IS NOT NULL THEN
3737            --where_clause := where_clause||'AND iim.item_number <= :to_item_number ';
3738            --END IF;
3739    --    dbms_output.put_line('after item_number');
3740 
3741     IF p_inventory_item_id IS NOT NULL
3742     THEN
3743       gmd_outbound_apis_pub.g_inventory_item_id := p_inventory_item_id;
3744       where_clause := where_clause||'AND gs.inventory_item_id = :inventory_item_id ';
3745       using_clause := using_clause||', gmd_outbound_apis_pub.g_inventory_item_id ';
3746     END IF;
3747 
3748     IF p_from_lot_number IS NOT NULL
3749     THEN
3750       gmd_outbound_apis_pub.g_from_lot_number := p_from_lot_number;
3751       where_clause := where_clause||'AND gs.lot_number >= :from_lot_number ';
3752       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_lot_number ';
3753     END IF;
3754 
3755     IF p_to_lot_number IS NOT NULL
3756     THEN
3757       gmd_outbound_apis_pub.g_to_lot_number := p_to_lot_number;
3758       where_clause := where_clause||'AND gs.lot_number <= :to_lot_number ';
3759       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_lot_number ';
3760     END IF;
3761 
3762     IF p_parent_lot_number IS NOT NULL
3763     THEN
3764       gmd_outbound_apis_pub.g_parent_lot_number := p_parent_lot_number;
3765       where_clause := where_clause||'AND gs.parent_lot_number = :parent_lot_number ';
3766       using_clause := using_clause||', gmd_outbound_apis_pub.g_parent_lot_number ';
3767     END IF;
3768 
3769     /* taken out for Bug 4165704
3770     IF p_lot_id IS NOT NULL
3771     THEN
3772       gmd_outbound_apis_pub.g_lot_id := p_lot_id;
3773       where_clause := where_clause||'AND gs.lot_id = :lot_id ';
3774       using_clause := using_clause||', gmd_outbound_apis_pub.g_lot_id ';
3775     END IF;
3776     */
3777 
3778     IF p_priority IS NOT NULL
3779     THEN
3780       gmd_outbound_apis_pub.g_priority := p_priority;
3781       where_clause := where_clause||'AND gs.priority = :priority ';
3782       using_clause := using_clause||', gmd_outbound_apis_pub.g_priority ';
3783     END IF;
3784 --    dbms_output.put_line('after priority');
3785 
3786     IF (p_spec_name IS NOT NULL OR p_spec_vers IS NOT NULL or p_spec_id is NOT NULL or p_grade_code is NOT NULL)
3787     THEN
3788       table_list := table_list ||',  gmd_specifications gspec ';
3789       where_clause := where_clause ||'AND gesd.spec_id = gspec.spec_id ';
3790 
3791       IF p_spec_name IS NOT NULL
3792       THEN
3793         gmd_outbound_apis_pub.g_spec_name := p_spec_name;
3794         where_clause := where_clause||'AND gspec.spec_name = :spec_name ';
3795         using_clause := using_clause||', gmd_outbound_apis_pub.g_spec_name ';
3796       END IF;
3797 
3798       IF p_spec_vers IS NOT NULL
3799       THEN
3800         gmd_outbound_apis_pub.g_spec_vers := p_spec_vers;
3801         where_clause := where_clause||'AND gspec.spec_vers = :spec_vers ';
3805       IF p_spec_id IS NOT NULL
3802         using_clause := using_clause||', gmd_outbound_apis_pub.g_spec_vers ';
3803       END IF;
3804 
3806       THEN
3807         gmd_outbound_apis_pub.g_spec_id := p_spec_id;
3808         where_clause := where_clause||'AND gspec.spec_id = :spec_id ';
3809         using_clause := using_clause||', gmd_outbound_apis_pub.g_spec_id ';
3810       END IF;
3811 
3812       IF p_grade_code IS NOT NULL
3813       THEN
3814         gmd_outbound_apis_pub.g_grade := p_grade_code;
3815         where_clause := where_clause||'AND gspec.grade = :grade ';
3816         using_clause := using_clause||', gmd_outbound_apis_pub.g_grade ';
3817       END IF;
3818     END IF;
3819 
3820     IF p_source IS NOT NULL
3821     THEN
3822       gmd_outbound_apis_pub.g_source := p_source;
3823       where_clause := where_clause||'AND gs.source = :source ';
3824       using_clause := using_clause||', gmd_outbound_apis_pub.g_source ';
3825     END IF;
3826 
3827     IF p_from_date_drawn IS NOT NULL
3828     THEN
3829       gmd_outbound_apis_pub.g_from_date_drawn := p_from_date_drawn;
3830       where_clause := where_clause||'AND gs.date_drawn >= :from_date_drawn ';
3831       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_date_drawn ';
3832     END IF;
3833 
3834     IF p_to_date_drawn IS NOT NULL
3835     THEN
3836       gmd_outbound_apis_pub.g_to_date_drawn := p_to_date_drawn;
3837       where_clause := where_clause||'AND gs.date_drawn <= :to_date_drawn ';
3838       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_date_drawn ';
3839     END IF;
3840 
3841     IF p_from_expiration_date IS NOT NULL
3842     THEN
3843       gmd_outbound_apis_pub.g_from_expiration_date := p_from_expiration_date;
3844       where_clause := where_clause||'AND gs.expiration_date >= :from_expiration_date ';
3845       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_expiration_date ';
3846     END IF;
3847 
3848     IF p_to_expiration_date IS NOT NULL
3849     THEN
3850       gmd_outbound_apis_pub.g_to_expiration_date := p_to_expiration_date;
3851       where_clause := where_clause||'AND gs.expiration_date <= :to_expiration_date ';
3852       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_expiration_date ';
3853     END IF;
3854 
3855     IF p_source_subinventory IS NOT NULL
3856     THEN
3857       gmd_outbound_apis_pub.g_source_subinventory := p_source_subinventory;
3858       where_clause := where_clause||'AND gs.source_subinventory = :source_subinventory ';
3859       using_clause := using_clause||', gmd_outbound_apis_pub.g_source_subinventory ';
3860     END IF;
3861 
3862     IF p_source_locator_id IS NOT NULL
3863     THEN
3864       gmd_outbound_apis_pub.g_source_locator_id := p_source_locator_id;
3865       where_clause := where_clause||'AND gs.source_locator_id = :source_locator_id ';
3866       using_clause := using_clause||', gmd_outbound_apis_pub.g_source_locator_id ';
3867     END IF;
3868 
3869     IF p_sample_disposition IS NOT NULL
3870     THEN
3871       gmd_outbound_apis_pub.g_sample_disposition := p_sample_disposition;
3872       where_clause := where_clause||'AND gssd.disposition = :sample_disposition ';
3873       using_clause := using_clause||', gmd_outbound_apis_pub.g_sample_disposition ';
3874     END IF;
3875 --    dbms_output.put_line('after sample_disp');
3876 
3877     IF p_storage_subinventory IS NOT NULL
3878     THEN
3879       gmd_outbound_apis_pub.g_storage_subinventory := p_storage_subinventory;
3880       where_clause := where_clause||'AND gs.storage_subinventory = :storage_subinventory ';
3881       using_clause := using_clause||', gmd_outbound_apis_pub.g_storage_subinventory ';
3882     END IF;
3883 
3884     IF p_storage_locator_id IS NOT NULL
3885     THEN
3886       gmd_outbound_apis_pub.g_storage_locator_id := p_storage_locator_id;
3887       where_clause := where_clause||'AND gs.storage_locator_id = :storage_locator_id ';
3888       using_clause := using_clause||', gmd_outbound_apis_pub.g_storage_locator_id ';
3889     END IF;
3890 
3891     IF p_lab_organization_id IS NOT NULL
3892     THEN
3893       gmd_outbound_apis_pub.g_lab_organization_id := p_lab_organization_id;
3894       where_clause := where_clause||'AND gs.lab_organization_id = :lab_organization_id ';
3895       using_clause := using_clause||', gmd_outbound_apis_pub.g_lab_organization_id ';
3896     END IF;
3897 
3898     IF p_external_id IS NOT NULL
3899     THEN
3900       gmd_outbound_apis_pub.g_external_id := p_external_id;
3901       where_clause := where_clause||'AND gs.external_id = :external_id ';
3902       using_clause := using_clause||', gmd_outbound_apis_pub.g_external_id ';
3903     END IF;
3904 
3905     IF p_sampler IS NOT NULL
3906     THEN
3907       table_list := table_list ||', fnd_user fu3 ';
3908       where_clause := where_clause ||'AND gs.sampler_id = fu3.user_id ';
3909 
3910       gmd_outbound_apis_pub.g_sampler := p_sampler;
3911       where_clause := where_clause||'AND fu3.user_name = :sampler ';
3912       using_clause := using_clause||', gmd_outbound_apis_pub.g_sampler ';
3913     END IF;
3914 
3915     IF p_lot_retest_ind IS NOT NULL
3916     THEN
3917       gmd_outbound_apis_pub.g_lot_retest_ind := p_lot_retest_ind;
3918       where_clause := where_clause||'AND gs.lot_retest_ind = :lot_retest_ind ';
3919       using_clause := using_clause||', gmd_outbound_apis_pub.g_lot_retest_ind ';
3920     END IF;
3921 
3922     IF p_subinventory IS NOT NULL
3923     THEN
3924       gmd_outbound_apis_pub.g_subinventory := p_subinventory;
3925       where_clause := where_clause||'AND gs.subinventory = :subinventory ';
3926       using_clause := using_clause||', gmd_outbound_apis_pub.g_subinventory ';
3927     END IF;
3928 
3929     IF p_locator_id IS NOT NULL
3930     THEN
3931       gmd_outbound_apis_pub.g_locator_id := p_locator_id;
3932       where_clause := where_clause||'AND gs.locator_id = :locator_id ';
3936     /* Bug 4165704: taken out for inventory convergence
3933       using_clause := using_clause||', gmd_outbound_apis_pub.g_locator_id ';
3934     END IF;
3935 
3937     IF p_location_id IS NOT NULL
3938     THEN
3939       table_list := table_list ||', ic_loct_mst ilm ';
3940       where_clause := where_clause ||'AND gs.location = ilm.location ';
3941 
3942       gmd_outbound_apis_pub.g_location_id := p_location_id;
3943       where_clause := where_clause||'AND ilm.inventory_location_id = :location_id ';
3944       using_clause := using_clause||', gmd_outbound_apis_pub.g_location_id ';
3945     END IF;
3946     */
3947 
3948     IF (p_wip_plant_code IS NOT NULL or p_wip_batch_no IS NOT NULL)
3949     THEN
3950       table_list := table_list ||', gme_batch_header gbh ';
3951       where_clause := where_clause ||'AND gs.batch_id = gbh.batch_id ';
3952       IF p_wip_plant_code IS NOT NULL
3953       THEN
3954         gmd_outbound_apis_pub.g_wip_plant_code := p_wip_plant_code;
3955         where_clause := where_clause||'AND gbh.plant_code = :wip_plant_code ';
3956         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_plant_code ';
3957       END IF;
3958 
3959       IF p_wip_batch_no IS NOT NULL
3960       THEN
3961         gmd_outbound_apis_pub.g_wip_batch_no := p_wip_batch_no;
3962         where_clause := where_clause||'AND gbh.batch_no = :wip_batch_no ';
3963         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_batch_no ';
3964       END IF;
3965     END IF;
3966 
3967     IF p_wip_batch_id IS NOT NULL
3968     THEN
3969       gmd_outbound_apis_pub.g_wip_batch_id := p_wip_batch_id;
3970       where_clause := where_clause||'AND gs.batch_id = :wip_batch_id ';
3971       using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_batch_id ';
3972     END IF;
3973 
3974     IF (p_wip_recipe_no IS NOT NULL or p_wip_recipe_version IS NOT NULL)
3975     THEN
3976       table_list := table_list ||', gmd_recipes_b r ';
3977       where_clause := where_clause ||'AND gs.recipe_id = r.recipe_id ';
3978 
3979       IF p_wip_recipe_no IS NOT NULL
3980       THEN
3981         gmd_outbound_apis_pub.g_wip_recipe_no := p_wip_recipe_no;
3982         where_clause := where_clause||'AND r.recipe_no = :wip_recipe_no ';
3983         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_recipe_no ';
3984       END IF;
3985 
3986       IF p_wip_recipe_version IS NOT NULL
3987       THEN
3988         gmd_outbound_apis_pub.g_wip_recipe_version := p_wip_recipe_version;
3989         where_clause := where_clause||'AND r.recipe_version = :wip_recipe_version ';
3990         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_recipe_version ';
3991       END IF;
3992     END IF;
3993 
3994     IF p_wip_recipe_id IS NOT NULL
3995     THEN
3996       gmd_outbound_apis_pub.g_wip_recipe_id := p_wip_recipe_id;
3997       where_clause := where_clause||'AND r.recipe_id = :wip_recipe_id ';
3998       using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_recipe_id ';
3999     END IF;
4000 
4001     -- BUG 3082684 - incorporate formula_vers
4002     IF p_wip_formula_no IS NOT NULL or p_wip_formula_version IS NOT NULL
4003     THEN
4004       table_list := table_list ||', fm_form_mst ffm ';
4005       where_clause := where_clause ||'AND gs.formula_id = ffm.formula_id ';
4006 
4007       IF p_wip_formula_no IS NOT NULL
4008       THEN
4009         gmd_outbound_apis_pub.g_wip_formula_no := p_wip_formula_no;
4010         where_clause := where_clause||'AND ffm.formula_no = :wip_formula_no ';
4011         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_formula_no ';
4012       END IF;
4013 
4014       IF p_wip_formula_version IS NOT NULL
4015       THEN
4016         gmd_outbound_apis_pub.g_wip_formula_vers := p_wip_formula_version;
4017         where_clause := where_clause||'AND ffm.formula_vers = :wip_formula_vers ';
4018         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_formula_vers ';
4019       END IF;
4020     END IF;
4021 
4022     IF p_wip_formula_id IS NOT NULL
4023     THEN
4024       gmd_outbound_apis_pub.g_wip_formula_id := p_wip_formula_id;
4025       where_clause := where_clause||'AND gs.formula_id = :wip_formula_id ';
4026       using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_formula_id ';
4027     END IF;
4028 
4029     IF (p_wip_formulaline IS NOT NULL or p_wip_line_type IS NOT NULL)
4030     THEN
4031       table_list := table_list ||', fm_matl_dtl fmd ';
4032       where_clause := where_clause ||'AND gs.formulaline_id = fmd.formulaline_id ';
4033 
4034       IF p_wip_formulaline IS NOT NULL
4035       THEN
4036         gmd_outbound_apis_pub.g_wip_formulaline := p_wip_formulaline;
4037         where_clause := where_clause||'AND fmd.line_no = :wip_formulaline ';
4038         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_formulaline ';
4039       END IF;
4040 
4041       IF p_wip_line_type IS NOT NULL
4042       THEN
4043         gmd_outbound_apis_pub.g_wip_line_type := p_wip_line_type;
4044         where_clause := where_clause||'AND fmd.line_type = :wip_line_type ';
4045         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_line_type ';
4046       END IF;
4047     END IF;
4048 
4049     IF p_wip_formulaline_id IS NOT NULL
4050     THEN
4051       gmd_outbound_apis_pub.g_wip_formulaline_id := p_wip_formulaline_id;
4052       where_clause := where_clause||'AND gs.formulaline_id = :wip_formulaline_id ';
4053       using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_formulaline_id ';
4054     END IF;
4055 
4056     IF (p_wip_routing_no IS NOT NULL or p_wip_routing_vers IS NOT NULL)
4057     THEN
4058       table_list := table_list ||', gmd_routings_b grout ';
4059       where_clause := where_clause ||'AND gs.routing_id = grout.routing_id ';
4060 
4061       IF p_wip_routing_no IS NOT NULL
4062       THEN
4066       END IF;
4063         gmd_outbound_apis_pub.g_wip_routing_no := p_wip_routing_no;
4064         where_clause := where_clause||'AND grout.routing_no = :wip_routing_no ';
4065         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_routing_no ';
4067 
4068       IF p_wip_routing_vers IS NOT NULL
4069       THEN
4070         gmd_outbound_apis_pub.g_wip_routing_vers := p_wip_routing_vers;
4071         where_clause := where_clause||'AND grout.routing_vers = :wip_routing_vers ';
4072         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_routing_vers ';
4073       END IF;
4074     END IF;
4075 
4076     IF p_wip_routing_id IS NOT NULL
4077     THEN
4078       gmd_outbound_apis_pub.g_wip_routing_id := p_wip_routing_id;
4079       where_clause := where_clause||'AND gs.routing_id = :wip_routing_id ';
4080       using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_routing_id ';
4081     END IF;
4082 
4083 
4084     IF p_wip_batchstep_no IS NOT NULL
4085     THEN
4086       gmd_outbound_apis_pub.g_wip_batchstep_no := p_wip_batchstep_no;
4087       where_clause := where_clause||'AND gs.step_no = :wip_batchstep_no ';
4088       using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_batchstep_no ';
4089     END IF;
4090 
4091     IF p_wip_batchstep_id IS NOT NULL
4092     THEN
4093       gmd_outbound_apis_pub.g_wip_batchstep_id := p_wip_batchstep_id;
4094       where_clause := where_clause||'AND gs.step_id = :wip_batchstep_id ';
4095       using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_batchstep_id ';
4096     END IF;
4097 
4098     IF (p_wip_oprn_no IS NOT NULL or p_wip_oprn_vers IS NOT NULL )
4099     THEN
4100       table_list := table_list ||', gmd_operations go ';
4101       where_clause := where_clause ||'AND gs.oprn_id = go.oprn_id ';
4102 
4103       IF p_wip_oprn_no IS NOT NULL
4104       THEN
4105         gmd_outbound_apis_pub.g_wip_oprn_no := p_wip_oprn_no;
4106         where_clause := where_clause||'AND go.oprn_no = :wip_oprn_no ';
4107         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_oprn_no ';
4108       END IF;
4109 
4110       IF p_wip_oprn_vers IS NOT NULL
4111       THEN
4112         gmd_outbound_apis_pub.g_wip_oprn_vers := p_wip_oprn_vers;
4113         where_clause := where_clause||'AND go.oprn_vers = :wip_oprn_vers ';
4114         using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_oprn_vers ';
4115       END IF;
4116     END IF;
4117 
4118     IF p_wip_oprn_id IS NOT NULL
4119     THEN
4120       gmd_outbound_apis_pub.g_wip_oprn_id := p_wip_oprn_id;
4121       where_clause := where_clause||'AND gs.oprn_id = :wip_oprn_id ';
4122       using_clause := using_clause||', gmd_outbound_apis_pub.g_wip_oprn_id ';
4123     END IF;
4124 
4125     IF p_cust_name IS NOT NULL
4126     THEN
4127       table_list := table_list ||', hz_cust_accounts_all hca, hz_parties hp ';
4128       where_clause := where_clause ||'AND gs.cust_id = hca.cust_account_id and hca.party_id = hp.party_id ';
4129 
4130       gmd_outbound_apis_pub.g_cust_name := p_cust_name;
4131       where_clause := where_clause||'AND hp.party_name = :cust_name ';
4132       using_clause := using_clause||', gmd_outbound_apis_pub.g_cust_name ';
4133     END IF;
4134 
4135     IF p_cust_id IS NOT NULL
4136     THEN
4137       gmd_outbound_apis_pub.g_cust_id := p_cust_id;
4138       where_clause := where_clause||'AND gs.cust_id = :cust_id ';
4139       using_clause := using_clause||', gmd_outbound_apis_pub.g_cust_id ';
4140     END IF;
4141 
4142     IF p_org_id IS NOT NULL
4143     THEN
4144       gmd_outbound_apis_pub.g_org_id := p_org_id;
4145       where_clause := where_clause||'AND gs.org_id = :org_id ';
4146       using_clause := using_clause||', gmd_outbound_apis_pub.g_org_id ';
4147     END IF;
4148 
4149     IF p_cust_ship_to_site_id IS NOT NULL
4150     THEN
4151       gmd_outbound_apis_pub.g_cust_ship_to_site_id := p_cust_ship_to_site_id;
4152       where_clause := where_clause||'AND gs.ship_to_site_id = :cust_ship_to_site_id ';
4153       using_clause := using_clause||', gmd_outbound_apis_pub.g_cust_ship_to_site_id ';
4154     END IF;
4155 --    dbms_output.put_line('after shipto site id');
4156 
4157     IF p_cust_order is NOT NULL or p_cust_order_type is NOT NULL
4158     THEN
4159       table_list := table_list ||', oe_order_headers_all ooh ';
4160       where_clause := where_clause ||'AND gs.order_id = ooh.header_id ';
4161 
4162       IF p_cust_order IS NOT NULL
4163       THEN
4164         gmd_outbound_apis_pub.g_cust_order := p_cust_order;
4165         where_clause := where_clause||'AND ooh.order_number = :cust_order ';
4166         using_clause := using_clause||', gmd_outbound_apis_pub.g_cust_order ';
4167       END IF;
4168 
4169       IF p_cust_order_type IS NOT NULL
4170       THEN
4171         table_list := table_list ||', oe_transaction_types_all ott ';
4172         where_clause := where_clause ||'AND ooh.order_type_id = ott.transaction_type_id ';
4173 
4174         gmd_outbound_apis_pub.g_cust_order_type := p_cust_order_type;
4175         where_clause := where_clause||'AND ott.transaction_type_code = :cust_order_type ';
4176         using_clause := using_clause||', gmd_outbound_apis_pub.g_cust_order_type ';
4177       END IF;
4178     END IF;
4179 
4180     IF p_cust_order_id IS NOT NULL
4181     THEN
4182       gmd_outbound_apis_pub.g_cust_order_id := p_cust_order_id;
4183       where_clause := where_clause||'AND gs.order_id = :cust_order_id ';
4184       using_clause := using_clause||', gmd_outbound_apis_pub.g_cust_order_id ';
4185     END IF;
4186 
4187     IF p_cust_order_line IS NOT NULL
4188     THEN
4189       table_list := table_list ||', oe_order_lines_all ool ';
4190       where_clause := where_clause ||'AND gs.order_line_id = ool.line_id ';
4191 
4192       gmd_outbound_apis_pub.g_cust_order_line := p_cust_order_line;
4196 
4193       where_clause := where_clause||'AND ool.line_number = :cust_order_line ';
4194       using_clause := using_clause||', gmd_outbound_apis_pub.g_cust_order_line ';
4195     END IF;
4197     IF p_cust_order_line_id IS NOT NULL
4198     THEN
4199       gmd_outbound_apis_pub.g_cust_order_line_id := p_cust_order_line_id;
4200       where_clause := where_clause||'AND gs.order_line_id = :cust_order_line_id ';
4201       using_clause := using_clause||', gmd_outbound_apis_pub.g_cust_order_line_id ';
4202     END IF;
4203 
4204     IF p_supplier IS NOT NULL
4205     THEN
4206       table_list := table_list ||', po_vendors v ';
4207       where_clause := where_clause ||'AND gs.supplier_id = v.vendor_id ';
4208 
4209       gmd_outbound_apis_pub.g_supplier := p_supplier;
4210       where_clause := where_clause||'AND v.vendor_name = :supplier ';     -- 5335829 rework  - changed from segment1
4211       using_clause := using_clause||', gmd_outbound_apis_pub.g_supplier ';
4212     END IF;
4213 
4214     IF p_supplier_id IS NOT NULL
4215     THEN
4216       gmd_outbound_apis_pub.g_supplier_id := p_supplier_id;
4217       where_clause := where_clause||'AND gs.supplier_id = :supplier_id ';
4218       using_clause := using_clause||', gmd_outbound_apis_pub.g_supplier_id ';
4219     END IF;
4220 
4221     IF p_supplier_site_id IS NOT NULL
4222     THEN
4223       gmd_outbound_apis_pub.g_supplier_site_id := p_supplier_site_id;
4224       where_clause := where_clause||'AND gs.supplier_site_id = :supplier_site_id ';
4225       using_clause := using_clause||', gmd_outbound_apis_pub.g_supplier_site_id ';
4226     END IF;
4227 
4228     IF p_supplier_po IS NOT NULL
4229     THEN
4230       table_list := table_list ||', po_headers_all ph ';
4231       where_clause := where_clause ||'AND gs.po_header_id = ph.po_header_id ';
4232 
4233       gmd_outbound_apis_pub.g_supplier_po := p_supplier_po;
4234       where_clause := where_clause||'AND ph.segment1 = :supplier_po ';
4235       using_clause := using_clause||', gmd_outbound_apis_pub.g_supplier_po ';
4236     END IF;
4237 
4238     IF p_supplier_po_id IS NOT NULL
4239     THEN
4240       gmd_outbound_apis_pub.g_supplier_po_id := p_supplier_po_id;
4241       where_clause := where_clause||'AND gs.po_header_id = :supplier_po_id ';
4242       using_clause := using_clause||', gmd_outbound_apis_pub.g_supplier_po_id ';
4243     END IF;
4244 
4245     IF p_supplier_po_line IS NOT NULL
4246     THEN
4247       table_list := table_list ||', po_lines_all pl ';
4248       where_clause := where_clause ||'AND gs.po_line_id = pl.po_line_id ';
4249 
4250       gmd_outbound_apis_pub.g_supplier_po_line := p_supplier_po_line;
4251       where_clause := where_clause||'AND pl.line_num = :supplier_po_line ';
4252       using_clause := using_clause||', gmd_outbound_apis_pub.g_supplier_po_line ';
4253     END IF;
4254 
4255     IF p_supplier_po_line_id IS NOT NULL
4256     THEN
4257       gmd_outbound_apis_pub.g_supplier_po_line_id := p_supplier_po_line_id;
4258       where_clause := where_clause||'AND gs.po_line_id = :supplier_po_line_id ';
4259       using_clause := using_clause||', gmd_outbound_apis_pub.g_supplier_po_line_id ';
4260     END IF;
4261 
4262     IF p_from_last_update_date IS NOT NULL
4263     THEN
4264       gmd_outbound_apis_pub.g_from_last_update_date := p_from_last_update_date;
4265       where_clause := where_clause||'AND gs.last_update_date >= :from_last_update_date ';
4266       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_last_update_date ';
4267     END IF;
4268 
4269     IF p_to_last_update_date IS NOT NULL
4270     THEN
4271       gmd_outbound_apis_pub.g_to_last_update_date := p_to_last_update_date;
4272       where_clause := where_clause||'AND gs.last_update_date <= :to_last_update_date ';
4273       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_last_update_date ';
4274     END IF;
4275 
4276     IF p_delete_mark IS NOT NULL
4277     THEN
4278       gmd_outbound_apis_pub.g_delete_mark := p_delete_mark ;
4279       where_clause := where_clause||'AND gs.delete_mark = :delete_mark ';
4280       using_clause := using_clause||', gmd_outbound_apis_pub.g_delete_mark ';
4281     END IF;
4282 
4283     -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
4284     IF p_from_date_received IS NOT NULL
4285     THEN
4286       gmd_outbound_apis_pub.g_from_date_received := p_from_date_received;
4287       where_clause := where_clause||'AND gs.date_received >= :from_date_received ';
4288       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_date_received ';
4289     END IF;
4290 
4291     IF p_to_date_received IS NOT NULL
4292     THEN
4293       gmd_outbound_apis_pub.g_to_date_received := p_to_date_received;
4294       where_clause := where_clause||'AND gs.date_received <= :to_date_received ';
4295       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_date_received ';
4296     END IF;
4297 
4298     IF p_from_date_required IS NOT NULL
4299     THEN
4300       gmd_outbound_apis_pub.g_from_date_required := p_from_date_required;
4301       where_clause := where_clause||'AND gs.date_required >= :from_date_required ';
4302       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_date_required ';
4303     END IF;
4304 
4305     IF p_to_date_required IS NOT NULL
4306     THEN
4307       gmd_outbound_apis_pub.g_to_date_required := p_to_date_required;
4308       where_clause := where_clause||'AND gs.date_required <= :to_date_required ';
4309       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_date_required ';
4310     END IF;
4311 
4312     IF p_resources IS NOT NULL
4313     THEN
4314       gmd_outbound_apis_pub.g_resources := p_resources;
4315       where_clause := where_clause||'AND gs.resources = :resources ';
4316       using_clause := using_clause||', gmd_outbound_apis_pub.g_resources ';
4317     END IF;
4318 
4319 --    dbms_output.put_line('after resources');
4323       where_clause := where_clause||'AND gs.instance_id = :instance_id ';
4320     IF p_instance_id IS NOT NULL
4321     THEN
4322       gmd_outbound_apis_pub.g_instance_id := p_instance_id;
4324       using_clause := using_clause||', gmd_outbound_apis_pub.g_instance_id ';
4325     END IF;
4326 
4327     IF p_from_retrieval_date IS NOT NULL
4328     THEN
4329       gmd_outbound_apis_pub.g_from_retrieval_date := p_from_retrieval_date;
4330       where_clause := where_clause||'AND gs.retrieval_date >= :from_retrieval_date ';
4331       using_clause := using_clause||', gmd_outbound_apis_pub.g_from_retrieval_date ';
4332     END IF;
4333 
4334     IF p_to_retrieval_date IS NOT NULL
4335     THEN
4336       gmd_outbound_apis_pub.g_to_retrieval_date := p_to_retrieval_date;
4337       where_clause := where_clause||'AND gs.retrieval_date <= :to_retrieval_date ';
4338       using_clause := using_clause||', gmd_outbound_apis_pub.g_to_retrieval_date ';
4339     END IF;
4340 
4341     IF p_sample_type IS NOT NULL
4342     THEN
4343       gmd_outbound_apis_pub.g_sample_type := p_sample_type;
4344       where_clause := where_clause||'AND gs.sample_type = :sample_type ';
4345       using_clause := using_clause||', gmd_outbound_apis_pub.g_sample_type ';
4346     END IF;
4347 
4348     l_ss_table_included := 'FALSE';
4349     IF (p_ss_organization_id IS NOT NULL OR p_ss_no IS NOT NULL OR p_ss_id IS NOT NULL) THEN
4350       table_list := table_list ||', gmd_stability_studies_b ss, gmd_ss_variants ssv ';
4351       where_clause := where_clause ||'AND ss.ss_id = ssv.ss_id AND ssv.variant_id = gs.variant_id ';
4352       l_ss_table_included := 'TRUE';
4353 
4354       IF p_ss_id IS NOT NULL
4355       THEN
4356         gmd_outbound_apis_pub.g_ss_id := p_ss_id;
4357         where_clause := where_clause||'AND ss.ss_id = :ss_id ';
4358         using_clause := using_clause||', gmd_outbound_apis_pub.g_ss_id ';
4359       END IF;
4360 
4361       IF p_ss_organization_id IS NOT NULL
4362       THEN
4363         gmd_outbound_apis_pub.g_ss_organization_id := p_ss_organization_id;
4364         where_clause := where_clause||'AND ss.organization_id = :ss_organization_id ';
4365         using_clause := using_clause||', gmd_outbound_apis_pub.g_ss_organization_id ';
4366       END IF;
4367 
4368       IF p_ss_no IS NOT NULL
4369       THEN
4370         gmd_outbound_apis_pub.g_ss_no := p_ss_no;
4371         where_clause := where_clause||'AND ss.ss_no = :ss_no ';
4372         using_clause := using_clause||', gmd_outbound_apis_pub.g_ss_no ';
4373       END IF;
4374 
4375     END IF;
4376 
4377     IF p_variant_no IS NOT NULL
4378     THEN
4379       IF (l_ss_table_included = 'FALSE') THEN
4380         table_list := table_list ||', gmd_ss_variants ssv ';
4381         where_clause := where_clause ||'AND ssv.variant_id = gs.variant_id ';
4382       END IF;
4383 
4384       gmd_outbound_apis_pub.g_variant_no := p_variant_no;
4385       where_clause := where_clause||'AND ssv.variant_no = :variant_no ';
4386       using_clause := using_clause||', gmd_outbound_apis_pub.g_variant_no ';
4387     END IF;
4388 
4389     IF p_variant_id IS NOT NULL
4390     THEN
4391       gmd_outbound_apis_pub.g_variant_id := p_variant_id;
4392       where_clause := where_clause||'AND gs.variant_id = :variant_id ';
4393       using_clause := using_clause||', gmd_outbound_apis_pub.g_variant_id ';
4394     END IF;
4395 --    dbms_output.put_line('after variant_id');
4396 
4397     IF p_time_point_id IS NOT NULL
4398     THEN
4399       gmd_outbound_apis_pub.g_time_point_id := p_time_point_id;
4400       where_clause := where_clause||'AND gs.time_point_id = :time_point_id ';
4401       using_clause := using_clause||', gmd_outbound_apis_pub.g_time_point_id ';
4402     END IF;
4403 
4404     -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
4405 
4406     -- START Incorporated Mini-Pack L Features to Outboud APIs
4407     IF p_retain_as IS NOT NULL
4408     THEN
4409       gmd_outbound_apis_pub.g_retain_as := p_retain_as;
4410       where_clause := where_clause||'AND gs.retain_as = :retain_as ';
4411       using_clause := using_clause||', gmd_outbound_apis_pub.g_retain_as ';
4412     END IF;
4413 
4414     -- END Incorporated Mini-Pack L Features to Outboud APIs
4415 
4416 
4417     -- That's more or less the job done. We just need to tell the system where to store the data
4418 
4419     into_clause := ' BULK COLLECT INTO gmd_outbound_apis_pub.g_samples_table ';
4420 
4421     execution_string := 'BEGIN EXECUTE IMMEDIATE '
4422                        ||''''
4423                        ||sql_statement||column_list||table_list||where_clause
4424                        ||''''
4425                        ||into_clause
4426                        ||using_clause
4427                        ||'; END;';
4428 
4429 
4430 --    dbms_output.put_line('The sql statement is:');
4431     i:= 1;
4432     LOOP
4433 --      dbms_output.put_line(substr(execution_string, i, 100));
4434       gme_debug.put_line(substr(execution_string, i, 100));
4435       EXIT WHEN i> LENGTH(execution_string);
4436       i := i+100;
4437     END LOOP;
4438 
4439 --    dbms_output.put_line('Executing stringxxx');
4440     gme_debug.put_line('Executing string');
4441     EXECUTE IMMEDIATE execution_string;
4442 
4443     -- Now fill in any missing data
4444 
4445 --    dbms_output.put_line('filling in missing data');
4446     FOR i IN 1..g_samples_table.count
4447     LOOP
4448         gme_debug.put_line('sample no=' || g_samples_table(i).sample_no);
4449       IF g_samples_table(i).spec_id is not NULL
4450        THEN
4451         gme_debug.put_line('select from gmd_specifications_b using spec_id of '
4452                           || g_samples_table(i).spec_id);
4453         select spec_name, spec_vers into g_samples_table(i).spec_name,g_samples_table(i).spec_vers
4454           from gmd_specifications_b
4458       IF g_samples_table(i).inventory_item_id is not NULL
4455           where spec_id = g_samples_table(i).spec_id;
4456       END IF;
4457 
4459       THEN
4460 --        dbms_output.put_line('select from mtl_system_items using inventory_item_id of '|| g_samples_table(i).inventory_item_id);
4461 --    dbms_output.put_line('filling in missing data');
4462         select concatenated_segments into g_samples_table(i).item_number
4463           from mtl_system_items_b_kfv
4464           where inventory_item_id = g_samples_table(i).inventory_item_id
4465             and organization_id  = g_samples_table(i).organization_id;
4466       END IF;
4467 
4468       IF g_samples_table(i).batch_id is not NULL
4469       THEN
4470 --        dbms_output.put_line('select from gme_batch_header with batch_id of '|| g_samples_table(i).batch_id);
4471         select plant_code, batch_no into g_samples_table(i).plant_code,g_samples_table(i).batch_no
4472           from gme_batch_header
4473           where batch_id = g_samples_table(i).batch_id;
4474       END IF;
4475 
4476       IF g_samples_table(i).recipe_id is not NULL
4477       THEN
4478 --        dbms_output.put_line('select from gmd_recipes_b with recipe_id of '|| g_samples_table(i).recipe_id);
4479         select recipe_no, recipe_version into g_samples_table(i).recipe_no,
4480                                               g_samples_table(i).recipe_version
4481           from gmd_recipes_b
4482           where recipe_id = g_samples_table(i).recipe_id;
4483       END IF;
4484 
4485       IF g_samples_table(i).formula_id is not NULL
4486       THEN
4487 --        dbms_output.put_line('select from fm_form_mst with formula_id of '|| g_samples_table(i).formula_id);
4488         select formula_no, formula_vers into g_samples_table(i).formula_no,
4489                                              g_samples_table(i).formula_vers
4490           from fm_form_mst
4491           where formula_id = g_samples_table(i).formula_id;
4492       END IF;
4493 
4494       IF g_samples_table(i).formulaline_id is not NULL
4495       THEN
4496         --dbms_output.put_line('select from fm_matl_dtl with formulaline_id of '
4497         --                  || g_samples_table(i).formulaline_id);
4498         select line_no, line_type into g_samples_table(i).formulaline_no,
4499                                        g_samples_table(i).line_type
4500           from fm_matl_dtl
4501           where formulaline_id = g_samples_table(i).formulaline_id;
4502       END IF;
4503 
4504       IF g_samples_table(i).routing_id is not NULL
4505       THEN
4506         --dbms_output.put_line('select from gmd_routings_b with routing_id of '
4507         --                  || g_samples_table(i).routing_id);
4508         select routing_no, routing_vers into g_samples_table(i).routing_no,g_samples_table(i).routing_vers
4509           from gmd_routings_b
4510           where routing_id = g_samples_table(i).routing_id;
4511       END IF;
4512 
4513       IF g_samples_table(i).oprn_id is not NULL
4514       THEN
4515         --dbms_output.put_line('select from gmd_operations with oprn_id of '|| g_samples_table(i).oprn_id);
4516         select oprn_no, oprn_vers into g_samples_table(i).oprn_no,g_samples_table(i).oprn_vers
4517           from gmd_operations
4518           where oprn_id = g_samples_table(i).oprn_id;
4519       END IF;
4520 
4521       IF g_samples_table(i).cust_id is not NULL
4522       THEN
4523         --dbms_output.put_line('select from hz_cust_accounts_all with cust_id of '
4524         --                  || g_samples_table(i).cust_id);
4525         select hp.party_name into g_samples_table(i).cust_name
4526           from hz_cust_accounts_all hca,hz_parties hp
4527           where hca.cust_account_id = g_samples_table(i).cust_id
4528             and hca.party_id = hp.party_id;
4529       END IF;
4530 
4531       IF g_samples_table(i).order_id is not NULL
4532       THEN
4533         --dbms_output.put_line('select from oe_order_headers_all with header_id of '
4534         --                  || g_samples_table(i).order_id);
4535         select ooh.order_number, ott.transaction_type_code into
4536                g_samples_table(i).order_number, g_samples_table(i).order_type
4537           from oe_order_headers_all ooh, oe_transaction_types_all ott
4538           where ooh.header_id = g_samples_table(i).order_id and
4539                 ooh.order_type_id = ott.transaction_type_id ;
4540       END IF;
4541 
4542       IF g_samples_table(i).order_line_id is not NULL
4543       THEN
4544         --dbms_output.put_line('select from oe_order_lines_all with line_id of '
4545         --                  || g_samples_table(i).order_line_id);
4546         select line_number into g_samples_table(i).order_line_number
4547           from oe_order_lines_all
4548           where line_id = g_samples_table(i).order_line_id;
4549       END IF;
4550 
4551       IF g_samples_table(i).org_id is not NULL
4552       THEN
4553         --dbms_output.put_line('select from hr_operating_units using org_id of '|| g_samples_table(i).org_id);
4554         select name into g_samples_table(i).org_name
4555           from hr_operating_units
4556           where organization_id = g_samples_table(i).org_id;
4557       END IF;
4558 
4559       IF g_samples_table(i).supplier_id is not NULL
4560       THEN
4561         --dbms_output.put_line('select from po_vendors using vendor_id of '|| g_samples_table(i).supplier_id);
4562         select segment1 into g_samples_table(i).supplier_no
4563           from po_vendors
4564           where vendor_id = g_samples_table(i).supplier_id;
4565       END IF;
4566 
4567       IF g_samples_table(i).sampler_id is not NULL
4568       THEN
4569         --dbms_output.put_line('select from fnd_user using user_id of '|| g_samples_table(i).sampler_id);
4570         select user_name into g_samples_table(i).sampler
4571           from fnd_user
4572           where user_id = g_samples_table(i).sampler_id;
4573       END IF;
4574 
4578         --                  || g_samples_table(i).po_header_id);
4575       IF g_samples_table(i).po_header_id is not NULL
4576       THEN
4577         --dbms_output.put_line('select from po_headers_all with po_header_id of '
4579         select segment1 into g_samples_table(i).po_number
4580           from po_headers_all
4581           where po_header_id = g_samples_table(i).po_header_id;
4582       END IF;
4583 
4584       IF g_samples_table(i).supplier_site_id is not NULL
4585       THEN
4586         --dbms_output.put_line('select from po_vendor_sites_all with supplier_site_id of '
4587         --                  || g_samples_table(i).supplier_site_id);
4588         select vendor_site_code into g_samples_table(i).supplier_site
4589           from po_vendor_sites_all
4590           where vendor_site_id = g_samples_table(i).supplier_site_id;
4591       END IF;
4592 
4593       IF g_samples_table(i).po_line_id is not NULL
4594       THEN
4595         --dbms_output.put_line('select from po_lines_all with po_line_id of '
4596         --                  || g_samples_table(i).po_line_id);
4597         select line_num into g_samples_table(i).po_line_number
4598           from po_lines_all
4599           where po_line_id = g_samples_table(i).po_line_id;
4600       END IF;
4601 
4602       IF g_samples_table(i).receipt_id is not NULL
4603       THEN
4604         --dbms_output.put_line('select from rcv_shipment_headers with shipment_header_id of '
4605         --                  || g_samples_table(i).receipt_id);
4606         select receipt_num into g_samples_table(i).receipt_no
4607           from rcv_shipment_headers
4608           where shipment_header_id = g_samples_table(i).receipt_id;
4609       END IF;
4610 
4611       IF g_samples_table(i).receipt_line_id is not NULL
4612       THEN
4613          -- Bug 3970893: receipt line id is changed from transaction if to shipment line id
4614         --dbms_output.put_line('select from rcv_shipment_lines with shipment_line_id of '
4615         --                  || g_samples_table(i).receipt_line_id);
4616         select rsl.line_num into g_samples_table(i).receipt_line
4617           from rcv_shipment_lines rsl
4618           where  rsl.shipment_line_id = g_samples_table(i).receipt_line_id;
4619       END IF;
4620 
4621       IF g_samples_table(i).ship_to_site_id is not NULL
4622       THEN
4623         --dbms_output.put_line('select from hz_cust_site_uses_all with site_use_id of '
4624         --                  || g_samples_table(i).ship_to_site_id);
4625           SELECT location into g_samples_table(i).ship_to_location
4626           FROM hz_cust_site_uses_all
4627           WHERE site_use_id = g_samples_table(i).ship_to_site_id;
4628       END IF;
4629 
4630     END LOOP;
4631 
4632     x_samples_table := gmd_outbound_apis_pub.g_samples_table;
4633     x_return_status := FND_API.G_RET_STS_SUCCESS;
4634 --    dbms_output.put_line('Returning table to caller');
4635 
4636   END IF;
4637 
4638   FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4639 --  dbms_output.put_line('Finished FETCH_SAMPLES');
4640 
4641 EXCEPTION
4642   WHEN OTHERS
4643   THEN
4644 
4645     FND_MESSAGE.SET_NAME('GMD',SQLCODE);
4646     FND_MSG_PUB.Add;
4647     FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4648     x_return_status := FND_API.G_RET_STS_ERROR;
4649 
4650 
4651 END fetch_samples;
4652 
4653 
4654 PROCEDURE fetch_sample_groups
4655 ( p_api_version                IN NUMBER
4656 , p_init_msg_list              IN VARCHAR2 DEFAULT FND_API.G_FALSE
4657 , p_user_name                  IN VARCHAR2
4658 , p_organization_id            IN VARCHAR2 DEFAULT NULL
4659 , p_from_sample_no             IN VARCHAR2 DEFAULT NULL
4660 , p_to_sample_no               IN VARCHAR2 DEFAULT NULL
4661 , p_sample_event_id            IN NUMBER   DEFAULT NULL
4662 , p_from_item_number           IN VARCHAR2 DEFAULT NULL
4663 , p_to_item_number             IN VARCHAR2 DEFAULT NULL
4664 , p_inventory_item_id          IN NUMBER   DEFAULT NULL
4665 , p_revision                   IN NUMBER   DEFAULT NULL
4666 , p_from_lot_number            IN VARCHAR2 DEFAULT NULL
4667 , p_to_lot_number              IN VARCHAR2 DEFAULT NULL
4668 , p_from_parent_lot_number     IN VARCHAR2 DEFAULT NULL
4669 , p_to_parent_lot_number       IN VARCHAR2 DEFAULT NULL
4670 , p_disposition                IN VARCHAR2 DEFAULT NULL
4671 , p_subinventory               IN VARCHAR2 DEFAULT NULL
4672 , p_locator_id                 IN VARCHAR2 DEFAULT NULL
4673 , p_wip_organization_id        IN VARCHAR2 DEFAULT NULL
4674 , p_wip_batch_no               IN VARCHAR2 DEFAULT NULL
4675 , p_wip_batch_id               IN NUMBER   DEFAULT NULL
4676 , p_wip_recipe_no              IN VARCHAR2 DEFAULT NULL
4677 , p_wip_recipe_vers            IN NUMBER   DEFAULT NULL
4678 , p_wip_recipe_id              IN NUMBER   DEFAULT NULL
4679 , p_wip_formula_no             IN VARCHAR2 DEFAULT NULL
4680 , p_wip_formula_vers           IN NUMBER   DEFAULT NULL
4681 , p_wip_formula_id             IN NUMBER   DEFAULT NULL
4682 , p_wip_formulaline_no         IN NUMBER   DEFAULT NULL
4683 , p_wip_formulaline_id         IN NUMBER   DEFAULT NULL
4684 , p_wip_formulaline_type       IN NUMBER   DEFAULT NULL
4685 , p_wip_routing_no             IN VARCHAR2 DEFAULT NULL
4686 , p_wip_routing_vers           IN NUMBER   DEFAULT NULL
4687 , p_wip_routing_id             IN NUMBER   DEFAULT NULL
4688 , p_wip_step_no                IN NUMBER   DEFAULT NULL
4689 , p_wip_step_id                IN NUMBER   DEFAULT NULL
4693 , p_customer                   IN NUMBER   DEFAULT NULL
4690 , p_wip_oprn_no                IN VARCHAR2 DEFAULT NULL
4691 , p_wip_oprn_vers              IN NUMBER   DEFAULT NULL
4692 , p_wip_oprn_id                IN NUMBER   DEFAULT NULL
4694 , p_customer_id                IN VARCHAR2 DEFAULT NULL
4695 , p_customer_org_id            IN NUMBER   DEFAULT NULL
4696 , p_customer_ship_to_location  IN VARCHAR2 DEFAULT NULL
4697 , p_customer_ship_to_location_id IN NUMBER   DEFAULT NULL
4698 , p_customer_order_number      IN NUMBER   DEFAULT NULL
4699 , p_customer_order_id          IN NUMBER   DEFAULT NULL
4700 , p_customer_order_type        IN NUMBER   DEFAULT NULL
4701 , p_customer_order_line        IN NUMBER   DEFAULT NULL
4702 , p_customer_order_line_id     IN NUMBER   DEFAULT NULL
4703 , p_supplier                   IN NUMBER   DEFAULT NULL
4704 , p_supplier_id                IN NUMBER   DEFAULT NULL
4705 , p_supplier_site              IN VARCHAR2 DEFAULT NULL
4706 , p_supplier_po_number         IN VARCHAR2 DEFAULT NULL
4707 , p_supplier_po_id             IN NUMBER   DEFAULT NULL
4708 , p_supplier_po_line           IN NUMBER   DEFAULT NULL
4709 , p_supplier_po_line_id        IN NUMBER   DEFAULT NULL
4710 , p_delete_mark                IN NUMBER   DEFAULT NULL
4711 , p_from_last_update_date      IN DATE     DEFAULT NULL
4712 , p_to_last_update_date        IN DATE     DEFAULT NULL
4713 -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
4714 , p_sg_organization_id         IN VARCHAR2 DEFAULT NULL
4715 , p_resources                  IN VARCHAR2 DEFAULT NULL
4716 , p_instance_id                IN NUMBER   DEFAULT NULL
4717 , p_ss_id                      IN NUMBER   DEFAULT NULL
4718 , p_ss_organization_id         IN VARCHAR2 DEFAULT NULL
4719 , p_ss_no                      IN VARCHAR2 DEFAULT NULL
4720 , p_variant_id                 IN NUMBER   DEFAULT NULL
4721 , p_variant_no                 IN NUMBER   DEFAULT NULL
4722 , p_time_point_id              IN NUMBER   DEFAULT NULL
4723 -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
4724 , x_sample_groups_table        OUT NOCOPY system.gmd_sampling_events_tab_type
4725 , x_return_status              OUT NOCOPY VARCHAR2
4726 , x_msg_count                  OUT NOCOPY NUMBER
4727 , x_msg_data                   OUT NOCOPY VARCHAR2
4728 )
4729 IS
4730   sql_statement            VARCHAR2(32000);
4731 
4732   main_column_list         VARCHAR2(10000);
4733   disp_column_list         VARCHAR2(10000);
4734 
4735   main_table_list          VARCHAR2(10000);
4736   disp_table_list          VARCHAR2(10000);
4737 
4738   main_where_clause        VARCHAR2(10000);
4739   disp_where_clause        VARCHAR2(10000);
4740 
4741   main_using_clause        VARCHAR2(10000);
4742   disp_using_clause        VARCHAR2(10000);
4743 
4744   main_into_clause         VARCHAR2(10000);
4745   main_execution_string    VARCHAR2(10000);
4746 
4747   row_count                NUMBER;
4748   l_api_name               VARCHAR2(240) := 'fetch_sample_groups';
4749 
4750   string_length            NUMBER;
4751   i                        NUMBER;
4752 
4753   l_ss_table_included      VARCHAR2(10);
4754 
4755 BEGIN
4756 
4757   IF NOT FND_API.Compatible_API_CALL
4758     (gmd_outbound_apis_pub.api_version, p_api_version, l_api_name,'GMD_OUTBOUND_APIS_PUB')
4759   OR NOT initialized_ok(p_user_name)
4760   THEN
4761     Raise FND_API.G_EXC_UNEXPECTED_ERROR;
4762   ELSE
4763     --dbms_output.put_line('Starting Fetch_Sample_Groups');
4764 
4765     /*  Initialize message list if p_int_msg_list is set TRUE.  */
4766     IF FND_API.to_boolean(p_init_msg_list)
4767     THEN
4768       FND_MSG_PUB.Initialize;
4769     END IF;
4770 
4771     -- Start construction for the select.
4772     --dbms_output.put_line('Starting statement construction');
4773 
4774     sql_statement := 'SELECT ';
4775 
4776     -- Build the query to retrieve all base rows from gmd_sampling events together with
4777     -- linked nested rowsets from gmd_event_spec_disp
4778     --
4779     -- The code looks worse than it is. All the next few sections do is build SQL
4780     -- clauses that are then glued together and executed dynamically with binds,
4781     -- depending on what has been specified in the parameters.
4782     --
4783     -- By doing one large BULK COLLECT using the constructed statement, almost everything
4784     -- is retrieved in a single hit, including all of the nested data. All that we need to
4785     -- do afterwards is fill in the gaps where a direct retrieval is not possible due to
4786     -- nullable columns where we cannot make outer joins.
4787     --
4788     -- The end result will resemble:
4789 
4790     /* SELECT gmd_sampling_event_rec_type
4791               ( <gmd_sampling_events table columns>
4792               , CAST
4793                 ( MULTISET
4794                   ( gmd_event_spec_disp_rec_type
4795                     ( SELECT <gmd_event_spec_disp table columns>
4796                       FROM   <gmd_event_spec_disp table set>
4797                       where  <GMD_EVENT_SPEC_DISP CONDITIONS WITH BIND VARIABLES> ***
4798                     )
4799                     AS gmd_event_spec_disps_tab_type
4800                   )
4801                 )
4802               )
4803        FROM
4804             <main table list>
4805        WHERE
4809     -- Here goes.....
4806             <sampling events constraining conditions with binds> ****
4807     */
4808 
4810 
4811     main_column_list :='  se.RECEIPT_ID, rh.RECEIPT_NUM, se.PO_HEADER_ID , ph.SEGMENT1'
4812                      ||', se.SAMPLING_EVENT_ID, se.ORIGINAL_SPEC_VR_ID, se.DISPOSITION'
4813                      ||', se.SAMPLE_REQ_CNT, se.SAMPLE_TAKEN_CNT, se.SAMPLING_PLAN_ID'
4814                      ||', se.EVENT_TYPE_CODE, se.SAMPLING_EVENT_ID, se.inventory_item_id, im.concatenated_segments'
4815                      ||', se.lot_number, se.parent_lot_number, se.subinventory, se.locator_id'
4816                      ||', se.BATCH_ID, bh.BATCH_NO, se.RECIPE_ID, r.RECIPE_NO'
4817                      ||', r.RECIPE_VERSION, se.FORMULA_ID, fh.FORMULA_NO, fh.FORMULA_VERS'
4818                      ||', se.FORMULALINE_ID, fd.LINE_NO, se.ROUTING_ID, se.OPRN_ID'
4819                      ||', se.CHARGE, se.CUST_ID, NULL, se.ORDER_ID, oh.ORDER_NUMBER'
4820                      ||', se.ORDER_LINE_ID, ol.line_number, se.ORG_ID, mp.organization_code, se.SUPPLIER_ID'
4821                      ||', NULL, se.PO_LINE_ID, pl.LINE_NUM, se.RECEIPT_LINE_ID'
4822                      ||', NULL, se.SUPPLIER_LOT_NO, se.COMPLETE_IND'
4823                      ||', se.SAMPLE_ID_TO_EVALUATE, se.COMPOSITE_ID_TO_EVALUATE, se.TEXT_CODE'
4824                      ||', se.CREATION_DATE, se.CREATED_BY, fu1.USER_NAME, se.LAST_UPDATED_BY'
4825                      ||', fu2.USER_NAME, se.LAST_UPDATE_DATE, se.LAST_UPDATE_LOGIN'
4826                      ||', se.SUPPLIER_SITE_ID, NULL, se.SHIP_TO_SITE_ID, NULL'
4827                      ||', se.STEP_ID, se.STEP_NO, se.LOT_RETEST_IND, se.RECOMPOSITE_IND'
4828                      ||', se.SAMPLE_ACTIVE_CNT '
4829                      ||', se.organization_id, se.resources, se.instance_id, se.time_point_id '
4830                      ||', se.variant_id, se.archived_taken, se.reserved_taken ';
4831 
4832     main_table_list  :=' gmd_sampling_events se, rcv_shipment_headers rh, po_headers_all ph'
4833                      ||',gme_batch_header bh, fnd_user fu1, fnd_user fu2, gmd_recipes_b r'
4834                      ||',fm_form_mst_b fh, oe_order_headers_all oh, mtl_system_items_b im'
4835                      ||',po_lines_all pl, oe_order_lines_all ol, fm_matl_dtl fd'
4836                      ||',mtl_parameters mp';
4837 
4838     main_where_clause:=' 1=:dummy AND se.receipt_id = rh.shipment_header_id(+)'
4839                      ||' AND se.po_header_id = ph.po_header_id(+)'
4840                      ||' AND se.inventory_item_id = im.inventory_item_id(+)'
4841                      ||' AND se.batch_id = bh.batch_id(+)'
4842                      ||' AND se.recipe_id = r.recipe_id(+)'
4843                      ||' AND se.formula_id = fh.formula_id(+)'
4844                      ||' AND se.order_id = oh.header_id(+)'
4845                      ||' AND se.po_line_id = pl.po_line_id(+)'
4846                      ||' AND se.order_line_id = ol.line_id(+)'
4847                      ||' AND se.formulaline_id = fd.formulaline_id(+)'
4848                      ||' AND se.org_id = mp.organization_id(+)'
4849                      ||' AND se.created_by = fu1.user_id'
4850                      ||' AND se.last_updated_by = fu2.user_id';
4851 
4852     main_using_clause:='1';
4853 
4854     disp_column_list :=' sd.EVENT_SPEC_DISP_ID, sd.SPEC_ID, gs.SPEC_NAME, gs.SPEC_VERS'
4855                      ||',sd.SPEC_VR_ID, sd.DISPOSITION, sd.SPEC_USED_FOR_LOT_ATTRIB_IND'
4856                      ||',sd.DELETE_MARK, sd.CREATION_DATE, sd.CREATED_BY, fu3.USER_NAME'
4857                      ||',sd.LAST_UPDATE_DATE, sd.LAST_UPDATED_BY, fu4.USER_NAME, sd.LAST_UPDATE_LOGIN';
4858 
4859     disp_table_list  :=' gmd_event_spec_disp sd, gmd_specifications_b gs, fnd_user fu3, fnd_user fu4';
4860 
4861     disp_where_clause:=' sd.spec_id = gs.spec_id(+) AND sd.created_by = fu3.user_id'
4862                      ||' AND sd.last_updated_by = fu4.user_id'
4863                      ||' AND sd.sampling_event_id = se.sampling_event_id'
4864                      ||' AND 1=:dummy ';
4865     disp_using_clause:='1';
4866 
4867 -- Bug 3124620; Changed where clause to validate the orgn_code
4868     IF p_organization_id IS NOT NULL
4869     THEN
4870       g_orgn_id := p_organization_id;
4871       main_where_clause := main_where_clause
4872                         ||' AND se.organization_id = :organization_id';
4873       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_orgn_id';
4874     END IF;
4875 
4876     IF p_from_sample_no IS NOT NULL OR p_to_sample_no IS NOT NULL
4877     THEN
4878       main_table_list := main_table_list||', gmd_samples gsa';
4879       main_where_clause := main_where_clause||' AND se.sampling_event_id = gsa.sampling_event_id';
4880 
4881       IF p_from_sample_no IS NOT NULL
4882       THEN
4883         g_from_sample_no := p_from_sample_no;
4884         main_where_clause := main_where_clause||' AND gsa.sample_no >= :from_sample';
4885         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_sample_no';
4886       END IF;
4887 
4888       IF p_to_sample_no IS NOT NULL
4889       THEN
4890         g_to_sample_no := p_to_sample_no;
4891         main_where_clause := main_where_clause||' AND gsa.sample_no <= :to_sample';
4892         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_sample_no';
4893       END IF;
4894     END IF;
4895 
4896 /*  Bug 3124627 - changed event_id to sampling_event_id                  */
4897     IF p_sample_event_id IS NOT NULL
4898     THEN
4899       g_sample_event_id := p_sample_event_id;
4900       main_where_clause := main_where_clause||
4901                             ' AND se.sampling_event_id = :sampling_event_id';
4902       main_using_clause := main_using_clause||
4903                             ', gmd_outbound_apis_pub.g_sample_event_id';
4904     END IF;
4905 
4906     IF (p_from_item_number IS NOT NULL OR p_to_item_number IS NOT NULL ) THEN
4907       g_from_item_number := p_from_item_number;
4908       g_to_item_number := p_to_item_number;
4909       main_where_clause := main_where_clause
4910                         ||' AND gs.inventory_item_id IN (SELECT inventory_item_id FROM mtl_system_items_b_kfv'
4911                         ||' WHERE concatenated_segments BETWEEN :from_item_number AND :to_item_number'
4912 			||' AND organization_id = gs.owner_organization_id)';
4913       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_item_number, gmd_outbound_apis_pub.g_to_item_number';
4914     END IF;     -- (p_from_item_number IS NOT NULL OR p_to_item_number IS NOT NULL )
4915 
4916           -- Bug 4165704 invconv: code replaced by above
4917           --IF p_from_item_number IS NOT NULL THEN
4918             --g_from_item_number := p_from_item_number;
4919             --main_where_clause := main_where_clause||' AND im.item_number >= :from_item';
4920             --main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_item_number';
4921             --END IF;
4922          --IF p_to_item_number IS NOT NULL THEN
4923            --g_to_item_number := p_to_item_number;
4924            --main_where_clause := main_where_clause||' AND im.item_number <= :to_item';
4925            --main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_item_number';
4926          --END IF;
4927 
4928     IF p_inventory_item_id IS NOT NULL
4929     THEN
4930       g_inventory_item_id := p_inventory_item_id;
4931       main_where_clause := main_where_clause||' AND se.inventory_item_id = :inventory_item_id';
4932       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_inventory_item_id';
4933     END IF;
4934 
4935     IF p_from_lot_number IS NOT NULL
4936     THEN
4937       g_from_lot_number := p_from_lot_number;
4938       main_where_clause := main_where_clause||' AND se.lot_number >= :from_lot_number';
4939       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_lot_number';
4940     END IF;
4941 
4942     IF p_to_lot_number IS NOT NULL
4943     THEN
4944       g_to_lot_number := p_to_lot_number;
4945       main_where_clause := main_where_clause||' AND se.lot_number <= :to_lot_number';
4946       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_lot_number';
4947     END IF;
4948 
4949     IF p_from_parent_lot_number IS NOT NULL
4950     THEN
4951       g_from_parent_lot_number := p_from_parent_lot_number;
4952       main_where_clause := main_where_clause||' AND se.parent_lot_number >= :from_parent_lot_number';
4956     IF p_to_parent_lot_number IS NOT NULL
4953       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_parent_lot_number';
4954     END IF;
4955 
4957     THEN
4958       g_to_parent_lot_number := p_to_parent_lot_number;
4959       main_where_clause := main_where_clause||' AND se.parent_lot_number <= :to_parent_lot_number';
4960       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_parent_lot_number';
4961     END IF;
4962 
4963     /* taken out for bug 4165704
4964     IF p_lot_id IS NOT NULL
4965     THEN
4966       g_lot_id := p_lot_id;
4967       main_where_clause := main_where_clause||' AND se.lot_id = :lot_id';
4968       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_lot_id';
4969     END IF;
4970     */
4971 
4972     IF p_subinventory IS NOT NULL
4973     THEN
4974       g_subinventory := p_subinventory;
4975       main_where_clause := main_where_clause||' AND se.subinventory = :subinventory';
4976       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_subinventory';
4977     END IF;
4978 
4979     IF p_locator_id IS NOT NULL
4980     THEN
4981       g_locator_id := p_locator_id;
4982       main_where_clause := main_where_clause||' AND se.locator_id = :locator_id';
4983       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_locator_id';
4984     END IF;
4985 
4986     /* taken out for invconv bug 4165704
4987     IF p_location_id IS NOT NULL
4988     THEN
4989       g_location_id := p_location_id;
4990       main_table_list := main_table_list||', ic_loct_mst lm';
4991       main_where_clause := main_where_clause||' AND se.location = il.location'
4992                                             ||' AND il.inventory_location_id = :location_id';
4993       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_location_id';
4994     END IF;
4995     */
4996 
4997     IF p_wip_organization_id IS NOT NULL
4998     THEN
4999       g_wip_orgn_id := p_wip_organization_id;
5000       main_where_clause := main_where_clause||' AND bh.plant_code = :wip_organization_id';
5001       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_orgn_id';
5002     END IF;
5003 
5004     IF p_wip_batch_no IS NOT NULL
5005     THEN
5006       g_wip_batch_no := p_wip_batch_no;
5007       main_where_clause := main_where_clause||' AND bh.batch_no = :wip_batch_no';
5008       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_batch_no';
5009     END IF;
5010 
5011     IF p_wip_batch_id IS NOT NULL
5012     THEN
5013       g_wip_batch_id := p_wip_batch_id;
5014       main_where_clause := main_where_clause||' AND se.batch_id = :wip_batch_id';
5015       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_batch_id';
5016     END IF;
5017 
5018     IF p_wip_formula_no IS NOT NULL
5019     THEN
5020       g_wip_formula_no := p_wip_formula_no;
5021       main_where_clause := main_where_clause||' AND fh.formula_no = :wip_formula_no';
5022       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_formula_no';
5023     END IF;
5024 
5025     IF p_wip_formula_vers IS NOT NULL
5026     THEN
5027       g_wip_formula_vers := p_wip_formula_vers;
5028       main_where_clause := main_where_clause||' AND fh.formula_no = :wip_formula_vers';
5029       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_formula_vers';
5030     END IF;
5031 
5032     IF p_wip_formula_id IS NOT NULL
5033     THEN
5034       g_wip_formula_id := p_wip_formula_id;
5035       main_where_clause := main_where_clause||' AND se.formula_id = :wip_formula_id';
5036       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_formula_id';
5037     END IF;
5038 
5039     IF p_wip_recipe_no IS NOT NULL
5040     THEN
5041       g_wip_recipe_no := p_wip_recipe_no;
5042       main_where_clause := main_where_clause||' AND r.recipe_no = :wip_recipe_no';
5043       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_recipe_no';
5044     END IF;
5045 
5046  /*  Bug 3124643;  Changed r.recipe_no to r.recipe_version                  */
5047     IF p_wip_recipe_vers IS NOT NULL
5048     THEN
5049       g_wip_recipe_vers := p_wip_recipe_vers;
5050       main_where_clause := main_where_clause||' AND r.recipe_version = :wip_recipe_vers';
5051       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_recipe_vers';
5052     END IF;
5053 
5054     IF p_wip_recipe_id IS NOT NULL
5055     THEN
5056       g_wip_recipe_id := p_wip_recipe_id;
5057       main_where_clause := main_where_clause||' AND se.recipe_id = :wip_recipe_id';
5058       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_formula_id';
5059     END IF;
5060 
5064       main_where_clause := main_where_clause||' AND se.formulaline_id = :wip_formulaline_id';
5061     IF p_wip_formulaline_id IS NOT NULL
5062     THEN
5063       g_wip_formulaline_id := p_wip_formulaline_id;
5065       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_formulaline_id';
5066     END IF;
5067 
5068     IF p_wip_formulaline_no IS NOT NULL OR p_wip_formulaline_type IS NOT NULL
5069     THEN
5070 
5071       IF p_wip_formulaline_no IS NOT NULL
5072       THEN
5073         g_wip_formulaline_no := p_wip_formulaline_no;
5074         main_where_clause := main_where_clause||' AND fd.line_no = :wip_formulaline_no';
5075         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_formulaline_no';
5076       END IF;
5077 
5078       IF p_wip_formulaline_type IS NOT NULL
5079       THEN
5080         g_wip_formulaline_type := p_wip_formulaline_type;
5081         main_where_clause := main_where_clause||' AND fd.formulaline_type = :wip_formulaline_type';
5082         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_formulaline_type';
5083       END IF;
5084     END IF;
5085 
5086     IF p_wip_step_id IS NOT NULL
5087     THEN
5088       g_wip_step_id := p_wip_step_id;
5089       main_where_clause := main_where_clause||' AND se.step_id = :wip_step_id';
5090       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_step_id';
5091     END IF;
5092 
5093     IF p_wip_step_no IS NOT NULL
5094     THEN
5095       g_wip_step_no := p_wip_step_no;
5096       main_where_clause := main_where_clause||' AND se.step_no = :wip_step_no';
5097       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_step_no';
5098     END IF;
5099 
5100     IF p_wip_oprn_id IS NOT NULL
5101     THEN
5102       g_wip_oprn_id := p_wip_oprn_id;
5103       main_where_clause := main_where_clause||' AND se.oprn_id = :wip_oprn_id';
5104       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_oprn_id';
5105     END IF;
5106 
5107     IF p_wip_oprn_no IS NOT NULL OR p_wip_oprn_vers IS NOT NULL
5108     THEN
5109       main_table_list := main_table_list||', gmd_operations_b go';
5110       main_where_clause := main_where_clause||' AND se.oprn_id = go.oprn_id';
5111 
5112       IF p_wip_oprn_no IS NOT NULL
5113       THEN
5114         g_wip_oprn_no := p_wip_oprn_no;
5115         main_where_clause := main_where_clause||' AND go.oprn_no = :wip_oprn_no';
5116         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_oprn_no';
5117       END IF;
5118 
5119       IF p_wip_oprn_id IS NOT NULL
5120       THEN
5121         g_wip_oprn_id := p_wip_oprn_id;
5122         main_where_clause := main_where_clause||' AND go.oprn_vers = :wip_oprn_vers';
5123         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_wip_oprn_vers';
5124       END IF;
5125     END IF;
5126 
5127     IF p_customer_order_id IS NOT NULL
5128     THEN
5129       g_customer_order_id := p_customer_order_id;
5130       main_where_clause := main_where_clause||' AND se.order_id = :customer_order_id';
5131       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_customer_order_id';
5132     END IF;
5133 
5134     IF p_customer_order_number IS NOT NULL
5135     THEN
5136       g_customer_order_number := p_customer_order_number;
5137       main_where_clause := main_where_clause||' AND oh.order_number = :customer_order_number';
5138       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_customer_order_number';
5139     END IF;
5140 
5141     IF p_customer IS NOT NULL
5142     THEN
5143       g_cust_name := p_customer;
5144       main_table_list := main_table_list ||', hz_cust_accounts_all hca, hz_parties hp ';
5145       main_where_clause := main_where_clause ||' AND se.cust_id = hca.cust_account_id'
5146                                              ||' AND hca.party_id = hp.party_id'
5147                                              ||' AND hp.party_name = :cust_name';
5148       main_using_clause := main_using_clause ||', gmd_outbound_apis_pub.g_cust_name ';
5149     END IF;
5150 
5151     IF p_customer_id IS NOT NULL
5152     THEN
5153       g_customer_id := p_customer_id;
5154       main_where_clause := main_where_clause||' AND se.cust_id = :customer_id';
5155       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_customer_id';
5156     END IF;
5157 
5158      /*  Bug 3124653 Added code to retrieve sample groups by customer_ship_to_location &
5159                      customer_ship_to_location_id           */
5160     IF p_customer_ship_to_location IS NOT NULL
5161     THEN
5162       g_customer_ship_to_location := p_customer_ship_to_location;
5163       main_where_clause := main_where_clause||
5164                      ' AND se.ship_to_site_id IN '||
5165                      '    ( select site_use_id  '||
5166                      '      from hz_cust_site_uses_all '||
5167                      '      where location = :customer_ship_to_location)';
5168       main_using_clause := main_using_clause||
5169                      ', gmd_outbound_apis_pub.g_customer_ship_to_location';
5170     END IF;
5171 
5172     IF p_customer_ship_to_location_id IS NOT NULL
5173     THEN
5174       g_customer_ship_to_location_id := p_customer_ship_to_location_id;
5175       main_where_clause := main_where_clause||
5176                      ' AND se.ship_to_site_id = :customer_ship_to_location_id';
5177       main_using_clause := main_using_clause||
5178                      ', gmd_outbound_apis_pub.g_customer_ship_to_location_id';
5179     END IF;
5180 
5181     IF p_customer_order_type IS NOT NULL
5182     THEN
5183       g_customer_order_type := p_customer_order_type;
5184       main_table_list := main_table_list||', oe_transaction_types_all tt';
5185       main_where_clause := main_where_clause||' AND oh.order_type_id = tt.transaction_type_id'
5189 
5186                                             ||' AND tt.transaction_type_code = :customer_order_type';
5187       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_customer_order_type';
5188     END IF;
5190     IF p_customer_order_line IS NOT NULL OR p_customer_order_line_id IS NOT NULL
5191     THEN
5192       main_where_clause := main_where_clause||' AND oh.header_id = ol.header_id';
5193 
5194       IF p_customer_order_line IS NOT NULL
5195       THEN
5196         g_customer_order_line := p_customer_order_line;
5197         main_where_clause := main_where_clause||' AND ol.line_number = :customer_order_line';
5198         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_customer_order_line';
5199       END IF;
5200 
5201       IF p_customer_order_line_id IS NOT NULL
5202       THEN
5203         g_customer_order_line_id := g_customer_order_line_id;
5204         main_where_clause := main_where_clause||' AND ol.line_id = :customer_order_line_id';
5205         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_customer_order_line_id';
5206       END IF;
5207     END IF;
5208 
5209     IF p_supplier_id IS NOT NULL
5210     THEN
5211       g_supplier_id := p_supplier_id;
5212       main_where_clause := main_where_clause||' AND se.supplier_id = :supplier_id';
5213       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supplier_id';
5214     END IF;
5215 
5216     IF p_supplier IS NOT NULL
5217     THEN
5218       g_supplier := p_supplier;
5219       main_table_list := main_table_list||', po_vendors v';
5220       main_where_clause := main_where_clause||' AND se.supplier_id = v.vendor_id'
5221                                             ||' AND v.segment1 = :supplier';
5222       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supplier';
5223     END IF;
5224 
5225     IF p_supplier_site IS NOT NULL
5226     THEN
5227       g_supplier_site := p_supplier_site;
5228       main_table_list := main_table_list||', po_vendor_sites_all  vsa';
5229       main_where_clause := main_where_clause||' AND  se.supplier_site_id = vsa.vendor_site_id'
5230                                             ||' AND  vsa.vendor_site_code = :supplier_site';
5231       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supplier_site';
5232     END IF;
5233 
5234     IF p_supplier_po_number IS NOT NULL
5235     THEN
5236       g_supplier_po_number := p_supplier_po_number;
5237       main_table_list := main_table_list||', po_headers_all pha';
5238       main_where_clause := main_where_clause||' AND se.po_header_id = pha.po_header_id'
5239                                             ||' AND pha.segment1 = :supplier_po_number';
5240       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supplier_po_number';
5241     END IF;
5242 
5243     IF p_supplier_po_id IS NOT NULL
5244     THEN
5245       g_supplier_po_id := p_supplier_po_id;
5246       main_where_clause := main_where_clause||' AND se.po_header_id = :supplier_po_id';
5247       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supplier_po_id';
5248     END IF;
5249 
5250     IF p_supplier_po_line IS NOT NULL
5251     THEN
5252       g_supplier_po_line := g_supplier_po_line;
5253       main_where_clause := main_where_clause||' AND pl.line_num = :supplier_po_line)';
5254       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supplier_po_line';
5255     END IF;
5256 
5257     IF p_supplier_po_line_id IS NOT NULL
5258     THEN
5259       g_supplier_po_line_id := p_supplier_po_line_id;
5260       main_where_clause := main_where_clause||' AND  se.po_line_id = :supplier_po_line_id';
5261       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_supplier_po_line_id';
5262     END IF;
5263 
5264     IF p_disposition IS NOT NULL
5265     THEN
5266       g_disposition := p_disposition;
5267       main_where_clause := main_where_clause||' AND  se.disposition = :disposition';
5268       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_disposition';
5269 
5270       disp_where_clause := disp_where_clause||' AND  sd.disposition = :disposition';
5271       disp_using_clause := disp_using_clause||', gmd_outbound_apis_pub.g_disposition';
5272     END IF;
5273 
5274     IF p_delete_mark IS NOT NULL
5275     THEN
5276       g_delete_mark := p_delete_mark;
5277       disp_where_clause := disp_where_clause||' AND  sd.delete_mark = :delete_mark';
5278       disp_using_clause := disp_using_clause||', gmd_outbound_apis_pub.g_delete_mark';
5279     END IF;
5280 
5281 
5282     IF p_from_last_update_date IS NOT NULL
5283     THEN
5284       gmd_outbound_apis_pub.g_from_last_update_date := p_from_last_update_date;
5285       main_where_clause := main_where_clause||'AND se.last_update_date >= :from_last_update_date ';
5286       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_from_last_update_date ';
5287 
5288       disp_where_clause := disp_where_clause||'AND sd.last_update_date >= :from_last_update_date ';
5289       disp_using_clause := disp_using_clause||', gmd_outbound_apis_pub.g_from_last_update_date ';
5290     END IF;
5291 
5292     IF p_to_last_update_date IS NOT NULL
5293     THEN
5294       gmd_outbound_apis_pub.g_to_last_update_date := p_to_last_update_date;
5295       main_where_clause := main_where_clause||'AND se.last_update_date <= :to_last_update_date ';
5296       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_to_last_update_date ';
5297 
5298       disp_where_clause := disp_where_clause||'AND sd.last_update_date <= :to_last_update_date ';
5299       disp_using_clause := disp_using_clause||', gmd_outbound_apis_pub.g_to_last_update_date ';
5300     END IF;
5301 
5302     -- START B3124291 Incorporated Mini-Pack K Features to Outboud APIs
5303     IF p_sg_organization_id IS NOT NULL
5304     THEN
5305       gmd_outbound_apis_pub.g_sg_organization_id := p_sg_organization_id;
5309 
5306       main_where_clause := main_where_clause||'AND se.organization_id = :sg_organization_id ';
5307       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_sg_organization_id ';
5308     END IF;
5310     IF p_resources IS NOT NULL
5311     THEN
5312       gmd_outbound_apis_pub.g_resources := p_resources;
5313       main_where_clause := main_where_clause||'AND se.resources = :resources ';
5314       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_resources ';
5315     END IF;
5316 
5317     IF p_instance_id IS NOT NULL
5318     THEN
5319       gmd_outbound_apis_pub.g_instance_id := p_instance_id;
5320       main_where_clause := main_where_clause||'AND se.instance_id = :instance_id ';
5321       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_instance_id ';
5322     END IF;
5323 
5324     l_ss_table_included := 'FALSE';
5325     IF (p_ss_organization_id IS NOT NULL OR p_ss_no IS NOT NULL OR p_ss_id IS NOT NULL) THEN
5326       main_table_list := main_table_list ||', gmd_stability_studies_b ss, gmd_ss_variants ssv ';
5327       main_where_clause := main_where_clause ||'AND ss.ss_id = ssv.ss_id AND ssv.variant_id = se.variant_id ';
5328       l_ss_table_included := 'TRUE';
5329 
5330       IF p_ss_id IS NOT NULL
5331       THEN
5332         gmd_outbound_apis_pub.g_ss_id := p_ss_id;
5333         main_where_clause := main_where_clause||'AND ss.ss_id = :ss_id ';
5334         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_ss_id ';
5335       END IF;
5336 
5337       IF p_ss_organization_id IS NOT NULL
5338       THEN
5339         gmd_outbound_apis_pub.g_ss_organization_id := p_ss_organization_id;
5340         main_where_clause := main_where_clause||'AND ss.organization_id = :ss_organization_id ';
5341         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_ss_organization_id ';
5342       END IF;
5343 
5344       IF p_ss_no IS NOT NULL
5345       THEN
5346         gmd_outbound_apis_pub.g_ss_no := p_ss_no;
5347         main_where_clause := main_where_clause||'AND ss.ss_no = :ss_no ';
5348         main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_ss_no ';
5349       END IF;
5350 
5351     END IF;
5352 
5353     IF p_variant_no IS NOT NULL
5354     THEN
5355       IF (l_ss_table_included = 'FALSE') THEN
5356         main_table_list := main_table_list ||', gmd_ss_variants ssv ';
5357         main_where_clause := main_where_clause ||'AND ssv.variant_id = se.variant_id ';
5358       END IF;
5359 
5360       gmd_outbound_apis_pub.g_variant_no := p_variant_no;
5361       main_where_clause := main_where_clause||'AND ssv.variant_no = :variant_no ';
5362       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_variant_no ';
5363     END IF;
5364 
5365     IF p_variant_id IS NOT NULL
5366     THEN
5367       gmd_outbound_apis_pub.g_variant_id := p_variant_id;
5368       main_where_clause := main_where_clause||'AND se.variant_id = :variant_id ';
5369       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_variant_id ';
5370     END IF;
5371 
5372     IF p_time_point_id IS NOT NULL
5373     THEN
5374       gmd_outbound_apis_pub.g_time_point_id := p_time_point_id;
5375       main_where_clause := main_where_clause||'AND se.time_point_id = :time_point_id ';
5376       main_using_clause := main_using_clause||', gmd_outbound_apis_pub.g_time_point_id ';
5377     END IF;
5378 
5379     -- END B3124291 Incorporated Mini-Pack K Features to Outboud APIs
5380 
5381 
5382 
5383     sql_statement := 'SELECT system.gmd_sampling_event_rec_type' -- 5284247
5384                   ||'('||main_column_list
5385                   ||', CAST'
5386                   ||'  ( MULTISET'
5387                   ||'    ( SELECT '  ||disp_column_list
5388                   ||'        FROM   '||disp_table_list
5389                   ||'        WHERE  '||disp_where_clause
5390                   ||'    ) AS system.gmd_event_spec_disps_tab_type' -- 5284242
5391                   ||'  )'
5392                   ||')'
5393                   ||' FROM ' ||main_table_list
5394                   ||' WHERE '||main_where_clause;
5395 
5396     -- That's more or less the job done. We just need to tell the system where to store the data
5397     -- and assemble the USING clause. Note thatthe main_using_clause is last in the list as it
5398     -- appears last in the query.
5399 
5400     main_into_clause := ' BULK COLLECT INTO gmd_outbound_apis_pub.g_sample_groups_table ';
5401     main_using_clause := ' USING '||disp_using_clause
5402                        ||','||main_using_clause;
5403 
5404     main_execution_string := 'BEGIN EXECUTE IMMEDIATE '
5405                        ||''''
5406                        ||sql_statement
5407                        ||''''
5408                        ||main_into_clause
5409                        ||main_using_clause
5410                        ||'; END;';
5411 
5412     string_length := LENGTH(main_execution_string);
5413 
5414 
5415     EXECUTE IMMEDIATE main_execution_string;
5416 
5417     IF g_sample_groups_table.count > 0
5418     THEN
5419       FOR i in 1 .. g_sample_groups_table.count
5420       LOOP
5421         IF g_sample_groups_table(i).receipt_line_id IS NOT NULL
5422         THEN
5423              -- Bug 3970893: receipt line id is changed from transaction if to shipment line id
5424              --   select rsl.line_num into g_sample_groups_table(i).receipt_line_number
5425              --   from rcv_shipment_lines rsl, rcv_transactions rt
5426              --   where rt.transaction_id = g_sample_groups_table(i).receipt_line_id
5427           select rsl.line_num into g_sample_groups_table(i).receipt_line_number
5428           from rcv_shipment_lines rsl
5429           where rsl.shipment_line_id = g_sample_groups_table(i).receipt_line_id;
5430         END IF;
5431 
5432         IF g_sample_groups_table(i).supplier_id IS NOT NULL
5433         THEN
5434           select segment1 into g_sample_groups_table(i).supplier_name
5435           from po_vendors
5436           where vendor_id=g_sample_groups_table(i).supplier_id;
5437         END IF;
5438 
5439         IF g_sample_groups_table(i).ship_to_site_id IS NOT NULL
5440         THEN
5441           select location into g_sample_groups_table(i).ship_to_site_name
5442           from hz_cust_site_uses_all
5443           where site_use_id = g_sample_groups_table(i).ship_to_site_id;
5444         END IF;
5445 
5446         IF g_sample_groups_table(i).supplier_site_id IS NOT NULL
5447         THEN
5448           select vendor_site_code into g_sample_groups_table(i).supplier_site_name
5449           from po_vendor_sites_all
5450           where vendor_site_id = g_sample_groups_table(i).supplier_site_id;
5451         END IF;
5452       END LOOP;
5453     END IF;
5454 
5455   END IF;
5456 
5457   x_sample_groups_table := g_sample_groups_table;
5458   x_return_status := FND_API.G_RET_STS_SUCCESS;
5459 
5460   FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5461 
5462 EXCEPTION
5463   WHEN OTHERS THEN
5464 
5465     FND_MESSAGE.SET_NAME('GMD',SQLCODE);
5466     FND_MSG_PUB.Add;
5467     FND_MSG_PUB.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5468     x_return_status := FND_API.G_RET_STS_ERROR;
5469 END fetch_sample_groups;
5470 
5471 end gmd_outbound_apis_pub;