<?php 	
session_start();	
require_once '../../Server/connection.php';
if($_POST){
	
  $FirstName = addslashes($_POST['fname']);
  $MiddleName = addslashes($_POST['mname']);
  $LastName = addslashes($_POST['lname']);
  $Email = addslashes(strtolower($_POST['email']));
  $tin = $_POST['tin'];
  $nssf_no = $_POST['nssf_no'];
  $nhif_no = $_POST['nhif_no'];
  $gender = $_POST['gender_id'];
  $PhoneNumber = addslashes($_POST['phone']);
  $n = substr($PhoneNumber,1);
  $n1 = '255';
  $phone_number = $n1.$n;
  $birthDate = $_POST['bdate'];
  $employDate = $_POST['empdate'];
  $birth_date = date('Y-m-d', strtotime($birthDate));
  $employment_date = date('Y-m-d', strtotime($employDate));
  $marital = $_POST['marital_id'];
  $rel_status = $_POST['rel_status'];
  
  if($rel_status=='Have Religion'){
  $religion_id = $_POST['relId'];
  $denomination_id = $_POST['denId'];   
  }
  
  $staffcateId = $_POST['staffcate_id'];
  
  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'];  
  }
  
  $emptype_id = $_POST['emptype_id'];
  $edu_level_id = $_POST['edu_level_id'];
  $upl_status = $_POST['upl_status'];
  $dep_status = $_POST['dep_status'];
  $parent_status = $_POST['parent_status'];
  $kin_status = $_POST['kin_status'];
  
  
  if($emptype_id==2){
  $contStart = $_POST['contStart'];
  $cont_dur_id = $_POST['cont_dur_id'];  
  $contEnd = date('Y-m-d', strtotime($contStart.' +$cont_dur_id month')); 
  
  
  $fSql120 = "INSERT INTO staff_contracts (staff_id,contract_start,contract_end,cont_dur_id,cont_status) 
				VALUES ('$staff_id','$contStart','$contEnd','$cont_dur_id','Renew Contract')";

  $resFSql120 = $conn->query($fSql120);
  }
  
  $region_id = $_POST['region_id'];
  $district_id = $_POST['district_id'];
  $ward_id = $_POST['ward_id'];
  
  
  
  $bankName = $_POST['bankName'];
  $bankAccountNo = $_POST['bankAccountNo'];
  $heslb_status = $_POST['heslb_status'];
  $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;
	 $EmployerSdlContri = 0.04 * $basicSalary;
	 }else{
	 $EmployeeNhifContri = 0;
     $EmployerNhifContri = 40000;
     $EmployerWcfContri = 0.006 * $basicSalary;	
     $EmployerSdlContri = 0.04 * $basicSalary;	 
	 }
	 $taxableAmount = $basicSalary -($EmployeeNssfContri+$EmployeeNhifContri);
	 if($taxableAmount>270000 && $taxableAmount<=520000){
		 $paye = ($taxableAmount - 270000)* 0.08;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>520000 && $taxableAmount<=760000){
		 $paye = (($taxableAmount - 520000)* 0.2) + 22500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>760000 && $taxableAmount<=1000000){
		 $paye = (($taxableAmount - 760000)* 0.25) + 70500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>1000000){
		 $paye = (($taxableAmount - 1000000)* 0.3) + 130500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else{
		 $paye = 0;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }
  }else{
	 $HeslbStatus =2;  
	 $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;
	 $EmployerSdlContri = 0.04 * $basicSalary;
	 }else{
	 $EmployeeNhifContri = 0;
     $EmployerNhifContri = 0.03 * $basicSalary;
	 $EmployerWcfContri = 0.006 * $basicSalary;
     $EmployerSdlContri = 0.04 * $basicSalary;	 
	 }
	 $taxableAmount = $basicSalary -($EmployeeNssfContri+$EmployeeNhifContri);
	 if($taxableAmount>270000 && $taxableAmount<=520000){
		 $paye = ($taxableAmount - 270000)* 0.08;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>520000 && $taxableAmount<=760000){
		 $paye = (($taxableAmount - 520000)* 0.2) + 22500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>760000 && $taxableAmount<=1000000){
		 $paye = (($taxableAmount - 760000)* 0.25) + 70500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>1000000){
		 $paye = (($taxableAmount - 1000000)* 0.3) + 130500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else{
		 $paye = 0;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }
  }
  
  
  
  
  $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 = "LUP"."/".$y."/".'000'.$staff_No;  
    }else if($num22>=9 && $num22<99){
   $staff_No = $num22 + 1;
  $y = date("Y");
  $staffNo = "LUP"."/".$y."/".'00'.$staff_No;  	 
	 }else if($num22>=99 && $num22<999){
   $staff_No = $num22 + 1;
  $y = date("Y");
  $staffNo = "LUP"."/".$y."/".'0'.$staff_No;  	 
	 }else{
	$staff_No = $num22 + 1;
  $y = date("Y");
  $staffNo = "LUP"."/".$y."/".$staff_No;  	 
	 }
	
	
	  $sql = "INSERT INTO staff (firstname,middlename,lastname,staffId,staff_tin,nssf_no,nhif_no,gender_id,email,phone_number,birth_date,emply_date,marital_id,dept_id,sect_id,des_id,eduLevel_id,emply_type_id,cont_dur_id,staffcate_id,status_id,rel_status,dep_status,parent_status,kin_status,religion_id,denom_id,region_id,district_id,ward_id,BasicSalary,EmployeeNssfContri,EmployeeNhifContri,taxableAmount,Paye,NetPay,EmployerNhifContri,EmployerWcfContri,EmployerNssfContri,EmployerSdlContri,HesBenId,bankName,BankAccountNo,staff_image) 
				VALUES ('$FirstName', '$MiddleName', '$LastName','$staffNo','$tin','$nssf_no','$nhif_no','$gender','$Email','$phone_number','$birth_date','$employment_date','$marital','$deptId','$sectId','$desId','$eduLevel_id','$emptype_id','$cont_dur_id','$staffcateId','$status_id','$rel_status','$dep_status','$parent_status','$kin_status','$religion_id','$denomination_id','$region_id','$district_id','$ward_id','$basicSalary','$EmployeeNssfContri','$EmployeeNhifContri','$taxableAmount','$paye','$netPay','$EmployerNhifContri','$EmployerWcfContr','$EmployerNssfContri','$EmployerSdlContri','$heslb_status','$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++) {			
				$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;
					
					
				}
	            }

               if($parent_status=='Yes'){
                for($x1 = 0; $x1 < count($_POST['prel_id']); $x1++) {

                  $phnNo = $_POST['p_phone'];
				  $n = substr($phnNo,1);
                  $n1 = '255';
                  $phnNumber = $n1.$n;
					
				$procRequestItemSql1 = "INSERT INTO parents_guardians (prel_id,staff_id,pfname,pmname,plname,pphone,pemail) 
				VALUES ('".$_POST['prel_id'][$x1]."','$staff_id','".$_POST['pf_name'][$x1]."','".$_POST['pm_name'][$x1]."','".$_POST['pl_name'][$x1]."','$phnNumber','".$_POST['p_email'][$x1]."')";

				$resFSql131 = $conn->query($procRequestItemSql1);		

				if($x1 == count($_POST['prel_id'])) {
					$procRequestItemStatus1 = true;
					
					
				}
	            }

				}				


                if($kin_status=='Yes'){
                for($x2 = 0; $x2 < count($_POST['krel_id']); $x2++) {

                  $knNo = $_POST['kphone'];
				  $n111 = substr($knNo,1);
                  $n11 = '255';
                  $knNumber = $n11.$n111;
					
				$procRequestItemSql2 = "INSERT INTO next_kin (staff_id,kfname,kmname,klname,kemail,kphone,prel_id) 
				VALUES ('$staff_id','".$_POST['kf_name'][$x2]."','".$_POST['km_name'][$x2]."','".$_POST['kl_name'][$x2]."','".$_POST['k_email'][$x2]."','$knNumber','".$_POST['krel_id'][$x2]."')";

				$resFSql132 = $conn->query($procRequestItemSql2);		

				if($x2 == count($_POST['krel_id'])) {
					$procRequestItemStatus2 = true;
					
					
				}
	            } 	
				}

