upgrade to beancount v3
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
from beancount import loader
|
||||
from beancount.query import query
|
||||
from beanquery import query
|
||||
from beancount.parser import printer
|
||||
import argparse
|
||||
from tabulate import tabulate
|
||||
from decimal import Decimal
|
||||
from beancount.core.amount import Amount, add, sub, mul
|
||||
from math import floor
|
||||
from beancount.core.amount import Amount, add, sub
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
|
||||
@@ -72,7 +71,7 @@ def get_debt_to_assets_ratio(balances, max):
|
||||
|
||||
def get_emergency_fund_ratio(balances, expenses, low, mid):
|
||||
liquid = 0
|
||||
living_expenses = expenses[0].position.get_only_position(
|
||||
living_expenses = expenses[0][0].get_only_position(
|
||||
).units.number / 12
|
||||
for account, balance in balances.items():
|
||||
if account.startswith("Assets:Liquid"):
|
||||
@@ -129,7 +128,7 @@ def get_solvency_ratio(balances, min):
|
||||
|
||||
|
||||
def get_interest_coverage_ratio(net_monthly_income, expenses, debt_payments, mortgage_payments, min):
|
||||
living_expenses = expenses[0].position.get_only_position().units.number
|
||||
living_expenses = expenses[0][0].get_only_position().units.number
|
||||
interest = debt_payments.number + mortgage_payments.number
|
||||
interest = interest if interest > 0 else 1
|
||||
result = round((net_monthly_income - living_expenses) / interest, 2)
|
||||
@@ -265,7 +264,7 @@ def get_balances(entries, options, date):
|
||||
entries, options, balance_query)
|
||||
balances = {}
|
||||
for row in rrows:
|
||||
balances[row.account] = row.position
|
||||
balances[row[0]] = row[1]
|
||||
return balances
|
||||
|
||||
|
||||
@@ -284,7 +283,7 @@ def get_income(entries, options, date):
|
||||
end_date} WHERE account ~ '^(Income:Work|Income:Savings|Income:Invest)' AND date >= {start_date}"
|
||||
rtypes, rrows = query.run_query(
|
||||
entries, options, income_query)
|
||||
net_monthly_income = rrows[0].position.get_only_position(
|
||||
net_monthly_income = rrows[0][0].get_only_position(
|
||||
).units.number * -1
|
||||
|
||||
start_date, end_date = get_last_year_timestamps(date)
|
||||
@@ -292,7 +291,7 @@ def get_income(entries, options, date):
|
||||
end_date} WHERE account ~ '^(Income:Work|Income:Savings|Income:Invest)' AND date >= {start_date}"
|
||||
rtypes, rrows = query.run_query(
|
||||
entries, options, income_query)
|
||||
net_yearly_income = rrows[0].position.get_only_position(
|
||||
net_yearly_income = rrows[0][0].get_only_position(
|
||||
).units.number * -1
|
||||
return net_monthly_income, net_yearly_income
|
||||
|
||||
@@ -308,7 +307,7 @@ def get_debt_payments(entries, options, date):
|
||||
entries, options, debt_payments_query)
|
||||
rtypes, rrows_mortgage = query.run_query(
|
||||
entries, options, mortgage_payments_query)
|
||||
debt_payments = rrows_debt[0].position.get_only_position().units if len(
|
||||
debt_payments = rrows_debt[0][0].get_only_position().units if len(
|
||||
rrows_debt) > 0 else Amount(Decimal(0), "EUR")
|
||||
mortgage_payments = rrows_mortgage[0].position.get_only_position(
|
||||
).units if len(rrows_mortgage) > 0 else Amount(Decimal(0), "EUR")
|
||||
@@ -321,13 +320,13 @@ def get_savings(entries, options, date):
|
||||
end_date} WHERE account ~ '^Assets:Invest:' AND date >= {start_date}"
|
||||
rtypes, rrows = query.run_query(
|
||||
entries, options, investments_query)
|
||||
result = rrows[0].position.get_only_position().units if len(
|
||||
result = rrows[0][0].get_only_position().units if len(
|
||||
rrows) > 0 else Amount(Decimal(0), "EUR")
|
||||
liabilities_query = f"SELECT convert(sum(position), \"EUR\") as position FROM date <= {
|
||||
end_date} WHERE account ~ '^Liabilities:Credit:Renta4:' AND date >= {start_date}"
|
||||
rtypes, rrows = query.run_query(
|
||||
entries, options, liabilities_query)
|
||||
liabilities = rrows[0].position.get_only_position().units if len(
|
||||
liabilities = rrows[0][0].get_only_position().units if len(
|
||||
rrows) > 0 else Amount(Decimal(0), "EUR")
|
||||
result = add(result, liabilities)
|
||||
return result
|
||||
@@ -345,7 +344,7 @@ def get_assets_pignorats(entries, options, date):
|
||||
date}) as price"
|
||||
rtypes, rrows = query.run_query(
|
||||
entries, options, pignorat_query)
|
||||
result = result + rrows[0].price * assets_pignorats[curr]
|
||||
result = result + rrows[0][0] * assets_pignorats[curr]
|
||||
return result
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
from beancount import loader
|
||||
from beancount.query import query
|
||||
from beanquery import query
|
||||
from beancount.core.data import Custom
|
||||
from beancount.core.amount import Amount, add, sub
|
||||
from beancount.parser import printer
|
||||
@@ -61,7 +61,7 @@ WHERE account ~ \"Equity:(LloguerMiquel|FacturesUtilitatsMiquel)\""""
|
||||
entries, options, equity_query)
|
||||
equity = {}
|
||||
for row in rrows:
|
||||
equity[row.account] = row.sum_position
|
||||
equity[row[0]] = row[1]
|
||||
return equity
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ WHERE account ~ \"Expenses\" OR account ~ \"Liabilities\""""
|
||||
entries, options, expenses_query)
|
||||
expenses = {}
|
||||
for row in rrows:
|
||||
expenses[row.account] = row.sum_position
|
||||
expenses[row[0]] = row[1]
|
||||
return expenses
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
from beancount import loader
|
||||
from beancount.query import query
|
||||
from beanquery import query
|
||||
from beancount.parser import printer
|
||||
import argparse
|
||||
from tabulate import tabulate
|
||||
@@ -9,6 +9,7 @@ from beancount.core.amount import Amount, add, sub, mul
|
||||
from datetime import date
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
|
||||
class bcolors:
|
||||
HEADER = '\033[95m'
|
||||
OKBLUE = '\033[94m'
|
||||
@@ -20,9 +21,11 @@ class bcolors:
|
||||
BOLD = '\033[1m'
|
||||
UNDERLINE = '\033[4m'
|
||||
|
||||
|
||||
def draw_line():
|
||||
print('─' * 30)
|
||||
|
||||
|
||||
def get_income_val(obj, key):
|
||||
if key in obj:
|
||||
amount = obj[key].get_only_position().units
|
||||
@@ -31,6 +34,7 @@ def get_income_val(obj, key):
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def get_expense_val(obj, key):
|
||||
if key in obj:
|
||||
amount = obj[key].get_only_position().units
|
||||
@@ -38,6 +42,7 @@ def get_expense_val(obj, key):
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def print_expenses_table(expenses):
|
||||
table = []
|
||||
for key, expense in expenses.items():
|
||||
@@ -45,6 +50,7 @@ def print_expenses_table(expenses):
|
||||
table.append([parts[1], get_expense_val(expenses, key)])
|
||||
print(tabulate(table))
|
||||
|
||||
|
||||
def get_total_inflows(income):
|
||||
sum = 0
|
||||
for account, balance in income.items():
|
||||
@@ -55,6 +61,7 @@ def get_total_inflows(income):
|
||||
else:
|
||||
return Amount(Decimal(0), "EUR")
|
||||
|
||||
|
||||
def get_total_outflows(expenses, total_investments):
|
||||
sum = 0
|
||||
for account, balance in expenses.items():
|
||||
@@ -65,43 +72,56 @@ def get_total_outflows(expenses, total_investments):
|
||||
else:
|
||||
return total_investments
|
||||
|
||||
|
||||
def get_total_investments(investments):
|
||||
sum = Amount(Decimal(0), "EUR")
|
||||
for inv in investments:
|
||||
sum = inv.cost_position if sum == Amount(Decimal(0), "EUR") else add(sum, inv.cost_position)
|
||||
sum = inv.cost_position if sum == Amount(
|
||||
Decimal(0), "EUR") else add(sum, inv.cost_position)
|
||||
if sum != 0 and sum != None:
|
||||
return Amount(Decimal(round(sum.number, 2)), sum.currency)
|
||||
else:
|
||||
return Amount(Decimal(0), "EUR")
|
||||
|
||||
|
||||
def print_report(start_date, period, income, expenses, investments):
|
||||
print(f"{bcolors.BOLD}Cash Flow Statement (period={period}, start_date={start_date}){bcolors.ENDC}")
|
||||
print(f"{bcolors.BOLD}Cash Flow Statement (period={
|
||||
period}, start_date={start_date}){bcolors.ENDC}")
|
||||
draw_line()
|
||||
print(f"{bcolors.BOLD}Inflows{bcolors.ENDC}")
|
||||
print(f"\t{bcolors.BOLD}Income{bcolors.ENDC}")
|
||||
print(tabulate([
|
||||
["Salari", get_income_val(income, "Income:Work:Zurich:Salari")],
|
||||
["Tickets Restaurant", get_income_val(income,"Income:Work:Zurich:TicketsRestaurant")],
|
||||
["Targeta Transport", get_income_val(income,"Income:Work:Zurich:TargetaTransport")],
|
||||
["Pla pensions Empleats Zurich", get_income_val(income,"Income:Work:Zurich:DZP")],
|
||||
["Seguro Mèdic", get_income_val(income,"Income:Work:Zurich:SeguroMedic")],
|
||||
["Tickets Restaurant", get_income_val(
|
||||
income, "Income:Work:Zurich:TicketsRestaurant")],
|
||||
["Targeta Transport", get_income_val(
|
||||
income, "Income:Work:Zurich:TargetaTransport")],
|
||||
["Pla pensions Empleats Zurich", get_income_val(
|
||||
income, "Income:Work:Zurich:DZP")],
|
||||
["Seguro Mèdic", get_income_val(
|
||||
income, "Income:Work:Zurich:SeguroMedic")],
|
||||
["Gimnàs", get_income_val(income, "Income:Work:Zurich:Gimnas")]
|
||||
]))
|
||||
print(f"\t{bcolors.BOLD}Income from Investment{bcolors.ENDC}")
|
||||
print(tabulate([
|
||||
["Capital Gains", get_income_val(income,"Income:Invest:R4:CapitalGains")],
|
||||
["Untaxable Capital Gains", get_income_val(income,"Income:Invest:R4:CapitalGains:Untaxable")],
|
||||
["Capital Gains", get_income_val(
|
||||
income, "Income:Invest:R4:CapitalGains")],
|
||||
["Untaxable Capital Gains", get_income_val(
|
||||
income, "Income:Invest:R4:CapitalGains:Untaxable")],
|
||||
["Dividends", get_income_val(income, "Income:Invest:R4:Dividends")],
|
||||
["Rentabilitat Estalvis", get_income_val(income,"Income:Savings:Caixabank:RentabilitatEstalvis")]
|
||||
["Rentabilitat Estalvis", get_income_val(
|
||||
income, "Income:Savings:Caixabank:RentabilitatEstalvis")]
|
||||
]))
|
||||
print(f"\t{bcolors.BOLD}Other Inflows{bcolors.ENDC}")
|
||||
print(tabulate([
|
||||
["Transferències", get_income_val(income,"Income:Other:Caixabank:Transferencia")],
|
||||
["Transferències", get_income_val(
|
||||
income, "Income:Other:Caixabank:Transferencia")],
|
||||
["Bizum", get_income_val(income, "Income:Other:Caixabank:Bizum")],
|
||||
["Devolucions", get_income_val(income, "Income:Other:Devolucions")]
|
||||
]))
|
||||
print(tabulate([
|
||||
["Total Inflows", f"{bcolors.BOLD}{get_total_inflows(income).to_string()}{bcolors.ENDC}"]
|
||||
["Total Inflows", f"{bcolors.BOLD}{get_total_inflows(income).to_string()}{
|
||||
bcolors.ENDC}"]
|
||||
]))
|
||||
draw_line()
|
||||
print(f"{bcolors.BOLD}Outflows{bcolors.ENDC}")
|
||||
@@ -112,44 +132,57 @@ def print_report(start_date, period, income, expenses, investments):
|
||||
["Compra de fons i accions", total_investments.to_string()]
|
||||
]))
|
||||
print(tabulate([
|
||||
["Total Outflows", f"{bcolors.BOLD}{get_total_outflows(expenses, total_investments).to_string()}{bcolors.ENDC}"]
|
||||
["Total Outflows", f"{bcolors.BOLD}{get_total_outflows(
|
||||
expenses, total_investments).to_string()}{bcolors.ENDC}"]
|
||||
]))
|
||||
draw_line()
|
||||
net_cash_flow = sub(get_total_inflows(income), get_total_outflows(expenses, total_investments))
|
||||
net_cash_flow = sub(get_total_inflows(income),
|
||||
get_total_outflows(expenses, total_investments))
|
||||
print(tabulate([
|
||||
["NET CASH FLOW", f"{bcolors.BOLD}{bcolors.OKGREEN if net_cash_flow.number >= 0 else bcolors.FAIL}{net_cash_flow.to_string()}{bcolors.ENDC}"]
|
||||
["NET CASH FLOW", f"{bcolors.BOLD}{bcolors.OKGREEN if net_cash_flow.number >= 0 else bcolors.FAIL}{
|
||||
net_cash_flow.to_string()}{bcolors.ENDC}"]
|
||||
]))
|
||||
|
||||
|
||||
def get_income(entries, options, period, start_date):
|
||||
period_delta = relativedelta(months=1) if period == "monthly" else relativedelta(years=1)
|
||||
period_delta = relativedelta(
|
||||
months=1) if period == "monthly" else relativedelta(years=1)
|
||||
end_date = date.fromisoformat(start_date) + period_delta
|
||||
income_query = f"SELECT account, convert(sum(position), \"EUR\") as sum_position FROM OPEN ON {start_date} CLOSE ON {end_date.isoformat()} WHERE account ~ \"Income\""
|
||||
income_query = f"SELECT account, convert(sum(position), \"EUR\") as sum_position FROM OPEN ON {
|
||||
start_date} CLOSE ON {end_date.isoformat()} WHERE account ~ \"Income\""
|
||||
rtypes, rrows = query.run_query(
|
||||
entries, options, income_query)
|
||||
income = {}
|
||||
for row in rrows:
|
||||
income[row.account] = row.sum_position
|
||||
income[row[0]] = row[1]
|
||||
return income
|
||||
|
||||
|
||||
def get_expenses(entries, options, period, start_date):
|
||||
period_delta = relativedelta(months=1) if period == "monthly" else relativedelta(years=1)
|
||||
period_delta = relativedelta(
|
||||
months=1) if period == "monthly" else relativedelta(years=1)
|
||||
end_date = date.fromisoformat(start_date) + period_delta
|
||||
expenses_query = f"SELECT account, convert(sum(position), \"EUR\") as sum_position FROM OPEN ON {start_date} CLOSE ON {end_date.isoformat()} WHERE account ~ \"Expenses\""
|
||||
expenses_query = f"SELECT account, convert(sum(position), \"EUR\") as sum_position FROM OPEN ON {
|
||||
start_date} CLOSE ON {end_date.isoformat()} WHERE account ~ \"Expenses\""
|
||||
rtypes, rrows = query.run_query(
|
||||
entries, options, expenses_query)
|
||||
expenses = {}
|
||||
for row in rrows:
|
||||
expenses[row.account] = row.sum_position
|
||||
expenses[row[0]] = row[1]
|
||||
return expenses
|
||||
|
||||
|
||||
def get_investments(entries, options, period, start_date):
|
||||
period_delta = relativedelta(months=1) if period == "monthly" else relativedelta(years=1)
|
||||
period_delta = relativedelta(
|
||||
months=1) if period == "monthly" else relativedelta(years=1)
|
||||
end_date = date.fromisoformat(start_date) + period_delta
|
||||
expenses_query = f"SELECT account, convert(cost(position), \"EUR\") as cost_position, currency, date WHERE account ~ \"Assets:Invest:R4:\" AND NOT currency ~ '^(EUR|USD)' AND date >= {start_date} AND date < {end_date.isoformat()}"
|
||||
expenses_query = f"SELECT account, convert(cost(position), \"EUR\") as cost_position, currency, date WHERE account ~ \"Assets:Invest:R4:\" AND NOT currency ~ '^(EUR|USD)' AND date >= {
|
||||
start_date} AND date < {end_date.isoformat()}"
|
||||
rtypes, rrows = query.run_query(
|
||||
entries, options, expenses_query)
|
||||
return rrows
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Generate cash flow report')
|
||||
parser.add_argument('start_date', metavar='start_date', type=str, nargs=1,
|
||||
@@ -172,4 +205,5 @@ def main():
|
||||
investments = get_investments(entries, options, period, start_date)
|
||||
print_report(start_date, period, income, expenses, investments)
|
||||
|
||||
|
||||
main()
|
||||
@@ -6,7 +6,6 @@ readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"babel==2.13.1",
|
||||
"beancount==2.3.6",
|
||||
"beautifulsoup4==4.12.3",
|
||||
"blinker==1.7.0",
|
||||
"bottle==0.12.25",
|
||||
|
||||
Reference in New Issue
Block a user