Member payment interface change.
[fsij-members-webapp.git] / membership / views.py
1 from django.shortcuts import render_to_response, get_object_or_404
2 from django.contrib.auth.decorators import login_required, user_passes_test
3 from fsij.membership.models import *
4 from django.http import HttpResponseRedirect
5 from django.core.urlresolvers import reverse
6 from fsij.settings import LOGIN_URL, FY_START_MONTH
7 import datetime, time, re
8 from fsij.accounting.models import LedgerEntry
9
10 def member_index(request, member):
11     return render_to_response('membership/member_index.html',
12                               {'member': member,
13                                })
14
15 def member_email(request, member, manage=False):
16     errors = None
17     try:
18         email = request.session['member_email_data']
19         del request.session['member_email_data']
20         errors = email['errors']
21     except (KeyError):
22         email = member.email
23     try: referrer = request.META['HTTP_REFERER']
24     except: referrer = None
25     if referrer:
26        parent_is_list = (not re.match(".*/list/$", referrer))
27     else: parent_is_list = None
28     return render_to_response('membership/member_email.html',
29                               {'member': member,
30                                'email': email,
31                                'errors': errors,
32                                'original_path': request.path,
33                                'reload_parent': parent_is_list and manage,
34                                })
35
36 def register_member_email(request, member):
37     email = request.POST['email']
38     errors = []
39     if not email:
40         errors.append('email')
41     if errors:
42         request.session['member_email_data'] = {
43             'email': email,
44             'errors': errors,}
45         return HttpResponseRedirect(request.POST['original_path'])
46     member.email = email
47     member.save()
48     if member.user:
49         member.user.email = email
50         member.user.save()
51     return HttpResponseRedirect(request.POST['original_path'])
52
53 def member_address(request, member, manage=False):
54     errors = None
55     try:
56         addr = request.session['member_address_data']
57         del request.session['member_address_data']
58         errors = addr['errors']
59     except (KeyError):
60         try:
61             addr = MemberAddress.objects.get(member=member)
62         except (MemberAddress.DoesNotExist):
63             addr = None
64     try: referrer = request.META['HTTP_REFERER']
65     except: referrer = None
66     if referrer:
67        parent_is_list = (not re.match(".*/list/$", referrer))
68     else: parent_is_list = None
69     return render_to_response('membership/member_address.html',
70                               {'member': member,
71                                'addr': addr,
72                                'errors': errors,
73                                'original_path': request.path,
74                                'reload_parent': parent_is_list and manage,
75                                })
76
77 def register_member_address(request, member):
78     try: request.POST['contact_is_home']
79     except: contact_is_home = False
80     else:   contact_is_home = True
81     zip_code = request.POST['zip_code']
82     address_line1 = request.POST['address_line1']
83     address_line2 = request.POST['address_line2']
84     address_line3 = request.POST['address_line3']
85     affiliation = request.POST['affiliation']
86     errors = []
87     if not contact_is_home and not affiliation:
88         errors.append('affiliation')
89     if not zip_code:
90         errors.append('zip_code')
91     if not address_line1:
92         errors.append('address_line1')
93     if errors:
94         request.session['member_address_data'] = {
95             'contact_is_home': contact_is_home,
96             'zip_code': zip_code,
97             'address_line1': address_line1,
98             'address_line2': address_line2,
99             'address_line3': address_line3,
100             'affiliation': affiliation,
101             'errors': errors,}
102         return HttpResponseRedirect(request.POST['original_path'])
103     date_updated = datetime.date.today()
104     try:
105         addr = MemberAddress.objects.get(member=member)
106         addr.contact_is_home = contact_is_home
107         addr.zip_code = zip_code
108         addr.address_line1 = address_line1
109         addr.address_line2 = address_line2
110         addr.address_line3 = address_line3
111         addr.affiliation = affiliation
112         addr.date_updated = date_updated
113     except (MemberAddress.DoesNotExist):
114         addr = MemberAddress(member = member,
115                              contact_is_home = contact_is_home,
116                              zip_code = zip_code,
117                              address_line1 = address_line1,
118                              address_line2 = address_line2,
119                              address_line3 = address_line3,
120                              affiliation = affiliation,
121                              date_updated = date_updated)
122     addr.save()
123     return HttpResponseRedirect(request.POST['original_path'])
124
125 def member_payment(request, member, manage=False):
126     try:
127         payments = member.memberpayment_set.order_by('-fee_year', '-date')
128     except (MemberPayment.DoesNotExist):
129         payments = None
130     if manage:
131         start_year = member.date_joined.year
132         if member.date_joined.month < FY_START_MONTH:
133             start_year = start_year - 1
134         end_today = datetime.date.today()
135         end_year = end_today.year
136         if end_today.month < FY_START_MONTH:
137             end_year = end_year - 1
138         fee_list = range(start_year, end_year + 1)
139         if member.member_type != 'A':
140             fee_list.append(9999) # Admission fee
141         fee_list_in_payments = map(lambda (p): p.fee_year, member.memberpayment_set.all())
142         fee_list = filter(lambda (f): not f in fee_list_in_payments, fee_list)
143     else:
144         fee_list = None
145     try: referrer = request.META['HTTP_REFERER']
146     except: referrer = None
147     if referrer:
148        parent_is_list = (not re.match(".*/list/$", referrer))
149     else: parent_is_list = None
150     return render_to_response('membership/member_payment.html',
151                               {'member': member,
152                                'payments': payments,
153                                'reload_parent': parent_is_list and manage,
154                                'manage' : manage,
155                                'fee_list' : fee_list,
156                                'member_type_list': MEMBER_TYPE })
157
158 def add_member_payment(request, member):
159     p = MemberPayment(member = member,
160                       date = datetime.date(*time.strptime(request.POST['date'], '%Y-%m-%d')[:3]),
161                       member_type = request.POST['member_type'],
162                       fee_year = request.POST['fee_year'],
163                       contribution = request.POST['contribution'])
164     p.save()
165     return HttpResponseRedirect(reverse('fsij.membership.views.manage_member_payment', args=(member.member_id,)))
166
167 def delete_member_payment(request, member, payment_id):
168     p = MemberPayment.objects.get(pk = payment_id)
169     p.delete()
170     return HttpResponseRedirect(reverse('fsij.membership.views.manage_member_payment', args=(member.member_id,)))
171
172 def add_member_payment_with_le(request):
173     le_id = request.POST['le_id']
174     member_id = request.POST['member_id']
175     le = get_object_or_404(LedgerEntry, pk=le_id)
176     member = get_object_or_404(Member, member_id=member_id)
177     date = le.t.date
178     if member.member_type != 'A':
179         contribution = 1
180     else:
181         contribution = member.corporatemember.contribution
182     p = MemberPayment(member = member,
183                       date = date,
184                       member_type = member.member_type,
185                       fee_year = 0,
186                       contribution = contribution,
187                       le = le)
188     p.save()
189     return HttpResponseRedirect(reverse('fsij.membership.views.manage_edit_member_payment', args=(p.id,)))
190
191 def edit_member_payment(request, member_payment):
192     try: referrer = request.META['HTTP_REFERER']
193     except: referrer = None
194     errors = None
195     try:
196         member_payment_data = request.session['member_payment_data']
197         del request.session['member_payment_data']
198         errors = member_payment_data['errors']
199     except (KeyError):
200         member_payment_data = None
201     #
202     le = member_payment.le
203     if le:
204         date = le.t.date
205     else:
206         date = datetime.date.today()
207     member = member_payment.member
208     if member:
209         start_year = member.date_joined.year
210         if member.date_joined.month < FY_START_MONTH:
211             start_year = start_year - 1
212         end_year = date.year
213         if date.month < FY_START_MONTH:
214             end_year = end_year - 1
215         fee_list = range(start_year, end_year + 1)
216         if member.member_type != 'A':
217             fee_list.append(0)    # Advance payment
218             fee_list.append(9999) # Admission fee
219         fee_list_in_payments = map(lambda (p): p.fee_year, member.memberpayment_set.all())
220         fee_list = filter(lambda (f): not f in fee_list_in_payments, fee_list)
221     else:
222         fee_list = None
223     return render_to_response('membership/edit_member_payment.html',
224                               {'date': date,
225                                'errors': errors,
226                                'member': member,
227                                'members': Member.objects.all(),
228                                'fee_list': fee_list,
229                                'member_type_list': MEMBER_TYPE,
230                                'original_path': referrer,
231                                })
232
233 def register_member_payment(request, member_payment):
234     member_id = request.POST['member_id']
235     #
236     errors = []
237     if not member_id:
238         errors.append('member_id')
239     if errors:
240         request.session['member_payment_data'] = {
241             'member_id': member_id,
242             'errors': errors,}
243         return HttpResponseRedirect(request.POST['original_path'])
244     member = get_object_or_404(Member, member_id=member_id)
245     member_type = request.POST['member_type']
246     contribution = request.POST['contribution']
247     fee_year = request.POST['fee_year']
248     member_payment.member = member
249     member_payment.member_type = member_type
250     member_payment.contribution = contribution
251     member_payment.fee_year = fee_year
252     member_payment.save()
253     return HttpResponseRedirect(request.POST['original_path'])
254
255 @login_required
256 def index(request):
257     member = request.user.get_profile()
258     return member_index(request, member)
259
260 @login_required
261 def email(request):
262     member = request.user.get_profile()
263     return member_email(request, member)
264
265 @login_required
266 def register_email(request):
267     member = request.user.get_profile()
268     return register_member_email(request, member)
269
270 @login_required
271 def address(request):
272     member = request.user.get_profile()
273     return member_address(request, member)
274
275 @login_required
276 def register_address(request):
277     member = request.user.get_profile()
278     return register_member_address(request, member)
279
280 @login_required
281 def payment(request):
282     member = request.user.get_profile()
283     return member_payment(request, member)
284
285
286 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
287 def manage_member(request, member_id, new):
288 ### XXX New member!!
289 ### XXX CorporateMember!!
290     errors = None
291     try:
292         member_data = request.session['member_data']
293         del request.session['member_data']
294         errors = member_data['errors']
295         member = member_data
296     except (KeyError):
297         member = Member.objects.get(member_id=member_id)
298     try: referrer = request.META['HTTP_REFERER']
299     except: referrer = None
300     if referrer:
301        parent_is_list = (not re.match(".*/list/$", referrer))
302     else: parent_is_list = None
303     return render_to_response('membership/member_edit.html',
304                               {'member': member,
305                                'member_type_list': MEMBER_TYPE,
306                                'original_path': request.path,
307                                'reload_parent': parent_is_list,
308                                'errors': errors,
309                                })
310
311 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
312 def manage_register_member(request, member_id):
313     member = Member.objects.get(member_id=member_id)
314     errors = []
315     member_type = request.POST['member_type']
316     mt_list = filter(lambda x: x[0] == member_type, MEMBER_TYPE)
317     if mt_list == ():
318         errors.append('member_type')
319     name_in_alphabet = request.POST['name_in_alphabet']
320     date_joined = request.POST['date_joined']
321     date_quit = request.POST['date_quit']
322     advance_payment = request.POST['advance_payment']
323     if not name_in_alphabet:
324         errors.append('name_in_alphabet')
325     if errors:
326         request.session['member_data'] = {
327             'member_id': member.member_id,
328             'member_type' : member_type,
329             'name_in_alphabet': name_in_alphabet,
330             'date_joined': date_joined,
331             'date_quit': date_quit,
332             'advance_payment': advance_payment,
333             'errors': errors,
334             }
335         return HttpResponseRedirect(request.POST['original_path'])
336     member.member_type = member_type
337     member.name_in_alphabet = name_in_alphabet
338     if date_joined and date_joined != "None":
339         member.date_joined = datetime.date(*time.strptime(date_joined, '%Y-%m-%d')[:3])
340         
341     if date_quit and date_quit != "None":
342         member.date_quit = datetime.date(*time.strptime(date_quit, '%Y-%m-%d')[:3])
343     member.advance_payment = advance_payment
344     member.save()
345     return HttpResponseRedirect(request.POST['original_path'])
346
347 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
348 def manage_member_email(request, member_id):
349     member = get_object_or_404(Member, member_id=member_id)
350     return member_email(request, member, manage=True)
351
352 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
353 def manage_register_member_email(request, member_id):
354     member = get_object_or_404(Member, member_id=member_id)
355     return register_member_email(request, member)
356
357 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
358 def manage_member_address(request, member_id):
359     member = get_object_or_404(Member, member_id=member_id)
360     return member_address(request, member, manage=True)
361
362 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
363 def manage_register_member_address(request, member_id):
364     member = get_object_or_404(Member, member_id=member_id)
365     return register_member_address(request, member)
366
367 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
368 def manage_member_payment(request, member_id):
369     member = get_object_or_404(Member, member_id=member_id)
370     return member_payment(request, member, manage=True)
371
372 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
373 def manage_add_member_payment(request, member_id):
374     member = get_object_or_404(Member, member_id=member_id)
375     return add_member_payment(request, member)
376
377 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
378 def manage_delete_member_payment(request, member_id, payment_id):
379     member = get_object_or_404(Member, member_id=member_id)
380     return delete_member_payment(request, member, payment_id)
381
382 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
383 def manage_add_member_payment_with_le(request):
384     return add_member_payment_with_le(request)
385
386 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
387 def manage_edit_member_payment(request, member_payment_id):
388     member_payment = get_object_or_404(MemberPayment, pk=member_payment_id)
389     return edit_member_payment(request, member_payment)
390
391 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
392 def manage_register_member_payment(request, member_payment_id):
393     member_payment = get_object_or_404(MemberPayment, pk=member_payment_id)
394     return register_member_payment(request, member_payment)
395
396 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
397 def member_list_top(request):
398     return render_to_response('membership/member_list_top.html',
399                               { })
400
401 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
402 def member_list(request):
403     member_list = Member.objects.filter(date_quit__isnull=True).order_by('member_id')
404     return render_to_response('membership/member_list.html',
405                               {'members': member_list,
406                                })
407
408 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
409 def payment_list(request):
410     payments = MemberPayment.objects.all().order_by('date')
411     return render_to_response('membership/payment_list.html',
412                               {'payments': payments,
413                                })
414
415 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
416 def valid_member_list(request,year):
417     year_value = int(year)
418     member_list = get_valid_member(year_value)
419     member_list_old = get_valid_member(year_value-1)
420     added = []
421     stable = []
422     deleted = []
423     for m in member_list:
424         if not m in member_list_old:
425             added.append(m)
426         else:
427             stable.append(m)
428     for m in member_list_old:
429         if not m in member_list:
430             deleted.append(m)
431     return render_to_response('membership/valid_member_list.html',
432                               {'stable': stable,
433                                'added': added,
434                                'deleted': deleted,
435                                'year': year,
436                                })
437
438 @user_passes_test(lambda u: u.has_perm('membership.can_manage'), LOGIN_URL)
439 def member_browse(request):
440     if 'query' in request.GET:
441         if request.GET['current_past_all'] == "current":
442             members = Member.objects.filter(date_quit__isnull=True).order_by('member_id')
443         elif request.GET['current_past_all'] == "past":
444             members = Member.objects.filter(date_quit__isnull=False).order_by('member_id')
445         else:
446             members = Member.objects.all()
447     else:
448         members = None
449     return render_to_response('membership/member_browse.html',
450                               {'req': request,
451                                'members': members,
452                                })