Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezvatmanager.php
Go to the documentation of this file.
1 <?php
17 class eZVATManager
18 {
26 static function getVAT( $object, $country )
27 {
28 // Load VAT handler.
29 if ( !is_object( $handler = eZVATManager::loadVATHandler() ) )
30 {
31 if ( $handler === true )
32 {
33 eZDebug::writeWarning( "No VAT handler specified but dynamic VAT charging is used." );
34 }
35
36 return null;
37 }
38
39 // Check if user country must be specified.
40 $requireUserCountry = eZVATManager::isUserCountryRequired();
41
42 // Determine user country if it's not specified
43 if ( $country === false )
44 $country = eZVATManager::getUserCountry();
45
46 if ( !$country && $requireUserCountry )
47 {
48 eZDebug::writeNotice( "User country is not specified." );
49 }
50
51 return $handler->getVatPercent( $object, $country );
52 }
53
60 static function isUserCountryRequired()
61 {
62 // Check if user country must be specified.
63 $requireUserCountry = true;
64 $shopINI = eZINI::instance( 'shop.ini' );
65 if ( $shopINI->hasVariable( 'VATSettings', 'RequireUserCountry' ) )
66 $requireUserCountry = ( $shopINI->variable( 'VATSettings', 'RequireUserCountry' ) == 'true' );
67 return $requireUserCountry;
68 }
69
76 static function getUserCountryAttributeName( $requireUserCountry )
77 {
78 $ini = eZINI::instance( 'shop.ini' );
79 if ( !$ini->hasVariable( 'VATSettings', 'UserCountryAttribute' ) )
80 {
81 if ( $requireUserCountry )
82 {
83 eZDebug::writeError( "Cannot find user country: please specify its attribute identifier " .
84 "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
85 __METHOD__ );
86 }
87 return null;
88 }
89
90 $countryAttributeName = $ini->variable( 'VATSettings', 'UserCountryAttribute' );
91 if ( !$countryAttributeName )
92 {
93 if ( $requireUserCountry )
94 {
95 eZDebug::writeError( "Cannot find user country: empty attribute name specified " .
96 "in the following setting: shop.ini.[VATSettings].UserCountryAttribute",
97 __METHOD__ );
98 }
99
100 return null;
101 }
102
103 return $countryAttributeName;
104 }
105
112 static function getUserCountry( $user = false, $considerPreferedCountry = true )
113 {
114 $requireUserCountry = eZVATManager::isUserCountryRequired();
115
116 // If current user has set his/her preferred country via the toolbar
117 if ( $considerPreferedCountry )
118 {
119 // return it
120 $country = eZShopFunctions::getPreferredUserCountry();
121 if ( $country )
122 {
123 eZDebug::writeDebug( "Applying user's preferred country <$country> while charging VAT" );
124 return $country;
125 }
126 }
127
128 // Otherwise fetch country saved in the user object.
129
130 if ( $user === false )
131 {
132 $user = eZUser::currentUser();
133 }
134
135 $userObject = $user->attribute( 'contentobject' );
136 $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
137
138 if ( $countryAttributeName === null )
139 return null;
140
141 $userDataMap = $userObject->attribute( 'data_map' );
142 if ( !isset( $userDataMap[$countryAttributeName] ) )
143 {
144 if ( $requireUserCountry )
145 {
146 eZDebug::writeError( "Cannot find user country: there is no attribute '$countryAttributeName' in object '" .
147 $userObject->attribute( 'name' ) .
148 "' of class '" .
149 $userObject->attribute( 'class_name' ) . "'.",
150 __METHOD__ );
151 }
152 return null;
153 }
154
155 $countryAttribute = $userDataMap[$countryAttributeName];
156 $countryContent = $countryAttribute->attribute( 'content' );
157
158 if ( $countryContent === null )
159 {
160 if ( $requireUserCountry )
161 {
162 eZDebug::writeError( "User country is not specified in object '" .
163 $userObject->attribute( 'name' ) .
164 "' of class '" .
165 $userObject->attribute( 'class_name' ) . "'." ,
166 __METHOD__ );
167 }
168 return null;
169 }
170
171 if ( is_object( $countryContent ) )
172 $country = $countryContent->attribute( 'value' );
173 elseif ( is_array( $countryContent ) )
174 {
175 if ( is_array( $countryContent['value'] ) )
176 {
177 foreach ( $countryContent['value'] as $item )
178 {
179 $country = $item['Alpha2'];
180 break;
181 }
182 }
183 else
184 {
185 $country = $countryContent['value'];
186 }
187 }
188 else
189 {
190 if ( $requireUserCountry )
191 {
192 eZDebug::writeError( "User country is not specified or specified incorrectly in object '" .
193 $userObject->attribute( 'name' ) .
194 "' of class '" .
195 $userObject->attribute( 'class_name' ) . "'." ,
196 __METHOD__ );
197 }
198 return null;
199 }
200
201 return $country;
202 }
203
210 static function setUserCountry( $user, $country )
211 {
212 $userObject = $user->attribute( 'contentobject' );
213 $requireUserCountry = eZVATManager::isUserCountryRequired();
214 $countryAttributeName = eZVATManager::getUserCountryAttributeName( $requireUserCountry );
215 if ( $countryAttributeName === null )
216 {
217 return false;
218 }
219
220 $userDataMap = $userObject->attribute( 'data_map' );
221 if ( !isset( $userDataMap[$countryAttributeName] ) )
222 {
223 if ( $requireUserCountry )
224 {
225 eZDebug::writeError( "Cannot set user country: there is no attribute '$countryAttributeName' in object '" .
226 $userObject->attribute( 'name' ) .
227 "' of class '" .
228 $userObject->attribute( 'class_name' ) . "'.",
229 __METHOD__ );
230 }
231
232 return false;
233 }
234
235 eZDebug::writeNotice( sprintf( "Saving country '%s' for user '%s'",
236 $country, $user->attribute( 'login' ) ) );
237
238 $countryAttribute = $userDataMap[$countryAttributeName];
239 $countryAttributeContent = $countryAttribute->content();
240 if ( is_array( $countryAttributeContent ) )
241 $countryAttributeContent['value'] = $country;
242 elseif ( is_object( $countryAttributeContent ) )
243 $countryAttributeContent->setAttribute( 'value', $country );
244 // not sure that this line is needed since content is returned by reference
245 $countryAttribute->setContent( $countryAttributeContent );
246 $countryAttribute->store();
247
248 return true;
249 }
250
251
255 static function isDynamicVatChargingEnabled()
256 {
257 if ( isset( $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] ) )
258 return $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'];
259
260 $enabled = is_object( eZVATManager::loadVATHandler() );
261 $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] = $enabled;
262 return $enabled;
263 }
264
274 static function loadVATHandler()
275 {
276 // FIXME: cache loaded handler.
277
278 $shopINI = eZINI::instance( 'shop.ini' );
279
280 if ( !$shopINI->hasVariable( 'VATSettings', 'Handler' ) )
281 return true;
282
283 $handlerName = $shopINI->variable( 'VATSettings', 'Handler' );
284 $repositoryDirectories = $shopINI->variable( 'VATSettings', 'RepositoryDirectories' );
285 $extensionDirectories = $shopINI->variable( 'VATSettings', 'ExtensionDirectories' );
286
287 $baseDirectory = eZExtension::baseDirectory();
288 foreach ( $extensionDirectories as $extensionDirectory )
289 {
290 $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/vathandlers';
291 if ( file_exists( $extensionPath ) )
292 $repositoryDirectories[] = $extensionPath;
293 }
294
295 $foundHandler = false;
296 foreach ( $repositoryDirectories as $repositoryDirectory )
297 {
298 $includeFile = "$repositoryDirectory/{$handlerName}vathandler.php";
299
300 if ( file_exists( $includeFile ) )
301 {
302 $foundHandler = true;
303 break;
304 }
305 }
306
307 if ( !$foundHandler )
308 {
309 eZDebug::writeError( "VAT handler '$handlerName' not found, " .
310 "searched in these directories: " .
311 implode( ', ', $repositoryDirectories ),
312 __METHOD__ );
313 return false;
314 }
315
316 $className = $handlerName . 'VATHandler';
317 return new $className;
318 }
319 }
320 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeDebug
static writeDebug( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:588
eZDebug\writeNotice
static writeNotice( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:459
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZExtension\baseDirectory
static baseDirectory(eZINI|null $siteINI=null)
Definition: ezextension.php:39
eZUser\currentUser
static currentUser()
Definition: ezuser.php:1716
eZVATManager
The class eZVATManager does.
Definition: ezvatmanager.php:18
eZVATManager\getUserCountry
static getUserCountry( $user=false, $considerPreferedCountry=true)
Definition: ezvatmanager.php:112
eZVATManager\getVAT
static getVAT( $object, $country)
Definition: ezvatmanager.php:26
eZVATManager\getUserCountryAttributeName
static getUserCountryAttributeName( $requireUserCountry)
Definition: ezvatmanager.php:76
eZVATManager\loadVATHandler
static loadVATHandler()
Definition: ezvatmanager.php:274
eZVATManager\isDynamicVatChargingEnabled
static isDynamicVatChargingEnabled()
Definition: ezvatmanager.php:255
eZVATManager\setUserCountry
static setUserCountry( $user, $country)
Definition: ezvatmanager.php:210
eZVATManager\isUserCountryRequired
static isUserCountryRequired()
Definition: ezvatmanager.php:60
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.