Quick Search:

View

Revision:

Diff

Diff from 1497 to:

Annotations

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

Annotated File View

hpdl
1
1 <?php
2 /*
hpdl
19
3   $Id: shopping_cart.php 1497 2007-03-29 13:40:05Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1420
8   Copyright (c) 2007 osCommerce
hpdl
1
9
hpdl
1497
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
1
13 */
14
hpdl
418
15   class osC_ShoppingCart {
hpdl
420
16     var $_contents = array(),
hpdl
435
17         $_sub_total = 0,
hpdl
420
18         $_total = 0,
19         $_weight = 0,
hpdl
421
20         $_tax = 0,
hpdl
435
21         $_tax_groups = array(),
hpdl
420
22         $_content_type,
23         $_products_in_stock = true;
hpdl
1
24
hpdl
418
25     function osC_ShoppingCart() {
hpdl
809
26       if (!isset($_SESSION['osC_ShoppingCart_data'])) {
hpdl
418
27         $_SESSION['osC_ShoppingCart_data'] = array('contents' => array(),
hpdl
435
28                                                    'sub_total_cost' => 0,
hpdl
419
29                                                    'total_cost' => 0,
30                                                    'total_weight' => 0,
hpdl
435
31                                                    'tax' => 0,
32                                                    'tax_groups' => array(),
hpdl
439
33                                                    'shipping_boxes_weight' => 0,
34                                                    'shipping_boxes' => 1,
35                                                    'shipping_address' => array('zone_id' => STORE_ZONE, 'country_id' => STORE_COUNTRY),
hpdl
421
36                                                    'shipping_method' => array(),
hpdl
439
37                                                    'billing_address' => array('zone_id' => STORE_ZONE, 'country_id' => STORE_COUNTRY),
38                                                    'billing_method' => array(),
hpdl
1420
39                                                    'shipping_quotes' => array(),
hpdl
439
40                                                    'order_totals' => array());
hpdl
438
41
42         $this->resetShippingAddress();
43         $this->resetBillingAddress();
hpdl
418
44       }
45
hpdl
420
46       $this->_contents =& $_SESSION['osC_ShoppingCart_data']['contents'];
hpdl
435
47       $this->_sub_total =& $_SESSION['osC_ShoppingCart_data']['sub_total_cost'];
hpdl
420
48       $this->_total =& $_SESSION['osC_ShoppingCart_data']['total_cost'];
49       $this->_weight =& $_SESSION['osC_ShoppingCart_data']['total_weight'];
hpdl
435
50       $this->_tax =& $_SESSION['osC_ShoppingCart_data']['tax'];
51       $this->_tax_groups =& $_SESSION['osC_ShoppingCart_data']['tax_groups'];
hpdl
439
52       $this->_shipping_boxes_weight =& $_SESSION['osC_ShoppingCart_data']['shipping_boxes_weight'];
53       $this->_shipping_boxes =& $_SESSION['osC_ShoppingCart_data']['shipping_boxes'];
hpdl
421
54       $this->_shipping_address =& $_SESSION['osC_ShoppingCart_data']['shipping_address'];
55       $this->_shipping_method =& $_SESSION['osC_ShoppingCart_data']['shipping_method'];
56       $this->_billing_address =& $_SESSION['osC_ShoppingCart_data']['billing_address'];
57       $this->_billing_method =& $_SESSION['osC_ShoppingCart_data']['billing_method'];
hpdl
1420
58       $this->_shipping_quotes =& $_SESSION['osC_ShoppingCart_data']['shipping_quotes'];
hpdl
439
59       $this->_order_totals =& $_SESSION['osC_ShoppingCart_data']['order_totals'];
hpdl
1
60     }
61
hpdl
1420
62     function update() {
63       if ( !isset($_SESSION['cartID']) ) {
64         $this->_calculate();
65       }
66     }
67
hpdl
418
68     function hasContents() {
hpdl
420
69       return !empty($this->_contents);
hpdl
418
70     }
71
72     function synchronizeWithDatabase() {
hpdl
809
73       global $osC_Database, $osC_Services, $osC_Language, $osC_Customer, $osC_Image;
hpdl
1
74
hpdl
809
75       if (!$osC_Customer->isLoggedOn()) {
hpdl
418
76         return false;
77       }
hpdl
1
78
79 // insert current cart contents in database
hpdl
418
80       if ($this->hasContents()) {
hpdl
809
81         foreach ($this->_contents as $products_id_string => $data) {
hpdl
420
82           $Qproduct = $osC_Database->query('select products_id, customers_basket_quantity from :table_customers_basket where customers_id = :customers_id and products_id = :products_id');
hpdl
19
83           $Qproduct->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
hpdl
184
84           $Qproduct->bindInt(':customers_id', $osC_Customer->getID());
hpdl
809
85           $Qproduct->bindValue(':products_id', $products_id_string);
hpdl
19
86           $Qproduct->execute();
87
hpdl
418
88           if ($Qproduct->numberOfRows() > 0) {
89             $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity where customers_id = :customers_id and products_id = :products_id');
90             $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
hpdl
420
91             $Qupdate->bindInt(':customers_basket_quantity', $data['quantity'] + $Qproduct->valueInt('customers_basket_quantity'));
hpdl
418
92             $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
hpdl
809
93             $Qupdate->bindValue(':products_id', $products_id_string);
hpdl
418
94             $Qupdate->execute();
95           } else {
hpdl
387
96             $Qnew = $osC_Database->query('insert into :table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values (:customers_id, :products_id, :customers_basket_quantity, now())');
hpdl
19
97             $Qnew->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
hpdl
184
98             $Qnew->bindInt(':customers_id', $osC_Customer->getID());
hpdl
809
99             $Qnew->bindValue(':products_id', $products_id_string);
hpdl
420
100             $Qnew->bindInt(':customers_basket_quantity', $data['quantity']);
hpdl
19
101             $Qnew->execute();
hpdl
1
102           }
103         }
104       }
105
106 // reset per-session cart contents, but not the database contents
hpdl
418
107       $this->reset();
hpdl
1
108
hpdl
599
109       $Qproducts = $osC_Database->query('select cb.products_id, cb.customers_basket_quantity, cb.customers_basket_date_added, p.products_price, p.products_tax_class_id, p.products_weight, p.products_weight_class, pd.products_name, pd.products_keyword, i.image from :table_customers_basket cb, :table_products p left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag), :table_products_description pd where cb.customers_id = :customers_id and cb.products_id = p.products_id and p.products_id = pd.products_id and pd.language_id = :language_id order by cb.customers_basket_date_added desc');
hpdl
19
110       $Qproducts->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
hpdl
418
111       $Qproducts->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
556
112       $Qproducts->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
hpdl
420
113       $Qproducts->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
hpdl
583
114       $Qproducts->bindInt(':default_flag', 1);
hpdl
184
115       $Qproducts->bindInt(':customers_id', $osC_Customer->getID());
hpdl
420
116       $Qproducts->bindInt(':language_id', $osC_Language->getID());
hpdl
19
117       $Qproducts->execute();
118
119       while ($Qproducts->next()) {
hpdl
809
120         $product = explode('#', $Qproducts->value('products_id'), 2);
121         $attributes_array = array();
122
123         if (isset($product[1])) {
124           $attributes = explode(';', $product[1]);
125
126           foreach ($attributes as $set) {
127             $attribute = explode(':', $set);
128
129             if (!is_numeric($attribute[0]) || !is_numeric($attribute[1])) {
130               continue 2; // skip product
131             }
132
133             $attributes_array[$attribute[0]] = $attribute[1];
134           }
135         }
136
hpdl
420
137         $price = $Qproducts->value('products_price');
hpdl
418
138
hpdl
809
139         if ($osC_Services->isStarted('specials')) {
140           global $osC_Specials;
hpdl
418
141
hpdl
809
142           if ($new_price = $osC_Specials->getPrice(osc_get_product_id($Qproducts->value('products_id')))) {
143             $price = $new_price;
144           }
hpdl
418
145         }
146
hpdl
420
147         $this->_contents[$Qproducts->value('products_id')] = array('id' => $Qproducts->value('products_id'),
148                                                                    'name' => $Qproducts->value('products_name'),
149                                                                    'keyword' => $Qproducts->value('products_keyword'),
hpdl
555
150                                                                    'image' => $Qproducts->value('image'),
hpdl
420
151                                                                    'price' => $price,
152                                                                    'final_price' => $price,
153                                                                    'quantity' => $Qproducts->valueInt('customers_basket_quantity'),
154                                                                    'weight' => $Qproducts->value('products_weight'),
155                                                                    'tax_class_id' => $Qproducts->valueInt('products_tax_class_id'),
156                                                                    'date_added' => osC_DateTime::getShort($Qproducts->value('customers_basket_date_added')),
157                                                                    'weight_class_id' => $Qproducts->valueInt('products_weight_class'));
158
hpdl
809
159         if (!empty($attributes_array)) {
160           foreach ($attributes_array as $option_id => $value_id) {
161             $Qattributes = $osC_Database->query('select pa.options_values_price, pa.price_prefix, po.products_options_name, pov.products_options_values_name from :table_products_attributes pa, :table_products_options po, :table_products_options_values pov where pa.products_id = :products_id and pa.options_id = :options_id and pa.options_values_id = :options_values_id and pa.options_id = po.products_options_id and po.language_id = :language_id and pa.options_values_id = pov.products_options_values_id and pov.language_id = :language_id');
162             $Qattributes->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
163             $Qattributes->bindTable(':table_products_options', TABLE_PRODUCTS_OPTIONS);
164             $Qattributes->bindTable(':table_products_options_values', TABLE_PRODUCTS_OPTIONS_VALUES);
165             $Qattributes->bindInt(':products_id', osc_get_product_id($Qproducts->value('products_id')));
166             $Qattributes->bindInt(':options_id', $option_id);
167             $Qattributes->bindInt(':options_values_id', $value_id);
168             $Qattributes->bindInt(':language_id', $osC_Language->getID());
169             $Qattributes->bindInt(':language_id', $osC_Language->getID());
170             $Qattributes->execute();
hpdl
19
171
hpdl
810
172             if ($Qattributes->numberOfRows() > 0) {
hpdl
809
173               $this->_contents[$Qproducts->value('products_id')]['attributes'][$option_id] = array('options_id' => $option_id,
174                                                                                                    'options_values_id' => $value_id,
175                                                                                                    'products_options_name' => $Qattributes->value('products_options_name'),
176                                                                                                    'products_options_values_name' => $Qattributes->value('products_options_values_name'),
177                                                                                                    'options_values_price' => $Qattributes->value('options_values_price'),
178                                                                                                    'price_prefix' => $Qattributes->value('price_prefix'));
hpdl
420
179
hpdl
809
180               if ($Qattributes->value('price_prefix') == '+') {
181                 $this->_contents[$Qproducts->value('products_id')]['final_price'] += $Qattributes->value('options_values_price');
182               } else {
183                 $this->_contents[$Qproducts->value('products_id')]['final_price'] -= $Qattributes->value('options_values_price');
184               }
hpdl
810
185             } else {
186               unset($this->_contents[$Qproducts->value('products_id')]);
187               continue 2; // skip product
hpdl
809
188             }
hpdl
420
189           }
hpdl
1
190         }
191       }
192
hpdl
418
193       $this->_cleanUp();
hpdl
419
194       $this->_calculate();
hpdl
1
195     }
196
197     function reset($reset_database = false) {
hpdl
199
198       global $osC_Database, $osC_Customer;
hpdl
1
199
hpdl
418
200       if (($reset_database === true) && $osC_Customer->isLoggedOn()) {
hpdl
19
201         $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id');
202         $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
hpdl
184
203         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
hpdl
19
204         $Qdelete->execute();
hpdl
418
205       }
hpdl
19
206
hpdl
420
207       $this->_contents = array();
hpdl
435
208       $this->_sub_total = 0;
hpdl
420
209       $this->_total = 0;
210       $this->_weight = 0;
hpdl
435
211       $this->_tax = 0;
212       $this->_tax_groups = array();
hpdl
420
213       $this->_content_type = null;
hpdl
439
214
hpdl
438
215       $this->resetShippingAddress();
hpdl
439
216       $this->resetShippingMethod();
hpdl
438
217       $this->resetBillingAddress();
hpdl
439
218       $this->resetBillingMethod();
hpdl
1420
219
220       if ( isset($_SESSION['cartID']) ) {
221         unset($_SESSION['cartID']);
222       }
hpdl
1
223     }
224
hpdl
809
225     function add($products_id_string, $attributes = null, $quantity = null) {
226       global $osC_Database, $osC_Services, $osC_Language, $osC_Customer, $osC_Image;
hpdl
1
227
hpdl
809
228       $products_id_string = osc_get_product_id_string($products_id_string, $attributes);
hpdl
734
229       $products_id = osc_get_product_id($products_id_string);
hpdl
1
230
hpdl
418
231       if (is_numeric($products_id)) {
hpdl
599
232         $Qcheck = $osC_Database->query('select p.products_price, p.products_tax_class_id, p.products_weight, p.products_weight_class, p.products_status, i.image from :table_products p left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag) where p.products_id = :products_id');
hpdl
19
233         $Qcheck->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
556
234         $Qcheck->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
hpdl
583
235         $Qcheck->bindInt(':default_flag', 1);
hpdl
19
236         $Qcheck->bindInt(':products_id', $products_id);
237         $Qcheck->execute();
hpdl
1
238
hpdl
418
239         if ($Qcheck->valueInt('products_status') === 1) {
240           if ($this->exists($products_id_string)) {
hpdl
809
241             if (!is_numeric($quantity)) {
hpdl
418
242               $quantity = $this->getQuantity($products_id_string) + 1;
243             }
244
hpdl
420
245             $this->_contents[$products_id_string]['quantity'] = $quantity;
hpdl
418
246
247 // update database
248             if ($osC_Customer->isLoggedOn()) {
249               $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity where customers_id = :customers_id and products_id = :products_id');
250               $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
251               $Qupdate->bindInt(':customers_basket_quantity', $quantity);
252               $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
253               $Qupdate->bindValue(':products_id', $products_id_string);
254               $Qupdate->execute();
255             }
hpdl
1
256           } else {
hpdl
809
257             if (!is_numeric($quantity)) {
hpdl
418
258               $quantity = 1;
259             }
260
hpdl
420
261             $Qproduct = $osC_Database->query('select products_name, products_keyword from :table_products_description where products_id = :products_id and language_id = :language_id');
262             $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
263             $Qproduct->bindInt(':products_id', $products_id);
264             $Qproduct->bindInt(':language_id', $osC_Language->getID());
265             $Qproduct->execute();
hpdl
418
266
hpdl
420
267             $price = $Qcheck->value('products_price');
268
hpdl
809
269             if ($osC_Services->isStarted('specials')) {
270               global $osC_Specials;
hpdl
418
271
hpdl
809
272               if ($new_price = $osC_Specials->getPrice($products_id)) {
273                 $price = $new_price;
274               }
hpdl
418
275             }
276
hpdl
420
277             $this->_contents[$products_id_string] = array('id' => $products_id_string,
278                                                           'name' => $Qproduct->value('products_name'),
279                                                           'keyword' => $Qproduct->value('products_keyword'),
hpdl
555
280                                                           'image' => $Qcheck->value('image'),
hpdl
420
281                                                           'price' => $price,
282                                                           'final_price' => $price,
283                                                           'quantity' => $quantity,
284                                                           'weight' => $Qcheck->value('products_weight'),
285                                                           'tax_class_id' => $Qcheck->valueInt('products_tax_class_id'),
286                                                           'date_added' => osC_DateTime::getShort(osC_DateTime::getNow()),
287                                                           'weight_class_id' => $Qcheck->valueInt('products_weight_class'));
288
hpdl
1
289 // insert into database
hpdl
19
290             if ($osC_Customer->isLoggedOn()) {
hpdl
387
291               $Qnew = $osC_Database->query('insert into :table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values (:customers_id, :products_id, :customers_basket_quantity, now())');
hpdl
19
292               $Qnew->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
hpdl
184
293               $Qnew->bindInt(':customers_id', $osC_Customer->getID());
hpdl
19
294               $Qnew->bindValue(':products_id', $products_id_string);
hpdl
418
295               $Qnew->bindInt(':customers_basket_quantity', $quantity);
hpdl
19
296               $Qnew->execute();
297             }
hpdl
1
298
hpdl
809
299             if (is_array($attributes) && !empty($attributes)) {
hpdl
418
300               foreach ($attributes as $option => $value) {
hpdl
420
301                 $Qattributes = $osC_Database->query('select pa.options_values_price, pa.price_prefix, po.products_options_name, pov.products_options_values_name from :table_products_attributes pa, :table_products_options po, :table_products_options_values pov where pa.products_id = :products_id and pa.options_id = :options_id and pa.options_values_id = :options_values_id and pa.options_id = po.products_options_id and po.language_id = :language_id and pa.options_values_id = pov.products_options_values_id and pov.language_id = :language_id');
hpdl
418
302                 $Qattributes->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
hpdl
420
303                 $Qattributes->bindTable(':table_products_options', TABLE_PRODUCTS_OPTIONS);
304                 $Qattributes->bindTable(':table_products_options_values', TABLE_PRODUCTS_OPTIONS_VALUES);
hpdl
418
305                 $Qattributes->bindValue(':products_id', $products_id);
306                 $Qattributes->bindInt(':options_id', $option);
307                 $Qattributes->bindInt(':options_values_id', $value);
hpdl
420
308                 $Qattributes->bindInt(':language_id', $osC_Language->getID());
309                 $Qattributes->bindInt(':language_id', $osC_Language->getID());
hpdl
418
310                 $Qattributes->execute();
311
hpdl
420
312                 $this->_contents[$products_id_string]['attributes'][$option] = array('options_id' => $option,
313                                                                                      'options_values_id' => $value,
314                                                                                      'products_options_name' => $Qattributes->value('products_options_name'),
315                                                                                      'products_options_values_name' => $Qattributes->value('products_options_values_name'),
316                                                                                      'options_values_price' => $Qattributes->value('options_values_price'),
317                                                                                      'price_prefix' => $Qattributes->value('price_prefix'));
hpdl
418
318
hpdl
420
319                 if ($Qattributes->value('price_prefix') == '+') {
320                   $this->_contents[$products_id_string]['final_price'] += $Qattributes->value('options_values_price');
321                 } else {
322                   $this->_contents[$products_id_string]['final_price'] -= $Qattributes->value('options_values_price');
323                 }
hpdl
1
324               }
325             }
326           }
327
hpdl
418
328           $this->_cleanUp();
hpdl
419
329           $this->_calculate();
hpdl
1
330         }
331       }
332     }
333
hpdl
418
334     function numberOfItems() {
335       $total = 0;
hpdl
1
336
hpdl
418
337       if ($this->hasContents()) {
hpdl
420
338         foreach (array_keys($this->_contents) as $products_id) {
hpdl
418
339           $total += $this->getQuantity($products_id);
hpdl
19
340         }
hpdl
1
341       }
342
hpdl
418
343       return $total;
hpdl
1
344     }
345
hpdl
418
346     function getQuantity($products_id) {
hpdl
420
347       if (isset($this->_contents[$products_id])) {
348         return $this->_contents[$products_id]['quantity'];
hpdl
1
349       }
hpdl
418
350
351       return 0;
hpdl
1
352     }
353
hpdl
418
354     function exists($products_id) {
hpdl
420
355       return isset($this->_contents[$products_id]);
hpdl
1
356     }
357
358     function remove($products_id) {
hpdl
19
359       global $osC_Database, $osC_Customer;
hpdl
1
360
hpdl
420
361       unset($this->_contents[$products_id]);
hpdl
418
362
hpdl
1
363 // remove from database
364       if ($osC_Customer->isLoggedOn()) {
hpdl
19
365         $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id and products_id = :products_id');
366         $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
hpdl
184
367         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
hpdl
19
368         $Qdelete->bindValue(':products_id', $products_id);
369         $Qdelete->execute();
hpdl
1
370       }
371
hpdl
419
372       $this->_calculate();
hpdl
1
373     }
374
hpdl
420
375     function getProducts() {
376       static $_is_sorted = false;
hpdl
19
377
hpdl
420
378       if ($_is_sorted === false) {
379         $_is_sorted = true;
hpdl
1
380
hpdl
420
381         uasort($this->_contents, array('osC_ShoppingCart', '_uasortProductsByDateAdded'));
hpdl
1
382       }
383
hpdl
420
384       return $this->_contents;
hpdl
1
385     }
386
hpdl
435
387     function getSubTotal() {
388       return $this->_sub_total;
389     }
390
hpdl
420
391     function getTotal() {
392       return $this->_total;
393     }
hpdl
1
394
hpdl
420
395     function getWeight() {
396       return $this->_weight;
hpdl
1
397     }
398
hpdl
420
399     function generateCartID($length = 5) {
hpdl
734
400       return osc_create_random_string($length, 'digits');
hpdl
1
401     }
402
hpdl
420
403     function getCartID() {
hpdl
1420
404       return $_SESSION['cartID'];
hpdl
1
405     }
406
hpdl
418
407     function getContentType() {
hpdl
19
408       global $osC_Database;
409
hpdl
420
410       $this->_content_type = 'physical';
hpdl
1
411
hpdl
500
412       if ( (DOWNLOAD_ENABLED == '1') && $this->hasContents() ) {
hpdl
420
413         foreach ($this->_contents as $products_id => $data) {
hpdl
418
414           if (isset($data['attributes'])) {
415             foreach ($data['attributes'] as $value) {
hpdl
19
416               $Qcheck = $osC_Database->query('select count(*) as total from :table_products_attributes pa, :table_products_attributes_download pad where pa.products_id = :products_id and pa.options_values_id = :options_values_id and pa.products_attributes_id = pad.products_attributes_id');
417               $Qcheck->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
418               $Qcheck->bindTable(':table_products_attributes_download', TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD);
419               $Qcheck->bindInt(':products_id', $products_id);
hpdl
420
420               $Qcheck->bindInt(':options_values_id', $value['options_values_id']);
hpdl
19
421               $Qcheck->execute();
hpdl
1
422
hpdl
19
423               if ($Qcheck->valueInt('total') > 0) {
hpdl
420
424                 switch ($this->_content_type) {
hpdl
1
425                   case 'physical':
hpdl
420
426                     $this->_content_type = 'mixed';
hpdl
1
427
hpdl
420
428                     return $this->_content_type;
hpdl
1
429                     break;
430                   default:
hpdl
420
431                     $this->_content_type = 'virtual';
hpdl
1
432                     break;
433                 }
434               } else {
hpdl
420
435                 switch ($this->_content_type) {
hpdl
1
436                   case 'virtual':
hpdl
420
437                     $this->_content_type = 'mixed';
hpdl
1
438
hpdl
420
439                     return $this->_content_type;
hpdl
1
440                     break;
441                   default:
hpdl
420
442                     $this->_content_type = 'physical';
hpdl
1
443                     break;
444                 }
445               }
446             }
447           } else {
hpdl
420
448             switch ($this->_content_type) {
hpdl
1
449               case 'virtual':
hpdl
420
450                 $this->_content_type = 'mixed';
hpdl
1
451
hpdl
420
452                 return $this->_content_type;
hpdl
1
453                 break;
454               default:
hpdl
420
455                 $this->_content_type = 'physical';
hpdl
1
456                 break;
457             }
458           }
459         }
460       }
461
hpdl
420
462       return $this->_content_type;
hpdl
1
463     }
hpdl
418
464
hpdl
420
465     function hasAttributes($products_id) {
hpdl
809
466       return isset($this->_contents[$products_id]['attributes']) && !empty($this->_contents[$products_id]['attributes']);
hpdl
420
467     }
468
469     function getAttributes($products_id) {
hpdl
809
470       if (isset($this->_contents[$products_id]['attributes']) && !empty($this->_contents[$products_id]['attributes'])) {
hpdl
420
471         return $this->_contents[$products_id]['attributes'];
472       }
473     }
474
475     function isInStock($products_id) {
476       global $osC_Database;
477
478       $Qstock = $osC_Database->query('select products_quantity from :table_products where products_id = :products_id');
479       $Qstock->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
734
480       $Qstock->bindInt(':products_id', osc_get_product_id($products_id));
hpdl
420
481       $Qstock->execute();
482
483       if (($Qstock->valueInt('products_quantity') - $this->_contents[$products_id]['quantity']) > 0) {
484         return true;
485       } elseif ($this->_products_in_stock === true) {
486         $this->_products_in_stock = false;
487       }
488
489       return false;
490     }
491
492     function hasStock() {
493       return $this->_products_in_stock;
494     }
495
hpdl
421
496     function hasShippingAddress() {
497       return isset($this->_shipping_address) && isset($this->_shipping_address['id']);
498     }
499
500     function setShippingAddress($address_id) {
501       global $osC_Database, $osC_Customer;
502
hpdl
439
503       $previous_address = false;
504
505       if (isset($this->_shipping_address['id'])) {
506         $previous_address = $this->getShippingAddress();
507       }
508
hpdl
732
509       $Qaddress = $osC_Database->query('select ab.entry_firstname, ab.entry_lastname, ab.entry_company, ab.entry_street_address, ab.entry_suburb, ab.entry_postcode, ab.entry_city, ab.entry_zone_id, ab.entry_telephone, z.zone_code, z.zone_name, ab.entry_country_id, c.countries_name, c.countries_iso_code_2, c.countries_iso_code_3, c.address_format, ab.entry_state from :table_address_book ab left join :table_zones z on (ab.entry_zone_id = z.zone_id) left join :table_countries c on (ab.entry_country_id = c.countries_id) where ab.customers_id = :customers_id and ab.address_book_id = :address_book_id');
hpdl
425
510       $Qaddress->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
511       $Qaddress->bindTable(':table_zones', TABLE_ZONES);
512       $Qaddress->bindTable(':table_countries', TABLE_COUNTRIES);
513       $Qaddress->bindInt(':customers_id', $osC_Customer->getID());
514       $Qaddress->bindInt(':address_book_id', $address_id);
515       $Qaddress->execute();
hpdl
421
516
517       $this->_shipping_address = array('id' => $address_id,
hpdl
425
518                                        'firstname' => $Qaddress->valueProtected('entry_firstname'),
519                                        'lastname' => $Qaddress->valueProtected('entry_lastname'),
520                                        'company' => $Qaddress->valueProtected('entry_company'),
521                                        'street_address' => $Qaddress->valueProtected('entry_street_address'),
522                                        'suburb' => $Qaddress->valueProtected('entry_suburb'),
523                                        'city' => $Qaddress->valueProtected('entry_city'),
524                                        'postcode' => $Qaddress->valueProtected('entry_postcode'),
hpdl
809
525                                        'state' => (!osc_empty($Qaddress->valueProtected('entry_state'))) ? $Qaddress->valueProtected('entry_state') : $Qaddress->valueProtected('zone_name'),
hpdl
425
526                                        'zone_id' => $Qaddress->valueInt('entry_zone_id'),
hpdl
479
527                                        'zone_code' => $Qaddress->value('zone_code'),
hpdl
425
528                                        'country_id' => $Qaddress->valueInt('entry_country_id'),
529                                        'country_title' => $Qaddress->value('countries_name'),
530                                        'country_iso_code_2' => $Qaddress->value('countries_iso_code_2'),
531                                        'country_iso_code_3' => $Qaddress->value('countries_iso_code_3'),
hpdl
732
532                                        'format' => $Qaddress->value('address_format'),
hpdl
479
533                                        'telephone_number' => $Qaddress->value('entry_telephone'));
hpdl
439
534
535       if ( is_array($previous_address) && ( ($previous_address['id'] != $this->_shipping_address['id']) || ($previous_address['country_id'] != $this->_shipping_address['country_id']) || ($previous_address['zone_id'] != $this->_shipping_address['zone_id']) || ($previous_address['state'] != $this->_shipping_address['state']) || ($previous_address['postcode'] != $this->_shipping_address['postcode']) ) ) {
536         $this->_calculate();
537       }
hpdl
421
538     }
539
540     function getShippingAddress($key = '') {
541       if (empty($key)) {
542         return $this->_shipping_address;
543       }
544
545       return $this->_shipping_address[$key];
546     }
547
hpdl
422
548     function resetShippingAddress() {
hpdl
438
549       global $osC_Customer;
550
hpdl
439
551       $this->_shipping_address = array('zone_id' => STORE_ZONE, 'country_id' => STORE_COUNTRY);
hpdl
438
552
553       if ($osC_Customer->isLoggedOn() && $osC_Customer->hasDefaultAddress()) {
554         $this->setShippingAddress($osC_Customer->getDefaultAddressID());
555       }
hpdl
422
556     }
557
hpdl
439
558     function setShippingMethod($shipping_array, $calculate_total = true) {
hpdl
421
559       $this->_shipping_method = $shipping_array;
hpdl
439
560
561       if ($calculate_total === true) {
562         $this->_calculate(false);
563       }
hpdl
421
564     }
565
566     function getShippingMethod($key = '') {
567       if (empty($key)) {
568         return $this->_shipping_method;
569       }
570
571       return $this->_shipping_method[$key];
572     }
573
574     function resetShippingMethod() {
575       $this->_shipping_method = array();
hpdl
438
576
hpdl
439
577       $this->_calculate();
hpdl
421
578     }
579
580     function hasShippingMethod() {
581       return !empty($this->_shipping_method);
582     }
583
584     function hasBillingAddress() {
hpdl
439
585       return isset($this->_billing_address) && isset($this->_billing_address['id']);
hpdl
421
586     }
587
588     function setBillingAddress($address_id) {
589       global $osC_Database, $osC_Customer;
590
hpdl
439
591       $previous_address = false;
592
593       if (isset($this->_billing_address['id'])) {
594         $previous_address = $this->getBillingAddress();
595       }
596
hpdl
732
597       $Qaddress = $osC_Database->query('select ab.entry_firstname, ab.entry_lastname, ab.entry_company, ab.entry_street_address, ab.entry_suburb, ab.entry_postcode, ab.entry_city, ab.entry_zone_id, ab.entry_telephone, z.zone_code, z.zone_name, ab.entry_country_id, c.countries_name, c.countries_iso_code_2, c.countries_iso_code_3, c.address_format, ab.entry_state from :table_address_book ab left join :table_zones z on (ab.entry_zone_id = z.zone_id) left join :table_countries c on (ab.entry_country_id = c.countries_id) where ab.customers_id = :customers_id and ab.address_book_id = :address_book_id');
hpdl
421
598       $Qaddress->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
599       $Qaddress->bindTable(':table_zones', TABLE_ZONES);
600       $Qaddress->bindTable(':table_countries', TABLE_COUNTRIES);
601       $Qaddress->bindInt(':customers_id', $osC_Customer->getID());
602       $Qaddress->bindInt(':address_book_id', $address_id);
603       $Qaddress->execute();
604
605       $this->_billing_address = array('id' => $address_id,
606                                       'firstname' => $Qaddress->valueProtected('entry_firstname'),
607                                       'lastname' => $Qaddress->valueProtected('entry_lastname'),
608                                       'company' => $Qaddress->valueProtected('entry_company'),
609                                       'street_address' => $Qaddress->valueProtected('entry_street_address'),
610                                       'suburb' => $Qaddress->valueProtected('entry_suburb'),
611                                       'city' => $Qaddress->valueProtected('entry_city'),
612                                       'postcode' => $Qaddress->valueProtected('entry_postcode'),
hpdl
809
613                                       'state' => (!osc_empty($Qaddress->valueProtected('entry_state'))) ? $Qaddress->valueProtected('entry_state') : $Qaddress->valueProtected('zone_name'),
hpdl
421
614                                       'zone_id' => $Qaddress->valueInt('entry_zone_id'),
hpdl
479
615                                       'zone_code' => $Qaddress->value('zone_code'),
hpdl
421
616                                       'country_id' => $Qaddress->valueInt('entry_country_id'),
hpdl
425
617                                       'country_title' => $Qaddress->value('countries_name'),
618                                       'country_iso_code_2' => $Qaddress->value('countries_iso_code_2'),
619                                       'country_iso_code_3' => $Qaddress->value('countries_iso_code_3'),
hpdl
732
620                                       'format' => $Qaddress->value('address_format'),
hpdl
479
621                                       'telephone_number' => $Qaddress->value('entry_telephone'));
hpdl
439
622
623       if ( is_array($previous_address) && ( ($previous_address['id'] != $this->_billing_address['id']) || ($previous_address['country_id'] != $this->_billing_address['country_id']) || ($previous_address['zone_id'] != $this->_billing_address['zone_id']) || ($previous_address['state'] != $this->_billing_address['state']) || ($previous_address['postcode'] != $this->_billing_address['postcode']) ) ) {
624         $this->_calculate();
625       }
hpdl
421
626     }
627
628     function getBillingAddress($key = '') {
629       if (empty($key)) {
630         return $this->_billing_address;
631       }
632
633       return $this->_billing_address[$key];
634     }
635
hpdl
424
636     function resetBillingAddress() {
hpdl
438
637       global $osC_Customer;
638
hpdl
439
639       $this->_billing_address = array('zone_id' => STORE_ZONE, 'country_id' => STORE_COUNTRY);
hpdl
438
640
641       if ($osC_Customer->isLoggedOn() && $osC_Customer->hasDefaultAddress()) {
642         $this->setBillingAddress($osC_Customer->getDefaultAddressID());
643       }
hpdl
424
644     }
645
hpdl
421
646     function setBillingMethod($billing_array) {
647       $this->_billing_method = $billing_array;
hpdl
439
648
649       $this->_calculate();
hpdl
421
650     }
651
652     function getBillingMethod($key = '') {
653       if (empty($key)) {
654         return $this->_billing_method;
655       }
656
657       return $this->_billing_method[$key];
658     }
659
660     function resetBillingMethod() {
661       $this->_billing_method = array();
hpdl
438
662
hpdl
439
663       $this->_calculate();
hpdl
421
664     }
665
666     function hasBillingMethod() {
667       return !empty($this->_billing_method);
668     }
669
hpdl
435
670     function getTaxingAddress($id = '') {
671       if ($this->getContentType() == 'virtual') {
672         return $this->getBillingAddress($id);
673       }
674
675       return $this->getShippingAddress($id);
676     }
677
hpdl
439
678     function addTaxAmount($amount) {
679       $this->_tax += $amount;
680     }
681
hpdl
830
682     function numberOfTaxGroups() {
683       return sizeof($this->_tax_groups);
684     }
685
hpdl
439
686     function addTaxGroup($group, $amount) {
687       if (isset($this->_tax_groups[$group])) {
688         $this->_tax_groups[$group] += $amount;
689       } else {
690         $this->_tax_groups[$group] = $amount;
691       }
692     }
693
694     function addToTotal($amount) {
695       $this->_total += $amount;
696     }
697
698     function getOrderTotals() {
699       return $this->_order_totals;
700     }
701
702     function getShippingBoxesWeight() {
703       return $this->_shipping_boxes_weight;
704     }
705
706     function numberOfShippingBoxes() {
707       return $this->_shipping_boxes;
708     }
709
hpdl
418
710     function _cleanUp() {
711       global $osC_Database, $osC_Customer;
712
hpdl
826
713       foreach ($this->_contents as $product_id_string => $data) {
hpdl
420
714         if ($data['quantity'] < 1) {
hpdl
826
715           unset($this->_contents[$product_id_string]);
hpdl
418
716
717 // remove from database
718           if ($osC_Customer->isLoggedOn()) {
719             $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id and products_id = :products_id');
720             $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
721             $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
hpdl
826
722             $Qdelete->bindValue(':products_id', $product_id_string);
hpdl
418
723             $Qdelete->execute();
724           }
725         }
726       }
727     }
728
hpdl
439
729     function _calculate($set_shipping = true) {
hpdl
733
730       global $osC_Currencies, $osC_Tax, $osC_Weight, $osC_Shipping, $osC_OrderTotal;
hpdl
435
731
732       $this->_sub_total = 0;
hpdl
420
733       $this->_total = 0;
734       $this->_weight = 0;
hpdl
435
735       $this->_tax = 0;
736       $this->_tax_groups = array();
hpdl
1420
737       $this->_shipping_boxes_weight = 0;
738       $this->_shipping_boxes = 0;
739       $this->_shipping_quotes = array();
740       $this->_order_totals = array();
hpdl
419
741
hpdl
1420
742       $_SESSION['cartID'] = $this->generateCartID();
743
hpdl
419
744       if ($this->hasContents()) {
hpdl
420
745         foreach ($this->_contents as $data) {
746           $products_weight = $osC_Weight->convert($data['weight'], $data['weight_class_id'], SHIPPING_WEIGHT_UNIT);
hpdl
435
747           $this->_weight += $products_weight * $data['quantity'];
hpdl
419
748
hpdl
435
749           $tax = $osC_Tax->getTaxRate($data['tax_class_id'], $this->getTaxingAddress('country_id'), $this->getTaxingAddress('zone_id'));
750           $tax_description = $osC_Tax->getTaxRateDescription($data['tax_class_id'], $this->getTaxingAddress('country_id'), $this->getTaxingAddress('zone_id'));
751
hpdl
748
752           $shown_price = $osC_Currencies->addTaxRateToPrice($data['final_price'], $tax, $data['quantity']);
hpdl
826
753
hpdl
435
754           $this->_sub_total += $shown_price;
755           $this->_total += $shown_price;
756
hpdl
500
757           if (DISPLAY_PRICE_WITH_TAX == '1') {
hpdl
435
758             $tax_amount = $shown_price - ($shown_price / (($tax < 10) ? '1.0' . str_replace('.', '', $tax) : '1.' . str_replace('.', '', $tax)));
759           } else {
760             $tax_amount = ($tax / 100) * $shown_price;
hpdl
826
761
762             $this->_total += $tax_amount;
hpdl
435
763           }
764
765           $this->_tax += $tax_amount;
766
767           if (isset($this->_tax_groups[$tax_description])) {
768             $this->_tax_groups[$tax_description] += $tax_amount;
769           } else {
770             $this->_tax_groups[$tax_description] = $tax_amount;
771           }
hpdl
419
772         }
hpdl
439
773
hpdl
1420
774         $this->_shipping_boxes_weight = $this->_weight;
775         $this->_shipping_boxes = 1;
hpdl
439
776
hpdl
1420
777         if (SHIPPING_BOX_WEIGHT >= ($this->_shipping_boxes_weight * SHIPPING_BOX_PADDING/100)) {
778           $this->_shipping_boxes_weight = $this->_shipping_boxes_weight + SHIPPING_BOX_WEIGHT;
779         } else {
780           $this->_shipping_boxes_weight = $this->_shipping_boxes_weight + ($this->_shipping_boxes_weight * SHIPPING_BOX_PADDING/100);
781         }
hpdl
439
782
hpdl
1420
783         if ($this->_shipping_boxes_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes
784           $this->_shipping_boxes = ceil($this->_shipping_boxes_weight / SHIPPING_MAX_WEIGHT);
785           $this->_shipping_boxes_weight = $this->_shipping_boxes_weight / $this->_shipping_boxes;
786         }
hpdl
439
787
hpdl
1420
788         if ($set_shipping === true) {
789           if (!class_exists('osC_Shipping')) {
790             include('includes/classes/shipping.php');
791           }
792
793           if (!$this->hasShippingMethod() || ($this->getShippingMethod('is_cheapest') === true)) {
794             $osC_Shipping = new osC_Shipping();
795             $this->setShippingMethod($osC_Shipping->getCheapestQuote(), false);