<?php 

session_start();
 
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("stafffunction.php");
include("Functions_UserAccess.php");
include("staf_function.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");
 mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$actionmode=$_GET['actionmode'];
$mode=$_GET['mode'];

$staffcode			= $_GET['staffcode']; if($staffcode == '') $staffcode	= $_POST['staffcode'];
$firstname 			= $_POST['firstname'];  if($firstname == '') $firstname	= $_GET['firstname'];
$lastname 			= $_POST['lastname'];  if($lastname == '') $lastname	= $_GET['lastname'];
$fathername 		= $_POST['fathername']; if($fathername == '') $fathername	= $_GET['fathername'];
$phone1 			= $_POST['phone1']; if($phone1 == '') $phone1	= $_GET['phone1'];
$phone2 			= $_POST['phone2']; if($phone2 == '') $phone2	= $_GET['phone2'];
$emailadress 		= $_POST['emailadress']; if($emailadress == '') $emailadress	= $_GET['emailadress'];
$nationality 		= $_POST['nationality']; if($nationality == '') $nationality	= $_GET['nationality'];
$curentpos 			= $_POST['curentpos']; if($curentpos == '') $curentpos	= $_GET['curentpos'];
$passportnumber 	= $_POST['passportnumber']; if($passportnumber == '') $passportnumber	= $_GET['passportnumber'];
$birthdate 			= $_POST['birthdate']; if($birthdate == '') $birthdate	= $_GET['birthdate'];
$religions 			= $_POST['religions']; if($religions == '') $religions	= $_GET['religions'];
$gender 			= $_POST['gender']; if($gender == '') $gender	= $_GET['gender'];
$bloodgroup 		= $_POST['bloodgroup']; if($bloodgroup == '') $bloodgroup	= $_GET['bloodgroup'];
$Staff_picture 		= $_POST['Staff_picture']; if($Staff_picture == '') $Staff_picture	= $_GET['Staff_picture'];
$inssurancenumber 	= $_POST['inssurancenumber']; if($inssurancenumber == '') $inssurancenumber	= $_GET['inssurancenumber'];
$inssurancedate 	= $_POST['inssurancedate']; if($inssurancedate == '') $inssurancedate	= $_GET['inssurancedate'];
$inssuranceexpire 	= $_POST['inssuranceexpire']; if($inssuranceexpire == '') $inssuranceexpire	= $_GET['inssuranceexpire'];
$passportdate 		= $_POST['passportdate']; if($passportdate == '') $passportdate	= $_GET['passportdate'];
$passportexpire 	= $_POST['passportexpire']; if($passportexpire == '') $passportexpire	= $_GET['passportexpire'];
$cardnumber 		= $_POST['cardnumber']; if($cardnumber == '') $cardnumber	= $_GET['cardnumber'];
$carddate 			= $_POST['carddate']; if($carddate == '') $carddate	= $_GET['carddate'];
$cardexpire 		= $_POST['cardexpire']; if($cardexpire == '') $cardexpire	= $_GET['cardexpire'];
$birthplace 		= $_POST['birthplace']; if($birthplace == '') $birthplace	= $_GET['birthplace'];
$visanumber 		= $_POST['visanumber']; if($visanumber == '') $visanumber	= $_GET['visanumber'];
$visadate 			= $_POST['visadate']; if($visadate == '') $visadate	= $_GET['visadate'];
$visaexpire 		= $_POST['visaexpire']; if($visaexpire == '') $visaexpire	= $_GET['visaexpire'];
$bankacc 			= $_POST['bankacc']; if($bankacc == '') $bankacc	= $_GET['bankacc'];
$bankname 			= $_POST['bankname']; if($bankname == '') $bankname	= $_GET['bankname'];
$quotaname 			= $_POST['quotaname']; if($quotaname == '') $quotaname	= $_GET['quotaname'];
$marialstatus 		= $_POST['marialstatus']; if($marialstatus == '') $marialstatus	= $_GET['marialstatus'];
$totalchildren 		= $_POST['totalchildren']; if($totalchildren == '') $totalchildren	= $_GET['totalchildren'];
$salaryamount 		= $_POST['salaryamount']; if($salaryamount == '') $salaryamount	= $_GET['salaryamount'];
$leavedays 			= $_POST['leavedays']; if($leavedays == '') $leavedays	= $_GET['leavedays'];
$visatype 			= $_POST['visatype']; if($visatype == '') $visatype	= $_GET['visatype'];
$tempnumber 		= $_POST['tempnumber']; if($tempnumber == '') $tempnumber	= $_GET['tempnumber'];
$tempdate 			= $_POST['tempdate']; if($tempdate == '') $tempdate	= $_GET['tempdate'];
$tempexpire 		= $_POST['tempexpire']; if($tempexpire == '') $tempexpire	= $_GET['tempexpire'];
$resigned 			= $_POST['resigned']; if($resigned == '') $resigned	= $_GET['resigned'];
$resigndate 		= $_POST['resigndate']; if($resigndate == '') $resigndate	= $_GET['resigndate'];
$project 			= $_POST['project']; if($project == '') $project	= $_GET['project'];
$quotaexpire 		= $_POST['quotaexpire']; if($quotaexpire == '') $quotaexpire	= $_GET['quotaexpire'];
$iban 				= $_POST['iban']; if($iban == '') $iban	= $_GET['iban'];
$sortcode 			= $_POST['sortcode']; if($sortcode == '') $sortcode	= $_GET['sortcode'];
$company 			= $_POST['company']; if($company == '') $company	= $_GET['company'];
$fullname 			= $_POST['fullname']; if($fullname == '') $fullname	= $_GET['fullname'];
$bankbranch 		= $_POST['bankbranch']; if($bankbranch == '') $bankbranch	= $_GET['bankbranch'];
$allowance 			= $_POST['allowance']; if($allowance == '') $allowance	= $_GET['allowance'];
$allowance2			= $_POST['allowance2']; if($allowance2 == '') $allowance2	= $_GET['allowance2'];
$accname 			= $_POST['accname']; if($accname == '') $accname	= $_GET['accname'];
$aliennumber 		= $_POST['aliennumber']; if($aliennumber == '') $aliennumber	= $_GET['aliennumber'];
$aliendate 			= $_POST['aliendate']; if($aliendate == '') $aliendate	= $_GET['aliendate'];
$alienexpire 		= $_POST['alienexpire']; if($alienexpire == '') $alienexpire	= $_GET['alienexpire'];
$resignreason		= $_POST['resignreason']; if($resignreason == '') $resignreason	= $_GET['resignreason'];
$famPackage			= $_POST['famPackage']; if($famPackage == '') $famPackage	= $_GET['famPackage'];

