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


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

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

  $bonusid = $_GET['payrollID'];

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

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

  $countPayroll = mysqli_num_rows($resultPayroll);

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

    $updated = $_GET['updated'];

  }

    $sqlView = "SELECT 
          employees.employeeid AS employeeid,
          employees.employeename AS employeename,
          employees.employeesurname AS employeesurname,
          employees.employeenic AS employeenic,
          employees.employee_tan_number AS employee_tan_number,
          employees.employee_join_date AS joining_date,
          departments.departmentname AS departmentname,
          positions.positionname AS positionname,
          bonuspayouts.bonusAmount AS bonusAmount,
          bonuspayouts.bonusAdjustment AS bonusAdjustment,
          bonuspayouts.finalBonusValue AS finalBonusValue,
          bonuspayouts.bonusPayeAmount AS bonusPayeAmount,
          bonuspayouts.employeeCSG AS employeeCSG,
          periods.startDate AS periodStart,
          periods.endDate AS periodEnd
          FROM bonuspayouts
          LEFT JOIN employees
          ON bonuspayouts.employeeid = employees.employeeid
          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
          LEFT JOIN periods
          ON bonuspayouts.bonusPayrollid = periods.periodid
          WHERE bonuspayouts.bonusPayrollid = $bonusid";

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

    $count = mysqli_num_rows($result);



    echo "<main role=\"main\" class=\"\">";

    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 EMPLOYEES START HERE
      $periodSelectID = 0;
      while ($rowperiods = mysqli_fetch_array($resultPayroll)) { 
        $periodSelectID = $rowperiods['periodid'];
          echo "<h1 class=\"h2\">Payslips - " . $rowperiods['startDate'] . " - " . $rowperiods['endDate'] . "</h1>";
      }

      echo "<div class=\"btn-toolbar mb-2 mb-md-0\">"; // TOOLBAR STARTS HERE
      echo "<div class=\"btn-group mr-2\">";
        echo "<a href=\"payrolls.php\" class=\"no-print btn btn-sm btn-outline-secondary\" role=\"button\">Back</a>";

          echo "</div>";
          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

  while ($rowpaysheet = mysqli_fetch_array($result)) { // FETCHING EMPLOYEE DATA START

  echo "<div class=\"card pagebreak\">" ;
        echo "<div class=\"card-body\">";    

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

      $getCompanyName = "SELECT * FROM companyfile WHERE companyid = $companyID";

        $resultCompanyName = mysqli_query($db,$getCompanyName);

                $numCompanyName = mysqli_num_rows($resultCompanyName);

        while ($rowCompanyName = mysqli_fetch_array($resultCompanyName)) { 
            echo "<tr><td colspan=\"2\">";

                          // Check if file already exists
            if (file_exists("logo.png")) {
              echo "<center>";
              echo "<img src = \"logo.png\" style=\"width:50px;height:50px;\">";
              echo "</center><br>";
              $uploadOk = 0;
            } else {
              echo "";
            }


            echo "<center><h3>" . $rowCompanyName['companyName'] . "</h3>";
            echo "<center><h5>PAYE Registration No." . $rowCompanyName['companyERN'] . "</h5>";

        }

     
      echo "<h4>Bonus Payslip for " . $rowpaysheet['periodStart'] ." to " . $rowpaysheet['periodEnd'] . "</h4></td></tr>";

      echo "<tr>";
      echo "<td colspan=\"3\">";
        echo "<p>Employee Name:&#9;<b>" . $rowpaysheet['employeename'] . ", " . $rowpaysheet['employeesurname'] . "</b></p>";
        echo "<p>Employee NIC:&#9;<b>" . $rowpaysheet['employeenic'] . "</b></p>";
        echo "<p>Employee Joining Date:&#9;<b>" . $rowpaysheet['joining_date'] . "</b></p>";

        if ($rowpaysheet['employee_tan_number'] != "") {
          echo "<p>Employee TAN Number:&#9;<b>" . $rowpaysheet['employee_tan_number'] . "</b></p>";
        }
        

        echo "<p>Posting:&#9;<b>" . $rowpaysheet['positionname'] . "," . $rowpaysheet['departmentname'] .  "</b></p>";

       

        

      echo "</td>";
      echo "</tr>";
      echo "<tr>";

      echo "<td>";
      $totalBonusInclusiveOfPaye = $rowpaysheet['bonusAmount'] + $rowpaysheet['bonusPayeAmount'];

      
      if ($rowpaysheet['bonusAdjustment'] != 0) {
        //echo "<p>Bonus Adjustment:&#9;<b>" . number_format($rowpaysheet['bonusAdjustment'], 2, '.', ',') . "</b></p>";
        echo "<p>Gross Bonus :&#9;<b>" . number_format($rowpaysheet['finalBonusValue'], 2, '.', ',') . "</b></p>";
      } else {
        echo "<p>Gross Bonus:&#9;<b>" . number_format($rowpaysheet['bonusAmount'], 2, '.', ',') . "</b></p>";

      }
      
      echo "<p>PAYE Deducted:&#9;<b>" . number_format($rowpaysheet['bonusPayeAmount'], 2, '.', ',') . "</b></p>";
      echo "<p>CSG Deducted:&#9;<b>" . number_format($rowpaysheet['employeeCSG'], 2, '.', ',') . "</b></p>";

      echo "</td>";

      echo "</tr>";


      echo "<tr>";
      echo "<td colspan=\"3\">";
      
      if ($rowpaysheet['finalBonusValue'] == 0 || $rowpaysheet['finalBonusValue'] == null) {
        $netBonus = $rowpaysheet['bonusAmount'] - $rowpaysheet['bonusPayeAmount'] - $rowpaysheet['employeeCSG'];
      } else {
        $netBonus = $rowpaysheet['finalBonusValue'] - $rowpaysheet['bonusPayeAmount'] - $rowpaysheet['employeeCSG'];
      }

      echo "<h4>Net Bonus (MUR):&#9;<b>" . number_format($netBonus, 2, '.', ',') . "</b>  <i>" . numberTowords($netBonus) . " only</i></h4>";
      echo "<center><p><i>This payslip is computer generated and requires no signature</i></p></center>";
      echo "</tr>";
      echo "</td>";

      echo "</table>";

  echo "</div>";
  echo "</div>"; //end cards



    


  } // FETCHING EMPLOYEE DATA END


    } // COUNT EMPLOYEES END HERE

} // GET ENDS HERE



include('footer1.php');

?>