Your IP : 127.0.0.1


Current Path : /home/dev2.destoffenstraat.com/app/code/Geissweb/Euvat-old/Observer/
Upload File :
Current File : /home/dev2.destoffenstraat.com/app/code/Geissweb/Euvat-old/Observer/QuoteAddressSaveBefore.php

<?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\Api\AddressRepositoryInterface;
use Magento\Customer\Model\Address;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

/**
 * Customer Observer Model
 */
class QuoteAddressSaveBefore implements ObserverInterface
{
    /**
     * @var ValidationRepository
     */
    public $validationRepository;

    /**
     * @var Logger
     */
    public $logger;
    /**
     * @var AddressRepositoryInterface
     */
    public $customerAddressRepository;

    /**
     * Constructor
     *
     * @param ValidationRepository       $validationRepository
     * @param Logger                     $logger
     * @param AddressRepositoryInterface $addressRepository
     */
    public function __construct(
        ValidationRepository $validationRepository,
        Logger $logger,
        AddressRepositoryInterface $addressRepository
    ) {
        $this->validationRepository = $validationRepository;
        $this->logger = $logger;
        $this->customerAddressRepository = $addressRepository;
    }

    /**
     * Address before save event handler
     * Adds VAT number validation data from table to address
     *
     * @param Observer $observer
     *
     * @return void
     */
    public function execute(Observer $observer)
    {
        $this->logger->debug("[QuoteAddressSaveBeforeObserver] START");

        /** @var $quoteAddress Address */
        $quoteAddress = $observer->getQuoteAddress();
        $vatId = $quoteAddress->getVatId();

        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 quote address id: " . $quoteAddress->getId());
                $quoteAddress->setVatIsValid($validation->getVatIsValid());
                $quoteAddress->setVatTraderName($validation->getVatTraderName());
                $quoteAddress->setVatTraderAddress($validation->getVatTraderAddress());
                $quoteAddress->setVatRequestSuccess($validation->getVatRequestSuccess());
                $quoteAddress->setVatRequestDate($validation->getVatRequestDate());
                $quoteAddress->setVatRequestId($validation->getVatRequestId());
            }
        } else {
            $this->logger->debug("No VAT number on quote address id: " . $quoteAddress->getId());
            $quoteAddress->setVatIsValid();
            $quoteAddress->setVatTraderName();
            $quoteAddress->setVatTraderAddress();
            $quoteAddress->setVatRequestSuccess();
            $quoteAddress->setVatRequestDate();
            $quoteAddress->setVatRequestId();
        }

        $this->logger->debug("[QuoteAddressSaveBeforeObserver] END");
    }
}