]> git.plutz.net Git - confetti/blob - ledgers/index.cgi
Merge commit 'de473d4e02928b374781a1930e052dfb215a6201'
[confetti] / ledgers / index.cgi
1 #!/bin/sh
2
3 . "$_EXEC/cgilite/storage.sh"
4 . "$_EXEC/pdiread.sh"
5
6 credit() {
7   printf '%03i\n' "$1" \
8   | sed -E 's;[0-9]{2}$;d&;; :0 s;([0-9])([0-9]{3}[dm]);\1m\2;; t0; y;dm;,.;'
9 }
10
11 { printf '
12   [form .upload action="%s/ledgers/csv_upload.sh" method="POST" enctype="multipart/form-data"
13     [label for=ledger_upload . %s:]
14     [input #ledger_upload type="file" name="csv" accept=".csv,text/csv"]
15     [input type=hidden name=session_id value="%s"]
16     [button type="submit" %s]
17   ]' \
18   "${_BASE}" "$(l10n "Postbank CSV")" "$SESSION_ID" "$(l10n Upload)"
19   printf '
20   [form .ledgers action="%s/ledgers/delete.sh" method=POST
21     [input type=hidden name=session_id value="%s"]
22     [h3 . %s]
23   ' "${_BASE}" "$SESSION_ID" "$(l10n Ledgers)"
24   for ledger in "$_DATA"/ledgers/????-??-??\ -\ ????-??-??\ -\ ????.tbl; do
25     ledger="${ledger##*/}"
26     [ "$ledger" = "????-??-?? - ????-??-?? - ????.tbl" ] && continue
27     printf '[p .ledger . %s [button type=submit name=delete value="%s" . %s]]' \
28            "$(HTML "${ledger% - ????.tbl}")" "$(HTML "$ledger")" "$(l10n delete)"
29   done
30   printf '  ]'
31   printf '
32   [form .ibanassign action="%s/ledgers/iban_assign.sh" method=POST
33     [input type=hidden name=session_id value="%s"]
34     [h3 . %s]
35   ' "${_BASE}" "$SESSION_ID" "$(l10n "IBAN Assignments")"
36   printf '[datalist id=lattendants .'
37   pdi_load "${_DATA}"/vcard/*.vcf |sed -n '/^FN\;:/!b; s;^FN\;:;;; p;' \
38   | while read name; do
39     printf '[option value="%s"]' "$(HTML "$name")"
40   done
41   printf ']'
42   l10n_attendant="$(l10n attendant)"
43   { pdi_load "${_DATA}"/vcard/*.vcf
44     printf 'BEGIN:LEDGERS\n'
45     cat "${_DATA}"/ledgers/????-??-??\ -\ ????-??-??\ -\ ????.tbl
46   } | "${_EXEC}"/ledgers/iban_assign.awk \
47   | while read -r state iban data; do
48     iban="$(UNSTRING "$iban")"
49     [ ! "$iban" ] && iban="??????????"
50     printf '[input type=checkbox id="use_%s" name="use_%s" value=true]' "$iban" "$iban"
51     printf '[fieldset .iban .%s [legend . %s ]' \
52            "$state" "$iban"
53     if   [ $state = sure ]; then
54       for card in $data; do
55         uid="${card%%/*}" name="$(UNSTRING "${card#*/}")"
56         printf '[span .card . %s]' "$(HTML "${name}")"
57       done
58       :
59     elif [ $state = guess ]; then
60       record="$(UNSTRING "${data%%      *}")"
61       cards="${data#*   }"
62       date="${record%%  *}"
63       principal="${record#*     *       *       }" principal="${principal%%     *}"
64       subject="${record#*       *       *       *       }" subject="${subject%% *}"
65       amount="${record#*        *       *       *       *       }" amount="${amount%%   *}"
66       printf '[p .principal . %s][p .date %s][p .amount %s][p .subject . %s]' \
67              "$(UNSTRING "$principal" |HTML)" "$date" "$(credit "$amount")" "$(UNSTRING "$subject" |HTML)"
68       n=0; for card in $cards; do
69         n=$((n+1)); uid="${card%%/*}" name="$(UNSTRING "${card#*/}")"
70         cat <<-EOF
71           [input type=checkbox id="check_${iban}_$n" name="check_${iban}_$n" value=true checked=checked]
72           [input .card name="fn_${iban}_$n" value="$(HTML "$name")" .disabled tabindex="-1"]
73           [label .del for="check_${iban}_$n" . -]
74         EOF
75       done
76       for m in 1 2 3 4 5 6 7 8; do
77         cat <<-EOF
78           [input type=checkbox id="check_${iban}_$((n+m))" name="check_${iban}_$((n+m))" value=true]
79           [input .card name="fn_${iban}_$((n+m))" value="" placeholder="${l10n_attendant}" list="lattendants"]
80           [label .add for="check_${iban}_$((n+m))" . +]
81         EOF
82       done
83       printf '[label .button for="use_%s" . %s]' "$iban" "$(l10n Accept Suggestions)"
84       printf '[label .button for="use_%s" . %s]' "$iban" "$(l10n Ignore Suggestions)"
85     elif [ $state = unknown ]; then
86       date="${data%%    *}"
87       principal="${data#*       *       *       }" principal="${principal%%     *}"
88       subject="${data#* *       *       *       }" subject="${subject%% *}"
89       amount="${data#*  *       *       *       *       }" amount="${amount%%   *}"
90       printf '[p .principal . %s][p .date %s][p .amount %s][p .subject . %s]' \
91              "$(UNSTRING "$principal" |HTML)" "$date" "$(credit "$amount")" "$(UNSTRING "$subject" |HTML)"
92       printf '[input name="check_" type=hidden][input type=hidden][label .del]'
93       n=0; for m in 1 2 3 4 5 6 7 8; do
94         cat <<-EOF
95           [input type=checkbox id="check_${iban}_$((n+m))" name="check_${iban}_$((n+m))" value=false]
96           [input .card name="fn_${iban}_$((n+m))" value="" placeholder="${l10n_attendant}" list="lattendants"]
97           [label .add for="check_${iban}_$((n+m))" . +]
98         EOF
99       done
100       printf '[label .button for="use_%s" . %s]' "$iban" "$(l10n Accept Suggestions)"
101       printf '[label .button for="use_%s" . %s]' "$iban" "$(l10n Ignore Suggestions)"
102     fi
103   printf ']'
104   done
105   printf '[button type=submit . %s]' "$(l10n Submit Changes)"
106   printf '  ]'
107 } | yield_page ledgers