|
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 / Helper / Threshold / |
Filename | /home/dev2.destoffenstraat.com/app/code/Geissweb/Euvat/Helper/Threshold/Calculator.php |
Size | 9.06 kb |
Permission | rwxrwxrwx |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 09-Jul-2024 08:41 |
Last accessed | 22-Aug-2025 02:07 |
Actions | edit | rename | delete | download (gzip) |
View | text | 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) 2020 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\Helper\Threshold;
use Geissweb\Euvat\Helper\Configuration;
use Geissweb\Euvat\Logger\Logger;
use Magento\Backend\Model\Session\Quote as BackendQuote;
use Magento\Checkout\Model\Session;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Sales\Api\Data\OrderAddressInterface;
/**
* Calculates Thresholds
*/
class Calculator
{
public ?CartInterface $currentQuote = null;
private Session $checkoutSession;
private CartRepositoryInterface $cartRepository;
private PriceCurrencyInterface $priceCurrency;
private Logger $logger;
private BackendQuote $backendSessionQuote;
private Configuration $configHelper;
private string $configSection = 'brexit_settings';
/**
* ThresholdCalculator constructor.
*
* @param Session $checkoutSession
* @param CartRepositoryInterface $cartRepository
* @param PriceCurrencyInterface $priceCurrency
* @param BackendQuote $backendSessionQuote
* @param \Geissweb\Euvat\Helper\Configuration $configHelper
* @param Logger $logger
*/
public function __construct(
Session $checkoutSession,
CartRepositoryInterface $cartRepository,
PriceCurrencyInterface $priceCurrency,
BackendQuote $backendSessionQuote,
Configuration $configHelper,
Logger $logger
) {
$this->checkoutSession = $checkoutSession;
$this->cartRepository = $cartRepository;
$this->logger = $logger;
$this->priceCurrency = $priceCurrency;
$this->backendSessionQuote = $backendSessionQuote;
$this->configHelper = $configHelper;
}
/**
* Set either admin or frontend quote
*
* @return void
*/
public function setCurrentQuote(): void
{
// Get admin quote
$adminSessionQuoteId = $this->backendSessionQuote->getQuoteId();
if (!empty($adminSessionQuoteId)) {
try {
$this->currentQuote = $this->cartRepository->get($adminSessionQuoteId);
// @phpcs:ignore
} catch (NoSuchEntityException $e) {
// Expired cart
}
}
// Get frontend quote
$quoteId = $this->checkoutSession->getQuoteId();
if (!empty($quoteId)) {
try {
$this->currentQuote = $this->cartRepository->get($quoteId);
// @phpcs:ignore
} catch (NoSuchEntityException $e) {
// Expired cart
}
}
}
/**
* Checks if shipping goes to UK
*
* @return bool
*/
public function isDeliveryToUk(): bool
{
if ($this->currentQuote !== null) {
if ($this->currentQuote->getIsVirtual()) {
return false;
}
// @phpstan-ignore-next-line
return $this->isAddressInUK($this->currentQuote->getShippingAddress());
}
return false;
}
/**
* Checks if address is in UK
*
* @param AddressInterface|OrderAddressInterface|QuoteAddress $address
*
* @return bool
*/
public function isAddressInUK($address): bool
{
$countryId = $address->getCountryId();
$postCode = $address->getPostcode();
$result = $countryId === 'GB' && $this->isPostcodeInNI($postCode) === false;
$this->logger->customLog("ThresholdCalculator isAddressInUK $countryId ($postCode): " . (int)$result);
return $result;
}
/**
* Checks if a postcode belongs to NI
*
* @param string|null $postCode
*
* @return bool
*/
public function isPostcodeInNI(?string $postCode) : bool
{
if (is_string($postCode)) {
$postcodeCheck = substr($postCode, 0, 2) === 'BT';
} else {
$postcodeCheck = false;
}
return $postcodeCheck;
}
/**
* Checks if delivery goes to EU
*
* @return bool
*/
public function isDeliveryToEU(): bool
{
if ($this->currentQuote !== null) {
if ($this->currentQuote->getIsVirtual()) {
return false;
}
// @phpstan-ignore-next-line
return $this->isAddressInEU($this->currentQuote->getShippingAddress());
}
return false;
}
/**
* Checks whether address is in EU considering NI as EU country
*
* @param AddressInterface|OrderAddressInterface|QuoteAddress $address
*
* @return bool
*/
public function isAddressInEU($address): bool
{
$result = false;
$countryId = $address->getCountryId();
$postCode = $address->getPostcode();
if (($this->configHelper->isEuCountry($countryId) && $countryId !== 'GB')
|| ($countryId === 'GB' && $this->isPostcodeInNI($postCode))
) {
$result = true;
}
$this->logger->customLog("ThresholdCalculator isAddressInEU $countryId ($postCode): " . (int)$result);
return $result;
}
/**
* Checks if delivery goes to NI
*
* @return bool
*/
public function isDeliveryToNI(): bool
{
if ($this->currentQuote !== null) {
if ($this->currentQuote->getIsVirtual()) {
return false;
}
// @phpstan-ignore-next-line
return $this->isAddressInNI($this->currentQuote->getShippingAddress());
}
return false;
}
/**
* Checks if address is in NI
*
* @param AddressInterface|OrderAddressInterface|QuoteAddress $address
*
* @return bool
*/
public function isAddressInNI($address): bool
{
$result = false;
$countryId = $address->getCountryId();
$postCode = $address->getPostcode();
if ($countryId === 'GB' && $this->isPostcodeInNI($postCode)) {
$result = true;
}
$this->logger->customLog("ThresholdCalculator isAddressInNI $countryId ($postCode): " . (int)$result);
return $result;
}
/**
* Checks if cart is above threshold
*
* @param int|float $threshold
* @param string $currencyCode
*
* @return bool
*/
public function isCurrentCartAbove($threshold, string $currencyCode): bool
{
if ($this->currentQuote !== null) {
// @phpstan-ignore-next-line
$value = $this->getPriceInTargetCurrency($this->currentQuote->getSubtotalWithDiscount(), $currencyCode);
if ($this->configHelper->isIncludeShippingInThresholdCalculation($this->getConfigSection())) {
$shipping = $this->getPriceInTargetCurrency(
$this->currentQuote->getShippingAddress()->getShippingAmount(), // @phpstan-ignore-line
$currencyCode
);
$value += $shipping;
}
return $value >= $threshold;
}
return false;
}
/**
* Gets the price in target currency
*
* @param int|float $price
* @param string $currencyCode
*
* @return float
*/
public function getPriceInTargetCurrency($price, string $currencyCode): float
{
if ($this->isNeedToConvert($currencyCode)) {
$convertedPrice = $this->priceCurrency->convert((float)$price, null, $currencyCode);
$this->logger->customLog("ThresholdCalculator converted $price in $currencyCode: $convertedPrice");
return $convertedPrice;
}
$this->logger->customLog("ThresholdCalculator price is $price $currencyCode");
return (float)$price;
}
/**
* Checks if we need to convert the currency
*
* @param string $targetCurrencyCode
*
* @return bool
*/
private function isNeedToConvert(string $targetCurrencyCode): bool
{
return $this->priceCurrency->getCurrency()->getCurrencyCode() !== $targetCurrencyCode;
}
/**
* Gets the config section needed
*
* @return string
*/
public function getConfigSection(): string
{
return $this->configSection;
}
/**
* Sets the config section
*
* @param string $configSection
*/
public function setConfigSection(string $configSection): void
{
$this->configSection = $configSection;
}
}
/**
* ||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) 2020 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\Helper\Threshold;
use Geissweb\Euvat\Helper\Configuration;
use Geissweb\Euvat\Logger\Logger;
use Magento\Backend\Model\Session\Quote as BackendQuote;
use Magento\Checkout\Model\Session;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Sales\Api\Data\OrderAddressInterface;
/**
* Calculates Thresholds
*/
class Calculator
{
public ?CartInterface $currentQuote = null;
private Session $checkoutSession;
private CartRepositoryInterface $cartRepository;
private PriceCurrencyInterface $priceCurrency;
private Logger $logger;
private BackendQuote $backendSessionQuote;
private Configuration $configHelper;
private string $configSection = 'brexit_settings';
/**
* ThresholdCalculator constructor.
*
* @param Session $checkoutSession
* @param CartRepositoryInterface $cartRepository
* @param PriceCurrencyInterface $priceCurrency
* @param BackendQuote $backendSessionQuote
* @param \Geissweb\Euvat\Helper\Configuration $configHelper
* @param Logger $logger
*/
public function __construct(
Session $checkoutSession,
CartRepositoryInterface $cartRepository,
PriceCurrencyInterface $priceCurrency,
BackendQuote $backendSessionQuote,
Configuration $configHelper,
Logger $logger
) {
$this->checkoutSession = $checkoutSession;
$this->cartRepository = $cartRepository;
$this->logger = $logger;
$this->priceCurrency = $priceCurrency;
$this->backendSessionQuote = $backendSessionQuote;
$this->configHelper = $configHelper;
}
/**
* Set either admin or frontend quote
*
* @return void
*/
public function setCurrentQuote(): void
{
// Get admin quote
$adminSessionQuoteId = $this->backendSessionQuote->getQuoteId();
if (!empty($adminSessionQuoteId)) {
try {
$this->currentQuote = $this->cartRepository->get($adminSessionQuoteId);
// @phpcs:ignore
} catch (NoSuchEntityException $e) {
// Expired cart
}
}
// Get frontend quote
$quoteId = $this->checkoutSession->getQuoteId();
if (!empty($quoteId)) {
try {
$this->currentQuote = $this->cartRepository->get($quoteId);
// @phpcs:ignore
} catch (NoSuchEntityException $e) {
// Expired cart
}
}
}
/**
* Checks if shipping goes to UK
*
* @return bool
*/
public function isDeliveryToUk(): bool
{
if ($this->currentQuote !== null) {
if ($this->currentQuote->getIsVirtual()) {
return false;
}
// @phpstan-ignore-next-line
return $this->isAddressInUK($this->currentQuote->getShippingAddress());
}
return false;
}
/**
* Checks if address is in UK
*
* @param AddressInterface|OrderAddressInterface|QuoteAddress $address
*
* @return bool
*/
public function isAddressInUK($address): bool
{
$countryId = $address->getCountryId();
$postCode = $address->getPostcode();
$result = $countryId === 'GB' && $this->isPostcodeInNI($postCode) === false;
$this->logger->customLog("ThresholdCalculator isAddressInUK $countryId ($postCode): " . (int)$result);
return $result;
}
/**
* Checks if a postcode belongs to NI
*
* @param string|null $postCode
*
* @return bool
*/
public function isPostcodeInNI(?string $postCode) : bool
{
if (is_string($postCode)) {
$postcodeCheck = substr($postCode, 0, 2) === 'BT';
} else {
$postcodeCheck = false;
}
return $postcodeCheck;
}
/**
* Checks if delivery goes to EU
*
* @return bool
*/
public function isDeliveryToEU(): bool
{
if ($this->currentQuote !== null) {
if ($this->currentQuote->getIsVirtual()) {
return false;
}
// @phpstan-ignore-next-line
return $this->isAddressInEU($this->currentQuote->getShippingAddress());
}
return false;
}
/**
* Checks whether address is in EU considering NI as EU country
*
* @param AddressInterface|OrderAddressInterface|QuoteAddress $address
*
* @return bool
*/
public function isAddressInEU($address): bool
{
$result = false;
$countryId = $address->getCountryId();
$postCode = $address->getPostcode();
if (($this->configHelper->isEuCountry($countryId) && $countryId !== 'GB')
|| ($countryId === 'GB' && $this->isPostcodeInNI($postCode))
) {
$result = true;
}
$this->logger->customLog("ThresholdCalculator isAddressInEU $countryId ($postCode): " . (int)$result);
return $result;
}
/**
* Checks if delivery goes to NI
*
* @return bool
*/
public function isDeliveryToNI(): bool
{
if ($this->currentQuote !== null) {
if ($this->currentQuote->getIsVirtual()) {
return false;
}
// @phpstan-ignore-next-line
return $this->isAddressInNI($this->currentQuote->getShippingAddress());
}
return false;
}
/**
* Checks if address is in NI
*
* @param AddressInterface|OrderAddressInterface|QuoteAddress $address
*
* @return bool
*/
public function isAddressInNI($address): bool
{
$result = false;
$countryId = $address->getCountryId();
$postCode = $address->getPostcode();
if ($countryId === 'GB' && $this->isPostcodeInNI($postCode)) {
$result = true;
}
$this->logger->customLog("ThresholdCalculator isAddressInNI $countryId ($postCode): " . (int)$result);
return $result;
}
/**
* Checks if cart is above threshold
*
* @param int|float $threshold
* @param string $currencyCode
*
* @return bool
*/
public function isCurrentCartAbove($threshold, string $currencyCode): bool
{
if ($this->currentQuote !== null) {
// @phpstan-ignore-next-line
$value = $this->getPriceInTargetCurrency($this->currentQuote->getSubtotalWithDiscount(), $currencyCode);
if ($this->configHelper->isIncludeShippingInThresholdCalculation($this->getConfigSection())) {
$shipping = $this->getPriceInTargetCurrency(
$this->currentQuote->getShippingAddress()->getShippingAmount(), // @phpstan-ignore-line
$currencyCode
);
$value += $shipping;
}
return $value >= $threshold;
}
return false;
}
/**
* Gets the price in target currency
*
* @param int|float $price
* @param string $currencyCode
*
* @return float
*/
public function getPriceInTargetCurrency($price, string $currencyCode): float
{
if ($this->isNeedToConvert($currencyCode)) {
$convertedPrice = $this->priceCurrency->convert((float)$price, null, $currencyCode);
$this->logger->customLog("ThresholdCalculator converted $price in $currencyCode: $convertedPrice");
return $convertedPrice;
}
$this->logger->customLog("ThresholdCalculator price is $price $currencyCode");
return (float)$price;
}
/**
* Checks if we need to convert the currency
*
* @param string $targetCurrencyCode
*
* @return bool
*/
private function isNeedToConvert(string $targetCurrencyCode): bool
{
return $this->priceCurrency->getCurrency()->getCurrencyCode() !== $targetCurrencyCode;
}
/**
* Gets the config section needed
*
* @return string
*/
public function getConfigSection(): string
{
return $this->configSection;
}
/**
* Sets the config section
*
* @param string $configSection
*/
public function setConfigSection(string $configSection): void
{
$this->configSection = $configSection;
}
}