From f8d89e340fd4ef3f16b4be3498f0eb2fc742f5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Fri, 26 May 2023 12:32:36 +0200 Subject: [PATCH] Account merging. --- fetch_fio.sh | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/fetch_fio.sh b/fetch_fio.sh index dcd75a6..67b56bb 100644 --- a/fetch_fio.sh +++ b/fetch_fio.sh @@ -157,6 +157,90 @@ download_year() { download_file "${APIURI}/periods/$apikey/$year-01-01/$year-12-31/transactions.csv" "$fname" } +# +# Gets the first year of the account statement. +# $1 - account number +first_acc_part() { + accno="$1" + ls "${BANK_DIR_PARTS}/" \ + | grep "^$accno" \ + | grep '.csv$' \ + | sort \ + | head -n 1 +} + +# +# Gets the last year of the account statement. +# $1 - account number +last_acc_part() { + accno="$1" + ls "${BANK_DIR_PARTS}/" \ + | grep "^$accno" \ + | grep '.csv$' \ + | sort -r \ + | head -n 1 +} + +# +# Returns all parts except for the first +# $1 - account number +all_but_first_parts() { + accno="$1" + first="`first_acc_part $1`" + ls "${BANK_DIR_PARTS}/" \ + | grep "^$accno" \ + | grep '.csv$' \ + | sort \ + | grep -v "$first" +} + +# +# Creates static header for given account +# $1 - account number +make_acc_header_static() { + grep -B 20 '^$' "$1" \ + | grep . \ + | egrep -v 'Balance|^date|^id' +} + +# +# Gets only given header +# $1 - path to file +# $2 - header name +get_header_field() { + grep "^$2" "$1" +} + +# +# Dynamic part +# $1 - account number +make_acc_header() { + first="`first_acc_part $1`" + last="`last_acc_part $2`" + firstfname="$BANK_DIR_PARTS/$first" + lastfname="$BANK_DIR_PARTS/$last" + make_acc_header_static "$firstfname" + get_header_field "$firstfname" openingBalance + get_header_field "$lastfname" closingBalance + get_header_field "$firstfname" dateStart + get_header_field "$lastfname" dateEnd + get_header_field "$firstfname" idFrom + get_header_field "$lastfname" idTo +} + +# +# Creates the complete merged account statement +# $1 - account number +merge_acc() { + make_acc_header "$1" + grep -A 100000 '^$' "$BANK_DIR_PARTS/`first_acc_part $1`" + for part in `all_but_first_parts $1` ; do + grep -A 100000 '^ID' "$BANK_DIR_PARTS/$part" \ + | grep -v '^ID' + done + +} + # Current year (last in seq) CYEAR=`date +%Y` @@ -209,6 +293,15 @@ while read accnt ; do log "Skipping $ACCNO in $year - already latest: $FILE_DATE" fi done + + # Merge the account + log "Merging $ACCNO" + merge_acc "$ACCNO" >"$BANK_DIR/$ACCNO.csv.tmp" + log "Renaming $ACCNO" + if [ -r "$ACCNO.csv" ] ; then + cp "$ACCNO.csv" "$ACCNO.csv.old" + fi + mv "$ACCNO.csv.tmp" "$ACCNO.csv" done < "$APIKEYS_FILE" # Mark