/**
* function to retrieve the result
*/
function getVLResult($machineType,$worksheetID,$sampleID,$factor) {
	global $default_resultFailureNewSampleMessage;
	
	$result=0;
	//first check if any result overrides are in place
	if($worksheetID && getDetailedTableInfo2("vl_results_override","sampleID='$sampleID' and worksheetID='$worksheetID'","id")) {
		$result=getDetailedTableInfo2("vl_results_override","sampleID='$sampleID' and worksheetID='$worksheetID'","result");
		return $result;
	} elseif(!$worksheetID && getDetailedTableInfo2("vl_results_override","sampleID='$sampleID'","id")) {
		$result=getDetailedTableInfo2("vl_results_override","sampleID='$sampleID'","result");
		return $result;
	} else {
		$rocheFailedResult=0;
		$rocheFailedResult="Invalid Test Result. Insufficient sample remained to repeat the assay.";
		$abbottFailedResult=0;
		$abbottFailedResult="Failed.";
		
		if($machineType=="roche") {
			if(isResultFailed($machineType,($worksheetID?$worksheetID:""),$sampleID)) {
				/*
				* 15/Sept/14: 
				* Hellen (hnansumba@gmail.com, CPHL) requested that once a sample has failed more than once,
				* there is insufficient sample left for a re-run hence;
				* Change result to read "invalid test result. Insufficient sample remained to repeat the assay"
				*/
				if(getDetailedTableInfo3("vl_results_merged","lower(machine)='".strtolower($machineType)."' and vlSampleID='$sampleID'","count(id)","num")>1 && 
					getDetailedTableInfo2("vl_results_merged","lower(machine)='".strtolower($machineType)."' and vlSampleID='$sampleID' order by created limit 1","resultAlphanumeric")==$rocheFailedResult) {
					/*
					* 21/Jan/15: 
					* (sewyisaac@yahoo.co.uk) 
					* Regarding the report where we do not have results, this is the massage to I propose.
					* There is No Result Given. The Test Failed the Quality Control Criteria. We advise you send a a new sample. 
					* 
					* 23/Dec/2015
					* Request from Joseph Kibirige (joseph.kibirige@yahoo.com, CPHL) and Prossy Mbabazi (pronam2000@yahoo.com, CPHL)
					* when a sample has failed twice, automatically override the 2nd result with
					* "There is No Result Given. The Test Failed the Quality Control Criteria. We advise you send a a new sample."
					*/
					logResultOverride($sampleID,$worksheetID,$default_resultFailureNewSampleMessage);
					//return
					return $default_resultFailureNewSampleMessage;
				} else {
					//return
					return $rocheFailedResult;
				}
			} else {
				$result=getDetailedTableInfo2("vl_results_roche",($worksheetID?"worksheetID='$worksheetID' and ":"")."SampleID='$sampleID' order by created desc limit 1","Result");
				$result=getVLNumericResult($result,$machineType,$factor);
				return $result;
			}
		} elseif($machineType=="abbott") {
			if(isResultFailed($machineType,($worksheetID?$worksheetID:""),$sampleID)) {
				/*
				* 15/Sept/14: 
				* Hellen (hnansumba@gmail.com, CPHL) requested that once a sample has failed more than once,
				* there is insufficient sample left for a re-run hence;
				* Change result to read "invalid test result. Insufficient sample remained to repeat the assay"
				* 
				* 22/Sept/14
				* Adjustment made to recognize both samples as failures
				*/
				if(getDetailedTableInfo3("vl_results_merged","lower(machine)='".strtolower($machineType)."' and vlSampleID='$sampleID'","count(id)","num")>1 && 
					getDetailedTableInfo2("vl_results_merged","lower(machine)='".strtolower($machineType)."' and vlSampleID='$sampleID' order by created limit 1","resultAlphanumeric")==$abbottFailedResult) {
					/*
					* 21/Jan/15: 
					* (sewyisaac@yahoo.co.uk) 
					* Regarding the report where we do not have results, this is the massage to I propose.
					* There is No Result Given. The Test Failed the Quality Control Criteria. We advise you send a a new sample. 
					* 
					* 23/Dec/2015
					* Request from Joseph Kibirige (joseph.kibirige@yahoo.com, CPHL) and Prossy Mbabazi (pronam2000@yahoo.com, CPHL)
					* when a sample has failed twice, automatically override the 2nd result with
					* "There is No Result Given. The Test Failed the Quality Control Criteria. We advise you send a a new sample."
					*/
					logResultOverride($sampleID,$worksheetID,$default_resultFailureNewSampleMessage);
					//return
					return $default_resultFailureNewSampleMessage;
				} else {
					//return
					return $abbottFailedResult;
				}
			} else {
				$result=getDetailedTableInfo2("vl_results_abbott",($worksheetID?"worksheetID='$worksheetID' and ":"")."sampleID='$sampleID' order by created desc limit 1","result");
				$result=getVLNumericResult($result,$machineType,$factor);
				return $result;
			}
		}
	}
}
									date(created)>='2015-12-27' 
										group by vlSampleID having num=1");
if(mysqlnumrows($query)) {
	while($q=mysqlfetcharray($query)) {
		//get the id
		$id=0;
		$id=getDetailedTableInfo2("vl_results_override","sampleID='$q[vlSampleID]' and date(created)>='2015-12-27' order by created desc limit 1","id");
		//log and remove record
		logDataRemoval("delete from vl_results_override where id='$id'");
		mysqlquery("delete from vl_results_override where id='$id'");
	}
}
*/

//task 4: a procedure to override key abbott results
$query=0;
$query=mysqlquery("select sampleID, count(id) num, max(created) dateCreated from vl_results_abbott 
						where 
							result='3119 A no clot exit detected error was encountered by the Liquid Handler.' and 
								sampleID not in (select sampleID from vl_results_override) 
									group by sampleID having num>1 order by num desc");
if(mysqlnumrows($query)) {
	while($q=mysqlfetcharray($query)) {
		//log result override
		logResultOverride($q["sampleID"],getDetailedTableInfo2("vl_results_abbott","sampleID='$q[sampleID]' and created='$q[dateCreated]' limit 1","worksheetID"),$default_resultFailureNewSampleMessage);
	}
}

//task 5: fix a result typo
mysqlquery("update vl_results_override set result='There is No Result Given. The Test Failed the Quality Control Criteria. We advise you send a new sample.' where result='There is No Result Given. The Test Failed the Quality Control Criteria. We advise you send a a new sample.'");
?>