<?php
include('../include/session.php');

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 


$list_num = $_POST['list_num'];
$month = $_POST['month_select'];

$i=0;
while ($i<$list_num) {
	
if (isset($_POST['leave_checkbox'.$i]) ) {
	  $emp_id = $_POST['emp_id_text'.$i];
	  $allowance = $_POST['allowance_text'.$i];
	  
	  
	  	$table_name = $_SESSION['db_prefix']."_leave_records";
		$balance_query = "SELECT balance FROM $table_name WHERE emp_id = '".$emp_id."' ORDER BY leave_id DESC LIMIT 1";
		$balance_result = mySQL_query($balance_query);
		$balance_num = mysql_num_rows($balance_result);
		if ($balance_num!=0) {
		$balance = mysql_result($balance_result, 0, "balance");
		}
		else {
			$balance=0;
		}
	 
	  
	  $balance=$balance+$allowance;
	  	$table_name = $_SESSION['db_prefix']."_leave_records";
		$leave_query = "INSERT INTO $table_name (emp_id, rec_date, days_plus, balance, reason ) VALUES ('".$emp_id."', '".$entry_date."', '".$allowance."', '".$balance."', 'month allowance ".$month."' ) ";
		execsql($leave_query);
	  
}
$i++;
}


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 	 window.location="../hr_module/hr_main.php?date=0";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	      window.location="leave_month_allowance.php"
	   </script>
        <?php		 
	   }
	   ?>

