Skip to content

Commit e587978

Browse files
committed
feat(ledger): create script for adding groceries
1 parent 7d68802 commit e587978

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

scripts/ledger/groceries

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#! /bin/sh
2+
3+
if [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$1" = "help" ] || [ -z "$2" ]; then
4+
cat <<EOF
5+
$ groceries TITLE AMOUNT DATE
6+
Add groceries to ledger file
7+
Paid in cash
8+
9+
Parameters:
10+
\$1: title of the transaction
11+
\$2: amount for the whole
12+
\$3: date
13+
14+
Example:
15+
$ groceries "Edeka" "18.5+5.6"
16+
EOF
17+
exit
18+
fi
19+
20+
command -v node >/dev/null || { echo "node is not installed" 1>&2; exit 127; }
21+
22+
get_input() {
23+
echo "$1"
24+
read ans
25+
echo $ans
26+
}
27+
28+
TITLE="$1"
29+
30+
amount_input="$2"
31+
AMOUNT_TOTAL=$(node -e "console.log( ($amount_input).toFixed(2) )")
32+
AMOUNT_SHARED=$(node -e "console.log( ($AMOUNT_TOTAL/2).toFixed(2) )")
33+
34+
date_input="$3"
35+
DATE=$(date +%Y-%m-%d)
36+
if echo "$date_input" | grep -Fe '-' >/dev/null; then
37+
DATE="$(date +%Y-)${date_input}"
38+
elif [ -n "$date_input" ]; then
39+
DATE="$(date +%Y-%m-)${date_input}"
40+
fi
41+
42+
print() {
43+
cat <<EOF
44+
$DATE $TITLE
45+
ex:food:groceries $AMOUNT_SHARED
46+
[pot:leben] -$AMOUNT_SHARED
47+
person:francis $AMOUNT_SHARED
48+
[person:francis] -$AMOUNT_SHARED
49+
[owed] $AMOUNT_SHARED
50+
[reserve] -$AMOUNT_SHARED
51+
wallet -$AMOUNT_TOTAL
52+
[wallet] $AMOUNT_TOTAL
53+
54+
EOF
55+
}
56+
57+
print >>$LEDGER_FILE

0 commit comments

Comments
 (0)