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 "$(line <$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 "$(line <$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=}" |grep -m1 -xE '[0-9]+' || printf 0)"
172 [td [textarea name=time
174 [td [textarea name=work
176 [td [input type=number name=hours value="%s"] ]
178 ]' "$(UNSTRING "${time#time=}" |HTML)" \
179 "$(UNSTRING "${work#work=}" |HTML)" \
180 "$hours" "$((hours * hourly)) €"
185 (nett) printf 'Sum: %7.2f €[br] + VAT: %7.2f €[br] [strong Total:] %7.2f €' \
187 (gross) printf '[strong Total:] %7.2f €[br] incl. nett: %7.2f €[br] + VAT: %7.2f €' \
189 (*) printf '[strong Total:] %.2f €' $nett ;;
194 [option value=open $( [ $status = open ] && printf selected=selected ) Open]
195 [option value=sent $( [ $status = sent ] && printf selected=selected ) Sent]
196 [option value=resent $( [ $status = resent ] && printf selected=selected ) Resent]
197 [option value=paid $( [ $status = paid ] && printf selected=selected ) Paid]
198 [option value=cancelled $( [ $status = cancelled ] && printf selected=selected ) Cancelled]
200 [submit "genpdf" "$tid" Export PDF]
201 [submit "update" "$tid" Update]
207 local id="$1" sender client date number vat vatrate \
208 total=0 caddress hourly time work hours
210 if [ -f "invoices/$id" ]; then
211 read -r sender client date number vat vatrate hourly x<<-EOF
212 $(sed q "invoices/$id")
215 [ "${hourly#hourly=}" -gt 0 ] 2>&- \
216 && hourly="${hourly#hourly=}" \
218 [ "${vatrate#vatrate=}" -ge 0 ] 2>&- \
219 && vatrate="${vatrate#vatrate=}" \
222 sed 1d "invoices/$id" \
223 | { while read -r time work hours; do
224 [ "${hours#hours=}" -gt 0 ] 2>&- \
225 && hours="${hours#hours=}" \
227 total=$((total + hours * hourly))
232 printf \"nett %.2f %.2f %.2f\",
233 $total, int($total * $vatrate + .5) / 100,
234 $total + int($total * $vatrate + .5) / 100
238 printf \"gross %.2f %.2f %.2f\",
239 $total - int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100,
240 int($total / (100 + $vatrate) * $vatrate * 100 + .5) / 100, $total
244 printf \"notax %.2f %.2f %.2f\",
255 local id="$(POST id |checkid)" extra=0 tid
256 tid="$(transid invoices/$id)"
258 if [ "$(POST update)" = "$tid" ] || [ "$(POST genpdf)" = "$tid" ]; then
261 for n in "$(POST_COUNT time)" "$(POST_COUNT work)" "$(POST_COUNT hours)"; do
262 [ "$n" -gt "$extra" ] && extra="$n"
265 { printf 'sender=%s client=%s date=%s number=%s vat=%s vatrate=%s hourly=%s status=%s\n' \
266 "$(POST sender)" "$(POST client)" \
267 "$(date -d "$(POST date)" +%s)" \
268 "$(POST number |STRING)" \
269 "$(POST vat |grep -m1 -xE 'smallbusiness|gross|nett')" \
270 "$(POST vatrate |grep -m1 -xE '[0-9]+')" \
271 "$(POST hourly |grep -m1 -xE '[0-9]+')" \
272 "$(POST status |grep -m1 -xE 'open|sent|resent|paid|cancelled')"
273 for n in $(seq 1 $extra); do
274 printf 'time=%s work=%s hours=%s\n' \
275 "$(POST time $n |STRING)" "$(POST work $n |STRING)" \
276 "$(POST hours $n |STRING)" \
277 | grep -xvF 'time= work= hours=0'
282 git add "invoices/$id"
283 git commit -m 'Update invoice info for "'"$(POST number)"'"' -- "invoices/$id"
286 if [ "$(POST genpdf)" ]; then
287 read -r sender client date x<"invoices/$id"
288 read -r saddress x <"senders/${sender#sender=}"
289 read -r caddress x <"clients/${client#client=}"
290 filename="Rechnung $(UNSTRING "${saddress#address=}" |sed 1q) an $(UNSTRING "${caddress#address=}" |sed 1q) $(date -d@"${date#date=}" +%F).pdf"
294 REDIRECT "/export/${id}.pdf/$(URL "${filename}" |sed s/%0D//g)"
297 REDIRECT "/invoices/$id"