<?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
*/
require_once('./classes/class.noseo_pagination.php'); # pagination class
include('includes/functions.php'); # general functions
$advanceAmount = 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 FROM tbl_staff_advance WHERE staff_id = '".$staffId."' AND deducted = 'no'");
	if(mysqli_num_rows($advanceRs)>0){
		while($advanceRec = mysqli_fetch_array($advanceRs)){
			$advanceAmount += $advanceRec['amount'];
		}
		$netAmount = $basicSalary-$advanceAmount;
	}*/	
} else {
	header('Location:index.php?page=manage_staff');
	exit();
}

$pageVars = '';
// Get All User details to list 
$where = ' id>0 ';

if(isset($_POST['add'])) {
	
	if(isset($_POST['description']) && $_POST['description']!='') {
		$description = trim($_POST['description']);
	} else {
		$description = '';
	}
		
	if(isset($_POST['advance_amount']) && $_POST['advance_amount']>0) {
		$advanceAmount = trim($_POST['advance_amount']);
	} else {
		$error = 'Amount should be larger than 0';
	}
	
	if(isset($_POST['staff']) && strlen($_POST['staff'])>0) {
		$staffId = trim($_POST['staff']);
	} else {
		$error = 'Please select staff';
	}	
	
	if(empty($error)) {	
				
		if(empty($error)) {
			$insertQry = "INSERT INTO tbl_staff_advance(staff_id, amount, date, deducted, description, added_by) VALUES (
			'".mysqli_real_escape_string($con, $staffId)."',
			 '".mysqli_real_escape_string($con, $advanceAmount)."',
			 '".$curDate."',
			 'no',
			 '".mysqli_real_escape_string($con, $description)."',	
			 '".$_SESSION['adminId']."')";
			
			$execInsert = mysqli_query($con,$insertQry);
			if($execInsert){
				
				//insert in to expense table
				mysqli_query($con, "INSERT INTO tbl_expenses (cat_id, amount, exp_date, date, added_by) VALUES ('6', '".$advanceAmount."', '".$curDate."', '".$curDate."','".mysqli_real_escape_string($con, $_SESSION['adminId'])."')");		
									
				$success = 'Advance Salary Added Successfully';
				header('Location:index.php?page=add_salary_advance&staff_id='.$staffId.'&success='.$success);
				exit();	
			}				
		}							
	} 
}

if(isset($_POST['staff']) && strlen($_POST['staff'])>0) {
	$staffId = trim($_POST['staff']);
	$where.= " AND staff_id = '".$staffId."' ";
} else if(isset($_REQUEST['staff_id']) && strlen($_REQUEST['staff_id'])>0) {
	$staffId = trim($_REQUEST['staff_id']);
	$where.= " AND staff_id = '".$staffId."' ";
}

$select = "SELECT * FROM tbl_staff_advance WHERE ".$where." ORDER BY id DESC";
//echo $select;
// Using pagination class
$pagination = new pagination_noseo($select);
$execDetails = $pagination->createPages(20);

$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
$pageNums = $pagination->pageNumber(5, $pageLnk);

?>