<?php require_once('../Connections/cn_conexion.php'); ?>
<?php include_once('../includes/funciones.php');?>
<?php
$idcliente=$_GET['idcliente'];
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


mysql_select_db($database_cn_conexion, $cn_conexion);
$query_rs_detcaja = "SELECT * FROM gastos, clientes, cuentas, distrito WHERE gastos.id_cliente='".$idcliente."' AND clientes.id_cliente=gastos.id_cliente AND gastos.id_cuenta =cuentas.id_cuenta AND distrito.id=clientes.dis_cliente  AND  gastos.hab_gasto='1' ORDER BY gastos.fecha  ASC , cuentas.nom_cuenta ASC";
$rs_detcaja = mysql_query($query_rs_detcaja, $cn_conexion) or die(mysql_error());
$row_rs_detcaja = mysql_fetch_assoc($rs_detcaja);
$totalRows_rs_detcaja = mysql_num_rows($rs_detcaja);

//echo($query_rs_detcaja);
?>
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="1%">&nbsp;</td>
      <td width="12%">Nombre</td>
      <td width="71%"><?php echo $row_rs_detcaja['nom_cliente']; ?></td>
      <td width="12%">&nbsp;</td>
      <td width="4%">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>Dirección</td>
      <td><?php echo $row_rs_detcaja['dir_cliente']; ?></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>Distrito</td>
      <td><?php echo $row_rs_detcaja['distrito']; ?></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>RUC</td>
      <td><?php echo $row_rs_detcaja['ruc_cliente']; ?></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="12%" height="20" bgcolor="#000066"><p class="texto_normal">Fecha</p></td>
          <td width="63%" bgcolor="#000066"><p class="texto_normal">Cuenta</p></td>
          <td width="10%" align="center" bgcolor="#000066"><p class="texto_normal">Egreso S/</p></td>
          <td width="15%" align="center" bgcolor="#000066"><p class="texto_normal">Ingreso S/</p></td>
        </tr>
      </table></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="3">
      <div id="show_caja">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <?php
        $suma_gastos=0;
		$suma_egresos=0;
		$suma_ingresos=0;
		$i=1;
		?>
        <?php do { ?>
        <?php
        $suma_gastos+=$row_rs_detcaja['monto_real'];
		$res=$i%2;
		if($res==0){
			$color="#CDEBFC";
			}else{
				$color="#ffffff";
				}
		$monto=$row_rs_detcaja['monto_real'];
		$montoabs=$row_rs_detcaja['monto'];		
		$moneda=$row_rs_detcaja['moneda'];
		$tipocambio=$row_rs_detcaja['tipo_cambio'];
		$tipocuenta=$row_rs_detcaja['tipo_cuenta'];
		?>
          <tr>
            <td width="12%" style="background-color:<?php echo($color);?>"><?php echo fecha_peru($row_rs_detcaja['fecha']); ?></td>
            <td colspan="2" style="background-color:<?php echo($color);?>"><?php echo $row_rs_detcaja['nom_cuenta']; ?></td>
            <td width="10%" align="right" style="background-color:<?php echo($color);?>">
			<?php 
				if($tipocuenta=='-1'){
					echo number_format(decimals($montoabs),2);	
					$suma_egresos=$suma_egresos+$montoabs;	
					}else{
						echo(" ");
						}
			 ?>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td width="15%" align="right" style="background-color:<?php echo($color);?>"><?php 
				if($tipocuenta=='1'){
					echo number_format(decimals($montoabs),2);		
					$suma_ingresos=$suma_ingresos+$montoabs;
					}else{
						echo(" ");
						}			
			 ?>
            &nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
          <?php $i++?>
          <?php } while ($row_rs_detcaja = mysql_fetch_assoc($rs_detcaja)); ?>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td align="right">Total:&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td align="right"><?php echo(number_format(decimals($suma_egresos),2));?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td align="right"><?php echo(number_format(decimals($suma_ingresos),2));?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td width="23%">&nbsp;</td>
            <td width="40%" align="right">&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td align="right">&nbsp;</td>
            <td align="right"><?php echo(number_format(decimals(abs($suma_ingresos-$suma_egresos)),2));?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
      </table>
      </div>
      </td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td align="right" valign="middle"><form id="form1" name="form1" method="post" action="reporte_detallecajaexcel.php">
        <input name="consulta" type="hidden" id="consulta" value="<?php echo($query_rs_detcaja);?>" />
        <input type="submit" name="button3" id="button3" value="Exportar Excel" />
        <input type="button" name="button5" id="button5" value="Cerrar" onclick="cerrarcaja_detalle()"/>
        &nbsp;&nbsp;&nbsp;
      </form></td>
      <td valign="middle"><form action="../reporte_pdf/carta_pendientepago.php" method="post" name="form2" target="_new" id="form2">
        <input name="consulta_carta" type="hidden" id="consulta_carta" value="<?php echo($query_rs_detcaja);?>" />
        <input name="sumagastos" type="hidden" id="sumagastos" value="<?php echo($suma_gastos);?>" />
      </form></td>
      <td>&nbsp;</td>
    </tr>
  </table>

<?php
mysql_free_result($rs_detcaja);
?>
