<?php
// allocation.php
// VIMS RDT Order / Pick Allocation  Process
// ----------------------------------------------------------------------------------------
// Modified 2019-04-26 - Over Allocation Correction.
// ----------------------------------------------------------------------------------------
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');
set_time_limit(0);

// Get Input Parameters from POST or GET
if (!empty($_POST))
{
	$mFunction  		= $_POST['function'];
	$mfConnect   		= $_POST['connection'];
	$mfUser				= $_POST['user'];
	$mfPwd				= $_POST['pword'];
	$mfHost              = $_POST['host'];
	$mfDbName			= $_POST['dbase'];
	$mfTableName 		= $_POST['table'];
	$mFilter			= $_POST['filter'];
	$mReturnFields		= $_POST['returnfields'];
	$mCurrentUser		= $_POST['currentuser'];
	$mRow				= $_POST['row'];
}
else
{
	$mFunction  		= $_GET['function'];
	$mfConnect   		= $_GET['connection'];
	$mfUser				= $_GET['user'];
	$mfPwd				= $_GET['pword'];
	$mfHost              = $_GET['host'];
	$mfDbName			= $_GET['dbase'];
	$mfTableName 		= $_GET['table'];
	$mFilter			= $_GET['filter'];
	$mReturnFields		= $_GET['returnfields'];
	$mCurrentUser		= $_GET['currentuser'];
	$mRow				= $_GET['row'];
}

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

$mStatus = "OK  -";

// echo "FAIL-TEST1:".$mFilter."-".$mHost."-".$mUser."-".$mPwd."-".$mDbName;
// return;

// Split Functions String
if (($mFunction != null) && (strtolower($mFunction) == "getfunction"))
{
	$mErrMsg = "";
// Explode Input String Into Array - Separated by ,
	$mFunctions = explode(",",$mFilter);
// Loop Through Functions
	$mReturn = "";
	$i = 0;
	while ($i<count($mFunctions))
	{
		try
		{
// php File name
			list($mFile,$mParms) = explode(":",$mFunctions[$i]);
// Non-Server Processes
// page:
			if (($mFile == "page") || ($mFile == "rdttime"))
			{
				if ($mReturn == "")
					$mReturn = $mFunctions[$i];
				else
					$mReturn .= "|AND|".$mFunctions[$i];
			}
			else
			{
				list($mCall,$mPassParm) = explode("?",$mParms);
// Replace Chars in Input String to emulate a JSON format instead of GET format
				$mPassParm = str_replace("=",":",$mPassParm);
				$mPassParm = str_replace("&",",",$mPassParm);
				$mPassParm .= ",currentUser:".$mCurrentUser;		
				$mPassParm .= ",host:".$mfHost;
				$mPassParm .= ",user:".$mfUser;
				$mPassParm .= ",pwd:".$mfPwd;
				$mPassParm .= ",dbase:".$mfDbName;
				$mPassParm .=",connect:".$mfConnect;
// Include File
				include_once($mFile.'.php');
// Function	
				if(function_exists($mCall))
					$mStatus = $mCall($mPassParm);
// Test for validation FAIL
				if (substr($mStatus,0,2)=="FA")
				{
					$mReturn = $mStatus;
					break;
				}
// Test for validation DATA:EOD
				if (substr($mStatus,0,8)=="DATA:EOD")
				{
					$mReturn = $mStatus;
					break;
				}
// Populate return string
				if ($mReturn == "")
					$mReturn = $mStatus;
				else
					$mReturn .= "|AND|".$mStatus;
// Test for 'skip'
				if (strpos($mStatus,"skip") !== false)
				{
					break;
				}
			}
		}
		catch (Exception $e) 
		{
			$mReturn = "FAIL-".$e;
		}
			$i++;
	}
	$mReturn = "ROW<".$mRow.">".$mReturn;
	echo $mReturn;
	return;
}

?>