3 _EXEC="$(realpath "${0%/*}")"
4 . "$_EXEC/cgilite/cgilite.sh"
5 . "$_EXEC/cgilite/storage.sh"
11 printf %s\\n "$*" >/dev/stderr
16 # generate time based ID
17 # Fixme: Unix time stamps assumed to be 32bit always
20 while [ "$d" -gt 0 ]; do
21 printf \\%o\\n $((d % 256))
28 | sed -n '2{y;+/;:_;;p}'
32 grep -m 1 -xE '[0-9a-zA-Z:_]{12}';
36 printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
37 ./cgilite/html-sh.sed <<EOF
39 [meta name="viewport" content="width=device-width"]
40 [link rel="stylesheet" type="text/css" href="/invoices.css"]
44 [a "/invoices/" Invoices]
45 [a "/clients/" Clients]
46 [a "/senders/" Senders]
55 if [ -f "clients/$id" ]; then
56 read -r address hourly <"clients/$id"
58 [ "$address" ] || address="address="
59 [ "$hourly" ] || hourly="hourly="
61 [form method="POST" action="/update_client"
63 <textarea name="address" placeholder="address">
65 [label for=hourly Hourly Rate:]
66 [input #hourly type=number name=hourly value="%s"]
67 [submit "update" "update" Update]
70 "$(UNSTRING "${address#address=}" |HTML)" \
71 "$(UNSTRING "${hourly#hourly=}" |grep -xE '[0-9]+')"
76 if [ -f "senders/$id" ]; then
77 address="$(cat "senders/$id")"
79 [ "$address" ] || address="Name
90 [form method="POST" action="/update_sender"
92 <textarea name="address" placeholder="address">%s</textarea>
93 [submit "update" "update" Update]
96 "$(HTML "${address}")"
100 [ -d clients/ ] && for c in clients/*; do
101 read -r address hourly <"$c"
102 address="$(UNSTRING "${address#address=}")"
103 [ "$address" ] || address="(no address)"
104 printf '[div .client .address <!--
107 [label Hourly Rate:] %s€
108 [a href="/clients/%s" Edit]]
109 ' "$(HTML "$address")" \
110 "$(HTML "${hourly#hourly=}")" \
111 "$(HTML "${c#clients/}")"
116 [ -d senders/ ] && for s in senders/*; do
118 [ "$address" ] || address="(no address)"
119 printf '[div .sender .address <!--
120 -->%s[a href="/senders/%s" Edit]]
121 ' "$(HTML "$address")" "$(HTML "${s#senders/}")"
126 local select="$1" n name
127 [ -d senders/ ] && for n in '' senders/*; do
128 [ "$n" ] && name="$(sed q "$n" |HTML)"
129 [ "${n#senders/}" = "$select" ] \
130 && printf '<option value="%s" selected=selected>%s</option>' "${n#senders/}" "$name" \
131 || printf '<option value="%s">%s</option>' "${n#senders/}" "$name"
136 local select="$1" n address hourly name
137 [ -d clients/ ] && for n in '' clients/*; do
138 [ "$n" ] && read -r address hourly <"$n"
139 name="$(UNSTRING "${address#address=}" |sed q |HTML)"
140 [ "${n#clients/}" = "$select" ] \
141 && printf '<option value="%s" selected=selected>%s</option>' "${n#clients/}" "$name" \
142 || printf '<option value="%s">%s</option>' "${n#clients/}" "$name"
147 [ -d invoices/ ] && for i in invoices/*; do
148 read -r sender client date number vat vatrate<<-EOF
151 [ -f "senders/${sender#sender=}" ] \
152 && sender="$(sed q "senders/${sender#sender=}")" \
154 [ ! -f "clients/${client#client=}" ] \
155 && client="(unset)" \
156 || read -r client hourly <"clients/${client#client=}"
157 [ "${date#date=}" -ge 0 ] 2>&- \
158 && date="$(date -d "@${date#date=}" +%x)" \
161 printf '[div .invoice
164 [label From:] %s [label To:] %s [label on] %s
165 [a href="/invoice/%s" Edit]
166 ]' "$(UNSTRING "${number#number=}" |HTML)" \
167 "$(HTML "$sender")" \
168 "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \
169 "$(HTML ${i#invoices/})"
175 if [ -f "invoices/$id" ]; then
176 read -r sender client date number vat vatrate<<-EOF
177 $(sed q "invoices/$id")
181 [ "${date#date=}" -ge 0 ] 2>&- \
182 && date="$(date -d "@${date#date=}" +%F)" \
183 || date="$(date +%F)"
184 [ "${number#number=}" ] || number="number=$(date +%s)"
185 [ "${vatrate#vatrate=}" -ge 0 ] 2>&- || vatrate="vatrate=19"
188 [form method="POST" action="/update_invoice"
189 [hidden "id" "$(HTML "$id")"]
193 $(sender_list "${sender#sender=}")
198 $(client_list "${client#client=}")
201 [label for=number Invoice Number:]
202 [input #number name=number value="$(UNSTRING "${number#number=}" |HTML)"]
204 [label for=date Date:]
205 [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
207 [radio "vat" "smallbusiness" #vatsb $([ "${vat#vat=}" = smallbusiness ] && printf checked) ]
208 [label for=vatsb Small business exemption from VAT]
209 [radio "vat" "nett" #vatnett $([ "${vat#vat=}" = nett ] && printf checked)]
210 [label for=vatnett Nett]<!--
211 -->[radio "vat" "gross" #vatgross $([ "${vat#vat=}" = gross ] && printf checked)]<!--
212 -->[label for=vatgross Gross]
213 [label for=vatrate VAT Rate: [input type=number name="vatrate" value="${vatrate#vatrate=}"]% ]
215 [submit "update" "update" Update]
220 info="$(PATH "${PATH_INFO}")"
224 . "$_EXEC/cgilite/file.sh"
225 FILE "$_EXEC/invoices.css"
229 printf '[a .new href="/clients/%s" New]' "$(timeid)"
230 } | yield_page clients
233 edit_client "${info#/clients/}" |yield_page client
236 id="$(POST id |checkid)"
237 if [ "$(POST update)" = update -a "$id" ]; then
239 printf 'address=%s hourly=%s' \
240 "$(POST address |STRING)" "$(POST hourly |STRING)" \
243 echo Invalid Data "$(POST id)" "$(POST update)" >&2
249 printf '[a .new href="/senders/%s" New]' "$(timeid)"
250 } | yield_page senders
253 edit_sender "${info#/senders/}" |yield_page sender
256 id="$(POST id |checkid)"
257 if [ "$(POST update)" = update -a "$id" ]; then
259 POST address >"senders/$id"
265 printf '[a .new href="/invoice/%s" New]' "$(timeid)"
266 } | yield_page invoices
269 edit_invoice "${info#/invoice/}" |yield_page invoice
272 id="$(POST id |checkid)"
273 if [ "$(POST update)" = update -a "$id" ]; then
275 printf 'sender=%s client=%s date=%s number=%s vat=%s vatrate=%s\n' \
276 "$(POST sender)" "$(POST client)" \
277 "$(date -d "$(POST date)" +%s)" \
278 "$(POST number |STRING)" \
279 "$(POST vat |grep -m1 -xE 'smallbusiness|gross|nett')" \
280 "$(POST vatrate |grep -m1 -xE '[0-9]+')" \
283 REDIRECT "/invoice/$id"
285 *) REDIRECT /invoices