Files
kupshop/bundles/KupShop/FeedGeneratorBundle/ContextProperties/PriceUtilsContextProperty.php
2025-08-02 16:30:27 +02:00

115 lines
3.5 KiB
PHP

<?php
namespace KupShop\FeedGeneratorBundle\ContextProperties;
use KupShop\FeedGeneratorBundle\Expressions\ExpressionObjectTrait;
use KupShop\FeedGeneratorBundle\Expressions\IExpressionObject;
use KupShop\FeedGeneratorBundle\Utils\IObjectInfo;
use KupShop\I18nBundle\Entity\Currency;
use KupShop\I18nBundle\Util\PriceConverter;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Contexts;
class PriceUtilsContextProperty implements IContextProperty, IExpressionObject, IObjectInfo
{
use ExpressionObjectTrait;
protected PriceConverter $priceConverter;
/**
* @private
*/
public static function getName(): string
{
return 'priceUtils';
}
/**
* Převede cenu (price) z jedné měny (fromCurrency) do druhé (toCurrency). Pokud daná měna na eshopu neexistuje, použije se aktuální kurz ČNB.
*
* @return float
*/
public function convertCurrency($price, $fromCurrency, $toCurrency)
{
$from = $this->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 <code>priceUtils.discountPrice(price.withVat, 10.5)</code>.
* Ukázka včetně naformátování ceny <code>price.format(priceUtils.discountPrice(price.withVat, 10.5))</code>
*/
public function discountPrice(float $price, float $discountPercentage): float
{
return \Decimal::create($price)->addDiscount($discountPercentage)->asFloat();
}
}