From 1cce12f8beb3983e4fc61c7a7d382cdd0cfbd4fd Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Mon, 6 Jul 2020 20:03:41 +0200 Subject: [PATCH] Add script to add slots with key 'equity-type' and value 'opening-balance' to detected opening balance accounts --- util/add-opening-balances | 118 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100755 util/add-opening-balances diff --git a/util/add-opening-balances b/util/add-opening-balances new file mode 100755 index 0000000000..692a0d1902 --- /dev/null +++ b/util/add-opening-balances @@ -0,0 +1,118 @@ +#!/bin/sh +# +# Find opening balance accounts by translations and add slot with key 'opening-balance' to make them translation independent +# +# @author Ralf Habacker +# +r=$(realpath $0) +r=$(dirname $r) +r=$(dirname $r) + +# debug +#grep -rn -A1 '"Opening Balances"' $r/po | grep msgstr > translation-of-opening-balance-with-files.txt +#grep -rnf translation-of-opening-balance.txt $r/data/accounts > all-opening-balances-accounts.txt + +if test -z "$1" || test "$1" == "--collect"; then + # collect all translations for 'Opening Balances' + grep -rn -A1 '"Opening Balances"' $r/po | grep msgstr | gawk '{ $1=""; print $0}' | sed 's,^ ,,g;s,",,g' | grep -v "^$" | sed 's,^,,g;s,$,.*,g' > translation-of-opening-balance.txt + + # collect all translations for 'Opening Balances' with optional last character + grep -rn -A1 '"Opening Balances"' $r/po | grep msgstr | gawk '{ $1=""; print $0}' | sed 's,^ ,,g;s,",,g' | grep -v "^$" | sed 's,^,,g;s,$,*,g' >> translation-of-opening-balance.txt + + # add custom strings + cat << EOF >> translation-of-opening-balance.txt +Openingsbalans +9000 Saldenvortrag Sachkonten +9000 Saldenvorträge Sachkonten +Saldenvorträge Sachkonten +EOF + +fi + +# collect all files that contains a translation for 'Opening Balances' +for i in $(grep -rnf translation-of-opening-balance.txt $r/data/accounts | sed 's,:[ ]*,:,g;s, ,#,g'); do + #echo $i + file=$(echo $i | sed 's,:.*$,,g') + pattern=$(echo $i | sed 's,^.*:<,<,g;s,#, ,g') + #echo "searching for $pattern in $file" + # add equity-type slot + gawk ' +BEGIN { + found = 0; + slotadded = 0; + slotpresent = 0; + patternfound = 0; +} + +# search for account name +$0 ~ PATTERN { + slotadded = 0 + slotpresent = 0 + patternfound = 1 + replace = 1 + #print "" +} + +# opening-balance slot is already available +replace && $1 == "equity-type" { + slotkeypresent = 1 +} + +slotkeypresent && $1 == "opening-balance" { + replace = 0 + slotpresent = 1 +} + +#exclude parent named like opening balance account +replace && $1 == "placeholder" { + replace = 0 +} + +# add new slots tag +replace && $1 == "" { + if (slotkeypresent) { + print FILE ": slot equity-type already present with different value, could not tag '" PATTERN "' as opening balance account" > /dev/stderr + } else { + print " " + print " " + print " equity-type" + print " opening-balance" + print " " + print " " + slotadded = 1 + replace = 0 + } +} + +{ + print $0; +} + +END { + if (!patternfound) + print FILE ": opening balance account not found with pattern " PATTERN > /dev/stderr + #if (!slotpresent && !slotadded) + #print FILE " opening balance account not found with pattern " PATTERN > /dev/stderr +} + ' "PATTERN=$pattern" $file > $file.new + mv $file.new $file +done + +oba="" +nooba="" +for i in $(find $r/data/accounts -name '*.gnucash-xea'); do + o=$(grep -Hn "opening-balance" $i) + if test -z "$o"; then + nooba="$nooba\n$i" + else + oba="$oba\n$i" + fi +done + +echo "------------------------------------------------------------" +echo -e "The following files do not have an account with slot key 'opening-balance'" +echo -e $nooba +echo +echo "------------------------------------------------------------" +echo -e "The following files have an account with slot key 'opening-balance'" +echo -e $oba