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:
@@ -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),
|
||||
});
|
||||
|
||||
@@ -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)),
|
||||
|
||||
Reference in New Issue
Block a user