recibo_express.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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);
  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', __DIR__ . '/logs/error.log');
  16. require __DIR__ . '/../vendor/autoload.php';
  17. use Mike42\Escpos\Printer;
  18. use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
  19. // Helper functions
  20. function printLinea($printer, $char = "-", $width = 48) {
  21. $printer->text(str_repeat($char, $width) . "\n");
  22. }
  23. function cutString($text, $maxLength) {
  24. return substr($text, 0, $maxLength);
  25. }
  26. function printAlignedText($printer, $leftText, $rightText, $width = 48) {
  27. $rightLen = strlen($rightText);
  28. $leftLen = $width - $rightLen;
  29. $leftPadded = str_pad($leftText, $leftLen, " ", STR_PAD_RIGHT);
  30. $printer->text($leftPadded . $rightText . "\n");
  31. }
  32. // Get data from POST
  33. $dataPrint = null;
  34. if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST["dataPrint"])) {
  35. $dataPrint = json_decode($_POST["dataPrint"]);
  36. }
  37. if ($dataPrint) {
  38. $PRINTER_NAME = $dataPrint->printer ?? 'POS-80C';
  39. try {
  40. $connector = new WindowsPrintConnector($PRINTER_NAME);
  41. $printer = new Printer($connector);
  42. // COMPANY HEADER
  43. $printer->setJustification(Printer::JUSTIFY_CENTER);
  44. $printer->setTextSize(2, 2);
  45. $printer->text(cutString(strtoupper($dataPrint->nombre_empresa), 24) . "\n");
  46. $printer->setTextSize(1, 1);
  47. //$printer->text(cutString(strtoupper($dataPrint->giro_empresa ?? ''), 48) . "\n");
  48. //$printer->text(cutString(strtoupper($dataPrint->direcion_empresa), 48) . "\n");
  49. //$printer->text(cutString(strtoupper($dataPrint->rsocial_empresa), 48) . "\n");
  50. //$printer->text("NRC: " . $dataPrint->nrc_empresa . " - NIT: " . $dataPrint->nit_empresa . "\n");
  51. printLinea($printer);
  52. // DOCUMENT INFO
  53. $printer->setJustification(Printer::JUSTIFY_LEFT);
  54. printAlignedText($printer, "Fecha: " . $dataPrint->fecha, "Caja: " . $dataPrint->caja);
  55. printAlignedText($printer, "Vendedor: " . $dataPrint->vendedor, "Cajero: " . $dataPrint->cajero);
  56. $printer->setJustification(Printer::JUSTIFY_CENTER);
  57. $printer->setEmphasis(true);
  58. $printer->text("RECIBO\n");
  59. $printer->text("No. " . $dataPrint->doc_numero . "\n");
  60. $printer->setEmphasis(false);
  61. printLinea($printer);
  62. // CUSTOMER INFO
  63. if (isset($dataPrint->cliente) && $dataPrint->cliente != "") {
  64. $printer->setJustification(Printer::JUSTIFY_LEFT);
  65. $printer->text("Cliente: " . $dataPrint->cliente . "\n");
  66. if (isset($dataPrint->direccion_cliente) && $dataPrint->direccion_cliente != "") {
  67. $printer->text("Direccion: " . $dataPrint->direccion_cliente . "\n");
  68. }
  69. printLinea($printer);
  70. }
  71. // PRODUCTS TABLE
  72. $printer->setJustification(Printer::JUSTIFY_LEFT);
  73. $printer->setEmphasis(true);
  74. $printer->text("CANT DESCRIPCION PRECIO TOTAL\n");
  75. printLinea($printer);
  76. $printer->setEmphasis(false);
  77. foreach ($dataPrint->productos_normal as $producto) {
  78. $cantidad = str_pad(number_format($producto->cant, 2), 5, " ", STR_PAD_RIGHT);
  79. $precio = str_pad(number_format($producto->costo, 2), 7, " ", STR_PAD_LEFT);
  80. $total = str_pad(number_format($producto->costo * $producto->cant, 2), 7, " ", STR_PAD_LEFT);
  81. $descripcion = wordwrap(strtoupper($producto->desc), 23, "\n", true);
  82. $lineasDescripcion = explode("\n", $descripcion);
  83. $printer->text("$cantidad " . str_pad($lineasDescripcion[0], 23, " ") . " $precio $total\n");
  84. for ($i = 1; $i < count($lineasDescripcion); $i++) {
  85. $printer->text(" " . $lineasDescripcion[$i] . "\n");
  86. }
  87. }
  88. printLinea($printer);
  89. // TOTALS
  90. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  91. $printer->setEmphasis(true);
  92. $printer->setTextSize(1, 2);
  93. $printer->text("TOTAL $" . number_format($dataPrint->totales->totalTotal, 2) . "\n");
  94. $printer->setTextSize(1, 1);
  95. $printer->setEmphasis(false);
  96. $printer->text("EFECTIVO $" . number_format($dataPrint->efectivo, 2) . "\n");
  97. if (isset($dataPrint->pos) && floatval($dataPrint->pos) > 0) {
  98. $printer->text("POS $" . number_format($dataPrint->pos, 2) . "\n");
  99. }
  100. $printer->setJustification(Printer::JUSTIFY_LEFT);
  101. printAlignedText($printer, "REF.: " . $dataPrint->referencia, "CAMBIO $" . number_format($dataPrint->cambio, 2));
  102. printLinea($printer);
  103. /*
  104. if (isset($dataPrint->notas) && $dataPrint->notas != "") {
  105. $printer->setJustification(Printer::JUSTIFY_LEFT);
  106. $printer->text("Notas: " . $dataPrint->notas . "\n");
  107. printLinea($printer);
  108. }
  109. */
  110. // FINAL MESSAGE
  111. $printer->setJustification(Printer::JUSTIFY_CENTER);
  112. $printer->text("\n");
  113. if (isset($dataPrint->mensaje_ticket) && $dataPrint->mensaje_ticket != "") {
  114. $mensajeLines = wordwrap($dataPrint->mensaje_ticket, 48, "\n", true);
  115. foreach (explode("\n", $mensajeLines) as $linea) {
  116. $printer->text($linea . "\n");
  117. }
  118. }
  119. // FINISH
  120. $printer->feed(2);
  121. $printer->cut();
  122. $printer->close();
  123. echo json_encode([
  124. "status" => "success",
  125. "message" => "Recibo impreso correctamente."
  126. ]);
  127. } catch (Exception $e) {
  128. echo json_encode([
  129. "status" => "error",
  130. "message" => "Error al imprimir: " . $e->getMessage()
  131. ]);
  132. }
  133. } else {
  134. echo json_encode([
  135. "status" => "error",
  136. "message" => "No se recibieron datos para imprimir."
  137. ]);
  138. }
  139. ?>