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<<-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<<-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 [ "${vat%=*}" = vat -a "${vat#vat=}" -ge 0 ] 2>&- \
186 && vatrate="${vat#vat=}" \
190 [form method="POST" action="/update_invoice"
191 [hidden "id" "$(HTML "$id")"]
195 $(sender_list "${sender#sender=}")
200 $(client_list "${client#client=}")
203 [label for=number Invoice Number:]
204 [input #number name=number value="$(UNSTRING "${number#number=}" |HTML)"]
206 [label for=date Date:]
207 [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
209 [radio "vat" "smallbusiness" #vatsb $([ "$vat" = smallbusiness ] && printf checked) ]
210 [label for=vatsb Small business exemption from VAT]
211 [radio "vat" "allgross" #vatgross $([ "$vat" = allgross ] && printf checked)]
212 [label for=vatgross All amounts are gross]
213 [radio "vat" "rate" #vatrate $([ "${vat%=*}" = vat ] && printf checked) ]
214 [label for=vatrate VAT Rate: [input type=number name="vatrate" value="${vatrate}"]% ]
216 [submit "update" "update" Update]
221 info="$(PATH "${PATH_INFO}")"
225 . "$_EXEC/cgilite/file.sh"
226 FILE "$_EXEC/invoices.css"
230 printf '[a .new href="/clients/%s" New]' "$(timeid)"
231 } | yield_page clients
234 edit_client "${info#/clients/}" |yield_page client
237 id="$(POST id |checkid)"
238 if [ "$(POST update)" = update -a "$id" ]; then
240 printf 'address=%s hourly=%s' \
241 "$(POST address |STRING)" "$(POST hourly |STRING)" \
244 echo Invalid Data "$(POST id)" "$(POST update)" >&2
250 printf '[a .new href="/senders/%s" New]' "$(timeid)"
251 } | yield_page senders
254 edit_sender "${info#/senders/}" |yield_page sender
257 id="$(POST id |checkid)"
258 if [ "$(POST update)" = update -a "$id" ]; then
260 POST address >"senders/$id"
266 printf '[a .new href="/invoice/%s" New]' "$(timeid)"
267 } | yield_page invoices
270 edit_invoice "${info#/invoice/}" |yield_page invoice
273 id="$(POST id |checkid)"
274 if [ "$(POST update)" = update -a "$id" ]; then
276 vat="$(POST vat |grep -m1 -xE 'smallbusiness|allgross|rate' )"
277 printf 'sender=%s client=%s date=%s number=%s %s\n' \
278 "$(POST sender)" "$(POST client)" \
279 "$(date -d "$(POST date)" +%s)" \
280 "$(POST number |STRING)" \
281 "$([ "$vat" = rate ] && printf 'vat=%i' "$(POST vatrate)" || printf %s "$vat" )" \
284 REDIRECT "/invoice/$id"
286 *) REDIRECT /invoices