-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
126 lines (119 loc) · 3.45 KB
/
renderer.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
const electron = require('electron');
const ejs = require('ejs');
const pdf = require('html-pdf');
const config = require('electron-json-config');
const smalltalk = require('smalltalk');
const axios = require('axios');
window.apiBaseUrl = 'https://api.cuentica.com/';
window.apiToken = config.get('cuenticaAPIToken');
window.currentTemplate = config.get('template', 'default');
config.set('template', window.currentTemplate);
electron.ipcRenderer.send('updateMenu', window.currentTemplate);
window.ajaxConfig = {
headers: {'X-AUTH-TOKEN': window.apiToken}
};
window.askForAPIToken = function() {
smalltalk.prompt('API Token', 'Insert here your Cuentica API token', window.apiToken).then(
function(value) {
config.set('cuenticaAPIToken', value);
window.apiToken = config.get('cuenticaAPIToken');
window.ajaxConfig = {
headers: {'X-AUTH-TOKEN': window.apiToken}
};
if (!value) {
config.set('cuenticaAPIToken', '');
window.askForAPIToken();
} else {
axios.get(
window.apiBaseUrl + 'company',
window.ajaxConfig
).then(
function(res) {
electron.remote.getCurrentWindow().reload();
},
function() {
electron.remote.dialog.showMessageBox(
{
type: 'error',
message: 'Invalid API token',
buttons: ['OK']
}
);
config.set('cuenticaAPIToken', '');
window.askForAPIToken();
}
);
}
},
function() {
if (!config.get('cuenticaAPIToken')) {
window.askForAPIToken();
}
}
);
};
electron.ipcRenderer.on('changeAPIKey', function() {
window.askForAPIToken();
});
electron.ipcRenderer.on('changeTemplate', function(e, template) {
window.currentTemplate = template;
config.set('template', template);
electron.ipcRenderer.send('updateMenu', template);
});
if (!window.apiToken) {
window.askForAPIToken();
}
window.generatePDF = function(invoice) {
const templateData = {
invoice: invoice,
path: electron.remote.app.getAppPath(),
templatePath: electron.remote.app.getAppPath() + '/templates/' + window.currentTemplate + '/'
};
const pdfConfig = {
format: 'A4',
orientation: 'portrait',
type: 'pdf'
};
const dialog = electron.remote.dialog;
dialog.showSaveDialog(
{
title: 'Save invoice as...',
defaultPath: invoice.invoice_serie + '-' + invoice.invoice_number + '.pdf',
filters: [
{
name: 'Portable Document Format (*.pdf)',
extensions: ['pdf']
},
{
name: 'All files',
extensions: ['*']
}
]
},
function(filepath) {
if (filepath) {
ejs.renderFile('./templates/' + window.currentTemplate + '/template.ejs', templateData, {}, function(err, html) {
if (err) {
dialog.showMessageBox(
{
type: 'error',
message: 'Error creating invoice: ' + err,
buttons: ['OK']
}
);
} else {
pdf.create(html, pdfConfig).toFile(filepath, function(err, res){
dialog.showMessageBox(
{
type: 'info',
message: 'Invoice created successfully at ' + res.filename,
buttons: ['OK']
}
);
});
}
});
}
}
);
};