From 3ca7e89f4fe8d06eb87e2bbd4ab80a8d411a0efb Mon Sep 17 00:00:00 2001
From: PCoder <purple.coder@yahoo.co.uk>
Date: Tue, 31 Dec 2019 17:28:11 +0530
Subject: [PATCH] Show VAT for eu countries only

---
 .../templates/datacenterlight/order_detail.html    | 14 ++++++++------
 datacenterlight/utils.py                           | 10 ++++++++++
 datacenterlight/views.py                           | 11 +++++++----
 hosting/templates/hosting/settings.html            | 11 +++++++----
 4 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/datacenterlight/templates/datacenterlight/order_detail.html b/datacenterlight/templates/datacenterlight/order_detail.html
index 5f61ccf3..db3f73a8 100644
--- a/datacenterlight/templates/datacenterlight/order_detail.html
+++ b/datacenterlight/templates/datacenterlight/order_detail.html
@@ -33,12 +33,14 @@
                           {{billing_address.street_address}}, {{billing_address.postal_code}}<br>
                           {{billing_address.city}}, {{billing_address.country}}
                           {% if billing_address.vat_number %}
-                          <br/>{% trans "VAT Number" %} {{billing_address.vat_number}}&nbsp;
-                                {% if vm.vat_validation_status == "verified" %}
-                                    <span class="fa fa-fw fa-check-circle" aria-hidden="true" title='{% trans "Your VAT number has been verified" %}'></span>
-                                {% else %}
-                                    <span class="fa fa-fw fa-info-circle" aria-hidden="true" title='{% trans "Your VAT number is under validation. VAT will be adjusted, once the validation is complete." %}'></span>
-                                {% endif %}
+                              <br/>{% trans "VAT Number" %} {{billing_address.vat_number}}
+                              {% if vm.vat_validation_status != "ch_vat" and vm.vat_validation_status != "not_needed" %}
+                                    {% if vm.vat_validation_status == "verified" %}
+                                        <span class="fa fa-fw fa-check-circle" aria-hidden="true" title='{% trans "Your VAT number has been verified" %}'></span>
+                                    {% else %}
+                                        <span class="fa fa-fw fa-info-circle" aria-hidden="true" title='{% trans "Your VAT number is under validation. VAT will be adjusted, once the validation is complete." %}'></span>
+                                    {% endif %}
+                              {% endif %}
                           {% endif %}
                       {% endwith %}
                     </p>
diff --git a/datacenterlight/utils.py b/datacenterlight/utils.py
index a082df7f..da9ebdc1 100644
--- a/datacenterlight/utils.py
+++ b/datacenterlight/utils.py
@@ -18,6 +18,11 @@ from .models import VMPricing, VMTemplate
 
 logger = logging.getLogger(__name__)
 
+eu_countries = ['at', 'be', 'bg', 'ch', 'cy', 'cz', 'hr', 'dk',
+                'ee', 'fi', 'fr', 'mc', 'de', 'gr', 'hu', 'ie', 'it',
+                'lv', 'lu', 'mt', 'nl', 'po', 'pt', 'ro','sk', 'si', 'es',
+                'se', 'gb']
+
 
 def get_cms_integration(name):
     current_site = Site.objects.get_current()
@@ -158,6 +163,11 @@ def validate_vat_number(stripe_customer_id, billing_address_id,
     if billing_address is not None:
         logger.debug("BillingAddress found: %s %s type=%s" % (
             billing_address_id, str(billing_address), type(billing_address)))
+        if billing_address.country.lower().strip() not in eu_countries:
+            return {
+                "validated_on": "",
+                "status": "not_needed"
+            }
         if billing_address.vat_number_validated_on:
             logger.debug("billing_address verified on %s" %
                          billing_address.vat_number_validated_on)
diff --git a/datacenterlight/views.py b/datacenterlight/views.py
index 5b98651c..1d832e39 100644
--- a/datacenterlight/views.py
+++ b/datacenterlight/views.py
@@ -562,7 +562,7 @@ class PaymentOrderView(FormView):
                 request.session["vat_validation_status"] = validate_result["status"]
 
             # For generic payment we take the user directly to confirmation
-            if ('generic_payment_type' in request.session and
+            if ('generic_p`ayment_type' in request.session and
                     self.request.session['generic_payment_type'] == 'generic'):
                 return HttpResponseRedirect(
                     reverse('datacenterlight:order_confirmation'))
@@ -617,7 +617,8 @@ class OrderConfirmationView(DetailView, FormView):
 
         if ('generic_payment_type' in request.session and
                 self.request.session['generic_payment_type'] == 'generic'):
-            if request.session["vat_validation_status"] == "verified":
+            if (request.session["vat_validation_status"] == "verified" or
+                    request.session["vat_validation_status"] == "not_needed"):
                 request.session['generic_payment_details']['vat_rate'] = 0
                 request.session['generic_payment_details']['vat_amount'] = 0
                 request.session['generic_payment_details']['amount'] = request.session['generic_payment_details']['amount_before_vat']
@@ -661,11 +662,13 @@ class OrderConfirmationView(DetailView, FormView):
             if user_vat_country.lower() == "ch":
                 vm_specs["vat"] = vat
                 vm_specs["vat_percent"] = vat_percent
+                vm_specs["vat_validation_status"] = "ch_vat"
             elif ("vat_validation_status" in request.session and
-                    request.session["vat_validation_status"] == "verified"):
+                    (request.session["vat_validation_status"] == "verified" or
+                     request.session["vat_validation_status"] == "not_needed")):
                 vm_specs["vat_percent"] = 0
                 vm_specs["vat"] = 0
-                vm_specs["vat_validation_status"] = "verified"
+                vm_specs["vat_validation_status"] = request.session["vat_validation_status"]
             else:
                 vm_specs["vat"] = vat
                 vm_specs["vat_percent"] = vat_percent
diff --git a/hosting/templates/hosting/settings.html b/hosting/templates/hosting/settings.html
index 0452f49e..5a0b7990 100644
--- a/hosting/templates/hosting/settings.html
+++ b/hosting/templates/hosting/settings.html
@@ -27,10 +27,13 @@
                             {% bootstrap_field field show_label=False type='fields' bound_css_class='' %}
                         {% endfor %}
                         {% if form.instance.vat_number %}
-                            {% if form.instance.vat_validation_status == "verified" %}
-                                        <span class="fa fa-fw fa-check-circle" aria-hidden="true" title='{% trans "Your VAT number has been verified" %}'></span>
-                            {% elif form.instance.vat_validation_status == "pending" %}
-                                        <span class="fa fa-fw fa-info-circle" aria-hidden="true" title='{% trans "Your VAT number is under validation. VAT will be adjusted, once the validation is complete." %}'></span>
+                            {% if form.instance.vat_validation_status != "ch_vat" and form.instance.vat_validation_status != "not_needed" %}
+
+                                {% if form.instance.vat_validation_status == "verified" %}
+                                            <span class="fa fa-fw fa-check-circle" aria-hidden="true" title='{% trans "Your VAT number has been verified" %}'></span>
+                                {% elif form.instance.vat_validation_status == "pending" %}
+                                            <span class="fa fa-fw fa-info-circle" aria-hidden="true" title='{% trans "Your VAT number is under validation. VAT will be adjusted, once the validation is complete." %}'></span>
+                                {% endif %}
                             {% endif %}
                         {% endif %}
                         <div class="form-group text-right">