<?php
class reporte_model2{

    private $db;
    private $consulta;
    private $listado;

    public function __construct(){
        $this->db=database::conexion();
        $this->listado=array();
    }

    public function get_reporte_excel($productos){
        error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_WARNING);
        require_once("./model/phpExcel/PHPExcel.php");
        require_once("./model/gestion_model.php");
        date_default_timezone_set("America/Lima");
        $fecha_hoy = date("Y-m-d");

        $this->listado = "";

        /* while($filas=$consulta->fetch_assoc()){
            $this->listado[]=$filas;
        } */
        date_default_timezone_set("America/Lima");
        $Fecha_Reporte = date('dmY');
        $Nombre = "./reportes/reporte_almacen".$Fecha_Reporte.".xls";

        PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("BIO SAC")
            ->setLastModifiedBy("BIO SAC")
            ->setTitle("REPORTE")
            ->setSubject("REPORTE")
            ->setDescription("REPORTE")
            ->setKeywords("REPORTE BIO SAC")
            ->setCategory("REPORTES BIO SAC");

        $objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri')->setSize(11);


        $objPHPExcel->getActiveSheet()->setCellValue('A1', 'PRODUCTO');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', 'SKU');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', 'DISPONIBLE');
        $objPHPExcel->getActiveSheet()->setCellValue('D1', 'MALOGRADO');
        $objPHPExcel->getActiveSheet()->setCellValue('E1', 'MERMA');

        $columnas = array('A','B','C','D','E');

        foreach ( $columnas as $dato ):
            $objPHPExcel->getActiveSheet()->getstyle($dato.'1')->getFont()->getcolor()->setRGB('FFFFF');
            $objPHPExcel->getActiveSheet()->getstyle($dato.'1')->applyFromArray(
                array(
                    'fill' => array(
                        'type' => PHPExcel_Style_Fill::FILL_SOLID,
                        'color' => array('rgb' => '666699')
                    )
                )
            );
            $objPHPExcel->getActiveSheet()->getColumnDimension($dato)->setAutoSize(true);
        endforeach;

        $Fila = 2;


        foreach ( $productos as $producto ){
                

            $objPHPExcel->getActiveSheet()->setCellValueExplicit('A'.$Fila, $producto['nombre_producto']);
            if($producto['bio'] == 1){
                $objPHPExcel->getActiveSheet()->setCellValueExplicit('B'.$Fila, $producto['sku']);
            }else{
                $objPHPExcel->getActiveSheet()->setCellValueExplicit('B'.$Fila, $producto['sku_cliente']);
            }
            
            $objPHPExcel->getActiveSheet()->setCellValueExplicit('C'.$Fila, $producto['disponible']);
            $objPHPExcel->getActiveSheet()->setCellValueExplicit('D'.$Fila, $producto['malogrado']);
            $objPHPExcel->getActiveSheet()->setCellValueExplicit('E'.$Fila, $producto['merma']);

            $Fila++; 
        }
        $objPHPExcel->getActiveSheet()->getStyle('V2:V'.$Fila)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
        $objPHPExcel->getActiveSheet()->getStyle('X2:X'.$Fila)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
        $objPHPExcel->getActiveSheet()->setTitle('REPORTE_BIO');
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save($Nombre);
        return $Nombre;
    }

    public function DescargarArchivo($productos){
        $fichero = $this->get_reporte_excel($productos);
        /* $basefichero = basename($fichero);
        header("Content-Type: application/octet-stream");
        header("Content-Length: ".filesize($fichero));
        header("Content-Disposition:attachment;filename=".$basefichero.""); */
        $arch = $fichero;     
        return $arch;
    }
}