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

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

// Odette Label

$mHaulierRef 		= $_GET['haulierreference'];
$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'];

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

// Load data
function LoadData($mHost, $mUser, $mPwd, $mDbName, $mHaulierRef)
{
// 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 receipt_header.document_reference AS FLD01, receipt_header.customer_reference AS customer_reference, receipt_body.part_number AS FLD05,part.part_description AS FLD16, receipt_detail.serial_reference AS FLD12, receipt_detail.ran_order AS FLD03, receipt_detail.received_qty AS FLD06, vendor_code AS FLD07, vendor_name AS FLD17, CASE WHEN inventory_master.inspect=1 THEN 'Y' ELSE '' END AS inspect, CASE WHEN inventory_master.decant=1 THEN 'Y' ELSE '' END AS decant, product_type.product_type_code AS product_name, CASE WHEN part.count_on_rcpt =1 THEN 'Y' ELSE '' END AS count FROM receipt_header LEFT JOIN product_type ON receipt_header.product_type_id=product_type.id LEFT JOIN receipt_body ON receipt_header.id=receipt_body.receipt_header_id LEFT JOIN part ON receipt_body.part_number=part.part_number LEFT JOIN receipt_detail ON receipt_body.id=receipt_detail.receipt_body_id LEFT JOIN inventory_master ON receipt_detail.part_number=inventory_master.part_number AND receipt_detail.serial_reference=inventory_master.serial_reference LEFT JOIN vendor ON receipt_header.vendor_id=vendor.id WHERE haulier_reference='".$mHaulierRef."'";

// mysql query construct
	if (!mysqli_query($mConnection,$mDataQuery))
		exit("FAIL-".mysqli_error($mConnection).":".$mdataQuery);
	$mData = mysqli_query($mConnection,$mDataQuery);
	$i = 0;
	while($mRow = mysqli_fetch_assoc($mData)) 
	{
		$data[] =($mRow);
		$i++;
	}
	mysqli_close($mConnection);
	return $data;
}

// odette label
function odetteLabel($mLabel, $data)
{
	print_r("Label:".$mLabel);
// Load Label Template
	$mPrintData = $mLabel;
//	print_r("Test-".$printData);

	$mFh = fopen($mPrintData, 'r');
	$mLabel = fread($mFh, filesize($mPrintData));
	fclose($mFh);
// Add NewLine Characters
	$lines=explode(PHP_EOL,$mLabel);
	$mContent = "";
	foreach($lines as $line)
	{
		$mContent .=$line."\n";
	}
//	print_r("Layout:".$mContent);
// Data
	foreach($data as $row)
	{
		$mLabelContent = $mContent;
		$i = 0;
		foreach($row as $k=>$v)
		{
// Replace Template placeholder with Value
			$mLabelContent = str_replace($k,$v,$mLabelContent);
			$i++;
		}

// Send Label to Printer
		$fp = fsockopen("172.16.1.132", 9100, $errno, $errstr, 30);
		fputs ($fp,"\x02L\n");
		fputs ($fp,"H08\n");
		fputs ($fp,"D11\n");
		fprintf($fp,$mLabelContent);
		fputs ($fp,"E\n"); 
		
		print_r("Data:".$mLabelContent);
	}
	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, $mHaulierRef);
odetteLabel($mLabel, $data);

?>