<?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 ("../quiebrestock_cst/cQuiebrestock_cst.php");
$oQuiebrestock_cst = new cQuiebrestock_cst();
require_once("../almacen/cAlmacen.php");
$oAlmacen = new cAlmacen();
$fec = date('Y-m-d');
require_once ("../formatos/formato.php");
require_once ("../formatos/fechas.php");

$dts1=$oAlmacen->mostrarAlmCajEmp($_GET['punven_id']);
			$dt1 = mysqli_fetch_array($dts1);
 			$punven_id=$dt1['tb_puntoventa_id'];
 			$piso_id=$dt1['tb_piso_id'];
 			$alm_id = $dt1['tb_almacen_id'];
 		
			mysqli_free_result($dts1);



// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("https://infosisperu.com")
->setLastModifiedBy("https://infosisperu.com")
->setTitle("VENTAS GENERAL")
->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
    )
  );
  
  $titulosColumnascompras = array(
  'NOMBRE',
  'EMPAQUE',
  'LABORATORIO',
  'CANTIDAD VENDIDA',
  'STOCK ACTUAL'
  );
  
  
  $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", 'QUIEBRE STOCK');
	// $objPHPExcel->getActiveSheet()->setCellValue("B$c", 'PRECIOS');
	$objPHPExcel->getActiveSheet()->getStyle("A$c:E$c")->applyFromArray($estiloTitulos);
	
	$c = $c+1;
  
  $letracolumn = 0;
  

  for ($i=0; $i < count($titulosColumnascompras) ; $i++) { 
    $letracolumn = $i;
    $objPHPExcel->getActiveSheet()->setCellValue($letras[$i].$c, $titulosColumnascompras[$i]);
  }
  
		$objPHPExcel->getActiveSheet()->getStyle("A$c:".$letras[$letracolumn]."$c")->applyFromArray($estiloTituloColumnas);
		$c = $c+1;
 
		$dts1=$oQuiebrestock_cst->mostrar_Quiebrestockporventas(fecha_mysql($_GET['fec1']),fecha_mysql($_GET['fec2']),$alm_id,$_GET['punven_id']);
		$num_rows= mysqli_num_rows($dts1);
		
		
		
		while($dt1 = mysqli_fetch_array($dts1)){
		
		     $pronom =  $dt1['tb_producto_nom'];
		    $canven =  $dt1['cantidadvendida'];
		    $marnom =  $dt1['tb_marca_nom'];
		    $empnom =  $dt1['tb_empaque_nom'];
		    $stock =  $dt1['stock'];
		    
		if($stock<=0){
             
	$objPHPExcel->getActiveSheet(0)
    ->setCellValue('A'.$c, $pronom)
    ->setCellValue('B'.$c, $empnom)
    ->setCellValue('C'.$c, $marnom)    
    ->setCellValue('D'.$c, formato_money($canven))
    ->setCellValue('E'.$c, $stock);
	 $c++;
		}
	
    $objPHPExcel->getActiveSheet(0)->getStyle("D$c:E$c")->getNumberFormat()->setFormatCode('0.00');
   
		
		
		
		}
		
	mysqli_free_result($dts1);
	/*
	$objPHPExcel->getActiveSheet(0)
    ->setCellValue('A'.$c, '')
    ->setCellValue('B'.$c, '') 
    ->setCellValue('C'.$c, '') 
    ->setCellValue('D'.$c, formato_money($totprecos))
    ->setCellValue('E'.$c, formato_money($totpreven));
	
	$objPHPExcel->getActiveSheet()->getStyle("A$c:E$c")->applyFromArray($estiloTotales);
	$objPHPExcel->getActiveSheet(0)->getStyle("D$c:E$c")->getNumberFormat()->setFormatCode('0.00');
	*/
	
for($z = 'A'; $z <= 'L'; $z++){
  $objPHPExcel->getActiveSheet()->getColumnDimension($z)->setAutoSize(TRUE);
}
	
	$objPHPExcel->getActiveSheet()->setTitle('QUIEBRE STOCK');
	
	$nombre_archivo="QUIEBRE STOCK ".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;
?>