From b037f9960718351b0470ab7ea67271b291bebb0e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Thu, 22 Nov 2018 17:14:34 +0100 Subject: [PATCH] functions for pdf export --- invoices.sh | 32 ++++--- odtgen.sh | 239 ++++++++++++++++++++++++++++++++++++++++++++++++++++ senders.sh | 33 +++++--- 3 files changed, 284 insertions(+), 20 deletions(-) create mode 100755 odtgen.sh diff --git a/invoices.sh b/invoices.sh index 9e07e9f..b856849 100755 --- a/invoices.sh +++ b/invoices.sh @@ -1,9 +1,10 @@ #!/bin/sh sender_list(){ - local select="$1" n name + local select="$1" n name address iban bic [ -d senders/ ] && for n in '' senders/*; do - [ "$n" ] && name="$(sed q "$n" |HTML)" + [ "$n" ] && read -r address iban bic <"$n" + name="$(UNSTRING "${address#address=}" |sed q |HTML)" [ "${n#senders/}" = "$select" ] \ && printf '' "${n#senders/}" "$name" \ || printf '' "${n#senders/}" "$name" @@ -22,16 +23,20 @@ client_list(){ } list_invoices(){ + local sender client date number vat vatrate iban bic hourly [ -d invoices/ ] && for i in invoices/*; do read -r sender client date number vat vatrate<<-EOF $(sed q "$i") EOF - [ -f "senders/${sender#sender=}" ] \ - && sender="$(sed q "senders/${sender#sender=}")" \ - || sender="(unset)" + + [ ! -f "senders/${sender#sender=}" ] \ + && sender="(unset)" \ + || read -r sender iban bic <"senders/${sender#sender=}" + [ ! -f "clients/${client#client=}" ] \ && client="(unset)" \ || read -r client hourly <"clients/${client#client=}" + [ "${date#date=}" -ge 0 ] 2>&- \ && date="$(date -d "@${date#date=}" +%x)" \ || date="(unset)" @@ -43,7 +48,7 @@ list_invoices(){ [label Amount:] %.2f € [a href="/invoices/%s" Edit] ]' "$(UNSTRING "${number#number=}" |HTML)" \ - "$(HTML "$sender")" \ + "$(UNSTRING "${sender#address=}" |sed q |HTML)" \ "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \ "$(invoice_total "${i#invoices/}")" \ "$(HTML ${i#invoices/})" @@ -72,6 +77,7 @@ edit_invoice(){ && read -r caddress hourly <"clients/${client#client=}" hourly="${hourly#hourly=}" + tid="$(tid "invoices/$id")" total=$(invoice_total "$id") cat <<-EOF @@ -137,7 +143,8 @@ $({ sed 1d "invoices/$id"; printf 'time= work= hours=\n'; } \ esac) ]] ] - [submit "update" "$(tid "invoices/$id")" Update] + [submit "genpdf" "$tid" Export PDF] + [submit "update" "$tid" Update] ] EOF } @@ -158,7 +165,7 @@ invoice_total(){ || hourly=0 sed 1d "invoices/$id" \ - | { while read time work hours; do + | { while read -r time work hours; do [ "${hours#hours=}" -gt 0 ] 2>&- \ && hours="${hours#hours=}" \ || hours=0 @@ -172,9 +179,10 @@ invoice_total(){ } update_invoice(){ - local id="$(POST id |checkid)" extra=0 + local id="$(POST id |checkid)" extra=0 tid + tid="$(tid invoices/$id)" - if [ "$(POST update)" = "$(tid "invoices/$id")" ]; then + if [ "$(POST update)" = "$tid" ] || [ "$(POST genpdf)" = "$tid" ]; then mkdir -p invoices for n in "$(POST_COUNT time)" "$(POST_COUNT work)" "$(POST_COUNT hours)"; do @@ -195,5 +203,9 @@ update_invoice(){ done } >"invoices/$id" fi + if [ "$(POST genpdf)" ]; then + . $_EXEC/odtgen.sh + genodt "$id" + fi REDIRECT "/invoices/$id" } diff --git a/odtgen.sh b/odtgen.sh new file mode 100755 index 0000000..dbe2945 --- /dev/null +++ b/odtgen.sh @@ -0,0 +1,239 @@ +#!/bin/zsh + +odt_manifest(){ + cat <<-EOF + + + + + + + EOF +} + +odt_stylesheet(){ + local iban="$2" bic="$3" + + cat <<-EOF + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Bankverbindung - IBAN: + ${iban} | + BIC: + ${bic} + + + + + + EOF +} + +odt_content(){ + local id="$1" sender="$2" client="$3" date="$4" number="$5" vat="$6" vatrate="$7" hourly="$8" + cat <<-EOF + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $(date -d @${date} +%d.%m.%Y) + + + + $(HTML "$sender" |sed 'a') + + + + $(HTML "$client" |sed 'a') + + Rechnung ${issuance} + + + + Vergütung: ${hourly} € / Std.$(case $vat in + (smallbusiness) + printf 'Gemäß Kleinunternehmerregelung (§19 Umsatzsteuergesetz) wird keine Umsatzsteuer berechnet.';; + (nett) printf 'zuzüglich %i%% MWSt.' "$vatrate";; + (gross) printf '(einschließlich %i\% MWSt.)' "$vatrate";; + esac) + + + + + + + + + + Datum + + + Art der Leistung + + + Stundenzahl + + + Betrag + + + + $(sed 1d "invoices/$id" |while read -r time work hours; do + time="$(UNSTRING "${time#time=}")" + work="$(UNSTRING "${work#work=}")" + hours="$(UNSTRING "${hours#hours=}")" + cat <<-TROW + + + ${time} + + + ${item} + + + ${hours} + + + $((${hourly} * ${hours})) € + + + TROW + done) + + + + + + + + + Summe: + + + $(invoice_total "$id") € + + + + + + + EOF +} + +genodt(){ + local id="$1" exdir="export/$1" + [ -d "$exdir" ] && rm "$exdir/META-INF" + + read -r sender client date number vat vatrate <<-EOF + $(sed q "invoices/$id") + EOF + read -r sender iban bic <"senders/${sender#sender=}" + read -r client hourly <"clients/${client#client=}" + + mkdir -p "$exdir/META-INF" + odt_manifest >"$exdir/META-INF/manifest.xml" + odt_stylesheet "$(UNSTRING "${iban#iban=}")" "$(UNSTRING "${bic#bic=}")" >"$exdir/styles.xml" + odt_content "$id" \ + "$(UNSTRING "${sender#address=}")" \ + "$(UNSTRING "${client#address=}")" \ + "${date#date=}" \ + "$(UNSTRING "${number#number=}")" \ + "${vat#vat=}" "${vatrate#vatrate=}" \ + "${hourly#hourly=}" \ + >"$exdir/content.xml" + + [ -f "export/${id}.odt" ] && rm "export/${id}.odt" + ( cd "$exdir" + zip "../${id}.odt" content.xml styles.xml META-INF/manifest.xml + ) + rm "$exdir/content.xml" "$exdir/styles.xml" "$exdir/META-INF/manifest.xml" + rmdir "$exdir/META-INF/" "$exdir/" + # lowriter --headless --convert-to pdf "${odtdoc}" +} diff --git a/senders.sh b/senders.sh index 4d1f612..8c98737 100755 --- a/senders.sh +++ b/senders.sh @@ -1,11 +1,16 @@ #!/bin/sh edit_sender(){ - id="$1" - if [ -f "senders/$id" ]; then - address="$(cat "senders/$id")" - fi - [ "$address" ] || address="Name + local id="$1" address iban bic + [ -f "senders/$id" ] \ + && read -r address iban bic <"senders/$id" + + iban="$(UNSTRING "${iban#iban=}")" + bic="$(UNSTRING "${bic#bic=}")" + + [ "${address#address=}" ] \ + && address="$(UNSTRING "${address#address=}")" \ + || address="Name Street City @@ -19,17 +24,23 @@ xxx / 000 / ### [form method="POST" action="/update_sender" [hidden "id" "%s"] + [label IBAN:][input name=iban value="%s" placeholder=IBAN] + [label BIC:][input name=bic value="%s" placeholder=BIC] [submit "update" "%s" Update] ]' \ - "$(HTML $id)" \ - "$(HTML "${address}")" \ + "$(HTML $id)" "$(HTML "${address}")" \ + "$(HTML "${iban}")" "$(HTML "${bic}")" \ "$(tid "senders/$id")" } list_senders(){ [ -d senders/ ] && for s in senders/*; do - address=$(cat "$s") - [ "$address" ] || address="(no address)" + read -r address iban bic <"$s" + + [ "${address#address=}" ] \ + && address="$(UNSTRING "${address#address=}")" \ + || address="(no address)" + printf '[div .sender .address %s[a href="/senders/%s" Edit]] ' "$(HTML "$address")" "$(HTML "${s#senders/}")" @@ -40,7 +51,9 @@ update_sender(){ id="$(POST id |checkid)" if [ "$(POST update)" = "$(tid "senders/$id")" ]; then mkdir -p senders - POST address >"senders/$id" + printf 'address=%s iban=%s bic=%s\n' \ + $(POST address |STRING) $(POST iban |STRING) \ + $(POST bic |STRING) >"senders/$id" REDIRECT "/senders/" else REDIRECT "/senders/$id" -- 2.39.2