Use country specific vats for dcl vm buy flow

This commit is contained in:
PCoder 2019-12-07 19:26:21 +05:30
parent b759471274
commit 3b0e479a70
3 changed files with 58 additions and 3 deletions

View file

@ -120,7 +120,7 @@
<strong class="pull-right">{{vm.price|floatformat:2|intcomma}} CHF</strong>
</p>
<p>
<small>{% trans "VAT" %} ({{ vm.vat_percent|floatformat:2|intcomma }}%) </small>
<small>{% trans "VAT for" %} {{vm.vat_country}} ({{vm.vat_percent}}%) : </small>
<strong class="pull-right">{{vm.vat|floatformat:2|intcomma}} CHF</strong>
</p>
{% endif %}

View file

@ -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)
})

View file

@ -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'):
"""