false, 'status' => 'error', 'message' => 'No se recibieron datos para imprimir' ]); exit; } try { // Nombre de la impresora de caja $nombreImpresora = isset($dataPrint['printer']) ? $dataPrint['printer'] : "POS-80C"; $connector = new WindowsPrintConnector($nombreImpresora); $printer = new Printer($connector); // ENCABEZADO $printer->setJustification(Printer::JUSTIFY_CENTER); $printer->setTextSize(2, 2); $printer->text($dataPrint['nombre_empresa'] . "\n"); $printer->setTextSize(1, 1); if (!empty($dataPrint['direcion_empresa'])) { $printer->text($dataPrint['direcion_empresa'] . "\n"); } if (!empty($dataPrint['nit_empresa'])) { $printer->text("NIT: " . $dataPrint['nit_empresa'] . "\n"); } if (!empty($dataPrint['nrc_empresa'])) { $printer->text("NRC: " . $dataPrint['nrc_empresa'] . "\n"); } $printer->text(str_repeat("=", 45) . "\n"); $printer->setEmphasis(true); $printer->text("R E C I B O\n"); $printer->setEmphasis(false); $printer->text(str_repeat("=", 45) . "\n"); // INFORMACIÓN DEL RECIBO $printer->setJustification(Printer::JUSTIFY_LEFT); if (!empty($dataPrint['doc_numero'])) { $printer->setEmphasis(true); $printer->text("Recibo #: "); $printer->setEmphasis(false); $printer->text($dataPrint['doc_numero'] . "\n"); } if (!empty($dataPrint['fecha'])) { $printer->setEmphasis(true); $printer->text("Fecha: "); $printer->setEmphasis(false); $printer->text($dataPrint['fecha'] . "\n"); } if (!empty($dataPrint['referencia'])) { $printer->setEmphasis(true); $printer->text("Referencia: "); $printer->setEmphasis(false); $printer->text($dataPrint['referencia'] . "\n"); } if (!empty($dataPrint['cliente'])) { $printer->setEmphasis(true); $printer->text("Cliente: "); $printer->setEmphasis(false); $printer->text($dataPrint['cliente'] . "\n"); } if (!empty($dataPrint['vendedor'])) { $printer->setEmphasis(true); $printer->text("Vendedor: "); $printer->setEmphasis(false); $printer->text($dataPrint['vendedor'] . "\n"); } if (!empty($dataPrint['caja'])) { $printer->setEmphasis(true); $printer->text("Caja: "); $printer->setEmphasis(false); $printer->text($dataPrint['caja'] . "\n"); } $printer->text(str_repeat("-", 45) . "\n"); // ENCABEZADO DE TABLA $printer->setEmphasis(true); $printer->text("CANT DESCRIPCION TOTAL\n"); $printer->setEmphasis(false); $printer->text(str_repeat("-", 45) . "\n"); // PRODUCTOS $subtotal = 0; if (isset($dataPrint['productos_normal']) && is_array($dataPrint['productos_normal'])) { foreach ($dataPrint['productos_normal'] as $producto) { // Convertir objeto a array si es necesario $productoArray = is_object($producto) ? (array)$producto : $producto; $cant = isset($productoArray['cant']) ? number_format($productoArray['cant'], 0) : '0'; $desc = isset($productoArray['desc']) ? $productoArray['desc'] : ''; $costo = isset($productoArray['costo']) ? floatval($productoArray['costo']) : 0; $cantNum = isset($productoArray['cant']) ? floatval($productoArray['cant']) : 0; $total = $cantNum * $costo; $subtotal += $total; // Línea del producto - cant y descripción $cantPad = str_pad($cant, 4); $descCorto = substr($desc, 0, 25); $totalStr = "$" . number_format($total, 2); $printer->text($cantPad . "x " . str_pad($descCorto, 27) . str_pad($totalStr, 10, " ", STR_PAD_LEFT) . "\n"); } } $printer->text(str_repeat("=", 45) . "\n"); // TOTALES $printer->setJustification(Printer::JUSTIFY_RIGHT); $totales = isset($dataPrint['totales']) && is_array($dataPrint['totales']) ? $dataPrint['totales'] : []; $totalGrabadas = isset($totales['totalGrabadas']) ? floatval(str_replace(',', '', $totales['totalGrabadas'])) : $subtotal; $descuento = isset($totales['descuento']) ? floatval(str_replace(',', '', $totales['descuento'])) : 0; $propina = isset($totales['propina']) ? floatval(str_replace(',', '', $totales['propina'])) : 0; $totalTotal = isset($totales['totalTotal']) ? floatval(str_replace(',', '', $totales['totalTotal'])) : 0; $printer->text("Subtotal: $" . number_format($totalGrabadas, 2) . "\n"); if ($descuento > 0) { $printer->text("Descuento: -$" . number_format($descuento, 2) . "\n"); } if ($propina > 0) { $printer->text("Propina: $" . number_format($propina, 2) . "\n"); } // FORMA DE PAGO $printer->setJustification(Printer::JUSTIFY_RIGHT); $efectivo = isset($dataPrint['efectivo']) ? floatval($dataPrint['efectivo']) : 0; $pos = isset($dataPrint['pos']) ? floatval($dataPrint['pos']) : 0; $cambio = isset($dataPrint['cambio']) ? floatval($dataPrint['cambio']) : 0; if ($efectivo > 0) { $printer->text("Efectivo: $" . number_format($efectivo, 2) . "\n"); } if ($pos > 0) { $printer->text("POS/Tarjeta: $" . number_format($pos, 2) . "\n"); } if ($cambio > 0) { $printer->text("Cambio: $" . number_format($cambio, 2) . "\n"); } $printer->text(str_repeat("-", 45) . "\n"); $printer->setEmphasis(true); $printer->setTextSize(2, 1); $printer->text("TOTAL: $" . number_format($totalTotal, 2) . "\n"); $printer->setTextSize(1, 1); $printer->setEmphasis(false); $printer->text(str_repeat("=", 45) . "\n"); // MENSAJE FINAL $printer->setJustification(Printer::JUSTIFY_CENTER); if (!empty($dataPrint['mensaje_ticket'])) { $printer->text($dataPrint['mensaje_ticket'] . "\n"); } $printer->text("\n¡Gracias por su compra!\n"); $printer->text("Este es un recibo, NO factura\n"); $printer->feed(3); $printer->cut(); $printer->close(); echo json_encode([ 'success' => true, 'status' => 'success', 'message' => 'Recibo impreso correctamente en ' . $nombreImpresora ]); } catch (Exception $e) { echo json_encode([ 'success' => false, 'status' => 'error', 'message' => 'Error al imprimir: ' . $e->getMessage() ]); } ?>