forked from brasil-js/node-danfe-nfephp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (49 loc) · 1.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
var spawn = require('child_process').spawn;
function executarPhp(arquivo, xml, parametros, callback) {
if(typeof parametros === 'function') {
callback = parametros;
parametros = [];
}
var php = spawn('php', [
arquivo + '.php'
].concat(parametros), {
cwd: __dirname
});
xml = xml.replace(/\n/g, '');
php.stdin.setEncoding = 'utf-8';
php.stdin.write(xml + '\n');
var pdf = [];
php.stdout.on('data', function(bytes) {
pdf.push(bytes);
});
php.on('close', function(code) {
if(code !== 0) {
return callback(new Error('Erro ao executar nfephp: ' + code));
}
callback(null, Buffer.concat(pdf));
});
}
function gerarDacce(xml, parametros, callback) {
executarPhp('dacce', xml, [
parametros.creditos,
parametros.razaoSocial || '',
parametros.logradouro || '',
parametros.numero || '',
parametros.complemento || '',
parametros.bairro || '',
parametros.cep || '',
parametros.municipio || '',
parametros.uf || '',
parametros.telefone || '',
parametros.email || '',
parametros.registroNacional && parametros.registroNacional.length === 11 ? parametros.registroNacional : '',
parametros.registroNacional && parametros.registroNacional.length === 14 ? parametros.registroNacional : ''
], callback);
}
module.exports.gerarDacce = gerarDacce;
function gerarDanfe(xml, parametros, callback) {
executarPhp('danfe', xml, [
parametros.creditos
], callback);
}
module.exports.gerarDanfe = gerarDanfe;