<?php
/**
 * PHPExcel
 *
 * Copyright (C) 2006 - 2013 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    1.7.9, 2013-06-02
 */

/** Error reporting */
//require_once('libconfig.php');
//require_once('libhtml.php');
//require_once('libmsginfo.php');
//require_once('libdefine.php');
//require_once('libdbconn.php');

$keyword=$_REQUEST['keyword'];
$cl=new MY_HTML();
//if($idfakultas){$nmfakultas=$cl->fn_get_fieldvalue("namafakultas","select namafakultas from ".DB_UNDIAN.".tbl_fakultas where id='$idfakultas'");}


error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

/** Include PHPExcel */

require_once 'Classes/PHPExcel.php';
if(preg_match("/^windows/i",strtolower(php_uname()))){PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);}

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// My data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','TELKOMCEL')
            ->setCellValue('A2','4G ICCID')
            ->setCellValue('A3','Reported By : '.$_SESSION['nik']);

//  set format cell
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);


// Print Header
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A6','No')
            ->setCellValue('B6','Status')
            ->setCellValue('C6','ICCI Number')
            ->setCellValue('D6','IMSI Number')
            ->setCellValue('E6','EKI')
            ->setCellValue('F6','KeyInd')
            ->setCellValue('G6','MSISDN')
            ->setCellValue('H6','Register Date')
            ->setCellValue('I6','Finished')
            ;

// === Merge Cells
//$objPHPExcel->getActiveSheet()->mergeCells('G5:H5');
//$objPHPExcel->getActiveSheet()->mergeCells('I5:J5');

$objPHPExcel->getActiveSheet()->getStyle('A6:I6')->getFont()->getColor()->setRGB('ffffff');
// Worksheet Name
$objPHPExcel->getActiveSheet()->setTitle('4GICCID_'.date("Ym"));

// === Set Background Cell
$objPHPExcel->cellColor("A6:I6","938e8e");

// === set Column Width A
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(6);
// === Set auto COlumn width
foreach(range('B','I') as $columnID)
{
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}

$swhere="";
$qry="select p.id,p.isprocess,p.iccid,p.imsi,p.eki,p.keyind,p.msisdn,p.regdate,p.status,p.regdate,p.finisheddate
      from ".DB_APORTIL.".tbl_marsal_4giccid i left join ".DB_APORTIL.".tbl_marsal_4gprovisioning p on (i.id=p.idiccid or i.id<>p.idiccid)
      where (i.imsi like '%$keyword%' or i.iccid like '%$keyword%') $swhere and p.isdeleted=0 and i.isdeleted=0
      order by i.id asc";
$qry="select i.id,p.isprocess,i.iccid,i.imsi,i.eki,p.keyind,p.msisdn,p.regdate,p.status,p.finisheddate
      from ".DB_APORTIL.".tbl_marsal_4giccid i left join ".DB_APORTIL.".tbl_marsal_4gprovisioning p on (i.id=p.idiccid or p.idiccid is null)
      where (i.imsi like '%$keyword%' or i.iccid like '%$keyword%') and (p.isdeleted=0 or p.isdeleted is null) and i.isdeleted=0
      order by i.id asc";
                  
$rsrpt=$cl->selectquery($qry);
if($rsrpt)
{
    $i=7;$j=1;
    while($rw=$cl->data_row_array($rsrpt))
    {
        $registerdate="";
        if($rw["regdate"]){$registerdate=date("Y-m-d H:i:s",strtotime($rw["regdate"]));}
        $finisheddate="";
        if($rw["finisheddate"]){$finisheddate=date("Y-m-d H:i:s",strtotime($rw["finisheddate"]));}
        $statusdescription="";
        if(!is_null($rw["status"]))
        {
            $statusdescription=$cl->fn_get_fieldvalue("description","select description from ".DB_APORTIL.".tbl_marsal_4gprovisioningstatus where status='".$rw["status"]."'");    
        }
        
                        
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i,$j)
                                            ->setCellValue('B'.$i,$statusdescription)
                                            ->setCellValue('C'.$i,$rw["iccid"])
                                            ->setCellValue('D'.$i,$rw["imsi"])
                                            ->setCellValue('E'.$i,$rw["eki"])
                                            ->setCellValue('F'.$i,$rw["keyind"])
                                            ->setCellValue('G'.$i,$rw["msisdn"])
                                            ->setCellValue('H'.$i,$registerdate)
                                            ->setCellValue('I'.$i,$finisheddate)
                                            ;
        $i+=1;$j+=1;
    }
}
$cl->clearrecordset($rsrpt);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a web browser (Excel2007)
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="4GICCID.xls"');
header('Cache-Control: max-age=0');

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