<?php 	
require_once '../../Server/connection.php';
$valid['success'] = array('success' => false, 'messages' => array());
//$valid['success'] = array('success' => false, 'messages' => array(), 'staff_id' => '');

if($_POST){	

  $FirstName = addslashes($_POST['FirstName']);
  $MiddleName = addslashes($_POST['MiddleName']);
  $LastName = addslashes($_POST['LastName']);
  $Email = addslashes(strtolower($_POST['Email']));
  $PhoneNumber = addslashes($_POST['PhoneNumber']);
  $staffcateId = $_POST['staffcate_id'];
  $upl_status = $_POST['upl_status'];
  
  $bankName = $_POST['bankName'];
  $bankAccountNo = $_POST['bankAccountNo'];
  
  $emptype_id = $_POST['emptype_id'];
  $eduLevel_id = $_POST['eduLevel_id'];
  
  $heslb_status = $_POST['heslb_status'];
  
  
  
  
  //staff gross salary (before Deductions)
  $basicSalary = $_POST['basicSalary'];
  
  if($heslb_status=='Yes'){
	 $HeslbStatus =1; 
	 $EmployeeHeslbContri = 0.15 * $basicSalary;
	 $EmployeeNssfContri = 0.1 * $basicSalary;
	 $EmployerNssfContri = 0.1 * $basicSalary;
	 $EmpNhifContri = 0.03 * $basicSalary;
	 if($EmpNhifContri>40000){
	 $EmployeeNhifContri = $EmpNhifContri;
	 $EmployerNhifContri = 0.03 * $basicSalary;
	 $EmployerWcfContri = 0.006 * $basicSalary;
	 }else{
	 $EmployeeNhifContri = 0;
     $EmployerNhifContri = 40000;
     $EmployerWcfContri = 0.006 * $basicSalary;	 
	 }
	 $taxableAmount = $basicSalary -($EmployeeNssfContri+$EmployeeNhifContri);
	 if($taxableAmount>0){
		 $paye = 0;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>270000){
		 $paye = ($taxableAmount - 270000)* 0.08;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>520000){
		 $paye = (($taxableAmount - 520000)* 0.2) + 22500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>760000){
		 $paye = (($taxableAmount - 760000)* 0.25) + 70500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else{
		 $paye = (($taxableAmount - 1000000)* 0.3) + 130500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }
  }else{
	 $HeslbStatus =0;  
	 $EmployeeHeslbContri =0;
	 $EmployeeNssfContri = 0.1 * $basicSalary;
	 $EmployerNssfContri = 0.1 * $basicSalary;
	 $EmpNhifContri = 0.03 * $basicSalary;
	 if($EmpNhifContri>40000){
	 $EmployeeNhifContri = $EmpNhifContri;
	 $EmployerNhifContri = 0.03 * $basicSalary;
	 $EmployerWcfContri = 0.006 * $basicSalary;
	 }else{
	 $EmployeeNhifContri = 0;
     $EmployerNhifContri = 0.03 * $basicSalary;
	 $EmployerWcfContri = 0.006 * $basicSalary;	 
	 }
	 $taxableAmount = $basicSalary -($EmployeeNssfContri+$EmployeeNhifContri);
	 if($taxableAmount>0){
		 $paye = 0;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>270000){
		 $paye = ($taxableAmount - 270000)* 0.08;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>520000){
		 $paye = (($taxableAmount - 520000)* 0.2) + 22500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>760000){
		 $paye = (($taxableAmount - 760000)* 0.25) + 70500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else{
		 $paye = (($taxableAmount - 1000000)* 0.3) + 130500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }
  }
  
 
  
  if($staffcateId==1){
  $deptId = $_POST['dept_id'];
  $sectId = $_POST['sect_id'];
  $desId = $_POST['des_id'];
  }else if($staffcateId==2){
  $deptId = $_POST['depart_id'];
  $sectId = $_POST['section_id'];
  $desId = $_POST['desi_id'];  
  }else if($staffcateId==3){
  $deptId = $_POST['deptId'];
  $sectId = " ";
  $desId = $_POST['desId'];  
  }else{
  $deptId = " ";
  $sectId = " ";
  $desId = $_POST['designId'];  
  }
  
    //$query153 = "select * from salaryscales where des_id='$desId'";
    //$result153 = $conn->query($query153);
    //$row153 = $result153->fetch_array();
    //$Salary = $row153['grossSalary'];
  
  
  $gender = $_POST['gender_id'];
  $marital = $_POST['marital_id'];
  $status_id = 1;
  $passwd = addslashes(strtoupper($LastName.'123'));
  $password = md5(addslashes(strtoupper($LastName.'123')));
  
  $birthDate = $_POST['birthDate'];
  $employDate = $_POST['employDate'];
  
  $birth_date = date('Y-m-d', strtotime($birthDate));
  $employment_date = date('Y-m-d', strtotime($employDate));
  
  
  
  //$link = "<a href='https://ems.edgeec.co.tz/'>EMS</a>"; 
  
  //$fromEmail = 'info@edgeec.co.tz';
  //$toEmail = $Email;
  
  $n = substr($PhoneNumber,1);
  $n1 = '255';
  $phone_number = $n1.$n;
  
  $staff_image = '../../staff_pictures/76327192860902d3d98acc.jpg';
  
  
  $sql22 = "Select * from staff";
  $result22 = $conn->query($sql22);
  $num22 = mysqli_num_rows($result22);
  
  if($num22>=0 && $num22<9){
  $staff_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BRVSN"."/".$y."/".'000'.$staff_No;  
    }else if($num22>=9 && $num22<99){
   $staff_No = $num22 + 1;
  $y = date("Y");
  $staffNo = "BRVSN"."/".$y."/".'00'.$staff_No;  	 
	 }else if($num22>=99 && $num22<999){
   $staff_No = $num22 + 1;
  $y = date("Y");
  $staffNo = "BRVSN"."/".$y."/".'0'.$staff_No;  	 
	 }else{
	$staff_No = $num22 + 1;
  $y = date("Y");
  $staffNo = "BRVSN"."/".$y."/".$staff_No;  	 
	 }
  
  
  
   //$message = "Hello"."&nbsp&nbsp".$FirstName."&nbsp&nbsp".$LastName."&nbsp&nbsp"."You have been registered into edge engineering Employee Management System - EMS, You will login using your email"."&nbsp&nbsp".$Email."&nbsp&nbsp"."and your default password will be"."&nbsp&nbsp".$passwd."&nbsp&nbsp"."You can change your password after login"."&nbsp&nbsp"."Please Click the following link to login into the System"."&nbsp&nbsp".$link;
   //$subject = "User Account Creation";
  
 
				
				$sql = "INSERT INTO staff (firstname,middlename, lastname,staffId,gender_id,email,phone_number,birth_date,emply_date,marital_id,dept_id,sect_id,des_id,eduLevel_id,emply_type_id,staffcate_id,status_id,BasicSalary,EmployeeNssfContri,EmployeeNhifContri,taxableAmount,Paye,NetPay,EmployerNhifContri,EmployerWcfContri,EmployerNssfContri,heslbBeneficiaryStatus,bankName,BankAccountNo,staff_image) 
				VALUES ('$FirstName', '$MiddleName', '$LastName','$staffNo','$gender','$Email','$phone_number','$birth_date','$employment_date','$marital','$deptId','$sectId','$desId','$eduLevel_id','$emptype_id','$staffcateId','$status_id','$basicSalary','$EmployeeNssfContri','$EmployeeNhifContri','$taxableAmount','$paye','$netPay','$EmployerNhifContri','$EmployerWcfContr','$EmployerNssfContri','$HeslbStatus','$bankName','$bankAccountNo','$staff_image')";
				
				
				$result = $conn->query($sql);
				
				if($result){
					
			$staff_id = $conn->insert_id;
				
				  if($upl_status=='Yes'){
  foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql12 = "INSERT INTO staffdocs (staff_id,fileName) 
				VALUES ('$staff_id','$location')";

		$resFSql12 = $conn->query($fSql12);
	          }
			
          		
			  
              }
			  
			  for($x = 0; $x < count($_POST['inst_name']); $x++) {			
		

				// add into order_item
				$procRequestItemSql = "INSERT INTO edu_background (eduLevel_id,staff_id,inst_name,edu_period) 
				VALUES ('".$_POST['edulevel_id'][$x]."','$staff_id','".$_POST['inst_name'][$x]."','".$_POST['edu_period'][$x]."')";

				$resFSql13 = $conn->query($procRequestItemSql);		

				if($x == count($_POST['inst_name'])) {
					$procRequestItemStatus = true;
					
					
				}	

		
	            } // /for quantity
				
				
				if($emptype_id==2){
  $contStart = $_POST['contStart']; 
  $contEnd = date('Y-m-d', strtotime($contStart.' +6 month')); 
  $duration = 6;  
  
  $fSql120 = "INSERT INTO staff_contracts (staff_id,contract_start,contract_end,duration) 
				VALUES ('$staff_id','$contStart','$contEnd','$duration')";

		        $resFSql120 = $conn->query($fSql120);
  }else if($emptype_id==3){
  $contStart = $_POST['contStart']; 
  $contEnd = date('Y-m-d', strtotime($contStart.' +12 month'));
  $duration = 12;
  $fSql120 = "INSERT INTO staff_contracts (staff_id,contract_start,contract_end,duration) 
				VALUES ('$staff_id','$contStart','$contEnd','$duration')";

		        $resFSql120 = $conn->query($fSql120);  
  }else{
  $contStart = $_POST['contStart']; 
  $contEnd = date('Y-m-d', strtotime($contStart.' +12 month'));
  $duration = 12;
  $fSql120 = "INSERT INTO staff_contracts (staff_id,contract_start,contract_end,duration) 
				VALUES ('$staff_id','$contStart','$contEnd','$duration')";

		        $resFSql120 = $conn->query($fSql120);  
       }
				
               }
				
				if($resFSql12&&$resFSql13&&$resFSql120){
					
					
				$valid['success'] = true;
			    $valid['messages'] = "Staff is Successfully Registered";
				
					
				}
				
				

	$conn->close();

	echo json_encode($valid);
 
} // /if $_POST