if($dep_status=='Yes'){
               for($x3 = 0; $x3 < count($_POST['drel_id']); $x3++) {

                  $dbDate = $_POST['db_date'];
				  $db_Date = date('Y-m-d', strtotime($dbDate));
				  
					
				$procRequestItemSql3 = "INSERT INTO dependants (dependant_rel_id,staff_id,dependant_fname,dependant_mname,dependant_lname,gender_id,dependant_birthDate) 
				VALUES ('".$_POST['drel_id'][$x3]."','$staff_id','".$_POST['df_name'][$x3]."','".$_POST['dm_name'][$x3]."','".$_POST['dl_name'][$x3]."','".$_POST['gender_id1'][$x3]."','$db_Date')";

				$resFSql133 = $conn->query($procRequestItemSql3);		

				if($x3 == count($_POST['drel_id'])) {
					$procRequestItemStatus3 = true;
					
					
				}
	            } 							
}	
				
				
				
               }
				
				if($resFSql12&&$resFSql13&&$resFSql131&&$resFSql132&&$resFSql133){
					
				$passwd = addslashes(strtoupper($LastName.'123'));
  $password = md5(addslashes(strtoupper($LastName.'123')));
  
  $link = "<a href='https://ems.edgeec.co.tz/'>CMS</a>"; 
  
  $fromEmail = 'info@brightvision.co.tz';
  $toEmail = $Email;
  $space = " ";
  
   $message = "Hello".$space.$FirstName.$space.$LastName.$space."You have been registered into Bright Vision Customer Management System - CMS, You will login using your email".$space.$Email.$space."and your default password will be".$space.$passwd.$space."You can change your password after login".$space."Please Click the following link to login into the System".$space.$link;
   $subject = "User Account Creation";
   
    $query150 = "select * from users where staff_id='$staff_id'";
   $result150 = $conn->query($query150);
   $num150 = mysqli_num_rows($result150);
   
    if($num150==0){
		
	$staffSql = "INSERT INTO users (staff_id,password,status,role_id) 
				VALUES ('$staff_id','$password','1','1')";
				
   $result23 = $conn->query($staffSql);
   
   if($result23){
					
			//if(@mail($toEmail,$subject,$message,"From:" . $fromEmail)){
					
			$_SESSION['success_message'] = "Staff has been registered Successfully";
                header("Location: ../createStaff.php");
                exit();
							 
			   // }
					
				}
		
		
	}
					
					
				
				
					
				}	
	


} 