<?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 = '/dev/usb/lp0';

$json = $_POST['json'];
//$json = '{"datos":[{"num_orden":"000000016","num_fact":"000000087","terminal":"SUC[1]-ESTACION[1]","usuario":"usuario","fecha":"2017-10-27 15:59:14"}],"productos":[{"descripcion":"ARTICULO DE PRUEBAS","cantidad":1}]}';

$obj = json_decode($json);
$datos = $obj->{'datos'};
$productos = $obj->{'productos'};
$num_orden = "";
$num_fact = "";
$terminal = "";
$usuario = "";
$fecha = "";

foreach ($datos as $valor) {
    $num_orden = $valor->{'num_orden'};
    $num_fact = $valor->{'num_fact'};
    $terminal = $valor->{'terminal'};
    $usuario = $valor->{'usuario'};
    $cliente = $valor->{'cliente'};
    $fecha = $valor->{'fecha'};
}

$pos = strpos($printer, ".");

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

//$handle = fopen("/dev/usb/lp0", "w");

@fwrite($handle, chr(27) . chr(64));
@fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
@fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
@fwrite($handle, "<< PEDIDO >>");
@fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, $c_descripcion);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Num. Orden.", 11) . " : " . $num_orden);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, str_pad("Num. Fact.", 11) . " : " . $num_fact);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
@fwrite($handle, str_pad("Cliente", 11) . " : " . $cliente);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
@fwrite($handle, str_pad("Terminal.", 11) . " : " . $terminal);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
@fwrite($handle, str_pad("Usuario", 11) . " : " . $usuario);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
@fwrite($handle, "------------------------------------------");

foreach ($productos as $valor) {

    $cantidad = $valor->{'cantidad'};
    $descripcion = $valor->{'descripcion'};
    $po_pr_detalle = $valor->{'det_comanda'};

    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
    @fwrite($handle, str_pad((int) $cantidad, 3) . $descripcion);

    if ($po_pr_detalle != "") {
        $po_pr_detalle = str_replace("\n", "\n   - ", $po_pr_detalle);
        @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
        @fwrite($handle, "   - " . $po_pr_detalle);
    }
}

@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(97) . chr(1)); //->Centro
@fwrite($handle, "POS");
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
@fwrite($handle, $fecha);
@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(10) . chr(10) . chr(10) . chr(10) . chr(10) . chr(29) . chr(86) . chr(49) . chr(12));
@fclose($handle); // cierra el fichero PRN*/
