[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/kernel/shop/classes/ -> ezcurrencyconverter.php (source)

   1  <?php
   2  //
   3  // Definition of eZCurrencyConverter class
   4  //
   5  // Created on: <28-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 ezcurrencyconverter.php
  30  */
  31  
  32  /*!
  33    \class eZCurrencyConverter ezcurrencyconverter.php
  34    \brief Handles conversions from one curreny into another, applying
  35    rounding if it's needed.
  36  
  37  
  38  */
  39  
  40  define( 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_NONE', 1 );
  41  define( 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_ROUND', 2 );
  42  define( 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_CEIL', 3 );
  43  define( 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_FLOOR', 4 );
  44  
  45  include_once ( 'kernel/shop/classes/ezcurrencydata.php' );
  46  
  47  class eZCurrencyConverter
  48  {
  49      function eZCurrencyConverter()
  50      {
  51          $this->setMathHandler( null );
  52          $this->setRoundingType( null );
  53          $this->setRoundingPrecision( null );
  54          $this->setRoundingTarget( null );
  55      }
  56  
  57      function &instance()
  58      {
  59          $impl =& $_GLOBALS["eZCurrencyConverterGlobalInstance"];
  60  
  61          if ( !is_object( $impl ) || get_class( $impl ) !== 'ezcurrencyconverter' )
  62          {
  63              $impl = new eZCurrencyConverter();
  64          }
  65  
  66          return $impl;
  67      }
  68  
  69      /*!
  70       \return converted value for $value form $fromCurrency to $toCurrency. Applyes rounding if needed.
  71      */
  72      function convert( $fromCurrency, $toCurrency, $value, $applyRounding = true )
  73      {
  74          if ( $fromCurrency == false || $toCurrency == false || $fromCurrency == $toCurrency || $value == 0 )
  75              return $value;
  76  
  77          $math =& $this->mathHandler();
  78  
  79          $crossRate = $this->crossRate( $fromCurrency, $toCurrency );
  80          $convertedValue = $math->mul( $value, $crossRate );
  81          if ( $applyRounding )
  82          {
  83              switch ( $this->roundingType() )
  84              {
  85                  case 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_ROUND':
  86                      {
  87                          $convertedValue = $math->round( $convertedValue, $this->roundingPrecision(), $this->roundingTarget() );
  88                      } break;
  89  
  90                  case 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_CEIL':
  91                      {
  92                          $convertedValue = $math->ceil( $convertedValue, $this->roundingPrecision(), $this->roundingTarget() );
  93                      } break;
  94  
  95                  case 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_FLOOR':
  96                      {
  97                          $convertedValue = $math->floor( $convertedValue, $this->roundingPrecision(), $this->roundingTarget() );
  98                      } break;
  99  
 100                  case 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_NONE':
 101                  default:
 102                      break;
 103              }
 104          }
 105  
 106          return $convertedValue;
 107      }
 108  
 109      /*!
 110       \return converted value for $value form currency specified in locale to $toCurrency. Applyes rounding if needed.
 111      */
 112      function convertFromLocaleCurrency( $toCurrency, $value, $applyRounding = true )
 113      {
 114          include_once ( 'lib/ezlocale/classes/ezlocale.php' );
 115          $locale =& eZLocale::instance();
 116          $fromCurrency = $locale->currencyShortName();
 117          $retValue = $this->convert( $fromCurrency, $toCurrency, $value, $applyRounding );
 118          return $retValue;
 119      }
 120  
 121      function rateValue( $currencyCode )
 122      {
 123          $rateValue = 0;
 124  
 125          $currencyList =& $this->currencyList();
 126          $currency =& $currencyList[$currencyCode];
 127  
 128          if ( is_object( $currency ) )
 129              $rateValue = $currency->rateValue();
 130  
 131          return $rateValue;
 132      }
 133  
 134      function crossRate( $fromCurrency, $toCurrency )
 135      {
 136          $fromRate = $this->rateValue( $fromCurrency );
 137          $toRate = $this->rateValue( $toCurrency );
 138  
 139          $crossRate = 0;
 140          if ( $fromRate > 0 )
 141          {
 142              $math =& $this->mathHandler();
 143              $crossRate = $math->div( $toRate, $fromRate );
 144          }
 145  
 146          return $crossRate;
 147      }
 148  
 149      function &mathHandler()
 150      {
 151          if ( $this->MathHandler === null )
 152          {
 153              include_once ( 'lib/ezutils/classes/ezini.php' );
 154              include_once ( 'lib/ezmath/classes/mathhandlers/ezphpmath.php' );
 155  
 156              $ini =& eZINI::instance( 'shop.ini' );
 157  
 158              $mathType = $ini->variable( 'MathSettings', 'MathHandler' );
 159              $mathType = strtolower( $mathType );
 160  
 161              $params = array( 'scale' => $ini->variable( 'MathSettings', 'MathScale' ) );
 162  
 163              $this->setMathHandler( eZPHPMath::create( $mathType, $params ) );
 164          }
 165  
 166          return $this->MathHandler;
 167      }
 168  
 169      function setMathHandler( $handler )
 170      {
 171          $this->MathHandler = $handler;
 172      }
 173  
 174      function &currencyList()
 175      {
 176          if ( !isset( $this->CurrencyList ) )
 177              $this->CurrencyList = eZCurrencyData::fetchList();
 178  
 179          return $this->CurrencyList;
 180      }
 181  
 182      function roundingType()
 183      {
 184          if ( $this->RoundingType === null )
 185          {
 186              include_once ( 'lib/ezutils/classes/ezini.php' );
 187              $ini =& eZINI::instance( 'shop.ini' );
 188  
 189              $roundingType = 'EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_' . strtoupper( $ini->variable( 'MathSettings', 'RoundingType' ) );
 190              if ( !defined( $roundingType ) )
 191                  $roundingType = EZ_CURRENCY_CONVERTER_ROUNDING_TYPE_NONE;
 192  
 193              $this->setRoundingType( $roundingType );
 194          }
 195  
 196          return $this->RoundingType;
 197      }
 198  
 199      function setRoundingType( $type )
 200      {
 201          $this->RoundingType = $type;
 202      }
 203  
 204      function roundingPrecision()
 205      {
 206          if ( $this->RoundingPrecision === null )
 207          {
 208              include_once ( 'lib/ezutils/classes/ezini.php' );
 209              $ini =& eZINI::instance( 'shop.ini' );
 210              $this->setRoundingPrecision( $ini->variable( 'MathSettings', 'RoundingPrecision' ) );
 211          }
 212  
 213          return $this->RoundingPrecision;
 214      }
 215  
 216      function setRoundingPrecision( $precision )
 217      {
 218          $this->RoundingPrecision = $precision;
 219      }
 220  
 221      function roundingTarget()
 222      {
 223          if ( $this->RoundingTarget === null )
 224          {
 225              include_once ( 'lib/ezutils/classes/ezini.php' );
 226              $ini =& eZINI::instance( 'shop.ini' );
 227              $this->setRoundingTarget( $ini->variable( 'MathSettings', 'RoundingTarget' ) );
 228          }
 229          return $this->RoundingTarget;
 230      }
 231  
 232      function setRoundingTarget( $target )
 233      {
 234          $this->RoundingTarget = $target;
 235      }
 236  
 237  
 238      var $CurrencyList;
 239      var $MathHandler;
 240      var $RoundingType;
 241      var $RoundingPrecision;
 242      var $RoundingTarget;
 243  }
 244  
 245  ?>


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