<?php
// require 'PhpSpreadsheet/vendor/autoload.php';
// require 'PhpSpreadsheet/src/PhpSpreadsheet/IOFactory.php';
// require 'PhpSpreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php';

$page              = escapeString($_GET['page']) ;
$search_name       = escapeString($_GET['search_name']) ;
$search_idno       = escapeString($_GET['search_idno']) ;
$search_department = ($_GET['search_department']) ;
$search_country    = escapeString($_GET['search_country']) ;
$search_branch     = escapeString($_GET['search_branch']) ;
$search_resigned   = escapeString($_GET['search_resigned']) ;
$sort_by           = escapeString($_GET['sort_by']) ;
$sort_by           = ( $sort_by != '' ? $sort_by : 'created_at' ) ;
$sort_by_type      = escapeString($_GET['sort_by_type']) ;
$export            = escapeString($_GET['export']) ;
$export            = ( $export == 'yes' ? 'yes' : 'no' ) ;
$search_mobile     = escapeString($_GET['search_mobile']) ;
$search_mail       = escapeString($_GET['search_mail']) ;
$search_tier       = escapeString($_GET['search_tier']) ;

$date_time         = escapeString($_GET['date_time']) ;
$date_current      = TODAYDAY ;
$date_time         = ( $date_time != '' ? $date_time : date('Y-m', strtotime($date_current)) ) ;
$day_of_month      = date("t", strtotime($date_time)) ;
$start_day         = 1;

$search_day_from   = escapeString($_GET['search_day_from']) ;
$search_day_to     = escapeString($_GET['search_day_to']) ;

// pagination
if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)
$start_from         = ($product_page - 1) * LIMIT ; //end next and prev page

$search_query       = '' ;

// add permission
/*
if ( $_SESSION['system_permission'] != 'admin' ){
    if ( permissionCheck($row_user, 'local-only') && permissionCheck($row_user, 'foreign-only') ){
        // do nothing
    }elseif ( permissionCheck($row_user, 'local-only') ){
        $search_query .= " AND a.country_id  = '1'" ;
    }else{
        $search_query .= " AND a.country_id != '1'" ;
    }
}
*/
if ( $search_name   != '' ){
    $search_query   .= " AND ( a.staff_name LIKE '%".$search_name."%' )" ;
}

if ( $search_idno   != '' ){
    $search_query   .= " AND ( a.staff_idno LIKE '%".$search_idno."%' )" ;
}

if( $search_mobile  != ''){
    $search_query   .= " AND a.staff_mobileno LIKE '%".$search_mobile."%'" ;
}

if( $search_mail    != ''){
    $search_query   .= " AND a.staff_email LIKE '%".$search_mail."%'" ;
}

if( $search_tier    != ''){
    $search_query   .= " AND a.staff_tier LIKE '%".$search_tier."%'" ;
}

$check_department    = false ;

if ( $search_department != '' ){
    $check_department = true ;
    if(arrayCheck($search_department)){
        $search_query .= " AND b.department_id IN (".implode(',',$search_department).")" ;
        foreach($search_department as $k => $v ){
            $search_department_url .= '&search_department%5B'.$k.'%5D='.$v;
        }
    }    
}

if ( $search_country != '' ){
    if ( $search_country == 'local' ){
        $search_query .= " AND a.country_id = '1'" ;
    }else{
        $search_query .= " AND a.country_id != '1'" ;
    }
}

if ( $search_branch != '' ){
    $search_query   .= " AND a.branch_id = '".$search_branch."'" ;
}

if ( $search_resigned != '' ){
    $search_query   .= " AND ( a.staff_date_resigned != '0000-00-00' and  a.staff_date_resigned IS NOT NULL )" ;
}else{
   $search_query    .= " AND ( a.staff_date_resigned IS NULL OR a.staff_date_resigned = '0000-00-00' OR a.staff_date_resigned >= '".$date_time."' )" ;
}

if ( $search_day_from ){
    $start_day       = $search_day_from;
}

