<?php
// report.php
// VIMS RDT Reports
// ----------------------------------------------------------------------------------------
// 2020-01-08 - Added LocationHash Label Call
// ----------------------------------------------------------------------------------------
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');
// Header to allow Cross-Server AJAX Connection
header('Access-Control-Allow-Origin: *');

// ----------------------------------------------------------------------------
// Use Statements
// ----------------------------------------------------------------------------
use classes\MysqliVeu;
use classes\RDTResponse;
use classes\ValueSanitiser;

// error_reporting(E_ALL);
// ini_set('display_errors', 1);

// ----------------------------------------------------------------------------
// Includes and Requires
// ----------------------------------------------------------------------------
require_once __DIR__ . '/coreInit.php';

// ----------------------------------------------------------------------------
// Global Variables
// ----------------------------------------------------------------------------

// Database connection
$database = new MysqliVeu();

$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();

$mErrMsg = "OK  -";

// ----------------------------------------------------------------------------
// Utility Functions
// ----------------------------------------------------------------------------

function getPrinter($mHost, $mUser, $mPwd, $mDbName, $mCurrentUser, $mPrinter)
{
// mysql connection - Use Passed values...
    $mConnection = mysqli_connect($mHost, $mUser, $mPwd, $mDbName) or
    exit('FAIL-Connect failed: ' . mysqli_connect_errno() . ' : ' . mysqli_connect_error());
    $mPrinterAdd = '';
    $mDataQuery = "SELECT id AS id, printer_address AS printer_address, description AS printer_name FROM printer_mapping WHERE printer_code='" . $mPrinter . "' OR printer_address='" . $mPrinter . "' LIMIT 1";
//	exit("FAIL-TEST1-".$mFilter."-".$mDataQuery);
    if (!$mData = mysqli_query($mConnection, $mDataQuery))
    {
        exit('FAIL-' . mysqli_error($mConnection) . $mDataQuery);
    }
//	exit ("FAIL-".mysqli_error($mConnection).$mDataQuery."-".mysqli_num_rows($mData));
    if (mysqli_num_rows($mData) == 0)
    {
        return ('FAIL-Invalid Printer Code.');
    }

    $mRow = mysqli_fetch_assoc($mData);
    $mPrinterId = ($mRow['id']);
    $mPrinterAdd = ($mRow['printer_address']);
    $mPrinterName = ($mRow['printer_name']);

// Store Session Printer
    if (isset($_SESSION['userData']))
    {
        $_SESSION['userData']['printer'] = $mPrinterName;
    }

    return ($mPrinterAdd);
}

function getPrinterList($mHost, $mUser, $mPwd, $mDbName, $mCurrentUser, $mKey = null, $mOffset = 0)
{
//	echo("FAIL-TEST3");
// mysql connection - Use Passed values...
    $mConnection = mysqli_connect($mHost, $mUser, $mPwd, $mDbName) or
    exit('FAIL-Connect failed: ' . mysqli_connect_errno() . ' : ' . mysqli_connect_error());

    $mDataQuery = 'SELECT printer_mapping.id AS id, printer_mapping.printer_code AS printer_code,printer_mapping.printer_address AS address FROM printer_mapping';
    if ($mKey != '' && $mKey != null)
    {
        $mDataQuery .= " WHERE printer_mapping.description LIKE('$mKey%')";
    }
    $mDataQuery .= ' LIMIT 50 OFFSET ' . $mOffset;
    if (!$mData = mysqli_query($mConnection, $mDataQuery))
    {
        exit ('FAIL-' . mysqli_error($mConnection) . '-' . $mDataQuery);
    }
//	exit ("FAIL-".mysqli_error($mConnection)."-".$mDataQuery."-".mysqli_num_rows($mData));
    $mDropdown = array();

    while ($mRow = mysqli_fetch_assoc($mData))
    {
        $mDropdown[] = ['value' => "printerskipap:{$mRow['printer_code']}", 'text' => $mRow['printer_code'],];
    }
    mysqli_close($mConnection);

    return 'DROP-' . json_encode($mDropdown);
}

// ----------------------------------------------------------------------------
// Routers
// ----------------------------------------------------------------------------

