add csv export of invoice list

This commit is contained in:
Thomas Steen Rasmussen 2021-08-11 09:36:57 +02:00
parent 8ddab4a600
commit b0656ff911
4 changed files with 39 additions and 1 deletions

View file

@ -18,7 +18,7 @@
<td>{{ invoice.id }}</td> <td>{{ invoice.id }}</td>
<td>{% if invoice.order %}{{ invoice.order.user.username }} &lt;{{ invoice.order.user.email }}&gt;{% else %}{{ invoice.customorder.customer }}{% endif %}</td> <td>{% if invoice.order %}{{ invoice.order.user.username }} &lt;{{ invoice.order.user.email }}&gt;{% else %}{{ invoice.customorder.customer }}{% endif %}</td>
<td>{{ invoice.created|date }}</td> <td>{{ invoice.created|date }}</td>
<td>{% if invoice.order %}{{ invoice.order.total }} DKK{% else %}{{ invoice.customorder.amount }}{% endif %}</td> <td>{% if invoice.order %}{{ invoice.order.total }}{% else %}{{ invoice.customorder.amount }}{% endif %}&nbsp;DKK</td>
<td>{{ invoice.get_order }}</td> <td>{{ invoice.get_order }}</td>
<td>{{ invoice.get_order.paid|truefalseicon }} {% if invoice.get_order.paid %}paid{% else %}unpaid{% endif %}</td> <td>{{ invoice.get_order.paid|truefalseicon }} {% if invoice.get_order.paid %}paid{% else %}unpaid{% endif %}</td>
<td> <td>

View file

@ -12,6 +12,9 @@ Invoices | {{ block.super }}
This table shows all invoices in the system. This table shows all invoices in the system.
</div> </div>
<a href="{% url 'backoffice:index' camp_slug=camp.slug %}" class="btn btn-default"><i class="fas fa-undo"></i> Backoffice</a>
<a href="{% url 'backoffice:invoice_list_csv' camp_slug=camp.slug %}" class="btn btn-primary"><i class="fas fa-file-csv"></i> CSV</a>
{% include 'includes/invoice_list_panel.html' %} {% include 'includes/invoice_list_panel.html' %}
{% endblock content %} {% endblock content %}

View file

@ -57,6 +57,7 @@ from .views import (
FacilityTypeListView, FacilityTypeListView,
FacilityTypeUpdateView, FacilityTypeUpdateView,
FacilityUpdateView, FacilityUpdateView,
InvoiceListCSVView,
InvoiceListView, InvoiceListView,
IrcOverView, IrcOverView,
MerchandiseOrdersView, MerchandiseOrdersView,
@ -670,6 +671,11 @@ urlpatterns = [
include( include(
[ [
path("", InvoiceListView.as_view(), name="invoice_list"), path("", InvoiceListView.as_view(), name="invoice_list"),
path(
"csv/",
InvoiceListCSVView.as_view(),
name="invoice_list_csv",
),
] ]
), ),
), ),

View file

@ -1,3 +1,4 @@
import csv
import logging import logging
import os import os
@ -6,6 +7,7 @@ from django.contrib import messages
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.files import File from django.core.files import File
from django.db.models import Count, Q, Sum from django.db.models import Count, Q, Sum
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
@ -451,3 +453,30 @@ class RevenueDetailView(CampViewMixin, EconomyTeamPermissionMixin, UpdateView):
class InvoiceListView(CampViewMixin, EconomyTeamPermissionMixin, ListView): class InvoiceListView(CampViewMixin, EconomyTeamPermissionMixin, ListView):
model = Invoice model = Invoice
template_name = "invoice_list.html" template_name = "invoice_list.html"
class InvoiceListCSVView(CampViewMixin, EconomyTeamPermissionMixin, ListView):
"""
CSV export of invoices for bookkeeping stuff
"""
def get(self, request, *args, **kwargs):
response = HttpResponse(content_type="text/csv")
response[
"Content-Disposition"
] = f'attachment; filename="bornhack-infoices-{timezone.now()}.csv"'
writer = csv.writer(response)
writer.writerow(["invoice", "invoice_date", "amount_dkk", "order", "paid"])
for invoice in Invoice.objects.all():
writer.writerow(
[
invoice.id,
invoice.created.date(),
invoice.order.total
if invoice.order
else invoice.customorder.amount,
invoice.get_order,
invoice.get_order.paid,
]
)
return response