| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- header("Access-Control-Allow-Origin: *");
- require __DIR__ . '/vendor/autoload.php';
- use Mike42\Escpos\Printer;
- use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
- use Mike42\Escpos\EscposImage;
- use Mike42\Escpos\PrintConnectors\FilePrintConnector;
- //Printer Name
- const PRINTER_NAME = 'POS-80C';
- $dataPrint = json_decode($_POST["dataPrint"]);
- if($dataPrint){
- //CONFIGURACIONES
- $maxDescripcion = 25; //Maximo de caracteres por linea para la descripcion del producto
- $hashEmpresa = "d1ac201a29655ce75bd01772528a6595dd41f9f7";
- //DATOS
- $nombreEmpresa = $dataPrint->nombre_empresa;
- $razonSocial = $dataPrint->rsocial_empresa;
- $giro = $dataPrint->giro_empresa;
- $documentoDueno = "NRC: ".$dataPrint->nrc_empresa." | NIT: ".$dataPrint->nit_empresa;
- $direccion = $dataPrint->direcion_empresa;
- $fecha = $dataPrint->fecha;
- $caja = $dataPrint->caja;
- $clienteNombre = $dataPrint->cliente;
- $productos = $dataPrint->productos_normal;
- $totalGravado = $dataPrint->totalGrabadas;
- $totalExento = $dataPrint->totalExento;
- $totalNoSujeto = $dataPrint->totalNS;
- $totalFinal = $dataPrint->totales->totalTotal;
- $dteNControl = $dataPrint->dte_numero_control;
- $dteCodGen = $dataPrint->dte_codigo_generacion;
- $disclaimer = "Este comprobante no es un documento fiscal. Su DTE será enviado al correo electrónico proporcionado.";
- $qrMessage = "También puedes escanear el código QR para ver y descargar tu factura electrónica en línea.";
- $mensaje = "¡Gracias por su compra!";
- $urlQR = "https://consultadte.factuexpress.com.sv?h=$hashEmpresa&dte=.$dteNControl";
- $numeroReferencia = $dataPrint->referencia;
- try {
- $connector = new WindowsPrintConnector(PRINTER_NAME);
- $printer = new Printer($connector);
- // ENCABEZADO
- $printer->setJustification(Printer::JUSTIFY_CENTER);
- $printer->setTextSize(2, 2);
- $printer->text("$nombreEmpresa\n");
- $printer->setTextSize(1, 1);
- $printer->text("$razonSocial\n");
- $printer->text("$giro\n");
- $printer->text("$documentoDueno\n");
- $printer->text("$direccion\n");
- $printer->text(str_repeat("-", 45) . "\n");
- //CLIENTE
- $printer->setJustification(Printer::JUSTIFY_LEFT);
- $printer->setEmphasis(true);
- $printer->text("Fecha: ");
- $printer->setEmphasis(false);
- $printer->text("$fecha");
- $printer->setEmphasis(true);
- $printer->text(" Caja: ");
- $printer->setEmphasis(false);
- $printer->text("$caja\n");
- $printer->setEmphasis(true);
- $printer->text("Cliente:\n");
- $printer->setEmphasis(false);
- $printer->text("$clienteNombre\n");
- //PRODUCTOS
- $printer->setEmphasis(true);
- $printer->text("Cant Descripcion Precio Total\n");
- $printer->text(str_repeat("-", 45) . "\n");
- $printer->setEmphasis(false);
-
- foreach ($productos as $producto) {
- $totalUnitario = $producto->cant * $producto->costo;
- $cantidad = str_pad(number_format($producto->cant, 2), 4, " ", STR_PAD_RIGHT);
- $precio = str_pad(number_format($producto->costo, 2), 7, " ", STR_PAD_LEFT);
- $total = str_pad(number_format($totalUnitario, 2), 7, " ", STR_PAD_LEFT);
- // Dividir la descripción en líneas
- $descripcion = wordwrap($producto->sku.' '.$producto->desc, $maxDescripcion, "\n", true);
- $lineasDescripcion = explode("\n", $descripcion);
- // Imprimir primera línea con cantidad, precio y total
- $printer->text("$cantidad " . str_pad($lineasDescripcion[0], $maxDescripcion, " ") . " $precio $total\n");
- // Imprimir líneas adicionales de la descripción (si hay más)
- for ($i = 1; $i < count($lineasDescripcion); $i++) {
- $printer->text(" " . str_pad($lineasDescripcion[$i], $maxDescripcion) . "\n");
- }
- }
-
- $printer->text(str_repeat("-", 45) . "\n");
-
- // TOTALES
- $printer->setJustification(Printer::JUSTIFY_RIGHT);
- $printer->setEmphasis(true);
- $printer->setTextSize(2, 1);
- $printer->text("TOTAL $" . number_format($totalFinal, 2) . "\n");
- $printer->setTextSize(1, 1);
- $printer->setEmphasis(false);
- $printer->text(str_repeat("_", 45) . "\n");
- //DTE
- $printer->setJustification(Printer::JUSTIFY_LEFT);
- $printer->text("NUMERO DE CONTROL: \n$dteNControl\n");
- $printer->text("CÓDIGO DE GENERACION: \n$dteCodGen\n\n");
- $printer->setJustification(Printer::JUSTIFY_CENTER);
- // DISCLAIMER
- $lineasDisclaimer = wordwrap($disclaimer, 45, "\n", true);
- foreach (explode("\n", $lineasDisclaimer) as $linea) {
- $printer->text($linea . "\n");
- }
-
- // CÓDIGO QR
- //$printer->text("$qrMessage\n");
- //$printer->qrCode($urlQR, Printer::QR_ECLEVEL_L, 6);
- $printer->text("\n$mensaje\n");
- // REFERENCIA
- $printer->text("\nRef: $numeroReferencia\n");
- $printer->setBarcodeHeight(40);
- $printer->setBarcodeWidth(4);
- $printer->barcode($numeroReferencia, Printer::BARCODE_CODE39);
- // CORTAR PAPEL Y CERRAR IMPRESORA
- $printer->feed(2);
- $printer->cut();
- $printer->close();
- echo "Ticket impreso correctamente.";
- } catch (Exception $e) {
- echo "Error al imprimir: " . $e->getMessage();
- }
- }
|