ticket.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  3. header("Access-Control-Allow-Origin: *");
  4. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  5. header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
  6. http_response_code(204); // Sin contenido
  7. exit;
  8. }
  9. header("Access-Control-Allow-Origin: *");
  10. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
  11. header("Access-Control-Allow-Headers: Content-Type, Authorization");
  12. error_reporting(E_ALL);
  13. ini_set('display_errors', 0);
  14. ini_set('log_errors', 1);
  15. ini_set('error_log', '/logs/error.log');
  16. require __DIR__ . '/vendor/autoload.php';
  17. use Mike42\Escpos\Printer;
  18. use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
  19. use Mike42\Escpos\EscposImage;
  20. // Helper functions for ticket formatting
  21. function cutString($text, $maxLength) {
  22. return substr($text, 0, $maxLength);
  23. }
  24. function printAlignedText($printer, $leftText, $rightText, $width = 48) {
  25. $rightLen = strlen($rightText);
  26. $leftLen = $width - $rightLen;
  27. $leftPadded = str_pad($leftText, $leftLen, " ", STR_PAD_RIGHT);
  28. $printer->text($leftPadded . $rightText . "\n");
  29. }
  30. function printSeparatorLine($printer, $char = "-", $width = 48) {
  31. $printer->text(str_repeat($char, $width) . "\n");
  32. }
  33. function printThreeColumns($printer, $left, $center, $right, $width = 48) {
  34. $colWidth = intval($width / 3);
  35. $leftPadded = str_pad($left, $colWidth, " ", STR_PAD_RIGHT);
  36. $centerPadded = str_pad($center, $colWidth, " ", STR_PAD_BOTH);
  37. $rightPadded = str_pad($right, $colWidth, " ", STR_PAD_LEFT);
  38. $printer->text($leftPadded . $centerPadded . $rightPadded . "\n");
  39. }
  40. // Get data from POST request
  41. $dataPrint = null;
  42. if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST["dataPrint"])) {
  43. $dataPrint = json_decode($_POST["dataPrint"]);
  44. }
  45. if ($dataPrint) {
  46. // Extract printer name
  47. $PRINTER_NAME = $dataPrint->printer ?? 'POS-80C';
  48. // Constants for formatting
  49. $maxDescripcion = 23; // Maximum characters per line for product description
  50. try {
  51. $connector = new WindowsPrintConnector($PRINTER_NAME);
  52. $printer = new Printer($connector);
  53. // COMPANY HEADER
  54. $printer->setJustification(Printer::JUSTIFY_CENTER);
  55. $printer->setTextSize(2, 2);
  56. $printer->text(cutString(strtoupper($dataPrint->nombre_empresa), 24) . "\n");
  57. $printer->setTextSize(1, 1);
  58. $printer->text(cutString(strtoupper($dataPrint->giro_empresa), 48) . "\n");
  59. $printer->text(cutString(strtoupper($dataPrint->direcion_empresa), 48) . "\n");
  60. $printer->text(cutString(strtoupper($dataPrint->rsocial_empresa), 48) . "\n");
  61. $printer->text("NRC: " . $dataPrint->nrc_empresa . " - NIT: " . $dataPrint->nit_empresa . "\n");
  62. printSeparatorLine($printer);
  63. // DATE, CASHIER AND TICKET INFO
  64. $printer->setJustification(Printer::JUSTIFY_LEFT);
  65. printAlignedText($printer, "Fecha: " . $dataPrint->fecha, "Caja: " . $dataPrint->caja);
  66. printAlignedText($printer, "Vendedor: " . $dataPrint->vendedor, "Cajero: " . $dataPrint->cajero);
  67. $printer->text("Serie: " . $dataPrint->doc_serie . " Del " . $dataPrint->doc_desde . " Al " . $dataPrint->doc_hasta . "\n");
  68. $printer->text("No Resolucion: " . $dataPrint->doc_resolucion . "\n");
  69. $printer->setJustification(Printer::JUSTIFY_CENTER);
  70. $printer->setEmphasis(true);
  71. $printer->text("Ticket: " . $dataPrint->doc_numero . "\n");
  72. $printer->setEmphasis(false);
  73. printSeparatorLine($printer);
  74. // PRODUCT TABLE HEADER
  75. $printer->setJustification(Printer::JUSTIFY_LEFT);
  76. $printer->setEmphasis(true);
  77. $printer->text("CANT DESCRIPCION PRECIO TOTAL\n");
  78. printSeparatorLine($printer);
  79. $printer->setEmphasis(false);
  80. // PRODUCTS
  81. foreach ($dataPrint->productos_normal as $producto) {
  82. $cantidad = str_pad(number_format($producto->cant, 2), 5, " ", STR_PAD_RIGHT);
  83. $precio = str_pad(number_format($producto->costo, 2), 7, " ", STR_PAD_LEFT);
  84. $total = str_pad(number_format($producto->costo * $producto->cant, 2), 7, " ", STR_PAD_LEFT);
  85. // Handle product description with word wrapping
  86. $descripcion_completa = ($producto->ref ? $producto->ref . ' - ' : '') . $producto->desc;
  87. $descripcion = wordwrap(strtoupper($descripcion_completa), $maxDescripcion, "\n", true);
  88. $lineasDescripcion = explode("\n", $descripcion);
  89. // Print first line with all data
  90. $printer->text("$cantidad " . str_pad($lineasDescripcion[0], $maxDescripcion, " ") . " $precio $total");
  91. // Add tax type indicator
  92. if (isset($producto->tipoventa)) {
  93. $printer->text(" " . strtoupper($producto->tipoventa));
  94. }
  95. $printer->text("\n");
  96. // Print additional description lines if any
  97. for ($i = 1; $i < count($lineasDescripcion); $i++) {
  98. $printer->text(" " . $lineasDescripcion[$i] . "\n");
  99. }
  100. }
  101. printSeparatorLine($printer);
  102. // TAX TOTALS SECTION
  103. $printer->setJustification(Printer::JUSTIFY_LEFT);
  104. $printer->text("G = Gravado. " . str_pad("TOTAL GRAVADO", 17) . "$" . number_format($dataPrint->totales->totalTotal, 2) . "\n");
  105. $printer->text("E = Exento. " . str_pad("TOTAL EXENTO", 17) . "$" . number_format($dataPrint->totales->totalExento, 2) . "\n");
  106. $printer->text("NS = No Sujeto. " . str_pad("TOTAL NO SUJETO", 17) . "$" . number_format($dataPrint->totales->totalNS, 2) . "\n");
  107. // PAYMENT SECTION
  108. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  109. $printer->setEmphasis(true);
  110. $printer->setTextSize(1, 2);
  111. $printer->text("TOTAL $" . number_format($dataPrint->totales->totalTotal, 2) . "\n");
  112. $printer->setTextSize(1, 1);
  113. $printer->setEmphasis(false);
  114. $printer->text("EFECTIVO $" . number_format($dataPrint->efectivo, 2) . "\n");
  115. $printer->setJustification(Printer::JUSTIFY_LEFT);
  116. printAlignedText($printer, "REF.: " . $dataPrint->referencia, "CAMBIO $" . number_format(($dataPrint->efectivo - $dataPrint->totales->totalTotal), 2));
  117. // CUSTOMER INFORMATION
  118. if (isset($dataPrint->cliente) && $dataPrint->cliente != "") {
  119. $printer->text("Cliente: " . $dataPrint->cliente . "\n");
  120. }
  121. if (isset($dataPrint->direccion_cliente) && $dataPrint->direccion_cliente != "") {
  122. $printer->text("Direccion: " . $dataPrint->direccion_cliente . "\n");
  123. }
  124. printSeparatorLine($printer);
  125. // FINAL MESSAGE
  126. $printer->setJustification(Printer::JUSTIFY_CENTER);
  127. $printer->text("\n");
  128. // Word wrap the message if it exists
  129. if (isset($dataPrint->mensaje_ticket) && $dataPrint->mensaje_ticket != "") {
  130. $mensajeLines = wordwrap($dataPrint->mensaje_ticket, 48, "\n", true);
  131. foreach (explode("\n", $mensajeLines) as $linea) {
  132. $printer->text($linea . "\n");
  133. }
  134. }
  135. // FINISH PRINTING
  136. $printer->feed(2);
  137. $printer->cut();
  138. $printer->close();
  139. echo json_encode([
  140. "status" => "success",
  141. "message" => "Ticket impreso correctamente."
  142. ]);
  143. } catch (Exception $e) {
  144. echo json_encode([
  145. "status" => "error",
  146. "message" => "Error al imprimir: " . $e->getMessage()
  147. ]);
  148. }
  149. }
  150. ?>