62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerVat\Model;
|
|
|
|
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
|
|
|
|
class EuCountries
|
|
{
|
|
public const CODES = [
|
|
'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE',
|
|
'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE',
|
|
];
|
|
|
|
public const RATES = [
|
|
'NL' => 21.0,
|
|
'AT' => 20.0,
|
|
'ES' => 21.0,
|
|
'CZ' => 21.0,
|
|
'PL' => 23.0,
|
|
'FR' => 20.0,
|
|
'IT' => 22.0,
|
|
'DE' => 19.0,
|
|
];
|
|
|
|
public function __construct(private readonly CollectionFactory $countryCollectionFactory)
|
|
{
|
|
}
|
|
|
|
public function isEu(string $countryId): bool
|
|
{
|
|
return in_array(strtoupper($countryId), self::CODES, true);
|
|
}
|
|
|
|
public function getRate(string $countryId): float
|
|
{
|
|
return self::RATES[strtoupper($countryId)] ?? 19.0;
|
|
}
|
|
|
|
public function getAppliedRate(string $countryId): float
|
|
{
|
|
return self::toAppliedRate($this->getRate($countryId));
|
|
}
|
|
|
|
public static function toAppliedRate(float $nominalRate): float
|
|
{
|
|
return $nominalRate > 0 ? ($nominalRate / (100 + $nominalRate)) * 100 : 0.0;
|
|
}
|
|
|
|
public function getOptions(): array
|
|
{
|
|
$options = [];
|
|
$collection = $this->countryCollectionFactory->create()
|
|
->addCountryIdFilter(self::CODES);
|
|
foreach ($collection as $country) {
|
|
$options[] = ['value' => $country->getCountryId(), 'label' => $country->getName()];
|
|
}
|
|
usort($options, static fn (array $a, array $b): int => strcasecmp((string)$a['label'], (string)$b['label']));
|
|
return $options;
|
|
}
|
|
}
|