<?php

    //include_once('connect.php');

    include('session.php');
    include('calculations.php');
    //include('header1.php');
    $companyID = $_SESSION['companyselect'];

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

    $bonusData = count($_POST["bonusrecordid"]);

    echo "<h1>" . $bonusData . "</h1>";

    $bonuspayrollid = $_POST["bonuspayrollid"];

    $enddate = "0000-01-01";

    $sqlGetBonusDetails = "SELECT *
            FROM periods
            WHERE periodid = $bonuspayrollid";

            $resultBonusDetails = mysqli_query($db,$sqlGetBonusDetails);

            $countBonusDetails = mysqli_num_rows($resultBonusDetails);

              if ($countBonusDetails !== 0) { 

                while ($rowBonusDetails = mysqli_fetch_array($resultBonusDetails)) {
                  $enddate = $rowBonusDetails['endDate'];
                }

               }
	
	for ($i=0; $i < $bonusData; $i++) { 

		if (trim($_POST['bonusrecordid'] != '')) {
            
            $employeeid = $_POST["employeeid"][$i];
            $bonusrecordid = $_POST["bonusrecordid"][$i];
            $bonus_value = $_POST["bonus_value"][$i];

            $bonus_adjustment = $_POST["bonus_adjustment"][$i];
            $adjustedBonusValue = $_POST["adjustedBonusValue"][$i];

            $edfamount = $_POST["edfamount"][$i];
            $payerate = $_POST["payerate"][$i];
            $edfmonths = $_POST["edfmonths"][$i];

            
            if ($bonus_adjustment == 0 || $bonus_adjustment == null) {
                $adjustedBonusValue = $bonus_value;
            }

            $calculatedPAYE = round(($adjustedBonusValue - ($edfamount / $edfmonths)) * ($payerate/100),0);
                
                $employeeCSG = getCSGEmployee(intval($adjustedBonusValue),$db);
                $employerCSG = getCSGEmployer(intval($adjustedBonusValue),$db);
                $employerPRGF = getPRGFEmployer(intval($adjustedBonusValue),$enddate,$companyID,$db);


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


            $queryToUpdateBonus = "UPDATE bonuspayouts SET bonusAmount = '$bonus_value',
            bonusAdjustment = '$bonus_adjustment',
            finalBonusValue = '$adjustedBonusValue',
            bonusPayeAmount = '$calculatedPAYE',
            employeeCSG = '$employeeCSG',
            employerCSG = '$employerCSG',
            employerPRGF = '$employerPRGF'
             WHERE bonusid = '$bonusrecordid'";

            if(mysqli_query($db, $queryToUpdateBonus)){
                echo "Bonus " . $bonusrecordid , " added successfully.<br>";
            } else {
                echo "Error <br> : " . mysqli_error($db);
            }
        }
        
	}

    header("location: viewBonusPayroll.php?payrollID=$bonuspayrollid");        

        include('footer1.php');
?>