custom/static-plugins/AlphaplanPriceConnector/src/Subscriber/ApPriceSubscriber.php line 66

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace AlphaplanPriceConnector\Subscriber;
  3. use AlphaplanPriceConnector\DataAbstractionLayer\ApPriceHelper;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  6. use Shopware\Core\Content\Product\Aggregate\ProductPrice\ProductPriceCollection;
  7. use Shopware\Core\Content\Product\Aggregate\ProductPrice\ProductPriceEntity;
  8. use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
  9. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. class ApPriceSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var ContainerInterface
  22.      */
  23.     protected ContainerInterface $container;
  24.     /**
  25.      * @var SystemConfigService
  26.      */
  27.     private SystemConfigService $systemConfigService;
  28.     /**
  29.      * @var ApPriceHelper
  30.      */
  31.     private ApPriceHelper $apPriceHelper;
  32.     private QuantityPriceCalculator $priceCalculator;
  33.     /**
  34.      * @var AbstractProductPriceCalculator
  35.      */
  36.     private $calculator;
  37.     public function __construct(
  38.         ContainerInterface $container,
  39.         QuantityPriceCalculator $priceCalculator,
  40.         AbstractProductPriceCalculator $calculator,
  41.         SystemConfigService $systemConfigService
  42.     )
  43.     {
  44.         $this->container $container;
  45.         $this->priceCalculator $priceCalculator;
  46.         $this->systemConfigService $systemConfigService;
  47.         $this->calculator $calculator;
  48.         $this->apPriceHelper = new ApPriceHelper($this->container->get(Connection::class), $container);
  49.     }
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             'sales_channel.product.loaded' => 'loaded',
  54.         ];
  55.     }
  56.     public function loaded(SalesChannelEntityLoadedEvent $event): void
  57.     {
  58.         /** @var SalesChannelProductEntity $product */
  59.         foreach ($event->getEntities() as $product) {
  60.             $this->calculateApPrices($event->getSalesChannelContext(), $product);
  61.         }
  62.         $this->calculator->calculate($event->getEntities(), $event->getSalesChannelContext());
  63.         /** @var SalesChannelProductEntity $product */
  64.         foreach ($event->getEntities() as $product) {
  65.             $product->setCalculatedMaxPurchase(
  66.                 $this->calculateMaxPurchase($product$event->getSalesChannelContext()->getSalesChannel()->getId())
  67.             );
  68.             $this->markAsNew($event->getSalesChannelContext(), $product);
  69.         }
  70.     }
  71.     private function calculateApPrices(SalesChannelContext $contextSalesChannelProductEntity $product): void
  72.     {
  73.         $custId $context->getCustomer() ? $context->getCustomer()->getId() : '0';
  74.         $priceSessionIdentifier =  $this->apPriceHelper->apGetPriceSessionIdentifier($context$custId$product->getId());
  75.         if(!isset($_SESSION['ap-tmp-prices'][$priceSessionIdentifier])){
  76.             $productApPrice $this->apPriceHelper->getApPrice($product->getId(), $context$context->getCustomer());
  77.             $_SESSION['ap-tmp-prices'][$priceSessionIdentifier] = $productApPrice;
  78.         }else{
  79.             $productApPrice $_SESSION['ap-tmp-prices'][$priceSessionIdentifier];
  80.         }
  81.         $validRule $this->apPriceHelper->getApAlwaysValidRule($context);
  82.         if (!empty($productApPrice) && $validRule) {
  83.             $newApProductPrices = array();
  84.             $newApProductPriceCollection = new ProductPriceCollection();
  85.             $apTax $this->apPriceHelper->getApTax($context$productApPrice[0]['ArtikelID']);
  86.             if (!empty($apTax)) {
  87.                 $product->setTax($context->getTaxRules()->get($apTax['taxId']));
  88.             }else{
  89.                 $apStandardTax $this->apPriceHelper->getStandardApTax($context$productApPrice[0]['ArtikelID']);
  90.                 if (!empty($apStandardTax)) {
  91.                     $product->setTax($context->getTaxRules()->get($apStandardTax['taxId']));
  92.                 }
  93.             }
  94.             foreach ($productApPrice as $index => $apPrice) {
  95.                 $productApPriceVENettoBase $productApPriceVENetto = (float)$apPrice['PENetto'];
  96.                 $productApDiscount $this->apPriceHelper->getApDiscount($apPrice$product->getId(), $context$context->getCustomer());
  97.                 $apCustomFields $product->getCustomFields();
  98.                 if (!empty($productApDiscount)) {
  99.                     $ApNewDiscountedPrice $this->apPriceHelper->getApNewDiscountedPrice($productApPriceVENetto$productApDiscount$apCustomFields);
  100.                     $productApPriceVENetto $ApNewDiscountedPrice['apPrice'];
  101.                     $newApProductDiscount = new Price($context->getCurrency()->getId(),
  102.                         $productApPriceVENettoBase,
  103.                         $productApPriceVENettoBase + (($productApPriceVENettoBase $product->getTax()->getTaxRate()) / 100),
  104.                         $product->getPrice()->get($context->getCurrency()->getId())->getLinked(),
  105.                         $product->getPrice()->get($context->getCurrency()->getId())->getListPrice());
  106.                     $product->setCustomFields($ApNewDiscountedPrice['apCustomFields']);
  107.                 } else {
  108.                     $newApProductDiscount $product->getPrice()->get($context->getCurrency()->getId())->getListPrice();
  109.                 }
  110.                 $newApProductPrice = new Price($context->getCurrency()->getId(),
  111.                     $productApPriceVENetto,
  112.                     $productApPriceVENetto + (($productApPriceVENetto $product->getTax()->getTaxRate()) / 100),
  113.                     $product->getPrice()->get($context->getCurrency()->getId())->getLinked(),
  114.                     $newApProductDiscount);
  115.                 $quantityStart $apPrice['PEAnzahl'] ? $apPrice['PEAnzahl'] : 1;
  116.                 $quantityEnd = isset($productApPrice[$index 1]) ? $productApPrice[$index 1]['PEAnzahl'] - null;
  117.                 $newApProductPriceEntity = (new ProductPriceEntity())
  118.                     ->assign(
  119.                         [
  120.                             'id' => Uuid::randomHex(),
  121.                             'productId' => $product->getId(),
  122.                             'quantityStart' => (int)$quantityStart,
  123.                             'quantityEnd' => $quantityEnd,
  124.                             'ruleId' => $validRule->getRuleId(),
  125.                             'price' => new PriceCollection([$newApProductPrice]),
  126.                         ]
  127.                     );
  128.                 $newApProductPrices[] = $newApProductPrice;
  129.                 $newApProductPriceCollection->add($newApProductPriceEntity);
  130.             }
  131.             $newApPriceCollection = new PriceCollection();
  132.             $newApPriceCollection->add($newApProductPrices[0]);
  133.             $product->setPrice($newApPriceCollection);
  134.             if (count($newApProductPrices) > 1) {
  135.                 $product->setPrices($newApProductPriceCollection);
  136.             } else {
  137.                 $product->setPrices(new ProductPriceCollection());
  138.             }
  139.         }else{
  140.             $product->setAvailable(false);
  141.             $product->setActive(false);
  142.             $product->setIsCloseout(true);
  143.         }
  144.     }
  145.     private function calculateMaxPurchase(SalesChannelProductEntity $productstring $salesChannelId): int
  146.     {
  147.         $fallback $this->systemConfigService->getInt('core.cart.maxQuantity'$salesChannelId);
  148.         $max $product->getMaxPurchase() ?? $fallback;
  149.         if ($product->getIsCloseout() && $product->getAvailableStock() < $max) {
  150.             $max = (int) $product->getAvailableStock();
  151.         }
  152.         $max floor($max / ($product->getPurchaseSteps() ?? 1)) * $product->getPurchaseSteps();
  153.         return (int) max($max0);
  154.     }
  155.     private function markAsNew(SalesChannelContext $contextSalesChannelProductEntity $product): void
  156.     {
  157.         $markAsNewDayRange $this->systemConfigService->get('core.listing.markAsNew'$context->getSalesChannel()->getId());
  158.         $now = new \DateTime();
  159.         /* @var SalesChannelProductEntity $product */
  160.         $product->setIsNew(
  161.             $product->getReleaseDate() instanceof \DateTimeInterface
  162.             && $product->getReleaseDate()->diff($now)->days <= $markAsNewDayRange
  163.         );
  164.     }
  165. }