  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: shipping.php 404 2006-01-25 22:55:27Z hpdl $ |
| |
| 3 | + | $Id: shipping.php 421 2006-02-08 17:53:17Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
|
|
 |
… |
|
10 | 10 | | Released under the GNU General Public License |
| |
11 | 11 | | */ |
| |
12 | 12 | | |
  |
13 | | - | class shipping { |
| |
14 | | - | var $modules; |
| |
| 13 | + | class osC_Shipping { |
| |
| 14 | + | var $_modules = array(), |
| |
| 15 | + | $_quotes = array(), |
| |
| 16 | + | $_shipping_boxes = 1, |
| |
| 17 | + | $_shipping_weight = 0; |
|
15 | 18 | | |
| |
16 | 19 | | // class constructor |
  |
17 | | - | function shipping($module = '') { |
| |
| 20 | + | function osC_Shipping($module = '') { |
|
18 | 21 | | global $osC_Language; |
| |
19 | 22 | | |
  |
20 | | - | if (defined('MODULE_SHIPPING_INSTALLED') && tep_not_null(MODULE_SHIPPING_INSTALLED)) { |
| |
21 | | - | $this->modules = explode(';', MODULE_SHIPPING_INSTALLED); |
| |
| 23 | + | if (isset($_SESSION['osC_Shipping_data']) === false) { |
| |
| 24 | + | $_SESSION['osC_Shipping_data'] = array('shipping_boxes' => 1, |
| |
| 25 | + | 'shipping_weight' => 0, |
| |
| 26 | + | 'quotes' => array()); |
| |
| 27 | + | } |
|
22 | 28 | | |
  |
| 29 | + | $this->_shipping_boxes =& $_SESSION['osC_Shipping_data']['shipping_boxes']; |
| |
| 30 | + | $this->_shipping_weight =& $_SESSION['osC_Shipping_data']['shipping_weight']; |
| |
| 31 | + | $this->_quotes =& $_SESSION['osC_Shipping_data']['quotes']; |
| |
| 32 | + | |
| |
| 33 | + | if (defined('MODULE_SHIPPING_INSTALLED') && (osc_empty(MODULE_SHIPPING_INSTALLED) === false)) { |
| |
| 34 | + | $this->_modules = explode(';', MODULE_SHIPPING_INSTALLED); |
| |
| 35 | + | |
|
23 | 36 | | $include_modules = array(); |
| |
24 | 37 | | |
  |
25 | | - | if ( (tep_not_null($module)) && (in_array(substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($_SERVER['PHP_SELF'], (strrpos($_SERVER['PHP_SELF'], '.')+1)), $this->modules)) ) { |
| |
| 38 | + | if ( (empty($module) === false) && (in_array(substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($_SERVER['PHP_SELF'], (strrpos($_SERVER['PHP_SELF'], '.')+1)), $this->_modules)) ) { |
|
26 | 39 | | $include_modules[] = array('class' => substr($module['id'], 0, strpos($module['id'], '_')), 'file' => substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($_SERVER['PHP_SELF'], (strrpos($_SERVER['PHP_SELF'], '.')+1))); |
| |
27 | 40 | | } else { |
  |
28 | | - | reset($this->modules); |
| |
29 | | - | while (list(, $value) = each($this->modules)) { |
| |
| 41 | + | foreach ($this->_modules as $value) { |
|
30 | 42 | | $class = substr($value, 0, strrpos($value, '.')); |
| |
31 | 43 | | $include_modules[] = array('class' => $class, 'file' => $value); |
| |
32 | 44 | | } |
| |
33 | 45 | | } |
| |
34 | 46 | | |
| |
35 | 47 | | $osC_Language->load('modules-shipping'); |
| |
36 | 48 | | |
  |
37 | | - | for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) { |
| |
38 | | - | include('includes/modules/shipping/' . $include_modules[$i]['file']); |
| |
| 49 | + | foreach ($include_modules as $module) { |
| |
| 50 | + | include('includes/modules/shipping/' . $module['file']); |
|
39 | 51 | | |
  |
40 | | - | $GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class']; |
| |
| 52 | + | $GLOBALS[$module['class']] = new $module['class']; |
|
41 | 53 | | } |
| |
42 | 54 | | } |
| |
43 | 55 | | } |
| |
44 | 56 | | |
  |
45 | | - | function quote($method = '', $module = '') { |
| |
46 | | - | global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes; |
| |
| 57 | + | function calculateQuotes($method = '', $module = '') { |
| |
| 58 | + | global $osC_ShoppingCart; |
|
47 | 59 | | |
  |
48 | | - | $quotes_array = array(); |
| |
| 60 | + | $this->_quotes = array(); |
|
49 | 61 | | |
  |
50 | | - | if (is_array($this->modules)) { |
| |
51 | | - | $shipping_quoted = ''; |
| |
52 | | - | $shipping_num_boxes = 1; |
| |
53 | | - | $shipping_weight = $total_weight; |
| |
| 62 | + | if (is_array($this->_modules)) { |
| |
| 63 | + | $this->_shipping_weight = $osC_ShoppingCart->getWeight(); |
|
54 | 64 | | |
  |
55 | | - | if (SHIPPING_BOX_WEIGHT >= $shipping_weight*SHIPPING_BOX_PADDING/100) { |
| |
56 | | - | $shipping_weight = $shipping_weight+SHIPPING_BOX_WEIGHT; |
| |
| 65 | + | if (SHIPPING_BOX_WEIGHT >= ($this->_shipping_weight * SHIPPING_BOX_PADDING/100)) { |
| |
| 66 | + | $this->_shipping_weight = $this->_shipping_weight + SHIPPING_BOX_WEIGHT; |
|
57 | 67 | | } else { |
  |
58 | | - | $shipping_weight = $shipping_weight + ($shipping_weight*SHIPPING_BOX_PADDING/100); |
| |
| 68 | + | $this->_shipping_weight = $this->_shipping_weight + ($this->_shipping_weight * SHIPPING_BOX_PADDING/100); |
|
59 | 69 | | } |
| |
60 | 70 | | |
  |
61 | | - | if ($shipping_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes |
| |
62 | | - | $shipping_num_boxes = ceil($shipping_weight/SHIPPING_MAX_WEIGHT); |
| |
63 | | - | $shipping_weight = $shipping_weight/$shipping_num_boxes; |
| |
| 71 | + | if ($this->_shipping_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes |
| |
| 72 | + | $this->_shipping_boxes = ceil($this->_shipping_weight / SHIPPING_MAX_WEIGHT); |
| |
| 73 | + | $this->_shipping_weight = $this->_shipping_weight / $this->_shipping_boxes; |
|
64 | 74 | | } |
| |
65 | 75 | | |
| |
66 | 76 | | $include_quotes = array(); |
| |
67 | 77 | | |
  |
68 | | - | reset($this->modules); |
| |
69 | | - | while (list(, $value) = each($this->modules)) { |
| |
| 78 | + | foreach ($this->_modules as $value) { |
|
70 | 79 | | $class = substr($value, 0, strrpos($value, '.')); |
| |
71 | 80 | | if (tep_not_null($module)) { |
| |
72 | 81 | | if ( ($module == $class) && ($GLOBALS[$class]->enabled) ) { |
| |
|
|
 |
… |
|
77 | 86 | | } |
| |
78 | 87 | | } |
| |
79 | 88 | | |
  |
80 | | - | $size = sizeof($include_quotes); |
| |
81 | | - | for ($i=0; $i<$size; $i++) { |
| |
82 | | - | $quotes = $GLOBALS[$include_quotes[$i]]->quote($method); |
| |
83 | | - | if (is_array($quotes)) $quotes_array[] = $quotes; |
| |
| 89 | + | foreach ($include_quotes as $module) { |
| |
| 90 | + | $quotes = $GLOBALS[$module]->quote($method); |
| |
| 91 | + | |
| |
| 92 | + | if (is_array($quotes)) { |
| |
| 93 | + | $this->_quotes[] = $quotes; |
| |
| 94 | + | } |
|
84 | 95 | | } |
| |
85 | 96 | | } |
  |
| 97 | + | } |
|
86 | 98 | | |
  |
87 | | - | return $quotes_array; |
| |
| 99 | + | function hasQuotes() { |
| |
| 100 | + | return !empty($this->_quotes); |
|
88 | 101 | | } |
| |
89 | 102 | | |
  |
90 | | - | function cheapest() { |
| |
91 | | - | if (is_array($this->modules)) { |
| |
| 103 | + | function getQuotes($method = '', $module = '') { |
| |
| 104 | + | if ( (empty($method) === false) && (empty($module) === false) ) { |
| |
| 105 | + | foreach ($this->_quotes as $quote) { |
| |
| 106 | + | if ($quote['id'] == $module) { |
| |
| 107 | + | foreach ($quote['methods'] as $quote_method) { |
| |
| 108 | + | if ($quote_method['id'] == $method) { |
| |
| 109 | + | return array('id' => $quote['id'], |
| |
| 110 | + | 'module' => $quote['module'], |
| |
| 111 | + | 'methods' => $quote_method, |
| |
| 112 | + | 'tax' => $quote['tax']); |
| |
| 113 | + | } |
| |
| 114 | + | } |
| |
| 115 | + | } |
| |
| 116 | + | } |
| |
| 117 | + | } |
| |
| 118 | + | |
| |
| 119 | + | return $this->_quotes; |
| |
| 120 | + | } |
| |
| 121 | + | |
| |
| 122 | + | function getCheapestQuote() { |
| |
| 123 | + | if (is_array($this->_modules)) { |
|
92 | 124 | | $rates = array(); |
| |
93 | 125 | | |
  |
94 | | - | reset($this->modules); |
| |
95 | | - | while (list(, $value) = each($this->modules)) { |
| |
| 126 | + | foreach ($this->_modules as $value) { |
|
96 | 127 | | $class = substr($value, 0, strrpos($value, '.')); |
| |
97 | 128 | | if ($GLOBALS[$class]->enabled) { |
| |
98 | 129 | | $quotes = $GLOBALS[$class]->quotes; |
  |
| 130 | + | |
|
99 | 131 | | for ($i=0, $n=sizeof($quotes['methods']); $i<$n; $i++) { |
| |
100 | 132 | | if (isset($quotes['methods'][$i]['cost']) && tep_not_null($quotes['methods'][$i]['cost'])) { |
| |
101 | 133 | | $rates[] = array('id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'], |
| |
|
|
 |
… |
|
107 | 139 | | } |
| |
108 | 140 | | |
| |
109 | 141 | | $cheapest = false; |
  |
| 142 | + | |
  |
110 | 143 | | for ($i=0, $n=sizeof($rates); $i<$n; $i++) { |
| |
111 | 144 | | if (is_array($cheapest)) { |
| |
112 | 145 | | if ($rates[$i]['cost'] < $cheapest['cost']) { |