<?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($NREGUIST, $CODIGO, $NOMBRE, $VOLUMEN, $LITROS, $VIDA_UTIL, $DENSIDAD, $OBSERVACION)
{

    $NOMBRE       = utf8_encode($NOMBRE);
    $LITROS       = number_format($LITROS,  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;}
    </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: 25px; padding-right: 25px;font-size: 13px;'>";

    $LITROS  = $LITROS * 1000;
    $content = rtrim(ltrim($content))."<h2>COMPOSICION DE FORMULA</h2>";
    $content = rtrim(ltrim($content))."<h4>CODIGO: $CODIGO  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PRODUCTO: $NOMBRE</h4><BR>";
    $content = rtrim(ltrim($content))."<TABLE>";
    $content = rtrim(ltrim($content))."<TR><TD><b>MASA (grs.):</b>      </TD><TD><b>$VOLUMEN   </b></TD><TD WIDTH='100'>&nbsp;</TD><TD><b>VOLUMEN (ml.):     </b></TD><TD><b>$LITROS   </b></TD></TR>";
    $content = rtrim(ltrim($content))."<TR><TD><b>VIDA UTIL (días) :</b></TD><TD><b>$VIDA_UTIL </b></TD><TD WIDTH='100'>&nbsp;</TD><TD><b>DENSIDAD (grs./ml.): </b></TD><TD><b>$DENSIDAD </b></TD></TR>";
    $content = rtrim(ltrim($content))."</TABLE>";

    //**************** Fotos NO
//    $content = rtrim(ltrim($content))."<h4>INSTRUCCIONES DE FABRICACION</h4>";
//    $content = rtrim(ltrim($content))."<TEXTAREA rows='6' cols='80'>$OBSERVACION</TEXTAREA>";

    $content = rtrim(ltrim($content))."<h4>DETALLE DE MATERIAS PRIMAS</h4>";
    $content = rtrim(ltrim($content))."
    <table class='table1'>
    <tr>
       <th>Orden</th>
       <th width=80>C&oacute;digo</th>
       <th width=350>Nombre Producto</th>
       <th align=right>Densidad &nbsp;<br>(grs./ml.) &nbsp;</th>
       <th align=right>Peso &nbsp;<br>(grs.) &nbsp;</th>
       <th align=right>Volumen &nbsp;<br>(ml.) &nbsp;</th>
    </tr>";
    
    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      = $_REQUEST['ID'];
}

// Lee datos de la ficha

$sql            = "SELECT AR.NREGUIST, AR.CODIGO, AR.NOMBRE, PCF.VOLUMEN, PCF.LITROS, PCF.VIDA_UTIL, PCF.DENSIDAD, PCF.OBSERVACION, PCF.CAMBIOS
FROM ART_DB AR LEFT JOIN PRODUCCION_PRODUCTOS_CAMPOS PPC ON (PPC.NREGUIST=AR.NREGUIST) LEFT JOIN PRODUCCION_CABECERA_FORMULAS PCF ON (PCF.NREGUIST_PROD_TERMINADO = AR.NREGUIST)
WHERE PCF.ID = $ID";

$resultPicking  = sqlsrv_query($db2, $sql);
$valoresPicking = sqlsrv_fetch_array($resultPicking, SQLSRV_FETCH_BOTH);

$NREGUIST       = $valoresPicking['NREGUIST'];
$CODIGO         = $valoresPicking['CODIGO'];
$NOMBRE         = $valoresPicking['NOMBRE'];
$VOLUMEN        = $valoresPicking['VOLUMEN'];
$CAMBIOS        = $valoresPicking['CAMBIOS'];
$LITROS         = $valoresPicking['LITROS'];
$VIDA_UTIL      = $valoresPicking['VIDA_UTIL'];
$DENSIDAD       = $valoresPicking['DENSIDAD'];
$OBSERVACION    = $valoresPicking['OBSERVACION'];

// get the HTML
$content  = '';
$titulos  = titulos($NREGUIST, $CODIGO, $NOMBRE, $VOLUMEN, $LITROS, $VIDA_UTIL, $DENSIDAD, $OBSERVACION);

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

// Lee Cotizacion
// Realizar una consulta SQL
$sql            = "SELECT PDF.ORDEN, AR.CODIGO, AR.NOMBRE, PDF.DENSIDAD, PDF.PESO, PDF.VOLUMEN FROM PRODUCCION_DETALLE_FORMULAS PDF 
LEFT JOIN ART_DB AR ON (AR.NREGUIST = PDF.NREGUIST_MATERIA_PRIMA) WHERE PDF.NREGUIST_PROD_TERMINADO = $NREGUIST ORDER BY PDF.ORDEN";

