]> git.plutz.net Git - invoices/commitdiff
total and vat calculation
authorPaul Hänsch <paul@plutz.net>
Thu, 22 Nov 2018 03:07:25 +0000 (04:07 +0100)
committerPaul Hänsch <paul@plutz.net>
Thu, 22 Nov 2018 03:07:25 +0000 (04:07 +0100)
invoices.css
invoices.sh

index 291b847b4f932d18be671e2307d9ddb5b27dadae..aecfd06c200b83f1fcbc9824e5a03598453ce3ea 100755 (executable)
@@ -9,6 +9,8 @@ body {
   color: black; background-color: white;
 }
 
+strong {font-weight: bold;}
+em {text-decoration: italic;}
 
 /* ==================== MENU ==================== */
 
@@ -184,6 +186,11 @@ form input[type=number] {
   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) {
@@ -192,7 +199,7 @@ form input[type=number] {
 .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; }
+.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 {
index a38693298d9c3c235ad9d14c5840dfca7c5f8749..9e07e9fab11ffbec85a9d97fbd37c31c02f59e94 100755 (executable)
@@ -40,16 +40,18 @@ list_invoices(){
       [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
 }
 
 edit_invoice(){
-  local id="$1" sender client date number vat vatrate caddress hourly
+  local id="$1" sender client date number vat vatrate caddress hourly total
   if [ -f "invoices/$id" ]; then
     read -r sender client date number vat vatrate<<-EOF
        $(sed q "invoices/$id")
@@ -59,13 +61,19 @@ edit_invoice(){
   [ "${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"
+  [ "${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 hourly <"clients/${client#client=}"
   hourly="${hourly#hourly=}"
 
+  total=$(invoice_total "$id")
+
   cat <<-EOF 
        [form method="POST" action="/update_invoice"
          [hidden "id" "$(HTML "$id")"]
@@ -81,7 +89,7 @@ edit_invoice(){
          ]
        
          [label for=number Invoice Number:]
-         [input #number name=number value="$(UNSTRING "${number#number=}" |HTML)"]
+         [input #number name=number value="$(UNSTRING "${number}" |HTML)"]
        
          [label for=date Date:]
          [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
@@ -92,31 +100,77 @@ edit_invoice(){
          [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=}"]% ]
+         [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; do
-      hours="$(UNSTRING "${hours#hours=}" |grep -m1 -xE '[0-9]+' || printf 0)"
-      printf '[tr
-              [td [textarea name=time
+  | while read -r time work hours; do
+    hours="$(UNSTRING "${hours#hours=}" |grep -m1 -xE '[0-9]+' || printf 0)"
+    printf '[tr
+            [td [textarea name=time
 %s] ]
-              [td [textarea name=work
+            [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
+            [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 $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" ;;
+            esac)
+            ]]
          ]
          [submit "update" "$(tid "invoices/$id")" 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<<-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
+
+    sed 1d "invoices/$id" \
+    | { while read time work hours; do
+        [ "${hours#hours=}" -gt 0 ] 2>&- \
+        && hours="${hours#hours=}" \
+        || hours=0
+        total=$((total + hours * hourly))
+      done
+      printf %.2f\\n "$total"
+    }
+  else
+    printf %.2f\\n 0
+  fi
+}
+
 update_invoice(){
   local id="$(POST id |checkid)" extra=0