This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
212 lines (187 loc) · 7.61 KB
/
index.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var axios = require('axios');
var numeral = require('numeral');
var moment = require('moment-timezone');
var GoogleSpreadsheet = require('google-spreadsheet');
var async = require('async');
var https = require('https');
var csvParse = require('csv-parse/lib/sync');
var config = {
spreadsheetId: process.env.GOOGLE_SPREADSHEET_ID,
apiToken: process.env.BILLY_API_TOKEN,
credentials: process.env.GOOGLE_CREDENTIALS,
financialYear: process.env.FINANCIAL_YEAR || 2017,
excludeAccount: function (accountNo) {
return accountNo >= 62000 && accountNo < 63000;
}
};
moment.locale('da');
function exportFromBillyToGoogleSpreadsheet() {
var context = {};
console.log('Export/Import function started');
async.series([
function loadOrganizationDetails(step) {
axios.get('https://api.billysbilling.com/v2/organization', {
headers: {
'X-Access-Token': config.apiToken
},
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}).then(function (result) {
console.log('Organization loaded');
context.organizationId = result.data.organization.id;
step();
});
},
function loadBalanceSheet(step) {
axios.get('https://api.billysbilling.com/v2/organizations/'+context.organizationId+'/balanceList.csv', {
params: {
accessToken: config.apiToken,
acceptLanguage: 'da_DK',
period: 'fiscalyear:'+context.organizationId+','+config.financialYear
},
responseType: 'arraybuffer',
transformResponse: [function (data) {
data = csvParse(data.toString('latin1'), {
columns: ['accountNo', 'accountText', 'amount'],
"skip_empty_lines": true,
from: 2
});
return data;
}],
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}).then(function (result) {
console.log('Balance Sheet loaded');
context.balanceSheet = result.data;
context.balanceSheetLookup = {};
context.balanceSheet.forEach(function(account) {
context.balanceSheetLookup[account.accountNo] = account;
});
step();
});
},
function loadOpeningBalance(step) {
axios.get('https://api.billysbilling.com/v2/organizations/'+context.organizationId+'/balanceList.csv', {
params: {
accessToken: config.apiToken,
acceptLanguage: 'da_DK',
period: 'dates:'+config.financialYear+'-01-01...'+config.financialYear+'-01-01'
},
responseType: 'arraybuffer',
transformResponse: [function (data) {
data = csvParse(data.toString('latin1'), {
columns: ['accountNo', 'accountText', 'amount'],
"skip_empty_lines": true,
from: 2
});
return data;
}],
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}).then(function (result) {
console.log('Opening Balance loaded');
result.data.forEach(function(account) {
if (context.balanceSheetLookup[account.accountNo]) {
context.balanceSheetLookup[account.accountNo].openingAmount = account.amount;
}
});
// Filter excluded accounts
context.balanceSheet = context.balanceSheet.filter(function(account) {
return config.excludeAccount(account.accountNo) === false;
});
step();
});
},
function loadLatestPostingDate(step) {
axios.get('https://api.billysbilling.com/v2/transactions', {
params: {
organizationId: context.organizationId,
sortProperty: 'entryDate',
sortDirection: 'DESC',
pageSize: 1,
entryDatePeriod: 'fiscalyear:'+context.organizationId+','+config.financialYear
},
headers: {
'X-Access-Token': config.apiToken
},
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}).then(function (result) {
console.log('Latest Postings loaded');
if (result.data.transactions && result.data.transactions.length > 0) {
context.lastPostingDate = moment(result.data.transactions[0].entryDate);
} else {
context.lastPostingDate = null;
}
step();
});
},
function loadDocument(step) {
console.log('Loading document');
context.doc = new GoogleSpreadsheet(config.spreadsheetId);
context.doc.useServiceAccountAuth(JSON.parse(config.credentials), step);
},
function getInfoAndWorksheets(step) {
context.doc.getInfo(function (err, info) {
if (err) {
step(err);
} else {
console.log('Sheet loaded');
context.sheet = info.worksheets[1];
step();
}
});
},
function updateData(step) {
context.sheet.getCells({
'min-row': 2,
'max-row': 150,
'min-col': 1,
'max-col': 4,
'return-empty': true
}, function(err, cells) {
console.log('Cells loaded');
var cellIndex = 0;
context.balanceSheet.forEach(function(account) {
var accountNoCell = cells[cellIndex];
var accountTextCell = cells[cellIndex+1];
var amountCell = cells[cellIndex+2];
var primoCell = cells[cellIndex+3];
accountNoCell.value = account.accountNo;
accountTextCell.value = account.accountText;
amountCell.value = numeral(account.amount).format('0');
primoCell.value = numeral(account.openingAmount).format('0');
cellIndex += 4;
});
context.sheet.bulkUpdateCells(cells, step);
});
},
function updateMeta(step) {
context.sheet.getCells({
'min-row': 4,
'max-row': 5,
'min-col': 6,
'max-col': 6,
'return-empty': true
}, function(err, cells) {
console.log('Last update cells loaded');
var now = moment().tz('Europe/Copenhagen');
cells[0].value = now.format('D. MMMM YYYY HH:mm');
if (context.lastPostingDate) {
cells[1].value = context.lastPostingDate.format('D. MMMM YYYY')
}
context.sheet.bulkUpdateCells(cells, step);
});
}
], function (err) {
if (err) {
console.error(err);
}
console.log('Done');
});
}
exportFromBillyToGoogleSpreadsheet();