Quick Search:

View

Revision:

Diff

Diff from 432 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/shipping.php

Annotated File View

hpdl
1
1 <?php
2 /*
hpdl
153
3   $Id: shipping.php 432 2006-02-15 07:41:34Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
404
8   Copyright (c) 2006 osCommerce
hpdl
1
9
10   Released under the GNU General Public License
11 */
12
hpdl
421
13   class osC_Shipping {
14     var $_modules = array(),
15         $_quotes = array(),
16         $_shipping_boxes = 1,
hpdl
425
17         $_shipping_weight = 0,
hpdl
432
18         $_keys,
19         $_group = 'shipping';
hpdl
1
20
21 // class constructor
hpdl
421
22     function osC_Shipping($module = '') {
hpdl
425
23       global $osC_Database, $osC_Language;
hpdl
377
24
hpdl
421
25       if (isset($_SESSION['osC_Shipping_data']) === false) {
26         $_SESSION['osC_Shipping_data'] = array('shipping_boxes' => 1,
27                                                'shipping_weight' => 0,
28                                                'quotes' => array());
29       }
hpdl
1
30
hpdl
421
31       $this->_shipping_boxes =& $_SESSION['osC_Shipping_data']['shipping_boxes'];
32       $this->_shipping_weight =& $_SESSION['osC_Shipping_data']['shipping_weight'];
33       $this->_quotes =& $_SESSION['osC_Shipping_data']['quotes'];
34
hpdl
425
35       $Qmodules = $osC_Database->query('select code from :table_templates_boxes where modules_group = "shipping"');
36       $Qmodules->bindTable(':table_templates_boxes', TABLE_TEMPLATES_BOXES);
37       $Qmodules->setCache('modules-shipping');
38       $Qmodules->execute();
hpdl
421
39
hpdl
425
40       while ($Qmodules->next()) {
41         $this->_modules[] = $Qmodules->value('code');
42       }
43
44       $Qmodules->freeResult();
45
46       if (empty($this->_modules) === false) {
47         if ((empty($module) === false) && in_array(substr($module['id'], 0, strpos($module['id'], '_')), $this->_modules)) {
hpdl
427
48           $this->_modules = array($module['id']);
hpdl
1
49         }
50
hpdl
404
51         $osC_Language->load('modules-shipping');
52
hpdl
427
53         foreach ($this->_modules as $module) {
54           include('includes/modules/shipping/' . $module . '.' . substr(basename(__FILE__), (strrpos(basename(__FILE__), '.')+1)));
hpdl
1
55
hpdl
427
56           $module_class = 'osC_Shipping_' . $module;
57
58           $GLOBALS[$module_class] = new $module_class();
59           $GLOBALS[$module_class]->initialize();
hpdl
1
60         }
hpdl
427
61
62         usort($this->_modules, array('osC_Shipping', '_usortModules'));
hpdl
1
63       }
64     }
65
hpdl
432
66 // class methods
67     function getCode() {
68       return $this->_code;
69     }
70
71     function getTitle() {
72       return $this->_title;
73     }
74
75     function getDescription() {
76       return $this->_description;
77     }
78
79     function getStatus() {
80       return $this->_status;
81     }
82
83     function getSortOrder() {
84       return $this->_sort_order;
85     }
86
hpdl
425
87     function calculateQuotes() {
hpdl
421
88       global $osC_ShoppingCart;
hpdl
1
89
hpdl
421
90       $this->_quotes = array();
hpdl
1
91
hpdl
421
92       if (is_array($this->_modules)) {
93         $this->_shipping_weight = $osC_ShoppingCart->getWeight();
hpdl
1
94
hpdl
421
95         if (SHIPPING_BOX_WEIGHT >= ($this->_shipping_weight * SHIPPING_BOX_PADDING/100)) {
96           $this->_shipping_weight = $this->_shipping_weight + SHIPPING_BOX_WEIGHT;
hpdl
1
97         } else {
hpdl
421
98           $this->_shipping_weight = $this->_shipping_weight + ($this->_shipping_weight * SHIPPING_BOX_PADDING/100);
hpdl
1
99         }
100
hpdl
421
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;
hpdl
1
104         }
105
106         $include_quotes = array();
107
hpdl
432
108         if (defined('MODULE_SHIPPING_FREE_STATUS') && (MODULE_SHIPPING_FREE_STATUS == 'True') && ($GLOBALS['osC_Shipping_free']->getStatus() === true)) {
hpdl
422
109           $include_quotes[] = 'free';
110         } else {
hpdl
425
111           foreach ($this->_modules as $module) {
hpdl
432
112             if ($GLOBALS['osC_Shipping_' . $module]->getStatus() === true) {
hpdl
425
113               $include_quotes[] = 'osC_Shipping_' . $module;
hpdl
1
114             }
115           }
116         }
117
hpdl
421
118         foreach ($include_quotes as $module) {
hpdl
425
119           $quotes = $GLOBALS[$module]->quote();
hpdl
421
120
121           if (is_array($quotes)) {
122             $this->_quotes[] = $quotes;
123           }
hpdl
1
124         }
125       }
hpdl
421
126     }
hpdl
1
127
hpdl
421
128     function hasQuotes() {
129       return !empty($this->_quotes);
hpdl
1
130     }
131
hpdl
422
132     function numberOfQuotes() {
133       $total_quotes = 0;
134
135       foreach ($this->_quotes as $quotes) {
136         $total_quotes += sizeof($quotes['methods']);
137       }
138
139       return $total_quotes;
140     }
141
hpdl
425
142     function getQuotes($module = '') {
143       if (empty($module) === false) {
144         list($module, $method) = explode('_', $module);
145
hpdl
421
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' => $quote['tax']);
154               }
155             }
156           }
157         }
158       }
159
160       return $this->_quotes;
161     }
162
163     function getCheapestQuote() {
164       if (is_array($this->_modules)) {
hpdl
1
165         $rates = array();
166
hpdl
421
167         foreach ($this->_modules as $value) {
hpdl
425
168           $class = 'osC_Shipping_' . $value;
hpdl
432
169           if ($GLOBALS[$class]->getStatus() === true) {
hpdl
1
170             $quotes = $GLOBALS[$class]->quotes;
hpdl
421
171
hpdl
1
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               }
178             }
179           }
180         }
181
182         $cheapest = false;
hpdl
421
183
hpdl
1
184         for ($i=0, $n=sizeof($rates); $i<$n; $i++) {
185           if (is_array($cheapest)) {
186             if ($rates[$i]['cost'] < $cheapest['cost']) {
187               $cheapest = $rates[$i];
188             }
189           } else {
190             $cheapest = $rates[$i];
191           }
192         }
193
194         return $cheapest;
195       }
196     }
hpdl
425
197
198     function hasActive() {
199       static $has_active;
200
201       if (isset($has_active) === false) {
202         $has_active = false;
203
204         foreach ($this->_modules as $module) {
hpdl
432
205           if ($GLOBALS['osC_Shipping_' . $module]->getStatus() === true) {
hpdl
425
206             $has_active = true;
207             break;
208           }
209         }
210       }
211
212       return $has_active;
213     }
214
215     function hasKeys() {
216       static $has_keys;
217
218       if (isset($has_keys) === false) {
219         $has_keys = (sizeof($this->getKeys()) > 0) ? true : false;
220       }
221
222       return $has_keys;
223     }
224
225     function install() {
226       global $osC_Database, $osC_Language;
227
228       $Qinstall = $osC_Database->query('insert into :table_templates_boxes (title, code, author_name, author_www, modules_group) values (:title, :code, :author_name, :author_www, :modules_group)');
229       $Qinstall->bindTable(':table_templates_boxes', TABLE_TEMPLATES_BOXES);
230       $Qinstall->bindValue(':title', $this->_title);
231       $Qinstall->bindValue(':code', $this->_code);
232       $Qinstall->bindValue(':author_name', $this->_author_name);
233       $Qinstall->bindValue(':author_www', $this->_author_www);
234       $Qinstall->bindValue(':modules_group', $this->_group);
235       $Qinstall->execute();
236
237       foreach ($osC_Language->getAll() as $key => $value) {
238         if (file_exists(dirname(__FILE__) . '/../languages/' . $key . '/modules/' . $this->_group . '/' . $this->_code . '.xml')) {
239           foreach ($osC_Language->extractDefinitions($key . '/modules/' . $this->_group . '/' . $this->_code . '.xml') as $def) {
240             $Qcheck = $osC_Database->query('select id from :table_languages_definitions where definition_key = :definition_key and content_group = :content_group and languages_id = :languages_id limit 1');
241             $Qcheck->bindTable(':table_languages_definitions', TABLE_LANGUAGES_DEFINITIONS);
242             $Qcheck->bindValue(':definition_key', $def['key']);
243             $Qcheck->bindValue(':content_group', $def['group']);
244             $Qcheck->bindInt(':languages_id', $value['id']);
245             $Qcheck->execute();
246
247             if ($Qcheck->numberOfRows() === 1) {
248               $Qdef = $osC_Database->query('update :table_languages_definitions set definition_value = :definition_value where definition_key = :definition_key and content_group = :content_group and languages_id = :languages_id');
249             } else {
250               $Qdef = $osC_Database->query('insert into :table_languages_definitions (languages_id, content_group, definition_key, definition_value) values (:languages_id, :content_group, :definition_key, :definition_value)');
251             }
252             $Qdef->bindTable(':table_languages_definitions', TABLE_LANGUAGES_DEFINITIONS);
253             $Qdef->bindInt(':languages_id', $value['id']);
254             $Qdef->bindValue(':content_group', $def['group']);
255             $Qdef->bindValue(':definition_key', $def['key']);
256             $Qdef->bindValue(':definition_value', $def['value']);
257             $Qdef->execute();
258           }
259         }
260       }
261
262       osC_Cache::clear('languages');
263     }
264
265     function remove() {
266       global $osC_Database, $osC_Language;
267
268       $Qdel = $osC_Database->query('delete from :table_templates_boxes where code = :code and modules_group = :modules_group');
269       $Qdel->bindTable(':table_templates_boxes', TABLE_TEMPLATES_BOXES);
270       $Qdel->bindValue(':code', $this->_code);
271       $Qdel->bindValue(':modules_group', $this->_group);
272       $Qdel->execute();
273
274       if ($this->hasKeys()) {
275         $Qdel = $osC_Database->query('delete from :table_configuration where configuration_key in (":configuration_key")');
276         $Qdel->bindTable(':table_configuration', TABLE_CONFIGURATION);
277         $Qdel->bindRaw(':configuration_key', implode('", "', $this->getKeys()));
278         $Qdel->execute();
279       }
280
281       if (file_exists(dirname(__FILE__) . '/../languages/' . $osC_Language->getCode() . '/modules/' . $this->_group . '/' . $this->_code . '.xml')) {
282         foreach ($osC_Language->extractDefinitions($osC_Language->getCode() . '/modules/' . $this->_group . '/' . $this->_code . '.xml') as $def) {
283           $Qdel = $osC_Database->query('delete from :table_languages_definitions where definition_key = :definition_key and content_group = :content_group');
284           $Qdel->bindTable(':table_languages_definitions', TABLE_LANGUAGES_DEFINITIONS);
285           $Qdel->bindValue(':definition_key', $def['key']);
286           $Qdel->bindValue(':content_group', $def['group']);
287           $Qdel->execute();
288         }
289
290         osC_Cache::clear('languages');
291       }
292     }
hpdl
427
293
294     function _usortModules($a, $b) {
hpdl
432
295       if ($GLOBALS['osC_Shipping_' . $a]->getSortOrder() == $GLOBALS['osC_Shipping_' . $b]->getSortOrder()) {
296         return strnatcasecmp($GLOBALS['osC_Shipping_' . $a]->getTitle(), $GLOBALS['osC_Shipping_' . $a]->getTitle());
hpdl
427
297       }
298
hpdl
432
299       return ($GLOBALS['osC_Shipping_' . $a]->getSortOrder() < $GLOBALS['osC_Shipping_' . $b]->getSortOrder()) ? -1 : 1;
hpdl
427
300     }
hpdl
1
301   }
302 ?>