86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\ZNZBundle\EventSubscriber\Ordering;
|
|
|
|
use External\ZNZBundle\Util\Product\ZNZProductUtil;
|
|
use KupShop\OrderingBundle\Event\CartEvent;
|
|
use KupShop\OrderingBundle\Exception\CartValidationException;
|
|
use KupShop\OrderingBundle\Util\VIES\DICValidator;
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
class CartSubscriber implements EventSubscriberInterface
|
|
{
|
|
public function __construct(
|
|
private ZNZProductUtil $productUtil,
|
|
private DICValidator $DICValidator,
|
|
) {
|
|
}
|
|
|
|
public static function getSubscribedEvents(): array
|
|
{
|
|
return [
|
|
CartEvent::CHECK_CART_ITEMS => [
|
|
['checkItemsQuantity', 200],
|
|
['prepareDIC', 255],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Opravim prefix u DIC.
|
|
*/
|
|
public function prepareDIC(CartEvent $event): void
|
|
{
|
|
$cart = $event->getCart();
|
|
$data = $cart->getData();
|
|
|
|
if (empty($cart->invoice['dic'])) {
|
|
return;
|
|
}
|
|
|
|
[$dicCountry, $dicNumber] = $this->DICValidator->getParsedVatNumber($cart->invoice['dic'], false);
|
|
if (empty($dicCountry) && !empty($data['dic_country'])) {
|
|
$cart->invoice['dic'] = $this->translateDICCountry($data['dic_country']).$dicNumber;
|
|
$cart->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Kontrola maximalniho poctu kusu v kosiku na jednu objednavku.
|
|
*
|
|
* V tabulce `znz_products_website` je sloupec `max_cart_quantity`, kterej rika, kolik kusu
|
|
* si muzu maximalne vlozit do kosiku na jednu objednavku.
|
|
*/
|
|
public function checkItemsQuantity(CartEvent $event): void
|
|
{
|
|
$purchaseState = $event->getCart()->getPurchaseState();
|
|
|
|
$products = $purchaseState->createProductCollection();
|
|
|
|
$this->productUtil->fetchZNZMaxCartQuantity($products);
|
|
|
|
foreach ($purchaseState->getProducts() as $item) {
|
|
if (!($maxQuantity = ($item->getProduct()->znz_max_cart_quantity ?? null))) {
|
|
continue;
|
|
}
|
|
|
|
if ($item->getPieces() > $maxQuantity) {
|
|
throw new CartValidationException(
|
|
sprintf(translate('error_max_cart_quantity', 'znz'), $item->getName(), $maxQuantity),
|
|
data: ['id' => 'outOfStock']
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function translateDICCountry(string $country): string
|
|
{
|
|
return match ($country) {
|
|
'GR' => 'EL',
|
|
default => $country,
|
|
};
|
|
}
|
|
}
|