]> git.plutz.net Git - invoices/blobdiff - invoices.sh
Merge commit '2e7a8a492ace7801db67815741dbfbb80f0608af' as 'cgilite'
[invoices] / invoices.sh
index 9e07e9fab11ffbec85a9d97fbd37c31c02f59e94..a88bc7fc59fc663892f198b92946407566f2f2cb 100755 (executable)
@@ -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 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"
@@ -13,7 +14,7 @@ sender_list(){
 client_list(){
   local select="$1" n address hourly name
   [ -d clients/ ] && for n in '' clients/*; do
-    [ "$n" ] && read -r address hourly <"$n"
+    [ "$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" \
@@ -22,42 +23,90 @@ client_list(){
 }
 
 list_invoices(){
-  [ -d invoices/ ] && for i in invoices/*; do
-    read -r sender client date number vat vatrate<<-EOF
+  [ -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="$(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
-      [label Amount:] %.2f €
-      [a href="/invoices/%s" Edit]
-    ]' "$(UNSTRING "${number#number=}" |HTML)" \
-       "$(HTML "$sender")" \
-       "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \
-       "$(invoice_total "${i#invoices/}")" \
-       "$(HTML ${i#invoices/})"
-  done
+
+  [ ! -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 total
+  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<<-EOF
+    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)"
@@ -69,10 +118,19 @@ edit_invoice(){
   || vatrate=19
 
   [ -f "clients/${client#client=}" ] \
-  && read -r caddress hourly <"clients/${client#client=}"
-  hourly="${hourly#hourly=}"
+  && 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}"
 
-  total=$(invoice_total "$id")
+  tid="$(transid "invoices/$id")"
+
+  read -r taxtype nett tax gross x<<-EOF
+       $(invoice_total "$id")
+       EOF
 
   cat <<-EOF 
        [form method="POST" action="/update_invoice"
@@ -94,6 +152,9 @@ edit_invoice(){
          [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)]
@@ -105,7 +166,7 @@ edit_invoice(){
          [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
+  | 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
@@ -120,24 +181,24 @@ $({ sed 1d "invoices/$id"; printf 'time= work= hours=\n'; } \
   done
 )
             [tr [td colspan=4 
-            $(case $vat in
-              vat=nett)
-                awk "BEGIN {
-                  printf \"Sum: %7.2f €<br/> + VAT: %7.2f €<br/> [strong Total:] %7.2f €\",
-                   $total, int($total * $vatrate + .5) / 100,
-                   $total + int($total * $vatrate + .5) / 100
-                }" ;;
-              vat=gross)
-                awk "BEGIN {
-                  printf \"[strong Total:] %7.2f €<br/> incl VAT: %7.2f €<br/>+ nett: %7.2f €\",
-                   $total, int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100,
-                   $total - int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100
-                }" ;;
-              *) printf '[strong Total:] %.2f €' "$total" ;;
+            $(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)
             ]]
          ]
-         [submit "update" "$(tid "invoices/$id")" Update]
+          [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
 }
@@ -147,24 +208,43 @@ invoice_total(){
         total=0 caddress hourly time work hours
 
   if [ -f "invoices/$id" ]; then
-    read -r sender client date number vat vatrate<<-EOF
+    read -r sender client date number vat vatrate hourly x<<-EOF
        $(sed q "invoices/$id")
        EOF
 
-    [ -f "clients/${client#client=}" ] \
-    && read -r caddress hourly <"clients/${client#client=}"
     [ "${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 time work hours; do
+    | { while read -r time work hours; do
         [ "${hours#hours=}" -gt 0 ] 2>&- \
         && hours="${hours#hours=}" \
         || hours=0
         total=$((total + hours * hourly))
       done
-      printf %.2f\\n "$total"
+      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
@@ -172,21 +252,24 @@ invoice_total(){
 }
 
 update_invoice(){
-  local id="$(POST id |checkid)" extra=0
+  local id="$(POST id |checkid)" extra=0 tid
+  tid="$(transid 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
       [ "$n" -gt "$extra" ] && extra="$n"
     done
 
-    { printf 'sender=%s        client=%s       date=%s number=%s       vat=%s  vatrate=%s\n' \
+    { 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 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)" \
@@ -194,6 +277,22 @@ update_invoice(){
         | 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"
 }