precuenta_diaria_caja.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Archivo para imprimir precuentas por rango de fecha (Precuenta Diaria)
  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. $dataPrint = is_array($_POST["dataPrint"]) ? $_POST["dataPrint"] : json_decode(json_encode($_POST["dataPrint"]), true);
  30. }
  31. if(!$dataPrint){
  32. echo json_encode([
  33. 'success' => false,
  34. 'status' => 'error',
  35. 'message' => 'No se recibieron datos para imprimir'
  36. ]);
  37. exit;
  38. }
  39. try {
  40. $nombreImpresora = isset($dataPrint['printer']) ? $dataPrint['printer'] : "POS-80C";
  41. $connector = new WindowsPrintConnector($nombreImpresora);
  42. $printer = new Printer($connector);
  43. // ── ENCABEZADO ────────────────────────────────────────────────
  44. $printer->setJustification(Printer::JUSTIFY_CENTER);
  45. $printer->setTextSize(2, 2);
  46. $printer->text($dataPrint['nombre_empresa'] . "\n");
  47. $printer->setTextSize(1, 1);
  48. if (!empty($dataPrint['direccion_empresa'])) {
  49. $printer->text($dataPrint['direccion_empresa'] . "\n");
  50. }
  51. $printer->text(str_repeat("=", 45) . "\n");
  52. $printer->setEmphasis(true);
  53. $printer->text("PRECUENTA DIARIA\n");
  54. $printer->setEmphasis(false);
  55. // Rango de fechas
  56. if (!empty($dataPrint['subtotal_fecha'])) {
  57. $printer->text("Periodo: " . $dataPrint['subtotal_fecha'] . "\n");
  58. }
  59. $printer->text(str_repeat("=", 45) . "\n");
  60. // ── INFORMACIÓN DE LA ORDEN ───────────────────────────────────
  61. $printer->setJustification(Printer::JUSTIFY_LEFT);
  62. if (isset($dataPrint['orden'])) {
  63. $orden = is_object($dataPrint['orden']) ? (array)$dataPrint['orden'] : $dataPrint['orden'];
  64. if (!empty($orden['num_orden'])) {
  65. $printer->setEmphasis(true);
  66. $printer->text("Orden #: ");
  67. $printer->setEmphasis(false);
  68. $printer->text($orden['num_orden'] . "\n");
  69. }
  70. if (!empty($orden['salon'])) {
  71. $printer->setEmphasis(true);
  72. $printer->text("Salon: ");
  73. $printer->setEmphasis(false);
  74. $printer->text($orden['salon'] . "\n");
  75. }
  76. if (!empty($orden['mesa'])) {
  77. $printer->setEmphasis(true);
  78. $printer->text("Mesa: ");
  79. $printer->setEmphasis(false);
  80. $printer->text($orden['mesa'] . "\n");
  81. }
  82. if (!empty($orden['cliente'])) {
  83. $printer->setEmphasis(true);
  84. $printer->text("Cliente: ");
  85. $printer->setEmphasis(false);
  86. $printer->text($orden['cliente'] . "\n");
  87. }
  88. if (!empty($orden['servicio'])) {
  89. $printer->setEmphasis(true);
  90. $printer->text("Servicio: ");
  91. $printer->setEmphasis(false);
  92. $printer->text($orden['servicio'] . "\n");
  93. }
  94. }
  95. $printer->text(str_repeat("-", 45) . "\n");
  96. // ── ENCABEZADO DE TABLA ───────────────────────────────────────
  97. $printer->setEmphasis(true);
  98. $printer->text("CANT DESCRIPCION TOTAL\n");
  99. $printer->setEmphasis(false);
  100. $printer->text(str_repeat("-", 45) . "\n");
  101. // ── PLATOS ────────────────────────────────────────────────────
  102. $subtotal = 0;
  103. if (isset($dataPrint['platos']) && is_array($dataPrint['platos'])) {
  104. foreach ($dataPrint['platos'] as $plato) {
  105. $platoArray = is_object($plato) ? (array)$plato : $plato;
  106. $cant = isset($platoArray['cant']) ? number_format($platoArray['cant'], 0) : '0';
  107. $nombre = isset($platoArray['nombre']) ? $platoArray['nombre'] : '';
  108. $precio = isset($platoArray['precio']) ? floatval($platoArray['precio']) : 0;
  109. $cantNum = isset($platoArray['cant']) ? floatval($platoArray['cant']) : 0;
  110. $total = $cantNum * $precio;
  111. $subtotal += $total;
  112. $cantPad = str_pad($cant, 4);
  113. $nombreCorto = substr($nombre, 0, 25);
  114. $totalStr = "$" . number_format($total, 2);
  115. $printer->text($cantPad . "x " . str_pad($nombreCorto, 27) . str_pad($totalStr, 10, " ", STR_PAD_LEFT) . "\n");
  116. // Acompañamientos
  117. if (isset($platoArray['acompanamientos']) && is_array($platoArray['acompanamientos']) && count($platoArray['acompanamientos']) > 0) {
  118. foreach ($platoArray['acompanamientos'] as $acomp) {
  119. $acompArray = is_object($acomp) ? (array)$acomp : $acomp;
  120. if (isset($acompArray['acompanamiento'])) {
  121. $printer->text(" + " . $acompArray['acompanamiento'] . "\n");
  122. }
  123. }
  124. }
  125. }
  126. }
  127. $printer->text(str_repeat("=", 45) . "\n");
  128. // ── TOTALES ───────────────────────────────────────────────────
  129. $printer->setJustification(Printer::JUSTIFY_RIGHT);
  130. $ordenSubtotal = $subtotal; // Para precuenta diaria usar suma real de los platos filtrados
  131. $ordenDescuento = 0;
  132. $ordenPropina = 0;
  133. if (isset($dataPrint['orden'])) {
  134. $orden = is_object($dataPrint['orden']) ? (array)$dataPrint['orden'] : $dataPrint['orden'];
  135. $ordenDescuento = isset($orden['descuento']) ? floatval($orden['descuento']) : 0;
  136. $ordenPropina = isset($orden['propina']) ? floatval($orden['propina']) : 0;
  137. }
  138. $printer->text("Subtotal: $" . number_format($ordenSubtotal, 2) . "\n");
  139. if ($ordenDescuento > 0) {
  140. $printer->text("Descuento: -$" . number_format($ordenDescuento, 2) . "\n");
  141. }
  142. if ($ordenPropina > 0) {
  143. $printer->text("Propina: $" . number_format($ordenPropina, 2) . "\n");
  144. }
  145. $totalFinal = $ordenSubtotal - $ordenDescuento + $ordenPropina;
  146. $printer->text(str_repeat("-", 45) . "\n");
  147. $printer->setEmphasis(true);
  148. $printer->setTextSize(2, 1);
  149. $printer->text("TOTAL: $" . number_format($totalFinal, 2) . "\n");
  150. $printer->setTextSize(1, 1);
  151. $printer->setEmphasis(false);
  152. $printer->text(str_repeat("=", 45) . "\n");
  153. $printer->setJustification(Printer::JUSTIFY_CENTER);
  154. $printer->text("\n¡Gracias por su preferencia!\n");
  155. $printer->text("Esta NO es una factura\n");
  156. $printer->feed(3);
  157. $printer->cut();
  158. $printer->close();
  159. echo json_encode([
  160. 'success' => true,
  161. 'status' => 'success',
  162. 'message' => 'Precuenta Diaria impresa correctamente en ' . $nombreImpresora
  163. ]);
  164. } catch (Exception $e) {
  165. echo json_encode([
  166. 'success' => false,
  167. 'status' => 'error',
  168. 'message' => 'Error al imprimir: ' . $e->getMessage()
  169. ]);
  170. }
  171. ?>