$staff_returned     = $_POST['staff_returned']; if($staff_returned == '') $staff_returned	= $_GET['staff_returned'];
$DepDate     		= $_POST['DepDate']; if($DepDate == '') $DepDate	= $_GET['DepDate'];
$DepTime     		= $_POST['DepTime']; if($DepTime == '') $DepTime	= $_GET['DepTime'];
$ArrivalDate     	= $_POST['ArrivalDate']; if($ArrivalDate == '') $ArrivalDate	= $_GET['ArrivalDate'];
$Arrival_time     	= $_POST['Arrival_time']; if($Arrival_time == '') $Arrival_time	= $_GET['Arrival_time'];
$Com_DepDate     	= $_POST['Com_DepDate']; if($Com_DepDate == '') $Com_DepDate	= $_GET['Com_DepDate'];
$Com_depTime     	= $_POST['Com_depTime']; if($Com_depTime == '') $Com_depTime	= $_GET['Com_depTime'];
$Com_ArrivalDate    = $_POST['Com_ArrivalDate']; if($Com_ArrivalDate == '') $Com_ArrivalDate	= $_GET['Com_ArrivalDate'];
$Com_ArrivalTime    = $_POST['Com_ArrivalTime']; if($Com_ArrivalTime == '') $Com_ArrivalTime	= $_GET['Com_ArrivalTime'];
$Airline_name     	= $_POST['Airline_name']; if($Airline_name == '') $Airline_name	= $_GET['Airline_name'];
$project_code     	= $_POST['project_code']; if($project_code == '') $project_code	= $_GET['project_code'];
$ticket_id 			= $_POST['ticket_id']; if($ticket_id == '') $ticket_id	= $_GET['ticket_id'];

