<?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 = "P841643B";
	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>';
		 

		 //$params .= '<serviceDate>'.date ('h:i', strtotime($nt[orderTime])) .' '. date ('l F j, Y', strtotime($nt[orderTime])).'</serviceDate>';
		  //if(!empty($nt[payMethod])){$paymentType="payment".$nt[payMethod];} //format as "paymentCredit" or "paymentCash"
		  $paymentType = $nt[payMethod];
      	 $params .='<ticketPayments>';
		 	$params .='<TicketPayment>';
				$params .= '<paymentType>'.$paymentType.'</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];
			$query2 = "Select itemName, itemPrice, taxable, pizzaTopping From menuItem Where itemKey = '" . $nt[itemNumber] . "'";
			//echo $query2;
			$rt2=mysql_query($query2);
			mysql_error();                    // show any errors
			if (mysql_num_rows($rt2) !== 0) 
			{
				while($nt2=mysql_fetch_array($rt2))
				{
				
					$price = $nt2[itemPrice];
					$taxable = $nt2[taxable];
					$itemName = $nt2[itemName];
					$itemNum =  $nt[itemNumber];
					$params .= '<itemKey>'.$inumber.'</itemKey>';
					$params .= '<name>'.$itemName.'</name>';
					$params .= '<quantity>'.$qty_field.'</quantity>';
					if($nt[size]!=="")
					{	
						$query3 = "Select price from itemSize where itemKey = '" . $nt[itemNumber] . "' AND size = '"  . $nt[size] . "'" ;
						$rt3=mysql_query($query3);    
						echo mysql_error();                   
							while($nt3=mysql_fetch_array($rt3))
							{
								$params .= '<price>'.$nt3['price'].'</price>';
							} 
						mysql_close;
					} 
					//$params .= '<price>'.$nt2[itemPrice].'</price>';
					$params .= '<taxable>'.$nt2[taxable].'</taxable>';
					$params .= '<discountCode></discountCode>';
					$params .= '<discountValue>0</discountValue>';
					
					if(!empty($nt[choiceca]) || !empty($nt[choiceb]) || !empty($nt[choicec]))
					{
						$params .= '<options>';
					}

					if(!empty($nt[choicea]))
					{ 
						$choicea = $nt[choicea];
						$queryop = 'SELECT b.price as oprice, b.optionkey as okey FROM menuItem a, options b WHERE a.itemName =  "'.$choicea.'" AND a.itemKey = b.itemkey AND b.parentkey =  "'.$itemNum.'"';
						$rtop=mysql_query($queryop);          // query executed 
						echo mysql_error();                    // if any error is there that will be printed to the screen
						while($ntop=mysql_fetch_array($rtop))
						{
							$params .= '<EntryOption>';
							$params .= '<optionKey>'.$ntop['okey'].'</optionKey>';
							$params .= '<price>'.$ntop['oprice'].'</price>';
							$params .= '<name>'.$choicea.'</name>';
							$params .= '</EntryOption>';
						} 
						
					}

					if(!empty($nt[choiceb]))
					{ 
						$choiceb = $nt[choiceb];
						$queryop = 'SELECT b.price as oprice, b.optionkey as okey FROM menuItem a, options b WHERE a.itemName =  "'.$choiceb.'" AND a.itemKey = b.itemkey AND b.parentkey =  "'.$itemNum.'"';

						$rtop=mysql_query($queryop);          // query executed 
						echo mysql_error();                    // if any error is there that will be printed to the screen
						while($ntop=mysql_fetch_array($rtop))
						{
							$params .= '<EntryOption>';
							$params .= '<optionKey>'.$ntop['okey'].'</optionKey>';
							$params .= '<price>'.$ntop['oprice'].'</price>';
							$params .= '<name>'.$choiceb.'</name>';
							$params .= '</EntryOption>';
						} 
					}

					
					if(!empty($nt[choicec]))
					{ 
						$choicec = $nt[choicec];
						$queryop = 'SELECT b.price as oprice, b.optionkey as okey FROM menuItem a, options b WHERE a.itemName =  "'.$choicec.'" AND a.itemKey = b.itemkey AND b.parentkey =  "'.$itemNum.'"';
						$rtop=mysql_query($queryop);          // query executed 
						echo mysql_error();                    // if any error is there that will be printed to the screen
						while($ntop=mysql_fetch_array($rtop))
						{
							$params .= '<EntryOption>';
							$params .= '<optionKey>'.$ntop['okey'].'</optionKey>';
							$params .= '<price>'.$ntop['oprice'].'</price>';
							$params .= '<name>'.$choicec.'</name>';
							$params .= '</EntryOption>';
						} 
					}
					
					if(!empty($nt[choiceca]) || !empty($nt[choiceb]) || !empty($nt[choicec]))
					{
						$params .= '</options>';
					}

				} 
			}


				//************if a size is selected, look up price for selected size
				if($nt[size]!=="")
				{	
					$params .= '<size>';
					$params .= '<size>'.$nt['size'].'</size>';
					$guesthtml.='&nbsp;&nbsp;' . $nt[size];
					$query3 = "Select price from itemSize where itemKey = '" . $nt[itemNumber] . "' AND size = '"  . $nt[size] . "'" ;
					$rt3=mysql_query($query3);    
					echo mysql_error();                   
					while($nt3=mysql_fetch_array($rt3))
					{
						$params .= '<price>'.$nt3['price'].'</price>';
						$params .= '</size>';
					} 
							
							
					mysql_close;
				} 

				if($taxable==1){
						$totaltax = number_format($totaltax + ($tax * ($price * $nt[qty])), 2, '.', ',');
				}
				
			 


            if(!empty($menudatabase))
			{
                include ('includes/db1config.php');
                include ('includes/mysql_connect.php');
            }

 			$query5 = "Select * FROM $itemtable where parentLineNum = '" . $nt[recordKey] . "' order by crust DESC, halfA DESC, halfB DESC ";
			$rt5=mysql_query($query5); 
			echo mysql_error();                    
			if(mysql_num_rows!==0)
			{
				$params .='<fixings>';
               if(!empty($menudatabase))
			   {	//select shared menu database for store to access local store settings
               		include ('includes/db2config.php');
                    include ('includes/mysql_connect.php');
               }
			   $icc = 1;
				while($nt5=mysql_fetch_array($rt5))
				{
					$query2 = "Select itemName, itemPrice, taxable From menuItem Where itemKey = '" . $nt5[itemNumber] . "'";
					$rt2=mysql_query($query2);
					echo mysql_error();

						while($nt2=mysql_fetch_array($rt2))
						{
			            	$price = $nt2[itemPrice];
                          		 if($nt[size]!==""){
            				  		 $query3 = "Select price from itemSize where itemKey = '" . $nt5[itemNumber] . "' AND size = '"  . $nt[size] . "'" ;
							  		 $rt3=mysql_query($query3);          // query executed
		   	 			      		 echo mysql_error();                    // if any error is there that will be printed to the screen
					      	   		 while($nt3=mysql_fetch_array($rt3)){
			                       		 $price = $nt3[price];
	        			    		 }
					       	   		 mysql_close;
				     			}
					 			$taxable = $nt2[taxable];
					 			$name = $nt2[itemName];
					 			$qty = $nt5[qty];
				 		}
				   
				   if($nt5[halfA]=='1')
				   {
					   $half= ' (Left half only)';
					} 
					elseif($nt5[halfB]=='1')
					{
						$half= ' (Right half only)';
					
					}
					else{
						$half='';
					}
		
					if($name!=''){
						$params .= '<EntryFixing>';
						$params .= '<fixingKey>'.$nt5['itemNumber'].'</fixingKey>';
						$params .= '<price>'. number_format(($qty * $price), 2, '.', ',').'</price>';
						$params .= '<name>'.$name.' '.$half.'</name>';
						$params .= '</EntryFixing>';

					}
if($fixingsarr !=''){
					
}
				}     
      //end of topping loop
	$params .='</fixings>';
	$params .='</TicketEntry>';
			}


		}
$params .='</Entries></createTicket>';
			


	}

}

mysql_close;


$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...";
	}
}

?>

