def pie_expense(request): year = request.GET.get(key="year") month = request.GET.get(key="month") figtitle = "支出内訳" if year is None and month is None: kakeibos = Kakeibos.objects.all() elif month is None: kakeibos = Kakeibos.objects.filter(date__year=year) figtitle = figtitle + "(" + str(year) + ")" else: kakeibos = Kakeibos.objects.filter(date__month=month, date__year=year) figtitle = figtitle + "(" + str(year) + "/" + str(month) + ")" expense = mylib.cal_sum_or_0(kakeibos.filter(way="支出(現金)")) debit = mylib.cal_sum_or_0(kakeibos.filter(way="引き落とし")) shared_expense = mylib.cal_sum_or_0(kakeibos.filter(way="共通支出")) credit = mylib.cal_sum_or_0(kakeibos.filter(way="支出(クレジット)")) data = { "Cash": expense, "Debit": debit, "Shared_expense": shared_expense, "Card": credit, } colors = { "Cash": "green", "Debit": "blue", "Shared_expense": "orange", "Card": "gray", } res = figure.fig_pie_basic_colored(data=data, figtitle=figtitle, colors=colors, xkcd=True) return res
def resource_current_val(rsname, val): try: rs = Resources.objects.get(name=rsname) # 初回 if rs.current_val is None or val is 0: move_to = mylib.cal_sum_or_0(Kakeibos.objects.filter(move_to=rs)) move_from = mylib.cal_sum_or_0(Kakeibos.objects.filter(move_from=rs)) rs.current_val = rs.initial_val + move_to - move_from # 初回以外 else: rs.current_val = rs.current_val + val rs.save() logger.info(rsname+":"+str(rs.current_val)) msg = rsname + " is updated successfully" logger.info(msg) status = True except Exception as e: msg = "Failed to update " + rsname msg = msg + ": " + str(e) logger.error(msg) status = False res = { "msg": msg, "status": status } return res
def pie_resource(request): year = request.GET.get(key="year") month = request.GET.get(key="month") figtitle = "資産内訳" if year is None and month is None: kakeibos = Kakeibos.objects.all() elif month is None: kakeibos = Kakeibos.objects.filter(date__lt=date(int(year) + 1, 1, 1)) figtitle = figtitle + "(" + str(year) + ")" else: kakeibos = Kakeibos.objects.filter( date__lt=date(int(year), int(month) + 1, 1)) figtitle = figtitle + "(" + str(year) + "/" + str(month) + ")" resources = Resources.objects.all() data = dict() for rs in resources: move_to = mylib.cal_sum_or_0(kakeibos.filter(move_to=rs)) move_from = mylib.cal_sum_or_0(kakeibos.filter(move_from=rs)) data[rs.name] = rs.initial_val + move_to - move_from res = figure.fig_pie_basic(data=data, figtitle=figtitle, figid=10, threshold=0) return res
def lines_usage_cash(request): ka = Kakeibos.objects.filter(move_from=Resources.objects.get(name="財布")) year = request.GET.get(key="year") month = request.GET.get(key="month") figtitle = "現金支出内訳" if year is None and month is None: kakeibos = ka.all() elif month is None: kakeibos = ka.filter(date__year=year) figtitle = figtitle + "(" + str(year) + ")" else: kakeibos = ka.filter(date__month=month, date__year=year) figtitle = figtitle + "(" + str(year) + "/" + str(month) + ")" left = list() usages = [i for i in Usages.objects.filter(is_expense=True)] height = [list() for i in usages] colors = [i.color for i in usages] for i in range(1, 32): ka = kakeibos.filter(date__day=i) left.append(i) tmp = 0 for us in usages: height[tmp].append(mylib.cal_sum_or_0(ka.filter(usage=us))) tmp += 1 heights = [i for i in height] lefts = [left for i in height] labels = [i.name for i in usages] res = figure.fig_lines_basic(heights=heights, lefts=lefts, colors=colors, labels=labels, figtitle=figtitle, figsize=(11, 11)) return res
def barline_usage(request): id = request.GET.get("usageid") figid = 100 + int(id) us = Usages.objects.get(pk=id) label = us.name height = list() height_sum = list() xticklabel = list() for j in range(2017, 2019): for i in range(4, 13): ka = Kakeibos.objects.filter(date__year=j, date__month=i) if ka.__len__() is 0: break else: ka = ka.filter(usage=us) height.append(mylib.cal_sum_or_0(ka)) height_sum.append(sum(height)) xticklabel.append(str(j - 2000) + "/" + str(i)) xlim = [i for i in range(0, len(height))] ylim = [i for i in range(0, max(height_sum), 100000)] yticklabel = [money.convert_yen(i) for i in ylim] res = figure.fig_barline_basic(height_bar=height, left_bar=xlim, label_bar=label, height_line=height_sum, left_line=xlim, label_line="cumulative", ylim=ylim, yticklabel=yticklabel, xlim=xlim, xticklabel=xticklabel, figsize=(10, 10), figid=figid, xkcd=True) return res
def pie_shared(request): year = request.GET.get(key="year") month = request.GET.get(key="month") figtitle = "共通支出内訳" if year is None and month is None: shared = SharedKakeibos.objects.all() figid = 15 elif month is None: shared = SharedKakeibos.objects.filter(date__year=year) figtitle = figtitle + "(" + str(year) + ")" figid = int(year) + 15 else: shared = SharedKakeibos.objects.filter(date__year=year, date__month=month) figtitle = figtitle + "(" + str(year) + "/" + str(month) + ")" figid = int(year) + int(month) + 15 usages = Usages.objects.filter(is_expense=True) data = dict() for us in usages: data[us.name] = mylib.cal_sum_or_0(shared.filter(usage=us)) res = figure.fig_pie_basic(data=data, figtitle=figtitle, figid=figid, threshold=5, xkcd=True) return res
def bars_resource(request): resources = Resources.objects.all() data = dict() year = request.GET.get(key="year") month = request.GET.get(key="month") figtitle = "資産内訳と先月比" if year is None or month is None: year = date.today().year month = date.today().month # 指定月の月末日を取得 today = date(int(year), int(month) + 1, 1) - relativedelta(days=1) figtitle = figtitle + "(" + str(year) + "/" + str(month) + ")" # 指定月の一ヶ月前の月末日を取得 last = date(int(year), int(month), 1) - relativedelta(days=1) # 範囲指定でレコード取得 ka = Kakeibos.objects.filter(date__lte=today) kal = Kakeibos.objects.filter(date__year=today.year, date__month=today.month) # 計算 for rs in resources: # left bar: this move_to = mylib.cal_sum_or_0(ka.filter(move_to=rs)) move_from = mylib.cal_sum_or_0(ka.filter(move_from=rs)) tmp = rs.initial_val + move_to - move_from if tmp < 0: tmp = 0 data[rs.name] = [tmp] # right bar: last move_to = mylib.cal_sum_or_0(kal.filter(move_to=rs)) move_from = mylib.cal_sum_or_0(kal.filter(move_from=rs)) tmp = data[rs.name][0] - move_to + move_from if tmp < 0: tmp = 0 data[rs.name].append(tmp) labels = [ str(today.year) + "/" + str(today.month), str(last.year) + "/" + str(last.month), ] res = figure.fig_bars_basic(data=data, figtitle=figtitle, vbar_labels=labels, figsize=(11, 11)) return res
def bars_balance(request): year = request.GET.get(key="year") month = request.GET.get(key="month") figtitle = "収支内訳" if year is None and month is None: kakeibos = Kakeibos.objects.all() elif month is None: kakeibos = Kakeibos.objects.filter(date__year=year) figtitle = figtitle + "(" + str(year) + ")" else: kakeibos = Kakeibos.objects.filter(date__month=month, date__year=year) figtitle = figtitle + "(" + str(year) + "/" + str(month) + ")" income = mylib.cal_sum_or_0(kakeibos.filter(way="収入")) expense = mylib.cal_sum_or_0(kakeibos.filter(way="支出(現金)")) debit = mylib.cal_sum_or_0(kakeibos.filter(way="引き落とし")) shared_expense = mylib.cal_sum_or_0(kakeibos.filter(way="共通支出")) credit = mylib.cal_sum_or_0(kakeibos.filter(way="支出(クレジット)")) data = { "支出(現金)": [0, expense], "引き落とし": [0, debit], "共通支出": [0, shared_expense], "支出(クレジット)": [0, credit], "収入": [income, 0], } colors = { "支出(現金)": "green", "引き落とし": "blue", "共通支出": "orange", "支出(クレジット)": "gray", "収入": "tomato", } vbar_labels = ['Income', 'Expense'] res = figure.fig_bars_basic_color( data=data, figtitle=figtitle, vbar_labels=vbar_labels, colors=colors, figsize=(10, 10), ) return res
def pie_credit(request): year = request.GET.get(key="year") month = request.GET.get(key="month") figtitle = "項目別クレジット内訳" credititems = CreditItems.objects.all() if year is None and month is None: credit = Credits.objects.all() elif month is None: credit = Credits.objects.filter(date__year=year) figtitle = figtitle + "(" + str(year) + ")" else: credit = Credits.objects.filter(date__year=year, date__month=month) figtitle = figtitle + "(" + str(year) + "/" + str(month) + ")" credit_sum = dict() for ci in credititems: credit_sum[ci.name] = mylib.cal_sum_or_0(credit.filter(credit_item=ci)) res = figure.fig_pie_basic(data=credit_sum, figtitle=figtitle, figsize=(12, 12), figid=3) return res
def pie_usage(request): ka = Kakeibos.objects.filter(move_to=None) year = request.GET.get(key="year") month = request.GET.get(key="month") figtitle = "支出内訳" figid = 250 if year is None and month is None: kakeibos = ka.all() elif month is None: kakeibos = ka.filter(date__year=year) figtitle = figtitle + "(" + str(year) + ")" figid = figid + int(year) else: kakeibos = ka.filter(date__month=month, date__year=year) figtitle = figtitle + "(" + str(year) + "/" + str(month) + ")" figid = figid + int(year) + int(month) usages = Usages.objects.filter(is_expense=True) data = dict() for us in usages: data[us.name] = mylib.cal_sum_or_0(kakeibos.filter(usage=us)) res = figure.fig_pie_basic(data=data, figtitle=figtitle, figid=figid) return res
def dashboard(request): today = date.today() # Form kakeibo_form = KakeiboForm(initial={'date': today}) shared_form = SharedKakeiboForm(initial={'date': today}) event_form = EventForm(initial={'date': today}) usual_records = UsualRecord.objects.all() # kakeibo kakeibos = Kakeibos.objects.filter(date__month=today.month, date__year=today.year) kakeibos_out = kakeibos.filter(way__in=("支出(現金)", "引き落とし")) kakeibos_expense = kakeibos.filter(way__in=("支出(クレジット)", "支出(現金)", "引き落とし")) # resource current_resource = Resources.objects.all() # usage current_usage = kakeibos_expense.values('usage__name').annotate( sum=Sum('fee')).order_by("-sum") # event events = Event.objects.filter(date__year=today.year).order_by( "-is_active", "-date") event_sum_plan = events.aggregate(sum=Sum('sum_plan'))['sum'] event_sum_actual = 0 for event in events: event_sum_actual += event.sum_actual() # 収入・支出・総資産 income = mylib.cal_sum_or_0(kakeibos.filter(way="収入")) expense = mylib.cal_sum_or_0(kakeibos_out) total = sum([r.current_val() for r in current_resource]) # status, progress_bar pb_kakeibo, status_kakeibo = process_kakeibo.kakeibo_status( income, expense) # shared seisan = mylib.seisan(today.year, today.month) # 赤字→精算あり if seisan['status'] == "赤字": status_shared = "danger" pb_shared = { "in": int(seisan['budget']['sum'] / seisan['payment']['sum'] * 100), "out": 100 } else: status_shared = "primary" pb_shared = { "in": 100, "out": int(seisan['payment']['sum'] / seisan['budget']['sum'] * 100) } # shared_grouped_by_usage shared_grouped_by_usage = SharedKakeibos.objects\ .filter(date__month=today.month, date__year=today.year)\ .values('usage__name').annotate(sum=Sum('fee')).order_by("-sum") # chart.js data = { "現金精算": [0, seisan['seisan'], 0, 0], "予算": [seisan['budget']['hoko'], 0, seisan['budget']['taka'], 0], "支払": [0, seisan['payment']['hoko'], 0, seisan['payment']['taka']], seisan['status']: seisan['rb'], } labels = ["朋子予算", "朋子支払", "敬士予算", "敬士支払"] bar_eom = {"data": data, "labels": labels} output = { "today": today, # kakeibo "inout": income - expense, "income": income, "expense": expense, "total": total, "current_usage": current_usage, "current_resource": current_resource, # shared "seisan": seisan, "shared_grouped_by_usage": shared_grouped_by_usage, "bar_eom": bar_eom, # progress bar and status "pb": { "kakeibo": pb_kakeibo, "shared": pb_shared }, "status": { "kakeibo": status_kakeibo, "shared": status_shared }, # kakeibo_form "kakeibo_form": kakeibo_form, "shared_form": shared_form, "username": request.user.username, "usual_records": usual_records, # events "events": events, "event_sum_actual": event_sum_actual, "event_sum_plan": event_sum_plan, "event_form": event_form, } logger.info("output: " + str(output)) return TemplateResponse(request, 'kakeibo/dashboard.html', output)
def mine(request): today = date.today() # Form kakeibo_form = KakeiboForm(initial={'date': today}) # 貯金扱いの口座 saving_account = [r.name for r in Resources.objects.filter(is_saving=True)] # check year and month from GET parameter year, month = process_kakeibo.yearmonth(request) kakeibos = Kakeibos.objects.filter(date__month=month, date__year=year) kakeibos_out = kakeibos.filter(way__in=("支出(現金)", "引き落とし")) kakeibos_expense = kakeibos.filter(way__in=("支出(クレジット)", "支出(現金)", "引き落とし")) income = mylib.cal_sum_or_0(kakeibos.filter(way="収入")) expense = mylib.cal_sum_or_0(kakeibos_out) # status, progress_bar pb_kakeibo, status_kakeibo = process_kakeibo.kakeibo_status( income, expense) # way current_way = kakeibos_expense.values('way').annotate( sum=Sum('fee')).order_by("-sum") # resource current_resource = Resources.objects.all() # usage current_usage = kakeibos_expense.values('usage__name').annotate( sum=Sum('fee')).order_by("-sum") # saved rs_saved = current_resource.filter(name__in=saving_account) move_to = mylib.cal_sum_or_0(kakeibos.filter(move_to__in=rs_saved)) move_from = mylib.cal_sum_or_0(kakeibos.filter(move_from__in=rs_saved)) saved = move_to - move_from # usage usages_chart = kakeibos_out.values('usage__name').annotate( sum=Sum('fee')).order_by("sum").reverse() # resources_year resources_year_chart, months_chart = process_kakeibo.resources_year_rev(12) # kakeibo-usage usage_list = [u.name for u in Usages.objects.filter(is_expense=True)] kakeibo_usage = process_kakeibo.usage_kakeibo_table(usage_list) # Consolidated_usages: dict --> [(name, val),(name, val),(name, val),...] consolidated_usages_chart = sorted( process_kakeibo.consolidated_usages().items(), key=lambda x: -x[1]) cash_usages_chart = sorted(process_kakeibo.cash_usages().items(), key=lambda x: -x[1]) # total total = sum([r.current_val() for r in current_resource]) total_saved = sum(rs.current_val() for rs in rs_saved) # 1年間での推移 saved_one_year_ago = 0 for ryc in resources_year_chart: if ryc['name'] in saving_account: saved_one_year_ago = ryc["val"][0] break change = { "total": total - sum([i['val'][0] for i in resources_year_chart]), "total_saved": total_saved - saved_one_year_ago } # 年間の収入・支出 inouts_grouped_by_months = process_kakeibo.inouts_grouped_by_months() # output output = { "today": { "year": year, "month": month }, # status "status": status_kakeibo, "saved": saved, "inout": income - expense, # progress_bar "pb_kakeibo": pb_kakeibo, "income": income, "expense": expense, # current list "current_way": current_way, "current_resource": current_resource, "current_usage": current_usage, # chart js "usages_chart": usages_chart, "resources_year_chart": resources_year_chart, "months_chart": months_chart, "consolidated_usages_chart": consolidated_usages_chart, # kus "kakeibo_usage_table": kakeibo_usage, "usage_list": usage_list, # total "total": total, "total_saved": total_saved, "change": change, # igbn "inouts_grouped_by_months": inouts_grouped_by_months, # cash_usages_chart "cash_usages_chart": cash_usages_chart, # form "kakeibo_form": kakeibo_form, } return TemplateResponse(request, 'kakeibo/mine.html', output)
def barline_expense_cash(request): year = request.GET.get("year") month = request.GET.get("month") rs = Resources.objects.get(name="財布") kakeibos = Kakeibos.objects.filter(move_from=rs) height = list() left = list() height_sum = list() xlim = list() xticklabel = list() if year is not None and month is not None: kakeibos = kakeibos.filter(date__year=year, date__month=month) figtitle = "現金支出推移 (" + str(year) + "/" + str(month) + ")" ylim = [i for i in range(0, 130000, 10000)] figsize = (10, 10) figid = 100 + int(year) + int(month) for j in range(1, 32): ka = kakeibos.filter(date__day=j) left.append(j) height.append(mylib.cal_sum_or_0(ka)) height_sum.append(sum(height)) elif year is not None: kakeibos = kakeibos.filter(date__year=year) figtitle = "現金支出推移 (" + str(year) + ")" ylim = [i for i in range(0, 1200000, 200000)] figsize = (12, 12) figid = 100 + int(year) for j in range(1, 13): ka = kakeibos.filter(date__month=j) left.append(j) height.append(mylib.cal_sum_or_0(ka)) height_sum.append(sum(height)) else: dates = [ka.date for ka in kakeibos] mindate = min(dates) maxdate = max(dates) figtitle = "現金支出推移" ylim = [i for i in range(0, 3000000, 500000)] figsize = (12, 12) figid = 100 tmp = 0 while mindate < maxdate: ka = kakeibos.filter(date__year=mindate.year, date__month=mindate.month) left.append(tmp) height.append(mylib.cal_sum_or_0(ka)) height_sum.append(sum(height)) mindate += relativedelta(months=1) xlim.append(tmp) xticklabel.append( str(mindate.year - 2000) + "/" + str(mindate.month)) tmp += 1 yticklabel = [money.convert_yen(i) for i in ylim] label_line = "支出累計 (" + money.convert_yen(sum(height)) + ")" res = figure.fig_barline_basic(height_bar=height, left_bar=left, label_bar="現金支出", height_line=height_sum, left_line=left, label_line=label_line, xlim=xlim, xticklabel=xticklabel, ylim=ylim, yticklabel=yticklabel, figtitle=figtitle, figsize=figsize, figid=figid) return res