precuenta.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. function printAlignedTextBold($printer, $leftLabel, $leftValue, $rightLabel, $rightValue, $width = 48) {
  33. // Imprimir etiqueta izquierda en negrita
  34. $printer->setEmphasis(true);
  35. $printer->text($leftLabel);
  36. $printer->setEmphasis(false);
  37. // Calcular espacios necesarios
  38. $rightText = $rightLabel . $rightValue;
  39. $rightLen = strlen($rightText);
  40. $leftTotal = strlen($leftLabel . $leftValue);
  41. $spaces = $width - $leftTotal - $rightLen;
  42. // Imprimir valor izquierdo y espacios
  43. $printer->text($leftValue . str_repeat(" ", $spaces));
  44. // Imprimir etiqueta derecha en negrita
  45. $printer->setEmphasis(true);
  46. $printer->text($rightLabel);
  47. $printer->setEmphasis(false);
  48. $printer->text($rightValue . "\n");
  49. }
  50. function printTextBold($printer, $label, $value) {
  51. $printer->setEmphasis(true);
  52. $printer->text($label);
  53. $printer->setEmphasis(false);
  54. $printer->text($value . "\n");
  55. }
  56. // Get data from POST
  57. $dataPrint = null;
  58. if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST["dataPrint"])) {
  59. $dataPrint = json_decode($_POST["dataPrint"]);
  60. }
  61. if ($dataPrint) {
  62. $PRINTER_NAME = $dataPrint->printer ?? 'POS-80C';
  63. try {
  64. $connector = new WindowsPrintConnector($PRINTER_NAME);
  65. $printer = new Printer($connector);
  66. // COMPANY HEADER
  67. $printer->setJustification(Printer::JUSTIFY_CENTER);
  68. $printer->setTextSize(2, 2);
  69. $printer->text(cutString(strtoupper($dataPrint->nombre_empresa), 24) . "\n");
  70. $printer->setTextSize(1, 1);
  71. //$printer->text(cutString(strtoupper($dataPrint->giro_empresa ?? ''), 48) . "\n");
  72. //$printer->text(cutString(strtoupper($dataPrint->direcion_empresa), 48) . "\n");
  73. //$printer->text(cutString(strtoupper($dataPrint->rsocial_empresa), 48) . "\n");
  74. //$printer->text("NRC: " . $dataPrint->nrc_empresa . " - NIT: " . $dataPrint->nit_empresa . "\n");
  75. printLinea($printer);
  76. // DOCUMENT INFO
  77. $printer->setJustification(Printer::JUSTIFY_LEFT);
  78. //$printer->text("Control de Ticket Interno");
  79. printAlignedTextBold($printer, "Fecha: ", $dataPrint->fecha, "Caja: ", $dataPrint->caja);
  80. printAlignedTextBold($printer, "Vendedor: ", $dataPrint->vendedor, "Mesa: ", $dataPrint->mesa_orden);
  81. printTextBold($printer, "Servicio: ", $dataPrint->servicio);
  82. printLinea($printer);
  83. // CUSTOMER INFO
  84. if (isset($dataPrint->cliente) && $dataPrint->cliente != "") {
  85. $printer->setJustification(Printer::JUSTIFY_LEFT);
  86. $printer->text("Cliente: " . $dataPrint->cliente . "\n");
  87. if (isset($dataPrint->direccion_cliente) && $dataPrint->direccion_cliente != "") {
  88. $printer->text("Direccion: " . $dataPrint->direccion_cliente . "\n");
  89. }
  90. printLinea($printer);
  91. }
  92. // PRODUCTS TABLE
  93. $printer->setJustification(Printer::JUSTIFY_LEFT);
  94. $printer->setEmphasis(true);
  95. $printer->text("CANT DESCRIPCION PRECIO TOTAL\n");
  96. printLinea($printer);
  97. $printer->setEmphasis(false);
  98. foreach ($dataPrint->productos_normal as $producto) {
  99. $cantidad = str_pad(number_format($producto->cant, 2), 5, " ", STR_PAD_RIGHT);
  100. $precio = str_pad(number_format($producto->costo, 2), 7, " ", STR_PAD_LEFT);
  101. $total = str_pad(number_format($producto->costo * $producto->cant, 2), 7, " ", STR_PAD_LEFT);
  102. $descripcion = wordwrap(strtoupper($producto->desc), 23, "\n", true);
  103. $lineasDescripcion = explode("\n", $descripcion);
  104. $printer->text("$cantidad " . str_pad($lineasDescripcion[0], 23, " ") . " $precio $total\n");
  105. for ($i = 1; $i < count($lineasDescripcion); $i++) {
  106. $printer->text(" " . $lineasDescripcion[$i] . "\n");
  107. }
  108. // Imprimir acompañamientos si existen
  109. if (isset($producto->acompanamientos) && $producto->acompanamientos != "") {
  110. $printer->text(" CON: " . strtoupper($producto->acompanamientos) . "\n");
  111. }
  112. // Imprimir notas si existen
  113. if (isset($producto->notas) && $producto->notas != "") {
  114. $printer->text(" * " . strtoupper($producto->notas) . "\n");
  115. }
  116. }
  117. printLinea($printer);
  118. // TOTALS
  119. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  120. $printer->setEmphasis(true);
  121. $printer->setTextSize(1, 2);
  122. $printer->text("TOTAL $" . number_format($dataPrint->totales->totalTotal, 2) . "\n");
  123. $printer->setTextSize(1, 1);
  124. $printer->setEmphasis(false);
  125. $printer->text("EFECTIVO $" . number_format($dataPrint->efectivo, 2) . "\n");
  126. $printer->text("CAMBIO $" . number_format($dataPrint->cambio, 2) . "\n");
  127. if (isset($dataPrint->pos) && floatval($dataPrint->pos) > 0) {
  128. $printer->text("POS $" . number_format($dataPrint->pos, 2) . "\n");
  129. }
  130. printLinea($printer);
  131. // NOTAS DE ORDEN
  132. if (isset($dataPrint->notas_orden) && $dataPrint->notas_orden != "") {
  133. $printer->setJustification(Printer::JUSTIFY_LEFT);
  134. $printer->setEmphasis(true);
  135. $printer->text("NOTAS ORDEN: \n");
  136. $printer->setEmphasis(false);
  137. $notasLines = wordwrap($dataPrint->notas_orden, 48, "\n", true);
  138. foreach (explode("\n", $notasLines) as $linea) {
  139. $printer->text($linea . "\n");
  140. }
  141. }
  142. printLinea($printer);
  143. $printer->setJustification(Printer::JUSTIFY_CENTER);
  144. $printer->text("\n");
  145. $printer->text("REF.: " . ($dataPrint->referencia) . "\n");
  146. /*
  147. if (isset($dataPrint->notas) && $dataPrint->notas != "") {
  148. $printer->setJustification(Printer::JUSTIFY_LEFT);
  149. $printer->text("Notas: " . $dataPrint->notas . "\n");
  150. printLinea($printer);
  151. }
  152. */
  153. /*
  154. $printer->setJustification(Printer::JUSTIFY_CENTER);
  155. $printer->text("\n");
  156. if (isset($dataPrint->mensaje_ticket) && $dataPrint->mensaje_ticket != "") {
  157. $mensajeLines = wordwrap($dataPrint->mensaje_ticket, 48, "\n", true);
  158. foreach (explode("\n", $mensajeLines) as $linea) {
  159. $printer->text($linea . "\n");
  160. }
  161. }
  162. */
  163. // FINISH
  164. $printer->feed(2);
  165. $printer->cut();
  166. $printer->close();
  167. echo json_encode([
  168. "status" => "success",
  169. "message" => "Recibo impreso correctamente."
  170. ]);
  171. } catch (Exception $e) {
  172. echo json_encode([
  173. "status" => "error",
  174. "message" => "Error al imprimir: " . $e->getMessage()
  175. ]);
  176. }
  177. } else {
  178. echo json_encode([
  179. "status" => "error",
  180. "message" => "No se recibieron datos para imprimir."
  181. ]);
  182. }
  183. ?>