<?php

//**************** Conexión a Base Maestra
$conn           = "192.168.9.100";
$username       = "sa";
$password       = "colico!981";
$db_name        = "diken";

$connectionInfo = array("Database"=>"$db_name", "UID"=>"$username", "PWD"=>"$password");
$db2            = sqlsrv_connect($conn, $connectionInfo);

require_once('pdf/html2pdf.class.php');

header("Content-Type: text/html;charset=utf-8");

function titulos($CODIGO, $NOMBRE, $CANTIDAD, $FECHA_F, $LOTE, $FECHA_V)
{
    $NOMBRE       = utf8_encode($NOMBRE);
    $LITROS       = number_format($CANTIDAD,  2, ',', '.');

    //**************** Valores
    $content      = '';
    $content      = "
    <style>
       body {
           font-family: Arial, Helvetica, Verdana;
           font-size: 13px;
       }
       .table1 {
           overflow: hidden;
           background: #fefefe;
           width: 80%;
           margin: 5% auto 0;
           box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
       }
       .table1 th {border: 1px solid #000000;}
       .table1 td {border: 1px solid #000000;}
       .table2 {
        overflow: hidden;
        background: #fefefe;
        width: 80%;
        margin: 5% auto 0;
        box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
        border: 1px solid #000000;
    }

    </style>
    <table style='width: 90%; margin: 1% auto 0;'>
        <tr>
           <td>
               <img src='http://sistemas.banados.cl/diken/logoReiniger.jpg' width='200'>
           </td>
           <td>
               <p>
                   Av. Dorsal 3225, Renca, Santiago, Chile.<br>
                   contacto@reiniger.cl
               </p>
           </td>
        </tr>
    </table>
    <BR><BR> 
    <div style='height: 700px; padding-left: 30px; padding-right: 25px;font-size: 13px;'>";

    $LITROS  = $LITROS * 1000;
    
    $content = rtrim(ltrim($content))."<TABLE><TR><TD WIDTH='420'> </TD><TD WIDTH='400'><h3>CERTIFICADO DE ANALISIS</h3></TD></TR></TABLE>";
    $content = rtrim(ltrim($content))."<TABLE class='table2'>";
    $content = rtrim(ltrim($content))."<TR><TD>";
    $content = rtrim(ltrim($content))."<TABLE>";
    $content = rtrim(ltrim($content))."<TR><TD>&nbsp;&nbsp; </TD></TR>";
    $content = rtrim(ltrim($content))."<TR>";
    $content = rtrim(ltrim($content))."<TD WIDTH='30'>&nbsp; </TD>";
    $content = rtrim(ltrim($content))."<TD WIDTH='80'><b>PRODUCTO:</b></TD><TD WIDTH='295'><b>$NOMBRE</b></TD>";
    $content = rtrim(ltrim($content))."<TD WIDTH='170'><b>FECHA DE FABRICACION:</b></TD><TD><b>$FECHA_F</b></TD>";    
    $content = rtrim(ltrim($content))."<TD WIDTH='50'>&nbsp; </TD>";
    $content = rtrim(ltrim($content))."</TR>";
    $content = rtrim(ltrim($content))."<TR><TD>&nbsp;&nbsp; </TD></TR>";
    $content = rtrim(ltrim($content))."<TR>";
    $content = rtrim(ltrim($content))."<TD WIDTH='30'>&nbsp; </TD>";
    $content = rtrim(ltrim($content))."<TD WIDTH='80'><b>LOTE: </b></TD><TD WIDTH='295'><b>$LOTE</b></TD>";
    $content = rtrim(ltrim($content))."<TD WIDTH='170'><b>FECHA DE VENCIMIENTO: </b></TD><TD><b>$FECHA_V</b></TD>";
    $content = rtrim(ltrim($content))."<TD WIDTH='50'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</TD>";
    $content = rtrim(ltrim($content))."</TR>";
    $content = rtrim(ltrim($content))."</TABLE>";
    $content = rtrim(ltrim($content))."</TD></TR>";
    $content = rtrim(ltrim($content))."<TR><TD>&nbsp;&nbsp; </TD></TR>";
    $content = rtrim(ltrim($content))."</TABLE>";
    
    return $content;
}

function footer()
{
    $content = '<br><br>';
    $content = rtrim(ltrim($content))."
    <table style='width: 80%; margin: 10% auto 0;'>
        <tr>
           <td width='90'>
                &nbsp;
           </td>
           <td>
               <img src='http://sistemas.banados.cl/diken/logoReiniger.jpg' width='200'>
           </td>
           <td>
               <p>
                   Senador Jaime Guzmán 3225, Renca, Santiago, Chile.<br>
                   contacto@reiniger.cl
               </p>
           </td>
        </tr>
    </table>";

    return $content;
}


/* Lee campo GET */
if (isset($_REQUEST['NREGUIST']))
{
    $NREGUIST = $_REQUEST['NREGUIST'];
}
/* Lee campo GET */
if (isset($_REQUEST['id']))
{
    $ID_PRODUCCION = $_REQUEST['id'];
}

// Lee producción

$sql                  = "SELECT AR.CODIGO, AR.NOMBRE, CANTIDAD, FECHA_F, FECHA_V FROM vPlanificacion VP LEFT JOIN ART_DB AR ON (AR.NREGUIST = VP.NREGUIST) WHERE ID = $ID_PRODUCCION";

$resultPlanificacion  = sqlsrv_query($db2, $sql);
$valoresPlanificacion = sqlsrv_fetch_array($resultPlanificacion, SQLSRV_FETCH_BOTH);

$CODIGO               = $valoresPlanificacion['CODIGO'];
$NOMBRE               = $valoresPlanificacion['NOMBRE'];
$CANTIDAD             = $valoresPlanificacion['CANTIDAD'];
$FECHA_F              = $valoresPlanificacion['FECHA_F'];
$FECHA_V              = $valoresPlanificacion['FECHA_V'];

// get the HTML
$content  = '';
$titulos  = titulos($CODIGO, $NOMBRE, $CANTIDAD, $FECHA_F, $LOTE=$ID_PRODUCCION, $FECHA_V);

$content  = rtrim(ltrim($content)).'<page>';
$content  = $titulos;

//**************** Fotos NO
$content = rtrim(ltrim($content))."<br><br><br><br><br><br>";
$content = rtrim(ltrim($content))."<table>
<tr>
    <th width=180 style='text-align: center; vertical-align: middle;'>PARAMETROS</th>
    <th width=180 style='text-align: center; vertical-align: middle;'>ESPECIFICACIONES</th>
    <th width=180 style='text-align: center; vertical-align: middle;'>RESULTADOS</th>
    <th width=120 style='text-align: center; vertical-align: middle;'>ESTADO</th>
</tr>";

// Lee Cotizacion
// Realizar una consulta SQL
$sql            = "SELECT RTRIM(LTRIM(PREGUNTA)) PREGUNTA, RTRIM(LTRIM(RESPUESTA)) RESPUESTA, rangos_inf, RTRIM(LTRIM(OPCIONES)) OPCIONES, RANGOS1, RANGOS2
FROM vRespuestasProduccion WHERE ID=$ID_PRODUCCION ORDER BY IDPREGUNTA";

$resultado      = sqlsrv_query($db2, $sql);
/*
if (!$resultado = sqlsrv_query($db2, $sql)) 
{
    echo "Lo sentimos, este sitio web está experimentando problemas.";
    echo "Error: La ejecución de la consulta falló debido a: \n";
    exit;
}
else
{
    */
    $ORDEN            = 1;
    while ($registro  = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC))
    {
           $Pregunta      = $registro['PREGUNTA'];
           $Respuesta     = $registro['RESPUESTA'];
           $rangos_inf    = $registro['rangos_inf'];
           $OPCIONES      = $registro['OPCIONES'];
           $RANGOS1       = $registro['RANGOS1'];
           $RANGOS2       = $registro['RANGOS2'];

           /* Elimina los a */
           $OPCIONES      = htmlentities($OPCIONES);
           $OPCIONES      = preg_replace('/\&(.)[^;]*;/', '\\1', $OPCIONES);
           $Pregunta      = preg_replace('([^A-Za-z0-9 ])', ' ', $Pregunta);

           if ($RANGOS1 != 0)
           {
               $OPCIONES  = $RANGOS1.' - '.$RANGOS2;
           }

           /* Para los rangos */
           $Estado        = 'No Cumple';
           If ($rangos_inf != 0)
           {
               /* Verifica si cumple o no con los rangos */
               $Respuesta = $rangos_inf;
               if ($rangos_inf >= $RANGOS1 && $rangos_inf <= $RANGOS2)
               {
                   $Estado = 'Cumple';
               }
           }
           else
           {
               /* Verifica si cumple o no */
               if ($OPCIONES == $Respuesta)
               {
                   $Estado = 'Cumple';
               }
           }

           $content   = rtrim(ltrim($content))."<tr>";
           $content   = rtrim(ltrim($content))."   <td width=80 style='padding-left: 10px;'>";
           $content   = rtrim(ltrim($content))."          <b>$Pregunta</b>";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td width=80 style='text-align: center; vertical-align: middle;'>";
           $content   = rtrim(ltrim($content))."          $OPCIONES";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td width=80 style='text-align: center; vertical-align: middle;'>";
           $content   = rtrim(ltrim($content))."          $Respuesta";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td width=80 style='text-align: center; vertical-align: middle;'>";
           $content   = rtrim(ltrim($content))."          $Estado";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."</tr>";
           $ORDEN    ++;
	}
//}
$content  = rtrim(ltrim($content))."</table>";

//$content  = rtrim(ltrim($content)).footer();
$content  = rtrim(ltrim($content))."<div style='padding-top: 150px;'>";
$content  = rtrim(ltrim($content))."<table style='width: 90%; margin: 10% auto 0; padding-left: 10%;'>";
$content  = rtrim(ltrim($content))."<tr>";
$content  = rtrim(ltrim($content))."<td width='50'><b> </b></td>";
$content  = rtrim(ltrim($content))."<td><b>_____________________________</b></td>";
$content  = rtrim(ltrim($content))."<td width='150'> </td>";
$content  = rtrim(ltrim($content))."<td><b>_____________________________</b></td>";
$content  = rtrim(ltrim($content))."<td width='50'><b> </b></td>";
$content  = rtrim(ltrim($content))."</tr>";
$content  = rtrim(ltrim($content))."<tr>";
$content  = rtrim(ltrim($content))."<td width='50'><b> </b></td>";
$content  = rtrim(ltrim($content))."<td style='text-align: center;'><b>Analista de Calidad</b></td>";
$content  = rtrim(ltrim($content))."<td width='100'> </td>";
$content  = rtrim(ltrim($content))."<td style='text-align: center;'><b>Encargado</b></td>";
$content  = rtrim(ltrim($content))."<td width='50'><b> </b></td>";
$content  = rtrim(ltrim($content))."</tr>";
$content  = rtrim(ltrim($content))."</table>";
$content  = rtrim(ltrim($content))."</div>";

//$content  = rtrim(ltrim($content)).footer();
$content  = rtrim(ltrim($content))."</div>";


$time     = time();

//echo $content;

 $html2pdf = new HTML2PDF('P','A4','fr');
 $html2pdf->WriteHTML($content);
 ob_end_clean();
 $html2pdf->Output($time.'.pdf');
?>