custom/static-plugins/CioApprovalThreshold/src/Controller/CustomerProfileController.php line 49

Open in your IDE?
  1. <?php
  2. namespace CioApprovalThreshold\Controller;
  3. use CioApprovalThreshold\Definition\Customer\CustomerApprovalThresholdEntity;
  4. use CioApprovalThreshold\States\ApprovalStates;
  5. use Psr\Container\ContainerExceptionInterface;
  6. use Psr\Container\NotFoundExceptionInterface;
  7. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  8. use Shopware\Core\Checkout\Customer\CustomerEntity;
  9. use Shopware\Core\Checkout\Order\OrderDefinition;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  18. use Shopware\Core\System\StateMachine\Transition;
  19. use Shopware\Storefront\Controller\StorefrontController;
  20. use Shopware\Storefront\Page\GenericPageLoader;
  21. use Symfony\Component\HttpFoundation\RedirectResponse;
  22. use Symfony\Component\HttpFoundation\Session\Session;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  27. use Shopware\Core\Framework\Context;
  28. class CustomerProfileController extends StorefrontController
  29. {
  30.     private StateMachineRegistry $stateMachineRegistry;
  31.     private GenericPageLoader $genericPageLoader;
  32.     public function __construct(StateMachineRegistry $stateMachineRegistryGenericPageLoader $genericPageLoader)
  33.     {
  34.         $this->stateMachineRegistry $stateMachineRegistry;
  35.         $this->genericPageLoader $genericPageLoader;
  36.     }
  37.     /**
  38.      * @Route("/account/approvalthreshold", name="frontend.account.cioapprovalthreshold.page", methods={"GET"}, defaults={"_routeScope"={"storefront"}})
  39.      * @param SalesChannelContext $context
  40.      * @return Response
  41.      * @throws ContainerExceptionInterface
  42.      * @throws NotFoundExceptionInterface
  43.      */
  44.     public function index(Request $requestSalesChannelContext $context): Response
  45.     {
  46.         if (!$context->getCustomer() instanceof CustomerEntity) {
  47.             throw new CustomerNotLoggedInException();
  48.         }
  49.         /** @var EntityRepository $customerRepository */
  50.         $customerRepository $this->container->get('customer.repository');
  51.         // all customers with a release limit over 0 points are qualified as default release customer
  52.         $criteria = new Criteria();
  53.         $criteria->addFilter(new RangeFilter('approvalThresholdExtension.releaseThreshold', [
  54.             RangeFilter::GT => 0
  55.         ]));
  56.         $possibleDefaultReleaser $customerRepository->search($criteria$context->getContext());
  57.         /** @var CustomerApprovalThresholdEntity|null $customerThreshold */
  58.         $customerThreshold $context->getCustomer()->getExtension('approvalThresholdExtension');
  59.         return $this->renderStorefront('@CioApprovalThreshold/storefront/page/account/approval_threshold.html.twig', [
  60.             'page' => $this->genericPageLoader->load($request$context),
  61.             'customerApprovalThreshold' => $customerThreshold?->getReleaseThreshold() ?? 0,
  62.             'customerDefaultReleaser' => $customerThreshold?->getDefaultReleaseCustomerId(),
  63.             'possibleDefaultReleaser' => $possibleDefaultReleaser->getElements()
  64.         ]);
  65.     }
  66.     /**
  67.      * @Route("/account/approvalthreshold", name="frontend.account.cioapprovalthreshold.save", methods={"POST"}, defaults={"_routeScope"={"storefront"}})
  68.      * @param Request $request
  69.      * @param SalesChannelContext $context
  70.      * @return RedirectResponse
  71.      * @throws ContainerExceptionInterface
  72.      * @throws NotFoundExceptionInterface
  73.      */
  74.     public function save(Request $requestSalesChannelContext $context): RedirectResponse
  75.     {
  76.         if (!$context->getCustomer() instanceof CustomerEntity) {
  77.             throw new CustomerNotLoggedInException();
  78.         }
  79.         $releaserId $request->request->get('defaultReleaseCustomer');
  80.         /** @var EntityRepository $customerRepository */
  81.         $customerRepository $this->container->get('customer.repository');
  82.         /** @var Session $session */
  83.         $session $request->getSession();
  84.         if (!is_string($releaserId) || !Uuid::isValid($releaserId)) {
  85.             $session->getFlashBag()->add('danger'$this->trans('account.cioAccountApproval.errorIncorrectCustomerId'));
  86.             return $this->redirectToRoute('frontend.account.cioapprovalthreshold.page');
  87.         }
  88.         $criteria = new Criteria();
  89.         $criteria->addFilter(new EqualsFilter('id'$releaserId));
  90.         $releaser $customerRepository->search($criteria$context->getContext())->first();
  91.         if (!$releaser instanceof CustomerEntity) {
  92.             $session->getFlashBag()->add('danger'$this->trans('account.cioAccountApproval.errorIncorrectCustomerId'));
  93.             return $this->redirectToRoute('frontend.account.cioapprovalthreshold.page');
  94.         }
  95.         $customerRepository->update([
  96.             [
  97.                 'id' => $context->getCustomer()->getId(),
  98.                 'approvalThresholdExtension' => [
  99.                     'defaultReleaseCustomerId' => $releaser->getId()
  100.                 ]
  101.             ]
  102.         ], $context->getContext());
  103.         $session->getFlashBag()->add('success'$this->trans('account.cioAccountApproval.messageSaveSuccessfully'));
  104.         return $this->redirectToRoute('frontend.account.cioapprovalthreshold.page');
  105.     }
  106.     /**
  107.      * Profile page where the customer can check the orders that are waiting for approval by the logged in customer.
  108.      * @Route("/account/openapprovalorders", name="frontend.account.openapprovalorders.page", methods={"GET"}, defaults={"_routeScope"={"storefront"}})
  109.      * @param SalesChannelContext $context
  110.      * @return Response
  111.      * @throws ContainerExceptionInterface
  112.      * @throws NotFoundExceptionInterface
  113.      */
  114.     public function openApprovalOrders(SalesChannelContext $context): Response
  115.     {
  116.         if (!$context->getCustomer() instanceof CustomerEntity) {
  117.             throw new CustomerNotLoggedInException();
  118.         }
  119.         /** @var EntityRepository $orderRepository */
  120.         $orderRepository $this->container->get('order.repository');
  121.         // all customers with a release limit over 0 points are qualified as default release customer
  122.         $criteria = new Criteria();
  123.         $criteria->addAssociation('stateMachineState');
  124.         $criteria->addAssociation('approvalThresholdExtension');
  125.         $criteria->addFilter(new EqualsFilter('stateMachineState.technicalName'ApprovalStates::STATE_OPEN_APPROVAL_REQUEST));
  126.         $criteria->addFilter(new EqualsFilter('approvalThresholdExtension.selectedReleaseCustomerId'$context->getCustomer()->getId()));
  127.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::DESCENDING));
  128.         $openOrders $orderRepository->search($criteria$context->getContext());
  129.         return $this->renderStorefront('@CioApprovalThreshold/storefront/page/account/open_approval_orders.html.twig', [
  130.             'openOrders' => $openOrders
  131.         ]);
  132.     }
  133.     /**
  134.      * @Route("/account/approvalthreshold/{orderId}/request/cancel", name="frontend.account.cioapprovalthreshold.request.cancel", methods={"GET"}, defaults={"_routeScope"={"storefront"}})
  135.      * @param SalesChannelContext $context
  136.      * @return Response
  137.      * @throws ContainerExceptionInterface
  138.      * @throws NotFoundExceptionInterface
  139.      */
  140.     public function cancelRequest(string $orderIdSalesChannelContext $context): Response
  141.     {
  142.         if (!$context->getCustomer() instanceof CustomerEntity) {
  143.             throw new CustomerNotLoggedInException();
  144.         }
  145.         /** @var EntityRepository $orderRepository */
  146.         $orderRepository $this->container->get('order.repository');
  147.         $criteria = new Criteria([$orderId]);
  148.         $criteria->addAssociation('lineItems');
  149.         $criteria->addAssociation('orderCustomer');
  150.         $criteria->addFilter(new EqualsFilter('orderCustomer.customerNumber'$context->getCustomer()->getCustomerNumber()));
  151.         $criteria->addFilter(new EqualsFilter('stateMachineState.technicalName'ApprovalStates::STATE_OPEN_APPROVAL_REQUEST));
  152.         $order $orderRepository->search($criteria$context->getContext())->first();
  153.         if ($order instanceof OrderEntity) {
  154.             $this->stateMachineRegistry->transition(
  155.                 new Transition(
  156.                     OrderDefinition::ENTITY_NAME,
  157.                     $order->getId(),
  158.                     ApprovalStates::CANCEL_REQUEST_AND_STORNO_ORDER_TRANSITION_NAME,
  159.                     'stateId'
  160.                 ),
  161.                 $context->getContext()
  162.             );
  163.             return $this->renderStorefront('@CioApprovalThreshold/storefront/page/account/cancel_approval_request.html.twig', [
  164.                 'order' => $order,
  165.             ]);
  166.         }
  167.         return $this->redirectToRoute('frontend.account.order.page');
  168.     }
  169. }