//          http://www.gnu.org/copyleft/gpl.html                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
require '../../../functions/globales.php';
require '../include/rutas.php';
require '../functions/main.php';
require '../struct/login2.php';
require '../functions/doc_functions.php';
if ($_POST['pdf']) {
    require FPDF_PDF_RUTA . 'lib/pdftable.inc.php';
    $p = new PDFTable('P');
    $p->SetMargins(3, 43, 0);
    $p->SetTitle(date("d-m-Y") . "_revision_direccion");
    $p->SetFillColor(255, 255, 255);
    $p->SetTextColor(0, 0, 0);
    $p->SetDrawColor(0, 0, 0);
    $doc_real = select_normal("Select * from kz_tec_doc_documentos_reales where id = " . $_POST['documento_real'] . "");
    $doc_real = $doc_real[0];
    $p->AddPage('P');
    $p->SetMargins(10, 10, 0);
    $p->setfont('Arial', 'B', 12);
    $color1 = 'A6A6A6';
    $color2 = 'FFFFFF';
    $tit = "bgcolor=#{$color1} color=#{$color2} align=center valign=middle style=bold height=8";
    $nor = "align=center height=5 valign=middle";
    $p->cell(0, 0, $doc_real['titulo'], 0, 0, 'C');
    $p->ln(15);
    $p->setfont('Arial', '', 10);
    $p->MultiCell(188, 5, $doc_real['contenido'], 0, 'J');
    $p->output();
}
 $p = new PDFTable('P');
 $p->SetMargins(3, 43, 0);
 if ($_POST['modo_resumen']) {
     $p->AddPage('L');
     $p->SetMargins(3, 10, 0);
     $p->titulo(5, $titulo);
     $p->setfont('Arial', '', 8);
     $p->SetTitle(date("d-m-Y") . "_informe");
     $p->SetFillColor(255, 255, 255);
     $p->SetTextColor(0, 0, 0);
     $p->SetDrawColor(0, 0, 0);
     $filtrostab = "<table>";
     $filtrostab .= $l_filtros;
     $filtrostab .= "</table>";
     $p->HTMLtable($filtrostab);
     $p->ln(5);
     $tabla_cantidad = "<table>\n\t\t\t\t<tr><td>Cantidad total de Acciones Correctivas: " . count($acpm) . "</td></tr>\t\n\t\t\t</table>";
     $p->HTMLtable($tabla_cantidad);
     $p->ln(5);
     $color1 = 'A6A6A6';
     $color2 = 'FFFFFF';
     $tit = "bgcolor=#{$color1} color=#{$color2} align=center valign=middle style=bold height=8";
     $nor = "align=center height=5 valign=middle";
     if (!$_POST['ag_tipo']) {
         $tabla = "<table border=1>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td {$tit} width=20>Num NC</td>\n\t\t\t\t\t\t<td {$tit} width=23>Fecha apertura</td>\n\t\t\t\t\t\t<td {$tit} width=140>Causa probable</td>\n\t\t\t\t\t\t<td {$tit} width=54>" . html_entity_decode("Tipo acci&oacute;n") . "</td>\n\t\t\t\t\t\t<td {$tit} width=15>Coste</td>\n\t\t\t\t\t\t<td {$tit} width=19>Fecha cierre</td>\n\t\t\t\t\t\t<td {$tit} width=19>Cierre eficaz</td>\n\t\t\t\t\t</tr>";
     }
 }
 if ($acpm) {
     foreach ($acpm as $key => $valor) {
         if (!$_POST['modo_resumen']) {
             $p->AddPage('P');
 $color1 = 'A6A6A6';
 $color2 = 'FFFFFF';
 $tit = "bgcolor=#{$color1} color=#{$color2} align=center valign=middle style=bold height=8";
 $contenido = "align=center height=5 valign=middle";
 $tabla = "<table border=1>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit} width=204>" . html_entity_decode("&Oacute;rdenes") . " del " . html_entity_decode("d&iacute;a") . " pendientes</td>\n\t\t\t\t</tr>";
 $temas_pendientes = select_normal("Select distinct(a.tema), a.id, c.departamento from kz_tec_dir_temas a, kz_tec_dir_reuniones c where a.id in (Select b.idtema from kz_tec_dir_temasreunion b where cerrado = 0 and c.id = b.idreunion) and c.departamento = '" . $valor['departamento'] . "'");
 if ($temas_pendientes) {
     foreach ($temas_pendientes as $key2 => $valor2) {
         $tabla .= "<tr>\n\t\t\t\t\t\t\t<td {$contenido}>" . $valor2['tema'] . "</td>\n\t\t\t\t\t\t</tr>";
     }
 } else {
     $tabla .= "<tr><td>No hay " . html_entity_decode("&oacute;rdenes del d&iacute;a") . " pendientes</td></tr>";
 }
 $tabla .= "</table>";
 $p->htmltable($tabla);
 $p->ln(2);
 $color1 = 'A6A6A6';
 $color2 = 'FFFFFF';
 $color3 = 'CECECE';
 $tit = "bgcolor=#{$color1} color=#{$color2} align=center valign=middle style=bold height=8";
 $tit2 = "bgcolor=#{$color3} color=#{$color2} align=center valign=middle style=bold height=8";
 $contenido = "align=center height=5 valign=middle";
 $tabla = "<table border=1>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit} colspan=3>Decisiones pendientes</td>\n\t\t\t\t</tr>";
 $dec_pendientes = select_normal("Select distinct(a.decision),  a.id, c.departamento, d.responsable, d.plazo from kz_tec_dir_decisiones a, kz_tec_dir_reuniones c, kz_tec_dir_decisionesreunion d where a.id in (Select b.iddecision from kz_tec_dir_decisionesreunion b where cerrado = 0 and c.id = b.idreunion) and c.departamento = '" . $valor['departamento'] . "' and d.iddecision = a.id and d.idreunion = c.id order by plazo desc");
 if ($dec_pendientes) {
     foreach ($dec_pendientes as $key2 => $valor2) {
         $tabla .= "<tr>\n\t\t\t\t\t\t\t<td {$tit2} width=132>" . html_entity_decode("Decisi&oacute;n") . "</td>\n\t\t\t\t\t\t\t<td {$tit2} width=55>Responsable</td>\n\t\t\t\t\t\t\t<td {$tit2} width=17>Plazo</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td {$contenido}>" . $valor2['decision'] . "</td>\n\t\t\t\t\t\t\t<td {$contenido}>" . $valor2['responsable'] . "</td>\n\t\t\t\t\t\t\t<td {$contenido}>" . $valor2['plazo'] . "</td>\n\t\t\t\t\t\t</tr>";
     }
 } else {
     $tabla .= "<tr><td width=204>No hay decisiones pendientes</td></tr>";
 }
 require FPDF_PDF_RUTA . 'lib/pdftable.inc.php';
 $p = new PDFTable('P');
 $p->SetMargins(3, 43, 0);
 if ($equipos) {
     foreach ($equipos as $key => $valor) {
         $p->AddPage('P');
         $p->SetMargins(3, 10, 0);
         $p->titulo(5, $titulo);
         $p->setfont('Arial', '', 8);
         $color1 = 'A6A6A6';
         $color2 = 'FFFFFF';
         $tit = "bgcolor=#{$color1} color=#{$color2} border=1 width=30%";
         $nor = "width=50%";
         $tabla = "<table>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>" . html_entity_decode("N&deg; equipo") . "</td>\n\t\t\t\t\t<td {$nor}>" . $valor['numero'] . "</td>\n\t\t\t\t\t<td {$tit}>" . html_entity_decode("Categor&iacute;a") . "</td>\n\t\t\t\t\t<td {$nor}>" . $valor['categoria'] . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>" . html_entity_decode("A&ntilde;o fab") . ".</td>\n\t\t\t\t\t<td {$nor}>" . $valor['anofab'] . "</td>\n\t\t\t\t\t<td {$tit}>Estado del equipo</td>\n\t\t\t\t\t<td {$nor}>" . $valor['estado'] . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>Ref.</td>\n\t\t\t\t\t<td {$nor}>" . $valor['ref'] . "</td>\n\t\t\t\t\t<td {$tit}>" . html_entity_decode("Ubicaci&oacute;n") . "</td>\n\t\t\t\t\t<td {$nor}>" . $valor['ubicacion'] . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>Fabricante</td>\n\t\t\t\t\t<td {$nor}>" . $valor['fab'] . "</td>\n\t\t\t\t\t<td {$tit}>Fecha adq.</td>\n\t\t\t\t\t<td {$nor}>" . $valor['fechaadq'] . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>Modelo</td>\n\t\t\t\t\t<td {$nor}>" . $valor['modelo'] . "</td>\n\t\t\t\t\t<td {$tit}>Precio adq.</td>\n\t\t\t\t\t<td {$nor}>" . $valor['precio'] . " euros</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>Tipo</td>\n\t\t\t\t\t<td {$nor}>" . $valor['tipo'] . "</td>\n\t\t\t\t\t<td {$tit}>S/N</td>\n\t\t\t\t\t<td {$nor}>" . $valor['sn'] . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>Referencia</td>\n\t\t\t\t\t<td {$nor}>" . $valor['referencia'] . "</td>\n\t\t\t\t\t<td {$tit}>Fecha retirada</td>\n\t\t\t\t\t<td {$nor}>" . $valor['fecharetirada'] . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>Elemento</td>\n\t\t\t\t\t<td {$nor}>" . $valor['elemento'] . "</td>\n\t\t\t\t\t<td {$tit}>" . html_entity_decode("Funci&oacute;n") . "</td>\n\t\t\t\t\t<td {$nor}>" . $valor['funcion'] . "</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit}>" . html_entity_decode("Descripci&oacute;n") . "</td>\n\t\t\t\t\t<td {$nor}>" . $valor['descripcion'] . "</td>\n\t\t\t\t\t<td {$tit}>CEE</td>\n\t\t\t\t\t<td {$nor}>" . $valor['cee'] . "</td>\n\t\t\t\t</tr>\n\t\t\t</table>";
         $p->htmltable($tabla);
         $p->ln(10);
         $pautas = select_normal("Select * from kz_tec_mant_correctivo where equipo = " . $valor['id'] . "");
         $color1 = 'A6A6A6';
         $color2 = 'FFFFFF';
         $tit = "bgcolor=#{$color1} color=#{$color2} align=center valign=middle style=bold height=8";
         $nor = "align=center height=5 valign=middle";
         $tabla = "<table border=1>\n\t\t\t\t<tr>\n\t\t\t\t\t<td border=0 colspan=5 style=bold>Mantenimiento correctivo</td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td {$tit} width=17>Fecha</td>\n\t\t\t\t\t<td {$tit} width=11>Euros</td>\n\t\t\t\t\t<td {$tit} width=85>Observaciones</td>\n\t\t\t\t\t<td {$tit} width=80>Materiales</td>\n\t\t\t\t\t<td {$tit} width=11>Horas</td>\n\t\t\t\t</tr>";
         $sumaeuros = 0;
         $sumahoras = 0;
         if (!$pautas) {
             $tabla .= "<tr><td colspan=5>No hay registros de mantenimiento correctivo</td></tr>";
         } else {
             foreach ($pautas as $keypau => $valorpau) {
                 $tabla .= "<tr>\n\t\t\t\t\t\t<td {$nor}>" . $valorpau['fecha_mant'] . "</td>\n\t\t\t\t\t\t<td {$nor}>" . $valorpau['euros'] . "</td>\n\t\t\t\t\t\t<td {$nor}>" . $valorpau['observaciones'] . "</td>\n\t\t\t\t\t\t<td {$nor}>" . $valorpau['materiales'] . "</td>\n\t\t\t\t\t\t<td {$nor}>" . $valorpau['horas'] . "</td>\n\t\t\t\t\t</tr>";
                 $sumahoras = $sumahoras + $valorpau['horas'];
                 $sumaeuros = $sumaeuros + $valorpau['euros'];
if ($equipos) {
    if ($_POST['pdf']) {
        require FPDF_PDF_RUTA . 'lib/pdftable.inc.php';
        $p = new PDFTable('P');
        $p->SetMargins(3, 43, 0);
        $p->AddPage('P');
        $p->SetMargins(3, 10, 0);
        $p->titulo(5, $titulo);
        $p->setfont('Arial', '', 8);
        $p->SetTitle(date("d-m-Y") . "_informe");
        $p->SetFillColor(255, 255, 255);
        $p->SetTextColor(0, 0, 0);
        $p->SetDrawColor(0, 0, 0);
        $p->Cell(0, 3, "Fecha inicio: " . $_POST['fecha1'], 0, 1);
        $p->Cell(0, 3, "Fecha fin: " . $_POST['fecha2'], 0, 1);
        $p->ln(5);
        if ($equipos) {
            foreach ($equipos as $key => $valor) {
                $mant = select_normal("Select * from kz_tec_mant_correctivo where equipo = " . $valor['id'] . " {$filtrosmant} order by fecha_mant desc");
                if ($mant) {
                    $color1 = 'A6A6A6';
                    $color2 = 'FFFFFF';
                    $tit = "bgcolor=#{$color1} color=#{$color2} align=center valign=middle style=bold height=8";
                    $nor = "align=center height=5 valign=middle";
                    $tabla = "<table border=1>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td {$tit} width=34>" . html_entity_decode("N&uacute;mero equipo") . "</td>\n\t\t\t\t\t\t\t<td {$tit} width=34>Ref.</td>\n\t\t\t\t\t\t\t<td {$tit} width=34>Fabricante</td>\n\t\t\t\t\t\t\t<td {$tit} width=34>Modelo</td>\n\t\t\t\t\t\t\t<td {$tit} width=34>Tipo</td>\n\t\t\t\t\t\t\t<td {$tit} width=34>Referencia</td>\n\t\t\t\t\t\t</tr>";
                    $tabla .= "<tr>\n\t\t\t\t\t\t\t<td {$nor}>" . $valor['numero'] . "</td>\n\t\t\t\t\t\t\t<td {$nor}>" . $valor['ref'] . "</td>\n\t\t\t\t\t\t\t<td {$nor}>" . $valor['fab'] . "</td>\t\n\t\t\t\t\t\t\t<td {$nor}>" . $valor['modelo'] . "</td>\n\t\t\t\t\t\t\t<td {$nor}>" . $valor['tipo'] . "</td>\t\n\t\t\t\t\t\t\t<td {$nor}>" . $valor['referencia'] . "</td>\n\t\t\t\t\t\t</tr>";
                    $tabla .= "</table>";
                    $p->SetMargins(10, 43, 0);
                    $p->htmltable($tabla);
                    $p->ln(5);
                    if ($mant) {