# This file is part of k4ever, a point-of-sale system
#   Contact............  <k4ever@lists.someserver.de>
#   Website............  http://k4ever.someserver.de/
#   Bug tracker........  http://k4ever.someserver.de/report
#
# Licensed under GNU Affero General Public License v3 or later

from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext

from transaction.forms import TransactionForm, VirtualTransactionForm
from transaction.models import Transaction, VirtualTransaction


@login_required
def overview(request):
	""" Creates an overview over the users transactions, also handles adding and transfering money. """
	# create history
	history = Transaction.objects.filter(user=request.user).order_by("-dateTime")
	vhistory = VirtualTransaction.objects.filter(Q(user=request.user) | Q(recipient=request.user)).order_by("-dateTime")

	# create forms
	form = TransactionForm()
	vform = VirtualTransactionForm()
	state = None
	payway = "payin"
	if request.method == 'POST' and '_formtype' in request.POST:
		if request.POST['_formtype'] == "normal":
			transaction = Transaction(user=request.user)
			form = TransactionForm(request.POST, instance=transaction)
			if form.is_valid():
				if form.cleaned_data['amount'] < 0:
					payway = "payout"
				form.save()
				form = TransactionForm()
				state = "success"
			else:
				state = "error"
				form = TransactionForm()
		elif request.POST['_formtype'] == "virtual":
			vtransaction = VirtualTransaction(user=request.user)
			vform = VirtualTransactionForm(request.POST, instance=vtransaction)
			if vform.is_valid():
				vform.save()
				vform = VirtualTransactionForm()
				state = "vsuccess"
			else:
				state = "verror"
		return HttpResponseRedirect("state/%s/%s/" % (state,payway))
	return render_to_response("transaction/overview.html", {'history': history, 'vhistory': vhistory, 'form': form, 'transacted': False, 'payout': None, 'error': False, 'vform': vform, 'vtransacted': False, 'verror': False}, RequestContext(request))


@login_required
def state(request, state="", payway=""):
	""" View which shows the state/return-msg of a transaction."""
	# create history
	history = Transaction.objects.filter(user=request.user).order_by("-dateTime")
	vhistory = VirtualTransaction.objects.filter(Q(user=request.user) | Q(recipient=request.user)).order_by("-dateTime")
	# create forms
	form = TransactionForm()
	vform = VirtualTransactionForm()

	error = verror = False
	transacted = vtransacted = False
	payout = False
	if state == "" or state == "error":
		error = True
	elif state == "success":
		transacted = True
	elif state == "verror":
		verror = True
	elif state == "vsuccess":
		vtransacted = True

	if payway == "payout":
		payout = True
	return render_to_response("transaction/overview.html", {'history': history, 'vhistory': vhistory, 'form': form, 'transacted': transacted, 'payout': payout, 'error': error, 'vform': vform, 'vtransacted': vtransacted, 'verror': verror}, RequestContext(request))
