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\Logger\Logger;
use Geissweb\Euvat\Model\ValidationRepository;
use Magento\Customer\Model\Address;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\ObserverInterface;
/**
* Customer Observer Model
*/
class BeforeAddressSaveObserver implements ObserverInterface
{
/**
* @var ValidationRepository
*/
private $validationRepository;
/**
* @var Logger
*/
private $logger;
/**
* @var RequestInterface
*/
private $request;
/**
* Constructor
*
* @param ValidationRepository $validationRepository
* @param Logger $logger
*/
public function __construct(
ValidationRepository $validationRepository,
RequestInterface $request,
Logger $logger
) {
$this->validationRepository = $validationRepository;
$this->logger = $logger;
$this->request = $request;
}
/**
* Address before save event handler
* Adds VAT number validation data from table to address
*
* @param \Magento\Framework\Event\Observer $observer
*
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->logger->debug("[BeforeAddressSaveObserver] START");
/** @var $customerAddress Address */
$customerAddress = $observer->getCustomerAddress();
$vatId = $customerAddress->getVatId();
if ($this->request->getParam('vat_id') === '' && !empty($vatId)
&& $this->request->getParam('entity_id') == $customerAddress->getId()
) {
$this->logger->debug("Workaround when VAT number was removed but persists in observer: $vatId");
$vatId = null;
}
if (!empty($vatId)) {
$this->logger->debug("Searching for validation: $vatId");
/** @var \Geissweb\Euvat\Api\Data\ValidationInterface $validation */
$validation = $this->validationRepository->getByVatId($vatId);
if ($validation) {
$this->logger->debug("Applying validation data to address id: " . $customerAddress->getId());
$customerAddress->setVatIsValid($validation->getVatIsValid());
$customerAddress->setVatTraderName($validation->getVatTraderName());
$customerAddress->setVatTraderAddress($validation->getVatTraderAddress());
$customerAddress->setVatRequestSuccess($validation->getVatRequestSuccess());
$customerAddress->setVatRequestDate($validation->getVatRequestDate());
$customerAddress->setVatRequestId($validation->getVatRequestId());
}
} else {
$this->logger->debug("No VAT number on address id: " . $customerAddress->getId());
$customerAddress->setVatIsValid();
$customerAddress->setVatTraderName();
$customerAddress->setVatTraderAddress();
$customerAddress->setVatRequestSuccess();
$customerAddress->setVatRequestDate();
$customerAddress->setVatRequestId();
}
$this->logger->debug("[BeforeAddressSaveObserver] END");
}
}