<?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 = '{"operacion":{"tipo":"2","nombre":"CLIENTE CONTADO","ruc":"000-000-000","direccion":"dorec","telefono":"0412-","desc":"0.00"},"productos":[{"pr_id":"101","descripcion":"ARTICULO DE PRUEBAS","cantidad":"1.000","precio":"1.250","desc":"0.00","iva":"0.00"}],"cant_prod":1,"pagos":[{"detalle":"Cash","monto":"1.250"}]}';

$obj = json_decode($json);

$productos = $obj->{'productos'};
$operacion = $obj->{'operacion'};
$varios = $obj->{'varios'};

$nombre = "";
$ruc = "";
$direccion = "";
$desc_g = 0;
$empresa = "";
$ruc_e = "";
$dir = "";
$fecha = "";
$factura = "";
$iva = "";
$tipo = "Factura";

foreach ($varios as $valor) {
    $empresa = $valor->{'empresa'};
    $ruc_e = $valor->{'ruc'};
    $dir = $valor->{'dir'};
    $fecha = $valor->{'fecha'};
    $factura = $valor->{'factura'};
    $iva = $valor->{'iva'};
    $tipo = $valor->{'tipo'};
}

foreach ($operacion as $valor) {
    $nombre = $operacion->{'nombre'};
    $ruc = $operacion->{'ruc'};
    $direccion = $operacion->{'direccion'};
    $desc_g = $operacion->{'desc'};
}

//print_r($nombre);

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

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

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

}

//$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, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "IMPRESORA NO FISCAL");
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, $empresa);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
//@fwrite($handle, $dir );
//@fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
@fwrite($handle, chr(27) . chr(97) . chr(0)); //->Izquierda
@fwrite($handle, "RUC: " . $ruc_e);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, $fecha);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, chr(27) . chr(33) . chr(8)); //negrita
@fwrite($handle, $tipo . ": " . $factura);
@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: " . $nombre);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "RUC: " . $ruc);
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "DIRECCION: " . $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, $tipo);
@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, "-----------------------------------------");

$subtotal = 0;
$descuento = 0;
$desc_general = 0;
foreach ($productos as $valor) {

    $pr_id = $valor->{'pr_id'};
    $descripcion = $valor->{'descripcion'};
    $precio = $valor->{'precio'};
    $cantidad = $valor->{'cantidad'};
    $desc = $valor->{'desc'};

    $subtotal = $subtotal + ($precio * $cantidad);

    $descuento = $descuento + $desc;

    $t_precio = $precio * $cantidad;

    @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, 20) . $descripcion);
    @fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
    @fwrite($handle, str_pad($valor->{'iva'} . "*" . $cantidad, 20) . str_pad(number_format($precio, 2, '.', ','), 10, " ", STR_PAD_LEFT) . str_pad(number_format($t_precio, 2, '.', ','), 10, " ", STR_PAD_LEFT));

}

$descuento = $descuento + $desc_g;

$total = $subtotal - $descuento + $iva;

@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: " . str_pad(number_format($subtotal, 2, '.', ',') . "  ", 15, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "Descuento: " . str_pad(number_format($descuento, 2, '.', ',') . "  ", 15, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "IVA (7.00%): " . str_pad(number_format($iva, 2, '.', ',') . "  ", 15, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "IVA#2 (00.00%): " . str_pad("0.00" . "  ", 15, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "IVA#3 (00.00%): " . str_pad("0.00" . "  ", 15, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "Servicios: " . 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: " . str_pad(number_format($total, 2, '.', ',') . "  ", 15, " ", STR_PAD_LEFT));
@fwrite($handle, chr(27) . chr(100) . chr(1)); //salto de linea
@fwrite($handle, "Recibido: " . str_pad(number_format($total, 2, '.', ',') . "  ", 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*/

?>