custom/plugins/AcrisDiscountGroupCS/src/Components/DiscountGroupGateway.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\DiscountGroup\Components;
  3. use Acris\DiscountGroup\Components\Event\DiscountGroupGatewayFilterParameterEvent;
  4. use Acris\DiscountGroup\Components\Filter\DiscountGroupActiveDataRangeFilter;
  5. use Acris\DiscountGroup\Custom\DiscountGroupDefinition;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. class DiscountGroupGateway
  18. {
  19.     private EntityRepositoryInterface $discountGroupRepository;
  20.     private EntityRepositoryInterface $productStreamMappingRepository;
  21.     /**
  22.      * @var EntitySearchResult|array|null
  23.      */
  24.     private $discountGroupSearchResult;
  25.     private EventDispatcherInterface $eventDispatcher;
  26.     public function __construct(
  27.         EntityRepositoryInterface $discountGroupRepository,
  28.         EntityRepositoryInterface $productStreamMappingRepository,
  29.         EventDispatcherInterface $eventDispatcher
  30.     ) {
  31.         $this->discountGroupRepository $discountGroupRepository;
  32.         $this->productStreamMappingRepository $productStreamMappingRepository;
  33.         $this->eventDispatcher $eventDispatcher;
  34.         $this->discountGroupSearchResult = [];
  35.     }
  36.     public function getAllDiscountGroups(SalesChannelContext $salesChannelContext, ?string $productId null, ?array $productStreamIds null): EntitySearchResult
  37.     {
  38.         if(!empty($productId) && !empty($this->discountGroupSearchResult) && is_array($this->discountGroupSearchResult) && array_key_exists($productId$this->discountGroupSearchResult)
  39.             && !empty($this->discountGroupSearchResult[$productId]) && $this->discountGroupSearchResult[$productId] instanceof EntitySearchResult) {
  40.             return $this->discountGroupSearchResult[$productId];
  41.         }
  42.         elseif (empty($productId) && !empty($this->discountGroupSearchResult) && $this->discountGroupSearchResult instanceof EntitySearchResult) {
  43.             return $this->discountGroupSearchResult;
  44.         }
  45.         $criteria = (new Criteria());
  46.         $criteria $this->addCriteria($productStreamIds$productId$criteria$salesChannelContext);
  47.         if (!empty($productId)) {
  48.             $this->discountGroupSearchResult[$productId] = $this->discountGroupRepository->search($criteria$salesChannelContext->getContext());
  49.             return $this->discountGroupSearchResult[$productId];
  50.         } else {
  51.             $this->discountGroupSearchResult $this->discountGroupRepository->search($criteria$salesChannelContext->getContext());
  52.             return $this->discountGroupSearchResult;
  53.         }
  54.     }
  55.     private function addCriteria(?array $productStreamIds, ?string $productIdCriteria $criteriaSalesChannelContext $salesChannelContext): Criteria
  56.     {
  57.         $customer $salesChannelContext->getCustomer();
  58.         $customerIds = !empty($customer) ? [$customer->getId()] : [];
  59.         $discountGroupValues = [];
  60.         if(!empty($customer) && !empty($customer->getCustomFields())) {
  61.             if(array_key_exists('acris_discount_group_customer_value'$customer->getCustomFields()) && !empty($customer->getCustomFields()['acris_discount_group_customer_value'])) {
  62.                 $discountGroupValues = [$customer->getCustomFields()['acris_discount_group_customer_value']];
  63.             } elseif(array_key_exists('acris_discount_group_value'$customer->getCustomFields()) && !empty($customer->getCustomFields()['acris_discount_group_value'])) {
  64.                 $discountGroupValues = [$customer->getCustomFields()['acris_discount_group_value']];
  65.             }
  66.         }
  67.         $event = new DiscountGroupGatewayFilterParameterEvent($customerIds$discountGroupValues$salesChannelContext);
  68.         $this->eventDispatcher->dispatch($event);
  69.         $customerFilters = [
  70.             new MultiFilter(MultiFilter::CONNECTION_AND, [
  71.                 new EqualsFilter('customerAssignmentType'DiscountGroupDefinition::CUSTOMER_ASSIGNMENT_TYPE_CUSTOMER_RULES),
  72.                 new EqualsAnyFilter('rules.id'$salesChannelContext->getRuleIds()),
  73.             ])
  74.         ];
  75.         if(!empty($event->getDiscountGroupValues())) {
  76.             array_unshift($customerFilters, new MultiFilter(MultiFilter::CONNECTION_AND, [
  77.                 new EqualsFilter('customerAssignmentType'DiscountGroupDefinition::CUSTOMER_ASSIGNMENT_TYPE_CUSTOMER_DISCOUNT_GROUP),
  78.                 new EqualsAnyFilter('discountGroup'$event->getDiscountGroupValues()),
  79.             ]));
  80.         }
  81.         if(!empty($event->getCustomerIds())) {
  82.             array_unshift($customerFilters, new MultiFilter(MultiFilter::CONNECTION_AND, [
  83.                 new EqualsFilter('customerAssignmentType'DiscountGroupDefinition::CUSTOMER_ASSIGNMENT_TYPE_CUSTOMER),
  84.                 new EqualsAnyFilter('customerId'$event->getCustomerIds())
  85.             ]));
  86.         }
  87.         $activeFilter = new DiscountGroupActiveDataRangeFilter();
  88.         $criteria->addAssociation('rules')
  89.             ->addAssociation('productStreams')
  90.             ->addFilter(new MultiFilter(MultiFilter::CONNECTION_AND, [
  91.                 new MultiFilter(MultiFilter::CONNECTION_OR$customerFilters),
  92.                 new EqualsFilter('active'true),
  93.                 $activeFilter
  94.             ]))
  95.             ->addSorting(new FieldSorting('priority'FieldSorting::DESCENDING));
  96.         if (is_array($productStreamIds)) {
  97.             $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  98.                 new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter('productAssignmentType'DiscountGroupDefinition::PRODUCT_ASSIGNMENT_TYPE_DYNAMIC_PRODUCT_GROUP)]),
  99.                 new MultiFilter(MultiFilter::CONNECTION_AND, [
  100.                     new EqualsFilter('productAssignmentType'DiscountGroupDefinition::PRODUCT_ASSIGNMENT_TYPE_DYNAMIC_PRODUCT_GROUP),
  101.                     new EqualsAnyFilter('productStreams.id'$productStreamIds),
  102.                     new NotFilter(NotFilter::CONNECTION_AND, [
  103.                         new EqualsFilter('productStreams.id'null)
  104.                     ])
  105.                 ])
  106.             ]));
  107.         }
  108.         if (!empty($productId)) {
  109.             $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  110.                 new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter('productAssignmentType'DiscountGroupDefinition::PRODUCT_ASSIGNMENT_TYPE_PRODUCT)]),
  111.                 new MultiFilter(MultiFilter::CONNECTION_AND, [
  112.                     new EqualsFilter('productAssignmentType'DiscountGroupDefinition::PRODUCT_ASSIGNMENT_TYPE_PRODUCT),
  113.                     new EqualsFilter('productId'$productId)
  114.                 ])
  115.             ]));
  116.         }
  117.         return $criteria;
  118.     }
  119.     public function getProductStreamIds(array $productIdsContext $context): array
  120.     {
  121.         $productStreamIds = [];
  122.         $idSearchResult $this->productStreamMappingRepository->searchIds((new Criteria())->addFilter(new EqualsAnyFilter('productId'$productIds)), $context);
  123.         if ($idSearchResult->getTotal() > 0) {
  124.             foreach ($idSearchResult->getIds() as $ids) {
  125.                 if (!empty($ids) && is_array($ids) && array_key_exists('productStreamId'$ids) && !empty($ids['productStreamId'])) {
  126.                     $productStreamIds[] = $ids['productStreamId'];
  127.                 }
  128.             }
  129.         }
  130.         return $productStreamIds;
  131.     }
  132. }