71 lines
2.5 KiB
JavaScript
71 lines
2.5 KiB
JavaScript
const { parseDate } = require('./alipay');
|
|
|
|
/**
|
|
* 微信账单CSV解析器
|
|
*
|
|
* 微信导出的CSV格式:
|
|
* 交易时间,交易类型,交易对方,商品,收/支,金额(元),支付方式,当前状态,交易单号,商户单号,备注
|
|
*/
|
|
function parseWechatCSV(records) {
|
|
const transactions = [];
|
|
|
|
for (const record of records) {
|
|
if (!record || Object.keys(record).length === 0) continue;
|
|
|
|
const dateStr = record['交易时间'] || record['时间'] || record['time'] || record['date'] || '';
|
|
const type = record['交易类型'] || record['类型'] || record['transaction_type'] || '';
|
|
const counterparty = record['交易对方'] || record['对方'] || record['counterparty'] || '';
|
|
const description = record['商品'] || record['商品名称'] || record['product'] || record['description'] || '';
|
|
const directionStr = record['收/支'] || record['收支'] || record['direction'] || '';
|
|
const amountStr = record['金额(元)'] || record['金额'] || record['amount'] || '0';
|
|
const method = record['支付方式'] || record['method'] || record['payment'] || '';
|
|
const status = record['当前状态'] || record['状态'] || record['status'] || '';
|
|
const orderNo = record['交易单号'] || record['订单号'] || record['order_no'] || '';
|
|
const notes = record['备注'] || record['memo'] || '';
|
|
|
|
if (!dateStr && !amountStr) continue;
|
|
|
|
let amount = parseFloat(String(amountStr).replace(/[,,¥¥\s]/g, ''));
|
|
if (isNaN(amount)) amount = 0;
|
|
|
|
let direction = 'expense';
|
|
if (directionStr.includes('收入') || directionStr.includes('收')) {
|
|
direction = 'income';
|
|
} else if (directionStr.includes('支出') || directionStr.includes('支')) {
|
|
direction = 'expense';
|
|
} else if (amount > 0) {
|
|
direction = 'income';
|
|
} else {
|
|
amount = -Math.abs(amount);
|
|
direction = 'expense';
|
|
}
|
|
|
|
if (direction === 'expense') {
|
|
amount = -Math.abs(amount);
|
|
} else {
|
|
amount = Math.abs(amount);
|
|
}
|
|
|
|
const parsedDate = parseDate(dateStr);
|
|
|
|
transactions.push({
|
|
transaction_date: parsedDate,
|
|
transaction_time: dateStr,
|
|
counterparty: counterparty.trim(),
|
|
description: description.trim() || type.trim(),
|
|
notes: notes.trim(),
|
|
amount,
|
|
direction,
|
|
status: status.trim(),
|
|
method: method.trim(),
|
|
order_no: orderNo.trim(),
|
|
transaction_type: type.trim(),
|
|
raw_data: JSON.stringify(record)
|
|
});
|
|
}
|
|
|
|
return transactions;
|
|
}
|
|
|
|
module.exports = { parseWechatCSV };
|