diff --git a/datacenterlight/templates/datacenterlight/order_detail.html b/datacenterlight/templates/datacenterlight/order_detail.html index bc8e7562..bd2edcb2 100644 --- a/datacenterlight/templates/datacenterlight/order_detail.html +++ b/datacenterlight/templates/datacenterlight/order_detail.html @@ -120,7 +120,7 @@ {{vm.price|floatformat:2|intcomma}} CHF

- {% trans "VAT" %} ({{ vm.vat_percent|floatformat:2|intcomma }}%) + {% trans "VAT for" %} {{vm.vat_country}} ({{vm.vat_percent}}%) : {{vm.vat|floatformat:2|intcomma}} CHF

{% endif %} diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 44226abb..d4e43703 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -27,7 +27,8 @@ from utils.forms import ( BillingAddress ) from utils.hosting_utils import ( - get_vm_price_with_vat, get_all_public_keys, get_vat_rate_for_country + get_vm_price_with_vat, get_all_public_keys, get_vat_rate_for_country, + get_vm_price_for_given_vat ) from utils.stripe_utils import StripeUtils from utils.tasks import send_plain_email_task @@ -600,8 +601,27 @@ class OrderConfirmationView(DetailView, FormView): request.session['generic_payment_details'], }) else: + vm_specs = request.session.get('specs') + user_vat_country = ( + request.session.get('billing_address_data').get("country") + ) + user_country_vat_rate = get_vat_rate_for_country(user_vat_country) + price, vat, vat_percent, discount = get_vm_price_for_given_vat( + cpu=vm_specs['cpu'], + memory=vm_specs['memory'], + ssd_size=vm_specs['disk_size'], + pricing_name=vm_specs['pricing_name'], + vat_rate=user_country_vat_rate * 100 + ) + vm_specs["price"] = price + vm_specs["vat"] = vat + vm_specs["vat_percent"] = vat_percent + vm_specs["vat_country"] = vat_percent + vm_specs["discount"] = discount + vm_specs["total_price"] = round(price + vat - discount['amount'], 2) + context.update({ - 'vm': request.session.get('specs'), + 'vm': vm_specs, 'form': UserHostingKeyForm(request=self.request), 'keys': get_all_public_keys(self.request.user) }) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index 9c0243e4..d4d27405 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -84,6 +84,41 @@ def get_vm_price(cpu, memory, disk_size, hdd_size=0, pricing_name='default'): return round(float(price), 2) +def get_vm_price_for_given_vat(cpu, memory, ssd_size, hdd_size=0, + pricing_name='default', vat_rate=0): + try: + pricing = VMPricing.objects.get(name=pricing_name) + except Exception as ex: + logger.error( + "Error getting VMPricing object for {pricing_name}." + "Details: {details}".format( + pricing_name=pricing_name, details=str(ex) + ) + ) + return None + + price = ( + (decimal.Decimal(cpu) * pricing.cores_unit_price) + + (decimal.Decimal(memory) * pricing.ram_unit_price) + + (decimal.Decimal(ssd_size) * pricing.ssd_unit_price) + + (decimal.Decimal(hdd_size) * pricing.hdd_unit_price) + ) + + vat = price * vat_rate * decimal.Decimal(0.01) + vat_percent = pricing.vat_percentage + + cents = decimal.Decimal('.01') + price = price.quantize(cents, decimal.ROUND_HALF_UP) + vat = vat.quantize(cents, decimal.ROUND_HALF_UP) + discount = { + 'name': pricing.discount_name, + 'amount': round(float(pricing.discount_amount), 2) + } + return (round(float(price), 2), round(float(vat), 2), + round(float(vat_percent), 2), discount) + + + def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0, pricing_name='default'): """