57 lines
2.0 KiB
PHP
57 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\HannahBundle\Util\Purchase;
|
|
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use KupShop\KupShopBundle\Util\Price\TotalPrice;
|
|
|
|
class PurchaseUtil extends \KupShop\OrderingBundle\Util\Purchase\PurchaseUtil
|
|
{
|
|
public function getPurchaseStateSavings(): TotalPrice
|
|
{
|
|
// ceneni kosiku pres SAP je povoleno pouze pro CZK menu
|
|
if (Contexts::get(CurrencyContext::class)->getActiveId() !== 'CZK') {
|
|
return parent::getPurchaseStateSavings();
|
|
}
|
|
|
|
$savingsWithVat = \DecimalConstants::zero();
|
|
$savingsWithoutVat = \DecimalConstants::zero();
|
|
|
|
foreach ($this->getPurchaseState()->getProducts() as $item) {
|
|
if (!$item->getProduct() || !$item->getProduct()->getProductPrice()) {
|
|
continue;
|
|
}
|
|
|
|
$productPrice = $item->getProduct()->getProductPrice();
|
|
|
|
if (!($priceCommon = $item->getNote()['sap']['priceCommon'] ?? null)) {
|
|
$priceCommon = $productPrice->getPriceWithVatWithoutDiscount()->mul(toDecimal($item->getPieces()));
|
|
}
|
|
|
|
// make sure priceCommon is decimal
|
|
$priceCommon = toDecimal($priceCommon);
|
|
|
|
if ($priceCommon->isPositive()) {
|
|
$diffWithVat = $priceCommon->sub($item->getPrice()->getPriceWithVat());
|
|
if ($diffWithVat->isPositive()) {
|
|
$savingsWithVat = $savingsWithVat->add($diffWithVat);
|
|
}
|
|
|
|
$diffWithoutVat = $priceCommon->removeVat($item->getPrice()->getVat())->sub($item->getPrice()->getPriceWithoutVat());
|
|
if ($diffWithoutVat->isPositive()) {
|
|
$savingsWithoutVat = $savingsWithoutVat->add($diffWithoutVat);
|
|
}
|
|
}
|
|
}
|
|
|
|
return new TotalPrice(
|
|
$savingsWithVat,
|
|
$savingsWithoutVat,
|
|
$this->currencyContext->getActive()
|
|
);
|
|
}
|
|
}
|