custom/plugins/Module23ImprovedTax/src/StoreFrontend/SalesChannel/ImprovedCountryTax.php line 52

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Module23ImprovedTax\StoreFrontend\SalesChannel;
  4. use Module23ImprovedTax\Extension\Core\System\Country\ImprovedTaxEntity;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Shopware\Core\System\Country\CountryEntity;
  9. use function array_filter;
  10. use function assert;
  11. class ImprovedCountryTax
  12. {
  13.     private ?CustomerEntity $customer = null;
  14.     public function __invoke(EntityLoadedEvent $event): void
  15.     {
  16.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  17.             return;
  18.         }
  19.         $country     = $this->country($event);
  20.         $improvedTax = $this->improvedTax($country);
  21.         if ($this->isCustomerCompany() && $improvedTax->isCompanyTaxFree()) {
  22.             $this->makeCompanyTaxFree($country);
  23.             return;
  24.         }
  25.         if ($this->isCustomerCompany() && $improvedTax->isCustomerTaxFree()) {
  26.             return;
  27.         }
  28.         $this->makeCustomerTaxFree($country);
  29.     }
  30.     private function makeCompanyTaxFree(CountryEntity $country): void
  31.     {
  32.         $country->setCompanyTaxFree(true);
  33.         $country->getCompanyTax()->setEnabled(true);
  34.         $country->setTaxFree(false);
  35.         $country->getCustomerTax()->setEnabled(false);
  36.     }
  37.     private function makeCustomerTaxFree(CountryEntity $country): void
  38.     {
  39.         $country->setTaxFree(true);
  40.         $country->getCustomerTax()->setEnabled(true);
  41.         $country->setCompanyTaxFree(false);
  42.         $country->getCompanyTax()->setEnabled(false);
  43.     }
  44.     private function isCustomerCompany(): bool
  45.     {
  46.         $isCompanyCustomer = false;
  47.         if ((null !== $this->customer)
  48.             && (null !== $this->customer->getDefaultBillingAddress())
  49.             && (false === empty($this->customer->getDefaultBillingAddress()->getCompany()))
  50.         ) {
  51.             $isCompanyCustomer = true;
  52.         }
  53.         return $isCompanyCustomer;
  54.     }
  55.     private function improvedTax(CountryEntity $country): ImprovedTaxEntity
  56.     {
  57.         $improvedTax = $country->getExtension('improvedTax');
  58.         assert($improvedTax instanceof ImprovedTaxEntity);
  59.         return $improvedTax;
  60.     }
  61.     private function country(EntityLoadedEvent $event): CountryEntity
  62.     {
  63.         return array_filter($event->getEntities())[0];
  64.     }
  65.     public function setCustomer(CustomerEntity $customer): void
  66.     {
  67.         $this->customer = $customer;
  68.     }
  69. }