# Path: banking/budget/models.py
# class Budget(models.Model):
#     name = models.CharField(max_length=50)
#     slug = models.SlugField(unique=True)
# 
#     def __str__(self):
#         return self.slug
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Budget, self).save()
# 
# class MonthlyBudget(models.Model):
#     budget = models.ForeignKey('Budget')
#     month = models.DateField()
#     planned = models.DecimalField(max_digits=65, decimal_places=2)
#     actual = models.DecimalField(max_digits=65, decimal_places=2, blank=True, null=True)
#     slug = models.SlugField(unique=True)
# 
#     def __str__(self):
#         return self.slug
# 
#     def save(self, *args, **kwargs):
#         forslug = "{0.budget}-{0.month}".format(self)
#         self.slug = slugify(forslug)
#         super(MonthlyBudget, self).save()
# 
# Path: banking/budget/forms.py
# class AddMonthlyBudget(forms.Form):
#     budget = forms.CharField(widget=forms.TextInput(attrs={'size': '15'}))
#     amount = forms.DecimalField(max_digits=65, decimal_places=2, widget=forms.TextInput(attrs={'size': '6'}))
#     starting_month = forms.DateField(widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'size': '10', 'data-start-week-day': "0"}))
#     duration = forms.ChoiceField(widget=forms.RadioSelect, choices=DURATION_CHOICES)
# 
# class FilterForm(forms.Form):
#     month = forms.DateField(widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'data-start-week-day': "0"}))


from django.shortcuts import render
from django.views.generic import View, CreateView, UpdateView
from django.db.models import Sum
from .models import Budget, MonthlyBudget
from transactions.models import Transaction
from .forms import AddMonthlyBudget, FilterForm
import datetime
        planned_total = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month).aggregate(Sum('planned'))
        actual_total = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month).aggregate(Sum('actual'))
        cash_in = Transaction.objects.filter(date__year=now.year, date__month=now.month, debit=False, transfer=False).aggregate(Sum('amount'))
        cash_out = Transaction.objects.filter(date__year=now.year, date__month=now.month, debit=True, transfer=False).aggregate(
            Sum('amount'))

        context = {'cash_out': cash_out, 'cash_in': cash_in, 'monthlybudgets': monthlybudgets,'budget_form': budget_form, 'header': header, 'filter_form': filter_form, 'planned_total': planned_total, 'actual_total': actual_total}
        return render(request, 'budget/monthlybudget.html', context)

    def post(self, request):
        budget_form = AddMonthlyBudget(prefix='budget_form')
        filter_form = FilterForm(prefix='filter_form')
        now = datetime.datetime.now()
        action = self.request.POST['action']
        monthlybudgets = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month)
        header = now.strftime('%B') + " " + str(now.year)
        planned_total = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month).aggregate(Sum('planned'))
        actual_total = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month).aggregate(
            Sum('actual'))
        cash_in = Transaction.objects.filter(date__year=now.year, date__month=now.month, debit=False, transfer=False).aggregate(
            Sum('amount'))
        cash_out = Transaction.objects.filter(date__year=now.year, date__month=now.month, debit=True, transfer=False).aggregate(
            Sum('amount'))

        if (action == 'add_budget'):
            budget_form = AddMonthlyBudget(request.POST, prefix = 'budget_form')
            if budget_form.is_valid():
                budget_data = {}
                for key, value in budget_form.cleaned_data.items():
                    budget_data[key] = value

# Path: banking/budget/models.py
# class Budget(models.Model):
#     name = models.CharField(max_length=50)
#     slug = models.SlugField(unique=True)
# 
#     def __str__(self):
#         return self.slug
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name)
#         super(Budget, self).save()
# 
# class MonthlyBudget(models.Model):
#     budget = models.ForeignKey('Budget')
#     month = models.DateField()
#     planned = models.DecimalField(max_digits=65, decimal_places=2)
#     actual = models.DecimalField(max_digits=65, decimal_places=2, blank=True, null=True)
#     slug = models.SlugField(unique=True)
# 
#     def __str__(self):
#         return self.slug
# 
#     def save(self, *args, **kwargs):
#         forslug = "{0.budget}-{0.month}".format(self)
#         self.slug = slugify(forslug)
#         super(MonthlyBudget, self).save()
# 
# Path: banking/budget/forms.py
# class AddMonthlyBudget(forms.Form):
#     budget = forms.CharField(widget=forms.TextInput(attrs={'size': '15'}))
#     amount = forms.DecimalField(max_digits=65, decimal_places=2, widget=forms.TextInput(attrs={'size': '6'}))
#     starting_month = forms.DateField(widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'size': '10', 'data-start-week-day': "0"}))
#     duration = forms.ChoiceField(widget=forms.RadioSelect, choices=DURATION_CHOICES)
# 
# class FilterForm(forms.Form):
#     month = forms.DateField(widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'data-start-week-day': "0"}))


# Path: banking/budget/views.py
from django.shortcuts import render
from django.views.generic import View, CreateView, UpdateView
from django.db.models import Sum
from .models import Budget, MonthlyBudget
from transactions.models import Transaction
from .forms import AddMonthlyBudget, FilterForm
import datetime

        planned_total = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month).aggregate(Sum('planned'))
        actual_total = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month).aggregate(Sum('actual'))
        cash_in = Transaction.objects.filter(date__year=now.year, date__month=now.month, debit=False, transfer=False).aggregate(Sum('amount'))
        cash_out = Transaction.objects.filter(date__year=now.year, date__month=now.month, debit=True, transfer=False).aggregate(
            Sum('amount'))

        context = {'cash_out': cash_out, 'cash_in': cash_in, 'monthlybudgets': monthlybudgets,'budget_form': budget_form, 'header': header, 'filter_form': filter_form, 'planned_total': planned_total, 'actual_total': actual_total}
        return render(request, 'budget/monthlybudget.html', context)

    def post(self, request):
        budget_form = AddMonthlyBudget(prefix='budget_form')
        filter_form = FilterForm(prefix='filter_form')
        now = datetime.datetime.now()
        action = self.request.POST['action']
        monthlybudgets = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month)
        header = now.strftime('%B') + " " + str(now.year)
        planned_total = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month).aggregate(Sum('planned'))
        actual_total = MonthlyBudget.objects.filter(month__year=now.year, month__month=now.month).aggregate(
            Sum('actual'))
        cash_in = Transaction.objects.filter(date__year=now.year, date__month=now.month, debit=False, transfer=False).aggregate(
            Sum('amount'))
        cash_out = Transaction.objects.filter(date__year=now.year, date__month=now.month, debit=True, transfer=False).aggregate(
            Sum('amount'))

        if (action == 'add_budget'):
            budget_form = AddMonthlyBudget(request.POST, prefix = 'budget_form')
            if budget_form.is_valid():
                budget_data = {}
                for key, value in budget_form.cleaned_data.items():
                    budget_data[key] = value

                newbudget = Budget.objects.filter(name=budget_data['budget']).first() #Look to see if the budget already exists