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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 include_once( "kernel/shop/classes/ezsimpleprice.php" );
00055 include_once( 'kernel/shop/classes/ezmultipricedata.php' );
00056
00057 define( 'EZ_MULTIPRICE_CALCULATION_TYPE_VAT_INCLUDE', 1 );
00058 define( 'EZ_MULTIPRICE_CALCULATION_TYPE_VAT_EXCLUDE', 2 );
00059 define( 'EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_INCLUDE', 3 );
00060 define( 'EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_EXCLUDE', 4 );
00061
00062 class eZMultiPrice extends eZSimplePrice
00063 {
00064
00065
00066
00067 function eZMultiPrice( &$classAttribute, &$contentObjectAttribute, $storedPrice = null )
00068 {
00069 eZSimplePrice::eZSimplePrice( $classAttribute, $contentObjectAttribute, $storedPrice );
00070
00071 $isVatIncluded = ( $classAttribute->attribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD ) == 1 );
00072 $VATID =& $classAttribute->attribute( EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD );
00073
00074 $this->setVatIncluded( $isVatIncluded );
00075 $this->setVatType( $VATID );
00076
00077 $this->IsDataDirty = false;
00078 $this->ContentObjectAttribute =& $contentObjectAttribute;
00079 }
00080
00081
00082
00083
00084 function attributes()
00085 {
00086 return array( 'currency_list',
00087 'auto_currency_list',
00088 'price_list',
00089 'auto_price_list',
00090 'custom_price_list',
00091 'inc_vat_price_list',
00092 'ex_vat_price_list',
00093 'discount_inc_vat_price_list',
00094 'discount_ex_vat_price_list' );
00095 }
00096
00097
00098
00099
00100 function hasAttribute( $attr )
00101 {
00102 $hasAttribute = in_array( $attr, eZMultiPrice::attributes() );
00103 if ( !$hasAttribute )
00104 $hasAttribute = eZSimplePrice::attributes( $attr );
00105
00106 return $hasAttribute;
00107 }
00108
00109
00110
00111
00112 function setAttribute( $attr, $value )
00113 {
00114 switch ( $attr )
00115 {
00116 case 'currency_list':
00117 {
00118 } break;
00119
00120 case 'auto_currency_list':
00121 {
00122 } break;
00123
00124 case 'price_list':
00125 {
00126 } break;
00127
00128 case 'auto_price_list':
00129 {
00130 } break;
00131
00132 case 'custom_price_list':
00133 {
00134 } break;
00135
00136 default:
00137 {
00138 eZSimplePrice::setAttribute( $attr, $value );
00139 } break;
00140 }
00141 }
00142
00143
00144
00145
00146 function &attribute( $attr )
00147 {
00148 switch ( $attr )
00149 {
00150 case 'currency_list':
00151 {
00152 return $this->currencyList();
00153 } break;
00154
00155 case 'auto_currency_list':
00156 {
00157 return $this->autoCurrencyList();
00158 } break;
00159
00160 case 'price_list':
00161 {
00162 return $this->priceList();
00163 } break;
00164
00165 case 'inc_vat_price_list':
00166 {
00167 return $this->incVATPriceList();
00168 } break;
00169
00170 case 'ex_vat_price_list':
00171 {
00172 return $this->exVATPriceList();
00173 } break;
00174
00175 case 'discount_inc_vat_price_list':
00176 {
00177 return $this->discountIncVATPriceList();
00178 } break;
00179
00180 case 'discount_ex_vat_price_list':
00181 {
00182 return $this->discountExVATPriceList();
00183 } break;
00184
00185 case 'auto_price_list':
00186 {
00187 return $this->autoPriceList();
00188 } break;
00189
00190 case 'custom_price_list':
00191 {
00192 return $this->customPriceList();
00193 } break;
00194
00195 default :
00196 {
00197 return eZSimplePrice::attribute( $attr );
00198 } break;
00199 }
00200 }
00201
00202
00203
00204
00205
00206 function &preferredCurrencyCode()
00207 {
00208 include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00209 $currency = eZShopFunctions::preferredCurrencyCode();
00210 return $currency;
00211 }
00212
00213 function ¤cyList()
00214 {
00215 if ( !isset( $this->CurrencyList ) )
00216 {
00217 include_once( 'kernel/shop/classes/ezcurrencydata.php' );
00218 $this->CurrencyList = eZCurrencyData::fetchList();
00219 }
00220
00221 return $this->CurrencyList;
00222 }
00223
00224
00225
00226
00227 function &autoCurrencyList()
00228 {
00229
00230
00231
00232 $autoCurrecyList = $this->currencyList();
00233 $customPriceList =& $this->customPriceList();
00234 foreach ( $customPriceList as $price )
00235 {
00236 if ( $price )
00237 {
00238 $currencyCode = $price->attribute( 'currency_code' );
00239 unset( $autoCurrecyList[$currencyCode] );
00240 }
00241 }
00242
00243 return $autoCurrecyList;
00244 }
00245
00246
00247
00248
00249 function &customPriceList()
00250 {
00251 return $this->priceList( EZ_MULTIPRICEDATA_VALUE_TYPE_CUSTOM );
00252 }
00253
00254 function &autoPriceList()
00255 {
00256 return $this->priceList( EZ_MULTIPRICEDATA_VALUE_TYPE_AUTO );
00257 }
00258
00259 function &priceList( $type = false )
00260 {
00261 if ( !isset( $this->PriceList ) )
00262 {
00263 if ( is_object( $this->ContentObjectAttribute ) )
00264 $this->PriceList = eZMultiPriceData::fetch( $this->ContentObjectAttribute->attribute( 'id' ), $this->ContentObjectAttribute->attribute( 'version' ) );
00265
00266 if ( !$this->PriceList )
00267 $this->PriceList = array();
00268 }
00269
00270 $priceList = array();
00271 if ( $type !== false )
00272 {
00273 $prices =& $this->priceList();
00274 $currencyCodeList = array_keys( $prices );
00275 foreach ( $currencyCodeList as $currencyCode )
00276 {
00277 if ( $prices[$currencyCode]->attribute( 'type' ) == $type )
00278 $priceList[$currencyCode] =& $prices[$currencyCode];
00279 }
00280 }
00281 else
00282 {
00283 $priceList =& $this->PriceList;
00284 }
00285
00286 return $priceList;
00287 }
00288
00289 function &incVATPriceList( $type = false )
00290 {
00291 return $this->calcPriceList( EZ_MULTIPRICE_CALCULATION_TYPE_VAT_INCLUDE, $type );
00292 }
00293
00294 function &exVATPriceList( $type = false )
00295 {
00296 return $this->calcPriceList( EZ_MULTIPRICE_CALCULATION_TYPE_VAT_EXCLUDE, $type );
00297 }
00298
00299 function &discountIncVATPriceList( $type = false )
00300 {
00301 return $this->calcPriceList( EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_INCLUDE, $type );
00302 }
00303
00304 function &discountExVATPriceList( $type = false )
00305 {
00306 return $this->calcPriceList( EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_EXCLUDE, $type );
00307 }
00308
00309 function &calcPriceList( $calculationType, $priceType )
00310 {
00311 $priceList = $this->priceList( $priceType );
00312
00313 $currencyCodeList = array_keys( $priceList );
00314 foreach ( $currencyCodeList as $currencyCode )
00315 {
00316 $price =& $priceList[$currencyCode];
00317 switch ( $calculationType )
00318 {
00319 case EZ_MULTIPRICE_CALCULATION_TYPE_VAT_INCLUDE :
00320 {
00321 $value = $this->calcIncVATPrice( $price->attribute( 'value' ) );
00322 } break;
00323
00324 case EZ_MULTIPRICE_CALCULATION_TYPE_VAT_EXCLUDE :
00325 {
00326 $value = $this->calcExVATPrice( $price->attribute( 'value' ) );
00327 } break;
00328
00329 case EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_INCLUDE :
00330 {
00331 $value = $this->calcDiscountIncVATPrice( $price->attribute( 'value' ) );
00332 } break;
00333
00334 case EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_EXCLUDE :
00335 {
00336 $value = $this->calcDiscountIncVATPrice( $price->attribute( 'value' ) );
00337 } break;
00338
00339 default:
00340 {
00341
00342 } break;
00343 }
00344
00345 $price->setAttribute( 'value', $value );
00346 }
00347
00348 return $priceList;
00349 }
00350
00351 function remove( $objectAttributeID, $objectAttributeVersion = null )
00352 {
00353 eZMultiPriceData::remove( $objectAttributeID, $objectAttributeVersion );
00354 }
00355
00356 function removePriceByCurrency( $currencyCode )
00357 {
00358 $price =& $this->priceByCurrency( $currencyCode );
00359 if ( $price )
00360 {
00361 $price->removeByID();
00362 $priceList =& $this->priceList();
00363 unset( $priceList[$currencyCode] );
00364 }
00365 }
00366
00367 function setCustomPrice( $currencyCode, $value )
00368 {
00369 $this->setPriceByCurrency( $currencyCode, $value, EZ_MULTIPRICEDATA_VALUE_TYPE_CUSTOM );
00370 }
00371
00372 function setAutoPrice( $currencyCode, $value )
00373 {
00374 $this->setPriceByCurrency( $currencyCode, $value, EZ_MULTIPRICEDATA_VALUE_TYPE_AUTO );
00375 }
00376
00377 function setPriceByCurrency( $currencyCode, $value, $type )
00378 {
00379 if ( !$this->updatePrice( $currencyCode, $value, $type ) &&
00380 !$this->addPrice( $currencyCode, $value, $type ) )
00381 {
00382 eZDebug::writeWarning( "Unable to set price in '$currencyCode'", 'eZMultiPrice::setPrice' );
00383 return false;
00384 }
00385
00386 return true;
00387 }
00388
00389 function setPrice( $value )
00390 {
00391 }
00392
00393 function updateAutoPriceList()
00394 {
00395 include_once( 'kernel/shop/classes/ezcurrencyconverter.php' );
00396 $converter =& eZCurrencyConverter::instance();
00397
00398 $basePrice = $this->basePrice();
00399 $basePriceValue = $basePrice ? $basePrice->attribute( 'value' ) : 0;
00400 $baseCurrencyCode = $basePrice ? $basePrice->attribute( 'currency_code' ) : false;
00401
00402 $autoCurrencyList =& $this->autoCurrencyList();
00403 foreach( $autoCurrencyList as $currencyCode => $currency )
00404 {
00405 $autoValue = $converter->convert( $baseCurrencyCode, $currencyCode, $basePriceValue );
00406 $this->setAutoPrice( $currencyCode, $autoValue );
00407 }
00408 }
00409
00410 function &createPrice( $currencyCode, $value, $type )
00411 {
00412 $price = false;
00413 if ( is_object( $this->ContentObjectAttribute ) && $this->currencyByCode( $currencyCode ) )
00414 {
00415 $price = eZMultiPriceData::create( $this->ContentObjectAttribute->attribute( 'id' ),
00416 $this->ContentObjectAttribute->attribute( 'version' ),
00417 $currencyCode,
00418 $value,
00419 $type );
00420 }
00421 return $price;
00422 }
00423
00424
00425 function &addPrice( $currencyCode, $value, $type )
00426 {
00427 $price =& $this->createPrice( $currencyCode, $value, $type );
00428 if( $price )
00429 {
00430 if ( $value === false )
00431 $price->setAttribute( 'value', '0.00' );
00432
00433 $priceList =& $this->priceList();
00434 $priceList[$price->attribute( 'currency_code' )] =& $price;
00435
00436 $this->setHasDirtyData( true );
00437 }
00438
00439 return $price;
00440 }
00441
00442 function &updatePrice( $currencyCode, $value, $type )
00443 {
00444 $price =& $this->priceByCurrency( $currencyCode );
00445 if( $price )
00446 {
00447 if ( $value !== false )
00448 $price->setAttribute( 'value', $value );
00449
00450 if ( $type !== false )
00451 $price->setAttribute( 'type', $type );
00452
00453 $this->setHasDirtyData( true );
00454 }
00455
00456 return $price;
00457 }
00458
00459 function &customPrice( $currencyCode )
00460 {
00461 return $this->priceByCurrency( $currencyCode, EZ_MULTIPRICEDATA_VALUE_TYPE_CUSTOM );
00462 }
00463
00464 function &autoPrice( $currencyCode )
00465 {
00466 return $this->priceByCurrency( $currencyCode, EZ_MULTIPRICEDATA_VALUE_TYPE_AUTO );
00467 }
00468
00469
00470
00471 function &priceByCurrency( $currencyCode, $type = false )
00472 {
00473 $price = false;
00474 $priceList =& $this->priceList();
00475
00476 if ( isset( $priceList[$currencyCode] ) )
00477 {
00478 if( $type === false || $priceList[$currencyCode]->attribute( 'type' ) == $type )
00479 $price =& $priceList[$currencyCode];
00480 }
00481
00482 return $price;
00483 }
00484
00485 function &price()
00486 {
00487 $value = '0.0';
00488 if ( $currencyCode = $this->preferredCurrencyCode() )
00489 {
00490 $price =& $this->priceByCurrency( $currencyCode );
00491 if ( $price )
00492 $value = $price->attribute( 'value' );
00493 }
00494
00495 return $value;
00496 }
00497
00498 function ¤cyByCode( $currencyCode )
00499 {
00500 $currnecy = false;
00501 $currencyList =& $this->currencyList();
00502 if ( isset( $currencyList[$currencyCode] ) )
00503 $currency =& $currencyList[$currencyCode];
00504
00505 return $currency;
00506 }
00507
00508 function store()
00509 {
00510 if ( $this->hasDirtyData() )
00511 {
00512 $this->storePriceList();
00513 $this->setHasDirtyData( false );
00514 }
00515 }
00516
00517 function storePriceList()
00518 {
00519 if ( isset( $this->PriceList ) && count( $this->PriceList ) > 0 )
00520 {
00521 $priceList =& $this->priceList();
00522 foreach ( $priceList as $price )
00523 $price->store();
00524 }
00525 }
00526
00527 function hasDirtyData()
00528 {
00529 return $this->HasDataDirty;
00530 }
00531
00532 function setHasDirtyData( $hasDirtyData )
00533 {
00534 $this->HasDataDirty = $hasDirtyData;
00535 }
00536
00537
00538
00539
00540 function baseCurrency()
00541 {
00542
00543
00544
00545 $baseCurrency = false;
00546 $customPriceList =& $this->customPriceList();
00547 $currencies = array_keys( $customPriceList );
00548 if ( count( $currencies ) > 0 )
00549 $baseCurrency = $currencies[0];
00550
00551 return $baseCurrency;
00552 }
00553
00554 function basePrice()
00555 {
00556 $baseCurrencyCode = $this->baseCurrency();
00557 $basePrice =& $this->priceByCurrency( $baseCurrencyCode );
00558 return $basePrice;
00559 }
00560
00561 function ¤cy()
00562 {
00563 return $this->preferredCurrencyCode();
00564 }
00565
00566 function DOMDocument()
00567 {
00568 $doc = new eZDOMDocument( 'Multiprice' );
00569 $root = $doc->createElementNode( 'ezmultiprice' );
00570 $doc->setRoot( $root );
00571
00572 $priceListNode = $doc->createElementNode( 'price-list' );
00573
00574 $priceList =& $this->attribute( 'price_list' );
00575 foreach ( $priceList as $price )
00576 {
00577 $currencyCode =& $price->attribute( 'currency_code' );
00578 $value =& $price->attribute( 'value' );
00579 $type =& $price->attribute( 'type' );
00580
00581 $priceNode = $doc->createElementNode( 'price' );
00582
00583 $priceNode->appendAttribute( eZDOMDocument::createAttributeNode( 'currency-code', $currencyCode ) );
00584 $priceNode->appendAttribute( eZDOMDocument::createAttributeNode( 'value', $value ) );
00585 $priceNode->appendAttribute( eZDOMDocument::createAttributeNode( 'type', $type ) );
00586
00587 $priceListNode->appendChild( $priceNode );
00588 unset( $priceNode );
00589 }
00590
00591 $root->appendChild( $priceListNode );
00592
00593 return $doc;
00594 }
00595
00596 function decodeDOMTree( $rootNode )
00597 {
00598 $priceNodes = $rootNode->elementChildrenByName( 'price-list' );
00599 if ( $priceNodes )
00600 {
00601 foreach( $priceNodes as $priceNode )
00602 {
00603 $currencyCode = $priceNode->attributeValue( 'currency-code');
00604 $value = $priceNode->attributeValue( 'value');
00605 $type = $priceNode->attributeValue( 'type');
00606
00607 $this->setPriceByCurrency( $currencyCode, $value, $type );
00608 }
00609 }
00610 }
00611
00612
00613 var $PriceList;
00614 var $CurrencyList;
00615 var $HasDataDirty;
00616 var $ContentObjectAttribute;
00617 }
00618
00619 ?>