Skip to content

Commit 93c17dc

Browse files
committed
ACP2E-182: Error while validating Store VAT Number
1 parent 847f217 commit 93c17dc

File tree

1 file changed

+75
-16
lines changed
  • app/code/Magento/Config/view/adminhtml/templates/system/config

1 file changed

+75
-16
lines changed

app/code/Magento/Config/view/adminhtml/templates/system/config/js.phtml

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ require([
1515
'prototype'
1616
], function () {
1717
18-
function inArray(arr, value)
19-
{
20-
for (var i = 0; i < arr.length; i++) {
21-
if (arr[i] === value) {
22-
return true;
23-
}
24-
}
25-
return false;
26-
}
27-
2818
var freeModel = Class.create();
2919
freeModel.prototype = {
3020
initialize : function()
@@ -93,8 +83,83 @@ $scriptString .= 'this.euCountryList = ' . json_encode($euCountries);
9383

9484
$scriptString .= <<<script
9585
86+
this.bindCountryVatNumberRelation();
9687
this.bindCountryRegionRelation();
9788
},
89+
bindCountryVatNumberRelation : function(parentId)
90+
{
91+
if (parentId) {
92+
// todo: fix bug in IE
93+
var countryElements = $$('#'+parentId+' .countries');
94+
} else {
95+
var countryElements = $$('.countries');
96+
}
97+
for(var i=0; i<countryElements.size(); i++) {
98+
Event.observe(countryElements[i], 'change', this.reloadVatNumberField.bind(this));
99+
this.initVatNumberField(countryElements[i]);
100+
}
101+
},
102+
reloadVatNumberField : function(event)
103+
{
104+
this.reload = true;
105+
var countryElement = Event.element(event);
106+
if (countryElement && countryElement.id) {
107+
this.refreshVatNumberField(countryElement);
108+
this.refreshValidateVatNumberButton(countryElement);
109+
}
110+
},
111+
initVatNumberField : function(element)
112+
{
113+
var countryElement = element;
114+
if (countryElement && countryElement.id) {
115+
this.refreshVatNumberField(countryElement);
116+
this.refreshValidateVatNumberButton(countryElement);
117+
}
118+
},
119+
refreshVatNumberField : function(countryElement)
120+
{
121+
var vatNumberElement = $(countryElement.id.replace(/country_id/, 'merchant_vat_number'));
122+
if (vatNumberElement) {
123+
this.vatNumberElement = vatNumberElement;
124+
if (vatNumberElement.value.length) {
125+
vatNumberElement.value = '';
126+
}
127+
}
128+
},
129+
refreshValidateVatNumberButton : function(countryElement)
130+
{
131+
var btnVatNumberElement = $(countryElement.id.replace(/country_id/, 'validate_vat_number'));
132+
if (btnVatNumberElement) {
133+
this.btnVatNumberElement = btnVatNumberElement;
134+
if (countryElement.value.length) {
135+
this.btnValidateVatNumber(countryElement.value);
136+
}
137+
}
138+
},
139+
btnValidateVatNumber: function(countryCode)
140+
{
141+
var resultElement = document.getElementById("validation_result");
142+
var isEuCountry = false;
143+
const fruits = Object.values(this.euCountryList);
144+
145+
if ( fruits.includes(countryCode)) {
146+
isEuCountry = true;
147+
}
148+
if (isEuCountry) {
149+
this.btnVatNumberElement.classList.remove('disabled');
150+
if (resultElement) {
151+
resultElement.style.display = "block";
152+
resultElement.innerHTML = '';
153+
if ( resultElement.classList.contains('admin__field-error') )
154+
resultElement.classList.remove('admin__field-error');
155+
}
156+
} else {
157+
this.btnVatNumberElement.classList.add('disabled');
158+
if (resultElement) {
159+
resultElement.style.display = "none";
160+
}
161+
}
162+
},
98163
bindCountryRegionRelation : function(parentId)
99164
{
100165
if (parentId) {
@@ -151,12 +216,6 @@ $scriptString .= <<<script
151216
if (regionElement) {
152217
this.regionElement = regionElement;
153218
if (countryElement.value.length) {
154-
var isEUCountry = false;
155-
if ( inArray(this.euCountryList, countryElement.value)) {
156-
isEUCountry = true;
157-
}
158-
this.validateVatNumber(isEUCountry);
159-
160219
var url = this.regionsUrl+'parent/'+countryElement.value;
161220
this.loader.load(url, {}, this.refreshRegionField.bind(this));
162221
} else {

0 commit comments

Comments
 (0)