  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: shipping.php 438 2006-02-16 14:50:54Z hpdl $ |
| |
| 3 | + | $Id: shipping.php 439 2006-02-19 16:33:18Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
|
|
 |
… |
|
12 | 12 | | |
| |
13 | 13 | | class osC_Shipping { |
| |
14 | 14 | | var $_modules = array(), |
  |
| 15 | + | $_selected_module, |
|
15 | 16 | | $_quotes = array(), |
  |
16 | | - | $_shipping_boxes = 1, |
| |
17 | | - | $_shipping_weight = 0, |
|
18 | 17 | | $_keys, |
| |
19 | 18 | | $_group = 'shipping'; |
| |
20 | 19 | | |
| |
|
|
 |
… |
|
23 | 22 | | global $osC_Database, $osC_Language; |
| |
24 | 23 | | |
| |
25 | 24 | | if (isset($_SESSION['osC_Shipping_data']) === false) { |
  |
26 | | - | $_SESSION['osC_Shipping_data'] = array('shipping_boxes' => 1, |
| |
27 | | - | 'shipping_weight' => 0, |
| |
28 | | - | 'quotes' => array()); |
| |
| 25 | + | $_SESSION['osC_Shipping_data'] = array('quotes' => array(), |
| |
| 26 | + | 'cartID' => null); |
|
29 | 27 | | } |
| |
30 | 28 | | |
  |
31 | | - | $this->_shipping_boxes =& $_SESSION['osC_Shipping_data']['shipping_boxes']; |
| |
32 | | - | $this->_shipping_weight =& $_SESSION['osC_Shipping_data']['shipping_weight']; |
|
33 | 29 | | $this->_quotes =& $_SESSION['osC_Shipping_data']['quotes']; |
  |
| 30 | + | $this->_cartID =& $_SESSION['osC_Shipping_data']['cartID']; |
|
34 | 31 | | |
| |
35 | 32 | | $Qmodules = $osC_Database->query('select code from :table_templates_boxes where modules_group = "shipping"'); |
| |
36 | 33 | | $Qmodules->bindTable(':table_templates_boxes', TABLE_TEMPLATES_BOXES); |
| |
|
|
 |
… |
|
44 | 41 | | $Qmodules->freeResult(); |
| |
45 | 42 | | |
| |
46 | 43 | | if (empty($this->_modules) === false) { |
  |
47 | | - | if ((empty($module) === false) && in_array(substr($module['id'], 0, strpos($module['id'], '_')), $this->_modules)) { |
| |
48 | | - | $this->_modules = array($module['id']); |
| |
| 44 | + | if ((empty($module) === false) && in_array(substr($module, 0, strpos($module, '_')), $this->_modules)) { |
| |
| 45 | + | $this->_selected_module = $module; |
| |
| 46 | + | $this->_modules = array(substr($module, 0, strpos($module, '_'))); |
|
49 | 47 | | } |
| |
50 | 48 | | |
| |
51 | 49 | | $osC_Language->load('modules-shipping'); |
| |
52 | 50 | | |
| |
53 | 51 | | foreach ($this->_modules as $module) { |
  |
54 | | - | include('includes/modules/shipping/' . $module . '.' . substr(basename(__FILE__), (strrpos(basename(__FILE__), '.')+1))); |
| |
55 | | - | |
|
56 | 52 | | $module_class = 'osC_Shipping_' . $module; |
| |
57 | 53 | | |
  |
| 54 | + | if (class_exists($module_class) === false) { |
| |
| 55 | + | include('includes/modules/shipping/' . $module . '.' . substr(basename(__FILE__), (strrpos(basename(__FILE__), '.')+1))); |
| |
| 56 | + | } |
| |
| 57 | + | |
|
58 | 58 | | $GLOBALS[$module_class] = new $module_class(); |
| |
59 | 59 | | $GLOBALS[$module_class]->initialize(); |
| |
60 | 60 | | } |
| |
61 | 61 | | |
| |
62 | 62 | | usort($this->_modules, array('osC_Shipping', '_usortModules')); |
| |
63 | 63 | | } |
  |
| 64 | + | |
| |
| 65 | + | $this->_calculate(); |
|
64 | 66 | | } |
| |
65 | 67 | | |
| |
66 | 68 | | // class methods |
| |
|
|
 |
… |
|
84 | 86 | | return $this->_sort_order; |
| |
85 | 87 | | } |
| |
86 | 88 | | |
  |
