custom/plugins/CioBudget/src/Decorator/OrderRouterDecorator.php line 168

Open in your IDE?
  1. <?php
  2. namespace CioBudget\Decorator;
  3. use CioBudget\Service\BudgetLoaderService;
  4. use CioBudget\Service\SessionService;
  5. use Psr\Container\ContainerInterface;
  6. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  7. use Shopware\Core\Checkout\Customer\SalesChannel\AccountService;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Shopware\Storefront\Framework\Page\StorefrontSearchResult;
  18. use Shopware\Storefront\Page\Account\Order\AccountOrderPage;
  19. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
  20. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoader;
  21. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  22. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  23. use Symfony\Component\HttpFoundation\Request;
  24. class OrderRouterDecorator extends AccountOrderPageLoader
  25. {
  26.     private AccountOrderPageLoader $decoratedService;
  27.     private GenericPageLoaderInterface $genericLoader;
  28.     private EventDispatcherInterface $eventDispatcher;
  29.     private AccountService $accountService;
  30.     private EntityRepositoryInterface $orderRepository;
  31.     private SessionService $sessionService;
  32.     private BudgetLoaderService $budgetLoaderService;
  33.     public function __construct(
  34.         AccountOrderPageLoader     $decoratedService,
  35.         GenericPageLoaderInterface $genericLoader,
  36.         EventDispatcherInterface   $eventDispatcher,
  37.         AccountService             $accountService,
  38.         EntityRepositoryInterface  $orderRepository,
  39.         SessionService             $sessionService,
  40.         BudgetLoaderService        $budgetLoaderService
  41.     )
  42.     {
  43.         $this->genericLoader $genericLoader;
  44.         $this->eventDispatcher $eventDispatcher;
  45.         $this->accountService $accountService;
  46.         $this->decoratedService $decoratedService;
  47.         $this->orderRepository $orderRepository;
  48.         $this->sessionService $sessionService;
  49.         $this->budgetLoaderService $budgetLoaderService;
  50.     }
  51.     public function load(Request $requestSalesChannelContext $salesChannelContext): AccountOrderPage
  52.     {
  53.         if (!$salesChannelContext->getCustomer() && $request->get('deepLinkCode'false) === false) {
  54.             throw new CustomerNotLoggedInException();
  55.         }
  56.         $page $this->genericLoader->load($request$salesChannelContext);
  57.         $page AccountOrderPage::createFrom($page);
  58.         if ($page->getMetaInformation()) {
  59.             $page->getMetaInformation()->setRobots('noindex,follow');
  60.         }
  61.         $page->setOrders(StorefrontSearchResult::createFrom($this->getOrders($request$salesChannelContext)));
  62.         $page->setDeepLinkCode($request->get('deepLinkCode'));
  63.         if ($request->get('deepLinkCode') && $page->getOrders()->first() !== null) {
  64.             $this->accountService->login(
  65.                 $page->getOrders()->first()->getOrderCustomer()->getCustomer()->getEmail(),
  66.                 $salesChannelContext,
  67.                 true
  68.             );
  69.         }
  70.         $this->eventDispatcher->dispatch(
  71.             new AccountOrderPageLoadedEvent($page$salesChannelContext$request)
  72.         );
  73.         return $page;
  74.     }
  75.     public function getOrders(Request $requestSalesChannelContext $salesChannelContext): EntitySearchResult
  76.     {
  77.         $orderNumber $request->query->get('orderNumber');
  78.         $budgetId $request->query->get('budgetId') ?: $this->sessionService->getBudgetIdFromSession();
  79.         $showBudgetOrder $request->query->get('showBudgetOrder') === 'true';
  80.         $criteria $this->createCriteria($request);
  81.         $criteria->setTitle(__CLASS__ '::' __FUNCTION__);
  82.         if ($orderNumber) {
  83.             $criteria->addFilter(new EqualsFilter('order.orderNumber'$orderNumber));
  84.         }
  85.         if ($budgetId && $showBudgetOrder) {
  86.             $criteria->addFilter(new EqualsFilter('order.transactions.paymentMethodId'$this->getBudgetPaymentMethodId()));
  87.             // get store id of the current budget
  88.             $storeId $this->budgetLoaderService->getStoreIdByBudgetId($budgetId);
  89.             // search by store id for including old orders from expired budgets of this store
  90.             $criteria->addFilter(new EqualsFilter('order.history.budget.store_id'$storeId));
  91.         } elseif (!$budgetId && $showBudgetOrder) {
  92.             // if $showBudgetOrder is set, we will only show budget-orders but without budgetId we cant
  93.             // to force a zero-search-result, we reset all filters and add an impossible filter
  94.             $criteria->resetFilters();
  95.             $criteria->resetAggregations();
  96.             $criteria->resetAssociations();
  97.             $criteria->resetQueries();
  98.             $criteria->resetSorting();
  99.             $criteria->resetGroupFields();
  100.             $criteria->resetPostFilters();
  101.             $criteria->addFilter(new EqualsFilter('order.orderNumber''DONT.FIND.ANYTHING'));
  102.         } else {
  103.             $criteria->addFilter(new EqualsFilter('order.orderCustomer.customerId'$salesChannelContext->getCustomer()->getId()));
  104.         }
  105.         return $this->orderRepository->search($criteria$salesChannelContext->getContext());
  106.     }
  107.     private function createCriteria(Request $request): Criteria
  108.     {
  109.         $limit $request->get('limit');
  110.         $limit $limit ? (int)$limit 10;
  111.         $page $request->get('p');
  112.         $page $page ? (int)$page 1;
  113.         $criteria = (new Criteria());
  114.         $criteria->setTitle(__CLASS__ '::' __FUNCTION__);
  115.         $criteria->addSorting(new FieldSorting('order.createdAt'FieldSorting::DESCENDING))
  116.             ->addAssociation('transactions.paymentMethod')
  117.             ->addAssociation('deliveries.shippingMethod')
  118.             ->addAssociation('orderCustomer.customer')
  119.             ->addAssociation('lineItems')
  120.             ->addAssociation('lineItems.cover')
  121.             ->addAssociation('addresses')
  122.             ->addAssociation('currency')
  123.             ->addAssociation('documents.documentType')
  124.             ->addAssociation('order.transactions')
  125.             ->addAssociation('cio_budget_history')
  126.             ->addAssociation('cio_budget_history.budget')
  127.             ->setLimit($limit)
  128.             ->setOffset(($page 1) * $limit)
  129.             ->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT)
  130.             ->addSorting(new FieldSorting('orderDateTime'FieldSorting::DESCENDING));
  131.         $criteria
  132.             ->getAssociation('transactions')
  133.             ->addSorting(new FieldSorting('createdAt'));
  134.         if ($request->get('deepLinkCode')) {
  135.             $criteria->addFilter(new EqualsFilter('deepLinkCode'$request->get('deepLinkCode')));
  136.         }
  137.         return $criteria;
  138.     }
  139.     public function setContainer(ContainerInterface $container)
  140.     {
  141.         $this->container $container;
  142.     }
  143.     private function getBudgetPaymentMethodId(): ?string
  144.     {
  145.         /** @var EntityRepositoryInterface $paymentRepository */
  146.         $paymentRepository $this->container->get('payment_method_translation.repository');
  147.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('payment_method_translation.name''Budget'));
  148.         $paymentMethodEntity $paymentRepository->search($paymentCriteriaContext::createDefaultContext())->first();
  149.         return $paymentMethodEntity->getPaymentMethodId();
  150.     }
  151. }