<?php session_start();
if (!isset($_SESSION['MM_Username'])) {
      header ("Location: modulos/inicio/index.php");
	  }
?>

<html>
<head>
<title>sadce -- unidad_tributaria</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #336699;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #FFFFCF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Actualizaci&oacute;n de Unidad Tributaria</h2></td></tr></table>
<?php
  if (!login()) exit;
?>
<div style="float: right"><a href="unidad_tributaria.php?a=logout">[ Salir ]</a></div>
<br>
<?php
  $conn = connect();
  $showrecs = 10;
  $pagerange = 10;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "update":
      sql_update();
      break;
  }

  switch ($a) {
    case "edit":
      editrec($recid);
      break;
    default:
      select();
      break;
  }


  mysql_close($conn);
?>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>

<?php function select()
  {
  global $a;
  global $showrecs;
  global $page;

  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table align="center" class="tbl" border="0" cellspacing="1" cellpadding="5" width="30%">
<tr>
<td class="hr" align="center">Actualizar</td>
<td class="hr" align="center"><?php echo "Valor (Participantes Nuevos)" ?></td>
<td class="hr" align="center"><?php echo "Valor (Participantes Regulares)" ?></td>
<td class="hr" align="center"><?php echo "Valor (Aranceles)" ?></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<td class="<?php echo $style ?> a" align="center"><a href="unidad_tributaria.php?a=edit&recid=<?php echo $i ?>"><img src="imagenes/edit.gif" border="0"></a></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["valor_nuevos"]) ?> Bs.</td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["valor"]) ?> Bs.</td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["valor_arancel"]) ?> Bs.</td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php } ?>

<?php function login()
{
  global $_POST;
  global $_SESSION;

  global $_GET;
  if (isset($_GET["a"]) && ($_GET["a"] == 'logout')) $_SESSION["logged_in"] = false;
  if (!isset($_SESSION["logged_in"])) $_SESSION["logged_in"] = false;
  if (!$_SESSION["logged_in"]) {
    $login = "";
    $password = "";
    if (isset($_POST["login"])) $login = @$_POST["login"];
    if (isset($_POST["password"])) $password = @$_POST["password"];

    if (($login != "") && ($password != "")) {
      $conn = mysql_connect("localhost", "ceula_sadce_admi", "j7BnJdCGzyQBGcud");
      mysql_select_db("ceulac_sadce");
      mysql_query ("SET NAMES 'utf8'");
      $sql = "select `psw_ut` from `usuarios` where `username` = '" .$login ."'";
      $res = mysql_query($sql, $conn) or die(mysql_error());
      $row = mysql_fetch_assoc($res) or $row = array(0 => "");;
      if (isset($row)) reset($row);

      if (isset($password) && ($password == trim(current($row)))) {
        $_SESSION["logged_in"] = true;
    }
    else {
?>
<p><b><font color="-1">Usuario/Contraseña inválido</font></b></p>
<?php } } }if (isset($_SESSION["logged_in"]) && (!$_SESSION["logged_in"])) { ?>
<form action="unidad_tributaria.php?a=reset" method="post">
<table  cellspacing="1" cellpadding="7" width="30%" align="center" style="border:1px solid #ccc;">
<tr>
<td align="right">Usuario:</td>
<td align="left"><input type="text" name="login" value="<?php echo $login ?>"></td>
</tr>
<tr>
<td width="101"><div align="right">Contraseña:</div></td>

<td align="left"><input type="password" name="password" value="<?php echo $password ?>"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="action" value="Acceder"></td>
</tr>
</table>
</form>
<?php
  }
  if (!isset($_SESSION["logged_in"])) $_SESSION["logged_in"] = false;
  return $_SESSION["logged_in"];
} ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5" width="%">
<tr>
<td class="hr" ><?php echo htmlspecialchars("Valor U.T. (Participantes Nuevos)")."&nbsp;" ?></td>
<td class="dr"><input name="valor_nuevos" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["valor_nuevos"])) ?>" size="5"> 
Bs.</td>
</tr>
<tr>
<td class="hr" ><?php echo htmlspecialchars("Valor U.T. (Participantes Regulares)")."&nbsp;" ?></td>
<td class="dr"><input name="valor" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["valor"])) ?>" size="5"> 
Bs.</td>
</tr>
<tr>
<td class="hr" ><?php echo htmlspecialchars("Valor U.T. (Aranceles)")."&nbsp;" ?></td>
<td class="dr"><input name="valor_arancel" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["valor_arancel"])) ?>" size="5"> 
Bs.</td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<?php if ($page > 1) { ?>
<td><a href="unidad_tributaria.php?page=<?php echo $page - 1 ?>">&lt;&lt;&nbsp;Prev</a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="unidad_tributaria.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="unidad_tributaria.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="unidad_tributaria.php?page=<?php echo $page + 1 ?>">Next&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="unidad_tributaria.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="unidad_tributaria.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Prior Record</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="unidad_tributaria.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Next Record</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form enctype="multipart/form-data" action="unidad_tributaria.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xvalor" value="<?php echo $row["valor"] ?>">
<input type="hidden" name="xvalor_nuevos" value="<?php echo $row["valor_nuevos"] ?>">
<input type="hidden" name="xvalor_arancel" value="<?php echo $row["valor_arancel"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Actualizar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  $conn = mysql_connect("localhost", "ceula_sadce_admi", "j7BnJdCGzyQBGcud");
  mysql_select_db("ceulac_sadce");
 
  mysql_query ("SET NAMES 'utf8'");
  return $conn;
}


