hv2-all-in-one/cli/AccountStatement.py

109 lines
3.8 KiB
Python

from db import dbGetMany, dbGetOne
from loguru import logger
import datetime
import iso8601
from utils import getParam
from Cheetah.Template import Template
def perform(dbh, params):
year = getParam(params, 'year', datetime.datetime.today().year)
accountEntries = dbGetMany(
dbh,
{
"statement": "SELECT * FROM account_statement_v WHERE fiscal_year = %(year)s",
"params": {
'year': year
}
}
)
overview = dbGetMany(
dbh,
{
"statement": "select sum(amount), category from account_statement_v where fiscal_year = %(year)s group by category",
"params": {
'year': year
}
}
)
sum_related = dbGetOne(
dbh,
{
"statement": "select coalesce(sum(amount), 0::numeric(10,2)) as sum from account_statement_v where fiscal_year = %(year)s and category != 'nicht abrechenbare Positionen'",
"params": {
'year': year
}
}
)
sum_unrelated = dbGetOne(
dbh,
{
"statement": "select coalesce(sum(amount), 0::numeric(10,2)) as sum from account_statement_v where fiscal_year = %(year)s and category = 'nicht abrechenbare Positionen'",
"params": {
'year': year
}
}
)
sum_total = dbGetOne(
dbh,
{
"statement": "select sum(amount) as sum from account_statement_v where fiscal_year = %(year)s",
"params": {
'year': year
}
}
)
sum_error = dbGetMany(
dbh,
{
"statement": """
select coalesce(sum(amount), 0::numeric(10,2)) as sum, 1 as error, 'booked in %(year1)s, accounted in %(year0)s' as remark from account_statement_v where fiscal_year = %(year0)s and extract(year from created_at) = %(year1)s
union
select coalesce(sum(amount), 0::numeric(10,2)) as sum, 2 as error, 'booked in %(year0)s, accounted in %(year1)s' as remark from account_statement_v where fiscal_year = %(year1)s and extract(year from created_at) = %(year0)s
union
select coalesce(sum(amount), 0::numeric(10,2)) as sum, 3 as error, 'booked in %(year2)s, accounted in %(year1)s' as remark from account_statement_v where fiscal_year = %(year1)s and extract(year from created_at) = %(year2)s
union
select coalesce(sum(amount), 0::numeric(10,2)) as sum, 4 as error, 'booked in %(year1)s, accounted in %(year2)s' as remark from account_statement_v where fiscal_year = %(year2)s and extract(year from created_at) = %(year1)s
order by error
""",
"params": {
'year0': year - 1,
'year1': year,
'year2': year + 1
}
}
)
count_entries = dbGetMany(
dbh,
{
"statement": "select extract(month from created_at) as month, count(*) as count from account_statement_v where extract(year from created_at) = %(year)s group by month",
"params": {
"year": year
}
}
)
raw_total = sum_total['sum']
logger.info(f"{raw_total=}")
err1 = sum_error[0]['sum']
logger.info(f"{err1=}")
err2 = sum_error[1]['sum']
logger.info(f"{err2=}")
err3 = sum_error[2]['sum']
logger.info(f"{err3=}")
err4 = sum_error[3]['sum']
logger.info(f"{err4=}")
adjusted_total = raw_total + err1 - err2 - err3 + err4
logger.info(f"{adjusted_total=}")
template = getParam(params, 'template', 'accountStatement.tmpl')
input = { 'year': year, 'entries': accountEntries, 'overview': overview, 'related': sum_related, 'unrelated': sum_unrelated, 'total': sum_total, 'errors': sum_error, 'adjusted_total' :adjusted_total, 'count_entries': count_entries }
tmpl = Template(file=template, searchList=[ input ])
print(tmpl)