<?php declare(strict_types=1);
namespace TemplaidRma\Controller;
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteException;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
use Shopware\Storefront\Page\GenericPageLoader;
use Symfony\Component\HttpFoundation\Request;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
use Shopware\Core\Framework\Routing\Annotation\Since;
use TemplaidRma\Storefront\Page\Account\RmaCreatePageLoader;
use TemplaidRma\Storefront\Page\Account\RmaPageLoader;
use TemplaidRma\Storefront\Page\Account\RmaDetailPageLoader;
use TemplaidRma\Service\RmaService;
/**
* @RouteScope(scopes={"storefront"})
*/
class RmaController extends StorefrontController
{
private GenericPageLoader $pageLoader;
private RmaPageLoader $rmaPageLoader;
private RmaService $rmaService;
private RmaCreatePageLoader $rmaCreatePageLoader;
private RmaDetailPageLoader $rmaDetailPageLoader;
public function __construct(
GenericPageLoader $pageLoader,
RmaPageLoader $rmaPageLoader,
RmaService $rmaService,
RmaCreatePageLoader $rmaCreatePageLoader,
RmaDetailPageLoader $rmaDetailPageLoader
) {
$this->pageLoader = $pageLoader;
$this->rmaPageLoader = $rmaPageLoader;
$this->rmaService = $rmaService;
$this->rmaCreatePageLoader = $rmaCreatePageLoader;
$this->rmaDetailPageLoader = $rmaDetailPageLoader;
}
/**
* @Since("6.0.0.0")
* @LoginRequired()
* @Route("/account/rma/index", name="frontend.rma.index.page", methods={"GET"})
* @NoStore()
*
* @throws CustomerNotLoggedInException
* @throws CategoryNotFoundException
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function rmaIndex(Request $request, SalesChannelContext $context, CustomerEntity $customer): Response
{
$page = $this->rmaPageLoader->load($request, $context, $customer);
return $this->renderStorefront('@Storefront/storefront/page/account/rma/index.html.twig', ['page' => $page]);
}
/**
* @Route("/account/rma/index/lookup", name="frontend.rma.orderlookup", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
*
* @throws NotFoundHttpException
*/
public function orderLookup(RequestDataBag $data, SalesChannelContext $context): Response
{
$orderNumber = (string)$data->get('orderNumber');
if ($orderNumber === '') {
$this->addFlash(self::DANGER, $this->trans('templaidRma.account.index.orderNumberMissing'));
return $this->redirectToRoute('frontend.rma.orderlookup');
}
$orderId = $this->rmaService->orderLookup($orderNumber, $context);
if ($this->rmaService->getRmaIdByOrderId($orderId, $context)) {
$this->addFlash(self::DANGER, $this->trans('templaidRma.account.create.orderRmaAlreadyExists'));
return $this->redirectToRoute('frontend.rma.index.page');
}
if($orderId) {
return $this->redirectToRoute(
'frontend.rma.create',
[
'orderId' => $orderId,
]
);
} else {
$this->addFlash(self::DANGER, $this->trans('templaidRma.account.create.orderNotFound'));
return $this->redirectToRoute('frontend.rma.index.page');
}
}
/**
* @Route("/account/rma/create/{orderId}", name="frontend.rma.create", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
* @LoginRequired()
* @throws NotFoundHttpException
*/
public function create(Request $request, SalesChannelContext $context, CustomerEntity $customer, string $orderId): Response
{
$page = $this->rmaCreatePageLoader->load($request, $context, $orderId);
return $this->renderStorefront('@Storefront/storefront/page/account/rma/create.html.twig', ['page' => $page]);
}
/**
* @Route("/account/rma/save", name="frontend.rma.create.save", options={"seo"="false"}, methods={"POST"})
* @LoginRequired()
* @throws WriteException
*/
public function createSave(RequestDataBag $data, SalesChannelContext $context): Response
{
try {
$data->remove('_csrf_token');
/**
* @var RequestDataBag $items
*/
$items = $data->get('rmaItems');
foreach ($items as $key => $item) {
$qty = (int) $item->get('qty');
if ($qty === 0) {
$items->remove($key);
continue;
}
$item->set('qty', $qty);
}
$data->set('rmaItems', $items);
$this->rmaService->createRma($data->all(), $context);
$this->addFlash(self::SUCCESS, $this->trans('templaidRma.account.create.createSaveSuccess'));
} catch (WriteException $e) {
$this->addFlash(self::DANGER, $this->trans('templaidRma.account.create.createSaveError'));
}
return $this->redirectToRoute('frontend.rma.index.page');
}
/**
* @Since("6.0.0.0")
* @LoginRequired()
* @Route("/account/rma/detail/{rmaId}", name="frontend.rma.detail", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
* @NoStore()
*
* @throws CustomerNotLoggedInException
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function rmaDetail(string $rmaId, Request $request, SalesChannelContext $context): Response
{
$page = $this->rmaDetailPageLoader->load($rmaId, $request, $context);
return $this->renderStorefront('@Storefront/storefront/page/account/rma/detail.html.twig', ['page' => $page]);
}
}