<?php

session_start();

date_default_timezone_set('America/Lima');

if (PHP_SAPI == 'cli')

  die('Se visualiza solamente desde un navegador.');

/** Include PHPExcel */

require_once dirname(__FILE__) . '/../../libreriasphp/phpexcel/Classes/PHPExcel.php';

require_once ("../../config/Cado.php");

require_once ("../../app/producto/cProducto.php");

$oProducto = new cProducto();

require_once ("../../app/formatos/formato.php");

$dts1=$oProducto->mostar_venta_producto(fecha_mysql($_GET['fec1']),fecha_mysql($_GET['fec2']),$_GET['docid']);

$num_rows= mysqli_num_rows($dts1);

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("")

->setLastModifiedBy("infosisperu.com.pe")

->setTitle("Reporte generado por infoisperu.com.pe")

->setSubject("Reporte")

->setDescription("")

->setKeywords("")

->setCategory("reporte excel infosisperu");

$objPHPExcel->setActiveSheetIndex(0);

$c=1;

$estiloTituloColumnas = array(

  'font' => array(

    'name'  => 'Arial',

    'bold'  => true,

    'size'  =>8,

    'color' => array(

      'rgb' => '000000'

      )

    ),

  'fill' => array(

    'type'  => PHPExcel_Style_Fill::FILL_SOLID,

    'color' => array(

      'rgb' => 'FAFAFA')

    ),

  'borders' => array(

    'outline' => array(

      'style' => PHPExcel_Style_Border::BORDER_THIN ,

      'color' => array(

        'rgb' => '143860'

        )

      )

    ),

  'alignment' =>  array(

    'horizontal'=> PHPExcel_Style_Alignment::HORIZONTAL_CENTER,

    'vertical'  => PHPExcel_Style_Alignment::VERTICAL_CENTER,

    'wrap'      => TRUE

    )

  );

  $titulosColumnas = array(

  'PRODUCTO',

  'MARCA',

  'CANTIDAD',

  'PRECIO'

  );

  $letras = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

  $letracolumn = 0;

  for ($i=0; $i < count($titulosColumnas) ; $i++) { 

    $letracolumn = $i;

    $objPHPExcel->getActiveSheet()->setCellValue($letras[$i].$c, $titulosColumnas[$i]);

  }

  $objPHPExcel->getActiveSheet()->getStyle("A$c:".$letras[$letracolumn]."$c")->applyFromArray($estiloTituloColumnas);

  if($num_rows>0){

    $c=$c+1;

    while($dt1 = mysqli_fetch_array($dts1))

    {

	

    $objPHPExcel->getActiveSheet(0)

    ->setCellValue('A'.$c, $dt1['nombre'])

    ->setCellValue('B'.$c, $dt1['marca'])    

    ->setCellValue('C'.$c, $dt1['totalven']*1)

    ->setCellValue('D'.$c, formato_money($dt1['preuni']));

    //$objPHPExcel->getActiveSheet(0)->getStyle("D$c:I$c")->getNumberFormat()->setFormatCode('0.00');

    $c++;

	}

	mysqli_free_result($dts1);

				}

	

for($z = 'A'; $z <= 'L'; $z++){

  $objPHPExcel->getActiveSheet()->getColumnDimension($z)->setAutoSize(TRUE);

}			

$objPHPExcel->getActiveSheet()->setTitle('TABLA VENTA - PRODUCTO');

$nombre_archivo="Informe venta - producto ".date('d-m-Y-h:i');

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header('Content-Disposition: attachment;filename="'.$nombre_archivo.'.xlsx"');

header('Cache-Control: max-age=0');

header('Cache-Control: max-age=1');

header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past

header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified

header ('Cache-Control: cache, must-revalidate'); 

header ('Pragma: public'); 

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save('php://output');

exit;

?>

