diff --git a/index.js b/index.js index 5e11803..db801e9 100644 --- a/index.js +++ b/index.js @@ -154,8 +154,8 @@ function printDottedLine() { function printProducts(products) { let ticket = ""; - const lineWidth = 46; // Ajusta según tu impresora (32, 42, 48 caracteres típicos) - const priceWidth = 10; // Espacio reservado para el precio (ej: "$999.99") + const lineWidth = 46; + const priceWidth = 10; for (const producto of products) { const nombre = removeAccents(producto?.nombre); @@ -163,22 +163,46 @@ function printProducts(products) { const precio = `$${producto?.precio}`; // 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 - const availableSpace = lineWidth - precio.length; + // Calcular espacio disponible para el texto completo (sin considerar el precio aún) + const availableSpace = lineWidth; - // Si el nombre es muy largo, cortarlo - if (leftPart.length > availableSpace) { - leftPart = leftPart.substring(0, availableSpace - 3) + "..."; + if (leftPart.length <= lineWidth - precio.length - 1) { + // Si cabe todo en una línea (con el precio) + const spaces = lineWidth - leftPart.length - precio.length; + const spacePadding = " ".repeat(Math.max(0, spaces)); + 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(); + } } - // Rellenar con espacios hasta que el precio quede alineado a la derecha - const spaces = lineWidth - leftPart.length - precio.length; - const spacePadding = " ".repeat(Math.max(0, spaces)); - - ticket += left() + leftPart + spacePadding + precio + line(); - if (producto?.observaciones?.length > 0) { const obs = removeAccents(producto.observaciones); ticket += "Observaciones: " + obs + line(); @@ -244,7 +268,8 @@ async function ticketDPSTock( // Total ticket += line(2); - ticket += left() + `Total: $${total}` + line(3); + //ticket += left() + `Total: $${total}` + line(3); + ticket += printBetween("Total: ", `$${total}`) + line(3); // Corte ticket += cut();