precuenta.php 7.8 KB

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