<?php
$target = 'guia';
include("funciones/funciones_backup.php");
include("conexion/conexion.php");
include("Menu/menu_inicio.php");
include("Usuarios/mensajeros.php");
include("logos.php");
unset($_SESSION['cadena_csv']);
unset($_SESSION['nombcsv']);
$fecha_actual = date("Y-m-d");
$_SESSION['nombcsv']='ConsultaOrdenArea_' . $fecha_actual . '.csv';
$_SESSION['cadena_csv']= "ORDEN;AREA;";
$nombrepagina = "CONSULTA ORDEN - AREA";
encabezado($target);
$user = $_SESSION['validar'];
$filas = 1;
if ($filas == 1) {

    class llamado {

        var $menuObj;

        function validar1($id_user, $id_sucursal) {
            $this->menuObj = new menu_inicio();
            $this->menuObj->menu2($id_user, $id_sucursal);
        }

    }

    $llamadoObj = new llamado();
    $llamadoObj->validar1($_GET['id_user'], $_GET['suc']);
    entrada($user, $_GET['id_user'], $_GET['suc']);
    echo"</td>";
    echo"<td width=79% background=\"imagenes/bg3.jpg\" valign=top>";
    mostrar_cabeza_trabajo($nombrepagina);

    class obtiene {

        var $userObj;

        function obtener($perfil) {
            $this->userObj = new select_zonas();
            $tabla = $this->userObj->get_tabla($_GET['suc']);
            $_SESSION['tabla_guia'] = $tabla;
        }

    }
    ?>
    <link rel="stylesheet" href="css/demos.css" media="screen" type="text/css">
    <link rel="stylesheet" href="css/modal-message.css" type="text/css">
    <script type="text/javascript" src="js/modal-message.js"></script>
    <script type="text/javascript">
        messageObj = new DHTML_modalMessage();	// We only create one object of this class
        messageObj.setShadowOffset(0);	// Large shadow
        function displayStaticMessage(messageContent, cssClass, src)
        {


            document.backup.guia.disabled = true;
            messageObj.setHtmlContent(messageContent);
            messageObj.setSize(450, 120);
            messageObj.setCssClassMessageBox(cssClass);
            messageObj.setSource(false);	// no html source since we want to use a static message here.
            messageObj.setShadowDivVisible(false);	// Disable shadow for these boxes
            messageObj.display();

        }
        function cierraMessage()
        {
            messageObj.close();
            document.backup.guia.disabled = false;
            document.backup.guia.focus();
        }


        function reproducir() {
            document.all['cancion'].src = 'sound/alert.wav'
        }

    </script>
    <script language="javascript" src="js/popcalendar.js"></script>
    <form action="" method="POST" name="backup"><CENTER>
            <table width="300" border="0" class="primaria">

                <tr class=miTable_tr>
                    <td colspan="2" class=miTable_th1><div align="center">..:: RANGO DE FECHAS ::..</div></td>
                </tr>


                <tr class="miTable_tr">
                    <td width="200" class="miTable_td">Fecha Inicial:</td>
                    <td width="200" class="miTable_td">
                        <INPUT type="hidden" name="id_user" value="<? echo $_GET['id_user']; ?>">
                        <INPUT type="hidden" name="suc" value="<? echo $_GET['suc']; ?>">
                        <INPUT type="hidden" name="tabla" value="<? echo $tabla; ?>">
                        <input type="text" name="f1" id="dates1" onClick="popUpCalendar(this, backup.dates1, 'yyyy-mm-dd');" class="inputbox" value="<? echo $fecha_actual; ?>">
                    </td>
                </tr>
                <tr class="miTable_tr">
                    <td width="200" class="miTable_td">Fecha Final:</td>
                    <td width="200" class="miTable_td">
                        <input type="text" name="f2" id="dates2" onClick="popUpCalendar(this, backup.dates2, 'yyyy-mm-dd');" class="inputbox" value="<? echo $fecha_actual; ?>">
                    </td>
                </tr>
<tr class="miTable_tr">
                    <td width="200" class="miTable_td">Area:</td>
                    <td width="200" class="miTable_td">
                        <input type="text" name="area" id="area" class="inputbox" value="" placeholder="No. de area">
                    </td>
                </tr>


                <tr class=miTable_tr>
                    <td colspan="2" class=miTable_th1><div align="center">..:: ORDEN - AREA ::..</div></td>
                </tr>

                <tr class="miTable_tr">
                    <td width="100" class="miTable_td">
                        No ODS:
                    </td>
                    <td width="200" class="miTable_td">
                        <textarea cols="20" rows="7" name="arr_ods" id="guia"></textarea>
                    </td>
                </tr>
                <tr class=miTable_tr>
                    <td class=miTable_th1 colspan="2">
                        <div align="center">
                            <input type="submit" name="Registrar" value="Consultar" class="inputbox">
                        </div>
                    </td>
                </tr>
            </table>
            
           <table>
                    <tr class="miTable_tr">
                    <td colspan='4' class="miTable_td">
                <center>                    
                    <a href='guarda_csv.php' title='Exportar CSV'>Exportar Csv<img src='imagenes/txt_icon.gif'></a>
                </center>
                </td>
           </table>
            
            
            <?
            $db = new conexion();
            $db->conectarse();

            function titulos() {
                $db = new conexion();
                $db->conectarse();
                echo "<div style='overflow-x:scroll;overflow-y:scroll;width:720px;height:400px'>";
                echo "<table border='0' width='720' class='primaria'>";
                echo "<tr class=miTable_tr>";
                echo "<td width=60 valign=top class=miTable_th1>ORDEN</td>";
                echo "<td width=140 valign=top class=miTable_th1>AREA</td>";
                $rsEst = $db->consultar("select * from estados_gestion where id_estado in ('12','13','14','15','16','17','18')");

                while ($ret = mysql_fetch_array($rsEst)) {
                    echo "<td valign=top class=miTable_th1 nowrap='nowrap'>" . $ret['nombre'] . "</td>";
                    $_SESSION['cadena_csv'] .= $ret['nombre'] . ';';
                }
		    echo "<td valign=top class=miTable_th1 nowrap='nowrap'>Devoluciones sin enviar</td>";
                    $_SESSION['cadena_csv'] .=  'Devoluciones sin enviar;';
                $_SESSION['cadena_csv'] = substr($_SESSION['cadena_csv'], 0, -1);
                $_SESSION['cadena_csv'] .=chr(13).chr(10);

                $db->cerrar();
            }

            function DibujaOds($num_ods) {
                $db = new conexion();
                $db->conectarse();

$filtro="";
$areafi=$_POST['area'];
if(!empty($areafi)){
	$filtro = " and especial2='".$_POST['area']."' ";
}

                $rs = $db->consultar("select distinct(especial2) from guias_proceso where orden='" . $num_ods . "'".$filtro);
                  while ($res = mysql_fetch_array($rs)) {
                    $area = $res[0];
                  
                    $_SESSION['cadena_csv'] .=$num_ods.';'.trim($area).';';
                    echo '<tr class="miTable_tr">';
                    echo "<td valign=top class=miTable_td><div align='center'>" . $num_ods . "</div></td>";
                    echo "<td valign=top class=miTable_td><div align='center'>" . $area . "</div></td>";
                    $ret = $db->consultar("select count(*) from guias_proceso where orden='" . $num_ods . "' and especial2='" . $area . "' and id_estado='12'".$filtro);
                    while ($row = mysql_fetch_array($ret)) {
                        echo "<td valign=top class=miTable_td><div align='center'>" . $row[0] . "</div></td>";
                        $_SESSION['cadena_csv'] .=$row[0].';';
                    }

                    $ret = $db->consultar("select count(*) from guias_proceso where orden='" . $num_ods . "' and especial2='" . $area . "' and id_estado='13'".$filtro);
                    while ($row = mysql_fetch_array($ret)) {
                        echo "<td valign=top class=miTable_td><div align='center'>" . $row[0] . "</div></td>";
                        $_SESSION['cadena_csv'] .=$row[0].';';
                    }

                    $ret = $db->consultar("select count(*) from guias_proceso where orden='" . $num_ods . "' and especial2='" . $area . "' and id_estado in('14','21')".$filtro);
                    while ($row = mysql_fetch_array($ret)) {
                        echo "<td valign=top class=miTable_td><div align='center'>" . $row[0] . "</div></td>";
                        $_SESSION['cadena_csv'] .=$row[0].';';
                    }

                    $ret = $db->consultar("select count(*) from guias_proceso where orden='" . $num_ods . "' and especial2='" . $area . "' and id_estado in('15','23')".$filtro);
                    while ($row = mysql_fetch_array($ret)) {
                        echo "<td valign=top class=miTable_td><div align='center'>" . $row[0] . "</div></td>";
                        $_SESSION['cadena_csv'] .=$row[0].';';
                    }

                    $ret = $db->consultar("select count(*) from guias_proceso where orden='" . $num_ods . "' and especial2='" . $area . "' and id_estado in('16','22')".$filtro);	
                    while ($row = mysql_fetch_array($ret)) {
                        echo "<td valign=top class=miTable_td><div align='center'>" . $row[0] . "</div></td>";
                        $_SESSION['cadena_csv'] .=$row[0].';';
                    }

                    $ret = $db->consultar("select count(*) from guias_proceso where orden='" . $num_ods . "' and especial2='" . $area . "' and id_estado='17'".$filtro);
                    while ($row = mysql_fetch_array($ret)) {
                        echo "<td valign=top class=miTable_td><div align='center'>" . $row[0] . "</div></td>";
                        $_SESSION['cadena_csv'] .=$row[0].';';
                    }

                    $ret = $db->consultar("select count(*) from guias_proceso where orden='" . $num_ods . "' and especial2='" . $area . "' and id_estado='18'".$filtro);
                    while ($row = mysql_fetch_array($ret)) {
                        echo "<td valign=top class=miTable_td><div align='center'>" . $row[0] . "</div></td>";
                        $_SESSION['cadena_csv'] .=$row[0].';';
                    }
			$ret = $db->consultar("select count(*) from gama.guias_proceso gp where no_paquete in('0','5000') and orden='" . $num_ods . "' and id_estado='16' and especial2='" . $area . "'".$filtro."");
                    while ($row = mysql_fetch_array($ret)) {
                        echo "<td valign=top class=miTable_td><div align='center'>" . $row[0] . "</div></td>";
                        $_SESSION['cadena_csv'] .=$row[0].';';
                    }
                $_SESSION['cadena_csv'] = substr($_SESSION['cadena_csv'], 0, -1);
                $_SESSION['cadena_csv'] .=chr(13) . chr(10);
                }
                $db->cerrar();
            }

            if (empty($arr_ods)) {
                if (!empty($f1) and !empty($f2)) {
                    titulos();
                    $rsEst = $db->consultar("select orden from ordenes where fecha_creacion>='" . $f1 . "' and fecha_creacion<='" . $f2 . "'");
                    while ($ret = mysql_fetch_array($rsEst)) {
                        $num_ods = $ret['orden'];
                        DibujaOds($num_ods);
                    }
                }
            } else {
                $input = explode("\n", $arr_ods);
                $arr_ods = array_unique($input);
                titulos();

                for ($ytr = 0; $ytr <= count($arr_ods); $ytr++) {
                    $num_ods = trim($arr_ods[$ytr]);
                    if (!empty($num_ods)) {
                        DibujaOds($num_ods);
                    }
                }
            }

            echo "</div>";            
            echo "</table>";
            mostrar_pie();
        }
//$pla=$_SESSION['planilla'];
        echo"</td>";
        echo"</tr>";
        echo"</table>";
        echo"<table width=503 border=0 align=center cellpadding=0 cellspacing=0>";
        echo"<tbody><tr>";
        echo"<td></td>";
        echo"</tr>";
        echo"<tr>";
        echo"<td height=7 background=imagenes/lineas_pie.jpg><img src=index2_files/spacer.html width=1 height=15></td>";
        echo"</tr>";
        echo"<tr>";
        ?>
        <td>
        <center>
            <? pie_de_pagina(); ?>
        </center>
        </td>
        <?php
        echo"</tr>";
        echo"</tbody></table>";

        echo"</center>";
        echo"</body>";
        echo"</html>";
        ?>
    </CENTER>
</form>

