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