136 lines
3.6 KiB
JavaScript
136 lines
3.6 KiB
JavaScript
const { parseDate } = require("./alipay");
|
|
|
|
/**
|
|
* 银行卡流水CSV解析器
|
|
*
|
|
* 银行导出的CSV格式因银行而异,常见格式:
|
|
* 交易日期,摘要,交易金额,余额,交易对方,备注
|
|
* 交易日期,收入金额,支出金额,余额,摘要
|
|
* 日期,类型,金额,余额,说明
|
|
*
|
|
* 此解析器尝试自动识别列名
|
|
*/
|
|
function parseBankCSV(records) {
|
|
const transactions = [];
|
|
|
|
for (const record of records) {
|
|
if (!record || Object.keys(record).length === 0) continue;
|
|
|
|
const dateStr =
|
|
record["交易日期"] ||
|
|
record["日期"] ||
|
|
record["交易时间"] ||
|
|
record["date"] ||
|
|
record["time"] ||
|
|
"";
|
|
const description =
|
|
record["摘要"] ||
|
|
record["说明"] ||
|
|
record["description"] ||
|
|
record["summary"] ||
|
|
record["用途"] ||
|
|
"";
|
|
const counterparty =
|
|
record["交易对方"] ||
|
|
record["对方"] ||
|
|
record["counterparty"] ||
|
|
record["对方户名"] ||
|
|
"";
|
|
const notes = record["备注"] || record["memo"] || record["附言"] || "";
|
|
const balanceStr = record["余额"] || record["balance"] || "";
|
|
const method =
|
|
record["交易方式"] || record["渠道"] || record["method"] || "";
|
|
|
|
// 银行通常有两种格式:单列金额(正负表示方向) 或 分列收入/支出
|
|
let amount = 0;
|
|
let direction = "expense";
|
|
|
|
const amountStr =
|
|
record["交易金额"] || record["金额"] || record["amount"] || "";
|
|
const incomeStr =
|
|
record["收入金额"] ||
|
|
record["收入"] ||
|
|
record["credit"] ||
|
|
record["贷方金额"] ||
|
|
"";
|
|
const expenseStr =
|
|
record["支出金额"] ||
|
|
record["支出"] ||
|
|
record["debit"] ||
|
|
record["借方金额"] ||
|
|
"";
|
|
|
|
if (
|
|
incomeStr &&
|
|
parseFloat(String(incomeStr).replace(/[,,¥¥\s]/g, "")) !== 0
|
|
) {
|
|
amount = parseFloat(String(incomeStr).replace(/[,,¥¥\s]/g, ""));
|
|
direction = "income";
|
|
} else if (
|
|
expenseStr &&
|
|
parseFloat(String(expenseStr).replace(/[,,¥¥\s]/g, "")) !== 0
|
|
) {
|
|
amount = -parseFloat(String(expenseStr).replace(/[,,¥¥\s]/g, ""));
|
|
direction = "expense";
|
|
} else if (amountStr) {
|
|
amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ""));
|
|
if (amount < 0) {
|
|
direction = "expense";
|
|
} else {
|
|
direction = "income";
|
|
}
|
|
}
|
|
|
|
if (isNaN(amount)) amount = 0;
|
|
|
|
let balance = null;
|
|
if (balanceStr) {
|
|
balance = parseFloat(String(balanceStr).replace(/[,,¥¥\s]/g, ""));
|
|
if (isNaN(balance)) balance = null;
|
|
}
|
|
|
|
const parsedDate = parseDate(dateStr);
|
|
|
|
transactions.push({
|
|
transaction_date: parsedDate,
|
|
transaction_time: dateStr,
|
|
counterparty: counterparty.trim(),
|
|
description: description.trim(),
|
|
notes: notes.trim(),
|
|
amount,
|
|
direction,
|
|
balance,
|
|
method: method.trim(),
|
|
raw_data: JSON.stringify(record),
|
|
});
|
|
}
|
|
|
|
return transactions;
|
|
}
|
|
|
|
const XLSX = require("xlsx");
|
|
|
|
function parseBankFromXLSX(filePath) {
|
|
const workbook = XLSX.readFile(filePath);
|
|
const sheetName = workbook.SheetNames[0];
|
|
const sheet = workbook.Sheets[sheetName];
|
|
const rows = XLSX.utils.sheet_to_json(sheet, {
|
|
header: 1,
|
|
defval: "",
|
|
raw: true,
|
|
});
|
|
if (!rows || rows.length < 2) return [];
|
|
// Fallback: use CSV parser on the rows treated as records
|
|
const headerRow = rows[0];
|
|
const dataRows = rows.slice(1).map((row) => {
|
|
const obj = {};
|
|
headerRow.forEach((h, i) => {
|
|
obj[h] = row[i];
|
|
});
|
|
return obj;
|
|
});
|
|
return parseBankCSV(dataRows);
|
|
}
|
|
|
|
module.exports = { parseBankCSV, parseBankFromXLSX };
|