django-pim / pcbank / views.py

# -*- coding: utf-8 -*-
from django.core.exceptions import PermissionDenied
from django.views.generic.create_update import create_object, update_object, delete_object
from django.views.generic.list_detail import object_list, object_detail
from django.views.generic.simple import direct_to_template
from pcbank.models import CreditCard, Bill, Income, Bank, Payee, Transaction, Roommate, RoommateBill,\
    Cheque
from decimal import Decimal
from django.shortcuts import render_to_response, redirect
from pcbank.forms import NewBillForm
from django.template import RequestContext
import datetime
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.utils import simplejson

@login_required
def overview(req):
    if not req.user.is_superuser:
        raise PermissionDenied
    total_income = Decimal("0.00")
    for income in Income.objects.filter(active=True):
        total_income += income.monthly_amount()
    bills = Decimal("0.00")
    for bill in Bill.objects.filter(paid=False):
        bills += bill.split_amount()
    credit = Decimal("0.00")
    debt = Decimal("0.00")
    for cc in CreditCard.objects.filter(active=True):
        credit += cc.available_credit()
        debt += cc.balance
    worth = Decimal("0.00")
    for bank in Bank.objects.filter(active=True):
        worth += bank.balance
    available_funds = credit + worth - bills
    worth -= debt
    worth -= bills
    roommate_due = Decimal("0.00")
    for bill in RoommateBill.objects.filter(paid=False):
        roommate_due += bill.amount()
    return render_to_response('pcbank/index.html', {'income':total_income, 'bills':bills, 'credit':credit, 'debt':debt, 'worth':worth, 'available_funds':available_funds, 'roommate_due':roommate_due})

@login_required
def create(req, *args, **kwargs):
    if not req.user.is_superuser:
        raise PermissionDenied
    return create_object(req, *args, **kwargs)

@login_required
def update(req, *args, **kwargs):
    if not req.user.is_superuser:
        raise PermissionDenied
    return update_object(req, *args, **kwargs)

@login_required
def delete(req, *args, **kwargs):
    if not req.user.is_superuser:
        raise PermissionDenied
    return delete_object(req, *args, **kwargs)

@login_required
def show_list(req, *args, **kwargs):
    if not req.user.is_superuser:
        raise PermissionDenied
    return object_list(req, *args, **kwargs)

@login_required
def details(req, *args, **kwargs):
    if not req.user.is_superuser:
        raise PermissionDenied
    return object_detail(req, *args, **kwargs)

@login_required
def flatpage(req, *args, **kwargs):
    if not req.user.is_superuser:
        raise PermissionDenied
    return direct_to_template(req, *args, **kwargs)

@login_required
def credit_table(req):
    if not req.user.is_superuser:
        raise PermissionDenied
    cc_list = CreditCard.objects.filter(active=True)
    total_balance = Decimal('0.00')
    total_credit = Decimal('0.00')
    total_due = Decimal('0.00')
    for cc in cc_list:
        total_balance += cc.balance
        total_credit += (cc.credit - cc.balance)
        total_due += cc.min_payment
    return render_to_response('pcbank/credit_table.html', {'cc_list':cc_list, 'total_balance':total_balance, 'total_credit':total_credit, 'total_due':total_due})

@login_required
def bill_table(req):
    if not req.user.is_superuser:
        raise PermissionDenied
    bill_list = Bill.objects.filter(paid=False).order_by('payee__due_date')
    cc_list = CreditCard.objects.filter(active=True, min_payment__gt=0)
    cc_due = Decimal('0.00')
    for cc in cc_list:
        cc_due += cc.min_payment
    total_due = Decimal('0.00')
    for bill in bill_list:
        total_due += bill.split_amount()
    total_due += cc_due
    return render_to_response('pcbank/bill_table.html', {'bill_list':bill_list, 'total_due':total_due, 'cc_due':cc_due})

@login_required
def mark_paid(req, object_id):
    if not req.user.is_superuser:
        raise PermissionDenied
    bill = Bill.objects.get(pk=object_id)
    bill.paid = True
    bill.save()
    return redirect('view-bill', object_id=object_id)

@login_required
def payday(req, object_id):
    if not req.user.is_superuser:
        raise PermissionDenied
    if req.method == 'GET':
        raise PermissionDenied
    if req.POST.get('bank_id', 'None') == 'None':
        raise PermissionDenied
    income = Income.objects.get(pk=object_id)
    bank = Bank.objects.get(pk=req.POST.get('bank_id', 1))
    bank.balance += income.amount
    bank.save()
    t = Transaction(from_obj=income, amount=income.amount, content_object=bank)
    t.save()
    return redirect('view-bank', object_id=bank.pk)

@login_required
def make_bank_payment(req, object_id):
    if not req.user.is_superuser:
        raise PermissionDenied
    if req.method == 'GET':
        raise PermissionDenied
    if req.POST.get('bank_id', 'None') == 'None':
        raise PermissionDenied
    bill = Bill.objects.get(pk=object_id)
    bank = Bank.objects.get(pk=req.POST.get('bank_id', 1))
    bank.balance -= bill.amount
    bank.save()
    bill.paid = True
    bill.save()
    payee = bill.payee
    t = Transaction(from_obj=bank, amount=bill.amount, content_object=payee)
    t.save()
    due_month = payee.due_date.month + 1
    if due_month > 12:
        payee.due_date = datetime.date(payee.due_date.year + 1, 1, payee.due_date.day)
    else:
        payee.due_date = datetime.date(payee.due_date.year, due_month, payee.due_date.day)
    payee.save()
    return redirect('view-bank', object_id=bank.pk)

