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\Helper;
use Geissweb\Euvat\Logger\Logger;
use Geissweb\Euvat\Validator\Syntax;
use Geissweb\Euvat\Model\ValidationRepository;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Framework\Intl\DateTimeFactory;
/**
* Class Functions
*/
class Functions
{
/**
* @var Configuration
*/
public $configHelper;
/**
* @var Logger
*/
public $logger;
/**
* @var ValidationRepository
*/
public $validationRepository;
/**
* @var DateTimeFactory
*/
public $dateTimeFactory;
/**
* @var Syntax
*/
public $syntaxValidator;
/**
* Configuration constructor.
*
* @param Configuration $configHelper
* @param Logger $logger
* @param ValidationRepository $validationRepository
* @param Syntax $syntaxValidator
* @param DateTimeFactory $dateTimeFactory
*/
public function __construct(
Configuration $configHelper,
Logger $logger,
ValidationRepository $validationRepository,
Syntax $syntaxValidator,
DateTimeFactory $dateTimeFactory
) {
$this->configHelper = $configHelper;
$this->logger = $logger;
$this->validationRepository = $validationRepository;
$this->dateTimeFactory = $dateTimeFactory;
$this->syntaxValidator = $syntaxValidator;
}
/**
* Estimates if we can reuse a existing validation
*
* @param $vatNumber
*
* @return bool
*/
public function getNeedToValidate($vatNumber)
{
$needToValidate = true;
$existingValidation = $this->getValidationData($vatNumber);
if ($existingValidation instanceof \Geissweb\Euvat\Api\Data\ValidationInterface) {
if ($this->configHelper->isPeriodicRevalidationEnabled()) {
//Estimate if we can reuse the existing validation
if ((bool)$existingValidation->getVatRequestSuccess()) {
$period = $this->configHelper->getRevalidationPeriod() * 30;
$validationDate = $this->dateTimeFactory->create($existingValidation->getVatRequestDate());
$now = $this->dateTimeFactory->create('now');
$daysSinceLast = $validationDate->diff($now)->days;
if ($daysSinceLast < $period) {
$needToValidate = false;
$this->logger->debug("getNeedToValidate got existing entry which is usable
($daysSinceLast days since last validation).");
}
} else {
$needToValidate = true;
}
}
if ($existingValidation->getVatRequestId() === 'OFFLINE'
|| $existingValidation->getVatRequestId() === 'INIT'
) {
$needToValidate = true;
}
}
$this->logger->debug("[getNeedToValidate] $vatNumber: ".(int)$needToValidate);
return $needToValidate;
}
/**
* Calculate the difference in months between two dates (v1 / 18.11.2013)
*
* @param \DateTime $date1
* @param \DateTime $date2
* @return int
*
* @thanks https://stackoverflow.com/users/766177/valentin-despa
*/
public function diffInMonths(\DateTime $date1, \DateTime $date2)
{
$diff = $date1->diff($date2);
$months = $diff->y * 12 + $diff->m + $diff->d / 30;
return (int)round($months);
}
/**
* @param $vatNumber
*
* @return bool|\Magento\Framework\Model\AbstractModel
*/
public function getValidationData($vatNumber)
{
return $this->validationRepository->getByVatId($vatNumber);
}
/**
* @param $order
* @param $vatBasedOn
*
* @return object|bool
*/
public function getBasedOnAddressFromOrder($order, $vatBasedOn)
{
foreach ($order->getAddresses() as $address) {
if ($address->getAddressType() == $vatBasedOn) {
return $address;
}
}
foreach ($order->getAddresses() as $address) {
if ($address->getAddressType() == 'billing') {
return $address;
}
}
return false;
}
/**
* @param AddressInterface|\Magento\Customer\Model\Address $address
* @param Validation|\Magento\Framework\Model\AbstractModel|
* \Geissweb\Euvat\Api\Data\ValidationInterface $vatValidation
* @return int
*/
public function getCustomerGroup($address, $vatValidation)
{
$this->logger->debug("[FunctionsHelper::Group Assignment]");
try {
if (!is_object($vatValidation)) {
if (is_object($address) && !$this->configHelper->isEuCountry($address->getCountryId())) {
$this->logger->debug("NON-Europe Group.");
return $this->configHelper->getTargetGroupOutsideEu();
}
return $this->configHelper->getTargetGroupDefault();
}
$vatdata = [
'address_country_id' => $address->getCountryId(),
'vat_country_id' => $vatValidation->getVatRequestCountryCode(),
'vat_id' => $vatValidation->getVatId(),
'vat_is_valid' => $vatValidation->getVatIsValid(),
'vat_request_success' => $vatValidation->getVatRequestSuccess()
];
$shopCc = $this->configHelper->getMerchantCountryCode();
if (isset($vatdata['address_country_id']) && !empty($vatdata['address_country_id'])) {
$customerCc = $vatdata['address_country_id'];
} elseif (isset($vatdata['vat_country_id']) && !empty($vatdata['vat_country_id'])) {
$customerCc = $vatdata['vat_country_id'];
// Fix Greece and Northern Ireland
if ($customerCc == "EL") {
$customerCc = "GR";
}
if ($customerCc == "XI") {
$customerCc = "GB";
}
} else {
$this->logger->debug("Default Group.");
return $this->configHelper->getTargetGroupDefault();
}
// Check the validation data and return best fitting group
if (isset($vatdata['vat_id']) && !empty($vatdata['vat_id'])) {
$this->logger->debug("Customer CC: $customerCc");
$this->logger->debug("VAT Number: " . $vatdata['vat_id']);
// Valid EU
if ($vatdata['vat_is_valid'] == true
&& $vatdata['vat_request_success'] == true
&& $shopCc != $customerCc
&& $this->configHelper->isEuCountry($customerCc)
) {
$this->logger->debug("Valid EU Group.");
return $this->configHelper->getTargetGroupEu();
//Valid Domestic
} elseif ($vatdata['vat_is_valid'] == true
&& $vatdata['vat_request_success'] == true
&& $shopCc == $customerCc
&& $this->configHelper->isEuCountry($customerCc)
) {
$this->logger->debug("Valid Domestic Group.");
return $this->configHelper->getTargetGroupDomestic();
//Invalid Number
} elseif ($vatdata['vat_is_valid'] == false
&& $vatdata['vat_request_success'] == true
) {
$this->logger->debug("Invalid Number Group.");
return $this->configHelper->getTargetGroupInvalid();
//Request fail
} elseif ($vatdata['vat_request_success'] == false) {
$this->logger->debug("Technical Error Group.");
return $this->configHelper->getTargetGroupErrors();
//non-EU
} else {
if (!$this->configHelper->isEuCountry($customerCc)) {
$this->logger->debug("NON-Europe Group.");
return $this->configHelper->getTargetGroupOutsideEu();
}
}
} else {// No validation data, try to identify country outside EU
if (!$this->configHelper->isEuCountry($customerCc)) {
$this->logger->debug("NON-Europe Group");
return $this->configHelper->getTargetGroupOutsideEu();
}
}
$this->logger->debug("Default Group.");
return $this->configHelper->getTargetGroupDefault();
} catch (\Exception $e) {
$this->logger->critical($e);
$this->logger->debug("Default Group :x");
return $this->configHelper->getTargetGroupDefault();
}
}
}