./cgilite/html-sh.sed <<EOF
[html [head
[meta name="viewport" content="width=device-width"]
- [link rel="stylesheet" type="text/css" href="invoices.css"]
+ [link rel="stylesheet" type="text/css" href="/invoices.css"]
[title Invoices]
] [body class="$1"
[div #menu
address="$(UNSTRING "${address#address=}")"
[ "$address" ] || address="(no address)"
printf '[div .client .address <!--
- -->%s[a href="/clients/%s" Edit]]
- ' "$(HTML "$address")" "$(HTML "${c#clients/}")"
+ -->%s
+
+[label Hourly Rate:] %s€
+ [a href="/clients/%s" Edit]]
+ ' "$(HTML "$address")" \
+ "$(HTML "${hourly#hourly=}")" \
+ "$(HTML "${c#clients/}")"
done
}
done
}
+sender_list(){
+ local select="$1" n name
+ [ -d senders/ ] && for n in '' senders/*; do
+ [ "$n" ] && name="$(sed q "$n" |HTML)"
+ [ "${n#senders/}" = "$select" ] \
+ && printf '<option value="%s" selected=selected>%s</option>' "${n#senders/}" "$name" \
+ || printf '<option value="%s">%s</option>' "${n#senders/}" "$name"
+ done
+}
+
+client_list(){
+ local select="$1" n address hourly name
+ [ -d clients/ ] && for n in '' clients/*; do
+ [ "$n" ] && read -r address hourly <"$n"
+ name="$(UNSTRING "${address#address=}" |sed q |HTML)"
+ [ "${n#clients/}" = "$select" ] \
+ && printf '<option value="%s" selected=selected>%s</option>' "${n#clients/}" "$name" \
+ || printf '<option value="%s">%s</option>' "${n#clients/}" "$name"
+ done
+}
+
list_invoices(){
[ -d invoices/ ] && for i in invoices/*; do
read -r sender client date number vat<<-EOF
- sed q "$i"
+ $(sed q "$i")
EOF
[ -f "senders/${sender#sender=}" ] \
&& sender="$(sed q "senders/${sender#sender=}")" \
|| sender="(unset)"
- [ -f "clients/${client#client=}" ] \
- && client="$(sed q "client/${client#client=}")" \
- || client="(unset)"
- [ "$date" -gt 0 ] \
- && date="$(date -d @$date +%x)" \
+ [ ! -f "clients/${client#client=}" ] \
+ && client="(unset)" \
+ || read -r client hourly <"clients/${client#client=}"
+ [ "${date#date=}" -ge 0 ] 2>&- \
+ && date="$(date -d "@${date#date=}" +%x)" \
|| date="(unset)"
printf '[div .invoice
[h2
%s]
[label From:] %s [label To:] %s [label on] %s
- [a href="/invoices/%s" Edit]
- ]' "$(HTML "$number")" "$(HTML "$sender")" \
- "$(HTML "$client")" "$(HTML "$date")"
+ [a href="/invoice/%s" Edit]
+ ]' "$(UNSTRING "${number#number=}" |HTML)" \
+ "$(HTML "$sender")" \
+ "$(UNSTRING "${client#address=}" |sed q |HTML)" "$(HTML "$date")" \
+ "$(HTML ${i#invoices/})"
done
}
+edit_invoice(){
+ id="$1"
+ if [ -f "invoices/$id" ]; then
+ read -r sender client date number vat<<-EOF
+ $(sed q "invoices/$id")
+ EOF
+ fi
+
+ [ "${date#date=}" -ge 0 ] 2>&- \
+ && date="$(date -d "@${date#date=}" +%F)" \
+ || date="$(date +%F)"
+ [ "${number#number=}" ] || number="number=$(date +%s)"
+ [ "${vat%=*}" = vat -a "${vat#vat=}" -ge 0 ] 2>&- \
+ && vatrate="${vat#vat=}" \
+ || vatrate=19
+
+ cat <<-EOF
+ [form method="POST" action="/update_invoice"
+ [hidden "id" "$(HTML "$id")"]
+
+ [label Sender:]
+ [select name=sender
+ $(sender_list "${sender#sender=}")
+ ]
+
+ [label Client:]
+ [select name=client
+ $(client_list "${client#client=}")
+ ]
+
+ [label for=number Invoice Number:]
+ [input #number name=number value="$(UNSTRING "${number#number=}" |HTML)"]
+
+ [label for=date Date:]
+ [input #date name=date value="${date}" placeholder="YYYY-MM-TT"]
+
+ [radio "vat" "smallbusiness" #vatsb $([ "$vat" = smallbusiness ] && printf checked) ]
+ [label for=vatsb Small business exemption from VAT]
+ [radio "vat" "allgross" #vatgross $([ "$vat" = allgross ] && printf checked)]
+ [label for=vatgross All amounts are gross]
+ [radio "vat" "rate" #vatrate $([ "${vat%=*}" = vat ] && printf checked) ]
+ [label for=vatrate VAT Rate: [input type=number name="vatrate" value="${vatrate}"]% ]
+
+ [submit "update" "update" Update]
+ ]
+ EOF
+}
+
info="$(PATH "${PATH_INFO}")"
case $info in
;;
/invoices)
{ list_invoices
- printf '[a href="/invoices/%s" New]' "$(timeid)"
+ printf '[a .new href="/invoice/%s" New]' "$(timeid)"
} | yield_page invoices
;;
/invoice/*)
- edit_invoice "${info#/invoices/}" |yield_page invoice
+ edit_invoice "${info#/invoice/}" |yield_page invoice
+ ;;
+ /update_invoice)
+ id="$(POST id |checkid)"
+ if [ "$(POST update)" = update -a "$id" ]; then
+ mkdir -p invoices
+ vat="$(POST vat |grep -m1 -xE 'smallbusiness|allgross|rate' )"
+ printf 'sender=%s client=%s date=%s number=%s %s\n' \
+ "$(POST sender)" "$(POST client)" \
+ "$(date -d "$(POST date)" +%s)" \
+ "$(POST number |STRING)" \
+ "$([ "$vat" = rate ] && printf 'vat=%i' "$(POST vatrate)" || printf %s "$vat" )" \
+ >"invoices/$id"
+ fi
+ REDIRECT "/invoice/$id"
;;
*) REDIRECT /invoices
;;
body {
color: black; background-color: white;
}
+
+
+/* ==================== MENU ==================== */
+
#menu {
margin: 0; padding: .5em 1em;
border-bottom: 1px solid black;
border-color: #888;
}
+
+/* ==================== ADDRESS LISTS ==================== */
+
+.invoices .invoice,
.clients .address,
.senders .address {
display: inline-block;
margin: 1em 0 0 1em;
width: 20em;
}
+.invoices .invoice {
+ white-space: normal;
+}
+.invoices .invoice h2{
+ display: block;
+ letter-spacing: .25em;
+ text-align: center;
+ margin-bottom: -.5em;
+ font-size: 1.125em;
+}
+.invoices .invoice label {
+ font-weight: bold;
+}
+.invoices .invoice label::before {
+ white-space: pre;
+ content: '\0A';
+}
+.invoices .invoice a,
.clients .address a,
.senders .address a,
a.new {
padding: .125em 1em;
border: 1px solid #08F;
}
+.invoices .invoice a:hover,
.clients .address a:hover,
.senders .address a:hover,
a.new:hover {
margin: 1em;
}
+
+/* ==================== ADDRESS FORMS ==================== */
+
+form select,
form textarea,
form input {
- padding: .25em .5em;
+ padding: .125em .5em;
background-color: #FFF;
text-align: left;
}
+form input[type=number] {
+ width: 5em;
+ text-align: right;
+}
+
+.invoice form,
.client form,
.sender form {
position: relative;
text-align: right;
font-weight: bold;
}
-.client form input#hourly {
- min-width: 4em; width: 5em;
- text-align: right;
-}
+.invoice form button[type=submit],
.client form button[type=submit],
.sender form button[type=submit] {
display: block;
border-radius: .25em;
border: 1px solid #08F;
}
+
+/* ==================== INVOICE FORMS ==================== */
+.invoice form select {
+ display: inline-block;
+ min-width: 20em;
+ margin-bottom: .5em;
+}
+
+.invoice form label {
+ font-weight: bold;
+ line-height: 2em;
+}
+
+.invoice form label::before {
+ white-space: pre;
+ content: '\0A';
+}
+.invoice form label:first-of-type::before {
+ content: '';
+}
+
+.invoice form input[type=radio] {display: none;}
+.invoice form label[for^=vat] {
+ display: block;
+ text-align: left;
+ line-height: 1.5em;
+}
+.invoice form label[for^=vat]::before {
+ content: '[ ] ';
+}
+.invoice form input:checked + label[for^=vat]::before {
+ content: '[x] ';
+}
+.invoice form label[for=vatrate] {
+ float: left;
+ display: inline-block;
+}