<?php require_once('../Connections/cn_conexion.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_habmax = "SELECT MAX(num_caja) as maxcaja FROM gastos";
$rs_habmax = mysql_query($query_rs_habmax, $cn_conexion) or die(mysql_error());
$row_rs_habmax = mysql_fetch_assoc($rs_habmax);
$totalRows_rs_habmax = mysql_num_rows($rs_habmax);

$maxcaja=$row_rs_habmax['maxcaja']+1;

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['idcliente']))) {
  $insertSQL = sprintf("INSERT INTO caja_gastos (id_cliente, num_caja, fecha_inicio, fecha_cierre) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($idcliente, "int"),
                       GetSQLValueString($maxcaja, "int"),
					   GetSQLValueString(date("Y-m-d"), "date"),
                       GetSQLValueString(date("Y-m-d"), "date"));

  mysql_select_db($database_cn_conexion, $cn_conexion);
  $Result1 = mysql_query($insertSQL, $cn_conexion) or die(mysql_error());
}

if ((isset($_GET['idcliente']))) {
  $updateSQL = sprintf("UPDATE gastos SET num_caja=%s, hab_gasto=%s WHERE id_cliente=%s AND hab_gasto=%s",
                       GetSQLValueString($maxcaja, "int"),
                       GetSQLValueString('0', "text"),
                       GetSQLValueString($idcliente, "int"),
					   GetSQLValueString('1', "int"));

  mysql_select_db($database_cn_conexion, $cn_conexion);
  $Result1 = mysql_query($updateSQL, $cn_conexion) or die(mysql_error());
}



mysql_select_db($database_cn_conexion, $cn_conexion);
$query_rs_clientesgastos = "SELECT * FROM gastos, clientes WHERE clientes.id_cliente=gastos.id_cliente AND gastos.hab_gasto='1'  GROUP BY gastos.id_cliente";
$rs_clientesgastos = mysql_query($query_rs_clientesgastos, $cn_conexion) or die(mysql_error());
$row_rs_clientesgastos = mysql_fetch_assoc($rs_clientesgastos);
$totalRows_rs_clientesgastos = mysql_num_rows($rs_clientesgastos);

//echo($maxcaja);
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="1%">&nbsp;</td>
    <td width="3%">&nbsp;</td>
    <td width="61%" height="30">Nombre del Cliente</td>
    <td width="17%">&nbsp;</td>
    <td width="12%">Fecha</td>
    <td width="6%">&nbsp;</td>
  </tr>
  <?php do { ?>
    <tr>
      <td>&nbsp;</td>
      <td><img src="../imagenes/cierre_caja.png" width="24" height="24" border="0">&nbsp;</td>
      <td height="25"><a href="#" class="enlace_normal" onClick="cerrar_caja('<?php echo $row_rs_clientesgastos['id_cliente']; ?>')"><?php echo $row_rs_clientesgastos['nom_cliente']; ?></a></td>
      <td><form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
        <input type="hidden" name="id" id="id">
        <input type="hidden" name="MM_insert" value="form1">
        <input type="hidden" name="MM_update" value="form1" />
      </form></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <?php } while ($row_rs_clientesgastos = mysql_fetch_assoc($rs_clientesgastos)); ?>
<tr>
  <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<?php
mysql_free_result($rs_clientesgastos);

mysql_free_result($rs_habmax);
?>
