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 for i in invoices/*; do case "$(sed 1q <$i)" in
38 *status=${n%:*}*) list_invoice "$i";;
45 local sender client date number vat vatrate iban bic hourly \
46 taxtype nett tax gross total status
48 read -r sender client date number vat vatrate hourly status x<<-EOF
52 [ ! -f "senders/${sender#sender=}" ] \
54 || read -r sender iban bic x<"senders/${sender#sender=}"
56 [ ! -f "clients/${client#client=}" ] \
58 || read -r client hourly x<"clients/${client#client=}"
60 [ "${date#date=}" -ge 0 ] 2>&- \
61 && date="$(date -d "@${date#date=}" +%x)" \
64 read -r taxtype nett tax gross x<<-EOF
65 $(invoice_total "${i#invoices/}")
68 nett) total="${nett} € + VAT";;
69 gross) total="${gross} € incl. VAT";;
70 *) total="${gross} €";;
74 status=sent|status=resent|status=paid|status=cancelled)
75 status="${status#status=}"
83 [label From:] %s [label To:] %s [label on] %s
85 [a href="/invoices/%s" Edit]
86 ]' "$(UNSTRING "${number#number=}" |HTML)" \
87 "$(UNSTRING "${sender#address=}" |sed q |HTML)" \
88 "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \
90 "$(HTML ${i#invoices/})"
94 local id="$1" sender client date number vat vatrate caddress hourly \
95 taxtype nett tax gross status
97 if [ -f "invoices/$id" ]; then
98 read -r sender client date number vat vatrate hourly status x<<-EOF
99 $(sed q "invoices/$id")
104 status=sent|status=resent|status=paid|status=cancelled)
105 status="${status#status=}"
110 [ "${date#date=}" -ge 0 ] 2>&- \
111 && date="$(date -d "@${date#date=}" +%F)" \
112 || date="$(date +%F)"
113 [ "${number#number=}" ] \
114 && number="${number#number=}" \
115 || number="$(date +%s)"
116 [ "${vatrate#vatrate=}" -ge 0 ] 2>&- \
117 && vatrate="${vatrate#vatrate=}" \
120 [ -f "clients/${client#client=}" ] \
121 && read -r caddress chourly x<"clients/${client#client=}"
122 [ "${chourly#hourly=}" -ge 0 ] 2>&- \
123 && chourly="${chourly#hourly=}" \
125 [ "${hourly#hourly=}" -ge 0 ] 2>&- \
126 && hourly="${hourly#hourly=}" \
127 || hourly="${chourly}"
129 tid="$(transid "invoices/$id")"
131 read -r taxtype nett tax gross x<<-EOF
132 $(invoice_total "$id")
136 [form method="POST" action="/update_invoice"
137 [hidden "id" "$(HTML "$id")"]
141 $(sender_list "${sender#sender=}")
146 $(client_list "${client#client=}")
149 [label for=number Invoice Number:]
150 [input #number name=number value="$(UNSTRING "${number}" |HTML)"]
152 [label for=date Date:]
153 [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
155 [label for=hourly Hourly Rate:]
156 [input #hourly type=number name=hourly value="${hourly}"]
158 [radio "vat" "smallbusiness" #vatsb $([ "${vat#vat=}" = smallbusiness ] && printf checked) ]
159 [label for=vatsb Small business exemption from VAT]
160 [radio "vat" "nett" #vatnett $([ "${vat#vat=}" = nett ] && printf checked)]
161 [label for=vatnett Nett]
162 [radio "vat" "gross" #vatgross $([ "${vat#vat=}" = gross ] && printf checked)]
163 [label for=vatgross Gross]
164 [label for=vatrate VAT Rate: [input type=number name="vatrate" value="${vatrate}"]% ]
167 [tr [th Date] [th Work] [th Hours] [th Price] ]
168 $({ sed 1d "invoices/$id"; printf 'time= work= hours=\n'; } \
169 | while read -r time work hours x; do
170 hours="$(UNSTRING "${hours#hours=}" \
171 |grep -m1 -xE '\.[0-9]+|[0-9]+\.?[0-9]*' || printf 0)"
173 [td [textarea name=time
175 [td [textarea name=work
177 [td [input type=number name=hours value="%g" step=any] ]
179 ]' "$(UNSTRING "${time#time=}" |HTML)" \
180 "$(UNSTRING "${work#work=}" |HTML)" \
182 "$(awk "BEGIN { printf \"%.2f €\", ${hours} * ${hourly}; }")"
187 (nett) printf 'Sum: %7.2f €[br] + VAT: %7.2f €[br] [strong Total:] %7.2f €' \
189 (gross) printf '[strong Total:] %7.2f €[br] incl. nett: %7.2f €[br] + VAT: %7.2f €' \
191 (*) printf '[strong Total:] %.2f €' $nett ;;
196 [option value=open $( [ $status = open ] && printf selected=selected ) Open]
197 [option value=sent $( [ $status = sent ] && printf selected=selected ) Sent]
198 [option value=resent $( [ $status = resent ] && printf selected=selected ) Resent]
199 [option value=paid $( [ $status = paid ] && printf selected=selected ) Paid]
200 [option value=cancelled $( [ $status = cancelled ] && printf selected=selected ) Cancelled]
202 [submit "genpdf" "$tid" Export PDF]
203 [submit "update" "$tid" Update]
209 local id="$1" sender client date number vat vatrate \
210 total=0 caddress hourly time work hours
212 if [ -f "invoices/$id" ]; then
213 read -r sender client date number vat vatrate hourly x<<-EOF
214 $(sed q "invoices/$id")
217 [ "${hourly#hourly=}" -gt 0 ] 2>&- \
218 && hourly="${hourly#hourly=}" \
220 [ "${vatrate#vatrate=}" -ge 0 ] 2>&- \
221 && vatrate="${vatrate#vatrate=}" \
224 sed 1d "invoices/$id" \
225 | { while read -r time work hours; do
226 [ "${hours#hours=}" ] 2>&- \
227 && hours="${hours#hours=}" \
229 total=$(awk "BEGIN { printf \"%.2f\", ${total} + ${hours} * ${hourly}; }")
234 printf \"nett %.2f %.2f %.2f\",
235 $total, int($total * $vatrate + .5) / 100,
236 $total + int($total * $vatrate + .5) / 100
240 printf \"gross %.2f %.2f %.2f\",
241 $total - int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100,
242 int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100, $total
246 printf \"notax %.2f %.2f %.2f\",
257 local id="$(POST id |checkid)" extra=0 tid
258 tid="$(transid invoices/$id)"
260 if [ "$(POST update)" = "$tid" ] || [ "$(POST genpdf)" = "$tid" ]; then
263 for n in "$(POST_COUNT time)" "$(POST_COUNT work)" "$(POST_COUNT hours)"; do
264 [ "$n" -gt "$extra" ] && extra="$n"
267 { printf 'sender=%s client=%s date=%s number=%s vat=%s vatrate=%s hourly=%s status=%s\n' \
268 "$(POST sender)" "$(POST client)" \
269 "$(date -d "$(POST date)" +%s)" \
270 "$(POST number |STRING)" \
271 "$(POST vat |grep -m1 -xE 'smallbusiness|gross|nett')" \
272 "$(POST vatrate |grep -m1 -xE '[0-9]+')" \
273 "$(POST hourly |grep -m1 -xE '[0-9]+')" \
274 "$(POST status |grep -m1 -xE 'open|sent|resent|paid|cancelled')"
275 for n in $(seq 1 $extra); do
276 printf 'time=%s work=%s hours=%s\n' \
277 "$(POST time $n |STRING)" "$(POST work $n |STRING)" \
278 "$(POST hours $n |STRING)" \
279 | grep -xvF 'time=\ work=\ hours=0'
284 git add "invoices/$id"
285 git commit -m 'Update invoice info for "'"$(POST number)"'"' -- "invoices/$id"
288 if [ "$(POST genpdf)" ]; then
289 read -r sender client date x<"invoices/$id"
290 read -r saddress x <"senders/${sender#sender=}"
291 read -r caddress x <"clients/${client#client=}"
292 filename="Rechnung $(UNSTRING "${saddress#address=}" |sed 1q) an $(UNSTRING "${caddress#address=}" |sed 1q) $(date -d@"${date#date=}" +%F).pdf"
296 REDIRECT "/export/${id}.pdf/$(URL "${filename}" |sed s/%0D//g)"
299 REDIRECT "/invoices/$id"