Member payment interface change.
[fsij-members-webapp.git] / accounting / views.py
1 # -*- coding: utf-8-*-
2 from django.shortcuts import render_to_response, get_object_or_404
3 from django.contrib.auth.decorators import login_required, user_passes_test
4 from django.http import HttpResponse, HttpResponseRedirect
5 from fsij.accounting.models import Transaction, Account, Business, LedgerEntry, FinancialYear, GeneralLedgerEntry
6 from fsij.membership.models import Member, MemberPayment
7 from fsij.settings import LOGIN_URL
8 import datetime, time
9
10 @user_passes_test(lambda u: u.has_perm('accounting.can_manage'), LOGIN_URL)
11 def transaction(request,id):
12     t = Transaction.objects.get(pk=id)
13     return render_to_response('accounting/transaction.html',
14                               {'t': t,
15                                })
16
17 @user_passes_test(lambda u: u.has_perm('accounting.can_manage'), LOGIN_URL)
18 def transaction_new(request):
19     date = datetime.date(*time.strptime(request.POST['date'], '%Y-%m-%d')[:3])
20     business = Business.objects.get(pk=request.POST['business'])
21     memo = request.POST['memo']
22     memo_detail = request.POST['memo_detail']
23     t = Transaction(date=date, business=business, memo=memo, memo_detail=memo_detail)
24     t.save()
25     d_account = Account.objects.get(pk=request.POST['debit'])
26     d_amount  = request.POST['debit_amount']
27     l = LedgerEntry(t=t, is_credit=False, account=d_account, amount=d_amount)
28     l.save()
29     c_account = Account.objects.get(pk=request.POST['credit'])
30     c_amount = request.POST['credit_amount']
31     l = LedgerEntry(t=t, is_credit=True, account=c_account, amount=c_amount)
32     l.save()
33     if request.POST['debit2']:
34         d_account = Account.objects.get(pk=request.POST['debit2'])
35         d_amount  = request.POST['debit_amount2']
36         l = LedgerEntry(t=t, is_credit=False, account=d_account, amount=d_amount)
37         l.save()
38     if request.POST['credit2']:
39         c_account = Account.objects.get(pk=request.POST['credit2'])
40         c_amount = request.POST['credit_amount2']
41         l = LedgerEntry(t=t, is_credit=True, account=c_account, amount=c_amount)
42         l.save()
43     if request.POST['debit3']:
44         d_account = Account.objects.get(pk=request.POST['debit3'])
45         d_amount  = request.POST['debit_amount3']
46         l = LedgerEntry(t=t, is_credit=False, account=d_account, amount=d_amount)
47         l.save()
48     if request.POST['credit3']:
49         c_account = Account.objects.get(pk=request.POST['credit3'])
50         c_amount = request.POST['credit_amount3']
51         l = LedgerEntry(t=t, is_credit=True, account=c_account, amount=c_amount)
52         l.save()
53     if request.POST['debit4']:
54         d_account = Account.objects.get(pk=request.POST['debit4'])
55         d_amount  = request.POST['debit_amount4']
56         l = LedgerEntry(t=t, is_credit=False, account=d_account, amount=d_amount)
57         l.save()
58     if request.POST['credit4']:
59         c_account = Account.objects.get(pk=request.POST['credit4'])
60         c_amount = request.POST['credit_amount4']
61         l = LedgerEntry(t=t, is_credit=True, account=c_account, amount=c_amount)
62         l.save()
63     return HttpResponseRedirect(request.POST['original_path'])
64
65 @user_passes_test(lambda u: u.has_perm('accounting.can_manage'), LOGIN_URL)
66 def transaction_delete(request,id):
67     t = Transaction.objects.get(pk=id)
68     t.delete()
69     return HttpResponseRedirect(request.POST['original_path'])
70
71
72 @user_passes_test(lambda u: u.has_perm('accounting.can_manage'), LOGIN_URL)
73 def transaction_list(request,year=None):
74     if year:
75         fy = FinancialYear(int(year))
76         tr_list = Transaction.objects.filter(date__range=(fy.begin(),fy.end())).order_by('date')
77     else:
78         tr_list = Transaction.objects.all().order_by('-date')
79     return render_to_response('accounting/transaction_list.html',
80                               {'year' : year,
81                                'tr_list': tr_list,
82                                'b_list': Business.objects.all(),
83                                'a_list': Account.objects.all(),
84                                'members': Member.objects.all(),
85                                'original_path': request.path,
86                                })
87
88 @user_passes_test(lambda u: u.has_perm('accounting.can_manage'), LOGIN_URL)
89 def account_list(request,year):
90     gles = GeneralLedgerEntry(int(year))
91     return render_to_response('accounting/account_list.html',
92                               {'year' : year,
93                                'ac_list': gles.get_gles(),
94                                })
95
96 @user_passes_test(lambda u: u.has_perm('accounting.can_view_sheets'), LOGIN_URL)
97 def bs_pl_cf(request,year):
98     gles = GeneralLedgerEntry(int(year))
99     cash_accounts = Account.objects.filter(is_cash=True)
100     cash_initial_list = map(lambda x: gles.get_initial_value(x), cash_accounts)
101     return render_to_response('accounting/bs_pl_cf.html',
102                               {'year' : year,
103                                'ac_list': gles.get_gles(),
104                                'fy': gles.fy,
105                                'pl_total': gles.get_pl_total(),
106                                'cash_total': gles.get_cash_total(),
107                                'cash_initial_list': cash_initial_list,
108                                })