getCurrency($fromCurrency);
$to = $this->getCurrency($toCurrency);
return $this->priceConverter->convert($from, $to, $price)->asFloat();
}
protected function getCurrency($id): Currency
{
$currency = $this->priceConverter->getCurrency($id);
if (!$currency) {
static $currencies = [];
if (!array_key_exists($id, $currencies)) {
// Když nemám měnu na eshopu, vyrobím si dočasnou s informacemi z ČNB
/** @var Currency $currency */
$currency = (clone Contexts::get(CurrencyContext::class)->getActive());
$currency->setId($id);
$currency->setName($id);
$currency->setRate(\CNB::getCurrency($id));
$currencies[$id] = $currency;
}
$currency = $currencies[$id];
}
return $currency;
}
/**
* @private
*
* @required
*/
public function setPriceConverter(PriceConverter $priceConverter): PriceUtilsContextProperty
{
$this->priceConverter = $priceConverter;
return $this;
}
/**
* Zaokrouhlí cenu (price) podle nastavení měny (currency) na eshopu. Pokud se měna nespecifikuje, použije se aktuální měna feedu.
*
* @return float
*/
public function roundCurrency($price, $currency = null)
{
if ($currency) {
$currency = $this->getCurrency($currency);
}
return roundPrice($price, -1, 'DB', null, $currency)->asFloat();
}
/**
* Naformátuje cenu (price) podle nastavení dané měny (currency) na eshopu včetně symboly měny. Pokud se měna nespecifikuje, použije se aktuální měna feedu.
*
* @return string
*/
public function formatCurrency($price, $currency = null)
{
if ($currency) {
$currency = $this->getCurrency($currency);
}
return printPrice($price, ['currency' => $currency]);
}
/**
* Vrátí cenu (float) s přidanou slevou.
*
* Např. pro získání ceny po slevě 10,5% s DPH lze využít priceUtils.discountPrice(price.withVat, 10.5).
* Ukázka včetně naformátování ceny price.format(priceUtils.discountPrice(price.withVat, 10.5))
*/
public function discountPrice(float $price, float $discountPercentage): float
{
return \Decimal::create($price)->addDiscount($discountPercentage)->asFloat();
}
}