version 0.1
This commit is contained in:
+57
-13
@@ -1,4 +1,4 @@
|
||||
const { parseDate } = require('./alipay');
|
||||
const { parseDate } = require("./alipay");
|
||||
|
||||
/**
|
||||
* 信用卡账单CSV解析器
|
||||
@@ -13,23 +13,44 @@ function parseCreditCardCSV(records) {
|
||||
for (const record of records) {
|
||||
if (!record || Object.keys(record).length === 0) continue;
|
||||
|
||||
const dateStr = record['交易日期'] || record['交易日'] || record['date'] || record['transaction_date'] || '';
|
||||
const postDateStr = record['入账日期'] || record['记账日'] || record['post_date'] || dateStr;
|
||||
const description = record['交易说明'] || record['交易描述'] || record['description'] || record['商品'] || '';
|
||||
const counterparty = record['交易对方'] || record['商户名称'] || record['merchant'] || record['counterparty'] || '';
|
||||
const notes = record['备注'] || record['memo'] || '';
|
||||
const dateStr =
|
||||
record["交易日期"] ||
|
||||
record["交易日"] ||
|
||||
record["date"] ||
|
||||
record["transaction_date"] ||
|
||||
"";
|
||||
const postDateStr =
|
||||
record["入账日期"] || record["记账日"] || record["post_date"] || dateStr;
|
||||
const description =
|
||||
record["交易说明"] ||
|
||||
record["交易描述"] ||
|
||||
record["description"] ||
|
||||
record["商品"] ||
|
||||
"";
|
||||
const counterparty =
|
||||
record["交易对方"] ||
|
||||
record["商户名称"] ||
|
||||
record["merchant"] ||
|
||||
record["counterparty"] ||
|
||||
"";
|
||||
const notes = record["备注"] || record["memo"] || "";
|
||||
|
||||
// 信用卡金额通常是正数表示消费(支出),负数表示还款(收入)
|
||||
const amountStr = record['交易金额'] || record['人民币金额'] || record['金额'] || record['amount'] || '0';
|
||||
const postAmountStr = record['入账金额'] || record['记账金额'] || '';
|
||||
const amountStr =
|
||||
record["交易金额"] ||
|
||||
record["人民币金额"] ||
|
||||
record["金额"] ||
|
||||
record["amount"] ||
|
||||
"0";
|
||||
const postAmountStr = record["入账金额"] || record["记账金额"] || "";
|
||||
|
||||
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ''));
|
||||
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ""));
|
||||
if (isNaN(amount)) amount = 0;
|
||||
|
||||
// 信用卡:正数=消费(欠款增加,对用户来说是支出)
|
||||
let direction = 'expense';
|
||||
let direction = "expense";
|
||||
if (amount < 0) {
|
||||
direction = 'income'; // 负数=还款/退款
|
||||
direction = "income"; // 负数=还款/退款
|
||||
amount = Math.abs(amount); // 存储为正值收入
|
||||
} else {
|
||||
amount = -Math.abs(amount); // 存储为负值支出
|
||||
@@ -46,11 +67,34 @@ function parseCreditCardCSV(records) {
|
||||
amount,
|
||||
direction,
|
||||
post_date: parseDate(postDateStr),
|
||||
raw_data: JSON.stringify(record)
|
||||
raw_data: JSON.stringify(record),
|
||||
});
|
||||
}
|
||||
|
||||
return transactions;
|
||||
}
|
||||
|
||||
module.exports = { parseCreditCardCSV };
|
||||
const XLSX = require("xlsx");
|
||||
|
||||
function parseCreditFromXLSX(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 [];
|
||||
const headerRow = rows[0];
|
||||
const dataRows = rows.slice(1).map((row) => {
|
||||
const obj = {};
|
||||
headerRow.forEach((h, i) => {
|
||||
obj[h] = row[i];
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
return parseCreditCardCSV(dataRows);
|
||||
}
|
||||
|
||||
module.exports = { parseCreditCardCSV, parseCreditFromXLSX };
|
||||
|
||||
Reference in New Issue
Block a user