64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\CykloSpecialityBundle\EventSubscriber;
|
|
|
|
use KupShop\KupShopBundle\Context\ContextManager;
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Event\CronEvent;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
class CronSubscriber implements EventSubscriberInterface
|
|
{
|
|
public function __construct(private ContextManager $contextManager)
|
|
{
|
|
}
|
|
|
|
public static function getSubscribedEvents(): array
|
|
{
|
|
return [
|
|
CronEvent::RUN_NORMAL => [
|
|
['recalculateDefaultPrices', 250],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function recalculateDefaultPrices(): void
|
|
{
|
|
if (!($priceListId = $this->getPriceListId())) {
|
|
return;
|
|
}
|
|
|
|
// prenest cenu od vychoziho ceniku k variantam
|
|
sqlQuery('UPDATE products_variations pv
|
|
JOIN pricelists_products pp ON pv.id = pp.id_variation AND pp.id_pricelist = '.$priceListId.'
|
|
SET pv.price = COALESCE(pp.price, pv.price);');
|
|
|
|
// nastavit produktu minimalni cenu z variant
|
|
sqlQuery('UPDATE products p
|
|
JOIN products_variations pv ON p.id = pv.id_product
|
|
SET p.price = COALESCE((SELECT MIN(price) FROM products_variations WHERE id_product = p.id), p.price);');
|
|
}
|
|
|
|
private function getPriceListId(): ?int
|
|
{
|
|
$priceListId = $this->contextManager->activateContexts([CurrencyContext::class => 'CZK'], function () {
|
|
Contexts::get(\KupShop\PricelistBundle\Context\PricelistContext::class)->clearCache();
|
|
|
|
$priceListId = Contexts::get(\KupShop\PricelistBundle\Context\PricelistContext::class)->getActiveId();
|
|
|
|
Contexts::get(\KupShop\PricelistBundle\Context\PricelistContext::class)->forceEmpty();
|
|
|
|
return $priceListId;
|
|
});
|
|
|
|
if ($priceListId) {
|
|
return (int) $priceListId;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|