-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
256 lines (211 loc) · 6.38 KB
/
app.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
var request = require('request');
var dateFormat = require('dateformat');
var fs = require('fs');
var debitoorKey = process.argv[2];
var projectKey = process.argv[3];
var logs = [];
var products = [];
var customers = [];
var logsFilename = "hours.json"
var debitoorUrl = "https://api.debitoor.com/api";
var getProductsUrl = debitoorUrl + "/products/v1";
var getCustomersUrl = debitoorUrl + "/customers/v1";
var createInvoiceUrl = debitoorUrl + "/sales/draftinvoices/v3"
var paymentTermsId = 3; // 30 days https://developers.debitoor.com/api-reference#paymentterms
var main = function () {
console.log(debitoorKey);
// VALIDATE INPUTS
if (!debitoorKey) {
console.error("Missing debitoor API key");
return;
}
if (debitoorKey.length != 174) {
console.error("Debitoor API key must be 174 chars. You gave " + debitoorKey.length + " chars.");
return;
}
getLogs();
getProducts();
}
var getProductsCallback = function () {
getCustomers();
}
var getCustomersCallback = function() {
createInvoices();
}
var getLogs = function () {
logs = JSON.parse(fs.readFileSync(logsFilename, 'utf8'));
}
var getProducts = function () {
var parameters = {
"token": debitoorKey
}
var url = getProductsUrl + parametersToQuery(parameters);
console.log(url);
request(url,
function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
//console.log('body:', body);
if (response && response.statusCode == 200) {
products = JSON.parse(body)
getProductsCallback();
}
});
}
var getCustomers = function () {
var parameters = {
"token": debitoorKey
}
var url = getCustomersUrl + parametersToQuery(parameters);
console.log(url);
request(url,
function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
//console.log('body:', body);
if (response && response.statusCode == 200) {
customers = JSON.parse(body)
getCustomersCallback();
}
});
}
var projectSku = function(log) {
var indexOf = log.project.indexOf(" - ");
return log.project.substr(0, indexOf);
}
var clientSku = function(log) {
var indexOf = log.client.indexOf(" - ");
return log.client.substr(0, indexOf);
}
var createInvoices = function () {
console.log("Creating invoices from " + logs.length + " logs");
var projectTitles = [];
logs.forEach(function(log){
var projectTitle = projectSku(log);
if(projectTitles.indexOf(projectTitle) === -1) {
projectTitles.push(projectTitle);
}
});
console.log("Creating invoices for " + projectTitles.length + " projects");
for (var i = 0; i < projectTitles.length; i++) {
var projectTitle = projectTitles[i];
var product = getProduct(projectTitle);
var customer = getCustomer(projectTitle);
console.log(projectTitle)
console.log(product.name)
console.log(customer.name)
console.log(projectKey);
if (product && customer && (projectKey == null || projectKey == projectTitle)) {
createInvoice(projectTitle )
}
}
}
var getProduct = function (projectTitle ) {
if (!projectTitle ) {
console.error("Project " + projectTitle + " has no client in Toggl.");
return;
}
for (var j = 0; j < products.length; j++) {
var product = products[j];
var debitoorSku = product.sku
if (debitoorSku == projectTitle) {
//console.log(project.title.project);
return product;
}
}
console.error("Could not find product for " + projectTitle );
}
var getCustomer = function (projectTitle) {
var log = {};
logs.forEach(function(localLog){
if (projectSku(localLog) === projectTitle) {
log = localLog;
return;
}
});
for (var j = 0; j < customers.length; j++) {
var customer = customers[j];
var debitoorSku = customer.notes
if (debitoorSku == clientSku(log)) {
//console.log(project.title.client);
return customer;
}
}
console.error("Could not find customer for " + project.title.client);
}
var createInvoice = function (projectTitle) {
console.log("Creating invoice for " + projectTitle);
var product = getProduct(projectTitle);
var customer = getCustomer(projectTitle);
if (product == null) {
console.error("Project " + projectTitle + " has no project in logs.");
return;
}
if (customer == null) {
console.error("Project " + projectTitle + " has no customer in logs.");
return;
}
var today = new Date;
var lastMonth = today.getMonth();
var month = today.getFullYear() + "-" + lastMonth
var todayDateString = dateFormat(today, "yyyy-mm-dd");
var invoiceNumber = product.sku + month.replace("-", "") + dateFormat(today, "mmdd"); //" " to force string
var parameters = {
"token": debitoorKey
}
var url = createInvoiceUrl + parametersToQuery(parameters);
console.log(url);
var languageCode = customer.countryCode == "GB" ? "en-GL" : "de-DE"; //No idea why "GL"
var notes = product.description ? product.description : "";
var monthOfService = customer.countryCode == "GB" ? "\nMonth of service: " : "\nLeistungsmonat: ";
notes += monthOfService + month;
var lines = [];
logs.forEach(function(localLog){
if (projectSku(localLog) === projectTitle) {
var line = {
quantity: Number((localLog.total_hours).toFixed(1)), // In hours
unitId: 1, // Hours
taxEnabled: product.taxEnabled,
taxRate: product.rate,
unitNetPrice: product.netUnitSalesPrice,
productName: localLog.task + (localLog.platform.length > 0 ? " " + localLog.platform : "") + (localLog.description.length > 0 ? ": " + localLog.description : "")
};
if (customer.countryCode != "DE") { //Non-EU required, in EU not allowed
line["productOrService"] = "service";
}
lines.push(line);
}
});
//console.log(lines);
var invoice =
{
number: invoiceNumber,
notes: notes,
date: todayDateString,
paymentTermsId: paymentTermsId,
customerId: customer.id,
lines: lines,
languageCode: languageCode
}
var requestBody = JSON.stringify(invoice);
//console.log(body);
request.post({ url: url, body: requestBody },
function (error, response, body) {
if (response.statusCode != 200) {
console.log("request body:", requestBody);
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.error('response body:', body);
}
});
}
var parametersToQuery = function (parameters) {
var query = "?";
for (var key in parameters) {
if (parameters.hasOwnProperty(key)) {
query = query + "&" + key + "=" + parameters[key];
}
}
return query;
}
main();