<?php
include '../../ajax/is_logged.php';
?>
<style type="text/css" media="print">
    #Imprime {
        height: auto;
        width: 377px;
        margin: 0px;
        padding: 0px;
        float: left;
        font-family: "Comic Sans MS", cursive;
        font-size: 7px;
        font-style: normal;
        line-height: normal;
        font-weight: normal;
        font-variant: normal;
        text-transform: none;
        color: #000;
    }
    @page{
        margin: 0;
    }
    .urgente {
        font-weight: bold;
    }
    .notas {
        font-size: 6px;
        font-style: italic;
        color: #555;
    }
</style>
<?php
require_once "../../db.php";
require_once "../../php_conexion.php";
include "../../funciones.php";

$simbolo_moneda = get_row('perfil', 'moneda', 'id_perfil', 1);
$factura_id     = mysqli_real_escape_string($conexion, (strip_tags($_REQUEST["id_factura"], ENT_QUOTES)));
$vista          = intval($_REQUEST['vista']);

/*Datos de la empresa*/
$sql           = mysqli_query($conexion, "SELECT * FROM perfil");
$rw            = mysqli_fetch_array($sql);
$moneda        = $rw["moneda"];
$bussines_name = $rw["nombre_empresa"];
$giro          = $rw["giro_empresa"];
$fiscal        = $rw["fiscal_empresa"];
$address       = $rw["direccion"];
$city          = $rw["ciudad"];
$state         = $rw["estado"];
$postal_code   = $rw["codigo_postal"];
$phone         = $rw["telefono"];
$email         = $rw["email"];
$logo_url      = $rw["logo_url"];
$users         = intval($_SESSION['id_users']);
$fecha         = date("d-m-Y H:i:s");

$sql = mysqli_query($conexion, "select nombre_users, apellido_users from users WHERE id_users = '" . $users . "'");
$rw  = mysqli_fetch_array($sql);
$nom_users = $rw['nombre_users'];
?>

<page pageset='new' backtop='10mm' backbottom='10mm' backleft='20mm' backright='20mm' footer='page'>
    <table width="175px" style="font-size:12px; font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif" border="0">
        <tr>
            <td colspan="3">
                <div align="center" style="font-size:22px"><strong><?php echo $bussines_name; ?></strong><br></div>
                <div align="center" style="font-size:16px"><strong><?php echo $giro; ?></strong><br></div>
                <div align="center" style="font-size:16px"><strong>RUC: <?php echo $fiscal; ?></strong><br></div>
                <div align="center" style="font-size:14px"><strong><?php echo $address; ?></strong><br></div>
                <div align="center" style="font-size:14px"><strong><?php echo $city . ' ' . $state; ?></strong></div>
                <div align="center" style="font-size:14px"><strong>Tel: </strong> <?php echo $phone; ?><br></div>
            </td>
        </tr>
        <tr>
            <td colspan="3"><center>-----------------------------------------</center></td>
        </tr>
        <tr>
            <td colspan="4">
                <div align="left">Mesa: <?php echo $factura_id; ?></div>
                <div align="left">Usuario: <?php echo $nom_users; ?><br></div>
                <div align="left">Fecha: <?php echo $fecha; ?><br></div>
            </td>
        </tr>
        <tr>
            <td colspan="3"><center>==============================</center></td>
        </tr>
        <tr>
            <td width="15%">Cant.</td>
            <td width="60%">Descrip.</td>
            <td width="25%">Notas</td>
        </tr>
        <tr>
            <td colspan="3"><center>==============================</center></td>
        </tr>
        <?php
        $sql_principal = mysqli_query($conexion, "SELECT DISTINCT identificativo FROM tmp_mesa WHERE mesa_tmp = '$factura_id'");
        while ($rw = mysqli_fetch_array($sql_principal)) {
            $identificativo = $rw['identificativo'];
            ?>
            <tr>
                <td colspan="3"><b>Cuenta: <?php echo $identificativo; ?></b></td>
            </tr>
            <?php
            $sWhere = " WHERE tmp_mesa.mesa_tmp = '$factura_id' AND productos.id_producto = tmp_mesa.producto_tmp AND tmp_mesa.identificativo = '$identificativo'";
            
            if ($vista != 0) {
                $sWhere .= " AND productos.vista='$vista'";
            }
            
            $sql_secundaria = "SELECT * FROM productos, tmp_mesa $sWhere AND tmp_mesa.new_cant > 0 GROUP BY tmp_mesa.id_tmp";
            $sql_secundaria_result = mysqli_query($conexion, $sql_secundaria);
            
            while ($row = mysqli_fetch_array($sql_secundaria_result)) {
                $cantidad = $row['new_cant'];
                $nombre_producto = $row['nombre_producto'];
                $detalle = !empty($row['detalle']) ? $row['detalle'] : '';
                $clase = ($row['new_cant'] > 0) ? 'urgente' : '';
                ?>
                <tr class="<?php echo $clase; ?>">
                    <td><?php echo $cantidad; ?></td>
                    <td><?php echo $nombre_producto; ?></td>
                    <td class="notas"><?php echo htmlspecialchars($detalle); ?></td>
                </tr>
                <?php
            }
        }
        ?>
        <tr>
            <td colspan="3"><center>----------------------------------------</center></td>
        </tr>
        <tr>
            <td colspan="3"><center>----------------------------------------</center></td>
        </tr>
        <tr>
            <td colspan="3"><center>----------------------------------------</center></td>
        </tr>
        <tr>
            <td colspan="3"><center>*DETALLE DE PEDIDO*</center></td>
        </tr>
        <tr>
            <td colspan="3"><center></center></td>
        </tr><br>
    </table>
</page>