recibo.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Archivo para imprimir recibos de venta
  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. $isc = '';
  29. if(isset($_POST["dataPrint"])){
  30. // jQuery envía el objeto como está, necesitamos convertirlo a array
  31. $dataPrint = is_array($_POST["dataPrint"]) ? $_POST["dataPrint"] : json_decode(json_encode($_POST["dataPrint"]), true);
  32. }
  33. if(!$dataPrint){
  34. echo json_encode([
  35. 'success' => false,
  36. 'status' => 'error',
  37. 'message' => 'No se recibieron datos para imprimir'
  38. ]);
  39. exit;
  40. }
  41. try {
  42. // Nombre de la impresora de caja
  43. $nombreImpresora = isset($dataPrint['printer']) ? $dataPrint['printer'] : "POS-80C";
  44. $connector = new WindowsPrintConnector($nombreImpresora);
  45. $printer = new Printer($connector);
  46. // ENCABEZADO
  47. $printer->setJustification(Printer::JUSTIFY_CENTER);
  48. $printer->setTextSize(2, 2);
  49. $printer->text($dataPrint['nombre_empresa'] . "\n");
  50. $printer->setTextSize(1, 1);
  51. if (!empty($dataPrint['direcion_empresa'])) {
  52. $printer->text($dataPrint['direcion_empresa'] . "\n");
  53. }
  54. if (!empty($dataPrint['nit_empresa'])) {
  55. $printer->text("NIT: " . $dataPrint['nit_empresa'] . "\n");
  56. }
  57. if (!empty($dataPrint['nrc_empresa'])) {
  58. $printer->text("NRC: " . $dataPrint['nrc_empresa'] . "\n");
  59. }
  60. $printer->text(str_repeat("=", 45) . "\n");
  61. $printer->setEmphasis(true);
  62. $printer->text("R E C I B O\n");
  63. $printer->setEmphasis(false);
  64. $printer->text(str_repeat("=", 45) . "\n");
  65. // INFORMACIÓN DEL RECIBO
  66. $printer->setJustification(Printer::JUSTIFY_LEFT);
  67. if (!empty($dataPrint['doc_numero'])) {
  68. $printer->setEmphasis(true);
  69. $printer->text("Recibo #: ");
  70. $printer->setEmphasis(false);
  71. $printer->text($dataPrint['doc_numero'] . "\n");
  72. }
  73. if (!empty($dataPrint['fecha'])) {
  74. $printer->setEmphasis(true);
  75. $printer->text("Fecha: ");
  76. $printer->setEmphasis(false);
  77. $printer->text($dataPrint['fecha'] . "\n");
  78. }
  79. if (!empty($dataPrint['referencia'])) {
  80. $printer->setEmphasis(true);
  81. $printer->text("Referencia: ");
  82. $printer->setEmphasis(false);
  83. $printer->text($dataPrint['referencia'] . "\n");
  84. }
  85. if (!empty($dataPrint['cliente'])) {
  86. $printer->setEmphasis(true);
  87. $printer->text("Cliente: ");
  88. $printer->setEmphasis(false);
  89. $printer->text($dataPrint['cliente'] . "\n");
  90. }
  91. if (!empty($dataPrint['vendedor'])) {
  92. $printer->setEmphasis(true);
  93. $printer->text("Vendedor: ");
  94. $printer->setEmphasis(false);
  95. $printer->text($dataPrint['vendedor'] . "\n");
  96. }
  97. if (!empty($dataPrint['caja'])) {
  98. $printer->setEmphasis(true);
  99. $printer->text("Caja: ");
  100. $printer->setEmphasis(false);
  101. $printer->text($dataPrint['caja'] . "\n");
  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. // PRODUCTOS
  110. $subtotal = 0;
  111. if (isset($dataPrint['productos_normal']) && is_array($dataPrint['productos_normal'])) {
  112. foreach ($dataPrint['productos_normal'] as $producto) {
  113. // Convertir objeto a array si es necesario
  114. $productoArray = is_object($producto) ? (array)$producto : $producto;
  115. $cant = isset($productoArray['cant']) ? number_format($productoArray['cant'], 0) : '0';
  116. $desc = isset($productoArray['desc']) ? $productoArray['desc'] : '';
  117. $costo = isset($productoArray['costo']) ? floatval($productoArray['costo']) : 0;
  118. $cantNum = isset($productoArray['cant']) ? floatval($productoArray['cant']) : 0;
  119. $total = $cantNum * $costo;
  120. $subtotal += $total;
  121. // Línea del producto - cant y descripción
  122. $cantPad = str_pad($cant, 4);
  123. $descCorto = substr($desc, 0, 25);
  124. $totalStr = "$" . number_format($total, 2);
  125. $printer->text($cantPad . "x " . str_pad($descCorto, 27) . str_pad($totalStr, 10, " ", STR_PAD_LEFT) . "\n");
  126. }
  127. }
  128. $printer->text(str_repeat("=", 45) . "\n");
  129. // TOTALES
  130. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  131. $totales = isset($dataPrint['totales']) && is_array($dataPrint['totales']) ? $dataPrint['totales'] : [];
  132. $totalGrabadas = isset($totales['totalGrabadas']) ? floatval(str_replace(',', '', $totales['totalGrabadas'])) : $subtotal;
  133. $descuento = isset($totales['descuento']) ? floatval(str_replace(',', '', $totales['descuento'])) : 0;
  134. $propina = isset($totales['propina']) ? floatval(str_replace(',', '', $totales['propina'])) : 0;
  135. $totalTotal = isset($totales['totalTotal']) ? floatval(str_replace(',', '', $totales['totalTotal'])) : 0;
  136. $printer->text("Subtotal: $" . number_format($totalGrabadas, 2) . "\n");
  137. if ($descuento > 0) {
  138. $printer->text("Descuento: -$" . number_format($descuento, 2) . "\n");
  139. }
  140. if ($propina > 0) {
  141. $printer->text("Propina: $" . number_format($propina, 2) . "\n");
  142. }
  143. // FORMA DE PAGO
  144. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  145. $efectivo = isset($dataPrint['efectivo']) ? floatval($dataPrint['efectivo']) : 0;
  146. $pos = isset($dataPrint['pos']) ? floatval($dataPrint['pos']) : 0;
  147. $cambio = isset($dataPrint['cambio']) ? floatval($dataPrint['cambio']) : 0;
  148. if ($efectivo > 0) {
  149. $printer->text("Efectivo: $" . number_format($efectivo, 2) . "\n");
  150. }
  151. if ($pos > 0) {
  152. $printer->text("POS/Tarjeta: $" . number_format($pos, 2) . "\n");
  153. }
  154. if ($cambio > 0) {
  155. $printer->text("Cambio: $" . number_format($cambio, 2) . "\n");
  156. }
  157. $printer->text(str_repeat("-", 45) . "\n");
  158. $printer->setEmphasis(true);
  159. $printer->setTextSize(2, 1);
  160. $printer->text("TOTAL: $" . number_format($totalTotal, 2) . "\n");
  161. $printer->setTextSize(1, 1);
  162. $printer->setEmphasis(false);
  163. $printer->text(str_repeat("=", 45) . "\n");
  164. // MENSAJE FINAL
  165. $printer->setJustification(Printer::JUSTIFY_CENTER);
  166. if (!empty($dataPrint['mensaje_ticket'])) {
  167. $printer->text($dataPrint['mensaje_ticket'] . "\n");
  168. }
  169. $printer->text("\n¡Gracias por su compra!\n");
  170. $printer->text("Este es un recibo, NO factura\n");
  171. $printer->feed(3);
  172. $printer->cut();
  173. $printer->close();
  174. echo json_encode([
  175. 'success' => true,
  176. 'status' => 'success',
  177. 'message' => 'Recibo impreso 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. ?>