otras_salidas.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  21. $dataPrint = json_decode($_POST["dataPrint"]);
  22. if($dataPrint && json_last_error() === JSON_ERROR_NONE) {
  23. //Printer Name
  24. $PRINTER_NAME = $dataPrint->printer ?? 'POS-80C';
  25. //CONFIGURACIONES
  26. $maxDescripcion = 27; //Maximo de caracteres por linea para la descripcion del producto
  27. $anchoTicket = 48; //Ancho del ticket en caracteres
  28. //DATOS
  29. $nombreEmpresa = $dataPrint->empresa;
  30. $concepto = $dataPrint->concepto;
  31. $fecha = date("d/m/Y H:i:s", strtotime($dataPrint->fecha)) ?? null;
  32. try {
  33. $connector = new WindowsPrintConnector($PRINTER_NAME);
  34. $printer = new Printer($connector);
  35. $printer->setJustification(Printer::JUSTIFY_CENTER);
  36. $printer->setTextSize(2, 2);
  37. $printer->text("$nombreEmpresa\n");
  38. $printer->setTextSize(1, 1);
  39. $printer->text("$concepto\n");
  40. $printer->text("Fecha: ".$fecha." \n");
  41. $printer->setJustification(Printer::JUSTIFY_LEFT);
  42. $printer->text("\n");
  43. //PRODUCTOS
  44. $printer->setEmphasis(true);
  45. $printer->text("Descripcion Cant Costo Total\n");
  46. $printer->text(str_repeat("-", $anchoTicket) . "\n");
  47. $printer->setEmphasis(false);
  48. if (isset($dataPrint->productos) && is_array($dataPrint->productos)) {
  49. foreach ($dataPrint->productos as $producto) {
  50. $cantidad = str_pad(number_format($producto->cant, 2), 4, " ", STR_PAD_RIGHT);
  51. $costo = str_pad(number_format($producto->costo, 4), 7, " ", STR_PAD_LEFT);
  52. $total = str_pad(number_format($producto->cant * $producto->costo, 4), 7, " ", STR_PAD_LEFT);
  53. $descripcion = wordwrap(($producto->sku ?? '') . ' - ' . ($producto->desc ?? ''), $maxDescripcion, "\n", true);
  54. $lineasDescripcion = explode("\n", $descripcion);
  55. // Imprimir primera línea con cantidad, precio y total
  56. $printer->text(str_pad($lineasDescripcion[0], $maxDescripcion, " ") . "$cantidad " . $costo . " $total\n");
  57. // Imprimir líneas adicionales de la descripción (si hay más)
  58. for ($i = 1; $i < count($lineasDescripcion); $i++) {
  59. $printer->text(str_pad($lineasDescripcion[$i], $maxDescripcion) . "\n");
  60. }
  61. $printer->text(str_repeat("-", $anchoTicket) . "\n");
  62. }
  63. }
  64. $printer->text("\n");
  65. $printer->text("IMPRESO: \n");
  66. $printer->text(gmdate('Y-m-d H:i:s',strtotime('- 6 hours')) . " Por: " . ($dataPrint->usuario ?? 'Desconocido') . "\n");
  67. $printer->text("\n");
  68. $printer->text(str_pad("Notas de salida: ", $anchoTicket, "_", STR_PAD_RIGHT) . "\n");
  69. $printer->text(str_repeat("_", $anchoTicket) . "\n");
  70. $printer->text(str_repeat("_", $anchoTicket) . "\n");
  71. $printer->text("\n");
  72. $printer->text(str_pad("Recibido por: ", $anchoTicket, "_", STR_PAD_RIGHT) . "\n");
  73. $printer->text(str_pad("Notas de recepción: ", $anchoTicket, "_", STR_PAD_RIGHT) . "\n");
  74. $printer->text(str_repeat("_", $anchoTicket) . "\n");
  75. $printer->text(str_repeat("_", $anchoTicket) . "\n");
  76. $printer->text("\n");
  77. $printer->setJustification(Printer::JUSTIFY_CENTER);
  78. $printer->text(str_pad("Fin de operación", $anchoTicket, " * ", STR_PAD_BOTH) . "\n");
  79. $printer->feed(2);
  80. $printer->cut();
  81. $printer->close();
  82. }
  83. catch (Exception $e) {
  84. echo json_encode([
  85. "status" => "error",
  86. "message" => "Error al imprimir: " . $e->getMessage()
  87. ]);
  88. }
  89. /*print_r($dataPrint);
  90. $empresa = $dataPrint['empresa'] ?? '';
  91. $concepto = $dataPrint['concepto'] ?? '';
  92. $productos = $dataPrint['productos'] ?? null;
  93. print_r($empresa);
  94. echo "<hr>";
  95. print_r($concepto);
  96. echo "<hr>";
  97. print_r($productos);*/
  98. }