]> git.plutz.net Git - invoices/commitdiff
Merge commit 'fe67294a5c9b198ad3dd383dcedf53fab0015e1c'
authorPaul Hänsch <paul@plutz.net>
Wed, 30 Sep 2020 08:34:12 +0000 (10:34 +0200)
committerPaul Hänsch <paul@plutz.net>
Wed, 30 Sep 2020 08:34:12 +0000 (10:34 +0200)
14 files changed:
.gitignore [new file with mode: 0644]
.gitmodules [new file with mode: 0644]
cgilite/cgilite.sh [moved from cgilite.sh with 100% similarity]
cgilite/file.sh [moved from file.sh with 100% similarity]
cgilite/html-sh.sed [moved from html-sh.sed with 100% similarity]
cgilite/logging.sh [moved from logging.sh with 100% similarity]
cgilite/session.sh [moved from session.sh with 100% similarity]
cgilite/storage.sh [moved from storage.sh with 100% similarity]
clients.sh [new file with mode: 0755]
invoices.cgi [new file with mode: 0755]
invoices.css [new file with mode: 0755]
invoices.sh [new file with mode: 0755]
odtgen.sh [new file with mode: 0755]
senders.sh [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..be77cba
--- /dev/null
@@ -0,0 +1,6 @@
+clients/
+invoices/
+senders/
+export/
+.*.swp
+serverkey
diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..e69de29
similarity index 100%
rename from cgilite.sh
rename to cgilite/cgilite.sh
similarity index 100%
rename from file.sh
rename to cgilite/file.sh
similarity index 100%
rename from html-sh.sed
rename to cgilite/html-sh.sed
similarity index 100%
rename from logging.sh
rename to cgilite/logging.sh
similarity index 100%
rename from session.sh
rename to cgilite/session.sh
similarity index 100%
rename from storage.sh
rename to cgilite/storage.sh
diff --git a/clients.sh b/clients.sh
new file mode 100755 (executable)
index 0000000..052a0c8
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+edit_client(){
+  id="$1"
+  if [ -f "clients/$id" ]; then
+    read -r address hourly <"clients/$id"
+  fi
+  [ "$address" ] || address="address="
+  [  "$hourly" ] ||  hourly="hourly="
+  printf '
+    [form method="POST" action="/update_client"
+      [hidden "id" "%s"]
+      <textarea name="address" placeholder="address">
+%s</textarea>
+      [label for=hourly Hourly Rate:]
+      [input #hourly type=number name=hourly value="%s"]
+      [submit "update" "%s" Update]
+    ]' \
+    "$(HTML $id)" \
+    "$(UNSTRING "${address#address=}" |HTML)" \
+    "$(UNSTRING "${hourly#hourly=}" |grep -xE '[0-9]+')" \
+    "$(transid "clients/$id")"
+}
+
+list_clients(){
+  [ -d clients/ ] && for c in clients/*; do
+    read -r address hourly <"$c"
+    address="$(UNSTRING "${address#address=}")"
+    [ "$address" ] || address="(no address)"
+    printf '[div .client .address <!--
+      -->%s
+
+[label Hourly Rate:] %s€
+    [a href="/clients/%s" Edit]]
+    ' "$(HTML "$address")" \
+      "$(HTML "${hourly#hourly=}")" \
+      "$(HTML "${c#clients/}")"
+  done
+}
+
+update_client(){
+  local id="$(POST id |checkid)"
+  if [ "$(POST update)" = "$(transid "clients/$id")" ]; then
+    mkdir -p clients
+    printf 'address=%s hourly=%s' \
+      "$(POST address |STRING)" "$(POST hourly |STRING)" \
+      >"clients/$id"
+
+    [ -d .git ] && {
+      git add "clients/$id"
+      git commit -m 'Update client info for "'"$(POST address |sed 1q)"'"' -- "clients/$id"
+    } >/dev/null
+
+    REDIRECT "/clients/"
+  else
+    error Invalid Data "$id" "$(POST update)"
+    REDIRECT "/clients/$1"
+  fi
+}
diff --git a/invoices.cgi b/invoices.cgi
new file mode 100755 (executable)
index 0000000..674da89
--- /dev/null
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+_EXEC="$(realpath "${0%/*}")"
+. "$_EXEC/cgilite/cgilite.sh"
+. "$_EXEC/cgilite/logging.sh"
+. "$_EXEC/cgilite/storage.sh"
+. "$_EXEC/cgilite/session.sh"
+
+yield_page(){
+printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+"$_EXEC/cgilite/html-sh.sed" <<EOF
+[html [head
+  [meta name="viewport" content="width=device-width"]
+  [link rel="stylesheet" type="text/css" href="/invoices.css"]
+  [title Invoices]
+] [body class="$1"
+  [div #menu
+    [a "/invoices/" Invoices]
+    [a "/clients/" Clients]
+    [a "/senders/" Senders]
+  ]
+  $(cat)
+] ]
+EOF
+}
+
+info="$(PATH "${PATH_INFO#http://*/}")"
+
+which git && [ ! -d .git ] && {
+  git init
+  mkdir -p clients/ invoices/ senders/
+  printf 'export/
+serverkey' >.gitignore
+  git add clients/ invoices/ senders/ .gitignore
+  git commit -m 'initialisation of invoice repo'
+} >&2
+
+case $info in
+  /invoices.css)
+    . "$_EXEC/cgilite/file.sh"
+    FILE "$_EXEC/invoices.css"
+    ;;
+  /clients|/clients/)
+    . "$_EXEC/clients.sh"
+    { list_clients
+      printf '[a .new href="/clients/%s" New]' "$(timeid)"
+    } | yield_page clients
+    ;;
+  /clients/*)
+    . "$_EXEC/clients.sh"
+    edit_client "${info#/clients/}" |yield_page client
+    ;;
+  /update_client)
+    . "$_EXEC/clients.sh"
+    update_client
+    ;;
+  /senders|/senders/)
+    . "$_EXEC/senders.sh"
+    { list_senders
+      printf '[a .new href="/senders/%s" New]' "$(timeid)"
+    } | yield_page senders
+    ;;
+  /senders/*)
+    . "$_EXEC/senders.sh"
+    edit_sender "${info#/senders/}" |yield_page sender
+    ;;
+  /update_sender)
+    . "$_EXEC/senders.sh"
+    update_sender
+    ;;
+  /invoices|/invoices/)
+    . "$_EXEC/invoices.sh"
+    { printf '[a .new href="/invoices/%s" New]' "$(timeid)"
+      list_invoices
+    } | yield_page invoices
+    ;;
+  /invoices/*)
+    . "$_EXEC/invoices.sh"
+    edit_invoice "${info#/invoices/}" |yield_page invoice
+    ;;
+  /update_invoice)
+    . "$_EXEC/invoices.sh"
+    update_invoice
+    ;;
+  /export/*.pdf/*)
+    . "$_EXEC/cgilite/file.sh"
+    file="${info%/*}"
+    FILE "${file#/}" application/pdf
+    ;;
+  *) REDIRECT /invoices
+    ;;
+esac
diff --git a/invoices.css b/invoices.css
new file mode 100755 (executable)
index 0000000..592fb55
--- /dev/null
@@ -0,0 +1,220 @@
+* {
+  margin: 0; padding: 0;
+  color: inherit; background-color: inherit;
+  font-size: medium; font-weight: normal;
+  text-decoration: none;
+  box-sizing: border-box;
+}
+body {
+  color: black; background-color: white;
+}
+
+strong {font-weight: bold;}
+em {text-decoration: italic;}
+
+h1 {
+  display: block;
+  margin: 1em 1em 0 1em;
+  font-weight: bold;
+  border-bottom: 1px solid #08F;
+}
+
+/* ==================== MENU ==================== */
+
+#menu {
+  margin: 0; padding: .5em 1em;
+  border-bottom: 1px solid black;
+}
+#menu a {
+  background-color: #EEF;
+  border: 1px solid black;
+  margin: .5em 1em .5em 0;
+  padding: .125em .5em;
+}
+#menu a:hover {
+  background-color: #FFF;
+  border-color: #888;
+}
+
+
+/* ==================== ADDRESS LISTS ==================== */
+
+.invoices .invoice,
+.clients .address,
+.senders .address {
+  display: inline-block;
+  background-color: #EEE;
+  white-space: pre;
+  padding: 1em 1em .5em 1em;
+  margin: 1em 0 0 1em;
+  width: 20em;
+}
+.invoices .invoice {
+  white-space: normal;
+}
+.invoices .invoice h2{
+  display: block;
+  letter-spacing: .25em;
+  text-align: center;
+  margin-bottom: -.5em;
+  font-size: 1.125em;
+}
+.invoices .invoice label {
+  font-weight: bold;
+}
+.invoices .invoice label::before {
+  white-space: pre;
+  content: '\0A';
+}
+.invoices .invoice a,
+.clients .address a,
+.senders .address a,
+a.new {
+  display: block;
+  background-color: #DEF;
+  text-align: center;
+  margin: .5em 3em 0 3em;
+  padding: .125em 1em;
+  border: 1px solid #08F;
+}
+.invoices .invoice a:hover,
+.clients .address a:hover,
+.senders .address a:hover,
+a.new:hover {
+  border-color: #8EF;
+}
+a.new {
+  display: inline-block;
+  margin: 1em;
+}
+
+
+/* ==================== ADDRESS FORMS ==================== */
+
+form select,
+form textarea,
+form input {
+  padding: .125em .5em;
+  background-color: #FFF;
+  text-align: left;
+  font-size: 12pt;
+}
+form input[type=number] {
+  width: 5em;
+  text-align: right;
+}
+
+.invoice form,
+.client form,
+.sender form {
+  position: relative;
+  display: inline-block;
+  margin: 1em; padding: 1em;
+  background-color: #EEE;
+  text-align: right;
+}
+.client form textarea,
+.sender form textarea {
+  display: block;
+  min-width: 30em;
+  min-height: 7em;
+  margin-bottom: .5em;
+}
+.sender form textarea {
+  min-height: 13em;
+}
+.client form label[for=hourly] {
+  margin-right: .5em;
+  line-height: 2em;
+  text-align: right;
+  font-weight: bold;
+}
+.invoice form button[type=submit],
+.client form button[type=submit],
+.sender form button[type=submit] {
+  margin: .5em 0 0 auto;
+  padding: .25em .5em;
+  background-color: #DEF;
+  border-radius: .25em;
+  border: 1px solid #08F;
+}
+.client form button[type=submit],
+.sender form button[type=submit] {
+  display: block;
+}
+
+/* ==================== INVOICE FORMS ==================== */
+.invoice form select {
+  display: inline-block;
+  min-width: 20em;
+  margin-bottom: .5em;
+}
+
+.invoice form label {
+  font-weight: bold;
+  line-height: 2em;
+}
+
+.invoice form input#date {
+  margin-right: .25em;
+}
+
+.invoice form label::before {
+  white-space: pre;
+  content: '\0A';
+}
+.invoice form label:first-of-type::before {
+  content: '';
+}
+
+.invoice form label[for=vatsb],
+.invoice form label[for=vatrate] {
+  display: block;
+  text-align: left;
+}
+.invoice form label[for=vatnett],
+.invoice form label[for=vatgross] {
+  display: inline-block;
+  float: left; margin-right: 1em;
+  line-height: 1em;
+}
+.invoice form input[type=radio] { display: none; }
+.invoice form input[type=radio] + label::before {
+  content: '(  ) ';
+}
+.invoice form input:checked + label[for^=vat]::before {
+  content: '(x) ';
+}
+
+.invoice form table {
+  width: 30em;
+  margin-top: 1em;
+  border-spacing: 0;
+}
+.invoice form table tr > th {
+  text-align: left;
+  padding-left: .5em;
+  font-weight: bold;
+}
+.invoice form table tr:last-child {
+  font-family: monospace;
+  white-space: pre;
+  text-align: right;
+}
+.invoice form table tr > th:nth-child(3),
+.invoice form table tr > td:nth-child(4),
+.invoice form table tr > th:nth-child(4) {
+  text-align: right;
+}
+.invoice form table tr > :nth-child(1) { width: 7em; }
+.invoice form table tr > :nth-child(2) { width: 15em; }
+.invoice form table tr > :nth-child(3) { width: 4em; }
+.invoice form table tr > :nth-child(4) { width: 4em; font-family: monospace; }
+.invoice form table tr > * textarea,
+.invoice form table tr > * input[type=number],
+.invoice form table tr > * input {
+  display: block;
+  font-size: 12pt;
+  width: 100%; height: 100%;
+  height: 4.625em;
+}
diff --git a/invoices.sh b/invoices.sh
new file mode 100755 (executable)
index 0000000..a88bc7f
--- /dev/null
@@ -0,0 +1,298 @@
+#!/bin/sh
+
+sender_list(){
+  local select="$1" n name address iban bic
+  [ -d senders/ ] && for n in '' senders/*; do
+    [ "$n" ] &&  read -r address iban bic x<"$n"
+    name="$(UNSTRING "${address#address=}" |sed q |HTML)"
+    [ "${n#senders/}" = "$select" ] \
+    && printf '<option value="%s" selected=selected>%s</option>' "${n#senders/}" "$name" \
+    || printf '<option value="%s">%s</option>' "${n#senders/}" "$name"
+  done
+}
+
+client_list(){
+  local select="$1" n address hourly name
+  [ -d clients/ ] && for n in '' clients/*; do
+    [ "$n" ] && read -r address hourly x<"$n"
+    name="$(UNSTRING "${address#address=}" |sed q |HTML)"
+    [ "${n#clients/}" = "$select" ] \
+    && printf '<option value="%s" selected=selected>%s</option>' "${n#clients/}" "$name" \
+    || printf '<option value="%s">%s</option>' "${n#clients/}" "$name"
+  done
+}
+
+list_invoices(){
+  [ -d invoices/ ] || return 0
+
+  printf '[h1 Open]'
+  for i in invoices/*; do case "$(sed 1q <$i)" in
+    *status=open*) list_invoice "$i";;
+    *status=*) :;;
+    *) list_invoice "$i";;
+  esac; done
+
+  for n in resent:Resent sent:Sent paid:Paid cancelled:Cancelled; do
+    printf '[h1 %s]' "${n#*:}"
+    for i in invoices/*; do case "$(sed 1q <$i)" in
+      *status=${n%:*}*) list_invoice "$i";;
+    esac; done
+  done
+}
+
+list_invoice(){
+  local i="$1"
+  local sender client date number vat vatrate iban bic hourly \
+        taxtype nett tax gross total status
+
+  read -r sender client date number vat vatrate hourly status x<<-EOF
+       $(sed q "$i")
+       EOF
+
+  [ ! -f "senders/${sender#sender=}" ] \
+  && sender="(unset)" \
+  || read -r sender iban bic x<"senders/${sender#sender=}"
+
+  [ ! -f "clients/${client#client=}" ] \
+  && client="(unset)" \
+  || read -r client hourly x<"clients/${client#client=}"
+
+  [ "${date#date=}" -ge 0 ] 2>&- \
+  && date="$(date -d "@${date#date=}" +%x)" \
+  || date="(unset)"
+
+  read -r taxtype nett tax gross x<<-EOF
+       $(invoice_total "${i#invoices/}")
+       EOF
+  case $taxtype in
+    nett)  total="${nett} € + VAT";;
+    gross) total="${gross} € incl. VAT";;
+    *) total="${gross} €";;
+  esac
+
+  case $status in
+    status=sent|status=resent|status=paid|status=cancelled)
+      status="${status#status=}"
+    ;;
+    *) status=open;;
+  esac
+
+  printf '[div .invoice
+    [h2
+        %s]
+    [label From:] %s [label To:] %s [label on] %s
+    [label Amount:] %s
+    [a href="/invoices/%s" Edit]
+  ]' "$(UNSTRING "${number#number=}" |HTML)" \
+     "$(UNSTRING "${sender#address=}" |sed q |HTML)" \
+     "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \
+     "$total" \
+     "$(HTML ${i#invoices/})"
+}
+
+edit_invoice(){
+  local id="$1" sender client date number vat vatrate caddress hourly \
+        taxtype nett tax gross status
+
+  if [ -f "invoices/$id" ]; then
+    read -r sender client date number vat vatrate hourly status x<<-EOF
+       $(sed q "invoices/$id")
+       EOF
+  fi
+
+  case $status in
+    status=sent|status=resent|status=paid|status=cancelled)
+      status="${status#status=}"
+    ;;
+    *) status=open;;
+  esac
+
+  [ "${date#date=}" -ge 0 ] 2>&- \
+  && date="$(date -d "@${date#date=}" +%F)" \
+  || date="$(date +%F)"
+  [ "${number#number=}" ] \
+  && number="${number#number=}" \
+  || number="$(date +%s)"
+  [ "${vatrate#vatrate=}" -ge 0 ] 2>&- \
+  && vatrate="${vatrate#vatrate=}" \
+  || vatrate=19
+
+  [ -f "clients/${client#client=}" ] \
+  && read -r caddress chourly x<"clients/${client#client=}"
+  [ "${chourly#hourly=}" -ge 0 ] 2>&- \
+  && chourly="${chourly#hourly=}" \
+  || chourly=0
+  [ "${hourly#hourly=}" -ge 0 ] 2>&- \
+  && hourly="${hourly#hourly=}" \
+  || hourly="${chourly}"
+
+  tid="$(transid "invoices/$id")"
+
+  read -r taxtype nett tax gross x<<-EOF
+       $(invoice_total "$id")
+       EOF
+
+  cat <<-EOF 
+       [form method="POST" action="/update_invoice"
+         [hidden "id" "$(HTML "$id")"]
+       
+         [label Sender:]
+         [select name=sender
+           $(sender_list "${sender#sender=}")
+         ]
+       
+         [label Client:]
+         [select name=client
+           $(client_list "${client#client=}")
+         ]
+       
+         [label for=number Invoice Number:]
+         [input #number name=number value="$(UNSTRING "${number}" |HTML)"]
+       
+         [label for=date Date:]
+         [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
+       
+         [label for=hourly Hourly Rate:]
+         [input #hourly type=number name=hourly value="${hourly}"]
+
+         [radio "vat" "smallbusiness" #vatsb $([ "${vat#vat=}" = smallbusiness ] && printf checked) ]
+         [label for=vatsb Small business exemption from VAT]
+         [radio "vat" "nett" #vatnett $([ "${vat#vat=}" = nett ] && printf checked)]
+         [label for=vatnett Nett]
+         [radio "vat" "gross" #vatgross $([ "${vat#vat=}" = gross ] && printf checked)]
+         [label for=vatgross Gross]
+         [label for=vatrate VAT Rate: [input type=number name="vatrate" value="${vatrate}"]% ]
+       
+         [table
+           [tr [th Date] [th Work] [th Hours] [th Price] ]
+$({ sed 1d "invoices/$id"; printf 'time= work= hours=\n'; } \
+  | while read -r time work hours x; do
+    hours="$(UNSTRING "${hours#hours=}" |grep -m1 -xE '[0-9]+' || printf 0)"
+    printf '[tr
+            [td [textarea name=time
+%s] ]
+            [td [textarea name=work
+%s] ]
+            [td [input type=number name=hours value="%s"] ]
+            [td %s]
+   ]' "$(UNSTRING "${time#time=}" |HTML)" \
+      "$(UNSTRING "${work#work=}" |HTML)" \
+      "$hours" "$((hours * hourly)) €"
+  done
+)
+            [tr [td colspan=4 
+            $(case $taxtype in
+              (nett)  printf 'Sum: %7.2f €[br] + VAT: %7.2f €[br] [strong Total:] %7.2f €' \
+                      $nett $tax $gross ;;
+              (gross) printf '[strong Total:] %7.2f €[br] incl. nett: %7.2f €[br] + VAT: %7.2f €' \
+                      $gross $nett $tax ;;
+              (*) printf '[strong Total:] %.2f €' $nett ;;
+            esac)
+            ]]
+         ]
+          [select name=status
+             [option value=open      $( [ $status = open      ] && printf selected=selected ) Open]
+             [option value=sent      $( [ $status = sent      ] && printf selected=selected ) Sent]
+             [option value=resent    $( [ $status = resent    ] && printf selected=selected ) Resent]
+             [option value=paid      $( [ $status = paid      ] && printf selected=selected ) Paid]
+             [option value=cancelled $( [ $status = cancelled ] && printf selected=selected ) Cancelled]
+          ]
+         [submit "genpdf" "$tid" Export PDF]
+         [submit "update" "$tid" Update]
+       ]
+       EOF
+}
+
+invoice_total(){
+  local id="$1" sender client date number vat vatrate \
+        total=0 caddress hourly time work hours
+
+  if [ -f "invoices/$id" ]; then
+    read -r sender client date number vat vatrate hourly x<<-EOF
+       $(sed q "invoices/$id")
+       EOF
+
+    [ "${hourly#hourly=}" -gt 0 ] 2>&- \
+    && hourly="${hourly#hourly=}" \
+    || hourly=0
+    [ "${vatrate#vatrate=}" -ge 0 ] 2>&- \
+    && vatrate="${vatrate#vatrate=}" \
+    || vatrate=19
+
+    sed 1d "invoices/$id" \
+    | { while read -r time work hours; do
+        [ "${hours#hours=}" -gt 0 ] 2>&- \
+        && hours="${hours#hours=}" \
+        || hours=0
+        total=$((total + hours * hourly))
+      done
+      case $vat in
+        vat=nett)
+          awk "BEGIN {
+            printf \"nett      %.2f    %.2f    %.2f\",
+              $total, int($total * $vatrate + .5) / 100,
+              $total + int($total * $vatrate + .5) / 100
+          }" ;;
+        vat=gross)
+          awk "BEGIN {
+            printf \"gross     %.2f    %.2f    %.2f\",
+              $total - int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100,
+              int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100, $total
+          }" ;;
+        *)
+          awk "BEGIN {
+            printf \"notax     %.2f    %.2f    %.2f\",
+              $total, 0, $total
+          }" ;;
+      esac
+    }
+  else
+    printf %.2f\\n 0
+  fi
+}
+
+update_invoice(){
+  local id="$(POST id |checkid)" extra=0 tid
+  tid="$(transid invoices/$id)"
+
+  if [ "$(POST update)" = "$tid" ] || [ "$(POST genpdf)" = "$tid" ]; then
+    mkdir -p invoices
+
+    for n in "$(POST_COUNT time)" "$(POST_COUNT work)" "$(POST_COUNT hours)"; do
+      [ "$n" -gt "$extra" ] && extra="$n"
+    done
+
+    { printf 'sender=%s        client=%s       date=%s number=%s       vat=%s  vatrate=%s      hourly=%s       status=%s\n' \
+        "$(POST sender)" "$(POST client)" \
+        "$(date -d "$(POST date)" +%s)" \
+        "$(POST number |STRING)" \
+        "$(POST vat |grep -m1 -xE 'smallbusiness|gross|nett')" \
+        "$(POST vatrate |grep -m1 -xE '[0-9]+')" \
+        "$(POST hourly |grep -m1 -xE '[0-9]+')" \
+        "$(POST status |grep -m1 -xE 'open|sent|resent|paid|cancelled')"
+      for n in $(seq 1 $extra); do
+        printf 'time=%s        work=%s hours=%s\n' \
+          "$(POST time $n |STRING)" "$(POST work $n |STRING)" \
+          "$(POST hours $n |STRING)" \
+        | grep -xvF 'time=     work=   hours=0'
+      done
+    } >"invoices/$id"
+
+    [ -d .git ] && {
+      git add "invoices/$id"
+      git commit -m 'Update invoice info for "'"$(POST number)"'"' -- "invoices/$id"
+    } >/dev/null
+  fi
+  if [ "$(POST genpdf)" ]; then
+    read -r sender client date x<"invoices/$id"
+    read -r saddress x <"senders/${sender#sender=}"
+    read -r caddress x <"clients/${client#client=}"
+    filename="Rechnung $(UNSTRING "${saddress#address=}" |sed 1q) an $(UNSTRING "${caddress#address=}" |sed 1q) $(date -d@"${date#date=}" +%F).pdf"
+
+    . $_EXEC/odtgen.sh
+    genpdf "$id"
+    REDIRECT "/export/${id}.pdf/$(URL "${filename}" |sed s/%0D//g)"
+    exit 0
+  fi
+  REDIRECT "/invoices/$id"
+}
diff --git a/odtgen.sh b/odtgen.sh
new file mode 100755 (executable)
index 0000000..67232a9
--- /dev/null
+++ b/odtgen.sh
@@ -0,0 +1,292 @@
+#!/bin/zsh
+
+odt_manifest(){
+  cat <<-EOF
+       <?xml version="1.0" encoding="UTF-8"?>
+       <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
+         <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.text"/>
+         <manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
+         <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
+       </manifest:manifest>
+       EOF
+}
+
+odt_stylesheet(){
+  local iban="$1" bic="$2"
+
+  cat <<-EOF
+       <?xml version="1.0" encoding="UTF-8"?>
+       <office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
+         <office:styles>
+           <style:style style:name="Standard" style:family="paragraph" style:class="text">
+             <style:paragraph-properties fo:text-align="start" style:justify-single-word="false"/>
+             <style:text-properties style:font-name="DejaVu Sans" fo:font-family="'DejaVu Sans'" style:font-family-generic="swiss" style:font-pitch="variable" style:font-size-asian="10.5pt" style:font-weight-asian="normal"/>
+           </style:style>
+         </office:styles>
+         <office:automatic-styles>
+           <style:style style:name="MP1" style:family="paragraph">
+             <style:paragraph-properties fo:text-align="center" style:justify-single-word="false"/>
+             <style:text-properties style:font-name="DejaVu Sans" fo:font-size="10.5pt" style:font-size-asian="10.5pt" style:font-size-complex="10.5pt"/>
+           </style:style>
+           <style:style style:name="MT1" style:family="text">
+             <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
+           </style:style>
+           <style:page-layout style:name="Mpm1">
+             <style:page-layout-properties fo:page-width="8.2681in" fo:page-height="11.6929in" style:num-format="1" style:print-orientation="portrait" fo:margin-top="0.7874in" fo:margin-bottom="0.7874in" fo:margin-left="0.7874in" fo:margin-right="0.7874in" style:writing-mode="lr-tb" style:footnote-max-height="0in">
+               <style:footnote-sep style:width="0.0071in" style:distance-before-sep="0.0398in" style:distance-after-sep="0.0398in" style:line-style="solid" style:adjustment="left" style:rel-width="25%" style:color="#000000"/>
+             </style:page-layout-properties>
+             <style:header-style/>
+             <style:footer-style>
+               <style:header-footer-properties fo:min-height="0in" fo:margin-left="0in" fo:margin-right="0in" fo:margin-top="0.1965in"/>
+             </style:footer-style>
+           </style:page-layout>
+         </office:automatic-styles>
+         <office:master-styles>
+           <style:master-page style:name="Standard" style:page-layout-name="Mpm1">
+             <style:footer>
+               <text:p text:style-name="MP1">
+                 <text:span text:style-name="MT1">Bankverbindung - IBAN:</text:span>
+                 <text:span>${iban}</text:span>
+                 <text:span text:style-name="MT1">| BIC:</text:span>
+                 <text:span>${bic}</text:span>
+               </text:p>
+             </style:footer>
+           </style:master-page>
+         </office:master-styles>
+       </office:document-styles>
+       EOF
+}
+
+odt_content(){
+  local id="$1" sender="$2" client="$3" date="$4" number="$5" vat="$6" vatrate="$7" hourly="$8" \
+        taxtype nett tax gross
+  read -r taxtype nett tax gross <<-EOF
+       $(invoice_total "$id")
+       EOF
+
+  cat <<-EOF
+       <?xml version="1.0" encoding="UTF-8"?>
+       <office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
+         <office:automatic-styles>
+           <style:style style:name="Table1" style:family="table">
+             <style:table-properties style:width="6.7in" fo:margin-top="0in" fo:margin-bottom="0in" table:align="margins"/>
+           </style:style>
+           <style:style style:name="Table1.A" style:family="table-column">
+             <style:table-column-properties style:column-width="1.1in" style:rel-column-width="11000*"/>
+           </style:style>
+           <style:style style:name="Table1.B" style:family="table-column">
+             <style:table-column-properties style:column-width="3.4in" style:rel-column-width="34000*"/>
+           </style:style>
+           <style:style style:name="Table1.CD" style:family="table-column">
+             <style:table-column-properties style:column-width="1.1in" style:rel-column-width="11000*"/>
+           </style:style>
+           <style:style style:name="Table1.1" style:family="table-cell">
+             <style:table-cell-properties fo:padding="0.0201in" fo:border-left="none" fo:border-right="none" fo:border-top="none" fo:border-bottom="0.05pt solid #000000"/>
+           </style:style>
+           <style:style style:name="Table1.2" style:family="table-cell">
+             <style:table-cell-properties fo:padding="0.0201in" fo:border="none"/>
+           </style:style>
+           <style:style style:name="Table1.6" style:family="table-cell">
+             <style:table-cell-properties fo:padding="0.0201in" fo:border-left="none" fo:border-right="none" fo:border-top="0.05pt solid #000000" fo:border-bottom="none"/>
+           </style:style>
+           <style:style style:name="P3" style:family="paragraph" style:parent-style-name="Standard">
+             <style:text-properties style:font-name="DejaVu Sans" fo:font-size="10.5pt" style:font-size-asian="10.5pt" style:font-size-complex="10.5pt"/>
+           </style:style>
+           <style:style style:name="P5" style:family="paragraph" style:parent-style-name="Standard">
+             <style:paragraph-properties fo:text-align="end" style:justify-single-word="false"/>
+             <style:text-properties fo:font-size="10.5pt" style:font-size-asian="10.5pt" style:font-size-complex="10.5pt"/>
+           </style:style>
+           <style:style style:name="P6" style:family="paragraph" style:parent-style-name="Standard">
+             <style:paragraph-properties fo:text-align="start" style:justify-single-word="false"/>
+             <style:text-properties fo:font-weight="bold" fo:font-size="10.5pt" style:font-size-complex="10.5pt"/>
+           </style:style>
+           <style:style style:name="P7" style:family="paragraph" style:parent-style-name="Standard">
+             <style:paragraph-properties fo:text-align="end" style:justify-single-word="false"/>
+             <style:text-properties fo:font-weight="bold" fo:font-size="10.5pt" style:font-size-complex="10.5pt"/>
+           </style:style>
+           <style:style style:name="T3" style:family="text">
+             <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
+           </style:style>
+           <style:style style:name="fr1" style:family="graphic" style:parent-style-name="Standard">
+             <style:graphic-properties fo:margin-left="0in" fo:margin-right="0in" fo:margin-top="0in" fo:margin-bottom="0in" style:wrap="none" style:vertical-pos="from-top" style:vertical-rel="page-content" style:horizontal-pos="right" style:horizontal-rel="page-content" fo:padding="0in" fo:border="none" style:shadow="none" draw:shadow-opacity="100%"/>
+           </style:style>
+           <style:style style:name="fr2" style:family="graphic" style:parent-style-name="Standard">
+             <style:graphic-properties fo:margin-left="0in" fo:margin-right="0in" fo:margin-top="0in" fo:margin-bottom="0in" style:wrap="none" style:vertical-pos="top" style:vertical-rel="page-content" style:horizontal-pos="right" style:horizontal-rel="page-content" fo:padding="0in" fo:border="none" style:shadow="none" draw:shadow-opacity="100%"/>
+           </style:style>
+           <style:style style:name="fr3" style:family="graphic" style:parent-style-name="Standard">
+             <style:graphic-properties fo:margin-left="0in" fo:margin-right="0in" fo:margin-top="0in" fo:margin-bottom="0in" style:wrap="none" style:vertical-pos="from-top" style:vertical-rel="page-content" style:horizontal-pos="left" style:horizontal-rel="page-content" fo:padding="0in" fo:border="none" style:shadow="none" draw:shadow-opacity="100%"/>
+           </style:style>
+         </office:automatic-styles>
+         <office:body>
+           <office:text>
+             <text:p text:style-name="P3">
+               <draw:frame draw:style-name="fr1" draw:name="Rahmen4" text:anchor-type="paragraph" svg:y="2.7598in" svg:width="1.7283in">
+                 <draw:text-box fo:min-height="0.2528in">
+                   <text:p text:style-name="P5">$(date -d @${date} +%d.%m.%Y)</text:p>
+                 </draw:text-box>
+               </draw:frame><draw:frame draw:style-name="fr2" draw:name="Rahmen2" text:anchor-type="paragraph" svg:width="1.8228in">
+                 <draw:text-box fo:min-height="1.8992in">
+                   <text:p text:style-name="P5"><!--
+                 -->$(HTML "$sender" |sed -r 's;(&#13\;)?&#10\;|(&#x0?[Dd]\;)?&#x0?[aA]\;|\r?$;<text:line-break/>;g')<!--
+                --></text:p>
+                 </draw:text-box>
+               </draw:frame><draw:frame draw:style-name="fr3" draw:name="Rahmen1" text:anchor-type="paragraph" svg:y="1.3799in" svg:width="3.4398in">
+                 <draw:text-box fo:min-height="1.3311in">
+                   <text:p text:style-name="P3"><!--
+                   -->$(HTML "$client" |sed -r 's;(&#13\;)?&#10\;|(&#x0?[Dd]\;)?&#x0?[aA]\;|\r?$;<text:line-break/>;g')<!--
+                  --></text:p>
+                 </draw:text-box>
+               </draw:frame><text:span text:style-name="T3">Rechnung ${number}</text:span>
+             </text:p>
+             <text:p text:style-name="P3"/>
+             <text:p text:style-name="P3"/>
+             <text:p text:style-name="P3">Vergütung: ${hourly} € / Std.<!--
+             -->$(case $vat in
+                  (smallbusiness)
+                    printf '<text:line-break/>Gemäß Kleinunternehmerregelung (§19 Umsatzsteuergesetz) wird keine Umsatzsteuer berechnet.';;
+                  (nett)  printf 'zuzüglich %i%% MwSt.' "$vatrate";;
+                  (gross) printf '(einschließlich %i%% MwSt.)' "$vatrate";;
+                 esac)<!--
+           --></text:p>
+             <text:p text:style-name="P3"/>
+             <table:table table:name="Table1" table:style-name="Table1">
+               <table:table-column table:style-name="Table1.A"/>
+               <table:table-column table:style-name="Table1.B"/>
+               <table:table-column table:style-name="Table1.CD"/>
+               <table:table-column table:style-name="Table1.CD"/>
+               <table:table-header-rows>
+                 <table:table-row>
+                   <table:table-cell table:style-name="Table1.1" office:value-type="string">
+                     <text:p text:style-name="P6">Datum</text:p>
+                   </table:table-cell>
+                   <table:table-cell table:style-name="Table1.1" office:value-type="string">
+                     <text:p text:style-name="P6">Art der Leistung</text:p>
+                   </table:table-cell>
+                   <table:table-cell table:style-name="Table1.1" office:value-type="string">
+                     <text:p text:style-name="P7">Stundenzahl</text:p>
+                   </table:table-cell>
+                   <table:table-cell table:style-name="Table1.1" office:value-type="string">
+                     <text:p text:style-name="P7">Betrag</text:p>
+                   </table:table-cell>
+                 </table:table-row>
+               </table:table-header-rows>
+               $(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
+                       <table:table-row>
+                         <table:table-cell table:style-name="Table1.2" office:value-type="string">
+                           <text:p text:style-name="P3">${time}</text:p>
+                         </table:table-cell>
+                         <table:table-cell table:style-name="Table1.2" office:value-type="string">
+                           <text:p text:style-name="P3">${work}</text:p>
+                         </table:table-cell>
+                         <table:table-cell table:style-name="Table1.2" office:value-type="string">
+                           <text:p text:style-name="P5">${hours}</text:p>
+                         </table:table-cell>
+                         <table:table-cell table:style-name="Table1.2" office:value-type="string">
+                           <text:p text:style-name="P5">$((${hourly} * ${hours})) €</text:p>
+                         </table:table-cell>
+                       </table:table-row>
+                       TROW
+               done)
+               <table:table-row>
+                 <table:table-cell table:style-name="Table1.6" office:value-type="string">
+                   <text:p text:style-name="P6"/>
+                 </table:table-cell>
+                  $(case $taxtype in
+                    (nett) cat <<-TCELL
+                       <table:table-cell table:style-name="Table1.6" table:number-columns-spanned="2" office:value-type="string">
+                         <text:p text:style-name="P5">
+                         Zwischensumme:<text:line-break/>
+                         zzgl. ${vatrate}% MwSt.:<text:line-break/>
+                         </text:p>
+                         <text:p text:style-name="P7">zu zahlender Betrag:</text:p>
+                       </table:table-cell>
+                       <table:table-cell table:style-name="Table1.6" office:value-type="string">
+                         <text:p text:style-name="P5">
+                         ${nett} €<text:line-break/>
+                         ${tax} €<text:line-break/>
+                         </text:p>
+                         <text:p text:style-name="P7">${gross} €</text:p>
+                       </table:table-cell>
+                       TCELL
+                        ;;
+                    (gross) cat <<-TCELL
+                       <table:table-cell table:style-name="Table1.6" table:number-columns-spanned="2" office:value-type="string">
+                         <text:p text:style-name="P7">zu zahlender Betrag:</text:p>
+                         <text:p text:style-name="P5">
+                         <text:line-break/>
+                         enthaltene MwSt (${vatrate}%):<text:line-break/>
+                         enthaltener Netto-Betrag:<!--
+                       --></text:p>
+                       </table:table-cell>
+                       <table:table-cell table:style-name="Table1.6" office:value-type="string">
+                         <text:p text:style-name="P7">${gross} €</text:p>
+                         <text:p text:style-name="P5">
+                         <text:line-break/>
+                         ${tax} €<text:line-break/>
+                         ${nett} €<!--
+                       --></text:p>
+                       </table:table-cell>
+                       TCELL
+                        ;;
+                    (*) cat <<-TCELL
+                       <table:table-cell table:style-name="Table1.6" table:number-columns-spanned="2" office:value-type="string">
+                         <text:p text:style-name="P7">zu zahlender Betrag:</text:p>
+                       </table:table-cell>
+                       <table:table-cell table:style-name="Table1.6" office:value-type="string">
+                         <text:p text:style-name="P7">${gross} €</text:p>
+                       </table:table-cell>
+                       TCELL
+                        ;;
+                  esac)
+               </table:table-row>
+             </table:table>
+           </office:text>
+         </office:body>
+       </office:document-content>
+       EOF
+}
+
+genodt(){
+  local id="$1" exdir="export/$1"
+  [ -d "$exdir" ] && {
+    rm "$exdir/content.xml" "$exdir/styles.xml" "$exdir/META-INF/manifest.xml"
+    rmdir "$exdir/META-INF/" "$exdir/"
+  }
+
+  read -r sender client date number vat vatrate hourly x<<-EOF
+       $(sed q "invoices/$id")
+       EOF
+  read -r sender iban bic x<"senders/${sender#sender=}"
+  read -r client chourly x<"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 -q "../${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/"
+}
+
+genpdf(){
+  local id="$1"
+  genodt "$id"
+  lowriter --convert-to pdf --outdir "export/" "export/${id}.odt" >/dev/null
+  rm "export/${id}.odt"
+}
+
diff --git a/senders.sh b/senders.sh
new file mode 100755 (executable)
index 0000000..757ce22
--- /dev/null
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+edit_sender(){
+  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
+
+Phone:
+000 000000
+
+Tax no.
+xxx / 000 / ###
+"
+  printf '
+    [form method="POST" action="/update_sender"
+      [hidden "id" "%s"]
+      <textarea name="address" placeholder="address">%s</textarea>
+      [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 "${iban}")" "$(HTML "${bic}")" \
+    "$(transid "senders/$id")"
+}
+
+list_senders(){
+  [ -d senders/ ] && for s in senders/*; do
+    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/}")"
+  done
+}
+
+update_sender(){
+  id="$(POST id |checkid)"
+  if [ "$(POST update)" = "$(transid "senders/$id")" ]; then
+    mkdir -p senders
+    printf 'address=%s iban=%s bic=%s\n' \
+    $(POST address |STRING) $(POST iban |STRING) \
+    $(POST bic |STRING) >"senders/$id"
+
+    [ -d .git ] && {
+      git add "senders/$id"
+      git commit -m 'Update sender info for "'"$(POST address |sed 1q)"'"' -- "senders/$id"
+    } >/dev/null
+
+    REDIRECT "/senders/"
+  else
+    REDIRECT "/senders/$id"
+  fi
+}