Your IP : 127.0.0.1
<?php
/**
* ||GEISSWEB| EU VAT Enhanced
*
* NOTICE OF LICENSE
*
* This source file is subject to the GEISSWEB End User License Agreement
* that is available through the world-wide-web at this URL: https://www.geissweb.de/legal-information/eula
*
* DISCLAIMER
*
* Do not edit this file if you wish to update the extension in the future. If you wish to customize the extension
* for your needs please refer to our support for more information.
*
* @copyright Copyright (c) 2015 GEISS Weblösungen (https://www.geissweb.de)
* @license https://www.geissweb.de/legal-information/eula GEISSWEB End User License Agreement
*/
namespace Geissweb\Euvat\Observer;
use Geissweb\Euvat\Helper\Configuration;
use Geissweb\Euvat\Helper\Functions;
use Geissweb\Euvat\Logger\Logger;
use Geissweb\Euvat\Model\ValidationFactory;
use Geissweb\Euvat\Model\ValidationRepository;
use Magento\Customer\Model\Address;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
/**
* Class AfterAddressSaveObserver
* Provides the new preference for Magento core observer
* @todo VIV_PROCESSED_FLAG registry stuff
*/
class AfterAddressSaveObserver implements ObserverInterface
{
/**
* VAT ID validation processed flag code
*/
const VIV_PROCESSED_FLAG = 'viv_after_address_save_processed';
/**
* @var Configuration
*/
public $configHelper;
/**
* @var ValidationRepository
*/
public $validationRepository;
/**
* @var Functions
*/
public $functionsHelper;
/**
* @var ValidationFactory
*/
public $validationFactory;
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
public $customerRepository;
/**
* @var Logger
*/
public $logger;
/**
* @var ResourceConnection
*/
public $db;
/**
* @var Registry
*/
protected $_coreRegistry;
/**
* @var CustomerSession
*/
private $customerSession;
/**
* @param Registry $coreRegistry
* @param CustomerSession $customerSession
* @param Configuration $configHelper
* @param Functions $functionsHelper
* @param ValidationRepository $validationRepository
* @param ValidationFactory $validationFactory
* @param ResourceConnection $db
* @param Logger $logger
*/
public function __construct(
Registry $coreRegistry,
CustomerSession $customerSession,
Configuration $configHelper,
Functions $functionsHelper,
ValidationRepository $validationRepository,
ValidationFactory $validationFactory,
ResourceConnection $db,
Logger $logger
) {
$this->_coreRegistry = $coreRegistry;
$this->customerSession = $customerSession;
$this->configHelper = $configHelper;
$this->functionsHelper = $functionsHelper;
$this->validationRepository = $validationRepository;
$this->validationFactory = $validationFactory;
$this->logger = $logger;
$this->db = $db;
}
/**
* @param \Magento\Framework\Event\Observer $observer
*
* @throws LocalizedException
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->logger->debug("[AfterAddressSaveObserver] START");
/** @var $customerAddress Address */
$customerAddress = $observer->getCustomerAddress();
$customer = $customerAddress->getCustomer();
if (!$this->configHelper->getUseGroupAssignment()
|| $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG)
|| !$this->_isDefaultBilling($customerAddress)
) {
$this->logger->debug("Aborting");
$this->logger->debug("[AfterAddressSaveObserver] END");
return;
}
try {
$this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true);
$vatId = $customerAddress->getVatId();
$this->logger->debug("Getting Validation Information for: " . $vatId);
/** @var \Geissweb\Euvat\Api\Data\ValidationInterface $vatValidation */
$vatValidation = $this->validationRepository->getByVatId($vatId);
if (!$vatValidation) {
$this->logger->debug("Found NO validation information for: " . $vatId);
$vatValidation = $this->validationFactory->create();
}
$isDisableAutoGroupChange = $customer->getDisableAutoGroupChange();
$currentGroupId = $customer->getGroupId();
$this->logger->debug("Current group: $currentGroupId");
$isExcludedGroup = in_array($currentGroupId, $this->configHelper->getExcludedGroups());
if (!$isDisableAutoGroupChange && !$isExcludedGroup) {
$groupId = $this->functionsHelper->getCustomerGroup($customerAddress, $vatValidation);
$this->logger->debug("Estimated new group: $groupId");
if ($currentGroupId != $groupId) {
$this->logger->debug("Assigning group $groupId");
try {
$this->db->getConnection()->beginTransaction();
$this->db->getConnection()->update(
$this->db->getTableName('customer_entity'),
['group_id' => $groupId],
['entity_id = ?' => (int)$customer->getId()]
);
$this->db->getConnection()->commit();
$this->customerSession->setCustomerGroupId($groupId);
} catch (\Exception $e) {
$this->db->getConnection()->rollBack();
$this->logger->debug("Critical DB Exception: " . $e->getMessage());
}
}
} else {
$this->logger->debug("Customer auto group change is disabled: " . $isDisableAutoGroupChange);
$this->logger->debug("Customer group is excluded: " . $isExcludedGroup);
}
$this->logger->debug("[AfterAddressSaveObserver] END");
} catch (\Exception $e) {
$this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true);
$this->logger->debug("[AfterAddressSaveObserver] END WITH EXCEPTION: " . $e->getMessage());
}
}
/**
* @param Address $address
*
* @return bool
* @throws LocalizedException
*/
protected function _isDefaultBilling($address)
{
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling()
|| $address->getIsPrimaryBilling()
|| $address->getIsDefaultBilling();
}
/**
* @param Address $address
*
* @return bool
* @throws LocalizedException
*/
protected function _isDefaultShipping($address)
{
return $address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping()
|| $address->getIsPrimaryShipping()
|| $address->getIsDefaultShipping();
}
}