<?php use_helper('UrlFunctions'); ?>
<script>
findControl = function(){
  
  concatValueStr();
  
}

exportFindControl = function(action){
  strFormSerialize = returnQueryStringForm();
  executeFind(strFormSerialize, action);
}

executeFind = function(str, action){
  if(typeof(action) == 'undefined'){
	  url = "<?=buildUrl('rdiarios/grid_pre_vencidos', "block=".$block)?>";
	}else{
	  switch (action){
	    case 'excel_pre_vencidos': parent.objMainTab.reqBody('<?=buildUrl('rdiarios/excel_pre_vencidos')?>','003&'+str); break;
	    case 'pdf': parent.objMainTab.reqBody('<?=buildUrl('rdiarios/pdf_pre_vencidos')?>','003&'+str); break;	    
	    case 'pdf_supervisor': parent.objMainTab.reqBody('<?=buildUrl('rdiarios/pdf_supervisor')?>','003&'+str); break;	    
	  }
	  
	  return;
	}
  //str = str + '&block=001';
  url = "<?=buildUrl('rdiarios/grid', "block=".$block)?>";
	var myAjax  = new Ajax.Updater(
                                	"resultGrtid",
                                	url,
                                	{
                                		asynchronous:	true,
                                		method			: 'get',
                                		parameters	: str,
                                		evalScripts	: true,
                                		onLoading   : cargandoImagenSerch(),
                                		onComplete	: function(){
                                			//objMainTab.showMenu();
                                		}
                                	});
}

concatValueStr = function(){
  strFormSerialize = returnQueryStringForm();
  if(arguments[0]){
    strFormSerialize += '&'+arguments[0];
  }
  executeFind(strFormSerialize);
}

function returnQueryStringForm(){
  cadenaFormulario = $('form1').serialize();
             var posicion = new Array();
             var a_pars = new Array();
             var miArray = new Array();
             posicion=[];
             a_pars=[];
             miArray=[];
             miArray = cadenaFormulario.split('&');
             x=0;
             for (i = 0; i < miArray.length; i++){
            	   posicion[i] = miArray[i].indexOf('=');
            	   if(typeof(miArray[i-1])!='undefined'){
            				if(miArray[i].substring(posicion[i],0)== miArray[i-1].substring(posicion[i-1],0) ){
           					 porcion = miArray[i].substring((1+posicion[i]));
           					 a_pars[x-1]+='@'+porcion;
           				}else{
          					 a_pars[x]= miArray[i];
         					 x++;
          				}
          		}else{
          			 a_pars[x]= miArray[i];
          			  x++;
          		}
           }
  cadenaFormulario = a_pars.join('&');
  cadenaFormulario += '&id_form=frmFindFedu';
  return(cadenaFormulario);
}

function cargandoImagenSerch(){
	//disabledWin.setPathImage('/images/common/progress.gif');
	disabledWin.show(true,true,'resultGrtid');
}

</script>
<script>
findControl = function(){
  
  var pars = $('form1').serialize();
  
	disabledWin.setPathImage('/images/common/progress.gif');
	disabledWin.show(true,true,'resultGrtid');
  
	url = "<?=buildUrl('rdiarios/grid_pre_vencidos', "block=".$block)?>";
	var myAjax  = new Ajax.Updater(
	"resultGrtid",
	url,
	{
		asynchronous:	true,
		method			: 'get',
		parameters	: pars,
		evalScripts	: true,
		onLoading		: function(){
			//$('contentMenu').innerHTML = $('messageLoadMenu').innerHTML;
		},
		onComplete	: function(){
			//objMainTab.showMenu();
		}
	});
  
}



actualizarMesDW = function(destino,elm){
  
  $(destino).options.length = 0;
  cbosucursal = $('cboSucursal').value;
  var url = '<?=buildUrl('ajax/request')?>';
  var pars = 'type=getMesDW&nombreElm='+destino+'&value='+elm.value+'&idsucursal='+cbosucursal;
  
	var myAjax = new Ajax.Request(
                            		url, 
                            		{
                            			method: 'get', 
                            			parameters: pars, 
                            			onComplete: actualizaJSON
                            		});
    
}

