<?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 = new stdClass();
	 $itemClass = new stdClass();
	 $optionsarr = new stdClass();
	 $fixingsarr = new stdClass();
	 $sizingarr = new stdClass();
	 
	 $optionsarr1 = array();
	 $fixingsarr1 = array();
	 $entries = array();
	 $itear = new stdClass();
	 
	 $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';
     $params->storeName = $nt[linkpos_store_name];
	 $params->storePassword = $nt[linkpos_store_pwd];
	 $params->partnerName = $nt[linkpos_partner_name];
	 $params->partnerPassword = $nt[linkpos_partner_pwd];
	 }

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


		 $query2="Select * from guestLogin where UserKey='" . $nt[UserKey] . "'";
		 $rt2=mysql_query($query2);
		 echo mysql_error();
		 
		 while($nt2=mysql_fetch_array($rt2)){

			  $params->customer->customerNumber  = $nt[UserKey];
			 $address = $nt2[FirstName] . ' ' . $nt2[LastName];
			 $params->customer->firstName =  $nt2[FirstName];
			 $params->customer->lastName =  $nt2[LastName];
			 if (!empty($nt2[CompanyName])){$address .= '<br>' . $nt2[CompanyName];}
			 $params->customer->address1=  $nt2[Address1] ;
			 $params->customer->address2= $nt2[Address2];
			 $params->customer->city=  $nt2[City];
			 $params->customer->state=  $nt2[State];
			 $params->customer->zipCode=  $nt2[Zip];
			 $params->customer->phoneNumber = $nt2[Phone];
			 $params->customer->emailAddress= $nt2[Email];
			 $cardtype = $nt2[cardType];
			 $cardmonth = $nt2[cardExpMonth];
			 $cardyear = $nt2[cardExpYear];
			 $cardnumber = $nt2[cardNumber];
			 $cardsec = $nt2[cardSec];
			 $cardholder = $nt2[cardholder];
			 $cardzip = $nt2[cardzip];
			
		}

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


	
		$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)){
			
			$qty_field =$nt[qty];
			$query2 = "Select itemName, itemPrice, taxable, pizzaTopping From menuItem Where itemKey = '" . $nt[itemNumber] . "'";
			$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->Entries->TicketEntry->itemKey = $nt[itemNumber];
				    $params->Entries->TicketEntry->name = $nt2[itemName];
					$params->Entries->TicketEntry->quantity = $qty_field;
					$params->Entries->TicketEntry->price = $nt2[itemPrice];
					$params->Entries->TicketEntry->taxable = $nt2[taxable];
					$params->Entries->TicketEntry->discountCode = '';
					$params->Entries->TicketEntry->discountValue = '0';
					$optionsarra = new stdClass();
					$optionsarrb = new stdClass();
					$optionsarrc = new stdClass();
					$icc = 0;
					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->Entries->TicketEntry->options[$icc]->EntryOption->optionKey= $ntop['okey'];
							$params->Entries->TicketEntry->options[$icc]->EntryOption->price =  $ntop['oprice'];
							$params->Entries->TicketEntry->options[$icc]->EntryOption->name =$choicec;
						} 
						$icc++;
					}
					else{
						$params->Entries->TicketEntry->options[$icc]->EntryOption->optionKey= '';
						$params->Entries->TicketEntry->options[$icc]->EntryOption->price =  '';
						$params->Entries->TicketEntry->options[$icc]->EntryOption->name ='';
						$icc++;
						}

					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->Entries->TicketEntry->options[$icc]->EntryOption->optionKey = $ntop['okey'];
							$params->Entries->TicketEntry->options[$icc]->EntryOption->price =  $ntop['oprice'];
							$params->Entries->TicketEntry->options[$icc]->EntryOption->name =$choiceb;
							$icc++;
						} 
					}

					
					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->Entries->TicketEntry->options[$icc]->EntryOption->optionKey = $ntop['okey'];
							$params->Entries->TicketEntry->options[$icc]->EntryOption->price =  $ntop['oprice'];
							$params->Entries->TicketEntry->options[$icc]->EntryOption->name =$choicec;
							$icc++;
						} 
					}

				} 
			}


				//************if a size is selected, look up price for selected size
				if($nt[size]!==""){	
					$sizingarr->size = $nt[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)){
						$sizingarr->price = $nt3[price];
					} 
					$params->Entries->TicketEntry->size = $sizingarr;
					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){
               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='';
					}
					$fixingsarr = new stdClass();
					if($name!=''){
					$params->Entries->TicketEntry->fixings->EntryFixing->fixingKey = $nt5[itemNumber];
					$params->Entries->TicketEntry->fixings->EntryFixing->price = number_format(($qty * $price), 2, '.', ',');
					$params->Entries->TicketEntry->fixings->EntryFixing->name =$name.' '.$half;
					}
if($fixingsarr !=''){
					
}
				}     
      //end of topping loop
	
			}
			$itear->TicketEntry = array($itear->TicketEntry,$params->Entries->TicketEntry);

		}
			/*		$preventry = array();
				$preventry[] = $params['Entries']['TicketEntry'] ;
			$params['Entries'] = $itear;*/
			


	}

}

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


try{

//$result = $client->CreateTicket($params);
//$result = $client->CreateTicket('company','123','pass','1233','8','Mohan','','','','','','12','12 06 91','HIya',$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)$reslt->success;
	if($success=='false'){
		$errorMessage =(string)$reslt->errorMessage;
		echo $errorMessage . "\n";
		$approved=0;
	} else {
		
		echo "tested ok...";
	}
}

?>

