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 include_once( "kernel/classes/ezpersistentobject.php" );
00039 include_once( "kernel/classes/ezcontentobject.php" );
00040 include_once( "kernel/classes/ezproductcollectionitemoption.php" );
00041
00042
00043 class eZProductCollectionItem extends eZPersistentObject
00044 {
00045 function eZProductCollectionItem( $row )
00046 {
00047 $this->eZPersistentObject( $row );
00048 }
00049
00050 function definition()
00051 {
00052 return array( "fields" => array( "id" => array( 'name' => 'ID',
00053 'datatype' => 'integer',
00054 'default' => 0,
00055 'required' => true ),
00056 "productcollection_id" => array( 'name' => "ProductCollectionID",
00057 'datatype' => 'integer',
00058 'default' => 0,
00059 'required' => true,
00060 'foreign_class' => 'eZProductCollection',
00061 'foreign_attribute' => 'id',
00062 'multiplicity' => '1..*' ),
00063 "contentobject_id" => array( 'name' => "ContentObjectID",
00064 'datatype' => 'integer',
00065 'default' => 0,
00066 'required' => true,
00067 'foreign_class' => 'eZContentObject',
00068 'foreign_attribute' => 'id',
00069 'multiplicity' => '1..*' ),
00070 "item_count" => array( 'name' => "ItemCount",
00071 'datatype' => 'integer',
00072 'default' => 0,
00073 'required' => true ),
00074 "price" => array( 'name' => "Price",
00075 'datatype' => 'float',
00076 'default' => 0,
00077 'required' => true ),
00078 'is_vat_inc' => array( 'name' => "IsVATIncluded",
00079 'datatype' => 'integer',
00080 'default' => 0,
00081 'required' => true ),
00082 'vat_value' => array( 'name' => "VATValue",
00083 'datatype' => 'float',
00084 'default' => 0,
00085 'required' => true ),
00086 'discount' => array( 'name' => "DiscountValue",
00087 'datatype' => 'integer',
00088 'default' => 0,
00089 'required' => true ),
00090 'name' => array( 'name' => 'Name',
00091 'datatype' => 'string',
00092 'default' => '',
00093 'required' => true ) ),
00094 'function_attributes' => array( 'contentobject' => 'contentObject',
00095 'option_list' => 'optionList' ),
00096 "keys" => array( "id" ),
00097 "increment_key" => "id",
00098 "relations" => array( "contentobject_id" => array( "class" => "ezcontentobject",
00099 "field" => "id" ),
00100 "productcollection_id" => array( "class" => "ezproductcollection",
00101 "field" => "id" ) ),
00102 "class_name" => "eZProductCollectionItem",
00103 "name" => "ezproductcollection_item" );
00104 }
00105
00106
00107
00108
00109
00110 function create( $productCollectionID )
00111 {
00112 $row = array( "productcollection_id" => $productCollectionID );
00113 return new eZProductCollectionItem( $row );
00114 }
00115
00116
00117
00118
00119 function clone()
00120 {
00121 $item = $this;
00122 $item->setAttribute( 'id', null );
00123 return $item;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 function ©( $collectionID )
00135 {
00136 $item = $this->clone();
00137 $item->setAttribute( 'productcollection_id', $collectionID );
00138 $item->store();
00139 $oldItemOptionList =& $this->optionList();
00140 foreach ( array_keys( $oldItemOptionList ) as $oldItemOptionKey )
00141 {
00142 $oldItemOption =& $oldItemOptionList[$oldItemOptionKey];
00143 $itemOption =& $oldItemOption->copy( $item->attribute( 'id' ) );
00144 }
00145 return $item;
00146 }
00147
00148 function fetch( $id, $asObject = true )
00149 {
00150 return eZPersistentObject::fetchObject( eZProductCollectionItem::definition(),
00151 null,
00152 array( "id" => $id ),
00153 $asObject );
00154 }
00155
00156 function fetchList( $conditions = null, $asObjects = true, $offset = false, $limit = false )
00157 {
00158 $limitation = null;
00159 if ( $offset !== false or $limit !== false )
00160 $limitation = array( 'offset' => $offset, 'length' => $limit );
00161
00162 return eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00163 null,
00164 $conditions,
00165 null,
00166 $limitation,
00167 $asObjects );
00168 }
00169
00170
00171
00172
00173 function discountPercent()
00174 {
00175 $discount = false;
00176 return $discount;
00177 }
00178
00179
00180
00181
00182 function &contentObject()
00183 {
00184 if ( $this->ContentObject === null )
00185 {
00186 if ( $this->ContentObjectID == 0 )
00187 {
00188 $retValue = null;
00189 return $retValue;
00190 }
00191 $this->ContentObject =& eZContentObject::fetch( $this->ContentObjectID );
00192 }
00193 return $this->ContentObject;
00194 }
00195
00196 function &optionList()
00197 {
00198 $optionList = eZProductCollectionItemOption::fetchList( $this->attribute( 'id' ) );
00199 return $optionList;
00200 }
00201
00202
00203
00204
00205
00206 function remove()
00207 {
00208 $itemOptionList = eZProductCollectionItemOption::fetchList( $this->attribute( 'id' ) );
00209
00210 $db =& eZDB::instance();
00211 $db->begin();
00212 foreach( array_keys( $itemOptionList ) as $key )
00213 {
00214 $itemOption =& $itemOptionList[$key];
00215 $itemOption->remove();
00216 }
00217 eZPersistentObject::remove();
00218 $db->commit();
00219 }
00220
00221
00222
00223
00224
00225
00226 function calculatePriceWithOptions( $currency = false )
00227 {
00228 include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00229
00230 $optionList = eZProductCollectionItemOption::fetchList( $this->attribute( 'id' ) );
00231 $contentObject =& $this->contentObject();
00232 $contentObjectVersion =& $contentObject->attribute( 'current_version' );
00233 $optionsPrice = 0.0;
00234 if ( count( $optionList ) > 0 )
00235 {
00236 $db =& eZDB::instance();
00237 $db->begin();
00238 foreach( $optionList as $option )
00239 {
00240 $objectAttribute = eZContentObjectAttribute::fetch( $option->attribute( 'object_attribute_id' ), $contentObjectVersion );
00241 if ( $objectAttribute == null )
00242 {
00243 $optionsPrice += 0.0;
00244 continue;
00245 }
00246
00247 $dataType = $objectAttribute->dataType();
00248 $optionData = $dataType->productOptionInformation( $objectAttribute, $option->attribute( 'option_item_id' ), $this, $option );
00249
00250 if ( $optionData )
00251 {
00252 $optionData['additional_price'] = eZShopFunctions::convertAdditionalPrice( $currency, $optionData['additional_price'] );
00253 $option->setAttribute( 'price', $optionData['additional_price'] );
00254 $option->store();
00255
00256 $optionsPrice += $optionData['additional_price'];
00257 }
00258
00259 }
00260 $db->commit();
00261 }
00262 return $optionsPrice;
00263 }
00264
00265 function verify( $currency = false )
00266 {
00267 $contentObject =& $this->attribute( 'contentobject' );
00268 if ( $contentObject != null && $contentObject->attribute( 'main_node_id' ) > 0 )
00269 {
00270 include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00271
00272 $attributes = $contentObject->contentObjectAttributes();
00273 $optionsPrice = $this->calculatePriceWithOptions( $currency );
00274
00275
00276
00277
00278
00279
00280 foreach ( $attributes as $attribute )
00281 {
00282 $dataType = $attribute->dataType();
00283 if ( eZShopFunctions::isProductDatatype( $dataType->isA() ) )
00284 {
00285 $priceObj =& $attribute->content();
00286
00287 $price = $priceObj->attribute( 'price' );
00288 $priceWithOptions = $price + $optionsPrice;
00289 if ( $priceWithOptions != $this->attribute( 'price' ) )
00290 {
00291 return false;
00292 }
00293 if ( $priceObj->attribute( 'is_vat_included' ) != ( $this->attribute( 'is_vat_inc' ) > 0 ) )
00294 {
00295 return false;
00296 }
00297 if ( $priceObj->attribute( 'vat_percent' ) != $this->attribute( 'vat_value' ) )
00298 {
00299 return false;
00300 }
00301 if ( $priceObj->discount() != $this->attribute( 'discount' ) )
00302 {
00303 return false;
00304 }
00305 return true;
00306 }
00307 }
00308
00309 }
00310 return false;
00311 }
00312
00313
00314
00315
00316
00317
00318
00319 function cleanupList( $productCollectionIDList )
00320 {
00321 $db =& eZDB::instance();
00322 $db->begin();
00323 $idText = $db->implodeWithTypeCast( ', ', $productCollectionIDList, 'int' );
00324 $rows = $db->arrayQuery( "SELECT id FROM ezproductcollection_item WHERE productcollection_id IN ( $idText )" );
00325 if ( count( $rows ) > 0 )
00326 {
00327 $itemIDList = array();
00328 foreach ( $rows as $row )
00329 {
00330 $itemIDList[] = $row['id'];
00331 }
00332 include_once( 'kernel/classes/ezproductcollectionitemoption.php' );
00333 eZProductCollectionItemOption::cleanupList( $itemIDList );
00334 }
00335 $db->query( "DELETE FROM ezproductcollection_item WHERE productcollection_id IN ( $idText )" );
00336 $db->commit();
00337
00338 }
00339
00340
00341 var $ContentObject = null;
00342 }
00343
00344 ?>