version 0.1
This commit is contained in:
+82
-23
@@ -1,4 +1,4 @@
|
||||
const { parseDate } = require('./alipay');
|
||||
const { parseDate } = require("./alipay");
|
||||
|
||||
/**
|
||||
* 银行卡流水CSV解析器
|
||||
@@ -16,33 +16,68 @@ function parseBankCSV(records) {
|
||||
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'] || '';
|
||||
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';
|
||||
let direction = "expense";
|
||||
|
||||
const amountStr = record['交易金额'] || record['金额'] || record['amount'] || '';
|
||||
const incomeStr = record['收入金额'] || record['收入'] || record['credit'] || record['贷方金额'] || '';
|
||||
const expenseStr = record['支出金额'] || record['支出'] || record['debit'] || record['借方金额'] || '';
|
||||
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';
|
||||
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, ''));
|
||||
amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ""));
|
||||
if (amount < 0) {
|
||||
direction = 'expense';
|
||||
direction = "expense";
|
||||
} else {
|
||||
direction = 'income';
|
||||
direction = "income";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +85,7 @@ function parseBankCSV(records) {
|
||||
|
||||
let balance = null;
|
||||
if (balanceStr) {
|
||||
balance = parseFloat(String(balanceStr).replace(/[,,¥¥\s]/g, ''));
|
||||
balance = parseFloat(String(balanceStr).replace(/[,,¥¥\s]/g, ""));
|
||||
if (isNaN(balance)) balance = null;
|
||||
}
|
||||
|
||||
@@ -66,11 +101,35 @@ function parseBankCSV(records) {
|
||||
direction,
|
||||
balance,
|
||||
method: method.trim(),
|
||||
raw_data: JSON.stringify(record)
|
||||
raw_data: JSON.stringify(record),
|
||||
});
|
||||
}
|
||||
|
||||
return transactions;
|
||||
}
|
||||
|
||||
module.exports = { parseBankCSV };
|
||||
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 };
|
||||
|
||||
Reference in New Issue
Block a user