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

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