<?php

 // ini_set('display_errors',1);

if (PHP_SAPI == 'cli')
  die('Se visualiza solamente desde un navegador.');
require_once dirname(__FILE__) . '/../../libreriasphp/phpexcel/Classes/PHPExcel.php';
date_default_timezone_set('America/Lima');
session_start();
require_once ("../../config/Cado.php");


require_once ("../../app/bonificacionvendedor_cst/cBonificacionvendedor_cst.php");
$oBonificacionvendedor_cst = new cBonificacionvendedor_cst();

require_once ("../../app/usuario/cUsuario.php");
$oUsuario = new cUsuario();
require_once ("../../app/formatos/fechas.php");
require_once ("../../app/formatos/formato.php");


$fec1 = $_GET["fec1"];
list($anio1,$mes1,$dia1) = explode('-',$fec1);

$fec2 = $_GET["fec2"];
list($anio2,$mes2,$dia2) = explode('-',$fec2);

$titulosColumnas = [];
$titulosColumnas[] = "VENDEDOR";
	 
for($i=$dia1;$i<=$dia2;$i++){
	$i = $i*1;
	$fec = $i.'-'.$mes1.'-'.$anio1;
	$titulosColumnas[] = $fec;
};

$titulosColumnas[] = "TOTAL";

//echo $dias;

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("https://infosisperu.com")
->setLastModifiedBy("https://infosisperu.com")
->setTitle("REPORTE BONIFICACION VENDEDOR")
->setSubject("Reporte")
->setDescription("Reporte generado por https://infosisperu.com")
->setKeywords("")
->setCategory("reporte excel");
$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
    )
  );
  
  $estiloTitulos = array(
  'font' => array(
    'name'  => 'Arial',
    'bold'  => true,
    'size'  =>8,
    'color' => array(
      'rgb' => 'FFFFFF'
      )
    ),
  'fill' => array(
    'type'  => PHPExcel_Style_Fill::FILL_SOLID,
    'color' => array(
      'rgb' => 'FF8800')
    ),
  '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
    )
  );
  
  $estiloTotales = array(
  'font' => array(
    'name'  => 'Arial',
    'bold'  => true,
    'size'  =>8,
    'color' => array(
      'rgb' => 'FFFFFF'
      )
    ),
  'fill' => array(
    'type'  => PHPExcel_Style_Fill::FILL_SOLID,
    'color' => array(
      'rgb' => '0d47a1')
    ),
  '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
    )
  );
     
 
  
 // print_r($titulosColumnascompras);
 
 //echo $titulosColumnascompras.'--';
 
	$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','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ',);

    $objPHPExcel->getActiveSheet()->setCellValue("A$c", 'BONIFICACIONES POR VENDEDOR');
	
	
	
	$c = $c+1;
  
	$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);
	$c = $c+1;
 
 
 $dts=$oUsuario->mostrarusuario_xpuntoventa($_GET['punven_id']);
 
  while($dt = mysqli_fetch_array($dts)){
      
    $l = 0;
		
	$objPHPExcel->getActiveSheet(0)
	->setCellValue('A'.$c, $dt['tb_usuario_nom'].' '.$dt['tb_usuario_ape']);
      
      
      
      
      for($i=$dia1;$i<=$dia2;$i++){
          $l++;
          $i = $i * 1;
        $fec = $i.'-'.$mes1.'-'.$anio1;
		$usu_id = $dt['tb_usuario_id'];
					
					
	$dts7 = $oBonificacionvendedor_cst->verSuma_BonificacionVendedorFecha(fecha_mysql($fec),$usu_id);
	$dt7 = mysqli_fetch_array($dts7);
	$totalventa = $dt7['totalventa'];
	
	$dts8 = $oBonificacionvendedor_cst->verSuma_clienteVendedorFecha(fecha_mysql($fec),$usu_id);
				$dt8 = mysqli_fetch_array($dts8);
				$totalcliente = $dt8['totalcliente']*1;
	
	
		$objPHPExcel->getActiveSheet(0)
			->setCellValue($letras[$l].$c, ''.formato_money($totalventa).' - '.$totalcliente);
         $data["total_".$i] += $totalventa;
        $data["totalventa_".$usu_id] += $totalventa;
      }
      
     
       $l++;
      	$objPHPExcel->getActiveSheet(0)
            ->setCellValue($letras[$l].$c, ''.formato_money($data["totalventa_".$usu_id]));
      
      
    $c++;  
  }
 
 
 $c = $c + 1;
 
 $objPHPExcel->getActiveSheet(0)
	->setCellValue('A'.$c, "TOTAL");
      
      $l = 0;
            
      for($i=$dia1;$i<=$dia2;$i++){
          $l++;
        $i = $i * 1;
		$objPHPExcel->getActiveSheet(0)
			->setCellValue($letras[$l].$c, ''.formato_money($data["total_".$i]));
      }
    
      
 
 
 $letracolumn = 0;
	
for($z = 'A'; $z <= 'Z'; $z++){
  $objPHPExcel->getActiveSheet()->getColumnDimension($z)->setAutoSize(TRUE);
}
	
 
	
	

	$objPHPExcel->getActiveSheet()->setTitle('TOTAL BONIFICACION POR VENDEDOR');
	
	$nombre_archivo="BONIFICACION_VENDEDOR".date('d-m-Y H-i:s');
// Redirect output to a client’s web browser (Excel5)
//header('Content-Type: application/vnd.ms-excel');
//header('Content-Disposition: attachment;filename="'.$nombre_archivo.'.xls"');
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$nombre_archivo.'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.date('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
//$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>