From bf053242deb8ba1ba58983995aeb2335c55f2aa0 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Mon, 25 Apr 2016 16:00:33 +1000 Subject: [PATCH] =?UTF-8?q?Closes=20#25=20=E2=80=94=20changes=20what=20inv?= =?UTF-8?q?oice=5Faccess=20will=20redirect=20to?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- registrasion/views.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/registrasion/views.py b/registrasion/views.py index c71cdc9d..0d9dba23 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -542,8 +542,14 @@ def _checkout_errors(request, errors): def invoice_access(request, access_code): - ''' Redirects to the first unpaid invoice for the attendee that matches - the given access code, if any. + ''' Redirects to an invoice for the attendee that matches the given access + code, if any. + + If the attendee has multiple invoices, we use the following tie-break: + + - If there's an unpaid invoice, show that, otherwise + - If there's a paid invoice, show the most recent one, otherwise + - Show the most recent invoid of all Arguments: @@ -552,21 +558,29 @@ def invoice_access(request, access_code): Returns: redirect: - Redirect to the first unpaid invoice for that user. + Redirect to the selected invoice for that user. Raises: - Http404: If there is no such invoice. + Http404: If the user has no invoices. ''' invoices = commerce.Invoice.objects.filter( user__attendee__access_code=access_code, - status=commerce.Invoice.STATUS_UNPAID, - ).order_by("issue_time") + ).order_by("-issue_time") + if not invoices: raise Http404() - invoice = invoices[0] + unpaid = invoices.filter(status=commerce.Invoice.STATUS_UNPAID) + paid = invoices.filter(status=commerce.Invoice.STATUS_PAID) + + if unpaid: + invoice = unpaid[0] # (should only be 1 unpaid invoice?) + elif paid: + invoice = paid[0] # Most recent paid invoice + else: + invoice = invoices[0] # Most recent of any invoices return redirect("invoice", invoice.id, access_code)