";
echo "ShippingZClasses.php [".SHIPPINGZCLASSES_VERSION."]
";
echo "ShippingZOpencart.php [".SHIPPINGZOPENCART_VERSION."]
";
echo "ShippingZMessages.php [".SHIPPINGZMESSAGES_VERSION."]
";
echo "Please, make sure all of the above files are same version.";
exit;
}
// Check for bootstrap file and bootstrap the DB
if(Check_Include_File('ShippingZOpencartBootstrap.php'))
require('ShippingZOpencartBootstrap.php');
// Can now access DB with Opencart functions
############################################## Always Enable Exception Handler ###############################################
error_reporting(E_ALL);
ini_set('display_errors', '1');
set_error_handler("ShippingZ_Exception_Error_Handler");
############################################## Class ShippingZOpencart ######################################
class ShippingZOpencart extends ShippingZGenericShoppingCart
{
//cart specific functions goes here
############################################## Function Check_DB_Access #################################
//Check Database access
#######################################################################################################
function Check_DB_Access()
{
global $db;
//check if opencart database can be acessed or not
$shipping = $db->query('SHOW COLUMNS FROM `'.DB_PREFIX.'order`');
if ($shipping->num_rows)
{
$this->display_msg=DB_SUCCESS_MSG;
}
else
{
$this->display_msg=DB_ERROR_MSG;
}
}
############################################## Function GetOrderCountByDate #################################
//Get order count
#######################################################################################################
function GetOrderCountByDate($datefrom,$dateto)
{
global $db;
$order_status_filter=$this->PrepareOpencartOrderStatusFilter();
$datefrom_timestamp=$this->GetServerTimeLocal(false,$datefrom);
$dateto_timestamp=$this->GetServerTimeLocal(false,$dateto);
$sql = "SELECT o.order_id FROM `" . DB_PREFIX . "order` o";
$sql .= " WHERE " . $order_status_filter . "";
$sql .= " ( DATE_FORMAT(date_modified,\"%Y-%m-%d %T\") between '".$this->MakeSqlSafe($this->GetServerTimeLocal(true,$datefrom))."' and '".$this->MakeSqlSafe($this->GetServerTimeLocal(true,$dateto))."') OR ( DATE_FORMAT(date_added,\"%Y-%m-%d %T\") between '".$this->MakeSqlSafe($this->GetServerTimeLocal(true,$datefrom))."' and '".$this->MakeSqlSafe($this->GetServerTimeLocal(true,$dateto))."')";
$orders= $db->query($sql);
return $orders->num_rows;
}
############################################## Function UpdateShippingInfo #################################
//Update order status
#######################################################################################################
function UpdateShippingInfo($OrderNumber,$TrackingNumber,$ShipDate='',$ShipmentType='',$Notes='',$Carrier='',$Service='')
{
global $db;
$sql = "SELECT * FROM `".DB_PREFIX."order` WHERE order_id=".$this->MakeSqlSafe($OrderNumber,1);
$result = $db->query($sql);
//check if order number is valid
if($result->num_rows>0)
{
if($ShipDate!="")
$shipped_on=$ShipDate;
else
$shipped_on=date("m/d/Y");
if($Carrier!="")
$Carrier=" via ".$Carrier;
if($Service!="")
$Service=" [".$Service."]";
foreach ($result->rows as $order_row)
{
$current_order_status=$order_row['order_status_id'];
}
//prepare $comments & save it
$comments="Shipped on $shipped_on".$Carrier.$Service.", Tracking number $TrackingNumber";
if(OPENCART_SHIPPED_STATUS_SET_TO_STATUS_3_SHIPPED==1)
{
$db->query("insert into " .DB_PREFIX."order_history
(order_id, order_status_id, date_added, notify, comment)
values ('" . $this->MakeSqlSafe($OrderNumber,1) . "', '3', now(), '0', '" . $this->MakeSqlSafe($comments). "')");
//update order status
$db->query(" update `".DB_PREFIX."order` set order_status_id='3' where order_id='". $this->MakeSqlSafe($OrderNumber,1) ."'");
}
else
{
if($current_order_status==1)
$change_order_status=2;
else if($current_order_status==2)
$change_order_status=3;
else
$change_order_status=$current_order_status;
$db->query("insert into ".DB_PREFIX."order_history
(order_id, order_status_id, date_added, notify, comment)
values ('" . $this->MakeSqlSafe($OrderNumber,1) . "', '".$change_order_status."', now(), '0', '" . $this->MakeSqlSafe($comments). "')");
if($change_order_status!=$current_order_status)
$db->query(" update `".DB_PREFIX."order` set order_status_id='$change_order_status' where order_id='". $this->MakeSqlSafe($OrderNumber,1) ."'");
}
$this->SetXmlMessageResponse($this->wrap_to_xml('UpdateMessage',"Success"));
}
else
{
//display error message
$this->display_msg=INVAID_ORDER_NUMBER_ERROR_MSG;
$this->SetXmlError(1,$this->display_msg);
}
}
############################################## Function Fetch_DB_Orders #################################
//Perform Database query & fetch orders based on date range
#######################################################################################################
function Fetch_DB_Orders($datefrom,$dateto)
{
global $db;
$order_status_filter=$this->PrepareOpencartOrderStatusFilter();
$datefrom_timestamp=$this->GetServerTimeLocal(false,$datefrom);
$dateto_timestamp=$this->GetServerTimeLocal(false,$dateto);
$sql = "SELECT o.* FROM `" . DB_PREFIX . "order` o";
$sql .= " WHERE " . $order_status_filter . "";
$sql .= " ( DATE_FORMAT(date_modified,\"%Y-%m-%d %T\") between '".$this->MakeSqlSafe($this->GetServerTimeLocal(true,$datefrom))."' and '".$this->MakeSqlSafe($this->GetServerTimeLocal(true,$dateto))."') OR ( DATE_FORMAT(date_added,\"%Y-%m-%d %T\") between '".$this->MakeSqlSafe($this->GetServerTimeLocal(true,$datefrom))."' and '".$this->MakeSqlSafe($this->GetServerTimeLocal(true,$dateto))."')";
$result=$db->query($sql);
$counter=0;
foreach ($result->rows as $opencart_orders_row)
{
//prepare order array
$this->opencart_orders[$counter]->orderid=$this->GetFieldNumber($opencart_orders_row,"order_id");
//shipping details
$this->opencart_orders[$counter]->order_shipping["FirstName"]=$this->GetFieldString($opencart_orders_row,"shipping_firstname");
$this->opencart_orders[$counter]->order_shipping["LastName"]=$this->GetFieldString($opencart_orders_row,"shipping_lastname");
$this->opencart_orders[$counter]->order_shipping["Company"]=$this->GetFieldString($opencart_orders_row,"shipping_company");
$this->opencart_orders[$counter]->order_shipping["Address1"]=$this->GetFieldString($opencart_orders_row,"shipping_address_1");
$this->opencart_orders[$counter]->order_shipping["Address2"]=$this->GetFieldString($opencart_orders_row,"shipping_address_2");
$this->opencart_orders[$counter]->order_shipping["City"]=$this->GetFieldString($opencart_orders_row,"shipping_city");
$this->opencart_orders[$counter]->order_shipping["State"]=$this->GetFieldString($opencart_orders_row,"shipping_zone");
$this->opencart_orders[$counter]->order_shipping["PostalCode"]=$this->GetFieldString($opencart_orders_row,"shipping_postcode");
$this->opencart_orders[$counter]->order_shipping["Country"]=$this->GetFieldString($opencart_orders_row,"shipping_country");
$this->opencart_orders[$counter]->order_shipping["Phone"]=$this->GetFieldString($opencart_orders_row,"telephone");
$this->opencart_orders[$counter]->order_shipping["EMail"]=$this->GetFieldString($opencart_orders_row,"email");
//billing details
$this->opencart_orders[$counter]->order_billing["FirstName"]=$this->GetFieldString($opencart_orders_row,"payment_firstname");
$this->opencart_orders[$counter]->order_billing["LastName"]=$this->GetFieldString($opencart_orders_row,"payment_lastname");
$this->opencart_orders[$counter]->order_billing["Company"]=$this->GetFieldString($opencart_orders_row,"payment_company");
$this->opencart_orders[$counter]->order_billing["Address1"]=$this->GetFieldString($opencart_orders_row,"payment_address_1");
$this->opencart_orders[$counter]->order_billing["Address2"]=$this->GetFieldString($opencart_orders_row,"payment_address_2");
$this->opencart_orders[$counter]->order_billing["City"]=$this->GetFieldString($opencart_orders_row,"payment_city");
$this->opencart_orders[$counter]->order_billing["State"]=$this->GetFieldString($opencart_orders_row,"payment_zone");
$this->opencart_orders[$counter]->order_billing["PostalCode"]=$this->GetFieldString($opencart_orders_row,"payment_postcode");
$this->opencart_orders[$counter]->order_billing["Country"]=$this->GetFieldString($opencart_orders_row,"payment_country");
$this->opencart_orders[$counter]->order_billing["Phone"]=$this->GetFieldString($opencart_orders_row,"telephone");
$this->opencart_orders[$counter]->order_billing["EMail"]=$this->GetFieldString($opencart_orders_row,"email");
//order info
$this->opencart_orders[$counter]->order_info["OrderDate"]=$this->ConvertServerTimeToUTC(true,strtotime($this->GetFieldString($opencart_orders_row,"date_added")));
$this->opencart_orders[$counter]->order_info["OrderNumber"]=$this->GetFieldNumber($opencart_orders_row,"order_id");
$this->opencart_orders[$counter]->order_info["PaymentType"]=$this->ConvertPaymentType($this->GetFieldString($opencart_orders_row,"payment_method"));
$shipping_charge="";
$tax="";
$order_total_result =$db->query('SELECT * from '.DB_PREFIX.'order_total where order_id='.$this->GetFieldNumber($opencart_orders_row,"order_id"));
foreach ($order_total_result->rows as $opencart_order_total)
{
if($this->GetFieldString($opencart_order_total,"code")=="shipping")
$shipping_charge=$this->GetFieldNumber($opencart_order_total,"value");
if($this->GetFieldString($opencart_order_total,"code")=="tax")
$tax=$this->GetFieldNumber($opencart_order_total,"value");
}
//get shipping charges
$this->opencart_orders[$counter]->order_info["ShippingChargesPaid"]=$this->FormatNumber($shipping_charge);
$this->opencart_orders[$counter]->order_info["ShipMethod"]=$this->GetFieldString($opencart_orders_row,"shipping_method");
//Extract tax amount
$this->opencart_orders[$counter]->order_info["ItemsTax"]=$this->FormatNumber($tax);
if($this->GetFieldNumber($opencart_orders_row,"order_status_id")!="1")
$this->opencart_orders[$counter]->order_info["PaymentStatus"]=2;
else
$this->opencart_orders[$counter]->order_info["PaymentStatus"]=0;
//Order status
if($this->GetFieldNumber($opencart_orders_row,"order_status_id")=="3")
$this->opencart_orders[$counter]->order_info["IsShipped"]=1;
else
$this->opencart_orders[$counter]->order_info["IsShipped"]=0;
//Get Customer Comments
$this->opencart_orders[$counter]->order_info["Comments"]=$this->GetFieldString($opencart_orders_row,"comment");
//Get order products
$items_cost=0;
$product_result_arr =$db->query('SELECT op.*,p.weight from '.DB_PREFIX.'order_product op, '.DB_PREFIX.'product p where op.order_id='.$this->GetFieldNumber($opencart_orders_row,"order_id").' and op.product_id=p.product_id');
$i=0;
foreach ($product_result_arr->rows as $product_row)
{
$unit_price=$this->GetFieldNumber($product_row,"price");
$this->opencart_orders[$counter]->order_product[$i]["Price"]=$this->FormatNumber($unit_price);
$this->opencart_orders[$counter]->order_product[$i]["ExternalID"]=$this->GetFieldString($product_row,"model");
$this->opencart_orders[$counter]->order_product[$i]["Quantity"]=$this->GetFieldNumber($product_row,"quantity");
$this->opencart_orders[$counter]->order_product[$i]["Total"]=$this->FormatNumber($unit_price*$this->GetFieldNumber($product_row,"quantity"));
$items_cost=$items_cost+$this->opencart_orders[$counter]->order_product[$i]["Total"];
$product_weight=$this->GetFieldNumber($product_row,"weight");
$this->opencart_orders[$counter]->order_product[$i]["Total_Product_Weight"]=$product_weight*$this->GetFieldNumber($product_row,"quantity");
//product options
$product_option_arr =$db->query('SELECT * from '.DB_PREFIX.'order_option where order_product_id='.$this->GetFieldNumber($product_row,"order_product_id").' and order_id='.$this->opencart_orders[$counter]->orderid);
$attributes="";
if($product_option_arr->num_rows)
{
foreach ($product_option_arr->rows as $option_arr_row)
{
if($attributes!="")
$attributes.=",";
$attributes.=$this->GetFieldString($option_arr_row,"name").":".$this->GetFieldString($option_arr_row,"value");
}
$this->opencart_orders[$counter]->order_product[$i]["Name"]=$this->GetFieldString($product_row,"name")." (".$attributes.")";
}
else
{
$this->opencart_orders[$counter]->order_product[$i]["Name"]=$this->GetFieldString($product_row,"name");
}
$i++;
}
$this->opencart_orders[$counter]->num_of_products=$i;
$this->opencart_orders[$counter]->order_info["ItemsTotal"]=$this->FormatNumber($items_cost,2);
$this->opencart_orders[$counter]->order_info["Total"]=$this->FormatNumber($this->GetFieldNumber($opencart_orders_row,"total"));
$counter++;
}//end foreach
}
################################### Function GetOrdersByDate($datefrom,$dateto) ######################
//Get orders based on date range
#######################################################################################################
function GetOrdersByDate($datefrom,$dateto)
{
$this->Fetch_DB_Orders($this->DateFrom,$this->DateTo);
if (isset($this->opencart_orders))
return $this->opencart_orders;
else
return array();
}
################################################ Function PrepareOpencartOrderStatusFilter #######################
//Prepare order status string based on settings
#######################################################################################################
function PrepareOpencartOrderStatusFilter()
{
$order_status_filter="";
//Order statuses in this case:Shipped ,Processing ,Pending
if(OPENCART_RETRIEVE_ORDER_STATUS_1_PENDING==1)
{
$order_status_filter=" o.order_status_id=1 ";
}
if(OPENCART_RETRIEVE_ORDER_STATUS_2_PROCESSING==1)
{
if($order_status_filter=="")
{
$order_status_filter.=" o.order_status_id=2 ";
}
else
{
$order_status_filter.=" OR o.order_status_id=2 ";
}
}
if(OPENCART_SHIPPED_STATUS_SET_TO_STATUS_3_SHIPPED==1)
{
if($order_status_filter=="")
{
$order_status_filter.=" o.order_status_id=3 ";
}
else
{
$order_status_filter.=" OR o.order_status_id=3 ";
}
}
if($order_status_filter!="")
$order_status_filter="( ".$order_status_filter." ) and";
return $order_status_filter;
}
function ConverCarrier($carrier)
{
$formatted_carrier="";
$carrier=strtolower($carrier);
switch($carrier)
{
case 'usps':
$formatted_carrier="USP";
break;
case 'ups':
$formatted_carrier="UPS";
break;
case 'fedex':
$formatted_carrier="FDX";
break;
case 'dhl':
$formatted_carrier="DHL";
break;
case 'aup':
$formatted_carrier="AUP";
break;
case 'chp':
$formatted_carrier="CHP";
break;
}
return $formatted_carrier;
}
}
######################################### End of class ShippingZOpencart ###################################################
//create object & perform tasks based on command
$obj_shipping_opencart=new ShippingZOpencart;
$obj_shipping_opencart->ExecuteCommand();
?>