<?php

include ('nusoap/truckstop_nusoap.php');
include ('data/truckstop_equipment.php');

$originCity         = $_POST['origin_city'];
$originState        = $_POST['origin_state'];
$originCountry      = 'USA';
$destinationCity    = $_POST['destination_city'];
$destinationState   = $_POST['destination_state'];
$destinationCountry = 'USA';
$equipmentType      = $equipmentTypes[$_POST['equipment_type']];
$orderId            = $_POST['quote_info_id'];
$pickupDate         = ($_POST['pickup_date'] != '') ? Date('Y-m-d', strtotime($_POST['pickup_date'])) : Date('Y-m-d', strtotime("+1 days"));
$pickupTime         =  '00:00:00';
$deliveryDate       =  Date('Y-m-d', strtotime("+1 days"));
$deliveryTime       = '00:00:00';
$distance           =  '0';
$weight             = ($_POST['weight'] != '') ? $_POST['weight'] : '0';
$stops              = null;
$charge             = ($_POST['charge'] != '') ? round($_POST['charge']) : '0';

// echo "originCity :".$originCity.'<br>';
// echo "originState :".$originState.'<br>';
// echo "originCountry :".$originCountry.'<br>';
// echo "destinationCity :".$destinationCity.'<br>';
// echo "destinationState :".$destinationState.'<br>';
// echo "destinationCountry :".$destinationCountry.'<br>';
// echo "equipmentType :".$equipmentType.'<br>';
// echo "orderId :".$orderId.'<br>';
// echo "pickupDate :".$pickupDate.'<br>';
// echo "pickupTime :".$pickupTime.'<br>';
// echo "deliveryDate :".$deliveryDate.'<br>';
// echo "deliveryTime :".$deliveryTime.'<br>';
// echo "distance :".$distance.'<br>';
// echo "weight :".$weight.'<br>';
// echo "charge :".$charge.'<br>';
// exit;
// $originCity         = 'Chicago';
// $originState        = 'IL';
// $originCountry      = 'USA';
// $destinationCity    = 'Las Vegas';
// $destinationState   = 'NM';
// $destinationCountry = 'USA';
// $equipmentType      = 'F';
// $orderId            = '111';
// $pickupDate         =  Date('Y-m-d', strtotime("+1 days"));
// $pickupTime         = '00:00:00';
// $deliveryDate       =  Date('Y-m-d', strtotime("+1 days"));
// $deliveryTime       = '00:00:00';
// $distance           = '0';
// $weight             = '0';
// $stops              =  null;
// $charge             = '0';


// production
// $wsdl = 'http://webservices.truckstop.com/v13/posting/LoadPosting.svc?singleWsdl';
$wsdl = 'http://testws.truckstop.com:8080/V13/Posting/LoadPosting.svc?singleWsdl';
// test
// $wsdl = 'http://testws.truckstop.com:8080/V13/Posting/LoadPosting.svc?singleWsdl';

$client = new nusoap_client($wsdl, 'wsdl');
// echo "<pre>";
// var_dump($client);exit;

// if($equipmentType && $orderId && $originCity && $originState && $destinationCity && $destinationState)
// {
	
$err   = $client->getError();
 // var_dump($err);exit;
if(!$err)
{
	// echo "string";exit;
	$integrationId  = '250261';
	$username       = 'REDDOGWS';
	$password       = 'R3D@!960';
	

	
	$loadParams  = null;
	$loadParams .= '<v11:PostLoads>';
	$loadParams .= '<v11:loads>';
    $loadParams .= '<web:IntegrationId>'.$integrationId.'</web:IntegrationId>';
    $loadParams .= '<web:Password>'.$password.'</web:Password>';
    $loadParams .= '<web:UserName>'.$username.'</web:UserName>';
    $loadParams .= '<web1:FullImport>false</web1:FullImport>';
    $loadParams .= '<web1:Handle>Dispatch</web1:Handle>';
	$loadParams .= '<web1:Loads>';
	$loadParams .= '<web2:Load>';
	$loadParams .= '<web2:DeliveryDate>'.$deliveryDate.'</web2:DeliveryDate>';
	$loadParams .= '<web2:DeliveryTime>'.$deliveryTime.'</web2:DeliveryTime>';
	$loadParams .= '<web2:DestinationCity>'.$destinationCity.'</web2:DestinationCity>';
    $loadParams .= '<web2:DestinationCountry>'.$destinationCountry.'</web2:DestinationCountry>';
    $loadParams .= '<web2:DestinationState>'.$destinationState.'</web2:DestinationState>';
	$loadParams .= '<web2:Distance>'.$distance.'</web2:Distance>';
	$loadParams .= '<web2:EquipmentID>0</web2:EquipmentID>';
	$loadParams .= '<web2:IsFavorite>false</web2:IsFavorite>';
	$loadParams .= '<web2:IsLoadFull>true</web2:IsLoadFull>';
	$loadParams .= '<web2:LoadId>0</web2:LoadId>';
	$loadParams .= '<web2:LoadNumber>'.$orderId.'</web2:LoadNumber>';
	$loadParams .= '<web2:OriginCity>'.$originCity.'</web2:OriginCity>';
    $loadParams .= '<web2:OriginCountry>'.$originCountry.'</web2:OriginCountry>';
    $loadParams .= '<web2:OriginState>'.$originState.'</web2:OriginState>';
	$loadParams .= '<web2:PaymentAmount>'.$charge.'</web2:PaymentAmount>';
	$loadParams .= '<web2:PickUpDate>'.$pickupDate.'</web2:PickUpDate>';
	$loadParams .= '<web2:PickUpTime>'.$pickupTime.'</web2:PickUpTime>';
	$loadParams .= '<web2:Quantity>1</web2:Quantity>';
	
	if($stops) {
		$loadParams .= '<web2:Stops>'.$stops.'</web2:Stops>';
	}
	
	$loadParams .= '<web2:TypeOfEquipment>'.$equipmentType.'</web2:TypeOfEquipment>';
	$loadParams .= '<web2:Weight>'.$weight.'</web2:Weight>';
	$loadParams .= '<web2:Width>0</web2:Width>';
	$loadParams .= '</web2:Load>';
	$loadParams .= '</web1:Loads>';
	$loadParams .= '</v11:loads>';
	$loadParams .= '</v11:PostLoads>';

	$result = $client->call('PostLoads', $loadParams);
	
	if ($client->fault){
		var_dump($result);exit;
		print_r($result);
		appendResponse($orderId, $result);
	}
	else
	{
		$err = $client->getError();
		var_dump($err);exit;
		if (!$err)
		{
			print_r($result);
			appendResponse($orderId, $result);
		}
	}
}
// }

function appendResponse($orderId, $result)
{
	file_put_contents('logs/truckstop.log', PHP_EOL.PHP_EOL.'Order ID: '.$orderId.PHP_EOL, FILE_APPEND);
	file_put_contents('logs/truckstop.log', print_r($result, TRUE), FILE_APPEND);	
}

echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';

