<?php
session_start();
/*if($_SESSION['adminkey'] !="security"){
	echo"You must be logged in as an administrator in order to export your menu.";
	exit;
}*/

//open db connection
$host = 'localhost'; 
$username = 'restaura_taste';
include('../custom/db1.php');
$password = 'Daughters2'; 
include ('../includes/mysql_connect.php');
$server = $_SERVER['SERVER_NAME'];
$image_path = $server . "/images/menu/";


$orderNum = "P841643";
if(isset($_REQUEST['order_num']))
{
	$orderNum = $_REQUEST['order_num'];	
}

$params = new stdClass();
/******** Credentials *************/
$query="select linkpos_partner_name, linkpos_partner_pwd, linkpos_company, linkpos_store_name, linkpos_store_pwd, salesTax from storePrefs";
$rt=mysql_query($query);
$salesTax = 0;
while($nt=mysql_fetch_array($rt))
{
	$params->companyName = $nt[linkpos_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];
	$params->menuKey = 1;
	$params->menuName = "TestMenu";
	$salesTax = (double)$nt['salesTax'];
	
	if((empty($nt[linkpos_company]))||(empty($nt[linkpos_store_name]))||(empty($nt[linkpos_store_pwd]))||(empty($nt[linkpos_partner_name]))||(empty($nt[linkpos_partner_pwd]))){
		echo $nt[linkpos_company].",". $nt[linkpos_store_name] . ",". $nt[linkpos_store_pwd].",". $nt[linkpos_partner_name].",". $nt[linkpos_partner_pwd];			
		echo"You need to setup your LinkPOS credentials before exporting your menu.";
		exit;
	}	
}

/******** Order Info **************/
/*****
      <ticketNumber>string</ticketNumber>
      <discountCode>string</discountCode>
      <discountValue>float</discountValue>
      <note>string</note>
      <date>string</date>
      <paid>boolean</paid>
      <paymentType>string</paymentType>
      <paymentAmount>float</paymentAmount>
      <tipAmount>float</tipAmount>
******/
$sql = "Select * from orders where orderNumber = '{$orderNum}'";
echo $sql;
$res = mysql_query($sql); 
if($res === false)
{
	echo "<br>Order not found";
	exit;
}

$orderRow = mysql_fetch_assoc($res);
$userKey = $orderRow['UserKey'];

$hasPaid = ($orderRow['paid'] == 1) ? true : false;

$params->ticketNumber = $orderNum;

//increment with each test
$params->paymentNumber = '3';

//$params['discountCode'] = "0";  //hold off on these... coupon codes are associated with items
$params->discountValue = 0;
$params->note = "no note";
//$params['note'] = $orderRow['note'];
$params->date = $orderRow['orderTime'];
//$params['paid'] = $hasPaid;
$params->paymentType = "Payment".$orderRow['payMethod'];
//$params->paymentAmount = $orderRow['paid'];
$params->paymentAmount = $orderRow['orderTotal'];
$params->tipAmount = $orderRow['tipamount'];


$serviceDate = $orderRow['schedule'];
if(strcmp($serviceDate, "now") === 0)
{
	$serviceDate = $orderRow['orderTime'];
}

/************ Ticket Entries ************/
$entries = array();

$query= "SELECT * FROM orderItem LEFT JOIN menuItem 
         on orderItem.itemNumber = menuItem.itemKey 
		 WHERE orderNumber = '{$orderNum}'";

