]> git.plutz.net Git - confetti/commitdiff
heuristic assignment transaction <-> vcard
authorPaul Hänsch <paul@plutz.net>
Wed, 17 Apr 2024 22:39:32 +0000 (00:39 +0200)
committerPaul Hänsch <paul@plutz.net>
Wed, 17 Apr 2024 22:39:32 +0000 (00:39 +0200)
ledgers/iban_assign.awk [new file with mode: 0755]
ledgers/index.cgi

diff --git a/ledgers/iban_assign.awk b/ledgers/iban_assign.awk
new file mode 100755 (executable)
index 0000000..090d950
--- /dev/null
@@ -0,0 +1,91 @@
+#!/bin/awk -f
+
+function STRING( inp ) {
+  gsub(/\\/, "\\\\", inp);
+  gsub(/\n/, "\\n",  inp);
+  gsub(/\r/, "\\r",  inp);
+  gsub(/\t/, "\\t",  inp);
+  gsub(/\+/, "\\+",  inp);
+  gsub(/ /,  "+",    inp);
+  return inp ? inp : "\\";
+}
+
+function UNSTRING( inp,    out, tmp ) {
+  while ( inp ) {
+         if ( inp ~ /^\\\\/) { out = out "\\"; sub(/^\\\\/, "", inp); }
+    else if ( inp ~ /^\\n/)  { out = out "\n"; sub(/^\\n/,  "", inp); }
+    else if ( inp ~ /^\\r/)  { out = out "\r"; sub(/^\\r/,  "", inp); }
+    else if ( inp ~ /^\\t/)  { out = out "\t"; sub(/^\\t/,  "", inp); }
+    else if ( inp ~ /^\\+/)  { out = out "+";  sub(/^\\+/,  "", inp); }
+    else if ( inp ~ /^\+/)   { out = out " ";  sub(/^\+/,   "", inp); }
+    else if ( inp ~ /^\\/)   { out = out "";   sub(/^\+/,   "", inp); }
+    else { tmp = inp; sub(/[\\+].*$/, "", tmp); out = out tmp; sub(/^[^\\+]*/, "", inp); }
+  }
+  return out;
+}
+
+function rx( regex ) {
+  gsub(/[].*+?^${}()|\\[]/, "\\\\&", regex);
+  return regex;
+}
+
+BEGIN {
+  FS = "       "; ledger = 0;
+  fn = n = uid = iban = tmp = "";
+  split("", uid_n); split("", uid_fn); split("", uid_iban);
+  split("", iban_uid); split("", ibans); split("", uids);
+
+  split("", sure); split("", unsure); split("", unknown);
+  split("", unsure_rec)
+}
+
+/^BEGIN;:VCARD$/ { fn = n = id = iban = tmp = ""; }
+
+   /^UID;[^:]*:/ { uid = $0; sub(/^[^;]+;[^:]*:/, "", uid); }
+    /^FN;[^:]*:/ {  fn = $0; sub(/^[^;]+;[^:]*:/, "",  fn); }
+     /^N;[^:]*:/ {   n = $0; sub(/^[^;]+;[^:]*:/, "",   n); sub(/;.*$/, "", n); }
+/^X-IBAN;[^:]*:/ { tmp = $0; sub(/^[^;]+;[^:]*:/, "", tmp); iban = iban ? iban " " tmp : tmp; }
+
+/^END;:VCARD$/ {
+  uid_n[uid] = n; uid_fn[uid] = fn; uid_iban[uid] = iban;
+  split(iban, ibans, / /);
+  for (iban in ibans) iban_uid[iban] = iban_uid[iban] ? iban_uid[iban] " " uid : uid;
+  fn = n = uid = iban = tmp = "";
+}
+
+strftime("%Y-%m-%d", $1, "UTC") == $2 && strftime("%Y-%m-%d", $3, "UTC") == $4 { ledger = 1; }
+
+ledger && strftime("%Y-%m-%d", $2, "UTC") == $1 {
+  if ($3 in iban_uid) {
+    sure[$3] = iban_uid[$3];
+  } else {
+    for (uid in uid_fn) if ( match(UNSTRING($5), rx(uid_fn[uid])) ) {
+      if (! match(unsure[$3], rx(uid))) unsure[$3] = unsure[$3] ? unsure[$3] " " uid : uid;
+      unsure_rec[$3] = $0
+    }
+    for (uid in uid_n) if ( uid_n[uid] && match(UNSTRING($4), rx(uid_n[uid])) ) {
+      if (! match(unsure[$3], rx(uid))) unsure[$3] = unsure[$3] ? unsure[$3] " " uid : uid;
+      unsure_rec[$3] = $0
+    }
+  }
+  if (!($3 in sure) && !($3 in unsure)) unknown[$3] = $0;
+}
+
+END {
+  for (iban in sure) {
+    line = "sure       " iban;
+    split(iban_uid[iban], uids, / /);
+    for (uid in uids) line = line "    " STRING(uid "/" uid_fn[uid]);
+    print line;
+  }
+  for (iban in unsure) {
+    line = "guess      " iban " " STRING(unsure_rec[iban]);
+    split(unsure[iban], uids, / /);
+    for (k in uids) line = line "      " STRING(uids[k] "/" uid_fn[uids[k]]);
+    print line;
+  }
+  for (iban in unknown) {
+    line = "unknown    " iban " " unknown[iban];
+    print line;
+  }
+}
index 1313ef843ad720a743f93e64a7965ed184963d0b..e8852941f20f72615c33e2b4c0f0d4d40878ad24 100755 (executable)
@@ -1,5 +1,8 @@
 #!/bin/sh
 
