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); $printer->text($dataPrint['direccion_empresa'] . "\n"); $printer->text(str_repeat("=", 45) . "\n"); $printer->setEmphasis(true); $printer->text("PRECUENTA\n"); $printer->setEmphasis(false); $printer->text(str_repeat("=", 45) . "\n"); // INFORMACIÓN DE LA ORDEN $printer->setJustification(Printer::JUSTIFY_LEFT); if (isset($dataPrint['orden'])) { $orden = is_object($dataPrint['orden']) ? (array)$dataPrint['orden'] : $dataPrint['orden']; if (!empty($orden['num_orden'])) { $printer->setEmphasis(true); $printer->text("Orden #: "); $printer->setEmphasis(false); $printer->text($orden['num_orden'] . "\n"); } if (!empty($orden['fecha'])) { $printer->setEmphasis(true); $printer->text("Fecha: "); $printer->setEmphasis(false); $printer->text(date('d/m/Y H:i', strtotime($orden['fecha'])) . "\n"); } if (!empty($orden['salon'])) { $printer->setEmphasis(true); $printer->text("Salon: "); $printer->setEmphasis(false); $printer->text($orden['salon'] . "\n"); } if (!empty($orden['mesa'])) { $printer->setEmphasis(true); $printer->text("Mesa: "); $printer->setEmphasis(false); $printer->text($orden['mesa'] . "\n"); } /* if (!empty($orden['mesero'])) { $printer->setEmphasis(true); $printer->text("Mesero: "); $printer->setEmphasis(false); $printer->text($orden['mesero'] . "\n"); }*/ if (!empty($orden['cliente'])) { $printer->setEmphasis(true); $printer->text("Cliente: "); $printer->setEmphasis(false); $printer->text($orden['cliente'] . "\n"); } if (!empty($orden['servicio'])) { $printer->setEmphasis(true); $printer->text("Servicio: "); $printer->setEmphasis(false); $printer->text($orden['servicio'] . "\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"); // PLATOS $subtotal = 0; if (isset($dataPrint['platos']) && is_array($dataPrint['platos'])) { foreach ($dataPrint['platos'] as $plato) { // Convertir objeto a array si es necesario $platoArray = is_object($plato) ? (array)$plato : $plato; $cant = isset($platoArray['cant']) ? number_format($platoArray['cant'], 0) : '0'; $nombre = isset($platoArray['nombre']) ? $platoArray['nombre'] : ''; $precio = isset($platoArray['precio']) ? floatval($platoArray['precio']) : 0; $cantNum = isset($platoArray['cant']) ? floatval($platoArray['cant']) : 0; $total = $cantNum * $precio; $subtotal += $total; // Línea del plato - cant y nombre $cantPad = str_pad($cant, 4); $nombreCorto = substr($nombre, 0, 25); $totalStr = "$" . number_format($total, 2); $printer->text($cantPad . "x " . str_pad($nombreCorto, 27) . str_pad($totalStr, 10, " ", STR_PAD_LEFT) . "\n"); // Acompañamientos if (isset($platoArray['acompanamientos']) && is_array($platoArray['acompanamientos']) && count($platoArray['acompanamientos']) > 0) { foreach ($platoArray['acompanamientos'] as $acomp) { $acompArray = is_object($acomp) ? (array)$acomp : $acomp; if (isset($acompArray['acompanamiento'])) { $printer->text(" + " . $acompArray['acompanamiento'] . "\n"); } } } /* if (!empty($platoArray['notas'])) { $printer->text(" Nota: " . $platoArray['notas'] . "\n"); }*/ } } $printer->text(str_repeat("=", 45) . "\n"); // TOTALES $printer->setJustification(Printer::JUSTIFY_RIGHT); $ordenSubtotal = 0; $ordenDescuento = 0; $ordenPropina = 0; if (isset($dataPrint['orden'])) { $orden = is_object($dataPrint['orden']) ? (array)$dataPrint['orden'] : $dataPrint['orden']; $ordenSubtotal = isset($orden['subtotal']) ? floatval($orden['subtotal']) : $subtotal; $ordenDescuento = isset($orden['descuento']) ? floatval($orden['descuento']) : 0; $ordenPropina = isset($orden['propina']) ? floatval($orden['propina']) : 0; } $printer->text("Subtotal: $" . number_format($ordenSubtotal, 2) . "\n"); if ($ordenDescuento > 0) { $printer->text("Descuento: -$" . number_format($ordenDescuento, 2) . "\n"); } if ($ordenPropina > 0) { $printer->text("Propina: $" . number_format($ordenPropina, 2) . "\n"); } $total = $ordenSubtotal - $ordenDescuento + $ordenPropina; $printer->text(str_repeat("-", 45) . "\n"); $printer->setEmphasis(true); $printer->setTextSize(2, 1); $printer->text("TOTAL: $" . number_format($total, 2) . "\n"); $printer->setTextSize(1, 1); $printer->setEmphasis(false); $printer->text(str_repeat("=", 45) . "\n"); $printer->setJustification(Printer::JUSTIFY_CENTER); $printer->text("\n¡Gracias por su preferencia!\n"); $printer->text("Esta NO es una factura\n"); $printer->feed(3); $printer->cut(); $printer->close(); echo json_encode([ 'success' => true, 'status' => 'success', 'message' => 'Precuenta impresa correctamente en ' . $nombreImpresora ]); } catch (Exception $e) { echo json_encode([ 'success' => false, 'status' => 'error', 'message' => 'Error al imprimir: ' . $e->getMessage() ]); } ?>