Add income and expense totals to transaction/deal list pages

Add income and expense totals to transaction and deal list pages
This commit is contained in:
2026-05-06 00:19:38 +08:00
parent cf88ede5bc
commit eefe22c8b7
3 changed files with 82 additions and 23 deletions
+8
View File
@@ -60,6 +60,12 @@ router.get("/", (req, res) => {
.prepare(`SELECT COUNT(*) as c FROM deals d ${where}`)
.get(...params).c;
const totalsResult = db
.prepare(
`SELECT COALESCE(SUM(CASE WHEN d.direction='income' THEN d.amount ELSE 0 END),0) as totalIncome, COALESCE(SUM(CASE WHEN d.direction='expense' THEN ABS(d.amount) ELSE 0 END),0) as totalExpense FROM deals d ${where}`,
)
.get(...params);
const deals = db
.prepare(
`
@@ -92,6 +98,8 @@ router.get("/", (req, res) => {
res.json({
deals,
total,
totalIncome: totalsResult.totalIncome,
totalExpense: totalsResult.totalExpense,
page: Number(page),
totalPages: Math.ceil(total / limit),
});
+8
View File
@@ -92,6 +92,12 @@ router.get("/", (req, res) => {
.prepare(`SELECT COUNT(*) as total FROM transactions t ${whereClause}`)
.get(...params);
const totalsResult = db
.prepare(
`SELECT COALESCE(SUM(CASE WHEN t.direction='income' THEN t.amount ELSE 0 END),0) as totalIncome, COALESCE(SUM(CASE WHEN t.direction='expense' THEN ABS(t.amount) ELSE 0 END),0) as totalExpense FROM transactions t ${whereClause}`,
)
.get(...params);
const transactions = db
.prepare(
`
@@ -108,6 +114,8 @@ router.get("/", (req, res) => {
res.json({
transactions,
total: countResult.total,
totalIncome: totalsResult.totalIncome,
totalExpense: totalsResult.totalExpense,
page: parseInt(page),
limit: parseInt(limit),
totalPages: Math.ceil(countResult.total / parseInt(limit)),