custom/plugins/AcrisTaxCS/src/Core/Checkout/Cart/Tax/TaxDetector.php line 131

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Tax\Core\Checkout\Cart\Tax;
  3. use Acris\Tax\Components\Service\TaxService;
  4. use Acris\Tax\Components\Service\VatIdValidationService;
  5. use Acris\Tax\Core\Checkout\Cart\TaxRuleLoader;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\System\Country\CountryEntity;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\Checkout\Cart\Tax\TaxDetector as ParentTaxDetector;
  12. class TaxDetector extends ParentTaxDetector
  13. {
  14.     /**
  15.      * @var ParentTaxDetector
  16.      */
  17.     private $parentTaxDetector;
  18.     /**
  19.      * @var EntityRepositoryInterface
  20.      */
  21.     private $customerGroupRulesRepository;
  22.     /**
  23.      * @var EntityRepositoryInterface
  24.      */
  25.     private $countryRulesRepository;
  26.     /**
  27.      * @var null|array
  28.      */
  29.     private $savedCustomerGroupRuleIds;
  30.     /**
  31.      * @var null|array
  32.      */
  33.     private $savedCountryRuleIds;
  34.     /**
  35.      * @var TaxRuleLoader
  36.      */
  37.     private $taxRuleLoader;
  38.     /**
  39.      * @var array|null
  40.      */
  41.     private $ruleIdsForContext;
  42.     /**
  43.      * @var TaxService
  44.      */
  45.     private $taxService;
  46.     public function __construct(
  47.         ParentTaxDetector $parentTaxDetector,
  48.         EntityRepositoryInterface $customerGroupRulesRepository,
  49.         EntityRepositoryInterface $countryRulesRepository,
  50.         TaxRuleLoader $taxRuleLoader,
  51.         TaxService $taxService
  52.     )
  53.     {
  54.         $this->parentTaxDetector $parentTaxDetector;
  55.         $this->customerGroupRulesRepository $customerGroupRulesRepository;
  56.         $this->countryRulesRepository $countryRulesRepository;
  57.         $this->savedCustomerGroupRuleIds null;
  58.         $this->savedCountryRuleIds null;
  59.         $this->taxRuleLoader $taxRuleLoader;
  60.         $this->taxService $taxService;
  61.     }
  62.     public function useGross(SalesChannelContext $context): bool
  63.     {
  64.         return $this->parentTaxDetector->useGross($context);
  65.     }
  66.     public function isNetDelivery(SalesChannelContext $context): bool
  67.     {
  68.         if ($this->taxService->isExcludedCountry($context->getShippingLocation()->getCountry()->getId(), $context->getSalesChannel()->getId()))
  69.             return false;
  70.         $ruleIdsFromSalesChannelContext $this->getRuleIdsFromSalesChannelContext($context);
  71.         $checkDefaultAdvancedTaxRules $this->checkDefaultAdvancedTaxRules($context$ruleIdsFromSalesChannelContext);
  72.         if (is_bool($checkDefaultAdvancedTaxRules)) return $checkDefaultAdvancedTaxRules;
  73.         $countryRuleIds $this->getCountryRuleIds($context);
  74.         if(empty($countryRuleIds)) {
  75.             return $this->parentTaxDetector->isNetDelivery($context);
  76.         }
  77.         foreach ($countryRuleIds as $countryRuleId) {
  78.             if(array_key_exists('rule_id'$countryRuleId) === false) {
  79.                 continue;
  80.             }
  81.             if(in_array($countryRuleId['rule_id'], $ruleIdsFromSalesChannelContext)) {
  82.                 return true;
  83.             }
  84.         }
  85.         return false;
  86.     }
  87.     public function isCompanyTaxFree(SalesChannelContext $contextCountryEntity $shippingLocationCountry): bool
  88.     {
  89.         if ($this->taxService->isExcludedCountry($shippingLocationCountry->getId(), $context->getSalesChannel()->getId()))
  90.             return false;
  91.         $ruleIdsFromSalesChannelContext $this->getRuleIdsFromSalesChannelContext($context);
  92.         $checkDefaultAdvancedTaxRules $this->checkDefaultAdvancedTaxRules($context$ruleIdsFromSalesChannelContext);
  93.         if (is_bool($checkDefaultAdvancedTaxRules)) return $checkDefaultAdvancedTaxRules;
  94.         $countryRuleIds $this->getCountryRuleIds($context);
  95.         if(empty($countryRuleIds)) {
  96.             return $this->parentTaxDetector->isCompanyTaxFree($context$shippingLocationCountry);
  97.         }
  98.         foreach ($countryRuleIds as $countryRuleId) {
  99.             if(array_key_exists('rule_id'$countryRuleId) === false) {
  100.                 continue;
  101.             }
  102.             if(in_array($countryRuleId['rule_id'], $ruleIdsFromSalesChannelContext)) {
  103.                 return true;
  104.             }
  105.         }
  106.         return false;
  107.     }
  108.     private function getRuleIdsFromSalesChannelContext(SalesChannelContext $context): array
  109.     {
  110.         if(empty($context->getRuleIds())) {
  111.             if(is_array($this->ruleIdsForContext) === true) {
  112.                 return $this->ruleIdsForContext;
  113.             }
  114.             $ruleIds $this->taxRuleLoader->loadRulesIdsForContext($context);
  115.             return $this->ruleIdsForContext $ruleIds;
  116.         }
  117.         return $context->getRuleIds();
  118.     }
  119.     private function getCustomerGroupRuleIds(SalesChannelContext $context)
  120.     {
  121.         if($this->savedCustomerGroupRuleIds === null) {
  122.             $customerGroupRuleIds $this->customerGroupRulesRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('customerGroupId'$context->getCurrentCustomerGroup()->getId())), $context->getContext());
  123.             $this->savedCustomerGroupRuleIds $customerGroupRuleIds->getIds();
  124.         }
  125.         return $this->savedCustomerGroupRuleIds;
  126.     }
  127.     private function getCountryRuleIds(SalesChannelContext $context)
  128.     {
  129.         if($this->savedCountryRuleIds === null) {
  130.             $countryRuleIds $this->countryRulesRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('countryId'$context->getShippingLocation()->getCountry()->getId())), $context->getContext());
  131.             $this->savedCountryRuleIds $countryRuleIds->getIds();
  132.         }
  133.         return $this->savedCountryRuleIds;
  134.     }
  135.     private function checkDefaultAdvancedTaxRules(SalesChannelContext $context, ?array $ruleIdsFromSalesChannelContext): ?bool
  136.     {
  137.         if (empty($ruleIdsFromSalesChannelContext) || !is_array($ruleIdsFromSalesChannelContext)) {
  138.             return null;
  139.         }
  140.         if (!$this->checkEuCountries($context->getShippingLocation()->getCountry())) return null;
  141.         $companyTax $context->getShippingLocation()->getCountry()->getCompanyTax();
  142.         if (list($rules$useAndRule) = $this->taxService->getDefaultAdvancedTaxRules($context->getSalesChannel()->getId())) {
  143.             if (!empty($rules) && is_bool($useAndRule)) {
  144.                 if ($useAndRule) {
  145.                     foreach ($rules as $rule) {
  146.                         if(!in_array($rule$ruleIdsFromSalesChannelContext)) {
  147.                             return false;
  148.                         }
  149.                     }
  150.                     $companyTax->setEnabled(true);
  151.                     return true;
  152.                 } else {
  153.                     foreach ($rules as $rule) {
  154.                         if(in_array($rule$ruleIdsFromSalesChannelContext)) {
  155.                             $companyTax->setEnabled(true);
  156.                             return true;
  157.                         }
  158.                     }
  159.                     return false;
  160.                 }
  161.             }
  162.         }
  163.         return null;
  164.     }
  165.     private function checkEuCountries(CountryEntity $country): bool
  166.     {
  167.         return in_array($country->getIso(), VatIdValidationService::DEFAULT_SPECIFIC_COUNTRIES);
  168.     }
  169. }