<?php
session_start();
require_once 'system/conn.php';
$branch=$_SESSION['branch'];
$date1=date('Y-m-d');
$date=date('Y-m', strtotime(date('Y-m')." -1 month"));
$date=explode('-',$date);
$month=$date[1];
$monthNum  = $month;
$dateObj   = DateTime::createFromFormat('!m', $monthNum);
$monthName = strtoupper($dateObj->format('F'));
$year=$date[0];
$year1 = $year.'-'.$month.'-'.'01';




$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:M1');
$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);




$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:M3')->applyFromArray($styleArray1);
$spreadsheet->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);



$sheet->setTitle('Gross sheet');
$date=date('m-d-Y', strtotime(date('Y-m')." -1 month"));
$date=explode('-',$date);
$month=$date[0];
$year=$date[2];
//print_r($month);exit;
$cnt = 1;
$stmt = $conn->prepare(" SELECT * FROM `attendance` where month='".$month."'  and year='".$year."' and `branch`='".$_SESSION['branch']."' ");
    
$stmt->execute();
$a = 1;
$i = 4;
  $sheet->setCellValue('A1', $br.' NEW EMPLOYEE SHEET FOR THE '.$monthName.' '. $year);
  $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');
  
   
while ($row = $stmt->fetch(PDO::FETCH_NAMED)) {
  $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['mobile_num']);
  $sheet->setCellValue('G'.$i, $row['aadhar_num']);
  $sheet->setCellValue('H'.$i, $row['pan_card']);
  $sheet->setCellValue('I'.$i, $row['e_doj']);
  $sheet->setCellValue('J'.$i, $row['dob']);
  $sheet->setCellValue('K'.$i, $row['salary']);
  $sheet->setCellValue('L'.$i, $row['bank_ac_no']);
  $sheet->setCellValue('M'.$i, $row['addr']);
  
 
  $i++;
  $a++;
}

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


// OUTPUT
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="users.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');
?>