<?php
require_once("../php/config.php");
$id = $_GET["id"];
$sqlcuota = mysql_query("select * from cuotas where id='$id'") or die(mysql_error());
$numero = @mysql_result($sqlcuota,0,"numero");
$id_prestamo = @mysql_result($sqlcuota,0,"id_prestamo");
$cliente = @mysql_result($sqlcuota,0,"cliente");



$capital = $_GET["capital"];
$interes = $_GET["interes"];
$mora = $_GET["mora"];
$total = $_GET["total"];

///$total = $_GET["total"];
$usuario = $_COOKIE["usuario"];
if($id==""){
    echo "No ha seleccionado un registro";
    sleep(2);
   
       echo "<script>
	            var param;
	            param ='id_prestamo=$id_prestamo';
				param +='&id=$id';
	            window.location.href='get_cuota.php?'+param;
	        </script>";
}



$x = mysql_query("insert into cobros set capital='$capital',
                                         interes='$interes',
                                         total='$total',
                                         mora='$mora',
                                         cliente='$cliente',
                                         id_prestamo='$id_prestamo',
                                         usuario='$usuario',
                                         fecha=CURDATE(),
                                         hora=NOW(),
                                         id_cuota='$id'") or die(mysql_error());
  


$h = mysql_query("update cuotas  set 
				                 balance_capital=balance_capital - '$capital',
				                 balance_interes=balance_interes - '$interes',
				                 balance=balance - '$total' where id<='$id' and id_prestamo='$id_prestamo' and balance>0") or die(mysql_error());
$h = mysql_query("update prestamos  set 
				                 balance=balance - '$total' where id_prestamo='$id_prestamo'") or die(mysql_error());
mysql_query("update prestamos  set 
				                 balance=0  where balance<1") or die(mysql_error());
	
	
echo "Pago grabado satisfactoriamente...";
  sleep(2);
     echo "<script>
	            var param;
	            param ='id_prestamo=$id_prestamo';
				param +='&total=$total';
				param +='&capital=$capital';
				param +='&interes=$interes';
				param +='&mora=$mora';
	            window.location.href='ticket_pago_prestamo.php?'+param;
	        </script>";
?>