exclude discount from total price

This commit is contained in:
Arvind Tiwari 2018-05-07 06:11:44 +05:30
parent 144d885fbd
commit 2ff8c25034
5 changed files with 27 additions and 25 deletions

View file

@ -36,22 +36,27 @@ class VMPricing(models.Model):
) )
discount_name = models.CharField(max_length=255, null=True, blank=True) discount_name = models.CharField(max_length=255, null=True, blank=True)
discount_amount = models.DecimalField( discount_amount = models.DecimalField(
max_digits=4, decimal_places=2, default=0 max_digits=6, decimal_places=2, default=0
) )
def __str__(self): def __str__(self):
return self.name + ' => ' + ' - '.join([ display_str = self.name + ' => ' + ' - '.join([
'{}/Core'.format(self.cores_unit_price.normalize()), '{}/Core'.format(self.cores_unit_price.normalize()),
'{}/GB RAM'.format(self.ram_unit_price.normalize()), '{}/GB RAM'.format(self.ram_unit_price.normalize()),
'{}/GB SSD'.format(self.ssd_unit_price.normalize()), '{}/GB SSD'.format(self.ssd_unit_price.normalize()),
'{}/GB HDD'.format(self.hdd_unit_price.normalize()), '{}/GB HDD'.format(self.hdd_unit_price.normalize()),
'{}% VAT'.format(self.vat_percentage.normalize()) '{}% VAT'.format(self.vat_percentage.normalize())
if not self.vat_inclusive else 'VAT-Incl', if not self.vat_inclusive else 'VAT-Incl',
'{} {}'.format(
self.discount_amount if self.discount_amount else '',
self.discount_name if self.discount_name else 'Discount'
),
]) ])
if self.discount_amount:
display_str = ' - '.join([
display_str,
'{} {}'.format(
self.discount_amount,
self.discount_name if self.discount_name else 'Discount'
)
])
return display_str
@classmethod @classmethod
def get_vm_pricing_by_name(cls, name): def get_vm_pricing_by_name(cls, name):

View file

@ -76,7 +76,7 @@
<span class="pull-right">{{vm.vat|floatformat:2|intcomma}} CHF</span> <span class="pull-right">{{vm.vat|floatformat:2|intcomma}} CHF</span>
</p> </p>
{% endif %} {% endif %}
{% if vm_pricing.discount_amount %} {% if vm.discount > 0 %}
<p class="text-primary"> <p class="text-primary">
{%trans "Discount" as discount_name %} {%trans "Discount" as discount_name %}
<span>{{ vm_pricing.discount_name|default:discount_name }}: </span> <span>{{ vm_pricing.discount_name|default:discount_name }}: </span>

View file

@ -158,7 +158,7 @@ class IndexView(CreateView):
) )
return HttpResponseRedirect(referer_url + "#order_form") return HttpResponseRedirect(referer_url + "#order_form")
price, vat, vat_percent = get_vm_price_with_vat( price, vat, vat_percent, discount = get_vm_price_with_vat(
cpu=cores, cpu=cores,
memory=memory, memory=memory,
ssd_size=storage, ssd_size=storage,
@ -171,7 +171,8 @@ class IndexView(CreateView):
'price': price, 'price': price,
'vat': vat, 'vat': vat,
'vat_percent': vat_percent, 'vat_percent': vat_percent,
'total_price': price + vat, 'discount': discount,
'total_price': price + vat - discount,
'pricing_name': vm_pricing_name 'pricing_name': vm_pricing_name
} }
request.session['specs'] = specs request.session['specs'] = specs
@ -388,9 +389,6 @@ class OrderConfirmationView(DetailView):
request.session.get('billing_address_data') request.session.get('billing_address_data')
), ),
'cms_integration': get_cms_integration('default'), 'cms_integration': get_cms_integration('default'),
'vm_pricing': VMPricing.get_vm_pricing_by_name(
self.request.session['specs']['pricing_name']
),
} }
return render(request, self.template_name, context) return render(request, self.template_name, context)

View file

