]> git.plutz.net Git - invoices/blob - invoices.cgi
store vat type and vat rate individually
[invoices] / invoices.cgi
1 #!/bin/sh
2
3 _EXEC="$(realpath "${0%/*}")"
4 . "$_EXEC/cgilite/cgilite.sh"
5 . "$_EXEC/cgilite/storage.sh"
6
7 debug(){
8   if [ $# = 0 ]; then
9     tee /dev/stderr
10   else
11     printf %s\\n "$*" >/dev/stderr
12   fi
13 }
14
15 timeid(){
16   # generate time based ID
17   # Fixme: Unix time stamps assumed to be 32bit always
18   d=$(date +%s)
19   { printf $(
20       while [ "$d" -gt 0 ]; do
21         printf \\%o\\n $((d % 256))
22         d=$((d / 256))
23       done |tac |tr -d \\n
24     )
25     head -c5 /dev/urandom
26   } \
27   | uuencode -m - \
28   | sed -n '2{y;+/;:_;;p}'
29 }
30
31 checkid(){
32   grep -m 1 -xE '[0-9a-zA-Z:_]{12}';
33 }
34
35 yield_page(){
36 printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
37 ./cgilite/html-sh.sed <<EOF
38 [html [head
39   [meta name="viewport" content="width=device-width"]
40   [link rel="stylesheet" type="text/css" href="/invoices.css"]
41   [title Invoices]
42 ] [body class="$1"
43   [div #menu
44     [a "/invoices/" Invoices]
45     [a "/clients/" Clients]
46     [a "/senders/" Senders]
47   ]
48   $(cat)
49 ] ]
50 EOF
51 }
52
53 edit_client(){
54   id="$1"
55   if [ -f "clients/$id" ]; then
56     read -r address hourly <"clients/$id"
57   fi
58   [ "$address" ] || address="address="
59   [  "$hourly" ] ||  hourly="hourly="
60   printf '
61     [form method="POST" action="/update_client"
62       [hidden "id" "%s"]
63       <textarea name="address" placeholder="address">
64 %s</textarea>
65       [label for=hourly Hourly Rate:]
66       [input #hourly type=number name=hourly value="%s"]
67       [submit "update" "update" Update]
68     ]' \
69     "$(HTML $id)" \
70     "$(UNSTRING "${address#address=}" |HTML)" \
71     "$(UNSTRING "${hourly#hourly=}" |grep -xE '[0-9]+')"
72 }
73
74 edit_sender(){
75   id="$1"
76   if [ -f "senders/$id" ]; then
77     address="$(cat "senders/$id")"
78   fi
79   [ "$address" ] || address="Name
80 Street
81 City
82
83 Phone:
84 000 000000
85
86 Tax no.
87 xxx / 000 / ###
88 "
89   printf '
90     [form method="POST" action="/update_sender"
91       [hidden "id" "%s"]
92       <textarea name="address" placeholder="address">%s</textarea>
93       [submit "update" "update" Update]
94     ]' \
95     "$(HTML $id)" \
96     "$(HTML "${address}")"
97 }
98
99 list_clients(){
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 <!--
105       -->%s
106
107 [label Hourly Rate:] %s€
108     [a href="/clients/%s" Edit]]
109     ' "$(HTML "$address")" \
110       "$(HTML "${hourly#hourly=}")" \
111       "$(HTML "${c#clients/}")"
112   done
113 }
114
115 list_senders(){
116   [ -d senders/ ] && for s in senders/*; do
117     address=$(cat "$s")
118     [ "$address" ] || address="(no address)"
119     printf '[div .sender .address <!--
120       -->%s[a href="/senders/%s" Edit]]
121     ' "$(HTML "$address")" "$(HTML "${s#senders/}")"
122   done
123 }
124
125 sender_list(){
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"
132   done
133 }
134
135 client_list(){
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"
143   done
144 }
145
146 list_invoices(){
147   [ -d invoices/ ] && for i in invoices/*; do
148     read -r sender client date number vat vatrate<<-EOF
149         $(sed q "$i")
150         EOF
151     [ -f "senders/${sender#sender=}" ] \
152     && sender="$(sed q "senders/${sender#sender=}")" \
153     || sender="(unset)"
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)" \
159     || date="(unset)"
160
161     printf '[div .invoice
162       [h2
163           %s]
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/})"
170   done
171 }
172
173 edit_invoice(){
174   id="$1"
175   if [ -f "invoices/$id" ]; then
176     read -r sender client date number vat vatrate<<-EOF
177         $(sed q "invoices/$id")
178         EOF
179   fi
180
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"
186
187   cat <<-EOF 
188         [form method="POST" action="/update_invoice"
189           [hidden "id" "$(HTML "$id")"]
190
191           [label Sender:]
192           [select name=sender
193             $(sender_list "${sender#sender=}")
194           ]
195
196           [label Client:]
197           [select name=client
198             $(client_list "${client#client=}")
199           ]
200         
201           [label for=number Invoice Number:]
202           [input #number name=number value="$(UNSTRING "${number#number=}" |HTML)"]
203
204           [label for=date Date:]
205           [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
206
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=}"]% ]
214
215           [submit "update" "update" Update]
216         ]
217         EOF
218 }
219
220 info="$(PATH "${PATH_INFO}")"
221
222 case $info in
223   /invoices.css)
224     . "$_EXEC/cgilite/file.sh"
225     FILE "$_EXEC/invoices.css"
226     ;;
227   /clients)
228     { list_clients
229       printf '[a .new href="/clients/%s" New]' "$(timeid)"
230     } | yield_page clients
231     ;;
232   /clients/*)
233     edit_client "${info#/clients/}" |yield_page client
234     ;;
235   /update_client)
236     id="$(POST id |checkid)"
237     if [ "$(POST update)" = update -a "$id" ]; then
238       mkdir -p clients
239       printf 'address=%s        hourly=%s' \
240         "$(POST address |STRING)" "$(POST hourly |STRING)" \
241         >"clients/$id"
242     else
243       echo Invalid Data "$(POST id)" "$(POST update)" >&2
244     fi
245     REDIRECT /clients/
246     ;;
247   /senders)
248     { list_senders
249       printf '[a .new href="/senders/%s" New]' "$(timeid)"
250     } | yield_page senders
251     ;;
252   /senders/*)
253     edit_sender "${info#/senders/}" |yield_page sender
254     ;;
255   /update_sender)
256     id="$(POST id |checkid)"
257     if [ "$(POST update)" = update -a "$id" ]; then
258       mkdir -p senders
259       POST address >"senders/$id"
260     fi
261     REDIRECT /senders/
262     ;;
263   /invoices)
264     { list_invoices
265       printf '[a .new href="/invoice/%s" New]' "$(timeid)"
266     } | yield_page invoices
267     ;;
268   /invoice/*)
269     edit_invoice "${info#/invoice/}" |yield_page invoice
270     ;;
271   /update_invoice)
272     id="$(POST id |checkid)"
273     if [ "$(POST update)" = update -a "$id" ]; then
274       mkdir -p invoices
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]+')" \
281       >"invoices/$id"
282     fi
283     REDIRECT "/invoice/$id"
284     ;;
285   *) REDIRECT /invoices
286     ;;
287 esac