<?

$database = array('user'=>'akicredi_user',
			 'host'=>'localhost', 
			 'pass'=>'zlmrErKx88tJ',
			  'db'=>'akicredi_db');
  
$link = mysql_connect($database['host'], $database['user'], $database['pass']);

$c = mysql_select_db($database['db'],$link);


$sql = "select IdPrestamo,MontoTotal,SaldoPrestamo,Estado,UsuarioCreacion 	
		from `cr_prestamo`
		where 1=1
			and eliminado=0
			and idPrestamo in(1833,
1748,
1690,
1798,
1464,
1717,
1898,
1569,
1566,
1859,
1859,
1810,
991,
1692,
1649,
1668,
1920,
1566,
1947,
1516,
1925,
1634,
1976,
1525,
1470,
1837,
1476,
1859,
1896,
1856,
1873,
1351,
1815,
1885,
1536,
1766,
1510,
1651,
1673,
1336,
1849,
1624,
1966,
1793,
1506,
1915,
1974,
1707,
1738,
1856,
1919,
1834,
1932,
1945,
1913,
1858,
1971,
1971,
1650,
1889,
1568,
1944,
1763,
1750,
1619,
1489,
1704,
1764,
1141,
1932,
1932,
1880,
1661,
1759,
1759,
1775,
1698,
1880,
1808,
1655)
			
		";
echo $sql;
$result = mysql_query($sql,$link);

while($row = mysql_fetch_array($result)){
	$MontoTotal = round($row['MontoTotal'],2);
	$sqlP = "select Sum(MontoPago) as sumaPagos
			from `cr_pago`
			where IdPrestamo = '".$row['IdPrestamo']."' and (tipoPago='1' or tipoPago='3')
			
		";
	echo $sqlP;
	$result2 = mysql_query($sqlP,$link);
	echo "<table>";
	while($rowP = mysql_fetch_array($result2)){
	            echo "*-*-*-*-*-";
				$SaldoPrestamo = round(round($MontoTotal,2) - round($rowP['sumaPagos'],2),2);
			
				$sqlU = "update `cr_prestamo`
							set 
							SaldoPrestamo = '".$SaldoPrestamo."',
							estado='P'
							where IdPrestamo = '".$row['IdPrestamo']."'
							
						";
				 echo $sqlU."<br>";
				 $resultU = mysql_query($sqlU,$link);
				
				$SaldoPrestamoA = round($row['SaldoPrestamo'],2);
				$SaldoPrestamo2 = round(round($MontoTotal,2) - round($rowP['sumaPagos'],2),2);
				if( $SaldoPrestamoA != $SaldoPrestamo2){
					echo $row['IdPrestamo']."--> ".$SaldoPrestamoA."= ".($SaldoPrestamo2)." <br>";
				}
				
			
					
					if($SaldoPrestamo2 == 0){
					
						$sqlU = "update `cr_prestamo`
							set Estado = 'C',
							SaldoPrestamo = '".$SaldoPrestamo."'
							where IdPrestamo = '".$row['IdPrestamo']."'
							
						";
						// echo "ccccccc->".$sqlU."<br>";
						 $resultU = mysql_query($sqlU,$link);
					}
					
			
			
		
		echo "</table>";
	}
}


?>