<?php
	require 'conexion.php';
	$mes = $_POST["mes"];
	$year = $_POST["year"];
	$user = $_POST["user"];
	$fondos = mysqli_query($con, "SELECT * FROM fondos WHERE User='$user'");
	while ($f = mysqli_fetch_assoc($fondos)){
		$consec = $f["Consec"];
		$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"];
		$chec = mysqli_query($con, "SELECT Saldo FROM saldos WHERE Fondo='$consec' ORDER BY Year DESC, Mes DESC");
		if (mysqli_num_rows($chec) > 0){
			$saldoIni = mysqli_fetch_assoc($chec)["Saldo"];
		}
		else{
			$saldoIni = 0;
		}
		$saldo = $saldoIni + $movs;
		mysqli_query($con, "INSERT INTO saldos (Saldo, Fondo, Mes, Year, User) VALUES ('$saldo', '$consec', '$mes', '$year', '$user')");
	}
	echo '
		<meta http-equiv="refresh" content="0; url=inicio.php">
	';
?>

