From a90bec98ec2d79e88da4bd99d2375063c747f3b2 Mon Sep 17 00:00:00 2001
From: PCoder <purple.coder@yahoo.co.uk>
Date: Mon, 24 Sep 2018 08:22:46 +0200
Subject: [PATCH] Make invoice for generic payment showable

---
 hosting/templates/hosting/order_detail.html | 148 +++++++++++---------
 hosting/views.py                            |  90 ++++++------
 2 files changed, 132 insertions(+), 106 deletions(-)

diff --git a/hosting/templates/hosting/order_detail.html b/hosting/templates/hosting/order_detail.html
index e2e38c35..0cbe103e 100644
--- a/hosting/templates/hosting/order_detail.html
+++ b/hosting/templates/hosting/order_detail.html
@@ -39,7 +39,7 @@
                     {% endif %}
                 </span>
             </p>
-            {% if order %}
+            {% if order and vm %}
                 <p>
                     <strong>{% trans "Status" %}: </strong>
                     <strong>
@@ -93,77 +93,97 @@
             <hr>
             <div>
                 <h4>{% trans "Order summary" %}</h4>
-                <p>
-                    <strong>{% trans "Product" %}:</strong>&nbsp;
-                    {% if vm.name %}
-                        {{ vm.name }}
-                    {% else %}
-                        {{ request.session.template.name }}
-                    {% endif %}
-                </p>
-                <div class="row">
-                    <div class="col-sm-6">
-                        {% if vm.created_at %}
-                            <p>
-                                <span>{% trans "Period" %}: </span>
-                                <span>
-                                    <span class="locale_date" data-format="YYYY/MM/DD">{{ vm.created_at|date:'Y-m-d h:i a' }}</span> - <span class="locale_date" data-format="YYYY/MM/DD">{{ subscription_end_date|date:'Y-m-d h:i a' }}</span>
-                                </span>
-                            </p>
+                {% if vm %}
+                    <p>
+                        <strong>{% trans "Product" %}:</strong>&nbsp;
+                        {% if vm.name %}
+                            {{ vm.name }}
+                        {% else %}
+                            {{ request.session.template.name }}
                         {% endif %}
-                        <p>
-                            <span>{% trans "Cores" %}: </span>
-                            {% if vm.cores %}
-                                <strong class="pull-right">{{vm.cores|floatformat}}</strong>
-                            {% else %}
-                                <strong class="pull-right">{{vm.cpu|floatformat}}</strong>
-                            {% endif %}
-                        </p>
-                        <p>
-                            <span>{% trans "Memory" %}: </span>
-                            <strong class="pull-right">{{vm.memory}} GB</strong>
-                        </p>
-                        <p>
-                            <span>{% trans "Disk space" %}: </span>
-                            <strong class="pull-right">{{vm.disk_size}} GB</strong>
-                        </p>
-                    </div>
-                    <div class="col-sm-12">
-                        <hr class="thin-hr">
-                    </div>
-                    {% if vm.vat > 0 or vm.discount.amount > 0 %}
+                    </p>
+                    <div class="row">
                         <div class="col-sm-6">
-                            <div class="subtotal-price">
-                                {% if vm.vat > 0 %}
-                                    <p>
-                                        <strong>{% trans "Subtotal" %} </strong>
-                                        <strong class="pull-right">{{vm.price|floatformat:2|intcomma}} CHF</strong>
-                                    </p>
-                                    <p>
-                                        <small>{% trans "VAT" %} ({{ vm.vat_percent|floatformat:2|intcomma }}%) </small>
-                                        <strong class="pull-right">{{vm.vat|floatformat:2|intcomma}} CHF</strong>
-                                    </p>
+                            {% if vm.created_at %}
+                                <p>
+                                    <span>{% trans "Period" %}: </span>
+                                    <span>
+                                        <span class="locale_date" data-format="YYYY/MM/DD">{{ vm.created_at|date:'Y-m-d h:i a' }}</span> - <span class="locale_date" data-format="YYYY/MM/DD">{{ subscription_end_date|date:'Y-m-d h:i a' }}</span>
+                                    </span>
+                                </p>
+                            {% endif %}
+                            <p>
+                                <span>{% trans "Cores" %}: </span>
+                                {% if vm.cores %}
+                                    <strong class="pull-right">{{vm.cores|floatformat}}</strong>
+                                {% else %}
+                                    <strong class="pull-right">{{vm.cpu|floatformat}}</strong>
                                 {% endif %}
