diff options
Diffstat (limited to 'bin/deklarera.sh')
-rwxr-xr-x | bin/deklarera.sh | 411 |
1 files changed, 411 insertions, 0 deletions
diff --git a/bin/deklarera.sh b/bin/deklarera.sh new file mode 100755 index 0000000..e17672f --- /dev/null +++ b/bin/deklarera.sh @@ -0,0 +1,411 @@ +#!/bin/bash -eu +# +# Require bash due to readline version of `read`. Also used for lower casing. +# +# Debian dependencies: (and likely others) +# datefudge evince inkscape libpdf-reuse-perl libgd-svg-perl mupdf pdf2svg + +YEAR="$(datefudge '6 months ago' date +%Y)" +SKIP=0 +CLEAR='no' + +usage() { + echo "${0} [--help] [--skip=<integer>]" + exit 1 +} + +show_help() { + echo '(y)es Run the command. Advances to the next on success.' + echo '(n)o Skip the command.' + echo '(c)lear Clear the screen.' + echo '(r)epeat Run the command. Asks again, never advances.' + echo '(h)elp Show this help' + echo '(q)uit Exit script.' +} + +handle_arg() { + local arg="${1}" + local param="${arg%=*}" value="${arg#*=}" + local ret=0 + + if [ "${arg}" = "${param}" ]; then + value="${2}" + ret=1 + fi + + case "${param}" in + '--help' | '-h') + usage + exit 0 + ;; + '--skip' | '-s') + SKIP="${value}" + return "${ret}" + ;; + *) + echo "Unknown parameter: ${param}" >&2 + exit 1 + ;; + esac + + return 0 +} + +SHIFT_NEXT='' +for ARG in "${@}"; do + shift + [ ! "${SHIFT_NEXT}" ] || { SHIFT_NEXT=''; continue; } + + if [ "${ARG#-}" != "${ARG}" ]; then + handle_arg "${ARG}" "${1}" || SHIFT_NEXT='y' + continue + fi + set -- "$@" "${ARG}" +done + +do_cmd() { + local start end duration pipe tee_pid + + if eval "${CMD}"; then + DONE='yes' + else + FAILED='yes' + fi + if [ "${FAILED}" != 'no' ]; then + echo 'Command failed.' >&2 + fi + [ "${CLEAR}" = 'no' ] || clear +} + +input() { + local file="${1}" var="${2}" prompt="${3}" value=${4:-} + local exists input tmp_file=$( mktemp ) + + if grep -q "^${var}" "${file}" 2>/dev/null; then + exists='y' + else + exists='' + fi + + if [ "${exists}" ]; then + if [ -e "${file}" ]; then + value=$( sed -n "s/^${var}: //p" <"${file}" ) + else + value='' + fi + fi + read -p "${prompt}" -i "${value}" -e input + + if [ "${exists}" ]; then + sed "s/^${var}: .*/${var}: ${input}/" <"${file}" >"${tmp_file}" + elif [ -e "${file}" ]; then + ( cat "${file}" 2>/dev/null; echo "${var}: ${input}" ) >>"${tmp_file}" + elif [ "${value}" ]; then + echo "${var}: ${input}" >>"${tmp_file}" + fi + mv "${tmp_file}" "${file}" +} + +############################################################################### + +copy_template() { + # Skatteverket used to publish 215003.pdf, a regular editable pdf. Now they + # seem to have fully gone for xfa forms. :( + # (https://unix.stackexchange.com/questions/265845/xfa-forms) + cp --recursive 'mall/0001-forenklad-arsbokslut/' "${YEAR}" +} + +mata_in_arsbokslut() { + local values="${YEAR}/0001-forenklad-arsbokslut/values.txt" + local b9 b10 b14 r1 r5 r11 + + input "${values}" 'B9' 'B9 Kassa och bank: ' + input "${values}" 'B10' 'B10 Eget kapital: ' + input "${values}" 'B14' 'B14 Skatteskulder: ' + input "${values}" 'R1' 'R1 Försäljning och utfört arbete…: ' + input "${values}" 'R5' 'R5 Varor, material och tjänster: ' + input "${values}" 'R11' 'R11 Bokfört resultat: ' + echo "${values}" + echo "${values}" | sed 's/./-/g' + cat "${values}" + + b9=$( sed -n 's/^B9: //p' <"${values}" ) + b10=$( sed -n 's/^B10: //p' <"${values}" ) + b14=$( sed -n 's/^B14: //p' <"${values}" ) + r1=$( sed -n 's/^R1: //p' <"${values}" ) + r5=$( sed -n 's/^R5: //p' <"${values}" ) + r11=$( sed -n 's/^R11: //p' <"${values}" ) + + if [ "$(( b10 + b14 ))" = "${b9}" ]; then + echo 'Balance seems to be ok.' + else + echo "Invalid numbers: B10 + B14 != B9. ($(( b10 + b14 )))" + return 1 + fi + if [ "$(( r1 - r5 ))" = "${r11}" ]; then + echo 'Result seems to be ok.' + else + echo "Invalid numbers: R1 - R5 != R11. ($(( r1 + r5 )))" + return 1 + fi +} + +mata_in_ne() { + local values="${YEAR}/0003-ne-enskild_näringsverksamhet/values.txt" + + # FIXME Get value från R43 of previous year. + input "${values}" 'R40' 'R40 Medgivna avdrag för egenavgifter…: ' + input "${values}" 'R41' 'R41 Påförda egenavgifter och…: ' + # FIXME Get these too from previous year. + input "${values}" 'Personnummer' 'Personnummer: ' + input "${values}" 'SNI-kategori' 'SNI-kategori: ' + echo 'https://www.verksamt.se/driva/skatter-och-avgifter/enskild-naringsverksamhet/preliminar-skatt-och-egenavgifter' + input "${values}" 'egenavgifter' 'egenavgifter: ' + + echo "${values}" + echo "${values}" | sed 's/./-/g' + cat "${values}" +} + +mata_in_k4() { + local values="${YEAR}/0004-k4/values.txt" + + input "${values}" 'Antal' 'Antal: ' + input "${values}" 'Beteckning' 'Beteckning: ' + input "${values}" 'Forsaljningspris' 'Försäljninspris: ' + input "${values}" 'Omkostnadsbelopp' 'Omkostnadsbelopp: ' + input "${values}" 'Vinst' 'Vinst: ' + + echo "${values}" + echo "${values}" | sed 's/./-/g' + cat "${values}" +} + +overfor_till_ink1() { + local ne="${1}" + local k4="${2}" + local values="${YEAR}/0005-ink1/values.txt" + local p7_4 p10_1 + + p7_4=$( sed -n 's/^Vinst: //p' <"${k4}" ) + p10_1=$( sed -n 's/^R47: //p' <"${ne}" ) + + input "${values}" 'p7_4' '7.4: ' "${p7_4}" + input "${values}" 'p10_1' '10.1: ' "${p10_1}" + input "${values}" 'email' 'E-postadress: ' + input "${values}" 'phone' 'Telefonnummer: ' + + echo "${values}" + echo "${values}" | sed 's/./-/g' + cat "${values}" +} + +overfor_till_moms() { + local bokslut="${1}" + local ink1="${2}" + local values="${YEAR}/0006-moms/values.txt" + local a05 g49 email phone + + a05=$( sed -n 's/^R1: //p' <"${bokslut}" ) + g49=$( sed -n 's/^B14: //p' <"${bokslut}" ) + email=$( sed -n 's/^email: //p' <"${ink1}" ) + phone=$( sed -n 's/^phone: //p' <"${ink1}" ) + + input "${values}" 'A05' 'A05 Momspliktig försäljning…: ' "${a05}" + input "${values}" 'B10' 'B10 Utgående moms 25%: ' + input "${values}" 'F48' 'F48 Ingående moms: ' + input "${values}" 'G49' 'G49 Moms att betala eller få tillbaka: ' + input "${values}" 'email' 'E-postadress: ' "${email}" + input "${values}" 'phone' 'Telefonnummer: ' "${phone}" + + echo "${values}" + echo "${values}" | sed 's/./-/g' + cat "${values}" +} + +ladda_ner_neblankett() { + echo "Please use your web browser to download SKV 2161" + echo 'https://skatteverket.se/funktioner/sok/sok.4.64a656d113f4c7597011b3.html?query=ne' + echo "Must be the 'Icke ifyllnadsbar pdf för utskrift' one." +} + +ladda_ner_k4blankett() { + echo "Please use your web browser to download SKV 2104" + echo 'https://skatteverket.se/funktioner/sok/sok.4.64a656d113f4c7597011b3.html?query=k4' + echo "Must be the 'Icke ifyllnadsbar pdf för utskrift' one." +} + +gor_neblankett_redigerbar() { + cd "${YEAR}/0003-ne-enskild_näringsverksamhet/" + pdf2svg 2161_*_20??-01-01?20??-12-31.pdf ne-%d.svg all + cd ../../ +} + +gor_k4blankett_redigerbar() { + cd "${YEAR}/0004-k4/" + pdf2svg 2104_*_web_20??*.pdf k4-%d.svg all + cd ../../ +} + +skriv_under() { +cat <<EOB + Remember + + #### ##### # #### #### ###### + # # # # # # # # # # # # + ## # ##### # ###### # ##### + # # # # # # # # # # # + #### # ###### # # #### ###### + + #### ### #### # # ## ##### # # ##### ###### + # # # # ## # # # # # # # # # + #### # # # # # # # # # # # # ##### + # # # ### # # # ###### # # # ##### # + # # # # # # ## # # # # # # # # + #### ### #### # # # # # #### # # ###### + + on every document! +EOB +} + +############################################################################### + +show_help +echo '' +COUNT=0 +for CMD in "mkdir '${YEAR}'" \ + 'copy_template' \ + \ + 'mata_in_arsbokslut' \ + "mupdf '$(( YEAR-1 ))/0001-forenklad-arsbokslut/215003-ifylld.pdf' &" \ + "evince '${YEAR}/0001-forenklad-arsbokslut/215003.pdf'" \ + "mupdf '${YEAR}/0001-forenklad-arsbokslut/215003-ifylld.pdf'" \ + "lp '${YEAR}/0001-forenklad-arsbokslut/215003-ifylld.pdf'" \ + \ + 'ladda_ner_neblankett' \ + "mkdir '${YEAR}/0003-ne-enskild_näringsverksamhet/'" \ + "mv ~/Downloads/2161_*_20??-01-01?20??-12-31.pdf \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/'" \ + 'gor_neblankett_redigerbar' \ + "cp '${YEAR}/0001-forenklad-arsbokslut/values.txt' \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/'" \ + 'mata_in_ne' \ + "( cd '${YEAR}/0003-ne-enskild_näringsverksamhet/'; \ + ../../bin/ne-ifyllning.pl 'values.txt' )" \ + "mupdf $(( YEAR-1 ))/0003-ne-enskild_näringsverksamhet/ne-framsida.pdf &" \ + "cp '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-1.svg' \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-framsida.svg'" \ + "inkscape '${YEAR}/0003-ne-enskild_näringsverksamhet/ne1-overlay.svg' &" \ + "inkscape '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-framsida.svg'" \ + "inkscape --export-filename \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-framsida.pdf' \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-framsida.svg'" \ + "mupdf $(( YEAR-1 ))/0003-ne-enskild_näringsverksamhet/ne-baksida.pdf &" \ + "cp '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-2.svg' \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-baksida.svg'" \ + "inkscape '${YEAR}/0003-ne-enskild_näringsverksamhet/ne2-overlay.svg' &" \ + "inkscape '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-baksida.svg'" \ + "inkscape --export-filename \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-baksida.pdf' \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/ne-baksida.svg'" \ + "(cd '${YEAR}/0003-ne-enskild_näringsverksamhet/'; \ + perl ../../bin/combine-ne.pl )" \ + "mupdf '${YEAR}/0003-ne-enskild_näringsverksamhet/ne.pdf' "\ + "lp '${YEAR}/0003-ne-enskild_näringsverksamhet/ne.pdf' "\ + \ + 'ladda_ner_k4blankett' \ + "mkdir '${YEAR}/0004-k4/'" \ + "mv ~/Downloads/2104_*.pdf \ + '${YEAR}/0004-k4/'" \ + 'gor_k4blankett_redigerbar' \ + "cp '${YEAR}/0003-ne-enskild_näringsverksamhet/values.txt' \ + '${YEAR}/0004-k4/'" \ + 'mata_in_k4' \ + "cp '${YEAR}/0004-k4/k4-1.svg' \ + '${YEAR}/0004-k4/k4-framsida.svg'" \ + "cp '${YEAR}/0004-k4/k4-2.svg' \ + '${YEAR}/0004-k4/k4-baksida.svg'" \ + "( cd '${YEAR}/0004-k4/'; \ + ../../bin/k4-ifyllning.pl 'values.txt' )" \ + "inkscape '${YEAR}/0004-k4/k4_1-overlay.svg' &" \ + "mupdf $(( YEAR-1 ))/0004-k4/k4-framsida.pdf &" \ + "inkscape '${YEAR}/0004-k4/k4-framsida.svg'" \ + "inkscape --export-filename \ + '${YEAR}/0004-k4/k4-framsida.pdf' \ + '${YEAR}/0004-k4/k4-framsida.svg'" \ + "mupdf $(( YEAR-1 ))/0004-k4/k4-baksida.pdf &" \ + "inkscape '${YEAR}/0004-k4/k4-baksida.svg'" \ + "inkscape --export-filename \ + '${YEAR}/0004-k4/k4-baksida.pdf' \ + '${YEAR}/0004-k4/k4-baksida.svg'" \ + "mupdf '${YEAR}/0004-k4/k4-framsida.pdf'" \ + "mupdf '${YEAR}/0004-k4/k4-baksida.pdf'" \ + "lp '${YEAR}/0004-k4/k4-framsida.pdf'" \ + "lp '${YEAR}/0004-k4/k4-baksida.pdf'" \ + \ + "mkdir '${YEAR}/0005-ink1/'" \ + "overfor_till_ink1 \ + '${YEAR}/0003-ne-enskild_näringsverksamhet/values.txt-out' \ + '${YEAR}/0004-k4/values.txt'" \ + "( cd '${YEAR}/0005-ink1/'; \ + ../../bin/ink1-ifyllning.pl 'values.txt' )" \ + "inkscape '${YEAR}/0005-ink1/ink1_1-overlay.svg'" \ + "inkscape --export-filename \ + '${YEAR}/0005-ink1/ink1-framsida.pdf' \ + '${YEAR}/0005-ink1/ink1_1-overlay.svg'" \ + "mupdf '${YEAR}/0005-ink1/ink1-framsida.pdf'" \ + "lp '${YEAR}/0005-ink1/ink1-framsida.pdf'" \ + "inkscape '${YEAR}/0005-ink1/ink1_2-overlay.svg'" \ + "inkscape --export-filename \ + '${YEAR}/0005-ink1/ink1-baksida.pdf' \ + '${YEAR}/0005-ink1/ink1_2-overlay.svg'" \ + "mupdf '${YEAR}/0005-ink1/ink1-baksida.pdf'" \ + "lp '${YEAR}/0005-ink1/ink1-baksida.pdf'" \ + "mkdir '${YEAR}/0006-moms/'" \ + "overfor_till_moms '${YEAR}/0001-forenklad-arsbokslut/values.txt' \ + '${YEAR}/0005-ink1/values.txt'" \ + "( cd '${YEAR}/0006-moms/'; \ + ../../bin/moms-ifyllning.pl 'values.txt' )" \ + "inkscape '${YEAR}/0006-moms/moms-overlay.svg'" \ + "inkscape --export-filename \ + '${YEAR}/0006-moms/mervärdesskattedeklaration.pdf' \ + '${YEAR}/0006-moms/moms-overlay.svg'" \ + "mupdf '${YEAR}/0006-moms/mervärdesskattedeklaration.pdf'" \ + "lp '${YEAR}/0006-moms/mervärdesskattedeklaration.pdf'" \ + 'skriv_under' +do + COUNT=$(( COUNT + 1 )) + [ "${COUNT}" -gt "${SKIP}" ] || continue + + DONE='no' + FAILED='no' + while [ "${DONE}" = 'no' ]; do + printf "About to execute (%s):\n'%s'\n" "${COUNT}" "${CMD}" + printf 'Should I? (Y/n/c/r/h/q) ' + read DUMMY + + case ${DUMMY,,} in + 'n' | 'no') + DONE='yes' + ;; + 'c' | 'clear') + clear + ;; + 'q' | 'quit') + exit 0 + ;; + 'y' | 'yes' | '') + do_cmd + ;; + 'r' | 'repeat') + do_cmd + DONE='no' + ;; + 'h' | 'help' | '') + show_help + ;; + *) + echo 'Invalid input.' >&2 ;; + esac + done +done |