]> git.plutz.net Git - confetti/blobdiff - ledgers/account.sh
basic update-function for ledgers
[confetti] / ledgers / account.sh
index d9d3f4bd08771b606c2c587f5300d43ccace7ffc..9d3329936e08d25ee74c3248b7bff216bd913fb1 100755 (executable)
@@ -1,12 +1,11 @@
 #!/bin/sh
 
 credit() {
-  printf '%+03i\n' "$1" \
+  printf '%+04i\n' "$1" \
   | sed -E 's;[0-9]{2}$;d&;; :0 s;([0-9])([0-9]{3}[dm]);\1m\2;; t0; y;dm;,.;'
 }
 
-if [ "$REQUEST_METHOD" = POST ]; then
-  uid="$(POST uid)"
+if uid="$(POST uid)"; then
   cfile="$(grep -lxF "UID;:${uid}" "${_DATA}/vcard/"*.vcf || grep -lxF "UID:${uid}" "${_DATA}/vcard/"*.vcf)"
   REDIRECT "${_BASE}/ledgers/account.sh?card=${cfile##*/}"
 fi
@@ -15,20 +14,45 @@ fi
 . "${_EXEC}/pdiread.sh"
 . "${_EXEC}/cards/l10n.sh"
 . "${_EXEC}/cards/widgets.sh"
+. "${_EXEC}/datetime.sh"
 
-cardfile="${_DATA}/vcard/$(GET card |PATH)"
-if [ ! -f "$cardfile" ]; then
+cardfile="$(GET card |PATH)" cardfile="${cardfile##*/}"
+if [ ! -f "${_DATA}/vcard/$cardfile" ]; then
   SET_COOKIE 0 message="Invalid account: $cardfile"
   REDIRECT "${_BASE}/ledgers/"
 fi
 
-cledger="${cardfile##*/}"
-cledger="${_DATA}/ledgers/vcf.${cledger%.vcf}.account"
+cledger="${_DATA}/ledgers/vcf.${cardfile%.vcf}.account"
 
