Quick Search:

View

Revision:

Diff

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