

<?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);

$_SESSION['po_op_id'] = $_GET['po_op_id'];
$_SESSION['database'] = $_GET['database'];
$_SESSION['u_id'] = $_GET['u_id'];

include 'conn.php';
include 'funciones.php';
$printer = "";

$config_impre = 0;
$sqlrest = "select distinct uui.ubic_id, printer_name from " . $_SESSION['database'] . ".user_ubic_impre uui
				join " . $_SESSION['database'] . ".productos_det pd on uui.ubic_id = pd.ubic_id
				join " . $_SESSION['database'] . ".tmp_producto_op tmp_p on pd.pr_id = tmp_p.po_id_prod
				where  tmp_p.po_visible_grid = 'y' and tmp_p.ubic_enviada = 0 and tmp_p.po_op_id = '" . $_SESSION['po_op_id'] . "' and uui.u_id = " . $_SESSION['u_id'];

$qurprint = $connection->query($sqlrest);

//print_r($sqlrest);
while ($print = mysqli_fetch_assoc($qurprint)) {

    //print_r($sqlrest);
    $config_impre = 1;
    $ubic_id = $print['ubic_id'];
    $printer = $print['printer_name'];

//COMANDA CON MESA
    $dato = "a  ";
//fwrite($handle, chr(27). chr(100). chr(N)); //-> Limpia el buffer, y salta N lineas, poner numero de saltos en la ‘N’, admite un 0.
    $sqlrest = "select distinct tmp_p.mesa, tmp_p.ubicacion, c.c_descripcion, u.u_descripcion, tmp_o.* " .
        " from " . $_SESSION['database'] . ".tmp_operacion tmp_o " .
        " left join " . $_SESSION['database'] . ".clientes c on  tmp_o.op_id_cliente = c.c_id " .
        " left join " . $_SESSION['database'] . ".usuarios u on  tmp_o.op_usuario = u.u_id " .
        " join " . $_SESSION['database'] . ".tmp_producto_op tmp_p on tmp_o.op_id = tmp_p.po_op_id  " .
        " where tmp_p.po_visible_grid = 'y' and op_id = '" . $_SESSION['po_op_id'] . "'";

//print_r($sqlrest);

    $qur = $connection->query($sqlrest);

    while ($r = mysqli_fetch_assoc($qur)) {

        $fecha = $r['op_fecha'];
        $op_id = $r['op_id'];
        $suc_id = $r['suc_id'];
        $op_estacion = $r['op_estacion'];
        $c_descripcion = $r['c_descripcion'];
        $u_descripcion = $r['u_descripcion'];
        $mesa = $r['mesa'];
        $ubicacion = $r['ubicacion'];
        $op_fecha = $r['op_fecha'];
    }

    $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("/dev/usb/lp0", "w");
    //$handle = fopen("USB002", 9100);
    //$handle = fopen("LPT1", 9100);
    //$handle = @fopen("\\\\127.0.0.1\\USB002", "w"); //WINDOWS

    @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. Ped.", 10) . " : " . $op_autoin);
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Num. Ref.", 10) . " : " . $op_id);
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Num. Cta.", 10) . " : " . $ubicacion);
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Terminal", 10));
    @fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
    @fwrite($handle, " : SUC[" . $suc_id . "]-ESTACION[" . $op_estacion . "]");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, str_pad("Abrio Mesa", 10) . " : " . $u_descripcion);
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad("Mesero", 10) . " : " . $u_descripcion);
    @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, "MESA: (" . $mesa . ")/CTA: (" . $ubicacion . ")");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(33) . chr(0)); //normal
    @fwrite($handle, "------------------------------------------");

    $sqlrest = "select * from " . $_SESSION['database'] . ".tmp_producto_op tmp_p " .
        "join " . $_SESSION['database'] . ".productos_det pd on tmp_p.po_id_prod = pd.pr_id " .
        "where tmp_p.po_visible_grid = 'y' and pd.ubic_id = " . $ubic_id . " and tmp_p.po_op_id = '" . $_SESSION['po_op_id'] . "' and tmp_p.ubic_enviada = 0";

    $qur = $connection->query($sqlrest);

    while ($r = mysqli_fetch_assoc($qur)) {

        @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) $r['po_cant'], 3) . $r['po_descripcion']);
        $po_pr_detalle = $r['po_pr_detalle'];

        if ($r['po_pr_detalle'] != "") {
            @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
            $po_pr_detalle = str_replace("|||", "\n   - ", $po_pr_detalle);
            @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, "VTA. C/MESA");
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
    @fwrite($handle, $op_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*/

}

