def get_annual_bar(year): record, year, month = data_aggregation(year=year) record = MouthCost(record, year, month) x, y = record.web_annual_bar(year) bar = draw.draw_balance_bar(x, y, title='年度收支', markline=1700 + 1700) return bar.dump_options()
def get_category_pie(year, month): record, year, month = data_aggregation(year, month) record = MouthCost(record, year, month) eat, other = record.web_category_pie() if year == 'null' or month == 'null': year = str(datetime.now().year) month = str(datetime.now().month) pie = draw.draw_category_pie(inner=eat[:NUMBER_WEB_CATEGORY_PIE_EAT], outside=other[:NUMBER_WEB_CATEGORY_PIE_OTHER], inner_title=f'{year}年{month}饮食报表', outer_title=f'{year}年{month}其他报表') return pie.dump_options()
def get_category(year, month): name = 'select_month' record, year, month = data_aggregation(year, month) record = MouthCost(record, year, month) data, columns = record.to_table(category=True) return render_template("category.html", usage_chart=url_for('get_category_pie', year=year, month=month), name=name, data=data, columns=columns)
def get_bar_chart(year, month): record, year, month = data_aggregation(year, month) record = MouthCost(record, year, month) title = '消费统计' if record: x, y = record.web_index_bar() title = '消费合计:{}元'.format(round(sum([float(i) for i in y[2][1]]), 2)) else: x = ['无数据'] y = [] line = draw.draw_balance_line(xaxis=x, yaxis=y, title=title) return line.dump_options()
def get_all_eat_other_record(year) -> tuple: """将指定年分的12个月的饮食和其他消费类别总计汇总""" eat_list = [] other_list = [] months = [month for month in range(1, 13)] for month in months: # 将整年的所有饮食和其他消费合并 record, year, month = data_aggregation(year=year, month=month) if record: manage = MouthCost(record, year, month) eat_cost, other_cost = manage.web_category_pie() eat_list.append(eat_cost) other_list.append(other_cost) return eat_list, other_list
def get_month_usage(): """ 首页饼状图 :return: Pie """ budget = float(MouthCost.current_fix_data(other_record)['budget']) record, year, month = data_aggregation(default=True) record = MouthCost(record, year, month) data = record.web_index_pie() pie = draw.draw_usage_pie(payout=data, budget=[('预算', budget)], title="本月结余") return pie.dump_options()
def get_current_month_bar(): """ 首页条形统计图 :return: Bar """ record, year, month = data_aggregation(default=True) record = MouthCost(record, year, month) x, y = record.web_index_bar() status, columns = record.to_table() markline = list(filter(lambda d: d['name'] == '日付上限', status))[0]['balance'] bar = draw.draw_balance_bar(xaxis=x, yaxis=y, markline=markline) return bar.dump_options()
def index(): record, year, month = data_aggregation(default=True) record = MouthCost(record, year, month) status, columns = record.to_table() paid_limit = list(filter(lambda d: d['name'] == '日付上限', status))[0]['balance'] return render_template("index.html", chart_url=url_for('get_current_month_bar'), usage_chart=url_for("get_month_usage"), data=status, columns=columns, paid_limit=paid_limit, note='当日可用¥')
def get_details(year, month): name = 'select_month' record, year, month = data_aggregation(year, month) record = MouthCost(record, year, month) data, columns = [], [] if record: data, columns = record.to_detail_table() return render_template("details.html", chart_url=url_for('get_bar_chart', year=year, month=month), name=name, data=data, columns=columns)
def get_date(year='null', month='null', default=None): """ :return: Dict """ year, month = MouthCost.get_special_date(year=year, month=month, default=default) path = f'{base_dir[:-4]}/cost_record/{year}/{year}_{month}.csv' record = MouthCost._read_csv(path) if record: record.sort( key=lambda date: datetime(year=int(year), month=int(date['date'].split('_')[0]), day=int(date['date'].split('_')[-1]))) return record
def get_update(year, month): name = 'select_month' record, year, month = data_aggregation(year, month) record = MouthCost(record, year, month) data, columns = [], [] if record: data, columns = record.to_detail_table(update=True) return render_template("update.html", date_f=(year, month), year=year, month=month, name=name, data=data, columns=columns, dt=datetime.now())
def add_record(params): try: date = params.get('date') name = params.get('name') payment = params.get('payment') type = params.get('type') note = params.get('note') year, month = eval(params.get('date_f')) month = date.split('_')[0] day = date.split('_')[-1] year, month = MouthCost.get_special_date( year=year, month=month, day=day, default=True, ) path = f'{base_dir[:-4]}/cost_record/{year}/{year}_{month}.csv' content = f'\n{date},{name},{payment},{type},{note}' # csv文件每两行中间都有一行空白行,解决办法就是写入后面加上newline='' with open(path, 'a+', encoding='GBK', newline='') as csv_file: csv_file.write(content) return (0, '新增成功!') except Exception as e: return (1, e)
def get_data_columns(year): month_total = 0 eat_total = 0 other_total = 0 salary_total = 0 rent_total = 0 salary_list = [] months = [month for month in range(1, 13)] for month in months: # 将整年的所有饮食和其他消费合并 record, year, month = data_aggregation(year=year, month=month) if record: manage = MouthCost(record, year, month) salary_list = manage.other_record # 吃穿总计 month_total += manage.all_total() salary_info = list( filter(lambda d: d['date'] == f"{year}_" f"{month}", salary_list)) # 再加上房租 if salary_info: month_total += float(salary_info[0].get('rent', 0)) eat_total += sum(manage.get_eat_y()) other_total += sum(manage.get_other_y()) for salary in salary_list: salary_total += eval(salary.get('salary', 0)) rent_total += eval(salary.get('rent', 0)) rest_total = round(salary_total - month_total, 2) status = [ { 'name': f'{year}年收入金额', 'balance': round(salary_total, 2) }, { 'name': f'{year}年饮食金额', 'balance': round(eat_total, 2) }, { 'name': f'{year}年其他金额', 'balance': round(other_total, 2) }, { 'name': f'{year}年吃穿金额', 'balance': round(eat_total + other_total, 2) }, { 'name': f'{year}年租房金额', 'balance': round(rent_total, 2) }, { 'name': f'{year}年支出金额', 'balance': round(month_total, 2) }, { 'name': f'{year}年盈亏金额', 'balance': f"{rest_total}" if rest_total > 0 else f"" f"-{rest_total}" }, ] columns = [ { "field": "name", # which is the field's name of data key "title": "名称", # display as the table header's name "sortable": False, }, { "field": "balance", "title": "金额 (元)", "sortable": False, }, ] return status, columns
# -*- coding: utf-8 -*- # File Name: get_date # Author : wjh # date: 2020/5/14 import os import re from datetime import datetime from flask import jsonify from api.bill_manage import MouthCost from setting import * base_dir = os.path.dirname(__file__) other_record = MouthCost.read_other_record(base_dir[:-4]) def get_date(year='null', month='null', default=None): """ :return: Dict """ year, month = MouthCost.get_special_date(year=year, month=month, default=default) path = f'{base_dir[:-4]}/cost_record/{year}/{year}_{month}.csv' record = MouthCost._read_csv(path) if record: record.sort( key=lambda date: datetime(year=int(year), month=int(date['date'].split('_')[0]),
import os import json from datetime import datetime from werkzeug.utils import redirect from flask import Flask, render_template, url_for, request, jsonify from api import draw from api import get_bill_record from api.bill_manage import MouthCost from api.get_bill_record import data_aggregation from setting import * app = Flask(__name__) other_record = MouthCost.read_other_record(os.path.dirname(__file__)) @app.route("/") @app.route("/index") def index(): record, year, month = data_aggregation(default=True) record = MouthCost(record, year, month) status, columns = record.to_table() paid_limit = list(filter(lambda d: d['name'] == '日付上限', status))[0]['balance'] return render_template("index.html", chart_url=url_for('get_current_month_bar'), usage_chart=url_for("get_month_usage"), data=status, columns=columns,