<?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 */

$keyword=$_REQUEST['keyword'];
$cl=new MY_HTML();

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Tokyo');

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);}

// === Init Vars
$month=$_REQUEST['month'];
$period=$_REQUEST['period'];
$totdays=cal_days_in_month(CAL_GREGORIAN,$month,$period);

$idemployee=$_REQUEST['idemployee'];
$idsubunit=$_REQUEST['idsubunit'];
            
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// My data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','Employee Absence Reporting')
            ->setCellValue('A2','Human Capital '.$_SESSION['employeename'])
            ->setCellValue('A3','Date Reporting : '.date("d/m/Y H:i:s"))
            ->setCellValue('A4','Periode : '.$cl->FN_GETMONTHNAME($month)." $period");

if($idsubunit)
{
    $subunitname=$cl->fn_get_fieldvalue("subunitname","select subunitname from ".DB_APORTIL.".tbl_employee_subunit where id='$idsubunit'");
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A5','Sub Unit : '.$subunitname);
}
            

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


// Print Header
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A7','No')
            ->setCellValue('B7','Employee Name')
            ->setCellValue('C7','Nik')
            ->setCellValue('D7','Position')
            ->setCellValue('E7','Dates')
            ->setCellValue('F7','Clock In')
            ->setCellValue('G7','Clock Out')
            ->setCellValue('H7','Reason')
            ;

// === Merge Cells

$objPHPExcel->getActiveSheet()->getStyle('A7:H7')->getFont()->getColor()->setRGB('ffffff');
// Worksheet Name
$objPHPExcel->getActiveSheet()->setTitle('Absence_'.date("Ym"));

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

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

$swhere="";//
if($idemployee)
{
    $swhere="a.idemployee='$idemployee' and ";
}
if($month)
{
    $swhere.="a.months='$month' and ";
}
if($period)
{
    $swhere.="a.years='$period' and ";
}

$qry="select a.idemployee,e.fullname,e.positionname,a.nik,a.date,a.clockin,a.clockout,a.reason 
      from ".DB_APORTIL.".tbl_employee e inner join ".DB_APORTIL.".tbl_hcm_absence a on e.id=a.idemployee
      where $swhere a.isdeleted=0 and a.idstatus in (0,3,4) 
      order by a.date asc";
      
$rsrpt=$cl->selectquery($qry);
if($rsrpt)
{
    $i=8;$j=1;
    while($rw=$cl->data_row_array($rsrpt))
    {
        $colors="000000";
        if((int)date("Hi",strtotime($rw['clockin']))>900)
        {
            $colors="FF0000";
        }
        $phpColor = new PHPExcel_Style_Color();
        $phpColor->setRGB($colors);
        $clockout=date("H:i",strtotime($rw['clockout']));
        if(!$rw['clockout']){$clockout="";}
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i,$j)
                                            ->setCellValue('B'.$i,$rw["fullname"])
                                            ->setCellValue('C'.$i,$rw["nik"])
                                            ->setCellValue('D'.$i,$rw["positionname"])
                                            ->setCellValue('E'.$i,date("d/m/Y",strtotime($rw['date'])))
                                            ->setCellValue('F'.$i,date("H:i",strtotime($rw['clockin'])))
                                            ->setCellValue('G'.$i,$clockout)
                                            ->setCellValue('H'.$i,$rw['reason'])
                                            ;
        $objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getFont()->setColor($phpColor);
        $objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getFont()->setColor($phpColor);
        $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="HCM_ABSENCE.xls"');
header('Cache-Control: max-age=0');

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