if ($config_impre == 0) {
    $json->datos = false;
} else {
    $sql = "update " . $_GET['database'] . ".tmp_producto_op set ubic_enviada = 1 where po_op_id = '" . $_SESSION['po_op_id'] . "'";
    $qur = $connection->query($sql);

    $json->datos = true;
}

print_r(json_encode($json));

/*
$sqlrest  =    "select * from 1002_tc.productos where pr_id in ('23','26')";

$qur = $connection->query($sqlrest);

while($r = mysqli_fetch_assoc($qur)){
$pr_id = $r['pr_id'];
$pr_descripcion = $r['pr_descripcion'];

fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0)); //->Izquierda
fwrite($handle, str_pad($pr_id,  24).$pr_descripcion);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, str_pad("7.00 * 1",  20).str_pad("20.25", 10, " ", STR_PAD_LEFT).str_pad("90.22", 10, " ", 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(97). chr(2)); //->Derecha
fwrite($handle, "Sub-Total: Bs".str_pad("9052.22"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "Descuento: Bs".str_pad("10.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "IVA (12.00%): Bs".str_pad("1.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "IVA#2 (10.00%): Bs".str_pad("0.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "IVA#3 (15.00%): Bs".str_pad("0.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "Servicios: Bs".str_pad("0.00"."  ", 15, " ", 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, "Total: Bs".str_pad("0.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "Recibido: Bs".str_pad("0.00"."  ", 15, " ", 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(10) . chr(10) . chr(10) . chr(10) . chr(10) . chr(29) . chr(86) . chr(49) . chr(12));
fclose($handle); // cierra el fichero PRN */

//IMPRESORA NO FISCAL
/*$dato = "a  ";

//fwrite($handle, chr(27). chr(100). chr(N)); //-> Limpia el buffer, y salta N lineas, poner numero de saltos en la ‘N’, admite un 0.

$handle = fopen("/dev/usb/lp0", "w");
fwrite($handle,chr(27). chr(64));
fwrite($handle, chr(27). chr(97). chr(1)); //->Centro
fwrite($handle,"DGI");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"IMPRESORA FISCAL");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"PARA DESARROLLO");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"BAYSHORE TECHNOLOGIES PANAMA");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"PLAZA CORDOBA, PISO 1 LOCAL 5");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0)); //->Izquierda
fwrite($handle,"RUC: 155609865-2-2015");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"20/06/2017 16:28:14 ");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
fwrite($handle,"Factura: PPBT112001395-000000001");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "-----------Datos del Consumidor-----------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "NOMBRE: ");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "RUC: ");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "DIRECCION: ");
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(1)); //->Centro
fwrite($handle, "FACTURA");
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, "Codigo                  Descripcion");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "Tasa% Cant.             Precio  Precio T.");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "-----------------------------------------");

$sqlrest  =    "select * from 1002_tc.productos where pr_id in ('23','26')";

$qur = $connection->query($sqlrest);

while($r = mysqli_fetch_assoc($qur)){
$pr_id = $r['pr_id'];
$pr_descripcion = $r['pr_descripcion'];

fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0)); //->Izquierda
fwrite($handle, str_pad($pr_id,  24).$pr_descripcion);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, str_pad("7.00 * 1",  20).str_pad("20.25", 10, " ", STR_PAD_LEFT).str_pad("90.22", 10, " ", 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(97). chr(2)); //->Derecha
fwrite($handle, "Sub-Total: Bs".str_pad("9052.22"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "Descuento: Bs".str_pad("10.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "IVA (12.00%): Bs".str_pad("1.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "IVA#2 (10.00%): Bs".str_pad("0.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "IVA#3 (15.00%): Bs".str_pad("0.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "Servicios: Bs".str_pad("0.00"."  ", 15, " ", 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, "Total: Bs".str_pad("0.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "Recibido: Bs".str_pad("0.00"."  ", 15, " ", STR_PAD_LEFT));
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
 */

?>