

<?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'];
//$printer = "\\127.0.0.1\USB002";
$json = $_POST['json'];

include 'funciones.php';

//$totalg = 0.00;

//$json = '{"puesto":"1","fechai":"7/8/2017  16:53:48","caja":0,"amesa":"1","cliente":0,"pedido":"110001654","usuario":0,"jsonproductos":[{"po_id_prod":"45","po_cant":"1.00000","po_descripcion":"ESPECIAL","po_precio":"222.2200","po_itbms":"0.000","porcdesc":"5.00","porcdesc_general":"0.00"},{"po_id_prod":"15","po_cant":"1.00000","po_descripcion":"COCACOLA  2LTR","po_precio":"200.0000","po_itbms":"7.000","porcdesc":"10.00","porcdesc_general":"0.00"}],"jsonbases":[{"po_itbms":"7.000","imp":"14.0000000000000000"}],"jsonoperacion":[{"op_fecha":"2017-08-07 15:27:21","op_subtotal":"422.2200","op_total":"403.7100","op_desc":"31.1100"}]}';

$obj = json_decode($json);
$productos = $obj->{'jsonproductos'};
$operacion = $obj->{'jsonoperacion'};
$bases = $obj->{'jsonbases'};

$fechaa = "";

foreach ($operacion as $valor) {
    $op_subtotal = $valor->{'op_subtotal'};
    $op_desc = $valor->{'op_desc'};
    $neto = number_format($op_subtotal - $op_desc, 2, '.', ',');
    $fechaa = $valor->{'op_fecha'};
    $operacion = $valor->{'operacion'};
}

$fechai = $obj->{'fechai'};
$pos = strpos($printer, ".");

if ($pos !== false) {
    $pos = strpos($printer, "\\\\");
    if ($pos !== false) {
        $handle = @fopen('' . $printer . '', "w");
        //$handle = @fopen("\\127.0.0.1\USB002", "w"); //WINDOWS
        //print_r($printer);
    } else {
        $handle = @fsockopen($printer, 9100);
        //print_r($printer);
    }
    //$handle = fopen($printer, "w");
    //print_r($printer);
} else {
    $handle = @fopen($printer, "w");
    //print_r($printer);
}
//print_r($printer);
//$handle = @fopen("\\127.0.0.1\USB002", "w"); //WINDOWS
@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, "PRE-CUENTA");
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "CUENTA:  MESA: " . $obj->{'mesa'} . " PUESTO: " . $obj->{'puesto'} . "");
@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("Num. Refer.:", 20, " ", STR_PAD_RIGHT) . str_pad($operacion, 22, " ", STR_PAD_RIGHT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Fecha Apertura:", 20, " ", STR_PAD_RIGHT) . str_pad($fechaa, 22, " ", STR_PAD_RIGHT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Fecha IMpresion:", 20, " ", STR_PAD_RIGHT) . str_pad($fechai, 22, " ", STR_PAD_RIGHT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Nombre Caja:", 20, " ", STR_PAD_RIGHT) . str_pad($obj->{'caja'}, 22, " ", STR_PAD_RIGHT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Abrio Mesa:", 20, " ", STR_PAD_RIGHT) . str_pad($obj->{'amesa'}, 22, " ", STR_PAD_RIGHT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Nombre Cliente:", 20, " ", STR_PAD_RIGHT) . str_pad($obj->{'cliente'}, 22, " ", STR_PAD_RIGHT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Numero Pedido:", 20, " ", STR_PAD_RIGHT) . str_pad($operacion, 22, " ", STR_PAD_RIGHT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Usuario:", 20, " ", STR_PAD_RIGHT) . str_pad($obj->{'usuario'}, 22, " ", 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, str_pad("Codigo:", 20, " ", STR_PAD_RIGHT) . str_pad("Descripcion", 22, " ", STR_PAD_RIGHT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Precio Bruto:", 20, " ", STR_PAD_RIGHT) . str_pad("%Desc.     Precio Net", 22, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "------------------------------------------");
$porcdesc_general = 0;
foreach ($productos as $valor) {

    $po_id_prod = $valor->{'po_id_prod'};
    $po_itbms = $valor->{'po_itbms'};
    $po_cant = $valor->{'po_cant'};
    $po_precio = $valor->{'po_precio'};
    $porcdesc = $valor->{'porcdesc'};
    $porcdesc_general = $valor->{'porcdesc_general'};
    $tdescu = $tdescu + ($po_precio * $po_cant) * $porcdesc / 100;
    $descu = number_format(($po_precio * $po_cant) * $porcdesc / 100, 2, '.', ',');
    $total = number_format(($po_precio * $po_cant) - $descu, 2, '.', ',');
    $po_descripcion = $valor->{'po_descripcion'};

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad($po_id_prod, 10, " ", STR_PAD_RIGHT) . str_pad("(" . number_format($po_itbms, 2, '.', ',') . "%) ", 8, " ", STR_PAD_LEFT) . str_pad($po_descripcion, 8, " ", STR_PAD_RIGHT));
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad(number_format($po_cant, 2, '.', ',') . " x " . number_format($po_precio, 2, '.', ','), 20, " ", STR_PAD_RIGHT) . str_pad($descu, 8, " ", STR_PAD_LEFT) . str_pad($total, 14, " ", 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

$totalg = $neto;

@fwrite($handle, str_pad("Sub.Bruto:", 14, " ", STR_PAD_LEFT) . str_pad("Desc.Prod.:", 14, " ", STR_PAD_LEFT) . str_pad("Sub.Neto", 14, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea

$descfact = $op_desc - $tdescu;
@fwrite($handle, str_pad(number_format($op_subtotal, 2, '.', ','), " 14", " ", STR_PAD_LEFT) . str_pad(number_format($tdescu, 2, '.', ','), 14, " ", STR_PAD_LEFT) . str_pad($neto, 14, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad($porcdesc_general . "% ", " 15", " ", STR_PAD_LEFT) . str_pad("Desc.Fact.:", 14, " ", STR_PAD_LEFT) . str_pad(number_format($descfact, 2, '.', ','), 13, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "------------------------------------------");
@fwrite($handle, str_pad("Sub Total:", 30, " ", STR_PAD_LEFT) . str_pad($neto, 12, " ", STR_PAD_LEFT));
foreach ($bases as $valor) {

    $imp = $valor->{'imp'};
    $po_itbms = $valor->{'po_itbms'};

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("ITBMS:  " . number_format($po_itbms, 2, '.', ',') . "%:", 30, " ", STR_PAD_LEFT) . str_pad(number_format($imp, 2, '.', ','), 12, " ", STR_PAD_LEFT));

    $totalg = $totalg + $imp;
}

@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("Total:", 30, " ", STR_PAD_LEFT) . str_pad(number_format($totalg, 2, '.', ','), 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
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("0% Servicio Sugerido:", 22, " ", STR_PAD_RIGHT) . str_pad("0.00", 20, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Total C/Servicio Sug.:", 22, " ", STR_PAD_RIGHT) . str_pad(number_format($totalg, 2, '.', ','), 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(1)); //->Centro
@fwrite($handle, "Gracias por su visita");
@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*/

?>