$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
{
    */
    while ($registro  = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC))
    {
           $ORDEN     = $registro['ORDEN'];
           $CODIGO    = $registro['CODIGO'];
           $NOMBRE    = $registro['NOMBRE'];
           $DENSIDAD  = $registro['DENSIDAD'];
           $PESO      = $registro['PESO'];
           $VOLUMEN   = $registro['VOLUMEN'];

           $NOMBRE    = utf8_encode($NOMBRE);

           $DENSIDAD  = number_format($DENSIDAD, 4, ',', '.');
           $PESO      = number_format($PESO,     3, ',', '.');
           $VOLUMEN   = number_format($VOLUMEN,  3, ',', '.');

           $content   = rtrim(ltrim($content))."<tr>";
           $content   = rtrim(ltrim($content))."   <td>";
           $content   = rtrim(ltrim($content))."          $ORDEN";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td>";
           $content   = rtrim(ltrim($content))."          $CODIGO";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td>";
           $content   = rtrim(ltrim($content))."          $NOMBRE";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td align=right>";
           $content   = rtrim(ltrim($content))."          $DENSIDAD &nbsp;";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td align=right>";
           $content   = rtrim(ltrim($content))."          $PESO &nbsp;";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td align=right>";
           $content   = rtrim(ltrim($content))."          $VOLUMEN &nbsp;";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."</tr>";
	}
//}
$content  = rtrim(ltrim($content))."</table>";

//**************** Fotos NO
$content = rtrim(ltrim($content))."<h4>PREGUNTAS PARA CONTROL DE CALIDAD</h4>";
$content = rtrim(ltrim($content))."<table class='table1'>
<tr>
    <th>Orden</th>
    <th width=282>Pregunta</th>
    <th width=80>Tipo Pregunta</th>
    <th align=right>Rango Inferior</th>
    <th align=right>Rango Mayor</th>
    <th>Opciones</th>
</tr>";

// Lee Cotizacion
// Realizar una consulta SQL
$sql            = "SELECT ORDEN, PREGUNTA, Case When TIPOPREGUNTA=1 Then 'Texto' Else
Case When TIPOPREGUNTA=2 Then 'Por Rangos' Else Case When TIPOPREGUNTA=3 Then 'Opciones' Else '' END END END TipoPreguntas, Rangos1, Rangos2, Opciones 
FROM vPreguntasFormulas WHERE IDFORMULA=$ID ORDER BY ORDEN";

$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))
    {
           $ORDEN         = $registro['ORDEN'];
           $Pregunta      = $registro['PREGUNTA'];
           $TipoPreguntas = $registro['TipoPreguntas'];
           $Rangos1       = $registro['Rangos1'];
           $Rangos2       = $registro['Rangos2'];
           $Opciones      = $registro['Opciones'];

           if ($TipoPreguntas == 'Por Rangos') 
           {
               $Rangos1       = number_format($Rangos1,  4, ',', '.');
               $Rangos2       = number_format($Rangos2,  4, ',', '.');  
           }
           else
           {
               $Rangos1        = '';
               $Rangos2        = ''; 
           }

           $content   = rtrim(ltrim($content))."<tr>";
           $content   = rtrim(ltrim($content))."   <td>";
           $content   = rtrim(ltrim($content))."          $ORDEN";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td>";
           $content   = rtrim(ltrim($content))."          $Pregunta";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td>";
           $content   = rtrim(ltrim($content))."          $TipoPreguntas";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td align=right>";
           $content   = rtrim(ltrim($content))."          $Rangos1 &nbsp;";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td align=right>";
           $content   = rtrim(ltrim($content))."          $Rangos2 &nbsp;";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."   <td>";
           $content   = rtrim(ltrim($content))."          $Opciones &nbsp;";
           $content   = rtrim(ltrim($content))."   </td>";
           $content   = rtrim(ltrim($content))."</tr>";
	}
//}
$content  = rtrim(ltrim($content))."</table>";

//$content  = rtrim(ltrim($content)).footer();
$content  = rtrim(ltrim($content))."<div style='padding-top: 50px;'>";
$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>Jefe de Producción</b></td>";
$content  = rtrim(ltrim($content))."<td width='100'> </td>";
$content  = rtrim(ltrim($content))."<td style='text-align: center;'><b>Gerente General</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))."<br><br>";
$content = rtrim(ltrim($content))."<TABLE>";
$content = rtrim(ltrim($content))."<TR><TD><b>Cambios:</b></TD><TD><b><textarea name='textarea' rows='3' cols='70'>$CAMBIOS</textarea></b></TD></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');
?>