<?php
$page              = escapeString($_GET['page']) ;
$search_name       = escapeString($_GET['search_name']) ;
$search_idno       = escapeString($_GET['search_idno']) ;
$search_branch     = escapeString($_GET['search_branch']) ;
$sort_by           = escapeString($_GET['sort_by']) ;
$sort_by           = ( $sort_by != '' ? $sort_by : 'a.created_at' ) ;
$sort_by_type      = escapeString($_GET['sort_by_type']) ;

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

$day_of_month      = date("t", strtotime($date_time)) ;
$start_day         = 1;
$limit             = LIMIT;

$export            = escapeString($_GET['export']) ;
$export            = ( $export == 'yes' ? 'yes' : 'no' ) ;

// 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 = '' ;


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

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



// page query

if($export != 'yes'){
    $mysqli_query = "SELECT a.*, b.staff_idno, b.staff_name FROM salary_slip a 
                     JOIN staff b ON (a.staff_id = b.staff_id) 
                     WHERE a.month = '$date_time_month' AND " ;

    $mysqli_query = $mysqli_query . " a.deleted_at IS NULL  " . $search_query . $user_branch_permission_sql_b ;
    $mysqli_page  = $mysqli->query($mysqli_query."  ORDER BY " . $sort_by . ' ' . $sort_by_type . " LIMIT $start_from, " . $limit ) ;

    // print_r($mysqli_query."  ORDER BY " . $sort_by . ' ' . $sort_by_type . " LIMIT $start_from, " . LIMIT);exit;
    // 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_branch='.$_GET['search_branch'].'&sort_by='.$_GET['sort_by'].'&sort_by_type='.$_GET['sort_by_type'] ;

    // load pagination
   
    $page_pagination = nextPrevious($product_page, $limit, $search_url, $mysqli_query) ;

}else{

    $mysqli_query = "SELECT a.*, b.staff_idno, b.staff_name FROM salary_slip a 
                     JOIN staff b ON (a.staff_id = b.staff_id) 
                     WHERE a.month = '$date_time_month' AND a.deleted_at IS NULL ". $search_query. $user_branch_permission_sql_b ;
    $mysqli_page  = $mysqli->query($mysqli_query."  ORDER BY a.staff_id") ;
}



$salary_slip = [];
if(mysqli_num_rows($mysqli_page) > 0){
    while($row = mysqli_fetch_assoc($mysqli_page)){
        $salary_slip[$row['slip_id']] = $row;
    }
}


