<?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['json'];

include 'conn.php';
include 'funciones.php';

//$json = $_POST['json'];
//$json = '{"datos":[{"printer_name":"192.168.1.114","num_fact":"000000087","terminal":"SUC[1]-ESTACION[1]","usuario":"usuario","fecha":"2017-10-27 15:59:14"}],"productos":[{"descripcion":"ARTICULO DE PRUEBAS","cantidad":1}]}';
//$json = '{"datos":[{"ubic_id":"1","printer_name":"192.168.1.114","op_fecha":"01/02/2018 03:28:11 p.m.","op_id":"120000011","suc_id":"2","op_estacion":"1","c_descripcion":"CLIENTE CONTADO","u_descripcion":"FINAL1","mesa": "1","ubicacion":"2"}], "det":[{"po_pr_detalle":"190"},{"po_pr_detalle":"191"},{"po_pr_detalle":"195"},{"po_pr_detalle":"196"}]}';
//$json = '{"datos":[{"ubic_id":"1","printer_name":"192.168.1.114","op_fecha":"01/02/2018 03:28:11","op_id":"120000011","suc_id":"2","op_estacion":"1","c_descripcion":"CLIENTE CONTADO","u_descripcion":"FINAL1","mesa": "1","ubicacion":"2"}], "det":[{"po_pr_detalle":"190"},{"po_pr_detalle":"191"},{"po_pr_detalle":"195"},{"po_pr_detalle":"196"}]}';

//$json = '{"datos":[{"ubic_id":1,"printer_name":"","op_fecha":"2018-03-08T15:03:53","op_id":110000094,"suc_id":1,"op_estacion":1,"c_descripcion":"CLIENTE CONTADO","mesa":7,"ubicacion":1,"u_descripcion":""}],"det":[{"po_pr_detalle":"","po_descripcion":"PRODUCTO DE PRUEBAS","po_cant":1}]}';

$json = $_POST['json'];

//$printer = $_POST['printer'];

$obj = json_decode($json);
$datos = $obj->{'datos'};
$det = $obj->{'det'};

foreach ($datos as $valor) {

    $ubic_id = $valor->{'ubic_id'};
    $printer = $valor->{'printer_name'};
    $op_fecha = $valor->{'op_fecha'};
    $op_id = $valor->{'op_id'};
    $suc_id = $valor->{'suc_id'};
    $fecha = $valor->{'op_fecha'};
    $op_id = $valor->{'op_id'};
    $suc_id = $valor->{'suc_id'};
    $op_estacion = $valor->{'op_estacion'};
    $c_descripcion = $valor->{'c_descripcion'};
    $u_descripcion = $valor->{'u_descripcion'};
    $mesa = $valor->{'mesa'};
    $ubicacion = $valor->{'ubicacion'};

}

//$printer = str_replace("!","/",$printer);
$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);
}

//print_r($printer . "   "."/dev/usb/lp0");
//$printer = "/dev/usb/lp0";
//$printer = $printer;
//print_r($printer);
//print_r($printer);

//$handle = fopen($printer, "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. 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, "------------------------------------------");

foreach ($det as $valor) {

    $po_pr_detalle = $valor->{'po_pr_detalle'};
    $po_descripcion = $valor->{'po_descripcion'};
    $po_cant = $valor->{'po_cant'};

    @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) $po_cant, 3) . $po_descripcion);

    if ($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*/

//print_r($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
 */

?>