<?php
	session_start();
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Inicio</title>
	<link rel="stylesheet" type="text/css" href="css/bootstrap/css/bootstrap.min.css">
    <script src="css/bootstrap/js/bootstrap.bundle.min.js"></script>
    <link rel="icon" href="../images/icon.png">
    <style type="text/css">
		form{
			width: 80%;
			margin: 10% auto;
		}

		.form-floating{
			margin: 2% 0;
		}
		
		body{
		    text-align: center;
		}
	</style>
	<script type="text/javascript">
	    function seguro(){
	        if (confirm("Hacer el cierre de mes?")){
	            document.getElementById("cierre").submit();
	        }
	    }
	</script>
</head>
<body>
	<?php
		require 'nav.php';
		require 'conexion.php';
		$user = $_SESSION["user"];
		$fondos = mysqli_query($con, "SELECT * FROM fondos WHERE User='$user'");
		while ($f = mysqli_fetch_assoc($fondos)){
			$limite = $f["Limite"];
			$consec = $f["Consec"];
			$cierre = mysqli_fetch_assoc(mysqli_query($con, "SELECT Saldo, Mes, Year FROM saldos WHERE Fondo='$consec' ORDER BY Year DESC, Mes DESC"));
			$saldoIni = $cierre["Saldo"];
			if ($cierre["Mes"] == "12"){
    			$mes = "1";
    			$year = $cierre["Year"] + 1;
			}
			else{
			    $mes = $cierre["Mes"] + 1;
			    $year = $cierre["Year"];
			}
			$movs = mysqli_fetch_assoc(mysqli_query($con, "SELECT SUM(Cant) AS Suma FROM movimientos WHERE Fondo='$consec' AND MONTH(Fecha)='$mes' AND YEAR(Fecha)='$year'"))["Suma"];
			$saldo = $saldoIni + $movs;
			echo '
	<h4>', $f["Nombre"],'</h4>
	<h5>Saldo: $', $saldo,'</h5>
			';
			if ($f["Credito"]){
				echo '
	<p>Límite de crédito: $', number_format($limite, 2), '</p>
	<p>Crédito restante: $', number_format($limite - $saldo, 2), '</p>
				';
			}
		}
		$activosCierre = mysqli_fetch_assoc(mysqli_query($con, "SELECT SUM(Saldo) AS Suma FROM saldos WHERE Fondo IN (SELECT Consec FROM fondos WHERE User='$user' AND Credito='0')"));
		$pasivosCierre = mysqli_fetch_assoc(mysqli_query($con, "SELECT SUM(Saldo) AS Suma FROM saldos WHERE Fondo IN (SELECT Consec FROM fondos WHERE User='$user' AND Credito='1')"));
		$fecha = mysqli_query($con, "SELECT Mes, Year FROM saldos WHERE User='$user' ORDER BY Year DESC, Mes DESC");
		$activosSaldo = $activosCierre["Suma"];
		$pasivosSaldo = $pasivosCierre["Suma"];
		if (mysqli_num_rows($fecha) > 0){
		    $f = mysqli_fetch_assoc($fecha);
		    if ($f["Mes"] == "12"){
    	    	$mes = "1";
    	    	$year = $f["Year"] + 1;
		    }
		    else{
		        $mes = $f["Mes"] + 1;
		        $year = $f["Year"];
		    }
		}
		else{
		    if (date("m") == "1"){
    	    	$mes = "12";
    	    	$year = date("Y") - 1;
		    }
		    else{
		        $mes = date("m") - 1;
		        $year = date("Y");
		    }
		}
		$activosMovs = mysqli_fetch_assoc(mysqli_query($con, "SELECT SUM(Cant) AS Suma FROM movimientos WHERE Fondo IN (SELECT Consec FROM fondos WHERE User='$user' AND Credito='0') AND MONTH(Fecha)='$mes' AND YEAR(Fecha)='$year'"))["Suma"];
		$pasivosMovs = mysqli_fetch_assoc(mysqli_query($con, "SELECT SUM(Cant) AS Suma FROM movimientos WHERE Fondo IN (SELECT Consec FROM fondos WHERE User='$user' AND Credito='1') AND MONTH(Fecha)='$mes' AND YEAR(Fecha)='$year'"))["Suma"];
		switch ($mes){
		    case "1":
		        $mesNom = "Enero";
		    break;
		    case "2":
		        $mesNom = "Febrero";
		    break;
		    case "3":
		        $mesNom = "Marzo";
		    break;
		    case "4":
		        $mesNom = "Abril";
		    break;
		    case "5":
		        $mesNom = "Mayo";
		    break;
		    case "6":
		        $mesNom = "Junio";
		    break;
		    case "7":
		        $mesNom = "Julio";
		    break;
		    case "8":
		        $mesNom = "Agosto";
		    break;
		    case "9":
		        $mesNom = "Septiembre";
		    break;
		    case "10":
		        $mesNom = "Octubre";
		    break;
		    case "11":
		        $mesNom = "Noviembre";
		    break;
		    case "12":
		        $mesNom = "Diciembre";
		    break;
		}
		echo '
	<form action="cierre.php" method="POST" id="cierre">
	    <input type="hidden" name="mes" value="', $mes,'">
	    <input type="hidden" name="year" value="', $year,'">
	    <input type="hidden" name="user" value="', $user,'">
	</form>
	<h3>Total activos: $', number_format($pasivosSaldo + $pasivosMovs, 2),'</h3>
	<h3>Total pasivos: $', number_format($pasivosSaldo + $pasivosMovs, 2),'</h3>
	<h3>Total: $', number_format($pasivosSaldo + $pasivosMovs - $pasivosSaldo - $pasivosMovs, 2),'</h3>
	<br>
	<h6>Cierre de ', $mesNom,'</h6>
	<button class="btn btn-primary" onclick="seguro()">Hacer cierre</button>
		';
	?>
</body>
</html>

