<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/functions.php'); # general functions


if(isset($_REQUEST['staff_id']) && $_REQUEST['staff_id']!=''){	
	$staffId = mysqli_real_escape_string($con, trim($_REQUEST['staff_id']));
	$staffRs = mysqli_query($con, "SELECT name FROM tbl_staff WHERE id = '".$staffId."'");
	if(mysqli_num_rows($staffRs)>0){
		$resStaff = mysqli_fetch_array($staffRs);
	} else {
		$error = 'Staff does not exist!';
	}
}

if(isset($_POST['add'])) {
		
	if(isset($_POST['date']) && strlen($_POST['date'])>0) {
		$date = trim($_POST['date']);
	} else {
		$error = 'Please select date';
	}
	
	if(isset($_POST['amount']) && strlen($_POST['amount'])>0) {
		$amount = trim($_POST['amount']);
		if(is_numeric($amount) && $amount>0){
			//ok
		} else {
			$error = 'Enter a positive numeric value for amount';
		}
	} else {
		$error = 'Please enter advance amount';
	}	
	
	if(empty($error)) {
				
		if(empty($error)) {
			$insertQry = "INSERT INTO tbl_staff_advance(staff_id, amount, date, deducted) VALUES (
			'".mysqli_real_escape_string($con, $staffId)."',
			 '".mysqli_real_escape_string($con, $amount)."',			 
			 '".mysqli_real_escape_string($con, $date)."',	
			 'no')";
			
			$execInsert = mysqli_query($con,$insertQry);
			$insert_id = mysqli_insert_id($con);		
			
			if($execInsert) {
				$success = 'Advance Added Successfully';
				header('Location:index.php?page=advance_report&success='.$success);
				exit();			
			}
			
		}	
						
	} 
}



?>