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/Config.php
Size23.16 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\Api\Data\ValidationInterface;
use Geissweb\Euvat\Helper\Configuration;
use Geissweb\Euvat\Helper\Threshold\Calculator;
use Geissweb\Euvat\Logger\Logger;
use Geissweb\Euvat\Model\System\Config\Source\DynamicShipping;
use Geissweb\Euvat\Model\ValidationRepository;
use Geissweb\Euvat\Registry\CronQuoteId;
use Magento\Backend\Model\Session\Quote;
use Magento\Customer\Model\Group;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Address\AddressModelInterface as CustomerAddressInterface;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface as QuoteAddressInterface;
use Magento\Tax\Model\Config as TaxModelConfig;
use Magento\Catalog\Model\Product\Type;

/**
* Class Config
* Makes some store configuration variables dynamic
*/
class Config
{
/**
* @var Configuration
*/
public $configHelper;
/**
* @var ValidationRepository
*/
public $validationRepository;
/**
* @var \Magento\Checkout\Model\Session
*/
public $checkoutSession;
/**
* @var Logger
*/
public $logger;
/**
* @var CartRepositoryInterface
*/
public $cartRepository;
/**
* @var Session
*/
public $customerSession;
/**
* @var int
*/
public $_customerGroup;
/**
* @var array<mixed>
*/
public $_cartProductDisplayRules;
/**
* @var array<mixed>
*/
public $_catalogProductDisplayRules;
/**
* @var array<mixed>
*/
public $_cartSubtotalDisplayRules;
/**
* @var State
*/
private $appState;
/**
* @var Quote
*/
private $backendSessionQuote;
/**
* @var Calculator
*/
private $thresholdCalculator;
/**
* @var CronQuoteId
*/
private $cronQuoteIdRegistry;

/**
* TaxConfig constructor.
*
* @param Configuration $configHelper
* @param ValidationRepository $validationRepository
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param Session $customerSession
* @param CartRepositoryInterface $cartRepository
* @param State $appState
* @param Quote $backendSessionQuote
* @param Logger $logger
* @param Calculator $thresholdCalc
* @param CronQuoteId $cronQuoteIdRegistry
*
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function __construct(
Configuration $configHelper,
ValidationRepository $validationRepository,
\Magento\Checkout\Model\Session $checkoutSession,
Session $customerSession,
CartRepositoryInterface $cartRepository,
State $appState,
Quote $backendSessionQuote,
Logger $logger,
Calculator $thresholdCalc,
CronQuoteId $cronQuoteIdRegistry
) {
$this->configHelper = $configHelper;
$this->validationRepository = $validationRepository;
$this->checkoutSession = $checkoutSession;
$this->customerSession = $customerSession;
$this->cartRepository = $cartRepository;
$this->logger = $logger;
$this->appState = $appState;
$this->backendSessionQuote = $backendSessionQuote;
$this->thresholdCalculator = $thresholdCalc;
$this->cronQuoteIdRegistry = $cronQuoteIdRegistry;

//$this->_customerGroup = $this->customerSession->getCustomer()->getGroupId();
$this->_customerGroup = $this->customerSession->getCustomerGroupId();
$this->_catalogProductDisplayRules = $this->configHelper->getCatalogPriceDisplayTypeRules();
$this->_cartProductDisplayRules = $this->configHelper->getCartProductPriceDisplayTypeRules();
$this->_cartSubtotalDisplayRules = $this->configHelper->getCartSubtotalPriceDisplayTypeRules();
}

/**
* Runs afterDisplayCartSubtotalInclTax
*
* @param TaxModelConfig $subject
* @param bool $result
*
* @return bool
*/
public function afterDisplayCartSubtotalInclTax(TaxModelConfig $subject, bool $result): bool
{
$this->logger->customLog('afterDisplayCartSubtotalInclTax default result: ' . (int)$result);
$this->logger->customLog('afterDisplayCartSubtotalInclTax current group: ' . $this->_customerGroup);
if (array_key_exists($this->_customerGroup, $this->_cartSubtotalDisplayRules)) {
$type = $this->_cartSubtotalDisplayRules[$this->_customerGroup];
return $subject::DISPLAY_TYPE_INCLUDING_TAX === $type;
}
return $result;
}

/**
* Runs afterDisplayCartSubtotalExclTax
*
* @param TaxModelConfig $subject
* @param bool $result
*
* @return bool
*/
public function afterDisplayCartSubtotalExclTax(TaxModelConfig $subject, bool $result): bool
{
$this->logger->customLog('afterDisplayCartSubtotalExclTax default result: ' . (int)$result);
$this->logger->customLog('afterDisplayCartSubtotalExclTax current group: ' . $this->_customerGroup);
if (array_key_exists($this->_customerGroup, $this->_cartSubtotalDisplayRules)) {
$type = $this->_cartSubtotalDisplayRules[$this->_customerGroup] ?? null;
return $subject::DISPLAY_TYPE_EXCLUDING_TAX === $type;
}
return $result;
}

/**
* Runs afterDisplayCartSubtotalBoth
*
* @param TaxModelConfig $subject
* @param bool $result
*
* @return bool
*/
public function afterDisplayCartSubtotalBoth(TaxModelConfig $subject, bool $result): bool
{
$this->logger->customLog('afterDisplayCartSubtotalBoth default result: ' . (int)$result);
$this->logger->customLog('afterDisplayCartSubtotalBoth current group: ' . $this->_customerGroup);
if (array_key_exists($this->_customerGroup, $this->_cartSubtotalDisplayRules)) {
$type = $this->_cartSubtotalDisplayRules[$this->_customerGroup] ?? null;
return $subject::DISPLAY_TYPE_BOTH === $type;
}
return $result;
}

/**
* Runs afterDisplayCartPricesInclTax
*
* @param TaxModelConfig $subject
* @param bool $result
*
* @return bool
*/
public function afterDisplayCartPricesInclTax(TaxModelConfig $subject, bool $result): bool
{
$this->logger->customLog('afterDisplayCartPricesInclTax default result: ' . (int)$result);
$this->logger->customLog('afterDisplayCartPricesInclTax current group: ' . $this->_customerGroup);
if (array_key_exists($this->_customerGroup, $this->_cartProductDisplayRules)) {
$type = $this->_cartProductDisplayRules[$this->_customerGroup] ?? null;
$this->logger->customLog('afterDisplayCartPricesInclTax found rule: ' . $type);
return $subject::DISPLAY_TYPE_INCLUDING_TAX === $type;
}
return $result;
}

/**
* Runs afterDisplayCartPricesExclTax
*
* @param TaxModelConfig $subject
* @param bool $result
*
* @return bool
*/
public function afterDisplayCartPricesExclTax(TaxModelConfig $subject, bool $result): bool
{
$this->logger->customLog('afterDisplayCartPricesExclTax default result: ' . (int)$result);
$this->logger->customLog('afterDisplayCartPricesExclTax current group: ' . $this->_customerGroup);
if (array_key_exists($this->_customerGroup, $this->_cartProductDisplayRules)) {
$type = $this->_cartProductDisplayRules[$this->_customerGroup] ?? null;
$this->logger->customLog('afterDisplayCartPricesExclTax found rule: ' . $type);
return $subject::DISPLAY_TYPE_EXCLUDING_TAX === $type;
}
return $result;
}

/**
* Runs afterDisplayCartPricesBoth
*
* @param TaxModelConfig $subject
* @param bool $result
*
* @return bool
*/
public function afterDisplayCartPricesBoth(TaxModelConfig $subject, bool $result): bool
{
$this->logger->customLog('afterDisplayCartPricesBoth default result: ' . (int)$result);
$this->logger->customLog('afterDisplayCartPricesBoth current group: ' . $this->_customerGroup);
if (array_key_exists($this->_customerGroup, $this->_cartProductDisplayRules)) {
$type = $this->_cartProductDisplayRules[$this->_customerGroup] ?? null;
$this->logger->customLog('afterDisplayCartPricesBoth found rule: ' . $type);
return $subject::DISPLAY_TYPE_BOTH === $type;
}
return $result;
}

/**
* Dynamic catalog price display
*
* 1 - Excluding tax
* 2 - Including tax
* 3 - Both
*
* @param TaxModelConfig $subject
* @param int $type
*
* @return int
*/
public function afterGetPriceDisplayType(TaxModelConfig $subject, int $type): int
{
$this->logger->customLog('afterGetPriceDisplayType current group: ' . $this->_customerGroup);
if (array_key_exists($this->_customerGroup, $this->_catalogProductDisplayRules)) {
$this->logger->customLog('afterGetPriceDisplayType found rule: ' . $type);
$type = $this->_catalogProductDisplayRules[$this->_customerGroup];
}
$this->logger->customLog('afterGetPriceDisplayType return ' . $type);
return $type;
}

/**
* Runs afterCrossBorderTradeEnabled
*
* @param TaxModelConfig $subject
* @param bool $result
*
* @return bool
*/
public function afterCrossBorderTradeEnabled(TaxModelConfig $subject, bool $result): bool
{
try {
if ($result) {
//Backend
if ($this->appState->getAreaCode() === Area::AREA_ADMINHTML) {
$this->logger->customLog("afterCrossBorderTradeEnabled processing AdminOrder");
$backendQuote = $this->backendSessionQuote->getQuote();
// To have the correct prices in case the group is for including tax
if ($this->configHelper->isNoDynamicGroup((int)$backendQuote->getCustomerGroupId())) {
return true;
}
$billingAddress = $backendQuote->getBillingAddress();
$shippingAddress = $backendQuote->getShippingAddress();

//Frontend
} else {
// To have the correct prices in case the group is for including tax
$currentCustomerGroup = ($this->customerSession->isLoggedIn()) ?
(int)$this->customerSession->getCustomer()->getGroupId() : Group::NOT_LOGGED_IN_ID;
if ($this->configHelper->isNoDynamicGroup($currentCustomerGroup)) {
return true;
}

$quoteId = $this->checkoutSession->getQuoteId();
if (!empty($quoteId)) {
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->cartRepository->get($quoteId);
$billingAddress = $quote->getBillingAddress();
$shippingAddress = $quote->getShippingAddress();
} else {
$billingAddress = $this->customerSession->getCustomer()->getDefaultBillingAddress();
$shippingAddress = $this->customerSession->getCustomer()->getDefaultShippingAddress();
}
}

if ((!$billingAddress instanceof QuoteAddressInterface || !$billingAddress instanceof CustomerAddressInterface)
|| (!$shippingAddress instanceof QuoteAddressInterface || !$shippingAddress instanceof CustomerAddressInterface)
) {
$this->logger->customLog("afterCrossBorderTradeEnabled no addresses found, return");
return true;
}

$countryId = $shippingAddress->getCountryId();
$vatNumber = $billingAddress->getVatId();

if (empty($countryId)) {
return true;
}

$this->logger->customLog("afterCrossBorderTradeEnabled isEuCountry($countryId): "
. (int)$this->configHelper->isEuCountry($countryId));

// GB should still be in the EU countries list
if ($this->configHelper->getDisableCbtForOutOfEurope()
&& !$this->configHelper->isEuCountry($countryId)
) {
$this->logger->customLog("afterCrossBorderTradeEnabled disableCbtForNonEurope");
$result = false;
}

// When used as non-EU company delivering to EU and threshold is exceeded
if ($this->configHelper->isEUThresholdEnabled()
&& $this->configHelper->getDisableCbtForIOSS()
) {
$this->thresholdCalculator->setConfigSection('ioss_settings');
if ($this->thresholdCalculator->isDeliveryToEU()
&& $this->thresholdCalculator->isCurrentCartAbove(
$this->configHelper->getEUThresholdValue(),
'EUR'
)
) {
$this->logger->customLog("afterCrossBorderTradeEnabled disableCbtForIOSS");
$result = false;
}
}

// When used as EU company delivering to UK
if ($this->configHelper->isUkThresholdEnabled()
&& $this->configHelper->getDisableCbtForBrexit()
&& $this->configHelper->getMerchantCountryCode() !== 'GB'
) {
$this->thresholdCalculator->setConfigSection('brexit_settings');
if ($this->thresholdCalculator->isDeliveryToUk()
&& $this->thresholdCalculator->isCurrentCartAbove(
$this->configHelper->getUkThresholdValue(),
'GBP'
)
) {
$this->logger->customLog("afterCrossBorderTradeEnabled brexitThreshold");
$result = false;
}
}

// For valid EU business
if (!empty($vatNumber) && $this->configHelper->getDisableCbtForEuBusiness()) {
$this->logger->customLog("afterCrossBorderTradeEnabled !empty($vatNumber)");
/** @var ValidationInterface $validation */
$validation = $this->validationRepository->getByVatId($vatNumber);
if ($validation instanceof ValidationInterface && $validation->getVatIsValid()) {
$this->logger->customLog("afterCrossBorderTradeEnabled disableCbtForEuBusiness");
$result = false;
}
}
}
$this->logger->customLog("afterCrossBorderTradeEnabled result: " . (int)$result);
return $result;
} catch (\Exception $e) {
$this->logger->critical($e);
}
return $result;
}

/**
* Runs afterGetShippingTaxClass
*
* @param TaxModelConfig $subject
* @param int $result
*
* @return int
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function afterGetShippingTaxClass(TaxModelConfig $subject, int $result): int
{
$this->logger->customLog("afterGetShippingTaxClass START result: $result");
$useDynamicShippingTax = $this->configHelper->getUseDynamicShippingTaxClass();
$defaultProductTaxClass = $this->configHelper->getDefaultProductTaxClass();
$defaultShippingTaxClass = $this->configHelper->getDefaultShippingTaxClass();
$reducedProductClass = $this->configHelper->getReducedProductTaxClass();
$reducedShippingClass = $this->configHelper->getReducedShippingTaxClass();
$superReducedProductClass = $this->configHelper->getSuperReducedProductTaxClass();
$superReducedShippingClass = $this->configHelper->getSuperReducedShippingTaxClass();
$this->logger->customLog("afterGetShippingTaxClass classes", [
'default product class' => $defaultProductTaxClass,
'default shipping class' => $defaultShippingTaxClass,
'reduced product class' => $reducedProductClass,
'reduced shipping class' => $reducedShippingClass,
'super reduced product class' => $superReducedProductClass,
'super reduced shipping class' => $superReducedShippingClass
]);

if ($useDynamicShippingTax > 0 && $reducedProductClass && $reducedShippingClass) {
$this->logger->customLog("afterGetShippingTaxClass useDynamicShippingTax");

if ($this->appState->getAreaCode() === Area::AREA_ADMINHTML) {
$this->logger->customLog("afterGetShippingTaxClass processing AdminOrder");
$items = $this->backendSessionQuote->getQuote()->getAllItems();
} elseif ($this->appState->getAreaCode() === Area::AREA_CRONTAB) {
$this->logger->customLog("afterGetShippingTaxClass processing CronJob");
$cronQuoteId = $this->cronQuoteIdRegistry->get();
if ($cronQuoteId !== null) {
/**@var \Magento\Quote\Model\Quote $quote */
$quote = $this->cartRepository->get($cronQuoteId);
$items = $quote->getAllItems(); // @phpstan-ignore-line
}
} else {
$this->logger->customLog("afterGetShippingTaxClass processing FrontQuote");
$quoteId = $this->checkoutSession->getQuoteId();
if (!empty($quoteId)) {
/**@var \Magento\Quote\Model\Quote $quote */
$quote = $this->cartRepository->get($quoteId);
$items = $quote->getAllItems(); // @phpstan-ignore-line
}
}

