<?php
echo"<html>";
echo"<html onMouseOver=\"self.status='CORRESERVICIOS & CIA LTDA '; return true\">";
echo '<script language="javascript">

window.moveTo(0,0);
window.resizeTo(screen.availWidth,screen.availHeight);
</script> ';
echo"<head>";
echo"<title>.::Sistema WEBSIM GAMA::. </title>";
echo"<meta name=\"author\" content='DESARROLLO WEB'>";
echo"<link rel='stylesheet' href='css/style.css' type=\"text/css\">";
echo"<link rel=\"SHORTCUT ICON\" href=imagenes/vc.png>";
echo"</head>";
echo"<body leftmargin=\"0\" topmargin=\"0\" marginheight=\"0\" marginwidth=\"0\" bgcolor=\"#ffffff\">";

include("conexion/conexion.php");
include("Menu/menu_inicio.php");
include("funciones/funciones_backup.php");
include("logos.php");

$guia = $_GET['guia'];
if (isset($_GET['prod'])) {
    $prod = $_GET['prod'];
} else {
    $prod = '';
}
$pagina = 'DETALLE GUIA No ' . $guia;
?>
<style type="text/css">
    td.comment{
        padding:0pt;
        padding-bottom:0pt;
        margin:0pt;  
    }
    .comment{
        margin:0pt;	
        width:500px;
        height:auto !important;
        overflow:scroll !important;
    }
