36 lines
1.2 KiB
PHP
36 lines
1.2 KiB
PHP
<?php
|
|
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\KupShopBundle\Util\Price\Price;
|
|
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
|
|
use KupShop\KupShopBundle\Util\Price\PriceUtil;
|
|
|
|
/**
|
|
* @param string $toCurrency
|
|
*
|
|
* @return Decimal|mixed|string
|
|
*/
|
|
function smarty_modifier_convert_price($price, $toCurrency = '')
|
|
{
|
|
/** @var CurrencyContext $currency */
|
|
$currencyContext = ServiceContainer::getService(CurrencyContext::class);
|
|
if (!($price instanceof Price)) {
|
|
if (is_array($price)) {
|
|
$priceUtil = ServiceContainer::getService(PriceUtil::class);
|
|
$price = $priceUtil->getFromPriceArray($price);
|
|
} else {
|
|
$price = new Price(toDecimal($price), $currencyContext->getActive(), 0);
|
|
}
|
|
}
|
|
|
|
if (array_key_exists($toCurrency, $currencyContext->getSupported())) {
|
|
$toCurrency = $currencyContext->getSupported()[$toCurrency];
|
|
$price = PriceCalculator::convert($price, $toCurrency);
|
|
|
|
return printPrice($price->getPriceWithVat(), ['currency' => $toCurrency]);
|
|
} else {
|
|
user_error('Modifier convert_price: Unsupported currency '.$toCurrency);
|
|
}
|
|
}
|