custom/plugins/AbasBecoCustomizations/src/Subscribers/Customer/CustomerLoginSubscriber.php line 66

Open in your IDE?
  1. <?php
  2. namespace AbasBecoCustomizations\Subscribers\Customer;
  3. use Abas\AbasConnector\Service\RestApi\Client\AbasRestApiClient;
  4. use Abas\AbasConnector\Service\RestApi\Request\Factories\DataRequestFactory;
  5. use Abas\AbasConnector\Service\RestApi\Response\Content\ErpDataObject;
  6. use Cassandra\Exception\RuntimeException;
  7. use PHPUnit\TextUI\Exception;
  8. use Shopware\Core\Checkout\Customer\CustomerEvents;
  9. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  10. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  18. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  19. use Shopware\Core\System\SalesChannel\SalesChannel\SalesChannelContextSwitcher;
  20. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class CustomerLoginSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var EntityRepositoryInterface
  26.      */
  27.     private $currencyRepository;
  28.     /**
  29.      * @var EntityRepositoryInterface
  30.      */
  31.     private $customerRepository;
  32.     /**
  33.      * @var SalesChannelContextSwitcher
  34.      */
  35.     private $contextSwitcher;
  36.     /**
  37.      * @var AbasRestApiClient
  38.      */
  39.     private $apiClient;
  40.     public function __construct(
  41.         EntityRepositoryInterface $currencyRepository,
  42.         EntityRepositoryInterface $customerRepository,
  43.         SalesChannelContextSwitcher $contextSwitcher,
  44.         AbasRestApiClient $apiClient
  45.     ) {
  46.         $this->currencyRepository $currencyRepository;
  47.         $this->customerRepository $customerRepository;
  48.         $this->contextSwitcher $contextSwitcher;
  49.         $this->apiClient $apiClient;
  50.     }
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             CustomerLoginEvent::class => 'setCurrency',
  55.             CustomerLogoutEvent::class => 'resetCurrency'
  56.         ];
  57.     }
  58.     public function setCurrency(CustomerLoginEvent $event)
  59.     {
  60.         $customFields $event->getCustomer()->getCustomFields();
  61.         if($customFields && array_key_exists('abas_database_and_group'$customFields) && $customFields['abas_database_and_group'] === "0:1"){
  62.             $request DataRequestFactory::createGetRequest('0:1'$this->getCustSelectionIdentifier($event->getCustomer()), 'waehr^iso3a');
  63.             $response $this->apiClient->executeRequest($request);
  64.             $iso $response->getObject(0)->getFieldValue("waehr^iso3a");
  65.             $this->setCurrencyInContext($iso$event->getContext(), $event->getSalesChannelContext());
  66.         }
  67.     }
  68.     public function resetCurrency(CustomerLogoutEvent $event)
  69.     {
  70.         $this->setCurrencyInContext("EUR"$event->getContext(), $event->getSalesChannelContext());
  71.     }
  72.     private function setCurrencyInContext(string $isoContext $contextSalesChannelContext $salesChannelContext)
  73.     {
  74.         $params=[];
  75.         try{
  76.             $newCurrencyId $this->currencyRepository->search((new Criteria())->addFilter(new EqualsFilter("isoCode"$iso)), $context)->first()->getId();
  77.             if($newCurrencyId !== $salesChannelContext->getCurrencyId()) {
  78.                 $params["currencyId"] = $newCurrencyId;
  79.             }
  80.             $rounding = new CashRoundingConfig(30.001true);
  81.             $params['itemRoundings'] = $rounding;
  82.             $params['roundings'] = $rounding;
  83.             $data = new DataBag($params);
  84.             $this->contextSwitcher->update($data$salesChannelContext);
  85.             //Just to be sure
  86.             $salesChannelContext->getContext()->setRounding($rounding);
  87.             $salesChannelContext->setItemRounding($rounding);
  88.         }catch (Exception $e){
  89.             // Propably ISO is not in SW
  90.         }
  91.     }
  92.     /**
  93.      * Return the abas identifier value (GUID or ID)
  94.      * @param Entity $entity
  95.      * @return string|null
  96.      */
  97.     protected function getCustSelectionIdentifier(Entity $entity)
  98.     {
  99.         $guid $this->getCustomFieldValue($entity'abas_guid');
  100.         if ($guid) {
  101.             return 'guid==' $guid;
  102.         }
  103.         return 'id==' $this->getCustomFieldValue($entity'abas_id');
  104.     }
  105.     /**
  106.      * Get a customFields value
  107.      * @param Entity $entity
  108.      * @param string $key
  109.      * @return string|null
  110.      */
  111.     protected function getCustomFieldValue(Entity $entitystring $key): ?string
  112.     {
  113.         if ($entity->has('customFields')) {
  114.             $customFields $entity->get('customFields');
  115.             if ($customFields && isset($customFields[$key])) {
  116.                 return $customFields[$key];
  117.             }
  118.         }
  119.         return null;
  120.     }
  121. }