// Gets receiving printer list
if (($mFunction === 'printersDropdownList'))
{
    $postedData = ValueSanitiser::array(json_decode(file_get_contents('php://input'), true));

    // Data posted validation
    if (empty($postedData['field']))
    {
        RDTResponse::fail('Missing printer selector');
    }

    // Checks if field value is valid
    $validColumns = ['printer_code', 'printer_address', 'description', 'department', 'printer_type'];

    if (!in_array($postedData['field'], $validColumns))
    {
        RDTResponse::fail('Invalid printer selector');
    }

    // Get printers list
    $sql = "SELECT 
            printer_code
        FROM printer_mapping
        WHERE {$postedData['field']} = ?";

    $result = $database->queryParamsArray($sql, [$postedData['value']]);

    if (empty($result))
    {
        RDTResponse::fail('No printers found');
    }

    // Creates Dropdown select
    $dropdownArray = [];
    foreach ($result as $row)
    {
        $dropdownArray[] = [
            'value' => "printer:{$row['printer_code']}",
            'text' => strtoupper($row['printer_code'])
            ];
    }

    // Success response
    RDTResponse::dropdown($dropdownArray, true);
}

// Check for Session Time-Out
if (($mFunction != null) && ($mFunction == "sessiontimeout"))
{
    $mTimeOut = 900;
    $mFilter = str_replace("=", ":", $mFilter);
    $mFilter = str_replace("&", ",", $mFilter);
// Explode Input String Into Array - Separated by ,
    $mArray1 = explode(",", $mFilter);
// Create Associative Array by Exploding Array Elements on :
    foreach ($mArray1 as $k1 => $v1)
    {
        list($k2, $v2) = explode(":", $mArray1[$k1]);
        if ($k2 == "timeout")
//			$mTimeOut = $v2*60;
        {
            $mTimeOut = $v2 * 3600;
        }
    }

    session_start();
//	if(time() - $_SESSION['userData']['timestamp'] > 900)
    if (time() - $_SESSION['userData']['timestamp'] > $mTimeOut)
    {
// subtract new timestamp from the old one
//		echo"<script>alert('15 Minutes over!');</script>";
        echo("FALO-Session Timed-out");
        unset($_SESSION['userData']);
        exit;
    }
    else
    {
        $_SESSION['userData']['timestamp'] = time(); //set new timestamp
    }
}

// printer mapping
elseif (($mFunction != null) && ($mFunction == "getprinter"))
{
    // Set default error message
    $mErrMsg = "";
    $mScreenOption = "";
    $mSkip = 0;

    // Explode Input Parm
    // Replace Chars in Input String to emulate a JSON format instead of GET format
    $mFilter = str_replace("=", ":", $mFilter);
    $mFilter = str_replace("&", ",", $mFilter);
    $i = 0;

    // Explode Input String Into Array - Separated by ,
    $mArray1 = explode(",", $mFilter);

    // Create Associative Array by Exploding Array Elements on :
    foreach ($mArray1 as $k1 => $v1)
    {
        list($k2, $v2) = explode(":", $mArray1[$k1]);
        if ($k2 == "printer")
        {
            $mPrinter = strtoupper(trim($v2));
        }
        elseif ($k2 == "currentUser")
        {
            $mCurrentUser = strtoupper(trim($v2));
        }
        elseif ($k2 == "screenoption")
        {
            $mScreenOption = strtoupper(trim($v2));
        }
    }

    $mPrinterAdd = "";
    $mDataQuery = "SELECT
            id AS id,
            printer_address AS printer_address,
            description AS printer_name
        FROM printer_mapping
        WHERE
            (printer_code = ? OR printer_address= ?)";

    $params = [$mPrinter, $mPrinter];

    $mData = $database->queryParamsRow($mDataQuery, $params);

    if (empty($mData))
    {
        RDTResponse::fail('Invalid Printer Code');
    }

    $mPrinterId = ($mData['id']);
    $mPrinterAdd = ($mData['printer_address']);
    $mPrinterName = ($mData['printer_name']);

    if (substr($mErrMsg, 0, 4) != "FAIL")
    {
        $mErrMsg = "OK  -getprinter";
    }

    $responseDataStr = "";
    if (!$mScreenOption || $mScreenOption == "")
    {
        $responseDataStr = "printercontent:{$mPrinterName},printerinput:{$mPrinterAdd}:I:page+";
    }
    else
    {
        $mSkip = strpos($mScreenOption, 'I');
        $mSkip += 1;
        $responseDataStr = "printercontent:{$mPrinterName},printerinput:{$mPrinterAdd}:{$mScreenOption}:skip{$mSkip}";
    }

    // Success Response
    RDTResponse::data($responseDataStr);
}

