-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImprimirService.java
More file actions
215 lines (175 loc) · 8.58 KB
/
Copy pathImprimirService.java
File metadata and controls
215 lines (175 loc) · 8.58 KB
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
package com.mateo.freetpv.service;
import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.escpos.EscPosConst;
import com.github.anastaciocintra.escpos.Style;
import com.github.anastaciocintra.escpos.image.BitonalThreshold;
import com.github.anastaciocintra.escpos.image.CoffeeImageImpl;
import com.github.anastaciocintra.escpos.image.EscPosImage;
import com.github.anastaciocintra.escpos.image.GraphicsImageWrapper;
import com.github.anastaciocintra.output.PrinterOutputStream;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.mateo.freetpv.model.DatosTicket;
import com.mateo.freetpv.model.LineaTicket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.print.PrintService;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.mateo.freetpv.util.ConversionUtil.centimosEuros;
public class ImprimirService {
private static int WIDTH = 32;
private static final Logger log = LoggerFactory.getLogger(ImprimirService.class);
public static void imprimirTicket(PrintService printService, DatosTicket ticket, int ancho, String codepage, boolean cortar) throws IOException {
WIDTH = ancho == 58 ? 32 : 48;
PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
EscPos escpos = new EscPos(printerOutputStream);
Style center = new Style()
.setJustification(EscPosConst.Justification.Center);
Style centerBold = new Style()
.setBold(true)
.setJustification(EscPosConst.Justification.Center);
Style left = new Style()
.setJustification(EscPosConst.Justification.Left_Default);
Style totalStyle = new Style()
.setBold(true)
.setJustification(EscPosConst.Justification.Center);
LocalDateTime ahora = LocalDateTime.now();
String fecha = ahora.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
String hora = ahora.format(DateTimeFormatter.ofPattern("HH:mm"));
escpos.initializePrinter();
EscPos.CharacterCodeTable charset = switch (codepage) {
case "CP850" -> EscPos.CharacterCodeTable.CP850_Multilingual;
default -> EscPos.CharacterCodeTable.CP858_Euro;
};
escpos.setCharacterCodeTable(charset);
escpos.feed(1);
escpos.writeLF(centerBold, ticket.nombreEmpresa());
if (ticket.mostrarCif()) escpos.writeLF(center, "CIF/NIF: " + ticket.cif());
escpos.writeLF(center, ticket.direccion());
escpos.writeLF(center, ticket.codigoPostal() + " " + ticket.ciudad());
if (ticket.mostrarTelefono()) escpos.writeLF(center, "Tlf: " + ticket.telefono());
escpos.writeLF(left, line());
escpos.writeLF(centerBold, ticket.tituloTicket());
escpos.writeLF(left, line());
escpos.writeLF(left, "Ticket Nº: N/A");
escpos.writeLF(left, twoCols("Fecha: " + fecha, "Hora: " + hora));
escpos.writeLF(left, twoCols("Caja: 1", "Empleado: " + ticket.nombreCamarero()));
escpos.writeLF(left, line());
escpos.writeLF(left, itemHeader());
escpos.writeLF(left, line());
for (LineaTicket linea : ticket.lineas()) {
escpos.writeLF(left, itemRow(linea.getNombreTicket(),
String.valueOf(linea.getCantidad()),
centimosEuros(linea.getPrecioUnitario()).orElse("0,00") + "€",
centimosEuros(linea.getSubtotal()).orElse("0,00") + "€"));
}
escpos.writeLF(left, line());
int total = ticket.lineas().stream().mapToInt(LineaTicket::getSubtotal).sum();
int iva = ticket.lineas().stream().mapToInt(l -> {
double factor = l.getIva() / 100.0;
return (int) Math.round(l.getSubtotal() * factor / (1 + factor));
}).sum();
int subtotal = total - iva;
escpos.writeLF(left, amountRow("Subtotal:", centimosEuros(subtotal).orElse("0,00") + " €"));
if (ticket.mostrarIva()) {
Map<Integer, List<LineaTicket>> lineasPorIva = ticket.lineas().stream()
.collect(Collectors.groupingBy(LineaTicket::getIva));
lineasPorIva.entrySet().stream()
.sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
.forEach(entry -> {
int tipo = entry.getKey();
if (tipo == 0) return;
int cuota = entry.getValue().stream().mapToInt(l -> {
double factor = l.getIva() / 100.0;
return (int) Math.round(l.getSubtotal() * factor / (1 + factor));
}).sum();
try {
escpos.writeLF(left, amountRow(
"IVA " + tipo + "%:",
centimosEuros(cuota).orElse("0,00") + " €"
));
} catch (IOException e) {
log.error("Error al escribir IVA", e);
}
});
}
escpos.writeLF(left, line());
escpos.writeLF(totalStyle, "TOTAL: " + centimosEuros(total).orElse("0,00") + " €");
escpos.writeLF(left, line());
escpos.writeLF(left, amountRow("Pago:", ticket.metodoPago()));
if (ticket.entregado() > 0) {
int cambio = ticket.entregado() - total;
escpos.writeLF(left, amountRow("Entregado:", centimosEuros(ticket.entregado()).orElse("0,00") + " €"));
escpos.writeLF(left, amountRow("Cambio:", centimosEuros(cambio).orElse("0,00") + " €"));
}
escpos.writeLF(left, line());
escpos.feed(1);
escpos.writeLF(center, ticket.mensajeFinal());
escpos.feed(1);
if (ticket.mostrarWeb()) escpos.writeLF(center, ticket.web());
if (ticket.mostrarQr() && !ticket.qr().isBlank()) {
try {
BitMatrix bitMatrix = new QRCodeWriter().encode(ticket.qr(), BarcodeFormat.QR_CODE, 200, 200);
BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper();
imageWrapper.setJustification(EscPosConst.Justification.Center);
EscPosImage escposImage = new EscPosImage(new CoffeeImageImpl(qrImage), new BitonalThreshold());
escpos.write(imageWrapper, escposImage);
} catch (WriterException e) {
log.error("Error al dibujar el código QR", e);
}
}
escpos.feed(2);
if (cortar) escpos.cut(EscPos.CutMode.FULL);
escpos.close();
}
public static void abrirCajon(PrintService printService) throws IOException {
PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
EscPos escpos = new EscPos(printerOutputStream);
escpos.pulsePin(EscPos.PinConnector.Pin_2, 120, 240);
escpos.pulsePin(EscPos.PinConnector.Pin_5, 120, 240);
escpos.close();
}
private static String line() {
return "-".repeat(WIDTH);
}
private static String twoCols(String left, String right) {
int spaces = WIDTH - left.length() - right.length();
if (spaces < 1) spaces = 1;
return left + " ".repeat(spaces) + right;
}
private static String amountRow(String label, String amount) {
String text = label + " " + amount;
if (text.length() > WIDTH) {
return text.substring(0, WIDTH);
}
int spaces = WIDTH - label.length() - amount.length();
return label + " ".repeat(Math.max(1, spaces)) + amount;
}
private static String itemHeader() {
return String.format("%-13s%3s%7s%9s",
"Producto", "Ud", "P.U", "Total");
}
private static String itemRow(String name, String qty, String unitPrice, String total) {
return String.format("%-13s%3s%7s%9s",
cut(name, 13),
qty,
unitPrice,
total
);
}
private static String cut(String text, int max) {
if (text == null) return "";
return text.length() <= max ? text : text.substring(0, max);
}
}