$birthdate 			= formatdate($birthdate);
$resigndate 		= formatdate($resigndate);
$employedate 		= formatdate($employedate); 
$passportdate 		= formatdate($passportdate);
$passportexpire 	= formatdate($passportexpire);
$inssurancedate 	= formatdate($inssurancedate);
$inssuranceexpire	= formatdate($inssuranceexpire);
$carddate 			= formatdate($carddate);
$cardexpire 		= formatdate($cardexpire);
$tempdate 			= formatdate($tempdate);
$tempexpire 		= formatdate($tempexpire);
$visadate 			= formatdate($visadate);
$visaexpire 		= formatdate($visaexpire);
$aliendate 			= formatdate($aliendate);
$alienexpire		= formatdate($alienexpire);
$quotaexpire		= formatdate($quotaexpire);


$DepDate			= formatdate($DepDate);
$ArrivalDate		= formatdate($ArrivalDate);
$Com_DepDate		= formatdate($Com_DepDate);
$Com_ArrivalDate	= formatdate($Com_ArrivalDate);
if($staff_returned == '') $staff_returned = 0;
if ($famPackage	== '') $famPackage = 0;

$sal_desc 		= $_GET['sal_desc'];
$sal_startdate 	= $_GET['sal_startdate'];
$sal_enddate 	= $_GET['sal_enddate'];
$sal_amount 	= $_GET['sal_amount'];
$salary_id 		= $_GET['salary_id'];

$sal_startdate 	= formatdate($sal_startdate);
$sal_enddate 	= formatdate($sal_enddate);

if ($actionmode == 'insertsalary' && $salary_id == ''){
	$queryins = "insert into staff_salary (EmployeeID, Description, date_value, Todate, Amount) values 
					('$staffcode', ".strformat($sal_desc).",".strformat($sal_startdate).",".strformat($sal_enddate).",".strformat($sal_amount)." ) ";
	$resins = mysql_query($queryins, $connection);
	echo "[BRK]";
	salarygrid($staffcode, $connection);
	echo "[BRK]";
	exit;
}
else if ($actionmode == 'updatesalary' && $salary_id != ''){
	$queryup = "update staff_salary set EmployeeID = '$staffcode', Description = ".strformat($sal_desc).", date_value = ".strformat($sal_startdate).", 
					Todate = ".strformat($sal_enddate).", Amount = ".strformat($sal_amount)." where AUTO_INC = $salary_id ";
	$resup = mysql_query($queryup, $connection);
	echo "[BRK]";
	salarygrid($staffcode, $connection);
	echo "[BRK]";
	exit;
}
else if ($actionmode == 'deleteSalary' && $salary_id != ''){
	$querydel = "delete from staff_salary where AUTO_INC = $salary_id ";
	$resdel = mysql_query($querydel, $connection);
	echo "[BRK]";
	salarygrid($staffcode, $connection);
	echo "[BRK]";
	exit;
}
if($mode != '' && $mode != 'deleteticket' )	{
	$staffcodequery="select staff_code from staff where staff_code='$staffcode'";
	$staffres=mysql_query($staffcodequery,$connection);
	$numr = mysql_num_rows($staffres);
	if($numr > 0 )  $actionmode = 'edit';
	else $actionmode = 'insert';
}
	