@login_required
def make_cc_payment(req, object_id):
    if not req.user.is_superuser:
        raise PermissionDenied
    if req.method == 'GET':
        raise PermissionDenied
    if req.POST.get('cc_id', 'None') == 'None':
        raise PermissionDenied
    bill = Bill.objects.get(pk=object_id)
    cc = CreditCard.objects.get(pk=req.POST.get('cc_id', 1))
    cc.balance += bill.amount
    cc.save()
    bill.paid = True
    bill.save()
    payee = bill.payee
    t = Transaction(from_obj=cc, amount=bill.amount, content_object=payee)
    t.save()
    due_month = payee.due_date.month + 1
    if due_month > 12:
        payee.due_date = datetime.date(payee.due_date.year + 1, 1, payee.due_date.day)
    else:
        payee.due_date = datetime.date(payee.due_date.year, due_month, payee.due_date.day)
    payee.save()
    return redirect('view-cc', object_id=cc.pk)

@login_required
def add_bill(req, object_id):
    if not req.user.is_superuser:
        raise PermissionDenied
    if req.method == 'GET':
        raise PermissionDenied
    payee = Payee.objects.get(pk=object_id)
    form = NewBillForm(req.POST)
    if form.is_valid():
        bill = form.save(commit=False)
        bill.payee = payee
        bill.save()
        return redirect(bill)
    return render_to_response('pcbank/payee_detail.html', {'object':payee, 'form':form}, context_instance=RequestContext(req))

@login_required
def add_roommatebill(req, object_id):
    if not req.user.is_superuser:
        raise PermissionDenied
    if req.method == 'GET':
        raise PermissionDenied
    if req.POST.get('bill_id', 'None') == 'None':
        raise PermissionDenied
    roommate = Roommate.objects.get(pk=object_id)
    bill = Bill.objects.get(pk=req.POST.get('bill_id', 1))
    rbill = RoommateBill(roommate=roommate, for_bill=bill)
    rbill.save()
    return redirect(rbill)

@login_required
def roommate_payment(req, object_id):
    if not req.user.is_superuser:
        raise PermissionDenied
    if req.method == 'GET':
        raise PermissionDenied
    if req.POST.get('bank_id', 'None') == 'None':
        raise PermissionDenied
    rbill = RoommateBill.objects.get(pk=object_id)
    bank = Bank.objects.get(pk=req.POST.get('bank_id', 1))
    bank.balance += rbill.amount()
    bank.save()
    rbill.paid = True
    rbill.pay_date = datetime.date.today()
    rbill.save()
    rmate = rbill.roommate
    rmate.total_given += rbill.amount()
    rmate.save()
    t = Transaction(from_obj=rbill.roommate, amount=rbill.amount(), content_object=bank)
    t.save()
    return redirect('view-bank', object_id=bank.pk)

@login_required
def bank_transfer(req, object_id):
    if not req.user.is_superuser:
        raise PermissionDenied
    if req.method == 'GET':
        raise PermissionDenied
    if req.POST.get('bank_id', 'None') == 'None':
        raise PermissionDenied
    from_bank = Bank.objects.get(pk=object_id)
    to_bank = Bank.objects.get(pk=req.POST.get('bank_id', 1))
    amount = Decimal(req.POST.get('amount', 0))
    from_bank.balance -= amount
    to_bank.balance += amount
    from_bank.save()
    to_bank.save()
    t = Transaction(from_obj=from_bank, amount=amount, content_object=to_bank)
    t.save()
    return redirect(to_bank)

@login_required
def monthly_budget(req):
    if not req.user.is_superuser:
        raise PermissionDenied
    paydays = 0
    month = req.GET.get('month', None)
    today = datetime.date.today()
    if month:
        month = int(month)
        if month < today.month:
            today = datetime.date(today.year + 1, month, 1)
        else:
            today = datetime.date(today.year, month, 1)
    for day in range(today.day, 30):
        if datetime.date(today.year, today.month, day).weekday() == 4:
            paydays += 1
    weekly_income = Decimal("0.00")
    for income in Income.objects.filter(active=True):
        weekly_income += income.amount
    total_income = weekly_income * paydays
    banks = Decimal("0.00")
    for bank in Bank.objects.filter(active=True):
        banks += bank.balance
    total = banks + total_income
    if today.month == 12:
        next_month = datetime.date(today.year + 1, 1, 1)
    else:
        next_month = datetime.date(today.year, today.month + 1, 1)
    cc = Decimal("0.00")
    for card in CreditCard.objects.filter(active=True, due_date__lte=next_month):
        cc += card.min_payment
    bill_list = Bill.objects.filter(paid=False, payee__due_date__lte=next_month)
    bills = Decimal("0.00")
    bills += cc
    for bill in bill_list:
        bills += bill.split_amount()
    left = total - bills
    if paydays == 0:
        per_week = left
    else:
        per_week = left / paydays
    return render_to_response('pcbank/monthly_budget.html', locals())

def json_service(req):
    lst = []
    for bank in Bank.objects.filter(active=True):
        lst.append({'name':bank.title, 'balance':"$%.02f" % bank.balance})
    for cc in CreditCard.objects.filter(active=True):
        lst.append({'name':cc.title, 'balance':"$%.02f" % cc.available_credit()})
    return HttpResponse(simplejson.dumps(lst), content_type='text/plain')

@login_required
def assign_transaction(req, cheque_id):
    cheque = Cheque.objects.get(number=cheque_id)
    transaction = Transaction.objects.get(pk=req.POST.get('transaction_id', 0))
    cheque.transaction = transaction
    cheque.save()
    return redirect(cheque)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.