aboutsummaryrefslogtreecommitdiff
path: root/quickLedger.sh
diff options
context:
space:
mode:
authorInigoGutierrez <inigogf.95@gmail.com>2022-08-01 23:14:29 +0200
committerInigoGutierrez <inigogf.95@gmail.com>2022-08-01 23:14:29 +0200
commit0a425fd64f18e1a37db9d2efbed312df702c6d2c (patch)
tree1fde46f7d5de849fa134c996847fd836fc9bf2c2 /quickLedger.sh
parent6ed630693d88fdb6d72419a493def26a2a1ecf5d (diff)
downloadscripts-0a425fd64f18e1a37db9d2efbed312df702c6d2c.tar.gz
scripts-0a425fd64f18e1a37db9d2efbed312df702c6d2c.zip
Created quickLedger.sh to quickly add entries to a ledger.
Diffstat (limited to 'quickLedger.sh')
-rwxr-xr-xquickLedger.sh105
1 files changed, 105 insertions, 0 deletions
diff --git a/quickLedger.sh b/quickLedger.sh
new file mode 100755
index 0000000..88f410f
--- /dev/null
+++ b/quickLedger.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+
+# quickLedger.sh: Quickly create a new entry for ledger
+#
+# Usage: quickLedger.sh [-t] [-f LEDGER_FILE] [-r REMOTE_LEDGER_FILE] [-c CURRENCY_SYMBOL]
+
+usageMessage='Usage: quickLedger.sh [-d] [-f LEDGER_FILE] [-r REMOTE_LEDGER_FILE] [-c CURRENCY_SYMBOL]'
+
+ledgerFile="$LEDGER_FILE"
+remoteLedgerFile="$REMOTE_LEDGER_FILE"
+currencySymbol='€'
+debug=''
+
+# $1: message
+# $2: exit code
+fatalError() {
+ printf "Error: $1" 1>&2
+ exit "$2"
+}
+
+organizeLines() {
+ sort | uniq -c | sort -nr | sed 's/^\s*[0-9]* //'
+}
+
+getPayee() {
+ grep '^\w' "$ledgerFile" | cut -d' ' -f2- | organizeLines | fzf --prompt 'Payee: '
+}
+
+# $1: fzf prompt
+getAccount() {
+ grep '^\s' "$ledgerFile" | awk '{print $1}' | organizeLines | fzf --prompt "$1" --no-clear
+}
+
+getComment() {
+ grep -o ';.*$' "$ledgerFile" | organizeLines | fzf --prompt "Comment: "
+}
+
+getAmount() {
+ read amount
+ while ! echo "$amount" | grep -Eq '[0-9]+(\.[0-9][0-9])?'; do
+ read amount
+ done
+ echo "$amount" | grep -Fq '.' || amount="${amount}.00"
+ amount="${amount}${currencySymbol}"
+ echo "$amount"
+}
+
+getTransaction() {
+ printf '%s %s\n\t%s\t%s\t%s\n\t%s\n' "$date" "$payee" "$targetAccount" "$amount" "$comment" "$originAccount"
+}
+
+# Process options
+while getopts ':tf:r:c:' opt; do
+ case $opt in
+
+ 't' )
+ debug='y' ;;
+
+ 'f' )
+ ledgerFile="$OPTARG" ;;
+
+ 'r' )
+ remoteLedgerFile="$OPTARG" ;;
+
+ 'c' )
+ currencySymbol="$OPTARG" ;;
+
+ '?' )
+ printf "${usageMessage}\n"
+ exit 1
+
+ esac
+done
+shift $(($OPTIND - 1))
+
+[ -z "$ledgerFile" ] &&
+ fatalError 'No ledger file. Set ledger file as $LEDGER_FILE or with -f option.\n' 2
+[ ! -f "$ledgerFile" ] && fatalError "No file [${ledgerFile}] found to use as ledger.\n" 3
+
+targetAccount="$(getAccount 'Target account: ')"
+[ -z "$targetAccount" ] && tput rmcup && exit 0
+originAccount="$(getAccount 'Origin account: ')"
+[ -z "$originAccount" ] && tput rmcup && exit 0
+
+payee="$(getPayee)"
+
+printf 'Amount: '
+amount="$(getAmount)"
+[ -z "$amount" ] && exit 0
+
+comment="$(getComment)"
+
+date="$(date '+%Y/%m/%d')"
+
+getTransaction
+
+printf 'Correct? [Yn]: '
+read correct
+[ "$correct" = "n" ] && exit 0
+
+[ -n "$debug" ] && exit 0
+
+echo >> "$ledgerFile"
+getTransaction >> "$ledgerFile"
+rsync -t "$ledgerFile" "$remoteLedgerFile" || fatalError 'rsync failed.' 4