DBA Data[Home] [Help]

PACKAGE BODY: APPS.GMD_RESULTS_GRP

Source


1 PACKAGE BODY gmd_results_grp AS
2 --$Header: GMDGRESB.pls 120.29.12020000.5 2012/12/05 17:59:43 plowe ship $
3 
4 -- Global variables
5 G_PKG_NAME      CONSTANT VARCHAR2(30) := 'GMD_RESULTS_GRP';
6    --Bug 3222090, magupta removed call to FND_PROFILE.VALUE('AFLOG_ENABLED')
7    --forward decl.
8    function set_debug_flag return varchar2;
9    --l_debug VARCHAR2(1) := NVL(FND_PROFILE.VALUE('AFLOG_ENABLED'),'N');
10    l_debug VARCHAR2(1) := set_debug_flag;
11 
12    FUNCTION set_debug_flag RETURN VARCHAR2 IS
13    l_debug VARCHAR2(1):= 'N';
14    BEGIN
15 
16     --gmd_debug.put_line('fnd_log.level_procedure '||FND_LOG.LEVEL_PROCEDURE);
17     --gmd_debug.put_line('fnd_log.g_current_runtime_level '||FND_LOG.G_CURRENT_RUNTIME_LEVEL);
18 
19     IF( FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
20       l_debug := 'Y';
21     END IF;
22     RETURN l_debug;
23    END set_debug_flag;
24 
25 -- Start of comments
26 --+==========================================================================+
27 --|                   Copyright (c) 1998 Oracle Corporation                  |
28 --|                          Redwood Shores, CA, USA                         |
29 --|                            All rights reserved.                          |
30 --+==========================================================================+
31 --| File Name          : GMDGRESB.pls                                        |
32 --| Package Name       : GMD_RESULTS_GRP                                     |
33 --| Type               : Group                                               |
34 --|                                                                          |
35 --| Notes                                                                    |
36 --|    This package contains group layer APIs for Results Entity             |
37 --|                                                                          |
38 --| HISTORY                                                                  |
39 --|    Chetan Nagar	08-Aug-2002	Created.                             |
40 --|                                                                          |
41 --|   Vipul Vaish 23-Jul-2003 Bug#3063671                                    |
42 --|    Added a call to the Validate_Result procedure in calc_expression      |
43 --|    which is used to update the gmd_spec_results table with proper values |
44 --|    for IN_SPEC_IND,ACTION_CODE and EVALUATION_IND columns when the test  |
45 --|    type is of Expression.
46 --|   Rameshwar 13-APR-2004 Bug#3545701                                      |
47 --|    Added test type 'U' in the get_rslt_and_spec_rslt procedure           |
48 --|    to retrieve the target values for the current specification.          |
49 --|  B.Stone  9-Sep-2004  Bug 3763419; Added Guaranteed by Manufacturer      |
50 --|                       evaluation with the same business rules as         |
51 --|                       Approved with Variance; Result value not allowed   |
52 --|                       with this evaluation.                              |
53 --|  rboddu   13-Sep-2004 Bug 376341. Replaced '1X' with '1Z'                |
54 --|  J. DiIorio Jan-05-2006 Bug#4691545 - Replaced profile retrieval of      |
55 --|                         GMD_INCLUDE_OPTIONAL_TEST with call to           |
56 --|                         gmd_quality_config.                              |
57 --| RLNAGARA 22-MAR-2006 Bug#5097709. Modified the procedure                 |
58 --| calc_expression so that expression type tests results are calculated     |
59 --| properly when their refernce tests evaluation is changed but not results.|
60 --| RLNAGARA 27-MAR-2006 Bug#5106039 Modified the proc change_sample_disposition |
61 --|  M.Grosser 04-May-2006:  BUG 5167171 - Forward Port of BUG 5097450       |
62 --|            Modified code to prevent the setting of sample disposition to |
63 --|            In Progress on the addition of a test if the current          |
64 --|            disposition is Pending                                        |
65 --| RAGSRIVA 09-May-2006 Bug# 5195678 Frontport Bug# 5158678                 |
66 --| RLNAGARA 22-May-2006 Bug 3892837 Modified the proc validate_results	     |
67 --| P Lowe   07-Jul-2006:  BUG 5353794 - add test_qty and test_qty_uom       |
68 --|            to cursor for method                                          |
69 --| RLNAGARA 21-Jul-2006  B5396610 Modified the CURSOR c_test_data in the proc|
70 --|                      create_composite_rows
71 --| RLNAGARA 01-Aug-2006 B5416103  Modified the procedure change_disp_for_auto_lot
72 --|                      and composite_and_change_lot so as to check for
73 --|                      control_lot_attrib_ind only while changing the lot status
74 --|                      instead of while changing the disposition of a sample.
75 --| srakrish 	18-Dec-2006     bug 5652689: Corrected the cursor	     |
76 --|					 fetch position			     |
77 --| P Lowe   23-Feb-2007:  BUG 5739844 - Unvalidated Tests are always In-Spec|
78 --| P Lowe   21-Jun-2010:  BUG 9668224 - In case where ct has all optional   |
79 --| tests ONLY and when the parameter Consider Optional Test Results is      |
80 --|  un-checked disposition should stay at 'Complete'                        |
81 --| PLOWE   19-JUL-2010   BUG  9733854 In PROC make_target_spec_the_base_spec|
82 --| For bug 9282975 condition was added to round it only if result is        |
83 --| in number not for char result - for this bug added                       |
84 --| 'and  x_rec.test_type  <> 'T'   '                                        |
85 --| QZENG   25-Oct-2011   Bug 6774613 Modified validate_result procedure to  |
86 --| display an error message for expression tests when it exceeds its test   |
87 --| range limits.	                                                     |
88 --|  P Lowe 22-Nov-2012	Bug 13355408 - added p_called_from_results_form IN   |
89 --| VARCHAR2  DEFAULT 'N' to PROCEDURE calc_expression - used to determine   |
90 --| when called from mass results entry  or results form  as for this bug    |
91 --| we should NOT recalculate test expressions when they have already been   |
92 --| stored in the database when called from results form                     |
93 --|  QZENG  15-Dec-2011 BUG 13476354 -                                       |
94 --|    in procedure create_rslt_and_sprc_rslt_rows, added test_duration to   |
95 --|    cursor c_spec_tests, and set value to result.planned_result_date      |
96 --|    in procedure add_tests_to_sample, added test_duration to cursor       |
97 --|    c_test_method, and set value to result.planned_result_date            |
98 --| QZENG 12-Apr-2012 Bug 13897977                                           |
99 --|    In procedure validate_results, set value to p_result_rec.out_of_spec  |
100 --|    and p_result_rec.in_spec when test is an additional one added manually|
101 --|  P Lowe 29-May-2012	    Bug 14021970 - CALCULATE EXPRESSION FUNCTION     |
102 --|     WILL FILL IN THE WRONG FIELD - added new procedure in                |
103 --|   calc_expression  -                                                     |
104 --|   populate_result_data_points_r to use replicate to calculate correct    |
105 --| test expressions    based on replicate used.                             |
106 --|  P Lowe 17-Sep-2012	    Bug 14349136   OPTIONAL TESTS                    |
107 --|  follow the rules for optional tests and the parameter 'Include Optional |
108 --|  Tests in Sample Disposition  - When we un check the Process Quality     |
109 --|  setup Consider Optional Tests Results  all optional tests populated     |
110 --|  or not will be ignored                                                  |
111 --|  P Lowe 03-Dec-2012	 bug 15885923 rework for Bug 14349136   OPTIONAL TESTS
112 --|  to follow the rules for optional tests and the parameter 'Include Optional
113 --|  Tests in Sample Disposition' in change_sample_disposition               |
114 --|  P Lowe 05-Dec-2012	 BUG 15942047 AND 15885923 rework                    |
115 --|  PM informed dev that "All of the required tests need a result           |
116 --|  evaluation in order for the sample disposition to automatically go      |
117 --|  to Complete."                                                           |
118 --|  In addition if you remove a test result it should go back to In-progress|
119 --|  So for the dispositon changes on the Results form, the parameter        |
120 --|  'Include Optional Tests in Sample Disposition" has no bearing - only    |
121 --|  for FINAL Disposition on the change sample form                         |
122 --|                                                                          |
123 --+==========================================================================+
124 -- End of comments
125 
126 
127 
128 --Start of comments
129 --+========================================================================+
130 --| API Name    : create_rslt_and_spec_rslt_rows                           |
131 --| TYPE        : Group                                                    |
132 --| Notes       : This procedure receives as input sample record and       |
133 --|               creates results and spec results records.                |
134 --|                                                                        |
135 --|               If everything is fine then OUT parameter                 |
136 --|               x_return_status is set to 'S' else appropriate           |
137 --|               error message is put on the stack and error              |
138 --|               is returned.                                             |
139 --|                                                                        |
140 --| HISTORY                                                                |
141 --|    Chetan Nagar	08-Aug-2002	Created.                           |
142 --|                                                                        |
143 --|    LeAta Jackson    01-Oct-2002    Added p_migration parameter. Mig    |
144 --|                                    code will send a Y and skip creating|
145 --|                                    sample_spec_disp record. Migration  |
146 --|                                    only calls this for samples with no |
147 --|                                    result records, but there is a spec.|
148 --|                                                                        |
149 --|    Chetan Nagar	05-Nov-2002	Change disposition of the          |
150 --|      Sampling Event and Event Spec Disp back to In Progress. Also      |
151 --|      set the recomposite flag.                                         |
152 --|                                                                        |
153 --|    LeAta Jackson    07-Nov-2002   Per Karen, no tester id if no result |
154 --|                                                                        |
155 --|    Susan Feinstein  10-Mar-2003   allow retained samples to save record|
156 --|                                   in gmd_sample_spec_disp table        |
157 --|                                                                        |
158 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
159 --|       in select for table - gmd_spec_tests_b                           |
160 --|    Brenda Stone     21-Aug-2003   Changed cursor c_vrs_vw; removed the |
161 --|                                   use of the view and replaced with    |
162 --|                                   UNION ALL of all the spec vr tables  |
163 --|                                   to improve performance               |
164 --|                                                                        |
165 --|    Chetan Nagar	23-Apr-2004	B3584185                           |
166 --|                                     Added gmd_stability_spec_vrs to    |
167 --|                                     the UNION query                    |
168 --|    nsrivast         25-Jul-05       Updated call to api                |
169 --|                                 gmd_samples_grp.update_lot_grade_batch |
170 --|  QZENG     15-Dec-2011: BUG 13476354 - added test_duration to cursor   |
171 --|            c_spec_tests, and set value to result.planned_result_date   |
172 --+========================================================================+
173 -- End of comments
174 
175 PROCEDURE create_rslt_and_spec_rslt_rows
176 (
177   p_sample            IN         GMD_SAMPLES%ROWTYPE
178 , p_migration         IN         VARCHAR2
179 , x_event_spec_disp   OUT NOCOPY GMD_EVENT_SPEC_DISP%ROWTYPE
180 , x_sample_spec_disp  OUT NOCOPY GMD_SAMPLE_SPEC_DISP%ROWTYPE
181 , x_results_tab       OUT NOCOPY GMD_API_PUB.gmd_results_tab
182 , x_spec_results_tab  OUT NOCOPY GMD_API_PUB.gmd_spec_results_tab
183 , x_return_status     OUT NOCOPY VARCHAR2
184 ) IS
185 
186   -- Cursors
187 
188    -- Bug 3088216: added test_qty, test_uom and test_method_id
189   -- Bug 13476354: added test_duration
190   CURSOR c_spec_tests (p_spec_id NUMBER, p_lot_retest_ind VARCHAR2) IS
191   SELECT   st.test_id
192          , st.seq
193          , st.test_replicate
194          , tm.test_kit_inv_item_id
195          , tm.resources, st.viability_duration
196          , st.test_qty
197          , st.test_qty_uom
198          , tm.test_method_id
199          , tm.test_duration
200   FROM   gmd_spec_tests_b st, gmd_test_methods_b tm
201   WHERE  st.spec_id = p_spec_id
202   AND    st.exclude_ind IS NULL
203   AND    ((p_lot_retest_ind IS NULL) OR
204           (st.retest_lot_expiry_ind =  p_lot_retest_ind)
205          )
206   AND    st.test_method_id =  tm.test_method_id
207   ;
208 
209  CURSOR c_vrs_vw ( p_spec_vr_id NUMBER) IS
210   SELECT spec_id
211   FROM   gmd_inventory_spec_vrs
212   where  spec_vr_id = p_spec_vr_id
213   UNION ALL
214   SELECT spec_id
215   FROM   gmd_wip_spec_vrs
216   where  spec_vr_id = p_spec_vr_id
217   UNION ALL
218   SELECT spec_id
219   FROM   gmd_customer_spec_vrs
220   where  spec_vr_id = p_spec_vr_id
221   UNION ALL
222   SELECT spec_id
223   FROM   gmd_supplier_spec_vrs
224   where  spec_vr_id = p_spec_vr_id
225   UNION ALL
226   SELECT spec_id
227   FROM   gmd_monitoring_spec_vrs
228   where  spec_vr_id = p_spec_vr_id
229   -- B3584185 Added following to the union query
230   UNION ALL
231   SELECT spec_id
232   FROM   gmd_stability_spec_vrs
233   where  spec_vr_id = p_spec_vr_id
234 ;
235 
236 
237 
238   -- Local Variables
239   temp_qty                       NUMBER;
240   dummy                          PLS_INTEGER;
241   l_lab_organization_id          NUMBER;
242   l_user_id                      NUMBER;
243   l_seq                          PLS_INTEGER;
244   l_date                         DATE;
245   l_spec_id                      NUMBER(15);
246   l_event_spec_disp_id           NUMBER(15);
247   l_return_status                VARCHAR2(1);
248   l_meaning                      VARCHAR2(80);
249 
250   l_sampling_event               gmd_sampling_events%ROWTYPE;
251   l_event_spec_disp              gmd_event_spec_disp%ROWTYPE;
252   l_sample_spec_disp             gmd_sample_spec_disp%ROWTYPE;
253   l_results                      gmd_results%ROWTYPE;
254   l_spec_results                 gmd_spec_results%ROWTYPE;
255 
256   l_in_sampling_event            gmd_sampling_events%ROWTYPE;
257   l_in_event_spec_disp           gmd_event_spec_disp%ROWTYPE;
258   l_out_event_spec_disp          gmd_event_spec_disp%ROWTYPE;
259   l_out_results                  gmd_results%ROWTYPE;
260 
261   -- Exceptions
262   e_sampling_event_fetch_error   EXCEPTION;
263   e_results_insert_error         EXCEPTION;
264   e_spec_results_insert_error    EXCEPTION;
265   e_event_spec_disp_insert_error EXCEPTION;
266   e_sample_spec_disp_insert_err  EXCEPTION;
267   e_event_spec_disp_fetch_error  EXCEPTION;
268 
269   -- DESC FLEX Enhancement
270   FLEX_EXISTS NUMBER := 0;
271   l_count number ;
272   appl_short_name         varchar2(30) := 'GMD';
273   desc_flex_name          varchar2(30) := 'GMD_QC_RESULTS_FLEX';
274   values_or_ids           varchar2(10) := 'V';
275   validation_date         DATE         := SYSDATE;
276   error_msg               VARCHAR2(5000);
277   n   number;
278   tf  boolean;
279   s number;
280   e number;
281   concatentated_values   VARCHAR2(250);
282   concatentated_ids      VARCHAR2(250);
283   errors_received        EXCEPTION;
284   error_segment          varchar2(30);
285   -- DESC FLEX Enhancement
286 
287 
288 
289 BEGIN
290   --  Initialize API return status to success
291   x_return_status := FND_API.G_RET_STS_SUCCESS;
292 
293   -- DESC FLEX Enhancement
294 	--*********************************************************
295 	--* set the context value                                 *
296 	--*********************************************************
297 	FND_FLEX_DESCVAL.set_context_value('Global Data Elements');
298 
299 	fnd_flex_descval.set_column_value('ATTRIBUTE1', '');
300 	fnd_flex_descval.set_column_value('ATTRIBUTE2', '');
301 	fnd_flex_descval.set_column_value('ATTRIBUTE3', '');
302 	fnd_flex_descval.set_column_value('ATTRIBUTE4', '');
303 	fnd_flex_descval.set_column_value('ATTRIBUTE5', '');
304 	fnd_flex_descval.set_column_value('ATTRIBUTE6', '');
305 	fnd_flex_descval.set_column_value('ATTRIBUTE7', '');
306 	fnd_flex_descval.set_column_value('ATTRIBUTE8', '');
307 	fnd_flex_descval.set_column_value('ATTRIBUTE9', '');
308 	fnd_flex_descval.set_column_value('ATTRIBUTE10', '');
309 	fnd_flex_descval.set_column_value('ATTRIBUTE11', '');
310 	fnd_flex_descval.set_column_value('ATTRIBUTE12', '');
311 	fnd_flex_descval.set_column_value('ATTRIBUTE13', '');
312 	fnd_flex_descval.set_column_value('ATTRIBUTE14', '');
313 	fnd_flex_descval.set_column_value('ATTRIBUTE15', '');
314 	fnd_flex_descval.set_column_value('ATTRIBUTE16', '');
315 	fnd_flex_descval.set_column_value('ATTRIBUTE17', '');
316 	fnd_flex_descval.set_column_value('ATTRIBUTE18', '');
317 	fnd_flex_descval.set_column_value('ATTRIBUTE19', '');
318 	fnd_flex_descval.set_column_value('ATTRIBUTE20', '');
319 	fnd_flex_descval.set_column_value('ATTRIBUTE21', '');
320 	fnd_flex_descval.set_column_value('ATTRIBUTE22', '');
321 	fnd_flex_descval.set_column_value('ATTRIBUTE23', '');
322 	fnd_flex_descval.set_column_value('ATTRIBUTE24', '');
323 	fnd_flex_descval.set_column_value('ATTRIBUTE25', '');
324 	fnd_flex_descval.set_column_value('ATTRIBUTE26', '');
325 	fnd_flex_descval.set_column_value('ATTRIBUTE27', '');
326 	fnd_flex_descval.set_column_value('ATTRIBUTE28', '');
327 	fnd_flex_descval.set_column_value('ATTRIBUTE29', '');
328 	fnd_flex_descval.set_column_value('ATTRIBUTE30', '');
329   -- DESC FLEX Enhancement
330 
331      -- Check that sample is not "Retain"
332      -- Bug 2790099: Retained samples still need an entry in
333      -- GMD_SAMPLE_SPEC_DISP table.
334     -- IF (p_sample.sample_disposition = '0RT') THEN
335     -- Well, if you just want to retain the sample then
336     -- why should I create the results rows. Abort.
337 
338 	-- Not anymore dude?
339 
340 
341     -- GMD_API_PUB.Log_Message('GMD_RETAIN_SAMPLE');
342     -- RAISE FND_API.G_EXC_ERROR;
343     -- END IF;
344 
345   -- Check that we have the Sampling Event
346   -- Now, even in case where there is no Spec for the sample
347   -- there still should be a sampling event as per new directions.
348   IF (p_sample.sampling_event_id IS NULL) THEN
349     -- I need sampling event to know which Spec is used.
350     GMD_API_PUB.Log_Message('GMD_SAMPLING_EVENT_MISSING');
351     RAISE FND_API.G_EXC_ERROR;
352   END IF;
353 
354   -- Now that we have sampling_event_id, fetch the sampling event record
355   -- l_sampling_event.sampling_event_id := p_sample.sampling_event_id;
356   l_in_sampling_event.sampling_event_id := p_sample.sampling_event_id;
357   IF NOT (GMD_SAMPLING_EVENTS_PVT.fetch_row(
358                    p_sampling_events => l_in_sampling_event,
359                    x_sampling_events => l_sampling_event)
360          )
361   THEN
362     -- Fetch Error.
363     RAISE e_sampling_event_fetch_error;
364   END IF;
365 
366   -- If the Sampling Event is set to - Accept, Accept w/Variance
367   -- or Reject then you can not add sample anymore.
368   IF (l_sampling_event.disposition IN ('4A', '5AV', '6RJ')) THEN
369     SELECT meaning
370     INTO   l_meaning
371     FROM   gem_lookups
372     WHERE  lookup_type = 'GMD_QC_SAMPLE_DISP'
373     AND    lookup_code = l_sampling_event.disposition;
374 
375     GMD_API_PUB.Log_Message('GMD_SAMPLING_EVENT_DISPOSED',
376 			    'DISPOSITION', l_meaning);
377     RAISE FND_API.G_EXC_ERROR;
378   END IF;
379 
380   -- All the required validations are over so let's start
381   -- doing some REAL work.
382 
383   -- Get the Spec ID from the SPEC_VR_ID, if SPEC_VR_ID is specified
384   -- in the Sampling Event
385   IF (l_sampling_event.original_spec_vr_id IS NOT NULL) THEN
386     OPEN c_vrs_vw(l_sampling_event.original_spec_vr_id);
387     FETCH c_vrs_vw INTO l_spec_id;
388     IF c_vrs_vw%NOTFOUND THEN
389       -- Now this can not happen that we have spec_vr_id and there is no Spec
390       GMD_API_PUB.Log_Message('GMD_SPEC_NOT_FOUND');
391       RAISE FND_API.G_EXC_ERROR;
392     END IF;
393     CLOSE c_vrs_vw;
394   END IF;
395 
396   l_lab_organization_id := p_sample.lab_organization_id;
397 
398   -- Get the user ID
399   IF p_sample.created_by IS NULL THEN
400     l_user_id  := FND_GLOBAL.user_id;
401   ELSE
402     l_user_id  := p_sample.created_by;
403   END IF;
404 
405   l_date     := SYSDATE;
406   l_seq      := 0;
407   dummy      := 0;
408 
409   -- Check if we already have a record in GMD_EVENT_SPEC_DISP
410   l_event_spec_disp_id := get_current_event_spec_disp_id(
411                                l_sampling_event.sampling_event_id);
412 
413   IF (l_event_spec_disp_id IS NULL) THEN
414     -- This is the first sample so create a record in GMD_EVENT_SPEC_DISP
415 
416     -- Construct the record
417     l_event_spec_disp.sampling_event_id            := l_sampling_event.sampling_event_id;
418     l_event_spec_disp.spec_id                      := l_spec_id;
419     l_event_spec_disp.spec_vr_id                   := l_sampling_event.original_spec_vr_id;
420     l_event_spec_disp.disposition                  := '1P';
421 
422     -- We need to see if we can default something here
423     -- As per 12-Sep-2002 meeting it will be Y for new reocrd
424     l_event_spec_disp.spec_used_for_lot_attrib_ind := 'Y';
425 
426     l_event_spec_disp.delete_mark                  := 0;
427     l_event_spec_disp.creation_date                := l_date;
428     l_event_spec_disp.created_by                   := l_user_id;
429     l_event_spec_disp.last_update_date             := l_date;
430     l_event_spec_disp.last_updated_by              := l_user_id;
431 
432     -- We are ready for insert in GMD_EVENT_SPEC_DISP, so then lets do it.
433     IF NOT(gmd_event_spec_disp_pvt.insert_row(
434                     p_event_spec_disp => l_event_spec_disp,
435                     x_event_spec_disp => l_out_event_spec_disp)
436             )
437     THEN
438       -- Insert Error
439       RAISE e_event_spec_disp_insert_error;
440     END IF;
441     l_event_spec_disp.event_spec_disp_id := l_out_event_spec_disp.event_spec_disp_id;
442   ELSE
443     -- Fetch the GMD_EVENT_SPEC_DISP record
444     -- l_event_spec_disp.event_spec_disp_id := l_event_spec_disp_id;
445     l_in_event_spec_disp.event_spec_disp_id := l_event_spec_disp_id;
446     IF NOT(gmd_event_spec_disp_pvt.fetch_row(
447                     p_event_spec_disp => l_in_event_spec_disp,
448                     x_event_spec_disp => l_event_spec_disp)
449             )
450     THEN
451       -- Insert Error
452       RAISE e_event_spec_disp_fetch_error;
453     END IF;
454   END IF;
455 
456   x_event_spec_disp := l_event_spec_disp;
457 
458   -- Migration calls this procedure for samples which match specs but do not
459   -- already have results.  So sample_spec_disp already has a row from earlier
460   -- in the migration script where the sample record was created.
461   -- Migration is the ONLY valid instance where we can skip inserting into
462   -- sample_spec_disp.
463 
464   IF (p_migration IS NULL OR p_migration <> 'Y')THEN
465     -- Create a record in GMD_SAMPLE_SPEC_DISP
466 
467     -- Construct the record
468     l_sample_spec_disp.event_spec_disp_id     := l_event_spec_disp.event_spec_disp_id;
469     l_sample_spec_disp.sample_id              := p_sample.sample_id;
470     l_sample_spec_disp.delete_mark            := 0;
471     l_sample_spec_disp.creation_date          := l_date;
472     l_sample_spec_disp.created_by             := l_user_id;
473     l_sample_spec_disp.last_update_date       := l_date;
474     l_sample_spec_disp.last_updated_by        := l_user_id;
475 
476      -- Bug 3079877: added planning samples
477     IF   (p_sample.sample_disposition = '0RT')
478       OR (p_sample.sample_disposition = '0PL') THEN
479        l_sample_spec_disp.disposition         := p_sample.sample_disposition;
480     ELSE
481        l_sample_spec_disp.disposition         := '1P';
482     END IF;    -- check for retained sample
483 
484   -- We are ready for insert, so then lets do it.
485     IF NOT(gmd_sample_spec_disp_pvt.Insert_Row(
486                     p_sample_spec_disp => l_sample_spec_disp)
487             )
488     THEN
489       -- Insert Error
490       RAISE e_sample_spec_disp_insert_err;
491     END IF;
492 
493     x_sample_spec_disp:= l_sample_spec_disp;
494   END IF;                  -- end if migration is calling this procedure
495 
496   -- By now the Event Spec Disp record is either created or fetched.
497   -- Now if the Event Spec Disp record has Spec then create
498   -- rows in GMD_RESULTS and GMD_SPEC_RESULTS for all the tests
499 
500 
501   -- Bug 2790099: Retained samples still need an entry in
502   --              GMD_SAMPLE_SPEC_DISP table.
503   -- Bug 3079877: Planned samples should not get an event disp record
504   IF (l_event_spec_disp.spec_id IS NOT NULL)
505      AND (p_sample.sample_disposition <> '0RT')
506      AND (p_sample.sample_disposition <> '0PL') THEN
507 
508     l_spec_id := l_event_spec_disp.spec_id;
509 
510     -- Go through all the tests that are part of the Spec
511     FOR l_spec_test IN c_spec_tests(l_spec_id, p_sample.lot_retest_ind)
512     LOOP
513 
514 	l_count := 0;
515 	FLEX_EXISTS := 0;
516 
517 	-- DESC FLEX Enhancement
518 	IF  FND_FLEX_DESCVAL.validate_desccols(
519 	      appl_short_name,
520 	      desc_flex_name,
521 	      values_or_ids,
522 	      validation_date)
523 	THEN
524   	      FLEX_EXISTS := 1;
525 	      l_count := fnd_flex_descval.segment_count;
526 	      --GMD_API_PUB.Log_Message('Descriptive Flex Field exists.Count ' || l_count);
527 	ELSE
528 	      error_segment := FND_FLEX_DESCVAL.error_segment;
529 	      -- raise errors_received;
530 		null ;
531         END IF;
532 
533 
534        if (l_count > 0) then
535 	   concatentated_values := FND_FLEX_DESCVAL.concatenated_values;
536 	   concatentated_ids := FND_FLEX_DESCVAL.concatenated_ids;
537            --GMD_API_PUB.Log_Message('Descriptive Flex Field. Values ' || concatentated_values);
538 	   --GMD_API_PUB.Log_Message('Descriptive Flex Field. IDs ' || concatentated_ids);
539        end if ;
540 	-- DESC FLEX Enhancement
541 
542 
543 
544       -- Go through as many times the test replicate mentioned in Spec Test.
545       FOR i IN 1..l_spec_test.test_replicate
546       LOOP
547         l_seq := l_seq + 10;
548         dummy := dummy + 1;
549 
550             -- Bug 3088216: test for uom conversion.  If the test uom is not
551             --              convertible to item uom, send error message.
552             -- Bug 3159303: code taken out since convertible test is done between sample
553             --              and item UOM in form GMDQSMPL.fmb
554             --IF (( l_spec_test.test_uom IS NOT NULL) AND ( l_spec_test.test_qty <> 0 )
555               --AND (l_spec_test.test_uom <> p_sample.sample_uom)) THEN
556               --temp_qty := gmicuom.uom_conversion(p_sample.item_id,
557                    --                              0,
558                         --                         p_sample.sample_qty,
559                              --                    p_sample.sample_uom,
560                                   --               l_spec_test.test_uom,
561                                        --          0);
562 
563                --IF (temp_qty < 0) THEN
564                   --OPEN c_item_no(p_sample.item_id);
565                   --FETCH c_item_no INTO l_item_no;
566                   --CLOSE c_item_no;
567                   --GMD_API_PUB.Log_Message('FM_SCALE_BAD_UOM_CONV',
568                       --                    'FROM_UOM',p_sample.sample_uom,
569                           --                'TO_UOM'  ,l_spec_test.test_uom,
570                              --             'ITEM_NO' ,l_item_no);
571                   --RAISE FND_API.G_EXC_ERROR;
572                --END IF;  -- (temp_qty < 0)
573             --END IF;       -- l_spec_test.test_uom <> p_sample.sample_uom
574                -- end bug 3088216
575 
576         -- Construct GMD_RESULTS record
577 
578         l_results.sample_id                     := p_sample.sample_id;
579         l_results.test_id                       := l_spec_test.test_id;
580         l_results.test_replicate_cnt            := i;
581         l_results.lab_organization_id           := l_lab_organization_id;
582         l_results.test_kit_inv_item_id          := l_spec_test.test_kit_inv_item_id;
583         l_results.seq                           := l_spec_test.seq;
584         l_results.delete_mark                   := 0;
585         l_results.creation_date                 := l_date;
586         l_results.created_by                    := l_user_id;
587         l_results.last_updated_by               := l_user_id;
588         l_results.last_update_date              := l_date;
589         l_results.planned_resource              := l_spec_test.resources;
590 
591           -- Bug 3088216: added test_qty, test_uom, test_method_id
592         l_results.test_qty                      := l_spec_test.test_qty;
593         l_results.test_qty_uom                  := l_spec_test.test_qty_uom;
594         l_results.test_method_id                := l_spec_test.test_method_id;
595 
596         IF (nvl(l_spec_test.viability_duration,0)  > 0 ) THEN
597           l_results.test_by_date                  := p_sample.date_drawn
598                                                   + l_spec_test.viability_duration/(60*60*24);
599         END IF;
600 
601         -- Bug 13476354: added planned_result_date
602         IF (nvl(l_spec_test.test_duration,0)  > 0 ) THEN
603           l_results.planned_result_date           := p_sample.date_drawn
604                                                   + l_spec_test.test_duration/(60*60*24);
605         END IF;
606 
607 	-- DESC FLEX Enhancement
608      FOR i IN 1..l_count LOOP
609  	   IF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE_CATEGORY' THEN
610 	     l_results.attribute_category :=  FND_FLEX_DESCVAL.segment_id(i);
611 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE1' THEN
612 	     l_results.attribute1 := FND_FLEX_DESCVAL.segment_id(i);
613 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE2' THEN
614 	     l_results.attribute2 := FND_FLEX_DESCVAL.segment_id(i);
615 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE3' THEN
616 	     l_results.attribute3 := FND_FLEX_DESCVAL.segment_id(i);
617 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE4' THEN
618 	     l_results.attribute4 := FND_FLEX_DESCVAL.segment_id(i);
619 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE5' THEN
620 	     l_results.attribute5 := FND_FLEX_DESCVAL.segment_id(i);
621 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE6' THEN
622 	     l_results.attribute6 := FND_FLEX_DESCVAL.segment_id(i);
623 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE7' THEN
624 	     l_results.attribute7 := FND_FLEX_DESCVAL.segment_id(i);
625 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE8' THEN
626 	     l_results.attribute8 := FND_FLEX_DESCVAL.segment_id(i);
627 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE9' THEN
628 	     l_results.attribute9 := FND_FLEX_DESCVAL.segment_id(i);
629 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE10' THEN
630 	     l_results.attribute10 := FND_FLEX_DESCVAL.segment_id(i);
631 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE11' THEN
632 	     l_results.attribute11 := FND_FLEX_DESCVAL.segment_id(i);
633 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE12' THEN
634 	     l_results.attribute12 := FND_FLEX_DESCVAL.segment_id(i);
635 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE13' THEN
636 	     l_results.attribute13 := FND_FLEX_DESCVAL.segment_id(i);
637 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE14' THEN
638 	     l_results.attribute14 := FND_FLEX_DESCVAL.segment_id(i);
639 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE15' THEN
640 	     l_results.attribute15 := FND_FLEX_DESCVAL.segment_id(i);
641 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE16' THEN
642 	     l_results.attribute16 := FND_FLEX_DESCVAL.segment_id(i);
643 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE17' THEN
644 	     l_results.attribute17 := FND_FLEX_DESCVAL.segment_id(i);
645 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE18' THEN
646 	     l_results.attribute18 := FND_FLEX_DESCVAL.segment_id(i);
647 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE19' THEN
648 	     l_results.attribute19 := FND_FLEX_DESCVAL.segment_id(i);
649 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE20' THEN
650 	     l_results.attribute20 := FND_FLEX_DESCVAL.segment_id(i);
651 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE21' THEN
652 	     l_results.attribute21 := FND_FLEX_DESCVAL.segment_id(i);
653 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE22' THEN
654 	     l_results.attribute22 := FND_FLEX_DESCVAL.segment_id(i);
655 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE23' THEN
656 	     l_results.attribute23 := FND_FLEX_DESCVAL.segment_id(i);
657 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE24' THEN
658 	     l_results.attribute24 := FND_FLEX_DESCVAL.segment_id(i);
659 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE25' THEN
660 	     l_results.attribute25 := FND_FLEX_DESCVAL.segment_id(i);
661 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE26' THEN
662 	     l_results.attribute26 := FND_FLEX_DESCVAL.segment_id(i);
663 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE27' THEN
664 	     l_results.attribute27 := FND_FLEX_DESCVAL.segment_id(i);
665 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE28' THEN
666 	     l_results.attribute28 := FND_FLEX_DESCVAL.segment_id(i);
667 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE29' THEN
668 	     l_results.attribute29 := FND_FLEX_DESCVAL.segment_id(i);
669 	   ELSIF FND_FLEX_DESCVAL.segment_column_name(i) = 'ATTRIBUTE30' THEN
670 	     l_results.attribute30 := FND_FLEX_DESCVAL.segment_id(i);
671 	  END IF;
672        END LOOP;
673 	-- DESC FLEX Enhancement
674 
675 
676 
677         -- We are ready for insert in GMD_RESULTS, so then lets do it.
678         IF NOT(GMD_RESULTS_PVT.Insert_Row(
679                       p_results => l_results,
680                       x_results => l_out_results)
681               )
682         THEN
683           -- Insert Error
684           RAISE e_results_insert_error;
685         END IF;
686 	l_results.RESULT_ID := l_out_results.RESULT_ID;
687 
688         x_results_tab(dummy) := l_results;
689 
690         -- Now, Construct GMD_SPEC_RESULTS record
691 
692         l_spec_results.event_spec_disp_id       := l_event_spec_disp.event_spec_disp_id;
693         l_spec_results.result_id                := l_results.result_id;
694         l_spec_results.delete_mark              := 0;
695         l_spec_results.creation_date            := l_date;
696         l_spec_results.created_by               := l_user_id;
697         l_spec_results.last_updated_by          := l_user_id;
698         l_spec_results.last_update_date         := l_date;
699 
700         -- We are ready for insert in GMD_SPEC_RESULTS, so then lets do it.
701         IF NOT(gmd_spec_results_pvt.Insert_Row(p_spec_results => l_spec_results))
702         THEN
703           -- Insert Error
704           RAISE e_spec_results_insert_error;
705         END IF;
706 
707         x_spec_results_tab(dummy) := l_spec_results;
708 
709       END LOOP;  -- Test Replicate Loop
710 
711     END LOOP;  -- Spec Tests Loop
712 
713   END IF; -- We have the Spec from Sampling Event
714 
715   -- Bug 2790099: Retained samples still need an entry in
716   --              GMD_SAMPLE_SPEC_DISP table.
717   -- Bug 3079877: iPlanning samples do not need to be recomposited
718   IF     (p_sample.sample_disposition <> '0RT')
719      AND (p_sample.sample_disposition <> '0PL') THEN
720      -- Since we altered the Sampling Event set the recomposite_flag to 'Y'
721      IF (nvl(l_sampling_event.sample_active_cnt, 0) > 1) THEN
722        se_recomposite_required (  p_sampling_event_id  => l_sampling_event.sampling_event_id
723                                 , p_event_spec_disp_id => l_event_spec_disp.event_spec_disp_id
724                                 , x_return_status      => l_return_status
725                                );
726        IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
727          RAISE FND_API.G_EXC_ERROR;
728        END IF;
729      END IF;
730 
731      IF NOT (l_event_spec_disp.disposition IN ('1P', '2I')) THEN
732        -- Set the disposition of the Event spec disp
733        -- back to "In Progress"
734        UPDATE gmd_event_spec_disp
735        SET    disposition = '2I',
736               last_updated_by = l_user_id,
737               last_update_date = l_date
738        WHERE  event_spec_disp_id = l_event_spec_disp.event_spec_disp_id
739        ;
740      END IF;
741 
742      IF NOT (l_sampling_event.disposition IN ('1P', '2I')) THEN
743        -- Set the disposition of the Sampling Event
744        -- back to "In Progress"
745        UPDATE gmd_sampling_events
746        SET    disposition = '2I',
747               last_updated_by = l_user_id,
748               last_update_date = l_date
749        WHERE  sampling_event_id = l_sampling_event.sampling_event_id
750        ;
751      END IF;
752 
753   END IF;  -- (p_sample.sample_disposition <> '0RT')
754 
755   -- All systems GO...
756 
757 EXCEPTION
758   WHEN    errors_Received then
759 	error_msg := fnd_flex_Descval.error_message ;
760 	s := 1;
761 	e := 200;
762 	GMD_API_PUB.Log_Message('GMD_API_ERROR',
763                             'PACKAGE','CREATE_RSLT_AND_SPEC_RSLT_ROWS',
764                             'ERROR', error_msg);
765   WHEN    FND_API.G_EXC_ERROR
766        OR e_sampling_event_fetch_error
767        OR e_results_insert_error
768        OR e_spec_results_insert_error
769        OR e_event_spec_disp_insert_error
770        OR e_sample_spec_disp_insert_err
771        OR e_event_spec_disp_fetch_error
772   THEN
773     x_return_status := FND_API.G_RET_STS_ERROR ;
774   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
775     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
776   WHEN OTHERS THEN
777     GMD_API_PUB.Log_Message('GMD_API_ERROR',
778                             'PACKAGE','CREATE_RSLT_AND_SPEC_RSLT_ROWS',
779                             'ERROR', SUBSTR(SQLERRM,1,100));
780     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
781 
782 END create_rslt_and_spec_rslt_rows;
783 
784 
785 
786 
787 
788 --Start of comments
789 --+========================================================================+
790 --| API Name    : delete_rslt_and_spec_rslt_rows                           |
791 --| TYPE        : Group                                                    |
792 --| Notes       : This routine is called when the sample disposition is    |
793 --|               changed from "Pending" to "Retain".  In this case the    |
794 --|               result and spec result rows that were created when the   |
795 --|               sample was pending is deleted.                           |
796 --|                                                                        |
797 --| HISTORY                                                                |
798 --|    Chetan Nagar	14-May-2003	Created.                           |
799 --+========================================================================+
800 -- End of comments
801 
802 PROCEDURE delete_rslt_and_spec_rslt_rows
803 (
804   p_sample_id     IN         NUMBER
805 , x_return_status OUT NOCOPY VARCHAR2
806 ) IS
807 BEGIN
808 
809   IF (l_debug = 'Y') THEN
810     gmd_debug.put_line('Entering procedure DELETE_RSLT_AND_SPEC_RSLT_ROWS');
811   END IF;
812 
813   --  Initialize API return status to success
814   x_return_status := FND_API.G_RET_STS_SUCCESS;
815 
816   DELETE gmd_spec_results
817   WHERE result_id IN (SELECT result_id
818                       FROM   gmd_results
819                       WHERE  sample_id = p_sample_id)
820   ;
821 
822   IF (l_debug = 'Y') THEN
823     gmd_debug.put_line('Rows deleted from gmd_spec_results: '|| SQL%ROWCOUNT);
824   END IF;
825 
826   DELETE gmd_results
827   WHERE  sample_id = p_sample_id
828   ;
829 
830   IF (l_debug = 'Y') THEN
831     gmd_debug.put_line('Rows deleted from gmd_results: '|| SQL%ROWCOUNT);
832   END IF;
833 
834   IF (l_debug = 'Y') THEN
835     gmd_debug.put_line('Leaving procedure DELETE_RSLT_AND_SPEC_RSLT_ROWS');
836   END IF;
837 
838 EXCEPTION
839   WHEN FND_API.G_EXC_ERROR THEN
840     x_return_status := FND_API.G_RET_STS_ERROR ;
841   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
842     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
843   WHEN OTHERS THEN
844     GMD_API_PUB.Log_Message('GMD_API_ERROR',
845                             'PACKAGE','DELETE_RSLT_AND_SPEC_RSLT_ROWS',
846                             'ERROR', SUBSTR(SQLERRM,1,100));
847     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
848 
849 END delete_rslt_and_spec_rslt_rows;
850 
851 
852 
853 
854 
855 --Start of comments
856 --+========================================================================+
857 --| API Name    : get_current_event_spec_disp_id                           |
858 --| TYPE        : Group                                                    |
859 --| Notes       : This procedure finds out current event_spec_disp_id      |
860 --|               for a given sampling_event_id.                           |
861 --|                                                                        |
862 --|                                                                        |
863 --| HISTORY                                                                |
864 --|    Chetan Nagar	12-Sep-2002	Created.                           |
865 --|                                                                        |
866 --+========================================================================+
867 -- End of comments
868 
869 FUNCTION get_current_event_spec_disp_id
870 (
871   p_sampling_event_id     IN  NUMBER
872 ) RETURN NUMBER IS
873 
874   -- Cursors
875   CURSOR c_event_disp(p_sampling_event_id NUMBER) IS
876   SELECT event_spec_disp_id
877   FROM   gmd_event_spec_disp
878   WHERE  sampling_event_id            = p_sampling_event_id
879   AND    spec_used_for_lot_attrib_ind = 'Y'
880   AND    delete_mark                  = 0
881   ;
882 
883   -- Local Variables
884   l_dummy                         NUMBER(15);
885 
886 BEGIN
887 
888   OPEN c_event_disp(p_sampling_event_id);
889   FETCH c_event_disp INTO l_dummy;
890   CLOSE c_event_disp;
891 
892   RETURN l_dummy;
893 
894 EXCEPTION
895   WHEN OTHERS THEN
896     RETURN NULL;
897 
898 END get_current_event_spec_disp_id;
899 
900 
901 
902 --Start of comments
903 --+========================================================================+
904 --| API Name    : compare_rslt_and_spec                                    |
905 --| TYPE        : Group                                                    |
906 --| Notes       : This procedure finds out Tests that are not in the given |
907 --|               result set and are in the Spec given.                    |
908 --|                                                                        |
909 --|               Example,                                                 |
910 --|                                                                        |
911 --|               Sample ID - 000001                                       |
912 --|                                                                        |
913 --|                 Tests                                                  |
914 --|                 -----                                                  |
915 --|                 T1                                                     |
916 --|                 T2                                                     |
917 --|                 T4                                                     |
918 --|                 T5                                                     |
919 --|                                                                        |
920 --|               User wants to compare this sample to Spec - S2           |
921 --|                                                                        |
922 --|               S2 has following tests                                   |
923 --|                                                                        |
924 --|                 Tests                                                  |
925 --|                 -----                                                  |
926 --|                 T1                                                     |
927 --|                 T3                                                     |
928 --|                 T6                                                     |
929 --|                                                                        |
930 --|               So when this proceudre is called, it will return a       |
931 --|               table of tests as follows:                               |
932 --|                                                                        |
933 --|               Tests in S2 and not in result set for Sample ID - 000001 |
934 --|                                                                        |
935 --|               Tests Missing                                            |
936 --|               -------------                                            |
937 --|               T3                                                       |
938 --|               T6                                                       |
939 --|                                                                        |
940 --| HISTORY                                                                |
941 --|    Chetan Nagar	08-Aug-2002	Created.                           |
942 --|                                                                        |
943 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
944 --|       in select for table - gmd_spec_tests_b                           |
945 --|                                                                        |
946 --+========================================================================+
947 -- End of comments
948 
949 PROCEDURE compare_rslt_and_spec
950 (
951   p_sample_id     IN         NUMBER
952 , p_spec_id       IN         NUMBER
953 , x_test_ids      OUT NOCOPY GMD_API_PUB.number_tab
954 , x_return_status OUT NOCOPY VARCHAR2
955 ) IS
956 
957   -- Cursors
958   CURSOR c_new_spec_tests (p_spec_id NUMBER) IS
959   SELECT st.test_id
960   FROM   gmd_spec_tests_b st
961   WHERE  st.spec_id = p_spec_id
962   AND    st.exclude_ind IS NULL
963   AND    st.test_id NOT IN
964     (SELECT r.test_id
965      FROM   gmd_results r
966      WHERE  r.sample_id = p_sample_id)
967   ORDER BY st.seq
968   ;
969 
970   -- Local Variables
971   i                              PLS_INTEGER;
972 
973 BEGIN
974   --  Initialize API return status to success
975   x_return_status := FND_API.G_RET_STS_SUCCESS;
976 
977   i := 0;
978 
979   -- Go throug all the tests that are part of the Spec
980   FOR l_spec_test IN c_new_spec_tests(p_spec_id)
981   LOOP
982     -- Found the test, add it to the table.
983     i := i + 1;
984     x_test_ids(i) := l_spec_test.test_id;
985 
986   END LOOP;
987 
988   -- All systems GO...
989 
990 EXCEPTION
991   WHEN    FND_API.G_EXC_ERROR
992   THEN
993     x_return_status := FND_API.G_RET_STS_ERROR ;
994   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
995     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
996   WHEN OTHERS THEN
997     GMD_API_PUB.Log_Message('GMD_API_ERROR',
998                             'PACKAGE','COMPARE_RSLT_AND_SPEC',
999                             'ERROR', SUBSTR(SQLERRM,1,100));
1000     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1001 
1002 END compare_rslt_and_spec;
1003 
1004 
1005 
1006 --Start of comments
1007 --+========================================================================+
1008 --| API Name    : compare_cmpst_rslt_and_spec                              |
1009 --| TYPE        : Group                                                    |
1010 --| Notes       : This procedure finds out Tests that are not in the given |
1011 --|               composite result set and are in the Spec given.          |
1012 --|                                                                        |
1013 --|               Example,                                                 |
1014 --|                                                                        |
1015 --|               Composite Results                                        |
1016 --|                                                                        |
1017 --|                 Tests                                                  |
1018 --|                 -----                                                  |
1019 --|                 T1                                                     |
1020 --|                 T2                                                     |
1021 --|                 T4                                                     |
1022 --|                 T5                                                     |
1023 --|                                                                        |
1024 --|               User wants to compare this sample to Spec - S2           |
1025 --|                                                                        |
1026 --|               S2 has following tests                                   |
1027 --|                                                                        |
1028 --|                 Tests                                                  |
1029 --|                 -----                                                  |
1030 --|                 T1                                                     |
1031 --|                 T3                                                     |
1032 --|                 T6                                                     |
1033 --|                                                                        |
1034 --|               So when this proceudre is called, it will return a       |
1035 --|               table of tests as follows:                               |
1036 --|                                                                        |
1037 --|               Tests in S2 and not in composite result set              |
1038 --|                                                                        |
1039 --|               Tests Missing                                            |
1040 --|               -------------                                            |
1041 --|               T3                                                       |
1042 --|               T6                                                       |
1043 --|                                                                        |
1044 --| HISTORY                                                                |
1045 --|    Chetan Nagar	16-Sep-2002	Created.                           |
1046 --|                                                                        |
1047 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
1048 --|       in select for table - gmd_spec_tests_b                           |
1049 --|                                                                        |
1050 --+========================================================================+
1051 -- End of comments
1052 
1053 PROCEDURE compare_cmpst_rslt_and_spec
1054 (
1055   p_composite_spec_disp_id IN         NUMBER
1056 , p_spec_id                IN         NUMBER
1057 , x_test_ids               OUT NOCOPY GMD_API_PUB.number_tab
1058 , x_return_status          OUT NOCOPY VARCHAR2
1059 ) IS
1060 
1061   -- Cursors
1062   CURSOR c_new_spec_tests (p_composite_spec_disp_id NUMBER) IS
1063   SELECT st.test_id
1064   FROM   gmd_spec_tests_b st
1065   WHERE  st.spec_id = p_spec_id
1066   AND    st.exclude_ind IS NULL
1067   AND    st.test_id NOT IN
1068     (SELECT cr.test_id
1069      FROM   gmd_composite_results cr
1070      WHERE  cr.composite_spec_disp_id = p_composite_spec_disp_id)
1071   ORDER BY st.seq
1072   ;
1073 
1074   -- Local Variables
1075   i                              PLS_INTEGER;
1076 
1077 BEGIN
1078   --  Initialize API return status to success
1079   x_return_status := FND_API.G_RET_STS_SUCCESS;
1080 
1081   i := 0;
1082 
1083   -- Go throug all the tests that are part of the Spec
1084   FOR l_spec_test IN c_new_spec_tests(p_composite_spec_disp_id)
1085   LOOP
1086     -- Found the test, add it to the table.
1087     i := i + 1;
1088     x_test_ids(i) := l_spec_test.test_id;
1089 
1090   END LOOP;
1091 
1092   -- All systems GO...
1093 
1094 EXCEPTION
1095   WHEN    FND_API.G_EXC_ERROR
1096   THEN
1097     x_return_status := FND_API.G_RET_STS_ERROR ;
1098   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1099     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1100   WHEN OTHERS THEN
1101     GMD_API_PUB.Log_Message('GMD_API_ERROR',
1102                             'PACKAGE','compare_cmpst_rslt_and_spec',
1103                             'ERROR', SUBSTR(SQLERRM,1,100));
1104     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1105 
1106 END compare_cmpst_rslt_and_spec;
1107 
1108 
1109 
1110 
1111 --Start of comments
1112 --+========================================================================+
1113 --| API Name    : rslt_is_in_spec                                          |
1114 --| TYPE        : Group                                                    |
1115 --| Notes       : This function finds out if the result supplied is        |
1116 --|               IN-SPEC as per the limits set in the specification.      |
1117 --|                                                                        |
1118 --|               Example,                                                 |
1119 --|                                                                        |
1120 --|               Test - pH Test                                           |
1121 --|                                                                        |
1122 --|               Result Value - 7.3                                       |
1123 --|                                                                        |
1124 --|               Spec Test Limits                                         |
1125 --|               ----------------                                         |
1126 --|                     Min - 4                                            |
1127 --|                  Target - 7                                            |
1128 --|                     Max - 9                                            |
1129 --|                                                                        |
1130 --|               So in this case the function will return TRUE since the  |
1131 --|               result value for pH Test is 7.3 and it is withing the    |
1132 --|               limits set in Specification.                             |
1133 --|                                                                        |
1134 --| PARAMETERS  : 1. p_spec_id         - Spec against which result is      |
1135 --|                                      compared.                         |
1136 --|               2. p_test_id         - Test for which the result         |
1137 --|                                      is supplied.                      |
1138 --|               3. p_rslt_value_num  - Value of the test result, passed  |
1139 --|                                      when the test type is             |
1140 --|                                      one of N, E, T                    |
1141 --|               4. p_rslt_value_char - Passed when the test type is L    |
1142 --|                                                                        |
1143 --| HISTORY                                                                |
1144 --|    Chetan Nagar	09-Aug-2002	Created.                           |
1145 --|    LeAta Jackson    08-Nov-2002     Karen said unvalidated tests       |
1146 --|                            should not automatically be in spec.        |
1147 --|                                                                        |
1148 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
1149 --|       in select for table - gmd_spec_tests_b                           |
1150 --|    srakrish 	18-Dec-2006     bug 5652689: Corrected the cursor  |
1151 --|					 fetch position			   |
1152 --+========================================================================+
1153 -- End of comments
1154 
1155 FUNCTION rslt_is_in_spec
1156 (
1157   p_spec_id         IN  NUMBER
1158 , p_test_id         IN  NUMBER
1159 , p_rslt_value_num  IN  NUMBER
1160 , p_rslt_value_char IN  VARCHAR2
1161 )
1162 RETURN VARCHAR2 IS
1163 
1164   -- Cursors
1165   CURSOR c_spec_test_val (p_spec_id NUMBER, p_test_id NUMBER) IS
1166   SELECT t.test_type, st.min_value_num, st.max_value_num, st.target_value_char
1167   FROM   gmd_qc_tests_b t, gmd_spec_tests_b st
1168   WHERE  t.test_id= st.test_id
1169   AND    st.exclude_ind IS NULL
1170   AND    st.spec_id = p_spec_id
1171   AND    st.test_id = p_test_id
1172   ;
1173 
1174   -- When test type is T, get the NUM seq if only Char value is passed
1175   CURSOR c_text_to_num(p_test_id NUMBER, p_value_char VARCHAR2) IS
1176   SELECT text_range_seq
1177   FROM   gmd_qc_test_values_b
1178   WHERE  test_id        = p_test_id
1179   AND    value_char     = p_value_char
1180   ;
1181 
1182   -- When test type is L, check that there exits a subranges that covers the result num
1183   CURSOR c_subranges(p_test_id NUMBER, p_num NUMBER) IS
1184   SELECT 1
1185   FROM   gmd_qc_test_values_b
1186   WHERE  test_id = p_test_id
1187   AND    nvl(min_num, p_num) <= p_num
1188   AND    nvl(max_num, p_num) >= p_num
1189   ;
1190 
1191 
1192 
1193   -- Local Variables
1194   x_in_spec                     VARCHAR2(1);
1195   l_test_min                    NUMBER;
1196   l_test_max                    NUMBER;
1197   l_rslt_value_num              NUMBER;
1198   dummy                         PLS_INTEGER;
1199 
1200   l_values                      c_spec_test_val%ROWTYPE;
1201 
1202 BEGIN
1203 
1204   IF (l_debug = 'Y') THEN
1205      gmd_debug.put_line('Entering procedure RSLT_IS_IN_SPEC');
1206   END IF;
1207 
1208   IF (l_debug = 'Y') THEN
1209      gmd_debug.put_line('Spec ID: ' || p_spec_id || ' Test ID: ' || p_test_id ||
1210             ' Num Result: ' || p_rslt_value_num || ' Char Result: ' || p_rslt_value_char);
1211   END IF;
1212 
1213   -- Initialize return status as Out-Of-Spec
1214   x_in_spec := NULL;
1215 
1216   -- Check that the required parameters are passed.
1217   IF (p_rslt_value_num IS NULL AND p_rslt_value_char IS NULL) OR
1218      (p_test_id IS NULL) OR
1219      (p_spec_id IS NULL)
1220   THEN
1221     RETURN NULL;
1222   END IF;
1223     --srakrish bug 5652689:
1224     OPEN c_spec_test_val(p_spec_id, p_test_id);
1225     FETCH c_spec_test_val INTO l_values;
1226     IF c_spec_test_val%NOTFOUND THEN
1227       CLOSE c_spec_test_val;
1228       RETURN NULL;
1229     END IF;
1230     CLOSE c_spec_test_val;
1231 
1232   IF (l_values.test_type = 'U') THEN
1233   -- Find out min, max, and target from the Spec test
1234     -- Unvalidated Tests are always In-Spec
1235    RETURN 'Y';   -- Bug 5739844
1236 
1237   ELSE
1238     --srakrish bug 5652689: Moved the cursor fetch to before the If condition
1239     /*OPEN c_spec_test_val(p_spec_id, p_test_id);
1240     FETCH c_spec_test_val INTO l_values;
1241     IF c_spec_test_val%NOTFOUND THEN
1242       CLOSE c_spec_test_val;
1243       RETURN NULL;
1244     END IF;
1245     CLOSE c_spec_test_val; */
1246 
1247     IF (l_debug = 'Y') THEN
1248        gmd_debug.put_line('Test Type: ' || l_values.test_type ||
1249                      ' Min: ' || l_values.min_value_num ||
1250                      ' Max: ' || l_values.max_value_num ||
1251                      ' Target: ' || l_values.target_value_char);
1252     END IF;
1253 
1254 
1255     IF (l_values.test_type = 'N' OR
1256          l_values.test_type = 'E' OR
1257          l_values.test_type = 'T'
1258         ) THEN
1259 
1260       -- Numeric, Expression or Text Range
1261 
1262 
1263 
1264       l_rslt_value_num := p_rslt_value_num;
1265       -- If Text Range and if the seq is not supplied then find one using Char value
1266       IF (l_values.test_type = 'T' AND l_rslt_value_num IS NULL) THEN
1267         IF (l_debug = 'Y') THEN
1268           gmd_debug.put_line('For test type T, the NUM value is missing.');
1269         END IF;
1270         OPEN c_text_to_num(p_test_id, p_rslt_value_char);
1271         FETCH c_text_to_num INTO l_rslt_value_num;
1272         IF (c_text_to_num%NOTFOUND) THEN
1273             IF (l_debug = 'Y') THEN
1274          			 gmd_debug.put_line('For test type T, the NUM value is NOT retrieved - early exit RETURN NULL ' );
1275         	END IF;
1276 
1277           CLOSE c_text_to_num;
1278           RETURN NULL;
1279         END IF;
1280 
1281         IF (l_debug = 'Y') THEN
1282           gmd_debug.put_line('For test type T, retrieved the NUM value: ' || l_rslt_value_num);
1283         END IF;
1284         CLOSE c_text_to_num;
1285 
1286       END IF;       -- end if test type is T and no seq number is given
1287 
1288      	 IF (l_values.min_value_num <= l_rslt_value_num AND
1289           l_rslt_value_num <= l_values.max_value_num) THEN
1290 
1291 		        -- The result is In-Spec
1292 		        x_in_spec := 'Y';
1293 
1294 		        IF (l_debug = 'Y') THEN
1295 		          gmd_debug.put_line('For N, E, and T it is IN-SPEC');
1296 		        END IF;
1297      	 END IF;
1298 
1299 
1300 
1301     ELSIF (l_values.test_type = 'L') THEN
1302 
1303       -- Numeric Range with Display Label
1304       IF (nvl(l_values.min_value_num, p_rslt_value_num) <= p_rslt_value_num AND
1305         p_rslt_value_num <= nvl(l_values.max_value_num, p_rslt_value_num)) THEN
1306 
1307         IF (l_debug = 'Y') THEN
1308           gmd_debug.put_line('For L it is IN-SPEC AT FIRST. Lets check individual ranges.');
1309         END IF;
1310         -- num range with display can have holes in the subranges
1311         -- check that the result does not fall into one of those holes
1312         OPEN c_subranges(p_test_id, p_rslt_value_num) ;
1313         FETCH c_subranges INTO dummy;
1314         IF c_subranges%FOUND THEN
1315           IF (l_debug = 'Y') THEN
1316    	    		gmd_debug.put_line('For L it is also in one of the subranges so it is IN-SPEC.');
1317           END IF;
1318           x_in_spec := 'Y';
1319         END IF;
1320         CLOSE c_subranges;
1321       END IF;
1322 
1323     ELSIF (l_values.test_type = 'V') THEN
1324 
1325       -- List of Values
1326       IF (p_rslt_value_char = l_values.target_value_char) THEN
1327         IF (l_debug = 'Y') THEN
1328           gmd_debug.put_line('For V it is equal to Target and so it is IN-SPEC');
1329         END IF;
1330         x_in_spec := 'Y';
1331       END IF;
1332 
1333     END IF;         -- end test type CASEs
1334 
1335 END IF;             -- end if test is nonvalidate or not
1336 
1337 -- did not get here
1338  IF (l_debug = 'Y') THEN
1339      gmd_debug.put_line('Exiting Procedure RSLT_IS_IN_SPEC - value of   x_in_sepc is ' ||  x_in_spec );
1340   END IF;
1341 
1342 RETURN x_in_spec;
1343 
1344 EXCEPTION
1345   WHEN OTHERS THEN
1346     IF (l_debug = 'Y') THEN
1347      gmd_debug.put_line('Exiting Procedure RSLT_IS_IN_SPEC with exception and returning NULL ');
1348   END IF;
1349 
1350     RETURN NULL;
1351 
1352 END rslt_is_in_spec;
1353 
1354 
1355 --Start of comments
1356 --+========================================================================+
1357 --| API Name    : add_tests_to_sample                                      |
1358 --| TYPE        : Group                                                    |
1359 --| Notes       : This function received as input table of test IDs that   |
1360 --|               are to be added to a given sample.                       |
1361 --|                                                                        |
1362 --|               The function will insert rows into GMD_RESULTS and       |
1363 --|               GMD_SPEC_RESULTS.                                        |
1364 --|                                                                        |
1365 --| PARAMETERS  :                                                          |
1366 --|                                                                        |
1367 --| 1. p_sample - Sample record for which tests are added to results       |
1368 --| 2. p_test_ids - Table of test ids to be added to the result            |
1369 --| 3. p_event_spec_disp_id  - Event Spec                                  |
1370 --|                                                                        |
1371 --| HISTORY                                                                |
1372 --|    Chetan Nagar	21-Aug-2002   Created.                             |
1373 --|                                                                        |
1374 --|    LeAta Jackson    07-Nov-2002   Per Karen, no tester id if no result |
1375 --|                                                                        |
1376 --|    Chetan Nagar	05-Dec-2002   Assign the OUT variables at proper   |
1377 --|                                   index position.                      |
1378 --|                                                                        |
1379 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
1380 --|       in select for table - gmd_spec_tests_b                           |
1381 --|  M.Grosser 04-May-2006:  BUG 5167171 - Forward Port of BUG 5097450     |
1382 --|            Modified code to prevent the setting of sample disposition  |
1383 --|            to In Progress on the addition of a test if the current     |
1384 --|            disposition is Pending                                      |
1385 --|  P Lowe    07-Jul-2006:  BUG 5353794 - add test_qty and test_qty_uom   |
1386 --|            to cursor for method                                        |
1387 --|  QZENG     15-Dec-2011: BUG 13476354 - added test_duration to cursor   |
1388 --|            c_test_method, and set value to result.planned_result_date  |
1389 --+========================================================================+
1390 -- End of comments
1391 
1392 PROCEDURE add_tests_to_sample
1393 (
1394   p_sample             IN         GMD_SAMPLES%ROWTYPE
1395 , p_test_ids           IN         GMD_API_PUB.number_tab
1396 , p_event_spec_disp_id IN         NUMBER
1397 , x_results_tab        OUT NOCOPY GMD_API_PUB.gmd_results_tab
1398 , x_spec_results_tab   OUT NOCOPY GMD_API_PUB.gmd_spec_results_tab
1399 , x_return_status      OUT NOCOPY VARCHAR2
1400 , p_test_qty           IN         NUMBER   default NULL
1401 , p_test_qty_uom       IN         VARCHAR2 default NULL
1402 )
1403 IS
1404 
1405   -- Cursors
1406 	  CURSOR c_test_method (p_test_id NUMBER) IS
1407 	  SELECT tm.test_kit_inv_item_id,
1408                  tm.test_replicate,
1409                  tm.test_method_id, -- 5353794
1410                  tm.test_qty,
1411                  tm.test_qty_uom,
1412                  tm.test_duration   -- Bug 13476354
1413 	  FROM   gmd_qc_tests_b t, gmd_test_methods_b tm
1414 	  WHERE  t.test_id =  p_test_id
1415 	  AND    t.test_method_id = tm.test_method_id
1416 	  ;
1417 
1418          -- Begin 3903309
1419 	 CURSOR c_get_sample_spec_disp (p_sample_id number) is
1420 	 SELECT disposition
1421          FROM gmd_sample_spec_disp
1422          WHERE sample_id = p_sample_id
1423          AND event_spec_disp_id = p_event_spec_disp_id ;
1424 
1425          l_sample_spec_disp varchar2(3) ;
1426 	 -- End 3903309
1427 
1428 	  -- Local Variables
1429 	  l_test_method_id               NUMBER;
1430 	  l_user_id                      NUMBER;
1431 	  l_seq                          PLS_INTEGER;
1432 	  l_date                         DATE;
1433 	  l_lab_organization             NUMBER;
1434 	  l_test_kit_inv_item_id             NUMBER;
1435 	  l_next_test_replicate_cnt      PLS_INTEGER;
1436 	  l_test_added_flag              BOOLEAN := FALSE;
1437 	  l_test_type                    VARCHAR2(2);
1438 	  l_additional_test_ind          VARCHAR2(1);
1439 	  l_replicate                    NUMBER(5);
1440 	  dummy                          PLS_INTEGER;
1441 	  out_var_idx                    PLS_INTEGER := 0;
1442 	  l_meaning                      VARCHAR2(80);
1443 	  l_viability_duration           NUMBER;
1444 	  l_resources                    GMD_TEST_METHODS_B.RESOURCES%TYPE;
1445 
1446 	  l_sample                       GMD_SAMPLES%ROWTYPE;
1447 	  l_sampling_event               GMD_SAMPLING_EVENTS%ROWTYPE;
1448 	  l_results                      GMD_RESULTS%ROWTYPE;
1449 	  l_spec_results                 GMD_SPEC_RESULTS%ROWTYPE;
1450 	  l_event_spec_disp              GMD_EVENT_SPEC_DISP%ROWTYPE;
1451 
1452 	  l_in_sampling_event            GMD_SAMPLING_EVENTS%ROWTYPE;
1453 	  l_in_event_spec_disp           GMD_EVENT_SPEC_DISP%ROWTYPE;
1454 	  l_out_results                  GMD_RESULTS%ROWTYPE;
1455 
1456 	  -- Exceptions
1457 	  e_results_insert_error         EXCEPTION;
1458 	  e_spec_results_insert_error    EXCEPTION;
1459 	  e_samples_fetch_error          EXCEPTION;
1460 	  e_sampling_event_fetch_error   EXCEPTION;
1461 	  e_event_spec_fetch_error       EXCEPTION;
1462     l_lab_organization_id          NUMBER;
1463 
1464     l_test_qty                     NUMBER;   -- 5353794
1465     l_test_qty_uom                 VARCHAR2(3); -- 5353794
1466     l_test_duration                NUMBER;   -- Bug 13476354
1467 
1468     BEGIN
1469 
1470 	  IF (l_debug = 'Y') THEN
1471 	     gmd_debug.put_line('Entering Procedure: ADD_TESTS_TO_SAMPLE');
1472 	  END IF;
1473 
1474 	  --  Initialize API return status to success
1475 	  x_return_status := FND_API.G_RET_STS_SUCCESS;
1476 
1477 	  l_user_id  := FND_GLOBAL.user_id;
1478 	  l_date     := SYSDATE;
1479 
1480 	  -- Fetch the Sample Record
1481 	  IF NOT (gmd_samples_pvt.fetch_row(
1482 			 p_samples => p_sample,
1483 			 x_samples => l_sample)
1484 		 )
1485 	  THEN
1486 	    -- Fetch Error.
1487 	    RAISE e_samples_fetch_error;
1488 	  END IF;
1489 
1490 
1491 	  -- Fetch the Sampling Event Record
1492 	  l_in_sampling_event.sampling_event_id := l_sample.sampling_event_id;
1493 	  IF NOT (gmd_sampling_events_pvt.fetch_row(
1494 			 p_sampling_events => l_in_sampling_event,
1495 			 x_sampling_events => l_sampling_event)
1496 		 )
1497 	  THEN
1498 	    -- Fetch Error.
1499 	    RAISE e_sampling_event_fetch_error;
1500 	  END IF;
1501 
1502          -- Begin 3903309
1503 	    OPEN c_get_sample_spec_disp (l_sample.sample_id) ;
1504 	    FETCH c_get_sample_spec_disp INTO l_sample_spec_disp ;
1505 	    CLOSE c_get_sample_spec_disp ;
1506          -- End 3903309
1507 
1508 	  -- If the Event Spec is set to - Accept, Accept w/Variance
1509 	  -- or Reject then you can not add sample anymore.
1510 	  -- IF (l_sampling_event.disposition IN ('4A', '5AV', '6RJ')) THEN
1511 	   IF (l_sample_spec_disp IN ('4A', '5AV', '6RJ')) THEN   -- 3903309
1512 	    SELECT meaning
1513 	    INTO   l_meaning
1514 	    FROM   gem_lookups
1515 	    WHERE  lookup_type = 'GMD_QC_SAMPLE_DISP'
1516 	    AND    lookup_code = l_sample_spec_disp;  -- 3903309
1517 
1518 	    GMD_API_PUB.Log_Message('GMD_CANT_ADD_TEST');
1519 	    RAISE FND_API.G_EXC_ERROR;
1520 	   END IF;
1521 
1522 	  -- Fetch the Event Spec Record
1523 	  IF p_event_spec_disp_id IS NOT NULL THEN
1524 	    -- l_event_spec_disp.event_spec_disp_id := p_event_spec_disp_id;
1525 	    l_in_event_spec_disp.event_spec_disp_id := p_event_spec_disp_id;
1526 	    IF NOT (GMD_EVENT_SPEC_DISP_PVT.fetch_row(
1527 			 p_event_spec_disp => l_in_event_spec_disp,
1528 			 x_event_spec_disp => l_event_spec_disp)
1529 		 )
1530 	    THEN
1531 	      -- Fetch Error.
1532 	      RAISE e_event_spec_fetch_error;
1533 	    END IF;
1534 	  END IF;
1535 
1536     l_lab_organization_id := l_sample.organization_id;
1537 
1538 	  l_seq      := 0;
1539 
1540 	  IF (l_debug = 'Y') THEN
1541 	     gmd_debug.put_line('Total Number of tests to be added: '|| p_test_ids.COUNT);
1542 	  END IF;
1543 
1544 	  -- Go through all the tests
1545 	  FOR i in 1..p_test_ids.COUNT
1546 	  LOOP
1547 	    -- Set the varibles for new test
1548 	    l_additional_test_ind := NULL;
1549 	    l_replicate           := 0;
1550 	    l_viability_duration  := 0;
1551 	    l_resources           := NULL;
1552 
1553 	    SELECT test_type
1554 	    INTO   l_test_type
1555 	    FROM   gmd_qc_tests_b
1556 	    WHERE  test_id = p_test_ids(i)
1557 	    ;
1558 
1559 	    IF (l_debug = 'Y') THEN
1560 	       gmd_debug.put_line('Working on Test ID: ' || p_test_ids(i) || ' Type: ' || l_test_type);
1561 	    END IF;
1562 	    IF (l_test_type = 'E') THEN
1563 
1564 	 /*     -- If the test we are adding is Expression then make sure that it is not
1565 	      -- there alrady
1566 	      OPEN c_res_test(l_sample.sample_id, p_test_ids(i));
1567 	      FETCH c_res_test INTO dummy;
1568 	      IF c_res_test%FOUND THEN
1569 		IF (l_debug = 'Y') THEN
1570 		  gmd_debug.put_line('Expression Test is already in the result set. Abort.');
1571 		END IF;
1572 		CLOSE c_res_test;
1573 		GMD_API_PUB.Log_Message('GMD_EXP_TEST_IS_THERE');
1574 		RAISE FND_API.G_EXC_ERROR;
1575 	      END IF;
1576 	      CLOSE c_res_test; */
1577 
1578 	      -- If the test we are adding is Expression then make sure that all the
1579 	      -- reference tests are added before
1580 	      IF NOT all_ref_tests_exist_in_sample(p_sample_id => l_sample.sample_id,
1581 						   p_test_id   => p_test_ids(i))
1582 	      THEN
1583 		IF (l_debug = 'Y') THEN
1584 		  gmd_debug.put_line('Some of the reference tests are missing from the result set. Abort.');
1585 		END IF;
1586 		GMD_API_PUB.Log_Message('GMD_REF_TESTS_MISSING');
1587 		RAISE FND_API.G_EXC_ERROR;
1588 	      END IF;
1589 
1590 	    END IF; -- l_test_type = 'E'
1591 
1592 	    -- Find out that the test we are adding is an additional_test for the Spec.
1593 	    IF (l_event_spec_disp.spec_id IS NOT NULL) THEN
1594 	      IF NOT gmd_spec_grp.spec_test_exist
1595 		     (  p_spec_id => l_event_spec_disp.spec_id
1596 		      , p_test_id => p_test_ids(i)
1597 		     )
1598 	      THEN
1599 		-- The test is not part of the Spec
1600 		l_additional_test_ind      := 'Y';
1601 	      END IF;
1602 	    ELSE
1603 	      -- Since there is no Spec, all the tests are additional
1604 	      l_additional_test_ind      := 'Y';
1605 	    END IF;
1606 	    IF (l_debug = 'Y') THEN
1607 	       gmd_debug.put_line('The additional test indicator is (Y/NULL): ' || l_additional_test_ind);
1608 	    END IF;
1609 
1610 
1611 	    -- Now Construct GMD_RESULTS record
1612 
1613 	    -- For this, gather required information in local varaibles
1614 
1615 	    -- 1. Get the next test_replicate_cnt
1616 	    SELECT NVL(MAX(test_replicate_cnt), 0) + 1
1617 	    INTO   l_next_test_replicate_cnt
1618 	    FROM   gmd_results
1619 	    WHERE  sample_id = l_sample.sample_id
1620 	    AND    test_id   = p_test_ids(i)
1621 	    ;
1622 
1623 	    -- 2. Get the test_kit_item_id
1624 	    -- Bug 3088216: added test_method
1625             OPEN c_test_method(p_test_ids(i));
1626             FETCH c_test_method INTO l_test_kit_inv_item_id,
1627                                      l_replicate,
1628                                      l_test_method_id,
1629                                      l_test_qty,     -- 5353784
1630                                      l_test_qty_uom,  -- 5353784
1631                                      l_test_duration  -- Bug 13476354
1632                                      ;
1633             CLOSE c_test_method;
1634             IF (l_debug = 'Y') THEN
1635                gmd_debug.put_line('Replicate from Test Method: ' || l_replicate);
1636             END IF;
1637 
1638     -- 3. If this is not an additional test then get the replicate override
1639     --    from the Spec
1640     IF (l_additional_test_ind IS NULL) THEN
1641       -- Test is part of the Spec so get the replicate from spec.
1642       SELECT st.test_replicate, st.viability_duration, tm.resources
1643       INTO   l_replicate,l_viability_duration, l_resources
1644       FROM   gmd_spec_tests_b st, gmd_test_methods_b tm
1645       WHERE  st.spec_id = l_event_spec_disp.spec_id
1646       AND    st.exclude_ind IS NULL
1647       AND    st.test_id = p_test_ids(i)
1648       AND    st.test_method_id = tm.test_method_id
1649       ;
1650       IF (l_debug = 'Y') THEN
1651          gmd_debug.put_line('Since the test is in Spec the Replicate from Spec: ' || l_replicate);
1652          gmd_debug.put_line('Viability Duration in seconds: ' || l_viability_duration);
1653       END IF;
1654     END IF;
1655 
1656     -- 3.5 If this is an expression, make sure the replicate = 1
1657     --     The form is supposed to stop an expression from using a
1658     --     method with >1 replicate.  It is not working, yet, so
1659     --     here is the double check.  LeAta  07Nov2002
1660     IF (l_test_type = 'E') THEN
1661       l_replicate := 1;
1662     END IF;            -- end if an expression, replicate = 1
1663 
1664     -- 4. Get the next sequence
1665     l_seq := next_seq_in_result(l_sample.sample_id, p_test_ids(i));
1666 
1667     l_results.sample_id                     := l_sample.sample_id;
1668     l_results.test_id                       := p_test_ids(i);
1669     l_results.test_replicate_cnt            := l_next_test_replicate_cnt;
1670     l_results.lab_organization_id           := l_lab_organization_id;
1671     l_results.test_kit_inv_item_id          := l_test_kit_inv_item_id;
1672     l_results.seq                           := l_seq;
1673     l_results.delete_mark                   := 0;
1674     l_results.creation_date                 := l_date;
1675     l_results.created_by                    := l_user_id;
1676     l_results.last_updated_by               := l_user_id;
1677     l_results.last_update_date              := l_date;
1678     l_results.planned_resource              := l_resources;
1679 
1680       -- BUG 3088216: added test qty, test uom and test method id to results table
1681       -- 5353794 - use from test Method if there
1682     IF l_test_qty > 0
1683      then
1684          l_results.test_qty                      := l_test_qty;
1685     else
1686     	 l_results.test_qty                      := p_test_qty;
1687     END IF;
1688     IF l_test_qty_uom  IS not null
1689      then
1690          l_results.test_qty_uom                  := l_test_qty_uom;
1691     else
1692     	 l_results.test_qty_uom                  := p_test_qty_uom;
1693     END IF;
1694 
1695 
1696     l_results.test_method_id                := l_test_method_id;
1697 
1698     IF (nvl(l_viability_duration,0)  > 0 ) THEN
1699       l_results.test_by_date                  := l_sample.date_drawn
1700                                                   + l_viability_duration/(60*60*24);
1701     END IF;
1702 
1703     -- Bug 13476354: added planned_result_date
1704     IF (nvl(l_test_duration,0)  > 0 ) THEN
1705       l_results.planned_result_date           := l_sample.date_drawn
1706                                               + l_test_duration/(60*60*24);
1707     END IF;
1708     -- Now, Construct GMD_SPEC_RESULTS record
1709     l_spec_results.event_spec_disp_id       := p_event_spec_disp_id;
1710     l_spec_results.additional_test_ind      := l_additional_test_ind;
1711     l_spec_results.delete_mark              := 0;
1712     l_spec_results.creation_date            := l_date;
1713     l_spec_results.created_by               := l_user_id;
1714     l_spec_results.last_updated_by          := l_user_id;
1715     l_spec_results.last_update_date         := l_date;
1716 
1717     -- We are ready with Result and Spec Result record, so lets
1718     -- insert them l_replicate times.
1719     FOR i IN 1..l_replicate
1720     LOOP
1721       IF (l_debug = 'Y') THEN
1722          gmd_debug.put_line('Inserting test for replicate: ' || l_results.TEST_REPLICATE_CNT);
1723       END IF;
1724       -- We are ready for insert in GMD_RESULTS, so then lets do it.
1725       IF NOT(GMD_RESULTS_PVT.Insert_Row(
1726                     p_results => l_results,
1727                     x_results => l_out_results)
1728             )
1729       THEN
1730         -- Insert Error
1731         RAISE e_results_insert_error;
1732       END IF;
1733       l_results.RESULT_ID := l_out_results.RESULT_ID;
1734 
1735       -- Increment Serial Counter for OUT variables
1736       out_var_idx := out_var_idx + 1;
1737 
1738       x_results_tab(out_var_idx) := l_results;
1739       IF (l_debug = 'Y') THEN
1740          gmd_debug.put_line('Result record inserted, Result ID: ' || l_results.RESULT_ID);
1741       END IF;
1742 
1743       -- Assign the result_id to Spec Result record
1744       l_spec_results.RESULT_ID                := l_results.RESULT_ID;
1745 
1746       -- We are ready for insert in GMD_SPEC_RESULTS, so then lets do it.
1747       IF NOT(GMD_SPEC_RESULTS_PVT.Insert_Row(p_spec_results => l_spec_results))
1748       THEN
1749         -- Insert Error
1750         RAISE e_spec_results_insert_error;
1751       END IF;
1752 
1753       x_spec_results_tab(out_var_idx) := l_spec_results;
1754 
1755       IF (l_debug = 'Y') THEN
1756          gmd_debug.put_line('Spec Result record inserted');
1757       END IF;
1758 
1759       -- Now increment the l_next_test_replicate_cnt
1760       l_results.TEST_REPLICATE_CNT := l_results.TEST_REPLICATE_CNT + 1;
1761     END LOOP;
1762 
1763     -- Set the flad to indicate that a new test is added to the result set.
1764     l_test_added_flag := TRUE;
1765 
1766   END LOOP;  -- All the tests
1767 
1768   IF (l_debug = 'Y') THEN
1769      gmd_debug.put_line('All the tests are added. Setting *In Progress* flags');
1770   END IF;
1771 
1772   --  M.Grosser 04-May-2006:  BUG 5167171 - Forward Port of BUG 5097450
1773   --            Modified code to prevent the setting of sample disposition
1774   --            to In Progress on the addition of a test if the current
1775   --            disposition is Pending
1776   --
1777   IF (l_test_added_flag AND l_sample_spec_disp <> '1P') THEN
1778     -- Now that we added a new test to the sample, the
1779     -- disposition of the sample must be set back to "In Progress"
1780 
1781     -- B3005589 The sample disposition is stored in gmd_sample_spec_disp.
1782     --          So the following update is not required.
1783     -- UPDATE gmd_samples
1784     -- SET    sample_disposition = '2I',
1785     --        last_updated_by = l_user_id,
1786     --        last_update_date = l_date
1787     -- WHERE  sample_id = l_sample.sample_id
1788     -- ;
1789 
1790     -- Set the disposition of the sample spec disp
1791     -- back to "In Progress"
1792     UPDATE gmd_sample_spec_disp
1793     SET    disposition = '2I',
1794            last_updated_by = l_user_id,
1795            last_update_date = l_date
1796     WHERE  event_spec_disp_id = p_event_spec_disp_id
1797     AND    sample_id = l_sample.sample_id
1798     ;
1799 
1800     IF (l_event_spec_disp.disposition <> '2I') THEN
1801       -- Set the disposition of the Event spec disp
1802       -- back to "In Progress"
1803       UPDATE gmd_event_spec_disp
1804       SET    disposition = '2I',
1805              last_updated_by = l_user_id,
1806              last_update_date = l_date
1807       WHERE  event_spec_disp_id = p_event_spec_disp_id
1808       ;
1809     END IF;
1810 
1811     -- Set the disposition of the Sampling Event
1812     -- back to "In Progress"
1813     -- Also set recomposite_ind to 'Y' since we modified the sampling event
1814     -- by adding a new test
1815     UPDATE gmd_sampling_events
1816     SET    disposition = '2I',
1817 	   recomposite_ind = 'Y',
1818            last_updated_by = l_user_id,
1819            last_update_date = l_date
1820     WHERE  sampling_event_id = l_sample.sampling_event_id
1821     ;
1822 
1823     IF (l_debug = 'Y') THEN
1824        gmd_debug.put_line('Setting *In Progress* flags completed.');
1825     END IF;
1826   END IF;
1827 
1828   IF (l_debug = 'Y') THEN
1829      gmd_debug.put_line('Leaving procedure: ADD_TESTS_TO_SAMPLE');
1830   END IF;
1831 
1832 
1833 EXCEPTION
1834   WHEN FND_API.G_EXC_ERROR  OR
1835        e_results_insert_error OR
1836        e_spec_results_insert_error OR
1837        e_samples_fetch_error OR
1838        e_event_spec_fetch_error OR
1839        e_sampling_event_fetch_error
1840   THEN
1841     x_return_status := FND_API.G_RET_STS_ERROR ;
1842   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1843     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1844   WHEN OTHERS THEN
1845     GMD_API_PUB.Log_Message('GMD_API_ERROR',
1846                             'PACKAGE','ADD_TESTS_TO_SAMPLE',
1847                             'ERROR', SUBSTR(SQLERRM,1,100));
1848     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1849 
1850 END add_tests_to_sample;
1851 
1852 
1853 
1854 --Start of comments
1855 --+========================================================================+
1856 --| API Name    : next_seq_in_result                                       |
1857 --| TYPE        : Group                                                    |
1858 --| Notes       : This function returns the next sequence for the test     |
1859 --|               in the result set for a given test of sample.            |
1860 --|                                                                        |
1861 --| HISTORY                                                                |
1862 --|    Chetan Nagar	21-Aug-2002	Created.                           |
1863 --|                                                                        |
1864 --+========================================================================+
1865 -- End of comments
1866 
1867 FUNCTION next_seq_in_result
1868 (
1869   p_sample_id NUMBER
1870 , p_test_id   NUMBER
1871 )
1872 RETURN NUMBER IS
1873 
1874   CURSOR c_seq IS
1875   SELECT seq
1876   FROM   gmd_results
1877   WHERE  sample_id = p_sample_id
1878   AND    test_id   = p_test_id
1879   ;
1880 
1881   l_seq   NUMBER := 10;
1882 
1883 BEGIN
1884 
1885   OPEN c_seq;
1886   FETCH c_seq INTO l_seq;
1887   IF c_seq%NOTFOUND THEN
1888 
1889     -- The test is not part of result set, so get the new seq
1890     SELECT (floor(nvl(max(seq),0) / 10) * 10) + 10
1891     INTO   l_seq
1892     FROM   gmd_results
1893     WHERE  sample_id = p_sample_id
1894     ;
1895 
1896   END IF;
1897 
1898   CLOSE c_seq;
1899 
1900   return l_seq;
1901 
1902 EXCEPTION
1903   WHEN OTHERS THEN
1904     RETURN 10;
1905 END next_seq_in_result;
1906 
1907 
1908 
1909 --Start of comments
1910 --+========================================================================+
1911 --| API Name    : all_ref_tests_exist_in_sample                            |
1912 --| TYPE        : Group                                                    |
1913 --| Notes       : This function returns TRUE if all the reference tests    |
1914 --|               for the given test are already part of sample, FALSE     |
1915 --|               otherwise.                                               |
1916 --|                                                                        |
1917 --| HISTORY                                                                |
1918 --|    Chetan Nagar	11-Oct-2002	Created.                           |
1919 --|                                                                        |
1920 --+========================================================================+
1921 -- End of comments
1922 
1923 FUNCTION all_ref_tests_exist_in_sample
1924 (
1925   p_sample_id IN NUMBER
1926 , p_test_id   IN NUMBER
1927 ) RETURN BOOLEAN IS
1928 
1929   -- Cursors
1930   CURSOR c_ref_tests(p_sample_id NUMBER, p_test_id NUMBER) IS
1931   SELECT 1
1932   FROM   gmd_qc_test_values_b tv
1933   WHERE  tv.test_id = p_test_id
1934   AND    tv.expression_ref_test_id NOT IN
1935     (SELECT test_id
1936      FROM   gmd_results
1937      WHERE  sample_id = p_sample_id)
1938   ;
1939 
1940   -- Local Variables
1941   dummy       PLS_INTEGER;
1942 
1943 BEGIN
1944   -- See if any of the reference test is missing
1945   OPEN c_ref_tests(p_sample_id, p_test_id);
1946   FETCH c_ref_tests INTO dummy;
1947   IF c_ref_tests%FOUND THEN
1948     CLOSE c_ref_tests;
1949     RETURN FALSE;
1950   END IF;
1951   CLOSE c_ref_tests;
1952 
1953   RETURN TRUE;
1954 
1955 EXCEPTION
1956   WHEN OTHERS THEN
1957     RETURN FALSE;
1958 END all_ref_tests_exist_in_sample;
1959 
1960 
1961 
1962 --Start of comments
1963 --+========================================================================+
1964 --| API Name    : add_test_to_samples                                      |
1965 --| TYPE        : Group                                                    |
1966 --| Notes       : This function received as input table of sample IDs to   |
1967 --|               which a given test is to be added.                       |
1968 --|                                                                        |
1969 --|               The function will insert rows into GMD_RESULTS and       |
1970 --|               GMD_SPEC_RESULTS with the specified test for each Sample.|
1971 --|                                                                        |
1972 --|               THIS ROUTINE RE-USES THE ROUTINE ADD_TESTS_TO_SAMPLE     |
1973 --|               BY ADDING ONE TEST TO ONE SAMPLE AT A TIME.              |
1974 --|                                                                        |
1975 --| PARAMETERS  :                                                          |
1976 --|                                                                        |
1977 --| 1. p_samples - Table of Sample IDs in which the test is to be added.   |
1978 --| 2. p_test_id - Test ID which is to be added to all the samples.        |
1979 --| 3. p_event_spec_disp_id  - Event Spec                                  |
1980 --|                                                                        |
1981 --| HISTORY                                                                |
1982 --|    Chetan Nagar	21-Aug-2002	Created.                           |
1983 --|                                                                        |
1984 --|    Susan Feinstein  11-AUG-2003     Bug 3088216: Added test_uom,       |
1985 --|                                      test_qty                          |
1986 --+========================================================================+
1987 -- End of comments
1988 PROCEDURE add_test_to_samples
1989 (
1990   p_sample_ids         IN         GMD_API_PUB.number_tab
1991 , p_test_id            IN         NUMBER
1992 , p_event_spec_disp_id IN         NUMBER
1993 , x_results_tab        OUT NOCOPY GMD_API_PUB.gmd_results_tab
1994 , x_spec_results_tab   OUT NOCOPY GMD_API_PUB.gmd_spec_results_tab
1995 , x_return_status      OUT NOCOPY VARCHAR2
1996 , p_test_qty           IN         NUMBER   DEFAULT NULL
1997 , p_test_qty_uom           IN         VARCHAR2 DEFAULT NULL
1998 )
1999 IS
2000 
2001   -- Local Variables
2002   l_sample                      GMD_SAMPLES%ROWTYPE;
2003   l_test_ids                    GMD_API_PUB.number_tab;
2004   l_results_tab                 GMD_API_PUB.gmd_results_tab;
2005   l_spec_results_tab            GMD_API_PUB.gmd_spec_results_tab;
2006 
2007   l_return_status               VARCHAR2(1);
2008 
2009 BEGIN
2010 
2011   IF (l_debug = 'Y') THEN
2012      gmd_debug.put_line('Entering Procedure ADD_TEST_TO_SAMPLES');
2013   END IF;
2014   --  Initialize API return status to success
2015   x_return_status := FND_API.G_RET_STS_SUCCESS;
2016 
2017   -- Since we are going to insert the same test to all
2018   -- the samples assign the test to table of test IDs
2019   l_test_ids(1) := p_test_id;
2020 
2021   -- Go thorugh all the Sample IDs
2022   FOR i in 1..p_sample_ids.COUNT
2023   LOOP
2024 
2025     l_sample.sample_id := p_sample_ids(i);
2026 
2027     IF (l_debug = 'Y') THEN
2028        gmd_debug.put_line('Going to add test ID: ' || l_test_ids(1) || ' in Sample ID: ' || l_sample.sample_id);
2029     END IF;
2030 
2031     -- Now we have all the parameters ready for routine add_tests_to_sample
2032     add_tests_to_sample(
2033       p_sample             => l_sample
2034     , p_test_ids           => l_test_ids
2035     , p_event_spec_disp_id => p_event_spec_disp_id
2036     , p_test_qty           => p_test_qty
2037     , p_test_qty_uom       => p_test_qty_uom
2038     , x_results_tab        => l_results_tab
2039     , x_spec_results_tab   => l_spec_results_tab
2040     , x_return_status      => l_return_status);
2041 
2042     IF l_return_status <> 'S' THEN
2043       -- Message must have been logged so just raise an exception.
2044       RAISE FND_API.G_EXC_ERROR;
2045     END IF;
2046 
2047     IF (l_debug = 'Y') THEN
2048        gmd_debug.put_line('Ttest ID: ' || l_test_ids(1) || ' is added to Sample ID: ' || l_sample.sample_id);
2049     END IF;
2050 
2051     -- If success then assign the OUT variables received
2052     x_results_tab(i)      := l_results_tab(1);
2053     x_spec_results_tab(i) := l_spec_results_tab(1);
2054 
2055   END LOOP;
2056 
2057   IF (l_debug = 'Y') THEN
2058      gmd_debug.put_line('Leaving Procedure ADD_TEST_TO_SAMPLES');
2059   END IF;
2060 
2061 EXCEPTION
2062   WHEN FND_API.G_EXC_ERROR THEN
2063     x_return_status := FND_API.G_RET_STS_ERROR ;
2064   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2065     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2066   WHEN OTHERS THEN
2067     GMD_API_PUB.Log_Message('GMD_API_ERROR',
2068                             'PACKAGE','ADD_TEST_TO_SAMPLES',
2069                             'ERROR', SUBSTR(SQLERRM,1,100));
2070     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2071 
2072 
2073 END add_test_to_samples;
2074 
2075 
2076 --Start of comments
2077 --+========================================================================+
2078 --| API Name    : make_target_spec_the_base_spec                           |
2079 --| TYPE        : Group                                                    |
2080 --| Notes       : This procedure is to make the target spec as the base    |
2081 --|               spec for a given sample.                                 |
2082 --|                                                                        |
2083 --|               This procedure will first create any missing replicate   |
2084 --|               of the tests for the target spec in gmd_results. Then    |
2085 --|               create a row in GMD_EVENT_SPEC_DISP and a row in         |
2086 --|               GMD_SAMPLE_SPEC_DISP and a set of rows in                |
2087 --|               GMD_SPEC_RESULTS                                         |
2088 --|                                                                        |
2089 --|                                                                        |
2090 --| PARAMETERS  :                                                          |
2091 --|                                                                        |
2092 --| 1. p_sample_id - Sample ID for which we are changing the base spec.    |
2093 --| 2. p_target_spec_id - Spec ID which will become base spec.             |
2094 --| 3. p_target_spec_cr_id - Spec VR ID which will become base spec VR.    |
2095 --|                                                                        |
2096 --| HISTORY                                                                |
2097 --|    Chetan Nagar	21-Aug-2002	Created.                                   |
2098 --|                                                                        |
2099 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition         |
2100 --|       in select for table - gmd_spec_tests_b                           |
2101 --|                                                                        |
2102 --|   RLNAGARA          07-Feb-2006     Modified the cursor c_vrs_vw and get_organization_code. |
2103 --|   RLNAGARA   19-MAY-2006 Bug#5220513 (FP of 5026764)                   |
2104 --|          Added code so as to re-evaluate the results against the target|
2105 --|     spec and change the evaluations of results appropriately.          |
2106 --|   PLOWE      19-JUL-2010 change PROC make_target_spec_the_base_spec    |
2107 --|   For bug 9282975 condition was added to round it only if result is    |
2108 --|   in number not for char result - for this bug added                   |
2109 --|   'and  x_rec.test_type  <> 'T'   '                                    |
2110 --+========================================================================+
2111 -- End of comments
2112 
2113 
2114 PROCEDURE make_target_spec_the_base_spec
2115 (
2116   p_sample_id          IN NUMBER
2117 , p_target_spec_id     IN NUMBER
2118 , p_target_spec_vr_id  IN NUMBER
2119 , x_return_status      OUT NOCOPY VARCHAR2
2120 )
2121 IS
2122 
2123   -- Cursors
2124   CURSOR c_gmd_results (p_sample_id NUMBER) IS
2125   SELECT result_id,
2126          test_id,
2127          result_value_num,
2128          result_value_char,
2129          result_date              --Bug 5220513
2130   FROM   gmd_results
2131   WHERE  sample_id = p_sample_id
2132   ;
2133 
2134   CURSOR c_spec_tests (p_spec_id NUMBER) IS
2135   SELECT   st.test_id
2136          , st.test_replicate
2137          , tm.test_kit_inv_item_id
2138   FROM   gmd_spec_tests_b st, gmd_test_methods_b tm
2139   WHERE  st.spec_id = p_spec_id
2140   AND    st.exclude_ind IS NULL
2141   AND    st.test_method_id =  tm.test_method_id
2142   ;
2143 
2144 --RLNAGARA Bug # 4918820 Changed the view from gmd_all_spec_vrs to gmd_com_spec_vrs_vl
2145   CURSOR c_vrs_vw (p_spec_vr_id NUMBER) IS
2146   SELECT spec_id
2147   FROM   gmd_com_spec_vrs_vl
2148   WHERE  spec_vr_id = p_spec_vr_id
2149   ;
2150 
2151   CURSOR c_event_spec (p_sampling_event_id NUMBER, p_spec_vr_id NUMBER) IS
2152   SELECT *
2153   FROM   gmd_event_spec_disp
2154   WHERE  sampling_event_id = p_sampling_event_id
2155   AND    spec_vr_id = p_spec_vr_id
2156   AND    spec_used_for_lot_attrib_ind = 'Y'
2157   ;
2158 
2159 --Begin Bug#5220513
2160    l_test_id 		GMD_QC_TESTS.test_id%TYPE;
2161    l_spec_id 		gmd_spec_tests_b.spec_id%TYPE;
2162 
2163   CURSOR c_evaluation_ind (p_result_id NUMBER, p_event_spec_disp_id NUMBER) IS
2164   SELECT evaluation_ind  from gmd_spec_results gsr, gmd_event_spec_disp  esd --, gmd_sample_spec_disp ssd
2165   WHERE gsr.result_id=p_result_id
2166   AND esd.event_spec_disp_id=p_event_spec_disp_id
2167   AND gsr.event_spec_disp_id=esd.event_spec_disp_id;
2168 
2169 
2170   CURSOR c_event_spec_disp_id (p_sample_id NUMBER) IS
2171   SELECT esd.event_spec_disp_id from gmd_event_spec_disp  esd, gmd_sample_spec_disp ssd
2172   WHERE ssd.sample_id=p_sample_id
2173   AND esd.event_spec_disp_id=ssd.event_spec_disp_id
2174   AND esd.spec_used_for_lot_attrib_ind = 'Y';
2175 
2176   CURSOR c_get_type IS
2177       SELECT  t.test_type, t.test_code, t.test_method_id, t.expression, t.test_unit,
2178       	      m.test_method_code
2179       FROM    gmd_qc_tests_b t , gmd_test_methods_b m
2180       WHERE   t.test_id = l_test_id
2181       AND     t.test_method_id = m.test_method_id;
2182 
2183   LocalTypeRec	c_get_type%ROWTYPE;
2184 
2185   CURSOR c_get_display IS
2186     SELECT  v.display_label_numeric_range
2187     FROM    gmd_qc_test_values v
2188     WHERE   v.test_id = l_test_id;
2189 
2190   LocalDisRec   c_get_display%ROWTYPE;
2191 
2192   CURSOR c_get_spec_test_num IS
2193       SELECT  s.min_value_num, s.max_value_num, s.target_value_num,s.display_precision
2194       FROM    gmd_spec_tests_b s
2195       WHERE   s.spec_id = l_spec_id
2196       AND     s.test_id = l_test_id
2197       AND     s.exclude_ind IS NULL;
2198 
2199   LocalNumRec   c_get_spec_test_num%ROWTYPE;
2200 
2201   CURSOR c_get_spec_test_char IS
2202       SELECT  s.min_value_char, s.max_value_char, s.target_value_char
2203       FROM    gmd_spec_tests_b s
2204       WHERE   s.spec_id = l_spec_id
2205       AND     s.test_id = l_test_id
2206       AND     s.exclude_ind IS NULL;
2207 
2208       LocalCharRec   c_get_spec_test_char%ROWTYPE;
2209 
2210    CURSOR   c_spec_test_all (p_spec_id NUMBER, p_test_id NUMBER) IS
2211     SELECT  *
2212     FROM   gmd_spec_tests_b
2213     WHERE  spec_id = p_spec_id
2214     AND    test_id = p_test_id
2215    ;
2216 
2217 
2218   -- Local Variables
2219   x_rec                      result_data;
2220   old_event_spec_disp_id     NUMBER;
2221   l_spec_test_all            GMD_SPEC_TESTS_B%ROWTYPE;
2222 
2223   --End Bug#5220513
2224 
2225 --RLNAGARA Bug 4918820 Changed from mtl_organizations to mtl_parameters
2226   CURSOR get_organization_code (l_organization_id NUMBER) IS
2227   SELECT organization_code
2228   FROM mtl_parameters
2229   WHERE organization_id = l_organization_id;
2230 
2231   -- Local Variables
2232   l_user_id                  NUMBER;
2233   l_date                     DATE;
2234   l_curr_replicate_cnt       PLS_INTEGER;
2235   l_missing_cnt              PLS_INTEGER;
2236   l_return_status            VARCHAR2(1);
2237 
2238   l_change_disp_to           VARCHAR2(3);
2239   l_message_data             VARCHAR2(2000);
2240 
2241   l_test_ids                 GMD_API_PUB.number_tab;
2242   l_results_tab              GMD_API_PUB.gmd_results_tab;
2243   l_spec_results_tab         GMD_API_PUB.gmd_spec_results_tab;
2244 
2245   l_event_spec_disp          GMD_EVENT_SPEC_DISP%ROWTYPE;
2246   l_sample_spec_disp         GMD_SAMPLE_SPEC_DISP%ROWTYPE;
2247   l_spec_result              GMD_SPEC_RESULTS%ROWTYPE;
2248   l_sample                   GMD_SAMPLES%ROWTYPE;
2249 
2250   l_in_sample                GMD_SAMPLES%ROWTYPE;
2251   l_out_event_spec_disp      GMD_EVENT_SPEC_DISP%ROWTYPE;
2252 
2253   -- Exceptions
2254   e_sample_fetch_error           EXCEPTION;
2255   e_event_spec_disp_insert_error EXCEPTION;
2256   e_sample_spec_disp_insert_err  EXCEPTION;
2257   e_spec_results_insert_error    EXCEPTION;
2258   l_organization_code         mtl_organizations.organization_code%TYPE;
2259 
2260 
2261 
2262 BEGIN
2263 
2264   IF (l_debug = 'Y') THEN
2265      gmd_debug.put_line('Entering Procedure - MAKE_TARGET_SPEC_THE_BASE_SPEC');
2266   END IF;
2267 
2268   --  Initialize API return status to success
2269   x_return_status := FND_API.G_RET_STS_SUCCESS;
2270 
2271   -- Make sure we have the sample record
2272   -- l_sample.sample_id := p_sample_id;
2273   l_in_sample.sample_id := p_sample_id;
2274   IF NOT (GMD_SAMPLES_PVT.fetch_row(
2275                    p_samples => l_in_sample,
2276                    x_samples => l_sample)
2277          )
2278   THEN
2279     -- Fetch Error.
2280 
2281      IF (l_debug = 'Y') THEN
2282     	 gmd_debug.put_line(' MAKE_TARGET_SPEC_THE_BASE_SPEC   RAISE e_sample_fetch_error ');
2283      END IF;
2284 
2285     RAISE e_sample_fetch_error;
2286   END IF;
2287 
2288 
2289   IF (l_debug = 'Y') THEN
2290 
2291     OPEN get_organization_code(l_sample.organization_id);
2292     FETCH get_organization_code into l_organization_code;
2293     CLOSE get_organization_code;
2294 
2295      gmd_debug.put_line('  Changing base spec for Sample ID - ' || l_sample.sample_id ||
2296                       ' Sample No - ' || l_organization_code
2297                                       || '-' || l_sample.sample_no ||
2298                       ' to Target Spec - ' || p_target_spec_id ||
2299                       ' Target Spec VR - ' || p_target_spec_vr_id);
2300   END IF;
2301 
2302   --Bug#5220513
2303   OPEN  c_event_spec_disp_id(l_sample.sample_id);
2304   FETCH c_event_spec_disp_id INTO old_event_spec_disp_id;
2305   CLOSE c_event_spec_disp_id;
2306 --Bug#5220513
2307 
2308   -- Check that sample is not "Retain"
2309   --  Bug 3079877: Check that sample is not "Planning"
2310   IF (l_sample.sample_disposition = '0RT')
2311    AND (l_sample.sample_disposition = '0PL') THEN
2312        -- For retain sample no comparison, please!
2313      GMD_API_PUB.Log_Message('GMD_RETAIN_SAMPLE');
2314       IF (l_debug = 'Y') THEN
2315     	 gmd_debug.put_line(' MAKE_TARGET_SPEC_THE_BASE_SPEC  For Retain or Planned sample  -    no comparison ');
2316      END IF;
2317 
2318 
2319      RAISE FND_API.G_EXC_ERROR;
2320   END IF;
2321 
2322   -- Check that we have the Sampling Event
2323   -- Now, even in case where there is no Spec for the sample
2324   -- there still should be a sampling event as per new directions.
2325   IF (l_sample.sampling_event_id IS NULL) THEN
2326     GMD_API_PUB.Log_Message('GMD_SAMPLING_EVENT_MISSING');
2327      IF (l_debug = 'Y') THEN
2328     	 gmd_debug.put_line(' MAKE_TARGET_SPEC_THE_BASE_SPEC  GMD_SAMPLING_EVENT_MISSING ');
2329      END IF;
2330 
2331     RAISE FND_API.G_EXC_ERROR;
2332   END IF;
2333 
2334   -- All the required validations are over so let's start
2335   -- doing some REAL work.
2336 
2337   -- Get the user ID
2338   IF l_sample.created_by IS NULL THEN
2339     l_user_id  := FND_GLOBAL.user_id;
2340   ELSE
2341     l_user_id  := l_sample.created_by;
2342   END IF;
2343 
2344   l_date     := SYSDATE;
2345 
2346   OPEN c_event_spec (l_sample.sampling_event_id, p_target_spec_vr_id);
2347   FETCH c_event_spec INTO l_event_spec_disp;
2348   IF c_event_spec%NOTFOUND THEN
2349     -- Since the new spec is going to be the current one, change
2350     -- all the previous ones to NOT Current
2351     UPDATE gmd_event_spec_disp
2352     set    spec_used_for_lot_attrib_ind = NULL
2353     where  sampling_event_id = l_sample.sampling_event_id
2354     and    spec_used_for_lot_attrib_ind = 'Y'
2355     ;
2356 
2357     IF (l_debug = 'Y') THEN
2358        gmd_debug.put_line('  Updated all previous gmd_event_spec_disp to NOT-Current.');
2359     END IF;
2360 
2361     -- Create a new record in GMD_EVENT_SPEC_DISP for the Target Spec
2362 
2363     -- Construct the record
2364     l_event_spec_disp.SAMPLING_EVENT_ID            := l_sample.sampling_event_id;
2365     l_event_spec_disp.SPEC_ID                      := p_target_spec_id;
2366     l_event_spec_disp.SPEC_VR_ID                   := p_target_spec_vr_id;
2367     l_event_spec_disp.DISPOSITION                  := '1P';
2368 
2369     -- We need to see if we can default something here
2370     -- New record gets - Y
2371     l_event_spec_disp.SPEC_USED_FOR_LOT_ATTRIB_IND := 'Y';
2372 
2373     l_event_spec_disp.DELETE_MARK                  := 0;
2374     l_event_spec_disp.CREATION_DATE                := l_date;
2375     l_event_spec_disp.CREATED_BY                   := l_user_id;
2376     l_event_spec_disp.LAST_UPDATE_DATE             := l_date;
2377     l_event_spec_disp.LAST_UPDATED_BY              := l_user_id;
2378 
2379     -- We are ready for insert in GMD_EVENT_SPEC_DISP, so then lets do it.
2380     IF NOT(GMD_EVENT_SPEC_DISP_PVT.Insert_Row(
2381                     p_event_spec_disp => l_event_spec_disp,
2382                     x_event_spec_disp => l_out_event_spec_disp)
2383             )
2384     THEN
2385       -- Insert Error
2386       RAISE e_event_spec_disp_insert_error;
2387     END IF;
2388     l_event_spec_disp.EVENT_SPEC_DISP_ID := l_out_event_spec_disp.EVENT_SPEC_DISP_ID;
2389 
2390     IF (l_debug = 'Y') THEN
2391        gmd_debug.put_line('  A new record is created in GMD_EVENT_SPEC_DISP. ID - ' || l_event_spec_disp.event_spec_disp_id);
2392     END IF;
2393 
2394   END IF;
2395   CLOSE c_event_spec;
2396 
2397   -- Create a record in GMD_SAMPLE_SPEC_DISP for the Target Spec
2398 
2399   -- Construct the record
2400   l_sample_spec_disp.EVENT_SPEC_DISP_ID           := l_event_spec_disp.event_spec_disp_id;
2401   l_sample_spec_disp.SAMPLE_ID                    := l_sample.sample_id;
2402   l_sample_spec_disp.DISPOSITION                  := '1P';
2403   l_sample_spec_disp.DELETE_MARK                  := 0;
2404   l_sample_spec_disp.CREATION_DATE                := l_date;
2405   l_sample_spec_disp.CREATED_BY                   := l_user_id;
2406   l_sample_spec_disp.LAST_UPDATE_DATE             := l_date;
2407   l_sample_spec_disp.LAST_UPDATED_BY              := l_user_id;
2408 
2409   -- We are ready for insert, so then lets do it.
2410   IF NOT(GMD_SAMPLE_SPEC_DISP_PVT.Insert_Row(
2411                   p_sample_spec_disp => l_sample_spec_disp)
2412           )
2413   THEN
2414     -- Insert Error
2415       IF (l_debug = 'Y') THEN
2416     	 gmd_debug.put_line(' MAKE_TARGET_SPEC_THE_BASE_SPEC  RAISE e_sample_spec_disp_insert_err  ');
2417      END IF;
2418 
2419     RAISE e_sample_spec_disp_insert_err;
2420   END IF;
2421 
2422   IF (l_debug = 'Y') THEN
2423      gmd_debug.put_line('  A new record is created in GMD_SAMPLE_SPEC_DISP.');
2424      gmd_debug.put_line('  Now duplicate all current GMD_SPEC_RESULTS under new Spec.');  -- up to here
2425   END IF;
2426 
2427   -- Create a set of records in GMD_SPEC_RESULTS for the target Spec
2428   -- In this run we are just creating records for all the results
2429   -- that are there in GMD_RESULTS
2430   FOR l_res IN c_gmd_results(l_sample.sample_id)
2431   LOOP
2432 
2433     -- Construct GMD_SPEC_RESULTS Record
2434     l_spec_result.EVENT_SPEC_DISP_ID       := l_event_spec_disp.event_spec_disp_id;
2435     l_spec_result.RESULT_ID                := l_res.result_id;
2436 
2437     --Bug#5220513
2438     OPEN c_evaluation_ind(l_res.result_id,old_event_spec_disp_id);
2439     FETCH  c_evaluation_ind INTO l_spec_result.evaluation_ind;
2440     CLOSE c_evaluation_ind;
2441     --Bug#5220513
2442 
2443     -- B2993072 Initialize the indicators
2444     l_spec_result.ADDITIONAL_TEST_IND      := NULL;
2445     l_spec_result.IN_SPEC_IND              := NULL;
2446 
2447     -- When we create Spec Result rows for the new Spec we need to check if
2448     -- the test is additional for the Target Spec or Not.
2449     IF (p_target_spec_id IS NOT NULL) THEN
2450       IF NOT gmd_spec_grp.spec_test_exist
2451              (  p_spec_id => p_target_spec_id
2452               , p_test_id => l_res.test_id
2453              )
2454       THEN
2455         -- The test is not in the Target Spec so it becomes Additional
2456         l_spec_result.ADDITIONAL_TEST_IND      := 'Y';
2457       ELSE
2458        -- Begin Bug#5220513
2459       x_rec.result := NULL;
2460       IF l_res.result_value_num IS NOT NULL THEN
2461         x_rec.result_num := l_res.result_value_num;
2462         x_rec.result := l_res.result_value_num;
2463         x_rec.result_date := l_res.result_date;
2464       END IF;
2465 
2466       IF l_res.result_value_char IS NOT NULL THEN
2467          x_rec.result_char := l_res.result_value_char;
2468          x_rec.result := l_res.result_value_char;
2469          x_rec.result_date := l_res.result_date;
2470       END IF;
2471       l_test_id := l_res.test_id;
2472       x_rec.test_id := l_test_id;
2473 
2474       -- For each test type get the test and method info
2475       OPEN c_get_type;
2476       FETCH c_get_type INTO LocalTypeRec;
2477       x_rec.test_type := LocalTypeRec.test_type;
2478       x_rec.test_code := LocalTypeRec.test_code;
2479       x_rec.expression := LocalTypeRec.expression;
2480       x_rec.unit := LocalTypeRec.test_unit;
2481       x_rec.method := LocalTypeRec.test_method_code;
2482       CLOSE c_get_type;
2483 
2484       IF (l_debug = 'Y') THEN
2485          gmd_debug.put_line('Retrieved Test and Method info:');
2486          gmd_debug.put_line('  Test: ' || LocalTypeRec.test_code || ' Method: ' ||  LocalTypeRec.test_method_code);
2487       END IF;
2488 
2489 
2490 
2491       l_spec_id := p_target_spec_id;
2492       x_rec.spec_id       := l_spec_id;
2493       OPEN c_get_spec_test_num;
2494       FETCH c_get_spec_test_num INTO LocalNumRec;
2495       IF c_get_spec_test_num%FOUND THEN
2496         x_rec.spec_target_num := LocalNumRec.target_value_num;
2497   	    x_rec.spec_min_num := LocalNumRec.min_value_num;
2498     	x_rec.spec_max_num := LocalNumRec.max_value_num;
2499     	  --Fetching the display precision into table type.
2500    	    x_rec.spec_display_precision:=LocalNumRec.display_precision;
2501     	x_rec.display_precision := x_rec.spec_display_precision;
2502     	x_rec.report_precision := x_rec.spec_display_precision;
2503       END IF;
2504       CLOSE c_get_spec_test_num;
2505       IF LocalTypeRec.test_type IN ('V', 'T', 'L', 'E','U') THEN
2506         OPEN c_get_spec_test_char;
2507         FETCH c_get_spec_test_char INTO LocalCharRec;
2508         IF c_get_spec_test_char%FOUND THEN
2509           x_rec.spec_target_char := LocalCharRec.target_value_char;
2510  	      x_rec.spec_min_char := LocalCharRec.min_value_char;
2511           x_rec.spec_max_char := LocalCharRec.max_value_char;
2512         END IF;
2513         CLOSE c_get_spec_test_char;
2514       END IF;
2515 
2516       IF (l_debug = 'Y') THEN
2517          gmd_debug.put_line('Got the Test and Spec info');
2518       END IF;
2519 
2520 
2521 
2522       OPEN c_spec_test_all(l_spec_id, l_test_id);
2523       FETCH c_spec_test_all INTO l_spec_test_all;
2524       CLOSE c_spec_test_all;
2525 
2526     x_rec.spec_min_num := l_spec_test_all.min_value_num;
2527     x_rec.spec_max_num := l_spec_test_all.max_value_num;
2528     x_rec.out_action_code := l_spec_test_all.out_of_spec_action;
2529     x_rec.exp_error_type  := l_spec_test_all.exp_error_type;
2530     x_rec.below_spec_min := l_spec_test_all.below_spec_min;
2531     x_rec.above_spec_min := l_spec_test_all.above_spec_min;
2532     x_rec.below_spec_max := l_spec_test_all.below_spec_max;
2533     x_rec.above_spec_max := l_spec_test_all.above_spec_max;
2534     x_rec.below_min_action_code := l_spec_test_all.below_min_action_code;
2535     x_rec.above_min_action_code := l_spec_test_all.above_min_action_code;
2536     x_rec.below_max_action_code := l_spec_test_all.below_max_action_code;
2537     x_rec.above_max_action_code := l_spec_test_all.above_max_action_code;
2538 
2539 
2540 
2541         -- Since the test is part of the Spec we can derive if it is IN-SPEC
2542 
2543         IF (l_debug = 'Y') THEN
2544         	 gmd_debug.put_line('calling rslt_is_in_spec ');
2545      		 END IF;
2546 
2547         l_spec_result.IN_SPEC_IND          := rslt_is_in_spec
2548                                               (p_spec_id         => p_target_spec_id,
2549                                                p_test_id         => l_res.test_id,
2550                                                p_rslt_value_num  => l_res.result_value_num,
2551                                                p_rslt_value_char => l_res.result_value_char
2552                                               );
2553 
2554          x_rec.in_spec := l_spec_result.IN_SPEC_IND;
2555 
2556 
2557          IF (l_debug = 'Y') THEN
2558         	 gmd_debug.put_line('called rslt_is_in_spec  -          ');
2559      		 END IF;
2560 
2561 
2562 
2563 
2564        -- 9282975 (12.1.1 ct bug - using triple checkin )  Added this condition to round it only if result is in number not for char result.
2565   	   IF l_res.result_value_num IS NOT NULL  and  x_rec.test_type  <> 'T'    -- 9733854 added  and  x_rec.test_type  <> 'T'
2566   	   THEN
2567 
2568 
2569 		 	     x_rec.value_in_report_prec := ROUND(to_number(x_rec.result),x_rec.report_precision);
2570 
2571     	     x_rec.result_num := ROUND(to_number(x_rec.result) ,x_rec.display_precision);
2572 
2573     	     x_rec.result := ROUND(to_number(x_rec.result) ,x_rec.display_precision);
2574 
2575 
2576        END IF;
2577 
2578 
2579         IF x_rec.test_type in ('N', 'L', 'E') THEN
2580            GMD_RESULTS_GRP.check_experimental_error  ( x_rec
2581                                                 , l_return_status );
2582     	    IF (l_return_status <> 'S') THEN
2583         	     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2584 	        END IF;
2585         END IF;
2586 
2587     	IF  x_rec.additional_test_ind IS NULL AND
2588       		x_rec.exp_error_type IS NULL AND
2589 	       (x_rec.in_spec <> 'Y' OR x_rec.in_spec IS NULL )  THEN
2590       		x_rec.out_of_spec := 'TRUE';
2591 	        x_rec.result_action_code := x_rec.out_action_code;
2592     	END IF;          -- end if result falls in either of the fuzzy zones
2593 
2594 
2595 	    IF x_rec.in_fuzzy_zone = 'TRUE' THEN
2596     	  x_rec.evaluation_ind := '3E';
2597 	    ELSIF (x_rec.in_spec ='Y') THEN
2598 	      x_rec.evaluation_ind := '0A';
2599         ELSE
2600       		IF x_rec.out_of_spec = 'FALSE' THEN
2601 	        	x_rec.result_action_code := NULL;
2602     		END IF;
2603             x_rec.evaluation_ind := NULL;
2604     END IF;    --  end setting evaluation ind.
2605     	  l_spec_result.evaluation_ind := x_rec.evaluation_ind ;
2606 
2607     	  -- End Bug#5220513
2608 
2609 	 -- B2993072
2610          -- Since the result is in spec, it is also 'ACCEPT' for the evaluation
2611 --         l_spec_result.evaluation_ind      := '0A';    Bug5220513 Commented this line
2612 
2613       END IF;
2614     ELSE
2615       l_spec_result.ADDITIONAL_TEST_IND      := 'Y';
2616     END IF;
2617 
2618 
2619     l_spec_result.DELETE_MARK              := 0;
2620     l_spec_result.CREATION_DATE            := l_date;
2621     l_spec_result.CREATED_BY               := l_user_id;
2622     l_spec_result.LAST_UPDATED_BY          := l_user_id;
2623     l_spec_result.LAST_UPDATE_DATE         := l_date;
2624 
2625     -- We are ready for insert in GMD_SPEC_RESULTS, so then lets do it.
2626     IF NOT(GMD_SPEC_RESULTS_PVT.Insert_Row(p_spec_results => l_spec_result))
2627     THEN
2628       -- Insert Error
2629 
2630       IF (l_debug = 'Y') THEN
2631         	 gmd_debug.put_line('  gmd_results_grp.make_target_spec_the_base_spec    Insert Error  from GMD_SPEC_RESULTS_PVT.Insert_Row    RAISE e_spec_results_insert_error  ');
2632      		 END IF;
2633 
2634       RAISE e_spec_results_insert_error;
2635     END IF;
2636 
2637     IF (l_debug = 'Y') THEN
2638        gmd_debug.put_line('    A duplicate record is created in GMD_SPEC_RESULTS for Result ID - ' || l_res.result_id);
2639     END IF;
2640 
2641   END LOOP; -- For all the existing results
2642 
2643   -- Now create rows in GMD_RESULTS and GMD_SPEC_RESULTS for
2644   -- the tests that are missing and are part of the Target Spec
2645 
2646   IF (l_debug = 'Y') THEN
2647      gmd_debug.put_line('  Now create rows in GMD_RESULTS and GMD_SPEC_RESULTS for the tests that are missing and are part of the Target Spec');
2648   END IF;
2649 
2650   -- Go throug all the tests that are part of the Spec
2651   FOR l_spec_test IN c_spec_tests(p_target_spec_id)
2652   LOOP
2653 
2654     l_curr_replicate_cnt := 0;
2655 
2656     -- Find out how many times the test from the target spec is carried
2657     -- out in the exisitng result set.
2658 
2659      IF (l_debug = 'Y') THEN
2660         	 gmd_debug.put_line('before SELECT nvl(max(test_replicate_cnt), 0) ');
2661      		 END IF;
2662 
2663 
2664     SELECT nvl(max(test_replicate_cnt), 0)
2665     INTO   l_curr_replicate_cnt
2666     FROM   GMD_RESULTS
2667     WHERE  sample_id = l_sample.sample_id
2668     AND    test_id   = l_spec_test.test_id
2669     ;
2670 
2671 
2672      IF (l_debug = 'Y') THEN
2673         	 gmd_debug.put_line('after SELECT nvl(max(test_replicate_cnt), 0)  ');
2674      		 END IF;
2675 
2676 
2677     -- Find out how many times still we need to do the test
2678     -- in order for Target Spec to be used.
2679     l_missing_cnt := l_spec_test.test_replicate - l_curr_replicate_cnt;
2680 
2681     IF (l_debug = 'Y') THEN
2682       gmd_debug.put_line('    Test ID - ' || l_spec_test.test_id ||
2683                          ' Replicate in Spec - ' || l_spec_test.test_replicate ||
2684                          ' Already times performed - ' || l_curr_replicate_cnt ||
2685                          ' To be performed for times - ' || l_missing_cnt);
2686     END IF;
2687 
2688     IF ( l_missing_cnt > 0 ) THEN
2689 
2690       -- This means we need to carry this same test some more times.
2691       -- So go through l_missing_cnt times
2692       l_test_ids.DELETE;
2693       FOR i IN 1..l_missing_cnt
2694       LOOP
2695         -- Here we can re-use the procedure add_tests_to_sample
2696         -- so construct the input parameters and then just call that
2697         -- procedure.
2698         l_test_ids(i) := l_spec_test.test_id;
2699 
2700       END LOOP;  -- Test Replicate Loop
2701 
2702       IF (l_debug = 'Y') THEN
2703          gmd_debug.put_line('      Since it needs to be performed, Test IDs table created and calling add_tests_to_sample.');
2704       END IF;
2705 
2706       -- We have all the parameter for inserting tests to a sample
2707       add_tests_to_sample
2708       (  p_sample             => l_sample
2709        , p_test_ids           => l_test_ids
2710        , p_event_spec_disp_id => l_event_spec_disp.event_spec_disp_id
2711        , x_results_tab        => l_results_tab
2712        , x_spec_results_tab   => l_spec_results_tab
2713        , x_return_status      => l_return_status
2714       );
2715 
2716       IF l_return_status <> 'S' THEN
2717         -- Message must have been logged so just raise an exception.
2718         RAISE FND_API.G_EXC_ERROR;
2719       END IF;
2720 
2721       IF (l_debug = 'Y') THEN
2722          gmd_debug.put_line('      Test template record added to both GMD_RESULTD and GMD_SPEC_RESULTS.');
2723       END IF;
2724 
2725       -- We ignore other out parameters
2726 
2727     END IF; -- Either test is missing or needs more replicate
2728 
2729   END LOOP;  -- Spec Tests Loop
2730 
2731   -- Now all the test adding business is over. So let's see it the sample disposition
2732   -- can be changed to Complete
2733   gmd_results_grp.change_sample_disposition
2734               ( p_sample_id        => l_sample.sample_id
2735               , x_change_disp_to   => l_change_disp_to
2736               , x_return_status    => l_return_status
2737               , x_message_data     => l_message_data
2738               );
2739   IF l_return_status <> 'S' THEN
2740     FND_MESSAGE.SET_NAME('GMD', l_message_data);
2741     RAISE FND_API.G_EXC_ERROR;
2742   END IF;
2743 
2744   IF (l_debug = 'Y') THEN
2745      gmd_debug.put_line('Leaving Procedure - MAKE_TARGET_SPEC_THE_BASE_SPEC');
2746   END IF;
2747 
2748 
2749 
2750   RETURN;
2751 
2752   -- All systems GO...
2753 
2754 EXCEPTION
2755   WHEN    FND_API.G_EXC_ERROR
2756        OR e_spec_results_insert_error
2757        OR e_event_spec_disp_insert_error
2758        OR e_sample_spec_disp_insert_err
2759   THEN
2760     IF (l_debug = 'Y') THEN
2761      gmd_debug.put_line('Leaving Procedure - MAKE_TARGET_SPEC_THE_BASE_SPEC  with return status of ERROR ');
2762   END IF;
2763 
2764     x_return_status := FND_API.G_RET_STS_ERROR ;
2765   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2766 
2767     IF (l_debug = 'Y') THEN
2768      gmd_debug.put_line('Leaving Procedure - MAKE_TARGET_SPEC_THE_BASE_SPEC  with return status of unexpected ERROR ');
2769   END IF;
2770     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2771   WHEN OTHERS THEN
2772 
2773     IF (l_debug = 'Y') THEN
2774      gmd_debug.put_line('Leaving Procedure - MAKE_TARGET_SPEC_THE_BASE_SPEC  with return status for OTHERS of unexpected ERROR ');
2775   END IF;
2776     GMD_API_PUB.Log_Message('GMD_API_ERROR',
2777                             'PACKAGE','MAKE_TARGET_SPEC_THE_BASE_SPEC',
2778                             'ERROR', SUBSTR(SQLERRM,1,100));
2779     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2780 
2781 
2782 END make_target_spec_the_base_spec;
2783 
2784 --
2785 
2786 
2787 --Start of comments
2788 --+========================================================================+
2789 --| API Name    : use_target_spec_for_cmpst_rslt                           |
2790 --| TYPE        : Group                                                    |
2791 --| Notes       : This procedure is to make the target spec as the base    |
2792 --|               spec for a given composite result set.                   |
2793 --|                                                                        |
2794 --|               NOTE: THIS PROCEDURE RE-USES THE PROCEDURE               |
2795 --|                     make_target_spec_the_base_spec                     |
2796 --|                                                                        |
2797 --|                                                                        |
2798 --|                                                                        |
2799 --| PARAMETERS  :                                                          |
2800 --|                                                                        |
2801 --| 1. p_composite_spec_disp_id - Table of Sample IDs in which the test is |
2802 --|                               to be added.                             |
2803 --| 2. p_target_spec_id - Spec ID which will become base spec.             |
2804 --| 3. p_target_spec_cr_id - Spec VR ID which will become base spec VR.    |
2805 --|                                                                        |
2806 --| HISTORY                                                                |
2807 --|    Chetan Nagar	16-Sep-2002	Created.                           |
2808 --|                                                                        |
2809 --+========================================================================+
2810 -- End of comments
2811 
2812 
2813 PROCEDURE use_target_spec_for_cmpst_rslt
2814 (
2815   p_composite_spec_disp_id IN NUMBER
2816 , p_target_spec_id         IN NUMBER
2817 , p_target_spec_vr_id      IN NUMBER
2818 , x_return_status          OUT NOCOPY VARCHAR2
2819 ) IS
2820 
2821   -- Cursors
2822   CURSOR c_get_se_id (p_composite_spec_disp_id NUMBER) IS
2823   SELECT sampling_event_id
2824   FROM   gmd_composite_spec_disp csd, gmd_event_spec_disp esd
2825   WHERE  csd.composite_spec_disp_id = p_composite_spec_disp_id
2826   AND    csd.event_spec_disp_id = esd.event_spec_disp_id
2827   AND    csd.latest_ind = 'Y'
2828   AND    csd.delete_mark = 0
2829   AND    esd.spec_used_for_lot_attrib_ind = 'Y'
2830   AND    esd.delete_mark = 0
2831   ;
2832 
2833   -- Local Variables
2834   l_sampling_event_id              NUMBER;
2835   l_sample_ids                     GMD_API_PUB.number_tab;
2836   l_return_status                  VARCHAR2(1);
2837 
2838 BEGIN
2839 
2840   IF (l_debug = 'Y') THEN
2841      gmd_debug.put_line('Entered Procedure - USE_TARGET_SPEC_FOR_CMPST_RSLT');
2842   END IF;
2843 
2844   --  Initialize API return status to success
2845   x_return_status := FND_API.G_RET_STS_SUCCESS;
2846 
2847   -- Get the Sampling Event ID
2848   OPEN c_get_se_id (p_composite_spec_disp_id);
2849   FETCH c_get_se_id INTO l_sampling_event_id;
2850   IF c_get_se_id%NOTFOUND THEN
2851     CLOSE c_get_se_id;
2852     GMD_API_PUB.Log_Message('GMD_SAMPLING_EVENT_NOT_FOUND');
2853     RAISE FND_API.G_EXC_ERROR;
2854   END IF;
2855   CLOSE c_get_se_id;
2856 
2857   IF (l_debug = 'Y') THEN
2858      gmd_debug.put_line('  Working on SE ID - ' || l_sampling_event_id);
2859   END IF;
2860 
2861   -- Now get all the samples that are part of this Sampling Event
2862   get_sample_ids_for_se (p_sampling_event_id => l_sampling_event_id,
2863                          x_sample_ids        => l_sample_ids,
2864                          x_return_status     => l_return_status);
2865   IF l_return_status <> 'S' THEN
2866     -- Message must have been logged so just raise an exception.
2867     RAISE FND_API.G_EXC_ERROR;
2868   END IF;
2869 
2870   IF (l_debug = 'Y') THEN
2871      gmd_debug.put_line('  There are ' || l_sample_ids.COUNT || ' samples in this SE ID.' );
2872   END IF;
2873 
2874   -- If the no of samples count is zero then error out
2875   IF (l_sample_ids.COUNT = 0) THEN
2876     GMD_API_PUB.Log_Message('GMD_NO_SAMPLE_TO_COMPOSITE');
2877     RAISE FND_API.G_EXC_ERROR;
2878   END IF;
2879 
2880   -- Now call "make_target_spec_the_base_spec" for each sample ID
2881   FOR i in 1..l_sample_ids.COUNT
2882   LOOP
2883     IF (l_debug = 'Y') THEN
2884        gmd_debug.put_line('    Calling make_target_spec_the_base_spec for Sample ID - ' || l_sample_ids(i));
2885     END IF;
2886     -- For each sample make the target spec as base spec.
2887     make_target_spec_the_base_spec(p_sample_id          => l_sample_ids(i),
2888                                    p_target_spec_id     => p_target_spec_id,
2889                                    p_target_spec_vr_id  => p_target_spec_vr_id,
2890                                    x_return_status      => l_return_status);
2891     IF l_return_status <> 'S' THEN
2892       -- Message must have been logged so just raise an exception.
2893       RAISE FND_API.G_EXC_ERROR;
2894     END IF;
2895     IF (l_debug = 'Y') THEN
2896        gmd_debug.put_line('    Target Spec Changed for Sample ID - ' || l_sample_ids(i));
2897     END IF;
2898   END LOOP;
2899 
2900   IF (l_debug = 'Y') THEN
2901      gmd_debug.put_line('Leaving Procedure - USE_TARGET_SPEC_FOR_CMPST_RSLT');
2902   END IF;
2903 
2904   RETURN;
2905   -- All systems go...
2906 
2907 
2908 EXCEPTION
2909   WHEN FND_API.G_EXC_ERROR THEN
2910 
2911 
2912 
2913     x_return_status := FND_API.G_RET_STS_ERROR ;
2914   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2915     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2916   WHEN OTHERS THEN
2917     GMD_API_PUB.Log_Message('GMD_API_ERROR',
2918                             'PACKAGE','USE_TARGET_SPEC_FOR_CMPST_RSLT',
2919                             'ERROR', SUBSTR(SQLERRM,1,100));
2920     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2921 
2922 END use_target_spec_for_cmpst_rslt;
2923 
2924 
2925 
2926 
2927 
2928 
2929 
2930 
2931 
2932 
2933 
2934 
2935 --+========================================================================+
2936 --| API Name    : get_rslt_and_spec_rslt                                   |
2937 --| TYPE        : Group                                                    |
2938 --| Notes       : This procedure receives as input sample record and       |
2939 --|               retrieves results and spec results records.              |
2940 --|                                                                        |
2941 --|               If everything is fine then OUT parameter                 |
2942 --|               x_return_status is set to 'S' else appropriate           |
2943 --|               error message is put on the stack and error              |
2944 --|               is returned.                                             |
2945 --|                                                                        |
2946 --| HISTORY                                                                |
2947 --|   Ger Kelly  10 Sep 2002	Created.                                   |
2948 --|   GK         11 Sep 2002  Made changes to the results cursor for replicates |
2949 --|   GK         19 Sep 2002 Added event_spec_disp_id as parameter,        |
2950 --|                           changed results_rec to gmd_results_rec_tbl   |
2951 --|   GK	 24 Sep 2002 Changed the cursor c_res to incorporate analytical fns |
2952 --|   GK	 17 Oct 2002 B2621648 - Changed the IF, ELSIF to accomodate for
2953 --|			     chars ,e.g. text_range that have both num and chars
2954 --|  Sukarna Reddy 29 Oct 2002.  B2620851. Added code to fetch test results
2955 --|                            if sample is not associated with specification.
2956 --|   			        indented the procedure code.
2957 --|  Rameshwar     27-FEB-2003   B#2871126
2958 --|                              Added display_precisions column to the select list
2959 --|                              in the cursor c_get_spec_test_num
2960 --|                                                                        |
2961 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
2962 --|       in select for table - gmd_spec_tests_b                           |
2963 --|  	Rameshwar   13-APR-2004   B#3545701                                |
2964 --|                              Added non validated test type to retrieve |
2965 --|                              target values for non-validated tests.    |
2966 --========================================================================+
2967 PROCEDURE get_rslt_and_spec_rslt
2968 (
2969   p_sample_id             IN         NUMBER,
2970   p_source_spec_id	  IN         NUMBER,
2971   p_target_spec_id	  IN         NUMBER,
2972   p_event_spec_disp_id 	  IN         NUMBER,
2973   x_results_rec_tbl       OUT NOCOPY GMD_RESULTS_GRP.gmd_results_rec_tbl,
2974   x_return_status         OUT NOCOPY VARCHAR2) IS
2975 
2976 
2977 /* Local Variables */
2978 
2979   l_spec_id             NUMBER(15);
2980   i			NUMBER :=0;
2981   j                     NUMBER :=0;
2982   k                     NUMBER :=0;
2983 
2984   l_display_label	VARCHAR2(80);
2985   l_spec_ind		VARCHAR2(1);
2986   l_test_id	        NUMBER;
2987 
2988   l_results_rec_tbl     GMD_RESULTS_GRP.gmd_results_rec_tbl;
2989 
2990   x_test_ids		GMD_API_PUB.number_tab;
2991   l_sample_id		GMD_API_PUB.number_tab;
2992   return_status		VARCHAR2(1);
2993 
2994   /* Cursors */
2995 
2996   CURSOR c_res IS
2997      SELECT *
2998      FROM	gmd_result_data_points_gt;
2999 
3000   CURSOR c_get_type IS
3001       SELECT  t.test_type, t.test_code, t.test_method_id, t.expression, t.test_unit,
3002       	      m.test_method_code
3003       FROM    gmd_qc_tests_b t , gmd_test_methods_b m
3004       WHERE   t.test_id = l_test_id
3005       AND     t.test_method_id = m.test_method_id;
3006   LocalTypeRec	c_get_type%ROWTYPE;
3007 
3008   CURSOR c_get_display IS
3009     SELECT  v.display_label_numeric_range
3010     FROM    gmd_qc_test_values v
3011     WHERE   v.test_id = l_test_id;
3012   LocalDisRec   c_get_display%ROWTYPE;
3013 
3014   --BEGIN BUG#2871126 Rameshwar
3015   --Added display_precision to the selct list.
3016   CURSOR c_get_spec_test_num IS
3017       SELECT  s.min_value_num, s.max_value_num, s.target_value_num,s.display_precision
3018       FROM    gmd_spec_tests_b s
3019       WHERE   s.spec_id = l_spec_id
3020       AND     s.test_id = l_test_id
3021       AND     s.exclude_ind IS NULL;
3022 
3023   X_additional_test gmd_spec_results.additional_test_ind%TYPE;
3024   X_display_precision gmd_qc_tests_b.display_precision%TYPE;
3025   --END BUG#2871126.
3026 
3027   LocalNumRec   c_get_spec_test_num%ROWTYPE;
3028 
3029 
3030    CURSOR c_get_spec_test_char IS
3031       SELECT  s.min_value_char, s.max_value_char, s.target_value_char
3032       FROM    gmd_spec_tests_b s
3033       WHERE   s.spec_id = l_spec_id
3034       AND     s.test_id = l_test_id
3035       AND     s.exclude_ind IS NULL;
3036 
3037   LocalCharRec   c_get_spec_test_char%ROWTYPE;
3038 
3039   CURSOR c_get_results(p_sample_id NUMBER) IS
3040     SELECT *
3041     FROM gmd_results
3042     WHERE sample_id = p_sample_id;
3043 
3044     l_qc_test         gmd_qc_tests%rowtype;
3045     l_test_mthd       gmd_test_methods%rowtype;
3046 
3047     l_in_qc_test      gmd_qc_tests%rowtype;
3048     l_in_test_mthd    gmd_test_methods%rowtype;
3049 
3050     l_ret_sts         BOOLEAN;
3051 
3052 BEGIN
3053 
3054   IF (l_debug = 'Y') THEN
3055      gmd_debug.put_line('Entered Procedure - GET_RSLT_AND_SPEC_RSLT');
3056      gmd_debug.put_line('Input Parameters:');
3057      gmd_debug.put_line('    Sample ID     : ' || p_sample_id);
3058      gmd_debug.put_line('    Source Spec ID: ' || p_source_spec_id);
3059      gmd_debug.put_line('    Target Spec ID: ' || p_target_spec_id);
3060      gmd_debug.put_line('    Event Spec ID : ' || p_event_spec_disp_id);
3061   END IF;
3062 
3063   --  Initialize API return status to success
3064   x_return_status := FND_API.G_RET_STS_SUCCESS;
3065 
3066   -- begin Bug 2620851  29 Oct 2002
3067   i := 1;
3068 
3069   -- IF there is no specification associated with sample then get the information from GMD_RESULTS directly.
3070   IF (p_source_spec_id IS NULL) THEN
3071 
3072     IF (l_debug = 'Y') THEN
3073        gmd_debug.put_line('Source Spec ID is NULL. So fetch data from GMD_RESULTS.');
3074     END IF;
3075 
3076     FOR c_result_rec IN C_get_results(p_sample_id) LOOP
3077       l_results_rec_tbl(i).test_id := c_result_rec.test_id;
3078 
3079       l_in_qc_test.test_id := c_result_rec.test_id;
3080       l_ret_sts := gmd_qc_tests_pvt.fetch_row(p_gmd_qc_tests => l_in_qc_test,
3081  	        	                      x_gmd_qc_tests => l_qc_test);
3082       l_results_rec_tbl(i).test_code := l_qc_test.test_code;
3083       l_results_rec_tbl(i).test_type := l_qc_test.test_type;
3084       l_results_rec_tbl(i).unit := l_qc_test.test_unit;
3085 
3086       l_in_test_mthd.test_method_id := l_qc_test.test_method_id;
3087       l_ret_sts := GMD_TEST_METHODS_PVT.fetch_row(p_test_methods => l_in_test_mthd,
3088 		                                  x_test_methods => l_test_mthd);
3089       l_results_rec_tbl(i).method     := l_test_mthd.test_method_code;
3090       l_results_rec_tbl(i).result_num := c_result_rec.result_value_num;
3091       l_results_rec_tbl(i).result_char:= c_result_rec.result_value_char;
3092       l_results_rec_tbl(i).expression := l_qc_test.expression;
3093       l_results_rec_tbl(i).min_num    := l_qc_test.min_value_num;
3094       l_results_rec_tbl(i).max_num    := l_qc_test.max_value_num;
3095       i := i + 1;
3096 
3097       IF (l_debug = 'Y') THEN
3098          gmd_debug.put_line('Fetched Result row with RESULT ID: '|| c_result_rec.result_id );
3099          gmd_debug.put_line('                        Test Code: '|| l_qc_test.test_code ||
3100                                          ' Replicate: ' || c_result_rec.test_replicate_cnt );
3101       END IF;
3102 
3103     END LOOP;
3104     x_results_rec_tbl := l_results_rec_tbl;
3105     --  end Bug 2620851
3106 
3107   ELSE
3108 
3109     IF (l_debug = 'Y') THEN
3110        gmd_debug.put_line('We have the source spec, fetch data using POPULATE_RESULT_DATA_POINTS');
3111     END IF;
3112 
3113     --Since we have a specification
3114     --retrieve rows in GMD_RESULTS and GMD_SPEC_RESULTS for all the tests
3115     l_results_rec_tbl.DELETE;
3116     i := 0;
3117 
3118     -- Added Sep24 for getting tests without results
3119     l_sample_id(1) := p_sample_id;
3120     GMD_RESULTS_GRP.populate_result_data_points(p_sample_ids         => l_sample_id,
3121        					        p_event_spec_disp_id => p_event_spec_disp_id,
3122 					        x_return_status      => x_return_status);
3123 
3124     -- Get the results for each sample and spec
3125     FOR LocalResRec IN c_res LOOP
3126       i := i + 1;
3127 
3128       IF (l_debug = 'Y') THEN
3129          gmd_debug.put_line(' ');
3130          gmd_debug.put_line('Operating on row: ' || i || ' from POPULATE_RESULT_DATA_POINTS');
3131       END IF;
3132 
3133 
3134       IF LocalResRec.data_num IS NOT NULL THEN
3135         l_results_rec_tbl(i).result_num := LocalResRec.data_num;
3136       END IF;
3137       IF LocalResRec.data_char IS NOT NULL THEN
3138         l_results_rec_tbl(i).result_char := LocalResRec.data_char;
3139       END IF;
3140 
3141       l_test_id := LocalResRec.test_id;
3142       l_results_rec_tbl(i).test_id := l_test_id;
3143 
3144       -- For each test type get the test and method info
3145       OPEN c_get_type;
3146       FETCH c_get_type INTO LocalTypeRec;
3147       l_results_rec_tbl(i).test_type := LocalTypeRec.test_type;
3148       l_results_rec_tbl(i).test_code := LocalTypeRec.test_code;
3149       l_results_rec_tbl(i).expression := LocalTypeRec.expression;
3150       l_results_rec_tbl(i).unit := LocalTypeRec.test_unit;
3151       l_results_rec_tbl(i).method := LocalTypeRec.test_method_code;
3152       CLOSE c_get_type;
3153 
3154       IF (l_debug = 'Y') THEN
3155          gmd_debug.put_line('Retrieved Test and Method info:');
3156          gmd_debug.put_line('  Test: ' || LocalTypeRec.test_code || ' Method: ' ||  LocalTypeRec.test_method_code);
3157       END IF;
3158 
3159       -- Get the values for the Current Spec
3160       l_spec_id := p_source_spec_id;
3161       OPEN c_get_spec_test_num;
3162       FETCH c_get_spec_test_num INTO LocalNumRec;
3163       IF c_get_spec_test_num%FOUND THEN
3164 
3165  	  l_results_rec_tbl(i).target_num := LocalNumRec.target_value_num;
3166     	  l_results_rec_tbl(i).min_num := LocalNumRec.min_value_num;
3167     	  l_results_rec_tbl(i).max_num := LocalNumRec.max_value_num;
3168 
3169         --BEGIN BUG#2871126
3170         --Fetching the display precision into table type.
3171           l_results_rec_tbl(i).display_precision:=LocalNumRec.display_precision;
3172         --END BUG#2871126
3173 
3174         IF (l_debug = 'Y') THEN
3175            gmd_debug.put_line('Retrieved Spec info:');
3176         END IF;
3177       END IF;
3178 
3179       CLOSE c_get_spec_test_num;
3180 
3181        --BEGIN BUG#2871126 Rameshwar
3182       SELECT sr.additional_test_ind
3183       INTO   X_additional_test
3184       FROM   gmd_spec_results sr
3185       WHERE  sr.event_spec_disp_id = p_event_spec_disp_id
3186       AND    sr.result_id          = LocalResRec.result_id
3187       ;
3188 
3189       IF X_additional_test = 'Y' THEN
3190 
3191         SELECT display_precision
3192         INTO X_display_precision
3193         FROM gmd_qc_tests_b
3194         WHERE test_id = l_test_id;
3195 
3196         l_results_rec_tbl(i).display_precision := X_display_precision;
3197 
3198         IF (l_debug = 'Y') THEN
3199            gmd_debug.put_line('The test is additional so get the display precision (' ||
3200                                                       X_display_precision || ' ) from Test');
3201         END IF;
3202 
3203       END IF;
3204       --END BUG#2871126
3205 
3206 
3207       IF (l_debug = 'Y') THEN
3208          gmd_debug.put_line('Now get the values for the Target Spec.');
3209       END IF;
3210 
3211       --Get the values for the Comparison Spec
3212       l_spec_id := p_target_spec_id;
3213 
3214       OPEN c_get_spec_test_num;
3215       FETCH c_get_spec_test_num INTO LocalNumRec;
3216       IF c_get_spec_test_num%FOUND THEN
3217         l_results_rec_tbl(i).spec_target_num := LocalNumRec.target_value_num;
3218 	  l_results_rec_tbl(i).spec_min_num := LocalNumRec.min_value_num;
3219     	  l_results_rec_tbl(i).spec_max_num := LocalNumRec.max_value_num;
3220 
3221           --BEGIN BUG#2871126 Rameshwar
3222     	  --Fetching the display precision into table type.
3223     	  l_results_rec_tbl(i).spec_display_precision:=LocalNumRec.display_precision;
3224     	  --END BUG#2871126
3225 
3226       END IF;
3227       CLOSE c_get_spec_test_num;
3228      --BEGIN BUG#3545701
3229      --Added non-validated test type
3230       IF LocalTypeRec.test_type IN ('V', 'T', 'L', 'E','U') THEN
3231      --END BUG#3545701
3232         -- Get the values for the Current Specfor chars
3233         l_spec_id := p_source_spec_id;
3234 
3235         OPEN c_get_spec_test_char;
3236         FETCH c_get_spec_test_char INTO LocalCharRec;
3237         IF c_get_spec_test_char%FOUND THEN
3238           l_results_rec_tbl(i).target_char := LocalCharRec.target_value_char;
3239           l_results_rec_tbl(i).min_char := LocalCharRec.min_value_char;
3240           l_results_rec_tbl(i).max_char := LocalCharRec.max_value_char;
3241         END IF;
3242         CLOSE c_get_spec_test_char;
3243 
3244         --Get the values for the Comparison Spec
3245         l_spec_id := p_target_spec_id;
3246         OPEN c_get_spec_test_char;
3247         FETCH c_get_spec_test_char INTO LocalCharRec;
3248         IF c_get_spec_test_char%FOUND THEN
3249           l_results_rec_tbl(i).spec_target_char := LocalCharRec.target_value_char;
3250  	  l_results_rec_tbl(i).spec_min_char := LocalCharRec.min_value_char;
3251           l_results_rec_tbl(i).spec_max_char := LocalCharRec.max_value_char;
3252         END IF;
3253         CLOSE c_get_spec_test_char;
3254       END IF;
3255 
3256       IF (l_debug = 'Y') THEN
3257          gmd_debug.put_line('Got the Test and Spec info');
3258       END IF;
3259 
3260       l_results_rec_tbl(i).in_spec := GMD_RESULTS_GRP.rslt_is_in_spec(p_source_spec_id,
3261       								      l_results_rec_tbl(i).test_id,
3262        								      l_results_rec_tbl(i).result_num,
3263        								      l_results_rec_tbl(i).result_char);
3264       IF (l_debug = 'Y') THEN
3265          gmd_debug.put_line('in spec res '||l_results_rec_tbl(i).in_spec);
3266       END IF;
3267       l_results_rec_tbl(i).spec_in_spec := GMD_RESULTS_GRP.rslt_is_in_spec( p_target_spec_id,
3268       									    l_results_rec_tbl(i).test_id,
3269        									    l_results_rec_tbl(i).result_num,
3270        									    l_results_rec_tbl(i).result_char);
3271 
3272       IF (l_debug = 'Y') THEN
3273          gmd_debug.put_line('A Spec In Spec Res '||l_results_rec_tbl(i).spec_in_spec);
3274       END IF;
3275       x_results_rec_tbl(i) := l_results_rec_tbl(i);
3276 
3277     END LOOP;  -- Results test Loop
3278 
3279     j := i;
3280     l_spec_id := p_target_spec_id;
3281 
3282     GMD_RESULTS_GRP.compare_rslt_and_spec(p_sample_id,
3283     					  l_spec_id,
3284     					  x_test_ids,
3285     					  return_status);
3286 
3287     FOR k in 1..x_test_ids.COUNT LOOP
3288       i := i + k;
3289       l_test_id := x_test_ids(k);
3290       OPEN c_get_type;
3291       FETCH c_get_type INTO LocalTypeRec;
3292       CLOSE c_get_type;
3293       l_results_rec_tbl(i).test_code := LocalTypeRec.test_code;
3294       l_results_rec_tbl(i).test_type := LocalTypeRec.test_type;
3295       l_results_rec_tbl(i).spec_test_id := l_test_id;
3296       l_results_rec_tbl(i).spec_in_spec := GMD_RESULTS_GRP.rslt_is_in_spec( p_target_spec_id,
3297       									    l_test_id,
3298       									    l_results_rec_tbl(i).result_num,
3299       									    l_results_rec_tbl(i).result_char);
3300       IF (l_debug = 'Y') THEN
3301          gmd_debug.put_line('B Spec In Spec Res '||l_results_rec_tbl(i).spec_in_spec);
3302       END IF;
3303 
3304       IF LocalTypeRec.test_type IN ('N', 'L', 'V', 'T', 'E', 'U')  THEN
3305          -- Get the values for the Comparison Spec
3306 
3307         OPEN c_get_spec_test_num;
3308         FETCH c_get_spec_test_num INTO LocalNumRec;
3309         IF c_get_spec_test_num %FOUND THEN
3310           l_results_rec_tbl(i).spec_target_num := LocalNumRec.target_value_num;
3311           l_results_rec_tbl(i).spec_min_num := LocalNumRec.min_value_num;
3312           l_results_rec_tbl(i).spec_max_num := LocalNumRec.max_value_num;
3313           --BEGIN BUG#2871126 Rameshwar
3314           l_results_rec_tbl(i).spec_display_precision:=LocalNumRec.display_precision;
3315           --END BUG#2871126
3316 
3317         END IF;
3318         CLOSE c_get_spec_test_num;
3319       END IF;
3320 
3321       IF LocalTypeRec.test_type IN ('V', 'T', 'L', 'E','U') THEN
3322         -- Get the values for the Comparison Test for chars
3323 
3324         OPEN c_get_spec_test_char;
3325         FETCH c_get_spec_test_char INTO LocalCharRec;
3326         IF c_get_spec_test_char%FOUND THEN
3327       	  l_results_rec_tbl(i).spec_target_char := LocalCharRec.target_value_char;
3328 	  l_results_rec_tbl(i).spec_min_char := LocalCharRec.min_value_char;
3329           l_results_rec_tbl(i).spec_max_char := LocalCharRec.max_value_char;
3330       	END IF;
3331  	CLOSE c_get_spec_test_char;
3332       END IF;
3333         x_results_rec_tbl(i) := l_results_rec_tbl(i);
3334     END LOOP;
3335    END IF;
3336 
3337 EXCEPTION
3338   WHEN FND_API.G_EXC_ERROR THEN
3339     x_return_status := FND_API.G_RET_STS_ERROR ;
3340   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3341     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3342   WHEN OTHERS THEN
3343     GMD_API_PUB.Log_Message('GMD_API_ERROR',
3344                             'PACKAGE','GET_SAMPLE_IDS_FOR_SE',
3345                             'ERROR', SUBSTR(SQLERRM,1,100));
3346     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3347 
3348 END get_rslt_and_spec_rslt;
3349 
3350 
3351 
3352 --Start of comments
3353 --+========================================================================+
3354 --| API Name    : composite_exist                                          |
3355 --| TYPE        : Group                                                    |
3356 --| Notes       : This procedure checks if there does not exist            |
3357 --|               composite results for the given event_spec_disp_id       |
3358 --|               or if the current composite are out of sync.             |
3359 --|                                                                        |
3360 --| HISTORY                                                                |
3361 --|    Chetan Nagar	26-Sep-2002	Created.                           |
3362 --|                                                                        |
3363 --+========================================================================+
3364 -- End of comments
3365 
3366 PROCEDURE composite_exist
3367 (
3368   p_sampling_event_id  IN         NUMBER
3369 , p_event_spec_disp_id IN         NUMBER
3370 , x_composite_exist    OUT NOCOPY VARCHAR2
3371 , x_composite_valid    OUT NOCOPY VARCHAR2
3372 , x_return_status      OUT NOCOPY VARCHAR2
3373 )
3374 IS
3375 
3376   -- Cursors
3377   CURSOR c_composite (p_event_spec_disp_id NUMBER) IS
3378   SELECT 1
3379   FROM   gmd_composite_spec_disp
3380   WHERE  event_spec_disp_id = p_event_spec_disp_id
3381   AND    nvl(latest_ind, 'N') = 'Y'
3382   ;
3383 
3384   CURSOR c_composite_valid (p_sampling_event_id NUMBER) IS
3385   SELECT nvl(recomposite_ind, 'N')
3386   FROM   gmd_sampling_events
3387   WHERE  sampling_event_id = p_sampling_event_id
3388   ;
3389 
3390   -- Local Variables
3391   dummy                  PLS_INTEGER;
3392   l_recomposite_ind      VARCHAR2(1);
3393   l_event_spec_disp_id   NUMBER;
3394 
3395 BEGIN
3396 
3397   IF (l_debug = 'Y') THEN
3398      gmd_debug.put_line('Entered Procedure - COMPOSITE_EXIST');
3399   END IF;
3400 
3401   --  Initialize API return status to success
3402   x_return_status := FND_API.G_RET_STS_SUCCESS;
3403 
3404   x_composite_exist := 'N';
3405   x_composite_valid := 'N';
3406 
3407   IF (p_sampling_event_id IS NULL AND p_event_spec_disp_id IS NULL) THEN
3408     GMD_API_PUB.Log_Message('GMD_INVALID_PARAMETERS');
3409     RAISE FND_API.G_EXC_ERROR;
3410   END IF;
3411 
3412   IF (p_event_spec_disp_id IS NULL) THEN
3413     l_event_spec_disp_id := get_current_event_spec_disp_id(p_sampling_event_id);
3414     IF (l_event_spec_disp_id IS NULL) THEN
3415       GMD_API_PUB.Log_Message('GMD_EVENT_SPEC_NOT_FOUND');
3416       RAISE FND_API.G_EXC_ERROR;
3417     END IF;
3418   ELSE
3419     l_event_spec_disp_id := p_event_spec_disp_id;
3420   END IF;
3421 
3422   IF (l_debug = 'Y') THEN
3423      gmd_debug.put_line('SE ID: ' || p_sampling_event_id || ' ESD ID: ' || l_event_spec_disp_id);
3424   END IF;
3425 
3426   -- See if we have the composite for the event_spec
3427   OPEN c_composite(l_event_spec_disp_id);
3428   FETCH c_composite INTO dummy;
3429   IF c_composite%FOUND THEN
3430      x_composite_exist := 'Y';
3431     IF (l_debug = 'Y') THEN
3432        gmd_debug.put_line('Composite Exist.');
3433     END IF;
3434   ELSE
3435     NULL;
3436     IF (l_debug = 'Y') THEN
3437        gmd_debug.put_line('Composite DOES NOT Exist.');
3438     END IF;
3439   END IF;
3440 
3441   CLOSE c_composite;
3442 
3443   IF (x_composite_exist = 'Y') THEN
3444     -- See if this composite is Valid
3445     OPEN c_composite_valid(p_sampling_event_id);
3446     FETCH c_composite_valid INTO l_recomposite_ind;
3447     IF c_composite_valid%NOTFOUND THEN
3448       CLOSE c_composite_valid;
3449       GMD_API_PUB.Log_Message('GMD_SAMPLING_EVENT_NOT_FOUND');
3450       RAISE FND_API.G_EXC_ERROR;
3451     END IF;
3452     CLOSE c_composite_valid;
3453 
3454     IF l_recomposite_ind = 'Y' THEN
3455       x_composite_valid := 'N';
3456       IF (l_debug = 'Y') THEN
3457          gmd_debug.put_line('Composite IS NOT Valid.');
3458       END IF;
3459     ELSE
3460       x_composite_valid := 'Y';
3461       IF (l_debug = 'Y') THEN
3462          gmd_debug.put_line('Composite IS Valid.');
3463       END IF;
3464     END IF;
3465   END IF;
3466 
3467   IF (l_debug = 'Y') THEN
3468      gmd_debug.put_line('Leaving Procedure - COMPOSITE_EXIST');
3469   END IF;
3470   RETURN;
3471 
3472 EXCEPTION
3473   WHEN FND_API.G_EXC_ERROR THEN
3474     x_return_status := FND_API.G_RET_STS_ERROR ;
3475   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3476     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3477   WHEN OTHERS THEN
3478     GMD_API_PUB.Log_Message('GMD_API_ERROR',
3479                             'PACKAGE','GET_SAMPLE_IDS_FOR_SE',
3480                             'ERROR', SUBSTR(SQLERRM,1,100));
3481     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3482 
3483 
3484 END composite_exist;
3485 
3486 
3487 
3488 
3489 --Start of comments
3490 --+========================================================================+
3491 --| API Name    : se_recomposite_required                                  |
3492 --| TYPE        : Group                                                    |
3493 --| Notes       : This procedure checks if there exist                     |
3494 --|               composite results for the given event_spec_disp_id.      |
3495 --|               If YES then it will mark that composite as NOT-Current   |
3496 --|               in gmd_smapling_events.                                  |
3497 --|                                                                        |
3498 --| HISTORY                                                                |
3499 --|    Chetan Nagar	26-Sep-2002	Created.                           |
3500 --|                                                                        |
3501 --+========================================================================+
3502 -- End of comments
3503 
3504 PROCEDURE se_recomposite_required
3505 (
3506   p_sampling_event_id  IN         NUMBER
3507 , p_event_spec_disp_id IN         NUMBER
3508 , x_return_status      OUT NOCOPY VARCHAR2
3509 )
3510 IS
3511 
3512   -- Cursors
3513   CURSOR c_composite (p_event_spec_disp_id NUMBER) IS
3514   SELECT 1
3515   FROM   gmd_composite_spec_disp
3516   WHERE  event_spec_disp_id = p_event_spec_disp_id
3517   AND    nvl(latest_ind, 'N') = 'Y'
3518   ;
3519 
3520   -- Local Variables
3521   dummy               PLS_INTEGER;
3522   l_event_spec_disp_id   NUMBER;
3523 
3524 BEGIN
3525   --  Initialize API return status to success
3526   x_return_status := FND_API.G_RET_STS_SUCCESS;
3527 
3528   IF (p_sampling_event_id IS NULL AND p_event_spec_disp_id IS NULL) THEN
3529     GMD_API_PUB.Log_Message('GMD_INVALID_PARAMETERS');
3530     RAISE FND_API.G_EXC_ERROR;
3531   END IF;
3532 
3533   IF (p_event_spec_disp_id IS NULL) THEN
3534     l_event_spec_disp_id := get_current_event_spec_disp_id(p_sampling_event_id);
3535     IF (l_event_spec_disp_id IS NULL) THEN
3536       GMD_API_PUB.Log_Message('GMD_EVENT_SPEC_NOT_FOUND');
3537       RAISE FND_API.G_EXC_ERROR;
3538     END IF;
3539   ELSE
3540     l_event_spec_disp_id := p_event_spec_disp_id;
3541   END IF;
3542 
3543   -- See if we have the composite for the event_spec
3544   OPEN c_composite(l_event_spec_disp_id);
3545   FETCH c_composite INTO dummy;
3546   IF c_composite%FOUND THEN
3547     -- We have found that the Sampling Event is composited before.
3548     -- So update the sampling event that recomposite is required.
3549     UPDATE gmd_sampling_events
3550     SET    recomposite_ind = 'Y'
3551     WHERE  sampling_event_id = p_sampling_event_id;
3552   END IF;
3553   CLOSE c_composite;
3554 
3555   RETURN;
3556 
3557 EXCEPTION
3558   WHEN FND_API.G_EXC_ERROR THEN
3559     x_return_status := FND_API.G_RET_STS_ERROR ;
3560   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3561     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3562   WHEN OTHERS THEN
3563     GMD_API_PUB.Log_Message('GMD_API_ERROR',
3564                             'PACKAGE','GET_SAMPLE_IDS_FOR_SE',
3565                             'ERROR', SUBSTR(SQLERRM,1,100));
3566     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3567 
3568 
3569 END se_recomposite_required;
3570 
3571 
3572 
3573 --Start of comments
3574 --+========================================================================+
3575 --| API Name    : result_recomposite_required                              |
3576 --| TYPE        : Group                                                    |
3577 --| Notes       : This procedure checks if there exist                     |
3578 --|               composite results for the given result_id.               |
3579 --|               If YES then it will mark that composite as NOT-Current   |
3580 --|               in gmd_smapling_events.                                  |
3581 --|                                                                        |
3582 --| HISTORY                                                                |
3583 --|    Chetan Nagar	26-Sep-2002	Created.                           |
3584 --|                                                                        |
3585 --+========================================================================+
3586 -- End of comments
3587 
3588 PROCEDURE result_recomposite_required
3589 (
3590   p_result_id          IN         NUMBER
3591 , p_event_spec_disp_id IN         NUMBER
3592 , x_return_status      OUT NOCOPY VARCHAR2
3593 )
3594 IS
3595 
3596   -- Cursors
3597   CURSOR c_composite (p_event_spec_disp_id NUMBER, p_result_id NUMBER) IS
3598   SELECT 1
3599   FROM   gmd_composite_spec_disp csd, gmd_composite_results cr, gmd_composite_result_assoc cra
3600   WHERE  csd.event_spec_disp_id = p_event_spec_disp_id
3601   AND    csd.composite_spec_disp_id = cr.composite_spec_disp_id
3602   AND    cr.composite_result_id = cra.composite_result_id
3603   AND    cra.result_id = p_result_id
3604   AND    nvl(csd.latest_ind, 'N') = 'Y'
3605   ;
3606 
3607   -- Local Variables
3608   dummy               PLS_INTEGER;
3609 
3610 BEGIN
3611   --  Initialize API return status to success
3612   x_return_status := FND_API.G_RET_STS_SUCCESS;
3613 
3614   IF (p_result_id IS NULL OR p_event_spec_disp_id IS NULL) THEN
3615     GMD_API_PUB.Log_Message('GMD_INVALID_PARAMETERS');
3616     RAISE FND_API.G_EXC_ERROR;
3617   END IF;
3618 
3619   -- See if we have the composite for the result
3620   OPEN c_composite(p_event_spec_disp_id, p_result_id);
3621   FETCH c_composite INTO dummy;
3622   IF c_composite%FOUND THEN
3623     -- We have found that the Sampling Event is composited before.
3624     -- So update the sampling event that recomposite is required.
3625     UPDATE gmd_sampling_events
3626     SET    recomposite_ind = 'Y'
3627     WHERE  sampling_event_id =
3628       (SELECT sampling_event_id
3629        FROM   gmd_event_spec_disp
3630        WHERE  event_spec_disp_id = p_event_spec_disp_id);
3631   END IF;
3632   CLOSE c_composite;
3633 
3634   RETURN;
3635 
3636 EXCEPTION
3637   WHEN FND_API.G_EXC_ERROR THEN
3638     x_return_status := FND_API.G_RET_STS_ERROR ;
3639   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3640     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3641   WHEN OTHERS THEN
3642     GMD_API_PUB.Log_Message('GMD_API_ERROR',
3643                             'PACKAGE','GET_SAMPLE_IDS_FOR_SE',
3644                             'ERROR', SUBSTR(SQLERRM,1,100));
3645     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3646 
3647 
3648 END result_recomposite_required;
3649 
3650 
3651 
3652 
3653 --Start of comments
3654 --+========================================================================+
3655 --| API Name    : get_sample_ids_for_se                                    |
3656 --| TYPE        : Group                                                    |
3657 --| Notes       : This procedure retrieves all sample_id s that are part   |
3658 --|               of the sampling_event_id supplied.                       |
3659 --|                                                                        |
3660 --|               This procedure should be called when user wants to       |
3661 --|               composite samples.                                       |
3662 --|                                                                        |
3663 --| HISTORY                                                                |
3664 --|    Chetan Nagar	04-Sep-2002	Created.                           |
3665 --|    Chetan Nagar	09-Dec-2002	Changed disposition from gmd_samples|
3666 --|                                     To gmd_sample_spec_disp.           |
3667 --|                                                                        |
3668 --+========================================================================+
3669 -- End of comments
3670 
3671 PROCEDURE get_sample_ids_for_se
3672 (
3673   p_sampling_event_id  IN         NUMBER
3674 , x_sample_ids         OUT NOCOPY GMD_API_PUB.number_tab
3675 , x_return_status      OUT NOCOPY VARCHAR2
3676 )
3677 IS
3678 
3679     -- Bug 3079877: do not composite planning samples
3680   CURSOR c_sample(p_sampling_event_id NUMBER) IS
3681   SELECT s.sample_id
3682   FROM   gmd_event_spec_disp esd,
3683          gmd_sample_spec_disp ssd,
3684          gmd_samples s
3685   WHERE  esd.sampling_event_id = p_sampling_event_id
3686   AND    esd.spec_used_for_lot_attrib_ind = 'Y'
3687   AND    esd.event_spec_disp_id = ssd.event_spec_disp_id
3688   AND    ssd.sample_id = s.sample_id
3689   AND    nvl(ssd.disposition, 'XX') NOT IN ('0RT', '7CN', '0PL')
3690   AND    esd.delete_mark     = 0
3691   AND    ssd.delete_mark     = 0
3692   AND    s.delete_mark       = 0
3693   ORDER BY s.sample_id
3694   ;
3695 
3696   -- Local Variables
3697   i                    PLS_INTEGER;
3698 
3699 BEGIN
3700 
3701   IF (l_debug = 'Y') THEN
3702      gmd_debug.put_line('Entered Procedure - GET_SAMPLE_IDS_FOR_SE');
3703   END IF;
3704 
3705   --  Initialize API return status to success
3706   x_return_status := FND_API.G_RET_STS_SUCCESS;
3707 
3708   -- Now get all the samples that are part of Sampling Event
3709   i := 1;
3710   FOR sample_rec IN c_sample(p_sampling_event_id)
3711   LOOP
3712     x_sample_ids(i) := sample_rec.sample_id;
3713     i := i + 1;
3714   END LOOP;
3715   IF (l_debug = 'Y') THEN
3716      gmd_debug.put_line('  Sampling Event ID - ' || p_sampling_event_id || ' has - ' || x_sample_ids.COUNT || ' samples.');
3717      gmd_debug.put_line('Leaving Procedure - GET_SAMPLE_IDS_FOR_SE');
3718   END IF;
3719   RETURN;
3720 
3721 EXCEPTION
3722   WHEN    FND_API.G_EXC_ERROR THEN
3723     x_return_status := FND_API.G_RET_STS_ERROR ;
3724   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3725     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3726   WHEN OTHERS THEN
3727     GMD_API_PUB.Log_Message('GMD_API_ERROR',
3728                             'PACKAGE','GET_SAMPLE_IDS_FOR_SE',
3729                             'ERROR', SUBSTR(SQLERRM,1,100));
3730     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3731 
3732 END get_sample_ids_for_se;
3733 
3734 
3735 
3736 
3737 --Start of comments
3738 --+========================================================================+
3739 --| API Name    : populate_result_data_points                              |
3740 --| TYPE        : Group                                                    |
3741 --| Notes       : This procedure populates global session temporary table  |
3742 --|               with the result value for each test of the sample given. |
3743 --|                                                                        |
3744 --|               Since there can be multiple replicates for the test in   |
3745 --|               a given sample, the last result, defined as having the   |
3746 --|               latest result_date, is considered as test result and is  |
3747 --|               picked up.                                               |
3748 --|                                                                        |
3749 --|               IF THERE ARE MORE THAN ONE RESULTs WITH THE SAME LATEST  |
3750 --|               result_date FOR A GIVEN sample_id AND test_id THEN       |
3751 --|               test_replicate_cnt WILL BE USED TO BREAK THE TIE.        |
3752 --|                                                                        |
3753 --| PARAMETERS  :                                                          |
3754 --|                                                                        |
3755 --| 1. p_sample_ids - Table of Sample IDs                                  |
3756 --| 2. p_event_spec_disp_id - Event Spec ID                                |
3757 --| 3. x_return_status  - Return Status                                    |
3758 --|                                                                        |
3759 --| HISTORY                                                                |
3760 --|    Chetan Nagar	03-Sep-2002	Created.                           |
3761 --|    LeAta Jackson    14-Oct-2002     Added NVL around date in order by. |
3762 --|                                      So results with dates are chosen  |
3763 --|                                      before rows with no results/dates.|
3764 --|    Jeff Baird       12-Nov-2002     Bug #2626977 Removed reference to  |
3765 --|                                      SY$MIN_DATE profile.              |
3766 --|    Manish Gupta     13-Jan-2004     Bug #B3373760, changed 50(Zero) to |
3767 --|                                     5O(Letter O)                       |
3768 --|    Manish Gupta     20-Jan-2004     Bug #B3358298, Included gmd_samples|
3769 --|                                     so that the retain_as samples are  |
3770 --|                                     Excluded.                          |
3771 --+========================================================================+
3772 -- End of comments
3773 
3774 PROCEDURE populate_result_data_points
3775 (
3776   p_sample_ids         IN GMD_API_PUB.number_tab
3777 , p_event_spec_disp_id IN NUMBER
3778 , x_return_status      OUT NOCOPY VARCHAR2
3779 )
3780 IS
3781 
3782   -- Local Variables
3783   l_sql_stmt           VARCHAR2(2000);
3784   l_start_date         DATE;
3785 
3786 BEGIN
3787 
3788   IF (l_debug = 'Y') THEN
3789      gmd_debug.put_line('Entering Procedure - POPULATE_RESULT_DATA_POINTS');
3790   END IF;
3791 
3792   --  Initialize API return status to success
3793   x_return_status := FND_API.G_RET_STS_SUCCESS;
3794   l_start_date  := GMA_CORE_PKG.get_date_constant_d('SY$MIN_DATE');
3795 -- Bug #2626977 (JKB) Removed reference to date profile above.
3796 
3797   EXECUTE IMMEDIATE 'delete from gmd_result_data_points_gt';
3798   l_sql_stmt :=
3799     'INSERT INTO gmd_result_data_points_gt (result_id, test_id, exclude_ind,  data_num, data_char)'
3800   ||' ('
3801   ||'  SELECT result_id, test_id, 0, result_value_num, result_value_char FROM'
3802   ||'  ('
3803   ||'    SELECT r.result_id, r.test_id, r.result_value_num, r.result_value_char,'
3804   ||'           r.result_date, r.test_replicate_cnt,'
3805   ||'           last_value(r.result_id)'
3806   ||'           over (partition by r.test_id order by NVL(r.result_date, :l_start_date),'
3807   ||'                                                 r.test_replicate_cnt'
3808   ||'                 range between unbounded preceding and unbounded following) rmax_id'
3809   ||'    FROM   gmd_results r, gmd_spec_results sr, gmd_samples s'
3810   ||'    WHERE  r.result_id = sr.result_id'
3811   ||'    AND    r.sample_id = :l_sample_id'
3812   ||'    AND    sr.event_spec_disp_id = :l_event_spec_disp_id'
3813   ||'    AND    nvl(sr.evaluation_ind, ' || '''' || 'XX' || '''' || ')  not in ('
3814   ||                         '''' || '5O' || '''' ||','|| '''' || '4C' || '''' ||')'
3815   ||'    AND    sr.delete_mark = 0'
3816   ||'    AND    r.delete_mark = 0'
3817   ||'    AND    r.sample_id = s.sample_id'
3818   ||'    AND    s.retain_as IS NULL'
3819   ||'  )'
3820   ||'  WHERE result_id = rmax_id'
3821   ||')'
3822   ;
3823 
3824   -- The code below is now removed/changed from the SQL Above
3825   --||'    FROM   gmd_results r, gmd_spec_results sr, gmd_qc_tests_b t'
3826   --||'    AND    r.test_id   = t.test_id'
3827   --||'    AND    t.test_type <> ' || '''' || 'U' || ''''
3828 
3829   -- GO through all the sample_ids and populate _GTMP table
3830   FOR i in 1..p_sample_ids.COUNT
3831   LOOP
3832     IF (l_debug = 'Y') THEN
3833        gmd_debug.put_line('  Finding results for Sample ID - ' || p_sample_ids(i));
3834     END IF;
3835     EXECUTE IMMEDIATE l_sql_stmt USING l_start_date, p_sample_ids(i), p_event_spec_disp_id;
3836   END LOOP;
3837 
3838   dump_data_points;
3839 
3840   IF (l_debug = 'Y') THEN
3841      gmd_debug.put_line('Leaving Procedure - POPULATE_RESULT_DATA_POINTS');
3842   END IF;
3843   -- All systems GO...
3844 
3845 EXCEPTION
3846   WHEN    FND_API.G_EXC_ERROR THEN
3847     x_return_status := FND_API.G_RET_STS_ERROR ;
3848   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3849     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3850   WHEN OTHERS THEN
3851     GMD_API_PUB.Log_Message('GMD_API_ERROR',
3852                             'PACKAGE','POPULATE_RESULT_DATA_POINTS',
3853                             'ERROR', SUBSTR(SQLERRM,1,100));
3854     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3855 
3856 END populate_result_data_points;
3857 
3858 
3859 PROCEDURE dump_data_points IS
3860   CURSOR c1 IS
3861   SELECT *
3862   FROM   gmd_result_data_points_gt
3863   ORDER BY TEST_ID;
3864 BEGIN
3865   IF (l_debug = 'Y') THEN
3866      gmd_debug.put_line('Data in session table - gmd_result_data_points_gt');
3867      gmd_debug.put_line('Result ID    Test ID    Data Num     Data Char    Exclude Ind');
3868   END IF;
3869 
3870   FOR c_rec IN c1
3871   LOOP
3872     IF (l_debug = 'Y') THEN
3873        gmd_debug.put_line(lpad(c_rec.result_id, 9, ' ')||' '||
3874                        lpad(c_rec.test_id, 10, ' ')||' '||
3875                        lpad(nvl(c_rec.data_num, 0), 15, ' ')||' '||
3876                        lpad(nvl(c_rec.data_char, 'NULL'), 13, ' ')||' '||
3877                        lpad(c_rec.exclude_ind, 14, ' '));
3878     END IF;
3879   END LOOP;
3880 END;
3881 
3882 
3883 
3884 --Start of comments
3885 --+========================================================================+
3886 --| API Name    : create_composite_rows                                    |
3887 --| TYPE        : Group                                                    |
3888 --| Notes       : NEED TO WRITE SOMETHING HERE                             |
3889 --|                                                                        |
3890 --|                                                                        |
3891 --|                                                                        |
3892 --|                                                                        |
3893 --|                                                                        |
3894 --| HISTORY                                                                |
3895 --|    Chetan Nagar	04-Sep-2002	Created.                           |
3896 --|                                                                        |
3897 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
3898 --|       in select for table - gmd_spec_tests_b                           |
3899 --|                                                                        |
3900 --|    RLNAGARA 21-Jul-2006 B5396610 Modified the CURSOR c_test_date.      |
3901 --+========================================================================+
3902 -- End of comments
3903 
3904 PROCEDURE create_composite_rows
3905 (
3906   p_event_spec_disp_id  IN         NUMBER
3907 , x_return_status       OUT NOCOPY VARCHAR2
3908 )
3909 IS
3910  -- Curosrs
3911  -- RLNAGARA B5396610 Modified below cursor to select tests in order of seq .
3912   CURSOR c_test_data IS
3913   SELECT gt.test_id
3914   FROM   gmd_result_data_points_gt gt, gmd_results r
3915   WHERE  gt.result_id = r.result_id
3916   GROUP BY gt.test_id
3917   ORDER BY min(r.seq)
3918   ;
3919 
3920   CURSOR c_all_test_data (p_test_id NUMBER) IS
3921   SELECT result_id, data_num, data_char
3922   FROM   gmd_result_data_points_gt
3923   WHERE  test_id = p_test_id
3924   ;
3925 
3926   CURSOR c_spec_id (p_event_spec_disp_id NUMBER) IS
3927   SELECT spec_id
3928   FROM   gmd_event_spec_disp
3929   WHERE  event_spec_disp_id = p_event_spec_disp_id
3930   ;
3931 
3932   CURSOR c_test_type (p_test_id NUMBER) IS
3933   SELECT test_type, display_precision
3934   FROM   gmd_qc_tests_b
3935   WHERE  test_id = p_test_id
3936   ;
3937 
3938   CURSOR c_spec_test (p_spec_id NUMBER, p_test_id NUMBER) IS
3939   SELECT display_precision
3940   FROM   gmd_spec_tests_b
3941   WHERE  spec_id = p_spec_id
3942   AND    test_id = p_test_id
3943   AND    exclude_ind IS NULL
3944   ;
3945 
3946   -- Local Variables
3947   l_user_id            NUMBER;
3948   l_date               DATE;
3949 
3950   l_spec_id            NUMBER;
3951   l_mean               NUMBER;
3952   l_median_num         NUMBER;
3953   l_median_char        VARCHAR2(80);
3954   l_mode_num           NUMBER;
3955   l_mode_char          VARCHAR2(80);
3956   l_high_num           NUMBER;
3957   l_high_char          VARCHAR2(80);
3958   l_low_num            NUMBER;
3959   l_low_char           VARCHAR2(80);
3960   l_range              NUMBER;
3961   l_standard_deviation NUMBER;
3962 
3963   l_display_precision      NUMBER(2);
3964   l_t_display_precision    NUMBER(2);
3965   l_st_display_precision   NUMBER(2);
3966 
3967   l_test_type          VARCHAR2(1);
3968   l_return_status      VARCHAR2(1);
3969   sample_cnt           PLS_INTEGER;
3970 
3971   l_composite_spec_disp          GMD_COMPOSITE_SPEC_DISP%ROWTYPE;
3972   l_composite_result             GMD_COMPOSITE_RESULTS%ROWTYPE;
3973   l_composite_result_assoc       GMD_COMPOSITE_RESULT_ASSOC%ROWTYPE;
3974 
3975   l_out_composite_spec_disp      GMD_COMPOSITE_SPEC_DISP%ROWTYPE;
3976   l_out_composite_result         GMD_COMPOSITE_RESULTS%ROWTYPE;
3977 
3978   -- Exceptions
3979   e_comp_spec_disp_insert_error  EXCEPTION;
3980   e_comp_result_insert_error     EXCEPTION;
3981   e_spec_comp_rslt_insert_error  EXCEPTION;
3982   e_comp_rslt_assoc_insert_error EXCEPTION;
3983 
3984 
3985 BEGIN
3986 
3987   IF (l_debug = 'Y') THEN
3988      gmd_debug.put_line('Entered Procedure - CREATE_COMPOSITE_ROWS');
3989   END IF;
3990 
3991   --  Initialize API return status to success
3992   x_return_status := FND_API.G_RET_STS_SUCCESS;
3993 
3994   -- Get the user ID
3995   l_user_id  := FND_GLOBAL.user_id;
3996   l_date     := SYSDATE;
3997 
3998   -- Get the SPEC_ID
3999   OPEN c_spec_id(p_event_spec_disp_id);
4000   FETCH c_spec_id INTO l_spec_id;
4001   IF c_spec_id%NOTFOUND THEN
4002     CLOSE c_spec_id;
4003     GMD_API_PUB.Log_Message('GMD_SPEC_NOT_FOUND');
4004   END IF;
4005   CLOSE c_spec_id;
4006 
4007   IF (l_debug = 'Y') THEN
4008      gmd_debug.put_line('  ESD ID-' || p_event_spec_disp_id || ' Spec ID -' || l_spec_id);
4009   END IF;
4010 
4011   -- Update all previous composite for this p_event_spec_disp_id as Not-Latest
4012   UPDATE  gmd_composite_spec_disp
4013   SET     latest_ind = NULL
4014   WHERE   event_spec_disp_id = p_event_spec_disp_id
4015   AND     latest_ind = 'Y'
4016   ;
4017 
4018   IF (l_debug = 'Y') THEN
4019      gmd_debug.put_line('  Changed latest_ind for old rows in gmd_composite_spec_disp');
4020   END IF;
4021 
4022   -- Now, create a row in GMD_COMPOSITE_SPEC_DISP with latest_ind = 'Y'
4023 
4024   -- Construct the record
4025   l_composite_spec_disp.EVENT_SPEC_DISP_ID       := p_event_spec_disp_id;
4026   l_composite_spec_disp.DISPOSITION              := '3C';
4027   l_composite_spec_disp.LATEST_IND               := 'Y';
4028   l_composite_spec_disp.DELETE_MARK              := 0;
4029   l_composite_spec_disp.CREATION_DATE            := l_date;
4030   l_composite_spec_disp.CREATED_BY               := l_user_id;
4031   l_composite_spec_disp.LAST_UPDATE_DATE         := l_date;
4032   l_composite_spec_disp.LAST_UPDATED_BY          := l_user_id;
4033 
4034   -- We are ready for insert in GME_COMPOSITE_SPEC_DISP, so then lets do it.
4035   IF NOT(GMD_COMPOSITE_SPEC_DISP_PVT.Insert_Row(
4036                   p_composite_spec_disp => l_composite_spec_disp,
4037                   x_composite_spec_disp => l_out_composite_spec_disp)
4038          )
4039   THEN
4040     -- Insert Error
4041     RAISE e_comp_spec_disp_insert_error;
4042   END IF;
4043   l_composite_spec_disp.COMPOSITE_SPEC_DISP_ID := l_out_composite_spec_disp.COMPOSITE_SPEC_DISP_ID;
4044 
4045   IF (l_debug = 'Y') THEN
4046      gmd_debug.put_line('  Record inserted in GMD_COMPOSITE_SPEC_DISP, CSD ID-' || l_composite_spec_disp.composite_spec_disp_id);
4047   END IF;
4048 
4049   -- Go through all the unique tests for this event spec disp id that
4050   -- we are compositing.
4051   IF (l_debug = 'Y') THEN
4052      gmd_debug.put_line('  Start calcualting composites for each test');
4053   END IF;
4054   FOR test_rec IN c_test_data
4055   LOOP
4056     l_t_display_precision  := -1;
4057     l_st_display_precision := -1;
4058 
4059     OPEN c_test_type(test_rec.test_id);
4060     FETCH c_test_type INTO l_test_type, l_t_display_precision;
4061     CLOSE c_test_type;
4062 
4063     IF (l_test_type in ('N', 'L', 'E')) THEN
4064       OPEN c_spec_test(l_spec_id, test_rec.test_id);
4065       FETCH c_spec_test INTO l_st_display_precision;
4066       CLOSE c_spec_test;
4067     END IF;
4068 
4069     IF (l_st_display_precision = -1) THEN
4070       l_display_precision := l_t_display_precision;
4071     ELSE
4072       l_display_precision := l_st_display_precision;
4073     END IF;
4074 
4075     -- Initialize values
4076     l_mean               := NULL;
4077     l_median_num         := NULL;
4078     l_median_char        := NULL;
4079     l_mode_num           := NULL;
4080     l_mode_char          := NULL;
4081     l_high_num           := NULL;
4082     l_high_char          := NULL;
4083     l_low_num            := NULL;
4084     l_low_char           := NULL;
4085     l_range              := NULL;
4086     l_standard_deviation := NULL;
4087 
4088     IF (l_debug = 'Y') THEN
4089        gmd_debug.put_line('    Processing Test ID-' || test_rec.test_id);
4090     END IF;
4091     -- Calculate composite values
4092 
4093     -- 1. Mean
4094     qc_mean( p_test_id       => test_rec.test_id
4095            , x_mean_num      => l_mean
4096            , x_return_status => l_return_status);
4097 
4098     IF (l_return_status <> 'S') THEN
4099       RAISE FND_API.G_EXC_ERROR;
4100     END IF;
4101     IF (l_test_type in ('N', 'L', 'E')) THEN
4102       l_mean := round(l_mean, l_display_precision);
4103     END IF;
4104     IF (l_debug = 'Y') THEN
4105        gmd_debug.put_line('    l_mean-'|| l_mean);
4106     END IF;
4107 
4108     -- 2. Median
4109     qc_median( p_test_id       => test_rec.test_id
4110              , x_median_num    => l_median_num
4111              , x_median_char   => l_median_char
4112              , x_return_status => l_return_status);
4113 
4114     IF (l_return_status <> 'S') THEN
4115       RAISE FND_API.G_EXC_ERROR;
4116     END IF;
4117     IF (l_debug = 'Y') THEN
4118        gmd_debug.put_line('    l_median_num-'|| l_median_num ||' l_median_char-'|| l_median_char);
4119     END IF;
4120 
4121     -- 3. Mode
4122     qc_mode( p_test_id       => test_rec.test_id
4123            , x_mode_num      => l_mode_num
4124            , x_mode_char     => l_mode_char
4125            , x_return_status => l_return_status);
4126 
4127     IF (l_return_status <> 'S') THEN
4128       RAISE FND_API.G_EXC_ERROR;
4129     END IF;
4130     IF (l_test_type in ('N', 'L', 'E')) THEN
4131       l_mode_num := round(l_mode_num, l_display_precision);
4132     END IF;
4133     IF (l_debug = 'Y') THEN
4134        gmd_debug.put_line('    l_mode_num-'|| l_mode_num ||' l_mode_char-'|| l_mode_char);
4135     END IF;
4136 
4137     -- 4. High
4138     qc_high( p_test_id       => test_rec.test_id
4139            , x_high_num      => l_high_num
4140            , x_high_char     => l_high_char
4141            , x_return_status => l_return_status);
4142 
4143     IF (l_return_status <> 'S') THEN
4144       RAISE FND_API.G_EXC_ERROR;
4145     END IF;
4146     IF (l_debug = 'Y') THEN
4147        gmd_debug.put_line('    l_high_num-'|| l_high_num ||' l_high_char-'|| l_high_char);
4148     END IF;
4149 
4150     -- 5. Low
4151     qc_low( p_test_id        => test_rec.test_id
4152            , x_low_num       => l_low_num
4153            , x_low_char      => l_low_char
4154            , x_return_status => l_return_status);
4155 
4156     IF (l_return_status <> 'S') THEN
4157       RAISE FND_API.G_EXC_ERROR;
4158     END IF;
4159     IF (l_debug = 'Y') THEN
4160        gmd_debug.put_line('   l_low_num - '|| l_low_num ||' l_low_char-'|| l_low_char);
4161     END IF;
4162 
4163     -- 6. Standard Deviation
4164     qc_standard_deviation( p_test_id       => test_rec.test_id
4165                          , x_stddev        => l_standard_deviation
4166                          , x_return_status => l_return_status);
4167 
4168     IF (l_return_status <> 'S') THEN
4169       RAISE FND_API.G_EXC_ERROR;
4170     END IF;
4171 
4172     IF (l_test_type in ('N', 'L', 'E')) THEN
4173       l_standard_deviation := round(l_standard_deviation, l_display_precision);
4174     END IF;
4175     IF (l_debug = 'Y') THEN
4176        gmd_debug.put_line('    l_standard_deviation - '|| l_standard_deviation);
4177     END IF;
4178 
4179     -- Now, we have all the composite values for the test so create
4180     -- a new record in GMD_COMPOSITE_RESULTS
4181 
4182     -- Construct the record
4183     l_composite_result.COMPOSITE_SPEC_DISP_ID   := l_composite_spec_disp.composite_spec_disp_id;
4184     l_composite_result.TEST_ID                  := test_rec.test_id;
4185     l_composite_result.MEAN                     := l_mean;
4186     l_composite_result.MEDIAN_NUM               := l_median_num;
4187     l_composite_result.MEDIAN_CHAR              := l_median_char;
4188     l_composite_result.MODE_NUM                 := l_mode_num;
4189     l_composite_result.MODE_CHAR                := l_mode_char;
4190     l_composite_result.HIGH_NUM                 := l_high_num;
4191     l_composite_result.HIGH_CHAR                := l_high_char;
4192     l_composite_result.LOW_NUM                  := l_low_num;
4193     l_composite_result.LOW_CHAR                 := l_low_char;
4194     IF (l_test_type in ('N', 'L', 'E')) THEN
4195       l_composite_result.RANGE                    := l_high_num - l_low_num;
4196     ELSE
4197       l_composite_result.RANGE                    := NULL;
4198     END IF;
4199     l_composite_result.STANDARD_DEVIATION       := l_standard_deviation;
4200 
4201     l_composite_result.IN_SPEC_IND              :=
4202                                     rslt_is_in_spec
4203                                     (p_spec_id         => l_spec_id,
4204                                      p_test_id         => test_rec.test_id,
4205                                      p_rslt_value_num  => l_mean,
4206                                      p_rslt_value_char => l_mode_char
4207                                     );
4208 
4209     l_composite_result.DELETE_MARK              := 0;
4210     l_composite_result.CREATION_DATE            := l_date;
4211     l_composite_result.CREATED_BY               := l_user_id;
4212     l_composite_result.LAST_UPDATE_DATE         := l_date;
4213     l_composite_result.LAST_UPDATED_BY          := l_user_id;
4214     -- We are ready for insert in GMD_COMPOSITE_RESULTS, so then lets do it.
4215     IF NOT(GMD_COMPOSITE_RESULTS_PVT.Insert_Row(
4216                     p_composite_results => l_composite_result,
4217                     x_composite_results => l_out_composite_result)
4218             )
4219     THEN
4220       -- Insert Error
4221       RAISE e_comp_result_insert_error;
4222     END IF;
4223     l_composite_result.COMPOSITE_RESULT_ID := l_out_composite_result.COMPOSITE_RESULT_ID;
4224 
4225     IF (l_debug = 'Y') THEN
4226        gmd_debug.put_line('    Record created in GMD_COMPOSITE_RESULTS. CR ID - ' || l_composite_result.composite_result_id);
4227     END IF;
4228 
4229     sample_cnt := 0;
4230 
4231     -- Now record all the result_ids that made up this composite
4232     FOR result_rec IN c_all_test_data(test_rec.test_id)
4233     LOOP
4234       -- Create a new record in GMD_COMPOSITE_RESULT_ASSOC
4235 
4236       -- Construct the record
4237       l_composite_result_assoc.COMPOSITE_RESULT_ID      := l_composite_result.composite_result_id;
4238       l_composite_result_assoc.RESULT_ID                := result_rec.result_id;
4239       l_composite_result_assoc.CREATION_DATE            := l_date;
4240       l_composite_result_assoc.CREATED_BY               := l_user_id;
4241       l_composite_result_assoc.LAST_UPDATE_DATE         := l_date;
4242       l_composite_result_assoc.LAST_UPDATED_BY          := l_user_id;
4243 
4244       -- We are ready for insert in GMD_COMPOSITE_RESULT_ASSOC, so then lets do it.
4245       IF NOT(GMD_COMPOSITE_RESULT_ASSOC_PVT.Insert_Row(
4246                       p_composite_result_assoc => l_composite_result_assoc)
4247               )
4248       THEN
4249         -- Insert Error
4250         RAISE e_comp_rslt_assoc_insert_error;
4251       END IF;
4252 
4253       IF (l_test_type in ('N', 'L', 'E') AND result_rec.data_num IS NOT NULL) THEN
4254         sample_cnt := sample_cnt + 1;
4255       ELSIF (l_test_type in ('T', 'V', 'U') AND result_rec.data_char IS NOT NULL) THEN
4256         sample_cnt := sample_cnt + 1;
4257       END IF;
4258 
4259       IF (l_debug = 'Y') THEN
4260          gmd_debug.put_line('      Created record in GMD_COMPOSITE_RESULT_ASSOC for associated Result ID -' || result_rec.result_id);
4261       END IF;
4262 
4263     END LOOP;  -- All atomic results for the test
4264 
4265     -- Now update the Sample Total And Sample Count Used fields
4266     UPDATE gmd_composite_results
4267     SET    sample_total = sample_cnt,
4268 	   sample_cnt_used = sample_cnt
4269     WHERE  composite_result_id = l_composite_result.composite_result_id
4270     AND    test_id = test_rec.test_id
4271     ;
4272 
4273   END LOOP;  -- All the tests which are composited across multiple samples
4274 
4275   IF (l_debug = 'Y') THEN
4276      gmd_debug.put_line('Leaving Procedure - CREATE_COMPOSITE_ROWS');
4277   END IF;
4278 
4279   RETURN;
4280 
4281 EXCEPTION
4282   WHEN FND_API.G_EXC_ERROR OR
4283        e_comp_spec_disp_insert_error OR
4284        e_comp_result_insert_error OR
4285        e_comp_rslt_assoc_insert_error
4286        THEN
4287     x_return_status := FND_API.G_RET_STS_ERROR ;
4288   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4289     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4290   WHEN OTHERS THEN
4291     GMD_API_PUB.Log_Message('GMD_API_ERROR',
4292                             'PACKAGE','CREATE_COMPOSITE_ROWS',
4293                             'ERROR', SUBSTR(SQLERRM,1,100));
4294     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4295 
4296 END create_composite_rows;
4297 
4298 
4299 
4300 --Start of comments
4301 --+========================================================================+
4302 --| API Name    : qc_mean                                                  |
4303 --| TYPE        : Group                                                    |
4304 --| Notes       : WRITE SOMETHING HERE                                     |
4305 --|                                                                        |
4306 --|                                                                        |
4307 --|                                                                        |
4308 --| HISTORY                                                                |
4309 --|    Chetan Nagar	04-Sep-2002	Created.                           |
4310 --|                                                                        |
4311 --+========================================================================+
4312 -- End of comments
4313 
4314 PROCEDURE qc_mean
4315 (
4316   p_test_id       IN         NUMBER
4317 , x_mean_num      OUT NOCOPY NUMBER
4318 , x_return_status OUT NOCOPY VARCHAR2
4319 )
4320 IS
4321 
4322   -- Curosrs
4323   CURSOR c_test_type(p_test_id NUMBER) IS
4324   SELECT test_type
4325   FROM   gmd_qc_tests_b
4326   WHERE  test_id = p_test_id
4327   ;
4328 
4329   CURSOR c_mean(p_test_id NUMBER) IS
4330   SELECT avg(data_num)
4331   FROM   gmd_result_data_points_gt
4332   WHERE  test_id = p_test_id
4333   AND    exclude_ind = 0
4334   AND    data_num IS NOT NULL
4335   ;
4336 
4337   -- Local Variables
4338   l_test_type        VARCHAR2(1);
4339 
4340 BEGIN
4341 
4342   IF (l_debug = 'Y') THEN
4343      gmd_debug.put_line('Entering Procedure - QC_MEAN');
4344   END IF;
4345 
4346   --  Initialize API return status to success
4347   x_return_status := FND_API.G_RET_STS_SUCCESS;
4348 
4349   -- Get the Test Type
4350   OPEN c_test_type(p_test_id);
4351   FETCH c_test_type INTO l_test_type;
4352   CLOSE c_test_type;
4353 
4354   IF (l_test_type in ('N', 'E', 'L')) THEN
4355     OPEN c_mean(p_test_id);
4356     FETCH c_mean INTO x_mean_num;
4357     CLOSE c_mean;
4358   END IF;
4359 
4360   IF (l_debug = 'Y') THEN
4361      gmd_debug.put_line('Leaving Procedure - QC_MEAN');
4362   END IF;
4363   RETURN;
4364 
4365 EXCEPTION
4366   WHEN FND_API.G_EXC_ERROR THEN
4367     x_return_status := FND_API.G_RET_STS_ERROR ;
4368   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4369     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4370   WHEN OTHERS THEN
4371     GMD_API_PUB.Log_Message('GMD_API_ERROR',
4372                             'PACKAGE','QC_MEAN',
4373                             'ERROR', SUBSTR(SQLERRM,1,100));
4374     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4375 
4376 END qc_mean;
4377 
4378 
4379 
4380 
4381 --Start of comments
4382 --+========================================================================+
4383 --| API Name    : qc_median                                                |
4384 --| TYPE        : Group                                                    |
4385 --| Notes       : This function returns median for the data points in      |
4386 --|               gmd_result_data_points_gt table for a give test_id.      |
4387 --|                                                                        |
4388 --|               If the data points are as below:                         |
4389 --|               5, 11, 14, 16, 20                                        |
4390 --|                                                                        |
4391 --|               Then the median is = 14 (Middle no. after sorting)       |
4392 --|                                                                        |
4393 --|               And if the data points are as below:                     |
4394 --|               5, 11, 14, 16, 20, 30                                    |
4395 --|                                                                        |
4396 --|               Then the median is = 15 (14+16)/2)                       |
4397 --|                                                                        |
4398 --|               THIS PROCEDURE CAN ALSO BE CALLED FOR CHARACTER TEST     |
4399 --|               TYPES.                                                   |
4400 --|                                                                        |
4401 --|                                                                        |
4402 --| HISTORY                                                                |
4403 --|    Chetan Nagar	04-Sep-2002	Created.                           |
4404 --|                                                                        |
4405 --+========================================================================+
4406 -- End of comments
4407 
4408 PROCEDURE qc_median
4409 (
4410   p_test_id       IN         NUMBER
4411 , x_median_num    OUT NOCOPY NUMBER
4412 , x_median_char   OUT NOCOPY VARCHAR2
4413 , x_return_status OUT NOCOPY VARCHAR2
4414 )
4415 IS
4416 
4417   -- Curosrs
4418   CURSOR c_test_type(p_test_id NUMBER) IS
4419   SELECT test_type
4420   FROM   gmd_qc_tests_b
4421   WHERE  test_id = p_test_id
4422   ;
4423 
4424   CURSOR c_median(p_test_id NUMBER) IS
4425   SELECT avg(data_num)
4426   FROM
4427   (
4428     SELECT max(data_num) data_num
4429     FROM
4430     (
4431       SELECT data_num
4432       FROM
4433       (
4434         SELECT data_num
4435         FROM   gmd_result_data_points_gt
4436         WHERE  test_id = p_test_id
4437         AND    exclude_ind = 0
4438 	AND    data_num IS NOT NULL
4439         ORDER BY data_num
4440       )
4441       WHERE rownum <= (SELECT ceil(count(*)/2)
4442                        FROM   gmd_result_data_points_gt
4443                        WHERE  test_id = p_test_id
4444                        AND    exclude_ind = 0
4445 		       AND    data_num IS NOT NULL)
4446     )
4447     UNION
4448     SELECT min(data_num) data_num
4449     FROM
4450     (
4451       SELECT data_num
4452       FROM
4453       (
4454         SELECT data_num
4455         FROM   gmd_result_data_points_gt
4456         WHERE  test_id = p_test_id
4457         AND    exclude_ind = 0
4458 	AND    data_num IS NOT NULL
4459         ORDER BY data_num desc
4460       )
4461       WHERE rownum <= (SELECT ceil(count(*)/2)
4462                        FROM gmd_result_data_points_gt
4463                        WHERE  test_id = p_test_id
4464                        AND    exclude_ind = 0
4465 		       AND    data_num IS NOT NULL)
4466     )
4467   )
4468   ;
4469 
4470   CURSOR c_num_to_text (p_test_id NUMBER, p_num NUMBER) IS
4471   SELECT value_char
4472   FROM   gmd_qc_test_values_b
4473   WHERE  test_id        = p_test_id
4474   AND    text_range_seq = p_num
4475   ;
4476 
4477   -- Local Variables
4478   l_test_type        VARCHAR2(1);
4479   l_count            NUMBER(15);
4480 
4481   -- Exceptions
4482   e_even_number_data_set      EXCEPTION;
4483 
4484 BEGIN
4485 
4486   IF (l_debug = 'Y') THEN
4487      gmd_debug.put_line('Entering Procedure - QC_MEDIAN');
4488   END IF;
4489 
4490   --  Initialize API return status to success
4491   x_return_status := FND_API.G_RET_STS_SUCCESS;
4492 
4493   -- Get the Test Type
4494   OPEN c_test_type(p_test_id);
4495   FETCH c_test_type INTO l_test_type;
4496   CLOSE c_test_type;
4497 
4498   -- Get the count of data points.
4499   SELECT nvl(count(*), 0)
4500   INTO   l_count
4501   FROM   gmd_result_data_points_gt
4502   WHERE  test_id = p_test_id
4503   AND    exclude_ind = 0
4504   AND    data_num IS NOT NULL
4505   ;
4506 
4507   -- For character data type we have to have odd number of data points
4508   -- to determine Median.
4509   IF (l_test_type = 'T' AND MOD(l_count, 2) = 0 )THEN
4510     -- Even number of data points, can't find Median, Chief!
4511     IF (l_debug = 'Y') THEN
4512        gmd_debug.put_line('Even data points. Go back.');
4513     END IF;
4514     RAISE e_even_number_data_set;
4515   END IF;
4516 
4517   IF (l_test_type in ('N', 'L', 'E', 'T')) THEN
4518     OPEN c_median(p_test_id);
4519     FETCH c_median INTO x_median_num;
4520     CLOSE c_median;
4521     IF (l_debug = 'Y') THEN
4522        gmd_debug.put_line('Got the median: '|| x_median_num);
4523     END IF;
4524   END IF;
4525 
4526   IF (l_test_type = 'T') THEN
4527     -- Convert Seq for text range back to Character
4528     OPEN c_num_to_text(p_test_id, x_median_num);
4529     FETCH c_num_to_text INTO x_median_char;
4530     CLOSE c_num_to_text;
4531     IF (l_debug = 'Y') THEN
4532        gmd_debug.put_line('converted the num to char: '|| x_median_char);
4533     END IF;
4534   END IF;
4535 
4536   IF (l_debug = 'Y') THEN
4537      gmd_debug.put_line('Leaving Procedure - QC_MEDIAN');
4538   END IF;
4539 
4540   RETURN;
4541 
4542 EXCEPTION
4543   WHEN e_even_number_data_set THEN
4544     x_median_num := NULL;
4545     x_median_char := NULL;
4546     RETURN;
4547   WHEN FND_API.G_EXC_ERROR THEN
4548     x_return_status := FND_API.G_RET_STS_ERROR ;
4549   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4550     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4551   WHEN OTHERS THEN
4552     GMD_API_PUB.Log_Message('GMD_API_ERROR',
4553                             'PACKAGE','qc_median',
4554                             'ERROR', SUBSTR(SQLERRM,1,100));
4555     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4556 
4557 END qc_median;
4558 
4559 
4560 
4561 
4562 --Start of comments
4563 --+========================================================================+
4564 --| API Name    : qc_mode                                                  |
4565 --| TYPE        : Group                                                    |
4566 --| Notes       : This function returns mode for the data points in        |
4567 --|               gmd_result_data_points_gt table for a give test_id.      |
4568 --|                                                                        |
4569 --| HISTORY                                                                |
4570 --|    Chetan Nagar	04-Sep-2002	Created.                           |
4571 --|                                                                        |
4572 --+========================================================================+
4573 -- End of comments
4574 
4575 PROCEDURE qc_mode
4576 (
4577   p_test_id       IN         NUMBER
4578 , x_mode_num      OUT NOCOPY NUMBER
4579 , x_mode_char     OUT NOCOPY VARCHAR2
4580 , x_return_status OUT NOCOPY VARCHAR2
4581 )
4582 IS
4583 
4584   -- Curosrs
4585   CURSOR c_mode_num(p_test_id NUMBER) IS
4586   SELECT data_num
4587   FROM   gmd_result_data_points_gt
4588   WHERE  test_id = p_test_id
4589   AND    exclude_ind = 0
4590   AND    data_num IS NOT NULL
4591   GROUP BY data_num
4592   HAVING count(*) = (SELECT max(count(*))
4593                      FROM   gmd_result_data_points_gt
4594                      WHERE  test_id = p_test_id
4595                      AND    exclude_ind = 0
4596 		     AND    data_num IS NOT NULL
4597                      GROUP BY data_num)
4598   ;
4599 
4600   CURSOR c_mode_char(p_test_id NUMBER) IS
4601   SELECT data_char
4602   FROM   gmd_result_data_points_gt
4603   WHERE  test_id = p_test_id
4604   AND    exclude_ind = 0
4605   AND    data_char IS NOT NULL
4606   GROUP BY data_char
4607   HAVING count(*) = (SELECT max(count(*))
4608                      FROM   gmd_result_data_points_gt
4609                      WHERE  test_id = p_test_id
4610                      AND    exclude_ind = 0
4611 		     AND    data_char IS NOT NULL
4612                      GROUP BY data_char)
4613   ;
4614 
4615 
4616   CURSOR c_test (p_test_id NUMBER) IS
4617   SELECT test_type
4618   FROM   gmd_qc_tests_b
4619   WHERE  test_id = p_test_id
4620   ;
4621 
4622   CURSOR c_num_to_text (p_test_id NUMBER, p_num NUMBER) IS
4623   SELECT value_char
4624   FROM   gmd_qc_test_values_b
4625   WHERE  test_id        = p_test_id
4626   AND    text_range_seq = p_num
4627   ;
4628 
4629   -- Local Variables
4630   l_test_type          VARCHAR2(1);
4631   dummy_num            NUMBER;
4632   dummy_char           VARCHAR2(80);
4633 
4634   -- Exceptions
4635   e_multi_modal_data_set     EXCEPTION;
4636 
4637 BEGIN
4638 
4639   IF (l_debug = 'Y') THEN
4640      gmd_debug.put_line('Entering Procedure - QC_MODE');
4641   END IF;
4642 
4643   --  Initialize API return status to success
4644   x_return_status := FND_API.G_RET_STS_SUCCESS;
4645 
4646   OPEN c_test (p_test_id);
4647   FETCH c_test INTO l_test_type;
4648   CLOSE c_test;
4649 
4650   IF (l_test_type IN ('N', 'L', 'E', 'T')) THEN
4651 
4652     OPEN c_mode_num(p_test_id);
4653     FETCH c_mode_num INTO x_mode_num;
4654 
4655     -- See if we can fetch another Mode, and If we can then the data set
4656     -- is Multi-Modal.
4657     FETCH c_mode_num INTO dummy_num;
4658     IF c_mode_num%FOUND THEN
4659       RAISE e_multi_modal_data_set;
4660     END IF;
4661     CLOSE c_mode_num;
4662 
4663   ELSIF (l_test_type = 'V') THEN
4664 
4665     OPEN c_mode_char(p_test_id);
4666     FETCH c_mode_char INTO x_mode_char;
4667 
4668     -- See if we can fetch another Mode, and If we can then the data set
4669     -- is Multi-Modal.
4670     FETCH c_mode_char INTO dummy_char;
4671     IF c_mode_char%FOUND THEN
4672       RAISE e_multi_modal_data_set;
4673     END IF;
4674 
4675     CLOSE c_mode_char;
4676   END IF;
4677 
4678   IF (l_test_type = 'T') THEN
4679     -- Convert Seq for text range back to Character
4680     OPEN c_num_to_text(p_test_id, x_mode_num);
4681     FETCH c_num_to_text INTO x_mode_char;
4682     CLOSE c_num_to_text;
4683 
4684     IF (l_debug = 'Y') THEN
4685        gmd_debug.put_line('converted the num to char: '|| x_mode_char);
4686     END IF;
4687   END IF;
4688 
4689   IF (l_debug = 'Y') THEN
4690      gmd_debug.put_line('Leaving Procedure - QC_MODE');
4691   END IF;
4692 
4693   RETURN;
4694 
4695 EXCEPTION
4696   WHEN e_multi_modal_data_set THEN
4697     -- We can log a message but since no one is going to use
4698     -- so just return
4699     x_mode_num := NULL;
4700     x_mode_char := NULL;
4701     RETURN;
4702   WHEN    FND_API.G_EXC_ERROR THEN
4703     x_return_status := FND_API.G_RET_STS_ERROR ;
4704   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4705     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4706   WHEN OTHERS THEN
4707     GMD_API_PUB.Log_Message('GMD_API_ERROR',
4708                             'PACKAGE','QC_MODE',
4709                             'ERROR', SUBSTR(SQLERRM,1,100));
4710     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4711 
4712 END qc_mode;
4713 
4714 
4715 
4716 --Start of comments
4717 --+========================================================================+
4718 --| API Name    : qc_high                                                  |
4719 --| TYPE        : Group                                                    |
4720 --| Notes       : WRITE SOMETHING HERE....                                 |
4721 --|                                                                        |
4722 --|                                                                        |
4723 --|                                                                        |
4724 --|                                                                        |
4725 --|                                                                        |
4726 --|                                                                        |
4727 --| HISTORY                                                                |
4728 --|    Chetan Nagar	04-Sep-2002	Created.                           |
4729 --|                                                                        |
4730 --+========================================================================+
4731 -- End of comments
4732 
4733 PROCEDURE qc_high
4734 (
4735   p_test_id       IN         NUMBER
4736 , x_high_num      OUT NOCOPY NUMBER
4737 , x_high_char     OUT NOCOPY VARCHAR2
4738 , x_return_status OUT NOCOPY VARCHAR2
4739 )
4740 IS
4741 
4742   -- Curosrs
4743   CURSOR c_high_num(p_test_id NUMBER) IS
4744   SELECT max(data_num)
4745   FROM   gmd_result_data_points_gt
4746   WHERE  test_id = p_test_id
4747   AND    exclude_ind = 0
4748   AND    data_num IS NOT NULL
4749   ;
4750 
4751   CURSOR c_test (p_test_id NUMBER) IS
4752   SELECT test_type
4753   FROM   gmd_qc_tests_b
4754   WHERE  test_id = p_test_id
4755   ;
4756 
4757   -- Local Variables
4758   l_test_type          VARCHAR2(1);
4759 
4760 BEGIN
4761 
4762   IF (l_debug = 'Y') THEN
4763      gmd_debug.put_line('Entering Procedure - QC_HIGH');
4764   END IF;
4765 
4766   --  Initialize API return status to success
4767   x_return_status := FND_API.G_RET_STS_SUCCESS;
4768 
4769   OPEN c_test (p_test_id);
4770   FETCH c_test INTO l_test_type;
4771   CLOSE c_test;
4772 
4773   IF (l_test_type in ('N', 'L', 'E', 'T')) THEN
4774     OPEN c_high_num(p_test_id);
4775     FETCH c_high_num INTO x_high_num;
4776     CLOSE c_high_num;
4777   END IF;
4778 
4779   IF (l_test_type = 'T' AND x_high_num IS NOT NULL) THEN
4780     -- Convert Seq for text range back to Character
4781     SELECT value_char
4782     INTO   x_high_char
4783     FROM   gmd_qc_test_values_b
4784     WHERE  test_id        = p_test_id
4785     AND    text_range_seq = x_high_num
4786     ;
4787   END IF;
4788 
4789   IF (l_debug = 'Y') THEN
4790      gmd_debug.put_line('Leaving Procedure - QC_HIGH');
4791   END IF;
4792 
4793   RETURN;
4794 
4795 EXCEPTION
4796   WHEN    FND_API.G_EXC_ERROR THEN
4797     x_return_status := FND_API.G_RET_STS_ERROR ;
4798   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4799     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4800   WHEN OTHERS THEN
4801     GMD_API_PUB.Log_Message('GMD_API_ERROR',
4802                             'PACKAGE','QC_HIGH',
4803                             'ERROR', SUBSTR(SQLERRM,1,100));
4804     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4805 
4806 END qc_high;
4807 
4808 
4809 
4810 
4811 --Start of comments
4812 --+========================================================================+
4813 --| API Name    : qc_low                                                   |
4814 --| TYPE        : Group                                                    |
4815 --| Notes       : WRITE SOMETHING HERE....                                 |
4816 --|                                                                        |
4817 --|                                                                        |
4818 --|                                                                        |
4819 --|                                                                        |
4820 --|                                                                        |
4821 --|                                                                        |
4822 --| HISTORY                                                                |
4823 --|    Chetan Nagar	04-Sep-2002	Created.                           |
4824 --|                                                                        |
4825 --+========================================================================+
4826 -- End of comments
4827 
4828 PROCEDURE qc_low
4829 (
4830   p_test_id       IN         NUMBER
4831 , x_low_num       OUT NOCOPY NUMBER
4832 , x_low_char      OUT NOCOPY VARCHAR2
4833 , x_return_status OUT NOCOPY VARCHAR2
4834 )
4835 IS
4836 
4837   -- Curosrs
4838   CURSOR c_low_num(p_test_id NUMBER) IS
4839   SELECT min(data_num)
4840   FROM   gmd_result_data_points_gt
4841   WHERE  test_id = p_test_id
4842   AND    exclude_ind = 0
4843   AND    data_num IS NOT NULL
4844   ;
4845 
4846   CURSOR c_test (p_test_id NUMBER) IS
4847   SELECT test_type
4848   FROM   gmd_qc_tests_b
4849   WHERE  test_id = p_test_id
4850   ;
4851 
4852   -- Local Variables
4853   l_test_type          VARCHAR2(1);
4854 
4855 BEGIN
4856 
4857   IF (l_debug = 'Y') THEN
4858      gmd_debug.put_line('Entering Procedure - QC_LOW');
4859   END IF;
4860 
4861   --  Initialize API return status to success
4862   x_return_status := FND_API.G_RET_STS_SUCCESS;
4863 
4864   OPEN c_test (p_test_id);
4865   FETCH c_test INTO l_test_type;
4866   CLOSE c_test;
4867 
4868   IF (l_test_type in ('N', 'L', 'E', 'T')) THEN
4869     OPEN c_low_num(p_test_id);
4870     FETCH c_low_num INTO x_low_num;
4871     CLOSE c_low_num;
4872   END IF;
4873 
4874   IF (l_test_type = 'T' AND x_low_num IS NOT NULL) THEN
4875     -- Convert Seq for text range back to Character
4876     SELECT value_char
4877     INTO   x_low_char
4878     FROM   gmd_qc_test_values_b
4879     WHERE  test_id        = p_test_id
4880     AND    text_range_seq = x_low_num
4881     ;
4882   END IF;
4883 
4884   IF (l_debug = 'Y') THEN
4885      gmd_debug.put_line('Leaving Procedure - QC_LOW');
4886   END IF;
4887 
4888   RETURN;
4889 
4890 EXCEPTION
4891   WHEN    FND_API.G_EXC_ERROR THEN
4892     x_return_status := FND_API.G_RET_STS_ERROR ;
4893   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4894     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4895   WHEN OTHERS THEN
4896     GMD_API_PUB.Log_Message('GMD_API_ERROR',
4897                             'PACKAGE','QC_LOW',
4898                             'ERROR', SUBSTR(SQLERRM,1,100));
4899     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4900 
4901 END qc_low;
4902 
4903 
4904 
4905 --Start of comments
4906 --+========================================================================+
4907 --| API Name    : qc_standard_deviation                                    |
4908 --| TYPE        : Group                                                    |
4909 --| Notes       : WRITE SOMETHING HERE                                     |
4910 --|                                                                        |
4911 --|                                                                        |
4912 --|                                                                        |
4913 --| HISTORY                                                                |
4914 --|    Chetan Nagar	04-Sep-2002	Created.                           |
4915 --|                                                                        |
4916 --+========================================================================+
4917 -- End of comments
4918 
4919 PROCEDURE qc_standard_deviation
4920 (
4921   p_test_id       IN         NUMBER
4922 , x_stddev        OUT NOCOPY NUMBER
4923 , x_return_status OUT NOCOPY VARCHAR2
4924 )
4925 IS
4926 
4927   -- Curosrs
4928   CURSOR c_test_type(p_test_id NUMBER) IS
4929   SELECT test_type
4930   FROM   gmd_qc_tests_b
4931   WHERE  test_id = p_test_id
4932   ;
4933 
4934   CURSOR c_stddev(p_test_id NUMBER) IS
4935   SELECT stddev(data_num)
4936   FROM   gmd_result_data_points_gt
4937   WHERE  test_id = p_test_id
4938   AND    exclude_ind = 0
4939   AND    data_num IS NOT NULL
4940   ;
4941 
4942   -- Local Variables
4943   l_test_type        VARCHAR2(1);
4944 
4945 BEGIN
4946 
4947   IF (l_debug = 'Y') THEN
4948      gmd_debug.put_line('Entering Procedure - QC_STANDARD_DEVIATION');
4949   END IF;
4950 
4951   --  Initialize API return status to success
4952   x_return_status := FND_API.G_RET_STS_SUCCESS;
4953 
4954   -- Get the Test Type
4955   OPEN c_test_type(p_test_id);
4956   FETCH c_test_type INTO l_test_type;
4957   CLOSE c_test_type;
4958 
4959   IF (l_test_type in ('N', 'E', 'L')) THEN
4960     OPEN c_stddev(p_test_id);
4961     FETCH c_stddev INTO x_stddev;
4962     CLOSE c_stddev;
4963   END IF;
4964 
4965   IF (l_debug = 'Y') THEN
4966      gmd_debug.put_line('Leaving Procedure - QC_STANDARD_DEVIATION');
4967   END IF;
4968 
4969   RETURN;
4970 
4971 EXCEPTION
4972   WHEN FND_API.G_EXC_ERROR THEN
4973     x_return_status := FND_API.G_RET_STS_ERROR ;
4974   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4975     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4976   WHEN OTHERS THEN
4977     GMD_API_PUB.Log_Message('GMD_API_ERROR',
4978                             'PACKAGE','qc_standard_deviation',
4979                             'ERROR', SUBSTR(SQLERRM,1,100));
4980     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4981 
4982 END qc_standard_deviation;
4983 
4984 --+========================================================================+
4985 --| API Name    : get_composite_rslt                              |
4986 --| TYPE        : Group                                                    |
4987 --| Notes       : This procedure receives as input composite spec disp id  and       |
4988 --|               retrieves composite results               |
4989 --|                                                                        |
4990 --|               If everything is fine then OUT parameter                 |
4991 --|               x_return_status is set to 'S' else appropriate           |
4992 --|               error message is put on the stack and error              |
4993 --|               is returned.                                             |
4994 --|                                                                        |
4995 --| HISTORY                                                                |
4996 --|   Ger Kelly  17 Sep 2002	Created.                           |
4997 --|   GK 	 17 Oct 2002    B 2621648 Changed the IF, ELSIF to IF END IF for the text chars
4998 --|                                                                        |
4999 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
5000 --|       in select for table - gmd_spec_tests_b                           |
5001 --+========================================================================+
5002 PROCEDURE get_composite_rslt
5003 (
5004   p_composite_spec_disp_id  IN  NUMBER,
5005   p_source_spec_id		  IN NUMBER,
5006   p_target_spec_id		  IN NUMBER,
5007   x_comresults_tab        OUT NOCOPY GMD_RESULTS_GRP.gmd_comres_tab,
5008   x_return_status         OUT NOCOPY VARCHAR2) IS
5009 
5010   -- Local Variables
5011 
5012   l_spec_id             NUMBER(15);
5013   i				        NUMBER :=0;
5014   j			            NUMBER :=0;
5015   k			            NUMBER :=0;
5016 
5017   l_spec_ind			VARCHAR2(1);
5018   l_test_id		        NUMBER;
5019   l_result_id           NUMBER(15);
5020   l_mode_num            NUMBER;
5021   l_mode_char           VARCHAR2(80);
5022   l_median_num          NUMBER;
5023   l_median_char         VARCHAR2(80);
5024 
5025   l_comres_tab          GMD_RESULTS_GRP.gmd_comres_tab;
5026   return_status			VARCHAR2(1);
5027   x_test_ids			GMD_API_PUB.number_tab;
5028 
5029 
5030   -- Cursors
5031 
5032 CURSOR c_get_composite_results IS
5033   SELECT cr.composite_spec_disp_id,
5034          cr.composite_result_id,
5035          cr.in_spec_ind,
5036          cr.test_id,
5037          cr.median_num,
5038          cr.median_char,
5039          cr.mode_num,
5040          cr.mode_char
5041    FROM  gmd_composite_results cr
5042    WHERE cr.composite_spec_disp_id = p_composite_spec_disp_id;
5043    LocalComResRec   c_get_composite_results%ROWTYPE;
5044 
5045  CURSOR c_get_type IS
5046       SELECT  t.test_type, t.test_code, t.test_method_id, m.test_method_code
5047       FROM    gmd_qc_tests_b t , gmd_test_methods_b m
5048       WHERE   t.test_id = l_test_id
5049       AND     t.test_method_id = m.test_method_id;
5050   LocalTypeRec	c_get_type%ROWTYPE;
5051 
5052   CURSOR c_get_spec_test_num IS
5053       SELECT  s.min_value_num, s.max_value_num, s.target_value_num
5054       FROM    gmd_spec_tests_b s
5055       WHERE   s.spec_id = l_spec_id
5056       AND     s.test_id = l_test_id
5057       AND     s.exclude_ind IS NULL;
5058   LocalNumRec   c_get_spec_test_num%ROWTYPE;
5059 
5060    CURSOR c_get_spec_test_char IS
5061       SELECT  s.min_value_char, s.max_value_char, s.target_value_char
5062       FROM    gmd_spec_tests_b s
5063       WHERE   s.spec_id = l_spec_id
5064       AND     s.test_id = l_test_id
5065       AND     s.exclude_ind IS NULL;
5066   LocalCharRec   c_get_spec_test_char%ROWTYPE;
5067 
5068 
5069   BEGIN
5070   --  Initialize API return status to success
5071   x_return_status := FND_API.G_RET_STS_SUCCESS;
5072 
5073   --Since we have a specification
5074    --retrieve rows in GMD_COM_RESULTS for all the tests
5075     l_comres_tab.DELETE;
5076     i := 0;
5077 
5078     -- Get the results for each sample and spec
5079 
5080       FOR LocalComResRec IN c_get_composite_results LOOP
5081       i := i + 1;
5082 
5083       l_test_id := LocalComResRec.test_id;
5084       l_comres_tab(i).test_id := l_test_id;
5085       l_comres_tab(i).in_spec := LocalComResRec.in_spec_ind;
5086       l_mode_num :=  LocalComResRec.mode_num;
5087       l_mode_char :=  LocalComResRec.mode_char;
5088       l_median_num := LocalComResRec.median_num;
5089       l_median_char := LocalComResRec.median_char;
5090 
5091       -- For each test type get the test and method info
5092       OPEN c_get_type;
5093       FETCH c_get_type INTO LocalTypeRec;
5094        l_comres_tab(i).test_code := LocalTypeRec.test_code;
5095       CLOSE c_get_type;
5096 
5097       IF LocalTypeRec.test_type IN ('T', 'L', 'V')  THEN
5098         IF l_mode_num IS NOT NULL THEN
5099          l_comres_tab(i).result_num := l_mode_num;
5100         ELSE
5101       	 l_comres_tab(i).result_char := l_mode_char;
5102         END IF;
5103       ELSIF LocalTypeRec.test_type IN ('N', 'U', 'E') THEN
5104         IF l_median_num IS NOT NULL THEN
5105        	 l_comres_tab(i).result_num := l_median_num;
5106         ELSE
5107       	 l_comres_tab(i).result_char := l_median_char;
5108         END IF;
5109       END IF;
5110       IF LocalTypeRec.test_type IN ('N', 'L', 'V', 'T', 'E', 'U')  THEN
5111          -- Get the values for the Current Spec
5112          l_spec_id := p_source_spec_id;
5113 
5114          OPEN c_get_spec_test_num;
5115          FETCH c_get_spec_test_num INTO LocalNumRec;
5116            IF c_get_spec_test_num %FOUND THEN
5117       	    l_comres_tab(i).target_num := LocalNumRec.target_value_num;
5118       	    l_comres_tab(i).min_num := LocalNumRec.min_value_num;
5119       	    l_comres_tab(i).max_num := LocalNumRec.max_value_num;
5120       	   END IF;
5121          CLOSE c_get_spec_test_num;
5122 
5123        --Get the values for the Comparison Spec
5124           l_spec_id := p_target_spec_id;
5125 
5126           OPEN c_get_spec_test_num;
5127           FETCH c_get_spec_test_num INTO LocalNumRec;
5128           IF c_get_spec_test_num %FOUND THEN
5129              l_comres_tab(i).spec_target_num := LocalNumRec.target_value_num;
5130       	     l_comres_tab(i).spec_min_num := LocalNumRec.min_value_num;
5131       	     l_comres_tab(i).spec_max_num := LocalNumRec.max_value_num;
5132           END IF;
5133           CLOSE c_get_spec_test_num;
5134 
5135       END IF;
5136       IF LocalTypeRec.test_type IN ('V', 'T', 'L', 'E') THEN
5137        -- Get the values for the Current Specfor chars
5138         l_spec_id := p_source_spec_id;
5139 
5140         OPEN c_get_spec_test_char;
5141         FETCH c_get_spec_test_char INTO LocalCharRec;
5142         IF c_get_spec_test_char%FOUND THEN
5143            l_comres_tab(i).target_char := LocalCharRec.target_value_char;
5144            l_comres_tab(i).min_char := LocalCharRec.min_value_char;
5145            l_comres_tab(i).max_char := LocalCharRec.max_value_char;
5146         END IF;
5147         CLOSE c_get_spec_test_char;
5148 
5149       --Get the values for the Comparison Spec
5150           l_spec_id := p_target_spec_id;
5151 
5152         OPEN c_get_spec_test_char;
5153         FETCH c_get_spec_test_char INTO LocalCharRec;
5154         IF c_get_spec_test_char%FOUND THEN
5155       	  l_comres_tab(i).spec_target_char := LocalCharRec.target_value_char;
5156 	      l_comres_tab(i).spec_min_char := LocalCharRec.min_value_char;
5157           l_comres_tab(i).spec_max_char := LocalCharRec.max_value_char;
5158       	END IF;
5159  	    CLOSE c_get_spec_test_char;
5160 
5161       END IF;
5162     l_comres_tab(i).in_spec := GMD_RESULTS_GRP.rslt_is_in_spec
5163 	( p_source_spec_id, l_test_id, l_comres_tab(i).result_num, l_comres_tab(i).result_char);
5164 IF (l_debug = 'Y') THEN
5165    gmd_debug.put_line('in speca '||l_comres_tab(i).in_spec);
5166 END IF;
5167     l_comres_tab(i).spec_in_spec := GMD_RESULTS_GRP.rslt_is_in_spec
5168 	( p_target_spec_id, l_test_id, l_comres_tab(i).result_num, l_comres_tab(i).result_char);
5169 IF (l_debug = 'Y') THEN
5170    gmd_debug.put_line('in spec '||l_comres_tab(i).spec_in_spec);
5171 END IF;
5172     x_comresults_tab(i) := l_comres_tab(i);
5173 
5174 
5175     END LOOP;  -- Composite Results test Loop
5176     j := i;
5177     l_spec_id := p_target_spec_id;
5178 
5179     GMD_RESULTS_GRP.compare_cmpst_rslt_and_spec
5180         (p_composite_spec_disp_id, l_spec_id, x_test_ids, return_status);
5181 
5182          FOR k in 1..x_test_ids.COUNT LOOP
5183 	    i := i + k;
5184             l_test_id := x_test_ids(k);
5185 
5186           OPEN c_get_type;
5187           FETCH c_get_type INTO LocalTypeRec;
5188 	  CLOSE c_get_type;
5189       	  l_comres_tab(i).test_code := LocalTypeRec.test_code;
5190 
5191         IF LocalTypeRec.test_type IN ('N', 'L', 'V', 'T', 'E', 'U')  THEN
5192       	  -- Get the values for the Comparison Spec
5193 
5194            OPEN c_get_spec_test_num;
5195            FETCH c_get_spec_test_num INTO LocalNumRec;
5196            IF c_get_spec_test_num %FOUND THEN
5197              l_comres_tab(i).spec_target_num := LocalNumRec.target_value_num;
5198       	     l_comres_tab(i).spec_min_num := LocalNumRec.min_value_num;
5199       	     l_comres_tab(i).spec_max_num := LocalNumRec.max_value_num;
5200            END IF;
5201            CLOSE c_get_spec_test_num;
5202         END IF;
5203 
5204         IF LocalTypeRec.test_type IN ('V', 'T', 'L', 'E') THEN
5205         -- Get the values for the Comparison Test for chars
5206 
5207         OPEN c_get_spec_test_char;
5208         FETCH c_get_spec_test_char INTO LocalCharRec;
5209         IF c_get_spec_test_char%FOUND THEN
5210       	    l_comres_tab(i).spec_target_char := LocalCharRec.target_value_char;
5211 	    l_comres_tab(i).spec_min_char := LocalCharRec.min_value_char;
5212             l_comres_tab(i).spec_max_char := LocalCharRec.max_value_char;
5213       	END IF;
5214  	CLOSE c_get_spec_test_char;
5215  	END IF;
5216 
5217  	l_comres_tab(i).spec_in_spec := GMD_RESULTS_GRP.rslt_is_in_spec
5218 		( p_target_spec_id, l_test_id, l_comres_tab(i).result_num, l_comres_tab(i).result_char);
5219 IF (l_debug = 'Y') THEN
5220    gmd_debug.put_line('in spec1 '||l_comres_tab(i).spec_in_spec);
5221 END IF;
5222 	 x_comresults_tab(i) := l_comres_tab(i);
5223 
5224     END LOOP;
5225 
5226 
5227 EXCEPTION
5228   WHEN OTHERS THEN
5229     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5230 
5231 END get_composite_rslt;
5232 
5233 
5234 
5235 
5236 
5237 --+========================================================================+
5238 --| change_lot_status
5239 --| DESCRIPTION
5240 --|    Called from check_disp procedure.
5241 --| This procedure gets the reason code from the quality config table and
5242 --| calls the update_lot procedure.
5243 --| This procedure assumes the proper disposition and status have already
5244 --| been determined.
5245 --+========================================================================+
5246 PROCEDURE change_lot_status
5247 ( p_sample_id        IN         NUMBER
5248 , p_organization_id  IN         NUMBER
5249 , p_lot_status       IN         VARCHAR2
5250 , x_return_status    OUT NOCOPY VARCHAR2
5251 ) IS
5252 
5253   -- Local Variables
5254   l_reason_id    gmd_quality_config.transaction_reason_id%TYPE;
5255   l_message_data   VARCHAR2(2000);
5256 
5257   -- this is almost the same cursor as cur_global_configurator
5258   -- in the samples form.
5259   CURSOR Cur_quality_config (p_organization_id NUMBER) IS
5260   SELECT transaction_reason_id
5261   FROM   gmd_quality_config
5262   WHERE  organization_id = p_organization_id
5263   AND    transaction_reason_id IS NOT NULL
5264   ;
5265 
5266 BEGIN
5267   --  Initialize API return status to success
5268   x_return_status := FND_API.G_RET_STS_SUCCESS;
5269 
5270   IF (l_debug = 'Y') THEN
5271     gmd_debug.put_line('Entering procedure: CHANGE_LOT_STATUS');
5272   END IF;
5273 
5274   -- Get the reason code from quality configuration/parameters
5275   OPEN  Cur_quality_config(p_organization_id);
5276   FETCH Cur_quality_config INTO l_reason_id ;
5277   IF Cur_quality_config%NOTFOUND THEN                 -- #1
5278     CLOSE Cur_quality_config;
5279     GMD_API_PUB.Log_Message('GMD_QM_INV_REASON_CODE');
5280     RAISE FND_API.G_EXC_ERROR;
5281   END IF;                                             -- #1
5282   CLOSE Cur_quality_config;
5283 
5284   IF (l_debug = 'Y') THEN
5285     gmd_debug.put_line('  Lot status ' || p_lot_status || ' Reason ID ' || TO_CHAR(l_reason_id));
5286   END IF;
5287 
5288 
5289   IF  p_lot_status IS NOT NULL  THEN                 -- #3
5290     -- no grade, no composite id,
5291 --rconv
5292     gmd_samples_grp.update_lot_grade_batch(         -- nsrivast
5293 	  	  p_sample_id		=> p_sample_id
5294 		, p_composite_spec_disp_id  => NULL
5295 		, p_to_lot_status_id	=> p_lot_status
5296         , p_from_lot_status_id	=> NULL --p_from_lot_status
5297 		, p_to_grade_code		=> NULL
5298         , p_from_grade_code		=> NULL
5299 		, p_to_qc_status	=> NULL
5300  	    , p_hold_date       => NULL
5301 		, p_reason_id		=> l_reason_id
5302  --   , p_update_child => NULL -- Added for Results Convergence. rboddu.
5303 		, x_return_status 	=> x_return_status
5304 		, x_message_data	=> l_message_data );
5305 
5306     IF x_return_status <> 'S' THEN          -- #4
5307       GMD_API_PUB.Log_Message(l_message_data);
5308       RAISE FND_API.G_EXC_ERROR;
5309     END IF;                                 -- #4
5310   END IF;                                            -- #3
5311 
5312   IF (l_debug = 'Y') THEN
5313     gmd_debug.put_line('  Leaving Procedure: CHANGE_LOT_STATUS');
5314   END IF;
5315 
5316 
5317 EXCEPTION
5318   WHEN    FND_API.G_EXC_ERROR THEN
5319     x_return_status := FND_API.G_RET_STS_ERROR ;
5320   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5321     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5322   WHEN OTHERS THEN
5323     GMD_API_PUB.Log_Message('GMD_API_ERROR',
5324                             'PACKAGE','CHANGE_LOT_STATUS','ERROR', SUBSTR(SQLERRM,1,100));
5325     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5326 END change_lot_status;
5327 
5328 
5329 
5330 
5331 
5332 --  /*#####################################################
5333 --  # NAME
5334 --  #    is_value_numeric
5335 --  # SYNOPSIS
5336 --  #    Proc  is_value_numeric
5337 --  #    Parms X_temp
5338 --  # DESCRIPTION
5339 --  #    This function returns TRUE if result is numeric else
5340 --  #    returns FALSE.
5341 --  #####################################################*/
5342 
5343 FUNCTION is_value_numeric (p_char_number VARCHAR2)
5344 RETURN BOOLEAN IS
5345   l_dummy NUMBER;
5346 BEGIN
5347   l_dummy := TO_NUMBER(p_char_number);
5348   RETURN TRUE;
5349 EXCEPTION
5350   WHEN VALUE_ERROR THEN
5351     RETURN FALSE;
5352   WHEN OTHERS THEN
5353     RETURN FALSE;
5354 END is_value_numeric;
5355 
5356 
5357 
5358 
5359 --  /*#####################################################
5360 --  # NAME
5361 --  #    check_experimental_error
5362 --  # SYNOPSIS
5363 --  #    Proc  check_experimental_error
5364 --  #    See QC4H DLD for write-up on fuzzy zones.
5365 --  #    1) If error type is PCT, calculate the raw numbers using spec min /spec max
5366 --  #    2) Calculate the upper and lower bounds of the fuzzy zones
5367 --  #    3) Check if the result falls within these bounds.
5368 --  #    4) If so, show the warning with the corresponding action code / desc.
5369 --  #       Pass back flags.
5370 --  # DESCRIPTION
5371 --  #   The following columns from RESULT_DATA record are used:
5372 --  #      result(already rounded, already validated against test limits),
5373 --  #      exp_error_type, min_num, max_num,
5374 --  #      spec_min_num, spec_max_num, out-action-code,
5375 --  #      below_spec_min, above_spec_min, below_spec_max, above_spec_max
5376 --  #      action code fields corresponding to above exp error regions.
5377 --  #   The following fields are updated by this procedure:
5378 --  #      in_fuzzy_zone, result_action_code
5379 --  # HISTORY
5380 --  # L.R.Jackson  Sept/Oct 2002
5381 --  #####################################################*/
5382 
5383 
5384 PROCEDURE check_experimental_error
5385 ( p_result_rec     IN OUT NOCOPY  RESULT_DATA
5386 , x_return_status     OUT NOCOPY  VARCHAR2
5387 )   IS
5388 
5389   L_below_min  NUMBER;
5390   L_above_min  NUMBER;
5391   L_below_max  NUMBER;
5392   L_above_max  NUMBER;
5393 
5394   L_test_range       NUMBER;     --  percentages are calculated
5395                                  --  against the test range.
5396   L_lower_bound_min  NUMBER;        -- fuzzy zone is from the lower bound
5397   L_upper_bound_min  NUMBER;        -- of the min to the upper bound of the spec min
5398   L_lower_bound_max  NUMBER;        -- and from the lower bound of the spec max
5399   L_upper_bound_max  NUMBER;        -- to the upper bound of the spec max.
5400                                     -- "outside" and "inside" fields CAN be null or 0
5401 
5402   -- assign the block.field to variables so that forms does not have to look up
5403   -- the value each time it is referenced in the procedure.
5404   L_result           NUMBER  := to_number(p_result_rec.result);
5405 
5406 BEGIN
5407 
5408   --  Initialize API return status to success
5409   x_return_status := FND_API.G_RET_STS_SUCCESS;
5410 
5411   IF (l_debug = 'Y') THEN
5412      gmd_debug.put_line('Entering procedure CHECK_EXPERIMENTAL_ERROR');
5413   END IF;
5414 
5415   p_result_rec.in_fuzzy_zone := 'FALSE';
5416 
5417   L_below_min  := p_result_rec.below_spec_min ;
5418   L_above_min  := p_result_rec.above_spec_min  ;
5419   L_below_max  := p_result_rec.below_spec_max  ;
5420   L_above_max  := p_result_rec.above_spec_max ;
5421 
5422   IF p_result_rec.exp_error_type IS NOT NULL THEN
5423     -- convert percentages to raw numbers
5424     IF p_result_rec.exp_error_type = 'P' THEN
5425       L_test_range := p_result_rec.max_num - p_result_rec.min_num;
5426       L_below_min  := (L_below_min * .01) * L_test_range;
5427       L_above_min  := (L_above_min * .01) * L_test_range;
5428       L_below_max  := (L_below_max * .01) * L_test_range;
5429       L_above_max  := (L_above_max * .01) * L_test_range;
5430     END IF;      -- end if error type is pct or num
5431 
5432     L_lower_bound_min := p_result_rec.spec_min_num - L_below_min;
5433     L_upper_bound_min := p_result_rec.spec_min_num + L_above_min;
5434     L_lower_bound_max := p_result_rec.spec_max_num - L_below_max;
5435     L_upper_bound_max := p_result_rec.spec_max_num + L_above_max;
5436 
5437     IF (l_debug = 'Y') THEN
5438       gmd_debug.put_line('Exp Error Type ' || p_result_rec.exp_error_type ||
5439             ' Result ' || L_result ||' Test Range ' || L_test_range);
5440       gmd_debug.put_line
5441            (' Lower Bound Min ' || L_lower_bound_min ||
5442             ' Upper Bound Min ' || L_upper_bound_min ||
5443             ' Lower Bound Max ' || L_lower_bound_max ||
5444             ' Upper Bound Max ' || L_upper_bound_max );
5445     END IF;
5446 
5447     IF (L_result >= L_lower_bound_min and L_result < p_result_rec.spec_min_num)  THEN --Bug5220513 made < instead of <=
5448         p_result_rec.result_action_code := p_result_rec.below_min_action_code ;
5449         p_result_rec.in_fuzzy_zone      := 'TRUE';
5450     ELSIF (L_result >= p_result_rec.spec_min_num and L_result < L_upper_bound_min)  THEN  --Bug5220513 made < instead of <=
5451         p_result_rec.result_action_code := p_result_rec.above_min_action_code;
5452         p_result_rec.in_fuzzy_zone      := 'TRUE';
5453     ELSIF (L_result >= L_lower_bound_max and L_result <= p_result_rec.spec_max_num) THEN
5454         p_result_rec.result_action_code := p_result_rec.below_max_action_code;
5455         p_result_rec.in_fuzzy_zone      := 'TRUE';
5456     ELSIF (L_result >= p_result_rec.spec_max_num and L_result <= L_upper_bound_max)  THEN
5457         p_result_rec.result_action_code := p_result_rec.above_max_action_code;
5458         p_result_rec.in_fuzzy_zone      := 'TRUE';
5459     ELSIF (L_result < p_result_rec.spec_min_num
5460         OR L_result > p_result_rec.spec_max_num)  THEN
5461         -- if out of bounds, copy out of bounds action code
5462         p_result_rec.result_action_code := p_result_rec.out_action_code ;
5463     END IF;          -- end if result falls in any of the fuzzy zones
5464 
5465     IF p_result_rec.in_fuzzy_zone = 'TRUE' THEN
5466       FND_MESSAGE.SET_NAME ('GMD', 'GMD_QC_IN_EXPERIMENTAL_ERROR');
5467     END IF;
5468     IF p_result_rec.result_action_code is not NULL THEN
5469       FND_MESSAGE.SET_NAME ('GMD', 'GMD_QM_RSLT_OUT_OF_SPEC_ACT');
5470     END IF;
5471   END IF;            -- end if there is a value in error type,
5472                      -- experimental error is optional.
5473 
5474   IF (l_debug = 'Y') THEN
5475      gmd_debug.put_line('In Fuzzy Zone ' || p_result_rec.in_fuzzy_zone ||
5476             ' Action Code ' || p_result_rec.result_action_code);
5477   END IF;
5478 
5479 EXCEPTION
5480   WHEN    FND_API.G_EXC_ERROR THEN
5481     x_return_status := FND_API.G_RET_STS_ERROR ;
5482   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5483     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5484   WHEN OTHERS THEN
5485     GMD_API_PUB.Log_Message('GMD_API_ERROR',
5486                             'PACKAGE','CHECK_EXPERIMENTAL_ERROR','ERROR', SUBSTR(SQLERRM,1,100));
5487     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5488 END check_experimental_error;
5489 
5490 
5491 --Start of comments
5492 --+========================================================================+
5493 --| API Name    : validate_resource                                        |
5494 --| TYPE        : Group                                                    |
5495 --| Notes       : Validates the resource and resource instance passed to   |
5496 --|               public API.                                              |
5497 --|                                                                        |
5498 --|                                                                        |
5499 --|                                                                        |
5500 --|                                                                        |
5501 --| HISTORY                                                                |
5502 --|    Manish Gupta     17-Jan-2003     Created.                           |
5503 --|                                                                        |
5504 --+========================================================================+
5505 FUNCTION isvalid_resource(p_lab_organization_id  NUMBER,
5506                           p_resource          VARCHAR2,
5507                           p_resource_instance NUMBER) RETURN BOOLEAN IS
5508 
5509 CURSOR c_validate_resource (p_resource VARCHAR)
5510 IS
5511  SELECT 1
5512  FROM cr_rsrc_mst
5513  WHERE resources = p_resource
5514  AND   delete_mark = 0;
5515 
5516 CURSOR c_validate_resource_instance (p_resource VARCHAR,
5517                                      p_resource_instance VARCHAR)
5518 IS
5519  SELECT 1
5520  FROM   gmp_resource_instances ri,
5521        cr_rsrc_dtl rd
5522  WHERE rd.resources  = p_resource
5523  AND   rd.organization_id =  p_lab_organization_id
5524  AND   rd.resource_id = ri.resource_id
5525  AND   ri.instance_id = p_resource_instance
5526  AND   ri.inactive_ind = 0;
5527 
5528 l_dummy     PLS_INTEGER;
5529 
5530 BEGIN
5531   --=========================================
5532    -- Validate Resource and Resource Instance.
5533    --==========================================
5534 
5535     IF (p_resource IS NULL AND p_resource_instance IS NOT NULL) THEN
5536       RETURN FALSE;
5537     END IF;
5538 
5539     IF (p_resource IS NOT NULL) THEN
5540 
5541       OPEN c_validate_resource(p_resource);
5542       FETCH c_validate_resource INTO l_dummy;
5543       IF (c_validate_resource%NOTFOUND) THEN
5544         CLOSE c_validate_resource;
5545         RETURN FALSE;
5546       END IF;
5547       CLOSE c_validate_resource;
5548 
5549       IF (p_resource_instance IS NOT NULL) THEN
5550 
5551         OPEN c_validate_resource_instance(p_resource,
5552                                           p_resource_instance);
5553         FETCH  c_validate_resource_instance INTO l_dummy;
5554         IF (c_validate_resource_instance%NOTFOUND) THEN
5555           CLOSE c_validate_resource_instance;
5556           RETURN FALSE;
5557         END IF;
5558         CLOSE c_validate_resource_instance;
5559 
5560       END IF;
5561 
5562     END IF;  -- p_results_rec.planned_resource IS NOT NULL
5563     RETURN TRUE;
5564 EXCEPTION
5565   WHEN OTHERS THEN
5566      NULL;
5567 END isvalid_resource;
5568 
5569 
5570 
5571 --/*#####################################################
5572 --  # NAME
5573 --  #    validate_result
5574 --  # SYNOPSIS
5575 --  #    Proc
5576 --  # DESCRIPTION
5577 --  #   1. If no result, clear other fields.
5578 --  #   2. Determine result_num and/or result_char
5579 --  #   3. Round, if numeric test type
5580 --  #   4. Check result against Test limits.
5581 --  #      If not called from the form and test type is V,
5582 --  #      validate that the result is IN the list in test_values table.
5583 --  #   5. Call API to determine in or out of spec
5584 --  #   6. Check experimental error, if numeric test type
5585 --  #   7. Set evaluation field and, possibly, action field.
5586 --  #      and out_of_spec flag.
5587 --  #
5588 --  #  The following fields from the RESULTS_DATA record are used:
5589 --  #   test_id and test type are required.
5590 --  #    test_id, test_type,result, min_num, max_num,
5591 --  #    spec_min_num, spec_max_num, spec_id, display_precision,
5592 --  #    report_precision, additional_test_ind, exp_error_type,
5593 --  #    4 experimental error number fields, 4 experimental error
5594 --  #    action code fields, out_action_code, called_from_form.
5595 --  #  The following fields are updated by this procedure:
5596 --  #    result, result_num, result_char, result_date, in_spec,
5597 --  #    evaluation_ind, result_action_code, display_label,
5598 --  #    value_in_report_precision, out_of_spec, in_fuzzy_zone,
5599 --  #    x_return_status
5600 --  #  Assumptions:  For numeric tests, if no spec-level exp
5601 --  #    error data is given, calling programs have provided
5602 --  #    any test-level experimental error data.
5603 --  #
5604 --  #  Srastogi : added test_id and test_type to the call
5605 --  #             GMD_SPEC_GRP.spec_test_min_target_max_valid
5606 --  # QZENG Bug 13897977 Set value to out_of_spec and in_spec when
5607 --  #    test is an additional one added manually.
5608 --  #####################################################*/
5609 
5610 
5611 PROCEDURE validate_result
5612 ( p_result_rec     IN OUT NOCOPY result_data
5613 , x_return_status  OUT    NOCOPY VARCHAR2
5614 ) IS
5615 
5616   L_num_result    NUMBER;
5617 
5618 BEGIN
5619 
5620   --  Initialize API return status to success
5621   x_return_status := FND_API.G_RET_STS_SUCCESS;
5622 
5623   IF (l_debug = 'Y') THEN
5624      gmd_debug.put_line('Entering procedure VALIDATE_RESULT');
5625   END IF;
5626 
5627   IF p_result_rec.result IS NULL THEN
5628         --#. ===========================
5629         --#. They didn't enter a result!
5630         --#. ===========================
5631 
5632     p_result_rec.result_date          := NULL;
5633     p_result_rec.in_spec              := NULL;
5634     p_result_rec.evaluation_ind       := NULL;
5635     p_result_rec.result_num           := NULL;
5636     p_result_rec.result_char          := NULL;
5637     p_result_rec.display_label        := NULL;
5638     p_result_rec.result_action_code   := NULL;
5639     p_result_rec.value_in_report_prec := NULL;
5640 
5641   ELSE
5642     --#. =========================================================
5643     --#. start fresh in case new result is ok
5644     --#. and no action code should be displayed.
5645     --#. =========================================================
5646     p_result_rec.result_action_code := NULL;
5647 
5648     IF p_result_rec.test_type in ('N', 'L', 'E') THEN
5649 
5650       --#. =========================================================
5651       --#. Ensure the value is numeric. If not, return user to field
5652       --#. Apply decimal precision.
5653       --#. =========================================================
5654       IF NOT (is_value_numeric (p_result_rec.result)) THEN
5655         GMD_API_PUB.Log_Message('GMD', 'QC_NOTNUMERIC');
5656         RAISE FND_API.G_EXC_ERROR;
5657       END IF;
5658 
5659       L_num_result := ROUND(to_number(p_result_rec.result) ,
5660                              p_result_rec.display_precision);
5661       p_result_rec.value_in_report_prec := ROUND(to_number(p_result_rec.result),
5662                              p_result_rec.report_precision);
5663       p_result_rec.result_num := l_num_result;
5664       p_result_rec.result     := l_num_result;    -- used in check-exp-error
5665 
5666       --#. =========================================================
5667       --#. Send test min, result, test max to function.
5668       --#. Is result within Test-level min and max?
5669       --#. =========================================================
5670 
5671       IF NOT GMD_SPEC_GRP.spec_test_min_target_max_valid
5672                            (  p_test_id   => p_result_rec.test_id
5673                             , p_test_type => p_result_rec.test_type
5674                             , p_validation_level => 'ST_TARGET'
5675                             , p_st_min    => NULL
5676                             , p_st_target => L_num_result
5677                             , p_st_max    => NULL
5678                             , p_t_min     => p_result_rec.min_num
5679                             , p_t_max     => p_result_rec.max_num
5680                            )     THEN
5681         GMD_API_PUB.Log_Message('GMD', 'NOT_IN_RANGE');
5682         --IF  p_result_rec.test_type = 'E' THEN  --Bug#6774613 QZENG
5683           -- if expression give warning, not error
5684           x_return_status := 'E';           -- expected error
5685         --ELSE
5686           RAISE FND_API.G_EXC_ERROR;
5687         --END IF;
5688       END IF;     -- end if within test min/max
5689 
5690       IF (l_debug = 'Y') THEN
5691         gmd_debug.put_line('Result is within test range.  Rounded value = ' || L_num_result);
5692       END IF;
5693 
5694       IF p_result_rec.test_type = 'L' THEN
5695          p_result_rec.display_label := GMD_QC_TEST_VALUES_GRP.get_test_value_desc
5696                                         (p_test_id        => p_result_rec.test_id,
5697                                          p_test_value_num => L_num_result
5698                                         );
5699       END IF;  -- end if test is num range w/ display, get label
5700 
5701     ELSE
5702       -- else test type is V, T, U
5703       IF p_result_rec.called_from_form = 'N'
5704             OR p_result_rec.called_from_form is NULL THEN
5705       null;
5706       END IF;
5707       -- move result to result_char
5708       p_result_rec.result_char := p_result_rec.result;
5709       --RLNAGARA Bug 3892837 Added below line because the result_num value will be assigned in the IF condition when there is
5710       -- a Numeric result but this does not get cleared and hence it has to be cleared when it is not Numeric.
5711       p_result_rec.result_num := NULL;
5712 
5713     END IF;           -- move result to result...num or result...char
5714 
5715     IF  p_result_rec.additional_test_ind IS NULL THEN
5716         --#. ====================================================
5717         --#. If this is not an additional test, then there should
5718         --#. be a spec.  So we can use the canned in_spec function.
5719         --#. With additional tests, there is not a spec.
5720         --#. ===================================================
5721 
5722       p_result_rec.in_spec := GMD_RESULTS_GRP.rslt_is_in_spec
5723                      (p_spec_id => p_result_rec.spec_id,
5724                       p_test_id => p_result_rec.test_id,
5725                       p_rslt_value_num  => p_result_rec.result_num,
5726                       p_rslt_value_char => p_result_rec.result_char);
5727       IF p_result_rec.in_spec IS NOT NULL THEN
5728         p_result_rec.out_of_spec := 'FALSE';
5729       END IF;
5730     --QZENG Bug 13897977 Considered tests that added manually
5731     ELSE
5732       p_result_rec.out_of_spec := 'FALSE';
5733       p_result_rec.in_spec := 'Y';
5734     END IF;    -- end if not an additional test/spec or no spec
5735 
5736     IF (l_debug = 'Y') THEN
5737       gmd_debug.put_line('After call to rslt_is_is_spec, before call to check_exp_error');
5738     END IF;
5739 
5740     IF p_result_rec.test_type in ('N', 'L', 'E') THEN
5741 
5742       GMD_RESULTS_GRP.check_experimental_error  ( p_result_rec
5743                                                 , x_return_status );
5744       IF (x_return_status <> 'S') THEN
5745         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5746       END IF;
5747     END IF;             -- only send numeric types for exp error checking
5748 
5749 
5750     --#. =========================================================
5751     --#. Only check tests from spec (num or alpha).  There is no
5752     --#. concept of out-of-spec with additional tests.
5753     --#. Check if error type is null because tests with error
5754     --#. types have already been evaluated in fuzzy zone code.
5755     --#. =========================================================
5756     IF  p_result_rec.additional_test_ind IS NULL AND
5757         p_result_rec.exp_error_type IS NULL AND
5758         p_result_rec.in_spec IS NULL  THEN
5759       p_result_rec.out_of_spec := 'TRUE';
5760       p_result_rec.result_action_code := p_result_rec.out_action_code;
5761     END IF;          -- end if result falls in either of the fuzzy zones
5762 
5763 
5764     IF p_result_rec.in_fuzzy_zone = 'TRUE' THEN
5765       p_result_rec.evaluation_ind := '3E';
5766     ELSIF (p_result_rec.in_spec IS NOT NULL) THEN
5767       p_result_rec.evaluation_ind := '0A';
5768     ELSE
5769       --#. ===================================================
5770       --#. Clear the eval indicator and the action code field.
5771       --#. They are the only ones which could be NULL because
5772       --#. of a result value.
5773       --#. ===================================================
5774       IF p_result_rec.out_of_spec = 'FALSE' THEN
5775         p_result_rec.result_action_code := NULL;
5776       END IF;
5777       p_result_rec.evaluation_ind := NULL;
5778     END IF;    --  end setting evaluation ind.
5779 
5780   END IF;      -- Result is null then clear, else validate
5781 
5782 
5783   --===========================================
5784   -- Validate Resource
5785   --===========================================
5786 
5787   IF (NOT isvalid_resource(p_result_rec.lab_organization_id,
5788                           p_result_rec.planned_resource,
5789                           p_result_rec.planned_resource_instance)) THEN
5790     GMD_API_PUB.Log_Message('GMD_INVALID_PARAMETERS');
5791     RAISE FND_API.G_EXC_ERROR;
5792   END IF;
5793 
5794   IF (NOT isvalid_resource(p_result_rec.lab_organization_id,
5795                           p_result_rec.actual_resource,
5796                           p_result_rec.actual_resource_instance)) THEN
5797     GMD_API_PUB.Log_Message('GMD_INVALID_PARAMETERS');
5798     RAISE FND_API.G_EXC_ERROR;
5799   END IF;
5800 
5801   IF (l_debug = 'Y') THEN
5802     gmd_debug.put_line('End of validate_result.  In-Spec ' || p_result_rec.in_spec
5803                         ||' Evaluation ' || p_result_rec.evaluation_ind
5804                         ||' Fuzzy ' || p_result_rec.in_fuzzy_zone
5805                         ||' Out of Spec ' || p_result_rec.out_of_spec);
5806   END IF;
5807 
5808 EXCEPTION
5809   WHEN    FND_API.G_EXC_ERROR THEN
5810     x_return_status := FND_API.G_RET_STS_ERROR ;
5811   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5812     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5813   WHEN OTHERS THEN
5814     GMD_API_PUB.Log_Message('GMD_API_ERROR',
5815                               'PACKAGE','VALIDATE_RESULT','ERROR', SUBSTR(SQLERRM,1,100));
5816     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5817 END validate_result;
5818 
5819 
5820 
5821 
5822 
5823 --/*###########################################################
5824 --# NAME
5825 --#    validate_evaluation_ind
5826 --# DESCRIPTION
5827 --#    Called from when_validate_item procedure.
5828 --#    Validate the Evaluation field based on in or out
5829 --#      of spec, experimental error, current evaluation.
5830 --#    The LOV is used for validation, so must include current value
5831 --#      in the list.  (User cannot CHANGE disp to Exp Error, but it
5832 --#      must be in the list if the system changes to Exp Error.)
5833 --#    Only the manager responsibility has Edit access to this field.
5834 --# HISTORY
5835 --############################################################*/
5836 
5837 PROCEDURE validate_evaluation_ind
5838 ( p_evaluation_ind      IN         VARCHAR2
5839 , p_in_spec_ind         IN         VARCHAR2
5840 , p_result_value        IN         VARCHAR2
5841 , x_return_status       OUT NOCOPY VARCHAR2
5842 )
5843 IS
5844 
5845 BEGIN
5846   --  Initialize API return status to success
5847   x_return_status := FND_API.G_RET_STS_SUCCESS;
5848 
5849   IF (l_debug = 'Y') THEN
5850     gmd_debug.put_line('Entering procedure VALIDATE_EVALUATION_IND');
5851     gmd_debug.put_line ('   Evaluation Ind = ' || p_evaluation_ind);
5852   END IF;
5853 
5854   --== If no result, eval can be set only to NULL, CANCEL, VOID or
5855   --    Bug 3763419 - added                  GUARANTEED BY MANUFACTURER
5856 
5857 
5858   IF (p_result_value IS NULL       AND
5859       p_evaluation_ind IS NOT NULL AND
5860       p_evaluation_ind NOT IN ('4C', '5O', '1Z'))
5861   THEN
5862     GMD_API_PUB.Log_message ('GMD', 'GMD_QM_EVAL_BLANK_RESULT');
5863     RAISE FND_API.G_EXC_ERROR;
5864   END IF;
5865 
5866   IF nvl(p_in_spec_ind, 'N') = 'Y'  THEN
5867     -- Result is IN-SPEC
5868     IF (p_evaluation_ind = '3E') THEN
5869       GMD_API_PUB.Log_Message('GMD', 'GMD_QM_EVAL_NO_EXP_ERROR');
5870       RAISE FND_API.G_EXC_ERROR;
5871     END IF;
5872   ELSE
5873     -- else not in spec
5874     --    Bug 3763419 - added         GUARANTEED BY MANUFACTURER
5875     IF (p_evaluation_ind NOT IN ('1V', '2R', '4C', '5O', '1Z')) THEN
5876       GMD_API_PUB.Log_Message('GMD', 'GMD_QM_EVAL_OUT_OF_SPEC');
5877       RAISE FND_API.G_EXC_ERROR;
5878     END IF;
5879   END IF;
5880 
5881 EXCEPTION
5882   WHEN    FND_API.G_EXC_ERROR THEN
5883     x_return_status := FND_API.G_RET_STS_ERROR ;
5884   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5885     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5886   WHEN OTHERS THEN
5887     GMD_API_PUB.Log_Message('GMD_API_ERROR',
5888                             'PACKAGE','VALIDATE_EVALUATION_IND','ERROR', SUBSTR(SQLERRM,1,100));
5889     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5890 END  validate_evaluation_ind;
5891 
5892 
5893 
5894 
5895 
5896 
5897 -- /*###############################################################
5898 -- # NAME
5899 -- #	calc_expression
5900 -- # SYNOPSIS
5901 -- #	proc calc_expression
5902 -- # DESCRIPTION
5903 -- # This assumes records have been posted (if called from forms).
5904 -- # 1. Start at the 1st record.
5905 -- # 2. For each record after the 1st, loop through records, checking
5906 -- #     if they are of type Expression.
5907 -- # 3. If the current test is of type Expression, call the API
5908 -- #     GMD_UTILITY_PKG.parse, which breaks the expression into its
5909 -- #     components.
5910 -- #
5911 -- #
5912 -- #
5913 -- #
5914 -- #
5915 -- #
5916 -- # HISTORY
5917 --|                                                                        |
5918 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
5919 --|       in select for table - gmd_spec_tests_b                           |
5920 --|                                                                        |
5921 --|    Vipul Vaish 23-Jul-2003 Bug#3063671                                 |
5922 --|       Added a call to the Validate_Result procedure which is used      |
5923 --|       to update the gmd_spec_results table with proper values when the |
5924 --|       test type is of Expression.                                      |
5925 --|    RajaSekhar 12-Nov-2003 BUG#3243631 Frontport for 3199585            |
5926 --|	Removed checking for the NULL results since expression can be	         |
5927 --|	calculated without Test Result if the expression is written with       |
5928 --|	nvl SQL function.                                                      |
5929 --|                                                                        |
5930 --|    Ravi Boddu 29-APR-2004 Bug 3447472 Test Groups Enhancement          |
5931 --|  Modifed cursors c_exp_test and c_all_ref_test to the data from base   |
5932 --| tables, rather than the Global Temp table gmd_result_data_points_gt    |
5933 --| which fetches only the most recent Test Replicates. Also not calling   |
5934 --| populate_result_data_points anymore
5935 --| RLNAGARA 16-MAR-2006 Bug#5076736. Updating the result date of   |
5936 --| results properly depending on the result value. |
5937 --| RLNAGARA 22-MAR-2006 Bug#5097709. Not evaluating the results           |
5938 --| of expression tests when the refernce tests result vlaues are not      |
5939 --| modified  and their evaluations are modified to void or cancel.        |
5940 --| RAGSRIVA 09-May-2006 Bug# 5195678 Frontport Bug# 5158678               |
5941 --| Modified the cursor c_exp_test to fetch the expression type tests      |
5942 --| details though the result value is not NULL.                           |
5943 --|  P Lowe 22-Nov-2012	Bug 13355408 - added p_called_from_results_form IN |
5944 --| VARCHAR2  DEFAULT 'N' to PROCEDURE calc_expression - used to determine |
5945 --| when called from mass results entry  or results form  as for this bug  |
5946 --| we should NOT recalculate test expressions when they have already been |
5947 --| stored in the database when called from results form                   |
5948 --| QZENG 13-Apr-2012 Bug 6774613                                          |
5949 --|    Moved update result value to another LOOP to avoid calculat exceed  |
5950 --|    its range.                                                          |
5951 --|  P Lowe 29-May-2012	    Bug 14021970 - CALCULATE EXPRESSION FUNCTION   |
5952 --|     WILL FILL IN THE WRONG FIELD - added new procedure in              |
5953 --|   calc_expression  -                                                   |
5954 --|   populate_result_data_points_r to use replicate to calculate correct  |
5955 --| test expressions    based on replicate used.                           |
5956 -- #######################################################################*/
5957 PROCEDURE calc_expression
5958 ( p_sample_id           IN         NUMBER
5959 , p_event_spec_disp_id  IN         NUMBER
5960 , p_spec_id             IN         NUMBER
5961 , p_called_from_results_form IN  VARCHAR2   -- 13355408  default 'N'
5962 , x_rslt_tbl            OUT NOCOPY rslt_tbl
5963 , x_return_status       OUT NOCOPY VARCHAR2
5964 ) IS
5965   --QZENG Bug 6774613 Start
5966   -- Added the type to save result value for update.
5967   TYPE TEST_RESULT IS RECORD
5968   (
5969    result_id gmd_results.result_id%type,
5970    result_value_num gmd_results.result_value_num%type
5971   );
5972 
5973   TYPE TEST_RESULTS IS TABLE OF TEST_RESULT;
5974 
5975   l_test_results TEST_RESULTS;
5976   --QZENG Bug 6774613 End
5977   CURSOR get_referenced_tests (p_exp_test_id NUMBER) IS
5978       SELECT  expression_ref_test_id
5979         FROM  gmd_qc_test_values_b
5980        where test_id = p_exp_test_id;
5981 
5982   -- Modified the following cursor to fetch all the test replicates
5983   -- whichever is not calculated the Result yet.
5984   -- Earlier the data was being fetched from Global Temp Table gmd_result_data_points_gt
5985   -- Test Groups Enh Bug# 3447472
5986   CURSOR c_exp_test IS
5987   SELECT t.test_id, t.test_code, t.expression, t.display_precision,
5988          t.report_precision, r.result_id, r.result_value_num , r.test_replicate_cnt   -- 14021970  added    r.test_replicate_cnt
5989   FROM   gmd_samples s,
5990          gmd_results r,
5991          gmd_spec_results sr,
5992          gmd_qc_tests_b t
5993   WHERE  s.sample_id = r.sample_id
5994   AND    s.sample_id = p_sample_id
5995   AND    s.retain_as IS NULL
5996   AND    r.result_id = sr.result_id
5997   AND    sr.event_spec_disp_id = p_event_spec_disp_id
5998   AND    NVL(sr.evaluation_ind, 'XX') NOT IN ('5O','4C')
5999   AND    r.test_id = t.test_id
6000   AND    t.test_type= 'E'
6001   AND    sr.delete_mark = 0
6002   AND    r.delete_mark = 0
6003  --AND    r.result_value_num IS NULL; -- Bug# 5195678
6004   AND  (   ( r.result_value_num IS NULL and p_called_from_results_form   = 'Y')
6005   OR p_called_from_results_form = 'N');   -- bug 13355408  this should only be taken out for when called from mass results entry
6006   --  bug 13355408   p_called_from_results_form used to determine when called from mass results entry  or results form
6007 
6008 
6009   --BEGIN BUG#3063671 Vipul Vaish
6010   --Declared a Cursor to fetch the test type
6011    CURSOR   c_spec_test_all (p_spec_id NUMBER, p_test_id NUMBER) IS
6012     SELECT  *
6013     FROM   gmd_spec_tests_b
6014     WHERE  spec_id = p_spec_id
6015     AND    test_id = p_test_id
6016    ;
6017 
6018    CURSOR   c_add_test_id (p_event_spec_id NUMBER, p_result_id NUMBER) IS
6019     SELECT ADDITIONAL_TEST_IND
6020     FROM   gmd_spec_results
6021     WHERE  event_spec_disp_id = p_event_spec_id
6022     AND    result_id          = p_result_id
6023     ;
6024 
6025    CURSOR c_qc_test_value(p_test_id NUMBER) IS
6026     SELECT min_value_num,max_value_num
6027     FROM   gmd_qc_tests_b
6028     WHERE  test_id = p_test_id
6029     ;
6030 
6031    l_additional_test_ind VARCHAR2(1);
6032    l_min_value_num number;
6033    l_max_value_num number;
6034    l_spec_test_all  GMD_SPEC_TESTS_B%ROWTYPE;
6035    --Declared the X_rec of result_data type.
6036    X_rec                result_data;
6037   --END BUG#3063671
6038 
6039 CURSOR c_all_ref_test (p_exp_test_id NUMBER) IS
6040   SELECT gtmp.data_num,
6041          t.test_code
6042   FROM   gmd_result_data_points_gt gtmp,
6043          gmd_results r,
6044          gmd_qc_tests_b t,
6045          gmd_qc_test_values_b tv
6046   WHERE  gtmp.result_id = r.result_id
6047   AND    r.test_id = t.test_id
6048   AND    t.test_id = tv.expression_ref_test_id
6049   AND    tv.test_id = p_exp_test_id
6050   ;
6051 
6052   CURSOR c_spec_test (p_spec_id NUMBER, p_test_id NUMBER) IS
6053   SELECT display_precision, report_precision
6054   FROM   gmd_spec_tests_b
6055   WHERE  spec_id = p_spec_id
6056   AND    test_id = p_test_id
6057   AND    exclude_ind IS NULL
6058   ;
6059 
6060   l_value             NUMBER;
6061   l_exptab            GMD_UTILITY_PKG.exptab;
6062   l_boolean           BOOLEAN  := FALSE;
6063   l_exp_is_null       BOOLEAN;
6064 
6065   i                       PLS_INTEGER;
6066   l_display_precision     PLS_INTEGER;
6067   l_report_precision      PLS_INTEGER;
6068   l_return_status         VARCHAR2(1);
6069   l_display_value         NUMBER;
6070   l_report_value          NUMBER;
6071   l_ref_tests             NUMBER := 0;  --Bug#5097709
6072   l_ref_count             NUMBER := 0;
6073   l_samples               GMD_API_PUB.number_tab;
6074 
6075 BEGIN
6076 
6077   --  Initialize API return status to success
6078   x_return_status := FND_API.G_RET_STS_SUCCESS;
6079 
6080   IF (l_debug = 'Y') THEN
6081     gmd_debug.put_line('Entering procedure CALC_EXPRESSION');
6082     gmd_debug.put_line('   Sample ID = ' || p_sample_id );
6083   END IF;
6084 
6085 
6086   l_samples(1) := p_sample_id;
6087 
6088    -- Load the result data points for the sample    -- 14021970    replace by populate_result_data_points_r below
6089  /* populate_result_data_points (
6090       p_sample_ids         => l_samples
6091     , p_event_spec_disp_id => p_event_spec_disp_id
6092     , x_return_status      => l_return_status);
6093 
6094   IF (l_return_status <> 'S') THEN
6095     -- Error message is already logged
6096     RAISE FND_API.G_EXC_ERROR;
6097   END IF; */
6098 
6099 
6100   -- Index variable
6101   i := 0;
6102   l_test_results := TEST_RESULTS(); --QZENG Bug 6774613 Added to init the table record.
6103 
6104   IF (l_debug = 'Y') THEN
6105         gmd_debug.put_line('about to loop thru   FOR l_exp_test IN c_exp_test   ');
6106 
6107   END IF;
6108 
6109   -- Go through All the Expressions that are in the Sample
6110   FOR l_exp_test IN c_exp_test
6111   LOOP
6112      -- bring  populate_result_data_points inside     pass replicate cnt to populate_result_data_points_r  so this is now inside of loop    -- 14021970
6113     IF (l_debug = 'Y') THEN
6114       gmd_debug.put_line('calling  procedure populate_result_data_points_r  i =  '  || i  );
6115 
6116     END IF;
6117     populate_result_data_points_r (
6118       p_sample_ids         => l_samples
6119     , p_event_spec_disp_id => p_event_spec_disp_id
6120     , p_test_replicate_cnt => l_exp_test.test_replicate_cnt
6121     , x_return_status      => l_return_status);
6122 
6123 		 IF (l_return_status <> 'S') THEN
6124 		    -- Error message is already logged
6125 		    IF (l_debug = 'Y') THEN
6126         gmd_debug.put_line('calling  procedure populate_result_data_points_r  l_return_status <> S  ');
6127 
6128        END IF;
6129 
6130 		    RAISE FND_API.G_EXC_ERROR;
6131 		  END IF;
6132 
6133 	  -- Initialize IN Variable
6134     l_exp_is_null := FALSE;
6135     l_display_value := NULL;
6136     l_report_value  := NULL;
6137 
6138     l_exptab.DELETE;
6139 
6140     -- Parse the expression
6141     GMD_UTILITY_PKG.parse (
6142 		x_exp           => l_exp_test.expression,
6143 		x_exptab        => l_exptab,
6144 		x_return_status => l_return_status)
6145          ;
6146 
6147     IF l_return_status <> 'S' THEN
6148       -- Error message must be already logged
6149        RAISE FND_API.G_EXC_ERROR;
6150     END IF;
6151      select count(*) INTO l_ref_count from gmd_qc_test_values_b where test_id=l_exp_test.test_id; --Bug#5097709
6152     -- Now we have all the ref. tests for the current expression in l_exptab so
6153     -- fill-in result_values in l_exptab from _GT table
6154     FOR l_all_ref_test IN c_all_ref_test(l_exp_test.test_id)
6155     LOOP
6156       -- BEGIN BUG#3243631
6157       -- Commented the below code.
6158       -- IF (l_all_ref_test.data_num IS NULL) THEN
6159       -- -- Expression can not be evaluated as one of the reference test
6160       -- -- does not have result
6161       --   l_exp_is_null := TRUE;
6162       --   EXIT;
6163       -- END IF;
6164       -- END BUG#3243631
6165       l_ref_tests := l_ref_tests+1; --Bug#5097709
6166       GMD_UTILITY_PKG.variable_value (
6167            pvar_name       => l_all_ref_test.test_code,
6168            pvar_value      => l_all_ref_test.data_num ,
6169            p_exptab        => l_exptab,
6170            x_return_status => l_return_status);
6171       -- Above procedure does  unconditional FND_MSG_PUB.INITIALIZE;
6172       -- No need to look at return status
6173       -- Do the following for the last iteration
6174       FND_MSG_PUB.INITIALIZE;
6175     END LOOP;
6176 
6177     -- Expression can only be calculated if all the reference
6178     -- tests have results entered.
6179     IF (NOT l_exp_is_null AND l_ref_tests = l_ref_count) THEN  --Bug#5097709
6180       -- l_exptab is filled-in with values now so evaluate the exp.
6181       GMD_UTILITY_PKG.evaluate_exp (
6182   		  pexptab         => l_exptab,
6183                   pexp_test       => l_boolean,
6184                   x_value         => l_value,
6185   		  x_return_status => x_return_status);
6186       IF x_return_status <> 'S' THEN
6187          RAISE FND_API.G_EXC_ERROR;
6188       END IF;
6189 
6190       -- We have got the expression calculated so format this to
6191       -- the display and report precision values
6192 
6193       -- Step 1. Get the Display and report precision from Spec else Test
6194       l_display_precision := 0;
6195       l_report_precision  := 0;
6196 
6197       IF (p_spec_id IS NULL) THEN
6198         -- Get it from the Test
6199         l_display_precision := l_exp_test.display_precision;
6200         l_report_precision  := l_exp_test.report_precision;
6201       ELSE
6202         -- Get it from the Spec Test
6203         OPEN c_spec_test(p_spec_id, l_exp_test.test_id);
6204         FETCH c_spec_test INTO l_display_precision, l_report_precision;
6205         -- Bug# 5087404 Added condition for Tests not in specification but directly included
6206         IF c_spec_test%ROWCOUNT = 0 THEN
6207             l_display_precision := l_exp_test.display_precision;
6208             l_report_precision  := l_exp_test.report_precision;
6209         END IF;
6210         CLOSE c_spec_test;
6211       END IF;
6212 
6213       l_display_value := ROUND(l_value, l_display_precision);
6214       l_report_value  := ROUND(l_value, l_report_precision);
6215 
6216     END IF;  -- Evalulate Expression
6217 
6218     l_ref_tests := 0;  --Bug#5097709
6219     l_ref_count := 0;
6220 
6221 
6222     -- No need to update the WHO columns as they are either set in the
6223     -- FORM or the API
6224     --QZENG Bug 6774613 Start
6225     --Commented out the following update sql, moved it to another LOOP
6226     --UPDATE gmd_results
6227     --SET    result_value_num = l_display_value,
6228     --       result_date = SYSDATE
6229     --WHERE  result_id = l_exp_test.result_id
6230     --;
6231     --Saved the result value to l_test_results
6232     l_test_results.EXTEND(i + 1);
6233     l_test_results(i + 1).result_id := l_exp_test.result_id;
6234     l_test_results(i + 1).result_value_num := l_display_value;
6235     --QZENG Bug 6774613 End
6236 --RLNAGARA Bug5076736 Updating the result date of results properly depending on the result value.
6237      UPDATE gmd_results
6238      SET    result_date = NULL
6239      WHERE  result_id = l_exp_test.result_id
6240      AND  result_value_num IS NULL;
6241 
6242     --BEGIN BUG#3063671 Vipul Vaish
6243     OPEN c_add_test_id(p_event_spec_disp_id, l_exp_test.result_id);
6244     FETCH c_add_test_id INTO l_additional_test_ind;
6245     CLOSE c_add_test_id;
6246 
6247     OPEN c_qc_test_value(l_exp_test.test_id);
6248     FETCH c_qc_test_value INTO l_min_value_num,l_max_value_num;
6249     CLOSE c_qc_test_value;
6250 
6251     --Fetching the values depending upon the test id and spec_id.
6252     OPEN c_spec_test_all(p_spec_id, l_exp_test.test_id);
6253     FETCH c_spec_test_all INTO l_spec_test_all;
6254     CLOSE c_spec_test_all;
6255     --Passing the necessary parameters which are useful while calling the
6256     --validate_result procedure.
6257     x_rec.test_id := l_exp_test.test_id;
6258     x_rec.test_type := 'E';
6259     x_rec.result := l_display_value;
6260     x_rec.spec_id := p_spec_id;
6261     x_rec.spec_min_num := l_spec_test_all.min_value_num;
6262     x_rec.spec_max_num := l_spec_test_all.max_value_num;
6263     x_rec.out_action_code := l_spec_test_all.out_of_spec_action;
6264     x_rec.display_precision :=l_display_precision;
6265     x_rec.report_precision := l_report_precision;
6266     x_rec.exp_error_type  := l_spec_test_all.exp_error_type;
6267     x_rec.below_spec_min := l_spec_test_all.below_spec_min;
6268     x_rec.above_spec_min := l_spec_test_all.above_spec_min;
6269     x_rec.below_spec_max := l_spec_test_all.below_spec_max;
6270     x_rec.above_spec_max := l_spec_test_all.above_spec_max;
6271     x_rec.below_min_action_code := l_spec_test_all.below_min_action_code;
6272     x_rec.above_min_action_code := l_spec_test_all.above_min_action_code;
6273     x_rec.below_max_action_code := l_spec_test_all.below_max_action_code;
6274     x_rec.above_max_action_code := l_spec_test_all.above_max_action_code;
6275     x_rec.additional_test_ind   :=l_additional_test_ind;
6276     x_rec.min_num := l_min_value_num;
6277     x_rec.max_num := l_max_value_num;
6278 
6279     GMD_RESULTS_GRP.validate_result(x_rec,l_return_status);
6280     IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6281        RAISE FND_API.G_EXC_ERROR;
6282     END IF;
6283     --Updating the gmd_spec_results table, with proper values
6284     --which returned during the above call.
6285     UPDATE gmd_spec_results
6286     SET    value_in_report_precision = l_report_value,
6287            in_spec_ind = x_rec.in_spec,
6288            action_code = x_rec.result_action_code,
6289            evaluation_ind = x_rec.evaluation_ind
6290     WHERE  event_spec_disp_id = p_event_spec_disp_id
6291     AND    result_id          = l_exp_test.result_id
6292     ;
6293     --END BUG#3063671.
6294 
6295     -- Increament the index and store the result in OUT variable
6296     i := i + 1;
6297     x_rslt_tbl(i).test_id := l_exp_test.test_id;
6298     x_rslt_tbl(i).value   := l_display_value;
6299 
6300   END LOOP;
6301   --QZENG Bug 6774613 Start
6302   --Update the result here.
6303   FOR m IN 1 .. l_test_results.COUNT
6304   LOOP
6305     UPDATE gmd_results
6306     SET    result_value_num = l_test_results(m).result_value_num,
6307           result_date = SYSDATE
6308     WHERE  result_id = l_test_results(m).result_id;
6309   END LOOP;
6310   --QZENG Bug 6774613 End
6311 EXCEPTION
6312   WHEN    FND_API.G_EXC_ERROR THEN
6313     x_return_status := FND_API.G_RET_STS_ERROR ;
6314   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6315     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6316   WHEN OTHERS THEN
6317     GMD_API_PUB.Log_Message('GMD_API_ERROR',
6318                               'PACKAGE','CALC_EXPRESSION','ERROR', SUBSTR(SQLERRM,1,100));
6319     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6320 END calc_expression;
6321 
6322 
6323 
6324 
6325 
6326 
6327 
6328 --+========================================================================+
6329 --|NAME
6330 --|   change_sample_disposition
6331 --|SYNOPSIS
6332 --|   Proc check
6333 --|DESCRIPTION
6334 --|   Called from the post_forms_commit procedure.
6335 --|
6336 --|   After posting, before committing, figure out any disposition changes.
6337 --|   If      all tests are canceled, change disposition to Pending.
6338 --|   Else If all tests have a results, change disposition to complete.
6339 --|   Else    Disposition Change not required.
6340 --|
6341 --|                                                                        |
6342 --|    Chetan Nagar	01-Apr-2002	Added exclude_ind column condition |
6343 --|       in select for table - gmd_spec_tests_b  |
6344 --|  RLNAGARA 27-Mar-2006  B5106039 Modified the procedure so that it considers the Flag |
6345 --|                        Consider Optional Tests Result which is there in Process Quality Parameters Form |
6346 --|  P Lowe 17-Sep-2012	    Bug 14349136   OPTIONAL TESTS                               |
6347 --|  follow the rules for optional tests and the parameter 'Include Optional            |
6348 --|  Tests in Sample Disposition  - When we un check the Process Quality                |
6349 --|  setup Consider Optional Tests Results  all optional tests populated                |
6350 --|  or not will be ignored                                                             |
6351 --|  P Lowe 03-Dec-2012	 bug 15885923 rework for Bug 14349136   OPTIONAL TESTS          |
6352 --|  to follow the rules for optional tests and the parameter 'Include Optional         |
6353 --|  Tests in Sample Disposition  -                                                     |
6354 --|  P Lowe 05-Dec-2012	 BUG 15942047 AND 15885923 rework                               |
6355 --|  PM informed dev that "All of the required tests need a result                      |
6356 --|  evaluation in order for the sample disposition to automatically go                 |
6357 --|  to Complete."                                                                      |
6358 --|  In addition if you remove a test result it should go back to In-progress           |
6359 --|  So for the dispositon changes on the Results form, the parameter                   |
6360 --|  'Include Optional Tests in Sample Disposition" has no bearing - only               |
6361 --|  for FINAL Disposition on the change sample form                                    |
6362 --+=====================================================================================+
6363 
6364 PROCEDURE  change_sample_disposition
6365 ( p_sample_id        IN         NUMBER
6366 , x_change_disp_to   OUT NOCOPY VARCHAR2
6367 , x_return_status    OUT NOCOPY VARCHAR2
6368 , x_message_data     OUT NOCOPY VARCHAR2
6369 ) IS
6370 
6371   -- Local Variables
6372   l_count                         NUMBER;
6373   l_incomplete_count_from_spec    NUMBER := 0;
6374   l_incomplete_count_additional   NUMBER := 0;
6375   l_change_disp_to                VARCHAR2(4);
6376   l_qlty_config_present           NUMBER;    --RLNAGARA B5106039
6377 
6378 
6379   l_sample_in                     GMD_SAMPLES%ROWTYPE;
6380   l_sample                        GMD_SAMPLES%ROWTYPE;
6381   l_sampling_event_in             GMD_SAMPLING_EVENTS%ROWTYPE;
6382   l_sampling_event                GMD_SAMPLING_EVENTS%ROWTYPE;
6383   l_event_spec_disp_in            GMD_EVENT_SPEC_DISP%ROWTYPE;
6384   l_event_spec_disp               GMD_EVENT_SPEC_DISP%ROWTYPE;
6385   l_sample_spec_disp_in           GMD_SAMPLE_SPEC_DISP%ROWTYPE;
6386   l_sample_spec_disp              GMD_SAMPLE_SPEC_DISP%ROWTYPE;
6387   l_update_disp_rec               GMD_SAMPLES_GRP.update_disp_rec;
6388 
6389   -- Exceptions
6390   e_sample_fetch_error            EXCEPTION;
6391   e_sampling_event_fetch_error    EXCEPTION;
6392   e_sample_spec_disp_fetch_error  EXCEPTION;
6393   e_event_spec_disp_fetch_error   EXCEPTION;
6394 
6395 BEGIN
6396 
6397   IF (l_debug = 'Y') THEN
6398     gmd_debug.put_line('Entering procedure CHANGE_SAMPLE_DISPOSITION');
6399   END IF;
6400 
6401   --  Initialize API return status to success
6402   x_return_status := FND_API.G_RET_STS_SUCCESS;
6403 
6404   -- Get the sample record
6405   l_sample_in.sample_id := p_sample_id;
6406   IF NOT (GMD_SAMPLES_PVT.fetch_row(
6407                    p_samples => l_sample_in,
6408                    x_samples => l_sample)
6409          )
6410   THEN
6411     -- Fetch Error.
6412     RAISE e_sample_fetch_error;
6413   END IF;
6414 
6415   -- Get the sampling event record
6416   l_sampling_event_in.sampling_event_id := l_sample.sampling_event_id;
6417   IF NOT (GMD_SAMPLING_EVENTS_PVT.fetch_row(
6418                    p_sampling_events => l_sampling_event_in,
6419                    x_sampling_events => l_sampling_event)
6420          )
6421   THEN
6422     -- Fetch Error.
6423     RAISE e_sampling_event_fetch_error;
6424   END IF;
6425 
6426   -- Check if we already have a record in GMD_EVENT_SPEC_DISP
6427   l_event_spec_disp_in.event_spec_disp_id := get_current_event_spec_disp_id(
6428                                l_sampling_event.sampling_event_id);
6429   -- Get the event spec disp record
6430   IF NOT (GMD_EVENT_SPEC_DISP_PVT.fetch_row(
6431                    p_event_spec_disp => l_event_spec_disp_in,
6432                    x_event_spec_disp => l_event_spec_disp)
6433          )
6434   THEN
6435     -- Fetch Error.
6436     RAISE e_event_spec_disp_fetch_error;
6437   END IF;
6438 
6439   -- Get the sample spec record
6440   l_sample_spec_disp_in.event_spec_disp_id := l_event_spec_disp.event_spec_disp_id;
6441   l_sample_spec_disp_in.sample_id          := l_sample.sample_id;
6442   IF NOT (GMD_SAMPLE_SPEC_DISP_PVT.fetch_row(
6443                    p_sample_spec_disp => l_sample_spec_disp_in,
6444                    x_sample_spec_disp => l_sample_spec_disp)
6445          )
6446   THEN
6447     -- Fetch Error.
6448     RAISE e_sample_spec_disp_fetch_error;
6449   END IF;
6450 
6451   -- =============================================================
6452   -- If all tests are cancelled (or marked for purge), change
6453   --   to PENDING.  (Change sample_spec_disp only, not event_spec.)
6454   -- This rule is applied regardless of if there is a spec for the event.
6455   -- =============================================================
6456 
6457   -- Select COUNT of tests that have result with valid evaluation
6458   SELECT count(1)
6459   INTO   l_count
6460   FROM   gmd_spec_results sr, gmd_results r
6461   WHERE  sr.event_spec_disp_id = l_event_spec_disp.event_spec_disp_id
6462   AND    sr.result_id          = r.result_id
6463   AND    r.sample_id           = l_sample.sample_id
6464   AND    (((r.result_value_num IS NOT NULL OR r.result_value_char IS NOT NULL) AND
6465           nvl(sr.evaluation_ind, 'XX') not in ( '4C')) OR
6466           (r.result_value_num IS NULL AND r.result_value_char IS NULL AND
6467           sr.evaluation_ind = '1Z'))
6468 
6469   AND    r.delete_mark         = 0
6470   ;
6471 
6472   IF (l_debug = 'Y') THEN
6473      gmd_debug.put_line ('  COUNT of tests that have result with valid evaluation: '  || l_count);
6474   END IF;
6475 
6476 
6477   IF l_count = 0  THEN
6478     -- There is no valid result on Sample, Set the disposition to 'Pending'
6479     l_change_disp_to := '1P';
6480   ELSE
6481     -- We have got some result so sample is 'In Progress'
6482     l_change_disp_to := '2I';
6483 
6484     -- =============================================================
6485     -- Check if this sample is COMPLETE.
6486     -- If all required tests and additional tests not marked as cancel
6487     --  or void have a value AND there is a spec, and all have an
6488     --  evaluation (other than 3E), then change sample_spec_Disp to COMPLETE.
6489     --
6490     -- If all samples are now complete or higher, then change event too.
6491     --
6492     -- If control_lot_attrib is ON, then check if all evaluations are
6493     --  "Accept".  If so, change disposition to Accept. Otherwise REJECT
6494     --  Change lot status.
6495     --
6496     -- =============================================================
6497 
6498     -- 1st select tests from spec (where sr.additional_test_ind  is null)
6499 
6500     IF l_event_spec_disp.spec_id is NOT NULL THEN
6501       -- Sample has a Spec
6502 --RLNAGARA B5106039 start
6503 --RLNAGARA B5106039 Added the below select stmt. Here I am checking whether the quality parameters has been defined for the sample organization
6504       SELECT count(1) INTO l_qlty_config_present
6505       FROM gmd_quality_config
6506       WHERE organization_id = l_sample.organization_id;
6507 
6508       IF l_qlty_config_present = 0 THEN --IF the process parameters for the sample org is not defined then consider only the non-optional test
6509 
6510 	      -- Select COUNT of Incomplete Tests in Sample WITH Spec
6511 	      SELECT count(1)
6512 	      INTO   l_incomplete_count_from_spec
6513 	      FROM   gmd_results r, gmd_spec_tests st, gmd_spec_results sr
6514 	      WHERE  sr.event_spec_disp_id = l_event_spec_disp.event_spec_disp_id
6515 	      AND    sr.result_id          = r.result_id
6516 	      AND    r.sample_id           = l_sample.sample_id
6517 	      AND    st.spec_id            = l_event_spec_disp.spec_id
6518 	      AND    st.test_id            = r.test_id
6519 	      AND    st.exclude_ind IS NULL
6520 	      AND    st.optional_ind IS NULL             -- Check only REQUIRED tests
6521 	      AND    (sr.evaluation_ind is NULL          -- No Evaluation --> Incomplete
6522 	               OR sr.evaluation_ind = '3E'       -- Evaluation is 'Exp Error' --> Incomplete
6523 	               OR ( r.result_value_num    IS NULL
6524 	                    AND r.result_value_char   IS NULL
6525 	                    AND sr.evaluation_ind not in ('4C', '5O', '1Z')  -- Result not entered
6526 	                  )
6527 	             )
6528 	      AND  r.delete_mark         = 0              -- Check only active ones
6529 	      ;
6530        ELSE --IF the process parameters for the sample org is defined then consider the tests based on the vlaue of include_optional_test_rslt_ind checkbox
6531 	      /* SELECT count(1)
6532 	      INTO   l_incomplete_count_from_spec
6533 	      FROM   gmd_results r, gmd_spec_tests st, gmd_spec_results sr,gmd_quality_config gc
6534 	      WHERE  sr.event_spec_disp_id = l_event_spec_disp.event_spec_disp_id
6535 	      AND    sr.result_id          = r.result_id
6536 	      AND    r.sample_id           = l_sample.sample_id
6537 	      AND    st.spec_id            = l_event_spec_disp.spec_id
6538 	      AND    st.test_id            = r.test_id
6539 	      AND    st.exclude_ind IS NULL
6540 	      AND    gc.organization_id = l_sample.organization_id
6541 	      AND   (
6542 	             ( (gc.include_optional_test_rslt_ind IS NULL OR gc.include_optional_test_rslt_ind='N') and st.optional_ind IS NULL)  OR
6543 	              (gc.include_optional_test_rslt_ind ='Y'
6544 	               and st.optional_ind = 'Y' and (r.result_value_num IS NOT NULL OR r.result_value_char IS NOT NULL)  -- 14349136  added this line
6545 	              )
6546 
6547 	            )
6548 	      AND    (sr.evaluation_ind is NULL          -- No Evaluation --> Incomplete
6549 	               OR sr.evaluation_ind = '3E'       -- Evaluation is 'Exp Error' --> Incomplete
6550 	               OR ( r.result_value_num    IS NULL
6551 	                    AND r.result_value_char   IS NULL
6552 	                    AND sr.evaluation_ind not in ('4C', '5O', '1Z')  -- Result not entered
6553 	                  )
6554 	             )
6555 	      AND  r.delete_mark         = 0  ;            -- Check only active ones
6556 
6557 
6558 
6559 	      SELECT count(1)
6560         INTO   l_incomplete_count_from_spec
6561         FROM   gmd_results r, gmd_spec_tests st, gmd_spec_results sr,gmd_quality_config gc
6562         WHERE  sr.event_spec_disp_id = l_event_spec_disp.event_spec_disp_id
6563         AND    sr.result_id          = r.result_id
6564         AND    r.sample_id           = l_sample.sample_id
6565         AND    st.spec_id            = l_event_spec_disp.spec_id
6566         AND    st.test_id            = r.test_id
6567         AND    st.exclude_ind IS NULL
6568         AND    gc.organization_id = l_sample.organization_id
6569         AND   (
6570                ( (gc.include_optional_test_rslt_ind IS NULL OR  gc.include_optional_test_rslt_ind='N') and st.optional_ind IS NULL)  OR
6571                 (gc.include_optional_test_rslt_ind ='Y'
6572                  and ((st.optional_ind = 'Y' and (r.result_value_num IS NOT NULL OR r.result_value_char IS NOT NULL))
6573                         OR (NVL(st.optional_ind, 'N') = 'N' and r.result_value_num IS NULL AND r.result_value_char IS NULL)  -- 14349136  added this line  reworked for 15885923
6574                 ))
6575               )
6576         AND (sr.evaluation_ind is NULL
6577                  OR sr.evaluation_ind = '3E'
6578 
6579                  OR ( r.result_value_num    IS NULL
6580                       AND r.result_value_char   IS NULL
6581                       AND sr.evaluation_ind not in ('4C', '5O', '1Z')  --  Result not entered
6582                     )
6583                )
6584         AND  r.delete_mark         = 0 ;           -- Check only active ones
6585 
6586 	      */
6587 
6588 	       --  replace above with below for bug 15885923 rework and bug 15942047
6589 
6590 	      SELECT count(1)
6591         INTO l_incomplete_count_from_spec
6592         FROM   gmd_results r, gmd_spec_tests st, gmd_spec_results sr,gmd_quality_config gc
6593         WHERE  sr.event_spec_disp_id = l_event_spec_disp.event_spec_disp_id
6594                      AND    sr.result_id          = r.result_id
6595                      AND    r.sample_id           = l_sample.sample_id
6596                      AND    st.spec_id            = l_event_spec_disp.spec_id
6597                      AND    st.test_id            = r.test_id
6598                      AND    st.exclude_ind IS NULL
6599                      AND    gc.organization_id =  l_sample.organization_id
6600                      AND NVL(st.optional_ind, 'N') = 'N'    -- Check only REQUIRED tests
6601                      --  15885923  new bug 15942047 rework
6602                      AND
6603 			               (sr.evaluation_ind is NULL       -- No Evaluation --> Incomplete
6604 			                 OR sr.evaluation_ind = '3E'    -- Evaluation is 'Exp Error' --> Incomplete
6605 			                 OR ( r.result_value_num    IS NULL
6606 			                      AND r.result_value_char   IS NULL
6607 			                      AND sr.evaluation_ind not in ('4C', '5O', '1Z')  --  Result not entered
6608 			                    )
6609 			               )
6610                      AND  r.delete_mark         = 0 ;
6611 
6612 
6613 
6614        END IF;  -- l_qlty_config_present = 0
6615 --RLNAGARA B5106039 end
6616     END IF;
6617 
6618     IF l_incomplete_count_from_spec = 0 THEN
6619       -- Try to select COUNT of Incomplete Tests in Sample WITHOUT Spec
6620       SELECT count(1)
6621       INTO   l_incomplete_count_additional
6622       FROM   gmd_results r, gmd_spec_results sr
6623       WHERE  sr.result_id          = r.result_id
6624       AND    sr.event_spec_disp_id = l_event_spec_disp.event_spec_disp_id
6625       AND    r.sample_id           = l_sample.sample_id
6626       AND    sr.additional_test_ind = 'Y'
6627       AND    (sr.evaluation_ind  is NULL
6628                OR sr.evaluation_ind = '3E'
6629                OR ( r.result_value_num    IS NULL
6630                     AND r.result_value_char   IS NULL
6631                     AND sr.evaluation_ind not in ('4C', '5O')
6632                   )
6633              )
6634       AND    r.delete_mark         = 0
6635       ;
6636     END IF;     -- end if all tests from spec are complete, then check ad hoc
6637 
6638     IF (l_debug = 'Y') THEN
6639       gmd_debug.put_line ('    COUNT of Incomplete Tests in Sample WITH Spec: '
6640                        || l_incomplete_count_from_spec);
6641       gmd_debug.put_line ('    COUNT of Incomplete Tests in Sample WITHOUT Spec: '
6642                        || l_incomplete_count_additional);
6643 
6644     END IF;
6645 
6646     IF  l_incomplete_count_from_spec + l_incomplete_count_additional = 0  THEN
6647 
6648       -- This means all the results have been entered so set the
6649       -- disposition to 'Complete'
6650       l_change_disp_to := '3C';
6651 
6652     END IF; -- Sample is Complete
6653 
6654     x_change_disp_to := l_change_disp_to;
6655 
6656   END IF;  -- Sample Disposition is Determined
6657 
6658   IF (l_debug = 'Y') THEN
6659      gmd_debug.put_line ('  Old disposition: ' || l_sample_spec_disp.disposition);
6660      gmd_debug.put_line ('  New disposition: ' || l_change_disp_to);
6661   END IF;
6662 
6663   -- See if the sample disposition is changing from what it was.
6664   IF (l_sample_spec_disp.disposition <> l_change_disp_to) THEN
6665 
6666     -- This means that we are changing the disposition of the sample
6667 
6668     -- =======================================================
6669     -- So change the disposition of
6670     -- 1. sample_spec_disp
6671     -- 2. sampling_events (only if one sample required, OR atleast sample_req_cnt are Complete)
6672     -- 3. event_spec_disp (only if one sample required, OR atleast sample_req_cnt are Complete)
6673     -- by calling GMD_SAMPLES_GRP.update_sample_comp_disp
6674     -- =======================================================
6675 
6676     -- Prepare IN parameter
6677     l_update_disp_rec.sample_id               := l_sample.sample_id;
6678     l_update_disp_rec.event_spec_disp_id      := l_event_spec_disp.event_spec_disp_id;
6679     -- l_update_disp_rec.called_from_results     := p_disp_out_rec.called_from_results;
6680     l_update_disp_rec.no_of_samples_for_event := l_sampling_event.sample_req_cnt;
6681     -- MAHESH
6682     l_update_disp_rec.curr_disposition 	      := l_sample_spec_disp.disposition;
6683 
6684 
6685 
6686     IF (l_debug = 'Y') THEN
6687        gmd_debug.put_line ('  Call GMD_SAMPLES_GRP.update_sample_comp_disp '|| l_sample_spec_disp.disposition);
6688     END IF;
6689 
6690     GMD_SAMPLES_GRP.update_sample_comp_disp(
6691                  p_update_disp_rec => l_update_disp_rec
6692                , p_to_disposition  => l_change_disp_to
6693                , x_return_status   => x_return_status
6694                , x_message_data    => x_message_data);
6695   END IF;
6696 
6697   IF (l_debug = 'Y') THEN
6698      gmd_debug.put_line ('Leaving Procedure: CHANGE_SAMPLE_DISPOSITION ' );
6699   END IF;
6700 
6701 EXCEPTION
6702   WHEN    FND_API.G_EXC_ERROR
6703        OR e_sample_fetch_error
6704        OR e_sampling_event_fetch_error
6705        OR e_sample_spec_disp_fetch_error
6706        OR e_event_spec_disp_fetch_error
6707   THEN
6708     x_return_status := FND_API.G_RET_STS_ERROR ;
6709   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6710     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6711   WHEN OTHERS THEN
6712     GMD_API_PUB.Log_Message('GMD_API_ERROR',
6713                             'PACKAGE','change_sample_disposition','ERROR', SUBSTR(SQLERRM,1,100));
6714     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6715 
6716 END change_sample_disposition;
6717 
6718 
6719 
6720 
6721 
6722 
6723 --+========================================================================+
6724 --|NAME change_disp_for_auto_lot
6725 --|
6726 --|DESCRIPTION
6727 --|  Called from a subscrition to the Event - Complete Sample
6728 --|
6729 --|  NOTE: Only called if:
6730 --|        1. Sample has Spec
6731 --|        2. Sample is 'COMPLETE'
6732 --|
6733 --|    If all tests have eval of Accept, and there is a spec, and control_lot_attrib
6734 --|          is on, change disposition to Accept.
6735 --|    If all tests are Accept or Accept w/Variance, change disp to Accept w/var.
6736 --|
6737 --|HISTORY
6738 --|  RLNAGARA 07-Feb-2006 Bug4918820 Modified the cursor c_sample_dtl
6739 --|  RLNAGARA 01-Aug-2006 Bug5416103 Modified the procedure so as to
6740 --|                       check for control_lot_attrib_ind only while
6741 --|                       changing the lot status instead of while changing
6742 --|                       the disposition of a sample.
6743 --|  Supriya Malluru 04-Oct-2007 Bug6439776. Included NVL() for l_config_opt.
6744 --| P Lowe   21-Jun-2010:  BUG 9668224 - In case where ct has all optional
6745 --| tests ONLY and when the parameter Consider Optional Test Results is
6746 --|  un-checked disposition should stay at 'Complete'                        |
6747 --+========================================================================+
6748 
6749 PROCEDURE change_disp_for_auto_lot
6750 ( p_sample_id           IN         NUMBER
6751 , x_change_disp_to      OUT NOCOPY VARCHAR2
6752 , x_return_status       OUT NOCOPY VARCHAR2
6753 ) IS
6754 
6755 --RLNAGARA Bug # 4918820 Changed the view from gmd_all_spec_vrs to gmd_com_spec_vrs_vl
6756 -- Cursor
6757   CURSOR c_sample_dtl (p_sample_id NUMBER) IS
6758   SELECT se.sampling_event_id,
6759          se.sample_active_cnt,
6760          se.sample_req_cnt,
6761          esd.event_spec_disp_id,
6762          esd.spec_id,
6763          esd.spec_vr_id,
6764          ssd.disposition,
6765          svr.control_lot_attrib_ind,
6766          svr.in_spec_lot_status_id,
6767          svr.out_of_spec_lot_status_id,
6768          s.organization_id
6769   FROM   gmd_sampling_events se,
6770          gmd_event_spec_disp esd,
6771          gmd_sample_spec_disp ssd,
6772          gmd_samples s,
6773          gmd_com_spec_vrs_vl svr
6774   WHERE  s.sample_id = p_sample_id
6775   AND    s.sampling_event_id = se.sampling_event_id
6776   AND    se.sampling_event_id = esd.sampling_event_id
6777   AND    esd.spec_used_for_lot_attrib_ind = 'Y'
6778   AND    esd.event_spec_disp_id = ssd.event_spec_disp_id
6779   AND    ssd.sample_id = s.sample_id
6780   AND    svr.spec_vr_id = esd.spec_vr_id
6781   AND    s.delete_mark = 0
6782   AND    esd.delete_mark = 0
6783   AND    ssd.delete_mark = 0
6784   ;
6785 
6786   l_sample_rec           c_sample_dtl%ROWTYPE;
6787   l_update_disp_rec      GMD_SAMPLES_GRP.update_disp_rec;
6788 
6789   l_change_disp_to       VARCHAR2(4);
6790   l_disposition          VARCHAR2(4);
6791 
6792   l_req_cnt              NUMBER;
6793   l_lot_disp             VARCHAR2(30);
6794   l_count                NUMBER;
6795   l_lot_status_id        GMD_QUALITY_CONFIG.in_spec_lot_status_id%TYPE;
6796   l_message_data         VARCHAR2(2000);
6797 
6798   l_include_optional     VARCHAR2(3);
6799   l_count_with_spec      NUMBER;
6800   l_count_wo_spec        NUMBER;
6801 
6802   /*====================
6803       BUG#4691545
6804     ====================*/
6805 
6806   CURSOR get_config_opt (p_org_id NUMBER) IS
6807   SELECT include_optional_test_rslt_ind
6808   FROM   gmd_quality_config
6809   WHERE  organization_id = p_org_id;
6810 
6811   l_config_opt           VARCHAR2(1);
6812 
6813   -- 9668224
6814 
6815  L_OPTIONAL_IND       VARCHAR2(1) := null;
6816  L_OPTIONAL_count  NUMBER := 0;
6817  l_count_tests     NUMBER := 0;
6818 
6819  CURSOR  count_optional_tests(p_ssample_id number,
6820                               p_event_spec_disp_id number ) IS
6821   SELECT  st.optional_ind
6822      FROM   gmd_event_spec_disp esd, gmd_results r, gmd_spec_results sr,
6823              gmd_spec_tests_b st
6824       WHERE  esd.event_spec_disp_id = p_event_spec_disp_id
6825       AND    esd.event_spec_disp_id = sr.event_spec_disp_id
6826       AND    sr.result_id           = r.result_id
6827       AND    r.sample_id            = p_ssample_id
6828       AND    r.delete_mark          = 0
6829       AND    sr.delete_mark         = 0
6830       AND    esd.spec_id = st.spec_id
6831       AND    st.test_id = r.test_id;
6832 
6833 
6834 BEGIN
6835   --  Initialize API return status to success
6836   x_return_status := FND_API.G_RET_STS_SUCCESS;
6837 
6838   IF (l_debug = 'Y') THEN
6839     gmd_debug.put_line('Entering procedure: CHANGE_DISP_FOR_AUTO_LOT.');
6840   END IF;
6841 
6842   OPEN c_sample_dtl(p_sample_id);
6843   FETCH c_sample_dtl INTO l_sample_rec;
6844   IF c_sample_dtl%NOTFOUND THEN
6845     CLOSE c_sample_dtl;
6846     GMD_API_PUB.Log_Message('GMD_INVALID_PARAMETERS');
6847     RAISE FND_API.G_EXC_ERROR;
6848   END IF;
6849   CLOSE c_sample_dtl;
6850 
6851   -- ========================================================
6852   -- Check_event is only called if there is a spec.
6853   --  Is control_lot_attrib_ind on?
6854   --  Check this sample.  (Check all samples in events further down.)
6855   -- ========================================================
6856 
6857   -- Is control_lot_attrib_ind on?
6858   --IF  l_sample_rec.control_lot_attrib_ind = 'Y' THEN                -- #1  RLNAGARA Bug5416103
6859 
6860     -- This means that user wants to change lot status automatically
6861     -- to either Accept or Reject.
6862 
6863     /*====================
6864         BUG#4691545
6865       ====================*/
6866 
6867     OPEN get_config_opt (l_sample_rec.organization_id);
6868     FETCH get_config_opt INTO l_config_opt;
6869     IF (get_config_opt%NOTFOUND) THEN
6870        l_config_opt := 'N';
6871     END IF;
6872     CLOSE get_config_opt;
6873     l_include_optional := NVL(l_config_opt,'N'); --Bug#6490789, Bug#6439776
6874 
6875     IF (l_debug = 'Y') THEN
6876       gmd_debug.put_line('Profile GMD: Include Optional Tests in Sample Disposition :' || l_include_optional);
6877     END IF;
6878 
6879     -- If all tests are accept, cancel or void --> Set to Accept.
6880     -- If all tests are accept, accept w/var,
6881     --                        cancel or void   --> Set to Accept w/Variance.
6882     --
6883     -- Note: Accept w/var uses the same lot status as Accept.
6884 
6885     -- Select COUNT of tests that either don't have a
6886     -- result OR are not (Accept, Cancel, or Void)
6887 
6888     -- B2820787 CHANGED FOLLOWING SELECT
6889     -- SELECT count(1)
6890     -- INTO   l_count
6891     -- FROM   gmd_results r, gmd_spec_results sr
6892     -- WHERE  sr.result_id          = r.result_id
6893     -- AND    sr.event_spec_disp_id = l_sample_rec.event_spec_disp_id
6894     -- AND    r.sample_id           = p_sample_id
6895     -- AND    (sr.evaluation_ind IS NULL OR
6896     --         sr.evaluation_ind    NOT IN ('0A', '4C', '5O')
6897     --        )
6898     -- AND    r.delete_mark         = 0
6899     -- ;
6900     -- WITH THE FOLLOWING
6901 
6902     -- B2820787 BEGIN
6903     l_count           := 0;
6904     l_count_with_spec := 0;
6905     l_count_wo_spec   := 0;
6906 
6907     IF l_sample_rec.spec_id IS NOT NULL THEN
6908       -- Sample has a Spec
6909 
6910       -- Select COUNT of Tests with Evaluation other then ACCEPT
6911       SELECT count(1)
6912       INTO   l_count_with_spec
6913       FROM   gmd_event_spec_disp esd, gmd_results r, gmd_spec_results sr,
6914              gmd_spec_tests_b st
6915       WHERE  esd.event_spec_disp_id = l_sample_rec.event_spec_disp_id
6916       AND    esd.event_spec_disp_id = sr.event_spec_disp_id
6917       AND    sr.result_id           = r.result_id
6918       AND    r.sample_id            = p_sample_id
6919       AND    (sr.evaluation_ind IS NULL OR
6920               sr.evaluation_ind    NOT IN ('0A', '4C', '5O', '1Z')
6921              )
6922       AND    r.delete_mark          = 0
6923       AND    sr.delete_mark         = 0
6924       AND    esd.spec_id = st.spec_id
6925       AND    st.test_id = r.test_id
6926       AND    ((l_include_optional = 'N' and st.optional_ind IS NULL) OR
6927               (l_include_optional = 'Y' and (r.result_value_num IS NOT NULL OR
6928 	                                     r.result_value_char IS NOT NULL)
6929               )
6930              )
6931       ;
6932 
6933     END IF;
6934 
6935     IF (l_count_with_spec = 0) THEN
6936 
6937       -- Select COUNT of Tests with Evaluation other then ACCEPT or ACCEPT W/ VAR
6938       SELECT count(1)
6939       INTO   l_count_wo_spec
6940       FROM   gmd_results r, gmd_spec_results sr
6941       WHERE  sr.event_spec_disp_id = l_sample_rec.event_spec_disp_id
6942       AND    sr.result_id           = r.result_id
6943       AND    r.sample_id            = p_sample_id
6944       AND    sr.additional_test_ind = 'Y'
6945       AND    (sr.evaluation_ind IS NULL OR
6946               sr.evaluation_ind    NOT IN ('0A', '4C', '5O')
6947              )
6948       AND    r.delete_mark          = 0
6949       AND    sr.delete_mark         = 0
6950       ;
6951 
6952     END IF;
6953 
6954     l_count := l_count_with_spec + l_count_wo_spec;
6955 
6956     -- B2820787 END
6957 
6958     IF (l_debug = 'Y') THEN
6959       gmd_debug.put_line('   Count of tests which do NOT have accept evaluation w/  SPEC :' || l_count);
6960       gmd_debug.put_line('   Count of tests which do NOT have accept evaluation wo/ SPEC :' || l_count);
6961       gmd_debug.put_line('   Count of tests which do NOT have accept evaluation Total    :' || l_count);
6962     END IF;
6963 
6964      --  9668224  start  In case where ct has all optional tests ONLY and when the parameter Consider Optional Test Results is un-checked
6965       -- disposition should stay at 'Complete'
6966 
6967 
6968 
6969       OPEN  count_optional_tests(p_sample_id, l_sample_rec.event_spec_disp_id );
6970 
6971       LOOP
6972 		    FETCH count_optional_tests into  L_OPTIONAL_IND;
6973 		    EXIT WHEN count_optional_tests%NOTFOUND ; -- exit when last row is fetched
6974 
6975         If L_OPTIONAL_IND = 'Y'  and l_include_optional = 'N' then
6976         	 L_OPTIONAL_count := L_OPTIONAL_count + 1;
6977         end iF;
6978         l_count_tests := l_count_tests + 1;
6979 
6980 
6981 		  END LOOP;
6982 		  CLOSE count_optional_tests;
6983 
6984 		  IF (l_debug = 'Y') THEN
6985       	gmd_debug.put_line('   Total Count of tests: ' || l_count_tests);
6986       	gmd_debug.put_line('   Count of tests which are optional : ' || L_OPTIONAL_count);
6987       END IF;
6988 
6989 		IF l_count_tests <>   L_OPTIONAL_count then -- This means that all test are NOT optional and   parameter Consider Optional Test Results is un-checked  ; only change disp to Accept.   9668224  end
6990 
6991 
6992     IF  l_count = 0  THEN                                -- #2
6993       -- This means that all results are 'Accept' so change
6994       -- the disposition for 'ACCEPT'
6995       l_change_disp_to := '4A';
6996 
6997       -- Set the lot status to In-Spec Lot Status
6998       IF l_sample_rec.control_lot_attrib_ind = 'Y' THEN       --RLNAGARA Bug5416103
6999 				l_lot_status_id := l_sample_rec.in_spec_lot_status_id;
7000       END IF;
7001 
7002     ELSE
7003       -- check Accept AND accept w/ var, cancel and void
7004 
7005       -- B2820787 REPLACED QUERY WITH
7006       -- SELECT COUNT(1)
7007       -- INTO   l_count
7008       -- FROM   gmd_results r, gmd_spec_results sr
7009       -- WHERE  sr.result_id          = r.result_id
7010       -- AND    sr.event_spec_disp_id = l_sample_rec.event_spec_disp_id
7011       -- AND    r.sample_id           = p_sample_id
7012       -- AND    (sr.evaluation_ind IS NULL OR
7013       --         sr.evaluation_ind    NOT IN ('0A', '1V', '4C', '5O')
7014       --        )
7015       -- AND    r.delete_mark         = 0
7016       -- ;
7017       -- THE FOLLOWING
7018 
7019       -- B2820787 BEGIN
7020       l_count           := 0;
7021       l_count_with_spec := 0;
7022       l_count_wo_spec   := 0;
7023 
7024       IF l_sample_rec.spec_id IS NOT NULL THEN
7025         -- Sample has a Spec
7026 
7027         -- Select COUNT of Tests with Evaluation other then ACCEPT
7028         SELECT count(1)
7029         INTO   l_count_with_spec
7030         FROM   gmd_event_spec_disp esd, gmd_results r, gmd_spec_results sr,
7031                gmd_spec_tests_b st
7032         WHERE  esd.event_spec_disp_id = l_sample_rec.event_spec_disp_id
7033         AND    esd.event_spec_disp_id = sr.event_spec_disp_id
7034         AND    sr.result_id           = r.result_id
7035         AND    r.sample_id            = p_sample_id
7036         AND    (sr.evaluation_ind IS NULL OR
7037                 sr.evaluation_ind    NOT IN ('0A', '1V', '4C', '5O', '1Z')
7038                )
7039         AND    r.delete_mark          = 0
7040         AND    sr.delete_mark         = 0
7041         AND    esd.spec_id = st.spec_id
7042         AND    st.test_id = r.test_id
7043         AND    ((l_include_optional = 'N' and st.optional_ind IS NULL) OR
7044                 (l_include_optional = 'Y' and (r.result_value_num IS NOT NULL OR
7045 	                                       r.result_value_char IS NOT NULL)
7046                 )
7047                )
7048         ;
7049 
7050       END IF;
7051 
7052       IF (l_count_with_spec = 0) THEN
7053 
7054         -- Select COUNT of Tests with Evaluation other then ACCEPT or ACCEPT W/ VAR
7055         -- Bug 3763419 -  Added Guaranteed by Manufacturer - 1Z
7056         SELECT count(1)
7057         INTO   l_count_wo_spec
7058         FROM   gmd_results r, gmd_spec_results sr
7059         WHERE  sr.event_spec_disp_id = l_sample_rec.event_spec_disp_id
7060         AND    sr.result_id           = r.result_id
7061         AND    r.sample_id            = p_sample_id
7062         AND    sr.additional_test_ind = 'Y'
7063         AND    (sr.evaluation_ind IS NULL OR
7064                 sr.evaluation_ind    NOT IN ('0A', '1V', '4C', '5O', '1Z')
7065                )
7066         AND    r.delete_mark          = 0
7067         AND    sr.delete_mark         = 0
7068         ;
7069 
7070       END IF;
7071 
7072       l_count := l_count_with_spec + l_count_wo_spec;
7073 
7074       -- B2820787 END
7075 
7076       IF (l_debug = 'Y') THEN
7077         gmd_debug.put_line('   Count of tests which do NOT have accept or accept w/ var evaluation w/  SPEC :' || l_count_with_spec);
7078         gmd_debug.put_line('   Count of tests which do NOT have accept or accept w/ var evaluation wo/ SPEC :' || l_count_wo_spec);
7079         gmd_debug.put_line('   Count of tests which do NOT have accept or accept w/ var evaluation Total    :' || l_count);
7080       END IF;
7081 
7082       IF  l_count = 0  THEN                                -- #2.1
7083         -- This means that all results are either 'Accept' or
7084         -- 'Accept with Variance' so set the Disposition to 'ACCEPT WITH VARIANCE'
7085         l_change_disp_to := '5AV';
7086 
7087         -- Set the lot status to In-Spec Lot Status
7088 						IF l_sample_rec.control_lot_attrib_ind = 'Y' THEN     --RLNAGARA Bug5416103
7089 						  l_lot_status_id := l_sample_rec.in_spec_lot_status_id;
7090 						END IF;
7091       ELSE
7092         -- This means that some result have 'Reject' evaluation so
7093         -- set the Disposition to 'REJECT'
7094         l_change_disp_to := '6RJ';
7095 
7096 				        -- Set the lot status to Out-of-Spec Lot Status
7097 					IF l_sample_rec.control_lot_attrib_ind = 'Y' THEN     --RLNAGARA Bug5416103
7098 					  l_lot_status_id := l_sample_rec.out_of_spec_lot_status_id;
7099 					END IF;
7100       END IF;           -- end if all are accept or accept w/var, cancel, void #2.1
7101 
7102     END IF;      -- end if all tests are accept, cancel, void or MFP    #2
7103 
7104     IF (l_debug = 'Y') THEN
7105       gmd_debug.put_line('   Lot status will be changed to: ' || to_char(l_lot_status_id));
7106     END IF;
7107 
7108     x_change_disp_to := l_change_disp_to;
7109 
7110 
7111 
7112     -- By now, we are setting the Sample Disposition to
7113     -- either of three - 4A, 5AV, 6RJ
7114 
7115     -- =======================================================
7116     -- So change the disposition of
7117     -- 1. sample_spec_disp
7118     -- 2. sampling_events (only if one sample required)
7119     -- 3. event_spec_disp (only if one sample required)
7120     -- by calling GMD_SAMPLES_GRP.update_sample_comp_disp
7121     --
7122     -- If there are more than one sample required then user
7123     -- has to go through composite results path to set the
7124     -- Event Disposition.
7125     -- =======================================================
7126 
7127     -- Prepare IN parameter
7128     l_update_disp_rec.sample_id               := p_sample_id;
7129     l_update_disp_rec.event_spec_disp_id      := l_sample_rec.event_spec_disp_id;
7130     -- l_update_disp_rec.called_from_results     := p_disp_out_rec.called_from_results;
7131     l_update_disp_rec.no_of_samples_for_event := l_sample_rec.sample_req_cnt;
7132      -- MAHESH
7133     l_update_disp_rec.curr_disposition        := l_sample_rec.disposition;
7134 
7135 
7136     IF (l_debug = 'Y') THEN
7137        gmd_debug.put_line ('  Call GMD_SAMPLES_GRP.update_sample_comp_disp '|| l_change_disp_to);
7138     END IF;
7139 
7140     GMD_SAMPLES_GRP.update_sample_comp_disp(
7141                  p_update_disp_rec => l_update_disp_rec
7142                , p_to_disposition  => l_change_disp_to
7143                , x_return_status   => x_return_status
7144                , x_message_data    => l_message_data);
7145     IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7146       GMD_API_PUB.Log_Message(l_message_data);
7147       RAISE FND_API.G_EXC_ERROR;
7148     END IF;
7149 
7150     -- The above procedure has changed the Sample Spec Disposition.
7151     -- It might also change the Sampling Event Disposition if this is the
7152     -- ONLY required sample.
7153 
7154     -- So if the Event is changed, then change the lot status to
7155     -- In-Spec or Out-of-Spec Lot status.
7156 
7157     SELECT disposition
7158     INTO   l_disposition
7159     FROM   gmd_sampling_events
7160     WHERE  sampling_event_id = l_sample_rec.sampling_event_id
7161     ;
7162 
7163     IF l_sample_rec.control_lot_attrib_ind = 'Y' THEN  --RLNAGARA Bug5416103
7164      IF (l_disposition IN ('4A', '5AV', '6RJ')) THEN
7165       change_lot_status(  p_sample_id        => p_sample_id
7166                         , p_organization_id  => l_sample_rec.organization_id
7167                         , p_lot_status       => l_lot_status_id
7168                         , x_return_status    => x_return_status
7169                         );
7170       IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7171         RAISE FND_API.G_EXC_ERROR;
7172       END IF;
7173      END IF; --l_disposition IN...
7174     END IF; -- control_lot_attrib_ind = Y ...
7175 
7176   --END IF;         -- end  if #1  if control_lot_attrib is on
7177 
7178  END IF; --IF l_count_tests <>   L_OPTIONAL_count -- This means that all test are NOT optional and   parameter Consider Optional Test Results is un-checked  ; only change disp to Accept.   9668224  end
7179 
7180 
7181 
7182   IF (l_debug = 'Y') THEN
7183     gmd_debug.put_line('Leaving Procedure: CHANGE_DISP_FOR_AUTO_LOT.');
7184   END IF;
7185 
7186 EXCEPTION
7187   WHEN    FND_API.G_EXC_ERROR THEN
7188     x_return_status := FND_API.G_RET_STS_ERROR ;
7189   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7190     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7191   WHEN OTHERS THEN
7192     GMD_API_PUB.Log_Message('GMD_API_ERROR',
7193                             'PACKAGE','CHANGE_DISP_FOR_AUTO_LOT','ERROR', SUBSTR(SQLERRM,1,100));
7194     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7195 
7196 END  change_disp_for_auto_lot;
7197 
7198 
7199 
7200 --Start of comments
7201 --+========================================================================+
7202 --| API Name    : composite_and_change_lot                                 |
7203 --| TYPE        : Group                                                    |
7204 --| Notes       : This procedure creates a composite results for the given |
7205 --|               sampling event id. It then checks if the Auto Lot Status |
7206 --|               change is required from the Spec. If so, then it will    |
7207 --|               find out the disposition of the composite result and     |
7208 --|               change the lot status based on the disposition           |
7209 --|                                                                        |
7210 --| HISTORY                                                                |
7211 --|    Chetan Nagar     06-Dec-2002     Created.                           |
7212 --|                                                                        |
7213 --|  RLNAGARA 07-Feb-2006 B4918820 Modified the cursor c_spec_dtl
7214 --|  RLNAGARA 01-Aug-2006 Bug5416103 Modified the procedure so as to
7215 --|                       check for control_lot_attrib_ind only while
7216 --|                       changing the lot status instead of while changing
7217 --|                       the disposition of a sample.
7218 --|  Supriya Malluru 04-Oct-2007 Bug6439776. Included NVL() for l_config_opt.
7219 --|  Supriya Malluru 12-Nov-2007 Bug6490789. Assigning Y if l_config_opt is
7220 --|                       Null.
7221 --|  Supriya Malluru 26-Nov-2007 Bug6609341.Reverted the fix of bug 6490789.
7222 --+========================================================================+
7223 -- End of comments
7224 PROCEDURE  composite_and_change_lot
7225 ( p_sampling_event_id IN         NUMBER
7226 , p_commit            IN         VARCHAR2
7227 , x_return_status     OUT NOCOPY VARCHAR2
7228 ) IS
7229 
7230   /*=================================================
7231      BUG#4691545 - Added organization_id to cursor.
7232     =================================================*/
7233 --RLNAGARA Bug 4918820 Changed the view from gmd_all_spec_vrs to gmd_com_spec_vrs_vl
7234   CURSOR c_spec_dtl (p_event_spec_disp_id NUMBER) IS
7235   SELECT esd.disposition,
7236          svr.control_lot_attrib_ind,
7237          svr.in_spec_lot_status_id,
7238          svr.out_of_spec_lot_status_id,
7239          svr.organization_id
7240   FROM   gmd_event_spec_disp esd,
7241          gmd_com_spec_vrs_vl svr,
7242          gmd_composite_spec_disp csd
7243   WHERE  esd.event_spec_disp_id = p_event_spec_disp_id
7244   AND    esd.spec_used_for_lot_attrib_ind = 'Y'
7245   AND    svr.spec_vr_id = esd.spec_vr_id
7246   AND    esd.delete_mark = 0
7247   ;
7248 
7249   CURSOR Cur_quality_config (p_organization_id VARCHAR2) IS
7250   SELECT transaction_reason_id
7251   FROM   gmd_quality_config
7252   WHERE  organization_id = p_organization_id
7253   AND    transaction_reason_id IS NOT NULL;
7254 
7255 
7256   l_sampling_event               GMD_SAMPLING_EVENTS%ROWTYPE;
7257   l_in_sampling_event            GMD_SAMPLING_EVENTS%ROWTYPE;
7258 
7259   l_spec_dtl                     c_spec_dtl%ROWTYPE;
7260   l_sample_ids                   GMD_API_PUB.number_tab;
7261   l_update_disp_rec              GMD_SAMPLES_GRP.update_disp_rec;
7262 
7263   l_event_spec_disp_id           NUMBER;
7264   l_composite_spec_disp_id       NUMBER;
7265   l_composite_exist              VARCHAR2(1);
7266   l_composite_valid              VARCHAR2(1);
7267   l_return_status                VARCHAR2(1);
7268   l_count                        PLS_INTEGER;
7269   l_count_with_spec              PLS_INTEGER;
7270   l_count_wo_spec                PLS_INTEGER;
7271   l_lot_status_id                VARCHAR2(4);
7272   l_change_disp_to               VARCHAR2(4);
7273   l_message_data                 VARCHAR2(2000);
7274   l_organization_id              NUMBER;
7275 
7276   l_include_optional             VARCHAR2(3);
7277   l_reason_id                    gmd_quality_config.transaction_reason_id%TYPE;
7278 
7279 
7280 /*
7281   l_event_spec_disp              GMD_EVENT_SPEC_DISP%ROWTYPE;
7282   l_sample_spec_disp             GMD_SAMPLE_SPEC_DISP%ROWTYPE;
7283   l_results                      GMD_RESULTS%ROWTYPE;
7284   l_spec_results                 GMD_SPEC_RESULTS%ROWTYPE;
7285 
7286   l_in_event_spec_disp           GMD_EVENT_SPEC_DISP%ROWTYPE;
7287   l_out_event_spec_disp          GMD_EVENT_SPEC_DISP%ROWTYPE;
7288   l_out_results                  GMD_RESULTS%ROWTYPE;
7289 */
7290 
7291   -- Exception
7292   e_sampling_event_fetch_error   EXCEPTION;
7293 
7294   /*====================
7295       BUG#4691545
7296     ====================*/
7297 
7298   CURSOR get_config_opt (p_org_id NUMBER) IS
7299   SELECT include_optional_test_rslt_ind
7300   FROM   gmd_quality_config
7301   WHERE  organization_id = p_org_id;
7302 
7303   l_config_opt           VARCHAR2(1);
7304 
7305 BEGIN
7306 
7307   IF (l_debug = 'Y') THEN
7308     gmd_debug.put_line('Entering procedure CHANGE_SAMPLE_DISPOSITION');
7309   END IF;
7310 
7311   --  Initialize API return status to success
7312   x_return_status := FND_API.G_RET_STS_SUCCESS;
7313 
7314   -- Now that we have sampling_event_id, fetch the sampling event record
7315   l_in_sampling_event.sampling_event_id := p_sampling_event_id;
7316   IF NOT (GMD_SAMPLING_EVENTS_PVT.fetch_row(
7317                    p_sampling_events => l_in_sampling_event,
7318                    x_sampling_events => l_sampling_event)
7319          )
7320   THEN
7321     -- Fetch Error.
7322     RAISE e_sampling_event_fetch_error;
7323   END IF;
7324 
7325   -- Composite is done only if there are nore than one active samples.
7326   IF (l_sampling_event.sample_active_cnt > 1) THEN
7327 
7328     l_event_spec_disp_id := get_current_event_spec_disp_id
7329                                (l_sampling_event.sampling_event_id);
7330     IF (l_event_spec_disp_id IS NULL) THEN
7331       GMD_API_PUB.Log_Message('GMD_EVENT_SPEC_NOT_FOUND');
7332       RAISE FND_API.G_EXC_ERROR;
7333     END IF;
7334 
7335     l_composite_exist := 'N';
7336     l_composite_valid := 'N';
7337 
7338     gmd_results_grp.composite_exist(
7339        p_sampling_event_id  => l_sampling_event.sampling_event_id
7340      , p_event_spec_disp_id => NULL
7341      , x_composite_exist    => l_composite_exist
7342      , x_composite_valid    => l_composite_valid
7343      , x_return_status      => l_return_status);
7344 
7345     IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7346        RAISE FND_API.G_EXC_ERROR;
7347     END IF;
7348 
7349     IF ((l_composite_exist = 'N') OR
7350         (l_composite_exist = 'Y' AND l_composite_valid = 'N')) THEN
7351       -- Get all the sample IDs for this Sampling Event
7352       GMD_RESULTS_GRP.get_sample_ids_for_se(l_sampling_event.sampling_event_id,
7353 					    l_sample_ids,
7354 					    l_return_status);
7355       IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7356         RAISE FND_API.G_EXC_ERROR;
7357       END IF;
7358 
7359       -- Populate Session Temp Table with Data Points
7360       GMD_RESULTS_GRP.populate_result_data_points
7361             (p_sample_ids         => l_sample_ids,
7362              p_event_spec_disp_id => l_event_spec_disp_id,
7363              x_return_status      => l_return_status);
7364       IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7365         RAISE FND_API.G_EXC_ERROR;
7366       END IF;
7367 
7368       -- Create new Composite Rows
7369       GMD_RESULTS_GRP.create_composite_rows
7370           (p_event_spec_disp_id => l_event_spec_disp_id,
7371            x_return_Status      => l_return_status);
7372       IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7373         RAISE FND_API.G_EXC_ERROR;
7374       END IF;
7375 
7376       -- At this time the composite results are created with IN/OUT-OF-SPEC flag set.
7377 
7378       OPEN c_spec_dtl(l_event_spec_disp_id);
7379       FETCH c_spec_dtl INTO l_spec_dtl;
7380       CLOSE c_spec_dtl;
7381 
7382       --IF (l_spec_dtl.control_lot_attrib_ind = 'Y') THEN   --RLNAGARA Bug 5416103
7383 
7384         -- The Auto Lot Status is ON so we need to change the
7385         -- disposition of the Composite Result to 'Accept' OR 'Reject'
7386 
7387         -- How do we do that, you might ask?
7388         -- Simple. If even a single result is OUT-OF-SPEC then the composite result
7389         -- is OUT-OF-SPEC else it is IN-SPEC.
7390 
7391     /*====================
7392         BUG#4691545
7393       ====================*/
7394 
7395     OPEN get_config_opt (l_spec_dtl.organization_id);
7396     FETCH get_config_opt INTO l_config_opt;
7397     IF (get_config_opt%NOTFOUND) THEN
7398        l_config_opt := 'N';
7399     END IF;
7400     CLOSE get_config_opt;
7401     l_include_optional := NVL(l_config_opt,'N'); --Bug#6490789,Bug#6439776
7402 
7403         IF (l_debug = 'Y') THEN
7404           gmd_debug.put_line('Profile GMD: Include Optional Tests in Sample Disposition :' || l_include_optional);
7405         END IF;
7406 
7407         -- B2820787 START
7408         -- REPLACED FOLLOWING WITH
7409         -- SELECT count(1)
7410         -- INTO   l_count
7411         -- FROM   gmd_composite_spec_disp csd,
7412         --        gmd_composite_results cr
7413         -- WHERE  csd.event_spec_disp_id = l_event_spec_disp_id
7414         -- AND    csd.latest_ind = 'Y'
7415         -- AND    csd.composite_spec_disp_id = cr.composite_spec_disp_id
7416         -- AND    cr.in_spec_ind IS NULL                  -- Result is out-of-spec
7417         -- ;
7418         -- THE FOLLOWING
7419 
7420         SELECT count(1)
7421         INTO   l_count_with_spec
7422         FROM   gmd_composite_spec_disp csd, gmd_event_spec_disp esd,
7423                gmd_composite_results cr, gmd_spec_tests_b st
7424         WHERE  csd.event_spec_disp_id = l_event_spec_disp_id
7425         AND    csd.latest_ind = 'Y'
7426         AND    csd.event_spec_disp_id = esd.event_spec_disp_id
7427         AND    csd.composite_spec_disp_id = cr.composite_spec_disp_id
7428         AND    cr.in_spec_ind IS NULL   -- Result is out-of-spec
7429         AND    st.spec_id = esd.spec_id
7430         AND    st.test_id = cr.test_id
7431         AND    ((l_include_optional = 'N' and st.optional_ind IS NULL) OR
7432                 (l_include_optional = 'Y' and (cr.mean IS NOT NULL or cr.mode_char IS NOT NULL))
7433                )
7434         ;
7435 
7436         SELECT count(1)
7437         INTO   l_count_wo_spec
7438         FROM   gmd_composite_spec_disp csd, gmd_event_spec_disp esd,
7439                gmd_composite_results cr
7440         WHERE  csd.event_spec_disp_id = l_event_spec_disp_id
7441         AND    csd.latest_ind = 'Y'
7442         AND    csd.event_spec_disp_id = esd.event_spec_disp_id
7443         AND    csd.composite_spec_disp_id = cr.composite_spec_disp_id
7444         AND    cr.in_spec_ind IS NULL                  -- Result is out-of-spec
7445         AND    cr.test_id NOT IN
7446                (SELECT st.test_id
7447                 FROM   gmd_spec_tests_b st
7448                 WHERE  st.spec_id = esd.spec_id)
7449         ;
7450 
7451         l_count := l_count_with_spec + l_count_wo_spec;
7452 
7453         -- B2820787 END
7454 
7455         IF (l_count = 0) THEN
7456           -- Everything is IN-SPEC so the lot is changed to In-Spec Lot Status
7457 	  IF (l_spec_dtl.control_lot_attrib_ind = 'Y') THEN   --RLNAGARA Bug5416103
7458 	      l_lot_status_id := l_spec_dtl.in_spec_lot_status_id;
7459 	  END IF;
7460 
7461           -- And the Disposition is changed to 'Accept
7462           l_change_disp_to := '4A';
7463         ELSE
7464           -- Something is OUT-OF-SPEC so the lot is changed to Out-of-Spec Lot Status
7465 	  IF (l_spec_dtl.control_lot_attrib_ind = 'Y') THEN   --RLNAGARA Bug5416103
7466 	     l_lot_status_id := l_spec_dtl.out_of_spec_lot_status_id;
7467 	  END IF;
7468 
7469           -- And the Disposition is changed to 'Reject'
7470           l_change_disp_to := '6RJ';
7471         END IF;
7472 
7473         SELECT composite_spec_disp_id
7474         INTO   l_composite_spec_disp_id
7475         FROM   gmd_composite_spec_disp csd
7476         WHERE  csd.event_spec_disp_id = l_event_spec_disp_id
7477         AND    csd.latest_ind = 'Y'
7478         ;
7479 
7480         -- Change the dispostion of the Event
7481         -- Prepare IN parameter
7482         l_update_disp_rec.composite_spec_disp_id  := l_composite_spec_disp_id;
7483         l_update_disp_rec.event_spec_disp_id      := l_event_spec_disp_id;
7484         l_update_disp_rec.no_of_samples_for_event := l_sampling_event.sample_req_cnt;
7485         l_update_disp_rec.sampling_event_id       := l_sampling_event.sampling_event_id;
7486         l_update_disp_rec.curr_disposition        := l_spec_dtl.disposition;
7487 
7488         IF (l_debug = 'Y') THEN
7489            gmd_debug.put_line ('  Call GMD_SAMPLES_GRP.update_sample_comp_disp '|| l_change_disp_to);
7490         END IF;
7491 
7492         GMD_SAMPLES_GRP.update_sample_comp_disp(
7493                      p_update_disp_rec => l_update_disp_rec
7494                    , p_to_disposition  => l_change_disp_to
7495                    , x_return_status   => l_return_status
7496                    , x_message_data    => l_message_data);
7497         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7498           GMD_API_PUB.Log_Message(l_message_data);
7499           RAISE FND_API.G_EXC_ERROR;
7500         END IF;
7501 
7502         -- Disposition has been changed so now change the Lot Status in Inventory
7503 	IF (l_spec_dtl.control_lot_attrib_ind = 'Y') THEN   --RLNAGARA Bug5416103
7504          IF  l_lot_status_id IS NOT NULL  THEN
7505 
7506           -- Get the orgn_code for the sampling event
7507           SELECT organization_id
7508           INTO   l_organization_id
7509           FROM   gmd_samples
7510           WHERE  sampling_event_id = l_sampling_event.sampling_event_id
7511           AND    rownum = 1 ;
7512 
7513           -- Get the reason code from quality configuration/parameters
7514           OPEN  Cur_quality_config(l_organization_id);
7515           FETCH Cur_quality_config INTO l_reason_id ;
7516           IF Cur_quality_config%NOTFOUND THEN
7517             CLOSE Cur_quality_config;
7518             GMD_API_PUB.Log_Message('GMD_QM_INV_REASON_CODE');
7519             RAISE FND_API.G_EXC_ERROR;
7520           END IF;
7521           CLOSE Cur_quality_config;
7522 
7523 --rconv
7524 
7525           gmd_samples_grp.update_lot_grade_batch(       --nsrivast
7526                   p_sample_id           => NULL
7527                 , p_composite_spec_disp_id  => l_composite_spec_disp_id
7528                 , p_to_lot_status_id       => l_lot_status_id
7529                 , p_from_lot_status_id       => NULL --l_lot_status_id
7530                 , p_to_grade_code            => NULL
7531                 , p_from_grade_code          => NULL
7532                 , p_to_qc_status        => NULL
7533                 , p_reason_id           => l_reason_id
7534                 , p_hold_date           => NULL
7535                 , x_return_status       => x_return_status
7536                 , x_message_data        => l_message_data );
7537 
7538 
7539           IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7540             GMD_API_PUB.Log_Message(l_message_data);
7541             RAISE FND_API.G_EXC_ERROR;
7542           END IF;
7543 
7544         END IF; -- l_lot_status IS NOT NULL
7545 
7546       END IF; -- l_spec_dtl.control_lot_attrib_ind = 'Y'
7547 
7548     END IF;  -- Need to create composite
7549 
7550     -- Check of p_commit.
7551     IF FND_API.to_boolean(p_commit)
7552     THEN
7553       COMMIT WORK;
7554     END IF;
7555 
7556   END IF;  -- l_sampling_event.active_sample_cnt > 1
7557 
7558 
7559 
7560 EXCEPTION
7561   WHEN FND_API.G_EXC_ERROR OR
7562        e_sampling_event_fetch_error
7563   THEN
7564     x_return_status := FND_API.G_RET_STS_ERROR ;
7565   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7566     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7567   WHEN OTHERS THEN
7568     GMD_API_PUB.Log_Message('GMD_API_ERROR',
7569                             'PACKAGE','change_sample_disposition','ERROR', SUBSTR(SQLERRM,1,100));
7570     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7571 END composite_and_change_lot;
7572 
7573 PROCEDURE update_exptest_value_null
7574 (p_exp_ref_test_id  IN gmd_qc_tests_b.test_id%TYPE
7575 , p_sample_id IN gmd_samples.sample_id%TYPE
7576 , p_event_spec_disp_id IN gmd_sample_spec_disp.event_spec_disp_id%TYPE
7577 , x_return_status     OUT NOCOPY VARCHAR2
7578 )
7579 --Start of comments
7580 --+========================================================================+
7581 --| API Name    : update_exptest_value_null                                |
7582 --| TYPE        : Group                                                    |
7583 --| Notes       : This procedure takes the sample information and          |
7584 --|               test_id, and updates the result of latest replicates     |
7585 --|               of all the Expression tests of the current sample,       |
7586 --|               which use the given test as dependent test, to NULL.     |              |
7587 --|                                                                        |
7588 --| HISTORY                                                                |
7589 --|    Ravi Boddu     31-Dec-2004       Created.                           |
7590 --|                                                                        |
7591 --+========================================================================+
7592 -- End of comments
7593 IS
7594     l_rslt_tbl gmd_results_grp.rslt_tbl;
7595     l_return_status VARCHAR2(10);
7596     l_sts BOOLEAN;
7597     l_start_date  DATE := GMA_CORE_PKG.get_date_constant_d('SY$MIN_DATE');
7598 
7599    CURSOR get_exp_tests (l_exp_ref_test_id gmd_qc_tests_b.test_id%TYPE) IS
7600    SELECT DISTINCT test_id
7601    FROM gmd_qc_test_values_b val
7602    WHERE  expression_ref_test_id = l_exp_ref_test_id ;
7603 
7604    CURSOR exp_tests_need_calc ( l_sample_id gmd_samples.sample_id%TYPE,
7605                              l_test_id gmd_qc_tests_b.test_id%TYPE,
7606                              l_event_spec_disp_id gmd_sample_spec_disp.event_spec_disp_id%TYPE)
7607    IS
7608     SELECT r.result_id
7609     FROM   gmd_results r, gmd_spec_results sr
7610     WHERE  r.result_id = sr.result_id
7611     AND    r.sample_id = l_sample_id
7612     AND    sr.event_spec_disp_id = l_event_spec_disp_id
7613     AND    NVL(sr.evaluation_ind, 'XX')  NOT IN ('50' ,'4C')
7614     AND    sr.delete_mark = 0
7615     AND    r.delete_mark = 0
7616     AND    r.test_id = l_test_id
7617     ORDER BY NVL(r.result_date,l_start_date) DESC , r.test_replicate_cnt DESC;
7618    l_result_id           NUMBER(15);
7619 
7620   BEGIN
7621 
7622     gmd_debug.put_line('Entering procedure update_exptest_value_null');
7623 
7624     --Initialize API return status to success
7625     x_return_status := FND_API.G_RET_STS_SUCCESS;
7626 
7627     FOR test in get_exp_tests(p_exp_ref_test_id)
7628     LOOP
7629       OPEN exp_tests_need_calc(p_sample_id, test.test_id,p_event_spec_disp_id);
7630       FETCH exp_tests_need_calc INTO l_result_id;
7631       CLOSE exp_tests_need_calc;
7632       IF l_result_id IS NOT NULL THEN
7633         UPDATE gmd_results SET result_value_num = NULL WHERE result_id = l_result_id;
7634       END IF;
7635     END LOOP;
7636 
7637   EXCEPTION
7638   WHEN OTHERS THEN
7639     GMD_API_PUB.Log_Message('GMD_API_ERROR','PACKAGE','update_exptest_value_null','ERROR', SUBSTR(SQLERRM,1,100));
7640     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7641 
7642   END update_exptest_value_null;
7643 
7644 PROCEDURE populate_result_data_points_r   -- 14021970    new procedure
7645 (
7646   p_sample_ids         IN GMD_API_PUB.number_tab
7647 , p_event_spec_disp_id IN NUMBER
7648 , p_test_replicate_cnt IN NUMBER
7649 , x_return_status      OUT NOCOPY VARCHAR2
7650 )
7651 IS
7652 
7653   -- Local Variables
7654   l_sql_stmt           VARCHAR2(2000);
7655   l_start_date         DATE;
7656 
7657 BEGIN
7658 
7659   IF (l_debug = 'Y') THEN
7660      gmd_debug.put_line('Entering Procedure - POPULATE_RESULT_DATA_POINTS_R');
7661   END IF;
7662 
7663   --  Initialize API return status to success
7664   x_return_status := FND_API.G_RET_STS_SUCCESS;
7665   l_start_date  := GMA_CORE_PKG.get_date_constant_d('SY$MIN_DATE');
7666 -- Bug #2626977 (JKB) Removed reference to date profile above.
7667 
7668   EXECUTE IMMEDIATE 'delete from gmd_result_data_points_gt';
7669   l_sql_stmt :=
7670     'INSERT INTO gmd_result_data_points_gt (result_id, test_id, exclude_ind,  data_num, data_char)'
7671   ||' ('
7672   ||'  SELECT result_id, test_id, 0, result_value_num, result_value_char FROM'
7673   ||'  ('
7674   ||'    SELECT r.result_id, r.test_id, r.result_value_num, r.result_value_char,'
7675   ||'           r.result_date, r.test_replicate_cnt'
7676   ||'    FROM   gmd_results r, gmd_spec_results sr, gmd_samples s'
7677   ||'    WHERE  r.result_id = sr.result_id'
7678   ||'    AND    r.sample_id = :l_sample_id'
7679   ||'    AND    r.test_replicate_cnt = :l_test_replicate_cnt'
7680   ||'    AND    sr.event_spec_disp_id = :l_event_spec_disp_id'
7681   ||'    AND    nvl(sr.evaluation_ind, ' || '''' || 'XX' || '''' || ')  not in ('
7682   ||                         '''' || '5O' || '''' ||','|| '''' || '4C' || '''' ||')'
7683   ||'    AND    sr.delete_mark = 0'
7684   ||'    AND    r.delete_mark = 0'
7685   ||'    AND    r.sample_id = s.sample_id'
7686   ||'    AND    s.retain_as IS NULL'
7687   ||'  )'
7688   ||')'
7689   ;
7690 
7691   -- The code below is now removed/changed from the SQL Above
7692   --||'    FROM   gmd_results r, gmd_spec_results sr, gmd_qc_tests_b t'
7693   --||'    AND    r.test_id   = t.test_id'
7694   --||'    AND    t.test_type <> ' || '''' || 'U' || ''''
7695 
7696   -- GO through all the sample_ids and populate _GTMP table
7697   FOR i in 1..p_sample_ids.COUNT
7698   LOOP
7699     IF (l_debug = 'Y') THEN
7700        gmd_debug.put_line('  Finding results for Sample ID = ' || p_sample_ids(i));
7701        gmd_debug.put_line('  p_test_replicate_cnt = ' || p_test_replicate_cnt);
7702     END IF;
7703     EXECUTE IMMEDIATE l_sql_stmt USING p_sample_ids(i), p_test_replicate_cnt, p_event_spec_disp_id;
7704   END LOOP;
7705 
7706   dump_data_points;
7707 
7708   IF (l_debug = 'Y') THEN
7709      gmd_debug.put_line('Leaving Procedure - POPULATE_RESULT_DATA_POINTS_R');
7710   END IF;
7711   -- All systems GO...
7712 
7713 EXCEPTION
7714   WHEN    FND_API.G_EXC_ERROR THEN
7715     x_return_status := FND_API.G_RET_STS_ERROR ;
7716   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7717     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7718   WHEN OTHERS THEN
7719     GMD_API_PUB.Log_Message('GMD_API_ERROR',
7720                             'PACKAGE','POPULATE_RESULT_DATA_POINTS',
7721                             'ERROR', SUBSTR(SQLERRM,1,100));
7722     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7723 
7724 END populate_result_data_points_r;
7725 
7726 END gmd_results_grp;