<?php
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;
	echo '<form name="formulario" id="formulario" method=post onSubmit="">';
	echo "<table style=\"padding:0;margin-top:0px;border-collapse:collapse;\" ";
	echo "width=420 align=center cellpadding=0 cellspacing=0>";
	echo "<tr>";
	echo "<td height=25 colspan=2 valign=top align=\"center\" class=\"FormTitulo\">$Titulo</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"FormCampo1\" width=80>Cliente :</td>";
	echo "<td class=\"interior\" width=320>";	
	echo "<input id=\"ccod\" name=\"ccod\" type=\"text\" style=\"width:60px;text-align:center\" ";
	echo "ondblclick=\"cargarCliente('ccod', 'cnom')\" value = \"$varCCOD\" onblur=\"select_Cliente('ccod', 'cnom')\" ";
	echo "onkeypress=\"return SoloNumeros(event)\" >";
	echo "&nbsp;&nbsp;<input id=\"cnom\" name=\"cnom\" type=\"text\" style=\"width:250px\" value = \"$varCNOM\" readonly\">";	
	echo "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td class=\"FormCampo1\" width=80>Fecha :</td>";
	echo "<td class=\"interior\" width=320>";
	echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	echo "<tr>";
	echo "<td width=\"90\" height=\"22\" valign=\"middle\">";
	echo "<input name=eini type=text id=eini style=\"width:80px; text-align:center\" value=\"$varFINI\">";
	echo "</td>";
	echo "<td width=\"40\" valign=\"middle\">";
	echo "<a href=\"javascript:void(0)\" ";
	echo "onClick=\"if(self.gfPop)gfPop.fPopCalendar(document.formulario.eini);return false;\" >";
	echo "<img class=PopcalTrigger src=\"HelloWorld/calbtn.gif\" width=34 height=22 border=0></a>";
	echo "</td>";
	echo "<td width=\"90\" height=\"22\" valign=\"middle\">";
	echo "<input name=efin type=text id=efin style=\"width:80px;  text-align:center\" value=\"$varFFIN\">";
	echo "</td>";
	echo "<td width=\"40\" valign=\"middle\">";
	echo "<a href=\"javascript:void(0)\" ";
	echo "onClick=\"if(self.gfPop)gfPop.fPopCalendar(document.formulario.efin);return false;\" >";
	echo "<img class=PopcalTrigger src=\"HelloWorld/calbtn.gif\" width=34 height=22 border=0></a>";
	echo "</td>";
	echo "<td width=\"104\" valign=\"middle\">&nbsp;".date("d/m/Y",time())."</td>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td height=25 colspan=2 valign=top align=\"center\" class=\"FormTitulo\">";
	echo "<input id=\"buscar\" name=\"buscar\" type=\"submit\" style=\"width:100px; ";
	echo "height:22px; font-size:10px; \" value=\"Consultar\">";
	echo "</td>";
	echo "</tr>";	
	echo "</table>";
	echo '</form>';
	if(isset($_POST["buscar"])) {
		$varFIni = ($_POST["eini"]=="") ? "NULL" : "'".FechaMySql($_POST["eini"])."'";
		$varFFin = ($_POST["efin"]=="") ? "NULL" : "'".FechaMySql($_POST["efin"])."'";
		
		$sql = "call spQuery_Pagos('$_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_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 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 P 
			inner join _sys_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 = $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_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";
	}
} 
?>