56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerVat\Model;
|
|
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
|
|
class TaxContextResolver
|
|
{
|
|
public const MODE_NO_TAX = 'no_tax';
|
|
public const MODE_EU_STANDARD = 'eu_standard_vat';
|
|
public const MODE_EU_VAT_REGISTERED = 'eu_vat_registered';
|
|
public const MODE_UK_STANDARD = 'uk_standard';
|
|
|
|
public function __construct(
|
|
private readonly StoreManagerInterface $storeManager,
|
|
private readonly VatApplicationService $applications,
|
|
private readonly EuCountries $euCountries
|
|
) {
|
|
}
|
|
|
|
public function resolve(int $storeId, int $customerId, string $shippingCountryId): TaxContext
|
|
{
|
|
$websiteCode = (string)$this->storeManager->getStore($storeId)->getWebsite()->getCode();
|
|
if (in_array($websiteCode, ['uk', 'gb_site'], true)) {
|
|
return new TaxContext(
|
|
self::MODE_UK_STANDARD,
|
|
20.0,
|
|
EuCountries::toAppliedRate(20.0),
|
|
false
|
|
);
|
|
}
|
|
if (!in_array($websiteCode, ['eu', 'eu_site'], true)) {
|
|
return new TaxContext(self::MODE_NO_TAX, 0.0, 0.0, false);
|
|
}
|
|
|
|
$countryId = strtoupper($shippingCountryId);
|
|
if (!$this->euCountries->isEu($countryId)) {
|
|
return new TaxContext(self::MODE_NO_TAX, 0.0, 0.0, false);
|
|
}
|
|
$nominalRate = $this->euCountries->getRate($countryId);
|
|
$appliedRate = $this->euCountries->getAppliedRate($countryId);
|
|
$approvedVat = $this->applications->findApproved($customerId, $countryId);
|
|
if ($approvedVat !== null) {
|
|
return new TaxContext(
|
|
self::MODE_EU_VAT_REGISTERED,
|
|
$nominalRate,
|
|
$appliedRate,
|
|
false,
|
|
$approvedVat
|
|
);
|
|
}
|
|
return new TaxContext(self::MODE_EU_STANDARD, $nominalRate, $appliedRate, false);
|
|
}
|
|
}
|