-                                {% if vm.discount.amount > 0 %}
-                                    <p class="text-primary">
-                                        {%trans "Discount" as discount_name %}
-                                        <strong>{{ vm.discount.name|default:discount_name }} </strong>
-                                        <strong class="pull-right">- {{ vm.discount.amount }} CHF</strong>
-                                    </p>
-                                {% endif %}
-                            </div>
+                            </p>
+                            <p>
+                                <span>{% trans "Memory" %}: </span>
+                                <strong class="pull-right">{{vm.memory}} GB</strong>
+                            </p>
+                            <p>
+                                <span>{% trans "Disk space" %}: </span>
+                                <strong class="pull-right">{{vm.disk_size}} GB</strong>
+                            </p>
                         </div>
                         <div class="col-sm-12">
                             <hr class="thin-hr">
                         </div>
-                    {% endif %}
-                    <div class="col-sm-6">
-                        <p class="total-price">
-                            <strong>{% trans "Total" %} </strong>
-                            <strong class="pull-right">{% if vm.total_price %}{{vm.total_price|floatformat:2|intcomma}}{% else %}{{vm.price|floatformat:2|intcomma}}{% endif %} CHF</strong>
-                        </p>
+                        {% if vm.vat > 0 or vm.discount.amount > 0 %}
+                            <div class="col-sm-6">
+                                <div class="subtotal-price">
+                                    {% if vm.vat > 0 %}
+                                        <p>
+                                            <strong>{% trans "Subtotal" %} </strong>
+                                            <strong class="pull-right">{{vm.price|floatformat:2|intcomma}} CHF</strong>
+                                        </p>
+                                        <p>
+                                            <small>{% trans "VAT" %} ({{ vm.vat_percent|floatformat:2|intcomma }}%) </small>
+                                            <strong class="pull-right">{{vm.vat|floatformat:2|intcomma}} CHF</strong>
+                                        </p>
+                                    {% endif %}
+                                    {% if vm.discount.amount > 0 %}
+                                        <p class="text-primary">
+                                            {%trans "Discount" as discount_name %}
+                                            <strong>{{ vm.discount.name|default:discount_name }} </strong>
+                                            <strong class="pull-right">- {{ vm.discount.amount }} CHF</strong>
+                                        </p>
+                                    {% endif %}
+                                </div>
+                            </div>
+                            <div class="col-sm-12">
+                                <hr class="thin-hr">
+                            </div>
+                        {% endif %}
+                        <div class="col-sm-6">
+                            <p class="total-price">
+                                <strong>{% trans "Total" %} </strong>
+                                <strong class="pull-right">{% if vm.total_price %}{{vm.total_price|floatformat:2|intcomma}}{% else %}{{vm.price|floatformat:2|intcomma}}{% endif %} CHF</strong>
+                            </p>
+                        </div>
                     </div>
-                </div>
+                {% else %}
+                    <div class="row">
+                        <div class="col-sm-6">
+                            <p>
+                                <span>{% trans "Amount" %}: </span>
+                                <strong class="pull-right">{{order.price|floatformat:2|intcomma}} CHF</strong>
+                            </p>
+                            {% if order.subscription_id %}
+                            <p>
+                                <span>{% trans "Recurring" %}: </span>
+                                <strong class="pull-right">{{order.created_at|date:'d'|ordinal}} {% trans "of every month" %}</strong>
+                            </p>
+                            {% endif %}
+                        </div>
+                        <div class="col-sm-12">
+                            <hr class="thin-hr">
+                        </div>
+                    </div>
+                {% endif %}
             </div>
             <hr class="thin-hr">
         </div>
diff --git a/hosting/views.py b/hosting/views.py
index 68142a38..55347543 100644
--- a/hosting/views.py
+++ b/hosting/views.py
@@ -862,32 +862,18 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
                 raise Http404
 
         if obj is not None:
