<?php
if(isset($_POST["bexp"])) {		
	$varFIni = ($_POST["eini"]=="") ? "NULL" : "'".FechaMySql($_POST["eini"])."'";
	$varFFin = ($_POST["efin"]=="") ? "NULL" : "'".FechaMySql($_POST["efin"])."'";		
	$sql = "call spQuery_Pagos_CxC('$_POST[ccod]', $varFIni, $varFFin)";
	$_SESSION["expor_sql_1"] =  $sql;
	$_SESSION["expor_sql_2"] = NULL;
	$varTitulo = (isset($_POST["titu"]))?$_POST["titu"]:"Reporte";
	$dir = "export.php?page=$varTitulo";
	header("location: $dir");
	}
	
require_once('clases/xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->setFlag('debug',true); 
$xajax->configure('javascript URI', 'clases/xajax/');

require "clases/KoolAjax/koolajax.php";
$koolajax->scriptFolder = "clases/KoolAjax";
	
function seleccionaCliente($ide) {
	if($ide<>"") {
		$var = VarServer("MYSQLI");
		$obj = new clsConexion($var[0],$var[1],$var[2],$var[3],$var[4]);
		$obj->AbrirConexion();
		$sql = "call spQuery_Lista_Cliente('$ide','','');";
		$rs = $obj->Query($sql);
		$obj->CerrarConexion();
		$Fila = $obj->FetchArray($rs);
		if($obj->NumFilas($rs)>0) $retorna = $Fila[1];
		else $retorna = "NO EXISTE";
		}
	return $retorna;
	}	
	
$xajax->processRequest();
$xajax->printJavascript("clases/xajax");

$koolajax->enableFunction("seleccionaCliente");	
echo $koolajax->Render();
		
function MyTable($obj, $Titulo, $Opcion, $Valor) {
$Link = $_SERVER['PHP_SELF'];
$MyTabla = "_sv_venta";
$myID = "id_venta";
?>
<script language="javascript">
function cargarCliente(ccod, cnom) {
	var Ruta = 'lista_Cliente.php?ccod='+ccod+'&cnom='+cnom;
	testwindow= window.open (Ruta, "mywindow", "location=1,status=1,scrollbars=1,width=600,height=550");
	}
		
function select_Cliente(ccod, cnom) {
			
	var ccod1 = document.getElementById(ccod);
	var cnom1 = document.getElementById(cnom);
	
	if(ccod1.value=="") {
		ccod1.value = "";
		cnom1.value = "";
		}
	else {			
		var dato = koolajax.callback(seleccionaCliente(ccod1.value));
		if(dato=="NO EXISTE") {
//			alert("CLIENTE NO EXISTE");
			ccod1.value = "";
			cnom1.value = dato;
			ccod1.focus();
			}
		else {
			cnom1.value = dato;
			}
		}
	}
</script>
<?php
if(!isset($Opcion)) {
	$varFINI = (isset($_POST["eini"]))?$_POST["eini"]:NULL;
	$varFFIN = (isset($_POST["efin"]))?$_POST["efin"]:NULL;
	$varCCOD = (isset($_POST["ccod"]))?$_POST["ccod"]:NULL;
	$varCNOM = (isset($_POST["cnom"]))?$_POST["cnom"]:NULL;
	$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"420", "clase"=>"Formtitulo", "titulo"=>$Titulo." - BUSQUEDA",
					"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST"),						
					"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$Titulo),
								   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$Opcion)
								  ),
					"conte"=>array(array("titulo"=>"Cliente :", "clase1"=>"FormCampo1", "clase2"=>"interior",
										 "campo"=>array("tipo"=>"varios",
														"contenido"=>array(array("tipo"=>"text", "id"=>"ccod", "name"=>"ccod", "valor"=>$_POST[ccod], "ancho"=>"50", 
																				 "alinear"=>"center", "blur"=>"select_Cliente('ccod', 'cnom')", 
																				 "dclick"=>"cargarCliente('ccod', 'cnom')"),
																		   array("tipo"=>"text", "id"=>"cnom", "name"=>"cnom", "valor"=>$_POST[cnom], "ancho"=>"200")
																		   )
													 )
										 ),							  
								   array("titulo"=>"Fecha Incial :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo"=>array("tipo"=>"fecha", "id"=>"eini", "name"=>"eini", "alinear"=>"center", "valor"=>$_POST[eini]) ),
								   array("titulo"=>"Fecha Final :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo"=>array("tipo"=>"fecha", "id"=>"efin", "name"=>"efin", "alinear"=>"center", "valor"=>$_POST[efin]) )									   
								   ),
					"boton"=>array(array("tipo"=>"submit", "id"=>"bcon", "name"=>"bcon", "ancho"=>"80", "alto"=>"22", "texto"=>"Consultar"),			  
								   array("tipo"=>"submit", "id"=>"bexp", "name"=>"bexp", "ancho"=>"80", "alto"=>"22", "texto"=>"Exportar")
								   )
					);
	echo $obj->setTablaForm($varTabla);
	if(isset($_POST["bcon"])) {
		$varFIni = ($_POST["eini"]=="") ? "NULL" : "'".FechaMySql($_POST["eini"])."'";
		$varFFin = ($_POST["efin"]=="") ? "NULL" : "'".FechaMySql($_POST["efin"])."'";		
		$sql = "call spQuery_Pagos_CxC('$_POST[ccod]', $varFIni, $varFFin)";
		
		$rs_cab = array(array("Tamano"=>10, "Titulo"=>"CPAG", "Alinear"=>"center"),
						array("Tamano"=>10, "Titulo"=>"CCLI", "Alinear"=>"center"),
						array("Tamano"=>250,"Titulo"=>"CLIENTE"),
						array("Tamano"=>110,"Titulo"=>"DOC.VENTA"),
						array("Tamano"=>10, "Titulo"=>"CTIP", "Alinear"=>"center"),
						array("Tamano"=>150,"Titulo"=>"TIPO PAGO"),
						array("Tamano"=>80, "Titulo"=>"NRO. PAGO"),
						array("Tamano"=>80, "Titulo"=>"FECHA", "Alinear"=>"center"),
						array("Tamano"=>80, "Titulo"=>"MONTO", "Alinear"=>"right")
					   );
		$Boton = array(0=>array(0 => array("Link"=>$Link, "Boton"=>"b_table_delete.png", "Permite"=>$_SESSION["_FDEL"], 
												"Mensaje"=>"Haga Click para <b>ELIMINAR</b> el Pago",
												"Click"=>"return confirm('¿ESTA SEGURO DE ELIMINAR ESTE PAGO?')"),
									1 => array("Campo"=>"opt","Valor"=>"ANUL", "Tipo"=>"IN"),
									2 => array("Campo"=>"valor","Valor" => 0)
									),
						   1=>array(0 => array("Link"=>$Link, "Boton"=>"b_table_edit.png",
												"Mensaje"=>"Haga Click para <b>MODIFICAR</b> el Pago"),
									1 => array("Campo"=>"opt","Valor"=>"EDIT", "Tipo"=>"IN"),
									2 => array("Campo"=>"valor","Valor" => 0)
									)							
						);
		echo $obj->SetGrid("98%",$rs_cab,$sql,NULL,2,NULL,NULL, NULL, "IZQ", $Boton);	
		}
	}
