vendor/shopware/core/Checkout/Cart/Tax/TaxDetector.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\Tax;
  3. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Shopware\Core\System\Country\CountryEntity;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. #[Package('checkout')]
  8. class TaxDetector
  9. {
  10.     public function useGross(SalesChannelContext $context): bool
  11.     {
  12.         return $context->getCurrentCustomerGroup()->getDisplayGross();
  13.     }
  14.     public function isNetDelivery(SalesChannelContext $context): bool
  15.     {
  16.         $shippingLocationCountry $context->getShippingLocation()->getCountry();
  17.         $countryTaxFree $shippingLocationCountry->getCustomerTax()->getEnabled();
  18.         if ($countryTaxFree) {
  19.             return true;
  20.         }
  21.         return $this->isCompanyTaxFree($context$shippingLocationCountry);
  22.     }
  23.     public function getTaxState(SalesChannelContext $context): string
  24.     {
  25.         if ($this->isNetDelivery($context)) {
  26.             return CartPrice::TAX_STATE_FREE;
  27.         }
  28.         if ($this->useGross($context)) {
  29.             return CartPrice::TAX_STATE_GROSS;
  30.         }
  31.         return CartPrice::TAX_STATE_NET;
  32.     }
  33.     public function isCompanyTaxFree(SalesChannelContext $contextCountryEntity $shippingLocationCountry): bool
  34.     {
  35.         $customer $context->getCustomer();
  36.         $countryCompanyTaxFree $shippingLocationCountry->getCompanyTax()->getEnabled();
  37.         if (!$countryCompanyTaxFree || !$customer || !$customer->getCompany()) {
  38.             return false;
  39.         }
  40.         $vatPattern $shippingLocationCountry->getVatIdPattern();
  41.         $vatIds array_filter($customer->getVatIds() ?? []);
  42.         if (empty($vatIds)) {
  43.             return false;
  44.         }
  45.         if (!empty($vatPattern) && $shippingLocationCountry->getCheckVatIdPattern()) {
  46.             $regex '/^' $vatPattern '$/i';
  47.             foreach ($vatIds as $vatId) {
  48.                 if (!preg_match($regex$vatId)) {
  49.                     return false;
  50.                 }
  51.             }
  52.         }
  53.         return true;
  54.     }
  55. }