// User Session printer mapping
elseif (($mFunction != null) && ($mFunction == "getuserprinter"))
{
// Set default error message
    $mErrMsg = "OK  -";
    $mPrinter = "";
    $mScreenOption = "";
    $mSkip = 0;
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
    $mFilter = str_replace("=", ":", $mFilter);
    $mFilter = str_replace("&", ",", $mFilter);
    $i = 0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "label")
			$mLabel = strtoupper($v2);
        if ($k2 == "printer")
			$mPrinter = $v2;
		elseif ($k2 == "currentUser")
			$mCurrentUser = strtoupper($v2);
        elseif ($k2 == "screenoption")
			$mScreenOption = strtoupper($v2);
	}

// Get Printer from Printer_mapping
//    if ($mPrinter == "")
//    {
    $mPrinter = getPrinter($mHost, $mUser, $mPwd, $mDbName, $mCurrentUser, $mLabel);
    if (substr($mPrinter, 0, 2) == "FA")
    {
        exit($mPrinter);
    }
//    }

// return Printer name & IP address
    $mPrinterAdd = $mPrinter;
    $mPrinterName = $mLabel;

	if (!$mScreenOption || $mScreenOption=="")
        exit("DATA:txtstoredvalue:".$mPrinterName.",printercontent:".$mPrinterAdd.",printerinput:".$mPrinterName.":I:page+");
    else
    {
        if (strpos($mScreenOption,'I') === true)
        {
            $mSkip = strpos($mScreenOption,'I');
            $mSkip +=1;
            exit("DATA:txtstoredvalue:".$mPrinterName.",printercontent:".$mPrinterAdd.",printerinput:".$mPrinterName.":".$mScreenOption.":skip".$mSkip);
        }
        else
        {
            exit("DATA:txtstoredvalue:".$mPrinterName.",printercontent:".$mPrinterAdd.",printerinput:".$mPrinterName.":".$mScreenOption);
        }
    }
}

// Pallet Labels
elseif (($mFunction != null) && ($mFunction=="pltlabelpdf"))
{
// Set default error message
	$mErrMsg = "OK  -";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "prefix")
			$mPrefix = strtoupper($v2);
		elseif ($k2 == "number")
			$mNumber = strtoupper($v2);
		elseif ($k2 == "printer")
			$mPrinter = strtoupper($v2);
		elseif ($k2 == "label")
			$mLabel = strtoupper($v2);
		elseif ($k2 == "currentUser")
			$mCurrentUser = strtoupper($v2);
	}

//	exit("FAIL-TEST1-".$mFilter);

	include_once('../reports/pltlabelpdf.php');
	$mStatus = labelPrint($mPrefix,$mNumber,$mPrinter,$mLabel,$mHost,$mUser,$mPwd,$mDbName,"n");
	if (substr($mStatus,0,4) != "FAIL")
		$mStatus = "OK  -pltlabelpdf";

}

// Location Labels
elseif (($mFunction != null) && ($mFunction=="loclabelpdf"))
{
// Set default error message
	$mErrMsg = "OK  -";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "fromloc")
			$mFromLoc = strtoupper($v2);
		elseif ($k2 == "toloc")
			$mToLoc = strtoupper($v2);
		elseif ($k2 == "number")
			$mNumber = strtoupper($v2);
		elseif ($k2 == "printer")
			$mPrinter = strtoupper($v2);
		elseif ($k2 == "label")
			$mLabel = strtoupper($v2);
		elseif ($k2 == "currentUser")
			$mCurrentUser = strtoupper($v2);
	}

//	exit("FAIL-TEST1-".$mFilter);

	include_once('../reports/locationlabelpdf.php');
	$mStatus = labelPrint($mFromLoc,$mToLoc,$mNumber,$mPrinter,$mLabel,$mHost,$mUser,$mPwd,$mDbName,"n");
	if (substr($mStatus,0,4) != "FAIL")
		$mStatus = "OK  -loclabelpdf";
}

// Unique Serial Labels
elseif (($mFunction != null) && ($mFunction=="srllabelpdf"))
{
// Set default error message
	$mErrMsg = "OK  -";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "prefix")
			$mPrefix = strtoupper($v2);
		elseif ($k2 == "number")
			$mNumber = strtoupper($v2);
		elseif ($k2 == "printer")
			$mPrinter = strtoupper($v2);
		elseif ($k2 == "label")
			$mLabel = strtoupper($v2);
		elseif ($k2 == "currentUser")
			$mCurrentUser = strtoupper($v2);
	}

//	exit("FAIL-TEST1-".$mFilter);

	include_once('../reports/srllabelpdf.php');
	$mStatus = labelPrint($mPrefix,$mNumber,$mPrinter,$mLabel,$mHost,$mUser,$mPwd,$mDbName,"n");
	if (substr($mStatus,0,4) != "FAIL")
		$mStatus = "OK  -srllabelpdf";
}

