X-Git-Url: http://git.plutz.net/?a=blobdiff_plain;f=invoices.sh;h=6538c967945693f7fe8711f4aa56a5f3b7450deb;hb=0332f87c1780ec8045980baa86e521fb003b8afa;hp=0e801157c4917249c9669cd69dd996021e17b2cd;hpb=de8eaf901067d4e0c72e5b0ca05f6dc12553fd79;p=invoices diff --git a/invoices.sh b/invoices.sh index 0e80115..6538c96 100755 --- a/invoices.sh +++ b/invoices.sh @@ -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 '' "${n#senders/}" "$name" \ || printf '' "${n#senders/}" "$name" @@ -22,34 +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 - [a href="/invoice/%s" Edit] + [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")" \ + "$total" \ "$(HTML ${i#invoices/})" done } edit_invoice(){ - id="$1" + 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") @@ -59,70 +79,165 @@ 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=}" + + tid="$(tid "invoices/$id")" + + read -r taxtype nett tax gross <<-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#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] ] + [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"] + + [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; do - hours="$(UNSTRING "${hours#hours=}" |grep -m1 -xE '[0-9]+' || printf 0)" - printf '[tr - [td [textarea name=date + | 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 ) - ] - [submit "update" "update" Update] + [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) + ]] + ] + [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<<-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 -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(){ - id="$(POST id |checkid)" - if [ "$(POST update)" = update -a "$id" ]; then + local id="$(POST id |checkid)" extra=0 tid + tid="$(tid invoices/$id)" + + if [ "$(POST update)" = "$tid" ] || [ "$(POST genpdf)" = "$tid" ]; 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" + + 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' \ + "$(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]+')" + 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" + 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" }