PageRenderTime 104ms CodeModel.GetById 88ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/administrator/components/com_virtuemart/classes/shipping/auspost.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 272 lines | 147 code | 49 blank | 76 comment | 15 complexity | 46b2904251534034886ec5ba9e855765 MD5 | raw file
  1<?php
  2if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3/**
  4*
  5* @version $Id: auspost.php 1095 2007-12-19 20:19:16Z soeren_nb $
  6* @package VirtueMart
  7* @subpackage shipping
  8* @copyright Copyright (C) 2006 Ben Wilson. All rights reserved.
  9* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 10* VirtueMart is free software. This version may have been modified pursuant
 11* to the GNU General Public License, and as distributed it includes or
 12* is derivative of works licensed under the GNU General Public License or
 13* other free or open source software licenses.
 14* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
 15*
 16* http://virtuemart.net
 17*/
 18
 19/**
 20*
 21* This class will charge a shipping rate determined by passing parameters to 
 22* Australia Post eDeliver Calculator located at http://drc.edeliver.com.au/ 
 23* @copyright (C) 2006 Ben Wilson, ben@diversionware.com.au
 24* 
 25*******************************************************************************
 26*/
 27class auspost {
 28
 29	var $classname = "auspost";
 30
 31	function list_rates( &$d ) {
 32		global $total, $tax_total, $CURRENCY_DISPLAY;
 33
 34		$cart = $_SESSION['cart'];
 35
 36		/** Read current Configuration ***/
 37		require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
 38
 39		if ( $_SESSION['auth']['show_price_including_tax'] != 1 ) {
 40			$taxrate = 1;
 41			$order_total = $total + $tax_total;
 42		}
 43		else {
 44			$taxrate = $this->get_tax_rate() + 1;
 45			$order_total = $total;
 46		}
 47
 48		//Create DB User Object for Current User
 49		$dbu = new ps_DB;
 50		$q  = "SELECT country,zip FROM #__{vm}_user_info WHERE user_info_id = '". $d["ship_to_info_id"] . "'";
 51		$dbu->query($q);
 52		if (!$dbu->next_record()) {
 53			/*$vmLogger->err( $VM_LANG->_('PHPSHOP_CHECKOUT_ERR_SHIPTO_NOT_FOUND',false) );
 54			return False;*/
 55		}
 56
 57		//Create DB Vendor Object for Shop Vendor
 58		$dbv = new ps_DB;
 59		$q  = "SELECT * from #__{vm}_vendor, #__{vm}_country WHERE vendor_id='" . $_SESSION["ps_vendor_id"] . "' AND (vendor_country=country_2_code OR vendor_country=country_3_code)";
 60		$dbv->query($q);
 61		$dbv->next_record();
 62
 63		//$dbv = new ps_DB
 64		//$q  = "SELECT * FROM #__{vm}_vendor WHERE vendor_id='".$_SESSION['ps_vendor_id']."'";
 65		//$dbv->query($q);
 66		//$dbv->next_record();
 67
 68		//set up the variables for Australia Post Query
 69
 70		//Postcode of the pick-up address (e.g. 3015)
 71		//$Order_Pickup_Postcode = '2615';
 72		//$Order_Pickup_Postcode = Pickup_Postcode;
 73		$Order_Pickup_Postcode = $dbv->f("vendor_zip");
 74
 75		//Postcode of the delivery destination (e.g. 2615)
 76		//$Order_Destination_Postcode = '2001';
 77		$Order_Destination_Postcode = $dbu->f("zip");
 78
 79		//The country of delivery destination designated by two alpha characters. For example, AU stands for Australia
 80		$Order_Country = 'AU';
 81
 82		//The weight of the parcel or item measured in grams (g)
 83		//$Order_Weight = '10000';
 84		$Order_WeightKG = $d['weight'] ;
 85		$Order_Weight = $Order_WeightKG * 1000;
 86
 87
 88		//The type of servive, available types are "Standard", "Express", "Air", "Sea", and "Economy"
 89		//$Order_Service_Type = Service_Type;
 90		$Order_Service_Type = 'STANDARD';
 91
 92		//The length of the item or parcel in millimetres (mm)
 93		//Auspost returns same value so long as this is valid ie between 100 and 500, so we use a fixed 250 as a placeholder
 94		$Order_Length = '250';
 95
 96		//The width of the item or parcel in millimetres (mm)
 97		$Order_Width = '250';
 98
 99		//The height of the item or parcel in millimetres (mm)
100		$Order_Height = '250';
101		
102		//This is the quantity of items for which the customer is estimating the delivery charges
103		//Always set to one, as virtuemart does the multiplying for us based on quantity in cart
104		$Order_Quantity = '1';
105
106		//Fee for packaging and handling, added to the delivery costs returned by auspost
107		$Order_Handling_Fee = Handling_Fee;
108
109	    // Collect variables into the query URI for Australia Post
110		$myfile=file('http://drc.edeliver.com.au/ratecalc.asp?Pickup_Postcode='.$Order_Pickup_Postcode.'&Destination_Postcode='.$Order_Destination_Postcode.'&Country='.$Order_Country.'&Weight='.$Order_Weight.'&Service_Type='.$Order_Service_Type.'&Length='.$Order_Length.'&Width='.$Order_Width.'&Height='.$Order_Height.'&Quantity='.$Order_Quantity);
111
112		// Get Australia Post charge value separate to 'charge='
113		$APchargeArray = split('=',$myfile[0]);
114		$APcharge = $APchargeArray[1];
115
116		// Get Australia Post Time separate to 'days='
117		$APtimeArray = split('=',$myfile[1]);
118		$APtime = $APtimeArray[1];
119
120		// error message
121		$APerrorArray = split('=',$myfile[2]);
122		$APerrorMessage = $APerrorArray[1];
123		(string) $strAPerrorMessage = $APerrorMessage;  //necessary to type cast this to a string otherwise below comparator doesn't work ???
124
125		if(substr($strAPerrorMessage,0,2) === "OK")
126		{
127			$Total_Shipping_Handling = $APcharge + $Order_Handling_Fee;
128
129			$_SESSION[$shipping_rate_id] = 1;
130
131			// THE ORDER OF THOSE VALUES IS IMPORTANT:
132			// ShippingClassName|carrier_name|rate_name|totalshippingcosts|rate_id
133			$shipping_rate_id = urlencode( $this->classname."|auspost|standard|".number_format($Total_Shipping_Handling,2));
134
135			$html .= "\n<input type=\"radio\" name=\"shipping_rate_id\" id=\"auspost\" checked=\"checked\" value=\"$shipping_rate_id\" />\n";
136			$html .= "<label for=\"auspost_shipping_rate\">Australia Post: ";
137			$html .= $CURRENCY_DISPLAY->getFullValue($Total_Shipping_Handling);
138			$html .= " (".$Order_WeightKG." kg)";
139			$html .= "</label>";
140
141			$_SESSION[$shipping_rate_id] = 1;
142
143			echo $html;
144			return true;
145		}
146		else
147		{
148			$html .= "<label>Australia Post shipping calculator failed, reason: ".$APerrorMessage;
149			echo $html;
150			return false;
151		}
152	}
153
154	function get_rate( &$d ) {
155
156	$shipping_rate_id = $d["shipping_rate_id"];
157	$is_arr = explode("|", urldecode(urldecode($shipping_rate_id)) );
158	$order_shipping = $is_arr[3];
159
160	return $order_shipping;
161
162	}
163
164
165	function get_tax_rate() {
166
167		/** Read current Configuration ***/
168		require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
169
170		if( intval(AUSPOST_TAX_CLASS)== 0 )
171		return( 0 );
172		else {
173			require_once( CLASSPATH. "ps_tax.php" );
174			$tax_rate = ps_tax::get_taxrate_by_id( intval(AUSPOST_TAX_CLASS) );
175			return $tax_rate;
176		}
177	}
178
179	/* Validate this Shipping method by checking if the SESSION contains the key
180	* @returns boolean False when the Shipping method is not in the SESSION
181	*/
182	function validate( $d ) {
183
184		$shipping_rate_id = $d["shipping_rate_id"];
185
186		if( array_key_exists( $shipping_rate_id, $_SESSION )) {
187			
188			return true;
189		}
190		else {
191			return false;
192		}
193	}
194	/**
195    * Show all configuration parameters for this Shipping method
196    * @returns boolean False when the Shipping method has no configration
197    */
198	function show_configuration() {
199		global $VM_LANG;
200		/** Read current Configuration ***/
201		require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
202    ?>
203      <table>
204    <tr>
205        <td><img src="http://drc.edeliver.com.au/bt_aphome.gif" alt="AusPost Logo"></td>
206    </tr>
207    <tr>
208        <td><strong>Packing and Handling Fee:</strong>
209		</td>
210		<td>
211            <input type="text" name="Handling_Fee" class="inputbox" value="<?php echo Handling_Fee ?>" />
212		</td>
213		<td>
214            <?php echo mm_ToolTip("This is your fee for packaging and handling, and is added to the delivery costs returned by auspost") ?>
215        </td>
216    </tr>
217	  <tr>
218		<td><strong><?php echo $VM_LANG->_('PHPSHOP_UPS_TAX_CLASS') ?></strong></td>
219		<td>
220		  <?php
221		  require_once(CLASSPATH.'ps_tax.php');
222		  ps_tax::list_tax_value("AUSPOST_TAX_CLASS", AUSPOST_TAX_CLASS) ?>
223		</td>
224		<td><?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_UPS_TAX_CLASS_TOOLTIP')) ?><td>
225	  </tr>	
226	</table>
227   <?php
228   // return false if there's no configuration
229   return true;
230	}
231	/**
232  * Returns the "is_writeable" status of the configuration file
233  * @param void
234  * @returns boolean True when the configuration file is writeable, false when not
235  */
236	function configfile_writeable() {
237		return is_writeable( CLASSPATH."shipping/".$this->classname.".cfg.php" );
238	}
239
240	/**
241	* Writes the configuration file for this shipping method
242	* @param array An array of objects
243	* @returns boolean True when writing was successful
244	*/
245	function write_configuration( &$d ) {
246	    global $vmLogger;
247
248		$my_config_array = array("Handling_Fee" => $d['Handling_Fee'],
249		"AUSPOST_TAX_CLASS" => $d['AUSPOST_TAX_CLASS']
250		);
251		$config = "<?php\n";
252		$config .= "if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); \n\n";
253		foreach( $my_config_array as $key => $value ) {
254			$config .= "define ('$key', '$value');\n";
255		}
256
257		$config .= "?>";
258
259		if ($fp = fopen(CLASSPATH ."shipping/".$this->classname.".cfg.php", "w")) {
260			fputs($fp, $config, strlen($config));
261			fclose ($fp);
262			return true;
263		}
264		else {
265			$vmLogger->err( "Error writing to configuration file" );
266			return false;
267		}
268	}
269}
270
271
272?>