version 2.0.2 from TER
[TYPO3CMS/Extensions/static_info_tables.git] / pi1 / class.tx_staticinfotables_pi1.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2004-2006 Stanislas Rolland <stanislas.rolland(arobas)fructifor.ca>
6 * All rights reserved
7 *
8 * This script is part of the Typo3 project. The Typo3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 *
29 * Class for handling static info tables: countries, and subdivisions, currencies, languages and taxes
30 *
31 * $Id$
32 *
33 * @author Stanislas Rolland <stanislas.rolland(arobas)fructifor.ca>
34 */
35
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 64: class tx_staticinfotables_pi1 extends tslib_pibase
42 * 92: function init()
43 * 131: function getStaticInfoName($type='COUNTRIES', $code, $country='', $countrySubdivision='', $self=0)
44 * 182: function buildStaticInfoSelector($type='COUNTRIES', $name='', $class='', $selected='', $country='', $submit=0, $id='', $title='', $addWhere='', $lang='', $local=FALSE)
45 * 250: function initCountries($param='UN', $lang='', $local=FALSE, $addWhere='')
46 * 300: function initCountrySubdivisions($param, $addWhere='')
47 * 345: function initCurrencies($addWhere='')
48 * 384: function initLanguages($addWhere='')
49 * 423: function optionsConstructor($names, $selected='')
50 * 444: function loadCurrencyInfo($currencyCode)
51 * 487: function formatAmount($amount, $displayCurrencyCode='')
52 * 511: function formatAddress($delim, $streetAddress, $city, $zip, $subdivisionCode='', $countryCode='')
53 * 559: function applyConsumerTaxes($amount, $taxClass=0, $shopCountryCode, $shopCountrySubdivisionCode, $buyerCountryCode, $buyerCountrySubdivisionCode, $EUThreshold=0)
54 * 678: function getCurrentLanguage()
55 *
56 * TOTAL FUNCTIONS: 13
57 * (This index is automatically created/updated by the extension "extdeveval")
58 *
59 */
60
61 require_once(PATH_tslib.'class.tslib_pibase.php');
62 require_once(t3lib_extMgm::extPath('static_info_tables').'class.tx_staticinfotables_div.php');
63
64 class tx_staticinfotables_pi1 extends tslib_pibase {
65
66 var $cObj; // The backReference to the mother cObj object set at call time
67 var $prefixId = 'tx_staticinfotables_pi1'; // Same as class name
68 var $scriptRelPath = 'pi1/class.tx_staticinfotables_pi1.php'; // Path to this script relative to the extension dir.
69 var $extKey = 'static_info_tables'; // The extension key.
70 var $conf = array();
71 var $currency; // default currency
72 var $currencyInfo = array();
73 var $defaultCountry;
74 var $defaultCountryZone;
75 var $defaultLanguage;
76 var $types = array('TERRITORIES', 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES', 'LANGUAGES', 'TAXES', 'SUBTAXES');
77 var $tables = array(
78 'TERRITORIES' => 'static_territories',
79 'COUNTRIES' => 'static_countries',
80 'SUBDIVISIONS' => 'static_country_zones',
81 'CURRENCIES' => 'static_currencies',
82 'LANGUAGES' => 'static_languages',
83 'TAXES' => 'static_taxes',
84 'SUBTAXES' => 'static_taxes'
85 );
86
87 /**
88 * Initializing the class: sets the language based on the TS configuration language property
89 *
90 * @return boolean Always returns true
91 */
92 function init() {
93 global $TSFE;
94
95 $this->tslib_pibase();
96 $this->conf = $TSFE->tmpl->setup['plugin.'][$this->prefixId.'.'];
97
98 //Get the default currency and make sure it does exist in table static_currencies
99 $this->currency = (trim($this->conf['currencyCode'])) ? trim($this->conf['currencyCode']) : 'EUR';
100 //If not set, we use the Euro
101 if (!$this->getStaticInfoName('CURRENCIES', $this->currency)) {
102 $this->currency = 'EUR';
103 }
104 $this->currencyInfo = $this->loadCurrencyInfo($this->currency);
105
106 $this->defaultCountry = trim($this->conf['countryCode']);
107 if (!$this->getStaticInfoName('COUNTRIES', $this->defaultCountry)) {
108 $this->defaultCountry = '';
109 }
110 $this->defaultCountryZone = trim($this->conf['countryZoneCode']);
111 if (!$this->getStaticInfoName('SUBDIVISIONS', $this->defaultCountryZone, $this->defaultCountry)) {
112 $this->defaultCountryZone = '';
113 }
114 $this->defaultLanguage = trim($this->conf['languageCode']);
115 if (!$this->getStaticInfoName('LANGUAGES', $this->defaultLanguage)) {
116 $this->defaultLanguage = '';
117 }
118 return true;
119 }
120
121 /**
122 * Getting the name of a country, country subdivision, currency, language, tax
123 *
124 * @param string Defines the type of entry of the requested name: 'TERRIRORIES', 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES', 'LANGUAGES', 'TAXES', 'SUBTAXES'
125 * @param string The ISO alpha-3 code of a territory, country or currency, or the ISO alpha-2 code of a language or the code of a country subdivision
126 * @param string The value of the country code (cn_iso_3) for which a name of type 'SUBDIVISIONS', 'TAXES' or 'SUBTAXES' is requested (meaningful only in these cases)
127 * @param string The value of the country subdivision code for which a name of type 'SUB_TAXES' is requested (meaningful only in this case)
128 * @param [type] $self: ...
129 * @return string The name of the object in the current language
130 */
131 function getStaticInfoName($type='COUNTRIES', $code, $country='', $countrySubdivision='', $self=0) {
132 global $TYPO3_DB, $TSFE, $TYPO3_CONF_VARS;
133
134 if (in_array($type, $this->types) && trim($code)) {
135 $table = $this->tables[$type];
136 $lang = $this->getCurrentLanguage();
137 $isoCode = array();
138 $isoCode[] = trim($code);
139 switch($type) {
140 case 'TERRITORIES':
141 case 'COUNTRIES':
142 case 'CURRENCIES':
143 $name = tx_staticinfotables_div::getTitleFromIsoCode($table, $isoCode, $lang);
144 break;
145 case 'SUBDIVISIONS':
146 case 'TAXES':
147 $isoCode[] = trim($country) ? trim($country) : $this->defaultCountry;
148 $name = tx_staticinfotables_div::getTitleFromIsoCode($table, $isoCode, $lang);
149 break;
150 case 'SUBTAXES':
151 $isoCode[] = trim($country) ? trim($country) : $this->defaultCountry;
152 $isoCode[] = trim($countrySubdivision) ? trim($countrySubdivision) : $this->defaultCountryZone;
153 $name = tx_staticinfotables_div::getTitleFromIsoCode($table, $isoCode, $lang);
154 break;
155 case 'LANGUAGES':
156 $isoCode = t3lib_div::trimExplode( '_', $code, 1);
157 $name = tx_staticinfotables_div::getTitleFromIsoCode($table, $isoCode, $lang, $self);
158 break;
159 }
160 return $name = $TSFE->csConv($name, $TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['charset']);
161 } else {
162 return FALSE;
163 }
164 }
165
166 /**
167 * Buils a HTML drop-down selector of countries, country subdivisions, currencies or languages
168 *
169 * @param string Defines the type of entries to be presented in the drop-down selector: 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES' or 'LANGUAGES'
170 * @param string A value for the name attribute of the <select> tag
171 * @param string A value for the class attribute of the <select> tag
172 * @param string The value of the code of the entry to be pre-selected in the drop-down selector: value of cn_iso_3, zn_code, cu_iso_3 or lg_iso_2
173 * @param string The value of the country code (cn_iso_3) for which a drop-down selector of type 'SUBDIVISIONS' is requested (meaningful only in this case)
174 * @param boolean/string If set to 1, an onchange attribute will be added to the <select> tag for immediate submit of the changed value; if set to other than 1, overrides the onchange script
175 * @param string A value for the id attribute of the <select> tag
176 * @param string A value for the title attribute of the <select> tag
177 * @param string A where clause for the records
178 * @param string language to be used
179 * @param boolean $local: If set, we are looking for the "local" title field
180 * @return string A set of HTML <select> and <option> tags
181 */
182 function buildStaticInfoSelector($type='COUNTRIES', $name='', $class='', $selected='', $country='', $submit=0, $id='', $title='', $addWhere='', $lang='', $local=FALSE) {
183
184 $selected = trim($selected);
185 $country = trim($country);
186 $nameAttribute = (trim($name)) ? 'name="'.htmlspecialchars(trim($name)).'" ' : '';
187 $classAttribute = (trim($class)) ? 'class="'.htmlspecialchars(trim($class)).'" ' : '';
188 $idAttribute = (trim($id)) ? 'id="'.htmlspecialchars(trim($id)).'" ' : '';
189 $titleAttribute = (trim($title)) ? 'title="'.htmlspecialchars(trim($title)).'" ' : '';
190 $onchangeAttribute = '';
191 if ($submit) {
192 if ($submit == 1) {
193 $onchangeAttribute = 'onchange='.t3lib_div::quoteJSvalue(str_replace('\'', '"', $this->conf['onChangeAttribute']));
194 } else {
195 $onchangeAttribute = 'onchange='.t3lib_div::quoteJSvalue(str_replace('\'', '"', $submit));
196 }
197 }
198 $selector = '<select size="1" '.$idAttribute.$nameAttribute.$titleAttribute.$classAttribute.$onchangeAttribute.'>'.chr(10);
199
200 switch($type) {
201 case 'COUNTRIES':
202 $names = $this->initCountries('ALL',$lang,$local,$addWhere);
203 $selected = ($selected ? $selected : $this->defaultCountry);
204 reset($names);
205 $selected = ($selected ? $selected : key($names));
206 break;
207 case 'SUBDIVISIONS':
208 $param = (trim($country) ? trim($country) : $this->defaultCountry);
209 $names = $this->initCountrySubdivisions($param,$addWhere);
210 if( $param == $this->defaultCountry ) {
211 $selected = ($selected ? $selected : $this->defaultCountryZone);
212 } else {
213 reset($names);
214 $selected = ($selected ? $selected : key($names));
215 }
216 break;
217 case 'CURRENCIES':
218 $names = $this->initCurrencies($addWhere);
219 $selected = ($selected ? $selected : $this->currency);
220 reset($names);
221 $selected = ($selected ? $selected : key($names));
222 break;
223 case 'LANGUAGES':
224 $names = $this->initLanguages($addWhere);
225 $selected = ($selected ? $selected : $this->defaultLanguage);
226 reset($names);
227 $selected = ($selected ? $selected : key($names));
228 break;
229 }
230 if( count($names) > 0 ) {
231 $selector .= $this->optionsConstructor($names, $selected);
232 $selector .= '</select>'.chr(10);
233 } else {
234 $selector = '';
235 }
236 return $selector;
237 }
238
239 /**
240 * Getting all countries into an array
241 * where the key is the ISO alpha-3 code of the country
242 * and where the value is the name of the country in the current language
243 *
244 * @param string It defines a selection: 'ALL', 'UN', 'EU'
245 * @param string language to be used
246 * @param boolean If set, we are looking for the "local" title field
247 * @param string additional WHERE clause
248 * @return array An array of names of countries
249 */
250 function initCountries($param='UN', $lang='', $local=FALSE, $addWhere='') {
251 global $TYPO3_DB, $TSFE, $TYPO3_CONF_VARS;
252
253 $table = $this->tables['COUNTRIES'];
254 $lang = $this->getCurrentLanguage();
255 $names = array();
256 $titleFields = tx_staticinfotables_div::getTCAlabelField($table, TRUE, $lang, $local);
257 $prefixedTitleFields = array();
258 $prefixedTitleFields[] = $table.'.cn_iso_3';
259 foreach ($titleFields as $titleField) {
260 $prefixedTitleFields[] = $table.'.'.$titleField;
261 }
262
263 array_unique($prefixedTitleFields);
264 $labelFields = implode(',', $prefixedTitleFields);
265 if ($param == 'UN') {
266 $where = 'cn_uno_member=1';
267 } elseif ($param == 'EU') {
268 $where = 'cn_eu_member=1';
269 } elseif ($param == 'ALL') {
270 $where = '1=1';
271 } else {
272 $where = '1=1';
273 }
274 $where .= ($addWhere ? ' AND '.$addWhere : '');
275
276 $res = $TYPO3_DB->exec_SELECTquery(
277 $labelFields,
278 $table,
279 $where.$TSFE->sys_page->enableFields($table)
280 );
281 while ($row = $TYPO3_DB->sql_fetch_assoc($res)) {
282 foreach ($titleFields as $titleField) {
283 if ($row[$titleField]) {
284 $names[$row['cn_iso_3']] = $TSFE->csConv($row[$titleField], $TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['charset']);
285 break;
286 }
287 }
288 }
289 uasort($names, 'strcoll');
290 return $names;
291 }
292
293 /**
294 * Getting all country subdivisions of a given country into an array
295 * where the key is the code of the subdivision
296 * and where the value is the name of the country subdivision in the current language
297 * You can leave the ISO code empty and use the additional WHERE clause instead of it.
298 *
299 * @param string The ISO alpha-3 code of a country
300 * @param string additional WHERE clause
301 * @return array An array of names of country subdivisions
302 */
303 function initCountrySubdivisions($param, $addWhere='') {
304 global $TYPO3_DB, $TSFE, $TYPO3_CONF_VARS;
305
306 $table = $this->tables['SUBDIVISIONS'];
307 if (strlen($param) == 3) {
308 $country = $param;
309 $where = 'zn_country_iso_3='.$TYPO3_DB->fullQuoteStr($country,$table);
310 } else {
311 $where = '1=1';
312 }
313 $where .= ($addWhere ? ' AND '.$addWhere : '');
314 $lang = $this->getCurrentLanguage();
315 $names = array();
316 $titleFields = tx_staticinfotables_div::getTCAlabelField($table, TRUE, $lang);
317 $prefixedTitleFields = array();
318 foreach ($titleFields as $titleField) {
319 $prefixedTitleFields[] = $table.'.'.$titleField;
320 }
321 $labelFields = implode(',', $prefixedTitleFields);
322 $res = $TYPO3_DB->exec_SELECTquery(
323 $table.'.zn_code,'.$labelFields,
324 $table,
325 $where.
326 $TSFE->sys_page->enableFields($table)
327 );
328 while ($row = $TYPO3_DB->sql_fetch_assoc($res)) {
329 foreach ($titleFields as $titleField) {
330 if ($row[$titleField]) {
331 $names[$row['zn_code']] = $TSFE->csConv($row[$titleField], $TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['charset']);
332 break;
333 }
334 }
335 }
336 uasort($names, 'strcoll');
337 return $names;
338 }
339
340 /**
341 * Getting all currencies into an array
342 * where the key is the ISO alpha-3 code of the currency
343 * and where the value are the name of the currency in the current language
344 *
345 * @param string additional WHERE clause
346 * @return array An array of names of currencies
347 */
348 function initCurrencies($addWhere='') {
349 global $TYPO3_DB, $TSFE, $TYPO3_CONF_VARS;
350
351 $where = '1=1'.($addWhere ? ' AND '.$addWhere : '');
352 $table = $this->tables['CURRENCIES'];
353 $lang = $this->getCurrentLanguage();
354 $names = array();
355 $titleFields = tx_staticinfotables_div::getTCAlabelField($table, TRUE, $lang);
356 $prefixedTitleFields = array();
357 foreach ($titleFields as $titleField) {
358 $prefixedTitleFields[] = $table.'.'.$titleField;
359 }
360 $labelFields = implode(',', $prefixedTitleFields);
361 $res = $TYPO3_DB->exec_SELECTquery(
362 $table.'.cu_iso_3,'.$labelFields,
363 $table,
364 $where.$TSFE->sys_page->enableFields($table)
365 );
366 while ($row = $TYPO3_DB->sql_fetch_assoc($res)) {
367 foreach ($titleFields as $titleField) {
368 if ($row[$titleField]) {
369 $names[$row['cu_iso_3']] = $TSFE->csConv($row[$titleField], $TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['charset']);
370 break;
371 }
372 }
373 }
374 uasort($names, 'strcoll');
375 return $names;
376 }
377
378 /**
379 * Getting all languages into an array
380 * where the key is the ISO alpha-2 code of the language
381 * and where the value are the name of the language in the current language
382 * Note: we exclude sacred and constructed languages
383 *
384 * @param string additional WHERE clause
385 * @return array An array of names of languages
386 */
387 function initLanguages($addWhere='') {
388 global $TYPO3_DB, $TSFE, $TYPO3_CONF_VARS;
389
390 $where = '1=1'.($addWhere ? ' AND '.$addWhere : '');
391 $table = $this->tables['LANGUAGES'];
392 $lang = $this->getCurrentLanguage();
393 $names = array();
394 $titleFields = tx_staticinfotables_div::getTCAlabelField($table, TRUE, $lang);
395 $prefixedTitleFields = array();
396 foreach ($titleFields as $titleField) {
397 $prefixedTitleFields[] = $table.'.'.$titleField;
398 }
399 $labelFields = implode(',', $prefixedTitleFields);
400 $res = $TYPO3_DB->exec_SELECTquery(
401 $table.'.lg_iso_2,'.$table.'.lg_country_iso_2,'.$labelFields,
402 $table,
403 $where.' AND lg_sacred = 0 AND lg_constructed = 0 '.
404 $TSFE->sys_page->enableFields($table)
405 );
406 while ($row = $TYPO3_DB->sql_fetch_assoc($res)) {
407 $code = $row['lg_iso_2'].($row['lg_country_iso_2']?'_'.$row['lg_country_iso_2']:'');
408 foreach ($titleFields as $titleField) {
409 if ($row[$titleField]) {
410 $names[$code] = $TSFE->csConv($row[$titleField], $TYPO3_CONF_VARS['EXTCONF'][$this->extKey]['charset']);
411 break;
412 }
413 }
414 }
415 uasort($names, 'strcoll');
416 return $names;
417 }
418
419 /**
420 * Builds a list of <option> tags
421 *
422 * @param array An array where the values will be the texts of an <option> tags and keys will be the values of the tags
423 * @param string A pre-selected value: if the value appears as a key, the <option> tag will bear a 'selected' attribute
424 * @return string A string of HTML <option> tags
425 */
426 function optionsConstructor($names, $selected='') {
427
428 $options = '';
429 reset($names);
430 while(list($value,$name)=each($names)) {
431 $options .= '<option value="'.htmlspecialchars($value).'"';
432 if( $selected == $value) {
433 $options .= ' selected="selected"';
434 }
435 $options .= '>'.htmlspecialchars($name).'</option>'.chr(10);
436 }
437
438 return $options;
439 }
440
441 /**
442 * Loading currency display parameters from Static Info Tables
443 *
444 * @param string An ISO alpha-3 currency code
445 * @return array An array of information regarding the currrency
446 */
447 function loadCurrencyInfo($currencyCode) {
448 global $TYPO3_DB;
449
450 // Fetching the currency record
451 $this->currencyInfo['cu_iso_3'] = trim($currencyCode);
452 $this->currencyInfo['cu_iso_3'] = ($this->currencyInfo['cu_iso_3']) ? $this->currencyInfo['cu_iso_3'] : $this->currency;
453 $res = $TYPO3_DB->exec_SELECTquery(
454 '*',
455 'static_currencies',
456 'cu_iso_3='.$TYPO3_DB->fullQuoteStr($this->currencyInfo['cu_iso_3'],'static_currencies')
457 );
458 // If not found we fetch the default currency!
459 if (!$TYPO3_DB->sql_num_rows($res)) {
460 $this->currencyInfo['cu_iso_3'] = $this->currency;
461 $res = $TYPO3_DB->exec_SELECTquery(
462 '*',
463 'static_currencies',
464 'cu_iso_3='.$TYPO3_DB->fullQuoteStr($this->currencyInfo['cu_iso_3'],'static_currencies')
465 );
466 }
467 $row = $TYPO3_DB->sql_fetch_assoc($res);
468
469 $this->currencyInfo['cu_name'] = $this->getStaticInfoName('CURRENCIES', $this->currencyInfo['cu_iso_3']);
470 $this->currencyInfo['cu_symbol_left'] = $row['cu_symbol_left'];
471 $this->currencyInfo['cu_symbol_right'] = $row['cu_symbol_right'];
472 $this->currencyInfo['cu_decimal_digits'] = $row['cu_decimal_digits'];
473 $this->currencyInfo['cu_decimal_point'] = $row['cu_decimal_point'];
474 $this->currencyInfo['cu_thousands_point'] = $row['cu_thousands_point'];
475
476 return $this->currencyInfo;
477 }
478
479 /**
480 * Formatting an amount in the currency loaded by loadCurrencyInfo($currencyCode)
481 *
482 * '' - the currency code is not displayed
483 * 'RIGHT' - the code is displayed at the right of the amount
484 * 'LEFT' - the code is displayed at the left of the amount
485 *
486 * @param float An amount to be displayed in the loaded currency
487 * @param string A flag specifying if the the currency code should be displayed:
488 * @return string The formated amounted
489 */
490 function formatAmount($amount, $displayCurrencyCode='') {
491
492 $formatedAmount = '';
493
494 if( $displayCurrencyCode == 'LEFT' ) { $formatedAmount .= $this->currencyInfo['cu_iso_3'].chr(32); }
495 $formatedAmount .= $this->currencyInfo['cu_symbol_left'];
496 $formatedAmount .= number_format($amount, intval($this->currencyInfo['cu_decimal_digits']), $this->currencyInfo['cu_decimal_point'], (($this->currencyInfo['cu_thousands_point'])?$this->currencyInfo['cu_thousands_point']:chr(32)));
497 $formatedAmount .= (($this->currencyInfo['cu_symbol_right'])?chr(32):'').$this->currencyInfo['cu_symbol_right'];
498 if( $displayCurrencyCode == 'RIGHT' ) { $formatedAmount .= chr(32).$this->currencyInfo['cu_iso_3']; }
499
500 return $formatedAmount;
501 }
502
503 /**
504 * Formatting an address in the format specified
505 *
506 * @param string A street address
507 * @param string A city
508 * @param string A country subdivision code (zn_code)
509 * @param string A ISO alpha-3 country code (cn_iso_3)
510 * @param string A zip code
511 * @param [type] $countryCode: ...
512 * @return string The formated address using the country address format (cn_address_format)
513 */
514 function formatAddress($delim, $streetAddress, $city, $zip, $subdivisionCode='', $countryCode='') {
515 global $TYPO3_DB;
516
517 $formatedAddress = '';
518
519 // Get country name
520 $countryName = $this->getStaticInfoName('COUNTRIES', (($countryCode)?$countryCode:$this->defaultCountry));
521 if (!$countryName) {
522 return $formatedAddress;
523 }
524
525 // Get address format
526 $res = $TYPO3_DB->exec_SELECTquery(
527 'cn_address_format',
528 'static_countries',
529 'cn_iso_3='.$TYPO3_DB->fullQuoteStr(trim((($countryCode)?$countryCode:$this->defaultCountry)),'static_countries')
530 );
531 $row = $TYPO3_DB->sql_fetch_assoc($res);
532 $addressFormat = $row['cn_address_format'];
533
534 // Get country subdivision name
535 $countrySubdivisionName = $this->getStaticInfoName('SUBDIVISIONS', (($subdivisionCode)?$subdivisionCode:$this->defaultCountryZone), (($countryCode)?$countryCode:$this->defaultCountry));
536
537 // Format the address
538 $formatedAddress = $this->conf['addressFormat.'][$addressFormat];
539 $formatedAddress = str_replace('%street', $streetAddress, $formatedAddress);
540 $formatedAddress = str_replace('%city', $city, $formatedAddress);
541 $formatedAddress = str_replace('%zip', $zip, $formatedAddress);
542 $formatedAddress = str_replace('%countrySubdivisionCode', $subdivisionCode, $formatedAddress);
543 $formatedAddress = str_replace('%countrySubdivisionName', $countrySubdivisionName, $formatedAddress);
544 $formatedAddress = str_replace('%countryName', strtoupper($countryName), $formatedAddress);
545 $formatedAddress = implode($delim, t3lib_div::trimExplode(';', $formatedAddress, 1));
546
547 return $formatedAddress;
548 }
549
550 /**
551 * Applying taxes to a given amount
552 *
553 * @param float An amount to which taxes should be applied
554 * @param integer The class of taxation of the product
555 * @param string The ISO alpha-3 code of the country of the selling shop
556 * @param string The country subdivision code of the region of the selling shop
557 * @param string The ISO alpha-3 code of the country of the buying consumer
558 * @param string The country subdivision code of the region of the buying consumer
559 * @param boolean Should be set if the shop has sales of goods beyond the regulatory threshold in the buyer's country (when both shop and buyer in EU)
560 * @return array An array of 4-plets of applied taxes: ('tx_name','tx_rate','tx_amount','tx_priority')
561 */
562 function applyConsumerTaxes($amount, $taxClass=0, $shopCountryCode, $shopCountrySubdivisionCode, $buyerCountryCode, $buyerCountrySubdivisionCode, $EUThreshold=0) {
563 global $TYPO3_DB;
564
565 $appliedTaxesIndex = 0;
566 $appliedTaxes = array();
567 $shopCountryCode = ($shopCountryCode) ? $shopCountryCode : $this->defaultCountry;
568 $buyerCountryCode = ($buyerCountryCode) ? $buyerCountryCode : $this->defaultCountry;
569
570 // Not taxable!
571 if (!$taxClass || !trim($shopCountryCode) || !trim($buyerCountryCode)) {
572 return $appliedTaxes;
573 }
574
575 // Get national taxes
576 if (trim($shopCountryCode) == trim($buyerCountryCode)) {
577 $res = $TYPO3_DB->exec_SELECTquery(
578 '*',
579 'static_taxes',
580 'tx_country_iso_3='.$TYPO3_DB->fullQuoteStr(trim($shopCountryCode),'static_taxes').
581 ' AND tx_scope="1"'.
582 ' AND (tx_class='.$TYPO3_DB->fullQuoteStr($taxClass,'static_taxes').' OR tx_class="3")'.
583 $this->enableFields('static_taxes')
584 );
585 while($row = $TYPO3_DB->sql_fetch_assoc($res)) {
586 $appliedTaxes[$appliedTaxesIndex] = array();
587 $appliedTaxes[$appliedTaxesIndex]['tx_name'] = $this->getStaticInfoName('TAXES', $row['tx_code'], trim($shopCountryCode));
588 $appliedTaxes[$appliedTaxesIndex]['tx_rate'] = doubleval($row['tx_rate']);
589 $appliedTaxes[$appliedTaxesIndex]['tx_priority'] = $row['tx_priority'];
590 $appliedTaxesIndex++;
591 }
592
593 // Get state or provincial taxes
594 if( trim($shopCountrySubdivisionCode) && trim($buyerCountrySubdivisionCode) && trim($shopCountrySubdivisionCode) == trim($buyerCountrySubdivisionCode) ) {
595 $res = $TYPO3_DB->exec_SELECTquery(
596 '*',
597 'static_taxes',
598 'tx_country_iso_3='.$TYPO3_DB->fullQuoteStr(trim($shopCountryCode),'static_taxes').
599 ' AND tx_zn_code='.$TYPO3_DB->fullQuoteStr(trim($shopCountrySubdivisionCode),'static_taxes').
600 ' AND tx_scope="2"'.
601 ' AND (tx_class='.$TYPO3_DB->fullQuoteStr($taxClass,'static_taxes').' OR tx_class="3")'.
602 $this->enableFields('static_taxes')
603 );
604 while($row = $TYPO3_DB->sql_fetch_assoc($res)) {
605 $appliedTaxes[$appliedTaxesIndex] = array();
606 $appliedTaxes[$appliedTaxesIndex]['tx_name'] = $this->getStaticInfoName('SUBTAXES', $row['tx_code'], trim($shopCountryCode), trim($shopCountrySubdivisionCode));
607 $appliedTaxes[$appliedTaxesIndex]['tx_rate'] = doubleval($row['tx_rate']);
608 $appliedTaxes[$appliedTaxesIndex]['tx_priority'] = $row['tx_priority'];
609 $appliedTaxesIndex++;
610 }
611 }
612 } else {
613 // Apply EU Internal Market rules for under threshold sales
614 $res = $TYPO3_DB->exec_SELECTquery(
615 'cn_eu_member',
616 'static_countries',
617 'cn_iso_3='.$TYPO3_DB->fullQuoteStr(trim($shopCountryCode),'static_countries')
618 );
619 $row = $TYPO3_DB->sql_fetch_assoc($res);
620 $shop_cn_eu_member = $row['cn_eu_member'];
621 if ($shop_cn_eu_member) {
622 $res = $TYPO3_DB->exec_SELECTquery(
623 'cn_eu_member',
624 'static_countries',
625 'cn_iso_3='.$TYPO3_DB->fullQuoteStr(trim($buyerCountryCode),'static_countries')
626 );
627 $row = $TYPO3_DB->sql_fetch_assoc($res);
628 $buyer_cn_eu_member = $row['cn_eu_member'];
629 if ($buyer_cn_eu_member) {
630 // Here we apply the rules of the European Union Internal Market
631 $taxCountryCode = trim($shopCountryCode);
632 if ($taxClass == '1' && $EUThreshold) {
633 $taxCountryCode = trim($buyerCountryCode);
634 }
635 $res = $TYPO3_DB->exec_SELECTquery(
636 '*',
637 'static_taxes',
638 'tx_country_iso_3='.$TYPO3_DB->fullQuoteStr($taxCountryCode,'static_taxes').
639 ' AND tx_scope="1"'.
640 ' AND (tx_class='.$TYPO3_DB->fullQuoteStr($taxClass,'static_taxes').' OR tx_class="3")'.
641 $this->enableFields('static_taxes')
642 );
643 while($row = $TYPO3_DB->sql_fetch_assoc($res)) {
644 $appliedTaxes[$appliedTaxesIndex] = array();
645 $appliedTaxes[$appliedTaxesIndex]['tx_name'] = $this->getStaticInfoName('TAXES', $row['tx_code'], trim($shopCountryCode));
646 $appliedTaxes[$appliedTaxesIndex]['tx_rate'] = doubleval($row['tx_rate']);
647 $appliedTaxes[$appliedTaxesIndex]['tx_priority'] = $row['tx_priority'];
648 $appliedTaxesIndex++;
649 }
650 }
651 }
652 }
653
654 // Apply rates
655 if( count($appliedTaxes) ) {
656 foreach ($appliedTaxes as $key => $row) {
657 $priority[$key] = $row['tx_priority'];
658 }
659 array_multisort($priority, SORT_ASC, $appliedTaxes);
660 $priority = $priority['0'];
661 $appliedTaxesAmount = $amount;
662 $baseAmount = $appliedTaxesAmount;
663 foreach ($appliedTaxes as $key => $row) {
664 if( $row['tx_priority'] > $priority ) {
665 $baseAmount = $appliedTaxesAmount;
666 $priority = $row['tx_priority'];
667 }
668 $taxedAmount = $row['tx_rate']*$baseAmount;
669 $appliedTaxes[$key]['tx_amount'] = round($taxedAmount, ceil(0 - log10($taxedAmount)) + $this->currencyInfo['cu_decimal_digits']);
670 $appliedTaxesAmount += $appliedTaxes[$key]['tx_amount'];
671 }
672 }
673 return $appliedTaxes;
674 }
675
676 /**
677 * Returns the current language as iso-2-alpha code
678 *
679 * @return string 'DE', 'EN', 'DK', ...
680 */
681 function getCurrentLanguage() {
682 global $TSFE, $TYPO3_DB;
683
684 $langCodeT3 = $TSFE->lang;
685 $csConvObj = $TSFE->csConvObj;
686
687 $res = $TYPO3_DB->exec_SELECTquery(
688 'lg_iso_2,lg_country_iso_2',
689 'static_languages',
690 'lg_typo3='.$TYPO3_DB->fullQuoteStr($langCodeT3,'static_languages')
691 );
692 while ($row = $TYPO3_DB->sql_fetch_assoc($res)) {
693 $lang = $row['lg_iso_2'].($row['lg_country_iso_2']?'_'.$row['lg_country_iso_2']:'');
694 }
695
696 return $lang ? $lang : $csConvObj->conv_case('utf-8',$langCodeT3,'toUpper');
697 }
698 }
699
700 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/static_info_tables/pi1/class.tx_staticinfotables_pi1.php']) {
701 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/static_info_tables/pi1/class.tx_staticinfotables_pi1.php']);
702 }
703
704 ?>