diff --git a/registrasion/templatetags/registrasion_tags.py b/registrasion/templatetags/registrasion_tags.py index 7d618171..07ea7c14 100644 --- a/registrasion/templatetags/registrasion_tags.py +++ b/registrasion/templatetags/registrasion_tags.py @@ -16,6 +16,16 @@ def available_categories(context): return CategoryController.available_categories(context.request.user) +@register.assignment_tag(takes_context=True) +def available_credit(context): + ''' Returns the amount of unclaimed credit available for this user. ''' + notes = rego.CreditNote.unclaimed().filter( + invoice__user=context.request.user, + ) + ret = notes.values("amount").aggregate(Sum("amount"))["amount__sum"] or 0 + return 0 - ret + + @register.assignment_tag(takes_context=True) def invoices(context): ''' Returns all of the invoices that this user has. ''' diff --git a/registrasion/urls.py b/registrasion/urls.py index 0949e4b4..b927edbf 100644 --- a/registrasion/urls.py +++ b/registrasion/urls.py @@ -10,6 +10,8 @@ urlpatterns = patterns( url(r"^invoice/([0-9]+)/([A-Z0-9]+)$", views.invoice, name="invoice"), url(r"^invoice/([0-9]+)/manual_payment$", views.manual_payment, name="manual_payment"), + url(r"^invoice/([0-9]+)/refund$", + views.refund, name="refund"), url(r"^invoice_access/([A-Z0-9]+)$", views.invoice_access, name="invoice_access"), url(r"^profile$", "edit_profile", name="attendee_edit"), diff --git a/registrasion/views.py b/registrasion/views.py index f0795647..509473aa 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -524,3 +524,24 @@ def manual_payment(request, invoice_id): } return render(request, "registrasion/manual_payment.html", data) + + +@login_required +def refund(request, invoice_id): + ''' Allows staff to refund payments against an invoice and request a + credit note.''' + + if not request.user.is_staff: + raise Http404() + + invoice_id = int(invoice_id) + inv = get_object_or_404(rego.Invoice, pk=invoice_id) + current_invoice = InvoiceController(inv) + + try: + current_invoice.refund() + messages.success(request, "This invoice has been refunded.") + except ValidationError as ve: + messages.error(request, ve) + + return redirect("invoice", invoice_id)