<?php declare(strict_types=1);
namespace SmartDato\DatoElectronicInvoicing\Storefront\Controller;
use Shopware\Core\Checkout\Cart\Exception\CartTokenNotFoundException;
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
use Shopware\Core\Checkout\Cart\Exception\OrderNotFoundException;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\CheckoutController as CheckoutControllerInner;
use SmartDato\DatoElectronicInvoicing\EntityManager\Customer\CustomerAddressEntityManager;
use SmartDato\DatoElectronicInvoicing\Service\PluginConfig\PluginConfigService;
use SmartDato\DatoElectronicInvoicing\Validation\Constraint\MainValidator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class CheckoutController extends StorefrontBaseController
{
/** @var CheckoutControllerInner */
private $checkoutControllerInner;
/** @var PluginConfigService */
private $pluginConfigService;
/**
* CheckoutController constructor.
*
* @param CheckoutControllerInner $checkoutControllerInner
* @param PluginConfigService $pluginConfigService
*/
public function __construct(
$checkoutControllerInner,
PluginConfigService $pluginConfigService
)
{
$this->checkoutControllerInner = $checkoutControllerInner;
$this->pluginConfigService = $pluginConfigService;
}
/**
* @Since("6.0.0.0")
* @Route("/checkout/cart", name="frontend.checkout.cart.page", options={"seo"="false"}, methods={"GET"})
*/
public function cartPage(Request $request, SalesChannelContext $context): Response
{
return $this->checkoutControllerInner->cartPage($request, $context);
}
/**
* @Since("6.0.0.0")
* @Route("/checkout/confirm", name="frontend.checkout.confirm.page", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
public function confirmPage(Request $request, SalesChannelContext $context): Response
{
return $this->checkoutControllerInner->confirmPage($request, $context);
}
/**
* @Since("6.0.0.0")
* @Route("/checkout/finish", name="frontend.checkout.finish.page", options={"seo"="false"}, methods={"GET"})
*
* @throws CustomerNotLoggedInException
* @throws MissingRequestParameterException
* @throws OrderNotFoundException
*/
public function finishPage(Request $request, SalesChannelContext $context, RequestDataBag $dataBag): Response
{
return $this->checkoutControllerInner->finishPage($request, $context, $dataBag);
}
/**
* @Since("6.0.0.0")
* @Route("/checkout/order", name="frontend.checkout.finish.order", options={"seo"="false"}, methods={"POST"})
*/
public function order(RequestDataBag $data, SalesChannelContext $context, Request $request): Response
{
$this->addFiscalCodeAndInvoicingFieldsValidationDefinitions($data, $context);
return $this->checkoutControllerInner->order($data, $context, $request);
}
/**
* @Since("6.0.0.0")
* @Route("/widgets/checkout/info", name="frontend.checkout.info", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*
* @throws CartTokenNotFoundException
*/
public function info(Request $request, SalesChannelContext $context): Response
{
return $this->checkoutControllerInner->info($request, $context);
}
/**
* @Since("6.0.0.0")
* @Route("/checkout/offcanvas", name="frontend.cart.offcanvas", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
*
* @throws CartTokenNotFoundException
*/
public function offcanvas(Request $request, SalesChannelContext $context): Response
{
return $this->checkoutControllerInner->offcanvas($request, $context);
}
/**
* Add fiscal code and invoicing fields validation definitions to validate order
*
* @param RequestDataBag $data
* @param SalesChannelContext $context
*/
private function addFiscalCodeAndInvoicingFieldsValidationDefinitions(RequestDataBag $data, SalesChannelContext $context): void
{
if ($context->getCustomer() === null) {
return;
}
$billingAddress = $context->getCustomer()->getActiveBillingAddress();
if ($billingAddress === null) {
return;
}
$validationConfig = $this->pluginConfigService->getValidationConfig($context->getSalesChannel()->getId());
if (!$validationConfig->active) {
return;
}
$this->setFiscalCodeAndInvoicingFieldsDataToValidate($data, $billingAddress);
MainValidator::createConstraintsAndAddToContext(
$context->getContext(),
CustomerAddressEntityManager::getCustomerTypeFromEntity($billingAddress),
$billingAddress->getCountryId(),
$validationConfig
);
}
/**
* Set fiscal code and invoicing fields data to validate these data
*
* @param RequestDataBag $data
* @param CustomerAddressEntity $billingAddress
*/
private function setFiscalCodeAndInvoicingFieldsDataToValidate(RequestDataBag $data, CustomerAddressEntity $billingAddress): void
{
$data->set(
MainValidator::DATO_FISCAL_CODE,
(string)CustomerAddressEntityManager::getFiscalCodeFromEntity($billingAddress)
);
$data->set(
MainValidator::DATO_ELECTRONIC_INVOICING_FIELDS,
CustomerAddressEntityManager::getInvoicingFieldsFromEntity($billingAddress)
);
}
}