<?php
namespace WbfkExtensions\Service;
use Maxia\MaxiaTaxSwitch6\Service\TaxSwitchService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class TaxSwitchServiceDecorator extends TaxSwitchService
{
private const FORCE_NET_FOR_PATHS = '~'
. '^/checkout/order$'
. '|' . '^/offer-checkout/[a-zA-Z0-9_-]*/order$'
. '|' . '^/offer/[a-zA-Z0-9_-]*$' // Offer Deeplink checkout
. '|' . '^/api/_action/sm-split-order/move-line-items-to-new-order$' // Plugin: SchnitzraumSplitOrderSW6 - Split Orders
. '|' . '^/api/_action/sm-split-order/copy-line-items-to-new-order$' // Plugin: SchnitzraumSplitOrderSW6 - Split Orders
. '|' . '^/api/_action/product-price/order/*$' // On Recalculating in the Backend
. '~';
public function __construct(
RequestStack $requestStack,
EventDispatcherInterface $eventDispatcher,
SystemConfigService $configService,
private readonly TaxSwitchService $decoratedService
) {
parent::__construct($requestStack, $eventDispatcher, $configService);
}
public function initializeContext(SalesChannelContext $context): SalesChannelContext
{
if($this->isOfferPreviewOrDownload()){
return $context;
}
return $this->decoratedService->initializeContext($context);
}
/**
* Returns the current tax switch setting.
* We force the taxState to be net for certain paths
* Set null/plugin deactivated state for offer preview or download from admin
*/
public function getDisplayNet(SalesChannelContext $context): ?bool
{
if($this->isOfferPreviewOrDownload()){
return null;
}
if ($context->hasState('allow_net_cart')) {
return true;
}
if ($this->getMainRequest() && preg_match(self::FORCE_NET_FOR_PATHS, $this->getMainRequest()->getPathInfo())) {
return true;
}
if ($this->isApiRequest()) {
/*
* Yes, on requests to the API, we want to force TAX_FREE or NET
* E.g. Orders created in the Admin
*/
return true;
}
return $this->decoratedService->getDisplayNet($context);
}
public function setDisplayNet(SalesChannelContext $context, bool $displayNet): void
{
$this->decoratedService->setDisplayNet($context, $displayNet);
}
protected function isOfferPreviewOrDownload(): bool
{
return $this->getMainRequest() && (
str_starts_with($this->getMainRequest()->getPathInfo(), '/api/_action/wbfk-offer/preview') ||
str_starts_with($this->getMainRequest()->getPathInfo(), '/api/_action/wbfk-offer/download')
);
}
}