<?php
	//require_once ('includes/mysql_connect.php');

    include ('../cmsadmin/includes/db1config.php');
    include ('../includes/mysql_connect.php');
   // include ('../cmsadmin/includes/admin-config.php');
	//if $allow_order_edits = 1, add controls to change the order
	//echo "allow editing: ".$allow_order_edits;
	include('../cmsadmin/menudatabase.php');

	$ordernumber = "P841643A";
	if(isset($_REQUEST['onum'])){
		$ordernumber = $_REQUEST['onum'];	
	}
	$ordertable = "orders";
	$itemtable = "orderItem";

	 //include main order info
	 $params = '<?xml version="1.0"?><createTicket>';

	 
	 $query="select linkpos_partner_name, linkpos_partner_pwd, linkpos_company, linkpos_store_name, linkpos_store_pwd from storePrefs";
	 $rt=mysql_query($query);
	 while($nt=mysql_fetch_array($rt)){
	 $params .= '<companyName>Demo Company</companyName>';
     $params .= '<storeName>'.$nt[linkpos_store_name].'</storeName>';
	 $params .= '<storePassword>'.$nt[linkpos_store_pwd].'</storePassword>';
	 $params .= '<partnerName>'.$nt[linkpos_partner_name].'</partnerName>';
	 $params .= '<partnerPassword>'.$nt[linkpos_partner_pwd].'</partnerPassword>';
	 }

     $query = "SELECT * FROM $ordertable where orderNumber = '" . $ordernumber . "'";
	 $rt=mysql_query($query);
	 echo mysql_error();     	
	 while($nt=mysql_fetch_array($rt)){		 
		 $params .= '<ticketNum>'.$nt[orderNumber].'</ticketNum>';
		 $params .= '<register>1</register>';
		 $params .= '<employee></employee>';
		 //$params .= '<discountCode></discountCode>';
		// $params .= '<discountValue>0</discountValue>';
		 $params .= '<couponID></couponID>';
		 $params .= '<table></table>';
		 $params .= '<note>'.$nt[store_comments].'</note>';
		 $params .= '<tag>AN123456</tag>';
		 $params .= '<date>'.date ('h:i', strtotime($nt[orderTime])) .' '. date ('l F j, Y', strtotime($nt[orderTime])).'</date>';
		 $params .= '<menuKey>1</menuKey>';
		 $params .= '<orderType>orderType</orderType>';
		 $paymentType = $nt[payMethod];

		 $params .= '<serviceDate>'.date ('h:i', strtotime($nt[orderTime])) .' '. date ('l F j, Y', strtotime($nt[orderTime])).'</serviceDate>';
      	 $params .='<ticketPayments>';
		 	$params .='<TicketPayment>';
				$params .= '<paymentType>'.$nt[payMethod].'</paymentType>';
				$params .= '<paymentNumber>'.$nt[orderNumber].'</paymentNumber>';
				$params .= '<paymentAmount>'.$nt[orderTotal].'</paymentAmount>';
				$params .= '<tipAmount>'.$nt[tipamount].'</tipAmount>';
			$params .='</TicketPayment>';
		 $params .='</ticketPayments>';

		 


		 $query2="Select * from guestLogin where UserKey='" . $nt[UserKey] . "'";
		 $rt2=mysql_query($query2);
		 echo mysql_error();
		 
		 while($nt2=mysql_fetch_array($rt2)){
			 $address = $nt2[FirstName] . ' ' . $nt2[LastName];
			 if (!empty($nt2[CompanyName])){$address .= '<br>' . $nt2[CompanyName];}
			 $cardtype = $nt2[cardType];
			 $cardmonth = $nt2[cardExpMonth];
			 $cardyear = $nt2[cardExpYear];
			 $cardnumber = $nt2[cardNumber];
			 $cardsec = $nt2[cardSec];
			 $cardholder = $nt2[cardholder];
			 $cardzip = $nt2[cardzip];
		 $params .='<customer>';
			$params .= '<customerNumber>'.$nt2[UserKey].'</customerNumber>';
			$params .= '<firstName>'.$nt2[FirstName].'</firstName>';
			$params .= '<lastName>'.$nt2[LastName].'</lastName>';
			$params .= '<address1>'.$nt2[Address1].'</address1>';
			$params .= '<address2>'.$nt2[Address2].'</address2>';
			$params .= '<city>'.$nt2[City].'</city>';
			$params .= '<state>'.$nt2[State].'</state>';
			$params .= '<zipCode>'.$nt2[Zip].'</zipCode>';
			$params .= '<phoneNumber>'.$nt2[Phone].'</phoneNumber>';
			$params .= '<emailAddress>'.$nt2[Email].'</emailAddress>';
		 $params .='</customer>';

			 
			
		}

		
		$query = "Select salesTax, deliveryCharge from storePrefs";
		$rt=mysql_query($query);
		echo mysql_error();  
		if (mysql_num_rows($rt) !== 0) {
			while($nt=mysql_fetch_array($rt)){
				$tax = $nt[salesTax];
				if ($delivery==1) {
					$deliverychg = $nt[deliveryCharge];
				}
				else {
         			  $deliverychg = 0;
				}
			}
		} 
		else {
			$tax = 0;
			$deliverychg = 0;
			}
		
		mysql_close;


		$gtotal = 0;
		$totaltax = 0;

		$params .= '<Entries>';
	
		$query = "Select * from $itemtable where orderNumber = '" . $ordernumber . "' AND crust=0  AND parentLineNum=0 AND price_adjust ='0.00'";
		$rt=mysql_query($query);
		echo mysql_error(); 
		if (mysql_num_rows($rt) == 0) {}
		else {	
			if(!empty($menudatabase)){	//select shared menu database for store for prices and detail
				include ('includes/db2config.php');
				include ('includes/mysql_connect.php');
			}
		
		while($nt=mysql_fetch_array($rt))
		{
			$params .= '<TicketEntry>';
			$qty_field =$nt[qty];
			$inumber = $nt[itemNumber];

				} 
			}


mysql_close;
//echo $params;

$prod_url ="http://olympuspos.net/pos/posmenuetta.asmx?WSDL";
$test_url ="http://olypointofsale.com/postest/posmenuetta.asmx?WSDL";

$wsdl = $test_url;


$client = new soapclient($wsdl, array('trace' => 1));

//echo $params;
try{

//$result = $client->CreateTicket($params);
//$result = $client->CreateTicket('company','123','pass','1233','8','Mohan','','','','','','12','12 06 91','HIya',$params);
$params = new SimpleXMLElement($params);

$result = $client->CreateTicket('CreateTicket',$params);
}catch (SoapFault $e){
    print_r($e);
    // or other error handling
}

//$result = $client->CreateTicket($params);
/*
echo "<br><br>Returned from SOAP: ";
print_r($result);
*/

echo "<br><br>Request:<br>";
$xmlDoc = DOMDocument::loadXML($client->__getLastRequest());
$xmlDoc->formatOutput=true;
print '<pre>' . htmlentities($xmlDoc->saveXML()).'</pre>';

echo "<br><br>Response:<br>";

$xmlDoc = DOMDocument::loadXML($client->__getLastResponse());
$obj = simplexml_load_string($client->__getLastResponse());
$xmlDoc->formatOutput=true;
print '<pre>' . htmlentities($xmlDoc->saveXML()).'</pre>' . "\n";

echo 'This is a Result Printed here' . "\n" . "\n";

foreach($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://olympuspos.net')->CreateTicketResponse->CreateTicketResult as $reslt)
{
	$success = (string)$result->success;
	if($success=='false'){
		$errorMessage =(string)$reslt->errorMessage;
		echo $errorMessage . "\n";
		$approved=0;
	} else {
		
		echo "tested ok...";
	}
}

?>

