4 local select="$1" n name address iban bic
5 [ -d senders/ ] && for n in '' senders/*; do
6 [ "$n" ] && read -r address iban bic x<"$n"
7 name="$(UNSTRING "${address#address=}" |sed q |HTML)"
8 [ "${n#senders/}" = "$select" ] \
9 && printf '<option value="%s" selected=selected>%s</option>' "${n#senders/}" "$name" \
10 || printf '<option value="%s">%s</option>' "${n#senders/}" "$name"
15 local select="$1" n address hourly name
16 [ -d clients/ ] && for n in '' clients/*; do
17 [ "$n" ] && read -r address hourly x<"$n"
18 name="$(UNSTRING "${address#address=}" |sed q |HTML)"
19 [ "${n#clients/}" = "$select" ] \
20 && printf '<option value="%s" selected=selected>%s</option>' "${n#clients/}" "$name" \
21 || printf '<option value="%s">%s</option>' "${n#clients/}" "$name"
26 [ -d invoices/ ] || return 0
29 for i in invoices/*; do case "$(sed 1q <$i)" in
30 *status=open*) list_invoice "$i";;
32 *) list_invoice "$i";;
35 for n in resent:Resent sent:Sent paid:Paid cancelled:Cancelled; do
36 printf '[h1 %s]' "${n#*:}"
37 printf "%s\n" invoices/* \
39 | while read i; do case "$(sed 1q <$i)" in
40 *status=${n%:*}*) list_invoice "$i";;
47 local sender client date number vat vatrate iban bic hourly \
48 taxtype nett tax gross total status
50 read -r sender client date number vat vatrate hourly status x<<-EOF
54 [ ! -f "senders/${sender#sender=}" ] \
56 || read -r sender iban bic x<"senders/${sender#sender=}"
58 [ ! -f "clients/${client#client=}" ] \
60 || read -r client hourly x<"clients/${client#client=}"
62 [ "${date#date=}" -ge 0 ] 2>&- \
63 && date="$(date -d "@${date#date=}" +%x)" \
66 read -r taxtype nett tax gross x<<-EOF
67 $(invoice_total "${i#invoices/}")
70 nett) total="${nett} € + VAT";;
71 gross) total="${gross} € incl. VAT";;
72 *) total="${gross} €";;
76 status=sent|status=resent|status=paid|status=cancelled)
77 status="${status#status=}"
85 [label From:] %s [label To:] %s [label on] %s
87 [a href="/invoices/%s" Edit]
88 ]' "$(UNSTRING "${number#number=}" |HTML)" \
89 "$(UNSTRING "${sender#address=}" |sed q |HTML)" \
90 "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \
92 "$(HTML ${i#invoices/})"
96 local id="$1" sender client date number vat vatrate caddress hourly \
97 taxtype nett tax gross status
99 if [ -f "invoices/$id" ]; then
100 read -r sender client date number vat vatrate hourly status x<<-EOF
101 $(sed q "invoices/$id")
106 status=sent|status=resent|status=paid|status=cancelled)
107 status="${status#status=}"
112 [ "${date#date=}" -ge 0 ] 2>&- \
113 && date="$(date -d "@${date#date=}" +%F)" \
114 || date="$(date +%F)"
115 [ "${number#number=}" ] \
116 && number="${number#number=}" \
117 || number="$(date +%s)"
118 [ "${vatrate#vatrate=}" -ge 0 ] 2>&- \
119 && vatrate="${vatrate#vatrate=}" \
122 [ -f "clients/${client#client=}" ] \
123 && read -r caddress chourly x<"clients/${client#client=}"
124 [ "${chourly#hourly=}" -ge 0 ] 2>&- \
125 && chourly="${chourly#hourly=}" \
127 [ "${hourly#hourly=}" -ge 0 ] 2>&- \
128 && hourly="${hourly#hourly=}" \
129 || hourly="${chourly}"
131 tid="$(transid "invoices/$id")"
133 read -r taxtype nett tax gross x<<-EOF
134 $(invoice_total "$id")
138 [form method="POST" action="/update_invoice"
139 [hidden "id" "$(HTML "$id")"]
143 $(sender_list "${sender#sender=}")
148 $(client_list "${client#client=}")
151 [label for=number Invoice Number:]
152 [input #number name=number value="$(UNSTRING "${number}" |HTML)"]
154 [label for=date Date:]
155 [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
157 [label for=hourly Hourly Rate:]
158 [input #hourly type=number name=hourly value="${hourly}"]
160 [radio "vat" "smallbusiness" #vatsb $([ "${vat#vat=}" = smallbusiness ] && printf checked) ]
161 [label for=vatsb Small business exemption from VAT]
162 [radio "vat" "nett" #vatnett $([ "${vat#vat=}" = nett ] && printf checked)]
163 [label for=vatnett Nett]
164 [radio "vat" "gross" #vatgross $([ "${vat#vat=}" = gross ] && printf checked)]
165 [label for=vatgross Gross]
166 [label for=vatrate VAT Rate: [input type=number name="vatrate" value="${vatrate}"]% ]
169 [tr [th Date] [th Work] [th Hours] [th Price] ]
170 $({ sed 1d "invoices/$id"; printf 'time= work= hours=\n'; } \
171 | while read -r time work hours x; do
172 hours="$(UNSTRING "${hours#hours=}" \
173 |grep -m1 -xE '\.[0-9]+|[0-9]+\.?[0-9]*' || printf 0)"
175 [td [textarea name=time
177 [td [textarea name=work
179 [td [input type=number name=hours value="%g" step=any] ]
181 ]' "$(UNSTRING "${time#time=}" |HTML)" \
182 "$(UNSTRING "${work#work=}" |HTML)" \
184 "$(awk "BEGIN { printf \"%.2f €\", ${hours} * ${hourly}; }")"
189 (nett) printf 'Sum: %7.2f €[br] + VAT: %7.2f €[br] [strong Total:] %7.2f €' \
191 (gross) printf '[strong Total:] %7.2f €[br] incl. nett: %7.2f €[br] + VAT: %7.2f €' \
193 (*) printf '[strong Total:] %.2f €' $nett ;;
198 [option value=open $( [ $status = open ] && printf selected=selected ) Open]
199 [option value=sent $( [ $status = sent ] && printf selected=selected ) Sent]
200 [option value=resent $( [ $status = resent ] && printf selected=selected ) Resent]
201 [option value=paid $( [ $status = paid ] && printf selected=selected ) Paid]
202 [option value=cancelled $( [ $status = cancelled ] && printf selected=selected ) Cancelled]
204 [submit "genpdf" "$tid" Export PDF]
205 [submit "update" "$tid" Update]
211 local id="$1" sender client date number vat vatrate \
212 total=0 caddress hourly time work hours
214 if [ -f "invoices/$id" ]; then
215 read -r sender client date number vat vatrate hourly x<<-EOF
216 $(sed q "invoices/$id")
219 [ "${hourly#hourly=}" -gt 0 ] 2>&- \
220 && hourly="${hourly#hourly=}" \
222 [ "${vatrate#vatrate=}" -ge 0 ] 2>&- \
223 && vatrate="${vatrate#vatrate=}" \
226 sed 1d "invoices/$id" \
227 | { while read -r time work hours; do
228 [ "${hours#hours=}" ] 2>&- \
229 && hours="${hours#hours=}" \
231 total=$(awk "BEGIN { printf \"%.2f\", ${total} + ${hours} * ${hourly}; }")
236 printf \"nett %.2f %.2f %.2f\",
237 $total, int($total * $vatrate + .5) / 100,
238 $total + int($total * $vatrate + .5) / 100
242 printf \"gross %.2f %.2f %.2f\",
243 $total - int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100,
244 int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100, $total
248 printf \"notax %.2f %.2f %.2f\",
259 local id="$(POST id |checkid)" extra=0 tid
260 tid="$(transid invoices/$id)"
262 if [ "$(POST update)" = "$tid" ] || [ "$(POST genpdf)" = "$tid" ]; then
265 for n in "$(POST_COUNT time)" "$(POST_COUNT work)" "$(POST_COUNT hours)"; do
266 [ "$n" -gt "$extra" ] && extra="$n"
269 { printf 'sender=%s client=%s date=%s number=%s vat=%s vatrate=%s hourly=%s status=%s\n' \
270 "$(POST sender)" "$(POST client)" \
271 "$(date -d "$(POST date)" +%s)" \
272 "$(POST number |STRING)" \
273 "$(POST vat |grep -m1 -xE 'smallbusiness|gross|nett')" \
274 "$(POST vatrate |grep -m1 -xE '[0-9]+')" \
275 "$(POST hourly |grep -m1 -xE '[0-9]+')" \
276 "$(POST status |grep -m1 -xE 'open|sent|resent|paid|cancelled')"
277 for n in $(seq 1 $extra); do
278 printf 'time=%s work=%s hours=%s\n' \
279 "$(POST time $n |STRING)" "$(POST work $n |STRING)" \
280 "$(POST hours $n |STRING)" \
281 | grep -xvF 'time=\ work=\ hours=0'
286 git add "invoices/$id"
287 git commit -m 'Update invoice info for "'"$(POST number)"'"' -- "invoices/$id"
290 if [ "$(POST genpdf)" ]; then
291 read -r sender client date x<"invoices/$id"
292 read -r saddress x <"senders/${sender#sender=}"
293 read -r caddress x <"clients/${client#client=}"
294 filename="Rechnung $(UNSTRING "${saddress#address=}" |sed 1q) an $(UNSTRING "${caddress#address=}" |sed 1q) $(date -d@"${date#date=}" +%F).pdf"
298 REDIRECT "/export/${id}.pdf/$(URL "${filename}" |sed s/%0D//g)"
301 REDIRECT "/invoices/$id"