Files
2026-05-05 16:36:57 +08:00

118 lines
3.4 KiB
JavaScript

const XLSX = require("xlsx");
const dayjs = require("dayjs");
/**
* 浦发银行 XLS/XLSX 流水解析
*
* 格式:
* Row 0: 元数据 (账号, 账户类型, 日期范围)
* Row 1: 表头 (顺序号, 交易日期, 交易类型, 存入金额, 取出金额, 余额, 交易对手名称, 交易对手账号, 附言)
* Row 2+: 数据
*/
function parseSPDBXLS(filePath) {
const workbook = XLSX.readFile(filePath);
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const rows = XLSX.utils.sheet_to_json(sheet, {
header: 1,
defval: "",
raw: true,
});
if (!rows || rows.length < 3) return [];
// 提取账号
const accountNo = String(rows[0][1] || "");
const transactions = [];
for (let i = 2; i < rows.length; i++) {
const row = rows[i];
if (!row || row.length < 8) continue;
const dateStr = String(row[1] || "").trim(); // "2025/05/06 01:12:46"
const typeRaw = String(row[2] || "").trim(); // "网上支付-财付通—来伊份"
const depositStr = row[3]; // 存入金额
const withdrawStr = row[4]; // 取出金额
const balanceStr = row[5]; // 余额
const counterparty = String(row[6] || "").trim(); // 对手名称
const opponentAcct = String(row[7] || "").trim(); // 对手账号
const appendix = String(row[8] || "").trim(); // 附言
if (!dateStr) continue;
// 金额:存入或取出
let amount = 0;
let direction = "expense";
if (depositStr !== "" && depositStr !== undefined && depositStr !== null) {
amount = parseFloat(depositStr);
direction = "income";
} else if (
withdrawStr !== "" &&
withdrawStr !== undefined &&
withdrawStr !== null
) {
amount = -parseFloat(withdrawStr);
direction = "expense";
}
if (isNaN(amount) || amount === 0) continue;
const balance = balanceStr ? parseFloat(balanceStr) : null;
// 日期/时间解析
let parsed = dayjs(dateStr, [
"YYYY/MM/DD HH:mm:ss",
"YYYY-MM-DD HH:mm:ss",
"YYYY/MM/DD",
]);
// UTC → CST (+8h)
parsed = parsed.add(8, "hour");
const txDate = parsed.format("YYYY-MM-DD");
const txTime = parsed.format("YYYY-MM-DD HH:mm:ss");
// 交易类型拆解: "网上支付-财付通—来伊份" → type="网上支付-财付通", desc="网上支付-财付通—来伊份"
let description = typeRaw;
let txType = typeRaw;
const dashIdx = typeRaw.indexOf("—");
if (dashIdx > 0) {
txType = typeRaw.substring(0, dashIdx);
}
// 支付方式
let method = "";
if (typeRaw.includes("财付通")) method = "财付通";
else if (typeRaw.includes("支付宝")) method = "支付宝";
else if (typeRaw.includes("工资")) method = "代发工资";
else if (typeRaw.includes("转账")) method = "银行转账";
else if (typeRaw.includes("银联")) method = "银联";
else method = txType;
transactions.push({
transaction_date: txDate,
transaction_time: txTime,
counterparty,
description,
notes: appendix, // 附言
amount,
direction,
balance,
method,
account_no: accountNo,
opponent_account: opponentAcct,
raw_data: JSON.stringify({
dateStr,
typeRaw,
depositStr,
withdrawStr,
balanceStr,
counterparty,
opponentAcct,
appendix,
}),
});
}
return transactions;
}
module.exports = { parseSPDBXLS };