From 4746b8e45680669f543aa019b78888c2665f723d Mon Sep 17 00:00:00 2001 From: Andreas Teuber <44349054+ateuber@users.noreply.github.com> Date: Mon, 13 Jul 2020 18:04:09 +0200 Subject: [PATCH] Ask only for VAT ID if company is inside EU (#1709) Co-authored-by: Andreas Teuber Co-authored-by: Raphael Michel --- src/pretix/base/forms/questions.py | 13 +++++++++---- src/pretix/static/pretixcontrol/js/ui/main.js | 18 ++++++++++++++++++ src/pretix/static/pretixpresale/js/ui/main.js | 18 ++++++++++++++++++ src/pretix/urls.py | 2 +- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/pretix/base/forms/questions.py b/src/pretix/base/forms/questions.py index 2e6fa4bc0..a808a04d9 100644 --- a/src/pretix/base/forms/questions.py +++ b/src/pretix/base/forms/questions.py @@ -580,7 +580,7 @@ class BaseInvoiceAddressForm(forms.ModelForm): 'data-display-dependency': '#id_is_business_1', 'autocomplete': 'organization', }), - 'vat_id': forms.TextInput(attrs={'data-display-dependency': '#id_is_business_1'}), + 'vat_id': forms.TextInput(attrs={'data-display-dependency': '#id_is_business_1', 'data-countries-in-eu': ','.join(EU_COUNTRIES)}), 'internal_reference': forms.TextInput, } labels = { @@ -630,6 +630,11 @@ class BaseInvoiceAddressForm(forms.ModelForm): ) self.fields['state'].widget.is_required = True + # Without JavaScript the VAT ID field is not hidden, so we empty the field if a country outside the EU is selected. + if cc and cc not in EU_COUNTRIES and fprefix + 'vat_id' in self.data: + self.data = self.data.copy() + del self.data[fprefix + 'vat_id'] + if not event.settings.invoice_address_required or self.all_optional: for k, f in self.fields.items(): f.required = False @@ -644,8 +649,6 @@ class BaseInvoiceAddressForm(forms.ModelForm): self.fields['company'].widget.is_required = True self.fields['company'].widget.attrs['required'] = 'required' del self.fields['company'].widget.attrs['data-display-dependency'] - if 'vat_id' in self.fields: - del self.fields['vat_id'].widget.attrs['data-display-dependency'] self.fields['name_parts'] = NamePartsFormField( max_length=255, @@ -677,6 +680,9 @@ class BaseInvoiceAddressForm(forms.ModelForm): data = self.cleaned_data if not data.get('is_business'): data['company'] = '' + data['vat_id'] = '' + if data.get('is_business') and not data.get('country') in EU_COUNTRIES: + data['vat_id'] = '' if self.event.settings.invoice_address_required: if data.get('is_business') and not data.get('company'): raise ValidationError(_('You need to provide a company name.')) @@ -697,7 +703,6 @@ class BaseInvoiceAddressForm(forms.ModelForm): ) and len(data.get('name_parts', {})) == 1: # Do not save the country if it is the only field set -- we don't know the user even checked it! self.cleaned_data['country'] = '' - if self.validate_vat_id and self.instance.vat_id_validated and 'vat_id' not in self.changed_data: pass elif self.validate_vat_id and data.get('is_business') and data.get('country') in EU_COUNTRIES and data.get('vat_id'): diff --git a/src/pretix/static/pretixcontrol/js/ui/main.js b/src/pretix/static/pretixcontrol/js/ui/main.js index 928f0cd34..254961224 100644 --- a/src/pretix/static/pretixcontrol/js/ui/main.js +++ b/src/pretix/static/pretixcontrol/js/ui/main.js @@ -314,6 +314,24 @@ var form_handlers = function (el) { dependency.closest('.form-group').find('input[name=' + dependency.attr("name") + ']').on("dp.change", update); }); + $("input[name$=vat_id][data-countries-in-eu]").each(function () { + var dependent = $(this), + dependency_country = $(this).closest(".panel-body, form").find('select[name$=country]'), + dependency_id_is_business_1 = $(this).closest(".panel-body, form").find('input[id$=id_is_business_1]'), + update = function (ev) { + if (dependency_id_is_business_1.length && !dependency_id_is_business_1.prop("checked")) { + dependent.closest(".form-group").hide(); + } else if (dependent.attr('data-countries-in-eu').split(',').includes(dependency_country.val())) { + dependent.closest(".form-group").show(); + } else { + dependent.closest(".form-group").hide(); + } + }; + update(); + dependency_country.on("change", update); + dependency_id_is_business_1.on("change", update); + }); + $("select[name$=state]:not([data-static])").each(function () { var dependent = $(this), counter = 0, diff --git a/src/pretix/static/pretixpresale/js/ui/main.js b/src/pretix/static/pretixpresale/js/ui/main.js index dd34b6ec0..b1410c49a 100644 --- a/src/pretix/static/pretixpresale/js/ui/main.js +++ b/src/pretix/static/pretixpresale/js/ui/main.js @@ -293,6 +293,24 @@ $(function () { dependency.closest('.form-group, form').find('input[name=' + dependency.attr("name") + ']').on("dp.change", update); }); + $("input[name$=vat_id][data-countries-in-eu]").each(function () { + var dependent = $(this), + dependency_country = $(this).closest(".panel-body, form").find('select[name$=country]'), + dependency_id_is_business_1 = $(this).closest(".panel-body, form").find('input[id$=id_is_business_1]'), + update = function (ev) { + if (dependency_id_is_business_1.length && !dependency_id_is_business_1.prop("checked")) { + dependent.closest(".form-group").hide(); + } else if (dependent.attr('data-countries-in-eu').split(',').includes(dependency_country.val())) { + dependent.closest(".form-group").show(); + } else { + dependent.closest(".form-group").hide(); + } + }; + update(); + dependency_country.on("change", update); + dependency_id_is_business_1.on("change", update); + }); + $("select[name$=state]").each(function () { var dependent = $(this), counter = 0, diff --git a/src/pretix/urls.py b/src/pretix/urls.py index e54ed12ff..d84b278f6 100644 --- a/src/pretix/urls.py +++ b/src/pretix/urls.py @@ -18,7 +18,7 @@ base_patterns = [ url(r'^metrics$', metrics.serve_metrics, name='metrics'), url(r'^csp_report/$', csp.csp_report, name='csp.report'), - url(r'^js_helpers/states/$', js_helpers.states, name='js_helpers.stats'), + url(r'^js_helpers/states/$', js_helpers.states, name='js_helpers.states'), url(r'^api/v1/', include(('pretix.api.urls', 'pretixapi'), namespace='api-v1')), url(r'^api/$', RedirectView.as_view(url='/api/v1/'), name='redirect-api-version') ]