Ejemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        objects_list = Expense.objects.all(user=request.user)
        first_date = None

        if objects_list:
            first_date = objects_list.last().timestamp or None

        order_field = request.GET.get("field")
        if order_field:
            ordering = request.GET.get("order", "") + order_field
            objects_list = objects_list.order_by(ordering)

        objects = helpers.get_paginator_object(request, objects_list, 30)
        total = Expense.objects.amount_sum(user=request.user)

        context = {
            "title":
            "Expenses",
            "objects":
            objects,
            "total":
            total,
            "first_date":
            first_date,
            "expense_to_income_ratio":
            helpers.expense_to_income_ratio(request.user),
        }

        return render(request, self.template_name, context)
Ejemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        context = {}
        date_str = ""
        expense = request.user.expenses

        year = int(request.GET.get('year', 0))
        month = int(request.GET.get('month', 0))
        if year or month:
            if year:
                expense = expense.filter(timestamp__year=year)
                date_str = f": {year}"
            if month:
                expense = expense.filter(timestamp__month=month)
                dt = date(year, month, 1)
                date_str = f": {dt.strftime('%B %Y')}"

            context['total'] = expense.aggregate(
                Sum('amount'))['amount__sum'] or 0

        days = expense.dates('timestamp', 'day', order='DESC')
        days = helpers.get_paginator_object(request, days, 50)

        data = []
        for day in days:
            day_sum = aggregate_sum(expense.filter(timestamp=day))
            data.append({
                'day': day,
                'day_sum': day_sum,
            })

        context['title'] = f'Day-Wise Expense{date_str}'
        context['data'] = data
        context['objects'] = days
        return render(request, self.template_name, context)
Ejemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        context = {}
        date_str = ""
        user = request.user
        year = request.GET.get('year')
        now = helpers.get_ist_datetime()
        expenses = Expense.objects.all(user=user)

        if year:
            year = int(year)
            total_months = now.month if now.year == year else 12
            expenses = expenses.filter(timestamp__year=year)
            context['total'] = expenses.aggregate(
                Sum('amount'))['amount__sum'] or 0
            context['monthly_average'] = context['total'] // total_months
            date_str = f": {year}"
            context['remark_url'] = reverse('expense:goto_year_expense',
                                            kwargs={'year': int(year)})
            context['daywise_url'] = reverse(
                'expense:day-wise-expense') + f'?year={year}'
            alt_first_date = date(year, 1, 1)
            if year == now.year:
                latest_date = date(year, now.month, 1)
            else:
                latest_date = date(year, 12, 1)
        else:
            alt_first_date = now.date().replace(month=1, day=1)
            latest_date = now.date().replace(day=1)

        # doing this way to maintain continuity of months
        first_date = expenses.dates('timestamp', 'month',
                                    order='ASC').first() or alt_first_date
        dates = helpers.get_dates_list(first_date, latest_date, day=1)
        dates = helpers.get_paginator_object(request, dates, 12)

        data = []
        for dt in dates:
            month_expense = Expense.objects.this_month(user=user,
                                                       year=dt.year,
                                                       month=dt.month)
            amount = aggregate_sum(month_expense)
            month_income = user.incomes.filter(timestamp__year=dt.year,
                                               timestamp__month=dt.month)
            month_income_sum = aggregate_sum(month_income)
            month_expense_to_income_ratio = helpers.calculate_ratio(
                amount, month_income_sum)
            data.append({
                'date': dt,
                'amount': amount,
                'month_eir': month_expense_to_income_ratio,
            })

        context['title'] = f'Monthly Expense{date_str}'
        context['data'] = data
        context['objects'] = dates
        return render(request, self.template_name, context)
Ejemplo n.º 4
0
    def get(self, request, *args, **kwargs):
        context = dict()
        date_str = ""
        from_date_str = to_date_str = None
        form = self.form_class(request.GET or None)

        if form.is_valid():
            remark = form.cleaned_data.get('remark', '').strip()
            from_date = form.cleaned_data.get('from_date')
            to_date = form.cleaned_data.get('to_date')
            objects = Expense.objects.all(user=request.user)

            if from_date and to_date:
                objects = objects.filter(timestamp__range=(from_date, to_date))
                from_date_str = default_date_format(from_date)
                to_date_str = default_date_format(to_date)
                date_str = f': {from_date_str} to {to_date_str}'
            elif from_date or to_date:
                the_date = from_date or to_date
                objects = objects.filter(timestamp=the_date)
                from_date_str = to_date_str = default_date_format(the_date)
                date_str = f': {from_date_str}'

            if remark == '""':
                objects = objects.filter(remark__isnull=True)
            elif remark:
                objects = helpers.search_expense_remark(objects, remark)

            total = aggregate_sum(objects)
            try:
                days = (to_date - from_date).days
                months = days / AVG_MONTH_DAYS
                if months > 1:
                    context['monthly_average'] = int(total / months)
            except:
                pass

            context['objects'] = helpers.get_paginator_object(
                request, objects, 30)
            context['total'] = total

        context['title'] = f'Expense Search{date_str}'
        context['form'] = form
        context['from_date'] = from_date_str
        context['to_date'] = to_date_str
        return render(request, self.template_name, context)
Ejemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        networth = NetWorth.objects.filter(user=request.user)

        history_cagr = 0
        if networth.exists():
            final = networth.first()
            start = networth.last()
            years = (final.date - start.date).days / 365
            history_cagr = calculate_cagr(final.amount, start.amount, years)

        objects = get_paginator_object(request, networth, 25)
        context = {
            'title': 'NetWorth',
            'objects': objects,
            'is_paginated': True,
            'history_cagr': history_cagr,
        }
        return render(request, self.template_name, context)
Ejemplo n.º 6
0
    def get(self, request, *args, **kwargs):
        user = request.user
        context = self.context.copy()
        now = helpers.get_ist_datetime()

        incomes = Income.objects.filter(user=user)
        year = request.GET.get('year')
        if year:
            year = int(year)
            total_months = now.month if year == now.year else 12
            incomes = incomes.filter(timestamp__year=year)
            context['title'] = f"{context['title']}: {year}"
            context['total'] = aggregate_sum(incomes)
            context['monthly_average'] = context['total'] // total_months
            alt_first_date = date(year, 1, 1)
            if year == now.year:
                latest_date = date(year, now.month, 1)
            else:
                latest_date = date(year, 12, 1)
        else:
            alt_first_date = now.date().replace(month=1, day=1)
            latest_date = now.date().replace(day=1)

        # doing this way to maintain continuity of months
        first_date = incomes.dates('timestamp', 'month',
                                   order='ASC').first() or alt_first_date
        dates = helpers.get_dates_list(first_date, latest_date, day=1)
        dates = helpers.get_paginator_object(request, dates, 12)

        data = []
        for dt in dates:
            amount = incomes.filter(
                timestamp__year=dt.year,
                timestamp__month=dt.month,
            ).aggregate(Sum('amount'))['amount__sum'] or 0
            data.append({
                'date': dt,
                'amount': amount,
            })

        context['data'] = data
        context['objects'] = dates
        return render(request, self.template_name, context)
Ejemplo n.º 7
0
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk')
        instance = AccountName.objects.get(id=pk)
        history = instance.amounts.all().order_by('-date')

        history_cagr = 0
        if history.exists():
            final = history.first()
            start = history.last()
            years = (final.date - start.date).days / 365
            history_cagr = calculate_cagr(final.amount, start.amount, years)

        objects = get_paginator_object(request, history, 25)
        context = {
            'title': f'{instance.name} ({instance.get_type_display()})',
            'objects': objects,
            'is_paginated': True,
            'history_cagr': history_cagr,
        }
        return render(request, self.template_name, context)
Ejemplo n.º 8
0
    def get(self, request, *args, **kwargs):
        user = request.user
        incomes = user.incomes
        expenses = user.expenses

        now = helpers.get_ist_datetime()
        latest_date = now.date().replace(month=1, day=1)
        first_income_date = incomes.dates('timestamp', 'year',
                                          order='ASC').first() or latest_date
        first_expense_date = expenses.dates('timestamp', 'year',
                                            order='ASC').first() or latest_date
        first_date = min(first_income_date, first_expense_date)
        dates = helpers.get_dates_list(first_date, latest_date, month=1, day=1)

        dates = helpers.get_paginator_object(request, dates, 5)

        data = []
        for date in dates:
            income_sum = aggregate_sum(
                incomes.filter(timestamp__year=date.year))
            expense_sum = aggregate_sum(
                expenses.filter(timestamp__year=date.year))
            expense_ratio = helpers.calculate_ratio(expense_sum, income_sum)

            data.append({
                'date': date,
                'income_sum': income_sum,
                'expense_sum': expense_sum,
                'saved': income_sum - expense_sum,
                'expense_ratio': expense_ratio,
            })

        context = {
            'title': 'Report Card',
            'now': helpers.get_ist_datetime(),
            'eir': helpers.expense_to_income_ratio(user),
            'data': data,
            'BANK_AMOUNT_PCT': BANK_AMOUNT_PCT * 100,
            'objects': dates,
        }
        return render(request, self.template_name, context)
