Files
kupshop/bundles/KupShop/KupShopBundle/Wrapper/PriceWrapper.php
2025-08-02 16:30:27 +02:00

187 lines
5.4 KiB
PHP

<?php
namespace KupShop\KupShopBundle\Wrapper;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\PriceLevelContext;
use KupShop\KupShopBundle\Template\ObjectWrapper;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\KupShopBundle\Util\Price\PriceLevelPrice;
use KupShop\KupShopBundle\Util\Price\ProductPrice;
/**
* Class PriceWrapper.
*/
class PriceWrapper extends ObjectWrapper
{
/** @var Price */
protected $object;
public static $objectType = Price::class;
public function field_value_with_vat()
{
return $this->round($this->field_value_with_vat_no_rounding());
}
public function field_value_without_vat()
{
// Zaokrouhluju alespon na 4 místa co má databáze. Jinak je cena bez DPH vždy nezaokrouhlená.
return $this->field_value_without_vat_no_rounding()->value(4);
}
public function field_price_with_vat()
{
return $this->field_value_with_vat();
}
public function field_price_without_vat()
{
return $this->field_value_without_vat();
}
public function field_vat()
{
return $this->object->getVat();
}
public function field_currency()
{
return $this->object->getCurrency()->getSymbol();
}
public function field_originalPrice(): ?PriceWrapper
{
if (method_exists($this->object, 'getOriginalPrice')) {
return PriceWrapper::wrap($this->object->getOriginalPrice());
}
return null;
}
public function field_value_with_vat_no_rounding()
{
return $this->convertToActiveCurrency($this->object->getPriceWithVat(false));
}
public function field_value_without_vat_no_rounding()
{
// Kvůli měnám musím nejprve cenu ve finální měně zaokrouhlit a pak teprve odečíst DPH
return $this->field_value_with_vat()->removeVat($this->field_vat());
}
protected function convertToActiveCurrency(\Decimal $value): \Decimal
{
$currencyContext = Contexts::get(CurrencyContext::class);
$currency = $currencyContext->getActive();
if ($this->object->getCurrency()->getId() != $currency->getId()) {
$converted = $value->mul($this->object->getCurrency()->getRate());
$converted = $converted->div($currency->getRate());
return $converted;
}
return $value;
}
protected function round(\Decimal $price): \Decimal
{
return roundPrice($price, null, 'DB', null, source: $this->object->getSource());
}
public function field_B2B_discount()
{
if ($this->object instanceof PriceLevelPrice) {
$priceLevelDiscount = $this->object->getPricelevelDiscount();
$originalPrice = $this->object->getOriginalPrice();
$discount = toDecimal($priceLevelDiscount['discount']);
switch ($priceLevelDiscount['unit'] ?? '') {
case 'perc':
if ($priceLevelDiscount['add'] == false && $originalPrice->getDiscount()) {
if (!$originalPrice->getDiscount()->lowerThan($discount)) {
$discount = \DecimalConstants::zero();
}
}
return $discount;
case 'price':
case 'final_price':
return $discount;
}
}
return \DecimalConstants::zero();
}
public function field_B2B_discount_unit()
{
if ($this->object instanceof PriceLevelPrice) {
$priceLevelDiscount = $this->object->getPricelevelDiscount();
switch ($priceLevelDiscount['unit'] ?? '') {
case 'perc':
return '%';
default:
return $this->object->getCurrency()->getSymbol();
}
}
return '';
}
public function field_B2B_discount_name()
{
$priceLevelContext = Contexts::get(PriceLevelContext::class);
if ($priceLevel = $priceLevelContext->getActive()) {
return $priceLevel->name;
}
return '';
}
public function field_B2B_price_without_vat()
{
return $this->field_value_without_vat();
}
public function field_B2B_price_original_vat()
{
if ($this->object instanceof PriceLevelPrice) {
return $this->convertToActiveCurrency($this->object->getOriginalPrice()->getPriceWithVat());
}
return null;
}
public function field_B2B_price_original()
{
if ($this->object instanceof PriceLevelPrice) {
return $this->convertToActiveCurrency($this->object->getOriginalPrice()->getPriceWithoutVat());
}
return null;
}
/** Cena bez DPH před slevou */
public function field_value_without_vat_without_discount(): ?\Decimal
{
if ($this->object instanceof ProductPrice) {
return $this->convertToActiveCurrency($this->object->getPriceWithoutDiscount()->getPriceWithoutVat());
}
return null;
}
/** Cena s DPH bez slevy */
public function field_value_with_vat_without_discount(): ?\Decimal
{
if ($this->object instanceof ProductPrice) {
return $this->convertToActiveCurrency($this->object->getPriceWithoutDiscount()->getPriceWithVat());
}
return null;
}
}