00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 include_once( "kernel/classes/ezpersistentobject.php" );
00041 include_once( "kernel/classes/ezproductcollection.php" );
00042 include_once( "kernel/classes/ezproductcollectionitem.php" );
00043 include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00044 include_once( "kernel/classes/ezuserdiscountrule.php" );
00045 include_once( "kernel/classes/ezcontentobjecttreenode.php" );
00046
00047 class eZWishList extends eZPersistentObject
00048 {
00049
00050
00051 function eZWishList( $row )
00052 {
00053 $this->eZPersistentObject( $row );
00054 }
00055
00056
00057
00058
00059 function definition()
00060 {
00061 return array( "fields" => array( "id" => array( 'name' => 'ID',
00062 'datatype' => 'integer',
00063 'default' => 0,
00064 'required' => true ),
00065 "user_id" => array( 'name' => "UserID",
00066 'datatype' => 'integer',
00067 'default' => 0,
00068 'required' => true,
00069 'foreign_class' => 'eZUser',
00070 'foreign_attribute' => 'contentobject_id',
00071 'multiplicity' => '1..*' ),
00072 "productcollection_id" => array( 'name' => "ProductCollectionID",
00073 'datatype' => 'integer',
00074 'default' => 0,
00075 'required' => true,
00076 'foreign_class' => 'eZProductCollection',
00077 'foreign_attribute' => 'id',
00078 'multiplicity' => '1..*' ) ),
00079 "keys" => array( "id" ),
00080 'function_attributes' => array( 'items' => 'items' ),
00081 "increment_key" => "id",
00082 "class_name" => "eZWishList",
00083 "name" => "ezwishlist" );
00084 }
00085
00086
00087 function discountPercent()
00088 {
00089 $discountPercent = 0;
00090 $user =& eZUser::currentUser();
00091 $userID = $user->attribute( 'contentobject_id' );
00092 $nodes = eZContentObjectTreeNode::fetchByContentObjectID( $userID );
00093 $idArray = array();
00094 $idArray[] = $userID;
00095 foreach ( $nodes as $node )
00096 {
00097 $parentNodeID = $node->attribute( 'parent_node_id' );
00098 $idArray[] = $parentNodeID;
00099 }
00100 $rules = eZUserDiscountRule::fetchByUserIDArray( $idArray );
00101 foreach ( $rules as $rule )
00102 {
00103 $percent = $rule->attribute( 'discount_percent' );
00104 if ( $discountPercent < $percent )
00105 $discountPercent = $percent;
00106 }
00107 return $discountPercent;
00108 }
00109
00110 function itemCount( $alternativeProductionID = false )
00111 {
00112 $countRes = eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00113 array(),
00114 array( "productcollection_id" => ( $alternativeProductionID === false ) ? $this->ProductCollectionID : $alternativeProductionID ),
00115 false,
00116 null,
00117 false,
00118 false,
00119 array( array( 'operation' => 'count( id )',
00120 'name' => 'count' ) ) );
00121 return $countRes[0]['count'];
00122 }
00123
00124 function &items( $asObject = true, $alternativeProductionID = false, $offset = false, $limit = false )
00125 {
00126 $productItems = eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00127 null,
00128 array( 'productcollection_id' => ( $alternativeProductionID === false ) ? $this->ProductCollectionID : $alternativeProductionID ),
00129 null,
00130 array( 'offset' => $offset,
00131 'length' => $limit ),
00132 $asObject );
00133
00134 $addedProducts = array();
00135 foreach ( $productItems as $productItem )
00136 {
00137 $discountPercent = 0.0;
00138 $isVATIncluded = true;
00139 $id = $productItem->attribute( 'id' );
00140 $contentObject = $productItem->attribute( 'contentobject' );
00141
00142 if ( $contentObject !== null )
00143 {
00144 $vatValue = $productItem->attribute( 'vat_value' );
00145 $count = $productItem->attribute( 'item_count' );
00146 $discountPercent = $productItem->attribute( 'discount' );
00147 $nodeID = $contentObject->attribute( 'main_node_id' );
00148 $objectName = $contentObject->attribute( 'name' );
00149
00150 $isVATIncluded = $productItem->attribute( 'is_vat_inc' );
00151 $price = $productItem->attribute( 'price' );
00152
00153 if ( $isVATIncluded )
00154 {
00155 $priceExVAT = $price / ( 100 + $vatValue ) * 100;
00156 $priceIncVAT = $price;
00157 }
00158 else
00159 {
00160 $priceExVAT = $price;
00161 $priceIncVAT = $price * ( 100 + $vatValue ) / 100;
00162 }
00163
00164 $totalPriceExVAT = $count * $priceExVAT * ( 100 - $discountPercent ) / 100;
00165 $totalPriceIncVAT = $count * $priceIncVAT * ( 100 - $discountPercent ) / 100 ;
00166
00167 $addedProduct = array( "id" => $id,
00168 "vat_value" => $vatValue,
00169 "item_count" => $count,
00170 "node_id" => $nodeID,
00171 "object_name" => $objectName,
00172 "price_ex_vat" => $priceExVAT,
00173 "price_inc_vat" => $priceIncVAT,
00174 "discount_percent" => $discountPercent,
00175 "total_price_ex_vat" => $totalPriceExVAT,
00176 "total_price_inc_vat" => $totalPriceIncVAT,
00177 'item_object' =>$productItem );
00178 $addedProducts[] = $addedProduct;
00179 }
00180 }
00181 return $addedProducts;
00182 }
00183
00184
00185
00186
00187
00188 function removeItem( $itemID )
00189 {
00190 $item = eZProductCollectionItem::fetch( $itemID );
00191 $item->remove();
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 function ¤tWishList( $asObject=true )
00201 {
00202 $http =& eZHTTPTool::instance();
00203
00204 $user =& eZUser::currentUser();
00205 $userID = $user->attribute( 'contentobject_id' );
00206 $WishListArray = eZPersistentObject::fetchObjectList( eZWishList::definition(),
00207 null, array( "user_id" => $userID
00208 ),
00209 null, null,
00210 $asObject );
00211
00212 $currentWishList = false;
00213 if ( count( $WishListArray ) == 0 )
00214 {
00215 $collection = eZProductCollection::create();
00216 $collection->store();
00217
00218 $currentWishList = new eZWishList( array( "user_id" => $userID,
00219 "productcollection_id" => $collection->attribute( "id" ) ) );
00220 $currentWishList->store();
00221 }
00222 else
00223 {
00224 $currentWishList =& $WishListArray[0];
00225 }
00226 return $currentWishList;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235 function cleanup()
00236 {
00237 $db =& eZDB::instance();
00238 $db->begin();
00239 $rows = $db->arrayQuery( "SELECT productcollection_id FROM ezwishlist" );
00240 if ( count( $rows ) > 0 )
00241 {
00242 $productCollectionIDList = array();
00243 foreach ( $rows as $row )
00244 {
00245 $productCollectionIDList[] = $row['productcollection_id'];
00246 }
00247 eZProductCollection::cleanupList( $productCollectionIDList );
00248 }
00249 $db->query( "DELETE FROM ezwishlist" );
00250 $db->commit();
00251 }
00252
00253
00254
00255
00256
00257 function removeByUserID( $userID )
00258 {
00259 eZPersistentObject::removeObject( eZWishList::definition(),
00260 array( 'user_id' => $userID ) );
00261 }
00262 }
00263
00264 ?>