<?php
$databaseUserName = '
insert_databaseusername';
$databasePassword = '
insert_databasepassword';
$databaseName = '
insert_databasename';
function fetchOrders()
{
//echo"Pre connect";
mysql_connect(localhost,$databaseUserName,$databasePassword);
//echo "attempting to select database";
@mysql_select_db($databaseName) or die( "Unable to select database");
$query="SELECT * FROM xcart_orders where status in ('P','Q') and LENGTH(tracking) > 3";
//echo "Executing query";
$result=mysql_query($query);
$num=mysql_numrows($result);
return $result;
mysql_close();
}
function checkOrderStatus()
{
$orders = fetchOrders();
$num=mysql_numrows($orders);
$i = 0;
if ($num > 0)
{
echo ('<table border="1">
<tr>
<th style="background-color:blue; color: gold">Order ID</th>
<th style="background-color:blue; color: gold">Customer</th>
<th style="background-color:blue; color: gold">Status</th>
<th style="background-color:blue; color: gold">Date</th>
<th style="background-color:blue; color: gold">Location</th>
<th style="background-color:blue; color: gold">Destination</th>
<th style="background-color:blue; color: gold">Shipping Type</th>
</tr>');
}
while ($i < $num)
{
$id = mysql_result($orders,$i,"orderid");
$trackingNumber = mysql_result($orders,$i,"tracking");
$destination = strtoupper(mysql_result($orders,$i,"s_city"));
$s_state = strtoupper(mysql_result($orders,$i,"s_state"));
$shippingType = mysql_result($orders,$i,"shipping");
$customer = mysql_result($orders,$i,"lastname");
$customerFirstName = mysql_result($orders, $i, "firstname");
$dispatchedEmailSent = mysql_result($orders,$i, "notes");
$emailAddress = mysql_result($orders,$i, "email");
$orderBaseURL = 'https://
www.mydomain.com/admin/order.php?orderid=';
//echo $id . ": " . $trackingNumber;
if (isDelivered($trackingNumber, $lastStatus, $lastDate, $lastLocation, $url))
{
mysql_connect(localhost,$databaseUserName,$databasePassword);
//echo "attempting to select database";
@mysql_select_db($databaseName) or die( "Unable to select database");
//echo " CLOSING ORDER #" . $id ;
$query="UPDATE xcart_orders SET status='C' WHERE orderid='$id'";
//echo " | " . $query;
mysql_query($query);
echo " >>> " . mysql_affected_rows() . " <<< ";
echo mysql_error();
mysql_close();
}
else
{
echo ("<tr><td><a href=\"{$orderBaseURL}{$id}\" target=\"_blank\">{$id}</a></td><td><a href=\"<a href="mailto:{$emailAddress}\">{$customer}</a></td><td><a">mailto:{$emailAddress}\">{$customer}</a></td><td><a href=\"{$url}\" target=\"_blank\">{$lastStatus}</a></td><td>{$lastDate}</td><td>{$lastLocation}</td><td>{$destination}, {$s_state}</td><td>{$shippingType}</td></tr>");
if (!preg_match("/Shipping Email Sent/", $dispatchedEmailSent, $sent))
{
$customer = ucwords(strtolower($customer));
$customerFirstName = ucwords(strtolower($customerFirstName));
$to = $emailAddress;
$subject = "MyCompanyName - Order Shipped";
$message = "<p>Good afternoon {$customerFirstName} {$customer}, <br /><br />Your order has been despatched by
MyCompanyName.</p>
<p>As of <b><span style=\"color: blue;\">{$lastDate}</span></b>,USPS recorded the last tracking event as <b><span style=\"color: blue;\">{$lastStatus}</span></b> in <b><span style=\"color: blue;\">{$lastLocation}</span></b>.</p><p>You can track your order status here:
http://auspost.com.au/track/track.html?id={$trackingNumber}</p><p><superscript>***</superscript>Please note that occasionally Australia Post will neglect to scan your item at the time of lodgement. This may result in 'No events found' being displayed. Generally, after the first scan at lodgement, no further updates are provided until the day of delivery.</p><p>Thank you for shopping with
My company Name</p>";
$header = "From:
sales@yourdomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
//$retval = mail ($to,$subject,$message,$header);
$retval = false; // DELETE ME WHEN FINAL VERSION
if( $retval == true )
{
date_default_timezone_set('
Australia/Melbourne');
$timeStamp = date('m/d/Y h:i:s a', time());
echo "Message sent successfully...";
mysql_connect(localhost,$databaseUserName,$databasePassword);
//echo "attempting to select database";
@mysql_select_db($databaseName) or die( "Unable to select database");
//echo " CLOSING ORDER #" . $id ;
$query="UPDATE xcart_orders SET notes='{$dispatchedEmailSent} \r\n <<< Shipping Email Sent @ {$timeStamp} >>> \r\n' WHERE orderid='$id'";
//echo " | " . $query;
mysql_query($query);
echo mysql_error();
mysql_close();
}
else
{
echo "Message could not be sent...";
}
}
}
$i++;
}
if ($num > 0) echo ('</table>');
echo $num . " orders currently in shipment";
}
function get_content($url)
{
$ch = curl_init();
$fakeAgent = 'IE 6 - Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322';
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT,$fakeAgent ) ;
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
}
function getElementsByClassName(\DOMDocument $DOMDocument, $ClassName) {
$Elements = $DOMDocument->getElementsByTagName("*");
$Matched = array();
for($i=0;$i<$Elements->length;$i++) {
if($Elements->item($i)->attributes->getNamedItem('class')->nodeValue == $ClassName) {
$Matched[]=$Elements->item($i);
}
}
return $Matched;
}
function isDelivered($trackingNumber, &$lastStatus, &$lastDate, &$lastLocation, &$url)
{
//$trackingNumber = '9405510200829774216112'; // TEMP!!!
$url = 'https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1=' . $trackingNumber;
$content = get_content($url);
$doc = new DOMDocument();
@$doc->loadHTML($content);
$cells = getElementsByClassName($doc, 'td-status');
$status = $cells[0]->nodeValue;
// $event[] = $cell->nodeValue;
// echo $status;
$cells = getElementsByClassName($doc, 'date-time sortable');
if (cells-count())
$date = $cells[0]->nodeValue;
// echo $date;
$cells = getElementsByClassName($doc, 'location sortable');
if (cells-count())
$location = $cells[0]->nodeValue;
// echo $location;
if ($status == "Delivered")
return true;
else
{
$lastStatus = strlen($status) > 1 ? $status : "No Tracking Information";
$lastDate = strlen($date) > 1 ? $date : "N/A";
$lastLocation = strlen($location) > 1 ? $location : "N/A";
//foreach ($event as $e)
//{
// if ($e == "Delivered") return true;
//}
}
return false;
}
checkOrderStatus();
//$delivered = isDelivered('0', $status, $lastDate, $lastLocation, $url);
//echo $tbody->length;
?>