tax-reports 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/sh
  2. BEAN_FILE=${BEAN_FILE:-~/Ledger/main.beancount}
  3. if [ $# -lt 1 ] ; then
  4. echo "$(basename $0) YEAR" >&2
  5. exit 1
  6. fi
  7. YEAR=$1
  8. FN="report-${YEAR}.tex"
  9. DIVIDENDS_QUERY=$(printf 'select account, sum(position) as total_dividends where account ~ "Income:.*:Dividends" and year = %s group by account' $YEAR)
  10. CAPITAL_GAINS_QUERY=$(printf 'select account, sum(position) as total_cap_gains where account ~ "Income:.*:CapGains" and year = %s group by account' $YEAR)
  11. OTHER_INCOME_QUERY=$(printf 'select account, sum(position) as total_income where account ~ "Income:.*" and not account ~ "Dividends" and not account ~ "CapGains" and not account ~ "Unrealized" and year = %s group by account' $YEAR)
  12. cat <<EOF >"${FN}"
  13. \documentclass[paper=letter,DIV=14]{scrartcl}
  14. \usepackage{booktabs,amsmath,dcolumn}
  15. \newcolumntype{d}[1]{D{.}{\thinspace\mathbf{.}\thinspace}{#1} }
  16. \title{Tax-related Reports for $YEAR}
  17. \author{}
  18. \date{}
  19. \begin{document}
  20. \maketitle
  21. \tableofcontents
  22. \newpage
  23. EOF
  24. cat <<EOF >>"${FN}"
  25. \addsec{Income: General}
  26. \begin{tabular}{ld{5}}
  27. \toprule
  28. \textbf{Type} & \multicolumn{1}{r}{\textbf{Amount (USD)}} \\\\
  29. \midrule
  30. EOF
  31. bean-query -f csv ${BEAN_FILE} "${OTHER_INCOME_QUERY}" | \
  32. awk 'NR>1' | \
  33. sed -e 's/\r//' | \
  34. sed -e 's/ USD//' | \
  35. sed -e 's/Income://' | \
  36. awk --field-separator=, '{printf "%s & %s\n", $1, $2}' | \
  37. awk -e '{print $0"\\\\"}'>> "${FN}"
  38. cat <<EOF >>"${FN}"
  39. \bottomrule
  40. \end{tabular}
  41. EOF
  42. cat <<EOF >>"${FN}"
  43. \addsec{Income: Dividends}
  44. \begin{tabular}{ld{5}}
  45. \toprule
  46. \textbf{Type} & \multicolumn{1}{r}{\textbf{Amount (USD)}} \\\\
  47. \midrule
  48. EOF
  49. bean-query -f csv ${BEAN_FILE} "${DIVIDENDS_QUERY}" | \
  50. awk 'NR>1' | \
  51. sed -e 's/\r//' | \
  52. sed -e 's/ USD//' | \
  53. sed -e 's/Income://' | \
  54. awk --field-separator=, '{printf "%s & %s\n", $1, $2}' | \
  55. awk -e '{print $0"\\\\"}'>> "${FN}"
  56. cat <<EOF >>"${FN}"
  57. \bottomrule
  58. \end{tabular}
  59. EOF
  60. cat <<EOF >>"${FN}"
  61. \addsec{Income: Capital Gains}
  62. \begin{tabular}{ld{5}}
  63. \toprule
  64. \textbf{Type} & \multicolumn{1}{r}{\textbf{Amount (USD)}} \\\\
  65. \midrule
  66. EOF
  67. bean-query -f csv ${BEAN_FILE} "${CAPITAL_GAINS_QUERY}" | \
  68. awk 'NR>1' | \
  69. sed -e 's/\r//' | \
  70. sed -e 's/ USD//' | \
  71. sed -e 's/Income://' | \
  72. awk --field-separator=, -e '{printf "%s & %s\n", $1, $2}' | \
  73. awk -e '{print $0"\\\\"}'>> "${FN}"
  74. cat <<EOF >>"${FN}"
  75. \bottomrule
  76. \end{tabular}
  77. EOF
  78. cat <<EOF >>"${FN}"
  79. \end{document}
  80. EOF
  81. latexmk -pdf "${FN}"
  82. latexmk -c "${FN}"