precuenta_movil.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Archivo para imprimir precuentas desde app móvil
  4. * Imprime solo en la impresora de caja (normalmente POS-80C)
  5. * Usa librería: mike42/escpos-php
  6. */
  7. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  8. header("Access-Control-Allow-Origin: *");
  9. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  10. header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
  11. http_response_code(204);
  12. exit;
  13. }
  14. header("Access-Control-Allow-Origin: *");
  15. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
  16. header("Access-Control-Allow-Headers: Content-Type, Authorization");
  17. header('Content-Type: application/json; charset=utf-8');
  18. error_reporting(E_ALL);
  19. ini_set('display_errors', 0);
  20. ini_set('log_errors', 1);
  21. ini_set('error_log', 'logs/error.log');
  22. require __DIR__ . '/vendor/autoload.php';
  23. use Mike42\Escpos\Printer;
  24. use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
  25. // Recibir datos
  26. $dataPrint = null;
  27. if(isset($_POST["dataPrint"])){
  28. if(is_string($_POST["dataPrint"])){
  29. $dataPrint = json_decode($_POST["dataPrint"], true);
  30. } else {
  31. $dataPrint = $_POST["dataPrint"];
  32. }
  33. }
  34. if(!$dataPrint){
  35. echo json_encode([
  36. 'success' => false,
  37. 'status' => 'error',
  38. 'message' => 'No se recibieron datos para imprimir'
  39. ]);
  40. exit;
  41. }
  42. try {
  43. // Nombre de la impresora de caja (ajustar según tu configuración)
  44. $nombreImpresora = isset($dataPrint['printer']) ? $dataPrint['printer'] : "POS-80C";
  45. $connector = new WindowsPrintConnector($nombreImpresora);
  46. $printer = new Printer($connector);
  47. $anchoPapel = isset($dataPrint['anchopapel']) ? intval($dataPrint['anchopapel']) : 80;
  48. // ENCABEZADO
  49. $printer->setJustification(Printer::JUSTIFY_CENTER);
  50. $printer->setTextSize(2, 2);
  51. $printer->text($dataPrint['empresa']['nombre_empresa'] . "\n");
  52. $printer->setTextSize(1, 1);
  53. $printer->text($dataPrint['empresa']['direccion_empresa'] . "\n");
  54. $printer->text("Tel: " . $dataPrint['empresa']['telefono_empresa'] . "\n");
  55. $printer->text(str_repeat("=", 45) . "\n");
  56. $printer->setEmphasis(true);
  57. $printer->text("PRECUENTA\n");
  58. $printer->setEmphasis(false);
  59. $printer->text(str_repeat("=", 45) . "\n");
  60. // INFORMACIÓN DE LA ORDEN
  61. $printer->setJustification(Printer::JUSTIFY_LEFT);
  62. $printer->setEmphasis(true);
  63. $printer->text("Orden #: ");
  64. $printer->setEmphasis(false);
  65. $printer->text($dataPrint['num_orden'] . "\n");
  66. $printer->setEmphasis(true);
  67. $printer->text("Fecha: ");
  68. $printer->setEmphasis(false);
  69. $printer->text(date('d/m/Y H:i', strtotime($dataPrint['fecha'])) . "\n");
  70. if (!empty($dataPrint['salon'])) {
  71. $printer->setEmphasis(true);
  72. $printer->text("Salon: ");
  73. $printer->setEmphasis(false);
  74. $printer->text($dataPrint['salon'] . "\n");
  75. }
  76. if (!empty($dataPrint['mesa'])) {
  77. $printer->setEmphasis(true);
  78. $printer->text("Mesa: ");
  79. $printer->setEmphasis(false);
  80. $printer->text($dataPrint['mesa'] . "\n");
  81. }
  82. /*
  83. if (!empty($dataPrint['mesero'])) {
  84. $printer->setEmphasis(true);
  85. $printer->text("Mesero: ");
  86. $printer->setEmphasis(false);
  87. $printer->text($dataPrint['mesero'] . "\n");
  88. }*/
  89. if (!empty($dataPrint['cliente'])) {
  90. $printer->setEmphasis(true);
  91. $printer->text("Cliente: ");
  92. $printer->setEmphasis(false);
  93. $printer->text($dataPrint['cliente'] . "\n");
  94. }
  95. if (!empty($dataPrint['servicio'])) {
  96. $printer->setEmphasis(true);
  97. $printer->text("Servicio: ");
  98. $printer->setEmphasis(false);
  99. $printer->text($dataPrint['servicio'] . "\n");
  100. }
  101. $printer->text(str_repeat("-", 45) . "\n");
  102. // ENCABEZADO DE TABLA
  103. $printer->setEmphasis(true);
  104. $printer->text("CANT DESCRIPCION TOTAL\n");
  105. $printer->setEmphasis(false);
  106. $printer->text(str_repeat("-", 45) . "\n");
  107. // PLATOS
  108. $subtotal = 0;
  109. foreach ($dataPrint['platos'] as $plato) {
  110. // Convertir objeto a array si es necesario
  111. $platoArray = is_object($plato) ? (array)$plato : $plato;
  112. $cant = number_format($platoArray['cant'], 0);
  113. $nombre = $platoArray['nombre'];
  114. $precio = $platoArray['precio'];
  115. $total = $platoArray['cant'] * $platoArray['precio'];
  116. $subtotal += $total;
  117. // Línea del plato - cant y nombre
  118. $cantPad = str_pad($cant, 4);
  119. $nombreCorto = substr($nombre, 0, 25);
  120. $totalStr = "$" . number_format($total, 2);
  121. $printer->text($cantPad . "x " . str_pad($nombreCorto, 27) . str_pad($totalStr, 10, " ", STR_PAD_LEFT) . "\n");
  122. // Acompañamientos
  123. if (isset($platoArray['acompanamientos']) && is_array($platoArray['acompanamientos']) && count($platoArray['acompanamientos']) > 0) {
  124. foreach ($platoArray['acompanamientos'] as $acomp) {
  125. $acompArray = is_object($acomp) ? (array)$acomp : $acomp;
  126. $printer->text(" + " . $acompArray['acompanamiento'] . "\n");
  127. }
  128. }
  129. /*
  130. if (!empty($platoArray['notas'])) {
  131. $printer->text(" Nota: " . $platoArray['notas'] . "\n");
  132. }*/
  133. }
  134. $printer->text(str_repeat("=", 45) . "\n");
  135. // TOTALES
  136. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  137. $printer->text("Subtotal: $" . number_format($dataPrint['subtotal'], 2) . "\n");
  138. if (isset($dataPrint['descuento']) && $dataPrint['descuento'] > 0) {
  139. $printer->text("Descuento: -$" . number_format($dataPrint['descuento'], 2) . "\n");
  140. }
  141. if (isset($dataPrint['propina']) && $dataPrint['propina'] > 0) {
  142. $printer->text("Propina: $" . number_format($dataPrint['propina'], 2) . "\n");
  143. }
  144. $total = $dataPrint['subtotal'] - (isset($dataPrint['descuento']) ? $dataPrint['descuento'] : 0) + (isset($dataPrint['propina']) ? $dataPrint['propina'] : 0);
  145. $printer->text(str_repeat("-", 45) . "\n");
  146. $printer->setEmphasis(true);
  147. $printer->setTextSize(2, 1);
  148. $printer->text("TOTAL: $" . number_format($total, 2) . "\n");
  149. $printer->setTextSize(1, 1);
  150. $printer->setEmphasis(false);
  151. $printer->text(str_repeat("=", 45) . "\n");
  152. $printer->setJustification(Printer::JUSTIFY_CENTER);
  153. $printer->text("\n¡Gracias por su preferencia!\n");
  154. $printer->text("Esta NO es una factura\n");
  155. $printer->feed(3);
  156. $printer->cut();
  157. $printer->close();
  158. echo json_encode([
  159. 'success' => true,
  160. 'status' => 'success',
  161. 'message' => 'Precuenta impresa correctamente en ' . $nombreImpresora
  162. ]);
  163. } catch (Exception $e) {
  164. echo json_encode([
  165. 'success' => false,
  166. 'status' => 'error',
  167. 'message' => 'Error al imprimir: ' . $e->getMessage()
  168. ]);
  169. }