Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezbasket.php
Go to the documentation of this file.
1 <?php
19 class eZBasket extends eZPersistentObject
20 {
21 public $ProductCollectionID;
22 public $SessionID;
23 public $OrderID;
27 const ITEM_LIMIT = 3000;
28
32 static function definition()
33 {
34 static $definition = array( "fields" => array( "id" => array( 'name' => 'ID',
35 'datatype' => 'integer',
36 'default' => 0,
37 'required' => true ),
38 "session_id" => array( 'name' => "SessionID",
39 'datatype' => 'string',
40 'default' => '',
41 'required' => true ),
42 "productcollection_id" => array( 'name' => "ProductCollectionID",
43 'datatype' => 'integer',
44 'default' => '0',
45 'required' => true,
46 'foreign_class' => 'eZProductCollection',
47 'foreign_attribute' => 'id',
48 'multiplicity' => '1..*' ),
49 "order_id" => array( 'name' => "OrderID",
50 'datatype' => 'integer',
51 'default' => 0,
52 'required' => false,
53 'foreign_class' => 'eZOrder',
54 'foreign_attribute' => 'id',
55 'multiplicity' => '1..*') ),
56 'function_attributes' => array( 'items' => 'items',
57 'items_ordered' => 'itemsOrdered',
58 'total_ex_vat' => 'totalExVAT',
59 'total_inc_vat' => 'totalIncVAT',
60 'is_empty' => 'isEmpty',
61 'productcollection' => 'productCollection',
62 'items_info' => 'itemsInfo' ),
63 "keys" => array( "id" ),
64 "increment_key" => "id",
65 "class_name" => "eZBasket",
66 "name" => "ezbasket" );
67 return $definition;
68 }
69
76 function items( $asObject = true, $sorts = array( 'contentobject_id' => 'desc' ) )
77 {
78 $productItems = eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
79 null,
80 array( 'productcollection_id' => $this->ProductCollectionID ),
81 $sorts,
82 null,
83 $asObject );
84 $addedProducts = array();
85 foreach ( $productItems as $productItem )
86 {
87 $discountPercent = 0.0;
88 $isVATIncluded = true;
89 $id = $productItem->attribute( 'id' );
90 $contentObject = $productItem->attribute( 'contentobject' );
91
92 if ( $contentObject !== null )
93 {
94 $vatValue = $productItem->attribute( 'vat_value' );
95
96 // If VAT is unknown yet then we use zero VAT percentage for price calculation.
97 $realVatValue = $vatValue;
98 if ( $vatValue == -1 )
99 $vatValue = 0;
100
101 $count = $productItem->attribute( 'item_count' );
102 $discountPercent = $productItem->attribute( 'discount' );
103 $nodeID = $contentObject->attribute( 'main_node_id' );
104 $objectName = $contentObject->name( false, $contentObject->currentLanguage() );
105
106 $isVATIncluded = $productItem->attribute( 'is_vat_inc' );
107 $price = $productItem->attribute( 'price' );
108
109 if ( $isVATIncluded )
110 {
111 $priceExVAT = $price / ( 100 + $vatValue ) * 100;
112 $priceIncVAT = $price;
113 $totalPriceExVAT = $count * $priceExVAT * ( 100 - $discountPercent ) / 100;
114 $totalPriceIncVAT = $count * $priceIncVAT * ( 100 - $discountPercent ) / 100 ;
115 }
116 else
117 {
118 $priceExVAT = $price;
119 $priceIncVAT = $price * ( 100 + $vatValue ) / 100;
120 $totalPriceExVAT = $count * $priceExVAT * ( 100 - $discountPercent ) / 100;
121 $totalPriceIncVAT = $count * $priceIncVAT * ( 100 - $discountPercent ) / 100 ;
122 }
123
124 $addedProduct = array( "id" => $id,
125 "vat_value" => $realVatValue,
126 "item_count" => $count,
127 "node_id" => $nodeID,
128 "object_name" => $objectName,
129 "price_ex_vat" => $priceExVAT,
130 "price_inc_vat" => $priceIncVAT,
131 "discount_percent" => $discountPercent,
132 "total_price_ex_vat" => $totalPriceExVAT,
133 "total_price_inc_vat" => $totalPriceIncVAT,
134 'item_object' => $productItem );
135 $addedProducts[] = $addedProduct;
136 }
137 }
138 return $addedProducts;
139 }
140
147 function itemsOrdered( $asObject = true, $order = true )
148 {
149 return $this->items( $asObject, array( 'id' => ( $order ? 'asc' : 'desc' )) );
150 }
151
155 function itemsInfo()
156 {
157 $basketInfo = array();
158 // Build a price summary for the items based on VAT.
159 foreach ( $this->items() as $item )
160 {
161 $vatValue = $item['vat_value'];
162 $itemCount = abs( $item['item_count'] );
163 $priceExVAT = $item['price_ex_vat'];
164 $priceIncVAT = $item['price_inc_vat'];
165 $totalPriceExVAT = $item['total_price_ex_vat'];
166 $totalPriceIncVAT = $item['total_price_inc_vat'];
167
168 if ( !isset( $basketInfo['price_info'][$vatValue]['price_ex_vat'] ) )
169 {
170 $basketInfo['price_info'][$vatValue]['price_ex_vat'] = ( $priceExVAT * $itemCount );
171 $basketInfo['price_info'][$vatValue]['price_inc_vat'] = ( $priceIncVAT * $itemCount );
172 $basketInfo['price_info'][$vatValue]['price_vat'] = ( $priceIncVAT - $priceExVAT ) * $itemCount;
173
174 $basketInfo['price_info'][$vatValue]['total_price_ex_vat'] = $totalPriceExVAT;
175 $basketInfo['price_info'][$vatValue]['total_price_inc_vat'] = $totalPriceIncVAT;
176 $basketInfo['price_info'][$vatValue]['total_price_vat'] = $totalPriceIncVAT - $totalPriceExVAT;
177 }
178 else
179 {
180 $basketInfo['price_info'][$vatValue]['price_ex_vat'] += ( $priceExVAT * $itemCount );
181 $basketInfo['price_info'][$vatValue]['price_inc_vat'] += ( $priceIncVAT * $itemCount );
182 $basketInfo['price_info'][$vatValue]['price_vat'] += ( $priceIncVAT - $priceExVAT ) * $itemCount;
183
184 $basketInfo['price_info'][$vatValue]['total_price_ex_vat'] += $totalPriceExVAT;
185 $basketInfo['price_info'][$vatValue]['total_price_inc_vat'] += $totalPriceIncVAT;
186 $basketInfo['price_info'][$vatValue]['total_price_vat'] += ( $totalPriceIncVAT - $totalPriceExVAT );
187 }
188
189 if ( !isset( $basketInfo['total_price_info']['total_price_ex_vat'] ) )
190 {
191 $basketInfo['total_price_info']['price_ex_vat'] = ( $priceExVAT * $itemCount );
192 $basketInfo['total_price_info']['price_inc_vat'] = ( $priceIncVAT * $itemCount );
193 $basketInfo['total_price_info']['price_vat'] = ( ( $priceIncVAT - $priceExVAT ) * $itemCount );
194
195 $basketInfo['total_price_info']['total_price_ex_vat'] = $totalPriceExVAT;
196 $basketInfo['total_price_info']['total_price_inc_vat'] = $totalPriceIncVAT;
197 $basketInfo['total_price_info']['total_price_vat'] = ( $totalPriceIncVAT - $totalPriceExVAT );
198 }
199 else
200 {
201 $basketInfo['total_price_info']['price_ex_vat'] += ( $priceExVAT * $itemCount );
202 $basketInfo['total_price_info']['price_inc_vat'] += ( $priceIncVAT * $itemCount );
203 $basketInfo['total_price_info']['price_vat'] += ( ( $priceIncVAT - $priceExVAT ) * $itemCount );
204
205 $basketInfo['total_price_info']['total_price_ex_vat'] += $totalPriceExVAT;
206 $basketInfo['total_price_info']['total_price_inc_vat'] += $totalPriceIncVAT;
207 $basketInfo['total_price_info']['total_price_vat'] += ( $totalPriceIncVAT - $totalPriceExVAT );
208 }
209 }
210
211 // Add shipping cost to the total items price and add / update additional price information.
212 $productCollectionID = $this->attribute( 'productcollection_id' );
213
214 // Add additional calculated information to the basketInfo array, that can be used in the template.
215 $shippingUpdateStatus = eZShippingManager::updatePriceInfo( $productCollectionID, $basketInfo );
216
217 ksort( $basketInfo['price_info'] );
218 return $basketInfo;
219 }
220
226 function isVATKnown()
227 {
228 $result = true;
229 foreach ( $this->items() as $item )
230 {
231 if ( $item['vat_value'] == -1 )
232 return false;
233 }
234
235 return true;
236 }
237
238 function totalIncVAT()
239 {
240 $items = $this->items();
241
242 $total = 0.0;
243 foreach ( $items as $item )
244 {
245 $total += $item['total_price_inc_vat'];
246 }
247 return $total;
248 }
249
250 function totalExVAT()
251 {
252 $items = $this->items();
253
254 $total = 0.0;
255 foreach ( $items as $item )
256 {
257 $total += $item['total_price_ex_vat'];
258 }
259 return $total;
260 }
261
266 function removeItem( $itemID )
267 {
268 $item = eZProductCollectionItem::fetch( $itemID );
269 if ( is_object( $item ) && $this->attribute( 'productcollection_id' ) == $item->attribute( 'productcollection_id' ) )
270 {
271 $item->remove();
272 }
273 }
274
275 function isEmpty()
276 {
277 $items = $this->items();
278 return count( $items ) < 1;
279 }
280
286 static function fetch( $sessionKey )
287 {
288 return eZPersistentObject::fetchObject( eZBasket::definition(),
289 null, array( "session_id" => $sessionKey ),
290 true );
291 }
292
299 static function currentBasket( $asObject=true, $byOrderID=-1 )
300 {
301 $basketList = array();
302
303 if( $byOrderID != -1 )
304 {
305 $basketList = eZPersistentObject::fetchObjectList( eZBasket::definition(),
306 null, array( "order_id" => $byOrderID ),
307 null, null,
308 $asObject );
309 }
310 else
311 {
312 $http = eZHTTPTool::instance();
313 $sessionID = $http->sessionID();
314
315 $basketList = eZPersistentObject::fetchObjectList( eZBasket::definition(),
316 null, array( "session_id" => $sessionID ),
317 null, null,
318 $asObject );
319 }
320
321 $currentBasket = false;
322 if ( count( $basketList ) == 0 )
323 {
324 $db = eZDB::instance();
325 $db->begin();
326 $collection = eZProductCollection::create();
327 $collection->store();
328
329 $currentBasket = new eZBasket( array( "session_id" => $sessionID,
330 "productcollection_id" => $collection->attribute( "id" ) ) );
331 $currentBasket->store();
332 $db->commit();
333 }
334 else
335 {
336 $currentBasket = $basketList[0];
337 }
338 return $currentBasket;
339 }
340
347 function createOrder()
348 {
349 // Make order
350 $productCollectionID = $this->attribute( 'productcollection_id' );
351
352 $user = eZUser::currentUser();
353 $userID = $user->attribute( 'contentobject_id' );
354
355 $time = time();
356 $order = new eZOrder( array( 'productcollection_id' => $productCollectionID,
357 'user_id' => $userID,
358 'is_temporary' => 1,
359 'created' => $time,
360 'status_id' => eZOrderStatus::PENDING,
361 'status_modified' => $time,
362 'status_modifier_id' => $userID
363 ) );
364
365 $db = eZDB::instance();
366 $db->begin();
367 $order->store();
368
369 $orderID = $order->attribute( 'id' );
370 $this->setAttribute( 'order_id', $orderID );
371 $this->store();
372 $db->commit();
373
374 return $order;
375 }
376
381 function updatePrices()
382 {
383 $productCollection = $this->attribute( 'productcollection' );
384 if ( $productCollection )
385 {
386 $currencyCode = '';
387 $items = $this->items();
388
389 $db = eZDB::instance();
390 $db->begin();
391 foreach( $items as $itemArray )
392 {
393 $item = $itemArray['item_object'];
394 $productContentObject = $item->attribute( 'contentobject' );
395 $priceObj = null;
396 $price = 0.0;
397 $attributes = $productContentObject->contentObjectAttributes();
398 foreach ( $attributes as $attribute )
399 {
400 $dataType = $attribute->dataType();
401 if ( eZShopFunctions::isProductDatatype( $dataType->isA() ) )
402 {
403 $priceObj = $attribute->content();
404 $price += $priceObj->attribute( 'price' );
405 break;
406 }
407 }
408 if ( !is_object( $priceObj ) )
409 break;
410
411 $currency = $priceObj->attribute( 'currency' );
412 $optionsPrice = $item->calculatePriceWithOptions( $currency );
413
414 $price += $optionsPrice;
415 $item->setAttribute( "price", $price );
416 if ( $priceObj->attribute( 'is_vat_included' ) )
417 {
418 $item->setAttribute( "is_vat_inc", '1' );
419 }
420 else
421 {
422 $item->setAttribute( "is_vat_inc", '0' );
423 }
424 $item->setAttribute( "vat_value", $priceObj->VATPercent( $productContentObject ) );
425 $item->setAttribute( "discount", $priceObj->attribute( 'discount_percent' ) );
426 $item->store();
427
428 $currencyCode = $priceObj->attribute( 'currency' );
429 }
430
431 $productCollection->setAttribute( 'currency_code', $currencyCode );
432 $productCollection->store();
433 $db->commit();
434
435 // update prices that are related to shipping values.
436 eZShippingManager::updateShippingInfo( $productCollection->attribute( 'id' ) );
437 }
438 }
439
445 static function cleanupExpired( $time )
446 {
447 $db = eZDB::instance();
448
449 if ( $db->hasRequiredServerVersion( '4.0', 'mysql' ) )
450 {
451 // If we have the required MySQL version we use a DELETE statement
452 // with multiple tables
453 $sql = "DELETE FROM ezbasket, ezproductcollection, ezproductcollection_item, ezproductcollection_item_opt
454 USING ezsession
455 JOIN ezbasket
456 ON ezsession.session_key = ezbasket.session_id
457 LEFT JOIN ezproductcollection
458 ON ezbasket.productcollection_id = ezproductcollection.id
459 LEFT JOIN ezproductcollection_item
460 ON ezproductcollection.id = ezproductcollection_item.productcollection_id
461 LEFT JOIN ezproductcollection_item_opt
462 ON ezproductcollection_item.id = ezproductcollection_item_opt.item_id";
463 if ( $time !== false )
464 $sql .= "\nWHERE ezsession.expiration_time < " . (int)$time;
465 $db->query( $sql );
466 return;
467 }
468
469 // Find all baskets whos session will expire
470 $sql = "SELECT id, productcollection_id
471 FROM ezbasket, ezsession
472 WHERE ezbasket.session_id = ezsession.session_key AND
473 ezsession.expiration_time < " . (int)$time;
474 $limit = self::ITEM_LIMIT;
475
476 do
477 {
478 $rows = $db->arrayQuery( $sql, array( 'offset' => 0, 'limit' => $limit ) );
479 if ( count( $rows ) == 0 )
480 break;
481
482 $productCollectionIDList = array();
483 $idList = array();
484 foreach ( $rows as $row )
485 {
486 $idList[] = (int)$row['id'];
487 $productCollectionIDList[] = (int)$row['productcollection_id'];
488 }
489 eZProductCollection::cleanupList( $productCollectionIDList );
490
491 $ids = implode( ', ', $idList );
492 $db->query( "DELETE FROM ezbasket WHERE id IN ( $ids )" );
493
494 } while ( true );
495 }
496
503 static function cleanup()
504 {
505 $db = eZDB::instance();
506 $sql = "SELECT productcollection_id FROM ezbasket";
507 $limit = self::ITEM_LIMIT;
508
509 $db->begin();
510 do
511 {
512 $rows = $db->arrayQuery( $sql, array( 'offset' => 0, 'limit' => $limit ) );
513
514 if ( count( $rows ) == 0 )
515 break;
516
517 $productCollectionIDList = array();
518 foreach ( $rows as $row )
519 {
520 $productCollectionIDList[] = (int)$row['productcollection_id'];
521 }
522 eZProductCollection::cleanupList( $productCollectionIDList );
523
524 $ids = implode( ', ', $productCollectionIDList );
525 $db->query( "DELETE FROM ezbasket WHERE productcollection_id IN ( $ids )" );
526 }
527 while ( true );
528 $db->commit();
529 }
530
534 function type()
535 {
536 $type = false;
537
538 // get first product
539 $productCollectionItemList = eZProductCollectionItem::fetchList( array( 'productcollection_id' => $this->attribute( 'productcollection_id' ) ),
540 true,
541 0,
542 1 );
543
544 if ( is_array( $productCollectionItemList ) && count( $productCollectionItemList ) === 1 )
545 {
546 $product = $productCollectionItemList[0]->attribute( 'contentobject' );
547 if ( is_object( $product ) )
548 {
549 $type = eZShopFunctions::productTypeByObject( $product );
550 }
551 }
552
553 return $type;
554 }
555
556 function canAddProduct( $contentObject )
557 {
558 $error = eZError::SHOP_OK;
559
560 $productType = eZShopFunctions::productTypeByObject( $contentObject );
561 if ( $productType === false )
562 {
563 $error = eZError::SHOP_NOT_A_PRODUCT;
564 }
565 else
566 {
567 if ( !eZShopFunctions::isSimplePriceProductType( $productType ) )
568 $error = eZShopFunctions::isPreferredCurrencyValid();
569
570 if ( $error === eZError::SHOP_OK )
571 {
572 $basketType = $this->type();
573 if ( $basketType !== false && $basketType !== $productType )
574 $error = eZError::SHOP_BASKET_INCOMPATIBLE_PRODUCT_TYPE;
575 }
576 }
577
578 return $error;
579 }
580
581 function productCollection()
582 {
583 return eZProductCollection::fetch( $this->attribute( 'productcollection_id' ) );
584 }
585
595 static function cleanupCurrentBasket( $useSetting = true )
596 {
597 $ini = eZINI::instance();
598 $removeBasket = true;
599 if ( $useSetting )
600 $removeBasket = $ini->hasVariable( 'ShopSettings', 'ClearBasketOnLogout' ) ? $ini->variable( 'ShopSettings', 'ClearBasketOnLogout' ) == 'enabled' : false;
601
602 if ( $removeBasket )
603 {
604 $basket = eZBasket::currentBasket();
605 if ( !is_object( $basket ) )
606 return false;
607 $db = eZDB::instance();
608 $db->begin();
609 $productCollectionID = $basket->attribute( 'productcollection_id' );
610 eZProductCollection::cleanupList( array( $productCollectionID ) );
611 $basket->remove();
612 $db->commit();
613 }
614
615 return true;
616 }
617 }
618
619 ?>
eZBasket
eZBasket handles shopping baskets
Definition: ezbasket.php:20
eZBasket\itemsInfo
itemsInfo()
Definition: ezbasket.php:155
eZBasket\itemsOrdered
itemsOrdered( $asObject=true, $order=true)
Definition: ezbasket.php:147
eZBasket\isEmpty
isEmpty()
Definition: ezbasket.php:275
eZBasket\$SessionID
$SessionID
Definition: ezbasket.php:22
eZBasket\items
items( $asObject=true, $sorts=array( 'contentobject_id'=> 'desc'))
Definition: ezbasket.php:76
eZBasket\$OrderID
$OrderID
Definition: ezbasket.php:23
eZBasket\type
type()
Definition: ezbasket.php:534
eZBasket\canAddProduct
canAddProduct( $contentObject)
Definition: ezbasket.php:556
eZBasket\totalIncVAT
totalIncVAT()
Definition: ezbasket.php:238
eZBasket\ITEM_LIMIT
const ITEM_LIMIT
Definition: ezbasket.php:27
eZBasket\isVATKnown
isVATKnown()
Definition: ezbasket.php:226
eZBasket\fetch
static fetch( $sessionKey)
Definition: ezbasket.php:286
eZBasket\definition
static definition()
Definition: ezbasket.php:32
eZBasket\currentBasket
static currentBasket( $asObject=true, $byOrderID=-1)
Definition: ezbasket.php:299
eZBasket\removeItem
removeItem( $itemID)
Definition: ezbasket.php:266
eZBasket\updatePrices
updatePrices()
Definition: ezbasket.php:381
eZBasket\cleanupCurrentBasket
static cleanupCurrentBasket( $useSetting=true)
Definition: ezbasket.php:595
eZBasket\totalExVAT
totalExVAT()
Definition: ezbasket.php:250
eZBasket\cleanup
static cleanup()
Definition: ezbasket.php:503
eZBasket\createOrder
createOrder()
Definition: ezbasket.php:347
eZBasket\cleanupExpired
static cleanupExpired( $time)
Definition: ezbasket.php:445
eZBasket\productCollection
productCollection()
Definition: ezbasket.php:581
eZBasket\$ProductCollectionID
$ProductCollectionID
Definition: ezbasket.php:21
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZError\SHOP_BASKET_INCOMPATIBLE_PRODUCT_TYPE
const SHOP_BASKET_INCOMPATIBLE_PRODUCT_TYPE
Definition: ezerror.php:61
eZError\SHOP_OK
const SHOP_OK
Definition: ezerror.php:59
eZError\SHOP_NOT_A_PRODUCT
const SHOP_NOT_A_PRODUCT
Definition: ezerror.php:60
eZHTTPTool\instance
static instance()
Definition: ezhttptool.php:189
eZOrderStatus\PENDING
const PENDING
Definition: ezorderstatus.php:53
eZOrder
eZOrder handles orders
Definition: ezorder.php:20
eZPersistentObject
Definition: ezpersistentobject.php:33
eZPersistentObject\fetchObject
static fetchObject( $def, $field_filters, $conds, $asObject=true, $grouping=null, $custom_fields=null)
Definition: ezpersistentobject.php:213
eZPersistentObject\store
store( $fieldFilters=null)
Definition: ezpersistentobject.php:304
eZPersistentObject\setAttribute
setAttribute( $attr, $val)
Definition: ezpersistentobject.php:1374
eZPersistentObject\count
static count( $def, $conds=null, $field=null)
Definition: ezpersistentobject.php:187
eZPersistentObject\attribute
attribute( $attr, $noFunction=false)
Definition: ezpersistentobject.php:1329
eZPersistentObject\fetchObjectList
static fetchObjectList( $def, $field_filters=null, $conds=null, $sorts=null, $limit=null, $asObject=true, $grouping=false, $custom_fields=null, $custom_tables=null, $custom_conds=null)
Definition: ezpersistentobject.php:806
eZProductCollectionItem\fetch
static fetch( $id, $asObject=true)
Definition: ezproductcollectionitem.php:116
eZProductCollectionItem\fetchList
static fetchList( $conditions=null, $asObjects=true, $offset=false, $limit=false)
Definition: ezproductcollectionitem.php:124
eZProductCollectionItem\definition
static definition()
Definition: ezproductcollectionitem.php:21
eZProductCollection\cleanupList
static cleanupList( $productCollectionIDList)
Definition: ezproductcollection.php:153
eZProductCollection\create
static create()
Definition: ezproductcollection.php:42
eZProductCollection\fetch
static fetch( $productCollectionID, $asObject=true)
Definition: ezproductcollection.php:90
eZShippingManager\updatePriceInfo
static updatePriceInfo( $productCollectionID, &$basketInfo)
Definition: ezshippingmanager.php:417
eZShippingManager\updateShippingInfo
static updateShippingInfo( $productCollectionID)
Definition: ezshippingmanager.php:84
eZUser\currentUser
static currentUser()
Definition: ezuser.php:1716
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.