<?php
session_start();
require_once 'system/conn.php';
$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 !

$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:K1');
$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('H')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('K')->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:P3')->applyFromArray($styleArray1);
$spreadsheet->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);

$sheet->setTitle('Attendance sheet');
//$stmt = $conn->prepare("SELECT emp_details.e_name , emp_details.designation, salary_attendance.emp_id, salary_attendance.t_a, salary_attendance.b_a, salary_attendance.o_a , salary_attendance.details  FROM `salary_attendance` LEFT JOIN `emp_details` ON salary_attendance.emp_id=emp_details.e_id AND salary_attendance.branch=emp_details.branch where salary_attendance.branch='".$_SESSION['branch']."' and salary_attendance.month='$month' and salary_attendance.year='$year' ORDER BY e_name ASC");
$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.' SITE EMPLOYEE ATTENDANCE SHEET '.strtoupper($monthName).' '. $year);
  $sheet->setCellValue('A3', 'SR NO');
  $sheet->setCellValue('B3', 'Emp Id');
  $sheet->setCellValue('C3', 'Name');
  $sheet->setCellValue('D3', 'Designation');
  $sheet->setCellValue('E3', 'Site');
  $sheet->setCellValue('F3', 'Attendance');
  $sheet->setCellValue('G3', 'Only OT');
  $sheet->setCellValue('H3', 'Cheque');
  $sheet->setCellValue('I3', 'Hold');
  $sheet->setCellValue('J3', 'Adv. Dedct.');
  $sheet->setCellValue('K3', 'Spc. Allow.');
 
   
while ($row = $stmt->fetch(PDO::FETCH_NAMED)) {
  $stmt1 = $conn->prepare(" SELECT * FROM `emp_details` where `e_id`='".$row['emp_id']."' and `branch`='".$_SESSION['branch']."' ");
  $stmt1->execute();
  while ($row1 = $stmt1->fetch(PDO::FETCH_NAMED)) {
      
  $sheet->setCellValue('A'.$i, $a);
  $sheet->setCellValue('B'.$i, $row['emp_id']);
  $sheet->setCellValue('C'.$i, $row1['e_name']);
  $sheet->setCellValue('D'.$i, $row1['designation']);
  
  $stmt2 = $conn->prepare(" SELECT * FROM `site` WHERE `id`='".$row['site']."' ");
  $stmt2->execute();
  while ($row2 = $stmt2->fetch(PDO::FETCH_NAMED)) {
  $sheet->setCellValue('E'.$i, $row2['name']);
  }
  
  $sheet->setCellValue('F'.$i, $row['attendance']);
  $sheet->setCellValue('G'.$i, $row['only_ot']);
  $sheet->setCellValue('H'.$i, $row['check']);
  $sheet->setCellValue('I'.$i, $row['block']);
  $sheet->setCellValue('J'.$i, $row['advance_deduction']);
  $sheet->setCellValue('K'.$i, $row['spc_allow']);
  
  }
  $i++;
  $a++;
}

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


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