eZ Publish  [4.0]
ezvatmanager.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZVATManager class
00004 //
00005 // Created on: <16-Feb-2006 23:02:53 vs>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezvatmanager.php
00032 */
00033 
00034 /*!
00035   \class eZVATManager ezvatmanager.php
00036   \brief The class eZVATManager does
00037 
00038 */
00039 
00040 class eZVATManager
00041 {
00042     /**
00043      * Get percentage of VAT type corresponding to the given product and country the user is from.
00044      *
00045      * \return Percentage, or null on error.
00046      * \public
00047      * \static
00048      */
00049     static function getVAT( $object, $country )
00050     {
00051         // Load VAT handler.
00052         if ( !is_object( $handler = eZVATManager::loadVATHandler() ) )
00053         {
00054             if ( $handler === true )
00055             {
00056                 eZDebug::writeWarning( "No VAT handler specified but dynamic VAT charging is used." );
00057             }
00058 
00059             return null;
00060         }
00061 
00062         // Check if user country must be specified.
00063         $requireUserCountry = eZVATManager::isUserCountryRequired();
00064 
00065         // Determine user country if it's not specified
00066         if ( $country === false )
00067             $country = eZVATManager::getUserCountry();
00068 
00069         if ( !$country && $requireUserCountry )
00070         {
00071             eZDebug::writeNotice( "User country is not specified." );
00072         }
00073 
00074         return $handler->getVatPercent( $object, $country );
00075     }
00076 
00077     /**
00078      * Check if users must have country specified.
00079      *
00080      * \public
00081      * \static
00082      */
00083     static function isUserCountryRequired()
00084     {
00085         // Check if user country must be specified.
00086         $requireUserCountry = true;
00087         $shopINI = eZINI::instance( 'shop.ini' );
00088         if ( $shopINI->hasVariable( 'VATSettings', 'RequireUserCountry' ) )
00089             $requireUserCountry = ( $shopINI->variable( 'VATSettings', 'RequireUserCountry' ) == 'true' );
00090         return $requireUserCountry;
00091     }
00092 
00093     /**
00094      * Determine name of content attribute that contains user's country.
00095      *
00096      * \private
00097      * \static
00098      */
00099     static function getUserCountryAttributeName( $requireUserCountry )
00100     {
00101         $ini = eZINI::instance( 'shop.ini' );
00102         if ( !$ini->hasVariable( 'VATSettings', 'UserCountryAttribute' ) )
00103         {
00104             if ( $requireUserCountry )
00105             {
00106                 eZDebug::writeError( "Cannot find user country: please specify its attribute identifier " .
00107                                        "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
00108                                      'getUserCountryAttributeName' );
00109             }
00110             return null;
00111         }
00112 
00113         $countryAttributeName = $ini->variable( 'VATSettings', 'UserCountryAttribute' );
00114         if ( !$countryAttributeName )
00115         {
00116             if ( $requireUserCountry )
00117             {
00118                 eZDebug::writeError( "Cannot find user country: empty attribute name specified " .
00119                                        "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
00120                                      'getUserCountryAttributeName' );
00121             }
00122 
00123             return null;
00124         }
00125 
00126         return $countryAttributeName;
00127     }
00128 
00129     /**
00130      * Determine user's country.
00131      *
00132      * \public
00133      * \static
00134      */
00135     static function getUserCountry( $user = false, $considerPreferedCountry = true )
00136     {
00137         $requireUserCountry = eZVATManager::isUserCountryRequired();
00138 
00139         // If current user has set his/her preferred country via the toolbar
00140         if ( $considerPreferedCountry )
00141         {
00142             // return it
00143             //include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00144             $country = eZShopFunctions::getPreferredUserCountry();
00145             if ( $country )
00146             {
00147                 eZDebug::writeDebug( "Applying user's preferred country <$country> while charging VAT" );
00148                 return $country;
00149             }
00150         }
00151 
00152         // Otherwise fetch country saved in the user object.
00153 
00154         if ( $user === false )
00155         {
00156             require_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00157             $user = eZUser::currentUser();
00158         }
00159 
00160         $userObject = $user->attribute( 'contentobject' );
00161         $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
00162 
00163         if ( $countryAttributeName === null )
00164             return null;
00165 
00166         $userDataMap = $userObject->attribute( 'data_map' );
00167         if ( !isset( $userDataMap[$countryAttributeName] ) )
00168         {
00169             if ( $requireUserCountry )
00170             {
00171                 eZDebug::writeError( "Cannot find user country: there is no attribute '$countryAttributeName' in object '" .
00172                                        $userObject->attribute( 'name' ) .
00173                                        "' of class '" .
00174                                        $userObject->attribute( 'class_name' ) . "'.",
00175                                      'eZVATManager::getUserCountry' );
00176             }
00177             return null;
00178         }
00179 
00180         $countryAttribute = $userDataMap[$countryAttributeName];
00181         $countryContent = $countryAttribute->attribute( 'content' );
00182 
00183         if ( $countryContent === null )
00184         {
00185             if ( $requireUserCountry )
00186             {
00187                 eZDebug::writeError( "User country is not specified in object '" .
00188                                        $userObject->attribute( 'name' ) .
00189                                        "' of class '" .
00190                                        $userObject->attribute( 'class_name' ) . "'." ,
00191                                      'eZVATManager::getUserCountry' );
00192             }
00193             return null;
00194         }
00195 
00196         if ( is_object( $countryContent ) )
00197             $country = $countryContent->attribute( 'value' );
00198         elseif ( is_array( $countryContent ) )
00199         {
00200             if ( is_array( $countryContent['value'] ) )
00201             {
00202                 foreach ( $countryContent['value'] as $item )
00203                 {
00204                     $country = $item['Alpha2'];
00205                     break;
00206                 }
00207             }
00208             else
00209             {
00210                 $country = $countryContent['value'];
00211             }
00212         }
00213         else
00214         {
00215             if ( $requireUserCountry )
00216             {
00217                 eZDebug::writeError( "User country is not specified or specified incorrectly in object '" .
00218                                        $userObject->attribute( 'name' ) .
00219                                        "' of class '" .
00220                                        $userObject->attribute( 'class_name' ) . "'." ,
00221                                      'eZVATManager::getUserCountry' );
00222             }
00223             return null;
00224         }
00225 
00226         return $country;
00227     }
00228 
00229     /**
00230      * Set user's country.
00231      *
00232      * \public
00233      * \static
00234      */
00235     static function setUserCountry( $user, $country )
00236     {
00237         $userObject = $user->attribute( 'contentobject' );
00238         $requireUserCountry = eZVATManager::isUserCountryRequired();
00239         require_once( 'kernel/classes/ezvatmanager.php' );
00240         $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
00241         if ( $countryAttributeName === null )
00242         {
00243             return false;
00244         }
00245 
00246         $userDataMap = $userObject->attribute( 'data_map' );
00247         if ( !isset( $userDataMap[$countryAttributeName] ) )
00248         {
00249             if ( $requireUserCountry )
00250             {
00251                 eZDebug::writeError( "Cannot set user country: there is no attribute '$countryAttributeName' in object '" .
00252                                        $userObject->attribute( 'name' ) .
00253                                        "' of class '" .
00254                                        $userObject->attribute( 'class_name' ) . "'.",
00255                                      'eZVATManager::getUserCountry' );
00256             }
00257 
00258             return false;
00259         }
00260 
00261         eZDebug::writeNotice( sprintf( "Saving country '%s' for user '%s'",
00262                                        $country, $user->attribute( 'login' ) ) );
00263 
00264         $countryAttribute = $userDataMap[$countryAttributeName];
00265         $countryAttributeContent = $countryAttribute->content();
00266         if ( is_array( $countryAttributeContent ) )
00267             $countryAttributeContent['value'] = $country;
00268         elseif ( is_object( $countryAttributeContent ) )
00269             $countryAttributeContent->setAttribute( 'value', $country );
00270         // not sure that this line is needed since content is returned by reference
00271         $countryAttribute->setContent( $countryAttributeContent );
00272         $countryAttribute->store();
00273 
00274         return true;
00275     }
00276 
00277 
00278     /*!
00279      \return true if a VAT handler is specified in the ini setting, false otherwise.
00280      */
00281     static function isDynamicVatChargingEnabled()
00282     {
00283         if ( isset( $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] ) )
00284             return $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'];
00285 
00286         $enabled = is_object( eZVATManager::loadVATHandler() );
00287         $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] = $enabled;
00288         return $enabled;
00289     }
00290 
00291     /*!
00292      Load VAT handler (if specified).
00293 
00294      \private
00295      \static
00296      \return true if no handler specified,
00297              false if a handler specified but could not be loaded,
00298              handler object if handler specified and found.
00299      */
00300     static function loadVATHandler()
00301     {
00302         // FIXME: cache loaded handler.
00303 
00304         $shopINI = eZINI::instance( 'shop.ini' );
00305 
00306         if ( !$shopINI->hasVariable( 'VATSettings', 'Handler' ) )
00307             return true;
00308 
00309         $handlerName = $shopINI->variable( 'VATSettings', 'Handler' );
00310         $repositoryDirectories = $shopINI->variable( 'VATSettings', 'RepositoryDirectories' );
00311         $extensionDirectories = $shopINI->variable( 'VATSettings', 'ExtensionDirectories' );
00312 
00313         $baseDirectory = eZExtension::baseDirectory();
00314         foreach ( $extensionDirectories as $extensionDirectory )
00315         {
00316             $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/vathandlers';
00317             if ( file_exists( $extensionPath ) )
00318                 $repositoryDirectories[] = $extensionPath;
00319         }
00320 
00321         $foundHandler = false;
00322         foreach ( $repositoryDirectories as $repositoryDirectory )
00323         {
00324             $includeFile = "$repositoryDirectory/{$handlerName}vathandler.php";
00325 
00326             if ( file_exists( $includeFile ) )
00327             {
00328                 $foundHandler = true;
00329                 break;
00330             }
00331         }
00332 
00333         if ( !$foundHandler )
00334         {
00335             eZDebug::writeError( "VAT handler '$handlerName' not found, " .
00336                                    "searched in these directories: " .
00337                                    implode( ', ', $repositoryDirectories ),
00338                                  'eVATManager::loadVATHandler' );
00339             return false;
00340         }
00341 
00342         require_once( $includeFile );
00343         $className = $handlerName . 'VATHandler';
00344         return new $className;
00345     }
00346 }
00347 ?>