// Location Hash / Single Label
elseif (($mFunction != null) && ($mFunction=="lochashlabelpdf"))
{
// Set default error message
	$mErrMsg = "OK  -";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
    foreach ($mArray1 as $k1 => $v1)
    {
        list($k2, $v2) = explode(":", $mArray1[$k1]);
        if ($k2 == "fromloc")
        {
            $mFromLoc = strtoupper($v2);
        }
        elseif ($k2 == "printer")
        {
            $mPrinter = strtoupper($v2);
        }
        elseif ($k2 == "label")
        {
            $mLabel = strtoupper($v2);
        }
        elseif ($k2 == "currentUser")
        {
            $mCurrentUser = strtoupper($v2);
        }
    }

//	exit("FAIL-TEST1-".$mFilter);

    include_once('../reports/locationhashlabelpdf.php');
    $mStatus = labelPrint($mFromLoc, $mPrinter, $mLabel, $mHost, $mUser, $mPwd, $mDbName, "n");
    if (substr($mStatus, 0, 4) != "FAIL")
    {
        $mStatus = "OK  -lochashlabelpdf";
    }
}

// Location Hash / Single Label
elseif (($mFunction != null) && ($mFunction=="srlodettepdf"))
{
// Set default error message
	$mErrMsg = "OK  -";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "partnumber")
			$mPartNumber = strtoupper($v2);
		elseif ($k2 == "quantity")
			$mQuantity = $v2;
		elseif ($k2 == "number")
			$mNumber = $v2;
		elseif ($k2 == "printer")
			$mPrinter = $v2;
		elseif ($k2 == "label")
			$mLabel = $v2;
		elseif ($k2 == "currentUser")
			$mCurrentUser = $v2;
	}

//	exit("FAIL-TEST1-".$mFilter);

	include_once('../reports/srlodettepdf.php');
	$mStatus = labelPrint($mPartNumber,$mQuantity,$mNumber,$mPrinter,$mLabel,$mHost,$mUser,$mPwd,$mDbName,$mCurrentUser,"n");
	if (substr($mStatus,0,4) != "FAIL")
		$mStatus = "OK  -srlodettepdf";
}

// RTS Labels
elseif (($mFunction != null) && ($mFunction=="rtslabelpdf"))
{
// Set default error message
	$mErrMsg = "";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
    $mLabelsAcross = 0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "rtsreference")
			$mRtsReference = strtoupper($v2);
        elseif ($k2 == "serialreference")
			$mSerialReference = strtoupper($v2);
        elseif ($k2 == "partnumber")
			$mPartNumber = strtoupper($v2);
		elseif ($k2 == "quantity")
			$mQuantity = $v2;
		elseif ($k2 == "number")
			$mNumber = $v2;
		elseif ($k2 == "printer")
			$mPrinter = $v2;
		elseif ($k2 == "label")
			$mLabel = $v2;
        elseif ($k2 == "labelsacross")
			$mLabelsAcross = $v2;
		elseif ($k2 == "currentUser")
			$mCurrentUser = $v2;
	}

//	exit("FAIL-TEST1-".$mFilter);
	session_start();
    if(isset($_SESSION['userData']))
        $_SESSION['userData']['printer'] = $mPrinter;
	include_once(__DIR__.'/../reports/rtslabelpdf.php');
//    $mStatus = labelPrint($mPrinter,$mLabel,$mHost,$mUser,$mPwd,$mDbName,$mCurrentUser,"n",$mRtsReference,$mSerialReference,$mPartNumber,$mLabelsAcross);
	$mStatus = labelPrint($mPrinter,$mLabel,$mCurrentUser,$mRtsReference,$mSerialReference,$mPartNumber,$mLabelsAcross,'n');
	if ($mStatus == "")
		$mStatus = "OK  -rtslabelpdf";
}
elseif (($mFunction != null) && ($mFunction=="rtslabeldatamaxpdf"))
{
// Set default error message
	$mErrMsg = "OK  -";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
    $mLabelsAcross = 0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "rtsreference")
			$mRtsReference = strtoupper($v2);
        elseif ($k2 == "serialreference")
			$mSerialReference = strtoupper($v2);
        elseif ($k2 == "partnumber")
			$mPartNumber = strtoupper($v2);
		elseif ($k2 == "quantity")
			$mQuantity = $v2;
		elseif ($k2 == "number")
			$mNumber = $v2;
		elseif ($k2 == "printer")
			$mPrinter = $v2;
		elseif ($k2 == "label")
			$mLabel = $v2;
        elseif ($k2 == "labelsacross")
			$mLabelsAcross = $v2;
		elseif ($k2 == "currentUser")
			$mCurrentUser = $v2;
	}

