<?php
	session_start();

	include("auth.inc.php");

	if (isset($_SESSION["pendiente"]) && ($_SESSION["pendiente"] == "1")) {
		$_SESSION["opcion"] = "3";
		$_SESSION["pendiente"] = "1";
		header("Location: inicio.php");
	} else {
		include "conn.inc.php";

		$anyo = date(Y);
		$mes = date(m);
		$dia = date(d);

		$fecha_alta = $anyo."-".$mes."-".$dia;

		$q_alta = "INSERT INTO matriculas (usuario_id, liquidacion_id, pendiente, fecha, responsable) VALUES ('".$_SESSION["alumno"]."', '".$_SESSION["liquid_id"]."', '1', '".$fecha_alta."', '".$_SESSION["nif"]."')";
		$r_alta = mysql_query($q_alta, $conn);

		$q_matricula = "SELECT * FROM matriculas WHERE usuario_id='".$_SESSION["alumno"]."' AND (fecha = '".date(Y."-".n."-".j)."')";
		$rs_matricula = mysql_query($q_matricula,$conn);
		$res_matricula = mysql_fetch_array($rs_matricula,MYSQL_ASSOC);

		if ($_SESSION["matricula"]) {
			foreach($_SESSION["matricula"] as $clave => $valor){
				$q_detalle = "INSERT INTO matriculas_detalles (matricula_id ,asignatura_id) VALUES ('".$res_matricula["id"]."', '".$valor["id"]."')";
				$r_detalle = mysql_query($q_detalle, $conn);
			}
			/*
			$q_det = "SELECT * FROM matriculas_detalles WHERE matricula_id='".$res_matricula["id"]."'";
			$rs_det = mysql_query($q_det,$conn);
			$res_det = mysql_fetch_array($rs_det,MYSQL_ASSOC);

			if (!$res_det) {
				$q_delete = "DELETE FROM matriculas WHERE id='".$_POST["id_matricula"]."'";
				$r_delete = mysql_query($q_delete, $conn);
			}
			*/

			$_SESSION["matricula_id"] = $res_matricula["id"];
		}

		$_SESSION["opcion"] = "3";
		$_SESSION["accion"] = "check_matricula";
		$_SESSION["pendiente"] = "3";
		$_SESSION["imprimir"] = "1";

		header("Location: inicio.php");
	}
?>