elseif($Opcion=="ANUL") {
		$Usr = (isset($_SESSION["_CUSE"]))  ? $_SESSION["_CUSE"] : "1";
		$sql = "call spMante_Pago_CxC_Anular($Valor, $Usr)";
		$obj->AbrirConexion();
		$obj->Query($sql);
		$obj->CerrarConexion();	
		echo "<script language='Javascript'>";
		echo "window.location='$Link'";
		echo "</script>\n";
		}
elseif($Opcion=="EDIT") {
	$sql = "select P.id_pago_cxc as CPAG, 
			P.id_cliente as CCLI, C.nombre as CLIENTE, 
			P.id_tipo_pago as CTIP, TP.nombre as TIPO, P.nro_doc_pago as NPAGO, 
			date_format(P.fecha,'%d/%m/%Y') as FECHA, P.MONTO 
			from _sv_pago_cxc P 
			inner join _sv_tipo_pago TP ON P.id_tipo_pago = TP.id_tipo_pago 
			inner join _sv_cliente C ON P.id_cliente = C.id_cliente 
			where P._estado = 'A'
			and P.id_pago_cxc = $Valor;";
	$varTipo = (isset($_POST["tipo"]))?$_POST["tipo"]:NULL;
	$obj->AbrirConexion();
	$rs = $obj->Query($sql);
	$obj->CerrarConexion();
	$fila = $obj->FetchArray($rs);
	
	echo "<form name=\"formulario\" id=\"formulario\" method=\"post\" onSubmit=\"return validaRegistroPago(this)\">";
	echo "<table style=\"padding:0;margin-top:0px;border-collapse:collapse;\" ";
	echo "width=450 align=center cellpadding=0 cellspacing=0>";
	echo "<tr>";
	echo "<td height=25 colspan=2 valign=top align=\"center\" class=\"FormTitulo\">";
	echo "<input name=\"txtcod\" type=\"hidden\" id=\"txtcod\" value=\"$fila[CPAG]\" />";
	echo "$Titulo - ACTUALIZAR";	
	echo "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"FormCampo1\" width=100>Cliente :</td>";
	echo "<td class=\"interior\" width=300>$fila[CCLI] - $fila[CLIENTE]</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"FormCampo1\">Tipo Pago :</td>";
	echo "<td class=\"interior\">$fila[CTIP] - $fila[TIPO]</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"FormCampo1\">Nro Pago :</td>";
	echo "<td class=\"interior\">$fila[NPAGO]</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"FormCampo1\">Fecha:</td>";
	echo "<td class=\"interior\">$fila[FECHA]</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"FormCampo1\">Monto :</td>";
	echo "<td class=\"interior\">".number_format($fila["MONTO"],"2",".",",")."</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"FormCampo1\">Nuevo Monto :</td>";
	echo "<td class=\"interior\">";
	echo "<input id=\"txtpago\" name=\"txtpago\" type=\"text\" style=\"width:100px;text-align:center\" onkeypress=\"return SoloNumeros(event)\" >";
	echo "</td>";
	echo "</tr>";
	/*
	echo "<tr>";
	echo "<td class=\"FormCampo1\">Observacion :</td>";
	echo "<td class=\"interior\">";
	echo "<textarea name=\"txtobs\" rows=\"5\" id=\"txtobs\" style=\"width:300px;\"></textarea>";
	echo "</td>";
	echo "</tr>";
	*/
	echo "<tr>";
	echo "<td height=25 colspan=10 valign=top aling=\"center\" class=\"FormTitulo\">";		
	echo "<input id=\"save\" name=\"save\" type=\"submit\" style=\"width:100px;height:22px;\" value=\"Registrar Pago\" ";
	echo "onClick=\"return confirm('¿DESEA GUARDAR ESTE PAGO?')\" >&nbsp;&nbsp;";
	echo "<input name=\"regresar\" type=\"button\" id=\"regresar\" style=\"width:100px; height:22px;\" ";
	echo "onClick=\"location='".$Link."'\" value=\"Regresar\">";
	echo "</td>";
	echo "</tr>";
	echo "</table>";	
	echo "</form>";		
	}
if(isset($_POST["save"])) {	
	$Usr = (isset($_SESSION["_CUSE"]))  ? $_SESSION["_CUSE"] : "1";
	$varFecha = FechaMySql($_POST[femi]);	
	$sql = "CALL spMante_Pago_CxC_Modificar($_POST[txtcod], $_POST[txtpago],  '', $Usr)";  
	$obj->AbrirConexion();
	$rs = $obj->Query($sql);
	$obj->CerrarConexion();
	echo "<script language='Javascript'>";
	echo "window.location='$Link'";
	echo "</script>\n";
	}
} 
?>