From 3813e11258fc1b07708cb5d8b3ae16e6c04340a7 Mon Sep 17 00:00:00 2001 From: Makussu Date: Sat, 1 Jun 2024 14:47:24 +0200 Subject: [PATCH] starting transaction --- routes/accounts.js | 22 ++- routes/transactions.js | 29 +++- sql/create_db.sql | 8 +- test/accounts.json | 301 ++++++++++++++++++++++++++++++++++++ test/transaction_6.json | 15 ++ test/transactions.json | 332 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 696 insertions(+), 11 deletions(-) create mode 100644 test/accounts.json create mode 100644 test/transaction_6.json create mode 100644 test/transactions.json diff --git a/routes/accounts.js b/routes/accounts.js index 1d6634f..7872d7c 100644 --- a/routes/accounts.js +++ b/routes/accounts.js @@ -4,7 +4,8 @@ const router = express.Router(); const db = require("../dependencies"); router.get("/", (req, res) => { - const accs = db.prepare("SELECT * FROM accounts").all(); + const accs = db.prepare(`SELECT number, name, qualifiedName, description, type, localBalance, te.balance FROM accounts + JOIN (SELECT SUM(amount) as balance, account_name FROM transaction_entries GROUP BY account_name) te ON te.account_name = name`).all(); new_accs = []; for (let i=0; i < accs.length; i++) { accs[i]["subaccounts"] = []; @@ -16,11 +17,12 @@ router.get("/", (req, res) => { router.post("/", (req, res) => { + const number = req.body.number; const name = req.body.name; const des = req.body.description; const type = req.body.type; const qualifiedName = req.body.parentAccount + ":" + name - const new_acc = db.prepare(`INSERT INTO accounts (name, qualifiedName, description, type, balance, localBalance) VALUES (?, ? ,?, ?, 0, 0)`).run(name, qualifiedName,des, type) + const new_acc = db.prepare(`INSERT INTO accounts (number, name, qualifiedName, description, type, balance, localBalance) VALUES (?, ?, ? ,?, ?, 0, 0)`).run(number ,name, qualifiedName,des, type) res.status(204).send() console.log(new_acc); @@ -38,8 +40,20 @@ router.get("/:account", (req, res) => { additionalPropl: {} }); } else { - acc['entries'] = [] - console.log(acc) + entries = db.prepare(` + SELECT + transaction_entries.transaction_id as id, + offsetAccounts, + transaction_entries.amount, + transaction_entries.label, + postingDate, + valueDate, + title + from transaction_entries + JOIN transactions ON transaction_entries.transaction_id = transactions.transaction_id + JOIN (SELECT account_name as offsetAccounts, transaction_id FROM transaction_entries) te ON te.transaction_id = id AND offsetAccounts IS NOT transaction_entries.account_name + WHERE transaction_entries.account_name = ?`).all(req.params.account) + acc['entries'] = entries res.send(acc); } }); diff --git a/routes/transactions.js b/routes/transactions.js index 2e205aa..3873e04 100644 --- a/routes/transactions.js +++ b/routes/transactions.js @@ -1,17 +1,40 @@ const express = require('express'); const router = express.Router(); +const db = require("../dependencies"); + router.get("/", (req, res) => { - res.send([]) - + const transactions = db.prepare("SELECT transaction_id as id, postingDate, valueDate, title FROM transactions").all(); + + res.send(transactions) }) router.post("/", (req, res) => { + const transaction = { + postingDate: req.body.postingDate, + valueDate: req.body.valueDate, + title: req.body.title, + entries: req.body.entries + } + + const db_transaction = db.prepare("INSERT INTO transactions (postingDate, valueDate, title) VALUES (?, ?, ?) RETURNING transaction_id").run(transaction.postingDate, transaction.valueDate, transaction.title) + + for (let i = 0; i < transaction.entries.length; i++) { + const db_entries = db.prepare("INSERT INTO transaction_entries (transaction_id, account_name, amount, label) VALUES (?, ?, ?, ?)").run(db_transaction.lastInsertRowid, transaction.entries[i].account, transaction.entries[i].amount, transaction.entries[i].label) + } + + res.send(db_transaction.lastInsertRowid) }) router.get("/:id", (req, res) => { - res.send({}) + const transaction = db.prepare("SELECT postingDate, valueDate, title FROM transactions WHERE transaction_id = ?").get(req.params.id); + const entries = db.prepare("SELECT account_name as account, amount, label FROM transaction_entries WHERE transaction_id = ?").all(req.params.id) + + transaction["entries"] = entries + console.log(transaction) + + res.send(transaction) }) router.put("/:id", (req, res) => { diff --git a/sql/create_db.sql b/sql/create_db.sql index 3301c44..9c483d4 100644 --- a/sql/create_db.sql +++ b/sql/create_db.sql @@ -1,7 +1,7 @@ CREATE TABLE accounts ( - account_id integer PRIMARY KEY, + number integer NOT NULL, name text NOT NULL, - qualifiedName text NOT NULL, + qualifiedName text NOT NULL UNIQUE, description text NOT NULL, type TEXT NOT NULL, balance float NOT NULL, @@ -17,9 +17,9 @@ CREATE TABLE transactions ( CREATE TABLE transaction_entries ( transaction_id int, - account_id int, + account_name text NOT NULL, amount float, label text, - FOREIGN KEY (transaction_id) REFERENCES transactions (transaction_id) FOREIGN KEY (account_id) REFERENCES account (account_id) PRIMARY KEY (transaction_id, account_id) + FOREIGN KEY (transaction_id) REFERENCES transactions (transaction_id) FOREIGN KEY (account_name) REFERENCES account (account_qualifiedName) PRIMARY KEY (transaction_id, account_id) ); diff --git a/test/accounts.json b/test/accounts.json new file mode 100644 index 0000000..fdd8d7a --- /dev/null +++ b/test/accounts.json @@ -0,0 +1,301 @@ +[ + { + "number": 1000, + "name": "Aktiva", + "qualifiedName": "Aktiva", + "type": "meta", + "balance": 7986.16, + "localBalance": 0, + "description": "Meta-Konto für das Vermögen", + "subaccounts": [ + { + "number": 1100, + "name": "Barvermögen", + "qualifiedName": "Aktiva:Barvermögen", + "type": "meta", + "balance": 7986.16, + "localBalance": 0, + "subaccounts": [ + { + "number": 1110, + "name": "Bargeld", + "qualifiedName": "Aktiva:Barvermögen:Bargeld", + "type": "default", + "balance": -11581, + "localBalance": -11581, + "subaccounts": [] + }, + { + "number": 1120, + "name": "Girokonto", + "qualifiedName": "Aktiva:Barvermögen:Girokonto", + "type": "default", + "balance": 14712.16, + "localBalance": 14712.16, + "subaccounts": [ + { + "number": 123, + "name": "123", + "qualifiedName": "Aktiva:Barvermögen:Girokonto:123", + "type": "default", + "balance": 0, + "localBalance": 0, + "description": "test", + "subaccounts": [ + { + "number": 1111, + "name": "Test", + "qualifiedName": "Aktiva:Barvermögen:Girokonto:123:Test", + "type": "default", + "balance": 0, + "localBalance": 0, + "description": "Test", + "subaccounts": [] + } + ] + } + ] + }, + { + "number": 1130, + "name": "Sparkonto", + "qualifiedName": "Aktiva:Barvermögen:Sparkonto", + "type": "default", + "balance": 3733, + "localBalance": 3733, + "subaccounts": [ + { + "number": 12345, + "name": "Test SSE", + "qualifiedName": "Aktiva:Barvermögen:Sparkonto:Test SSE", + "type": "default", + "balance": 0, + "localBalance": 0, + "description": "TEST SSE", + "subaccounts": [] + } + ] + }, + { + "number": 187420, + "name": "Uiiih", + "qualifiedName": "Aktiva:Barvermögen:Uiiih", + "type": "default", + "balance": 1122, + "localBalance": 1122, + "description": "Memooooo", + "subaccounts": [] + } + ] + } + ] + }, + { + "number": 4000, + "name": "Erträge", + "qualifiedName": "Erträge", + "type": "meta", + "balance": -7090.64, + "localBalance": 0, + "description": "Meta-Konto für alle Einnahmen", + "subaccounts": [ + { + "number": 4100, + "name": "Gehalt", + "qualifiedName": "Erträge:Gehalt", + "type": "default", + "balance": -5458.64, + "localBalance": -5458.64, + "subaccounts": [] + }, + { + "number": 4200, + "name": "Kindergeld", + "qualifiedName": "Erträge:Kindergeld", + "type": "default", + "balance": -1000, + "localBalance": -1000, + "subaccounts": [] + }, + { + "number": 4300, + "name": "BAföG", + "qualifiedName": "Erträge:BAföG", + "type": "default", + "balance": 0, + "localBalance": 0, + "subaccounts": [ + { + "number": 1236, + "name": "Test Konto 3", + "qualifiedName": "Erträge:BAföG:Test Konto 3", + "type": "meta", + "balance": 0, + "localBalance": 0, + "description": "Test Konto 2 Beschreibung", + "subaccounts": [] + } + ] + }, + { + "number": 4400, + "name": "Stipendium", + "qualifiedName": "Erträge:Stipendium", + "type": "default", + "balance": 0, + "localBalance": 0, + "subaccounts": [] + }, + { + "number": 4500, + "name": "Unterhalt", + "qualifiedName": "Erträge:Unterhalt", + "type": "default", + "balance": -600, + "localBalance": -600, + "subaccounts": [] + }, + { + "number": 4800, + "name": "Zinsen", + "qualifiedName": "Erträge:Zinsen", + "type": "default", + "balance": -32, + "localBalance": -32, + "subaccounts": [] + }, + { + "number": 4900, + "name": "Sonstiges", + "qualifiedName": "Erträge:Sonstiges", + "type": "default", + "balance": 0, + "localBalance": 0, + "subaccounts": [] + } + ] + }, + { + "number": 6000, + "name": "Aufwendungen", + "qualifiedName": "Aufwendungen", + "type": "meta", + "balance": 3598.6, + "localBalance": 0, + "description": "Meta-Konto für alle Ausgaben", + "subaccounts": [ + { + "number": 6100, + "name": "Miete", + "qualifiedName": "Aufwendungen:Miete", + "type": "meta", + "balance": 2240, + "localBalance": 0, + "subaccounts": [ + { + "number": 6110, + "name": "Kaltmiete", + "qualifiedName": "Aufwendungen:Miete:Kaltmiete", + "type": "default", + "balance": 1920, + "localBalance": 1920, + "subaccounts": [] + }, + { + "number": 6120, + "name": "Nebenkosten", + "qualifiedName": "Aufwendungen:Miete:Nebenkosten", + "type": "default", + "balance": 320, + "localBalance": 320, + "subaccounts": [] + } + ] + }, + { + "number": 6200, + "name": "Telekommunikation", + "qualifiedName": "Aufwendungen:Telekommunikation", + "type": "meta", + "balance": 219.6, + "localBalance": 0, + "subaccounts": [ + { + "number": 6210, + "name": "Festnetz", + "qualifiedName": "Aufwendungen:Telekommunikation:Festnetz", + "type": "default", + "balance": 139.8, + "localBalance": 139.8, + "subaccounts": [] + }, + { + "number": 6220, + "name": "Mobilfunk", + "qualifiedName": "Aufwendungen:Telekommunikation:Mobilfunk", + "type": "default", + "balance": 79.8, + "localBalance": 79.8, + "subaccounts": [] + } + ] + }, + { + "number": 6300, + "name": "Semesterbeitrag", + "qualifiedName": "Aufwendungen:Semesterbeitrag", + "type": "default", + "balance": 246, + "localBalance": 246, + "subaccounts": [] + }, + { + "number": 6400, + "name": "Lebensmittel", + "qualifiedName": "Aufwendungen:Lebensmittel", + "type": "default", + "balance": 624, + "localBalance": 624, + "subaccounts": [] + }, + { + "number": 6500, + "name": "Mitgliedschaften", + "qualifiedName": "Aufwendungen:Mitgliedschaften", + "type": "default", + "balance": 79, + "localBalance": 79, + "subaccounts": [] + }, + { + "number": 6900, + "name": "Sonstiges", + "qualifiedName": "Aufwendungen:Sonstiges", + "type": "default", + "balance": 190, + "localBalance": 190, + "subaccounts": [] + }, + { + "number": 9999, + "name": "Testkonto", + "qualifiedName": "Aufwendungen:Testkonto", + "type": "default", + "balance": 0, + "localBalance": 0, + "description": "Moin", + "subaccounts": [] + } + ] + }, + { + "number": 9000, + "name": "Anfangsbestand", + "qualifiedName": "Anfangsbestand", + "type": "default", + "balance": -4494.12, + "localBalance": -4494.12, + "description": "Konto für Anfangsbestand zum 01.11.2023", + "subaccounts": [] + } +] diff --git a/test/transaction_6.json b/test/transaction_6.json new file mode 100644 index 0000000..09cbf67 --- /dev/null +++ b/test/transaction_6.json @@ -0,0 +1,15 @@ +{ + "postingDate": "2024-01-29", + "valueDate": "2024-01-30", + "title": "Gehalt 01/24", + "entries": [ + { + "account": "Aktiva:Barvermögen:Girokonto", + "amount": 1214.66 + }, + { + "account": "Erträge:Gehalt", + "amount": -1214.66 + } + ] +} diff --git a/test/transactions.json b/test/transactions.json new file mode 100644 index 0000000..843d850 --- /dev/null +++ b/test/transactions.json @@ -0,0 +1,332 @@ +[ + { + "id": 1, + "postingDate": "2023-10-31", + "valueDate": "2023-10-31", + "title": "Anfangsbestand zum 01.11.2023" + }, + { + "id": 2, + "postingDate": "2023-12-06", + "valueDate": "2023-12-07", + "title": "Semesterbeitrag SoSe 2024" + }, + { + "id": 3, + "postingDate": "2023-11-20", + "valueDate": "2023-11-20", + "title": "Weihnachtsgeld" + }, + { + "id": 4, + "postingDate": "2023-11-30", + "valueDate": "2023-11-30", + "title": "Gehalt 11/23" + }, + { + "id": 5, + "postingDate": "2023-12-29", + "valueDate": "2023-12-29", + "title": "Gehalt 12/23" + }, + { + "id": 6, + "postingDate": "2024-01-29", + "valueDate": "2024-01-30", + "title": "Gehalt 01/24" + }, + { + "id": 7, + "postingDate": "2024-02-26", + "valueDate": "2024-02-27", + "title": "Gehalt 02/24" + }, + { + "id": 8, + "postingDate": "2023-11-15", + "valueDate": "2023-11-15", + "title": "Kindergeld 11/23" + }, + { + "id": 9, + "postingDate": "2023-12-15", + "valueDate": "2023-12-15", + "title": "Kindergeld 12/23" + }, + { + "id": 10, + "postingDate": "2024-01-15", + "valueDate": "2024-01-15", + "title": "Kindergeld 01/24" + }, + { + "id": 11, + "postingDate": "2024-02-15", + "valueDate": "2024-02-15", + "title": "Kindergeld 02/24" + }, + { + "id": 12, + "postingDate": "2023-11-15", + "valueDate": "2023-11-15", + "title": "Unterhalt 11/23" + }, + { + "id": 13, + "postingDate": "2023-12-15", + "valueDate": "2023-12-15", + "title": "Unterhalt 12/23" + }, + { + "id": 14, + "postingDate": "2024-01-15", + "valueDate": "2024-01-15", + "title": "Unterhalt 01/24" + }, + { + "id": 15, + "postingDate": "2024-02-15", + "valueDate": "2024-02-15", + "title": "Unterhalt 02/24" + }, + { + "id": 16, + "postingDate": "2023-11-03", + "valueDate": "2023-11-03", + "title": "Miete 11/23" + }, + { + "id": 17, + "postingDate": "2023-12-03", + "valueDate": "2023-12-03", + "title": "Miete 12/23" + }, + { + "id": 18, + "postingDate": "2024-01-01", + "valueDate": "2024-01-01", + "title": "Miete 01/24" + }, + { + "id": 19, + "postingDate": "2024-02-02", + "valueDate": "2024-02-02", + "title": "Miete 02/24" + }, + { + "id": 20, + "postingDate": "2023-11-15", + "valueDate": "2023-11-15", + "title": "Festnetz-Rechnung 11/23" + }, + { + "id": 21, + "postingDate": "2023-12-15", + "valueDate": "2023-12-15", + "title": "Festnetz-Rechnung 12/23" + }, + { + "id": 22, + "postingDate": "2024-01-15", + "valueDate": "2024-01-15", + "title": "Festnetz-Rechnung 01/24" + }, + { + "id": 23, + "postingDate": "2024-02-15", + "valueDate": "2024-02-15", + "title": "Festnetz-Rechnung 02/24" + }, + { + "id": 24, + "postingDate": "2023-11-07", + "valueDate": "2023-11-08", + "title": "Mobilfunk-Rechnung 11/23" + }, + { + "id": 25, + "postingDate": "2023-12-07", + "valueDate": "2023-12-08", + "title": "Mobilfunk-Rechnung 12/23" + }, + { + "id": 26, + "postingDate": "2024-01-07", + "valueDate": "2024-01-08", + "title": "Mobilfunk-Rechnung 01/24" + }, + { + "id": 27, + "postingDate": "2024-02-07", + "valueDate": "2024-02-08", + "title": "Mobilfunk-Rechnung 02/24" + }, + { + "id": 29, + "postingDate": "2023-12-11", + "valueDate": "2023-12-13", + "title": "GA: Abhebung" + }, + { + "id": 30, + "postingDate": "2023-11-30", + "valueDate": "2023-12-01", + "title": "GA: Abhebung" + }, + { + "id": 31, + "postingDate": "2024-01-23", + "valueDate": "2024-02-12", + "title": "GA: Kreditkartenabhebung" + }, + { + "id": 32, + "postingDate": "2023-12-01", + "valueDate": "2023-12-04", + "title": "Mitgliedsbeitrag DJH e.V." + }, + { + "id": 33, + "postingDate": "2023-12-28", + "valueDate": "2023-12-28", + "title": "Mitgliedsbeitrag CCC e.V." + }, + { + "id": 34, + "postingDate": "2023-11-03", + "valueDate": "2023-11-03", + "title": "Lebensmittel" + }, + { + "id": 35, + "postingDate": "2023-11-09", + "valueDate": "2023-11-09", + "title": "Lebensmittel" + }, + { + "id": 36, + "postingDate": "2023-11-14", + "valueDate": "2023-11-14", + "title": "Lebensmittel" + }, + { + "id": 37, + "postingDate": "2023-11-18", + "valueDate": "2023-11-18", + "title": "Lebensmittel" + }, + { + "id": 38, + "postingDate": "2023-11-24", + "valueDate": "2023-11-24", + "title": "Lebensmittel" + }, + { + "id": 39, + "postingDate": "2023-12-02", + "valueDate": "2023-12-02", + "title": "Lebensmittel" + }, + { + "id": 40, + "postingDate": "2023-12-08", + "valueDate": "2023-12-08", + "title": "Lebensmittel" + }, + { + "id": 41, + "postingDate": "2023-12-18", + "valueDate": "2023-12-18", + "title": "Lebensmittel" + }, + { + "id": 42, + "postingDate": "2024-01-03", + "valueDate": "2024-01-03", + "title": "Lebensmittel" + }, + { + "id": 43, + "postingDate": "2024-01-09", + "valueDate": "2024-01-09", + "title": "Lebensmittel" + }, + { + "id": 44, + "postingDate": "2024-01-13", + "valueDate": "2024-01-13", + "title": "Lebensmittel" + }, + { + "id": 45, + "postingDate": "2024-01-19", + "valueDate": "2024-01-19", + "title": "Lebensmittel" + }, + { + "id": 46, + "postingDate": "2024-01-26", + "valueDate": "2024-01-26", + "title": "Lebensmittel" + }, + { + "id": 47, + "postingDate": "2024-02-03", + "valueDate": "2024-02-03", + "title": "Lebensmittel" + }, + { + "id": 48, + "postingDate": "2024-02-10", + "valueDate": "2024-02-10", + "title": "Lebensmittel" + }, + { + "id": 52, + "postingDate": "2023-12-01", + "valueDate": "2023-12-04", + "title": "Ticket 37. Chaos Communication Congress" + }, + { + "id": 53, + "postingDate": "2023-12-31", + "valueDate": "2024-01-01", + "title": "Zinsen 4/2024" + }, + { + "id": 54, + "postingDate": "2024-02-12", + "valueDate": "2024-02-12", + "title": "Umbuchung Sparkonto 12.02." + }, + { + "id": 56, + "postingDate": "2024-05-22", + "valueDate": "2024-05-23", + "title": "Test SSE" + }, + { + "id": 57, + "postingDate": "2024-05-23", + "valueDate": "2024-05-24", + "title": "test sse" + }, + { + "id": 58, + "postingDate": "2024-05-23", + "valueDate": "2024-05-24", + "title": "moin" + }, + { + "id": 59, + "postingDate": "2024-05-24", + "valueDate": "2024-05-26", + "title": "Moin" + }, + { + "id": 60, + "postingDate": "2024-05-25", + "valueDate": "2024-05-26", + "title": "Tach" + } +]