Files
kupshop/bundles/KupShop/GTMBundle/Utils/PriceComputer.php
2025-08-02 16:30:27 +02:00

92 lines
2.5 KiB
PHP

<?php
namespace KupShop\GTMBundle\Utils;
use Decimal;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\KupShopBundle\Util\Price\TotalPrice;
use KupShop\KupShopBundle\Wrapper\PriceWrapper;
class PriceComputer
{
/**
* @var CurrencyContext
*/
protected $currencyContext;
public function __construct(CurrencyContext $currencyContext)
{
$this->currencyContext = $currencyContext;
}
/**
* @param TotalPrice|Price|array|\Decimal $price
*
* @return string
*/
public function getPrice($price, $withVat = null, $round = true)
{
if ($withVat !== null) {
$sendWithVat = $withVat;
} else {
$sendWithVat = !self::sendPriceWithoutVAT();
}
if ($price instanceof TotalPrice) {
// Je to trochu uchylny to vytahovat do Decimal, pak z toho delat pole, pak zas objekt Price..
// ale je to kvuli prevodu do meny - zbytek tehle funkce
$price = $sendWithVat ? $price->getPriceWithVat() : $price->getPriceWithoutVat($round);
}
if ($price instanceof \Decimal) {
$price = [
'value_without_vat' => $price,
'value_with_vat' => $price,
'vat' => 0,
];
}
if (!is_object($price)) {
$price = new Price(toDecimal($price['value_without_vat']), $this->currencyContext->getActive(), $price['vat']);
}
if ($price instanceof PriceWrapper) {
$price = $price->getObject();
}
$price = $this->convertToCorrectPrice($price);
return ($sendWithVat ? $price->getPriceWithVat() : $price->getPriceWithoutVat($round))->printFloatValue(-2);
}
/**
* @return Price
*/
public function convertToCorrectPrice($price)
{
return \KupShop\KupShopBundle\Util\Price\PriceCalculator::convert($price, $this->getOutputCurrency());
}
/**
* @return \KupShop\I18nBundle\Entity\Currency
*/
public function getOutputCurrency()
{
$dbcfg = \Settings::getDefault();
if (empty($dbcfg->analytics['google_tag_manager']['currency'])) {
return $this->currencyContext->getActive();
}
return $this->currencyContext->getAll()[$dbcfg->analytics['google_tag_manager']['currency']];
}
public static function sendPriceWithoutVAT()
{
$dbcfg = \Settings::getDefault();
return ($dbcfg['analytics']['google_tag_manager']['withoutVAT'] ?? 'N') == 'Y';
}
}