<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

error_reporting(E_ERROR | E_PARSE);

$printer = $_POST['printer'];
$json = $_POST['json'];
$impconfig = 0;
$_POST['impconfig'];

$total = 0.00;

//$json = '{"serie":"","fecha":"2017-07-27 09:13:00","caja":"","control":"","usuario":"","inicioturno":"27/7/2017  9:5:26","finturno":"27/7/2017  9:12:23","numerofactini":293,"numerofactfin":293,"totalfact":1,"totaldev":1,"totalfactaptos":0,"facturas":"117.50","notacred":0,"ventaneta":117.5,"impuestos":"4.90","ventascontado":"122.40","ventascredito":0,"descuentos":"-7.50","aperturacaja":500,"montoentrada":0,"montosalida":0,"diferencia":0,"abonoctes":0,"abonoaptos":0,"abonocert":0,"manejoservicio":0,"totalabono":0,"dinerotturno":622.4,"dinerogaveta":600,"depositorealizar":122.4,"formapago":[{"ind":"EF","ventas":-1.3999999999999986,"pagos":"0.00","es":"0.00","total":-1.3999999999999986},{"ind":"CH","ventas":40,"pagos":"0.00","es":"0.00","total":40},{"ind":"TD","ventas":60,"pagos":"0.00","es":"0.00","total":60},{"ind":"EC","ventas":167.6,"pagos":"0.00","es":"0.00","total":167.6},{"ind":"OT","ventas":0,"pagos":"0.00","es":"0.00","total":0}],"bases":[{"ind":"E","base":47.5,"imp":0,"total":47.5},{"ind":"7.0","base":70,"imp":4.9,"total":74.9},{"ind":"10.0","base":0,"imp":0,"total":0},{"ind":"15.0","base":0,"imp":0,"total":0}],"formaventas":[{"tipo":"Debito","forma":"Manual","monto":60},{"tipo":"Credito","forma":"Manual","monto":167.6},{"tipo":"Cheque","forma":"Manual","monto":40}]}';

$obj = json_decode($json);
$formapago = $obj->{'formapago'};
$bases = $obj->{'bases'};
$formaventas = $obj->{'formaventas'};

$serie = $obj->{'serie'};
$fecha = $obj->{'fecha'};
$caja = $obj->{'caja'};
$nombreempresa = $obj->{'nombreempresa'};
$emp_idfiscal = $obj->{'emp_idfiscal'};
$control = $obj->{'control'};
$usuario = $obj->{'usuario'};
$reporte_x = $obj->{'reporte_x'};
$tipo = $obj->{'tipo'};

$inicioturno = $obj->{'inicioturno'};
$finturno = $obj->{'finturno'};
$numerofactini = $obj->{'numerofactini'};
$numerofactfin = $obj->{'numerofactfin'};

$totalfact = $obj->{'totalfact'};
$totaldev = $obj->{'totaldev'};
$totalfactaptos = $obj->{'totalfactaptos'};

$facturas = number_format($obj->{'facturas'}, 2, '.', ',');
$notacred = number_format($obj->{'notacred'}, 2, '.', ',');
$ventaneta = number_format($obj->{'ventaneta'}, 2, '.', ',');
$impuestos = number_format($obj->{'impuestos'}, 2, '.', ',');
$totalventas = number_format($obj->{'totalventas'}, 2, '.', ',');
$ventascontado = number_format($obj->{'ventascontado'}, 2, '.', ',');
$ventascredito = number_format($obj->{'ventascredito'}, 2, '.', ',');
$descuentos = number_format($obj->{'descuentos'}, 2, '.', ',');
$descuentoslibre = number_format($obj->{'descuentoslibre'}, 2, '.', ',');
$aperturacaja = number_format($obj->{'aperturacaja'}, 2, '.', ',');
$montoentrada = number_format($obj->{'montoentrada'}, 2, '.', ',');
$montosalida = number_format($obj->{'montosalida'}, 2, '.', ',');
$diferencia = number_format($obj->{'diferencia'}, 2, '.', ',');
$abonoctes = number_format($obj->{'abonoctes'}, 2, '.', ',');
$abonoaptos = number_format($obj->{'abonoaptos'}, 2, '.', ',');
$abonocert = number_format($obj->{'abonocert'}, 2, '.', ',');
$manejoservicio = number_format($obj->{'manejoservicio'}, 2, '.', ',');
$totalabono = $abonoctes + $abonoaptos + $abonocert + $manejoservicio;
$totalabono = number_format($totalabono, 2, '.', ',');
$dinerotturno = number_format($obj->{'dinerotturno'}, 2, '.', ',');
$dinerogaveta = number_format($obj->{'dinerogaveta'}, 2, '.', ',');
$depositorealizar = number_format($obj->{'depositorealizar'}, 2, '.', ',');
$diferenciacajero = $obj->{'dinerogaveta'}-$obj->{'dinerotturno'};
$diferenciacajero = number_format($diferenciacajero, 2, '.', ',');

