]> git.plutz.net Git - invoices/commitdiff
split code into multiple files
authorPaul Hänsch <paul@plutz.net>
Mon, 19 Nov 2018 12:03:26 +0000 (13:03 +0100)
committerPaul Hänsch <paul@plutz.net>
Mon, 19 Nov 2018 12:03:26 +0000 (13:03 +0100)
clients.sh [new file with mode: 0755]
invoices.cgi
invoices.sh [new file with mode: 0755]
senders.sh [new file with mode: 0755]

diff --git a/clients.sh b/clients.sh
new file mode 100755 (executable)
index 0000000..fc047a2
--- /dev/null
@@ -0,0 +1,50 @@
+#!/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" "update" Update]
+    ]' \
+    "$(HTML $id)" \
+    "$(UNSTRING "${address#address=}" |HTML)" \
+    "$(UNSTRING "${hourly#hourly=}" |grep -xE '[0-9]+')"
+}
+
+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(){
+  id="$(POST id |checkid)"
+  if [ "$(POST update)" = update -a "$id" ]; then
+    mkdir -p clients
+    printf 'address=%s hourly=%s' \
+      "$(POST address |STRING)" "$(POST hourly |STRING)" \
+      >"clients/$id"
+  else
+    echo Invalid Data "$(POST id)" "$(POST update)" >&2
+  fi
+}
index cc505d4c52f3f09f7b9296811405f4b58906d551..7e2f35193ae76bfd1012f3d91e0f62c7a40c3837 100755 (executable)
@@ -8,7 +8,7 @@ _EXEC="$(realpath "${0%/*}")"
 
 yield_page(){
 printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
-./cgilite/html-sh.sed <<EOF
+"$_EXEC/cgilite/html-sh.sed" <<EOF
 [html [head
   [meta name="viewport" content="width=device-width"]
   [link rel="stylesheet" type="text/css" href="/invoices.css"]
@@ -24,191 +24,6 @@ printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
 EOF
 }
 
-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" "update" Update]
-    ]' \
-    "$(HTML $id)" \
-    "$(UNSTRING "${address#address=}" |HTML)" \
-    "$(UNSTRING "${hourly#hourly=}" |grep -xE '[0-9]+')"
-}
-
-edit_sender(){
-  id="$1"
-  if [ -f "senders/$id" ]; then
-    address="$(cat "senders/$id")"
-  fi
-  [ "$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>
-      [submit "update" "update" Update]
-    ]' \
-    "$(HTML $id)" \
-    "$(HTML "${address}")"
-}
-
-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
-}
-
-list_senders(){
-  [ -d senders/ ] && for s in senders/*; do
-    address=$(cat "$s")
-    [ "$address" ] || address="(no address)"
-    printf '[div .sender .address <!--
-      -->%s[a href="/senders/%s" Edit]]
-    ' "$(HTML "$address")" "$(HTML "${s#senders/}")"
-  done
-}
-
-sender_list(){
-  local select="$1" n name
-  [ -d senders/ ] && for n in '' senders/*; do
-    [ "$n" ] && name="$(sed q "$n" |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 <"$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/ ] && 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 "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)"
-
-    printf '[div .invoice
-      [h2
-          %s]
-      [label From:] %s [label To:] %s [label on] %s
-      [a href="/invoice/%s" Edit]
-    ]' "$(UNSTRING "${number#number=}" |HTML)" \
-       "$(HTML "$sender")" \
-       "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \
-       "$(HTML ${i#invoices/})"
-  done
-}
-
-edit_invoice(){
-  id="$1"
-  if [ -f "invoices/$id" ]; then
-    read -r sender client date number vat vatrate<<-EOF
-       $(sed q "invoices/$id")
-       EOF
-  fi
-
-  [ "${date#date=}" -ge 0 ] 2>&- \
-  && date="$(date -d "@${date#date=}" +%F)" \
-  || date="$(date +%F)"
-  [ "${number#number=}" ] || number="number=$(date +%s)"
-  [ "${vatrate#vatrate=}" -ge 0 ] 2>&- || vatrate="vatrate=19"
-
-  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#number=}" |HTML)"]
-
-          [label for=date Date:]
-          [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
-
-          [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#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; do
-      hours="$(UNSTRING "${hours#hours=}" |grep -m1 -xE '[0-9]+' || printf 0)"
-      printf '[tr
-              [td [textarea name=date
-%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
-)
-          ]
-         [submit "update" "update" Update]
-       ]
-       EOF
-}
-
 info="$(PATH "${PATH_INFO}")"
 
 case $info in
@@ -217,61 +32,48 @@ case $info in
     FILE "$_EXEC/invoices.css"
     ;;
   /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)
-    id="$(POST id |checkid)"
-    if [ "$(POST update)" = update -a "$id" ]; then
-      mkdir -p clients
-      printf 'address=%s       hourly=%s' \
-        "$(POST address |STRING)" "$(POST hourly |STRING)" \
-        >"clients/$id"
-    else
-      echo Invalid Data "$(POST id)" "$(POST update)" >&2
-    fi
+    . "$_EXEC/clients.sh"
+    update_client
     REDIRECT /clients/
     ;;
   /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)
-    id="$(POST id |checkid)"
-    if [ "$(POST update)" = update -a "$id" ]; then
-      mkdir -p senders
-      POST address >"senders/$id"
-    fi
+    . "$_EXEC/senders.sh"
+    update_sender
     REDIRECT /senders/
     ;;
   /invoices)
+    . "$_EXEC/invoices.sh"
     { list_invoices
       printf '[a .new href="/invoice/%s" New]' "$(timeid)"
     } | yield_page invoices
     ;;
   /invoice/*)
+    . "$_EXEC/invoices.sh"
     edit_invoice "${info#/invoice/}" |yield_page invoice
     ;;
   /update_invoice)
-    id="$(POST id |checkid)"
-    if [ "$(POST update)" = update -a "$id" ]; then
-      mkdir -p invoices
-      printf 'sender=%s        client=%s       date=%s number=%s       vat=%s  vatrate=%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]+')" \
-      >"invoices/$id"
-    fi
+    . "$_EXEC/invoices.sh"
+    update_invoice
     REDIRECT "/invoice/$id"
     ;;
   *) REDIRECT /invoices
diff --git a/invoices.sh b/invoices.sh
new file mode 100755 (executable)
index 0000000..0e80115
--- /dev/null
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+sender_list(){
+  local select="$1" n name
+  [ -d senders/ ] && for n in '' senders/*; do
+    [ "$n" ] && name="$(sed q "$n" |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 <"$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/ ] && 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 "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)"
+
+    printf '[div .invoice
+      [h2
+          %s]
+      [label From:] %s [label To:] %s [label on] %s
+      [a href="/invoice/%s" Edit]
+    ]' "$(UNSTRING "${number#number=}" |HTML)" \
+       "$(HTML "$sender")" \
+       "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \
+       "$(HTML ${i#invoices/})"
+  done
+}
+
+edit_invoice(){
+  id="$1"
+  if [ -f "invoices/$id" ]; then
+    read -r sender client date number vat vatrate<<-EOF
+       $(sed q "invoices/$id")
+       EOF
+  fi
+
+  [ "${date#date=}" -ge 0 ] 2>&- \
+  && date="$(date -d "@${date#date=}" +%F)" \
+  || date="$(date +%F)"
+  [ "${number#number=}" ] || number="number=$(date +%s)"
+  [ "${vatrate#vatrate=}" -ge 0 ] 2>&- || vatrate="vatrate=19"
+
+  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#number=}" |HTML)"]
+
+          [label for=date Date:]
+          [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
+
+          [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#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; do
+      hours="$(UNSTRING "${hours#hours=}" |grep -m1 -xE '[0-9]+' || printf 0)"
+      printf '[tr
+              [td [textarea name=date
+%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
+)
+          ]
+         [submit "update" "update" Update]
+       ]
+       EOF
+}
+
+update_invoice(){
+  id="$(POST id |checkid)"
+  if [ "$(POST update)" = update -a "$id" ]; then
+    mkdir -p invoices
+    printf 'sender=%s  client=%s       date=%s number=%s       vat=%s  vatrate=%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]+')" \
+    >"invoices/$id"
+  fi
+}
diff --git a/senders.sh b/senders.sh
new file mode 100755 (executable)
index 0000000..90c3d38
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+edit_sender(){
+  id="$1"
+  if [ -f "senders/$id" ]; then
+    address="$(cat "senders/$id")"
+  fi
+  [ "$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>
+      [submit "update" "update" Update]
+    ]' \
+    "$(HTML $id)" \
+    "$(HTML "${address}")"
+}
+
+list_senders(){
+  [ -d senders/ ] && for s in senders/*; do
+    address=$(cat "$s")
+    [ "$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)" = update -a "$id" ]; then
+    mkdir -p senders
+    POST address >"senders/$id"
+  fi
+}