<?php
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');
// validaterow - Retrieve Specified Fields from Requested Table
// echo "test";
$mFunction  		= $_GET['function'];
$mConnect   		= $_GET['connection'];
$mUser				= $_GET['user'];
$mPwd				= $_GET['pword'];
$mHost              = $_GET['host'];
$mDbName			= $_GET['dbase'];
$mTableName 		= $_GET['table'];
$mFilter			= $_GET['filter'];
$mReturnFields		= $_GET['returnfields'];

$mFilter = str_replace("|AND|","&",$mFilter);
$mFilter = str_replace("/dbase/",$mDbName,$mFilter);

$mSQLData = array();

$mConnection=mysqli_connect($mHost,$mUser,$mPwd,$mDbName) or
	exit("Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());

// execute SQL command to test for existance of specified data - (eg. SELECT id FROM /dbase/.part WHERE part_number='/partNumber/')	
if (($mFunction != null) && ($mFunction == "validaterow"))
{
	$mErrMsg = "FAIL-No Matching Records";
	$s = strpos($mFilter,"FAIL-");
	if ($s > 0)
	{
		$mErrMsg = substr($mFilter,$s);
		$mFilter = substr($mFilter,0,$s);
	}
	$mDataQuery = str_replace(";",",",$mFilter);
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
//		$mReturnData[] =(array_merge(array("status" => "DATA:"),$mSQLData));
	}
	else
	{
//		$mStatus = "FAIL-No Matching Records";
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// Qty Validation - format - value(condition)
elseif (($mFunction != null) && ($mFunction == "qty"))
{
	$mQty = "";
	$mFrom = "";
	$mTo = "";
	$mErrMsg = "OK  -";
// Test for condition
	$e = strpos($mFilter,"(");
	if ($e > 0)
		$mQty = substr($mFilter,0,$e);
	else
		$mQty = $mFilter;
// Qty Range		
	$s = strpos($mFilter,"..");
	if ($s > 0)
	{
		$mFrom = substr($mFilter,$e+1,$s-($e+1));
		$e = strpos($mFilter,")");
		$mTo = substr($mFilter,$s+2,$e-($s+2));
		if (($mQty < $mFrom) || ($mQty > $mTo))
			$mErrMsg = "FAIL-Qty Range Error".":".$mQty."(".$mFrom."..".$mTo.")";
	}
	$s = strpos($mFilter,"(NE");
	if ($s > 0)
	{
		$e = strpos($mFilter,")");
		$mTo = substr($mFilter,$s+3,$e-($s+3));
		if ($mQty == $mTo)
			$mErrMsg = "FAIL-Qty Error".":".$mQty."=".$mTo;
	}
	$s = strpos($mFilter,"(EQ");
	if ($s > 0)
	{
		$e = strpos($mFilter,")");
		$mTo = substr($mFilter,$s+3,$e-($s+3));
		if ($mQty != $mTo)
			$mErrMsg = "FAIL-Qty Error".":".$mQty."<>".$mTo;
	}
	if ($mErrMsg == "OK  -")
	{
// Test for zero
		if ($mFilter == 0)
			$mErrMsg = "FAIL-zero qty.";
	}
//	$mErrMsg = "FAIL-Qty Range Error".":".$mQty.":".$mFrom.":".$mTo;
}
// Workstation Validation
elseif (($mFunction != null) && ($mFunction == "workstationval")){
	$mErrMsg = "FAIL-Invalid Cell";
	$mStatus = "OK  -";	
	$mDataQuery = "SELECT workstation_config.workstation_code as cell FROM workstation_config WHERE workstation_config.workstation_code = '".$mFilter."'";
	
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$i++;
	}
	
	if ($i <= 0 )
	{
		$mStatus = $mErrMsg;
	}
	
}
// Location Code Validation - Pickable
elseif (($mFunction != null) && ($mFunction == "locationpickable"))
{
	$mErrMsg = "FAIL-No Matching Records";
	$s = strpos($mFilter,"FAIL-");
	if ($s > 0)
	{
		$mErrMsg = substr($mFilter,$s);
		$mFilter = substr($mFilter,0,$s);
	}
	$mDataQuery = "SELECT location.id AS id, location.location_code AS location_code FROM location JOIN location_type ON location.location_type_id=location_type.id JOIN inventory_status ON location_type.default_inventory_status_id=inventory_status.id WHERE (location_code='".$mFilter."' OR location_hash='".$mFilter."') AND non_pickable=0 LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// Location Code Validation - Marshall
elseif (($mFunction != null) && ($mFunction == "locationmarshall"))
{
	$mErrMsg = "FAIL-No Matching Records";
	$s = strpos($mFilter,"FAIL-");
	if ($s > 0)
	{
		$mErrMsg = substr($mFilter,$s);
		$mFilter = substr($mFilter,0,$s);
	}
	$mDataQuery = "SELECT location.id AS id, location.location_code AS location_code FROM location JOIN location_type ON location.location_type_id=location_type.id JOIN inventory_status ON location_type.default_inventory_status_id=inventory_status.id WHERE (location_code='".$mFilter."' OR location_hash='".$mFilter."') AND (substr(location_type_code,1,2)='MA' OR substr(location_type_code,1,2)='TR') LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
//Location Code Validation - Goods in 
elseif (($mFunction != null) && ($mFunction == "locationgoodsin"))
{
	$mErrMsg = "FAIL-No Matching Records";
	$s = strpos($mFilter,"FAIL-");
	if ($s > 0)
	{
		$mErrMsg = substr($mFilter,$s);
		$mFilter = substr($mFilter,0,$s);
	}
	$mDataQuery = "SELECT location.id AS id, location.location_code AS location_code FROM location JOIN location_type ON location.location_type_id=location_type.id JOIN inventory_status ON location_type.default_inventory_status_id=inventory_status.id WHERE (location_code='".$mFilter."' OR location_hash='".$mFilter."') AND location_type_code='GI' LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// Location Code Validation - Trailer
elseif (($mFunction != null) && ($mFunction == "locationtrailer"))
{
	$mErrMsg = "FAIL-No Matching Records";
	$s = strpos($mFilter,"FAIL-");
	if ($s > 0)
	{
		$mErrMsg = substr($mFilter,$s);
		$mFilter = substr($mFilter,0,$s);
	}
	$mDataQuery = "SELECT location.id AS id, location.location_code AS location_code FROM location JOIN location_type ON location.location_type_id=location_type.id JOIN inventory_status ON location_type.default_inventory_status_id=inventory_status.id WHERE (location_code='".$mFilter."' OR location_hash='".$mFilter."') AND substr(location_type_code,1,2)='TR' LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// Location Code Validation - Single Part Location
elseif (($mFunction != null) && ($mFunction == "locationnotmixed"))
{
// Parse-out PartNumber & toLocationCode
	$s = strpos($mFilter,"partNumber=");
	$s = $s + 11;
	$e = strpos($mFilter,"toLocationCode=");
	if ($e <= $s)
		$e = strlen($mFilter);
	$mPartNumber = substr($mFilter,$s,$e-$s);
	$s = strpos($mFilter,"toLocationCode=");
	$s = $s + 15;
	$e = strpos($mFilter,"partNumber=");
	if ($e <= $s)
		$e = strlen($mFilter);
	$mToLocationCode = substr($mFilter,$s,$e-$s);
// Get Location Settings
	$mErrMsg = "";
	$mDataQuery = "SELECT location.id AS id, location.multi_part_location AS multi_part_location, location.multi_vendor_location AS multi_vendor_location FROM location WHERE location.location_code='".$mToLocationCode."' OR location.location_hash='".$mToLocationCode."' LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mLocId = "";
	$mMultiPart = "0";
	$mMultiVendor = "0";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mLocId = ($mRow['id']);
		$mMultiPart = ($mRow['multi_part_location']);
		$mMultiVendor = ($mRow['multi_vendor_location']);
		$i++;
	}
	if ($i == 0)
	{
		$mErrMsg = "FAIL-Invalid Location -".$mPartNumber.":".$mToLocationCode;
	}
	else
	{
		if (($mMultiPart == "0") || ($mMultiVendor == "0"))
		{
			$mDataQuery = "SELECT part.id AS id, part.vendor_id AS vendor_id FROM part WHERE part_number = '".$mPartNumber."' LIMIT 1";
			$mData = mysqli_query($mConnection,$mDataQuery);
			$i = 0;
			$mPartId = "";
			$mVendorId = "";
			while($mRow = mysqli_fetch_assoc($mData)) 
			{
				$mPartId = ($mRow['id']);
				$mVendorId = ($mRow['vendor_id']);
				$i++;
			}			
			if ($i == 0)
			{
				$mErrMsg = "FAIL-Invalid Part -".$mPartNumber.":".$mToLocationCode;
			}
			else
			{
				$mDataQuery = "SELECT inventory_master.id AS id, inventory_master.part_id AS part_id, part.vendor_id AS vendor_id FROM inventory_master LEFT JOIN part ON inventory_master.part_id=part.id WHERE current_location_id =".$mLocId;
				$mData = mysqli_query($mConnection,$mDataQuery);
				$i = 0;
				$mInvId = "";
				$mInvPartId = "";
				$mInvVendorId = "";
				while($mRow = mysqli_fetch_assoc($mData)) 
				{
					$mInvId = ($mRow['id']);
					$mInvPartId = ($mRow['part_id']);
					$mInvVendorId = ($mRow['vendor_id']);
					if (($mMultiPart == "0") && ($mPartId != $mInvPartId))
					{
						$mErrMsg = "FAIL-Different Part In Loc.-".$mPartNumber.":".$mToLocationCode;
						$i++;
						break;
					}
					if (($mMultiVendor == "0") && ($mVendorId != $mInvVendorId))
					{
						$mErrMsg = "FAIL-Different Vendor Part In Loc.-".$mPartNumber.":".$mToLocationCode;
						$i++;
						break;
					}
				}
			}
		}
	}
	mysqli_close($mConnection);
	if ($mErrMsg == "")
	{
		$mStatus = "OK  -";	
		$mDataStr = "DATA:";
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// check for valid Pick Inventory Part / Serial / Location
elseif (($mFunction != null) && ($mFunction == "pickvalidate"))
{
	
}
// check for valid Inventory Part / Serial
elseif (($mFunction != null) && ($mFunction == "inventoryvalidate"))
{
	$mErrMsg = "";
	$mDataQuery = "SELECT inventory_master.id AS id FROM inventory_master JOIN part ON inventory_master.part_id=part.id WHERE ".$mFilter." LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mInvId = "";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mInvId = ($mRow['id']);
		$i++;
	}
	mysqli_close($mConnection);
	if ($i == 0)
	{
		$mErrMsg = "FAIL-Invalid Inventory";		
	}
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// check for duplicate Inventory Serials
elseif (($mFunction != null) && ($mFunction == "inventoryserial"))
{
	$mErrMsg = "";
	$mDataQuery = "SELECT inventory_master.id AS id, part.part_number AS part_number FROM inventory_master LEFT JOIN part ON inventory_master.part_id=part.id WHERE serial_reference = '".$mFilter."' LIMIT 2";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mInvId = "";
	$mInvPart = "";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mInvId = ($mRow['id']);
		$mInvPart = ($mRow['part_number']);
		$i++;
	}
	mysqli_close($mConnection);
	if ($i == 0)
	{
		$mErrMsg = "FAIL-Invalid Serial:".$mFilter;		
	}
	elseif ($i == 1)
	{
		$mRtn = $mInvPart;
		$mStatus = "DATA:partNumber:".$mRtn.":P";	
		$mDataStr = "DATA:partNumber:".$mRtn.":P";
	}
	elseif ($i > 1)
	{
		$mRtn = "";
		$mStatus = "DATA:partNumber::I";	
		$mDataStr = "DATA:partNumber::I";
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = $mErrMsg;
	}
}
// Strip Chars. from part_number
elseif (($mFunction != null) && ($mFunction == "partnumber"))
{
	$mErrMsg = "FAIL-No Matching Records";
	$s = strpos($mFilter,"FAIL-");
	if ($s > 0)
	{
		$mErrMsg = substr($mFilter,$s);
		$mFilter = substr($mFilter,0,$s);
	}	
	$mDataQuery = "SELECT part_number as partNumber FROM part WHERE part_number = substr('".$mFilter."',1,10) LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mPartNum = "";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] = ($mRow);
		$mPartNum = ($mRow['partNumber']);
		$i++;
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
//		$mStatus = $mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// inventory_master validation ($mFilter = SELECT criteria (part_number='PPPP' AND serial_reference='SSSS')
elseif (($mFunction != null) && ($mFunction == "inventorypick"))
{
	$mErrMsg = "FAIL-No Matching Records";
	$s = strpos($mFilter,"FAIL-");
	if ($s > 0)
	{
		$mErrMsg = substr($mFilter,$s);
		$mFilter = substr($mFilter,0,$s);
	}	
	$mDataQuery = "SELECT inventory_master.id AS id,inventory_master.current_location_id AS current_location_id, part.part_number AS part_number FROM inventory_master JOIN part ON inventory_master.part_id=part.id WHERE ".$mFilter." LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mInvLocId = "";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$mInvLocId = ($mRow['current_location_id']);
		$i++;
	}	
	if ($mInvLocId != "")
	{
		$i = 0;
		$mDataQuery = "SELECT location.id AS id, location.location_code AS location_code FROM location JOIN location_type ON location.location_type_id=location_type.id JOIN inventory_status ON location_type.default_inventory_status_id=inventory_status.id WHERE location.id=".$mInvLocId." AND non_pickable=0 LIMIT 1";
		$mData = mysqli_query($mConnection,$mDataQuery);
		$i = 0;
		while($mRow = mysqli_fetch_assoc($mData)) 
		{
			$i++;
		}
		If ($i == 0)
			$mErrMsg = "FAIL-Serial is non-pickable";
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
elseif (($mFunction != null) && ($mFunction == "inventorymar"))
{
	$mErrMsg = "FAIL-No Matching Records";
	$s = strpos($mFilter,"FAIL-");
	if ($s > 0)
	{
		$mErrMsg = substr($mFilter,$s);
		$mFilter = substr($mFilter,0,$s);
	}	
	$mDataQuery = "SELECT inventory_master.id AS id,inventory_master.current_location_id AS current_location_id, part.part_number AS part_number FROM inventory_master JOIN part ON inventory_master.part_id=part.id WHERE ".$mFilter." LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mInvLocId = "";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$mInvLocId = ($mRow['current_location_id']);
		$i++;
	}	
	if ($mInvLocId != "")
	{
		$i = 0;
		$mDataQuery = "SELECT location.id AS id, location.location_code AS location_code FROM location JOIN location_type ON location.location_type_id=location_type.id LEFT JOIN inventory_status ON location_type.default_inventory_status_id=inventory_status.id WHERE location.id=".$mInvLocId." AND location_type_code='MAR' LIMIT 1";
		$mData = mysqli_query($mConnection,$mDataQuery);
		$i = 0;
		while($mRow = mysqli_fetch_assoc($mData)) 
		{
			$i++;
		}
		If ($i == 0)
			$mErrMsg = "FAIL-Serial is non-marshall";
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
elseif (($mFunction != null) && ($mFunction == "getPartforSerial"))
{
		
}
elseif (($mFunction != null) && ($mFunction == "completedocument"))
{
//	exit("FAIL-TEST1:".$mFilter);
	$mErrMsg = "FAIL-No Matching Document";
	$mDataQuery = "SELECT id AS id FROM document_header WHERE document_reference_code='".$mFilter."' LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mDocId = "";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mDocId = ($mRow['id']);
		$i++;
	}
	if ($i == 0)
	{
		$mErrMsg = "FAIL-No Matching Document";
	}
	else
	{
		$mDataQuery = "UPDATE document_header SET complete=1 WHERE id =".$mDocId;
		mysqli_query($mConnection,$mDataQuery);
		$mErrMsg = "OKMS- Doc.".$mFilter." Complete";
	}
	mysqli_close($mConnection);
	exit($mErrMsg);
}
elseif (($mFunction != null) && ($mFunction == "updaterow"))
{
//	exit("FAIL-TEST1:".$mFilter);
	$mDataQuery = $mFilter;
	mysqli_query($mConnection,$mDataQuery);
	$mStatus = "OKMS- Doc.Complete";
	mysqli_close($mConnection);
	exit("OKMS-Doc.Complete");
}
// Validate SQP Ref.
elseif (($mFunction != null) && ($mFunction == "validatesqp"))
{
// Parse-out PartNumber & toLocationCode
	$s = strpos($mFilter,"documentReferenceCode=");
	$s = $s + 22;
	$e = strpos($mFilter,"Stillage=");
	if ($e <= $s)
		$e = strlen($mFilter);
	$mDocumentReferenceCode = substr($mFilter,$s,$e-$s);
	$s = strpos($mFilter,"Stillage=");
	$s = $s + 9;
	$e = strpos($mFilter,"documentReferenceCode=");
	if ($e <= $s)
		$e = strlen($mFilter);
	$mStillage = substr($mFilter,$s,$e-$s);
// Get Document Header
	$mErrMsg = "";
	$mDataQuery = "SELECT id AS id, stillage AS stillage FROM document_header WHERE document_reference_code='".$mDocumentReferenceCode."' LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mDocId = "";
	$mDocStillage = "";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mDocId = ($mRow['id']);
		$mDocStillage = ($mRow['stillage']);
		$i++;
	}
	if ($i == 0)
	{
		$mErrMsg = "FAIL-Invalid Ref. -".$mDocumentReferenceCode;
	}
	else
	{
		if ($mStillage != $mDocStillage)
		{
			$mErrMsg = "FAIL-Reference in Use - ".$mDocumentReferenceCode.":".$mDocStillage;
		}
	}
	mysqli_close($mConnection);
	if ($mErrMsg == "")
	{
		$mStatus = "OK  -";	
		$mDataStr = "DATA:";
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// Validate SQP Stillage.
elseif (($mFunction != null) && ($mFunction == "validatestillage"))
{
// Parse-out Stillage
	$s = strpos($mFilter,"Stillage=");
	$s = $s + 9;
	$e = strpos($mFilter,"documentReferenceCode=");
	if ($e <= $s)
		$e = strlen($mFilter);
	$mStillage = substr($mFilter,$s,$e-$s);
// Get Document Header
	$mErrMsg = "";
	$mDataQuery = "SELECT id AS id, document_reference_code AS documentReferenceCode FROM document_header WHERE stillage='".$mStillage."' AND complete=0 LIMIT 1";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	$mDocId = "";
	$mDocDocumentReferenceCode = "";
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mDocId = ($mRow['id']);
		$mDocDocumentReferenceCode = ($mRow['documentReferenceCode']);
//		$mSQLData[] = ($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	if ($i == 0)
	{
		$mErrMsg = "FAIL-Invalid Ref. for Stillage -".$mStillage;
	}
	else
	{
		if ($mDocDocumentReferenceCode != "")
		{
			$mStatus = "OK  -";	
			$mDataStr = "DATA:";
//			$mDataStr = json_encode($mSQLData);
//			$mDataStr = str_replace("{","",$mDataStr);
//			$mDataStr = str_replace("}","",$mDataStr);
//			$mDataStr = str_replace("[","",$mDataStr);
//			$mDataStr = str_replace("]","",$mDataStr);
//			$mDataStr = str_replace('"',"",$mDataStr);
//			$mDataStr = "DATA:".$mDataStr;
		}
		else
		{
			$mStatus = "OK  -";	
			$mDataStr = "DATA:";
		}
	}	
	if ($mErrMsg == "")
	{		
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
// Transaction_type List
elseif (($mFunction != null) && ($mFunction == "transactions"))
{
	$mErrMsg = "FAIL-Transactions";
	$mStatus = "FAIL-Transactions";
	$mDataQuery = "SELECT short_code,transaction_description FROM transaction_type";
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";	
		$mDataStr = json_encode($mSQLData);
		$mDataStr = str_replace("{","",$mDataStr);
		$mDataStr = str_replace("}","",$mDataStr);
		$mDataStr = str_replace("[","",$mDataStr);
		$mDataStr = str_replace("]","",$mDataStr);
		$mDataStr = str_replace('"',"",$mDataStr);
		$mDataStr = "DATA:".$mDataStr;
	}
	else
	{
		$mStatus = $mErrMsg;
		$mDataStr = "";
	}
}
else
{
	$mDataQuery = "SELECT ".$mReturnFields." FROM ".$mTableName." WHERE ".$mFilter;
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$mSQLData[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	If ($i > 0)
	{
		$mStatus = "OK  -";		
//		$mReturnData[] =(array_merge(array("status" => "OK  - "),$mSQLData));
	}
	else
	{
		$mStatus = "FAIL-No Matching Records";
	}
}
// echo json_encode($mReturnData);
if (($mStatus == "") && ($mErrMsg != ""))
	$mStatus = $mErrMsg;
echo json_encode(array_merge(array("status" => $mStatus),array("data" => $mDataStr),$mSQLData));
return;
?>