|
|
@@ -0,0 +1,154 @@
|
|
|
+<?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;
|
|
|
+
|
|
|
+$dataPrint = json_decode($_POST["dataPrint"]);
|
|
|
+
|
|
|
+if($dataPrint){
|
|
|
+ //Printer Name
|
|
|
+ $PRINTER_NAME = $dataPrint->printer ?? 'POS-80C';
|
|
|
+ //CONFIGURACIONES
|
|
|
+ $maxDescripcion = 25; //Maximo de caracteres por linea para la descripcion del producto
|
|
|
+
|
|
|
+ //DATOS
|
|
|
+ $nombreEmpresa = $dataPrint->nombre_empresa;
|
|
|
+ $giro = $dataPrint->giro_empresa;
|
|
|
+ $direccion = $dataPrint->direcion_empresa;
|
|
|
+
|
|
|
+ //$documento = strToUpper($dataPrint->documento) ?? null;
|
|
|
+
|
|
|
+ $fecha = $dataPrint->fecha;
|
|
|
+ $caja = $dataPrint->caja;
|
|
|
+
|
|
|
+ $clienteNombre = $dataPrint->cliente;
|
|
|
+ $clienteDocumento = $dataPrint->clienteDoc ?? null;
|
|
|
+ if($clienteDocumento && $dataPrint->clienteDocNum){
|
|
|
+ $clienteDocumento = $clienteDocumento.": ".$dataPrint->clienteDocNum;
|
|
|
+ }
|
|
|
+
|
|
|
+ $productos = $dataPrint->productos_normal;
|
|
|
+
|
|
|
+ $totalGravado = $dataPrint->totalGrabadas;
|
|
|
+ $totalExento = $dataPrint->totalExento;
|
|
|
+ $totalNoSujeto = $dataPrint->totalNS;
|
|
|
+ $totalFinal = $dataPrint->totales->totalTotal;
|
|
|
+ $totalCambio = $dataPrint->efectivo - $totalFinal;
|
|
|
+
|
|
|
+ $mensaje = "¡Gracias por su compra!";
|
|
|
+
|
|
|
+ $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");
|
|
|
+ //DOCUMENTO
|
|
|
+ if($documento){
|
|
|
+ $printer->setEmphasis(true);
|
|
|
+ $printer->text("$documento\n");
|
|
|
+ $printer->setEmphasis(false);
|
|
|
+ $printer->text(str_repeat("-", 45) . "\n");
|
|
|
+ }
|
|
|
+ //GENERAL
|
|
|
+ $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");
|
|
|
+ //CLIENTE
|
|
|
+ if($clienteNombre && $clienteNombre != ""){
|
|
|
+ $printer->setEmphasis(true);
|
|
|
+ $printer->text("Cliente:\n");
|
|
|
+ $printer->setEmphasis(false);
|
|
|
+ $printer->text("$clienteNombre\n");
|
|
|
+ }
|
|
|
+ if($clienteDocumento){
|
|
|
+ $printer->text("$clienteDocumento\n");
|
|
|
+ }
|
|
|
+ if($clienteNRC){
|
|
|
+ $printer->text("NRC: $clienteNRC\n");
|
|
|
+ }
|
|
|
+ $printer->text("\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("EFECTIVO $" . number_format($dataPrint->efectivo, 2) . "\n");
|
|
|
+ $printer->text("CAMBIO $" . number_format($totalCambio, 2) . "\n");
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $printer->text(str_repeat("_", 45) . "\n");
|
|
|
+
|
|
|
+ $printer->setJustification(Printer::JUSTIFY_CENTER);
|
|
|
+ // DISCLAIMER
|
|
|
+ $lineasDisclaimer = wordwrap($disclaimer, 45, "\n", true);
|
|
|
+ foreach (explode("\n", $lineasDisclaimer) as $linea) {
|
|
|
+ $printer->text($linea . "\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->pulse(); //Abrir gaveta
|
|
|
+ $printer->close();
|
|
|
+
|
|
|
+ echo "Ticket impreso correctamente.";
|
|
|
+
|
|
|
+ } catch (Exception $e) {
|
|
|
+ echo "Error al imprimir: " . $e->getMessage();
|
|
|
+ }
|
|
|
+}
|