<?
require("../conectar.php");
$id=$_POST['id'];
$sql="SELECT p.id_pago,p.id_concepto FROM `pagos`as p WHERE p.id_concepto=(select p.id_concepto from pagos as p where id_pago='$id')";
$num=mysql_query($sql);
$id_con=mysql_fetch_array($num);
$idconcepto=$id_con["id_concepto"];
$numer=mysql_num_rows($num);
if($numer==1)
{
$sql="UPDATE  pladepagos SET  status='Por pagar', id_pago='0' WHERE  id_pago='$id'; ";
mysql_query($sql);
}
$sql="DELETE FROM pagos WHERE pagos.id_pago='$id' LIMIT 1";
mysql_query($sql);
  $sql="SELECT subtotal as total, tipomoneda,divisa FROM conceptos WHERE id_concepto='$idconcepto' AND fecha<>'0000-00-00'";
	                     $r=mysql_query($sql);
                         $l=mysql_fetch_array($r);
	                     $total=$l["total"];
	                     $moneda=$l["tipomoneda"];
	                     $divisa=$l["divisa"];
	                     if($moneda!="USD"){
	                     $total=round($total/$divisa,2);
	                     }
	
	                    $sql="SELECT monto,tipo_moneda,divisa  FROM pagos WHERE id_concepto='$idconcepto'  AND fecha<>'0000-00-00'";
	                     $r=mysql_query($sql);
                         while($l=mysql_fetch_array($r)){
                             $totalpagado=$l["monto"];
                             $monedapago=$l["tipo_moneda"];
		                     $divisa=$l["divisa"];
	                         if($monedapago!="USD"){
	                             $totalpagado=$totalpagado/$divisa;
	                         }
		                      $totalp=$totalp+$totalpagado;
	                         }
	                         $totalp=round($totalp,2);
	                         $resto=$total-$totalp;
	                         if($resto<=0 || $resto<="0.00"){
	                             $status="PAGADO";
	                         }elseif(($resto>0 || $resto>"0.00") && $resto<$total){
	                             $status="ANTICIPO";
	                         }elseif($resto==$total){
	                             $status="LIBRE";
	                         }
	                      $sql="UPDATE conceptos SET status_renta='$status' WHERE id_concepto='$idconcepto'";
                          mysql_query($sql);


?>﻿