if ( $search_day_to ){
    $day_of_month    = $search_day_to;
}

$get_user_tier       = userTierQuery( $row_user ) ;
// $search_query       .= ( $get_user_tier['check'] ? " AND a.staff_tier IN ( ".implode(', ', $get_user_tier['tiers'])." )" : '' ) ;
 

// page query
$mysqli_query = "SELECT a.staff_id, a.staff_name, a.staff_idno, a.staff_image, a.job_position_id, a.group_id, a.staff_tier, GROUP_CONCAT( DISTINCT CONCAT(c.department_desc) ) as department_desc FROM staff a
                 LEFT JOIN staff_department b ON (a.staff_id = b.staff_id)
                 LEFT JOIN setting_department_translation c ON (b.department_id = c.department_id)
                 WHERE b.deleted_at IS NULL AND c.lang = 'en' AND " ;

$mysqli_query = $mysqli_query . " a.deleted_at IS NULL  " . $search_query . $user_branch_permission_sql_a . " GROUP BY a.staff_id" ;
$mysqli_page  = $mysqli->query( $mysqli_query."  ORDER BY (" . $sort_by . ' * 1) ' . $sort_by_type . ', ' . $sort_by . ' ' . $sort_by_type . ( $export == 'no' ? " LIMIT $start_from, " . LIMIT : "" ) ) ;

// set search url
$search_url = 'page='.$page.'&page_mode='.$_GET['page_mode'].'&edit='.$_GET['edit'].'&date_time='.$_GET['date_time'].'&search_name='.$_GET['search_name'].'&search_idno='.$_GET['search_idno'].'&search_country='.$_GET['search_country'].'&search_branch='.$_GET['search_branch'].'&sort_by='.$_GET['sort_by'].'&sort_by_type='.$_GET['sort_by_type'].'&search_resigned='.$_GET['search_resigned'].$search_department_url."&search_day_to=".$search_day_to."&search_day_from=".$search_day_from ;

// load pagination
if ( $export == 'no' ){
    $page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;
}

// get all staff
$attendances = [] ;
$staff_list  = [] ; 