-{ card="$(pdi_load "$cardfile")"
+if tid="$(POST tid)"; then
+  if [ "$tid" != "$(transid "$cledger")" ]; then
+    SET_COOKIE 0 message="Ledger was changed since last edit"
+    REDIRECT "$REQUEST_URI"
+  fi
+  tdate="$(isdate "$(POST tdate)")"
+  tref="$(POST tref |grep -m1 -xE '.+')"
+  tamount="$(POST tamount \
+             | sed -E '
+             s;^([\+-]?[0-9]+)[\.,]([0-9][0-9])$;\1\2;;
+             s;^([\+-]?[0-9]+)$;&00;;
+           ' | grep -m1 -xE '[\+-]?[0-9]+')"
+  # debug "TDATE: $tdate TREF: $tref AMOUNT: $tamount"
+  if ! [ "$tdate" -a "$tref" -a "$tamount" ]; then
+    SET_COOKIE 0 message="Transaction info invalid"
+    REDIRECT "$REQUEST_URI"
+  fi
+  tdtstamp="$(date -ud "$tdate" +%s)"
+  printf '%s   %i      %s      \       %s      %i\n' \
+         "${tdate}" "${tdtstamp}" "$(STRING "${cardfile%.vcf}")" \
+         "$(STRING "$tref")" "${tamount}" \
+         >>"${cledger}"
+  REDIRECT "$REQUEST_URI"
+fi
+
+{ card="$(pdi_load "${_DATA}/vcard/$cardfile")"
   cat <<-EOF
        [h1 $(l10n Payments)]
-       [div .card #${cardfile##*/}
+       [div .card #${cardfile}
          [div .section .basic . $(
            card_item "$card" FN GENDER NICKNAME BDAY X-ZACK-JOINDATE X-ZACK-LEAVEDATE SOUND PHOTO LOGO
          )]
@@ -37,7 +61,7 @@ cledger="${_DATA}/ledgers/vcf.${cledger%.vcf}.account"
          [div .section .address . $(card_item "$card" ADR X-IBAN)]
          [div .section .note    . $(card_item "$card" NOTE)]
          [div .section .attendance [h3 $(l10n course_attendance) ] [ul
-           $(grep -F "     ${cardfile##*/}" "$_DATA/mappings/attendance" |while read each discard; do
+           $(grep -F " ${cardfile}" "$_DATA/mappings/attendance" |while read each discard; do
              printf '[li [a .item .attendance href="%s/courses#%s" . %s]]' \
                     "${_BASE}" "$each" \
                     "$(pdi_value "$(pdi_load "$_DATA/ical/$each")" SUMMARY || l10n "(unnamed course)" |unescape |HTML)"
@@ -46,43 +70,57 @@ cledger="${_DATA}/ledgers/vcf.${cledger%.vcf}.account"
          ]
        ]
        EOF
-  printf '[table .transactions [thead
-          [tr [th .date . %s][th .orig . %s][th .reference . %s][th .amount . %s][th .balance . %s]]' \
-         "$(l10n Date)" "$(l10n Originator)" "$(l10n "Reference Text")" "$(l10n Amount)" "$(l10n Balance)"
-  printf '][tbody'
+  cat <<-EOF
+       [form method=POST
+         [hidden "tid" "$(transid "${cledger}")"]
+         [table .transactions
+           [thead [tr
+             [th .date . $(l10n Date)][th .orig . $(l10n Originator)]
+             [th .reference . $(l10n "Reference Text")]
+             [th .amount . $(l10n Amount)][th .balance . $(l10n Balance)]
+           ]]
+           [tbody
+       EOF
   cnt="$(pdi_count "$card" X-IBAN)"
-  while [ "$cnt" -gt 0 ]; do
-    pdi_value "$card" X-IBAN "$cnt" |RXLITERAL
-    cnt=$((cnt - 1))
-  done \
-  | {
-    while read -r iban; do
-      grep -hE "^[^\t]+        [^\t]+  ${iban} " "${_DATA}/ledgers/"*.tbl
+  { while [ "$cnt" -gt 0 ]; do
+      pdi_value "$card" X-IBAN "$cnt" |RXLITERAL
+      cnt=$((cnt - 1))
     done
-    if [ -f "$cledger" ]; then
-      :
-    fi
-  } \
+    RXLITERAL "${cardfile%.vcf}"; echo
+  } |debug \
+  | while read -r iban; do
+    grep -hE "^[^\t]+  [^\t]+  ${iban} " "${_DATA}/ledgers/"*
+  done \
   | sort -n -k2 \
   | { total=0
-    while read -r date dtstamp iban accname subject amount; do
+    while read -r date dtstamp iban accname reftext amount; do
       total=$((total + amount))
-      printf '[tr [td .date . %s][td .orig [span . %s][span . %s]][td .reference . %s][td .amount . %s][td .balance . %s]]' \
-             "$date" "$(HTML "$iban")" \
-             "$(UNSTRING "$accname" |HTML)" "$(UNSTRING "$subject" |HTML)" \
-             "$(credit "$amount")" "$(credit "$total")"
+      if [ "$iban" = "${cardfile%.vcf}" ]; then
+        printf '[tr [td .date . %s][td .orig [span . %s][span . %s]][td .reference . %s]
+                [td .amount . %s][td .balance . %s]]' \
+               "$date" "$(l10n "Credit Account")" \
+               "$(UNSTRING "$accname" |HTML)" "$(UNSTRING "$reftext" |HTML)" \
+               "$(credit "$amount")" "$(credit "$total")"
+      else
+        printf '[tr [td .date . %s][td .orig [span . %s][span . %s]][td .reference . %s]
+                [td .amount . %s][td .balance . %s]]' \
+               "$date" "$(HTML "$iban")" \
+               "$(UNSTRING "$accname" |HTML)" "$(UNSTRING "$reftext" |HTML)" \
+               "$(credit "$amount")" "$(credit "$total")"
+      fi
     done
   }
-  printf '[tr [th colspan=5 . %s]]' "$(l10n 'Manual Record')"
-  printf '[tr [td .date [input type=date placeholder="%s" name=tdate]]
-              [td .orig [input id=trec_once  type=radio name="trec" value="once" selected]
-                        [label for=trec_once  . %s]<br/>
-                        [input id=trec_month type=radio name="trec" value="month"]
-                        [label for=trec_month . %s]
-                        [input type=date name="trec_until" placeholder="%s"]]
-              [td .reference . [textarea placeholder="%s" name=treference]]
-              [td .amount colspan=2 [input type=number placeholder="%s" name=tamount value=0.00 step=.01]]' \
-       "$(l10n Date)" "$(l10n once)" "$(l10n "monthly until")" "$(l10n until)" "$(l10n "Reference Text")" "$(l10n Amount)"
-  printf ']]]'
+  cat <<-EOF
+       [tr [th colspan=5 . $(l10n 'Manual Record')]]
+       [tr [td .date
+              [input type=date placeholder="$(l10n Date)" name=tdate value="$(date +%F)"]
+             [input type=checkbox id=rr_month name=recur value=monthly]
+             [label for=rr_month $(l10n Recur Monthly)]
+           ][td .orig ]
+           [td .reference . [textarea placeholder="$(l10n "Reference Text")" name=tref]]
+           [td .amount [input type=number placeholder="$(l10n Amount)" name=tamount value=0.00 step=.01]]
+           [td .balance [button type=submit . $(l10n Submit)]]
+       ]]]]
+       EOF
 } \
 | yield_page ledgers