<?php    
    $tabla="resultados";
    
    $Laboratorio= isset($_REQUEST['p_Laboratorio']) ? $_REQUEST['p_Laboratorio'] : '';
    $Establecimiento= isset($_REQUEST['p_Establecimiento']) ? $_REQUEST['p_Establecimiento'] : '';
    $vdesde= isset($_REQUEST['p_Tomadesde']) ? $_REQUEST['p_Tomadesde'] : '';
    $vhasta= isset($_REQUEST['p_Tomahasta']) ? $_REQUEST['p_Tomahasta'] : '';
    $res_id= isset($_REQUEST['p_res_id']) ? $_REQUEST['p_res_id'] : '';

    

    
    $where ="";
    if ($Laboratorio <>'') {$where .= " and dil_laboratorio_id in ($Laboratorio) "; }
    if ($Establecimiento <>'') {$where .= " and dab_ltoma_id in ($Establecimiento) "; }
    if ($vdesde<>'') {$where .= " and daad_ftoma >= '$vdesde' "; }
    if ($vhasta<>'') {$where .= " and daad_ftoma <= '$vhasta' "; }    
    if ($res_id <>'') {$where .= " and id in ($res_id) "; }

    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $offset = ($page-1)*$rows;
    $result = array();
    
    include '../rdb/conn.php';
   $sql="	select ";
	$sql.=" count(*)";
	$sql.="	from";
	$sql.="	resultados r,productos p,matrices m ,conjuntos_analiticos ca,centros_regionales cr,laboratorios l";
	$sql.="	where ";
	$sql.="	r.dil_producto_id=p.id and ";
	$sql.="	r.dil_matriz_id=m.id and";
	$sql.="	r.dil_laboratorio_id = l.id and";
	$sql.="	r.dil_conjunto_id=ca.id and";
	$sql.="	r.daad_centro_regional_id=cr.id";
        $sql.= $where ;
        
    $rs = mysqli_query( $conn,$sql);
    $row = mysqli_fetch_row($rs);
    $result["total"] = $row[0];
    
    if ($result["total"] === 0) {exit;}  
    
    $sql="	select ";
	$sql.="		r.id";
	$sql.="		,fresultado";
	$sql.="		,daad_ftoma";
	$sql.="		,daad_directiva_muestreo";
	$sql.="		,cr.nombre daad_centro_regional";
	$sql.="		,daad_acta";
	$sql.="		,p.nombre producto";
	$sql.="		,m.nombre matriz";
	$sql.="		,ca.nombre conjunto";
	$sql.="		,daab_ltoma_snof";
	$sql.="		,dab_ltoma_razon_social";
	$sql.="		,dac_renspa";
	$sql.="	from";
	$sql.="	resultados r,productos p,matrices m ,conjuntos_analiticos ca,centros_regionales cr,laboratorios l";
	$sql.="	where ";
	$sql.="	r.dil_producto_id=p.id and ";
	$sql.="	r.dil_matriz_id=m.id and";
	$sql.="	r.dil_laboratorio_id = l.id and";
	$sql.="	r.dil_conjunto_id=ca.id and";
	$sql.="	r.daad_centro_regional_id=cr.id";
        $sql.= $where . " order by id ";

    
    $rs = mysqli_query( $conn,$sql);

    $items = array();
	error_reporting(E_ALL);
	ini_set('display_errors', TRUE);
	ini_set('display_startup_errors', TRUE);
	define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
	date_default_timezone_set('Europe/London');
	/** Include PHPExcel */
	require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
	$objPHPExcel = new PHPExcel();
	// Set document properties
	$objPHPExcel->getProperties()->setCreator("SENASA")
								 ->setLastModifiedBy("SENASA")
								 ->setTitle("Resultados")
								 ->setSubject("Reporte de Resultados")
								 ->setDescription("Reporte de resultado")
								 ->setKeywords("Reporte de resultado")
								 ->setCategory("Reporte de resultado");


	// Create a first sheet
	$objPHPExcel->setActiveSheetIndex(0);
    


	
	$objPHPExcel->getActiveSheet()->setCellValue('A1', "ID");
	$objPHPExcel->getActiveSheet()->setCellValue('B1', "F.RESULTADO");
	$objPHPExcel->getActiveSheet()->setCellValue('C1', "F.TOMA ");
	$objPHPExcel->getActiveSheet()->setCellValue('D1', "DIRECTIVA");
	$objPHPExcel->getActiveSheet()->setCellValue('E1', "C.REGIONAL");
	$objPHPExcel->getActiveSheet()->setCellValue('F1', "ACTA");
	$objPHPExcel->getActiveSheet()->setCellValue('G1', "PRODUCTO");	
	$objPHPExcel->getActiveSheet()->setCellValue('H1', "MATRIZ");	
	$objPHPExcel->getActiveSheet()->setCellValue('I1', "CONJUNTO");	
	$objPHPExcel->getActiveSheet()->setCellValue('J1', "SNOF");	
	$objPHPExcel->getActiveSheet()->setCellValue('K1', "ESTABLECIMIENTO");	
	$objPHPExcel->getActiveSheet()->setCellValue('L1', "RNSPA");	

	$objPHPExcel->getActiveSheet()->getStyle("A1:L1")->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FFC0C0C0');
	$objPHPExcel->getActiveSheet()->getStyle("A1:L1")->getFont()->setBold(true);
	
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(30);
	$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(40);
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(20);

        


	// Freeze panes
	$objPHPExcel->getActiveSheet()->freezePane('A2');


	// Rows to repeat at top
	$objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);
    $i=2;	
    
	$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&CResultados');
	$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('Pagina &P / &N');
	$objPHPExcel->getActiveSheet()->getHeaderFooter()->setEvenFooter('Pagina &P / &N');  
	
	
	$styleArray = array(
	    'font'  => array(
	        'bold'  => true,
	        'color' => array('rgb' => '000000'),
	        'size'  => 15,
	        'name'  => 'Verdana'
	    ));

    $i=1;			
    while($row = mysqli_fetch_object($rs)){
    		$i++;
	$objPHPExcel->getActiveSheet()->setCellValue('A'. $i, $row->id);
	$objPHPExcel->getActiveSheet()->setCellValue('B'. $i, $row->fresultado);
	$objPHPExcel->getActiveSheet()->setCellValue('C'. $i, $row->daad_ftoma);
	$objPHPExcel->getActiveSheet()->setCellValue('D'. $i, $row->daad_directiva_muestreo);
	$objPHPExcel->getActiveSheet()->setCellValue('E'. $i, $row->daad_centro_regional);
	$objPHPExcel->getActiveSheet()->setCellValue('F'. $i, $row->daad_acta);
	$objPHPExcel->getActiveSheet()->setCellValue('G'. $i, $row->producto);	
	$objPHPExcel->getActiveSheet()->setCellValue('H'. $i, $row->matriz);	
	$objPHPExcel->getActiveSheet()->setCellValue('I'. $i, $row->conjunto);	
	$objPHPExcel->getActiveSheet()->setCellValue('J'. $i, $row->daab_ltoma_snof);	
	$objPHPExcel->getActiveSheet()->setCellValue('K'. $i, $row->dab_ltoma_razon_social);	
	$objPHPExcel->getActiveSheet()->setCellValue('L'. $i, $row->dac_renspa);	
	
        $objPHPExcel->getActiveSheet()->getStyle("A".$i.":L".$i)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );			
    }

//	
	$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);	
	$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
	$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToHeight(0);
       
	$objPHPExcel->getActiveSheet()->getStyle('A1:L'.$objPHPExcel->getActiveSheet()->getHighestRow())->getAlignment()->setWrapText(true); 
	    
        $objPHPExcel->setActiveSheetIndex(0);
	
    
    // Save Excel 2007 file
    $callStartTime = microtime(true);

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Resultados.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); 
    

    
?>