diff --git a/datacenterlight/models.py b/datacenterlight/models.py index 56a19f03..ff7eeb8d 100644 --- a/datacenterlight/models.py +++ b/datacenterlight/models.py @@ -36,22 +36,27 @@ class VMPricing(models.Model): ) discount_name = models.CharField(max_length=255, null=True, blank=True) discount_amount = models.DecimalField( - max_digits=4, decimal_places=2, default=0 + max_digits=6, decimal_places=2, default=0 ) def __str__(self): - return self.name + ' => ' + ' - '.join([ + display_str = self.name + ' => ' + ' - '.join([ '{}/Core'.format(self.cores_unit_price.normalize()), '{}/GB RAM'.format(self.ram_unit_price.normalize()), '{}/GB SSD'.format(self.ssd_unit_price.normalize()), '{}/GB HDD'.format(self.hdd_unit_price.normalize()), '{}% VAT'.format(self.vat_percentage.normalize()) 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 def get_vm_pricing_by_name(cls, name): diff --git a/datacenterlight/templates/datacenterlight/order_detail.html b/datacenterlight/templates/datacenterlight/order_detail.html index 13d2c61e..3b269377 100644 --- a/datacenterlight/templates/datacenterlight/order_detail.html +++ b/datacenterlight/templates/datacenterlight/order_detail.html @@ -76,7 +76,7 @@ {{vm.vat|floatformat:2|intcomma}} CHF

{% endif %} - {% if vm_pricing.discount_amount %} + {% if vm.discount > 0 %}

{%trans "Discount" as discount_name %} {{ vm_pricing.discount_name|default:discount_name }}: diff --git a/datacenterlight/views.py b/datacenterlight/views.py index bc5ea49e..8f4c886f 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -158,7 +158,7 @@ class IndexView(CreateView): ) 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, memory=memory, ssd_size=storage, @@ -171,7 +171,8 @@ class IndexView(CreateView): 'price': price, 'vat': vat, 'vat_percent': vat_percent, - 'total_price': price + vat, + 'discount': discount, + 'total_price': price + vat - discount, 'pricing_name': vm_pricing_name } request.session['specs'] = specs @@ -388,9 +389,6 @@ class OrderConfirmationView(DetailView): request.session.get('billing_address_data') ), '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) diff --git a/hosting/views.py b/hosting/views.py index 1353229a..99897841 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -655,7 +655,7 @@ class PaymentVMView(LoginRequiredMixin, FormView): 'stripe_key': settings.STRIPE_API_PUBLIC_KEY, 'vm_pricing': VMPricing.get_vm_pricing_by_name( self.request.session['specs']['pricing_name'] - ) + ), }) return context @@ -753,7 +753,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): context['vm'] = vm_detail.__dict__ context['vm']['name'] = '{}-{}'.format( 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'], ssd_size=context['vm']['disk_size'], memory=context['vm']['memory'], @@ -762,8 +762,9 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): ) context['vm']['vat'] = vat context['vm']['price'] = price + context['vm']['discount'] = discount 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() except VMDetail.DoesNotExist: try: @@ -772,7 +773,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): ) vm = manager.get_vm(obj.vm_id) 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'], ssd_size=context['vm']['disk_size'], memory=context['vm']['memory'], @@ -781,8 +782,9 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): ) context['vm']['vat'] = vat context['vm']['price'] = price + context['vm']['discount'] = discount context['vm']['vat_percent'] = vat_percent - context['vm']['total_price'] = price + vat + context['vm']['total_price'] = price + vat - discount except WrongIdError: messages.error( self.request, @@ -809,9 +811,6 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): context['cc_brand'] = card_details.get('response_object').get( 'cc_brand') context['vm'] = self.request.session.get('specs') - context['vm_pricing'] = VMPricing.get_vm_pricing_by_name( - self.request.session['specs']['pricing_name'] - ), return context @method_decorator(decorators) @@ -1071,7 +1070,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View): extra_tags='storage') 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, memory=memory, ssd_size=storage, @@ -1085,7 +1084,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View): 'price': price, 'vat': vat, 'vat_percent': vat_percent, - 'total_price': price + vat, + 'total_price': price + vat - discount, 'pricing_name': vm_pricing_name } diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index b6e267a2..9e96634f 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -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(memory) * pricing.ram_unit_price) + (decimal.Decimal(ssd_size) * pricing.ssd_unit_price) + - (decimal.Decimal(hdd_size) * pricing.hdd_unit_price) - - pricing.discount_amount + (decimal.Decimal(hdd_size) * pricing.hdd_unit_price) ) if pricing.vat_inclusive: 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') price = price.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)