]> Sergey Matveev's repositories - pem.git/blob - pem
Raise copyright years
[pem.git] / pem
1 #!/bin/sh -e
2 # pem -- GNU Personal Expenses Manager inspired simpler script
3 # Copyright (C) 2015-2021 Sergey Matveev (stargrave@stargrave.org)
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 # This script is simpler version of GNU PEM software. It uses sqlite3 as
19 # a database storage.
20 #
21 # Schema that is used for it:
22 #
23 #     CREATE TABLE expenses (
24 #         id INTEGER PRIMARY KEY,
25 #         dt DATETIME NOT NULL,
26 #         earned NUMERIC NOT NULL,
27 #         spent NUMERIC NOT NULL,
28 #         descr TEXT NOT NULL
29 #     );
30 #     CREATE TABLE tags (
31 #         id INTEGER PRIMARY KEY,
32 #         name TEXT NOT NULL
33 #     );
34 #     CREATE TABLE m2m (
35 #         id INTEGER PRIMARY KEY,
36 #         expense_id INTEGER,
37 #         tag_id INTEGER,
38 #         FOREIGN KEY(expense_id) REFERENCES expenses(id),
39 #         FOREIGN KEY(tag_id) REFERENCES tags(id)
40 #     );
41
42 SQLITE=sqlite3
43 DB=$HOME/secure/money.db
44
45 case "$1" in
46 s)
47     dt="$2"
48     [ -n "$dt" ] || dt="-1 day"
49     $SQLITE -column -header $DB "
50         SELECT
51             date(expenses.dt) AS \"when\",
52             expenses.earned - expenses.spent AS amount,
53             group_concat(tags.name, ',') AS tags,
54             expenses.descr AS \"what\"
55         FROM expenses
56         LEFT JOIN m2m ON m2m.expense_id=expenses.id
57         LEFT JOIN tags ON m2m.tag_id=tags.id
58         GROUP BY expenses.id
59         ORDER BY dt DESC"
60     ;;
61 t)
62     $SQLITE -column -header $DB "
63         SELECT
64             SUM(earned) AS earned,
65             SUM(spent) AS spent,
66             SUM(earned) - SUM(spent) AS balance
67         FROM expenses"
68     ;;
69 *)
70     spent=0
71     earned=0
72     descr="$1"
73     money="$2"
74     tags="$3"
75     [ -n "$descr" ]
76     [ -n "$money" ]
77     if echo $money | grep -q "^-"; then
78         earned=$(echo $money | sed 's/^-//')
79     else
80         spent=$money
81     fi
82     $SQLITE $DB "INSERT INTO expenses
83         (dt, earned, spent, descr) VALUES
84         (datetime('now'), $earned, $spent, '$descr')"
85     last_id=$($SQLITE $DB "SELECT id FROM expenses ORDER BY id DESC LIMIT 1")
86     for tag in $(echo $tags | sed 's/,/ /g'); do
87         tag_id=$($SQLITE $DB "SELECT id FROM tags WHERE name='$tag'")
88         if [ "$tag_id" = "" ]; then
89             $SQLITE $DB "INSERT INTO tags (name) VALUES ('$tag')"
90             tag_id=$($SQLITE $DB "SELECT id FROM tags WHERE name='$tag'")
91         fi
92         $SQLITE $DB "INSERT INTO m2m (expense_id, tag_id) VALUES ($last_id, $tag_id)"
93     done
94     ;;
95 esac