-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
209 lines (181 loc) · 6.52 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
const exec = require('child_process').exec,
fs = require('fs'),
EOL = '\r\n';
function setJSONParams(params) {
return new Promise(function(resolve, reject) {
fs.writeFile('' + __dirname + '\\command.json', JSON.stringify(params), function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
function callToDriver(params) {
return setJSONParams(params).then(function() {
return new Promise(function(resolve, reject) {
exec('"' + __dirname + '\\driver.exe"', function (error, stdout, stderr) {
if (error) {
reject(error);
return;
}
resolve(stdout);
});
});
});
}
function HasarFiscalPrinter(model) {
if (HasarFiscalPrinter.MODELS.indexOf(model) === -1) {
throw new Error('Invalid Hasar fiscal printer model: ' + model);
}
this.model = model;
}
HasarFiscalPrinter.prototype.reporteX = function reporteX() {
if (!HasarFiscalPrinter.canUseIn(this.model, 'REPORTE_X')) {
throw new Error('This model can not use method reportX.');
}
return callToDriver({
model: this.model,
action: "REPORTE_X"
}).then(function(result) {
var rta = {};
result = result.split(EOL);
rta.numeroReporte = result.shift();
rta.cantidadDFCancelados = result.shift();
rta.cantidadDNFHEmitidos = result.shift();
rta.cantidadDNFEmitidos = result.shift();
rta.cantidadDFEmitidos = result.shift();
rta.ultimoDocFiscalBC = result.shift();
rta.ultimoDocFiscalA = result.shift();
rta.montoVentasDocFiscal = result.shift();
rta.montoIVADocFiscal = result.shift();
rta.montoImpInternosDocFiscal = result.shift();
rta.montoPercepcionesDocFiscal = result.shift();
rta.montoIVANoInscriptoDocFiscal = result.shift();
rta.ultimaNotaCreditoBC = result.shift();
rta.ultimaNotaCreditoA = result.shift();
rta.montoVentasNotaCredito = result.shift();
rta.montoIVANotaCredito = result.shift();
rta.montoImpInternosNotaCredito = result.shift();
rta.montoPercepcionesNotaCredito = result.shift();
rta.montoIVANoInscriptoNotaCredito = result.shift();
rta.ultimoRemito = result.shift();
rta.cantidadNCCanceladas = result.shift();
rta.cantidadDFBCEmitidos = result.shift();
rta.cantidadDFAEmitidos = result.shift();
rta.cantidadNCBCEmitidas = result.shift();
rta.cantidadNCAEmitidas = result.shift();
return rta;
})
}
HasarFiscalPrinter.prototype.reporteZ = function reporteZ(from, to) {
if (!HasarFiscalPrinter.canUseIn(this.model, 'REPORTE_Z')) {
throw new Error('This model can not use method reportZ.');
}
if (from instanceof Date) {
from = from.getFullYear().toString().substr(2,2) +
('0' + (from.getMonth() + 1)).slice(-2) +
('0' + from.getDate()).slice(-2);
}
if (from instanceof Date) {
to = to.getFullYear().toString().substr(2,2) +
('0' + (to.getMonth() + 1)).slice(-2) +
('0' + to.getDate()).slice(-2);
}
return callToDriver({
model: this.model,
action: "REPORTE_Z",
from: from,
to: to
}).then(function(result) {
return result;
});
}
HasarFiscalPrinter.prototype.ticketFiscal = function ticketFiscal(type, ticket) {
if (!HasarFiscalPrinter.canUseIn(this.model, type)) {
throw new Error('This model can not print this type of ticket.');
}
ticket = {
model : this.model,
action : 'TICKET',
type : type,
client : {
name : ticket.client.name || 'Consumidor final',
id : ticket.client.id || '0000000000',
idType : ticket.client.idType || 'CUIT',
address: ticket.client.address || '------',
regimen: ticket.client.regimen || 'CONSUMIDOR_FINAL'
},
text : ticket.text || '',
items: ticket.items.map(function formatItem(item, idx) {
item = {
title : item.title || 'Item ' + idx,
price : item.price || 1,
quantity: item.quantity || 1,
iva : item.iva || 21,
discount: item.discount || 0
}
if (item.discount === 0) {
delete item.discount;
}
return item;
}),
discount: ticket.discount || 0
}
if (ticket.discount === 0) {
delete ticket.discount;
}
return callToDriver(ticket).then(function(result) {
return result.split(EOL).shift();
});
}
HasarFiscalPrinter.MODELS = ['615', '715', '320'];
HasarFiscalPrinter.SUPPORTED_TOOLS = {
REPORTE_X: {
description: 'Generar reporte X',
models: ['615', '715', '320']
},
REPORTE_Z: {
description: 'Generar reporte Z por rango de fechas',
models: ['615', '715', '320']
},
TICKET_FACTURA_A: {
description: 'Generar ticket factura A',
models: ['615', '715', '320']
},
TICKET_FACTURA_B: {
description: 'Generar ticket factura B',
models: ['615', '715', '320']
}
};
HasarFiscalPrinter.canUseIn = function canUseIn(model, tool) {
return HasarFiscalPrinter.SUPPORTED_TOOLS[tool].models.indexOf(model) !== -1;
}
module.exports = HasarFiscalPrinter;
// Sugar
HasarFiscalPrinter.whatCanIDo = function whatCanIDo(model) {
const models = model ? (Array.isArray(model) ? model : [model]) : HasarFiscalPrinter.MODELS,
lengthModels = models.length,
tools = Object.keys(HasarFiscalPrinter.SUPPORTED_TOOLS),
lengthTools = tools.length;
console.log('┌─────────────────────────────────────────────────────────────┐');
console.log('│■ WITH HASAR FISCAL PRINTER YOU... │');
for(var nro=0; nro<lengthModels; nro++) {
console.log('├─────────────────────────────────────────────────────────────┤');
console.log('│■■ USING MODEL "' + models[nro] + '"...');
console.log('│■■■■ CAN DO:');
for(var tNro=0; tNro<lengthTools; tNro++) {
if (HasarFiscalPrinter.canUseIn(models[nro], tools[tNro])) {
console.log('│ » ' + HasarFiscalPrinter.SUPPORTED_TOOLS[tools[tNro]].description);
}
}
console.log('│■■■■ CAN NOT DO:');
for(var tNro=0; tNro<lengthTools; tNro++) {
if (!HasarFiscalPrinter.canUseIn(models[nro], tools[tNro])) {
console.log(' * ' + HasarFiscalPrinter.SUPPORTED_TOOLS[tools[tNro]].description);
}
}
}
console.log('└─────────────────────────────────────────────────────────────┘');
}