if (!isset($items)) {
$this->logger->customLog("afterGetShippingTaxClass no items found");
return $result;
}

switch ($useDynamicShippingTax) {
case DynamicShipping::TYPE_BY_RATE_WITH_LARGEST_TOTAL:
$this->logger->customLog("afterGetShippingTaxClass TYPE_BY_RATE_WITH_LARGEST_TOTAL");
$totals = [];
foreach ($items as $item) {
if ($item->getProductType() === Type::TYPE_BUNDLE) {
continue;
}
$productTaxClassId = (int)$item->getProduct()->getTaxClassId();
$this->logger->customLog(sprintf(
"Type: %s **** rowTotal: %s **** taxClass: %s",
$item->getProductType(),
$item->getRowTotal(),
$productTaxClassId
), $item->getData());

if (isset($totals[$productTaxClassId])) {
$totals[$productTaxClassId] += $item->getRowTotal();
} else {
$totals[$productTaxClassId] = $item->getRowTotal();
}
}
$this->logger->customLog("afterGetShippingTaxClass calculated totals", $totals);

$arsortTotals = $asortTotals = $totals;
asort($asortTotals);
arsort($arsortTotals);

if (count($totals) > 1
&& array_values($asortTotals) === array_values($arsortTotals)
) {
$classToUse = $result;
} else {
reset($arsortTotals);
$classToUse = key($arsortTotals);
}
$this->logger->customLog("afterGetShippingTaxClass calculated product class to use: $classToUse");

if ($classToUse === $reducedProductClass) {
$this->logger->customLog("afterGetShippingTaxClass using reduced
shipping class: $reducedShippingClass");
return $reducedShippingClass;
}
if ($classToUse === $superReducedProductClass) {
$this->logger->customLog("afterGetShippingTaxClass using super reduced
shipping class: $superReducedShippingClass");
return $superReducedShippingClass;
}
$this->logger->customLog("afterGetShippingTaxClass using default shipping class: $defaultShippingTaxClass");
break;

case DynamicShipping::TYPE_BY_RATE_WITH_HIGHEST_RATE:
$this->logger->customLog("afterGetShippingTaxClass TYPE_BY_RATE_WITH_HIGHEST_RATE");
$classes = [];
foreach ($items as $item) {
if ($item->getProductType() === Type::TYPE_BUNDLE) {
continue;
}
$productTaxClassId = (int)$item->getProduct()->getTaxClassId();
$this->logger->customLog(sprintf(
"Type: %s **** taxClass: %s",
$item->getProductType(),
$productTaxClassId
), $item->getData());

$classes[] = $productTaxClassId;
}
$this->logger->customLog("afterGetShippingTaxClass classes for cart items", $classes);
if (in_array($defaultProductTaxClass, $classes)) {
$this->logger->customLog("afterGetShippingTaxClass using default shipping class");
break;
}
if (in_array($reducedProductClass, $classes)) {
$this->logger->customLog("afterGetShippingTaxClass using reduced shipping: $reducedShippingClass");
return $reducedShippingClass;
}
if (in_array($superReducedProductClass, $classes)) {
$this->logger->customLog("afterGetShippingTaxClass super reduced: $superReducedShippingClass");
return $superReducedShippingClass;
}
break;

case DynamicShipping::TYPE_DEFAULT:
default:
break;
}
}
return $result;
}
}