87 | | - | function calculateQuotes() { |
| |
88 | | - | global $osC_ShoppingCart; |
| |
89 | | - | |
| |
90 | | - | $this->_quotes = array(); |
| |
91 | | - | |
| |
92 | | - | if (is_array($this->_modules)) { |
| |
93 | | - | $this->_shipping_weight = $osC_ShoppingCart->getWeight(); |
| |
94 | | - | |
| |
95 | | - | if (SHIPPING_BOX_WEIGHT >= ($this->_shipping_weight * SHIPPING_BOX_PADDING/100)) { |
| |
96 | | - | $this->_shipping_weight = $this->_shipping_weight + SHIPPING_BOX_WEIGHT; |
| |
97 | | - | } else { |
| |
98 | | - | $this->_shipping_weight = $this->_shipping_weight + ($this->_shipping_weight * SHIPPING_BOX_PADDING/100); |
| |
99 | | - | } |
| |
100 | | - | |
| |
101 | | - | if ($this->_shipping_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes |
| |
102 | | - | $this->_shipping_boxes = ceil($this->_shipping_weight / SHIPPING_MAX_WEIGHT); |
| |
103 | | - | $this->_shipping_weight = $this->_shipping_weight / $this->_shipping_boxes; |
| |
104 | | - | } |
| |
105 | | - | |
| |
106 | | - | $include_quotes = array(); |
| |
107 | | - | |
| |
108 | | - | if (defined('MODULE_SHIPPING_FREE_STATUS') && (MODULE_SHIPPING_FREE_STATUS == 'True') && ($GLOBALS['osC_Shipping_free']->getStatus() === true)) { |
| |
109 | | - | $include_quotes[] = 'free'; |
| |
110 | | - | } else { |
| |
111 | | - | foreach ($this->_modules as $module) { |
| |
112 | | - | if ($GLOBALS['osC_Shipping_' . $module]->getStatus() === true) { |
| |
113 | | - | $include_quotes[] = 'osC_Shipping_' . $module; |
| |
114 | | - | } |
| |
115 | | - | } |
| |
116 | | - | } |
| |
117 | | - | |
| |
118 | | - | foreach ($include_quotes as $module) { |
| |
119 | | - | $quotes = $GLOBALS[$module]->quote(); |
| |
120 | | - | |
| |
121 | | - | if (is_array($quotes)) { |
| |
122 | | - | $this->_quotes[] = $quotes; |
| |
123 | | - | } |
| |
124 | | - | } |
| |
125 | | - | } |
| |
126 | | - | } |
| |
127 | | - | |
|
128 | 89 | | function hasQuotes() { |
| |
129 | 90 | | return !empty($this->_quotes); |
| |
130 | 91 | | } |
| |
|
|
 |
… |
|
139 | 100 | | return $total_quotes; |
| |
140 | 101 | | } |
| |
141 | 102 | | |
  |
