spacefrogg-overlay/ledger-tools/ledger-camt-csv

43 lines
1.2 KiB
Plaintext
Raw Permalink Normal View History

2020-10-18 14:27:19 +02:00
#!/bin/sh
# Sparkasse Germany CAMT CSV format
# Sparkasse Germany VISA CSV format
set -e
2020-11-28 20:42:02 +01:00
PATH=@binpath@:$PATH
2020-10-18 14:27:19 +02:00
2020-11-28 20:42:02 +01:00
[ $# -ge 2 ] || { printf "Usage: ledger-camt-csv [-visa | -camt] CSV ACCOUNT\n">&2; exit 1; }
2020-10-18 14:27:19 +02:00
CAMT_HDR=",date,posted,,note,,,,,,,payee,pIBAN,pBIC,amount,,"
VISA_HDR=",date,posted,oAmount,oCommodity,,amount,,payee,note,,,,,,"
HDR=$CAMT_HDR
case $1 in
-visa) shift
HDR=$VISA_HDR
;;
-camt) shift
HDR=$CAMT_HDR
;;
esac
DATE_FMT="%d.%m.%y"
INFILE=$1
_INDIR=$(dirname "$INFILE")
if [ "${_INDIR##/}" != "new" ]; then
printf "Error: Expecting input file in new/ of a maildir-like directory structure\n" >&2
exit 1
fi
BASEDIR=$(dirname $1)/..
BASEDIR=$(realpath "$BASEDIR")
MANGLED=$(mktemp -p $BASEDIR/tmp/ mangled-XXXXXX.csv)
OUTFILE=$BASEDIR/tmp/$(basename "${INFILE%.CSV}.ledger")
{ echo "$HDR"; tail -n+2 "$INFILE"; } |
sed -e 's/";"/","/g' |
iconv -f ISO-8859-1 -t UTF-8 >$MANGLED
2020-11-28 20:42:02 +01:00
ledger convert "$MANGLED" --input-date-format "$DATE_FMT" --invert --rich-data --account $2 >$OUTFILE
2020-10-18 14:27:19 +02:00
echo "$BASEDIR/new/"$(basename "$OUTFILE") >&2
rm -f "$MANGLED"
mv "$INFILE" "$BASEDIR/cur"
mv "$OUTFILE" "$BASEDIR/new"