if($actionmode == 'edit') {
	$staffcodequery="select staff_code from staff where staff_code='$staffcode'";
	$staffres=mysql_query($staffcodequery,$connection);
	if($staffrtable = mysql_fetch_row($staffres)){
		$quotaquery = "select Staff_quota from staff where staff_code='$staffcode'; ";	
		$resquota = mysql_query($quotaquery,$connection);
		$rwquota = mysql_fetch_row($resquota);
		$oldquota = $rwquota[0];
		
	$updatestaff="update staff set Staff_code='$staffcode', Staff_firstName=".strformat($firstname).", Staff_lastName=".strformat($lastname).", 
			Staff_fatherName=".strformat($fathername).",Staff_phone1=".strformat($phone1).", Staff_phone2=".strformat($phone2).", Staff_email=".strformat($emailadress).", 
			Staff_nationality=".strformat($nationality).", Staff_Jobtitle=".strformat($curentpos).",Staff_PassportNb=".strformat($passportnumber).",
			Staff_Dob=".strformat($birthdate).", Staff_religion=".strformat($religions).", Staff_beginingDate=".strformat($employedate).", Staff_gender=".strformat($gender).", 
			Staff_bloodgrp=".strformat($bloodgroup).",";
	
	$updatestaff = $updatestaff." Staff_prvjob=".strformat($prvjob).", 
			Staff_remark=".strformat($resignreason).", Staff_InsNb=".strformat($inssurancenumber).", Staff_Insissuedate=".strformat($inssurancedate).", Staff_InsExpDate=".strformat($inssuranceexpire).", 
			Staff_passIssueDate=".strformat($passportdate).", Staff_PassExpDate=".strformat($passportexpire).", Staff_Rpnb=".strformat($cardnumber).", Staff_Rpissuedate=".strformat($carddate).", 
			Staff_rpexpdate=".strformat($cardexpire).", Staff_placeofbirth=".strformat($birthplace).", Staff_visaNo=".strformat($visanumber).", Staff_visaIssueDate=".strformat($visadate).",
			Staff_visaExpDate=".strformat($visaexpire).", Staff_bankAcc=".strformat($bankacc).", Staff_bankName=".strformat($bankname).", Staff_quota=".strformat($quotaname).", Staff_MaritalStatus=".strformat($marialstatus).",
			Staff_totalChildren=".strformat($totalchildren).", Staff_salary=".strformat($salaryamount).", Staff_leaveDays=".strformat($leavedays).", Staff_visatype=".strformat($visatype).", staff_TempNo=".strformat($tempnumber).",
			staff_TempIssueDate=".strformat($tempdate).", staff_TempExpDate=".strformat($tempexpire).", staff_Resigned=".strformat($resigned).", staff_ResignedDate=".strformat($resigndate).", project_code=".strformat($project).", 
			Staff_quotaExpired=".strformat($quotaexpire).", staff_IBAN=".strformat($iban).", 
			staff_SortCode=".strformat($sortcode).", Staff_company=".strformat($company).", Staff_beginningDate=".strformat($employedate).", Staff_fullName=".strformat($fullname).", Staff_bankbranch=".strformat($bankbranch).", 
			Staff_localallowance=".strformat($allowance).", Staff_defaulAll=".strformat($allowance2).",	Staff_accname=".strformat($accname).",
			Staff_AllienNo=".strformat($aliennumber).", Staff_AllienFrom=".strformat($aliendate).", Staff_AllienTo=".strformat($alienexpire)." , staff_familyPackage = ".strformat($famPackage)." 
			where staff_code='$staffcode'";
		//echo $updatestaff;exit;
	
	$queryupd=mysql_query($updatestaff,$connection);
	
	
	if($queryupd){
	if($quotaname != $oldquota){
		$updatequota = "update quota set quota_used=0 where quota_code='$oldquota';";
		$resquota = mysql_query($updatequota,$connection);
	}	
	if($quotaname != '' ){
		$updatequota = "update quota set quota_used=1 where quota_code='$quotaname';";
		$resquota = mysql_query($updatequota,$connection);
	}
	}
	
	} 
}
else if($actionmode == 'insert'){
$insertastaff="insert into staff ( Staff_code, Staff_firstName, Staff_lastName, Staff_fatherName, Staff_phone1, Staff_phone2,
			Staff_email, Staff_nationality, Staff_Jobtitle, Staff_PassportNb,
			Staff_Dob, Staff_religion, Staff_beginingDate, Staff_gender, 
			Staff_bloodgrp,Staff_prvjob, 
			Staff_remark, Staff_InsNb, Staff_Insissuedate, Staff_InsExpDate, 
			Staff_passIssueDate, Staff_PassExpDate, Staff_Rpnb, Staff_Rpissuedate, 
			Staff_rpexpdate, Staff_placeofbirth, Staff_visaNo, Staff_visaIssueDate,
			Staff_visaExpDate, Staff_bankAcc, Staff_bankName, Staff_quota, Staff_MaritalStatus,
			Staff_totalChildren, Staff_salary, Staff_leaveDays, Staff_visatype, staff_TempNo,
			staff_TempIssueDate, staff_TempExpDate, staff_Resigned, staff_ResignedDate, project_code, 
			Staff_quotaExpired, staff_IBAN,staff_SortCode, Staff_company, Staff_beginningDate, Staff_fullName, Staff_bankbranch,  
			Staff_localallowance, Staff_defaulAll,	Staff_accname, Staff_AllienNo, Staff_AllienFrom, Staff_AllienTo, staff_familyPackage )
			values (
			'$staffcode', ".strformat($firstname).", ".strformat($lastname).", ".strformat($fathername).",".strformat($phone1).", ".strformat($phone2).", 
			".strformat($emailadress).", ".strformat($nationality).", ".strformat($curentpos).",".strformat($passportnumber).",
			".strformat($birthdate).", ".strformat($religions).", ".strformat($employedate).", ".strformat($gender).", 
			".strformat($bloodgroup).", ".strformat($prvjob).", 
			".strformat($resignreason).", ".strformat($inssurancenumber).", ".strformat($inssurancedate).", ".strformat($inssuranceexpire).", 
			".strformat($passportdate).", ".strformat($passportexpire).", ".strformat($cardnumber).", ".strformat($carddate).", 
			".strformat($cardexpire).", ".strformat($birthplace).", ".strformat($visanumber).", ".strformat($visadate).",
			".strformat($visaexpire).", ".strformat($bankacc).", ".strformat($bankname).", ".strformat($quotaname).", ".strformat($marialstatus).",
			".strformat($totalchildren).", ".strformat($salaryamount).", ".strformat($leavedays).", ".strformat($visatype).", ".strformat($tempnumber).",
			".strformat($tempdate).", ".strformat($tempexpire).", ".strformat($resigned).", ".strformat($resigndate).", ".strformat($project).", 
			".strformat($quotaexpire).", ".strformat($iban).", 
			".strformat($sortcode).", ".strformat($company).", ".strformat($employedate).", ".strformat($fullname).", ".strformat($bankbranch).", 
			".strformat($allowance).", ".strformat($allowance2).",	".strformat($accname).",
			".strformat($aliennumber).", ".strformat($aliendate).", ".strformat($alienexpire).", ".strformat($famPackage)." )
			";
				
		//echo $insertastaff; exit;
		$queryres=mysql_query($insertastaff,$connection);
		if($quotaname != '' && $queryres ){
			$updatequota = "update quota set quota_used=1 where quota_code='$quotaname';";
			$resquota = mysql_query($updatequota,$connection);
		}
	}	