</style>
<CENTER>
    <table width="800" border="0" cellpadding="0" cellspacing='0' class="primaria">
        <tr class=miTable_tr>
            <td colspan="4" class=miTable_th1><div align="center">..:: <?php echo $pagina; ?> ::..</div></td>
        </tr>
        <?php

        class actualizarEstado {

            var $consultarObj;
            var $conexionObj;
            var $estado;

            function validar($guia) {
                $this->userObj = new select_zonas();
                $this->conexionObj = new conexion();
                $this->conexionObj->conectarse();
                $rst_validar = $this->conexionObj->consultar("SELECT * FROM rastreo_guias WHERE guia='" . $guia . "' ");
                //echo "<br>";
                if (mysql_num_rows($rst_validar) >= 1) {
                    //echo '<center><table width="500" border="0" class="primaria">';                    
                    echo '<table width="500" border="0" cellpadding="0" cellspacing="0" class="primaria">';
                    echo '<tr class=miTable_tr>';
                    echo '<td colspan="2" class=miTable_th1><div align="center">..:: RASTREO DEL ENVIO ::..</div></td>';
                    echo '</tr>';
                    echo '<td class=miTable_th1>ESTADO</td>';
                    echo '<td class=miTable_th1>FECHA</td>';
                    echo '</tr>';
                    while ($rastreo = mysql_fetch_array($rst_validar)) {
                        $rst_estado = $this->conexionObj->consultar("SELECT id_estado, nombre FROM estados_gestion order by id_estado");
                        while ($estado = mysql_fetch_array($rst_estado)) {
                            $id_estado = 'E' . $estado[0];
                            $fecha_estado = 'F' . $estado[0];
                            $datos[] = array('estado' => $estado[1], 'fecha' => $rastreo[$fecha_estado], 'sucursal' => $rastreo[19]);
                        }
                        foreach ($datos as $llave => $fila) {
                            $estado[$llave] = $fila['estado'];
                            $fecha[$llave] = $fila['fecha'];
                            $sucursal[$llave] = $fila['sucursal'];
                        }
                        foreach ($datos as $llave => $fila) {
                            if ($fila['fecha'] > 0) {
                                echo '<tr class=miTable_tr>';
                                echo '<td class=miTable_td>' . $fila['estado'] . '</td>';
                                echo '<td class=miTable_td>' . $fila['fecha'] . '</td>';
                                echo '</tr>';
                            }
                        }
                    }
                    echo '<tr class=miTable_tr>';
                    echo '<td colspan="3" class=miTable_th1></td></tr>';
                    echo '</table>';
                    echo '</td>';

                    /////////Rastreo Reenvios/////////
                    $rst_reenvio = $this->conexionObj->consultar("SELECT * FROM reenvio as a inner join 
			mensajeros as b on b.id_mensajero=a.id_mensajero WHERE guia='" . $guia . "' order by id desc");
                    if (mysql_num_rows($rst_reenvio) >= 1) {
                        while ($rastreo_reenvio = mysql_fetch_array($rst_reenvio)) {
                            //////dibujo tabla
                            echo '<br><br><center><table width="300" border="0" class="primaria"><tr bgcolor="Red">
				<td><font color="white"><div align="center">
				..:: Informacion Reenvio ::..</div></font></td></tr>';
                            echo '</tr>';
                            echo '<td class=miTable_td> <div align="center"> Mensajero:' . $rastreo_reenvio[22] . ' </div></td>';
                            echo '</table>';
                            echo '</center>';
                            echo '<center><table width="500" border="0" class="primaria">';
                            echo '<tr class=miTable_tr>';
                            echo '<td class=miTable_th1>ESTADO</td>';
                            echo '<td class=miTable_th1>FECHA</td>';
                            echo '</tr>';
                            ///////////
                            $rst_estados = '';
                            $data = '';
                            $id_estado = '';
                            $fecha_estado = '';
                            $cursor = '';
                            $rst_estados = $this->conexionObj->consultar("SELECT id_estado, nombre FROM estados_gestion");
                            while ($est = mysql_fetch_array($rst_estados)) {
                                $id_estado = 'E' . $est[0];
                                $fecha_estado = 'F' . $est[0];
                                @$data[] = array('estado' => $est[1], 'fecha' => $rastreo_reenvio[$fecha_estado]);
                            }
                            foreach ($data as $llave => $fila) {
                                $estado[$llave] = $fila['estado'];
                                $fecha[$llave] = $fila['fecha'];
                                @$nombre[$llave] = $fila['nombre'];
                            }
                            array_multisort($fecha, SORT_ASC, $estado, SORT_ASC, $data);
                            //print_r($datos);					
                            //$cursor=array_elimina_duplicados($data,"fecha");
                            $cursor = $data;
                            foreach ($cursor as $llave => $fila) {
                                if ($fila['fecha'] > 0) {
                                    echo '<tr class=miTable_tr>';
                                    echo '<td class=miTable_td>' . $fila['estado'] . '</td>';
                                    echo '<td class=miTable_td>' . $fila['fecha'] . '</td>';
                                    //echo '<td class=miTable_td>'.$fila['nombre'].'</td>';							
                                    echo '</tr>';
                                }
                            }

                            echo '<tr class=miTable_tr>';
                            echo '<td colspan="3" class=miTable_th1></td></tr>';
                            echo '</table></center>';
                        }
                    }///////////FIN RASTREO REENVIOS///////
                } else {
                    $msgBOX2 = "No Se Encontro Estados de la Guia.";
                    echo "<script>
	             	  alert('$msgBOX2');
					   window.close();
	  		  	      </script>";
                }
            }

        }

        class guia_consulta {

            var $conexionObj2;

            function url_exists($url) {
                $handle = @fopen($url, "r");
                if ($handle === false)
                    return false;
                fclose($handle);
                return true;
            }

            function detalle_guia($tabla, $guia, $prod) {
                include("logos.php");                                
                $this->conexionObj2 = new conexion();
                $this->conexionObj2->conectarse();
                $rst_select_lotes = $this->conexionObj2->consultar("SELECT gp.id_conse, gp.guia, gp.tipo_guia, gp.id_estado, 
			           gp.id_devolucion, gp.id_cliente, 
					   gp.orden, gp.id_origen, gp.id_destino, gp.nom_zona, gp.cod_zona, gp.id_sucursal, 
					   gp.fecha_cargue, gp.hora_cargue, gp.id_mensajero,gp.id_impresion, gp.id_planilla,
					   gp.id_manifiesto, gp.id_usuario, gp.nom_cliente, gp.empresa, gp.destinatario, 
					   gp.direccion, gp.identificacion, gp.especial1, gp.especial2, gp.especial3, gp.especial4, 
					   gp.especial5,gp.especial6,gp.especial7,gp.especial8,gp.especial9,gp.especial10,
                                           gp.telefono, gp.ciudad, gp.path,m.identificacion as cod_men, m.nombre as nom_men, s.nombre as nom_suc,
					   d.nombre as nom_dev, o.producto, e.nombre as nom_est, gp.cod_zona,gp.no_paquete                                          
				FROM guias_proceso as gp
					 inner join ordenes as o on (gp.orden=o.orden)
					 left outer join estados_gestion as e on (gp.id_estado=e.id_estado)
					 left outer join mensajeros as m on (gp.id_mensajero=m.id_mensajero)
					 left outer join sucursales as s on (gp.id_sucursal=s.id_sucursal)
					 left outer join devoluciones as d on (gp.id_devolucion=d.id_devolucion)					 
				WHERE gp.guia = '$guia'");
                $perfil = $_SESSION['perfil'];
                while ($row_select_lotes = mysql_fetch_array($rst_select_lotes)) {
                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tguia . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['guia'];

                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tplanilla . "</td>";
                    echo "<td width=275  valign=top class=miTable_td>" . $row_select_lotes['id_planilla'] . "</td></tr>";
                    echo "</td></tr>";


                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Torden . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['orden'];
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tdevolucion . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['nom_dev'] . "</td></tr>";
                    echo "</td></tr>";

                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tempresa . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['empresa'];
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Testado . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['nom_est'] . "</td></tr>";
                    echo "</td></tr>";

                    $f_entrega = '00-00-0000';
                    if (!empty($row_select_lotes['especial5']) and $row_select_lotes['especial5'] != '0000-00-00') {
                        $f_entrega = date("d-m-Y", strtotime($row_select_lotes['especial5']));
                    }
                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tdestinatario . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['destinatario'];
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tidentificacion . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['identificacion'] . "</td></tr>";
                    echo "</td></tr>";


                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tdireccion . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['direccion'];
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Ttelefono . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['telefono'];
                    echo "</td></tr>";


                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tproducto . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['producto'];
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tzona . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['nom_zona'] . "</td></tr>";
                    echo "</td></tr>";


                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tciudad . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['ciudad'];
                    echo "<td width=100 valign=top height=42 class=miTable_th1>CODIGO ZONA</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['cod_zona'] . "</td></tr>";
                    echo "</td></tr>";

                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tmensajero . "</td>";
                    echo "<td valign=top class=miTable_td>" . $row_select_lotes['cod_men'];
                    if (!empty($Tespecial5)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial5 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $f_entrega . "</td></tr>";
                    }
                    echo "</td></tr>";

                    echo "<tr class=miTable_tr>";
                    echo "<td width=100 valign=top height=42 class=miTable_th1>REGIMEN:</td>";
                    echo "<td valign=top class=miTable_td>MENSAJERIA EXPRESSA</td>";
                    if (!empty($Tespecial1)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial1 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial1'] . "</td>";
                    }
                    if (!empty($Tespecial4)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial4 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial4'] . "</td></tr>";
                    }
                    echo "</td></tr>";


                    if (!empty($Tespecial3)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial3 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial3'] . "</td>";
                    }

                    if (!empty($Tespecial2)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial2 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial2'] . "</td>";
                    }
                    echo "</td></tr>";


                    if (!empty($Tespecial6)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial6 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial6'] . "</td>";
                    }

                    if (!empty($Tespecial7)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial7 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial7'] . "</td>";
                    }
                    echo "</td></tr>";

                    if (!empty($Tespecial8)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial8 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial8'] . "</td>";
                    }

                    if (!empty($Tespecial9)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial9 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial9'] . "</td>";
                    }
                    echo "</td></tr>";
                    if (!empty($Tespecial10)) {
                        echo "<td width=100 valign=top height=42 class=miTable_th1>" . $Tespecial10 . "</td>";
                        echo "<td valign=top class=miTable_td>" . $row_select_lotes['especial10'] . "</td>";
                    }

                    echo "</td></tr>";

                    echo "<td>";
                    echo "<td>";

                    ##if($perfil!=5){

                    echo "</tr>";
                    echo "</td>";
                    echo "</table>";
                    ##}
                    echo '<br>';    
                    $actualizarEstadoObj = new actualizarEstado();
                    $actualizarEstadoObj->validar($_GET['guia']);
                    ?>
                    <style type="text/css">
                        #container { position: relative; }
                        .mask { position: absolute; top: 0; left: 0; }
                    </style>
                    <CENTER>
                        <?php
                        $sql_ip = "select ip_publica from info_empresa ";
                        $serv = ip_pub($sql_ip);
                        $ruta = $row_select_lotes['path'];
                        $rtatest = trim($row_select_lotes['path']);
                        $ruta2 = '..' . trim($row_select_lotes['path']);
                        if ($ruta == '') {
                            echo"<TABLE width='170' border=0>";
                            echo"<TR>";
                            echo"<th class=miTable_th1>Esta Guia No Posee Imagen";
                            echo "</th>";
                            echo"</TR>";
                            echo"</TABLE>";
                        } else {
                            if ($this->url_exists($ruta2)) {
                                $fp = true;
                            } else {
                                $fp = false;
                            }
                            if (!$fp) {
                                $sql_ip = "select direccion_ip from direccion_ip";
                                $rst_ip = $this->conexionObj2->consultar($sql_ip);
                                $row = mysql_num_rows($rst_ip);
                                if ($row > 0) {
                                    while ($row_ip = mysql_fetch_array($rst_ip)) {
                                        $id_ip = trim($row_ip['direccion_ip']);
                                        $ruta2 = "http://" . $id_ip . $row_select_lotes['path'];
                                        $ImgTif = $id_ip . $row_select_lotes['path'];
                                        $archivo = @fopen($ruta2, 'r');
                                        if ($archivo) {
                                            $fp = true;
                                            fclose($archivo);
                                            break;
                                        }
                                    }
                                }
                            }


                            if ($fp) {
                                @list($ancho, $altura, $tipo, $atr) = getimagesize($ruta2);
                                echo"<TABLE width='$ancho' height='$altura px' border=0>";
                                echo"<TR>";
                                echo"<th>
					<div id=container>
						<img src='" . $ruta2 . "' border=1 width='$ancho' height='$altura px' title='GUIA No. $guia' >
					</div></div>";
                                echo "</th>";
                                echo"</TR>";
                                echo"</TABLE>";
                            } else {
                                echo"<TABLE width='170' border=0>";
                                echo"<TR>";
                                echo"<th class=miTable_th1>La Imagen no se encuentra en la ruta especificada. ";
                                echo "</th>";
                                echo"</TR>";
                                echo"</TABLE>";
                            }
                        }
                        ?>
                    </CENTER>
                    <?PHP
                }
            }

        }

        $tabla = $_SESSION['tabla_guia'];
        $guia_consultaObj = new guia_consulta();
        $guia_consultaObj->detalle_guia($tabla, $guia, $prod);

        //$actualizarEstadoObj = new actualizarEstado();
        //$actualizarEstadoObj->validar($_GET['guia']);
        ?>
        <center>
            <input type="button" value="Cerrar" onclick="self.close()" />
            <br>
            <br>
        </center>
    </table>    
</CENTER>
<?php
echo"<table width=300 border=0 align=center cellpadding=0 cellspacing=0>";
echo"<tbody><tr>";
echo"<td></td>";
echo"</tr>";
echo"<tr>";
?>
<td>
<CENTER>
    <?php
    pie_de_pagina();
    ?>
</CENTER>
</td>
<?php
echo"</tr>";
echo"</tbody></table>";
echo"</body>";
echo"</html>";
?>