//	exit("FAIL-TEST1-".$mFilter);
	session_start();
    if(isset($_SESSION['userData']))
        $_SESSION['userData']['printer'] = $mPrinter;
	include_once('../reports/rtslabeldatamaxpdf.php');
    $mStatus = labelPrint($mPrinter,$mLabel,$mHost,$mUser,$mPwd,$mDbName,$mCurrentUser,"n",$mRtsReference,$mSerialReference,$mPartNumber,$mLabelsAcross);
	if (substr($mStatus,0,4) != "FAIL")
		$mStatus = "OK  -rtslabelpdf";
}
// Decant Labels
elseif (($mFunction != null) && ($mFunction=="decantlabelpdf"))
{
// Set default error message
	$mErrMsg = "";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "decantid")
			$mDecantId = $v2;
		elseif ($k2 == "decantref")
			$mDecantRef = $v2;
		elseif ($k2 == "printer")
			$mPrinter = strtoupper($v2);
		elseif ($k2 == "label")
			$mLabel = strtoupper($v2);
		elseif ($k2 == "currentUser")
			$mCurrentUser = strtoupper($v2);
	}

//	exit("FAIL-TEST1-".$mFilter."-".$mDecantId);


	if ($mPrinter == "")
	{
		$mPrinter="default";
	}
//	exit("FAIL-TEST1-".$mFilter."-".$mPrinter);
	if ($mErrMsg == "")
	{
		include_once('../reports/decantlabelpdf.php');
		$mStatus = labelPrint($mDecantId,$mDecantRef,$mPrinter,$mLabel,$mHost,$mUser,$mPwd,$mDbName,"n");
	}
//	if (substr($mStatus,0,4) != "FAIL")
		$mStatus = "OK  -decantlabelpdf";
}

// Decant Labels
elseif (($mFunction != null) && ($mFunction=="insplabelpdf"))
{
// Set default error message
	$mErrMsg = "";
// Explode Input Parm
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
	$i=0;
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "docref")
			$mDocRef = $v2;
		elseif ($k2 == "printer")
			$mPrinter = strtoupper($v2);
		elseif ($k2 == "label")
			$mLabel = strtoupper($v2);
		elseif ($k2 == "currentUser")
			$mCurrentUser = strtoupper($v2);
	}

//	exit("FAIL-TEST1-".$mFilter."-".$mDecantId);


	if ($mPrinter == "")
	{
		$mPrinter="default";
	}
//	exit("FAIL-TEST1-".$mFilter."-".$mPrinter);
	if ($mErrMsg == "")
	{
		include_once('../reports/insplabelpdf.php');
		$mStatus = labelPrint($mDocRef,$mPrinter,$mLabel,$mHost,$mUser,$mPwd,$mDbName,"n");
	}
//	if (substr($mStatus,0,4) != "FAIL")
		$mStatus = "OK  -insplabelpdf";
}

// Get train list for select
elseif (($mFunction != null) && (strtolower($mFunction) == "getprinterlist"))
{
//	echo("FAIL-TEST2");
	$mErrMsg = "";
    $mOffset=0;
// Replace Chars in Input String to emulate a JSON format instead of GET format
	$mFilter = str_replace("=",":",$mFilter);
	$mFilter = str_replace("&",",",$mFilter);
// Explode Input String Into Array - Separated by ,
	$mArray1 = explode(",",$mFilter);
// Create Associative Array by Exploding Array Elements on :
	foreach($mArray1 as $k1=>$v1)
	{
		list($k2, $v2) = explode(":",$mArray1[$k1]);
		if ($k2 == "key")
			$mKey = trim($v2);
        elseif ($k2 == "offset")
			$mOffset = trim($v2);
		elseif ($k2 == "currentUser")
			$mCurrentUser = trim($v2);
		elseif ($k2 == "transactionType")
			$mTransactionTypeId = strtoupper(trim($v2));
	}
	$mStatus = getPrinterList($mHost,$mUser,$mPwd,$mDbName,$mCurrentUser,$mKey,$mOffset);
	echo $mStatus;
	return;
}

if (($mStatus == "") && ($mErrMsg != ""))
{
    $mStatus = $mErrMsg;
}
// echo json_encode(array_merge(array("status" => $mStatus),array("data" => $mDataStr),$mSQLData));
if ($mDataStr == "")
{
    echo $mStatus;
}
elseif (substr($mDataStr, 0, 4) == "DATA")
{
    echo $mDataStr;
}
else
{
    echo $mStatus . $mDataStr;
}
exit;