$rt=mysql_query($query);
while($irow=mysql_fetch_array($rt))
{
    echo "hello";
	$itemClass = new stdClass();
/*
          <itemKey>int</itemKey>
          <name>string</name>
          <quantity>float</quantity>
          <price>float</price>
          <taxable>float</taxable>
          <discountCode>string</discountCode>
          <discountValue>float</discountValue>
          <size>
            <size>string</size>
            <price>float</price>
          </size>
          <options>
            <EntryOption xsi:nil="true" />
            <EntryOption xsi:nil="true" />
          </options>
          <fixings>
            <EntryFixing xsi:nil="true" />
            <EntryFixing xsi:nil="true" />
          </fixings>
*/	
	$discountCode = "";
	$discountValue = 0;
	if(((float)$irow['upgrade_discount']) > 0)
	{
		$discountCode = "upgrade";
		$discountValue = ((float)$irow['upgrade_discount']);
	}
	if(((float)$irow['price_adjust']) !== 0)
	{
		
	}

	$itemClass->itemKey = $irow['itemNumber'];
	$itemClass->name = $irow['itemName'];
	$itemClass->quantity = $irow['qty'];
	$itemClass->price = $irow['itemPrice'];
	$itemClass->taxable = ((double)$irow['itemPrice']) * $salesTax;
	$itemClass->discountCode = $discountCode;
	$itemClass->discountValue = $discountValue;

	//Fixings
    //    $fixings['']
//        $EntryFixing = array();

 $queryFixings= "SELECT itemNumber FROM orderItem WHERE parentLineNum = '{$irow['recordKey']}'";
$EntryFixing =  new stdClass();
$itemNumbersFixings = mysql_query($queryFixings);
while($frow=mysql_fetch_array($itemNumbersFixings))
{
    $queryFixings2= "SELECT * FROM fixings WHERE fixingKey = '{$frow['itemNumber']}'";
$queryFixings2 = "Select itemName, itemPrice, taxable From menuItem Where itemKey = '" . $frow[itemNumber] . "'";
    $fixingItems = mysql_query($queryFixings2);

    while($fixingItem = mysql_fetch_array($fixingItems)){
        //price
        $EntryFixing->fixingKey = $frow[itemNumber];
        $EntryFixing->price = $fixingItem['itemPrice'];
        $EntryFixing->name = $fixingItems['itemName'];

    }

    $itemClass->EntryFixing = $EntryFixing;


}

 
	//Sizes
    $sizes = new stdClass();	
    $sizes->size = 'normal';
    $sizes->price = 11;
    $itemClass->size = $sizes;
    

	//Options
    $options =  new stdClass();



    $options->choicea = $irow['choicea'];
    $optionA  = mysql_fetch_array(mysql_query("select itemKey from menuItem where itemName = '$options->choicea'"));
    $OpsA=mysql_fetch_array(mysql_query("select optionkey, price from options where itemkey='{$optionA['itemKey']}' and choice='1'"));
   
    $optionsA->optionKey = $OpsA['optionkey'];

    $optionsA->name = $options->choicea;
    $optionsA->price = $OpsA['price'];
    


    $options->choiceb = $irow['choiceb'];
    $optionB  = mysql_fetch_array(mysql_query("select itemKey from menuItem where itemName = '$options->choiceb'"));
    $OpsB=mysql_fetch_array(mysql_query("select optionKey, price from options where itemKey='{$optionB['itemKey']}' and choice='2'"));

    $optionsB =  new stdClass();
    $optionsB->name = $options->choiceb;
    $optionsB->optionKey = $OpsB['optionkey'];
    $optionsB->price = $OpsB['price'];
    
    $options->choicec = $irow['choicec'];
    $optionC  = mysql_fetch_array(mysql_query("select itemKey from menuItem where itemName = '$options->choicec'"));
    $OpsC=mysql_fetch_array(mysql_query("select optionKey, price from options where itemKey='{$optionC['itemKey']}' and choice='3'"));
    $optionsC =  new stdClass();
    $optionsC->name = $options->choiceb;
    $optionsC->optionKey = $OpsC['optionkey'];
    $optionsC->price = $OpsC['price'];
    


//    $itemClass->a->EntryOption = $optionsA;
  //  $itemClass->b->EntryOption = $optionsB;
    //  $itemClass->c->EntryOption = $optionsC;
    //

$itemClass->EntryOption = array (
    $optionsA, $optionsB, $optionsC
);

 
	//add to entries array
	$entries[] = $itemClass;	
}

$params->Entries = $entries;


/********** Customer ************/
/*
      <customer>
        <customerNumber>string</customerNumber>
        <firstName>string</firstName>
        <lastName>string</lastName>
        <address1>string</address1>
        <address2>string</address2>
        <city>string</city>
        <state>string</state>
        <zipCode>string</zipCode>
        <phoneNumber>string</phoneNumber>
        <emailAddress>string</emailAddress>
      </customer>
*/
$customerObj = new stdClass();
$sql = "select * from guestLogin where UserKey = '{$userKey}'";
$res = mysql_query($sql);
if($res === false)
{
	echo "<br>Guest not found";
	//exit;
}
$orderGuest = mysql_fetch_assoc($res);

$customerObj->customerNumber = "21";
$customerObj->firstName = $orderGuest['FirstName'];
$customerObj->lastName = $orderGuest['LastName'];
$customerObj->address1 = $orderGuest['Address1'];
$customerObj->address2 = $orderGuest['Address2'];
$customerObj->city = $orderGuest['City'];
$customerObj->state = $orderGuest['State'];
$customerObj->zipCode = $orderGuest['Zip'];
$customerObj->phoneNumber = $orderGuest['Phone'];
$customerObj->emailAddress = $orderGuest['Email'];


$params->customer =  $customerObj;
$params->orderType = '';
$params->serviceDate = $serviceDate;


//$display = new Array_Functions();
//$display->display_hierarchy($params);	

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

$wsdl = $test_url;


try{
$client = new soapclient($wsdl, array('trace' => 1));
//$result = $client->CreateTicket($params);
$result = $client->CreateTicket('company','123','pass','1233','8','Mohan','','','','','','12','12 06 91','HIya',$params);
}catch (SoapFault $e){
    print_r($e);
    // or other error handling
}


/*
$client = new soapclient(NULL, array('location' => 'http://olypointofsale.com/postest/posmenuetta.asmx', 
           'uri' => 'http://olypointofsale.com/postest/posmenuetta.asmx?WSDL',
	        'trace' => 1,
      	     'use' => SOAP_LITERAL));		   
*/



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());
$xmlDoc->formatOutput=true;
print '<pre>' . htmlentities($xmlDoc->saveXML()).'</pre>';


?>
