Mejora de nombres

This commit is contained in:
martin
2025-10-13 14:47:00 -03:00
parent d143679251
commit 3666df9f8b

View File

@@ -154,8 +154,8 @@ function printDottedLine() {
function printProducts(products) { function printProducts(products) {
let ticket = ""; let ticket = "";
const lineWidth = 46; // Ajusta según tu impresora (32, 42, 48 caracteres típicos) const lineWidth = 46;
const priceWidth = 10; // Espacio reservado para el precio (ej: "$999.99") const priceWidth = 10;
for (const producto of products) { for (const producto of products) {
const nombre = removeAccents(producto?.nombre); const nombre = removeAccents(producto?.nombre);
@@ -163,21 +163,45 @@ function printProducts(products) {
const precio = `$${producto?.precio}`; const precio = `$${producto?.precio}`;
// Construir la parte izquierda: "2 x Producto" // Construir la parte izquierda: "2 x Producto"
let leftPart = `${cantidad} x ${nombre}`; const prefix = `${cantidad} x `;
const leftPart = `${prefix}${nombre}`;
// Calcular cuántos espacios necesitamos // Calcular espacio disponible para el texto completo (sin considerar el precio aún)
const availableSpace = lineWidth - precio.length; const availableSpace = lineWidth;
// Si el nombre es muy largo, cortarlo if (leftPart.length <= lineWidth - precio.length - 1) {
if (leftPart.length > availableSpace) { // Si cabe todo en una línea (con el precio)
leftPart = leftPart.substring(0, availableSpace - 3) + "...";
}
// Rellenar con espacios hasta que el precio quede alineado a la derecha
const spaces = lineWidth - leftPart.length - precio.length; const spaces = lineWidth - leftPart.length - precio.length;
const spacePadding = " ".repeat(Math.max(0, spaces)); const spacePadding = " ".repeat(Math.max(0, spaces));
ticket += left() + leftPart + spacePadding + precio + line(); ticket += left() + leftPart + spacePadding + precio + line();
} else {
// Dividir el texto en líneas
const lines = [];
let remainingText = leftPart;
const indentation = " ".repeat(prefix.length);
// Primera línea sin indentación
lines.push(remainingText.substring(0, lineWidth));
remainingText = remainingText.substring(lineWidth);
// Líneas intermedias con indentación
while (remainingText.length > lineWidth - indentation.length - precio.length - 1) {
const lineText = indentation + remainingText.substring(0, lineWidth - indentation.length);
lines.push(lineText);
remainingText = remainingText.substring(lineWidth - indentation.length);
}
// Última línea con el precio
const lastLineText = indentation + remainingText;
const spaces = lineWidth - lastLineText.length - precio.length;
const spacePadding = " ".repeat(Math.max(0, spaces));
lines.push(lastLineText + spacePadding + precio);
// Agregar todas las líneas al ticket
for (const _line of lines) {
ticket += left() + _line + line();
}
}
if (producto?.observaciones?.length > 0) { if (producto?.observaciones?.length > 0) {
const obs = removeAccents(producto.observaciones); const obs = removeAccents(producto.observaciones);
@@ -244,7 +268,8 @@ async function ticketDPSTock(
// Total // Total
ticket += line(2); ticket += line(2);
ticket += left() + `Total: $${total}` + line(3); //ticket += left() + `Total: $${total}` + line(3);
ticket += printBetween("Total: ", `$${total}`) + line(3);
// Corte // Corte
ticket += cut(); ticket += cut();