custom/plugins/AcrisTaxCS/src/Storefront/Subscriber/AddressVatIdSubscriber.php line 176

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Tax\Storefront\Subscriber;
  3. use Acris\Tax\Components\Service\VatIdValidationService;
  4. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Framework\Event\DataMappingEvent;
  7. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  8. use Shopware\Core\PlatformRequest;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. class AddressVatIdSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var VatIdValidationService
  18.      */
  19.     private $vatIdValidationService;
  20.     /**
  21.      * @var SystemConfigService
  22.      */
  23.     private $systemConfigService;
  24.     /**
  25.      * @var RequestStack
  26.      */
  27.     private $requestStack;
  28.     public function __construct(
  29.         VatIdValidationService $vatIdValidationService,
  30.         SystemConfigService $systemConfigService,
  31.         RequestStack $requestStack
  32.     ) {
  33.         $this->vatIdValidationService $vatIdValidationService;
  34.         $this->systemConfigService $systemConfigService;
  35.         $this->requestStack $requestStack;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onRegisterCustomerEvent',
  41.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onMappingEventAddressCreate',
  42.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onMappingEventAddressShippingRegister',
  43.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onMappingEventAddressBillingRegister',
  44.             CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'onMappingEventCustomerProfileSave',
  45.             AddressListingPageLoadedEvent::class => 'onAddressListingPageLoaded',
  46.             CartConvertedEvent::class => 'onCartConverted'
  47.         ];
  48.     }
  49.     public function onRegisterCustomerEvent(DataMappingEvent $event): void
  50.     {
  51.         $output $event->getOutput();
  52.         $input $event->getInput();
  53.         if (!$input->get('billingAddress')) {
  54.             return;
  55.         }
  56.         if (!$input->get('vatIds')) {
  57.             return;
  58.         }
  59.         $vatIds $event->getInput()->get('vatIds');
  60.         /** @var DataBag $billing */
  61.         $billing $event->getInput()->get('billingAddress');
  62.         $billing->set('customFields', [
  63.             'acris_address_vat_id' => $vatIds[0]
  64.         ]);
  65.         $event->setOutput($output);
  66.     }
  67.     public function onMappingEventAddressCreate(DataMappingEvent $event): void
  68.     {
  69.         $inputData $event->getInput();
  70.         $outputData $event->getOutput();
  71.         $customFields $inputData->get('customFields') ?? [];
  72.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  73.             return;
  74.         }
  75.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  76.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  77.         if($salesChannelContext instanceof SalesChannelContext && empty($salesChannelId) !== true && !empty($this->systemConfigService->get('AcrisTaxCS.config.addressVatIdSavingBehavior'$salesChannelId)) === true
  78.             && (empty($customFields) === true || empty($customFields->get('acris_address_vat_id')) === true)) {
  79.             $customFields['acris_address_vat_id'] = null;
  80.             $outputData['customFields'] = $customFields;
  81.             $event->setOutput($outputData);
  82.             return;
  83.         }
  84.         if (empty($customFields)) return;
  85.         $addressVatId $customFields->get('acris_address_vat_id');
  86.         $addressVatIdNew = ($addressVatId) ?? null;
  87.         $customFields['acris_address_vat_id'] = $addressVatIdNew;
  88.         $outputData['customFields'] = $customFields;
  89.         $event->setOutput($outputData);
  90.     }
  91.     public function onMappingEventAddressBillingRegister(DataMappingEvent $event)
  92.     {
  93.         $output $event->getOutput();
  94.         $customFields $event->getInput()->get('customFields') ?? [];
  95.         if (empty($customFields)) return;
  96.         $output['customFields'] = $customFields;
  97.         $event->setOutput($output);
  98.     }
  99.     public function onMappingEventCustomerProfileSave(DataMappingEvent $event)
  100.     {
  101.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  102.             return;
  103.         }
  104.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  105.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  106.         if(!$salesChannelContext instanceof SalesChannelContext || empty($salesChannelId) === true || !$this->systemConfigService->get('AcrisTaxCS.config.personalDataVatIdSavingBehavior'$salesChannelId)) {
  107.             return;
  108.         }
  109.         $output $event->getOutput();
  110.         $vatIds $event->getInput()->get('vatIds');
  111.         if (empty($vatIds)) {
  112.             $output['vatIds'] = null;
  113.         }
  114.         $event->setOutput($output);
  115.     }
  116.     public function onMappingEventAddressShippingRegister(DataMappingEvent $event): void
  117.     {
  118.         $inputData $event->getInput();
  119.         $outputData $event->getOutput();
  120.         $customFields $inputData->get('customFields') ?? [];
  121.         if (empty($customFields)) return;
  122.         $addressVatId $customFields->get('acris_address_vat_id');
  123.         $addressVatIdNew = ($addressVatId) ?? null;
  124.         $customFields['acris_address_vat_id'] = $addressVatIdNew;
  125.         $outputData['customFields'] = $customFields;
  126.         $event->setOutput($outputData);
  127.     }
  128.     public function onAddressListingPageLoaded(AddressListingPageLoadedEvent $event): void
  129.     {
  130.         if (empty($event->getSalesChannelContext()->getCustomer()) || $event->getPage()->getAddresses()->count() === 0) return;
  131.         $this->vatIdValidationService->checkPageAddresses($event->getPage()->getAddresses(), $event->getSalesChannelContext()->getCustomer(), $event->getSalesChannelContext());
  132.     }
  133.     public function onCartConverted(CartConvertedEvent $event): void
  134.     {
  135.         if (empty($event->getSalesChannelContext()) || empty($event->getSalesChannelContext()->getCustomer())) return;
  136.         $customer $event->getSalesChannelContext()->getCustomer();
  137.         $convertedCart $event->getConvertedCart();
  138.         if (empty($customer->getCustomFields()) || !array_key_exists('orderCustomer'$convertedCart)) return;
  139.         $convertedCart['orderCustomer']['customFields'] = $customer->getCustomFields();
  140.         $event->setConvertedCart($convertedCart);
  141.     }
  142. }