-            # invoice for previous order
-            try:
-                vm_detail = VMDetail.objects.get(vm_id=obj.vm_id)
-                context['vm'] = vm_detail.__dict__
-                context['vm']['name'] = '{}-{}'.format(
-                    context['vm']['configuration'], context['vm']['vm_id'])
-                price, vat, vat_percent, discount = get_vm_price_with_vat(
-                    cpu=context['vm']['cores'],
-                    ssd_size=context['vm']['disk_size'],
-                    memory=context['vm']['memory'],
-                    pricing_name=(obj.vm_pricing.name
-                                  if obj.vm_pricing else 'default')
-                )
-                context['vm']['vat'] = vat
-                context['vm']['price'] = price
-                context['vm']['discount'] = discount
-                context['vm']['vat_percent'] = vat_percent
-                context['vm']['total_price'] = price + vat - discount['amount']
-                context['subscription_end_date'] = vm_detail.end_date()
-            except VMDetail.DoesNotExist:
+            if obj.generic_payment_id is not None:
+                # generic payment case
+                logger.debug("Generic payment case")
+
+            else:
+                # invoice for previous order
+                logger.debug("Invoice of VM order")
                 try:
-                    manager = OpenNebulaManager(
-                        email=owner.email, password=owner.password
-                    )
-                    vm = manager.get_vm(obj.vm_id)
-                    context['vm'] = VirtualMachineSerializer(vm).data
+                    vm_detail = VMDetail.objects.get(vm_id=obj.vm_id)
+                    context['vm'] = vm_detail.__dict__
+                    context['vm']['name'] = '{}-{}'.format(
+                        context['vm']['configuration'], context['vm']['vm_id'])
                     price, vat, vat_percent, discount = get_vm_price_with_vat(
                         cpu=context['vm']['cores'],
                         ssd_size=context['vm']['disk_size'],
@@ -899,23 +885,43 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
                     context['vm']['price'] = price
                     context['vm']['discount'] = discount
                     context['vm']['vat_percent'] = vat_percent
-                    context['vm']['total_price'] = (
-                            price + vat - discount['amount']
-                    )
-                except WrongIdError:
-                    messages.error(
-                        self.request,
-                        _('The VM you are looking for is unavailable at the '
-                          'moment. Please contact Data Center Light support.')
-                    )
-                    self.kwargs['error'] = 'WrongIdError'
-                    context['error'] = 'WrongIdError'
-                except ConnectionRefusedError:
-                    messages.error(
-                        self.request,
-                        _('In order to create a VM, you need to create/upload '
-                          'your SSH KEY first.')
-                    )
+                    context['vm']['total_price'] = price + vat - discount['amount']
+                    context['subscription_end_date'] = vm_detail.end_date()
+                except VMDetail.DoesNotExist:
+                    try:
+                        manager = OpenNebulaManager(
+                            email=owner.email, password=owner.password
+                        )
+                        vm = manager.get_vm(obj.vm_id)
+                        context['vm'] = VirtualMachineSerializer(vm).data
+                        price, vat, vat_percent, discount = get_vm_price_with_vat(
+                            cpu=context['vm']['cores'],
+                            ssd_size=context['vm']['disk_size'],
+                            memory=context['vm']['memory'],
+                            pricing_name=(obj.vm_pricing.name
+                                          if obj.vm_pricing else 'default')
+                        )
+                        context['vm']['vat'] = vat
+                        context['vm']['price'] = price
+                        context['vm']['discount'] = discount
+                        context['vm']['vat_percent'] = vat_percent
+                        context['vm']['total_price'] = (
+                                price + vat - discount['amount']
+                        )
+                    except WrongIdError:
+                        messages.error(
+                            self.request,
+                            _('The VM you are looking for is unavailable at the '
+                              'moment. Please contact Data Center Light support.')
+                        )
+                        self.kwargs['error'] = 'WrongIdError'
+                        context['error'] = 'WrongIdError'
+                    except ConnectionRefusedError:
+                        messages.error(
+                            self.request,
+                            _('In order to create a VM, you need to create/upload '
+                              'your SSH KEY first.')
+                        )
         else:
             # new order, confirm payment
             if 'token' in self.request.session: