b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / dev2.destoffenstraat.com / app / code / Geissweb / Euvat / Plugin / Tax /
Filename/home/dev2.destoffenstraat.com/app/code/Geissweb/Euvat/Plugin/Tax/Calculation.php
Size9.03 kb
Permissionrwxrwxrwx
Ownerroot : root
Create time17-Aug-2025 10:26
Last modified09-Jul-2024 08:41
Last accessed22-Aug-2025 02:07
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?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
*/
declare(strict_types=1);

namespace Geissweb\Euvat\Plugin\Tax;

use Geissweb\Euvat\Helper\Configuration;
use Geissweb\Euvat\Helper\Functions;
use Geissweb\Euvat\Helper\Threshold\Calculator;
use Geissweb\Euvat\Logger\Logger;
use Geissweb\Euvat\Model\Validation;
use Geissweb\Euvat\Model\ValidationRepository;
use Geissweb\Euvat\Model\CaseIdentifier;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Class Calculation does all the tricky VAT stuff
*/
class Calculation
{
/**
* @var \Geissweb\Euvat\Model\Validation
*/
public Validation $validationModel;
/**
* @var \Geissweb\Euvat\Model\ValidationRepository
*/
public ValidationRepository $validationRepository;
/**
* @var \Geissweb\Euvat\Helper\Configuration
*/
public Configuration $configHelper;
/**
* @var \Geissweb\Euvat\Helper\Functions
*/
public Functions $functionsHelper;
/**
* @var \Geissweb\Euvat\Logger\Logger
*/
public Logger $logger;
/**
* @var \Magento\Framework\Api\ExtensibleDataObjectConverter
*/
public ExtensibleDataObjectConverter $converter;
/**
* @var \Magento\Customer\Model\Session
*/
public Session $customerSession;
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
private CustomerRepositoryInterface $customerRepository;
/**
* @var \Geissweb\Euvat\Helper\Threshold\Calculator
*/
private Calculator $thresholdCalculator;
/**
* @var \Geissweb\Euvat\Model\CaseIdentifier
*/
private CaseIdentifier $caseIdentifier;

/**
* TaxCalculation constructor.
*
* @param Validation $validationModel
* @param ValidationRepository $validationRepository
* @param Configuration $configHelper
* @param Functions $functionsHelper
* @param Logger $logger
* @param ExtensibleDataObjectConverter $converter
* @param Session $customerSession
* @param CustomerRepositoryInterface $customerRepository
* @param Calculator $thresholdCalculator
* @param \Geissweb\Euvat\Model\CaseIdentifier $caseIdentifier
*/
public function __construct(
Validation $validationModel,
ValidationRepository $validationRepository,
Configuration $configHelper,
Functions $functionsHelper,
Logger $logger,
ExtensibleDataObjectConverter $converter,
Session $customerSession,
CustomerRepositoryInterface $customerRepository,
Calculator $thresholdCalculator,
CaseIdentifier $caseIdentifier
) {
$this->validationModel = $validationModel;
$this->validationRepository = $validationRepository;
$this->configHelper = $configHelper;
$this->functionsHelper = $functionsHelper;
$this->logger = $logger;
$this->converter = $converter;
$this->customerSession = $customerSession;
$this->customerRepository = $customerRepository;
$this->thresholdCalculator = $thresholdCalculator;
$this->caseIdentifier = $caseIdentifier;
}

/**
* Check if customer is in no dynamic group
*
* @param int|null $customerId
* @return bool
*/
private function isNoDynamicCustomerGroup(?int $customerId): bool
{
if ($customerId === null || $customerId === 0) {
return false;
}
try {
if ($this->customerSession->isLoggedIn()) {
$currentCustomerGroup = $this->customerSession->getCustomer()->getGroupId();
} else { //for the admin create order
$customer = $this->customerRepository->getById($customerId);
$currentCustomerGroup = $customer->getGroupId();
}
$this->logger->customLog("aroundGetRateRequest Current group id: $currentCustomerGroup");
if ($this->configHelper->isNoDynamicGroup((int)$currentCustomerGroup)) {
$this->logger->customLog("aroundGetRateRequest return proceed because of no dynamic tax group");
return true;
}
} catch (NoSuchEntityException $e) {
return false;
} catch (LocalizedException $e) {
$this->logger->critical($e);
}

return false;
}

/**
* Modify the tax rate request as needed
*
* @param \Magento\Tax\Model\Calculation $subject
* @param callable $proceed
* @param null $shippingAddress
* @param null|\Magento\Customer\Model\Data\Address $billingAddress
* @param null $customerTaxClass
* @param null $store
* @param null $customerId
*
* @return mixed
*/
public function aroundGetRateRequest(
\Magento\Tax\Model\Calculation $subject,
callable $proceed,
$shippingAddress = null,
$billingAddress = null,
$customerTaxClass = null,
$store = null,
$customerId = null
) {
if (!$this->configHelper->getUseVatCalculation()
|| $this->isNoDynamicCustomerGroup((int)$customerId)
) {
$this->logger->customLog("[aroundGetRateRequest] dynamic tax class application is disabled.");
return $proceed($shippingAddress, $billingAddress, $customerTaxClass, $store, $customerId);
}

$this->caseIdentifier->setBillingAddress($billingAddress)
->setShippingAddress($shippingAddress)
->analyzeAddresses();
$taxClassToSet = $this->caseIdentifier->identifyCustomerTaxClass();
$taxDestination = $this->caseIdentifier->identifyTaxDestination();
if ($taxDestination !== false && is_object($shippingAddress)) {
$shippingAddress->setCountryId($taxDestination);
}

$shopCc = $this->configHelper->getMerchantCountryCode();
if ($shopCc !== 'GB'
&& $this->configHelper->isUkThresholdEnabled()
&& $taxClassToSet === $this->configHelper->getConsumerTaxClass()
) {
$this->thresholdCalculator->setConfigSection('brexit_settings');
$this->thresholdCalculator->setCurrentQuote();

if ($this->thresholdCalculator->isDeliveryToUk()
&& $this->thresholdCalculator->isCurrentCartAbove(
$this->configHelper->getUkThresholdValue(),
'GBP'
)
) {
$this->logger->customLog("[aroundGetRateRequest] UK Threshold exceeded");
$taxClassToSet = $this->configHelper->getExcludingTaxClass();
}
}

if ($this->configHelper->isEUThresholdEnabled()
&& (!$this->configHelper->isEuCountry($shopCc) || $shopCc === 'GB') //GB should be in EU country list for NI
&& $taxClassToSet === $this->configHelper->getConsumerTaxClass()
) {
$this->thresholdCalculator->setConfigSection('ioss_settings');
$this->thresholdCalculator->setCurrentQuote();

if ($this->thresholdCalculator->isDeliveryToEU()
&& !$this->thresholdCalculator->isDeliveryToNI()
&& $this->thresholdCalculator->isCurrentCartAbove(
$this->configHelper->getEUThresholdValue(),
'EUR'
)
) {
$this->logger->customLog("[aroundGetRateRequest] IOSS Threshold exceeded");
$taxClassToSet = $this->configHelper->getExcludingTaxClass();
}
}

$this->logger->customLog("[aroundGetRateRequest] evaluated class id: " . $taxClassToSet);
$returnRequest = $proceed($shippingAddress, $billingAddress, $taxClassToSet, $store, $customerId);

if ($taxDestination !== false
&& $this->configHelper->getEnableThresholdCountries()
&& $this->configHelper->isThresholdCountry($taxDestination)
&& $this->configHelper->isCbtEnabled()
&& $returnRequest->getCountryId() === $this->configHelper->getMerchantCountryCode()
) {
$this->logger->customLog("[aroundGetRateRequest] CBT enabled and threshold country $taxDestination.");
$returnRequest->setCountryId($taxDestination);
}

return $returnRequest;
}
}