<?php
require_once('clases/xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
//$xajax->setFlag('debug',true); 
$xajax->configure('javascript URI', 'clases/xajax/');
function xExportar($varForm) {
	$objResponse=new xajaxResponse();
	$sql = "call spQuery_Rentabilidad_Producto('$varForm[anio]',$varForm[pcod]);";
	$varCab = array(array("cab"=>"ENE ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"FEB ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"MAR ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"ABR ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"MAY ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"JUN ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"JUL ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"AGO ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"SET ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"OCT ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"NOV ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"DIC ".$varForm["anio"], "det"=>array("CAN","PPC","PPV","UTI","PUB")),
					array("cab"=>"TOTAL ".$varForm["anio"], "det"=>array("CAN","UTI","PUB"))
					);
	$varNiv = array(0 =>array("COD"=>"CPRO","DES"=>"PRODUCTO"),
					1 =>array("COD"=>"XPRO","DES"=>"PROVEEDOR"));
	$varFor = array(5 =>array("decimal"=>"5"),6 =>array("decimal"=>"5"),8 =>array("tipodat"=>"porc"),
						10=>array("decimal"=>"5"),11=>array("decimal"=>"5"),13=>array("tipodat"=>"porc"),
						15=>array("decimal"=>"5"),16=>array("decimal"=>"5"),18=>array("tipodat"=>"porc"),
						20=>array("decimal"=>"5"),21=>array("decimal"=>"5"),23=>array("tipodat"=>"porc"),
						25=>array("decimal"=>"5"),26=>array("decimal"=>"5"),28=>array("tipodat"=>"porc"),
						30=>array("decimal"=>"5"),31=>array("decimal"=>"5"),33=>array("tipodat"=>"porc"),
						35=>array("decimal"=>"5"),36=>array("decimal"=>"5"),38=>array("tipodat"=>"porc"),
						40=>array("decimal"=>"5"),41=>array("decimal"=>"5"),43=>array("tipodat"=>"porc"),
						45=>array("decimal"=>"5"),46=>array("decimal"=>"5"),48=>array("tipodat"=>"porc"),
						50=>array("decimal"=>"5"),51=>array("decimal"=>"5"),53=>array("tipodat"=>"porc"),
						55=>array("decimal"=>"5"),56=>array("decimal"=>"5"),58=>array("tipodat"=>"porc"),
						60=>array("decimal"=>"5"),61=>array("decimal"=>"5"),63=>array("tipodat"=>"porc"),
						66=>array("tipodat"=>"porc")
						);
	$_SESSION["_varNiv"] = $varNiv;
	$_SESSION["_varPag"] = $varForm["titu"];
	$_SESSION["_varCab"] = $varCab;
	$_SESSION["_varSql"] = $sql;
	$_SESSION["_varFor"] = $varFor;
	$objResponse->redirect("export_grid_combinado.php?page=".$varForm["titu"]);
    return $objResponse;
	}
	
function xBuscar($varForm) {
	$var = VarServer("MYSQLI");
	$obj = new clsMargenProducto($var[0],$var[1],$var[2],$var[3],$var[4]);	
	$varTabla = $obj->GridDatos($varForm);
	$respuestaXajax = new xajaxResponse();
	$respuestaXajax->assign("divDatos", "innerHTML", utf8_encode($varTabla));
	$varMarcar="ResaltarSeleccionado()";
	$respuestaXajax->script($varMarcar); 
	return $respuestaXajax;
	}

$xajax->register(XAJAX_FUNCTION, 'xBuscar');
$xajax->register(XAJAX_FUNCTION, 'xExportar');
$xajax->register(XAJAX_FUNCTION, 'seleccionar_proveedor');
$xajax->processRequest();
$xajax->printJavascript("clases/xajax");
?>
<script language="javascript">		
	function xBuscarJS() {		
		if (xajax.$("anio") && xajax.$("anio").value=="Ninguno") { 
			alert("ERROR AL PROCESAR DATOS...!!!!!\nDEBE SELECCIONAR UN ANIO");
			xajax.$("anio").focus()
			return (false); 
			}
		else {
			xajax_xBuscar(xajax.getFormValues('form'));
			}		
		}
		
	function xExportarJS() {
		if (xajax.$("anio") && xajax.$("anio").value=="Ninguno") { 
			alert("ERROR AL PROCESAR DATOS...!!!!!\nDEBE SELECCIONAR UN ANIO");
			xajax.$("anio").focus()
			return (false); 
			}
		else if (xajax.$("pcod") && xajax.$("pcod").value=="") { 
			alert("ERROR AL PROCESAR DATOS...!!!!!\nDEBE SELECCIONAR UN PROVEEDOR");
			xajax.$("pcod").focus()
			return (false); 
			}
		else {
			xajax_xExportar(xajax.getFormValues('form'));
			}
		}
	
	function xImprimirJS() {
		var varAni = xajax.$("anio").value;
		var varPro = xajax.$("pcod").value;		
		if (xajax.$("anio") && xajax.$("anio").value=="Ninguno") { 
			alert("ERROR AL PROCESAR DATOS...!!!!!\nDEBE SELECCIONAR UN ANIO");
			xajax.$("anio").focus()
			return (false); 
			}
		else if (xajax.$("pcod") && xajax.$("pcod").value=="") { 
			alert("ERROR AL PROCESAR DATOS...!!!!!\nDEBE SELECCIONAR UN PROVEEDOR");
			xajax.$("pcod").focus()
			return (false); 
			}
		else {
			var Ruta = 'print/print_gerencial_margen_producto.php?anio='+varAni+'&pro='+varPro;
			testwindow= window.open (Ruta, "print_gerencial_margen_producto", "location=no,status=no,toolbar=no,scrollbars=1,width=900,height=500");
			}
		}

</script>
<?php		
function MyTable($obj, $titulo, $Opcion, $Valor) {
	if(!isset($Opcion)) {
		echo "<div id=\"divForm\">";
		echo $obj->FormuBusqueda();
		echo "<div id=\"divDatos\">";
		//echo $obj->GridDatos();		
		echo "</div>";
		echo "</div>";
		}
	}
?>