<?php
   include('session.php');
   include('header1.php');
   include('ietpartial.php');
   include('calculations.php');

// start role specific 
$user_role = $_SESSION['role'];
$companyID = $_SESSION['companyselect'];

$period=$_GET['payrollID'];

$checkEntry1 = "SELECT * FROM periods WHERE periodid = $period AND bonusBoolean = '1' AND companyid = '$companyID';";

$resultCheck1 = mysqli_query($db,$checkEntry1);

$resultCheckcount1 = mysqli_num_rows($resultCheck1);

while ($rowpaysheet = mysqli_fetch_array($resultCheck1)) {
    $startdate=$rowpaysheet['startDate'];
    $enddate=$rowpaysheet['endDate'];
}

$date1 = $startdate;
$date2 = $enddate;

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);
$month2 = date('m', $ts2);

$diff = (($year2 - $year1) * 12) + ($month2 - $month1);

$numofmonths = $diff+1;



          $sqlView = "SELECT 
                  employees.employeename AS employeename,
                  employees.employeesurname AS employeesurname,
                  employees.employeeid AS employeeid,
                  employees.employee_paye_rate AS employee_paye_rate,
                  employees.employee_edf_amount AS employee_edf_amount,
                  employees.edf_months_value AS edf_months_value,
                  departments.departmentname AS departmentname,
                  positions.positionname AS positionname,
                  SUM(carbenefits.tiervalue) AS carbenefitvalue,
                  SUM(payrollbasicdetails.basicSal) AS basicSal,                  
                  SUM(payrollbasicdetails.specialBonus) AS specialBonus,                  
                  SUM(payrollbasicdetails.commission) AS commission,                  
                  SUM(payrollbasicdetails.absencesValue) AS absencesValue,                  
                  SUM(payrollbasicdetails.totalPackage) AS totalPackage,                  
                  SUM(payrollbasicdetails.overtime) AS overtime,
                  SUM(payrollbasicdetails.totalcarallowance) AS totalcarallowance
                  FROM payrollbasicdetails
                  LEFT JOIN employees
                  ON payrollbasicdetails.employeeid = employees.employeeid
                  LEFT JOIN carbenefits
                  ON employees.employee_carbenefit = carbenefits.carbenefitid
                  LEFT JOIN employee_job
                  ON employee_job.employeeid = employees.employeeid
                  LEFT JOIN positions
                  ON employee_job.positionid = positions.positionid
                  LEFT JOIN departments
                  ON employee_job.departmentid = departments.departmentid
                  RIGHT JOIN periods
                  ON payrollbasicdetails.payrollid = periods.periodid
                  WHERE periods.startDate BETWEEN '$startdate' AND '$enddate'
                  AND employees.companyid = $companyID
                  GROUP BY employeename";
                

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

                  $count = mysqli_num_rows($result);

                  echo "<main role=\"main\" class=\"col-md-9 ml-sm-auto col-lg-10 px-4\">";

            echo "<div class=\"d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom\">";

            if ($count !== 0) { // COUNT START HERE
              
                  echo "<h1 class=\"h2\">Bonus - " . $startdate . " - " . $enddate . "</h1>";

                   $checkEntry = "SELECT * FROM periods WHERE startDate = '$startdate' AND endDate = '$enddate' AND bonusBoolean = '1' AND companyid = '$companyID';";

                  $resultCheck = mysqli_query($db,$checkEntry);

                  $resultCheckcount = mysqli_num_rows($resultCheck);

                

                  

                  echo "</h1>";
             
              echo "<div class=\"btn-toolbar mb-2 mb-md-0\">"; // TOOLBAR STARTS HERE

                  echo "</div>";

            echo "</div>";

            // --- ALLOWANCES START HERE

            $allowances = array();

            $sqlAllowances = "SELECT *
            FROM benefits
            ORDER BY benefitname ASC;";

            $resultAllowances = mysqli_query($db,$sqlAllowances);

            $countAllowances = mysqli_num_rows($resultAllowances);

              if ($countAllowances !== 0) { 

                while ($rowAllowances = mysqli_fetch_array($resultAllowances)) {
                  $allowances[$rowAllowances['benefitid']] = $rowAllowances['benefitname'];
                }

               }

            // --- ALLOWANCES END HERE

            // --- DEDUCTIONS START HERE

            $deductions1 = array();

            $sqlDeductions1 = "SELECT *
            FROM deductions
            ORDER BY deductionname ASC;";

            $resultDeductions = mysqli_query($db,$sqlDeductions1);

            $countDeductions = mysqli_num_rows($resultAllowances);

              if ($countDeductions !== 0) { 

                while ($rowdDeductions = mysqli_fetch_array($resultDeductions)) {
                  $deductions1[$rowdDeductions['deductionid']] = $rowdDeductions['deductionname'];
                }

               }

            // --- DEDUCTIONS END HERE


            // TABLE STARTS HERE

          echo "<table class = \"table\" border=\"1\">";

          echo "<thead>";
          echo "<td>Employee Name</td>";
          echo "<td>Department</td>";
          echo "<td>Position</td>";
          echo "<td>Basic Salary - SUM</td>";
          echo "<td>Special Bonus - SUM</td>";
          echo "<td>Allowances - SUM</td>";
          echo "<td>Commission - SUM</td>";
          echo "<td>Absences - SUM</td>";
          echo "<td>Overtime - SUM</td>";
          echo "<td>Gross Bonus</td>"; 
          echo "<td>PAYE</td>";
          //echo "<td>PRGF</td>";
          echo "<td>Employee CSG</td>";
          //echo "<td>Employer CSG</td>";
          echo "<td>Net Bonus</td>";
          echo "</thead>";
          echo "<tbody>";

          $sum_GrossBonus = 0.0;
          $sum_PAYE = 0.0;
          $sum_EmployeeCSG = 0.0;
          $sum_NetBonus = 0.0;

          while ($rowpaysheet = mysqli_fetch_array($result)) { // FETCHING EMPLOYEE DATA START
            echo "<tr>";
            echo "<td>" . $rowpaysheet['employeesurname'] . ", " . $rowpaysheet['employeename'] . "</td>";
            echo "<td>" . $rowpaysheet['departmentname'] . "</td>";
            echo "<td>" . $rowpaysheet['positionname'] . "</td>";
            echo "<td>" . $rowpaysheet['basicSal'] . "</td>";
            echo "<td>" . $rowpaysheet['specialBonus'] . "</td>";


                $sqlTotalBonusAllowances = "SELECT
                benefits.benefitname AS benefitname,
                employee_benefits.employeeid AS employeeid,
                sum(employee_benefits.benefitvalue) AS benefitvalue
                FROM employee_benefits
                INNER JOIN benefits
                ON employee_benefits.benefitid = benefits.benefitid
                WHERE employee_benefits.employeeid = '" . $rowpaysheet['employeeid'] . "' 
                AND benefitname IN ('House Allowance','Other Allowance');";

                $resultTotalBonusAllowance = mysqli_query($db,$sqlTotalBonusAllowances);

                $countTotalBonusAllowance = mysqli_num_rows($resultTotalBonusAllowance);

                $totalBonusAllowances = 0;

                  if ($countTotalBonusAllowance !== 0) { 

                    while ($rowTotalBonusAllowance = mysqli_fetch_array($resultTotalBonusAllowance)) {
                      $totalBonusAllowances = $rowTotalBonusAllowance['benefitvalue'] * $numofmonths;

                    }

                   } 


            echo "<td>" . $totalBonusAllowances . "</td>";
            echo "<td>" . $rowpaysheet['commission'] . "</td>";
            echo "<td>" . $rowpaysheet['absencesValue'] . "</td>";
            echo "<td>" . $rowpaysheet['overtime'] . "</td>";



          $bonusValue = round(($rowpaysheet['basicSal'] + $rowpaysheet['specialBonus'] + $totalBonusAllowances + $rowpaysheet['commission'] - $rowpaysheet['absencesValue']  + $rowpaysheet['overtime']  )/$numofmonths,0);
            
          echo "<td>" . $bonusValue . "</td>";
          $sum_GrossBonus += $bonusValue;

            $calculatedPAYE = round(($bonusValue - ($rowpaysheet['employee_edf_amount'] / $rowpaysheet['edf_months_value'])) * ($rowpaysheet['employee_paye_rate']/100),0);

            if ($calculatedPAYE < 0 ){ 
              $calculatedPAYE = 0;
            }

            echo "<td>" . $calculatedPAYE . "</td>";
            $sum_PAYE += $calculatedPAYE;

            // PRGF
            
            $calculatedPRGF = getPRGFEmployer(intval($bonusValue),$enddate,$companyID,$db);
            $employerPRGF = $calculatedPRGF;
            //echo "<td>" . $calculatedPRGF . "</td>";


            // Employee CSG
            $employeeCSG = getCSGEmployee(intval($bonusValue),$db);

            echo "<td>" . $employeeCSG . "</td>";
            $sum_EmployeeCSG += $employeeCSG;


            // Employer CSG
            
            $employerCSG = getCSGEmployer(intval($bonusValue),$db);

            //echo "<td>" . $employerCSG . "</td>";


            $finalBonus = $bonusValue - $calculatedPAYE - $employeeCSG;
            echo "<td>" . $finalBonus . "</td>";
            $sum_NetBonus += $finalBonus;


           
            echo "</tr>";

           

         
                  


          } // FETCHING EMPLOYEE DATA END

          echo "<tr>";

          echo "<td></td>";
          echo "<td></td>";
          echo "<td></td>";
          echo "<td></td>";
          echo "<td></td>";
          echo "<td></td>";
          echo "<td></td>";
          echo "<td></td>";
          echo "<td></td>";
          echo "<td><b>" . $sum_GrossBonus . "</b></td>";
          echo "<td><b>" . $sum_PAYE . "</b></td>";
          echo "<td><b>" . $sum_EmployeeCSG . "</b></td>";
          echo "<td><b>" . $sum_NetBonus . "</b></td>";

          echo "</tr>";

          echo "</tbody>";
          echo "</table>";

            // TABLE ENDS HERE


            } // COUNT EMPLOYEES END HERE


    



include('footer1.php');
?>