]> git.plutz.net Git - invoices/blob - invoices.cgi
initial commit
[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[a href="/clients/%s" Edit]]
106     ' "$(HTML "$address")" "$(HTML "${c#clients/}")"
107   done
108 }
109
110 list_senders(){
111   [ -d senders/ ] && for s in senders/*; do
112     address=$(cat "$s")
113     [ "$address" ] || address="(no address)"
114     printf '[div .sender .address <!--
115       -->%s[a href="/senders/%s" Edit]]
116     ' "$(HTML "$address")" "$(HTML "${s#senders/}")"
117   done
118 }
119
120 list_invoices(){
121   [ -d invoices/ ] && for i in invoices/*; do
122     read -r sender client date number vat<<-EOF
123         sed q "$i"
124         EOF
125     [ -f "senders/${sender#sender=}" ] \
126     && sender="$(sed q "senders/${sender#sender=}")" \
127     || sender="(unset)"
128     [ -f "clients/${client#client=}" ] \
129     && client="$(sed q "client/${client#client=}")" \
130     || client="(unset)"
131     [ "$date" -gt 0 ] \
132     && date="$(date -d @$date +%x)" \
133     || date="(unset)"
134
135     printf '[div .invoice
136       [h2
137           %s]
138       [label From:] %s [label To:] %s [label on] %s
139       [a href="/invoices/%s" Edit]
140     ]' "$(HTML "$number")" "$(HTML "$sender")" \
141        "$(HTML "$client")" "$(HTML "$date")"
142   done
143 }
144
145 info="$(PATH "${PATH_INFO}")"
146
147 case $info in
148   /invoices.css)
149     . "$_EXEC/cgilite/file.sh"
150     FILE "$_EXEC/invoices.css"
151     ;;
152   /clients)
153     { list_clients
154       printf '[a .new href="/clients/%s" New]' "$(timeid)"
155     } | yield_page clients
156     ;;
157   /clients/*)
158     edit_client "${info#/clients/}" |yield_page client
159     ;;
160   /update_client)
161     id="$(POST id |checkid)"
162     if [ "$(POST update)" = update -a "$id" ]; then
163       mkdir -p clients
164       printf 'address=%s        hourly=%s' \
165         "$(POST address |STRING)" "$(POST hourly |STRING)" \
166         >"clients/$id"
167     else
168       echo Invalid Data "$(POST id)" "$(POST update)" >&2
169     fi
170     REDIRECT /clients/
171     ;;
172   /senders)
173     { list_senders
174       printf '[a .new href="/senders/%s" New]' "$(timeid)"
175     } | yield_page senders
176     ;;
177   /senders/*)
178     edit_sender "${info#/senders/}" |yield_page sender
179     ;;
180   /update_sender)
181     id="$(POST id |checkid)"
182     if [ "$(POST update)" = update -a "$id" ]; then
183       mkdir -p senders
184       POST address >"senders/$id"
185     fi
186     REDIRECT /senders/
187     ;;
188   /invoices)
189     { list_invoices
190       printf '[a href="/invoices/%s" New]' "$(timeid)"
191     } | yield_page invoices
192     ;;
193   /invoice/*)
194     edit_invoice "${info#/invoices/}" |yield_page invoice
195     ;;
196   *) REDIRECT /invoices
197     ;;
198 esac