<?php
session_start();
require("../conectar.php");
require("libFormato.php");

$hoy=$_GET["hoy"];

$consulta = "";
$condbusqueda="";
$concepto=$_POST["concepto"];
$recibio=$_POST["recibio"];
$cantidad=$_POST["cantidad"];
$Nombre=$_POST["Nombre"];
$APaterno=$_POST["APaterno"];
$AMaterno=$_POST["AMaterno"];
$where 	= " ";
$cont 	= -1;

	$recibeCampos = array($concepto,$recibio,$cantidad,$Nombre,$APaterno,$AMaterno);
	$nombreswhere = array('p.concepto','concat(e.nombre,e.a_pat,e.a_mat)','p.monto','c.nombre','c.a_pat','c.a_mat');
    $like = " LIKE '%";
    $likef= "%'";
	for($i=0; $i<=6; $i++)
	{
		if ($recibeCampos[$i] == "")
		{
				//No concatena campo	
		}else{	//Si concatena campo
				$where  .= $nombreswhere[$i].$like.$recibeCampos[$i].$likef." and ";
			}
	}

	$maxwhere = strlen($where);
	if($maxwhere>0){
		$where = substr($where, 0, $maxwhere-5);	
	}		
	

	mysql_query("SET NAMES 'utf8'");
	$sql=mysql_query("SELECT d.id_departamento as depa,cm.id_complejo,p.id_renta,p.comprobante,p.usu_val,(SELECT CONCAT(nombre,' ',a_pat) FROM empleados WHERE id_empleado=p.usu_val) as usuval,p.id_pago,p.fecha as fecha,p.concepto as concepto,p.formapago as formapago, p.monto as cantidad, p.tipo_moneda as moneda, p.divisa as divisa, p.f_entrada as entrada, p.f_salida as salida,p.valadmon, cm.nombre as propiedad, d.clave as clave, e.nombre as nombree, e.a_pat as apate,p.id_concepto,p.valarq,concat(SUBSTRING(e.nombre, 1, 1),SUBSTRING(e.a_pat, 1, 1),SUBSTRING(e.a_mat, 1, 1)) as ejecutivo,CONCAT(c.a_pat,' ',c.a_mat,' ',c.nombre) as cliente,r.id_cliente 
		FROM pagos as p  
			INNER JOIN complejo as cm ON p.propiedad=cm.id_complejo 
			INNER JOIN  departamentos as d ON p.clave=d.id_departamento 
			INNER JOIN empleados as e ON p.id_emp=e.id_empleado 
			INNER JOIN renta r ON p.id_renta=r.id_renta 
			INNER JOIN clientes c ON c.id_cliente = r.id_cliente
		WHERE $where and valadmon = 's' order by p.fecha desc");
	$cuantos = mysql_num_rows($sql); 
	if($cuantos>0){	?>
        <h4>Resultado</h4>
<div class="row">
	 <div class="large-12 columns">
		<table class="datos" cellspacing="0" width="100%" id="listamsg"> 
		  <thead>
			<tr class="trb">
				     <th align="center"><b>Fecha</b></th>
					 <th align="center"><b>Cliente</b></th>
					 <th align="center"><b>Concepto</b></th>
					 <th align="center"><b>Forma de pago</b></th>
					 <th align="center"><b>Cantidad $</b></th>
					 <th align="center"><b>Moneda</b></th>
					 <th align="center"><b>TC $</b></th>
					 <th align="center"><b>Propiedad</b></th>
					 <th align="center"><b>Periodo</b></th>
					 <th align="center"><b>Recibio</b></th>
					 <th align="center"><b>Comp.</b></th>
					 <th align="center"><b>Modificar comp.</b></th>
					 <th align="center"><b>Com</b></th>
                     <th align="center"><b>Eliminar pago</b></th>
					 <th align="center"><b>Valido</b></th>
					 <th align="center"><b>Val admon</b></th>
					 <th align="center"><b>Pago modificado</b></th>
					 <th align="center"><b>Actualizar</b></th>
           	</tr>
          </thead>
          <tbody>		  
			<?php 
			    $k=1;
				while($ls=mysql_fetch_array($sql)){
					     if($ls['valadmon']=='s'){$readonly="readonly";$ch="checked";$img="<img src='images/emok.png' width='15' height='15' border='0'>";}else{$readonly="";$ch="";$img="";}
						
						 $valarq=$ls['valarq'];
						 $select="SELECT nombre, a_pat FROM empleados WHERE id_empleado='$ls[usu_val]'";
						 $q=mysql_query($select);
						 $e=mysql_fetch_array($q);
						 $admonval=$ls['usuval'];
						 $p++;
					    ?>
						 <tr id="a<?echo$p?>" class="trb">
						     
						     <td><?echo $ls['fecha']?></td>
							 <td>
 								<a href="cedulas.php?idcliente=<?echo$ls['id_cliente'];?>&idrenta=<?echo$ls['id_renta'];?>&idconcepto=<?php echo $ls['id_concepto'];?>" target="_blank"><label style="font-size:8px;"><?echo html_entity_decode($ls['cliente']);?></label></a>
							 </td>
						     <td>
 								<select name="concepto<?echo $k?>" id="concepto<?echo $k?>" class="selec">
							         <option value="<?php echo $ls['concepto'];?>" selected="selected"><?php echo $ls['concepto'];?></option>
								     <option value="RENTA">RENTA NUEVA</option> 
								     <option value="DEPOSITO">GARANTIA</option>
								     <option value="IMPUESTOS">IMPUESTOS</option>
								     <option value="SEGUIMIENTO">RENTA SEGUIMIENTO</option>
									 <option value="EXTRAS">EXTRAS</option>
						        </select>					   
						     </td>
						     <td>
						     	<select name="formaPago<?echo $k?>" id="formaPago<?echo $k?>" class="selec" onchange="javascript:ver(this.value,<?echo $k?>);" style="width:50px;">
								     <option value="<?php echo $ls['formapago']?>"><?php echo $ls['formapago']?></option>
								     <option value="EP">EFECTIVO</option>
			                         <option value="ED">EFECTIVO DOLARES</option>
								     <!--option value="TC ECO">TARJETA DE CREDITO ECONOSUITES</option-->
			                         <option value="TC HABI">TARJETA DE CREDITO HABITACIONES</option>
									 <!--option value="TD">TARJETA DE DEBITO ECONOSUITES</option-->
			                         <option value="TD HABI">TARJETA DE DEBITO HABITACIONES</option-->
			                         <!--option value="TS">TRANSFERENCIA SUIZA</option-->
								     <!--option value="TM">TRANSFERENCIA MERRYLINCH</option-->
								     <!--option value="TIBJA">TRANSFERENCIA INTERBANCARIA JOSE ALBERTO</option-->
								     <!--option value="TIBECO">TRANSFERENCIA INTERBANCARIA ECONOSUITES</option-->
			                         <option value="TIBHABI">TRANSFERENCIA INTERBANCARIA HABITACIONES</option>
								     <!--option value="DJA">DEPOSITO CUENTA JOSE ALBERTO</option-->
								     <!--option value="DECO">DEPOSITO ECONOSUITES</option-->
								     <option value="PP">PAYPAL</option>
								     <option value="EXTRANET">EXTRANET</option>
								     <option value="VA">VOUCHER ABIERTO</option>
								     <option value="CORTESIA">CORTESIA</option>
									 <option value="AIRBNB TRANSFERENCIA">AIRBNB TRANSFERENCIA</option>
									 <option value="INTERMEDIARIO DE PAGOS (OXXO,SEVEN)">INTERMEDIARIO DE PAGOS (OXXO,SEVEN)</option>
									 <option value="NO SHOW">NO SHOW</option>
                 	     		 </select> 					     	
						     </td>
						     <td align="right">
							 	<input type="text" name="cantidadmod<?php echo$k;?>" id="cantidadmod<?php echo$k;?>" value="<?php echo $ls['cantidad'];?>" class="inpd"><div id="msgcambiacantidad<?php echo$k;?>"></div>
							 </td>
							 <!--font style="float:left">$</font> <?echo Moneda2($ls['cantidad']);?></td-->
						     <td>
						     	<input type="text" name="moneda<?php echo $k;?>" id="moneda<?php echo $k;?>" value="<?php echo $ls['moneda'];?>" class="inpd">					     
						     </td>
						     <td align="right"><?echo $ls['divisa']?></td>
						     <td>
						     	<select name="propiedad<?php echo $k;?>" id="propiedad<?php echo $k;?>" class="selec" onchange="depas(this.id,<?php echo $k?>);">
			                         <option value="<?php echo  $ls['id_complejo'];?>"><?php echo  $ls['propiedad'];?></option>
						             <?php
						                $sqld=mysql_query("select id_complejo,nombre from complejo",$db_conexion);
						                while($prop=mysql_fetch_array($sqld))
						                     {
						                      echo "<option value='$prop[id_complejo]' style='font-size:10.5px;'>$prop[nombre]</option>";
						                     }
						             ?>
						        </select>
						        <select name="departamento<?php echo $k;?>" id="departamento<?php echo $k;?>" class="selec">
		                           <option value="<?php echo $ls['depa'];?>"><?php echo $ls['clave'];?></option>
					            </select>     	
						     </td>
						     <td>
						     	<input type="text" id="fentrada<?php echo $k; ?>" name="fentrada<?php echo $k; ?>" size="18" value="<?php echo $ls['entrada'];?>" class="inpd" readOnly>
						     	/<input type="text" id="fsalida<?php echo $k; ?>" name="fsalida<?php echo $k; ?>" size="18" value="<?php echo $ls['salida'];?>" class="inpd" readOnly>
						    	<script src="js/moment.js"></script>
								<script src="js/pikaday.js"></script>
								<script>
								var picker = new Pikaday(
								{
									field: document.getElementById('fentrada<?php echo $k; ?>'),
									format : "YYYY-MM-DD",
									firstDay: 1,
									minDate: new Date(1930, 0, 1),
									maxDate: new Date(<?php echo date('Y')+5;?>, 12, 31),
									yearRange: [1930,<?php echo date('Y')+5;?>]
								});
								</script>
								<script>
								var picker = new Pikaday(
								{
									field: document.getElementById('fsalida<?php echo $k; ?>'),
									format : "YYYY-MM-DD",
									firstDay: 1,
									minDate: new Date(1930, 0, 1),
									maxDate: new Date(<?php echo date('Y')+5;?>, 12, 31),
									yearRange: [1930,<?php echo date('Y')+5;?>]
								});
								</script>

						     <?php //echo $ls['entrada']."/".$ls['salida']?></td>

						     <td title="<?echo htmlentities($ls['nombree']." ".$ls['apate']);?>"><?echo htmlentities($ls['ejecutivo']);?></td>
						 
						     <td><?if($ls["comprobante"]==""){echo "";}else{?><a href="<?echo $ls["comprobante"];?>" target="_blank"><img src="images/zoom.png" width="18" height="18" border='0' style="margin-left:-5px;" alt="Ver comprobante"></a><?}?></td>
						     <td><img onclick="ventana(<?echo $ls["id_pago"];?>)" src="images/zoom.png" width="18" height="18" border='0' style="margin-left:-5px;" alt="Ver comprobante"></td>
 <td><img onclick="ventanas(<?echo $ls["id_pago"];?>)" src="images/zoom.png" width="18" height="18" border='0' style="margin-left:-5px;" alt="Ver comprobante"></td>
						      <td><?if($valarq=='s'){}else{?><img onclick="borrar('<?echo $ls["id_pago"];?>','<?echo"a$p"?>' )" src="images/borrar.png" width="18" height="18" border='0' style="margin-left:-5px;" alt="Ver comprobante">&nbsp;&nbsp;&nbsp;
							  <?}?>
							  <!--<a href="#" onclick="borrar2('<?echo $ls["id_pago"];?>','<?echo"a$p"?>' )"></a>-->
							  </td>
	
						     	 <td><?echo $admonval?></td>
						     <td><input type="checkbox" id="val<?echo$k;?>" name="val<?echo$k;?>" <?echo$ch;?> value="s" onclick="javascript:validapago('<?echo$ls['id_pago']?>','<?echo$k?>','RENTA','<?echo$ls['id_renta']?>','<?echo$ls['id_cliente'];?>','<?echo$ls['id_concepto'];?>');"><div id="validado<?echo$k;?>" style="position:absolute;width:15px;height:15px;margin-top:-17px;margin-left:17px"><?echo$img;?></div></td>
							 <td>
							   <?php 
							      $sqlpagmod = "SELECT COUNT(idpago) as pagosmod FROM bitacorapagos WHERE idpago = ".$ls['id_pago'];
								  $ejecutapagmod = mysql_query($sqlpagmod);
								  $regpagmod = mysql_fetch_array($ejecutapagmod);
								  if($regpagmod['pagosmod'] > 0){ ?>
									  <a href="bitacorapagos.php?idp=<?php echo $ls['id_pago'];?>" target="_blank" onClick="window.open(this.href, this.target, 'width=500,height=600'); return false;">Ver</a>
								  <?php } ?>
							 </td>
							 <td><a href="javascript:cambiacantidad('<?php echo$ls['id_pago']?>','<?php echo$k;?>');">Actualizar</a></td>
						 </tr>
						<?
						$k++;
					}
					?>
			</tbody>	 
		</table>   
	</div>
</div>
		<?php }else{ echo "<p class='parrafo'><font color='red'>No se encontraron registros</font></p>";}  ?>	
		 <script>
             $(".loading").fadeOut('fast');
         </script>	
		 <script type="text/javascript">
		    // For build to fit into DataTables site builder...
			$('#listamsg')
			.removeClass( 'display' )
			.addClass('tdisplay');
		</script>