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


if ($ooperacion !='OTRO(A)' && $ooperacion !='')
 {
  $sql="select id from concepto where tipo='$tipooperacion'";
  $rc=mysql_query($sql);
  $lc=mysql_fetch_object($rc);
  mysql_free_result($rc);

  $sql="select idconcepto from conceptodet where idconcepto='$lc->id' and operacion='$ooperacion'";
  $r=mysql_query($sql);
  $nr=mysql_num_rows($r);
  if ($nr==0)
   {
	$sql="insert into conceptodet(idconcepto, operacion) values ('$lc->id', '$ooperacion')";
	mysql_query($sql);
   }
  mysql_free_result($r);  
  $operacion=$ooperacion;
 }


echo <<<h
<html>

<head>
<title>CATÁLOGO DE CUENTAS</title>

<LINK href="estilo.css" rel=stylesheet>
<SCRIPT LANGUAGE="JavaScript">

function tipoopera (f) 
 {
  f.operacion.length=0;
h;

$sql="SELECT id, tipo FROM concepto";
$rc=mysql_query($sql);
while($lc=mysql_fetch_object ($rc)){
echo <<<h
  if (f.tipooperacion.value=='$lc->tipo'){
h;

	$i=0;

	$sql="SELECT idconcepto, operacion FROM conceptodet WHERE idconcepto='$lc->id' ORDER BY operacion";
	$rcd=mysql_query($sql);
	while($lcd=mysql_fetch_object ($rcd)){
echo <<<h
		f.operacion.options[$i]=new Option("$lcd->operacion","$lcd->operacion");
h;
		$i++;
	}

echo "}";
}

echo "}";


echo <<<h
function validar(f){
	var strMensaje="";
h;


echo <<<h
	
}

</script>
<script language="JavaScript" src="calen.js"></script>
</head>

<body bgcolor="#FFFFFF" text="#000000" link="#0000ff" vlink="#0000ff" alink="#ff0000"
h;

if ($act!='s')
echo <<<h
onLoad="this.document.forma.num.focus();"
h;

echo ">";




	

if ($ins=='s'){
	$sql="SELECT id FROM catalogocuentas WHERE num='$num' AND numdet='$numdet' and numdet2='$numdet2' and numdet3='$numdet3' and numdet4='$numdet4' and numdet5='$numdet5' and numdet6='$numdet6' AND operacion='$operacion'"; 
	$res=mysql_query($sql );
	$cant=mysql_num_rows($res);
	if($cant!=0){
		echo "<center>La cuenta ya existe</center>";
	}
	mysql_free_result ($res);
	$cuan=$cant;

	
	if ($cuan==0){		
		if ($tipooperacion=='CHEQUE' || $tipooperacion=='TRANSFERENCIA')
			$reporte="EGRESOS";
		else if ($tipooperacion=='DEPOSITO')
			$reporte="INGRESOS";

		$sql="INSERT INTO `catalogocuentas` ( `id` , `num` , `numdet` ,`numdet2`,`numdet3`,`numdet4`,`numdet5`,`numdet6`, `operacion` , `descripcion` , `tipo` , `grupo` , `naturaleza` , `reporte`, saldoinicial, tipogasto ) VALUES ('', '$num', '$numdet','$numdet2','$numdet3','$numdet4','$numdet5','$numdet6','$descripcion', '$descripcion', '$tipo', '$grupo', '$naturaleza', '$reporte', '$saldoinicial', '$tipogasto')"; 
		mysql_query($sql);
		$num=""; $numdet="";$numdet2="";$numdet3="";$numdet4="";$numdet5="";$numdet6="";$operacion=""; $descripcion=""; $tipo=""; $grupo=""; $naturaleza=""; $reporte=""; $saldoinicial=""; $tipogasto="";
	}
   
	if ($c==2){
		$color="#8080FF";
		$c=3;
	} 
    else if ($c==1){
		$color="#00C0C0";
		$c=2;
	}
    else if ($c==3){
		$color="#C0C000";
		$c=4;
	}
	else if ($c==4){
		$color="#0000C0";
		$c=5;
	}
	else if ($c==5){
		$color="#00C000";
		$c=1;
	}
}
else{
	$color="#306098";
	$c=1;
}


if ($mod=='s'){
	$sql="SELECT * FROM catalogocuentas WHERE id='$cual'";
	$r=mysql_query($sql);
	$lc=mysql_fetch_object ($r);
	mysql_free_result($r);
	$num=$lc->num; $numdet=$lc->numdet; $numdet2=$lc->numdet2;$numdet3=$lc->numdet3;$numdet4=$lc->numdet4;$numdet5=$lc->numdet5;$numdet6=$lc->numdet6;$operacion=$lc->operacion; $descripcion=$lc->descripcion; $tipo=$lc->tipo; $grupo=$lc->grupo; $naturaleza=$lc->naturaleza; $saldoinicial=$lc->saldoinicial; $tipogasto=$lc->tipogasto;

	
}

if ($act=='s'){
	$sql="SELECT id FROM catalogocuentas WHERE num='$num' AND numdet='$numdet' AND numdet2='$numdet2' AND numdet3='$numdet3' AND numdet4='$numdet4' AND numdet5='$numdet5' AND numdet6='$numdet6' AND operacion='$descripcion' AND id<>'$cual'"; 
	$res=mysql_query($sql );
	$cant=mysql_num_rows($res);
	if($cant!=0){
		echo "<center>La cuenta ya existe</center>";
	}
    $cuan=$cant;

	if ($cuan==0){
		if ($tipooperacion=='CHEQUE' || $tipooperacion=='TRANSFERENCIA')
			$reporte="EGRESOS";
		else if ($tipooperacion=='DEPOSITO')
			$reporte="INGRESOS";

		if ($tipooperacion!='')
			$asql=", reporte='$reporte'";

		$sql="UPDATE `catalogocuentas` SET num='$num', numdet='$numdet',numdet2='$numdet2', numdet3='$numdet3',numdet4='$numdet4',numdet5='$numdet5',numdet6='$numdet6',operacion='$descripcion', descripcion='$descripcion', tipo='$tipo', grupo='$grupo', naturaleza='$naturaleza', saldoinicial='$saldoinicial', tipogasto='$tipogasto' $asql  WHERE id='$cual'";
		mysql_query($sql);
	}
         
		
    mysql_close();

echo <<<h
  <SCRIPT language="JavaScript">
	close();
  </script>
h;
		exit();
}



if ($mod=='s')
	$agr="act=s&cual=$cual";
else
	$agr="ins=s";


echo <<<h
<form name="forma" method="post" action="catalogoCuentasCap.php?$agr&c=$c" onSubmit="return  validar(this)">

  <table width="1200" border="0" cellspacing="0" cellpadding="4" align="center" class=tform>
    
	<tr>
      <td colspan="4" height="30" align="center" bgcolor=$color>
       <b><font face="Verdana, Arial, Helvetica, sans-serif" size="3" color="#FFFFFF">.:
          REGISTRO	CATÁLOGO DE CUENTAS :.</font>
      </td>
    </tr>
h;



echo <<<h
	<tr>
      <td width="50" align="right">
        Cuenta: 
      </td>
      <td width="150" colspan="3">
        <input type="text" name="num" class="imputbox" size="5"  maxlength="6" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue=false;" value="$num"> . <input type="text" name="numdet" class="imputbox" size="5"  maxlength="6" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue=false;" value="$numdet"> . <input type="text" name="numdet2" class="imputbox" size="5"  maxlength="6" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue=false;" value="$numdet2">.<input type="text" name="numdet3" class="imputbox" size="5"  maxlength="6" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue=false;" value="$numdet3">.<input type="text" name="numdet4" class="imputbox" size="5"  maxlength="6" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue=false;" value="$numdet4">.<input type="text" name="numdet5" class="imputbox" size="5"  maxlength="6" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue=false;" value="$numdet5">.<input type="text" name="numdet6" class="imputbox" size="5"  maxlength="6" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue=false;" value="$numdet6">
	</td>


</tr>
<tr>
	<td width="50" align="right">
		Descripción: 
	</td>
	<td width="225">
		<input type="text" name="descripcion" class="imputbox" size="35" maxlength="50" onChange="javascript:this.value=this.value.toUpperCase();" value="$descripcion">
	</td>
</tr>
<tr>
	<td width="50" align="right">
		Tipo:
	</td>
	<td width="225">
		<select name="tipo" class="sel">
			<option value="$tipo">$tipo
			<option value="ENCABEZADO">ENCABEZADO
			<option value="BALANCE">BALANCE
			<option value="RESULTADO">RESULTADO
			<option value="">
		</select>
	</td>
	<td width="50" align="right">
		Grupo de Balance:
	</td>
	<td width="225">
		<select name="grupo" class="sel">
			<option value="$grupo">$grupo
			<option value="ACTIVO CIRCULANTE">ACTIVO CIRCULANTE
			<option value="ACTIVO FIJO">ACTIVO FIJO
			<option value="ACTIVO DIFERIDO">ACTIVO DIFERIDO
			<option value="PASIVO CIRCULANTE">PASIVO CIRCULANTE
			<option value="PASIVO FIJO">PASIVO FIJO
			<option value="PASIVO DIFERIDO">PASIVO DIFERIDO
			<option value="CAPITAL CONTABLE">CAPITAL CONTABLE
			<option value="">
		</select>
	</td>
</tr>
<tr>
	<td width="50" align="right">
		Naturaleza:
	</td>
	<td width="225">
		<select name="naturaleza" class="sel">
			<option value="$naturaleza">$naturaleza
			<option value="DEUDORA">DEUDORA
			<option value="ACREEDORA">ACREEDORA
			<option value="">
		</select>
	</td>
	<td width="50" align="right">
		Saldo Inicial: 
	</td>
	<td width="225">
		<input type="text" name="saldoinicial" class="imputbox" size="15" maxlength="20" onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue=false;" value="$saldoinicial">
	</td>
</tr>
<tr>
	<td width="50" align="right">
		Tipo de Gasto:
	</td>
	<td width="225">
		<select name="tipogasto" class="sel">
			<option value="$tipogasto">$tipogasto
			<option value="FIJO">FIJO
			<option value="VARIABLE">VARIABLE
			<option value="OTRO">OTRO
			<option value="CORPORATIVO">CORPORATIVO
			<option value="">
		</select>
	</td>
	<td width="50" align="right">
		
	</td>
	<td width="225">
		
	</td>
</tr>
h;

echo <<<h
<tr>
	<td colspan="4" height="40" align="center">
		<input type="submit" name="Submit" value="  Registrar  " class="botones">
	</td>
</tr>
</table>
</form>
</body>

</html>
h;

mysql_close();
?>