<?php

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

date_default_timezone_set('Europe/London');

/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2015 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    ##VERSION##, ##DATE##
 */

/** PHPExcel */

include "../../conexion.php";
include "../../parametros.php";
$cr=0;
$sql_datospolitica=mysqli_query($conexion,"SELECT grupoempresarial,count(*) from empresa 
										 group by grupoempresarial
										 order by grupoempresarial asc");
while($datospoli= mysqli_fetch_row($sql_datospolitica)) 
 {
		$codgrupoempresarial=$datospoli[0];
		$total=$datospoli[1];
		
	$narl="";
	$ngrupoempresarial="";
	 $sql_contar =mysqli_query($conexion,"select nombre from grupo_empresarial where id='".$codgrupoempresarial."'");
	 while($datos= mysqli_fetch_row($sql_contar)){  $ngrupoempresarial=$datos[0];  }
	if($ngrupoempresarial<>""){} else {$ngrupoempresarial='No Definido';}	
	$row=1;
	
	
    $Anombre[$cr]=$ngrupoempresarial;
	$Atotal[$cr]=$total;
	$cr++;
 }
$totalitem=$cr;
$tletra=$cr+1;
//$A$5

//$letrasA="Worksheet!$A2:$A".$totalitem;
//$letrasB="Worksheet!$2:$B".$totalitem;
//ECHO $letrasA;
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';

echo "<A href='verdocexcelempresa.php?reporte=7'  >DESCARGAR EXCEL</A><br>";

$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
	array(
		array('',	''),
		array($Anombre[0],   $Atotal[0]),
		array($Anombre[1],   $Atotal[1]),
		array($Anombre[2],   $Atotal[2]),
		array($Anombre[3],   $Atotal[3]),
		array($Anombre[4],   $Atotal[4]),
		array($Anombre[5],   $Atotal[5]),
		array($Anombre[6],   $Atotal[6]),
		array($Anombre[7],   $Atotal[7]),
		array($Anombre[8],   $Atotal[8]),
		array($Anombre[9],   $Atotal[9]),
		array($Anombre[10],   $Atotal[10]),
		array($Anombre[11],   $Atotal[11]),
		array($Anombre[12],   $Atotal[12]),
		array($Anombre[13],   $Atotal[13]),
		array($Anombre[14],   $Atotal[14]),
		array($Anombre[15],   $Atotal[15]),
		array($Anombre[16],   $Atotal[16]),
		array($Anombre[17],   $Atotal[17]),
		array($Anombre[18],   $Atotal[18]),
		array($Anombre[19],   $Atotal[19]),
		array($Anombre[20],   $Atotal[20]),
		array($Anombre[21],   $Atotal[21]),
		array($Anombre[22],   $Atotal[22]),
		array($Anombre[23],   $Atotal[23]),
		array($Anombre[24],   $Atotal[24]),
		array($Anombre[25],   $Atotal[25]),
		array($Anombre[26],   $Atotal[26]),
		array($Anombre[27],   $Atotal[27]),
		array($Anombre[28],   $Atotal[28]),
		array($Anombre[29],   $Atotal[29]),
		
	)
);

$dataSeriesLabels1 = array(
	new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),	//	2011
);



$xAxisTickValues1 = array(
	new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A2:$A'.$totalitem, NULL, $totalitem),	//	Q1 to Q4
);

$dataSeriesValues1 = array(
	new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B2:$B'.$totalitem, NULL, $totalitem),
);

//	Build the dataseries
$series1 = new PHPExcel_Chart_DataSeries(
	PHPExcel_Chart_DataSeries::TYPE_PIECHART,				// plotType
	NULL,			                                        // plotGrouping (Pie charts don't have any grouping)
	range(0, count($dataSeriesValues1)-1),					// plotOrder
	$dataSeriesLabels1,										// plotLabel
	$xAxisTickValues1,										// plotCategory
	$dataSeriesValues1										// plotValues
);

//	Set up a layout object for the Pie chart
$layout1 = new PHPExcel_Chart_Layout();
$layout1->setShowVal(TRUE);
$layout1->setShowPercent(TRUE);

//	Set the series in the plot area
$plotArea1 = new PHPExcel_Chart_PlotArea($layout1, array($series1));
//	Set the chart legend
$legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);

$title1 = new PHPExcel_Chart_Title('EMPRESAS POR GRUPO EMPRESARIAL');


//	Create the chart
$chart1 = new PHPExcel_Chart(
	'chart1',		// name
	$title1,		// title
	$legend1,		// legend
	$plotArea1,		// plotArea
	true,			// plotVisibleOnly
	0,				// displayBlanksAs
	NULL,			// xAxisLabel
	NULL			// yAxisLabel		- Pie charts don't have a Y-Axis
);

//	Set the position where the chart should appear in the worksheet
$chart1->setTopLeftPosition('D2');
$chart1->setBottomRightPosition('L20');

//	Add the chart to the worksheet
$objWorksheet->addChart($chart1);


/*
// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;


// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

// Echo done
echo date('H:i:s') , " Done writing file" , EOL;
echo 'File has been created in ' , getcwd() , EOL;
*/
echo date('H:i:s') , "Se genero el archivo Excel" , EOL;

