1<?php
2// TestPlanResults.php
3// Test Plan Results Entry.
4
5include('includes/session.php');
6$ViewTopic = '';/* ?????????? */
7$BookMark = 'TestPlanResults';
8$Title = _('Test Plan Results');
9include('includes/header.php');
10
11if (isset($_GET['SelectedSampleID'])){
12	$SelectedSampleID =mb_strtoupper($_GET['SelectedSampleID']);
13} elseif(isset($_POST['SelectedSampleID'])){
14	$SelectedSampleID =mb_strtoupper($_POST['SelectedSampleID']);
15}
16
17if (!isset($_POST['FromDate'])){
18	$_POST['FromDate']=Date(($_SESSION['DefaultDateFormat']), strtotime($UpcomingDate . ' - 15 days'));
19}
20if (!isset($_POST['ToDate'])){
21	$_POST['ToDate'] = Date($_SESSION['DefaultDateFormat']);
22}
23
24if (isset($Errors)) {
25	unset($Errors);
26}
27
28$Errors = array();
29
30echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/maintenance.png" title="' . _('Search') . '" alt="" />' . ' ' . $Title . '</p>';
31if (isset($_GET['CopyResults']) OR isset($_POST['CopyResults'])) {
32	if (!isset($_POST['CopyToSampleID']) OR $_POST['CopyToSampleID']=='' OR !isset($_POST['Copy'])) {
33		echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">
34			<div>
35			<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
36			<input type="hidden" name="SelectedSampleID" value="' . $SelectedSampleID . '" />
37			<input type="hidden" name="CopyResults" value="CopyResults" />';
38		if (isset($_POST['ResetPart'])) {
39			unset($SelectedStockItem);
40		}
41
42		if (isset($SampleID) AND $SampleID != '') {
43			if (!is_numeric($SampleID)) {
44				prnMsg(_('The Sample ID entered') . ' <U>' . _('MUST') . '</U> ' . _('be numeric'), 'error');
45				unset($SampleID);
46			} else {
47				echo _('Sample ID') . ' - ' . $SampleID;
48			}
49		}
50		if (!Is_Date($_POST['FromDate'])) {
51			$InputError = 1;
52			prnMsg(_('Invalid From Date'),'error');
53			$_POST['FromDate']=Date(($_SESSION['DefaultDateFormat']), strtotime($UpcomingDate . ' - 15 days'));
54		}
55		if (!Is_Date($_POST['ToDate'])) {
56			$InputError = 1;
57			prnMsg(_('Invalid To Date'),'error');
58			$_POST['ToDate'] = Date($_SESSION['DefaultDateFormat']);
59		}
60		if (isset($_POST['SearchParts'])) {
61			if ($_POST['Keywords'] AND $_POST['StockCode']) {
62				prnMsg(_('Stock description keywords have been used in preference to the Stock code extract entered'), 'info');
63			}
64			if ($_POST['Keywords']) {
65				//insert wildcard characters in spaces
66				$SearchString = '%' . str_replace(' ', '%', $_POST['Keywords']) . '%';
67				$SQL = "SELECT stockmaster.stockid,
68						stockmaster.description,
69						stockmaster.decimalplaces,
70						SUM(locstock.quantity) as qoh,
71						stockmaster.units,
72					FROM stockmaster INNER JOIN locstock
73					ON stockmaster.stockid = locstock.stockid
74					INNER JOIN locationusers ON locationusers.loccode = locstock.loccode
75							AND locationusers.userid='" .  $_SESSION['UserID'] . "'
76							AND locationusers.canview=1
77					WHERE stockmaster.description " . LIKE  . " '" . $SearchString ."'
78					AND stockmaster.categoryid='" . $_POST['StockCat'] . "'
79					GROUP BY stockmaster.stockid,
80						stockmaster.description,
81						stockmaster.decimalplaces,
82						stockmaster.units
83					ORDER BY stockmaster.stockid";
84			} elseif ($_POST['StockCode']) {
85				$SQL = "SELECT stockmaster.stockid,
86						stockmaster.description,
87						stockmaster.decimalplaces,
88						SUM(locstock.quantity) AS qoh,
89						stockmaster.units
90					FROM stockmaster INNER JOIN locstock
91						ON stockmaster.stockid = locstock.stockid
92					INNER JOIN locationusers ON locationusers.loccode = locstock.loccode
93							AND locationusers.userid='" .  $_SESSION['UserID'] . "'
94							AND locationusers.canview=1
95					WHERE stockmaster.stockid " . LIKE  . " '%" . $_POST['StockCode'] . "%'
96					AND stockmaster.categoryid='" . $_POST['StockCat'] . "'
97					GROUP BY stockmaster.stockid,
98						stockmaster.description,
99						stockmaster.decimalplaces,
100						stockmaster.units
101					ORDER BY stockmaster.stockid";
102			} elseif (!$_POST['StockCode'] AND !$_POST['Keywords']) {
103				$SQL = "SELECT stockmaster.stockid,
104						stockmaster.description,
105						stockmaster.decimalplaces,
106						SUM(locstock.quantity) AS qoh,
107						stockmaster.units
108					FROM stockmaster INNER JOIN locstock ON stockmaster.stockid = locstock.stockid
109					INNER JOIN locationusers ON locationusers.loccode = locstock.loccode
110							AND locationusers.userid='" .  $_SESSION['UserID'] . "'
111							AND locationusers.canview =1
112					WHERE stockmaster.categoryid='" . $_POST['StockCat'] . "'
113					GROUP BY stockmaster.stockid,
114						stockmaster.description,
115						stockmaster.decimalplaces,
116						stockmaster.units
117					ORDER BY stockmaster.stockid";
118			}
119
120			$ErrMsg = _('No stock items were returned by the SQL because');
121			$DbgMsg = _('The SQL used to retrieve the searched parts was');
122			$StockItemsResult = DB_query($SQL, $ErrMsg, $DbgMsg);
123		}
124
125		if (true or !isset($LotNumber) or $LotNumber == "") { //revisit later, right now always show all inputs
126			echo '<table class="selection"><tr><td>';
127			if (isset($SelectedStockItem)) {
128				echo _('For the part') . ':<b>' . $SelectedStockItem . '</b> ' . _('and') . ' <input type="hidden" name="SelectedStockItem" value="' . $SelectedStockItem . '" />';
129			}
130			echo _('Lot Number') . ': <input name="LotNumber" autofocus="autofocus" maxlength="20" size="12" value="' . $LotNumber . '"/> ' . _('Sample ID') . ': <input name="SampleID" maxlength="10" size="10" value="' . $SampleID . '"/> ';
131			echo _('From Sample Date') . ': <input name="FromDate" size="10" class="date" value="' . $_POST['FromDate'] . '" /> ' . _('To Sample Date') . ': <input name="ToDate" size="10" class="date" value="' . $_POST['ToDate'] . '" /> ';
132			echo '<input type="submit" name="SearchSamples" value="' . _('Search Samples') . '" /></td>
133				</tr>
134				</table>';
135		}
136		$SQL = "SELECT categoryid,
137					categorydescription
138				FROM stockcategory
139				ORDER BY categorydescription";
140		$result1 = DB_query($SQL);
141		echo '
142				<table class="selection">
143				<tr>
144					<td>';
145		echo _('To search for Pick Lists for a specific part use the part selection facilities below') . '</td></tr>';
146		echo '<tr>
147				<td>' . _('Select a stock category') . ':<select name="StockCat">';
148		while ($MyRow1 = DB_fetch_array($result1)) {
149			if (isset($_POST['StockCat']) and $MyRow1['categoryid'] == $_POST['StockCat']) {
150				echo '<option selected="selected" value="' . $MyRow1['categoryid'] . '">' . $MyRow1['categorydescription'] . '</option>';
151			} else {
152				echo '<option value="' . $MyRow1['categoryid'] . '">' . $MyRow1['categorydescription'] . '</option>';
153			}
154		}
155		echo '</select></td>
156				<td>' . _('Enter text extracts in the') . ' <b>' . _('description') . '</b>:</td>
157				<td><input type="text" name="Keywords" size="20" maxlength="25" /></td>
158			</tr>
159			<tr>
160				<td></td>
161				<td><b>' . _('OR') . ' </b>' . _('Enter extract of the') . '<b> ' . _('Stock Code') . '</b>:</td>
162				<td><input type="text" name="StockCode" size="15" maxlength="18" /></td>
163			</tr>
164			<tr>
165				<td colspan="3">
166					<div class="centre">
167						<input type="submit" name="SearchParts" value="' . _('Search Parts Now') . '" />
168						<input type="submit" name="ResetPart" value="' . _('Show All') . '" />
169					</div>
170				</td>
171			</tr>
172			</table>
173			<br />
174			<br />';
175
176		if (isset($StockItemsResult)) {
177			echo '<table class="selection">
178				<thead>
179					<tr>
180						<th class="ascending">' . _('Code') . '</th>
181						<th class="ascending">' . _('Description') . '</th>
182						<th class="ascending">' . _('On Hand') . '</th>
183						<th class="ascending">' . _('Units') . '</th>
184					</tr>
185				</thead>
186				<tbody>';
187
188			while ($MyRow = DB_fetch_array($StockItemsResult)) {
189				echo '<tr class="striped_row">
190					<td><input type="submit" name="SelectedStockItem" value="' . $MyRow['stockid'] . '"</td>
191					<td>' . $MyRow['description'] . '</td>
192					<td class="number">' . locale_number_format($MyRow['qoh'],$MyRow['decimalplaces']) . '</td>
193					<td>' . $MyRow['units'] . '</td>
194					</tr>';
195			}
196			//end of while loop
197			echo '</tbody></table>';
198		}
199		//end if stock search results to show
200		else {
201			$FromDate = FormatDateForSQL($_POST['FromDate']);
202			$ToDate = FormatDateForSQL($_POST['ToDate']);
203			if (isset($LotNumber) AND $LotNumber != '') {
204				$SQL = "SELECT sampleid,
205								prodspeckey,
206								description,
207								lotkey,
208								identifier,
209								createdby,
210								sampledate,
211								cert
212							FROM qasamples
213							LEFT OUTER JOIN stockmaster on stockmaster.stockid=qasamples.prodspeckey
214							WHERE lotkey='" . filter_number_format($LotNumber) . "'
215							AND sampleid<>'" . $SelectedSampleID . "'";
216			} elseif (isset($SampleID) AND $SampleID != '') {
217				$SQL = "SELECT sampleid,
218								prodspeckey,
219								description,
220								lotkey,
221								identifier,
222								createdby,
223								sampledate,
224								cert
225							FROM qasamples
226							LEFT OUTER JOIN stockmaster on stockmaster.stockid=qasamples.prodspeckey
227							WHERE sampleid='" . filter_number_format($SampleID) . "'
228							AND sampleid<>'" . $SelectedSampleID . "'";
229			} else {
230				if (isset($SelectedStockItem)) {
231					$SQL = "SELECT sampleid,
232								prodspeckey,
233								description,
234								lotkey,
235								identifier,
236								createdby,
237								sampledate,
238								cert
239							FROM qasamples
240							INNER JOIN stockmaster on stockmaster.stockid=qasamples.prodspeckey
241							WHERE stockid='" . $SelectedStockItem . "'
242							AND sampledate>='$FromDate'
243							AND sampledate <='$ToDate'
244							AND sampleid<>'" . $SelectedSampleID . "'";
245				} else {
246					$SQL = "SELECT sampleid,
247								prodspeckey,
248								description,
249								lotkey,
250								identifier,
251								createdby,
252								sampledate,
253								comments,
254								cert
255							FROM qasamples
256							LEFT OUTER JOIN stockmaster on stockmaster.stockid=qasamples.prodspeckey
257							WHERE sampledate>='$FromDate'
258							AND sampledate <='$ToDate'
259							AND sampleid<>'" . $SelectedSampleID . "'";
260				} //no stock item selected
261			} //end no sample id selected
262			$ErrMsg = _('No QA samples were returned by the SQL because');
263			$SampleResult = DB_query($SQL, $ErrMsg);
264			if (DB_num_rows($SampleResult) > 0) {
265
266				echo '<table cellpadding="2" width="90%" class="selection">
267					<thead>
268						<tr>
269							<th class="ascending">' . _('Copy Results') . '</th>
270							<th class="ascending">' . _('Enter Results') . '</th>
271							<th class="ascending">' . _('Specification') . '</th>
272							<th class="ascending">' . _('Description') . '</th>
273							<th class="ascending">' . _('Lot / Serial') . '</th>
274							<th class="ascending">' . _('Identifier') . '</th>
275							<th class="ascending">' . _('Created By') . '</th>
276							<th class="ascending">' . _('Sample Date') . '</th>
277							<th class="ascending">' . _('Comments') . '</th>
278							<th class="ascending">' . _('Cert Allowed') . '</th>
279						</tr>
280					</thead>
281					<tbody>';
282
283				while ($MyRow = DB_fetch_array($SampleResult)) {
284					$ModifySampleID = $RootPath . '/TestPlanResults.php?SelectedSampleID=' . $MyRow['sampleid'];
285					$Copy = '<a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?SelectedSampleID=' . $SelectedSampleID .'&CopyToSampleID=' . $MyRow['sampleid'] .'">' . _('Copy to This Sample') .'</a>';
286					$FormatedSampleDate = ConvertSQLDate($MyRow['sampledate']);
287
288					if ($MyRow['cert']==1) {
289						$CertAllowed='<a target="_blank" href="'. $RootPath . '/PDFCOA.php?LotKey=' .$MyRow['lotkey'] .'&ProdSpec=' .$MyRow['prodspeckey'] .'">' . _('Yes') . '</a>';
290					} else {
291						$CertAllowed=_('No');
292					}
293
294					echo '<tr class="striped_row">
295							<td><input type="radio" name="CopyToSampleID" value="' . $MyRow['sampleid'] .'">
296							<td><a target="blank" href="' . $ModifySampleID . '">' . str_pad($MyRow['sampleid'],10,'0',STR_PAD_LEFT) . '</a></td>
297							<td>' . $MyRow['prodspeckey'] . '</td>
298							<td>' . $MyRow['description'] . '</td>
299							<td>' . $MyRow['lotkey'] . '</td>
300							<td>' .  $MyRow['identifier']  . '</td>
301							<td>' .  $MyRow['createdby']  . '</td>
302							<td>' . $FormatedSampleDate . '</td>
303							<td>' . $MyRow['comments'] . '</td>
304							<td>' . $CertAllowed . '</td>
305							</tr>';
306				} //end of while loop
307				echo '</tbody></table>';
308			} // end if Pick Lists to show
309		}
310		echo '</div>' . _('Override existing Test values?') .
311			 '<input type="checkbox" name="OverRide"><input type="submit" name="Copy" value="' . _('Copy') . '" />
312			  </form>';
313		include('includes/footer.php');
314		exit;
315	} else {
316		$sql = "SELECT sampleresults.testid,
317						sampleresults.defaultvalue,
318						sampleresults.targetvalue,
319						sampleresults.rangemin,
320						sampleresults.rangemax,
321						sampleresults.testvalue,
322						sampleresults.testdate,
323						sampleresults.testedby,
324						sampleresults.comments,
325						sampleresults.isinspec,
326						sampleresults.showoncert,
327						sampleresults.showontestplan,
328						prodspeckey,
329						type
330					FROM sampleresults
331					INNER JOIN qasamples ON qasamples.sampleid=sampleresults.sampleid
332					INNER JOIN qatests ON qatests.testid=sampleresults.testid
333					WHERE sampleresults.sampleid='" .$SelectedSampleID. "'";
334		$msg = _('Test Results have been copied to sample') . ' ' . $_POST['CopyToSampleID']  . ' from sample' . ' ' . $SelectedSampleID ;
335		$ErrMsg = _('The insert of the test results failed because');
336		$DbgMsg = _('The SQL that was used and failed was');
337		$result = DB_query($sql,$ErrMsg, $DbgMsg);
338
339		while ($MyRow = DB_fetch_array($result)) {
340			$result2 = DB_query("SELECT count(testid) FROM prodspecs
341						WHERE testid = '".$MyRow['testid']."'
342						AND keyval='".$MyRow['prodspeckey']."'");
343			$MyRow2 = DB_fetch_row($result2);
344			if($MyRow2[0]>0) {
345				$ManuallyAdded=0;
346			} else {
347				$ManuallyAdded=1;
348			}
349			$result2 = DB_query("SELECT resultid, targetvalue,rangemin, rangemax FROM sampleresults
350						WHERE testid = '".$MyRow['testid']."'
351						AND sampleid='".$_POST['CopyToSampleID']."'");
352			$MyRow2 = DB_fetch_array($result2);
353			$IsInSpec=1;
354			$CompareVal='yes';
355			$CompareRange='no';
356			if ($MyRow['targetvalue']=='') {
357				$CompareVal='no';
358			}
359			if ($MyRow['type']==4) {
360				//$RangeDisplay=$MyRow['rangemin'] . '-'  . $MyRow['rangemax'] . ' ' . $MyRow['units'];
361				$RangeDisplay='';
362				if ($MyRow['rangemin'] > '' OR $MyRow['rangemax'] > '') {
363					if ($MyRow['rangemin'] > '' AND $MyRow['rangemax'] == '') {
364						$RangeDisplay='> ' . $MyRow['rangemin'] . ' ' . $MyRow['units'];
365					} elseif ($MyRow['rangemin']== '' AND $MyRow['rangemax'] > '') {
366						$RangeDisplay='< ' . $MyRow['rangemax'] . ' ' . $MyRow['units'];
367					} else {
368						$RangeDisplay=$MyRow['rangemin'] . ' - ' . $MyRow['rangemax'] . ' ' . $MyRow['units'];
369					}
370					$CompareRange='yes';
371				}
372
373			} else {
374				$RangeDisplay='&nbsp;';
375				$CompareRange='no';
376			}
377			if ($MyRow['type']==3) {
378				$CompareVal='no';
379			}
380			//var_dump($CompareVal); var_dump($CompareRange);
381			if ($CompareVal=='yes'){
382				if ($CompareRange=='yes'){
383					if ($MyRow2['rangemin'] > '' AND $MyRow2['rangemax'] > '') {
384						if (($MyRow['testvalue']<>$MyRow2['targetvalue']) AND ($MyRow['testvalue']<$MyRow2['rangemin'] OR $MyRow['testvalue'] > $MyRow2['rangemax'])) {
385							$IsInSpec=0;
386						}
387					} elseif ($MyRow2['rangemin'] > '' AND $MyRow2['rangemax'] == '') {
388						if (($MyRow['testvalue']<>$MyRow2['targetvalue']) AND ($MyRow['testvalue']<=$MyRow2['rangemin'])) {
389							$IsInSpec=0;
390						}
391					} elseif ($MyRow2['rangemin'] == '' AND $MyRow2['rangemax'] > '') {
392						if (($MyRow['testvalue']<>$MyRow2['targetvalue']) AND ($MyRow['testvalue'] >= $MyRow2['rangemax'])) {
393							$IsInSpec=0;
394						}
395					}
396					//var_dump($MyRow['testvalue']); var_dump($MyRow2['targetvalue']); var_dump($MyRow2['rangemin']); var_dump($MyRow2['rangemax']);
397				} else {
398					if (($MyRow['testvalue']<>$MyRow2['targetvalue'])) {
399						$IsInSpec=0;
400					}
401				}
402			}
403			if($MyRow2[0]>'') {
404				//test already exists on CopyToSample
405				if ($_POST['OverRide']=='on') {
406					$updsql = "UPDATE sampleresults
407								SET	testvalue='" .$MyRow['testvalue']. "',
408									testdate='" .$MyRow['testdate']. "',
409									testedby='" .$MyRow['testedby']. "',
410									isinspec='" .$IsInSpec. "'
411								WHERE sampleid='" . $_POST['CopyToSampleID'] ."'
412								AND resultid='".$MyRow2[0]."'";
413					$msg = _('Test Results have been overwritten to sample') . ' ' . $_POST['CopyToSampleID']  . _(' from sample') . ' ' . $SelectedSampleID  . _(' for test ') . $MyRow['testid'];
414					$ErrMsg = _('The insert of the test results failed because');
415					$DbgMsg = _('The SQL that was used and failed was');
416					$updresult = DB_query($updsql,$ErrMsg, $DbgMsg);
417					prnMsg($msg , 'success');
418				} else {
419					$msg = _('Test Results have NOT BEEN overwritten for Result ID ') . $MyRow2[0];
420					prnMsg($msg , 'warning');
421				}
422			} else {
423				//Need to insert the test and results
424				$inssql = "INSERT INTO sampleresults
425							(sampleid,
426							testid,
427							defaultvalue,
428							targetvalue,
429							testvalue,
430							rangemin,
431							rangemax,
432							showoncert,
433							showontestplan,
434							comments,
435							manuallyadded,
436							testedby,
437							testdate,
438							isinspec)
439						VALUES ( '"  . $_POST['CopyToSampleID'] . "',
440								'"  . $MyRow['testid'] . "',
441								'"  . $MyRow['defaultvalue'] . "',
442								'"  . $MyRow['targetvalue']. "',
443								'"  . $MyRow['testvalue']. "',
444								'"  . $MyRow['rangemin'] . "',
445								'"  . $MyRow['rangemax'] . "',
446								'"  . $MyRow['showoncert'] . "',
447								'"  . $MyRow['showontestplan'] . "',
448								'"  . $MyRow['comments'] . "',
449								'"  . $ManuallyAdded . "',
450								'"  . $MyRow['testedby'] . "',
451								'"  . $MyRow['testdate'] . "',
452								'"  . $IsInSpec . "'
453								)";
454				$msg = _('Test Results have been copied to') . ' ' . $_POST['CopyToSampleID'] . ' ' . _('from') . ' ' . $SelectedSampleID . ' ' . _('for') . ' ' . $MyRow['testid'];
455				$ErrMsg = _('The insert of the test results failed because');
456				$DbgMsg = _('The SQL that was used and failed was');
457				$insresult = DB_query($inssql,$ErrMsg, $DbgMsg);
458				prnMsg($msg , 'success');
459			}
460		} //while loop on myrow
461		$SelectedSampleID=$_POST['CopyToSampleID'];
462		unset($_GET['CopyResults']);
463		unset($_POST['CopyResults']);
464	} //else
465} //CopySpec
466
467if (isset($_GET['ListTests'])) {
468	$sql = "SELECT qatests.testid,
469				name,
470				method,
471				units,
472				type,
473				numericvalue,
474				qatests.defaultvalue
475			FROM qatests
476			LEFT JOIN sampleresults
477			ON sampleresults.testid=qatests.testid
478			AND sampleresults.sampleid='".$SelectedSampleID."'
479			WHERE qatests.active='1'
480			AND sampleresults.sampleid IS NULL";
481	$result = DB_query($sql);
482	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">
483		<div>
484		<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
485		<table class="selection">
486		<thead>
487			<tr>
488				<th class="ascending">' . _('Add') . '</th>
489				<th class="ascending">' . _('Name') . '</th>
490				<th class="ascending">' . _('Method') . '</th>
491				<th class="ascending">' . _('Units') . '</th>
492				<th>' . _('Possible Values') . '</th>
493				<th>' . _('Target Value') . '</th>
494				<th>' . _('Range Min') . '</th>
495				<th>' . _('Range Max') . '</th>
496			</tr>
497		</thead>
498		<tbody>';
499
500	$x=0;
501	while ($MyRow=DB_fetch_array($result)) {
502
503		$x++;
504		$Class='';
505		$RangeMin='';
506		$RangeMax='';
507		if ($MyRow['numericvalue'] == 1) {
508			$IsNumeric = _('Yes');
509			$Class="number";
510		} else {
511			$IsNumeric = _('No');
512		}
513
514		switch ($MyRow['type']) {
515		 	case 0; //textbox
516		 		$TypeDisp='Text Box';
517		 		break;
518		 	case 1; //select box
519		 		$TypeDisp='Select Box';
520				break;
521			case 2; //checkbox
522				$TypeDisp='Check Box';
523				break;
524			case 3; //datebox
525				$TypeDisp='Date Box';
526				$Class="date";
527				break;
528			case 4; //range
529				$TypeDisp='Range';
530				$RangeMin='<input  class="' .$Class. '" type="text" name="AddRangeMin' .$x.'" />';
531				$RangeMax='<input  class="' .$Class. '" type="text" name="AddRangeMax' .$x.'" />';
532				break;
533		} //end switch
534		printf('<tr class="striped_row">
535			<td>%s</td>
536			<td>%s</td>
537			<td>%s</td>
538			<td>%s</td>
539			<td>%s</td>
540			<td>%s</td>
541			<td>%s</td>
542			<td>%s</td>
543			</tr>',
544			'<input type="checkbox" name="AddRow' .$x.'"><input type="hidden" name="AddTestID' .$x.'" value="' .$MyRow['testid']. '">',
545			$MyRow['name'],
546			$MyRow['method'],
547			$MyRow['units'],
548			$MyRow['defaultvalue'],
549			'<input  class="' .$Class. '" type="text" name="AddTargetValue' .$x.'" />',
550			$RangeMin,
551			$RangeMax);
552
553	} //END WHILE LIST LOOP
554
555	echo '</tbody></table><br />
556			<div class="centre">
557				<input type="hidden" name="SelectedSampleID" value="' . $SelectedSampleID . '" />
558				<input type="hidden" name="AddTestsCounter" value="' . $x . '" />
559				<input type="submit" name="AddTests" value="' . _('Add') . '" />
560			</div>
561		</div>
562		</form>';
563	include('includes/footer.php');
564	exit;
565}  //ListTests
566if (isset($_POST['AddTests'])) {
567	for ($i=0;$i<=$_POST['AddTestsCounter'];$i++){
568		if ($_POST['AddRow' .$i]=='on') {
569				if ($_POST['AddRangeMin' .$i]=='') {
570				$AddRangeMin="NULL";
571			} else {
572				$AddRangeMin="'" . $_POST['AddRangeMin' .$i] . "'";
573			}
574			if ($_POST['AddRangeMax' .$i]=='') {
575				$AddRangeMax="NULL";
576			} else {
577				$AddRangeMax="'" . $_POST['AddRangeMax' .$i] . "'";
578			}
579			$sql = "INSERT INTO sampleresults
580							(sampleid,
581							testid,
582							defaultvalue,
583							targetvalue,
584							rangemin,
585							rangemax,
586							showoncert,
587							showontestplan,
588							manuallyadded)
589						SELECT '"  . $SelectedSampleID . "',
590								testid,
591								defaultvalue,
592								'"  .  $_POST['AddTargetValue' .$i] . "',
593								"  . $AddRangeMin . ",
594								"  . $AddRangeMax . ",
595								showoncert,
596								'1',
597								'1'
598						FROM qatests WHERE testid='" .$_POST['AddTestID' .$i]. "'";
599			$msg = _('A Sample Result record has been added for Test ID') . ' ' . $_POST['AddTestID' .$i]  . ' for ' . ' ' . $KeyValue ;
600			$ErrMsg = _('The insert of the Sample Result failed because');
601			$DbgMsg = _('The SQL that was used and failed was');
602			$result = DB_query($sql,$ErrMsg, $DbgMsg);
603			prnMsg($msg , 'success');
604		} //if on
605	} //for
606} //AddTests
607
608if (isset($_POST['submit'])) {
609
610	//initialise no input errors assumed initially before we test
611	$InputError = 0;
612
613	for ($i=1;$i<=$_POST['TestResultsCounter'];$i++){
614		$IsInSpec=1;
615		//var_dump($_POST['CompareVal' .$i]); var_dump($_POST['CompareRange' .$i]);
616		if ($_POST['CompareVal' .$i]=='yes'){
617			if ($_POST['CompareRange' .$i]=='yes'){
618				//if (($_POST['TestValue' .$i]<>$_POST['ExpectedValue' .$i]) AND ($_POST['TestValue' .$i]<$_POST['MinVal' .$i] OR $_POST['TestValue' .$i] > $_POST['MaxVal' .$i])) {
619				//	$IsInSpec=0;
620				//}
621				if ($_POST['MinVal' .$i] > '' AND $_POST['MaxVal' .$i] > '') {
622					if (($_POST['TestValue' .$i]<>$_POST['ExpectedValue' .$i]) AND ($_POST['TestValue' .$i]<$_POST['MinVal' .$i] OR $_POST['TestValue' .$i] > $_POST['MaxVal' .$i])) {
623						//echo "one";
624						$IsInSpec=0;
625					}
626				} elseif ($_POST['MinVal' .$i] > '' AND $_POST['MaxVal' .$i] == '') {
627					if (($_POST['TestValue' .$i]<>$_POST['ExpectedValue' .$i]) AND ($_POST['TestValue' .$i] <= $_POST['MinVal' .$i])) {
628						//echo "two";
629						$IsInSpec=0;
630					}
631				} elseif ($_POST['MinVal' .$i] == '' AND $_POST['MaxVal' .$i] > '') {
632					if (($_POST['TestValue' .$i]<>$_POST['ExpectedValue' .$i]) AND ($_POST['TestValue' .$i] >= $_POST['MaxVal' .$i])) {
633						//echo "three";
634						$IsInSpec=0;
635					}
636				}
637				//echo "four";
638				//var_dump($_POST['TestValue' .$i]); var_dump($_POST['ExpectedValue' .$i]); var_dump($_POST['MinVal' .$i]); var_dump($_POST['MaxVal' .$i]); var_dump($IsInSpec);
639			} else {
640				if (($_POST['TestValue' .$i]<>$_POST['ExpectedValue' .$i])) {
641					$IsInSpec=0;
642				}
643			}
644		}
645		$sql = "UPDATE sampleresults SET testedby='".  $_POST['TestedBy' .$i] . "',
646										testdate='". FormatDateForSQL($_POST['TestDate' .$i]) . "',
647										testvalue='".  $_POST['TestValue' .$i] . "',
648										showoncert='".  $_POST['ShowOnCert' .$i] . "',
649										isinspec='".  $IsInSpec . "'
650						WHERE resultid='".  $_POST['ResultID' .$i] . "'";
651
652		$msg = _('Sample Results were updated for Result ID') . ' ' . $_POST['ResultID' .$i] ;
653		$ErrMsg = _('The updated of the sampleresults failed because');
654		$DbgMsg = _('The SQL that was used and failed was');
655		$result = DB_query($sql,$ErrMsg, $DbgMsg);
656		prnMsg($msg , 'success');
657	} //for
658	//check to see all values are in spec or at least entered
659	$result = DB_query("SELECT count(sampleid) FROM sampleresults
660						WHERE sampleid = '".$SelectedSampleID."'
661						AND showoncert='1'
662						AND testvalue=''");
663	$MyRow = DB_fetch_row($result);
664	if($MyRow[0]>0) {
665		$sql = "UPDATE qasamples SET identifier='" . $_POST['Identifier'] . "',
666									comments='" . $_POST['Comments'] . "',
667									cert='0'
668				WHERE sampleid = '".$SelectedSampleID."'";
669		$msg = _('Test Results have not all been entered.  This Lot is not able to be used for a a Certificate of Analysis');
670		$ErrMsg = _('The update of the QA Sample failed because');
671		$DbgMsg = _('The SQL that was used and failed was');
672		$result = DB_query($sql,$ErrMsg, $DbgMsg);
673		prnMsg($msg , 'error');
674	}
675}
676if (isset($_GET['Delete'])) {
677	$sql= "SELECT COUNT(*) FROM sampleresults WHERE sampleresults.resultid='".$_GET['ResultID']."'
678											AND sampleresults.manuallyadded='1'";
679	$result = DB_query($sql);
680	$MyRow = DB_fetch_row($result);
681	if ($MyRow[0]==0) {
682		prnMsg(_('Cannot delete this Result ID because it is a part of the Product Specification'),'error');
683	} else {
684		$sql="DELETE FROM sampleresults WHERE resultid='". $_GET['ResultID']."'";
685		$ErrMsg = _('The sample results could not be deleted because');
686		$result = DB_query($sql,$ErrMsg);
687
688		prnMsg(_('Result QA Sample') . ' ' . $_GET['ResultID'] . _('has been deleted from the database'),'success');
689		unset($_GET['ResultID']);
690		unset($delete);
691		unset ($_GET['delete']);
692	}
693}
694if (!isset($SelectedSampleID)) {
695	echo '<div class="centre">
696			<a href="' . $RootPath . '/SelectQASamples.php">' .  _('Select a sample to enter results against') . '</a>
697		</div>';
698	prnMsg(_('This page can only be opened if a QA Sample has been selected. Please select a sample first'),'info');
699	include ('includes/footer.php');
700	exit;
701}
702
703echo '<div class="centre"><a href="' . $RootPath . '/SelectQASamples.php">' . _('Back to Samples') . '</a></div>';
704
705
706echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
707echo '<div>';
708echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
709
710
711$sql = "SELECT prodspeckey,
712				description,
713				lotkey,
714				identifier,
715				sampledate,
716				comments,
717				cert
718		FROM qasamples
719		LEFT OUTER JOIN stockmaster on stockmaster.stockid=qasamples.prodspeckey
720		WHERE sampleid='".$SelectedSampleID."'";
721
722$result = DB_query($sql);
723$MyRow = DB_fetch_array($result);
724
725if ($MyRow['cert']==1){
726	$Cert=_('Yes');
727} else {
728	$Cert=_('No');
729}
730
731echo '<input type="hidden" name="SelectedSampleID" value="' . $SelectedSampleID . '" />';
732echo '<table class="selection">
733		<tr>
734			<th>' . _('Sample ID') . '</th>
735			<th>' . _('Specification') . '</th>
736			<th>' . _('Lot / Serial') . '</th>
737			<th>' . _('Identifier') . '</th>
738			<th>' . _('Sample Date') . '</th>
739			<th>' . _('Comments') . '</th>
740			<th>' . _('Used for Cert') . '</th>
741		</tr>';
742
743echo '<tr class="striped_row"><td>' . str_pad($SelectedSampleID,10,'0',STR_PAD_LEFT)  . '</td>
744	<td>' . $MyRow['prodspeckey'] . ' - ' . $MyRow['description'] . '</td>
745	<td>' . $MyRow['lotkey'] . '</td>
746	<td>' . $MyRow['identifier'] . '</td>
747	<td>' . ConvertSQLDate($MyRow['sampledate']) . '</td>
748	<td>' . $MyRow['comments'] . '</td>
749	<td>' . $Cert . '</td>
750	</tr>	</table><br />';
751$LotKey=$MyRow['lotkey'];
752$ProdSpec=$MyRow['prodspeckey'];
753$CanCert=$MyRow['cert'];
754$sql = "SELECT sampleid,
755				resultid,
756				sampleresults.testid,
757				qatests.name,
758				qatests.method,
759				qatests.units,
760				qatests.type,
761				qatests.numericvalue,
762				sampleresults.defaultvalue,
763				sampleresults.targetvalue,
764				sampleresults.rangemin,
765				sampleresults.rangemax,
766				sampleresults.testvalue,
767				sampleresults.testdate,
768				sampleresults.testedby,
769				sampleresults.showoncert,
770				isinspec,
771				sampleresults.manuallyadded
772		FROM sampleresults
773		INNER JOIN qatests ON qatests.testid=sampleresults.testid
774		WHERE sampleresults.sampleid='".$SelectedSampleID."'
775		AND sampleresults.showontestplan='1'
776		ORDER BY groupby, name";
777
778$result = DB_query($sql);
779
780echo '<table cellpadding="2" width="90%" class="selection">
781	<thead>
782		<tr>
783			<th class="ascending">' . _('Test Name') . '</th>
784			<th class="ascending">' . _('Test Method') . '</th>
785			<th class="ascending">' . _('Range') . '</th>
786			<th class="ascending">' . _('Target Value') . '</th>
787			<th class="ascending">' . _('Test Date') . '</th>
788			<th class="ascending">' . _('Tested By') . '</th>
789			<th class="ascending">' . _('Test Result') . '</th>
790			<th class="ascending">' . _('On Cert') . '</th>
791		</tr>
792	</thead>
793	<tbody>';
794
795$x = 0;
796
797$techsql = "SELECT userid,
798						realname
799					FROM www_users
800					INNER JOIN securityroles ON securityroles.secroleid=www_users.fullaccess
801					INNER JOIN securitygroups on securitygroups.secroleid=securityroles.secroleid
802					WHERE blocked='0'
803					AND tokenid='16'";
804
805$techresult = DB_query($techsql);
806
807
808while ($MyRow = DB_fetch_array($result)) {
809	$x++;
810	$CompareVal='yes';
811	$CompareRange='no';
812	if ($MyRow['targetvalue']=='') {
813		$CompareVal='no';
814	}
815	if ($MyRow['type']==4) {
816		//$RangeDisplay=$MyRow['rangemin'] . '-'  . $MyRow['rangemax'] . ' ' . $MyRow['units'];
817		$RangeDisplay='';
818		if ($MyRow['rangemin'] > '' OR $MyRow['rangemax'] > '') {
819			//var_dump($MyRow['rangemin']); var_dump($MyRow['rangemax']);
820			if ($MyRow['rangemin'] > '' AND $MyRow['rangemax'] == '') {
821				$RangeDisplay='> ' . $MyRow['rangemin'] . ' ' . $MyRow['units'];
822			} elseif ($MyRow['rangemin']== '' AND $MyRow['rangemax'] > '') {
823				$RangeDisplay='< ' . $MyRow['rangemax'] . ' ' . $MyRow['units'];
824			} else {
825				$RangeDisplay=$MyRow['rangemin'] . ' - ' . $MyRow['rangemax'] . ' ' . $MyRow['units'];
826			}
827			$CompareRange='yes';
828		}
829		//$CompareRange='yes';
830		$CompareVal='yes';
831	} else {
832		$RangeDisplay='&nbsp;';
833		$CompareRange='no';
834	}
835	if ($MyRow['type']==3) {
836		$CompareVal='no';
837	}
838	if ($MyRow['showoncert'] == 1) {
839		$ShowOnCertText = _('Yes');
840	} else {
841		$ShowOnCertText = _('No');
842	}
843	if ($MyRow['testdate']=='0000-00-00'){
844		$TestDate=ConvertSQLDate(date('Y-m-d'));
845	} else {
846		$TestDate=ConvertSQLDate($MyRow['testdate']);
847	}
848
849	$BGColor='';
850	if ($MyRow['testvalue']=='') {
851		$BGColor=' style="background-color:yellow;" ';
852	} else {
853		if ($MyRow['isinspec']==0) {
854		$BGColor=' style="background-color:orange;" ';
855		}
856	}
857
858	$Class='';
859	if ($MyRow['numericvalue'] == 1) {
860		$Class="number";
861	}
862	switch ($MyRow['type']) {
863		case 0; //textbox
864			$TypeDisp='Text Box';
865			$TestResult='<input type="text" size="10" maxlength="20" class="' . $Class . '" name="TestValue' .$x .'" value="' . $MyRow['testvalue'] . '"' . $BGColor . '/>';
866			break;
867		case 1; //select box
868			$TypeDisp='Select Box';
869			$OptionValues = explode(',',$MyRow['defaultvalue']);
870			$TestResult='<select name="TestValue' .$x .'"' . $BGColor . '/>';
871			foreach ($OptionValues as $PropertyOptionValue){
872				if ($PropertyOptionValue == $MyRow['testvalue']){
873					$TestResult.='<option selected="selected" value="' . $PropertyOptionValue . '">' . $PropertyOptionValue . '</option>';
874				} else {
875					$TestResult.='<option value="' . $PropertyOptionValue . '">' . $PropertyOptionValue . '</option>';
876				}
877			}
878			$TestResult.='</select>';
879			break;
880		case 2; //checkbox
881			$TypeDisp='Check Box';
882			break;
883		case 3; //datebox
884			$TypeDisp='Date Box';
885			$Class="date";
886			$TestResult='<input type="text" size="10" maxlength="20" class="' . $Class . '" name="TestValue' .$x .'" value="' . $MyRow['testvalue'] . '"' . $BGColor . '/>';
887			break;
888		case 4; //range
889			$TypeDisp='Range';
890			//$Class="number";
891			$TestResult='<input type="text" size="10" maxlength="20" class="' . $Class . '" name="TestValue' .$x .'" value="' . $MyRow['testvalue'] . '"' . $BGColor . '/>';
892			break;
893	} //end switch
894	if ($MyRow['manuallyadded']==1) {
895		$Delete = '<a href="' .htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8')  .'?Delete=yes&amp;SelectedSampleID=' . $MyRow['sampleid'].'&amp;ResultID=' . $MyRow['resultid']. '" onclick="return confirm(\'' . _('Are you sure you wish to delete this Test from this Sample ?') . '\');">' . _('Delete').'</a>';
896		//echo $MyRow['showoncert'];
897		$ShowOnCert='<select name="ShowOnCert' .$x .'">';
898		if ($MyRow['showoncert']==1) {
899			$ShowOnCert.= '<option value="1" selected="selected">' . _('Yes') . '</option>';
900			$ShowOnCert.= '<option value="0">' . _('No') . '</option>';
901		} else {
902			$ShowOnCert.= '<option value="0" selected="selected">' . _('No') . '</option>';
903			$ShowOnCert.= '<option value="1">' . _('Yes') . '</option>';
904		}
905		$ShowOnCert.='</select>';
906	} else {
907		$Delete ='';
908		$ShowOnCert='<input type="hidden" name="ShowOnCert' .$x .'" value="' . $MyRow['showoncert'] . '" />' .$ShowOnCertText;
909	}
910	if ($MyRow['testedby']=='') {
911		$MyRow['testedby']=$_SESSION['UserID'];
912	}
913	echo '<tr class="striped_row">
914			<td><input type="hidden" name="ResultID' .$x. '" value="' . $MyRow['resultid'] . '" /> ' . $MyRow['name'] . '
915			<input type="hidden" name="ExpectedValue' .$x. '" value="' . $MyRow['targetvalue'] . '" />
916			<input type="hidden" name="MinVal' .$x. '" value="' . $MyRow['rangemin'] . '" />
917			<input type="hidden" name="MaxVal' .$x. '" value="' . $MyRow['rangemax'] . '" />
918			<input type="hidden" name="CompareRange' .$x. '" value="' . $CompareRange . '" />
919			<input type="hidden" name="CompareVal' .$x. '" value="' . $CompareVal . '" />
920			</td>
921			<td>' . $MyRow['method'] . '</td>
922			<td>' . $RangeDisplay . '</td>
923			<td>' . $MyRow['targetvalue'] . ' ' . $MyRow['units'] . '</td>
924			<td><input type="text" class="date" name="TestDate' .$x. '" size="10" maxlength="10" value="' . $TestDate . '" /> </td>
925			<td><select name="TestedBy' .$x .'"/>';
926	while ($techrow = DB_fetch_array($techresult)) {
927		if ($techrow['userid'] == $MyRow['testedby']){
928			echo '<option selected="selected" value="' . $techrow['userid'] . '">' .$techrow['realname'] . '</option>';
929		} else {
930			echo '<option value="' .$techrow['userid'] . '">' . $techrow['realname'] . '</option>';
931		}
932	}
933	echo '</select>';
934	DB_data_seek($techresult,0);
935	echo '<td>' . $TestResult . '</td>
936			<td>' . $ShowOnCert . '</td>
937			<td>' . $Delete . '</td>
938		</tr>';
939}
940
941echo '</tbody>
942		</table>
943		<div class="centre">
944			<input type="hidden" name="TestResultsCounter" value="' . $x . '" />
945			<input type="submit" name="submit" value="' . _('Enter Information') . '" />
946		</div>
947	</div>
948	</form>';
949
950echo '<div class="centre"><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?ListTests=yes&amp;SelectedSampleID=' .$SelectedSampleID .'">' . _('Add More Tests') . '</a></div>';
951echo '<div class="centre"><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?CopyResults=yes&amp;SelectedSampleID=' .$SelectedSampleID .'">' . _('Copy These Results') . '</a></div>';
952
953if ($CanCert==1){
954	echo '<div class="centre"><a target="_blank" href="'. $RootPath . '/PDFCOA.php?LotKey=' .$LotKey .'&ProdSpec=' . $ProdSpec. '">' . _('Print COA') . '</a></div>';
955}
956
957include('includes/footer.php');
958?>
959