PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/modules/shipping/zones.php

https://bitbucket.org/flth/xtcm
PHP | 233 lines | 95 code | 23 blank | 115 comment | 20 complexity | 541a0ba6abe958ca880303a63689f623 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /* -----------------------------------------------------------------------------------------
  3. $Id: zones.php 899 2005-04-29 02:40:57Z hhgag $
  4. XT-Commerce - community made shopping
  5. http://www.xt-commerce.com
  6. Copyright (c) 2003 XT-Commerce
  7. -----------------------------------------------------------------------------------------
  8. based on:
  9. (c) 2000-2001 The Exchange Project (earlier name of osCommerce)
  10. (c) 2002-2003 osCommerce(zones.php,v 1.19 2003/02/05); www.oscommerce.com
  11. (c) 2003 nextcommerce (zones.php,v 1.7 2003/08/24); www.nextcommerce.org
  12. Released under the GNU General Public License
  13. ---------------------------------------------------------------------------------------*/
  14. /*
  15. * USAGE
  16. * By default, the module comes with support for 1 zone. This can be
  17. * easily changed by editing the line below in the zones constructor
  18. * that defines $this->num_zones.
  19. *
  20. * Next, you will want to activate the module by going to the Admin screen,
  21. * clicking on Modules, then clicking on Shipping. A list of all shipping
  22. * modules should appear. Click on the green dot next to the one labeled
  23. * zones.php. A list of settings will appear to the right. Click on the
  24. * Edit button.
  25. *
  26. * PLEASE NOTE THAT YOU WILL LOSE YOUR CURRENT SHIPPING RATES AND OTHER
  27. * SETTINGS IF YOU TURN OFF THIS SHIPPING METHOD. Make sure you keep a
  28. * backup of your shipping settings somewhere at all times.
  29. *
  30. * If you want an additional handling charge applied to orders that use this
  31. * method, set the Handling Fee field.
  32. *
  33. * Next, you will need to define which countries are in each zone. Determining
  34. * this might take some time and effort. You should group a set of countries
  35. * that has similar shipping charges for the same weight. For instance, when
  36. * shipping from the US, the countries of Japan, Australia, New Zealand, and
  37. * Singapore have similar shipping rates. As an example, one of my customers
  38. * is using this set of zones:
  39. * 1: USA
  40. * 2: Canada
  41. * 3: Austria, Belgium, Great Britain, France, Germany, Greenland, Iceland,
  42. * Ireland, Italy, Norway, Holland/Netherlands, Denmark, Poland, Spain,
  43. * Sweden, Switzerland, Finland, Portugal, Israel, Greece
  44. * 4: Japan, Australia, New Zealand, Singapore
  45. * 5: Taiwan, China, Hong Kong
  46. *
  47. * When you enter these country lists, enter them into the Zone X Countries
  48. * fields, where "X" is the number of the zone. They should be entered as
  49. * two character ISO country codes in all capital letters. They should be
  50. * separated by commas with no spaces or other punctuation. For example:
  51. * 1: US
  52. * 2: CA
  53. * 3: AT,BE,GB,FR,DE,GL,IS,IE,IT,NO,NL,DK,PL,ES,SE,CH,FI,PT,IL,GR
  54. * 4: JP,AU,NZ,SG
  55. * 5: TW,CN,HK
  56. *
  57. * Now you need to set up the shipping rate tables for each zone. Again,
  58. * some time and effort will go into setting the appropriate rates. You
  59. * will define a set of weight ranges and the shipping price for each
  60. * range. For instance, you might want an order than weighs more than 0
  61. * and less than or equal to 3 to cost 5.50 to ship to a certain zone.
  62. * This would be defined by this: 3:5.5
  63. *
  64. * You should combine a bunch of these rates together in a comma delimited
  65. * list and enter them into the "Zone X Shipping Table" fields where "X"
  66. * is the zone number. For example, this might be used for Zone 1:
  67. * 1:3.5,2:3.95,3:5.2,4:6.45,5:7.7,6:10.4,7:11.85, 8:13.3,9:14.75,10:16.2,11:17.65,
  68. * 12:19.1,13:20.55,14:22,15:23.45
  69. *
  70. * The above example includes weights over 0 and up to 15. Note that
  71. * units are not specified in this explanation since they should be
  72. * specific to your locale.
  73. *
  74. * CAVEATS
  75. * At this time, it does not deal with weights that are above the highest amount
  76. * defined. This will probably be the next area to be improved with the
  77. * module. For now, you could have one last very high range with a very
  78. * high shipping rate to discourage orders of that magnitude. For
  79. * instance: 999:1000
  80. *
  81. * If you want to be able to ship to any country in the world, you will
  82. * need to enter every country code into the Country fields. For most
  83. * shops, you will not want to enter every country. This is often
  84. * because of too much fraud from certain places. If a country is not
  85. * listed, then the module will add a $0.00 shipping charge and will
  86. * indicate that shipping is not available to that destination.
  87. * PLEASE NOTE THAT THE ORDER CAN STILL BE COMPLETED AND PROCESSED!
  88. *
  89. * It appears that the osC shipping system automatically rounds the
  90. * shipping weight up to the nearest whole unit. This makes it more
  91. * difficult to design precise shipping tables. If you want to, you
  92. * can hack the shipping.php file to get rid of the rounding.
  93. *
  94. * Lastly, there is a limit of 255 characters on each of the Zone
  95. * Shipping Tables and Zone Countries.
  96. *
  97. * Released under the GNU General Public License
  98. *
  99. */
  100. class zones {
  101. var $code, $title, $description, $enabled, $num_zones;
  102. /**
  103. * class constructor
  104. */
  105. function zones() {
  106. $this->code = 'zones';
  107. $this->title = MODULE_SHIPPING_ZONES_TEXT_TITLE;
  108. $this->description = MODULE_SHIPPING_ZONES_TEXT_DESCRIPTION;
  109. $this->sort_order = MODULE_SHIPPING_ZONES_SORT_ORDER;
  110. $this->icon = '';
  111. $this->tax_class = MODULE_SHIPPING_ZONES_TAX_CLASS;
  112. $this->enabled = ((MODULE_SHIPPING_ZONES_STATUS == 'True') ? true : false);
  113. /**
  114. * CUSTOMIZE THIS SETTING FOR THE NUMBER OF ZONES NEEDED
  115. *
  116. * + CUSTOMIZE THE SETTING IN lang/LANGUAGE/modules/shipping/zones.php
  117. */
  118. //BOF - 29.07.2009 - Dokuman - up to 9 zones possible, no changes in langfile needed
  119. //$this->num_zones = 1;
  120. $this->num_zones = 9;
  121. //EOF - 29.07.2009 - Dokuman - up to 9 zones possible, no changes in langfile needed
  122. }
  123. /**
  124. * class methods
  125. */
  126. function quote($method = '') {
  127. global $order, $shipping_weight;
  128. $dest_country = $order->delivery['country']['iso_code_2'];
  129. $dest_zone = 0;
  130. $error = false;
  131. for ($i=1; $i<=$this->num_zones; $i++) {
  132. $countries_table = constant('MODULE_SHIPPING_ZONES_COUNTRIES_' . $i);
  133. $country_zones = explode(",", $countries_table); // Hetfield - 2009-08-18 - replaced deprecated function split with explode to be ready for PHP >= 5.3
  134. if (in_array($dest_country, $country_zones)) {
  135. $dest_zone = $i;
  136. break;
  137. }
  138. }
  139. if ($dest_zone == 0) {
  140. $error = true;
  141. } else {
  142. $shipping = -1;
  143. $zones_cost = constant('MODULE_SHIPPING_ZONES_COST_' . $dest_zone);
  144. $zones_table = preg_split("/[:,]/" , $zones_cost); // Hetfield - 2009-08-18 - replaced deprecated function split with preg_split to be ready for PHP >= 5.3
  145. $size = sizeof($zones_table);
  146. for ($i=0; $i<$size; $i+=2) {
  147. if ($shipping_weight <= $zones_table[$i]) {
  148. $shipping = $zones_table[$i+1];
  149. $shipping_method = MODULE_SHIPPING_ZONES_TEXT_WAY . ' ' . $dest_country . ' : ' . $shipping_weight . ' ' . MODULE_SHIPPING_ZONES_TEXT_UNITS;
  150. break;
  151. }
  152. }
  153. if ($shipping == -1) {
  154. $shipping_cost = 0;
  155. $shipping_method = MODULE_SHIPPING_ZONES_UNDEFINED_RATE;
  156. } else {
  157. $shipping_cost = ($shipping + constant('MODULE_SHIPPING_ZONES_HANDLING_' . $dest_zone));
  158. }
  159. }
  160. $this->quotes = array('id' => $this->code,
  161. 'module' => MODULE_SHIPPING_ZONES_TEXT_TITLE,
  162. 'methods' => array(array('id' => $this->code,
  163. 'title' => $shipping_method,
  164. 'cost' => $shipping_cost)));
  165. if ($this->tax_class > 0) {
  166. $this->quotes['tax'] = xtc_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
  167. }
  168. if (xtc_not_null($this->icon)) $this->quotes['icon'] = xtc_image($this->icon, $this->title);
  169. if ($error == true) $this->quotes['error'] = MODULE_SHIPPING_ZONES_INVALID_ZONE;
  170. return $this->quotes;
  171. }
  172. function check() {
  173. if (!isset($this->_check)) {
  174. $check_query = xtc_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_ZONES_STATUS'");
  175. $this->_check = xtc_db_num_rows($check_query);
  176. }
  177. return $this->_check;
  178. }
  179. function install() {
  180. xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_SHIPPING_ZONES_STATUS', 'True', '6', '0', 'xtc_cfg_select_option(array(\'True\', \'False\'), ', now())");
  181. xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values ('MODULE_SHIPPING_ZONES_ALLOWED', '', '6', '0', now())");
  182. xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) values ('MODULE_SHIPPING_ZONES_TAX_CLASS', '0', '6', '0', 'xtc_get_tax_class_title', 'xtc_cfg_pull_down_tax_classes(', now())");
  183. xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values ('MODULE_SHIPPING_ZONES_SORT_ORDER', '0', '6', '0', now())");
  184. for ($i = 1; $i <= $this->num_zones; $i++) {
  185. $default_countries = '';
  186. if ($i == 1) {
  187. $default_countries = 'US,CA';
  188. }
  189. xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values ('MODULE_SHIPPING_ZONES_COUNTRIES_" . $i ."', '" . $default_countries . "', '6', '0', now())");
  190. xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values ('MODULE_SHIPPING_ZONES_COST_" . $i ."', '3:8.50,7:10.50,99:20.00', '6', '0', now())");
  191. xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values ('MODULE_SHIPPING_ZONES_HANDLING_" . $i."', '0', '6', '0', now())");
  192. }
  193. }
  194. function remove() {
  195. xtc_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
  196. }
  197. function keys() {
  198. $keys = array('MODULE_SHIPPING_ZONES_STATUS','MODULE_SHIPPING_ZONES_ALLOWED', 'MODULE_SHIPPING_ZONES_TAX_CLASS', 'MODULE_SHIPPING_ZONES_SORT_ORDER');
  199. for ($i=1; $i<=$this->num_zones; $i++) {
  200. $keys[] = 'MODULE_SHIPPING_ZONES_COUNTRIES_' . $i;
  201. $keys[] = 'MODULE_SHIPPING_ZONES_COST_' . $i;
  202. $keys[] = 'MODULE_SHIPPING_ZONES_HANDLING_' . $i;
  203. }
  204. return $keys;
  205. }
  206. }
  207. ?>