122 lines
5.2 KiB
JavaScript
122 lines
5.2 KiB
JavaScript
const XLSX = require('xlsx');
|
|
const iconv = require('iconv-lite');
|
|
const fs = require('fs');
|
|
const dayjs = require('dayjs');
|
|
|
|
function parseAlipayCSV(records) {
|
|
const transactions = [];
|
|
for (const record of records) {
|
|
if (!record || Object.keys(record).length === 0) continue;
|
|
const dateStr = record['交易时间'] || record['交易创建时间'] || record['交易日期'] || record['付款时间'] || '';
|
|
const counterparty = record['交易对方'] || record['交易对象'] || record['counterparty'] || '';
|
|
const description = record['商品说明'] || record['商品名称'] || record['商品'] || record['description'] || '';
|
|
const notes = record['备注'] || record['memo'] || '';
|
|
const directionStr = record['收/支'] || record['收支'] || record['direction'] || '';
|
|
const amountStr = record['金额'] || record['金额(元)'] || record['amount'] || '0';
|
|
const status = record['交易状态'] || record['status'] || '';
|
|
const method = record['收/付款方式'] || record['支付方式'] || record['method'] || '支付宝';
|
|
const orderNo = record['交易号'] || record['交易订单号'] || record['订单号'] || '';
|
|
|
|
if (!dateStr && !amountStr) continue;
|
|
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ''));
|
|
if (isNaN(amount)) amount = 0;
|
|
|
|
let direction = 'expense';
|
|
if (directionStr.includes('收入') || directionStr === '收入') direction = 'income';
|
|
else if (directionStr.includes('支')) direction = 'expense';
|
|
|
|
if (direction === 'expense') amount = -Math.abs(amount);
|
|
else amount = Math.abs(amount);
|
|
|
|
transactions.push({
|
|
transaction_date: parseDate(dateStr),
|
|
transaction_time: dateStr.includes(':') ? dateStr.trim() : parseDate(dateStr),
|
|
counterparty: counterparty.trim(), description: description.trim(), notes: notes.trim(),
|
|
amount, direction, status: status.trim(), method, order_no: orderNo.trim(),
|
|
raw_data: JSON.stringify(record)
|
|
});
|
|
}
|
|
return transactions;
|
|
}
|
|
|
|
function parseDate(s) {
|
|
if (!s) return dayjs().format('YYYY-MM-DD');
|
|
const fmts = ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD HH:mm', 'YYYY-MM-DD', 'YYYY/MM/DD HH:mm:ss', 'YYYY/MM/DD'];
|
|
for (const f of fmts) { const d = dayjs(s, f); if (d.isValid()) return d.format('YYYY-MM-DD'); }
|
|
const d = dayjs(s); return d.isValid() ? d.format('YYYY-MM-DD') : dayjs().format('YYYY-MM-DD');
|
|
}
|
|
|
|
// ── 新版支付宝 GBK CSV(Tab+逗号混合分隔) ──
|
|
|
|
function parseAlipayFile(filePath) {
|
|
const buf = fs.readFileSync(filePath);
|
|
let content;
|
|
try { content = iconv.decode(buf, 'gbk'); } catch (e) { content = buf.toString('utf-8'); }
|
|
|
|
const lines = content.split('\n').filter(l => l.trim());
|
|
if (lines.length < 5) return [];
|
|
|
|
const skip = ['支付宝', '账号', '起始日期', '---', '导出时间', '待收入', '已支出', '待支出', '交易号'];
|
|
|
|
const transactions = [];
|
|
for (const line of lines) {
|
|
if (skip.some(p => line.startsWith(p))) continue;
|
|
|
|
// 逗号分割 + 清理前导 tab 和空白
|
|
const cols = line.split(',').map(c => c.replace(/^\t/, '').trim());
|
|
if (cols.length < 12) continue;
|
|
|
|
const tradeNo = cols[0];
|
|
const createTime = cols[2]; // 交易创建时间
|
|
const payTime = cols[3]; // 付款时间
|
|
const source = cols[5]; // 交易来源地
|
|
const type = cols[6]; // 类型
|
|
const counterparty = cols[7]; // 交易对方
|
|
const product = cols[8]; // 商品名称
|
|
const amountStr = cols[9]; // 金额
|
|
const directionStr = cols[10]; // 收/支
|
|
const status = cols[11]; // 交易状态
|
|
const refundStr = cols[13]; // 成功退款(元)
|
|
const notes = cols[14] || ''; // 备注
|
|
|
|
// 跳过:不计收支 / 已成功退款的交易
|
|
if (directionStr.includes('不计收支')) continue;
|
|
if (refundStr && parseFloat(refundStr) > 0) continue;
|
|
|
|
// 优先付款时间,退款时可能为空
|
|
const dateStr = payTime || createTime;
|
|
if (!dateStr || !amountStr) continue;
|
|
|
|
let amount = parseFloat(amountStr);
|
|
if (isNaN(amount) || amount === 0) continue;
|
|
|
|
let direction = 'expense';
|
|
if (directionStr.includes('收入')) direction = 'income';
|
|
else if (directionStr.includes('不计收支')) direction = 'income';
|
|
else if (directionStr.includes('支出')) direction = 'expense';
|
|
|
|
if (direction === 'expense') amount = -Math.abs(amount);
|
|
else amount = Math.abs(amount);
|
|
|
|
transactions.push({
|
|
transaction_date: parseDate(dateStr),
|
|
transaction_time: dateStr.includes(':') ? dateStr : parseDate(dateStr),
|
|
counterparty, description: product || type, notes, amount, direction, status,
|
|
method: source.includes('淘宝') ? '淘宝' : '支付宝', order_no: tradeNo,
|
|
raw_data: JSON.stringify({ cols })
|
|
});
|
|
}
|
|
return transactions;
|
|
}
|
|
|
|
function parseAlipayFromXLSX(filePath) {
|
|
const wb = XLSX.readFile(filePath);
|
|
const ws = wb.Sheets[wb.SheetNames[0]];
|
|
const rows = XLSX.utils.sheet_to_json(ws, { header: 1, defval: '', raw: true });
|
|
if (!rows || rows.length < 2) return [];
|
|
const headers = rows[0];
|
|
return parseAlipayCSV(rows.slice(1).map(row => { const o = {}; headers.forEach((h, i) => { o[h] = row[i]; }); return o; }));
|
|
}
|
|
|
|
module.exports = { parseAlipayCSV, parseAlipayFromXLSX, parseAlipayFile };
|