Files
kupshop/bundles/KupShop/QuantityDiscountBundle/Twig/Components/QuantityDiscounts/Discounts.php
2025-08-02 16:30:27 +02:00

113 lines
3.7 KiB
PHP

<?php
namespace KupShop\QuantityDiscountBundle\Twig\Components\QuantityDiscounts;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataTrait;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
use KupShop\QuantityDiscountBundle\Context\QuantityDiscountContext;
use KupShop\QuantityDiscountBundle\Query\QuantityDiscount;
use KupShop\QuantityDiscountBundle\Util\QuantityDiscountPrice;
use Query\Operator;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
#[AsTwigComponent(template: '@QuantityDiscount/components/QuantityDiscounts/Discounts.1.html.twig')]
#[Component(1, [
new Version(1, newJs: null),
])]
class Discounts extends BaseComponent
{
use ProductDataTrait;
public ?string $title = null;
protected array $discounts;
public function __construct(private readonly QuantityDiscountPrice $quantityDiscountPrice)
{
}
public function getActive(): bool|int
{
$quantityDiscountContext = Contexts::get(QuantityDiscountContext::class);
return $quantityDiscountContext->getActive();
}
public function getDiscounts(): array
{
if (!isset($this->discounts)) {
$this->discounts = [];
$qb = sqlQueryBuilder()
->select('pieces, discount, discount_type')
->from('products_quantity_discounts')
->where(Operator::equals(['id_product' => $this->id_product]))
->orderBy('pieces', 'ASC');
$orX = ['id_variation IS NULL'];
if ($this->id_variation) {
$orX[] = Operator::equals(['id_variation' => $this->id_variation]);
}
$qb->andWhere(Operator::orX($orX));
$qb->andWhere(QuantityDiscount::byGroup($this->getActive()));
$data = $qb->execute();
if ($data->rowCount() == 0) {
return $this->discounts;
}
$priceBase = $this->quantityDiscountPrice->getPrice($this->id_product, 1, $this->id_variation);
$unit = $this->product->unit['short_name'] ?? 'ks';
$this->discounts[1] = [
'pieces_from' => 1,
'pieces_to' => null,
'unit' => $unit,
'discount' => 0,
'discount_value' => 0,
'discount_type' => null,
'price' => $priceBase,
];
$lastItem = &$this->discounts[1];
foreach ($data as $discount) {
$price = $this->quantityDiscountPrice->getPrice($this->id_product, $discount['pieces'], $this->id_variation);
$this->discounts[$discount['pieces']] = [
'pieces_from' => $discount['pieces'],
'pieces_to' => null,
'unit' => $unit,
'discount' => $discount['discount'],
'discount_value' => PriceCalculator::sub($priceBase, $price),
'discount_type' => $discount['discount_type'],
'price' => $price,
];
$lastItem['pieces_to'] = $discount['pieces'] - 1;
$lastItem = &$this->discounts[$discount['pieces']];
}
}
return $this->discounts;
}
public function getShouldShowDiscounts(): bool
{
$shouldShow = false;
foreach ($this->getDiscounts() as $discount) {
if ((int) $discount['discount'] > 0) {
$shouldShow = true;
break;
}
}
return $shouldShow;
}
}