diff --git a/src/Config/Config.php b/src/Config/Config.php index 72c91d6..946dbe0 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -36,6 +36,11 @@ class Config CONST VALIDATE_COMPANY_CONFIG_KEY = 'WS_VATCHECK_VALIDATE_COMPANY'; CONST VALIDATE_COMPANY_DEFAULT = false; + // Configuration key holding the admin-selected list of EU countries allowed + // to order VAT-free (JSON-encoded array of ISO codes). Absent/empty means + // "all countries VatNumber::$euIso supports" — see EuCountryAllowList. + CONST EU_COUNTRIES_CONFIG_KEY = 'WS_VATCHECK_EU_COUNTRIES'; + // Article 39bis W.BTW intra-community exemption notice, per invoice language. CONST INTRACOMMUNITY_EXEMPTION_TEXTS = [ 'nl' => 'Vrijgestelde intracommunautaire levering – artikel 39bis W.BTW', diff --git a/src/Model/EuCountryAllowList.php b/src/Model/EuCountryAllowList.php new file mode 100644 index 0000000..67095ef --- /dev/null +++ b/src/Model/EuCountryAllowList.php @@ -0,0 +1,40 @@ +getEnabledCountriesFormValues(); + } + + private function getEnabledCountriesFormValues() + { + $values = []; + foreach (EuCountryAllowList::getEnabledIsoCodes() as $isoCode) { + $values[Config::EU_COUNTRIES_CONFIG_KEY . '_' . $isoCode] = true; + } + + return $values; + } + + private function getCountryChoices() + { + $supported = EuCountryAllowList::getAllSupportedIsoCodes(); + $choices = []; + + foreach (Country::getCountries(Context::getContext()->language->id) as $country) { + if (in_array($country['iso_code'], $supported, true)) { + $choices[] = [ + 'id' => $country['iso_code'], + 'name' => $country['name'] . ' (' . $country['iso_code'] . ')', + ]; + } + } + + return $choices; } private function getSettingsFormStructure() @@ -132,6 +170,23 @@ trait SettingsForm ], ], ], + [ + 'type' => 'checkbox', + 'label' => $this->trans('Enabled EU countries', [], 'Modules.Ws_VatCheck.Admin'), + 'name' => Config::EU_COUNTRIES_CONFIG_KEY, + 'multiple' => true, + 'desc' => $this->trans( + 'Countries a customer can order VAT-free from with a valid VAT number.', + [], + 'Modules.Ws_VatCheck.Admin' + ), + 'values' => [ + 'query' => $this->getCountryChoices(), + 'id' => 'id', + 'name' => 'name', + 'value' => 'id', + ], + ], ], 'submit' => [ 'title' => $this->trans('Save', [], 'Admin.Actions'), diff --git a/src/Module/Front/FrontHooks.php b/src/Module/Front/FrontHooks.php index 0ce89d2..b5cb7cc 100644 --- a/src/Module/Front/FrontHooks.php +++ b/src/Module/Front/FrontHooks.php @@ -4,6 +4,7 @@ namespace Websmid\VatCheck\Module\Front; use Websmid\VatCheck\TaxManager\CacheTaxManager; use Websmid\VatCheck\Model\VatNumber; +use Websmid\VatCheck\Model\EuCountryAllowList; use Websmid\VatCheck\Config\Config; use Address; use Configuration; @@ -27,7 +28,7 @@ trait FrontHooks $countryIso = (string)Country::getIsoById((string)$form->getValue('id_country')); $vatNumber = new VatNumber($vatNumber, $countryIso); - if( $vatNumber->isNotEuIso() ){ + if( $vatNumber->isNotEuIso() || !EuCountryAllowList::isEnabled($countryIso) ){ return true; } diff --git a/src/Module/Install.php b/src/Module/Install.php index ac0c931..faec900 100644 --- a/src/Module/Install.php +++ b/src/Module/Install.php @@ -3,6 +3,7 @@ namespace Websmid\VatCheck\Module; use Websmid\VatCheck\Config\Config; +use Websmid\VatCheck\Model\EuCountryAllowList; use Db; use Configuration; @@ -39,6 +40,13 @@ trait Install return false; } + if (!Configuration::updateValue( + Config::EU_COUNTRIES_CONFIG_KEY, + json_encode(EuCountryAllowList::getAllSupportedIsoCodes()) + )) { + return false; + } + return true; } diff --git a/src/TaxManager/CacheTaxManager.php b/src/TaxManager/CacheTaxManager.php index 014c159..46cc7c9 100644 --- a/src/TaxManager/CacheTaxManager.php +++ b/src/TaxManager/CacheTaxManager.php @@ -12,6 +12,7 @@ use Configuration; use Context; use Websmid\VatCheck\Model\VatCache; use Websmid\VatCheck\Model\VatNumber; +use Websmid\VatCheck\Model\EuCountryAllowList; use Websmid\VatCheck\Adapter\EuValidatorAdapter; class CacheTaxManager implements TaxManagerInterface @@ -27,7 +28,7 @@ class CacheTaxManager implements TaxManagerInterface } $isoCode = Country::getIsoById((int)$address->id_country); - if( !array_key_exists($isoCode, VatNumber::$euIso) ){ + if( !EuCountryAllowList::isEnabled($isoCode) ){ return false; } diff --git a/upgrade/upgrade-1.0.3.php b/upgrade/upgrade-1.0.3.php index a92a5e0..d2d8d57 100644 --- a/upgrade/upgrade-1.0.3.php +++ b/upgrade/upgrade-1.0.3.php @@ -1,6 +1,7 @@