1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import datetime
from decimal import Decimal
from beancount import loader
from beancount.query import query
import calendar
import sys
import os
from dateutil.relativedelta import relativedelta
_template = r'''
\documentclass[UTF8]{ctexart}
\usepackage{multicol}
\usepackage{geometry}
\setlength\columnsep{1cm}
\geometry{a4paper,left=1.5cm,right=1.5cm,top=0cm,bottom=1.5cm}
\columnseprule=1pt
\title{Monthly Beancount Report}
\author{GitHub Actions}
\date{}
\begin{document}
\maketitle
\begin{center}\small
\textbf{\Large Statement of Comprehensive Income} \\
\date{ENV_YEAR-ENV_MONTH-01 to ENV_YEAR-ENV_MONTH-ENV_DAY}
\begin{multicols}{2}
\begin{center}
\textbf{Income Detail} \\
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
ACCOUNT & TOTAL(CNY) \\
\hline
ENV_LINES_INCOME
\end{tabular*}
\vfill\null
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
\hline
Total & ENV_TOTAL_INCOME \\
\end{tabular*}
\end{center}
\columnbreak
\begin{center}
\textbf{Expenses Detail} \\
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
ACCOUNT & TOTAL(CNY) \\
\hline
ENV_LINES_EXPENSES
\end{tabular*}
\vfill\null
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
\hline
Total & ENV_TOTAL_EXPENSES \\
\end{tabular*}
\end{center}
\end{multicols}
\begin{center}
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
\hline
Net Profit & ENV_NET_PROFIT \\
\end{tabular*}
\end{center}
\vfill\null
\textbf{\Large Statement of Financial Position} \\
\date{As of ENV_YEAR-ENV_MONTH-ENV_DAY}
\begin{multicols}{2}
\begin{center}
\textbf{Assets Detail} \\
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
ACCOUNT & TOTAL(CNY) \\
\hline
ENV_LINES_ASSETS
\end{tabular*}
\vfill\null
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
\hline
Total & ENV_TOTAL_ASSETS \\
\end{tabular*}
\end{center}
\columnbreak
\begin{center}
\textbf{Liabilities Detail} \\
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
ACCOUNT & TOTAL(CNY) \\
\hline
ENV_LINES_LIABILITIES
\end{tabular*}
\vfill\null
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
\hline
Total & ENV_TOTAL_LIABILITIES \\
\end{tabular*}
\end{center}
\end{multicols}
\begin{center}
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}lr@{}}
\hline
Debt Ratio & ENV_DEBT_RATIO \\
\end{tabular*}
\vfill\null
\end{center}
\end{center}
\end{document}
'''
class Buffer:
def __init__(self, value: str):
self.value = value
def replace(self, _old: str, _new: str):
self.value = self.value.replace(_old, _new)
def __str__(self):
return self.value
if __name__ == '__main__':
# load file
ledger_data, errors, options = loader.load_file(
os.path.join(os.getcwd(), sys.argv[1]))
# init buffer
_buffer = Buffer(_template)
# init time
lm = datetime.datetime.now() - relativedelta(months=1)
year = lm.strftime("%Y")
month = lm.strftime("%m")
ENV_YEAR = year
ENV_MONTH = month
year = int(year)
month = int(month)
day = calendar.monthrange(year, month)[1]
ENV_DAY = str(day)
# ENV_LINES_INCOME
sql_query = r'SELECT account, abs(sum(cost(position))) as total WHERE account ~ "^Income:*" and year = ' + \
ENV_YEAR + r' and month = ' + ENV_MONTH + \
r' GROUP BY month, account ORDER BY account, total DESC'
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_LINES_INCOME = ""
for rrow in rrows:
ENV_LINES_INCOME += rrow[0] + r' & ' + \
'{0:.2f}'.format(rrow[1]) + r' \\' + '\n'
# ENV_TOTAL_INCOME
sql_query = r'SELECT abs(sum(cost(position))) WHERE account ~ "^Income:*" and year = ' + \
ENV_YEAR + r' and month = ' + ENV_MONTH + r' GROUP BY month'
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_TOTAL_INCOME = '{0:.2f}'.format(rrows[0][0])
# ENV_LINES_EXPENSES
sql_query = r'SELECT account, sum(cost(position)) as total WHERE account ~ "^Expenses:*" and year = ' + \
ENV_YEAR + r' and month = ' + ENV_MONTH + \
r' GROUP BY month, account ORDER BY account, total DESC'
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_LINES_EXPENSES = ""
for rrow in rrows:
ENV_LINES_EXPENSES += rrow[0] + r' & ' + \
'{0:.2f}'.format(rrow[1]) + r' \\' + '\n'
# ENV_TOTAL_EXPENSES
sql_query = r'SELECT sum(cost(position)) WHERE account ~ "^Expenses:*" and year = ' + \
ENV_YEAR + r' and month = ' + ENV_MONTH + r' GROUP BY month'
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_TOTAL_EXPENSES = '{0:.2f}'.format(rrows[0][0])
# ENV_NET_PROFIT
sql_query = r'SELECT abs(sum(cost(position))) WHERE year = ' + ENV_YEAR + r' and month = ' + \
ENV_MONTH + \
r' and ( account ~ "^Expenses:*" or account ~ "^Income:*" ) GROUP BY month'
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_NET_PROFIT = '{0:.2f}'.format(rrows[0][0])
# ENV_LINES_ASSETS
sql_query = r'SELECT account, sum(cost(position)) as total WHERE account ~ "^Assets:*" and year <= ' + \
ENV_YEAR + r' and month <= ' + ENV_MONTH + r' and day <= ' + \
ENV_DAY+r' ORDER BY account, total DESC'
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_LINES_ASSETS = ""
for rrow in rrows:
ENV_LINES_ASSETS += rrow[0] + r' & ' + \
'{0:.2f}'.format(rrow[1]) + r' \\' + '\n'
# ENV_TOTAL_ASSETS
sql_query = r'SELECT sum(cost(position)) as total WHERE account ~ "^Assets:*" and year <= ' + \
ENV_YEAR + r' and month <= ' + ENV_MONTH + r' and day <= '+ENV_DAY
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_TOTAL_ASSETS = '{0:.2f}'.format(rrows[0][0])
# ENV_LINES_LIABILITIES
sql_query = r'SELECT account, abs(sum(cost(position))) as total WHERE account ~ "^Liabilities:*" and year <= ' + \
ENV_YEAR + r' and month <= ' + ENV_MONTH + r' and day <= ' + \
ENV_DAY+r' ORDER BY account, total DESC'
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_LINES_LIABILITIES = ""
for rrow in rrows:
ENV_LINES_LIABILITIES += rrow[0] + r' & ' + \
'{0:.2f}'.format(rrow[1]) + r' \\' + '\n'
# ENV_TOTAL_LIABILITIES
sql_query = r'SELECT abs(sum(cost(position))) as total WHERE account ~ "^Liabilities:*" and year <= ' + \
ENV_YEAR + r' and month <= ' + ENV_MONTH + r' and day <= '+ENV_DAY
rrows = query.run_query(ledger_data, options, sql_query, numberify=True)[1]
ENV_TOTAL_LIABILITIES = '{0:.2f}'.format(rrows[0][0])
# ENV_DEBT_RATIO
ENV_DEBT_RATIO = "{:.2%}".format(
Decimal(ENV_TOTAL_LIABILITIES)/Decimal(ENV_TOTAL_ASSETS)).replace(r'%', r'\%')
_buffer.replace(r"ENV_YEAR", ENV_YEAR)
_buffer.replace(r"ENV_MONTH", ENV_MONTH)
_buffer.replace(r"ENV_DAY", ENV_DAY)
_buffer.replace(r"ENV_LINES_INCOME", ENV_LINES_INCOME)
_buffer.replace(r"ENV_TOTAL_INCOME", ENV_TOTAL_INCOME)
_buffer.replace(r"ENV_LINES_EXPENSES", ENV_LINES_EXPENSES)
_buffer.replace(r"ENV_TOTAL_EXPENSES", ENV_TOTAL_EXPENSES)
_buffer.replace(r"ENV_NET_PROFIT", ENV_NET_PROFIT)
_buffer.replace(r"ENV_LINES_ASSETS", ENV_LINES_ASSETS)
_buffer.replace(r"ENV_TOTAL_ASSETS", ENV_TOTAL_ASSETS)
_buffer.replace(r"ENV_LINES_LIABILITIES", ENV_LINES_LIABILITIES)
_buffer.replace(r"ENV_TOTAL_LIABILITIES", ENV_TOTAL_LIABILITIES)
_buffer.replace(r"ENV_DEBT_RATIO", ENV_DEBT_RATIO)
print(_buffer)
|