custom/plugins/TemplaidRma/src/Controller/RmaController.php line 106

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TemplaidRma\Controller;
  3. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteException;
  8. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  9. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Controller\StorefrontController;
  12. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  13. use Shopware\Storefront\Page\GenericPageLoader;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  20. use Shopware\Core\Framework\Routing\Annotation\Since;
  21. use TemplaidRma\Storefront\Page\Account\RmaCreatePageLoader;
  22. use TemplaidRma\Storefront\Page\Account\RmaPageLoader;
  23. use TemplaidRma\Storefront\Page\Account\RmaDetailPageLoader;
  24. use TemplaidRma\Service\RmaService;
  25. /**
  26.  * @RouteScope(scopes={"storefront"})
  27.  */
  28. class RmaController extends StorefrontController
  29. {
  30.     private GenericPageLoader $pageLoader;
  31.     private RmaPageLoader $rmaPageLoader;
  32.     private RmaService $rmaService;
  33.     private RmaCreatePageLoader $rmaCreatePageLoader;
  34.     private RmaDetailPageLoader $rmaDetailPageLoader;
  35.     public function __construct(
  36.         GenericPageLoader $pageLoader,
  37.         RmaPageLoader $rmaPageLoader,
  38.         RmaService $rmaService,
  39.         RmaCreatePageLoader $rmaCreatePageLoader,
  40.         RmaDetailPageLoader $rmaDetailPageLoader
  41.     ) {
  42.         $this->pageLoader $pageLoader;
  43.         $this->rmaPageLoader $rmaPageLoader;
  44.         $this->rmaService $rmaService;
  45.         $this->rmaCreatePageLoader $rmaCreatePageLoader;
  46.         $this->rmaDetailPageLoader $rmaDetailPageLoader;
  47.     }
  48.     /**
  49.      * @Since("6.0.0.0")
  50.      * @LoginRequired()
  51.      * @Route("/account/rma/index", name="frontend.rma.index.page", methods={"GET"})
  52.      * @NoStore()
  53.      *
  54.      * @throws CustomerNotLoggedInException
  55.      * @throws CategoryNotFoundException
  56.      * @throws InconsistentCriteriaIdsException
  57.      * @throws MissingRequestParameterException
  58.      */
  59.     public function rmaIndex(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  60.     {
  61.         $page $this->rmaPageLoader->load($request$context$customer);
  62.         return $this->renderStorefront('@Storefront/storefront/page/account/rma/index.html.twig', ['page' => $page]);
  63.     }
  64.     /**
  65.      * @Route("/account/rma/index/lookup", name="frontend.rma.orderlookup", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  66.      *
  67.      * @throws NotFoundHttpException
  68.      */
  69.     public function orderLookup(RequestDataBag $dataSalesChannelContext $context): Response
  70.     {
  71.         $orderNumber = (string)$data->get('orderNumber');
  72.         if ($orderNumber === '') {
  73.             $this->addFlash(self::DANGER$this->trans('templaidRma.account.index.orderNumberMissing'));
  74.             return $this->redirectToRoute('frontend.rma.orderlookup');
  75.         }
  76.         $orderId $this->rmaService->orderLookup($orderNumber$context);
  77.         if ($this->rmaService->getRmaIdByOrderId($orderId$context)) {
  78.             $this->addFlash(self::DANGER$this->trans('templaidRma.account.create.orderRmaAlreadyExists'));
  79.             return $this->redirectToRoute('frontend.rma.index.page');
  80.         }
  81.         if($orderId) {
  82.             return $this->redirectToRoute(
  83.                 'frontend.rma.create',
  84.                 [
  85.                     'orderId' => $orderId,
  86.                 ]
  87.             );
  88.         } else {
  89.             $this->addFlash(self::DANGER$this->trans('templaidRma.account.create.orderNotFound'));
  90.             return $this->redirectToRoute('frontend.rma.index.page');
  91.         }
  92.     }
  93.     /**
  94.      * @Route("/account/rma/create/{orderId}", name="frontend.rma.create", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  95.      * @LoginRequired()
  96.      * @throws NotFoundHttpException
  97.      */
  98.     public function create(Request $requestSalesChannelContext $contextCustomerEntity $customerstring $orderId): Response
  99.     {
  100.         $page $this->rmaCreatePageLoader->load($request$context$orderId);
  101.         return $this->renderStorefront('@Storefront/storefront/page/account/rma/create.html.twig', ['page' => $page]);
  102.     }
  103.     /**
  104.      * @Route("/account/rma/save", name="frontend.rma.create.save", options={"seo"="false"}, methods={"POST"})
  105.      * @LoginRequired()
  106.      * @throws WriteException
  107.      */
  108.     public function createSave(RequestDataBag $dataSalesChannelContext $context): Response
  109.     {
  110.         try {
  111.             $data->remove('_csrf_token');
  112.             /**
  113.              * @var RequestDataBag $items
  114.              */
  115.             $items $data->get('rmaItems');
  116.             foreach ($items as $key => $item) {
  117.                 $qty = (int) $item->get('qty');
  118.                 if ($qty === 0) {
  119.                     $items->remove($key);
  120.                     continue;
  121.                 }
  122.                 $item->set('qty'$qty);
  123.             }
  124.             $data->set('rmaItems'$items);
  125.             $this->rmaService->createRma($data->all(), $context);
  126.             $this->addFlash(self::SUCCESS$this->trans('templaidRma.account.create.createSaveSuccess'));
  127.         } catch (WriteException $e) {
  128.             $this->addFlash(self::DANGER$this->trans('templaidRma.account.create.createSaveError'));
  129.         }
  130.         return $this->redirectToRoute('frontend.rma.index.page');
  131.     }
  132.     /**
  133.      * @Since("6.0.0.0")
  134.      * @LoginRequired()
  135.      * @Route("/account/rma/detail/{rmaId}", name="frontend.rma.detail", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  136.      * @NoStore()
  137.      *
  138.      * @throws CustomerNotLoggedInException
  139.      * @throws InconsistentCriteriaIdsException
  140.      * @throws MissingRequestParameterException
  141.      */
  142.     public function rmaDetail(string $rmaIdRequest $requestSalesChannelContext $context): Response
  143.     {
  144.         $page $this->rmaDetailPageLoader->load($rmaId$request$context);
  145.         return $this->renderStorefront('@Storefront/storefront/page/account/rma/detail.html.twig', ['page' => $page]);
  146.     }
  147. }