//export excel
if($export == 'yes'){

    include 'PhpExcel/PHPExcel.php' ;

    // set excel content
    $page_filename = 'Staff Salary - '.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,
        )
    );

    $styleArrayTitle2 = array( 
        'font'  => array(
            'bold'  => true ,
            'color' => array( 'rgb' => '000000' ) ,
        ),
        '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,
        )
    ) ;

    $styleArrayRed = array(
        'font'  => array(
            'bold'  => true ,
            'color' => array( 'rgb' => '000000' ) ,
        ),
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFC0CB')
        ),
        'borders' => array(
            'outline' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
            )
        ),
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
    );

    $styleArrayYellow = array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFFFB1')
        ),
    );

    $styleArrayPink = array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFD8F1')
        ),
    );

    $styleArraySmoke = array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'F9F1F0')
        ),
    );

    $styleArrayGreen = array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'ddf7e3')
        ),
    );

    $styleArrayYellowGreen = array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FAFBF4')
        ),
    );

    $count     = 1 ;
    $firstChar = 'A' ;
    $lastChar = "Q";

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

    $array_title = array( 'No.', 'ID', 'Staff Name', "Basic Salary\r(RM)", "Commission\r(RM)", "Allowance\r(RM)", "Deduction\r- (RM)", "Subtotal\r(RM)", "EPF\r- (RM)", "SOCSO\r- (RM)", "EIS\r- (RM)", "MZF\r- (RM)", "PCB\r- (RM)", "Total\r(RM)", "Employer EPF\r(RM)", "Employer SOCSO\r(RM)", "Employer EIS\r(RM)") ;


    foreach($array_title as $k => $v){
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar.$count, $v ) ;
        $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayTitle2 ) ;
        $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->getAlignment()->setWrapText(true);
        if($firstChar >= 'D' && $firstChar <= 'G'){
            $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayYellow ) ;
        }else if($firstChar == 'H'){
            $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayPink ) ;
        }else if($firstChar >= 'I' && $firstChar <= 'M'){
            $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArraySmoke ) ;
        }else if($firstChar == 'N'){
            $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayGreen ) ;
        }else if($firstChar >= 'O'){
            $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayYellowGreen ) ;
        }     
        $firstChar++;
    }

    if(count($salary_slip) > 0){
        $counter = 0;
        $total_salary = $total_em_epf = $total_em_socso = $total_em_eis = 0;
        foreach($salary_slip as $key => $value){
            $firstChar = 'A';
            $counter ++ ;
            $count ++ ;
            $array_content = array($counter, $value['staff_idno'], $value['staff_name'], $value['basic_salary'], $value['commission'], $value['allowance'], $value['deduction'], $value['sub_total'], $value['staff_epf'], $value['staff_socso'], $value['staff_eis'], $value['staff_zakat'], $value['staff_pcb'], $value['total'], $value['employer_epf'], $value['employer_socso'], $value['employer_eis']);
            $total_salary += $value['total'];
            $total_em_epf += $value['employer_epf'];
            $total_em_socso += $value['employer_socso'];
            $total_em_eis += $value['employer_eis'];
            foreach($array_content as $key2 => $value2){
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar.$count, $value2 ) ;
                $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArray ) ;
                if($firstChar >= 'D' && $firstChar <= 'G'){
                    $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayYellow ) ;
                }else if($firstChar == 'H'){
                    $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayPink ) ;
                }else if($firstChar >= 'I' && $firstChar <= 'M'){
                    $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArraySmoke ) ;
                }else if($firstChar == 'N'){
                    $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayGreen ) ;
                }else if($firstChar >= 'O'){
                    $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayYellowGreen ) ;
                } 
                $firstChar++;
            }    
        }

        $count++;
        $count++;
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue( 'N'.$count, 'Total Salary' ) ;
        $objPHPExcel->getActiveSheet()->getStyle( 'N'.$count )->applyFromArray( $styleArrayRed ) ;
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue( 'O'.$count, "Total\rEmployer EPF" ) ;
        $objPHPExcel->getActiveSheet()->getStyle( 'O'.$count )->applyFromArray( $styleArrayRed ) ;
        $objPHPExcel->getActiveSheet()->getStyle( 'O'.$count )->getAlignment()->setWrapText(true);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue( 'P'.$count, "Total\rEmployer SOCSO" ) ;
        $objPHPExcel->getActiveSheet()->getStyle( 'P'.$count )->applyFromArray( $styleArrayRed ) ;
        $objPHPExcel->getActiveSheet()->getStyle( 'P'.$count )->getAlignment()->setWrapText(true);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue( 'Q'.$count, "Total\rEmployer EIS" ) ;
        $objPHPExcel->getActiveSheet()->getStyle( 'Q'.$count )->applyFromArray( $styleArrayRed ) ;
        $objPHPExcel->getActiveSheet()->getStyle( 'Q'.$count )->getAlignment()->setWrapText(true);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue( 'R'.$count, 'GRAND TOTAL' ) ;
        $objPHPExcel->getActiveSheet()->getStyle( 'R'.$count )->applyFromArray( $styleArrayRed ) ;

        $grand_total = $total_salary + $total_em_epf + $total_em_socso + $total_em_eis;

        $count++;
        $firstChar = 'A';
        $array_footer = array('', '', '', '', '', '', '', '', '', '', '', '', '', $total_salary, $total_em_epf, $total_em_socso, $total_em_eis, $grand_total);
        foreach($array_footer as $kk => $vv){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $firstChar.$count, $vv ) ;
            if($firstChar >= 'N'){
                $objPHPExcel->getActiveSheet()->getStyle( $firstChar.$count )->applyFromArray( $styleArrayRed ) ;
            }     
            $firstChar++;
        }
    }

    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(4);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(7);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
    $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(8);
    $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(8);
    $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(9);
    $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(11);
    $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(12);
    $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(15);



    
    // Submission from
    header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) ; 
    header( 'Content-Disposition: attachment;filename="'.$page_filename.'.xls"' ) ;
    header( 'Cache-Control: max-age=0' ) ;
    // save to pc
    $objWriter->save('php://output') ;
    header( "Refresh: 0" ) ;
    exit ;

}



?>