diff --git a/schema.sql b/schema.sql index 92c6992..b12e2d2 100644 --- a/schema.sql +++ b/schema.sql @@ -258,3 +258,41 @@ CREATE VIEW ReconstructedBankStatement AS WHERE targetCredit IS NULL -- exclude internal transfers ORDER BY date ASC ; + +-- History view: All incoming, outgoing payments and internal transfers +CREATE VIEW History AS + SELECT c.date AS date, + c.purpose AS purpose, + account, + c.value AS credit, + NULL AS debit, + NULL AS contra, + NULL AS billId + FROM Credit AS c + LEFT OUTER JOIN Debit AS d ON c.ID=d.targetCredit + GROUP BY c.ID + HAVING count(d.billId) == 0 -- exclude internal transfers + UNION -- internal transfers with account as source + SELECT DATE(timestamp) AS date, + d.purpose AS purpose, + d.debtor AS account, + NULL AS credit, + t.amount AS debit, + c.account AS contra, + d.billId AS billId + FROM Transfer t + LEFT JOIN Credit AS c ON c.Id = t.fromCredit + LEFT JOIN Debit AS d ON d.billId = t.billId + UNION -- internal transfers with account as target + SELECT DATE(timestamp) AS date, + d.purpose AS purpose, + c.account AS account, + t.amount AS credit, + NULL AS debit, + d.debtor AS contra, + d.billId AS billId + FROM Transfer t + LEFT JOIN Debit AS d ON d.billId = t.billId + LEFT JOIN Credit AS c ON c.Id = t.fromCredit + ORDER BY date ASC +; diff --git a/t/schema.out b/t/schema.out index d76b92d..57fe5c5 100644 --- a/t/schema.out +++ b/t/schema.out @@ -9,9 +9,9 @@ john: 0 +0 0 Club: 0 +0 -16250 alex: 7200 +16250 0 # Some updates and deletes that could, unless denied, destroy consistency ... -Error: near line 300: paid is set and adjusted automatically according to added Transfer records -Error: near line 301: Debt is involved in transfers to revoke at first -Error: near line 302: FOREIGN KEY constraint failed +Error: near line 338: paid is set and adjusted automatically according to added Transfer records +Error: near line 339: Debt is involved in transfers to revoke at first +Error: near line 340: FOREIGN KEY constraint failed # After revoking transactions, you are free to change or delete debts and credits ... Club: 7200 +0 0 alex: 0 +0 0