# Path: banking/transactions/models.py
# class Transaction(models.Model):
#     date = models.DateField()
#     account = models.ForeignKey(Account)
#     amount = models.DecimalField(max_digits=65, decimal_places=2)
#     balance = models.DecimalField(max_digits=65, decimal_places=2)
#     beneficiary = models.CharField(max_length=50)
#     budget = models.ForeignKey(MonthlyBudget, blank=True, null=True)
#     category = models.ForeignKey('Category', blank=True, null=True)
#     debit = models.BooleanField(default=True)
#     transfer = models.BooleanField(default=False)
#     slug = models.SlugField(unique=True)
# 
#     def __str__(self):
#         return self.slug
# 
#     def save(self, *args, **kwargs):
#         time = datetime.datetime.now().time().strftime("%H%M%S") #Add the hour, min, and second to the slug to avoid Unique constraint conflicts
#         forslug = "{0.date}-{1}-{0.account}-{0.amount}".format(self, time)
#         self.slug = slugify(forslug)
#         super(Transaction, self).save()
# 
# Path: banking/transactions/forms.py
# class AddTransactionForm(forms.Form):
#     date = forms.DateField( widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'size': '10', 'data-start-week-day': "0"}))
#     budget = forms.CharField()
#     beneficiary = forms.CharField()
#     category = forms.CharField()
#     amount = forms.DecimalField(max_digits=65, decimal_places=2, localize=True, widget=forms.TextInput(attrs={'size': '6'}))
#     direction = forms.ChoiceField(widget=forms.RadioSelect, choices=DIRECTION_CHOICES, initial="O")
# 
#     def __init__(self, *args, **kwargs): #Need to overide the init so that the modelchoice queryset will update each time the form loads
#         super(AddTransactionForm, self).__init__(*args, **kwargs)
#         self.fields['budget'] = forms.ModelChoiceField(queryset=Budget.objects.all(), required=False)
#         self.fields['category'] = forms.ModelChoiceField(queryset=Category.objects.all(), required=False)
# 
# class FilterForm(forms.Form):
#     thirty_days_ago = datetime.datetime.today() + datetime.timedelta(-30)
#     thirty_days_ago = thirty_days_ago.strftime('%Y-%m-%d')
#     start_date = forms.DateField(widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'data-start-date': thirty_days_ago, 'data-start-week-day': "0"}))
#     end_date = 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, FormView
from django.db.models import Sum
from .models import Transaction
from accounts.models import Account
from .forms import AddTransactionForm, FilterForm
from budget.models import MonthlyBudget
import datetime



class AccountTransactions(View): #View transactions from a single account

    def get_context_data(self, *args, **kwargs):
        transaction_form = AddTransactionForm(prefix='transaction_form')
        filter_form = FilterForm(prefix='filter_form')
        accountobj = Account.objects.get(slug=kwargs['account'])
        today = datetime.datetime.today().strftime('%Y-%m-%d')

        thirty_days_ago = datetime.datetime.today() + datetime.timedelta(-30)
        thirty_days_ago = thirty_days_ago.strftime('%Y-%m-%d')

        start_date = kwargs['start_date']
        end_date = kwargs['end_date']

        if start_date == 0 and end_date == 0:

# Path: banking/transactions/models.py
# class Transaction(models.Model):
#     date = models.DateField()
#     account = models.ForeignKey(Account)
#     amount = models.DecimalField(max_digits=65, decimal_places=2)
#     balance = models.DecimalField(max_digits=65, decimal_places=2)
#     beneficiary = models.CharField(max_length=50)
#     budget = models.ForeignKey(MonthlyBudget, blank=True, null=True)
#     category = models.ForeignKey('Category', blank=True, null=True)
#     debit = models.BooleanField(default=True)
#     transfer = models.BooleanField(default=False)
#     slug = models.SlugField(unique=True)
# 
#     def __str__(self):
#         return self.slug
# 
#     def save(self, *args, **kwargs):
#         time = datetime.datetime.now().time().strftime("%H%M%S") #Add the hour, min, and second to the slug to avoid Unique constraint conflicts
#         forslug = "{0.date}-{1}-{0.account}-{0.amount}".format(self, time)
#         self.slug = slugify(forslug)
#         super(Transaction, self).save()
# 
# Path: banking/transactions/forms.py
# class AddTransactionForm(forms.Form):
#     date = forms.DateField( widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'size': '10', 'data-start-week-day': "0"}))
#     budget = forms.CharField()
#     beneficiary = forms.CharField()
#     category = forms.CharField()
#     amount = forms.DecimalField(max_digits=65, decimal_places=2, localize=True, widget=forms.TextInput(attrs={'size': '6'}))
#     direction = forms.ChoiceField(widget=forms.RadioSelect, choices=DIRECTION_CHOICES, initial="O")
# 
#     def __init__(self, *args, **kwargs): #Need to overide the init so that the modelchoice queryset will update each time the form loads
#         super(AddTransactionForm, self).__init__(*args, **kwargs)
#         self.fields['budget'] = forms.ModelChoiceField(queryset=Budget.objects.all(), required=False)
#         self.fields['category'] = forms.ModelChoiceField(queryset=Category.objects.all(), required=False)
# 
# class FilterForm(forms.Form):
#     thirty_days_ago = datetime.datetime.today() + datetime.timedelta(-30)
#     thirty_days_ago = thirty_days_ago.strftime('%Y-%m-%d')
#     start_date = forms.DateField(widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'data-start-date': thirty_days_ago, 'data-start-week-day': "0"}))
#     end_date = forms.DateField(widget=forms.TextInput(attrs={'class': 'ink-datepicker', 'data-start-week-day': "0"}))


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




class AccountTransactions(View): #View transactions from a single account

    def get_context_data(self, *args, **kwargs):
        transaction_form = AddTransactionForm(prefix='transaction_form')
        filter_form = FilterForm(prefix='filter_form')
        accountobj = Account.objects.get(slug=kwargs['account'])
        today = datetime.datetime.today().strftime('%Y-%m-%d')

        thirty_days_ago = datetime.datetime.today() + datetime.timedelta(-30)
        thirty_days_ago = thirty_days_ago.strftime('%Y-%m-%d')

        start_date = kwargs['start_date']
        end_date = kwargs['end_date']

        if start_date == 0 and end_date == 0:

            transactions = Transaction.objects.filter(account=accountobj, date__range=[thirty_days_ago, today]).order_by('date', 'id')