2016-06-04 12:04:17 +02:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2016-06-04 01:49:24 +02:00
|
|
|
from django.db.models import Sum
|
2016-06-04 04:29:21 +02:00
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
2016-06-04 04:37:56 +02:00
|
|
|
from refunding.forms import RefundFormPublic, PaymentForm
|
2016-06-04 01:55:27 +02:00
|
|
|
from refunding.models import Payment, Refund
|
2016-06-03 20:17:22 +02:00
|
|
|
|
2016-06-04 01:49:24 +02:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def not_refunded_payments(request):
|
|
|
|
payments = Payment.objects.filter(refund=None)
|
2016-06-04 03:49:37 +02:00
|
|
|
value_sum = payments.aggregate(Sum('value')).get('value__sum')
|
|
|
|
if value_sum:
|
|
|
|
value_sum /= 100
|
|
|
|
else:
|
|
|
|
value_sum = 0
|
|
|
|
|
2016-06-04 01:49:24 +02:00
|
|
|
context = {
|
|
|
|
'payments': payments,
|
2016-06-04 03:49:37 +02:00
|
|
|
'sum': value_sum,
|
2016-06-04 01:49:24 +02:00
|
|
|
'default_nothing': 'No payment to be refunded.'
|
|
|
|
}
|
2016-06-04 02:01:33 +02:00
|
|
|
return render(request, "refunding/payments.html", context)
|
2016-06-04 01:55:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def latest_refunds(request):
|
|
|
|
refunds = Refund.objects.all().order_by('date')[:20]
|
|
|
|
context = {
|
|
|
|
'refunds': refunds,
|
|
|
|
'default_nothing': 'No refund to show.'
|
|
|
|
}
|
2016-06-04 02:01:33 +02:00
|
|
|
return render(request, "refunding/refunds.html", context)
|
2016-06-04 03:44:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2016-06-04 12:04:17 +02:00
|
|
|
@permission_required('refunding.add_refund')
|
2016-06-04 03:44:41 +02:00
|
|
|
def new_refund(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RefundFormPublic(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
refund = form.save()
|
|
|
|
refund.user = request.user
|
|
|
|
refund.save()
|
|
|
|
return redirect('latest_refunds')
|
|
|
|
else:
|
|
|
|
form = RefundFormPublic()
|
|
|
|
|
|
|
|
context = {
|
2016-06-04 04:29:21 +02:00
|
|
|
'form': form,
|
|
|
|
'title': 'New refund'
|
2016-06-04 03:44:41 +02:00
|
|
|
}
|
2016-06-04 04:37:56 +02:00
|
|
|
|
2016-06-04 12:04:17 +02:00
|
|
|
return render(request, "refunding/refund_payment_detail.html", context)
|
2016-06-04 04:37:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2016-06-04 12:04:17 +02:00
|
|
|
@permission_required('refunding.add_payment')
|
2016-06-04 04:37:56 +02:00
|
|
|
def new_payment(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = PaymentForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
payment = form.save(commit=False)
|
|
|
|
payment.user = request.user
|
|
|
|
payment.save()
|
|
|
|
return redirect('not_refunded_payments')
|
|
|
|
else:
|
|
|
|
form = PaymentForm()
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'form': form,
|
|
|
|
'title': 'New payment'
|
|
|
|
}
|
2016-06-04 12:04:17 +02:00
|
|
|
|
2016-06-04 04:37:56 +02:00
|
|
|
return render(request, "refunding/refund_payment_detail.html", context)
|
2016-06-04 04:29:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2016-06-04 12:04:17 +02:00
|
|
|
@permission_required('refunding.change_refund')
|
2016-06-04 04:29:21 +02:00
|
|
|
def refund_edit(request, pk):
|
|
|
|
refund = get_object_or_404(Refund, pk=pk)
|
2016-06-04 12:04:17 +02:00
|
|
|
|
2016-06-04 04:29:21 +02:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = RefundFormPublic(request.POST, instance=refund)
|
|
|
|
if form.is_valid():
|
|
|
|
refund = form.save()
|
|
|
|
refund.user = request.user
|
|
|
|
refund.save()
|
|
|
|
return redirect('latest_refunds')
|
|
|
|
else:
|
|
|
|
form = RefundFormPublic(instance=refund)
|
2016-06-04 12:04:17 +02:00
|
|
|
|
2016-06-04 04:29:21 +02:00
|
|
|
context = {
|
|
|
|
'form': form,
|
|
|
|
'title': 'Edit refund'
|
|
|
|
}
|
2016-06-04 12:04:17 +02:00
|
|
|
|
2016-06-04 04:37:56 +02:00
|
|
|
return render(request, 'refunding/refund_payment_detail.html', context)
|