<?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/ventasucursalhora_cst/cVentasucursalhora_cst.php");
$oVentasucursalhora_cst = new cVentasucursalhora_cst();

require_once ("../../app/puntoventa/cPuntoventa.php");
$oPuntoventa = new cPuntoventa();

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


$fec1 = $_GET["fec1"];
list($anio,$mes,$dia) = explode('-',$fec1);

$titulosColumnas = [];
$titulosColumnas[] = "SUCURSAL";

$arraydia = array('12:00 AM', '01:00 AM', '02:00 AM','03:00 AM', '04:00 AM','05:00 AM', '06:00 AM','07:00 AM', 
				'08:00 AM','09:00 AM', '10:00 AM','11:00 AM','12:00 PM', '01:00 PM', '02:00 PM','03:00 PM', '04:00 PM','05:00 PM', '06:00 PM','07:00 PM', 
				'08:00 PM','09:00 PM', '10:00 PM','11:00 PM');
$array_numdia = count($arraydia);
				
for($i=0;$i<$array_numdia;$i++){
	$titulosColumnas[] = $arraydia[$i];
};

$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 CONSOLIDADO CONTABLES")
->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", 'VENTAS POR HORA');
	
	
	
	$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=$oPuntoventa->filtrar('','');
 
  while($dt = mysqli_fetch_array($dts)){
      $totalfila = 0;
    $l = 0;
		
	$objPHPExcel->getActiveSheet(0)
	->setCellValue('A'.$c, $dt['tb_puntoventa_nom']);
      
      
      for($i=0;$i<$array_numdia;$i++){
			$l++;
        	$punven_id = $dt['tb_puntoventa_id'];
					if($i==0){
						$des = strtotime($anio."-".$mes."-".$dia." 12:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 12:59:00 am");
					}else if($i==1){
						$des = strtotime($anio."-".$mes."-".$dia." 01:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 01:59:00 am");
					}else if($i==2){
						$des = strtotime($anio."-".$mes."-".$dia." 02:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 02:59:00 am");
					}else if($i==3){
						$des = strtotime($anio."-".$mes."-".$dia." 03:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 03:59:00 am");
					}else if($i==4){
						$des = strtotime($anio."-".$mes."-".$dia." 04:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 04:59:00 am");
					}else if($i==5){
						$des = strtotime($anio."-".$mes."-".$dia." 05:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 05:59:00 am");
					}else if($i==6){
						$des = strtotime($anio."-".$mes."-".$dia." 06:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 06:59:00 am");
					}else if($i==7){
						$des = strtotime($anio."-".$mes."-".$dia." 07:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 07:59:00 am");
					}else if($i==8){
						$des = strtotime($anio."-".$mes."-".$dia." 08:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 08:59:00 am");
					}else if($i==9){
						$des = strtotime($anio."-".$mes."-".$dia." 09:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 09:59:00 am");
					}else if($i==10){
						$des = strtotime($anio."-".$mes."-".$dia." 10:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 10:59:00 am");
					}else if($i==11){
						$des = strtotime($anio."-".$mes."-".$dia." 11:00:00 am");
						$has = strtotime($anio."-".$mes."-".$dia." 11:59:00 am");
					}else if($i==12){
						$des = strtotime($anio."-".$mes."-".$dia." 12:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 12:59:00 pm");
					}else if($i==13){
						$des = strtotime($anio."-".$mes."-".$dia." 01:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 01:59:00 pm");
					}else if($i==14){
						$des = strtotime($anio."-".$mes."-".$dia." 02:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 02:59:00 pm");
					}else if($i==15){
						$des = strtotime($anio."-".$mes."-".$dia." 03:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 03:59:00 pm");
					}else if($i==16){
						$des = strtotime($anio."-".$mes."-".$dia." 04:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 04:59:00 pm");
					}else if($i==17){
						$des = strtotime($anio."-".$mes."-".$dia." 05:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 05:59:00 pm");
					}else if($i==18){
						$des = strtotime($anio."-".$mes."-".$dia." 06:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 06:59:00 pm");
					}else if($i==19){
						$des = strtotime($anio."-".$mes."-".$dia." 07:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 07:59:00 pm");
					}else if($i==20){
						$des = strtotime($anio."-".$mes."-".$dia." 08:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 08:59:00 pm");
					}else if($i==21){
						$des = strtotime($anio."-".$mes."-".$dia." 09:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 09:59:00 pm");
					}else if($i==22){
						$des = strtotime($anio."-".$mes."-".$dia." 10:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 10:59:00 pm");
					}else if($i==23){
						$des = strtotime($anio."-".$mes."-".$dia." 11:00:00 pm");
						$has = strtotime($anio."-".$mes."-".$dia." 11:59:00 pm");
					}
			
	
			$dts7 = $oVentasucursalhora_cst->mostrarSumaVentaxHora($des,$has,$punven_id);
				$dt7 = mysqli_fetch_array($dts7);
				$totalventa = $dt7['sumtot'];
					
	
		$objPHPExcel->getActiveSheet(0)
			->setCellValue($letras[$l].$c, ($totalventa));
			
        $data["total_".$i] += $totalventa;
		$totalfila += $totalventa;
      

        
      }
      
      
     $l++;
     $objPHPExcel->getActiveSheet(0)
			->setCellValue($letras[$l].$c, ($totalfila));
     
    $c++;  
  }
 
 
 $c = $c + 1;
 
 $objPHPExcel->getActiveSheet(0)
	->setCellValue('A'.$c, "TOTAL");
      
      $l = 0;
            
     for($i=0;$i<$array_numdia;$i++){
        $l++;
        $i = $i * 1;
		$objPHPExcel->getActiveSheet(0)
			->setCellValue($letras[$l].$c, ($data["total_".$i]));
      }
    
      
 
 
 $letracolumn = 0;
	
for($z = 'A'; $z <= 'Z'; $z++){
  $objPHPExcel->getActiveSheet()->getColumnDimension($z)->setAutoSize(TRUE);
}
	
 
	
	

	$objPHPExcel->getActiveSheet()->setTitle('VENTAS POR SUCURSAL');
	
	$nombre_archivo="VENTASSUCURSAL".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;
?>