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