<?php
namespace Cogi\CogiLicenseManager\Subscriber;
use Cogi\CogiLicenseManager\Core\License\LicenseEntity;
use Cogi\CogiLicenseManager\Core\LicenseKey\LicenseKeyEntity;
use Cogi\CogiLicenseManager\Core\LicenseProduct\LicenseProductEntity;
use Cogi\CogiLicenseManager\Service\LicenseManagerService;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Product implements EventSubscriberInterface {
/**
* @var SalesChannelContext
*/
protected $salesChannelContext;
/**
* @var array
*/
protected $config;
/**
* @var EntityRepositoryInterface
*/
protected $licenseProductRepository;
/**
* @var EntityRepositoryInterface
*/
protected $licenseRepository;
/**
* @var EntityRepositoryInterface
*/
protected $licensePoolRepository;
/**
* @var LicenseManagerService
*/
protected LicenseManagerService $licenseService;
public function __construct(SystemConfigService $systemConfigService, EntityRepositoryInterface $licenseProductRepository, EntityRepositoryInterface $licenseRepository, EntityRepositoryInterface $licensePoolRepository, LicenseManagerService $licenseService) {
$this->config = $systemConfigService->getDomain('CogiLicenseManager.config');
$this->licenseProductRepository = $licenseProductRepository;
$this->licenseRepository = $licenseRepository;
$this->licensePoolRepository = $licensePoolRepository;
$this->licenseService = $licenseService;
}
public static function getSubscribedEvents(): array {
return [
ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded',
SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolved',
];
}
/**
* @param EntityLoadedEvent $event
*/
public function onProductLoaded(EntityLoadedEvent $event): void {
if (!($this->salesChannelContext instanceof SalesChannelContext)) {
return;
}
/** @var ProductEntity $product */
foreach ($event->getEntities() as $product) {
$criteria = new Criteria();
$criteria->addAssociation('pool');
$criteria->addFilter(new EqualsFilter('productId', $product->getId()));
$result = $this->licenseProductRepository->search($criteria, $this->salesChannelContext->getContext())->getEntities();
if (!($result->first() === null)) {
$product->addExtension("cogiLicenseManager", $result);
/** @var LicenseProductEntity $licenseProduct */
$licenseProduct = $result->first();
$stock = 0;
$freeLicenses = $this->licenseService->getFreeLicenses($licenseProduct->getPool()->getId(), $this->salesChannelContext->getContext(), 0);
/** @var LicenseKeyEntity $key */
foreach ($freeLicenses as $key) {
if ($key->isMultiKey()) {
if ($key->getMultiKeyLimit() > 0 && $key->getMultiKeyLimit() > $key->getMultiKeyUsed()){
$stock += ($key->getMultiKeyLimit() - $key->getMultiKeyUsed()) - $this->licenseService->getReservedCountMultiKey($key->id, $this->salesChannelContext->getContext());
}
}else{
$stock++;
}
}
if ($licenseProduct->getCount() > 1) {
if (($stock / $licenseProduct->getCount()) < 1) {
$stock = 0;
} else {
$i = ($stock / $licenseProduct->getCount());
$stock = round($i, 0, PHP_ROUND_HALF_DOWN);
if ($stock < 1) $stock = 1;
}
}
$product->setMaxPurchase($stock);
$product->setStock($stock);
if ($stock != $product->getStock()) {
$newStock = $this->licenseService->updateProductStock($product->getId(), $licenseProduct->getPool()->getId());
if ($newStock >= 0) {
$product->setStock($newStock);
$product->setMaxPurchase($newStock);
}
}
}
}
}
/**
* @param SalesChannelContextResolvedEvent $event
*/
public function onSalesChannelContextResolved(SalesChannelContextResolvedEvent $event): void {
$this->salesChannelContext = $event->getSalesChannelContext();
}
/**
* Gibt wieder ob ein Automatisches anpassen des Bestandes Aktiviert ist
* @return bool
*/
private function isAutoStockEnabled(): bool {
return $this->config['CogiLicenseManager.config.stockupdate'] ?? true;
}
}