<?php
namespace AbasBecoCustomizations\Subscribers\Customer;
use Abas\AbasConnector\Service\RestApi\Client\AbasRestApiClient;
use Abas\AbasConnector\Service\RestApi\Request\Factories\DataRequestFactory;
use Abas\AbasConnector\Service\RestApi\Response\Content\ErpDataObject;
use Cassandra\Exception\RuntimeException;
use PHPUnit\TextUI\Exception;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
use Shopware\Core\System\SalesChannel\SalesChannel\SalesChannelContextSwitcher;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomerLoginSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $currencyRepository;
/**
* @var EntityRepositoryInterface
*/
private $customerRepository;
/**
* @var SalesChannelContextSwitcher
*/
private $contextSwitcher;
/**
* @var AbasRestApiClient
*/
private $apiClient;
public function __construct(
EntityRepositoryInterface $currencyRepository,
EntityRepositoryInterface $customerRepository,
SalesChannelContextSwitcher $contextSwitcher,
AbasRestApiClient $apiClient
) {
$this->currencyRepository = $currencyRepository;
$this->customerRepository = $customerRepository;
$this->contextSwitcher = $contextSwitcher;
$this->apiClient = $apiClient;
}
public static function getSubscribedEvents()
{
return [
CustomerLoginEvent::class => 'setCurrency',
CustomerLogoutEvent::class => 'resetCurrency'
];
}
public function setCurrency(CustomerLoginEvent $event)
{
$customFields = $event->getCustomer()->getCustomFields();
if($customFields && array_key_exists('abas_database_and_group', $customFields) && $customFields['abas_database_and_group'] === "0:1"){
$request = DataRequestFactory::createGetRequest('0:1', $this->getCustSelectionIdentifier($event->getCustomer()), 'waehr^iso3a');
$response = $this->apiClient->executeRequest($request);
$iso = $response->getObject(0)->getFieldValue("waehr^iso3a");
$this->setCurrencyInContext($iso, $event->getContext(), $event->getSalesChannelContext());
}
}
public function resetCurrency(CustomerLogoutEvent $event)
{
$this->setCurrencyInContext("EUR", $event->getContext(), $event->getSalesChannelContext());
}
private function setCurrencyInContext(string $iso, Context $context, SalesChannelContext $salesChannelContext)
{
$params=[];
try{
$newCurrencyId = $this->currencyRepository->search((new Criteria())->addFilter(new EqualsFilter("isoCode", $iso)), $context)->first()->getId();
if($newCurrencyId !== $salesChannelContext->getCurrencyId()) {
$params["currencyId"] = $newCurrencyId;
}
$rounding = new CashRoundingConfig(3, 0.001, true);
$params['itemRoundings'] = $rounding;
$params['roundings'] = $rounding;
$data = new DataBag($params);
$this->contextSwitcher->update($data, $salesChannelContext);
//Just to be sure
$salesChannelContext->getContext()->setRounding($rounding);
$salesChannelContext->setItemRounding($rounding);
}catch (Exception $e){
// Propably ISO is not in SW
}
}
/**
* Return the abas identifier value (GUID or ID)
* @param Entity $entity
* @return string|null
*/
protected function getCustSelectionIdentifier(Entity $entity)
{
$guid = $this->getCustomFieldValue($entity, 'abas_guid');
if ($guid) {
return 'guid==' . $guid;
}
return 'id==' . $this->getCustomFieldValue($entity, 'abas_id');
}
/**
* Get a customFields value
* @param Entity $entity
* @param string $key
* @return string|null
*/
protected function getCustomFieldValue(Entity $entity, string $key): ?string
{
if ($entity->has('customFields')) {
$customFields = $entity->get('customFields');
if ($customFields && isset($customFields[$key])) {
return $customFields[$key];
}
}
return null;
}
}