[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/datatypes/ezmultiprice/ -> ezmultiprice.php (source)

   1  <?php
   2  //
   3  // Definition of eZMultiPrice class
   4  //
   5  // Created on: <04-Nov-2005 12:26:52 dl>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezmultiprice.php
  30  */
  31  
  32  /*!
  33    \class eZMultiPrice ezmultiprice.php
  34    \ingroup eZDatatype
  35    \brief Handles prices in different currencies with VAT and discounts.
  36  
  37    The available attributes are:
  38    - vat_type
  39    - current_user
  40    - is_vat_included
  41    - selected_vat_type
  42    - vat_percent
  43    - inc_vat_price
  44    - ex_vat_price
  45    - discount_percent
  46    - discount_price_inc_vat
  47    - discount_price_ex_vat
  48    - has_discount
  49    - price
  50  */
  51  
  52  include_once ( "kernel/shop/classes/ezsimpleprice.php" );
  53  include_once ( 'kernel/shop/classes/ezmultipricedata.php' );
  54  
  55  define( 'EZ_MULTIPRICE_CALCULATION_TYPE_VAT_INCLUDE', 1 );
  56  define( 'EZ_MULTIPRICE_CALCULATION_TYPE_VAT_EXCLUDE', 2 );
  57  define( 'EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_INCLUDE', 3 );
  58  define( 'EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_EXCLUDE', 4 );
  59  
  60  class eZMultiPrice extends eZSimplePrice
  61  {
  62      /*!
  63       Constructor
  64      */
  65      function eZMultiPrice( &$classAttribute, &$contentObjectAttribute, $storedPrice = null )
  66      {
  67          eZSimplePrice::eZSimplePrice( $classAttribute, $contentObjectAttribute, $storedPrice );
  68  
  69          $isVatIncluded = ( $classAttribute->attribute( EZ_DATATYPESTRING_MULTIPRICE_INCLUDE_VAT_FIELD ) == 1 );
  70          $VATID =& $classAttribute->attribute( EZ_DATATYPESTRING_MULTIPRICE_VAT_ID_FIELD );
  71  
  72          $this->setVatIncluded( $isVatIncluded );
  73          $this->setVatType( $VATID );
  74  
  75          $this->IsDataDirty = false;
  76          $this->ContentObjectAttribute =& $contentObjectAttribute;
  77      }
  78  
  79      /*!
  80       \return An array with attributes that is available.
  81      */
  82      function attributes()
  83      {
  84          return array( 'currency_list',
  85                        'auto_currency_list',
  86                        'price_list',
  87                        'auto_price_list',
  88                        'custom_price_list',
  89                        'inc_vat_price_list',
  90                        'ex_vat_price_list',
  91                        'discount_inc_vat_price_list',
  92                        'discount_ex_vat_price_list' );
  93      }
  94  
  95      /*!
  96       \return \c true if the attribute named \a $attr exists.
  97      */
  98      function hasAttribute( $attr )
  99      {
 100          $hasAttribute = in_array( $attr, eZMultiPrice::attributes() );
 101          if ( !$hasAttribute )
 102              $hasAttribute = eZSimplePrice::attributes( $attr );
 103  
 104          return $hasAttribute;
 105      }
 106  
 107      /*!
 108        Sets the attribute named \a $attr to value \a $value.
 109      */
 110      function setAttribute( $attr, $value )
 111      {
 112          switch ( $attr )
 113          {
 114              case 'currency_list':
 115              {
 116              } break;
 117  
 118              case 'auto_currency_list':
 119              {
 120              } break;
 121  
 122              case 'price_list':
 123              {
 124              } break;
 125  
 126              case 'auto_price_list':
 127              {
 128              } break;
 129  
 130              case 'custom_price_list':
 131              {
 132              } break;
 133  
 134              default:
 135              {
 136                  eZSimplePrice::setAttribute( $attr, $value );
 137              } break;
 138          }
 139      }
 140  
 141      /*!
 142       \return The value of the attribute named \a $attr or \c null if it doesn't exist.
 143      */
 144      function &attribute( $attr )
 145      {
 146          switch ( $attr )
 147          {
 148              case 'currency_list':
 149              {
 150                  return $this->currencyList();
 151              } break;
 152  
 153              case 'auto_currency_list':
 154              {
 155                  return $this->autoCurrencyList();
 156              } break;
 157  
 158              case 'price_list':
 159              {
 160                  return $this->priceList();
 161              } break;
 162  
 163              case 'inc_vat_price_list':
 164              {
 165                  return $this->incVATPriceList();
 166              } break;
 167  
 168              case 'ex_vat_price_list':
 169              {
 170                  return $this->exVATPriceList();
 171              } break;
 172  
 173              case 'discount_inc_vat_price_list':
 174              {
 175                  return $this->discountIncVATPriceList();
 176              } break;
 177  
 178              case 'discount_ex_vat_price_list':
 179              {
 180                  return $this->discountExVATPriceList();
 181              } break;
 182  
 183              case 'auto_price_list':
 184              {
 185                  return $this->autoPriceList();
 186              } break;
 187  
 188              case 'custom_price_list':
 189              {
 190                  return $this->customPriceList();
 191              } break;
 192  
 193              default :
 194              {
 195                  return eZSimplePrice::attribute( $attr );
 196              } break;
 197          }
 198      }
 199  
 200      /*!
 201       functional attribute
 202      */
 203  
 204      function &preferredCurrencyCode()
 205      {
 206          include_once ( 'kernel/shop/classes/ezshopfunctions.php' );
 207          $currency = eZShopFunctions::preferredCurrencyCode();
 208          return $currency;
 209      }
 210  
 211      function &currencyList()
 212      {
 213          if ( !isset( $this->CurrencyList ) )
 214          {
 215              include_once ( 'kernel/shop/classes/ezcurrencydata.php' );
 216              $this->CurrencyList = eZCurrencyData::fetchList();
 217          }
 218  
 219          return $this->CurrencyList;
 220      }
 221  
 222      /*!
 223       functional attribute
 224      */
 225      function &autoCurrencyList()
 226      {
 227          // 'auto currencies' are the currencies used for 'auto' prices.
 228          // 'auto currencies' = 'all currencies' - 'currencies of custom prices'
 229  
 230          $autoCurrecyList = $this->currencyList();
 231          $customPriceList =& $this->customPriceList();
 232          foreach ( $customPriceList as $price )
 233          {
 234              if ( $price )
 235              {
 236                  $currencyCode = $price->attribute( 'currency_code' );
 237                  unset( $autoCurrecyList[$currencyCode] );
 238              }
 239          }
 240  
 241          return $autoCurrecyList;
 242      }
 243  
 244      /*!
 245       functional attribute
 246      */
 247      function &customPriceList()
 248      {
 249          return $this->priceList( EZ_MULTIPRICEDATA_VALUE_TYPE_CUSTOM );
 250      }
 251  
 252      function &autoPriceList()
 253      {
 254          return $this->priceList( EZ_MULTIPRICEDATA_VALUE_TYPE_AUTO );
 255      }
 256  
 257      function &priceList( $type = false )
 258      {
 259          if ( !isset( $this->PriceList ) )
 260          {
 261              if ( is_object( $this->ContentObjectAttribute ) )
 262                  $this->PriceList = eZMultiPriceData::fetch( $this->ContentObjectAttribute->attribute( 'id' ), $this->ContentObjectAttribute->attribute( 'version' ) );
 263  
 264              if ( !$this->PriceList )
 265                  $this->PriceList = array();
 266          }
 267  
 268          $priceList = array();
 269          if ( $type !== false )
 270          {
 271              $prices =& $this->priceList();
 272              $currencyCodeList = array_keys( $prices );
 273              foreach ( $currencyCodeList as $currencyCode )
 274              {
 275                  if ( $prices[$currencyCode]->attribute( 'type' ) == $type )
 276                      $priceList[$currencyCode] =& $prices[$currencyCode];
 277              }
 278          }
 279          else
 280          {
 281              $priceList =& $this->PriceList;
 282          }
 283  
 284          return $priceList;
 285      }
 286  
 287      function &incVATPriceList( $type = false )
 288      {
 289          return $this->calcPriceList( EZ_MULTIPRICE_CALCULATION_TYPE_VAT_INCLUDE, $type );
 290      }
 291  
 292      function &exVATPriceList( $type = false )
 293      {
 294          return $this->calcPriceList( EZ_MULTIPRICE_CALCULATION_TYPE_VAT_EXCLUDE, $type );
 295      }
 296  
 297      function &discountIncVATPriceList( $type = false )
 298      {
 299          return $this->calcPriceList( EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_INCLUDE, $type );
 300      }
 301  
 302      function &discountExVATPriceList( $type = false )
 303      {
 304          return $this->calcPriceList( EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_EXCLUDE, $type );
 305      }
 306  
 307      function &calcPriceList( $calculationType, $priceType )
 308      {
 309          $priceList = $this->priceList( $priceType );
 310  
 311          $currencyCodeList = array_keys( $priceList );
 312          foreach ( $currencyCodeList as $currencyCode )
 313          {
 314              $price =& $priceList[$currencyCode];
 315              switch ( $calculationType )
 316              {
 317                  case EZ_MULTIPRICE_CALCULATION_TYPE_VAT_INCLUDE :
 318                  {
 319                      $value = $this->calcIncVATPrice( $price->attribute( 'value' ) );
 320                  } break;
 321  
 322                  case EZ_MULTIPRICE_CALCULATION_TYPE_VAT_EXCLUDE :
 323                  {
 324                      $value = $this->calcExVATPrice( $price->attribute( 'value' ) );
 325                  } break;
 326  
 327                  case EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_INCLUDE :
 328                  {
 329                      $value = $this->calcDiscountIncVATPrice( $price->attribute( 'value' ) );
 330                  } break;
 331  
 332                  case EZ_MULTIPRICE_CALCULATION_TYPE_DISCOUNT_EXCLUDE :
 333                  {
 334                      $value = $this->calcDiscountIncVATPrice( $price->attribute( 'value' ) );
 335                  } break;
 336  
 337                  default:
 338                  {
 339                      // do nothing
 340                  } break;
 341              }
 342  
 343              $price->setAttribute( 'value', $value );
 344          }
 345  
 346          return $priceList;
 347      }
 348  
 349      function remove( $objectAttributeID, $objectAttributeVersion = null )
 350      {
 351          eZMultiPriceData::remove( $objectAttributeID, $objectAttributeVersion );
 352      }
 353  
 354      function removePriceByCurrency( $currencyCode )
 355      {
 356          $price =& $this->priceByCurrency( $currencyCode );
 357          if ( $price )
 358          {
 359              $price->removeByID();
 360              $priceList =& $this->priceList();
 361              unset( $priceList[$currencyCode] );
 362          }
 363      }
 364  
 365      function setCustomPrice( $currencyCode, $value )
 366      {
 367          $this->setPriceByCurrency( $currencyCode, $value, EZ_MULTIPRICEDATA_VALUE_TYPE_CUSTOM );
 368      }
 369  
 370      function setAutoPrice( $currencyCode, $value )
 371      {
 372          $this->setPriceByCurrency( $currencyCode, $value, EZ_MULTIPRICEDATA_VALUE_TYPE_AUTO );
 373      }
 374  
 375      function setPriceByCurrency( $currencyCode, $value, $type )
 376      {
 377          if ( !$this->updatePrice( $currencyCode, $value, $type ) &&
 378               !$this->addPrice( $currencyCode, $value, $type ) )
 379          {
 380              eZDebug::writeWarning( "Unable to set price in '$currencyCode'", 'eZMultiPrice::setPrice' );
 381              return false;
 382          }
 383  
 384          return true;
 385      }
 386  
 387      function setPrice( $value )
 388      {
 389      }
 390  
 391      function updateAutoPriceList()
 392      {
 393          include_once ( 'kernel/shop/classes/ezcurrencyconverter.php' );
 394          $converter =& eZCurrencyConverter::instance();
 395  
 396          $basePrice = $this->basePrice();
 397          $basePriceValue = $basePrice ? $basePrice->attribute( 'value' ) : 0;
 398          $baseCurrencyCode = $basePrice ? $basePrice->attribute( 'currency_code' ) : false;
 399  
 400          $autoCurrencyList =& $this->autoCurrencyList();
 401          foreach( $autoCurrencyList as $currencyCode => $currency )
 402          {
 403              $autoValue = $converter->convert( $baseCurrencyCode, $currencyCode, $basePriceValue );
 404              $this->setAutoPrice( $currencyCode, $autoValue );
 405          }
 406      }
 407  
 408      function &createPrice( $currencyCode, $value, $type )
 409      {
 410          $price = false;
 411          if ( is_object( $this->ContentObjectAttribute ) && $this->currencyByCode( $currencyCode ) )
 412          {
 413              $price = eZMultiPriceData::create( $this->ContentObjectAttribute->attribute( 'id' ),
 414                                                 $this->ContentObjectAttribute->attribute( 'version' ),
 415                                                 $currencyCode,
 416                                                 $value,
 417                                                 $type );
 418          }
 419          return $price;
 420      }
 421  
 422  
 423      function &addPrice( $currencyCode, $value, $type )
 424      {
 425          $price =& $this->createPrice( $currencyCode, $value, $type );
 426          if( $price )
 427          {
 428              if ( $value === false )
 429                  $price->setAttribute( 'value', '0.00' );
 430  
 431              $priceList =& $this->priceList();
 432              $priceList[$price->attribute( 'currency_code' )] =& $price;
 433  
 434              $this->setHasDirtyData( true );
 435          }
 436  
 437          return $price;
 438      }
 439  
 440      function &updatePrice( $currencyCode, $value, $type )
 441      {
 442          $price =& $this->priceByCurrency( $currencyCode );
 443          if( $price )
 444          {
 445              if ( $value !== false )
 446                  $price->setAttribute( 'value', $value );
 447  
 448              if ( $type !== false )
 449                  $price->setAttribute( 'type', $type );
 450  
 451              $this->setHasDirtyData( true );
 452          }
 453  
 454          return $price;
 455      }
 456  
 457      function &customPrice( $currencyCode )
 458      {
 459          return $this->priceByCurrency( $currencyCode, EZ_MULTIPRICEDATA_VALUE_TYPE_CUSTOM );
 460      }
 461  
 462      function &autoPrice( $currencyCode )
 463      {
 464          return $this->priceByCurrency( $currencyCode, EZ_MULTIPRICEDATA_VALUE_TYPE_AUTO );
 465      }
 466  
 467      /*!
 468      */
 469      function &priceByCurrency( $currencyCode, $type = false )
 470      {
 471          $price = false;
 472          $priceList =& $this->priceList();
 473  
 474          if ( isset( $priceList[$currencyCode] ) )
 475          {
 476              if( $type === false || $priceList[$currencyCode]->attribute( 'type' ) == $type )
 477                  $price =& $priceList[$currencyCode];
 478          }
 479  
 480          return $price;
 481      }
 482  
 483      function &price()
 484      {
 485          $value = '0.0';
 486          if ( $currencyCode = $this->preferredCurrencyCode() )
 487          {
 488              $price =& $this->priceByCurrency( $currencyCode );
 489              if ( $price )
 490                  $value = $price->attribute( 'value' );
 491          }
 492  
 493          return $value;
 494      }
 495  
 496      function &currencyByCode( $currencyCode )
 497      {
 498          $currnecy = false;
 499          $currencyList =& $this->currencyList();
 500          if ( isset( $currencyList[$currencyCode] ) )
 501              $currency =& $currencyList[$currencyCode];
 502  
 503          return $currency;
 504      }
 505  
 506      function store()
 507      {
 508          if ( $this->hasDirtyData() )
 509          {
 510              $this->storePriceList();
 511              $this->setHasDirtyData( false );
 512          }
 513      }
 514  
 515      function storePriceList()
 516      {
 517          if ( isset( $this->PriceList ) && count( $this->PriceList ) > 0 )
 518          {
 519              $priceList =& $this->priceList();
 520              foreach ( $priceList as $price )
 521                  $price->store();
 522          }
 523      }
 524  
 525      function hasDirtyData()
 526      {
 527          return $this->HasDataDirty;
 528      }
 529  
 530      function setHasDirtyData( $hasDirtyData )
 531      {
 532          $this->HasDataDirty = $hasDirtyData;
 533      }
 534  
 535      /*!
 536       Returns a currency code of the first custom price.
 537      */
 538      function baseCurrency()
 539      {
 540          // use value of the first custom price as
 541          // base price and base currency.
 542  
 543          $baseCurrency = false;
 544          $customPriceList =& $this->customPriceList();
 545          $currencies = array_keys( $customPriceList );
 546          if ( count( $currencies ) > 0 )
 547              $baseCurrency = $currencies[0];
 548  
 549          return $baseCurrency;
 550      }
 551  
 552      function basePrice()
 553      {
 554          $baseCurrencyCode = $this->baseCurrency();
 555          $basePrice =& $this->priceByCurrency( $baseCurrencyCode );
 556          return $basePrice;
 557      }
 558  
 559      function &currency()
 560      {
 561          return $this->preferredCurrencyCode();
 562      }
 563  
 564      function DOMDocument()
 565      {
 566          $doc = new eZDOMDocument( 'Multiprice' );
 567          $root = $doc->createElementNode( 'ezmultiprice' );
 568          $doc->setRoot( $root );
 569  
 570          $priceListNode = $doc->createElementNode( 'price-list' );
 571  
 572          $priceList =& $this->attribute( 'price_list' );
 573          foreach ( $priceList as $price )
 574          {
 575              $currencyCode =& $price->attribute( 'currency_code' );
 576              $value =& $price->attribute( 'value' );
 577              $type =& $price->attribute( 'type' );
 578  
 579              $priceNode = $doc->createElementNode( 'price' );
 580  
 581              $priceNode->appendAttribute( eZDOMDocument::createAttributeNode( 'currency-code', $currencyCode ) );
 582              $priceNode->appendAttribute( eZDOMDocument::createAttributeNode( 'value', $value ) );
 583              $priceNode->appendAttribute( eZDOMDocument::createAttributeNode( 'type', $type ) );
 584  
 585              $priceListNode->appendChild( $priceNode );
 586              unset( $priceNode );
 587          }
 588  
 589          $root->appendChild( $priceListNode );
 590  
 591          return $doc;
 592      }
 593  
 594      function decodeDOMTree( $rootNode )
 595      {
 596          $priceNodes = $rootNode->elementChildrenByName( 'price-list' );
 597          if ( $priceNodes )
 598          {
 599              foreach( $priceNodes as $priceNode )
 600              {
 601                  $currencyCode = $priceNode->attributeValue( 'currency-code');
 602                  $value = $priceNode->attributeValue( 'value');
 603                  $type = $priceNode->attributeValue( 'type');
 604  
 605                  $this->setPriceByCurrency( $currencyCode, $value, $type );
 606              }
 607          }
 608      }
 609  
 610      /// \privatesection
 611      var $PriceList;
 612      var $CurrencyList;
 613      var $HasDataDirty;
 614      var $ContentObjectAttribute;
 615  }
 616  
 617  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7