142 | | - | function getQuotes($module = '') { |
| |
143 | | - | if (empty($module) === false) { |
| |
144 | | - | list($module, $method) = explode('_', $module); |
| |
| 103 | + | function getQuotes() { |
| |
| 104 | + | return $this->_quotes; |
| |
| 105 | + | } |
|
145 | 106 | | |
  |
146 | | - | foreach ($this->_quotes as $quote) { |
| |
147 | | - | if ($quote['id'] == $module) { |
| |
148 | | - | foreach ($quote['methods'] as $quote_method) { |
| |
149 | | - | if ($quote_method['id'] == $method) { |
| |
150 | | - | return array('id' => $quote['id'], |
| |
151 | | - | 'module' => $quote['module'], |
| |
152 | | - | 'methods' => $quote_method, |
| |
153 | | - | 'tax_class_id' => $quote['tax_class_id']); |
| |
154 | | - | } |
| |
155 | | - | } |
| |
156 | | - | } |
| |
157 | | - | } |
| |
| 107 | + | function getQuote($module = '') { |
| |
| 108 | + | if (empty($module)) { |
| |
| 109 | + | $module = $this->_selected_module; |
|
158 | 110 | | } |
| |
159 | 111 | | |
  |
160 | | - | return $this->_quotes; |
| |
161 | | - | } |
| |
| 112 | + | list($module_id, $method_id) = explode('_', $module); |
|
162 | 113 | | |
  |
163 | | - | function getCheapestQuote() { |
| |
164 | | - | if (is_array($this->_modules)) { |
| |
165 | | - | $rates = array(); |
| |
| 114 | + | $rate = array(); |
|
166 | 115 | | |
  |
167 | | - | foreach ($this->_modules as $value) { |
| |
168 | | - | $class = 'osC_Shipping_' . $value; |
| |
169 | | - | if ($GLOBALS[$class]->getStatus() === true) { |
| |
170 | | - | $quotes = $GLOBALS[$class]->quotes; |
| |
| 116 | + | foreach ($this->_quotes as $quote) { |
| |
| 117 | + | if ($quote['id'] == $module_id) { |
| |
| 118 | + | foreach ($quote['methods'] as $method) { |
| |
| 119 | + | if ($method['id'] == $method_id) { |
| |
| 120 | + | $rate = array('id' => $module, |
| |
| 121 | + | 'title' => $quote['module'] . ((empty($method['title']) === false) ? ' (' . $method['title'] . ')' : ''), |
| |
| 122 | + | 'cost' => $method['cost'], |
| |
| 123 | + | 'tax_class_id' => $quote['tax_class_id']); |
|
171 | 124 | | |
  |
172 | | - | for ($i=0, $n=sizeof($quotes['methods']); $i<$n; $i++) { |
| |
173 | | - | if (isset($quotes['methods'][$i]['cost']) && tep_not_null($quotes['methods'][$i]['cost'])) { |
| |
174 | | - | $rates[] = array('id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'], |
| |
175 | | - | 'title' => $quotes['module'] . ' (' . $quotes['methods'][$i]['title'] . ')', |
| |
176 | | - | 'cost' => $quotes['methods'][$i]['cost'], |
| |
177 | | - | 'tax_class_id' => $quotes['tax_class_id']); |
| |
178 | | - | } |
| |
| 125 | + | break 2; |
|
179 | 126 | | } |
| |
180 | 127 | | } |
| |
181 | 128 | | } |
  |
| 129 | + | } |
|
182 | 130 | | |
  |
183 | | - | $cheapest = false; |
| |
| 131 | + | return $rate; |
| |
| 132 | + | } |
|
184 | 133 | | |
  |
185 | | - | for ($i=0, $n=sizeof($rates); $i<$n; $i++) { |
| |
186 | | - | if (is_array($cheapest)) { |
| |
187 | | - | if ($rates[$i]['cost'] < $cheapest['cost']) { |
| |
188 | | - | $cheapest = $rates[$i]; |
| |
189 | | - | } |
| |
190 | | - | } else { |
| |
191 | | - | $cheapest = $rates[$i]; |
| |
| 134 | + | function getCheapestQuote() { |
| |
| 135 | + | $rate = array(); |
| |
| 136 | + | |
| |
| 137 | + | foreach ($this->_quotes as $quote) { |
| |
| 138 | + | foreach ($quote['methods'] as $method) { |
| |
| 139 | + | if (empty($rate) || ($method['cost'] < $rate['cost'])) { |
| |
| 140 | + | $rate = array('id' => $quote['id'] . '_' . $method['id'], |
| |
| 141 | + | 'title' => $quote['module'] . ((empty($method['title']) === false) ? ' (' . $method['title'] . ')' : ''), |
| |
| 142 | + | 'cost' => $method['cost'], |
| |
| 143 | + | 'tax_class_id' => $quote['tax_class_id'], |
| |
| 144 | + | 'is_cheapest' => true); |
|
192 | 145 | | } |
| |
193 | 146 | | } |
  |
194 | | - | |
| |
195 | | - | return $cheapest; |
|
196 | 147 | | } |
  |
| 148 | + | |
| |
| 149 | + | return $rate; |
|
197 | 150 | | } |
| |
198 | 151 | | |
| |
199 | 152 | | function hasActive() { |
| |
|
|
 |
… |
|
292 | 245 | | } |
| |
293 | 246 | | } |
| |
294 | 247 | | |
  |
| 248 | + | function _calculate() { |
| |
| 249 | + | global $osC_ShoppingCart; |
| |
| 250 | + | |
| |
| 251 | + | if ($this->_cartID != $osC_ShoppingCart->getCartID()) { |
| |
| 252 | + | $this->_cartID = $osC_ShoppingCart->getCartID(); |
| |
| 253 | + | |
| |
| 254 | + | $this->_quotes = array(); |
| |
| 255 | + | |
| |
| 256 | + | if (is_array($this->_modules)) { |
| |
| 257 | + | $include_quotes = array(); |
| |
| 258 | + | |
| |
| 259 | + | if (defined('MODULE_SHIPPING_FREE_STATUS') && (MODULE_SHIPPING_FREE_STATUS == 'True') && ($GLOBALS['osC_Shipping_free']->getStatus() === true)) { |
| |
| 260 | + | $include_quotes[] = 'osC_Shipping_free'; |
| |
| 261 | + | } else { |
| |
| 262 | + | foreach ($this->_modules as $module) { |
| |
| 263 | + | if ($GLOBALS['osC_Shipping_' . $module]->getStatus() === true) { |
| |
| 264 | + | $include_quotes[] = 'osC_Shipping_' . $module; |
| |
| 265 | + | } |
| |
| 266 | + | } |
| |
| 267 | + | } |
| |
| 268 | + | |
| |
| 269 | + | foreach ($include_quotes as $module) { |
| |
| 270 | + | $quotes = $GLOBALS[$module]->quote(); |
| |
| 271 | + | |
| |
| 272 | + | if (is_array($quotes)) { |
| |
| 273 | + | $this->_quotes[] = $quotes; |
| |
| 274 | + | } |
| |
| 275 | + | } |
| |
| 276 | + | } |
| |
| 277 | + | } |
| |
| 278 | + | } |
| |
| 279 | + | |
  |
295 | 280 | | function _usortModules($a, $b) { |
| |
296 | 281 | | if ($GLOBALS['osC_Shipping_' . $a]->getSortOrder() == $GLOBALS['osC_Shipping_' . $b]->getSortOrder()) { |
| |
297 | 282 | | return strnatcasecmp($GLOBALS['osC_Shipping_' . $a]->getTitle(), $GLOBALS['osC_Shipping_' . $a]->getTitle()); |