<?php
header('Cache-Control: no-cache, no-store, must-revalidate');   
header('Expires: 0');

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

// Odette Label

$mPickRef 			= $_GET['pickreference'];
$mConnect   		= $_GET['connection'];
$mUser				= $_GET['user'];
$mPwd				= $_GET['pword'];
$mHost              = $_GET['host'];
$mDbName			= $_GET['dbase'];
$mTableName 		= $_GET['table'];
$mFilter			= $_GET['filter'];
$mReturnFields		= $_GET['returnfields'];
$mCurrentUser       = $_GET['currentuser'];
$mLabel      		= $_GET['label'];
$mOutput			= $_GET['output'];
$mPrinter			= $_GET['printer'];

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

if (($mPrinter == null) || ($mPrinter == "") || (!$mPrinter))
	$mPrinter = "172.16.1.21";

// Load data
function LoadData($mHost, $mUser, $mPwd, $mDbName, $mPickRef)
{
// Read file lines
	$data = array();
// mysql connection
	$mConnection=mysqli_connect($mHost,$mUser,$mPwd,$mDbName) or
		exit("FAIL-Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
		
	$mDataQuery = "SELECT pick_detail.document_reference AS FLD02, order_header.customer_reference AS FLD06, pick_detail.part_number AS FLD04, pick_detail.serial_reference AS FLD02X, CASE WHEN order_body.ran_order = '' OR order_body.ran_order IS NULL THEN 'NO-RAN' ELSE order_body.ran_order END AS FLD01, order_header.time_slot AS FLD03, pick_detail.qty_allocated AS qty_allocated, pick_detail.location_code AS FLD05 FROM pick_detail LEFT JOIN pick_body ON pick_detail.pick_body_id=pick_body.id LEFT JOIN order_body ON pick_body.order_body_id=order_body.id LEFT JOIN order_header ON order_body.order_header_id=order_header.id WHERE pick_detail.document_reference='".$mPickRef."'";
// mysql query construct
	if (!mysqli_query($mConnection,$mDataQuery))
		exit("FAIL-".mysqli_error($mConnection).":".$mdataQuery);
	
	if (!mysqli_query($mConnection,$mDataQuery))
	{
		exit("FAIL-".mysqli_error($mConnection));
	}
	
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$data[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	print_r("Rows:".$i.":".$mDataQuery);
	return $data;
}

// Pick label
function pickLabel($mLabel, $data, $mPrinter)
{
	print_r("Label:".$mLabel);
// Load Label Template
	$mPrintData = $mLabel;

	$mFh = fopen($mPrintData, 'r');
	$mLabel = fread($mFh, filesize($mPrintData));
	fclose($mFh);
// Add NewLine Characters
	$lines=explode(PHP_EOL,$mLabel);
	$mContent1 = "";
	$mContent2 = "";
	$c = -1;
	foreach($lines as $line)
	{
		if (substr($line,0,5) != "LABEL")
			if ($c == 0)
				$mContent1 .= $line."\n";
			else
				$mContent2 .= $line."\n";
//			$mContent[0] .=$line."\n";
		else
		{
			$c++;
			if ($c > 1)
				$c = 0;
		}
	}
//	print_r("Layout:".$mContent);
// Label Data
	$mLabelContent1 = $mContent1;
	$mLabelContent2 = $mContent2;
	$c = 0;
	foreach($data as $row)
	{
		$i = 0;
		foreach($row as $k=>$v)
		{
// Replace Template placeholder with Value
			if ($c == 0)
				$mLabelContent1 = str_replace($k,$v,$mLabelContent1);
			else
				$mLabelContent2 = str_replace($k,$v,$mLabelContent2);
			$i++;
		}
		$c++;
		if ($c > 1)
		{
// Send Label to Printer
			print_r("Printer:".$mPrinter);
			$fp = fsockopen($mPrinter, 9100, $errno, $errstr, 30);
			fputs ($fp,"\x02L\n");
			fputs ($fp,"H08\n");
			fputs ($fp,"D11\n");
			fprintf($fp,$mLabelContent1.$mLabelContent2);
			fputs ($fp,"E\n"); 

			print_r("Data1:".$mLabelContent1);
			print_r("Data2:".$mLabelContent2);
			$mLabelContent1 = $mContent1;
			$mLabelContent2 = $mContent2;
			$c = 0;
		}
	}
	if ($c == 1)
	{
		$fp = fsockopen($mPrinter, 9100, $errno, $errstr, 30);
		fputs ($fp,"\x02L\n");
		fputs ($fp,"H08\n");
		fputs ($fp,"D11\n");
		fprintf($fp,$mLabelContent1);
		fputs ($fp,"E\n");
	}
	fclose($fp);
}

// Return Default mySql Connection
function defaultDbConnection()
{
// get default System Settings from xml file	
	$mSystem = "DEFAULT";
// Get System Definition XML from URL
	$mData = file_get_contents("../../vimsconfig/systemsettings.xml");
// Parse XML to XML Object Array
	$xml = simplexml_load_string($mData, 'SimpleXMLElement', LIBXML_NOCDATA);
// Split-out System Definitions from XML 'fields' Nodes
	$i = 0;

	for ($i = 0; $i <= 150; $i++) 
	{			
		$mSystemId = "";
		$mPath = "";
		$mDbHost = "";
		$mDbUser = "";
		$mDbPwd = "";
		$mDbase = "";
		$mMqServer = "";
		$mCompany = "";
		if (("".$xml->systems->system[$i]->systemid."" == "") || ("".$xml->systems->system[$i]->systemid."" == false) || ("".$xml->systems->system[$i]->systemid."" == false))
			break;
		
		$mSystemId = "".$xml->systems->system[$i]->systemid."";
		if (($mSystemId == "") || ($mSystemId == false))
			break;
		
// Store System Settings from "trandef/systemsettings.xml"		
		if (strToUpper($mSystem) == strToUpper($mSystemId))
		{
			$mPath = "".$xml->systems->system[$i]->path."";
			$mDbHost = "".$xml->systems->system[$i]->dbhost."";
			$mDbUser = "".$xml->systems->system[$i]->dbuser."";
			$mDbPwd = "".$xml->systems->system[$i]->dbpwd."";
			$mDbase = "".$xml->systems->system[$i]->dbase."";
			break;
		}
		if (($mSystemId == "") || ($mSystemId == false))
			break;
	}
	$mDbDefaults = array();
	$mDbDefaults[0] = $mDbHost;
	$mDbDefaults[1] = $mDbUser;
	$mDbDefaults[2] = $mDbPwd;
	$mDbDefaults[3] = $mDbase;
	$mDbDefaults[4] = $mPath;
	return $mDbDefaults;
}

// Data loading
if (($mHost == "") || ($mHost == null))
{
	$mDbDefaults = defaultDbConnection();
	$mHost = $mDbDefaults[0];
	$mUser = $mDbDefaults[1];
	$mPwd = $mDbDefaults[2];
	$mDbName = $mDbDefaults[3];
	$mPath = $mDbDefaults[4];
}
// date / time
	$date = date('d/m/Y h:i:s a', time());

$data = LoadData($mHost, $mUser, $mPwd, $mDbName, $mPickRef);
pickLabel($mLabel, $data, $mPrinter);

?>