| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
- header("Access-Control-Allow-Origin: *");
- header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
- header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
- http_response_code(204);
- exit;
- }
- header("Access-Control-Allow-Origin: *");
- header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
- header("Access-Control-Allow-Headers: Content-Type, Authorization");
- error_reporting(E_ALL);
- ini_set('display_errors', 0);
- ini_set('log_errors', 1);
- ini_set('error_log', __DIR__ . '/logs/error.log');
- require __DIR__ . '/vendor/autoload.php';
- use Mike42\Escpos\Printer;
- use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
- // Helper functions
- function printLinea($printer, $char = "-", $width = 48) {
- $printer->text(str_repeat($char, $width) . "\n");
- }
- function cutString($text, $maxLength) {
- return substr($text, 0, $maxLength);
- }
- function printAlignedText($printer, $leftText, $rightText, $width = 48) {
- $rightLen = strlen($rightText);
- $leftLen = $width - $rightLen;
- $leftPadded = str_pad($leftText, $leftLen, " ", STR_PAD_RIGHT);
- $printer->text($leftPadded . $rightText . "\n");
- }
- function printAlignedTextBold($printer, $leftLabel, $leftValue, $rightLabel, $rightValue, $width = 48) {
- // Imprimir etiqueta izquierda en negrita
- $printer->setEmphasis(true);
- $printer->text($leftLabel);
- $printer->setEmphasis(false);
-
- // Calcular espacios necesarios
- $rightText = $rightLabel . $rightValue;
- $rightLen = strlen($rightText);
- $leftTotal = strlen($leftLabel . $leftValue);
- $spaces = $width - $leftTotal - $rightLen;
-
- // Imprimir valor izquierdo y espacios
- $printer->text($leftValue . str_repeat(" ", $spaces));
-
- // Imprimir etiqueta derecha en negrita
- $printer->setEmphasis(true);
- $printer->text($rightLabel);
- $printer->setEmphasis(false);
- $printer->text($rightValue . "\n");
- }
- function printTextBold($printer, $label, $value) {
- $printer->setEmphasis(true);
- $printer->text($label);
- $printer->setEmphasis(false);
- $printer->text($value . "\n");
- }
- // Get data from POST
- $dataPrint = null;
- if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST["dataPrint"])) {
- $dataPrint = json_decode($_POST["dataPrint"]);
- }
- if ($dataPrint) {
- $PRINTER_NAME = $dataPrint->printer ?? 'POS-80C';
-
- try {
- $connector = new WindowsPrintConnector($PRINTER_NAME);
- $printer = new Printer($connector);
-
- // COMPANY HEADER
- $printer->setJustification(Printer::JUSTIFY_CENTER);
- $printer->setTextSize(2, 2);
- $printer->text(cutString(strtoupper($dataPrint->nombre_empresa), 24) . "\n");
-
- $printer->setTextSize(1, 1);
- //$printer->text(cutString(strtoupper($dataPrint->giro_empresa ?? ''), 48) . "\n");
- //$printer->text(cutString(strtoupper($dataPrint->direcion_empresa), 48) . "\n");
- //$printer->text(cutString(strtoupper($dataPrint->rsocial_empresa), 48) . "\n");
- //$printer->text("NRC: " . $dataPrint->nrc_empresa . " - NIT: " . $dataPrint->nit_empresa . "\n");
-
- printLinea($printer);
-
- // DOCUMENT INFO
- $printer->setJustification(Printer::JUSTIFY_LEFT);
- //$printer->text("Control de Ticket Interno");
- printAlignedTextBold($printer, "Fecha: ", $dataPrint->fecha, "Caja: ", $dataPrint->caja);
- printAlignedTextBold($printer, "Vendedor: ", $dataPrint->vendedor, "Mesa: ", $dataPrint->mesa_orden);
- printTextBold($printer, "Servicio: ", $dataPrint->servicio);
-
- printLinea($printer);
-
- // CUSTOMER INFO
- if (isset($dataPrint->cliente) && $dataPrint->cliente != "") {
- $printer->setJustification(Printer::JUSTIFY_LEFT);
- $printer->text("Cliente: " . $dataPrint->cliente . "\n");
- if (isset($dataPrint->direccion_cliente) && $dataPrint->direccion_cliente != "") {
- $printer->text("Direccion: " . $dataPrint->direccion_cliente . "\n");
- }
- printLinea($printer);
- }
-
- // PRODUCTS TABLE
- $printer->setJustification(Printer::JUSTIFY_LEFT);
- $printer->setEmphasis(true);
- $printer->text("CANT DESCRIPCION PRECIO TOTAL\n");
- printLinea($printer);
- $printer->setEmphasis(false);
-
- foreach ($dataPrint->productos_normal as $producto) {
- $cantidad = str_pad(number_format($producto->cant, 2), 5, " ", STR_PAD_RIGHT);
- $precio = str_pad(number_format($producto->costo, 2), 7, " ", STR_PAD_LEFT);
- $total = str_pad(number_format($producto->costo * $producto->cant, 2), 7, " ", STR_PAD_LEFT);
-
- $descripcion = wordwrap(strtoupper($producto->desc), 23, "\n", true);
- $lineasDescripcion = explode("\n", $descripcion);
-
- $printer->text("$cantidad " . str_pad($lineasDescripcion[0], 23, " ") . " $precio $total\n");
-
- for ($i = 1; $i < count($lineasDescripcion); $i++) {
- $printer->text(" " . $lineasDescripcion[$i] . "\n");
- }
-
- // Imprimir acompañamientos si existen
- if (isset($producto->acompanamientos) && $producto->acompanamientos != "") {
- $printer->text(" CON: " . strtoupper($producto->acompanamientos) . "\n");
- }
-
- // Imprimir notas si existen
- if (isset($producto->notas) && $producto->notas != "") {
- $printer->text(" * " . strtoupper($producto->notas) . "\n");
- }
- }
-
- printLinea($printer);
-
- // TOTALS
- $printer->setJustification(Printer::JUSTIFY_RIGHT);
- $printer->setEmphasis(true);
- $printer->setTextSize(1, 2);
- $printer->text("TOTAL $" . number_format($dataPrint->totales->totalTotal, 2) . "\n");
- $printer->setTextSize(1, 1);
- $printer->setEmphasis(false);
-
- $printer->text("EFECTIVO $" . number_format($dataPrint->efectivo, 2) . "\n");
- $printer->text("CAMBIO $" . number_format($dataPrint->cambio, 2) . "\n");
-
- if (isset($dataPrint->pos) && floatval($dataPrint->pos) > 0) {
- $printer->text("POS $" . number_format($dataPrint->pos, 2) . "\n");
- }
- printLinea($printer);
- // NOTAS DE ORDEN
- if (isset($dataPrint->notas_orden) && $dataPrint->notas_orden != "") {
- $printer->setJustification(Printer::JUSTIFY_LEFT);
- $printer->setEmphasis(true);
- $printer->text("NOTAS ORDEN: \n");
- $printer->setEmphasis(false);
- $notasLines = wordwrap($dataPrint->notas_orden, 48, "\n", true);
- foreach (explode("\n", $notasLines) as $linea) {
- $printer->text($linea . "\n");
- }
- }
-
- printLinea($printer);
- $printer->setJustification(Printer::JUSTIFY_CENTER);
- $printer->text("\n");
- $printer->text("REF.: " . ($dataPrint->referencia) . "\n");
-
- /*
- if (isset($dataPrint->notas) && $dataPrint->notas != "") {
- $printer->setJustification(Printer::JUSTIFY_LEFT);
- $printer->text("Notas: " . $dataPrint->notas . "\n");
- printLinea($printer);
- }
- */
- /*
- $printer->setJustification(Printer::JUSTIFY_CENTER);
- $printer->text("\n");
-
- if (isset($dataPrint->mensaje_ticket) && $dataPrint->mensaje_ticket != "") {
- $mensajeLines = wordwrap($dataPrint->mensaje_ticket, 48, "\n", true);
- foreach (explode("\n", $mensajeLines) as $linea) {
- $printer->text($linea . "\n");
- }
- }
- */
-
- // FINISH
- $printer->feed(2);
- $printer->cut();
- $printer->close();
-
- echo json_encode([
- "status" => "success",
- "message" => "Recibo impreso correctamente."
- ]);
-
- } catch (Exception $e) {
- echo json_encode([
- "status" => "error",
- "message" => "Error al imprimir: " . $e->getMessage()
- ]);
- }
- } else {
- echo json_encode([
- "status" => "error",
- "message" => "No se recibieron datos para imprimir."
- ]);
- }
- ?>
|