precuenta.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // Si es string JSON, decodificar
  60. if(is_string($_POST["dataPrint"])){
  61. $dataPrint = json_decode($_POST["dataPrint"]);
  62. } else {
  63. // Si jQuery ya lo deserializó como array, convertir a objeto
  64. $dataPrint = json_decode(json_encode($_POST["dataPrint"]));
  65. }
  66. }
  67. if ($dataPrint) {
  68. $PRINTER_NAME = $dataPrint->printer ?? 'POS-80C';
  69. try {
  70. $connector = new WindowsPrintConnector($PRINTER_NAME);
  71. $printer = new Printer($connector);
  72. // COMPANY HEADER
  73. $printer->setJustification(Printer::JUSTIFY_CENTER);
  74. $printer->setTextSize(2, 2);
  75. $printer->text(cutString(strtoupper($dataPrint->nombre_empresa), 24) . "\n");
  76. $printer->setTextSize(1, 1);
  77. //$printer->text(cutString(strtoupper($dataPrint->giro_empresa ?? ''), 48) . "\n");
  78. //$printer->text(cutString(strtoupper($dataPrint->direcion_empresa), 48) . "\n");
  79. //$printer->text(cutString(strtoupper($dataPrint->rsocial_empresa), 48) . "\n");
  80. //$printer->text("NRC: " . $dataPrint->nrc_empresa . " - NIT: " . $dataPrint->nit_empresa . "\n");
  81. printLinea($printer);
  82. // DOCUMENT INFO
  83. $printer->setJustification(Printer::JUSTIFY_LEFT);
  84. //$printer->text("Control de Ticket Interno");
  85. printAlignedTextBold($printer, "Fecha: ", $dataPrint->fecha, "Caja: ", $dataPrint->caja);
  86. printAlignedTextBold($printer, "Vendedor: ", $dataPrint->vendedor, "Mesa: ", $dataPrint->mesa_orden);
  87. printTextBold($printer, "Servicio: ", $dataPrint->servicio);
  88. printLinea($printer);
  89. // CUSTOMER INFO
  90. if (isset($dataPrint->cliente) && $dataPrint->cliente != "") {
  91. $printer->setJustification(Printer::JUSTIFY_LEFT);
  92. $printer->text("Cliente: " . $dataPrint->cliente . "\n");
  93. if (isset($dataPrint->direccion_cliente) && $dataPrint->direccion_cliente != "") {
  94. $printer->text("Direccion: " . $dataPrint->direccion_cliente . "\n");
  95. }
  96. printLinea($printer);
  97. }
  98. // PRODUCTS TABLE
  99. $printer->setJustification(Printer::JUSTIFY_LEFT);
  100. $printer->setEmphasis(true);
  101. $printer->text("CANT DESCRIPCION PRECIO TOTAL\n");
  102. printLinea($printer);
  103. $printer->setEmphasis(false);
  104. foreach ($dataPrint->productos_normal as $producto) {
  105. $cantidad = str_pad(number_format($producto->cant, 2), 5, " ", STR_PAD_RIGHT);
  106. $precio = str_pad(number_format($producto->costo, 2), 7, " ", STR_PAD_LEFT);
  107. $total = str_pad(number_format($producto->costo * $producto->cant, 2), 7, " ", STR_PAD_LEFT);
  108. $descripcion = wordwrap(strtoupper($producto->desc), 23, "\n", true);
  109. $lineasDescripcion = explode("\n", $descripcion);
  110. $printer->text("$cantidad " . str_pad($lineasDescripcion[0], 23, " ") . " $precio $total\n");
  111. for ($i = 1; $i < count($lineasDescripcion); $i++) {
  112. $printer->text(" " . $lineasDescripcion[$i] . "\n");
  113. }
  114. // Imprimir acompañamientos si existen
  115. if (isset($producto->acompanamientos) && $producto->acompanamientos != "") {
  116. $printer->setEmphasis(true);
  117. $printer->text(" CON: ");
  118. $printer->setEmphasis(false);
  119. $printer->text(strtoupper($producto->acompanamientos) . "\n");
  120. }
  121. // Imprimir notas si existen
  122. if (isset($producto->notas) && $producto->notas != "") {
  123. $printer->setEmphasis(true);
  124. $printer->text(" NOTA: ");
  125. $printer->setEmphasis(false);
  126. $printer->text(strtoupper($producto->notas) . "\n");
  127. }
  128. }
  129. printLinea($printer);
  130. // TOTALS
  131. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  132. $printer->setEmphasis(true);
  133. $printer->setTextSize(1, 2);
  134. $printer->text("TOTAL $" . number_format($dataPrint->totales->totalTotal, 2) . "\n");
  135. $printer->setTextSize(1, 1);
  136. $printer->setEmphasis(false);
  137. $printer->text("EFECTIVO $" . number_format($dataPrint->efectivo, 2) . "\n");
  138. $printer->text("CAMBIO $" . number_format($dataPrint->cambio, 2) . "\n");
  139. if (isset($dataPrint->pos) && floatval($dataPrint->pos) > 0) {
  140. $printer->text("POS $" . number_format($dataPrint->pos, 2) . "\n");
  141. }
  142. printLinea($printer);
  143. // NOTAS DE ORDEN
  144. if (isset($dataPrint->notas_orden) && $dataPrint->notas_orden != "") {
  145. $printer->setJustification(Printer::JUSTIFY_LEFT);
  146. $printer->setEmphasis(true);
  147. $printer->text("NOTAS ORDEN:\n");
  148. $printer->setEmphasis(false);
  149. $notasLines = wordwrap($dataPrint->notas_orden, 48, "\n", true);
  150. foreach (explode("\n", $notasLines) as $linea) {
  151. $printer->text($linea . "\n");
  152. }
  153. }
  154. printLinea($printer);
  155. $printer->setJustification(Printer::JUSTIFY_CENTER);
  156. $printer->text("\n");
  157. $printer->text("REF.: " . ($dataPrint->referencia) . "\n");
  158. /*
  159. if (isset($dataPrint->notas) && $dataPrint->notas != "") {
  160. $printer->setJustification(Printer::JUSTIFY_LEFT);
  161. $printer->text("Notas: " . $dataPrint->notas . "\n");
  162. printLinea($printer);
  163. }
  164. */
  165. /*
  166. $printer->setJustification(Printer::JUSTIFY_CENTER);
  167. $printer->text("\n");
  168. if (isset($dataPrint->mensaje_ticket) && $dataPrint->mensaje_ticket != "") {
  169. $mensajeLines = wordwrap($dataPrint->mensaje_ticket, 48, "\n", true);
  170. foreach (explode("\n", $mensajeLines) as $linea) {
  171. $printer->text($linea . "\n");
  172. }
  173. }
  174. */
  175. // FINISH
  176. $printer->feed(2);
  177. $printer->cut();
  178. $printer->close();
  179. echo json_encode([
  180. "status" => "success",
  181. "message" => "Recibo impreso correctamente."
  182. ]);
  183. } catch (Exception $e) {
  184. echo json_encode([
  185. "status" => "error",
  186. "message" => "Error al imprimir: " . $e->getMessage()
  187. ]);
  188. }
  189. } else {
  190. echo json_encode([
  191. "status" => "error",
  192. "message" => "No se recibieron datos para imprimir."
  193. ]);
  194. }
  195. ?>