2018-01-22 17:58:51 +01:00
|
|
|
import calendar
|
|
|
|
|
2016-06-04 13:17:18 +02:00
|
|
|
from django.contrib import messages
|
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
|
|
|
|
2016-06-04 12:13:53 +02:00
|
|
|
#############################
|
|
|
|
# -------- Refunds -------- #
|
|
|
|
#############################
|
2016-06-04 01:55:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def latest_refunds(request):
|
2016-08-17 17:37:46 +02:00
|
|
|
refunds = Refund.objects.all().order_by('-date', '-id')[:20]
|
2016-06-04 01:55:27 +02:00
|
|
|
context = {
|
|
|
|
'refunds': refunds,
|
2016-10-27 09:35:44 +02:00
|
|
|
'default_nothing': 'Aucun remboursement à afficher.'
|
2016-06-04 01:55:27 +02:00
|
|
|
}
|
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()
|
2016-07-28 14:04:13 +02:00
|
|
|
|
2016-10-27 09:35:44 +02:00
|
|
|
messages.success(request, 'Remboursement créé avec succès')
|
2016-06-04 03:44:41 +02:00
|
|
|
return redirect('latest_refunds')
|
|
|
|
else:
|
|
|
|
form = RefundFormPublic()
|
|
|
|
|
|
|
|
context = {
|
2016-06-04 04:29:21 +02:00
|
|
|
'form': form,
|
2016-10-27 09:35:44 +02:00
|
|
|
'title': 'Nouveau remboursement'
|
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
|
|
|
|
|
|
|
|
2016-06-04 12:13:53 +02:00
|
|
|
@login_required
|
|
|
|
@permission_required('refunding.change_refund')
|
|
|
|
def refund_edit(request, pk):
|
|
|
|
refund = get_object_or_404(Refund, pk=pk)
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RefundFormPublic(request.POST, instance=refund)
|
|
|
|
if form.is_valid():
|
|
|
|
refund = form.save()
|
|
|
|
refund.user = request.user
|
|
|
|
refund.save()
|
2016-07-28 14:04:13 +02:00
|
|
|
|
2016-10-27 09:35:44 +02:00
|
|
|
messages.success(request, 'Remboursement modifié avec succès')
|
2016-06-04 12:13:53 +02:00
|
|
|
return redirect('latest_refunds')
|
|
|
|
else:
|
|
|
|
form = RefundFormPublic(instance=refund)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'form': form,
|
2016-10-27 09:35:44 +02:00
|
|
|
'title': 'Modifier un remboursement',
|
2016-06-04 13:17:18 +02:00
|
|
|
'refund': refund
|
2016-06-04 12:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return render(request, 'refunding/refund_payment_detail.html', context)
|
|
|
|
|
|
|
|
|
2016-06-04 13:17:18 +02:00
|
|
|
@login_required
|
|
|
|
@permission_required('refunding.delete_refund')
|
|
|
|
def refund_delete(request, pk):
|
|
|
|
refund = get_object_or_404(Refund, pk=pk)
|
|
|
|
|
|
|
|
refund.delete()
|
|
|
|
|
2016-10-27 09:35:44 +02:00
|
|
|
messages.success(request, "Remboursement supprimé avec succès")
|
2016-06-04 13:17:18 +02:00
|
|
|
|
|
|
|
return latest_refunds(request)
|
|
|
|
|
|
|
|
|
2016-06-04 12:13:53 +02:00
|
|
|
##############################
|
|
|
|
# -------- Payments -------- #
|
|
|
|
##############################
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def not_refunded_payments(request):
|
2016-08-17 17:37:46 +02:00
|
|
|
payments = Payment.objects.filter(refund=None).order_by('-date', '-id')
|
2016-06-04 12:13:53 +02:00
|
|
|
value_sum = payments.aggregate(Sum('value')).get('value__sum')
|
|
|
|
if value_sum:
|
|
|
|
value_sum /= 100
|
|
|
|
else:
|
|
|
|
value_sum = 0
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'payments': payments,
|
|
|
|
'sum': value_sum,
|
2016-10-27 09:35:44 +02:00
|
|
|
'default_nothing': 'Rien à rembourser :)'
|
2016-06-04 12:13:53 +02:00
|
|
|
}
|
|
|
|
return render(request, "refunding/payments.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()
|
2016-07-28 14:04:13 +02:00
|
|
|
|
2016-10-27 09:35:44 +02:00
|
|
|
messages.success(request, 'Paiement créé avec succès')
|
2016-06-04 04:37:56 +02:00
|
|
|
return redirect('not_refunded_payments')
|
|
|
|
else:
|
|
|
|
form = PaymentForm()
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'form': form,
|
2016-10-27 09:35:44 +02:00
|
|
|
'title': 'Nouveau paiement'
|
2016-06-04 04:37:56 +02:00
|
|
|
}
|
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:13:53 +02:00
|
|
|
@permission_required('refunding.change_payment')
|
|
|
|
def payment_edit(request, pk):
|
|
|
|
payment = get_object_or_404(Payment, pk=pk)
|
2016-06-04 12:04:17 +02:00
|
|
|
|
2016-06-04 04:29:21 +02:00
|
|
|
if request.method == 'POST':
|
2016-06-04 12:13:53 +02:00
|
|
|
form = PaymentForm(request.POST, instance=payment)
|
2016-06-04 04:29:21 +02:00
|
|
|
if form.is_valid():
|
2016-06-04 12:13:53 +02:00
|
|
|
payment = form.save(commit=False)
|
|
|
|
payment.user = request.user
|
|
|
|
payment.save()
|
2016-07-28 14:04:13 +02:00
|
|
|
|
2016-10-27 09:35:44 +02:00
|
|
|
messages.success(request, 'Paiement modifié avec succès')
|
2016-06-04 12:13:53 +02:00
|
|
|
return redirect('not_refunded_payments')
|
2016-06-04 04:29:21 +02:00
|
|
|
else:
|
2016-06-04 12:13:53 +02:00
|
|
|
form = PaymentForm(instance=payment)
|
2016-06-04 12:04:17 +02:00
|
|
|
|
2016-06-04 04:29:21 +02:00
|
|
|
context = {
|
|
|
|
'form': form,
|
2016-10-27 09:35:44 +02:00
|
|
|
'title': 'Modifier un paiement',
|
2016-06-04 13:17:18 +02:00
|
|
|
'payment': payment
|
2016-06-04 04:29:21 +02:00
|
|
|
}
|
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 13:17:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@permission_required('refunding.delete_payment')
|
|
|
|
def payment_delete(request, pk):
|
|
|
|
payment = get_object_or_404(Payment, pk=pk)
|
|
|
|
|
|
|
|
payment.delete()
|
|
|
|
|
2016-10-27 09:35:44 +02:00
|
|
|
messages.success(request, "Paiement supprimé avec succès")
|
2016-06-04 13:17:18 +02:00
|
|
|
|
|
|
|
return not_refunded_payments(request)
|
2016-08-17 17:36:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def already_refunded_payments(request):
|
|
|
|
refunded = Payment.objects.exclude(refund=None).order_by('-date', '-id')
|
|
|
|
value_sum = refunded.aggregate(Sum('value')).get('value__sum')
|
|
|
|
if value_sum:
|
|
|
|
value_sum /= 100
|
|
|
|
else:
|
|
|
|
value_sum = 0
|
|
|
|
|
2018-01-22 17:58:51 +01:00
|
|
|
start_date = refunded.last().date
|
|
|
|
start_date = start_date.replace(day=1)
|
|
|
|
|
|
|
|
end_date = refunded.first().date
|
|
|
|
month = end_date.month
|
|
|
|
year = end_date.year + month // 12
|
|
|
|
month = month % 12 + 1
|
|
|
|
end_date = end_date.replace(year=year, month=month, day=1)
|
|
|
|
|
|
|
|
diff = (end_date.year - start_date.year) * 12 + end_date.month - start_date.month
|
|
|
|
|
|
|
|
monthly = value_sum / diff
|
|
|
|
|
2016-08-17 17:36:19 +02:00
|
|
|
context = {
|
|
|
|
'refunded': refunded,
|
|
|
|
'sum': value_sum,
|
2018-01-22 17:58:51 +01:00
|
|
|
'monthly': monthly,
|
2016-10-27 09:35:44 +02:00
|
|
|
'default_nothing': 'Aucun paiement remboursé :)'
|
2016-08-17 17:36:19 +02:00
|
|
|
}
|
|
|
|
return render(request, "refunding/refunded_payments.html", context)
|