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

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

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

  include('header2.php');

          $startdate=$_POST['startdate'];
          $enddate=$_POST['enddate'];
          if (isset($_POST['getallemployees'])) {

            $getallemployees=$_POST['getallemployees'];
          }
          
          $getemployeeid=$_POST['getemployeeid'];


          $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.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 periods.startDate BETWEEN '$startdate' AND '$enddate'
            AND periods.bonusBoolean = '1'
                  AND employees.companyid = $companyID";

          if (isset($getallemployees)) {

                  } else {
                    $sqlAddonEmployee = " AND bonuspayouts.employeeid = '$getemployeeid';";
                    $sqlView = $sqlView . $sqlAddonEmployee;                    
                  }

                  $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 START HERE
              
                  echo "<h1 class=\"h2\">Payslip Report - " . $startdate . " - " . $enddate . "
                  <a href = \"report-BonuspayslipByPeriod.php\">< Back</a>
                  </h1>";

                  if (isset($getallemployees)) {

                  } else {
                    echo " Single Employee ";
                  }


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

                  echo "</div>";

            echo "</div>";






  // 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 "";
              echo "</center><br>";
              $uploadOk = 0;
            } else {
              echo "";
            }

            echo "
            <style>
            .header-style {

              vertical-align: middle;
            }

            </style>
            ";



            echo "<table border=\"0\"><tr><td><img src = \"logo.png\" style=\"width:150px;height:150px;\"></td>
            <td class = \"header-payslip\"><h3>" . $rowCompanyName['companyName'] . "</h3>";
            echo "<center><h5>PAYE Registration No." . $rowCompanyName['companyERN'] . "</h5>
            <h4>Bonus Payslip for " . $rowpaysheet['periodStart'] ." to " . $rowpaysheet['periodEnd'] . "</h4>
            </td>
            </tr></table>";

        }

      
      echo "</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'];
      echo "<p>Bonus for Period:&#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\">";
      $netBonus = $rowpaysheet['bonusAmount'] - $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




    }


            } // COUNT EMPLOYEES END HERE


      }  else { ///////////////////

        include('header1.php');

  $sqlGetEmployee = "SELECT * FROM employees WHERE companyid = $companyID";

  $resultEmployee = mysqli_query($db,$sqlGetEmployee);

  $countEmployee = mysqli_num_rows($resultEmployee);

  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\">";


    	echo "<h1 class=\"h2\">Bonus Payslip by Period</h1>";
 
    
    	echo "</div>";

    	echo "<form method = \"POST\" action=\"report-BonuspayslipByPeriod.php\">";

      echo "<p>Start Date:<input type = \"date\" class=\"form-control\" name = \"startdate\"></p>";

      echo "<p>End Date:</p><input type = \"date\" class=\"form-control\" name = \"enddate\"></p>";

      echo "<p>All Employees <input type = \"checkbox\" name = \"getallemployees\"></p>";

      echo "<p>If Single Employee, Choose here:";

  if ($countEmployee !== 0) { 

    echo "<select name=\"getemployeeid\" id=\"getemployeeid\" class=\"form-control\">";

    while ($rowemployee = mysqli_fetch_array($resultEmployee)) {

      echo "<option value ='" . $rowemployee['employeeid'] . "'>" . $rowemployee['employeesurname'] . ", " . $rowemployee['employeename']  . "</option>";

    }

      echo "</select></p>";
} else {
  echo "<p>No employee on system..</p>";
}

    echo "<input type=\"submit\"> </form>";

   }



include('footer1.php');
?>