if( $mysqli_page->num_rows > 0 ){

    // loop all position
    $array_position  = [] ;
    $mysqli_position = $mysqli->query( "SELECT a.job_position_id, b.job_position_desc FROM setting_job_position a
                                        LEFT JOIN setting_job_position_translation b ON ( a.job_position_id = b.job_position_id )
                                        WHERE a.deleted_at IS NULL AND b.lang = 'en'" ) ;

    if( $mysqli_position->num_rows > 0 ){

        $row_position   = $mysqli_position->fetch_assoc() ;
        $array_position[$row_position['job_position_id']] = $row_position['job_position_desc'] ;
    }

    while( $row_staff = $mysqli_page->fetch_assoc() ){

        $staff_id = $row_staff['staff_id'] ;
        $attendances[$staff_id]['staff_id']           = $staff_id ;
        $attendances[$staff_id]['staff_idno']         = $row_staff['staff_idno'] ;
        $attendances[$staff_id]['staff_name']         = $row_staff['staff_name'] ;
        $attendances[$staff_id]['staff_positionname'] = $array_position[$row_staff['job_position_id']] ;
        $attendances[$staff_id]['staff_department']   = $row_staff['department_desc'] ;
        $attendances[$staff_id]['staff_image']        = PATH . ( $row_staff['staff_image'] != '' ? 'uploads/Staff/'.$row_staff['staff_image'] : 'images/NoProduct.jpg' ) ;
        $attendances[$staff_id]['group_id']           = $row_staff['group_id'] ;
        $attendances[$staff_id]['staff_tier']         = $row_staff['staff_tier'] ;
        $staff_list[] = $staff_id ;
    }
}

$array_health  = [] ;
if ( count($staff_list) > 0 ){
    $mysqli_health = $mysqli->query( "SELECT staff_id, degree, request_date FROM staff_health 
                                      WHERE deleted_at IS NULL AND request_date LIKE '".$date_time."%' AND staff_id IN (".implode(',', $staff_list).")" ) ;

    if( $mysqli_health->num_rows > 0 ){
        while ( $row_health = $mysqli_health->fetch_assoc() ){
            $array_health[$row_health['staff_id']][date('Y-m-d', strtotime($row_health['request_date']))] = $row_health['degree'] ;
        }
    }
}

// get all selected month attendances
$get_attendances = $mysqli->query("SELECT staff_id, record_from, code, abnormal, latitude, longitude, created_at, updated_at FROM staff_attendance
                                   WHERE deleted_at IS NULL AND staff_id IN (".implode(',', $staff_list).") AND created_at LIKE '".$date_time."-%'
                                   ORDER BY created_at ASC") ;

if ( $get_attendances->num_rows > 0 ){
    while ( $v = $get_attendances->fetch_assoc() ){
        $created_at = $v['created_at'] ;
        $date_group = date('Y-m-d', strtotime($created_at)) ;
        $attendances[$v['staff_id']]['group'][$date_group][] = [
            'abnormal'      => $v['abnormal'],
            'latitude'      => $v['latitude'],
            'longitude'     => $v['longitude'],
            'record_from'   => $v['record_from'],
            'code'          => $v['code'],
            'created_at'    => $created_at,
            'updated_at'    => $v['updated_at']
        ] ;
    }
}

$get_department =  $mysqli->query("SELECT a.staff_id, b.department_desc FROM staff_department a
                                   LEFT JOIN setting_department_translation b on ( a.department_id = b.department_id )
                                   WHERE b.lang = 'en' AND a.staff_id IN (".implode(',', $staff_list).") ");

if ( $get_department->num_rows > 0 ){
    while ( $v = $get_department->fetch_assoc() ){

        $attendances[$v['staff_id']]['department'][] = $v['department_desc'];
    }
}

// export excel
if ( $export == 'yes' ){
    include 'PhpExcel/PHPExcel.php' ;
    
    // get all attendance list
    $array_list     = [] ;
    $get_attendaces = $mysqli->query("SELECT list_id, list_work_day, list_ot_day, list_late, list_amt, list_early_out, list_time_off, list_type_remark, staff_id, list_date, list_work_day, list_ot_day, list_allow_food, list_food, list_attendance_remark FROM staff_attendance_list
                              WHERE deleted_at IS NULL AND list_date LIKE '".$date_time."%'") ;

    if ( $get_attendaces->num_rows > 0 ){
        while ( $row_list = $get_attendaces->fetch_assoc() ){

            $array_list[$row_list['staff_id']][$row_list['list_date']] = $row_list ;
        }
    }

    // set excel content
    $page_filename = 'Attenndance-'.date( 'Ymd', time() ) ;
    $objPHPExcel   = new PHPExcel() ;
    $objPHPExcel->getProperties()
                ->setCreator(COMPANY)
                ->setTitle(COMPANY)
                ->setSubject(COMPANY)
                ->setDescription(COMPANY)
                ->setKeywords(COMPANY)
                ->setCategory(COMPANY) ;

    $objPHPExcel->getActiveSheet()->setTitle( $page_filename ) ;
    $objPHPExcel->setActiveSheetIndex(0);
    $objWriter = PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel5' ) ;

    $styleArrayTitle = array(
        'font'  => array(
            'bold'  => true ,
            'color' => array( 'rgb' => '000000' ) ,
            'size'  => 15
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );

    $styleArrayDay = array(
        'font'  => array(
            'bold'  => true
        ),
        'borders' => array(
            'outline' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
            )
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    ) ;

    $styleArrayBg1 = array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'AFABAB')
        ),
        'borders' => array(
            'outline' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
            )
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    ) ;

    $styleArrayBg2 = array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFDA65')
        ),
        'borders' => array(
            'outline' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
            )
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    ) ;

    $styleArray = array(
        'borders' => array(
            'outline' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
            )
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    ) ;

    $styleArrayLeft = array(
        'borders' => array(
            'outline' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
            )
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
        )
    ) ;

    $styleArray2 = array(
        'alignment' => array(
            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
        )
    ) ;

    $styleArrayRed = array(
    'font'  => array(
        'color' => array('rgb' => 'FF0000'),
    ));

    $count     = 1 ;
    $firstChar = 'A' ;
    // $lastChar  = 'AG' ;
    $lastChar = "A";

    for ($i= $start_day; $i <= ($day_of_month) ; $i++) { 
        $lastChar++;
    }

    // title name
    $objPHPExcel->getActiveSheet()->mergeCells( $firstChar.$count.':'.$lastChar.$count ) ;
    $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count.':'.$lastChar.$count )->applyFromArray( $styleArrayTitle ) ;
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar.$count, 'Attendance Clock Table' ) ;
    $count++ ;

    // $array_title = array( 'DD', 'DAY', 'MIN', 'AMT', 'OT', 'FOOD', 'CK', 'RK', 'HL' ) ;
    $array_title = array( 'DD', 'CK' ) ;

    // loop all attendance record
    if ( count($attendances) > 0 ){
        foreach ( $attendances as $k => $v ){

            $count++ ;
            $count++ ;

            $date_group = $v['group'] ;
            $staff_idno = ucwords($v['staff_idno']) ;

            $objPHPExcel->getActiveSheet()->getStyle($firstChar.$count)->applyFromArray( $styleArray ) ;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar.$count, 'US' ) ;
            $firstChar2 = $firstChar ;
            $firstChar2++ ;

            $mc1 = $firstChar2.$count;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar2.$count, 'ID: '.$staff_idno.'  Name: '.$v['staff_name'].'    Department: '.implode(",", $v['department'])) ;
            $mc2 = $lastChar.$count ;
            $objPHPExcel->getActiveSheet()->mergeCells($mc1.':'.$mc2) ;
            $objPHPExcel->getActiveSheet()->getStyle($mc1.':'.$mc2)->applyFromArray( $styleArrayLeft ) ;
            $count++ ;

            $count2 = $count ;



            for ($row = 6; $row <= 10; $row++) {
                $objPHPExcel->getActiveSheet()->getStyle('AG' . $row)->applyFromArray($styleArray);
                $objPHPExcel->getActiveSheet(0)->setCellValue('AG' . $row, '=SUM(B' . $row . ':AF' . $row . ')');
            }



            if( arrayCheck( $array_title ) ){
                foreach( $array_title as $key_title => $value_title ){
                    $objPHPExcel->getActiveSheet()->getStyle($firstChar.$count)->applyFromArray( $styleArray ) ;
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar.$count, $value_title ) ;
                    $firstChar2 = $firstChar ;
                    $firstChar2++ ;

                    $total_day  = 0 ;
                    $total_ot   = 0 ;
                    $total_min  = 0 ;
                    $total_amt  = 0 ;
                    $total_fb   = 0 ;

                    for( $i = $start_day ; $i <= $day_of_month ; $i++ ){
                        $objPHPExcel->getActiveSheet()->getStyle($firstChar2.$count)->applyFromArray( $styleArray ) ; 

                        // default parameter ;
                        $report_day       = 0 ;
                        $report_min       = '00:00:00' ;
                        $report_ot        = 0 ;
                        $report_amt       = 0 ;
                        $report_fb        = 0 ;
                        $list_late        = 0 ;
                        $list_early_out   = 0 ;
                        $report_wt        = '' ;
                        $list_type_remark = '' ;
                        $report_remark    = '' ;
                        $report_health    = '' ;
                        $report_allow_food = '' ;
                        $new_day          = $date_time.'-'.str_pad($i, 2, '0', STR_PAD_LEFT) ;
                        $apply_color      = [] ; 

                        // get attendance list report
                        $list_date        = $new_day ;
                        $row_report       = $array_list[$v['staff_id']][$list_date] ;

                        if ( $row_report != undefined && arrayCheck($row_report) ){

                            $report_day       = ( $row_report['list_work_day'] != '' ? $row_report['list_work_day'] : 0 ) ;
                            $report_ot        = ( $row_report['list_ot_day'] != '' ? $row_report['list_ot_day'] : 0 ) ;
                            $list_late        = ( $row_report['list_late'] != '00:00:00' ? $row_report['list_late'] : 0 ) ;
                            $list_early_out   = ( $row_report['list_early_out'] != '00:00:00' ? $row_report['list_early_out'] : 0 ) ;
                            $list_type_remark = $row_report['list_type_remark'] ;
                            $report_remark    = $row_report['list_attendance_remark'];
                            $report_health    = ( $array_health[$v['staff_id']][$list_date] != '' ? $array_health[$v['staff_id']][$list_date] : '-');

                            // $report_min       = ( $row_report['list_late'] != '00:00:00' ? substr($row_report['list_late'], 0, -3) : '00:00' ) ;
                            $report_amt       = ( $row_report['list_amt'] != '' ? $row_report['list_amt'] : 0 ) ;
                            $report_fb        = ( $row_report['list_allow_food'] == 'yes' ? $row_report['list_food'] : 0 ) ;
                            $report_allow_food = ( $row_report['list_allow_food'] == 'yes' ? '1' : '' ) ;

                            if ( $row_report['list_late'] != '00:00:00' ){
                                $report_min = commonAddTime($row_report['list_late'], '00:00:01') ;
                            }

                            if ( $row_report['list_early_out'] != '00:00:00' ){
                                $report_min = commonAddTime($row_report['list_early_out'], $report_min) ;
                            }

                            if ( $row_report['list_time_off'] != '00:00:00' && $row_report['list_time_off'] != '' ){
                                $report_min = commonAddTime($row_report['list_time_off'], $report_min) ;
                            }
                        }

                        $report_min   = substr($report_min, 0, -3) ;
                        
                        // 'DD', 'DAY', 'MIN', 'AMT', 'OT', 'CK', 'HL'
                        $content      = '' ;
                        $show_decimal = true ;

                        switch ( $value_title ){
                            case 'DD' :
                                $content       = $i ;
                                $show_decimal  = false ;
                            break ;

                            case 'DAY' :
                                $content       = $report_day ;
                                $total_day    += $report_day ;
                            break ;

                            case 'MIN' :
                                $content       = ( $report_min != '00:00' ? numberFormat(convertMinutes($report_min)) : '' ) ;
                                $total_min    += numberFormat(convertMinutes($report_min)) ;
                            break ;

                            case 'AMT' :
                                $content       = ( $report_amt > 0 ? $report_amt : '' ) ;
                                $total_amt    += $report_amt ;
                            break ;

                            case 'OT' :
                                $content       = ( $report_ot > 0 ? $report_ot : '' ) ;
                                $total_ot     += $report_ot ;
                            break ;

                            case 'FOOD' :
                                $content       = ( $report_allow_food ) ;
                                // $total_fb     += $report_fb ;
                            break ;

                            case 'CK' :
                                $boolean_abnormal = false ;
                                $show_decimal     = false ;
                                $array_group      = [] ;

                                if ( !empty($date_group[$new_day]) ){
                                    
                                    $get_group = $date_group[$new_day] ;

                                    foreach( $get_group as $k_group => $v_group ){
                                        $array_group[] = date('H:i', strtotime($v_group['created_at'])) ;

                                        if ( $v_group['abnormal'] == 'yes' ){
                                            $boolean_abnormal = true ;
                                        }
                                    }
                                }
                                
                                $style_red = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => 'FF0000')));
                                $style_yellow = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => 'FFFF00')));
                                $style_green = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => '00FF00')));
                                $style_blue = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => '0000FF')));
                                $style_orange = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => 'FF8000')));
                                $style_purple = array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => 'C000FF')));

                                $apply_text  = '' ;
                                $objRichText = new PHPExcel_RichText();

                                switch( $list_type_remark ){
                                    case 'OD' : // OFF DAY
                                        $apply_text  = 'OFF DAY' ;
                                        $apply_color = $style_green ;
                                    break ;

                                    case 'MC' : // SICK LEAVE
                                        $apply_text  = 'SICK LEAVE' ;
                                        $apply_color = $style_yellow; ;
                                    break ;

                                    case 'UL' : // UNPAID LEAVE
                                        $apply_text  = 'UNPAID LEAVE'; ;
                                        $apply_color = $style_red ;
                                    break ;

                                    case 'AL' : // ANNUAL LEAVE    
                                        $apply_text  = 'ANNUAL LEAVE' ;
                                        $apply_color = $style_blue ;
                                    break ;

                                    case 'AS' : // ABSENT       
                                        $apply_text  = 'ABSENT' ;
                                        $apply_color = $style_orange ;
                                    break ;

                                    case 'HL' : // HOLIDAY LIST       
                                        $apply_text  = 'HOLIDAY LIST' ;
                                       // $apply_color = $style_orange ;
                                    break ;

                                    case 'CL' : // COMPANY LEAVE       
                                        $apply_text  = 'COMPANY LEAVE' ;
                                       // $apply_color = $style_orange ;
                                    break ;

                                    case 'SP' : // SUSPEND LEAVE       
                                        $apply_text  = 'SUSPEND LEAVE' ;
                                       // $apply_color = $style_orange ;
                                    break ;

                                    default :
                                        if ( $boolean_abnormal ){              
                                            $apply_color = $style_purple ;
                                        }
                                }

                                $content = implode("\n", $array_group) . ( $apply_text != '' ? "\n".$apply_text : '' ) ;
                            break ;

                            case "RK":
                                $content       = $report_remark ;
                            break;

                            case "HL":
                                $content       = $report_health ;
                            break;
                        }

                        if ( $show_decimal ){

                            $objPHPExcel->setActiveSheetIndex(0)->getStyle( $firstChar2.$count )->getNumberFormat()->setFormatCode('#,##0.00') ;
                        }

                        ################################################################## add color ##################################################################
                        
                        if($value_title == 'CK'){
                            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar2.$count, $content ) ;
                            $objPHPExcel->getActiveSheet()->getStyle($firstChar2.$count)->getAlignment()->setWrapText(true);

                            if(ALLOWCOLOREXCEL == "YES" && arrayCheck($apply_color)){

                                $objPHPExcel->getActiveSheet()->getStyle($firstChar2.$count)->applyFromArray($apply_color);
                            }  
                        }else{

                            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar2.$count, $content ) ;
                        }

                        ################################################################## add color ##################################################################

                        $firstChar2++ ;

                        // if after day 31, add total
                        if ( $i == $search_day_to ){

                            $last_content = '' ;

                            switch ( $value_title ){
                                case 'DD' :
                                    $last_content = 'TOTAL' ;
                                break ;

                                case 'DAY' :
                                    $last_content = $total_day ;
                                break ;

                                case 'MIN' :
                                    $last_content = $total_min ;
                                break;

                                case 'AMT' :
                                    $last_content = $total_amt ;
                                break ;

                                case 'OT' :
                                    $last_content = $total_ot ;
                                break ;

                                case 'CK' :
                                break ;
                            }

                            $objPHPExcel->getActiveSheet()->getStyle($firstChar2.$count)->applyFromArray( $styleArray ) ;
                            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar2.$count, $last_content ) ;
                            $firstChar2++ ;
                        }
                    }
                    
                    $count++ ;
                }
            }
        }
    }

    // Submission from
    header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) ; 
    header( 'Content-Disposition: attachment;filename="'.$page_filename.'.xlsx"' ) ;
    header( 'Cache-Control: max-age=0' ) ;

    // save to pc
    $objWriter->save('php://output') ;
    header( "Refresh: 0" ) ;
    exit ;
}

?>