<?php
session_start();
$date=explode('-',date("Y-m-d", strtotime("last day of previous month")));
$month=$date[1];
$year=$date[0];

$monthNum  =$month;
$dateObj   = DateTime::createFromFormat('!m', $monthNum);
$monthName = strtoupper($dateObj->format('F'));


// CONNECT TO DATABASE
// ! CHANGE THESE SETTINGS TO YOUR OWN !
      require_once '../system/conn.php';


$query=$conn->prepare("SELECT name FROM branch WHERE id=:param");
$query->bindParam(':param', $_SESSION['branch']);
$query->execute();
$result = $query -> fetch(PDO::FETCH_ASSOC);
$br=$result['name'];
// CREATE PHPSPREADSHEET OBJECT
require "excel_test/vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// CREATE A NEW SPREADSHEET + POPULATE DATA
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$spreadsheet->getActiveSheet()->mergeCells('A1:O1');
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);




$styleArray = [
    'font' => [
        'bold' => true,
    ],
    'alignment' => [
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
    ],
    'fill' => [
        'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
        'rotation' => 90,
        'startColor' => [
            'argb' => 'FFA0A0A0',
        ],
        'endColor' => [
            'argb' => 'FFFFFFFF',
        ],
    ],
];
$styleArray1 = [
    'font' => [
        'bold' => true,
    ],
    'alignment' => [
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
    ],
];

$spreadsheet->getActiveSheet()->getStyle('A3:O3')->applyFromArray($styleArray1);
$spreadsheet->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);



$sheet->setTitle('Gross sheet');

$stmt = $conn->prepare("SELECT `e_id`, `e_name`, `fname`,`designation`, `mobile_num`,`uan_no`, `aadhar_num`, `pan_card`,`e_doj`,`dob`, `salary`, `bank_ac_no`, `addr`, `pf`  from emp_details WHERE `branch`='".$_SESSION['branch']."' AND `status`='1' ORDER BY e_name ASC");
    
$stmt->execute();
$a = 1;
$i = 4;
  $sheet->setCellValue('A1', $_GET['ex']. 'Year Of Experience');
  $sheet->setCellValue('A3', 'SR NO');
  $sheet->setCellValue('B3', 'EID');
  $sheet->setCellValue('C3', 'NAME');
  $sheet->setCellValue('D3', 'FATHER NAME');
  $sheet->setCellValue('E3', 'DESIGNATION');
  $sheet->setCellValue('F3', 'UAN NO');
  $sheet->setCellValue('G3', 'MOBILE');
  $sheet->setCellValue('H3', 'ADHAR');
  $sheet->setCellValue('I3', 'PAN');
  $sheet->setCellValue('J3', 'DOJ');
  $sheet->setCellValue('K3', 'DOB');
  $sheet->setCellValue('L3', 'SALARY');
  $sheet->setCellValue('M3', 'ACCOUNT');
  $sheet->setCellValue('N3', 'ADDRESS');
  $sheet->setCellValue('O3', 'PF STATUS');
  
   
while ($row = $stmt->fetch(PDO::FETCH_NAMED)) {
    $ex=  $_GET['ex']; 
                                        $exp=$row['e_doj'];
                                        $exp=explode('-',$exp);
                                        $date = "$exp[0]-$exp[1]-$exp[2]";
                                        $dob = new DateTime($date);
                                        $now = new DateTime();
                                        $now->diff($dob)->y;
                                        $difference = time() - strtotime($date);
                                        //print_r($difference);exit;
                                        $p=floor($difference / 31556926);
                                        if($p==$ex){
  $sheet->setCellValue('A'.$i, $a);
  $sheet->setCellValue('B'.$i, $row['e_id']);
  $sheet->setCellValue('C'.$i, $row['e_name']);
  $sheet->setCellValue('D'.$i, $row['fname']);
  $sheet->setCellValue('E'.$i, $row['designation']);
  $sheet->setCellValue('F'.$i, $row['uan_no']);
  $sheet->setCellValue('G'.$i, $row['mobile_num']);
  $sheet->setCellValue('H'.$i, $row['aadhar_num']);
  $sheet->setCellValue('I'.$i, $row['pan_card']);
  $sheet->setCellValue('J'.$i, $row['e_doj']);
  $sheet->setCellValue('K'.$i, $row['dob']);
  $sheet->setCellValue('L'.$i, $row['salary']);
  $sheet->setCellValue('M'.$i, ' '.$row['bank_ac_no']);
  $sheet->setCellValue('N'.$i, $row['addr']);
  $sheet->setCellValue('O'.$i, $row['pf']);
  
 
  $i++;
  $a++;
}}

$spreadsheet->getActiveSheet()->getStyle('A'.$i.':O'.$i)->applyFromArray($styleArray1);


// OUTPUT
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Experience Employees(Admin).xlsx"');
header('Cache-Control: max-age=0');
header('Expires: Fri, 11 Nov 2011 11:11:11 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer->save('php://output');
?>