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

require_once ("../../app/notacredito/cNotacredito.php");
$oNotacredito = new cNotacredito();
$fec = date('Y-m-d');
require_once ("../../app/formatos/formato.php");
require_once ("../../app/formatos/fechas.php");
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("https://infosisperu.com")
->setLastModifiedBy("https://infosisperu.com")
->setTitle("REPORTE CONSOLIDADO IGV")
->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
    )
  );
  
  $titulos = array(
  'FECHA EMISION',
  'EMPRESA',
  'DOCUMENTO',
  'DOC. MODIFICA',
  'RUC',
  'CLIENTE', 
  'MONEDA',
  'TOTAL'
  );
  
  $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');
  
	$objPHPExcel->getActiveSheet()->setCellValue("A$c", 'REPORTE DE NOTAS DE CREDITO');
	//$objPHPExcel->getActiveSheet()->margeCells("A1:G1");
	$objPHPExcel->getActiveSheet()->getStyle("A$c:H$c")->applyFromArray($estiloTitulos);
	
	$c = $c+1;
  
  $letracolumn = 0;
  
  for ($i=0; $i < count($titulos) ; $i++) { 
    $letracolumn = $i;
    $objPHPExcel->getActiveSheet()->setCellValue($letras[$i].$c, $titulos[$i]);
	}
	
	$objPHPExcel->getActiveSheet()->getStyle("A$c:".$letras[$letracolumn]."$c")->applyFromArray($estiloTituloColumnas);
		$c = $c+1;
 
	$dtsa = $oNotacredito->filtroreporte_agru('','',fecha_mysql($_GET['fec1']),fecha_mysql($_GET['fec2']),$_GET['agrufec'],$_GET['agruemp'],$_GET['agrucli']);
	$num_rows= mysqli_num_rows($dtsa);

	if($num_rows>0){
	
	while($dta = mysqli_fetch_array($dtsa))
	{ 					//echo $_GET['ordpro'];
		$dts1 = $oNotacredito->filtroreporte(fecha_mysql($dta['fecha']),$_GET['agrufec'],$_GET['agruemp'],$_GET['agrucli'],
		$dta['emp_id'],$dta['cli_id'],fecha_mysql($_GET['fec1']),fecha_mysql($_GET['fec2']));
		$num_rowsf= mysqli_num_rows($dts1);
	while($dt1 = mysqli_fetch_array($dts1))
	{
		
			$mone_id = $dt1['tb_moneda_id'];	
			
			if($mone_id==1){
					$lbl_mone = 'S/ ';
			}else if($mone_id==2){
					$lbl_mone = 'US$ ';
			}
									
	$objPHPExcel->getActiveSheet(0)
    ->setCellValue('A'.$c, mostrarFecha($dt1['tb_notacredito_fec']))
    ->setCellValue('B'.$c, $dt1['tb_empresa_nomcom'])    
    ->setCellValue('C'.$c, $dt1['tb_notacredito_numdoc'])
    ->setCellValue('D'.$c, $dt1['tb_venta_numdoc'])
    ->setCellValue('E'.$c, $dt1['tb_cliente_doc'])
    ->setCellValue('F'.$c, $dt1['tb_cliente_nom'])
    ->setCellValue('G'.$c, $lbl_mone)   
    ->setCellValue('H'.$c, formato_money($dt1['tb_notacredito_tot']));
	
	$objPHPExcel->getActiveSheet()->getStyle("D$c:D$c")->applyFromArray($estiloTitulos);
    $objPHPExcel->getActiveSheet(0)->getStyle("H$c:H$c")->getNumberFormat()->setFormatCode('0.00');
    $c++;
		
		
	}
	
		if($_GET['agrufec']=='1'){
			$lbl = 'NOTAS DE CREDITO POR FECHA : ';
			$fe = mostrarFecha($dta['fecha']);
		}else if($_GET['agruemp']=='1'){
			$lbl = 'NOTAS DE CREDITO POR EMPRESA : ';
			$fe = $dta['emp'];
		}else if($_GET['agrucli']=='1'){
			$lbl = 'NOTAS DE CREDITO POR CLIENTE : ';
			$fe = $dta['cli'];
		}
	
	$objPHPExcel->getActiveSheet(0)
    ->setCellValue('A'.$c, '')
    ->setCellValue('B'.$c, '')    
    ->setCellValue('C'.$c, '')
    ->setCellValue('D'.$c, '') 
    ->setCellValue('E'.$c, '')
    ->setCellValue('F'.$c, $lbl.' '.$fe)
    ->setCellValue('G'.$c, '')
    ->setCellValue('H'.$c, 'S/ '.formato_money($dta['soles']).' - '. 'US$ '.formato_money($dta['dolares']));
	$objPHPExcel->getActiveSheet()->getStyle("A$c:H$c")->applyFromArray($estiloTotales);
	$objPHPExcel->getActiveSheet(0)->getStyle("H$c:H$c")->getNumberFormat()->setFormatCode('0.00');
	$c = $c +1;
	//$objPHPExcel->getActiveSheet()->getStyle("A$c:G$c")->applyFromArray($estiloTituloColumnas);
	
	}
	mysqli_free_result($dtsa);
	
	}else{
		$objPHPExcel->getActiveSheet()->setCellValue("A$c", 'SIN DATOS');
	}
		
	
	
	
	
for($z = 'A'; $z <= 'L'; $z++){
  $objPHPExcel->getActiveSheet()->getColumnDimension($z)->setAutoSize(TRUE);
}
	
	$objPHPExcel->getActiveSheet()->setTitle('NOTAS DE CREDITO POR FECHA');
	
	$nombre_archivo="REPORTE DE NOTAS DE CREDITO POR FECHA ".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;
?>