function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  $sql = "SELECT `valor`,`valor_nuevos`,`valor_arancel` FROM `unidad_tributaria`";
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM `unidad_tributaria`";
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_update()
{
  global $conn;
  global $_POST;
  $valor_ut=$_POST["valor"];
  $valor_ut_arancel=$_POST["valor_arancel"];
  $sql = "update `unidad_tributaria` set `valor`=" .sqlvalue(@$_POST["valor"], false) .",`valor_nuevos`=" .sqlvalue(@$_POST["valor_nuevos"], false) .",`valor_arancel`=" .sqlvalue(@$_POST["valor_arancel"], false) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
  
  
// Actualiza el costo en Bs. de los aranceles en funcion de la nueva unidad tributaria
$sqlAranceles = "SELECT * FROM aranceles";
$rstAranceles = mysql_query($sqlAranceles, $conn) or die(mysql_error());
//$row_materias = mysql_fetch_assoc($rst_materias);
$totalRowsAranceles = mysql_num_rows($rstAranceles);  

if($totalRowsAranceles >0)
{
	while($arancel = mysql_fetch_assoc($rstAranceles))
		{
         $codigo_arancel=$arancel["codigo"];
		 $ut=$arancel["costo_UT"];
		 $costo_bs=$ut*	$valor_ut_arancel;	
		 $sql = "update aranceles set `costo_Bs`='$costo_bs' where codigo='$codigo_arancel'";
  		 mysql_query($sql, $conn) or die(mysql_error());
		}
}		

// Actualiza el costo total de la Inversion del Programa en funcion de la nueva unidad tributaria
$sqlProgramas = "SELECT * FROM carreras where tipo<>'00' and tipo<>'99'";
$rstProgramas = mysql_query($sqlProgramas, $conn) or die(mysql_error());
//$row_materias = mysql_fetch_assoc($rst_materias);
$totalRowsProgramas = mysql_num_rows($rstProgramas);  

if($totalRowsProgramas >0)
{
	while($programa = mysql_fetch_assoc($rstProgramas))
		{
         $id_programa=$programa["id_carrera"];
		 $creditos=$programa["creditos_grado"];
		 $costo_ut=$programa["costo_uc"];
		 $costo_bs=$creditos*$costo_ut*$valor_ut;	
		 $sql = "update carreras set `inversion`='$costo_bs' where id_carrera='$id_programa'";
  		 mysql_query($sql, $conn) or die(mysql_error());
		}
}		

}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`valor`";
  if (@$_POST["xvalor"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xvalor"], false);
  };
  $pk .= ")";
  return $pk;
}
 ?>