</script>
<script>
actualizaJSON = function(request, json){
  var numeroElementos = json.length;
  for (var i = 0; i < numeroElementos; i++){
    if($(json[i][0])){
  	  switch ($(json[i][0]).tagName){
  	    case 'TD'      : Element.update(json[i][0], json[i][1]); break;
  	    case 'INPUT'   : $(json[i][0]).value=json[i][1]; break;
  	    case 'SELECT'  : 
  	      var objDDL = $(json[i][0]);
  	      if(typeof(cont) == 'undefined'){
  	        var cont = 0;
  	        //objDDL.options.length = 1;
  	      }
  	      var a_contentValues = json[i][1].split("_;_");
      		var value	= a_contentValues[0];
      		var text	= a_contentValues[1];
  	      var option	= new Option(text,value);
      		try{
      			objDDL.add(option,null);
      		}catch (e){
      			objDDL.add(option,-1);
      		}
  	      cont++;
  	    break;
  	  }
    }else{
      if(json[i][0].indexOf("function") > -1){
        eval("scriptEval = "+json[i][0]);
        scriptEval();
      }
    }
  }
}
</script>

<?php
$fechaDia = date("Y-m-d");
if(myUser::getUser()->getRoleId() == "sys.res"){
	$idAdministrador = myUser::getUser()->getIdColaborador();
	$o_SucursalBean = new SucursalBean();
	$o_SucursalBean->setIdAdministrador($idAdministrador);
	$o_SucursalBL = new SucursalBL();
	$a_o_sucursal = $o_SucursalBL->busquedaSucursal($o_SucursalBean);
	if(is_object($a_o_sucursal[0])){
		$idSucursal = $a_o_sucursal[0]->getIdSucursal();
		$where_s = "and idsucursal = '".$idSucursal."'";
	}
}elseif(myUser::getUser()->getRoleId() == "sys.res"){
	$where_s = "";
}else{
	$where_s = " and 1=1";
}
$con = Conexion::getConexion();
$sql = "SELECT fecha
		FROM `dw_matriz`
		WHERE 1
		
		GROUP BY SUBSTRING(fecha, 1, 7)
		order by fecha desc
		 ";
	
$result = mysql_query($sql,$con);

$num_rows = mysql_num_rows($result);
    

if($num_rows > 0){
  while ($o_valor = mysql_fetch_object($result)) {
  	$a_anos[substr($o_valor->fecha,0,4)] =  substr($o_valor->fecha,0,4);
  	
  }
}


$sql = "SELECT fecha
		FROM `dw_matriz`
		WHERE 1 and fecha like '".$a_anos[date('Y')]."%'
		GROUP BY SUBSTRING(fecha, 1, 7)
		order by fecha desc
		 ";
	
$result = mysql_query($sql,$con);

$num_rows = mysql_num_rows($result);
    

if($num_rows > 0){
  while ($o_valor = mysql_fetch_object($result)) {
  	
  	$a_mes[] =  substr($o_valor->fecha,5,2);
  }
}

$sql = "SELECT idsucursal,nombresucursal
		FROM `dw_matriz`
		WHERE 1 $where_s
		GROUP BY idsucursal
		 ";
//echo $sql;
$result = mysql_query($sql,$con);

$num_rows = mysql_num_rows($result);
    

if($num_rows > 0){
  while ($o_valor = mysql_fetch_object($result)) {
  	$a_sucursal[$o_valor->idsucursal] =  $o_valor->nombresucursal;
  }
}

?>

