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\Validation;
use Geissweb\Euvat\Model\ValidationRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Event\Observer;
use Magento\Framework\Exception\LocalizedException;
/**
* Class VatValidationAfter
* Creates or updates VAT number validation data
*/
class VatValidationAfter implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var Validation
*/
public $validationModel;
/**
* @var ValidationRepository
*/
public $validationRepository;
/**
* @var SearchCriteriaBuilder
*/
public $search;
/**
* @var Logger
*/
private $logger;
/**
* Constructor
*
* @param Validation $validationModel
* @param ValidationRepository $validationRepository
* @param Logger $logger
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
Validation $validationModel,
ValidationRepository $validationRepository,
Logger $logger,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->validationModel = $validationModel;
$this->validationRepository = $validationRepository;
$this->search = $searchCriteriaBuilder;
$this->logger = $logger;
}
/**
* Save validation on customer address
*
* @param Observer $observer
*
* @return void
* @throws LocalizedException
*/
public function execute(Observer $observer)
{
$this->logger->debug("VatValidationAfter Start");
/** @var \Geissweb\Euvat\Model\Validation\Result $result */
$result = $observer->getEvent()->getValidationResult();
$searchCriteria = $this->search->addFilter(
'vat_id',
$result->getVatRequestCountryCode() . $result->getVatId(),
'eq'
)->create();
$validationList = $this->validationRepository->getList($searchCriteria);
$items = $validationList->getItems();
$this->logger->debug("VatValidationAfter checking VAT number: "
. $result->getVatRequestCountryCode() . $result->getVatId());
if ($validationList->getTotalCount() > 0) {
$this->logger->debug("VatValidationAfter updating validation information.");
foreach ($items as $item) {
$item->setHandle($result->getHandle());
$item->setVatId($result->getVatRequestCountryCode() . $result->getVatId());
$item->setVatIsValid($result->getVatIsValid());
$item->setVatRequestDate($result->getVatRequestDate());
$item->setVatRequestId($result->getVatRequestId());
$item->setVatRequestSuccess($result->getVatRequestSuccess());
$item->setVatTraderName($result->getVatTraderName());
$item->setVatTraderAddress($result->getVatTraderAddress());
$item->setRequestMessage($result->getRequestMessage());
try {
$this->validationRepository->save($item);
} catch (LocalizedException $exc) {
$this->logger->critical($exc);
}
}
} else {
$this->logger->debug("VatValidationAfter creating validation information.");
$this->validationModel->clearInstance();
$this->validationModel->setHandle($result->getHandle());
$this->validationModel->setVatId($result->getVatRequestCountryCode() . $result->getVatId());
$this->validationModel->setVatIsValid($result->getVatIsValid());
$this->validationModel->setVatRequestDate($result->getVatRequestDate());
$this->validationModel->setVatRequestId($result->getVatRequestId());
$this->validationModel->setVatRequestSuccess($result->getVatRequestSuccess());
$this->validationModel->setVatTraderName($result->getVatTraderName());
$this->validationModel->setVatTraderAddress($result->getVatTraderAddress());
$this->validationModel->setRequestMessage($result->getRequestMessage());
try {
$this->validationRepository->save($this->validationModel);
} catch (LocalizedException $exc) {
$this->logger->critical($exc);
}
}
$this->logger->debug("VatValidationAfter End");
}
}