Fix the way we calculate VAT and final VM price

1. Get the VM's pricing from its config and VMPricing
2. Compute and apply discount if any
3. Apply VAT on the discounted amount from 2
This commit is contained in:
PCoder 2019-01-12 11:02:33 +01:00
commit 484bd53bd2
5 changed files with 60 additions and 61 deletions

View file

@ -95,8 +95,14 @@ def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0,
:param ssd_size: Disk space of the VM (SSD)
:param hdd_size: The HDD size
:param pricing_name: The pricing name to be used
:return: The a tuple containing the price of the VM, the VAT and the
VAT percentage
:return: A dict containing
1. the price of the VM,
2. the applied VAT amount
3. the VAT percentage
4. the discount (a dict containing the discount name and
the discount amount),
5. the price after discount
6. the final price
"""
try:
pricing = VMPricing.objects.get(name=pricing_name)
@ -109,28 +115,33 @@ def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0,
)
return None
price = (
price = decimal.Decimal(
(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)
)
discount = {
'name': pricing.discount_name,
'amount': round(pricing.discount_amount, 2)
}
price_after_discount = price - discount['amount']
if pricing.vat_inclusive:
vat = decimal.Decimal(0)
vat_percent = decimal.Decimal(0)
else:
vat = price * pricing.vat_percentage * decimal.Decimal(0.01)
vat = price_after_discount * pricing.vat_percentage * 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 {
'price': round(price, 2),
'vat': round(vat, 2),
'vat_percent': round(vat_percent, 2),
'discount': discount,
'price_after_discount': round(price_after_discount, 2),
'total_price': round(price_after_discount + vat, 2)
}
return (round(float(price), 2), round(float(vat), 2),
round(float(vat_percent), 2), discount)
def ping_ok(host_ipv6):