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
+66 -23
View File
@@ -23,7 +23,7 @@ import {
function fmtDatetime(dt) {
if (!dt) return "";
return String(dt).replace(/:00$/, "").substring(0, 16);
return String(dt).substring(0, 19);
}
const DEAL_FILTER_DEFAULT = {
@@ -44,6 +44,8 @@ export default function Transactions() {
total: 0,
page: 1,
totalPages: 0,
totalIncome: 0,
totalExpense: 0,
});
const [filters, setFilters] = useState({
page: 1,
@@ -68,12 +70,14 @@ export default function Transactions() {
total: 0,
page: 1,
totalPages: 0,
totalIncome: 0,
totalExpense: 0,
});
const [clearConfirm, setClearConfirm] = useState(false);
const [editingDeal, setEditingDeal] = useState(null);
const [dealFilters, setDealFilters] = useState({ ...DEAL_FILTER_DEFAULT });
/* ── sort helpers ── */
/* sort helpers */
function dealToggleSort(field) {
setDealFilters((p) => ({
...p,
@@ -133,7 +137,7 @@ export default function Transactions() {
setToast({ message: msg, type, key: Date.now() });
}
/* ── restore classify progress ── */
/* restore classify progress */
useEffect(() => {
const saved = sessionStorage.getItem("classifyTask");
if (!saved) return;
@@ -226,7 +230,7 @@ export default function Transactions() {
}));
}
/* ── deal edit / clear ── */
/* deal edit / clear */
async function handleUpdateDeal() {
if (!editingDeal) return;
try {
@@ -310,7 +314,9 @@ export default function Transactions() {
setClassifying(false);
}
/* ════════════════════════════════ RENDER ════════════════════════════════ */
/* ============================== RENDER ============================== */
const hasTotals = data.totalIncome || data.totalExpense;
return (
<div>
<div className="page-header">
@@ -318,7 +324,6 @@ export default function Transactions() {
<p>查看、筛选、排序、对账和分类</p>
</div>
{/* 操作按钮行 */}
<div
style={{ display: "flex", gap: 10, marginBottom: 20, flexWrap: "wrap" }}
>
@@ -346,7 +351,7 @@ export default function Transactions() {
</button>
</div>
{/* ── 交易记录 ── */}
{/* --- 交易记录 --- */}
<div className="card">
<div className="card-header">
<h3>交易记录</h3>
@@ -355,7 +360,6 @@ export default function Transactions() {
</span>
</div>
{/* 筛选栏 */}
<div className="filter-bar" style={{ padding: "0 20px 12px" }}>
<div className="form-group">
<label>开始</label>
@@ -412,6 +416,27 @@ export default function Transactions() {
onChange={(e) => updateFilter("keyword", e.target.value)}
/>
</div>
{hasTotals ? (
<div
style={{
alignSelf: "flex-end",
fontSize: 12,
color: "var(--text-secondary)",
whiteSpace: "nowrap",
paddingBottom: 8,
marginLeft: "auto",
}}
>
筛选结果 · 收入{" "}
<span style={{ color: "var(--success)", fontWeight: 600 }}>
+{(data.totalIncome || 0).toFixed(2)}
</span>{" "}
· 支出{" "}
<span style={{ color: "var(--danger)", fontWeight: 600 }}>
-{(data.totalExpense || 0).toFixed(2)}
</span>
</div>
) : null}
</div>
{loading ? (
@@ -459,12 +484,11 @@ export default function Transactions() {
textAlign: "right",
fontWeight: 600,
whiteSpace: "nowrap",
color:
tx.direction === "income"
? "var(--success)"
: "var(--danger)",
}}
className={
tx.direction === "income"
? "stat-value income"
: "stat-value expense"
}
>
{formatAmount(tx.amount)}
</td>
@@ -534,7 +558,7 @@ export default function Transactions() {
)}
</div>
{/* ── 关联交易 ── */}
{/* --- 关联交易 --- */}
<div className="card" style={{ marginTop: 20 }}>
<div className="card-header">
<h3>关联交易</h3>
@@ -543,7 +567,6 @@ export default function Transactions() {
</span>
</div>
{/* 筛选栏 */}
<div className="filter-bar" style={{ padding: "0 20px 12px" }}>
<div className="form-group">
<label>开始</label>
@@ -600,6 +623,27 @@ export default function Transactions() {
onChange={(e) => updateDealFilter("keyword", e.target.value)}
/>
</div>
{deals.totalIncome || deals.totalExpense ? (
<div
style={{
alignSelf: "flex-end",
fontSize: 12,
color: "var(--text-secondary)",
whiteSpace: "nowrap",
paddingBottom: 8,
marginLeft: "auto",
}}
>
筛选结果 · 收入{" "}
<span style={{ color: "var(--success)", fontWeight: 600 }}>
+{(deals.totalIncome || 0).toFixed(2)}
</span>{" "}
· 支出{" "}
<span style={{ color: "var(--danger)", fontWeight: 600 }}>
-{(deals.totalExpense || 0).toFixed(2)}
</span>
</div>
) : null}
</div>
{dealLoading ? (
@@ -649,12 +693,11 @@ export default function Transactions() {
textAlign: "right",
fontWeight: 600,
whiteSpace: "nowrap",
color:
parseFloat(d.amount) >= 0
? "var(--success)"
: "var(--danger)",
}}
className={
parseFloat(d.amount) >= 0
? "stat-value income"
: "stat-value expense"
}
>
{formatAmount(d.amount)}
</td>
@@ -742,7 +785,7 @@ export default function Transactions() {
)}
</div>
{/* ── 清空确认弹窗 ── */}
{/* --- 清空确认弹窗 --- */}
{clearConfirm && (
<div className="modal-overlay">
<div className="modal" style={{ maxWidth: 400 }}>
@@ -773,7 +816,7 @@ export default function Transactions() {
</div>
)}
{/* ── Toast ── */}
{/* --- Toast --- */}
{toast && (
<div
style={{
@@ -822,7 +865,7 @@ export default function Transactions() {
</div>
)}
{/* ── 编辑 Deal 弹窗 ── */}
{/* --- 编辑 Deal 弹窗 --- */}
{editingDeal && (
<div className="modal-overlay">
<div
+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)),