<?
require "../../conexion/conectar.php";
require("../../libs/funciones.php");

conectar();
$query="select sum(monto) as total,rfcemisor,tipodocto,serie,folio,uuid,fecha_pago,monto from pago_doc_detalle group by rfcemisor,tipodocto,serie,folio,uuid";

if($result = mysql_query($query)){
	if(mysql_num_rows($result) > 0){
		while($tmp=mysql_fetch_array($result)){
		
			$query2="select total from recibedoce where
			rfcemisor='".$tmp['rfcemisor']."' and tipodocto='".$tmp['tipodocto']."' and serie='".$tmp['serie']."'
			 and folio='".$tmp['folio']."' and uuid='".$tmp['uuid']."' limit 1";

			if($resR = mysql_query($query2)){
				if(mysql_num_rows($resR) > 0){
					$tempR=mysql_fetch_array($resR);
					if($tmp['total'] < $tempR['total']){
						$status="R";
					}else{
						$status="P";
					}
					
					$query3="update recibedoce set status_pago='$status' where
			                 rfcemisor='".$tmp['rfcemisor']."' and tipodocto='".$tmp['tipodocto']."' and serie='".$tmp['serie']."'
			                 and folio='".$tmp['folio']."' and uuid='".$tmp['uuid']."'";
					if(!mysql_query($query3)){
						echo "Error al actualizar recibedoce para folio: ".$tmp['rfcemisor']."_".$tmp['tipodocto']."_".$tmp['serie']."_".$tmp['folio']."_".$tmp['uuid']." - ".mysql_error()."<br/>";
					}
				}
			}else{
				echo mysql_error();
			}
		}
	}
}else{
	echo mysql_error();
}

?>