<img src="/images/common/spacer.gif" width="10">
<table class="cuerpo" style="background: #E3E9E2" width="99%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top">
      <form name="form1" id="form1" method="POST">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="titleBody"><li>&nbsp;&nbsp;Prestamos vencidos</li></td>
          <td align="right" valign="baseline">
            
            <img src="/images/common/buttons/btnExcel.gif" title="Reporte " onClick="exportFindControl('excel_pre_vencidos');" style="cursor:pointer"/>
            <img src="/images/common/buttons/btnPdf.gif" title="Reporte Semanal PDF" onClick="exportFindControl('pdf');" style="cursor:pointer"/>
          </td>
        </tr>
      </table>
      <table class="marco" width="100%">
        <tr>
          <td align="center" valign="top">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="100%" valign="top" >
                  <table class="SubCabecera" width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                       <td valign="top" >
                          <table class="SubCabecera"  width="40%" border="0" cellpadding="3" cellspacing="0">
                           <tr>
                           	   
	                           <td width="12%" height="26" nowrap="nowrap" class="cabecera"><label class="label" for="txtNombre">Sucursal</label></td>
	                           <td>
	                           <select name="cboSucursal" id="cboSucursal" type="text" class="combo" > 
			                       <?php foreach ($a_sucursal as $iKey => $val){ ?>
			                        <option value="<?=$iKey?>">
			                          <?=$val?>
			                        </option>
			                        <?php } ?> 
								 </select>
	                           </td>
	                             <td width="8%" height="26" nowrap="nowrap" class="cabecera"><label class="label" for="txtNombre">F.&nbsp;Inicial</label></td>
                     <td nowrap="nowrap"><input class="text" type="text" ondblclick="this.value=''" name="txtFechaInicial" id="txtFechaInicial" size="7" readonly value="<?=$fechaDia?>"/>&nbsp;<button type="button" onclick="return false" id="btnFechaInicial">...</button></td>
                      <script type="text/javascript">
                            document.getElementById("txtFechaInicial").disabled = false;
                            Calendar.setup({
                              inputField : "txtFechaInicial",
                              ifFormat : "%Y-%m-%d",
                              daFormat : "%Y-%m-%d",
                              button   : "btnFechaInicial"
                            });
                        </script>
                      <td width="8%" height="26" nowrap="nowrap" class="cabecera"><label class="label" for="txtNombre">F.&nbsp;Final</label></td>
                      
                      <td nowrap="nowrap"><input class="text" type="text" ondblclick="this.value=''" name="txtFechaFinal" id="txtFechaFinal" size="7" readonly value="<?=$fechaDia?>"/>&nbsp;<button type="button" onclick="return false" id="btnFechaFinal">...</button></td>
                      <script type="text/javascript">
                            document.getElementById("txtFechaFinal").disabled = false;
                            Calendar.setup({
                              inputField : "txtFechaFinal",
                              ifFormat : "%Y-%m-%d",
                              daFormat : "%Y-%m-%d",
                              button   : "btnFechaFinal"
                            });
                        </script>
                          </table>
                       </td>
                       
                    </tr>
                  </table>
                </td>
                </tr>   
			  <tr>
			    <td align="right" style="vertical-align:top"><input class="button" type="button" value="Buscar" onclick="findControl();"/></td>
			  </tr>
            </table>
          </td>
        </tr>
      </table>
       
      </form>
      <div id="resultGrtid">
      </div>
    </td>
  </tr>
</table>


<?php
if(isset($insert)){
  ?>
  <script>
    elementLoad.show(undefined,"Se inserto la ficha satisfactoriamente.");
    setTimeout("elementLoad.hide()",5000);
    $('IdPrestamo').value = "<?=$id?>";
    //$('cboEstadoPrestamo').value = "<?=$cboEstadoPrestamo?>";
    //$('cboMercado').value = "<?=$cboMercado?>";
    
    
    findControl();
  </script>
  <?php
}
if(isset($update)){
  ?>
  <script>
    elementLoad.show(undefined,"Se actualizo la ficha satisfactoriamente.");
    setTimeout("elementLoad.hide()",5000);
    $('IdPrestamo').value = "<?=$id?>";
   
    findControl();
  </script>
  <?php
}
if(isset($delete)){
  ?>
  <script>
    elementLoad.show(undefined,"Se elimino la ficha satisfactoriamente.");
    setTimeout("elementLoad.hide()",5000);
    $('IdPrestamo').value = "<?=$id?>";
    
    findControl();
    
  </script>
  <?php 
  
}
?>