if($mode == 'insertticket')	{
	$insert = "insert into staff_ticket (Staff_code,staff_name, DepDate, DepTime, ArrivalDate, Arrival_time,
			Com_DepDate, Com_depTime, Com_ArrivalDate, Com_ArrivalTime, Airline_name, project_code, staff_returned, staff_destination)
			values ('$staffcode', ".strformat($firstname).", ".strformat($DepDate).", ".strformat($DepTime).", ".strformat($ArrivalDate)." , ".strformat($Arrival_time).",  
			".strformat($Com_DepDate).",  ".strformat($Com_depTime).", ".strformat($Com_ArrivalDate).",".strformat($Com_ArrivalTime).",	".strformat($Airline_name).",
			".strformat($project_code).", $staff_returned, ".strformat($staff_dest)." ) ";
	
	$res = mysql_query($insert,$connection);
} 
else if($mode == 'updateticket')	{
	$update = "update staff_ticket set Staff_code = '$staffcode', staff_name= ".strformat($firstname).", DepDate=".strformat($DepDate).",DepTime= ".strformat($DepTime).", 
			ArrivalDate = ".strformat($ArrivalDate)." , Arrival_time = ".strformat($Arrival_time).", Com_DepDate=".strformat($Com_DepDate).", 
			Com_depTime=".strformat($Com_depTime).", Com_ArrivalDate= ".strformat($Com_ArrivalDate).", Com_ArrivalTime = ".strformat($Com_ArrivalTime).",	
			Airline_name=".strformat($Airline_name).", project_code=".strformat($project_code).", staff_returned= $staff_returned,
			staff_destination = ".strformat($staff_dest)." where autoincrement = $ticket_id ";
	
	$res = mysql_query($update,$connection);
} else if($mode == 'deleteticket')	{ 
	$del = "delete from staff_ticket where autoincrement = $ticket_id";// echo $del;exit;
	$res = mysql_query($del,$connection);
}
if(is_uploaded_file($_FILES['image']['tmp_name'])) {
	 
	$fileType1 = $_FILES['image']['type'];$fileName1 = $_FILES['image']['name'];
	$tmpName1  = $_FILES['image']['tmp_name'];$fileSize1 = $_FILES['image']['size'];

	$dest_dir='./photos';
	$foldername = $dest_dir."/".$staffcode;

	if (!is_dir($foldername)){
		mkdir($foldername, 0700, true);}
					
	$dest1 = $foldername.'\\'.$fileName1;
	//echo "dest1".$dest1;
	$picture=$fileName1;

	$fp1 = fopen($tmpName1, 'r');
	$content1 = fread($fp1, $fileSize1);
	//$content1 = addslashes($content1);
	fclose($fp1);
if(!get_magic_quotes_gpc())
	$fileName1 = addslashes($fileName1);

$r1 = move_uploaded_file ($tmpName1, $dest1);
if($picture != ''){
		$updatestaff =  "update staff set Staff_picture='$picture' where staff_code='$staffcode'";
	//echo $updatestaff;exit;
$queryres=mysql_query($updatestaff,$connection);}
}
	
 if($actionmode==	'deletestaff'){
	$code=$_GET['code'];
	$staffcode=$_GET['staffcode'];
	$searcchcondition=$_GET['searcchcondition'];
	$deletestaff="delete from staff where staff_code='$code'";
	//echo deletestaff;
	$queryres=mysql_query($deletestaff,$connection);
	//echo $staffcode."dfG";
	if($queryres) $message = "Staff was Successfully deleted!";
	
}



function strformat($str){
	if($str == '') $str = "NULL";
	else $str = "'".ereg_replace("'", "\'", trim($str))."'";
	return $str;
}

function formatdate($dateval){
	if($dateval != ''){
	$ndate = explode("-", $dateval);
	$dateval = $ndate[2]."-".$ndate[1]."-".$ndate[0];
}
	return $dateval;
}
if($actionmode==	'deletestaff')
	header("Location:./Form_StaffSearch.php?message=$message");
else if ($mode != '')
	header("Location:./staff.php?staffcode=$staffcode&vTabFocus=Ticket");
else header("Location:./staff.php");
exit;

	?>