-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClaseFiscal.cpp
390 lines (317 loc) · 10.2 KB
/
ClaseFiscal.cpp
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsmn.h"
#include "fiscalStructures.h"
#include "fiscalengine.h"
char* ReadFile(char *filename) {
char *buffer = NULL;
int filesize, readsize;
FILE *handler = fopen(filename, "r");
if (handler) {
fseek(handler, 0, SEEK_END);
filesize = ftell(handler);
rewind(handler);
buffer = (char*) malloc(sizeof(char) * (filesize + 1));
readsize = fread(buffer, sizeof(char), filesize, handler);
buffer[filesize] = '\0';
if (filesize != readsize) {
free(buffer);
buffer = NULL;
}
fclose(handler);
}
return buffer;
}
/*
* A small example of jsmn parsing when JSON structure is known and number of
* tokens is predictable.
*/
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}
int main (int argc, char* argv[]) {
char *p = strrchr(argv[0], '\\');
int pNro; // Param number
int read;
jsmn_parser parser;
FisCommand command;
jsmntok_t toks[1024]; /* We expect no more than 1024 tokens */
char *commands;
if (p) {
p[0] = 0;
}
commands = ReadFile(strcat(argv[0], "\\command.json"));
jsmn_init(&parser);
read = jsmn_parse(&parser, commands, strlen(commands), toks, sizeof(toks) / sizeof(toks[0]));
/* Assume the top-level element is an object */
if (read < 1 || toks[0].type != JSMN_OBJECT) {
printf("Failed to parse JSON: %d\n", read);
return 1;
}
// Init command
strncpy(command.ticket.client.id, "NONE", 5);
for (pNro = 1; pNro < read; pNro++) {
/***********************************/
/** Parse model of fiscal printer **/
/***********************************/
if (jsoneq(commands, &toks[pNro], "model") == 0) {
pNro++;
if (jsoneq(commands, &toks[pNro], "615") == 0) {
command.printer.model = FIS_MODEL_H615;
continue;
}
if (jsoneq(commands, &toks[pNro], "715") == 0) {
command.printer.model = FIS_MODEL_H715;
continue;
}
if (jsoneq(commands, &toks[pNro], "320") == 0) {
command.printer.model = FIS_MODEL_H320;
continue;
}
/*/
* Agregar una nueva condicion if, en este punto, lo que estamos haciendo es mapear
* el valor que se espera en el archivo command.json al codigo interno unico del modelo.
/*/
if (jsoneq(commands, &toks[pNro], "441") == 0) {
command.printer.model = FIS_MODEL_H441;
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
/*****************************/
/** Parse action of command **/
/*****************************/
if (jsoneq(commands, &toks[pNro], "action") == 0) {
pNro++;
if (jsoneq(commands, &toks[pNro], "TICKET") == 0) {
command.action = FIS_TICKET;
// Populate defaults
command.ticket.discount = 0;
continue;
}
if (jsoneq(commands, &toks[pNro], "REPORTE_X") == 0) {
command.action = FIS_REPORTE_X;
continue;
}
if (jsoneq(commands, &toks[pNro], "REPORTE_Z") == 0) {
command.action = FIS_REPORTE_Z;
continue;
}
if (jsoneq(commands, &toks[pNro], "RESUMEN") == 0) {
command.action = FIS_RESUMEN;
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
/***************************/
/** Parse type of command **/
/***************************/
if (jsoneq(commands, &toks[pNro], "type") == 0) {
pNro++;
if (command.action == FIS_TICKET) {
if (jsoneq(commands, &toks[pNro], "TICKET_FACTURA_B") == 0) {
command.ticket.type = FACTURA_B;
continue;
}
if (jsoneq(commands, &toks[pNro], "TICKET_FACTURA_A") == 0) {
command.ticket.type = FACTURA_A;
continue;
}
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
/*****************************/
/** Parse range of Z report **/
/*****************************/
if (jsoneq(commands, &toks[pNro], "from") == 0) {
pNro++;
if (command.action == FIS_REPORTE_Z) {
int len = toks[pNro].end - toks[pNro].start;
strncpy(command.report.from, commands + toks[pNro].start, 6);
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
if (jsoneq(commands, &toks[pNro], "to") == 0) {
pNro++;
if (command.action == FIS_REPORTE_Z) {
int len = toks[pNro].end - toks[pNro].start;
strncpy(command.report.to, commands + toks[pNro].start, 6);
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
if (jsoneq(commands, &toks[pNro], "text") == 0) {
pNro++;
if (command.action == FIS_TICKET) {
int count = toks[pNro].end - toks[pNro].start;
strncpy(command.ticket.text, commands + toks[pNro].start, count);
command.ticket.text[count] = '\0';
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
if (jsoneq(commands, &toks[pNro], "discount") == 0) {
char value[6];
pNro++;
strncpy(value, commands + toks[pNro].start, toks[pNro].end - toks[pNro].start);
command.ticket.discount = atof(value);
continue;
}
/*****************************/
/** Parse client of tickets **/
/*****************************/
if (jsoneq(commands, &toks[pNro], "client") == 0) {
pNro++;
if (command.action == FIS_TICKET && toks[pNro].type == JSMN_OBJECT) {
int cpNro;
int cpCount = toks[pNro].size;
for (cpNro = 0; cpNro < cpCount; cpNro++) {
pNro++;
if (jsoneq(commands, &toks[pNro], "name") == 0) {
int count;
pNro++;
count = toks[pNro].end - toks[pNro].start;
strncpy(command.ticket.client.name, commands + toks[pNro].start, count);
command.ticket.client.name[count] = '\0';
continue;
}
if (jsoneq(commands, &toks[pNro], "id") == 0) {
pNro++;
strncpy(command.ticket.client.id, commands + toks[pNro].start, toks[pNro].end - toks[pNro].start);
continue;
}
if (jsoneq(commands, &toks[pNro], "idType") == 0) {
pNro++;
if (jsoneq(commands, &toks[pNro], "CUIT") == 0) {
command.ticket.client.idType = CUIT;
continue;
}
if (jsoneq(commands, &toks[pNro], "CUIL") == 0) {
command.ticket.client.idType = CUIL;
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
if (jsoneq(commands, &toks[pNro], "address") == 0) {
int count;
pNro++;
count = toks[pNro].end - toks[pNro].start;
strncpy(command.ticket.client.address, commands + toks[pNro].start, count);
command.ticket.client.address[count] = '\0';
continue;
}
if (jsoneq(commands, &toks[pNro], "regimen") == 0) {
pNro++;
if (jsoneq(commands, &toks[pNro], "RESPONSABLE_INSCRIPTO") == 0) {
command.ticket.client.regimen = RESPONSABLE_INSCRIPTO;
continue;
}
if (jsoneq(commands, &toks[pNro], "MONOTRIBUTISTA") == 0) {
command.ticket.client.regimen = MONOTRIBUTISTA;
continue;
}
if (jsoneq(commands, &toks[pNro], "CONSUMIDOR_FINAL") == 0) {
command.ticket.client.regimen = CONSUMIDOR_FINAL;
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
/*****************************/
/** Parse items of tickets **/
/*****************************/
if (jsoneq(commands, &toks[pNro], "items") == 0) {
pNro++;
if (command.action == FIS_TICKET && toks[pNro].type == JSMN_ARRAY) {
int iNro; // Item number
int itemsCount = toks[pNro].size;
command.ticket.itemsCount = itemsCount;
command.ticket.items = (ItemTicket *) malloc(itemsCount * sizeof(ItemTicket));
for (iNro = 0; iNro < itemsCount; iNro++) {
int ipNro; // Item prop number
int itemPropsCount = toks[pNro + 1].size;
// Populate defaults:
command.ticket.items[iNro].price = 0;
command.ticket.items[iNro].quantity = 0;
command.ticket.items[iNro].iva = 0;
command.ticket.items[iNro].discount = 0;
pNro++;
/* Assume the item element is an object */
if (toks[pNro].type != JSMN_OBJECT) {
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
for (ipNro = 0; ipNro < itemPropsCount; ipNro++) {
pNro++;
if (jsoneq(commands, &toks[pNro], "title") == 0) {
int count;
pNro++;
count = toks[pNro].end - toks[pNro].start;
strncpy(command.ticket.items[iNro].title, commands + toks[pNro].start, count);
command.ticket.items[iNro].title[count] = '\0';
continue;
}
if (jsoneq(commands, &toks[pNro], "price") == 0) {
char price[10];
pNro++;
strncpy(price, commands + toks[pNro].start, toks[pNro].end - toks[pNro].start);
command.ticket.items[iNro].price = atof(price);
continue;
}
if (jsoneq(commands, &toks[pNro], "quantity") == 0) {
char quantity[6];
pNro++;
strncpy(quantity, commands + toks[pNro].start, toks[pNro].end - toks[pNro].start);
command.ticket.items[iNro].quantity = atof(quantity);
continue;
}
if (jsoneq(commands, &toks[pNro], "iva") == 0) {
char iva[6];
pNro++;
strncpy(iva, commands + toks[pNro].start, toks[pNro].end - toks[pNro].start);
command.ticket.items[iNro].iva = atof(iva);
continue;
}
if (jsoneq(commands, &toks[pNro], "discount") == 0) {
char value[6];
pNro++;
strncpy(value, commands + toks[pNro].start, toks[pNro].end - toks[pNro].start);
command.ticket.items[iNro].discount = atof(value);
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
}
continue;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
printf("Invalid token: %s\n", toks[pNro]);
return 1;
}
runFiscalCommand(command);
return 0;
}