if ($impconfig == 1) {
    $pos = strpos($printer, ".");

    if ($pos !== false) {
        $handle = @fsockopen($printer, 9100);
    } else {
        $handle = @fopen($printer, "w");

    }

    @fwrite($handle, chr(27) . chr(64));
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro
    @fwrite($handle, $nombreempresa);
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
    @fwrite($handle, "R.U.C.:  " . $emp_idfiscal);
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda

    @fwrite($handle, str_pad("SERIE#:", 10, " ", STR_PAD_RIGHT) . str_pad($serie, 20, " ", STR_PAD_RIGHT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Fecha:", 10, " ", STR_PAD_RIGHT) . str_pad($fecha, 20, " ", STR_PAD_RIGHT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Caja:", 10, " ", STR_PAD_RIGHT) . str_pad($caja, 20, " ", STR_PAD_RIGHT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Control #:", 10, " ", STR_PAD_RIGHT) . str_pad($control, 20, " ", STR_PAD_RIGHT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Usuario:", 10, " ", STR_PAD_RIGHT) . str_pad($usuario, 20, " ", STR_PAD_RIGHT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro

    if ($tipo == 'z') {
        @fwrite($handle, "REPORTE Z #: " . $reporte_x);
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
        @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
        @fwrite($handle, str_pad("Inicio: ", 22, " ", STR_PAD_RIGHT) . str_pad($inicioturno, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Fin de: ", 22, " ", STR_PAD_RIGHT) . str_pad($finturno, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

        @fwrite($handle, str_pad("Num. fact. inicio:", 22, " ", STR_PAD_RIGHT) . str_pad($numerofactini, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Num. fact. final:", 22, " ", STR_PAD_RIGHT) . str_pad($numerofactfin, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Tot Fact. emitidas:", 22, " ", STR_PAD_RIGHT) . str_pad($totalfact, 20, " ", STR_PAD_LEFT));

    } else {
        @fwrite($handle, "REPORTE X #: " . $reporte_x);
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
        @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
        @fwrite($handle, str_pad("Inicio turno:", 22, " ", STR_PAD_RIGHT) . str_pad($inicioturno, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Fin de turno:", 22, " ", STR_PAD_RIGHT) . str_pad($finturno, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

        @fwrite($handle, str_pad("Numero fact. inicio:", 22, " ", STR_PAD_RIGHT) . str_pad($numerofactini, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Numero fact. final:", 22, " ", STR_PAD_RIGHT) . str_pad($numerofactfin, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

        @fwrite($handle, str_pad("Total Facturas:", 22, " ", STR_PAD_RIGHT) . str_pad($totalfact, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Total Devoluciones:", 22, " ", STR_PAD_RIGHT) . str_pad($totaldev, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Total Facturas Aptos:", 22, " ", STR_PAD_RIGHT) . str_pad($totalfactaptos, 20, " ", STR_PAD_LEFT));

    }

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro

    @fwrite($handle, "DETALLE DEL CIERRE");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "------------------------------------------");
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
    @fwrite($handle, str_pad("=FACTURAS:", 22, " ", STR_PAD_RIGHT) . str_pad($facturas, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("-NOTAS DE CREDITO:", 22, " ", STR_PAD_RIGHT) . str_pad($notacred, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, str_pad("=VENTA NETA:", 22, " ", STR_PAD_RIGHT) . str_pad($ventaneta, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, str_pad("+IMPUESTOS:", 22, " ", STR_PAD_RIGHT) . str_pad($impuestos, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "==========================================");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, str_pad("TOTAL DE VENTAS:", 22, " ", STR_PAD_RIGHT) . str_pad($totalventas, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

    @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, str_pad("Ventas de contado:", 22, " ", STR_PAD_RIGHT) . str_pad($ventascontado, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Ventas a credito:", 22, " ", STR_PAD_RIGHT) . str_pad($ventascredito, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Descuentos:", 22, " ", STR_PAD_RIGHT) . str_pad($descuentos, 20, " ", STR_PAD_LEFT));

    if ($tipo != 'z') {
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
        @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro
        @fwrite($handle, "Desglose de entradas/salidas");
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
        @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
        @fwrite($handle, "------------------------------------------");
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Apertura de Caja:", 22, " ", STR_PAD_RIGHT) . str_pad($aperturacaja, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Monto por entradas:", 22, " ", STR_PAD_RIGHT) . str_pad($montoentrada, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Monto por salidas:", 22, " ", STR_PAD_RIGHT) . str_pad($montosalida, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, "==========================================");
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
        @fwrite($handle, str_pad("=Diferencia:", 22, " ", STR_PAD_RIGHT) . str_pad($diferencia, 20, " ", STR_PAD_LEFT));
    }

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro

    @fwrite($handle, "Recibo de Dinero");

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, "------------------------------------------");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
    @fwrite($handle, str_pad("*Abonos ctes:", 22, " ", STR_PAD_RIGHT) . str_pad($abonoctes, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("+Abonos aptoss:", 22, " ", STR_PAD_RIGHT) . str_pad($abonoaptos, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("+Abonos cert:", 22, " ", STR_PAD_RIGHT) . str_pad($abonocert, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("+Manejo/Servicio:", 22, " ", STR_PAD_RIGHT) . str_pad($manejoservicio, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "==========================================");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, str_pad("=Total Pagos/Abono:", 22, " ", STR_PAD_RIGHT) . str_pad($totalabono, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro

    @fwrite($handle, "Formas de Pago (CAJA)");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "------------------------------------------");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Ventas", 10, " ", STR_PAD_LEFT) . str_pad("Pagos", 10, " ", STR_PAD_LEFT) . str_pad("E/S", 10, " ", STR_PAD_LEFT) . str_pad("Total", 10, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal

    foreach ($formapago as $valor) {

        $ventas = $valor->{'ventas'};
        $pagos = $valor->{'pagos'};
        $es = $valor->{'es'};
        $total = $valor->{'total'};
        $ind = $valor->{'ind'};

        @fwrite($handle, str_pad($ind . ".", 0, " ", STR_PAD_LEFT) . str_pad(number_format($valor->{'ventas'}, 2, '.', ','), 8, " ", STR_PAD_LEFT) . str_pad(number_format($valor->{'pagos'}, 2, '.', ','), 10, " ", STR_PAD_LEFT) . str_pad(number_format($valor->{'es'}, 2, '.', ','), 10, " ", STR_PAD_LEFT) . str_pad(number_format($valor->{'total'}, 2, '.', ','), 10, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

        $totalVentasP = $totalVentasP + $ventas;
        $totalpagos = $totalpagos + $pagos;
        $totales = $totales + $es;
        $totalt = $totalt + $total;

    }

    @fwrite($handle, "==========================================");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, str_pad("   ", 0, " ", STR_PAD_LEFT) . str_pad(number_format($totalVentasP, 2, '.', ','), 8, " ", STR_PAD_LEFT) . str_pad(number_format($totalpagos, 2, '.', ','), 10, " ", STR_PAD_LEFT) . str_pad(number_format($totales, 2, '.', ','), 10, " ", STR_PAD_LEFT) . str_pad(number_format($totalt, 2, '.', ','), 10, " ", STR_PAD_LEFT));

    if ($tipo != 'z') {
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
        @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro
        @fwrite($handle, "Arqueo de caja:");
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, "------------------------------------------");
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
        @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
        @fwrite($handle, str_pad("Dinero t. turno:", 22, " ", STR_PAD_RIGHT) . str_pad($dinerotturno, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Dinero t. gaveta:", 22, " ", STR_PAD_RIGHT) . str_pad($dinerogaveta, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, "==========================================");
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Diferencia cajero:", 22, " ", STR_PAD_RIGHT) . str_pad($diferenciacajero, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, str_pad("Depósito a realizar:", 22, " ", STR_PAD_RIGHT) . str_pad($depositorealizar, 20, " ", STR_PAD_LEFT));

    }

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro
    @fwrite($handle, "Resumen de impuestos.");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "(incluye facturas de credito)");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, str_pad("T.%", 5, " ", STR_PAD_LEFT) . str_pad("Base", 13, " ", STR_PAD_LEFT) . str_pad("Imp.", 12, " ", STR_PAD_LEFT) . str_pad("Total", 12, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "------------------------------------------");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

    $totalbase = 0;
    $totalimp = 0;
    $totalt = 0;

    foreach ($bases as $valor) {

        $base = $valor->{'base'};
        $imp = $valor->{'imp'};
        $total = $valor->{'total'};
        $ind = $valor->{'ind'};

        @fwrite($handle, str_pad($ind . ".", 5, " ", STR_PAD_LEFT) . str_pad(number_format($valor->{'base'}, 2, '.', ','), 13, " ", STR_PAD_LEFT) . str_pad(number_format($valor->{'imp'}, 2, '.', ','), 12, " ", STR_PAD_LEFT) . str_pad(number_format($valor->{'total'}, 2, '.', ','), 12, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

        $totalbase = $totalbase + $base;
        $totalimp = $totalimp + $imp;
        $totalt = $totalt + $total;

    }

    $totalbase = number_format($totalbase, 2, '.', ',');
    $totalimp = number_format($totalimp, 2, '.', ',');
    $totalt = number_format($totalt, 2, '.', ',');

    @fwrite($handle, "==========================================");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, str_pad("Total:", 5, " ", STR_PAD_LEFT) . str_pad($totalbase, 12, " ", STR_PAD_LEFT) . str_pad($totalimp, 12, " ", STR_PAD_LEFT) . str_pad($totalt, 12, " ", STR_PAD_LEFT));

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro

    @fwrite($handle, "Formas de Pago (VENTAS).");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "------------------------------------------");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal

    foreach ($formaventas as $valor) {

        $tipoforma = $valor->{'tipo'};
        $forma = $valor->{'forma'};
        $monto = number_format($valor->{'monto'}, 2, '.', ',');

        @fwrite($handle, str_pad($tipoforma, 22, " ", STR_PAD_RIGHT) . str_pad($forma . "     " . $monto, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, "               ------------------------");
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
        @fwrite($handle, str_pad($tipoforma, 22, " ", STR_PAD_RIGHT) . str_pad($forma . "     " . $monto, 20, " ", STR_PAD_LEFT));
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal

    }

    @fwrite($handle, "==========================================");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, str_pad(" ", 22, " ", STR_PAD_RIGHT) . str_pad($totalt, 20, " ", STR_PAD_LEFT));

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro

    @fwrite($handle, "Resumen de Descuentos");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "------------------------------------------");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, str_pad("Desc. Libre", 22, " ", STR_PAD_RIGHT) . str_pad($descuentoslibre, 20, " ", STR_PAD_LEFT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, "==========================================");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, chr(27) . chr(97) . chr(1)); //->Centro

    @fwrite($handle, "FINAL DEL REPORTE");

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(10) . chr(10) . chr(10) . chr(10) . chr(10) . chr(29) . chr(86) . chr(49) . chr(12));
    @fclose($handle); // cierra el fichero PRN*/

} else {

    $cadena = "";

    $cadena = "";

    $cadena = $cadena . $nombreempresa;
    $cadena = $cadena . ";";
    $cadena = $cadena . "R.U.C.:   " . $emp_idfiscal;
    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("SERIE#:", 10, "!", STR_PAD_RIGHT) . str_pad($serie, 20, "!", STR_PAD_RIGHT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Fecha:", 10, "!", STR_PAD_RIGHT) . str_pad($fecha, 20, "!", STR_PAD_RIGHT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Caja:", 10, "!", STR_PAD_RIGHT) . str_pad($caja, 20, "!", STR_PAD_RIGHT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Control #:", 10, "!", STR_PAD_RIGHT) . str_pad($control, 20, "!", STR_PAD_RIGHT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Usuario:", 10, "!", STR_PAD_RIGHT) . str_pad($usuario, 20, "!", STR_PAD_RIGHT);
    $cadena = $cadena . ";";
    $cadena = $cadena . ";";

    if ($tipo == 'z') {
        $cadena = $cadena . "@REPORTE Z #: " . $reporte_x;
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Inicio: ", 22, "!", STR_PAD_RIGHT) . str_pad($inicioturno, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Fin de: ", 22, "!", STR_PAD_RIGHT) . str_pad($finturno, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Num. fact. inicio:", 22, "!", STR_PAD_RIGHT) . str_pad($numerofactini, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Num. fact. final:", 22, "!", STR_PAD_RIGHT) . str_pad($numerofactfin, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Tot Fact. emitidas:", 22, "!", STR_PAD_RIGHT) . str_pad($totalfact, 20, "!", STR_PAD_LEFT);

    } else {
        $cadena = $cadena . "@REPORTE X #: " . $reporte_x;
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Inicio turno:", 22, "!", STR_PAD_RIGHT) . str_pad($inicioturno, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Fin de turno:", 22, "!", STR_PAD_RIGHT) . str_pad($finturno, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Numero fact. inicio:", 22, "!", STR_PAD_RIGHT) . str_pad($numerofactini, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Numero fact. final:", 22, "!", STR_PAD_RIGHT) . str_pad($numerofactfin, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Total Facturas:", 22, "!", STR_PAD_RIGHT) . str_pad($totalfact, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Total Devoluciones:", 22, "!", STR_PAD_RIGHT) . str_pad($totaldev, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Total Facturas Aptos:", 22, "!", STR_PAD_RIGHT) . str_pad($totalfactaptos, 20, "!", STR_PAD_LEFT);

    }

    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . "@DETALLE DEL CIERRE";
    $cadena = $cadena . ";";
    $cadena = $cadena . "------------------------------------------";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("=FACTURAS:", 22, "!", STR_PAD_RIGHT) . str_pad($facturas, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("-NOTAS DE CREDITO:", 22, "!", STR_PAD_RIGHT) . str_pad($notacred, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("=VENTA NETA:", 22, "!", STR_PAD_RIGHT) . str_pad($ventaneta, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("+IMPUESTOS:", 22, "!", STR_PAD_RIGHT) . str_pad($impuestos, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . "==========================================";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("TOTAL DE VENTAS:", 22, "!", STR_PAD_RIGHT) . str_pad($totalventas, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Ventas de contado:", 22, "!", STR_PAD_RIGHT) . str_pad($ventascontado, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Ventas a credito:", 22, "!", STR_PAD_RIGHT) . str_pad($ventascredito, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Descuentos:", 22, "!", STR_PAD_RIGHT) . str_pad($descuentos, 20, "!", STR_PAD_LEFT);

    if ($tipo != 'z') {
        $cadena = $cadena . ";";
        $cadena = $cadena . ";";
        $cadena = $cadena . "@Desglose de entradas/salidas";
        $cadena = $cadena . ";";
        $cadena = $cadena . "------------------------------------------";
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Apertura de Caja:", 22, "!", STR_PAD_RIGHT) . str_pad($aperturacaja, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Monto por entradas:", 22, "!", STR_PAD_RIGHT) . str_pad($montoentrada, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Monto por salidas:", 22, "!", STR_PAD_RIGHT) . str_pad($montosalida, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . "==========================================";
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("=Diferencia:", 22, "!", STR_PAD_RIGHT) . str_pad($diferencia, 20, "!", STR_PAD_LEFT);

    }

    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . "@Recibo de Dinero";
    $cadena = $cadena . ";";
    $cadena = $cadena . "------------------------------------------";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("*Abonos ctes:", 22, "!", STR_PAD_RIGHT) . str_pad($abonoctes, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("+Abonos aptoss:", 22, "!", STR_PAD_RIGHT) . str_pad($abonoaptos, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("+Abonos cert:", 22, "!", STR_PAD_RIGHT) . str_pad($abonocert, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("+Manejo/Servicio:", 22, "!", STR_PAD_RIGHT) . str_pad($manejoservicio, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . "==========================================";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("=Total Pagos/Abono:", 22, "!", STR_PAD_RIGHT) . str_pad($totalabono, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . "@Formas de Pago (CAJA)";
    $cadena = $cadena . ";";
    $cadena = $cadena . "------------------------------------------";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Ventas", 10, "!", STR_PAD_LEFT) . str_pad("Pagos", 10, "!", STR_PAD_LEFT) . str_pad("E/S", 10, "!", STR_PAD_LEFT) . str_pad("Total", 10, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";

    foreach ($formapago as $valor) {

        $ventas = $valor->{'ventas'};
        $pagos = $valor->{'pagos'};
        $es = $valor->{'es'};
        $total = $valor->{'total'};
        $ind = $valor->{'ind'};

        $cadena = $cadena . str_pad($ind . ".", 0, " ", STR_PAD_LEFT) . str_pad(number_format($valor->{'ventas'}, 2, '.', ','), 8, "!", STR_PAD_LEFT) . str_pad(number_format($valor->{'pagos'}, 2, '.', ','), 10, "!", STR_PAD_LEFT) . str_pad(number_format($valor->{'es'}, 2, '.', ','), 10, "!", STR_PAD_LEFT) . str_pad(number_format($valor->{'total'}, 2, '.', ','), 10, "!", STR_PAD_LEFT);

        $cadena = $cadena . ";";

        $totalVentasP = $totalVentasP + $ventas;
        $totalpagos = $totalpagos + $pagos;
        $totales = $totales + $es;
        $totalt = $totalt + $total;

    }

    $cadena = $cadena . "==========================================";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("   ", 0, " ", STR_PAD_LEFT) . str_pad(number_format($totalVentasP, 2, '.', ','), 8, "!", STR_PAD_LEFT) . str_pad(number_format($totalpagos, 2, '.', ','), 10, "!", STR_PAD_LEFT) . str_pad(number_format($totales, 2, '.', ','), 10, "!", STR_PAD_LEFT) . str_pad(number_format($totalt, 2, '.', ','), 10, "!", STR_PAD_LEFT);

    if ($tipo != 'z') {
        $cadena = $cadena . ";";
        $cadena = $cadena . ";";
        $cadena = $cadena . "@Arqueo de caja:";
        $cadena = $cadena . ";";
        $cadena = $cadena . "------------------------------------------";
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Dinero t. turno:", 22, "!", STR_PAD_RIGHT) . str_pad($dinerotturno, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Dinero t. gaveta:", 22, "!", STR_PAD_RIGHT) . str_pad($dinerogaveta, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . "==========================================";
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Diferencia cajero:", 22, "!", STR_PAD_RIGHT) . str_pad($diferenciacajero, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad("Depósito a realizar:", 22, "!", STR_PAD_RIGHT) . str_pad($depositorealizar, 20, "!", STR_PAD_LEFT);

    }

    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . "@Resumen de impuestos.";
    $cadena = $cadena . ";";
    $cadena = $cadena . "(incluye facturas de credito)";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("T.%", 5, "!", STR_PAD_LEFT) . str_pad("Base", 13, "!", STR_PAD_LEFT) . str_pad("Imp.", 12, "!", STR_PAD_LEFT) . str_pad("Total", 12, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . "------------------------------------------";
    $cadena = $cadena . ";";

    $totalbase = 0;
    $totalimp = 0;
    $totalt = 0;

    foreach ($bases as $valor) {

        $base = $valor->{'base'};
        $imp = $valor->{'imp'};
        $total = $valor->{'total'};
        $ind = $valor->{'ind'};

        $cadena = $cadena . str_pad($ind . ".", 5, "!", STR_PAD_LEFT) . str_pad(number_format($valor->{'base'}, 2, '.', ','), 13, "!", STR_PAD_LEFT) . str_pad(number_format($valor->{'imp'}, 2, '.', ','), 12, "!", STR_PAD_LEFT) . str_pad(number_format($valor->{'total'}, 2, '.', ','), 12, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";

        $totalbase = $totalbase + $base;
        $totalimp = $totalimp + $imp;
        $totalt = $totalt + $total;

    }

    $totalbase = number_format($totalbase, 2, '.', ',');
    $totalimp = number_format($totalimp, 2, '.', ',');
    $totalt = number_format($totalt, 2, '.', ',');

    $cadena = $cadena . "==========================================";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Total:", 5, "!", STR_PAD_LEFT) . str_pad($totalbase, 12, "!", STR_PAD_LEFT) . str_pad($totalimp, 12, "!", STR_PAD_LEFT) . str_pad($totalt, 12, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . "@Formas de Pago (VENTAS).";
    $cadena = $cadena . ";";
    $cadena = $cadena . "------------------------------------------";
    $cadena = $cadena . ";";

    foreach ($formaventas as $valor) {

        $tipo = $valor->{'tipo'};
        $formaforma = $valor->{'forma'};
        $monto = number_format($valor->{'monto'}, 2, '.', ',');
        $cadena = $cadena . str_pad($tipo, 22, "!", STR_PAD_RIGHT) . str_pad($formaforma . "     " . $monto, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . "               ------------------------";
        $cadena = $cadena . ";";
        $cadena = $cadena . str_pad($tipo, 22, "!", STR_PAD_RIGHT) . str_pad($formaforma . "     " . $monto, 20, "!", STR_PAD_LEFT);
        $cadena = $cadena . ";";
        $cadena = $cadena . ";";

    }

    $cadena = $cadena . "==========================================";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad(" ", 22, "!", STR_PAD_RIGHT) . str_pad($totalt, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . "@Resumen de Descuentos";
    $cadena = $cadena . ";";
    $cadena = $cadena . "------------------------------------------";
    $cadena = $cadena . ";";
    $cadena = $cadena . str_pad("Desc. Libre", 22, "!", STR_PAD_RIGHT) . str_pad($descuentoslibre, 20, "!", STR_PAD_LEFT);
    $cadena = $cadena . ";";
    $cadena = $cadena . "==========================================";
    $cadena = $cadena . ";";
    $cadena = $cadena . ";";
    $cadena = $cadena . "@FINAL DEL REPORTE";
    $cadena = $cadena . ";";

    // $handle = fopen('/dev/ttyUSB0','w');

//abre cupon

    $arre = chr(2) . chr(6) . chr(0) . chr(28) . chr(83) . chr(48) . chr(49) . chr(208) . chr(0);

//@fwrite($handle,$arre );

//usleep(600000);

//echo  'listafinal '.$send;

    $arre = chr(2) . chr(6) . chr(0) . chr(28) . chr(20) . chr(112) . chr(10) . chr(170) . chr(0);
    echo "\n";
    echo "\n";

    echo 'hexadec: ' . strToHex($arre);

    $conv = strToHex($arre);
    echo "\n";
    echo "\n";

    echo 'mandando' . hexToStr($conv);

    $caena1 = nl2br($cadena);

// var_dump( explode( ';', $caena1 ) );

//var_dump( explode( ';', $cadena ) );

//$cadena=  str_replace("!", " ", $cadena);

    $array = explode(';', $cadena);

    $longitud = count($array);
//$longitud =20;

    echo $longitud;

    $texto99 = '';

//Recorro todos los elementos
    $imprimel = 0;
    $completo = '';
    for ($i = 0; $i < $longitud; $i++) {
        //saco el valor de cada elemento
        //      echo  str_replace("!", " ", $array[$i]);

        $vacio = $array[$i];

        if ($vacio == '') {
            //echo 'vacioioioioioioioioioioioioio';

        } else {

            $imprimel = $imprimel + 1;

            $lalinea = str_replace("!", " ", $array[$i]);

            $lalinea = str_pad($lalinea, 48, " ", STR_PAD_RIGHT);

            $texto1 = $texto1 . $lalinea;

            //  echo  'ototototototot: ' .$imprimel;

            if ($imprimel == 5) {

                echo $texto1;
                echo "\n";

                $imprimel = 0;
                $termino = imprime($texto1);

                $completo = $completo . ';' . strToHex($termino);

//echo $termino;

//@fwrite($handle,$termino );

//usleep(600000);
                $texto1 = '';

            }

        }

    }

    $arre = chr(2) . chr(4) . chr(0) . chr(28) . chr(21) . chr(49) . chr(0);

    echo 'hexadec: completo ' . $completo . ';' . strToHex($arre);

    $url1 = "http://localhost:8080/webservice.carlos/rest/todos/fiscalcierre";

    $data_string99 = $completo . ';' . strToHex($arre);
//$data_string99=$json;

    $url1 = "http://localhost:8080/webservice.carlos/rest/todos/fiscalcierre";

    $ch = curl_init($url1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string99);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string99))
    );
    $result = curl_exec($ch);
    curl_close($ch);

//print_r($caena1);
    $json1->cierrefiscal = $caena1;
//print_r($json1);
    print_r(json_encode($json1));

}
function strToHex($string)
{
    $hex = '';
    for ($i = 0; $i < strlen($string); $i++) {
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= substr('0' . $hexCode, -2);
    }
    return strToUpper($hex);
}
function hexToStr($hex)
{
    $string = '';
    for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
        $string .= chr(hexdec($hex[$i] . $hex[$i + 1]));
    }
    return $string;
}

function letratohexa($palabra)
{
    $matriz1 = str_split($palabra);

    $stringlisto = '';
    $suma = 0;
    foreach ($matriz1 as $key => $num) {
//    echo ' letra:  '.  $num.' hex '.strToHex($num).' Dec'. hexdec(strToHex($num));

        $suma = $suma + hexdec(strToHex($num));

//      echo '  Sumando:'.($suma+58);

        $_SESSION['suma'] = dechex($suma + 58);

        echo "\n";

        $stringlisto = $stringlisto . ' ' . hexdec(strToHex($num));
    }

    echo "\n";
    echo "\n";

    return $stringlisto;
}

function imprime($texto1)
{

    $arre0 = chr(2);

    $cmd = chr(28);
    $fijo1 = chr(20);
    $fijo2 = chr(10);

    $texto = str_replace("<br />", "", $texto1);

    $texto = str_replace("!", " ", $texto);

    $texto = str_replace("enter", "
", $texto);
    $texto = substr($texto, 0, 240);

//echo 'sql' .$texto;
    $send = letratohexa($texto);

    $textofinal = $fijo1 . $texto . $fijo2;

    $tamini = (strlen($textofinal) + 3);

    $tam = strlen($_SESSION['suma']);

    echo 'tamaño' . $tam;
    echo "\n";

    if ($tam < 4) {
        $sumi = '0' . $_SESSION['suma'];

        echo 'menos de 4';
        echo "\n";

    } else {
        echo 'mas de 4';
        echo "\n";

        $sumi = $_SESSION['suma'];}
    ;

    echo substr($sumi, 0, 2);
    echo "\n";
    echo substr($sumi, 2, 2);

    $min = hexdec(substr($sumi, 0, 2));
    $max = hexdec(substr($sumi, 2, 2));
    echo "\n";
    echo 'Maximo' . $max;
    echo "\n";
    echo 'min' . $min;

    $NBL = chr($tamini);

    $NBS = chr(0);

    $CSL = chr($max);
    $CSH = chr($min);

    $arre = $arre0 . $NBL . $NBS . $cmd . $textofinal . $CSL . $CSH;

//@fwrite($handle,$arre );

//usleep(600000);

    return $arre;

};
?>
