custom/plugins/AcrisTaxCS/src/Subscriber/VatIdValiationSubscriber.php line 71

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Tax\Subscriber;
  3. use Acris\Tax\Core\Checkout\Customer\Validation\Constraint\CustomerVatId;
  4. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. class VatIdValiationSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $systemConfigService;
  18.     /**
  19.      * @var RequestStack
  20.      */
  21.     private $requestStack;
  22.     public function __construct(SystemConfigService $systemConfigServiceRequestStack $requestStack)
  23.     {
  24.         $this->systemConfigService $systemConfigService;
  25.         $this->requestStack $requestStack;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             'framework.validation.customer.profile.update' => 'onValidateCustomerUpdate',
  31.             'framework.validation.customer.create' => 'onValidateCustomerCreate',
  32.             'framework.validation.order.update' => 'onValidateOrder',
  33.             'framework.validation.order.create' => 'onValidateOrder'
  34.         ];
  35.     }
  36.     public function onValidateCustomerUpdate(BuildValidationEvent $event): void
  37.     {
  38.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  39.             return;
  40.         }
  41.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  42.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  43.         if(!$salesChannelContext instanceof SalesChannelContext || empty($salesChannelId) === true) {
  44.             return;
  45.         }
  46.         if (!$this->systemConfigService->get('AcrisTaxCS.config.validateVatID'$salesChannelId)) {
  47.             return;
  48.         }
  49.         $customer $salesChannelContext->getCustomer();
  50.         if(!$customer instanceof CustomerEntity) {
  51.             return;
  52.         }
  53.         $billingAddress $customer->getDefaultBillingAddress();
  54.         if (empty($billingAddress) === true) {
  55.             return;
  56.         }
  57.         $billingAddressArray $this->convertAddress($billingAddress);
  58.         $this->validateVatIds($request->get('vatIds'), $billingAddressArray$event$salesChannelContext);
  59.     }
  60.     public function onValidateCustomerCreate(BuildValidationEvent $event): void
  61.     {
  62.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  63.             return;
  64.         }
  65.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  66.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  67.         if(!$salesChannelContext instanceof SalesChannelContext || empty($salesChannelId) === true) {
  68.             return;
  69.         }
  70.         if (!$this->systemConfigService->get('AcrisTaxCS.config.validateVatID'$salesChannelId)) {
  71.             return;
  72.         }
  73.         $billingAddress $request->get('billingAddress');
  74.         if (empty($billingAddress) === true) {
  75.             $billingAddress $request->get('address');
  76.         }
  77.         if (empty($billingAddress) === true) {
  78.             return;
  79.         }
  80.         $this->validateVatIds($request->get('vatIds'), $billingAddress$event$salesChannelContext);
  81.     }
  82.     public function onValidateOrder(BuildValidationEvent $event)
  83.     {
  84.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  85.             return;
  86.         }
  87.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  88.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  89.         if(!$salesChannelContext instanceof SalesChannelContext || empty($salesChannelId) === true) {
  90.             return;
  91.         }
  92.         if (!$this->systemConfigService->get('AcrisTaxCS.config.validateVatID'$salesChannelId) ||
  93.             !$this->systemConfigService->get('AcrisTaxCS.config.validateVatIDOnOrder'$salesChannelId)) {
  94.             return;
  95.         }
  96.         if(empty($salesChannelContext->getCustomer()) === true || empty($salesChannelContext->getCustomer()->getDefaultBillingAddress())) {
  97.             return;
  98.         }
  99.         $billingAddressArray $this->convertAddress($salesChannelContext->getCustomer()->getDefaultBillingAddress());
  100.         $this->validateVatIds($salesChannelContext->getCustomer()->getVatIds(), $billingAddressArray$event$salesChannelContext);
  101.     }
  102.     private function convertAddress(CustomerAddressEntity $addressEntity): array
  103.     {
  104.         return [
  105.             'street' => $addressEntity->getStreet(),
  106.             'city' => $addressEntity->getCity(),
  107.             'zipcode' => $addressEntity->getZipcode(),
  108.             'countryId' => $addressEntity->getCountryId()
  109.         ];
  110.     }
  111.     private function validateVatIds($vatIds, array $billingAddressBuildValidationEvent $eventSalesChannelContext $salesChannelContext)
  112.     {
  113.         if(empty($vatIds)) {
  114.             return;
  115.         }
  116.         foreach ($vatIds as $vatId) {
  117.             if(empty($vatId)) {
  118.                 continue;
  119.             }
  120.             $options = [
  121.                 'address' => $billingAddress,
  122.                 'vatId' => $vatId,
  123.                 'context' => $event->getContext(),
  124.                 'salesChannelContext' => $salesChannelContext,
  125.                 'isCheckOrder' => false
  126.             ];
  127.             $properties $event->getDefinition()->getProperties();
  128.             if (!array_key_exists('vatIds'$properties)) {
  129.                 $event->getDefinition()->add('vatIds', new CustomerVatId($options));
  130.             }
  131.         }
  132.     }
  133. }