PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/ZenCart/includes/classes/shipping.php

http://goldcat.googlecode.com/
PHP | 185 lines | 132 code | 21 blank | 32 comment | 38 complexity | 4b8bb0702300b60d81c6dbafcaf2ed0b MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * shipping class
  4. *
  5. * @package classes
  6. * @copyright Copyright 2003-2010 Zen Cart Development Team
  7. * @copyright Portions Copyright 2003 osCommerce
  8. * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  9. * @version $Id: shipping.php 15880 2010-04-11 16:24:30Z wilt $
  10. */
  11. if (!defined('IS_ADMIN_FLAG')) {
  12. die('Illegal Access');
  13. }
  14. /**
  15. * shipping class
  16. * Class used for interfacing with shipping modules
  17. *
  18. * @package classes
  19. */
  20. class shipping extends base {
  21. var $modules;
  22. // class constructor
  23. function shipping($module = '') {
  24. global $PHP_SELF, $messageStack;
  25. if (defined('MODULE_SHIPPING_INSTALLED') && zen_not_null(MODULE_SHIPPING_INSTALLED)) {
  26. $this->modules = explode(';', MODULE_SHIPPING_INSTALLED);
  27. $include_modules = array();
  28. if ( (zen_not_null($module)) && (in_array(substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules)) ) {
  29. $include_modules[] = array('class' => substr($module['id'], 0, strpos($module['id'], '_')), 'file' => substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)));
  30. } else {
  31. reset($this->modules);
  32. while (list(, $value) = each($this->modules)) {
  33. $class = substr($value, 0, strrpos($value, '.'));
  34. $include_modules[] = array('class' => $class, 'file' => $value);
  35. }
  36. }
  37. for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) {
  38. // include(DIR_WS_LANGUAGES . $_SESSION['language'] . '/modules/shipping/' . $include_modules[$i]['file']);
  39. $lang_file = zen_get_file_directory(DIR_WS_LANGUAGES . $_SESSION['language'] . '/modules/shipping/', $include_modules[$i]['file'], 'false');
  40. if (@file_exists($lang_file)) {
  41. include_once($lang_file);
  42. } else {
  43. if (IS_ADMIN_FLAG === false && is_object($messageStack)) {
  44. $messageStack->add('checkout_shipping', WARNING_COULD_NOT_LOCATE_LANG_FILE . $lang_file, 'caution');
  45. } else {
  46. $messageStack->add_session(WARNING_COULD_NOT_LOCATE_LANG_FILE . $lang_file, 'caution');
  47. }
  48. }
  49. $this->enabled = TRUE;
  50. $this->notify('NOTIFY_SHIPPING_MODULE_ENABLE', $include_modules[$i]['class']);
  51. if ($this->enabled)
  52. {
  53. include_once(DIR_WS_MODULES . 'shipping/' . $include_modules[$i]['file']);
  54. $GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class'];
  55. }
  56. }
  57. }
  58. }
  59. function calculate_boxes_weight_and_tare() {
  60. global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes;
  61. if (is_array($this->modules)) {
  62. $shipping_quoted = '';
  63. $shipping_num_boxes = 1;
  64. $shipping_weight = $total_weight;
  65. $za_tare_array = preg_split("/[:,]/" , SHIPPING_BOX_WEIGHT);
  66. $zc_tare_percent= $za_tare_array[0];
  67. $zc_tare_weight= $za_tare_array[1];
  68. $za_large_array = preg_split("/[:,]/" , SHIPPING_BOX_PADDING);
  69. $zc_large_percent= $za_large_array[0];
  70. $zc_large_weight= $za_large_array[1];
  71. // SHIPPING_BOX_WEIGHT = tare
  72. // SHIPPING_BOX_PADDING = Large Box % increase
  73. // SHIPPING_MAX_WEIGHT = Largest package
  74. /*
  75. if (SHIPPING_BOX_WEIGHT >= $shipping_weight*SHIPPING_BOX_PADDING/100) {
  76. $shipping_weight = $shipping_weight+SHIPPING_BOX_WEIGHT;
  77. } else {
  78. $shipping_weight = $shipping_weight + ($shipping_weight*SHIPPING_BOX_PADDING/100);
  79. }
  80. */
  81. switch (true) {
  82. // large box add padding
  83. case(SHIPPING_MAX_WEIGHT <= $shipping_weight):
  84. $shipping_weight = $shipping_weight + ($shipping_weight*($zc_large_percent/100)) + $zc_large_weight;
  85. break;
  86. default:
  87. // add tare weight < large
  88. $shipping_weight = $shipping_weight + ($shipping_weight*($zc_tare_percent/100)) + $zc_tare_weight;
  89. break;
  90. }
  91. if ($shipping_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes
  92. // $shipping_num_boxes = ceil($shipping_weight/SHIPPING_MAX_WEIGHT);
  93. $zc_boxes = zen_round(($shipping_weight/SHIPPING_MAX_WEIGHT), 2);
  94. $shipping_num_boxes = ceil($zc_boxes);
  95. $shipping_weight = $shipping_weight/$shipping_num_boxes;
  96. }
  97. }
  98. $this->notify('NOTIFY_SHIPPING_MODULE_CALCULATE_BOXES_AND_TARE');
  99. }
  100. function quote($method = '', $module = '', $calc_boxes_weight_tare = true) {
  101. $quotes_array = array();
  102. if ($calc_boxes_weight_tare) $this->calculate_boxes_weight_and_tare();
  103. if (is_array($this->modules)) {
  104. $include_quotes = array();
  105. reset($this->modules);
  106. while (list(, $value) = each($this->modules)) {
  107. $class = substr($value, 0, strrpos($value, '.'));
  108. if (zen_not_null($module)) {
  109. if ( ($module == $class) && ($GLOBALS[$class]->enabled) ) {
  110. $include_quotes[] = $class;
  111. }
  112. } elseif ($GLOBALS[$class]->enabled) {
  113. $include_quotes[] = $class;
  114. }
  115. }
  116. $size = sizeof($include_quotes);
  117. for ($i=0; $i<$size; $i++) {
  118. $quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
  119. if (is_array($quotes)) $quotes_array[] = $quotes;
  120. }
  121. }
  122. $this->notify('NOTIFY_SHIPPING_MODULE_GET_ALL_QUOTES', $quotes_array);
  123. return $quotes_array;
  124. }
  125. function cheapest() {
  126. if (is_array($this->modules)) {
  127. $rates = array();
  128. reset($this->modules);
  129. while (list(, $value) = each($this->modules)) {
  130. $class = substr($value, 0, strrpos($value, '.'));
  131. if ($GLOBALS[$class]->enabled) {
  132. $quotes = $GLOBALS[$class]->quotes;
  133. $size = sizeof($quotes['methods']);
  134. for ($i=0; $i<$size; $i++) {
  135. // if ($quotes['methods'][$i]['cost']) {
  136. if (isset($quotes['methods'][$i]['cost'])){
  137. $rates[] = array('id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'],
  138. 'title' => $quotes['module'] . ' (' . $quotes['methods'][$i]['title'] . ')',
  139. 'cost' => $quotes['methods'][$i]['cost'],
  140. 'module' => $quotes['id']
  141. );
  142. }
  143. }
  144. }
  145. }
  146. $cheapest = false;
  147. $size = sizeof($rates);
  148. for ($i=0; $i<$size; $i++) {
  149. if (is_array($cheapest)) {
  150. // never quote storepickup as lowest - needs to be configured in shipping module
  151. if ($rates[$i]['cost'] < $cheapest['cost'] and $rates[$i]['module'] != 'storepickup') {
  152. $cheapest = $rates[$i];
  153. }
  154. } else {
  155. if ($rates[$i]['module'] != 'storepickup') {
  156. $cheapest = $rates[$i];
  157. }
  158. }
  159. }
  160. $this->notify('NOTIFY_SHIPPING_MODULE_CALCULATE_CHEAPEST', $cheapest);
  161. return $cheapest;
  162. }
  163. }
  164. }