DBA Data[Home] [Help]

PACKAGE BODY: APPS.GMD_RESULTS_GRP

Source


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