custom/plugins/SwagCommercial/src/MultiWarehouse/Subscriber/ProductSalesSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Commercial\MultiWarehouse\Subscriber;
  3. use Shopware\Commercial\Licensing\License;
  4. use Shopware\Commercial\MultiWarehouse\Domain\Order\ProductSalesUpdater;
  5. use Shopware\Core\Checkout\Order\OrderDefinition;
  6. use Shopware\Core\Checkout\Order\OrderStates;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * @internal
  12.  *
  13.  * @package inventory
  14.  */
  15. class ProductSalesSubscriber implements EventSubscriberInterface
  16. {
  17.     private ProductSalesUpdater $salesUpdater;
  18.     public function __construct(ProductSalesUpdater $salesUpdater)
  19.     {
  20.         $this->salesUpdater $salesUpdater;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             StateMachineTransitionEvent::class => 'onOrderStateChanged',
  26.         ];
  27.     }
  28.     public function onOrderStateChanged(StateMachineTransitionEvent $event): void
  29.     {
  30.         if (!License::get('MULTI_INVENTORY-3749997')) {
  31.             return;
  32.         }
  33.         if ($event->getEntityName() !== OrderDefinition::ENTITY_NAME || $event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  34.             return;
  35.         }
  36.         if ($event->getToPlace()->getTechnicalName() === OrderStates::STATE_COMPLETED) {
  37.             $this->salesUpdater->increaseSales($event->getContext(), $event->getEntityId());
  38.             return;
  39.         }
  40.         if ($event->getFromPlace()->getTechnicalName() === OrderStates::STATE_COMPLETED) {
  41.             $this->salesUpdater->decreaseSales($event->getContext(), $event->getEntityId());
  42.         }
  43.     }
  44. }