def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['start'] = first_day(datetime.today()) context['end'] = last_day(datetime.today() + relativedelta(months=6)) context['cash_flow_start'] = first_day(context['start'] - relativedelta(months=2)) context['cash_flow_end'] = last_day(context['start'] + relativedelta(months=2)) # incomes vs expenses context['incomes_expenses'] = Transaction.objects.cash_flow( self.request.user, context['start'], context['end']) context['cash_flow'] = Transaction.objects.cash_flow_by_category( self.request.user, context['cash_flow_start'], context['cash_flow_end']) # assets balance assets_range = month_range(context['start'], context['end']) context['assets_balance'] = [ (date, Account.objects.accounts_balance(self.request.user, last_day(date), payed=None)) for date in assets_range ] return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) category = get_object_or_404(self.request.user.category_set, pk=self.kwargs['pk']) one_month_ago = first_day(datetime.today()) - relativedelta(months=1) two_months_ago = first_day(datetime.today() - relativedelta(months=2)) context.update({ 'category': category, 'one_month_ago': one_month_ago, 'two_months_ago': two_months_ago, 'one_month_ago_credit': category.get_credit_balance(start=one_month_ago, end=last_day(one_month_ago)), 'one_month_ago_debit': category.get_debit_balance(start=one_month_ago, end=last_day(one_month_ago)), 'two_months_ago_credit': category.get_credit_balance(start=two_months_ago, end=last_day(two_months_ago)), 'two_months_ago_debit': category.get_debit_balance(start=two_months_ago, end=last_day(two_months_ago)) }) return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) category = get_object_or_404(self.request.user.category_set, pk=self.kwargs['pk']) one_month_ago = first_day(datetime.today()) - relativedelta(months=1) two_months_ago = first_day(datetime.today() - relativedelta(months=2)) context.update({ 'category': category, 'one_month_ago': one_month_ago, 'two_months_ago': two_months_ago, 'one_month_ago_credit': category.get_credit_balance( start=one_month_ago, end=last_day(one_month_ago)), 'one_month_ago_debit': category.get_debit_balance( start=one_month_ago, end=last_day(one_month_ago)), 'two_months_ago_credit': category.get_credit_balance( start=two_months_ago, end=last_day(two_months_ago)), 'two_months_ago_debit': category.get_debit_balance( start=two_months_ago, end=last_day(two_months_ago))}) return context
def assets_balance(request): start = first_day(date_param(request, 'start', datetime.today() - relativedelta(months=12))) end = last_day(date_param(request, 'end', datetime.today())) data = [ (d, Account.objects.accounts_balance(request.user, last_day(d))) for d in month_range(start, end)] return render(request, 'finance/home/_assets_balance.html', {'start': start, 'end': end, 'data': data})
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) form = self.get_filter_form() context['filter_form'] = form context['accounts'] = self.request.user.account_set.order_by('name') context['categories'] = self.request.user.category_set.order_by('name') if form.is_valid(): start = form.cleaned_data['start'] context['start'] = start end = form.cleaned_data['end'] context['end'] = end context['transactions'] = Transaction.objects.get_transactions( self.request.user, start, end, form.cleaned_data['account'], form.cleaned_data['category'], form.cleaned_data['is_payed'], form.cleaned_data['type'], form.cleaned_data['description']) context['credit_balance'] = Transaction.objects.get_credit_balance( self.request.user, start, end, form.cleaned_data['account'], form.cleaned_data['category'], form.cleaned_data['is_payed'], form.cleaned_data['type'], form.cleaned_data['description']) context['debit_balance'] = Transaction.objects.get_debit_balance( self.request.user, start, end, form.cleaned_data['account'], form.cleaned_data['category'], form.cleaned_data['is_payed'], form.cleaned_data['type'], form.cleaned_data['description']) context['total_balance'] = Transaction.objects.get_balance( self.request.user, end=end, accounts=form.cleaned_data['account'], categories=form.cleaned_data['category'], payed=form.cleaned_data['is_payed'], type=form.cleaned_data['type'], description=form.cleaned_data['description']) # next and previous months links context['current_month'] = start previous_month = first_day(start - relativedelta(months=1)) context['previous_month'] = previous_month, last_day( previous_month) next_month = first_day(start + relativedelta(months=1)) context['next_month'] = next_month, last_day(next_month) return context
def cash_flow(self, user, start, end, payed=None): """ Calculate expenses vs incomes for past period """ result = [] for date in month_range(first_day(start), last_day(end)): first = first_day(date) last = last_day(date) incomes = self.get_credit_balance(user, first, last, payed=payed) expenses = self.get_debit_balance(user, first, last, payed=payed) result.append({ 'date': date, 'incomes': incomes, 'expenses': expenses, 'balance': incomes - expenses, }) return result
def accounts_panel(request): start = date_param(request, 'start', default=None) end = date_param(request, 'end', default=last_day(datetime.today())) payed = boolean_param(request, 'payed', None) data = _accounts_with_balance(request, start, end, payed=payed) return render(request, 'finance/home/_accounts_panel.html', {'start': start, 'end': end, 'payed': payed, 'data': data})
def get_filter_form(self): data = self.request.GET.copy() if 'start' not in data: data['start'] = first_day(datetime.today()) if 'end' not in data: data['end'] = last_day(datetime.today()) return AccountDetailsFilterForm(self.request.user, data=data)
def cashflow_panel(request): start = date_param(request, 'start', first_day(datetime.today())) end = date_param(request, 'end', last_day(datetime.today())) incomes = Transaction.objects.get_credit_balance(request.user, start, end, payed=True) expenses = Transaction.objects.get_debit_balance(request.user, start, end, payed=True) # calculate percentage of incomes vs expenses percent = 0 if incomes > 0 or expenses > 0: bigger = max(incomes, expenses) smaller = min(incomes, expenses) percent = smaller / bigger * 100 if incomes == 0 and expenses == 0: incomes_percent = 0 expenses_percent = 0 elif incomes >= expenses: incomes_percent = 100 expenses_percent = percent else: incomes_percent = percent expenses_percent = 100 return render(request, 'finance/home/_cashflow_panel.html', {'start': start, 'end': end, 'incomes': incomes, 'incomes_percent': incomes_percent, 'expenses': expenses, 'expenses_percent': expenses_percent})
def budgets_panel(request): context = {'start': first_day(date_param(request, 'start', datetime.today()))} context['budgets'] = Budget.objects.get_queryset(request.user).filter(date=context['start']) # average budgets completion if context['budgets']: percent_complete_sum = sum(budget.percent_complete for budget in context['budgets']) percent_complete_avg = percent_complete_sum / len(context['budgets']) context['budget_complete_avg'] = percent_complete_avg if first_day(datetime.today()) > context['start']: # past month, use the last day expected_day = last_day(context['start']).timetuple().tm_mday elif first_day(datetime.today()) < context['start']: # future month, use first day expected_day = 1 else: # current month, use current day expected_day = datetime.today().timetuple().tm_mday num_days_in_month = monthrange(context['start'].timetuple().tm_year, context['start'].timetuple().tm_mon)[1] context['budget_complete_avg_expected'] = (100 / num_days_in_month) * expected_day return render(request, 'finance/home/_budgets_panel.html', context)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['start'] = first_day(datetime.today()) context['end'] = last_day(datetime.today() + relativedelta(months=6)) context['cash_flow_start'] = first_day(context['start'] - relativedelta(months=2)) context['cash_flow_end'] = last_day(context['start'] + relativedelta(months=2)) # incomes vs expenses context['incomes_expenses'] = Transaction.objects.cash_flow( self.request.user, context['start'], context['end']) context['cash_flow'] = Transaction.objects.cash_flow_by_category( self.request.user, context['cash_flow_start'], context['cash_flow_end']) # assets balance assets_range = month_range(context['start'], context['end']) context['assets_balance'] = [ (date, Account.objects.accounts_balance(self.request.user, last_day(date), payed=None)) for date in assets_range] return context
def top_expenses_per_category(request): start = date_param(request, 'start', default=first_day(datetime.today())) end = date_param(request, 'end', default=last_day(datetime.today())) expenses = request.user.transaction_set.exclude( category__name='Transferência' ).filter( type=Transaction.TYPE_DEBIT, date__gte=start, date__lte=end ).values( 'category__name' ).annotate( Sum('amount') ) return render(request, 'finance/home/_top_expenses_per_category.html', {'start': start, 'end': end, 'data': sorted(expenses, key=lambda x: x['amount__sum'], reverse=True)[:10]})
def clean_end(self): return self.cleaned_data['end'] or last_day(datetime.today())
def last_day(date): return utils.last_day(date)
def date_end(self): return last_day(self.date)
def incomes_expenses_per_month(request): start = date_param(request, 'start', first_day(datetime.today() - relativedelta(months=12))) end = date_param(request, 'end', last_day(datetime.today())) cash_flow = Transaction.objects.cash_flow(request.user, start, end) return render(request, 'finance/home/_incomes_expenses_per_month.html', {'start': start, 'end': end, 'data': cash_flow})
def incomes_expenses_per_day(request): start = date_param(request, 'start', default=first_day(datetime.today())) end = date_param(request, 'end', default=last_day(datetime.today())) cash_flow = Transaction.objects.cash_flow_by_day(request.user, start, end) return render(request, 'finance/home/_incomes_expenses_per_day.html', {'start': start, 'end': end, 'data': cash_flow})
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) form = self.get_filter_form() context['filter_form'] = form context['accounts'] = self.request.user.account_set.order_by('name') context['categories'] = self.request.user.category_set.order_by('name') if form.is_valid(): start = form.cleaned_data['start'] context['start'] = start end = form.cleaned_data['end'] context['end'] = end context['transactions'] = Transaction.objects.get_transactions( self.request.user, start, end, form.cleaned_data['account'], form.cleaned_data['category'], form.cleaned_data['is_payed'], form.cleaned_data['type'], form.cleaned_data['description']) context['credit_balance'] = Transaction.objects.get_credit_balance( self.request.user, start, end, form.cleaned_data['account'], form.cleaned_data['category'], form.cleaned_data['is_payed'], form.cleaned_data['type'], form.cleaned_data['description']) context['debit_balance'] = Transaction.objects.get_debit_balance( self.request.user, start, end, form.cleaned_data['account'], form.cleaned_data['category'], form.cleaned_data['is_payed'], form.cleaned_data['type'], form.cleaned_data['description']) context['total_balance'] = Transaction.objects.get_balance( self.request.user, end=end, accounts=form.cleaned_data['account'], categories=form.cleaned_data['category'], payed=form.cleaned_data['is_payed'], type=form.cleaned_data['type'], description=form.cleaned_data['description']) # next and previous months links context['current_month'] = start previous_month = first_day(start - relativedelta(months=1)) context['previous_month'] = previous_month, last_day(previous_month) next_month = first_day(start + relativedelta(months=1)) context['next_month'] = next_month, last_day(next_month) return context