Quick Search:

View

Revision:

Diff

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