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