]> git.plutz.net Git - invoices/blobdiff - invoices.sh
tax calculation in pdf export
[invoices] / invoices.sh
index 9e07e9fab11ffbec85a9d97fbd37c31c02f59e94..6538c967945693f7fe8711f4aa56a5f3b7450deb 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 <"$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"
@@ -22,36 +23,53 @@ client_list(){
 }
 
 list_invoices(){
+  local sender client date number vat vatrate iban bic hourly \
+        taxtype nett tax gross total
+
   [ -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)"
 
+    read -r taxtype nett tax gross <<-EOF
+       $(invoice_total "${i#invoices/}")
+       EOF
+    case $taxtype in
+      nett)  total="${nett} € + VAT";;
+      gross) total="${gross} € incl. VAT";;
+      *) total="${gross} €";;
+    esac
+
     printf '[div .invoice
       [h2
           %s]
       [label From:] %s [label To:] %s [label on] %s
-      [label Amount:] %.2f €
+      [label Amount:] %s
       [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/}")" \
+       "$total" \
        "$(HTML ${i#invoices/})"
   done
 }
 
 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
+
   if [ -f "invoices/$id" ]; then
     read -r sender client date number vat vatrate<<-EOF
        $(sed q "invoices/$id")
@@ -72,7 +90,11 @@ edit_invoice(){
   && read -r caddress hourly <"clients/${client#client=}"
   hourly="${hourly#hourly=}"
 
-  total=$(invoice_total "$id")
+  tid="$(tid "invoices/$id")"
+
+  read -r taxtype nett tax gross <<-EOF
+       $(invoice_total "$id")
+       EOF
 
   cat <<-EOF 
        [form method="POST" action="/update_invoice"
@@ -120,24 +142,17 @@ $({ 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]
+         [submit "genpdf" "$tid" Export PDF]
+         [submit "update" "$tid" Update]
        ]
        EOF
 }
@@ -156,15 +171,36 @@ invoice_total(){
     [ "${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,9 +208,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 +232,12 @@ update_invoice(){
       done
     } >"invoices/$id"
   fi
+  if [ "$(POST genpdf)" ]; then
+    . $_EXEC/odtgen.sh
+    genodt "$id"
+    lowriter --convert-to pdf --outdir export/ "export/${id}.odt" >/dev/null
+    REDIRECT "/export/${id}.pdf"
+    exit 0
+  fi
   REDIRECT "/invoices/$id"
 }