From 965cc3adf94ecf9d2043faf08fd19a9c2f369c42 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 22 Apr 2019 11:08:53 +0200 Subject: [PATCH] Show correct period start and period end in invoice list Previously, we used to take the first element in the bill line item, for invoice start and end date. However, it can be faulty in cases where the first item does not represent a VM itself (which we are sure has a monthly subscription and a correct start and end dates) --- hosting/models.py | 1 + hosting/templates/hosting/invoices.html | 2 +- hosting/views.py | 8 +++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/hosting/models.py b/hosting/models.py index 7b7a7792..0d42996e 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -521,6 +521,7 @@ class HostingBillLineItem(AssignPermissionsMixin, models.Model): ) return item_detail + class VMDetail(models.Model): user = models.ForeignKey(CustomUser) vm_id = models.IntegerField(default=0) diff --git a/hosting/templates/hosting/invoices.html b/hosting/templates/hosting/invoices.html index f2486111..c0956a58 100644 --- a/hosting/templates/hosting/invoices.html +++ b/hosting/templates/hosting/invoices.html @@ -31,7 +31,7 @@ {{ invoice.order.vm_id }} {{ ips|get_value_from_dict:invoice.invoice_number|join:"
" }} {% with line_items|get_value_from_dict:invoice.invoice_number as line_items_to_show %} - {{ line_items_to_show.0.period_start | date:'Y-m-d' }} — {{ line_items_to_show.0.period_end | date:'Y-m-d' }} + {{ period_start | date:'Y-m-d' }} — {{ period_end | date:'Y-m-d' }} {% endwith %} {{ invoice.total_in_chf|floatformat:2|intcomma }} diff --git a/hosting/views.py b/hosting/views.py index 2d7a3093..901dae20 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1190,7 +1190,13 @@ class InvoiceListView(LoginRequiredMixin, ListView): try: vm_detail = VMDetail.objects.get(vm_id=mhb.order.vm_id) ips_dict[mhb.invoice_number] = [vm_detail.ipv6, vm_detail.ipv4] - line_items_dict[mhb.invoice_number] = HostingBillLineItem.objects.filter(monthly_hosting_bill=mhb) + all_line_items = HostingBillLineItem.objects.filter(monthly_hosting_bill=mhb) + for line_item in all_line_items: + if line_item.get_item_detail_str() != "": + context['period_start'] = line_item.period_start + context['period_end'] = line_item.period_end + break + line_items_dict[mhb.invoice_number] = all_line_items except VMDetail.DoesNotExist as dne: ips_dict[mhb.invoice_number] = ['--'] logger.debug("VMDetail for {} doesn't exist".format(