1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 | 1x
1x
4x
1x
2x
1x
6x
2x
2x
2x
2x
2x
2x
6x
6x
1x
1x
6x
1x
2x
2x
2x
2x
4x
2x
1x
1x
2x
1x
| const ofxSerializer = require('ofx');
// Dependencies
const moment = require('./moment');
// Functions
const csvAmount = n => (n > 0 ? `+${n.toFixed(2)}` : `${n.toFixed(2)}`);
const csvTransaction = t =>
`${moment(t.timestamp).format(moment.formats.default)}` +
`,"${csvAmount(t.amount)}"` +
`,"${t.description}"` +
`,"${csvAmount(t.balance)}"\r\n`;
const qifTransaction = (transaction, type = 'default') => {
let format;
switch (type) {
case 'aus':
format = moment.formats.aus;
break;
case 'us':
format = moment.formats.us;
break;
default:
format = moment.formats.default;
break;
}
const lline = type === 'default' ? `L${transaction.amount >= 0 ? 'DEP' : 'DEBIT'}\r\n` : '';
return (
`D${moment(transaction.timestamp).format(format)}\r\n` +
`T${transaction.amount.toFixed(2)}\r\n` +
`P${transaction.description}\r\n${lline}^\r\n`
);
};
// Transactions Serialization
const csv = transactions => transactions.map(csvTransaction).join('');
const qif = (transactions, type = 'default') =>
`!Type:Bank\r\n${transactions.map(transaction => qifTransaction(transaction, type)).join('')}`;
const ofx = (transactions, account, from, to) => {
const current = moment().format('YYYYMMDDHHmmss');
const header = {
OFXHEADER: '100',
DATA: 'OFXSGML',
VERSION: '102',
SECURITY: 'NONE',
ENCODING: 'USASCII',
CHARSET: '1252',
COMPRESSION: 'NONE',
OLDFILEUID: 'NONE',
NEWFILEUID: 'NONE',
};
const body = {
SIGNONMSGSRSV1: {
SONRS: {
STATUS: {
CODE: 0,
SEVERITY: 'INFO',
},
DTSERVER: current,
LANGUAGE: 'ENG',
},
},
};
// BANKTRANLIST
const translist = {
DTSTART: moment(from, moment.formats.default).format('YYYYMMDD000000'),
DTEND: moment(to, moment.formats.default).format('YYYYMMDD000000'),
STMTTRN: transactions.map(t => ({
TRNTYPE: t.amount > 0 ? 'CREDIT' : 'DEBIT',
DTPOSTED: moment(t.timestamp).format('YYYYMMDD'),
DTUSER: moment(t.timestamp).format('YYYYMMDD'),
TRNAMT: t.amount.toFixed(2),
// use timestamp as FITID if necessary to fix the OFX missing FITID issue.
FITID: t.receiptnumber || t.timestamp,
MEMO: t.description.replace(';', ''),
})),
};
if (account.type === 'DDA') {
body.BANKMSGSRSV1 = {
STMTTRNRS: {
TRNUID: 1,
STATUS: {
CODE: 0,
SEVERITY: 'INFO',
},
STMTRS: {
CURDEF: 'AUD',
BANKACCTFROM: {
BANKID: account.bsb,
ACCTID: account.account,
ACCTTYPE: 'SAVINGS',
},
BANKTRANLIST: translist,
LEDGERBAL: {
BALAMT: account.balance,
DTASOF: current,
},
AVAILBAL: {
BALAMT: account.available,
DTASOF: current,
},
},
},
};
} else {
body.CREDITCARDMSGSRSV1 = {
CCSTMTTRNRS: {
TRNUID: 1,
STATUS: {
CODE: 0,
SEVERITY: 'INFO',
},
CCSTMTRS: {
CURDEF: 'AUD',
CCACCTFROM: {
ACCTID: account.number,
},
BANKTRANLIST: translist,
LEDGERBAL: {
BALAMT: account.balance,
DTASOF: current,
},
AVAILBAL: {
BALAMT: account.available,
DTASOF: current,
},
},
},
};
}
return ofxSerializer.serialize(header, body);
};
// Exports
module.exports = {
csv,
qif,
ofx,
};
|