Ejemplo n.º 9
0
    def get(self, request, *args, **kwargs):
        user = request.user
        now = get_ist_datetime()
        incomes = Income.objects.filter(user=user)

        latest_date = now.date().replace(month=1, day=1)
        first_date = incomes.dates('timestamp', 'year',
                                   order='ASC').first() or latest_date
        dates = helpers.get_dates_list(first_date, latest_date, month=1, day=1)
        dates = helpers.get_paginator_object(request, dates, 5)

        data = []
        for dt in dates:
            total_months = now.month if dt.year == now.year else 12
            amount = aggregate_sum(incomes.filter(timestamp__year=dt.year))
            avg_income = amount // total_months
            data.append((dt, amount, avg_income))

        self.context['data'] = data
        self.context['objects'] = dates
        return render(request, self.template_name, self.context)
Ejemplo n.º 10
0
    def get(self, request, *args, **kwargs):
        user = request.user
        now = helpers.get_ist_datetime()
        expense_sum = user.expenses.aggregate(
            Sum('amount'))['amount__sum'] or 0
        income_sum = user.incomes.aggregate(Sum('amount'))['amount__sum'] or 0

        latest_date = now.date().replace(month=1, day=1)
        first_date = user.expenses.dates('timestamp', 'year',
                                         order='ASC').first() or latest_date
        dates = helpers.get_dates_list(first_date, latest_date, month=1, day=1)
        dates = helpers.get_paginator_object(request, dates, 5)

        data = []
        for date in dates:
            year = date.year
            total_months = now.month if now.year == year else 12
            amount = aggregate_sum(
                Expense.objects.this_year(user=user, year=year))
            year_income_sum = aggregate_sum(
                user.incomes.filter(timestamp__year=year))

            expense_ratio = helpers.calculate_ratio(amount, expense_sum)
            expense_to_income_ratio = helpers.calculate_ratio(
                amount, income_sum)
            year_expense_to_income_ratio = helpers.calculate_ratio(
                amount, year_income_sum)

            data.append({
                'year': year,
                'amount': amount,
                'monthly_average': amount // total_months,
                'year_eir': year_expense_to_income_ratio,
                'eir': expense_to_income_ratio,
                'expense_ratio': expense_ratio,
            })

        self.context['data'] = data
        self.context['objects'] = dates
        return render(request, self.template_name, self.context)
Ejemplo n.º 11
0
    def get(self, request, *args, **kwargs):
        day = int(kwargs.get('day', 0))
        month = int(kwargs.get('month', 0))
        year = int(kwargs.get('year', 0))
        date_str = ""
        from_date = to_date = None
        daywise_url_reverse = reverse('expense:day-wise-expense')

        if day:
            objects = Expense.objects.this_day(user=request.user,
                                               year=year,
                                               month=month,
                                               day=day)
            dt = date(year, month, day)
            date_str = f': {dt.strftime("%d %b %Y")}'
            remark_url_name = "goto_day_expense"
            daywise_url = ""
            from_date = to_date = default_date_format(dt)
        elif month:
            objects = Expense.objects.this_month(user=request.user,
                                                 year=year,
                                                 month=month)
            dt = date(year, month, 1)
            date_str = f': {dt.strftime("%b %Y")}'
            remark_url_name = "remark_monthly_expense"
            daywise_url = f'{daywise_url_reverse}?year={year}&month={month}'
            from_date = default_date_format(date(year, month, 1))
            to_date = default_date_format(
                date(year, month,
                     calendar.monthrange(year, month)[1]))
        elif year:
            objects = Expense.objects.this_year(user=request.user, year=year)
            date_str = f': {year}'
            remark_url_name = "goto_year_expense"
            daywise_url = f'{daywise_url_reverse}?year={year}'
            from_date = default_date_format(date(year, 1, 1))
            to_date = default_date_format(date(year, 12, 31))

        total = objects.aggregate(Sum('amount'))['amount__sum'] or 0
        objects = helpers.get_paginator_object(request, objects, 50)

        context = {
            "title":
            f"Expenses{date_str}",
            "objects":
            objects,
            "total":
            total,
            "remark_url":
            reverse(f'expense:{remark_url_name}',
                    kwargs={k: int(v)
                            for k, v in kwargs.items()}),
            "daywise_url":
            daywise_url,
            "from_date":
            from_date,
            "to_date":
            to_date,
        }

        return render(request, self.template_name, context)