@ -655,7 +655,7 @@ class PaymentVMView(LoginRequiredMixin, FormView):
'stripe_key': settings.STRIPE_API_PUBLIC_KEY, 'stripe_key': settings.STRIPE_API_PUBLIC_KEY,
'vm_pricing': VMPricing.get_vm_pricing_by_name( 'vm_pricing': VMPricing.get_vm_pricing_by_name(
self.request.session['specs']['pricing_name'] self.request.session['specs']['pricing_name']
) ),
}) })
return context return context
@ -753,7 +753,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
context['vm'] = vm_detail.__dict__ context['vm'] = vm_detail.__dict__
context['vm']['name'] = '{}-{}'.format( context['vm']['name'] = '{}-{}'.format(
context['vm']['configuration'], context['vm']['vm_id']) context['vm']['configuration'], context['vm']['vm_id'])
price, vat, vat_percent = get_vm_price_with_vat( price, vat, vat_percent, discount = get_vm_price_with_vat(
cpu=context['vm']['cores'], cpu=context['vm']['cores'],
ssd_size=context['vm']['disk_size'], ssd_size=context['vm']['disk_size'],
memory=context['vm']['memory'], memory=context['vm']['memory'],
@ -762,8 +762,9 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
) )
context['vm']['vat'] = vat context['vm']['vat'] = vat
context['vm']['price'] = price context['vm']['price'] = price
context['vm']['discount'] = discount
context['vm']['vat_percent'] = vat_percent context['vm']['vat_percent'] = vat_percent
context['vm']['total_price'] = price + vat context['vm']['total_price'] = price + vat - discount
context['subscription_end_date'] = vm_detail.end_date() context['subscription_end_date'] = vm_detail.end_date()
except VMDetail.DoesNotExist: except VMDetail.DoesNotExist:
try: try:
@ -772,7 +773,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
) )
vm = manager.get_vm(obj.vm_id) vm = manager.get_vm(obj.vm_id)
context['vm'] = VirtualMachineSerializer(vm).data context['vm'] = VirtualMachineSerializer(vm).data
price, vat, vat_percent = get_vm_price_with_vat( price, vat, vat_percent, discount = get_vm_price_with_vat(
cpu=context['vm']['cores'], cpu=context['vm']['cores'],
ssd_size=context['vm']['disk_size'], ssd_size=context['vm']['disk_size'],
memory=context['vm']['memory'], memory=context['vm']['memory'],
@ -781,8 +782,9 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
) )
context['vm']['vat'] = vat context['vm']['vat'] = vat
context['vm']['price'] = price context['vm']['price'] = price
context['vm']['discount'] = discount
context['vm']['vat_percent'] = vat_percent context['vm']['vat_percent'] = vat_percent
context['vm']['total_price'] = price + vat context['vm']['total_price'] = price + vat - discount
except WrongIdError: except WrongIdError:
messages.error( messages.error(
self.request, self.request,
@ -809,9 +811,6 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
context['cc_brand'] = card_details.get('response_object').get( context['cc_brand'] = card_details.get('response_object').get(
'cc_brand') 'cc_brand')
context['vm'] = self.request.session.get('specs') context['vm'] = self.request.session.get('specs')
context['vm_pricing'] = VMPricing.get_vm_pricing_by_name(
self.request.session['specs']['pricing_name']
),
return context return context
@method_decorator(decorators) @method_decorator(decorators)
@ -1071,7 +1070,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
extra_tags='storage') extra_tags='storage')
return redirect(CreateVirtualMachinesView.as_view()) return redirect(CreateVirtualMachinesView.as_view())
price, vat, vat_percent = get_vm_price_with_vat( price, vat, vat_percent, discount = get_vm_price_with_vat(
cpu=cores, cpu=cores,
memory=memory, memory=memory,
ssd_size=storage, ssd_size=storage,
@ -1085,7 +1084,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
'price': price, 'price': price,
'vat': vat, 'vat': vat,
'vat_percent': vat_percent, 'vat_percent': vat_percent,
'total_price': price + vat, 'total_price': price + vat - discount,
'pricing_name': vm_pricing_name 'pricing_name': vm_pricing_name
} }

View file

@ -111,8 +111,7 @@ def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0,
(decimal.Decimal(cpu) * pricing.cores_unit_price) + (decimal.Decimal(cpu) * pricing.cores_unit_price) +
(decimal.Decimal(memory) * pricing.ram_unit_price) + (decimal.Decimal(memory) * pricing.ram_unit_price) +
(decimal.Decimal(ssd_size) * pricing.ssd_unit_price) + (decimal.Decimal(ssd_size) * pricing.ssd_unit_price) +
(decimal.Decimal(hdd_size) * pricing.hdd_unit_price) - (decimal.Decimal(hdd_size) * pricing.hdd_unit_price)
pricing.discount_amount
) )
if pricing.vat_inclusive: if pricing.vat_inclusive:
vat = decimal.Decimal(0) vat = decimal.Decimal(0)
@ -124,4 +123,5 @@ def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0,
cents = decimal.Decimal('.01') cents = decimal.Decimal('.01')
price = price.quantize(cents, decimal.ROUND_HALF_UP) price = price.quantize(cents, decimal.ROUND_HALF_UP)
vat = vat.quantize(cents, decimal.ROUND_HALF_UP) vat = vat.quantize(cents, decimal.ROUND_HALF_UP)
return float(price), float(vat), float(vat_percent) discount = pricing.discount_amount
return float(price), float(vat), float(vat_percent), float(discount)