00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 class eZVATManager
00041 {
00042
00043
00044
00045
00046
00047
00048
00049 function getVAT( $object, $country )
00050 {
00051
00052 if ( !is_object( $handler = eZVATManager::loadVATHandler() ) )
00053 {
00054 if ( $handler === true )
00055 eZDebug::writeWarning( "No VAT handler specified but dynamic VAT charging is used." );
00056
00057 return null;
00058 }
00059
00060
00061 $requireUserCountry = eZVATManager::isUserCountryRequired();
00062
00063
00064 if ( $country === false )
00065 $country = eZVATManager::getUserCountry();
00066
00067 if ( !$country && $requireUserCountry )
00068 eZDebug::writeNotice( "User country is not specified." );
00069
00070 return $handler->getVatPercent( $object, $country );
00071 }
00072
00073
00074
00075
00076
00077
00078
00079 function isUserCountryRequired()
00080 {
00081
00082 $requireUserCountry = true;
00083 $shopINI =& eZINI::instance( 'shop.ini' );
00084 if ( $shopINI->hasVariable( 'VATSettings', 'RequireUserCountry' ) )
00085 $requireUserCountry = ( $shopINI->variable( 'VATSettings', 'RequireUserCountry' ) == 'true' );
00086 return $requireUserCountry;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095 function getUserCountryAttributeName( $requireUserCountry )
00096 {
00097 $ini =& eZINI::instance( 'shop.ini' );
00098 if ( !$ini->hasVariable( 'VATSettings', 'UserCountryAttribute' ) )
00099 {
00100 if ( $requireUserCountry )
00101 {
00102 eZDebug::writeError( "Cannot find user country: please specify its attribute identifier " .
00103 "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
00104 'getUserCountryAttributeName' );
00105 }
00106 return null;
00107 }
00108
00109 $countryAttributeName = $ini->variable( 'VATSettings', 'UserCountryAttribute' );
00110 if ( !$countryAttributeName )
00111 {
00112 if ( $requireUserCountry )
00113 {
00114 eZDebug::writeError( "Cannot find user country: empty attribute name specified " .
00115 "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
00116 'getUserCountryAttributeName' );
00117 }
00118
00119 return null;
00120 }
00121
00122 return $countryAttributeName;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131 function getUserCountry( $user = false, $considerPreferedCountry = true )
00132 {
00133 $requireUserCountry = eZVATManager::isUserCountryRequired();
00134
00135
00136 if ( $considerPreferedCountry )
00137 {
00138
00139 include_once( 'kernel/shop/classes/ezshopfunctions.php' );
00140 $country = eZShopFunctions::getPreferredUserCountry();
00141 if ( $country )
00142 {
00143 eZDebug::writeDebug( "Applying user's preferred country <$country> while charging VAT" );
00144 return $country;
00145 }
00146 }
00147
00148
00149
00150 if ( $user === false )
00151 {
00152 require_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00153 $user = eZUser::currentUser();
00154 }
00155
00156 $userObject = $user->attribute( 'contentobject' );
00157 $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
00158
00159 if ( $countryAttributeName === null )
00160 return null;
00161
00162 $userDataMap = $userObject->attribute( 'data_map' );
00163 if ( !isset( $userDataMap[$countryAttributeName] ) )
00164 {
00165 if ( $requireUserCountry )
00166 {
00167 eZDebug::writeError( "Cannot find user country: there is no attribute '$countryAttributeName' in object '" .
00168 $userObject->attribute( 'name' ) .
00169 "' of class '" .
00170 $userObject->attribute( 'class_name' ) . "'.",
00171 'eZVATManager::getUserCountry' );
00172 }
00173 return null;
00174 }
00175
00176 $countryAttribute = $userDataMap[$countryAttributeName];
00177 $countryContent = $countryAttribute->attribute( 'content' );
00178
00179 if ( $countryContent === null )
00180 {
00181 if ( $requireUserCountry )
00182 {
00183 eZDebug::writeError( "User country is not specified in object '" .
00184 $userObject->attribute( 'name' ) .
00185 "' of class '" .
00186 $userObject->attribute( 'class_name' ) . "'." ,
00187 'eZVATManager::getUserCountry' );
00188 }
00189 return null;
00190 }
00191
00192 if ( is_object( $countryContent ) )
00193 $country = $countryContent->attribute( 'value' );
00194 elseif ( is_array( $countryContent ) )
00195 {
00196 if ( is_array( $countryContent['value'] ) )
00197 {
00198 foreach ( $countryContent['value'] as $item )
00199 {
00200 $country = $item['Alpha2'];
00201 break;
00202 }
00203 }
00204 else
00205 {
00206 $country = $countryContent['value'];
00207 }
00208 }
00209 else
00210 {
00211 if ( $requireUserCountry )
00212 {
00213 eZDebug::writeError( "User country is not specified or specified incorrectly in object '" .
00214 $userObject->attribute( 'name' ) .
00215 "' of class '" .
00216 $userObject->attribute( 'class_name' ) . "'." ,
00217 'eZVATManager::getUserCountry' );
00218 }
00219 return null;
00220 }
00221
00222 return $country;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231 function setUserCountry( $user, $country )
00232 {
00233 $userObject = $user->attribute( 'contentobject' );
00234 $requireUserCountry = eZVATManager::isUserCountryRequired();
00235 require_once( 'kernel/classes/ezvatmanager.php' );
00236 $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
00237 if ( $countryAttributeName === null )
00238 return false;
00239
00240 $userDataMap = $userObject->attribute( 'data_map' );
00241 if ( !isset( $userDataMap[$countryAttributeName] ) )
00242 {
00243 if ( $requireUserCountry )
00244 {
00245 eZDebug::writeError( "Cannot set user country: there is no attribute '$countryAttributeName' in object '" .
00246 $userObject->attribute( 'name' ) .
00247 "' of class '" .
00248 $userObject->attribute( 'class_name' ) . "'.",
00249 'eZVATManager::getUserCountry' );
00250 }
00251
00252 return false;
00253 }
00254
00255 eZDebug::writeNotice( sprintf( "Saving country '%s' for user '%s'",
00256 $country, $user->attribute( 'login' ) ) );
00257
00258 $countryAttribute = $userDataMap[$countryAttributeName];
00259 $countryAttributeContent = $countryAttribute->content();
00260 if ( is_array( $countryAttributeContent ) )
00261 $countryAttributeContent['value'] = $country;
00262 elseif ( is_object( $countryAttributeContent ) )
00263 $countryAttributeContent->setAttribute( 'value', $country );
00264
00265 $countryAttribute->setContent( $countryAttributeContent );
00266 $countryAttribute->store();
00267
00268 return true;
00269 }
00270
00271
00272
00273
00274
00275 function isDynamicVatChargingEnabled()
00276 {
00277 if ( isset( $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] ) )
00278 return $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'];
00279
00280 $enabled = is_object( eZVATManager::loadVATHandler() );
00281 $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] = $enabled;
00282 return $enabled;
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 function loadVATHandler()
00295 {
00296
00297
00298 $shopINI =& eZINI::instance( 'shop.ini' );
00299
00300 if ( !$shopINI->hasVariable( 'VATSettings', 'Handler' ) )
00301 return true;
00302
00303 $handlerName = $shopINI->variable( 'VATSettings', 'Handler' );
00304 $repositoryDirectories = $shopINI->variable( 'VATSettings', 'RepositoryDirectories' );
00305 $extensionDirectories = $shopINI->variable( 'VATSettings', 'ExtensionDirectories' );
00306
00307 $baseDirectory = eZExtension::baseDirectory();
00308 foreach ( $extensionDirectories as $extensionDirectory )
00309 {
00310 $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/vathandlers';
00311 if ( file_exists( $extensionPath ) )
00312 $repositoryDirectories[] = $extensionPath;
00313 }
00314
00315 $foundHandler = false;
00316 foreach ( $repositoryDirectories as $repositoryDirectory )
00317 {
00318 $includeFile = "$repositoryDirectory/{$handlerName}vathandler.php";
00319
00320 if ( file_exists( $includeFile ) )
00321 {
00322 $foundHandler = true;
00323 break;
00324 }
00325 }
00326
00327 if ( !$foundHandler )
00328 {
00329 eZDebug::writeError( "VAT handler '$handlerName' not found, " .
00330 "searched in these directories: " .
00331 implode( ', ', $repositoryDirectories ),
00332 'eVATManager::loadVATHandler' );
00333 return false;
00334 }
00335
00336 require_once( $includeFile );
00337 $className = $handlerName . 'VATHandler';
00338 return new $className;
00339 }
00340 }
00341 ?>