Member payment interface change.
[fsij-members-webapp.git] / membership / views.py
index 8278db0..76f2cd4 100644 (file)
@@ -5,6 +5,7 @@ from django.http import HttpResponseRedirect
 from django.core.urlresolvers import reverse
 from fsij.settings import LOGIN_URL, FY_START_MONTH
 import datetime, time, re
+from fsij.accounting.models import LedgerEntry
 
 def member_index(request, member):
     return render_to_response('membership/member_index.html',
@@ -16,7 +17,7 @@ def member_email(request, member, manage=False):
     try:
         email = request.session['member_email_data']
         del request.session['member_email_data']
-        errors = addr['errors']
+        errors = email['errors']
     except (KeyError):
         email = member.email
     try: referrer = request.META['HTTP_REFERER']
@@ -168,6 +169,89 @@ def delete_member_payment(request, member, payment_id):
     p.delete()
     return HttpResponseRedirect(reverse('fsij.membership.views.manage_member_payment', args=(member.member_id,)))
 
+def add_member_payment_with_le(request):
+    le_id = request.POST['le_id']
+    member_id = request.POST['member_id']
+    le = get_object_or_404(LedgerEntry, pk=le_id)
+    member = get_object_or_404(Member, member_id=member_id)
+    date = le.t.date
+    if member.member_type != 'A':
+        contribution = 1
+    else:
+        contribution = member.corporatemember.contribution
+    p = MemberPayment(member = member,
+                      date = date,
+                      member_type = member.member_type,
+                      fee_year = 0,
+                      contribution = contribution,
+                      le = le)
+    p.save()
+    return HttpResponseRedirect(reverse('fsij.membership.views.manage_edit_member_payment', args=(p.id,)))
+
+def edit_member_payment(request, member_payment):
+    try: referrer = request.META['HTTP_REFERER']
+    except: referrer = None
+    errors = None
+    try:
+        member_payment_data = request.session['member_payment_data']
+        del request.session['member_payment_data']
+        errors = member_payment_data['errors']
+    except (KeyError):
+        member_payment_data = None
+    #
+    le = member_payment.le
+    if le:
+        date = le.t.date
+    else:
+        date = datetime.date.today()
+    member = member_payment.member
+    if member:
+        start_year = member.date_joined.year
+        if member.date_joined.month < FY_START_MONTH:
+            start_year = start_year - 1
+        end_year = date.year
+        if date.month < FY_START_MONTH:
+            end_year = end_year - 1
+        fee_list = range(start_year, end_year + 1)
+        if member.member_type != 'A':
+            fee_list.append(0)    # Advance payment
+            fee_list.append(9999) # Admission fee
+        fee_list_in_payments = map(lambda (p): p.fee_year, member.memberpayment_set.all())
+        fee_list = filter(lambda (f): not f in fee_list_in_payments, fee_list)
+    else:
+        fee_list = None
+    return render_to_response('membership/edit_member_payment.html',
+                              {'date': date,
+                               'errors': errors,
+                               'member': member,
+                               'members': Member.objects.all(),
+                               'fee_list': fee_list,
+                               'member_type_list': MEMBER_TYPE,
+                               'original_path': referrer,
+                               })
+
+def register_member_payment(request, member_payment):
+    member_id = request.POST['member_id']
+    #
+    errors = []
+    if not member_id:
+        errors.append('member_id')
+    if errors:
+        request.session['member_payment_data'] = {
+            'member_id': member_id,
+            'errors': errors,}
+        return HttpResponseRedirect(request.POST['original_path'])
+    member = get_object_or_404(Member, member_id=member_id)
+    member_type = request.POST['member_type']
+    contribution = request.POST['contribution']
+    fee_year = request.POST['fee_year']
+    member_payment.member = member
+    member_payment.member_type = member_type
+    member_payment.contribution = contribution
+    member_payment.fee_year = fee_year
+    member_payment.save()
+    return HttpResponseRedirect(request.POST['original_path'])
+
 @login_required
 def index(request):
     member = request.user.get_profile()
@@ -295,6 +379,20 @@ def manage_delete_member_payment(request, member_id, payment_id):
     member = get_object_or_404(Member, member_id=member_id)
     return delete_member_payment(request, member, payment_id)
 
+@user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
+def manage_add_member_payment_with_le(request):
+    return add_member_payment_with_le(request)
+
+@user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
+def manage_edit_member_payment(request, member_payment_id):
+    member_payment = get_object_or_404(MemberPayment, pk=member_payment_id)
+    return edit_member_payment(request, member_payment)
+
+@user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
+def manage_register_member_payment(request, member_payment_id):
+    member_payment = get_object_or_404(MemberPayment, pk=member_payment_id)
+    return register_member_payment(request, member_payment)
+
 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
 def member_list_top(request):
     return render_to_response('membership/member_list_top.html',