<?php
  session_start();
  //error_reporting(E_ALL);
  error_reporting(0);
  
 set_time_limit(0);
  ini_set('memory_limit','256M'); 
  ob_start();
	
  define('__ROOT__', dirname(dirname(__FILE__))); 
 // require_once(__ROOT__.'\siga\html\ClsGeneradorHtml.class.php'); 
 // require_once(__ROOT__.'\siga\Connections\dbms.class.php');
 // require_once(__ROOT__.'\siga\Connections\dbmy.class.php');

   require_once('html/ClsGeneradorHtml.class.php'); 
   require_once('Connections/dbms.class.php');
   require_once('Connections/dbmy.class.php');
	
	extract ($_POST); ///EXTRAE VARIABLES DE POST A VARIABLES SIMPLES CON EL MISMO NOMBRE
	extract ($_GET);

	function celda($letrai,$posi,$letraf,$posf,$valor,$hojaActiva,$tamano,$negrita){
			$hojaActiva->mergeCells($letrai.$posi.':'.$letraf.$posf);
			$hojaActiva->getStyle($letrai.$posi,$letraf.$posf)->getAlignment()->setWrapText(true);
			$hojaActiva->getStyle($letrai.$posi,$letraf.$posf)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$hojaActiva->getStyle($letrai.$posi,$letraf.$posf)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$hojaActiva->getStyle($letrai.$posi.':'.$letraf.$posf)->getBorders()->getOutline()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$hojaActiva->setCellValue($letrai.$posi,utf8_decode($valor));
			$hojaActiva->getStyle($letrai.$posi.':'.$letraf.$posf)->getFont()->setBold($negrita)->setSize($tamano);

																
	}
	function celda1($letrai,$posi,$letraf,$posf,$valor,$hojaActiva,$tamano,$negrita){
			$hojaActiva->mergeCells($letrai.$posi.':'.$letraf.$posf);
			$hojaActiva->getStyle($letrai.$posi,$letraf.$posf)->getAlignment()->setWrapText(true);
			$hojaActiva->getStyle($letrai.$posi,$letraf.$posf)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$hojaActiva->getStyle($letrai.$posi,$letraf.$posf)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$hojaActiva->getStyle($letrai.$posi.':'.$letraf.$posf)->getBorders()->getOutline()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$hojaActiva->setCellValue($letrai.$posi,$valor);
			$hojaActiva->getStyle($letrai.$posi.':'.$letraf.$posf)->getFont()->setBold($negrita)->setSize($tamano);

																
	}
	if (!empty($cmdImprimir)){
		
			//require_once (__ROOT__.'\siga\phpexcel\Classes\PHPExcel\IOFactory.php');
			require_once ('phpexcel/Classes/PHPExcel/IOFactory.php');
                       $objPHPExcel = PHPExcel_IOFactory::load("Estadisticas_Inscritos.xls");	

			$hojaActiva=$objPHPExcel->setActiveSheetIndex(0);
			$bd =new dbms('mysql','no');
	    
			$cedula_alumno=$_GET["cedula_alumno"];
			if(empty($cedula_alumno)){
				$cedula_alumno=$cedula_alumno;
			}
			$sql ="SELECT count(distinct(id_alumno))as total, semestre as Semestre,mencion,nombre_sede as Sede  FROM   `inscripciones` inner join sedes on inscripciones.sede = sedes.codigo WHERE `id_periodo` = 70 group by semestre,sede,mencion order by sede,semestre";
								$posinicial = 9;
								$posifinal=9;	
							 
			$bd->EjecutarQuery($sql);
			$data=$bd->DatosArray();
			if (($bd->count()) > 0){
				foreach($data as $clave=>$registro){
					celda1('A',$posinicial,'C',$posifinal,$data[$clave]['Semestre'],$hojaActiva,8,false);
					celda1('J',$posinicial,'M',$posifinal,$data[$clave]['mencion'],$hojaActiva,8,false);
					celda1('N',$posinicial,'AH',$posifinal,$data[$clave]['Sede'],$hojaActiva,8,false);
					celda1('AI',$posinicial,'AO',$posifinal,$data[$clave]['total'],$hojaActiva,8,false);

					$posinicial=$posifinal+1;			
					$posifinal=$posifinal+1;
	
				}
			}
								
																 
							 
			//ob_get_flush();
				ob_end_clean();
    
    
    
					// Redirect output to a clients web browser (Excel5)
					header('Content-Type: application/vnd.ms-excel;charset=utf-8');
					header('Content-Disposition: attachment;filename="Estadisticas de Inscritos.xls"');
					header('Cache-Control: max-age=0');

					$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
					///$objWriter->save('./pdf_files/ICR_'.$mes.'-'.$ano.'.xls');
					$objWriter->save('php://output');
	}else{	

		$ClsGeneradorHtml=new ClsGeneradorHtml("","");
		$forma= $ClsGeneradorHtml->Pagina_Top("Impresion de Estadisticas de Inscritos","",$script);
		echo $forma;
		echo $ClsGeneradorHtml->Pagina_Inicio_Body();
		 echo "<FORM  method='POST' name='frmcerrar' target='_self' title='Cerrar'>
					<center>
						 <INPUT TYPE='SUBMIT'  name='cmdImprimir' value='Imprimir' align='middle'   title='Generar' >
						<INPUT TYPE='SUBMIT'   name='cmdCerrar' value='Cerrar' align='middle'  onClick='window.close();' title='Cerrar' >
			  </center>
			 </FORM>";	
		echo $ClsGeneradorHtml->Pagina_Fin_Body();
		echo $ClsGeneradorHtml->Pagina_Fin_Html();
		

	}		
?>
