[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/kernel/classes/ -> ezvatmanager.php (source)

   1  <?php
   2  //
   3  // Definition of eZVATManager class
   4  //
   5  // Created on: <16-Feb-2006 23:02:53 vs>
   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 ezvatmanager.php
  30  */
  31  
  32  /*!
  33    \class eZVATManager ezvatmanager.php
  34    \brief The class eZVATManager does
  35  
  36  */
  37  
  38  class eZVATManager
  39  {
  40      /**
  41       * Get percentage of VAT type corresponding to the given product and country the user is from.
  42       *
  43       * \return Percentage, or null on error.
  44       * \public
  45       * \static
  46       */
  47      function getVAT( $object, $country )
  48      {
  49          // Load VAT handler.
  50          if ( !is_object( $handler = eZVATManager::loadVATHandler() ) )
  51          {
  52              if ( $handler === true )
  53                  eZDebug::writeWarning( "No VAT handler specified but dynamic VAT charging is used." );
  54  
  55              return null;
  56          }
  57  
  58          // Check if user country must be specified.
  59          $requireUserCountry = eZVATManager::isUserCountryRequired();
  60  
  61          // Determine user country if it's not specified
  62          if ( $country === false )
  63              $country = eZVATManager::getUserCountry();
  64  
  65          if ( !$country && $requireUserCountry )
  66              eZDebug::writeNotice( "User country is not specified." );
  67  
  68          return $handler->getVatPercent( $object, $country );
  69      }
  70  
  71      /**
  72       * Check if users must have country specified.
  73       *
  74       * \public
  75       * \static
  76       */
  77      function isUserCountryRequired()
  78      {
  79          // Check if user country must be specified.
  80          $requireUserCountry = true;
  81          $shopINI =& eZINI::instance( 'shop.ini' );
  82          if ( $shopINI->hasVariable( 'VATSettings', 'RequireUserCountry' ) )
  83              $requireUserCountry = ( $shopINI->variable( 'VATSettings', 'RequireUserCountry' ) == 'true' );
  84          return $requireUserCountry;
  85      }
  86  
  87      /**
  88       * Determine name of content attribute that contains user's country.
  89       *
  90       * \private
  91       * \static
  92       */
  93      function getUserCountryAttributeName( $requireUserCountry )
  94      {
  95          $ini =& eZINI::instance( 'shop.ini' );
  96          if ( !$ini->hasVariable( 'VATSettings', 'UserCountryAttribute' ) )
  97          {
  98              if ( $requireUserCountry )
  99              {
 100                  eZDebug::writeError( "Cannot find user country: please specify its attribute identifier " .
 101                                       "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
 102                                       'getUserCountryAttributeName' );
 103              }
 104              return null;
 105          }
 106  
 107          $countryAttributeName = $ini->variable( 'VATSettings', 'UserCountryAttribute' );
 108          if ( !$countryAttributeName )
 109          {
 110              if ( $requireUserCountry )
 111              {
 112                  eZDebug::writeError( "Cannot find user country: empty attribute name specified " .
 113                                       "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
 114                                       'getUserCountryAttributeName' );
 115              }
 116  
 117              return null;
 118          }
 119  
 120          return $countryAttributeName;
 121      }
 122  
 123      /**
 124       * Determine user's country.
 125       *
 126       * \public
 127       * \static
 128       */
 129      function getUserCountry( $user = false, $considerPreferedCountry = true )
 130      {
 131          $requireUserCountry = eZVATManager::isUserCountryRequired();
 132  
 133          // If current user has set his/her preferred country via the toolbar
 134          if ( $considerPreferedCountry )
 135          {
 136              // return it
 137              include_once ( 'kernel/shop/classes/ezshopfunctions.php' );
 138              $country = eZShopFunctions::getPreferredUserCountry();
 139              if ( $country )
 140              {
 141                  eZDebug::writeDebug( "Applying user's preferred country <$country> while charging VAT" );
 142                  return $country;
 143              }
 144          }
 145  
 146          // Otherwise fetch country saved in the user object.
 147  
 148          if ( $user === false )
 149          {
 150              require_once ( 'kernel/classes/datatypes/ezuser/ezuser.php' );
 151              $user = eZUser::currentUser();
 152          }
 153  
 154          $userObject = $user->attribute( 'contentobject' );
 155          $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
 156  
 157          if ( $countryAttributeName === null )
 158              return null;
 159  
 160          $userDataMap = $userObject->attribute( 'data_map' );
 161          if ( !isset( $userDataMap[$countryAttributeName] ) )
 162          {
 163              if ( $requireUserCountry )
 164              {
 165                  eZDebug::writeError( "Cannot find user country: there is no attribute '$countryAttributeName' in object '" .
 166                                         $userObject->attribute( 'name' ) .
 167                                         "' of class '" .
 168                                         $userObject->attribute( 'class_name' ) . "'.",
 169                                       'eZVATManager::getUserCountry' );
 170              }
 171              return null;
 172          }
 173  
 174          $countryAttribute = $userDataMap[$countryAttributeName];
 175          $countryContent = $countryAttribute->attribute( 'content' );
 176  
 177          if ( $countryContent === null )
 178          {
 179              if ( $requireUserCountry )
 180              {
 181                  eZDebug::writeError( "User country is not specified in object '" .
 182                                         $userObject->attribute( 'name' ) .
 183                                         "' of class '" .
 184                                         $userObject->attribute( 'class_name' ) . "'." ,
 185                                       'eZVATManager::getUserCountry' );
 186              }
 187              return null;
 188          }
 189  
 190          if ( is_object( $countryContent ) )
 191              $country = $countryContent->attribute( 'value' );
 192          elseif ( is_array( $countryContent ) )
 193              $country = $countryContent['value'];
 194          else
 195          {
 196              if ( $requireUserCountry )
 197              {
 198                  eZDebug::writeError( "User country is not specified or specified incorrectly in object '" .
 199                                         $userObject->attribute( 'name' ) .
 200                                         "' of class '" .
 201                                         $userObject->attribute( 'class_name' ) . "'." ,
 202                                       'eZVATManager::getUserCountry' );
 203              }
 204              return null;
 205          }
 206  
 207          return $country;
 208      }
 209  
 210      /**
 211       * Set user's country.
 212       *
 213       * \public
 214       * \static
 215       */
 216      function setUserCountry( $user, $country )
 217      {
 218          $userObject = $user->attribute( 'contentobject' );
 219          $requireUserCountry = eZVATManager::isUserCountryRequired();
 220          require_once ( 'kernel/classes/ezvatmanager.php' );
 221          $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
 222          if ( $countryAttributeName === null )
 223              return false;
 224  
 225          $userDataMap = $userObject->attribute( 'data_map' );
 226          if ( !isset( $userDataMap[$countryAttributeName] ) )
 227          {
 228              if ( $requireUserCountry )
 229              {
 230                  eZDebug::writeError( "Cannot set user country: there is no attribute '$countryAttributeName' in object '" .
 231                                         $userObject->attribute( 'name' ) .
 232                                         "' of class '" .
 233                                         $userObject->attribute( 'class_name' ) . "'.",
 234                                       'eZVATManager::getUserCountry' );
 235              }
 236  
 237              return false;
 238          }
 239  
 240          eZDebug::writeNotice( sprintf( "Saving country '%s' for user '%s'",
 241                                         $country, $user->attribute( 'login' )  ) );
 242  
 243          $countryAttribute = $userDataMap[$countryAttributeName];
 244          $countryAttributeContent = $countryAttribute->content();
 245          if ( is_array( $countryAttributeContent ) )
 246              $countryAttributeContent['value'] = $country;
 247          elseif ( is_object( $countryAttributeContent ) )
 248              $countryAttributeContent->setAttribute( 'value', $country );
 249          // not sure that this line is needed since content is returned by reference
 250          $countryAttribute->setContent( $countryAttributeContent );
 251          $countryAttribute->store();
 252  
 253          return true;
 254      }
 255  
 256  
 257      /*!
 258       \return true if a VAT handler is specified in the ini setting, false otherwise.
 259       */
 260      function isDynamicVatChargingEnabled()
 261      {
 262          if ( isset( $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] ) )
 263              return $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'];
 264  
 265          $enabled = is_object( eZVATManager::loadVATHandler() );
 266          $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] = $enabled;
 267          return $enabled;
 268      }
 269  
 270      /*!
 271       Load VAT handler (if specified).
 272  
 273       \private
 274       \static
 275       \return true if no handler specified,
 276               false if a handler specified but could not be loaded,
 277               handler object if handler specified and found.
 278       */
 279      function loadVATHandler()
 280      {
 281          // FIXME: cache loaded handler.
 282  
 283          $shopINI =& eZINI::instance( 'shop.ini' );
 284  
 285          if ( !$shopINI->hasVariable( 'VATSettings', 'Handler' ) )
 286              return true;
 287  
 288          $handlerName = $shopINI->variable( 'VATSettings', 'Handler' );
 289          $repositoryDirectories = $shopINI->variable( 'VATSettings', 'RepositoryDirectories' );
 290          $extensionDirectories = $shopINI->variable( 'VATSettings', 'ExtensionDirectories' );
 291  
 292          $baseDirectory = eZExtension::baseDirectory();
 293          foreach ( $extensionDirectories as $extensionDirectory )
 294          {
 295              $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/vathandlers';
 296              if ( file_exists( $extensionPath ) )
 297                  $repositoryDirectories[] = $extensionPath;
 298          }
 299  
 300          $foundHandler = false;
 301          foreach ( $repositoryDirectories as $repositoryDirectory )
 302          {
 303              $includeFile = "$repositoryDirectory/{$handlerName}vathandler.php";
 304  
 305              if ( file_exists( $includeFile ) )
 306              {
 307                  $foundHandler = true;
 308                  break;
 309              }
 310          }
 311  
 312          if ( !$foundHandler )
 313          {
 314              eZDebug::writeError( "VAT handler '$handlerName' not found, " .
 315                                   "searched in these directories: " .
 316                                   implode( ', ', $repositoryDirectories ),
 317                                   'eVATManager::loadVATHandler' );
 318              return false;
 319          }
 320  
 321          require_once( $includeFile );
 322          $className = $handlerName . 'VATHandler';
 323          return new $className;
 324      }
 325  }
 326  ?>


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