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

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.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  52.             return;
  53.         }
  54.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  55.         if (!$this->assignAddressVatId($salesChannelId)) return;
  56.         $output $event->getOutput();
  57.         $input $event->getInput();
  58.         if (!$input->get('billingAddress')) {
  59.             return;
  60.         }
  61.         if (!$input->get('vatIds')) {
  62.             return;
  63.         }
  64.         $vatIds $event->getInput()->get('vatIds');
  65.         /** @var DataBag $billing */
  66.         $billing $event->getInput()->get('billingAddress');
  67.         $customFields $billing->get('customFields') ?? [];
  68.         if ($customFields instanceof DataBag) {
  69.             $customFields->set('acris_address_vat_id'$vatIds[0]);
  70.         }
  71.         elseif (is_array($customFields)) {
  72.             $customFields['acris_address_vat_id'] = $vatIds[0];
  73.         }
  74.         $billing->set('customFields'$customFields);
  75.         $event->setOutput($output);
  76.     }
  77.     public function onMappingEventAddressCreate(DataMappingEvent $event): void
  78.     {
  79.         $inputData $event->getInput();
  80.         $outputData $event->getOutput();
  81.         $customFields $inputData->get('customFields') ?? [];
  82.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  83.             return;
  84.         }
  85.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  86.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  87.         if (!$this->assignAddressVatId($salesChannelId)) return;
  88.         if($salesChannelContext instanceof SalesChannelContext && empty($salesChannelId) !== true && !empty($this->systemConfigService->get('AcrisTaxCS.config.addressVatIdSavingBehavior'$salesChannelId)) === true
  89.             && (empty($customFields) === true || empty($customFields->get('acris_address_vat_id')) === true)) {
  90.             if (array_key_exists('customFields'$outputData)) {
  91.                 $outputData['customFields']['acris_address_vat_id'] = null;
  92.             } else {
  93.                 $outputData['customFields'] = ['acris_address_vat_id' => null];
  94.             }
  95.             $event->setOutput($outputData);
  96.             return;
  97.         }
  98.         if (empty($customFields)) return;
  99.         $addressVatId $customFields->get('acris_address_vat_id');
  100.         $addressVatIdNew = ($addressVatId) ?? null;
  101.         if (array_key_exists('customFields'$outputData)) {
  102.             $outputData['customFields']['acris_address_vat_id'] = $addressVatIdNew;
  103.         } else {
  104.             $outputData['customFields'] = ['acris_address_vat_id' => $addressVatIdNew];
  105.         }
  106.         $event->setOutput($outputData);
  107.     }
  108.     public function onMappingEventAddressBillingRegister(DataMappingEvent $event)
  109.     {
  110.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  111.             return;
  112.         }
  113.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  114.         if (!$this->assignAddressVatId($salesChannelId)) return;
  115.         $output $event->getOutput();
  116.         $customFields $event->getInput()->get('customFields') ?? [];
  117.         if (empty($customFields)) return;
  118.         if ($customFields instanceof DataBag) {
  119.             $vatId $customFields->get('acris_address_vat_id');
  120.         }
  121.         elseif (is_array($customFields)) {
  122.             $vatId $customFields['acris_address_vat_id'];
  123.         }
  124.         if (empty($vatId)) return;
  125.         if (array_key_exists('customFields'$output)) {
  126.             $output['customFields']['acris_address_vat_id'] = $vatId;
  127.         } else {
  128.             $output['customFields'] = ['acris_address_vat_id' => $vatId];
  129.         }
  130.         $event->setOutput($output);
  131.     }
  132.     public function onMappingEventCustomerProfileSave(DataMappingEvent $event)
  133.     {
  134.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  135.             return;
  136.         }
  137.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  138.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  139.         if (!$this->assignAddressVatId($salesChannelId)) return;
  140.         if(!$salesChannelContext instanceof SalesChannelContext || empty($salesChannelId) === true || !$this->systemConfigService->get('AcrisTaxCS.config.personalDataVatIdSavingBehavior'$salesChannelId)) {
  141.             return;
  142.         }
  143.         $output $event->getOutput();
  144.         $vatIds $event->getInput()->get('vatIds');
  145.         if (empty($vatIds)) {
  146.             $output['vatIds'] = null;
  147.         }
  148.         $event->setOutput($output);
  149.     }
  150.     public function onMappingEventAddressShippingRegister(DataMappingEvent $event): void
  151.     {
  152.         if(empty($this->requestStack) === true || empty($request $this->requestStack->getCurrentRequest()) === true) {
  153.             return;
  154.         }
  155.         $salesChannelId $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  156.         if (!$this->assignAddressVatId($salesChannelId)) return;
  157.         $inputData $event->getInput();
  158.         $outputData $event->getOutput();
  159.         $customFields $inputData->get('customFields') ?? [];
  160.         if (empty($customFields)) return;
  161.         $addressVatId $customFields->get('acris_address_vat_id');
  162.         $addressVatIdNew = ($addressVatId) ?? null;
  163.         if (array_key_exists('customFields'$outputData)) {
  164.             $outputData['customFields']['acris_address_vat_id'] = $addressVatIdNew;
  165.         } else {
  166.             $outputData['customFields'] = ['acris_address_vat_id' => $addressVatIdNew];
  167.         }
  168.         $event->setOutput($outputData);
  169.     }
  170.     public function onAddressListingPageLoaded(AddressListingPageLoadedEvent $event): void
  171.     {
  172.         if (empty($event->getSalesChannelContext()->getCustomer()) || $event->getPage()->getAddresses()->count() === 0) return;
  173.         $this->vatIdValidationService->checkPageAddresses($event->getPage()->getAddresses(), $event->getSalesChannelContext()->getCustomer(), $event->getSalesChannelContext());
  174.     }
  175.     public function onCartConverted(CartConvertedEvent $event): void
  176.     {
  177.         if (empty($event->getSalesChannelContext()) || empty($event->getSalesChannelContext()->getCustomer())) return;
  178.         $customer $event->getSalesChannelContext()->getCustomer();
  179.         $convertedCart $event->getConvertedCart();
  180.         if (empty($customer->getCustomFields()) || !array_key_exists('orderCustomer'$convertedCart)) return;
  181.         $convertedCart['orderCustomer']['customFields'] = $customer->getCustomFields();
  182.         $event->setConvertedCart($convertedCart);
  183.     }
  184.     private function assignAddressVatId(?string $salesChannelId): bool
  185.     {
  186.         return $this->systemConfigService->get('AcrisTaxCS.config.vatIdAt'$salesChannelId) === VatIdValidationService::DEFAULT_OPTION_SAVE_ADDRESS_VAT_ID;
  187.     }
  188. }