<?php
	if(!isset($_SESSION)) session_start();
	include('clases/clase.conexion.php'); 
	include('funciones/variables.php');
	include_once "Spreadsheet/Excel/Writer.php";
	$VarCon = VarServer("MYSQLI");
	$obj = new clsConexion($VarCon[0],$VarCon[1],$VarCon[2],$VarCon[3],$VarCon[4]);

	ini_set("memory_limit","64M");
	
	function setFormatFuncion($varValor1=NULL, $varValor2=NULL, $varFuncion=NULL) {
		$varResp = NULL;
		switch($varFuncion) {
			case "porc":
			case "PORC":
			case "%":
				if($varValor1==0) $varResp = "0.00";
				else $varResp =  $varValor2/$varValor1;
				break;
			case "crec":
				if($Sum[$valor2]==0) $varResp="0.00";
				else $varResp =  $varValor1/$varValor2*100-100;
				break;
			case "suma":
				$varResp = $varValor1+$varValor1;
				break;
			case "resta":
				$varResp = $varValor1-$varValor1;
				break;
			default:
				$varResp = $varValor1+$varValor1;
				break;
			}
		return $varResp;
		}
		
	$ArrayCabe 	 = $_SESSION["_varCab"];
	$ArrayNivel  = $_SESSION["_varNiv"];
	$varSql 	 = $_SESSION["_varSql"];
	$varPag 	 = $_SESSION["_varPag"];
	$ArrayFormat = $_SESSION["_varFor"];
	$varNumNiv = count($ArrayNivel);
	$varNumCol = 0;
	$varNumComb= count($ArrayCabe);
	foreach($ArrayCabe as $varCombina) {
		$varNumCol = $varNumCol + count($varCombina["det"]);
		}
		
	$obj->AbrirConexion();
	$ArrayDato = $obj->Query($varSql);
	$obj->CerrarConexion();
	
	
	$xls = new Spreadsheet_Excel_Writer();
	$xls->send($varPag.".xls");	
	$sheet = $xls->addWorksheet("Data");
	

	//Formato Datos Numero
	$FormDatNum = $xls->addFormat();
	$FormDatNum->setNumFormat("#,##0.00;[Red](#,##0.00)");		
	//Formato Datos Entero
	$FormDatEnt = $xls->addFormat();
	$FormDatEnt->setNumFormat("#,##0;[Red]#,##0");	
	//Formato Datos Porcentaje
	$FormDatPor = $xls->addFormat();
	$FormDatPor->setNumFormat("0.00%");		
	
	//Formato Supervisor Texto
	$FormSupText = $xls->addFormat();
	$FormSupText->setFgColor('yellow');
	$FormSupText->setBold();	
	$FormSupText->setTop(1);
	$FormSupText->setBottom(1);	
	//Formato Supervisor Numero
	$FormSupNum = $xls->addFormat();
	$FormSupNum->setFgColor('yellow');
	$FormSupNum->setNumFormat("#,##0.00;[Red](#,##0.00)");
	$FormSupNum->setBold();	
	$FormSupNum->setTop(1);
	$FormSupNum->setBottom(1);
	//Formato Supervisor Entero
	$FormSupEnt = $xls->addFormat();
	$FormSupEnt->setFgColor('yellow');
	$FormSupEnt->setNumFormat("#,##0;[Red](#,##0)");
	$FormSupEnt->setBold();	
	$FormSupEnt->setTop(1);
	$FormSupEnt->setBottom(1);
	//Formato Supervisor Porcentaje
	$FormSupPor = $xls->addFormat();
	$FormSupPor->setFgColor('yellow');
	$FormSupPor->setNumFormat("#,##0.00%;[Red](#,##0.00)%");
	$FormSupPor->setBold();	
	$FormSupPor->setTop(1);
	$FormSupPor->setBottom(1);
	
	//Formato CD Texto
	$FormCDText = $xls->addFormat();
	$FormCDText->setFgColor('cyan');
	$FormCDText->setBold();	
	$FormCDText->setTop(1);
	$FormCDText->setBottom(1);	
	//Formato CD Numero
	$FormCDNum = $xls->addFormat();
	$FormCDNum->setFgColor('cyan');
	$FormCDNum->setNumFormat("#,##0.00;[Red](#,##0.00)");
	$FormCDNum->setBold();	
	$FormCDNum->setTop(1);
	$FormCDNum->setBottom(1);
	//Formato CD Entero
	$FormCDEnt = $xls->addFormat();
	$FormCDEnt->setFgColor('cyan');
	$FormCDEnt->setNumFormat("#,##0;[Red](#,##0)");
	$FormCDEnt->setBold();	
	$FormCDEnt->setTop(1);
	$FormCDEnt->setBottom(1);
	//Formato CD Porcentaje
	$FormCDPor = $xls->addFormat();
	$FormCDPor->setFgColor('cyan');
	$FormCDPor->setNumFormat("#,##0.00%;[Red](#,##0.00)%");
	$FormCDPor->setBold();	
	$FormCDPor->setTop(1);
	$FormCDPor->setBottom(1);
	//Formato Cabecera
	$FormCab = $xls->addFormat();
	$FormCab->setBorder(1);
	$FormCab->setBold();
	$FormCab->setAlign('center');
	$FormCab->setAlign('vcenter');
	$FormCab->setFgColor(3);
	
	$varFila =0;	
	$sheet->write($varFila,0, $ArrayNivel[0]["COD"], $FormCab);
	$sheet->write($varFila+1,0, "", $FormCab);
	$sheet->setMerge($varFila, 0, $varFila+1, 0);	
	$sheet->write($varFila,1, $ArrayNivel[0]["DES"], $FormCab);
	$sheet->write($varFila+1,1, "", $FormCab);
	$sheet->setMerge($varFila, 1, $varFila+1, 1);
	
	$varCol  = 2;
	$varCol_1= 2;
	$varPos = 6;
	$varDat[0] = $varDat[1] = $varDat[2] = $varDat[3] = $varDat[4] = $varDat[5] = 0;
	foreach($ArrayCabe as $varCombina) {
		$varCol_1 = $varCol;
		foreach($varCombina["det"] as $Data) {
			if(isset($varCombina["factor"])) {
				$varDat[$varPos] = $varCombina["factor"];
				}
			else {
				$varDat[$varPos] = 0;
				}
			$sheet->write($varFila,$varCol, "", $FormCab);
			$varCol = $varCol + 1;
			$varPos = $varPos + 1;
			}
		$sheet->write($varFila,$varCol_1, $varCombina["cab"], $FormCab);
		$sheet->setMerge($varFila,$varCol_1 , $varFila, $varCol-1);
		}
	$varFila = $varFila + 1;
	$varCol  = 2;
	$varCol_1= 2;
	foreach($ArrayCabe as $varCombina) {
		$varCol_1 = $varCol;
		foreach($varCombina["det"] as $Data) {
			$sheet->writeString($varFila,$varCol, $Data, $FormCab);
			$varCol = $varCol + 1;
			}
		}
	
	$varNivel_2_Cod_ant = "";
	$varNivel_2_Cod_act = "";
	$varNivel_2_Nom = "";
	$varNivel_1_Cod_ant = "";
	$varNivel_1_Cod_act = "";
	$varNivel_1_Nom = "";
	
	for($i=0; $i<=$varNumCol; $i++) {
		$varSumNiv_1[$i] = 0;
		$varSumNiv_2[$i] = 0;
		$varSumTotal[$i] = 0;
		}
	while($fila = $obj->FetchArray($ArrayDato)) {			
	
		//Nivel 1 Por lo General Grupo de Supervisores
		if($varNumNiv >= 2) {
			$varPosCol = 4;
			if($varNivel_1_Cod_act=="" && $varNivel_1_Cod_ant=="") {
				$varNivel_1_Cod_act = $fila[$ArrayNivel[1]["COD"]];
				$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
				$varNivel_1_Nom = $fila[$ArrayNivel[1]["DES"]];
				for($i=0; $i<=$varNumCol; $i++) {
					if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_1[$i] = $varSumNiv_1[$i] + floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_1[$i] = $varSumNiv_1[$i] + $fila[$i+$varNumNiv*2-1];		
					}		
				}
			else{
				$varNivel_1_Cod_act = $fila[$ArrayNivel[1]["COD"]];
				$varNivel_1_Cod_ant = $varNivel_1_Cod_ant;
				if($varNivel_1_Cod_act == $varNivel_1_Cod_ant) {
					for($i=0; $i<=$varNumCol; $i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_1[$i] = $varSumNiv_1[$i] + floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_1[$i] = $varSumNiv_1[$i] + $fila[$i+$varNumNiv*2-1];	
						}	
					}
				}
			
			if($varNivel_1_Cod_act <> $varNivel_1_Cod_ant) {
				$varFila = $varFila + 1;
				$sheet->writeString($varFila,0, $varNivel_1_Cod_ant, $FormSupText);
				$sheet->writeString($varFila,1, $varNivel_1_Nom, $FormSupText);
				$varPos = 1;			
				foreach($ArrayCabe as $varCombina) {
					foreach($varCombina["det"] as $Data) {
						if ($varSumNiv_1[$varPos]>0) {
							if($varDat[$varPos+5]<>0) {
								$varFactor = $varDat[$varPos+5];
								$varSuma = floor($varSumNiv_1[$varPos]/$varFactor) + round($varSumNiv_1[$varPos])%round($varFactor)/100;								
								}
							else
								$varSuma = $varSumNiv_1[$varPos];
							//$sheet->write($varFila,$varPos+1, $varSuma, $FormSupNum);
							}
						else
							$varSuma = "";
							//$sheet->write($varFila,$varPos+1, "", $FormSupNum);
						
						if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
							$varPos1 = $varSumNiv_1[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
							$varPos2 = $varSumNiv_1[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
							$varSuma = setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
							}
						$varForSup = $FormSupNum;
						if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $varForSup = $FormSupPor;
						$sheet->write($varFila,$varPos+1, $varSuma, $varForSup);
						$varPos = $varPos + 1;
						
						}
					}				
				$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
				$varNivel_1_Nom     = $fila[$ArrayNivel[1]["DES"]];
				for($i=1; $i<=$varNumCol;$i++) {
					if($varDat[$i+$varNumNiv*2-1]<>0) {
						$varFactor = $varDat[$i+$varNumNiv*2-1];
						$varSumNiv_1[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
						}
					else
						$varSumNiv_1[$i] = $fila[$i+$varNumNiv*2-1];
					}
				}
			}
		
		//Nivel 2: Por lo General Grupo de CD'S
		if($varNumNiv >= 3) {
			$varPosCol = 6;
			$varColData = $i+$varNumNiv*2-1;
			if($varNivel_2_Cod_act=="" && $varNivel_2_Cod_ant=="") {
				$varNivel_2_Cod_act = $fila[$ArrayNivel[2]["COD"]];
				$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
				$varNivel_2_Nom = $fila[$ArrayNivel[2]["DES"]];
				for($i=0; $i<=$varNumCol; $i++) {
					if($varDat[$i+$varNumNiv*2-1]<>0) {
						$varFactor = $varDat[$i+$varNumNiv*2-1];
						$varSumNiv_2[$i] = $varSumNiv_2[$i] + floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
						}
					else
						$varSumNiv_2[$i] = $varSumNiv_2[$i] + $fila[$i+$varNumNiv*2-1];
					}		
				}
			else{
				$varNivel_2_Cod_act = $fila[$ArrayNivel[2]["COD"]];
				$varNivel_2_Cod_ant = $varNivel_2_Cod_ant;
				if($varNivel_2_Cod_act == $varNivel_2_Cod_ant) {
					for($i=0; $i<=$varNumCol; $i++) {
						if($varDat[$i+$varNumNiv*2-1]<>0) {
							$varFactor = $varDat[$i+$varNumNiv*2-1];
							$varSumNiv_2[$i] = $varSumNiv_2[$i] + floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
							}
						else
							$varSumNiv_2[$i] = $varSumNiv_2[$i] + $fila[$i+$varNumNiv*2-1];	
						}	
					}
				}
			
			if($varNivel_2_Cod_act <> $varNivel_2_Cod_ant) {
				$varFila = $varFila + 1;
				$sheet->writeString($varFila,0, $varNivel_2_Cod_ant, $FormCDText);
				$sheet->writeString($varFila,1, $varNivel_2_Nom, $FormCDText);
				$varPos = 1;			
				foreach($ArrayCabe as $varCombina) {
					foreach($varCombina["det"] as $Data) {
						if ($varSumNiv_2[$varPos]>0) {
							if($varDat[$varPos+5]<>0) {
								$varFactor = $varDat[$varPos+5];
								$varSuma = floor($varSumNiv_2[$varPos]/$varFactor) + round($varSumNiv_2[$varPos])%round($varFactor)/100;								
								}
							else
								$varSuma = $varSumNiv_2[$varPos];
							//$sheet->write($varFila,$varPos+1, $varSuma, $FormCDNum);
							}
						else
							$varSuma = "";
							//$sheet->write($varFila,$varPos+1, "", $FormCDNum);
						
						if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
							$varPos1 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
							$varPos2 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
							$varSuma = setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
							}
						$varForCd = $FormCDNum;
						if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $varForCd = $FormCDPor;
						
						$sheet->write($varFila,$varPos+1, $varSuma, $varForCd);
						$varPos = $varPos + 1;
						}
					}				
				$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
				$varNivel_2_Nom     = $fila[$ArrayNivel[2]["DES"]];
				for($i=1; $i<=$varNumCol;$i++) {
					if($varDat[$i+$varNumNiv*2-1]<>0) {
						$varFactor = $varDat[$i+$varNumNiv*2-1];
						$varSumNiv_2[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
						}
					else
						$varSumNiv_2[$i] = $fila[$i+$varNumNiv*2-1];
					}
				$varFila = $varFila + 2;
				$sheet->write($varFila,0, $ArrayNivel[0]["COD"], $FormCab);
				$sheet->write($varFila+1,0, "", $FormCab);
				$sheet->setMerge($varFila, 0, $varFila+1, 0);	
				$sheet->write($varFila,1, $ArrayNivel[0]["DES"], $FormCab);
				$sheet->write($varFila+1,1, "", $FormCab);
				$sheet->setMerge($varFila, 1, $varFila+1, 1);
				
				$varCol  = 2;
				$varCol_1= 2;
				foreach($ArrayCabe as $varCombina) {
					$varCol_1 = $varCol;
					foreach($varCombina["det"] as $Data) {
						$sheet->write($varFila,$varCol, "", $FormCab);
						$varCol = $varCol + 1;
						}
					$sheet->write($varFila,$varCol_1, $varCombina["cab"], $FormCab);
					$sheet->setMerge($varFila,$varCol_1 , $varFila, $varCol-1);
					}
				$varFila = $varFila + 1;
				$varCol  = 2;
				$varCol_1= 2;
				foreach($ArrayCabe as $varCombina) {
					$varCol_1 = $varCol;
					foreach($varCombina["det"] as $Data) {
						$sheet->write($varFila,$varCol, $Data, $FormCab);
						$varCol = $varCol + 1;
						}
					}
				
				}

			}
		$varFila = $varFila + 1;
		$sheet->writeString($varFila,0, $fila[$ArrayNivel[0]["COD"]]);
		$sheet->writeString($varFila,1, $fila[$ArrayNivel[0]["DES"]]);
		$varPos = $varPosCol;
		$varPos = $varNumNiv*2;
		//Recorremos el Contenido		
		foreach($ArrayCabe as $varCombina) {
			foreach($varCombina["det"] as $Data) {
				$varPosEx = $varPos - ($varNumNiv*2-1) + 1;
				$type = $obj->Fieldtype($ArrayDato, $varPos);
				$Tabla.= "<td align=\"right\" class=\"interior\">";
				if (strtoupper($type)=="STRING") {
					$sheet->writeString($varFila,$varPos-4, $fila[$varPos]);
					}
				else {
					$varForDat = $FormDatNum;
					$varValor  = $fila[$varPos];
					if(isset($ArrayFormat[$varPos]["tipodat"]) && $ArrayFormat[$varPos]["tipodat"]=="porc") {
						$varForDat = $FormDatPor;
						$varValor  = $fila[$varPos]/100;
						}
					if ($fila[$varPos]<>0) $sheet->write($varFila,$varPosEx, $varValor, $varForDat);	
					}
				$varPos = $varPos + 1;
				}
			}				
		}
	
	//Ultimo Nivel 1
	if($varNumNiv >= 2) {
		$varFila = $varFila + 1;
		$sheet->writeString($varFila,0, $varNivel_1_Cod_ant, $FormSupText);
		$sheet->writeString($varFila,1, $varNivel_1_Nom, $FormSupText);
		$varPos = 1;
		$varPosSum  = $varNumNiv*2-1;		
		foreach($ArrayCabe as $varCombina) {
			foreach($varCombina["det"] as $Data) {
				if ($varSumNiv_1[$varPos]>0) {
					if($varDat[$varPos+$varPosSum]<>0) {
						$varFactor = $varDat[$varPos+$varPosSum];
						$varSuma = floor($varSumNiv_1[$varPos]/$varFactor) + round($varSumNiv_1[$varPos])%round($varFactor)/100;								
						}
					else
						$varSuma = $varSumNiv_1[$varPos];
					//$sheet->write($varFila,$varPos+1, $varSuma, $FormSupNum);
					}
				else
					$varSuma = "";
					//$sheet->write($varFila,$varPos+1, "", $FormSupNum);
				if(isset($ArrayFormat[$varPos+$varPosSum]["formula"]) && count($ArrayFormat[$varPos+$varPosSum]["formula"])>0) {
					$varPos1 = $varSumNiv_1[$ArrayFormat[$varPos+$varPosSum]["formula"]["valor1"]-$varPosSum];
					$varPos2 = $varSumNiv_1[$ArrayFormat[$varPos+$varPosSum]["formula"]["valor2"]-$varPosSum];
					$varSuma = setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+$varPosSum]["formula"]["funcion"]);
					}
				$varForSup = $FormSupNum;
				if(isset($ArrayFormat[$varPos+$varPosSum]["tipodat"]) && $ArrayFormat[$varPos+$varPosSum]["tipodat"]=="porc")  $varForSup = $FormSupPor;
				$sheet->write($varFila,$varPos+1, $varSuma, $varForSup);

				$varPos = $varPos + 1;
				}
			}				
		$varNivel_1_Cod_ant = $fila[$ArrayNivel[1]["COD"]];
		$varNivel_1_Nom     = $fila[$ArrayNivel[1]["DES"]];
		for($i=1; $i<=$varNumCol;$i++) {
			if($varDat[$i+$varNumNiv*2-1]<>0) {
				$varFactor = $varDat[$i+$varNumNiv*2-1];
				$varSumNiv_1[$i] = floor($fila[$i+$varNumNiv*2-1])*$varFactor + ($fila[$i+$varNumNiv*2-1]-floor($fila[$i+$varNumNiv*2-1]))*100;
				}
			else
				$varSumNiv_1[$i] = $fila[$i+$varNumNiv*2-1];
			}
		}
	if($varNumNiv >= 3) {
		$varFila = $varFila + 1;
		$sheet->writeString($varFila,0, $varNivel_2_Cod_ant, $FormCDText);
		$sheet->writeString($varFila,1, $varNivel_2_Nom, $FormCDText);
		$varPos = 1;			
		foreach($ArrayCabe as $varCombina) {
			foreach($varCombina["det"] as $Data) {
				if ($varSumNiv_2[$varPos]>0) {
					if($varDat[$varPos+5]<>0) {
						$varFactor = $varDat[$varPos+5];
						$varSuma = floor(round($varSumNiv_2[$varPos])/$varFactor) + (round($varSumNiv_2[$varPos],0))%$varFactor/100;								
						}
					else
						$varSuma = $varSumNiv_2[$varPos];
					//$sheet->write($varFila,$varPos+1, $varSuma, $FormCDNum);
					}
				else
					$varSuma = "";
					//$sheet->write($varFila,$varPos+1, "", $FormCDNum);
				if(isset($ArrayFormat[$varPos+5]["formula"]) && count($ArrayFormat[$varPos+5]["formula"])>0) {
					$varPos1 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor1"]-5];
					$varPos2 = $varSumNiv_2[$ArrayFormat[$varPos+5]["formula"]["valor2"]-5];
					$varSuma = setFormatFuncion($varPos1, $varPos2, $ArrayFormat[$varPos+5]["formula"]["funcion"]);
					}
				$varForCd = $FormCDNum;
				if(isset($ArrayFormat[$varPos+5]["tipodat"]) && $ArrayFormat[$varPos+5]["tipodat"]=="porc")  $varForCd = $FormCDPor;
				
				$sheet->write($varFila,$varPos+1, $varSuma, $varForCd);
				$varPos = $varPos + 1;
				}		
			}			
		$varNivel_2_Cod_ant = $fila[$ArrayNivel[2]["COD"]];
		$varNivel_2_Nom     = $fila[$ArrayNivel[2]["DES"]];
		for($i=1; $i<=$varNumCol;$i++) {
			$varSumNiv_2[$i] =  $fila[$i+$varNumNiv*2-1];
			}
		}
	$xls->close();		
?>
