/kernel/classes/ezvatmanager.php

https://github.com/GunioRobot/ezpublish · PHP · 320 lines · 231 code · 31 blank · 58 comment · 23 complexity · c65cfa3629480076b3b90426de243a0c MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZVATManager class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZVATManager ezvatmanager.php
  12. \brief The class eZVATManager does
  13. */
  14. class eZVATManager
  15. {
  16. /**
  17. * Get percentage of VAT type corresponding to the given product and country the user is from.
  18. *
  19. * \return Percentage, or null on error.
  20. * \public
  21. * \static
  22. */
  23. static function getVAT( $object, $country )
  24. {
  25. // Load VAT handler.
  26. if ( !is_object( $handler = eZVATManager::loadVATHandler() ) )
  27. {
  28. if ( $handler === true )
  29. {
  30. eZDebug::writeWarning( "No VAT handler specified but dynamic VAT charging is used." );
  31. }
  32. return null;
  33. }
  34. // Check if user country must be specified.
  35. $requireUserCountry = eZVATManager::isUserCountryRequired();
  36. // Determine user country if it's not specified
  37. if ( $country === false )
  38. $country = eZVATManager::getUserCountry();
  39. if ( !$country && $requireUserCountry )
  40. {
  41. eZDebug::writeNotice( "User country is not specified." );
  42. }
  43. return $handler->getVatPercent( $object, $country );
  44. }
  45. /**
  46. * Check if users must have country specified.
  47. *
  48. * \public
  49. * \static
  50. */
  51. static function isUserCountryRequired()
  52. {
  53. // Check if user country must be specified.
  54. $requireUserCountry = true;
  55. $shopINI = eZINI::instance( 'shop.ini' );
  56. if ( $shopINI->hasVariable( 'VATSettings', 'RequireUserCountry' ) )
  57. $requireUserCountry = ( $shopINI->variable( 'VATSettings', 'RequireUserCountry' ) == 'true' );
  58. return $requireUserCountry;
  59. }
  60. /**
  61. * Determine name of content attribute that contains user's country.
  62. *
  63. * \private
  64. * \static
  65. */
  66. static function getUserCountryAttributeName( $requireUserCountry )
  67. {
  68. $ini = eZINI::instance( 'shop.ini' );
  69. if ( !$ini->hasVariable( 'VATSettings', 'UserCountryAttribute' ) )
  70. {
  71. if ( $requireUserCountry )
  72. {
  73. eZDebug::writeError( "Cannot find user country: please specify its attribute identifier " .
  74. "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
  75. __METHOD__ );
  76. }
  77. return null;
  78. }
  79. $countryAttributeName = $ini->variable( 'VATSettings', 'UserCountryAttribute' );
  80. if ( !$countryAttributeName )
  81. {
  82. if ( $requireUserCountry )
  83. {
  84. eZDebug::writeError( "Cannot find user country: empty attribute name specified " .
  85. "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
  86. __METHOD__ );
  87. }
  88. return null;
  89. }
  90. return $countryAttributeName;
  91. }
  92. /**
  93. * Determine user's country.
  94. *
  95. * \public
  96. * \static
  97. */
  98. static function getUserCountry( $user = false, $considerPreferedCountry = true )
  99. {
  100. $requireUserCountry = eZVATManager::isUserCountryRequired();
  101. // If current user has set his/her preferred country via the toolbar
  102. if ( $considerPreferedCountry )
  103. {
  104. // return it
  105. $country = eZShopFunctions::getPreferredUserCountry();
  106. if ( $country )
  107. {
  108. eZDebug::writeDebug( "Applying user's preferred country <$country> while charging VAT" );
  109. return $country;
  110. }
  111. }
  112. // Otherwise fetch country saved in the user object.
  113. if ( $user === false )
  114. {
  115. $user = eZUser::currentUser();
  116. }
  117. $userObject = $user->attribute( 'contentobject' );
  118. $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
  119. if ( $countryAttributeName === null )
  120. return null;
  121. $userDataMap = $userObject->attribute( 'data_map' );
  122. if ( !isset( $userDataMap[$countryAttributeName] ) )
  123. {
  124. if ( $requireUserCountry )
  125. {
  126. eZDebug::writeError( "Cannot find user country: there is no attribute '$countryAttributeName' in object '" .
  127. $userObject->attribute( 'name' ) .
  128. "' of class '" .
  129. $userObject->attribute( 'class_name' ) . "'.",
  130. __METHOD__ );
  131. }
  132. return null;
  133. }
  134. $countryAttribute = $userDataMap[$countryAttributeName];
  135. $countryContent = $countryAttribute->attribute( 'content' );
  136. if ( $countryContent === null )
  137. {
  138. if ( $requireUserCountry )
  139. {
  140. eZDebug::writeError( "User country is not specified in object '" .
  141. $userObject->attribute( 'name' ) .
  142. "' of class '" .
  143. $userObject->attribute( 'class_name' ) . "'." ,
  144. __METHOD__ );
  145. }
  146. return null;
  147. }
  148. if ( is_object( $countryContent ) )
  149. $country = $countryContent->attribute( 'value' );
  150. elseif ( is_array( $countryContent ) )
  151. {
  152. if ( is_array( $countryContent['value'] ) )
  153. {
  154. foreach ( $countryContent['value'] as $item )
  155. {
  156. $country = $item['Alpha2'];
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. $country = $countryContent['value'];
  163. }
  164. }
  165. else
  166. {
  167. if ( $requireUserCountry )
  168. {
  169. eZDebug::writeError( "User country is not specified or specified incorrectly in object '" .
  170. $userObject->attribute( 'name' ) .
  171. "' of class '" .
  172. $userObject->attribute( 'class_name' ) . "'." ,
  173. __METHOD__ );
  174. }
  175. return null;
  176. }
  177. return $country;
  178. }
  179. /**
  180. * Set user's country.
  181. *
  182. * \public
  183. * \static
  184. */
  185. static function setUserCountry( $user, $country )
  186. {
  187. $userObject = $user->attribute( 'contentobject' );
  188. $requireUserCountry = eZVATManager::isUserCountryRequired();
  189. $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
  190. if ( $countryAttributeName === null )
  191. {
  192. return false;
  193. }
  194. $userDataMap = $userObject->attribute( 'data_map' );
  195. if ( !isset( $userDataMap[$countryAttributeName] ) )
  196. {
  197. if ( $requireUserCountry )
  198. {
  199. eZDebug::writeError( "Cannot set user country: there is no attribute '$countryAttributeName' in object '" .
  200. $userObject->attribute( 'name' ) .
  201. "' of class '" .
  202. $userObject->attribute( 'class_name' ) . "'.",
  203. __METHOD__ );
  204. }
  205. return false;
  206. }
  207. eZDebug::writeNotice( sprintf( "Saving country '%s' for user '%s'",
  208. $country, $user->attribute( 'login' ) ) );
  209. $countryAttribute = $userDataMap[$countryAttributeName];
  210. $countryAttributeContent = $countryAttribute->content();
  211. if ( is_array( $countryAttributeContent ) )
  212. $countryAttributeContent['value'] = $country;
  213. elseif ( is_object( $countryAttributeContent ) )
  214. $countryAttributeContent->setAttribute( 'value', $country );
  215. // not sure that this line is needed since content is returned by reference
  216. $countryAttribute->setContent( $countryAttributeContent );
  217. $countryAttribute->store();
  218. return true;
  219. }
  220. /*!
  221. \return true if a VAT handler is specified in the ini setting, false otherwise.
  222. */
  223. static function isDynamicVatChargingEnabled()
  224. {
  225. if ( isset( $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] ) )
  226. return $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'];
  227. $enabled = is_object( eZVATManager::loadVATHandler() );
  228. $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] = $enabled;
  229. return $enabled;
  230. }
  231. /*!
  232. Load VAT handler (if specified).
  233. \private
  234. \static
  235. \return true if no handler specified,
  236. false if a handler specified but could not be loaded,
  237. handler object if handler specified and found.
  238. */
  239. static function loadVATHandler()
  240. {
  241. // FIXME: cache loaded handler.
  242. $shopINI = eZINI::instance( 'shop.ini' );
  243. if ( !$shopINI->hasVariable( 'VATSettings', 'Handler' ) )
  244. return true;
  245. $handlerName = $shopINI->variable( 'VATSettings', 'Handler' );
  246. $repositoryDirectories = $shopINI->variable( 'VATSettings', 'RepositoryDirectories' );
  247. $extensionDirectories = $shopINI->variable( 'VATSettings', 'ExtensionDirectories' );
  248. $baseDirectory = eZExtension::baseDirectory();
  249. foreach ( $extensionDirectories as $extensionDirectory )
  250. {
  251. $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/vathandlers';
  252. if ( file_exists( $extensionPath ) )
  253. $repositoryDirectories[] = $extensionPath;
  254. }
  255. $foundHandler = false;
  256. foreach ( $repositoryDirectories as $repositoryDirectory )
  257. {
  258. $includeFile = "$repositoryDirectory/{$handlerName}vathandler.php";
  259. if ( file_exists( $includeFile ) )
  260. {
  261. $foundHandler = true;
  262. break;
  263. }
  264. }
  265. if ( !$foundHandler )
  266. {
  267. eZDebug::writeError( "VAT handler '$handlerName' not found, " .
  268. "searched in these directories: " .
  269. implode( ', ', $repositoryDirectories ),
  270. __METHOD__ );
  271. return false;
  272. }
  273. $className = $handlerName . 'VATHandler';
  274. return new $className;
  275. }
  276. }
  277. ?>