+. "$_EXEC/cgilite/storage.sh"
+. "$_EXEC/pdiread.sh"
+
 { printf '
   [form .upload action="%s/ledgers/csv_upload.sh" method="POST" enctype="multipart/form-data"
     [label for=ledger_upload . %s:]
@@ -8,4 +11,44 @@
     [button type="submit" %s]
   ]' \
   "${_BASE}" "$(l10n "Postbank CSV")" "$SESSION_ID" "$(l10n Upload)"
+  printf '
+  [form .ledgers action="%s/ledgers/delete.sh" method=POST
+    [input type=hidden name=session_id value="%s"]
+    [h3 . %s]
+  ' "${_BASE}" "$SESSION_ID" "$(l10n Ledgers)"
+  for ledger in "$_DATA"/ledgers/????-??-??\ -\ ????-??-??\ -\ ????.tbl; do
+    ledger="${ledger##*/}"
+    [ "$ledger" = "????-??-?? - ????-??-?? - ????.tbl" ] && continue
+    printf '[p .ledger . %s [button type=submit name=delete value="%s" . %s]]' \
+           "$(HTML "${ledger% - ????.tbl}")" "$(HTML "$ledger")" "$(l10n delete)"
+  done
+  printf '  ]'
+  printf '
+  [form .ledgers action="%s/ledgers/delete.sh" method=POST
+    [input type=hidden name=session_id value="%s"]
+    [h3 . %s]
+  ' "${_BASE}" "$SESSION_ID" "$(l10n "IBAN Assignments")"
+  { pdi_load "${_DATA}"/vcard/*.vcf
+    cat "${_DATA}"/ledgers/????-??-??\ -\ ????-??-??\ -\ ????.tbl
+  } | "${_EXEC}"/ledgers/iban_assign.awk \
+  | while read -r state iban data; do
+    printf '[fieldset .iban .%s [legend . %s ]' \
+           "$state" "$iban"
+      if [ $state = sure ]; then
+      :
+    elif [ $state = guess ]; then
+      record="$(UNSTRING "${data%%     *}")"
+      principal="${record#*    *       *       }" principal="${principal%%     *}"
+      subject="${record#*      *       *       *       }" subject="${subject%% *}"
+      printf '[p .principal . %s][p .subject . %s]' \
+             "$(UNSTRING "$principal" |HTML)" "$(UNSTRING "$subject" |HTML)"
+    elif [ $state = unknown ]; then
+      principal="${data#*      *       *       }" principal="${principal%%     *}"
+      subject="${data#*        *       *       *       }" subject="${subject%% *}"
+      printf '[p .principal . %s][p .subject . %s]' \
+             "$(UNSTRING "$principal" |HTML)" "$(UNSTRING "$subject" |HTML)"
+    fi
+  printf ']'
+  done
+  printf '  ]'
 } | yield_page ledgers