def report_year(year): start = date(year, 1, 1) end = date(year + 1, 1, 1) - relativedelta(days=1) date_args = "date_start=%s&date_end=%s" % (start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")) worth = obtain_worth(end) aggregate = Minor.listForReport(start, end) (income, expense) = obtain_aggregate(aggregate) result = Minor.listForYearlyReport(year) month_table = init_table() table = {} for month in xrange(1, 13): table[month] = copy.deepcopy(month_table) table[month]["income"] = 0 table[month]["expense"] = 0 for (major, _, _) in table[1]["majors"]: table[month][("major", major)] = 0 table["total"] = {} table["order"] = table[1]["order"] table["majors"] = table[1]["majors"] for minor_id in table["order"]: table["total"][minor_id] = 0 for (major, _, _) in table["majors"]: table["total"][("major", major)] = 0 for row in result: (major_id, major_name, isIncome, minor_name, _) = table[row.month][row.id] table[row.month][row.id] = (major_id, major_name, isIncome, minor_name, row.amount) table[row.month]["income" if isIncome else "expense"] += row.amount table[row.month][("major", major_id)] += row.amount table["total"][row.id] += row.amount table["total"][("major", major_id)] += row.amount return render_template( "report.html", year=year, table=table, worth=worth, income=income, expense=expense, date_args=date_args )
def major_detail(id): major = get_object_or_404(Major, Major.id == id) minors = Minor.listWithStats(id) num_items = 0 for minor in minors: num_items += minor.count return render_template('major.html', major=major, minors=minors, num_items=num_items)
def minor_edit(id): try: minor = Minor.get(Minor.id == id) minor.name = request.form['name'] minor.major = request.form['major_id'] minor.save() except: return jsonify(success=False) return jsonify(success=True)
def minor_add(): try: major_id = request.form['major_id'] major = get_object_or_404(Major, Major.id == major_id) minor = Minor.create(name=request.form['name'], major=major) except: flash('Category #%d not found.' % major_id, 'danger') return jsonify(success=False) flash('A new subcategory is added.', 'success') return jsonify(success=True)
def major_add(): if request.method == 'POST': if request.form.get('major_id'): major = get_object_or_404(Major, Major.id == request.form['major_id']) minors = Minor.listWithStats(request.form['major_id']) major.name = request.form['name'] major.income = bool(request.form.get('income')) major.save() flash('Category #%d updated successfully.' % major.id, 'success') else: major = Major.create(name=request.form['name'], income=bool(request.form.get('income'))) minors = [] for minor_name in string.split(request.form['minors'], ','): if len(minor_name) > 0: minor = Minor.create(name=string.strip(minor_name), major=major) minors.append(minor) flash('A category created successfully.', 'success') return render_template('major.html', major=major, minors=minors) return render_template('major.html')
def report_last_days(days): end = date.today() start = end - relativedelta(days=(days - 1)) date_args = "&date_start=%s&date_end=%s" % (start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")) worth = obtain_worth(end) result = Minor.listForReport(start, end) (income, expense) = obtain_aggregate(result) table = simple_minor_table(result) return render_template( "report.html", days=days, table=table, worth=worth, income=income, expense=expense, date_args=date_args )
def report_year(year): start = date(year, 1, 1) end = date(year + 1, 1, 1) - relativedelta(days=1) date_args = 'date_start=%s&date_end=%s' % \ (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d')) worth = obtain_worth(end) aggregate = Minor.listForReport(start, end) (income, expense) = obtain_aggregate(aggregate) result = Minor.listForYearlyReport(year) month_table = init_table() table = {} for month in xrange(1, 13): table[month] = copy.deepcopy(month_table) table[month]['income'] = 0 table[month]['expense'] = 0 for (major, _, _) in table[1]['majors']: table[month][('major', major)] = 0 table['total'] = {} table['order'] = table[1]['order'] table['majors'] = table[1]['majors'] for minor_id in table['order']: table['total'][minor_id] = 0 for (major, _, _) in table['majors']: table['total'][('major', major)] = 0 for row in result: (major_id, major_name, isIncome, minor_name, _) = table[row.month][row.id] table[row.month][row.id] = (major_id, major_name, isIncome, minor_name, row.amount) table[row.month]['income' if isIncome else 'expense'] += row.amount table[row.month][('major', major_id)] += row.amount table['total'][row.id] += row.amount table['total'][('major', major_id)] += row.amount return render_template('report.html', year=year, table=table, worth=worth, income=income, expense=expense, date_args=date_args)
def report_last_days(days): end = date.today() start = end - relativedelta(days=(days - 1)) date_args = '&date_start=%s&date_end=%s' % \ (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d')) worth = obtain_worth(end) result = Minor.listForReport(start, end) (income, expense) = obtain_aggregate(result) table = simple_minor_table(result) return render_template('report.html', days=days, table=table, worth=worth, income=income, expense=expense, date_args=date_args)
def report_month(year, month): start = date(year, month, 1) end = date(year, month, 1) + relativedelta(months=1) - relativedelta(days=1) date_args = "&date_start=%s&date_end=%s" % (start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")) worth = obtain_worth(end) result = Minor.listForReport(start, end) (income, expense) = obtain_aggregate(result) table = simple_minor_table(result) return render_template( "report.html", year=year, month=date(1900, month, 1).strftime("%B"), table=table, worth=worth, income=income, expense=expense, date_args=date_args, )
def report_month(year, month): start = date(year, month, 1) end = date(year, month, 1) + relativedelta(months=1) - relativedelta(days=1) date_args = '&date_start=%s&date_end=%s' % \ (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d')) worth = obtain_worth(end) result = Minor.listForReport(start, end) (income, expense) = obtain_aggregate(result) table = simple_minor_table(result) return render_template('report.html', year=year, month=date(1900, month, 1).strftime('%B'), table=table, worth=worth, income=income, expense=expense, date_args=date_args)
def major_delete(id): major = get_object_or_404(Major, Major.id == id) major.delete_instance() minors = Minor.delete().where(Minor.major == id).execute() flash('Category #%d is deleted.' % id, 'success') return jsonify(success=True)