def test_next_month(self): context = self.client.get( reverse('account_view', args=[self.account.id])).context next_month = last_day_of_month( datetime.date.today()) + datetime.timedelta(days=1) self.assertEquals(context['next_month'].month, next_month.month) self.assertEquals(context['next_month'].year, next_month.year)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'recurrences' income = 0 expenses = 0 today = date.today() last = last_day_of_month(today) remaining = 0 for t in context['transactions']: if t.interval == RecurringTransaction.MONTHLY or ( t.interval == RecurringTransaction.ANNUALLY and t.date.month == today.month and t.date.year == today.year): if t.transaction_type == Transaction.WITHDRAW: expenses += t.amount if t.date <= last: remaining -= t.amount elif t.transaction_type == Transaction.DEPOSIT: income += t.amount if t.date <= last: remaining += t.amount context['expenses'] = expenses context['income'] = income context['total'] = income - expenses context['remaining'] = remaining return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'categories' dstart = self.month dend = last_day_of_month(dstart) categories = Split.objects.personal().past().date_range( dstart, dend).order_by('category').values( 'category', 'category__name').annotate(spent=Sum('amount')) categories = [(e['category'], e['category__name'], abs(e['spent'])) for e in categories] all_categories = list( Category.objects.exclude(active=False).values_list('id', 'name')) for id, name, spent in categories: if id: all_categories.remove((id, name)) for id, name in all_categories: categories.append((id, name, 0)) context['categories'] = categories context['month'] = self.month context['next_month'] = self.month + relativedelta(months=1) context['previous_month'] = self.month - relativedelta(months=1) return context
def get_initial(self): # assigned categories budgets = Budget.objects.for_month(self.month) budget_spending = Split.objects.personal().past().date_range( self.month, last_day_of_month(self.month)).values( 'category', 'category__name').annotate(spent=Sum('amount')) budget_spending = {e['category']: abs(e['spent']) for e in budget_spending} initial = [] # existing budgets for budget in budgets: initial.append({ 'budget_id': budget.id, 'category_id': budget.category_id, 'category_name': budget.category.name, 'spent': budget_spending.get(budget.category_id, 0), 'amount': budget.amount, 'left': - budget_spending.get(budget.category_id, 0) + budget.amount, 'month': self.month, }) ids = [budget.category_id for budget in budgets] for category in Category.objects.exclude(id__in=ids).exclude(active=False): initial.append({ 'budget_id': -1, 'category_id': category.id, 'category_name': category.name, 'spent': budget_spending.get(category.id, 0), 'amount': 0, 'left': - budget_spending.get(category.id, 0), 'month': self.month, }) return initial
def get_context_data(self, **kwargs): dstart = date.today().replace(day=1) dend = last_day_of_month(dstart) context = super().get_context_data(**kwargs) context['menu'] = 'home' queryset = Split.objects.personal() context['balance'] = queryset.aggregate( models.Sum('amount'))['amount__sum'] or 0 queryset = queryset.date_range(dstart, dend) context['income'] = abs(queryset.income().past().aggregate( models.Sum('amount'))['amount__sum'] or 0) context['expenses'] = abs(queryset.expense().past().aggregate( models.Sum('amount'))['amount__sum'] or 0) context['difference'] = context['income'] - context['expenses'] context['accounts'] = Account.objects.filter( account_type=Account.PERSONAL, show_on_dashboard=True) upcoming = Split.objects.personal().upcoming().transfers_once() recurrences = RecurringTransaction.objects.due_in_month() for recurrence in recurrences: if recurrence.is_due: context['overdue_transactions'] = True break context['upcoming_transactions'] = upcoming context['upcoming_recurrences'] = recurrences context['transactions'] = Split.objects.personal().transfers_once( ).past().select_related('account', 'opposing_account', 'category', 'transaction')[:10] outstanding = 0 for t in upcoming: outstanding += t.amount for r in recurrences: if r.transaction_type == Transaction.WITHDRAW: outstanding -= r.amount elif r.transaction_type == Transaction.DEPOSIT: outstanding += r.amount context['outstanding'] = outstanding context['expected_balance'] = context['balance'] + outstanding # last month previous_last = dstart - timedelta(days=1) previous_first = previous_last.replace(day=1) queryset = Split.objects.personal().date_range(previous_first, previous_last) context['previous_income'] = abs( queryset.income().aggregate(models.Sum('amount'))['amount__sum'] or 0) context['previous_expenses'] = abs( queryset.expense().aggregate(models.Sum('amount'))['amount__sum'] or 0) context['previous_difference'] = context['previous_income'] - context[ 'previous_expenses'] context['today'] = date.today() context['past'] = date.today() - timedelta(days=60) return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'charts' context['today'] = date.today() context['minus_3_months'] = date.today() - relativedelta(months=3) context['minus_6_months'] = date.today() - relativedelta(months=6) context['minus_12_months'] = date.today() - relativedelta(years=1) context['first_day_of_month'] = date.today().replace(day=1) context['last_day_of_month'] = last_day_of_month(date.today()) return context
def get_queryset(self): queryset = super().get_queryset() queryset = queryset.filter(account=self.kwargs.get('pk')).select_related( 'category', 'account', 'transaction', 'opposing_account') if 'month' in self.kwargs: self.month = datetime.strptime(self.kwargs.get('month'), '%Y%m') else: self.month = datetime.combine(date.today().replace(day=1), datetime.min.time()) self.dend = last_day_of_month(self.month) queryset = queryset.date_range(self.month, self.dend) return queryset
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'recurrences' amount = 0 income = 0 expenses = 0 today = date.today() last = last_day_of_month(today) remaining = 0 for t in context['transactions']: if t.interval == RecurringTransaction.MONTHLY or ( t.interval == RecurringTransaction.ANNUALLY and t.date.month == today.month and t.date.year == today.year ) or t.interval == RecurringTransaction.WEEKLY or t.interval == RecurringTransaction.DAILY: difference = abs( (last_day_of_month(date.today()) - t.date).days) if t.interval == RecurringTransaction.WEEKLY: amount = t.amount * (1 + math.floor( (difference / 7) / t.multiplier)) elif t.interval == RecurringTransaction.DAILY: amount = t.amount * (1 + math.floor(difference / t.multiplier)) else: amount = t.amount if t.transaction_type == Transaction.WITHDRAW: expenses += amount if t.date <= last: remaining -= amount elif t.transaction_type == Transaction.DEPOSIT: income += amount if t.date <= last: remaining += amount context['expenses'] = expenses context['income'] = income context['total'] = income - expenses context['remaining'] = remaining return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'categories' dstart = self.month dend = last_day_of_month(dstart) expenses = Split.objects.personal().expense().past().date_range( dstart, dend).order_by('category').values( 'category', 'category__name').annotate(spent=Sum('amount')) income = Split.objects.personal().income().past().date_range( dstart, dend).order_by('category').values( 'category', 'category__name').annotate(income=Sum('amount')) categories = [] sum_income = sum(x['income'] for x in income) sum_expense = sum(x['spent'] for x in expenses) category_map = {} for index, c in enumerate(expenses): id = c['category'] categories.append({ 'id': id, 'name': c['category__name'] or '', 'spent': c['spent'], 'income': 0 }) category_map[id] = index for c in income: id = c['category'] if id in category_map: categories[category_map[id]]['income'] = c['income'] else: categories.append({ 'id': id, 'name': c['category__name'] or '', 'income': c['income'], 'spent': 0 }) categories.sort(key=lambda c: c['name']) context['categories'] = categories context['sum_income'] = sum_income context['sum_expense'] = sum_expense context['month'] = self.month context['next_month'] = self.month + relativedelta(months=1) context['previous_month'] = self.month - relativedelta(months=1) return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'categories' dstart = date.today().replace(day=1) dend = last_day_of_month(dstart) splits = Split.objects.personal().date_range( dstart, dend).select_related('category') categories = defaultdict(int) for s in splits: categories[s.category] += s.amount for c in categories.keys(): categories[c] = abs(categories[c]) context['categories'] = dict(categories) return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'buffet' dstart = self.month dend = last_day_of_month(dstart) split = Split.objects.personal().past().date_range(dstart, dend).order_by('buffet').values('buffet').annotate(spent=Sum('amount')) buffet = [] spent = 0 income_total = 0 if len(split): for c in split: try: buffet.append({'id': c['buffet'] if c['buffet'] is not None else 0, 'name': get_buffet_type_str(c['buffet']), 'total': c['spent'], 'percent': 0,}) if c['buffet'] == 3: income_total = c['spent'] except: pass buffet = sorted(buffet, key=lambda x: x['id'] == 5) saving_total = 0 for buff in buffet: if 1 <= buff['id'] <= 3: saving_total+=buff['total'] if 1 <= buff['id'] <= 2: buff['total'] = -buff['total'] if income_total > 0: buff['percent'] = buff['total'] / income_total else: buff['percent'] = 0 if buff['id'] == 0: buff['id'] = 7 if buff['id'] == 4: buff['id'] = 5 if income_total > 0: saving_percent = saving_total/income_total else: saving_percent = 0 try: if buffet[0]['id'] == 7: buffet.append(buffet.pop(0)) except: pass buffet.insert(2, {'id': 6, 'name': 'Savings', 'total': saving_total, 'percent': saving_percent}) context['buffet'] = buffet context['month'] = self.month context['next_month'] = self.month + relativedelta(months=1) context['previous_month'] = self.month - relativedelta(months=1) return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'charts' context['today'] = date.today() context['minus_3_months'] = date.today() - relativedelta(months=3) context['minus_6_months'] = date.today() - relativedelta(months=6) context['minus_12_months'] = date.today() - relativedelta(years=1) context['minus_24_months'] = date.today() - relativedelta(years=2) context['minus_36_months'] = date.today() - relativedelta(years=3) context['minus_48_months'] = date.today() - relativedelta(years=4) context['minus_60_months'] = date.today() - relativedelta(years=5) context['max'] = Split.objects.order_by('date').first().date context['first_day_of_month'] = date.today().replace(day=1) context['last_day_of_month'] = last_day_of_month(date.today()) return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['menu'] = 'categories' dstart = date.today().replace(day=1) dend = last_day_of_month(dstart) categories = Split.objects.personal().past().date_range( dstart, dend).order_by('category').values( 'category', 'category__name').annotate(spent=Sum('amount')) categories = [(e['category'], e['category__name'], abs(e['spent'])) for e in categories] all_categories = list(Category.objects.values_list('id', 'name')) for id, name, spent in categories: if id: all_categories.remove((id, name)) for id, name in all_categories: categories.append((id, name, 0)) context['categories'] = categories return context
def get_context_data(self, **kwargs): dstart = date.today().replace(day=1) dend = last_day_of_month(dstart) context = super().get_context_data(**kwargs) context['menu'] = 'home' queryset = Split.objects.personal().past() context['balance'] = queryset.aggregate( models.Sum('amount'))['amount__sum'] or 0 queryset = queryset.date_range(dstart, dend) context['income'] = abs( queryset.income().aggregate(models.Sum('amount'))['amount__sum'] or 0) context['expenses'] = abs( queryset.expense().aggregate(models.Sum('amount'))['amount__sum'] or 0) context['difference'] = context['income'] - context['expenses'] context['accounts'] = Account.objects.personal().shown_on_dashboard() upcoming = Split.objects.personal().upcoming().transfers_once() recurrences = RecurringTransaction.objects.due_in_month() context['upcoming_transactions'] = upcoming context['upcoming_recurrences'] = recurrences context['transactions'] = Split.objects.personal().transfers_once( ).past().select_related('account', 'opposing_account', 'category', 'transaction')[:10] outstanding = 0 for t in upcoming: if t.transaction.transaction_type != Transaction.TRANSFER: outstanding += t.amount context['working_balance'] = context['balance'] + outstanding outstanding = 0 amount = 0 for r in recurrences: difference = abs((last_day_of_month(date.today()) - r.date).days) if r.interval == RecurringTransaction.WEEKLY: amount = r.amount * (1 + math.floor( (difference / 7) / r.multiplier)) elif r.interval == RecurringTransaction.DAILY: amount = r.amount * (1 + math.floor(difference / r.multiplier)) else: amount = r.amount if r.transaction_type == Transaction.WITHDRAW: outstanding -= amount elif r.transaction_type == Transaction.DEPOSIT: outstanding += amount if r.is_due: context['overdue_transactions'] = True context['outstanding'] = outstanding context['expected_balance'] = context['working_balance'] + outstanding # last month previous_last = dstart - timedelta(days=1) previous_first = previous_last.replace(day=1) queryset = Split.objects.personal().date_range(previous_first, previous_last) context['previous_income'] = abs( queryset.income().aggregate(models.Sum('amount'))['amount__sum'] or 0) context['previous_expenses'] = abs( queryset.expense().aggregate(models.Sum('amount'))['amount__sum'] or 0) context['previous_difference'] = context['previous_income'] - context[ 'previous_expenses'] context['today'] = date.today() context['last_month'] = previous_first context['past'] = date.today() - timedelta(days=60) return context