feat: 新增记账系统(收支记录、报销跟踪、月度汇总)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { db, DEFAULT_LOCATIONS, SCHEMA_VERSION, type Attendance, type Credential, type LogItem, type Memo, type Project, type WeeklyNote, type WorkLog } from './schema'
|
||||
import { db, DEFAULT_LOCATIONS, SCHEMA_VERSION, type Attendance, type Credential, type LogItem, type Memo, type Project, type Transaction, type WeeklyNote, type WorkLog } from './schema'
|
||||
import { addDays, nowLocalISO, toDateStr } from '../lib/date'
|
||||
|
||||
// ---------- 设置 ----------
|
||||
@@ -347,6 +347,38 @@ export async function searchMemos(q: string, limit = 4): Promise<Memo[]> {
|
||||
.slice(0, limit)
|
||||
}
|
||||
|
||||
// ---------- 记账 ----------
|
||||
|
||||
export interface TransactionInput {
|
||||
type: string
|
||||
category: string
|
||||
amount: number
|
||||
date: string
|
||||
note: string
|
||||
reimbursable: number
|
||||
reimbursed: number
|
||||
}
|
||||
|
||||
export async function listTransactionsBetween(start: string, end: string): Promise<Transaction[]> {
|
||||
return db.transaction.where('date').between(start, end, true, true).toArray()
|
||||
}
|
||||
|
||||
export async function addTransaction(input: TransactionInput): Promise<void> {
|
||||
await db.transaction.add({ ...input, created_at: nowLocalISO() })
|
||||
}
|
||||
|
||||
export async function updateTransaction(id: number, input: TransactionInput): Promise<void> {
|
||||
await db.transaction.update(id, { ...input })
|
||||
}
|
||||
|
||||
export async function deleteTransaction(id: number): Promise<void> {
|
||||
await db.transaction.delete(id)
|
||||
}
|
||||
|
||||
export async function setTransactionReimbursed(id: number, done: boolean): Promise<void> {
|
||||
await db.transaction.update(id, { reimbursed: done ? 1 : 0 })
|
||||
}
|
||||
|
||||
// ---------- 备份 ----------
|
||||
|
||||
export async function exportAll(): Promise<string> {
|
||||
@@ -358,7 +390,8 @@ export async function exportAll(): Promise<string> {
|
||||
db.weekly_note.toArray(),
|
||||
db.setting.toArray(),
|
||||
db.memo.toArray(),
|
||||
db.credential.toArray()
|
||||
db.credential.toArray(),
|
||||
db.transaction.toArray()
|
||||
])
|
||||
return JSON.stringify({
|
||||
schema_version: SCHEMA_VERSION,
|
||||
@@ -371,7 +404,8 @@ export async function exportAll(): Promise<string> {
|
||||
weekly_note: tables[4],
|
||||
setting: tables[5],
|
||||
memo: tables[6],
|
||||
credential: tables[7]
|
||||
credential: tables[7],
|
||||
transaction: tables[8]
|
||||
}
|
||||
}, null, 2)
|
||||
}
|
||||
@@ -383,15 +417,15 @@ export async function importAll(jsonText: string): Promise<void> {
|
||||
} catch {
|
||||
throw new Error('不是有效的 JSON 文件')
|
||||
}
|
||||
if (!parsed || (parsed.schema_version !== 1 && parsed.schema_version !== 2) || !parsed.data) {
|
||||
if (!parsed || (parsed.schema_version !== 1 && parsed.schema_version !== 2 && parsed.schema_version !== 3) || !parsed.data) {
|
||||
throw new Error('文件格式或 schema 版本不匹配')
|
||||
}
|
||||
const d = parsed.data
|
||||
await db.transaction('rw', [db.project, db.work_log, db.log_item, db.attendance, db.weekly_note, db.setting, db.memo, db.credential], async () => {
|
||||
await db.transaction('rw', [db.project, db.work_log, db.log_item, db.attendance, db.weekly_note, db.setting, db.memo, db.credential, db.transaction], async () => {
|
||||
await Promise.all([
|
||||
db.project.clear(), db.work_log.clear(), db.log_item.clear(),
|
||||
db.attendance.clear(), db.weekly_note.clear(), db.setting.clear(),
|
||||
db.memo.clear(), db.credential.clear()
|
||||
db.memo.clear(), db.credential.clear(), db.transaction.clear()
|
||||
])
|
||||
if (d.project && d.project.length) await db.project.bulkAdd(d.project as never[])
|
||||
if (d.work_log && d.work_log.length) await db.work_log.bulkAdd(d.work_log as never[])
|
||||
@@ -401,5 +435,6 @@ export async function importAll(jsonText: string): Promise<void> {
|
||||
if (d.setting && d.setting.length) await db.setting.bulkAdd(d.setting as never[])
|
||||
if (d.memo && d.memo.length) await db.memo.bulkAdd(d.memo as never[])
|
||||
if (d.credential && d.credential.length) await db.credential.bulkAdd(d.credential as never[])
|
||||
if (d.transaction && d.transaction.length) await db.transaction.bulkAdd(d.transaction as never[])
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user