precuenta_caja.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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($dataPrint['direccion_empresa'] . "\n");
  51. $printer->text(str_repeat("=", 45) . "\n");
  52. $printer->setEmphasis(true);
  53. $printer->text("PRECUENTA\n");
  54. $printer->setEmphasis(false);
  55. $printer->text(str_repeat("=", 45) . "\n");
  56. // INFORMACIÓN DE LA ORDEN
  57. $printer->setJustification(Printer::JUSTIFY_LEFT);
  58. if (isset($dataPrint['orden'])) {
  59. $orden = is_object($dataPrint['orden']) ? (array)$dataPrint['orden'] : $dataPrint['orden'];
  60. if (!empty($orden['num_orden'])) {
  61. $printer->setEmphasis(true);
  62. $printer->text("Orden #: ");
  63. $printer->setEmphasis(false);
  64. $printer->text($orden['num_orden'] . "\n");
  65. }
  66. if (!empty($orden['fecha'])) {
  67. $printer->setEmphasis(true);
  68. $printer->text("Fecha: ");
  69. $printer->setEmphasis(false);
  70. $printer->text(date('d/m/Y H:i', strtotime($orden['fecha'])) . "\n");
  71. }
  72. if (!empty($orden['salon'])) {
  73. $printer->setEmphasis(true);
  74. $printer->text("Salon: ");
  75. $printer->setEmphasis(false);
  76. $printer->text($orden['salon'] . "\n");
  77. }
  78. if (!empty($orden['mesa'])) {
  79. $printer->setEmphasis(true);
  80. $printer->text("Mesa: ");
  81. $printer->setEmphasis(false);
  82. $printer->text($orden['mesa'] . "\n");
  83. }
  84. /*
  85. if (!empty($orden['mesero'])) {
  86. $printer->setEmphasis(true);
  87. $printer->text("Mesero: ");
  88. $printer->setEmphasis(false);
  89. $printer->text($orden['mesero'] . "\n");
  90. }*/
  91. if (!empty($orden['cliente'])) {
  92. $printer->setEmphasis(true);
  93. $printer->text("Cliente: ");
  94. $printer->setEmphasis(false);
  95. $printer->text($orden['cliente'] . "\n");
  96. }
  97. if (!empty($orden['servicio'])) {
  98. $printer->setEmphasis(true);
  99. $printer->text("Servicio: ");
  100. $printer->setEmphasis(false);
  101. $printer->text($orden['servicio'] . "\n");
  102. }
  103. }
  104. $printer->text(str_repeat("-", 45) . "\n");
  105. // ENCABEZADO DE TABLA
  106. $printer->setEmphasis(true);
  107. $printer->text("CANT DESCRIPCION TOTAL\n");
  108. $printer->setEmphasis(false);
  109. $printer->text(str_repeat("-", 45) . "\n");
  110. // PLATOS
  111. $subtotal = 0;
  112. if (isset($dataPrint['platos']) && is_array($dataPrint['platos'])) {
  113. foreach ($dataPrint['platos'] as $plato) {
  114. // Convertir objeto a array si es necesario
  115. $platoArray = is_object($plato) ? (array)$plato : $plato;
  116. $cant = isset($platoArray['cant']) ? number_format($platoArray['cant'], 0) : '0';
  117. $nombre = isset($platoArray['nombre']) ? $platoArray['nombre'] : '';
  118. $precio = isset($platoArray['precio']) ? floatval($platoArray['precio']) : 0;
  119. $cantNum = isset($platoArray['cant']) ? floatval($platoArray['cant']) : 0;
  120. $total = $cantNum * $precio;
  121. $subtotal += $total;
  122. // Línea del plato - cant y nombre
  123. $cantPad = str_pad($cant, 4);
  124. $nombreCorto = substr($nombre, 0, 25);
  125. $totalStr = "$" . number_format($total, 2);
  126. $printer->text($cantPad . "x " . str_pad($nombreCorto, 27) . str_pad($totalStr, 10, " ", STR_PAD_LEFT) . "\n");
  127. // Acompañamientos
  128. if (isset($platoArray['acompanamientos']) && is_array($platoArray['acompanamientos']) && count($platoArray['acompanamientos']) > 0) {
  129. foreach ($platoArray['acompanamientos'] as $acomp) {
  130. $acompArray = is_object($acomp) ? (array)$acomp : $acomp;
  131. if (isset($acompArray['acompanamiento'])) {
  132. $printer->text(" + " . $acompArray['acompanamiento'] . "\n");
  133. }
  134. }
  135. }
  136. /*
  137. if (!empty($platoArray['notas'])) {
  138. $printer->text(" Nota: " . $platoArray['notas'] . "\n");
  139. }*/
  140. }
  141. }
  142. $printer->text(str_repeat("=", 45) . "\n");
  143. // TOTALES
  144. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  145. $ordenSubtotal = 0;
  146. $ordenDescuento = 0;
  147. $ordenPropina = 0;
  148. if (isset($dataPrint['orden'])) {
  149. $orden = is_object($dataPrint['orden']) ? (array)$dataPrint['orden'] : $dataPrint['orden'];
  150. $ordenSubtotal = isset($orden['subtotal']) ? floatval($orden['subtotal']) : $subtotal;
  151. $ordenDescuento = isset($orden['descuento']) ? floatval($orden['descuento']) : 0;
  152. $ordenPropina = isset($orden['propina']) ? floatval($orden['propina']) : 0;
  153. }
  154. $printer->text("Subtotal: $" . number_format($ordenSubtotal, 2) . "\n");
  155. if ($ordenDescuento > 0) {
  156. $printer->text("Descuento: -$" . number_format($ordenDescuento, 2) . "\n");
  157. }
  158. if ($ordenPropina > 0) {
  159. $printer->text("Propina: $" . number_format($ordenPropina, 2) . "\n");
  160. }
  161. $total = $ordenSubtotal - $ordenDescuento + $ordenPropina;
  162. $printer->text(str_repeat("-", 45) . "\n");
  163. $printer->setEmphasis(true);
  164. $printer->setTextSize(2, 1);
  165. $printer->text("TOTAL: $" . number_format($total, 2) . "\n");
  166. $printer->setTextSize(1, 1);
  167. $printer->setEmphasis(false);
  168. $printer->text(str_repeat("=", 45) . "\n");
  169. $printer->setJustification(Printer::JUSTIFY_CENTER);
  170. $printer->text("\n¡Gracias por su preferencia!\n");
  171. $printer->text("Esta NO es una factura\n");
  172. $printer->feed(3);
  173. $printer->cut();
  174. $printer->close();
  175. echo json_encode([
  176. 'success' => true,
  177. 'status' => 'success',
  178. 'message' => 'Precuenta impresa correctamente en ' . $nombreImpresora
  179. ]);
  180. } catch (Exception $e) {
  181. echo json_encode([
  182. 'success' => false,
  183. 'status' => 'error',
  184. 'message' => 'Error al imprimir: ' . $e->getMessage()
  185. ]);
  186. }
  187. ?>