<?php
session_start();
$month=$_GET['m'];
$year=$_GET['y'];
$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';


// 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:R1');
$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);
$spreadsheet->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('P')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('Q')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getColumnDimension('R')->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:R3')->applyFromArray($styleArray1);
$spreadsheet->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);



$sheet->setTitle('Gross sheet');
$stmt = $conn->prepare(" SELECT `name`,`designation`,`emp_id`,`branch`,`attendance`,`basic_pay`,`hra`,`convyance`,`medical`,`gross_pay`,`pf`,`esic`,`advance`,`tax`,`spc_allow`,`deduction`,`net_pay` FROM `prv_emp_salary` WHERE `month`='$month' AND `year`='$year' UNION SELECT 'z', '','','TOTAL',  SUM(`attendance`),  SUM(`basic_pay`),  SUM(`hra`),  SUM(`convyance`),  SUM(`medical`),  SUM(`gross_pay`),  SUM(`pf`),  SUM(`esic`),  SUM(`advance`),  SUM(`tax`),  SUM(`spc_allow`),  SUM(`deduction`), SUM(`net_pay`) from `prv_emp_salary` WHERE `month`='$month' and `year`='$year' ");
 //print_r($stmt);exit;   
$stmt->execute();
$a = 1;
$i = 4;
  $sheet->setCellValue('A1', ' GROSS WAGE SHEET FOR THE '.$monthName.' '. $year);
  $sheet->setCellValue('A3', 'SR NO');
  $sheet->setCellValue('B3', 'NAME');
  $sheet->setCellValue('C3', 'DESIGNATION');
  $sheet->setCellValue('D3', 'EMP ID');
  $sheet->setCellValue('E3', 'ATTENDENCE');
  $sheet->setCellValue('F3', 'BASIC PAY');
  $sheet->setCellValue('G3', 'HRA');
  $sheet->setCellValue('H3', 'CONVEYANCE');
  $sheet->setCellValue('I3', 'MEDICAL');
  $sheet->setCellValue('J3', 'GROSS');
  $sheet->setCellValue('K3', 'PF');
  $sheet->setCellValue('L3', 'ESIC');
  $sheet->setCellValue('M3', 'ADVANCE');
  $sheet->setCellValue('N3', 'TAX');
  $sheet->setCellValue('O3', 'SPECIAL ALLOWANCES');
  $sheet->setCellValue('P3', 'DEDUCTION');
  $sheet->setCellValue('Q3', 'NET-PAY');
  $sheet->setCellValue('R3', 'BRANCH');
   
while ($row = $stmt->fetch(PDO::FETCH_NAMED)) {
  $sheet->setCellValue('A'.$i, $a);
  $sheet->setCellValue('B'.$i, $row['name']);
  $sheet->setCellValue('C'.$i, $row['designation']);
  $sheet->setCellValue('D'.$i, $row['emp_id']);
  $sheet->setCellValue('E'.$i, $row['attendance']);
  $sheet->setCellValue('F'.$i, $row['basic_pay']);
  $sheet->setCellValue('G'.$i, $row['hra']);
  $sheet->setCellValue('H'.$i, $row['convyance']);
  $sheet->setCellValue('I'.$i, $row['medical']);
  $sheet->setCellValue('J'.$i, $row['gross_pay']);
  $sheet->setCellValue('K'.$i, $row['pf']);
  $sheet->setCellValue('L'.$i, $row['esic']);
  $sheet->setCellValue('M'.$i, $row['advance']);
  $sheet->setCellValue('N'.$i, $row['tax']);
  $sheet->setCellValue('O'.$i, $row['spc_allow']);
  $sheet->setCellValue('P'.$i, $row['deduction']);
  $sheet->setCellValue('Q'.$i, $row['net_pay']);
  
    $stmt1 = $conn->prepare("SELECT `name`  from branch WHERE `id`='".$row['branch']."' ");
    $stmt1->execute();
    while ($row1 = $stmt1->fetch(PDO::FETCH_NAMED)) {
      $sheet->setCellValue('R'.$i, $row1['name']);
    }
 
  $i++;
  $a++;
}

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


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