# Path: books/models.py
# class DebtLoan(models.Model):
#     DEBT = 0
#     LOAN = 1
#     CATEGORY_CHOICES = (
#         (DEBT, 'debt'),
#         (LOAN, 'loan'),
#     )
# 
#     with_who = fields.CharField(max_length=255)
#     title = fields.CharField(max_length=255, null=True, blank=True)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.PositiveSmallIntegerField(choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         if self.title:
#             return "{}: {}".format(self.with_who, self.title)
#         else:
#             return "{}".format(self.with_who)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()
# 
# Path: books/models.py
# class Transaction(models.Model):
#     EXPENSE = 'exp'
#     INCOME = 'inc'
#     CATEGORY_CHOICES = (
#         (EXPENSE, 'expense'),
#         (INCOME, 'income'),
#     )
# 
#     title = fields.CharField(max_length=255)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.CharField(max_length=3, choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return "{}".format(self.title)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()
# 
# Path: books/forms.py
# class TransactionForm(forms.ModelForm):
#     class Meta:
# class DebtLoanForm(forms.ModelForm):
#     class Meta:
# 
# Path: books/services.py
# def get_months_transactions(user):
# def get_last_months_transactions(user):
# def get_this_years_transactions(user):


from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.urls import reverse
from django.db.models import Sum
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.shortcuts import render
from books.models import DebtLoan
from books.models import Transaction
from books import forms
from books import services



@login_required
def transaction_list(request):
    ctx = {}

    user_id = request.user.id
    user = User.objects.get(id=user_id)

    fltr = request.session.get('filter:transaction_list', None)
    if fltr == "last_month":

# Path: books/models.py
# class DebtLoan(models.Model):
#     DEBT = 0
#     LOAN = 1
#     CATEGORY_CHOICES = (
#         (DEBT, 'debt'),
#         (LOAN, 'loan'),
#     )
# 
#     with_who = fields.CharField(max_length=255)
#     title = fields.CharField(max_length=255, null=True, blank=True)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.PositiveSmallIntegerField(choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         if self.title:
#             return "{}: {}".format(self.with_who, self.title)
#         else:
#             return "{}".format(self.with_who)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()
# 
# Path: books/models.py
# class Transaction(models.Model):
#     EXPENSE = 'exp'
#     INCOME = 'inc'
#     CATEGORY_CHOICES = (
#         (EXPENSE, 'expense'),
#         (INCOME, 'income'),
#     )
# 
#     title = fields.CharField(max_length=255)
#     amount = fields.DecimalField(max_digits=10, decimal_places=2)
#     category = fields.CharField(max_length=3, choices=CATEGORY_CHOICES)
#     created = fields.DateTimeField(default=timezone.now, editable=False)
#     modified = fields.DateTimeField(default=timezone.now)
#     active = fields.BooleanField(default=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return "{}".format(self.title)
# 
#     def deactivate(self):
#         if self.active:
#             self.active = False
#             self.save()
# 
# Path: books/forms.py
# class TransactionForm(forms.ModelForm):
#     class Meta:
# class DebtLoanForm(forms.ModelForm):
#     class Meta:
# 
# Path: books/services.py
# def get_months_transactions(user):
# def get_last_months_transactions(user):
# def get_this_years_transactions(user):


# Path: books/views.py
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.urls import reverse
from django.db.models import Sum
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.shortcuts import render
from books.models import DebtLoan
from books.models import Transaction
from books import forms
from books import services




@login_required
def transaction_list(request):
    ctx = {}

    user_id = request.user.id
    user = User.objects.get(id=user_id)

    fltr = request.session.get('filter:transaction_list', None)
    if fltr == "last_month":

        user_transactions = services.get_last_months_transactions(user)