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) 2020 GEISS Weblösungen (https://www.geissweb.de)
* @license https://www.geissweb.de/legal-information/eula GEISSWEB End User License Agreement
*/
namespace Geissweb\Euvat\Helper\Threshold;
use Geissweb\Euvat\Helper\Configuration;
use Geissweb\Euvat\Logger\Logger;
use Magento\Backend\Model\Session\Quote;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Store\Model\StoreManagerInterface;
/**
* Calculates Thresholds
*/
class Calculator
{
/**
* @var CartInterface|\Magento\Quote\Model\Quote $quote
*/
public $currentQuote = null;
/**
* @var Session
*/
private $checkoutSession;
/**
* @var CartRepositoryInterface
*/
private $cartRepository;
/**
* @var PriceCurrencyInterface
*/
private $priceCurrency;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var Logger
*/
private $logger;
/**
* @var Quote
*/
private $backendSessionQuote;
/**
* @var \Geissweb\Euvat\Helper\Configuration
*/
private $configHelper;
/**
* ThresholdCalculator constructor.
*
* @param Session $checkoutSession
* @param CartRepositoryInterface $cartRepository
* @param PriceCurrencyInterface $priceCurrency
* @param StoreManagerInterface $storeManager
* @param Quote $backendSessionQuote
* @param \Geissweb\Euvat\Helper\Configuration $configHelper
* @param Logger $logger
*/
public function __construct(
Session $checkoutSession,
CartRepositoryInterface $cartRepository,
PriceCurrencyInterface $priceCurrency,
StoreManagerInterface $storeManager,
Quote $backendSessionQuote,
Configuration $configHelper,
Logger $logger
) {
$this->checkoutSession = $checkoutSession;
$this->cartRepository = $cartRepository;
$this->logger = $logger;
$this->priceCurrency = $priceCurrency;
$this->storeManager = $storeManager;
$this->backendSessionQuote = $backendSessionQuote;
$this->configHelper = $configHelper;
// Get admin quote
$adminSessionQuoteId = $this->backendSessionQuote->getQuoteId();
if (!empty($adminSessionQuoteId)) {
try {
$this->currentQuote = $this->cartRepository->get($adminSessionQuoteId);
} catch (NoSuchEntityException $e) {
$this->logger->critical($e);
}
}
// Get frontend quote
$quoteId = $this->checkoutSession->getQuoteId();
if (!empty($quoteId)) {
try {
$this->currentQuote = $this->cartRepository->get($quoteId);
} catch (NoSuchEntityException $e) {
$this->logger->critical($e);
}
}
}
/**
* @return bool
*/
public function isDeliveryToUk()
{
if ($this->currentQuote !== null) {
if ($this->currentQuote->getIsVirtual()) {
return false;
}
return $this->isAddressInUK($this->currentQuote->getShippingAddress());
}
return false;
}
/**
* @param $address
*
* @return bool
*/
public function isAddressInUK($address)
{
$countryId = $address->getCountryId();
$postCode = $address->getPostcode();
$result = $countryId === 'GB' && substr($postCode, 0, 2) != 'BT';
$this->logger->debug("ThresholdCalculator isAddressInUK $countryId ($postCode): " . (int)$result);
return $result;
}
/**
* @return bool
*/
public function isDeliveryToEU()
{
if ($this->currentQuote !== null) {
if ($this->currentQuote->getIsVirtual()) {
return false;
}
return $this->isAddressInEU($this->currentQuote->getShippingAddress());
}
return false;
}
/**
* @param $address
*
* @return bool
*/
public function isAddressInEU($address)
{
$result = false;
$countryId = $address->getCountryId();
$postCode = $address->getPostcode();
if (($this->configHelper->isEuCountry($countryId) && $countryId !== 'GB')
|| ($countryId === 'GB' && substr($postCode, 0, 2) == 'BT')
) {
$result = true;
}
$this->logger->debug("ThresholdCalculator isAddressInEU $countryId ($postCode): " . (int)$result);
return $result;
}
/**
* @param int|float $threshold
* @param $currencyCode
*
* @return bool
*/
public function isCurrentCartAbove($threshold, $currencyCode)
{
if ($this->currentQuote !== null) {
return $this->getPriceInTargetCurrency($this->currentQuote->getSubtotalWithDiscount(), $currencyCode) > $threshold;
}
return false;
}
/**
* Gets the price in target currency
*
* @param $price
* @param $currencyCode
*
* @return float
*/
public function getPriceInTargetCurrency($price, $currencyCode)
{
if ($this->isNeedToConvert($currencyCode)) {
$convertedPrice = $this->priceCurrency->convert($price, null, $currencyCode);
$this->logger->debug("UkThresholdCalculator converted $price in $currencyCode: $convertedPrice");
return $convertedPrice;
}
$this->logger->debug("ThresholdCalculator price is $price $currencyCode");
return $price;
}
/**
* @return bool
*/
private function isNeedToConvert($targetCurrencyCode)
{
return $this->priceCurrency->getCurrency()->getCurrencyCode() !== $targetCurrencyCode;
}
}