/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
  2. if( !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. * This class will charge a shipping rate determined by passing parameters to
  21. * Australia Post eDeliver Calculator located at http://drc.edeliver.com.au/
  22. * @copyright (C) 2006 Ben Wilson, ben@diversionware.com.au
  23. *
  24. *******************************************************************************
  25. */
  26. class auspost {
  27. var $classname = "auspost";
  28. function list_rates( &$d ) {
  29. global $total, $tax_total, $CURRENCY_DISPLAY;
  30. $cart = $_SESSION['cart'];
  31. /** Read current Configuration ***/
  32. require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
  33. if ( $_SESSION['auth']['show_price_including_tax'] != 1 ) {
  34. $taxrate = 1;
  35. $order_total = $total + $tax_total;
  36. }
  37. else {
  38. $taxrate = $this->get_tax_rate() + 1;
  39. $order_total = $total;
  40. }
  41. //Create DB User Object for Current User
  42. $dbu = new ps_DB;
  43. $q = "SELECT country,zip FROM #__{vm}_user_info WHERE user_info_id = '". $d["ship_to_info_id"] . "'";
  44. $dbu->query($q);
  45. if (!$dbu->next_record()) {
  46. /*$vmLogger->err( $VM_LANG->_('PHPSHOP_CHECKOUT_ERR_SHIPTO_NOT_FOUND',false) );
  47. return False;*/
  48. }
  49. //Create DB Vendor Object for Shop Vendor
  50. $dbv = new ps_DB;
  51. $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)";
  52. $dbv->query($q);
  53. $dbv->next_record();
  54. //$dbv = new ps_DB
  55. //$q = "SELECT * FROM #__{vm}_vendor WHERE vendor_id='".$_SESSION['ps_vendor_id']."'";
  56. //$dbv->query($q);
  57. //$dbv->next_record();
  58. //set up the variables for Australia Post Query
  59. //Postcode of the pick-up address (e.g. 3015)
  60. //$Order_Pickup_Postcode = '2615';
  61. //$Order_Pickup_Postcode = Pickup_Postcode;
  62. $Order_Pickup_Postcode = $dbv->f("vendor_zip");
  63. //Postcode of the delivery destination (e.g. 2615)
  64. //$Order_Destination_Postcode = '2001';
  65. $Order_Destination_Postcode = $dbu->f("zip");
  66. //The country of delivery destination designated by two alpha characters. For example, AU stands for Australia
  67. $Order_Country = 'AU';
  68. //The weight of the parcel or item measured in grams (g)
  69. //$Order_Weight = '10000';
  70. $Order_WeightKG = $d['weight'] ;
  71. $Order_Weight = $Order_WeightKG * 1000;
  72. //The type of servive, available types are "Standard", "Express", "Air", "Sea", and "Economy"
  73. //$Order_Service_Type = Service_Type;
  74. $Order_Service_Type = 'STANDARD';
  75. //The length of the item or parcel in millimetres (mm)
  76. //Auspost returns same value so long as this is valid ie between 100 and 500, so we use a fixed 250 as a placeholder
  77. $Order_Length = '250';
  78. //The width of the item or parcel in millimetres (mm)
  79. $Order_Width = '250';
  80. //The height of the item or parcel in millimetres (mm)
  81. $Order_Height = '250';
  82. //This is the quantity of items for which the customer is estimating the delivery charges
  83. //Always set to one, as virtuemart does the multiplying for us based on quantity in cart
  84. $Order_Quantity = '1';
  85. //Fee for packaging and handling, added to the delivery costs returned by auspost
  86. $Order_Handling_Fee = Handling_Fee;
  87. // Collect variables into the query URI for Australia Post
  88. $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);
  89. // Get Australia Post charge value separate to 'charge='
  90. $APchargeArray = split('=',$myfile[0]);
  91. $APcharge = $APchargeArray[1];
  92. // Get Australia Post Time separate to 'days='
  93. $APtimeArray = split('=',$myfile[1]);
  94. $APtime = $APtimeArray[1];
  95. // error message
  96. $APerrorArray = split('=',$myfile[2]);
  97. $APerrorMessage = $APerrorArray[1];
  98. (string) $strAPerrorMessage = $APerrorMessage; //necessary to type cast this to a string otherwise below comparator doesn't work ???
  99. if(substr($strAPerrorMessage,0,2) === "OK")
  100. {
  101. $Total_Shipping_Handling = $APcharge + $Order_Handling_Fee;
  102. $_SESSION[$shipping_rate_id] = 1;
  103. // THE ORDER OF THOSE VALUES IS IMPORTANT:
  104. // ShippingClassName|carrier_name|rate_name|totalshippingcosts|rate_id
  105. $shipping_rate_id = urlencode( $this->classname."|auspost|standard|".number_format($Total_Shipping_Handling,2));
  106. $html .= "\n<input type=\"radio\" name=\"shipping_rate_id\" id=\"auspost\" checked=\"checked\" value=\"$shipping_rate_id\" />\n";
  107. $html .= "<label for=\"auspost_shipping_rate\">Australia Post: ";
  108. $html .= $CURRENCY_DISPLAY->getFullValue($Total_Shipping_Handling);
  109. $html .= " (".$Order_WeightKG." kg)";
  110. $html .= "</label>";
  111. $_SESSION[$shipping_rate_id] = 1;
  112. echo $html;
  113. return true;
  114. }
  115. else
  116. {
  117. $html .= "<label>Australia Post shipping calculator failed, reason: ".$APerrorMessage;
  118. echo $html;
  119. return false;
  120. }
  121. }
  122. function get_rate( &$d ) {
  123. $shipping_rate_id = $d["shipping_rate_id"];
  124. $is_arr = explode("|", urldecode(urldecode($shipping_rate_id)) );
  125. $order_shipping = $is_arr[3];
  126. return $order_shipping;
  127. }
  128. function get_tax_rate() {
  129. /** Read current Configuration ***/
  130. require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
  131. if( intval(AUSPOST_TAX_CLASS)== 0 )
  132. return( 0 );
  133. else {
  134. require_once( CLASSPATH. "ps_tax.php" );
  135. $tax_rate = ps_tax::get_taxrate_by_id( intval(AUSPOST_TAX_CLASS) );
  136. return $tax_rate;
  137. }
  138. }
  139. /* Validate this Shipping method by checking if the SESSION contains the key
  140. * @returns boolean False when the Shipping method is not in the SESSION
  141. */
  142. function validate( $d ) {
  143. $shipping_rate_id = $d["shipping_rate_id"];
  144. if( array_key_exists( $shipping_rate_id, $_SESSION )) {
  145. return true;
  146. }
  147. else {
  148. return false;
  149. }
  150. }
  151. /**
  152. * Show all configuration parameters for this Shipping method
  153. * @returns boolean False when the Shipping method has no configration
  154. */
  155. function show_configuration() {
  156. global $VM_LANG;
  157. /** Read current Configuration ***/
  158. require_once(CLASSPATH ."shipping/".$this->classname.".cfg.php");
  159. ?>
  160. <table>
  161. <tr>
  162. <td><img src="http://drc.edeliver.com.au/bt_aphome.gif" alt="AusPost Logo"></td>
  163. </tr>
  164. <tr>
  165. <td><strong>Packing and Handling Fee:</strong>
  166. </td>
  167. <td>
  168. <input type="text" name="Handling_Fee" class="inputbox" value="<?php echo Handling_Fee ?>" />
  169. </td>
  170. <td>
  171. <?php echo mm_ToolTip("This is your fee for packaging and handling, and is added to the delivery costs returned by auspost") ?>
  172. </td>
  173. </tr>
  174. <tr>
  175. <td><strong><?php echo $VM_LANG->_('PHPSHOP_UPS_TAX_CLASS') ?></strong></td>
  176. <td>
  177. <?php
  178. require_once(CLASSPATH.'ps_tax.php');
  179. ps_tax::list_tax_value("AUSPOST_TAX_CLASS", AUSPOST_TAX_CLASS) ?>
  180. </td>
  181. <td><?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_UPS_TAX_CLASS_TOOLTIP')) ?><td>
  182. </tr>
  183. </table>
  184. <?php
  185. // return false if there's no configuration
  186. return true;
  187. }
  188. /**
  189. * Returns the "is_writeable" status of the configuration file
  190. * @param void
  191. * @returns boolean True when the configuration file is writeable, false when not
  192. */
  193. function configfile_writeable() {
  194. return is_writeable( CLASSPATH."shipping/".$this->classname.".cfg.php" );
  195. }
  196. /**
  197. * Writes the configuration file for this shipping method
  198. * @param array An array of objects
  199. * @returns boolean True when writing was successful
  200. */
  201. function write_configuration( &$d ) {
  202. global $vmLogger;
  203. $my_config_array = array("Handling_Fee" => $d['Handling_Fee'],
  204. "AUSPOST_TAX_CLASS" => $d['AUSPOST_TAX_CLASS']
  205. );
  206. $config = "<?php\n";
  207. $config .= "if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); \n\n";
  208. foreach( $my_config_array as $key => $value ) {
  209. $config .= "define ('$key', '$value');\n";
  210. }
  211. $config .= "?>";
  212. if ($fp = fopen(CLASSPATH ."shipping/".$this->classname.".cfg.php", "w")) {
  213. fputs($fp, $config, strlen($config));
  214. fclose ($fp);
  215. return true;
  216. }
  217. else {
  218. $vmLogger->err( "Error writing to configuration file" );
  219. return false;
  220. }
  221. }
  222. }
  223. ?>