<?php
   include('session.php');
   include('connect.php');
   include('logging.php');
   $companyID = $_SESSION['companyselect'];


if ($_SERVER["REQUEST_METHOD"] == "GET") {

  $payrollid = $_GET['payrollID'];

  $sqlViewPayroll = "SELECT *
  FROM periods
  WHERE periodid = $payrollid;";

  $yearOfPeriod = "0000";
  $monthOfPeriod = "00";
  $periodDateData = "";

  $resultPayroll = mysqli_query($db,$sqlViewPayroll);

  $countPayroll = mysqli_num_rows($resultPayroll);

  if ($countPayroll != 0) {

      while ($rowPayrollData = mysqli_fetch_array($resultPayroll)) {

        $dateextract = strtotime($rowPayrollData['endDate']);
        $yearOfPeriod = date("Y",$dateextract);
        $monthOfPeriod = date("m",$dateextract);
        $periodDateData = $yearOfPeriod . $monthOfPeriod;

      }

  }

  if(isset($_GET['updated'])) {

    $updated = $_GET['updated'];

  }

    $sqlView = "SELECT 
          employees.employeename AS employeename,
          employees.employeesurname AS employeesurname,
          employees.employeenic AS employeenic,
          employees.employee_fulltime_boolean AS employeefulltime,
          employees.employee_contribution_code AS employeeccode,
          payrollbasicdetails.basicSal AS basicSal
          FROM payrollbasicdetails
          LEFT JOIN employees
          ON payrollbasicdetails.employeeid = employees.employeeid
          WHERE payrollbasicdetails.payrollid = $payrollid";

    $sqlBonusCSG = "SELECT
    employees.employeename AS employeename,
    employees.employeesurname AS employeesurname,
    employees.employeenic AS employeenic,
    employees.employee_fulltime_boolean AS employeefulltime,
    employees.employee_contribution_code AS employeeccode,
    bonuspayouts.bonusAmount AS bonusamount
    FROM bonuspayouts
    LEFT JOIN employees
    ON bonuspayouts.employeeid = employees.employeeid
    WHERE bonuspayouts.bonusPayrollid = $payrollid";

  
    $result = mysqli_query($db,$sqlBonusCSG);

    $count = mysqli_num_rows($result);

    if ($count != 0) {

    	//The name of the CSV file that will be downloaded by the user.
		$fileName = 'csg-bonus-return' . '-' . $periodDateData . '.csv';
		 
		//Set the Content-Type and Content-Disposition headers.
		header('Content-Type: application/excel');
		header('Content-Disposition: attachment; filename="' . $fileName . '"');

		$data = array();

		$sqlViewCompanyFile = "SELECT *
			FROM companyfile
      WHERE companyid = $companyID";
  
	    $resultCompanyFile = mysqli_query($db,$sqlViewCompanyFile);

	    while ($rowCompanyFile = mysqli_fetch_array($resultCompanyFile)) {

	    	array_push($data, array("CSGINFO",$rowCompanyFile['companyERN'],$rowCompanyFile['companyName'],$periodDateData,"28460","427","test@mns.mu",$rowCompanyFile['accountantPNumber'],$rowCompanyFile['accountantMNumber'],$rowCompanyFile['accountantName'],"Accountant"));
	     }


    	 while ($rowpayroll = mysqli_fetch_array($result)) { 

    	 	array_push($data, 
          array(
            $rowpayroll['employeenic'], 
            $rowpayroll['employeename'], 
            $rowpayroll['employeesurname'],
            number_format($rowpayroll['bonusamount'], 0, '.', ','),
            number_format($rowpayroll['bonusamount'], 0, '.', ','),
            $rowpayroll['employeeccode'],"M","1","Y"));
    	 }

    	 
		//Open up a PHP output stream using the function fopen.
		$fp = fopen('php://output', 'w');
		 
		//Loop through the array containing our CSV data.
		foreach ($data as $row) {
		    //fputcsv formats the array into a CSV format.
		    //It then writes the result to our output stream.
		    fputcsv($fp, $row);
		}
		 
		//Close the file handle.
		fclose($fp);
    }


 




} else {
	echo "Payroll not specified..";
}

?>