custom/plugins/WbfkOffer/src/Subscriber/OfferLoadedSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * WBFK
  5.  * Copyright © 2023 WBFK - Anselm Ruby
  6.  *
  7.  * @copyright  Copyright (c) 2022, 2023 WBFK - Anselm Ruby (http://www.wbfk.at)
  8.  * @author     Anselm Ruby <anselm.ruby@wbfk.at>
  9.  */
  10. namespace Wbfk\Offer\Subscriber;
  11. use Shopware\Core\Checkout\Cart\Price\GrossPriceCalculator;
  12. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  13. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  14. use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
  15. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  16. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  17. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  18. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Wbfk\Offer\Core\Entity\Offer\Aggregate\OfferDocument\OfferDocumentComputedData;
  23. use Wbfk\Offer\Core\Entity\Offer\Aggregate\OfferDocument\OfferDocumentEntity;
  24. use Wbfk\Offer\Core\Entity\Offer\Aggregate\OfferItem\OfferItemCollection;
  25. use Wbfk\Offer\Core\Entity\Offer\Aggregate\OfferItem\OfferItemComputedData;
  26. use Wbfk\Offer\Core\Entity\Offer\Aggregate\OfferItem\OfferItemEntity;
  27. use Wbfk\Offer\Core\Entity\Offer\OfferComputedData;
  28. use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
  29. use Wbfk\Offer\Service\OfferDataComputer;
  30. class OfferLoadedSubscriber implements EventSubscriberInterface
  31. {
  32.     public function __construct(
  33.         private readonly OfferDataComputer $offerDataComputer,
  34.     ) {
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             'wbfk_offer.loaded' => [
  40.                 ['offerLoaded'],
  41.             ],
  42.         ];
  43.     }
  44.     public function offerLoaded(EntityLoadedEvent $event): void
  45.     {
  46.         $offers $event->getEntities();
  47.         $isSourceAdmin $event->getContext()->getSource() instanceof AdminApiSource;
  48.         foreach ($offers as $offer) {
  49.             if (!($offer instanceof OfferEntity) || !$offer->getItems()) {
  50.                 continue;
  51.             }
  52.             // Get the last user generated configuration for this offer
  53.             $lastConfiguration $offer->getDocuments()?->getLatestUserGenerated();
  54.             // Calculate offer data with the latest configuration
  55.             $offerComputedDate $this->offerDataComputer->computeOfferData($offer$event->getContext(), $lastConfiguration);
  56.             $offer->setComputed($offerComputedDate);
  57.             if (!$isSourceAdmin) {
  58.                 continue;
  59.             }
  60.             // Calculate price of all configurations/documents if the context source is admin API
  61.             /** @var OfferDocumentEntity $document */
  62.             foreach ($configurations ?? [] as $document) {
  63.                 // Calculate price of all configurations/documents
  64.                 $document->setComputed($this->offerDataComputer->calculateOfferDocumentPrice($offerComputedDate->nestedItems$document));
  65.             }
  66.         }
  67.     }
  68. }