Account merging.

This commit is contained in:
Dominik Pantůček 2023-05-26 12:32:36 +02:00
parent abd9f4aa5b
commit f8d89e340f

View file

@ -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