<?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
$advanceAmount = 0;
$advanceReturnAmount = 0;
$netAmount = 0;
$curDate = date('Y-m-d H:i:s');

//get staff list
$staffListRs = mysqli_query($con, "SELECT id, name FROM tbl_staff WHERE status = 'Active'");

if(isset($_REQUEST['staff_id']) && $_REQUEST['staff_id']!=''){	
	$staffId = mysqli_real_escape_string($con, trim($_REQUEST['staff_id']));
	$staffRs = mysqli_query($con, "SELECT name, salary FROM tbl_staff WHERE id = '".$staffId."'");
	if(mysqli_num_rows($staffRs)>0){
		$resStaff = mysqli_fetch_array($staffRs);
		$basicSalary = $resStaff['salary']; 
		$netAmount = $basicSalary;
	} else {
		$error = 'Staff does not exist!';
	}
	
	//get advance amount given to staff if any
	$advanceRs = mysqli_query($con, "SELECT amount, advance_return FROM tbl_staff_advance WHERE staff_id = '".$staffId."'");
	if(mysqli_num_rows($advanceRs)>0){
		while($advanceRec = mysqli_fetch_array($advanceRs)){
			$advanceAmount += $advanceRec['amount'];
			$advanceReturnAmount += $advanceRec['advance_return'];
		}
		$advanceAmount = $advanceAmount-$advanceReturnAmount;
		$netAmount = $basicSalary-$advanceAmount;
	}	
} else {
	header('Location:index.php?page=manage_staff');
	exit();
}

if(isset($_POST['add'])) {
	
	//print_r($_POST); exit;
	$grossSalary = 0;
	
	
	if(isset($_POST['advance_return']) && strlen($_POST['advance_return'])>0) {
		$advance_return = trim($_POST['advance_return']);
	} else {
		$advance_return = 0;
	}
	
	if(isset($_POST['advances']) && strlen($_POST['advances'])>0) {
		$advances = trim($_POST['advances']);
	} else {
		$advances = 0;
	}
	
	if(isset($_POST['deductions']) && strlen($_POST['deductions'])>0) {
		$deductions = trim($_POST['deductions']);
	} else {
		$deductions = 0;
	}
	
	if(isset($_POST['insentives']) && strlen($_POST['insentives'])>0) {
		$insentives = trim($_POST['insentives']);
	} else {
		$insentives = 0;
	}
	
	if(isset($_POST['tot_leaves']) && strlen($_POST['tot_leaves'])>0) {
		$tot_leaves = trim($_POST['tot_leaves']);
	} else {
		$tot_leaves = 0;
	}	
	
	if(isset($_POST['salary_date']) && strlen($_POST['salary_date'])>0) {
		$salaryDate = trim($_POST['salary_date']);
	} else {
		$error = 'Please select Salary Date';
	}
		
	if(isset($_POST['month']) && strlen($_POST['month'])>0) {
		$month = trim($_POST['month']);
	} else {
		$error = 'Please select Month';
	}
	
	if(isset($_POST['year']) && strlen($_POST['year'])>0) {
		$year = trim($_POST['year']);
	} else {
		$error = 'Please select Year';
	}
	
	if(isset($_POST['salary']) && strlen($_POST['salary'])>0) {
		$salary = trim($_POST['salary']);
	} else {
		$error = 'Please set salary for the staff in manage staff';
	}
	
	if(isset($_POST['staff']) && strlen($_POST['staff'])>0) {
		$staffId = trim($_POST['staff']);
	} else {
		$error = 'Please select staff';
	}	
	
	if(empty($error)) {	
		
		//get basic salary of staff
		$staffBasicSalary = getStaffSalary($staffId);
		if($staffBasicSalary>0){
			$grossSalary = ($staffBasicSalary+$insentives) - ($deductions+$advance_return);
		} else {
			$error = 'Please set salary for the staff in manage staff';
		}
		
		//check if already added for this staff for this month
		$chkRs = mysqli_query($con, "SELECT id FROM tbl_staff_salary WHERE staff_id = '".$staffId."' AND year = '".$year."' AND month = '".$month."'");
		if(mysqli_num_rows($chkRs)>0){
			$error = 'Salary already added for the month!';
		}
		
		//gross salary
		
		
		if(empty($error)) {
			$insertQry = "INSERT INTO tbl_staff_salary(staff_id, year, month, salary_date, basic, insentives, deductions, advances, advance_return, tot_leaves, gross_salary, added_by, added_date) VALUES (
			'".mysqli_real_escape_string($con, $staffId)."',
			 '".mysqli_real_escape_string($con, $year)."',
			 '".mysqli_real_escape_string($con, $month)."',
			 '".mysqli_real_escape_string($con, $salaryDate)."',	
			 '".mysqli_real_escape_string($con, $staffBasicSalary)."',	
			 '".mysqli_real_escape_string($con, $insentives)."',	
			 '".mysqli_real_escape_string($con, $deductions)."',
			 '".mysqli_real_escape_string($con, $advances)."',	
			 '".mysqli_real_escape_string($con, $advance_return)."',	
			 '".mysqli_real_escape_string($con, $tot_leaves)."',
			 '".mysqli_real_escape_string($con, $grossSalary)."',
			 '".mysqli_real_escape_string($con, $_SESSION['adminId'])."',
			 '".$curDate."')";
			
			$execInsert = mysqli_query($con,$insertQry);
			$insert_id = mysqli_insert_id($con);		
			
			if($execInsert) {
					
				if($advance_return>0){
					mysqli_query($con, "INSERT INTO tbl_staff_advance (staff_id, salary_id, date, deducted, advance_return, added_by) VALUES ('".$staffId."', '".$insert_id."', '".$curDate."', 'yes', '".$advance_return."', '".$_SESSION['adminId']."')");
				} 
				
				//insert in to expense table
				mysqli_query($con, "INSERT INTO tbl_expenses (cat_id, amount, exp_date, date, added_by) VALUES ('6', '".$grossSalary."', '".$salaryDate."', '".$curDate."','".mysqli_real_escape_string($con, $_SESSION['adminId'])."')");		
						
		
				$success = 'Salary Added Successfully';
				header('Location:index.php?page=salary_report&success='.$success);
				exit();			
			}
			
		}	
						
	} 
}



?>